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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
4f917be24a5667d959a858f391aac11f7e28d0d1 | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/group_theory/submonoid/operations.lean | 3c70a1a6d5ba81f62a16e54be2066ba04ee03fa6 | [
"Apache-2.0"
] | permissive | stjordanis/mathlib | 51e286d19140e3788ef2c470bc7b953e4991f0c9 | 2568d41bca08f5d6bf39d915434c8447e21f42ee | refs/heads/master | 1,631,748,053,501 | 1,627,938,886,000 | 1,627,938,886,000 | 228,728,358 | 0 | 0 | Apache-2.0 | 1,576,630,588,000 | 1,576,630,587,000 | null | UTF-8 | Lean | false | false | 32,349 | 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, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard,
Amelia Livingston, Yury Kudryashov
-/
import group_theory.submonoid.basic
import data.equiv.mul_add
import algebra.group.prod
import algebra.group.inj_surj
/-!
# Operations on `submonoid`s
In this file we define various operations on `submonoid`s and `monoid_hom`s.
## Main definitions
### Conversion between multiplicative and additive definitions
* `submonoid.to_add_submonoid`, `submonoid.to_add_submonoid'`, `add_submonoid.to_submonoid`,
`add_submonoid.to_submonoid'`: convert between multiplicative and additive submonoids of `M`,
`multiplicative M`, and `additive M`. These are stated as `order_iso`s.
### (Commutative) monoid structure on a submonoid
* `submonoid.to_monoid`, `submonoid.to_comm_monoid`: a submonoid inherits a (commutative) monoid
structure.
### Group actions by submonoids
* `submonoid.mul_action`, `submonoid.distrib_mul_action`: a submonoid inherits (distributive)
multiplicative actions.
### Operations on submonoids
* `submonoid.comap`: preimage of a submonoid under a monoid homomorphism as a submonoid of the
domain;
* `submonoid.map`: image of a submonoid under a monoid homomorphism as a submonoid of the codomain;
* `submonoid.prod`: product of two submonoids `s : submonoid M` and `t : submonoid N` as a submonoid
of `M × N`;
### Monoid homomorphisms between submonoid
* `submonoid.subtype`: embedding of a submonoid into the ambient monoid.
* `submonoid.inclusion`: given two submonoids `S`, `T` such that `S ≤ T`, `S.inclusion T` is the
inclusion of `S` into `T` as a monoid homomorphism;
* `mul_equiv.submonoid_congr`: converts a proof of `S = T` into a monoid isomorphism between `S`
and `T`.
* `submonoid.prod_equiv`: monoid isomorphism between `s.prod t` and `s × t`;
### Operations on `monoid_hom`s
* `monoid_hom.mrange`: range of a monoid homomorphism as a submonoid of the codomain;
* `monoid_hom.mrestrict`: restrict a monoid homomorphism to a submonoid;
* `monoid_hom.cod_mrestrict`: restrict the codomain of a monoid homomorphism to a submonoid;
* `monoid_hom.mrange_restrict`: restrict a monoid homomorphism to its range;
## Tags
submonoid, range, product, map, comap
-/
variables {M N P : Type*} [mul_one_class M] [mul_one_class N] [mul_one_class P] (S : submonoid M)
/-!
### Conversion to/from `additive`/`multiplicative`
-/
section
/-- Submonoids of monoid `M` are isomorphic to additive submonoids of `additive M`. -/
@[simps]
def submonoid.to_add_submonoid : submonoid M ≃o add_submonoid (additive M) :=
{ to_fun := λ S,
{ carrier := additive.to_mul ⁻¹' S,
zero_mem' := S.one_mem',
add_mem' := S.mul_mem' },
inv_fun := λ S,
{ carrier := additive.of_mul ⁻¹' S,
one_mem' := S.zero_mem',
mul_mem' := S.add_mem' },
left_inv := λ x, by cases x; refl,
right_inv := λ x, by cases x; refl,
map_rel_iff' := λ a b, iff.rfl, }
/-- Additive submonoids of an additive monoid `additive M` are isomorphic to submonoids of `M`. -/
abbreviation add_submonoid.to_submonoid' : add_submonoid (additive M) ≃o submonoid M :=
submonoid.to_add_submonoid.symm
lemma submonoid.to_add_submonoid_closure (S : set M) :
(submonoid.closure S).to_add_submonoid = add_submonoid.closure (additive.to_mul ⁻¹' S) :=
le_antisymm
(submonoid.to_add_submonoid.to_galois_connection.l_le $
submonoid.closure_le.2 add_submonoid.subset_closure)
(add_submonoid.closure_le.2 submonoid.subset_closure)
lemma add_submonoid.to_submonoid'_closure (S : set (additive M)) :
(add_submonoid.closure S).to_submonoid' = submonoid.closure (multiplicative.of_add ⁻¹' S) :=
le_antisymm
(add_submonoid.to_submonoid'.to_galois_connection.l_le $
add_submonoid.closure_le.2 submonoid.subset_closure)
(submonoid.closure_le.2 add_submonoid.subset_closure)
end
section
variables {A : Type*} [add_zero_class A]
/-- Additive submonoids of an additive monoid `A` are isomorphic to
multiplicative submonoids of `multiplicative A`. -/
@[simps]
def add_submonoid.to_submonoid : add_submonoid A ≃o submonoid (multiplicative A) :=
{ to_fun := λ S,
{ carrier := multiplicative.to_add ⁻¹' S,
one_mem' := S.zero_mem',
mul_mem' := S.add_mem' },
inv_fun := λ S,
{ carrier := multiplicative.of_add ⁻¹' S,
zero_mem' := S.one_mem',
add_mem' := S.mul_mem' },
left_inv := λ x, by cases x; refl,
right_inv := λ x, by cases x; refl,
map_rel_iff' := λ a b, iff.rfl, }
/-- Submonoids of a monoid `multiplicative A` are isomorphic to additive submonoids of `A`. -/
abbreviation submonoid.to_add_submonoid' : submonoid (multiplicative A) ≃o add_submonoid A :=
add_submonoid.to_submonoid.symm
lemma add_submonoid.to_submonoid_closure (S : set A) :
(add_submonoid.closure S).to_submonoid = submonoid.closure (multiplicative.to_add ⁻¹' S) :=
le_antisymm
(add_submonoid.to_submonoid.to_galois_connection.l_le $
add_submonoid.closure_le.2 submonoid.subset_closure)
(submonoid.closure_le.2 add_submonoid.subset_closure)
lemma submonoid.to_add_submonoid'_closure (S : set (multiplicative A)) :
(submonoid.closure S).to_add_submonoid' = add_submonoid.closure (additive.of_mul ⁻¹' S) :=
le_antisymm
(submonoid.to_add_submonoid'.to_galois_connection.l_le $
submonoid.closure_le.2 add_submonoid.subset_closure)
(add_submonoid.closure_le.2 submonoid.subset_closure)
end
namespace submonoid
open set
/-!
### `comap` and `map`
-/
/-- The preimage of a submonoid along a monoid homomorphism is a submonoid. -/
@[to_additive "The preimage of an `add_submonoid` along an `add_monoid` homomorphism is an
`add_submonoid`."]
def comap (f : M →* N) (S : submonoid N) : submonoid M :=
{ carrier := (f ⁻¹' S),
one_mem' := show f 1 ∈ S, by rw f.map_one; exact S.one_mem,
mul_mem' := λ a b ha hb,
show f (a * b) ∈ S, by rw f.map_mul; exact S.mul_mem ha hb }
@[simp, to_additive]
lemma coe_comap (S : submonoid N) (f : M →* N) : (S.comap f : set M) = f ⁻¹' S := rfl
@[simp, to_additive]
lemma mem_comap {S : submonoid N} {f : M →* N} {x : M} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl
@[to_additive]
lemma comap_comap (S : submonoid P) (g : N →* P) (f : M →* N) :
(S.comap g).comap f = S.comap (g.comp f) :=
rfl
@[simp, to_additive]
lemma comap_id (S : submonoid P) : S.comap (monoid_hom.id _) = S :=
ext (by simp)
/-- The image of a submonoid along a monoid homomorphism is a submonoid. -/
@[to_additive "The image of an `add_submonoid` along an `add_monoid` homomorphism is
an `add_submonoid`."]
def map (f : M →* N) (S : submonoid M) : submonoid N :=
{ carrier := (f '' S),
one_mem' := ⟨1, S.one_mem, f.map_one⟩,
mul_mem' := begin rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩, exact ⟨x * y, S.mul_mem hx hy,
by rw f.map_mul; refl⟩ end }
@[simp, to_additive]
lemma coe_map (f : M →* N) (S : submonoid M) :
(S.map f : set N) = f '' S := rfl
@[simp, to_additive]
lemma mem_map {f : M →* N} {S : submonoid M} {y : N} :
y ∈ S.map f ↔ ∃ x ∈ S, f x = y :=
mem_image_iff_bex
@[to_additive]
lemma mem_map_of_mem (f : M →* N) {S : submonoid M} {x : M} (hx : x ∈ S) : f x ∈ S.map f :=
mem_image_of_mem f hx
@[to_additive]
lemma apply_coe_mem_map (f : M →* N) (S : submonoid M) (x : S) : f x ∈ S.map f :=
mem_map_of_mem f x.prop
@[to_additive]
lemma map_map (g : N →* P) (f : M →* N) : (S.map f).map g = S.map (g.comp f) :=
set_like.coe_injective $ image_image _ _ _
@[to_additive]
lemma map_le_iff_le_comap {f : M →* N} {S : submonoid M} {T : submonoid N} :
S.map f ≤ T ↔ S ≤ T.comap f :=
image_subset_iff
@[to_additive]
lemma gc_map_comap (f : M →* N) : galois_connection (map f) (comap f) :=
λ S T, map_le_iff_le_comap
@[to_additive]
lemma map_le_of_le_comap {T : submonoid N} {f : M →* N} : S ≤ T.comap f → S.map f ≤ T :=
(gc_map_comap f).l_le
@[to_additive]
lemma le_comap_of_map_le {T : submonoid N} {f : M →* N} : S.map f ≤ T → S ≤ T.comap f :=
(gc_map_comap f).le_u
@[to_additive]
lemma le_comap_map {f : M →* N} : S ≤ (S.map f).comap f :=
(gc_map_comap f).le_u_l _
@[to_additive]
lemma map_comap_le {S : submonoid N} {f : M →* N} : (S.comap f).map f ≤ S :=
(gc_map_comap f).l_u_le _
@[to_additive]
lemma monotone_map {f : M →* N} : monotone (map f) :=
(gc_map_comap f).monotone_l
@[to_additive]
lemma monotone_comap {f : M →* N} : monotone (comap f) :=
(gc_map_comap f).monotone_u
@[simp, to_additive]
lemma map_comap_map {f : M →* N} : ((S.map f).comap f).map f = S.map f :=
congr_fun ((gc_map_comap f).l_u_l_eq_l) _
@[simp, to_additive]
lemma comap_map_comap {S : submonoid N} {f : M →* N} : ((S.comap f).map f).comap f = S.comap f :=
congr_fun ((gc_map_comap f).u_l_u_eq_u) _
@[to_additive]
lemma map_sup (S T : submonoid M) (f : M →* N) : (S ⊔ T).map f = S.map f ⊔ T.map f :=
(gc_map_comap f).l_sup
@[to_additive]
lemma map_supr {ι : Sort*} (f : M →* N) (s : ι → submonoid M) :
(supr s).map f = ⨆ i, (s i).map f :=
(gc_map_comap f).l_supr
@[to_additive]
lemma comap_inf (S T : submonoid N) (f : M →* N) : (S ⊓ T).comap f = S.comap f ⊓ T.comap f :=
(gc_map_comap f).u_inf
@[to_additive]
lemma comap_infi {ι : Sort*} (f : M →* N) (s : ι → submonoid N) :
(infi s).comap f = ⨅ i, (s i).comap f :=
(gc_map_comap f).u_infi
@[simp, to_additive] lemma map_bot (f : M →* N) : (⊥ : submonoid M).map f = ⊥ :=
(gc_map_comap f).l_bot
@[simp, to_additive] lemma comap_top (f : M →* N) : (⊤ : submonoid N).comap f = ⊤ :=
(gc_map_comap f).u_top
@[simp, to_additive] lemma map_id (S : submonoid M) : S.map (monoid_hom.id M) = S :=
ext (λ x, ⟨λ ⟨_, h, rfl⟩, h, λ h, ⟨_, h, rfl⟩⟩)
section galois_coinsertion
variables {ι : Type*} {f : M →* N} (hf : function.injective f)
include hf
/-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/
def gci_map_comap : galois_coinsertion (map f) (comap f) :=
(gc_map_comap f).to_galois_coinsertion
(λ S x, by simp [mem_comap, mem_map, hf.eq_iff])
lemma comap_map_eq_of_injective (S : submonoid M) : (S.map f).comap f = S :=
(gci_map_comap hf).u_l_eq _
lemma comap_surjective_of_injective : function.surjective (comap f) :=
(gci_map_comap hf).u_surjective
lemma map_injective_of_injective : function.injective (map f) :=
(gci_map_comap hf).l_injective
lemma comap_inf_map_of_injective (S T : submonoid M) : (S.map f ⊓ T.map f).comap f = S ⊓ T :=
(gci_map_comap hf).u_inf_l _ _
lemma comap_infi_map_of_injective (S : ι → submonoid M) : (⨅ i, (S i).map f).comap f = infi S :=
(gci_map_comap hf).u_infi_l _
lemma comap_sup_map_of_injective (S T : submonoid M) : (S.map f ⊔ T.map f).comap f = S ⊔ T :=
(gci_map_comap hf).u_sup_l _ _
lemma comap_supr_map_of_injective (S : ι → submonoid M) : (⨆ i, (S i).map f).comap f = supr S :=
(gci_map_comap hf).u_supr_l _
lemma map_le_map_iff_of_injective {S T : submonoid M} : S.map f ≤ T.map f ↔ S ≤ T :=
(gci_map_comap hf).l_le_l_iff
lemma map_strict_mono_of_injective : strict_mono (map f) :=
(gci_map_comap hf).strict_mono_l
end galois_coinsertion
section galois_insertion
variables {ι : Type*} {f : M →* N} (hf : function.surjective f)
include hf
/-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/
def gi_map_comap : galois_insertion (map f) (comap f) :=
(gc_map_comap f).to_galois_insertion
(λ S x h, let ⟨y, hy⟩ := hf x in mem_map.2 ⟨y, by simp [hy, h]⟩)
lemma map_comap_eq_of_surjective (S : submonoid N) : (S.comap f).map f = S :=
(gi_map_comap hf).l_u_eq _
lemma map_surjective_of_surjective : function.surjective (map f) :=
(gi_map_comap hf).l_surjective
lemma comap_injective_of_surjective : function.injective (comap f) :=
(gi_map_comap hf).u_injective
lemma map_inf_comap_of_surjective (S T : submonoid N) : (S.comap f ⊓ T.comap f).map f = S ⊓ T :=
(gi_map_comap hf).l_inf_u _ _
lemma map_infi_comap_of_surjective (S : ι → submonoid N) : (⨅ i, (S i).comap f).map f = infi S :=
(gi_map_comap hf).l_infi_u _
lemma map_sup_comap_of_surjective (S T : submonoid N) : (S.comap f ⊔ T.comap f).map f = S ⊔ T :=
(gi_map_comap hf).l_sup_u _ _
lemma map_supr_comap_of_surjective (S : ι → submonoid N) : (⨆ i, (S i).comap f).map f = supr S :=
(gi_map_comap hf).l_supr_u _
lemma comap_le_comap_iff_of_surjective {S T : submonoid N} : S.comap f ≤ T.comap f ↔ S ≤ T :=
(gi_map_comap hf).u_le_u_iff
lemma comap_strict_mono_of_surjective : strict_mono (comap f) :=
(gi_map_comap hf).strict_mono_u
end galois_insertion
/-- A submonoid of a monoid inherits a multiplication. -/
@[to_additive "An `add_submonoid` of an `add_monoid` inherits an addition."]
instance has_mul : has_mul S := ⟨λ a b, ⟨a.1 * b.1, S.mul_mem a.2 b.2⟩⟩
/-- A submonoid of a monoid inherits a 1. -/
@[to_additive "An `add_submonoid` of an `add_monoid` inherits a zero."]
instance has_one : has_one S := ⟨⟨_, S.one_mem⟩⟩
@[simp, to_additive] lemma coe_mul (x y : S) : (↑(x * y) : M) = ↑x * ↑y := rfl
@[simp, to_additive] lemma coe_one : ((1 : S) : M) = 1 := rfl
attribute [norm_cast] coe_mul coe_one
attribute [norm_cast] add_submonoid.coe_add add_submonoid.coe_zero
/-- A submonoid of a unital magma inherits a unital magma structure. -/
@[to_additive "An `add_submonoid` of an unital additive magma inherits an unital additive magma
structure."]
instance to_mul_one_class {M : Type*} [mul_one_class M] (S : submonoid M) : mul_one_class S :=
subtype.coe_injective.mul_one_class coe rfl (λ _ _, rfl)
/-- A submonoid of a monoid inherits a monoid structure. -/
@[to_additive "An `add_submonoid` of an `add_monoid` inherits an `add_monoid`
structure."]
instance to_monoid {M : Type*} [monoid M] (S : submonoid M) : monoid S :=
subtype.coe_injective.monoid coe rfl (λ _ _, rfl)
/-- A submonoid of a `comm_monoid` is a `comm_monoid`. -/
@[to_additive "An `add_submonoid` of an `add_comm_monoid` is
an `add_comm_monoid`."]
instance to_comm_monoid {M} [comm_monoid M] (S : submonoid M) : comm_monoid S :=
subtype.coe_injective.comm_monoid coe rfl (λ _ _, rfl)
/-- A submonoid of an `ordered_comm_monoid` is an `ordered_comm_monoid`. -/
@[to_additive "An `add_submonoid` of an `ordered_add_comm_monoid` is
an `ordered_add_comm_monoid`."]
instance to_ordered_comm_monoid {M} [ordered_comm_monoid M] (S : submonoid M) :
ordered_comm_monoid S :=
subtype.coe_injective.ordered_comm_monoid coe rfl (λ _ _, rfl)
/-- A submonoid of a `linear_ordered_comm_monoid` is a `linear_ordered_comm_monoid`. -/
@[to_additive "An `add_submonoid` of a `linear_ordered_add_comm_monoid` is
a `linear_ordered_add_comm_monoid`."]
instance to_linear_ordered_comm_monoid {M} [linear_ordered_comm_monoid M] (S : submonoid M) :
linear_ordered_comm_monoid S :=
subtype.coe_injective.linear_ordered_comm_monoid coe rfl (λ _ _, rfl)
/-- A submonoid of an `ordered_cancel_comm_monoid` is an `ordered_cancel_comm_monoid`. -/
@[to_additive "An `add_submonoid` of an `ordered_cancel_add_comm_monoid` is
an `ordered_cancel_add_comm_monoid`."]
instance to_ordered_cancel_comm_monoid {M} [ordered_cancel_comm_monoid M] (S : submonoid M) :
ordered_cancel_comm_monoid S :=
subtype.coe_injective.ordered_cancel_comm_monoid coe rfl (λ _ _, rfl)
/-- A submonoid of a `linear_ordered_cancel_comm_monoid` is a `linear_ordered_cancel_comm_monoid`.
-/
@[to_additive "An `add_submonoid` of a `linear_ordered_cancel_add_comm_monoid` is
a `linear_ordered_cancel_add_comm_monoid`."]
instance to_linear_ordered_cancel_comm_monoid {M} [linear_ordered_cancel_comm_monoid M]
(S : submonoid M) : linear_ordered_cancel_comm_monoid S :=
subtype.coe_injective.linear_ordered_cancel_comm_monoid coe rfl (λ _ _, rfl)
/-- The natural monoid hom from a submonoid of monoid `M` to `M`. -/
@[to_additive "The natural monoid hom from an `add_submonoid` of `add_monoid` `M` to `M`."]
def subtype : S →* M := ⟨coe, rfl, λ _ _, rfl⟩
@[simp, to_additive] theorem coe_subtype : ⇑S.subtype = coe := rfl
/-- A submonoid is isomorphic to its image under an injective function -/
@[to_additive "An additive submonoid is isomorphic to its image under an injective function"]
noncomputable def equiv_map_of_injective
(f : M →* N) (hf : function.injective f) : S ≃* S.map f :=
{ map_mul' := λ _ _, subtype.ext (f.map_mul _ _), ..equiv.set.image f S hf }
@[simp, to_additive] lemma coe_equiv_map_of_injective_apply
(f : M →* N) (hf : function.injective f) (x : S) :
(equiv_map_of_injective S f hf x : N) = f x := rfl
/-- An induction principle on elements of the type `submonoid.closure s`.
If `p` holds for `1` and all elements of `s`, and is preserved under multiplication, then `p`
holds for all elements of the closure of `s`.
The difference with `submonoid.closure_induction` is that this acts on the subtype.
-/
@[to_additive "An induction principle on elements of the type `add_submonoid.closure s`.
If `p` holds for `0` and all elements of `s`, and is preserved under addition, then `p`
holds for all elements of the closure of `s`.
The difference with `add_submonoid.closure_induction` is that this acts on the subtype."]
lemma closure_induction' (s : set M) {p : closure s → Prop}
(Hs : ∀ x (h : x ∈ s), p ⟨x, subset_closure h⟩)
(H1 : p 1)
(Hmul : ∀ x y, p x → p y → p (x * y))
(x : closure s) :
p x :=
subtype.rec_on x $ λ x hx, begin
refine exists.elim _ (λ (hx : x ∈ closure s) (hc : p ⟨x, hx⟩), hc),
exact closure_induction hx
(λ x hx, ⟨subset_closure hx, Hs x hx⟩)
⟨one_mem _, H1⟩
(λ x y hx hy, exists.elim hx $ λ hx' hx, exists.elim hy $ λ hy' hy,
⟨mul_mem _ hx' hy', Hmul _ _ hx hy⟩),
end
attribute [elab_as_eliminator] submonoid.closure_induction' add_submonoid.closure_induction'
/-- Given `submonoid`s `s`, `t` of monoids `M`, `N` respectively, `s × t` as a submonoid
of `M × N`. -/
@[to_additive prod "Given `add_submonoid`s `s`, `t` of `add_monoid`s `A`, `B` respectively, `s × t`
as an `add_submonoid` of `A × B`."]
def prod (s : submonoid M) (t : submonoid N) : submonoid (M × N) :=
{ carrier := (s : set M).prod t,
one_mem' := ⟨s.one_mem, t.one_mem⟩,
mul_mem' := λ p q hp hq, ⟨s.mul_mem hp.1 hq.1, t.mul_mem hp.2 hq.2⟩ }
@[to_additive coe_prod]
lemma coe_prod (s : submonoid M) (t : submonoid N) :
(s.prod t : set (M × N)) = (s : set M).prod (t : set N) :=
rfl
@[to_additive mem_prod]
lemma mem_prod {s : submonoid M} {t : submonoid N} {p : M × N} :
p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl
@[to_additive prod_mono]
lemma prod_mono {s₁ s₂ : submonoid M} {t₁ t₂ : submonoid N} (hs : s₁ ≤ s₂) (ht : t₁ ≤ t₂) :
s₁.prod t₁ ≤ s₂.prod t₂ :=
set.prod_mono hs ht
@[to_additive prod_top]
lemma prod_top (s : submonoid M) :
s.prod (⊤ : submonoid N) = s.comap (monoid_hom.fst M N) :=
ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst]
@[to_additive top_prod]
lemma top_prod (s : submonoid N) :
(⊤ : submonoid M).prod s = s.comap (monoid_hom.snd M N) :=
ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd]
@[simp, to_additive top_prod_top]
lemma top_prod_top : (⊤ : submonoid M).prod (⊤ : submonoid N) = ⊤ :=
(top_prod _).trans $ comap_top _
@[to_additive] lemma bot_prod_bot : (⊥ : submonoid M).prod (⊥ : submonoid N) = ⊥ :=
set_like.coe_injective $ by simp [coe_prod, prod.one_eq_mk]
/-- The product of submonoids is isomorphic to their product as monoids. -/
@[to_additive prod_equiv "The product of additive submonoids is isomorphic to their product
as additive monoids"]
def prod_equiv (s : submonoid M) (t : submonoid N) : s.prod t ≃* s × t :=
{ map_mul' := λ x y, rfl, .. equiv.set.prod ↑s ↑t }
open monoid_hom
@[to_additive]
lemma map_inl (s : submonoid M) : s.map (inl M N) = s.prod ⊥ :=
ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨hx, set.mem_singleton 1⟩,
λ ⟨hps, hp1⟩, ⟨p.1, hps, prod.ext rfl $ (set.eq_of_mem_singleton hp1).symm⟩⟩
@[to_additive]
lemma map_inr (s : submonoid N) : s.map (inr M N) = prod ⊥ s :=
ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨set.mem_singleton 1, hx⟩,
λ ⟨hp1, hps⟩, ⟨p.2, hps, prod.ext (set.eq_of_mem_singleton hp1).symm rfl⟩⟩
@[simp, to_additive prod_bot_sup_bot_prod]
lemma prod_bot_sup_bot_prod (s : submonoid M) (t : submonoid N) :
(s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t :=
le_antisymm (sup_le (prod_mono (le_refl s) bot_le) (prod_mono bot_le (le_refl t))) $
assume p hp, prod.fst_mul_snd p ▸ mul_mem _
((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, set.mem_singleton 1⟩)
((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨set.mem_singleton 1, hp.2⟩)
@[to_additive]
lemma mem_map_equiv {f : M ≃* N} {K : submonoid M} {x : N} :
x ∈ K.map f.to_monoid_hom ↔ f.symm x ∈ K :=
@set.mem_image_equiv _ _ ↑K f.to_equiv x
@[to_additive]
lemma map_equiv_eq_comap_symm (f : M ≃* N) (K : submonoid M) :
K.map f.to_monoid_hom = K.comap f.symm.to_monoid_hom :=
set_like.coe_injective (f.to_equiv.image_eq_preimage K)
@[to_additive]
lemma comap_equiv_eq_map_symm (f : N ≃* M) (K : submonoid M) :
K.comap f.to_monoid_hom = K.map f.symm.to_monoid_hom :=
(map_equiv_eq_comap_symm f.symm K).symm
end submonoid
namespace monoid_hom
open submonoid
/-- For many categories (monoids, modules, rings, ...) the set-theoretic image of a morphism `f` is
a subobject of the codomain. When this is the case, it is useful to define the range of a morphism
in such a way that the underlying carrier set of the range subobject is definitionally
`set.range f`. In particular this means that the types `↥(set.range f)` and `↥f.range` are
interchangeable without proof obligations.
A convenient candidate definition for range which is mathematically correct is `map ⊤ f`, just as
`set.range` could have been defined as `f '' set.univ`. However, this lacks the desired definitional
convenience, in that it both does not match `set.range`, and that it introduces a redudant `x ∈ ⊤`
term which clutters proofs. In such a case one may resort to the `copy`
pattern. A `copy` function converts the definitional problem for the carrier set of a subobject
into a one-off propositional proof obligation which one discharges while writing the definition of
the definitionally convenient range (the parameter `hs` in the example below).
A good example is the case of a morphism of monoids. A convenient definition for
`monoid_hom.mrange` would be `(⊤ : submonoid M).map f`. However since this lacks the required
definitional convenience, we first define `submonoid.copy` as follows:
```lean
protected def copy (S : submonoid M) (s : set M) (hs : s = S) : submonoid M :=
{ carrier := s,
one_mem' := hs.symm ▸ S.one_mem',
mul_mem' := hs.symm ▸ S.mul_mem' }
```
and then finally define:
```lean
def mrange (f : M →* N) : submonoid N :=
((⊤ : submonoid M).map f).copy (set.range f) set.image_univ.symm
```
-/
library_note "range copy pattern"
/-- The range of a monoid homomorphism is a submonoid. See Note [range copy pattern]. -/
@[to_additive "The range of an `add_monoid_hom` is an `add_submonoid`."]
def mrange (f : M →* N) : submonoid N :=
((⊤ : submonoid M).map f).copy (set.range f) set.image_univ.symm
@[simp, to_additive]
lemma coe_mrange (f : M →* N) :
(f.mrange : set N) = set.range f :=
rfl
@[simp, to_additive] lemma mem_mrange {f : M →* N} {y : N} :
y ∈ f.mrange ↔ ∃ x, f x = y :=
iff.rfl
@[to_additive] lemma mrange_eq_map (f : M →* N) : f.mrange = (⊤ : submonoid M).map f :=
by ext; simp
@[to_additive]
lemma map_mrange (g : N →* P) (f : M →* N) : f.mrange.map g = (g.comp f).mrange :=
by simpa only [mrange_eq_map] using (⊤ : submonoid M).map_map g f
@[to_additive]
lemma mrange_top_iff_surjective {N} [mul_one_class N] {f : M →* N} :
f.mrange = (⊤ : submonoid N) ↔ function.surjective f :=
set_like.ext'_iff.trans $ iff.trans (by rw [coe_mrange, coe_top]) set.range_iff_surjective
/-- The range of a surjective monoid hom is the whole of the codomain. -/
@[to_additive "The range of a surjective `add_monoid` hom is the whole of the codomain."]
lemma mrange_top_of_surjective {N} [mul_one_class N] (f : M →* N) (hf : function.surjective f) :
f.mrange = (⊤ : submonoid N) :=
mrange_top_iff_surjective.2 hf
@[to_additive]
lemma mclosure_preimage_le (f : M →* N) (s : set N) :
closure (f ⁻¹' s) ≤ (closure s).comap f :=
closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx
/-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated
by the image of the set. -/
@[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals
the `add_submonoid` generated by the image of the set."]
lemma map_mclosure (f : M →* N) (s : set M) :
(closure s).map f = closure (f '' s) :=
le_antisymm
(map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _)
(mclosure_preimage_le _ _))
(closure_le.2 $ set.image_subset _ subset_closure)
/-- Restriction of a monoid hom to a submonoid of the domain. -/
@[to_additive "Restriction of an add_monoid hom to an `add_submonoid` of the domain."]
def mrestrict {N : Type*} [mul_one_class N] (f : M →* N) (S : submonoid M) : S →* N :=
f.comp S.subtype
@[simp, to_additive]
lemma mrestrict_apply {N : Type*} [mul_one_class N] (f : M →* N) (x : S) : f.mrestrict S x = f x :=
rfl
/-- Restriction of a monoid hom to a submonoid of the codomain. -/
@[to_additive "Restriction of an `add_monoid` hom to an `add_submonoid` of the codomain.", simps]
def cod_mrestrict (f : M →* N) (S : submonoid N) (h : ∀ x, f x ∈ S) : M →* S :=
{ to_fun := λ n, ⟨f n, h n⟩,
map_one' := subtype.eq f.map_one,
map_mul' := λ x y, subtype.eq (f.map_mul x y) }
/-- Restriction of a monoid hom to its range interpreted as a submonoid. -/
@[to_additive "Restriction of an `add_monoid` hom to its range interpreted as a submonoid."]
def mrange_restrict {N} [mul_one_class N] (f : M →* N) : M →* f.mrange :=
f.cod_mrestrict f.mrange $ λ x, ⟨x, rfl⟩
@[simp, to_additive]
lemma coe_mrange_restrict {N} [mul_one_class N] (f : M →* N) (x : M) :
(f.mrange_restrict x : N) = f x :=
rfl
end monoid_hom
namespace submonoid
open monoid_hom
@[to_additive]
lemma mrange_inl : (inl M N).mrange = prod ⊤ ⊥ :=
by simpa only [mrange_eq_map] using map_inl ⊤
@[to_additive]
lemma mrange_inr : (inr M N).mrange = prod ⊥ ⊤ :=
by simpa only [mrange_eq_map] using map_inr ⊤
@[to_additive]
lemma mrange_inl' : (inl M N).mrange = comap (snd M N) ⊥ := mrange_inl.trans (top_prod _)
@[to_additive]
lemma mrange_inr' : (inr M N).mrange = comap (fst M N) ⊥ := mrange_inr.trans (prod_top _)
@[simp, to_additive]
lemma mrange_fst : (fst M N).mrange = ⊤ :=
(fst M N).mrange_top_of_surjective $ @prod.fst_surjective _ _ ⟨1⟩
@[simp, to_additive]
lemma mrange_snd : (snd M N).mrange = ⊤ :=
(snd M N).mrange_top_of_surjective $ @prod.snd_surjective _ _ ⟨1⟩
@[simp, to_additive]
lemma mrange_inl_sup_mrange_inr : (inl M N).mrange ⊔ (inr M N).mrange = ⊤ :=
by simp only [mrange_inl, mrange_inr, prod_bot_sup_bot_prod, top_prod_top]
/-- The monoid hom associated to an inclusion of submonoids. -/
@[to_additive "The `add_monoid` hom associated to an inclusion of submonoids."]
def inclusion {S T : submonoid M} (h : S ≤ T) : S →* T :=
S.subtype.cod_mrestrict _ (λ x, h x.2)
@[simp, to_additive]
lemma range_subtype (s : submonoid M) : s.subtype.mrange = s :=
set_like.coe_injective $ (coe_mrange _).trans $ subtype.range_coe
@[to_additive] lemma eq_top_iff' : S = ⊤ ↔ ∀ x : M, x ∈ S :=
eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩
@[to_additive] lemma eq_bot_iff_forall : S = ⊥ ↔ ∀ x ∈ S, x = (1 : M) :=
begin
split,
{ intros h x x_in,
rwa [h, mem_bot] at x_in },
{ intros h,
ext x,
rw mem_bot,
exact ⟨h x, by { rintros rfl, exact S.one_mem }⟩ },
end
@[to_additive] lemma nontrivial_iff_exists_ne_one (S : submonoid M) :
nontrivial S ↔ ∃ x ∈ S, x ≠ (1:M) :=
begin
split,
{ introI h,
rcases exists_ne (1 : S) with ⟨⟨h, h_in⟩, h_ne⟩,
use [h, h_in],
intro hyp,
apply h_ne,
simpa [hyp] },
{ rintros ⟨x, x_in, hx⟩,
apply nontrivial_of_ne (⟨x, x_in⟩ : S) 1,
intro hyp,
apply hx,
simpa [has_one.one] using hyp },
end
/-- A submonoid is either the trivial submonoid or nontrivial. -/
@[to_additive] lemma bot_or_nontrivial (S : submonoid M) : S = ⊥ ∨ nontrivial S :=
begin
classical,
by_cases h : ∀ x ∈ S, x = (1 : M),
{ left,
exact S.eq_bot_iff_forall.mpr h },
{ right,
push_neg at h,
simpa [nontrivial_iff_exists_ne_one] using h },
end
/-- A submonoid is either the trivial submonoid or contains a nonzero element. -/
@[to_additive] lemma bot_or_exists_ne_one (S : submonoid M) : S = ⊥ ∨ ∃ x ∈ S, x ≠ (1:M) :=
begin
convert S.bot_or_nontrivial,
rw nontrivial_iff_exists_ne_one
end
end submonoid
namespace mul_equiv
variables {S} {T : submonoid M}
/-- Makes the identity isomorphism from a proof that two submonoids of a multiplicative
monoid are equal. -/
@[to_additive "Makes the identity additive isomorphism from a proof two
submonoids of an additive monoid are equal."]
def submonoid_congr (h : S = T) : S ≃* T :=
{ map_mul' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h }
-- this name is primed so that the version to `f.range` instead of `f.mrange` can be unprimed.
/-- A monoid homomorphism `f : M →* N` with a left-inverse `g : N → M` defines a multiplicative
equivalence between `M` and `f.mrange`.
This is a bidirectional version of `monoid_hom.mrange_restrict`. -/
@[to_additive /-"
An additive monoid homomorphism `f : M →+ N` with a left-inverse `g : N → M` defines an additive
equivalence between `M` and `f.mrange`.
This is a bidirectional version of `add_monoid_hom.mrange_restrict`. "-/, simps {simp_rhs := tt}]
def of_left_inverse' (f : M →* N) {g : N → M} (h : function.left_inverse g f) : M ≃* f.mrange :=
{ to_fun := f.mrange_restrict,
inv_fun := g ∘ f.mrange.subtype,
left_inv := h,
right_inv := λ x, subtype.ext $
let ⟨x', hx'⟩ := monoid_hom.mem_mrange.mp x.prop in
show f (g x) = x, by rw [←hx', h x'],
.. f.mrange_restrict }
end mul_equiv
/-! ### Actions by `submonoid`s
These instances tranfer the action by an element `m : M` of a monoid `M` written as `m • a` onto the
action by an element `s : S` of a submonoid `S : submonoid M` such that `s • a = (s : M) • a`.
These instances work particularly well in conjunction with `monoid.to_mul_action`, enabling
`s • m` as an alias for `↑s * m`.
-/
section actions
namespace submonoid
variables {M' : Type*} {α β : Type*} [monoid M']
/-- The action by a submonoid is the action by the underlying monoid. -/
@[to_additive /-"The additive action by an add_submonoid is the action by the underlying
add_monoid. "-/]
instance [mul_action M' α] (S : submonoid M') : mul_action S α :=
mul_action.comp_hom _ S.subtype
@[to_additive]
lemma smul_def [mul_action M' α] {S : submonoid M'} (g : S) (m : α) : g • m = (g : M') • m := rfl
/-- The action by a submonoid is the action by the underlying monoid. -/
instance [add_monoid α] [distrib_mul_action M' α] (S : submonoid M') : distrib_mul_action S α :=
distrib_mul_action.comp_hom _ S.subtype
@[to_additive]
instance smul_comm_class_left
[mul_action M' β] [has_scalar α β] [smul_comm_class M' α β] (S : submonoid M') :
smul_comm_class S α β :=
⟨λ a, (smul_comm (a : M') : _)⟩
@[to_additive]
instance smul_comm_class_right
[has_scalar α β] [mul_action M' β] [smul_comm_class α M' β] (S : submonoid M') :
smul_comm_class α S β :=
⟨λ a s, (smul_comm a (s : M') : _)⟩
/-- Note that this provides `is_scalar_tower S M' M'` which is needed by `smul_mul_assoc`. -/
instance
[has_scalar α β] [mul_action M' α] [mul_action M' β] [is_scalar_tower M' α β] (S : submonoid M') :
is_scalar_tower S α β :=
⟨λ a, (smul_assoc (a : M') : _)⟩
example {S : submonoid M'} : is_scalar_tower S M' M' := by apply_instance
end submonoid
end actions
|
40497e6a7c535cbc4729e05c8dcb0480ad78569e | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/category_theory/connected.lean | 621e27f79bbc4318b2a83f4478dafe788823dce3 | [
"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 | 9,128 | 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 data.list.chain
import category_theory.punit
/-!
# Connected category
Define a connected category as a _nonempty_ category for which every functor
to a discrete category is isomorphic to the constant functor.
NB. Some authors include the empty category as connected, we do not.
We instead are interested in categories with exactly one 'connected
component'.
We give some equivalent definitions:
- A nonempty category for which every functor to a discrete category is
constant on objects.
See `any_functor_const_on_obj` and `connected.of_any_functor_const_on_obj`.
- A nonempty category for which every function `F` for which the presence of a
morphism `f : j₁ ⟶ j₂` implies `F j₁ = F j₂` must be constant everywhere.
See `constant_of_preserves_morphisms` and `connected.of_constant_of_preserves_morphisms`.
- A nonempty category for which any subset of its elements containing the
default and closed under morphisms is everything.
See `induct_on_objects` and `connected.of_induct`.
- A nonempty category for which every object is related under the reflexive
transitive closure of the relation "there is a morphism in some direction
from `j₁` to `j₂`".
See `connected_zigzag` and `zigzag_connected`.
- A nonempty category for which for any two objects there is a sequence of
morphisms (some reversed) from one to the other.
See `exists_zigzag'` and `connected_of_zigzag`.
We also prove the result that the functor given by `(X × -)` preserves any
connected limit. That is, any limit of shape `J` where `J` is a connected
category is preserved by the functor `(X × -)`.
-/
universes v₁ v₂ u₁ u₂
open category_theory.category
namespace category_theory
section connected
-- See note [default priority]
set_option default_priority 100
/--
We define a connected category as a _nonempty_ category for which every
functor to a discrete category is constant.
NB. Some authors include the empty category as connected, we do not.
We instead are interested in categories with exactly one 'connected
component'.
This allows us to show that the functor X ⨯ - preserves connected limits.
-/
class connected (J : Type v₂) [category.{v₁} J] extends inhabited J :=
(iso_constant : Π {α : Type v₂} (F : J ⥤ discrete α), F ≅ (functor.const J).obj (F.obj default))
end connected
variables {J : Type v₂} [category.{v₁} J]
/--
If J is connected, any functor to a discrete category is constant on objects.
The converse is given in `connected.of_any_functor_const_on_obj`.
-/
lemma any_functor_const_on_obj [connected J] {α : Type v₂} (F : J ⥤ discrete α) (j : J) :
F.obj j = F.obj (default J) :=
((connected.iso_constant F).hom.app j).down.1
/--
If any functor to a discrete category is constant on objects, J is connected.
The converse of `any_functor_const_on_obj`.
-/
def connected.of_any_functor_const_on_obj [inhabited J]
(h : ∀ {α : Type v₂} (F : J ⥤ discrete α), ∀ (j : J), F.obj j = F.obj (default J)) :
connected J :=
{ iso_constant := λ α F, nat_iso.of_components (λ B, eq_to_iso (h F B)) (λ _ _ _, subsingleton.elim _ _) }
/--
If `J` is connected, then given any function `F` such that the presence of a
morphism `j₁ ⟶ j₂` implies `F j₁ = F j₂`, we have that `F` is constant.
This can be thought of as a local-to-global property.
The converse is shown in `connected.of_constant_of_preserves_morphisms`
-/
lemma constant_of_preserves_morphisms [connected J] {α : Type v₂} (F : J → α)
(h : ∀ (j₁ j₂ : J) (f : j₁ ⟶ j₂), F j₁ = F j₂) (j : J) :
F j = F (default J) :=
any_functor_const_on_obj { obj := F, map := λ _ _ f, eq_to_hom (h _ _ f) } j
/--
`J` is connected if: given any function `F : J → α` which is constant for any
`j₁, j₂` for which there is a morphism `j₁ ⟶ j₂`, then `F` is constant.
This can be thought of as a local-to-global property.
The converse of `constant_of_preserves_morphisms`.
-/
def connected.of_constant_of_preserves_morphisms [inhabited J]
(h : ∀ {α : Type v₂} (F : J → α), (∀ {j₁ j₂ : J} (f : j₁ ⟶ j₂), F j₁ = F j₂) → (∀ j : J, F j = F (default J))) :
connected J :=
connected.of_any_functor_const_on_obj (λ _ F, h F.obj (λ _ _ f, (F.map f).down.1))
/--
An inductive-like property for the objects of a connected category.
If `default J` is in the set `p`, and `p` is closed under morphisms of `J`,
then `p` contains all of `J`.
The converse is given in `connected.of_induct`.
-/
lemma induct_on_objects [connected J] (p : set J) (h0 : default J ∈ p)
(h1 : ∀ {j₁ j₂ : J} (f : j₁ ⟶ j₂), j₁ ∈ p ↔ j₂ ∈ p) (j : J) :
j ∈ p :=
begin
injection (constant_of_preserves_morphisms (λ k, ulift.up (k ∈ p)) (λ j₁ j₂ f, _) j) with i,
rwa i,
dsimp,
exact congr_arg ulift.up (propext (h1 f)),
end
/--
If any maximal connected component of J containing the default is all of J, then J is connected.
The converse of `induct_on_objects`.
-/
def connected.of_induct [inhabited J]
(h : ∀ (p : set J), default J ∈ p → (∀ {j₁ j₂ : J} (f : j₁ ⟶ j₂), j₁ ∈ p ↔ j₂ ∈ p) → ∀ (j : J), j ∈ p) :
connected J :=
connected.of_constant_of_preserves_morphisms (λ α F a, h {j | F j = F (default J)} rfl (λ _ _ f, by simp [a f] ))
/-- j₁ and j₂ are related by `zag` if there is a morphism between them. -/
@[reducible]
def zag (j₁ j₂ : J) : Prop := nonempty (j₁ ⟶ j₂) ∨ nonempty (j₂ ⟶ j₁)
/--
`j₁` and `j₂` are related by `zigzag` if there is a chain of
morphisms from `j₁` to `j₂`, with backward morphisms allowed.
-/
@[reducible]
def zigzag : J → J → Prop := relation.refl_trans_gen zag
/-- Any equivalence relation containing (⟶) holds for all pairs of a connected category. -/
lemma equiv_relation [connected J] (r : J → J → Prop) (hr : _root_.equivalence r)
(h : ∀ {j₁ j₂ : J} (f : j₁ ⟶ j₂), r j₁ j₂) :
∀ (j₁ j₂ : J), r j₁ j₂ :=
begin
have z: ∀ (j : J), r (default J) j :=
induct_on_objects (λ k, r (default J) k)
(hr.1 (default J)) (λ _ _ f, ⟨λ t, hr.2.2 t (h f), λ t, hr.2.2 t (hr.2.1 (h f))⟩),
intros, apply hr.2.2 (hr.2.1 (z _)) (z _)
end
/-- In a connected category, any two objects are related by `zigzag`. -/
lemma connected_zigzag [connected J] (j₁ j₂ : J) : zigzag j₁ j₂ :=
equiv_relation _
(mk_equivalence _
relation.reflexive_refl_trans_gen
(relation.refl_trans_gen.symmetric (λ _ _ _, by rwa [zag, or_comm]))
relation.transitive_refl_trans_gen)
(λ _ _ f, relation.refl_trans_gen.single (or.inl (nonempty.intro f))) _ _
/--
If any two objects in an inhabited category are related by `zigzag`, the category is connected.
-/
def zigzag_connected [inhabited J] (h : ∀ (j₁ j₂ : J), zigzag j₁ j₂) : connected J :=
begin
apply connected.of_induct,
intros,
have: ∀ (j₁ j₂ : J), zigzag j₁ j₂ → (j₁ ∈ p ↔ j₂ ∈ p),
{ introv k,
induction k,
{ refl },
{ rw k_ih,
rcases k_a_1 with ⟨⟨_⟩⟩ | ⟨⟨_⟩⟩,
apply a_1 k_a_1,
apply (a_1 k_a_1).symm } },
rwa this j (default J) (h _ _)
end
lemma exists_zigzag' [connected J] (j₁ j₂ : J) :
∃ l, list.chain zag j₁ l ∧ list.last (j₁ :: l) (list.cons_ne_nil _ _) = j₂ :=
list.exists_chain_of_relation_refl_trans_gen (connected_zigzag _ _)
/--
If any two objects in an inhabited category are linked by a sequence of (potentially reversed)
morphisms, then J is connected.
The converse of `exists_zigzag'`.
-/
def connected_of_zigzag [inhabited J]
(h : ∀ (j₁ j₂ : J), ∃ l, list.chain zag j₁ l ∧ list.last (j₁ :: l) (list.cons_ne_nil _ _) = j₂) :
connected J :=
begin
apply connected.of_induct,
intros p d k j,
obtain ⟨l, zags, lst⟩ := h j (default J),
apply list.chain.induction p l zags lst _ d,
rintros _ _ (⟨⟨_⟩⟩ | ⟨⟨_⟩⟩),
{ exact (k a).2 },
{ exact (k a).1 }
end
/-- If `discrete α` is connected, then `α` is (type-)equivalent to `punit`. -/
def discrete_connected_equiv_punit {α : Type*} [connected (discrete α)] : α ≃ punit :=
discrete.equiv_of_equivalence
{ functor := functor.star _,
inverse := discrete.functor (λ _, default _),
unit_iso := by apply connected.iso_constant,
counit_iso := functor.punit_ext _ _ }
variables {C : Type u₂} [category.{v₂} C]
/--
For objects `X Y : C`, any natural transformation `α : const X ⟶ const Y` from a connected
category must be constant.
This is the key property of connected categories which we use to establish properties about limits.
-/
lemma nat_trans_from_connected [conn : connected J] {X Y : C}
(α : (functor.const J).obj X ⟶ (functor.const J).obj Y) :
∀ (j : J), α.app j = (α.app (default J) : X ⟶ Y) :=
@constant_of_preserves_morphisms _ _ _
(X ⟶ Y)
(λ j, α.app j)
(λ _ _ f, (by { have := α.naturality f, erw [id_comp, comp_id] at this, exact this.symm }))
end category_theory
|
8cc30cc9df78fa6b178322ef9a6b0725ab4673be | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/topology/stone_cech.lean | 87a23914342df5b20dbce7ca3f955606837db924 | [
"Apache-2.0"
] | permissive | ChrisHughes24/mathlib | 98322577c460bc6b1fe5c21f42ce33ad1c3e5558 | a2a867e827c2a6702beb9efc2b9282bd801d5f9a | refs/heads/master | 1,583,848,251,477 | 1,565,164,247,000 | 1,565,164,247,000 | 129,409,993 | 0 | 1 | Apache-2.0 | 1,565,164,817,000 | 1,523,628,059,000 | Lean | UTF-8 | Lean | false | false | 11,194 | 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 inducing of `α` in `ultrafilter α`. -/
lemma dense_inducing_pure : @dense_inducing _ _ ⊥ _ (pure : α → ultrafilter α) :=
by letI : topological_space α := ⊥; exact
dense_inducing.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⟩)
(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⟩)
-- The following refined version will never be used
/-- `pure : α → ultrafilter α` defines a dense embedding of `α` in `ultrafilter α`. -/
lemma dense_embedding_pure : @dense_embedding _ _ ⊥ _ (pure : α → ultrafilter α) :=
by letI : topological_space α := ⊥ ;
exact { inj := ultrafilter_pure_injective, ..dense_inducing_pure }
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 γ] [t2_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_inducing_pure.extend f
lemma ultrafilter_extend_extends (f : α → γ) : ultrafilter.extend f ∘ pure = f :=
begin
letI : topological_space α := ⊥,
letI : discrete_topology α := ⟨rfl⟩,
exact funext (dense_inducing_pure.extend_eq_of_cont continuous_of_discrete_topology)
end
variables [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_inducing_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_inducing_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_inducing_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
|
31ae6d5f9deba3e0fa02f39043fcf8c110617c87 | 968e2f50b755d3048175f176376eff7139e9df70 | /examples/prop_logic_theory/unnamed_635.lean | 4af5ef5609da7dda2f1078027a1fe1379cf33f5c | [] | no_license | gihanmarasingha/mth1001_sphinx | 190a003269ba5e54717b448302a27ca26e31d491 | 05126586cbf5786e521be1ea2ef5b4ba3c44e74a | refs/heads/master | 1,672,913,933,677 | 1,604,516,583,000 | 1,604,516,583,000 | 309,245,750 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 326 | lean | variables p q r : Prop
-- BEGIN
example (h₁ : p → q) (h₂ : q → r) (h₃ : p) : r :=
begin
apply h₂, -- By implication elimination on `h₂`, it suffices to prove `q`.
apply h₁, -- By implication elimination on `h₁`, it suffices to prove `p`.
exact h₃, -- This follows by reiteration on `h₃`.
end
-- END |
e86ba06af7db0382c7b11f485c98729f79d882c3 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/data/num/lemmas.lean | e712dabd37660f848d02cee29e2f789fa7dacd6e | [
"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 | 50,017 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
-/
import data.num.bitwise
import data.int.char_zero
import data.nat.gcd
import data.nat.psub
/-!
# Properties of the binary representation of integers
-/
local attribute [simp] add_assoc
namespace pos_num
variables {α : Type*}
@[simp, norm_cast] theorem cast_one [has_one α] [has_add α] :
((1 : pos_num) : α) = 1 := rfl
@[simp] theorem cast_one' [has_one α] [has_add α] : (pos_num.one : α) = 1 := rfl
@[simp, norm_cast] theorem cast_bit0 [has_one α] [has_add α] (n : pos_num) :
(n.bit0 : α) = _root_.bit0 n := rfl
@[simp, norm_cast] theorem cast_bit1 [has_one α] [has_add α] (n : pos_num) :
(n.bit1 : α) = _root_.bit1 n := rfl
@[simp, norm_cast] theorem cast_to_nat [add_monoid α] [has_one α] :
∀ n : pos_num, ((n : ℕ) : α) = n
| 1 := nat.cast_one
| (bit0 p) := (nat.cast_bit0 _).trans $ congr_arg _root_.bit0 p.cast_to_nat
| (bit1 p) := (nat.cast_bit1 _).trans $ congr_arg _root_.bit1 p.cast_to_nat
@[simp, norm_cast] theorem to_nat_to_int (n : pos_num) : ((n : ℕ) : ℤ) = n :=
by rw [← int.nat_cast_eq_coe_nat, cast_to_nat]
@[simp, norm_cast] theorem cast_to_int [add_group α] [has_one α] (n : pos_num) :
((n : ℤ) : α) = n :=
by rw [← to_nat_to_int, int.cast_coe_nat, cast_to_nat]
theorem succ_to_nat : ∀ n, (succ n : ℕ) = n + 1
| 1 := rfl
| (bit0 p) := rfl
| (bit1 p) := (congr_arg _root_.bit0 (succ_to_nat p)).trans $
show ↑p + 1 + ↑p + 1 = ↑p + ↑p + 1 + 1, by simp [add_left_comm]
theorem one_add (n : pos_num) : 1 + n = succ n := by cases n; refl
theorem add_one (n : pos_num) : n + 1 = succ n := by cases n; refl
@[norm_cast]
theorem add_to_nat : ∀ m n, ((m + n : pos_num) : ℕ) = m + n
| 1 b := by rw [one_add b, succ_to_nat, add_comm]; refl
| a 1 := by rw [add_one a, succ_to_nat]; refl
| (bit0 a) (bit0 b) := (congr_arg _root_.bit0 (add_to_nat a b)).trans $
show ((a + b) + (a + b) : ℕ) = (a + a) + (b + b), by simp [add_left_comm]
| (bit0 a) (bit1 b) := (congr_arg _root_.bit1 (add_to_nat a b)).trans $
show ((a + b) + (a + b) + 1 : ℕ) = (a + a) + (b + b + 1), by simp [add_left_comm]
| (bit1 a) (bit0 b) := (congr_arg _root_.bit1 (add_to_nat a b)).trans $
show ((a + b) + (a + b) + 1 : ℕ) = (a + a + 1) + (b + b), by simp [add_comm, add_left_comm]
| (bit1 a) (bit1 b) :=
show (succ (a + b) + succ (a + b) : ℕ) = (a + a + 1) + (b + b + 1),
by rw [succ_to_nat, add_to_nat]; simp [add_left_comm]
theorem add_succ : ∀ (m n : pos_num), m + succ n = succ (m + n)
| 1 b := by simp [one_add]
| (bit0 a) 1 := congr_arg bit0 (add_one a)
| (bit1 a) 1 := congr_arg bit1 (add_one a)
| (bit0 a) (bit0 b) := rfl
| (bit0 a) (bit1 b) := congr_arg bit0 (add_succ a b)
| (bit1 a) (bit0 b) := rfl
| (bit1 a) (bit1 b) := congr_arg bit1 (add_succ a b)
theorem bit0_of_bit0 : Π n, _root_.bit0 n = bit0 n
| 1 := rfl
| (bit0 p) := congr_arg bit0 (bit0_of_bit0 p)
| (bit1 p) := show bit0 (succ (_root_.bit0 p)) = _, by rw bit0_of_bit0; refl
theorem bit1_of_bit1 (n : pos_num) : _root_.bit1 n = bit1 n :=
show _root_.bit0 n + 1 = bit1 n, by rw [add_one, bit0_of_bit0]; refl
@[norm_cast]
theorem mul_to_nat (m) : ∀ n, ((m * n : pos_num) : ℕ) = m * n
| 1 := (mul_one _).symm
| (bit0 p) := show (↑(m * p) + ↑(m * p) : ℕ) = ↑m * (p + p), by rw [mul_to_nat, left_distrib]
| (bit1 p) := (add_to_nat (bit0 (m * p)) m).trans $
show (↑(m * p) + ↑(m * p) + ↑m : ℕ) = ↑m * (p + p) + m, by rw [mul_to_nat, left_distrib]
theorem to_nat_pos : ∀ n : pos_num, 0 < (n : ℕ)
| 1 := zero_lt_one
| (bit0 p) := let h := to_nat_pos p in add_pos h h
| (bit1 p) := nat.succ_pos _
theorem cmp_to_nat_lemma {m n : pos_num} : (m:ℕ) < n → (bit1 m : ℕ) < bit0 n :=
show (m:ℕ) < n → (m + m + 1 + 1 : ℕ) ≤ n + n,
by intro h; rw [nat.add_right_comm m m 1, add_assoc]; exact add_le_add h h
theorem cmp_swap (m) : ∀n, (cmp m n).swap = cmp n m :=
by induction m with m IH m IH; intro n;
cases n with n n; try {unfold cmp}; try {refl}; rw ←IH; cases cmp m n; refl
theorem cmp_to_nat : ∀ (m n), (ordering.cases_on (cmp m n) ((m:ℕ) < n) (m = n) ((n:ℕ) < m) : Prop)
| 1 1 := rfl
| (bit0 a) 1 := let h : (1:ℕ) ≤ a := to_nat_pos a in add_le_add h h
| (bit1 a) 1 := nat.succ_lt_succ $ to_nat_pos $ bit0 a
| 1 (bit0 b) := let h : (1:ℕ) ≤ b := to_nat_pos b in add_le_add h h
| 1 (bit1 b) := nat.succ_lt_succ $ to_nat_pos $ bit0 b
| (bit0 a) (bit0 b) := begin
have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro,
{ exact add_lt_add this this },
{ rw this },
{ exact add_lt_add this this }
end
| (bit0 a) (bit1 b) := begin dsimp [cmp],
have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro,
{ exact nat.le_succ_of_le (add_lt_add this this) },
{ rw this, apply nat.lt_succ_self },
{ exact cmp_to_nat_lemma this }
end
| (bit1 a) (bit0 b) := begin dsimp [cmp],
have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro,
{ exact cmp_to_nat_lemma this },
{ rw this, apply nat.lt_succ_self },
{ exact nat.le_succ_of_le (add_lt_add this this) },
end
| (bit1 a) (bit1 b) := begin
have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro,
{ exact nat.succ_lt_succ (add_lt_add this this) },
{ rw this },
{ exact nat.succ_lt_succ (add_lt_add this this) }
end
@[norm_cast]
theorem lt_to_nat {m n : pos_num} : (m:ℕ) < n ↔ m < n :=
show (m:ℕ) < n ↔ cmp m n = ordering.lt, from
match cmp m n, cmp_to_nat m n with
| ordering.lt, h := by simp at h; simp [h]
| ordering.eq, h := by simp at h; simp [h, lt_irrefl]; exact dec_trivial
| ordering.gt, h := by simp [not_lt_of_gt h]; exact dec_trivial
end
@[norm_cast]
theorem le_to_nat {m n : pos_num} : (m:ℕ) ≤ n ↔ m ≤ n :=
by rw ← not_lt; exact not_congr lt_to_nat
end pos_num
namespace num
variables {α : Type*}
open pos_num
theorem add_zero (n : num) : n + 0 = n := by cases n; refl
theorem zero_add (n : num) : 0 + n = n := by cases n; refl
theorem add_one : ∀ n : num, n + 1 = succ n
| 0 := rfl
| (pos p) := by cases p; refl
theorem add_succ : ∀ (m n : num), m + succ n = succ (m + n)
| 0 n := by simp [zero_add]
| (pos p) 0 := show pos (p + 1) = succ (pos p + 0),
by rw [pos_num.add_one, add_zero]; refl
| (pos p) (pos q) := congr_arg pos (pos_num.add_succ _ _)
@[simp, norm_cast] theorem add_of_nat (m) : ∀ n, ((m + n : ℕ) : num) = m + n
| 0 := (add_zero _).symm
| (n+1) := show ((m + n : ℕ) + 1 : num) = m + (↑ n + 1),
by rw [add_one, add_one, add_succ, add_of_nat]
theorem bit0_of_bit0 : ∀ n : num, bit0 n = n.bit0
| 0 := rfl
| (pos p) := congr_arg pos p.bit0_of_bit0
theorem bit1_of_bit1 : ∀ n : num, bit1 n = n.bit1
| 0 := rfl
| (pos p) := congr_arg pos p.bit1_of_bit1
@[simp, norm_cast] theorem cast_zero [has_zero α] [has_one α] [has_add α] :
((0 : num) : α) = 0 := rfl
@[simp] theorem cast_zero' [has_zero α] [has_one α] [has_add α] :
(num.zero : α) = 0 := rfl
@[simp, norm_cast] theorem cast_one [has_zero α] [has_one α] [has_add α] :
((1 : num) : α) = 1 := rfl
@[simp] theorem cast_pos [has_zero α] [has_one α] [has_add α]
(n : pos_num) : (num.pos n : α) = n := rfl
theorem succ'_to_nat : ∀ n, (succ' n : ℕ) = n + 1
| 0 := (_root_.zero_add _).symm
| (pos p) := pos_num.succ_to_nat _
theorem succ_to_nat (n) : (succ n : ℕ) = n + 1 := succ'_to_nat n
@[simp, norm_cast] theorem cast_to_nat [add_monoid α] [has_one α] : ∀ n : num, ((n : ℕ) : α) = n
| 0 := nat.cast_zero
| (pos p) := p.cast_to_nat
@[simp, norm_cast] theorem to_nat_to_int (n : num) : ((n : ℕ) : ℤ) = n :=
by rw [← int.nat_cast_eq_coe_nat, cast_to_nat]
@[simp, norm_cast] theorem cast_to_int [add_group α] [has_one α] (n : num) : ((n : ℤ) : α) = n :=
by rw [← to_nat_to_int, int.cast_coe_nat, cast_to_nat]
@[norm_cast]
theorem to_of_nat : Π (n : ℕ), ((n : num) : ℕ) = n
| 0 := rfl
| (n+1) := by rw [nat.cast_add_one, add_one, succ_to_nat, to_of_nat]
@[simp, norm_cast]
theorem of_nat_cast [add_monoid α] [has_one α] (n : ℕ) : ((n : num) : α) = n :=
by rw [← cast_to_nat, to_of_nat]
@[norm_cast] theorem of_nat_inj {m n : ℕ} : (m : num) = n ↔ m = n :=
⟨λ h, function.left_inverse.injective to_of_nat h, congr_arg _⟩
@[norm_cast]
theorem add_to_nat : ∀ m n, ((m + n : num) : ℕ) = m + n
| 0 0 := rfl
| 0 (pos q) := (_root_.zero_add _).symm
| (pos p) 0 := rfl
| (pos p) (pos q) := pos_num.add_to_nat _ _
@[norm_cast]
theorem mul_to_nat : ∀ m n, ((m * n : num) : ℕ) = m * n
| 0 0 := rfl
| 0 (pos q) := (zero_mul _).symm
| (pos p) 0 := rfl
| (pos p) (pos q) := pos_num.mul_to_nat _ _
theorem cmp_to_nat : ∀ (m n), (ordering.cases_on (cmp m n) ((m:ℕ) < n) (m = n) ((n:ℕ) < m) : Prop)
| 0 0 := rfl
| 0 (pos b) := to_nat_pos _
| (pos a) 0 := to_nat_pos _
| (pos a) (pos b) :=
by { have := pos_num.cmp_to_nat a b; revert this; dsimp [cmp];
cases pos_num.cmp a b, exacts [id, congr_arg pos, id] }
@[norm_cast]
theorem lt_to_nat {m n : num} : (m:ℕ) < n ↔ m < n :=
show (m:ℕ) < n ↔ cmp m n = ordering.lt, from
match cmp m n, cmp_to_nat m n with
| ordering.lt, h := by simp at h; simp [h]
| ordering.eq, h := by simp at h; simp [h, lt_irrefl]; exact dec_trivial
| ordering.gt, h := by simp [not_lt_of_gt h]; exact dec_trivial
end
@[norm_cast]
theorem le_to_nat {m n : num} : (m:ℕ) ≤ n ↔ m ≤ n :=
by rw ← not_lt; exact not_congr lt_to_nat
end num
namespace pos_num
@[simp] theorem of_to_nat : Π (n : pos_num), ((n : ℕ) : num) = num.pos n
| 1 := rfl
| (bit0 p) :=
show ↑(p + p : ℕ) = num.pos p.bit0,
by rw [num.add_of_nat, of_to_nat];
exact congr_arg num.pos p.bit0_of_bit0
| (bit1 p) :=
show ((p + p : ℕ) : num) + 1 = num.pos p.bit1,
by rw [num.add_of_nat, of_to_nat];
exact congr_arg num.pos p.bit1_of_bit1
end pos_num
namespace num
@[simp, norm_cast] theorem of_to_nat : Π (n : num), ((n : ℕ) : num) = n
| 0 := rfl
| (pos p) := p.of_to_nat
@[norm_cast] theorem to_nat_inj {m n : num} : (m : ℕ) = n ↔ m = n :=
⟨λ h, function.left_inverse.injective of_to_nat h, congr_arg _⟩
/--
This tactic tries to turn an (in)equality about `num`s to one about `nat`s by rewriting.
```lean
example (n : num) (m : num) : n ≤ n + m :=
begin
num.transfer_rw,
exact nat.le_add_right _ _
end
```
-/
meta def transfer_rw : tactic unit :=
`[repeat {rw ← to_nat_inj <|> rw ← lt_to_nat <|> rw ← le_to_nat},
repeat {rw add_to_nat <|> rw mul_to_nat <|> rw cast_one <|> rw cast_zero}]
/--
This tactic tries to prove (in)equalities about `num`s by transfering them to the `nat` world and
then trying to call `simp`.
```lean
example (n : num) (m : num) : n ≤ n + m := by num.transfer
```
-/
meta def transfer : tactic unit := `[intros, transfer_rw, try {simp}]
instance : comm_semiring num :=
by refine {
add := (+),
zero := 0,
zero_add := zero_add,
add_zero := add_zero,
mul := (*),
one := 1, .. }; try {transfer}; simp [mul_add, mul_left_comm, mul_comm, add_comm]
instance : ordered_cancel_add_comm_monoid num :=
{ add_left_cancel := by {intros a b c, transfer_rw, apply add_left_cancel},
add_right_cancel := by {intros a b c, transfer_rw, apply add_right_cancel},
lt := (<),
lt_iff_le_not_le := by {intros a b, transfer_rw, apply lt_iff_le_not_le},
le := (≤),
le_refl := by transfer,
le_trans := by {intros a b c, transfer_rw, apply le_trans},
le_antisymm := by {intros a b, transfer_rw, apply le_antisymm},
add_le_add_left := by {intros a b h c, revert h, transfer_rw,
exact λ h, add_le_add_left h c},
le_of_add_le_add_left := by {intros a b c, transfer_rw, apply le_of_add_le_add_left},
..num.comm_semiring }
instance : linear_ordered_semiring num :=
{ le_total := by {intros a b, transfer_rw, apply le_total},
zero_le_one := dec_trivial,
mul_lt_mul_of_pos_left := by {intros a b c, transfer_rw, apply mul_lt_mul_of_pos_left},
mul_lt_mul_of_pos_right := by {intros a b c, transfer_rw, apply mul_lt_mul_of_pos_right},
decidable_lt := num.decidable_lt,
decidable_le := num.decidable_le,
decidable_eq := num.decidable_eq,
exists_pair_ne := ⟨0, 1, dec_trivial⟩,
..num.comm_semiring, ..num.ordered_cancel_add_comm_monoid }
@[norm_cast]
theorem dvd_to_nat (m n : num) : (m : ℕ) ∣ n ↔ m ∣ n :=
⟨λ ⟨k, e⟩, ⟨k, by rw [← of_to_nat n, e]; simp⟩,
λ ⟨k, e⟩, ⟨k, by simp [e, mul_to_nat]⟩⟩
end num
namespace pos_num
variables {α : Type*}
open num
@[norm_cast] theorem to_nat_inj {m n : pos_num} : (m : ℕ) = n ↔ m = n :=
⟨λ h, num.pos.inj $ by rw [← pos_num.of_to_nat, ← pos_num.of_to_nat, h],
congr_arg _⟩
theorem pred'_to_nat : ∀ n, (pred' n : ℕ) = nat.pred n
| 1 := rfl
| (bit0 n) :=
have nat.succ ↑(pred' n) = ↑n,
by rw [pred'_to_nat n, nat.succ_pred_eq_of_pos (to_nat_pos n)],
match pred' n, this : ∀ k : num, nat.succ ↑k = ↑n →
↑(num.cases_on k 1 bit1 : pos_num) = nat.pred (_root_.bit0 n) with
| 0, (h : ((1:num):ℕ) = n) := by rw ← to_nat_inj.1 h; refl
| num.pos p, (h : nat.succ ↑p = n) :=
by rw ← h; exact (nat.succ_add p p).symm
end
| (bit1 n) := rfl
@[simp] theorem pred'_succ' (n) : pred' (succ' n) = n :=
num.to_nat_inj.1 $ by rw [pred'_to_nat, succ'_to_nat,
nat.add_one, nat.pred_succ]
@[simp] theorem succ'_pred' (n) : succ' (pred' n) = n :=
to_nat_inj.1 $ by rw [succ'_to_nat, pred'_to_nat,
nat.add_one, nat.succ_pred_eq_of_pos (to_nat_pos _)]
instance : has_dvd pos_num := ⟨λ m n, pos m ∣ pos n⟩
@[norm_cast] theorem dvd_to_nat {m n : pos_num} : (m:ℕ) ∣ n ↔ m ∣ n :=
num.dvd_to_nat (pos m) (pos n)
theorem size_to_nat : ∀ n, (size n : ℕ) = nat.size n
| 1 := nat.size_one.symm
| (bit0 n) := by rw [size, succ_to_nat, size_to_nat, cast_bit0,
nat.size_bit0 $ ne_of_gt $ to_nat_pos n]
| (bit1 n) := by rw [size, succ_to_nat, size_to_nat, cast_bit1,
nat.size_bit1]
theorem size_eq_nat_size : ∀ n, (size n : ℕ) = nat_size n
| 1 := rfl
| (bit0 n) := by rw [size, succ_to_nat, nat_size, size_eq_nat_size]
| (bit1 n) := by rw [size, succ_to_nat, nat_size, size_eq_nat_size]
theorem nat_size_to_nat (n) : nat_size n = nat.size n :=
by rw [← size_eq_nat_size, size_to_nat]
theorem nat_size_pos (n) : 0 < nat_size n :=
by cases n; apply nat.succ_pos
/--
This tactic tries to turn an (in)equality about `pos_num`s to one about `nat`s by rewriting.
```lean
example (n : pos_num) (m : pos_num) : n ≤ n + m :=
begin
pos_num.transfer_rw,
exact nat.le_add_right _ _
end
```
-/
meta def transfer_rw : tactic unit :=
`[repeat {rw ← to_nat_inj <|> rw ← lt_to_nat <|> rw ← le_to_nat},
repeat {rw add_to_nat <|> rw mul_to_nat <|> rw cast_one <|> rw cast_zero}]
/--
This tactic tries to prove (in)equalities about `pos_num`s by transferring them to the `nat` world
and then trying to call `simp`.
```lean
example (n : pos_num) (m : pos_num) : n ≤ n + m := by pos_num.transfer
```
-/
meta def transfer : tactic unit :=
`[intros, transfer_rw, try {simp [add_comm, add_left_comm, mul_comm, mul_left_comm]}]
instance : add_comm_semigroup pos_num :=
by refine {add := (+), ..}; transfer
instance : comm_monoid pos_num :=
by refine {mul := (*), one := 1, ..}; transfer
instance : distrib pos_num :=
by refine {add := (+), mul := (*), ..}; {transfer, simp [mul_add, mul_comm]}
instance : linear_order pos_num :=
{ lt := (<),
lt_iff_le_not_le := by {intros a b, transfer_rw, apply lt_iff_le_not_le},
le := (≤),
le_refl := by transfer,
le_trans := by {intros a b c, transfer_rw, apply le_trans},
le_antisymm := by {intros a b, transfer_rw, apply le_antisymm},
le_total := by {intros a b, transfer_rw, apply le_total},
decidable_lt := by apply_instance,
decidable_le := by apply_instance,
decidable_eq := by apply_instance }
@[simp] theorem cast_to_num (n : pos_num) : ↑n = num.pos n :=
by rw [← cast_to_nat, ← of_to_nat n]
@[simp, norm_cast]
theorem bit_to_nat (b n) : (bit b n : ℕ) = nat.bit b n :=
by cases b; refl
@[simp, norm_cast]
theorem cast_add [add_monoid α] [has_one α] (m n) : ((m + n : pos_num) : α) = m + n :=
by rw [← cast_to_nat, add_to_nat, nat.cast_add, cast_to_nat, cast_to_nat]
@[simp, norm_cast, priority 500]
theorem cast_succ [add_monoid α] [has_one α] (n : pos_num) : (succ n : α) = n + 1 :=
by rw [← add_one, cast_add, cast_one]
@[simp, norm_cast]
theorem cast_inj [add_monoid α] [has_one α] [char_zero α] {m n : pos_num} : (m:α) = n ↔ m = n :=
by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_inj, to_nat_inj]
@[simp]
theorem one_le_cast [linear_ordered_semiring α] (n : pos_num) : (1 : α) ≤ n :=
by rw [← cast_to_nat, ← nat.cast_one, nat.cast_le]; apply to_nat_pos
@[simp]
theorem cast_pos [linear_ordered_semiring α] (n : pos_num) : 0 < (n : α) :=
lt_of_lt_of_le zero_lt_one (one_le_cast n)
@[simp, norm_cast]
theorem cast_mul [semiring α] (m n) : ((m * n : pos_num) : α) = m * n :=
by rw [← cast_to_nat, mul_to_nat, nat.cast_mul, cast_to_nat, cast_to_nat]
@[simp]
theorem cmp_eq (m n) : cmp m n = ordering.eq ↔ m = n :=
begin
have := cmp_to_nat m n,
cases cmp m n; simp at this ⊢; try {exact this};
{ simp [show m ≠ n, from λ e, by rw e at this; exact lt_irrefl _ this] }
end
@[simp, norm_cast]
theorem cast_lt [linear_ordered_semiring α] {m n : pos_num} : (m:α) < n ↔ m < n :=
by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_lt, lt_to_nat]
@[simp, norm_cast]
theorem cast_le [linear_ordered_semiring α] {m n : pos_num} : (m:α) ≤ n ↔ m ≤ n :=
by rw ← not_lt; exact not_congr cast_lt
end pos_num
namespace num
variables {α : Type*}
open pos_num
theorem bit_to_nat (b n) : (bit b n : ℕ) = nat.bit b n :=
by cases b; cases n; refl
theorem cast_succ' [add_monoid α] [has_one α] (n) : (succ' n : α) = n + 1 :=
by rw [← pos_num.cast_to_nat, succ'_to_nat, nat.cast_add_one, cast_to_nat]
theorem cast_succ [add_monoid α] [has_one α] (n) : (succ n : α) = n + 1 := cast_succ' n
@[simp, norm_cast] theorem cast_add [semiring α] (m n) : ((m + n : num) : α) = m + n :=
by rw [← cast_to_nat, add_to_nat, nat.cast_add, cast_to_nat, cast_to_nat]
@[simp, norm_cast] theorem cast_bit0 [semiring α] (n : num) : (n.bit0 : α) = _root_.bit0 n :=
by rw [← bit0_of_bit0, _root_.bit0, cast_add]; refl
@[simp, norm_cast] theorem cast_bit1 [semiring α] (n : num) : (n.bit1 : α) = _root_.bit1 n :=
by rw [← bit1_of_bit1, _root_.bit1, bit0_of_bit0, cast_add, cast_bit0]; refl
@[simp, norm_cast] theorem cast_mul [semiring α] : ∀ m n, ((m * n : num) : α) = m * n
| 0 0 := (zero_mul _).symm
| 0 (pos q) := (zero_mul _).symm
| (pos p) 0 := (mul_zero _).symm
| (pos p) (pos q) := pos_num.cast_mul _ _
theorem size_to_nat : ∀ n, (size n : ℕ) = nat.size n
| 0 := nat.size_zero.symm
| (pos p) := p.size_to_nat
theorem size_eq_nat_size : ∀ n, (size n : ℕ) = nat_size n
| 0 := rfl
| (pos p) := p.size_eq_nat_size
theorem nat_size_to_nat (n) : nat_size n = nat.size n :=
by rw [← size_eq_nat_size, size_to_nat]
@[simp] theorem of_nat'_eq : ∀ n, num.of_nat' n = n :=
nat.binary_rec rfl $ λ b n IH, begin
rw of_nat' at IH ⊢,
rw [nat.binary_rec_eq, IH],
{ cases b; simp [nat.bit, bit0_of_bit0, bit1_of_bit1] },
{ refl }
end
theorem zneg_to_znum (n : num) : -n.to_znum = n.to_znum_neg := by cases n; refl
theorem zneg_to_znum_neg (n : num) : -n.to_znum_neg = n.to_znum := by cases n; refl
theorem to_znum_inj {m n : num} : m.to_znum = n.to_znum ↔ m = n :=
⟨λ h, by cases m; cases n; cases h; refl, congr_arg _⟩
@[simp, norm_cast squash] theorem cast_to_znum [has_zero α] [has_one α] [has_add α] [has_neg α] :
∀ n : num, (n.to_znum : α) = n
| 0 := rfl
| (num.pos p) := rfl
@[simp] theorem cast_to_znum_neg [add_group α] [has_one α] :
∀ n : num, (n.to_znum_neg : α) = -n
| 0 := neg_zero.symm
| (num.pos p) := rfl
@[simp] theorem add_to_znum (m n : num) : num.to_znum (m + n) = m.to_znum + n.to_znum :=
by cases m; cases n; refl
end num
namespace pos_num
open num
theorem pred_to_nat {n : pos_num} (h : 1 < n) : (pred n : ℕ) = nat.pred n :=
begin
unfold pred,
have := pred'_to_nat n,
cases e : pred' n,
{ have : (1:ℕ) ≤ nat.pred n :=
nat.pred_le_pred ((@cast_lt ℕ _ _ _).2 h),
rw [← pred'_to_nat, e] at this,
exact absurd this dec_trivial },
{ rw [← pred'_to_nat, e], refl }
end
theorem sub'_one (a : pos_num) : sub' a 1 = (pred' a).to_znum :=
by cases a; refl
theorem one_sub' (a : pos_num) : sub' 1 a = (pred' a).to_znum_neg :=
by cases a; refl
theorem lt_iff_cmp {m n} : m < n ↔ cmp m n = ordering.lt := iff.rfl
theorem le_iff_cmp {m n} : m ≤ n ↔ cmp m n ≠ ordering.gt :=
not_congr $ lt_iff_cmp.trans $
by rw ← cmp_swap; cases cmp m n; exact dec_trivial
end pos_num
namespace num
variables {α : Type*}
open pos_num
theorem pred_to_nat : ∀ (n : num), (pred n : ℕ) = nat.pred n
| 0 := rfl
| (pos p) := by rw [pred, pos_num.pred'_to_nat]; refl
theorem ppred_to_nat : ∀ (n : num), coe <$> ppred n = nat.ppred n
| 0 := rfl
| (pos p) := by rw [ppred, option.map_some, nat.ppred_eq_some.2];
rw [pos_num.pred'_to_nat, nat.succ_pred_eq_of_pos (pos_num.to_nat_pos _)]; refl
theorem cmp_swap (m n) : (cmp m n).swap = cmp n m :=
by cases m; cases n; try {unfold cmp}; try {refl}; apply pos_num.cmp_swap
theorem cmp_eq (m n) : cmp m n = ordering.eq ↔ m = n :=
begin
have := cmp_to_nat m n,
cases cmp m n; simp at this ⊢; try {exact this};
{ simp [show m ≠ n, from λ e, by rw e at this; exact lt_irrefl _ this] }
end
@[simp, norm_cast]
theorem cast_lt [linear_ordered_semiring α] {m n : num} : (m:α) < n ↔ m < n :=
by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_lt, lt_to_nat]
@[simp, norm_cast]
theorem cast_le [linear_ordered_semiring α] {m n : num} : (m:α) ≤ n ↔ m ≤ n :=
by rw ← not_lt; exact not_congr cast_lt
@[simp, norm_cast]
theorem cast_inj [linear_ordered_semiring α] {m n : num} : (m:α) = n ↔ m = n :=
by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_inj, to_nat_inj]
theorem lt_iff_cmp {m n} : m < n ↔ cmp m n = ordering.lt := iff.rfl
theorem le_iff_cmp {m n} : m ≤ n ↔ cmp m n ≠ ordering.gt :=
not_congr $ lt_iff_cmp.trans $
by rw ← cmp_swap; cases cmp m n; exact dec_trivial
theorem bitwise_to_nat {f : num → num → num} {g : bool → bool → bool}
(p : pos_num → pos_num → num)
(gff : g ff ff = ff)
(f00 : f 0 0 = 0)
(f0n : ∀ n, f 0 (pos n) = cond (g ff tt) (pos n) 0)
(fn0 : ∀ n, f (pos n) 0 = cond (g tt ff) (pos n) 0)
(fnn : ∀ m n, f (pos m) (pos n) = p m n)
(p11 : p 1 1 = cond (g tt tt) 1 0)
(p1b : ∀ b n, p 1 (pos_num.bit b n) = bit (g tt b) (cond (g ff tt) (pos n) 0))
(pb1 : ∀ a m, p (pos_num.bit a m) 1 = bit (g a tt) (cond (g tt ff) (pos m) 0))
(pbb : ∀ a b m n, p (pos_num.bit a m) (pos_num.bit b n) = bit (g a b) (p m n))
: ∀ m n : num, (f m n : ℕ) = nat.bitwise g m n :=
begin
intros, cases m with m; cases n with n;
try { change zero with 0 };
try { change ((0:num):ℕ) with 0 },
{ rw [f00, nat.bitwise_zero]; refl },
{ unfold nat.bitwise, rw [f0n, nat.binary_rec_zero],
cases g ff tt; refl },
{ unfold nat.bitwise,
generalize h : (pos m : ℕ) = m', revert h,
apply nat.bit_cases_on m' _, intros b m' h,
rw [fn0, nat.binary_rec_eq, nat.binary_rec_zero, ←h],
cases g tt ff; refl,
apply nat.bitwise_bit_aux gff },
{ rw fnn,
have : ∀b (n : pos_num), (cond b ↑n 0 : ℕ) = ↑(cond b (pos n) 0 : num) :=
by intros; cases b; refl,
induction m with m IH m IH generalizing n; cases n with n n,
any_goals { change one with 1 },
any_goals { change pos 1 with 1 },
any_goals { change pos_num.bit0 with pos_num.bit ff },
any_goals { change pos_num.bit1 with pos_num.bit tt },
any_goals { change ((1:num):ℕ) with nat.bit tt 0 },
all_goals {
repeat {
rw show ∀ b n, (pos (pos_num.bit b n) : ℕ) = nat.bit b ↑n,
by intros; cases b; refl },
rw nat.bitwise_bit },
any_goals { assumption },
any_goals { rw [nat.bitwise_zero, p11], cases g tt tt; refl },
any_goals { rw [nat.bitwise_zero_left, this, ← bit_to_nat, p1b] },
any_goals { rw [nat.bitwise_zero_right _ gff, this, ← bit_to_nat, pb1] },
all_goals { rw [← show ∀ n, ↑(p m n) = nat.bitwise g ↑m ↑n, from IH],
rw [← bit_to_nat, pbb] } }
end
@[simp, norm_cast] theorem lor_to_nat : ∀ m n, (lor m n : ℕ) = nat.lor m n :=
by apply bitwise_to_nat (λx y, pos (pos_num.lor x y)); intros; try {cases a}; try {cases b}; refl
@[simp, norm_cast] theorem land_to_nat : ∀ m n, (land m n : ℕ) = nat.land m n :=
by apply bitwise_to_nat pos_num.land; intros; try {cases a}; try {cases b}; refl
@[simp, norm_cast] theorem ldiff_to_nat : ∀ m n, (ldiff m n : ℕ) = nat.ldiff m n :=
by apply bitwise_to_nat pos_num.ldiff; intros; try {cases a}; try {cases b}; refl
@[simp, norm_cast] theorem lxor_to_nat : ∀ m n, (lxor m n : ℕ) = nat.lxor m n :=
by apply bitwise_to_nat pos_num.lxor; intros; try {cases a}; try {cases b}; refl
@[simp, norm_cast] theorem shiftl_to_nat (m n) : (shiftl m n : ℕ) = nat.shiftl m n :=
begin
cases m; dunfold shiftl, {symmetry, apply nat.zero_shiftl},
simp, induction n with n IH, {refl},
simp [pos_num.shiftl, nat.shiftl_succ], rw ←IH
end
@[simp, norm_cast] theorem shiftr_to_nat (m n) : (shiftr m n : ℕ) = nat.shiftr m n :=
begin
cases m with m; dunfold shiftr, {symmetry, apply nat.zero_shiftr},
induction n with n IH generalizing m, {cases m; refl},
cases m with m m; dunfold pos_num.shiftr,
{ rw [nat.shiftr_eq_div_pow], symmetry, apply nat.div_eq_of_lt,
exact @nat.pow_lt_pow_of_lt_right 2 dec_trivial 0 (n+1) (nat.succ_pos _) },
{ transitivity, apply IH,
change nat.shiftr m n = nat.shiftr (bit1 m) (n+1),
rw [add_comm n 1, nat.shiftr_add],
apply congr_arg (λx, nat.shiftr x n), unfold nat.shiftr,
change (bit1 ↑m : ℕ) with nat.bit tt m,
rw nat.div2_bit },
{ transitivity, apply IH,
change nat.shiftr m n = nat.shiftr (bit0 m) (n + 1),
rw [add_comm n 1, nat.shiftr_add],
apply congr_arg (λx, nat.shiftr x n), unfold nat.shiftr,
change (bit0 ↑m : ℕ) with nat.bit ff m,
rw nat.div2_bit }
end
@[simp] theorem test_bit_to_nat (m n) : test_bit m n = nat.test_bit m n :=
begin
cases m with m; unfold test_bit nat.test_bit,
{ change (zero : nat) with 0, rw nat.zero_shiftr, refl },
induction n with n IH generalizing m;
cases m; dunfold pos_num.test_bit, {refl},
{ exact (nat.bodd_bit _ _).symm },
{ exact (nat.bodd_bit _ _).symm },
{ change ff = nat.bodd (nat.shiftr 1 (n + 1)),
rw [add_comm, nat.shiftr_add], change nat.shiftr 1 1 with 0,
rw nat.zero_shiftr; refl },
{ change pos_num.test_bit m n = nat.bodd (nat.shiftr (nat.bit tt m) (n + 1)),
rw [add_comm, nat.shiftr_add], unfold nat.shiftr,
rw nat.div2_bit, apply IH },
{ change pos_num.test_bit m n = nat.bodd (nat.shiftr (nat.bit ff m) (n + 1)),
rw [add_comm, nat.shiftr_add], unfold nat.shiftr,
rw nat.div2_bit, apply IH },
end
end num
namespace znum
variables {α : Type*}
open pos_num
@[simp, norm_cast] theorem cast_zero [has_zero α] [has_one α] [has_add α] [has_neg α] :
((0 : znum) : α) = 0 := rfl
@[simp] theorem cast_zero' [has_zero α] [has_one α] [has_add α] [has_neg α] :
(znum.zero : α) = 0 := rfl
@[simp, norm_cast] theorem cast_one [has_zero α] [has_one α] [has_add α] [has_neg α] :
((1 : znum) : α) = 1 := rfl
@[simp] theorem cast_pos [has_zero α] [has_one α] [has_add α] [has_neg α]
(n : pos_num) : (pos n : α) = n := rfl
@[simp] theorem cast_neg [has_zero α] [has_one α] [has_add α] [has_neg α]
(n : pos_num) : (neg n : α) = -n := rfl
@[simp, norm_cast] theorem cast_zneg [add_group α] [has_one α] : ∀ n, ((-n : znum) : α) = -n
| 0 := neg_zero.symm
| (pos p) := rfl
| (neg p) := (neg_neg _).symm
theorem neg_zero : (-0 : znum) = 0 := rfl
theorem zneg_pos (n : pos_num) : -pos n = neg n := rfl
theorem zneg_neg (n : pos_num) : -neg n = pos n := rfl
theorem zneg_zneg (n : znum) : - -n = n := by cases n; refl
theorem zneg_bit1 (n : znum) : -n.bit1 = (-n).bitm1 := by cases n; refl
theorem zneg_bitm1 (n : znum) : -n.bitm1 = (-n).bit1 := by cases n; refl
theorem zneg_succ (n : znum) : -n.succ = (-n).pred :=
by cases n; try {refl}; rw [succ, num.zneg_to_znum_neg]; refl
theorem zneg_pred (n : znum) : -n.pred = (-n).succ :=
by rw [← zneg_zneg (succ (-n)), zneg_succ, zneg_zneg]
@[simp, norm_cast] theorem neg_of_int : ∀ n, ((-n : ℤ) : znum) = -n
| (n+1:ℕ) := rfl
| 0 := rfl
| -[1+n] := (zneg_zneg _).symm
@[simp] theorem abs_to_nat : ∀ n, (abs n : ℕ) = int.nat_abs n
| 0 := rfl
| (pos p) := congr_arg int.nat_abs p.to_nat_to_int
| (neg p) := show int.nat_abs ((p:ℕ):ℤ) = int.nat_abs (- p),
by rw [p.to_nat_to_int, int.nat_abs_neg]
@[simp] theorem abs_to_znum : ∀ n : num, abs n.to_znum = n
| 0 := rfl
| (num.pos p) := rfl
@[simp, norm_cast] theorem cast_to_int [add_group α] [has_one α] : ∀ n : znum, ((n : ℤ) : α) = n
| 0 := rfl
| (pos p) := by rw [cast_pos, cast_pos, pos_num.cast_to_int]
| (neg p) := by rw [cast_neg, cast_neg, int.cast_neg, pos_num.cast_to_int]
theorem bit0_of_bit0 : ∀ n : znum, _root_.bit0 n = n.bit0
| 0 := rfl
| (pos a) := congr_arg pos a.bit0_of_bit0
| (neg a) := congr_arg neg a.bit0_of_bit0
theorem bit1_of_bit1 : ∀ n : znum, _root_.bit1 n = n.bit1
| 0 := rfl
| (pos a) := congr_arg pos a.bit1_of_bit1
| (neg a) := show pos_num.sub' 1 (_root_.bit0 a) = _,
by rw [pos_num.one_sub', a.bit0_of_bit0]; refl
@[simp, norm_cast] theorem cast_bit0 [add_group α] [has_one α] :
∀ n : znum, (n.bit0 : α) = bit0 n
| 0 := (add_zero _).symm
| (pos p) := by rw [znum.bit0, cast_pos, cast_pos]; refl
| (neg p) := by rw [znum.bit0, cast_neg, cast_neg, pos_num.cast_bit0,
_root_.bit0, _root_.bit0, neg_add_rev]
@[simp, norm_cast] theorem cast_bit1 [add_group α] [has_one α] :
∀ n : znum, (n.bit1 : α) = bit1 n
| 0 := by simp [znum.bit1, _root_.bit1, _root_.bit0]
| (pos p) := by rw [znum.bit1, cast_pos, cast_pos]; refl
| (neg p) := begin
rw [znum.bit1, cast_neg, cast_neg],
cases e : pred' p with a;
have : p = _ := (succ'_pred' p).symm.trans
(congr_arg num.succ' e),
{ change p=1 at this, subst p,
simp [_root_.bit1, _root_.bit0] },
{ rw [num.succ'] at this, subst p,
have : (↑(-↑a:ℤ) : α) = -1 + ↑(-↑a + 1 : ℤ), {simp [add_comm]},
simpa [_root_.bit1, _root_.bit0, -add_comm] },
end
@[simp] theorem cast_bitm1 [add_group α] [has_one α]
(n : znum) : (n.bitm1 : α) = bit0 n - 1 :=
begin
conv { to_lhs, rw ← zneg_zneg n },
rw [← zneg_bit1, cast_zneg, cast_bit1],
have : ((-1 + n + n : ℤ) : α) = (n + n + -1 : ℤ), {simp [add_comm, add_left_comm]},
simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg]
end
theorem add_zero (n : znum) : n + 0 = n := by cases n; refl
theorem zero_add (n : znum) : 0 + n = n := by cases n; refl
theorem add_one : ∀ n : znum, n + 1 = succ n
| 0 := rfl
| (pos p) := congr_arg pos p.add_one
| (neg p) := by cases p; refl
end znum
namespace pos_num
variables {α : Type*}
theorem cast_to_znum : ∀ n : pos_num, (n : znum) = znum.pos n
| 1 := rfl
| (bit0 p) := (znum.bit0_of_bit0 p).trans $ congr_arg _ (cast_to_znum p)
| (bit1 p) := (znum.bit1_of_bit1 p).trans $ congr_arg _ (cast_to_znum p)
theorem cast_sub' [add_group α] [has_one α] : ∀ m n : pos_num, (sub' m n : α) = m - n
| a 1 := by rw [sub'_one, num.cast_to_znum,
← num.cast_to_nat, pred'_to_nat, ← nat.sub_one];
simp [pos_num.cast_pos]
| 1 b := by rw [one_sub', num.cast_to_znum_neg, ← neg_sub, neg_inj,
← num.cast_to_nat, pred'_to_nat, ← nat.sub_one];
simp [pos_num.cast_pos]
| (bit0 a) (bit0 b) := begin
rw [sub', znum.cast_bit0, cast_sub'],
have : ((a + -b + (a + -b) : ℤ) : α) = a + a + (-b + -b), {simp [add_left_comm]},
simpa [_root_.bit0, sub_eq_add_neg]
end
| (bit0 a) (bit1 b) := begin
rw [sub', znum.cast_bitm1, cast_sub'],
have : ((-b + (a + (-b + -1)) : ℤ) : α) = (a + -1 + (-b + -b):ℤ),
{ simp [add_comm, add_left_comm] },
simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg]
end
| (bit1 a) (bit0 b) := begin
rw [sub', znum.cast_bit1, cast_sub'],
have : ((-b + (a + (-b + 1)) : ℤ) : α) = (a + 1 + (-b + -b):ℤ),
{ simp [add_comm, add_left_comm] },
simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg]
end
| (bit1 a) (bit1 b) := begin
rw [sub', znum.cast_bit0, cast_sub'],
have : ((-b + (a + -b) : ℤ) : α) = a + (-b + -b), {simp [add_left_comm]},
simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg]
end
theorem to_nat_eq_succ_pred (n : pos_num) : (n:ℕ) = n.pred' + 1 :=
by rw [← num.succ'_to_nat, n.succ'_pred']
theorem to_int_eq_succ_pred (n : pos_num) : (n:ℤ) = (n.pred' : ℕ) + 1 :=
by rw [← n.to_nat_to_int, to_nat_eq_succ_pred]; refl
end pos_num
namespace num
variables {α : Type*}
@[simp] theorem cast_sub' [add_group α] [has_one α] : ∀ m n : num, (sub' m n : α) = m - n
| 0 0 := (sub_zero _).symm
| (pos a) 0 := (sub_zero _).symm
| 0 (pos b) := (zero_sub _).symm
| (pos a) (pos b) := pos_num.cast_sub' _ _
@[simp] theorem of_nat_to_znum : ∀ n : ℕ, to_znum n = n
| 0 := rfl
| (n+1) := by rw [nat.cast_add_one, nat.cast_add_one,
znum.add_one, add_one, ← of_nat_to_znum]; cases (n:num); refl
@[simp] theorem of_nat_to_znum_neg (n : ℕ) : to_znum_neg n = -n :=
by rw [← of_nat_to_znum, zneg_to_znum]
theorem mem_of_znum' : ∀ {m : num} {n : znum}, m ∈ of_znum' n ↔ n = to_znum m
| 0 0 := ⟨λ _, rfl, λ _, rfl⟩
| (pos m) 0 := ⟨λ h, by cases h, λ h, by cases h⟩
| m (znum.pos p) := option.some_inj.trans $
by cases m; split; intro h; try {cases h}; refl
| m (znum.neg p) := ⟨λ h, by cases h, λ h, by cases m; cases h⟩
theorem of_znum'_to_nat : ∀ (n : znum), coe <$> of_znum' n = int.to_nat' n
| 0 := rfl
| (znum.pos p) := show _ = int.to_nat' p, by rw [← pos_num.to_nat_to_int p]; refl
| (znum.neg p) := congr_arg (λ x, int.to_nat' (-x)) $
show ((p.pred' + 1 : ℕ) : ℤ) = p, by rw ← succ'_to_nat; simp
@[simp] theorem of_znum_to_nat : ∀ (n : znum), (of_znum n : ℕ) = int.to_nat n
| 0 := rfl
| (znum.pos p) := show _ = int.to_nat p, by rw [← pos_num.to_nat_to_int p]; refl
| (znum.neg p) := congr_arg (λ x, int.to_nat (-x)) $
show ((p.pred' + 1 : ℕ) : ℤ) = p, by rw ← succ'_to_nat; simp
@[simp] theorem cast_of_znum [add_group α] [has_one α] (n : znum) :
(of_znum n : α) = int.to_nat n :=
by rw [← cast_to_nat, of_znum_to_nat]
@[simp, norm_cast] theorem sub_to_nat (m n) : ((m - n : num) : ℕ) = m - n :=
show (of_znum _ : ℕ) = _, by rw [of_znum_to_nat, cast_sub',
← to_nat_to_int, ← to_nat_to_int, int.to_nat_sub]
end num
namespace znum
variables {α : Type*}
@[simp, norm_cast] theorem cast_add [add_group α] [has_one α] : ∀ m n, ((m + n : znum) : α) = m + n
| 0 a := by cases a; exact (_root_.zero_add _).symm
| b 0 := by cases b; exact (_root_.add_zero _).symm
| (pos a) (pos b) := pos_num.cast_add _ _
| (pos a) (neg b) := pos_num.cast_sub' _ _
| (neg a) (pos b) := (pos_num.cast_sub' _ _).trans $
show ↑b + -↑a = -↑a + ↑b, by rw [← pos_num.cast_to_int a, ← pos_num.cast_to_int b,
← int.cast_neg, ← int.cast_add (-a)]; simp [add_comm]
| (neg a) (neg b) := show -(↑(a + b) : α) = -a + -b, by rw [
pos_num.cast_add, neg_eq_iff_neg_eq, neg_add_rev, neg_neg, neg_neg,
← pos_num.cast_to_int a, ← pos_num.cast_to_int b, ← int.cast_add]; simp [add_comm]
@[simp] theorem cast_succ [add_group α] [has_one α] (n) : ((succ n : znum) : α) = n + 1 :=
by rw [← add_one, cast_add, cast_one]
@[simp, norm_cast] theorem mul_to_int : ∀ m n, ((m * n : znum) : ℤ) = m * n
| 0 a := by cases a; exact (_root_.zero_mul _).symm
| b 0 := by cases b; exact (_root_.mul_zero _).symm
| (pos a) (pos b) := pos_num.cast_mul a b
| (pos a) (neg b) := show -↑(a * b) = ↑a * -↑b, by rw [pos_num.cast_mul, neg_mul_eq_mul_neg]
| (neg a) (pos b) := show -↑(a * b) = -↑a * ↑b, by rw [pos_num.cast_mul, neg_mul_eq_neg_mul]
| (neg a) (neg b) := show ↑(a * b) = -↑a * -↑b, by rw [pos_num.cast_mul, neg_mul_neg]
theorem cast_mul [ring α] (m n) : ((m * n : znum) : α) = m * n :=
by rw [← cast_to_int, mul_to_int, int.cast_mul, cast_to_int, cast_to_int]
@[simp, norm_cast] theorem of_to_int : Π (n : znum), ((n : ℤ) : znum) = n
| 0 := rfl
| (pos a) := by rw [cast_pos, ← pos_num.cast_to_nat,
int.cast_coe_nat', ← num.of_nat_to_znum, pos_num.of_to_nat]; refl
| (neg a) := by rw [cast_neg, neg_of_int, ← pos_num.cast_to_nat,
int.cast_coe_nat', ← num.of_nat_to_znum_neg, pos_num.of_to_nat]; refl
@[norm_cast]
theorem to_of_int : Π (n : ℤ), ((n : znum) : ℤ) = n
| (n : ℕ) := by rw [int.cast_coe_nat,
← num.of_nat_to_znum, num.cast_to_znum, ← num.cast_to_nat,
int.nat_cast_eq_coe_nat, num.to_of_nat]
| -[1+ n] := by rw [int.cast_neg_succ_of_nat, cast_zneg,
add_one, cast_succ, int.neg_succ_of_nat_eq,
← num.of_nat_to_znum, num.cast_to_znum, ← num.cast_to_nat,
int.nat_cast_eq_coe_nat, num.to_of_nat]
theorem to_int_inj {m n : znum} : (m : ℤ) = n ↔ m = n :=
⟨λ h, function.left_inverse.injective of_to_int h, congr_arg _⟩
@[simp, norm_cast] theorem of_int_cast [add_group α] [has_one α] (n : ℤ) : ((n : znum) : α) = n :=
by rw [← cast_to_int, to_of_int]
@[simp, norm_cast] theorem of_nat_cast [add_group α] [has_one α] (n : ℕ) : ((n : znum) : α) = n :=
of_int_cast n
@[simp] theorem of_int'_eq : ∀ n, znum.of_int' n = n
| (n : ℕ) := to_int_inj.1 $ by simp [znum.of_int']
| -[1+ n] := to_int_inj.1 $ by simp [znum.of_int']
theorem cmp_to_int : ∀ (m n), (ordering.cases_on (cmp m n) ((m:ℤ) < n) (m = n) ((n:ℤ) < m) : Prop)
| 0 0 := rfl
| (pos a) (pos b) := begin
have := pos_num.cmp_to_nat a b; revert this; dsimp [cmp];
cases pos_num.cmp a b; dsimp;
[simp, exact congr_arg pos, simp [gt]]
end
| (neg a) (neg b) := begin
have := pos_num.cmp_to_nat b a; revert this; dsimp [cmp];
cases pos_num.cmp b a; dsimp;
[simp, simp {contextual := tt}, simp [gt]]
end
| (pos a) 0 := pos_num.cast_pos _
| (pos a) (neg b) := lt_trans (neg_lt_zero.2 $ pos_num.cast_pos _) (pos_num.cast_pos _)
| 0 (neg b) := neg_lt_zero.2 $ pos_num.cast_pos _
| (neg a) 0 := neg_lt_zero.2 $ pos_num.cast_pos _
| (neg a) (pos b) := lt_trans (neg_lt_zero.2 $ pos_num.cast_pos _) (pos_num.cast_pos _)
| 0 (pos b) := pos_num.cast_pos _
@[norm_cast]
theorem lt_to_int {m n : znum} : (m:ℤ) < n ↔ m < n :=
show (m:ℤ) < n ↔ cmp m n = ordering.lt, from
match cmp m n, cmp_to_int m n with
| ordering.lt, h := by simp at h; simp [h]
| ordering.eq, h := by simp at h; simp [h, lt_irrefl]; exact dec_trivial
| ordering.gt, h := by simp [not_lt_of_gt h]; exact dec_trivial
end
theorem le_to_int {m n : znum} : (m:ℤ) ≤ n ↔ m ≤ n :=
by rw ← not_lt; exact not_congr lt_to_int
@[simp, norm_cast]
theorem cast_lt [linear_ordered_ring α] {m n : znum} : (m:α) < n ↔ m < n :=
by rw [← cast_to_int m, ← cast_to_int n, int.cast_lt, lt_to_int]
@[simp, norm_cast]
theorem cast_le [linear_ordered_ring α] {m n : znum} : (m:α) ≤ n ↔ m ≤ n :=
by rw ← not_lt; exact not_congr cast_lt
@[simp, norm_cast]
theorem cast_inj [linear_ordered_ring α] {m n : znum} : (m:α) = n ↔ m = n :=
by rw [← cast_to_int m, ← cast_to_int n, int.cast_inj, to_int_inj]
/--
This tactic tries to turn an (in)equality about `znum`s to one about `int`s by rewriting.
```lean
example (n : znum) (m : znum) : n ≤ n + m * m :=
begin
znum.transfer_rw,
exact le_add_of_nonneg_right (mul_self_nonneg _)
end
```
-/
meta def transfer_rw : tactic unit :=
`[repeat {rw ← to_int_inj <|> rw ← lt_to_int <|> rw ← le_to_int},
repeat {rw cast_add <|> rw mul_to_int <|> rw cast_one <|> rw cast_zero}]
/--
This tactic tries to prove (in)equalities about `znum`s by transfering them to the `int` world and
then trying to call `simp`.
```lean
example (n : znum) (m : znum) : n ≤ n + m * m :=
begin
znum.transfer,
exact mul_self_nonneg _
end
```
-/
meta def transfer : tactic unit :=
`[intros, transfer_rw, try {simp [add_comm, add_left_comm, mul_comm, mul_left_comm]}]
instance : linear_order znum :=
{ lt := (<),
lt_iff_le_not_le := by {intros a b, transfer_rw, apply lt_iff_le_not_le},
le := (≤),
le_refl := by transfer,
le_trans := by {intros a b c, transfer_rw, apply le_trans},
le_antisymm := by {intros a b, transfer_rw, apply le_antisymm},
le_total := by {intros a b, transfer_rw, apply le_total},
decidable_eq := znum.decidable_eq,
decidable_le := znum.decidable_le,
decidable_lt := znum.decidable_lt }
instance : add_comm_group znum :=
{ add := (+),
add_assoc := by transfer,
zero := 0,
zero_add := zero_add,
add_zero := add_zero,
add_comm := by transfer,
neg := has_neg.neg,
add_left_neg := by transfer }
instance : linear_ordered_comm_ring znum :=
{ mul := (*),
mul_assoc := by transfer,
one := 1,
one_mul := by transfer,
mul_one := by transfer,
left_distrib := by {transfer, simp [mul_add]},
right_distrib := by {transfer, simp [mul_add, mul_comm]},
mul_comm := by transfer,
exists_pair_ne := ⟨0, 1, dec_trivial⟩,
add_le_add_left := by {intros a b h c, revert h, transfer_rw, exact λ h, add_le_add_left h c},
mul_pos := λ a b, show 0 < a → 0 < b → 0 < a * b, by {transfer_rw, apply mul_pos},
zero_le_one := dec_trivial,
..znum.linear_order, ..znum.add_comm_group }
@[simp, norm_cast] theorem dvd_to_int (m n : znum) : (m : ℤ) ∣ n ↔ m ∣ n :=
⟨λ ⟨k, e⟩, ⟨k, by rw [← of_to_int n, e]; simp⟩,
λ ⟨k, e⟩, ⟨k, by simp [e]⟩⟩
end znum
namespace pos_num
theorem divmod_to_nat_aux {n d : pos_num} {q r : num}
(h₁ : (r:ℕ) + d * _root_.bit0 q = n)
(h₂ : (r:ℕ) < 2 * d) :
((divmod_aux d q r).2 + d * (divmod_aux d q r).1 : ℕ) = ↑n ∧
((divmod_aux d q r).2 : ℕ) < d :=
begin
unfold divmod_aux,
have : ∀ {r₂}, num.of_znum' (num.sub' r (num.pos d)) = some r₂ ↔ (r : ℕ) = r₂ + d,
{ intro r₂,
apply num.mem_of_znum'.trans,
rw [← znum.to_int_inj, num.cast_to_znum,
num.cast_sub', sub_eq_iff_eq_add, ← int.coe_nat_inj'],
simp },
cases e : num.of_znum' (num.sub' r (num.pos d)) with r₂;
simp [divmod_aux],
{ refine ⟨h₁, lt_of_not_ge (λ h, _)⟩,
cases nat.le.dest h with r₂ e',
rw [← num.to_of_nat r₂, add_comm] at e',
cases e.symm.trans (this.2 e'.symm) },
{ have := this.1 e,
split,
{ rwa [_root_.bit1, add_comm _ 1, mul_add, mul_one,
← add_assoc, ← this] },
{ rwa [this, two_mul, add_lt_add_iff_right] at h₂ } }
end
theorem divmod_to_nat (d n : pos_num) :
(n / d : ℕ) = (divmod d n).1 ∧
(n % d : ℕ) = (divmod d n).2 :=
begin
rw nat.div_mod_unique (pos_num.cast_pos _),
induction n with n IH n IH,
{ exact divmod_to_nat_aux (by simp; refl)
(nat.mul_le_mul_left 2
(pos_num.cast_pos d : (0 : ℕ) < d)) },
{ unfold divmod,
cases divmod d n with q r, simp only [divmod] at IH ⊢,
apply divmod_to_nat_aux; simp,
{ rw [_root_.bit1, _root_.bit1, add_right_comm,
bit0_eq_two_mul ↑n, ← IH.1,
mul_add, ← bit0_eq_two_mul,
mul_left_comm, ← bit0_eq_two_mul] },
{ rw ← bit0_eq_two_mul,
exact nat.bit1_lt_bit0 IH.2 } },
{ unfold divmod,
cases divmod d n with q r, simp only [divmod] at IH ⊢,
apply divmod_to_nat_aux; simp,
{ rw [bit0_eq_two_mul ↑n, ← IH.1,
mul_add, ← bit0_eq_two_mul,
mul_left_comm, ← bit0_eq_two_mul] },
{ rw ← bit0_eq_two_mul,
exact nat.bit0_lt IH.2 } }
end
@[simp] theorem div'_to_nat (n d) : (div' n d : ℕ) = n / d :=
(divmod_to_nat _ _).1.symm
@[simp] theorem mod'_to_nat (n d) : (mod' n d : ℕ) = n % d :=
(divmod_to_nat _ _).2.symm
end pos_num
namespace num
@[simp, norm_cast] theorem div_to_nat : ∀ n d, ((n / d : num) : ℕ) = n / d
| 0 0 := rfl
| 0 (pos d) := (nat.zero_div _).symm
| (pos n) 0 := (nat.div_zero _).symm
| (pos n) (pos d) := pos_num.div'_to_nat _ _
@[simp, norm_cast] theorem mod_to_nat : ∀ n d, ((n % d : num) : ℕ) = n % d
| 0 0 := rfl
| 0 (pos d) := (nat.zero_mod _).symm
| (pos n) 0 := (nat.mod_zero _).symm
| (pos n) (pos d) := pos_num.mod'_to_nat _ _
theorem gcd_to_nat_aux : ∀ {n} {a b : num},
a ≤ b → (a * b).nat_size ≤ n → (gcd_aux n a b : ℕ) = nat.gcd a b
| 0 0 b ab h := (nat.gcd_zero_left _).symm
| 0 (pos a) 0 ab h := (not_lt_of_ge ab).elim rfl
| 0 (pos a) (pos b) ab h :=
(not_lt_of_le h).elim $ pos_num.nat_size_pos _
| (nat.succ n) 0 b ab h := (nat.gcd_zero_left _).symm
| (nat.succ n) (pos a) b ab h := begin
simp [gcd_aux],
rw [nat.gcd_rec, gcd_to_nat_aux, mod_to_nat], {refl},
{ rw [← le_to_nat, mod_to_nat],
exact le_of_lt (nat.mod_lt _ (pos_num.cast_pos _)) },
rw [nat_size_to_nat, mul_to_nat, nat.size_le] at h ⊢,
rw [mod_to_nat, mul_comm],
rw [pow_succ', ← nat.mod_add_div b (pos a)] at h,
refine lt_of_mul_lt_mul_right (lt_of_le_of_lt _ h) (nat.zero_le 2),
rw [mul_two, mul_add],
refine add_le_add_left (nat.mul_le_mul_left _
(le_trans (le_of_lt (nat.mod_lt _ (pos_num.cast_pos _))) _)) _,
suffices : 1 ≤ _, simpa using nat.mul_le_mul_left (pos a) this,
rw [nat.le_div_iff_mul_le _ _ a.cast_pos, one_mul],
exact le_to_nat.2 ab
end
@[simp] theorem gcd_to_nat : ∀ a b, (gcd a b : ℕ) = nat.gcd a b :=
have ∀ a b : num, (a * b).nat_size ≤ a.nat_size + b.nat_size,
begin
intros,
simp [nat_size_to_nat],
rw [nat.size_le, pow_add],
exact mul_lt_mul'' (nat.lt_size_self _)
(nat.lt_size_self _) (nat.zero_le _) (nat.zero_le _)
end,
begin
intros, unfold gcd, split_ifs,
{ exact gcd_to_nat_aux h (this _ _) },
{ rw nat.gcd_comm,
exact gcd_to_nat_aux (le_of_not_le h) (this _ _) }
end
theorem dvd_iff_mod_eq_zero {m n : num} : m ∣ n ↔ n % m = 0 :=
by rw [← dvd_to_nat, nat.dvd_iff_mod_eq_zero,
← to_nat_inj, mod_to_nat]; refl
instance decidable_dvd : decidable_rel ((∣) : num → num → Prop)
| a b := decidable_of_iff' _ dvd_iff_mod_eq_zero
end num
instance pos_num.decidable_dvd : decidable_rel ((∣) : pos_num → pos_num → Prop)
| a b := num.decidable_dvd _ _
namespace znum
@[simp, norm_cast] theorem div_to_int : ∀ n d, ((n / d : znum) : ℤ) = n / d
| 0 0 := rfl
| 0 (pos d) := (int.zero_div _).symm
| 0 (neg d) := (int.zero_div _).symm
| (pos n) 0 := (int.div_zero _).symm
| (neg n) 0 := (int.div_zero _).symm
| (pos n) (pos d) := (num.cast_to_znum _).trans $
by rw ← num.to_nat_to_int; simp
| (pos n) (neg d) := (num.cast_to_znum_neg _).trans $
by rw ← num.to_nat_to_int; simp
| (neg n) (pos d) := show - _ = (-_/↑d), begin
rw [n.to_int_eq_succ_pred, d.to_int_eq_succ_pred,
← pos_num.to_nat_to_int, num.succ'_to_nat,
num.div_to_nat],
change -[1+ n.pred' / ↑d] = -[1+ n.pred' / (d.pred' + 1)],
rw d.to_nat_eq_succ_pred
end
| (neg n) (neg d) := show ↑(pos_num.pred' n / num.pos d).succ' = (-_ / -↑d), begin
rw [n.to_int_eq_succ_pred, d.to_int_eq_succ_pred,
← pos_num.to_nat_to_int, num.succ'_to_nat,
num.div_to_nat],
change (nat.succ (_/d) : ℤ) = nat.succ (n.pred'/(d.pred' + 1)),
rw d.to_nat_eq_succ_pred
end
@[simp, norm_cast] theorem mod_to_int : ∀ n d, ((n % d : znum) : ℤ) = n % d
| 0 d := (int.zero_mod _).symm
| (pos n) d := (num.cast_to_znum _).trans $
by rw [← num.to_nat_to_int, cast_pos, num.mod_to_nat,
← pos_num.to_nat_to_int, abs_to_nat]; refl
| (neg n) d := (num.cast_sub' _ _).trans $
by rw [← num.to_nat_to_int, cast_neg, ← num.to_nat_to_int,
num.succ_to_nat, num.mod_to_nat, abs_to_nat,
← int.sub_nat_nat_eq_coe, n.to_int_eq_succ_pred]; refl
@[simp] theorem gcd_to_nat (a b) : (gcd a b : ℕ) = int.gcd a b :=
(num.gcd_to_nat _ _).trans $ by simpa
theorem dvd_iff_mod_eq_zero {m n : znum} : m ∣ n ↔ n % m = 0 :=
by rw [← dvd_to_int, int.dvd_iff_mod_eq_zero,
← to_int_inj, mod_to_int]; refl
instance : decidable_rel ((∣) : znum → znum → Prop)
| a b := decidable_of_iff' _ dvd_iff_mod_eq_zero
end znum
namespace int
/-- Cast a `snum` to the corresponding integer. -/
def of_snum : snum → ℤ :=
snum.rec' (λ a, cond a (-1) 0) (λa p IH, cond a (bit1 IH) (bit0 IH))
instance snum_coe : has_coe snum ℤ := ⟨of_snum⟩
end int
instance : has_lt snum := ⟨λa b, (a : ℤ) < b⟩
instance : has_le snum := ⟨λa b, (a : ℤ) ≤ b⟩
|
c1f0f09c656cfc9a30db5f0405037fab1f605dc0 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/real/conjugate_exponents_auto.lean | 0cd19a5fc57aa732e5b073960ffe0123216db977 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,820 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel, Yury Kudryashov
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.real.ennreal
import Mathlib.PostPort
universes l
namespace Mathlib
/-!
# Real conjugate exponents
`p.is_conjugate_exponent q` registers the fact that the real numbers `p` and `q` are `> 1` and
satisfy `1/p + 1/q = 1`. This property shows up often in analysis, especially when dealing with
`L^p` spaces.
We make several basic facts available through dot notation in this situation.
We also introduce `p.conjugate_exponent` for `p / (p-1)`. When `p > 1`, it is conjugate to `p`.
-/
namespace real
/-- Two real exponents `p, q` are conjugate if they are `> 1` and satisfy the equality
`1/p + 1/q = 1`. This condition shows up in many theorems in analysis, notably related to `L^p`
norms. -/
structure is_conjugate_exponent (p : ℝ) (q : ℝ) where
one_lt : 1 < p
inv_add_inv_conj : 1 / p + 1 / q = 1
/-- The conjugate exponent of `p` is `q = p/(p-1)`, so that `1/p + 1/q = 1`. -/
def conjugate_exponent (p : ℝ) : ℝ := p / (p - 1)
namespace is_conjugate_exponent
/- Register several non-vanishing results following from the fact that `p` has a conjugate exponent
`q`: many computations using these exponents require clearing out denominators, which can be done
with `field_simp` given a proof that these denominators are non-zero, so we record the most usual
ones. -/
theorem pos {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 0 < p :=
lt_trans zero_lt_one (one_lt h)
theorem nonneg {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 0 ≤ p := le_of_lt (pos h)
theorem ne_zero {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : p ≠ 0 := ne_of_gt (pos h)
theorem sub_one_pos {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 0 < p - 1 :=
iff.mpr sub_pos (one_lt h)
theorem sub_one_ne_zero {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : p - 1 ≠ 0 :=
ne_of_gt (sub_one_pos h)
theorem one_div_pos {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 0 < 1 / p :=
iff.mpr one_div_pos (pos h)
theorem one_div_nonneg {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 0 ≤ 1 / p :=
le_of_lt (one_div_pos h)
theorem one_div_ne_zero {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 1 / p ≠ 0 :=
ne_of_gt (one_div_pos h)
theorem conj_eq {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : q = p / (p - 1) := sorry
theorem conjugate_eq {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : conjugate_exponent p = q :=
Eq.symm (conj_eq h)
theorem sub_one_mul_conj {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : (p - 1) * q = p :=
mul_comm q (p - 1) ▸ iff.mp (eq_div_iff (sub_one_ne_zero h)) (conj_eq h)
theorem mul_eq_add {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : p * q = p + q := sorry
protected theorem symm {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) :
is_conjugate_exponent q p :=
sorry
theorem one_lt_nnreal {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) : 1 < nnreal.of_real p :=
sorry
theorem inv_add_inv_conj_nnreal {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) :
1 / nnreal.of_real p + 1 / nnreal.of_real q = 1 :=
sorry
theorem inv_add_inv_conj_ennreal {p : ℝ} {q : ℝ} (h : is_conjugate_exponent p q) :
1 / ennreal.of_real p + 1 / ennreal.of_real q = 1 :=
sorry
end is_conjugate_exponent
theorem is_conjugate_exponent_iff {p : ℝ} {q : ℝ} (h : 1 < p) :
is_conjugate_exponent p q ↔ q = p / (p - 1) :=
sorry
theorem is_conjugate_exponent_conjugate_exponent {p : ℝ} (h : 1 < p) :
is_conjugate_exponent p (conjugate_exponent p) :=
iff.mpr (is_conjugate_exponent_iff h) rfl
end Mathlib |
9bea22b982217342dbcddd149abd91f0b2acd8ce | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/defaultEliminator.lean | fa67666a91889516cc191ab64ec775e72c62bc2a | [
"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 | 985 | lean | @[eliminator] protected def Nat.recDiag {motive : Nat → Nat → Sort u}
(zero_zero : motive 0 0)
(succ_zero : (x : Nat) → motive x 0 → motive (x + 1) 0)
(zero_succ : (y : Nat) → motive 0 y → motive 0 (y + 1))
(succ_succ : (x y : Nat) → motive x y → motive (x + 1) (y + 1))
(x y : Nat) : motive x y :=
let rec go : (x y : Nat) → motive x y
| 0, 0 => zero_zero
| x+1, 0 => succ_zero x (go x 0)
| 0, y+1 => zero_succ y (go 0 y)
| x+1, y+1 => succ_succ x y (go x y)
go x y
termination_by go x y => (x, y)
def f (x y : Nat) :=
match x, y with
| 0, 0 => 1
| x+1, 0 => f x 0
| 0, y+1 => f 0 y
| x+1, y+1 => f x y
termination_by f x y => (x, y)
example (x y : Nat) : f x y > 0 := by
induction x, y with
| zero_zero => decide
| succ_zero x ih => simp [f, ih]
| zero_succ y ih => simp [f, ih]
| succ_succ x y ih => simp [f, ih]
example (x y : Nat) : f x y > 0 := by
induction x, y <;> simp [f, *]
|
106fb0e5d187977f064c501d9c6e793efa383472 | 0f5090f82d527e0df5bf3adac9f9e2e1d81d71e2 | /src/apurva/complex_dot_product.lean | a7ec0300227012ac9f9bb1e63f30e8ac7f8f7912 | [] | no_license | apurvanakade/mc2020-lean-projects | 36eb42c4baccc37183635c36f8e1b3afa4ec1230 | 02466225aa629ab1232043bcc0a053a099fdb939 | refs/heads/master | 1,688,791,717,534 | 1,597,874,092,000 | 1,597,874,092,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,164 | lean | import data.complex.basic
import data.fintype.basic
import data.matrix.basic
import linear_algebra.finite_dimensional
import .complex_transpose
import analysis.normed_space.basic
import tactic
noncomputable theory
open_locale big_operators
open_locale matrix
universes u v
variables {n m : Type u} [fintype n] [fintype m]
local notation `Euc` := (n → ℂ)
namespace vector
/-- Complex conjugate of a vector.-/
@[simp]
def conj (v : Euc) : Euc :=
λ i, complex.conj (v i)
@[simp]
lemma conj_val (v : Euc) (i : n) : conj v i = complex.conj (v i) := rfl
@[simp]
lemma smul_conj (x : ℂ) (v : Euc) :
conj (x • v) = x.conj • conj(v) :=
by ext; simp
@[simp]
def complex_dot_product (v : Euc) (w : Euc) : ℂ :=
-- ∑ i, complex.conj (v i) * w i
matrix.dot_product v (conj w)
@[simp]
lemma complex_dot_product_zero {v : Euc} : complex_dot_product v (0 : Euc) = 0 :=
by {ext; simp}
lemma complex_dot_product_symm {v w : Euc} :
(complex_dot_product v w) = (complex_dot_product w v).conj :=
sorry
@[simp]
lemma zero_complex_dot_product {v : Euc} : complex_dot_product (0 : Euc) v = 0 :=
by {ext;simp}
@[simp]
lemma add_complex_dot_product {v w u : Euc} :
complex_dot_product (v + w) u = complex_dot_product v u + complex_dot_product w u :=
by {ext;simp}
@[simp]
lemma complex_dot_product_add {v w u : Euc} :
complex_dot_product v (w + u) = complex_dot_product v w + complex_dot_product v u :=
by {rw [complex_dot_product_symm, @complex_dot_product_symm _ _ v w, @complex_dot_product_symm _ _ v u],
simp only [conj, ring_hom.map_add, eq_self_iff_true, add_complex_dot_product]}
@[simp]
lemma complex_dot_product_sub {v w u : Euc} :
complex_dot_product (v - w) u = complex_dot_product v u - complex_dot_product w u :=
begin
suffices : complex_dot_product v u = complex_dot_product (v - w) u + complex_dot_product w u,
by exact eq_sub_of_add_eq (eq.symm this),
set u' := v - w with hu,
have : v = u' + w, by {exact eq_add_of_sub_eq rfl},
rw this, apply add_complex_dot_product,
end
@[simp]
lemma diagonal_complex_dot_product [decidable_eq n] (v w : Euc) (i : n) :
complex_dot_product (matrix.diagonal v i) w = v i * w i :=
have ∀ j ≠ i, matrix.diagonal v i j * w j = 0 := λ j hij,
by simp [matrix.diagonal_val_ne' hij],
by convert finset.sum_eq_single i (λ j _, this j) _; sorry
@[simp]
lemma complex_dot_product_diagonal [decidable_eq n] (v w : Euc) (i : n) :
complex_dot_product v (matrix.diagonal w i) = v i * w i :=
have ∀ j ≠ i, v j * matrix.diagonal w i j = 0 := λ j hij, by simp [matrix.diagonal_val_ne' hij],
by convert finset.sum_eq_single i (λ j _, this j) _; sorry
@[simp] lemma complex_dot_product_diagonal' [decidable_eq n] (v w : Euc) (i : n) :
complex_dot_product v (λ j, matrix.diagonal w j i) = v i * w i :=
have ∀ j ≠ i, v j * matrix.diagonal w j i = 0 := λ j hij, by simp [matrix.diagonal_val_ne hij],
by convert finset.sum_eq_single i (λ j _, this j) _; sorry
@[simp] lemma neg_complex_dot_product (v w : Euc) :
complex_dot_product (-v) w = - complex_dot_product v w :=
by simp [matrix.dot_product]
@[simp] lemma complex_dot_product_neg (v w : Euc) : complex_dot_product v (-w) = - complex_dot_product v w :=
by simp only [matrix.dot_product, conj, pi.neg_apply, mul_neg_eq_neg_mul_symm, ring_hom.map_neg, finset.sum_neg_distrib,
complex_dot_product]
@[simp] lemma smul_complex_dot_product (x : ℂ) (v w : Euc) :
complex_dot_product (x • v) w = x * complex_dot_product v w :=
by simp only [complex_dot_product, matrix.smul_dot_product]
@[simp] lemma complex_dot_product_smul (x : ℂ) (v w : Euc) :
complex_dot_product v (x • w) = x.conj * complex_dot_product v w :=
by simp only [complex_dot_product, smul_conj, matrix.dot_product_smul]
@[simp]
def orthogonal (v : Euc) (w : Euc) : Prop :=
(complex_dot_product v w = 0)
def complex_norm (v : Euc) := complex_dot_product v v
end vector
section subspace
def is_orthogonal (S : set Euc) : subspace ℂ Euc → Prop :=
λ W, ∀ w ∈ W, ∀ v ∈ S, (vector.complex_dot_product v w = 0)
def orthogonal_complement (S : set Euc) : subspace ℂ Euc :=
Inf {W | is_orthogonal S W}
end subspace |
e9db9bb11bfafc3e90a07523121cf55b5ffef461 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Compiler/LCNF/Passes.lean | b4742a6856b0ff2845f0f4e5c88156154bbde306 | [
"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 | 4,369 | lean | /-
Copyright (c) 2022 Henrik Böving. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Henrik Böving
-/
import Lean.Compiler.LCNF.PassManager
import Lean.Compiler.LCNF.PullLetDecls
import Lean.Compiler.LCNF.CSE
import Lean.Compiler.LCNF.Simp
import Lean.Compiler.LCNF.PullFunDecls
import Lean.Compiler.LCNF.ReduceJpArity
import Lean.Compiler.LCNF.JoinPoints
import Lean.Compiler.LCNF.Specialize
import Lean.Compiler.LCNF.PhaseExt
import Lean.Compiler.LCNF.ToMono
import Lean.Compiler.LCNF.LambdaLifting
import Lean.Compiler.LCNF.FloatLetIn
import Lean.Compiler.LCNF.ReduceArity
import Lean.Compiler.LCNF.ElimDeadBranches
namespace Lean.Compiler.LCNF
open PassInstaller
def init : Pass where
name := `init
run := fun decls => do
decls.forM (·.saveBase)
return decls
phase := .base
-- Helper pass used for debugging purposes
def trace (phase := Phase.base) : Pass where
name := `trace
run := pure
phase := phase
def saveBase : Pass :=
.mkPerDeclaration `saveBase (fun decl => do (← normalizeFVarIds decl).saveBase; return decl) .base
def saveMono : Pass :=
.mkPerDeclaration `saveMono (fun decl => do (← normalizeFVarIds decl).saveMono; return decl) .mono
def builtinPassManager : PassManager := {
passes := #[
init,
pullInstances,
cse,
simp,
floatLetIn,
findJoinPoints,
pullFunDecls,
reduceJpArity,
/-
We apply `implementedBy` replacements before `specialize` to ensure we specialize the replacement.
One possible improvement is to perform only the replacements if the target declaration is a specialization target,
and on phase 2 (aka mono) perform the remaining replacements.
-/
simp { etaPoly := true, inlinePartial := true, implementedBy := true } (occurrence := 1),
eagerLambdaLifting,
specialize,
simp (occurrence := 2),
cse (occurrence := 1),
saveBase, -- End of base phase
toMono,
simp (occurrence := 3) (phase := .mono),
reduceJpArity (phase := .mono),
extendJoinPointContext (phase := .mono) (occurrence := 0),
floatLetIn (phase := .mono) (occurrence := 1),
reduceArity,
commonJoinPointArgs,
simp (occurrence := 4) (phase := .mono),
floatLetIn (phase := .mono) (occurrence := 2),
elimDeadBranches,
lambdaLifting,
extendJoinPointContext (phase := .mono) (occurrence := 1),
simp (occurrence := 5) (phase := .mono),
cse (occurrence := 2) (phase := .mono),
saveMono -- End of mono phase
]
}
def runImportedDecls (importedDeclNames : Array (Array Name)) : CoreM PassManager := do
let mut m := builtinPassManager
for declNames in importedDeclNames do
for declName in declNames do
m ← runFromDecl m declName
return m
builtin_initialize passManagerExt : PersistentEnvExtension Name (Name × PassManager) (List Name × PassManager) ←
registerPersistentEnvExtension {
mkInitial := return ([], builtinPassManager)
addImportedFn := fun ns => return ([], ← ImportM.runCoreM <| runImportedDecls ns)
addEntryFn := fun (installerDeclNames, _) (installerDeclName, managerNew) => (installerDeclName :: installerDeclNames, managerNew)
exportEntriesFn := fun s => s.1.reverse.toArray
}
def getPassManager : CoreM PassManager :=
return passManagerExt.getState (← getEnv) |>.2
def addPass (declName : Name) : CoreM Unit := do
let info ← getConstInfo declName
match info.type with
| .const `Lean.Compiler.LCNF.PassInstaller .. =>
let managerNew ← runFromDecl (← getPassManager) declName
modifyEnv fun env => passManagerExt.addEntry env (declName, managerNew)
| _ =>
throwError "invalid 'cpass' only 'PassInstaller's can be added via the 'cpass' attribute: {info.type}"
builtin_initialize
registerBuiltinAttribute {
name := `cpass
descr := "compiler passes for the code generator"
add := fun declName stx kind => do
Attribute.Builtin.ensureNoArgs stx
unless kind == AttributeKind.global do throwError "invalid attribute 'cpass', must be global"
discard <| addPass declName
applicationTime := .afterCompilation
}
builtin_initialize
registerTraceClass `Compiler.saveBase (inherited := true)
registerTraceClass `Compiler.saveMono (inherited := true)
registerTraceClass `Compiler.trace (inherited := true)
end Lean.Compiler.LCNF
|
e0816ea9aea088424ea6f257b5b7a8b08969b6de | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/long_term.lean | ba8e2844deb0b1a21dacdef6c0fb6e4e6c9b17a4 | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 350 | lean | def long_term :=
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1) =
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1)+
(1+1+1+1+1+1+1+1+1)
#print long_term
|
25d8429a5c8d0cd64b27a610edc31967c95b1767 | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/algebra/category/Module/limits.lean | f37d2eeede8a98f13a6ad9211771c87c28e618f8 | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 4,427 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import algebra.category.Module.basic
import algebra.category.Group.limits
/-!
# The category of R-modules has all limits
Further, these limits are preserved by the forgetful functor --- that is,
the underlying types are just the limits in the category of types.
-/
open category_theory
open category_theory.limits
universes u v
noncomputable theory
namespace Module
variables {R : Type u} [ring R]
variables {J : Type v} [small_category J]
instance add_comm_group_obj (F : J ⥤ Module.{v} R) (j) :
add_comm_group ((F ⋙ forget (Module R)).obj j) :=
by { change add_comm_group (F.obj j), apply_instance }
instance module_obj (F : J ⥤ Module.{v} R) (j) :
module R ((F ⋙ forget (Module R)).obj j) :=
by { change module R (F.obj j), apply_instance }
/--
The flat sections of a functor into `Module R` form a submodule of all sections.
-/
def sections_submodule (F : J ⥤ Module R) :
submodule R (Π j, F.obj j) :=
{ carrier := (F ⋙ forget (Module R)).sections,
smul_mem' := λ r s sh j j' f,
begin
simp only [forget_map_eq_coe, functor.comp_map, pi.smul_apply, linear_map.map_smul],
dsimp [functor.sections] at sh,
rw sh f,
end,
..(AddGroup.sections_add_subgroup (F ⋙ forget₂ (Module R) AddCommGroup.{v} ⋙ forget₂ AddCommGroup AddGroup.{v})) }
instance limit_add_comm_group (F : J ⥤ Module R) :
add_comm_group (types.limit_cone (F ⋙ forget (Module.{v} R))).X :=
begin
change add_comm_group (sections_submodule F),
apply_instance,
end
instance limit_module (F : J ⥤ Module R) :
module R (types.limit_cone (F ⋙ forget (Module.{v} R))).X :=
begin
change module R (sections_submodule F),
apply_instance,
end
/-- `limit.π (F ⋙ forget Ring) j` as a `ring_hom`. -/
def limit_π_linear_map (F : J ⥤ Module R) (j) :
(types.limit_cone (F ⋙ forget (Module.{v} R))).X →ₗ[R] (F ⋙ forget (Module R)).obj j :=
{ to_fun := (types.limit_cone (F ⋙ forget (Module R))).π.app j,
map_smul' := λ x y, rfl,
map_add' := λ x y, rfl }
namespace has_limits
-- The next two definitions are used in the construction of `has_limits (Module R)`.
-- After that, the limits should be constructed using the generic limits API,
-- e.g. `limit F`, `limit.cone F`, and `limit.is_limit F`.
/--
Construction of a limit cone in `Module R`.
(Internal use only; use the limits API.)
-/
def limit_cone (F : J ⥤ Module R) : cone F :=
{ X := Module.of R (types.limit_cone (F ⋙ forget _)).X,
π :=
{ app := limit_π_linear_map F,
naturality' := λ j j' f,
linear_map.coe_inj ((types.limit_cone (F ⋙ forget _)).π.naturality f) } }
/--
Witness that the limit cone in `Module R` is a limit cone.
(Internal use only; use the limits API.)
-/
def limit_cone_is_limit (F : J ⥤ Module R) : is_limit (limit_cone F) :=
begin
refine is_limit.of_faithful
(forget (Module R)) (types.limit_cone_is_limit _)
(λ s, ⟨_, _, _⟩) (λ s, rfl); tidy
end
end has_limits
open has_limits
/-- The category of R-modules has all limits. -/
@[irreducible]
instance has_limits : has_limits (Module.{v} R) :=
{ has_limits_of_shape := λ J 𝒥, by exactI
{ has_limit := λ F, has_limit.mk
{ cone := limit_cone F,
is_limit := limit_cone_is_limit F } } }
/--
An auxiliary declaration to speed up typechecking.
-/
def forget₂_AddCommGroup_preserves_limits_aux (F : J ⥤ Module R) :
is_limit ((forget₂ (Module R) AddCommGroup).map_cone (limit_cone F)) :=
AddCommGroup.limit_cone_is_limit (F ⋙ forget₂ (Module R) AddCommGroup)
/--
The forgetful functor from R-modules to abelian groups preserves all limits.
-/
instance forget₂_AddCommGroup_preserves_limits : preserves_limits (forget₂ (Module R) AddCommGroup.{v}) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ F, preserves_limit_of_preserves_limit_cone
(limit_cone_is_limit F) (forget₂_AddCommGroup_preserves_limits_aux F) } }
/--
The forgetful functor from R-modules to types preserves all limits.
-/
instance forget_preserves_limits : preserves_limits (forget (Module R)) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ F, preserves_limit_of_preserves_limit_cone
(limit_cone_is_limit F) (types.limit_cone_is_limit (F ⋙ forget _)) } }
end Module
|
90e280efc816c8d50783a39009f86c07ce98e39c | fe208a542cea7b2d6d7ff79f94d535f6d11d814a | /src/Logic/question6.lean | 2113c3b8499a36bac54581ff3e17a7d93049457d | [] | no_license | ImperialCollegeLondon/M1F_room_342_questions | c4b98b14113fe900a7f388762269305faff73e63 | 63de9a6ab9c27a433039dd5530bc9b10b1d227f7 | refs/heads/master | 1,585,807,312,561 | 1,545,232,972,000 | 1,545,232,972,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 975 | lean | /- Question 6 on John Britnell's handout
for M1F group project on 18/10/18.
Formalised by Kevin Buzzard, with many
thanks to Gabriel Ebner for showing him
how to do it.
-/
inductive fml
| atom (i : ℕ)
| imp (a b : fml)
| not (a : fml)
open fml
infixr ` →' `:50 := imp -- right associative
prefix `¬' `:40 := fml.not
inductive prf : fml → Type
| axk (p q) : prf (p →' q →' p)
| axs (p q r) : prf $ (p →' q →' r) →' (p →' q) →' (p →' r)
| axX (p q) : prf $ ((¬' q) →' (¬' p)) →' p →' q
| mp {p q} : prf (p →' q) → prf p → prf q -- bracket change
open prf
/-
-- example usage:
lemma p_of_p_of_p_of_q (p q : fml) : prf $ (p →' q) →' (p →' p) :=
begin
apply mp (axs p q p),
exact (axk p q)
end
-- or just
lemma p_of_p_of_p_of_q' (p q : fml) : prf $ (p →' q) →' (p →' p) :=
mp (axs p q p) (axk p q)
-/
lemma Q6a (p : fml) : prf $ p →' p := sorry
theorem Q6b (p : fml) : prf $ p →' ¬' ¬' p := sorry |
04a0258ff94ad6f8e9c47603cc29eb3c102a668e | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/analysis/special_functions/bernstein.lean | a939c1f6b8e1bdaae320312b053ed0db0e1d9752 | [
"Apache-2.0"
] | permissive | stjordanis/mathlib | 51e286d19140e3788ef2c470bc7b953e4991f0c9 | 2568d41bca08f5d6bf39d915434c8447e21f42ee | refs/heads/master | 1,631,748,053,501 | 1,627,938,886,000 | 1,627,938,886,000 | 228,728,358 | 0 | 0 | Apache-2.0 | 1,576,630,588,000 | 1,576,630,587,000 | null | UTF-8 | Lean | false | false | 12,922 | lean | /-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import ring_theory.polynomial.bernstein
import topology.continuous_function.polynomial
/-!
# Bernstein approximations and Weierstrass' theorem
We prove that the Bernstein approximations
```
∑ k : fin (n+1), f (k/n : ℝ) * n.choose k * x^k * (1-x)^(n-k)
```
for a continuous function `f : C([0,1], ℝ)` converge uniformly to `f` as `n` tends to infinity.
Our proof follows [Richard Beals' *Analysis, an introduction*][beals-analysis], §7D.
The original proof, due to [Bernstein](bernstein1912) in 1912, is probabilistic,
and relies on Bernoulli's theorem,
which gives bounds for how quickly the observed frequencies in a
Bernoulli trial approach the underlying probability.
The proof here does not directly rely on Bernoulli's theorem,
but can also be given a probabilistic account.
* Consider a weighted coin which with probability `x` produces heads,
and with probability `1-x` produces tails.
* The value of `bernstein n k x` is the probability that
such a coin gives exactly `k` heads in a sequence of `n` tosses.
* If such an appearance of `k` heads results in a payoff of `f(k / n)`,
the `n`-th Bernstein approximation for `f` evaluated at `x` is the expected payoff.
* The main estimate in the proof bounds the probability that
the observed frequency of heads differs from `x` by more than some `δ`,
obtaining a bound of `(4 * n * δ^2)⁻¹`, irrespective of `x`.
* This ensures that for `n` large, the Bernstein approximation is (uniformly) close to the
payoff function `f`.
(You don't need to think in these terms to follow the proof below: it's a giant `calc` block!)
This result proves Weierstrass' theorem that polynomials are dense in `C([0,1], ℝ)`,
although we defer an abstract statement of this until later.
-/
noncomputable theory
open_locale classical
open_locale big_operators
open_locale bounded_continuous_function
open_locale unit_interval
/--
The Bernstein polynomials, as continuous functions on `[0,1]`.
-/
def bernstein (n ν : ℕ) : C(I, ℝ) :=
(bernstein_polynomial ℝ n ν).to_continuous_map_on I
@[simp] lemma bernstein_apply (n ν : ℕ) (x : I) :
bernstein n ν x = n.choose ν * x^ν * (1-x)^(n-ν) :=
begin
dsimp [bernstein, polynomial.to_continuous_map_on, polynomial.to_continuous_map,
bernstein_polynomial],
simp,
end
lemma bernstein_nonneg {n ν : ℕ} {x : I} :
0 ≤ bernstein n ν x :=
begin
simp only [bernstein_apply],
exact mul_nonneg
(mul_nonneg (nat.cast_nonneg _) (pow_nonneg (by unit_interval) _))
(pow_nonneg (by unit_interval) _),
end
/-!
We now give a slight reformulation of `bernstein_polynomial.variance`.
-/
namespace bernstein
/--
Send `k : fin (n+1)` to the equally spaced points `k/n` in the unit interval.
-/
def z {n : ℕ} (k : fin (n+1)) : I :=
⟨(k : ℝ) / n,
begin
cases n,
{ norm_num },
{ have h₁ : 0 < (n.succ : ℝ) := by exact_mod_cast (nat.succ_pos _),
have h₂ : ↑k ≤ n.succ := by exact_mod_cast (fin.le_last k),
rw [set.mem_Icc, le_div_iff h₁, div_le_iff h₁],
norm_cast,
simp [h₂], },
end⟩
local postfix `/ₙ`:90 := z
lemma probability (n : ℕ) (x : I) :
∑ k : fin (n+1), bernstein n k x = 1 :=
begin
have := bernstein_polynomial.sum ℝ n,
apply_fun (λ p, polynomial.aeval (x : ℝ) p) at this,
simp [alg_hom.map_sum, finset.sum_range] at this,
exact this,
end
lemma variance {n : ℕ} (h : 0 < (n : ℝ)) (x : I) :
∑ k : fin (n+1), (x - k/ₙ : ℝ)^2 * bernstein n k x = x * (1-x) / n :=
begin
have h' : (n : ℝ) ≠ 0 := ne_of_gt h,
apply_fun (λ x : ℝ, x * n) using group_with_zero.mul_right_injective h',
apply_fun (λ x : ℝ, x * n) using group_with_zero.mul_right_injective h',
dsimp,
conv_lhs { simp only [finset.sum_mul, z], },
conv_rhs { rw div_mul_cancel _ h', },
have := bernstein_polynomial.variance ℝ n,
apply_fun (λ p, polynomial.aeval (x : ℝ) p) at this,
simp [alg_hom.map_sum, finset.sum_range, ←polynomial.nat_cast_mul] at this,
convert this using 1,
{ congr' 1, funext k,
rw [mul_comm _ (n : ℝ), mul_comm _ (n : ℝ), ←mul_assoc, ←mul_assoc],
congr' 1,
field_simp [h],
ring, },
{ ring, },
end
end bernstein
open bernstein
local postfix `/ₙ`:2000 := z
local notation `|`x`|` := abs x
/--
The `n`-th approximation of a continuous function on `[0,1]` by Bernstein polynomials,
given by `∑ k, f (k/n) * bernstein n k x`.
-/
def bernstein_approximation (n : ℕ) (f : C(I, ℝ)) : C(I, ℝ) :=
∑ k : fin (n+1), f k/ₙ • bernstein n k
/-!
We now set up some of the basic machinery of the proof that the Bernstein approximations
converge uniformly.
A key player is the set `S f ε h n x`,
for some function `f : C(I, ℝ)`, `h : 0 < ε`, `n : ℕ` and `x : I`.
This is the set of points `k` in `fin (n+1)` such that
`k/n` is within `δ` of `x`, where `δ` is the modulus of uniform continuity for `f`,
chosen so `|f x - f y| < ε/2` when `|x - y| < δ`.
We show that if `k ∉ S`, then `1 ≤ δ^-2 * (x - k/n)^2`.
-/
namespace bernstein_approximation
@[simp] lemma apply (n : ℕ) (f : C(I, ℝ)) (x : I) :
bernstein_approximation n f x = ∑ k : fin (n+1), f k/ₙ * bernstein n k x :=
by simp [bernstein_approximation]
/--
The modulus of (uniform) continuity for `f`, chosen so `|f x - f y| < ε/2` when `|x - y| < δ`.
-/
def δ (f : C(I, ℝ)) (ε : ℝ) (h : 0 < ε) : ℝ := f.modulus (ε/2) (half_pos h)
/--
The set of points `k` so `k/n` is within `δ` of `x`.
-/
def S (f : C(I, ℝ)) (ε : ℝ) (h : 0 < ε) (n : ℕ) (x : I) : finset (fin (n+1)) :=
{ k : fin (n+1) | dist k/ₙ x < δ f ε h }.to_finset
/--
If `k ∈ S`, then `f(k/n)` is close to `f x`.
-/
lemma lt_of_mem_S
{f : C(I, ℝ)} {ε : ℝ} {h : 0 < ε} {n : ℕ} {x : I} {k : fin (n+1)} (m : k ∈ S f ε h n x) :
|f k/ₙ - f x| < ε/2 :=
begin
apply f.dist_lt_of_dist_lt_modulus (ε/2) (half_pos h),
simpa [S] using m,
end
/--
If `k ∉ S`, then as `δ ≤ |x - k/n|`, we have the inequality `1 ≤ δ^-2 * (x - k/n)^2`.
This particular formulation will be helpful later.
-/
lemma le_of_mem_S_compl
{f : C(I, ℝ)} {ε : ℝ} {h : 0 < ε} {n : ℕ} {x : I} {k : fin (n+1)} (m : k ∈ (S f ε h n x)ᶜ) :
(1 : ℝ) ≤ (δ f ε h)^(-2 : ℤ) * (x - k/ₙ) ^ 2 :=
begin
simp only [finset.mem_compl, not_lt, set.mem_to_finset, set.mem_set_of_eq, S] at m,
field_simp,
erw [le_div_iff (pow_pos f.modulus_pos 2), one_mul],
apply sq_le_sq,
rw abs_eq_self.mpr (le_of_lt f.modulus_pos),
rw [dist_comm] at m,
exact m,
end
end bernstein_approximation
open bernstein_approximation
open bounded_continuous_function
open filter
open_locale topological_space
/--
The Bernstein approximations
```
∑ k : fin (n+1), f (k/n : ℝ) * n.choose k * x^k * (1-x)^(n-k)
```
for a continuous function `f : C([0,1], ℝ)` converge uniformly to `f` as `n` tends to infinity.
This is the proof given in [Richard Beals' *Analysis, an introduction*][beals-analysis], §7D,
and reproduced on wikipedia.
-/
theorem bernstein_approximation_uniform (f : C(I, ℝ)) :
tendsto (λ n : ℕ, bernstein_approximation n f) at_top (𝓝 f) :=
begin
simp only [metric.nhds_basis_ball.tendsto_right_iff, metric.mem_ball, dist_eq_norm],
intros ε h,
let δ := δ f ε h,
have nhds_zero := tendsto_const_div_at_top_nhds_0_nat (2 * ∥f∥ * δ ^ (-2 : ℤ)),
filter_upwards [nhds_zero.eventually (gt_mem_nhds (half_pos h)), eventually_gt_at_top 0],
intros n nh npos',
have npos : 0 < (n:ℝ) := by exact_mod_cast npos',
-- Two easy inequalities we'll need later:
have w₁ : 0 ≤ 2 * ∥f∥ := mul_nonneg (by norm_num) (norm_nonneg f),
have w₂ : 0 ≤ 2 * ∥f∥ * δ^(-2 : ℤ) := mul_nonneg w₁ pow_minus_two_nonneg,
-- As `[0,1]` is compact, it suffices to check the inequality pointwise.
rw (continuous_map.norm_lt_iff _ h),
intro x,
-- The idea is to split up the sum over `k` into two sets,
-- `S`, where `x - k/n < δ`, and its complement.
let S := S f ε h n x,
calc
|(bernstein_approximation n f - f) x|
= |bernstein_approximation n f x - f x|
: rfl
... = |bernstein_approximation n f x - f x * 1|
: by rw mul_one
... = |bernstein_approximation n f x - f x * (∑ k : fin (n+1), bernstein n k x)|
: by rw bernstein.probability
... = |∑ k : fin (n+1), (f k/ₙ - f x) * bernstein n k x|
: by simp [bernstein_approximation, finset.mul_sum, sub_mul]
... ≤ ∑ k : fin (n+1), |(f k/ₙ - f x) * bernstein n k x|
: finset.abs_sum_le_sum_abs _ _
... = ∑ k : fin (n+1), |f k/ₙ - f x| * bernstein n k x
: by simp_rw [abs_mul, abs_eq_self.mpr bernstein_nonneg]
... = ∑ k in S, |f k/ₙ - f x| * bernstein n k x +
∑ k in Sᶜ, |f k/ₙ - f x| * bernstein n k x
: (S.sum_add_sum_compl _).symm
-- We'll now deal with the terms in `S` and the terms in `Sᶜ` in separate calc blocks.
... < ε/2 + ε/2 : add_lt_add_of_le_of_lt _ _
... = ε : add_halves ε,
{ -- We now work on the terms in `S`: uniform continuity and `bernstein.probability`
-- quickly give us a bound.
calc ∑ k in S, |f k/ₙ - f x| * bernstein n k x
≤ ∑ k in S, ε/2 * bernstein n k x
: finset.sum_le_sum
(λ k m, (mul_le_mul_of_nonneg_right (le_of_lt (lt_of_mem_S m))
bernstein_nonneg))
... = ε/2 * ∑ k in S, bernstein n k x
: by rw finset.mul_sum
-- In this step we increase the sum over `S` back to a sum over all of `fin (n+1)`,
-- so that we can use `bernstein.probability`.
... ≤ ε/2 * ∑ k : fin (n+1), bernstein n k x
: mul_le_mul_of_nonneg_left
(finset.sum_le_univ_sum_of_nonneg (λ k, bernstein_nonneg))
(le_of_lt (half_pos h))
... = ε/2 : by rw [bernstein.probability, mul_one] },
{ -- We now turn to working on `Sᶜ`: we control the difference term just using `∥f∥`,
-- and then insert a `δ^(-2) * (x - k/n)^2` factor
-- (which is at least one because we are not in `S`).
calc ∑ k in Sᶜ, |f k/ₙ - f x| * bernstein n k x
≤ ∑ k in Sᶜ, (2 * ∥f∥) * bernstein n k x
: finset.sum_le_sum
(λ k m, mul_le_mul_of_nonneg_right (f.dist_le_two_norm _ _)
bernstein_nonneg)
... = (2 * ∥f∥) * ∑ k in Sᶜ, bernstein n k x
: by rw finset.mul_sum
... ≤ (2 * ∥f∥) * ∑ k in Sᶜ, δ^(-2 : ℤ) * (x - k/ₙ)^2 * bernstein n k x
: mul_le_mul_of_nonneg_left
(finset.sum_le_sum (λ k m, begin
conv_lhs { rw ←one_mul (bernstein _ _ _), },
exact mul_le_mul_of_nonneg_right
(le_of_mem_S_compl m) bernstein_nonneg,
end)) w₁
-- Again enlarging the sum from `Sᶜ` to all of `fin (n+1)`
... ≤ (2 * ∥f∥) * ∑ k : fin (n+1), δ^(-2 : ℤ) * (x - k/ₙ)^2 * bernstein n k x
: mul_le_mul_of_nonneg_left
(finset.sum_le_univ_sum_of_nonneg
(λ k, mul_nonneg
(mul_nonneg pow_minus_two_nonneg (sq_nonneg _))
bernstein_nonneg)) w₁
... = (2 * ∥f∥) * δ^(-2 : ℤ) * ∑ k : fin (n+1), (x - k/ₙ)^2 * bernstein n k x
: by conv_rhs {
rw [mul_assoc, finset.mul_sum], simp only [←mul_assoc], }
-- `bernstein.variance` and `x ∈ [0,1]` gives the uniform bound
... = (2 * ∥f∥) * δ^(-2 : ℤ) * x * (1-x) / n
: by { rw variance npos, ring, }
... ≤ (2 * ∥f∥) * δ^(-2 : ℤ) / n
: (div_le_div_right npos).mpr
begin
apply mul_nonneg_le_one_le w₂,
apply mul_nonneg_le_one_le w₂ (le_refl _),
all_goals { unit_interval, },
end
... < ε/2 : nh, }
end
|
9cb33a7a69fd002b7879c25c6fe1c203cf2f5483 | e0f9ba56b7fedc16ef8697f6caeef5898b435143 | /src/computability/reduce.lean | 8e4c8cc9950375abd003f242cccb3c21f703261a | [
"Apache-2.0"
] | permissive | anrddh/mathlib | 6a374da53c7e3a35cb0298b0cd67824efef362b4 | a4266a01d2dcb10de19369307c986d038c7bb6a6 | refs/heads/master | 1,656,710,827,909 | 1,589,560,456,000 | 1,589,560,456,000 | 264,271,800 | 0 | 0 | Apache-2.0 | 1,589,568,062,000 | 1,589,568,061,000 | null | UTF-8 | Lean | false | false | 17,528 | lean | /-
Copyright (c) 2019 Minchao Wu. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Minchao Wu, Mario Carneiro
-/
import computability.halting
/-!
# Strong reducibility and degrees.
This file defines the notions of many-one reduction and one-one reduction between sets, and
shows that the corresponding degrees form a semilattice.
## Notations
This file uses the local notation `⊕'` for `sum.elim` to denote the disjoint union of two degrees,
and `deg` for the `many_one_degree.of` a set.
## References
* [Robert Soare, *Recursively enumerable sets and degrees*][soare1987]
## Tags
computability, reducibility, reduction
-/
open function
/--
`p` is many-one reducible to `q` if there is a computable function translating questions about `p`
to questions about `q`.
-/
def many_one_reducible {α β} [primcodable α] [primcodable β] (p : α → Prop) (q : β → Prop) :=
∃ f, computable f ∧ ∀ a, p a ↔ q (f a)
infix ` ≤₀ `:1000 := many_one_reducible
theorem many_one_reducible.mk {α β} [primcodable α] [primcodable β] {f : α → β} (q : β → Prop)
(h : computable f) : (λ a, q (f a)) ≤₀ q := ⟨f, h, λ a, iff.rfl⟩
@[refl]
theorem many_one_reducible_refl {α} [primcodable α] (p : α → Prop) :
p ≤₀ p := ⟨id, computable.id, by simp⟩
@[trans]
theorem many_one_reducible.trans {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop} : p ≤₀ q → q ≤₀ r → p ≤₀ r
| ⟨f, c₁, h₁⟩ ⟨g, c₂, h₂⟩ := ⟨g ∘ f, c₂.comp c₁,
λ a, ⟨λ h, by rwa [←h₂, ←h₁], λ h, by rwa [h₁, h₂]⟩⟩
theorem reflexive_many_one_reducible {α} [primcodable α] : reflexive (@many_one_reducible α α _ _) :=
many_one_reducible_refl
theorem transitive_many_one_reducible {α} [primcodable α] : transitive (@many_one_reducible α α _ _) :=
λ p q r, many_one_reducible.trans
/--
`p` is one-one reducible to `q` if there is an injective computable function translating questions
about `p` to questions about `q`.
-/
def one_one_reducible {α β} [primcodable α] [primcodable β] (p : α → Prop) (q : β → Prop) :=
∃ f, computable f ∧ injective f ∧ ∀ a, p a ↔ q (f a)
infix ` ≤₁ `:1000 := one_one_reducible
theorem one_one_reducible.mk {α β} [primcodable α] [primcodable β] {f : α → β} (q : β → Prop)
(h : computable f) (i : injective f) : (λ a, q (f a)) ≤₁ q := ⟨f, h, i, λ a, iff.rfl⟩
@[refl]
theorem one_one_reducible_refl {α} [primcodable α] (p : α → Prop) :
p ≤₁ p := ⟨id, computable.id, injective_id, by simp⟩
@[trans]
theorem one_one_reducible.trans {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop} : p ≤₁ q → q ≤₁ r → p ≤₁ r
| ⟨f, c₁, i₁, h₁⟩ ⟨g, c₂, i₂, h₂⟩ := ⟨g ∘ f, c₂.comp c₁, injective_comp i₂ i₁,
λ a, ⟨λ h, by rwa [←h₂, ←h₁], λ h, by rwa [h₁, h₂]⟩⟩
theorem one_one_reducible.to_many_one {α β} [primcodable α] [primcodable β]
{p : α → Prop} {q : β → Prop} : p ≤₁ q → p ≤₀ q
| ⟨f, c, i, h⟩ := ⟨f, c, h⟩
theorem one_one_reducible.of_equiv {α β} [primcodable α] [primcodable β] {e : α ≃ β} (q : β → Prop)
(h : computable e) : (q ∘ e) ≤₁ q := one_one_reducible.mk _ h e.injective
theorem one_one_reducible.of_equiv_symm {α β} [primcodable α] [primcodable β] {e : α ≃ β} (q : β → Prop)
(h : computable e.symm) : q ≤₁ (q ∘ e) :=
by convert one_one_reducible.of_equiv _ h; funext; simp
theorem reflexive_one_one_reducible {α} [primcodable α] : reflexive (@one_one_reducible α α _ _) :=
one_one_reducible_refl
theorem transitive_one_one_reducible {α} [primcodable α] : transitive (@one_one_reducible α α _ _) :=
λ p q r, one_one_reducible.trans
namespace computable_pred
variables {α : Type*} {β : Type*} {σ : Type*}
variables [primcodable α] [primcodable β] [primcodable σ]
open computable
theorem computable_of_many_one_reducible
{p : α → Prop} {q : β → Prop}
(h₁ : p ≤₀ q) (h₂ : computable_pred q) : computable_pred p :=
begin
rcases h₁ with ⟨f, c, hf⟩,
rw [show p = λ a, q (f a), from set.ext hf],
rcases computable_iff.1 h₂ with ⟨g, hg, rfl⟩,
exact ⟨by apply_instance, by simpa using hg.comp c⟩
end
theorem computable_of_one_one_reducible
{p : α → Prop} {q : β → Prop}
(h : p ≤₁ q) : computable_pred q → computable_pred p :=
computable_of_many_one_reducible h.to_many_one
end computable_pred
/-- `p` and `q` are many-one equivalent if each one is many-one reducible to the other. -/
def many_one_equiv {α β} [primcodable α] [primcodable β]
(p : α → Prop) (q : β → Prop) := p ≤₀ q ∧ q ≤₀ p
/-- `p` and `q` are one-one equivalent if each one is one-one reducible to the other. -/
def one_one_equiv {α β} [primcodable α] [primcodable β]
(p : α → Prop) (q : β → Prop) := p ≤₁ q ∧ q ≤₁ p
theorem many_one_equiv_refl {α} [primcodable α] (p : α → Prop) : many_one_equiv p p :=
⟨many_one_reducible_refl _, many_one_reducible_refl _⟩
theorem many_one_equiv.symm {α β} [primcodable α] [primcodable β]
{p : α → Prop} {q : β → Prop} : many_one_equiv p q → many_one_equiv q p := and.swap
@[trans]
theorem many_one_equiv.trans {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop} :
many_one_equiv p q → many_one_equiv q r → many_one_equiv p r
| ⟨pq, qp⟩ ⟨qr, rq⟩ := ⟨pq.trans qr, rq.trans qp⟩
theorem equivalence_of_many_one_equiv {α} [primcodable α] : equivalence (@many_one_equiv α α _ _) :=
⟨many_one_equiv_refl, λ x y, many_one_equiv.symm, λ x y z, many_one_equiv.trans⟩
@[refl]
theorem one_one_equiv_refl {α} [primcodable α] (p : α → Prop) : one_one_equiv p p :=
⟨one_one_reducible_refl _, one_one_reducible_refl _⟩
@[symm]
theorem one_one_equiv.symm {α β} [primcodable α] [primcodable β]
{p : α → Prop} {q : β → Prop} : one_one_equiv p q → one_one_equiv q p := and.swap
@[trans]
theorem one_one_equiv.trans {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop} :
one_one_equiv p q → one_one_equiv q r → one_one_equiv p r
| ⟨pq, qp⟩ ⟨qr, rq⟩ := ⟨pq.trans qr, rq.trans qp⟩
theorem equivalence_of_one_one_equiv {α} [primcodable α] : equivalence (@one_one_equiv α α _ _) :=
⟨one_one_equiv_refl, λ x y, one_one_equiv.symm, λ x y z, one_one_equiv.trans⟩
theorem one_one_equiv.to_many_one {α β} [primcodable α] [primcodable β]
{p : α → Prop} {q : β → Prop} : one_one_equiv p q → many_one_equiv p q
| ⟨pq, qp⟩ := ⟨pq.to_many_one, qp.to_many_one⟩
/-- sets up to many-one equivalence -/
def many_one_equiv_setoid {α} [primcodable α] : setoid (set α) :=
⟨many_one_equiv, @equivalence_of_many_one_equiv α _⟩
/-- a computable bijection -/
def equiv.computable {α β} [primcodable α] [primcodable β] (e : α ≃ β) := computable e ∧ computable e.symm
theorem equiv.computable.symm {α β} [primcodable α] [primcodable β] {e : α ≃ β} :
e.computable → e.symm.computable := and.swap
theorem equiv.computable.trans {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{e₁ : α ≃ β} {e₂ : β ≃ γ} :
e₁.computable → e₂.computable → (e₁.trans e₂).computable
| ⟨l₁, r₁⟩ ⟨l₂, r₂⟩ := ⟨l₂.comp l₁, r₁.comp r₂⟩
theorem computable.eqv (α) [denumerable α] : (denumerable.eqv α).computable :=
⟨computable.encode, computable.of_nat _⟩
theorem computable.equiv₂ (α β) [denumerable α] [denumerable β] :
(denumerable.equiv₂ α β).computable :=
(computable.eqv _).trans (computable.eqv _).symm
theorem one_one_equiv.of_equiv {α β} [primcodable α] [primcodable β]
{e : α ≃ β} (h : e.computable) {p} : one_one_equiv (p ∘ e) p :=
⟨one_one_reducible.of_equiv _ h.1, one_one_reducible.of_equiv_symm _ h.2⟩
theorem many_one_equiv.of_equiv {α β} [primcodable α] [primcodable β]
{e : α ≃ β} (h : e.computable) {p} : many_one_equiv (p ∘ e) p :=
(one_one_equiv.of_equiv h).to_many_one
theorem many_one_equiv.le_congr_left {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop}
(h : many_one_equiv p q) : p ≤₀ r ↔ q ≤₀ r := ⟨h.2.trans, h.1.trans⟩
theorem many_one_equiv.le_congr_right {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop}
(h : many_one_equiv q r) : p ≤₀ q ↔ p ≤₀ r := ⟨λ h', h'.trans h.1, λ h', h'.trans h.2⟩
theorem one_one_equiv.le_congr_left {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop}
(h : one_one_equiv p q) : p ≤₁ r ↔ q ≤₁ r := ⟨h.2.trans, h.1.trans⟩
theorem one_one_equiv.le_congr_right {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop}
(h : one_one_equiv q r) : p ≤₁ q ↔ p ≤₁ r := ⟨λ h', h'.trans h.1, λ h', h'.trans h.2⟩
theorem many_one_equiv.congr_left {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop}
(h : many_one_equiv p q) : many_one_equiv p r ↔ many_one_equiv q r :=
and_congr h.le_congr_left h.le_congr_right
theorem many_one_equiv.congr_right {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop}
(h : many_one_equiv q r) : many_one_equiv p q ↔ many_one_equiv p r :=
and_congr h.le_congr_right h.le_congr_left
theorem one_one_equiv.congr_left {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop}
(h : one_one_equiv p q) : one_one_equiv p r ↔ one_one_equiv q r :=
and_congr h.le_congr_left h.le_congr_right
theorem one_one_equiv.congr_right {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop}
(h : one_one_equiv q r) : one_one_equiv p q ↔ one_one_equiv p r :=
and_congr h.le_congr_right h.le_congr_left
-- local attribute [instance] many_one_equiv_setoid
/-- A many-one degree is an equivalence class of sets up to many-one equivalence. -/
def many_one_degree (α) [primcodable α] := quotient (@many_one_equiv_setoid α _)
instance {α} [primcodable α] : inhabited (many_one_degree α) :=
⟨@quotient.mk _(@many_one_equiv_setoid α _) ∅⟩
local infix ` ⊕' `:1001 := sum.elim
open nat.primrec
theorem one_one_reducible.disjoin_left {α β} [primcodable α] [primcodable β]
{p : α → Prop} {q : β → Prop} : p ≤₁ p ⊕' q :=
⟨sum.inl, computable.sum_inl, λ x y, sum.inl.inj_iff.1, λ a, iff.rfl⟩
theorem one_one_reducible.disjoin_right {α β} [primcodable α] [primcodable β]
{p : α → Prop} {q : β → Prop} : q ≤₁ p ⊕' q :=
⟨sum.inr, computable.sum_inr, λ x y, sum.inr.inj_iff.1, λ a, iff.rfl⟩
theorem disjoin_many_one_reducible {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop} : p ≤₀ r → q ≤₀ r → p ⊕' q ≤₀ r
| ⟨f, c₁, h₁⟩ ⟨g, c₂, h₂⟩ := ⟨sum.elim f g,
computable.id.sum_cases (c₁.comp computable.snd).to₂ (c₂.comp computable.snd).to₂,
λ x, by cases x; [apply h₁, apply h₂]⟩
theorem disjoin_le {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{p : α → Prop} {q : β → Prop} {r : γ → Prop} : p ⊕' q ≤₀ r ↔ p ≤₀ r ∧ q ≤₀ r :=
⟨λ h, ⟨one_one_reducible.disjoin_left.to_many_one.trans h,
one_one_reducible.disjoin_right.to_many_one.trans h⟩,
λ ⟨h₁, h₂⟩, disjoin_many_one_reducible h₁ h₂⟩
/--
For many-one degrees `d₁` and `d₂`, `d₁ ≤ d₂` if the sets in `d₁` are many-one reducible to the
sets in `d₂`.
-/
def many_one_degree.le {α β} [primcodable α] [primcodable β]
(d₁ : many_one_degree α) (d₂ : many_one_degree β) : Prop :=
quotient.lift_on₂' d₁ d₂ (λ a b, a ≤₀ b)
(λ a b c d h₁ h₂, propext $ (h₁.le_congr_left).trans (h₂.le_congr_right))
instance many_one_degree.has_le {α} [primcodable α] : has_le (many_one_degree α) := ⟨many_one_degree.le⟩
/-- the many-one degree of a set or predicate -/
def many_one_degree.of {α} [primcodable α] : (α → Prop) → many_one_degree α := quotient.mk'
local notation `deg` := many_one_degree.of
@[simp] theorem many_one_degree.of_le_of {α β} [primcodable α] [primcodable β]
(p : α → Prop) (q : β → Prop) : (deg p).le (deg q) ↔ p ≤₀ q := iff.rfl
@[simp] theorem many_one_degree.of_le_of' {α} [primcodable α] (p q : α → Prop) :
deg p ≤ deg q ↔ p ≤₀ q := iff.rfl
theorem many_one_degree.le_refl {α} [primcodable α] (d : many_one_degree α) : d.le d :=
quotient.induction_on' d many_one_reducible_refl
theorem many_one_degree.le_antisymm {α} [primcodable α] {d₁ d₂ : many_one_degree α} :
d₁ ≤ d₂ → d₂ ≤ d₁ → d₁ = d₂ :=
quotient.induction_on₂' d₁ d₂ $ λ p q h₁ h₂, quotient.sound' ⟨h₁, h₂⟩
theorem many_one_degree.le_trans {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{d₁ : many_one_degree α} {d₂ : many_one_degree β} {d₃ : many_one_degree γ} :
d₁.le d₂ → d₂.le d₃ → d₁.le d₃ :=
quotient.induction_on₃' d₁ d₂ d₃ $ λ a b c, many_one_reducible.trans
/--
Given a computable bijection `e` from `α` to `β`, the inverse image from `set β` to `set α` lifts
to a map on many-one degrees.
-/
def many_one_degree.comap {α β} [primcodable α] [primcodable β] (e : α ≃ β) (he : e.computable)
(d : many_one_degree β) : many_one_degree α :=
quotient.lift_on' d (λ p, deg (p ∘ e)) $
λ a b h, quotient.sound' $
((many_one_equiv.of_equiv he).congr_left).2 $ ((many_one_equiv.of_equiv he).congr_right).2 h
theorem many_one_degree.le_comap_left {α β γ} [primcodable α] [primcodable β] [primcodable γ]
(e : α ≃ β) (he : e.computable) {d₁ : many_one_degree β} {d₂ : many_one_degree γ} :
(d₁.comap e he).le d₂ ↔ d₁.le d₂ :=
quotient.induction_on₂' d₁ d₂ $ λ p q, (many_one_equiv.of_equiv he).le_congr_left
theorem many_one_degree.le_comap_right {α β γ} [primcodable α] [primcodable β] [primcodable γ]
(e : β ≃ γ) (he : e.computable) {d₁ : many_one_degree α} {d₂ : many_one_degree γ} :
d₁.le (d₂.comap e he) ↔ d₁.le d₂ :=
quotient.induction_on₂' d₁ d₂ $ λ p q, (many_one_equiv.of_equiv he).le_congr_right
/-- the join of two degrees, induced by the disjoint union of two underlying sets -/
def many_one_degree.add {α β} [primcodable α] [primcodable β]
(d₁ : many_one_degree α) (d₂ : many_one_degree β) : many_one_degree (α ⊕ β) :=
quotient.lift_on₂' d₁ d₂ (λ a b, deg (a ⊕' b)) $
λ a b c d ⟨hl₁, hr₁⟩ ⟨hl₂, hr₂⟩, quotient.sound' $
⟨disjoin_many_one_reducible
(hl₁.trans one_one_reducible.disjoin_left.to_many_one)
(hl₂.trans one_one_reducible.disjoin_right.to_many_one),
disjoin_many_one_reducible
(hr₁.trans one_one_reducible.disjoin_left.to_many_one)
(hr₂.trans one_one_reducible.disjoin_right.to_many_one)⟩
instance degree_add {α} [denumerable α] : has_add (many_one_degree α) :=
⟨λ d₁ d₂, (d₁.add d₂).comap _ (computable.equiv₂ _ _)⟩
theorem many_one_degree.add_le {α β γ} [primcodable α] [primcodable β] [primcodable γ]
{d₁ : many_one_degree α} {d₂ : many_one_degree β} {d₃ : many_one_degree γ} :
(d₁.add d₂).le d₃ ↔ d₁.le d₃ ∧ d₂.le d₃ :=
quotient.induction_on₃' d₁ d₂ d₃ $ λ p q r, disjoin_le
theorem many_one_degree.le_add_left {α β} [primcodable α] [primcodable β]
(d₁ : many_one_degree α) (d₂ : many_one_degree β) : d₁.le (d₁.add d₂) :=
(many_one_degree.add_le.1 (many_one_degree.le_refl _)).1
theorem many_one_degree.le_add_right {α β} [primcodable α] [primcodable β]
(d₁ : many_one_degree α) (d₂ : many_one_degree β) : d₂.le (d₁.add d₂) :=
(many_one_degree.add_le.1 (many_one_degree.le_refl _)).2
theorem many_one_degree.add_le' {α β} [denumerable α] [primcodable β]
{d₁ d₂ : many_one_degree α} {d₃ : many_one_degree β} :
(d₁ + d₂).le d₃ ↔ d₁.le d₃ ∧ d₂.le d₃ :=
(many_one_degree.le_comap_left _ _).trans many_one_degree.add_le
theorem many_one_degree.le_add_left' {α} [denumerable α]
(d₁ d₂ : many_one_degree α) : d₁ ≤ d₁ + d₂ :=
(many_one_degree.add_le'.1 (many_one_degree.le_refl _)).1
theorem many_one_degree.le_add_right' {α} [denumerable α]
(d₁ d₂ : many_one_degree α) : d₂ ≤ d₁ + d₂ :=
(many_one_degree.add_le'.1 (many_one_degree.le_refl _)).2
instance many_one_degree.semilattice_sup {α} [denumerable α] :
semilattice_sup (many_one_degree α) :=
{ le := has_le.le,
sup := has_add.add,
le_refl := many_one_degree.le_refl,
le_antisymm := λ a b, many_one_degree.le_antisymm,
le_trans := λ a b c, many_one_degree.le_trans,
le_sup_left := many_one_degree.le_add_left',
le_sup_right := many_one_degree.le_add_right',
sup_le := λ a b c h₁ h₂, many_one_degree.add_le'.2 ⟨h₁, h₂⟩ }
|
3459d1bed7e96ad2185211b702f7bd142a13826e | 367134ba5a65885e863bdc4507601606690974c1 | /src/geometry/manifold/diffeomorph.lean | af60c088eeb7c27fd00a424a41aa39f5f295a5bc | [
"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,942 | lean | /-
Copyright © 2020 Nicolò Cavalleri. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Nicolò Cavalleri.
-/
import geometry.manifold.times_cont_mdiff_map
/-!
# Diffeomorphisms
This file implements diffeomorphisms.
## Definitions
* `times_diffeomorph I I' M M' n`: `n`-times continuously differentiable diffeomorphism between
`M` and `M'` with respect to I and I'
* `diffeomorph I I' M M'` : smooth diffeomorphism between `M` and `M'` with respect to I and I'
## Notations
* `M ≃ₘ^n⟮I, I'⟯ M'` := `times_diffeomorph I J M N n`
* `M ≃ₘ⟮I, I'⟯ M'` := `times_diffeomorph I J M N ⊤`
## Implementation notes
This notion of diffeomorphism is needed although there is already a notion of structomorphism
because structomorphisms do not allow the model spaces `H` and `H'` of the two manifolds to be
different, i.e. for a structomorphism one has to impose `H = H'` which is often not the case in
practice.
-/
open_locale manifold
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{F : Type*} [normed_group F] [normed_space 𝕜 F]
{H : Type*} [topological_space H]
{H' : Type*} [topological_space H']
{G : Type*} [topological_space G]
(I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H')
(J : model_with_corners 𝕜 F G)
section diffeomorph
variables (M : Type*) [topological_space M] [charted_space H M]
(M' : Type*) [topological_space M'] [charted_space H' M']
(N : Type*) [topological_space N] [charted_space G N]
(n : with_top ℕ)
/--
`n`-times continuously differentiable diffeomorphism between `M` and `M'` with respect to I and I'
-/
@[protect_proj, nolint has_inhabited_instance]
structure times_diffeomorph extends M ≃ M' :=
(times_cont_mdiff_to_fun : times_cont_mdiff I I' n to_fun)
(times_cont_mdiff_inv_fun : times_cont_mdiff I' I n inv_fun)
/-- A `diffeomorph` is just a smooth `times_diffeomorph`. -/
@[reducible] def diffeomorph := times_diffeomorph I I' M M' ⊤
infix ` ≃ₘ `:50 := times_diffeomorph _ _
localized "notation M ` ≃ₘ^ `n `⟮` I `,` J `⟯` N := times_diffeomorph I J M N n" in manifold
localized "notation M ` ≃ₘ⟮` I `,` J `⟯` N := times_diffeomorph I J M N ⊤" in manifold
namespace times_diffeomorph
instance : has_coe_to_fun (M ≃ₘ^n⟮I, I'⟯ M') := ⟨λ _, M → M', λe, e.to_equiv⟩
instance : has_coe (M ≃ₘ^n⟮I, I'⟯ M') C^n⟮I, M; I', M'⟯ := ⟨λ Φ, ⟨Φ, Φ.times_cont_mdiff_to_fun⟩⟩
protected lemma continuous (h : M ≃ₘ^n⟮I, I'⟯ M') : continuous h :=
h.times_cont_mdiff_to_fun.continuous
protected lemma times_cont_mdiff (h : M ≃ₘ^n⟮I, I'⟯ M') : times_cont_mdiff I I' n h :=
h.times_cont_mdiff_to_fun
protected lemma smooth (h : M ≃ₘ⟮I, I'⟯ M') : smooth I I' h := h.times_cont_mdiff_to_fun
lemma coe_eq_to_equiv (h : M ≃ₘ^n⟮I, I'⟯ M') (x : M) : h x = h.to_equiv x := rfl
/-- Identity map as a diffeomorphism. -/
protected def refl : M ≃ₘ^n⟮I, I⟯ M :=
{ times_cont_mdiff_to_fun := times_cont_mdiff_id,
times_cont_mdiff_inv_fun := times_cont_mdiff_id,
..equiv.refl M }
/-- Composition of two diffeomorphisms. -/
protected def trans (h₁ : M ≃ₘ^n⟮I, I'⟯ M') (h₂ : M' ≃ₘ^n⟮I', J⟯ N) :
M ≃ₘ^n⟮I, J⟯ N :=
{ times_cont_mdiff_to_fun := h₂.times_cont_mdiff_to_fun.comp h₁.times_cont_mdiff_to_fun,
times_cont_mdiff_inv_fun := h₁.times_cont_mdiff_inv_fun.comp h₂.times_cont_mdiff_inv_fun,
.. equiv.trans h₁.to_equiv h₂.to_equiv }
/-- Inverse of a diffeomorphism. -/
protected def symm (h : M ≃ₘ^n⟮I, J⟯ N) : N ≃ₘ^n⟮J, I⟯ M :=
{ times_cont_mdiff_to_fun := h.times_cont_mdiff_inv_fun,
times_cont_mdiff_inv_fun := h.times_cont_mdiff_to_fun,
.. h.to_equiv.symm }
end times_diffeomorph
end diffeomorph
|
59efb99b8f6e7bf92f0d3d72787c9d7fd06161cb | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebraic_topology/simplicial_object.lean | 71c21b0de39fec581fa2fe79116afe4675bfbe19 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 23,917 | lean | /-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Scott Morrison, Adam Topaz
-/
import algebraic_topology.simplex_category
import category_theory.arrow
import category_theory.limits.functor_category
import category_theory.opposites
/-!
# Simplicial objects in a category.
A simplicial object in a category `C` is a `C`-valued presheaf on `simplex_category`.
(Similarly a cosimplicial object is functor `simplex_category ⥤ C`.)
Use the notation `X _[n]` in the `simplicial` locale to obtain the `n`-th term of a
(co)simplicial object `X`, where `n` is a natural number.
-/
open opposite
open category_theory
open category_theory.limits
universes v u v' u'
namespace category_theory
variables (C : Type u) [category.{v} C]
/-- The category of simplicial objects valued in a category `C`.
This is the category of contravariant functors from `simplex_category` to `C`. -/
@[derive category, nolint has_nonempty_instance]
def simplicial_object := simplex_categoryᵒᵖ ⥤ C
namespace simplicial_object
localized "notation (name := simplicial_object.at) X ` _[`:1000 n `]` :=
(X : category_theory.simplicial_object hole!).obj (opposite.op (simplex_category.mk n))"
in simplicial
instance {J : Type v} [small_category J] [has_limits_of_shape J C] :
has_limits_of_shape J (simplicial_object C) := by {dsimp [simplicial_object], apply_instance}
instance [has_limits C] : has_limits (simplicial_object C) := ⟨infer_instance⟩
instance {J : Type v} [small_category J] [has_colimits_of_shape J C] :
has_colimits_of_shape J (simplicial_object C) := by {dsimp [simplicial_object], apply_instance}
instance [has_colimits C] : has_colimits (simplicial_object C) := ⟨infer_instance⟩
variables {C} (X : simplicial_object C)
/-- Face maps for a simplicial object. -/
def δ {n} (i : fin (n+2)) : X _[n+1] ⟶ X _[n] :=
X.map (simplex_category.δ i).op
/-- Degeneracy maps for a simplicial object. -/
def σ {n} (i : fin (n+1)) : X _[n] ⟶ X _[n+1] :=
X.map (simplex_category.σ i).op
/-- Isomorphisms from identities in ℕ. -/
def eq_to_iso {n m : ℕ} (h : n = m) : X _[n] ≅ X _[m] :=
X.map_iso (eq_to_iso (by rw h))
@[simp] lemma eq_to_iso_refl {n : ℕ} (h : n = n) : X.eq_to_iso h = iso.refl _ :=
by { ext, simp [eq_to_iso], }
/-- The generic case of the first simplicial identity -/
@[reassoc]
lemma δ_comp_δ {n} {i j : fin (n+2)} (H : i ≤ j) :
X.δ j.succ ≫ X.δ i = X.δ i.cast_succ ≫ X.δ j :=
by { dsimp [δ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_δ H] }
@[reassoc]
lemma δ_comp_δ' {n} {i : fin (n+2)} {j : fin (n+3)} (H : i.cast_succ < j) :
X.δ j ≫ X.δ i = X.δ i.cast_succ ≫
X.δ (j.pred (λ hj, by simpa only [hj, fin.not_lt_zero] using H)) :=
by { dsimp [δ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_δ' H] }
@[reassoc]
lemma δ_comp_δ'' {n} {i : fin (n+3)} {j : fin (n+2)} (H : i ≤ j.cast_succ) :
X.δ j.succ ≫ X.δ (i.cast_lt (nat.lt_of_le_of_lt (fin.le_iff_coe_le_coe.mp H) j.is_lt)) =
X.δ i ≫ X.δ j :=
by { dsimp [δ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_δ'' H] }
/-- The special case of the first simplicial identity -/
@[reassoc]
lemma δ_comp_δ_self {n} {i : fin (n+2)} : X.δ i.cast_succ ≫ X.δ i = X.δ i.succ ≫ X.δ i :=
by { dsimp [δ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_δ_self] }
@[reassoc]
lemma δ_comp_δ_self' {n} {j : fin (n+3)} {i : fin (n+2)} (H : j = i.cast_succ) :
X.δ j ≫ X.δ i = X.δ i.succ ≫ X.δ i :=
by { subst H, rw δ_comp_δ_self, }
/-- The second simplicial identity -/
@[reassoc]
lemma δ_comp_σ_of_le {n} {i : fin (n+2)} {j : fin (n+1)} (H : i ≤ j.cast_succ) :
X.σ j.succ ≫ X.δ i.cast_succ = X.δ i ≫ X.σ j :=
by { dsimp [δ, σ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_σ_of_le H] }
/-- The first part of the third simplicial identity -/
@[reassoc]
lemma δ_comp_σ_self {n} {i : fin (n+1)} :
X.σ i ≫ X.δ i.cast_succ = 𝟙 _ :=
begin
dsimp [δ, σ],
simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_σ_self, op_id, X.map_id],
end
@[reassoc]
lemma δ_comp_σ_self' {n} {j : fin (n+2)} {i : fin (n+1)} (H : j = i.cast_succ):
X.σ i ≫ X.δ j = 𝟙 _ := by { subst H, rw δ_comp_σ_self, }
/-- The second part of the third simplicial identity -/
@[reassoc]
lemma δ_comp_σ_succ {n} {i : fin (n+1)} :
X.σ i ≫ X.δ i.succ = 𝟙 _ :=
begin
dsimp [δ, σ],
simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_σ_succ, op_id, X.map_id],
end
@[reassoc]
lemma δ_comp_σ_succ' {n} {j : fin (n+2)} {i : fin (n+1)} (H : j = i.succ) :
X.σ i ≫ X.δ j = 𝟙 _ := by { subst H, rw δ_comp_σ_succ, }
/-- The fourth simplicial identity -/
@[reassoc]
lemma δ_comp_σ_of_gt {n} {i : fin (n+2)} {j : fin (n+1)} (H : j.cast_succ < i) :
X.σ j.cast_succ ≫ X.δ i.succ = X.δ i ≫ X.σ j :=
by { dsimp [δ, σ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_σ_of_gt H] }
@[reassoc]
lemma δ_comp_σ_of_gt' {n} {i : fin (n+3)} {j : fin (n+2)} (H : j.succ < i) :
X.σ j ≫ X.δ i = X.δ (i.pred (λ hi, by simpa only [fin.not_lt_zero, hi] using H)) ≫
X.σ (j.cast_lt ((add_lt_add_iff_right 1).mp (lt_of_lt_of_le
(by simpa only [fin.val_eq_coe, ← fin.coe_succ]
using fin.lt_iff_coe_lt_coe.mp H) i.is_le))) :=
by { dsimp [δ, σ], simpa only [←X.map_comp, ←op_comp, simplex_category.δ_comp_σ_of_gt' H], }
/-- The fifth simplicial identity -/
@[reassoc]
lemma σ_comp_σ {n} {i j : fin (n+1)} (H : i ≤ j) :
X.σ j ≫ X.σ i.cast_succ = X.σ i ≫ X.σ j.succ :=
by { dsimp [δ, σ], simp only [←X.map_comp, ←op_comp, simplex_category.σ_comp_σ H] }
open_locale simplicial
@[simp, reassoc]
lemma δ_naturality {X' X : simplicial_object C} (f : X ⟶ X') {n : ℕ} (i : fin (n+2)) :
X.δ i ≫ f.app (op [n]) = f.app (op [n+1]) ≫ X'.δ i := f.naturality _
@[simp, reassoc]
lemma σ_naturality {X' X : simplicial_object C} (f : X ⟶ X') {n : ℕ} (i : fin (n+1)) :
X.σ i ≫ f.app (op [n+1]) = f.app (op [n]) ≫ X'.σ i := f.naturality _
variable (C)
/-- Functor composition induces a functor on simplicial objects. -/
@[simps]
def whiskering (D : Type*) [category D] :
(C ⥤ D) ⥤ simplicial_object C ⥤ simplicial_object D :=
whiskering_right _ _ _
/-- Truncated simplicial objects. -/
@[derive category, nolint has_nonempty_instance]
def truncated (n : ℕ) := (simplex_category.truncated n)ᵒᵖ ⥤ C
variable {C}
namespace truncated
instance {n} {J : Type v} [small_category J] [has_limits_of_shape J C] :
has_limits_of_shape J (simplicial_object.truncated C n) := by {dsimp [truncated], apply_instance}
instance {n} [has_limits C] : has_limits (simplicial_object.truncated C n) := ⟨infer_instance⟩
instance {n} {J : Type v} [small_category J] [has_colimits_of_shape J C] :
has_colimits_of_shape J (simplicial_object.truncated C n) :=
by {dsimp [truncated], apply_instance}
instance {n} [has_colimits C] : has_colimits (simplicial_object.truncated C n) := ⟨infer_instance⟩
variable (C)
/-- Functor composition induces a functor on truncated simplicial objects. -/
@[simps]
def whiskering {n} (D : Type*) [category D] :
(C ⥤ D) ⥤ truncated C n ⥤ truncated D n :=
whiskering_right _ _ _
variable {C}
end truncated
section skeleton
/-- The skeleton functor from simplicial objects to truncated simplicial objects. -/
def sk (n : ℕ) : simplicial_object C ⥤ simplicial_object.truncated C n :=
(whiskering_left _ _ _).obj simplex_category.truncated.inclusion.op
end skeleton
variable (C)
/-- The constant simplicial object is the constant functor. -/
abbreviation const : C ⥤ simplicial_object C := category_theory.functor.const _
/-- The category of augmented simplicial objects, defined as a comma category. -/
@[derive category, nolint has_nonempty_instance]
def augmented := comma (𝟭 (simplicial_object C)) (const C)
variable {C}
namespace augmented
/-- Drop the augmentation. -/
@[simps]
def drop : augmented C ⥤ simplicial_object C := comma.fst _ _
/-- The point of the augmentation. -/
@[simps]
def point : augmented C ⥤ C := comma.snd _ _
/-- The functor from augmented objects to arrows. -/
@[simps]
def to_arrow : augmented C ⥤ arrow C :=
{ obj := λ X,
{ left := (drop.obj X) _[0],
right := (point.obj X),
hom := X.hom.app _ },
map := λ X Y η,
{ left := (drop.map η).app _,
right := (point.map η),
w' := begin
dsimp,
rw ← nat_trans.comp_app,
erw η.w,
refl,
end } }
/-- The compatibility of a morphism with the augmentation, on 0-simplices -/
@[reassoc]
lemma w₀ {X Y : augmented C} (f : X ⟶ Y) :
(augmented.drop.map f).app (op (simplex_category.mk 0)) ≫
Y.hom.app (op (simplex_category.mk 0)) =
X.hom.app (op (simplex_category.mk 0)) ≫ augmented.point.map f :=
by convert congr_app f.w (op (simplex_category.mk 0))
variable (C)
/-- Functor composition induces a functor on augmented simplicial objects. -/
@[simp]
def whiskering_obj (D : Type*) [category D] (F : C ⥤ D) :
augmented C ⥤ augmented D :=
{ obj := λ X,
{ left := ((whiskering _ _).obj F).obj (drop.obj X),
right := F.obj (point.obj X),
hom := whisker_right X.hom F ≫ (functor.const_comp _ _ _).hom },
map := λ X Y η,
{ left := whisker_right η.left _,
right := F.map η.right,
w' := begin
ext,
dsimp,
rw [category.comp_id, category.comp_id, ← F.map_comp, ← F.map_comp, ← nat_trans.comp_app],
erw η.w,
refl,
end } }
/-- Functor composition induces a functor on augmented simplicial objects. -/
@[simps]
def whiskering (D : Type u') [category.{v'} D] :
(C ⥤ D) ⥤ augmented C ⥤ augmented D :=
{ obj := whiskering_obj _ _,
map := λ X Y η,
{ app := λ A,
{ left := whisker_left _ η,
right := η.app _,
w' := begin
ext n,
dsimp,
rw [category.comp_id, category.comp_id, η.naturality],
end }, }, }
variable {C}
end augmented
/-- Augment a simplicial object with an object. -/
@[simps]
def augment (X : simplicial_object C) (X₀ : C) (f : X _[0] ⟶ X₀)
(w : ∀ (i : simplex_category) (g₁ g₂ : [0] ⟶ i),
X.map g₁.op ≫ f = X.map g₂.op ≫ f) : simplicial_object.augmented C :=
{ left := X,
right := X₀,
hom :=
{ app := λ i, X.map (simplex_category.const i.unop 0).op ≫ f,
naturality' := begin
intros i j g,
dsimp,
rw ← g.op_unop,
simpa only [← X.map_comp, ← category.assoc, category.comp_id, ← op_comp] using w _ _ _,
end } }
@[simp]
lemma augment_hom_zero (X : simplicial_object C) (X₀ : C) (f : X _[0] ⟶ X₀) (w) :
(X.augment X₀ f w).hom.app (op [0]) = f :=
by { dsimp, rw [simplex_category.hom_zero_zero ([0].const 0), op_id, X.map_id, category.id_comp] }
end simplicial_object
/-- Cosimplicial objects. -/
@[derive category, nolint has_nonempty_instance]
def cosimplicial_object := simplex_category ⥤ C
namespace cosimplicial_object
localized "notation (name := cosimplicial_object.at) X ` _[`:1000 n `]` :=
(X : category_theory.cosimplicial_object hole!).obj (simplex_category.mk n)" in simplicial
instance {J : Type v} [small_category J] [has_limits_of_shape J C] :
has_limits_of_shape J (cosimplicial_object C) := by {dsimp [cosimplicial_object], apply_instance}
instance [has_limits C] : has_limits (cosimplicial_object C) := ⟨infer_instance⟩
instance {J : Type v} [small_category J] [has_colimits_of_shape J C] :
has_colimits_of_shape J (cosimplicial_object C) :=
by {dsimp [cosimplicial_object], apply_instance}
instance [has_colimits C] : has_colimits (cosimplicial_object C) := ⟨infer_instance⟩
variables {C} (X : cosimplicial_object C)
/-- Coface maps for a cosimplicial object. -/
def δ {n} (i : fin (n+2)) : X _[n] ⟶ X _[n+1] :=
X.map (simplex_category.δ i)
/-- Codegeneracy maps for a cosimplicial object. -/
def σ {n} (i : fin (n+1)) : X _[n+1] ⟶ X _[n] :=
X.map (simplex_category.σ i)
/-- Isomorphisms from identities in ℕ. -/
def eq_to_iso {n m : ℕ} (h : n = m) : X _[n] ≅ X _[m] :=
X.map_iso (eq_to_iso (by rw h))
@[simp] lemma eq_to_iso_refl {n : ℕ} (h : n = n) : X.eq_to_iso h = iso.refl _ :=
by { ext, simp [eq_to_iso], }
/-- The generic case of the first cosimplicial identity -/
@[reassoc]
lemma δ_comp_δ {n} {i j : fin (n+2)} (H : i ≤ j) :
X.δ i ≫ X.δ j.succ = X.δ j ≫ X.δ i.cast_succ :=
by { dsimp [δ], simp only [←X.map_comp, simplex_category.δ_comp_δ H], }
@[reassoc]
lemma δ_comp_δ' {n} {i : fin (n+2)} {j : fin (n+3)} (H : i.cast_succ < j) :
X.δ i ≫ X.δ j = X.δ (j.pred (λ hj, by simpa only [hj, fin.not_lt_zero] using H)) ≫
X.δ i.cast_succ :=
by { dsimp [δ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_δ' H] }
@[reassoc]
lemma δ_comp_δ'' {n} {i : fin (n+3)} {j : fin (n+2)} (H : i ≤ j.cast_succ) :
X.δ (i.cast_lt (nat.lt_of_le_of_lt (fin.le_iff_coe_le_coe.mp H) j.is_lt)) ≫ X.δ j.succ =
X.δ j ≫ X.δ i :=
by { dsimp [δ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_δ'' H] }
/-- The special case of the first cosimplicial identity -/
@[reassoc]
lemma δ_comp_δ_self {n} {i : fin (n+2)} : X.δ i ≫ X.δ i.cast_succ = X.δ i ≫ X.δ i.succ :=
by { dsimp [δ], simp only [←X.map_comp, simplex_category.δ_comp_δ_self] }
@[reassoc]
lemma δ_comp_δ_self' {n} {i : fin (n+2)} {j : fin (n+3)} (H : j = i.cast_succ) :
X.δ i ≫ X.δ j = X.δ i ≫ X.δ i.succ :=
by { subst H, rw δ_comp_δ_self, }
/-- The second cosimplicial identity -/
@[reassoc]
lemma δ_comp_σ_of_le {n} {i : fin (n+2)} {j : fin (n+1)} (H : i ≤ j.cast_succ) :
X.δ i.cast_succ ≫ X.σ j.succ = X.σ j ≫ X.δ i :=
by { dsimp [δ, σ], simp only [←X.map_comp, simplex_category.δ_comp_σ_of_le H] }
/-- The first part of the third cosimplicial identity -/
@[reassoc]
lemma δ_comp_σ_self {n} {i : fin (n+1)} :
X.δ i.cast_succ ≫ X.σ i = 𝟙 _ :=
begin
dsimp [δ, σ],
simp only [←X.map_comp, simplex_category.δ_comp_σ_self, X.map_id],
end
@[reassoc]
lemma δ_comp_σ_self' {n} {j : fin (n+2)} {i : fin (n+1)} (H : j = i.cast_succ) :
X.δ j ≫ X.σ i = 𝟙 _ :=
by { subst H, rw δ_comp_σ_self, }
/-- The second part of the third cosimplicial identity -/
@[reassoc]
lemma δ_comp_σ_succ {n} {i : fin (n+1)} :
X.δ i.succ ≫ X.σ i = 𝟙 _ :=
begin
dsimp [δ, σ],
simp only [←X.map_comp, simplex_category.δ_comp_σ_succ, X.map_id],
end
@[reassoc]
lemma δ_comp_σ_succ' {n} {j : fin (n+2)} {i : fin (n+1)} (H : j = i.succ) :
X.δ j ≫ X.σ i = 𝟙 _ :=
by { subst H, rw δ_comp_σ_succ, }
/-- The fourth cosimplicial identity -/
@[reassoc]
lemma δ_comp_σ_of_gt {n} {i : fin (n+2)} {j : fin (n+1)} (H : j.cast_succ < i) :
X.δ i.succ ≫ X.σ j.cast_succ = X.σ j ≫ X.δ i :=
by { dsimp [δ, σ], simp only [←X.map_comp, simplex_category.δ_comp_σ_of_gt H] }
@[reassoc]
lemma δ_comp_σ_of_gt' {n} {i : fin (n+3)} {j : fin (n+2)} (H : j.succ < i) :
X.δ i ≫ X.σ j = X.σ (j.cast_lt ((add_lt_add_iff_right 1).mp (lt_of_lt_of_le
(by simpa only [fin.val_eq_coe, ← fin.coe_succ]
using fin.lt_iff_coe_lt_coe.mp H) i.is_le))) ≫
X.δ (i.pred (λ hi, by simpa only [fin.not_lt_zero, hi] using H)) :=
by { dsimp [δ, σ], simpa only [←X.map_comp, ←op_comp, simplex_category.δ_comp_σ_of_gt' H], }
/-- The fifth cosimplicial identity -/
@[reassoc]
lemma σ_comp_σ {n} {i j : fin (n+1)} (H : i ≤ j) :
X.σ i.cast_succ ≫ X.σ j = X.σ j.succ ≫ X.σ i :=
by { dsimp [δ, σ], simp only [←X.map_comp, simplex_category.σ_comp_σ H] }
@[simp, reassoc]
lemma δ_naturality {X' X : cosimplicial_object C} (f : X ⟶ X') {n : ℕ} (i : fin (n+2)) :
X.δ i ≫ f.app (simplex_category.mk (n+1)) =
f.app (simplex_category.mk n) ≫ X'.δ i := f.naturality _
@[simp, reassoc]
lemma σ_naturality {X' X : cosimplicial_object C} (f : X ⟶ X') {n : ℕ} (i : fin (n+1)) :
X.σ i ≫ f.app (simplex_category.mk n) =
f.app (simplex_category.mk (n+1)) ≫ X'.σ i := f.naturality _
variable (C)
/-- Functor composition induces a functor on cosimplicial objects. -/
@[simps]
def whiskering (D : Type*) [category D] :
(C ⥤ D) ⥤ cosimplicial_object C ⥤ cosimplicial_object D :=
whiskering_right _ _ _
/-- Truncated cosimplicial objects. -/
@[derive category, nolint has_nonempty_instance]
def truncated (n : ℕ) := simplex_category.truncated n ⥤ C
variable {C}
namespace truncated
instance {n} {J : Type v} [small_category J] [has_limits_of_shape J C] :
has_limits_of_shape J (cosimplicial_object.truncated C n) :=
by {dsimp [truncated], apply_instance}
instance {n} [has_limits C] : has_limits (cosimplicial_object.truncated C n) := ⟨infer_instance⟩
instance {n} {J : Type v} [small_category J] [has_colimits_of_shape J C] :
has_colimits_of_shape J (cosimplicial_object.truncated C n) :=
by {dsimp [truncated], apply_instance}
instance {n} [has_colimits C] : has_colimits (cosimplicial_object.truncated C n) := ⟨infer_instance⟩
variable (C)
/-- Functor composition induces a functor on truncated cosimplicial objects. -/
@[simps]
def whiskering {n} (D : Type*) [category D] :
(C ⥤ D) ⥤ truncated C n ⥤ truncated D n :=
whiskering_right _ _ _
variable {C}
end truncated
section skeleton
/-- The skeleton functor from cosimplicial objects to truncated cosimplicial objects. -/
def sk (n : ℕ) : cosimplicial_object C ⥤ cosimplicial_object.truncated C n :=
(whiskering_left _ _ _).obj simplex_category.truncated.inclusion
end skeleton
variable (C)
/-- The constant cosimplicial object. -/
abbreviation const : C ⥤ cosimplicial_object C := category_theory.functor.const _
/-- Augmented cosimplicial objects. -/
@[derive category, nolint has_nonempty_instance]
def augmented := comma (const C) (𝟭 (cosimplicial_object C))
variable {C}
namespace augmented
/-- Drop the augmentation. -/
@[simps]
def drop : augmented C ⥤ cosimplicial_object C := comma.snd _ _
/-- The point of the augmentation. -/
@[simps]
def point : augmented C ⥤ C := comma.fst _ _
/-- The functor from augmented objects to arrows. -/
@[simps]
def to_arrow : augmented C ⥤ arrow C :=
{ obj := λ X,
{ left := (point.obj X),
right := (drop.obj X) _[0],
hom := X.hom.app _ },
map := λ X Y η,
{ left := (point.map η),
right := (drop.map η).app _,
w' := begin
dsimp,
rw ← nat_trans.comp_app,
erw ← η.w,
refl,
end } }
variable (C)
/-- Functor composition induces a functor on augmented cosimplicial objects. -/
@[simp]
def whiskering_obj (D : Type*) [category D] (F : C ⥤ D) :
augmented C ⥤ augmented D :=
{ obj := λ X,
{ left := F.obj (point.obj X),
right := ((whiskering _ _).obj F).obj (drop.obj X),
hom := (functor.const_comp _ _ _).inv ≫ whisker_right X.hom F },
map := λ X Y η,
{ left := F.map η.left,
right := whisker_right η.right _,
w' := begin
ext,
dsimp,
rw [category.id_comp, category.id_comp, ← F.map_comp, ← F.map_comp, ← nat_trans.comp_app],
erw ← η.w,
refl,
end } }
/-- Functor composition induces a functor on augmented cosimplicial objects. -/
@[simps]
def whiskering (D : Type u') [category.{v'} D] :
(C ⥤ D) ⥤ augmented C ⥤ augmented D :=
{ obj := whiskering_obj _ _,
map := λ X Y η,
{ app := λ A,
{ left := η.app _,
right := whisker_left _ η,
w' := begin
ext n,
dsimp,
rw [category.id_comp, category.id_comp, η.naturality],
end }, }, }
variable {C}
end augmented
open_locale simplicial
/-- Augment a cosimplicial object with an object. -/
@[simps]
def augment (X : cosimplicial_object C) (X₀ : C) (f : X₀ ⟶ X.obj [0])
(w : ∀ (i : simplex_category) (g₁ g₂ : [0] ⟶ i),
f ≫ X.map g₁ = f ≫ X.map g₂) : cosimplicial_object.augmented C :=
{ left := X₀,
right := X,
hom :=
{ app := λ i, f ≫ X.map (simplex_category.const i 0),
naturality' := begin
intros i j g,
dsimp,
simpa [← X.map_comp] using w _ _ _,
end } }
@[simp]
lemma augment_hom_zero (X : cosimplicial_object C) (X₀ : C) (f : X₀ ⟶ X.obj [0]) (w) :
(X.augment X₀ f w).hom.app [0] = f :=
by { dsimp, rw [simplex_category.hom_zero_zero ([0].const 0), X.map_id, category.comp_id] }
end cosimplicial_object
/-- The anti-equivalence between simplicial objects and cosimplicial objects. -/
@[simps]
def simplicial_cosimplicial_equiv : (simplicial_object C)ᵒᵖ ≌ (cosimplicial_object Cᵒᵖ) :=
functor.left_op_right_op_equiv _ _
/-- The anti-equivalence between cosimplicial objects and simplicial objects. -/
@[simps]
def cosimplicial_simplicial_equiv : (cosimplicial_object C)ᵒᵖ ≌ (simplicial_object Cᵒᵖ) :=
functor.op_unop_equiv _ _
variable {C}
/-- Construct an augmented cosimplicial object in the opposite
category from an augmented simplicial object. -/
@[simps]
def simplicial_object.augmented.right_op (X : simplicial_object.augmented C) :
cosimplicial_object.augmented Cᵒᵖ :=
{ left := opposite.op X.right,
right := X.left.right_op,
hom := X.hom.right_op }
/-- Construct an augmented simplicial object from an augmented cosimplicial
object in the opposite category. -/
@[simps]
def cosimplicial_object.augmented.left_op (X : cosimplicial_object.augmented Cᵒᵖ) :
simplicial_object.augmented C :=
{ left := X.right.left_op,
right := X.left.unop,
hom := X.hom.left_op }
/-- Converting an augmented simplicial object to an augmented cosimplicial
object and back is isomorphic to the given object. -/
@[simps]
def simplicial_object.augmented.right_op_left_op_iso (X : simplicial_object.augmented C) :
X.right_op.left_op ≅ X :=
comma.iso_mk X.left.right_op_left_op_iso (eq_to_iso $ by simp) (by tidy)
/-- Converting an augmented cosimplicial object to an augmented simplicial
object and back is isomorphic to the given object. -/
@[simps]
def cosimplicial_object.augmented.left_op_right_op_iso (X : cosimplicial_object.augmented Cᵒᵖ) :
X.left_op.right_op ≅ X :=
comma.iso_mk (eq_to_iso $ by simp) X.right.left_op_right_op_iso (by tidy)
variable (C)
/-- A functorial version of `simplicial_object.augmented.right_op`. -/
@[simps]
def simplicial_to_cosimplicial_augmented :
(simplicial_object.augmented C)ᵒᵖ ⥤ cosimplicial_object.augmented Cᵒᵖ :=
{ obj := λ X, X.unop.right_op,
map := λ X Y f,
{ left := f.unop.right.op,
right := f.unop.left.right_op,
w' := begin
ext x,
dsimp,
simp_rw ← op_comp,
congr' 1,
exact (congr_app f.unop.w (op x)).symm,
end } }
/-- A functorial version of `cosimplicial_object.augmented.left_op`. -/
@[simps]
def cosimplicial_to_simplicial_augmented :
cosimplicial_object.augmented Cᵒᵖ ⥤ (simplicial_object.augmented C)ᵒᵖ :=
{ obj := λ X, opposite.op X.left_op,
map := λ X Y f, quiver.hom.op $
{ left := f.right.left_op,
right := f.left.unop,
w' := begin
ext x,
dsimp,
simp_rw ← unop_comp,
congr' 1,
exact (congr_app f.w x.unop).symm,
end} }
/-- The contravariant categorical equivalence between augmented simplicial
objects and augmented cosimplicial objects in the opposite category. -/
@[simps functor inverse]
def simplicial_cosimplicial_augmented_equiv :
(simplicial_object.augmented C)ᵒᵖ ≌ cosimplicial_object.augmented Cᵒᵖ :=
equivalence.mk
(simplicial_to_cosimplicial_augmented _)
(cosimplicial_to_simplicial_augmented _)
(nat_iso.of_components (λ X, X.unop.right_op_left_op_iso.op) $ λ X Y f,
by { dsimp, rw ←f.op_unop, simp_rw ← op_comp, congr' 1, tidy })
(nat_iso.of_components (λ X, X.left_op_right_op_iso) $ by tidy)
end category_theory
|
fc587089256e3bfc036d0f656ea4d387ed1ad1d1 | 3dc4623269159d02a444fe898d33e8c7e7e9461b | /.github/workflows/project_1_a_decrire/projet_A2/sous_foncteur_droite affine/sous_foncteur.lean | 022879597547c283e602883bb5a7d8d8a824d14f | [] | no_license | Or7ando/lean | cc003e6c41048eae7c34aa6bada51c9e9add9e66 | d41169cf4e416a0d42092fb6bdc14131cee9dd15 | refs/heads/master | 1,650,600,589,722 | 1,587,262,906,000 | 1,587,262,906,000 | 255,387,160 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,088 | lean | import algebra.category.CommRing.basic
import algebra.ring
import tactic
import data.polynomial
import algebra.ring
import category_theory.types
import data.int.basic
universes v u
open CommRing
open is_ring_hom
open category_theory
open polynomial
open int
/--
## The goal is study the set of solution of polynomial equation in one variable ! We form a functor V_P : CommRing → Type v
## For all ring
##
-/
variables(R : Type v)[comm_ring R](P : polynomial ℤ)
structure V (R: Type v)[comm_ring R] := -- set of solution of P(x) = 0 with x in R
(x : R) -- if φ : R → R' is a ring morphism then we have application
(certif : eval₂(int.cast) (x) (P) = 0 ) -- φ : V(P)(R) → V(P(R')
@[ext]lemma ext : ∀ {ζ1 ζ2 : V P R}, ζ1.x = ζ2.x → ζ1 = ζ2 := λ ζ1 ζ2,
begin
cases ζ1,
cases ζ2,
intro h,
congr ; try { assumption },
end
definition map_V {R : Type v} [comm_ring R] {R' : Type v}[comm_ring R'] (f : R → R') [is_ring_hom f] : (V P R) → (V P R') := λ ζ,begin
exact {x := f ζ.x, certif :=
begin
have H : eval₂ (f ∘ int.cast) (f ζ.x) P = f (eval₂ int.cast (ζ.x) P),
rw ← hom_eval₂ P int.cast f ζ.x,
have G : f ∘ int.cast = int.cast,
exact (int.eq_cast') (f ∘ int.cast),
rw G at H,
rw H,
have cer : eval₂ int.cast ζ.x P =0,
exact ζ.certif,
rw cer,
exact map_zero f,
end}
end
lemma map_V_comp {R : Type v} [comm_ring R] {R' : Type v}[comm_ring R'] (f : R → R') [is_ring_hom f](ζ : V P R) : (map_V P f ζ).x = f ζ.x := rfl
def V_i : CommRing ⥤ Type v :=
{ obj := λ R, V P R,
map := λ R R' f, map_V P f,
map_id' := λ R, begin
apply funext,
intro ζ,
ext,
rw types_id,
rw map_V_comp,
exact rfl,
end,
map_comp' := λ R R' R'' f g,
begin
apply funext,
intro ζ,
ext,
rw map_V_comp,
rw types_comp,
rw map_V_comp,
rw map_V_comp,
exact rfl,
end
}
def F := (V_i P).obj
#print F |
0a14eb23d21fcaa9a8510bf047211ac421184f97 | 92b50235facfbc08dfe7f334827d47281471333b | /library/data/rat/basic.lean | 1c97bc9943cba0cc14c7d5a05c4af836b6b7b816 | [
"Apache-2.0"
] | permissive | htzh/lean | 24f6ed7510ab637379ec31af406d12584d31792c | d70c79f4e30aafecdfc4a60b5d3512199200ab6e | refs/heads/master | 1,607,677,731,270 | 1,437,089,952,000 | 1,437,089,952,000 | 37,078,816 | 0 | 0 | null | 1,433,780,956,000 | 1,433,780,955,000 | null | UTF-8 | Lean | false | false | 21,126 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Jeremy Avigad
The rational numbers as a field generated by the integers, defined as the usual quotient.
-/
import data.int algebra.field
open int quot eq.ops
record prerat : Type :=
(num : ℤ) (denom : ℤ) (denom_pos : denom > 0)
/-
prerat: the representations of the rationals as integers num, denom, with denom > 0.
note: names are not protected, because it is not expected that users will open prerat.
-/
namespace prerat
/- the equivalence relation -/
definition equiv (a b : prerat) : Prop := num a * denom b = num b * denom a
infix `≡` := equiv
theorem equiv.refl [refl] (a : prerat) : a ≡ a := rfl
theorem equiv.symm [symm] {a b : prerat} (H : a ≡ b) : b ≡ a := !eq.symm H
theorem num_eq_zero_of_equiv {a b : prerat} (H : a ≡ b) (na_zero : num a = 0) : num b = 0 :=
have H1 : num a * denom b = 0, from !zero_mul ▸ na_zero ▸ rfl,
have H2 : num b * denom a = 0, from H ▸ H1,
show num b = 0, from or_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero H2) (ne_of_gt (denom_pos a))
theorem num_pos_of_equiv {a b : prerat} (H : a ≡ b) (na_pos : num a > 0) : num b > 0 :=
have H1 : num a * denom b > 0, from mul_pos na_pos (denom_pos b),
have H2 : num b * denom a > 0, from H ▸ H1,
show num b > 0, from pos_of_mul_pos_right H2 (le_of_lt (denom_pos a))
theorem num_neg_of_equiv {a b : prerat} (H : a ≡ b) (na_neg : num a < 0) : num b < 0 :=
have H1 : num a * denom b < 0, from mul_neg_of_neg_of_pos na_neg (denom_pos b),
have H2 : -(-num b * denom a) < 0, from !neg_mul_eq_neg_mul⁻¹ ▸ !neg_neg⁻¹ ▸ H ▸ H1,
have H3 : -num b > 0, from pos_of_mul_pos_right (pos_of_neg_neg H2) (le_of_lt (denom_pos a)),
neg_of_neg_pos H3
theorem equiv_of_num_eq_zero {a b : prerat} (H1 : num a = 0) (H2 : num b = 0) : a ≡ b :=
by rewrite [↑equiv, H1, H2, *zero_mul]
theorem equiv.trans [trans] {a b c : prerat} (H1 : a ≡ b) (H2 : b ≡ c) : a ≡ c :=
decidable.by_cases
(assume b0 : num b = 0,
have a0 : num a = 0, from num_eq_zero_of_equiv (equiv.symm H1) b0,
have c0 : num c = 0, from num_eq_zero_of_equiv H2 b0,
equiv_of_num_eq_zero a0 c0)
(assume bn0 : num b ≠ 0,
have H3 : num b * denom b ≠ 0, from mul_ne_zero bn0 (ne_of_gt (denom_pos b)),
have H4 : (num b * denom b) * (num a * denom c) = (num b * denom b) * (num c * denom a),
from calc
(num b * denom b) * (num a * denom c) = (num a * denom b) * (num b * denom c) :
by rewrite [*mul.assoc, *mul.left_comm (num a), *mul.left_comm (num b)]
... = (num b * denom a) * (num b * denom c) : {H1}
... = (num b * denom a) * (num c * denom b) : {H2}
... = (num b * denom b) * (num c * denom a) :
by rewrite [*mul.assoc, *mul.left_comm (denom a),
*mul.left_comm (denom b), mul.comm (denom a)],
eq_of_mul_eq_mul_left H3 H4)
theorem equiv.is_equivalence : equivalence equiv :=
mk_equivalence equiv equiv.refl @equiv.symm @equiv.trans
definition setoid : setoid prerat :=
setoid.mk equiv equiv.is_equivalence
/- field operations -/
definition of_int (i : int) : prerat := prerat.mk i 1 !of_nat_succ_pos
definition zero : prerat := of_int 0
definition one : prerat := of_int 1
private theorem mul_denom_pos (a b : prerat) : denom a * denom b > 0 :=
mul_pos (denom_pos a) (denom_pos b)
definition add (a b : prerat) : prerat :=
prerat.mk (num a * denom b + num b * denom a) (denom a * denom b) (mul_denom_pos a b)
definition mul (a b : prerat) : prerat :=
prerat.mk (num a * num b) (denom a * denom b) (mul_denom_pos a b)
definition neg (a : prerat) : prerat :=
prerat.mk (- num a) (denom a) (denom_pos a)
theorem of_int_add (a b : ℤ) : of_int (#int a + b) ≡ add (of_int a) (of_int b) :=
by esimp [equiv, num, denom, one, add, of_int]; rewrite [*int.mul_one]
theorem of_int_mul (a b : ℤ) : of_int (#int a * b) ≡ mul (of_int a) (of_int b) :=
!equiv.refl
theorem of_int_neg (a : ℤ) : of_int (#int -a) ≡ neg (of_int a) :=
!equiv.refl
theorem of_int.inj {a b : ℤ} : of_int a ≡ of_int b → a = b :=
by rewrite [↑of_int, ↑equiv, *mul_one]; intros; assumption
definition inv : prerat → prerat
| inv (prerat.mk nat.zero d dp) := zero
| inv (prerat.mk (nat.succ n) d dp) := prerat.mk d (nat.succ n) !of_nat_succ_pos
| inv (prerat.mk -[1+n] d dp) := prerat.mk (-d) (nat.succ n) !of_nat_succ_pos
theorem equiv_zero_of_num_eq_zero {a : prerat} (H : num a = 0) : a ≡ zero :=
by rewrite [↑equiv, H, ↑zero, ↑num, ↑of_int, *zero_mul]
theorem num_eq_zero_of_equiv_zero {a : prerat} : a ≡ zero → num a = 0 :=
by rewrite [↑equiv, ↑zero, ↑of_int, mul_one, zero_mul]; intro H; exact H
theorem inv_zero {d : int} (dp : d > 0) : inv (mk nat.zero d dp) = zero :=
begin rewrite [↑inv, ▸*] end
theorem inv_zero' : inv zero = zero := inv_zero (of_nat_succ_pos nat.zero)
theorem inv_of_pos {n d : int} (np : n > 0) (dp : d > 0) : inv (mk n d dp) ≡ mk d n np :=
obtain (n' : nat) (Hn' : n = of_nat n'), from exists_eq_of_nat (le_of_lt np),
have H1 : (#nat n' > nat.zero), from lt_of_of_nat_lt_of_nat (Hn' ▸ np),
obtain (k : nat) (Hk : n' = nat.succ k), from nat.exists_eq_succ_of_lt H1,
have H2 : d * n = d * nat.succ k, by rewrite [Hn', Hk],
Hn'⁻¹ ▸ (Hk⁻¹ ▸ H2)
theorem inv_neg {n d : int} (np : n > 0) (dp : d > 0) : inv (mk (-n) d dp) ≡ mk (-d) n np :=
obtain (n' : nat) (Hn' : n = of_nat n'), from exists_eq_of_nat (le_of_lt np),
have H1 : (#nat n' > nat.zero), from lt_of_of_nat_lt_of_nat (Hn' ▸ np),
obtain (k : nat) (Hk : n' = nat.succ k), from nat.exists_eq_succ_of_lt H1,
have H2 : -d * n = -d * nat.succ k, by rewrite [Hn', Hk],
have H3 : inv (mk -[1+k] d dp) ≡ mk (-d) n np, from H2,
have H4 : -[1+k] = -n, from calc
-[1+k] = -(nat.succ k) : rfl
... = -n : by rewrite [Hk⁻¹, Hn'],
H4 ▸ H3
theorem inv_of_neg {n d : int} (nn : n < 0) (dp : d > 0) :
inv (mk n d dp) ≡ mk (-d) (-n) (neg_pos_of_neg nn) :=
have H : inv (mk (-(-n)) d dp) ≡ mk (-d) (-n) (neg_pos_of_neg nn),
from inv_neg (neg_pos_of_neg nn) dp,
!neg_neg ▸ H
/- operations respect equiv -/
theorem add_equiv_add {a1 b1 a2 b2 : prerat} (eqv1 : a1 ≡ a2) (eqv2 : b1 ≡ b2) :
add a1 b1 ≡ add a2 b2 :=
calc
(num a1 * denom b1 + num b1 * denom a1) * (denom a2 * denom b2)
= num a1 * denom a2 * denom b1 * denom b2 + num b1 * denom b2 * denom a1 * denom a2 :
by rewrite [mul.right_distrib, *mul.assoc, mul.left_comm (denom b1),
mul.comm (denom b2), *mul.assoc]
... = num a2 * denom a1 * denom b1 * denom b2 + num b2 * denom b1 * denom a1 * denom a2 :
by rewrite [↑equiv at *, eqv1, eqv2]
... = (num a2 * denom b2 + num b2 * denom a2) * (denom a1 * denom b1) :
by rewrite [mul.right_distrib, *mul.assoc, *mul.left_comm (denom b2),
*mul.comm (denom b1), *mul.assoc, mul.left_comm (denom a2)]
theorem mul_equiv_mul {a1 b1 a2 b2 : prerat} (eqv1 : a1 ≡ a2) (eqv2 : b1 ≡ b2) :
mul a1 b1 ≡ mul a2 b2 :=
calc
(num a1 * num b1) * (denom a2 * denom b2)
= (num a1 * denom a2) * (num b1 * denom b2) : by rewrite [*mul.assoc, mul.left_comm (num b1)]
... = (num a2 * denom a1) * (num b2 * denom b1) : by rewrite [↑equiv at *, eqv1, eqv2]
... = (num a2 * num b2) * (denom a1 * denom b1) : by rewrite [*mul.assoc, mul.left_comm (num b2)]
theorem neg_equiv_neg {a b : prerat} (eqv : a ≡ b) : neg a ≡ neg b :=
calc
-num a * denom b = -(num a * denom b) : neg_mul_eq_neg_mul
... = -(num b * denom a) : {eqv}
... = -num b * denom a : neg_mul_eq_neg_mul
theorem inv_equiv_inv : ∀{a b : prerat}, a ≡ b → inv a ≡ inv b
| (mk an ad adp) (mk bn bd bdp) :=
assume H,
lt.by_cases
(assume an_neg : an < 0,
have bn_neg : bn < 0, from num_neg_of_equiv H an_neg,
calc
inv (mk an ad adp) ≡ mk (-ad) (-an) (neg_pos_of_neg an_neg) : inv_of_neg an_neg adp
... ≡ mk (-bd) (-bn) (neg_pos_of_neg bn_neg) :
by rewrite [↑equiv at *, ▸*, *neg_mul_neg, mul.comm ad, mul.comm bd, H]
... ≡ inv (mk bn bd bdp) : (inv_of_neg bn_neg bdp)⁻¹)
(assume an_zero : an = 0,
have bn_zero : bn = 0, from num_eq_zero_of_equiv H an_zero,
eq.subst (calc
inv (mk an ad adp) = inv (mk 0 ad adp) : {an_zero}
... = zero : inv_zero
... = inv (mk 0 bd bdp) : inv_zero
... = inv (mk bn bd bdp) : bn_zero) !equiv.refl)
(assume an_pos : an > 0,
have bn_pos : bn > 0, from num_pos_of_equiv H an_pos,
calc
inv (mk an ad adp) ≡ mk ad an an_pos : inv_of_pos an_pos adp
... ≡ mk bd bn bn_pos :
by rewrite [↑equiv at *, ▸*, mul.comm ad, mul.comm bd, H]
... ≡ inv (mk bn bd bdp) : (inv_of_pos bn_pos bdp)⁻¹)
/- properties -/
theorem add.comm (a b : prerat) : add a b ≡ add b a :=
by rewrite [↑add, ↑equiv, ▸*, add.comm, mul.comm (denom a)]
theorem add.assoc (a b c : prerat) : add (add a b) c ≡ add a (add b c) :=
by rewrite [↑add, ↑equiv, ▸*, *(mul.comm (num c)), *(λy, mul.comm y (denom a)), *mul.left_distrib,
*mul.right_distrib, *mul.assoc, *add.assoc]
theorem add_zero (a : prerat) : add a zero ≡ a :=
by rewrite [↑add, ↑equiv, ↑zero, ↑of_int, ▸*, *mul_one, zero_mul, add_zero]
theorem add.left_inv (a : prerat) : add (neg a) a ≡ zero :=
by rewrite [↑add, ↑equiv, ↑neg, ↑zero, ↑of_int, ▸*, -neg_mul_eq_neg_mul, add.left_inv, *zero_mul]
theorem mul.comm (a b : prerat) : mul a b ≡ mul b a :=
by rewrite [↑mul, ↑equiv, mul.comm (num a), mul.comm (denom a)]
theorem mul.assoc (a b c : prerat) : mul (mul a b) c ≡ mul a (mul b c) :=
by rewrite [↑mul, ↑equiv, *mul.assoc]
theorem mul_one (a : prerat) : mul a one ≡ a :=
by rewrite [↑mul, ↑one, ↑of_int, ↑equiv, ▸*, *mul_one]
-- with the simplifier this will be easy
theorem mul.left_distrib (a b c : prerat) : mul a (add b c) ≡ add (mul a b) (mul a c) :=
begin
rewrite [↑mul, ↑add, ↑equiv, ▸*, *mul.left_distrib, *mul.right_distrib, -*int.mul.assoc],
apply sorry
end
theorem mul_inv_cancel : ∀{a : prerat}, ¬ a ≡ zero → mul a (inv a) ≡ one
| (mk an ad adp) :=
assume H,
let a := mk an ad adp in
lt.by_cases
(assume an_neg : an < 0,
let ia := mk (-ad) (-an) (neg_pos_of_neg an_neg) in
calc
mul a (inv a) ≡ mul a ia : mul_equiv_mul !equiv.refl (inv_of_neg an_neg adp)
... ≡ one : begin
esimp [equiv, num, denom, one, mul, of_int],
rewrite [*int.mul_one, *int.one_mul, int.mul.comm,
neg_mul_comm]
end)
(assume an_zero : an = 0, absurd (equiv_zero_of_num_eq_zero an_zero) H)
(assume an_pos : an > 0,
let ia := mk ad an an_pos in
calc
mul a (inv a) ≡ mul a ia : mul_equiv_mul !equiv.refl (inv_of_pos an_pos adp)
... ≡ one : begin
esimp [equiv, num, denom, one, mul, of_int],
rewrite [*int.mul_one, *int.one_mul, int.mul.comm]
end)
theorem zero_not_equiv_one : ¬ zero ≡ one :=
begin
esimp [equiv, zero, one, of_int],
rewrite [zero_mul, int.mul_one],
exact zero_ne_one
end
theorem mul_denom_equiv (a : prerat) : mul a (of_int (denom a)) ≡ of_int (num a) :=
by esimp [mul, of_int, equiv]; rewrite [*int.mul_one]
/- Reducing a fraction to lowest terms. Needed to choose a canonical representative of rat, and
define numerator and denominator. -/
definition reduce : prerat → prerat
| (mk an ad adpos) :=
have pos : ad div gcd an ad > 0, from div_pos_of_pos_of_dvd adpos !gcd_nonneg !gcd_dvd_right,
if an = 0 then prerat.zero
else mk (an div gcd an ad) (ad div gcd an ad) pos
protected theorem eq {a b : prerat} (Hn : num a = num b) (Hd : denom a = denom b) : a = b :=
begin
cases a with [an, ad, adpos],
cases b with [bn, bd, bdpos],
generalize adpos, generalize bdpos,
esimp at *,
rewrite [Hn, Hd],
intros, apply rfl
end
theorem reduce_equiv : ∀ a : prerat, reduce a ≡ a
| (mk an ad adpos) :=
decidable.by_cases
(assume anz : an = 0,
by krewrite [↑reduce, if_pos anz, ↑equiv, anz, *zero_mul])
(assume annz : an ≠ 0,
by rewrite [↑reduce, if_neg annz, ↑equiv, int.mul.comm, -!mul_div_assoc !gcd_dvd_left,
-!mul_div_assoc !gcd_dvd_right, int.mul.comm])
theorem reduce_eq_reduce : ∀{a b : prerat}, a ≡ b → reduce a = reduce b
| (mk an ad adpos) (mk bn bd bdpos) :=
assume H : an * bd = bn * ad,
decidable.by_cases
(assume anz : an = 0,
have H' : bn * ad = 0, by rewrite [-H, anz, zero_mul],
assert bnz : bn = 0,
from or_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero H') (ne_of_gt adpos),
by rewrite [↑reduce, if_pos anz, if_pos bnz])
(assume annz : an ≠ 0,
assert bnnz : bn ≠ 0, from
assume bnz,
have H' : an * bd = 0, by rewrite [H, bnz, zero_mul],
have anz : an = 0,
from or_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero H') (ne_of_gt bdpos),
show false, from annz anz,
begin
rewrite [↑reduce, if_neg annz, if_neg bnnz],
apply prerat.eq,
{apply div_gcd_eq_div_gcd H adpos bdpos},
{esimp, rewrite [gcd.comm, gcd.comm bn],
apply div_gcd_eq_div_gcd_of_nonneg,
rewrite [int.mul.comm, -H, int.mul.comm],
apply annz,
apply bnnz,
apply le_of_lt adpos,
apply le_of_lt bdpos},
end)
end prerat
/-
the rationals
-/
definition rat : Type.{1} := quot prerat.setoid
notation `ℚ` := rat
local attribute prerat.setoid [instance]
namespace rat
/- operations -/
definition of_int [coercion] (i : ℤ) : ℚ := ⟦prerat.of_int i⟧
definition of_nat [coercion] (n : ℕ) : ℚ := ⟦prerat.of_int n⟧
definition of_num [coercion] [reducible] (n : num) : ℚ := of_int (int.of_num n)
definition add : ℚ → ℚ → ℚ :=
quot.lift₂
(λ a b : prerat, ⟦prerat.add a b⟧)
(take a1 a2 b1 b2, assume H1 H2, quot.sound (prerat.add_equiv_add H1 H2))
definition mul : ℚ → ℚ → ℚ :=
quot.lift₂
(λ a b : prerat, ⟦prerat.mul a b⟧)
(take a1 a2 b1 b2, assume H1 H2, quot.sound (prerat.mul_equiv_mul H1 H2))
definition neg : ℚ → ℚ :=
quot.lift
(λ a : prerat, ⟦prerat.neg a⟧)
(take a1 a2, assume H, quot.sound (prerat.neg_equiv_neg H))
definition inv : ℚ → ℚ :=
quot.lift
(λ a : prerat, ⟦prerat.inv a⟧)
(take a1 a2, assume H, quot.sound (prerat.inv_equiv_inv H))
definition reduce : ℚ → prerat :=
quot.lift
(λ a : prerat, prerat.reduce a)
@prerat.reduce_eq_reduce
definition num (a : ℚ) : ℤ := prerat.num (reduce a)
definition denom (a : ℚ) : ℤ := prerat.denom (reduce a)
theorem denom_pos (a : ℚ): denom a > 0 :=
prerat.denom_pos (reduce a)
protected definition prio := num.pred int.prio
infix [priority rat.prio] + := rat.add
infix [priority rat.prio] * := rat.mul
prefix [priority rat.prio] - := rat.neg
definition sub [reducible] (a b : rat) : rat := a + (-b)
postfix [priority rat.prio] ⁻¹ := rat.inv
infix [priority rat.prio] - := rat.sub
/- properties -/
theorem of_int_add (a b : ℤ) : of_int (#int a + b) = of_int a + of_int b :=
quot.sound (prerat.of_int_add a b)
theorem of_int_mul (a b : ℤ) : of_int (#int a * b) = of_int a * of_int b :=
quot.sound (prerat.of_int_mul a b)
theorem of_int_neg (a : ℤ) : of_int (#int -a) = -(of_int a) :=
quot.sound (prerat.of_int_neg a)
theorem of_int_sub (a b : ℤ) : of_int (#int a - b) = of_int a - of_int b :=
calc
of_int (#int a - b) = of_int a + of_int (#int -b) : of_int_add
... = of_int a - of_int b : {of_int_neg b}
theorem of_int.inj {a b : ℤ} (H : of_int a = of_int b) : a = b :=
prerat.of_int.inj (quot.exact H)
theorem of_nat_eq (a : ℕ) : of_nat a = of_int (int.of_nat a) := rfl
theorem of_nat_add (a b : ℕ) : of_nat (#nat a + b) = of_nat a + of_nat b :=
by rewrite [*of_nat_eq, int.of_nat_add, rat.of_int_add]
theorem of_nat_mul (a b : ℕ) : of_nat (#nat a * b) = of_nat a * of_nat b :=
by rewrite [*of_nat_eq, int.of_nat_mul, rat.of_int_mul]
theorem of_nat_sub {a b : ℕ} (H : #nat a ≥ b) : of_nat (#nat a - b) = of_nat a - of_nat b :=
by rewrite [*of_nat_eq, int.of_nat_sub H, rat.of_int_sub]
theorem add.comm (a b : ℚ) : a + b = b + a :=
quot.induction_on₂ a b (take u v, quot.sound !prerat.add.comm)
theorem add.assoc (a b c : ℚ) : a + b + c = a + (b + c) :=
quot.induction_on₃ a b c (take u v w, quot.sound !prerat.add.assoc)
theorem add_zero (a : ℚ) : a + 0 = a :=
quot.induction_on a (take u, quot.sound !prerat.add_zero)
theorem zero_add (a : ℚ) : 0 + a = a := !add.comm ▸ !add_zero
theorem add.left_inv (a : ℚ) : -a + a = 0 :=
quot.induction_on a (take u, quot.sound !prerat.add.left_inv)
theorem mul.comm (a b : ℚ) : a * b = b * a :=
quot.induction_on₂ a b (take u v, quot.sound !prerat.mul.comm)
theorem mul.assoc (a b c : ℚ) : a * b * c = a * (b * c) :=
quot.induction_on₃ a b c (take u v w, quot.sound !prerat.mul.assoc)
theorem mul_one (a : ℚ) : a * 1 = a :=
quot.induction_on a (take u, quot.sound !prerat.mul_one)
theorem one_mul (a : ℚ) : 1 * a = a := !mul.comm ▸ !mul_one
theorem mul.left_distrib (a b c : ℚ) : a * (b + c) = a * b + a * c :=
quot.induction_on₃ a b c (take u v w, quot.sound !prerat.mul.left_distrib)
theorem mul.right_distrib (a b c : ℚ) : (a + b) * c = a * c + b * c :=
by rewrite [mul.comm, mul.left_distrib, *mul.comm c]
theorem mul_inv_cancel {a : ℚ} : a ≠ 0 → a * a⁻¹ = 1 :=
quot.induction_on a
(take u,
assume H,
quot.sound (!prerat.mul_inv_cancel (assume H1, H (quot.sound H1))))
theorem inv_mul_cancel {a : ℚ} (H : a ≠ 0) : a⁻¹ * a = 1 :=
!mul.comm ▸ mul_inv_cancel H
theorem zero_ne_one : (0 : ℚ) ≠ 1 :=
assume H, prerat.zero_not_equiv_one (quot.exact H)
definition has_decidable_eq [instance] : decidable_eq ℚ :=
take a b, quot.rec_on_subsingleton₂ a b
(take u v,
if H : prerat.num u * prerat.denom v = prerat.num v * prerat.denom u
then decidable.inl (quot.sound H)
else decidable.inr (assume H1, H (quot.exact H1)))
theorem inv_zero : inv 0 = 0 :=
quot.sound (prerat.inv_zero' ▸ !prerat.equiv.refl)
theorem quot_reduce (a : ℚ) : ⟦reduce a⟧ = a :=
quot.induction_on a (take u, quot.sound !prerat.reduce_equiv)
theorem mul_denom (a : ℚ) : a * denom a = num a :=
have H : ⟦reduce a⟧ * of_int (denom a) = of_int (num a), from quot.sound (!prerat.mul_denom_equiv),
quot_reduce a ▸ H
section migrate_algebra
open [classes] algebra
protected definition discrete_field [reducible] : algebra.discrete_field rat :=
⦃algebra.discrete_field,
add := add,
add_assoc := add.assoc,
zero := 0,
zero_add := zero_add,
add_zero := add_zero,
neg := neg,
add_left_inv := add.left_inv,
add_comm := add.comm,
mul := mul,
mul_assoc := mul.assoc,
one := (of_num 1),
one_mul := one_mul,
mul_one := mul_one,
left_distrib := mul.left_distrib,
right_distrib := mul.right_distrib,
mul_comm := mul.comm,
mul_inv_cancel := @mul_inv_cancel,
inv_mul_cancel := @inv_mul_cancel,
zero_ne_one := zero_ne_one,
inv_zero := inv_zero,
has_decidable_eq := has_decidable_eq⦄
local attribute rat.discrete_field [instance]
definition divide (a b : rat) := algebra.divide a b
infix `/` := divide
definition dvd (a b : rat) := algebra.dvd a b
migrate from algebra with rat
replacing sub → rat.sub, divide → divide, dvd → dvd
end migrate_algebra
theorem eq_num_div_denom (a : ℚ) : a = num a / denom a :=
have H : of_int (denom a) ≠ 0, from assume H', ne_of_gt (denom_pos a) (of_int.inj H'),
iff.mp' (eq_div_iff_mul_eq H) (mul_denom a)
theorem of_nat_div {a b : ℤ} (H : b ∣ a) : of_int (a div b) = of_int a / of_int b :=
decidable.by_cases
(assume bz : b = 0,
by rewrite [bz, div_zero, int.div_zero])
(assume bnz : b ≠ 0,
have bnz' : of_int b ≠ 0, from assume oibz, bnz (of_int.inj oibz),
have H' : of_int (a div b) * of_int b = of_int a, from
int.dvd.elim H
(take c, assume Hc : a = b * c,
by rewrite [Hc, !int.mul_div_cancel_left bnz, mul.comm]),
iff.mp' (eq_div_iff_mul_eq bnz') H')
end rat
|
31cd6ec4f6b950c51ab240b6f8ee9932998bfcab | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/bad_id.lean | 50ef61c6b9316651c635ec663a327490f53f3426 | [
"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 | 71 | lean | import data.num
definition x.y : nat := 10
definition x.1 :nat := 10
|
d27501bbeb14234847352b04921a3076c496e6cf | 2fbe653e4bc441efde5e5d250566e65538709888 | /src/topology/metric_space/hausdorff_distance.lean | 6f32c4e018fa81da9231da3d9aadc09ec5627767 | [
"Apache-2.0"
] | permissive | aceg00/mathlib | 5e15e79a8af87ff7eb8c17e2629c442ef24e746b | 8786ea6d6d46d6969ac9a869eb818bf100802882 | refs/heads/master | 1,649,202,698,930 | 1,580,924,783,000 | 1,580,924,783,000 | 149,197,272 | 0 | 0 | Apache-2.0 | 1,537,224,208,000 | 1,537,224,207,000 | null | UTF-8 | Lean | false | false | 32,434 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Sébastien Gouëzel
-/
import topology.metric_space.isometry topology.instances.ennreal
topology.metric_space.lipschitz
/-!
# Hausdorff distance
The Hausdorff distance on subsets of a metric (or emetric) space.
Given two subsets `s` and `t` of a metric space, their Hausdorff distance is the smallest `d`
such that any point `s` is within `d` of a point in `t`, and conversely. This quantity
is often infinite (think of `s` bounded and `t` unbounded), and therefore better
expressed in the setting of emetric spaces.
## Main definitions
This files introduces:
* `inf_edist x s`, the infimum edistance of a point `x` to a set `s` in an emetric space
* `Hausdorff_edist s t`, the Hausdorff edistance of two sets in an emetric space
* Versions of these notions on metric spaces, called respectively `inf_dist` and
`Hausdorff_dist`.
-/
noncomputable theory
open_locale classical
universes u v w
open classical lattice set function topological_space filter
namespace emetric
section inf_edist
open_locale ennreal
variables {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {x y : α} {s t : set α} {Φ : α → β}
/-- The minimal edistance of a point to a set -/
def inf_edist (x : α) (s : set α) : ennreal := Inf ((edist x) '' s)
@[simp] lemma inf_edist_empty : inf_edist x ∅ = ∞ :=
by unfold inf_edist; simp
/-- The edist to a union is the minimum of the edists -/
@[simp] lemma inf_edist_union : inf_edist x (s ∪ t) = inf_edist x s ⊓ inf_edist x t :=
by simp [inf_edist, image_union, Inf_union]
/-- The edist to a singleton is the edistance to the single point of this singleton -/
@[simp] lemma inf_edist_singleton : inf_edist x {y} = edist x y :=
by simp [inf_edist]
/-- The edist to a set is bounded above by the edist to any of its points -/
lemma inf_edist_le_edist_of_mem (h : y ∈ s) : inf_edist x s ≤ edist x y :=
Inf_le ((mem_image _ _ _).2 ⟨y, h, by refl⟩)
/-- If a point `x` belongs to `s`, then its edist to `s` vanishes -/
lemma inf_edist_zero_of_mem (h : x ∈ s) : inf_edist x s = 0 :=
le_zero_iff_eq.1 $ @edist_self _ _ x ▸ inf_edist_le_edist_of_mem h
/-- The edist is monotonous with respect to inclusion -/
lemma inf_edist_le_inf_edist_of_subset (h : s ⊆ t) : inf_edist x t ≤ inf_edist x s :=
Inf_le_Inf (image_subset _ h)
/-- If the edist to a set is `< r`, there exists a point in the set at edistance `< r` -/
lemma exists_edist_lt_of_inf_edist_lt {r : ennreal} (h : inf_edist x s < r) :
∃y∈s, edist x y < r :=
let ⟨t, ⟨ht, tr⟩⟩ := Inf_lt_iff.1 h in
let ⟨y, ⟨ys, hy⟩⟩ := (mem_image _ _ _).1 ht in
⟨y, ys, by rwa ← hy at tr⟩
/-- The edist of `x` to `s` is bounded by the sum of the edist of `y` to `s` and
the edist from `x` to `y` -/
lemma inf_edist_le_inf_edist_add_edist : inf_edist x s ≤ inf_edist y s + edist x y :=
begin
have : ∀z ∈ s, Inf (edist x '' s) ≤ edist y z + edist x y := λz hz, calc
Inf (edist x '' s) ≤ edist x z :
Inf_le ((mem_image _ _ _).2 ⟨z, hz, by refl⟩)
... ≤ edist x y + edist y z : edist_triangle _ _ _
... = edist y z + edist x y : add_comm _ _,
have : (λz, z + edist x y) (Inf (edist y '' s)) = Inf ((λz, z + edist x y) '' (edist y '' s)),
{ refine Inf_of_continuous _ _ (by simp),
{ exact continuous_id.add continuous_const },
{ assume a b h, simp, apply add_le_add_right' h }},
simp only [inf_edist] at this,
rw [inf_edist, inf_edist, this, ← image_comp],
simpa only [and_imp, function.comp_app, lattice.le_Inf_iff, exists_imp_distrib, ball_image_iff]
end
/-- The edist to a set depends continuously on the point -/
lemma continuous_inf_edist : continuous (λx, inf_edist x s) :=
continuous_of_le_add_edist 1 (by simp) $
by simp only [one_mul, inf_edist_le_inf_edist_add_edist, forall_2_true_iff]
/-- The edist to a set and to its closure coincide -/
lemma inf_edist_closure : inf_edist x (closure s) = inf_edist x s :=
begin
refine le_antisymm (inf_edist_le_inf_edist_of_subset subset_closure) _,
refine ennreal.le_of_forall_epsilon_le (λε εpos h, _),
have εpos' : (0 : ennreal) < ε := by simpa,
have : inf_edist x (closure s) < inf_edist x (closure s) + ε/2 :=
ennreal.lt_add_right h (ennreal.half_pos εpos'),
rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ycs, hy⟩,
-- y : α, ycs : y ∈ closure s, hy : edist x y < inf_edist x (closure s) + ↑ε / 2
rcases emetric.mem_closure_iff'.1 ycs (ε/2) (ennreal.half_pos εpos') with ⟨z, zs, dyz⟩,
-- z : α, zs : z ∈ s, dyz : edist y z < ↑ε / 2
calc inf_edist x s ≤ edist x z : inf_edist_le_edist_of_mem zs
... ≤ edist x y + edist y z : edist_triangle _ _ _
... ≤ (inf_edist x (closure s) + ε / 2) + (ε/2) : add_le_add' (le_of_lt hy) (le_of_lt dyz)
... = inf_edist x (closure s) + ↑ε : by simp [ennreal.add_halves]
end
/-- A point belongs to the closure of `s` iff its infimum edistance to this set vanishes -/
lemma mem_closure_iff_inf_edist_zero : x ∈ closure s ↔ inf_edist x s = 0 :=
⟨λh, by rw ← inf_edist_closure; exact inf_edist_zero_of_mem h,
λh, emetric.mem_closure_iff'.2 $ λε εpos, exists_edist_lt_of_inf_edist_lt (by rwa h)⟩
/-- Given a closed set `s`, a point belongs to `s` iff its infimum edistance to this set vanishes -/
lemma mem_iff_ind_edist_zero_of_closed (h : is_closed s) : x ∈ s ↔ inf_edist x s = 0 :=
begin
convert ← mem_closure_iff_inf_edist_zero,
exact closure_eq_iff_is_closed.2 h
end
/-- The infimum edistance is invariant under isometries -/
lemma inf_edist_image (hΦ : isometry Φ) :
inf_edist (Φ x) (Φ '' t) = inf_edist x t :=
begin
simp only [inf_edist],
apply congr_arg,
ext b, split,
{ assume hb,
rcases (mem_image _ _ _).1 hb with ⟨y, ⟨hy, hy'⟩⟩,
rcases (mem_image _ _ _).1 hy with ⟨z, ⟨hz, hz'⟩⟩,
rw [← hy', ← hz', hΦ x z],
exact mem_image_of_mem _ hz },
{ assume hb,
rcases (mem_image _ _ _).1 hb with ⟨y, ⟨hy, hy'⟩⟩,
rw [← hy', ← hΦ x y],
exact mem_image_of_mem _ (mem_image_of_mem _ hy) }
end
end inf_edist --section
/-- The Hausdorff edistance between two sets is the smallest `r` such that each set
is contained in the `r`-neighborhood of the other one -/
def Hausdorff_edist {α : Type u} [emetric_space α] (s t : set α) : ennreal :=
Sup ((λx, inf_edist x t) '' s) ⊔ Sup ((λx, inf_edist x s) '' t)
lemma Hausdorff_edist_def {α : Type u} [emetric_space α] (s t : set α) :
Hausdorff_edist s t = Sup ((λx, inf_edist x t) '' s) ⊔ Sup ((λx, inf_edist x s) '' t) := rfl
attribute [irreducible] Hausdorff_edist
section Hausdorff_edist
open_locale ennreal
variables {α : Type u} {β : Type v} [emetric_space α] [emetric_space β]
{x y : α} {s t u : set α} {Φ : α → β}
/-- The Hausdorff edistance of a set to itself vanishes -/
@[simp] lemma Hausdorff_edist_self : Hausdorff_edist s s = 0 :=
begin
erw [Hausdorff_edist_def, lattice.sup_idem, ← le_bot_iff],
apply Sup_le _,
simp [le_bot_iff, inf_edist_zero_of_mem] {contextual := tt},
end
/-- The Haudorff edistances of `s` to `t` and of `t` to `s` coincide -/
lemma Hausdorff_edist_comm : Hausdorff_edist s t = Hausdorff_edist t s :=
by unfold Hausdorff_edist; apply sup_comm
/-- Bounding the Hausdorff edistance by bounding the edistance of any point
in each set to the other set -/
lemma Hausdorff_edist_le_of_inf_edist {r : ennreal}
(H1 : ∀x ∈ s, inf_edist x t ≤ r) (H2 : ∀x ∈ t, inf_edist x s ≤ r) :
Hausdorff_edist s t ≤ r :=
begin
simp only [Hausdorff_edist, -mem_image, set.ball_image_iff, lattice.Sup_le_iff, lattice.sup_le_iff],
exact ⟨H1, H2⟩
end
/-- Bounding the Hausdorff edistance by exhibiting, for any point in each set,
another point in the other set at controlled distance -/
lemma Hausdorff_edist_le_of_mem_edist {r : ennreal}
(H1 : ∀x ∈ s, ∃y ∈ t, edist x y ≤ r) (H2 : ∀x ∈ t, ∃y ∈ s, edist x y ≤ r) :
Hausdorff_edist s t ≤ r :=
begin
refine Hausdorff_edist_le_of_inf_edist _ _,
{ assume x xs,
rcases H1 x xs with ⟨y, yt, hy⟩,
exact le_trans (inf_edist_le_edist_of_mem yt) hy },
{ assume x xt,
rcases H2 x xt with ⟨y, ys, hy⟩,
exact le_trans (inf_edist_le_edist_of_mem ys) hy }
end
/-- The distance to a set is controlled by the Hausdorff distance -/
lemma inf_edist_le_Hausdorff_edist_of_mem (h : x ∈ s) : inf_edist x t ≤ Hausdorff_edist s t :=
begin
rw Hausdorff_edist_def,
refine le_trans (le_Sup _) le_sup_left,
exact mem_image_of_mem _ h
end
/-- If the Hausdorff distance is `<r`, then any point in one of the sets has
a corresponding point at distance `<r` in the other set -/
lemma exists_edist_lt_of_Hausdorff_edist_lt {r : ennreal} (h : x ∈ s) (H : Hausdorff_edist s t < r) :
∃y∈t, edist x y < r :=
exists_edist_lt_of_inf_edist_lt $ calc
inf_edist x t ≤ Sup ((λx, inf_edist x t) '' s) : le_Sup (mem_image_of_mem _ h)
... ≤ Sup ((λx, inf_edist x t) '' s) ⊔ Sup ((λx, inf_edist x s) '' t) : le_sup_left
... < r : by rwa Hausdorff_edist_def at H
/-- The distance from `x` to `s`or `t` is controlled in terms of the Hausdorff distance
between `s` and `t` -/
lemma inf_edist_le_inf_edist_add_Hausdorff_edist :
inf_edist x t ≤ inf_edist x s + Hausdorff_edist s t :=
ennreal.le_of_forall_epsilon_le $ λε εpos h, begin
have εpos' : (0 : ennreal) < ε := by simpa,
have : inf_edist x s < inf_edist x s + ε/2 :=
ennreal.lt_add_right (ennreal.add_lt_top.1 h).1 (ennreal.half_pos εpos'),
rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ys, dxy⟩,
-- y : α, ys : y ∈ s, dxy : edist x y < inf_edist x s + ↑ε / 2
have : Hausdorff_edist s t < Hausdorff_edist s t + ε/2 :=
ennreal.lt_add_right (ennreal.add_lt_top.1 h).2 (ennreal.half_pos εpos'),
rcases exists_edist_lt_of_Hausdorff_edist_lt ys this with ⟨z, zt, dyz⟩,
-- z : α, zt : z ∈ t, dyz : edist y z < Hausdorff_edist s t + ↑ε / 2
calc inf_edist x t ≤ edist x z : inf_edist_le_edist_of_mem zt
... ≤ edist x y + edist y z : edist_triangle _ _ _
... ≤ (inf_edist x s + ε/2) + (Hausdorff_edist s t + ε/2) : add_le_add' (le_of_lt dxy) (le_of_lt dyz)
... = inf_edist x s + Hausdorff_edist s t + ε : by simp [ennreal.add_halves, add_comm]
end
/-- The Hausdorff edistance is invariant under eisometries -/
lemma Hausdorff_edist_image (h : isometry Φ) :
Hausdorff_edist (Φ '' s) (Φ '' t) = Hausdorff_edist s t :=
begin
unfold Hausdorff_edist,
congr,
{ ext b,
split,
{ assume hb,
rcases (mem_image _ _ _ ).1 hb with ⟨y, ⟨hy, hy'⟩⟩,
rcases (mem_image _ _ _ ).1 hy with ⟨z, ⟨hz, hz'⟩⟩,
rw [← hy', ← hz', inf_edist_image h],
exact mem_image_of_mem _ hz },
{ assume hb,
rcases (mem_image _ _ _ ).1 hb with ⟨y, ⟨hy, hy'⟩⟩,
rw [← hy', ← inf_edist_image h],
exact mem_image_of_mem _ (mem_image_of_mem _ hy) }},
{ ext b,
split,
{ assume hb,
rcases (mem_image _ _ _ ).1 hb with ⟨y, ⟨hy, hy'⟩⟩,
rcases (mem_image _ _ _ ).1 hy with ⟨z, ⟨hz, hz'⟩⟩,
rw [← hy', ← hz', inf_edist_image h],
exact mem_image_of_mem _ hz },
{ assume hb,
rcases (mem_image _ _ _ ).1 hb with ⟨y, ⟨hy, hy'⟩⟩,
rw [← hy', ← inf_edist_image h],
exact mem_image_of_mem _ (mem_image_of_mem _ hy) }}
end
/-- The Hausdorff distance is controlled by the diameter of the union -/
lemma Hausdorff_edist_le_ediam (hs : s.nonempty) (ht : t.nonempty) : Hausdorff_edist s t ≤ diam (s ∪ t) :=
begin
rcases hs with ⟨x, xs⟩,
rcases ht with ⟨y, yt⟩,
refine Hausdorff_edist_le_of_mem_edist _ _,
{ exact λz hz, ⟨y, yt, edist_le_diam_of_mem (subset_union_left _ _ hz) (subset_union_right _ _ yt)⟩ },
{ exact λz hz, ⟨x, xs, edist_le_diam_of_mem (subset_union_right _ _ hz) (subset_union_left _ _ xs)⟩ }
end
/-- The Hausdorff distance satisfies the triangular inequality -/
lemma Hausdorff_edist_triangle : Hausdorff_edist s u ≤ Hausdorff_edist s t + Hausdorff_edist t u :=
begin
rw Hausdorff_edist_def,
simp only [and_imp, set.mem_image, lattice.Sup_le_iff, exists_imp_distrib,
lattice.sup_le_iff, -mem_image, set.ball_image_iff],
split,
show ∀x ∈ s, inf_edist x u ≤ Hausdorff_edist s t + Hausdorff_edist t u, from λx xs, calc
inf_edist x u ≤ inf_edist x t + Hausdorff_edist t u : inf_edist_le_inf_edist_add_Hausdorff_edist
... ≤ Hausdorff_edist s t + Hausdorff_edist t u :
add_le_add_right' (inf_edist_le_Hausdorff_edist_of_mem xs),
show ∀x ∈ u, inf_edist x s ≤ Hausdorff_edist s t + Hausdorff_edist t u, from λx xu, calc
inf_edist x s ≤ inf_edist x t + Hausdorff_edist t s : inf_edist_le_inf_edist_add_Hausdorff_edist
... ≤ Hausdorff_edist u t + Hausdorff_edist t s :
add_le_add_right' (inf_edist_le_Hausdorff_edist_of_mem xu)
... = Hausdorff_edist s t + Hausdorff_edist t u : by simp [Hausdorff_edist_comm, add_comm]
end
/-- The Hausdorff edistance between a set and its closure vanishes -/
@[simp] lemma Hausdorff_edist_self_closure : Hausdorff_edist s (closure s) = 0 :=
begin
erw ← le_bot_iff,
simp only [Hausdorff_edist, inf_edist_closure, -le_zero_iff_eq, and_imp,
set.mem_image, lattice.Sup_le_iff, exists_imp_distrib, lattice.sup_le_iff,
set.ball_image_iff, ennreal.bot_eq_zero, -mem_image],
simp only [inf_edist_zero_of_mem, mem_closure_iff_inf_edist_zero, le_refl, and_self,
forall_true_iff] {contextual := tt}
end
/-- Replacing a set by its closure does not change the Hausdorff edistance. -/
@[simp] lemma Hausdorff_edist_closure₁ : Hausdorff_edist (closure s) t = Hausdorff_edist s t :=
begin
refine le_antisymm _ _,
{ calc _ ≤ Hausdorff_edist (closure s) s + Hausdorff_edist s t : Hausdorff_edist_triangle
... = Hausdorff_edist s t : by simp [Hausdorff_edist_comm] },
{ calc _ ≤ Hausdorff_edist s (closure s) + Hausdorff_edist (closure s) t : Hausdorff_edist_triangle
... = Hausdorff_edist (closure s) t : by simp }
end
/-- Replacing a set by its closure does not change the Hausdorff edistance. -/
@[simp] lemma Hausdorff_edist_closure₂ : Hausdorff_edist s (closure t) = Hausdorff_edist s t :=
by simp [@Hausdorff_edist_comm _ _ s _]
/-- The Hausdorff edistance between sets or their closures is the same -/
@[simp] lemma Hausdorff_edist_closure : Hausdorff_edist (closure s) (closure t) = Hausdorff_edist s t :=
by simp
/-- Two sets are at zero Hausdorff edistance if and only if they have the same closure -/
lemma Hausdorff_edist_zero_iff_closure_eq_closure : Hausdorff_edist s t = 0 ↔ closure s = closure t :=
⟨begin
assume h,
refine subset.antisymm _ _,
{ have : s ⊆ closure t := λx xs, mem_closure_iff_inf_edist_zero.2 $ begin
erw ← le_bot_iff,
have := @inf_edist_le_Hausdorff_edist_of_mem _ _ _ _ t xs,
rwa h at this,
end,
by rw ← @closure_closure _ _ t; exact closure_mono this },
{ have : t ⊆ closure s := λx xt, mem_closure_iff_inf_edist_zero.2 $ begin
erw ← le_bot_iff,
have := @inf_edist_le_Hausdorff_edist_of_mem _ _ _ _ s xt,
rw Hausdorff_edist_comm at h,
rwa h at this,
end,
by rw ← @closure_closure _ _ s; exact closure_mono this }
end,
λh, by rw [← Hausdorff_edist_closure, h, Hausdorff_edist_self]⟩
/-- Two closed sets are at zero Hausdorff edistance if and only if they coincide -/
lemma Hausdorff_edist_zero_iff_eq_of_closed (hs : is_closed s) (ht : is_closed t) :
Hausdorff_edist s t = 0 ↔ s = t :=
by rw [Hausdorff_edist_zero_iff_closure_eq_closure, closure_eq_iff_is_closed.2 hs,
closure_eq_iff_is_closed.2 ht]
/-- The Haudorff edistance to the empty set is infinite -/
lemma Hausdorff_edist_empty (ne : s.nonempty) : Hausdorff_edist s ∅ = ∞ :=
begin
rcases ne with ⟨x, xs⟩,
have : inf_edist x ∅ ≤ Hausdorff_edist s ∅ := inf_edist_le_Hausdorff_edist_of_mem xs,
simpa using this,
end
/-- If a set is at finite Hausdorff edistance of a nonempty set, it is nonempty -/
lemma nonempty_of_Hausdorff_edist_ne_top (hs : s.nonempty) (fin : Hausdorff_edist s t ≠ ⊤) :
t.nonempty :=
t.eq_empty_or_nonempty.elim (λ ht, (fin $ ht.symm ▸ Hausdorff_edist_empty hs).elim) id
lemma empty_or_nonempty_of_Hausdorff_edist_ne_top (fin : Hausdorff_edist s t ≠ ⊤) :
s = ∅ ∧ t = ∅ ∨ s.nonempty ∧ t.nonempty :=
begin
cases s.eq_empty_or_nonempty with hs hs,
{ cases t.eq_empty_or_nonempty with ht ht,
{ exact or.inl ⟨hs, ht⟩ },
{ rw Hausdorff_edist_comm at fin,
exact or.inr ⟨nonempty_of_Hausdorff_edist_ne_top ht fin, ht⟩ } },
{ exact or.inr ⟨hs, nonempty_of_Hausdorff_edist_ne_top hs fin⟩ }
end
end Hausdorff_edist -- section
end emetric --namespace
/-Now, we turn to the same notions in metric spaces. To avoid the difficulties related to
Inf and Sup on ℝ (which is only conditionnally complete), we use the notions in ennreal formulated
in terms of the edistance, and coerce them to ℝ. Then their properties follow readily from the
corresponding properties in ennreal, modulo some tedious rewriting of inequalities from one to the
other -/
namespace metric
section
variables {α : Type u} {β : Type v} [metric_space α] [metric_space β] {s t u : set α} {x y : α} {Φ : α → β}
open emetric
/-- The minimal distance of a point to a set -/
def inf_dist (x : α) (s : set α) : ℝ := ennreal.to_real (inf_edist x s)
/-- the minimal distance is always nonnegative -/
lemma inf_dist_nonneg : 0 ≤ inf_dist x s := by simp [inf_dist]
/-- the minimal distance to the empty set is 0 (if you want to have the more reasonable
value ∞ instead, use `inf_edist`, which takes values in ennreal) -/
@[simp] lemma inf_dist_empty : inf_dist x ∅ = 0 :=
by simp [inf_dist]
/-- In a metric space, the minimal edistance to a nonempty set is finite -/
lemma inf_edist_ne_top (h : s.nonempty) : inf_edist x s ≠ ⊤ :=
begin
rcases h with ⟨y, hy⟩,
apply lt_top_iff_ne_top.1,
calc inf_edist x s ≤ edist x y : inf_edist_le_edist_of_mem hy
... < ⊤ : lt_top_iff_ne_top.2 (edist_ne_top _ _)
end
/-- The minimal distance of a point to a set containing it vanishes -/
lemma inf_dist_zero_of_mem (h : x ∈ s) : inf_dist x s = 0 :=
by simp [inf_edist_zero_of_mem h, inf_dist]
/-- The minimal distance to a singleton is the distance to the unique point in this singleton -/
@[simp] lemma inf_dist_singleton : inf_dist x {y} = dist x y :=
by simp [inf_dist, inf_edist, dist_edist]
/-- The minimal distance to a set is bounded by the distance to any point in this set -/
lemma inf_dist_le_dist_of_mem (h : y ∈ s) : inf_dist x s ≤ dist x y :=
begin
rw [dist_edist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ⟨_, h⟩) (edist_ne_top _ _)],
exact inf_edist_le_edist_of_mem h
end
/-- The minimal distance is monotonous with respect to inclusion -/
lemma inf_dist_le_inf_dist_of_subset (h : s ⊆ t) (hs : s.nonempty) :
inf_dist x t ≤ inf_dist x s :=
begin
have ht : t.nonempty := hs.mono h,
rw [inf_dist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ht) (inf_edist_ne_top hs)],
exact inf_edist_le_inf_edist_of_subset h
end
/-- If the minimal distance to a set is `<r`, there exists a point in this set at distance `<r` -/
lemma exists_dist_lt_of_inf_dist_lt {r : real} (h : inf_dist x s < r) (hs : s.nonempty) :
∃y∈s, dist x y < r :=
begin
have rpos : 0 < r := lt_of_le_of_lt inf_dist_nonneg h,
have : inf_edist x s < ennreal.of_real r,
{ rwa [inf_dist, ← ennreal.to_real_of_real (le_of_lt rpos), ennreal.to_real_lt_to_real (inf_edist_ne_top hs)] at h,
simp },
rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ys, hy⟩,
rw [edist_dist, ennreal.of_real_lt_of_real_iff rpos] at hy,
exact ⟨y, ys, hy⟩,
end
/-- The minimal distance from `x` to `s` is bounded by the distance from `y` to `s`, modulo
the distance between `x` and `y` -/
lemma inf_dist_le_inf_dist_add_dist : inf_dist x s ≤ inf_dist y s + dist x y :=
begin
cases s.eq_empty_or_nonempty with hs hs,
{ by simp [hs, dist_nonneg] },
{ rw [inf_dist, inf_dist, dist_edist, ← ennreal.to_real_add (inf_edist_ne_top hs) (edist_ne_top _ _),
ennreal.to_real_le_to_real (inf_edist_ne_top hs)],
{ apply inf_edist_le_inf_edist_add_edist },
{ simp [ennreal.add_eq_top, inf_edist_ne_top hs, edist_ne_top] }}
end
variable (s)
/-- The minimal distance to a set is Lipschitz in point with constant 1 -/
lemma lipschitz_inf_dist_pt : lipschitz_with 1 (λx, inf_dist x s) :=
lipschitz_with.one_of_le_add $ λ x y, inf_dist_le_inf_dist_add_dist
/-- The minimal distance to a set is uniformly continuous in point -/
lemma uniform_continuous_inf_dist_pt :
uniform_continuous (λx, inf_dist x s) :=
(lipschitz_inf_dist_pt s).to_uniform_continuous
/-- The minimal distance to a set is continuous in point -/
lemma continuous_inf_dist_pt : continuous (λx, inf_dist x s) :=
(uniform_continuous_inf_dist_pt s).continuous
variable {s}
/-- The minimal distance to a set and its closure coincide -/
lemma inf_dist_eq_closure : inf_dist x (closure s) = inf_dist x s :=
by simp [inf_dist, inf_edist_closure]
/-- A point belongs to the closure of `s` iff its infimum distance to this set vanishes -/
lemma mem_closure_iff_inf_dist_zero (h : s.nonempty) : x ∈ closure s ↔ inf_dist x s = 0 :=
by simp [mem_closure_iff_inf_edist_zero, inf_dist, ennreal.to_real_eq_zero_iff, inf_edist_ne_top h]
/-- Given a closed set `s`, a point belongs to `s` iff its infimum distance to this set vanishes -/
lemma mem_iff_inf_dist_zero_of_closed (h : is_closed s) (hs : s.nonempty) :
x ∈ s ↔ inf_dist x s = 0 :=
begin
have := @mem_closure_iff_inf_dist_zero _ _ s x hs,
rwa closure_eq_iff_is_closed.2 h at this
end
/-- The infimum distance is invariant under isometries -/
lemma inf_dist_image (hΦ : isometry Φ) :
inf_dist (Φ x) (Φ '' t) = inf_dist x t :=
by simp [inf_dist, inf_edist_image hΦ]
/-- The Hausdorff distance between two sets is the smallest nonnegative `r` such that each set is
included in the `r`-neighborhood of the other. If there is no such `r`, it is defined to
be `0`, arbitrarily -/
def Hausdorff_dist (s t : set α) : ℝ := ennreal.to_real (Hausdorff_edist s t)
/-- The Hausdorff distance is nonnegative -/
lemma Hausdorff_dist_nonneg : 0 ≤ Hausdorff_dist s t :=
by simp [Hausdorff_dist]
/-- If two sets are nonempty and bounded in a metric space, they are at finite Hausdorff edistance -/
lemma Hausdorff_edist_ne_top_of_nonempty_of_bounded (hs : s.nonempty) (ht : t.nonempty)
(bs : bounded s) (bt : bounded t) : Hausdorff_edist s t ≠ ⊤ :=
begin
rcases hs with ⟨cs, hcs⟩,
rcases ht with ⟨ct, hct⟩,
rcases (bounded_iff_subset_ball ct).1 bs with ⟨rs, hrs⟩,
rcases (bounded_iff_subset_ball cs).1 bt with ⟨rt, hrt⟩,
have : Hausdorff_edist s t ≤ ennreal.of_real (max rs rt),
{ apply Hausdorff_edist_le_of_mem_edist,
{ assume x xs,
existsi [ct, hct],
have : dist x ct ≤ max rs rt := le_trans (hrs xs) (le_max_left _ _),
rwa [edist_dist, ennreal.of_real_le_of_real_iff],
exact le_trans dist_nonneg this },
{ assume x xt,
existsi [cs, hcs],
have : dist x cs ≤ max rs rt := le_trans (hrt xt) (le_max_right _ _),
rwa [edist_dist, ennreal.of_real_le_of_real_iff],
exact le_trans dist_nonneg this }},
exact ennreal.lt_top_iff_ne_top.1 (lt_of_le_of_lt this (by simp [lt_top_iff_ne_top]))
end
/-- The Hausdorff distance between a set and itself is zero -/
@[simp] lemma Hausdorff_dist_self_zero : Hausdorff_dist s s = 0 :=
by simp [Hausdorff_dist]
/-- The Hausdorff distance from `s` to `t` and from `t` to `s` coincide -/
lemma Hausdorff_dist_comm : Hausdorff_dist s t = Hausdorff_dist t s :=
by simp [Hausdorff_dist, Hausdorff_edist_comm]
/-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable
value ∞ instead, use `Hausdorff_edist`, which takes values in ennreal) -/
@[simp] lemma Hausdorff_dist_empty : Hausdorff_dist s ∅ = 0 :=
begin
cases s.eq_empty_or_nonempty with h h,
{ simp [h] },
{ simp [Hausdorff_dist, Hausdorff_edist_empty h] }
end
/-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable
value ∞ instead, use `Hausdorff_edist`, which takes values in ennreal) -/
@[simp] lemma Hausdorff_dist_empty' : Hausdorff_dist ∅ s = 0 :=
by simp [Hausdorff_dist_comm]
/-- Bounding the Hausdorff distance by bounding the distance of any point
in each set to the other set -/
lemma Hausdorff_dist_le_of_inf_dist {r : ℝ} (hr : r ≥ 0)
(H1 : ∀x ∈ s, inf_dist x t ≤ r) (H2 : ∀x ∈ t, inf_dist x s ≤ r) :
Hausdorff_dist s t ≤ r :=
begin
by_cases h1 : Hausdorff_edist s t = ⊤,
by rwa [Hausdorff_dist, h1, ennreal.top_to_real],
cases s.eq_empty_or_nonempty with hs hs,
by rwa [hs, Hausdorff_dist_empty'],
cases t.eq_empty_or_nonempty with ht ht,
by rwa [ht, Hausdorff_dist_empty],
have : Hausdorff_edist s t ≤ ennreal.of_real r,
{ apply Hausdorff_edist_le_of_inf_edist _ _,
{ assume x hx,
have I := H1 x hx,
rwa [inf_dist, ← ennreal.to_real_of_real hr,
ennreal.to_real_le_to_real (inf_edist_ne_top ht) ennreal.of_real_ne_top] at I },
{ assume x hx,
have I := H2 x hx,
rwa [inf_dist, ← ennreal.to_real_of_real hr,
ennreal.to_real_le_to_real (inf_edist_ne_top hs) ennreal.of_real_ne_top] at I }},
rwa [Hausdorff_dist, ← ennreal.to_real_of_real hr,
ennreal.to_real_le_to_real h1 ennreal.of_real_ne_top]
end
/-- Bounding the Hausdorff distance by exhibiting, for any point in each set,
another point in the other set at controlled distance -/
lemma Hausdorff_dist_le_of_mem_dist {r : ℝ} (hr : 0 ≤ r)
(H1 : ∀x ∈ s, ∃y ∈ t, dist x y ≤ r) (H2 : ∀x ∈ t, ∃y ∈ s, dist x y ≤ r) :
Hausdorff_dist s t ≤ r :=
begin
apply Hausdorff_dist_le_of_inf_dist hr,
{ assume x xs,
rcases H1 x xs with ⟨y, yt, hy⟩,
exact le_trans (inf_dist_le_dist_of_mem yt) hy },
{ assume x xt,
rcases H2 x xt with ⟨y, ys, hy⟩,
exact le_trans (inf_dist_le_dist_of_mem ys) hy }
end
/-- The Hausdorff distance is controlled by the diameter of the union -/
lemma Hausdorff_dist_le_diam (hs : s.nonempty) (bs : bounded s) (ht : t.nonempty) (bt : bounded t) :
Hausdorff_dist s t ≤ diam (s ∪ t) :=
begin
rcases hs with ⟨x, xs⟩,
rcases ht with ⟨y, yt⟩,
refine Hausdorff_dist_le_of_mem_dist diam_nonneg _ _,
{ exact λz hz, ⟨y, yt, dist_le_diam_of_mem (bounded_union.2 ⟨bs, bt⟩)
(subset_union_left _ _ hz) (subset_union_right _ _ yt)⟩ },
{ exact λz hz, ⟨x, xs, dist_le_diam_of_mem (bounded_union.2 ⟨bs, bt⟩)
(subset_union_right _ _ hz) (subset_union_left _ _ xs)⟩ }
end
/-- The distance to a set is controlled by the Hausdorff distance -/
lemma inf_dist_le_Hausdorff_dist_of_mem (hx : x ∈ s) (fin : Hausdorff_edist s t ≠ ⊤) :
inf_dist x t ≤ Hausdorff_dist s t :=
begin
have ht : t.nonempty := nonempty_of_Hausdorff_edist_ne_top ⟨x, hx⟩ fin,
rw [Hausdorff_dist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ht) fin],
exact inf_edist_le_Hausdorff_edist_of_mem hx
end
/-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance
`<r` of a point in the other set -/
lemma exists_dist_lt_of_Hausdorff_dist_lt {r : ℝ} (h : x ∈ s) (H : Hausdorff_dist s t < r)
(fin : Hausdorff_edist s t ≠ ⊤) : ∃y∈t, dist x y < r :=
begin
have r0 : 0 < r := lt_of_le_of_lt (Hausdorff_dist_nonneg) H,
have : Hausdorff_edist s t < ennreal.of_real r,
by rwa [Hausdorff_dist, ← ennreal.to_real_of_real (le_of_lt r0),
ennreal.to_real_lt_to_real fin (ennreal.of_real_ne_top)] at H,
rcases exists_edist_lt_of_Hausdorff_edist_lt h this with ⟨y, hy, yr⟩,
rw [edist_dist, ennreal.of_real_lt_of_real_iff r0] at yr,
exact ⟨y, hy, yr⟩
end
/-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance
`<r` of a point in the other set -/
lemma exists_dist_lt_of_Hausdorff_dist_lt' {r : ℝ} (h : y ∈ t) (H : Hausdorff_dist s t < r)
(fin : Hausdorff_edist s t ≠ ⊤) : ∃x∈s, dist x y < r :=
begin
rw Hausdorff_dist_comm at H,
rw Hausdorff_edist_comm at fin,
simpa [dist_comm] using exists_dist_lt_of_Hausdorff_dist_lt h H fin
end
/-- The infimum distance to `s` and `t` are the same, up to the Hausdorff distance
between `s` and `t` -/
lemma inf_dist_le_inf_dist_add_Hausdorff_dist (fin : Hausdorff_edist s t ≠ ⊤) :
inf_dist x t ≤ inf_dist x s + Hausdorff_dist s t :=
begin
rcases empty_or_nonempty_of_Hausdorff_edist_ne_top fin with ⟨hs,ht⟩|⟨hs,ht⟩,
{ simp only [hs, ht, Hausdorff_dist_empty, inf_dist_empty, zero_add] },
rw [inf_dist, inf_dist, Hausdorff_dist, ← ennreal.to_real_add (inf_edist_ne_top hs) fin,
ennreal.to_real_le_to_real (inf_edist_ne_top ht)],
{ exact inf_edist_le_inf_edist_add_Hausdorff_edist },
{ exact ennreal.add_ne_top.2 ⟨inf_edist_ne_top hs, fin⟩ }
end
/-- The Hausdorff distance is invariant under isometries -/
lemma Hausdorff_dist_image (h : isometry Φ) :
Hausdorff_dist (Φ '' s) (Φ '' t) = Hausdorff_dist s t :=
by simp [Hausdorff_dist, Hausdorff_edist_image h]
/-- The Hausdorff distance satisfies the triangular inequality -/
lemma Hausdorff_dist_triangle (fin : Hausdorff_edist s t ≠ ⊤) :
Hausdorff_dist s u ≤ Hausdorff_dist s t + Hausdorff_dist t u :=
begin
by_cases Hausdorff_edist s u = ⊤,
{ calc Hausdorff_dist s u = 0 + 0 : by simp [Hausdorff_dist, h]
... ≤ Hausdorff_dist s t + Hausdorff_dist t u :
add_le_add (Hausdorff_dist_nonneg) (Hausdorff_dist_nonneg) },
{ have Dtu : Hausdorff_edist t u < ⊤ := calc
Hausdorff_edist t u ≤ Hausdorff_edist t s + Hausdorff_edist s u : Hausdorff_edist_triangle
... = Hausdorff_edist s t + Hausdorff_edist s u : by simp [Hausdorff_edist_comm]
... < ⊤ : by simp [ennreal.add_lt_top]; simp [ennreal.lt_top_iff_ne_top, h, fin],
rw [Hausdorff_dist, Hausdorff_dist, Hausdorff_dist,
← ennreal.to_real_add fin (lt_top_iff_ne_top.1 Dtu), ennreal.to_real_le_to_real h],
{ exact Hausdorff_edist_triangle },
{ simp [ennreal.add_eq_top, lt_top_iff_ne_top.1 Dtu, fin] }}
end
/-- The Hausdorff distance satisfies the triangular inequality -/
lemma Hausdorff_dist_triangle' (fin : Hausdorff_edist t u ≠ ⊤) :
Hausdorff_dist s u ≤ Hausdorff_dist s t + Hausdorff_dist t u :=
begin
rw Hausdorff_edist_comm at fin,
have I : Hausdorff_dist u s ≤ Hausdorff_dist u t + Hausdorff_dist t s := Hausdorff_dist_triangle fin,
simpa [add_comm, Hausdorff_dist_comm] using I
end
/-- The Hausdorff distance between a set and its closure vanish -/
@[simp] lemma Hausdorff_dist_self_closure : Hausdorff_dist s (closure s) = 0 :=
by simp [Hausdorff_dist]
/-- Replacing a set by its closure does not change the Hausdorff distance. -/
@[simp] lemma Hausdorff_dist_closure₁ : Hausdorff_dist (closure s) t = Hausdorff_dist s t :=
by simp [Hausdorff_dist]
/-- Replacing a set by its closure does not change the Hausdorff distance. -/
@[simp] lemma Hausdorff_dist_closure₂ : Hausdorff_dist s (closure t) = Hausdorff_dist s t :=
by simp [Hausdorff_dist]
/-- The Hausdorff distance between two sets and their closures coincide -/
@[simp] lemma Hausdorff_dist_closure : Hausdorff_dist (closure s) (closure t) = Hausdorff_dist s t :=
by simp [Hausdorff_dist]
/-- Two sets are at zero Hausdorff distance if and only if they have the same closures -/
lemma Hausdorff_dist_zero_iff_closure_eq_closure (fin : Hausdorff_edist s t ≠ ⊤) :
Hausdorff_dist s t = 0 ↔ closure s = closure t :=
by simp [Hausdorff_edist_zero_iff_closure_eq_closure.symm, Hausdorff_dist,
ennreal.to_real_eq_zero_iff, fin]
/-- Two closed sets are at zero Hausdorff distance if and only if they coincide -/
lemma Hausdorff_dist_zero_iff_eq_of_closed (hs : is_closed s) (ht : is_closed t)
(fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s t = 0 ↔ s = t :=
by simp [(Hausdorff_edist_zero_iff_eq_of_closed hs ht).symm, Hausdorff_dist,
ennreal.to_real_eq_zero_iff, fin]
end --section
end metric --namespace
|
dcdff7afbc2e260cd8e1cfce51410becc52c99c1 | fecda8e6b848337561d6467a1e30cf23176d6ad0 | /src/order/filter/at_top_bot.lean | c75ab4a95bae3a8a8929b2e8e165d9b3f4999cfc | [
"Apache-2.0"
] | permissive | spolu/mathlib | bacf18c3d2a561d00ecdc9413187729dd1f705ed | 480c92cdfe1cf3c2d083abded87e82162e8814f4 | refs/heads/master | 1,671,684,094,325 | 1,600,736,045,000 | 1,600,736,045,000 | 297,564,749 | 1 | 0 | null | 1,600,758,368,000 | 1,600,758,367,000 | null | UTF-8 | Lean | false | false | 41,678 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Jeremy Avigad, Yury Kudryashov, Patrick Massot
-/
import order.filter.bases
/-!
# `at_top` and `at_bot` filters on preorded sets, monoids and groups.
In this file we define the filters
* `at_top`: corresponds to `n → +∞`;
* `at_bot`: corresponds to `n → -∞`.
Then we prove many lemmas like “if `f → +∞`, then `f ± c → +∞`”.
-/
variables {ι ι' α β γ : Type*}
open set
open_locale classical filter big_operators
namespace filter
/-- `at_top` is the filter representing the limit `→ ∞` on an ordered set.
It is generated by the collection of up-sets `{b | a ≤ b}`.
(The preorder need not have a top element for this to be well defined,
and indeed is trivial when a top element exists.) -/
def at_top [preorder α] : filter α := ⨅ a, 𝓟 {b | a ≤ b}
/-- `at_bot` is the filter representing the limit `→ -∞` on an ordered set.
It is generated by the collection of down-sets `{b | b ≤ a}`.
(The preorder need not have a bottom element for this to be well defined,
and indeed is trivial when a bottom element exists.) -/
def at_bot [preorder α] : filter α := ⨅ a, 𝓟 {b | b ≤ a}
lemma mem_at_top [preorder α] (a : α) : {b : α | a ≤ b} ∈ @at_top α _ :=
mem_infi_sets a $ subset.refl _
lemma Ioi_mem_at_top [preorder α] [no_top_order α] (x : α) : Ioi x ∈ (at_top : filter α) :=
let ⟨z, hz⟩ := no_top x in mem_sets_of_superset (mem_at_top z) $ λ y h, lt_of_lt_of_le hz h
lemma mem_at_bot [preorder α] (a : α) : {b : α | b ≤ a} ∈ @at_bot α _ :=
mem_infi_sets a $ subset.refl _
lemma Iio_mem_at_bot [preorder α] [no_bot_order α] (x : α) : Iio x ∈ (at_bot : filter α) :=
let ⟨z, hz⟩ := no_bot x in mem_sets_of_superset (mem_at_bot z) $ λ y h, lt_of_le_of_lt h hz
lemma at_top_basis [nonempty α] [semilattice_sup α] :
(@at_top α _).has_basis (λ _, true) Ici :=
has_basis_infi_principal (directed_of_sup $ λ a b, Ici_subset_Ici.2)
lemma at_top_basis' [semilattice_sup α] (a : α) :
(@at_top α _).has_basis (λ x, a ≤ x) Ici :=
⟨λ t, (@at_top_basis α ⟨a⟩ _).mem_iff.trans
⟨λ ⟨x, _, hx⟩, ⟨x ⊔ a, le_sup_right, λ y hy, hx (le_trans le_sup_left hy)⟩,
λ ⟨x, _, hx⟩, ⟨x, trivial, hx⟩⟩⟩
lemma at_bot_basis [nonempty α] [semilattice_inf α] :
(@at_bot α _).has_basis (λ _, true) Iic :=
@at_top_basis (order_dual α) _ _
lemma at_bot_basis' [semilattice_inf α] (a : α) :
(@at_bot α _).has_basis (λ x, x ≤ a) Iic :=
@at_top_basis' (order_dual α) _ _
@[instance]
lemma at_top_ne_bot [nonempty α] [semilattice_sup α] : ne_bot (at_top : filter α) :=
at_top_basis.forall_nonempty_iff_ne_bot.1 $ λ a _, nonempty_Ici
@[instance]
lemma at_bot_ne_bot [nonempty α] [semilattice_inf α] : ne_bot (at_bot : filter α) :=
@at_top_ne_bot (order_dual α) _ _
@[simp]
lemma mem_at_top_sets [nonempty α] [semilattice_sup α] {s : set α} :
s ∈ (at_top : filter α) ↔ ∃a:α, ∀b≥a, b ∈ s :=
at_top_basis.mem_iff.trans $ exists_congr $ λ _, exists_const _
@[simp]
lemma mem_at_bot_sets [nonempty α] [semilattice_inf α] {s : set α} :
s ∈ (at_bot : filter α) ↔ ∃a:α, ∀b≤a, b ∈ s :=
@mem_at_top_sets (order_dual α) _ _ _
@[simp]
lemma eventually_at_top [semilattice_sup α] [nonempty α] {p : α → Prop} :
(∀ᶠ x in at_top, p x) ↔ (∃ a, ∀ b ≥ a, p b) :=
mem_at_top_sets
@[simp]
lemma eventually_at_bot [semilattice_inf α] [nonempty α] {p : α → Prop} :
(∀ᶠ x in at_bot, p x) ↔ (∃ a, ∀ b ≤ a, p b) :=
mem_at_bot_sets
lemma eventually_ge_at_top [preorder α] (a : α) : ∀ᶠ x in at_top, a ≤ x := mem_at_top a
lemma eventually_le_at_bot [preorder α] (a : α) : ∀ᶠ x in at_bot, x ≤ a := mem_at_bot a
lemma at_top_countable_basis [nonempty α] [semilattice_sup α] [encodable α] :
has_countable_basis (at_top : filter α) (λ _, true) Ici :=
{ countable := countable_encodable _,
.. at_top_basis }
lemma at_bot_countable_basis [nonempty α] [semilattice_inf α] [encodable α] :
has_countable_basis (at_bot : filter α) (λ _, true) Iic :=
{ countable := countable_encodable _,
.. at_bot_basis }
lemma is_countably_generated_at_top [nonempty α] [semilattice_sup α] [encodable α] :
(at_top : filter $ α).is_countably_generated :=
at_top_countable_basis.is_countably_generated
lemma is_countably_generated_at_bot [nonempty α] [semilattice_inf α] [encodable α] :
(at_bot : filter $ α).is_countably_generated :=
at_bot_countable_basis.is_countably_generated
lemma order_top.at_top_eq (α) [order_top α] : (at_top : filter α) = pure ⊤ :=
le_antisymm (le_pure_iff.2 $ (eventually_ge_at_top ⊤).mono $ λ b, top_unique)
(le_infi $ λ b, le_principal_iff.2 le_top)
lemma order_bot.at_bot_eq (α) [order_bot α] : (at_bot : filter α) = pure ⊥ :=
@order_top.at_top_eq (order_dual α) _
lemma tendsto_at_top_pure [order_top α] (f : α → β) :
tendsto f at_top (pure $ f ⊤) :=
(order_top.at_top_eq α).symm ▸ tendsto_pure_pure _ _
lemma tendsto_at_bot_pure [order_bot α] (f : α → β) :
tendsto f at_bot (pure $ f ⊥) :=
@tendsto_at_top_pure (order_dual α) _ _ _
lemma eventually.exists_forall_of_at_top [semilattice_sup α] [nonempty α] {p : α → Prop}
(h : ∀ᶠ x in at_top, p x) : ∃ a, ∀ b ≥ a, p b :=
eventually_at_top.mp h
lemma eventually.exists_forall_of_at_bot [semilattice_inf α] [nonempty α] {p : α → Prop}
(h : ∀ᶠ x in at_bot, p x) : ∃ a, ∀ b ≤ a, p b :=
eventually_at_bot.mp h
lemma frequently_at_top [semilattice_sup α] [nonempty α] {p : α → Prop} :
(∃ᶠ x in at_top, p x) ↔ (∀ a, ∃ b ≥ a, p b) :=
by simp only [filter.frequently, eventually_at_top, not_exists, not_forall, not_not]
lemma frequently_at_bot [semilattice_inf α] [nonempty α] {p : α → Prop} :
(∃ᶠ x in at_bot, p x) ↔ (∀ a, ∃ b ≤ a, p b) :=
@frequently_at_top (order_dual α) _ _ _
lemma frequently_at_top' [semilattice_sup α] [nonempty α] [no_top_order α] {p : α → Prop} :
(∃ᶠ x in at_top, p x) ↔ (∀ a, ∃ b > a, p b) :=
begin
rw frequently_at_top,
split ; intros h a,
{ cases no_top a with a' ha',
rcases h a' with ⟨b, hb, hb'⟩,
exact ⟨b, lt_of_lt_of_le ha' hb, hb'⟩ },
{ rcases h a with ⟨b, hb, hb'⟩,
exact ⟨b, le_of_lt hb, hb'⟩ },
end
lemma frequently_at_bot' [semilattice_inf α] [nonempty α] [no_bot_order α] {p : α → Prop} :
(∃ᶠ x in at_bot, p x) ↔ (∀ a, ∃ b < a, p b) :=
@frequently_at_top' (order_dual α) _ _ _ _
lemma frequently.forall_exists_of_at_top [semilattice_sup α] [nonempty α] {p : α → Prop}
(h : ∃ᶠ x in at_top, p x) : ∀ a, ∃ b ≥ a, p b :=
frequently_at_top.mp h
lemma frequently.forall_exists_of_at_bot [semilattice_inf α] [nonempty α] {p : α → Prop}
(h : ∃ᶠ x in at_bot, p x) : ∀ a, ∃ b ≤ a, p b :=
frequently_at_bot.mp h
lemma map_at_top_eq [nonempty α] [semilattice_sup α] {f : α → β} :
at_top.map f = (⨅a, 𝓟 $ f '' {a' | a ≤ a'}) :=
(at_top_basis.map _).eq_infi
lemma map_at_bot_eq [nonempty α] [semilattice_inf α] {f : α → β} :
at_bot.map f = (⨅a, 𝓟 $ f '' {a' | a' ≤ a}) :=
@map_at_top_eq (order_dual α) _ _ _ _
lemma tendsto_at_top [preorder β] (m : α → β) (f : filter α) :
tendsto m f at_top ↔ (∀b, ∀ᶠ a in f, b ≤ m a) :=
by simp only [at_top, tendsto_infi, tendsto_principal, mem_set_of_eq]
lemma tendsto_at_bot [preorder β] (m : α → β) (f : filter α) :
tendsto m f at_bot ↔ (∀b, ∀ᶠ a in f, m a ≤ b) :=
@tendsto_at_top α (order_dual β) _ m f
lemma tendsto_at_top_mono' [preorder β] (l : filter α) ⦃f₁ f₂ : α → β⦄ (h : f₁ ≤ᶠ[l] f₂) :
tendsto f₁ l at_top → tendsto f₂ l at_top :=
assume h₁, (tendsto_at_top _ _).2 $ λ b, mp_sets ((tendsto_at_top _ _).1 h₁ b)
(monotone_mem_sets (λ a ha ha₁, le_trans ha₁ ha) h)
lemma tendsto_at_bot_mono' [preorder β] (l : filter α) ⦃f₁ f₂ : α → β⦄ (h : f₁ ≤ᶠ[l] f₂) :
tendsto f₂ l at_bot → tendsto f₁ l at_bot :=
@tendsto_at_top_mono' _ (order_dual β) _ _ _ _ h
lemma tendsto_at_top_mono [preorder β] {l : filter α} {f g : α → β} (h : ∀ n, f n ≤ g n) :
tendsto f l at_top → tendsto g l at_top :=
tendsto_at_top_mono' l $ eventually_of_forall h
lemma tendsto_at_bot_mono [preorder β] {l : filter α} {f g : α → β} (h : ∀ n, f n ≤ g n) :
tendsto g l at_bot → tendsto f l at_bot :=
@tendsto_at_top_mono _ (order_dual β) _ _ _ _ h
/-!
### Sequences
-/
lemma inf_map_at_top_ne_bot_iff [semilattice_sup α] [nonempty α] {F : filter β} {u : α → β} :
ne_bot (F ⊓ (map u at_top)) ↔ ∀ U ∈ F, ∀ N, ∃ n ≥ N, u n ∈ U :=
by simp_rw [inf_ne_bot_iff_frequently_left, frequently_map, frequently_at_top]; refl
lemma inf_map_at_bot_ne_bot_iff [semilattice_inf α] [nonempty α] {F : filter β} {u : α → β} :
ne_bot (F ⊓ (map u at_bot)) ↔ ∀ U ∈ F, ∀ N, ∃ n ≤ N, u n ∈ U :=
@inf_map_at_top_ne_bot_iff (order_dual α) _ _ _ _ _
lemma extraction_of_frequently_at_top' {P : ℕ → Prop} (h : ∀ N, ∃ n > N, P n) :
∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P (φ n) :=
begin
choose u hu using h,
cases forall_and_distrib.mp hu with hu hu',
exact ⟨u ∘ (nat.rec 0 (λ n v, u v)), strict_mono.nat (λ n, hu _), λ n, hu' _⟩,
end
lemma extraction_of_frequently_at_top {P : ℕ → Prop} (h : ∃ᶠ n in at_top, P n) :
∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P (φ n) :=
begin
rw frequently_at_top' at h,
exact extraction_of_frequently_at_top' h,
end
lemma extraction_of_eventually_at_top {P : ℕ → Prop} (h : ∀ᶠ n in at_top, P n) :
∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P (φ n) :=
extraction_of_frequently_at_top h.frequently
lemma exists_le_of_tendsto_at_top [semilattice_sup α] [preorder β] {u : α → β}
(h : tendsto u at_top at_top) : ∀ a b, ∃ a' ≥ a, b ≤ u a' :=
begin
intros a b,
have : ∀ᶠ x in at_top, a ≤ x ∧ b ≤ u x :=
(eventually_ge_at_top a).and (h.eventually $ eventually_ge_at_top b),
haveI : nonempty α := ⟨a⟩,
rcases this.exists with ⟨a', ha, hb⟩,
exact ⟨a', ha, hb⟩
end
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma exists_le_of_tendsto_at_bot [semilattice_sup α] [preorder β] {u : α → β}
(h : tendsto u at_top at_bot) : ∀ a b, ∃ a' ≥ a, u a' ≤ b :=
@exists_le_of_tendsto_at_top _ (order_dual β) _ _ _ h
lemma exists_lt_of_tendsto_at_top [semilattice_sup α] [preorder β] [no_top_order β]
{u : α → β} (h : tendsto u at_top at_top) : ∀ a b, ∃ a' ≥ a, b < u a' :=
begin
intros a b,
cases no_top b with b' hb',
rcases exists_le_of_tendsto_at_top h a b' with ⟨a', ha', ha''⟩,
exact ⟨a', ha', lt_of_lt_of_le hb' ha''⟩
end
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma exists_lt_of_tendsto_at_bot [semilattice_sup α] [preorder β] [no_bot_order β]
{u : α → β} (h : tendsto u at_top at_bot) : ∀ a b, ∃ a' ≥ a, u a' < b :=
@exists_lt_of_tendsto_at_top _ (order_dual β) _ _ _ _ h
/--
If `u` is a sequence which is unbounded above,
then after any point, it reaches a value strictly greater than all previous values.
-/
lemma high_scores [linear_order β] [no_top_order β] {u : ℕ → β}
(hu : tendsto u at_top at_top) : ∀ N, ∃ n ≥ N, ∀ k < n, u k < u n :=
begin
letI := classical.DLO β,
intros N,
let A := finset.image u (finset.range $ N+1), -- A = {u 0, ..., u N}
have Ane : A.nonempty,
from ⟨u 0, finset.mem_image_of_mem _ (finset.mem_range.mpr $ nat.zero_lt_succ _)⟩,
let M := finset.max' A Ane,
have ex : ∃ n ≥ N, M < u n,
from exists_lt_of_tendsto_at_top hu _ _,
obtain ⟨n, hnN, hnM, hn_min⟩ : ∃ n, N ≤ n ∧ M < u n ∧ ∀ k, N ≤ k → k < n → u k ≤ M,
{ use nat.find ex,
rw ← and_assoc,
split,
{ simpa using nat.find_spec ex },
{ intros k hk hk',
simpa [hk] using nat.find_min ex hk' } },
use [n, hnN],
intros k hk,
by_cases H : k ≤ N,
{ have : u k ∈ A,
from finset.mem_image_of_mem _ (finset.mem_range.mpr $ nat.lt_succ_of_le H),
have : u k ≤ M,
from finset.le_max' A (u k) this,
exact lt_of_le_of_lt this hnM },
{ push_neg at H,
calc u k ≤ M : hn_min k (le_of_lt H) hk
... < u n : hnM },
end
/--
If `u` is a sequence which is unbounded below,
then after any point, it reaches a value strictly smaller than all previous values.
-/
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma low_scores [linear_order β] [no_bot_order β] {u : ℕ → β}
(hu : tendsto u at_top at_bot) : ∀ N, ∃ n ≥ N, ∀ k < n, u n < u k :=
@high_scores (order_dual β) _ _ _ hu
/--
If `u` is a sequence which is unbounded above,
then it `frequently` reaches a value strictly greater than all previous values.
-/
lemma frequently_high_scores [linear_order β] [no_top_order β] {u : ℕ → β}
(hu : tendsto u at_top at_top) : ∃ᶠ n in at_top, ∀ k < n, u k < u n :=
by simpa [frequently_at_top] using high_scores hu
/--
If `u` is a sequence which is unbounded below,
then it `frequently` reaches a value strictly smaller than all previous values.
-/
lemma frequently_low_scores [linear_order β] [no_bot_order β] {u : ℕ → β}
(hu : tendsto u at_top at_bot) : ∃ᶠ n in at_top, ∀ k < n, u n < u k :=
@frequently_high_scores (order_dual β) _ _ _ hu
lemma strict_mono_subseq_of_tendsto_at_top
{β : Type*} [linear_order β] [no_top_order β]
{u : ℕ → β} (hu : tendsto u at_top at_top) :
∃ φ : ℕ → ℕ, strict_mono φ ∧ strict_mono (u ∘ φ) :=
let ⟨φ, h, h'⟩ := extraction_of_frequently_at_top (frequently_high_scores hu) in
⟨φ, h, λ n m hnm, h' m _ (h hnm)⟩
lemma strict_mono_subseq_of_id_le {u : ℕ → ℕ} (hu : ∀ n, n ≤ u n) :
∃ φ : ℕ → ℕ, strict_mono φ ∧ strict_mono (u ∘ φ) :=
strict_mono_subseq_of_tendsto_at_top (tendsto_at_top_mono hu tendsto_id)
lemma strict_mono_tendsto_at_top {φ : ℕ → ℕ} (h : strict_mono φ) :
tendsto φ at_top at_top :=
tendsto_at_top_mono h.id_le tendsto_id
section ordered_add_comm_monoid
variables [ordered_add_comm_monoid β] {l : filter α} {f g : α → β}
lemma tendsto_at_top_add_nonneg_left' (hf : ∀ᶠ x in l, 0 ≤ f x) (hg : tendsto g l at_top) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_mono' l (hf.mono (λ x, le_add_of_nonneg_left)) hg
lemma tendsto_at_bot_add_nonpos_left' (hf : ∀ᶠ x in l, f x ≤ 0) (hg : tendsto g l at_bot) :
tendsto (λ x, f x + g x) l at_bot :=
@tendsto_at_top_add_nonneg_left' _ (order_dual β) _ _ _ _ hf hg
lemma tendsto_at_top_add_nonneg_left (hf : ∀ x, 0 ≤ f x) (hg : tendsto g l at_top) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_add_nonneg_left' (eventually_of_forall hf) hg
lemma tendsto_at_bot_add_nonpos_left (hf : ∀ x, f x ≤ 0) (hg : tendsto g l at_bot) :
tendsto (λ x, f x + g x) l at_bot :=
@tendsto_at_top_add_nonneg_left _ (order_dual β) _ _ _ _ hf hg
lemma tendsto_at_top_add_nonneg_right' (hf : tendsto f l at_top) (hg : ∀ᶠ x in l, 0 ≤ g x) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_mono' l (monotone_mem_sets (λ x, le_add_of_nonneg_right) hg) hf
lemma tendsto_at_bot_add_nonpos_right' (hf : tendsto f l at_bot) (hg : ∀ᶠ x in l, g x ≤ 0) :
tendsto (λ x, f x + g x) l at_bot :=
@tendsto_at_top_add_nonneg_right' _ (order_dual β) _ _ _ _ hf hg
lemma tendsto_at_top_add_nonneg_right (hf : tendsto f l at_top) (hg : ∀ x, 0 ≤ g x) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_add_nonneg_right' hf (eventually_of_forall hg)
lemma tendsto_at_bot_add_nonpos_right (hf : tendsto f l at_bot) (hg : ∀ x, g x ≤ 0) :
tendsto (λ x, f x + g x) l at_bot :=
@tendsto_at_top_add_nonneg_right _ (order_dual β) _ _ _ _ hf hg
lemma tendsto_at_top_add (hf : tendsto f l at_top) (hg : tendsto g l at_top) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_add_nonneg_left' ((tendsto_at_top (λ (a : α), f a) l).mp hf 0) hg
lemma tendsto_at_bot_add (hf : tendsto f l at_bot) (hg : tendsto g l at_bot) :
tendsto (λ x, f x + g x) l at_bot :=
@tendsto_at_top_add _ (order_dual β) _ _ _ _ hf hg
end ordered_add_comm_monoid
section ordered_cancel_add_comm_monoid
variables [ordered_cancel_add_comm_monoid β] {l : filter α} {f g : α → β}
lemma tendsto_at_top_of_add_const_left (C : β) (hf : tendsto (λ x, C + f x) l at_top) :
tendsto f l at_top :=
(tendsto_at_top _ l).2 $ assume b,
((tendsto_at_top _ _).1 hf (C + b)).mono (λ x, le_of_add_le_add_left)
lemma tendsto_at_bot_of_add_const_left (C : β) (hf : tendsto (λ x, C + f x) l at_bot) :
tendsto f l at_bot :=
@tendsto_at_top_of_add_const_left _ (order_dual β) _ _ _ C hf
lemma tendsto_at_top_of_add_const_right (C : β) (hf : tendsto (λ x, f x + C) l at_top) :
tendsto f l at_top :=
(tendsto_at_top _ l).2 $ assume b,
((tendsto_at_top _ _).1 hf (b + C)).mono (λ x, le_of_add_le_add_right)
lemma tendsto_at_bot_of_add_const_right (C : β) (hf : tendsto (λ x, f x + C) l at_bot) :
tendsto f l at_bot :=
@tendsto_at_top_of_add_const_right _ (order_dual β) _ _ _ C hf
lemma tendsto_at_top_of_add_bdd_above_left' (C) (hC : ∀ᶠ x in l, f x ≤ C)
(h : tendsto (λ x, f x + g x) l at_top) :
tendsto g l at_top :=
tendsto_at_top_of_add_const_left C
(tendsto_at_top_mono' l (hC.mono (λ x hx, add_le_add_right hx (g x))) h)
lemma tendsto_at_bot_of_add_bdd_below_left' (C) (hC : ∀ᶠ x in l, C ≤ f x)
(h : tendsto (λ x, f x + g x) l at_bot) :
tendsto g l at_bot :=
@tendsto_at_top_of_add_bdd_above_left' _ (order_dual β) _ _ _ _ C hC h
lemma tendsto_at_top_of_add_bdd_above_left (C) (hC : ∀ x, f x ≤ C) :
tendsto (λ x, f x + g x) l at_top → tendsto g l at_top :=
tendsto_at_top_of_add_bdd_above_left' C (univ_mem_sets' hC)
lemma tendsto_at_bot_of_add_bdd_below_left (C) (hC : ∀ x, C ≤ f x) :
tendsto (λ x, f x + g x) l at_bot → tendsto g l at_bot :=
@tendsto_at_top_of_add_bdd_above_left _ (order_dual β) _ _ _ _ C hC
lemma tendsto_at_top_of_add_bdd_above_right' (C) (hC : ∀ᶠ x in l, g x ≤ C)
(h : tendsto (λ x, f x + g x) l at_top) :
tendsto f l at_top :=
tendsto_at_top_of_add_const_right C
(tendsto_at_top_mono' l (hC.mono (λ x hx, add_le_add_left hx (f x))) h)
lemma tendsto_at_bot_of_add_bdd_below_right' (C) (hC : ∀ᶠ x in l, C ≤ g x)
(h : tendsto (λ x, f x + g x) l at_bot) :
tendsto f l at_bot :=
@tendsto_at_top_of_add_bdd_above_right' _ (order_dual β) _ _ _ _ C hC h
lemma tendsto_at_top_of_add_bdd_above_right (C) (hC : ∀ x, g x ≤ C) :
tendsto (λ x, f x + g x) l at_top → tendsto f l at_top :=
tendsto_at_top_of_add_bdd_above_right' C (univ_mem_sets' hC)
lemma tendsto_at_bot_of_add_bdd_below_right (C) (hC : ∀ x, C ≤ g x) :
tendsto (λ x, f x + g x) l at_bot → tendsto f l at_bot :=
@tendsto_at_top_of_add_bdd_above_right _ (order_dual β) _ _ _ _ C hC
end ordered_cancel_add_comm_monoid
section ordered_group
variables [ordered_add_comm_group β] (l : filter α) {f g : α → β}
lemma tendsto_at_top_add_left_of_le' (C : β) (hf : ∀ᶠ x in l, C ≤ f x) (hg : tendsto g l at_top) :
tendsto (λ x, f x + g x) l at_top :=
@tendsto_at_top_of_add_bdd_above_left' _ _ _ l (λ x, -(f x)) (λ x, f x + g x) (-C)
(by simpa) (by simpa)
lemma tendsto_at_bot_add_left_of_ge' (C : β) (hf : ∀ᶠ x in l, f x ≤ C) (hg : tendsto g l at_bot) :
tendsto (λ x, f x + g x) l at_bot :=
@tendsto_at_top_add_left_of_le' _ (order_dual β) _ _ _ _ C hf hg
lemma tendsto_at_top_add_left_of_le (C : β) (hf : ∀ x, C ≤ f x) (hg : tendsto g l at_top) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_add_left_of_le' l C (univ_mem_sets' hf) hg
lemma tendsto_at_bot_add_left_of_ge (C : β) (hf : ∀ x, f x ≤ C) (hg : tendsto g l at_bot) :
tendsto (λ x, f x + g x) l at_bot :=
@tendsto_at_top_add_left_of_le _ (order_dual β) _ _ _ _ C hf hg
lemma tendsto_at_top_add_right_of_le' (C : β) (hf : tendsto f l at_top) (hg : ∀ᶠ x in l, C ≤ g x) :
tendsto (λ x, f x + g x) l at_top :=
@tendsto_at_top_of_add_bdd_above_right' _ _ _ l (λ x, f x + g x) (λ x, -(g x)) (-C)
(by simp [hg]) (by simp [hf])
lemma tendsto_at_bot_add_right_of_ge' (C : β) (hf : tendsto f l at_bot) (hg : ∀ᶠ x in l, g x ≤ C) :
tendsto (λ x, f x + g x) l at_bot :=
@tendsto_at_top_add_right_of_le' _ (order_dual β) _ _ _ _ C hf hg
lemma tendsto_at_top_add_right_of_le (C : β) (hf : tendsto f l at_top) (hg : ∀ x, C ≤ g x) :
tendsto (λ x, f x + g x) l at_top :=
tendsto_at_top_add_right_of_le' l C hf (univ_mem_sets' hg)
lemma tendsto_at_bot_add_right_of_ge (C : β) (hf : tendsto f l at_bot) (hg : ∀ x, g x ≤ C) :
tendsto (λ x, f x + g x) l at_bot :=
@tendsto_at_top_add_right_of_le _ (order_dual β) _ _ _ _ C hf hg
lemma tendsto_at_top_add_const_left (C : β) (hf : tendsto f l at_top) :
tendsto (λ x, C + f x) l at_top :=
tendsto_at_top_add_left_of_le' l C (univ_mem_sets' $ λ _, le_refl C) hf
lemma tendsto_at_bot_add_const_left (C : β) (hf : tendsto f l at_bot) :
tendsto (λ x, C + f x) l at_bot :=
@tendsto_at_top_add_const_left _ (order_dual β) _ _ _ C hf
lemma tendsto_at_top_add_const_right (C : β) (hf : tendsto f l at_top) :
tendsto (λ x, f x + C) l at_top :=
tendsto_at_top_add_right_of_le' l C hf (univ_mem_sets' $ λ _, le_refl C)
lemma tendsto_at_bot_add_const_right (C : β) (hf : tendsto f l at_bot) :
tendsto (λ x, f x + C) l at_bot :=
@tendsto_at_top_add_const_right _ (order_dual β) _ _ _ C hf
end ordered_group
open_locale filter
lemma tendsto_at_top' [nonempty α] [semilattice_sup α] (f : α → β) (l : filter β) :
tendsto f at_top l ↔ (∀s ∈ l, ∃a, ∀b≥a, f b ∈ s) :=
by simp only [tendsto_def, mem_at_top_sets]; refl
lemma tendsto_at_bot' [nonempty α] [semilattice_inf α] (f : α → β) (l : filter β) :
tendsto f at_bot l ↔ (∀s ∈ l, ∃a, ∀b≤a, f b ∈ s) :=
@tendsto_at_top' (order_dual α) _ _ _ _ _
theorem tendsto_at_top_principal [nonempty β] [semilattice_sup β] {f : β → α} {s : set α} :
tendsto f at_top (𝓟 s) ↔ ∃N, ∀n≥N, f n ∈ s :=
by rw [tendsto_iff_comap, comap_principal, le_principal_iff, mem_at_top_sets]; refl
theorem tendsto_at_bot_principal [nonempty β] [semilattice_inf β] {f : β → α} {s : set α} :
tendsto f at_bot (𝓟 s) ↔ ∃N, ∀n≤N, f n ∈ s :=
@tendsto_at_top_principal _ (order_dual β) _ _ _ _
/-- A function `f` grows to `+∞` independent of an order-preserving embedding `e`. -/
lemma tendsto_at_top_embedding [preorder β] [preorder γ]
{f : α → β} {e : β → γ} {l : filter α}
(hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, c ≤ e b) :
tendsto (e ∘ f) l at_top ↔ tendsto f l at_top :=
begin
rw [tendsto_at_top, tendsto_at_top],
split,
{ assume hc b,
filter_upwards [hc (e b)] assume a, (hm b (f a)).1 },
{ assume hb c,
rcases hu c with ⟨b, hc⟩,
filter_upwards [hb b] assume a ha, le_trans hc ((hm b (f a)).2 ha) }
end
/-- A function `f` goes to `-∞` independent of an order-preserving embedding `e`. -/
lemma tendsto_at_bot_embedding [preorder β] [preorder γ]
{f : α → β} {e : β → γ} {l : filter α}
(hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, e b ≤ c) :
tendsto (e ∘ f) l at_bot ↔ tendsto f l at_bot :=
begin
rw [tendsto_at_bot, tendsto_at_bot],
split,
{ assume hc b,
filter_upwards [hc (e b)] assume a, (hm (f a) b).1 },
{ assume hb c,
rcases hu c with ⟨b, hc⟩,
filter_upwards [hb b] assume a ha, le_trans ((hm (f a) b).2 ha) hc }
end
lemma tendsto_at_top_at_top [nonempty α] [semilattice_sup α] [preorder β] (f : α → β) :
tendsto f at_top at_top ↔ ∀ b : β, ∃ i : α, ∀ a : α, i ≤ a → b ≤ f a :=
iff.trans tendsto_infi $ forall_congr $ assume b, tendsto_at_top_principal
lemma tendsto_at_top_at_bot [nonempty α] [semilattice_sup α] [preorder β] (f : α → β) :
tendsto f at_top at_bot ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), i ≤ a → f a ≤ b :=
@tendsto_at_top_at_top α (order_dual β) _ _ _ f
lemma tendsto_at_bot_at_top [nonempty α] [semilattice_inf α] [preorder β] (f : α → β) :
tendsto f at_bot at_top ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), a ≤ i → b ≤ f a :=
@tendsto_at_top_at_top (order_dual α) β _ _ _ f
lemma tendsto_at_bot_at_bot [nonempty α] [semilattice_inf α] [preorder β] (f : α → β) :
tendsto f at_bot at_bot ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), a ≤ i → f a ≤ b :=
@tendsto_at_top_at_top (order_dual α) (order_dual β) _ _ _ f
lemma tendsto_at_top_at_top_of_monotone [preorder α] [preorder β] {f : α → β} (hf : monotone f)
(h : ∀ b, ∃ a, b ≤ f a) :
tendsto f at_top at_top :=
tendsto_infi.2 $ λ b, tendsto_principal.2 $ let ⟨a, ha⟩ := h b in
mem_sets_of_superset (mem_at_top a) $ λ a' ha', le_trans ha (hf ha')
lemma tendsto_at_bot_at_bot_of_monotone [preorder α] [preorder β] {f : α → β} (hf : monotone f)
(h : ∀ b, ∃ a, f a ≤ b) :
tendsto f at_bot at_bot :=
tendsto_infi.2 $ λ b, tendsto_principal.2 $ let ⟨a, ha⟩ := h b in
mem_sets_of_superset (mem_at_bot a) $ λ a' ha', le_trans (hf ha') ha
lemma tendsto_at_top_at_top_iff_of_monotone [nonempty α] [semilattice_sup α] [preorder β]
{f : α → β} (hf : monotone f) :
tendsto f at_top at_top ↔ ∀ b : β, ∃ a : α, b ≤ f a :=
(tendsto_at_top_at_top f).trans $ forall_congr $ λ b, exists_congr $ λ a,
⟨λ h, h a (le_refl a), λ h a' ha', le_trans h $ hf ha'⟩
lemma tendsto_at_bot_at_bot_iff_of_monotone [nonempty α] [semilattice_inf α] [preorder β]
{f : α → β} (hf : monotone f) :
tendsto f at_bot at_bot ↔ ∀ b : β, ∃ a : α, f a ≤ b :=
(tendsto_at_bot_at_bot f).trans $ forall_congr $ λ b, exists_congr $ λ a,
⟨λ h, h a (le_refl a), λ h a' ha', le_trans (hf ha') h⟩
alias tendsto_at_top_at_top_of_monotone ← monotone.tendsto_at_top_at_top
alias tendsto_at_bot_at_bot_of_monotone ← monotone.tendsto_at_bot_at_bot
alias tendsto_at_top_at_top_iff_of_monotone ← monotone.tendsto_at_top_at_top_iff
alias tendsto_at_bot_at_bot_iff_of_monotone ← monotone.tendsto_at_bot_at_bot_iff
lemma tendsto_finset_range : tendsto finset.range at_top at_top :=
finset.range_mono.tendsto_at_top_at_top finset.exists_nat_subset_range
lemma at_top_finset_eq_infi : (at_top : filter $ finset α) = ⨅ x : α, 𝓟 (Ici {x}) :=
begin
refine le_antisymm (le_infi (λ i, le_principal_iff.2 $ mem_at_top {i})) _,
refine le_infi (λ s, le_principal_iff.2 $ mem_infi_iff.2 _),
refine ⟨↑s, s.finite_to_set, _, λ i, mem_principal_self _, _⟩,
simp only [subset_def, mem_Inter, set_coe.forall, mem_Ici, finset.le_iff_subset,
finset.mem_singleton, finset.subset_iff, forall_eq], dsimp,
exact λ t, id
end
/-- If `f` is a monotone sequence of `finset`s and each `x` belongs to one of `f n`, then
`tendsto f at_top at_top`. -/
lemma tendsto_at_top_finset_of_monotone [preorder β]
{f : β → finset α} (h : monotone f) (h' : ∀ x : α, ∃ n, x ∈ f n) :
tendsto f at_top at_top :=
begin
simp only [at_top_finset_eq_infi, tendsto_infi, tendsto_principal],
intro a,
rcases h' a with ⟨b, hb⟩,
exact eventually.mono (mem_at_top b)
(λ b' hb', le_trans (finset.singleton_subset_iff.2 hb) (h hb')),
end
alias tendsto_at_top_finset_of_monotone ← monotone.tendsto_at_top_finset
lemma tendsto_finset_image_at_top_at_top {i : β → γ} {j : γ → β} (h : function.left_inverse j i) :
tendsto (finset.image j) at_top at_top :=
(finset.image_mono j).tendsto_at_top_finset $ assume a,
⟨{i a}, by simp only [finset.image_singleton, h a, finset.mem_singleton]⟩
lemma tendsto_finset_preimage_at_top_at_top {f : α → β} (hf : function.injective f) :
tendsto (λ s : finset β, s.preimage f (hf.inj_on _)) at_top at_top :=
(finset.monotone_preimage hf).tendsto_at_top_finset $
λ x, ⟨{f x}, finset.mem_preimage.2 $ finset.mem_singleton_self _⟩
lemma prod_at_top_at_top_eq {β₁ β₂ : Type*} [semilattice_sup β₁] [semilattice_sup β₂] :
(at_top : filter β₁) ×ᶠ (at_top : filter β₂) = (at_top : filter (β₁ × β₂)) :=
begin
by_cases ne : nonempty β₁ ∧ nonempty β₂,
{ cases ne,
resetI,
simp [at_top, prod_infi_left, prod_infi_right, infi_prod],
exact infi_comm },
{ rw not_and_distrib at ne,
cases ne;
{ have : ¬ (nonempty (β₁ × β₂)), by simp [ne],
rw [at_top.filter_eq_bot_of_not_nonempty ne, at_top.filter_eq_bot_of_not_nonempty this],
simp only [bot_prod, prod_bot] } }
end
lemma prod_at_bot_at_bot_eq {β₁ β₂ : Type*} [semilattice_inf β₁] [semilattice_inf β₂] :
(at_bot : filter β₁) ×ᶠ (at_bot : filter β₂) = (at_bot : filter (β₁ × β₂)) :=
@prod_at_top_at_top_eq (order_dual β₁) (order_dual β₂) _ _
lemma prod_map_at_top_eq {α₁ α₂ β₁ β₂ : Type*} [semilattice_sup β₁] [semilattice_sup β₂]
(u₁ : β₁ → α₁) (u₂ : β₂ → α₂) :
(map u₁ at_top) ×ᶠ (map u₂ at_top) = map (prod.map u₁ u₂) at_top :=
by rw [prod_map_map_eq, prod_at_top_at_top_eq, prod.map_def]
lemma prod_map_at_bot_eq {α₁ α₂ β₁ β₂ : Type*} [semilattice_inf β₁] [semilattice_inf β₂]
(u₁ : β₁ → α₁) (u₂ : β₂ → α₂) :
(map u₁ at_bot) ×ᶠ (map u₂ at_bot) = map (prod.map u₁ u₂) at_bot :=
@prod_map_at_top_eq _ _ (order_dual β₁) (order_dual β₂) _ _ _ _
/-- A function `f` maps upwards closed sets (at_top sets) to upwards closed sets when it is a
Galois insertion. The Galois "insertion" and "connection" is weakened to only require it to be an
insertion and a connetion above `b'`. -/
lemma map_at_top_eq_of_gc [semilattice_sup α] [semilattice_sup β] {f : α → β} (g : β → α) (b' : β)
(hf : monotone f) (gc : ∀a, ∀b≥b', f a ≤ b ↔ a ≤ g b) (hgi : ∀b≥b', b ≤ f (g b)) :
map f at_top = at_top :=
begin
refine le_antisymm
(hf.tendsto_at_top_at_top $ λ b, ⟨g (b ⊔ b'), le_sup_left.trans $ hgi _ le_sup_right⟩) _,
rw [@map_at_top_eq _ _ ⟨g b'⟩],
refine le_infi (λ a, infi_le_of_le (f a ⊔ b') $ principal_mono.2 $ λ b hb, _),
rw [mem_set_of_eq, sup_le_iff] at hb,
exact ⟨g b, (gc _ _ hb.2).1 hb.1, le_antisymm ((gc _ _ hb.2).2 (le_refl _)) (hgi _ hb.2)⟩
end
lemma map_at_bot_eq_of_gc [semilattice_inf α] [semilattice_inf β] {f : α → β} (g : β → α) (b' : β)
(hf : monotone f) (gc : ∀a, ∀b≤b', b ≤ f a ↔ g b ≤ a) (hgi : ∀b≤b', f (g b) ≤ b) :
map f at_bot = at_bot :=
@map_at_top_eq_of_gc (order_dual α) (order_dual β) _ _ _ _ _ hf.order_dual gc hgi
lemma map_add_at_top_eq_nat (k : ℕ) : map (λa, a + k) at_top = at_top :=
map_at_top_eq_of_gc (λa, a - k) k
(assume a b h, add_le_add_right h k)
(assume a b h, (nat.le_sub_right_iff_add_le h).symm)
(assume a h, by rw [nat.sub_add_cancel h])
lemma map_sub_at_top_eq_nat (k : ℕ) : map (λa, a - k) at_top = at_top :=
map_at_top_eq_of_gc (λa, a + k) 0
(assume a b h, nat.sub_le_sub_right h _)
(assume a b _, nat.sub_le_right_iff_le_add)
(assume b _, by rw [nat.add_sub_cancel])
lemma tendsto_add_at_top_nat (k : ℕ) : tendsto (λa, a + k) at_top at_top :=
le_of_eq (map_add_at_top_eq_nat k)
lemma tendsto_sub_at_top_nat (k : ℕ) : tendsto (λa, a - k) at_top at_top :=
le_of_eq (map_sub_at_top_eq_nat k)
lemma tendsto_add_at_top_iff_nat {f : ℕ → α} {l : filter α} (k : ℕ) :
tendsto (λn, f (n + k)) at_top l ↔ tendsto f at_top l :=
show tendsto (f ∘ (λn, n + k)) at_top l ↔ tendsto f at_top l,
by rw [← tendsto_map'_iff, map_add_at_top_eq_nat]
lemma map_div_at_top_eq_nat (k : ℕ) (hk : 0 < k) : map (λa, a / k) at_top = at_top :=
map_at_top_eq_of_gc (λb, b * k + (k - 1)) 1
(assume a b h, nat.div_le_div_right h)
(assume a b _,
calc a / k ≤ b ↔ a / k < b + 1 : by rw [← nat.succ_eq_add_one, nat.lt_succ_iff]
... ↔ a < (b + 1) * k : nat.div_lt_iff_lt_mul _ _ hk
... ↔ _ :
begin
cases k,
exact (lt_irrefl _ hk).elim,
simp [mul_add, add_mul, nat.succ_add, nat.lt_succ_iff]
end)
(assume b _,
calc b = (b * k) / k : by rw [nat.mul_div_cancel b hk]
... ≤ (b * k + (k - 1)) / k : nat.div_le_div_right $ nat.le_add_right _ _)
/-- If `u` is a monotone function with linear ordered codomain and the range of `u` is not bounded
above, then `tendsto u at_top at_top`. -/
lemma tendsto_at_top_at_top_of_monotone' [preorder ι] [linear_order α]
{u : ι → α} (h : monotone u) (H : ¬bdd_above (range u)) :
tendsto u at_top at_top :=
begin
apply h.tendsto_at_top_at_top,
intro b,
rcases not_bdd_above_iff.1 H b with ⟨_, ⟨N, rfl⟩, hN⟩,
exact ⟨N, le_of_lt hN⟩,
end
/-- If `u` is a monotone function with linear ordered codomain and the range of `u` is not bounded
below, then `tendsto u at_bot at_bot`. -/
lemma tendsto_at_bot_at_bot_of_monotone' [preorder ι] [linear_order α]
{u : ι → α} (h : monotone u) (H : ¬bdd_below (range u)) :
tendsto u at_bot at_bot :=
@tendsto_at_top_at_top_of_monotone' (order_dual ι) (order_dual α) _ _ _ h.order_dual H
lemma unbounded_of_tendsto_at_top [nonempty α] [semilattice_sup α] [preorder β] [no_top_order β]
{f : α → β} (h : tendsto f at_top at_top) :
¬ bdd_above (range f) :=
begin
rintros ⟨M, hM⟩,
cases mem_at_top_sets.mp (h $ Ioi_mem_at_top M) with a ha,
apply lt_irrefl M,
calc
M < f a : ha a (le_refl _)
... ≤ M : hM (set.mem_range_self a)
end
lemma unbounded_of_tendsto_at_bot [nonempty α] [semilattice_sup α] [preorder β] [no_bot_order β]
{f : α → β} (h : tendsto f at_top at_bot) :
¬ bdd_below (range f) :=
@unbounded_of_tendsto_at_top _ (order_dual β) _ _ _ _ _ h
lemma unbounded_of_tendsto_at_top' [nonempty α] [semilattice_inf α] [preorder β] [no_top_order β]
{f : α → β} (h : tendsto f at_bot at_top) :
¬ bdd_above (range f) :=
@unbounded_of_tendsto_at_top (order_dual α) _ _ _ _ _ _ h
lemma unbounded_of_tendsto_at_bot' [nonempty α] [semilattice_inf α] [preorder β] [no_bot_order β]
{f : α → β} (h : tendsto f at_bot at_bot) :
¬ bdd_below (range f) :=
@unbounded_of_tendsto_at_top (order_dual α) (order_dual β) _ _ _ _ _ h
/-- If a monotone function `u : ι → α` tends to `at_top` along *some* non-trivial filter `l`, then
it tends to `at_top` along `at_top`. -/
lemma tendsto_at_top_of_monotone_of_filter [preorder ι] [preorder α] {l : filter ι}
{u : ι → α} (h : monotone u) [ne_bot l] (hu : tendsto u l at_top) :
tendsto u at_top at_top :=
h.tendsto_at_top_at_top $ λ b, (hu.eventually (mem_at_top b)).exists
/-- If a monotone function `u : ι → α` tends to `at_bot` along *some* non-trivial filter `l`, then
it tends to `at_bot` along `at_bot`. -/
lemma tendsto_at_bot_of_monotone_of_filter [preorder ι] [preorder α] {l : filter ι}
{u : ι → α} (h : monotone u) [ne_bot l] (hu : tendsto u l at_bot) :
tendsto u at_bot at_bot :=
@tendsto_at_top_of_monotone_of_filter (order_dual ι) (order_dual α) _ _ _ _ h.order_dual _ hu
lemma tendsto_at_top_of_monotone_of_subseq [preorder ι] [preorder α] {u : ι → α}
{φ : ι' → ι} (h : monotone u) {l : filter ι'} [ne_bot l]
(H : tendsto (u ∘ φ) l at_top) :
tendsto u at_top at_top :=
tendsto_at_top_of_monotone_of_filter h (tendsto_map' H)
lemma tendsto_at_bot_of_monotone_of_subseq [preorder ι] [preorder α] {u : ι → α}
{φ : ι' → ι} (h : monotone u) {l : filter ι'} [ne_bot l]
(H : tendsto (u ∘ φ) l at_bot) :
tendsto u at_bot at_bot :=
tendsto_at_bot_of_monotone_of_filter h (tendsto_map' H)
lemma tendsto_neg_at_top_at_bot [ordered_add_comm_group α] :
tendsto (has_neg.neg : α → α) at_top at_bot :=
begin
simp only [tendsto_at_bot, neg_le],
exact λ b, eventually_ge_at_top _
end
lemma tendsto_neg_at_bot_at_top [ordered_add_comm_group α] :
tendsto (has_neg.neg : α → α) at_bot at_top :=
@tendsto_neg_at_top_at_bot (order_dual α) _
/-- Let `f` and `g` be two maps to the same commutative monoid. This lemma gives a sufficient
condition for comparison of the filter `at_top.map (λ s, ∏ b in s, f b)` with
`at_top.map (λ s, ∏ b in s, g b)`. This is useful to compare the set of limit points of
`Π b in s, f b` as `s → at_top` with the similar set for `g`. -/
@[to_additive]
lemma map_at_top_finset_prod_le_of_prod_eq [comm_monoid α] {f : β → α} {g : γ → α}
(h_eq : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ ∏ x in u', g x = ∏ b in v', f b) :
at_top.map (λs:finset β, ∏ b in s, f b) ≤ at_top.map (λs:finset γ, ∏ x in s, g x) :=
by rw [map_at_top_eq, map_at_top_eq];
from (le_infi $ assume b, let ⟨v, hv⟩ := h_eq b in infi_le_of_le v $
by simp [set.image_subset_iff]; exact hv)
lemma has_antimono_basis.tendsto [semilattice_sup ι] [nonempty ι] {l : filter α}
{p : ι → Prop} {s : ι → set α} (hl : l.has_antimono_basis p s) {φ : ι → α}
(h : ∀ i : ι, φ i ∈ s i) : tendsto φ at_top l :=
(at_top_basis.tendsto_iff hl.to_has_basis).2 $ assume i hi,
⟨i, trivial, λ j hij, hl.decreasing hi (hl.mono hij hi) hij (h j)⟩
namespace is_countably_generated
/-- An abstract version of continuity of sequentially continuous functions on metric spaces:
if a filter `k` is countably generated then `tendsto f k l` iff for every sequence `u`
converging to `k`, `f ∘ u` tends to `l`. -/
lemma tendsto_iff_seq_tendsto {f : α → β} {k : filter α} {l : filter β}
(hcb : k.is_countably_generated) :
tendsto f k l ↔ (∀ x : ℕ → α, tendsto x at_top k → tendsto (f ∘ x) at_top l) :=
suffices (∀ x : ℕ → α, tendsto x at_top k → tendsto (f ∘ x) at_top l) → tendsto f k l,
from ⟨by intros; apply tendsto.comp; assumption, by assumption⟩,
begin
rcases hcb.exists_antimono_seq with ⟨g, gmon, gbasis⟩,
have gbasis : k.has_basis (λ _, true) (λ i, (g i)),
{ subst gbasis,
exact has_basis_infi_principal (directed_of_sup gmon) },
contrapose,
simp only [not_forall, gbasis.tendsto_left_iff, exists_const, not_exists, not_imp],
rintro ⟨B, hBl, hfBk⟩,
choose x h using hfBk,
use x, split,
{ exact (at_top_basis.tendsto_iff gbasis).2 (λ i _, ⟨i, trivial, λ j hj, gmon hj (h j).1⟩) },
{ simp only [tendsto_at_top', (∘), not_forall, not_exists],
use [B, hBl],
intro i, use [i, (le_refl _)],
apply (h i).right },
end
lemma tendsto_of_seq_tendsto {f : α → β} {k : filter α} {l : filter β}
(hcb : k.is_countably_generated) :
(∀ x : ℕ → α, tendsto x at_top k → tendsto (f ∘ x) at_top l) → tendsto f k l :=
hcb.tendsto_iff_seq_tendsto.2
lemma subseq_tendsto {f : filter α} (hf : is_countably_generated f)
{u : ℕ → α}
(hx : ne_bot (f ⊓ map u at_top)) :
∃ (θ : ℕ → ℕ), (strict_mono θ) ∧ (tendsto (u ∘ θ) at_top f) :=
begin
rcases hf.has_antimono_basis with ⟨B, h⟩,
have : ∀ N, ∃ n ≥ N, u n ∈ B N,
from λ N, filter.inf_map_at_top_ne_bot_iff.mp hx _ (h.to_has_basis.mem_of_mem trivial) N,
choose φ hφ using this,
cases forall_and_distrib.mp hφ with φ_ge φ_in,
have lim_uφ : tendsto (u ∘ φ) at_top f,
from h.tendsto φ_in,
have lim_φ : tendsto φ at_top at_top,
from (tendsto_at_top_mono φ_ge tendsto_id),
obtain ⟨ψ, hψ, hψφ⟩ : ∃ ψ : ℕ → ℕ, strict_mono ψ ∧ strict_mono (φ ∘ ψ),
from strict_mono_subseq_of_tendsto_at_top lim_φ,
exact ⟨φ ∘ ψ, hψφ, lim_uφ.comp $ strict_mono_tendsto_at_top hψ⟩,
end
end is_countably_generated
end filter
open filter finset
/-- Let `g : γ → β` be an injective function and `f : β → α` be a function from the codomain of `g`
to a commutative monoid. Suppose that `f x = 1` outside of the range of `g`. Then the filters
`at_top.map (λ s, ∏ i in s, f (g i))` and `at_top.map (λ s, ∏ i in s, f i)` coincide.
The additive version of this lemma is used to prove the equality `∑' x, f (g x) = ∑' y, f y` under
the same assumptions.-/
@[to_additive]
lemma function.injective.map_at_top_finset_prod_eq [comm_monoid α] {g : γ → β}
(hg : function.injective g) {f : β → α} (hf : ∀ x ∉ set.range g, f x = 1) :
map (λ s, ∏ i in s, f (g i)) at_top = map (λ s, ∏ i in s, f i) at_top :=
begin
apply le_antisymm; refine map_at_top_finset_prod_le_of_prod_eq (λ s, _),
{ refine ⟨s.preimage g (hg.inj_on _), λ t ht, _⟩,
refine ⟨t.image g ∪ s, finset.subset_union_right _ _, _⟩,
rw [← finset.prod_image (hg.inj_on _)],
refine (prod_subset (subset_union_left _ _) _).symm,
simp only [finset.mem_union, finset.mem_image],
refine λ y hy hyt, hf y (mt _ hyt),
rintros ⟨x, rfl⟩,
exact ⟨x, ht (finset.mem_preimage.2 $ hy.resolve_left hyt), rfl⟩ },
{ refine ⟨s.image g, λ t ht, _⟩,
simp only [← prod_preimage _ _ (hg.inj_on _) _ (λ x _, hf x)],
exact ⟨_, (image_subset_iff_subset_preimage _).1 ht, rfl⟩ }
end
/-- Let `g : γ → β` be an injective function and `f : β → α` be a function from the codomain of `g`
to an additive commutative monoid. Suppose that `f x = 0` outside of the range of `g`. Then the
filters `at_top.map (λ s, ∑ i in s, f (g i))` and `at_top.map (λ s, ∑ i in s, f i)` coincide.
This lemma is used to prove the equality `∑' x, f (g x) = ∑' y, f y` under
the same assumptions.-/
add_decl_doc function.injective.map_at_top_finset_sum_eq
|
2732e6bd45588b1838246cd2ba39c5fc88e1c81d | da23b545e1653cafd4ab88b3a42b9115a0b1355f | /src/tidy/rewrite_search/tactic.lean | 4afd38d5541ba759ac415d0942862b9031300a73 | [] | 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 | 6,389 | lean | -- Copyright (c) 2018 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Keeley Hoek, Scott Morrison
import tidy.rewrite_all
import .init
open interactive interactive.types expr tactic
namespace tidy.rewrite_search
def how.to_tactic (rule_strings : list string) : how → option string
| (how.defeq) := none
| (how.rewrite index s location) := some ("nth_rewrite" ++ (match s with | side.L := "_lhs" | side.R := "_rhs" end) ++ " " ++ to_string location ++ " " ++ (rule_strings.nth index).iget)
meta def explain_proof (rule_strings : list string) (steps : list how) : string :=
string.intercalate ",\n" (steps.map $ λ s : how, (s.to_tactic rule_strings).to_list).join
def how.concisely (rule_strings : list string) : how → option string
| (how.defeq) := none
| (how.rewrite index side location) := some (rule_strings.nth index).iget
meta def explain_proof_concisely (rule_strings : list string) (steps : list how) (needs_refl : bool) : string :=
"erw [" ++ (string.intercalate ", " (steps.map $ λ s : how, (s.concisely rule_strings).to_list).join) ++ "]" ++ (if needs_refl then ", refl" else "")
-- fails if we can't just use rewrite
-- otherwise, returns 'tt' if we need a `refl` at the end
meta def check_if_simple_rewrite_succeeds (rewrites : list (expr × bool)) : tactic bool :=
lock_tactic_state $
focus1 $
do
t ← target,
rewrites.mmap' (λ q : expr × bool, rewrite_target q.1 {symm := q.2, md := semireducible}),
(reflexivity reducible >> return ff) <|> (reflexivity >> return tt)
meta def pp_rules (rs : list (expr × bool)) : tactic (list string) := rs.mmap (λ p, (do pp ← pretty_print p.1, return (if p.2 then ("←" ++ pp) else pp)))
meta def handle_search_result {α β γ : Type} (cfg : config α β γ) (rules : list (expr × bool)) (result : search_result) : tactic string := do
match result with
| search_result.failure reason := fail reason
| search_result.success proof steps := do
if cfg.trace then do
pp ← pretty_print proof,
trace format!"rewrite_search found proof:\n{pp}"
else skip,
rules_strings ← pp_rules rules,
explanation ← (do
let rewrites := (steps.map $ λ s, match s with
| how.defeq := []
| how.rewrite index _ _ := [(rules.nth index).iget]
end).join,
needs_refl ← check_if_simple_rewrite_succeeds rewrites,
return (explain_proof_concisely rules_strings steps needs_refl)) <|> return (explain_proof rules_strings steps),
if cfg.trace_result then trace explanation
else skip,
exact proof,
return explanation
end
meta def do_rewrite_search {α β γ : Type} (rs : list (expr × bool)) (cfg : config α β γ) : tactic string := do
t ← target,
match t with
| `(%%lhs = %%rhs) := do
-- if cfg.trace_rules then
-- do rs_strings ← pp_rules rs,
-- trace ("rewrite_search using:\n---\n" ++ (string.intercalate "\n" rs_strings) ++ "\n---")
-- else skip,
-- FIXME there is a bit of code duplication because we change the type of
-- "cfg" when we try a fallback config...
i ← try_mk_search_instance cfg rs lhs rhs,
match i with
| some i := do
result ← i.search_until_solved,
handle_search_result cfg rs result
| none := do
trace "\nError initialising rewrite_search instance, falling back to emergency config!\n",
new_cfg ← pure (mk_fallback_config cfg),
i ← try_mk_search_instance new_cfg rs lhs rhs,
match i with
| some i := do
result ← i.search_until_solved,
handle_search_result new_cfg rs result
| none := do
fail "Could not initialise emergency rewrite_search instance!"
end
end
| _ := fail "target is not an equation"
end
open tidy.rewrite_search.strategy.edit_distance
meta def default_config : config unit ed_partial unit := {}
meta def pick_default_config : tactic unit := `[exact tidy.rewrite_search.default_config]
-- TODO coerce {} = ∅ into default_config
end tidy.rewrite_search
namespace tactic.interactive
open tidy.rewrite_search
meta def rewrite_search {α β γ : Type} (rs : parse rw_rules) (cfg : config α β γ . pick_default_config) : tactic string := do
rs ← rs.rules.mmap (λ r, do e ← to_expr' r.rule, pure (e, r.symm)),
do_rewrite_search rs cfg
meta def is_eq_after_binders : expr → bool
| (expr.pi n bi d b) := is_eq_after_binders b
| `(%%a = %%b) := tt
| _ := ff
meta def load_exprs : list name → tactic (list expr)
| [] := return []
| (a :: rest) := do
names ← attribute.get_instances a,
u ← names.mmap $ mk_const,
l ← load_exprs rest,
return (u ++ l)
meta def rewrite_search_using {α β γ : Type} (as : list name) (cfg : config α β γ . pick_default_config) : tactic string := do
tgt ← target,
if tgt.has_meta_var then
fail "rewrite_search is not suitable for goals containing metavariables"
else skip,
exprs ← load_exprs as,
hyps ← local_context,
hyps ← hyps.mfilter (λ h, (do t ← infer_type h, return ¬ t.has_meta_var)),
let exprs := exprs ++ hyps,
-- rules ← close_under_apps exprs, -- TODO don't do this for everything, it's too expensive: only for specially marked lemmas
let rules := exprs,
rules ← rules.mfilter $ λ r, (do t ← infer_type r, return (is_eq_after_binders t)),
let pairs := rules.map (λ e, (e, ff)) ++ rules.map (λ e, (e, tt)),
do_rewrite_search pairs cfg
end tactic.interactive
meta def search_attribute : user_attribute := {
name := `search,
descr := ""
}
run_cmd attribute.register `search_attribute
-- structure cat :=
-- (O : Type)
-- (H : O → O → Type)
-- (i : Π o : O, H o o)
-- (c : Π {X Y Z : O} (f : H X Y) (g : H Y Z), H X Z)
-- (li : Π {X Y : O} (f : H X Y), c (i X) f = f)
-- (ri : Π {X Y : O} (f : H X Y), c f (i Y) = f)
-- (a : Π {W X Y Z : O} (f : H W X) (g : H X Y) (h : H Y Z), c (c f g) h = c f (c g h))
-- attribute [search] cat.li cat.a
-- private example (C : cat) (X Y Z : C.O) (f : C.H X Y) (g : C.H Y X) (w : C.c g f = C.i Y) (h k : C.H Y Z) (p : C.c f h = C.c f k) : h = k :=
-- begin
-- rewrite_search_using `search {trace := tt, trace_rules:=tt},
-- end
-- PROJECT cache all_rewrites_list?
|
9ad29416c483faa23f20ecf4ce307e07c8687a33 | 3dc4623269159d02a444fe898d33e8c7e7e9461b | /.github/workflows/geo/src/group_objet/G.lean | ed957c5767c0fdf7293ecaa600985fb86c4a2996 | [] | no_license | Or7ando/lean | cc003e6c41048eae7c34aa6bada51c9e9add9e66 | d41169cf4e416a0d42092fb6bdc14131cee9dd15 | refs/heads/master | 1,650,600,589,722 | 1,587,262,906,000 | 1,587,262,906,000 | 255,387,160 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,539 | lean | import .groupk
open category_theory
open category_theory.limits
open category_theory.category
universes v u
open Product_stuff
namespace GROUP_OBJ
structure group_obj (C : Type u)[𝒞 : category.{v} C][has_binary_products.{v} C][has_terminal.{v} C] :=
(X : C)
(μ : X ⨯ X ⟶ X)
(inv : X ⟶ X)
(ε : T C ⟶ X)
(hyp_one_mul : (T X | 𝟙 X) ≫ (ε ⊗ 𝟙 X) ≫ μ = 𝟙 X)
(hyp_mul_one : (𝟙 X | T X) ≫ ( 𝟙 X ⊗ ε) ≫ μ = 𝟙 X)
(hyp_mul_inv : (𝟙 X | inv) ≫ μ = (T X) ≫ ε )
(hyp_assoc : (μ ⊗ 𝟙 X) ≫ (μ) = (prod.associator X X X).hom ≫ (𝟙 X ⊗ μ) ≫ μ ) -- (a *b) * c = (a * (b * c))
#print "ger"
structure Rr (C : Type u)[𝒞 : category.{v} C][has_binary_products.{v} C][has_terminal.{v} C] :=
#print notation
open lem
#print notation
-- structure group_obj (C : Type u)[𝒞 : category.{v} C][has_binary_products.{v} C][has_terminal.{v} C] :=
-- (X : C)
-- (μ : X ⨯ X ⟶ X)
-- (inv : X ⟶ X)
-- (ε : T C ⟶ X)
-- (hyp_one_mul : (T X | 𝟙 X) ≫ (ε ⊗ 𝟙 X) ≫ μ = 𝟙 X)
-- (hyp_mul_one : (𝟙 X | T X) ≫ ( 𝟙 X ⊗ ε) ≫ μ = 𝟙 X)
-- (hyp_mul_inv : (𝟙 X | inv) ≫ μ = (T X) ≫ ε )
-- (hyp_assoc : (μ ⊗ 𝟙 X) ≫ (μ) = (prod.associator X X X).hom ≫ (𝟙 X ⊗ μ) ≫ μ ) -- (a *b) * c = (a * (b * c))
-- variables {C : Type u}
-- variables [𝒞 : category.{v} C]
-- variables [has_binary_products.{v} C][has_terminal.{v} C]
-- include 𝒞
-- instance coee : has_coe (group_obj C) C := ⟨λ F, F.X⟩
-- variables (G : group_obj C)
-- #eval 2+2
-- /-
-- First Goal : make a instance of group on the point Hom (Y, G) = G(Y)
-- -/
-- -- Idea Fix R : We have (Γ × Γ )(R ) ≃ Γ (R) × Γ (R) : Let g1 g2 ∈ Γ (R)
-- -- we get φ ∈ (Γ × Γ) R. Next :
-- -- ε : Γ × Γ → Γ give β : (Γ × Γ) R → Γ R via Yoneda.map finaly : β φ is ok !
-- --
-- --
-- --
-- def one (R : C) : R[G.X] :=
-- begin
-- exact (terminal.from R ≫ G.ε),
-- end
-- def mul (R : C) : R[G.X] → R[G.X] → R[G.X] := λ g1 g2,
-- begin
-- let φ := ( g1 | g2),
-- -- let γ := (prod.mk g1 g2 : (yoneda.obj G.X).obj (op R) × (yoneda.obj G.X).obj (op R)), -- × versus ⨯
-- -- let θ := (Yoneda_preserve_product R G.X G.X ).inv,
-- let β := (R< (G.μ) > : R[G.X ⨯ G.X] ⟶ R[G.X]),
-- exact β φ,
-- end
-- variables (R : C)
-- include R
-- instance yoneda_mul : has_mul (R[(G : C)]) := ⟨mul G R ⟩
-- instance yoneda_one : has_one (R[(G :C)]) := ⟨one G R ⟩
-- lemma mul_comp (a b : R [(G : C)] ) : a * b = (R < G.μ >) (a | b) := rfl -- priority R < g.μ > (a | b) not ()
-- lemma one_comp : (1 : (R[(G : C)])) = terminal.from R ≫ G.ε := rfl
-- #print group
-- -- group.mul : Π {α : Type u} [c : group α], α → α → α
-- -- group.mul_assoc : ∀ {α : Type u} [c : group α] (a b c_1 : α), a * b * c_1 = a * (b * c_1)
-- -- group.one : Π (α : Type u) [c : group α], α
-- -- group.one_mul : ∀ {α : Type u} [c : group α] (a : α), 1 * a = a
-- -- group.mul_one : ∀ {α : Type u} [c : group α] (a : α), a * 1 = a
-- -- group.inv : Π {α : Type u} [c : group α], α → α
-- -- group.mul_left_inv : ∀ {α : Type u} [c : group α] (a : α), a⁻¹ * a = 1
-- -- lemma pre_des (R: C) : (R < T G.X> | 𝟙 (R[G.X])) ≫ (R < G.ε > ⊗ 𝟙 (R[G.X])) = (( R < T G.X> ≫ (R < G.ε>)) | 𝟙 (R[G.X])) :=
-- -- begin exact destruction (R < T G.X>) (R < G.ε >), end
-- def one_mul' (a : R[(G : C)]) : 1 * a = a := begin
-- sorry,
-- -- rw mul_comp,rw one_comp,
-- -- -- (hyp_one_mul : (T X | 𝟙 X) ≫ (ε ⊗ 𝟙 X) ≫ μ = 𝟙 X)
-- -- have V : (R <(T G.X | 𝟙 G.X)>) ≫ (R<(G.ε ⊗ 𝟙 G.X)>) ≫ (R<G.μ>) = (R<𝟙 G.X>),
-- -- rw ← yoneda_sugar.composition,rw ← yoneda_sugar.composition,
-- -- rw G.hyp_one_mul,
-- -- rw yoneda_sugar_prod_inv at V,rw ← assoc at V,
-- -- rw yoneda_sugar.otimes at V,
-- -- have hyp : (((R < T G.X> | R < 𝟙 G.X>) ≫ (yoneda_sugar.prod R (T C) G.X).inv) ≫
-- -- (yoneda_sugar.prod R (T C) G.X).hom ≫
-- -- (R < G.ε> ⊗ R < 𝟙 G.X>) ≫ (yoneda_sugar.prod R G.X G.X).inv) ≫
-- -- (R < G.μ>) = (((R < T G.X> | R < 𝟙 G.X>) ≫ ((yoneda_sugar.prod R (T C) G.X).inv) ≫
-- -- (yoneda_sugar.prod R (T C) G.X).hom) ≫
-- -- (R < G.ε> ⊗ R < 𝟙 G.X>) ≫ (yoneda_sugar.prod R G.X G.X).inv) ≫
-- -- (R < G.μ>),
-- -- simp,
-- -- rw yoneda_sugar.prod.inv_hom at hyp,rw hyp at V,
-- -- -- rw yoneda_sugar.id at V,have V' : (𝟙 (R[(G : C)])) a = a, exact rfl,
-- -- -- erw ← V at V', rw ← V',
-- -- have fact_2 : ((R < T G.X> | R < 𝟙 G.X>) ≫ 𝟙 (R[T C] ⨯ R[G.X])) = (R < T G.X> | R < 𝟙 G.X>),
-- -- simp,
-- -- rw fact_2 at V,
-- -- have fact_3 : ((R < T G.X> | R < 𝟙 G.X>) ≫ (R < G.ε> ⊗ R < 𝟙 G.X>) ≫ (yoneda_sugar.prod R G.X G.X).inv) ≫
-- -- (R < G.μ>) = (((R < T G.X> | R < 𝟙 G.X>) ≫ (R < G.ε> ⊗ R < 𝟙 G.X>)) ≫ (yoneda_sugar.prod R G.X G.X).inv) ≫
-- -- (R < G.μ>), sorry,
-- -- rw yoneda_sugar.id at fact_3,
-- -- rw pre_des R at fact_3,
-- -- scott_and_kevin_ultimate_tatic -- :D
-- -- -- rw destruction(R < T G.X>) (R < G.ε>) at fact_3,
-- -- sorry, -- tooooooooo difficult for the moment !!!!
-- end
end GROUP_OBJ |
22c4e3da7888b15dcf06c6bbee0a044a9378e56c | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/group_theory/perm/basic.lean | c7254e8cd62bd237f98b4547f9532bbf81177721 | [] | 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 | 8,717 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Mario Carneiro
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.equiv.basic
import Mathlib.algebra.group.basic
import Mathlib.algebra.group.hom
import Mathlib.algebra.group.pi
import Mathlib.algebra.group.prod
import Mathlib.PostPort
universes u u_1 u_2
namespace Mathlib
/-!
# The group of permutations (self-equivalences) of a type `α`
This file defines the `group` structure on `equiv.perm α`.
-/
namespace equiv
namespace perm
protected instance perm_group {α : Type u} : group (perm α) :=
group.mk (fun (f g : perm α) => equiv.trans g f) sorry (equiv.refl α) sorry sorry equiv.symm
(div_inv_monoid.div._default (fun (f g : perm α) => equiv.trans g f) sorry (equiv.refl α) sorry sorry equiv.symm)
sorry
theorem mul_apply {α : Type u} (f : perm α) (g : perm α) (x : α) : coe_fn (f * g) x = coe_fn f (coe_fn g x) :=
trans_apply g f x
theorem one_apply {α : Type u} (x : α) : coe_fn 1 x = x :=
rfl
@[simp] theorem inv_apply_self {α : Type u} (f : perm α) (x : α) : coe_fn (f⁻¹) (coe_fn f x) = x :=
symm_apply_apply f x
@[simp] theorem apply_inv_self {α : Type u} (f : perm α) (x : α) : coe_fn f (coe_fn (f⁻¹) x) = x :=
apply_symm_apply f x
theorem one_def {α : Type u} : 1 = equiv.refl α :=
rfl
theorem mul_def {α : Type u} (f : perm α) (g : perm α) : f * g = equiv.trans g f :=
rfl
theorem inv_def {α : Type u} (f : perm α) : f⁻¹ = equiv.symm f :=
rfl
@[simp] theorem coe_mul {α : Type u} (f : perm α) (g : perm α) : ⇑(f * g) = ⇑f ∘ ⇑g :=
rfl
@[simp] theorem coe_one {α : Type u} : ⇑1 = id :=
rfl
theorem eq_inv_iff_eq {α : Type u} {f : perm α} {x : α} {y : α} : x = coe_fn (f⁻¹) y ↔ coe_fn f x = y :=
eq_symm_apply f
theorem inv_eq_iff_eq {α : Type u} {f : perm α} {x : α} {y : α} : coe_fn (f⁻¹) x = y ↔ x = coe_fn f y :=
symm_apply_eq f
/-! Lemmas about mixing `perm` with `equiv`. Because we have multiple ways to express
`equiv.refl`, `equiv.symm`, and `equiv.trans`, we want simp lemmas for every combination.
The assumption made here is that if you're using the group structure, you want to preserve it after
simp. -/
@[simp] theorem trans_one {α : Sort u_1} {β : Type u_2} (e : α ≃ β) : equiv.trans e 1 = e :=
trans_refl e
@[simp] theorem mul_refl {α : Type u} (e : perm α) : e * equiv.refl α = e :=
trans_refl e
@[simp] theorem one_symm {α : Type u} : equiv.symm 1 = 1 :=
refl_symm
@[simp] theorem refl_inv {α : Type u} : equiv.refl α⁻¹ = 1 :=
refl_symm
@[simp] theorem one_trans {α : Type u_1} {β : Sort u_2} (e : α ≃ β) : equiv.trans 1 e = e :=
refl_trans e
@[simp] theorem refl_mul {α : Type u} (e : perm α) : equiv.refl α * e = e :=
refl_trans e
@[simp] theorem inv_trans {α : Type u} (e : perm α) : equiv.trans (e⁻¹) e = 1 :=
symm_trans e
@[simp] theorem mul_symm {α : Type u} (e : perm α) : e * equiv.symm e = 1 :=
symm_trans e
@[simp] theorem trans_inv {α : Type u} (e : perm α) : equiv.trans e (e⁻¹) = 1 :=
trans_symm e
@[simp] theorem symm_mul {α : Type u} (e : perm α) : equiv.symm e * e = 1 :=
trans_symm e
/-! Lemmas about `equiv.perm.sum_congr` re-expressed via the group structure. -/
@[simp] theorem sum_congr_mul {α : Type u_1} {β : Type u_2} (e : perm α) (f : perm β) (g : perm α) (h : perm β) : sum_congr e f * sum_congr g h = sum_congr (e * g) (f * h) :=
sum_congr_trans g h e f
@[simp] theorem sum_congr_inv {α : Type u_1} {β : Type u_2} (e : perm α) (f : perm β) : sum_congr e f⁻¹ = sum_congr (e⁻¹) (f⁻¹) :=
sum_congr_symm e f
@[simp] theorem sum_congr_one {α : Type u_1} {β : Type u_2} : sum_congr 1 1 = 1 :=
sum_congr_refl
/-- `equiv.perm.sum_congr` as a `monoid_hom`, with its two arguments bundled into a single `prod`.
This is particularly useful for its `monoid_hom.range` projection, which is the subgroup of
permutations which do not exchange elements between `α` and `β`. -/
def sum_congr_hom (α : Type u_1) (β : Type u_2) : perm α × perm β →* perm (α ⊕ β) :=
monoid_hom.mk (fun (a : perm α × perm β) => sum_congr (prod.fst a) (prod.snd a)) sum_congr_one sorry
theorem sum_congr_hom_injective {α : Type u_1} {β : Type u_2} : function.injective ⇑(sum_congr_hom α β) := sorry
@[simp] theorem sum_congr_swap_one {α : Type u_1} {β : Type u_2} [DecidableEq α] [DecidableEq β] (i : α) (j : α) : sum_congr (swap i j) 1 = swap (sum.inl i) (sum.inl j) :=
sum_congr_swap_refl i j
@[simp] theorem sum_congr_one_swap {α : Type u_1} {β : Type u_2} [DecidableEq α] [DecidableEq β] (i : β) (j : β) : sum_congr 1 (swap i j) = swap (sum.inr i) (sum.inr j) :=
sum_congr_refl_swap i j
/-! Lemmas about `equiv.perm.sigma_congr_right` re-expressed via the group structure. -/
@[simp] theorem sigma_congr_right_mul {α : Type u_1} {β : α → Type u_2} (F : (a : α) → perm (β a)) (G : (a : α) → perm (β a)) : sigma_congr_right F * sigma_congr_right G = sigma_congr_right (F * G) :=
sigma_congr_right_trans G F
@[simp] theorem sigma_congr_right_inv {α : Type u_1} {β : α → Type u_2} (F : (a : α) → perm (β a)) : sigma_congr_right F⁻¹ = sigma_congr_right fun (a : α) => F a⁻¹ :=
sigma_congr_right_symm F
@[simp] theorem sigma_congr_right_one {α : Type u_1} {β : α → Type u_2} : sigma_congr_right 1 = 1 :=
sigma_congr_right_refl
/-- `equiv.perm.sigma_congr_right` as a `monoid_hom`.
This is particularly useful for its `monoid_hom.range` projection, which is the subgroup of
permutations which do not exchange elements between fibers. -/
def sigma_congr_right_hom {α : Type u_1} (β : α → Type u_2) : ((a : α) → perm (β a)) →* perm (sigma fun (a : α) => β a) :=
monoid_hom.mk sigma_congr_right sorry sorry
theorem sigma_congr_right_hom_injective {α : Type u_1} {β : α → Type u_2} : function.injective ⇑(sigma_congr_right_hom β) := sorry
end perm
@[simp] theorem swap_inv {α : Type u} [DecidableEq α] (x : α) (y : α) : swap x y⁻¹ = swap x y :=
rfl
@[simp] theorem swap_mul_self {α : Type u} [DecidableEq α] (i : α) (j : α) : swap i j * swap i j = 1 :=
swap_swap i j
theorem swap_mul_eq_mul_swap {α : Type u} [DecidableEq α] (f : perm α) (x : α) (y : α) : swap x y * f = f * swap (coe_fn (f⁻¹) x) (coe_fn (f⁻¹) y) := sorry
theorem mul_swap_eq_swap_mul {α : Type u} [DecidableEq α] (f : perm α) (x : α) (y : α) : f * swap x y = swap (coe_fn f x) (coe_fn f y) * f := sorry
/-- Left-multiplying a permutation with `swap i j` twice gives the original permutation.
This specialization of `swap_mul_self` is useful when using cosets of permutations.
-/
@[simp] theorem swap_mul_self_mul {α : Type u} [DecidableEq α] (i : α) (j : α) (σ : perm α) : swap i j * (swap i j * σ) = σ :=
eq.mpr (id (Eq._oldrec (Eq.refl (swap i j * (swap i j * σ) = σ)) (Eq.symm (mul_assoc (swap i j) (swap i j) σ))))
(eq.mpr (id (Eq._oldrec (Eq.refl (swap i j * swap i j * σ = σ)) (swap_mul_self i j)))
(eq.mpr (id (Eq._oldrec (Eq.refl (1 * σ = σ)) (one_mul σ))) (Eq.refl σ)))
/-- Right-multiplying a permutation with `swap i j` twice gives the original permutation.
This specialization of `swap_mul_self` is useful when using cosets of permutations.
-/
@[simp] theorem mul_swap_mul_self {α : Type u} [DecidableEq α] (i : α) (j : α) (σ : perm α) : σ * swap i j * swap i j = σ :=
eq.mpr (id (Eq._oldrec (Eq.refl (σ * swap i j * swap i j = σ)) (mul_assoc σ (swap i j) (swap i j))))
(eq.mpr (id (Eq._oldrec (Eq.refl (σ * (swap i j * swap i j) = σ)) (swap_mul_self i j)))
(eq.mpr (id (Eq._oldrec (Eq.refl (σ * 1 = σ)) (mul_one σ))) (Eq.refl σ)))
/-- A stronger version of `mul_right_injective` -/
@[simp] theorem swap_mul_involutive {α : Type u} [DecidableEq α] (i : α) (j : α) : function.involutive (Mul.mul (swap i j)) :=
swap_mul_self_mul i j
/-- A stronger version of `mul_left_injective` -/
@[simp] theorem mul_swap_involutive {α : Type u} [DecidableEq α] (i : α) (j : α) : function.involutive fun (_x : perm α) => _x * swap i j :=
mul_swap_mul_self i j
theorem swap_mul_eq_iff {α : Type u} [DecidableEq α] {i : α} {j : α} {σ : perm α} : swap i j * σ = σ ↔ i = j := sorry
theorem mul_swap_eq_iff {α : Type u} [DecidableEq α] {i : α} {j : α} {σ : perm α} : σ * swap i j = σ ↔ i = j := sorry
theorem swap_mul_swap_mul_swap {α : Type u} [DecidableEq α] {x : α} {y : α} {z : α} (hwz : x ≠ y) (hxz : x ≠ z) : swap y z * swap x y * swap y z = swap z x := sorry
|
ac163f46d68d5729c6a7149a5d15dd921a365064 | 958488bc7f3c2044206e0358e56d7690b6ae696c | /lean/exists.lean | 573f5df9accad2c5148fdb2fba0c70fbd2810bde | [] | no_license | possientis/Prog | a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4 | d4b3debc37610a88e0dac3ac5914903604fd1d1f | refs/heads/master | 1,692,263,717,723 | 1,691,757,179,000 | 1,691,757,179,000 | 40,361,602 | 3 | 0 | null | 1,679,896,438,000 | 1,438,953,859,000 | Coq | UTF-8 | Lean | false | false | 3,425 | lean | open nat -- zero_lt_succ
lemma L1 : ∃ (n : ℕ), n > 0
:= exists.intro 1
(show 1 > 0, from zero_lt_succ 0)
lemma L2 : ∃ (n : ℕ), n > 0
:= have H : 1 > 0, from zero_lt_succ 0, -- assert (1 > 0) as H in Coq
exists.intro 1 H
lemma L3 : ∃ (n : ℕ), n > 0
:= ⟨1, zero_lt_succ 0⟩
lemma L4 : ∀ (n : ℕ), n > 0 → ∃ (m : ℕ), m < n
:= assume n H, exists.intro 0 H
lemma L5 : ∀ (n : ℕ), n > 0 → ∃ (m : ℕ), m < n
:= assume n H, ⟨0,H⟩
lemma L6 : ∀ (n m p : ℕ), n < m → m < p → ∃ (q : ℕ), n < q ∧ q < p
:= assume n m p H1 H2, exists.intro m (and.intro H1 H2)
lemma L7 : ∀ (n m p : ℕ), n < m → m < p → ∃ (q : ℕ), n < q ∧ q < p
:= assume n m p H1 H2, ⟨m, H1, H2⟩
--#check @exists.intro
--#check @zero_lt_succ
--#check L1
--#check L2
--#check L3
--#check L4
--#check L5
--#check L6
--#check L7
variable g : ℕ → ℕ → ℕ
variable Hg : g 0 0 = 0
theorem T1 : ∃ (n : ℕ), g n n = n := ⟨0,Hg⟩
theorem T2 : ∃ (n : ℕ), g n 0 = n := ⟨0,Hg⟩
theorem T3 : ∃ (n : ℕ), g 0 0 = n := ⟨0,Hg⟩
theorem T4 : ∃ (n : ℕ), g n n = 0 := ⟨0,Hg⟩
--#check T1
--#check T2
--#check T3
--#check T4
set_option pp.implicit true -- display implicit arguments
--#print T1
--#print T2
--#print T3
--#print T4
--#check @exists.elim
lemma L8 : ∀ (α : Type) (p q : α → Prop), (∃ (x : α), p x ∧ q x) → ∃ (x : α), q x ∧ p x
:= assume α p q H,
show ∃ (x : α), q x ∧ p x,
from exists.elim H
(assume x H, exists.intro x
(show q x ∧ p x, from ⟨H.right,H.left⟩))
--#check L8
lemma L9 : ∀ (α : Type) (p q : α → Prop), (∃ (x : α), p x ∧ q x) → ∃ (x : α), q x ∧ p x
:= assume α p q H,
show ∃ (x : α), q x ∧ p x,
from exists.elim H
(assume x H,⟨x,H.right,H.left⟩)
--#check L9
lemma L10 : ∀ (α : Type) (p q : α → Prop), (∃ (x : α), p x ∧ q x) → ∃ (x : α), q x ∧ p x
:= assume α p q H,
match H with ⟨x,H⟩
:= ⟨x,H.right,H.left⟩
end
--#check L10
lemma L11 : ∀ (α : Type) (p q : α → Prop), (∃ (x : α), p x ∧ q x) → ∃ (x : α), q x ∧ p x
:= assume α p q H,
match H with ⟨(x:α),(H:p x ∧ q x)⟩
:= ⟨x,H.right,H.left⟩
end
--#check L11
lemma L12 : ∀ (α : Type) (p q : α → Prop), (∃ (x : α), p x ∧ q x) → ∃ (x : α), q x ∧ p x
:= assume α p q H,
match H with ⟨x, H1, H2⟩
:= ⟨x,H2,H1⟩
end
--#check L12
lemma L13 : ∀ (α : Type) (p q : α → Prop), (∃ (x : α), p x ∧ q x) → ∃ (x : α), q x ∧ p x
:= assume α p q H,
let ⟨x,H1,H2⟩ := H in ⟨x,H2,H1⟩
--#check L13
lemma L14 : ∀ (α : Type) (p q : α → Prop), (∃ (x : α), p x ∧ q x) → ∃ (x : α), q x ∧ p x
:= assume α p q ⟨x,H1,H2⟩, ⟨x,H2,H1⟩
--#check L14
def is_even (n : ℕ) : Prop := ∃ (m : ℕ), n = 2 * m
lemma L15 : ∀ (n m : ℕ), is_even n → is_even m → is_even (n + m)
:= assume n m ⟨k,H⟩ ⟨k',H'⟩, ⟨k + k',
show n + m = 2 * (k + k'),
from calc
n + m = 2 * k + m : by rw H
... = 2 * k + 2 * k' : by rw H'
... = 2 * (k + k') : by rw mul_add
⟩
--#check L15
lemma L16 : ∀ (n m : ℕ), is_even n → is_even m → is_even (n + m)
:= assume n m ⟨k,H⟩ ⟨k',H'⟩, ⟨k + k', by rw [H, H', mul_add]⟩
--#check L16
|
bccfa30094f217abdef2c0e4b5227d4a06d93579 | 82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7 | /src/Lean/Compiler/IR/CompilerM.lean | f251930060e879fedc54ce3d51a87d9751972550 | [
"Apache-2.0"
] | permissive | banksonian/lean4 | 3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc | 78da6b3aa2840693eea354a41e89fc5b212a5011 | refs/heads/master | 1,673,703,624,165 | 1,605,123,551,000 | 1,605,123,551,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,829 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Environment
import Lean.Compiler.IR.Basic
import Lean.Compiler.IR.Format
namespace Lean.IR
inductive LogEntry
| step (cls : Name) (decls : Array Decl)
| message (msg : Format)
namespace LogEntry
protected def fmt : LogEntry → Format
| step cls decls => Format.bracket "[" (format cls) "]" ++ decls.foldl (fun fmt decl => fmt ++ Format.line ++ format decl) Format.nil
| message msg => msg
instance : ToFormat LogEntry := ⟨LogEntry.fmt⟩
end LogEntry
abbrev Log := Array LogEntry
def Log.format (log : Log) : Format :=
log.foldl (init := Format.nil) fun fmt entry =>
f!"{fmt}{Format.line}{entry}"
@[export lean_ir_log_to_string]
def Log.toString (log : Log) : String :=
log.format.pretty
structure CompilerState :=
(env : Environment)
(log : Log := #[])
abbrev CompilerM := ReaderT Options (EStateM String CompilerState)
def log (entry : LogEntry) : CompilerM Unit :=
modify fun s => { s with log := s.log.push entry }
def tracePrefixOptionName := `trace.compiler.ir
private def isLogEnabledFor (opts : Options) (optName : Name) : Bool :=
match opts.find optName with
| some (DataValue.ofBool v) => v
| other => opts.getBool tracePrefixOptionName
private def logDeclsAux (optName : Name) (cls : Name) (decls : Array Decl) : CompilerM Unit := do
let opts ← read
if isLogEnabledFor opts optName then
log (LogEntry.step cls decls)
@[inline] def logDecls (cls : Name) (decl : Array Decl) : CompilerM Unit :=
logDeclsAux (tracePrefixOptionName ++ cls) cls decl
private def logMessageIfAux {α : Type} [ToFormat α] (optName : Name) (a : α) : CompilerM Unit := do
let opts ← read
if isLogEnabledFor opts optName then
log (LogEntry.message (format a))
@[inline] def logMessageIf {α : Type} [ToFormat α] (cls : Name) (a : α) : CompilerM Unit :=
logMessageIfAux (tracePrefixOptionName ++ cls) a
@[inline] def logMessage {α : Type} [ToFormat α] (cls : Name) (a : α) : CompilerM Unit :=
logMessageIfAux tracePrefixOptionName a
@[inline] def modifyEnv (f : Environment → Environment) : CompilerM Unit :=
modify fun s => { s with env := f s.env }
open Std (HashMap)
abbrev DeclMap := SMap Name Decl
/- Create an array of decls to be saved on .olean file.
`decls` may contain duplicate entries, but we assume the one that occurs last is the most recent one. -/
private def mkEntryArray (decls : List Decl) : Array Decl :=
/- Remove duplicates by adding decls into a map -/
let map : HashMap Name Decl := {}
let map := decls.foldl (init := map) fun map decl => map.insert decl.name decl
map.fold (fun a k v => a.push v) #[]
builtin_initialize declMapExt : SimplePersistentEnvExtension Decl DeclMap ←
registerSimplePersistentEnvExtension {
name := `IRDecls,
addImportedFn := fun as =>
let m : DeclMap := mkStateFromImportedEntries (fun s (d : Decl) => s.insert d.name d) {} as
m.switch,
addEntryFn := fun s d => s.insert d.name d,
toArrayFn := mkEntryArray
}
@[export lean_ir_find_env_decl]
def findEnvDecl (env : Environment) (n : Name) : Option Decl :=
(declMapExt.getState env).find? n
def findDecl (n : Name) : CompilerM (Option Decl) := do
let s ← get
pure $ findEnvDecl s.env n
def containsDecl (n : Name) : CompilerM Bool := do
let s ← get
pure $ (declMapExt.getState s.env).contains n
def getDecl (n : Name) : CompilerM Decl := do
let (some decl) ← findDecl n | throw s!"unknown declaration '{n}'"
pure decl
@[export lean_ir_add_decl]
def addDeclAux (env : Environment) (decl : Decl) : Environment :=
declMapExt.addEntry env decl
def getDecls (env : Environment) : List Decl :=
declMapExt.getEntries env
def getEnv : CompilerM Environment := do
let s ← get; pure s.env
def addDecl (decl : Decl) : CompilerM Unit :=
modifyEnv fun env => declMapExt.addEntry env decl
def addDecls (decls : Array Decl) : CompilerM Unit :=
decls.forM addDecl
def findEnvDecl' (env : Environment) (n : Name) (decls : Array Decl) : Option Decl :=
match decls.find? (fun decl => decl.name == n) with
| some decl => some decl
| none => (declMapExt.getState env).find? n
def findDecl' (n : Name) (decls : Array Decl) : CompilerM (Option Decl) := do
let s ← get; pure $ findEnvDecl' s.env n decls
def containsDecl' (n : Name) (decls : Array Decl) : CompilerM Bool := do
if decls.any fun decl => decl.name == n then
pure true
else
let s ← get
pure $ (declMapExt.getState s.env).contains n
def getDecl' (n : Name) (decls : Array Decl) : CompilerM Decl := do
let (some decl) ← findDecl' n decls | throw s!"unknown declaration '{n}'"
pure decl
end IR
end Lean
|
d44dd3fe029d5b9a98b07657402f42f7f975703a | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/linear_algebra/matrix/diagonal.lean | 4adf3a8c42c18fd531ef00797a5760562c24ca96 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 2,930 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Patrick Massot, Casper Putz, Anne Baanen
-/
import linear_algebra.matrix.to_lin
/-!
# Diagonal matrices
This file contains some results on the linear map corresponding to a
diagonal matrix (`range`, `ker` and `rank`).
## Tags
matrix, diagonal, linear_map
-/
noncomputable theory
open linear_map matrix set submodule
open_locale big_operators
open_locale matrix
universes u v w
namespace matrix
section comm_ring
variables {n : Type*} [fintype n] [decidable_eq n] {R : Type v} [comm_ring R]
lemma proj_diagonal (i : n) (w : n → R) :
(proj i).comp (to_lin' (diagonal w)) = (w i) • proj i :=
linear_map.ext $ λ j, mul_vec_diagonal _ _ _
lemma diagonal_comp_std_basis (w : n → R) (i : n) :
(diagonal w).to_lin'.comp (linear_map.std_basis R (λ_:n, R) i) =
(w i) • linear_map.std_basis R (λ_:n, R) i :=
linear_map.ext $ λ x, (diagonal_mul_vec_single w _ _).trans (pi.single_smul' i (w i) x)
lemma diagonal_to_lin' (w : n → R) :
(diagonal w).to_lin' = linear_map.pi (λi, w i • linear_map.proj i) :=
linear_map.ext $ λ v, funext $ λ i, mul_vec_diagonal _ _ _
end comm_ring
section field
variables {m n : Type*} [fintype m] [fintype n]
variables {K : Type u} [field K] -- maybe try to relax the universe constraint
lemma ker_diagonal_to_lin' [decidable_eq m] (w : m → K) :
ker (diagonal w).to_lin' = (⨆i∈{i | w i = 0 }, range (linear_map.std_basis K (λi, K) i)) :=
begin
rw [← comap_bot, ← infi_ker_proj, comap_infi],
have := λ i : m, ker_comp (to_lin' (diagonal w)) (proj i),
simp only [comap_infi, ← this, proj_diagonal, ker_smul'],
have : univ ⊆ {i : m | w i = 0} ∪ {i : m | w i = 0}ᶜ, { rw set.union_compl_self },
exact (supr_range_std_basis_eq_infi_ker_proj K (λi:m, K)
disjoint_compl_right this (set.to_finite _)).symm
end
lemma range_diagonal [decidable_eq m] (w : m → K) :
(diagonal w).to_lin'.range = (⨆ i ∈ {i | w i ≠ 0}, (linear_map.std_basis K (λi, K) i).range) :=
begin
dsimp only [mem_set_of_eq],
rw [← map_top, ← supr_range_std_basis, map_supr],
congr, funext i,
rw [← linear_map.range_comp, diagonal_comp_std_basis, ← range_smul']
end
lemma rank_diagonal [decidable_eq m] [decidable_eq K] (w : m → K) :
rank (diagonal w).to_lin' = fintype.card { i // w i ≠ 0 } :=
begin
have hu : univ ⊆ {i : m | w i = 0}ᶜ ∪ {i : m | w i = 0}, { rw set.compl_union_self },
have hd : disjoint {i : m | w i ≠ 0} {i : m | w i = 0} := disjoint_compl_left,
have B₁ := supr_range_std_basis_eq_infi_ker_proj K (λi:m, K) hd hu (set.to_finite _),
have B₂ := @infi_ker_proj_equiv K _ _ (λi:m, K) _ _ _ _ (by simp; apply_instance) hd hu,
rw [rank, range_diagonal, B₁, ←@dim_fun' K],
apply linear_equiv.dim_eq,
apply B₂,
end
end field
end matrix
|
eec1c8c363eba303fc66553591d43af20f7fd15b | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/topology/category/Top/opens.lean | 1b246724a5f3ea21fd17b36b0017f02d1a0ac198 | [
"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 | 10,372 | 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.opens
import category_theory.category.preorder
import category_theory.eq_to_hom
import topology.category.Top.epi_mono
/-!
# The category of open sets in a topological space.
We define `to_Top : opens X ⥤ Top` and
`map (f : X ⟶ Y) : opens Y ⥤ opens X`, given by taking preimages of open sets.
Unfortunately `opens` isn't (usefully) a functor `Top ⥤ Cat`.
(One can in fact define such a functor,
but using it results in unresolvable `eq.rec` terms in goals.)
Really it's a 2-functor from (spaces, continuous functions, equalities)
to (categories, functors, natural isomorphisms).
We don't attempt to set up the full theory here, but do provide the natural isomorphisms
`map_id : map (𝟙 X) ≅ 𝟭 (opens X)` and
`map_comp : map (f ≫ g) ≅ map g ⋙ map f`.
Beyond that, there's a collection of simp lemmas for working with these constructions.
-/
open category_theory
open topological_space
open opposite
universe u
namespace topological_space.opens
variables {X Y Z : Top.{u}}
/-!
Since `opens X` has a partial order, it automatically receives a `category` instance.
Unfortunately, because we do not allow morphisms in `Prop`,
the morphisms `U ⟶ V` are not just proofs `U ≤ V`, but rather
`ulift (plift (U ≤ V))`.
-/
instance opens_hom_has_coe_to_fun {U V : opens X} : has_coe_to_fun (U ⟶ V) (λ f, U → V) :=
⟨λ f x, ⟨x, f.le x.2⟩⟩
/-!
We now construct as morphisms various inclusions of open sets.
-/
-- This is tedious, but necessary because we decided not to allow Prop as morphisms in a category...
/--
The inclusion `U ⊓ V ⟶ U` as a morphism in the category of open sets.
-/
def inf_le_left (U V : opens X) : U ⊓ V ⟶ U := inf_le_left.hom
/--
The inclusion `U ⊓ V ⟶ V` as a morphism in the category of open sets.
-/
def inf_le_right (U V : opens X) : U ⊓ V ⟶ V := inf_le_right.hom
/--
The inclusion `U i ⟶ supr U` as a morphism in the category of open sets.
-/
def le_supr {ι : Type*} (U : ι → opens X) (i : ι) : U i ⟶ supr U := (le_supr U i).hom
/--
The inclusion `⊥ ⟶ U` as a morphism in the category of open sets.
-/
def bot_le (U : opens X) : ⊥ ⟶ U := bot_le.hom
/--
The inclusion `U ⟶ ⊤` as a morphism in the category of open sets.
-/
def le_top (U : opens X) : U ⟶ ⊤ := le_top.hom
-- We do not mark this as a simp lemma because it breaks open `x`.
-- Nevertheless, it is useful in `sheaf_of_functions`.
lemma inf_le_left_apply (U V : opens X) (x) :
(inf_le_left U V) x = ⟨x.1, (@_root_.inf_le_left _ _ U V : _ ≤ _) x.2⟩ :=
rfl
@[simp]
lemma inf_le_left_apply_mk (U V : opens X) (x) (m) :
(inf_le_left U V) ⟨x, m⟩ = ⟨x, (@_root_.inf_le_left _ _ U V : _ ≤ _) m⟩ :=
rfl
@[simp]
lemma le_supr_apply_mk {ι : Type*} (U : ι → opens X) (i : ι) (x) (m) :
(le_supr U i) ⟨x, m⟩ = ⟨x, (_root_.le_supr U i : _) m⟩ :=
rfl
/--
The functor from open sets in `X` to `Top`,
realising each open set as a topological space itself.
-/
def to_Top (X : Top.{u}) : opens X ⥤ Top :=
{ obj := λ U, ⟨U.val, infer_instance⟩,
map := λ U V i, ⟨λ x, ⟨x.1, i.le x.2⟩,
(embedding.continuous_iff embedding_subtype_coe).2 continuous_induced_dom⟩ }
@[simp]
lemma to_Top_map (X : Top.{u}) {U V : opens X} {f : U ⟶ V} {x} {h} :
((to_Top X).map f) ⟨x, h⟩ = ⟨x, f.le h⟩ :=
rfl
/--
The inclusion map from an open subset to the whole space, as a morphism in `Top`.
-/
@[simps]
def inclusion {X : Top.{u}} (U : opens X) : (to_Top X).obj U ⟶ X :=
{ to_fun := _,
continuous_to_fun := continuous_subtype_coe }
lemma open_embedding {X : Top.{u}} (U : opens X) : open_embedding (inclusion U) :=
is_open.open_embedding_subtype_coe U.2
/--
The inclusion of the top open subset (i.e. the whole space) is an isomorphism.
-/
def inclusion_top_iso (X : Top.{u}) : (to_Top X).obj ⊤ ≅ X :=
{ hom := inclusion ⊤,
inv := ⟨λ x, ⟨x, trivial⟩, continuous_def.2 $ λ U ⟨S, hS, hSU⟩, hSU ▸ hS⟩ }
/-- `opens.map f` gives the functor from open sets in Y to open set in X,
given by taking preimages under f. -/
def map (f : X ⟶ Y) : opens Y ⥤ opens X :=
{ obj := λ U, ⟨ f ⁻¹' U.val, U.property.preimage f.continuous ⟩,
map := λ U V i, ⟨ ⟨ λ x h, i.le h ⟩ ⟩ }.
@[simp] lemma map_obj (f : X ⟶ Y) (U) (p) :
(map f).obj ⟨U, p⟩ = ⟨f ⁻¹' U, p.preimage f.continuous⟩ := rfl
@[simp] lemma map_id_obj (U : opens X) : (map (𝟙 X)).obj U = U :=
let ⟨_,_⟩ := U in rfl
@[simp] lemma map_id_obj' (U) (p) : (map (𝟙 X)).obj ⟨U, p⟩ = ⟨U, p⟩ :=
rfl
@[simp] lemma map_id_obj_unop (U : (opens X)ᵒᵖ) : (map (𝟙 X)).obj (unop U) = unop U :=
let ⟨_,_⟩ := U.unop in rfl
@[simp] lemma op_map_id_obj (U : (opens X)ᵒᵖ) : (map (𝟙 X)).op.obj U = U :=
by simp
/--
The inclusion `U ⟶ (map f).obj ⊤` as a morphism in the category of open sets.
-/
def le_map_top (f : X ⟶ Y) (U : opens X) : U ⟶ (map f).obj ⊤ :=
le_top U
@[simp] lemma map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) :
(map (f ≫ g)).obj U = (map f).obj ((map g).obj U) :=
rfl
@[simp] lemma map_comp_obj' (f : X ⟶ Y) (g : Y ⟶ Z) (U) (p) :
(map (f ≫ g)).obj ⟨U, p⟩ = (map f).obj ((map g).obj ⟨U, p⟩) :=
rfl
@[simp] lemma map_comp_map (f : X ⟶ Y) (g : Y ⟶ Z) {U V} (i : U ⟶ V) :
(map (f ≫ g)).map i = (map f).map ((map g).map i) :=
rfl
@[simp] lemma map_comp_obj_unop (f : X ⟶ Y) (g : Y ⟶ Z) (U) :
(map (f ≫ g)).obj (unop U) = (map f).obj ((map g).obj (unop U)) :=
rfl
@[simp] lemma op_map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) :
(map (f ≫ g)).op.obj U = (map f).op.obj ((map g).op.obj U) :=
rfl
lemma map_supr (f : X ⟶ Y) {ι : Type*} (U : ι → opens Y) :
(map f).obj (supr U) = supr ((map f).obj ∘ U) :=
begin
apply subtype.eq, rw [supr_def, supr_def, map_obj],
dsimp, rw set.preimage_Union, refl,
end
section
variable (X)
/--
The functor `opens X ⥤ opens X` given by taking preimages under the identity function
is naturally isomorphic to the identity functor.
-/
@[simps]
def map_id : map (𝟙 X) ≅ 𝟭 (opens X) :=
{ hom := { app := λ U, eq_to_hom (map_id_obj U) },
inv := { app := λ U, eq_to_hom (map_id_obj U).symm } }
lemma map_id_eq : map (𝟙 X) = 𝟭 (opens X) :=
by { unfold map, congr, ext, refl, ext }
end
/--
The natural isomorphism between taking preimages under `f ≫ g`, and the composite
of taking preimages under `g`, then preimages under `f`.
-/
@[simps]
def map_comp (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) ≅ map g ⋙ map f :=
{ hom := { app := λ U, eq_to_hom (map_comp_obj f g U) },
inv := { app := λ U, eq_to_hom (map_comp_obj f g U).symm } }
lemma map_comp_eq (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) = map g ⋙ map f :=
rfl
/--
If two continuous maps `f g : X ⟶ Y` are equal,
then the functors `opens Y ⥤ opens X` they induce are isomorphic.
-/
-- We could make `f g` implicit here, but it's nice to be able to see when
-- they are the identity (often!)
def map_iso (f g : X ⟶ Y) (h : f = g) : map f ≅ map g :=
nat_iso.of_components (λ U, eq_to_iso (congr_fun (congr_arg functor.obj (congr_arg map h)) U) )
(by obviously)
lemma map_eq (f g : X ⟶ Y) (h : f = g) : map f = map g :=
by { unfold map, congr, ext, rw h, rw h, assumption' }
@[simp] lemma map_iso_refl (f : X ⟶ Y) (h) : map_iso f f h = iso.refl (map _) := rfl
@[simp] lemma map_iso_hom_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) :
(map_iso f g h).hom.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h)) U) :=
rfl
@[simp] lemma map_iso_inv_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) :
(map_iso f g h).inv.app U =
eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h.symm)) U) :=
rfl
/-- A homeomorphism of spaces gives an equivalence of categories of open sets. -/
@[simps] def map_map_iso {X Y : Top.{u}} (H : X ≅ Y) : opens Y ≌ opens X :=
{ functor := map H.hom,
inverse := map H.inv,
unit_iso := nat_iso.of_components (λ U, eq_to_iso (by simp [map, set.preimage_preimage]))
(by { intros _ _ _, simp }),
counit_iso := nat_iso.of_components (λ U, eq_to_iso (by simp [map, set.preimage_preimage]))
(by { intros _ _ _, simp }) }
end topological_space.opens
/--
An open map `f : X ⟶ Y` induces a functor `opens X ⥤ opens Y`.
-/
@[simps]
def is_open_map.functor {X Y : Top} {f : X ⟶ Y} (hf : is_open_map f) :
opens X ⥤ opens Y :=
{ obj := λ U, ⟨f '' U, hf U U.2⟩,
map := λ U V h, ⟨⟨set.image_subset _ h.down.down⟩⟩ }
/--
An open map `f : X ⟶ Y` induces an adjunction between `opens X` and `opens Y`.
-/
def is_open_map.adjunction {X Y : Top} {f : X ⟶ Y} (hf : is_open_map f) :
adjunction hf.functor (topological_space.opens.map f) :=
adjunction.mk_of_unit_counit
{ unit := { app := λ U, hom_of_le $ λ x hxU, ⟨x, hxU, rfl⟩ },
counit := { app := λ V, hom_of_le $ λ y ⟨x, hfxV, hxy⟩, hxy ▸ hfxV } }
instance is_open_map.functor_full_of_mono {X Y : Top} {f : X ⟶ Y} (hf : is_open_map f)
[H : mono f] : full hf.functor :=
{ preimage := λ U V i, hom_of_le (λ x hx, by
{ obtain ⟨y, hy, eq⟩ := i.le ⟨x, hx, rfl⟩, exact (Top.mono_iff_injective f).mp H eq ▸ hy }) }
instance is_open_map.functor_faithful {X Y : Top} {f : X ⟶ Y} (hf : is_open_map f) :
faithful hf.functor := {}
namespace topological_space.opens
open topological_space
@[simp] lemma open_embedding_obj_top {X : Top} (U : opens X) :
U.open_embedding.is_open_map.functor.obj ⊤ = U :=
by { ext1, exact set.image_univ.trans subtype.range_coe }
@[simp] lemma inclusion_map_eq_top {X : Top} (U : opens X) :
(opens.map U.inclusion).obj U = ⊤ :=
by { ext1, exact subtype.coe_preimage_self _ }
@[simp]
lemma adjunction_counit_app_self {X : Top} (U : opens X) :
U.open_embedding.is_open_map.adjunction.counit.app U = eq_to_hom (by simp) :=
by ext
lemma inclusion_top_functor (X : Top) :
(@opens.open_embedding X ⊤).is_open_map.functor =
map (inclusion_top_iso X).inv :=
begin
apply functor.hext, intro, abstract obj_eq { ext,
exact ⟨ λ ⟨⟨_,_⟩,h,rfl⟩, h, λ h, ⟨⟨x,trivial⟩,h,rfl⟩ ⟩ },
intros, apply subsingleton.helim, congr' 1,
iterate 2 {apply inclusion_top_functor.obj_eq},
end
end topological_space.opens
|
e031783b44c601e35d6e57eca3f60ed46b33f030 | 367134ba5a65885e863bdc4507601606690974c1 | /src/algebra/group/pi.lean | 04bdd13c7fa985a82d48146b9b7cc7d3033cb010 | [
"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 | 6,492 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Patrick Massot
-/
import data.pi
import tactic.pi_instances
import algebra.group.defs
import algebra.group.hom
/-!
# Pi instances for groups and monoids
This file defines instances for group, monoid, semigroup and related structures on Pi types.
-/
universes u v w
variable {I : Type u} -- The indexing type
variable {f : I → Type v} -- The family of types already equipped with instances
variables (x y : Π i, f i) (i : I)
namespace pi
@[to_additive]
instance semigroup [∀ i, semigroup $ f i] : semigroup (Π i : I, f i) :=
by refine_struct { mul := (*), .. }; tactic.pi_instance_derive_field
@[to_additive]
instance comm_semigroup [∀ i, comm_semigroup $ f i] : comm_semigroup (Π i : I, f i) :=
by refine_struct { mul := (*), .. }; tactic.pi_instance_derive_field
@[to_additive]
instance monoid [∀ i, monoid $ f i] : monoid (Π i : I, f i) :=
by refine_struct { one := (1 : Π i, f i), mul := (*), .. }; tactic.pi_instance_derive_field
@[to_additive]
instance comm_monoid [∀ i, comm_monoid $ f i] : comm_monoid (Π i : I, f i) :=
by refine_struct { one := (1 : Π i, f i), mul := (*), .. }; tactic.pi_instance_derive_field
@[to_additive]
instance div_inv_monoid [∀ i, div_inv_monoid $ f i] :
div_inv_monoid (Π i : I, f i) :=
{ div_eq_mul_inv := λ x y, funext (λ i, div_eq_mul_inv (x i) (y i)),
.. pi.monoid, .. pi.has_div, .. pi.has_inv }
@[to_additive]
instance group [∀ i, group $ f i] : group (Π i : I, f i) :=
by refine_struct { one := (1 : Π i, f i), mul := (*), inv := has_inv.inv, div := has_div.div, .. };
tactic.pi_instance_derive_field
@[to_additive]
instance comm_group [∀ i, comm_group $ f i] : comm_group (Π i : I, f i) :=
by refine_struct { one := (1 : Π i, f i), mul := (*), inv := has_inv.inv, div := has_div.div, .. };
tactic.pi_instance_derive_field
@[to_additive add_left_cancel_semigroup]
instance left_cancel_semigroup [∀ i, left_cancel_semigroup $ f i] :
left_cancel_semigroup (Π i : I, f i) :=
by refine_struct { mul := (*) }; tactic.pi_instance_derive_field
@[to_additive add_right_cancel_semigroup]
instance right_cancel_semigroup [∀ i, right_cancel_semigroup $ f i] :
right_cancel_semigroup (Π i : I, f i) :=
by refine_struct { mul := (*) }; tactic.pi_instance_derive_field
instance mul_zero_class [∀ i, mul_zero_class $ f i] :
mul_zero_class (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), mul := (*), .. }; tactic.pi_instance_derive_field
instance monoid_with_zero [∀ i, monoid_with_zero $ f i] :
monoid_with_zero (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), one := (1 : Π i, f i), mul := (*), .. };
tactic.pi_instance_derive_field
instance comm_monoid_with_zero [∀ i, comm_monoid_with_zero $ f i] :
comm_monoid_with_zero (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), one := (1 : Π i, f i), mul := (*), .. };
tactic.pi_instance_derive_field
section instance_lemmas
open function
variables {α β γ : Type*}
@[simp, to_additive] lemma const_one [has_one β] : const α (1 : β) = 1 := rfl
@[simp, to_additive] lemma comp_one [has_one β] {f : β → γ} : f ∘ 1 = const α (f 1) := rfl
@[simp, to_additive] lemma one_comp [has_one γ] {f : α → β} : (1 : β → γ) ∘ f = 1 := rfl
end instance_lemmas
end pi
section monoid_hom
variables (f) [Π i, monoid (f i)]
/-- Evaluation of functions into an indexed collection of monoids at a point is a monoid
homomorphism. -/
@[to_additive "Evaluation of functions into an indexed collection of additive monoids at a point
is an additive monoid homomorphism."]
def monoid_hom.apply (i : I) : (Π i, f i) →* f i :=
{ to_fun := λ g, g i,
map_one' := rfl,
map_mul' := λ x y, rfl, }
@[simp, to_additive]
lemma monoid_hom.apply_apply (i : I) (g : Π i, f i) :
(monoid_hom.apply f i) g = g i := rfl
/-- Coercion of a `monoid_hom` into a function is itself a `monoid_hom`.
See also `monoid_hom.eval`. -/
@[simps, to_additive "Coercion of an `add_monoid_hom` into a function is itself a `add_monoid_hom`.
See also `add_monoid_hom.eval`. "]
def monoid_hom.coe_fn (α β : Type*) [monoid α] [comm_monoid β] : (α →* β) →* (α → β) :=
{ to_fun := λ g, g,
map_one' := rfl,
map_mul' := λ x y, rfl, }
end monoid_hom
section single
variables [decidable_eq I]
open pi
variables (f)
/-- The zero-preserving homomorphism including a single value
into a dependent family of values, as functions supported at a point.
This is the `zero_hom` version of `pi.single`. -/
@[simps] def zero_hom.single [Π i, has_zero $ f i] (i : I) : zero_hom (f i) (Π i, f i) :=
{ to_fun := single i,
map_zero' := function.update_eq_self i 0 }
/-- The additive monoid homomorphism including a single additive monoid
into a dependent family of additive monoids, as functions supported at a point.
This is the `add_monoid_hom` version of `pi.single`. -/
@[simps] def add_monoid_hom.single [Π i, add_monoid $ f i] (i : I) : f i →+ Π i, f i :=
{ to_fun := single i,
map_add' := λ x y, funext $ λ j, begin
refine (apply_single₂ _ (λ _, _) i x y j).symm,
exact zero_add 0,
end,
.. (zero_hom.single f i) }
/-- The multiplicative homomorphism including a single `monoid_with_zero`
into a dependent family of monoid_with_zeros, as functions supported at a point.
This is the `mul_hom` version of `pi.single`. -/
@[simps] def mul_hom.single [Π i, monoid_with_zero $ f i] (i : I) : mul_hom (f i) (Π i, f i) :=
{ to_fun := single i,
map_mul' := λ x y, funext $ λ j, begin
refine (apply_single₂ _ (λ _, _) i x y j).symm,
exact zero_mul 0,
end, }
variables {f}
@[simp]
lemma pi.single_zero [Π i, has_zero $ f i] (i : I) :
single i (0 : f i) = 0 :=
(zero_hom.single f i).map_zero
lemma pi.single_add [Π i, add_monoid $ f i] (i : I) (x y : f i) :
single i (x + y) = single i x + single i y :=
(add_monoid_hom.single f i).map_add x y
lemma pi.single_neg [Π i, add_group $ f i] (i : I) (x : f i) :
single i (-x) = -single i x :=
(add_monoid_hom.single f i).map_neg x
lemma pi.single_sub [Π i, add_group $ f i] (i : I) (x y : f i) :
single i (x - y) = single i x - single i y :=
(add_monoid_hom.single f i).map_sub x y
lemma pi.single_mul [Π i, monoid_with_zero $ f i] (i : I) (x y : f i) :
single i (x * y) = single i x * single i y :=
(mul_hom.single f i).map_mul x y
end single
|
b756900a43dae09883c5e9013618f8fb54cd58ba | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/linear_algebra/contraction.lean | 107f32aa8ded7d0be26f14a379d05d56d113aad1 | [] | 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,501 | lean | /-
Copyright (c) 2020 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.linear_algebra.dual
import Mathlib.PostPort
universes u v
namespace Mathlib
/-!
# Contractions
Given modules $M, N$ over a commutative ring $R$, this file defines the natural linear maps:
$M^* \otimes M \to R$, $M \otimes M^* \to R$, and $M^* \otimes N → Hom(M, N)$, as well as proving
some basic properties of these maps.
## Tags
contraction, dual module, tensor product
-/
/-- The natural left-handed pairing between a module and its dual. -/
def contract_left (R : Type u) (M : Type v) [comm_ring R] [add_comm_group M] [module R M] : linear_map R (tensor_product R (module.dual R M) M) R :=
linear_map.to_fun (tensor_product.uncurry R (module.dual R M) M R) linear_map.id
/-- The natural right-handed pairing between a module and its dual. -/
def contract_right (R : Type u) (M : Type v) [comm_ring R] [add_comm_group M] [module R M] : linear_map R (tensor_product R M (module.dual R M)) R :=
linear_map.to_fun (tensor_product.uncurry R M (module.dual R M) R) (linear_map.flip linear_map.id)
/-- The natural map associating a linear map to the tensor product of two modules. -/
def dual_tensor_hom (R : Type u) (M : Type v) (N : Type v) [comm_ring R] [add_comm_group M] [add_comm_group N] [module R M] [module R N] : linear_map R (tensor_product R (module.dual R M) N) (linear_map R M N) :=
let M' : Type (max v u) := module.dual R M;
coe_fn (tensor_product.uncurry R M' N (linear_map R M N)) linear_map.smul_rightₗ
@[simp] theorem contract_left_apply {R : Type u} {M : Type v} [comm_ring R] [add_comm_group M] [module R M] (f : module.dual R M) (m : M) : coe_fn (contract_left R M) (tensor_product.tmul R f m) = coe_fn f m :=
tensor_product.uncurry_apply linear_map.id f m
@[simp] theorem contract_right_apply {R : Type u} {M : Type v} [comm_ring R] [add_comm_group M] [module R M] (f : module.dual R M) (m : M) : coe_fn (contract_right R M) (tensor_product.tmul R m f) = coe_fn f m :=
tensor_product.uncurry_apply (linear_map.flip linear_map.id) m f
@[simp] theorem dual_tensor_hom_apply {R : Type u} {M : Type v} {N : Type v} [comm_ring R] [add_comm_group M] [add_comm_group N] [module R M] [module R N] (f : module.dual R M) (m : M) (n : N) : coe_fn (coe_fn (dual_tensor_hom R M N) (tensor_product.tmul R f n)) m = coe_fn f m • n := sorry
|
f96ce062abba75510a53ba02bd4a19df20cb19dd | 1437b3495ef9020d5413178aa33c0a625f15f15f | /category_theory/category.lean | fdbe473ea2fa681ba80eb274f7870134c09e06f7 | [
"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 | 6,859 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Stephen Morgan, Scott Morrison, Johannes Hölzl, Reid Barton
Defines a category, as a typeclass parametrised by the type of objects.
Introduces notations
`X ⟶ Y` for the morphism spaces,
`f ≫ g` for composition in the 'arrows' convention.
Users may like to add `f ⊚ g` for composition in the standard convention, using
```
local notation f ` ⊚ `:80 g:80 := category.comp g f -- type as \oo
```
-/
import tactic.restate_axiom
import tactic.replacer
import tactic.interactive
import tactic.tidy
namespace category_theory
universes v u -- The order in this declaration matters: v often needs to be explicitly specified while u often can be omitted
/-
The propositional fields of `category` are annotated with the auto_param `obviously`,
which is defined here as a
[`replacer` tactic](https://github.com/leanprover/mathlib/blob/master/docs/tactics.md#def_replacer).
We then immediately set up `obviously` to call `tidy`. Later, this can be replaced with more
powerful tactics.
-/
def_replacer obviously
@[obviously] meta def obviously' := tactic.tidy
/--
The typeclass `category C` describes morphisms associated to objects of type `C`.
The universe levels of the objects and morphisms are unconstrained, and will often need to be
specified explicitly, as `category.{v} C`. (See also `large_category` and `small_category`.)
-/
class category (obj : Type u) : Type (max u (v+1)) :=
(hom : obj → obj → Type v)
(infixr ` ⟶ `:10 := hom)
(id : Π X : obj, X ⟶ X)
(notation `𝟙` := id)
(comp : Π {X Y Z : obj}, (X ⟶ Y) → (Y ⟶ Z) → (X ⟶ Z))
(infixr ` ≫ `:80 := comp)
(id_comp' : ∀ {X Y : obj} (f : X ⟶ Y), 𝟙 X ≫ f = f . obviously)
(comp_id' : ∀ {X Y : obj} (f : X ⟶ Y), f ≫ 𝟙 Y = f . obviously)
(assoc' : ∀ {W X Y Z : obj} (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z),
(f ≫ g) ≫ h = f ≫ (g ≫ h) . obviously)
notation `𝟙` := category.id -- type as \b1
infixr ` ≫ `:80 := category.comp -- type as \gg
infixr ` ⟶ `:10 := category.hom -- type as \h
-- `restate_axiom` is a command that creates a lemma from a structure field,
-- discarding any auto_param wrappers from the type.
-- (It removes a backtick from the name, if it finds one, and otherwise adds "_lemma".)
restate_axiom category.id_comp'
restate_axiom category.comp_id'
restate_axiom category.assoc'
attribute [simp] category.id_comp category.comp_id category.assoc
attribute [trans] category.comp
lemma category.assoc_symm {C : Type u} [category.{v} C] {W X Y Z : C} (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) :
f ≫ (g ≫ h) = (f ≫ g) ≫ h :=
by rw ←category.assoc
/--
A `large_category` has objects in one universe level higher than the universe level of
the morphisms. It is useful for examples such as the category of types, or the category
of groups, etc.
-/
abbreviation large_category (C : Type (u+1)) : Type (u+1) := category.{u} C
/--
A `small_category` has objects and morphisms in the same universe level.
-/
abbreviation small_category (C : Type u) : Type (u+1) := category.{u} C
structure bundled (c : Type u → Type v) :=
(α : Type u)
(str : c α)
instance (c : Type u → Type v) : has_coe_to_sort (bundled c) :=
{ S := Type u, coe := bundled.α }
def mk_ob {c : Type u → Type v} (α : Type u) [str : c α] : bundled c :=
@bundled.mk c α str
/-- `concrete_category hom` collects the evidence that a type constructor `c` and a morphism
predicate `hom` can be thought of as a concrete category.
In a typical example, `c` is the type class `topological_space` and `hom` is `continuous`. -/
structure concrete_category {c : Type u → Type v}
(hom : out_param $ ∀{α β : Type u}, c α → c β → (α → β) → Prop) :=
(hom_id : ∀{α} (ia : c α), hom ia ia id)
(hom_comp : ∀{α β γ} (ia : c α) (ib : c β) (ic : c γ) {f g},
hom ia ib f → hom ib ic g → hom ia ic (g ∘ f))
attribute [class] concrete_category
instance {c : Type u → Type v} (hom : ∀{α β : Type u}, c α → c β → (α → β) → Prop)
[h : concrete_category @hom] : category (bundled c) :=
{ hom := λa b, subtype (hom a.2 b.2),
id := λa, ⟨@id a.1, h.hom_id a.2⟩,
comp := λa b c f g, ⟨g.1 ∘ f.1, h.hom_comp a.2 b.2 c.2 f.2 g.2⟩ }
@[simp] lemma concrete_category_id
{c : Type u → Type v} (hom : ∀{α β : Type u}, c α → c β → (α → β) → Prop)
[h : concrete_category @hom] (X : bundled c) : subtype.val (𝟙 X) = id := rfl
@[simp] lemma concrete_category_comp
{c : Type u → Type v} (hom : ∀{α β : Type u}, c α → c β → (α → β) → Prop)
[h : concrete_category @hom] {X Y Z : bundled c} (f : X ⟶ Y) (g : Y ⟶ Z) :
subtype.val (f ≫ g) = g.val ∘ f.val := rfl
instance {c : Type u → Type v} (hom : ∀{α β : Type u}, c α → c β → (α → β) → Prop)
[h : concrete_category @hom] {R S : bundled c} : has_coe_to_fun (R ⟶ S) :=
{ F := λ f, R → S,
coe := λ f, f.1 }
@[simp] lemma bundled_hom_coe
{c : Type u → Type v} (hom : ∀{α β : Type u}, c α → c β → (α → β) → Prop)
[h : concrete_category @hom] {R S : bundled c} (val : R → S) (prop) (r : R) :
(⟨val, prop⟩ : R ⟶ S) r = val r := rfl
section
variables {C : Type u} [𝒞 : category.{v} C] {X Y Z : C}
include 𝒞
class epi (f : X ⟶ Y) : Prop :=
(left_cancellation : Π {Z : C} (g h : Y ⟶ Z) (w : f ≫ g = f ≫ h), g = h)
class mono (f : X ⟶ Y) : Prop :=
(right_cancellation : Π {Z : C} (g h : Z ⟶ X) (w : g ≫ f = h ≫ f), g = h)
@[simp] lemma cancel_epi (f : X ⟶ Y) [epi f] (g h : Y ⟶ Z) : (f ≫ g = f ≫ h) ↔ g = h :=
⟨ λ p, epi.left_cancellation g h p, begin intro a, subst a end ⟩
@[simp] lemma cancel_mono (f : X ⟶ Y) [mono f] (g h : Z ⟶ X) : (g ≫ f = h ≫ f) ↔ g = h :=
⟨ λ p, mono.right_cancellation g h p, begin intro a, subst a end ⟩
end
section
variable (C : Type u)
variable [category.{v} C]
universe u'
instance ulift_category : category.{v} (ulift.{u'} C) :=
{ hom := λ X Y, (X.down ⟶ Y.down),
id := λ X, 𝟙 X.down,
comp := λ _ _ _ f g, f ≫ g }
-- We verify that this previous instance can lift small categories to large categories.
example (D : Type u) [small_category D] : large_category (ulift.{u+1} D) := by apply_instance
end
variables (α : Type u)
instance [preorder α] : small_category α :=
{ hom := λ U V, ulift (plift (U ≤ V)),
id := λ X, ⟨ ⟨ le_refl X ⟩ ⟩,
comp := λ X Y Z f g, ⟨ ⟨ le_trans f.down.down g.down.down ⟩ ⟩ }
section
variables {C : Type u} [𝒞 : category.{v} C]
include 𝒞
def End (X : C) := X ⟶ X
instance {X : C} : monoid (End X) := by refine { one := 𝟙 X, mul := λ x y, x ≫ y, .. } ; obviously
end
end category_theory
|
1c05e015a371ff9436ad4ca8df20ddb65ea32d9a | 206422fb9edabf63def0ed2aa3f489150fb09ccb | /src/order/ideal.lean | 88894aa49b756ca83e14be65122f5a7ff4147178 | [
"Apache-2.0"
] | permissive | hamdysalah1/mathlib | b915f86b2503feeae268de369f1b16932321f097 | 95454452f6b3569bf967d35aab8d852b1ddf8017 | refs/heads/master | 1,677,154,116,545 | 1,611,797,994,000 | 1,611,797,994,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,003 | lean | /-
Copyright (c) 2020 David Wärn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: David Wärn
-/
import order.basic
import data.equiv.encodable.basic
/-!
# Order ideals, cofinal sets, and the Rasiowa–Sikorski lemma
## Main definitions
Throughout this file, `P` is at least a preorder, but some sections require more
structure, such as a bottom element, a top element, or a join-semilattice structure.
- `ideal P`: the type of upward directed, downward closed subsets of `P`.
Dual to the notion of a filter on a preorder.
- `cofinal P`: the type of subsets of `P` containing arbitrarily large elements.
Dual to the notion of 'dense set' used in forcing.
- `ideal_of_cofinals p 𝒟`, where `p : P`, and `𝒟` is a countable family of cofinal
subsets of P: an ideal in `P` which contains `p` and intersects every set in `𝒟`.
## References
- https://en.wikipedia.org/wiki/Ideal_(order_theory)
- https://en.wikipedia.org/wiki/Cofinal_(mathematics)
- https://en.wikipedia.org/wiki/Rasiowa–Sikorski_lemma
Note that for the Rasiowa–Sikorski lemma, Wikipedia uses the opposite ordering on `P`,
in line with most presentations of forcing.
## Tags
ideal, cofinal, dense, countable, generic
-/
namespace order
variables {P : Type*}
/-- An ideal on a preorder `P` is a subset of `P` that is
- nonempty
- upward directed
- downward closed. -/
structure ideal (P) [preorder P] :=
(carrier : set P)
(nonempty : carrier.nonempty)
(directed : directed_on (≤) carrier)
(mem_of_le : ∀ {x y : P}, x ≤ y → y ∈ carrier → x ∈ carrier)
namespace ideal
section preorder
variables [preorder P] {x : P} {I J : ideal P}
/-- The smallest ideal containing a given element. -/
def principal (p : P) : ideal P :=
{ carrier := { x | x ≤ p },
nonempty := ⟨p, le_refl _⟩,
directed := λ x hx y hy, ⟨p, le_refl _, hx, hy⟩,
mem_of_le := λ x y hxy hy, le_trans hxy hy, }
instance [inhabited P] : inhabited (ideal P) :=
⟨ideal.principal $ default P⟩
/-- An ideal of `P` can be viewed as a subset of `P`. -/
instance : has_coe (ideal P) (set P) := ⟨carrier⟩
/-- For the notation `x ∈ I`. -/
instance : has_mem P (ideal P) := ⟨λ x I, x ∈ (I : set P)⟩
/-- Two ideals are equal when their underlying sets are equal. -/
@[ext] lemma ext : ∀ (I J : ideal P), (I : set P) = J → I = J
| ⟨_, _, _, _⟩ ⟨_, _, _, _⟩ rfl := rfl
/-- The partial ordering by subset inclusion, inherited from `set P`. -/
instance : partial_order (ideal P) := partial_order.lift coe ext
@[trans] lemma mem_of_mem_of_le : x ∈ I → I ≤ J → x ∈ J :=
@set.mem_of_mem_of_subset P x I J
@[simp] lemma principal_le_iff : principal x ≤ I ↔ x ∈ I :=
⟨λ (h : ∀ {y}, y ≤ x → y ∈ I), h (le_refl x),
λ h_mem y (h_le : y ≤ x), I.mem_of_le h_le h_mem⟩
end preorder
section order_bot
variables [order_bot P] {I : ideal P}
/-- A specific witness of `I.nonempty` when `P` has a bottom element. -/
@[simp] lemma bot_mem : ⊥ ∈ I :=
I.mem_of_le bot_le I.nonempty.some_mem
/-- There is a bottom ideal when `P` has a bottom element. -/
instance : order_bot (ideal P) :=
{ bot := principal ⊥,
bot_le := by simp,
.. ideal.partial_order }
end order_bot
/-- There is a top ideal when `P` has a top element. -/
instance {P} [order_top P] : order_top (ideal P) :=
{ top := principal ⊤,
le_top := λ I x h, le_top,
.. ideal.partial_order }
section semilattice_sup
variables [semilattice_sup P] {x y : P} {I : ideal P}
/-- A specific witness of `I.directed` when `P` has joins. -/
lemma sup_mem (x y ∈ I) : x ⊔ y ∈ I :=
let ⟨z, h_mem, hx, hy⟩ := I.directed x (by assumption) y (by assumption) in
I.mem_of_le (sup_le hx hy) h_mem
@[simp] lemma sup_mem_iff : x ⊔ y ∈ I ↔ x ∈ I ∧ y ∈ I :=
⟨λ h, ⟨I.mem_of_le le_sup_left h, I.mem_of_le le_sup_right h⟩,
λ h, sup_mem x y h.left h.right⟩
end semilattice_sup
end ideal
/-- For a preorder `P`, `cofinal P` is the type of subsets of `P`
containing arbitrarily large elements. They are the dense sets in
the topology whose open sets are terminal segments. -/
structure cofinal (P) [preorder P] :=
(carrier : set P)
(mem_gt : ∀ x : P, ∃ y ∈ carrier, x ≤ y)
namespace cofinal
variables [preorder P]
instance : inhabited (cofinal P) :=
⟨{ carrier := set.univ, mem_gt := λ x, ⟨x, trivial, le_refl _⟩}⟩
instance : has_mem P (cofinal P) := ⟨λ x D, x ∈ D.carrier⟩
variables (D : cofinal P) (x : P)
/-- A (noncomputable) element of a cofinal set lying above a given element. -/
noncomputable def above : P := classical.some $ D.mem_gt x
lemma above_mem : D.above x ∈ D :=
exists.elim (classical.some_spec $ D.mem_gt x) $ λ a _, a
lemma le_above : x ≤ D.above x :=
exists.elim (classical.some_spec $ D.mem_gt x) $ λ _ b, b
end cofinal
section ideal_of_cofinals
variables [preorder P] (p : P) {ι : Type*} [encodable ι] (𝒟 : ι → cofinal P)
/-- Given a starting point, and a countable family of cofinal sets,
this is an increasing sequence that intersects each cofinal set. -/
noncomputable def sequence_of_cofinals : ℕ → P
| 0 := p
| (n+1) := match encodable.decode ι n with
| none := sequence_of_cofinals n
| some i := (𝒟 i).above (sequence_of_cofinals n)
end
lemma sequence_of_cofinals.monotone : monotone (sequence_of_cofinals p 𝒟) :=
by { apply monotone_of_monotone_nat, intros n, dunfold sequence_of_cofinals,
cases encodable.decode ι n, { refl }, { apply cofinal.le_above }, }
lemma sequence_of_cofinals.encode_mem (i : ι) :
sequence_of_cofinals p 𝒟 (encodable.encode i + 1) ∈ 𝒟 i :=
by { dunfold sequence_of_cofinals, rw encodable.encodek, apply cofinal.above_mem, }
/-- Given an element `p : P` and a family `𝒟` of cofinal subsets of a preorder `P`,
indexed by a countable type, `ideal_of_cofinals p 𝒟` is an ideal in `P` which
- contains `p`, according to `mem_ideal_of_cofinals p 𝒟`, and
- intersects every set in `𝒟`, according to `cofinal_meets_ideal_of_cofinals p 𝒟`.
This proves the Rasiowa–Sikorski lemma. -/
def ideal_of_cofinals : ideal P :=
{ carrier := { x : P | ∃ n, x ≤ sequence_of_cofinals p 𝒟 n },
nonempty := ⟨p, 0, le_refl _⟩,
directed := λ x ⟨n, hn⟩ y ⟨m, hm⟩,
⟨_, ⟨max n m, le_refl _⟩,
le_trans hn $ sequence_of_cofinals.monotone p 𝒟 (le_max_left _ _),
le_trans hm $ sequence_of_cofinals.monotone p 𝒟 (le_max_right _ _) ⟩,
mem_of_le := λ x y hxy ⟨n, hn⟩, ⟨n, le_trans hxy hn⟩, }
lemma mem_ideal_of_cofinals : p ∈ ideal_of_cofinals p 𝒟 := ⟨0, le_refl _⟩
/-- `ideal_of_cofinals p 𝒟` is `𝒟`-generic. -/
lemma cofinal_meets_ideal_of_cofinals (i : ι) : ∃ x : P, x ∈ 𝒟 i ∧ x ∈ ideal_of_cofinals p 𝒟 :=
⟨_, sequence_of_cofinals.encode_mem p 𝒟 i, _, le_refl _⟩
end ideal_of_cofinals
end order
|
08014db2fc80e9cad627365dbd51a5fe22025027 | 6ae186a0c6ab366b39397ec9250541c9d5aeb023 | /src/category_theory/adjunctions/initial.lean | 573733a44164302eb3c9d70bff571fd37a0c6d21 | [] | no_license | ThanhPhamPhuong/lean-category-theory | 0d5c4fe1137866b4fe29ec2753d99aa0d0667881 | 968a29fe7c0b20e10d8a27e120aca8ddc184e1ea | refs/heads/master | 1,587,206,682,489 | 1,544,045,056,000 | 1,544,045,056,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 733 | lean | -- Copyright (c) 2017 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Scott Morrison
import category_theory.adjunctions
import category_theory.universal.comma_categories
open category_theory
namespace categories.adjunctions
-- PROJECT
-- def unit_component_in_slice_category
-- {C D : Category} {L : Functor C D} {R : Functor D C} (A : Adjunction L R) (X : C.Obj)
-- : (SliceCategory X).Obj := sorry
-- def unit_components_initial_in_slice_category
-- {C D : Category} {L : Functor C D} {R : Functor D C} (A : Adjunction L R) (X : C.Obj)
-- : is_initial (unit_component_in_slice_category A X) := sorry
-- and so on
end categories.adjunctions |
0b0292155f48990f1aff1590c99f163beb41a482 | 737dc4b96c97368cb66b925eeea3ab633ec3d702 | /stage0/src/Lean/Elab/Tactic/Conv/Rewrite.lean | 9f8803b35ca1b8ad86ae441804627c0cf33ebb55 | [
"Apache-2.0"
] | permissive | Bioye97/lean4 | 1ace34638efd9913dc5991443777b01a08983289 | bc3900cbb9adda83eed7e6affeaade7cfd07716d | refs/heads/master | 1,690,589,820,211 | 1,631,051,000,000 | 1,631,067,598,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 951 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Tactic.Rewrite
import Lean.Elab.Tactic.Rewrite
import Lean.Elab.Tactic.Conv.Basic
namespace Lean.Elab.Tactic.Conv
open Meta
def evalRewriteCore (mode : TransparencyMode) : Tactic := fun stx =>
withRWRulesSeq stx[0] stx[1] fun symm term => do
Term.withSynthesize <| withMainContext do
let e ← elabTerm term none true
let r ← rewrite (← getMainGoal) (← getLhs) e symm (mode := mode)
updateLhs r.eNew r.eqProof
replaceMainGoal ((← getMainGoal) :: r.mvarIds)
@[builtinTactic Lean.Parser.Tactic.Conv.rewrite] def evalRewrite : Tactic :=
evalRewriteCore TransparencyMode.instances
@[builtinTactic Lean.Parser.Tactic.Conv.erewrite] def evalERewrite : Tactic :=
evalRewriteCore TransparencyMode.default
end Lean.Elab.Tactic.Conv
|
c23183083b910ce76f0b6554f78eb133c5dc8af3 | a4673261e60b025e2c8c825dfa4ab9108246c32e | /tests/lean/run/lean3_zulip_issues_1.lean | ec273095cead2976948945f5dbe198458b06a6ad | [
"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 | 1,264 | lean | example : Prop := ∀ n, (n:Nat) + n = n.succ
example : Prop := ∀ n, n.succ = (n:Nat) + n
example : Prop := ∀ n, (n:Nat) + n.succ = n
example : Prop := ∀ n, n.succ + (n:Nat) = n
example : Prop := ∀ n, (n.succ:Nat) + n = n
example : Prop := ∀ n, (n:Nat).succ + n = n
def fib: Nat → Nat
| 0 => 0
| 1 => 1
| n + 2 => fib n + fib (n + 1)
theorem fib50Eq : fib 50 = 12586269025 :=
rfl
inductive type : Type
| A : type
| B : type
inductive val : type → Type
| cA : val type.A
| cB : val type.B
inductive wrap : Type
| val : ∀ {t : type}, (val t) → wrap
def f : wrap → Nat
| wrap.val val.cA => 1
| _ => 1
example (a : Nat) : True := by
have ∀ n, n ≥ 0 → a ≤ a from fun _ _ => Nat.leRefl ..
exact True.intro
example (ᾰ : Nat) : True := by
have ∀ n, n ≥ 0 → ᾰ ≤ ᾰ from fun _ _ => Nat.leRefl ..
exact True.intro
inductive Vec.{u} (α : Type u) : Nat → Type u
| nil : Vec α 0
| cons : α → {n : Nat} → Vec α n → Vec α (n+1)
constant Vars : Type
structure Lang :=
(funcs : Nat → Type)
(consts : Type)
inductive Term (L : Lang) : Type
| const_term : L.consts → Term L
| var_term : Vars → Term L
| func_term (n : Nat) (f : L.funcs n) (v : Vec (Term L) n) : Term L
|
535f0fd079bb74ffd051273bf1b2e40c7e78ae17 | 1b8f093752ba748c5ca0083afef2959aaa7dace5 | /src/category_theory/homological_algebra/chain_complex.lean | 17a0b4cc1d8d5487df7c2361443a40ea9557f7c3 | [] | no_license | khoek/lean-category-theory | 7ec4cda9cc64a5a4ffeb84712ac7d020dbbba386 | 63dcb598e9270a3e8b56d1769eb4f825a177cd95 | refs/heads/master | 1,585,251,725,759 | 1,539,344,445,000 | 1,539,344,445,000 | 145,281,070 | 0 | 0 | null | 1,534,662,376,000 | 1,534,662,376,000 | null | UTF-8 | Lean | false | false | 2,661 | lean | /- Attempting to "live-formalise", from the PhD students reading group meeting on June 24 2018, starting at the beginning of homological algebra. -/
import category_theory.category
import category_theory.universal.zero
import category_theory.limits.products
universes u₁ v₁ u₂ v₂
open category_theory
open category_theory.limits
class Ab_category (C: Type u₁) extends category.{u₁ v₁} C := --- we really need to setup enriched categories
(hom_groups : Π X Y : C, comm_group (X ⟶ Y))
(compose_is_homomorphism : Π X Y Z : C,
begin
haveI : comm_group (X ⟶ Y) := by apply_instance, -- we can get these, but not the product automatically?
haveI : comm_group ((X ⟶ Y) × (Y ⟶ Z)) := by sorry, -- surely this should just appear magically.
exact is_group_hom (λ p : (X ⟶ Y) × (Y ⟶ Z), p.1 ≫ p.2)
end)
-- variables (C : Type u₁) [𝒞 : Ab_category.{u₁ v₁} C] (D : Type u₂) [𝒟 : Ab_category.{u₂ v₂} D]
-- def additive_functor extends Functor C D := sorry
-- TODO should be has_finite_products, but that doesn't exist yet
class additive_category (C : Type u₁) extends (Ab_category.{u₁ v₁} C), (has_zero_object.{u₁ v₁} C), (has_products.{u₁ v₁} C)
/- Examples -/
/- Field is not additve, it doesn't have a zero object, or all products. -/
/- Abelian groups = Z-mod is an additive category. -/
/- mod-R, Vec_F, even dimensional vector spaces, are all additive categories -/
structure chain_complex (C : Type u₁) [additive_category.{u₁ v₁} C] : Type (max u₁ v₁) :=
(chain_objects : ℤ → C)
(differentials : Π n : ℤ, (chain_objects n) ⟶ (chain_objects (n+1)))
-- squares to zero!
structure chain_map {C : Type u₁} [additive_category C] (M N : chain_complex C) :=
(component : Π n : ℤ, M.chain_objects n ⟶ N.chain_objects n)
(commutes : Π n : ℤ, component n ≫ N.differentials n = M.differentials n ≫ component (n+1))
class abelian_category (C : Type u₁) extends (additive_category.{u₁ v₁} C)/-, (has_Equalizers.{u₁ u₂} C), (has_Coequalizers.{u₁ u₂} C)-/ .
-- TODO: monics are regular
instance category_of_chain_complexes {C : Type u₁} [additive_category.{u₁ v₁} C] : category.{(max u₁ v₁)} (chain_complex C) :=
{ hom := λ M N, chain_map M N,
comp := sorry,
id := sorry,
id_comp' := sorry, comp_id' := sorry, assoc' := sorry
}
instance chain_complexes_are_abelian_too (C : Type u₁) [abelian_category.{u₁ v₁} C] : abelian_category (chain_complex C) := sorry
-- mostly, work componentwise
-- cycles, boundaries, homology, quasi-isomorphism
-- Example: singular chains in a topological space
|
33396a08bba7c80041917af57b0b86f3695a80bc | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/localcoe.lean | e7748e4a240523dcee29871709a08fa4f59914e3 | [
"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 | 229 | lean | open nat
section
inductive NatA :=
| a : NatA
| s : NatA → NatA
open NatA
definition foo (n : nat) : NatA :=
nat.rec_on n a (λ n' r, s r)
local attribute foo [coercion]
check let a : nat := 10 in s a
end
|
9987749692aa3bf81be86334a50d8e3c0af8551d | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Meta/KExprMap.lean | d57b33df7ddd38d0b14f974dd131b8521672c3f0 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | leanprover/lean4 | 4bdf9790294964627eb9be79f5e8f6157780b4cc | f1f9dc0f2f531af3312398999d8b8303fa5f096b | refs/heads/master | 1,693,360,665,786 | 1,693,350,868,000 | 1,693,350,868,000 | 129,571,436 | 2,827 | 311 | Apache-2.0 | 1,694,716,156,000 | 1,523,760,560,000 | Lean | UTF-8 | Lean | false | false | 1,557 | lean | /-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
import Lean.HeadIndex
import Lean.Meta.Basic
namespace Lean.Meta
/--
A mapping that indentifies definitionally equal expressions.
We implement it as a mapping from `HeadIndex` to `AssocList Expr α`.
Remark: this map may be quite inefficient if there are many `HeadIndex` collisions.
-/
structure KExprMap (α : Type) where
map : PHashMap HeadIndex (AssocList Expr α) := {}
deriving Inhabited
/-- Return `some v` if there is an entry `e ↦ v` in `m`. -/
def KExprMap.find? (m : KExprMap α) (e : Expr) : MetaM (Option α) := do
match m.map.find? e.toHeadIndex with
| none => return none
| some ps =>
for ⟨e', a⟩ in ps do
if (← isDefEq e e') then
return some a
return none
private def updateList (ps : AssocList Expr α) (e : Expr) (v : α) : MetaM (AssocList Expr α) := do
match ps with
| AssocList.nil => return AssocList.cons e v ps
| AssocList.cons e' v' ps =>
if (← isDefEq e e') then
return AssocList.cons e v ps
else
return AssocList.cons e' v' (← updateList ps e v)
/-- Insert `e ↦ v` into `m` -/
def KExprMap.insert (m : KExprMap α) (e : Expr) (v : α) : MetaM (KExprMap α) :=
let k := e.toHeadIndex
match m.map.find? k with
| none => return { map := m.map.insert k (AssocList.cons e v AssocList.nil) }
| some ps => return { map := m.map.insert k (← updateList ps e v) }
end Lean.Meta
|
1e89f2c791457fcf56a76bc029459d153ec7b355 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/topology/algebra/uniform_convergence.lean | 461c976b2f7186474ca6236c558eb7e8559e3cfc | [
"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 | 3,856 | lean | /-
Copyright (c) 2022 Anatole Dedecker. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anatole Dedecker
-/
import topology.uniform_space.uniform_convergence_topology
import topology.algebra.uniform_group
/-!
# Algebraic facts about the topology of uniform convergence
This file contains algebraic compatibility results about the uniform structure of uniform
convergence / `𝔖`-convergence. They will mostly be useful for defining strong topologies on the
space of continuous linear maps between two topological vector spaces.
## Main statements
* `uniform_convergence.uniform_group` : if `G` is a uniform group, then the uniform structure of
uniform convergence makes `α → G` a uniform group
* `uniform_convergence_on.uniform_group` : if `G` is a uniform group, then the uniform structure of
`𝔖`-convergence, for any `𝔖 : set (set α)`, makes `α → G` a uniform group
## TODO
* Let `E` be a TVS, `𝔖 : set (set α)` and `H` a submodule of `α → E`. If the image of any `S ∈ 𝔖`
by any `u ∈ H` is bounded (in the sense of `bornology.is_vonN_bounded`), then `H`, equipped with
the topology of `𝔖`-convergence, is a TVS.
## References
* [N. Bourbaki, *General Topology, Chapter X*][bourbaki1966]
## Tags
uniform convergence, strong dual
-/
section group
variables {α G : Type*} [group G] [uniform_space G] [uniform_group G] {𝔖 : set $ set α}
local attribute [-instance] Pi.uniform_space
/-- If `G` is a uniform group, then the uniform structure of uniform convergence makes `α → G`
a uniform group as well. -/
@[to_additive "If `G` is a uniform additive group, then the uniform structure of uniform
convergence makes `α → G` a uniform additive group as well."]
protected lemma uniform_convergence.uniform_group :
@uniform_group (α → G) (uniform_convergence.uniform_space α G) _ :=
begin
-- Since `(/) : G × G → G` is uniformly continuous,
-- `uniform_convergence.postcomp_uniform_continuous` tells us that
-- `((/) ∘ —) : (α → G × G) → (α → G)` is uniformly continuous too. By precomposing with
-- `uniform_convergence.uniform_equiv_prod_arrow`, this gives that
-- `(/) : (α → G) × (α → G) → (α → G)` is also uniformly continuous
letI : uniform_space (α → G) := uniform_convergence.uniform_space α G,
letI : uniform_space (α → G × G) := uniform_convergence.uniform_space α (G × G),
exact ⟨(uniform_convergence.postcomp_uniform_continuous uniform_continuous_div).comp
uniform_convergence.uniform_equiv_prod_arrow.symm.uniform_continuous⟩
end
/-- Let `𝔖 : set (set α)`. If `G` is a uniform group, then the uniform structure of
`𝔖`-convergence makes `α → G` a uniform group as well. -/
@[to_additive "Let `𝔖 : set (set α)`. If `G` is a uniform additive group, then the uniform
structure of `𝔖`-convergence makes `α → G` a uniform additive group as well. "]
protected lemma uniform_convergence_on.uniform_group :
@uniform_group (α → G) (uniform_convergence_on.uniform_space α G 𝔖) _ :=
begin
-- Since `(/) : G × G → G` is uniformly continuous,
-- `uniform_convergence_on.postcomp_uniform_continuous` tells us that
-- `((/) ∘ —) : (α → G × G) → (α → G)` is uniformly continuous too. By precomposing with
-- `uniform_convergence_on.uniform_equiv_prod_arrow`, this gives that
-- `(/) : (α → G) × (α → G) → (α → G)` is also uniformly continuous
letI : uniform_space (α → G) := uniform_convergence_on.uniform_space α G 𝔖,
letI : uniform_space (α → G × G) := uniform_convergence_on.uniform_space α (G × G) 𝔖,
exact ⟨(uniform_convergence_on.postcomp_uniform_continuous uniform_continuous_div).comp
uniform_convergence_on.uniform_equiv_prod_arrow.symm.uniform_continuous⟩
end
end group
|
8bca25f15403bf652b44b339789505ddbe150aef | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/limits/cones_auto.lean | e0336f4289b6389ff8c11512ec79cdcb5b1c6b6e | [] | 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 | 38,674 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Stephen Morgan, Scott Morrison, Floris van Doorn
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.const
import Mathlib.category_theory.discrete_category
import Mathlib.category_theory.yoneda
import Mathlib.category_theory.reflects_isomorphisms
import Mathlib.PostPort
universes v u l u'
namespace Mathlib
namespace category_theory
namespace functor
/--
`F.cones` is the functor assigning to an object `X` the type of
natural transformations from the constant functor with value `X` to `F`.
An object representing this functor is a limit of `F`.
-/
def cones {J : Type v} [small_category J] {C : Type u} [category C] (F : J ⥤ C) : Cᵒᵖ ⥤ Type v :=
functor.op (const J) ⋙ obj yoneda F
/--
`F.cocones` is the functor assigning to an object `X` the type of
natural transformations from `F` to the constant functor with value `X`.
An object corepresenting this functor is a colimit of `F`.
-/
@[simp] theorem cocones_obj {J : Type v} [small_category J] {C : Type u} [category C] (F : J ⥤ C)
(X : C) : obj (cocones F) X = (F ⟶ obj (const J) X) :=
Eq.refl (F ⟶ obj (const J) X)
end functor
/--
Functorially associated to each functor `J ⥤ C`, we have the `C`-presheaf consisting of
cones with a given cone point.
-/
@[simp] theorem cones_map (J : Type v) [small_category J] (C : Type u) [category C] (F : J ⥤ C)
(G : J ⥤ C) (f : F ⟶ G) :
functor.map (cones J C) f =
whisker_left (functor.op (functor.const J)) (functor.map yoneda f) :=
Eq.refl (functor.map (cones J C) f)
/--
Contravariantly associated to each functor `J ⥤ C`, we have the `C`-copresheaf consisting of
cocones with a given cocone point.
-/
@[simp] theorem cocones_obj (J : Type v) [small_category J] (C : Type u) [category C]
(F : J ⥤ Cᵒᵖ) : functor.obj (cocones J C) F = functor.cocones (opposite.unop F) :=
Eq.refl (functor.obj (cocones J C) F)
namespace limits
/--
A `c : cone F` is:
* an object `c.X` and
* a natural transformation `c.π : c.X ⟶ F` from the constant `c.X` functor to `F`.
`cone F` is equivalent, via `cone.equiv` below, to `Σ X, F.cones.obj X`.
-/
structure cone {J : Type v} [small_category J] {C : Type u} [category C] (F : J ⥤ C) where
X : C
π : functor.obj (functor.const J) X ⟶ F
protected instance inhabited_cone {C : Type u} [category C] (F : discrete PUnit ⥤ C) :
Inhabited (cone F) :=
{ default := cone.mk (functor.obj F PUnit.unit) (nat_trans.mk fun (X : discrete PUnit) => sorry) }
@[simp] theorem cone.w {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(c : cone F) {j : J} {j' : J} (f : j ⟶ j') :
nat_trans.app (cone.π c) j ≫ functor.map F f = nat_trans.app (cone.π c) j' :=
sorry
/--
A `c : cocone F` is
* an object `c.X` and
* a natural transformation `c.ι : F ⟶ c.X` from `F` to the constant `c.X` functor.
`cocone F` is equivalent, via `cone.equiv` below, to `Σ X, F.cocones.obj X`.
-/
structure cocone {J : Type v} [small_category J] {C : Type u} [category C] (F : J ⥤ C) where
X : C
ι : F ⟶ functor.obj (functor.const J) X
protected instance inhabited_cocone {C : Type u} [category C] (F : discrete PUnit ⥤ C) :
Inhabited (cocone F) :=
{ default :=
cocone.mk (functor.obj F PUnit.unit) (nat_trans.mk fun (X : discrete PUnit) => sorry) }
@[simp] theorem cocone.w_assoc {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(c : cocone F) {j : J} {j' : J} (f : j ⟶ j') {X' : C}
(f' : functor.obj (functor.obj (functor.const J) (cocone.X c)) j' ⟶ X') :
functor.map F f ≫ nat_trans.app (cocone.ι c) j' ≫ f' = nat_trans.app (cocone.ι c) j ≫ f' :=
sorry
namespace cone
/-- The isomorphism between a cone on `F` and an element of the functor `F.cones`. -/
def equiv {J : Type v} [small_category J] {C : Type u} [category C] (F : J ⥤ C) :
cone F ≅ sigma fun (X : Cᵒᵖ) => functor.obj (functor.cones F) X :=
iso.mk (fun (c : cone F) => sigma.mk (opposite.op (X c)) (π c))
fun (c : sigma fun (X : Cᵒᵖ) => functor.obj (functor.cones F) X) =>
mk (opposite.unop (sigma.fst c)) (sigma.snd c)
/-- A map to the vertex of a cone naturally induces a cone by composition. -/
@[simp] def extensions {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(c : cone F) : functor.obj yoneda (X c) ⟶ functor.cones F :=
nat_trans.mk
fun (X : Cᵒᵖ) (f : functor.obj (functor.obj yoneda (X c)) X) =>
functor.map (functor.const J) f ≫ π c
/-- A map to the vertex of a cone induces a cone by composition. -/
@[simp] def extend {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(c : cone F) {X : C} (f : X ⟶ X c) : cone F :=
mk X (nat_trans.app (extensions c) (opposite.op X) f)
@[simp] theorem extend_π {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(c : cone F) {X : Cᵒᵖ} (f : opposite.unop X ⟶ X c) :
π (extend c f) = nat_trans.app (extensions c) X f :=
rfl
/-- Whisker a cone by precomposition of a functor. -/
def whisker {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C} {K : Type v}
[small_category K] (E : K ⥤ J) (c : cone F) : cone (E ⋙ F) :=
mk (X c) (whisker_left E (π c))
end cone
namespace cocone
/-- The isomorphism between a cocone on `F` and an element of the functor `F.cocones`. -/
def equiv {J : Type v} [small_category J] {C : Type u} [category C] (F : J ⥤ C) :
cocone F ≅ sigma fun (X : C) => functor.obj (functor.cocones F) X :=
iso.mk (fun (c : cocone F) => sigma.mk (X c) (ι c))
fun (c : sigma fun (X : C) => functor.obj (functor.cocones F) X) =>
mk (sigma.fst c) (sigma.snd c)
/-- A map from the vertex of a cocone naturally induces a cocone by composition. -/
@[simp] def extensions {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(c : cocone F) : functor.obj coyoneda (opposite.op (X c)) ⟶ functor.cocones F :=
nat_trans.mk
fun (X : C) (f : functor.obj (functor.obj coyoneda (opposite.op (X c))) X) =>
ι c ≫ functor.map (functor.const J) f
/-- A map from the vertex of a cocone induces a cocone by composition. -/
@[simp] def extend {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(c : cocone F) {X : C} (f : X c ⟶ X) : cocone F :=
mk X (nat_trans.app (extensions c) X f)
@[simp] theorem extend_ι {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(c : cocone F) {X : C} (f : X c ⟶ X) : ι (extend c f) = nat_trans.app (extensions c) X f :=
rfl
/--
Whisker a cocone by precomposition of a functor. See `whiskering` for a functorial
version.
-/
@[simp] theorem whisker_ι {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
{K : Type v} [small_category K] (E : K ⥤ J) (c : cocone F) :
ι (whisker E c) = whisker_left E (ι c) :=
Eq.refl (ι (whisker E c))
end cocone
/-- A cone morphism between two cones for the same diagram is a morphism of the cone points which
commutes with the cone legs. -/
structure cone_morphism {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(A : cone F) (B : cone F)
where
hom : cone.X A ⟶ cone.X B
w' :
autoParam (∀ (j : J), hom ≫ nat_trans.app (cone.π B) j = nat_trans.app (cone.π A) j)
(Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously")
(Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") [])
@[simp] theorem cone_morphism.w {J : Type v} [small_category J] {C : Type u} [category C]
{F : J ⥤ C} {A : cone F} {B : cone F} (c : cone_morphism A B) (j : J) :
cone_morphism.hom c ≫ nat_trans.app (cone.π B) j = nat_trans.app (cone.π A) j :=
sorry
@[simp] theorem cone_morphism.w_assoc {J : Type v} [small_category J] {C : Type u} [category C]
{F : J ⥤ C} {A : cone F} {B : cone F} (c : cone_morphism A B) (j : J) {X' : C}
(f' : functor.obj F j ⟶ X') :
cone_morphism.hom c ≫ nat_trans.app (cone.π B) j ≫ f' = nat_trans.app (cone.π A) j ≫ f' :=
sorry
protected instance inhabited_cone_morphism {J : Type v} [small_category J] {C : Type u} [category C]
{F : J ⥤ C} (A : cone F) : Inhabited (cone_morphism A A) :=
{ default := cone_morphism.mk 𝟙 }
/-- The category of cones on a given diagram. -/
protected instance cone.category {J : Type v} [small_category J] {C : Type u} [category C]
{F : J ⥤ C} : category (cone F) :=
category.mk
namespace cones
/-- To give an isomorphism between cones, it suffices to give an
isomorphism between their vertices which commutes with the cone
maps. -/
def ext {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C} {c : cone F}
{c' : cone F} (φ : cone.X c ≅ cone.X c')
(w : ∀ (j : J), nat_trans.app (cone.π c) j = iso.hom φ ≫ nat_trans.app (cone.π c') j) :
c ≅ c' :=
iso.mk (cone_morphism.mk (iso.hom φ)) (cone_morphism.mk (iso.inv φ))
/--
Given a cone morphism whose object part is an isomorphism, produce an
isomorphism of cones.
-/
def cone_iso_of_hom_iso {J : Type v} [small_category J] {C : Type u} [category C] {K : J ⥤ C}
{c : cone K} {d : cone K} (f : c ⟶ d) [i : is_iso (cone_morphism.hom f)] : is_iso f :=
is_iso.mk (cone_morphism.mk (inv (cone_morphism.hom f)))
/--
Functorially postcompose a cone for `F` by a natural transformation `F ⟶ G` to give a cone for `G`.
-/
@[simp] theorem postcompose_map_hom {J : Type v} [small_category J] {C : Type u} [category C]
{F : J ⥤ C} {G : J ⥤ C} (α : F ⟶ G) (c₁ : cone F) (c₂ : cone F) (f : c₁ ⟶ c₂) :
cone_morphism.hom (functor.map (postcompose α) f) = cone_morphism.hom f :=
Eq.refl (cone_morphism.hom (functor.map (postcompose α) f))
/-- Postcomposing a cone by the composite natural transformation `α ≫ β` is the same as
postcomposing by `α` and then by `β`. -/
def postcompose_comp {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
{G : J ⥤ C} {H : J ⥤ C} (α : F ⟶ G) (β : G ⟶ H) :
postcompose (α ≫ β) ≅ postcompose α ⋙ postcompose β :=
nat_iso.of_components
(fun (s : cone F) => ext (iso.refl (cone.X (functor.obj (postcompose (α ≫ β)) s))) sorry) sorry
/-- Postcomposing by the identity does not change the cone up to isomorphism. -/
def postcompose_id {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C} :
postcompose 𝟙 ≅ 𝟭 :=
nat_iso.of_components
(fun (s : cone F) => ext (iso.refl (cone.X (functor.obj (postcompose 𝟙) s))) sorry) sorry
/--
If `F` and `G` are naturally isomorphic functors, then they have equivalent categories of
cones.
-/
@[simp] theorem postcompose_equivalence_unit_iso {J : Type v} [small_category J] {C : Type u}
[category C] {F : J ⥤ C} {G : J ⥤ C} (α : F ≅ G) :
equivalence.unit_iso (postcompose_equivalence α) =
nat_iso.of_components
(fun (s : cone F) =>
ext (iso.refl (cone.X (functor.obj 𝟭 s))) (postcompose_equivalence._proof_1 α s))
(postcompose_equivalence._proof_2 α) :=
Eq.refl (equivalence.unit_iso (postcompose_equivalence α))
/--
Whiskering on the left by `E : K ⥤ J` gives a functor from `cone F` to `cone (E ⋙ F)`.
-/
@[simp] theorem whiskering_obj {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
{K : Type v} [small_category K] (E : K ⥤ J) (c : cone F) :
functor.obj (whiskering E) c = cone.whisker E c :=
Eq.refl (functor.obj (whiskering E) c)
/--
Whiskering by an equivalence gives an equivalence between categories of cones.
-/
@[simp] theorem whiskering_equivalence_inverse {J : Type v} [small_category J] {C : Type u}
[category C] {F : J ⥤ C} {K : Type v} [small_category K] (e : K ≌ J) :
equivalence.inverse (whiskering_equivalence e) =
whiskering (equivalence.inverse e) ⋙
postcompose
(iso.inv (functor.associator (equivalence.inverse e) (equivalence.functor e) F) ≫
whisker_right (iso.hom (equivalence.counit_iso e)) F ≫
iso.hom (functor.left_unitor F)) :=
Eq.refl (equivalence.inverse (whiskering_equivalence e))
/--
The categories of cones over `F` and `G` are equivalent if `F` and `G` are naturally isomorphic
(possibly after changing the indexing category by an equivalence).
-/
def equivalence_of_reindexing {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
{K : Type v} [small_category K] {G : K ⥤ C} (e : K ≌ J) (α : equivalence.functor e ⋙ F ≅ G) :
cone F ≌ cone G :=
equivalence.trans (whiskering_equivalence e) (postcompose_equivalence α)
/-- Forget the cone structure and obtain just the cone point. -/
def forget {J : Type v} [small_category J] {C : Type u} [category C] (F : J ⥤ C) : cone F ⥤ C :=
functor.mk (fun (t : cone F) => cone.X t) fun (s t : cone F) (f : s ⟶ t) => cone_morphism.hom f
/-- A functor `G : C ⥤ D` sends cones over `F` to cones over `F ⋙ G` functorially. -/
@[simp] theorem functoriality_obj_π_app {J : Type v} [small_category J] {C : Type u} [category C]
(F : J ⥤ C) {D : Type u'} [category D] (G : C ⥤ D) (A : cone F) (j : J) :
nat_trans.app (cone.π (functor.obj (functoriality F G) A)) j =
functor.map G (nat_trans.app (cone.π A) j) :=
Eq.refl (nat_trans.app (cone.π (functor.obj (functoriality F G) A)) j)
protected instance functoriality_full {J : Type v} [small_category J] {C : Type u} [category C]
(F : J ⥤ C) {D : Type u'} [category D] (G : C ⥤ D) [full G] [faithful G] :
full (functoriality F G) :=
full.mk
fun (X Y : cone F)
(t : functor.obj (functoriality F G) X ⟶ functor.obj (functoriality F G) Y) =>
cone_morphism.mk (functor.preimage G (cone_morphism.hom t))
protected instance functoriality_faithful {J : Type v} [small_category J] {C : Type u} [category C]
(F : J ⥤ C) {D : Type u'} [category D] (G : C ⥤ D) [faithful G] :
faithful (functoriality F G) :=
faithful.mk
/--
If `e : C ≌ D` is an equivalence of categories, then `functoriality F e.functor` induces an
equivalence between cones over `F` and cones over `F ⋙ e.functor`.
-/
@[simp] theorem functoriality_equivalence_counit_iso {J : Type v} [small_category J] {C : Type u}
[category C] (F : J ⥤ C) {D : Type u'} [category D] (e : C ≌ D) :
equivalence.counit_iso (functoriality_equivalence F e) =
nat_iso.of_components
(fun (c : cone (F ⋙ equivalence.functor e)) =>
ext (iso.app (equivalence.counit_iso e) (cone.X c))
(functoriality_equivalence._proof_3 F e c))
(functoriality_equivalence._proof_4 F e) :=
Eq.refl (equivalence.counit_iso (functoriality_equivalence F e))
/--
If `F` reflects isomorphisms, then `cones.functoriality F` reflects isomorphisms
as well.
-/
protected instance reflects_cone_isomorphism {J : Type v} [small_category J] {C : Type u}
[category C] {D : Type u'} [category D] (F : C ⥤ D) [reflects_isomorphisms F] (K : J ⥤ C) :
reflects_isomorphisms (functoriality K F) :=
reflects_isomorphisms.mk
fun (A B : cone K) (f : A ⟶ B) (_inst_3_1 : is_iso (functor.map (functoriality K F) f)) =>
cone_iso_of_hom_iso f
end cones
/-- A cocone morphism between two cocones for the same diagram is a morphism of the cocone points
which commutes with the cocone legs. -/
structure cocone_morphism {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(A : cocone F) (B : cocone F)
where
hom : cocone.X A ⟶ cocone.X B
w' :
autoParam (∀ (j : J), nat_trans.app (cocone.ι A) j ≫ hom = nat_trans.app (cocone.ι B) j)
(Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously")
(Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") [])
protected instance inhabited_cocone_morphism {J : Type v} [small_category J] {C : Type u}
[category C] {F : J ⥤ C} (A : cocone F) : Inhabited (cocone_morphism A A) :=
{ default := cocone_morphism.mk 𝟙 }
@[simp] theorem cocone_morphism.w {J : Type v} [small_category J] {C : Type u} [category C]
{F : J ⥤ C} {A : cocone F} {B : cocone F} (c : cocone_morphism A B) (j : J) :
nat_trans.app (cocone.ι A) j ≫ cocone_morphism.hom c = nat_trans.app (cocone.ι B) j :=
sorry
@[simp] theorem cocone_morphism.w_assoc {J : Type v} [small_category J] {C : Type u} [category C]
{F : J ⥤ C} {A : cocone F} {B : cocone F} (c : cocone_morphism A B) (j : J) {X' : C}
(f' : cocone.X B ⟶ X') :
nat_trans.app (cocone.ι A) j ≫ cocone_morphism.hom c ≫ f' = nat_trans.app (cocone.ι B) j ≫ f' :=
sorry
@[simp] theorem cocone.category_to_category_struct_id_hom {J : Type v} [small_category J]
{C : Type u} [category C] {F : J ⥤ C} (B : cocone F) : cocone_morphism.hom 𝟙 = 𝟙 :=
Eq.refl (cocone_morphism.hom 𝟙)
namespace cocones
/-- To give an isomorphism between cocones, it suffices to give an
isomorphism between their vertices which commutes with the cocone
maps. -/
@[simp] theorem ext_inv_hom {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
{c : cocone F} {c' : cocone F} (φ : cocone.X c ≅ cocone.X c')
(w : ∀ (j : J), nat_trans.app (cocone.ι c) j ≫ iso.hom φ = nat_trans.app (cocone.ι c') j) :
cocone_morphism.hom (iso.inv (ext φ w)) = iso.inv φ :=
Eq.refl (cocone_morphism.hom (iso.inv (ext φ w)))
/--
Given a cocone morphism whose object part is an isomorphism, produce an
isomorphism of cocones.
-/
def cocone_iso_of_hom_iso {J : Type v} [small_category J] {C : Type u} [category C] {K : J ⥤ C}
{c : cocone K} {d : cocone K} (f : c ⟶ d) [i : is_iso (cocone_morphism.hom f)] : is_iso f :=
is_iso.mk (cocone_morphism.mk (inv (cocone_morphism.hom f)))
/--
Functorially precompose a cocone for `F` by a natural transformation `G ⟶ F` to give a cocone for `G`.
-/
def precompose {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C} {G : J ⥤ C}
(α : G ⟶ F) : cocone F ⥤ cocone G :=
functor.mk (fun (c : cocone F) => cocone.mk (cocone.X c) (α ≫ cocone.ι c))
fun (c₁ c₂ : cocone F) (f : c₁ ⟶ c₂) => cocone_morphism.mk (cocone_morphism.hom f)
/-- Precomposing a cocone by the composite natural transformation `α ≫ β` is the same as
precomposing by `β` and then by `α`. -/
def precompose_comp {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
{G : J ⥤ C} {H : J ⥤ C} (α : F ⟶ G) (β : G ⟶ H) :
precompose (α ≫ β) ≅ precompose β ⋙ precompose α :=
nat_iso.of_components
(fun (s : cocone H) => ext (iso.refl (cocone.X (functor.obj (precompose (α ≫ β)) s))) sorry)
sorry
/-- Precomposing by the identity does not change the cocone up to isomorphism. -/
def precompose_id {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C} :
precompose 𝟙 ≅ 𝟭 :=
nat_iso.of_components
(fun (s : cocone F) => ext (iso.refl (cocone.X (functor.obj (precompose 𝟙) s))) sorry) sorry
/--
If `F` and `G` are naturally isomorphic functors, then they have equivalent categories of
cocones.
-/
@[simp] theorem precompose_equivalence_functor {J : Type v} [small_category J] {C : Type u}
[category C] {F : J ⥤ C} {G : J ⥤ C} (α : G ≅ F) :
equivalence.functor (precompose_equivalence α) = precompose (iso.hom α) :=
Eq.refl (equivalence.functor (precompose_equivalence α))
/--
Whiskering on the left by `E : K ⥤ J` gives a functor from `cocone F` to `cocone (E ⋙ F)`.
-/
def whiskering {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C} {K : Type v}
[small_category K] (E : K ⥤ J) : cocone F ⥤ cocone (E ⋙ F) :=
functor.mk (fun (c : cocone F) => cocone.whisker E c)
fun (c c' : cocone F) (f : c ⟶ c') => cocone_morphism.mk (cocone_morphism.hom f)
/--
Whiskering by an equivalence gives an equivalence between categories of cones.
-/
def whiskering_equivalence {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
{K : Type v} [small_category K] (e : K ≌ J) : cocone F ≌ cocone (equivalence.functor e ⋙ F) :=
equivalence.mk' (whiskering (equivalence.functor e))
(whiskering (equivalence.inverse e) ⋙
precompose
(iso.inv (functor.left_unitor F) ≫
whisker_right (iso.inv (equivalence.counit_iso e)) F ≫
iso.inv (functor.associator (equivalence.inverse e) (equivalence.functor e) F)))
(nat_iso.of_components (fun (s : cocone F) => ext (iso.refl (cocone.X (functor.obj 𝟭 s))) sorry)
sorry)
(nat_iso.of_components
(fun (s : cocone (equivalence.functor e ⋙ F)) =>
ext
(iso.refl
(cocone.X
(functor.obj
((whiskering (equivalence.inverse e) ⋙
precompose
(iso.inv (functor.left_unitor F) ≫
whisker_right (iso.inv (equivalence.counit_iso e)) F ≫
iso.inv
(functor.associator (equivalence.inverse e) (equivalence.functor e)
F))) ⋙
whiskering (equivalence.functor e))
s)))
sorry)
sorry)
/--
The categories of cocones over `F` and `G` are equivalent if `F` and `G` are naturally isomorphic
(possibly after changing the indexing category by an equivalence).
-/
@[simp] theorem equivalence_of_reindexing_functor_obj {J : Type v} [small_category J] {C : Type u}
[category C] {F : J ⥤ C} {K : Type v} [small_category K] {G : K ⥤ C} (e : K ≌ J)
(α : equivalence.functor e ⋙ F ≅ G) (X : cocone F) :
functor.obj (equivalence.functor (equivalence_of_reindexing e α)) X =
functor.obj (precompose (iso.inv α)) (cocone.whisker (equivalence.functor e) X) :=
Eq.refl (functor.obj (precompose (iso.inv α)) (cocone.whisker (equivalence.functor e) X))
/-- Forget the cocone structure and obtain just the cocone point. -/
@[simp] theorem forget_map {J : Type v} [small_category J] {C : Type u} [category C] (F : J ⥤ C)
(s : cocone F) (t : cocone F) (f : s ⟶ t) : functor.map (forget F) f = cocone_morphism.hom f :=
Eq.refl (functor.map (forget F) f)
/-- A functor `G : C ⥤ D` sends cocones over `F` to cocones over `F ⋙ G` functorially. -/
@[simp] theorem functoriality_map_hom {J : Type v} [small_category J] {C : Type u} [category C]
(F : J ⥤ C) {D : Type u'} [category D] (G : C ⥤ D) (_x : cocone F) :
∀ (_x_1 : cocone F) (f : _x ⟶ _x_1),
cocone_morphism.hom (functor.map (functoriality F G) f) =
functor.map G (cocone_morphism.hom f) :=
fun (_x_1 : cocone F) (f : _x ⟶ _x_1) =>
Eq.refl (cocone_morphism.hom (functor.map (functoriality F G) f))
protected instance functoriality_full {J : Type v} [small_category J] {C : Type u} [category C]
(F : J ⥤ C) {D : Type u'} [category D] (G : C ⥤ D) [full G] [faithful G] :
full (functoriality F G) :=
full.mk
fun (X Y : cocone F)
(t : functor.obj (functoriality F G) X ⟶ functor.obj (functoriality F G) Y) =>
cocone_morphism.mk (functor.preimage G (cocone_morphism.hom t))
protected instance functoriality_faithful {J : Type v} [small_category J] {C : Type u} [category C]
(F : J ⥤ C) {D : Type u'} [category D] (G : C ⥤ D) [faithful G] :
faithful (functoriality F G) :=
faithful.mk
/--
If `e : C ≌ D` is an equivalence of categories, then `functoriality F e.functor` induces an
equivalence between cocones over `F` and cocones over `F ⋙ e.functor`.
-/
@[simp] theorem functoriality_equivalence_functor {J : Type v} [small_category J] {C : Type u}
[category C] (F : J ⥤ C) {D : Type u'} [category D] (e : C ≌ D) :
equivalence.functor (functoriality_equivalence F e) = functoriality F (equivalence.functor e) :=
Eq.refl (equivalence.functor (functoriality_equivalence F e))
/--
If `F` reflects isomorphisms, then `cocones.functoriality F` reflects isomorphisms
as well.
-/
protected instance reflects_cocone_isomorphism {J : Type v} [small_category J] {C : Type u}
[category C] {D : Type u'} [category D] (F : C ⥤ D) [reflects_isomorphisms F] (K : J ⥤ C) :
reflects_isomorphisms (functoriality K F) :=
reflects_isomorphisms.mk
fun (A B : cocone K) (f : A ⟶ B) (_inst_3_1 : is_iso (functor.map (functoriality K F) f)) =>
cocone_iso_of_hom_iso f
end cocones
end limits
namespace functor
/-- The image of a cone in C under a functor G : C ⥤ D is a cone in D. -/
/-- The image of a cocone in C under a functor G : C ⥤ D is a cocone in D. -/
@[simp] theorem map_cone_X {J : Type v} [small_category J] {C : Type u} [category C] {D : Type u'}
[category D] {F : J ⥤ C} (H : C ⥤ D) (c : limits.cone F) :
limits.cone.X (map_cone H c) = obj H (limits.cone.X c) :=
Eq.refl (obj H (limits.cone.X c))
@[simp] theorem map_cocone_ι_app {J : Type v} [small_category J] {C : Type u} [category C]
{D : Type u'} [category D] {F : J ⥤ C} (H : C ⥤ D) (c : limits.cocone F) (j : J) :
nat_trans.app (limits.cocone.ι (map_cocone H c)) j =
map H (nat_trans.app (limits.cocone.ι c) j) :=
Eq.refl (map H (nat_trans.app (limits.cocone.ι c) j))
/-- Given a cone morphism `c ⟶ c'`, construct a cone morphism on the mapped cones functorially. -/
def map_cone_morphism {J : Type v} [small_category J] {C : Type u} [category C] {D : Type u'}
[category D] {F : J ⥤ C} (H : C ⥤ D) {c : limits.cone F} {c' : limits.cone F} (f : c ⟶ c') :
map_cone H c ⟶ map_cone H c' :=
map (limits.cones.functoriality F H) f
/-- Given a cocone morphism `c ⟶ c'`, construct a cocone morphism on the mapped cocones functorially. -/
def map_cocone_morphism {J : Type v} [small_category J] {C : Type u} [category C] {D : Type u'}
[category D] {F : J ⥤ C} (H : C ⥤ D) {c : limits.cocone F} {c' : limits.cocone F} (f : c ⟶ c') :
map_cocone H c ⟶ map_cocone H c' :=
map (limits.cocones.functoriality F H) f
/-- If `H` is an equivalence, we invert `H.map_cone` and get a cone for `F` from a cone
for `F ⋙ H`.-/
def map_cone_inv {J : Type v} [small_category J] {C : Type u} [category C] {D : Type u'}
[category D] {F : J ⥤ C} (H : C ⥤ D) [is_equivalence H] (c : limits.cone (F ⋙ H)) :
limits.cone F :=
obj (equivalence.inverse (limits.cones.functoriality_equivalence F (as_equivalence H))) c
/-- `map_cone` is the left inverse to `map_cone_inv`. -/
def map_cone_map_cone_inv {J : Type v} [small_category J] {C : Type u} [category C] {D : Type u'}
[category D] {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H] (c : limits.cone (F ⋙ H)) :
map_cone H (map_cone_inv H c) ≅ c :=
iso.app (equivalence.counit_iso (limits.cones.functoriality_equivalence F (as_equivalence H))) c
/-- `map_cone` is the right inverse to `map_cone_inv`. -/
def map_cone_inv_map_cone {J : Type v} [small_category J] {C : Type u} [category C] {D : Type u'}
[category D] {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H] (c : limits.cone F) :
map_cone_inv H (map_cone H c) ≅ c :=
iso.app
(iso.symm (equivalence.unit_iso (limits.cones.functoriality_equivalence F (as_equivalence H))))
c
/-- If `H` is an equivalence, we invert `H.map_cone` and get a cone for `F` from a cone
for `F ⋙ H`.-/
def map_cocone_inv {J : Type v} [small_category J] {C : Type u} [category C] {D : Type u'}
[category D] {F : J ⥤ C} (H : C ⥤ D) [is_equivalence H] (c : limits.cocone (F ⋙ H)) :
limits.cocone F :=
obj (equivalence.inverse (limits.cocones.functoriality_equivalence F (as_equivalence H))) c
/-- `map_cocone` is the left inverse to `map_cocone_inv`. -/
def map_cocone_map_cocone_inv {J : Type v} [small_category J] {C : Type u} [category C]
{D : Type u'} [category D] {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H]
(c : limits.cocone (F ⋙ H)) : map_cocone H (map_cocone_inv H c) ≅ c :=
iso.app (equivalence.counit_iso (limits.cocones.functoriality_equivalence F (as_equivalence H))) c
/-- `map_cocone` is the right inverse to `map_cocone_inv`. -/
def map_cocone_inv_map_cocone {J : Type v} [small_category J] {C : Type u} [category C]
{D : Type u'} [category D] {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H] (c : limits.cocone F) :
map_cocone_inv H (map_cocone H c) ≅ c :=
iso.app
(iso.symm
(equivalence.unit_iso (limits.cocones.functoriality_equivalence F (as_equivalence H))))
c
/-- `functoriality F _ ⋙ postcompose (whisker_left F _)` simplifies to `functoriality F _`. -/
@[simp] theorem functoriality_comp_postcompose_inv_app_hom {J : Type v} [small_category J]
{C : Type u} [category C] {D : Type u'} [category D] {F : J ⥤ C} {H : C ⥤ D} {H' : C ⥤ D}
(α : H ≅ H') (X : limits.cone F) :
limits.cone_morphism.hom (nat_trans.app (iso.inv (functoriality_comp_postcompose α)) X) =
nat_trans.app (iso.inv α) (limits.cone.X X) :=
Eq.refl (nat_trans.app (iso.inv α) (limits.cone.X X))
/--
For `F : J ⥤ C`, given a cone `c : cone F`, and a natural isomorphism `α : H ≅ H'` for functors
`H H' : C ⥤ D`, the postcomposition of the cone `H.map_cone` using the isomorphism `α` is
isomorphic to the cone `H'.map_cone`.
-/
def postcompose_whisker_left_map_cone {J : Type v} [small_category J] {C : Type u} [category C]
{D : Type u'} [category D] {F : J ⥤ C} {H : C ⥤ D} {H' : C ⥤ D} (α : H ≅ H')
(c : limits.cone F) :
obj (limits.cones.postcompose (whisker_left F (iso.hom α))) (map_cone H c) ≅ map_cone H' c :=
iso.app (functoriality_comp_postcompose α) c
/--
`map_cone` commutes with `postcompose`. In particular, for `F : J ⥤ C`, given a cone `c : cone F`, a
natural transformation `α : F ⟶ G` and a functor `H : C ⥤ D`, we have two obvious ways of producing
a cone over `G ⋙ H`, and they are both isomorphic.
-/
@[simp] theorem map_cone_postcompose_inv_hom {J : Type v} [small_category J] {C : Type u}
[category C] {D : Type u'} [category D] {F : J ⥤ C} {G : J ⥤ C} (H : C ⥤ D) {α : F ⟶ G}
{c : limits.cone F} : limits.cone_morphism.hom (iso.inv (map_cone_postcompose H)) = 𝟙 :=
Eq.refl 𝟙
/--
`map_cone` commutes with `postcompose_equivalence`
-/
@[simp] theorem map_cone_postcompose_equivalence_functor_hom_hom {J : Type v} [small_category J]
{C : Type u} [category C] {D : Type u'} [category D] {F : J ⥤ C} {G : J ⥤ C} (H : C ⥤ D)
{α : F ≅ G} {c : limits.cone F} :
limits.cone_morphism.hom (iso.hom (map_cone_postcompose_equivalence_functor H)) = 𝟙 :=
Eq.refl 𝟙
/-- `functoriality F _ ⋙ precompose (whisker_left F _)` simplifies to `functoriality F _`. -/
@[simp] theorem functoriality_comp_precompose_inv_app_hom {J : Type v} [small_category J]
{C : Type u} [category C] {D : Type u'} [category D] {F : J ⥤ C} {H : C ⥤ D} {H' : C ⥤ D}
(α : H ≅ H') (X : limits.cocone F) :
limits.cocone_morphism.hom (nat_trans.app (iso.inv (functoriality_comp_precompose α)) X) =
nat_trans.app (iso.inv α) (limits.cocone.X X) :=
Eq.refl (nat_trans.app (iso.inv α) (limits.cocone.X X))
/--
For `F : J ⥤ C`, given a cocone `c : cocone F`, and a natural isomorphism `α : H ≅ H'` for functors
`H H' : C ⥤ D`, the precomposition of the cocone `H.map_cocone` using the isomorphism `α` is
isomorphic to the cocone `H'.map_cocone`.
-/
def precompose_whisker_left_map_cocone {J : Type v} [small_category J] {C : Type u} [category C]
{D : Type u'} [category D] {F : J ⥤ C} {H : C ⥤ D} {H' : C ⥤ D} (α : H ≅ H')
(c : limits.cocone F) :
obj (limits.cocones.precompose (whisker_left F (iso.inv α))) (map_cocone H c) ≅
map_cocone H' c :=
iso.app (functoriality_comp_precompose α) c
/--
`map_cocone` commutes with `precompose`. In particular, for `F : J ⥤ C`, given a cocone
`c : cocone F`, a natural transformation `α : F ⟶ G` and a functor `H : C ⥤ D`, we have two obvious
ways of producing a cocone over `G ⋙ H`, and they are both isomorphic.
-/
@[simp] theorem map_cocone_precompose_hom_hom {J : Type v} [small_category J] {C : Type u}
[category C] {D : Type u'} [category D] {F : J ⥤ C} {G : J ⥤ C} (H : C ⥤ D) {α : F ⟶ G}
{c : limits.cocone G} : limits.cocone_morphism.hom (iso.hom (map_cocone_precompose H)) = 𝟙 :=
Eq.refl 𝟙
/--
`map_cocone` commutes with `precompose_equivalence`
-/
@[simp] theorem map_cocone_precompose_equivalence_functor_inv_hom {J : Type v} [small_category J]
{C : Type u} [category C] {D : Type u'} [category D] {F : J ⥤ C} {G : J ⥤ C} (H : C ⥤ D)
{α : F ≅ G} {c : limits.cocone G} :
limits.cocone_morphism.hom (iso.inv (map_cocone_precompose_equivalence_functor H)) = 𝟙 :=
Eq.refl 𝟙
/--
`map_cone` commutes with `whisker`
-/
@[simp] theorem map_cone_whisker_inv_hom {J : Type v} [small_category J] {C : Type u} [category C]
{D : Type u'} [category D] {F : J ⥤ C} (H : C ⥤ D) {K : Type v} [small_category K] {E : K ⥤ J}
{c : limits.cone F} : limits.cone_morphism.hom (iso.inv (map_cone_whisker H)) = 𝟙 :=
Eq.refl 𝟙
/--
`map_cocone` commutes with `whisker`
-/
@[simp] theorem map_cocone_whisker_inv_hom {J : Type v} [small_category J] {C : Type u} [category C]
{D : Type u'} [category D] {F : J ⥤ C} (H : C ⥤ D) {K : Type v} [small_category K] {E : K ⥤ J}
{c : limits.cocone F} : limits.cocone_morphism.hom (iso.inv (map_cocone_whisker H)) = 𝟙 :=
Eq.refl 𝟙
end functor
end category_theory
namespace category_theory.limits
/-- Change a `cocone F` into a `cone F.op`. -/
@[simp] theorem cocone.op_X {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(c : cocone F) : cone.X (cocone.op c) = opposite.op (cocone.X c) :=
Eq.refl (cone.X (cocone.op c))
/-- Change a `cone F` into a `cocone F.op`. -/
def cone.op {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C} (c : cone F) :
cocone (functor.op F) :=
cocone.mk (opposite.op (cone.X c))
(nat_trans.mk fun (j : Jᵒᵖ) => has_hom.hom.op (nat_trans.app (cone.π c) (opposite.unop j)))
/-- Change a `cocone F.op` into a `cone F`. -/
def cocone.unop {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(c : cocone (functor.op F)) : cone F :=
cone.mk (opposite.unop (cocone.X c))
(nat_trans.mk fun (j : J) => has_hom.hom.unop (nat_trans.app (cocone.ι c) (opposite.op j)))
/-- Change a `cone F.op` into a `cocone F`. -/
@[simp] theorem cone.unop_X {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
(c : cone (functor.op F)) : cocone.X (cone.unop c) = opposite.unop (cone.X c) :=
Eq.refl (cocone.X (cone.unop c))
/--
The category of cocones on `F`
is equivalent to the opposite category of
the category of cones on the opposite of `F`.
-/
@[simp] theorem cocone_equivalence_op_cone_op_unit_iso {J : Type v} [small_category J] {C : Type u}
[category C] (F : J ⥤ C) :
equivalence.unit_iso (cocone_equivalence_op_cone_op F) =
nat_iso.of_components
(fun (c : cocone F) =>
cocones.ext (iso.refl (cocone.X (functor.obj 𝟭 c)))
(cocone_equivalence_op_cone_op._proof_7 F c))
(cocone_equivalence_op_cone_op._proof_8 F) :=
Eq.refl (equivalence.unit_iso (cocone_equivalence_op_cone_op F))
/-- Change a cocone on `F.left_op : Jᵒᵖ ⥤ C` to a cocone on `F : J ⥤ Cᵒᵖ`. -/
-- Here and below we only automatically generate the `@[simp]` lemma for the `X` field,
-- as we can write a simpler `rfl` lemma for the components of the natural transformation by hand.
def cone_of_cocone_left_op {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ (Cᵒᵖ)}
(c : cocone (functor.left_op F)) : cone F :=
cone.mk (opposite.op (cocone.X c))
(nat_trans.remove_left_op
(cocone.ι c ≫ iso.hom (functor.const.op_obj_unop (opposite.op (cocone.X c)))))
/-- Change a cone on `F : J ⥤ Cᵒᵖ` to a cocone on `F.left_op : Jᵒᵖ ⥤ C`. -/
def cocone_left_op_of_cone {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ (Cᵒᵖ)}
(c : cone F) : cocone (functor.left_op F) :=
cocone.mk (opposite.unop (cone.X c)) (nat_trans.left_op (cone.π c))
/-- Change a cone on `F.left_op : Jᵒᵖ ⥤ C` to a cocone on `F : J ⥤ Cᵒᵖ`. -/
/- When trying use `@[simps]` to generate the `ι_app` field of this definition, `@[simps]` tries to
reduce the RHS using `expr.dsimp` and `expr.simp`, but for some reason the expression is not
being simplified properly. -/
def cocone_of_cone_left_op {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ (Cᵒᵖ)}
(c : cone (functor.left_op F)) : cocone F :=
cocone.mk (opposite.op (cone.X c))
(nat_trans.remove_left_op
(iso.hom (functor.const.op_obj_unop (opposite.op (cone.X c))) ≫ cone.π c))
@[simp] theorem cocone_of_cone_left_op_ι_app {J : Type v} [small_category J] {C : Type u}
[category C] {F : J ⥤ (Cᵒᵖ)} (c : cone (functor.left_op F)) (j : J) :
nat_trans.app (cocone.ι (cocone_of_cone_left_op c)) j =
has_hom.hom.op (nat_trans.app (cone.π c) (opposite.op j)) :=
sorry
/-- Change a cocone on `F : J ⥤ Cᵒᵖ` to a cone on `F.left_op : Jᵒᵖ ⥤ C`. -/
def cone_left_op_of_cocone {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ (Cᵒᵖ)}
(c : cocone F) : cone (functor.left_op F) :=
cone.mk (opposite.unop (cocone.X c)) (nat_trans.left_op (cocone.ι c))
end category_theory.limits
namespace category_theory.functor
/-- The opposite cocone of the image of a cone is the image of the opposite cocone. -/
def map_cone_op {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C} {D : Type u'}
[category D] (G : C ⥤ D) (t : limits.cone F) :
limits.cone.op (map_cone G t) ≅ map_cocone (functor.op G) (limits.cone.op t) :=
limits.cocones.ext (iso.refl (limits.cocone.X (limits.cone.op (map_cone G t)))) sorry
/-- The opposite cone of the image of a cocone is the image of the opposite cone. -/
def map_cocone_op {J : Type v} [small_category J] {C : Type u} [category C] {F : J ⥤ C}
{D : Type u'} [category D] (G : C ⥤ D) {t : limits.cocone F} :
limits.cocone.op (map_cocone G t) ≅ map_cone (functor.op G) (limits.cocone.op t) :=
limits.cones.ext (iso.refl (limits.cone.X (limits.cocone.op (map_cocone G t)))) sorry
end Mathlib |
bd290e973e6f365ad8a488fbc9e21014dd5511d8 | b561a44b48979a98df50ade0789a21c79ee31288 | /stage0/src/Lean/Meta/Tactic/Simp/Rewrite.lean | b64f5555207ac9b0fbd35d236398b6804e5aa08f | [
"Apache-2.0"
] | permissive | 3401ijk/lean4 | 97659c475ebd33a034fed515cb83a85f75ccfb06 | a5b1b8de4f4b038ff752b9e607b721f15a9a4351 | refs/heads/master | 1,693,933,007,651 | 1,636,424,845,000 | 1,636,424,845,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,134 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.AppBuilder
import Lean.Meta.SynthInstance
import Lean.Meta.Tactic.Simp.Types
namespace Lean.Meta.Simp
def synthesizeArgs (lemmaName : Name) (xs : Array Expr) (bis : Array BinderInfo) (discharge? : Expr → SimpM (Option Expr)) : SimpM Bool := do
for x in xs, bi in bis do
let type ← inferType x
if bi.isInstImplicit then
unless (← synthesizeInstance x type) do
return false
else if (← instantiateMVars x).isMVar then
if (← isProp type) then
match (← discharge? type) with
| some proof =>
unless (← isDefEq x proof) do
trace[Meta.Tactic.simp.discharge] "{lemmaName}, failed to assign proof{indentExpr type}"
return false
| none =>
trace[Meta.Tactic.simp.discharge] "{lemmaName}, failed to discharge hypotheses{indentExpr type}"
return false
else if (← isClass? type).isSome then
unless (← synthesizeInstance x type) do
return false
return true
where
synthesizeInstance (x type : Expr) : SimpM Bool := do
match (← trySynthInstance type) with
| LOption.some val =>
if (← isDefEq x val) then
return true
else
trace[Meta.Tactic.simp.discharge] "{lemmaName}, failed to assign instance{indentExpr type}"
return false
| _ =>
trace[Meta.Tactic.simp.discharge] "{lemmaName}, failed to synthesize instance{indentExpr type}"
return false
private def tryLemmaCore (lhs : Expr) (xs : Array Expr) (bis : Array BinderInfo) (val : Expr) (type : Expr) (e : Expr) (lemma : SimpLemma) (numExtraArgs : Nat) (discharge? : Expr → SimpM (Option Expr)) : SimpM (Option Result) := do
let rec go (e : Expr) : SimpM (Option Result) := do
if (← isDefEq lhs e) then
unless (← synthesizeArgs lemma.getName xs bis discharge?) do
return none
let proof ← instantiateMVars (mkAppN val xs)
if ← hasAssignableMVar proof then
trace[Meta.Tactic.simp.rewrite] "{lemma}, has unassigned metavariables after unification"
return none
let rhs ← instantiateMVars type.appArg!
if e == rhs then
return none
if lemma.perm && !Expr.lt rhs e then
trace[Meta.Tactic.simp.rewrite] "{lemma}, perm rejected {e} ==> {rhs}"
return none
trace[Meta.Tactic.simp.rewrite] "{lemma}, {e} ==> {rhs}"
return some { expr := rhs, proof? := proof }
else
unless lhs.isMVar do
-- We do not report unification failures when `lhs` is a metavariable
-- Example: `x = ()`
-- TODO: reconsider if we want lemmas such as `(x : Unit) → x = ()`
trace[Meta.Tactic.simp.unify] "{lemma}, failed to unify {lhs} with {e}"
return none
/- Check whether we need something more sophisticated here.
This simple approach was good enough for Mathlib 3 -/
let mut extraArgs := #[]
let mut e := e
for i in [:numExtraArgs] do
extraArgs := extraArgs.push e.appArg!
e := e.appFn!
extraArgs := extraArgs.reverse
match (← go e) with
| none => return none
| some { expr := eNew, proof? := none } => return some { expr := mkAppN eNew extraArgs }
| some { expr := eNew, proof? := some proof } =>
let mut proof := proof
for extraArg in extraArgs do
proof ← mkCongrFun proof extraArg
return some { expr := mkAppN eNew extraArgs, proof? := some proof }
def tryLemmaWithExtraArgs? (e : Expr) (lemma : SimpLemma) (numExtraArgs : Nat) (discharge? : Expr → SimpM (Option Expr)) : SimpM (Option Result) :=
withNewMCtxDepth do
let val ← lemma.getValue
let type ← inferType val
let (xs, bis, type) ← forallMetaTelescopeReducing type
let type ← whnf (← instantiateMVars type)
let lhs := type.appFn!.appArg!
tryLemmaCore lhs xs bis val type e lemma numExtraArgs discharge?
def tryLemma? (e : Expr) (lemma : SimpLemma) (discharge? : Expr → SimpM (Option Expr)) : SimpM (Option Result) := do
withNewMCtxDepth do
let val ← lemma.getValue
let type ← inferType val
let (xs, bis, type) ← forallMetaTelescopeReducing type
let type ← whnf (← instantiateMVars type)
let lhs := type.appFn!.appArg!
match (← tryLemmaCore lhs xs bis val type e lemma 0 discharge?) with
| some result => return some result
| none =>
let lhsNumArgs := lhs.getAppNumArgs
let eNumArgs := e.getAppNumArgs
if eNumArgs > lhsNumArgs then
tryLemmaCore lhs xs bis val type e lemma (eNumArgs - lhsNumArgs) discharge?
else
return none
/-
Remark: the parameter tag is used for creating trace messages. It is irrelevant otherwise.
-/
def rewrite (e : Expr) (s : DiscrTree SimpLemma) (erased : Std.PHashSet Name) (discharge? : Expr → SimpM (Option Expr)) (tag : String) : SimpM Result := do
let candidates ← s.getMatchWithExtra e
if candidates.isEmpty then
trace[Debug.Meta.Tactic.simp] "no theorems found for {tag}-rewriting {e}"
return { expr := e }
else
let candidates := candidates.insertionSort fun e₁ e₂ => e₁.1.priority < e₂.1.priority
for (lemma, numExtraArgs) in candidates do
unless inErasedSet lemma do
if let some result ← tryLemmaWithExtraArgs? e lemma numExtraArgs discharge? then
return result
return { expr := e }
where
inErasedSet (lemma : SimpLemma) : Bool :=
match lemma.name? with
| none => false
| some name => erased.contains name
def rewriteCtorEq? (e : Expr) : MetaM (Option Result) := withReducibleAndInstances do
match e.eq? with
| none => return none
| some (_, lhs, rhs) =>
let lhs ← whnf lhs
let rhs ← whnf rhs
let env ← getEnv
match lhs.constructorApp? env, rhs.constructorApp? env with
| some (c₁, _), some (c₂, _) =>
if c₁.name != c₂.name then
withLocalDeclD `h e fun h =>
return some { expr := mkConst ``False, proof? := (← mkEqFalse' (← mkLambdaFVars #[h] (← mkNoConfusion (mkConst ``False) h))) }
else
return none
| _, _ => return none
@[inline] def tryRewriteCtorEq (e : Expr) (x : SimpM Step) : SimpM Step := do
match (← rewriteCtorEq? e) with
| some r => return Step.done r
| none => x
def rewriteUsingDecide? (e : Expr) : MetaM (Option Result) := withReducibleAndInstances do
if e.hasFVar || e.hasMVar || e.isConstOf ``True || e.isConstOf ``False then
return none
else
try
let d ← mkDecide e
let r ← withDefault <| whnf d
if r.isConstOf ``true then
return some { expr := mkConst ``True, proof? := mkAppN (mkConst ``eq_true_of_decide) #[e, d.appArg!, (← mkEqRefl (mkConst ``true))] }
else if r.isConstOf ``false then
let h ← mkEqRefl d
return some { expr := mkConst ``False, proof? := mkAppN (mkConst ``eq_false_of_decide) #[e, d.appArg!, (← mkEqRefl (mkConst ``false))] }
else
return none
catch _ =>
return none
@[inline] def tryRewriteUsingDecide (e : Expr) (x : SimpM Step) : SimpM Step := do
if (← read).config.decide then
match (← rewriteUsingDecide? e) with
| some r => return Step.done r
| none => x
else
x
def rewritePre (e : Expr) (discharge? : Expr → SimpM (Option Expr)) : SimpM Step := do
let lemmas ← (← read).simpLemmas
return Step.visit (← rewrite e lemmas.pre lemmas.erased discharge? (tag := "pre"))
def rewritePost (e : Expr) (discharge? : Expr → SimpM (Option Expr)) : SimpM Step := do
let lemmas ← (← read).simpLemmas
return Step.visit (← rewrite e lemmas.post lemmas.erased discharge? (tag := "post"))
def preDefault (e : Expr) (discharge? : Expr → SimpM (Option Expr)) : SimpM Step :=
tryRewriteCtorEq e <| rewritePre e discharge?
def postDefault (e : Expr) (discharge? : Expr → SimpM (Option Expr)) : SimpM Step := do
-- TODO: try equation lemmas
tryRewriteCtorEq e <| tryRewriteUsingDecide e <| rewritePost e discharge?
end Lean.Meta.Simp
|
dcfc2e2e3fae8e565fb7f518164c42ddb08d3c13 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/deprecated/subfield_auto.lean | 59d07a277a6eb7b02dc90af22ccd80688c83d6d8 | [] | 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,067 | lean | /-
Copyright (c) 2018 Andreas Swerdlow. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andreas Swerdlow
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.deprecated.subring
import Mathlib.algebra.group_with_zero.power
import Mathlib.PostPort
universes u_1 l u_2
namespace Mathlib
class is_subfield {F : Type u_1} [field F] (S : set F) extends is_subring S where
inv_mem : ∀ {x : F}, x ∈ S → x⁻¹ ∈ S
protected instance is_subfield.field {F : Type u_1} [field F] (S : set F) [is_subfield S] :
field ↥S :=
let cr_inst : comm_ring ↥S := subset.comm_ring;
field.mk comm_ring.add sorry comm_ring.zero sorry sorry comm_ring.neg comm_ring.sub sorry sorry
comm_ring.mul sorry comm_ring.one sorry sorry sorry sorry sorry
(fun (x : ↥S) => { val := ↑x⁻¹, property := sorry }) sorry sorry sorry
theorem is_subfield.pow_mem {F : Type u_1} [field F] {a : F} {n : ℤ} {s : set F} [is_subfield s]
(h : a ∈ s) : a ^ n ∈ s :=
int.cases_on n (fun (n : ℕ) => is_submonoid.pow_mem h)
fun (n : ℕ) => is_subfield.inv_mem (is_submonoid.pow_mem h)
protected instance univ.is_subfield {F : Type u_1} [field F] : is_subfield set.univ :=
is_subfield.mk fun (x : F) (ᾰ : x ∈ set.univ) => trivial
/- note: in the next two declarations, if we let type-class inference figure out the instance
`ring_hom.is_subring_preimage` then that instance only applies when particular instances of
`is_add_subgroup _` and `is_submonoid _` are chosen (which are not the default ones).
If we specify it explicitly, then it doesn't complain. -/
protected instance preimage.is_subfield {F : Type u_1} [field F] {K : Type u_2} [field K]
(f : F →+* K) (s : set K) [is_subfield s] : is_subfield (⇑f ⁻¹' s) :=
is_subfield.mk
fun (a : F) (ha : coe_fn f a ∈ s) =>
(fun (this : coe_fn f (a⁻¹) ∈ s) => this)
(eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn f (a⁻¹) ∈ s)) (ring_hom.map_inv f a)))
(is_subfield.inv_mem ha))
protected instance image.is_subfield {F : Type u_1} [field F] {K : Type u_2} [field K] (f : F →+* K)
(s : set F) [is_subfield s] : is_subfield (⇑f '' s) :=
is_subfield.mk fun (a : K) (_x : a ∈ ⇑f '' s) => sorry
protected instance range.is_subfield {F : Type u_1} [field F] {K : Type u_2} [field K]
(f : F →+* K) : is_subfield (set.range ⇑f) :=
eq.mpr (id (Eq._oldrec (Eq.refl (is_subfield (set.range ⇑f))) (Eq.symm set.image_univ)))
(image.is_subfield f set.univ)
namespace field
/-- `field.closure s` is the minimal subfield that includes `s`. -/
def closure {F : Type u_1} [field F] (S : set F) : set F :=
set_of
fun (x : F) =>
∃ (y : F), ∃ (H : y ∈ ring.closure S), ∃ (z : F), ∃ (H : z ∈ ring.closure S), y / z = x
theorem ring_closure_subset {F : Type u_1} [field F] {S : set F} : ring.closure S ⊆ closure S :=
fun (x : F) (hx : x ∈ ring.closure S) =>
Exists.intro x
(Exists.intro hx (Exists.intro 1 (Exists.intro is_submonoid.one_mem (div_one x))))
protected instance closure.is_submonoid {F : Type u_1} [field F] {S : set F} :
is_submonoid (closure S) :=
sorry
protected instance closure.is_subfield {F : Type u_1} [field F] {S : set F} :
is_subfield (closure S) :=
sorry
theorem mem_closure {F : Type u_1} [field F] {S : set F} {a : F} (ha : a ∈ S) : a ∈ closure S :=
ring_closure_subset (ring.mem_closure ha)
theorem subset_closure {F : Type u_1} [field F] {S : set F} : S ⊆ closure S :=
fun (_x : F) => mem_closure
theorem closure_subset {F : Type u_1} [field F] {S : set F} {T : set F} [is_subfield T]
(H : S ⊆ T) : closure S ⊆ T :=
sorry
theorem closure_subset_iff {F : Type u_1} [field F] (s : set F) (t : set F) [is_subfield t] :
closure s ⊆ t ↔ s ⊆ t :=
{ mp := set.subset.trans subset_closure, mpr := closure_subset }
theorem closure_mono {F : Type u_1} [field F] {s : set F} {t : set F} (H : s ⊆ t) :
closure s ⊆ closure t :=
closure_subset (set.subset.trans H subset_closure)
end field
theorem is_subfield_Union_of_directed {F : Type u_1} [field F] {ι : Type u_2} [hι : Nonempty ι]
(s : ι → set F) [∀ (i : ι), is_subfield (s i)]
(directed : ∀ (i j : ι), ∃ (k : ι), s i ⊆ s k ∧ s j ⊆ s k) :
is_subfield (set.Union fun (i : ι) => s i) :=
sorry
protected instance is_subfield.inter {F : Type u_1} [field F] (S₁ : set F) (S₂ : set F)
[is_subfield S₁] [is_subfield S₂] : is_subfield (S₁ ∩ S₂) :=
is_subfield.mk
fun (x : F) (hx : x ∈ S₁ ∩ S₂) =>
{ left := is_subfield.inv_mem (and.left hx), right := is_subfield.inv_mem (and.right hx) }
protected instance is_subfield.Inter {F : Type u_1} [field F] {ι : Sort u_2} (S : ι → set F)
[h : ∀ (y : ι), is_subfield (S y)] : is_subfield (set.Inter S) :=
is_subfield.mk
fun (x : F) (hx : x ∈ set.Inter S) =>
iff.mpr set.mem_Inter fun (y : ι) => is_subfield.inv_mem (iff.mp set.mem_Inter hx y)
end Mathlib |
f900081c2c634a335f6cbcd0f309ea302ca87764 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/group_theory/group_action/pi.lean | afae7ce4c1bc232d0d65c34afb013d40be568200 | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 7,724 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Patrick Massot
-/
import algebra.group.pi
import group_theory.group_action.defs
/-!
# Pi instances for multiplicative actions
This file defines instances for mul_action and related structures on Pi Types
-/
universes u v w
variable {I : Type u} -- The indexing type
variable {f : I → Type v} -- The family of types already equipped with instances
variables (x y : Π i, f i) (i : I)
namespace pi
@[to_additive pi.has_vadd]
instance has_scalar {α : Type*} [Π i, has_scalar α $ f i] :
has_scalar α (Π i : I, f i) :=
⟨λ s x, λ i, s • (x i)⟩
@[to_additive]
lemma smul_def {α : Type*} [Π i, has_scalar α $ f i] (s : α) : s • x = λ i, s • x i := rfl
@[simp, to_additive]
lemma smul_apply {α : Type*} [Π i, has_scalar α $ f i] (s : α) : (s • x) i = s • x i := rfl
@[to_additive pi.has_vadd']
instance has_scalar' {g : I → Type*} [Π i, has_scalar (f i) (g i)] :
has_scalar (Π i, f i) (Π i : I, g i) :=
⟨λ s x, λ i, (s i) • (x i)⟩
@[simp, to_additive]
lemma smul_apply' {g : I → Type*} [∀ i, has_scalar (f i) (g i)] (s : Π i, f i) (x : Π i, g i) :
(s • x) i = s i • x i :=
rfl
instance is_scalar_tower {α β : Type*}
[has_scalar α β] [Π i, has_scalar β $ f i] [Π i, has_scalar α $ f i]
[Π i, is_scalar_tower α β (f i)] : is_scalar_tower α β (Π i : I, f i) :=
⟨λ x y z, funext $ λ i, smul_assoc x y (z i)⟩
instance is_scalar_tower' {g : I → Type*} {α : Type*}
[Π i, has_scalar α $ f i] [Π i, has_scalar (f i) (g i)] [Π i, has_scalar α $ g i]
[Π i, is_scalar_tower α (f i) (g i)] : is_scalar_tower α (Π i : I, f i) (Π i : I, g i) :=
⟨λ x y z, funext $ λ i, smul_assoc x (y i) (z i)⟩
instance is_scalar_tower'' {g : I → Type*} {h : I → Type*}
[Π i, has_scalar (f i) (g i)] [Π i, has_scalar (g i) (h i)] [Π i, has_scalar (f i) (h i)]
[Π i, is_scalar_tower (f i) (g i) (h i)] : is_scalar_tower (Π i, f i) (Π i, g i) (Π i, h i) :=
⟨λ x y z, funext $ λ i, smul_assoc (x i) (y i) (z i)⟩
@[to_additive]
instance smul_comm_class {α β : Type*}
[Π i, has_scalar α $ f i] [Π i, has_scalar β $ f i] [∀ i, smul_comm_class α β (f i)] :
smul_comm_class α β (Π i : I, f i) :=
⟨λ x y z, funext $ λ i, smul_comm x y (z i)⟩
@[to_additive]
instance smul_comm_class' {g : I → Type*} {α : Type*}
[Π i, has_scalar α $ g i] [Π i, has_scalar (f i) (g i)] [∀ i, smul_comm_class α (f i) (g i)] :
smul_comm_class α (Π i : I, f i) (Π i : I, g i) :=
⟨λ x y z, funext $ λ i, smul_comm x (y i) (z i)⟩
@[to_additive]
instance smul_comm_class'' {g : I → Type*} {h : I → Type*}
[Π i, has_scalar (g i) (h i)] [Π i, has_scalar (f i) (h i)]
[∀ i, smul_comm_class (f i) (g i) (h i)] : smul_comm_class (Π i, f i) (Π i, g i) (Π i, h i) :=
⟨λ x y z, funext $ λ i, smul_comm (x i) (y i) (z i)⟩
instance {α : Type*} [Π i, has_scalar α $ f i] [Π i, has_scalar αᵐᵒᵖ $ f i]
[∀ i, is_central_scalar α (f i)] : is_central_scalar α (Π i, f i) :=
⟨λ r m, funext $ λ i, op_smul_eq_smul _ _⟩
/-- If `f i` has a faithful scalar action for a given `i`, then so does `Π i, f i`. This is
not an instance as `i` cannot be inferred. -/
@[to_additive pi.has_faithful_vadd_at]
lemma has_faithful_scalar_at {α : Type*}
[Π i, has_scalar α $ f i] [Π i, nonempty (f i)] (i : I) [has_faithful_scalar α (f i)] :
has_faithful_scalar α (Π i, f i) :=
⟨λ x y h, eq_of_smul_eq_smul $ λ a : f i, begin
classical,
have := congr_fun (h $ function.update (λ j, classical.choice (‹Π i, nonempty (f i)› j)) i a) i,
simpa using this,
end⟩
@[to_additive pi.has_faithful_vadd]
instance has_faithful_scalar {α : Type*}
[nonempty I] [Π i, has_scalar α $ f i] [Π i, nonempty (f i)] [Π i, has_faithful_scalar α (f i)] :
has_faithful_scalar α (Π i, f i) :=
let ⟨i⟩ := ‹nonempty I› in has_faithful_scalar_at i
@[to_additive]
instance mul_action (α) {m : monoid α} [Π i, mul_action α $ f i] :
@mul_action α (Π i : I, f i) m :=
{ smul := (•),
mul_smul := λ r s f, funext $ λ i, mul_smul _ _ _,
one_smul := λ f, funext $ λ i, one_smul α _ }
@[to_additive]
instance mul_action' {g : I → Type*} {m : Π i, monoid (f i)} [Π i, mul_action (f i) (g i)] :
@mul_action (Π i, f i) (Π i : I, g i) (@pi.monoid I f m) :=
{ smul := (•),
mul_smul := λ r s f, funext $ λ i, mul_smul _ _ _,
one_smul := λ f, funext $ λ i, one_smul _ _ }
instance distrib_mul_action (α) {m : monoid α} {n : ∀ i, add_monoid $ f i}
[∀ i, distrib_mul_action α $ f i] :
@distrib_mul_action α (Π i : I, f i) m (@pi.add_monoid I f n) :=
{ smul_zero := λ c, funext $ λ i, smul_zero _,
smul_add := λ c f g, funext $ λ i, smul_add _ _ _,
..pi.mul_action _ }
instance distrib_mul_action' {g : I → Type*} {m : Π i, monoid (f i)} {n : Π i, add_monoid $ g i}
[Π i, distrib_mul_action (f i) (g i)] :
@distrib_mul_action (Π i, f i) (Π i : I, g i) (@pi.monoid I f m) (@pi.add_monoid I g n) :=
{ smul_add := by { intros, ext x, apply smul_add },
smul_zero := by { intros, ext x, apply smul_zero } }
lemma single_smul {α} [monoid α] [Π i, add_monoid $ f i]
[Π i, distrib_mul_action α $ f i] [decidable_eq I] (i : I) (r : α) (x : f i) :
single i (r • x) = r • single i x :=
single_op (λ i : I, ((•) r : f i → f i)) (λ j, smul_zero _) _ _
/-- A version of `pi.single_smul` for non-dependent functions. It is useful in cases Lean fails
to apply `pi.single_smul`. -/
lemma single_smul' {α β} [monoid α] [add_monoid β]
[distrib_mul_action α β] [decidable_eq I] (i : I) (r : α) (x : β) :
single i (r • x) = r • single i x :=
single_smul i r x
lemma single_smul₀ {g : I → Type*} [Π i, monoid_with_zero (f i)] [Π i, add_monoid (g i)]
[Π i, distrib_mul_action (f i) (g i)] [decidable_eq I] (i : I) (r : f i) (x : g i) :
single i (r • x) = single i r • single i x :=
single_op₂ (λ i : I, ((•) : f i → g i → g i)) (λ j, smul_zero _) _ _ _
instance mul_distrib_mul_action (α) {m : monoid α} {n : Π i, monoid $ f i}
[Π i, mul_distrib_mul_action α $ f i] :
@mul_distrib_mul_action α (Π i : I, f i) m (@pi.monoid I f n) :=
{ smul_one := λ c, funext $ λ i, smul_one _,
smul_mul := λ c f g, funext $ λ i, smul_mul' _ _ _,
..pi.mul_action _ }
instance mul_distrib_mul_action' {g : I → Type*} {m : Π i, monoid (f i)} {n : Π i, monoid $ g i}
[Π i, mul_distrib_mul_action (f i) (g i)] :
@mul_distrib_mul_action (Π i, f i) (Π i : I, g i) (@pi.monoid I f m) (@pi.monoid I g n) :=
{ smul_mul := by { intros, ext x, apply smul_mul' },
smul_one := by { intros, ext x, apply smul_one } }
end pi
namespace function
@[to_additive]
lemma update_smul {α : Type*} [Π i, has_scalar α (f i)] [decidable_eq I]
(c : α) (f₁ : Π i, f i) (i : I) (x₁ : f i) :
update (c • f₁) i (c • x₁) = c • update f₁ i x₁ :=
funext $ λ j, (apply_update (λ i, (•) c) f₁ i x₁ j).symm
end function
namespace set
@[to_additive]
lemma piecewise_smul {α : Type*} [Π i, has_scalar α (f i)] (s : set I) [Π i, decidable (i ∈ s)]
(c : α) (f₁ g₁ : Π i, f i) :
s.piecewise (c • f₁) (c • g₁) = c • s.piecewise f₁ g₁ :=
s.piecewise_op _ _ (λ _, (•) c)
end set
section extend
@[to_additive] lemma function.extend_smul {R α β γ : Type*} [has_scalar R γ]
(r : R) (f : α → β) (g : α → γ) (e : β → γ) :
function.extend f (r • g) (r • e) = r • function.extend f g e :=
funext $ λ _, by convert (apply_dite ((•) r) _ _ _).symm
end extend
|
098756520f97c16c6a13891f20592e1544493c0e | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/algebra/group/hom.lean | c8c3e5af7f5a65cc4eb655e5a45023b2b748ce8d | [
"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 | 35,577 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Kevin Buzzard, Scott Morrison, Johan Commelin, Chris Hughes,
Johannes Hölzl, Yury Kudryashov
-/
import algebra.group.commute
import algebra.group_with_zero.defs
/-!
# monoid and group homomorphisms
This file defines the bundled structures for monoid and group homomorphisms. Namely, we define
`monoid_hom` (resp., `add_monoid_hom`) to be bundled homomorphisms between multiplicative (resp.,
additive) monoids or groups.
We also define coercion to a function, and usual operations: composition, identity homomorphism,
pointwise multiplication and pointwise inversion.
This file also defines the lesser-used (and notation-less) homomorphism types which are used as
building blocks for other homomorphisms:
* `zero_hom`
* `one_hom`
* `add_hom`
* `mul_hom`
* `monoid_with_zero_hom`
## Notations
* `→*` for bundled monoid homs (also use for group homs)
* `→+` for bundled add_monoid homs (also use for add_group homs)
## implementation notes
There's a coercion from bundled homs to fun, and the canonical
notation is to use the bundled hom as a function via this coercion.
There is no `group_hom` -- the idea is that `monoid_hom` is used.
The constructor for `monoid_hom` needs a proof of `map_one` as well
as `map_mul`; a separate constructor `monoid_hom.mk'` will construct
group homs (i.e. monoid homs between groups) given only a proof
that multiplication is preserved,
Implicit `{}` brackets are often used instead of type class `[]` brackets. This is done when the
instances can be inferred because they are implicit arguments to the type `monoid_hom`. When they
can be inferred from the type it is faster to use this method than to use type class inference.
Historically this file also included definitions of unbundled homomorphism classes; they were
deprecated and moved to `deprecated/group`.
## Tags
monoid_hom, add_monoid_hom
-/
variables {M : Type*} {N : Type*} {P : Type*} -- monoids
{G : Type*} {H : Type*} -- groups
-- for easy multiple inheritance
set_option old_structure_cmd true
/-- Homomorphism that preserves zero -/
structure zero_hom (M : Type*) (N : Type*) [has_zero M] [has_zero N] :=
(to_fun : M → N)
(map_zero' : to_fun 0 = 0)
/-- Homomorphism that preserves addition -/
structure add_hom (M : Type*) (N : Type*) [has_add M] [has_add N] :=
(to_fun : M → N)
(map_add' : ∀ x y, to_fun (x + y) = to_fun x + to_fun y)
/-- Bundled add_monoid homomorphisms; use this for bundled add_group homomorphisms too. -/
structure add_monoid_hom (M : Type*) (N : Type*) [add_monoid M] [add_monoid N]
extends zero_hom M N, add_hom M N
attribute [nolint doc_blame] add_monoid_hom.to_add_hom
attribute [nolint doc_blame] add_monoid_hom.to_zero_hom
infixr ` →+ `:25 := add_monoid_hom
/-- Homomorphism that preserves one -/
@[to_additive]
structure one_hom (M : Type*) (N : Type*) [has_one M] [has_one N] :=
(to_fun : M → N)
(map_one' : to_fun 1 = 1)
/-- Homomorphism that preserves multiplication -/
@[to_additive]
structure mul_hom (M : Type*) (N : Type*) [has_mul M] [has_mul N] :=
(to_fun : M → N)
(map_mul' : ∀ x y, to_fun (x * y) = to_fun x * to_fun y)
/-- Bundled monoid homomorphisms; use this for bundled group homomorphisms too. -/
@[to_additive]
structure monoid_hom (M : Type*) (N : Type*) [monoid M] [monoid N] extends one_hom M N, mul_hom M N
/-- Bundled monoid with zero homomorphisms; use this for bundled group with zero homomorphisms
too. -/
structure monoid_with_zero_hom (M : Type*) (N : Type*) [monoid_with_zero M] [monoid_with_zero N]
extends zero_hom M N, monoid_hom M N
attribute [nolint doc_blame, to_additive] monoid_hom.to_mul_hom
attribute [nolint doc_blame, to_additive] monoid_hom.to_one_hom
attribute [nolint doc_blame] monoid_with_zero_hom.to_monoid_hom
attribute [nolint doc_blame] monoid_with_zero_hom.to_zero_hom
infixr ` →* `:25 := monoid_hom
-- completely uninteresting lemmas about coercion to function, that all homs need
section coes
/-! Bundled morphisms can be down-cast to weaker bundlings -/
@[to_additive]
instance monoid_hom.has_coe_to_one_hom {mM : monoid M} {mN : monoid N} :
has_coe (M →* N) (one_hom M N) := ⟨monoid_hom.to_one_hom⟩
@[to_additive]
instance monoid_hom.has_coe_to_mul_hom {mM : monoid M} {mN : monoid N} :
has_coe (M →* N) (mul_hom M N) := ⟨monoid_hom.to_mul_hom⟩
instance monoid_with_zero_hom.has_coe_to_monoid_hom
{mM : monoid_with_zero M} {mN : monoid_with_zero N} :
has_coe (monoid_with_zero_hom M N) (M →* N) := ⟨monoid_with_zero_hom.to_monoid_hom⟩
instance monoid_with_zero_hom.has_coe_to_zero_hom
{mM : monoid_with_zero M} {mN : monoid_with_zero N} :
has_coe (monoid_with_zero_hom M N) (zero_hom M N) := ⟨monoid_with_zero_hom.to_zero_hom⟩
/-! The simp-normal form of morphism coercion is `f.to_..._hom`. This choice is primarily because
this is the way things were before the above coercions were introduced. Bundled morphisms defined
elsewhere in Mathlib may choose `↑f` as their simp-normal form instead. -/
@[simp, to_additive]
lemma monoid_hom.coe_eq_to_one_hom {mM : monoid M} {mN : monoid N} (f : M →* N) :
(f : one_hom M N) = f.to_one_hom := rfl
@[simp, to_additive]
lemma monoid_hom.coe_eq_to_mul_hom {mM : monoid M} {mN : monoid N} (f : M →* N) :
(f : mul_hom M N) = f.to_mul_hom := rfl
@[simp]
lemma monoid_with_zero_hom.coe_eq_to_monoid_hom {mM : monoid_with_zero M} {mN : monoid_with_zero N}
(f : monoid_with_zero_hom M N) : (f : M →* N) = f.to_monoid_hom := rfl
@[simp]
lemma monoid_with_zero_hom.coe_eq_to_zero_hom {mM : monoid_with_zero M} {mN : monoid_with_zero N}
(f : monoid_with_zero_hom M N) : (f : zero_hom M N) = f.to_zero_hom := rfl
@[to_additive]
instance {mM : has_one M} {mN : has_one N} : has_coe_to_fun (one_hom M N) :=
⟨_, one_hom.to_fun⟩
@[to_additive]
instance {mM : has_mul M} {mN : has_mul N} : has_coe_to_fun (mul_hom M N) :=
⟨_, mul_hom.to_fun⟩
@[to_additive]
instance {mM : monoid M} {mN : monoid N} : has_coe_to_fun (M →* N) :=
⟨_, monoid_hom.to_fun⟩
instance {mM : monoid_with_zero M} {mN : monoid_with_zero N} :
has_coe_to_fun (monoid_with_zero_hom M N) :=
⟨_, monoid_with_zero_hom.to_fun⟩
-- these must come after the coe_to_fun definitions
initialize_simps_projections zero_hom (to_fun → apply)
initialize_simps_projections add_hom (to_fun → apply)
initialize_simps_projections add_monoid_hom (to_fun → apply)
initialize_simps_projections one_hom (to_fun → apply)
initialize_simps_projections mul_hom (to_fun → apply)
initialize_simps_projections monoid_hom (to_fun → apply)
initialize_simps_projections monoid_with_zero_hom (to_fun → apply)
@[simp, to_additive]
lemma one_hom.to_fun_eq_coe [has_one M] [has_one N] (f : one_hom M N) : f.to_fun = f := rfl
@[simp, to_additive]
lemma mul_hom.to_fun_eq_coe [has_mul M] [has_mul N] (f : mul_hom M N) : f.to_fun = f := rfl
@[simp, to_additive]
lemma monoid_hom.to_fun_eq_coe [monoid M] [monoid N] (f : M →* N) : f.to_fun = f := rfl
@[simp]
lemma monoid_with_zero_hom.to_fun_eq_coe [monoid_with_zero M] [monoid_with_zero N]
(f : monoid_with_zero_hom M N) : f.to_fun = f := rfl
@[simp, to_additive]
lemma one_hom.coe_mk [has_one M] [has_one N]
(f : M → N) (h1) : ⇑(one_hom.mk f h1) = f := rfl
@[simp, to_additive]
lemma mul_hom.coe_mk [has_mul M] [has_mul N]
(f : M → N) (hmul) : ⇑(mul_hom.mk f hmul) = f := rfl
@[simp, to_additive]
lemma monoid_hom.coe_mk [monoid M] [monoid N]
(f : M → N) (h1 hmul) : ⇑(monoid_hom.mk f h1 hmul) = f := rfl
@[simp]
lemma monoid_with_zero_hom.coe_mk [monoid_with_zero M] [monoid_with_zero N]
(f : M → N) (h0 h1 hmul) : ⇑(monoid_with_zero_hom.mk f h0 h1 hmul) = f := rfl
@[simp, to_additive]
lemma monoid_hom.to_one_hom_coe [monoid M] [monoid N] (f : M →* N) :
(f.to_one_hom : M → N) = f := rfl
@[simp, to_additive]
lemma monoid_hom.to_mul_hom_coe [monoid M] [monoid N] (f : M →* N) :
(f.to_mul_hom : M → N) = f := rfl
@[simp]
lemma monoid_with_zero_hom.to_zero_hom_coe [monoid_with_zero M] [monoid_with_zero N]
(f : monoid_with_zero_hom M N) :
(f.to_zero_hom : M → N) = f := rfl
@[simp]
lemma monoid_with_zero_hom.to_monoid_hom_coe [monoid_with_zero M] [monoid_with_zero N]
(f : monoid_with_zero_hom M N) :
(f.to_monoid_hom : M → N) = f := rfl
@[to_additive]
theorem one_hom.congr_fun [has_one M] [has_one N]
{f g : one_hom M N} (h : f = g) (x : M) : f x = g x :=
congr_arg (λ h : one_hom M N, h x) h
@[to_additive]
theorem mul_hom.congr_fun [has_mul M] [has_mul N]
{f g : mul_hom M N} (h : f = g) (x : M) : f x = g x :=
congr_arg (λ h : mul_hom M N, h x) h
@[to_additive]
theorem monoid_hom.congr_fun [monoid M] [monoid N]
{f g : M →* N} (h : f = g) (x : M) : f x = g x :=
congr_arg (λ h : M →* N, h x) h
theorem monoid_with_zero_hom.congr_fun [monoid_with_zero M] [monoid_with_zero N]
{f g : monoid_with_zero_hom M N} (h : f = g) (x : M) : f x = g x :=
congr_arg (λ h : monoid_with_zero_hom M N, h x) h
@[to_additive]
theorem one_hom.congr_arg [has_one M] [has_one N]
(f : one_hom M N) {x y : M} (h : x = y) : f x = f y :=
congr_arg (λ x : M, f x) h
@[to_additive]
theorem mul_hom.congr_arg [has_mul M] [has_mul N]
(f : mul_hom M N) {x y : M} (h : x = y) : f x = f y :=
congr_arg (λ x : M, f x) h
@[to_additive]
theorem monoid_hom.congr_arg [monoid M] [monoid N]
(f : M →* N) {x y : M} (h : x = y) : f x = f y :=
congr_arg (λ x : M, f x) h
theorem monoid_with_zero_hom.congr_arg [monoid_with_zero M] [monoid_with_zero N]
(f : monoid_with_zero_hom M N) {x y : M} (h : x = y) : f x = f y :=
congr_arg (λ x : M, f x) h
@[to_additive]
lemma one_hom.coe_inj [has_one M] [has_one N] ⦃f g : one_hom M N⦄ (h : (f : M → N) = g) : f = g :=
by cases f; cases g; cases h; refl
@[to_additive]
lemma mul_hom.coe_inj [has_mul M] [has_mul N] ⦃f g : mul_hom M N⦄ (h : (f : M → N) = g) : f = g :=
by cases f; cases g; cases h; refl
@[to_additive]
lemma monoid_hom.coe_inj [monoid M] [monoid N] ⦃f g : M →* N⦄ (h : (f : M → N) = g) : f = g :=
by cases f; cases g; cases h; refl
lemma monoid_with_zero_hom.coe_inj [monoid_with_zero M] [monoid_with_zero N]
⦃f g : monoid_with_zero_hom M N⦄ (h : (f : M → N) = g) : f = g :=
by cases f; cases g; cases h; refl
@[ext, to_additive]
lemma one_hom.ext [has_one M] [has_one N] ⦃f g : one_hom M N⦄ (h : ∀ x, f x = g x) : f = g :=
one_hom.coe_inj (funext h)
@[ext, to_additive]
lemma mul_hom.ext [has_mul M] [has_mul N] ⦃f g : mul_hom M N⦄ (h : ∀ x, f x = g x) : f = g :=
mul_hom.coe_inj (funext h)
@[ext, to_additive]
lemma monoid_hom.ext [monoid M] [monoid N] ⦃f g : M →* N⦄ (h : ∀ x, f x = g x) : f = g :=
monoid_hom.coe_inj (funext h)
@[ext]
lemma monoid_with_zero_hom.ext [monoid_with_zero M] [monoid_with_zero N]
⦃f g : monoid_with_zero_hom M N⦄ (h : ∀ x, f x = g x) : f = g :=
monoid_with_zero_hom.coe_inj (funext h)
attribute [ext] zero_hom.ext add_hom.ext add_monoid_hom.ext
@[to_additive]
lemma one_hom.ext_iff [has_one M] [has_one N] {f g : one_hom M N} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, one_hom.ext h⟩
@[to_additive]
lemma mul_hom.ext_iff [has_mul M] [has_mul N] {f g : mul_hom M N} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, mul_hom.ext h⟩
@[to_additive]
lemma monoid_hom.ext_iff [monoid M] [monoid N] {f g : M →* N} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, monoid_hom.ext h⟩
lemma monoid_with_zero_hom.ext_iff [monoid_with_zero M] [monoid_with_zero N]
{f g : monoid_with_zero_hom M N} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, monoid_with_zero_hom.ext h⟩
end coes
@[simp, to_additive]
lemma one_hom.map_one [has_one M] [has_one N] (f : one_hom M N) : f 1 = 1 := f.map_one'
/-- If `f` is a monoid homomorphism then `f 1 = 1`. -/
@[simp, to_additive]
lemma monoid_hom.map_one [monoid M] [monoid N] (f : M →* N) : f 1 = 1 := f.map_one'
@[simp]
lemma monoid_with_zero_hom.map_one [monoid_with_zero M] [monoid_with_zero N]
(f : monoid_with_zero_hom M N) : f 1 = 1 := f.map_one'
/-- If `f` is an additive monoid homomorphism then `f 0 = 0`. -/
add_decl_doc add_monoid_hom.map_zero
@[simp]
lemma monoid_with_zero_hom.map_zero [monoid_with_zero M] [monoid_with_zero N]
(f : monoid_with_zero_hom M N) : f 0 = 0 := f.map_zero'
@[simp, to_additive]
lemma mul_hom.map_mul [has_mul M] [has_mul N]
(f : mul_hom M N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b
/-- If `f` is a monoid homomorphism then `f (a * b) = f a * f b`. -/
@[simp, to_additive]
lemma monoid_hom.map_mul [monoid M] [monoid N]
(f : M →* N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b
@[simp]
lemma monoid_with_zero_hom.map_mul [monoid_with_zero M] [monoid_with_zero N]
(f : monoid_with_zero_hom M N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b
/-- If `f` is an additive monoid homomorphism then `f (a + b) = f a + f b`. -/
add_decl_doc add_monoid_hom.map_add
namespace monoid_hom
variables {mM : monoid M} {mN : monoid N} {mP : monoid P}
variables [group G] [comm_group H]
include mM mN
@[to_additive]
lemma map_mul_eq_one (f : M →* N) {a b : M} (h : a * b = 1) : f a * f b = 1 :=
by rw [← f.map_mul, h, f.map_one]
/-- Given a monoid homomorphism `f : M →* N` and an element `x : M`, if `x` has a right inverse,
then `f x` has a right inverse too. For elements invertible on both sides see `is_unit.map`. -/
@[to_additive "Given an add_monoid homomorphism `f : M →+ N` and an element `x : M`, if `x` has
a right inverse, then `f x` has a right inverse too."]
lemma map_exists_right_inv (f : M →* N) {x : M} (hx : ∃ y, x * y = 1) :
∃ y, f x * y = 1 :=
let ⟨y, hy⟩ := hx in ⟨f y, f.map_mul_eq_one hy⟩
/-- Given a monoid homomorphism `f : M →* N` and an element `x : M`, if `x` has a left inverse,
then `f x` has a left inverse too. For elements invertible on both sides see `is_unit.map`. -/
@[to_additive "Given an add_monoid homomorphism `f : M →+ N` and an element `x : M`, if `x` has
a left inverse, then `f x` has a left inverse too. For elements invertible on both sides see
`is_add_unit.map`."]
lemma map_exists_left_inv (f : M →* N) {x : M} (hx : ∃ y, y * x = 1) :
∃ y, y * f x = 1 :=
let ⟨y, hy⟩ := hx in ⟨f y, f.map_mul_eq_one hy⟩
end monoid_hom
/-- The identity map from a type with 1 to itself. -/
@[to_additive]
def one_hom.id (M : Type*) [has_one M] : one_hom M M :=
{ to_fun := id, map_one' := rfl, }
/-- The identity map from a type with multiplication to itself. -/
@[to_additive]
def mul_hom.id (M : Type*) [has_mul M] : mul_hom M M :=
{ to_fun := id, map_mul' := λ _ _, rfl, }
/-- The identity map from a monoid to itself. -/
@[to_additive]
def monoid_hom.id (M : Type*) [monoid M] : M →* M :=
{ to_fun := id, map_one' := rfl, map_mul' := λ _ _, rfl, }
/-- The identity map from a monoid_with_zero to itself. -/
def monoid_with_zero_hom.id (M : Type*) [monoid_with_zero M] : monoid_with_zero_hom M M :=
{ to_fun := id, map_zero' := rfl, map_one' := rfl, map_mul' := λ _ _, rfl, }
/-- The identity map from an type with zero to itself. -/
add_decl_doc zero_hom.id
/-- The identity map from an type with addition to itself. -/
add_decl_doc add_hom.id
/-- The identity map from an additive monoid to itself. -/
add_decl_doc add_monoid_hom.id
@[simp, to_additive] lemma one_hom.id_apply {M : Type*} [has_one M] (x : M) :
one_hom.id M x = x := rfl
@[simp, to_additive] lemma mul_hom.id_apply {M : Type*} [has_mul M] (x : M) :
mul_hom.id M x = x := rfl
@[simp, to_additive] lemma monoid_hom.id_apply {M : Type*} [monoid M] (x : M) :
monoid_hom.id M x = x := rfl
@[simp] lemma monoid_with_zero_hom.id_apply {M : Type*} [monoid_with_zero M] (x : M) :
monoid_with_zero_hom.id M x = x := rfl
/-- Composition of `one_hom`s as a `one_hom`. -/
@[to_additive]
def one_hom.comp [has_one M] [has_one N] [has_one P]
(hnp : one_hom N P) (hmn : one_hom M N) : one_hom M P :=
{ to_fun := hnp ∘ hmn, map_one' := by simp, }
/-- Composition of `mul_hom`s as a `mul_hom`. -/
@[to_additive]
def mul_hom.comp [has_mul M] [has_mul N] [has_mul P]
(hnp : mul_hom N P) (hmn : mul_hom M N) : mul_hom M P :=
{ to_fun := hnp ∘ hmn, map_mul' := by simp, }
/-- Composition of monoid morphisms as a monoid morphism. -/
@[to_additive]
def monoid_hom.comp [monoid M] [monoid N] [monoid P] (hnp : N →* P) (hmn : M →* N) : M →* P :=
{ to_fun := hnp ∘ hmn, map_one' := by simp, map_mul' := by simp, }
/-- Composition of `monoid_with_zero_hom`s as a `monoid_with_zero_hom`. -/
def monoid_with_zero_hom.comp [monoid_with_zero M] [monoid_with_zero N] [monoid_with_zero P]
(hnp : monoid_with_zero_hom N P) (hmn : monoid_with_zero_hom M N) : monoid_with_zero_hom M P :=
{ to_fun := hnp ∘ hmn, map_zero' := by simp, map_one' := by simp, map_mul' := by simp, }
/-- Composition of `zero_hom`s as a `zero_hom`. -/
add_decl_doc zero_hom.comp
/-- Composition of `add_hom`s as a `add_hom`. -/
add_decl_doc add_hom.comp
/-- Composition of additive monoid morphisms as an additive monoid morphism. -/
add_decl_doc add_monoid_hom.comp
@[simp, to_additive] lemma one_hom.coe_comp [has_one M] [has_one N] [has_one P]
(g : one_hom N P) (f : one_hom M N) :
⇑(g.comp f) = g ∘ f := rfl
@[simp, to_additive] lemma mul_hom.coe_comp [has_mul M] [has_mul N] [has_mul P]
(g : mul_hom N P) (f : mul_hom M N) :
⇑(g.comp f) = g ∘ f := rfl
@[simp, to_additive] lemma monoid_hom.coe_comp [monoid M] [monoid N] [monoid P]
(g : N →* P) (f : M →* N) :
⇑(g.comp f) = g ∘ f := rfl
@[simp] lemma monoid_with_zero_hom.coe_comp [monoid_with_zero M] [monoid_with_zero N]
[monoid_with_zero P]
(g : monoid_with_zero_hom N P) (f : monoid_with_zero_hom M N) :
⇑(g.comp f) = g ∘ f := rfl
@[to_additive] lemma one_hom.comp_apply [has_one M] [has_one N] [has_one P]
(g : one_hom N P) (f : one_hom M N) (x : M) :
g.comp f x = g (f x) := rfl
@[to_additive] lemma mul_hom.comp_apply [has_mul M] [has_mul N] [has_mul P]
(g : mul_hom N P) (f : mul_hom M N) (x : M) :
g.comp f x = g (f x) := rfl
@[to_additive] lemma monoid_hom.comp_apply [monoid M] [monoid N] [monoid P]
(g : N →* P) (f : M →* N) (x : M) :
g.comp f x = g (f x) := rfl
lemma monoid_with_zero_hom.comp_apply [monoid_with_zero M] [monoid_with_zero N] [monoid_with_zero P]
(g : monoid_with_zero_hom N P) (f : monoid_with_zero_hom M N) (x : M) :
g.comp f x = g (f x) := rfl
/-- Composition of monoid homomorphisms is associative. -/
@[to_additive] lemma one_hom.comp_assoc {Q : Type*} [has_one M] [has_one N] [has_one P] [has_one Q]
(f : one_hom M N) (g : one_hom N P) (h : one_hom P Q) :
(h.comp g).comp f = h.comp (g.comp f) := rfl
@[to_additive] lemma mul_hom.comp_assoc {Q : Type*} [has_mul M] [has_mul N] [has_mul P] [has_mul Q]
(f : mul_hom M N) (g : mul_hom N P) (h : mul_hom P Q) :
(h.comp g).comp f = h.comp (g.comp f) := rfl
@[to_additive] lemma monoid_hom.comp_assoc {Q : Type*} [monoid M] [monoid N] [monoid P] [monoid Q]
(f : M →* N) (g : N →* P) (h : P →* Q) :
(h.comp g).comp f = h.comp (g.comp f) := rfl
lemma monoid_with_zero_hom.comp_assoc {Q : Type*}
[monoid_with_zero M] [monoid_with_zero N] [monoid_with_zero P] [monoid_with_zero Q]
(f : monoid_with_zero_hom M N) (g : monoid_with_zero_hom N P) (h : monoid_with_zero_hom P Q) :
(h.comp g).comp f = h.comp (g.comp f) := rfl
@[to_additive]
lemma one_hom.cancel_right [has_one M] [has_one N] [has_one P]
{g₁ g₂ : one_hom N P} {f : one_hom M N} (hf : function.surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
⟨λ h, one_hom.ext $ (forall_iff_forall_surj hf).1 (one_hom.ext_iff.1 h), λ h, h ▸ rfl⟩
@[to_additive]
lemma mul_hom.cancel_right [has_mul M] [has_mul N] [has_mul P]
{g₁ g₂ : mul_hom N P} {f : mul_hom M N} (hf : function.surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
⟨λ h, mul_hom.ext $ (forall_iff_forall_surj hf).1 (mul_hom.ext_iff.1 h), λ h, h ▸ rfl⟩
@[to_additive]
lemma monoid_hom.cancel_right [monoid M] [monoid N] [monoid P]
{g₁ g₂ : N →* P} {f : M →* N} (hf : function.surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
⟨λ h, monoid_hom.ext $ (forall_iff_forall_surj hf).1 (monoid_hom.ext_iff.1 h), λ h, h ▸ rfl⟩
lemma monoid_with_zero_hom.cancel_right
[monoid_with_zero M] [monoid_with_zero N] [monoid_with_zero P]
{g₁ g₂ : monoid_with_zero_hom N P} {f : monoid_with_zero_hom M N} (hf : function.surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
⟨λ h, monoid_with_zero_hom.ext $ (forall_iff_forall_surj hf).1 (monoid_with_zero_hom.ext_iff.1 h),
λ h, h ▸ rfl⟩
@[to_additive]
lemma one_hom.cancel_left [has_one M] [has_one N] [has_one P]
{g : one_hom N P} {f₁ f₂ : one_hom M N} (hg : function.injective g) :
g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ :=
⟨λ h, one_hom.ext $ λ x, hg $ by rw [← one_hom.comp_apply, h, one_hom.comp_apply],
λ h, h ▸ rfl⟩
@[to_additive]
lemma mul_hom.cancel_left [has_one M] [has_one N] [has_one P]
{g : one_hom N P} {f₁ f₂ : one_hom M N} (hg : function.injective g) :
g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ :=
⟨λ h, one_hom.ext $ λ x, hg $ by rw [← one_hom.comp_apply, h, one_hom.comp_apply],
λ h, h ▸ rfl⟩
@[to_additive]
lemma monoid_hom.cancel_left [monoid M] [monoid N] [monoid P]
{g : N →* P} {f₁ f₂ : M →* N} (hg : function.injective g) :
g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ :=
⟨λ h, monoid_hom.ext $ λ x, hg $ by rw [← monoid_hom.comp_apply, h, monoid_hom.comp_apply],
λ h, h ▸ rfl⟩
lemma monoid_with_zero_hom.cancel_left
[monoid_with_zero M] [monoid_with_zero N] [monoid_with_zero P]
{g : monoid_with_zero_hom N P} {f₁ f₂ : monoid_with_zero_hom M N} (hg : function.injective g) :
g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ :=
⟨λ h, monoid_with_zero_hom.ext $ λ x, hg $ by rw [
← monoid_with_zero_hom.comp_apply, h, monoid_with_zero_hom.comp_apply],
λ h, h ▸ rfl⟩
@[simp, to_additive] lemma one_hom.comp_id [has_one M] [has_one N]
(f : one_hom M N) : f.comp (one_hom.id M) = f := one_hom.ext $ λ x, rfl
@[simp, to_additive] lemma mul_hom.comp_id [has_mul M] [has_mul N]
(f : mul_hom M N) : f.comp (mul_hom.id M) = f := mul_hom.ext $ λ x, rfl
@[simp, to_additive] lemma monoid_hom.comp_id [monoid M] [monoid N]
(f : M →* N) : f.comp (monoid_hom.id M) = f := monoid_hom.ext $ λ x, rfl
@[simp] lemma monoid_with_zero_hom.comp_id [monoid_with_zero M] [monoid_with_zero N]
(f : monoid_with_zero_hom M N) : f.comp (monoid_with_zero_hom.id M) = f :=
monoid_with_zero_hom.ext $ λ x, rfl
@[simp, to_additive] lemma one_hom.id_comp [has_one M] [has_one N]
(f : one_hom M N) : (one_hom.id N).comp f = f := one_hom.ext $ λ x, rfl
@[simp, to_additive] lemma mul_hom.id_comp [has_mul M] [has_mul N]
(f : mul_hom M N) : (mul_hom.id N).comp f = f := mul_hom.ext $ λ x, rfl
@[simp, to_additive] lemma monoid_hom.id_comp [monoid M] [monoid N]
(f : M →* N) : (monoid_hom.id N).comp f = f := monoid_hom.ext $ λ x, rfl
@[simp] lemma monoid_with_zero_hom.id_comp [monoid_with_zero M] [monoid_with_zero N]
(f : monoid_with_zero_hom M N) : (monoid_with_zero_hom.id N).comp f = f :=
monoid_with_zero_hom.ext $ λ x, rfl
section End
namespace monoid
variables (M) [monoid M]
/-- The monoid of endomorphisms. -/
protected def End := M →* M
namespace End
instance : monoid (monoid.End M) :=
{ mul := monoid_hom.comp,
one := monoid_hom.id M,
mul_assoc := λ _ _ _, monoid_hom.comp_assoc _ _ _,
mul_one := monoid_hom.comp_id,
one_mul := monoid_hom.id_comp }
instance : inhabited (monoid.End M) := ⟨1⟩
instance : has_coe_to_fun (monoid.End M) := ⟨_, monoid_hom.to_fun⟩
end End
@[simp] lemma coe_one : ((1 : monoid.End M) : M → M) = id := rfl
@[simp] lemma coe_mul (f g) : ((f * g : monoid.End M) : M → M) = f ∘ g := rfl
end monoid
namespace add_monoid
variables (A : Type*) [add_monoid A]
/-- The monoid of endomorphisms. -/
protected def End := A →+ A
namespace End
instance : monoid (add_monoid.End A) :=
{ mul := add_monoid_hom.comp,
one := add_monoid_hom.id A,
mul_assoc := λ _ _ _, add_monoid_hom.comp_assoc _ _ _,
mul_one := add_monoid_hom.comp_id,
one_mul := add_monoid_hom.id_comp }
instance : inhabited (add_monoid.End A) := ⟨1⟩
instance : has_coe_to_fun (add_monoid.End A) := ⟨_, add_monoid_hom.to_fun⟩
end End
@[simp] lemma coe_one : ((1 : add_monoid.End A) : A → A) = id := rfl
@[simp] lemma coe_mul (f g) : ((f * g : add_monoid.End A) : A → A) = f ∘ g := rfl
end add_monoid
end End
/-- `1` is the homomorphism sending all elements to `1`. -/
@[to_additive]
instance [has_one M] [has_one N] : has_one (one_hom M N) := ⟨⟨λ _, 1, rfl⟩⟩
/-- `1` is the multiplicative homomorphism sending all elements to `1`. -/
@[to_additive]
instance [has_mul M] [monoid N] : has_one (mul_hom M N) := ⟨⟨λ _, 1, λ _ _, (one_mul 1).symm⟩⟩
/-- `1` is the monoid homomorphism sending all elements to `1`. -/
@[to_additive]
instance [monoid M] [monoid N] : has_one (M →* N) := ⟨⟨λ _, 1, rfl, λ _ _, (one_mul 1).symm⟩⟩
/-- `0` is the homomorphism sending all elements to `0`. -/
add_decl_doc zero_hom.has_zero
/-- `0` is the additive homomorphism sending all elements to `0`. -/
add_decl_doc add_hom.has_zero
/-- `0` is the additive monoid homomorphism sending all elements to `0`. -/
add_decl_doc add_monoid_hom.has_zero
@[simp, to_additive] lemma one_hom.one_apply [has_one M] [has_one N]
(x : M) : (1 : one_hom M N) x = 1 := rfl
@[simp, to_additive] lemma monoid_hom.one_apply [monoid M] [monoid N]
(x : M) : (1 : M →* N) x = 1 := rfl
@[simp, to_additive] lemma one_hom.one_comp [has_one M] [has_one N] [has_one P] (f : one_hom M N) :
(1 : one_hom N P).comp f = 1 := rfl
@[simp, to_additive] lemma one_hom.comp_one [has_one M] [has_one N] [has_one P] (f : one_hom N P) :
f.comp (1 : one_hom M N) = 1 :=
by { ext, simp only [one_hom.map_one, one_hom.coe_comp, function.comp_app, one_hom.one_apply] }
@[to_additive]
instance [has_one M] [has_one N] : inhabited (one_hom M N) := ⟨1⟩
@[to_additive]
instance [has_mul M] [monoid N] : inhabited (mul_hom M N) := ⟨1⟩
@[to_additive]
instance [monoid M] [monoid N] : inhabited (M →* N) := ⟨1⟩
-- unlike the other homs, `monoid_with_zero_hom` does not have a `1` or `0`
instance [monoid_with_zero M] : inhabited (monoid_with_zero_hom M M) := ⟨monoid_with_zero_hom.id M⟩
namespace monoid_hom
variables [mM : monoid M] [mN : monoid N] [mP : monoid P]
variables [group G] [comm_group H]
/-- Given two monoid morphisms `f`, `g` to a commutative monoid, `f * g` is the monoid morphism
sending `x` to `f x * g x`. -/
@[to_additive]
instance {M N} {mM : monoid M} [comm_monoid N] : has_mul (M →* N) :=
⟨λ f g,
{ to_fun := λ m, f m * g m,
map_one' := show f 1 * g 1 = 1, by simp,
map_mul' := begin intros, show f (x * y) * g (x * y) = f x * g x * (f y * g y),
rw [f.map_mul, g.map_mul, ←mul_assoc, ←mul_assoc, mul_right_comm (f x)], end }⟩
/-- Given two additive monoid morphisms `f`, `g` to an additive commutative monoid, `f + g` is the
additive monoid morphism sending `x` to `f x + g x`. -/
add_decl_doc add_monoid_hom.has_add
@[simp, to_additive] lemma mul_apply {M N} {mM : monoid M} {mN : comm_monoid N}
(f g : M →* N) (x : M) :
(f * g) x = f x * g x := rfl
@[simp, to_additive] lemma one_comp [monoid M] [monoid N] [monoid P] (f : M →* N) :
(1 : N →* P).comp f = 1 := rfl
@[simp, to_additive] lemma comp_one [monoid M] [monoid N] [monoid P] (f : N →* P) :
f.comp (1 : M →* N) = 1 :=
by { ext, simp only [map_one, coe_comp, function.comp_app, one_apply] }
@[to_additive] lemma mul_comp [monoid M] [comm_monoid N] [comm_monoid P]
(g₁ g₂ : N →* P) (f : M →* N) :
(g₁ * g₂).comp f = g₁.comp f * g₂.comp f := rfl
@[to_additive] lemma comp_mul [monoid M] [comm_monoid N] [comm_monoid P]
(g : N →* P) (f₁ f₂ : M →* N) :
g.comp (f₁ * f₂) = g.comp f₁ * g.comp f₂ :=
by { ext, simp only [mul_apply, function.comp_app, map_mul, coe_comp] }
/-- (M →* N) is a comm_monoid if N is commutative. -/
@[to_additive]
instance {M N} [monoid M] [comm_monoid N] : comm_monoid (M →* N) :=
{ mul := (*),
mul_assoc := by intros; ext; apply mul_assoc,
one := 1,
one_mul := by intros; ext; apply one_mul,
mul_one := by intros; ext; apply mul_one,
mul_comm := by intros; ext; apply mul_comm }
/-- `flip` arguments of `f : M →* N →* P` -/
@[to_additive "`flip` arguments of `f : M →+ N →+ P`"]
def flip {mM : monoid M} {mN : monoid N} {mP : comm_monoid P} (f : M →* N →* P) :
N →* M →* P :=
{ to_fun := λ y, ⟨λ x, f x y, by rw [f.map_one, one_apply], λ x₁ x₂, by rw [f.map_mul, mul_apply]⟩,
map_one' := ext $ λ x, (f x).map_one,
map_mul' := λ y₁ y₂, ext $ λ x, (f x).map_mul y₁ y₂ }
@[simp, to_additive] lemma flip_apply {mM : monoid M} {mN : monoid N} {mP : comm_monoid P}
(f : M →* N →* P) (x : M) (y : N) :
f.flip y x = f x y :=
rfl
/-- Evaluation of a `monoid_hom` at a point as a monoid homomorphism. See also `monoid_hom.apply`
for the evaluation of any function at a point. -/
@[to_additive "Evaluation of an `add_monoid_hom` at a point as an additive monoid homomorphism.
See also `add_monoid_hom.apply` for the evaluation of any function at a point."]
def eval [monoid M] [comm_monoid N] : M →* (M →* N) →* N := (monoid_hom.id (M →* N)).flip
@[simp, to_additive]
lemma eval_apply [monoid M] [comm_monoid N] (x : M) (f : M →* N) : eval x f = f x := rfl
/-- Composition of monoid morphisms (`monoid_hom.comp`) as a monoid morphism. -/
@[to_additive "Composition of additive monoid morphisms
(`add_monoid_hom.comp`) as an additive monoid morphism.", simps]
def comp_hom [monoid M] [comm_monoid N] [comm_monoid P] :
(N →* P) →* (M →* N) →* (M →* P) :=
{ to_fun := λ g, { to_fun := g.comp, map_one' := comp_one g, map_mul' := comp_mul g },
map_one' := by { ext1 f, exact one_comp f },
map_mul' := λ g₁ g₂, by { ext1 f, exact mul_comp g₁ g₂ f } }
/-- If two homomorphism from a group to a monoid are equal at `x`, then they are equal at `x⁻¹`. -/
@[to_additive "If two homomorphism from an additive group to an additive monoid are equal at `x`,
then they are equal at `-x`." ]
lemma eq_on_inv {G} [group G] [monoid M] {f g : G →* M} {x : G} (h : f x = g x) :
f x⁻¹ = g x⁻¹ :=
left_inv_eq_right_inv (f.map_mul_eq_one $ inv_mul_self x) $
h.symm ▸ g.map_mul_eq_one $ mul_inv_self x
/-- Group homomorphisms preserve inverse. -/
@[simp, to_additive]
theorem map_inv {G H} [group G] [group H] (f : G →* H) (g : G) : f g⁻¹ = (f g)⁻¹ :=
eq_inv_of_mul_eq_one $ f.map_mul_eq_one $ inv_mul_self g
/-- Group homomorphisms preserve division. -/
@[simp, to_additive]
theorem map_mul_inv {G H} [group G] [group H] (f : G →* H) (g h : G) :
f (g * h⁻¹) = (f g) * (f h)⁻¹ := by rw [f.map_mul, f.map_inv]
/-- A homomorphism from a group to a monoid is injective iff its kernel is trivial. -/
@[to_additive]
lemma injective_iff {G H} [group G] [monoid H] (f : G →* H) :
function.injective f ↔ (∀ a, f a = 1 → a = 1) :=
⟨λ h x hfx, h $ hfx.trans f.map_one.symm,
λ h x y hxy, mul_inv_eq_one.1 $ h _ $ by rw [f.map_mul, hxy, ← f.map_mul, mul_inv_self, f.map_one]⟩
include mM
/-- Makes a group homomorphism from a proof that the map preserves multiplication. -/
@[to_additive "Makes an additive group homomorphism from a proof that the map preserves addition."]
def mk' (f : M → G) (map_mul : ∀ a b : M, f (a * b) = f a * f b) : M →* G :=
{ to_fun := f,
map_mul' := map_mul,
map_one' := mul_self_iff_eq_one.1 $ by rw [←map_mul, mul_one] }
@[simp, to_additive]
lemma coe_mk' {f : M → G} (map_mul : ∀ a b : M, f (a * b) = f a * f b) :
⇑(mk' f map_mul) = f := rfl
omit mM
/-- Makes a group homomorphism from a proof that the map preserves right division `λ x y, x * y⁻¹`.
-/
@[to_additive "Makes an additive group homomorphism from a proof that the map preserves
the operation `λ a b, a + -b`. See also `add_monoid_hom.of_map_sub` for a version using
`λ a b, a - b`."]
def of_map_mul_inv {H : Type*} [group H] (f : G → H)
(map_div : ∀ a b : G, f (a * b⁻¹) = f a * (f b)⁻¹) :
G →* H :=
mk' f $ λ x y,
calc f (x * y) = f x * (f $ 1 * 1⁻¹ * y⁻¹)⁻¹ : by simp only [one_mul, one_inv, ← map_div, inv_inv]
... = f x * f y : by { simp only [map_div], simp only [mul_right_inv, one_mul, inv_inv] }
@[simp, to_additive] lemma coe_of_map_mul_inv {H : Type*} [group H] (f : G → H)
(map_div : ∀ a b : G, f (a * b⁻¹) = f a * (f b)⁻¹) :
⇑(of_map_mul_inv f map_div) = f :=
rfl
/-- If `f` is a monoid homomorphism to a commutative group, then `f⁻¹` is the homomorphism sending
`x` to `(f x)⁻¹`. -/
@[to_additive]
instance {M G} [monoid M] [comm_group G] : has_inv (M →* G) :=
⟨λ f, mk' (λ g, (f g)⁻¹) $ λ a b, by rw [←mul_inv, f.map_mul]⟩
/-- If `f` is an additive monoid homomorphism to an additive commutative group, then `-f` is the
homomorphism sending `x` to `-(f x)`. -/
add_decl_doc add_monoid_hom.has_neg
@[simp, to_additive] lemma inv_apply {M G} {mM : monoid M} {gG : comm_group G}
(f : M →* G) (x : M) :
f⁻¹ x = (f x)⁻¹ := rfl
/-- If `f` and `g` are monoid homomorphisms to a commutative group, then `f / g` is the homomorphism
sending `x` to `(f x) / (g x). -/
@[to_additive]
instance {M G} [monoid M] [comm_group G] : has_div (M →* G) :=
⟨λ f g, mk' (λ x, f x / g x) $ λ a b,
by simp [div_eq_mul_inv, mul_assoc, mul_left_comm, mul_comm]⟩
/-- If `f` and `g` are monoid homomorphisms to an additive commutative group, then `f - g`
is the homomorphism sending `x` to `(f x) - (g x). -/
add_decl_doc add_monoid_hom.has_sub
@[simp, to_additive] lemma div_apply {M G} {mM : monoid M} {gG : comm_group G}
(f g : M →* G) (x : M) :
(f / g) x = f x / g x := rfl
/-- If `G` is a commutative group, then `M →* G` a commutative group too. -/
@[to_additive]
instance {M G} [monoid M] [comm_group G] : comm_group (M →* G) :=
{ inv := has_inv.inv,
div := has_div.div,
div_eq_mul_inv := by { intros, ext, apply div_eq_mul_inv },
mul_left_inv := by intros; ext; apply mul_left_inv,
..monoid_hom.comm_monoid }
/-- If `G` is an additive commutative group, then `M →+ G` an additive commutative group too. -/
add_decl_doc add_monoid_hom.add_comm_group
end monoid_hom
namespace add_monoid_hom
variables {A B : Type*} [add_monoid A] [add_comm_group B] [add_group G] [add_group H]
/-- Additive group homomorphisms preserve subtraction. -/
@[simp] theorem map_sub (f : G →+ H) (g h : G) : f (g - h) = (f g) - (f h) :=
by rw [sub_eq_add_neg, sub_eq_add_neg, f.map_add_neg g h]
/-- Define a morphism of additive groups given a map which respects difference. -/
def of_map_sub (f : G → H) (hf : ∀ x y, f (x - y) = f x - f y) : G →+ H :=
of_map_add_neg f (by simpa only [sub_eq_add_neg] using hf)
@[simp] lemma coe_of_map_sub (f : G → H) (hf : ∀ x y, f (x - y) = f x - f y) :
⇑(of_map_sub f hf) = f :=
rfl
end add_monoid_hom
section commute
variables [monoid M] [monoid N] {a x y : M}
@[simp, to_additive]
protected lemma semiconj_by.map (h : semiconj_by a x y) (f : M →* N) :
semiconj_by (f a) (f x) (f y) :=
by simpa only [semiconj_by, f.map_mul] using congr_arg f h
@[simp, to_additive]
protected lemma commute.map (h : commute x y) (f : M →* N) : commute (f x) (f y) := h.map f
end commute
|
240fffbee062ed9b2ac14388074bd6c4d1455736 | 367134ba5a65885e863bdc4507601606690974c1 | /src/data/dfinsupp.lean | 6ecd36358d2f2ec370a03d241fd724486b608dac | [
"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 | 45,140 | 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.big_operators.basic
import data.set.finite
import group_theory.submonoid.basic
/-!
# 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 v)
namespace dfinsupp
variable [Π i, has_zero (β i)]
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)]
variables {β₁ : ι → Type v₁} {β₂ : ι → Type v₂}
variables [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)]
instance : has_coe_to_fun (Π₀ i, β i) :=
⟨λ _, Π i, β i, λ f, quotient.lift_on f pre.to_fun $ λ _ _, funext⟩
instance : has_zero (Π₀ i, β i) := ⟨⟦⟨λ i, 0, ∅, λ i, or.inr rfl⟩⟧⟩
instance : inhabited (Π₀ i, β i) := ⟨0⟩
@[simp] lemma zero_apply (i : ι) : (0 : Π₀ i, β i) i = 0 := rfl
lemma coe_fn_injective : @function.injective (Π₀ i, β i) (Π i, β i) coe_fn :=
λ f g H, quotient.induction_on₂ f g (λ _ _ H, quotient.sound H) (congr_fun H)
@[ext] lemma ext {f g : Π₀ i, β i} (H : ∀ i, f i = g i) : f = g :=
coe_fn_injective (funext H)
/-- The composition of `f : β₁ → β₂` and `g : Π₀ i, β₁ i` is
`map_range f hf g : Π₀ i, β₂ i`, well defined when `f 0 = 0`. -/
def map_range (f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0) (g : Π₀ i, β₁ i) : Π₀ i, β₂ i :=
quotient.lift_on g (λ x, ⟦(⟨λ i, f i (x.1 i), x.2,
λ i, or.cases_on (x.3 i) or.inl $ λ H, or.inr $ by rw [H, hf]⟩ : pre ι β₂)⟧) $ λ x y H,
quotient.sound $ λ 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
/-- 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)
(g₁ : Π₀ i, β₁ i) (g₂ : Π₀ i, β₂ i) : (Π₀ i, β i) :=
begin
refine quotient.lift_on₂ g₁ g₂ (λ x y, ⟦(⟨λ i, f i (x.1 i) (y.1 i), x.2 + y.2,
λ i, _⟩ : pre ι β)⟧) _,
{ 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₂ y₁ y₂ H1 H2, quotient.sound $ λ 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_monoid (β i)] : has_add (Π₀ i, β i) :=
⟨zip_with (λ _, (+)) (λ _, add_zero 0)⟩
lemma add_apply [Π i, add_monoid (β 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_monoid (β i)] (g₁ g₂ : Π₀ i, β i) :
⇑(g₁ + g₂) = g₁ + g₂ :=
funext $ add_apply g₁ g₂
instance [Π i, add_monoid (β i)] : add_monoid (Π₀ i, β i) :=
{ add_monoid .
zero := 0,
add := (+),
add_assoc := λ f g h, ext $ λ i, by simp only [add_apply, add_assoc],
zero_add := λ f, ext $ λ i, by simp only [add_apply, zero_apply, zero_add],
add_zero := λ f, ext $ λ i, by simp only [add_apply, zero_apply, add_zero] }
instance is_add_monoid_hom [Π i, add_monoid (β i)] {i : ι} :
is_add_monoid_hom (λ g : Π₀ i : ι, β i, g i) :=
{ map_add := λ f g, add_apply f g i, map_zero := zero_apply i }
instance [Π i, add_group (β i)] : has_neg (Π₀ i, β i) :=
⟨λ f, f.map_range (λ _, has_neg.neg) (λ _, neg_zero)⟩
instance [Π i, add_comm_monoid (β i)] : add_comm_monoid (Π₀ i, β i) :=
{ add_comm := λ f g, ext $ λ i, by simp only [add_apply, add_comm],
.. dfinsupp.add_monoid }
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)] : add_group (Π₀ i, β i) :=
{ add_left_neg := λ f, ext $ λ i, by simp only [add_apply, neg_apply, zero_apply, add_left_neg],
.. dfinsupp.add_monoid,
.. (infer_instance : has_neg (Π₀ i, β i)) }
lemma sub_apply [Π i, add_group (β i)] (g₁ g₂ : Π₀ i, β i) (i : ι) :
(g₁ - g₂) i = g₁ i - g₂ i :=
by rw [sub_eq_add_neg]; simp [sub_eq_add_neg]
@[simp] lemma coe_sub [Π i, add_group (β i)] (g₁ g₂ : Π₀ i, β i) :
⇑(g₁ - g₂) = g₁ - g₂ :=
funext $ sub_apply g₁ g₂
instance [Π i, add_comm_group (β i)] : add_comm_group (Π₀ i, β i) :=
{ add_comm := λ f g, ext $ λ i, by simp only [add_apply, add_comm],
..dfinsupp.add_group }
/-- Dependent functions with finite support inherit a semiring action from an action on each
coordinate. -/
instance {γ : Type w} [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] :
has_scalar γ (Π₀ i, β i) :=
⟨λc v, v.map_range (λ _, (•) c) (λ _, smul_zero _)⟩
lemma smul_apply {γ : Type w} [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 {γ : Type w} [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 w} {δ : 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 w} {δ : Type*} [monoid γ] [monoid δ]
[Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] [Π i, distrib_mul_action δ (β i)]
[has_scalar γ δ] [Π 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)] }
/-- Dependent functions with finite support inherit a `distrib_mul_action` structure from such a
structure on each coordinate. -/
instance {γ : Type w} [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] :
distrib_mul_action γ (Π₀ i, β i) :=
{ smul_zero := λ c, ext $ λ i, by simp only [smul_apply, smul_zero, zero_apply],
smul_add := λ c x y, ext $ λ i, by simp only [add_apply, smul_apply, smul_add],
one_smul := λ x, ext $ λ i, by simp only [smul_apply, one_smul],
mul_smul := λ r s x, ext $ λ i, by simp only [smul_apply, smul_smul],
..dfinsupp.has_scalar }
/-- Dependent functions with finite support inherit a semimodule structure from such a structure on
each coordinate. -/
instance {γ : Type w} [semiring γ] [Π i, add_comm_monoid (β i)] [Π i, semimodule γ (β i)] :
semimodule γ (Π₀ 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] (f : Π₀ i, β i) : Π₀ i, β i :=
quotient.lift_on f (λ x, ⟦(⟨λ i, if p i then x.1 i else 0, x.2,
λ i, or.cases_on (x.3 i) or.inl $ λ H, or.inr $ by rw [H, if_t_t]⟩ : pre ι β)⟧) $ λ x y H,
quotient.sound $ λ 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_monoid (β 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]
/-- `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]
(f : Π₀ i, β i) : Π₀ i : subtype p, β i :=
begin
fapply quotient.lift_on f,
{ intro x,
refine ⟦⟨λ i, x.1 (i : ι),
(x.2.filter p).attach.map $ λ j, ⟨j, (multiset.mem_filter.1 j.2).2⟩, _⟩⟧,
refine λ i, or.cases_on (x.3 i) (λ H, _) or.inr,
left, rw multiset.mem_map, refine ⟨⟨i, multiset.mem_filter.2 ⟨H, i.2⟩⟩, _, subtype.eta _ _⟩,
apply multiset.mem_attach },
intros x y H,
exact quotient.sound (λ i, H i)
end
@[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_monoid (β 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]
instance subtype_domain.is_add_monoid_hom [Π i, add_monoid (β i)]
{p : ι → Prop} [decidable_pred p] :
is_add_monoid_hom (subtype_domain p : (Π₀ i : ι, β i) → Π₀ i : subtype p, β i) :=
{ map_add := λ _ _, subtype_domain_add, map_zero := subtype_domain_zero }
@[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_supp (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⟩⟧
@[simp] lemma mk_apply {s : finset ι} {x : Π i : (↑s : set ι), β i} {i : ι} :
(mk s x : Π i, β i) i = if H : i ∈ s then x ⟨i, H⟩ else 0 :=
rfl
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
/-- 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
@[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 (⟨hi, hxi⟩ | ⟨hi, hj⟩),
{ subst hi,
rw eq_of_heq hxi, },
{ rw [hi, hj, dfinsupp.single_zero, dfinsupp.single_zero], }, },
end
/-- 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 }
/-- Redefine `f i` to be `0`. -/
def erase (i : ι) (f : Π₀ i, β i) : Π₀ i, β i :=
quotient.lift_on f (λ x, ⟦(⟨λ j, if j = i then 0 else x.1 j, x.2,
λ j, or.cases_on (x.3 j) or.inl $ λ H, or.inr $ by simp only [H, if_t_t]⟩ : pre ι β)⟧) $ λ x y H,
quotient.sound $ λ 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]
end basic
section add_monoid
variable [Π i, add_monoid (β 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
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, map_add' := λ _ _, single_add }
variables {β}
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,
by_cases H1 : i ∈ s,
{ have H2 : ∀ j, j ∈ s ∨ f j = 0,
{ intro j, cases H j with H2 H2,
{ cases multiset.mem_cons.1 H2 with H3 H3,
{ left, rw H3, exact H1 },
{ left, exact H3 } },
right, exact H2 },
have H3 : (⟦{to_fun := f, pre_support := i ::ₘ s, zero := H}⟧ : Π₀ i, β i)
= ⟦{to_fun := f, pre_support := s, zero := H2}⟧,
{ exact quotient.sound (λ i, rfl) },
rw H3, apply ih },
have H2 : p (erase i ⟦{to_fun := f, pre_support := i ::ₘ s, zero := H}⟧),
{ dsimp only [erase, quotient.lift_on_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_monoid γ] ⦃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_monoid γ] ⦃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_monoid (β 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]
instance [Π i, add_group (β i)] {s : finset ι} : is_add_group_hom (@mk ι β _ _ s) :=
{ map_add := λ _ _, mk_add }
section
variables (γ : Type w) [semiring γ] [Π i, add_comm_monoid (β i)] [Π i, semimodule γ (β i)]
include γ
@[simp] lemma mk_smul {s : finset ι} {c : γ} (x : Π i : (↑s : set ι), β i.1) :
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
@[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 {β₁ : ι → Type v₁} {β₂ : ι → Type v₂}
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 {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; by_cases h2 : f j ≠ 0; simp at h2; simp [h1, h2] }
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 h1 : p i; by_cases h2 : f i ≠ 0;
try {simp at h2}; dsimp; simp [h1, h2, ← subtype.val_eq_coe]
@[simp] lemma support_subtype_domain {f : Π₀ i, β i} :
(subtype_domain p f).support = f.support.subtype p :=
by ext i; by_cases h1 : p i; by_cases h2 : f i ≠ 0;
try {simp at h2}; dsimp; simp [h1, h2]
end filter_and_subtype_domain
end support_basic
lemma support_add [Π i, add_monoid (β 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, semimodule γ (β 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 [f.mem_support_iff, not_not] at h,
have hg : g i = 0, by rwa [h₁, g.mem_support_iff, not_not] at h,
by rw [hf, hg],
by intro h; subst h; simp⟩
section prod_and_sum
variables {γ : Type w}
-- [to_additive sum] for dfinsupp.prod doesn't work, the equation lemmas are not generated
/-- `sum f g` is the sum of `g i (f i)` over the support of `f`. -/
def sum [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [add_comm_monoid γ]
(f : Π₀ i, β i) (g : Π i, β i → γ) : γ :=
∑ i in f.support, g i (f i)
/-- `prod f g` is the product of `g i (f i)` over the support of `f`. -/
@[to_additive]
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₂) :=
(f.support.sum_hom (λf : Π₀ i, β i, f i₂)).symm
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, (f.mem_support_iff i).mp 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)⁻¹ :=
f.support.prod_hom (@has_inv.inv γ _)
@[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]
/--
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_monoid (β 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_monoid (β 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_monoid (β 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_monoid (β 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
omit dec
lemma sum_add_hom_comm {ι₁ ι₂ : Sort*} {β₁ : ι₁ → Type*} {β₂ : ι₂ → Type*} {γ : Type*}
[decidable_eq ι₁] [decidable_eq ι₂] [Π i, add_monoid (β₁ i)] [Π i, add_monoid (β₂ 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_monoid (β 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_monoid (β 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_monoid (β 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_monoid (β 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_monoid (β 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_monoid (β 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_monoid (β 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 :=
eq.symm (s.sum_hom _)
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
end dfinsupp
/-! ### Product and sum lemmas for bundled morphisms -/
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_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 add_monoid_hom
variables {R S : Type*}
open dfinsupp
/-! The above lemmas, repeated for `dfinsupp.sum_add_hom`. -/
@[simp]
lemma map_dfinsupp_sum_add_hom [add_comm_monoid R] [add_comm_monoid S] [Π i, add_comm_monoid (β 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_monoid R] [add_comm_monoid S] [Π i, add_comm_monoid (β 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_monoid R] [add_comm_monoid S] [Π i, add_comm_monoid (β 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
end
|
e1c0fd3aef76591c0e9b5b0c9dc61cbe63f4ca00 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/topology/local_homeomorph.lean | 7dd5435056cf3856aec999afd46e88896521b4d4 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 27,714 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import data.equiv.local_equiv topology.continuous_on topology.homeomorph
/-!
# Local homeomorphisms
This file defines homeomorphisms between open subsets of topological spaces. An element `e` of
`local_homeomorph α β` is an extension of `local_equiv α β`, i.e., it is a pair of functions
`e.to_fun` and `e.inv_fun`, inverse of each other on the sets `e.source` and `e.target`.
Additionally, we require that these sets are open, and that the functions are continuous on them.
Equivalently, they are homeomorphisms there.
Contrary to equivs, we do not register the coercion to functions and we use explicitly to_fun and
inv_fun: coercions create unification problems for manifolds.
## Main definitions
`homeomorph.to_local_homeomorph`: associating a local homeomorphism to a homeomorphism, with
source = target = univ
`local_homeomorph.symm` : the inverse of a local homeomorphism
`local_homeomorph.trans` : the composition of two local homeomorphisms
`local_homeomorph.refl` : the identity local homeomorphism
`local_homeomorph.of_set`: the identity on a set `s`
`eq_on_source` : equivalence relation describing the "right" notion of equality for local
homeomorphisms
## Implementation notes
Most statements are copied from their local_equiv versions, although some care is required
especially when restricting to subsets, as these should be open subsets.
For design notes, see `local_equiv.lean`.
-/
open function set
open_locale topological_space
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
[topological_space α] [topological_space β] [topological_space γ] [topological_space δ]
/-- local homeomorphisms, defined on open subsets of the space -/
structure local_homeomorph (α : Type*) (β : Type*) [topological_space α] [topological_space β]
extends local_equiv α β :=
(open_source : is_open source)
(open_target : is_open target)
(continuous_to_fun : continuous_on to_fun source)
(continuous_inv_fun : continuous_on inv_fun target)
/-- A homeomorphism induces a local homeomorphism on the whole space -/
def homeomorph.to_local_homeomorph (e : homeomorph α β) :
local_homeomorph α β :=
{ open_source := is_open_univ,
open_target := is_open_univ,
continuous_to_fun := by { erw ← continuous_iff_continuous_on_univ, exact e.continuous_to_fun },
continuous_inv_fun := by { erw ← continuous_iff_continuous_on_univ, exact e.continuous_inv_fun },
..e.to_equiv.to_local_equiv }
namespace local_homeomorph
variables (e : local_homeomorph α β) (e' : local_homeomorph β γ)
lemma eq_of_local_equiv_eq {e e' : local_homeomorph α β}
(h : e.to_local_equiv = e'.to_local_equiv) : e = e' :=
begin
cases e, cases e',
dsimp at *,
induction h,
refl
end
/-- Two local homeomorphisms are equal when they have equal to_fun, inv_fun and source. It is not
sufficient to have equal to_fun and source, as this only determines inv_fun on the target. This
would only be true for a weaker notion of equality, arguably the right one, called `eq_on_source`. -/
@[ext]
protected lemma ext (e' : local_homeomorph α β) (h : ∀x, e.to_fun x = e'.to_fun x)
(hinv: ∀x, e.inv_fun x = e'.inv_fun x) (hs : e.source = e'.source) : e = e' :=
eq_of_local_equiv_eq (local_equiv.ext e.to_local_equiv e'.to_local_equiv h hinv hs)
/-- The inverse of a local homeomorphism -/
protected def symm : local_homeomorph β α :=
{ open_source := e.open_target,
open_target := e.open_source,
continuous_to_fun := e.continuous_inv_fun,
continuous_inv_fun := e.continuous_to_fun,
..e.to_local_equiv.symm }
@[simp] lemma symm_to_local_equiv : e.symm.to_local_equiv = e.to_local_equiv.symm := rfl
@[simp] lemma symm_to_fun : e.symm.to_fun = e.inv_fun := rfl
@[simp] lemma symm_inv_fun : e.symm.inv_fun = e.to_fun := rfl
@[simp] lemma symm_source : e.symm.source = e.target := rfl
@[simp] lemma symm_target : e.symm.target = e.source := rfl
@[simp] lemma symm_symm : e.symm.symm = e := eq_of_local_equiv_eq $ by simp
/-- A local homeomorphism is continuous at any point of its source -/
lemma continuous_at_to_fun {x : α} (h : x ∈ e.source) : continuous_at e.to_fun x :=
(e.continuous_to_fun x h).continuous_at (mem_nhds_sets e.open_source h)
/-- A local homeomorphism inverse is continuous at any point of its target -/
lemma continuous_at_inv_fun {x : β} (h : x ∈ e.target) : continuous_at e.inv_fun x :=
e.symm.continuous_at_to_fun h
/-- Preimage of interior or interior of preimage coincide for local homeomorphisms, when restricted
to the source. -/
lemma preimage_interior (s : set β) :
e.source ∩ e.to_fun ⁻¹' (interior s) = e.source ∩ interior (e.to_fun ⁻¹' s) :=
begin
apply subset.antisymm,
{ exact e.continuous_to_fun.preimage_interior_subset_interior_preimage e.open_source },
{ calc e.source ∩ interior (e.to_fun ⁻¹' s)
= (e.source ∩ e.to_fun ⁻¹' e.target) ∩ interior (e.to_fun ⁻¹' s) : begin
congr,
apply (inter_eq_self_of_subset_left _).symm,
apply e.to_local_equiv.source_subset_preimage_target,
end
... = (e.source ∩ interior (e.to_fun ⁻¹' s)) ∩ (e.to_fun ⁻¹' e.target) :
by simp [inter_comm, inter_assoc]
... = (e.source ∩ e.to_fun ⁻¹' (e.inv_fun ⁻¹' (interior (e.to_fun ⁻¹' s)))) ∩ (e.to_fun ⁻¹' e.target) :
by rw e.to_local_equiv.source_inter_preimage_inv_preimage
... = e.source ∩ e.to_fun ⁻¹' (e.target ∩ e.inv_fun ⁻¹' (interior (e.to_fun ⁻¹' s))) :
by rw [inter_comm e.target, preimage_inter, inter_assoc]
... ⊆ e.source ∩ e.to_fun ⁻¹' (e.target ∩ interior (e.inv_fun ⁻¹' (e.to_fun ⁻¹' s))) : begin
apply inter_subset_inter (subset.refl _) (preimage_mono _),
exact e.continuous_inv_fun.preimage_interior_subset_interior_preimage e.open_target
end
... = e.source ∩ e.to_fun ⁻¹' (interior (e.target ∩ e.inv_fun ⁻¹' (e.to_fun ⁻¹' s))) :
by rw [interior_inter, interior_eq_of_open e.open_target]
... = e.source ∩ e.to_fun ⁻¹' (interior (e.target ∩ s)) :
by rw e.to_local_equiv.target_inter_inv_preimage_preimage
... = e.source ∩ e.to_fun ⁻¹' e.target ∩ e.to_fun ⁻¹' (interior s) :
by rw [interior_inter, preimage_inter, interior_eq_of_open e.open_target, inter_assoc]
... = e.source ∩ e.to_fun ⁻¹' (interior s) : begin
congr,
apply inter_eq_self_of_subset_left,
apply e.to_local_equiv.source_subset_preimage_target
end }
end
/-- The image of an open set in the source is open. -/
lemma image_open_of_open {s : set α} (hs : is_open s) (h : s ⊆ e.source) : is_open (e.to_fun '' s) :=
begin
have : e.to_fun '' s = e.target ∩ e.inv_fun ⁻¹' s :=
e.to_local_equiv.image_eq_target_inter_inv_preimage h,
rw this,
exact e.continuous_inv_fun.preimage_open_of_open e.open_target hs
end
/-- Restricting a local homeomorphism `e` to `e.source ∩ s` when `s` is open. This is sometimes hard
to use because of the openness assumption, but it has the advantage that when it can
be used then its local_equiv is defeq to local_equiv.restr -/
protected def restr_open (s : set α) (hs : is_open s) :
local_homeomorph α β :=
{ open_source := is_open_inter e.open_source hs,
open_target := (continuous_on_open_iff e.open_target).1 e.continuous_inv_fun s hs,
continuous_to_fun := e.continuous_to_fun.mono (inter_subset_left _ _),
continuous_inv_fun := e.continuous_inv_fun.mono (inter_subset_left _ _),
..e.to_local_equiv.restr s}
@[simp] lemma restr_open_source (s : set α) (hs : is_open s) :
(e.restr_open s hs).source = e.source ∩ s := rfl
@[simp] lemma restr_open_to_local_equiv (s : set α) (hs : is_open s) :
(e.restr_open s hs).to_local_equiv = e.to_local_equiv.restr s := rfl
/-- Restricting a local homeomorphism `e` to `e.source ∩ interior s`. We use the interior to make
sure that the restriction is well defined whatever the set s, since local homeomorphisms are by
definition defined on open sets. In applications where `s` is open, this coincides with the
restriction of local equivalences -/
protected def restr (s : set α) : local_homeomorph α β :=
e.restr_open (interior s) is_open_interior
@[simp] lemma restr_to_fun (s : set α) : (e.restr s).to_fun = e.to_fun := rfl
@[simp] lemma restr_inv_fun (s : set α) : (e.restr s).inv_fun = e.inv_fun := rfl
@[simp] lemma restr_source (s : set α) : (e.restr s).source = e.source ∩ interior s := rfl
@[simp] lemma restr_target (s : set α) :
(e.restr s).target = e.target ∩ e.inv_fun ⁻¹' (interior s) := rfl
@[simp] lemma restr_to_local_equiv (s : set α) :
(e.restr s).to_local_equiv = (e.to_local_equiv).restr (interior s) := rfl
lemma restr_source' (s : set α) (hs : is_open s) : (e.restr s).source = e.source ∩ s :=
by rw [e.restr_source, interior_eq_of_open hs]
lemma restr_to_local_equiv' (s : set α) (hs : is_open s):
(e.restr s).to_local_equiv = e.to_local_equiv.restr s :=
by rw [e.restr_to_local_equiv, interior_eq_of_open hs]
lemma restr_eq_of_source_subset {e : local_homeomorph α β} {s : set α} (h : e.source ⊆ s) :
e.restr s = e :=
begin
apply eq_of_local_equiv_eq,
rw restr_to_local_equiv,
apply local_equiv.restr_eq_of_source_subset,
have := interior_mono h,
rwa interior_eq_of_open (e.open_source) at this
end
@[simp] lemma restr_univ {e : local_homeomorph α β} : e.restr univ = e :=
restr_eq_of_source_subset (subset_univ _)
lemma restr_source_inter (s : set α) : e.restr (e.source ∩ s) = e.restr s :=
begin
refine local_homeomorph.ext _ _ (λx, rfl) (λx, rfl) _,
simp [interior_eq_of_open e.open_source],
rw [← inter_assoc, inter_self]
end
/-- The identity on the whole space as a local homeomorphism. -/
protected def refl (α : Type*) [topological_space α] : local_homeomorph α α :=
(homeomorph.refl α).to_local_homeomorph
@[simp] lemma refl_source : (local_homeomorph.refl α).source = univ := rfl
@[simp] lemma refl_target : (local_homeomorph.refl α).target = univ := rfl
@[simp] lemma refl_symm : (local_homeomorph.refl α).symm = local_homeomorph.refl α := rfl
@[simp] lemma refl_to_fun : (local_homeomorph.refl α).to_fun = id := rfl
@[simp] lemma refl_inv_fun : (local_homeomorph.refl α).inv_fun = id := rfl
@[simp] lemma refl_local_equiv : (local_homeomorph.refl α).to_local_equiv = local_equiv.refl α := rfl
section
variables {s : set α} (hs : is_open s)
/-- The identity local equiv on a set `s` -/
def of_set (s : set α) (hs : is_open s) : local_homeomorph α α :=
{ open_source := hs,
open_target := hs,
continuous_to_fun := continuous_id.continuous_on,
continuous_inv_fun := continuous_id.continuous_on,
..local_equiv.of_set s }
@[simp] lemma of_set_source : (of_set s hs).source = s := rfl
@[simp] lemma of_set_target : (of_set s hs).target = s := rfl
@[simp] lemma of_set_to_fun : (of_set s hs).to_fun = id := rfl
@[simp] lemma of_set_inv_fun : (of_set s hs).inv_fun = id := rfl
@[simp] lemma of_set_symm : (of_set s hs).symm = of_set s hs := rfl
@[simp] lemma of_set_to_local_equiv : (of_set s hs).to_local_equiv = local_equiv.of_set s := rfl
end
/-- Composition of two local homeomorphisms when the target of the first and the source of
the second coincide. -/
protected def trans' (h : e.target = e'.source) : local_homeomorph α γ :=
{ open_source := e.open_source,
open_target := e'.open_target,
continuous_to_fun := begin
apply continuous_on.comp e'.continuous_to_fun e.continuous_to_fun,
rw ← h,
exact e.to_local_equiv.source_subset_preimage_target
end,
continuous_inv_fun := begin
apply continuous_on.comp e.continuous_inv_fun e'.continuous_inv_fun,
rw h,
exact e'.to_local_equiv.target_subset_preimage_source
end,
..local_equiv.trans' e.to_local_equiv e'.to_local_equiv h }
/-- Composing two local homeomorphisms, by restricting to the maximal domain where their
composition is well defined. -/
protected def trans : local_homeomorph α γ :=
local_homeomorph.trans' (e.symm.restr_open e'.source e'.open_source).symm
(e'.restr_open e.target e.open_target) (by simp [inter_comm])
@[simp] lemma trans_to_local_equiv :
(e.trans e').to_local_equiv = e.to_local_equiv.trans e'.to_local_equiv := rfl
@[simp] lemma trans_to_fun : (e.trans e').to_fun = e'.to_fun ∘ e.to_fun := rfl
@[simp] lemma trans_inv_fun : (e.trans e').inv_fun = e.inv_fun ∘ e'.inv_fun := rfl
lemma trans_symm_eq_symm_trans_symm : (e.trans e').symm = e'.symm.trans e.symm :=
by cases e; cases e'; refl
/- This could be considered as a simp lemma, but there are many situations where it makes something
simple into something more complicated. -/
lemma trans_source : (e.trans e').source = e.source ∩ e.to_fun ⁻¹' e'.source :=
local_equiv.trans_source e.to_local_equiv e'.to_local_equiv
lemma trans_source' : (e.trans e').source = e.source ∩ e.to_fun ⁻¹' (e.target ∩ e'.source) :=
local_equiv.trans_source' e.to_local_equiv e'.to_local_equiv
lemma trans_source'' : (e.trans e').source = e.inv_fun '' (e.target ∩ e'.source) :=
local_equiv.trans_source'' e.to_local_equiv e'.to_local_equiv
lemma image_trans_source : e.to_fun '' (e.trans e').source = e.target ∩ e'.source :=
local_equiv.image_trans_source e.to_local_equiv e'.to_local_equiv
lemma trans_target : (e.trans e').target = e'.target ∩ e'.inv_fun ⁻¹' e.target := rfl
lemma trans_target' : (e.trans e').target = e'.target ∩ e'.inv_fun ⁻¹' (e'.source ∩ e.target) :=
trans_source' e'.symm e.symm
lemma trans_target'' : (e.trans e').target = e'.to_fun '' (e'.source ∩ e.target) :=
trans_source'' e'.symm e.symm
lemma inv_image_trans_target : e'.inv_fun '' (e.trans e').target = e'.source ∩ e.target :=
image_trans_source e'.symm e.symm
lemma trans_assoc (e'' : local_homeomorph γ δ) :
(e.trans e').trans e'' = e.trans (e'.trans e'') :=
eq_of_local_equiv_eq $ local_equiv.trans_assoc e.to_local_equiv e'.to_local_equiv e''.to_local_equiv
@[simp] lemma trans_refl : e.trans (local_homeomorph.refl β) = e :=
eq_of_local_equiv_eq $ local_equiv.trans_refl e.to_local_equiv
@[simp] lemma refl_trans : (local_homeomorph.refl α).trans e = e :=
eq_of_local_equiv_eq $ local_equiv.refl_trans e.to_local_equiv
lemma trans_of_set {s : set β} (hs : is_open s) :
e.trans (of_set s hs) = e.restr (e.to_fun ⁻¹' s) :=
local_homeomorph.ext _ _ (λx, rfl) (λx, rfl) $
by simp [local_equiv.trans_source, (e.preimage_interior _).symm, interior_eq_of_open hs]
lemma trans_of_set' {s : set β} (hs : is_open s) :
e.trans (of_set s hs) = e.restr (e.source ∩ e.to_fun ⁻¹' s) :=
by rw [trans_of_set, restr_source_inter]
lemma of_set_trans {s : set α} (hs : is_open s) :
(of_set s hs).trans e = e.restr s :=
local_homeomorph.ext _ _ (λx, rfl) (λx, rfl) $
by simp [local_equiv.trans_source, interior_eq_of_open hs, inter_comm]
lemma of_set_trans' {s : set α} (hs : is_open s) :
(of_set s hs).trans e = e.restr (e.source ∩ s) :=
by rw [of_set_trans, restr_source_inter]
lemma restr_trans (s : set α) :
(e.restr s).trans e' = (e.trans e').restr s :=
eq_of_local_equiv_eq $ local_equiv.restr_trans e.to_local_equiv e'.to_local_equiv (interior s)
/-- `eq_on_source e e'` means that `e` and `e'` have the same source, and coincide there. They
should really be considered the same local equiv. -/
def eq_on_source (e e' : local_homeomorph α β) : Prop :=
e.source = e'.source ∧ (∀x ∈ e.source, e.to_fun x = e'.to_fun x)
lemma eq_on_source_iff (e e' : local_homeomorph α β) :
eq_on_source e e' ↔ local_equiv.eq_on_source e.to_local_equiv e'.to_local_equiv :=
by refl
/-- `eq_on_source` is an equivalence relation -/
instance : setoid (local_homeomorph α β) :=
{ r := eq_on_source,
iseqv := ⟨
λe, (@local_equiv.eq_on_source_setoid α β).iseqv.1 e.to_local_equiv,
λe e' h, (@local_equiv.eq_on_source_setoid α β).iseqv.2.1 ((eq_on_source_iff e e').1 h),
λe e' e'' h h', (@local_equiv.eq_on_source_setoid α β).iseqv.2.2
((eq_on_source_iff e e').1 h) ((eq_on_source_iff e' e'').1 h')⟩ }
lemma eq_on_source_refl : e ≈ e := setoid.refl _
/-- If two local homeomorphisms are equivalent, so are their inverses -/
lemma eq_on_source_symm {e e' : local_homeomorph α β} (h : e ≈ e') : e.symm ≈ e'.symm :=
local_equiv.eq_on_source_symm h
/-- Two equivalent local homeomorphisms have the same source -/
lemma source_eq_of_eq_on_source {e e' : local_homeomorph α β} (h : e ≈ e') : e.source = e'.source :=
h.1
/-- Two equivalent local homeomorphisms have the same target -/
lemma target_eq_of_eq_on_source {e e' : local_homeomorph α β} (h : e ≈ e') : e.target = e'.target :=
(eq_on_source_symm h).1
/-- Two equivalent local homeomorphisms have coinciding `to_fun` on the source -/
lemma apply_eq_of_eq_on_source {e e' : local_homeomorph α β} (h : e ≈ e') {x : α} (hx : x ∈ e.source) :
e.to_fun x = e'.to_fun x :=
h.2 x hx
/-- Two equivalent local homeomorphisms have coinciding `inv_fun` on the target -/
lemma inv_apply_eq_of_eq_on_source {e e' : local_homeomorph α β} (h : e ≈ e') {x : β} (hx : x ∈ e.target) :
e.inv_fun x = e'.inv_fun x :=
(eq_on_source_symm h).2 x hx
/-- Composition of local homeomorphisms respects equivalence -/
lemma eq_on_source_trans {e e' : local_homeomorph α β} {f f' : local_homeomorph β γ}
(he : e ≈ e') (hf : f ≈ f') : e.trans f ≈ e'.trans f' :=
begin
change local_equiv.eq_on_source (e.trans f).to_local_equiv (e'.trans f').to_local_equiv,
simp only [trans_to_local_equiv],
apply local_equiv.eq_on_source_trans,
exact he,
exact hf
end
/-- Restriction of local homeomorphisms respects equivalence -/
lemma eq_on_source_restr {e e' : local_homeomorph α β} (he : e ≈ e') (s : set α) :
e.restr s ≈ e'.restr s :=
local_equiv.eq_on_source_restr he _
/-- Composition of a local homeomorphism and its inverse is equivalent to the restriction of the
identity to the source -/
lemma trans_self_symm :
e.trans e.symm ≈ local_homeomorph.of_set e.source e.open_source :=
local_equiv.trans_self_symm _
lemma trans_symm_self :
e.symm.trans e ≈ local_homeomorph.of_set e.target e.open_target :=
e.symm.trans_self_symm
lemma eq_of_eq_on_source_univ {e e' : local_homeomorph α β} (h : e ≈ e')
(s : e.source = univ) (t : e.target = univ) : e = e' :=
eq_of_local_equiv_eq $ local_equiv.eq_of_eq_on_source_univ _ _ h s t
section prod
/-- The product of two local homeomorphisms, as a local homeomorphism on the product space. -/
def prod (e : local_homeomorph α β) (e' : local_homeomorph γ δ) : local_homeomorph (α × γ) (β × δ) :=
{ open_source := is_open_prod e.open_source e'.open_source,
open_target := is_open_prod e.open_target e'.open_target,
continuous_to_fun := continuous_on.prod
(continuous_on.comp e.continuous_to_fun continuous_fst.continuous_on (prod_subset_preimage_fst _ _))
(continuous_on.comp e'.continuous_to_fun continuous_snd.continuous_on (prod_subset_preimage_snd _ _)),
continuous_inv_fun := continuous_on.prod
(continuous_on.comp e.continuous_inv_fun continuous_fst.continuous_on (prod_subset_preimage_fst _ _))
(continuous_on.comp e'.continuous_inv_fun continuous_snd.continuous_on (prod_subset_preimage_snd _ _)),
..e.to_local_equiv.prod e'.to_local_equiv }
@[simp] lemma prod_to_local_equiv (e : local_homeomorph α β) (e' : local_homeomorph γ δ) :
(e.prod e').to_local_equiv = e.to_local_equiv.prod e'.to_local_equiv := rfl
@[simp] lemma prod_source (e : local_homeomorph α β) (e' : local_homeomorph γ δ) :
(e.prod e').source = set.prod e.source e'.source := rfl
@[simp] lemma prod_target (e : local_homeomorph α β) (e' : local_homeomorph γ δ) :
(e.prod e').target = set.prod e.target e'.target := rfl
@[simp] lemma prod_to_fun (e : local_homeomorph α β) (e' : local_homeomorph γ δ) :
(e.prod e').to_fun = (λp, (e.to_fun p.1, e'.to_fun p.2)) := rfl
@[simp] lemma prod_inv_fun (e : local_homeomorph α β) (e' : local_homeomorph γ δ) :
(e.prod e').inv_fun = (λp, (e.inv_fun p.1, e'.inv_fun p.2)) := rfl
end prod
section continuity
/-- Continuity within a set at a point can be read under right composition with a local
homeomorphism, if the point is in its target -/
lemma continuous_within_at_iff_continuous_within_at_comp_right
{f : β → γ} {s : set β} {x : β} (h : x ∈ e.target) :
continuous_within_at f s x ↔
continuous_within_at (f ∘ e.to_fun) (e.to_fun ⁻¹' s) (e.inv_fun x) :=
begin
split,
{ assume f_cont,
have : e.to_fun (e.inv_fun x) = x := e.right_inv h,
rw ← this at f_cont,
have : e.source ∈ 𝓝 (e.inv_fun x) := mem_nhds_sets e.open_source (e.map_target h),
rw [← continuous_within_at_inter this, inter_comm],
exact continuous_within_at.comp f_cont
((e.continuous_at_to_fun (e.map_target h)).continuous_within_at) (inter_subset_right _ _) },
{ assume fe_cont,
have : continuous_within_at ((f ∘ e.to_fun) ∘ e.inv_fun) (s ∩ e.target) x,
{ apply continuous_within_at.comp fe_cont,
apply (e.continuous_at_inv_fun h).continuous_within_at,
assume x hx,
simp [hx.1, hx.2, e.map_target] },
have : continuous_within_at f (s ∩ e.target) x :=
continuous_within_at.congr this (λy hy, by simp [hy.2]) (by simp [h]),
rwa continuous_within_at_inter at this,
exact mem_nhds_sets e.open_target h }
end
/-- Continuity at a point can be read under right composition with a local homeomorphism, if the
point is in its target -/
lemma continuous_at_iff_continuous_at_comp_right
{f : β → γ} {x : β} (h : x ∈ e.target) :
continuous_at f x ↔ continuous_at (f ∘ e.to_fun) (e.inv_fun x) :=
by rw [← continuous_within_at_univ, e.continuous_within_at_iff_continuous_within_at_comp_right h,
preimage_univ, continuous_within_at_univ]
/-- A function is continuous on a set if and only if its composition with a local homeomorphism
on the right is continuous on the corresponding set. -/
lemma continuous_on_iff_continuous_on_comp_right {f : β → γ} {s : set β} (h : s ⊆ e.target) :
continuous_on f s ↔ continuous_on (f ∘ e.to_fun) (e.source ∩ e.to_fun ⁻¹' s) :=
begin
split,
{ assume f_cont x hx,
have := e.continuous_within_at_iff_continuous_within_at_comp_right (e.map_source hx.1),
rw e.left_inv hx.1 at this,
have A := f_cont _ hx.2,
rw this at A,
exact A.mono (inter_subset_right _ _), },
{ assume fe_cont x hx,
have := e.continuous_within_at_iff_continuous_within_at_comp_right (h hx),
rw this,
have : e.source ∈ 𝓝 (e.inv_fun x) := mem_nhds_sets e.open_source (e.map_target (h hx)),
rw [← continuous_within_at_inter this, inter_comm],
exact fe_cont _ (by simp [hx, h hx, e.map_target (h hx)]) }
end
/-- Continuity within a set at a point can be read under left composition with a local
homeomorphism if a neighborhood of the initial point is sent to the source of the local
homeomorphism-/
lemma continuous_within_at_iff_continuous_within_at_comp_left
{f : γ → α} {s : set γ} {x : γ} (hx : f x ∈ e.source) (h : f ⁻¹' e.source ∈ nhds_within x s) :
continuous_within_at f s x ↔ continuous_within_at (e.to_fun ∘ f) s x :=
begin
rw [← continuous_within_at_inter' h, ← continuous_within_at_inter' h],
split,
{ assume f_cont,
have : e.source ∈ 𝓝 (f x) := mem_nhds_sets e.open_source hx,
apply continuous_within_at.comp (e.continuous_to_fun (f x) hx) f_cont (inter_subset_right _ _) },
{ assume fe_cont,
have : continuous_within_at (e.inv_fun ∘ (e.to_fun ∘ f)) (s ∩ f ⁻¹' e.source) x,
{ have : continuous_within_at e.inv_fun univ (e.to_fun (f x))
:= (e.continuous_at_inv_fun (e.map_source hx)).continuous_within_at,
exact continuous_within_at.comp this fe_cont (subset_univ _) },
exact this.congr (λy hy, by simp [e.left_inv hy.2]) (by simp [e.left_inv hx]) }
end
/-- Continuity at a point can be read under left composition with a local homeomorphism if a
neighborhood of the initial point is sent to the source of the local homeomorphism-/
lemma continuous_at_iff_continuous_at_comp_left
{f : γ → α} {x : γ} (h : f ⁻¹' e.source ∈ 𝓝 x) :
continuous_at f x ↔ continuous_at (e.to_fun ∘ f) x :=
begin
have hx : f x ∈ e.source := (mem_of_nhds h : _),
have h' : f ⁻¹' e.source ∈ nhds_within x univ, by rwa nhds_within_univ,
rw [← continuous_within_at_univ, ← continuous_within_at_univ,
e.continuous_within_at_iff_continuous_within_at_comp_left hx h']
end
/-- A function is continuous on a set if and only if its composition with a local homeomorphism
on the left is continuous on the corresponding set. -/
lemma continuous_on_iff_continuous_on_comp_left {f : γ → α} {s : set γ} (h : s ⊆ f ⁻¹' e.source) :
continuous_on f s ↔ continuous_on (e.to_fun ∘ f) s :=
begin
split,
{ assume f_cont,
exact continuous_on.comp e.continuous_to_fun f_cont h },
{ assume fe_cont,
have : continuous_on (e.inv_fun ∘ e.to_fun ∘ f) s,
{ apply continuous_on.comp e.continuous_inv_fun fe_cont,
assume x hx,
have : f x ∈ e.source := h hx,
simp [this] },
refine continuous_on.congr_mono this (λx hx, _) (subset.refl _),
have : f x ∈ e.source := h hx,
simp [this] }
end
end continuity
/-- If a local homeomorphism has source and target equal to univ, then it induces a homeomorphism
between the whole spaces, expressed in this definition. -/
def to_homeomorph_of_source_eq_univ_target_eq_univ (h : e.source = (univ : set α))
(h' : e.target = univ) : homeomorph α β :=
{ to_fun := e.to_fun,
inv_fun := e.inv_fun,
left_inv := λx, e.left_inv $ by { rw h, exact mem_univ _ },
right_inv := λx, e.right_inv $ by { rw h', exact mem_univ _ },
continuous_to_fun := begin
rw [continuous_iff_continuous_on_univ],
convert e.continuous_to_fun,
rw h
end,
continuous_inv_fun := begin
rw [continuous_iff_continuous_on_univ],
convert e.continuous_inv_fun,
rw h'
end }
@[simp] lemma to_homeomorph_to_fun (h : e.source = (univ : set α)) (h' : e.target = univ) :
(e.to_homeomorph_of_source_eq_univ_target_eq_univ h h').to_fun = e.to_fun := rfl
@[simp] lemma to_homeomorph_inv_fun (h : e.source = (univ : set α)) (h' : e.target = univ) :
(e.to_homeomorph_of_source_eq_univ_target_eq_univ h h').inv_fun = e.inv_fun := rfl
end local_homeomorph
namespace homeomorph
variables (e : homeomorph α β) (e' : homeomorph β γ)
/- Register as simp lemmas that the fields of a local homeomorphism built from a homeomorphism
correspond to the fields of the original homeomorphism. -/
@[simp] lemma to_local_homeomorph_source : e.to_local_homeomorph.source = univ := rfl
@[simp] lemma to_local_homeomorph_target : e.to_local_homeomorph.target = univ := rfl
@[simp] lemma to_local_homeomorph_to_fun : e.to_local_homeomorph.to_fun = e.to_fun := rfl
@[simp] lemma to_local_homeomorph_inv_fun : e.to_local_homeomorph.inv_fun = e.inv_fun := rfl
@[simp] lemma refl_to_local_homeomorph :
(homeomorph.refl α).to_local_homeomorph = local_homeomorph.refl α := rfl
@[simp] lemma symm_to_local_homeomorph : e.symm.to_local_homeomorph = e.to_local_homeomorph.symm := rfl
@[simp] lemma trans_to_local_homeomorph :
(e.trans e').to_local_homeomorph = e.to_local_homeomorph.trans e'.to_local_homeomorph :=
local_homeomorph.eq_of_local_equiv_eq $ equiv.trans_to_local_equiv _ _
end homeomorph
|
e95abaca1f433fb3cd7f0a6f56956e567b93d008 | fc086f79b20cf002d6f34b023749998408e94fbf | /src/tidy/auto_cast.lean | 0d14ebe4eeb70de064afb570c8ae774e57147ac9 | [] | no_license | semorrison/lean-tidy | f039460136b898fb282f75efedd92f2d5c5d90f8 | 6c1d46de6cff05e1c2c4c9692af812bca3e13b6c | refs/heads/master | 1,624,461,332,392 | 1,559,655,744,000 | 1,559,655,744,000 | 96,569,994 | 9 | 4 | null | 1,538,287,895,000 | 1,499,455,306,000 | Lean | UTF-8 | Lean | false | false | 502 | lean | -- Copyright (c) 2017 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Scott Morrison
import tidy.tidy
open tactic
set_option trace.tidy true
@[reducible] def {u} auto_cast {α β : Sort u} {h : α = β} (a : α) := cast h a
@[simp] lemma {u} auto_cast_identity {α : Sort u} (p : α = α) (a : α) : @auto_cast α α p a = a :=
begin unfold auto_cast, unfold cast, end
notation `⟬` p `⟭` := @auto_cast _ _ (by obviously) p
|
1a1bebece4136543f3157bcb66efc16cbc706aa8 | 2c41ae31b2b771ad5646ad880201393f5269a7f0 | /Lean/Examples/Parnas/Information_Hiding.lean | a624f1110f5895f2b64c3d9cdc5d8ee630fffb70 | [] | no_license | kevinsullivan/Boehm | 926f25bc6f1a8b6bd47d333d936fdfc278228312 | 55208395bff20d48a598b7fa33a4d55a2447a9cf | refs/heads/master | 1,586,127,134,302 | 1,488,252,326,000 | 1,488,252,326,000 | 32,836,930 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,974 | lean | import Examples.Parnas.DesignStructure
import SystemModel.System
-- This is a model of the example from Parnas 72
import Qualities.Changeable
import Examples.Parnas.Shared_Info
set_option eqn_compiler.max_steps 256
inductive kwicParameter | computer | corpus | user
| line_abs | line_alg | line_data
| alph_abs | alph_alg | alph_data
| circ_abs | circ_alg | circ_data
| input_abs | input_alg | input_data
| output_abs | output_alg | output_data
| master
open kwicParameter
definition uses : kwicParameter -> kwicParameter -> Prop
| kwicParameter.master kwicParameter.line_abs := true
| kwicParameter.master kwicParameter.input_abs := true
| kwicParameter.master kwicParameter.circ_abs := true
| kwicParameter.master kwicParameter.alph_abs := true
| kwicParameter.master kwicParameter.output_abs := true
| kwicParameter.line_alg kwicParameter.line_data := true
| kwicParameter.line_data kwicParameter.line_alg := true
| kwicParameter.input_alg kwicParameter.input_data := true
| kwicParameter.input_alg kwicParameter.line_abs := true
| kwicParameter.input_data kwicParameter.input_alg := true
| kwicParameter.circ_alg kwicParameter.circ_data := true
| kwicParameter.circ_alg kwicParameter.line_abs := true
| kwicParameter.circ_data kwicParameter.circ_alg := true
| kwicParameter.circ_data kwicParameter.line_abs := true
| kwicParameter.alph_alg kwicParameter.alph_data := true
| kwicParameter.alph_alg kwicParameter.line_abs := true
| kwicParameter.alph_data kwicParameter.alph_alg := true
| kwicParameter.alph_data kwicParameter.line_abs := true
| kwicParameter.output_alg kwicParameter.output_data := true
| kwicParameter.output_alg kwicParameter.line_abs := true
| kwicParameter.output_data kwicParameter.output_alg := true
| _ _ := false
definition satisfies : kwicParameter -> kwicParameter -> Prop
| computer line_data := true
| computer line_alg := true
| computer input_data := true
| computer circ_data := true
| computer alph_data := true
| computer alph_alg := true
| computer output_data := true
| computer output_alg := true
| corpus line_data := true
| corpus line_alg := true
| corpus input_data := true
| corpus input_alg := true
| corpus circ_data := true
| corpus circ_alg := true
| corpus alph_data := true
| corpus alph_alg := true
| corpus output_data := true
| corpus output_alg := true
| user input_alg := true
| user circ_alg := true
| user alph_alg := true
| user master := true
| line_abs line_data := true
| line_abs line_alg := true
| input_abs input_data := true
| input_abs input_alg := true
| circ_abs circ_data := true
| circ_abs circ_alg := true
| alph_abs alph_data := true
| alph_abs alph_alg := true
| output_abs output_data := true
| output_abs output_alg := true
| _ _ := false
definition input_mod : Module :=
{ name := "Input", elements := [input_data, input_alg, input_abs]}
definition line_mod : Module :=
{ name := "Line", elements := [line_data, line_alg, line_abs]}
definition circ_mod : Module :=
{ name := "Circular", elements := [circ_data, circ_alg, circ_abs]}
definition alph_mod : Module :=
{ name := "Alphabetizer", elements := [alph_data, alph_alg, alph_abs]}
definition out_mod : Module :=
{ name := "Output", elements := [output_data, output_alg, output_abs]}
definition master_mod : Module :=
{ name := "Master", elements := [master]}
definition kwic_modules : list Module :=
[input_mod, line_mod, circ_mod, alph_mod, out_mod, master_mod]
definition kwic_volatile : kwicParameter -> Prop
| corpus := true
| computer := true
| user := true
| _ := false
definition kwic_ds: DesignStructure :=
{ Modules := kwic_modules, Volatile := kwic_volatile, Uses:= uses, Satisfies:= satisfies }
inductive kwic_ds_type
| mk_kwic_ds : (@DesignStructure kwicParameter) -> Prop -> kwic_ds_type
definition extract_kwic_ds : kwic_ds_type -> (@DesignStructure kwicParameter)
| (kwic_ds_type.mk_kwic_ds ds _) := ds
record kwic :=
mk :: (kwic_volatile_state: kwicVolatileState) (kwic_design: kwic_ds_type)
definition kwicSystemType : SystemType := { Contexts:=kwicContexts, Stakeholders:= kwicStakeholders, Phases:= kwicPhases, ArtifactType:= kwic, ValueType:= kwicValue }
/-
Abbreviations for writing propositions, assertions, actions.
-/
definition kwicSystemState := @SystemInstance kwicSystemType
definition kwicAssertion := @Assertion kwicSystemType
definition kwicAction := @Action kwicSystemType
/- Test more specifically whether a system is modular -/
definition isModular (ks: kwicSystemState): Prop := modular (extract_kwic_ds ks^.artifact^.kwic_design)
/- Test more specifically whether a system is modular with respect to a single parameter -/
definition isModular_wrt (kp: kwicParameter) (ks: kwicSystemState): Prop := modular_wrt kp (extract_kwic_ds ks^.artifact^.kwic_design)
definition computerPre (ks: kwicSystemState): Prop := ks^.artifact^.kwic_volatile_state^.computer_state = computerState.computer_pre
definition computerPost (ks: kwicSystemState): Prop := ks^.artifact^.kwic_volatile_state^.computer_state = computerState.computer_post
definition corpusPre (ks: kwicSystemState): Prop := ks^.artifact^.kwic_volatile_state^.corpus_state = corpusState.corpus_pre
definition corpusPost (ks: kwicSystemState): Prop := ks^.artifact^.kwic_volatile_state^.corpus_state = corpusState.corpus_post
definition userPre (ks: kwicSystemState): Prop := ks^.artifact^.kwic_volatile_state^.user_state = userState.user_pre
definition userPost (ks: kwicSystemState): Prop := ks^.artifact^.kwic_volatile_state^.user_state = userState.user_post
definition inMaintenancePhase (ks: kwicSystemState): Prop := ks^.phase = kwicPhases.maintenance
definition computerPreState : kwicAssertion := fun ks: kwicSystemState, computerPre ks
definition computerPostState : kwicAssertion := fun ks: kwicSystemState, computerPost ks
definition corpusPreState : kwicAssertion := fun ks: kwicSystemState, corpusPre ks
definition corpusPostState : kwicAssertion := fun ks: kwicSystemState,corpusPost ks
definition userPreState : kwicAssertion := fun ks: kwicSystemState, userPre ks
definition userPostState : kwicAssertion := fun ks: kwicSystemState, userPost ks
/-
The customerChangeCorpus action changes the corpus from its pre-state to its post-state and makes no other changes.
-/
definition customerChangeCorpus: kwicAction :=
fun ks: kwicSystemState,
{ context:=kwicContexts.nominal,
phase:=kwicPhases.maintenance,
artifact:=
{ kwic_volatile_state :=
{ computer_state := ks^.artifact^.kwic_volatile_state^.computer_state,
corpus_state:= corpusState.corpus_post,
user_state:= ks^.artifact^.kwic_volatile_state^.user_state },
kwic_design := (kwic_ds_type.mk_kwic_ds kwic_ds (kwic_ds=kwic_ds))},
value := ks^.value } |
a54c1ac530729f271cc0413c1a1229cd9f0b3d23 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/nat/parity.lean | 5ec996b4dfb0b5bb4cae2f302ef53d03c47ec60d | [
"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 | 11,118 | lean | /-
Copyright (c) 2019 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Benjamin Davidson
-/
import data.nat.modeq
import algebra.parity
/-!
# Parity of natural numbers
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file contains theorems about the `even` and `odd` predicates on the natural numbers.
## Tags
even, odd
-/
namespace nat
variables {m n : ℕ}
@[simp] theorem mod_two_ne_one : ¬ 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 % 2 = 0 ↔ n % 2 = 1 :=
by cases mod_two_eq_zero_or_one n with h h; simp [h]
theorem even_iff : even n ↔ n % 2 = 0 :=
⟨λ ⟨m, hm⟩, by simp [← two_mul, hm],
λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by simp [← two_mul, h])⟩⟩
theorem odd_iff : odd n ↔ n % 2 = 1 :=
⟨λ ⟨m, hm⟩, by norm_num [hm, add_mod],
λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by rw [h, add_comm])⟩⟩
lemma not_even_iff : ¬ even n ↔ n % 2 = 1 :=
by rw [even_iff, mod_two_ne_zero]
lemma not_odd_iff : ¬ odd n ↔ n % 2 = 0 :=
by rw [odd_iff, mod_two_ne_one]
lemma even_iff_not_odd : even n ↔ ¬ odd n :=
by rw [not_odd_iff, even_iff]
@[simp] lemma odd_iff_not_even : odd n ↔ ¬ even n :=
by rw [not_even_iff, odd_iff]
lemma is_compl_even_odd : is_compl {n : ℕ | even n} {n | odd n} :=
by simp only [←set.compl_set_of, is_compl_compl, odd_iff_not_even]
lemma even_or_odd (n : ℕ) : even n ∨ odd n :=
or.imp_right odd_iff_not_even.2 $ em $ even n
lemma even_or_odd' (n : ℕ) : ∃ k, n = 2 * k ∨ n = 2 * k + 1 :=
by simpa only [← two_mul, exists_or_distrib, ← odd, ← even] using even_or_odd n
lemma even_xor_odd (n : ℕ) : xor (even n) (odd n) :=
begin
cases even_or_odd n with h,
{ exact or.inl ⟨h, even_iff_not_odd.mp h⟩ },
{ exact or.inr ⟨h, odd_iff_not_even.mp h⟩ },
end
lemma even_xor_odd' (n : ℕ) : ∃ k, xor (n = 2 * k) (n = 2 * k + 1) :=
begin
rcases even_or_odd n with ⟨k, rfl⟩ | ⟨k, rfl⟩;
use k,
{ simpa only [← two_mul, xor, true_and, eq_self_iff_true, not_true, or_false, and_false]
using (succ_ne_self (2*k)).symm },
{ simp only [xor, add_right_eq_self, false_or, eq_self_iff_true, not_true, not_false_iff,
one_ne_zero, and_self] },
end
@[simp] theorem two_dvd_ne_zero : ¬ 2 ∣ n ↔ n % 2 = 1 :=
even_iff_two_dvd.symm.not.trans not_even_iff
instance : decidable_pred (even : ℕ → Prop) := λ n, decidable_of_iff _ even_iff.symm
instance : decidable_pred (odd : ℕ → Prop) := λ n, decidable_of_iff _ odd_iff_not_even.symm
theorem mod_two_add_add_odd_mod_two (m : ℕ) {n : ℕ} (hn : odd n) : m % 2 + (m + n) % 2 = 1 :=
(even_or_odd m).elim (λ hm, by rw [even_iff.1 hm, odd_iff.1 (hm.add_odd hn)]) $
λ hm, by rw [odd_iff.1 hm, even_iff.1 (hm.add_odd hn)]
@[simp] theorem mod_two_add_succ_mod_two (m : ℕ) : m % 2 + (m + 1) % 2 = 1 :=
mod_two_add_add_odd_mod_two m odd_one
@[simp] theorem succ_mod_two_add_mod_two (m : ℕ) : (m + 1) % 2 + m % 2 = 1 :=
by rw [add_comm, mod_two_add_succ_mod_two]
@[simp] theorem not_even_one : ¬ even 1 :=
by rw even_iff; norm_num
@[parity_simps] theorem even_add : even (m + n) ↔ (even m ↔ even n) :=
by 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₂, nat.add_mod];
norm_num
theorem even_add' : even (m + n) ↔ (odd m ↔ odd n) :=
by rw [even_add, even_iff_not_odd, even_iff_not_odd, not_iff_not]
@[parity_simps] theorem even_add_one : even (n + 1) ↔ ¬ even n :=
by simp [even_add]
@[simp] theorem not_even_bit1 (n : ℕ) : ¬ even (bit1 n) :=
by simp [bit1] with parity_simps
lemma two_not_dvd_two_mul_add_one (n : ℕ) : ¬(2 ∣ 2 * n + 1) :=
by simp [add_mod]
lemma two_not_dvd_two_mul_sub_one : Π {n} (w : 0 < n), ¬(2 ∣ 2 * n - 1)
| (n + 1) _ := two_not_dvd_two_mul_add_one n
@[parity_simps] theorem even_sub (h : n ≤ m) : even (m - n) ↔ (even m ↔ even n) :=
begin
conv { to_rhs, rw [←tsub_add_cancel_of_le h, even_add] },
by_cases h : even n; simp [h]
end
theorem even_sub' (h : n ≤ m) : even (m - n) ↔ (odd m ↔ odd n) :=
by rw [even_sub h, even_iff_not_odd, even_iff_not_odd, not_iff_not]
theorem odd.sub_odd (hm : odd m) (hn : odd n) : even (m - n) :=
(le_total n m).elim
(λ h, by simp only [even_sub' h, *])
(λ h, by simp only [tsub_eq_zero_iff_le.mpr h, even_zero])
@[parity_simps] theorem even_mul : even (m * n) ↔ even m ∨ even n :=
by 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₂, nat.mul_mod];
norm_num
theorem odd_mul : odd (m * n) ↔ odd m ∧ odd n :=
by simp [not_or_distrib] with parity_simps
theorem odd.of_mul_left (h : odd (m * n)) : odd m :=
(odd_mul.mp h).1
theorem odd.of_mul_right (h : odd (m * n)) : odd n :=
(odd_mul.mp h).2
/-- If `m` and `n` are natural numbers, then the natural number `m^n` is even
if and only if `m` is even and `n` is positive. -/
@[parity_simps] theorem even_pow : even (m ^ n) ↔ even m ∧ n ≠ 0 :=
by { induction n with n ih; simp [*, pow_succ', even_mul], tauto }
theorem even_pow' (h : n ≠ 0) : even (m ^ n) ↔ even m :=
even_pow.trans $ and_iff_left h
theorem even_div : even (m / n) ↔ m % (2 * n) / n = 0 :=
by rw [even_iff_two_dvd, dvd_iff_mod_eq_zero, nat.div_mod_eq_mod_mul_div, mul_comm]
@[parity_simps] theorem odd_add : odd (m + n) ↔ (odd m ↔ even n) :=
by rw [odd_iff_not_even, even_add, not_iff, odd_iff_not_even]
theorem odd_add' : odd (m + n) ↔ (odd n ↔ even m) :=
by rw [add_comm, odd_add]
lemma ne_of_odd_add (h : odd (m + n)) : m ≠ n :=
λ hnot, by simpa [hnot] with parity_simps using h
@[parity_simps] theorem odd_sub (h : n ≤ m) : odd (m - n) ↔ (odd m ↔ even n) :=
by rw [odd_iff_not_even, even_sub h, not_iff, odd_iff_not_even]
theorem odd.sub_even (h : n ≤ m) (hm : odd m) (hn : even n) : odd (m - n) :=
(odd_sub h).mpr $ iff_of_true hm hn
theorem odd_sub' (h : n ≤ m) : odd (m - n) ↔ (odd n ↔ even m) :=
by rw [odd_iff_not_even, even_sub h, not_iff, not_iff_comm, odd_iff_not_even]
theorem even.sub_odd (h : n ≤ m) (hm : even m) (hn : odd n) : odd (m - n) :=
(odd_sub' h).mpr $ iff_of_true hn hm
lemma even_mul_succ_self (n : ℕ) : even (n * (n + 1)) :=
begin
rw even_mul,
convert n.even_or_odd,
simp with parity_simps
end
lemma even_mul_self_pred (n : ℕ) : even (n * (n - 1)) :=
begin
cases n,
{ exact even_zero },
{ rw mul_comm,
apply even_mul_succ_self }
end
lemma two_mul_div_two_of_even : even n → 2 * (n / 2) = n :=
λ h, nat.mul_div_cancel_left' (even_iff_two_dvd.mp h)
lemma div_two_mul_two_of_even : even n → n / 2 * 2 = n :=
λ h, nat.div_mul_cancel (even_iff_two_dvd.mp h)
lemma two_mul_div_two_add_one_of_odd (h : odd n) : 2 * (n / 2) + 1 = n :=
by { rw mul_comm, convert nat.div_add_mod' n 2, rw odd_iff.mp h }
lemma div_two_mul_two_add_one_of_odd (h : odd n) : n / 2 * 2 + 1 = n :=
by { convert nat.div_add_mod' n 2, rw odd_iff.mp h }
lemma one_add_div_two_mul_two_of_odd (h : odd n) : 1 + n / 2 * 2 = n :=
by { rw add_comm, convert nat.div_add_mod' n 2, rw odd_iff.mp h }
lemma bit0_div_two : bit0 n / 2 = n :=
by rw [←nat.bit0_eq_bit0, bit0_eq_two_mul, two_mul_div_two_of_even (even_bit0 n)]
lemma bit1_div_two : bit1 n / 2 = n :=
by rw [←nat.bit1_eq_bit1, bit1, bit0_eq_two_mul, nat.two_mul_div_two_add_one_of_odd (odd_bit1 n)]
@[simp] lemma bit0_div_bit0 : bit0 n / bit0 m = n / m :=
by rw [bit0_eq_two_mul m, ←nat.div_div_eq_div_mul, bit0_div_two]
@[simp] lemma bit1_div_bit0 : bit1 n / bit0 m = n / m :=
by rw [bit0_eq_two_mul, ←nat.div_div_eq_div_mul, bit1_div_two]
@[simp] lemma bit0_mod_bit0 : bit0 n % bit0 m = bit0 (n % m) :=
by rw [bit0_eq_two_mul n, bit0_eq_two_mul m, bit0_eq_two_mul (n % m), nat.mul_mod_mul_left]
@[simp] lemma bit1_mod_bit0 : bit1 n % bit0 m = bit1 (n % m) :=
begin
have h₁ := congr_arg bit1 (nat.div_add_mod n m),
-- `∀ m n : ℕ, bit0 m * n = bit0 (m * n)` seems to be missing...
rw [bit1_add, bit0_eq_two_mul, ← mul_assoc, ← bit0_eq_two_mul] at h₁,
have h₂ := nat.div_add_mod (bit1 n) (bit0 m),
rw [bit1_div_bit0] at h₂,
exact add_left_cancel (h₂.trans h₁.symm),
end
-- Here are examples of how `parity_simps` can be used with `nat`.
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 nat
open nat
namespace function
namespace involutive
variables {α : Type*} {f : α → α} {n : ℕ}
theorem iterate_bit0 (hf : involutive f) (n : ℕ) : f^[bit0 n] = id :=
by rw [bit0, ← two_mul, iterate_mul, involutive_iff_iter_2_eq_id.1 hf, iterate_id]
theorem iterate_bit1 (hf : involutive f) (n : ℕ) : f^[bit1 n] = f :=
by rw [bit1, iterate_succ, hf.iterate_bit0, comp.left_id]
theorem iterate_even (hf : involutive f) (hn : even n) : f^[n] = id :=
let ⟨m, hm⟩ := hn in hm.symm ▸ hf.iterate_bit0 m
theorem iterate_odd (hf : involutive f) (hn : odd n) : f^[n] = f :=
let ⟨m, hm⟩ := odd_iff_exists_bit1.mp hn in hm.symm ▸ hf.iterate_bit1 m
theorem iterate_eq_self (hf : involutive f) (hne : f ≠ id) : f^[n] = f ↔ odd n :=
⟨λ H, odd_iff_not_even.2 $ λ hn, hne $ by rwa [hf.iterate_even hn, eq_comm] at H, hf.iterate_odd⟩
theorem iterate_eq_id (hf : involutive f) (hne : f ≠ id) : f^[n] = id ↔ even n :=
⟨λ H, even_iff_not_odd.2 $ λ hn, hne $ by rwa [hf.iterate_odd hn] at H, hf.iterate_even⟩
end involutive
end function
variables {R : Type*} [monoid R] [has_distrib_neg R] {n : ℕ}
lemma neg_one_pow_eq_one_iff_even (h : (-1 : R) ≠ 1) : (-1 : R) ^ n = 1 ↔ even n :=
⟨λ h', of_not_not $ λ hn, h $ (odd.neg_one_pow $ odd_iff_not_even.mpr hn).symm.trans h',
even.neg_one_pow⟩
/-- If `a` is even, then `n` is odd iff `n % a` is odd. -/
lemma odd.mod_even_iff {n a : ℕ} (ha : even a) : odd (n % a) ↔ odd n :=
((even_sub' $ mod_le n a).mp $ even_iff_two_dvd.mpr $ (even_iff_two_dvd.mp ha).trans $
dvd_sub_mod n).symm
/-- If `a` is even, then `n` is even iff `n % a` is even. -/
lemma even.mod_even_iff {n a : ℕ} (ha : even a) : even (n % a) ↔ even n :=
((even_sub $ mod_le n a).mp $ even_iff_two_dvd.mpr $ (even_iff_two_dvd.mp ha).trans $
dvd_sub_mod n).symm
/-- If `n` is odd and `a` is even, then `n % a` is odd. -/
lemma odd.mod_even {n a : ℕ} (hn : odd n) (ha : even a) : odd (n % a) :=
(odd.mod_even_iff ha).mpr hn
/-- If `n` is even and `a` is even, then `n % a` is even. -/
lemma even.mod_even {n a : ℕ} (hn : even n) (ha : even a) : even (n % a) :=
(even.mod_even_iff ha).mpr hn
theorem odd.of_dvd_nat {m n : ℕ} (hn : odd n) (hm : m ∣ n) : odd m :=
odd_iff_not_even.2 $ mt hm.even (odd_iff_not_even.1 hn)
/-- `2` is not a factor of an odd natural number. -/
theorem odd.ne_two_of_dvd_nat {m n : ℕ} (hn : odd n) (hm : m ∣ n) : m ≠ 2 :=
begin
rintro rfl,
exact absurd (hn.of_dvd_nat hm) dec_trivial
end
|
8dec005de49ce2d04e4762b27776cc316afa86bb | 5756a081670ba9c1d1d3fca7bd47cb4e31beae66 | /Mathport/Syntax/Translate.lean | 55e753c1ebbc9744b02c0aef2de46b74581bf306 | [
"Apache-2.0"
] | permissive | leanprover-community/mathport | 2c9bdc8292168febf59799efdc5451dbf0450d4a | 13051f68064f7638970d39a8fecaede68ffbf9e1 | refs/heads/master | 1,693,841,364,079 | 1,693,813,111,000 | 1,693,813,111,000 | 379,357,010 | 27 | 10 | Apache-2.0 | 1,691,309,132,000 | 1,624,384,521,000 | Lean | UTF-8 | Lean | false | false | 3,489 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Daniel Selsam
-/
import Mathport.Syntax.Translate.Basic
import Mathport.Syntax.Translate.Attributes
import Mathport.Syntax.Translate.Notation
import Mathport.Syntax.Translate.Parser
import Mathport.Syntax.Translate.Expr
import Mathport.Syntax.Translate.Tactic
import Mathport.Syntax.Translate.Tactic.Builtin
import Mathport.Syntax.Transform
namespace Mathport
open Lean hiding Expr Expr.app Expr.const Expr.sort Level Level.imax Level.max Level.param Command
open Lean.Elab.Command (CommandElabM liftCoreM)
namespace Translate
open AST3
partial def M.run' (m : M α) (notations : Array Notation) (commands : Array Command)
(config : Config) : CommandElabM α := do
let s ← ST.mkRef {}
let rec ctx := {
config, notations, commands
transform := Transform.transform
trExpr := fun e => trExpr' e ctx s
trTactic := fun e => trTactic' e ctx s
trCommand := fun c => trCommand' c ctx s }
m ctx s
def M.run (m : M α) (comments : Array Comment) :
(notations : Array Notation) → (commands : Array Command) →
(config : Config) → CommandElabM α :=
M.run' $ do
let tactics ← Tactic.builtinTactics
let niTactics ← Tactic.builtinNITactics
let convs ← Tactic.builtinConvs
let userNotas ← Tactic.builtinUserNotation
let userAttrs ← Tactic.builtinUserAttrs
let userCmds ← Tactic.builtinUserCmds
modify fun s => { s with
tactics, niTactics, convs, userNotas, userAttrs, userCmds,
remainingComments := comments.qsort (positionToStringPos ·.start < positionToStringPos ·.start) |>.toList }
m
def AST3toData4 (path : Path) : AST3 → M Data4
| ⟨prel, imp, commands, _, _, _⟩ => do
let prel := prel.map fun _ => mkNode ``Parser.Module.prelude #[mkAtom "prelude"]
let imp ← imp.foldlM (init := #[]) fun imp ns =>
ns.foldlM (init := imp) fun imp n =>
return imp.push $ mkNode ``Parser.Module.import #[mkAtom "import",
mkNullNode, mkIdent (← renameModule n.kind)]
let fmt ← liftCoreM $ PrettyPrinter.format Parser.Module.header.formatter $
mkNode ``Parser.Module.header #[mkOptionalNode prel, mkNullNode imp]
let commitInfo := (← read).config.commitInfo
printFirstLineComments
printOutput fmt
-- todo: use the pretty-printer?
if let some ci := commitInfo then
let commit := ci.fileRevs.findD (path.mod3.toFilePath.toString ++ ".lean") ci.commit
printOutput f!"#align_import {path.mod3} from {repr ci.repo}@{repr commit}\n\n"
else
printOutput f!"#align_import {path.mod3}\n\n"
commands.forM fun c => do
try trCommand c
catch e =>
let e := s!"error in {(← getEnv).mainModule}: {← e.toMessageData.toString}"
println! e
-- println! (repr c.kind)
printOutput f!"/- {e}\nLean 3 AST:\n{(repr c.kind).group}-/\n\n"
printRemainingComments
pure ⟨(← get).output, HashMap.empty⟩
end Translate
def AST3toData4 (path : Path) (ast : AST3) : (config : Config) → CommandElabM Data4 :=
(Translate.AST3toData4 path ast).run ast.comments ast.indexed_nota ast.indexed_cmds
def tactic3toSyntax (containingFile : AST3) (tac3 : Spanned AST3.Tactic) : (config : Config) → CommandElabM Syntax.Tactic :=
(Translate.trTactic tac3).run #[] containingFile.indexed_nota containingFile.indexed_cmds
|
d33517055154e3fccad567796eae0e23020db061 | 46125763b4dbf50619e8846a1371029346f4c3db | /src/data/nat/totient.lean | 2c6155d32c6cdc8d088e0f15c0e954a7e46c6cf1 | [
"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 | 3,523 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import algebra.big_operators data.nat.gcd
open finset
namespace nat
def totient (n : ℕ) : ℕ := ((range n).filter (nat.coprime n)).card
localized "notation `φ` := nat.totient" in nat
lemma totient_le (n : ℕ) : φ n ≤ n :=
calc totient n ≤ (range n).card : card_le_of_subset (filter_subset _)
... = n : card_range _
lemma totient_pos : ∀ {n : ℕ}, 0 < n → 0 < φ n
| 0 := dec_trivial
| 1 := dec_trivial
| (n+2) := λ h, card_pos.2 ⟨1, mem_filter.2 ⟨mem_range.2 dec_trivial, coprime_one_right _⟩⟩
lemma sum_totient (n : ℕ) : ((range n.succ).filter (∣ n)).sum φ = n :=
if hn0 : n = 0 then by rw hn0; refl
else
calc ((range n.succ).filter (∣ n)).sum φ
= ((range n.succ).filter (∣ n)).sum (λ d, ((range (n / d)).filter (λ m, gcd (n / d) m = 1)).card) :
eq.symm $ sum_bij (λ d _, n / d)
(λ d hd, mem_filter.2 ⟨mem_range.2 $ lt_succ_of_le $ nat.div_le_self _ _,
by conv {to_rhs, rw ← nat.mul_div_cancel' (mem_filter.1 hd).2}; simp⟩)
(λ _ _, rfl)
(λ a b ha hb h,
have ha : a * (n / a) = n, from nat.mul_div_cancel' (mem_filter.1 ha).2,
have 0 < (n / a), from nat.pos_of_ne_zero (λ h, by simp [*, lt_irrefl] at *),
by rw [← nat.mul_right_inj this, ha, h, nat.mul_div_cancel' (mem_filter.1 hb).2])
(λ b hb,
have hb : b < n.succ ∧ b ∣ n, by simpa [-range_succ] using hb,
have hbn : (n / b) ∣ n, from ⟨b, by rw nat.div_mul_cancel hb.2⟩,
have hnb0 : (n / b) ≠ 0, from λ h, by simpa [h, ne.symm hn0] using nat.div_mul_cancel hbn,
⟨n / b, mem_filter.2 ⟨mem_range.2 $ lt_succ_of_le $ nat.div_le_self _ _, hbn⟩,
by rw [← nat.mul_right_inj (nat.pos_of_ne_zero hnb0),
nat.mul_div_cancel' hb.2, nat.div_mul_cancel hbn]⟩)
... = ((range n.succ).filter (∣ n)).sum (λ d, ((range n).filter (λ m, gcd n m = d)).card) :
sum_congr rfl (λ d hd,
have hd : d ∣ n, from (mem_filter.1 hd).2,
have hd0 : 0 < d, from nat.pos_of_ne_zero (λ h, hn0 (eq_zero_of_zero_dvd $ h ▸ hd)),
card_congr (λ m hm, d * m)
(λ m hm, have hm : m < n / d ∧ gcd (n / d) m = 1, by simpa using hm,
mem_filter.2 ⟨mem_range.2 $ nat.mul_div_cancel' hd ▸
(mul_lt_mul_left hd0).2 hm.1,
by rw [← nat.mul_div_cancel' hd, gcd_mul_left, hm.2, mul_one]⟩)
(λ a b ha hb h, (nat.mul_left_inj hd0).1 h)
(λ b hb, have hb : b < n ∧ gcd n b = d, by simpa using hb,
⟨b / d, mem_filter.2 ⟨mem_range.2 ((mul_lt_mul_left (show 0 < d, from hb.2 ▸ hb.2.symm ▸ hd0)).1
(by rw [← hb.2, nat.mul_div_cancel' (gcd_dvd_left _ _),
nat.mul_div_cancel' (gcd_dvd_right _ _)]; exact hb.1)),
hb.2 ▸ coprime_div_gcd_div_gcd (hb.2.symm ▸ hd0)⟩,
hb.2 ▸ nat.mul_div_cancel' (gcd_dvd_right _ _)⟩))
... = ((filter (∣ n) (range n.succ)).bind (λ d, (range n).filter (λ m, gcd n m = d))).card :
(card_bind (by intros; apply disjoint_filter.2; cc)).symm
... = (range n).card :
congr_arg card (finset.ext.2 (λ m, ⟨by finish,
λ hm, have h : m < n, from mem_range.1 hm,
mem_bind.2 ⟨gcd n m, mem_filter.2 ⟨mem_range.2 (lt_succ_of_le (le_of_dvd (lt_of_le_of_lt (nat.zero_le _) h)
(gcd_dvd_left _ _))), gcd_dvd_left _ _⟩, mem_filter.2 ⟨hm, rfl⟩⟩⟩))
... = n : card_range _
end nat
|
79f2ea11a579333824e56e2eb5312dd2b36a9e85 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/topology/category/TopCommRing.lean | 40d6388467bc26fe0a4381664dd5094ac181d708 | [] | 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 | 4,244 | 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 Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.category.CommRing.basic
import Mathlib.topology.category.Top.basic
import Mathlib.topology.algebra.ring
import Mathlib.PostPort
universes u l u_1 u_2
namespace Mathlib
/-- A bundled topological commutative ring. -/
structure TopCommRing
where
α : Type u
is_comm_ring : comm_ring α
is_topological_space : topological_space α
is_topological_ring : topological_ring α
namespace TopCommRing
protected instance has_coe_to_sort : has_coe_to_sort TopCommRing :=
has_coe_to_sort.mk (Type u) α
protected instance category_theory.category : category_theory.category TopCommRing :=
category_theory.category.mk
protected instance category_theory.concrete_category : category_theory.concrete_category TopCommRing :=
category_theory.concrete_category.mk
(category_theory.functor.mk (fun (R : TopCommRing) => ↥R) fun (R S : TopCommRing) (f : R ⟶ S) => ⇑(subtype.val f))
/-- Construct a bundled `TopCommRing` from the underlying type and the appropriate typeclasses. -/
def of (X : Type u) [comm_ring X] [topological_space X] [topological_ring X] : TopCommRing :=
mk X
@[simp] theorem coe_of (X : Type u) [comm_ring X] [topological_space X] [topological_ring X] : ↥(of X) = X :=
rfl
protected instance forget_topological_space (R : TopCommRing) : topological_space (category_theory.functor.obj (category_theory.forget TopCommRing) R) :=
is_topological_space R
protected instance forget_comm_ring (R : TopCommRing) : comm_ring (category_theory.functor.obj (category_theory.forget TopCommRing) R) :=
is_comm_ring R
protected instance forget_topological_ring (R : TopCommRing) : topological_ring (category_theory.functor.obj (category_theory.forget TopCommRing) R) :=
is_topological_ring R
protected instance has_forget_to_CommRing : category_theory.has_forget₂ TopCommRing CommRing :=
category_theory.has_forget₂.mk' (fun (R : TopCommRing) => CommRing.of ↥R) sorry
(fun (R S : TopCommRing) (f : R ⟶ S) => subtype.val f) sorry
protected instance forget_to_CommRing_topological_space (R : TopCommRing) : topological_space ↥(category_theory.functor.obj (category_theory.forget₂ TopCommRing CommRing) R) :=
is_topological_space R
/-- The forgetful functor to Top. -/
protected instance has_forget_to_Top : category_theory.has_forget₂ TopCommRing Top :=
category_theory.has_forget₂.mk' (fun (R : TopCommRing) => Top.of ↥R) sorry
(fun (R S : TopCommRing) (f : R ⟶ S) => continuous_map.mk ⇑(subtype.val f)) sorry
protected instance forget_to_Top_comm_ring (R : TopCommRing) : comm_ring ↥(category_theory.functor.obj (category_theory.forget₂ TopCommRing Top) R) :=
is_comm_ring R
protected instance forget_to_Top_topological_ring (R : TopCommRing) : topological_ring ↥(category_theory.functor.obj (category_theory.forget₂ TopCommRing Top) R) :=
is_topological_ring R
/--
The forgetful functors to `Type` do not reflect isomorphisms,
but the forgetful functor from `TopCommRing` to `Top` does.
-/
protected instance category_theory.forget₂.category_theory.reflects_isomorphisms : category_theory.reflects_isomorphisms (category_theory.forget₂ TopCommRing Top) :=
category_theory.reflects_isomorphisms.mk
fun (X Y : TopCommRing) (f : X ⟶ Y)
(_x : category_theory.is_iso (category_theory.functor.map (category_theory.forget₂ TopCommRing Top) f)) =>
let i_Top :
category_theory.functor.obj (category_theory.forget₂ TopCommRing Top) X ≅
category_theory.functor.obj (category_theory.forget₂ TopCommRing Top) Y :=
category_theory.as_iso (category_theory.functor.map (category_theory.forget₂ TopCommRing Top) f);
let e_Ring : ↥X ≃+* ↥Y :=
ring_equiv.mk (ring_hom.to_fun (subtype.val f))
(equiv.inv_fun
(category_theory.iso.to_equiv (category_theory.functor.map_iso (category_theory.forget Top) i_Top)))
sorry sorry sorry sorry;
category_theory.is_iso.mk { val := ↑(ring_equiv.symm e_Ring), property := sorry }
|
4ae4d484de612b91d4fbf99128dd1ed2d6bbfa60 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/data/nat/digits.lean | 046f13006e9b01313c944389d2f27b126b6a7e35 | [
"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 | 21,799 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Shing Tak Lam, Mario Carneiro
-/
import data.int.modeq
import data.list.indexes
import tactic.interval_cases
import tactic.linarith
/-!
# Digits of a natural number
This provides a basic API for extracting the digits of a natural number in a given base,
and reconstructing numbers from their digits.
We also prove some divisibility tests based on digits, in particular completing
Theorem #85 from https://www.cs.ru.nl/~freek/100/.
A basic `norm_digits` tactic is also provided for proving goals of the form
`nat.digits a b = l` where `a` and `b` are numerals.
-/
namespace nat
/-- (Impl.) An auxiliary definition for `digits`, to help get the desired definitional unfolding. -/
def digits_aux_0 : ℕ → list ℕ
| 0 := []
| (n+1) := [n+1]
/-- (Impl.) An auxiliary definition for `digits`, to help get the desired definitional unfolding. -/
def digits_aux_1 (n : ℕ) : list ℕ := list.repeat 1 n
/-- (Impl.) An auxiliary definition for `digits`, to help get the desired definitional unfolding. -/
def digits_aux (b : ℕ) (h : 2 ≤ b) : ℕ → list ℕ
| 0 := []
| (n+1) :=
have (n+1)/b < n+1 := nat.div_lt_self (nat.succ_pos _) h,
(n+1) % b :: digits_aux ((n+1)/b)
@[simp] lemma digits_aux_zero (b : ℕ) (h : 2 ≤ b) : digits_aux b h 0 = [] := by rw digits_aux
lemma digits_aux_def (b : ℕ) (h : 2 ≤ b) (n : ℕ) (w : 0 < n) :
digits_aux b h n = n % b :: digits_aux b h (n/b) :=
begin
cases n,
{ cases w, },
{ rw [digits_aux], }
end
/--
`digits b n` gives the digits, in little-endian order,
of a natural number `n` in a specified base `b`.
In any base, we have `of_digits b L = L.foldr (λ x y, x + b * y) 0`.
* For any `2 ≤ b`, we have `l < b` for any `l ∈ digits b n`,
and the last digit is not zero.
This uniquely specifies the behaviour of `digits b`.
* For `b = 1`, we define `digits 1 n = list.repeat 1 n`.
* For `b = 0`, we define `digits 0 n = [n]`, except `digits 0 0 = []`.
Note this differs from the existing `nat.to_digits` in core, which is used for printing numerals.
In particular, `nat.to_digits b 0 = [0]`, while `digits b 0 = []`.
-/
def digits : ℕ → ℕ → list ℕ
| 0 := digits_aux_0
| 1 := digits_aux_1
| (b+2) := digits_aux (b+2) (by norm_num)
@[simp] lemma digits_zero (b : ℕ) : digits b 0 = [] :=
by rcases b with _|⟨_|⟨_⟩⟩; simp [digits, digits_aux_0, digits_aux_1]
@[simp] lemma digits_zero_zero : digits 0 0 = [] := rfl
@[simp] lemma digits_zero_succ (n : ℕ) : digits 0 (n.succ) = [n+1] := rfl
theorem digits_zero_succ' : ∀ {n : ℕ} (w : 0 < n), digits 0 n = [n]
| 0 h := absurd h dec_trivial
| (n+1) _ := rfl
@[simp] lemma digits_one (n : ℕ) : digits 1 n = list.repeat 1 n := rfl
@[simp] lemma digits_one_succ (n : ℕ) : digits 1 (n + 1) = 1 :: digits 1 n := rfl
@[simp] lemma digits_add_two_add_one (b n : ℕ) :
digits (b+2) (n+1) = (((n+1) % (b+2)) :: digits (b+2) ((n+1) / (b+2))) :=
by { rw [digits, digits_aux_def], exact succ_pos n }
theorem digits_def' : ∀ {b : ℕ} (h : 2 ≤ b) {n : ℕ} (w : 0 < n),
digits b n = n % b :: digits b (n/b)
| 0 h := absurd h dec_trivial
| 1 h := absurd h dec_trivial
| (b+2) h := digits_aux_def _ _
@[simp]
lemma digits_of_lt (b x : ℕ) (w₁ : 0 < x) (w₂ : x < b) : digits b x = [x] :=
begin
cases b,
{ cases w₂ },
{ cases b,
{ interval_cases x, },
{ cases x,
{ cases w₁, },
{ rw [digits_add_two_add_one, nat.div_eq_of_lt w₂, digits_zero, nat.mod_eq_of_lt w₂] } } }
end
lemma digits_add (b : ℕ) (h : 2 ≤ b) (x y : ℕ) (w : x < b) (w' : 0 < x ∨ 0 < y) :
digits b (x + b * y) = x :: digits b y :=
begin
cases b,
{ cases h, },
{ cases b,
{ norm_num at h, },
{ cases y,
{ norm_num at w',
simp [w, w'], },
dsimp [digits],
rw digits_aux_def,
{ congr,
{ simp [nat.add_mod, nat.mod_eq_of_lt w], },
{ simp [mul_comm (b+2), nat.add_mul_div_right, nat.div_eq_of_lt w], }
},
{ apply nat.succ_pos, }, }, },
end
/--
`of_digits b L` takes a list `L` of natural numbers, and interprets them
as a number in semiring, as the little-endian digits in base `b`.
-/
-- If we had a function converting a list into a polynomial,
-- and appropriate lemmas about that function,
-- we could rewrite this in terms of that.
def of_digits {α : Type*} [semiring α] (b : α) : list ℕ → α
| [] := 0
| (h :: t) := h + b * of_digits t
lemma of_digits_eq_foldr {α : Type*} [semiring α] (b : α) (L : list ℕ) :
of_digits b L = L.foldr (λ x y, x + b * y) 0 :=
begin
induction L with d L ih,
{ refl, },
{ dsimp [of_digits], rw ih, },
end
lemma of_digits_eq_sum_map_with_index_aux (b : ℕ) (l : list ℕ) :
((list.range l.length).zip_with ((λ (i a : ℕ), a * b ^ i) ∘ succ) l).sum =
b * ((list.range l.length).zip_with (λ i a, a * b ^ i) l).sum :=
begin
suffices : (list.range l.length).zip_with (((λ (i a : ℕ), a * b ^ i) ∘ succ)) l =
(list.range l.length).zip_with (λ i a, b * (a * b ^ i)) l,
{ simp [this] },
congr,
ext,
simp [pow_succ],
ring
end
lemma of_digits_eq_sum_map_with_index (b : ℕ) (L : list ℕ):
of_digits b L = (L.map_with_index (λ i a, a * b ^ i)).sum :=
begin
rw [list.map_with_index_eq_enum_map, list.enum_eq_zip_range,
list.map_uncurry_zip_eq_zip_with, of_digits_eq_foldr],
induction L with hd tl hl,
{ simp },
{ simpa [list.range_succ_eq_map, list.zip_with_map_left, of_digits_eq_sum_map_with_index_aux]
using or.inl hl }
end
@[simp] lemma of_digits_singleton {b n : ℕ} : of_digits b [n] = n := by simp [of_digits]
@[simp] lemma of_digits_one_cons {α : Type*} [semiring α] (h : ℕ) (L : list ℕ) :
of_digits (1 : α) (h :: L) = h + of_digits 1 L :=
by simp [of_digits]
lemma of_digits_append {b : ℕ} {l1 l2 : list ℕ} :
of_digits b (l1 ++ l2) = of_digits b l1 + b^(l1.length) * of_digits b l2 :=
begin
induction l1 with hd tl IH,
{ simp [of_digits] },
{ rw [of_digits, list.cons_append, of_digits, IH, list.length_cons, pow_succ'],
ring }
end
@[norm_cast] lemma coe_of_digits (α : Type*) [semiring α] (b : ℕ) (L : list ℕ) :
((of_digits b L : ℕ) : α) = of_digits (b : α) L :=
begin
induction L with d L ih,
{ refl, },
{ dsimp [of_digits], push_cast, rw ih, }
end
@[norm_cast] lemma coe_int_of_digits (b : ℕ) (L : list ℕ) :
((of_digits b L : ℕ) : ℤ) = of_digits (b : ℤ) L :=
begin
induction L with d L ih,
{ refl, },
{ dsimp [of_digits], push_cast, rw ih, }
end
lemma digits_zero_of_eq_zero {b : ℕ} (h : 1 ≤ b) {L : list ℕ} (w : of_digits b L = 0) :
∀ l ∈ L, l = 0 :=
begin
induction L with d L ih,
{ intros l m,
cases m, },
{ intros l m,
dsimp [of_digits] at w,
rcases m with ⟨rfl⟩,
{ convert nat.eq_zero_of_add_eq_zero_right w, simp, },
{ exact ih ((nat.mul_right_inj h).mp (nat.eq_zero_of_add_eq_zero_left w)) _ m, }, }
end
lemma digits_of_digits
(b : ℕ) (h : 2 ≤ b) (L : list ℕ)
(w₁ : ∀ l ∈ L, l < b) (w₂ : ∀ (h : L ≠ []), L.last h ≠ 0) :
digits b (of_digits b L) = L :=
begin
induction L with d L ih,
{ dsimp [of_digits], simp },
{ dsimp [of_digits],
replace w₂ := w₂ (by simp),
rw digits_add b h,
{ rw ih,
{ simp, },
{ intros l m, apply w₁, exact list.mem_cons_of_mem _ m, },
{ intro h,
{ rw [list.last_cons _ h] at w₂,
convert w₂, }}},
{ convert w₁ d (list.mem_cons_self _ _), simp, },
{ by_cases h' : L = [],
{ rcases h' with rfl,
simp at w₂,
left,
apply nat.pos_of_ne_zero,
convert w₂, simp, },
{ right,
apply nat.pos_of_ne_zero,
contrapose! w₂,
apply digits_zero_of_eq_zero _ w₂,
{ rw list.last_cons _ h',
exact list.last_mem h', },
{ exact le_of_lt h, }, }, }, },
end
lemma of_digits_digits (b n : ℕ) : of_digits b (digits b n) = n :=
begin
cases b with b,
{ cases n with n,
{ refl, },
{ change of_digits 0 [n+1] = n+1,
dsimp [of_digits],
simp, } },
{ cases b with b,
{ induction n with n ih,
{ refl, },
{ simp only [ih, add_comm 1, of_digits_one_cons, nat.cast_id, digits_one_succ], } },
{ apply nat.strong_induction_on n _, clear n,
intros n h,
cases n,
{ rw digits_zero, refl, },
{ simp only [nat.succ_eq_add_one, digits_add_two_add_one],
dsimp [of_digits],
rw h _ (nat.div_lt_self' n b),
rw [nat.cast_id, nat.mod_add_div], }, }, },
end
lemma of_digits_one (L : list ℕ) : of_digits 1 L = L.sum :=
begin
induction L with d L ih,
{ refl, },
{ simp [of_digits, list.sum_cons, ih], }
end
/-!
### Properties
This section contains various lemmas of properties relating to `digits` and `of_digits`.
-/
lemma digits_eq_nil_iff_eq_zero {b n : ℕ} : digits b n = [] ↔ n = 0 :=
begin
split,
{ intro h,
have : of_digits b (digits b n) = of_digits b [], by rw h,
convert this,
rw of_digits_digits },
{ rintro rfl,
simp }
end
lemma digits_ne_nil_iff_ne_zero {b n : ℕ} : digits b n ≠ [] ↔ n ≠ 0 :=
not_congr digits_eq_nil_iff_eq_zero
private lemma digits_last_aux {b n : ℕ} (h : 2 ≤ b) (w : 0 < n) :
digits b n = ((n % b) :: digits b (n / b)) :=
begin
rcases b with _|_|b,
{ finish },
{ norm_num at h },
rcases n with _|n,
{ norm_num at w },
simp,
end
lemma digits_last {b m : ℕ} (h : 2 ≤ b) (hm : 0 < m) (p q) :
(digits b m).last p = (digits b (m/b)).last q :=
by { simp only [digits_last_aux h hm], rw list.last_cons }
lemma digits.injective (b : ℕ) : function.injective b.digits :=
function.left_inverse.injective (of_digits_digits b)
@[simp] lemma digits_inj_iff {b n m : ℕ} :
b.digits n = b.digits m ↔ n = m :=
(digits.injective b).eq_iff
lemma last_digit_ne_zero (b : ℕ) {m : ℕ} (hm : m ≠ 0) :
(digits b m).last (digits_ne_nil_iff_ne_zero.mpr hm) ≠ 0 :=
begin
rcases b with _|_|b,
{ cases m; finish },
{ cases m, { finish },
simp_rw [digits_one, list.last_repeat_succ 1 m],
norm_num },
revert hm,
apply nat.strong_induction_on m,
intros n IH hn,
have hnpos : 0 < n := nat.pos_of_ne_zero hn,
by_cases hnb : n < b + 2,
{ simp_rw [digits_of_lt b.succ.succ n hnpos hnb],
exact pos_iff_ne_zero.mp hnpos },
{ rw digits_last (show 2 ≤ b + 2, from dec_trivial) hnpos,
refine IH _ (nat.div_lt_self hnpos dec_trivial) _,
{ rw ←pos_iff_ne_zero,
exact nat.div_pos (le_of_not_lt hnb) dec_trivial } },
end
/-- The digits in the base b+2 expansion of n are all less than b+2 -/
lemma digits_lt_base' {b m : ℕ} : ∀ {d}, d ∈ digits (b+2) m → d < b+2 :=
begin
apply nat.strong_induction_on m,
intros n IH d hd,
cases n with n,
{ rw digits_zero at hd, cases hd }, -- base b+2 expansion of 0 has no digits
rw digits_add_two_add_one at hd,
cases hd,
{ rw hd, exact n.succ.mod_lt (by linarith) },
{ exact IH _ (nat.div_lt_self (nat.succ_pos _) (by linarith)) hd }
end
/-- The digits in the base b expansion of n are all less than b, if b ≥ 2 -/
lemma digits_lt_base {b m d : ℕ} (hb : 2 ≤ b) (hd : d ∈ digits b m) : d < b :=
begin
rcases b with _ | _ | b; try {linarith},
exact digits_lt_base' hd,
end
/-- an n-digit number in base b + 2 is less than (b + 2)^n -/
lemma of_digits_lt_base_pow_length' {b : ℕ} {l : list ℕ} (hl : ∀ x ∈ l, x < b+2) :
of_digits (b+2) l < (b+2)^(l.length) :=
begin
induction l with hd tl IH,
{ simp [of_digits], },
{ rw [of_digits, list.length_cons, pow_succ],
have : (of_digits (b + 2) tl + 1) * (b+2) ≤ (b + 2) ^ tl.length * (b+2) :=
mul_le_mul (IH (λ x hx, hl _ (list.mem_cons_of_mem _ hx)))
(by refl) dec_trivial (nat.zero_le _),
suffices : ↑hd < b + 2,
{ linarith },
norm_cast,
exact hl hd (list.mem_cons_self _ _) }
end
/-- an n-digit number in base b is less than b^n if b ≥ 2 -/
lemma of_digits_lt_base_pow_length {b : ℕ} {l : list ℕ} (hb : 2 ≤ b) (hl : ∀ x ∈ l, x < b) :
of_digits b l < b^l.length :=
begin
rcases b with _ | _ | b; try { linarith },
exact of_digits_lt_base_pow_length' hl,
end
/-- Any number m is less than (b+2)^(number of digits in the base b + 2 representation of m) -/
lemma lt_base_pow_length_digits' {b m : ℕ} : m < (b + 2) ^ (digits (b + 2) m).length :=
begin
convert of_digits_lt_base_pow_length' (λ _, digits_lt_base'),
rw of_digits_digits (b+2) m,
end
/-- Any number m is less than b^(number of digits in the base b representation of m) -/
lemma lt_base_pow_length_digits {b m : ℕ} (hb : 2 ≤ b) : m < b^(digits b m).length :=
begin
rcases b with _ | _ | b; try { linarith },
exact lt_base_pow_length_digits',
end
lemma of_digits_digits_append_digits {b m n : ℕ} :
of_digits b (digits b n ++ digits b m) = n + b ^ (digits b n).length * m:=
by rw [of_digits_append, of_digits_digits, of_digits_digits]
lemma digits_len_le_digits_len_succ (b n : ℕ) : (digits b n).length ≤ (digits b (n + 1)).length :=
begin
cases b,
{ -- base 0
cases n; simp },
{ cases b,
{ -- base 1
simp },
{ -- base >= 2
apply nat.strong_induction_on n,
clear n,
intros n IH,
cases n,
{ simp },
{ rw [digits_add_two_add_one, digits_add_two_add_one],
by_cases hdvd : (b.succ.succ) ∣ (n.succ+1),
{ rw [nat.succ_div_of_dvd hdvd, list.length_cons, list.length_cons, nat.succ_le_succ_iff],
apply IH,
exact nat.div_lt_self (by linarith) (by linarith) },
{ rw nat.succ_div_of_not_dvd hdvd,
refl } } } }
end
lemma le_digits_len_le (b n m : ℕ) (h : n ≤ m) : (digits b n).length ≤ (digits b m).length :=
monotone_of_monotone_nat (digits_len_le_digits_len_succ b) h
lemma pow_length_le_mul_of_digits {b : ℕ} {l : list ℕ} (hl : l ≠ []) (hl2 : l.last hl ≠ 0):
(b + 2) ^ l.length ≤ (b + 2) * of_digits (b+2) l :=
begin
rw [←list.init_append_last hl],
simp only [list.length_append, list.length, zero_add, list.length_init, of_digits_append,
list.length_init, of_digits_singleton, add_comm (l.length - 1), pow_add, pow_one],
apply nat.mul_le_mul_left,
refine le_trans _ (nat.le_add_left _ _),
have : 0 < l.last hl, { rwa [pos_iff_ne_zero] },
convert nat.mul_le_mul_left _ this, rw [mul_one]
end
/--
Any non-zero natural number `m` is greater than
(b+2)^((number of digits in the base (b+2) representation of m) - 1)
-/
lemma base_pow_length_digits_le' (b m : ℕ) (hm : m ≠ 0) :
(b + 2) ^ ((digits (b + 2) m).length) ≤ (b + 2) * m :=
begin
have : digits (b + 2) m ≠ [], from digits_ne_nil_iff_ne_zero.mpr hm,
convert pow_length_le_mul_of_digits this (last_digit_ne_zero _ hm),
rwa of_digits_digits,
end
/--
Any non-zero natural number `m` is greater than
b^((number of digits in the base b representation of m) - 1)
-/
lemma base_pow_length_digits_le (b m : ℕ) (hb : 2 ≤ b): m ≠ 0 → b ^ ((digits b m).length) ≤ b * m :=
begin
rcases b with _ | _ | b; try { linarith },
exact base_pow_length_digits_le' b m,
end
/-! ### Modular Arithmetic -/
-- This is really a theorem about polynomials.
lemma dvd_of_digits_sub_of_digits {α : Type*} [comm_ring α]
{a b k : α} (h : k ∣ a - b) (L : list ℕ) :
k ∣ of_digits a L - of_digits b L :=
begin
induction L with d L ih,
{ change k ∣ 0 - 0, simp, },
{ simp only [of_digits, add_sub_add_left_eq_sub],
exact dvd_mul_sub_mul h ih, }
end
lemma of_digits_modeq' (b b' : ℕ) (k : ℕ) (h : b ≡ b' [MOD k]) (L : list ℕ) :
of_digits b L ≡ of_digits b' L [MOD k] :=
begin
induction L with d L ih,
{ refl, },
{ dsimp [of_digits],
dsimp [nat.modeq] at *,
conv_lhs { rw [nat.add_mod, nat.mul_mod, h, ih], },
conv_rhs { rw [nat.add_mod, nat.mul_mod], }, }
end
lemma of_digits_modeq (b k : ℕ) (L : list ℕ) : of_digits b L ≡ of_digits (b % k) L [MOD k] :=
of_digits_modeq' b (b % k) k (nat.modeq.symm (nat.modeq.mod_modeq b k)) L
lemma of_digits_mod (b k : ℕ) (L : list ℕ) : of_digits b L % k = of_digits (b % k) L % k :=
of_digits_modeq b k L
lemma of_digits_zmodeq' (b b' : ℤ) (k : ℕ) (h : b ≡ b' [ZMOD k]) (L : list ℕ) :
of_digits b L ≡ of_digits b' L [ZMOD k] :=
begin
induction L with d L ih,
{ refl, },
{ dsimp [of_digits],
dsimp [int.modeq] at *,
conv_lhs { rw [int.add_mod, int.mul_mod, h, ih], },
conv_rhs { rw [int.add_mod, int.mul_mod], }, }
end
lemma of_digits_zmodeq (b : ℤ) (k : ℕ) (L : list ℕ) :
of_digits b L ≡ of_digits (b % k) L [ZMOD k] :=
of_digits_zmodeq' b (b % k) k (int.modeq.symm (int.modeq.mod_modeq b ↑k)) L
lemma of_digits_zmod (b : ℤ) (k : ℕ) (L : list ℕ) :
of_digits b L % k = of_digits (b % k) L % k :=
of_digits_zmodeq b k L
lemma modeq_digits_sum (b b' : ℕ) (h : b' % b = 1) (n : ℕ) :
n ≡ (digits b' n).sum [MOD b] :=
begin
rw ←of_digits_one,
conv { congr, skip, rw ←(of_digits_digits b' n) },
convert of_digits_modeq _ _ _,
exact h.symm,
end
lemma modeq_three_digits_sum (n : ℕ) : n ≡ (digits 10 n).sum [MOD 3] :=
modeq_digits_sum 3 10 (by norm_num) n
lemma modeq_nine_digits_sum (n : ℕ) : n ≡ (digits 10 n).sum [MOD 9] :=
modeq_digits_sum 9 10 (by norm_num) n
lemma zmodeq_of_digits_digits (b b' : ℕ) (c : ℤ) (h : b' ≡ c [ZMOD b]) (n : ℕ) :
n ≡ of_digits c (digits b' n) [ZMOD b] :=
begin
conv { congr, skip, rw ←(of_digits_digits b' n) },
rw coe_int_of_digits,
apply of_digits_zmodeq' _ _ _ h,
end
lemma of_digits_neg_one : Π (L : list ℕ),
of_digits (-1 : ℤ) L = (L.map (λ n : ℕ, (n : ℤ))).alternating_sum
| [] := rfl
| [n] := by simp [of_digits, list.alternating_sum]
| (a :: b :: t) :=
begin
simp only [of_digits, list.alternating_sum, list.map_cons, of_digits_neg_one t],
push_cast,
ring,
end
lemma modeq_eleven_digits_sum (n : ℕ) :
n ≡ ((digits 10 n).map (λ n : ℕ, (n : ℤ))).alternating_sum [ZMOD 11] :=
begin
have t := zmodeq_of_digits_digits 11 10 (-1 : ℤ) (by unfold int.modeq; norm_num) n,
rwa of_digits_neg_one at t
end
/-! ## Divisibility -/
lemma dvd_iff_dvd_digits_sum (b b' : ℕ) (h : b' % b = 1) (n : ℕ) :
b ∣ n ↔ b ∣ (digits b' n).sum :=
begin
rw ←of_digits_one,
conv_lhs { rw ←(of_digits_digits b' n) },
rw [nat.dvd_iff_mod_eq_zero, nat.dvd_iff_mod_eq_zero, of_digits_mod, h],
end
lemma three_dvd_iff (n : ℕ) : 3 ∣ n ↔ 3 ∣ (digits 10 n).sum :=
dvd_iff_dvd_digits_sum 3 10 (by norm_num) n
lemma nine_dvd_iff (n : ℕ) : 9 ∣ n ↔ 9 ∣ (digits 10 n).sum :=
dvd_iff_dvd_digits_sum 9 10 (by norm_num) n
lemma dvd_iff_dvd_of_digits (b b' : ℕ) (c : ℤ) (h : (b : ℤ) ∣ (b' : ℤ) - c) (n : ℕ) :
b ∣ n ↔ (b : ℤ) ∣ of_digits c (digits b' n) :=
begin
rw ←int.coe_nat_dvd,
exact dvd_iff_dvd_of_dvd_sub
(int.modeq.modeq_iff_dvd.1
(zmodeq_of_digits_digits b b' c (int.modeq.modeq_iff_dvd.2 h).symm _).symm),
end
lemma eleven_dvd_iff (n : ℕ) :
11 ∣ n ↔ (11 : ℤ) ∣ ((digits 10 n).map (λ n : ℕ, (n : ℤ))).alternating_sum :=
begin
have t := dvd_iff_dvd_of_digits 11 10 (-1 : ℤ) (by norm_num) n,
rw of_digits_neg_one at t,
exact t,
end
/-! ### `norm_digits` tactic -/
namespace norm_digits
theorem digits_succ
(b n m r l)
(e : r + b * m = n)
(hr : r < b)
(h : nat.digits b m = l ∧ 2 ≤ b ∧ 0 < m) :
nat.digits b n = r :: l ∧ 2 ≤ b ∧ 0 < n :=
begin
rcases h with ⟨h, b2, m0⟩,
have b0 : 0 < b := by linarith,
have n0 : 0 < n := by linarith [mul_pos b0 m0],
refine ⟨_, b2, n0⟩,
obtain ⟨rfl, rfl⟩ := (nat.div_mod_unique b0).2 ⟨e, hr⟩,
subst h, exact nat.digits_def' b2 n0,
end
theorem digits_one
(b n) (n0 : 0 < n) (nb : n < b) :
nat.digits b n = [n] ∧ 2 ≤ b ∧ 0 < n :=
begin
have b2 : 2 ≤ b := by linarith,
refine ⟨_, b2, n0⟩,
rw [nat.digits_def' b2 n0, nat.mod_eq_of_lt nb,
(nat.div_eq_zero_iff (by linarith : 0 < b)).2 nb, nat.digits_zero],
end
open tactic
/-- Helper function for the `norm_digits` tactic. -/
meta def eval_aux (eb : expr) (b : ℕ) :
expr → ℕ → instance_cache → tactic (instance_cache × expr × expr)
| en n ic := do
let m := n / b,
let r := n % b,
(ic, er) ← ic.of_nat r,
(ic, pr) ← norm_num.prove_lt_nat ic er eb,
if m = 0 then do
(_, pn0) ← norm_num.prove_pos ic en,
return (ic, `([%%en] : list nat), `(digits_one %%eb %%en %%pn0 %%pr))
else do
em ← expr.of_nat `(ℕ) m,
(_, pe) ← norm_num.derive `(%%er + %%eb * %%em : ℕ),
(ic, el, p) ← eval_aux em m ic,
return (ic, `(@list.cons ℕ %%er %%el),
`(digits_succ %%eb %%en %%em %%er %%el %%pe %%pr %%p))
/--
A tactic for normalizing expressions of the form `nat.digits a b = l` where
`a` and `b` are numerals.
```
example : nat.digits 10 123 = [3,2,1] := by norm_num
```
-/
@[norm_num] meta def eval : expr → tactic (expr × expr)
| `(nat.digits %%eb %%en) := do
b ← expr.to_nat eb,
n ← expr.to_nat en,
if n = 0 then return (`([] : list ℕ), `(nat.digits_zero %%eb))
else if b = 0 then do
ic ← mk_instance_cache `(ℕ),
(_, pn0) ← norm_num.prove_pos ic en,
return (`([%%en] : list ℕ), `(@nat.digits_zero_succ' %%en %%pn0))
else if b = 1 then do
ic ← mk_instance_cache `(ℕ),
(_, pn0) ← norm_num.prove_pos ic en,
s ← simp_lemmas.add_simp simp_lemmas.mk `list.repeat,
(rhs, p2, _) ← simplify s [] `(list.repeat 1 %%en),
p ← mk_eq_trans `(nat.digits_one %%en) p2,
return (rhs, p)
else do
ic ← mk_instance_cache `(ℕ),
(_, l, p) ← eval_aux eb b en n ic,
p ← mk_app ``and.left [p],
return (l, p)
| _ := failed
end norm_digits
end nat
|
8c367dd10ca75c6366de1709ba652043207f4526 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /tests/lean/run/602.lean | f71fe9e02d01203e4557762c95783939186fb065 | [
"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,193 | lean | class Semiring (α : Type) where add : α → α → α
class Ring (α : Type) where add : α → α → α
class AddCommMonoid (α : Type) where
class AddCommGroup (α : Type) where
class Module (α β : Type) [Semiring α] [AddCommMonoid β] where
class NormedField (α : Type) where
add : α → α → α
add_comm : ∀ (x y : α), @Add.add _ ⟨add⟩ x y = @Add.add _ ⟨add⟩ y x
class SemiNormedGroup (α : Type) where
class SemiNormedSpace (α β : Type) [NormedField α] [SemiNormedGroup β] where
instance SemiNormedGroup.toAddCommMonoid [SemiNormedGroup α] : AddCommMonoid α := {}
instance Ring.toSemiring [instR : Ring α] : Semiring α := { add := instR.add }
instance NormedField.toRing [instNF : NormedField α] : Ring α := { add := instNF.add }
-- @[inferTCGoalsRL]
instance SemiNormedSpace.toModule [NormedField α] [SemiNormedGroup β] [SemiNormedSpace α β] : Module α β := {}
opaque R : Type := Unit
opaque foo (a b : R) : R := a
instance R.NormedField : NormedField R := { add := foo, add_comm := sorry }
instance R.Ring : Ring R := { add := foo }
variable {E : Type} [instSNG : SemiNormedGroup E] [instSNS : SemiNormedSpace R E]
#synth Module R E
|
bec0501c7ea91ab63aa49e6b76eab192fc8da1d6 | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /stage0/src/Lean/Elab/Tactic/ElabTerm.lean | bb9d32462d139506b124324bf6eb0c5720712ea4 | [
"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 | 14,306 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Tactic.Constructor
import Lean.Meta.Tactic.Assert
import Lean.Meta.Tactic.Clear
import Lean.Meta.Tactic.Rename
import Lean.Elab.Tactic.Basic
import Lean.Elab.SyntheticMVars
namespace Lean.Elab.Tactic
open Meta
/-! # `elabTerm` for Tactics and basic tactics that use it. -/
/-- Elaborate `stx` in the current `MVarContext`. If given, the `expectedType` will be used to help
elaboration but not enforced (use `elabTermEnsuringType` to enforce an expected type). -/
def elabTerm (stx : Syntax) (expectedType? : Option Expr) (mayPostpone := false) : TacticM Expr := do
/- If error recovery is disabled, we disable `Term.withoutErrToSorry` -/
if (← read).recover then
go
else
Term.withoutErrToSorry go
where
go : TermElabM Expr :=
withRef stx do -- <|
let e ← Term.elabTerm stx expectedType?
Term.synthesizeSyntheticMVars mayPostpone
instantiateMVars e
/-- Elaborate `stx` in the current `MVarContext`. If given, the `expectedType` will be used to help
elaboration and then a `TypeMismatchError` will be thrown if the elaborated type doesn't match. -/
def elabTermEnsuringType (stx : Syntax) (expectedType? : Option Expr) (mayPostpone := false) : TacticM Expr := do
let e ← elabTerm stx expectedType? mayPostpone
-- We do use `Term.ensureExpectedType` because we don't want coercions being inserted here.
match expectedType? with
| none => return e
| some expectedType =>
let eType ← inferType e
-- We allow synthetic opaque metavars to be assigned in the following step since the `isDefEq` is not really
-- part of the elaboration, but part of the tactic. See issue #492
unless (← withAssignableSyntheticOpaque <| isDefEq eType expectedType) do
Term.throwTypeMismatchError none expectedType eType e
return e
/-- Try to close main goal using `x target`, where `target` is the type of the main goal. -/
def closeMainGoalUsing (x : Expr → TacticM Expr) (checkUnassigned := true) : TacticM Unit :=
withMainContext do
closeMainGoal (checkUnassigned := checkUnassigned) (← x (← getMainTarget))
def logUnassignedAndAbort (mvarIds : Array MVarId) : TacticM Unit := do
if (← Term.logUnassignedUsingErrorInfos mvarIds) then
throwAbortTactic
def filterOldMVars (mvarIds : Array MVarId) (mvarCounterSaved : Nat) : MetaM (Array MVarId) := do
let mctx ← getMCtx
return mvarIds.filter fun mvarId => (mctx.getDecl mvarId |>.index) >= mvarCounterSaved
@[builtinTactic «exact»] def evalExact : Tactic := fun stx =>
match stx with
| `(tactic| exact $e) => closeMainGoalUsing (checkUnassigned := false) fun type => do
let mvarCounterSaved := (← getMCtx).mvarCounter
let r ← elabTermEnsuringType e type
logUnassignedAndAbort (← filterOldMVars (← getMVars r) mvarCounterSaved)
return r
| _ => throwUnsupportedSyntax
/--
Execute `k`, and collect new "holes" in the resulting expression.
-/
def withCollectingNewGoalsFrom (k : TacticM Expr) (tagSuffix : Name) (allowNaturalHoles := false) : TacticM (Expr × List MVarId) := do
let mvarCounterSaved := (← getMCtx).mvarCounter
let val ← k
let newMVarIds ← getMVarsNoDelayed val
/- ignore let-rec auxiliary variables, they are synthesized automatically later -/
let newMVarIds ← newMVarIds.filterM fun mvarId => return !(← Term.isLetRecAuxMVar mvarId)
let newMVarIds ← if allowNaturalHoles then
pure newMVarIds.toList
else
let naturalMVarIds ← newMVarIds.filterM fun mvarId => return (← mvarId.getKind).isNatural
let syntheticMVarIds ← newMVarIds.filterM fun mvarId => return !(← mvarId.getKind).isNatural
let naturalMVarIds ← filterOldMVars naturalMVarIds mvarCounterSaved
logUnassignedAndAbort naturalMVarIds
pure syntheticMVarIds.toList
tagUntaggedGoals (← getMainTag) tagSuffix newMVarIds
return (val, newMVarIds)
def elabTermWithHoles (stx : Syntax) (expectedType? : Option Expr) (tagSuffix : Name) (allowNaturalHoles := false) : TacticM (Expr × List MVarId) := do
withCollectingNewGoalsFrom (elabTermEnsuringType stx expectedType?) tagSuffix allowNaturalHoles
/-- If `allowNaturalHoles == true`, then we allow the resultant expression to contain unassigned "natural" metavariables.
Recall that "natutal" metavariables are created for explicit holes `_` and implicit arguments. They are meant to be
filled by typing constraints.
"Synthetic" metavariables are meant to be filled by tactics and are usually created using the synthetic hole notation `?<hole-name>`. -/
def refineCore (stx : Syntax) (tagSuffix : Name) (allowNaturalHoles : Bool) : TacticM Unit := do
withMainContext do
let (val, mvarIds') ← elabTermWithHoles stx (← getMainTarget) tagSuffix allowNaturalHoles
let mvarId ← getMainGoal
let val ← instantiateMVars val
unless val == mkMVar mvarId do
if val.findMVar? (· == mvarId) matches some _ then
throwError "'refine' tactic failed, value{indentExpr val}\ndepends on the main goal metavariable '{mkMVar mvarId}'"
mvarId.assign val
replaceMainGoal mvarIds'
@[builtinTactic «refine»] def evalRefine : Tactic := fun stx =>
match stx with
| `(tactic| refine $e) => refineCore e `refine (allowNaturalHoles := false)
| _ => throwUnsupportedSyntax
@[builtinTactic «refine'»] def evalRefine' : Tactic := fun stx =>
match stx with
| `(tactic| refine' $e) => refineCore e `refine' (allowNaturalHoles := true)
| _ => throwUnsupportedSyntax
@[builtinTactic «specialize»] def evalSpecialize : Tactic := fun stx => withMainContext do
match stx with
| `(tactic| specialize $e:term) =>
let (e, mvarIds') ← elabTermWithHoles e none `specialize (allowNaturalHoles := true)
let h := e.getAppFn
if h.isFVar then
let localDecl ← h.fvarId!.getDecl
let mvarId ← (← getMainGoal).assert localDecl.userName (← inferType e).headBeta e
let (_, mvarId) ← mvarId.intro1P
let mvarId ← mvarId.tryClear h.fvarId!
replaceMainGoal (mvarId :: mvarIds')
else
throwError "'specialize' requires a term of the form `h x_1 .. x_n` where `h` appears in the local context"
| _ => throwUnsupportedSyntax
/--
Given a tactic
```
apply f
```
we want the `apply` tactic to create all metavariables. The following
definition will return `@f` for `f`. That is, it will **not** create
metavariables for implicit arguments.
A similar method is also used in Lean 3.
This method is useful when applying lemmas such as:
```
theorem infLeRight {s t : Set α} : s ⊓ t ≤ t
```
where `s ≤ t` here is defined as
```
∀ {x : α}, x ∈ s → x ∈ t
```
-/
def elabTermForApply (stx : Syntax) (mayPostpone := true) : TacticM Expr := do
if stx.isIdent then
match (← Term.resolveId? stx (withInfo := true)) with
| some e => return e
| _ => pure ()
/-
By disabling the "error recovery" (and consequently "error to sorry") feature,
we make sure an `apply e` fails without logging an error message.
The motivation is that `apply` is frequently used when writing tactic such as
```
cases h <;> intro h' <;> first | apply t[h'] | ....
```
Here the type of `h'` may be different in each case, and the term `t[h']` containing `h'` may even fail to
be elaborated in some cases. When this happens we want the tactic to fail without reporting any error to the user,
and the next tactic is tried.
A drawback of disabling "error to sorry" is that there is no error recovery after the error is thrown, and features such
as auto-completion are affected.
By disabling "error to sorry", we also limit ourselves to at most one error at `t[h']`.
By disabling "error to sorry", we also miss the opportunity to catch mistakes is tactic code such as
`first | apply nonsensical-term | assumption`
This should not be a big problem for the `apply` tactic since we usually provide small terms there.
Note that we do not disable "error to sorry" at `exact` and `refine` since they are often used to elaborate big terms,
and we do want error recovery there, and we want to see the error messages.
We should probably provide options for allowing users to control this behavior.
see issue #1037
More complex solution:
- We do not disable "error to sorry"
- We elaborate term and check whether errors were produced
- If there are other tactic braches and there are errors, we remove the errors from the log, and throw a new error to force the tactic to backtrack.
-/
withoutRecover <| elabTerm stx none mayPostpone
def evalApplyLikeTactic (tac : MVarId → Expr → MetaM (List MVarId)) (e : Syntax) : TacticM Unit := do
withMainContext do
let val ← elabTermForApply e
let mvarIds' ← tac (← getMainGoal) val
Term.synthesizeSyntheticMVarsNoPostponing
replaceMainGoal mvarIds'
def getFVarId (id : Syntax) : TacticM FVarId := withRef id do
-- use apply-like elaboration to suppress insertion of implicit arguments
let e ← withMainContext do
elabTermForApply id (mayPostpone := false)
match e with
| Expr.fvar fvarId => return fvarId
| _ => throwError "unexpected term '{e}'; expected single reference to variable"
def getFVarIds (ids : Array Syntax) : TacticM (Array FVarId) := do
withMainContext do ids.mapM getFVarId
@[builtinTactic Lean.Parser.Tactic.apply] def evalApply : Tactic := fun stx =>
match stx with
| `(tactic| apply $e) => evalApplyLikeTactic (·.apply) e
| _ => throwUnsupportedSyntax
@[builtinTactic Lean.Parser.Tactic.constructor] def evalConstructor : Tactic := fun _ =>
withMainContext do
let mvarIds' ← (← getMainGoal).constructor
Term.synthesizeSyntheticMVarsNoPostponing
replaceMainGoal mvarIds'
@[builtinTactic Lean.Parser.Tactic.withReducible] def evalWithReducible : Tactic := fun stx =>
withReducible <| evalTactic stx[1]
@[builtinTactic Lean.Parser.Tactic.withReducibleAndInstances] def evalWithReducibleAndInstances : Tactic := fun stx =>
withReducibleAndInstances <| evalTactic stx[1]
@[builtinTactic Lean.Parser.Tactic.withUnfoldingAll] def evalWithUnfoldingAll : Tactic := fun stx =>
withTransparency TransparencyMode.all <| evalTactic stx[1]
/--
Elaborate `stx`. If it a free variable, return it. Otherwise, assert it, and return the free variable.
Note that, the main goal is updated when `Meta.assert` is used in the second case. -/
def elabAsFVar (stx : Syntax) (userName? : Option Name := none) : TacticM FVarId :=
withMainContext do
let e ← elabTerm stx none
match e with
| .fvar fvarId => pure fvarId
| _ =>
let type ← inferType e
let intro (userName : Name) (preserveBinderNames : Bool) : TacticM FVarId := do
let mvarId ← getMainGoal
let (fvarId, mvarId) ← liftMetaM do
let mvarId ← mvarId.assert userName type e
Meta.intro1Core mvarId preserveBinderNames
replaceMainGoal [mvarId]
return fvarId
match userName? with
| none => intro `h false
| some userName => intro userName true
@[builtinTactic Lean.Parser.Tactic.rename] def evalRename : Tactic := fun stx =>
match stx with
| `(tactic| rename $typeStx:term => $h:ident) => do
withMainContext do
/- Remark: we also use `withoutRecover` to make sure `elabTerm` does not succeed
using `sorryAx`, and we get `"failed to find ..."` which will not be logged because
it contains synthetic sorry's -/
let fvarId ← withoutModifyingState <| withNewMCtxDepth <| withoutRecover do
let type ← elabTerm typeStx none (mayPostpone := true)
let fvarId? ← (← getLCtx).findDeclRevM? fun localDecl => do
if (← isDefEq type localDecl.type) then return localDecl.fvarId else return none
match fvarId? with
| none => throwError "failed to find a hypothesis with type{indentExpr type}"
| some fvarId => return fvarId
replaceMainGoal [← (← getMainGoal).rename fvarId h.getId]
| _ => throwUnsupportedSyntax
/--
Make sure `expectedType` does not contain free and metavariables.
It applies zeta-reduction to eliminate let-free-vars.
-/
private def preprocessPropToDecide (expectedType : Expr) : TermElabM Expr := do
let mut expectedType ← instantiateMVars expectedType
if expectedType.hasFVar then
expectedType ← zetaReduce expectedType
if expectedType.hasFVar || expectedType.hasMVar then
throwError "expected type must not contain free or meta variables{indentExpr expectedType}"
return expectedType
@[builtinTactic Lean.Parser.Tactic.decide] def evalDecide : Tactic := fun _ =>
closeMainGoalUsing fun expectedType => do
let expectedType ← preprocessPropToDecide expectedType
let d ← mkDecide expectedType
let d ← instantiateMVars d
let r ← withDefault <| whnf d
unless r.isConstOf ``true do
throwError "failed to reduce to 'true'{indentExpr r}"
let s := d.appArg! -- get instance from `d`
let rflPrf ← mkEqRefl (toExpr true)
return mkApp3 (Lean.mkConst ``of_decide_eq_true) expectedType s rflPrf
private def mkNativeAuxDecl (baseName : Name) (type value : Expr) : TermElabM Name := do
let auxName ← Term.mkAuxName baseName
let decl := Declaration.defnDecl {
name := auxName, levelParams := [], type, value
hints := .abbrev
safety := .safe
}
addDecl decl
compileDecl decl
pure auxName
@[builtinTactic Lean.Parser.Tactic.nativeDecide] def evalNativeDecide : Tactic := fun _ =>
closeMainGoalUsing fun expectedType => do
let expectedType ← preprocessPropToDecide expectedType
let d ← mkDecide expectedType
let auxDeclName ← mkNativeAuxDecl `_nativeDecide (Lean.mkConst `Bool) d
let rflPrf ← mkEqRefl (toExpr true)
let s := d.appArg! -- get instance from `d`
return mkApp3 (Lean.mkConst ``of_decide_eq_true) expectedType s <| mkApp3 (Lean.mkConst ``Lean.ofReduceBool) (Lean.mkConst auxDeclName) (toExpr true) rflPrf
end Lean.Elab.Tactic
|
2eaad1608f7d0131d495ec303d01897e17a51767 | bb31430994044506fa42fd667e2d556327e18dfe | /src/group_theory/perm/cycle/type.lean | 1aac2fe826279acf52474c86adec4e3ecd2cbd73 | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 28,022 | lean | /-
Copyright (c) 2020 Thomas Browning. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Thomas Browning
-/
import algebra.gcd_monoid.multiset
import combinatorics.partition
import data.list.rotate
import group_theory.perm.cycle.basic
import ring_theory.int.basic
import tactic.linarith
/-!
# Cycle Types
In this file we define the cycle type of a permutation.
## Main definitions
- `σ.cycle_type` where `σ` is a permutation of a `fintype`
- `σ.partition` where `σ` is a permutation of a `fintype`
## Main results
- `sum_cycle_type` : The sum of `σ.cycle_type` equals `σ.support.card`
- `lcm_cycle_type` : The lcm of `σ.cycle_type` equals `order_of σ`
- `is_conj_iff_cycle_type_eq` : Two permutations are conjugate if and only if they have the same
cycle type.
- `exists_prime_order_of_dvd_card`: For every prime `p` dividing the order of a finite group `G`
there exists an element of order `p` in `G`. This is known as Cauchy's theorem.
-/
namespace equiv.perm
open equiv list multiset
variables {α : Type*} [fintype α]
section cycle_type
variables [decidable_eq α]
/-- The cycle type of a permutation -/
def cycle_type (σ : perm α) : multiset ℕ :=
σ.cycle_factors_finset.1.map (finset.card ∘ support)
lemma cycle_type_def (σ : perm α) :
σ.cycle_type = σ.cycle_factors_finset.1.map (finset.card ∘ support) := rfl
lemma cycle_type_eq' {σ : perm α} (s : finset (perm α))
(h1 : ∀ f : perm α, f ∈ s → f.is_cycle) (h2 : (s : set (perm α)).pairwise disjoint)
(h0 : s.noncomm_prod id (h2.imp $ λ _ _, disjoint.commute) = σ) :
σ.cycle_type = s.1.map (finset.card ∘ support) :=
begin
rw cycle_type_def,
congr,
rw cycle_factors_finset_eq_finset,
exact ⟨h1, h2, h0⟩
end
lemma cycle_type_eq {σ : perm α} (l : list (perm α)) (h0 : l.prod = σ)
(h1 : ∀ σ : perm α, σ ∈ l → σ.is_cycle) (h2 : l.pairwise disjoint) :
σ.cycle_type = l.map (finset.card ∘ support) :=
begin
have hl : l.nodup := nodup_of_pairwise_disjoint_cycles h1 h2,
rw cycle_type_eq' l.to_finset,
{ simp [list.dedup_eq_self.mpr hl] },
{ simpa using h1 },
{ simpa [hl] using h0 },
{ simpa [list.dedup_eq_self.mpr hl] using h2.forall disjoint.symmetric }
end
lemma cycle_type_one : (1 : perm α).cycle_type = 0 :=
cycle_type_eq [] rfl (λ _, false.elim) pairwise.nil
lemma cycle_type_eq_zero {σ : perm α} : σ.cycle_type = 0 ↔ σ = 1 :=
by simp [cycle_type_def, cycle_factors_finset_eq_empty_iff]
lemma card_cycle_type_eq_zero {σ : perm α} : σ.cycle_type.card = 0 ↔ σ = 1 :=
by rw [card_eq_zero, cycle_type_eq_zero]
lemma two_le_of_mem_cycle_type {σ : perm α} {n : ℕ} (h : n ∈ σ.cycle_type) : 2 ≤ n :=
begin
simp only [cycle_type_def, ←finset.mem_def, function.comp_app, multiset.mem_map,
mem_cycle_factors_finset_iff] at h,
obtain ⟨_, ⟨hc, -⟩, rfl⟩ := h,
exact hc.two_le_card_support
end
lemma one_lt_of_mem_cycle_type {σ : perm α} {n : ℕ} (h : n ∈ σ.cycle_type) : 1 < n :=
two_le_of_mem_cycle_type h
lemma is_cycle.cycle_type {σ : perm α} (hσ : is_cycle σ) : σ.cycle_type = [σ.support.card] :=
cycle_type_eq [σ] (mul_one σ) (λ τ hτ, (congr_arg is_cycle (list.mem_singleton.mp hτ)).mpr hσ)
(pairwise_singleton disjoint σ)
lemma card_cycle_type_eq_one {σ : perm α} : σ.cycle_type.card = 1 ↔ σ.is_cycle :=
begin
rw card_eq_one,
simp_rw [cycle_type_def, multiset.map_eq_singleton, ←finset.singleton_val,
finset.val_inj, cycle_factors_finset_eq_singleton_iff],
split,
{ rintro ⟨_, _, ⟨h, -⟩, -⟩,
exact h },
{ intro h,
use [σ.support.card, σ],
simp [h] }
end
lemma disjoint.cycle_type {σ τ : perm α} (h : disjoint σ τ) :
(σ * τ).cycle_type = σ.cycle_type + τ.cycle_type :=
begin
rw [cycle_type_def, cycle_type_def, cycle_type_def, h.cycle_factors_finset_mul_eq_union,
←multiset.map_add, finset.union_val, multiset.add_eq_union_iff_disjoint.mpr _],
exact finset.disjoint_val.2 h.disjoint_cycle_factors_finset
end
lemma cycle_type_inv (σ : perm α) : σ⁻¹.cycle_type = σ.cycle_type :=
cycle_induction_on (λ τ : perm α, τ⁻¹.cycle_type = τ.cycle_type) σ rfl
(λ σ hσ, by rw [hσ.cycle_type, hσ.inv.cycle_type, support_inv])
(λ σ τ hστ hc hσ hτ, by rw [mul_inv_rev, hστ.cycle_type, ←hσ, ←hτ, add_comm,
disjoint.cycle_type (λ x, or.imp (λ h : τ x = x, inv_eq_iff_eq.mpr h.symm)
(λ h : σ x = x, inv_eq_iff_eq.mpr h.symm) (hστ x).symm)])
lemma cycle_type_conj {σ τ : perm α} : (τ * σ * τ⁻¹).cycle_type = σ.cycle_type :=
begin
revert τ,
apply cycle_induction_on _ σ,
{ intro,
simp },
{ intros σ hσ τ,
rw [hσ.cycle_type, hσ.conj.cycle_type, card_support_conj] },
{ intros σ τ hd hc hσ hτ π,
rw [← conj_mul, hd.cycle_type, disjoint.cycle_type, hσ, hτ],
intro a,
apply (hd (π⁻¹ a)).imp _ _;
{ intro h, rw [perm.mul_apply, perm.mul_apply, h, apply_inv_self] } }
end
lemma sum_cycle_type (σ : perm α) : σ.cycle_type.sum = σ.support.card :=
cycle_induction_on (λ τ : perm α, τ.cycle_type.sum = τ.support.card) σ
(by rw [cycle_type_one, sum_zero, support_one, finset.card_empty])
(λ σ hσ, by rw [hσ.cycle_type, coe_sum, list.sum_singleton])
(λ σ τ hστ hc hσ hτ, by rw [hστ.cycle_type, sum_add, hσ, hτ, hστ.card_support_mul])
lemma sign_of_cycle_type' (σ : perm α) :
sign σ = (σ.cycle_type.map (λ n, -(-1 : ℤˣ) ^ n)).prod :=
cycle_induction_on (λ τ : perm α, sign τ = (τ.cycle_type.map (λ n, -(-1 : ℤˣ) ^ n)).prod) σ
(by rw [sign_one, cycle_type_one, multiset.map_zero, prod_zero])
(λ σ hσ, by rw [hσ.sign, hσ.cycle_type, coe_map, coe_prod,
list.map_singleton, list.prod_singleton])
(λ σ τ hστ hc hσ hτ, by rw [sign_mul, hσ, hτ, hστ.cycle_type, multiset.map_add, prod_add])
lemma sign_of_cycle_type (f : perm α) :
sign f = (-1 : ℤˣ)^(f.cycle_type.sum + f.cycle_type.card) :=
cycle_induction_on
(λ f : perm α, sign f = (-1 : ℤˣ)^(f.cycle_type.sum + f.cycle_type.card))
f
( -- base_one
by rw [equiv.perm.cycle_type_one, sign_one, multiset.sum_zero, multiset.card_zero, pow_zero] )
( -- base_cycles
λ f hf,
by rw [equiv.perm.is_cycle.cycle_type hf, hf.sign,
coe_sum, list.sum_cons, sum_nil, add_zero, coe_card, length_singleton,
pow_add, pow_one, mul_comm, neg_mul, one_mul] )
( -- induction_disjoint
λ f g hfg hf Pf Pg,
by rw [equiv.perm.disjoint.cycle_type hfg,
multiset.sum_add, multiset.card_add,← add_assoc,
add_comm f.cycle_type.sum g.cycle_type.sum,
add_assoc g.cycle_type.sum _ _,
add_comm g.cycle_type.sum _,
add_assoc, pow_add,
← Pf, ← Pg,
equiv.perm.sign_mul])
lemma lcm_cycle_type (σ : perm α) : σ.cycle_type.lcm = order_of σ :=
cycle_induction_on (λ τ : perm α, τ.cycle_type.lcm = order_of τ) σ
(by rw [cycle_type_one, lcm_zero, order_of_one])
(λ σ hσ, by rw [hσ.cycle_type, coe_singleton, lcm_singleton, hσ.order_of,
normalize_eq])
(λ σ τ hστ hc hσ hτ, by rw [hστ.cycle_type, lcm_add, lcm_eq_nat_lcm, hστ.order_of, hσ, hτ])
lemma dvd_of_mem_cycle_type {σ : perm α} {n : ℕ} (h : n ∈ σ.cycle_type) : n ∣ order_of σ :=
begin
rw ← lcm_cycle_type,
exact dvd_lcm h,
end
lemma order_of_cycle_of_dvd_order_of (f : perm α) (x : α) :
order_of (cycle_of f x) ∣ order_of f :=
begin
by_cases hx : f x = x,
{ rw ←cycle_of_eq_one_iff at hx,
simp [hx] },
{ refine dvd_of_mem_cycle_type _,
rw [cycle_type, multiset.mem_map],
refine ⟨f.cycle_of x, _, _⟩,
{ rwa [←finset.mem_def, cycle_of_mem_cycle_factors_finset_iff, mem_support] },
{ simp [(is_cycle_cycle_of _ hx).order_of] } }
end
lemma two_dvd_card_support {σ : perm α} (hσ : σ ^ 2 = 1) : 2 ∣ σ.support.card :=
(congr_arg (has_dvd.dvd 2) σ.sum_cycle_type).mp
(multiset.dvd_sum (λ n hn, by rw le_antisymm (nat.le_of_dvd zero_lt_two $
(dvd_of_mem_cycle_type hn).trans $ order_of_dvd_of_pow_eq_one hσ) (two_le_of_mem_cycle_type hn)))
lemma cycle_type_prime_order {σ : perm α} (hσ : (order_of σ).prime) :
∃ n : ℕ, σ.cycle_type = repeat (order_of σ) (n + 1) :=
begin
rw eq_repeat_of_mem (λ n hn, or_iff_not_imp_left.mp
(hσ.eq_one_or_self_of_dvd n (dvd_of_mem_cycle_type hn)) (one_lt_of_mem_cycle_type hn).ne'),
use σ.cycle_type.card - 1,
rw tsub_add_cancel_of_le,
rw [nat.succ_le_iff, pos_iff_ne_zero, ne, card_cycle_type_eq_zero],
intro H,
rw [H, order_of_one] at hσ,
exact hσ.ne_one rfl,
end
lemma is_cycle_of_prime_order {σ : perm α} (h1 : (order_of σ).prime)
(h2 : σ.support.card < 2 * (order_of σ)) : σ.is_cycle :=
begin
obtain ⟨n, hn⟩ := cycle_type_prime_order h1,
rw [←σ.sum_cycle_type, hn, multiset.sum_repeat, nsmul_eq_mul, nat.cast_id, mul_lt_mul_right
(order_of_pos σ), nat.succ_lt_succ_iff, nat.lt_succ_iff, le_zero_iff] at h2,
rw [←card_cycle_type_eq_one, hn, card_repeat, h2],
end
lemma cycle_type_le_of_mem_cycle_factors_finset {f g : perm α}
(hf : f ∈ g.cycle_factors_finset) :
f.cycle_type ≤ g.cycle_type :=
begin
rw mem_cycle_factors_finset_iff at hf,
rw [cycle_type_def, cycle_type_def, hf.left.cycle_factors_finset_eq_singleton],
refine map_le_map _,
simpa [←finset.mem_def, mem_cycle_factors_finset_iff] using hf
end
lemma cycle_type_mul_mem_cycle_factors_finset_eq_sub {f g : perm α}
(hf : f ∈ g.cycle_factors_finset) :
(g * f⁻¹).cycle_type = g.cycle_type - f.cycle_type :=
begin
suffices : (g * f⁻¹).cycle_type + f.cycle_type = g.cycle_type - f.cycle_type + f.cycle_type,
{ rw tsub_add_cancel_of_le (cycle_type_le_of_mem_cycle_factors_finset hf) at this,
simp [←this] },
simp [←(disjoint_mul_inv_of_mem_cycle_factors_finset hf).cycle_type,
tsub_add_cancel_of_le (cycle_type_le_of_mem_cycle_factors_finset hf)]
end
theorem is_conj_of_cycle_type_eq {σ τ : perm α} (h : cycle_type σ = cycle_type τ) : is_conj σ τ :=
begin
revert τ,
apply cycle_induction_on _ σ,
{ intros τ h,
rw [cycle_type_one, eq_comm, cycle_type_eq_zero] at h,
rw h },
{ intros σ hσ τ hστ,
have hτ := card_cycle_type_eq_one.2 hσ,
rw [hστ, card_cycle_type_eq_one] at hτ,
apply hσ.is_conj hτ,
rw [hσ.cycle_type, hτ.cycle_type, coe_eq_coe, singleton_perm] at hστ,
simp only [and_true, eq_self_iff_true] at hστ,
exact hστ },
{ intros σ τ hστ hσ h1 h2 π hπ,
rw [hστ.cycle_type] at hπ,
{ have h : σ.support.card ∈ map (finset.card ∘ perm.support) π.cycle_factors_finset.val,
{ simp [←cycle_type_def, ←hπ, hσ.cycle_type] },
obtain ⟨σ', hσ'l, hσ'⟩ := multiset.mem_map.mp h,
have key : is_conj (σ' * (π * σ'⁻¹)) π,
{ rw is_conj_iff,
use σ'⁻¹,
simp [mul_assoc] },
refine is_conj.trans _ key,
have hs : σ.cycle_type = σ'.cycle_type,
{ rw [←finset.mem_def, mem_cycle_factors_finset_iff] at hσ'l,
rw [hσ.cycle_type, ←hσ', hσ'l.left.cycle_type] },
refine hστ.is_conj_mul (h1 hs) (h2 _) _,
{ rw [cycle_type_mul_mem_cycle_factors_finset_eq_sub, ←hπ, add_comm, hs,
add_tsub_cancel_right],
rwa finset.mem_def },
{ exact (disjoint_mul_inv_of_mem_cycle_factors_finset hσ'l).symm } } }
end
theorem is_conj_iff_cycle_type_eq {σ τ : perm α} :
is_conj σ τ ↔ σ.cycle_type = τ.cycle_type :=
⟨λ h, begin
obtain ⟨π, rfl⟩ := is_conj_iff.1 h,
rw cycle_type_conj,
end, is_conj_of_cycle_type_eq⟩
@[simp] lemma cycle_type_extend_domain {β : Type*} [fintype β] [decidable_eq β]
{p : β → Prop} [decidable_pred p] (f : α ≃ subtype p) {g : perm α} :
cycle_type (g.extend_domain f) = cycle_type g :=
begin
apply cycle_induction_on _ g,
{ rw [extend_domain_one, cycle_type_one, cycle_type_one] },
{ intros σ hσ,
rw [(hσ.extend_domain f).cycle_type, hσ.cycle_type, card_support_extend_domain] },
{ intros σ τ hd hc hσ hτ,
rw [hd.cycle_type, ← extend_domain_mul, (hd.extend_domain f).cycle_type, hσ, hτ] }
end
lemma cycle_type_of_subtype {p : α → Prop} [decidable_pred p] {g : perm (subtype p)}:
cycle_type (g.of_subtype) = cycle_type g := cycle_type_extend_domain (equiv.refl (subtype p))
lemma mem_cycle_type_iff {n : ℕ} {σ : perm α} :
n ∈ cycle_type σ ↔ ∃ c τ : perm α, σ = c * τ ∧ disjoint c τ ∧ is_cycle c ∧ c.support.card = n :=
begin
split,
{ intro h,
obtain ⟨l, rfl, hlc, hld⟩ := trunc_cycle_factors σ,
rw cycle_type_eq _ rfl hlc hld at h,
obtain ⟨c, cl, rfl⟩ := list.exists_of_mem_map h,
rw (list.perm_cons_erase cl).pairwise_iff (λ _ _ hd, _) at hld,
swap, { exact hd.symm },
refine ⟨c, (l.erase c).prod, _, _, hlc _ cl, rfl⟩,
{ rw [← list.prod_cons,
(list.perm_cons_erase cl).symm.prod_eq' (hld.imp (λ _ _, disjoint.commute))] },
{ exact disjoint_prod_right _ (λ g, list.rel_of_pairwise_cons hld) } },
{ rintros ⟨c, t, rfl, hd, hc, rfl⟩,
simp [hd.cycle_type, hc.cycle_type] }
end
lemma le_card_support_of_mem_cycle_type {n : ℕ} {σ : perm α} (h : n ∈ cycle_type σ) :
n ≤ σ.support.card :=
(le_sum_of_mem h).trans (le_of_eq σ.sum_cycle_type)
lemma cycle_type_of_card_le_mem_cycle_type_add_two {n : ℕ} {g : perm α}
(hn2 : fintype.card α < n + 2) (hng : n ∈ g.cycle_type) :
g.cycle_type = {n} :=
begin
obtain ⟨c, g', rfl, hd, hc, rfl⟩ := mem_cycle_type_iff.1 hng,
by_cases g'1 : g' = 1,
{ rw [hd.cycle_type, hc.cycle_type, coe_singleton, g'1, cycle_type_one, add_zero] },
contrapose! hn2,
apply le_trans _ (c * g').support.card_le_univ,
rw [hd.card_support_mul],
exact add_le_add_left (two_le_card_support_of_ne_one g'1) _,
end
end cycle_type
lemma card_compl_support_modeq [decidable_eq α] {p n : ℕ} [hp : fact p.prime] {σ : perm α}
(hσ : σ ^ p ^ n = 1) : σ.supportᶜ.card ≡ fintype.card α [MOD p] :=
begin
rw [nat.modeq_iff_dvd' σ.supportᶜ.card_le_univ, ←finset.card_compl, compl_compl],
refine (congr_arg _ σ.sum_cycle_type).mp (multiset.dvd_sum (λ k hk, _)),
obtain ⟨m, -, hm⟩ := (nat.dvd_prime_pow hp.out).mp (order_of_dvd_of_pow_eq_one hσ),
obtain ⟨l, -, rfl⟩ := (nat.dvd_prime_pow hp.out).mp
((congr_arg _ hm).mp (dvd_of_mem_cycle_type hk)),
exact dvd_pow_self _ (λ h, (one_lt_of_mem_cycle_type hk).ne $ by rw [h, pow_zero]),
end
lemma exists_fixed_point_of_prime {p n : ℕ} [hp : fact p.prime] (hα : ¬ p ∣ fintype.card α)
{σ : perm α} (hσ : σ ^ p ^ n = 1) : ∃ a : α, σ a = a :=
begin
classical,
contrapose! hα,
simp_rw ← mem_support at hα,
exact nat.modeq_zero_iff_dvd.mp ((congr_arg _ (finset.card_eq_zero.mpr (compl_eq_bot.mpr
(finset.eq_univ_iff_forall.mpr hα)))).mp (card_compl_support_modeq hσ).symm),
end
lemma exists_fixed_point_of_prime' {p n : ℕ} [hp : fact p.prime] (hα : p ∣ fintype.card α)
{σ : perm α} (hσ : σ ^ p ^ n = 1) {a : α} (ha : σ a = a) : ∃ b : α, σ b = b ∧ b ≠ a :=
begin
classical,
have h : ∀ b : α, b ∈ σ.supportᶜ ↔ σ b = b :=
λ b, by rw [finset.mem_compl, mem_support, not_not],
obtain ⟨b, hb1, hb2⟩ := finset.exists_ne_of_one_lt_card (lt_of_lt_of_le hp.out.one_lt
(nat.le_of_dvd (finset.card_pos.mpr ⟨a, (h a).mpr ha⟩) (nat.modeq_zero_iff_dvd.mp
((card_compl_support_modeq hσ).trans (nat.modeq_zero_iff_dvd.mpr hα))))) a,
exact ⟨b, (h b).mp hb1, hb2⟩,
end
lemma is_cycle_of_prime_order' {σ : perm α} (h1 : (order_of σ).prime)
(h2 : fintype.card α < 2 * (order_of σ)) : σ.is_cycle :=
begin
classical,
exact is_cycle_of_prime_order h1 (lt_of_le_of_lt σ.support.card_le_univ h2),
end
lemma is_cycle_of_prime_order'' {σ : perm α} (h1 : (fintype.card α).prime)
(h2 : order_of σ = fintype.card α) : σ.is_cycle :=
is_cycle_of_prime_order' ((congr_arg nat.prime h2).mpr h1)
begin
classical,
rw [←one_mul (fintype.card α), ←h2, mul_lt_mul_right (order_of_pos σ)],
exact one_lt_two,
end
section cauchy
variables (G : Type*) [group G] (n : ℕ)
/-- The type of vectors with terms from `G`, length `n`, and product equal to `1:G`. -/
def vectors_prod_eq_one : set (vector G n) :=
{v | v.to_list.prod = 1}
namespace vectors_prod_eq_one
lemma mem_iff {n : ℕ} (v : vector G n) :
v ∈ vectors_prod_eq_one G n ↔ v.to_list.prod = 1 := iff.rfl
lemma zero_eq : vectors_prod_eq_one G 0 = {vector.nil} :=
set.eq_singleton_iff_unique_mem.mpr ⟨eq.refl (1 : G), λ v hv, v.eq_nil⟩
lemma one_eq : vectors_prod_eq_one G 1 = {vector.nil.cons 1} :=
begin
simp_rw [set.eq_singleton_iff_unique_mem, mem_iff,
vector.to_list_singleton, list.prod_singleton, vector.head_cons],
exact ⟨rfl, λ v hv, v.cons_head_tail.symm.trans (congr_arg2 vector.cons hv v.tail.eq_nil)⟩,
end
instance zero_unique : unique (vectors_prod_eq_one G 0) :=
by { rw zero_eq, exact set.unique_singleton vector.nil }
instance one_unique : unique (vectors_prod_eq_one G 1) :=
by { rw one_eq, exact set.unique_singleton (vector.nil.cons 1) }
/-- Given a vector `v` of length `n`, make a vector of length `n + 1` whose product is `1`,
by appending the inverse of the product of `v`. -/
@[simps] def vector_equiv : vector G n ≃ vectors_prod_eq_one G (n + 1) :=
{ to_fun := λ v, ⟨v.to_list.prod⁻¹ ::ᵥ v,
by rw [mem_iff, vector.to_list_cons, list.prod_cons, inv_mul_self]⟩,
inv_fun := λ v, v.1.tail,
left_inv := λ v, v.tail_cons v.to_list.prod⁻¹,
right_inv := λ v, subtype.ext ((congr_arg2 vector.cons (eq_inv_of_mul_eq_one_left (by
{ rw [←list.prod_cons, ←vector.to_list_cons, v.1.cons_head_tail],
exact v.2 })).symm rfl).trans v.1.cons_head_tail) }
/-- Given a vector `v` of length `n` whose product is 1, make a vector of length `n - 1`,
by deleting the last entry of `v`. -/
def equiv_vector : vectors_prod_eq_one G n ≃ vector G (n - 1) :=
((vector_equiv G (n - 1)).trans (if hn : n = 0 then (show vectors_prod_eq_one G (n - 1 + 1) ≃
vectors_prod_eq_one G n, by { rw hn, apply equiv_of_unique })
else by rw tsub_add_cancel_of_le (nat.pos_of_ne_zero hn).nat_succ_le)).symm
instance [fintype G] : fintype (vectors_prod_eq_one G n) :=
fintype.of_equiv (vector G (n - 1)) (equiv_vector G n).symm
lemma card [fintype G] :
fintype.card (vectors_prod_eq_one G n) = fintype.card G ^ (n - 1) :=
(fintype.card_congr (equiv_vector G n)).trans (card_vector (n - 1))
variables {G n} {g : G} (v : vectors_prod_eq_one G n) (j k : ℕ)
/-- Rotate a vector whose product is 1. -/
def rotate : vectors_prod_eq_one G n :=
⟨⟨_, (v.1.1.length_rotate k).trans v.1.2⟩, list.prod_rotate_eq_one_of_prod_eq_one v.2 k⟩
lemma rotate_zero : rotate v 0 = v :=
subtype.ext (subtype.ext v.1.1.rotate_zero)
lemma rotate_rotate : rotate (rotate v j) k = rotate v (j + k) :=
subtype.ext (subtype.ext (v.1.1.rotate_rotate j k))
lemma rotate_length : rotate v n = v :=
subtype.ext (subtype.ext ((congr_arg _ v.1.2.symm).trans v.1.1.rotate_length))
end vectors_prod_eq_one
/-- For every prime `p` dividing the order of a finite group `G` there exists an element of order
`p` in `G`. This is known as Cauchy's theorem. -/
lemma _root_.exists_prime_order_of_dvd_card {G : Type*} [group G] [fintype G] (p : ℕ)
[hp : fact p.prime] (hdvd : p ∣ fintype.card G) : ∃ x : G, order_of x = p :=
begin
have hp' : p - 1 ≠ 0 := mt tsub_eq_zero_iff_le.mp (not_le_of_lt hp.out.one_lt),
have Scard := calc p ∣ fintype.card G ^ (p - 1) : hdvd.trans (dvd_pow (dvd_refl _) hp')
... = fintype.card (vectors_prod_eq_one G p) : (vectors_prod_eq_one.card G p).symm,
let f : ℕ → vectors_prod_eq_one G p → vectors_prod_eq_one G p :=
λ k v, vectors_prod_eq_one.rotate v k,
have hf1 : ∀ v, f 0 v = v := vectors_prod_eq_one.rotate_zero,
have hf2 : ∀ j k v, f k (f j v) = f (j + k) v :=
λ j k v, vectors_prod_eq_one.rotate_rotate v j k,
have hf3 : ∀ v, f p v = v := vectors_prod_eq_one.rotate_length,
let σ := equiv.mk (f 1) (f (p - 1))
(λ s, by rw [hf2, add_tsub_cancel_of_le hp.out.one_lt.le, hf3])
(λ s, by rw [hf2, tsub_add_cancel_of_le hp.out.one_lt.le, hf3]),
have hσ : ∀ k v, (σ ^ k) v = f k v :=
λ k v, nat.rec (hf1 v).symm (λ k hk, eq.trans (by exact congr_arg σ hk) (hf2 k 1 v)) k,
replace hσ : σ ^ (p ^ 1) = 1 := perm.ext (λ v, by rw [pow_one, hσ, hf3, one_apply]),
let v₀ : vectors_prod_eq_one G p := ⟨vector.repeat 1 p, (list.prod_repeat 1 p).trans (one_pow p)⟩,
have hv₀ : σ v₀ = v₀ := subtype.ext (subtype.ext (list.rotate_repeat (1 : G) p 1)),
obtain ⟨v, hv1, hv2⟩ := exists_fixed_point_of_prime' Scard hσ hv₀,
refine exists_imp_exists (λ g hg, order_of_eq_prime _ (λ hg', hv2 _))
(list.rotate_one_eq_self_iff_eq_repeat.mp (subtype.ext_iff.mp (subtype.ext_iff.mp hv1))),
{ rw [←list.prod_repeat, ←v.1.2, ←hg, (show v.val.val.prod = 1, from v.2)] },
{ rw [subtype.ext_iff_val, subtype.ext_iff_val, hg, hg', v.1.2],
refl },
end
/-- For every prime `p` dividing the order of a finite additive group `G` there exists an element of
order `p` in `G`. This is the additive version of Cauchy's theorem. -/
lemma _root_.exists_prime_add_order_of_dvd_card {G : Type*} [add_group G] [fintype G] (p : ℕ)
[hp : fact p.prime] (hdvd : p ∣ fintype.card G) : ∃ x : G, add_order_of x = p :=
@exists_prime_order_of_dvd_card (multiplicative G) _ _ _ _ hdvd
attribute [to_additive exists_prime_add_order_of_dvd_card] exists_prime_order_of_dvd_card
end cauchy
lemma subgroup_eq_top_of_swap_mem [decidable_eq α] {H : subgroup (perm α)}
[d : decidable_pred (∈ H)] {τ : perm α} (h0 : (fintype.card α).prime)
(h1 : fintype.card α ∣ fintype.card H) (h2 : τ ∈ H) (h3 : is_swap τ) :
H = ⊤ :=
begin
haveI : fact (fintype.card α).prime := ⟨h0⟩,
obtain ⟨σ, hσ⟩ := exists_prime_order_of_dvd_card (fintype.card α) h1,
have hσ1 : order_of (σ : perm α) = fintype.card α := (order_of_subgroup σ).trans hσ,
have hσ2 : is_cycle ↑σ := is_cycle_of_prime_order'' h0 hσ1,
have hσ3 : (σ : perm α).support = ⊤ :=
finset.eq_univ_of_card (σ : perm α).support (hσ2.order_of.symm.trans hσ1),
have hσ4 : subgroup.closure {↑σ, τ} = ⊤ := closure_prime_cycle_swap h0 hσ2 hσ3 h3,
rw [eq_top_iff, ←hσ4, subgroup.closure_le, set.insert_subset, set.singleton_subset_iff],
exact ⟨subtype.mem σ, h2⟩,
end
section partition
variables [decidable_eq α]
/-- The partition corresponding to a permutation -/
def partition (σ : perm α) : (fintype.card α).partition :=
{ parts := σ.cycle_type + repeat 1 (fintype.card α - σ.support.card),
parts_pos := λ n hn,
begin
cases mem_add.mp hn with hn hn,
{ exact zero_lt_one.trans (one_lt_of_mem_cycle_type hn) },
{ exact lt_of_lt_of_le zero_lt_one (ge_of_eq (multiset.eq_of_mem_repeat hn)) },
end,
parts_sum := by rw [sum_add, sum_cycle_type, multiset.sum_repeat, nsmul_eq_mul,
nat.cast_id, mul_one, add_tsub_cancel_of_le σ.support.card_le_univ] }
lemma parts_partition {σ : perm α} :
σ.partition.parts = σ.cycle_type + repeat 1 (fintype.card α - σ.support.card) := rfl
lemma filter_parts_partition_eq_cycle_type {σ : perm α} :
(partition σ).parts.filter (λ n, 2 ≤ n) = σ.cycle_type :=
begin
rw [parts_partition, filter_add, multiset.filter_eq_self.2 (λ _, two_le_of_mem_cycle_type),
multiset.filter_eq_nil.2 (λ a h, _), add_zero],
rw multiset.eq_of_mem_repeat h,
dec_trivial
end
lemma partition_eq_of_is_conj {σ τ : perm α} :
is_conj σ τ ↔ σ.partition = τ.partition :=
begin
rw [is_conj_iff_cycle_type_eq],
refine ⟨λ h, _, λ h, _⟩,
{ rw [nat.partition.ext_iff, parts_partition, parts_partition,
← sum_cycle_type, ← sum_cycle_type, h] },
{ rw [← filter_parts_partition_eq_cycle_type, ← filter_parts_partition_eq_cycle_type, h] }
end
end partition
/-!
### 3-cycles
-/
/-- A three-cycle is a cycle of length 3. -/
def is_three_cycle [decidable_eq α] (σ : perm α) : Prop := σ.cycle_type = {3}
namespace is_three_cycle
variables [decidable_eq α] {σ : perm α}
lemma cycle_type (h : is_three_cycle σ) : σ.cycle_type = {3} := h
lemma card_support (h : is_three_cycle σ) : σ.support.card = 3 :=
by rw [←sum_cycle_type, h.cycle_type, multiset.sum_singleton]
lemma _root_.card_support_eq_three_iff : σ.support.card = 3 ↔ σ.is_three_cycle :=
begin
refine ⟨λ h, _, is_three_cycle.card_support⟩,
by_cases h0 : σ.cycle_type = 0,
{ rw [←sum_cycle_type, h0, sum_zero] at h,
exact (ne_of_lt zero_lt_three h).elim },
obtain ⟨n, hn⟩ := exists_mem_of_ne_zero h0,
by_cases h1 : σ.cycle_type.erase n = 0,
{ rw [←sum_cycle_type, ←cons_erase hn, h1, cons_zero, multiset.sum_singleton] at h,
rw [is_three_cycle, ←cons_erase hn, h1, h, ←cons_zero] },
obtain ⟨m, hm⟩ := exists_mem_of_ne_zero h1,
rw [←sum_cycle_type, ←cons_erase hn, ←cons_erase hm, multiset.sum_cons, multiset.sum_cons] at h,
-- TODO: linarith [...] should solve this directly
have : ∀ {k}, 2 ≤ m → 2 ≤ n → n + (m + k) = 3 → false, { intros, linarith },
cases this (two_le_of_mem_cycle_type (mem_of_mem_erase hm)) (two_le_of_mem_cycle_type hn) h,
end
lemma is_cycle (h : is_three_cycle σ) : is_cycle σ :=
by rw [←card_cycle_type_eq_one, h.cycle_type, card_singleton]
lemma sign (h : is_three_cycle σ) : sign σ = 1 :=
begin
rw [equiv.perm.sign_of_cycle_type, h.cycle_type],
refl,
end
lemma inv {f : perm α} (h : is_three_cycle f) : is_three_cycle (f⁻¹) :=
by rwa [is_three_cycle, cycle_type_inv]
@[simp] lemma inv_iff {f : perm α} : is_three_cycle (f⁻¹) ↔ is_three_cycle f :=
⟨by { rw ← inv_inv f, apply inv }, inv⟩
lemma order_of {g : perm α} (ht : is_three_cycle g) :
order_of g = 3 :=
by rw [←lcm_cycle_type, ht.cycle_type, multiset.lcm_singleton, normalize_eq]
lemma is_three_cycle_sq {g : perm α} (ht : is_three_cycle g) :
is_three_cycle (g * g) :=
begin
rw [←pow_two, ←card_support_eq_three_iff, support_pow_coprime, ht.card_support],
rw [ht.order_of, nat.coprime_iff_gcd_eq_one],
norm_num,
end
end is_three_cycle
section
variable [decidable_eq α]
lemma is_three_cycle_swap_mul_swap_same
{a b c : α} (ab : a ≠ b) (ac : a ≠ c) (bc : b ≠ c) :
is_three_cycle (swap a b * swap a c) :=
begin
suffices h : support (swap a b * swap a c) = {a, b, c},
{ rw [←card_support_eq_three_iff, h],
simp [ab, ac, bc] },
apply le_antisymm ((support_mul_le _ _).trans (λ x, _)) (λ x hx, _),
{ simp [ab, ac, bc] },
{ simp only [finset.mem_insert, finset.mem_singleton] at hx,
rw mem_support,
simp only [perm.coe_mul, function.comp_app, ne.def],
obtain rfl | rfl | rfl := hx,
{ rw [swap_apply_left, swap_apply_of_ne_of_ne ac.symm bc.symm],
exact ac.symm },
{ rw [swap_apply_of_ne_of_ne ab.symm bc, swap_apply_right],
exact ab },
{ rw [swap_apply_right, swap_apply_left],
exact bc } }
end
open subgroup
lemma swap_mul_swap_same_mem_closure_three_cycles
{a b c : α} (ab : a ≠ b) (ac : a ≠ c) :
(swap a b * swap a c) ∈ closure {σ : perm α | is_three_cycle σ } :=
begin
by_cases bc : b = c,
{ subst bc,
simp [one_mem] },
exact subset_closure (is_three_cycle_swap_mul_swap_same ab ac bc)
end
lemma is_swap.mul_mem_closure_three_cycles {σ τ : perm α}
(hσ : is_swap σ) (hτ : is_swap τ) :
σ * τ ∈ closure {σ : perm α | is_three_cycle σ } :=
begin
obtain ⟨a, b, ab, rfl⟩ := hσ,
obtain ⟨c, d, cd, rfl⟩ := hτ,
by_cases ac : a = c,
{ subst ac,
exact swap_mul_swap_same_mem_closure_three_cycles ab cd },
have h' : swap a b * swap c d = swap a b * swap a c * (swap c a * swap c d),
{ simp [swap_comm c a, mul_assoc] },
rw h',
exact mul_mem (swap_mul_swap_same_mem_closure_three_cycles ab ac)
(swap_mul_swap_same_mem_closure_three_cycles (ne.symm ac) cd),
end
end
end equiv.perm
|
192847641a508bf8feb29ed37c102f0d921b381d | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/order/filter/cofinite.lean | fb058508f7ffaf1f3296f848dab7378c3d926777 | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 2,293 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Jeremy Avigad, Yury Kudryashov
-/
import order.filter.at_top_bot
/-!
# The cofinite filter
In this file we define
`cofinite`: the filter of sets with finite complement
and prove its basic properties. In particular, we prove that for `ℕ` it is equal to `at_top`.
## TODO
Define filters for other cardinalities of the complement.
-/
open set
open_locale classical
namespace filter
variables {α : Type*}
/-- The cofinite filter is the filter of subsets whose complements are finite. -/
def cofinite : filter α :=
{ sets := {s | finite sᶜ},
univ_sets := by simp only [compl_univ, finite_empty, mem_set_of_eq],
sets_of_superset := assume s t (hs : finite sᶜ) (st: s ⊆ t),
hs.subset $ compl_subset_compl.2 st,
inter_sets := assume s t (hs : finite sᶜ) (ht : finite (tᶜ)),
by simp only [compl_inter, finite.union, ht, hs, mem_set_of_eq] }
@[simp] lemma mem_cofinite {s : set α} : s ∈ (@cofinite α) ↔ finite sᶜ := iff.rfl
instance cofinite_ne_bot [infinite α] : ne_bot (@cofinite α) :=
mt empty_in_sets_eq_bot.mpr $ by { simp only [mem_cofinite, compl_empty], exact infinite_univ }
lemma frequently_cofinite_iff_infinite {p : α → Prop} :
(∃ᶠ x in cofinite, p x) ↔ set.infinite {x | p x} :=
by simp only [filter.frequently, filter.eventually, mem_cofinite, compl_set_of, not_not,
set.infinite]
end filter
open filter
lemma set.infinite_iff_frequently_cofinite {α : Type*} {s : set α} :
set.infinite s ↔ (∃ᶠ x in cofinite, x ∈ s) :=
frequently_cofinite_iff_infinite.symm
/-- For natural numbers the filters `cofinite` and `at_top` coincide. -/
lemma nat.cofinite_eq_at_top : @cofinite ℕ = at_top :=
begin
ext s,
simp only [mem_cofinite, mem_at_top_sets],
split,
{ assume hs,
use (hs.to_finset.sup id) + 1,
assume b hb,
by_contradiction hbs,
have := hs.to_finset.subset_range_sup_succ (finite.mem_to_finset.2 hbs),
exact not_lt_of_le hb (finset.mem_range.1 this) },
{ rintros ⟨N, hN⟩,
apply (finite_lt_nat N).subset,
assume n hn,
change n < N,
exact lt_of_not_ge (λ hn', hn $ hN n hn') }
end
|
d9423d394f3fc6382405bc370c7c68c5ed8d46ed | f3849be5d845a1cb97680f0bbbe03b85518312f0 | /tests/lean/meta_wf_error.lean | 716e36542577b4c59e75f4a0c8777f1d7e36205a | [
"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 | 384 | lean | def R : (Σ _ : nat, nat) → (Σ _ : nat, nat) → Prop :=
sigma.lex nat.lt (λ _, empty_relation)
def Rwf : well_founded R :=
sigma.lex_wf nat.lt_wf (λ _, empty_wf)
meta def Div : nat → nat → nat
| x y :=
if h : 0 < y ∧ y ≤ x
then
have x - y < x, from nat.sub_lt (nat.lt_of_lt_of_le h.left h.right) h.left,
Div (x - y) y + 1
else 0
using_well_founded R Rwf
|
9c2cd63869f939eb756aec24dcba8f6389b068e5 | f41725a360d902d3c7939fdf81a5acaf0d0467f0 | /src/primitive_element.lean | aae79b1667f580bd13dbabe15fa888ffb7d77cb2 | [] | no_license | pglutz/galois_theory | 978765d82b7586c21fd719b84b21d5eea030b25d | 4561c2c97d4c49377356e1d7a2051dedc87d30ba | refs/heads/master | 1,671,472,063,361 | 1,603,597,360,000 | 1,603,597,360,000 | 281,502,125 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 19,048 | lean | import adjoin
import subfield_stuff
import linear_algebra.finite_dimensional
import linear_algebra.basic
import data.set.finite
import field_theory.tower
import algebra.gcd_monoid
import field_theory.splitting_field
import field_theory.separable
noncomputable theory
local attribute [instance, priority 100] classical.prop_decidable
/- Some stupid lemmas used below. Maybe some of them are already in mathlib? -/
-- This should go into field_theory/subfield eventually probably
lemma is_subfield.pow_mem {K : Type*} [field K] {a : K} {n : ℤ} {s : set K} [is_subfield s] (h : a ∈ s) : a ^ n ∈ s :=
begin
by_cases hn : n ≥ 0,
{ lift n to ℕ using hn,
exact is_submonoid.pow_mem h, },
{ rw [(show n = (-1)*(-n), by ring), fpow_mul, fpow_neg a, fpow_one],
lift -n to ℕ using (show -n ≥ 0, by linarith),
exact is_submonoid.pow_mem (is_subfield.inv_mem h), },
end
-- Is this really not in mathlib?
/-- If M is an algebra over a field F and x is a nonzero element of F then x as an element of M is also nonzero. -/
lemma ne_zero_of_ne_zero (F M : Type*) [field F] [comm_semiring M] [nontrivial M] [algebra F M]
{x : F} (hx : x ≠ 0) : algebra_map F M x ≠ 0 :=
begin
revert hx,
contrapose!,
intro h,
rw ← (algebra_map F M).map_zero at h,
exact (algebra_map F M).injective h,
end
namespace polynomial
variables (F : Type*) [field F]
lemma gcd_eval_zero (f g : polynomial F) (α : F) (hf : f.eval α = 0) (hg : g.eval α = 0) : (euclidean_domain.gcd f g).eval α = 0 :=
begin
rw euclidean_domain.gcd_eq_gcd_ab f g,
rw [polynomial.eval_add,polynomial.eval_mul,polynomial.eval_mul,hf,hg,zero_mul,zero_mul,zero_add],
end
variables {E : Type*} [field E] [algebra F E]
lemma gcd_root_left (f g : polynomial F) (α : E) (hα : (euclidean_domain.gcd f g).eval₂ (algebra_map F E) α = 0) :
f.eval₂ (algebra_map F E) α = 0 :=
begin
cases euclidean_domain.gcd_dvd_left f g with p hp,
rw [hp,polynomial.eval₂_mul,hα,zero_mul],
end
lemma gcd_root_right (f g : polynomial F) (α : E) (hα : (euclidean_domain.gcd f g).eval₂ (algebra_map F E) α = 0) :
g.eval₂ (algebra_map F E) α = 0 :=
begin
cases euclidean_domain.gcd_dvd_right f g with p hp,
rw [hp,polynomial.eval₂_mul,hα,zero_mul],
end
end polynomial
/- Proof of the primitive element theorem. -/
open finite_dimensional
section
variables (F : Type*) [field F] {E : Type*} [field E] [algebra F E]
/-- Trivial case of the primitive element theorem. -/
lemma primitive_element_trivial (F_eq_E : set.range (algebra_map F E) = (⊤ : set E)) :
∃ α : E, F[α] = (⊤ : set E) :=
begin
use 0,
ext,
split,
exact λ _, trivial,
rw ← F_eq_E,
rintros ⟨x, rfl⟩,
apply adjoin.field_mem,
end
/- Primitive element theorem for finite fields. -/
-- Replaces earlier messy proof, courtesy of Aaron Anderson & Markus Himmel on zulip
/-- A finite dimensional vector space over a finite field is finite. -/
def finite_of_findim_over_finite [fintype F] [hE : finite_dimensional F E] : fintype E :=
module.fintype_of_fintype (classical.some_spec (finite_dimensional.exists_is_basis_finset F E) : _)
/-- Primitive element theorem assuming E is finite. -/
lemma primitive_element_fin_aux [fintype E] : ∃ α : E, F[α] = (⊤ : set E) :=
begin
obtain ⟨α, hα⟩ := is_cyclic.exists_generator (units E),
use α,
ext,
refine ⟨λ _, dec_trivial, λ _, _⟩,
by_cases hx : x = 0,
{ rw hx,
exact is_add_submonoid.zero_mem, },
{ obtain ⟨n, hn⟩ := set.mem_range.mp (hα (units.mk0 x hx)),
rw (show x = (α : E)^n, by norm_cast at *; simp *),
exact is_subfield.pow_mem (adjoin_simple_contains_element F ↑α),
},
end
/-- Primitive element theorem for finite dimensional extension of a finite field. -/
theorem primitive_element_fin [fintype F] [hfd : finite_dimensional F E] :
∃ α : E, F[α] = (⊤ : set E) :=
begin
haveI : fintype E := finite_of_findim_over_finite F,
exact primitive_element_fin_aux F,
end
end
/- Primitive element theorem for infinite fields. -/
section
variables {F : Type*} [field F] {E : Type*} [field E] (ϕ : F →+* E)
lemma primitive_element_two_aux (α β : E) {f g : polynomial F} [F_inf : infinite F] (hf : f ≠ 0) (hg : g ≠ 0) (f_monic : polynomial.monic f) (g_monic : polynomial.monic g) :
∃ c : F, ∀ (α' ∈ (f.map ϕ).roots) (β' ∈ (g.map ϕ).roots), β' ≠ β → ϕ c ≠ -(α' - α)/(β' - β) :=
begin
let sf := (f.map ϕ).roots,
let sg := (g.map ϕ).roots,
let s := {c : E | ∃ (α' ∈ sf) (β' ∈ sg), β' ≠ β ∧ c = -(α' - α)/(β' - β)},
let s' := ϕ⁻¹' s,
let r : E → E → E := λ α' β', -(α' - α)/(β' - β),
have hr : ∀ c ∈ s, ∃ α' β', ((α' ∈ sf) ∧ (β' ∈ sg)) ∧ r α' β' = c :=
begin
intros c hc,
rw set.mem_set_of_eq at hc,
tauto,
end,
have s_fin : s.finite :=
begin
refine (set.finite.image (λ z : E × E, r z.1 z.2) (set.finite_mem_finset (sf.product sg))).subset _,
simpa only [set.subset_def, set.mem_image, prod.exists, finset.mem_product] using hr,
end,
have s'_fin : s'.finite := s_fin.preimage ((ring_hom.injective ϕ).inj_on (⇑ϕ ⁻¹' s)),
obtain ⟨c, hc⟩ := infinite.exists_not_mem_finset s'_fin.to_finset,
rw [set.finite.mem_to_finset, set.mem_preimage, set.mem_set_of_eq] at hc,
push_neg at hc,
exact ⟨c, hc⟩,
end
lemma primitive_element_two_inf_key_aux {β : F} {h : polynomial F} (h_ne_zero : h ≠ 0) (h_sep : h.separable)
(h_root : h.eval β = 0) (h_splits : polynomial.splits ϕ h) (h_roots : ∀ x ∈ (h.map ϕ).roots, x = ϕ β) :
h = (polynomial.C (polynomial.leading_coeff h)) * (polynomial.X - polynomial.C β) :=
begin
have h_map_separable : (h.map ϕ).separable :=
begin
apply polynomial.separable.map,
exact h_sep,
end,
rw polynomial.splits_iff_exists_multiset at h_splits,
cases h_splits with s hs,
have s_elements : ∀ x ∈ s, x = ϕ β :=
begin
intros x hx,
have is_root : x ∈ (h.map ϕ).roots,
rw polynomial.mem_roots,
dsimp[polynomial.is_root],
rw polynomial.eval_map,
rw polynomial.eval₂_eq_eval_map,
rw hs,
rw polynomial.eval_mul,
cases multiset.exists_cons_of_mem hx with y hy,
rw hy,
rw multiset.map_cons,
simp only [polynomial.eval_X, multiset.prod_cons, polynomial.eval_C, zero_mul, polynomial.eval_mul, polynomial.eval_sub, mul_zero, sub_self],
exact polynomial.map_ne_zero h_ne_zero,
exact h_roots x is_root,
end,
replace s_elements : ∀ x ∈ multiset.map (λ (a : E), polynomial.X - polynomial.C a) s, x = polynomial.X - polynomial.C (ϕ β) :=
begin
intros x hx,
rw multiset.mem_map at hx,
cases hx with a ha,
specialize s_elements a ha.1,
rw s_elements at ha,
exact ha.2.symm,
end,
replace s_elements := multiset.eq_repeat_of_mem s_elements,
rw s_elements at hs,
rw multiset.prod_repeat at hs,
rw multiset.card_map at hs,
rw hs at h_map_separable,
have hf : ¬is_unit (polynomial.X - polynomial.C (ϕ β)) :=
begin
rw polynomial.is_unit_iff_degree_eq_zero,
rw polynomial.degree_X_sub_C,
exact dec_trivial,
end,
have map_injective := polynomial.map_injective ϕ ϕ.injective,
have hn : s.card ≠ 0 :=
begin
intro hs_card,
rw hs_card at hs,
rw pow_zero at hs,
rw mul_one at hs,
rw ←polynomial.map_C at hs,
replace hs := map_injective hs,
rw hs at h_root,
rw polynomial.eval_C at h_root,
rw polynomial.leading_coeff_eq_zero at h_root,
exact h_ne_zero h_root,
end,
rw (polynomial.separable.of_pow hf hn (polynomial.separable.of_mul_right h_map_separable)).2 at hs,
rw pow_one at hs,
apply map_injective,
rw hs,
rw polynomial.map_mul,
rw polynomial.map_C,
rw polynomial.map_sub,
rw polynomial.map_X,
rw polynomial.map_C,
end
end
variables {F : Type*} [field F] {E : Type*} [field E] [algebra F E]
lemma primitive_element_two_inf_key (α β : E) [F_sep : is_separable F E]
(F_inf : infinite F) : ∃ c : F, β ∈ F[α + (algebra_map F E) c * β] :=
begin
rcases F_sep α with ⟨hα, hf⟩,
rcases F_sep β with ⟨hβ, hg⟩,
let f := minimal_polynomial hα,
let g := minimal_polynomial hβ,
let f_E := f.map (algebra_map F E),
let g_E := g.map (algebra_map F E),
let E' := polynomial.splitting_field g_E,
let ιFE := algebra_map F E,
let ιEE' := algebra_map E E',
let ιFE' := ιEE'.comp(ιFE),
have key := primitive_element_two_aux ιFE' (ιEE' α) (ιEE' β) (minimal_polynomial.ne_zero hα) (minimal_polynomial.ne_zero hβ) (minimal_polynomial.monic hα) (minimal_polynomial.monic hβ),
cases key with c hc,
use c,
let γ := α+(ιFE c)*β,
let f' := f_E.comp(polynomial.C γ-(polynomial.C (ιFE c)) * (polynomial.X)),
let h := euclidean_domain.gcd f' g_E,
have h_sep : h.separable :=
begin
have div := euclidean_domain.gcd_dvd_right f' g_E,
cases div with p mul,
dsimp[←h] at mul,
apply polynomial.separable.of_mul_left,
rw ←mul,
exact polynomial.separable.map hg,
end,
have h_ne_zero : h ≠ 0 :=
begin
intro h_eq_zero,
rw euclidean_domain.gcd_eq_zero_iff at h_eq_zero,
apply polynomial.map_monic_ne_zero (minimal_polynomial.monic hβ) h_eq_zero.2,
end,
have h_map_separable : (h.map ιEE').separable :=
begin
apply polynomial.separable.map,
exact h_sep,
end,
have h_root : h.eval β = 0 :=
begin
apply polynomial.gcd_eval_zero,
rw [polynomial.eval_comp,polynomial.eval_sub,polynomial.eval_mul,polynomial.eval_C,polynomial.eval_C,polynomial.eval_X,add_sub_cancel],
rw [polynomial.eval_map,←polynomial.aeval_def,minimal_polynomial.aeval],
rw [polynomial.eval_map,←polynomial.aeval_def,minimal_polynomial.aeval],
end,
have h_splits : polynomial.splits (algebra_map E E') h :=
polynomial.splits_of_splits_of_dvd (algebra_map E E') (polynomial.map_ne_zero (minimal_polynomial.ne_zero hβ)) (polynomial.splitting_field.splits g_E) (euclidean_domain.gcd_dvd_right f' g_E),
have h_roots : ∀ x ∈ (h.map ιEE').roots, x = algebra_map E E' β :=
begin
intros x hx,
rw polynomial.mem_roots at hx,
dsimp[polynomial.is_root] at hx,
rw polynomial.eval_map at hx,
have f_root : f'.eval₂ (algebra_map E E') x = 0 := polynomial.gcd_root_left E f' g_E x hx,
simp only [polynomial.eval₂_comp,polynomial.eval₂_map,polynomial.eval₂_sub,polynomial.eval₂_mul,polynomial.eval₂_C,polynomial.eval₂_X] at f_root,
replace f_root : _ ∈ (f.map ιFE').roots,
rw polynomial.mem_roots,
dsimp[polynomial.is_root],
rw polynomial.eval_map,
exact f_root,
exact polynomial.map_ne_zero (minimal_polynomial.ne_zero hα),
specialize hc _ f_root,
have g_root : g_E.eval₂ (algebra_map E E') x = 0 := polynomial.gcd_root_right E f' g_E x hx,
simp only [polynomial.eval₂_map] at g_root,
replace g_root : _ ∈ (g.map ιFE').roots,
rw polynomial.mem_roots,
dsimp[polynomial.is_root],
rw polynomial.eval_map,
exact g_root,
exact polynomial.map_ne_zero (minimal_polynomial.ne_zero hβ),
specialize hc _ g_root,
by_contradiction,
specialize hc a,
apply hc,
dsimp[ιEE'],
rw[neg_sub,ring_hom.map_add,←sub_add,←sub_sub,sub_self,zero_sub,neg_add_eq_sub,ring_hom.map_mul,←mul_sub],
symmetry,
apply mul_div_cancel,
rw sub_ne_zero,
exact a,
exact polynomial.map_ne_zero h_ne_zero,
end,
replace key := primitive_element_two_inf_key_aux ιEE' h_ne_zero h_sep h_root h_splits h_roots,
let f_Fγ := (f.map(algebra_map F F[γ])).comp(polynomial.C (adjoin_simple.gen F γ)-(polynomial.C ↑c) * (polynomial.X)),
let g_Fγ := g.map(algebra_map F F[γ]),
have composition2 : (algebra_map F[γ] E).comp(algebra_map F F[γ]) = algebra_map F E := by ext;refl,
have f_map : f_Fγ.map(algebra_map F[γ] E) = f' :=
begin
dsimp[f_Fγ,f',f_E],
rw ←composition2,
rw ←polynomial.map_map,
set p := f.map(algebra_map F F[γ]),
dsimp[←p],
rw polynomial.map_comp (algebra_map F[γ] E) p (polynomial.C (adjoin_simple.gen F γ)-(polynomial.C ↑c) * (polynomial.X)),
rw [polynomial.map_sub,polynomial.map_C,adjoin_simple.gen_eq_alpha,polynomial.map_mul,polynomial.map_C,polynomial.map_X],
refl,
end,
have g_map : g_Fγ.map(algebra_map F[γ] E) = g_E :=
begin
rw polynomial.map_map,
rw composition2,
end,
dsimp[h] at key,
rw [←f_map,←g_map] at key,
have swap : euclidean_domain.gcd (f_Fγ.map(algebra_map F[γ] E)) (g_Fγ.map(algebra_map F[γ] E)) = (euclidean_domain.gcd f_Fγ g_Fγ).map(algebra_map F[γ] E),
convert polynomial.gcd_map (algebra_map F[γ] E),
rw swap at key,
set p := euclidean_domain.gcd f_Fγ g_Fγ,
set k := (p.map(algebra_map F[γ] E)).leading_coeff,
dsimp[←k] at key,
rw mul_sub at key,
rw ←polynomial.C_mul at key,
have coeff0 : algebra_map F[γ] E (p.coeff 0) = -(k*β) :=
by rw [←polynomial.coeff_map,key, polynomial.coeff_sub, polynomial.coeff_C_mul, polynomial.coeff_C_zero, polynomial.coeff_X_zero, mul_zero, zero_sub],
have coeff1 : algebra_map F[γ] E (p.coeff 1) = k :=
begin
rw [←polynomial.coeff_map,key,polynomial.coeff_sub,polynomial.coeff_mul_X,polynomial.coeff_C_zero,polynomial.coeff_C],
change k - 0 = k,
rw sub_zero,
end,
have k_ne_zero : k≠0 :=
begin
intro k_eq_zero,
rw [polynomial.leading_coeff_eq_zero,←polynomial.map_zero (algebra_map F[γ] E)] at k_eq_zero,
replace k_eq_zero := polynomial.map_injective (algebra_map F[γ] E) (algebra_map F[γ] E).injective k_eq_zero,
rw euclidean_domain.gcd_eq_zero_iff at k_eq_zero,
apply polynomial.map_monic_ne_zero (minimal_polynomial.monic hβ) k_eq_zero.2,
end,
have last_step : β = algebra_map F[γ] E (-p.coeff 0 / p.coeff 1) :=
by rw [division_def,ring_hom.map_mul,ring_hom.map_neg,ring_hom.map_inv,coeff0,coeff1,neg_neg,mul_comm,←mul_assoc,inv_mul_cancel k_ne_zero,one_mul],
change β = ↑(-p.coeff 0 / p.coeff 1) at last_step,
have h := subtype.mem (-p.coeff 0 / p.coeff 1),
rw ←last_step at h,
exact h,
end
/-- Primitive element theorem for adjoining two elements to an infinite field. -/
lemma primitive_element_two_inf (α β : E) (F_sep : is_separable F E)
(F_inf : infinite F) : ∃ γ : E, F[α, β] = F[γ] :=
begin
obtain ⟨c, β_in_Fγ⟩ := primitive_element_two_inf_key α β F_inf,
let c' := algebra_map F E c,
let γ := α + c'*β,
have γ_in_Fγ : γ ∈ F[γ] := adjoin_simple_contains_element F γ,
have c_in_Fγ : c' ∈ F[γ] := adjoin.field_mem F {γ} c,
have cβ_in_Fγ : c'*β ∈ F[γ] := is_submonoid.mul_mem c_in_Fγ β_in_Fγ,
have α_in_Fγ : α ∈ F[γ] := by rw (show α = γ - c'*β, by simp *);
exact is_add_subgroup.sub_mem F[γ] γ (c'*β) γ_in_Fγ cβ_in_Fγ,
have αβ_in_Fγ : {α, β} ⊆ F[γ] := λ x hx, by cases hx; cases hx; assumption,
have Fαβ_sub_Fγ : F[α, β] ⊆ F[γ] := adjoin_subset' F {α, β} αβ_in_Fγ,
have α_in_Fαβ : α ∈ F[α, β] := adjoin.set_mem F {α, β} ⟨α, set.mem_insert α {β}⟩,
have β_in_Fαβ : β ∈ F[α, β] := adjoin.set_mem F {α, β} ⟨β, set.mem_insert_of_mem α rfl⟩,
have c_in_Fαβ : c' ∈ (F[α, β] : set E) := adjoin.field_mem F {α, β} c,
have cβ_in_Fαβ : c'*β ∈ F[α, β] := is_submonoid.mul_mem c_in_Fαβ β_in_Fαβ,
have γ_in_Fαβ : γ ∈ F[α, β] := is_add_submonoid.add_mem α_in_Fαβ cβ_in_Fαβ,
have Fγ_sub_Fαβ : F[γ] ⊆ F[α, β] := adjoin_simple_subset' F γ γ_in_Fαβ,
exact ⟨γ, set.subset.antisymm Fαβ_sub_Fγ Fγ_sub_Fαβ⟩,
end
universe u
/-- Primitive element theorem for infinite fields. -/
theorem primitive_element_inf (F E : Type u) [field F] [field E] [algebra F E] (F_sep : is_separable F E) (F_findim: finite_dimensional F E)
(F_inf : infinite F) (n : ℕ) (hn : findim F E = n) : (∃ α : E, F[α] = (⊤ : set E)) :=
begin
tactic.unfreeze_local_instances,
revert F,
apply n.strong_induction_on,
clear n,
intros n ih F hF hFE F_sep F_findim F_inf hn,
by_cases F_neq_E : set.range (algebra_map F E) = (⊤ : set E),
{ exact primitive_element_trivial F F_neq_E, },
{ have : ∃ α : E, α ∉ set.range (algebra_map F E) :=
begin
revert F_neq_E,
contrapose!,
exact λ h, set.ext (λ x, ⟨λ _, dec_trivial, λ _, h x⟩),
end,
rcases this with ⟨α, hα⟩,
by_cases h : F[α] = (⊤ : set E),
{ exact ⟨α, h⟩, },
{ have Fα_findim : finite_dimensional F[α] E := adjoin_findim_of_findim F α,
have Fα_le_n : findim F[α] E < n := by rw ← hn; exact adjoin_dim_lt F hα,
have Fα_inf : infinite F[α] := adjoin_inf_of_inf F {α} F_inf,
have Fα_sep : is_separable F[α] E := adjoin_separable F {α},
obtain ⟨β, hβ⟩ := ih (findim F[α] E) Fα_le_n F[α]
Fα_sep Fα_findim Fα_inf rfl,
obtain ⟨γ, hγ⟩ := primitive_element_two_inf α β F_sep F_inf,
rw [adjoin_simple_twice, hγ] at hβ,
exact ⟨γ, hβ⟩,
},
},
end
/- Actual primitive element theorem. -/
/-- Primitive element theorem in same universe. -/
theorem primitive_element_aux (F E : Type u) [field F] [field E] [algebra F E]
(F_sep : is_separable F E) (F_findim : finite_dimensional F E) :
(∃ α : E, F[α] = (⊤ : set E)) :=
begin
by_cases F_finite : nonempty (fintype F),
exact nonempty.elim F_finite (λ h : fintype F, @primitive_element_fin F _ E _ _ h F_findim),
exact primitive_element_inf F E F_sep F_findim (not_nonempty_fintype.mp F_finite) (findim F E) rfl,
end
/-- Primitive element theorem in different universes. -/
theorem primitive_element (F_sep : is_separable F E) (F_findim : finite_dimensional F E) :
(∃ α : E, F[α] = (⊤ : set E)) :=
begin
set F' := set.range (algebra_map F E) with hF',
have F'_sep : is_separable F' E := inclusion.separable F_sep,
have F'_findim : finite_dimensional F' E := inclusion.finite_dimensional F_findim,
obtain ⟨α, hα⟩ := primitive_element_aux F' E F'_sep F'_findim,
exact ⟨α, by simp only [*, adjoin_equals_adjoin_range]⟩,
end |
a350d033d9e59207f35e39b78c54d01494b0b34f | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/topology/algebra/ordered/left_right.lean | 556c50f356951b43400af4a4e667463392d6c51b | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,511 | lean | /-
Copyright (c) 2021 Anatole Dedecker. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anatole Dedecker
-/
import topology.continuous_on
/-!
# Left and right continuity
In this file we prove a few lemmas about left and right continuous functions:
* `continuous_within_at_Ioi_iff_Ici`: two definitions of right continuity
(with `(a, ∞)` and with `[a, ∞)`) are equivalent;
* `continuous_within_at_Iio_iff_Iic`: two definitions of left continuity
(with `(-∞, a)` and with `(-∞, a]`) are equivalent;
* `continuous_at_iff_continuous_left_right`, `continuous_at_iff_continuous_left'_right'` :
a function is continuous at `a` if and only if it is left and right continuous at `a`.
## Tags
left continuous, right continuous
-/
open set filter
open_locale topological_space
section partial_order
variables {α β : Type*} [topological_space α] [partial_order α] [topological_space β]
lemma continuous_within_at_Ioi_iff_Ici {a : α} {f : α → β} :
continuous_within_at f (Ioi a) a ↔ continuous_within_at f (Ici a) a :=
by simp only [← Ici_diff_left, continuous_within_at_diff_self]
lemma continuous_within_at_Iio_iff_Iic {a : α} {f : α → β} :
continuous_within_at f (Iio a) a ↔ continuous_within_at f (Iic a) a :=
@continuous_within_at_Ioi_iff_Ici (order_dual α) _ ‹topological_space α› _ _ _ f
end partial_order
variables {α β : Type*} [topological_space α] [linear_order α] [topological_space β]
lemma nhds_left_sup_nhds_right (a : α) :
𝓝[Iic a] a ⊔ 𝓝[Ici a] a = 𝓝 a :=
by rw [← nhds_within_union, Iic_union_Ici, nhds_within_univ]
lemma nhds_left'_sup_nhds_right (a : α) :
𝓝[Iio a] a ⊔ 𝓝[Ici a] a = 𝓝 a :=
by rw [← nhds_within_union, Iio_union_Ici, nhds_within_univ]
lemma nhds_left_sup_nhds_right' (a : α) :
𝓝[Iic a] a ⊔ 𝓝[Ioi a] a = 𝓝 a :=
by rw [← nhds_within_union, Iic_union_Ioi, nhds_within_univ]
lemma continuous_at_iff_continuous_left_right {a : α} {f : α → β} :
continuous_at f a ↔ continuous_within_at f (Iic a) a ∧ continuous_within_at f (Ici a) a :=
by simp only [continuous_within_at, continuous_at, ← tendsto_sup, nhds_left_sup_nhds_right]
lemma continuous_at_iff_continuous_left'_right' {a : α} {f : α → β} :
continuous_at f a ↔ continuous_within_at f (Iio a) a ∧ continuous_within_at f (Ioi a) a :=
by rw [continuous_within_at_Ioi_iff_Ici, continuous_within_at_Iio_iff_Iic,
continuous_at_iff_continuous_left_right]
|
07d48abb51eba4bbcaaf21693bc1894ad194854c | 6b45072eb2b3db3ecaace2a7a0241ce81f815787 | /data/rat.lean | 7475eef1e02caa639afca90c2f57ab232d35a2a5 | [] | no_license | avigad/library_dev | 27b47257382667b5eb7e6476c4f5b0d685dd3ddc | 9d8ac7c7798ca550874e90fed585caad030bbfac | refs/heads/master | 1,610,452,468,791 | 1,500,712,839,000 | 1,500,713,478,000 | 69,311,142 | 1 | 0 | null | 1,474,942,903,000 | 1,474,942,902,000 | null | UTF-8 | Lean | false | false | 15,867 | 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
Introduces the rational numbers as discrete, linear ordered field.
-/
import standard
/- linorder -/
section linear_order_cases_on
universes u v
variables {α : Type u} [decidable_linear_order α] {β : Sort v}
def linear_order_cases_on (a b : α) (h_eq : a = b → β) (h_lt : a < b → β) (h_gt : a > b → β) : β :=
if h₁ : a = b then h_eq h₁ else
if h₂ : a < b then h_lt h₂ else h_gt ((lt_or_gt_of_ne h₁)^.resolve_left h₂)
variables {a b : α} {h_eq : a = b → β} {h_lt : a < b → β} {h_gt : a > b → β}
lemma linear_order_cases_on_eq (h : a = b) : linear_order_cases_on a b h_eq h_lt h_gt = h_eq h :=
dif_pos h
lemma linear_order_cases_on_lt (h : a < b) : linear_order_cases_on a b h_eq h_lt h_gt = h_lt h :=
eq.trans (dif_neg $ ne_of_lt h) $ dif_pos h
lemma linear_order_cases_on_gt (h : a > b) : linear_order_cases_on a b h_eq h_lt h_gt = h_gt h :=
eq.trans (dif_neg $ (ne_of_lt h)^.symm) (dif_neg $ not_lt_of_ge $ le_of_lt h)
end linear_order_cases_on
/- linorder ring -/
section ordered_ring
universes u
variables {α : Type u} [linear_ordered_ring α] {a b : α}
lemma mul_nonneg_iff_right_nonneg_of_pos (h : 0 < a) : 0 ≤ b * a ↔ 0 ≤ b :=
⟨assume : 0 ≤ b * a, nonneg_of_mul_nonneg_right this h, assume : 0 ≤ b, mul_nonneg this $ le_of_lt h⟩
end ordered_ring
/- auxiliary -/
lemma not_antimono {a b : Prop} (nb : ¬ b) (h : (a → b)) : ¬ a :=
assume ha, nb (h ha)
/- rational numbers -/
namespace rat
protected def num_denum := ℤ × {d:ℤ // d > 0}
protected def rel : rat.num_denum → rat.num_denum → Prop
| ⟨n₁, ⟨d₁, _⟩⟩ ⟨n₂, ⟨d₂, _⟩⟩ := n₁ * d₂ = n₂ * d₁
private lemma rel_trans : Π{p q r}, rat.rel p q → rat.rel q r → rat.rel p r
| ⟨n₁, ⟨d₁, _⟩⟩ ⟨n₂, ⟨d₂, _⟩⟩ ⟨n₃, ⟨d₃, _⟩⟩ :=
assume (h₁ : n₁ * d₂ = n₂ * d₁),
assume (h₂ : n₂ * d₃ = n₃ * d₂),
show n₁ * d₃ = n₃ * d₁,
from eq_of_mul_eq_mul_right (ne_of_lt ‹d₂ > 0›)^.symm (by cc)
instance setoid_rat.rel : setoid rat.num_denum :=
{r := rat.rel, iseqv :=
⟨assume ⟨_, ⟨_, _⟩⟩, rfl,
assume ⟨n₁, ⟨d₁, _⟩⟩ ⟨n₂, ⟨d₂, _⟩⟩ h, h^.symm,
assume a b c, rel_trans⟩}
@[simp]
protected theorem rel_eq {n₁ d₁ n₂ d₂ : ℤ} { h₁ : d₁ > 0 } { h₂ : d₂ > 0 } :
@setoid.r rat.num_denum _ (n₁, ⟨d₁, h₁⟩) (n₂, ⟨d₂, h₂⟩) = (n₁ * d₂ = n₂ * d₁) :=
rfl
end rat
def rat := quotient rat.setoid_rat.rel
notation `ℚ` := rat
namespace rat
protected def zero : ℚ := ⟦⟨0, ⟨1, zero_lt_one⟩⟩⟧
instance : has_zero ℚ := ⟨rat.zero⟩
protected def one : ℚ := ⟦⟨1, ⟨1, zero_lt_one⟩⟩⟧
instance : has_one ℚ := ⟨rat.one⟩
private def add' : rat.num_denum → rat.num_denum → ℚ
| ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩ := ⟦⟨n₁ * d₂ + n₂ * d₁, ⟨d₁ * d₂, mul_pos h₁ h₂⟩⟩⟧
protected def add : ℚ → ℚ → ℚ :=
quotient.lift₂ add' $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩ ⟨n₃, ⟨d₃, h₃⟩⟩ ⟨n₄, ⟨d₄, h₄⟩⟩,
assume (h₁ : n₁ * d₃ = n₃ * d₁) (h₂ : n₂ * d₄ = n₄ * d₂),
quotient.sound $
calc (n₁ * d₂ + n₂ * d₁) * (d₃ * d₄) =
(n₁ * d₃) * d₂ * d₄ + (n₂ * d₄) * (d₁ * d₃) : by simp [mul_add, add_mul]
... = (n₃ * d₁) * d₂ * d₄ + (n₄ * d₂) * (d₁ * d₃) : by rw [h₁, h₂]
... = (n₃ * d₄ + n₄ * d₃) * (d₁ * d₂) : by simp [mul_add, add_mul]
instance : has_add ℚ := ⟨rat.add⟩
private def neg' : rat.num_denum → ℚ
| ⟨n, d⟩ := ⟦⟨-n, d⟩⟧
protected def neg : ℚ → ℚ :=
quotient.lift neg' $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩,
assume (h : n₁ * d₂ = n₂ * d₁),
quotient.sound $ show (-n₁) * d₂ = (-n₂) * d₁,
by simp [h]
instance : has_neg ℚ := ⟨rat.neg⟩
private def mul' : rat.num_denum → rat.num_denum → ℚ
| ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩ := ⟦⟨n₁ * n₂, ⟨d₁ * d₂, mul_pos h₁ h₂⟩⟩⟧
protected def mul : ℚ → ℚ → ℚ :=
quotient.lift₂ mul' $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩ ⟨n₃, ⟨d₃, h₃⟩⟩ ⟨n₄, ⟨d₄, h₄⟩⟩,
assume (h₁ : n₁ * d₃ = n₃ * d₁) (h₂ : n₂ * d₄ = n₄ * d₂),
quotient.sound $
calc (n₁ * n₂) * (d₃ * d₄) =
(n₁ * d₃) * (n₂ * d₄) : by simp
... = (n₃ * d₁) * (n₄ * d₂) : by rw [h₁, h₂]
... = (n₃ * n₄) * (d₁ * d₂) : by simp
instance : has_mul ℚ := ⟨rat.mul⟩
private def inv' : rat.num_denum → ℚ
| ⟨n, ⟨d, h⟩⟩ := linear_order_cases_on n 0
(assume : n = 0, 0)
(assume : n < 0, ⟦⟨-d, ⟨-n, neg_pos_of_neg this⟩⟩⟧)
(assume : n > 0, ⟦⟨d, ⟨n, this⟩⟩⟧)
private lemma inv'_zero : Π{d : {d:ℤ // d > 0}}, inv' ⟨0, d⟩ = 0
| ⟨d, p⟩ := linear_order_cases_on_eq rfl
private lemma inv'_pos {n d : ℤ} {h : d > 0} (p : n > 0) : inv' ⟨n, ⟨d, h⟩⟩ = ⟦⟨d, ⟨n, p⟩⟩⟧ :=
linear_order_cases_on_gt p
private lemma inv'_neg {n d : ℤ} {h : d > 0} (p : n < 0) : inv' ⟨n, ⟨d, h⟩⟩ = ⟦⟨- d, ⟨- n, neg_pos_of_neg p⟩⟩⟧ :=
linear_order_cases_on_lt p
protected def inv : ℚ → ℚ :=
quotient.lift inv' $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩,
assume h_eq : n₁ * d₂ = n₂ * d₁,
linear_order_cases_on n₁ 0
(assume : n₁ = 0,
have n₂ * d₁ = 0,
by simp [this] at h_eq; simp [h_eq],
have n₂ = 0,
from (eq_zero_or_eq_zero_of_mul_eq_zero this)^.resolve_right $ (ne_of_lt h₁)^.symm,
by simp [this, ‹n₁ = 0›, inv'_zero])
(assume : n₁ < 0,
have n₂ * d₁ < 0,
from h_eq ▸ mul_neg_of_neg_of_pos this ‹0 < d₂›,
have n₂ < 0,
from neg_of_mul_neg_right this $ le_of_lt ‹d₁ > 0›,
begin
rw [inv'_neg this, inv'_neg ‹n₁ < 0›],
apply quotient.sound,
simp [h_eq]
end)
(assume : n₁ > 0,
have n₂ * d₁ > 0,
from h_eq ▸ mul_pos this ‹0 < d₂›,
have n₂ > 0,
from pos_of_mul_pos_right this $ le_of_lt ‹d₁ > 0›,
begin
rw [inv'_pos this, inv'_pos ‹n₁ > 0›],
apply quotient.sound,
simp [h_eq]
end)
instance : has_inv ℚ := ⟨rat.inv⟩
variables (a b c : ℚ)
protected lemma add_zero : a + 0 = a :=
quotient.induction_on a $ λ⟨n, ⟨d, h⟩⟩, quotient.sound $
by simp [rat.rel]
protected lemma zero_add : 0 + a = a :=
quotient.induction_on a $ λ⟨n, ⟨d, h⟩⟩, quotient.sound $
by simp [rat.rel]
protected lemma add_comm : a + b = b + a :=
quotient.induction_on₂ a b $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩, quotient.sound $
by simp [rat.rel]
protected lemma add_assoc : a + b + c = a + (b + c) :=
quotient.induction_on₃ a b c $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩ ⟨n₃, ⟨d₃, h₃⟩⟩, quotient.sound $
by simp [rat.rel, mul_add, add_mul]
protected lemma add_left_neg : -a + a = 0 :=
quotient.induction_on a $ λ⟨n, ⟨d, h⟩⟩, quotient.sound $
by simp [rat.rel]
protected lemma mul_one : a * 1 = a :=
quotient.induction_on a $ λ⟨n, ⟨d, h⟩⟩, quotient.sound $
by simp [rat.rel]
protected lemma one_mul : 1 * a = a :=
quotient.induction_on a $ λ⟨n, ⟨d, h⟩⟩, quotient.sound $
by simp [rat.rel]
protected lemma mul_comm : a * b = b * a :=
quotient.induction_on₂ a b $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩, quotient.sound $
by simp [rat.rel]
protected lemma mul_assoc : a * b * c = a * (b * c) :=
quotient.induction_on₃ a b c $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩ ⟨n₃, ⟨d₃, h₃⟩⟩, quotient.sound $
by simp [rat.rel]
protected lemma add_mul : (a + b) * c = a * c + b * c :=
quotient.induction_on₃ a b c $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩ ⟨n₃, ⟨d₃, h₃⟩⟩, quotient.sound $
by simp [rat.rel, mul_add, add_mul]
protected lemma mul_add : a * (b + c) = a * b + a * c :=
quotient.induction_on₃ a b c $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩ ⟨n₃, ⟨d₃, h₃⟩⟩, quotient.sound $
by simp [rat.rel, mul_add, add_mul]
private lemma rat_eq_zero : ∀{a : rat.num_denum}, ⟦a⟧ = (0:ℚ) → a.1 = 0
| ⟨n, ⟨d, h⟩⟩ eq_0 :=
have n * 1 = 0 * d, from quotient.exact eq_0,
begin simp at this, assumption end
private lemma eq_zero_of_rat_eq_zero : ∀{a : rat.num_denum}, a.1 = 0 → ⟦a⟧ = (0:ℚ)
| ⟨n, ⟨d, h⟩⟩ (_ : n = 0) := begin simp [‹n = 0›], apply quotient.sound, simp [rat.rel] end
private lemma rat_eq_zero_iff {a : rat.num_denum} : ⟦a⟧ = (0:ℚ) ↔ a.1 = 0 :=
⟨rat_eq_zero, eq_zero_of_rat_eq_zero⟩
protected lemma zero_ne_one : 0 ≠ (1:ℚ) :=
assume h, zero_ne_one (rat_eq_zero h^.symm)^.symm
protected lemma mul_inv_cancel : a ≠ 0 → a * a⁻¹ = 1 :=
quotient.induction_on a $ λ⟨n, ⟨d, h⟩⟩ neq0,
let a : rat.num_denum := ⟨n, ⟨d, h⟩⟩ in
linear_order_cases_on n 0
(assume : n = 0, by rw [this, @eq_zero_of_rat_eq_zero ⟨0, ⟨d, h⟩⟩ rfl] at neq0; contradiction)
(assume : n < 0,
have @has_inv.inv rat _ ⟦a⟧ = ⟦⟨-d, ⟨-n, neg_pos_of_neg this⟩⟩⟧,
from @inv'_neg n d h _,
begin simp [this], apply quotient.sound, simp [rat.rel] end)
(assume : n > 0,
have @has_inv.inv rat _ ⟦a⟧ = ⟦⟨d, ⟨n, this⟩⟩⟧,
from @inv'_pos n d h _,
begin simp [this], apply quotient.sound, simp [rat.rel] end)
protected lemma inv_mul_cancel (h : a ≠ 0) : a⁻¹ * a = 1 :=
eq.trans (rat.mul_comm _ _) (rat.mul_inv_cancel _ h)
instance decidable_eq_rat.rel : Π{a b : rat.num_denum}, decidable (rat.rel a b)
| ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩ := show decidable (n₁ * d₂ = n₂ * d₁), by apply_instance
instance decidable_eq_rat : decidable_eq ℚ :=
by dunfold rat; apply_instance
instance field_rat : discrete_field ℚ :=
{ zero := rat.zero,
add := rat.add,
neg := rat.neg,
one := rat.one,
mul := rat.mul,
inv := rat.inv,
zero_add := rat.zero_add,
add_zero := rat.add_zero,
add_comm := rat.add_comm,
add_assoc := rat.add_assoc,
add_left_neg := rat.add_left_neg,
mul_one := rat.mul_one,
one_mul := rat.one_mul,
mul_comm := rat.mul_comm,
mul_assoc := rat.mul_assoc,
left_distrib := rat.mul_add,
right_distrib := rat.add_mul,
zero_ne_one := rat.zero_ne_one,
mul_inv_cancel := rat.mul_inv_cancel,
inv_mul_cancel := rat.inv_mul_cancel,
has_decidable_eq := by apply_instance,
inv_zero := quotient.sound rfl }
private def nonneg' : rat.num_denum → Prop
| ⟨n₁, ⟨d₁, h₁⟩⟩ := 0 ≤ n₁
protected def nonneg : ℚ → Prop :=
quotient.lift nonneg' $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩ (h : n₁ * d₂ = n₂ * d₁),
propext $ calc (0 ≤ n₁) ↔ (0 ≤ n₁ * d₂) : (mul_nonneg_iff_right_nonneg_of_pos h₂)^.symm
... ↔ (0 ≤ n₂ * d₁) : by rw h
... ↔ (0 ≤ n₂) : mul_nonneg_iff_right_nonneg_of_pos h₁
protected def nonneg_add : rat.nonneg a → rat.nonneg b → rat.nonneg (a + b) :=
quotient.induction_on₂ a b $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩ _ _,
add_nonneg (mul_nonneg ‹0 ≤ n₁› (le_of_lt ‹0 < d₂›)) (mul_nonneg ‹0 ≤ n₂› (le_of_lt ‹0 < d₁›))
protected def nonneg_mul : rat.nonneg a → rat.nonneg b → rat.nonneg (a * b) :=
quotient.induction_on₂ a b $ λ⟨n₁, ⟨d₁, h₁⟩⟩ ⟨n₂, ⟨d₂, h₂⟩⟩ _ _,
mul_nonneg ‹0 ≤ n₁› ‹0 ≤ n₂›
protected def nonneg_antisymm : rat.nonneg a → rat.nonneg (-a) → a = 0 :=
quotient.induction_on a $ λ⟨n₁, ⟨d₁, h₁⟩⟩ (h₂ : 0 ≤ n₁) (h₃ : 0 ≤ -n₁), quotient.sound $
le_antisymm (by simp; exact le_neg_of_le_neg h₃) (by simp; exact h₂)
protected def nonneg_total : rat.nonneg a ∨ rat.nonneg (-a) :=
quotient.induction_on a $ λ⟨n₁, ⟨d₁, h₁⟩⟩,
show 0 ≤ n₁ ∨ 0 ≤ -n₁,
from or.imp_right neg_nonneg_of_nonpos (le_total 0 n₁)
@[instance]
def decidable_nonneg : decidable (rat.nonneg a) :=
quotient.rec_on_subsingleton a
(λ⟨n, ⟨d, h⟩⟩, if h : 0 ≤ n then is_true h else is_false h)
protected def le (a b : ℚ) := rat.nonneg (b - a)
instance : has_le ℚ := ⟨rat.le⟩
protected lemma le_refl : a ≤ a :=
show rat.nonneg (a - a), begin rw [sub_self], exact le_refl (0 : int) end
instance : linear_strong_order_pair ℚ :=
{ le := rat.le,
lt := λa b, a ≤ b ∧ a ≠ b,
le_iff_lt_or_eq := assume a b,
⟨assume : a ≤ b, if h : a = b then or.inr h else or.inl ⟨this, h⟩,
or.rec and.left (assume : a = b, show a ≤ b, from this ▸ rat.le_refl _)⟩,
lt_irrefl := assume a ⟨_, h⟩, h rfl,
le_refl := rat.le_refl,
le_trans := assume a b c h_ab h_bc,
have rat.nonneg (b - a + (c - b)),
from rat.nonneg_add _ _ h_ab h_bc,
show rat.nonneg (c - a),
by simp at this; assumption,
le_antisymm := assume a b h_ab h_ba,
have a = - - b,
from eq_neg_of_add_eq_zero $ rat.nonneg_antisymm _ h_ba (by simp; assumption),
by rw neg_neg at this; assumption,
le_total := assume a b,
have rat.nonneg (b - a) ∨ rat.nonneg (- (b - a)),
from rat.nonneg_total _,
by rw neg_sub at this; assumption }
protected def zero_le_of_nonneg : rat.nonneg a → 0 ≤ a :=
quotient.induction_on a $ assume ⟨n, ⟨d, h⟩⟩ _, show 0 ≤ n * 1 + (- 0) * d,
by simp; assumption
protected def nonneg_of_zero_le : 0 ≤ a → rat.nonneg a :=
quotient.induction_on a $ assume ⟨n, ⟨d, h⟩⟩, assume : 0 ≤ n * 1 + (- 0) * d,
by simp at this; assumption
instance : discrete_linear_ordered_field ℚ :=
{ rat.field_rat with
le := (≤),
lt := (<),
le_refl := le_refl,
le_trans := assume a b c, le_trans,
le_antisymm := assume a b, le_antisymm,
le_total := le_total,
le_iff_lt_or_eq := assume a b, le_iff_lt_or_eq,
lt_irrefl := lt_irrefl,
le_of_lt := assume a b, le_of_lt,
lt_of_lt_of_le := assume a b c, lt_of_lt_of_le,
lt_of_le_of_lt := assume a b c, lt_of_le_of_lt,
zero_lt_one := ⟨rat.zero_le_of_nonneg _ (@zero_le_one int _), zero_ne_one⟩,
add_le_add_left := assume a b (h_ab : rat.nonneg (b - a)) c,
show rat.nonneg ((c + b) - (c + a)), by rw add_sub_add_left_eq_sub; assumption,
add_lt_add_left := assume a b ⟨a_le_b, a_ne_b⟩ c,
show rat.nonneg ((c + b) - (c + a)) ∧ c + a ≠ c + b,
by rw [add_sub_add_left_eq_sub]; exact ⟨a_le_b, not_antimono a_ne_b add_left_cancel⟩,
mul_nonneg := assume a b ha hb, rat.zero_le_of_nonneg _ $
rat.nonneg_mul _ _ (rat.nonneg_of_zero_le a ha) (rat.nonneg_of_zero_le b hb),
mul_pos := assume a b ⟨nn_a, a_ne_zero⟩ ⟨nn_b, b_ne_zero⟩,
⟨rat.zero_le_of_nonneg _ $ rat.nonneg_mul _ _
(rat.nonneg_of_zero_le a nn_a) (rat.nonneg_of_zero_le b nn_b),
(mul_ne_zero a_ne_zero^.symm b_ne_zero^.symm)^.symm⟩,
decidable_eq := by apply_instance,
decidable_le := assume a b, rat.decidable_nonneg (b - a),
decidable_lt := assume a b, show decidable (rat.nonneg (b - a) ∧ a ≠ b), by apply_instance }
end rat
|
5175a12b43bede607c7f5f1be5203e255fd61886 | 592ee40978ac7604005a4e0d35bbc4b467389241 | /Library/generated/mathscheme-lean/MultSemigroup.lean | 1dbafa34b5c2f34d07fe9747e45fd53ee666a106 | [] | no_license | ysharoda/Deriving-Definitions | 3e149e6641fae440badd35ac110a0bd705a49ad2 | dfecb27572022de3d4aa702cae8db19957523a59 | refs/heads/master | 1,679,127,857,700 | 1,615,939,007,000 | 1,615,939,007,000 | 229,785,731 | 4 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,216 | lean | import init.data.nat.basic
import init.data.fin.basic
import data.vector
import .Prelude
open Staged
open nat
open fin
open vector
section MultSemigroup
structure MultSemigroup (A : Type) : Type :=
(times : (A → (A → A)))
(associative_times : (∀ {x y z : A} , (times (times x y) z) = (times x (times y z))))
open MultSemigroup
structure Sig (AS : Type) : Type :=
(timesS : (AS → (AS → AS)))
structure Product (A : Type) : Type :=
(timesP : ((Prod A A) → ((Prod A A) → (Prod A A))))
(associative_timesP : (∀ {xP yP zP : (Prod A A)} , (timesP (timesP xP yP) zP) = (timesP xP (timesP yP zP))))
structure Hom {A1 : Type} {A2 : Type} (Mu1 : (MultSemigroup A1)) (Mu2 : (MultSemigroup A2)) : Type :=
(hom : (A1 → A2))
(pres_times : (∀ {x1 x2 : A1} , (hom ((times Mu1) x1 x2)) = ((times Mu2) (hom x1) (hom x2))))
structure RelInterp {A1 : Type} {A2 : Type} (Mu1 : (MultSemigroup A1)) (Mu2 : (MultSemigroup A2)) : Type 1 :=
(interp : (A1 → (A2 → Type)))
(interp_times : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((times Mu1) x1 x2) ((times Mu2) y1 y2))))))
inductive MultSemigroupTerm : Type
| timesL : (MultSemigroupTerm → (MultSemigroupTerm → MultSemigroupTerm))
open MultSemigroupTerm
inductive ClMultSemigroupTerm (A : Type) : Type
| sing : (A → ClMultSemigroupTerm)
| timesCl : (ClMultSemigroupTerm → (ClMultSemigroupTerm → ClMultSemigroupTerm))
open ClMultSemigroupTerm
inductive OpMultSemigroupTerm (n : ℕ) : Type
| v : ((fin n) → OpMultSemigroupTerm)
| timesOL : (OpMultSemigroupTerm → (OpMultSemigroupTerm → OpMultSemigroupTerm))
open OpMultSemigroupTerm
inductive OpMultSemigroupTerm2 (n : ℕ) (A : Type) : Type
| v2 : ((fin n) → OpMultSemigroupTerm2)
| sing2 : (A → OpMultSemigroupTerm2)
| timesOL2 : (OpMultSemigroupTerm2 → (OpMultSemigroupTerm2 → OpMultSemigroupTerm2))
open OpMultSemigroupTerm2
def simplifyCl {A : Type} : ((ClMultSemigroupTerm A) → (ClMultSemigroupTerm A))
| (timesCl x1 x2) := (timesCl (simplifyCl x1) (simplifyCl x2))
| (sing x1) := (sing x1)
def simplifyOpB {n : ℕ} : ((OpMultSemigroupTerm n) → (OpMultSemigroupTerm n))
| (timesOL x1 x2) := (timesOL (simplifyOpB x1) (simplifyOpB x2))
| (v x1) := (v x1)
def simplifyOp {n : ℕ} {A : Type} : ((OpMultSemigroupTerm2 n A) → (OpMultSemigroupTerm2 n A))
| (timesOL2 x1 x2) := (timesOL2 (simplifyOp x1) (simplifyOp x2))
| (v2 x1) := (v2 x1)
| (sing2 x1) := (sing2 x1)
def evalB {A : Type} : ((MultSemigroup A) → (MultSemigroupTerm → A))
| Mu (timesL x1 x2) := ((times Mu) (evalB Mu x1) (evalB Mu x2))
def evalCl {A : Type} : ((MultSemigroup A) → ((ClMultSemigroupTerm A) → A))
| Mu (sing x1) := x1
| Mu (timesCl x1 x2) := ((times Mu) (evalCl Mu x1) (evalCl Mu x2))
def evalOpB {A : Type} {n : ℕ} : ((MultSemigroup A) → ((vector A n) → ((OpMultSemigroupTerm n) → A)))
| Mu vars (v x1) := (nth vars x1)
| Mu vars (timesOL x1 x2) := ((times Mu) (evalOpB Mu vars x1) (evalOpB Mu vars x2))
def evalOp {A : Type} {n : ℕ} : ((MultSemigroup A) → ((vector A n) → ((OpMultSemigroupTerm2 n A) → A)))
| Mu vars (v2 x1) := (nth vars x1)
| Mu vars (sing2 x1) := x1
| Mu vars (timesOL2 x1 x2) := ((times Mu) (evalOp Mu vars x1) (evalOp Mu vars x2))
def inductionB {P : (MultSemigroupTerm → Type)} : ((∀ (x1 x2 : MultSemigroupTerm) , ((P x1) → ((P x2) → (P (timesL x1 x2))))) → (∀ (x : MultSemigroupTerm) , (P x)))
| ptimesl (timesL x1 x2) := (ptimesl _ _ (inductionB ptimesl x1) (inductionB ptimesl x2))
def inductionCl {A : Type} {P : ((ClMultSemigroupTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClMultSemigroupTerm A)) , ((P x1) → ((P x2) → (P (timesCl x1 x2))))) → (∀ (x : (ClMultSemigroupTerm A)) , (P x))))
| psing ptimescl (sing x1) := (psing x1)
| psing ptimescl (timesCl x1 x2) := (ptimescl _ _ (inductionCl psing ptimescl x1) (inductionCl psing ptimescl x2))
def inductionOpB {n : ℕ} {P : ((OpMultSemigroupTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpMultSemigroupTerm n)) , ((P x1) → ((P x2) → (P (timesOL x1 x2))))) → (∀ (x : (OpMultSemigroupTerm n)) , (P x))))
| pv ptimesol (v x1) := (pv x1)
| pv ptimesol (timesOL x1 x2) := (ptimesol _ _ (inductionOpB pv ptimesol x1) (inductionOpB pv ptimesol x2))
def inductionOp {n : ℕ} {A : Type} {P : ((OpMultSemigroupTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpMultSemigroupTerm2 n A)) , ((P x1) → ((P x2) → (P (timesOL2 x1 x2))))) → (∀ (x : (OpMultSemigroupTerm2 n A)) , (P x)))))
| pv2 psing2 ptimesol2 (v2 x1) := (pv2 x1)
| pv2 psing2 ptimesol2 (sing2 x1) := (psing2 x1)
| pv2 psing2 ptimesol2 (timesOL2 x1 x2) := (ptimesol2 _ _ (inductionOp pv2 psing2 ptimesol2 x1) (inductionOp pv2 psing2 ptimesol2 x2))
def stageB : (MultSemigroupTerm → (Staged MultSemigroupTerm))
| (timesL x1 x2) := (stage2 timesL (codeLift2 timesL) (stageB x1) (stageB x2))
def stageCl {A : Type} : ((ClMultSemigroupTerm A) → (Staged (ClMultSemigroupTerm A)))
| (sing x1) := (Now (sing x1))
| (timesCl x1 x2) := (stage2 timesCl (codeLift2 timesCl) (stageCl x1) (stageCl x2))
def stageOpB {n : ℕ} : ((OpMultSemigroupTerm n) → (Staged (OpMultSemigroupTerm n)))
| (v x1) := (const (code (v x1)))
| (timesOL x1 x2) := (stage2 timesOL (codeLift2 timesOL) (stageOpB x1) (stageOpB x2))
def stageOp {n : ℕ} {A : Type} : ((OpMultSemigroupTerm2 n A) → (Staged (OpMultSemigroupTerm2 n A)))
| (sing2 x1) := (Now (sing2 x1))
| (v2 x1) := (const (code (v2 x1)))
| (timesOL2 x1 x2) := (stage2 timesOL2 (codeLift2 timesOL2) (stageOp x1) (stageOp x2))
structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type :=
(timesT : ((Repr A) → ((Repr A) → (Repr A))))
end MultSemigroup |
1b6a4ed90570a96d8a878ab75255bb90dafeefbd | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/order/bounds.lean | b21866f407970364cdb9546c37214e8e62bdd738 | [
"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 | 37,669 | 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, Yury Kudryashov
-/
import data.set.intervals.basic
/-!
# Upper / lower bounds
In this file we define:
* `upper_bounds`, `lower_bounds` : the set of upper bounds (resp., lower bounds) of a set;
* `bdd_above s`, `bdd_below s` : the set `s` is bounded above (resp., below), i.e., the set of upper
(resp., lower) bounds of `s` is nonempty;
* `is_least s a`, `is_greatest s a` : `a` is a least (resp., greatest) element of `s`;
for a partial order, it is unique if exists;
* `is_lub s a`, `is_glb s a` : `a` is a least upper bound (resp., a greatest lower bound)
of `s`; for a partial order, it is unique if exists.
We also prove various lemmas about monotonicity, behaviour under `∪`, `∩`, `insert`, and provide
formulas for `∅`, `univ`, and intervals.
-/
open set order_dual (to_dual of_dual)
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x}
section
variables [preorder α] [preorder β] {s t : set α} {a b : α}
/-!
### Definitions
-/
/-- The set of upper bounds of a set. -/
def upper_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → a ≤ x }
/-- The set of lower bounds of a set. -/
def lower_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → x ≤ a }
/-- A set is bounded above if there exists an upper bound. -/
def bdd_above (s : set α) := (upper_bounds s).nonempty
/-- A set is bounded below if there exists a lower bound. -/
def bdd_below (s : set α) := (lower_bounds s).nonempty
/-- `a` is a least element of a set `s`; for a partial order, it is unique if exists. -/
def is_least (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ lower_bounds s
/-- `a` is a greatest element of a set `s`; for a partial order, it is unique if exists -/
def is_greatest (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ upper_bounds s
/-- `a` is a least upper bound of a set `s`; for a partial order, it is unique if exists. -/
def is_lub (s : set α) : α → Prop := is_least (upper_bounds s)
/-- `a` is a greatest lower bound of a set `s`; for a partial order, it is unique if exists. -/
def is_glb (s : set α) : α → Prop := is_greatest (lower_bounds s)
lemma mem_upper_bounds : a ∈ upper_bounds s ↔ ∀ x ∈ s, x ≤ a := iff.rfl
lemma mem_lower_bounds : a ∈ lower_bounds s ↔ ∀ x ∈ s, a ≤ x := iff.rfl
/-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` such that `x`
is not greater than or equal to `y`. This version only assumes `preorder` structure and uses
`¬(y ≤ x)`. A version for linear orders is called `not_bdd_above_iff`. -/
lemma not_bdd_above_iff' : ¬bdd_above s ↔ ∀ x, ∃ y ∈ s, ¬(y ≤ x) :=
by simp [bdd_above, upper_bounds, set.nonempty]
/-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` such that `x`
is not less than or equal to `y`. This version only assumes `preorder` structure and uses
`¬(x ≤ y)`. A version for linear orders is called `not_bdd_below_iff`. -/
lemma not_bdd_below_iff' : ¬bdd_below s ↔ ∀ x, ∃ y ∈ s, ¬(x ≤ y) :=
@not_bdd_above_iff' (order_dual α) _ _
/-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` that is greater
than `x`. A version for preorders is called `not_bdd_above_iff'`. -/
lemma not_bdd_above_iff {α : Type*} [linear_order α] {s : set α} :
¬bdd_above s ↔ ∀ x, ∃ y ∈ s, x < y :=
by simp only [not_bdd_above_iff', not_le]
/-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` that is less
than `x`. A version for preorders is called `not_bdd_below_iff'`. -/
lemma not_bdd_below_iff {α : Type*} [linear_order α] {s : set α} :
¬bdd_below s ↔ ∀ x, ∃ y ∈ s, y < x :=
@not_bdd_above_iff (order_dual α) _ _
/-!
### Monotonicity
-/
lemma upper_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) :
upper_bounds t ⊆ upper_bounds s :=
λ b hb x h, hb $ hst h
lemma lower_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) :
lower_bounds t ⊆ lower_bounds s :=
λ b hb x h, hb $ hst h
lemma upper_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : a ∈ upper_bounds s → b ∈ upper_bounds s :=
λ ha x h, le_trans (ha h) hab
lemma lower_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : b ∈ lower_bounds s → a ∈ lower_bounds s :=
λ hb x h, le_trans hab (hb h)
lemma upper_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) :
a ∈ upper_bounds t → b ∈ upper_bounds s :=
λ ha, upper_bounds_mono_set hst $ upper_bounds_mono_mem hab ha
lemma lower_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) :
b ∈ lower_bounds t → a ∈ lower_bounds s :=
λ hb, lower_bounds_mono_set hst $ lower_bounds_mono_mem hab hb
/-- If `s ⊆ t` and `t` is bounded above, then so is `s`. -/
lemma bdd_above.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_above t → bdd_above s :=
nonempty.mono $ upper_bounds_mono_set h
/-- If `s ⊆ t` and `t` is bounded below, then so is `s`. -/
lemma bdd_below.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_below t → bdd_below s :=
nonempty.mono $ lower_bounds_mono_set h
/-- If `a` is a least upper bound for sets `s` and `p`, then it is a least upper bound for any
set `t`, `s ⊆ t ⊆ p`. -/
lemma is_lub.of_subset_of_superset {s t p : set α} (hs : is_lub s a) (hp : is_lub p a)
(hst : s ⊆ t) (htp : t ⊆ p) : is_lub t a :=
⟨upper_bounds_mono_set htp hp.1, lower_bounds_mono_set (upper_bounds_mono_set hst) hs.2⟩
/-- If `a` is a greatest lower bound for sets `s` and `p`, then it is a greater lower bound for any
set `t`, `s ⊆ t ⊆ p`. -/
lemma is_glb.of_subset_of_superset {s t p : set α} (hs : is_glb s a) (hp : is_glb p a)
(hst : s ⊆ t) (htp : t ⊆ p) : is_glb t a :=
@is_lub.of_subset_of_superset (order_dual α) _ a s t p hs hp hst htp
lemma is_least.mono (ha : is_least s a) (hb : is_least t b) (hst : s ⊆ t) : b ≤ a :=
hb.2 (hst ha.1)
lemma is_greatest.mono (ha : is_greatest s a) (hb : is_greatest t b) (hst : s ⊆ t) : a ≤ b :=
hb.2 (hst ha.1)
lemma is_lub.mono (ha : is_lub s a) (hb : is_lub t b) (hst : s ⊆ t) : a ≤ b :=
hb.mono ha $ upper_bounds_mono_set hst
lemma is_glb.mono (ha : is_glb s a) (hb : is_glb t b) (hst : s ⊆ t) : b ≤ a :=
hb.mono ha $ lower_bounds_mono_set hst
lemma subset_lower_bounds_upper_bounds (s : set α) : s ⊆ lower_bounds (upper_bounds s) :=
λ x hx y hy, hy hx
lemma subset_upper_bounds_lower_bounds (s : set α) : s ⊆ upper_bounds (lower_bounds s) :=
λ x hx y hy, hy hx
lemma set.nonempty.bdd_above_lower_bounds (hs : s.nonempty) : bdd_above (lower_bounds s) :=
hs.mono (subset_upper_bounds_lower_bounds s)
lemma set.nonempty.bdd_below_upper_bounds (hs : s.nonempty) : bdd_below (upper_bounds s) :=
hs.mono (subset_lower_bounds_upper_bounds s)
/-!
### Conversions
-/
lemma is_least.is_glb (h : is_least s a) : is_glb s a := ⟨h.2, λ b hb, hb h.1⟩
lemma is_greatest.is_lub (h : is_greatest s a) : is_lub s a := ⟨h.2, λ b hb, hb h.1⟩
lemma is_lub.upper_bounds_eq (h : is_lub s a) : upper_bounds s = Ici a :=
set.ext $ λ b, ⟨λ hb, h.2 hb, λ hb, upper_bounds_mono_mem hb h.1⟩
lemma is_glb.lower_bounds_eq (h : is_glb s a) : lower_bounds s = Iic a :=
@is_lub.upper_bounds_eq (order_dual α) _ _ _ h
lemma is_least.lower_bounds_eq (h : is_least s a) : lower_bounds s = Iic a :=
h.is_glb.lower_bounds_eq
lemma is_greatest.upper_bounds_eq (h : is_greatest s a) : upper_bounds s = Ici a :=
h.is_lub.upper_bounds_eq
lemma is_lub_le_iff (h : is_lub s a) : a ≤ b ↔ b ∈ upper_bounds s :=
by { rw h.upper_bounds_eq, refl }
lemma le_is_glb_iff (h : is_glb s a) : b ≤ a ↔ b ∈ lower_bounds s :=
by { rw h.lower_bounds_eq, refl }
lemma is_lub_iff_le_iff : is_lub s a ↔ ∀ b, a ≤ b ↔ b ∈ upper_bounds s :=
⟨λ h b, is_lub_le_iff h, λ H, ⟨(H _).1 le_rfl, λ b hb, (H b).2 hb⟩⟩
lemma is_glb_iff_le_iff : is_glb s a ↔ ∀ b, b ≤ a ↔ b ∈ lower_bounds s :=
@is_lub_iff_le_iff (order_dual α) _ _ _
/-- If `s` has a least upper bound, then it is bounded above. -/
lemma is_lub.bdd_above (h : is_lub s a) : bdd_above s := ⟨a, h.1⟩
/-- If `s` has a greatest lower bound, then it is bounded below. -/
lemma is_glb.bdd_below (h : is_glb s a) : bdd_below s := ⟨a, h.1⟩
/-- If `s` has a greatest element, then it is bounded above. -/
lemma is_greatest.bdd_above (h : is_greatest s a) : bdd_above s := ⟨a, h.2⟩
/-- If `s` has a least element, then it is bounded below. -/
lemma is_least.bdd_below (h : is_least s a) : bdd_below s := ⟨a, h.2⟩
lemma is_least.nonempty (h : is_least s a) : s.nonempty := ⟨a, h.1⟩
lemma is_greatest.nonempty (h : is_greatest s a) : s.nonempty := ⟨a, h.1⟩
/-!
### Union and intersection
-/
@[simp] lemma upper_bounds_union : upper_bounds (s ∪ t) = upper_bounds s ∩ upper_bounds t :=
subset.antisymm
(λ b hb, ⟨λ x hx, hb (or.inl hx), λ x hx, hb (or.inr hx)⟩)
(λ b hb x hx, hx.elim (λ hs, hb.1 hs) (λ ht, hb.2 ht))
@[simp] lemma lower_bounds_union : lower_bounds (s ∪ t) = lower_bounds s ∩ lower_bounds t :=
@upper_bounds_union (order_dual α) _ s t
lemma union_upper_bounds_subset_upper_bounds_inter :
upper_bounds s ∪ upper_bounds t ⊆ upper_bounds (s ∩ t) :=
union_subset
(upper_bounds_mono_set $ inter_subset_left _ _)
(upper_bounds_mono_set $ inter_subset_right _ _)
lemma union_lower_bounds_subset_lower_bounds_inter :
lower_bounds s ∪ lower_bounds t ⊆ lower_bounds (s ∩ t) :=
@union_upper_bounds_subset_upper_bounds_inter (order_dual α) _ s t
lemma is_least_union_iff {a : α} {s t : set α} :
is_least (s ∪ t) a ↔ (is_least s a ∧ a ∈ lower_bounds t ∨ a ∈ lower_bounds s ∧ is_least t a) :=
by simp [is_least, lower_bounds_union, or_and_distrib_right, and_comm (a ∈ t), and_assoc]
lemma is_greatest_union_iff :
is_greatest (s ∪ t) a ↔ (is_greatest s a ∧ a ∈ upper_bounds t ∨
a ∈ upper_bounds s ∧ is_greatest t a) :=
@is_least_union_iff (order_dual α) _ a s t
/-- If `s` is bounded, then so is `s ∩ t` -/
lemma bdd_above.inter_of_left (h : bdd_above s) : bdd_above (s ∩ t) :=
h.mono $ inter_subset_left s t
/-- If `t` is bounded, then so is `s ∩ t` -/
lemma bdd_above.inter_of_right (h : bdd_above t) : bdd_above (s ∩ t) :=
h.mono $ inter_subset_right s t
/-- If `s` is bounded, then so is `s ∩ t` -/
lemma bdd_below.inter_of_left (h : bdd_below s) : bdd_below (s ∩ t) :=
h.mono $ inter_subset_left s t
/-- If `t` is bounded, then so is `s ∩ t` -/
lemma bdd_below.inter_of_right (h : bdd_below t) : bdd_below (s ∩ t) :=
h.mono $ inter_subset_right s t
/-- If `s` and `t` are bounded above sets in a `semilattice_sup`, then so is `s ∪ t`. -/
lemma bdd_above.union [semilattice_sup γ] {s t : set γ} :
bdd_above s → bdd_above t → bdd_above (s ∪ t) :=
begin
rintros ⟨bs, hs⟩ ⟨bt, ht⟩,
use bs ⊔ bt,
rw upper_bounds_union,
exact ⟨upper_bounds_mono_mem le_sup_left hs,
upper_bounds_mono_mem le_sup_right ht⟩
end
/-- The union of two sets is bounded above if and only if each of the sets is. -/
lemma bdd_above_union [semilattice_sup γ] {s t : set γ} :
bdd_above (s ∪ t) ↔ bdd_above s ∧ bdd_above t :=
⟨λ h, ⟨h.mono $ subset_union_left s t, h.mono $ subset_union_right s t⟩,
λ h, h.1.union h.2⟩
lemma bdd_below.union [semilattice_inf γ] {s t : set γ} :
bdd_below s → bdd_below t → bdd_below (s ∪ t) :=
@bdd_above.union (order_dual γ) _ s t
/--The union of two sets is bounded above if and only if each of the sets is.-/
lemma bdd_below_union [semilattice_inf γ] {s t : set γ} :
bdd_below (s ∪ t) ↔ bdd_below s ∧ bdd_below t :=
@bdd_above_union (order_dual γ) _ s t
/-- If `a` is the least upper bound of `s` and `b` is the least upper bound of `t`,
then `a ⊔ b` is the least upper bound of `s ∪ t`. -/
lemma is_lub.union [semilattice_sup γ] {a b : γ} {s t : set γ}
(hs : is_lub s a) (ht : is_lub t b) :
is_lub (s ∪ t) (a ⊔ b) :=
⟨λ c h, h.cases_on (λ h, le_sup_of_le_left $ hs.left h) (λ h, le_sup_of_le_right $ ht.left h),
assume c hc, sup_le
(hs.right $ assume d hd, hc $ or.inl hd) (ht.right $ assume d hd, hc $ or.inr hd)⟩
/-- If `a` is the greatest lower bound of `s` and `b` is the greatest lower bound of `t`,
then `a ⊓ b` is the greatest lower bound of `s ∪ t`. -/
lemma is_glb.union [semilattice_inf γ] {a₁ a₂ : γ} {s t : set γ}
(hs : is_glb s a₁) (ht : is_glb t a₂) :
is_glb (s ∪ t) (a₁ ⊓ a₂) :=
@is_lub.union (order_dual γ) _ _ _ _ _ hs ht
/-- If `a` is the least element of `s` and `b` is the least element of `t`,
then `min a b` is the least element of `s ∪ t`. -/
lemma is_least.union [linear_order γ] {a b : γ} {s t : set γ}
(ha : is_least s a) (hb : is_least t b) : is_least (s ∪ t) (min a b) :=
⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1],
(ha.is_glb.union hb.is_glb).1⟩
/-- If `a` is the greatest element of `s` and `b` is the greatest element of `t`,
then `max a b` is the greatest element of `s ∪ t`. -/
lemma is_greatest.union [linear_order γ] {a b : γ} {s t : set γ}
(ha : is_greatest s a) (hb : is_greatest t b) : is_greatest (s ∪ t) (max a b) :=
⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1],
(ha.is_lub.union hb.is_lub).1⟩
lemma is_lub.inter_Ici_of_mem [linear_order γ] {s : set γ} {a b : γ} (ha : is_lub s a)
(hb : b ∈ s) : is_lub (s ∩ Ici b) a :=
⟨λ x hx, ha.1 hx.1, λ c hc, have hbc : b ≤ c, from hc ⟨hb, le_rfl⟩,
ha.2 $ λ x hx, (le_total x b).elim (λ hxb, hxb.trans hbc) $ λ hbx, hc ⟨hx, hbx⟩⟩
lemma is_glb.inter_Iic_of_mem [linear_order γ] {s : set γ} {a b : γ} (ha : is_glb s a)
(hb : b ∈ s) : is_glb (s ∩ Iic b) a :=
@is_lub.inter_Ici_of_mem (order_dual γ) _ _ _ _ ha hb
/-!
### Specific sets
#### Unbounded intervals
-/
lemma is_least_Ici : is_least (Ici a) a := ⟨left_mem_Ici, λ x, id⟩
lemma is_greatest_Iic : is_greatest (Iic a) a := ⟨right_mem_Iic, λ x, id⟩
lemma is_lub_Iic : is_lub (Iic a) a := is_greatest_Iic.is_lub
lemma is_glb_Ici : is_glb (Ici a) a := is_least_Ici.is_glb
lemma upper_bounds_Iic : upper_bounds (Iic a) = Ici a := is_lub_Iic.upper_bounds_eq
lemma lower_bounds_Ici : lower_bounds (Ici a) = Iic a := is_glb_Ici.lower_bounds_eq
lemma bdd_above_Iic : bdd_above (Iic a) := is_lub_Iic.bdd_above
lemma bdd_below_Ici : bdd_below (Ici a) := is_glb_Ici.bdd_below
lemma bdd_above_Iio : bdd_above (Iio a) := ⟨a, λ x hx, le_of_lt hx⟩
lemma bdd_below_Ioi : bdd_below (Ioi a) := ⟨a, λ x hx, le_of_lt hx⟩
section
variables [linear_order γ] [densely_ordered γ]
lemma is_lub_Iio {a : γ} : is_lub (Iio a) a :=
⟨λ x hx, le_of_lt hx, λ y hy, le_of_forall_ge_of_dense hy⟩
lemma is_glb_Ioi {a : γ} : is_glb (Ioi a) a := @is_lub_Iio (order_dual γ) _ _ a
lemma upper_bounds_Iio {a : γ} : upper_bounds (Iio a) = Ici a := is_lub_Iio.upper_bounds_eq
lemma lower_bounds_Ioi {a : γ} : lower_bounds (Ioi a) = Iic a := is_glb_Ioi.lower_bounds_eq
end
/-!
#### Singleton
-/
lemma is_greatest_singleton : is_greatest {a} a :=
⟨mem_singleton a, λ x hx, le_of_eq $ eq_of_mem_singleton hx⟩
lemma is_least_singleton : is_least {a} a :=
@is_greatest_singleton (order_dual α) _ a
lemma is_lub_singleton : is_lub {a} a := is_greatest_singleton.is_lub
lemma is_glb_singleton : is_glb {a} a := is_least_singleton.is_glb
lemma bdd_above_singleton : bdd_above ({a} : set α) := is_lub_singleton.bdd_above
lemma bdd_below_singleton : bdd_below ({a} : set α) := is_glb_singleton.bdd_below
@[simp] lemma upper_bounds_singleton : upper_bounds {a} = Ici a := is_lub_singleton.upper_bounds_eq
@[simp] lemma lower_bounds_singleton : lower_bounds {a} = Iic a := is_glb_singleton.lower_bounds_eq
/-!
#### Bounded intervals
-/
lemma bdd_above_Icc : bdd_above (Icc a b) := ⟨b, λ _, and.right⟩
lemma bdd_below_Icc : bdd_below (Icc a b) := ⟨a, λ _, and.left⟩
lemma bdd_above_Ico : bdd_above (Ico a b) := bdd_above_Icc.mono Ico_subset_Icc_self
lemma bdd_below_Ico : bdd_below (Ico a b) := bdd_below_Icc.mono Ico_subset_Icc_self
lemma bdd_above_Ioc : bdd_above (Ioc a b) := bdd_above_Icc.mono Ioc_subset_Icc_self
lemma bdd_below_Ioc : bdd_below (Ioc a b) := bdd_below_Icc.mono Ioc_subset_Icc_self
lemma bdd_above_Ioo : bdd_above (Ioo a b) := bdd_above_Icc.mono Ioo_subset_Icc_self
lemma bdd_below_Ioo : bdd_below (Ioo a b) := bdd_below_Icc.mono Ioo_subset_Icc_self
lemma is_greatest_Icc (h : a ≤ b) : is_greatest (Icc a b) b :=
⟨right_mem_Icc.2 h, λ x, and.right⟩
lemma is_lub_Icc (h : a ≤ b) : is_lub (Icc a b) b := (is_greatest_Icc h).is_lub
lemma upper_bounds_Icc (h : a ≤ b) : upper_bounds (Icc a b) = Ici b :=
(is_lub_Icc h).upper_bounds_eq
lemma is_least_Icc (h : a ≤ b) : is_least (Icc a b) a :=
⟨left_mem_Icc.2 h, λ x, and.left⟩
lemma is_glb_Icc (h : a ≤ b) : is_glb (Icc a b) a := (is_least_Icc h).is_glb
lemma lower_bounds_Icc (h : a ≤ b) : lower_bounds (Icc a b) = Iic a :=
(is_glb_Icc h).lower_bounds_eq
lemma is_greatest_Ioc (h : a < b) : is_greatest (Ioc a b) b :=
⟨right_mem_Ioc.2 h, λ x, and.right⟩
lemma is_lub_Ioc (h : a < b) : is_lub (Ioc a b) b :=
(is_greatest_Ioc h).is_lub
lemma upper_bounds_Ioc (h : a < b) : upper_bounds (Ioc a b) = Ici b :=
(is_lub_Ioc h).upper_bounds_eq
lemma is_least_Ico (h : a < b) : is_least (Ico a b) a :=
⟨left_mem_Ico.2 h, λ x, and.left⟩
lemma is_glb_Ico (h : a < b) : is_glb (Ico a b) a :=
(is_least_Ico h).is_glb
lemma lower_bounds_Ico (h : a < b) : lower_bounds (Ico a b) = Iic a :=
(is_glb_Ico h).lower_bounds_eq
section
variables [semilattice_sup γ] [densely_ordered γ]
lemma is_glb_Ioo {a b : γ} (h : a < b) :
is_glb (Ioo a b) a :=
⟨λ x hx, hx.1.le, λ x hx,
begin
cases eq_or_lt_of_le (le_sup_right : a ≤ x ⊔ a) with h₁ h₂,
{ exact h₁.symm ▸ le_sup_left },
obtain ⟨y, lty, ylt⟩ := exists_between h₂,
apply (not_lt_of_le (sup_le (hx ⟨lty, ylt.trans_le (sup_le _ h.le)⟩) lty.le) ylt).elim,
obtain ⟨u, au, ub⟩ := exists_between h,
apply (hx ⟨au, ub⟩).trans ub.le,
end⟩
lemma lower_bounds_Ioo {a b : γ} (hab : a < b) : lower_bounds (Ioo a b) = Iic a :=
(is_glb_Ioo hab).lower_bounds_eq
lemma is_glb_Ioc {a b : γ} (hab : a < b) : is_glb (Ioc a b) a :=
(is_glb_Ioo hab).of_subset_of_superset (is_glb_Icc hab.le) Ioo_subset_Ioc_self Ioc_subset_Icc_self
lemma lower_bound_Ioc {a b : γ} (hab : a < b) : lower_bounds (Ioc a b) = Iic a :=
(is_glb_Ioc hab).lower_bounds_eq
end
section
variables [semilattice_inf γ] [densely_ordered γ]
lemma is_lub_Ioo {a b : γ} (hab : a < b) : is_lub (Ioo a b) b :=
by simpa only [dual_Ioo] using is_glb_Ioo hab.dual
lemma upper_bounds_Ioo {a b : γ} (hab : a < b) : upper_bounds (Ioo a b) = Ici b :=
(is_lub_Ioo hab).upper_bounds_eq
lemma is_lub_Ico {a b : γ} (hab : a < b) : is_lub (Ico a b) b :=
by simpa only [dual_Ioc] using is_glb_Ioc hab.dual
lemma upper_bounds_Ico {a b : γ} (hab : a < b) : upper_bounds (Ico a b) = Ici b :=
(is_lub_Ico hab).upper_bounds_eq
end
lemma bdd_below_iff_subset_Ici : bdd_below s ↔ ∃ a, s ⊆ Ici a := iff.rfl
lemma bdd_above_iff_subset_Iic : bdd_above s ↔ ∃ a, s ⊆ Iic a := iff.rfl
lemma bdd_below_bdd_above_iff_subset_Icc : bdd_below s ∧ bdd_above s ↔ ∃ a b, s ⊆ Icc a b :=
by simp only [Ici_inter_Iic.symm, subset_inter_iff, bdd_below_iff_subset_Ici,
bdd_above_iff_subset_Iic, exists_and_distrib_left, exists_and_distrib_right]
/-!
#### Univ
-/
lemma is_greatest_univ [order_top γ] : is_greatest (univ : set γ) ⊤ :=
⟨mem_univ _, λ x hx, le_top⟩
@[simp] lemma order_top.upper_bounds_univ [order_top γ] : upper_bounds (univ : set γ) = {⊤} :=
by rw [is_greatest_univ.upper_bounds_eq, Ici_top]
lemma is_lub_univ [order_top γ] : is_lub (univ : set γ) ⊤ :=
is_greatest_univ.is_lub
@[simp] lemma order_bot.lower_bounds_univ [order_bot γ] : lower_bounds (univ : set γ) = {⊥} :=
@order_top.upper_bounds_univ (order_dual γ) _
lemma is_least_univ [order_bot γ] : is_least (univ : set γ) ⊥ :=
@is_greatest_univ (order_dual γ) _
lemma is_glb_univ [order_bot γ] : is_glb (univ : set γ) ⊥ :=
is_least_univ.is_glb
@[simp] lemma no_top_order.upper_bounds_univ [no_top_order α] : upper_bounds (univ : set α) = ∅ :=
eq_empty_of_subset_empty $ λ b hb, let ⟨x, hx⟩ := no_top b in
not_le_of_lt hx (hb trivial)
@[simp] lemma no_bot_order.lower_bounds_univ [no_bot_order α] : lower_bounds (univ : set α) = ∅ :=
@no_top_order.upper_bounds_univ (order_dual α) _ _
@[simp] lemma not_bdd_above_univ [no_top_order α] : ¬bdd_above (univ : set α) :=
by simp [bdd_above]
@[simp] lemma not_bdd_below_univ [no_bot_order α] : ¬bdd_below (univ : set α) :=
@not_bdd_above_univ (order_dual α) _ _
/-!
#### Empty set
-/
@[simp] lemma upper_bounds_empty : upper_bounds (∅ : set α) = univ :=
by simp only [upper_bounds, eq_univ_iff_forall, mem_set_of_eq, ball_empty_iff, forall_true_iff]
@[simp] lemma lower_bounds_empty : lower_bounds (∅ : set α) = univ :=
@upper_bounds_empty (order_dual α) _
@[simp] lemma bdd_above_empty [nonempty α] : bdd_above (∅ : set α) :=
by simp only [bdd_above, upper_bounds_empty, univ_nonempty]
@[simp] lemma bdd_below_empty [nonempty α] : bdd_below (∅ : set α) :=
by simp only [bdd_below, lower_bounds_empty, univ_nonempty]
lemma is_glb_empty [order_top γ] : is_glb ∅ (⊤:γ) :=
by simp only [is_glb, lower_bounds_empty, is_greatest_univ]
lemma is_lub_empty [order_bot γ] : is_lub ∅ (⊥:γ) :=
@is_glb_empty (order_dual γ) _
lemma is_lub.nonempty [no_bot_order α] (hs : is_lub s a) : s.nonempty :=
let ⟨a', ha'⟩ := no_bot a in
ne_empty_iff_nonempty.1 $ assume h,
have a ≤ a', from hs.right $ by simp only [h, upper_bounds_empty],
not_le_of_lt ha' this
lemma is_glb.nonempty [no_top_order α] (hs : is_glb s a) : s.nonempty :=
@is_lub.nonempty (order_dual α) _ _ _ _ hs
lemma nonempty_of_not_bdd_above [ha : nonempty α] (h : ¬bdd_above s) : s.nonempty :=
nonempty.elim ha $ λ x, (not_bdd_above_iff'.1 h x).imp $ λ a ha, ha.fst
lemma nonempty_of_not_bdd_below [ha : nonempty α] (h : ¬bdd_below s) : s.nonempty :=
@nonempty_of_not_bdd_above (order_dual α) _ _ _ h
/-!
#### insert
-/
/-- Adding a point to a set preserves its boundedness above. -/
@[simp] lemma bdd_above_insert [semilattice_sup γ] (a : γ) {s : set γ} :
bdd_above (insert a s) ↔ bdd_above s :=
by simp only [insert_eq, bdd_above_union, bdd_above_singleton, true_and]
lemma bdd_above.insert [semilattice_sup γ] (a : γ) {s : set γ} (hs : bdd_above s) :
bdd_above (insert a s) :=
(bdd_above_insert a).2 hs
/--Adding a point to a set preserves its boundedness below.-/
@[simp] lemma bdd_below_insert [semilattice_inf γ] (a : γ) {s : set γ} :
bdd_below (insert a s) ↔ bdd_below s :=
by simp only [insert_eq, bdd_below_union, bdd_below_singleton, true_and]
lemma bdd_below.insert [semilattice_inf γ] (a : γ) {s : set γ} (hs : bdd_below s) :
bdd_below (insert a s) :=
(bdd_below_insert a).2 hs
lemma is_lub.insert [semilattice_sup γ] (a) {b} {s : set γ} (hs : is_lub s b) :
is_lub (insert a s) (a ⊔ b) :=
by { rw insert_eq, exact is_lub_singleton.union hs }
lemma is_glb.insert [semilattice_inf γ] (a) {b} {s : set γ} (hs : is_glb s b) :
is_glb (insert a s) (a ⊓ b) :=
by { rw insert_eq, exact is_glb_singleton.union hs }
lemma is_greatest.insert [linear_order γ] (a) {b} {s : set γ} (hs : is_greatest s b) :
is_greatest (insert a s) (max a b) :=
by { rw insert_eq, exact is_greatest_singleton.union hs }
lemma is_least.insert [linear_order γ] (a) {b} {s : set γ} (hs : is_least s b) :
is_least (insert a s) (min a b) :=
by { rw insert_eq, exact is_least_singleton.union hs }
@[simp] lemma upper_bounds_insert (a : α) (s : set α) :
upper_bounds (insert a s) = Ici a ∩ upper_bounds s :=
by rw [insert_eq, upper_bounds_union, upper_bounds_singleton]
@[simp] lemma lower_bounds_insert (a : α) (s : set α) :
lower_bounds (insert a s) = Iic a ∩ lower_bounds s :=
by rw [insert_eq, lower_bounds_union, lower_bounds_singleton]
/-- When there is a global maximum, every set is bounded above. -/
@[simp] protected lemma order_top.bdd_above [order_top γ] (s : set γ) : bdd_above s :=
⟨⊤, assume a ha, order_top.le_top a⟩
/-- When there is a global minimum, every set is bounded below. -/
@[simp] protected lemma order_bot.bdd_below [order_bot γ] (s : set γ) : bdd_below s :=
⟨⊥, assume a ha, order_bot.bot_le a⟩
/-!
#### Pair
-/
lemma is_lub_pair [semilattice_sup γ] {a b : γ} : is_lub {a, b} (a ⊔ b) :=
is_lub_singleton.insert _
lemma is_glb_pair [semilattice_inf γ] {a b : γ} : is_glb {a, b} (a ⊓ b) :=
is_glb_singleton.insert _
lemma is_least_pair [linear_order γ] {a b : γ} : is_least {a, b} (min a b) :=
is_least_singleton.insert _
lemma is_greatest_pair [linear_order γ] {a b : γ} : is_greatest {a, b} (max a b) :=
is_greatest_singleton.insert _
/-!
#### Lower/upper bounds
-/
@[simp] lemma is_lub_lower_bounds : is_lub (lower_bounds s) a ↔ is_glb s a :=
⟨λ H, ⟨λ x hx, H.2 $ subset_upper_bounds_lower_bounds s hx, H.1⟩, is_greatest.is_lub⟩
@[simp] lemma is_glb_upper_bounds : is_glb (upper_bounds s) a ↔ is_lub s a :=
@is_lub_lower_bounds (order_dual α) _ _ _
end
/-!
### (In)equalities with the least upper bound and the greatest lower bound
-/
section preorder
variables [preorder α] {s : set α} {a b : α}
lemma lower_bounds_le_upper_bounds (ha : a ∈ lower_bounds s) (hb : b ∈ upper_bounds s) :
s.nonempty → a ≤ b
| ⟨c, hc⟩ := le_trans (ha hc) (hb hc)
lemma is_glb_le_is_lub (ha : is_glb s a) (hb : is_lub s b) (hs : s.nonempty) : a ≤ b :=
lower_bounds_le_upper_bounds ha.1 hb.1 hs
lemma is_lub_lt_iff (ha : is_lub s a) : a < b ↔ ∃ c ∈ upper_bounds s, c < b :=
⟨λ hb, ⟨a, ha.1, hb⟩, λ ⟨c, hcs, hcb⟩, lt_of_le_of_lt (ha.2 hcs) hcb⟩
lemma lt_is_glb_iff (ha : is_glb s a) : b < a ↔ ∃ c ∈ lower_bounds s, b < c :=
@is_lub_lt_iff (order_dual α) _ s _ _ ha
lemma le_of_is_lub_le_is_glb {x y} (ha : is_glb s a) (hb : is_lub s b) (hab : b ≤ a)
(hx : x ∈ s) (hy : y ∈ s) : x ≤ y :=
calc x ≤ b : hb.1 hx
... ≤ a : hab
... ≤ y : ha.1 hy
end preorder
section partial_order
variables [partial_order α] {s : set α} {a b : α}
lemma is_least.unique (Ha : is_least s a) (Hb : is_least s b) : a = b :=
le_antisymm (Ha.right Hb.left) (Hb.right Ha.left)
lemma is_least.is_least_iff_eq (Ha : is_least s a) : is_least s b ↔ a = b :=
iff.intro Ha.unique (assume h, h ▸ Ha)
lemma is_greatest.unique (Ha : is_greatest s a) (Hb : is_greatest s b) : a = b :=
le_antisymm (Hb.right Ha.left) (Ha.right Hb.left)
lemma is_greatest.is_greatest_iff_eq (Ha : is_greatest s a) : is_greatest s b ↔ a = b :=
iff.intro Ha.unique (assume h, h ▸ Ha)
lemma is_lub.unique (Ha : is_lub s a) (Hb : is_lub s b) : a = b :=
Ha.unique Hb
lemma is_glb.unique (Ha : is_glb s a) (Hb : is_glb s b) : a = b :=
Ha.unique Hb
lemma set.subsingleton_of_is_lub_le_is_glb (Ha : is_glb s a) (Hb : is_lub s b) (hab : b ≤ a) :
s.subsingleton :=
λ x hx y hy, le_antisymm (le_of_is_lub_le_is_glb Ha Hb hab hx hy)
(le_of_is_lub_le_is_glb Ha Hb hab hy hx)
lemma is_glb_lt_is_lub_of_ne (Ha : is_glb s a) (Hb : is_lub s b)
{x y} (Hx : x ∈ s) (Hy : y ∈ s) (Hxy : x ≠ y) :
a < b :=
lt_iff_le_not_le.2
⟨lower_bounds_le_upper_bounds Ha.1 Hb.1 ⟨x, Hx⟩,
λ hab, Hxy $ set.subsingleton_of_is_lub_le_is_glb Ha Hb hab Hx Hy⟩
end partial_order
section linear_order
variables [linear_order α] {s : set α} {a b : α}
lemma lt_is_lub_iff (h : is_lub s a) : b < a ↔ ∃ c ∈ s, b < c :=
by simp only [← not_le, is_lub_le_iff h, mem_upper_bounds, not_forall]
lemma is_glb_lt_iff (h : is_glb s a) : a < b ↔ ∃ c ∈ s, c < b :=
@lt_is_lub_iff (order_dual α) _ _ _ _ h
lemma is_lub.exists_between (h : is_lub s a) (hb : b < a) :
∃ c ∈ s, b < c ∧ c ≤ a :=
let ⟨c, hcs, hbc⟩ := (lt_is_lub_iff h).1 hb in ⟨c, hcs, hbc, h.1 hcs⟩
lemma is_lub.exists_between' (h : is_lub s a) (h' : a ∉ s) (hb : b < a) :
∃ c ∈ s, b < c ∧ c < a :=
let ⟨c, hcs, hbc, hca⟩ := h.exists_between hb
in ⟨c, hcs, hbc, hca.lt_of_ne $ λ hac, h' $ hac ▸ hcs⟩
lemma is_glb.exists_between (h : is_glb s a) (hb : a < b) :
∃ c ∈ s, a ≤ c ∧ c < b :=
let ⟨c, hcs, hbc⟩ := (is_glb_lt_iff h).1 hb in ⟨c, hcs, h.1 hcs, hbc⟩
lemma is_glb.exists_between' (h : is_glb s a) (h' : a ∉ s) (hb : a < b) :
∃ c ∈ s, a < c ∧ c < b :=
let ⟨c, hcs, hac, hcb⟩ := h.exists_between hb
in ⟨c, hcs, hac.lt_of_ne $ λ hac, h' $ hac.symm ▸ hcs, hcb⟩
end linear_order
/-!
### Least upper bound and the greatest lower bound in linear ordered additive commutative groups
-/
section linear_ordered_add_comm_group
variables [linear_ordered_add_comm_group α] {s : set α} {a ε : α}
lemma is_glb.exists_between_self_add (h : is_glb s a) (hε : 0 < ε) :
∃ b ∈ s, a ≤ b ∧ b < a + ε :=
h.exists_between $ lt_add_of_pos_right _ hε
lemma is_glb.exists_between_self_add' (h : is_glb s a) (h₂ : a ∉ s) (hε : 0 < ε) :
∃ b ∈ s, a < b ∧ b < a + ε :=
h.exists_between' h₂ $ lt_add_of_pos_right _ hε
lemma is_lub.exists_between_sub_self (h : is_lub s a) (hε : 0 < ε) : ∃ b ∈ s, a - ε < b ∧ b ≤ a :=
h.exists_between $ sub_lt_self _ hε
lemma is_lub.exists_between_sub_self' (h : is_lub s a) (h₂ : a ∉ s) (hε : 0 < ε) :
∃ b ∈ s, a - ε < b ∧ b < a :=
h.exists_between' h₂ $ sub_lt_self _ hε
end linear_ordered_add_comm_group
/-!
### Images of upper/lower bounds under monotone functions
-/
namespace monotone
variables [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α}
lemma mem_upper_bounds_image (Ha : a ∈ upper_bounds s) :
f a ∈ upper_bounds (f '' s) :=
ball_image_of_ball (assume x H, Hf (Ha ‹x ∈ s›))
lemma mem_lower_bounds_image (Ha : a ∈ lower_bounds s) :
f a ∈ lower_bounds (f '' s) :=
ball_image_of_ball (assume x H, Hf (Ha ‹x ∈ s›))
lemma image_upper_bounds_subset_upper_bounds_image (hf : monotone f) :
f '' upper_bounds s ⊆ upper_bounds (f '' s) :=
begin
rintro _ ⟨a, ha, rfl⟩,
exact hf.mem_upper_bounds_image ha,
end
lemma image_lower_bounds_subset_lower_bounds_image (hf : monotone f) :
f '' lower_bounds s ⊆ lower_bounds (f '' s) :=
hf.dual.image_upper_bounds_subset_upper_bounds_image
/-- The image under a monotone function of a set which is bounded above is bounded above. -/
lemma map_bdd_above (hf : monotone f) : bdd_above s → bdd_above (f '' s)
| ⟨C, hC⟩ := ⟨f C, hf.mem_upper_bounds_image hC⟩
/-- The image under a monotone function of a set which is bounded below is bounded below. -/
lemma map_bdd_below (hf : monotone f) : bdd_below s → bdd_below (f '' s)
| ⟨C, hC⟩ := ⟨f C, hf.mem_lower_bounds_image hC⟩
/-- A monotone map sends a least element of a set to a least element of its image. -/
lemma map_is_least (Ha : is_least s a) : is_least (f '' s) (f a) :=
⟨mem_image_of_mem _ Ha.1, Hf.mem_lower_bounds_image Ha.2⟩
/-- A monotone map sends a greatest element of a set to a greatest element of its image. -/
lemma map_is_greatest (Ha : is_greatest s a) : is_greatest (f '' s) (f a) :=
⟨mem_image_of_mem _ Ha.1, Hf.mem_upper_bounds_image Ha.2⟩
lemma is_lub_image_le (Ha : is_lub s a) {b : β} (Hb : is_lub (f '' s) b) :
b ≤ f a :=
Hb.2 (Hf.mem_upper_bounds_image Ha.1)
lemma le_is_glb_image (Ha : is_glb s a) {b : β} (Hb : is_glb (f '' s) b) :
f a ≤ b :=
Hb.2 (Hf.mem_lower_bounds_image Ha.1)
end monotone
namespace antitone
variables [preorder α] [preorder β] {f : α → β} (hf : antitone f) {a : α} {s : set α}
lemma mem_upper_bounds_image (ha : a ∈ lower_bounds s) :
f a ∈ upper_bounds (f '' s) :=
hf.dual_right.mem_lower_bounds_image ha
lemma mem_lower_bounds_image (ha : a ∈ upper_bounds s) :
f a ∈ lower_bounds (f '' s) :=
hf.dual_right.mem_upper_bounds_image ha
lemma image_lower_bounds_subset_upper_bounds_image (hf : antitone f) :
f '' lower_bounds s ⊆ upper_bounds (f '' s) :=
hf.dual_right.image_lower_bounds_subset_lower_bounds_image
lemma image_upper_bounds_subset_lower_bounds_image (hf : antitone f) :
f '' upper_bounds s ⊆ lower_bounds (f '' s) :=
hf.dual_right.image_upper_bounds_subset_upper_bounds_image
/-- The image under an antitone function of a set which is bounded above is bounded below. -/
lemma map_bdd_above (hf : antitone f) : bdd_above s → bdd_below (f '' s) :=
hf.dual_right.map_bdd_above
/-- The image under an antitone function of a set which is bounded below is bounded above. -/
lemma map_bdd_below (hf : antitone f) : bdd_below s → bdd_above (f '' s) :=
hf.dual_right.map_bdd_below
/-- An antitone map sends a greatest element of a set to a least element of its image. -/
lemma map_is_greatest (ha : is_greatest s a) : is_least (f '' s) (f a) :=
hf.dual_right.map_is_greatest ha
/-- An antitone map sends a least element of a set to a greatest element of its image. -/
lemma map_is_least (ha : is_least s a) : is_greatest (f '' s) (f a) :=
hf.dual_right.map_is_least ha
lemma is_lub_image_le (ha : is_glb s a) {b : β} (hb : is_lub (f '' s) b) : b ≤ f a :=
hf.dual_left.is_lub_image_le ha hb
lemma le_is_glb_image (ha : is_lub s a) {b : β} (hb : is_glb (f '' s) b) : f a ≤ b :=
hf.dual_left.le_is_glb_image ha hb
end antitone
lemma is_glb.of_image [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y}, f x ≤ f y ↔ x ≤ y)
{s : set α} {x : α} (hx : is_glb (f '' s) (f x)) :
is_glb s x :=
⟨λ y hy, hf.1 $ hx.1 $ mem_image_of_mem _ hy,
λ y hy, hf.1 $ hx.2 $ monotone.mem_lower_bounds_image (λ x y, hf.2) hy⟩
lemma is_lub.of_image [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y}, f x ≤ f y ↔ x ≤ y)
{s : set α} {x : α} (hx : is_lub (f '' s) (f x)) :
is_lub s x :=
@is_glb.of_image (order_dual α) (order_dual β) _ _ f (λ x y, hf) _ _ hx
lemma is_lub_pi {π : α → Type*} [Π a, preorder (π a)] {s : set (Π a, π a)} {f : Π a, π a} :
is_lub s f ↔ ∀ a, is_lub (function.eval a '' s) (f a) :=
begin
classical,
refine ⟨λ H a, ⟨(function.monotone_eval a).mem_upper_bounds_image H.1, λ b hb, _⟩, λ H, ⟨_, _⟩⟩,
{ suffices : function.update f a b ∈ upper_bounds s,
from function.update_same a b f ▸ H.2 this a,
refine λ g hg, le_update_iff.2 ⟨hb $ mem_image_of_mem _ hg, λ i hi, H.1 hg i⟩ },
{ exact λ g hg a, (H a).1 (mem_image_of_mem _ hg) },
{ exact λ g hg a, (H a).2 ((function.monotone_eval a).mem_upper_bounds_image hg) }
end
lemma is_glb_pi {π : α → Type*} [Π a, preorder (π a)] {s : set (Π a, π a)} {f : Π a, π a} :
is_glb s f ↔ ∀ a, is_glb (function.eval a '' s) (f a) :=
@is_lub_pi α (λ a, order_dual (π a)) _ s f
lemma is_lub_prod [preorder α] [preorder β] {s : set (α × β)} (p : α × β) :
is_lub s p ↔ is_lub (prod.fst '' s) p.1 ∧ is_lub (prod.snd '' s) p.2 :=
begin
refine ⟨λ H, ⟨⟨monotone_fst.mem_upper_bounds_image H.1, λ a ha, _⟩,
⟨monotone_snd.mem_upper_bounds_image H.1, λ a ha, _⟩⟩, λ H, ⟨_, _⟩⟩,
{ suffices : (a, p.2) ∈ upper_bounds s, from (H.2 this).1,
exact λ q hq, ⟨ha $ mem_image_of_mem _ hq, (H.1 hq).2⟩ },
{ suffices : (p.1, a) ∈ upper_bounds s, from (H.2 this).2,
exact λ q hq, ⟨(H.1 hq).1, ha $ mem_image_of_mem _ hq⟩ },
{ exact λ q hq, ⟨H.1.1 $ mem_image_of_mem _ hq, H.2.1 $ mem_image_of_mem _ hq⟩ },
{ exact λ q hq, ⟨H.1.2 $ monotone_fst.mem_upper_bounds_image hq,
H.2.2 $ monotone_snd.mem_upper_bounds_image hq⟩ }
end
lemma is_glb_prod [preorder α] [preorder β] {s : set (α × β)} (p : α × β) :
is_glb s p ↔ is_glb (prod.fst '' s) p.1 ∧ is_glb (prod.snd '' s) p.2 :=
@is_lub_prod (order_dual α) (order_dual β) _ _ _ _
namespace order_iso
variables [preorder α] [preorder β] (f : α ≃o β)
lemma upper_bounds_image {s : set α} :
upper_bounds (f '' s) = f '' upper_bounds s :=
subset.antisymm
(λ x hx, ⟨f.symm x, λ y hy, f.le_symm_apply.2 (hx $ mem_image_of_mem _ hy), f.apply_symm_apply x⟩)
f.monotone.image_upper_bounds_subset_upper_bounds_image
lemma lower_bounds_image {s : set α} :
lower_bounds (f '' s) = f '' lower_bounds s :=
@upper_bounds_image (order_dual α) (order_dual β) _ _ f.dual _
@[simp] lemma is_lub_image {s : set α} {x : β} :
is_lub (f '' s) x ↔ is_lub s (f.symm x) :=
⟨λ h, is_lub.of_image (λ _ _, f.le_iff_le) ((f.apply_symm_apply x).symm ▸ h),
λ h, is_lub.of_image (λ _ _, f.symm.le_iff_le) $ (f.symm_image_image s).symm ▸ h⟩
lemma is_lub_image' {s : set α} {x : α} :
is_lub (f '' s) (f x) ↔ is_lub s x :=
by rw [is_lub_image, f.symm_apply_apply]
@[simp] lemma is_glb_image {s : set α} {x : β} :
is_glb (f '' s) x ↔ is_glb s (f.symm x) :=
f.dual.is_lub_image
lemma is_glb_image' {s : set α} {x : α} :
is_glb (f '' s) (f x) ↔ is_glb s x :=
f.dual.is_lub_image'
@[simp] lemma is_lub_preimage {s : set β} {x : α} :
is_lub (f ⁻¹' s) x ↔ is_lub s (f x) :=
by rw [← f.symm_symm, ← image_eq_preimage, is_lub_image]
lemma is_lub_preimage' {s : set β} {x : β} :
is_lub (f ⁻¹' s) (f.symm x) ↔ is_lub s x :=
by rw [is_lub_preimage, f.apply_symm_apply]
@[simp] lemma is_glb_preimage {s : set β} {x : α} :
is_glb (f ⁻¹' s) x ↔ is_glb s (f x) :=
f.dual.is_lub_preimage
lemma is_glb_preimage' {s : set β} {x : β} :
is_glb (f ⁻¹' s) (f.symm x) ↔ is_glb s x :=
f.dual.is_lub_preimage'
end order_iso
|
898fb92fc7419583647735a1f6330345d874ec91 | 02005f45e00c7ecf2c8ca5db60251bd1e9c860b5 | /src/algebra/continued_fractions/computation/correctness_terminating.lean | 548191a71e9e2f2d0811d056f675cca72ae59638 | [
"Apache-2.0"
] | permissive | anthony2698/mathlib | 03cd69fe5c280b0916f6df2d07c614c8e1efe890 | 407615e05814e98b24b2ff322b14e8e3eb5e5d67 | refs/heads/master | 1,678,792,774,873 | 1,614,371,563,000 | 1,614,371,563,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,835 | lean | /-
Copyright (c) 2020 Kevin Kappelmann. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Kappelmann
-/
import algebra.continued_fractions.computation.translations
import algebra.continued_fractions.terminated_stable
import algebra.continued_fractions.continuants_recurrence
import order.filter.at_top_bot
/-!
# Correctness of Terminating Continued Fraction Computations (`gcf.of`)
## Summary
Let us write `gcf` for `generalized_continued_fraction`. We show the correctness of the
algorithm computing continued fractions (`gcf.of`) in case of termination in the following sense:
At every step `n : ℕ`, we can obtain the value `v` by adding a specific residual term to the last
denominator of the fraction described by `(gcf.of v).convergents' n`. The residual term will be zero
exactly when the continued fraction terminated; otherwise, the residual term will be given by the
fractional part stored in `gcf.int_fract_pair.stream v n`.
For an example, refer to `gcf.comp_exact_value_correctness_of_stream_eq_some` and for more
information about the computation process, refer to `algebra.continued_fraction.computation.basic`.
## Main definitions
- `gcf.comp_exact_value` can be used to compute the exact value approximated by the continued
fraction `gcf.of v` by adding a residual term as described in the summary.
## Main Theorems
- `gcf.comp_exact_value_correctness_of_stream_eq_some` shows that `gcf.comp_exact_value` indeed
returns the value `v` when given the convergent and fractional part as described in the summary.
- `gcf.of_correctness_of_terminated_at` shows the equality `v = (gcf.of v).convergents n`
if `gcf.of v` terminated at position `n`.
-/
namespace generalized_continued_fraction
open generalized_continued_fraction as gcf
variables {K : Type*} [linear_ordered_field K] {v : K} {n : ℕ}
/--
Given two continuants `pconts` and `conts` and a value `fr`, this function returns
- `conts.a / conts.b` if `fr = 0`
- `exact_conts.a / exact_conts.b` where `exact_conts = next_continuants 1 fr⁻¹ pconts conts`
otherwise.
This function can be used to compute the exact value approxmated by a continued fraction `gcf.of v`
as described in lemma `comp_exact_value_correctness_of_stream_eq_some`.
-/
protected def comp_exact_value (pconts conts : gcf.pair K) (fr : K) : K :=
-- if the fractional part is zero, we exactly approximated the value by the last continuants
if fr = 0 then conts.a / conts.b
-- otherwise, we have to include the fractional part in a final continuants step.
else let exact_conts := next_continuants 1 fr⁻¹ pconts conts in
exact_conts.a / exact_conts.b
variable [floor_ring K]
/-- Just a computational lemma we need for the next main proof. -/
protected lemma comp_exact_value_correctness_of_stream_eq_some_aux_comp {a : K} (b c : K)
(fract_a_ne_zero : fract a ≠ 0) :
((⌊a⌋ : K) * b + c) / (fract a) + b = (b * a + c) / fract a :=
by { field_simp [fract_a_ne_zero], rw [fract], ring }
open generalized_continued_fraction as gcf
/--
Shows the correctness of `comp_exact_value` in case the continued fraction `gcf.of v` did not
terminate at position `n`. That is, we obtain the value `v` if we pass the two successive
(auxiliary) continuants at positions `n` and `n + 1` as well as the fractional part at
`int_fract_pair.stream n` to `comp_exact_value`.
The correctness might be seen more readily if one uses `convergents'` to evaluate the continued
fraction. Here is an example to illustrate the idea:
Let `(v : ℚ) := 3.4`. We have
- `gcf.int_fract_pair.stream v 0 = some ⟨3, 0.4⟩`, and
- `gcf.int_fract_pair.stream v 1 = some ⟨2, 0.5⟩`.
Now `(gcf.of v).convergents' 1 = 3 + 1/2`, and our fractional term at position `2` is `0.5`. We
hence have `v = 3 + 1/(2 + 0.5) = 3 + 1/2.5 = 3.4`. This computation corresponds exactly to the one
using the recurrence equation in `comp_exact_value`.
-/
lemma comp_exact_value_correctness_of_stream_eq_some :
∀ {ifp_n : int_fract_pair K}, int_fract_pair.stream v n = some ifp_n →
v = gcf.comp_exact_value
((gcf.of v).continuants_aux n)
((gcf.of v).continuants_aux $ n + 1)
ifp_n.fr :=
begin
let g := gcf.of v,
induction n with n IH,
{ assume ifp_zero stream_zero_eq, -- nat.zero
have : int_fract_pair.of v = ifp_zero, by
{ have : int_fract_pair.stream v 0 = some (int_fract_pair.of v), from rfl,
simpa only [this] using stream_zero_eq },
cases this,
cases decidable.em (fract v = 0) with fract_eq_zero fract_ne_zero,
-- fract v = 0; we must then have `v = ⌊v⌋`
{ suffices : v = ⌊v⌋, by simpa [continuants_aux, fract_eq_zero, gcf.comp_exact_value],
calc
v = fract v + ⌊v⌋ : by rw fract_add_floor
... = ⌊v⌋ : by simp [fract_eq_zero] },
-- fract v ≠ 0; the claim then easily follows by unfolding a single computation step
{ field_simp [continuants_aux, next_continuants, next_numerator, next_denominator,
gcf.of_h_eq_floor, gcf.comp_exact_value, fract_ne_zero] } },
{ assume ifp_succ_n succ_nth_stream_eq, -- nat.succ
obtain ⟨ifp_n, nth_stream_eq, nth_fract_ne_zero, _⟩ :
∃ ifp_n, int_fract_pair.stream v n = some ifp_n ∧ ifp_n.fr ≠ 0
∧ int_fract_pair.of ifp_n.fr⁻¹ = ifp_succ_n, from
int_fract_pair.succ_nth_stream_eq_some_iff.elim_left succ_nth_stream_eq,
-- introduce some notation
let conts := g.continuants_aux (n + 2),
set pconts := g.continuants_aux (n + 1) with pconts_eq,
set ppconts := g.continuants_aux n with ppconts_eq,
cases decidable.em (ifp_succ_n.fr = 0) with ifp_succ_n_fr_eq_zero ifp_succ_n_fr_ne_zero,
-- ifp_succ_n.fr = 0
{ suffices : v = conts.a / conts.b, by
simpa [gcf.comp_exact_value, ifp_succ_n_fr_eq_zero],
-- use the IH and the fact that ifp_n.fr⁻¹ = ⌊ifp_n.fr⁻¹⌋ to prove this case
obtain ⟨ifp_n', nth_stream_eq', ifp_n_fract_inv_eq_floor⟩ :
∃ ifp_n, int_fract_pair.stream v n = some ifp_n ∧ ifp_n.fr⁻¹ = ⌊ifp_n.fr⁻¹⌋, from
int_fract_pair.exists_succ_nth_stream_of_fr_zero succ_nth_stream_eq ifp_succ_n_fr_eq_zero,
have : ifp_n' = ifp_n, by injection (eq.trans (nth_stream_eq').symm nth_stream_eq),
cases this,
have s_nth_eq : g.s.nth n = some ⟨1, ⌊ifp_n.fr⁻¹⌋⟩, from
gcf.nth_of_eq_some_of_nth_int_fract_pair_stream_fr_ne_zero nth_stream_eq nth_fract_ne_zero,
rw [←ifp_n_fract_inv_eq_floor] at s_nth_eq,
suffices : v = gcf.comp_exact_value ppconts pconts ifp_n.fr, by
simpa [conts, continuants_aux, s_nth_eq,gcf.comp_exact_value, nth_fract_ne_zero] using this,
exact (IH nth_stream_eq) },
-- ifp_succ_n.fr ≠ 0
{ -- use the IH to show that the following equality suffices
suffices : gcf.comp_exact_value ppconts pconts ifp_n.fr
= gcf.comp_exact_value pconts conts ifp_succ_n.fr, by
{ have : v = gcf.comp_exact_value ppconts pconts ifp_n.fr, from IH nth_stream_eq,
conv_lhs { rw this }, assumption },
-- get the correspondence between ifp_n and ifp_succ_n
obtain ⟨ifp_n', nth_stream_eq', ifp_n_fract_ne_zero, ⟨refl⟩⟩ :
∃ ifp_n, int_fract_pair.stream v n = some ifp_n ∧ ifp_n.fr ≠ 0
∧ int_fract_pair.of ifp_n.fr⁻¹ = ifp_succ_n, from
int_fract_pair.succ_nth_stream_eq_some_iff.elim_left succ_nth_stream_eq,
have : ifp_n' = ifp_n, by injection (eq.trans (nth_stream_eq').symm nth_stream_eq),
cases this,
-- get the correspondence between ifp_n and g.s.nth n
have s_nth_eq : g.s.nth n = some ⟨1, (⌊ifp_n.fr⁻¹⌋ : K)⟩,
from gcf.nth_of_eq_some_of_nth_int_fract_pair_stream_fr_ne_zero nth_stream_eq
ifp_n_fract_ne_zero,
-- the claim now follows by unfolding the definitions and tedious calculations
-- some shorthand notation
let ppA := ppconts.a, let ppB := ppconts.b,
let pA := pconts.a, let pB := pconts.b,
have : gcf.comp_exact_value ppconts pconts ifp_n.fr
= (ppA + ifp_n.fr⁻¹ * pA) / (ppB + ifp_n.fr⁻¹ * pB), by
-- unfold comp_exact_value and the convergent computation once
{ field_simp [ifp_n_fract_ne_zero, gcf.comp_exact_value, next_continuants, next_numerator,
next_denominator], ac_refl },
rw this,
-- two calculations needed to show the claim
have tmp_calc := gcf.comp_exact_value_correctness_of_stream_eq_some_aux_comp
pA ppA ifp_succ_n_fr_ne_zero,
have tmp_calc' := gcf.comp_exact_value_correctness_of_stream_eq_some_aux_comp
pB ppB ifp_succ_n_fr_ne_zero,
rw inv_eq_one_div at tmp_calc tmp_calc',
have : fract (1 / ifp_n.fr) ≠ 0, by simpa using ifp_succ_n_fr_ne_zero,
-- now unfold the recurrence one step and simplify both sides to arrive at the conclusion
field_simp [conts, gcf.comp_exact_value,
(gcf.continuants_aux_recurrence s_nth_eq ppconts_eq pconts_eq), next_continuants,
next_numerator, next_denominator, this, tmp_calc, tmp_calc'],
ac_refl } }
end
/-- The convergent of `gcf.of v` at step `n - 1` is exactly `v` if the `int_fract_pair.stream` of
the corresponding continued fraction terminated at step `n`. -/
lemma of_correctness_of_nth_stream_eq_none
(nth_stream_eq_none : int_fract_pair.stream v n = none) :
v = (gcf.of v).convergents (n - 1) :=
begin
induction n with n IH,
case nat.zero { contradiction }, -- int_fract_pair.stream v 0 ≠ none
case nat.succ
{ rename nth_stream_eq_none succ_nth_stream_eq_none,
let g := gcf.of v,
change v = g.convergents n,
have : int_fract_pair.stream v n = none
∨ ∃ ifp, int_fract_pair.stream v n = some ifp ∧ ifp.fr = 0, from
int_fract_pair.succ_nth_stream_eq_none_iff.elim_left succ_nth_stream_eq_none,
rcases this with ⟨nth_stream_eq_none⟩ | ⟨ifp_n, nth_stream_eq, nth_stream_fr_eq_zero⟩,
{ cases n with n',
{ contradiction }, -- int_fract_pair.stream v 0 ≠ none
{ have : g.terminated_at n', from
gcf.of_terminated_at_n_iff_succ_nth_int_fract_pair_stream_eq_none.elim_right
nth_stream_eq_none,
have : g.convergents (n' + 1) = g.convergents n', from
gcf.convergents_stable_of_terminated n'.le_succ this,
rw this,
exact (IH nth_stream_eq_none) } },
{ simpa [nth_stream_fr_eq_zero, gcf.comp_exact_value] using
(comp_exact_value_correctness_of_stream_eq_some nth_stream_eq) } }
end
/-- If `gcf.of v` terminated at step `n`, then the `n`th convergent is exactly `v`. -/
theorem of_correctness_of_terminated_at (terminated_at_n : (gcf.of v).terminated_at n) :
v = (gcf.of v).convergents n :=
have int_fract_pair.stream v (n + 1) = none, from
gcf.of_terminated_at_n_iff_succ_nth_int_fract_pair_stream_eq_none.elim_left terminated_at_n,
of_correctness_of_nth_stream_eq_none this
/-- If `gcf.of v` terminates, then there is `n : ℕ` such that the `n`th convergent is exactly
`v`. -/
lemma of_correctness_of_terminates (terminates : (gcf.of v).terminates) :
∃ (n : ℕ), v = (gcf.of v).convergents n :=
exists.elim terminates
( assume n terminated_at_n,
exists.intro n (of_correctness_of_terminated_at terminated_at_n) )
open filter
/-- If `gcf.of v` terminates, then its convergents will eventually always be `v`. -/
lemma of_correctness_at_top_of_terminates (terminates : (gcf.of v).terminates) :
∀ᶠ n in at_top, v = (gcf.of v).convergents n :=
begin
rw eventually_at_top,
obtain ⟨n, terminated_at_n⟩ : ∃ n, (gcf.of v).terminated_at n, from terminates,
use n,
assume m m_geq_n,
rw (gcf.convergents_stable_of_terminated m_geq_n terminated_at_n),
exact of_correctness_of_terminated_at terminated_at_n
end
end generalized_continued_fraction
|
c984aed077d2a3056579bc3d2094384701b5811c | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/topology/algebra/uniform_ring.lean | 51c5d227962294da6fc8dc05ab808c197fcaebe7 | [
"Apache-2.0"
] | permissive | fpvandoorn/mathlib | b21ab4068db079cbb8590b58fda9cc4bc1f35df4 | b3433a51ea8bc07c4159c1073838fc0ee9b8f227 | refs/heads/master | 1,624,791,089,608 | 1,556,715,231,000 | 1,556,715,231,000 | 165,722,980 | 5 | 0 | Apache-2.0 | 1,552,657,455,000 | 1,547,494,646,000 | Lean | UTF-8 | Lean | false | false | 6,588 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Johannes Hölzl
Theory of topological rings with uniform structure.
-/
import topology.algebra.group_completion topology.algebra.ring
open classical set lattice filter topological_space add_comm_group
local attribute [instance] classical.prop_decidable
noncomputable theory
namespace uniform_space.completion
open dense_embedding uniform_space
variables (α : Type*) [ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] [separated α]
instance is_Z_bilin_mul : is_Z_bilin (λp:α×α, p.1 * p.2) :=
⟨assume a a' b, add_mul a a' b, assume a b b', mul_add a b b'⟩
instance : has_one (completion α) := ⟨(1:α)⟩
instance : has_mul (completion α) :=
⟨λa b, extend (dense_embedding_coe.prod dense_embedding_coe)
((coe : α → completion α) ∘ (λp:α×α, p.1 * p.2)) (a, b)⟩
lemma coe_one : ((1 : α) : completion α) = 1 := rfl
lemma continuous_mul' : continuous (λp:completion α×completion α, p.1 * p.2) :=
suffices continuous $ extend (dense_embedding_coe.prod dense_embedding_coe) $
((coe : α → completion α) ∘ (λp:α×α, p.1 * p.2)),
{ convert this, ext ⟨a, b⟩, refl },
extend_Z_bilin dense_embedding_coe dense_embedding_coe (continuous.comp continuous_mul' (continuous_coe α))
section rules
variables {α}
lemma coe_mul (a b : α) : ((a * b : α) : completion α) = a * b :=
eq.symm (extend_e_eq (dense_embedding_coe.prod dense_embedding_coe) (a, b))
lemma continuous_mul {β : Type*} [topological_space β] {f g : β → completion α}
(hf : continuous f) (hg : continuous g) : continuous (λb, f b * g b) :=
(continuous.prod_mk hf hg).comp (continuous_mul' α)
end rules
instance : ring (completion α) :=
{ one_mul := assume a, completion.induction_on a
(is_closed_eq (continuous_mul continuous_const continuous_id) continuous_id)
(assume a, by rw [← coe_one, ← coe_mul, one_mul]),
mul_one := assume a, completion.induction_on a
(is_closed_eq (continuous_mul continuous_id continuous_const) continuous_id)
(assume a, by rw [← coe_one, ← coe_mul, mul_one]),
mul_assoc := assume a b c, completion.induction_on₃ a b c
(is_closed_eq
(continuous_mul (continuous_mul continuous_fst (continuous_snd.comp continuous_fst))
(continuous_snd.comp continuous_snd))
(continuous_mul continuous_fst
(continuous_mul (continuous_snd.comp continuous_fst) (continuous_snd.comp continuous_snd))))
(assume a b c, by rw [← coe_mul, ← coe_mul, ← coe_mul, ← coe_mul, mul_assoc]),
left_distrib := assume a b c, completion.induction_on₃ a b c
(is_closed_eq
(continuous_mul continuous_fst (continuous_add
(continuous_snd.comp continuous_fst)
(continuous_snd.comp continuous_snd)))
(continuous_add
(continuous_mul continuous_fst (continuous_snd.comp continuous_fst))
(continuous_mul continuous_fst (continuous_snd.comp continuous_snd))))
(assume a b c, by rw [← coe_add, ← coe_mul, ← coe_mul, ← coe_mul, ←coe_add, mul_add]),
right_distrib := assume a b c, completion.induction_on₃ a b c
(is_closed_eq
(continuous_mul (continuous_add continuous_fst
(continuous_snd.comp continuous_fst)) (continuous_snd.comp continuous_snd))
(continuous_add
(continuous_mul continuous_fst (continuous_snd.comp continuous_snd))
(continuous_mul (continuous_snd.comp continuous_fst) (continuous_snd.comp continuous_snd))))
(assume a b c, by rw [← coe_add, ← coe_mul, ← coe_mul, ← coe_mul, ←coe_add, add_mul]),
..completion.add_comm_group, ..completion.has_mul α, ..completion.has_one α }
instance is_ring_hom_coe : is_ring_hom (coe : α → completion α) :=
⟨coe_one α, assume a b, coe_mul a b, assume a b, coe_add a b⟩
universe u
instance is_ring_hom_extension
{β : Type u} [uniform_space β] [ring β] [uniform_add_group β] [topological_ring β]
[complete_space β] [separated β]
{f : α → β} [is_ring_hom f] (hf : continuous f) :
is_ring_hom (completion.extension f) :=
have hf : uniform_continuous f, from uniform_continuous_of_continuous hf,
{ map_one := by rw [← coe_one, extension_coe hf, is_ring_hom.map_one f],
map_add := assume a b, completion.induction_on₂ a b
(is_closed_eq
(continuous_add'.comp continuous_extension)
(continuous_add (continuous_fst.comp continuous_extension) (continuous_snd.comp continuous_extension)))
(assume a b,
by rw [← coe_add, extension_coe hf, extension_coe hf, extension_coe hf, is_add_group_hom.map_add f]),
map_mul := assume a b, completion.induction_on₂ a b
(is_closed_eq
((continuous_mul' α).comp continuous_extension)
(_root_.continuous_mul (continuous_fst.comp continuous_extension) (continuous_snd.comp continuous_extension)))
(assume a b,
by rw [← coe_mul, extension_coe hf, extension_coe hf, extension_coe hf, is_ring_hom.map_mul f]) }
end uniform_space.completion
namespace uniform_space
variables {α : Type*}
lemma ring_sep_rel (α) [comm_ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] :
separation_setoid α = submodule.quotient_rel (ideal.closure ⊥) :=
setoid.ext $ assume x y, group_separation_rel x y
lemma ring_sep_quot (α) [r : comm_ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] :
quotient (separation_setoid α) = (⊥ : ideal α).closure.quotient :=
by rw [@ring_sep_rel α r]; refl
def sep_quot_equiv_ring_quot (α)
[r : comm_ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] :
quotient (separation_setoid α) ≃ (⊥ : ideal α).closure.quotient :=
quotient.congr $ assume x y, group_separation_rel x y
/- TODO: use a form of transport a.k.a. lift definition a.k.a. transfer -/
instance [comm_ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] :
comm_ring (quotient (separation_setoid α)) :=
by rw ring_sep_quot α; apply_instance
instance [comm_ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] :
topological_ring (quotient (separation_setoid α)) :=
begin
convert topological_ring_quotient (⊥ : ideal α).closure,
{ apply ring_sep_rel },
{ dsimp [topological_ring_quotient_topology, quotient.topological_space, to_topological_space],
congr,
apply ring_sep_rel,
apply ring_sep_rel },
{ apply ring_sep_rel },
{ simp [uniform_space.comm_ring] },
end
end uniform_space
|
5464850c214a575ef13bd4881d2e2d82d478da16 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/geometry/euclidean/circumcenter.lean | 17594c68339b529bdf10e3dcaba768d7daedf9d6 | [
"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 | 38,269 | lean | /-
Copyright (c) 2020 Joseph Myers. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joseph Myers
-/
import geometry.euclidean.basic
import linear_algebra.affine_space.finite_dimensional
import tactic.derive_fintype
/-!
# Circumcenter and circumradius
This file proves some lemmas on points equidistant from a set of
points, and defines the circumradius and circumcenter of a simplex.
There are also some definitions for use in calculations where it is
convenient to work with affine combinations of vertices together with
the circumcenter.
## Main definitions
* `circumcenter` and `circumradius` are the circumcenter and
circumradius of a simplex.
## References
* https://en.wikipedia.org/wiki/Circumscribed_circle
-/
noncomputable theory
open_locale big_operators
open_locale classical
open_locale real
open_locale real_inner_product_space
namespace euclidean_geometry
open inner_product_geometry
variables {V : Type*} {P : Type*} [inner_product_space ℝ V] [metric_space P]
[normed_add_torsor V P]
include V
open affine_subspace
/-- `p` is equidistant from two points in `s` if and only if its
`orthogonal_projection` is. -/
lemma dist_eq_iff_dist_orthogonal_projection_eq {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {p1 p2 : P} (p3 : P) (hp1 : p1 ∈ s) (hp2 : p2 ∈ s) :
dist p1 p3 = dist p2 p3 ↔
dist p1 (orthogonal_projection s p3) = dist p2 (orthogonal_projection s p3) :=
begin
rw [←mul_self_inj_of_nonneg dist_nonneg dist_nonneg,
←mul_self_inj_of_nonneg dist_nonneg dist_nonneg,
dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq
p3 hp1,
dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq
p3 hp2],
simp
end
/-- `p` is equidistant from a set of points in `s` if and only if its
`orthogonal_projection` is. -/
lemma dist_set_eq_iff_dist_orthogonal_projection_eq {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {ps : set P} (hps : ps ⊆ s) (p : P) :
(set.pairwise_on ps (λ p1 p2, dist p1 p = dist p2 p) ↔
(set.pairwise_on ps (λ p1 p2, dist p1 (orthogonal_projection s p) =
dist p2 (orthogonal_projection s p)))) :=
⟨λ h p1 hp1 p2 hp2 hne,
(dist_eq_iff_dist_orthogonal_projection_eq p (hps hp1) (hps hp2)).1 (h p1 hp1 p2 hp2 hne),
λ h p1 hp1 p2 hp2 hne,
(dist_eq_iff_dist_orthogonal_projection_eq p (hps hp1) (hps hp2)).2 (h p1 hp1 p2 hp2 hne)⟩
/-- There exists `r` such that `p` has distance `r` from all the
points of a set of points in `s` if and only if there exists (possibly
different) `r` such that its `orthogonal_projection` has that distance
from all the points in that set. -/
lemma exists_dist_eq_iff_exists_dist_orthogonal_projection_eq {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {ps : set P} (hps : ps ⊆ s) (p : P) :
(∃ r, ∀ p1 ∈ ps, dist p1 p = r) ↔
∃ r, ∀ p1 ∈ ps, dist p1 ↑(orthogonal_projection s p) = r :=
begin
have h := dist_set_eq_iff_dist_orthogonal_projection_eq hps p,
simp_rw set.pairwise_on_eq_iff_exists_eq at h,
exact h
end
/-- The induction step for the existence and uniqueness of the
circumcenter. Given a nonempty set of points in a nonempty affine
subspace whose direction is complete, such that there is a unique
(circumcenter, circumradius) pair for those points in that subspace,
and a point `p` not in that subspace, there is a unique (circumcenter,
circumradius) pair for the set with `p` added, in the span of the
subspace with `p` added. -/
lemma exists_unique_dist_eq_of_insert {s : affine_subspace ℝ P}
[complete_space s.direction] {ps : set P} (hnps : ps.nonempty) {p : P}
(hps : ps ⊆ s) (hp : p ∉ s)
(hu : ∃! cccr : (P × ℝ), cccr.fst ∈ s ∧ ∀ p1 ∈ ps, dist p1 cccr.fst = cccr.snd) :
∃! cccr₂ : (P × ℝ), cccr₂.fst ∈ affine_span ℝ (insert p (s : set P)) ∧
∀ p1 ∈ insert p ps, dist p1 cccr₂.fst = cccr₂.snd :=
begin
haveI : nonempty s := set.nonempty.to_subtype (hnps.mono hps),
rcases hu with ⟨⟨cc, cr⟩, ⟨hcc, hcr⟩, hcccru⟩,
simp only [prod.fst, prod.snd] at hcc hcr hcccru,
let x := dist cc (orthogonal_projection s p),
let y := dist p (orthogonal_projection s p),
have hy0 : y ≠ 0 := dist_orthogonal_projection_ne_zero_of_not_mem hp,
let ycc₂ := (x * x + y * y - cr * cr) / (2 * y),
let cc₂ := (ycc₂ / y) • (p -ᵥ orthogonal_projection s p : V) +ᵥ cc,
let cr₂ := real.sqrt (cr * cr + ycc₂ * ycc₂),
use (cc₂, cr₂),
simp only [prod.fst, prod.snd],
have hpo : p = (1 : ℝ) • (p -ᵥ orthogonal_projection s p : V) +ᵥ orthogonal_projection s p,
{ simp },
split,
{ split,
{ refine vadd_mem_of_mem_direction _ (mem_affine_span ℝ (set.mem_insert_of_mem _ hcc)),
rw direction_affine_span,
exact submodule.smul_mem _ _
(vsub_mem_vector_span ℝ (set.mem_insert _ _)
(set.mem_insert_of_mem _ (orthogonal_projection_mem _))) },
{ intros p1 hp1,
rw [←mul_self_inj_of_nonneg dist_nonneg (real.sqrt_nonneg _),
real.mul_self_sqrt (add_nonneg (mul_self_nonneg _) (mul_self_nonneg _))],
cases hp1,
{ rw hp1,
rw [hpo,
dist_sq_smul_orthogonal_vadd_smul_orthogonal_vadd
(orthogonal_projection_mem p) hcc _ _
(vsub_orthogonal_projection_mem_direction_orthogonal s p),
←dist_eq_norm_vsub V p, dist_comm _ cc],
field_simp [hy0],
ring },
{ rw [dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq
_ (hps hp1),
orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ hcc, subtype.coe_mk,
hcr _ hp1, dist_eq_norm_vsub V cc₂ cc, vadd_vsub, norm_smul, ←dist_eq_norm_vsub V,
real.norm_eq_abs, abs_div, abs_of_nonneg dist_nonneg, div_mul_cancel _ hy0,
abs_mul_abs_self] } } },
{ rintros ⟨cc₃, cr₃⟩ ⟨hcc₃, hcr₃⟩,
simp only [prod.fst, prod.snd] at hcc₃ hcr₃,
obtain ⟨t₃, cc₃', hcc₃', hcc₃''⟩ :
∃ (r : ℝ) (p0 : P) (hp0 : p0 ∈ s), cc₃ = r • (p -ᵥ ↑((orthogonal_projection s) p)) +ᵥ p0,
{ rwa mem_affine_span_insert_iff (orthogonal_projection_mem p) at hcc₃ },
have hcr₃' : ∃ r, ∀ p1 ∈ ps, dist p1 cc₃ = r :=
⟨cr₃, λ p1 hp1, hcr₃ p1 (set.mem_insert_of_mem _ hp1)⟩,
rw [exists_dist_eq_iff_exists_dist_orthogonal_projection_eq hps cc₃, hcc₃'',
orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ hcc₃'] at hcr₃',
cases hcr₃' with cr₃' hcr₃',
have hu := hcccru (cc₃', cr₃'),
simp only [prod.fst, prod.snd] at hu,
replace hu := hu ⟨hcc₃', hcr₃'⟩,
rw prod.ext_iff at hu,
simp only [prod.fst, prod.snd] at hu,
cases hu with hucc hucr,
substs hucc hucr,
have hcr₃val : cr₃ = real.sqrt (cr₃' * cr₃' + (t₃ * y) * (t₃ * y)),
{ cases hnps with p0 hp0,
have h' : ↑(⟨cc₃', hcc₃'⟩ : s) = cc₃' := rfl,
rw [←hcr₃ p0 (set.mem_insert_of_mem _ hp0), hcc₃'',
←mul_self_inj_of_nonneg dist_nonneg (real.sqrt_nonneg _),
real.mul_self_sqrt (add_nonneg (mul_self_nonneg _) (mul_self_nonneg _)),
dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq
_ (hps hp0),
orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ hcc₃', h', hcr p0 hp0,
dist_eq_norm_vsub V _ cc₃', vadd_vsub, norm_smul, ←dist_eq_norm_vsub V p,
real.norm_eq_abs, ←mul_assoc, mul_comm _ (|t₃|), ←mul_assoc, abs_mul_abs_self],
ring },
replace hcr₃ := hcr₃ p (set.mem_insert _ _),
rw [hpo, hcc₃'', hcr₃val, ←mul_self_inj_of_nonneg dist_nonneg (real.sqrt_nonneg _),
dist_sq_smul_orthogonal_vadd_smul_orthogonal_vadd
(orthogonal_projection_mem p) hcc₃' _ _
(vsub_orthogonal_projection_mem_direction_orthogonal s p),
dist_comm, ←dist_eq_norm_vsub V p,
real.mul_self_sqrt (add_nonneg (mul_self_nonneg _) (mul_self_nonneg _))] at hcr₃,
change x * x + _ * (y * y) = _ at hcr₃,
rw [(show x * x + (1 - t₃) * (1 - t₃) * (y * y) =
x * x + y * y - 2 * y * (t₃ * y) + t₃ * y * (t₃ * y), by ring), add_left_inj] at hcr₃,
have ht₃ : t₃ = ycc₂ / y,
{ field_simp [←hcr₃, hy0],
ring },
subst ht₃,
change cc₃ = cc₂ at hcc₃'',
congr',
rw hcr₃val,
congr' 2,
field_simp [hy0],
ring }
end
/-- Given a finite nonempty affinely independent family of points,
there is a unique (circumcenter, circumradius) pair for those points
in the affine subspace they span. -/
lemma _root_.affine_independent.exists_unique_dist_eq {ι : Type*} [hne : nonempty ι] [fintype ι]
{p : ι → P} (ha : affine_independent ℝ p) :
∃! cccr : (P × ℝ), cccr.fst ∈ affine_span ℝ (set.range p) ∧
∀ i, dist (p i) cccr.fst = cccr.snd :=
begin
unfreezingI { induction hn : fintype.card ι with m hm generalizing ι },
{ exfalso,
have h := fintype.card_pos_iff.2 hne,
rw hn at h,
exact lt_irrefl 0 h },
{ cases m,
{ rw fintype.card_eq_one_iff at hn,
cases hn with i hi,
haveI : unique ι := ⟨⟨i⟩, hi⟩,
use (p i, 0),
simp only [prod.fst, prod.snd, set.range_unique, affine_subspace.mem_affine_span_singleton],
split,
{ simp_rw [hi (default ι)],
use rfl,
intro i1,
rw hi i1,
exact dist_self _ },
{ rintros ⟨cc, cr⟩,
simp only [prod.fst, prod.snd],
rintros ⟨rfl, hdist⟩,
rw hi (default ι),
congr',
rw ←hdist (default ι),
exact dist_self _ } },
{ have i := hne.some,
let ι2 := {x // x ≠ i},
have hc : fintype.card ι2 = m + 1,
{ rw fintype.card_of_subtype (finset.univ.filter (λ x, x ≠ i)),
{ rw finset.filter_not,
simp_rw eq_comm,
rw [finset.filter_eq, if_pos (finset.mem_univ _),
finset.card_sdiff (finset.subset_univ _), finset.card_singleton, finset.card_univ,
hn],
simp },
{ simp } },
haveI : nonempty ι2 := fintype.card_pos_iff.1 (hc.symm ▸ nat.zero_lt_succ _),
have ha2 : affine_independent ℝ (λ i2 : ι2, p i2) := ha.subtype _,
replace hm := hm ha2 hc,
have hr : set.range p = insert (p i) (set.range (λ i2 : ι2, p i2)),
{ change _ = insert _ (set.range (λ i2 : {x | x ≠ i}, p i2)),
rw [←set.image_eq_range, ←set.image_univ, ←set.image_insert_eq],
congr' with j,
simp [classical.em] },
change ∃! (cccr : P × ℝ), (_ ∧ ∀ i2, (λ q, dist q cccr.fst = cccr.snd) (p i2)),
conv { congr, funext, conv { congr, skip, rw ←set.forall_range_iff } },
dsimp only,
rw hr,
change ∃! (cccr : P × ℝ), (_ ∧ ∀ (i2 : ι2), (λ q, dist q cccr.fst = cccr.snd) (p i2)) at hm,
conv at hm { congr, funext, conv { congr, skip, rw ←set.forall_range_iff } },
rw ←affine_span_insert_affine_span,
refine exists_unique_dist_eq_of_insert
(set.range_nonempty _)
(subset_span_points ℝ _)
_
hm,
convert ha.not_mem_affine_span_diff i set.univ,
change set.range (λ i2 : {x | x ≠ i}, p i2) = _,
rw ←set.image_eq_range,
congr' with j, simp, refl } }
end
end euclidean_geometry
namespace affine
namespace simplex
open finset affine_subspace euclidean_geometry
variables {V : Type*} {P : Type*} [inner_product_space ℝ V] [metric_space P]
[normed_add_torsor V P]
include V
/-- The pair (circumcenter, circumradius) of a simplex. -/
def circumcenter_circumradius {n : ℕ} (s : simplex ℝ P n) : (P × ℝ) :=
s.independent.exists_unique_dist_eq.some
/-- The property satisfied by the (circumcenter, circumradius) pair. -/
lemma circumcenter_circumradius_unique_dist_eq {n : ℕ} (s : simplex ℝ P n) :
(s.circumcenter_circumradius.fst ∈ affine_span ℝ (set.range s.points) ∧
∀ i, dist (s.points i) s.circumcenter_circumradius.fst = s.circumcenter_circumradius.snd) ∧
(∀ cccr : (P × ℝ), (cccr.fst ∈ affine_span ℝ (set.range s.points) ∧
∀ i, dist (s.points i) cccr.fst = cccr.snd) → cccr = s.circumcenter_circumradius) :=
s.independent.exists_unique_dist_eq.some_spec
/-- The circumcenter of a simplex. -/
def circumcenter {n : ℕ} (s : simplex ℝ P n) : P :=
s.circumcenter_circumradius.fst
/-- The circumradius of a simplex. -/
def circumradius {n : ℕ} (s : simplex ℝ P n) : ℝ :=
s.circumcenter_circumradius.snd
/-- The circumcenter lies in the affine span. -/
lemma circumcenter_mem_affine_span {n : ℕ} (s : simplex ℝ P n) :
s.circumcenter ∈ affine_span ℝ (set.range s.points) :=
s.circumcenter_circumradius_unique_dist_eq.1.1
/-- All points have distance from the circumcenter equal to the
circumradius. -/
@[simp] lemma dist_circumcenter_eq_circumradius {n : ℕ} (s : simplex ℝ P n) :
∀ i, dist (s.points i) s.circumcenter = s.circumradius :=
s.circumcenter_circumradius_unique_dist_eq.1.2
/-- All points have distance to the circumcenter equal to the
circumradius. -/
@[simp] lemma dist_circumcenter_eq_circumradius' {n : ℕ} (s : simplex ℝ P n) :
∀ i, dist s.circumcenter (s.points i) = s.circumradius :=
begin
intro i,
rw dist_comm,
exact dist_circumcenter_eq_circumradius _ _
end
/-- Given a point in the affine span from which all the points are
equidistant, that point is the circumcenter. -/
lemma eq_circumcenter_of_dist_eq {n : ℕ} (s : simplex ℝ P n) {p : P}
(hp : p ∈ affine_span ℝ (set.range s.points)) {r : ℝ} (hr : ∀ i, dist (s.points i) p = r) :
p = s.circumcenter :=
begin
have h := s.circumcenter_circumradius_unique_dist_eq.2 (p, r),
simp only [hp, hr, forall_const, eq_self_iff_true, and_self, prod.ext_iff] at h,
exact h.1
end
/-- Given a point in the affine span from which all the points are
equidistant, that distance is the circumradius. -/
lemma eq_circumradius_of_dist_eq {n : ℕ} (s : simplex ℝ P n) {p : P}
(hp : p ∈ affine_span ℝ (set.range s.points)) {r : ℝ} (hr : ∀ i, dist (s.points i) p = r) :
r = s.circumradius :=
begin
have h := s.circumcenter_circumradius_unique_dist_eq.2 (p, r),
simp only [hp, hr, forall_const, eq_self_iff_true, and_self, prod.ext_iff] at h,
exact h.2
end
/-- The circumradius is non-negative. -/
lemma circumradius_nonneg {n : ℕ} (s : simplex ℝ P n) : 0 ≤ s.circumradius :=
s.dist_circumcenter_eq_circumradius 0 ▸ dist_nonneg
/-- The circumradius of a simplex with at least two points is
positive. -/
lemma circumradius_pos {n : ℕ} (s : simplex ℝ P (n + 1)) : 0 < s.circumradius :=
begin
refine lt_of_le_of_ne s.circumradius_nonneg _,
intro h,
have hr := s.dist_circumcenter_eq_circumradius,
simp_rw [←h, dist_eq_zero] at hr,
have h01 := s.independent.injective.ne (dec_trivial : (0 : fin (n + 2)) ≠ 1),
simpa [hr] using h01
end
/-- The circumcenter of a 0-simplex equals its unique point. -/
lemma circumcenter_eq_point (s : simplex ℝ P 0) (i : fin 1) :
s.circumcenter = s.points i :=
begin
have h := s.circumcenter_mem_affine_span,
rw [set.range_unique, mem_affine_span_singleton] at h,
rw h,
congr
end
/-- The circumcenter of a 1-simplex equals its centroid. -/
lemma circumcenter_eq_centroid (s : simplex ℝ P 1) :
s.circumcenter = finset.univ.centroid ℝ s.points :=
begin
have hr : set.pairwise_on set.univ
(λ i j : fin 2, dist (s.points i) (finset.univ.centroid ℝ s.points) =
dist (s.points j) (finset.univ.centroid ℝ s.points)),
{ intros i hi j hj hij,
rw [finset.centroid_insert_singleton_fin, dist_eq_norm_vsub V (s.points i),
dist_eq_norm_vsub V (s.points j), vsub_vadd_eq_vsub_sub, vsub_vadd_eq_vsub_sub,
←one_smul ℝ (s.points i -ᵥ s.points 0), ←one_smul ℝ (s.points j -ᵥ s.points 0)],
fin_cases i; fin_cases j; simp [-one_smul, ←sub_smul]; norm_num },
rw set.pairwise_on_eq_iff_exists_eq at hr,
cases hr with r hr,
exact (s.eq_circumcenter_of_dist_eq
(centroid_mem_affine_span_of_card_eq_add_one ℝ _ (finset.card_fin 2))
(λ i, hr i (set.mem_univ _))).symm
end
/-- If there exists a distance that a point has from all vertices of a
simplex, the orthogonal projection of that point onto the subspace
spanned by that simplex is its circumcenter. -/
lemma orthogonal_projection_eq_circumcenter_of_exists_dist_eq {n : ℕ} (s : simplex ℝ P n)
{p : P} (hr : ∃ r, ∀ i, dist (s.points i) p = r) :
↑(orthogonal_projection (affine_span ℝ (set.range s.points)) p) = s.circumcenter :=
begin
change ∃ r : ℝ, ∀ i, (λ x, dist x p = r) (s.points i) at hr,
conv at hr { congr, funext, rw ←set.forall_range_iff },
rw exists_dist_eq_iff_exists_dist_orthogonal_projection_eq (subset_affine_span ℝ _) p at hr,
cases hr with r hr,
exact s.eq_circumcenter_of_dist_eq
(orthogonal_projection_mem p) (λ i, hr _ (set.mem_range_self i)),
end
/-- If a point has the same distance from all vertices of a simplex,
the orthogonal projection of that point onto the subspace spanned by
that simplex is its circumcenter. -/
lemma orthogonal_projection_eq_circumcenter_of_dist_eq {n : ℕ} (s : simplex ℝ P n) {p : P}
{r : ℝ} (hr : ∀ i, dist (s.points i) p = r) :
↑(orthogonal_projection (affine_span ℝ (set.range s.points)) p) = s.circumcenter :=
s.orthogonal_projection_eq_circumcenter_of_exists_dist_eq ⟨r, hr⟩
/-- The orthogonal projection of the circumcenter onto a face is the
circumcenter of that face. -/
lemma orthogonal_projection_circumcenter {n : ℕ} (s : simplex ℝ P n) {fs : finset (fin (n + 1))}
{m : ℕ} (h : fs.card = m + 1) :
↑(orthogonal_projection (affine_span ℝ (set.range (s.face h).points)) s.circumcenter) =
(s.face h).circumcenter :=
begin
have hr : ∃ r, ∀ i, dist ((s.face h).points i) s.circumcenter = r,
{ use s.circumradius,
simp [face_points] },
exact orthogonal_projection_eq_circumcenter_of_exists_dist_eq _ hr
end
/-- Two simplices with the same points have the same circumcenter. -/
lemma circumcenter_eq_of_range_eq {n : ℕ} {s₁ s₂ : simplex ℝ P n}
(h : set.range s₁.points = set.range s₂.points) : s₁.circumcenter = s₂.circumcenter :=
begin
have hs : s₁.circumcenter ∈ affine_span ℝ (set.range s₂.points) :=
h ▸ s₁.circumcenter_mem_affine_span,
have hr : ∀ i, dist (s₂.points i) s₁.circumcenter = s₁.circumradius,
{ intro i,
have hi : s₂.points i ∈ set.range s₂.points := set.mem_range_self _,
rw [←h, set.mem_range] at hi,
rcases hi with ⟨j, hj⟩,
rw [←hj, s₁.dist_circumcenter_eq_circumradius j] },
exact s₂.eq_circumcenter_of_dist_eq hs hr
end
omit V
/-- An index type for the vertices of a simplex plus its circumcenter.
This is for use in calculations where it is convenient to work with
affine combinations of vertices together with the circumcenter. (An
equivalent form sometimes used in the literature is placing the
circumcenter at the origin and working with vectors for the vertices.) -/
@[derive fintype]
inductive points_with_circumcenter_index (n : ℕ)
| point_index : fin (n + 1) → points_with_circumcenter_index
| circumcenter_index : points_with_circumcenter_index
open points_with_circumcenter_index
instance points_with_circumcenter_index_inhabited (n : ℕ) :
inhabited (points_with_circumcenter_index n) :=
⟨circumcenter_index⟩
/-- `point_index` as an embedding. -/
def point_index_embedding (n : ℕ) : fin (n + 1) ↪ points_with_circumcenter_index n :=
⟨λ i, point_index i, λ _ _ h, by injection h⟩
/-- The sum of a function over `points_with_circumcenter_index`. -/
lemma sum_points_with_circumcenter {α : Type*} [add_comm_monoid α] {n : ℕ}
(f : points_with_circumcenter_index n → α) :
∑ i, f i = (∑ (i : fin (n + 1)), f (point_index i)) + f circumcenter_index :=
begin
have h : univ = insert circumcenter_index (univ.map (point_index_embedding n)),
{ ext x,
refine ⟨λ h, _, λ _, mem_univ _⟩,
cases x with i,
{ exact mem_insert_of_mem (mem_map_of_mem _ (mem_univ i)) },
{ exact mem_insert_self _ _ } },
change _ = ∑ i, f (point_index_embedding n i) + _,
rw [add_comm, h, ←sum_map, sum_insert],
simp_rw [mem_map, not_exists],
intros x hx h,
injection h
end
include V
/-- The vertices of a simplex plus its circumcenter. -/
def points_with_circumcenter {n : ℕ} (s : simplex ℝ P n) : points_with_circumcenter_index n → P
| (point_index i) := s.points i
| circumcenter_index := s.circumcenter
/-- `points_with_circumcenter`, applied to a `point_index` value,
equals `points` applied to that value. -/
@[simp] lemma points_with_circumcenter_point {n : ℕ} (s : simplex ℝ P n) (i : fin (n + 1)) :
s.points_with_circumcenter (point_index i) = s.points i :=
rfl
/-- `points_with_circumcenter`, applied to `circumcenter_index`, equals the
circumcenter. -/
@[simp] lemma points_with_circumcenter_eq_circumcenter {n : ℕ} (s : simplex ℝ P n) :
s.points_with_circumcenter circumcenter_index = s.circumcenter :=
rfl
omit V
/-- The weights for a single vertex of a simplex, in terms of
`points_with_circumcenter`. -/
def point_weights_with_circumcenter {n : ℕ} (i : fin (n + 1)) : points_with_circumcenter_index n → ℝ
| (point_index j) := if j = i then 1 else 0
| circumcenter_index := 0
/-- `point_weights_with_circumcenter` sums to 1. -/
@[simp] lemma sum_point_weights_with_circumcenter {n : ℕ} (i : fin (n + 1)) :
∑ j, point_weights_with_circumcenter i j = 1 :=
begin
convert sum_ite_eq' univ (point_index i) (function.const _ (1 : ℝ)),
{ ext j,
cases j ; simp [point_weights_with_circumcenter] },
{ simp }
end
include V
/-- A single vertex, in terms of `points_with_circumcenter`. -/
lemma point_eq_affine_combination_of_points_with_circumcenter {n : ℕ} (s : simplex ℝ P n)
(i : fin (n + 1)) :
s.points i =
(univ : finset (points_with_circumcenter_index n)).affine_combination
s.points_with_circumcenter (point_weights_with_circumcenter i) :=
begin
rw ←points_with_circumcenter_point,
symmetry,
refine affine_combination_of_eq_one_of_eq_zero _ _ _
(mem_univ _)
(by simp [point_weights_with_circumcenter])
_,
intros i hi hn,
cases i,
{ have h : i_1 ≠ i := λ h, hn (h ▸ rfl),
simp [point_weights_with_circumcenter, h] },
{ refl }
end
omit V
/-- The weights for the centroid of some vertices of a simplex, in
terms of `points_with_circumcenter`. -/
def centroid_weights_with_circumcenter {n : ℕ} (fs : finset (fin (n + 1)))
: points_with_circumcenter_index n → ℝ
| (point_index i) := if i ∈ fs then ((card fs : ℝ) ⁻¹) else 0
| circumcenter_index := 0
/-- `centroid_weights_with_circumcenter` sums to 1, if the `finset` is
nonempty. -/
@[simp] lemma sum_centroid_weights_with_circumcenter {n : ℕ} {fs : finset (fin (n + 1))}
(h : fs.nonempty) :
∑ i, centroid_weights_with_circumcenter fs i = 1 :=
begin
simp_rw [sum_points_with_circumcenter, centroid_weights_with_circumcenter, add_zero,
←fs.sum_centroid_weights_eq_one_of_nonempty ℝ h,
set.sum_indicator_subset _ fs.subset_univ],
rcongr
end
include V
/-- The centroid of some vertices of a simplex, in terms of
`points_with_circumcenter`. -/
lemma centroid_eq_affine_combination_of_points_with_circumcenter {n : ℕ} (s : simplex ℝ P n)
(fs : finset (fin (n + 1))) :
fs.centroid ℝ s.points =
(univ : finset (points_with_circumcenter_index n)).affine_combination
s.points_with_circumcenter (centroid_weights_with_circumcenter fs) :=
begin
simp_rw [centroid_def, affine_combination_apply,
weighted_vsub_of_point_apply, sum_points_with_circumcenter,
centroid_weights_with_circumcenter, points_with_circumcenter_point, zero_smul,
add_zero, centroid_weights,
set.sum_indicator_subset_of_eq_zero
(function.const (fin (n + 1)) ((card fs : ℝ)⁻¹))
(λ i wi, wi • (s.points i -ᵥ classical.choice add_torsor.nonempty))
fs.subset_univ
(λ i, zero_smul ℝ _),
set.indicator_apply],
congr, funext, congr' 2
end
omit V
/-- The weights for the circumcenter of a simplex, in terms of
`points_with_circumcenter`. -/
def circumcenter_weights_with_circumcenter (n : ℕ) : points_with_circumcenter_index n → ℝ
| (point_index i) := 0
| circumcenter_index := 1
/-- `circumcenter_weights_with_circumcenter` sums to 1. -/
@[simp] lemma sum_circumcenter_weights_with_circumcenter (n : ℕ) :
∑ i, circumcenter_weights_with_circumcenter n i = 1 :=
begin
convert sum_ite_eq' univ circumcenter_index (function.const _ (1 : ℝ)),
{ ext ⟨j⟩ ; simp [circumcenter_weights_with_circumcenter] },
{ simp }
end
include V
/-- The circumcenter of a simplex, in terms of
`points_with_circumcenter`. -/
lemma circumcenter_eq_affine_combination_of_points_with_circumcenter {n : ℕ}
(s : simplex ℝ P n) :
s.circumcenter = (univ : finset (points_with_circumcenter_index n)).affine_combination
s.points_with_circumcenter (circumcenter_weights_with_circumcenter n) :=
begin
rw ←points_with_circumcenter_eq_circumcenter,
symmetry,
refine affine_combination_of_eq_one_of_eq_zero _ _ _ (mem_univ _) rfl _,
rintros ⟨i⟩ hi hn ; tauto
end
omit V
/-- The weights for the reflection of the circumcenter in an edge of a
simplex. This definition is only valid with `i₁ ≠ i₂`. -/
def reflection_circumcenter_weights_with_circumcenter {n : ℕ} (i₁ i₂ : fin (n + 1)) :
points_with_circumcenter_index n → ℝ
| (point_index i) := if i = i₁ ∨ i = i₂ then 1 else 0
| circumcenter_index := -1
/-- `reflection_circumcenter_weights_with_circumcenter` sums to 1. -/
@[simp] lemma sum_reflection_circumcenter_weights_with_circumcenter {n : ℕ} {i₁ i₂ : fin (n + 1)}
(h : i₁ ≠ i₂) : ∑ i, reflection_circumcenter_weights_with_circumcenter i₁ i₂ i = 1 :=
begin
simp_rw [sum_points_with_circumcenter, reflection_circumcenter_weights_with_circumcenter,
sum_ite, sum_const, filter_or, filter_eq'],
rw card_union_eq,
{ simp },
{ simpa only [if_true, mem_univ, disjoint_singleton] using h }
end
include V
/-- The reflection of the circumcenter of a simplex in an edge, in
terms of `points_with_circumcenter`. -/
lemma reflection_circumcenter_eq_affine_combination_of_points_with_circumcenter {n : ℕ}
(s : simplex ℝ P n) {i₁ i₂ : fin (n + 1)} (h : i₁ ≠ i₂) :
reflection (affine_span ℝ (s.points '' {i₁, i₂})) s.circumcenter =
(univ : finset (points_with_circumcenter_index n)).affine_combination
s.points_with_circumcenter (reflection_circumcenter_weights_with_circumcenter i₁ i₂) :=
begin
have hc : card ({i₁, i₂} : finset (fin (n + 1))) = 2,
{ simp [h] },
have h_faces : ↑(orthogonal_projection (affine_span ℝ (s.points '' {i₁, i₂})) s.circumcenter)
= ↑(orthogonal_projection (affine_span ℝ (set.range (s.face hc).points)) s.circumcenter),
{ apply eq_orthogonal_projection_of_eq_subspace,
simp },
rw [euclidean_geometry.reflection_apply, h_faces, s.orthogonal_projection_circumcenter hc,
circumcenter_eq_centroid, s.face_centroid_eq_centroid hc,
centroid_eq_affine_combination_of_points_with_circumcenter,
circumcenter_eq_affine_combination_of_points_with_circumcenter, ←@vsub_eq_zero_iff_eq V,
affine_combination_vsub, weighted_vsub_vadd_affine_combination, affine_combination_vsub,
weighted_vsub_apply, sum_points_with_circumcenter],
simp_rw [pi.sub_apply, pi.add_apply, pi.sub_apply, sub_smul, add_smul, sub_smul,
centroid_weights_with_circumcenter, circumcenter_weights_with_circumcenter,
reflection_circumcenter_weights_with_circumcenter, ite_smul, zero_smul, sub_zero,
apply_ite2 (+), add_zero, ←add_smul, hc, zero_sub, neg_smul, sub_self, add_zero],
convert sum_const_zero,
norm_num
end
end simplex
end affine
namespace euclidean_geometry
open affine affine_subspace finite_dimensional
variables {V : Type*} {P : Type*} [inner_product_space ℝ V] [metric_space P]
[normed_add_torsor V P]
include V
/-- Given a nonempty affine subspace, whose direction is complete,
that contains a set of points, those points are cospherical if and
only if they are equidistant from some point in that subspace. -/
lemma cospherical_iff_exists_mem_of_complete {s : affine_subspace ℝ P} {ps : set P} (h : ps ⊆ s)
[nonempty s] [complete_space s.direction] :
cospherical ps ↔ ∃ (center ∈ s) (radius : ℝ), ∀ p ∈ ps, dist p center = radius :=
begin
split,
{ rintro ⟨c, hcr⟩,
rw exists_dist_eq_iff_exists_dist_orthogonal_projection_eq h c at hcr,
exact ⟨orthogonal_projection s c, orthogonal_projection_mem _, hcr⟩ },
{ exact λ ⟨c, hc, hd⟩, ⟨c, hd⟩ }
end
/-- Given a nonempty affine subspace, whose direction is
finite-dimensional, that contains a set of points, those points are
cospherical if and only if they are equidistant from some point in
that subspace. -/
lemma cospherical_iff_exists_mem_of_finite_dimensional {s : affine_subspace ℝ P} {ps : set P}
(h : ps ⊆ s) [nonempty s] [finite_dimensional ℝ s.direction] :
cospherical ps ↔ ∃ (center ∈ s) (radius : ℝ), ∀ p ∈ ps, dist p center = radius :=
cospherical_iff_exists_mem_of_complete h
/-- All n-simplices among cospherical points in an n-dimensional
subspace have the same circumradius. -/
lemma exists_circumradius_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P}
(h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction]
(hd : finrank ℝ s.direction = n) (hc : cospherical ps) :
∃ r : ℝ, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumradius = r :=
begin
rw cospherical_iff_exists_mem_of_finite_dimensional h at hc,
rcases hc with ⟨c, hc, r, hcr⟩,
use r,
intros sx hsxps,
have hsx : affine_span ℝ (set.range sx.points) = s,
{ refine sx.independent.affine_span_eq_of_le_of_card_eq_finrank_add_one
(span_points_subset_coe_of_subset_coe (hsxps.trans h)) _,
simp [hd] },
have hc : c ∈ affine_span ℝ (set.range sx.points) := hsx.symm ▸ hc,
exact (sx.eq_circumradius_of_dist_eq
hc
(λ i, hcr (sx.points i) (hsxps (set.mem_range_self i)))).symm
end
/-- Two n-simplices among cospherical points in an n-dimensional
subspace have the same circumradius. -/
lemma circumradius_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P}
(h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction]
(hd : finrank ℝ s.direction = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n}
(hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) :
sx₁.circumradius = sx₂.circumradius :=
begin
rcases exists_circumradius_eq_of_cospherical_subset h hd hc with ⟨r, hr⟩,
rw [hr sx₁ hsx₁, hr sx₂ hsx₂]
end
/-- All n-simplices among cospherical points in n-space have the same
circumradius. -/
lemma exists_circumradius_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V]
(hd : finrank ℝ V = n) (hc : cospherical ps) :
∃ r : ℝ, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumradius = r :=
begin
haveI : nonempty (⊤ : affine_subspace ℝ P) := set.univ.nonempty,
rw [←finrank_top, ←direction_top ℝ V P] at hd,
refine exists_circumradius_eq_of_cospherical_subset _ hd hc,
exact set.subset_univ _
end
/-- Two n-simplices among cospherical points in n-space have the same
circumradius. -/
lemma circumradius_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V]
(hd : finrank ℝ V = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n}
(hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) :
sx₁.circumradius = sx₂.circumradius :=
begin
rcases exists_circumradius_eq_of_cospherical hd hc with ⟨r, hr⟩,
rw [hr sx₁ hsx₁, hr sx₂ hsx₂]
end
/-- All n-simplices among cospherical points in an n-dimensional
subspace have the same circumcenter. -/
lemma exists_circumcenter_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P}
(h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction]
(hd : finrank ℝ s.direction = n) (hc : cospherical ps) :
∃ c : P, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumcenter = c :=
begin
rw cospherical_iff_exists_mem_of_finite_dimensional h at hc,
rcases hc with ⟨c, hc, r, hcr⟩,
use c,
intros sx hsxps,
have hsx : affine_span ℝ (set.range sx.points) = s,
{ refine sx.independent.affine_span_eq_of_le_of_card_eq_finrank_add_one
(span_points_subset_coe_of_subset_coe (hsxps.trans h)) _,
simp [hd] },
have hc : c ∈ affine_span ℝ (set.range sx.points) := hsx.symm ▸ hc,
exact (sx.eq_circumcenter_of_dist_eq
hc
(λ i, hcr (sx.points i) (hsxps (set.mem_range_self i)))).symm
end
/-- Two n-simplices among cospherical points in an n-dimensional
subspace have the same circumcenter. -/
lemma circumcenter_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P}
(h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction]
(hd : finrank ℝ s.direction = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n}
(hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) :
sx₁.circumcenter = sx₂.circumcenter :=
begin
rcases exists_circumcenter_eq_of_cospherical_subset h hd hc with ⟨r, hr⟩,
rw [hr sx₁ hsx₁, hr sx₂ hsx₂]
end
/-- All n-simplices among cospherical points in n-space have the same
circumcenter. -/
lemma exists_circumcenter_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V]
(hd : finrank ℝ V = n) (hc : cospherical ps) :
∃ c : P, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumcenter = c :=
begin
haveI : nonempty (⊤ : affine_subspace ℝ P) := set.univ.nonempty,
rw [←finrank_top, ←direction_top ℝ V P] at hd,
refine exists_circumcenter_eq_of_cospherical_subset _ hd hc,
exact set.subset_univ _
end
/-- Two n-simplices among cospherical points in n-space have the same
circumcenter. -/
lemma circumcenter_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V]
(hd : finrank ℝ V = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n}
(hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) :
sx₁.circumcenter = sx₂.circumcenter :=
begin
rcases exists_circumcenter_eq_of_cospherical hd hc with ⟨r, hr⟩,
rw [hr sx₁ hsx₁, hr sx₂ hsx₂]
end
/-- Suppose all distances from `p₁` and `p₂` to the points of a
simplex are equal, and that `p₁` and `p₂` lie in the affine span of
`p` with the vertices of that simplex. Then `p₁` and `p₂` are equal
or reflections of each other in the affine span of the vertices of the
simplex. -/
lemma eq_or_eq_reflection_of_dist_eq {n : ℕ} {s : simplex ℝ P n} {p p₁ p₂ : P} {r : ℝ}
(hp₁ : p₁ ∈ affine_span ℝ (insert p (set.range s.points)))
(hp₂ : p₂ ∈ affine_span ℝ (insert p (set.range s.points)))
(h₁ : ∀ i, dist (s.points i) p₁ = r) (h₂ : ∀ i, dist (s.points i) p₂ = r) :
p₁ = p₂ ∨ p₁ = reflection (affine_span ℝ (set.range s.points)) p₂ :=
begin
let span_s := affine_span ℝ (set.range s.points),
have h₁' := s.orthogonal_projection_eq_circumcenter_of_dist_eq h₁,
have h₂' := s.orthogonal_projection_eq_circumcenter_of_dist_eq h₂,
rw [←affine_span_insert_affine_span,
mem_affine_span_insert_iff (orthogonal_projection_mem p)] at hp₁ hp₂,
obtain ⟨r₁, p₁o, hp₁o, hp₁⟩ := hp₁,
obtain ⟨r₂, p₂o, hp₂o, hp₂⟩ := hp₂,
obtain rfl : ↑(orthogonal_projection span_s p₁) = p₁o,
{ have := orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ hp₁o,
rw ← hp₁ at this,
rw this,
refl },
rw h₁' at hp₁,
obtain rfl : ↑(orthogonal_projection span_s p₂) = p₂o,
{ have := orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ hp₂o,
rw ← hp₂ at this,
rw this,
refl },
rw h₂' at hp₂,
have h : s.points 0 ∈ span_s := mem_affine_span ℝ (set.mem_range_self _),
have hd₁ : dist p₁ s.circumcenter * dist p₁ s.circumcenter =
r * r - s.circumradius * s.circumradius,
{ rw [dist_comm, ←h₁ 0,
dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq p₁ h],
simp only [h₁', dist_comm p₁, add_sub_cancel', simplex.dist_circumcenter_eq_circumradius] },
have hd₂ : dist p₂ s.circumcenter * dist p₂ s.circumcenter =
r * r - s.circumradius * s.circumradius,
{ rw [dist_comm, ←h₂ 0,
dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq p₂ h],
simp only [h₂', dist_comm p₂, add_sub_cancel', simplex.dist_circumcenter_eq_circumradius] },
rw [←hd₂, hp₁, hp₂, dist_eq_norm_vsub V _ s.circumcenter,
dist_eq_norm_vsub V _ s.circumcenter, vadd_vsub, vadd_vsub, ←real_inner_self_eq_norm_sq,
←real_inner_self_eq_norm_sq, real_inner_smul_left, real_inner_smul_left,
real_inner_smul_right, real_inner_smul_right, ←mul_assoc, ←mul_assoc] at hd₁,
by_cases hp : p = orthogonal_projection span_s p,
{ rw [hp₁, hp₂, ←hp],
simp only [true_or, eq_self_iff_true, smul_zero, vsub_self] },
{ have hz : ⟪p -ᵥ orthogonal_projection span_s p, p -ᵥ orthogonal_projection span_s p⟫ ≠ 0,
by simpa only [ne.def, vsub_eq_zero_iff_eq, inner_self_eq_zero] using hp,
rw [mul_left_inj' hz, mul_self_eq_mul_self_iff] at hd₁,
rw [hp₁, hp₂],
cases hd₁,
{ left,
rw hd₁ },
{ right,
rw [hd₁,
reflection_vadd_smul_vsub_orthogonal_projection p r₂ s.circumcenter_mem_affine_span,
neg_smul] } }
end
end euclidean_geometry
|
bf2064be369f68d206f186f1ad17355a830b373e | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/monad/equiv_mon_auto.lean | 6abc7129a137fb3485f17c0036d5d89bc6013e28 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,248 | lean | /-
Copyright (c) 2020 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.monad.bundled
import Mathlib.category_theory.monoidal.End
import Mathlib.category_theory.monoidal.Mon_
import Mathlib.category_theory.category.Cat
import Mathlib.PostPort
universes u v
namespace Mathlib
/-!
# The equivalence between `Monad C` and `Mon_ (C ⥤ C)`.
A monad "is just" a monoid in the category of endofunctors.
# Definitions/Theorems
1. `to_Mon` associates a monoid object in `C ⥤ C` to any monad on `C`.
2. `Monad_to_Mon` is the functorial version of `to_Mon`.
3. `of_Mon` associates a monad on `C` to any monoid object in `C ⥤ C`.
4. `Monad_Mon_equiv` is the equivalence between `Monad C` and `Mon_ (C ⥤ C)`.
-/
namespace category_theory
namespace Monad
/-- To every `Monad C` we associated a monoid object in `C ⥤ C`.-/
@[simp] theorem to_Mon_mul {C : Type u} [category C] : ∀ (ᾰ : Monad C), Mon_.mul (to_Mon ᾰ) = μ_ :=
fun (ᾰ : Monad C) => Eq.refl (Mon_.mul (to_Mon ᾰ))
/-- Passing from `Monad C` to `Mon_ (C ⥤ C)` is functorial. -/
@[simp] theorem Monad_to_Mon_obj (C : Type u) [category C] :
∀ (ᾰ : Monad C), functor.obj (Monad_to_Mon C) ᾰ = to_Mon ᾰ :=
fun (ᾰ : Monad C) => Eq.refl (functor.obj (Monad_to_Mon C) ᾰ)
/-- To every monoid object in `C ⥤ C` we associate a `Monad C`. -/
def of_Mon {C : Type u} [category C] : Mon_ (C ⥤ C) → Monad C :=
fun (M : Mon_ (C ⥤ C)) => mk (Mon_.X M)
/-- Passing from `Mon_ (C ⥤ C)` to `Monad C` is functorial. -/
def Mon_to_Monad (C : Type u) [category C] : Mon_ (C ⥤ C) ⥤ Monad C :=
functor.mk of_Mon
fun (_x _x_1 : Mon_ (C ⥤ C)) (f : _x ⟶ _x_1) =>
monad_hom.mk (nat_trans.mk (nat_trans.app (Mon_.hom.hom f)))
namespace Monad_Mon_equiv
/-- Isomorphism of functors used in `Monad_Mon_equiv` -/
@[simp] theorem counit_iso_hom_app_hom {C : Type u} [category C] (_x : Mon_ (C ⥤ C)) :
Mon_.hom.hom (nat_trans.app (iso.hom counit_iso) _x) = 𝟙 :=
Eq.refl (Mon_.hom.hom (nat_trans.app (iso.hom counit_iso) _x))
/-- Auxilliary definition for `Monad_Mon_equiv` -/
def unit_iso_hom {C : Type u} [category C] : 𝟭 ⟶ Monad_to_Mon C ⋙ Mon_to_Monad C :=
nat_trans.mk fun (_x : Monad C) => monad_hom.mk (nat_trans.mk fun (_x_1 : C) => 𝟙)
/-- Auxilliary definition for `Monad_Mon_equiv` -/
@[simp] theorem unit_iso_inv_app_to_nat_trans_app {C : Type u} [category C] (_x : Monad C) :
∀ (_x_1 : C), nat_trans.app (monad_hom.to_nat_trans (nat_trans.app unit_iso_inv _x)) _x_1 = 𝟙 :=
fun (_x_1 : C) =>
Eq.refl (nat_trans.app (monad_hom.to_nat_trans (nat_trans.app unit_iso_inv _x)) _x_1)
/-- Isomorphism of functors used in `Monad_Mon_equiv` -/
def unit_iso {C : Type u} [category C] : 𝟭 ≅ Monad_to_Mon C ⋙ Mon_to_Monad C :=
iso.mk unit_iso_hom unit_iso_inv
end Monad_Mon_equiv
/-- Oh, monads are just monoids in the category of endofunctors (equivalence of categories). -/
def Monad_Mon_equiv (C : Type u) [category C] : Monad C ≌ Mon_ (C ⥤ C) :=
equivalence.mk' (Monad_to_Mon C) (Mon_to_Monad C) sorry sorry
end Mathlib |
de7da4e842e5798cfa352e0554132027aba41581 | 1a61aba1b67cddccce19532a9596efe44be4285f | /hott/cubical/squareover.hlean | 21cefa21223756c65d3fceb5e693052ba165756f | [
"Apache-2.0"
] | permissive | eigengrau/lean | 07986a0f2548688c13ba36231f6cdbee82abf4c6 | f8a773be1112015e2d232661ce616d23f12874d0 | refs/heads/master | 1,610,939,198,566 | 1,441,352,386,000 | 1,441,352,494,000 | 41,903,576 | 0 | 0 | null | 1,441,352,210,000 | 1,441,352,210,000 | null | UTF-8 | Lean | false | false | 10,488 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Floris van Doorn
Squareovers
-/
import .square
open eq equiv is_equiv equiv.ops
namespace eq
-- we give the argument B explicitly, because Lean would find (λa, B a) by itself, which
-- makes the type uglier (of course the two terms are definitionally equal)
inductive squareover {A : Type} (B : A → Type) {a₀₀ : A} {b₀₀ : B a₀₀} :
Π{a₂₀ a₀₂ a₂₂ : A}
{p₁₀ : a₀₀ = a₂₀} {p₁₂ : a₀₂ = a₂₂} {p₀₁ : a₀₀ = a₀₂} {p₂₁ : a₂₀ = a₂₂}
(s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
{b₂₀ : B a₂₀} {b₀₂ : B a₀₂} {b₂₂ : B a₂₂}
(q₁₀ : pathover B b₀₀ p₁₀ b₂₀) (q₁₂ : pathover B b₀₂ p₁₂ b₂₂)
(q₀₁ : pathover B b₀₀ p₀₁ b₀₂) (q₂₁ : pathover B b₂₀ p₂₁ b₂₂),
Type :=
idsquareo : squareover B ids idpo idpo idpo idpo
variables {A A' : Type} {B : A → Type}
{a a' a'' a₀₀ a₂₀ a₄₀ a₀₂ a₂₂ a₂₄ a₀₄ a₄₂ a₄₄ : A}
/-a₀₀-/ {p₁₀ : a₀₀ = a₂₀} /-a₂₀-/ {p₃₀ : a₂₀ = a₄₀} /-a₄₀-/
{p₀₁ : a₀₀ = a₀₂} /-s₁₁-/ {p₂₁ : a₂₀ = a₂₂} /-s₃₁-/ {p₄₁ : a₄₀ = a₄₂}
/-a₀₂-/ {p₁₂ : a₀₂ = a₂₂} /-a₂₂-/ {p₃₂ : a₂₂ = a₄₂} /-a₄₂-/
{p₀₃ : a₀₂ = a₀₄} /-s₁₃-/ {p₂₃ : a₂₂ = a₂₄} /-s₃₃-/ {p₄₃ : a₄₂ = a₄₄}
/-a₀₄-/ {p₁₄ : a₀₄ = a₂₄} /-a₂₄-/ {p₃₄ : a₂₄ = a₄₄} /-a₄₄-/
{s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁}
{b₀₀ : B a₀₀} {b₂₀ : B a₂₀} {b₄₀ : B a₄₀}
{b₀₂ : B a₀₂} {b₂₂ : B a₂₂} {b₄₂ : B a₄₂}
{b₀₄ : B a₀₄} {b₂₄ : B a₂₄} {b₄₄ : B a₄₄}
/-b₀₀-/ {q₁₀ : b₀₀ =[p₁₀] b₂₀} /-b₂₀-/ {q₃₀ : b₂₀ =[p₃₀] b₄₀} /-b₄₀-/
{q₀₁ : b₀₀ =[p₀₁] b₀₂} /-t₁₁-/ {q₂₁ : b₂₀ =[p₂₁] b₂₂} /-t₃₁-/ {q₄₁ : b₄₀ =[p₄₁] b₄₂}
/-b₀₂-/ {q₁₂ : b₀₂ =[p₁₂] b₂₂} /-b₂₂-/ {q₃₂ : b₂₂ =[p₃₂] b₄₂} /-b₄₂-/
{q₀₃ : b₀₂ =[p₀₃] b₀₄} /-t₁₃-/ {q₂₃ : b₂₂ =[p₂₃] b₂₄} /-t₃₃-/ {q₄₃ : b₄₂ =[p₄₃] b₄₄}
/-b₀₄-/ {q₁₄ : b₀₄ =[p₁₄] b₂₄} /-b₂₄-/ {q₃₄ : b₂₄ =[p₃₄] b₄₄} /-b₄₄-/
definition squareo := @squareover A B a₀₀
definition idsquareo [reducible] [constructor] (b₀₀ : B a₀₀) := @squareover.idsquareo A B a₀₀ b₀₀
definition idso [reducible] [constructor] := @squareover.idsquareo A B a₀₀ b₀₀
definition apds (f : Πa, B a) (s : square p₁₀ p₁₂ p₀₁ p₂₁)
: squareover B s (apdo f p₁₀) (apdo f p₁₂) (apdo f p₀₁) (apdo f p₂₁) :=
square.rec_on s idso
definition vrflo : squareover B vrfl q₁₀ q₁₀ idpo idpo :=
by induction q₁₀; exact idso
definition hrflo : squareover B hrfl idpo idpo q₁₀ q₁₀ :=
by induction q₁₀; exact idso
definition vdeg_squareover {q₁₀' : b₀₀ =[p₁₀] b₂₀} (r : q₁₀ = q₁₀')
: squareover B vrfl q₁₀ q₁₀' idpo idpo :=
by induction r;exact vrflo
definition hdeg_squareover {q₀₁' : b₀₀ =[p₀₁] b₀₂} (r : q₀₁ = q₀₁')
: squareover B hrfl idpo idpo q₀₁ q₀₁' :=
by induction r; exact hrflo
-- relating squareovers to squares
definition square_of_squareover (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) :
square (!con_tr ⬝ ap (λa, p₂₁ ▸ a) (tr_eq_of_pathover q₁₀))
(tr_eq_of_pathover q₁₂)
(ap (λq, q ▸ b₀₀) (eq_of_square s₁₁) ⬝ !con_tr ⬝ ap (λa, p₁₂ ▸ a) (tr_eq_of_pathover q₀₁))
(tr_eq_of_pathover q₂₁) :=
by induction t₁₁; esimp; constructor
/-
definition squareover_of_square
(q : square (!con_tr ⬝ ap (λa, p₂₁ ▸ a) (tr_eq_of_pathover q₁₀))
(tr_eq_of_pathover q₁₂)
(ap (λq, q ▸ b₀₀) (eq_of_square s₁₁) ⬝ !con_tr ⬝ ap (λa, p₁₂ ▸ a) (tr_eq_of_pathover q₀₁))
(tr_eq_of_pathover q₂₁))
: squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁ :=
sorry
-/
definition square_of_squareover_ids {b₀₀ b₀₂ b₂₀ b₂₂ : B a}
(t : b₀₀ = b₂₀) (b : b₀₂ = b₂₂) (l : b₀₀ = b₀₂) (r : b₂₀ = b₂₂)
(so : squareover B ids (pathover_idp_of_eq t)
(pathover_idp_of_eq b)
(pathover_idp_of_eq l)
(pathover_idp_of_eq r)) : square t b l r :=
begin
let H := square_of_squareover so, -- use apply ... in
rewrite [▸* at H,+idp_con at H,+ap_id at H,↑pathover_idp_of_eq at H],
rewrite [+to_right_inv !(pathover_equiv_tr_eq (refl a)) at H],
exact H
end
definition squareover_ids_of_square {b₀₀ b₀₂ b₂₀ b₂₂ : B a}
(t : b₀₀ = b₂₀) (b : b₀₂ = b₂₂) (l : b₀₀ = b₀₂) (r : b₂₀ = b₂₂) (q : square t b l r)
: squareover B ids (pathover_idp_of_eq t)
(pathover_idp_of_eq b)
(pathover_idp_of_eq l)
(pathover_idp_of_eq r) :=
square.rec_on q idso
-- relating pathovers to squareovers
definition pathover_of_squareover' (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁)
: q₁₀ ⬝o q₂₁ =[eq_of_square s₁₁] q₀₁ ⬝o q₁₂ :=
by induction t₁₁; constructor
definition pathover_of_squareover {s : p₁₀ ⬝ p₂₁ = p₀₁ ⬝ p₁₂}
(t₁₁ : squareover B (square_of_eq s) q₁₀ q₁₂ q₀₁ q₂₁)
: q₁₀ ⬝o q₂₁ =[s] q₀₁ ⬝o q₁₂ :=
begin
revert s t₁₁, refine equiv_rect' !square_equiv_eq⁻¹ᵉ (λa b, squareover B b _ _ _ _ → _) _,
intro s, exact pathover_of_squareover'
end
definition squareover_of_pathover {s : p₁₀ ⬝ p₂₁ = p₀₁ ⬝ p₁₂}
(r : q₁₀ ⬝o q₂₁ =[s] q₀₁ ⬝o q₁₂) : squareover B (square_of_eq s) q₁₀ q₁₂ q₀₁ q₂₁ :=
by induction q₁₂; esimp [concato] at r;induction r;induction q₂₁;induction q₁₀;constructor
definition pathover_top_of_squareover (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁)
: q₁₀ =[eq_top_of_square s₁₁] q₀₁ ⬝o q₁₂ ⬝o q₂₁⁻¹ᵒ :=
by induction t₁₁; constructor
definition squareover_of_pathover_top {s : p₁₀ = p₀₁ ⬝ p₁₂ ⬝ p₂₁⁻¹}
(r : q₁₀ =[s] q₀₁ ⬝o q₁₂ ⬝o q₂₁⁻¹ᵒ)
: squareover B (square_of_eq_top s) q₁₀ q₁₂ q₀₁ q₂₁ :=
by induction q₂₁; induction q₁₂; esimp at r;induction r;induction q₁₀;constructor
definition pathover_of_hdeg_squareover {p₀₁' : a₀₀ = a₀₂} {r : p₀₁ = p₀₁'} {q₀₁' : b₀₀ =[p₀₁'] b₀₂}
(t : squareover B (hdeg_square r) idpo idpo q₀₁ q₀₁') : q₀₁ =[r] q₀₁' :=
by induction r; induction q₀₁'; exact (pathover_of_squareover' t)⁻¹ᵒ
definition pathover_of_vdeg_squareover {p₁₀' : a₀₀ = a₂₀} {r : p₁₀ = p₁₀'} {q₁₀' : b₀₀ =[p₁₀'] b₂₀}
(t : squareover B (vdeg_square r) q₁₀ q₁₀' idpo idpo) : q₁₀ =[r] q₁₀' :=
by induction r; induction q₁₀'; exact pathover_of_squareover' t
definition squareover_of_eq_top (r : change_path (eq_top_of_square s₁₁) q₁₀ = q₀₁ ⬝o q₁₂ ⬝o q₂₁⁻¹ᵒ)
: squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁ :=
begin
induction s₁₁, revert q₁₂ q₁₀ r,
eapply idp_rec_on q₂₁, clear q₂₁,
intro q₁₂,
eapply idp_rec_on q₁₂, clear q₁₂,
esimp, intros,
induction r, eapply idp_rec_on q₁₀,
constructor
end
definition eq_top_of_squareover (r : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁)
: change_path (eq_top_of_square s₁₁) q₁₀ = q₀₁ ⬝o q₁₂ ⬝o q₂₁⁻¹ᵒ :=
by induction r; reflexivity
/-
definition squareover_equiv_pathover (q₁₀ : b₀₀ =[p₁₀] b₂₀) (q₁₂ : b₀₂ =[p₁₂] b₂₂)
(q₀₁ : b₀₀ =[p₀₁] b₀₂) (q₂₁ : b₂₀ =[p₂₁] b₂₂)
: squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁ ≃ q₁₀ ⬝o q₂₁ =[eq_of_square s₁₁] q₀₁ ⬝o q₁₂ :=
begin
fapply equiv.MK,
{ exact pathover_of_squareover},
{ intro r, rewrite [-to_left_inv !square_equiv_eq s₁₁], apply squareover_of_pathover, exact r},
{ intro r, }, --need characterization of squareover lying over ids.
{ intro s, induction s, apply idp},
end
-/
definition eq_of_vdeg_squareover {q₁₀' : b₀₀ =[p₁₀] b₂₀}
(p : squareover B vrfl q₁₀ q₁₀' idpo idpo) : q₁₀ = q₁₀' :=
begin
let H := square_of_squareover p, -- use apply ... in
induction p₁₀, -- if needed we can remove this induction and use con_tr_idp in types/eq2
rewrite [▸* at H,idp_con at H,+ap_id at H],
let H' := eq_of_vdeg_square H,
exact eq_of_fn_eq_fn !pathover_equiv_tr_eq H'
end
-- definition vdeg_tr_squareover {q₁₂ : p₀₁ ▸ b₀₀ =[p₁₂] p₂₁ ▸ b₂₀} (r : q₁₀ =[_] q₁₂)
-- : squareover B s₁₁ q₁₀ q₁₂ !pathover_tr !pathover_tr :=
-- by induction p;exact vrflo
/- charcaterization of pathovers in pathovers -/
-- in this version the fibration (B) of the pathover does not depend on the variable a
definition pathover_pathover {a' a₂' : A'} {p : a' = a₂'} {f g : A' → A}
{b : Πa, B (f a)} {b₂ : Πa, B (g a)} {q : Π(a' : A'), f a' = g a'}
(r : pathover B (b a') (q a') (b₂ a'))
(r₂ : pathover B (b a₂') (q a₂') (b₂ a₂'))
(s : squareover B (natural_square_tr q p) r r₂
(pathover_ap B f (apdo b p)) (pathover_ap B g (apdo b₂ p)))
: pathover (λa, pathover B (b a) (q a) (b₂ a)) r p r₂ :=
begin
induction p, esimp at s, apply pathover_idp_of_eq, apply eq_of_vdeg_squareover, exact s
end
end eq
|
67db70ea090d554f41d42cf83171c51ccdc6ae80 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/combinatorics/quiver/subquiver.lean | 55a4aaea26b281758c0683885f6d53b72bb40b42 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 2,495 | lean | /-
Copyright (c) 2021 David Wärn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: David Wärn
-/
import order.bounded_order
import combinatorics.quiver.basic
/-!
## Wide subquivers
A wide subquiver `H` of a quiver `H` consists of a subset of the edge set `a ⟶ b` for
every pair of vertices `a b : V`. We include 'wide' in the name to emphasize that these
subquivers by definition contain all vertices.
-/
universes v u
/-- A wide subquiver `H` of `G` picks out a set `H a b` of arrows from `a` to `b`
for every pair of vertices `a b`.
NB: this does not work for `Prop`-valued quivers. It requires `G : quiver.{v+1} V`. -/
def wide_subquiver (V) [quiver.{v+1} V] :=
Π a b : V, set (a ⟶ b)
/-- A type synonym for `V`, when thought of as a quiver having only the arrows from
some `wide_subquiver`. -/
@[nolint unused_arguments has_nonempty_instance]
def wide_subquiver.to_Type (V) [quiver V] (H : wide_subquiver V) : Type u := V
instance wide_subquiver_has_coe_to_sort {V} [quiver V] :
has_coe_to_sort (wide_subquiver V) (Type u) :=
{ coe := λ H, wide_subquiver.to_Type V H }
/-- A wide subquiver viewed as a quiver on its own. -/
instance wide_subquiver.quiver {V} [quiver V] (H : wide_subquiver V) : quiver H :=
⟨λ a b, { f // f ∈ H a b }⟩
namespace quiver
instance {V} [quiver V] : has_bot (wide_subquiver V) := ⟨λ a b, ∅⟩
instance {V} [quiver V] : has_top (wide_subquiver V) := ⟨λ a b, set.univ⟩
instance {V} [quiver V] : inhabited (wide_subquiver V) := ⟨⊤⟩
/-- `total V` is the type of _all_ arrows of `V`. -/
-- TODO Unify with `category_theory.arrow`? (The fields have been named to match.)
@[ext, nolint has_nonempty_instance]
structure total (V : Type u) [quiver.{v} V] : Sort (max (u+1) v) :=
(left : V)
(right : V)
(hom : left ⟶ right)
/-- A wide subquiver of `G` can equivalently be viewed as a total set of arrows. -/
def wide_subquiver_equiv_set_total {V} [quiver V] :
wide_subquiver V ≃ set (total V) :=
{ to_fun := λ H, { e | e.hom ∈ H e.left e.right },
inv_fun := λ S a b, { e | total.mk a b e ∈ S },
left_inv := λ H, rfl,
right_inv := by { intro S, ext, cases x, refl } }
/-- An `L`-labelling of a quiver assigns to every arrow an element of `L`. -/
def labelling (V : Type u) [quiver V] (L : Sort*) := Π ⦃a b : V⦄, (a ⟶ b) → L
instance {V : Type u} [quiver V] (L) [inhabited L] : inhabited (labelling V L) :=
⟨λ a b e, default⟩
end quiver
|
ecb9b844cb56b83f98983b2fc1edd222daedb3f6 | 0c1546a496eccfb56620165cad015f88d56190c5 | /library/data/vector.lean | 5603c4c61e2f0c9721937dc4ccf268ebb854f2c4 | [
"Apache-2.0"
] | permissive | Solertis/lean | 491e0939957486f664498fbfb02546e042699958 | 84188c5aa1673fdf37a082b2de8562dddf53df3f | refs/heads/master | 1,610,174,257,606 | 1,486,263,620,000 | 1,486,263,620,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,823 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
Tuples are lists of a fixed size.
It is implemented as a subtype.
-/
import data.list
universe variables u v w
def vector (α : Type u) (n : ℕ) := { l : list α // l^.length = n }
namespace vector
variables {α : Type u} {β : Type v} {φ : Type w}
variable {n : ℕ}
instance [decidable_eq α] : decidable_eq (vector α n) :=
begin unfold vector, apply_instance end
@[pattern] def nil : vector α 0 := ⟨[], rfl⟩
@[pattern] def cons : α → vector α n → vector α (nat.succ n)
| a ⟨ v, h ⟩ := ⟨ a::v, congr_arg nat.succ h ⟩
@[reducible] def length (v : vector α n) : ℕ := n
notation a :: b := cons a b
notation `[` l:(foldr `, ` (h t, cons h t) nil `]`) := l
open nat
def head : vector α (nat.succ n) → α
| ⟨ [], h ⟩ := by contradiction
| ⟨ a :: v, h ⟩ := a
theorem head_cons (a : α) : Π (v : vector α n), head (a :: v) = a
| ⟨ l, h ⟩ := rfl
def tail : vector α (succ n) → vector α n
| ⟨ [], h ⟩ := by contradiction
| ⟨ a :: v, h ⟩ := ⟨ v, congr_arg pred h ⟩
theorem tail_cons (a : α) : Π (v : vector α n), tail (a :: v) = v
| ⟨ l, h ⟩ := rfl
def to_list : vector α n → list α | ⟨ l, h ⟩ := l
def append {n m : nat} : vector α n → vector α m → vector α (n + m)
| ⟨ l₁, h₁ ⟩ ⟨ l₂, h₂ ⟩ := ⟨ l₁ ++ l₂, by simp_using_hs ⟩
/- map -/
def map (f : α → β) : vector α n → vector β n
| ⟨ l, h ⟩ := ⟨ list.map f l, by simp_using_hs ⟩
@[simp] lemma map_nil (f : α → β) : map f nil = nil := rfl
lemma map_cons (f : α → β) (a : α) : Π (v : vector α n), map f (a::v) = f a :: map f v
| ⟨l,h⟩ := rfl
def map₂ (f : α → β → φ) : vector α n → vector β n → vector φ n
| ⟨ x, _ ⟩ ⟨ y, _ ⟩ := ⟨ list.map₂ f x y, by simp_using_hs ⟩
def repeat (a : α) (n : ℕ) : vector α n :=
⟨ list.repeat a n, list.length_repeat a n ⟩
def dropn (i : ℕ) : vector α n → vector α (n - i)
| ⟨l, p⟩ := ⟨ list.dropn i l, by simp_using_hs ⟩
def taken (i : ℕ) : vector α n → vector α (min i n)
| ⟨l, p⟩ := ⟨ list.taken i l, by simp_using_hs ⟩
section accum
open prod
variable {σ : Type}
def map_accumr (f : α → σ → σ × β) : vector α n → σ → σ × vector β n
| ⟨ x, px ⟩ c :=
let res := list.map_accumr f x c in
⟨ res.1, res.2, by simp_using_hs ⟩
def map_accumr₂ {α β σ φ : Type} (f : α → β → σ → σ × φ)
: vector α n → vector β n → σ → σ × vector φ n
| ⟨ x, px ⟩ ⟨ y, py ⟩ c :=
let res := list.map_accumr₂ f x y c in
⟨ res.1, res.2, by simp_using_hs ⟩
end accum
end vector
|
cb670f96650ef0970f631e2e361ac5b44ccdba11 | b074a51e20fdb737b2d4c635dd292fc54685e010 | /src/tactic/core.lean | 6b30da95c3ded7ba405c8d0d5224cf2f856402de | [
"Apache-2.0"
] | permissive | minchaowu/mathlib | 2daf6ffdb5a56eeca403e894af88bcaaf65aec5e | 879da1cf04c2baa9eaa7bd2472100bc0335e5c73 | refs/heads/master | 1,609,628,676,768 | 1,564,310,105,000 | 1,564,310,105,000 | 99,461,307 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 38,964 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Simon Hudon, Scott Morrison, Keeley Hoek
-/
import data.dlist.basic category.basic meta.expr meta.rb_map tactic.cache
namespace expr
open tactic
attribute [derive has_reflect] binder_info
protected meta def of_nat (α : expr) : ℕ → tactic expr :=
nat.binary_rec
(tactic.mk_mapp ``has_zero.zero [some α, none])
(λ b n tac, if n = 0 then mk_mapp ``has_one.one [some α, none] else
do e ← tac, tactic.mk_app (cond b ``bit1 ``bit0) [e])
protected meta def of_int (α : expr) : ℤ → tactic expr
| (n : ℕ) := expr.of_nat α n
| -[1+ n] := do
e ← expr.of_nat α (n+1),
tactic.mk_app ``has_neg.neg [e]
/- only traverses the direct descendents -/
meta def {u} traverse {m : Type → Type u} [applicative m]
{elab elab' : bool} (f : expr elab → m (expr elab')) :
expr elab → m (expr elab')
| (var v) := pure $ var v
| (sort l) := pure $ sort l
| (const n ls) := pure $ const n ls
| (mvar n n' e) := mvar n n' <$> f e
| (local_const n n' bi e) := local_const n n' bi <$> f e
| (app e₀ e₁) := app <$> f e₀ <*> f e₁
| (lam n bi e₀ e₁) := lam n bi <$> f e₀ <*> f e₁
| (pi n bi e₀ e₁) := pi n bi <$> f e₀ <*> f e₁
| (elet n e₀ e₁ e₂) := elet n <$> f e₀ <*> f e₁ <*> f e₂
| (macro mac es) := macro mac <$> list.traverse f es
meta def mfoldl {α : Type} {m} [monad m] (f : α → expr → m α) : α → expr → m α
| x e := prod.snd <$> (state_t.run (e.traverse $ λ e',
(get >>= monad_lift ∘ flip f e' >>= put) $> e') x : m _)
end expr
namespace interaction_monad
open result
meta def get_result {σ α} (tac : interaction_monad σ α) :
interaction_monad σ (interaction_monad.result σ α) | s :=
match tac s with
| r@(success _ s') := success r s'
| r@(exception _ _ s') := success r s'
end
end interaction_monad
namespace lean.parser
open lean interaction_monad.result
meta def of_tactic' {α} (tac : tactic α) : parser α :=
do r ← of_tactic (interaction_monad.get_result tac),
match r with
| (success a _) := return a
| (exception f pos _) := exception f pos
end
-- Override the builtin `lean.parser.of_tactic` coe, which is broken.
-- (See test/tactics.lean for a failure case.)
@[priority 2000]
meta instance has_coe' {α} : has_coe (tactic α) (parser α) :=
⟨of_tactic'⟩
meta def emit_command_here (str : string) : lean.parser string :=
do (_, left) ← with_input command_like str,
return left
-- Emit a source code string at the location being parsed.
meta def emit_code_here : string → lean.parser unit
| str := do left ← emit_command_here str,
if left.length = 0 then return ()
else emit_code_here left
end lean.parser
namespace name
meta def head : name → string
| (mk_string s anonymous) := s
| (mk_string s p) := head p
| (mk_numeral n p) := head p
| anonymous := "[anonymous]"
meta def is_private (n : name) : bool :=
n.head = "_private"
meta def last : name → string
| (mk_string s _) := s
| (mk_numeral n _) := repr n
| anonymous := "[anonymous]"
meta def length : name → ℕ
| (mk_string s anonymous) := s.length
| (mk_string s p) := s.length + 1 + p.length
| (mk_numeral n p) := p.length
| anonymous := "[anonymous]".length
end name
namespace environment
meta def decl_filter_map {α : Type} (e : environment) (f : declaration → option α) : list α :=
e.fold [] $ λ d l, match f d with
| some r := r :: l
| none := l
end
meta def decl_map {α : Type} (e : environment) (f : declaration → α) : list α :=
e.decl_filter_map $ λ d, some (f d)
meta def get_decls (e : environment) : list declaration :=
e.decl_map id
meta def get_trusted_decls (e : environment) : list declaration :=
e.decl_filter_map (λ d, if d.is_trusted then some d else none)
meta def get_decl_names (e : environment) : list name :=
e.decl_map declaration.to_name
end environment
namespace format
meta def intercalate (x : format) : list format → format :=
format.join ∘ list.intersperse x
end format
namespace tactic
meta def eval_expr' (α : Type*) [_inst_1 : reflected α] (e : expr) : tactic α :=
mk_app ``id [e] >>= eval_expr α
-- `mk_fresh_name` returns identifiers starting with underscores,
-- which are not legal when emitted by tactic programs. Turn the
-- useful source of random names provided by `mk_fresh_name` into
-- names which are usable by tactic programs.
--
-- The returned name has four components which are all strings.
meta def mk_user_fresh_name : tactic name :=
do nm ← mk_fresh_name,
return $ `user__ ++ nm.pop_prefix.sanitize_name ++ `user__
meta def is_simp_lemma : name → tactic bool :=
succeeds ∘ tactic.has_attribute `simp
meta def local_decls : tactic (name_map declaration) :=
do e ← tactic.get_env,
let xs := e.fold native.mk_rb_map
(λ d s, if environment.in_current_file' e d.to_name
then s.insert d.to_name d else s),
pure xs
/--
Returns a pair (e, t), where `e ← mk_const d.to_name`, and `t = d.type`
but with universe params updated to match the fresh universe metavariables in `e`.
This should have the same effect as just
```
do e ← mk_const d.to_name,
t ← infer_type e,
return (e, t)
```
but is hopefully faster.
-/
meta def decl_mk_const (d : declaration) : tactic (expr × expr) :=
do subst ← d.univ_params.mmap $ λ u, prod.mk u <$> mk_meta_univ,
let e : expr := expr.const d.to_name (prod.snd <$> subst),
return (e, d.type.instantiate_univ_params subst)
meta def simp_lemmas_from_file : tactic name_set :=
do s ← local_decls,
let s := s.map (expr.list_constant ∘ declaration.value),
xs ← s.to_list.mmap ((<$>) name_set.of_list ∘ mfilter tactic.is_simp_lemma ∘ name_set.to_list ∘ prod.snd),
return $ name_set.filter (λ x, ¬ s.contains x) (xs.foldl name_set.union mk_name_set)
meta def file_simp_attribute_decl (attr : name) : tactic unit :=
do s ← simp_lemmas_from_file,
trace format!"run_cmd mk_simp_attr `{attr}",
let lmms := format.join $ list.intersperse " " $ s.to_list.map to_fmt,
trace format!"local attribute [{attr}] {lmms}"
meta def mk_local (n : name) : expr :=
expr.local_const n n binder_info.default (expr.const n [])
meta def local_def_value (e : expr) : tactic expr := do
do (v,_) ← solve_aux `(true) (do
(expr.elet n t v _) ← (revert e >> target)
| fail format!"{e} is not a local definition",
return v),
return v
meta def check_defn (n : name) (e : pexpr) : tactic unit :=
do (declaration.defn _ _ _ d _ _) ← get_decl n,
e' ← to_expr e,
guard (d =ₐ e') <|> trace d >> failed
-- meta def compile_eqn (n : name) (univ : list name) (args : list expr) (val : expr) (num : ℕ) : tactic unit :=
-- do let lhs := (expr.const n $ univ.map level.param).mk_app args,
-- stmt ← mk_app `eq [lhs,val],
-- let vs := stmt.list_local_const,
-- let stmt := stmt.pis vs,
-- (_,pr) ← solve_aux stmt (tactic.intros >> reflexivity),
-- add_decl $ declaration.thm (n <.> "equations" <.> to_string (format!"_eqn_{num}")) univ stmt (pure pr)
meta def to_implicit : expr → expr
| (expr.local_const uniq n bi t) := expr.local_const uniq n binder_info.implicit t
| e := e
meta def pis : list expr → expr → tactic expr
| (e@(expr.local_const uniq pp info _) :: es) f := do
t ← infer_type e,
f' ← pis es f,
pure $ expr.pi pp info t (expr.abstract_local f' uniq)
| _ f := pure f
meta def lambdas : list expr → expr → tactic expr
| (e@(expr.local_const uniq pp info _) :: es) f := do
t ← infer_type e,
f' ← lambdas es f,
pure $ expr.lam pp info t (expr.abstract_local f' uniq)
| _ f := pure f
meta def extract_def (n : name) (trusted : bool) (elab_def : tactic unit) : tactic unit :=
do cxt ← list.map to_implicit <$> local_context,
t ← target,
(eqns,d) ← solve_aux t elab_def,
d ← instantiate_mvars d,
t' ← pis cxt t,
d' ← lambdas cxt d,
let univ := t'.collect_univ_params,
add_decl $ declaration.defn n univ t' d' (reducibility_hints.regular 1 tt) trusted,
applyc n
meta def exact_dec_trivial : tactic unit := `[exact dec_trivial]
/-- Runs a tactic for a result, reverting the state after completion -/
meta def retrieve {α} (tac : tactic α) : tactic α :=
λ s, result.cases_on (tac s)
(λ a s', result.success a s)
result.exception
/-- Repeat a tactic at least once, calling it recursively on all subgoals,
until it fails. This tactic fails if the first invocation fails. -/
meta def repeat1 (t : tactic unit) : tactic unit := t; repeat t
/-- `iterate_range m n t`: Repeat the given tactic at least `m` times and
at most `n` times or until `t` fails. Fails if `t` does not run at least m times. -/
meta def iterate_range : ℕ → ℕ → tactic unit → tactic unit
| 0 0 t := skip
| 0 (n+1) t := try (t >> iterate_range 0 n t)
| (m+1) n t := t >> iterate_range m (n-1) t
meta def replace_at (tac : expr → tactic (expr × expr)) (hs : list expr) (tgt : bool) : tactic bool :=
do to_remove ← hs.mfilter $ λ h, do {
h_type ← infer_type h,
succeeds $ do
(new_h_type, pr) ← tac h_type,
assert h.local_pp_name new_h_type,
mk_eq_mp pr h >>= tactic.exact },
goal_simplified ← succeeds $ do {
guard tgt,
(new_t, pr) ← target >>= tac,
replace_target new_t pr },
to_remove.mmap' (λ h, try (clear h)),
return (¬ to_remove.empty ∨ goal_simplified)
meta def simp_bottom_up' (post : expr → tactic (expr × expr)) (e : expr) (cfg : simp_config := {}) : tactic (expr × expr) :=
prod.snd <$> simplify_bottom_up () (λ _, (<$>) (prod.mk ()) ∘ post) e cfg
meta structure instance_cache :=
(α : expr)
(univ : level)
(inst : name_map expr)
meta def mk_instance_cache (α : expr) : tactic instance_cache :=
do u ← mk_meta_univ,
infer_type α >>= unify (expr.sort (level.succ u)),
u ← get_univ_assignment u,
return ⟨α, u, mk_name_map⟩
namespace instance_cache
meta def get (c : instance_cache) (n : name) : tactic (instance_cache × expr) :=
match c.inst.find n with
| some i := return (c, i)
| none := do e ← mk_app n [c.α] >>= mk_instance,
return (⟨c.α, c.univ, c.inst.insert n e⟩, e)
end
open expr
meta def append_typeclasses : expr → instance_cache → list expr →
tactic (instance_cache × list expr)
| (pi _ binder_info.inst_implicit (app (const n _) (var _)) body) c l :=
do (c, p) ← c.get n, return (c, p :: l)
| _ c l := return (c, l)
meta def mk_app (c : instance_cache) (n : name) (l : list expr) : tactic (instance_cache × expr) :=
do d ← get_decl n,
(c, l) ← append_typeclasses d.type.binding_body c l,
return (c, (expr.const n [c.univ]).mk_app (c.α :: l))
end instance_cache
meta def match_head (e : expr) : expr → tactic unit
| e' :=
unify e e'
<|> do `(_ → %%e') ← whnf e',
v ← mk_mvar,
match_head (e'.instantiate_var v)
meta def find_matching_head : expr → list expr → tactic (list expr)
| e [] := return []
| e (H :: Hs) :=
do t ← infer_type H,
((::) H <$ match_head e t <|> pure id) <*> find_matching_head e Hs
meta def subst_locals (s : list (expr × expr)) (e : expr) : expr :=
(e.abstract_locals (s.map (expr.local_uniq_name ∘ prod.fst)).reverse).instantiate_vars (s.map prod.snd)
meta def set_binder : expr → list binder_info → expr
| e [] := e
| (expr.pi v _ d b) (bi :: bs) := expr.pi v bi d (set_binder b bs)
| e _ := e
meta def last_explicit_arg : expr → tactic expr
| (expr.app f e) :=
do t ← infer_type f >>= whnf,
if t.binding_info = binder_info.default
then pure e
else last_explicit_arg f
| e := pure e
private meta def get_expl_pi_arity_aux : expr → tactic nat
| (expr.pi n bi d b) :=
do m ← mk_fresh_name,
let l := expr.local_const m n bi d,
new_b ← whnf (expr.instantiate_var b l),
r ← get_expl_pi_arity_aux new_b,
if bi = binder_info.default then
return (r + 1)
else
return r
| e := return 0
/-- Compute the arity of explicit arguments of the given (Pi-)type -/
meta def get_expl_pi_arity (type : expr) : tactic nat :=
whnf type >>= get_expl_pi_arity_aux
/-- Compute the arity of explicit arguments of the given function -/
meta def get_expl_arity (fn : expr) : tactic nat :=
infer_type fn >>= get_expl_pi_arity
/-- variation on `assert` where a (possibly incomplete)
proof of the assertion is provided as a parameter.
``(h,gs) ← local_proof `h p tac`` creates a local `h : p` and
use `tac` to (partially) construct a proof for it. `gs` is the
list of remaining goals in the proof of `h`.
The benefits over assert are:
- unlike with ``h ← assert `h p, tac`` , `h` cannot be used by `tac`;
- when `tac` does not complete the proof of `h`, returning the list
of goals allows one to write a tactic using `h` and with the confidence
that a proof will not boil over to goals left over from the proof of `h`,
unlike what would be the case when using `tactic.swap`.
-/
meta def local_proof (h : name) (p : expr) (tac₀ : tactic unit) :
tactic (expr × list expr) :=
focus1 $
do h' ← assert h p,
[g₀,g₁] ← get_goals,
set_goals [g₀], tac₀,
gs ← get_goals,
set_goals [g₁],
return (h', gs)
meta def var_names : expr → list name
| (expr.pi n _ _ b) := n :: var_names b
| _ := []
meta def drop_binders : expr → tactic expr
| (expr.pi n bi t b) := b.instantiate_var <$> mk_local' n bi t >>= drop_binders
| e := pure e
meta def subobject_names (struct_n : name) : tactic (list name × list name) :=
do env ← get_env,
[c] ← pure $ env.constructors_of struct_n | fail "too many constructors",
vs ← var_names <$> (mk_const c >>= infer_type),
fields ← env.structure_fields struct_n,
return $ fields.partition (λ fn, ↑("_" ++ fn.to_string) ∈ vs)
meta def expanded_field_list' : name → tactic (dlist $ name × name) | struct_n :=
do (so,fs) ← subobject_names struct_n,
ts ← so.mmap (λ n, do
e ← mk_const (n.update_prefix struct_n) >>= infer_type >>= drop_binders,
expanded_field_list' $ e.get_app_fn.const_name),
return $ dlist.join ts ++ dlist.of_list (fs.map $ prod.mk struct_n)
open functor function
meta def expanded_field_list (struct_n : name) : tactic (list $ name × name) :=
dlist.to_list <$> expanded_field_list' struct_n
meta def get_classes (e : expr) : tactic (list name) :=
attribute.get_instances `class >>= list.mfilter (λ n,
succeeds $ mk_app n [e] >>= mk_instance)
open nat
meta def mk_mvar_list : ℕ → tactic (list expr)
| 0 := pure []
| (succ n) := (::) <$> mk_mvar <*> mk_mvar_list n
/-- Returns the only goal, or fails if there isn't just one goal. -/
meta def get_goal : tactic expr :=
do gs ← get_goals,
match gs with
| [a] := return a
| [] := fail "there are no goals"
| _ := fail "there are too many goals"
end
/--`iterate_at_most_on_all_goals n t`: repeat the given tactic at most `n` times on all goals,
or until it fails. Always succeeds. -/
meta def iterate_at_most_on_all_goals : nat → tactic unit → tactic unit
| 0 tac := trace "maximal iterations reached"
| (succ n) tac := tactic.all_goals $ (do tac, iterate_at_most_on_all_goals n tac) <|> skip
/--`iterate_at_most_on_subgoals n t`: repeat the tactic `t` at most `n` times on the first
goal and on all subgoals thus produced, or until it fails. Fails iff `t` fails on
current goal. -/
meta def iterate_at_most_on_subgoals : nat → tactic unit → tactic unit
| 0 tac := trace "maximal iterations reached"
| (succ n) tac := focus1 (do tac, iterate_at_most_on_all_goals n tac)
/--`apply_list l`: try to apply the tactics in the list `l` on the first goal, and
fail if none succeeds -/
meta def apply_list_expr : list expr → tactic unit
| [] := fail "no matching rule"
| (h::t) := do interactive.concat_tags (apply h) <|> apply_list_expr t
/-- constructs a list of expressions given a list of p-expressions, as follows:
- if the p-expression is the name of a theorem, use `i_to_expr_for_apply` on it
- if the p-expression is a user attribute, add all the theorems with this attribute
to the list.-/
meta def build_list_expr_for_apply : list pexpr → tactic (list expr)
| [] := return []
| (h::t) := do
tail ← build_list_expr_for_apply t,
a ← i_to_expr_for_apply h,
(do l ← attribute.get_instances (expr.const_name a),
m ← list.mmap mk_const l,
return (m.append tail))
<|> return (a::tail)
/--`apply_rules hs n`: apply the list of rules `hs` (given as pexpr) and `assumption` on the
first goal and the resulting subgoals, iteratively, at most `n` times -/
meta def apply_rules (hs : list pexpr) (n : nat) : tactic unit :=
do l ← build_list_expr_for_apply hs,
iterate_at_most_on_subgoals n (assumption <|> apply_list_expr l)
meta def replace (h : name) (p : pexpr) : tactic unit :=
do h' ← get_local h,
p ← to_expr p,
note h none p,
clear h'
/-- Auxiliary function for `iff_mp` and `iff_mpr`. Takes a name, which should be either `` `iff.mp``
or `` `iff.mpr``. If the passed expression is an iterated function type eventually producing an
`iff`, returns an expression with the `iff` converted to either the forwards or backwards
implication, as requested. -/
meta def mk_iff_mp_app (iffmp : name) : expr → (nat → expr) → option expr
| (expr.pi n bi e t) f := expr.lam n bi e <$> mk_iff_mp_app t (λ n, f (n+1) (expr.var n))
| `(%%a ↔ %%b) f := some $ @expr.const tt iffmp [] a b (f 0)
| _ f := none
meta def iff_mp_core (e ty: expr) : option expr :=
mk_iff_mp_app `iff.mp ty (λ_, e)
meta def iff_mpr_core (e ty: expr) : option expr :=
mk_iff_mp_app `iff.mpr ty (λ_, e)
/-- Given an expression whose type is (a possibly iterated function producing) an `iff`,
create the expression which is the forward implication. -/
meta def iff_mp (e : expr) : tactic expr :=
do t ← infer_type e,
iff_mp_core e t <|> fail "Target theorem must have the form `Π x y z, a ↔ b`"
/-- Given an expression whose type is (a possibly iterated function producing) an `iff`,
create the expression which is the reverse implication. -/
meta def iff_mpr (e : expr) : tactic expr :=
do t ← infer_type e,
iff_mpr_core e t <|> fail "Target theorem must have the form `Π x y z, a ↔ b`"
/--
Attempts to apply `e`, and if that fails, if `e` is an `iff`,
try applying both directions separately.
-/
meta def apply_iff (e : expr) : tactic (list (name × expr)) :=
let ap e := tactic.apply e {new_goals := new_goals.non_dep_only} in
ap e <|> (iff_mp e >>= ap) <|> (iff_mpr e >>= ap)
meta def symm_apply (e : expr) (cfg : apply_cfg := {}) : tactic (list (name × expr)) :=
tactic.apply e cfg <|> (symmetry >> tactic.apply e cfg)
meta def apply_assumption
(asms : tactic (list expr) := local_context)
(tac : tactic unit := skip) : tactic unit :=
do { ctx ← asms,
ctx.any_of (λ H, symm_apply H >> tac) } <|>
do { exfalso,
ctx ← asms,
ctx.any_of (λ H, symm_apply H >> tac) }
<|> fail "assumption tactic failed"
meta def change_core (e : expr) : option expr → tactic unit
| none := tactic.change e
| (some h) :=
do num_reverted : ℕ ← revert h,
expr.pi n bi d b ← target,
tactic.change $ expr.pi n bi e b,
intron num_reverted
/--
assuming olde and newe are defeq when elaborated, replaces occurences of olde with newe at hypothesis h.
-/
meta def change_with_at (olde newe : pexpr) (hyp : name) : tactic unit :=
do h ← get_local hyp,
tp ← infer_type h,
olde ← to_expr olde, newe ← to_expr newe,
let repl_tp := tp.replace (λ a n, if a = olde then some newe else none),
change_core repl_tp (some h)
meta def metavariables : tactic (list expr) :=
do r ← result,
pure (r.list_meta_vars)
/-- Succeeds only if the current goal is a proposition. -/
meta def propositional_goal : tactic unit :=
do goals ← get_goals,
p ← is_proof goals.head,
guard p
/-- Succeeds only if we can construct an instance showing the
current goal is a subsingleton type. -/
meta def subsingleton_goal : tactic unit :=
do goals ← get_goals,
ty ← infer_type goals.head >>= instantiate_mvars,
to_expr ``(subsingleton %%ty) >>= mk_instance >> skip
/-- Succeeds only if the current goal is "terminal", in the sense
that no other goals depend on it. -/
meta def terminal_goal : tactic unit :=
-- We can't merely test for subsingletons, as sometimes in the presence of metavariables
-- `propositional_goal` succeeds while `subsingleton_goal` does not.
propositional_goal <|> subsingleton_goal <|>
do g₀ :: _ ← get_goals,
mvars ← (λ L, list.erase L g₀) <$> metavariables,
mvars.mmap' $ λ g, do
t ← infer_type g >>= instantiate_mvars,
d ← kdepends_on t g₀,
monad.whenb d $
pp t >>= λ s, fail ("The current goal is not terminal: " ++ s.to_string ++ " depends on it.")
meta def triv' : tactic unit := do c ← mk_const `trivial, exact c reducible
variable {α : Type}
private meta def iterate_aux (t : tactic α) : list α → tactic (list α)
| L := (do r ← t, iterate_aux (r :: L)) <|> return L
/-- Apply a tactic as many times as possible, collecting the results in a list. -/
meta def iterate' (t : tactic α) : tactic (list α) :=
list.reverse <$> iterate_aux t []
/-- Like iterate', but fail if the tactic does not succeed at least once. -/
meta def iterate1 (t : tactic α) : tactic (α × list α) :=
do r ← decorate_ex "iterate1 failed: tactic did not succeed" t,
L ← iterate' t,
return (r, L)
meta def intros1 : tactic (list expr) :=
iterate1 intro1 >>= λ p, return (p.1 :: p.2)
/-- `successes` invokes each tactic in turn, returning the list of successful results. -/
meta def successes (tactics : list (tactic α)) : tactic (list α) :=
list.filter_map id <$> monad.sequence (tactics.map (λ t, try_core t))
/-- Return target after instantiating metavars and whnf -/
private meta def target' : tactic expr :=
target >>= instantiate_mvars >>= whnf
/--
Just like `split`, `fsplit` applies the constructor when the type of the target is an inductive data type with one constructor.
However it does not reorder goals or invoke `auto_param` tactics.
-/
-- FIXME check if we can remove `auto_param := ff`
meta def fsplit : tactic unit :=
do [c] ← target' >>= get_constructors_for | tactic.fail "fsplit tactic failed, target is not an inductive datatype with only one constructor",
mk_const c >>= λ e, apply e {new_goals := new_goals.all, auto_param := ff} >> skip
run_cmd add_interactive [`fsplit]
/-- Calls `injection` on each hypothesis, and then, for each hypothesis on which `injection`
succeeds, clears the old hypothesis. -/
meta def injections_and_clear : tactic unit :=
do l ← local_context,
results ← successes $ l.map $ λ e, injection e >> clear e,
when (results.empty) (fail "could not use `injection` then `clear` on any hypothesis")
run_cmd add_interactive [`injections_and_clear]
/-- calls `cases` on every local hypothesis, succeeding if
it succeeds on at least one hypothesis. -/
meta def case_bash : tactic unit :=
do l ← local_context,
r ← successes (l.reverse.map (λ h, cases h >> skip)),
when (r.empty) failed
/-- given a proof `pr : t`, adds `h : t` to the current context, where the name `h` is fresh. -/
meta def note_anon (e : expr) : tactic expr :=
do n ← get_unused_name "lh",
note n none e
/-- `find_local t` returns a local constant with type t, or fails if none exists. -/
meta def find_local (t : pexpr) : tactic expr :=
do t' ← to_expr t,
prod.snd <$> solve_aux t' assumption
/-- `dependent_pose_core l`: introduce dependent hypothesis, where the proofs depend on the values
of the previous local constants. `l` is a list of local constants and their values. -/
meta def dependent_pose_core (l : list (expr × expr)) : tactic unit := do
let lc := l.map prod.fst,
let lm := l.map (λ⟨l, v⟩, (l.local_uniq_name, v)),
t ← target,
new_goal ← mk_meta_var (t.pis lc),
old::other_goals ← get_goals,
set_goals (old :: new_goal :: other_goals),
exact ((new_goal.mk_app lc).instantiate_locals lm),
return ()
/-- like `mk_local_pis` but translating into weak head normal form before checking if it is a Π. -/
meta def mk_local_pis_whnf : expr → tactic (list expr × expr) | e := do
(expr.pi n bi d b) ← whnf e | return ([], e),
p ← mk_local' n bi d,
(ps, r) ← mk_local_pis (expr.instantiate_var b p),
return ((p :: ps), r)
/-- Changes `(h : ∀xs, ∃a:α, p a) ⊢ g` to `(d : ∀xs, a) (s : ∀xs, p (d xs) ⊢ g` -/
meta def choose1 (h : expr) (data : name) (spec : name) : tactic expr := do
t ← infer_type h,
(ctxt, t) ← mk_local_pis_whnf t,
`(@Exists %%α %%p) ← whnf t transparency.all | fail "expected a term of the shape ∀xs, ∃a, p xs a",
α_t ← infer_type α,
expr.sort u ← whnf α_t transparency.all,
value ← mk_local_def data (α.pis ctxt),
t' ← head_beta (p.app (value.mk_app ctxt)),
spec ← mk_local_def spec (t'.pis ctxt),
dependent_pose_core [
(value, ((((expr.const `classical.some [u]).app α).app p).app (h.mk_app ctxt)).lambdas ctxt),
(spec, ((((expr.const `classical.some_spec [u]).app α).app p).app (h.mk_app ctxt)).lambdas ctxt)],
try (tactic.clear h),
intro1,
intro1
/-- Changes `(h : ∀xs, ∃as, p as) ⊢ g` to a list of functions `as`, an a final hypothesis on `p as` -/
meta def choose : expr → list name → tactic unit
| h [] := fail "expect list of variables"
| h [n] := do
cnt ← revert h,
intro n,
intron (cnt - 1),
return ()
| h (n::ns) := do
v ← get_unused_name >>= choose1 h n,
choose v ns
/-- This makes sure that the execution of the tactic does not change the tactic state.
This can be helpful while using rewrite, apply, or expr munging.
Remember to instantiate your metavariables before you're done! -/
meta def lock_tactic_state {α} (t : tactic α) : tactic α
| s := match t s with
| result.success a s' := result.success a s
| result.exception msg pos s' := result.exception msg pos s
end
/-- similar to `mk_local_pis` but make meta variables instead of
local constants -/
meta def mk_meta_pis : expr → tactic (list expr × expr)
| (expr.pi n bi d b) := do
p ← mk_meta_var d,
(ps, r) ← mk_meta_pis (expr.instantiate_var b p),
return ((p :: ps), r)
| e := return ([], e)
/--
Hole command used to fill in a structure's field when specifying an instance.
In the following:
```
instance : monad id :=
{! !}
```
invoking hole command `Instance Stub` produces:
```
instance : monad id :=
{ map := _,
map_const := _,
pure := _,
seq := _,
seq_left := _,
seq_right := _,
bind := _ }
```
-/
@[hole_command] meta def instance_stub : hole_command :=
{ name := "Instance Stub",
descr := "Generate a skeleton for the structure under construction.",
action := λ _,
do tgt ← target >>= whnf,
let cl := tgt.get_app_fn.const_name,
env ← get_env,
fs ← expanded_field_list cl,
let fs := fs.map prod.snd,
let fs := format.intercalate (",\n " : format) $ fs.map (λ fn, format!"{fn} := _"),
let out := format.to_string format!"{{ {fs} }",
return [(out,"")] }
meta def strip_prefix' (n : name) : list string → name → tactic name
| s name.anonymous := pure $ s.foldl (flip name.mk_string) name.anonymous
| s (name.mk_string a p) :=
do let n' := s.foldl (flip name.mk_string) name.anonymous,
do { n'' ← tactic.resolve_constant n',
if n'' = n
then pure n'
else strip_prefix' (a :: s) p }
<|> strip_prefix' (a :: s) p
| s (name.mk_numeral a p) := interaction_monad.failed
meta def strip_prefix : name → tactic name
| n@(name.mk_string a a_1) := strip_prefix' n [a] a_1
| _ := interaction_monad.failed
meta def is_default_local : expr → bool
| (expr.local_const _ _ binder_info.default _) := tt
| _ := ff
meta def mk_patterns (t : expr) : tactic (list format) :=
do let cl := t.get_app_fn.const_name,
env ← get_env,
let fs := env.constructors_of cl,
fs.mmap $ λ f,
do { (vs,_) ← mk_const f >>= infer_type >>= mk_local_pis,
let vs := vs.filter (λ v, is_default_local v),
vs ← vs.mmap (λ v,
do v' ← get_unused_name v.local_pp_name,
pose v' none `(()),
pure v' ),
vs.mmap' $ λ v, get_local v >>= clear,
let args := list.intersperse (" " : format) $ vs.map to_fmt,
f ← strip_prefix f,
if args.empty
then pure $ format!"| {f} := _\n"
else pure format!"| ({f} {format.join args}) := _\n" }
/--
Hole command used to generate a `match` expression.
In the following:
```
meta def foo (e : expr) : tactic unit :=
{! e !}
```
invoking hole command `Match Stub` produces:
```
meta def foo (e : expr) : tactic unit :=
match e with
| (expr.var a) := _
| (expr.sort a) := _
| (expr.const a a_1) := _
| (expr.mvar a a_1 a_2) := _
| (expr.local_const a a_1 a_2 a_3) := _
| (expr.app a a_1) := _
| (expr.lam a a_1 a_2 a_3) := _
| (expr.pi a a_1 a_2 a_3) := _
| (expr.elet a a_1 a_2 a_3) := _
| (expr.macro a a_1) := _
end
```
-/
@[hole_command] meta def match_stub : hole_command :=
{ name := "Match Stub",
descr := "Generate a list of equations for a `match` expression.",
action := λ es,
do [e] ← pure es | fail "expecting one expression",
e ← to_expr e,
t ← infer_type e >>= whnf,
fs ← mk_patterns t,
e ← pp e,
let out := format.to_string format!"match {e} with\n{format.join fs}end\n",
return [(out,"")] }
/--
Hole command used to generate a `match` expression.
In the following:
```
meta def foo : {! expr → tactic unit !} -- `:=` is omitted
```
invoking hole command `Equations Stub` produces:
```
meta def foo : expr → tactic unit
| (expr.var a) := _
| (expr.sort a) := _
| (expr.const a a_1) := _
| (expr.mvar a a_1 a_2) := _
| (expr.local_const a a_1 a_2 a_3) := _
| (expr.app a a_1) := _
| (expr.lam a a_1 a_2 a_3) := _
| (expr.pi a a_1 a_2 a_3) := _
| (expr.elet a a_1 a_2 a_3) := _
| (expr.macro a a_1) := _
```
A similar result can be obtained by invoking `Equations Stub` on the following:
```
meta def foo : expr → tactic unit := -- do not forget to write `:=`!!
{! !}
```
```
meta def foo : expr → tactic unit := -- don't forget to erase `:=`!!
| (expr.var a) := _
| (expr.sort a) := _
| (expr.const a a_1) := _
| (expr.mvar a a_1 a_2) := _
| (expr.local_const a a_1 a_2 a_3) := _
| (expr.app a a_1) := _
| (expr.lam a a_1 a_2 a_3) := _
| (expr.pi a a_1 a_2 a_3) := _
| (expr.elet a a_1 a_2 a_3) := _
| (expr.macro a a_1) := _
```
-/
@[hole_command] meta def eqn_stub : hole_command :=
{ name := "Equations Stub",
descr := "Generate a list of equations for a recursive definition.",
action := λ es,
do t ← match es with
| [t] := to_expr t
| [] := target
| _ := fail "expecting one type"
end,
e ← whnf t,
(v :: _,_) ← mk_local_pis e | fail "expecting a Pi-type",
t' ← infer_type v,
fs ← mk_patterns t',
t ← pp t,
let out :=
if es.empty then
format.to_string format!"-- do not forget to erase `:=`!!\n{format.join fs}"
else format.to_string format!"{t}\n{format.join fs}",
return [(out,"")] }
/--
This command lists the constructors that can be used to satisfy the expected type.
When used in the following hole:
```
def foo : ℤ ⊕ ℕ :=
{! !}
```
the command will produce:
```
def foo : ℤ ⊕ ℕ :=
{! sum.inl, sum.inr !}
```
and will display:
```
sum.inl : ℤ → ℤ ⊕ ℕ
sum.inr : ℕ → ℤ ⊕ ℕ
```
-/
@[hole_command] meta def list_constructors_hole : hole_command :=
{ name := "List Constructors",
descr := "Show the list of constructors of the expected type.",
action := λ es,
do t ← target >>= whnf,
(_,t) ← mk_local_pis t,
let cl := t.get_app_fn.const_name,
let args := t.get_app_args,
env ← get_env,
let cs := env.constructors_of cl,
ts ← cs.mmap $ λ c,
do { e ← mk_const c,
t ← infer_type (e.mk_app args) >>= pp,
c ← strip_prefix c,
pure format!"\n{c} : {t}\n" },
fs ← format.intercalate ", " <$> cs.mmap (strip_prefix >=> pure ∘ to_fmt),
let out := format.to_string format!"{{! {fs} !}",
trace (format.join ts).to_string,
return [(out,"")] }
meta def classical : tactic unit :=
do h ← get_unused_name `_inst,
mk_const `classical.prop_decidable >>= note h none,
reset_instance_cache
open expr
meta def add_prime : name → name
| (name.mk_string s p) := name.mk_string (s ++ "'") p
| n := (name.mk_string "x'" n)
meta def mk_comp (v : expr) : expr → tactic expr
| (app f e) :=
if e = v then pure f
else do
guard (¬ v.occurs f) <|> fail "bad guard",
e' ← mk_comp e >>= instantiate_mvars,
f ← instantiate_mvars f,
mk_mapp ``function.comp [none,none,none,f,e']
| e :=
do guard (e = v),
t ← infer_type e,
mk_mapp ``id [t]
meta def mk_higher_order_type : expr → tactic expr
| (pi n bi d b@(pi _ _ _ _)) :=
do v ← mk_local_def n d,
let b' := (b.instantiate_var v),
(pi n bi d ∘ flip abstract_local v.local_uniq_name) <$> mk_higher_order_type b'
| (pi n bi d b) :=
do v ← mk_local_def n d,
let b' := (b.instantiate_var v),
(l,r) ← match_eq b' <|> fail format!"not an equality {b'}",
l' ← mk_comp v l,
r' ← mk_comp v r,
mk_app ``eq [l',r']
| e := failed
open lean.parser interactive.types
@[user_attribute]
meta def higher_order_attr : user_attribute unit (option name) :=
{ name := `higher_order,
parser := optional ident,
descr :=
"From a lemma of the shape `f (g x) = h x` derive an auxiliary lemma of the
form `f ∘ g = h` for reasoning about higher-order functions.",
after_set := some $ λ lmm _ _,
do env ← get_env,
decl ← env.get lmm,
let num := decl.univ_params.length,
let lvls := (list.iota num).map (`l).append_after,
let l : expr := expr.const lmm $ lvls.map level.param,
t ← infer_type l >>= instantiate_mvars,
t' ← mk_higher_order_type t,
(_,pr) ← solve_aux t' $ do {
intros, applyc ``_root_.funext, intro1, applyc lmm; assumption },
pr ← instantiate_mvars pr,
lmm' ← higher_order_attr.get_param lmm,
lmm' ← (flip name.update_prefix lmm.get_prefix <$> lmm') <|> pure (add_prime lmm),
add_decl $ declaration.thm lmm' lvls t' (pure pr),
copy_attribute `simp lmm tt lmm',
copy_attribute `functor_norm lmm tt lmm' }
attribute [higher_order map_comp_pure] map_pure
private meta def tactic.use_aux (h : pexpr) : tactic unit :=
(focus1 (refine h >> done)) <|> (fconstructor >> tactic.use_aux)
meta def tactic.use (l : list pexpr) : tactic unit :=
focus1 $ l.mmap' $ λ h, tactic.use_aux h <|> fail format!"failed to instantiate goal with {h}"
meta def clear_aux_decl_aux : list expr → tactic unit
| [] := skip
| (e::l) := do cond e.is_aux_decl (tactic.clear e) skip, clear_aux_decl_aux l
meta def clear_aux_decl : tactic unit :=
local_context >>= clear_aux_decl_aux
precedence `setup_tactic_parser`:0
@[user_command]
meta def setup_tactic_parser_cmd (_ : interactive.parse $ tk "setup_tactic_parser") : lean.parser unit :=
emit_code_here "
open lean
open lean.parser
open interactive interactive.types
local postfix `?`:9001 := optional
local postfix *:9001 := many .
"
meta def trace_error (t : tactic α) (msg : string) : tactic α
| s := match t s with
| (result.success r s') := result.success r s'
| (result.exception (some msg) p s') := (trace (msg ()) >> result.exception (some msg) p) s'
| (result.exception none p s') := result.exception none p s'
end
/--
This combinator is for testing purposes. It succeeds if `t` fails with message `msg`,
and fails otherwise.
-/
meta def {u} success_if_fail_with_msg {α : Type u} (t : tactic α) (msg : string) : tactic unit :=
λ s, match t s with
| (interaction_monad.result.exception msg' _ s') :=
if msg = (msg'.iget ()).to_string then result.success () s
else mk_exception "failure messages didn't match" none s
| (interaction_monad.result.success a s) :=
mk_exception "success_if_fail_with_msg combinator failed, given tactic succeeded" none s
end
open lean interactive
meta def pformat := tactic format
meta def pformat.mk (fmt : format) : pformat := pure fmt
meta def to_pfmt {α} [has_to_tactic_format α] (x : α) : pformat :=
pp x
meta instance pformat.has_to_tactic_format : has_to_tactic_format pformat :=
⟨ id ⟩
meta instance : has_append pformat :=
⟨ λ x y, (++) <$> x <*> y ⟩
meta instance tactic.has_to_tactic_format [has_to_tactic_format α] : has_to_tactic_format (tactic α) :=
⟨ λ x, x >>= to_pfmt ⟩
private meta def parse_pformat : string → list char → parser pexpr
| acc [] := pure ``(to_pfmt %%(reflect acc))
| acc ('\n'::s) :=
do f ← parse_pformat "" s,
pure ``(to_pfmt %%(reflect acc) ++ pformat.mk format.line ++ %%f)
| acc ('{'::'{'::s) := parse_pformat (acc ++ "{") s
| acc ('{'::s) :=
do (e, s) ← with_input (lean.parser.pexpr 0) s.as_string,
'}'::s ← return s.to_list | fail "'}' expected",
f ← parse_pformat "" s,
pure ``(to_pfmt %%(reflect acc) ++ to_pfmt %%e ++ %%f)
| acc (c::s) := parse_pformat (acc.str c) s
reserve prefix `pformat! `:100
/-- See `format!` in `init/meta/interactive_base.lean`.
The main differences are that `pp` is called instead of `to_fmt` and that we can use
arguments of type `tactic α` in the quotations.
Now, consider the following:
```
e ← to_expr ``(3 + 7),
trace format!"{e}" -- outputs `has_add.add.{0} nat nat.has_add (bit1.{0} nat nat.has_one nat.has_add (has_one.one.{0} nat nat.has_one)) ...`
trace pformat!"{e}" -- outputs `3 + 7`
```
The difference is significant. And now, the following is expressible:
```
e ← to_expr ``(3 + 7),
trace pformat!"{e} : {infer_type e}" -- outputs `3 + 7 : ℕ`
```
See also: `trace!` and `fail!`
-/
@[user_notation]
meta def pformat_macro (_ : parse $ tk "pformat!") (s : string) : parser pexpr :=
parse_pformat "" s.to_list
reserve prefix `fail! `:100
/--
the combination of `pformat` and `fail`
-/
@[user_notation]
meta def fail_macro (_ : parse $ tk "fail!") (s : string) : parser pexpr :=
do e ← pformat_macro () s,
pure ``((%%e : pformat) >>= fail)
reserve prefix `trace! `:100
/--
the combination of `pformat` and `fail`
-/
@[user_notation]
meta def trace_macro (_ : parse $ tk "trace!") (s : string) : parser pexpr :=
do e ← pformat_macro () s,
pure ``((%%e : pformat) >>= trace)
end tactic
open tactic
|
3c791c4c8ddaf6dab4818d6cb6650750c0bfd10b | 4727251e0cd73359b15b664c3170e5d754078599 | /src/linear_algebra/affine_space/slope.lean | 9613b06504f49ba6c4db165f54b33f04c7208ca5 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 4,991 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import algebra.order.module
import linear_algebra.affine_space.affine_map
import tactic.field_simp
/-!
# Slope of a function
In this file we define the slope of a function `f : k → PE` taking values in an affine space over
`k` and prove some basic theorems about `slope`. The `slope` function naturally appears in the Mean
Value Theorem, and in the proof of the fact that a function with nonnegative second derivative on an
interval is convex on this interval.
## Tags
affine space, slope
-/
open affine_map
variables {k E PE : Type*} [field k] [add_comm_group E] [module k E] [add_torsor E PE]
include E
/-- `slope f a b = (b - a)⁻¹ • (f b -ᵥ f a)` is the slope of a function `f` on the interval
`[a, b]`. Note that `slope f a a = 0`, not the derivative of `f` at `a`. -/
def slope (f : k → PE) (a b : k) : E := (b - a)⁻¹ • (f b -ᵥ f a)
lemma slope_fun_def (f : k → PE) : slope f = λ a b, (b - a)⁻¹ • (f b -ᵥ f a) := rfl
omit E
lemma slope_def_field (f : k → k) (a b : k) : slope f a b = (f b - f a) / (b - a) :=
(div_eq_inv_mul _ _).symm
@[simp] lemma slope_same (f : k → PE) (a : k) : (slope f a a : E) = 0 :=
by rw [slope, sub_self, inv_zero, zero_smul]
include E
lemma slope_def_module (f : k → E) (a b : k) : slope f a b = (b - a)⁻¹ • (f b - f a) := rfl
@[simp] lemma sub_smul_slope (f : k → PE) (a b : k) : (b - a) • slope f a b = f b -ᵥ f a :=
begin
rcases eq_or_ne a b with rfl | hne,
{ rw [sub_self, zero_smul, vsub_self] },
{ rw [slope, smul_inv_smul₀ (sub_ne_zero.2 hne.symm)] }
end
lemma sub_smul_slope_vadd (f : k → PE) (a b : k) : (b - a) • slope f a b +ᵥ f a = f b :=
by rw [sub_smul_slope, vsub_vadd]
@[simp] lemma slope_vadd_const (f : k → E) (c : PE) :
slope (λ x, f x +ᵥ c) = slope f :=
begin
ext a b,
simp only [slope, vadd_vsub_vadd_cancel_right, vsub_eq_sub]
end
@[simp] lemma slope_sub_smul (f : k → E) {a b : k} (h : a ≠ b):
slope (λ x, (x - a) • f x) a b = f b :=
by simp [slope, inv_smul_smul₀ (sub_ne_zero.2 h.symm)]
lemma eq_of_slope_eq_zero {f : k → PE} {a b : k} (h : slope f a b = (0:E)) : f a = f b :=
by rw [← sub_smul_slope_vadd f a b, h, smul_zero, zero_vadd]
lemma affine_map.slope_comp {F PF : Type*} [add_comm_group F] [module k F] [add_torsor F PF]
(f : PE →ᵃ[k] PF) (g : k → PE) (a b : k) :
slope (f ∘ g) a b = f.linear (slope g a b) :=
by simp only [slope, (∘), f.linear.map_smul, f.linear_map_vsub]
lemma linear_map.slope_comp {F : Type*} [add_comm_group F] [module k F]
(f : E →ₗ[k] F) (g : k → E) (a b : k) :
slope (f ∘ g) a b = f (slope g a b) :=
f.to_affine_map.slope_comp g a b
lemma slope_comm (f : k → PE) (a b : k) : slope f a b = slope f b a :=
by rw [slope, slope, ← neg_vsub_eq_vsub_rev, smul_neg, ← neg_smul, neg_inv, neg_sub]
/-- `slope f a c` is a linear combination of `slope f a b` and `slope f b c`. This version
explicitly provides coefficients. If `a ≠ c`, then the sum of the coefficients is `1`, so it is
actually an affine combination, see `line_map_slope_slope_sub_div_sub`. -/
lemma sub_div_sub_smul_slope_add_sub_div_sub_smul_slope (f : k → PE) (a b c : k) :
((b - a) / (c - a)) • slope f a b + ((c - b) / (c - a)) • slope f b c = slope f a c :=
begin
by_cases hab : a = b,
{ subst hab,
rw [sub_self, zero_div, zero_smul, zero_add],
by_cases hac : a = c,
{ simp [hac] },
{ rw [div_self (sub_ne_zero.2 $ ne.symm hac), one_smul] } },
by_cases hbc : b = c, { subst hbc, simp [sub_ne_zero.2 (ne.symm hab)] },
rw [add_comm],
simp_rw [slope, div_eq_inv_mul, mul_smul, ← smul_add,
smul_inv_smul₀ (sub_ne_zero.2 $ ne.symm hab), smul_inv_smul₀ (sub_ne_zero.2 $ ne.symm hbc),
vsub_add_vsub_cancel],
end
/-- `slope f a c` is an affine combination of `slope f a b` and `slope f b c`. This version uses
`line_map` to express this property. -/
lemma line_map_slope_slope_sub_div_sub (f : k → PE) (a b c : k) (h : a ≠ c) :
line_map (slope f a b) (slope f b c) ((c - b) / (c - a)) = slope f a c :=
by field_simp [sub_ne_zero.2 h.symm, ← sub_div_sub_smul_slope_add_sub_div_sub_smul_slope f a b c,
line_map_apply_module]
/-- `slope f a b` is an affine combination of `slope f a (line_map a b r)` and
`slope f (line_map a b r) b`. We use `line_map` to express this property. -/
lemma line_map_slope_line_map_slope_line_map (f : k → PE) (a b r : k) :
line_map (slope f (line_map a b r) b) (slope f a (line_map a b r)) r = slope f a b :=
begin
obtain (rfl|hab) : a = b ∨ a ≠ b := classical.em _, { simp },
rw [slope_comm _ a, slope_comm _ a, slope_comm _ _ b],
convert line_map_slope_slope_sub_div_sub f b (line_map a b r) a hab.symm using 2,
rw [line_map_apply_ring, eq_div_iff (sub_ne_zero.2 hab), sub_mul, one_mul, mul_sub, ← sub_sub,
sub_sub_cancel]
end
|
61a63cde8d948ee7b7d874e240ff5e82f683b3d3 | 32317185abf7e7c963f4c67c190aec61af6b3628 | /tests/lean/run/class8.lean | 47d9d8320a8a8b12ccc18e505574f5275c945fdb | [
"Apache-2.0"
] | permissive | Andrew-Zipperer-unorganized/lean | 198a2317f21198cd8d26e7085e484b86277f17f7 | dcb35008e1474a0abebe632b1dced120e5f8c009 | refs/heads/master | 1,622,526,520,945 | 1,453,576,559,000 | 1,454,612,842,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,087 | lean | import logic data.prod
open tactic prod
inductive inh [class] (A : Type) : Prop :=
intro : A -> inh A
attribute inh.intro [instance]
theorem inh_elim {A : Type} {B : Prop} (H1 : inh A) (H2 : A → B) : B
:= inh.rec H2 H1
theorem inh_exists {A : Type} {P : A → Prop} (H : ∃x, P x) : inh A
:= obtain w Hw, from H, inh.intro w
theorem inh_bool [instance] : inh Prop
:= inh.intro true
theorem inh_fun [instance] {A B : Type} [H : inh B] : inh (A → B)
:= inh.rec (λb, inh.intro (λa : A, b)) H
theorem pair_inh [instance] {A : Type} {B : Type} [H1 : inh A] [H2 : inh B] : inh (prod A B)
:= inh_elim H1 (λa, inh_elim H2 (λb, inh.intro (pair a b)))
definition assump := eassumption
tactic_hint assump
theorem tst {A B : Type} (H : inh B) : inh (A → B → B)
set_option trace.class_instances true
theorem T1 {A B C D : Type} {P : C → Prop} (a : A) (H1 : inh B) (H2 : ∃x, P x) : inh ((A → A) × B × (D → C) × Prop) :=
have h1 [visible] : inh A, from inh.intro a,
have h2 [visible] : inh C, from inh_exists H2,
_
reveal T1
(*
print(get_env():find("T1"):value())
*)
|
69ba02a114fba725825c7f2ebf599134610a2882 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/topology/metric_space/emetric_paracompact.lean | c1dad1d3d4defd6b11be7e2c6ddc1f4e5daf53b2 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 8,558 | lean | /-
Copyright (c) 202 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import set_theory.ordinal.basic
import topology.metric_space.emetric_space
import topology.paracompact
/-!
# (Extended) metric spaces are paracompact
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we provide two instances:
* `emetric.paracompact_space`: a `pseudo_emetric_space` is paracompact; formalization is based
on [MR0236876];
* `emetric.normal_of_metric`: an `emetric_space` is a normal topological space.
## Tags
metric space, paracompact space, normal space
-/
variable {α : Type*}
open_locale ennreal topology
open set
namespace emetric
/-- A `pseudo_emetric_space` is always a paracompact space. Formalization is based
on [MR0236876]. -/
@[priority 100] -- See note [lower instance priority]
instance [pseudo_emetric_space α] : paracompact_space α :=
begin
classical,
/- We start with trivial observations about `1 / 2 ^ k`. Here and below we use `1 / 2 ^ k` in
the comments and `2⁻¹ ^ k` in the code. -/
have pow_pos : ∀ k : ℕ, (0 : ℝ≥0∞) < 2⁻¹ ^ k,
from λ k, ennreal.pow_pos (ennreal.inv_pos.2 ennreal.two_ne_top) _,
have hpow_le : ∀ {m n : ℕ}, m ≤ n → (2⁻¹ : ℝ≥0∞) ^ n ≤ 2⁻¹ ^ m,
from λ m n h, pow_le_pow_of_le_one' (ennreal.inv_le_one.2 ennreal.one_lt_two.le) h,
have h2pow : ∀ n : ℕ, 2 * (2⁻¹ : ℝ≥0∞) ^ (n + 1) = 2⁻¹ ^ n,
by { intro n, simp [pow_succ, ← mul_assoc, ennreal.mul_inv_cancel] },
-- Consider an open covering `S : set (set α)`
refine ⟨λ ι s ho hcov, _⟩,
simp only [Union_eq_univ_iff] at hcov,
-- choose a well founded order on `S`
letI : linear_order ι := linear_order_of_STO well_ordering_rel,
have wf : well_founded ((<) : ι → ι → Prop) := @is_well_founded.wf ι well_ordering_rel _,
-- Let `ind x` be the minimal index `s : S` such that `x ∈ s`.
set ind : α → ι := λ x, wf.min {i : ι | x ∈ s i} (hcov x),
have mem_ind : ∀ x, x ∈ s (ind x), from λ x, wf.min_mem _ (hcov x),
have nmem_of_lt_ind : ∀ {x i}, i < (ind x) → x ∉ s i,
from λ x i hlt hxi, wf.not_lt_min _ (hcov x) hxi hlt,
/- The refinement `D : ℕ → ι → set α` is defined recursively. For each `n` and `i`, `D n i`
is the union of balls `ball x (1 / 2 ^ n)` over all points `x` such that
* `ind x = i`;
* `x` does not belong to any `D m j`, `m < n`;
* `ball x (3 / 2 ^ n) ⊆ s i`;
We define this sequence using `nat.strong_rec_on'`, then restate it as `Dn` and `memD`.
-/
set D : ℕ → ι → set α :=
λ n, nat.strong_rec_on' n (λ n D' i,
⋃ (x : α) (hxs : ind x = i) (hb : ball x (3 * 2⁻¹ ^ n) ⊆ s i)
(hlt : ∀ (m < n) (j : ι), x ∉ D' m ‹_› j), ball x (2⁻¹ ^ n)),
have Dn : ∀ n i, D n i = ⋃ (x : α) (hxs : ind x = i) (hb : ball x (3 * 2⁻¹ ^ n) ⊆ s i)
(hlt : ∀ (m < n) (j : ι), x ∉ D m j), ball x (2⁻¹ ^ n),
from λ n s, by { simp only [D], rw nat.strong_rec_on_beta' },
have memD : ∀ {n i y}, y ∈ D n i ↔ ∃ x (hi : ind x = i) (hb : ball x (3 * 2⁻¹ ^ n) ⊆ s i)
(hlt : ∀ (m < n) (j : ι), x ∉ D m j), edist y x < 2⁻¹ ^ n,
{ intros n i y, rw [Dn n i], simp only [mem_Union, mem_ball] },
-- The sets `D n i` cover the whole space. Indeed, for each `x` we can choose `n` such that
-- `ball x (3 / 2 ^ n) ⊆ s (ind x)`, then either `x ∈ D n i`, or `x ∈ D m i` for some `m < n`.
have Dcov : ∀ x, ∃ n i, x ∈ D n i,
{ intro x,
obtain ⟨n, hn⟩ : ∃ n : ℕ, ball x (3 * 2⁻¹ ^ n) ⊆ s (ind x),
{ -- This proof takes 5 lines because we can't import `specific_limits` here
rcases is_open_iff.1 (ho $ ind x) x (mem_ind x) with ⟨ε, ε0, hε⟩,
have : 0 < ε / 3 := ennreal.div_pos_iff.2 ⟨ε0.lt.ne', ennreal.coe_ne_top⟩,
rcases ennreal.exists_inv_two_pow_lt this.ne' with ⟨n, hn⟩,
refine ⟨n, subset.trans (ball_subset_ball _) hε⟩,
simpa only [div_eq_mul_inv, mul_comm] using (ennreal.mul_lt_of_lt_div hn).le },
by_contra' h,
apply h n (ind x),
exact memD.2 ⟨x, rfl, hn, λ _ _ _, h _ _, mem_ball_self (pow_pos _)⟩ },
-- Each `D n i` is a union of open balls, hence it is an open set
have Dopen : ∀ n i, is_open (D n i),
{ intros n i,
rw Dn,
iterate 4 { refine is_open_Union (λ _, _) },
exact is_open_ball },
-- the covering `D n i` is a refinement of the original covering: `D n i ⊆ s i`
have HDS : ∀ n i, D n i ⊆ s i,
{ intros n s x,
rw memD,
rintro ⟨y, rfl, hsub, -, hyx⟩,
refine hsub (lt_of_lt_of_le hyx _),
calc 2⁻¹ ^ n = 1 * 2⁻¹ ^ n : (one_mul _).symm
... ≤ 3 * 2⁻¹ ^ n : mul_le_mul_right' _ _,
-- TODO: use `norm_num`
have : ((1 : ℕ) : ℝ≥0∞) ≤ (3 : ℕ), from nat.cast_le.2 (by norm_num1),
exact_mod_cast this },
-- Let us show the rest of the properties. Since the definition expects a family indexed
-- by a single parameter, we use `ℕ × ι` as the domain.
refine ⟨ℕ × ι, λ ni, D ni.1 ni.2, λ _, Dopen _ _, _, _, λ ni, ⟨ni.2, HDS _ _⟩⟩,
-- The sets `D n i` cover the whole space as we proved earlier
{ refine Union_eq_univ_iff.2 (λ x, _),
rcases Dcov x with ⟨n, i, h⟩,
exact ⟨⟨n, i⟩, h⟩ },
{ /- Let us prove that the covering `D n i` is locally finite. Take a point `x` and choose
`n`, `i` so that `x ∈ D n i`. Since `D n i` is an open set, we can choose `k` so that
`B = ball x (1 / 2 ^ (n + k + 1)) ⊆ D n i`. -/
intro x,
rcases Dcov x with ⟨n, i, hn⟩,
have : D n i ∈ 𝓝 x, from is_open.mem_nhds (Dopen _ _) hn,
rcases (nhds_basis_uniformity uniformity_basis_edist_inv_two_pow).mem_iff.1 this
with ⟨k, -, hsub : ball x (2⁻¹ ^ k) ⊆ D n i⟩,
set B := ball x (2⁻¹ ^ (n + k + 1)),
refine ⟨B, ball_mem_nhds _ (pow_pos _), _⟩,
-- The sets `D m i`, `m > n + k`, are disjoint with `B`
have Hgt : ∀ (m ≥ n + k + 1) (i : ι), disjoint (D m i) B,
{ rintros m hm i,
rw disjoint_iff_inf_le,
rintros y ⟨hym, hyx⟩,
rcases memD.1 hym with ⟨z, rfl, hzi, H, hz⟩,
have : z ∉ ball x (2⁻¹ ^ k), from λ hz, H n (by linarith) i (hsub hz), apply this,
calc edist z x ≤ edist y z + edist y x : edist_triangle_left _ _ _
... < (2⁻¹ ^ m) + (2⁻¹ ^ (n + k + 1)) : ennreal.add_lt_add hz hyx
... ≤ (2⁻¹ ^ (k + 1)) + (2⁻¹ ^ (k + 1)) :
add_le_add (hpow_le $ by linarith) (hpow_le $ by linarith)
... = (2⁻¹ ^ k) : by rw [← two_mul, h2pow] },
-- For each `m ≤ n + k` there is at most one `j` such that `D m j ∩ B` is nonempty.
have Hle : ∀ m ≤ n + k, set.subsingleton {j | (D m j ∩ B).nonempty},
{ rintros m hm j₁ ⟨y, hyD, hyB⟩ j₂ ⟨z, hzD, hzB⟩,
by_contra' h' : j₁ ≠ j₂,
wlog h : j₁ < j₂ generalizing j₁ j₂ y z,
{ exact this z hzD hzB y hyD hyB h'.symm (h'.lt_or_lt.resolve_left h), },
rcases memD.1 hyD with ⟨y', rfl, hsuby, -, hdisty⟩,
rcases memD.1 hzD with ⟨z', rfl, -, -, hdistz⟩,
suffices : edist z' y' < 3 * 2⁻¹ ^ m, from nmem_of_lt_ind h (hsuby this),
calc edist z' y' ≤ edist z' x + edist x y' : edist_triangle _ _ _
... ≤ (edist z z' + edist z x) + (edist y x + edist y y') :
add_le_add (edist_triangle_left _ _ _) (edist_triangle_left _ _ _)
... < (2⁻¹ ^ m + 2⁻¹ ^ (n + k + 1)) + (2⁻¹ ^ (n + k + 1) + 2⁻¹ ^ m) :
by apply_rules [ennreal.add_lt_add]
... = 2 * (2⁻¹ ^ m + 2⁻¹ ^ (n + k + 1)) : by simp only [two_mul, add_comm]
... ≤ 2 * (2⁻¹ ^ m + 2⁻¹ ^ (m + 1)) :
mul_le_mul' le_rfl $ add_le_add le_rfl $ hpow_le (add_le_add hm le_rfl)
... = 3 * 2⁻¹ ^ m : by rw [mul_add, h2pow, bit1, add_mul, one_mul] },
-- Finally, we glue `Hgt` and `Hle`
have : (⋃ (m ≤ n + k) (i ∈ {i : ι | (D m i ∩ B).nonempty}), {(m, i)}).finite :=
(finite_le_nat _).bUnion' (λ i hi, (Hle i hi).finite.bUnion' (λ _ _, finite_singleton _)),
refine this.subset (λ I hI, _), simp only [mem_Union],
refine ⟨I.1, _, I.2, hI, prod.mk.eta.symm⟩,
exact not_lt.1 (λ hlt, (Hgt I.1 hlt I.2).le_bot hI.some_spec) }
end
@[priority 100] -- see Note [lower instance priority]
instance normal_of_emetric [emetric_space α] : normal_space α := normal_of_paracompact_t2
end emetric
|
84d5d2cf8b0d6bda711e128a5614d052821b1ac3 | 766b82465c89f7c306a9c07004605f5d564fd7f7 | /src/game/basic/level02.lean | c20061d42009e4e6f3ccd1427c677685776fb121 | [
"Apache-2.0"
] | permissive | stanescuUW/integer-number-game | ca4293a46c51db178f3bdb248118075caf87f582 | fced68b04a59ef0f4ea41b5beb2df87e0428c761 | refs/heads/master | 1,669,860,820,240 | 1,597,966,427,000 | 1,597,966,427,000 | 289,131,361 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,103 | lean | import data.nat.prime
import data.rat.basic
import data.real.basic
import tactic
import game.basic.level01
namespace uwyo -- hide
/-
# Chapter 1 : Basic facts
## Level 2
In this level you need to prove a slightly different statement
of the fact that `sqrt 2` is irrational. This will require you to work more closely
with Lean's tools for handling rationals.
-/
-- begin hide
lemma rat_times_rat (r : ℚ) : r * r * ↑(r.denom) ^ 2 = ↑(r.num) ^ 2 :=
begin
have h1 := @rat.mul_denom_eq_num r,
rw pow_two,
rw mul_assoc, rw ← mul_assoc r r.denom r.denom,
rw h1, rw ← mul_assoc, rw mul_comm, rw ← mul_assoc,
rw mul_comm ↑r.denom r, rw h1, rw pow_two, done
end
-- end hide
/- Lemma
There doesn't exist a rational number `r` such that `r ^ 2 = 2`.
-/
theorem rational_not_sqrt_two : ¬ ∃ r : ℚ, r ^ 2 = (2:ℚ) :=
begin
intro h,
cases h with r H,
let num := r.num, set den := r.denom with hden,
--explicitly build the hypothetical rational number r
have hr := @rat.num_denom r,
rw ← hr at H, -- use it in the main assumption
-- now we can figure out some properties of r.num and r.denom
-- first off, the denominator is not zero; this is encoded in r.
have hdenom := r.pos, -- the denom is actually positive
have hdne : r.denom ≠ 0, linarith, -- so it is non zero; linarith can handle that
set n := int.nat_abs num with hn1, -- give it a new name to type less
have hn : (n ^2 : ℤ) = num ^ 2,
norm_cast, rw ← int.nat_abs_pow_two num, rw ← int.coe_nat_pow,
have G : (2:ℚ) * (r.denom ^2) = (r.num ^ 2),
rw ← H, norm_cast, simp,
rw pow_two r, simp * at *,
exact rat_times_rat r,
norm_cast at G,
rw ← hn at G,
have g1 : nat.gcd n den = 1,
have g11 := r.cop,
unfold nat.coprime at g11,
have g12 := nat.coprime.pow_left 2 g11,
have g13 := nat.coprime.coprime_mul_left g12,
rw ← hn1 at g13, exact g13,
have E := sqrt_two_irrational g1,
have g2 := G.symm,
norm_cast at g2,
done
end
end uwyo -- hide
|
dedf5a6a4ff657c8e70c2aa96f22442515ef9c9f | e898bfefd5cb60a60220830c5eba68cab8d02c79 | /uexp/src/uexp/rules/ex1sigmod92.lean | 8586a544328652e92c06845de73fe14e56160d08 | [
"BSD-2-Clause"
] | permissive | kkpapa/Cosette | 9ed09e2dc4c1ecdef815c30b5501f64a7383a2ce | fda8fdbbf0de6c1be9b4104b87bbb06cede46329 | refs/heads/master | 1,584,573,128,049 | 1,526,370,422,000 | 1,526,370,422,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,636 | lean | import ..sql
import ..tactics
import ..u_semiring
import ..extra_constants
import ..cosette_tactics
import ..TDP
open Expr
open Proj
open Pred
open SQL
variables i1 i20 i85 : const datatypes.int
theorem rule :
forall (Γ scm_pur scm_itm scm_itp : Schema)
(rel_pur : relation scm_pur)
(rel_itm : relation scm_itm)
(rel_itp : relation scm_itp)
(pur_ponum : Column datatypes.int scm_pur)
(pur_odate : Column datatypes.int scm_pur)
(pur_vendn : Column datatypes.int scm_pur)
(itm_itemn : Column datatypes.int scm_itm)
(itm_type : Column datatypes.int scm_itm)
(itp_itemn : Column datatypes.int scm_itp)
(itp_ponum : Column datatypes.int scm_itp)
(ik : isKey itm_itemn rel_itm),
denoteSQL
(SELECT1 (combine (right⋅left⋅itm_itemn)
(right⋅right⋅right))
FROM1 (product (table rel_itm)
(DISTINCT (SELECT1 (combine (right⋅left⋅itp_itemn)
(right⋅right⋅pur_vendn))
FROM1 (product (table rel_itp)
(table rel_pur))
WHERE (and (equal (uvariable (right⋅left⋅itp_ponum))
(uvariable (right⋅right⋅pur_ponum)))
(castPred (combine (right⋅right⋅pur_odate)
(e2p (constantExpr i85)))
predicates.gt)))))
WHERE (and (and (equal (uvariable (right⋅left⋅itm_itemn))
(uvariable (right⋅right⋅left)))
(castPred (combine (right⋅left⋅itm_itemn)
(e2p (constantExpr i1)))
predicates.gt))
(castPred (combine (e2p (constantExpr i20))
(right⋅left⋅itm_itemn))
predicates.gt)) : SQL Γ _) =
denoteSQL
(DISTINCT (SELECT1 (combine (right⋅left⋅itm_itemn)
(right⋅right⋅right⋅pur_vendn))
FROM1 (product (table rel_itm)
(product (table rel_itp)
(table rel_pur)))
WHERE (and (and (and (and (equal (uvariable (right⋅right⋅left⋅itp_ponum))
(uvariable (right⋅right⋅right⋅pur_ponum)))
(equal (uvariable (right⋅left⋅itm_itemn))
(uvariable (right⋅right⋅left⋅itp_itemn))))
(castPred (combine (right⋅right⋅right⋅pur_odate)
(e2p (constantExpr i85)))
predicates.gt))
(castPred (combine (right⋅left⋅itm_itemn)
(e2p (constantExpr i1)))
predicates.gt))
(castPred (combine (e2p (constantExpr i20))
(right⋅left⋅itm_itemn))
predicates.gt))) : SQL Γ _) :=
begin
intros,
unfold_all_denotations,
funext,
simp,
unify_sigs,
-- remove_dup_sigs_lhs, -- fail to unify here, need to abstract
sorry
end |
981ef4601d02030b5f3810adecda53e375c1ca9a | 957a80ea22c5abb4f4670b250d55534d9db99108 | /library/init/data/quot.lean | a7e5dfdf4b3be18a49d07b02907f62fc7be85ee3 | [
"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 | 10,372 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
Quotient types.
-/
prelude
/- We import propext here, otherwise we would need a quot.lift for propositions. -/
import init.data.sigma.basic init.logic init.propext init.data.setoid
universes u v
-- iff can now be used to do substitutions in a calculation
attribute [subst]
lemma iff_subst {a b : Prop} {p : Prop → Prop} (h₁ : a ↔ b) (h₂ : p a) : p b :=
eq.subst (propext h₁) h₂
namespace quot
constant sound : Π {α : Sort u} {r : α → α → Prop} {a b : α}, r a b → quot.mk r a = quot.mk r b
attribute [elab_as_eliminator] lift ind
protected lemma lift_beta {α : Sort u} {r : α → α → Prop} {β : Sort v} (f : α → β) (c : ∀ a b, r a b → f a = f b) (a : α) : lift f c (quot.mk r a) = f a :=
rfl
protected lemma ind_beta {α : Sort u} {r : α → α → Prop} {β : quot r → Prop} (p : ∀ a, β (quot.mk r a)) (a : α) : (ind p (quot.mk r a) : β (quot.mk r a)) = p a :=
rfl
attribute [reducible, elab_as_eliminator]
protected def lift_on {α : Sort u} {β : Sort v} {r : α → α → Prop} (q : quot r) (f : α → β) (c : ∀ a b, r a b → f a = f b) : β :=
lift f c q
attribute [elab_as_eliminator]
protected lemma induction_on {α : Sort u} {r : α → α → Prop} {β : quot r → Prop} (q : quot r) (h : ∀ a, β (quot.mk r a)) : β q :=
ind h q
lemma exists_rep {α : Sort u} {r : α → α → Prop} (q : quot r) : ∃ a : α, (quot.mk r a) = q :=
quot.induction_on q (λ a, ⟨a, rfl⟩)
section
variable {α : Sort u}
variable {r : α → α → Prop}
variable {β : quot r → Sort v}
local notation `⟦`:max a `⟧` := quot.mk r a
attribute [reducible]
protected def indep (f : Π a, β ⟦a⟧) (a : α) : psigma β :=
⟨⟦a⟧, f a⟩
protected lemma indep_coherent (f : Π a, β ⟦a⟧)
(h : ∀ (a b : α) (p : r a b), (eq.rec (f a) (sound p) : β ⟦b⟧) = f b)
: ∀ a b, r a b → quot.indep f a = quot.indep f b :=
λ a b e, psigma.eq (sound e) (h a b e)
protected lemma lift_indep_pr1
(f : Π a, β ⟦a⟧) (h : ∀ (a b : α) (p : r a b), (eq.rec (f a) (sound p) : β ⟦b⟧) = f b)
(q : quot r) : (lift (quot.indep f) (quot.indep_coherent f h) q).1 = q :=
quot.ind (λ (a : α), eq.refl (quot.indep f a).1) q
attribute [reducible, elab_as_eliminator]
protected def rec
(f : Π a, β ⟦a⟧) (h : ∀ (a b : α) (p : r a b), (eq.rec (f a) (sound p) : β ⟦b⟧) = f b)
(q : quot r) : β q :=
eq.rec_on (quot.lift_indep_pr1 f h q) ((lift (quot.indep f) (quot.indep_coherent f h) q).2)
attribute [reducible, elab_as_eliminator]
protected def rec_on
(q : quot r) (f : Π a, β ⟦a⟧) (h : ∀ (a b : α) (p : r a b), (eq.rec (f a) (sound p) : β ⟦b⟧) = f b) : β q :=
quot.rec f h q
attribute [reducible, elab_as_eliminator]
protected def rec_on_subsingleton
[h : ∀ a, subsingleton (β ⟦a⟧)] (q : quot r) (f : Π a, β ⟦a⟧) : β q :=
quot.rec f (λ a b h, subsingleton.elim _ (f b)) q
attribute [reducible, elab_as_eliminator]
protected def hrec_on
(q : quot r) (f : Π a, β ⟦a⟧) (c : ∀ (a b : α) (p : r a b), f a == f b) : β q :=
quot.rec_on q f
(λ a b p, eq_of_heq (calc
(eq.rec (f a) (sound p) : β ⟦b⟧) == f a : eq_rec_heq (sound p) (f a)
... == f b : c a b p))
end
end quot
def quotient {α : Sort u} (s : setoid α) :=
@quot α setoid.r
namespace quotient
protected def mk {α : Sort u} [s : setoid α] (a : α) : quotient s :=
quot.mk setoid.r a
notation `⟦`:max a `⟧`:0 := quotient.mk a
def sound {α : Sort u} [s : setoid α] {a b : α} : a ≈ b → ⟦a⟧ = ⟦b⟧ :=
quot.sound
attribute [reducible, elab_as_eliminator]
protected def lift {α : Sort u} {β : Sort v} [s : setoid α] (f : α → β) : (∀ a b, a ≈ b → f a = f b) → quotient s → β :=
quot.lift f
attribute [elab_as_eliminator]
protected lemma ind {α : Sort u} [s : setoid α] {β : quotient s → Prop} : (∀ a, β ⟦a⟧) → ∀ q, β q :=
quot.ind
attribute [reducible, elab_as_eliminator]
protected def lift_on {α : Sort u} {β : Sort v} [s : setoid α] (q : quotient s) (f : α → β) (c : ∀ a b, a ≈ b → f a = f b) : β :=
quot.lift_on q f c
attribute [elab_as_eliminator]
protected lemma induction_on {α : Sort u} [s : setoid α] {β : quotient s → Prop} (q : quotient s) (h : ∀ a, β ⟦a⟧) : β q :=
quot.induction_on q h
lemma exists_rep {α : Sort u} [s : setoid α] (q : quotient s) : ∃ a : α, ⟦a⟧ = q :=
quot.exists_rep q
section
variable {α : Sort u}
variable [s : setoid α]
variable {β : quotient s → Sort v}
protected def rec
(f : Π a, β ⟦a⟧) (h : ∀ (a b : α) (p : a ≈ b), (eq.rec (f a) (quotient.sound p) : β ⟦b⟧) = f b)
(q : quotient s) : β q :=
quot.rec f h q
attribute [reducible, elab_as_eliminator]
protected def rec_on
(q : quotient s) (f : Π a, β ⟦a⟧) (h : ∀ (a b : α) (p : a ≈ b), (eq.rec (f a) (quotient.sound p) : β ⟦b⟧) = f b) : β q :=
quot.rec_on q f h
attribute [reducible, elab_as_eliminator]
protected def rec_on_subsingleton
[h : ∀ a, subsingleton (β ⟦a⟧)] (q : quotient s) (f : Π a, β ⟦a⟧) : β q :=
@quot.rec_on_subsingleton _ _ _ h q f
attribute [reducible, elab_as_eliminator]
protected def hrec_on
(q : quotient s) (f : Π a, β ⟦a⟧) (c : ∀ (a b : α) (p : a ≈ b), f a == f b) : β q :=
quot.hrec_on q f c
end
section
universes u_a u_b u_c
variables {α : Sort u_a} {β : Sort u_b} {φ : Sort u_c}
variables [s₁ : setoid α] [s₂ : setoid β]
include s₁ s₂
attribute [reducible, elab_as_eliminator]
protected def lift₂
(f : α → β → φ)(c : ∀ a₁ a₂ b₁ b₂, a₁ ≈ b₁ → a₂ ≈ b₂ → f a₁ a₂ = f b₁ b₂)
(q₁ : quotient s₁) (q₂ : quotient s₂) : φ :=
quotient.lift
(λ (a₁ : α), quotient.lift (f a₁) (λ (a b : β), c a₁ a a₁ b (setoid.refl a₁)) q₂)
(λ (a b : α) (h : a ≈ b),
@quotient.ind β s₂
(λ (a_1 : quotient s₂),
(quotient.lift (f a) (λ (a_1 b : β), c a a_1 a b (setoid.refl a)) a_1)
=
(quotient.lift (f b) (λ (a b_1 : β), c b a b b_1 (setoid.refl b)) a_1))
(λ (a' : β), c a a' b a' h (setoid.refl a'))
q₂)
q₁
attribute [reducible, elab_as_eliminator]
protected def lift_on₂
(q₁ : quotient s₁) (q₂ : quotient s₂) (f : α → β → φ) (c : ∀ a₁ a₂ b₁ b₂, a₁ ≈ b₁ → a₂ ≈ b₂ → f a₁ a₂ = f b₁ b₂) : φ :=
quotient.lift₂ f c q₁ q₂
attribute [elab_as_eliminator]
protected lemma ind₂ {φ : quotient s₁ → quotient s₂ → Prop} (h : ∀ a b, φ ⟦a⟧ ⟦b⟧) (q₁ : quotient s₁) (q₂ : quotient s₂) : φ q₁ q₂ :=
quotient.ind (λ a₁, quotient.ind (λ a₂, h a₁ a₂) q₂) q₁
attribute [elab_as_eliminator]
protected lemma induction_on₂
{φ : quotient s₁ → quotient s₂ → Prop} (q₁ : quotient s₁) (q₂ : quotient s₂) (h : ∀ a b, φ ⟦a⟧ ⟦b⟧) : φ q₁ q₂ :=
quotient.ind (λ a₁, quotient.ind (λ a₂, h a₁ a₂) q₂) q₁
attribute [elab_as_eliminator]
protected lemma induction_on₃
[s₃ : setoid φ]
{δ : quotient s₁ → quotient s₂ → quotient s₃ → Prop} (q₁ : quotient s₁) (q₂ : quotient s₂) (q₃ : quotient s₃) (h : ∀ a b c, δ ⟦a⟧ ⟦b⟧ ⟦c⟧)
: δ q₁ q₂ q₃ :=
quotient.ind (λ a₁, quotient.ind (λ a₂, quotient.ind (λ a₃, h a₁ a₂ a₃) q₃) q₂) q₁
end
section exact
variable {α : Sort u}
variable [s : setoid α]
include s
private def rel (q₁ q₂ : quotient s) : Prop :=
quotient.lift_on₂ q₁ q₂
(λ a₁ a₂, a₁ ≈ a₂)
(λ a₁ a₂ b₁ b₂ a₁b₁ a₂b₂,
propext (iff.intro
(λ a₁a₂, setoid.trans (setoid.symm a₁b₁) (setoid.trans a₁a₂ a₂b₂))
(λ b₁b₂, setoid.trans a₁b₁ (setoid.trans b₁b₂ (setoid.symm a₂b₂)))))
local infix `~` := rel
private lemma rel.refl : ∀ q : quotient s, q ~ q :=
λ q, quot.induction_on q (λ a, setoid.refl a)
private lemma eq_imp_rel {q₁ q₂ : quotient s} : q₁ = q₂ → q₁ ~ q₂ :=
assume h, eq.rec_on h (rel.refl q₁)
lemma exact {a b : α} : ⟦a⟧ = ⟦b⟧ → a ≈ b :=
assume h, eq_imp_rel h
end exact
section
universes u_a u_b u_c
variables {α : Sort u_a} {β : Sort u_b}
variables [s₁ : setoid α] [s₂ : setoid β]
include s₁ s₂
attribute [reducible, elab_as_eliminator]
protected def rec_on_subsingleton₂
{φ : quotient s₁ → quotient s₂ → Sort u_c} [h : ∀ a b, subsingleton (φ ⟦a⟧ ⟦b⟧)]
(q₁ : quotient s₁) (q₂ : quotient s₂) (f : Π a b, φ ⟦a⟧ ⟦b⟧) : φ q₁ q₂:=
@quotient.rec_on_subsingleton _ s₁ (λ q, φ q q₂) (λ a, quotient.ind (λ b, h a b) q₂) q₁
(λ a, quotient.rec_on_subsingleton q₂ (λ b, f a b))
end
end quotient
section
variable {α : Type u}
variable (r : α → α → Prop)
inductive eqv_gen : α → α → Prop
| rel {} : Π x y, r x y → eqv_gen x y
| refl {} : Π x, eqv_gen x x
| symm {} : Π x y, eqv_gen x y → eqv_gen y x
| trans {} : Π x y z, eqv_gen x y → eqv_gen y z → eqv_gen x z
theorem eqv_gen.is_equivalence : equivalence (@eqv_gen α r) :=
mk_equivalence _ eqv_gen.refl eqv_gen.symm eqv_gen.trans
def eqv_gen.setoid : setoid α :=
setoid.mk _ (eqv_gen.is_equivalence r)
theorem quot.exact {a b : α} (H : quot.mk r a = quot.mk r b) : eqv_gen r a b :=
@quotient.exact _ (eqv_gen.setoid r) a b (@congr_arg _ _ _ _
(quot.lift (@quotient.mk _ (eqv_gen.setoid r)) (λx y h, quot.sound (eqv_gen.rel x y h))) H)
theorem quot.eqv_gen_sound {r : α → α → Prop} {a b : α} (H : eqv_gen r a b) : quot.mk r a = quot.mk r b :=
eqv_gen.rec_on H
(λ x y h, quot.sound h)
(λ x, rfl)
(λ x y _ IH, eq.symm IH)
(λ x y z _ _ IH₁ IH₂, eq.trans IH₁ IH₂)
end
open decidable
instance {α : Sort u} {s : setoid α} [d : ∀ a b : α, decidable (a ≈ b)] : decidable_eq (quotient s) :=
λ q₁ q₂ : quotient s,
quotient.rec_on_subsingleton₂ q₁ q₂
(λ a₁ a₂,
match (d a₁ a₂) with
| (is_true h₁) := is_true (quotient.sound h₁)
| (is_false h₂) := is_false (λ h, absurd (quotient.exact h) h₂)
end)
|
bbf4d98c6a4b2e4bdcce667ebcfd5f62a9c154dd | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/analysis/normed_space/indicator_function.lean | 8dc7e9d15c95ecc9f16a781653776760da0f336a | [
"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 | 1,340 | lean | /-
Copyright (c) 2020 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou, Yury Kudryashov
-/
import analysis.normed.group.basic
import algebra.indicator_function
/-!
# Indicator function and norm
This file contains a few simple lemmas about `set.indicator` and `norm`.
## Tags
indicator, norm
-/
variables {α E : Type*} [normed_group E] {s t : set α} (f : α → E) (a : α)
open set
lemma norm_indicator_eq_indicator_norm :
∥indicator s f a∥ = indicator s (λa, ∥f a∥) a :=
flip congr_fun a (indicator_comp_of_zero norm_zero).symm
lemma nnnorm_indicator_eq_indicator_nnnorm :
nnnorm (indicator s f a) = indicator s (λa, nnnorm (f a)) a :=
flip congr_fun a (indicator_comp_of_zero nnnorm_zero).symm
lemma norm_indicator_le_of_subset (h : s ⊆ t) (f : α → E) (a : α) :
∥indicator s f a∥ ≤ ∥indicator t f a∥ :=
begin
simp only [norm_indicator_eq_indicator_norm],
exact indicator_le_indicator_of_subset ‹_› (λ _, norm_nonneg _) _
end
lemma indicator_norm_le_norm_self : indicator s (λa, ∥f a∥) a ≤ ∥f a∥ :=
indicator_le_self' (λ _ _, norm_nonneg _) a
lemma norm_indicator_le_norm_self : ∥indicator s f a∥ ≤ ∥f a∥ :=
by { rw norm_indicator_eq_indicator_norm, apply indicator_norm_le_norm_self }
|
134ef910ceec3631e8f9eb0aeed91ac2c2f98632 | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/algebra/regular.lean | b0deb0daf01954c2a23af1ddf39c526e69125e46 | [
"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 | 12,378 | lean | /-
Copyright (c) 2021 Damiano Testa. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Damiano Testa
-/
import algebra.group
import algebra.group_power.basic
import algebra.iterate_hom
/-!
# Regular elements
We introduce left-regular, right-regular and regular elements.
By definition, a regular element in a commutative ring is a non-zero divisor.
Lemma `is_regular_of_ne_zero` implies that every non-zero element of an integral domain is regular.
Since it assumes that the ring is a `cancel_monoid_with_zero` it applies also, for instance, to `ℕ`.
The lemmas in Section `mul_zero_class` show that the `0` element is (left/right-)regular if and
only if the `mul_zero_class` is trivial. This is useful when figuring out stopping conditions for
regular sequences: if `0` is ever an element of a regular sequence, then we can extend the sequence
by adding one further `0`.
The final goal is to develop part of the API to prove, eventually, results about non-zero-divisors.
-/
variables {R : Type*} {a b : R}
section has_mul
variable [has_mul R]
/-- A left-regular element is an element `c` such that multiplication on the left by `c`
is injective on the left. -/
def is_left_regular (c : R) := function.injective ((*) c)
/-- A right-regular element is an element `c` such that multiplication on the right by `c`
is injective on the right. -/
def is_right_regular (c : R) := function.injective (* c)
/-- A regular element is an element `c` such that multiplication by `c` both on the left and
on the right is injective. -/
structure is_regular (c : R) : Prop :=
(left : is_left_regular c)
(right : is_right_regular c)
end has_mul
section semigroup
variable [semigroup R]
/-- In a semigroup, the product of left-regular elements is left-regular. -/
lemma is_left_regular.mul (lra : is_left_regular a) (lrb : is_left_regular b) :
is_left_regular (a * b) :=
show function.injective ((*) (a * b)), from (comp_mul_left a b) ▸ lra.comp lrb
/-- In a semigroup, the product of right-regular elements is right-regular. -/
lemma is_right_regular.mul (rra : is_right_regular a) (rrb : is_right_regular b) :
is_right_regular (a * b) :=
show function.injective (* (a * b)), from (comp_mul_right b a) ▸ rrb.comp rra
/-- If an element `b` becomes left-regular after multiplying it on the left by a left-regular
element, then `b` is left-regular. -/
lemma is_left_regular.of_mul (ab : is_left_regular (a * b)) :
is_left_regular b :=
function.injective.of_comp (by rwa comp_mul_left a b)
/-- An element is left-regular if and only if multiplying it on the left by a left-regular element
is left-regular. -/
@[simp] lemma mul_is_left_regular_iff (b : R) (ha : is_left_regular a) :
is_left_regular (a * b) ↔ is_left_regular b :=
⟨λ ab, is_left_regular.of_mul ab, λ ab, is_left_regular.mul ha ab⟩
/-- If an element `b` becomes right-regular after multiplying it on the right by a right-regular
element, then `b` is right-regular. -/
lemma is_right_regular.of_mul (ab : is_right_regular (b * a)) :
is_right_regular b :=
begin
refine λ x y xy, ab (_ : x * (b * a) = y * (b * a)),
rw [← mul_assoc, ← mul_assoc],
exact congr_fun (congr_arg has_mul.mul xy) a,
end
/-- An element is right-regular if and only if multiplying it on the right with a right-regular
element is right-regular. -/
@[simp] lemma mul_is_right_regular_iff (b : R) (ha : is_right_regular a) :
is_right_regular (b * a) ↔ is_right_regular b :=
⟨λ ab, is_right_regular.of_mul ab, λ ab, is_right_regular.mul ab ha⟩
/-- Two elements `a` and `b` are regular if and only if both products `a * b` and `b * a`
are regular. -/
lemma is_regular_mul_and_mul_iff :
is_regular (a * b) ∧ is_regular (b * a) ↔ is_regular a ∧ is_regular b :=
begin
refine ⟨_, _⟩,
{ rintros ⟨ab, ba⟩,
exact ⟨⟨is_left_regular.of_mul ba.left, is_right_regular.of_mul ab.right⟩,
⟨is_left_regular.of_mul ab.left, is_right_regular.of_mul ba.right⟩⟩ },
{ rintros ⟨ha, hb⟩,
exact ⟨⟨(mul_is_left_regular_iff _ ha.left).mpr hb.left,
(mul_is_right_regular_iff _ hb.right).mpr ha.right⟩,
⟨(mul_is_left_regular_iff _ hb.left).mpr ha.left,
(mul_is_right_regular_iff _ ha.right).mpr hb.right⟩⟩ }
end
/-- The "most used" implication of `mul_and_mul_iff`, with split hypotheses, instead of `∧`. -/
lemma is_regular.and_of_mul_of_mul (ab : is_regular (a * b)) (ba : is_regular (b * a)) :
is_regular a ∧ is_regular b :=
is_regular_mul_and_mul_iff.mp ⟨ab, ba⟩
end semigroup
section monoid
variable [monoid R]
/-- Any power of a left-regular element is left-regular. -/
lemma is_left_regular.pow (n : ℕ) (rla : is_left_regular a) : is_left_regular (a ^ n) :=
by simp only [is_left_regular, ← mul_left_iterate, rla.iterate n]
/-- Any power of a right-regular element is right-regular. -/
lemma is_right_regular.pow (n : ℕ) (rra : is_right_regular a) : is_right_regular (a ^ n) :=
by { rw [is_right_regular, ← mul_right_iterate], exact rra.iterate n }
/-- Any power of a regular element is regular. -/
lemma is_regular.pow (n : ℕ) (ra : is_regular a) : is_regular (a ^ n) :=
⟨is_left_regular.pow n ra.left, is_right_regular.pow n ra.right⟩
/-- An element `a` is left-regular if and only if a positive power of `a` is left-regular. -/
lemma is_left_regular.pow_iff {n : ℕ} (n0 : 0 < n) :
is_left_regular (a ^ n) ↔ is_left_regular a :=
begin
refine ⟨_, is_left_regular.pow n⟩,
rw [← nat.succ_pred_eq_of_pos n0, pow_succ'],
exact is_left_regular.of_mul,
end
/-- An element `a` is right-regular if and only if a positive power of `a` is right-regular. -/
lemma is_right_regular.pow_iff {n : ℕ} (n0 : 0 < n) :
is_right_regular (a ^ n) ↔ is_right_regular a :=
begin
refine ⟨_, is_right_regular.pow n⟩,
rw [← nat.succ_pred_eq_of_pos n0, pow_succ],
exact is_right_regular.of_mul,
end
/-- An element `a` is regular if and only if a positive power of `a` is regular. -/
lemma is_regular.pow_iff {n : ℕ} (n0 : 0 < n) :
is_regular (a ^ n) ↔ is_regular a :=
⟨λ h, ⟨(is_left_regular.pow_iff n0).mp h.left, (is_right_regular.pow_iff n0).mp h.right⟩,
λ h, ⟨is_left_regular.pow n h.left, is_right_regular.pow n h.right⟩⟩
end monoid
section mul_zero_class
variables [mul_zero_class R]
/-- The element `0` is left-regular if and only if `R` is trivial. -/
lemma is_left_regular.subsingleton (h : is_left_regular (0 : R)) : subsingleton R :=
⟨λ a b, h $ eq.trans (zero_mul a) (zero_mul b).symm⟩
/-- The element `0` is right-regular if and only if `R` is trivial. -/
lemma is_right_regular.subsingleton (h : is_right_regular (0 : R)) : subsingleton R :=
⟨λ a b, h $ eq.trans (mul_zero a) (mul_zero b).symm⟩
/-- The element `0` is regular if and only if `R` is trivial. -/
lemma is_regular.subsingleton (h : is_regular (0 : R)) : subsingleton R :=
h.left.subsingleton
/-- The element `0` is left-regular if and only if `R` is trivial. -/
lemma is_left_regular_zero_iff_subsingleton : is_left_regular (0 : R) ↔ subsingleton R :=
begin
refine ⟨λ h, h.subsingleton, _⟩,
intros H a b h,
exact @subsingleton.elim _ H a b
end
/-- In a non-trivial `mul_zero_class`, the `0` element is not left-regular. -/
lemma not_is_left_regular_zero_iff : ¬ is_left_regular (0 : R) ↔ nontrivial R :=
begin
rw [nontrivial_iff, not_iff_comm, is_left_regular_zero_iff_subsingleton, subsingleton_iff],
push_neg,
exact iff.rfl
end
/-- The element `0` is right-regular if and only if `R` is trivial. -/
lemma is_right_regular_zero_iff_subsingleton : is_right_regular (0 : R) ↔ subsingleton R :=
begin
refine ⟨λ h, h.subsingleton, _⟩,
intros H a b h,
exact @subsingleton.elim _ H a b
end
/-- In a non-trivial `mul_zero_class`, the `0` element is not right-regular. -/
lemma not_is_right_regular_zero_iff : ¬ is_right_regular (0 : R) ↔ nontrivial R :=
begin
rw [nontrivial_iff, not_iff_comm, is_right_regular_zero_iff_subsingleton, subsingleton_iff],
push_neg,
exact iff.rfl
end
/-- The element `0` is regular if and only if `R` is trivial. -/
lemma is_regular_iff_subsingleton : is_regular (0 : R) ↔ subsingleton R :=
⟨λ h, h.left.subsingleton,
λ h, ⟨is_left_regular_zero_iff_subsingleton.mpr h, is_right_regular_zero_iff_subsingleton.mpr h⟩⟩
/-- A left-regular element of a `nontrivial` `mul_zero_class` is non-zero. -/
lemma is_left_regular.ne_zero [nontrivial R] (la : is_left_regular a) : a ≠ 0 :=
begin
rintro rfl,
rcases exists_pair_ne R with ⟨x, y, xy⟩,
refine xy (la _),
rw [zero_mul, zero_mul]
end
/-- A right-regular element of a `nontrivial` `mul_zero_class` is non-zero. -/
lemma is_right_regular.ne_zero [nontrivial R] (ra : is_right_regular a) : a ≠ 0 :=
begin
rintro rfl,
rcases exists_pair_ne R with ⟨x, y, xy⟩,
refine xy (ra (_ : x * 0 = y * 0)),
rw [mul_zero, mul_zero]
end
/-- A regular element of a `nontrivial` `mul_zero_class` is non-zero. -/
lemma is_regular.ne_zero [nontrivial R] (la : is_regular a) : a ≠ 0 :=
la.left.ne_zero
/-- In a non-trivial ring, the element `0` is not left-regular -- with typeclasses. -/
lemma not_is_left_regular_zero [nR : nontrivial R] : ¬ is_left_regular (0 : R) :=
not_is_left_regular_zero_iff.mpr nR
/-- In a non-trivial ring, the element `0` is not right-regular -- with typeclasses. -/
lemma not_is_right_regular_zero [nR : nontrivial R] : ¬ is_right_regular (0 : R) :=
not_is_right_regular_zero_iff.mpr nR
/-- In a non-trivial ring, the element `0` is not regular -- with typeclasses. -/
lemma not_is_regular_zero [nontrivial R] : ¬ is_regular (0 : R) :=
λ h, is_regular.ne_zero h rfl
end mul_zero_class
section comm_semigroup
variable [comm_semigroup R]
/-- A product is regular if and only if the factors are. -/
lemma is_regular_mul_iff : is_regular (a * b) ↔ is_regular a ∧ is_regular b :=
begin
refine iff.trans _ is_regular_mul_and_mul_iff,
refine ⟨λ ab, ⟨ab, by rwa mul_comm⟩, λ rab, rab.1⟩
end
end comm_semigroup
section monoid
variables [monoid R]
/-- In a monoid, `1` is regular. -/
lemma is_regular_one : is_regular (1 : R) :=
⟨λ a b ab, (one_mul a).symm.trans (eq.trans ab (one_mul b)),
λ a b ab, (mul_one a).symm.trans (eq.trans ab (mul_one b))⟩
/-- An element admitting a left inverse is left-regular. -/
lemma is_left_regular_of_mul_eq_one (h : b * a = 1) : is_left_regular a :=
@is_left_regular.of_mul R _ a _ (by { rw h, exact is_regular_one.left })
/-- An element admitting a right inverse is right-regular. -/
lemma is_right_regular_of_mul_eq_one (h : a * b = 1) : is_right_regular a :=
@is_right_regular.of_mul R _ a _ (by { rw h, exact is_regular_one.right })
/-- If `R` is a monoid, an element in `units R` is regular. -/
lemma units.is_regular (a : units R) : is_regular (a : R) :=
⟨is_left_regular_of_mul_eq_one a.inv_mul, is_right_regular_of_mul_eq_one a.mul_inv⟩
/-- A unit in a monoid is regular. -/
lemma is_unit.is_regular (ua : is_unit a) : is_regular a :=
begin
rcases ua with ⟨a, rfl⟩,
exact units.is_regular a,
end
end monoid
section left_or_right_cancel_semigroup
/-- Elements of a left cancel semigroup are left regular. -/
lemma is_left_regular_of_left_cancel_semigroup [left_cancel_semigroup R] (g : R) :
is_left_regular g :=
mul_right_injective g
/-- Elements of a right cancel semigroup are right regular. -/
lemma is_right_regular_of_right_cancel_semigroup [right_cancel_semigroup R] (g : R) :
is_right_regular g :=
mul_left_injective g
end left_or_right_cancel_semigroup
section cancel_monoid
variables [cancel_monoid R]
/-- Elements of a cancel monoid are regular. Cancel semigroups do not appear to exist. -/
lemma is_regular_of_cancel_monoid (g : R) : is_regular g :=
⟨mul_right_injective g, mul_left_injective g⟩
end cancel_monoid
section cancel_monoid_with_zero
variables [cancel_monoid_with_zero R]
/-- Non-zero elements of an integral domain are regular. -/
lemma is_regular_of_ne_zero (a0 : a ≠ 0) : is_regular a :=
⟨λ b c, (mul_right_inj' a0).mp, λ b c, (mul_left_inj' a0).mp⟩
/-- In a non-trivial integral domain, an element is regular iff it is non-zero. -/
lemma is_regular_iff_ne_zero [nontrivial R] : is_regular a ↔ a ≠ 0 :=
⟨is_regular.ne_zero, is_regular_of_ne_zero⟩
end cancel_monoid_with_zero
|
feb2ad9d2bc82b29c86a45df10eaed1818dedf06 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/algebra/category/Module/filtered_colimits.lean | 147ff80344193ad8d4a0c1928779a5574507b32c | [
"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 | 7,215 | lean | /-
Copyright (c) 2021 Justus Springer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Justus Springer
-/
import algebra.category.Module.basic
import algebra.category.CommRing.filtered_colimits
/-!
# The forgetful functor from `R`-modules preserves filtered colimits.
Forgetful functors from algebraic categories usually don't preserve colimits. However, they tend
to preserve _filtered_ colimits.
In this file, we start with a ring `R`, a small filtered category `J` and a functor
`F : J ⥤ Module R`. We show that the colimit of `F ⋙ forget₂ (Module R) AddCommGroup`
(in `AddCommGroup`) carries the structure of an `R`-module, thereby showing that the forgetful
functor `forget₂ (Module R) AddCommGroup` preserves filtered colimits. In particular, this implies
that `forget (Module R)` preserves filtered colimits.
-/
universes u v
noncomputable theory
open_locale classical
open category_theory
open category_theory.limits
open category_theory.is_filtered (renaming max → max') -- avoid name collision with `_root_.max`.
open AddMon.filtered_colimits (colimit_zero_eq colimit_add_mk_eq)
namespace Module.filtered_colimits
section
-- We use parameters here, mainly so we can have the abbreviations `M` and `M.mk` below, without
-- passing around `F` all the time.
parameters {R : Type u} [ring R] {J : Type v} [small_category J] [is_filtered J]
parameters (F : J ⥤ Module.{v} R)
/--
The colimit of `F ⋙ forget₂ (Module R) AddCommGroup` in the category `AddCommGroup`.
In the following, we will show that this has the structure of an `R`-module.
-/
abbreviation M : AddCommGroup :=
AddCommGroup.filtered_colimits.colimit (F ⋙ forget₂ (Module R) AddCommGroup)
/-- The canonical projection into the colimit, as a quotient type. -/
abbreviation M.mk : (Σ j, F.obj j) → M := quot.mk (types.quot.rel (F ⋙ forget (Module R)))
lemma M.mk_eq (x y : Σ j, F.obj j)
(h : ∃ (k : J) (f : x.1 ⟶ k) (g : y.1 ⟶ k), F.map f x.2 = F.map g y.2) :
M.mk x = M.mk y :=
quot.eqv_gen_sound (types.filtered_colimit.eqv_gen_quot_rel_of_rel (F ⋙ forget (Module R)) x y h)
/-- The "unlifted" version of scalar multiplication in the colimit. -/
def colimit_smul_aux (r : R) (x : Σ j, F.obj j) : M :=
M.mk ⟨x.1, r • x.2⟩
lemma colimit_smul_aux_eq_of_rel (r : R) (x y : Σ j, F.obj j)
(h : types.filtered_colimit.rel (F ⋙ forget (Module R)) x y) :
colimit_smul_aux r x = colimit_smul_aux r y :=
begin
apply M.mk_eq,
obtain ⟨k, f, g, hfg⟩ := h,
use [k, f, g],
simp only [category_theory.functor.comp_map, forget_map_eq_coe] at hfg,
rw [linear_map.map_smul, linear_map.map_smul, hfg],
end
/-- Scalar multiplication in the colimit. See also `colimit_smul_aux`. -/
instance colimit_has_scalar : has_scalar R M :=
{ smul := λ r x, begin
refine quot.lift (colimit_smul_aux F r) _ x,
intros x y h,
apply colimit_smul_aux_eq_of_rel,
apply types.filtered_colimit.rel_of_quot_rel,
exact h,
end }
@[simp]
lemma colimit_smul_mk_eq (r : R) (x : Σ j, F.obj j) : r • M.mk x = M.mk ⟨x.1, r • x.2⟩ := rfl
instance colimit_module : module R M :=
{ one_smul := λ x, begin
apply quot.induction_on x, clear x, intro x, cases x with j x,
erw [colimit_smul_mk_eq F 1 ⟨j, x⟩, one_smul],
refl,
end,
mul_smul := λ r s x, begin
apply quot.induction_on x, clear x, intro x, cases x with j x,
erw [colimit_smul_mk_eq F (r * s) ⟨j, x⟩, colimit_smul_mk_eq F s ⟨j, x⟩,
colimit_smul_mk_eq F r ⟨j, _⟩, mul_smul],
end,
smul_add := λ r x y, begin
apply quot.induction_on₂ x y, clear x y, intros x y, cases x with i x, cases y with j y,
erw [colimit_add_mk_eq _ ⟨i, x⟩ ⟨j, y⟩ (max' i j) (left_to_max i j) (right_to_max i j),
colimit_smul_mk_eq, smul_add, colimit_smul_mk_eq, colimit_smul_mk_eq,
colimit_add_mk_eq _ ⟨i, _⟩ ⟨j, _⟩ (max' i j) (left_to_max i j) (right_to_max i j),
linear_map.map_smul, linear_map.map_smul],
refl,
end,
smul_zero := λ r, begin
erw [colimit_zero_eq _ (is_filtered.nonempty.some : J), colimit_smul_mk_eq, smul_zero],
refl,
end,
zero_smul := λ x, begin
apply quot.induction_on x, clear x, intro x, cases x with j x,
erw [colimit_smul_mk_eq, zero_smul, colimit_zero_eq _ j],
refl,
end,
add_smul := λ r s x, begin
apply quot.induction_on x, clear x, intro x, cases x with j x,
erw [colimit_smul_mk_eq, add_smul, colimit_smul_mk_eq, colimit_smul_mk_eq,
colimit_add_mk_eq _ ⟨j, _⟩ ⟨j, _⟩ j (𝟙 j) (𝟙 j), category_theory.functor.map_id,
id_apply, id_apply],
refl,
end }
/-- The bundled `R`-module giving the filtered colimit of a diagram. -/
def colimit : Module R := Module.of R M
/-- The linear map from a given `R`-module in the diagram to the colimit module. -/
def cocone_morphism (j : J) : F.obj j ⟶ colimit :=
{ map_smul' := λ r x, begin erw colimit_smul_mk_eq F r ⟨j, x⟩, refl, end,
.. (AddCommGroup.filtered_colimits.colimit_cocone (F ⋙ forget₂ (Module R) AddCommGroup)).ι.app j }
/-- The cocone over the proposed colimit module. -/
def colimit_cocone : cocone F :=
{ X := colimit,
ι :=
{ app := cocone_morphism,
naturality' := λ j j' f,
linear_map.coe_injective ((types.colimit_cocone (F ⋙ forget (Module R))).ι.naturality f) } }
/--
Given a cocone `t` of `F`, the induced monoid linear map from the colimit to the cocone point.
We already know that this is a morphism between additive groups. The only thing left to see is that
it is a linear map, i.e. preserves scalar multiplication.
-/
def colimit_desc (t : cocone F) : colimit ⟶ t.X :=
{ map_smul' := λ r x, begin
apply quot.induction_on x, clear x, intro x, cases x with j x,
erw colimit_smul_mk_eq,
exact linear_map.map_smul (t.ι.app j) r x,
end,
.. (AddCommGroup.filtered_colimits.colimit_cocone_is_colimit
(F ⋙ forget₂ (Module R) AddCommGroup)).desc
((forget₂ (Module R) AddCommGroup.{v}).map_cocone t) }
/-- The proposed colimit cocone is a colimit in `Module R`. -/
def colimit_cocone_is_colimit : is_colimit colimit_cocone :=
{ desc := colimit_desc,
fac' := λ t j, linear_map.coe_injective $
(types.colimit_cocone_is_colimit (F ⋙ forget (Module R))).fac
((forget (Module R)).map_cocone t) j,
uniq' := λ t m h, linear_map.coe_injective $
(types.colimit_cocone_is_colimit (F ⋙ forget (Module R))).uniq
((forget (Module R)).map_cocone t) m ((λ j, funext $ λ x, linear_map.congr_fun (h j) x)) }
instance forget₂_AddCommGroup_preserves_filtered_colimits :
preserves_filtered_colimits (forget₂ (Module R) AddCommGroup.{v}) :=
{ preserves_filtered_colimits := λ J _ _, by exactI
{ preserves_colimit := λ F, preserves_colimit_of_preserves_colimit_cocone
(colimit_cocone_is_colimit F)
(AddCommGroup.filtered_colimits.colimit_cocone_is_colimit
(F ⋙ forget₂ (Module R) AddCommGroup.{v})) } }
instance forget_preserves_filtered_colimits : preserves_filtered_colimits (forget (Module R)) :=
limits.comp_preserves_filtered_colimits (forget₂ (Module R) AddCommGroup) (forget AddCommGroup)
end
end Module.filtered_colimits
|
1d90bf90bd1a19fd901d1d341b6dbdd75cfa45da | a19a4fce1e5677f4d20cbfdf60c04b6386ab8210 | /hott/cubical/square.hlean | 3777f6d2cf4d231626e6dd20d8be1b5f3320a3b4 | [
"Apache-2.0"
] | permissive | nthomas103/lean | 9c341a316e7d9faa00546462f90a8aa402e17eac | 04eaf184a92606a56e54d0d6c8d59437557263fc | refs/heads/master | 1,586,061,106,806 | 1,454,640,115,000 | 1,454,641,279,000 | 51,127,143 | 0 | 0 | null | 1,454,648,683,000 | 1,454,648,683,000 | null | UTF-8 | Lean | false | false | 27,416 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Jakob von Raumer
Squares in a type
-/
import types.eq
open eq equiv is_equiv sigma
namespace eq
variables {A B : Type} {a a' a'' a₀₀ a₂₀ a₄₀ a₀₂ a₂₂ a₂₄ a₀₄ a₄₂ a₄₄ a₁ a₂ a₃ a₄ : A}
/-a₀₀-/ {p₁₀ p₁₀' : a₀₀ = a₂₀} /-a₂₀-/ {p₃₀ : a₂₀ = a₄₀} /-a₄₀-/
{p₀₁ p₀₁' : a₀₀ = a₀₂} /-s₁₁-/ {p₂₁ p₂₁' : a₂₀ = a₂₂} /-s₃₁-/ {p₄₁ : a₄₀ = a₄₂}
/-a₀₂-/ {p₁₂ p₁₂' : a₀₂ = a₂₂} /-a₂₂-/ {p₃₂ : a₂₂ = a₄₂} /-a₄₂-/
{p₀₃ : a₀₂ = a₀₄} /-s₁₃-/ {p₂₃ : a₂₂ = a₂₄} /-s₃₃-/ {p₄₃ : a₄₂ = a₄₄}
/-a₀₄-/ {p₁₄ : a₀₄ = a₂₄} /-a₂₄-/ {p₃₄ : a₂₄ = a₄₄} /-a₄₄-/
inductive square {A : Type} {a₀₀ : A}
: Π{a₂₀ a₀₂ a₂₂ : A}, a₀₀ = a₂₀ → a₀₂ = a₂₂ → a₀₀ = a₀₂ → a₂₀ = a₂₂ → Type :=
ids : square idp idp idp idp
/- square top bottom left right -/
variables {s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁} {s₃₁ : square p₃₀ p₃₂ p₂₁ p₄₁}
{s₁₃ : square p₁₂ p₁₄ p₀₃ p₂₃} {s₃₃ : square p₃₂ p₃₄ p₂₃ p₄₃}
definition ids [reducible] [constructor] := @square.ids
definition idsquare [reducible] [constructor] (a : A) := @square.ids A a
definition hrefl [unfold 4] (p : a = a') : square idp idp p p :=
by induction p; exact ids
definition vrefl [unfold 4] (p : a = a') : square p p idp idp :=
by induction p; exact ids
definition hrfl [reducible] [unfold 4] {p : a = a'} : square idp idp p p :=
!hrefl
definition vrfl [reducible] [unfold 4] {p : a = a'} : square p p idp idp :=
!vrefl
definition hdeg_square [unfold 6] {p q : a = a'} (r : p = q) : square idp idp p q :=
by induction r;apply hrefl
definition vdeg_square [unfold 6] {p q : a = a'} (r : p = q) : square p q idp idp :=
by induction r;apply vrefl
definition hdeg_square_idp (p : a = a') : hdeg_square (refl p) = hrfl :=
by cases p; reflexivity
definition vdeg_square_idp (p : a = a') : vdeg_square (refl p) = vrfl :=
by cases p; reflexivity
definition hconcat [unfold 16] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (s₃₁ : square p₃₀ p₃₂ p₂₁ p₄₁)
: square (p₁₀ ⬝ p₃₀) (p₁₂ ⬝ p₃₂) p₀₁ p₄₁ :=
by induction s₃₁; exact s₁₁
definition vconcat [unfold 16] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (s₁₃ : square p₁₂ p₁₄ p₀₃ p₂₃)
: square p₁₀ p₁₄ (p₀₁ ⬝ p₀₃) (p₂₁ ⬝ p₂₃) :=
by induction s₁₃; exact s₁₁
definition hinverse [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀⁻¹ p₁₂⁻¹ p₂₁ p₀₁ :=
by induction s₁₁;exact ids
definition vinverse [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₂ p₁₀ p₀₁⁻¹ p₂₁⁻¹ :=
by induction s₁₁;exact ids
definition eq_vconcat [unfold 11] {p : a₀₀ = a₂₀} (r : p = p₁₀) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) :
square p p₁₂ p₀₁ p₂₁ :=
by induction r; exact s₁₁
definition vconcat_eq [unfold 12] {p : a₀₂ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₁₂ = p) :
square p₁₀ p p₀₁ p₂₁ :=
by induction r; exact s₁₁
definition eq_hconcat [unfold 11] {p : a₀₀ = a₀₂} (r : p = p₀₁) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) :
square p₁₀ p₁₂ p p₂₁ :=
by induction r; exact s₁₁
definition hconcat_eq [unfold 12] {p : a₂₀ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₂₁ = p) :
square p₁₀ p₁₂ p₀₁ p :=
by induction r; exact s₁₁
infix ` ⬝h `:75 := hconcat --type using \tr
infix ` ⬝v `:75 := vconcat --type using \tr
infix ` ⬝hp `:75 := hconcat_eq --type using \tr
infix ` ⬝vp `:75 := vconcat_eq --type using \tr
infix ` ⬝ph `:75 := eq_hconcat --type using \tr
infix ` ⬝pv `:75 := eq_vconcat --type using \tr
postfix `⁻¹ʰ`:(max+1) := hinverse --type using \-1h
postfix `⁻¹ᵛ`:(max+1) := vinverse --type using \-1v
definition transpose [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₀₁ p₂₁ p₁₀ p₁₂ :=
by induction s₁₁;exact ids
definition aps [unfold 12] {B : Type} (f : A → B) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: square (ap f p₁₀) (ap f p₁₂) (ap f p₀₁) (ap f p₂₁) :=
by induction s₁₁;exact ids
definition natural_square [unfold 8] {f g : A → B} (p : f ~ g) (q : a = a') :
square (ap f q) (ap g q) (p a) (p a') :=
eq.rec_on q hrfl
definition natural_square_tr [unfold 8] {f g : A → B} (p : f ~ g) (q : a = a') :
square (p a) (p a') (ap f q) (ap g q) :=
eq.rec_on q vrfl
/- canceling, whiskering and moving thinks along the sides of the square -/
definition whisker_tl (p : a = a₀₀) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: square (p ⬝ p₁₀) p₁₂ (p ⬝ p₀₁) p₂₁ :=
by induction s₁₁;induction p;constructor
definition whisker_br (p : a₂₂ = a) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: square p₁₀ (p₁₂ ⬝ p) p₀₁ (p₂₁ ⬝ p) :=
by induction p;exact s₁₁
definition whisker_rt (p : a = a₂₀) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: square (p₁₀ ⬝ p⁻¹) p₁₂ p₀₁ (p ⬝ p₂₁) :=
by induction s₁₁;induction p;constructor
definition whisker_tr (p : a₂₀ = a) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: square (p₁₀ ⬝ p) p₁₂ p₀₁ (p⁻¹ ⬝ p₂₁) :=
by induction s₁₁;induction p;constructor
definition whisker_bl (p : a = a₀₂) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: square p₁₀ (p ⬝ p₁₂) (p₀₁ ⬝ p⁻¹) p₂₁ :=
by induction s₁₁;induction p;constructor
definition whisker_lb (p : a₀₂ = a) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: square p₁₀ (p⁻¹ ⬝ p₁₂) (p₀₁ ⬝ p) p₂₁ :=
by induction s₁₁;induction p;constructor
definition cancel_tl (p : a = a₀₀) (s₁₁ : square (p ⬝ p₁₀) p₁₂ (p ⬝ p₀₁) p₂₁)
: square p₁₀ p₁₂ p₀₁ p₂₁ :=
by induction p; rewrite +idp_con at s₁₁; exact s₁₁
definition cancel_br (p : a₂₂ = a) (s₁₁ : square p₁₀ (p₁₂ ⬝ p) p₀₁ (p₂₁ ⬝ p))
: square p₁₀ p₁₂ p₀₁ p₂₁ :=
by induction p;exact s₁₁
definition cancel_rt (p : a = a₂₀) (s₁₁ : square (p₁₀ ⬝ p⁻¹) p₁₂ p₀₁ (p ⬝ p₂₁))
: square p₁₀ p₁₂ p₀₁ p₂₁ :=
by induction p; rewrite idp_con at s₁₁; exact s₁₁
definition cancel_tr (p : a₂₀ = a) (s₁₁ : square (p₁₀ ⬝ p) p₁₂ p₀₁ (p⁻¹ ⬝ p₂₁))
: square p₁₀ p₁₂ p₀₁ p₂₁ :=
by induction p; rewrite [▸* at s₁₁,idp_con at s₁₁]; exact s₁₁
definition cancel_bl (p : a = a₀₂) (s₁₁ : square p₁₀ (p ⬝ p₁₂) (p₀₁ ⬝ p⁻¹) p₂₁)
: square p₁₀ p₁₂ p₀₁ p₂₁ :=
by induction p; rewrite idp_con at s₁₁; exact s₁₁
definition cancel_lb (p : a₀₂ = a) (s₁₁ : square p₁₀ (p⁻¹ ⬝ p₁₂) (p₀₁ ⬝ p) p₂₁)
: square p₁₀ p₁₂ p₀₁ p₂₁ :=
by induction p; rewrite [▸* at s₁₁,idp_con at s₁₁]; exact s₁₁
definition move_top_of_left {p : a₀₀ = a} {q : a = a₀₂} (s : square p₁₀ p₁₂ (p ⬝ q) p₂₁)
: square (p⁻¹ ⬝ p₁₀) p₁₂ q p₂₁ :=
by apply cancel_tl p; rewrite con_inv_cancel_left; exact s
definition move_top_of_left' {p : a = a₀₀} {q : a = a₀₂} (s : square p₁₀ p₁₂ (p⁻¹ ⬝ q) p₂₁)
: square (p ⬝ p₁₀) p₁₂ q p₂₁ :=
by apply cancel_tl p⁻¹; rewrite inv_con_cancel_left; exact s
definition move_left_of_top {p : a₀₀ = a} {q : a = a₂₀} (s : square (p ⬝ q) p₁₂ p₀₁ p₂₁)
: square q p₁₂ (p⁻¹ ⬝ p₀₁) p₂₁ :=
by apply cancel_tl p; rewrite con_inv_cancel_left; exact s
definition move_left_of_top' {p : a = a₀₀} {q : a = a₂₀} (s : square (p⁻¹ ⬝ q) p₁₂ p₀₁ p₂₁)
: square q p₁₂ (p ⬝ p₀₁) p₂₁ :=
by apply cancel_tl p⁻¹; rewrite inv_con_cancel_left; exact s
definition move_bot_of_right {p : a₂₀ = a} {q : a = a₂₂} (s : square p₁₀ p₁₂ p₀₁ (p ⬝ q))
: square p₁₀ (p₁₂ ⬝ q⁻¹) p₀₁ p :=
by apply cancel_br q; rewrite inv_con_cancel_right; exact s
definition move_bot_of_right' {p : a₂₀ = a} {q : a₂₂ = a} (s : square p₁₀ p₁₂ p₀₁ (p ⬝ q⁻¹))
: square p₁₀ (p₁₂ ⬝ q) p₀₁ p :=
by apply cancel_br q⁻¹; rewrite con_inv_cancel_right; exact s
definition move_right_of_bot {p : a₀₂ = a} {q : a = a₂₂} (s : square p₁₀ (p ⬝ q) p₀₁ p₂₁)
: square p₁₀ p p₀₁ (p₂₁ ⬝ q⁻¹) :=
by apply cancel_br q; rewrite inv_con_cancel_right; exact s
definition move_right_of_bot' {p : a₀₂ = a} {q : a₂₂ = a} (s : square p₁₀ (p ⬝ q⁻¹) p₀₁ p₂₁)
: square p₁₀ p p₀₁ (p₂₁ ⬝ q) :=
by apply cancel_br q⁻¹; rewrite con_inv_cancel_right; exact s
definition move_top_of_right {p : a₂₀ = a} {q : a = a₂₂} (s : square p₁₀ p₁₂ p₀₁ (p ⬝ q))
: square (p₁₀ ⬝ p) p₁₂ p₀₁ q :=
by apply cancel_rt p; rewrite con_inv_cancel_right; exact s
definition move_right_of_top {p : a₀₀ = a} {q : a = a₂₀} (s : square (p ⬝ q) p₁₂ p₀₁ p₂₁)
: square p p₁₂ p₀₁ (q ⬝ p₂₁) :=
by apply cancel_tr q; rewrite inv_con_cancel_left; exact s
definition move_bot_of_left {p : a₀₀ = a} {q : a = a₀₂} (s : square p₁₀ p₁₂ (p ⬝ q) p₂₁)
: square p₁₀ (q ⬝ p₁₂) p p₂₁ :=
by apply cancel_lb q; rewrite inv_con_cancel_left; exact s
definition move_left_of_bot {p : a₀₂ = a} {q : a = a₂₂} (s : square p₁₀ (p ⬝ q) p₀₁ p₂₁)
: square p₁₀ q (p₀₁ ⬝ p) p₂₁ :=
by apply cancel_bl p; rewrite con_inv_cancel_right; exact s
/- some higher ∞-groupoid operations -/
definition vconcat_vrfl (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: s₁₁ ⬝v vrefl p₁₂ = s₁₁ :=
by induction s₁₁; reflexivity
definition hconcat_hrfl (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: s₁₁ ⬝h hrefl p₂₁ = s₁₁ :=
by induction s₁₁; reflexivity
/- equivalences -/
definition eq_of_square [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : p₁₀ ⬝ p₂₁ = p₀₁ ⬝ p₁₂ :=
by induction s₁₁; apply idp
definition square_of_eq (r : p₁₀ ⬝ p₂₁ = p₀₁ ⬝ p₁₂) : square p₁₀ p₁₂ p₀₁ p₂₁ :=
by induction p₁₂; esimp at r; induction r; induction p₂₁; induction p₁₀; exact ids
definition eq_top_of_square [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: p₁₀ = p₀₁ ⬝ p₁₂ ⬝ p₂₁⁻¹ :=
by induction s₁₁; apply idp
definition square_of_eq_top (r : p₁₀ = p₀₁ ⬝ p₁₂ ⬝ p₂₁⁻¹) : square p₁₀ p₁₂ p₀₁ p₂₁ :=
by induction p₂₁; induction p₁₂; esimp at r;induction r;induction p₁₀;exact ids
definition eq_bot_of_square [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: p₁₂ = p₀₁⁻¹ ⬝ p₁₀ ⬝ p₂₁ :=
by induction s₁₁; apply idp
definition square_equiv_eq [constructor] (t : a₀₀ = a₀₂) (b : a₂₀ = a₂₂)
(l : a₀₀ = a₂₀) (r : a₀₂ = a₂₂) : square t b l r ≃ t ⬝ r = l ⬝ b :=
begin
fapply equiv.MK,
{ exact eq_of_square},
{ exact square_of_eq},
{ intro s, induction b, esimp [concat] at s, induction s, induction r, induction t, apply idp},
{ intro s, induction s, apply idp},
end
definition hdeg_square_equiv' [constructor] (p q : a = a') : square idp idp p q ≃ p = q :=
by transitivity _;apply square_equiv_eq;transitivity _;apply eq_equiv_eq_symm;
apply equiv_eq_closed_right;apply idp_con
definition vdeg_square_equiv' [constructor] (p q : a = a') : square p q idp idp ≃ p = q :=
by transitivity _;apply square_equiv_eq;apply equiv_eq_closed_right; apply idp_con
definition eq_of_hdeg_square [reducible] {p q : a = a'} (s : square idp idp p q) : p = q :=
to_fun !hdeg_square_equiv' s
definition eq_of_vdeg_square [reducible] {p q : a = a'} (s : square p q idp idp) : p = q :=
to_fun !vdeg_square_equiv' s
definition top_deg_square (l : a₁ = a₂) (b : a₂ = a₃) (r : a₄ = a₃)
: square (l ⬝ b ⬝ r⁻¹) b l r :=
by induction r;induction b;induction l;constructor
definition bot_deg_square (l : a₁ = a₂) (t : a₁ = a₃) (r : a₃ = a₄)
: square t (l⁻¹ ⬝ t ⬝ r) l r :=
by induction r;induction t;induction l;constructor
/-
the following two equivalences have as underlying inverse function the functions
hdeg_square and vdeg_square, respectively.
See example below the definition
-/
definition hdeg_square_equiv [constructor] (p q : a = a') :
square idp idp p q ≃ p = q :=
begin
fapply equiv_change_fun,
{ fapply equiv_change_inv, apply hdeg_square_equiv', exact hdeg_square,
intro s, induction s, induction p, reflexivity},
{ exact eq_of_hdeg_square},
{ reflexivity}
end
definition vdeg_square_equiv [constructor] (p q : a = a') :
square p q idp idp ≃ p = q :=
begin
fapply equiv_change_fun,
{ fapply equiv_change_inv, apply vdeg_square_equiv',exact vdeg_square,
intro s, induction s, induction p, reflexivity},
{ exact eq_of_vdeg_square},
{ reflexivity}
end
example (p q : a = a') : to_inv (hdeg_square_equiv p q) = hdeg_square := idp
/-
characterization of pathovers in a equality type. The type B of the equality is fixed here.
A version where B may also varies over the path p is given in the file squareover
-/
definition eq_pathover [unfold 7] {f g : A → B} {p : a = a'} {q : f a = g a} {r : f a' = g a'}
(s : square q r (ap f p) (ap g p)) : q =[p] r :=
by induction p;apply pathover_idp_of_eq;exact eq_of_vdeg_square s
definition square_of_pathover [unfold 7]
{f g : A → B} {p : a = a'} {q : f a = g a} {r : f a' = g a'}
(s : q =[p] r) : square q r (ap f p) (ap g p) :=
by induction p;apply vdeg_square;exact eq_of_pathover_idp s
/- interaction of equivalences with operations on squares -/
definition eq_pathover_equiv_square [constructor] {f g : A → B}
(p : a = a') (q : f a = g a) (r : f a' = g a') : q =[p] r ≃ square q r (ap f p) (ap g p) :=
equiv.MK square_of_pathover
eq_pathover
begin
intro s, induction p, esimp [square_of_pathover,eq_pathover],
exact ap vdeg_square (to_right_inv !pathover_idp (eq_of_vdeg_square s))
⬝ to_left_inv !vdeg_square_equiv s
end
begin
intro s, induction p, esimp [square_of_pathover,eq_pathover],
exact ap pathover_idp_of_eq (to_right_inv !vdeg_square_equiv (eq_of_pathover_idp s))
⬝ to_left_inv !pathover_idp s
end
definition square_of_pathover_eq_concato {f g : A → B} {p : a = a'} {q q' : f a = g a}
{r : f a' = g a'} (s' : q = q') (s : q' =[p] r)
: square_of_pathover (s' ⬝po s) = s' ⬝pv square_of_pathover s :=
by induction s;induction s';reflexivity
definition square_of_pathover_concato_eq {f g : A → B} {p : a = a'} {q : f a = g a}
{r r' : f a' = g a'} (s' : r = r') (s : q =[p] r)
: square_of_pathover (s ⬝op s') = square_of_pathover s ⬝vp s' :=
by induction s;induction s';reflexivity
definition square_of_pathover_concato {f g : A → B} {p : a = a'} {p' : a' = a''} {q : f a = g a}
{q' : f a' = g a'} {q'' : f a'' = g a''} (s : q =[p] q') (s' : q' =[p'] q'')
: square_of_pathover (s ⬝o s')
= ap_con f p p' ⬝ph (square_of_pathover s ⬝v square_of_pathover s') ⬝hp (ap_con g p p')⁻¹ :=
by induction s';induction s;esimp [ap_con,hconcat_eq];exact !vconcat_vrfl⁻¹
definition eq_of_square_hrfl [unfold 4] (p : a = a') : eq_of_square hrfl = idp_con p :=
by induction p;reflexivity
definition eq_of_square_vrfl [unfold 4] (p : a = a') : eq_of_square vrfl = (idp_con p)⁻¹ :=
by induction p;reflexivity
definition eq_of_square_hdeg_square {p q : a = a'} (r : p = q)
: eq_of_square (hdeg_square r) = !idp_con ⬝ r⁻¹ :=
by induction r;induction p;reflexivity
definition eq_of_square_vdeg_square {p q : a = a'} (r : p = q)
: eq_of_square (vdeg_square r) = r ⬝ !idp_con⁻¹ :=
by induction r;induction p;reflexivity
definition eq_of_square_eq_vconcat {p : a₀₀ = a₂₀} (r : p = p₁₀) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: eq_of_square (r ⬝pv s₁₁) = whisker_right r p₂₁ ⬝ eq_of_square s₁₁ :=
by induction s₁₁;cases r;reflexivity
definition eq_of_square_eq_hconcat {p : a₀₀ = a₀₂} (r : p = p₀₁) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁)
: eq_of_square (r ⬝ph s₁₁) = eq_of_square s₁₁ ⬝ (whisker_right r p₁₂)⁻¹ :=
by induction r;reflexivity
definition eq_of_square_vconcat_eq {p : a₀₂ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₁₂ = p)
: eq_of_square (s₁₁ ⬝vp r) = eq_of_square s₁₁ ⬝ whisker_left p₀₁ r :=
by induction r;reflexivity
definition eq_of_square_hconcat_eq {p : a₂₀ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₂₁ = p)
: eq_of_square (s₁₁ ⬝hp r) = (whisker_left p₁₀ r)⁻¹ ⬝ eq_of_square s₁₁ :=
by induction s₁₁; induction r;reflexivity
-- definition vconcat_eq [unfold 11] {p : a₀₂ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₁₂ = p) :
-- square p₁₀ p p₀₁ p₂₁ :=
-- by induction r; exact s₁₁
-- definition eq_hconcat [unfold 11] {p : a₀₀ = a₀₂} (r : p = p₀₁)
-- (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀ p₁₂ p p₂₁ :=
-- by induction r; exact s₁₁
-- definition hconcat_eq [unfold 11] {p : a₂₀ = a₂₂}
-- (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₂₁ = p) : square p₁₀ p₁₂ p₀₁ p :=
-- by induction r; exact s₁₁
-- the following definition is very slow, maybe it's interesting to see why?
-- definition eq_pathover_equiv_square' {f g : A → B}(p : a = a') (q : f a = g a) (r : f a' = g a')
-- : square q r (ap f p) (ap g p) ≃ q =[p] r :=
-- equiv.MK eq_pathover
-- square_of_pathover
-- (λs, begin
-- induction p, rewrite [↑[square_of_pathover,eq_pathover],
-- to_right_inv !vdeg_square_equiv (eq_of_pathover_idp s),
-- to_left_inv !pathover_idp s]
-- end)
-- (λs, begin
-- induction p, rewrite [↑[square_of_pathover,eq_pathover],▸*,
-- to_right_inv !(@pathover_idp A) (eq_of_vdeg_square s),
-- to_left_inv !vdeg_square_equiv s]
-- end)
/- recursors for squares where some sides are reflexivity -/
definition rec_on_b [recursor] {a₀₀ : A}
{P : Π{a₂₀ a₁₂ : A} {t : a₀₀ = a₂₀} {l : a₀₀ = a₁₂} {r : a₂₀ = a₁₂}, square t idp l r → Type}
{a₂₀ a₁₂ : A} {t : a₀₀ = a₂₀} {l : a₀₀ = a₁₂} {r : a₂₀ = a₁₂}
(s : square t idp l r) (H : P ids) : P s :=
have H2 : P (square_of_eq (eq_of_square s)),
from eq.rec_on (eq_of_square s : t ⬝ r = l) (by induction r; induction t; exact H),
left_inv (to_fun !square_equiv_eq) s ▸ H2
definition rec_on_r [recursor] {a₀₀ : A}
{P : Π{a₀₂ a₂₁ : A} {t : a₀₀ = a₂₁} {b : a₀₂ = a₂₁} {l : a₀₀ = a₀₂}, square t b l idp → Type}
{a₀₂ a₂₁ : A} {t : a₀₀ = a₂₁} {b : a₀₂ = a₂₁} {l : a₀₀ = a₀₂}
(s : square t b l idp) (H : P ids) : P s :=
let p : l ⬝ b = t := (eq_of_square s)⁻¹ in
have H2 : P (square_of_eq (eq_of_square s)⁻¹⁻¹),
from @eq.rec_on _ _ (λx p, P (square_of_eq p⁻¹)) _ p (by induction b; induction l; exact H),
left_inv (to_fun !square_equiv_eq) s ▸ !inv_inv ▸ H2
definition rec_on_l [recursor] {a₀₁ : A}
{P : Π {a₂₀ a₂₂ : A} {t : a₀₁ = a₂₀} {b : a₀₁ = a₂₂} {r : a₂₀ = a₂₂},
square t b idp r → Type}
{a₂₀ a₂₂ : A} {t : a₀₁ = a₂₀} {b : a₀₁ = a₂₂} {r : a₂₀ = a₂₂}
(s : square t b idp r) (H : P ids) : P s :=
let p : t ⬝ r = b := eq_of_square s ⬝ !idp_con in
have H2 : P (square_of_eq (p ⬝ !idp_con⁻¹)),
from eq.rec_on p (by induction r; induction t; exact H),
left_inv (to_fun !square_equiv_eq) s ▸ !con_inv_cancel_right ▸ H2
definition rec_on_t [recursor] {a₁₀ : A}
{P : Π {a₀₂ a₂₂ : A} {b : a₀₂ = a₂₂} {l : a₁₀ = a₀₂} {r : a₁₀ = a₂₂}, square idp b l r → Type}
{a₀₂ a₂₂ : A} {b : a₀₂ = a₂₂} {l : a₁₀ = a₀₂} {r : a₁₀ = a₂₂}
(s : square idp b l r) (H : P ids) : P s :=
let p : l ⬝ b = r := (eq_of_square s)⁻¹ ⬝ !idp_con in
assert H2 : P (square_of_eq ((p ⬝ !idp_con⁻¹)⁻¹)),
from eq.rec_on p (by induction b; induction l; exact H),
assert H3 : P (square_of_eq ((eq_of_square s)⁻¹⁻¹)),
from eq.rec_on !con_inv_cancel_right H2,
assert H4 : P (square_of_eq (eq_of_square s)),
from eq.rec_on !inv_inv H3,
proof
left_inv (to_fun !square_equiv_eq) s ▸ H4
qed
definition rec_on_tb [recursor] {a : A}
{P : Π{b : A} {l : a = b} {r : a = b}, square idp idp l r → Type}
{b : A} {l : a = b} {r : a = b}
(s : square idp idp l r) (H : P ids) : P s :=
have H2 : P (square_of_eq (eq_of_square s)),
from eq.rec_on (eq_of_square s : idp ⬝ r = l) (by induction r; exact H),
left_inv (to_fun !square_equiv_eq) s ▸ H2
definition rec_on_lr [recursor] {a : A}
{P : Π{a' : A} {t : a = a'} {b : a = a'}, square t b idp idp → Type}
{a' : A} {t : a = a'} {b : a = a'}
(s : square t b idp idp) (H : P ids) : P s :=
let p : idp ⬝ b = t := (eq_of_square s)⁻¹ in
assert H2 : P (square_of_eq (eq_of_square s)⁻¹⁻¹),
from @eq.rec_on _ _ (λx q, P (square_of_eq q⁻¹)) _ p (by induction b; exact H),
to_left_inv (!square_equiv_eq) s ▸ !inv_inv ▸ H2
--we can also do the other recursors (tl, tr, bl, br, tbl, tbr, tlr, blr), but let's postpone this until they are needed
definition whisker_square [unfold 14 15 16 17] (r₁₀ : p₁₀ = p₁₀') (r₁₂ : p₁₂ = p₁₂')
(r₀₁ : p₀₁ = p₀₁') (r₂₁ : p₂₁ = p₂₁') (s : square p₁₀ p₁₂ p₀₁ p₂₁)
: square p₁₀' p₁₂' p₀₁' p₂₁' :=
by induction r₁₀; induction r₁₂; induction r₀₁; induction r₂₁; exact s
/- squares commute with some operations on 2-paths -/
definition square_inv2 {p₁ p₂ p₃ p₄ : a = a'}
{t : p₁ = p₂} {b : p₃ = p₄} {l : p₁ = p₃} {r : p₂ = p₄} (s : square t b l r)
: square (inverse2 t) (inverse2 b) (inverse2 l) (inverse2 r) :=
by induction s;constructor
definition square_con2 {p₁ p₂ p₃ p₄ : a₁ = a₂} {q₁ q₂ q₃ q₄ : a₂ = a₃}
{t₁ : p₁ = p₂} {b₁ : p₃ = p₄} {l₁ : p₁ = p₃} {r₁ : p₂ = p₄}
{t₂ : q₁ = q₂} {b₂ : q₃ = q₄} {l₂ : q₁ = q₃} {r₂ : q₂ = q₄}
(s₁ : square t₁ b₁ l₁ r₁) (s₂ : square t₂ b₂ l₂ r₂)
: square (t₁ ◾ t₂) (b₁ ◾ b₂) (l₁ ◾ l₂) (r₁ ◾ r₂) :=
by induction s₂;induction s₁;constructor
open is_trunc
definition is_hset.elims [H : is_hset A] : square p₁₀ p₁₂ p₀₁ p₂₁ :=
square_of_eq !is_hset.elim
-- definition square_of_con_inv_hsquare {p₁ p₂ p₃ p₄ : a₁ = a₂}
-- {t : p₁ = p₂} {b : p₃ = p₄} {l : p₁ = p₃} {r : p₂ = p₄}
-- (s : square (con_inv_eq_idp t) (con_inv_eq_idp b) (l ◾ r⁻²) idp)
-- : square t b l r :=
-- sorry --by induction s
/- Square fillers -/
-- TODO replace by "more algebraic" fillers?
variables (p₁₀ p₁₂ p₀₁ p₂₁)
definition square_fill_t : Σ (p : a₀₀ = a₂₀), square p p₁₂ p₀₁ p₂₁ :=
by induction p₀₁; induction p₂₁; exact ⟨_, !vrefl⟩
definition square_fill_b : Σ (p : a₀₂ = a₂₂), square p₁₀ p p₀₁ p₂₁ :=
by induction p₀₁; induction p₂₁; exact ⟨_, !vrefl⟩
definition square_fill_l : Σ (p : a₀₀ = a₀₂), square p₁₀ p₁₂ p p₂₁ :=
by induction p₁₀; induction p₁₂; exact ⟨_, !hrefl⟩
definition square_fill_r : Σ (p : a₂₀ = a₂₂) , square p₁₀ p₁₂ p₀₁ p :=
by induction p₁₀; induction p₁₂; exact ⟨_, !hrefl⟩
/- Squares having an 'ap' term on one face -/
--TODO find better names
definition square_Flr_ap_idp {A B : Type} {c : B} {f : A → B} (p : Π a, f a = c)
{a b : A} (q : a = b) : square (p a) (p b) (ap f q) idp :=
by induction q; apply vrfl
definition square_Flr_idp_ap {A B : Type} {c : B} {f : A → B} (p : Π a, c = f a)
{a b : A} (q : a = b) : square (p a) (p b) idp (ap f q) :=
by induction q; apply vrfl
definition square_ap_idp_Flr {A B : Type} {b : B} {f : A → B} (p : Π a, f a = b)
{a b : A} (q : a = b) : square (ap f q) idp (p a) (p b) :=
by induction q; apply hrfl
/- Matching eq_hconcat with hconcat etc. -/
-- TODO maybe rename hconcat_eq and the like?
variable (s₁₁)
definition ph_eq_pv_h_vp {p : a₀₀ = a₀₂} (r : p = p₀₁) :
r ⬝ph s₁₁ = !idp_con⁻¹ ⬝pv ((hdeg_square r) ⬝h s₁₁) ⬝vp !idp_con :=
by cases r; cases s₁₁; esimp
definition hdeg_h_eq_pv_ph_vp {p : a₀₀ = a₀₂} (r : p = p₀₁) :
hdeg_square r ⬝h s₁₁ = !idp_con ⬝pv (r ⬝ph s₁₁) ⬝vp !idp_con⁻¹ :=
by cases r; cases s₁₁; esimp
definition hp_eq_h {p : a₂₀ = a₂₂} (r : p₂₁ = p) :
s₁₁ ⬝hp r = s₁₁ ⬝h hdeg_square r :=
by cases r; cases s₁₁; esimp
definition pv_eq_ph_vdeg_v_vh {p : a₀₀ = a₂₀} (r : p = p₁₀) :
r ⬝pv s₁₁ = !idp_con⁻¹ ⬝ph ((vdeg_square r) ⬝v s₁₁) ⬝hp !idp_con :=
by cases r; cases s₁₁; esimp
definition vdeg_v_eq_ph_pv_hp {p : a₀₀ = a₂₀} (r : p = p₁₀) :
vdeg_square r ⬝v s₁₁ = !idp_con ⬝ph (r ⬝pv s₁₁) ⬝hp !idp_con⁻¹ :=
by cases r; cases s₁₁; esimp
definition vp_eq_v {p : a₀₂ = a₂₂} (r : p₁₂ = p) :
s₁₁ ⬝vp r = s₁₁ ⬝v vdeg_square r :=
by cases r; cases s₁₁; esimp
end eq
|
15d48fe0be22548ea93366b550cd79b748555ebb | 4727251e0cd73359b15b664c3170e5d754078599 | /src/analysis/normed/group/basic.lean | 05c587581b6df2728d72d5dff3ca02b2d3330d22 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 50,375 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Johannes Hölzl
-/
import order.liminf_limsup
import topology.algebra.uniform_group
import topology.metric_space.algebra
import topology.metric_space.isometry
import topology.sequences
/-!
# Normed (semi)groups
In this file we define four classes:
* `has_norm`, `has_nnnorm`: auxiliary classes endowing a type `α` with a function `norm : α → ℝ`
(notation: `∥x∥`) and `nnnorm : α → ℝ≥0` (notation: `∥x∥₊`), respectively;
* `semi_normed_group`: a seminormed group is an additive group with a norm and a pseudo metric space
structures that agree with each other: `∀ x y, dist x y = ∥x - y∥`;
* `normed_group`: a normed group is an additive group with a norm and a metric space structures that
agree with each other: `∀ x y, dist x y = ∥x - y∥`.
We also prove basic properties of (semi)normed groups and provide some instances.
## Tags
normed group
-/
variables {α ι E F G : Type*}
open filter metric
open_locale topological_space big_operators nnreal ennreal uniformity pointwise
/-- Auxiliary class, endowing a type `E` with a function `norm : E → ℝ` with notation `∥x∥`. This
class is designed to be extended in more interesting classes specifying the properties of the norm.
-/
class has_norm (E : Type*) := (norm : E → ℝ)
export has_norm (norm)
notation `∥` e `∥` := norm e
/-- A seminormed group is an additive group endowed with a norm for which `dist x y = ∥x - y∥`
defines a pseudometric space structure. -/
class semi_normed_group (E : Type*) extends has_norm E, add_comm_group E, pseudo_metric_space E :=
(dist_eq : ∀ x y : E, dist x y = norm (x - y))
/-- A normed group is an additive group endowed with a norm for which `dist x y = ∥x - y∥` defines
a metric space structure. -/
class normed_group (E : Type*) extends has_norm E, add_comm_group E, metric_space E :=
(dist_eq : ∀ x y : E, dist x y = norm (x - y))
/-- A normed group is a seminormed group. -/
@[priority 100] -- see Note [lower instance priority]
instance normed_group.to_semi_normed_group [h : normed_group E] : semi_normed_group E :=
{ .. h }
/-- Construct a seminormed group from a translation invariant pseudodistance. -/
def semi_normed_group.of_add_dist [has_norm E] [add_comm_group E] [pseudo_metric_space E]
(H1 : ∀ x : E, ∥x∥ = dist x 0)
(H2 : ∀ x y z : E, dist x y ≤ dist (x + z) (y + z)) : semi_normed_group E :=
{ dist_eq := λ x y, begin
rw H1, apply le_antisymm,
{ rw [sub_eq_add_neg, ← add_right_neg y], apply H2 },
{ have := H2 (x - y) 0 y, rwa [sub_add_cancel, zero_add] at this }
end }
/-- Construct a seminormed group from a translation invariant pseudodistance -/
def semi_normed_group.of_add_dist' [has_norm E] [add_comm_group E] [pseudo_metric_space E]
(H1 : ∀ x : E, ∥x∥ = dist x 0)
(H2 : ∀ x y z : E, dist (x + z) (y + z) ≤ dist x y) : semi_normed_group E :=
{ dist_eq := λ x y, begin
rw H1, apply le_antisymm,
{ have := H2 (x - y) 0 y, rwa [sub_add_cancel, zero_add] at this },
{ rw [sub_eq_add_neg, ← add_right_neg y], apply H2 }
end }
/-- A seminormed group can be built from a seminorm that satisfies algebraic properties. This is
formalised in this structure. -/
structure semi_normed_group.core (E : Type*) [add_comm_group E] [has_norm E] : Prop :=
(norm_zero : ∥(0 : E)∥ = 0)
(triangle : ∀ x y : E, ∥x + y∥ ≤ ∥x∥ + ∥y∥)
(norm_neg : ∀ x : E, ∥-x∥ = ∥x∥)
/-- Constructing a seminormed group from core properties of a seminorm, i.e., registering the
pseudodistance and the pseudometric space structure from the seminorm properties. Note that in most
cases this instance creates bad definitional equalities (e.g., it does not take into account
a possibly existing `uniform_space` instance on `E`). -/
def semi_normed_group.of_core (E : Type*) [add_comm_group E] [has_norm E]
(C : semi_normed_group.core E) : semi_normed_group E :=
{ dist := λ x y, ∥x - y∥,
dist_eq := assume x y, by refl,
dist_self := assume x, by simp [C.norm_zero],
dist_triangle := assume x y z,
calc ∥x - z∥ = ∥x - y + (y - z)∥ : by rw sub_add_sub_cancel
... ≤ ∥x - y∥ + ∥y - z∥ : C.triangle _ _,
dist_comm := assume x y,
calc ∥x - y∥ = ∥ -(y - x)∥ : by simp
... = ∥y - x∥ : by { rw [C.norm_neg] } }
instance : normed_group punit :=
{ norm := function.const _ 0,
dist_eq := λ _ _, rfl, }
@[simp] lemma punit.norm_eq_zero (r : punit) : ∥r∥ = 0 := rfl
noncomputable instance : normed_group ℝ :=
{ norm := λ x, |x|,
dist_eq := assume x y, rfl }
lemma real.norm_eq_abs (r : ℝ) : ∥r∥ = |r| := rfl
section semi_normed_group
variables [semi_normed_group E] [semi_normed_group F] [semi_normed_group G]
lemma dist_eq_norm (g h : E) : dist g h = ∥g - h∥ :=
semi_normed_group.dist_eq _ _
lemma dist_eq_norm' (g h : E) : dist g h = ∥h - g∥ :=
by rw [dist_comm, dist_eq_norm]
@[simp] lemma dist_zero_right (g : E) : dist g 0 = ∥g∥ :=
by rw [dist_eq_norm, sub_zero]
@[simp] lemma dist_zero_left : dist (0 : E) = norm :=
funext $ λ g, by rw [dist_comm, dist_zero_right]
lemma tendsto_norm_cocompact_at_top [proper_space E] :
tendsto norm (cocompact E) at_top :=
by simpa only [dist_zero_right] using tendsto_dist_right_cocompact_at_top (0 : E)
lemma norm_sub_rev (g h : E) : ∥g - h∥ = ∥h - g∥ :=
by simpa only [dist_eq_norm] using dist_comm g h
@[simp] lemma norm_neg (g : E) : ∥-g∥ = ∥g∥ :=
by simpa using norm_sub_rev 0 g
@[simp] lemma dist_add_left (g h₁ h₂ : E) : dist (g + h₁) (g + h₂) = dist h₁ h₂ :=
by simp [dist_eq_norm]
@[simp] lemma dist_add_right (g₁ g₂ h : E) : dist (g₁ + h) (g₂ + h) = dist g₁ g₂ :=
by simp [dist_eq_norm]
lemma dist_neg (x y : E) : dist (-x) y = dist x (-y) :=
by simp_rw [dist_eq_norm, ←norm_neg (-x - y), neg_sub, sub_neg_eq_add, add_comm]
@[simp] lemma dist_neg_neg (g h : E) : dist (-g) (-h) = dist g h := by rw [dist_neg, neg_neg]
@[simp] lemma dist_sub_left (g h₁ h₂ : E) : dist (g - h₁) (g - h₂) = dist h₁ h₂ :=
by simp only [sub_eq_add_neg, dist_add_left, dist_neg_neg]
@[simp] lemma dist_sub_right (g₁ g₂ h : E) : dist (g₁ - h) (g₂ - h) = dist g₁ g₂ :=
by simpa only [sub_eq_add_neg] using dist_add_right _ _ _
@[simp] theorem dist_self_add_right (g h : E) : dist g (g + h) = ∥h∥ :=
by rw [← dist_zero_left, ← dist_add_left g 0 h, add_zero]
@[simp] theorem dist_self_add_left (g h : E) : dist (g + h) g = ∥h∥ :=
by rw [dist_comm, dist_self_add_right]
@[simp] theorem dist_self_sub_right (g h : E) : dist g (g - h) = ∥h∥ :=
by rw [sub_eq_add_neg, dist_self_add_right, norm_neg]
@[simp] theorem dist_self_sub_left (g h : E) : dist (g - h) g = ∥h∥ :=
by rw [dist_comm, dist_self_sub_right]
/-- In a (semi)normed group, negation `x ↦ -x` tends to infinity at infinity. TODO: use
`bornology.cobounded` instead of `filter.comap has_norm.norm filter.at_top`. -/
lemma filter.tendsto_neg_cobounded :
tendsto (has_neg.neg : E → E) (comap norm at_top) (comap norm at_top) :=
by simpa only [norm_neg, tendsto_comap_iff, (∘)] using tendsto_comap
/-- **Triangle inequality** for the norm. -/
lemma norm_add_le (g h : E) : ∥g + h∥ ≤ ∥g∥ + ∥h∥ :=
by simpa [dist_eq_norm] using dist_triangle g 0 (-h)
lemma norm_add_le_of_le {g₁ g₂ : E} {n₁ n₂ : ℝ} (H₁ : ∥g₁∥ ≤ n₁) (H₂ : ∥g₂∥ ≤ n₂) :
∥g₁ + g₂∥ ≤ n₁ + n₂ :=
le_trans (norm_add_le g₁ g₂) (add_le_add H₁ H₂)
lemma dist_add_add_le (g₁ g₂ h₁ h₂ : E) :
dist (g₁ + g₂) (h₁ + h₂) ≤ dist g₁ h₁ + dist g₂ h₂ :=
by simpa only [dist_add_left, dist_add_right] using dist_triangle (g₁ + g₂) (h₁ + g₂) (h₁ + h₂)
lemma dist_add_add_le_of_le {g₁ g₂ h₁ h₂ : E} {d₁ d₂ : ℝ}
(H₁ : dist g₁ h₁ ≤ d₁) (H₂ : dist g₂ h₂ ≤ d₂) :
dist (g₁ + g₂) (h₁ + h₂) ≤ d₁ + d₂ :=
le_trans (dist_add_add_le g₁ g₂ h₁ h₂) (add_le_add H₁ H₂)
lemma dist_sub_sub_le (g₁ g₂ h₁ h₂ : E) :
dist (g₁ - g₂) (h₁ - h₂) ≤ dist g₁ h₁ + dist g₂ h₂ :=
by simpa only [sub_eq_add_neg, dist_neg_neg] using dist_add_add_le g₁ (-g₂) h₁ (-h₂)
lemma dist_sub_sub_le_of_le {g₁ g₂ h₁ h₂ : E} {d₁ d₂ : ℝ}
(H₁ : dist g₁ h₁ ≤ d₁) (H₂ : dist g₂ h₂ ≤ d₂) :
dist (g₁ - g₂) (h₁ - h₂) ≤ d₁ + d₂ :=
le_trans (dist_sub_sub_le g₁ g₂ h₁ h₂) (add_le_add H₁ H₂)
lemma abs_dist_sub_le_dist_add_add (g₁ g₂ h₁ h₂ : E) :
|dist g₁ h₁ - dist g₂ h₂| ≤ dist (g₁ + g₂) (h₁ + h₂) :=
by simpa only [dist_add_left, dist_add_right, dist_comm h₂]
using abs_dist_sub_le (g₁ + g₂) (h₁ + h₂) (h₁ + g₂)
@[simp] lemma norm_nonneg (g : E) : 0 ≤ ∥g∥ :=
by { rw[←dist_zero_right], exact dist_nonneg }
@[simp] lemma norm_zero : ∥(0 : E)∥ = 0 := by rw [← dist_zero_right, dist_self]
lemma ne_zero_of_norm_ne_zero {g : E} : ∥g∥ ≠ 0 → g ≠ 0 := mt $ by { rintro rfl, exact norm_zero }
@[nontriviality] lemma norm_of_subsingleton [subsingleton E] (x : E) : ∥x∥ = 0 :=
by rw [subsingleton.elim x 0, norm_zero]
lemma norm_sum_le (s : finset ι) (f : ι → E) : ∥∑ i in s, f i∥ ≤ ∑ i in s, ∥f i∥ :=
s.le_sum_of_subadditive norm norm_zero norm_add_le f
lemma norm_sum_le_of_le (s : finset ι) {f : ι → E} {n : ι → ℝ} (h : ∀ b ∈ s, ∥f b∥ ≤ n b) :
∥∑ b in s, f b∥ ≤ ∑ b in s, n b :=
le_trans (norm_sum_le s f) (finset.sum_le_sum h)
lemma dist_sum_sum_le_of_le (s : finset ι) {f g : ι → E} {d : ι → ℝ}
(h : ∀ b ∈ s, dist (f b) (g b) ≤ d b) :
dist (∑ b in s, f b) (∑ b in s, g b) ≤ ∑ b in s, d b :=
begin
simp only [dist_eq_norm, ← finset.sum_sub_distrib] at *,
exact norm_sum_le_of_le s h
end
lemma dist_sum_sum_le (s : finset ι) (f g : ι → E) :
dist (∑ b in s, f b) (∑ b in s, g b) ≤ ∑ b in s, dist (f b) (g b) :=
dist_sum_sum_le_of_le s (λ _ _, le_rfl)
lemma norm_sub_le (g h : E) : ∥g - h∥ ≤ ∥g∥ + ∥h∥ :=
by simpa [dist_eq_norm] using dist_triangle g 0 h
lemma norm_sub_le_of_le {g₁ g₂ : E} {n₁ n₂ : ℝ} (H₁ : ∥g₁∥ ≤ n₁) (H₂ : ∥g₂∥ ≤ n₂) :
∥g₁ - g₂∥ ≤ n₁ + n₂ :=
le_trans (norm_sub_le g₁ g₂) (add_le_add H₁ H₂)
lemma dist_le_norm_add_norm (g h : E) : dist g h ≤ ∥g∥ + ∥h∥ :=
by { rw dist_eq_norm, apply norm_sub_le }
lemma abs_norm_sub_norm_le (g h : E) : |∥g∥ - ∥h∥| ≤ ∥g - h∥ :=
by simpa [dist_eq_norm] using abs_dist_sub_le g h 0
lemma norm_sub_norm_le (g h : E) : ∥g∥ - ∥h∥ ≤ ∥g - h∥ :=
le_trans (le_abs_self _) (abs_norm_sub_norm_le g h)
lemma dist_norm_norm_le (g h : E) : dist ∥g∥ ∥h∥ ≤ ∥g - h∥ :=
abs_norm_sub_norm_le g h
/-- The direct path from `0` to `v` is shorter than the path with `u` inserted in between. -/
lemma norm_le_insert (u v : E) : ∥v∥ ≤ ∥u∥ + ∥u - v∥ :=
calc ∥v∥ = ∥u - (u - v)∥ : by abel
... ≤ ∥u∥ + ∥u - v∥ : norm_sub_le u _
lemma norm_le_insert' (u v : E) : ∥u∥ ≤ ∥v∥ + ∥u - v∥ :=
by { rw norm_sub_rev, exact norm_le_insert v u }
lemma norm_le_add_norm_add (u v : E) :
∥u∥ ≤ ∥u + v∥ + ∥v∥ :=
calc ∥u∥ = ∥u + v - v∥ : by rw add_sub_cancel
... ≤ ∥u + v∥ + ∥v∥ : norm_sub_le _ _
lemma ball_eq (y : E) (ε : ℝ) : metric.ball y ε = { x | ∥x - y∥ < ε} :=
by { ext, simp [dist_eq_norm], }
lemma ball_zero_eq (ε : ℝ) : ball (0 : E) ε = {x | ∥x∥ < ε} :=
set.ext $ assume a, by simp
lemma mem_ball_iff_norm {g h : E} {r : ℝ} :
h ∈ ball g r ↔ ∥h - g∥ < r :=
by rw [mem_ball, dist_eq_norm]
lemma add_mem_ball_iff_norm {g h : E} {r : ℝ} :
g + h ∈ ball g r ↔ ∥h∥ < r :=
by rw [mem_ball_iff_norm, add_sub_cancel']
lemma mem_ball_iff_norm' {g h : E} {r : ℝ} :
h ∈ ball g r ↔ ∥g - h∥ < r :=
by rw [mem_ball', dist_eq_norm]
@[simp] lemma mem_ball_zero_iff {ε : ℝ} {x : E} : x ∈ ball (0 : E) ε ↔ ∥x∥ < ε :=
by rw [mem_ball, dist_zero_right]
lemma mem_closed_ball_iff_norm {g h : E} {r : ℝ} :
h ∈ closed_ball g r ↔ ∥h - g∥ ≤ r :=
by rw [mem_closed_ball, dist_eq_norm]
@[simp] lemma mem_closed_ball_zero_iff {ε : ℝ} {x : E} : x ∈ closed_ball (0 : E) ε ↔ ∥x∥ ≤ ε :=
by rw [mem_closed_ball, dist_zero_right]
lemma add_mem_closed_ball_iff_norm {g h : E} {r : ℝ} :
g + h ∈ closed_ball g r ↔ ∥h∥ ≤ r :=
by rw [mem_closed_ball_iff_norm, add_sub_cancel']
lemma mem_closed_ball_iff_norm' {g h : E} {r : ℝ} :
h ∈ closed_ball g r ↔ ∥g - h∥ ≤ r :=
by rw [mem_closed_ball', dist_eq_norm]
lemma norm_le_of_mem_closed_ball {g h : E} {r : ℝ} (H : h ∈ closed_ball g r) :
∥h∥ ≤ ∥g∥ + r :=
calc
∥h∥ = ∥g + (h - g)∥ : by rw [add_sub_cancel'_right]
... ≤ ∥g∥ + ∥h - g∥ : norm_add_le _ _
... ≤ ∥g∥ + r : by { apply add_le_add_left, rw ← dist_eq_norm, exact H }
lemma norm_le_norm_add_const_of_dist_le {a b : E} {c : ℝ} (h : dist a b ≤ c) :
∥a∥ ≤ ∥b∥ + c :=
norm_le_of_mem_closed_ball h
lemma norm_lt_of_mem_ball {g h : E} {r : ℝ} (H : h ∈ ball g r) :
∥h∥ < ∥g∥ + r :=
calc
∥h∥ = ∥g + (h - g)∥ : by rw [add_sub_cancel'_right]
... ≤ ∥g∥ + ∥h - g∥ : norm_add_le _ _
... < ∥g∥ + r : by { apply add_lt_add_left, rw ← dist_eq_norm, exact H }
lemma norm_lt_norm_add_const_of_dist_lt {a b : E} {c : ℝ} (h : dist a b < c) :
∥a∥ < ∥b∥ + c :=
norm_lt_of_mem_ball h
lemma bounded_iff_forall_norm_le {s : set E} : bounded s ↔ ∃ C, ∀ x ∈ s, ∥x∥ ≤ C :=
by simpa only [set.subset_def, mem_closed_ball_iff_norm, sub_zero]
using bounded_iff_subset_ball (0 : E)
@[simp] lemma preimage_add_ball (x y : E) (r : ℝ) : ((+) y) ⁻¹' (ball x r) = ball (x - y) r :=
begin
ext z,
simp only [dist_eq_norm, set.mem_preimage, mem_ball],
abel
end
@[simp] lemma preimage_add_closed_ball (x y : E) (r : ℝ) :
((+) y) ⁻¹' (closed_ball x r) = closed_ball (x - y) r :=
begin
ext z,
simp only [dist_eq_norm, set.mem_preimage, mem_closed_ball],
abel
end
@[simp] lemma mem_sphere_iff_norm (v w : E) (r : ℝ) : w ∈ sphere v r ↔ ∥w - v∥ = r :=
by simp [dist_eq_norm]
@[simp] lemma mem_sphere_zero_iff_norm {w : E} {r : ℝ} : w ∈ sphere (0:E) r ↔ ∥w∥ = r :=
by simp [dist_eq_norm]
@[simp] lemma norm_eq_of_mem_sphere {r : ℝ} (x : sphere (0:E) r) : ∥(x:E)∥ = r :=
mem_sphere_zero_iff_norm.mp x.2
lemma preimage_add_sphere (x y : E) (r : ℝ) :
((+) y) ⁻¹' (sphere x r) = sphere (x - y) r :=
begin
ext z,
simp only [set.mem_preimage, mem_sphere_iff_norm],
abel
end
lemma ne_zero_of_mem_sphere {r : ℝ} (hr : r ≠ 0) (x : sphere (0 : E) r) : (x : E) ≠ 0 :=
ne_zero_of_norm_ne_zero $ by rwa norm_eq_of_mem_sphere x
lemma ne_zero_of_mem_unit_sphere (x : sphere (0:E) 1) : (x:E) ≠ 0 :=
ne_zero_of_mem_sphere one_ne_zero _
/-- We equip the sphere, in a seminormed group, with a formal operation of negation, namely the
antipodal map. -/
instance {r : ℝ} : has_neg (sphere (0:E) r) :=
{ neg := λ w, ⟨-↑w, by simp⟩ }
@[simp] lemma coe_neg_sphere {r : ℝ} (v : sphere (0:E) r) :
(((-v) : sphere _ _) : E) = - (v:E) :=
rfl
namespace isometric
-- TODO This material is superseded by similar constructions such as
-- `affine_isometry_equiv.const_vadd`; deduplicate
/-- Addition `y ↦ y + x` as an `isometry`. -/
protected def add_right (x : E) : E ≃ᵢ E :=
{ isometry_to_fun := isometry_emetric_iff_metric.2 $ λ y z, dist_add_right _ _ _,
.. equiv.add_right x }
@[simp] lemma add_right_to_equiv (x : E) :
(isometric.add_right x).to_equiv = equiv.add_right x := rfl
@[simp] lemma coe_add_right (x : E) : (isometric.add_right x : E → E) = λ y, y + x := rfl
lemma add_right_apply (x y : E) : (isometric.add_right x : E → E) y = y + x := rfl
@[simp] lemma add_right_symm (x : E) :
(isometric.add_right x).symm = isometric.add_right (-x) :=
ext $ λ y, rfl
/-- Addition `y ↦ x + y` as an `isometry`. -/
protected def add_left (x : E) : E ≃ᵢ E :=
{ isometry_to_fun := isometry_emetric_iff_metric.2 $ λ y z, dist_add_left _ _ _,
to_equiv := equiv.add_left x }
@[simp] lemma add_left_to_equiv (x : E) :
(isometric.add_left x).to_equiv = equiv.add_left x := rfl
@[simp] lemma coe_add_left (x : E) : ⇑(isometric.add_left x) = (+) x := rfl
@[simp] lemma add_left_symm (x : E) :
(isometric.add_left x).symm = isometric.add_left (-x) :=
ext $ λ y, rfl
variable (E)
/-- Negation `x ↦ -x` as an `isometry`. -/
protected def neg : E ≃ᵢ E :=
{ isometry_to_fun := isometry_emetric_iff_metric.2 $ λ x y, dist_neg_neg _ _,
to_equiv := equiv.neg E }
variable {E}
@[simp] lemma neg_symm : (isometric.neg E).symm = isometric.neg E := rfl
@[simp] lemma neg_to_equiv : (isometric.neg E).to_equiv = equiv.neg E := rfl
@[simp] lemma coe_neg : ⇑(isometric.neg E) = has_neg.neg := rfl
end isometric
theorem normed_group.tendsto_nhds_zero {f : α → E} {l : filter α} :
tendsto f l (𝓝 0) ↔ ∀ ε > 0, ∀ᶠ x in l, ∥ f x ∥ < ε :=
metric.tendsto_nhds.trans $ by simp only [dist_zero_right]
lemma normed_group.tendsto_nhds_nhds {f : E → F} {x : E} {y : F} :
tendsto f (𝓝 x) (𝓝 y) ↔ ∀ ε > 0, ∃ δ > 0, ∀ x', ∥x' - x∥ < δ → ∥f x' - y∥ < ε :=
by simp_rw [metric.tendsto_nhds_nhds, dist_eq_norm]
lemma normed_group.cauchy_seq_iff [nonempty α] [semilattice_sup α] {u : α → E} :
cauchy_seq u ↔ ∀ ε > 0, ∃ N, ∀ m, N ≤ m → ∀ n, N ≤ n → ∥u m - u n∥ < ε :=
by simp [metric.cauchy_seq_iff, dist_eq_norm]
lemma normed_group.nhds_basis_norm_lt (x : E) :
(𝓝 x).has_basis (λ (ε : ℝ), 0 < ε) (λ (ε : ℝ), { y | ∥y - x∥ < ε }) :=
begin
simp_rw ← ball_eq,
exact metric.nhds_basis_ball,
end
lemma normed_group.nhds_zero_basis_norm_lt :
(𝓝 (0 : E)).has_basis (λ (ε : ℝ), 0 < ε) (λ (ε : ℝ), { y | ∥y∥ < ε }) :=
begin
convert normed_group.nhds_basis_norm_lt (0 : E),
simp,
end
lemma normed_group.uniformity_basis_dist :
(𝓤 E).has_basis (λ (ε : ℝ), 0 < ε) (λ ε, {p : E × E | ∥p.fst - p.snd∥ < ε}) :=
begin
convert metric.uniformity_basis_dist,
simp [dist_eq_norm]
end
open finset
/-- A homomorphism `f` of seminormed groups is Lipschitz, if there exists a constant `C` such that
for all `x`, one has `∥f x∥ ≤ C * ∥x∥`. The analogous condition for a linear map of
(semi)normed spaces is in `normed_space.operator_norm`. -/
lemma add_monoid_hom.lipschitz_of_bound (f : E →+ F) (C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) :
lipschitz_with (real.to_nnreal C) f :=
lipschitz_with.of_dist_le' $ λ x y, by simpa only [dist_eq_norm, f.map_sub] using h (x - y)
lemma lipschitz_on_with_iff_norm_sub_le {f : E → F} {C : ℝ≥0} {s : set E} :
lipschitz_on_with C f s ↔ ∀ (x ∈ s) (y ∈ s), ∥f x - f y∥ ≤ C * ∥x - y∥ :=
by simp only [lipschitz_on_with_iff_dist_le_mul, dist_eq_norm]
lemma lipschitz_on_with.norm_sub_le {f : E → F} {C : ℝ≥0} {s : set E} (h : lipschitz_on_with C f s)
{x y : E} (x_in : x ∈ s) (y_in : y ∈ s) : ∥f x - f y∥ ≤ C * ∥x - y∥ :=
lipschitz_on_with_iff_norm_sub_le.mp h x x_in y y_in
lemma lipschitz_on_with.norm_sub_le_of_le {f : E → F} {C : ℝ≥0} {s : set E}
(h : lipschitz_on_with C f s){x y : E} (x_in : x ∈ s) (y_in : y ∈ s) {d : ℝ} (hd : ∥x - y∥ ≤ d) :
∥f x - f y∥ ≤ C * d :=
(h.norm_sub_le x_in y_in).trans $ mul_le_mul_of_nonneg_left hd C.2
lemma lipschitz_with_iff_norm_sub_le {f : E → F} {C : ℝ≥0} :
lipschitz_with C f ↔ ∀ x y, ∥f x - f y∥ ≤ C * ∥x - y∥ :=
by simp only [lipschitz_with_iff_dist_le_mul, dist_eq_norm]
alias lipschitz_with_iff_norm_sub_le ↔ lipschitz_with.norm_sub_le _
lemma lipschitz_with.norm_sub_le_of_le {f : E → F} {C : ℝ≥0} (h : lipschitz_with C f)
{x y : E} {d : ℝ} (hd : ∥x - y∥ ≤ d) :
∥f x - f y∥ ≤ C * d :=
(h.norm_sub_le x y).trans $ mul_le_mul_of_nonneg_left hd C.2
/-- A homomorphism `f` of seminormed groups is continuous, if there exists a constant `C` such that
for all `x`, one has `∥f x∥ ≤ C * ∥x∥`.
The analogous condition for a linear map of normed spaces is in `normed_space.operator_norm`. -/
lemma add_monoid_hom.continuous_of_bound (f : E →+ F) (C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) :
continuous f :=
(f.lipschitz_of_bound C h).continuous
lemma is_compact.exists_bound_of_continuous_on [topological_space α]
{s : set α} (hs : is_compact s) {f : α → E} (hf : continuous_on f s) :
∃ C, ∀ x ∈ s, ∥f x∥ ≤ C :=
begin
have : bounded (f '' s) := (hs.image_of_continuous_on hf).bounded,
rcases bounded_iff_forall_norm_le.1 this with ⟨C, hC⟩,
exact ⟨C, λ x hx, hC _ (set.mem_image_of_mem _ hx)⟩,
end
lemma add_monoid_hom.isometry_iff_norm (f : E →+ F) : isometry f ↔ ∀ x, ∥f x∥ = ∥x∥ :=
begin
simp only [isometry_emetric_iff_metric, dist_eq_norm, ← f.map_sub],
refine ⟨λ h x, _, λ h x y, h _⟩,
simpa using h x 0
end
lemma add_monoid_hom.isometry_of_norm (f : E →+ F) (hf : ∀ x, ∥f x∥ = ∥x∥) : isometry f :=
f.isometry_iff_norm.2 hf
lemma controlled_sum_of_mem_closure {s : add_subgroup E} {g : E}
(hg : g ∈ closure (s : set E)) {b : ℕ → ℝ} (b_pos : ∀ n, 0 < b n) :
∃ v : ℕ → E,
tendsto (λ n, ∑ i in range (n+1), v i) at_top (𝓝 g) ∧
(∀ n, v n ∈ s) ∧
∥v 0 - g∥ < b 0 ∧
∀ n > 0, ∥v n∥ < b n :=
begin
obtain ⟨u : ℕ → E, u_in : ∀ n, u n ∈ s, lim_u : tendsto u at_top (𝓝 g)⟩ :=
mem_closure_iff_seq_limit.mp hg,
obtain ⟨n₀, hn₀⟩ : ∃ n₀, ∀ n ≥ n₀, ∥u n - g∥ < b 0,
{ have : {x | ∥x - g∥ < b 0} ∈ 𝓝 g,
{ simp_rw ← dist_eq_norm,
exact metric.ball_mem_nhds _ (b_pos _) },
exact filter.tendsto_at_top'.mp lim_u _ this },
set z : ℕ → E := λ n, u (n + n₀),
have lim_z : tendsto z at_top (𝓝 g) := lim_u.comp (tendsto_add_at_top_nat n₀),
have mem_𝓤 : ∀ n, {p : E × E | ∥p.1 - p.2∥ < b (n + 1)} ∈ 𝓤 E :=
λ n, by simpa [← dist_eq_norm] using metric.dist_mem_uniformity (b_pos $ n+1),
obtain ⟨φ : ℕ → ℕ, φ_extr : strict_mono φ,
hφ : ∀ n, ∥z (φ $ n + 1) - z (φ n)∥ < b (n + 1)⟩ :=
lim_z.cauchy_seq.subseq_mem mem_𝓤,
set w : ℕ → E := z ∘ φ,
have hw : tendsto w at_top (𝓝 g),
from lim_z.comp φ_extr.tendsto_at_top,
set v : ℕ → E := λ i, if i = 0 then w 0 else w i - w (i - 1),
refine ⟨v, tendsto.congr (finset.eq_sum_range_sub' w) hw , _,
hn₀ _ (n₀.le_add_left _), _⟩,
{ rintro ⟨⟩,
{ change w 0 ∈ s,
apply u_in },
{ apply s.sub_mem ; apply u_in }, },
{ intros l hl,
obtain ⟨k, rfl⟩ : ∃ k, l = k+1, exact nat.exists_eq_succ_of_ne_zero (ne_of_gt hl),
apply hφ },
end
lemma controlled_sum_of_mem_closure_range {j : E →+ F} {h : F}
(Hh : h ∈ (closure $ (j.range : set F))) {b : ℕ → ℝ} (b_pos : ∀ n, 0 < b n) :
∃ g : ℕ → E,
tendsto (λ n, ∑ i in range (n+1), j (g i)) at_top (𝓝 h) ∧
∥j (g 0) - h∥ < b 0 ∧
∀ n > 0, ∥j (g n)∥ < b n :=
begin
rcases controlled_sum_of_mem_closure Hh b_pos with ⟨v, sum_v, v_in, hv₀, hv_pos⟩,
choose g hg using v_in,
change ∀ (n : ℕ), j (g n) = v n at hg,
refine ⟨g, by simpa [← hg] using sum_v, by simpa [hg 0] using hv₀, λ n hn,
by simpa [hg] using hv_pos n hn⟩
end
section nnnorm
/-- Auxiliary class, endowing a type `α` with a function `nnnorm : α → ℝ≥0` with notation `∥x∥₊`. -/
class has_nnnorm (E : Type*) := (nnnorm : E → ℝ≥0)
export has_nnnorm (nnnorm)
notation `∥`e`∥₊` := nnnorm e
@[priority 100] -- see Note [lower instance priority]
instance semi_normed_group.to_has_nnnorm : has_nnnorm E := ⟨λ a, ⟨norm a, norm_nonneg a⟩⟩
@[simp, norm_cast] lemma coe_nnnorm (a : E) : (∥a∥₊ : ℝ) = norm a := rfl
@[simp] lemma coe_comp_nnnorm : (coe : ℝ≥0 → ℝ) ∘ (nnnorm : E → ℝ≥0) = norm := rfl
lemma norm_to_nnreal {a : E} : ∥a∥.to_nnreal = ∥a∥₊ :=
@real.to_nnreal_coe ∥a∥₊
lemma nndist_eq_nnnorm (a b : E) : nndist a b = ∥a - b∥₊ := nnreal.eq $ dist_eq_norm _ _
@[simp] lemma nnnorm_zero : ∥(0 : E)∥₊ = 0 :=
nnreal.eq norm_zero
lemma ne_zero_of_nnnorm_ne_zero {g : E} : ∥g∥₊ ≠ 0 → g ≠ 0 :=
mt $ by { rintro rfl, exact nnnorm_zero }
lemma nnnorm_add_le (g h : E) : ∥g + h∥₊ ≤ ∥g∥₊ + ∥h∥₊ :=
nnreal.coe_le_coe.1 $ norm_add_le g h
@[simp] lemma nnnorm_neg (g : E) : ∥-g∥₊ = ∥g∥₊ :=
nnreal.eq $ norm_neg g
lemma nndist_nnnorm_nnnorm_le (g h : E) : nndist ∥g∥₊ ∥h∥₊ ≤ ∥g - h∥₊ :=
nnreal.coe_le_coe.1 $ dist_norm_norm_le g h
/-- The direct path from `0` to `v` is shorter than the path with `u` inserted in between. -/
lemma nnnorm_le_insert (u v : E) : ∥v∥₊ ≤ ∥u∥₊ + ∥u - v∥₊ := norm_le_insert u v
lemma nnnorm_le_insert' (u v : E) : ∥u∥₊ ≤ ∥v∥₊ + ∥u - v∥₊ := norm_le_insert' u v
lemma nnnorm_le_add_nnnorm_add (u v : E) : ∥u∥₊ ≤ ∥u + v∥₊ + ∥v∥₊ := norm_le_add_norm_add u v
lemma of_real_norm_eq_coe_nnnorm (x : E) : ennreal.of_real ∥x∥ = (∥x∥₊ : ℝ≥0∞) :=
ennreal.of_real_eq_coe_nnreal _
lemma edist_eq_coe_nnnorm_sub (x y : E) : edist x y = (∥x - y∥₊ : ℝ≥0∞) :=
by rw [edist_dist, dist_eq_norm, of_real_norm_eq_coe_nnnorm]
lemma edist_eq_coe_nnnorm (x : E) : edist x 0 = (∥x∥₊ : ℝ≥0∞) :=
by rw [edist_eq_coe_nnnorm_sub, _root_.sub_zero]
lemma mem_emetric_ball_zero_iff {x : E} {r : ℝ≥0∞} : x ∈ emetric.ball (0 : E) r ↔ ↑∥x∥₊ < r :=
by rw [emetric.mem_ball, edist_eq_coe_nnnorm]
lemma nndist_add_add_le (g₁ g₂ h₁ h₂ : E) :
nndist (g₁ + g₂) (h₁ + h₂) ≤ nndist g₁ h₁ + nndist g₂ h₂ :=
nnreal.coe_le_coe.1 $ dist_add_add_le g₁ g₂ h₁ h₂
lemma edist_add_add_le (g₁ g₂ h₁ h₂ : E) :
edist (g₁ + g₂) (h₁ + h₂) ≤ edist g₁ h₁ + edist g₂ h₂ :=
by { simp only [edist_nndist], norm_cast, apply nndist_add_add_le }
@[simp] lemma edist_add_left (g h₁ h₂ : E) : edist (g + h₁) (g + h₂) = edist h₁ h₂ :=
by simp [edist_dist]
@[simp] lemma edist_add_right (g₁ g₂ h : E) : edist (g₁ + h) (g₂ + h) = edist g₁ g₂ :=
by simp [edist_dist]
lemma edist_neg (x y : E) : edist (-x) y = edist x (-y) := by simp_rw [edist_dist, dist_neg]
@[simp] lemma edist_neg_neg (x y : E) : edist (-x) (-y) = edist x y := by rw [edist_neg, neg_neg]
@[simp] lemma edist_sub_left (g h₁ h₂ : E) : edist (g - h₁) (g - h₂) = edist h₁ h₂ :=
by simp only [sub_eq_add_neg, edist_add_left, edist_neg_neg]
@[simp] lemma edist_sub_right (g₁ g₂ h : E) : edist (g₁ - h) (g₂ - h) = edist g₁ g₂ :=
by simpa only [sub_eq_add_neg] using edist_add_right _ _ _
lemma nnnorm_sum_le (s : finset ι) (f : ι → E) :
∥∑ a in s, f a∥₊ ≤ ∑ a in s, ∥f a∥₊ :=
s.le_sum_of_subadditive nnnorm nnnorm_zero nnnorm_add_le f
lemma nnnorm_sum_le_of_le (s : finset ι) {f : ι → E} {n : ι → ℝ≥0} (h : ∀ b ∈ s, ∥f b∥₊ ≤ n b) :
∥∑ b in s, f b∥₊ ≤ ∑ b in s, n b :=
(norm_sum_le_of_le s h).trans_eq nnreal.coe_sum.symm
lemma add_monoid_hom.lipschitz_of_bound_nnnorm (f : E →+ F) (C : ℝ≥0) (h : ∀ x, ∥f x∥₊ ≤ C * ∥x∥₊) :
lipschitz_with C f :=
@real.to_nnreal_coe C ▸ f.lipschitz_of_bound C h
end nnnorm
namespace lipschitz_with
variables [pseudo_emetric_space α] {K Kf Kg : ℝ≥0} {f g : α → E}
lemma neg (hf : lipschitz_with K f) : lipschitz_with K (λ x, -f x) :=
λ x y, (edist_neg_neg _ _).trans_le $ hf x y
lemma add (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) :
lipschitz_with (Kf + Kg) (λ x, f x + g x) :=
λ x y,
calc edist (f x + g x) (f y + g y) ≤ edist (f x) (f y) + edist (g x) (g y) :
edist_add_add_le _ _ _ _
... ≤ Kf * edist x y + Kg * edist x y :
add_le_add (hf x y) (hg x y)
... = (Kf + Kg) * edist x y :
(add_mul _ _ _).symm
lemma sub (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) :
lipschitz_with (Kf + Kg) (λ x, f x - g x) :=
by simpa only [sub_eq_add_neg] using hf.add hg.neg
end lipschitz_with
namespace antilipschitz_with
variables [pseudo_emetric_space α] {K Kf Kg : ℝ≥0} {f g : α → E}
lemma add_lipschitz_with (hf : antilipschitz_with Kf f) (hg : lipschitz_with Kg g)
(hK : Kg < Kf⁻¹) : antilipschitz_with (Kf⁻¹ - Kg)⁻¹ (λ x, f x + g x) :=
begin
letI : pseudo_metric_space α := pseudo_emetric_space.to_pseudo_metric_space hf.edist_ne_top,
refine antilipschitz_with.of_le_mul_dist (λ x y, _),
rw [nnreal.coe_inv, ← div_eq_inv_mul],
rw le_div_iff (nnreal.coe_pos.2 $ tsub_pos_iff_lt.2 hK),
rw [mul_comm, nnreal.coe_sub hK.le, sub_mul],
calc ↑Kf⁻¹ * dist x y - Kg * dist x y ≤ dist (f x) (f y) - dist (g x) (g y) :
sub_le_sub (hf.mul_le_dist x y) (hg.dist_le_mul x y)
... ≤ _ : le_trans (le_abs_self _) (abs_dist_sub_le_dist_add_add _ _ _ _)
end
lemma add_sub_lipschitz_with (hf : antilipschitz_with Kf f) (hg : lipschitz_with Kg (g - f))
(hK : Kg < Kf⁻¹) : antilipschitz_with (Kf⁻¹ - Kg)⁻¹ g :=
by simpa only [pi.sub_apply, add_sub_cancel'_right] using hf.add_lipschitz_with hg hK
lemma le_mul_norm_sub {f : E → F} (hf : antilipschitz_with K f) (x y : E) :
∥x - y∥ ≤ K * ∥f x - f y∥ :=
by simp [← dist_eq_norm, hf.le_mul_dist x y]
end antilipschitz_with
/-- A group homomorphism from an `add_comm_group` to a `semi_normed_group` induces a
`semi_normed_group` structure on the domain.
See note [reducible non-instances] -/
@[reducible]
def semi_normed_group.induced {E} [add_comm_group E] (f : E →+ F) : semi_normed_group E :=
{ norm := λ x, ∥f x∥,
dist_eq := λ x y, by simpa only [add_monoid_hom.map_sub, ← dist_eq_norm],
.. pseudo_metric_space.induced f semi_normed_group.to_pseudo_metric_space, }
/-- A subgroup of a seminormed group is also a seminormed group,
with the restriction of the norm. -/
instance add_subgroup.semi_normed_group (s : add_subgroup E) : semi_normed_group s :=
semi_normed_group.induced s.subtype
/-- If `x` is an element of a subgroup `s` of a seminormed group `E`, its norm in `s` is equal to
its norm in `E`. -/
@[simp] lemma add_subgroup.coe_norm {E : Type*} [semi_normed_group E]
{s : add_subgroup E} (x : s) :
∥(x : s)∥ = ∥(x:E)∥ :=
rfl
/-- If `x` is an element of a subgroup `s` of a seminormed group `E`, its norm in `s` is equal to
its norm in `E`.
This is a reversed version of the `simp` lemma `add_subgroup.coe_norm` for use by `norm_cast`.
-/
@[norm_cast] lemma add_subgroup.norm_coe {E : Type*} [semi_normed_group E] {s : add_subgroup E}
(x : s) :
∥(x : E)∥ = ∥(x : s)∥ :=
rfl
/-- A submodule of a seminormed group is also a seminormed group, with the restriction of the norm.
See note [implicit instance arguments]. -/
instance submodule.semi_normed_group {𝕜 : Type*} {_ : ring 𝕜}
{E : Type*} [semi_normed_group E] {_ : module 𝕜 E} (s : submodule 𝕜 E) : semi_normed_group s :=
{ norm := λx, norm (x : E),
dist_eq := λx y, dist_eq_norm (x : E) (y : E) }
/-- If `x` is an element of a submodule `s` of a normed group `E`, its norm in `s` is equal to its
norm in `E`.
See note [implicit instance arguments]. -/
@[simp] lemma submodule.coe_norm {𝕜 : Type*} {_ : ring 𝕜}
{E : Type*} [semi_normed_group E] {_ : module 𝕜 E} {s : submodule 𝕜 E} (x : s) :
∥(x : s)∥ = ∥(x : E)∥ :=
rfl
/-- If `x` is an element of a submodule `s` of a normed group `E`, its norm in `E` is equal to its
norm in `s`.
This is a reversed version of the `simp` lemma `submodule.coe_norm` for use by `norm_cast`.
See note [implicit instance arguments]. -/
@[norm_cast] lemma submodule.norm_coe {𝕜 : Type*} {_ : ring 𝕜}
{E : Type*} [semi_normed_group E] {_ : module 𝕜 E} {s : submodule 𝕜 E} (x : s) :
∥(x : E)∥ = ∥(x : s)∥ :=
rfl
/-- seminormed group instance on the product of two seminormed groups, using the sup norm. -/
noncomputable instance prod.semi_normed_group : semi_normed_group (E × F) :=
{ norm := λx, max ∥x.1∥ ∥x.2∥,
dist_eq := assume (x y : E × F),
show max (dist x.1 y.1) (dist x.2 y.2) = (max ∥(x - y).1∥ ∥(x - y).2∥), by simp [dist_eq_norm] }
lemma prod.norm_def (x : E × F) : ∥x∥ = (max ∥x.1∥ ∥x.2∥) := rfl
lemma prod.nnnorm_def (x : E × F) : ∥x∥₊ = max (∥x.1∥₊) (∥x.2∥₊) :=
by { have := x.norm_def, simp only [← coe_nnnorm] at this, exact_mod_cast this }
lemma norm_fst_le (x : E × F) : ∥x.1∥ ≤ ∥x∥ :=
le_max_left _ _
lemma norm_snd_le (x : E × F) : ∥x.2∥ ≤ ∥x∥ :=
le_max_right _ _
lemma norm_prod_le_iff {x : E × F} {r : ℝ} :
∥x∥ ≤ r ↔ ∥x.1∥ ≤ r ∧ ∥x.2∥ ≤ r :=
max_le_iff
/-- seminormed group instance on the product of finitely many seminormed groups,
using the sup norm. -/
noncomputable instance pi.semi_normed_group {π : ι → Type*} [fintype ι]
[Π i, semi_normed_group (π i)] : semi_normed_group (Π i, π i) :=
{ norm := λ f, ↑(finset.univ.sup (λ b, ∥f b∥₊)),
dist_eq := assume x y,
congr_arg (coe : ℝ≥0 → ℝ) $ congr_arg (finset.sup finset.univ) $ funext $ assume a,
show nndist (x a) (y a) = ∥x a - y a∥₊, from nndist_eq_nnnorm _ _ }
lemma pi.norm_def {π : ι → Type*} [fintype ι] [Π i, semi_normed_group (π i)] (f : Π i, π i) :
∥f∥ = ↑(finset.univ.sup (λ b, ∥f b∥₊)) := rfl
lemma pi.nnnorm_def {π : ι → Type*} [fintype ι] [Π i, semi_normed_group (π i)] (f : Π i, π i) :
∥f∥₊ = finset.univ.sup (λ b, ∥f b∥₊) := subtype.eta _ _
/-- The seminorm of an element in a product space is `≤ r` if and only if the norm of each
component is. -/
lemma pi_norm_le_iff {π : ι → Type*} [fintype ι] [∀i, semi_normed_group (π i)] {r : ℝ}
(hr : 0 ≤ r) {x : Πi, π i} : ∥x∥ ≤ r ↔ ∀i, ∥x i∥ ≤ r :=
by simp only [← dist_zero_right, dist_pi_le_iff hr, pi.zero_apply]
lemma pi_nnnorm_le_iff {π : ι → Type*} [fintype ι] [∀i, semi_normed_group (π i)] {r : ℝ≥0}
{x : Πi, π i} : ∥x∥₊ ≤ r ↔ ∀i, ∥x i∥₊ ≤ r :=
pi_norm_le_iff r.coe_nonneg
/-- The seminorm of an element in a product space is `< r` if and only if the norm of each
component is. -/
lemma pi_norm_lt_iff {π : ι → Type*} [fintype ι] [∀i, semi_normed_group (π i)] {r : ℝ}
(hr : 0 < r) {x : Πi, π i} : ∥x∥ < r ↔ ∀i, ∥x i∥ < r :=
by simp only [← dist_zero_right, dist_pi_lt_iff hr, pi.zero_apply]
lemma pi_nnnorm_lt_iff {π : ι → Type*} [fintype ι] [∀i, semi_normed_group (π i)] {r : ℝ≥0}
(hr : 0 < r) {x : Πi, π i} : ∥x∥₊ < r ↔ ∀i, ∥x i∥₊ < r :=
pi_norm_lt_iff hr
lemma norm_le_pi_norm {π : ι → Type*} [fintype ι] [∀i, semi_normed_group (π i)] (x : Πi, π i)
(i : ι) : ∥x i∥ ≤ ∥x∥ :=
(pi_norm_le_iff (norm_nonneg x)).1 le_rfl i
lemma nnnorm_le_pi_nnnorm {π : ι → Type*} [fintype ι] [∀i, semi_normed_group (π i)] (x : Πi, π i)
(i : ι) : ∥x i∥₊ ≤ ∥x∥₊ :=
norm_le_pi_norm x i
@[simp] lemma pi_norm_const [nonempty ι] [fintype ι] (a : E) : ∥(λ i : ι, a)∥ = ∥a∥ :=
by simpa only [← dist_zero_right] using dist_pi_const a 0
@[simp] lemma pi_nnnorm_const [nonempty ι] [fintype ι] (a : E) :
∥(λ i : ι, a)∥₊ = ∥a∥₊ :=
nnreal.eq $ pi_norm_const a
/-- The $L^1$ norm is less than the $L^\infty$ norm scaled by the cardinality. -/
lemma pi.sum_norm_apply_le_norm {π : ι → Type*} [fintype ι] [∀i, semi_normed_group (π i)]
(x : Π i, π i) :
∑ i, ∥x i∥ ≤ fintype.card ι • ∥x∥ :=
calc ∑ i, ∥x i∥ ≤ ∑ i : ι, ∥x∥ : finset.sum_le_sum $ λ i hi, norm_le_pi_norm x i
... = fintype.card ι • ∥x∥ : finset.sum_const _
/-- The $L^1$ norm is less than the $L^\infty$ norm scaled by the cardinality. -/
lemma pi.sum_nnnorm_apply_le_nnnorm {π : ι → Type*} [fintype ι] [∀i, semi_normed_group (π i)]
(x : Π i, π i) :
∑ i, ∥x i∥₊ ≤ fintype.card ι • ∥x∥₊ :=
nnreal.coe_sum.trans_le $ pi.sum_norm_apply_le_norm x
lemma tendsto_iff_norm_tendsto_zero {f : α → E} {a : filter α} {b : E} :
tendsto f a (𝓝 b) ↔ tendsto (λ e, ∥f e - b∥) a (𝓝 0) :=
by { convert tendsto_iff_dist_tendsto_zero, simp [dist_eq_norm] }
lemma tendsto_zero_iff_norm_tendsto_zero {f : α → E} {a : filter α} :
tendsto f a (𝓝 0) ↔ tendsto (λ e, ∥f e∥) a (𝓝 0) :=
by { rw [tendsto_iff_norm_tendsto_zero], simp only [sub_zero] }
/-- Special case of the sandwich theorem: if the norm of `f` is eventually bounded by a real
function `g` which tends to `0`, then `f` tends to `0`.
In this pair of lemmas (`squeeze_zero_norm'` and `squeeze_zero_norm`), following a convention of
similar lemmas in `topology.metric_space.basic` and `topology.algebra.order`, the `'` version is
phrased using "eventually" and the non-`'` version is phrased absolutely. -/
lemma squeeze_zero_norm' {f : α → E} {g : α → ℝ} {t₀ : filter α}
(h : ∀ᶠ n in t₀, ∥f n∥ ≤ g n)
(h' : tendsto g t₀ (𝓝 0)) : tendsto f t₀ (𝓝 0) :=
tendsto_zero_iff_norm_tendsto_zero.mpr
(squeeze_zero' (eventually_of_forall (λ n, norm_nonneg _)) h h')
/-- Special case of the sandwich theorem: if the norm of `f` is bounded by a real function `g` which
tends to `0`, then `f` tends to `0`. -/
lemma squeeze_zero_norm {f : α → E} {g : α → ℝ} {t₀ : filter α}
(h : ∀ n, ∥f n∥ ≤ g n) (h' : tendsto g t₀ (𝓝 0)) :
tendsto f t₀ (𝓝 0) :=
squeeze_zero_norm' (eventually_of_forall h) h'
lemma tendsto_norm_sub_self (x : E) : tendsto (λ g : E, ∥g - x∥) (𝓝 x) (𝓝 0) :=
by simpa [dist_eq_norm] using tendsto_id.dist (tendsto_const_nhds : tendsto (λ g, (x:E)) (𝓝 x) _)
lemma tendsto_norm {x : E} : tendsto (λg : E, ∥g∥) (𝓝 x) (𝓝 ∥x∥) :=
by simpa using tendsto_id.dist (tendsto_const_nhds : tendsto (λ g, (0:E)) _ _)
lemma tendsto_norm_zero : tendsto (λg : E, ∥g∥) (𝓝 0) (𝓝 0) :=
by simpa using tendsto_norm_sub_self (0:E)
@[continuity]
lemma continuous_norm : continuous (λg:E, ∥g∥) :=
by simpa using continuous_id.dist (continuous_const : continuous (λ g, (0:E)))
@[continuity]
lemma continuous_nnnorm : continuous (λ (a : E), ∥a∥₊) :=
continuous_subtype_mk _ continuous_norm
lemma lipschitz_with_one_norm : lipschitz_with 1 (norm : E → ℝ) :=
by simpa only [dist_zero_left] using lipschitz_with.dist_right (0 : E)
lemma lipschitz_with_one_nnnorm : lipschitz_with 1 (has_nnnorm.nnnorm : E → ℝ≥0) :=
lipschitz_with_one_norm
lemma uniform_continuous_norm : uniform_continuous (norm : E → ℝ) :=
lipschitz_with_one_norm.uniform_continuous
lemma uniform_continuous_nnnorm : uniform_continuous (λ (a : E), ∥a∥₊) :=
uniform_continuous_subtype_mk uniform_continuous_norm _
/-- A helper lemma used to prove that the (scalar or usual) product of a function that tends to zero
and a bounded function tends to zero. This lemma is formulated for any binary operation
`op : E → F → G` with an estimate `∥op x y∥ ≤ A * ∥x∥ * ∥y∥` for some constant A instead of
multiplication so that it can be applied to `(*)`, `flip (*)`, `(•)`, and `flip (•)`. -/
lemma filter.tendsto.op_zero_is_bounded_under_le' {f : α → E} {g : α → F} {l : filter α}
(hf : tendsto f l (𝓝 0)) (hg : is_bounded_under (≤) l (norm ∘ g)) (op : E → F → G)
(h_op : ∃ A, ∀ x y, ∥op x y∥ ≤ A * ∥x∥ * ∥y∥) :
tendsto (λ x, op (f x) (g x)) l (𝓝 0) :=
begin
cases h_op with A h_op,
rcases hg with ⟨C, hC⟩, rw eventually_map at hC,
rw normed_group.tendsto_nhds_zero at hf ⊢,
intros ε ε₀,
rcases exists_pos_mul_lt ε₀ (A * C) with ⟨δ, δ₀, hδ⟩,
filter_upwards [hf δ δ₀, hC] with i hf hg,
refine (h_op _ _).trans_lt _,
cases le_total A 0 with hA hA,
{ exact (mul_nonpos_of_nonpos_of_nonneg (mul_nonpos_of_nonpos_of_nonneg hA (norm_nonneg _))
(norm_nonneg _)).trans_lt ε₀ },
calc A * ∥f i∥ * ∥g i∥ ≤ A * δ * C :
mul_le_mul (mul_le_mul_of_nonneg_left hf.le hA) hg (norm_nonneg _) (mul_nonneg hA δ₀.le)
... = A * C * δ : mul_right_comm _ _ _
... < ε : hδ
end
/-- A helper lemma used to prove that the (scalar or usual) product of a function that tends to zero
and a bounded function tends to zero. This lemma is formulated for any binary operation
`op : E → F → G` with an estimate `∥op x y∥ ≤ ∥x∥ * ∥y∥` instead of multiplication so that it
can be applied to `(*)`, `flip (*)`, `(•)`, and `flip (•)`. -/
lemma filter.tendsto.op_zero_is_bounded_under_le {f : α → E} {g : α → F} {l : filter α}
(hf : tendsto f l (𝓝 0)) (hg : is_bounded_under (≤) l (norm ∘ g)) (op : E → F → G)
(h_op : ∀ x y, ∥op x y∥ ≤ ∥x∥ * ∥y∥) :
tendsto (λ x, op (f x) (g x)) l (𝓝 0) :=
hf.op_zero_is_bounded_under_le' hg op ⟨1, λ x y, (one_mul (∥x∥)).symm ▸ h_op x y⟩
section
variables {l : filter α} {f : α → E} {a : E}
lemma filter.tendsto.norm (h : tendsto f l (𝓝 a)) : tendsto (λ x, ∥f x∥) l (𝓝 ∥a∥) :=
tendsto_norm.comp h
lemma filter.tendsto.nnnorm (h : tendsto f l (𝓝 a)) :
tendsto (λ x, ∥f x∥₊) l (𝓝 (∥a∥₊)) :=
tendsto.comp continuous_nnnorm.continuous_at h
end
section
variables [topological_space α] {f : α → E} {s : set α} {a : α} {b : E}
lemma continuous.norm (h : continuous f) : continuous (λ x, ∥f x∥) := continuous_norm.comp h
lemma continuous.nnnorm (h : continuous f) : continuous (λ x, ∥f x∥₊) :=
continuous_nnnorm.comp h
lemma continuous_at.norm (h : continuous_at f a) : continuous_at (λ x, ∥f x∥) a := h.norm
lemma continuous_at.nnnorm (h : continuous_at f a) : continuous_at (λ x, ∥f x∥₊) a := h.nnnorm
lemma continuous_within_at.norm (h : continuous_within_at f s a) :
continuous_within_at (λ x, ∥f x∥) s a :=
h.norm
lemma continuous_within_at.nnnorm (h : continuous_within_at f s a) :
continuous_within_at (λ x, ∥f x∥₊) s a :=
h.nnnorm
lemma continuous_on.norm (h : continuous_on f s) : continuous_on (λ x, ∥f x∥) s :=
λ x hx, (h x hx).norm
lemma continuous_on.nnnorm (h : continuous_on f s) : continuous_on (λ x, ∥f x∥₊) s :=
λ x hx, (h x hx).nnnorm
end
/-- If `∥y∥→∞`, then we can assume `y≠x` for any fixed `x`. -/
lemma eventually_ne_of_tendsto_norm_at_top {l : filter α} {f : α → E}
(h : tendsto (λ y, ∥f y∥) l at_top) (x : E) :
∀ᶠ y in l, f y ≠ x :=
begin
have : ∀ᶠ y in l, 1 + ∥x∥ ≤ ∥f y∥ := h (mem_at_top (1 + ∥x∥)),
refine this.mono (λ y hy hxy, _),
subst x,
exact not_le_of_lt zero_lt_one (add_le_iff_nonpos_left.1 hy)
end
@[priority 100] -- see Note [lower instance priority]
instance semi_normed_group.has_lipschitz_add : has_lipschitz_add E :=
{ lipschitz_add := ⟨2, lipschitz_with.prod_fst.add lipschitz_with.prod_snd⟩ }
/-- A seminormed group is a uniform additive group, i.e., addition and subtraction are uniformly
continuous. -/
@[priority 100] -- see Note [lower instance priority]
instance normed_uniform_group : uniform_add_group E :=
⟨(lipschitz_with.prod_fst.sub lipschitz_with.prod_snd).uniform_continuous⟩
@[priority 100] -- see Note [lower instance priority]
instance normed_top_group : topological_add_group E :=
by apply_instance -- short-circuit type class inference
lemma nat.norm_cast_le [has_one E] : ∀ n : ℕ, ∥(n : E)∥ ≤ n * ∥(1 : E)∥
| 0 := by simp
| (n + 1) := by { rw [n.cast_succ, n.cast_succ, add_mul, one_mul],
exact norm_add_le_of_le (nat.norm_cast_le n) le_rfl }
lemma semi_normed_group.mem_closure_iff {s : set E} {x : E} :
x ∈ closure s ↔ ∀ ε > 0, ∃ y ∈ s, ∥x - y∥ < ε :=
by simp [metric.mem_closure_iff, dist_eq_norm]
lemma norm_le_zero_iff' [separated_space E] {g : E} :
∥g∥ ≤ 0 ↔ g = 0 :=
begin
letI : normed_group E := { to_metric_space := of_t2_pseudo_metric_space ‹_›,
.. ‹semi_normed_group E› },
rw [← dist_zero_right], exact dist_le_zero
end
lemma norm_eq_zero_iff' [separated_space E] {g : E} : ∥g∥ = 0 ↔ g = 0 :=
(norm_nonneg g).le_iff_eq.symm.trans norm_le_zero_iff'
lemma norm_pos_iff' [separated_space E] {g : E} : 0 < ∥g∥ ↔ g ≠ 0 :=
by rw [← not_le, norm_le_zero_iff']
lemma cauchy_seq_sum_of_eventually_eq {u v : ℕ → E} {N : ℕ} (huv : ∀ n ≥ N, u n = v n)
(hv : cauchy_seq (λ n, ∑ k in range (n+1), v k)) : cauchy_seq (λ n, ∑ k in range (n + 1), u k) :=
begin
let d : ℕ → E := λ n, ∑ k in range (n + 1), (u k - v k),
rw show (λ n, ∑ k in range (n + 1), u k) = d + (λ n, ∑ k in range (n + 1), v k),
by { ext n, simp [d] },
have : ∀ n ≥ N, d n = d N,
{ intros n hn,
dsimp [d],
rw eventually_constant_sum _ hn,
intros m hm,
simp [huv m hm] },
exact (tendsto_at_top_of_eventually_const this).cauchy_seq.add hv
end
end semi_normed_group
section normed_group
/-- Construct a normed group from a translation invariant distance -/
def normed_group.of_add_dist [has_norm E] [add_comm_group E] [metric_space E]
(H1 : ∀ x : E, ∥x∥ = dist x 0)
(H2 : ∀ x y z : E, dist x y ≤ dist (x + z) (y + z)) : normed_group E :=
{ dist_eq := λ x y, begin
rw H1, apply le_antisymm,
{ rw [sub_eq_add_neg, ← add_right_neg y], apply H2 },
{ have := H2 (x-y) 0 y, rwa [sub_add_cancel, zero_add] at this }
end }
/-- A normed group can be built from a norm that satisfies algebraic properties. This is
formalised in this structure. -/
structure normed_group.core (E : Type*) [add_comm_group E] [has_norm E] : Prop :=
(norm_eq_zero_iff : ∀ x : E, ∥x∥ = 0 ↔ x = 0)
(triangle : ∀ x y : E, ∥x + y∥ ≤ ∥x∥ + ∥y∥)
(norm_neg : ∀ x : E, ∥-x∥ = ∥x∥)
/-- The `semi_normed_group.core` induced by a `normed_group.core`. -/
lemma normed_group.core.to_semi_normed_group.core {E : Type*} [add_comm_group E] [has_norm E]
(C : normed_group.core E) : semi_normed_group.core E :=
{ norm_zero := (C.norm_eq_zero_iff 0).2 rfl,
triangle := C.triangle,
norm_neg := C.norm_neg }
/-- Constructing a normed group from core properties of a norm, i.e., registering the distance and
the metric space structure from the norm properties. -/
def normed_group.of_core (E : Type*) [add_comm_group E] [has_norm E]
(C : normed_group.core E) : normed_group E :=
{ eq_of_dist_eq_zero := λ x y h,
begin
rw [dist_eq_norm] at h,
exact sub_eq_zero.mp ((C.norm_eq_zero_iff _).1 h)
end
..semi_normed_group.of_core E (normed_group.core.to_semi_normed_group.core C) }
variables [normed_group E] [normed_group F]
@[simp] lemma norm_eq_zero {g : E} : ∥g∥ = 0 ↔ g = 0 := norm_eq_zero_iff'
lemma norm_ne_zero_iff {g : E} : ∥g∥ ≠ 0 ↔ g ≠ 0 := not_congr norm_eq_zero
@[simp] lemma norm_pos_iff {g : E} : 0 < ∥ g ∥ ↔ g ≠ 0 := norm_pos_iff'
@[simp] lemma norm_le_zero_iff {g : E} : ∥g∥ ≤ 0 ↔ g = 0 := norm_le_zero_iff'
lemma norm_sub_eq_zero_iff {u v : E} : ∥u - v∥ = 0 ↔ u = v :=
by rw [norm_eq_zero, sub_eq_zero]
lemma eq_of_norm_sub_le_zero {g h : E} (a : ∥g - h∥ ≤ 0) : g = h :=
by rwa [← sub_eq_zero, ← norm_le_zero_iff]
lemma eq_of_norm_sub_eq_zero {u v : E} (h : ∥u - v∥ = 0) : u = v :=
norm_sub_eq_zero_iff.1 h
@[simp] lemma nnnorm_eq_zero {a : E} : ∥a∥₊ = 0 ↔ a = 0 :=
by rw [← nnreal.coe_eq_zero, coe_nnnorm, norm_eq_zero]
lemma nnnorm_ne_zero_iff {g : E} : ∥g∥₊ ≠ 0 ↔ g ≠ 0 := not_congr nnnorm_eq_zero
/-- An injective group homomorphism from an `add_comm_group` to a `normed_group` induces a
`normed_group` structure on the domain.
See note [reducible non-instances]. -/
@[reducible]
def normed_group.induced {E} [add_comm_group E]
(f : E →+ F) (h : function.injective f) : normed_group E :=
{ .. semi_normed_group.induced f,
.. metric_space.induced f h normed_group.to_metric_space, }
/-- A subgroup of a normed group is also a normed group, with the restriction of the norm. -/
instance add_subgroup.normed_group (s : add_subgroup E) : normed_group s :=
normed_group.induced s.subtype subtype.coe_injective
/-- A submodule of a normed group is also a normed group, with the restriction of the norm.
See note [implicit instance arguments]. -/
instance submodule.normed_group {𝕜 : Type*} {_ : ring 𝕜}
{E : Type*} [normed_group E] {_ : module 𝕜 E} (s : submodule 𝕜 E) : normed_group s :=
{ ..submodule.semi_normed_group s }
/-- normed group instance on the product of two normed groups, using the sup norm. -/
noncomputable instance prod.normed_group : normed_group (E × F) := { ..prod.semi_normed_group }
/-- normed group instance on the product of finitely many normed groups, using the sup norm. -/
noncomputable instance pi.normed_group {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] :
normed_group (Πi, π i) := { ..pi.semi_normed_group }
lemma tendsto_norm_sub_self_punctured_nhds (a : E) : tendsto (λ x, ∥x - a∥) (𝓝[≠] a) (𝓝[>] 0) :=
(tendsto_norm_sub_self a).inf $ tendsto_principal_principal.2 $ λ x hx,
norm_pos_iff.2 $ sub_ne_zero.2 hx
lemma tendsto_norm_nhds_within_zero : tendsto (norm : E → ℝ) (𝓝[≠] 0) (𝓝[>] 0) :=
tendsto_norm_zero.inf $ tendsto_principal_principal.2 $ λ x, norm_pos_iff.2
/-! Some relations with `has_compact_support` -/
lemma has_compact_support_norm_iff [topological_space α] {f : α → E} :
has_compact_support (λ x, ∥ f x ∥) ↔ has_compact_support f :=
has_compact_support_comp_left $ λ x, norm_eq_zero
alias has_compact_support_norm_iff ↔ _ has_compact_support.norm
lemma continuous.bounded_above_of_compact_support [topological_space α] {f : α → E}
(hf : continuous f) (hsupp : has_compact_support f) : ∃ C, ∀ x, ∥f x∥ ≤ C :=
by simpa [bdd_above_def] using hf.norm.bdd_above_range_of_has_compact_support hsupp.norm
end normed_group
|
3fdd98eccf311d57c7a7225882e511ce720be846 | 99b5e6372af1f404777312358869f95be7de84a3 | /src/hott/types/nat/basic.lean | afe4bbdeb0da17c37c37f2dec428bb58f045683c | [
"Apache-2.0"
] | permissive | forked-from-1kasper/hott3 | 8fa064ab5e8c9d6752a783d74ab226ddc5b5232a | 2db24de7a361a7793b0eae4ca5c3fd4d4a0fc691 | refs/heads/master | 1,584,867,131,028 | 1,530,766,841,000 | 1,530,766,841,000 | 139,797,034 | 0 | 0 | Apache-2.0 | 1,530,766,961,000 | 1,530,766,961,000 | null | UTF-8 | Lean | false | false | 10,137 | lean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
(Ported from standard library)
Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad
Basic operations on the natural numbers.
-/
import hott.algebra.ring
universes u v w
hott_theory
namespace hott
open eq hott.algebra is_trunc nat unit hott.decidable hott.binary
namespace nat
-- /- a variant of add, defined by recursion on the first argument -/
-- @[hott] def addl (x y : ℕ) : ℕ :=
-- nat.rec_on x y (λ n r, succ r)
-- infix ` ⊕ ` := addl
-- @[hott] theorem addl_succ_right (n m : ℕ) : n ⊕ succ m = succ (n ⊕ m) :=
-- nat.rec_on n
-- rfl
-- (λ n₁ ih, calc
-- succ n₁ ⊕ succ m = succ (n₁ ⊕ succ m) : rfl
-- ... = succ (succ (n₁ ⊕ m)) : ih
-- ... = succ (succ n₁ ⊕ m) : rfl)
-- @[hott] theorem add_eq_addl (x : ℕ) : Πy, x + y = x ⊕ y :=
-- nat.rec_on x
-- (λ y, nat.rec_on y
-- rfl
-- (λ y₁ ih, calc
-- 0 + succ y₁ = succ (0 + y₁) : rfl
-- ... = succ (0 ⊕ y₁) : {ih}
-- ... = 0 ⊕ (succ y₁) : rfl))
-- (λ x₁ ih₁ y, nat.rec_on y
-- (calc
-- succ x₁ + 0 = succ (x₁ + 0) : rfl
-- ... = succ (x₁ ⊕ 0) : {ih₁ 0}
-- ... = succ x₁ ⊕ 0 : rfl)
-- (λ y₁ ih₂, calc
-- succ x₁ + succ y₁ = succ (succ x₁ + y₁) : rfl
-- ... = succ (succ x₁ ⊕ y₁) : {ih₂}
-- ... = succ x₁ ⊕ succ y₁ : addl_succ_right))
/- successor and predecessor -/
@[hott, reducible] protected def code : ℕ → ℕ → Type
| 0 0 := unit
| 0 (succ m) := empty
| (succ n) 0 := empty
| (succ n) (succ m) := code n m
@[hott] protected def refl : Πn, nat.code n n
| 0 := star
| (succ n) := refl n
@[hott] protected def encode {n m : ℕ} (p : n = m) : nat.code n m :=
p ▸ nat.refl n
@[hott] protected def decode : Π(n m : ℕ), nat.code n m → n = m
| 0 0 := λc, idp
| 0 (succ l) := λc, empty.elim c
| (succ k) 0 := λc, empty.elim c
| (succ k) (succ l) := λc, ap succ (decode k l c)
@[hott] def succ_ne_zero (n : ℕ) : succ n ≠ 0 :=
nat.encode
@[hott, hsimp] def pred_zero : pred 0 = 0 :=
rfl
@[hott, hsimp] def pred_succ (n : ℕ) : pred (succ n) = n :=
rfl
@[hott] theorem eq_zero_sum_eq_succ_pred (n : ℕ) : n = 0 ⊎ n = succ (pred n) :=
begin
hinduction n with n IH,
{ exact sum.inl rfl },
{ exact sum.inr rfl }
end
@[hott] theorem exists_eq_succ_of_ne_zero {n : ℕ} (H : n ≠ 0) : Σk : ℕ, n = succ k :=
sigma.mk (pred n) (sum.resolve_left (eq_zero_sum_eq_succ_pred n) H)
@[hott] def succ.inj {n m : ℕ} (H : succ n = succ m) : n = m :=
nat.decode n m (@nat.encode (succ n) (succ m) H)
@[hott] theorem eq_of_succ_eq_succ {n m : ℕ} (H : succ n = succ m) : n = m := succ.inj H
@[hott] theorem succ_ne_self {n : ℕ} : succ n ≠ n :=
begin
induction n with n IH,
{ apply succ_ne_zero },
{ intro H, apply IH, apply succ.inj, exact H }
end
@[hott, instance, priority nat.prio] protected def has_decidable_eq : Π x y : nat, decidable (x = y)
| 0 0 := inl rfl
| (succ x) 0 := inr (succ_ne_zero x)
| 0 (succ y) := inr (ne.symm (succ_ne_zero y))
| (succ x) (succ y) :=
match has_decidable_eq x y with
| inl xeqy := inl (ap succ xeqy)
| inr xney := inr (λH, xney (succ.inj H))
end
@[hott] theorem discriminate {B : Type _} {n : ℕ} (H1: n = 0 → B) (H2 : Πm, n = succ m → B) : B :=
have H : n = n → B, begin induction n with n IH, exact H1, apply H2 end,
H rfl
@[hott] theorem two_step_rec_on {P : ℕ → Type _} (a : ℕ) (H1 : P 0) (H2 : P 1)
(H3 : Π (n : ℕ) (IH1 : P n) (IH2 : P (succ n)), P (succ (succ n))) : P a :=
have stronger : P a × P (succ a), from
nat.rec_on a
(pair H1 H2)
(λk IH,
have IH1 : P k, from IH.fst,
have IH2 : P (succ k), from IH.snd,
pair IH2 (H3 k IH1 IH2)),
stronger.fst
@[hott, elab_as_eliminator] theorem sub_induction {P : ℕ → ℕ → Type _} (n m : ℕ) (H1 : Πm, P 0 m)
(H2 : Πn, P (succ n) 0) (H3 : Πn m, P n m → P (succ n) (succ m)) : P n m :=
have general : Πm, P n m, from nat.rec_on n H1
(λk : ℕ,
assume IH : Πm, P k m,
λm : ℕ,
nat.cases_on m (H2 k) (λl, (H3 k l (IH l)))),
general m
/- addition -/
@[hott, hsimp] protected def add_zero (n : ℕ) : n + 0 = n :=
rfl
@[hott, hsimp] def add_succ (n m : ℕ) : n + succ m = succ (n + m) :=
rfl
@[hott, hsimp] protected def zero_add (n : ℕ) : 0 + n = n :=
begin
induction n with n IH,
reflexivity,
exact ap succ IH
end
@[hott, hsimp] def succ_add (n m : ℕ) : (succ n) + m = succ (n + m) :=
begin
induction m with m IH,
reflexivity,
exact ap succ IH
end
@[hott, hsimp] protected def add_comm (n m : ℕ) : n + m = m + n :=
begin
induction n with n IH,
{ apply nat.zero_add},
{ exact succ_add n m ⬝ ap succ IH}
end
@[hott] protected def add_add (n l k : ℕ) : n + l + k = n + (k + l) :=
begin
induction l with l IH,
reflexivity,
exact succ_add (n + l) k ⬝ ap succ IH
end
@[hott] def succ_add_eq_succ_add (n m : ℕ) : succ n + m = n + succ m :=
succ_add n m
@[hott, hsimp] protected def add_assoc (n m k : ℕ) : (n + m) + k = n + (m + k) :=
begin
induction k with k IH,
reflexivity,
exact ap succ IH
end
@[hott] protected theorem add_left_comm : Π (n m k : ℕ), n + (m + k) = m + (n + k) :=
left_comm nat.add_comm nat.add_assoc
@[hott] protected theorem add_right_comm : Π (n m k : ℕ), n + m + k = n + k + m :=
right_comm nat.add_comm nat.add_assoc
@[hott] protected def add_left_cancel {n m k : ℕ} : n + m = n + k → m = k :=
nat.rec_on n
(λH : 0 + m = 0 + k,
(nat.zero_add _)⁻¹ ⬝ H ⬝ nat.zero_add _)
(λ(n : ℕ) (IH : n + m = n + k → m = k) (H : succ n + m = succ n + k),
have succ (n + m) = succ (n + k),
from calc
succ (n + m) = succ n + m : by rwr succ_add
... = succ n + k : H
... = succ (n + k) : by rwr succ_add,
have n + m = n + k, from succ.inj this,
IH this)
@[hott] protected def add_right_cancel {n m k : ℕ} (H : n + m = k + m) : n = k :=
have H2 : m + n = m + k, from nat.add_comm _ _ ⬝ H ⬝ nat.add_comm _ _,
nat.add_left_cancel H2
@[hott] theorem eq_zero_of_add_eq_zero_right {n m : ℕ} : n + m = 0 → n = 0 :=
nat.rec_on n
(λ(H : 0 + m = 0), rfl)
(λk IH,
assume H : succ k + m = 0,
absurd
(show succ (k + m) = 0, from calc
succ (k + m) = succ k + m : by rwr succ_add
... = 0 : H)
(succ_ne_zero _))
@[hott] theorem eq_zero_of_add_eq_zero_left {n m : ℕ} (H : n + m = 0) : m = 0 :=
eq_zero_of_add_eq_zero_right (nat.add_comm _ _ ⬝ H)
@[hott] theorem eq_zero_prod_eq_zero_of_add_eq_zero {n m : ℕ} (H : n + m = 0) : n = 0 × m = 0 :=
pair (eq_zero_of_add_eq_zero_right H) (eq_zero_of_add_eq_zero_left H)
@[hott, hsimp] theorem add_one (n : ℕ) : n + 1 = succ n := rfl
@[hott] theorem one_add (n : ℕ) : 1 + n = succ n :=
succ_add 0 n ⬝ ap succ (nat.zero_add n)
/- multiplication -/
@[hott, hsimp] protected def mul_zero (n : ℕ) : n * 0 = 0 :=
rfl
@[hott, hsimp] def mul_succ (n m : ℕ) : n * succ m = n * m + n :=
rfl
-- commutativity, distributivity, associativity, identity
@[hott, hsimp] protected def zero_mul (n : ℕ) : 0 * n = 0 :=
begin induction n with n IH, refl, exact IH end
@[hott, hsimp] def succ_mul (n m : ℕ) : succ n * m = n * m + m :=
begin
hinduction m with m IH,
{ refl },
{ rwr [mul_succ, IH, add_succ, nat.add_assoc, nat.add_comm m n, ←nat.add_assoc] }
end
@[hott, hsimp] protected def mul_comm (n m : ℕ) : n * m = m * n :=
begin
hinduction m with m IH,
{ rwr [nat.zero_mul] },
{ rwr [mul_succ, succ_mul, IH] }
end
@[hott] protected def right_distrib (n m k : ℕ) : (n + m) * k = n * k + m * k :=
begin
hinduction k with k IH,
{ refl },
{ rwr [mul_succ, IH, nat.add_assoc (n*k), ←nat.add_assoc (m*k), nat.add_comm (m*k),
nat.add_assoc n, ←nat.add_assoc (n*k)] }
end
@[hott] protected def left_distrib (n m k : ℕ) : n * (m + k) = n * m + n * k :=
calc
n * (m + k) = (m + k) * n : by rwr nat.mul_comm
... = m * n + k * n : by rwr nat.right_distrib
... = n * m + k * n : by rwr nat.mul_comm
... = n * m + n * k : by rwr nat.mul_comm k n
@[hott, hsimp] protected def mul_assoc (n m k : ℕ) : (n * m) * k = n * (m * k) :=
begin
hinduction k with k IH,
{ refl },
{ rwr [mul_succ, IH, ←nat.left_distrib] }
end
@[hott, hsimp] protected def mul_one (n : ℕ) : n * 1 = n :=
calc
n * 1 = n * 0 + n : by rwr mul_succ
... = 0 + n : by rwr nat.mul_zero
... = n : by rwr nat.zero_add
@[hott, hsimp] protected def one_mul (n : ℕ) : 1 * n = n :=
calc
1 * n = n * 1 : by rwr nat.mul_comm
... = n : by rwr nat.mul_one
@[hott] theorem eq_zero_sum_eq_zero_of_mul_eq_zero {n m : ℕ} : n * m = 0 → n = 0 ⊎ m = 0 :=
begin
intro H, hinduction n with n IH, { exact sum.inl rfl },
induction m with m IH, { exact sum.inr rfl },
apply empty.elim, exact succ_ne_zero _ H
end
@[hott, instance] protected def comm_semiring : hott.algebra.comm_semiring nat :=
{ add := nat.add,
add_assoc := nat.add_assoc,
zero := nat.zero,
zero_add := nat.zero_add,
add_zero := nat.add_zero,
add_comm := nat.add_comm,
mul := nat.mul,
mul_assoc := nat.mul_assoc,
one := nat.succ nat.zero,
one_mul := nat.one_mul,
mul_one := nat.mul_one,
left_distrib := nat.left_distrib,
right_distrib := nat.right_distrib,
zero_mul := nat.zero_mul,
mul_zero := nat.mul_zero,
mul_comm := nat.mul_comm,
is_set_carrier := by apply_instance }
end nat
section
open nat
@[hott] def iterate {A : Type _} (op : A → A) : ℕ → A → A
| 0 := λ a, a
| (succ k) := λ a, op (iterate k a)
notation f `^[`:80 n:0 `]`:0 := iterate f n
end
end hott
|
b159ddda865ae8cd22498715abf44aead0e4725f | dd4e652c749fea9ac77e404005cb3470e5f75469 | /src/missing_mathlib/algebra/group/units.lean | 501ab7dc809b018e123eb2d1a0a4706c73c23c5d | [] | no_license | skbaek/cvx | e32822ad5943541539966a37dee162b0a5495f55 | c50c790c9116f9fac8dfe742903a62bdd7292c15 | refs/heads/master | 1,623,803,010,339 | 1,618,058,958,000 | 1,618,058,958,000 | 176,293,135 | 3 | 2 | null | null | null | null | UTF-8 | Lean | false | false | 291 | lean | import algebra.group.units
universe variables u
lemma mul_unit_eq_iff_mul_inv_eq {α : Type u} [monoid α] (a b : α) (c : units α) :
a * c = b ↔ a = b * (@has_inv.inv (units α) _ c) :=
by rw [←units.inv_mul_cancel_right b c, units.mul_right_inj, mul_assoc, units.mul_inv, mul_one]
|
74f42cef5aa27f85c0ce8cc8acf3805942b029e5 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/group_theory/transfer.lean | 3bc1bc754c47a2f7fb1a156485060762f587646d | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 10,529 | lean | /-
Copyright (c) 2022 Thomas Browning. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Thomas Browning
-/
import group_theory.complement
import group_theory.sylow
/-!
# The Transfer Homomorphism
In this file we construct the transfer homomorphism.
## Main definitions
- `diff ϕ S T` : The difference of two left transversals `S` and `T` under the homomorphism `ϕ`.
- `transfer ϕ` : The transfer homomorphism induced by `ϕ`.
- `transfer_center_pow`: The transfer homomorphism `G →* center G`.
## Main results
- `transfer_center_pow_apply`:
The transfer homomorphism `G →* center G` is given by `g ↦ g ^ (center G).index`.
- `ker_transfer_sylow_is_complement'`: Burnside's transfer (or normal `p`-complement) theorem:
If `hP : N(P) ≤ C(P)`, then `(transfer P hP).ker` is a normal `p`-complement.
-/
open_locale big_operators
variables {G : Type*} [group G] {H : subgroup G} {A : Type*} [comm_group A] (ϕ : H →* A)
namespace subgroup
namespace left_transversals
open finset mul_action
open_locale pointwise
variables (R S T : left_transversals (H : set G)) [finite_index H]
/-- The difference of two left transversals -/
@[to_additive "The difference of two left transversals"]
noncomputable def diff : A :=
let α := mem_left_transversals.to_equiv S.2, β := mem_left_transversals.to_equiv T.2 in
(@finset.univ (G ⧸ H) H.fintype_quotient_of_finite_index).prod $
λ q, ϕ ⟨(α q)⁻¹ * β q, quotient_group.left_rel_apply.mp $
quotient.exact' ((α.symm_apply_apply q).trans (β.symm_apply_apply q).symm)⟩
@[to_additive] lemma diff_mul_diff : diff ϕ R S * diff ϕ S T = diff ϕ R T :=
prod_mul_distrib.symm.trans (prod_congr rfl (λ q hq, (ϕ.map_mul _ _).symm.trans (congr_arg ϕ
(by simp_rw [subtype.ext_iff, coe_mul, coe_mk, mul_assoc, mul_inv_cancel_left]))))
@[to_additive] lemma diff_self : diff ϕ T T = 1 :=
mul_right_eq_self.mp (diff_mul_diff ϕ T T T)
@[to_additive] lemma diff_inv : (diff ϕ S T)⁻¹ = diff ϕ T S :=
inv_eq_of_mul_eq_one_right $ (diff_mul_diff ϕ S T S).trans $ diff_self ϕ S
@[to_additive] lemma smul_diff_smul (g : G) : diff ϕ (g • S) (g • T) = diff ϕ S T :=
let h := H.fintype_quotient_of_finite_index in by exactI
prod_bij' (λ q _, g⁻¹ • q) (λ _ _, mem_univ _) (λ _ _, congr_arg ϕ (by simp_rw [coe_mk,
smul_apply_eq_smul_apply_inv_smul, smul_eq_mul, mul_inv_rev, mul_assoc, inv_mul_cancel_left]))
(λ q _, g • q) (λ _ _, mem_univ _) (λ q _, smul_inv_smul g q) (λ q _, inv_smul_smul g q)
end left_transversals
end subgroup
namespace monoid_hom
open mul_action subgroup subgroup.left_transversals
/-- Given `ϕ : H →* A` from `H : subgroup G` to a commutative group `A`,
the transfer homomorphism is `transfer ϕ : G →* A`. -/
@[to_additive "Given `ϕ : H →+ A` from `H : add_subgroup G` to an additive commutative group `A`,
the transfer homomorphism is `transfer ϕ : G →+ A`."]
noncomputable def transfer [finite_index H] : G →* A :=
let T : left_transversals (H : set G) := inhabited.default in
{ to_fun := λ g, diff ϕ T (g • T),
map_one' := by rw [one_smul, diff_self],
map_mul' := λ g h, by rw [mul_smul, ←diff_mul_diff, smul_diff_smul] }
variables (T : left_transversals (H : set G))
@[to_additive] lemma transfer_def [finite_index H] (g : G) : transfer ϕ g = diff ϕ T (g • T) :=
by rw [transfer, ←diff_mul_diff, ←smul_diff_smul, mul_comm, diff_mul_diff]; refl
/-- Explicit computation of the transfer homomorphism. -/
lemma transfer_eq_prod_quotient_orbit_rel_zpowers_quot [finite_index H]
(g : G) [fintype (quotient (orbit_rel (zpowers g) (G ⧸ H)))] :
transfer ϕ g = ∏ (q : quotient (orbit_rel (zpowers g) (G ⧸ H))),
ϕ ⟨q.out'.out'⁻¹ * g ^ function.minimal_period ((•) g) q.out' * q.out'.out',
quotient_group.out'_conj_pow_minimal_period_mem H g q.out'⟩ :=
begin
classical,
letI := H.fintype_quotient_of_finite_index,
calc transfer ϕ g = ∏ (q : G ⧸ H), _ : transfer_def ϕ (transfer_transversal H g) g
... = _ : ((quotient_equiv_sigma_zmod H g).symm.prod_comp _).symm
... = _ : finset.prod_sigma _ _ _
... = _ : fintype.prod_congr _ _ (λ q, _),
simp only [quotient_equiv_sigma_zmod_symm_apply,
transfer_transversal_apply', transfer_transversal_apply''],
rw fintype.prod_eq_single (0 : zmod (function.minimal_period ((•) g) q.out')) (λ k hk, _),
{ simp only [if_pos, zmod.cast_zero, zpow_zero, one_mul, mul_assoc] },
{ simp only [if_neg hk, inv_mul_self],
exact map_one ϕ },
end
/-- Auxillary lemma in order to state `transfer_eq_pow`. -/
lemma transfer_eq_pow_aux (g : G)
(key : ∀ (k : ℕ) (g₀ : G), g₀⁻¹ * g ^ k * g₀ ∈ H → g₀⁻¹ * g ^ k * g₀ = g ^ k) :
g ^ H.index ∈ H :=
begin
by_cases hH : H.index = 0,
{ rw [hH, pow_zero],
exact H.one_mem },
letI := fintype_of_index_ne_zero hH,
classical,
replace key : ∀ (k : ℕ) (g₀ : G), g₀⁻¹ * g ^ k * g₀ ∈ H → g ^ k ∈ H :=
λ k g₀ hk, (_root_.congr_arg (∈ H) (key k g₀ hk)).mp hk,
replace key : ∀ q : G ⧸ H, g ^ function.minimal_period ((•) g) q ∈ H :=
λ q, key (function.minimal_period ((•) g) q) q.out'
(quotient_group.out'_conj_pow_minimal_period_mem H g q),
let f : quotient (orbit_rel (zpowers g) (G ⧸ H)) → zpowers g :=
λ q, (⟨g, mem_zpowers g⟩ : zpowers g) ^ function.minimal_period ((•) g) q.out',
have hf : ∀ q, f q ∈ H.subgroup_of (zpowers g) := λ q, key q.out',
replace key := subgroup.prod_mem (H.subgroup_of (zpowers g)) (λ q (hq : q ∈ finset.univ), hf q),
simpa only [minimal_period_eq_card, finset.prod_pow_eq_pow_sum, fintype.card_sigma,
fintype.card_congr (self_equiv_sigma_orbits (zpowers g) (G ⧸ H)), index_eq_card] using key,
end
lemma transfer_eq_pow [finite_index H] (g : G)
(key : ∀ (k : ℕ) (g₀ : G), g₀⁻¹ * g ^ k * g₀ ∈ H → g₀⁻¹ * g ^ k * g₀ = g ^ k) :
transfer ϕ g = ϕ ⟨g ^ H.index, transfer_eq_pow_aux g key⟩ :=
begin
classical,
letI := H.fintype_quotient_of_finite_index,
change ∀ k g₀ (hk : g₀⁻¹ * g ^ k * g₀ ∈ H), ↑(⟨g₀⁻¹ * g ^ k * g₀, hk⟩ : H) = g ^ k at key,
rw [transfer_eq_prod_quotient_orbit_rel_zpowers_quot, ←finset.prod_to_list, list.prod_map_hom],
refine congr_arg ϕ (subtype.coe_injective _),
rw [H.coe_mk, ←(zpowers g).coe_mk g (mem_zpowers g), ←(zpowers g).coe_pow, (zpowers g).coe_mk,
index_eq_card, fintype.card_congr (self_equiv_sigma_orbits (zpowers g) (G ⧸ H)),
fintype.card_sigma, ←finset.prod_pow_eq_pow_sum, ←finset.prod_to_list],
simp only [coe_list_prod, list.map_map, ←minimal_period_eq_card],
congr' 2,
funext,
apply key,
end
lemma transfer_center_eq_pow [finite_index (center G)] (g : G) :
transfer (monoid_hom.id (center G)) g = ⟨g ^ (center G).index, (center G).pow_index_mem g⟩ :=
transfer_eq_pow (id (center G)) g (λ k _ hk, by rw [←mul_right_inj, hk, mul_inv_cancel_right])
variables (G)
/-- The transfer homomorphism `G →* center G`. -/
noncomputable def transfer_center_pow [finite_index (center G)] : G →* center G :=
{ to_fun := λ g, ⟨g ^ (center G).index, (center G).pow_index_mem g⟩,
map_one' := subtype.ext (one_pow (center G).index),
map_mul' := λ a b, by simp_rw [←show ∀ g, (_ : center G) = _,
from transfer_center_eq_pow, map_mul] }
variables {G}
@[simp] lemma transfer_center_pow_apply [finite_index (center G)] (g : G) :
↑(transfer_center_pow G g) = g ^ (center G).index :=
rfl
section burnside_transfer
variables {p : ℕ} (P : sylow p G) (hP : (P : subgroup G).normalizer ≤ (P : subgroup G).centralizer)
include hP
/-- The homomorphism `G →* P` in Burnside's transfer theorem. -/
noncomputable def transfer_sylow [finite_index (P : subgroup G)] : G →* (P : subgroup G) :=
@transfer G _ P P (@subgroup.is_commutative.comm_group G _ P
⟨⟨λ a b, subtype.ext (hP (le_normalizer b.2) a a.2)⟩⟩) (monoid_hom.id P) _
variables [fact p.prime] [finite (sylow p G)]
/-- Auxillary lemma in order to state `transfer_sylow_eq_pow`. -/
lemma transfer_sylow_eq_pow_aux (g : G) (hg : g ∈ P) (k : ℕ) (g₀ : G) (h : g₀⁻¹ * g ^ k * g₀ ∈ P) :
g₀⁻¹ * g ^ k * g₀ = g ^ k :=
begin
haveI : (P : subgroup G).is_commutative := ⟨⟨λ a b, subtype.ext (hP (le_normalizer b.2) a a.2)⟩⟩,
replace hg := (P : subgroup G).pow_mem hg k,
obtain ⟨n, hn, h⟩ := P.conj_eq_normalizer_conj_of_mem (g ^ k) g₀ hg h,
exact h.trans (commute.inv_mul_cancel (hP hn (g ^ k) hg).symm),
end
variables [finite_index (P : subgroup G)]
lemma transfer_sylow_eq_pow (g : G) (hg : g ∈ P) : transfer_sylow P hP g =
⟨g ^ (P : subgroup G).index, transfer_eq_pow_aux g (transfer_sylow_eq_pow_aux P hP g hg)⟩ :=
by apply transfer_eq_pow
lemma transfer_sylow_restrict_eq_pow :
⇑((transfer_sylow P hP).restrict (P : subgroup G)) = (^ (P : subgroup G).index) :=
funext (λ g, transfer_sylow_eq_pow P hP g g.2)
/-- Burnside's normal p-complement theorem: If `N(P) ≤ C(P)`, then `P` has a normal complement. -/
lemma ker_transfer_sylow_is_complement' : is_complement' (transfer_sylow P hP).ker P :=
begin
have hf : function.bijective ((transfer_sylow P hP).restrict (P : subgroup G)) :=
(transfer_sylow_restrict_eq_pow P hP).symm ▸ (P.2.pow_equiv' (not_dvd_index_sylow P
(mt index_eq_zero_of_relindex_eq_zero index_ne_zero_of_finite))).bijective,
rw [function.bijective, ←range_top_iff_surjective, restrict_range] at hf,
have := range_top_iff_surjective.mp (top_le_iff.mp (hf.2.ge.trans (map_le_range _ P))),
rw [←(comap_injective this).eq_iff, comap_top, comap_map_eq, sup_comm, set_like.ext'_iff,
normal_mul, ←ker_eq_bot_iff, ←(map_injective (P : subgroup G).subtype_injective).eq_iff,
ker_restrict, subgroup_of_map_subtype, subgroup.map_bot, coe_top] at hf,
exact is_complement'_of_disjoint_and_mul_eq_univ (disjoint_iff.2 hf.1) hf.2,
end
lemma not_dvd_card_ker_transfer_sylow : ¬ p ∣ nat.card (transfer_sylow P hP).ker :=
(ker_transfer_sylow_is_complement' P hP).index_eq_card ▸ not_dvd_index_sylow P $
mt index_eq_zero_of_relindex_eq_zero index_ne_zero_of_finite
lemma ker_transfer_sylow_disjoint (Q : subgroup G) (hQ : is_p_group p Q) :
disjoint (transfer_sylow P hP).ker Q :=
disjoint_iff.mpr $ card_eq_one.mp $ (hQ.to_le inf_le_right).card_eq_or_dvd.resolve_right $
λ h, not_dvd_card_ker_transfer_sylow P hP $ h.trans $ nat_card_dvd_of_le _ _ inf_le_left
end burnside_transfer
end monoid_hom
|
ee1c8b3871197e610a6b120a8f944d90a1d735c0 | 0c1546a496eccfb56620165cad015f88d56190c5 | /library/init/algebra/ring.lean | 998b7f1580dc60fd1a5c8758a2f0af10816d9f6b | [
"Apache-2.0"
] | permissive | Solertis/lean | 491e0939957486f664498fbfb02546e042699958 | 84188c5aa1673fdf37a082b2de8562dddf53df3f | refs/heads/master | 1,610,174,257,606 | 1,486,263,620,000 | 1,486,263,620,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,885 | 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
universe variable 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:α))
lemma zero_ne_one [s: zero_ne_one_class α] : 0 ≠ (1:α) :=
@zero_ne_one_class.zero_ne_one α s
/- 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 : α) :=
begin unfold bit0, reflexivity end
lemma ne_zero_of_mul_ne_zero_right {a b : α} (h : a * b ≠ 0) : a ≠ 0 :=
suppose 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 :=
suppose 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 α
/- 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
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_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
suppose 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
(suppose 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
(suppose a - b = 0, or.inl (eq_of_sub_eq_zero this))
(suppose a + b = 0, or.inr (eq_neg_of_add_eq_zero this)))
(suppose a = b ∨ a = -b, or.elim this
(suppose a = b, by rewrite this)
(suppose 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
|
893dcfddefbeab2681d5434920390e84fa1f4b1e | 26ac254ecb57ffcb886ff709cf018390161a9225 | /src/ring_theory/algebra.lean | c5e68d64ab98dab8eecd5b5fbd7a7b068fefe537 | [
"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 | 44,519 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Yury Kudryashov
-/
import data.matrix.basic
import linear_algebra.tensor_product
import ring_theory.subsemiring
/-!
# Algebra over Commutative Semiring (under category)
In this file we define algebra over commutative (semi)rings, algebra homomorphisms `alg_hom`,
algebra equivalences `alg_equiv`, and `subalgebra`s. We also define usual operations on `alg_hom`s
(`id`, `comp`) and subalgebras (`map`, `comap`).
If `S` is an `R`-algebra and `A` is an `S`-algebra then `algebra.comap.algebra R S A` can be used
to provide `A` with a structure of an `R`-algebra. Other than that, `algebra.comap` is now
deprecated and replcaed with `is_algebra_tower`.
## Notations
* `A →ₐ[R] B` : `R`-algebra homomorphism from `A` to `B`.
* `A ≃ₐ[R] B` : `R`-algebra equivalence from `A` to `B`.
-/
noncomputable theory
universes u v w u₁ v₁
open_locale tensor_product big_operators
section prio
-- We set this priority to 0 later in this file
set_option default_priority 200 -- see Note [default priority]
/-- 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. -/
@[nolint has_inhabited_instance]
class algebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A]
extends has_scalar R A, R →+* A :=
(commutes' : ∀ r x, to_fun r * x = x * to_fun r)
(smul_def' : ∀ r x, r • x = to_fun r * x)
end prio
/-- Embedding `R →+* A` given by `algebra` structure. -/
def algebra_map (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] : R →+* A :=
algebra.to_ring_hom
/-- Creating an algebra from a morphism to the center of a semiring. -/
def ring_hom.to_algebra' {R S} [comm_semiring R] [semiring S] (i : R →+* S)
(h : ∀ c x, i c * x = x * i c) :
algebra R S :=
{ smul := λ c x, i c * x,
commutes' := h,
smul_def' := λ c x, rfl,
.. i}
/-- Creating an algebra from a morphism to a commutative semiring. -/
def ring_hom.to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) :
algebra R S :=
i.to_algebra' $ λ _, mul_comm _
namespace algebra
variables {R : Type u} {S : Type v} {A : Type w}
/-- Let `R` be a commutative semiring, let `A` be a semiring with a `semimodule R` structure.
If `(r • 1) * x = x * (r • 1) = r • x` for all `r : R` and `x : A`, then `A` is an `algebra`
over `R`. -/
def of_semimodule' [comm_semiring R] [semiring A] [semimodule R A]
(h₁ : ∀ (r : R) (x : A), (r • 1) * x = r • x)
(h₂ : ∀ (r : R) (x : A), x * (r • 1) = r • x) : algebra R A :=
{ to_fun := λ r, r • 1,
map_one' := one_smul _ _,
map_mul' := λ r₁ r₂, by rw [h₁, mul_smul],
map_zero' := zero_smul _ _,
map_add' := λ r₁ r₂, add_smul r₁ r₂ 1,
commutes' := λ r x, by simp only [h₁, h₂],
smul_def' := λ r x, by simp only [h₁] }
/-- Let `R` be a commutative semiring, let `A` be a semiring with a `semimodule R` structure.
If `(r • x) * y = x * (r • y) = r • (x * y)` for all `r : R` and `x y : A`, then `A`
is an `algebra` over `R`. -/
def of_semimodule [comm_semiring R] [semiring A] [semimodule R A]
(h₁ : ∀ (r : R) (x y : A), (r • x) * y = r • (x * y))
(h₂ : ∀ (r : R) (x y : A), x * (r • y) = r • (x * y)) : algebra R A :=
of_semimodule' (λ r x, by rw [h₁, one_mul]) (λ r x, by rw [h₂, mul_one])
section semiring
variables [comm_semiring R] [comm_semiring S] [semiring A] [algebra R A]
lemma smul_def'' (r : R) (x : A) : r • x = algebra_map R A r * x :=
algebra.smul_def' r x
/--
To prove two algebra structures on a fixed `[comm_semiring R] [semiring A]` agree,
it suffices to check the `algebra_map`s agree.
-/
-- We'll later use this to show `algebra ℤ M` is a subsingleton.
@[ext]
lemma algebra_ext {R : Type*} [comm_semiring R] {A : Type*} [semiring A] (P Q : algebra R A)
(w : ∀ (r : R), by { haveI := P, exact algebra_map R A r } = by { haveI := Q, exact algebra_map R A r }) :
P = Q :=
begin
unfreezingI { rcases P with ⟨⟨P⟩⟩, rcases Q with ⟨⟨Q⟩⟩ },
congr,
{ funext r a,
replace w := congr_arg (λ s, s * a) (w r),
simp only [←algebra.smul_def''] at w,
apply w, },
{ ext r,
exact w r, },
{ apply proof_irrel_heq, },
{ apply proof_irrel_heq, },
end
@[priority 200] -- see Note [lower instance priority]
instance to_semimodule : semimodule R A :=
{ one_smul := by simp [smul_def''],
mul_smul := by simp [smul_def'', mul_assoc],
smul_add := by simp [smul_def'', mul_add],
smul_zero := by simp [smul_def''],
add_smul := by simp [smul_def'', add_mul],
zero_smul := by simp [smul_def''] }
-- from now on, we don't want to use the following instance anymore
attribute [instance, priority 0] algebra.to_has_scalar
lemma smul_def (r : R) (x : A) : r • x = algebra_map R A r * x :=
algebra.smul_def' r x
lemma algebra_map_eq_smul_one (r : R) : algebra_map R A r = r • 1 :=
calc algebra_map R A r = algebra_map R A r * 1 : (mul_one _).symm
... = r • 1 : (algebra.smul_def r 1).symm
theorem commutes (r : R) (x : A) : algebra_map R A r * x = x * algebra_map R A r :=
algebra.commutes' r x
theorem left_comm (r : R) (x y : A) : x * (algebra_map R A r * y) = algebra_map R 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]
variables (R)
instance id : algebra R R := (ring_hom.id R).to_algebra
variables {R}
namespace id
@[simp] lemma map_eq_self (x : R) : algebra_map R R x = x := rfl
@[simp] lemma smul_eq_mul (x y : R) : x • y = x * y := rfl
end id
/-- Algebra over a subsemiring. -/
instance of_subsemiring (S : subsemiring R) : algebra S A :=
{ smul := λ s x, (s : R) • x,
commutes' := λ r x, algebra.commutes r x,
smul_def' := λ r x, algebra.smul_def r x,
.. (algebra_map R A).comp (subsemiring.subtype S) }
/-- Algebra over a subring. -/
instance of_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A]
(S : set R) [is_subring S] : algebra S A :=
{ smul := λ s x, (s : R) • x,
commutes' := λ r x, algebra.commutes r x,
smul_def' := λ r x, algebra.smul_def r x,
.. (algebra_map R A).comp (⟨coe, rfl, λ _ _, rfl, rfl, λ _ _, rfl⟩ : S →+* R) }
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])
/-- The multiplication on the left in an algebra is a linear map. -/
def lmul_left (r : A) : A →ₗ A :=
lmul R A r
/-- The multiplication on the right in an algebra is a linear map. -/
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 semiring
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,
map_one' := one_smul _ _,
map_zero' := zero_smul _ _,
map_add' := λ r₁ r₂, add_smul _ _ _,
map_mul' := λ r₁ r₂, by { ext x, simp [mul_smul] },
commutes' := by { intros, ext, simp },
smul_def' := by { intros, ext, simp } }
instance matrix_algebra (n : Type u) (R : Type v)
[fintype n] [decidable_eq n] [comm_semiring R] : algebra R (matrix n n R) :=
{ commutes' := by { intros, simp [matrix.scalar], },
smul_def' := by { intros, simp [matrix.scalar], },
..(matrix.scalar n) }
set_option old_structure_cmd true
/-- Defining the homomorphism in the category R-Alg. -/
@[nolint has_inhabited_instance]
structure alg_hom (R : Type u) (A : Type v) (B : Type w)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends ring_hom A B :=
(commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r)
run_cmd tactic.add_doc_string `alg_hom.to_ring_hom "Reinterpret an `alg_hom` as a `ring_hom`"
infixr ` →ₐ `:25 := alg_hom _
notation A ` →ₐ[`:25 R `] ` B := alg_hom R A B
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁}
section semiring
variables [comm_semiring R] [semiring A] [semiring B] [semiring C] [semiring D]
variables [algebra R A] [algebra R B] [algebra R C] [algebra R D]
instance : has_coe_to_fun (A →ₐ[R] B) := ⟨_, λ f, f.to_fun⟩
instance coe_ring_hom : has_coe (A →ₐ[R] B) (A →+* B) := ⟨alg_hom.to_ring_hom⟩
instance coe_monoid_hom : has_coe (A →ₐ[R] B) (A →* B) := ⟨λ f, ↑(f : A →+* B)⟩
instance coe_add_monoid_hom : has_coe (A →ₐ[R] B) (A →+ B) := ⟨λ f, ↑(f : A →+* B)⟩
@[simp, norm_cast] lemma coe_mk {f : A → B} (h₁ h₂ h₃ h₄ h₅) :
⇑(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := rfl
@[simp, norm_cast] lemma coe_to_ring_hom (f : A →ₐ[R] B) : ⇑(f : A →+* B) = f := rfl
-- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute.
@[norm_cast] lemma coe_to_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →* B) = f := rfl
-- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute.
@[norm_cast] lemma coe_to_add_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →+ B) = f := rfl
variables (φ : A →ₐ[R] B)
theorem coe_fn_inj ⦃φ₁ φ₂ : A →ₐ[R] B⦄ (H : ⇑φ₁ = φ₂) : φ₁ = φ₂ :=
by { cases φ₁, cases φ₂, congr, exact H }
theorem coe_ring_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+* B)) :=
λ φ₁ φ₂ H, coe_fn_inj $ show ((φ₁ : (A →+* B)) : A → B) = ((φ₂ : (A →+* B)) : A → B),
from congr_arg _ H
theorem coe_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →* B)) :=
ring_hom.coe_monoid_hom_injective.comp coe_ring_hom_injective
theorem coe_add_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+ B)) :=
ring_hom.coe_add_monoid_hom_injective.comp coe_ring_hom_injective
@[ext]
theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ :=
coe_fn_inj $ funext H
theorem ext_iff {φ₁ φ₂ : A →ₐ[R] B} : φ₁ = φ₂ ↔ ∀ x, φ₁ x = φ₂ x :=
⟨by { rintro rfl x, refl }, ext⟩
@[simp]
theorem commutes (r : R) : φ (algebra_map R A r) = algebra_map R B r := φ.commutes' r
theorem comp_algebra_map : φ.to_ring_hom.comp (algebra_map R A) = algebra_map R B :=
ring_hom.ext $ φ.commutes
@[simp] lemma map_add (r s : A) : φ (r + s) = φ r + φ s :=
φ.to_ring_hom.map_add r s
@[simp] lemma map_zero : φ 0 = 0 :=
φ.to_ring_hom.map_zero
@[simp] lemma map_mul (x y) : φ (x * y) = φ x * φ y :=
φ.to_ring_hom.map_mul x y
@[simp] lemma map_one : φ 1 = 1 :=
φ.to_ring_hom.map_one
@[simp] lemma map_smul (r : R) (x : A) : φ (r • x) = r • φ x :=
by simp only [algebra.smul_def, map_mul, commutes]
@[simp] lemma map_pow (x : A) (n : ℕ) : φ (x ^ n) = (φ x) ^ n :=
φ.to_ring_hom.map_pow x n
lemma map_sum {ι : Type*} (f : ι → A) (s : finset ι) :
φ (∑ x in s, f x) = ∑ x in s, φ (f x) :=
φ.to_ring_hom.map_sum f s
@[simp] lemma map_nat_cast (n : ℕ) : φ n = n :=
φ.to_ring_hom.map_nat_cast n
section
variables (R A)
/-- Identity map as an `alg_hom`. -/
protected def id : A →ₐ[R] A :=
{ commutes' := λ _, rfl,
..ring_hom.id A }
end
@[simp] lemma id_apply (p : A) : alg_hom.id R A p = p := rfl
/-- Composition of algebra homeomorphisms. -/
def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ[R] C :=
{ commutes' := λ r : R, by rw [← φ₁.commutes, ← φ₂.commutes]; refl,
.. φ₁.to_ring_hom.comp ↑φ₂ }
@[simp] lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) :
φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl
@[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ :=
ext $ λ x, rfl
@[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ :=
ext $ λ x, rfl
theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) :
(φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) :=
ext $ λ x, rfl
/-- R-Alg ⥤ R-Mod -/
def to_linear_map : A →ₗ B :=
{ to_fun := φ,
map_add' := φ.map_add,
map_smul' := φ.map_smul }
@[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl
theorem to_linear_map_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
@[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
end semiring
section comm_semiring
variables [comm_semiring R] [comm_semiring A] [comm_semiring B]
variables [algebra R A] [algebra R B]
variables (φ : A →ₐ[R] B)
lemma map_prod {ι : Type*} (f : ι → A) (s : finset ι) :
φ (∏ x in s, f x) = ∏ x in s, φ (f x) :=
φ.to_ring_hom.map_prod f s
end comm_semiring
section ring
variables [comm_ring R] [ring A] [ring B] [ring C]
variables [algebra R A] [algebra R B] [algebra R C] (φ : A →ₐ[R] B)
@[simp] lemma map_neg (x) : φ (-x) = -φ x :=
φ.to_ring_hom.map_neg x
@[simp] lemma map_sub (x y) : φ (x - y) = φ x - φ y :=
φ.to_ring_hom.map_sub x y
end ring
end alg_hom
set_option old_structure_cmd true
/-- An equivalence of algebras is an equivalence of rings commuting with the actions of scalars. -/
structure alg_equiv (R : Type u) (A : Type v) (B : Type w)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
extends A ≃ B, A ≃* B, A ≃+ B, A ≃+* B :=
(commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r)
attribute [nolint doc_blame] alg_equiv.to_ring_equiv
attribute [nolint doc_blame] alg_equiv.to_equiv
attribute [nolint doc_blame] alg_equiv.to_add_equiv
attribute [nolint doc_blame] alg_equiv.to_mul_equiv
notation A ` ≃ₐ[`:50 R `] ` A' := alg_equiv R A A'
namespace alg_equiv
variables {R : Type u} {A₁ : Type v} {A₂ : Type w} {A₃ : Type u₁}
variables [comm_semiring R] [semiring A₁] [semiring A₂] [semiring A₃]
variables [algebra R A₁] [algebra R A₂] [algebra R A₃]
instance : has_coe_to_fun (A₁ ≃ₐ[R] A₂) := ⟨_, alg_equiv.to_fun⟩
@[ext]
lemma ext {f g : A₁ ≃ₐ[R] A₂} (h : ∀ a, f a = g a) : f = g :=
begin
have h₁ : f.to_equiv = g.to_equiv := equiv.ext h,
cases f, cases g, congr,
{ exact (funext h) },
{ exact congr_arg equiv.inv_fun h₁ }
end
lemma coe_fun_injective : @function.injective (A₁ ≃ₐ[R] A₂) (A₁ → A₂) (λ e, (e : A₁ → A₂)) :=
begin
intros f g w,
ext,
exact congr_fun w a,
end
instance has_coe_to_ring_equiv : has_coe (A₁ ≃ₐ[R] A₂) (A₁ ≃+* A₂) := ⟨alg_equiv.to_ring_equiv⟩
@[simp] lemma mk_apply {to_fun inv_fun left_inv right_inv map_mul map_add commutes a} :
(⟨to_fun, inv_fun, left_inv, right_inv, map_mul, map_add, commutes⟩ : A₁ ≃ₐ[R] A₂) a = to_fun a :=
rfl
@[simp] lemma to_fun_apply {e : A₁ ≃ₐ[R] A₂} {a : A₁} : e.to_fun a = e a := rfl
@[simp, norm_cast] lemma coe_ring_equiv (e : A₁ ≃ₐ[R] A₂) : ((e : A₁ ≃+* A₂) : A₁ → A₂) = e := rfl
lemma coe_ring_equiv_injective : function.injective (λ e : A₁ ≃ₐ[R] A₂, (e : A₁ ≃+* A₂)) :=
begin
intros f g w,
ext,
replace w : ((f : A₁ ≃+* A₂) : A₁ → A₂) = ((g : A₁ ≃+* A₂) : A₁ → A₂) :=
congr_arg (λ e : A₁ ≃+* A₂, (e : A₁ → A₂)) w,
exact congr_fun w a,
end
@[simp] lemma map_add (e : A₁ ≃ₐ[R] A₂) : ∀ x y, e (x + y) = e x + e y := e.to_add_equiv.map_add
@[simp] lemma map_zero (e : A₁ ≃ₐ[R] A₂) : e 0 = 0 := e.to_add_equiv.map_zero
@[simp] lemma map_mul (e : A₁ ≃ₐ[R] A₂) : ∀ x y, e (x * y) = (e x) * (e y) := e.to_mul_equiv.map_mul
@[simp] lemma map_one (e : A₁ ≃ₐ[R] A₂) : e 1 = 1 := e.to_mul_equiv.map_one
@[simp] lemma commutes (e : A₁ ≃ₐ[R] A₂) : ∀ (r : R), e (algebra_map R A₁ r) = algebra_map R A₂ r :=
e.commutes'
@[simp] lemma map_neg {A₁ : Type v} {A₂ : Type w}
[ring A₁] [ring A₂] [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) :
∀ x, e (-x) = -(e x) := e.to_add_equiv.map_neg
@[simp] lemma map_sub {A₁ : Type v} {A₂ : Type w}
[ring A₁] [ring A₂] [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) :
∀ x y, e (x - y) = e x - e y := e.to_add_equiv.map_sub
lemma map_sum (e : A₁ ≃ₐ[R] A₂) {ι : Type*} (f : ι → A₁) (s : finset ι) :
e (∑ x in s, f x) = ∑ x in s, e (f x) :=
e.to_add_equiv.map_sum f s
instance has_coe_to_alg_hom : has_coe (A₁ ≃ₐ[R] A₂) (A₁ →ₐ[R] A₂) :=
⟨λ e, { map_one' := e.map_one, map_zero' := e.map_zero, ..e }⟩
@[simp, norm_cast] lemma coe_alg_hom (e : A₁ ≃ₐ[R] A₂) : ((e : A₁ →ₐ[R] A₂) : A₁ → A₂) = e :=
rfl
lemma injective (e : A₁ ≃ₐ[R] A₂) : function.injective e := e.to_equiv.injective
lemma surjective (e : A₁ ≃ₐ[R] A₂) : function.surjective e := e.to_equiv.surjective
lemma bijective (e : A₁ ≃ₐ[R] A₂) : function.bijective e := e.to_equiv.bijective
instance : has_one (A₁ ≃ₐ[R] A₁) := ⟨{commutes' := λ r, rfl, ..(1 : A₁ ≃+* A₁)}⟩
instance : inhabited (A₁ ≃ₐ[R] A₁) := ⟨1⟩
/-- Algebra equivalences are reflexive. -/
@[refl]
def refl : A₁ ≃ₐ[R] A₁ := 1
@[simp] lemma coe_refl : (@refl R A₁ _ _ _ : A₁ →ₐ[R] A₁) = alg_hom.id R A₁ :=
alg_hom.ext (λ x, rfl)
/-- Algebra equivalences are symmetric. -/
@[symm]
def symm (e : A₁ ≃ₐ[R] A₂) : A₂ ≃ₐ[R] A₁ :=
{ commutes' := λ r, by { rw ←e.to_ring_equiv.symm_apply_apply (algebra_map R A₁ r), congr,
change _ = e _, rw e.commutes, },
..e.to_ring_equiv.symm, }
@[simp] lemma inv_fun_apply {e : A₁ ≃ₐ[R] A₂} {a : A₂} : e.inv_fun a = e.symm a := rfl
@[simp] lemma symm_symm {e : A₁ ≃ₐ[R] A₂} : e.symm.symm = e :=
by { ext, refl, }
/-- Algebra equivalences are transitive. -/
@[trans]
def trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : A₁ ≃ₐ[R] A₃ :=
{ commutes' := λ r, show e₂.to_fun (e₁.to_fun _) = _, by rw [e₁.commutes', e₂.commutes'],
..(e₁.to_ring_equiv.trans e₂.to_ring_equiv), }
@[simp] lemma apply_symm_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e (e.symm x) = x :=
e.to_equiv.apply_symm_apply
@[simp] lemma symm_apply_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e.symm (e x) = x :=
e.to_equiv.symm_apply_apply
@[simp] lemma trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₁) :
(e₁.trans e₂) x = e₂ (e₁ x) := rfl
@[simp] lemma comp_symm (e : A₁ ≃ₐ[R] A₂) :
alg_hom.comp (e : A₁ →ₐ[R] A₂) ↑e.symm = alg_hom.id R A₂ :=
by { ext, simp }
@[simp] lemma symm_comp (e : A₁ ≃ₐ[R] A₂) :
alg_hom.comp ↑e.symm (e : A₁ →ₐ[R] A₂) = alg_hom.id R A₁ :=
by { ext, simp }
/-- If an algebra morphism has an inverse, it is a algebra isomorphism. -/
def of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ : f.comp g = alg_hom.id R A₂) (h₂ : g.comp f = alg_hom.id R A₁) : A₁ ≃ₐ[R] A₂ :=
{ inv_fun := g,
left_inv := alg_hom.ext_iff.1 h₂,
right_inv := alg_hom.ext_iff.1 h₁,
..f }
end alg_equiv
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`. If `S` is an `R`-algebra and `A` is an `S`-algebra then
`algebra.comap.algebra R S A` can be used to provide `A` with a structure of an `R`-algebra.
Other than that, `algebra.comap` is now deprecated and replcaed with `is_algebra_tower`. -/
/- 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 unused_arguments]
def comap : Type w := A
instance comap.inhabited [h : inhabited A] : inhabited (comap R S A) := h
instance comap.semiring [h : semiring A] : semiring (comap R S A) := h
instance comap.ring [h : ring A] : ring (comap R S A) := h
instance comap.comm_semiring [h : comm_semiring A] : comm_semiring (comap R S A) := h
instance comap.comm_ring [h : comm_ring A] : comm_ring (comap R S A) := h
instance comap.algebra' [comm_semiring S] [semiring A] [h : algebra S A] :
algebra S (comap R S A) := h
/-- Identity homomorphism `A →ₐ[S] comap R S A`. -/
def comap.to_comap [comm_semiring S] [semiring A] [algebra S A] :
A →ₐ[S] comap R S A := alg_hom.id S A
/-- Identity homomorphism `comap R S A →ₐ[S] A`. -/
def comap.of_comap [comm_semiring S] [semiring A] [algebra S A] :
comap R S A →ₐ[S] A := alg_hom.id S A
variables [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A]
/-- `R ⟶ S` induces `S-Alg ⥤ R-Alg` -/
instance comap.algebra : algebra R (comap R S A) :=
{ smul := λ r x, (algebra_map R S r • x : A),
commutes' := λ r x, algebra.commutes _ _,
smul_def' := λ _ _, algebra.smul_def _ _,
.. (algebra_map S A).comp (algebra_map R S) }
/-- Embedding of `S` into `comap R S A`. -/
def to_comap : S →ₐ[R] comap R S A :=
{ commutes' := λ r, rfl,
.. algebra_map S A }
theorem to_comap_apply (x) : to_comap R S A x = algebra_map S A x := rfl
end algebra
namespace alg_hom
variables {R : Type u} {S : Type v} {A : Type w} {B : Type u₁}
variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring 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 R S r)
..φ }
end alg_hom
namespace rat
instance algebra_rat {α} [division_ring α] [char_zero α] : algebra ℚ α :=
(rat.cast_hom α).to_algebra' $ λ r x, r.cast_commute x
end rat
/-- A subalgebra is a subring that includes the range of `algebra_map`. -/
structure subalgebra (R : Type u) (A : Type v)
[comm_semiring R] [semiring A] [algebra R A] : Type v :=
(carrier : subsemiring A)
(algebra_map_mem' : ∀ r, algebra_map R A r ∈ carrier)
namespace subalgebra
variables {R : Type u} {A : Type v} {B : Type w}
variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B]
include R
instance : has_coe (subalgebra R A) (subsemiring A) :=
⟨λ S, S.carrier⟩
instance : has_mem A (subalgebra R A) :=
⟨λ x S, x ∈ (S : set A)⟩
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
theorem ext_iff {S T : subalgebra R A} : S = T ↔ ∀ x : A, x ∈ S ↔ x ∈ T :=
⟨λ h x, by rw h, ext⟩
variables (S : subalgebra R A)
theorem algebra_map_mem (r : R) : algebra_map R A r ∈ S :=
S.algebra_map_mem' r
theorem srange_le : (algebra_map R A).srange ≤ S :=
λ x ⟨r, _, hr⟩, hr ▸ S.algebra_map_mem r
theorem range_subset : set.range (algebra_map R A) ⊆ S :=
λ x ⟨r, hr⟩, hr ▸ S.algebra_map_mem r
theorem range_le : set.range (algebra_map R A) ≤ S :=
S.range_subset
theorem one_mem : (1 : A) ∈ S :=
subsemiring.one_mem S
theorem mul_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x * y ∈ S :=
subsemiring.mul_mem S hx hy
theorem smul_mem {x : A} (hx : x ∈ S) (r : R) : r • x ∈ S :=
(algebra.smul_def r x).symm ▸ S.mul_mem (S.algebra_map_mem r) hx
theorem pow_mem {x : A} (hx : x ∈ S) (n : ℕ) : x ^ n ∈ S :=
subsemiring.pow_mem S hx n
theorem zero_mem : (0 : A) ∈ S :=
subsemiring.zero_mem S
theorem add_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x + y ∈ S :=
subsemiring.add_mem S hx hy
theorem neg_mem {R : Type u} {A : Type v} [comm_ring R] [ring A]
[algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) : -x ∈ S :=
neg_one_smul R x ▸ S.smul_mem hx _
theorem sub_mem {R : Type u} {A : Type v} [comm_ring R] [ring A]
[algebra R A] (S : subalgebra R A) {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x - y ∈ S :=
S.add_mem hx $ S.neg_mem hy
theorem nsmul_mem {x : A} (hx : x ∈ S) (n : ℕ) : n •ℕ x ∈ S :=
subsemiring.nsmul_mem S hx n
theorem gsmul_mem {R : Type u} {A : Type v} [comm_ring R] [ring A]
[algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) (n : ℤ) : n •ℤ x ∈ S :=
int.cases_on n (λ i, S.nsmul_mem hx i) (λ i, S.neg_mem $ S.nsmul_mem hx _)
theorem coe_nat_mem (n : ℕ) : (n : A) ∈ S :=
subsemiring.coe_nat_mem S n
theorem coe_int_mem {R : Type u} {A : Type v} [comm_ring R] [ring A]
[algebra R A] (S : subalgebra R A) (n : ℤ) : (n : A) ∈ S :=
int.cases_on n (λ i, S.coe_nat_mem i) (λ i, S.neg_mem $ S.coe_nat_mem $ i + 1)
theorem list_prod_mem {L : list A} (h : ∀ x ∈ L, x ∈ S) : L.prod ∈ S :=
subsemiring.list_prod_mem S h
theorem list_sum_mem {L : list A} (h : ∀ x ∈ L, x ∈ S) : L.sum ∈ S :=
subsemiring.list_sum_mem S h
theorem multiset_prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A]
[algebra R A] (S : subalgebra R A) {m : multiset A} (h : ∀ x ∈ m, x ∈ S) : m.prod ∈ S :=
subsemiring.multiset_prod_mem S m h
theorem multiset_sum_mem {m : multiset A} (h : ∀ x ∈ m, x ∈ S) : m.sum ∈ S :=
subsemiring.multiset_sum_mem S m h
theorem prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A]
[algebra R A] (S : subalgebra R A) {ι : Type w} {t : finset ι} {f : ι → A}
(h : ∀ x ∈ t, f x ∈ S) : ∏ x in t, f x ∈ S :=
subsemiring.prod_mem S h
theorem sum_mem {ι : Type w} {t : finset ι} {f : ι → A}
(h : ∀ x ∈ t, f x ∈ S) : ∑ x in t, f x ∈ S :=
subsemiring.sum_mem S h
instance {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A]
(S : subalgebra R A) : is_add_submonoid (S : set A) :=
{ zero_mem := S.zero_mem,
add_mem := λ _ _, S.add_mem }
instance {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A]
(S : subalgebra R A) : is_submonoid (S : set A) :=
{ one_mem := S.one_mem,
mul_mem := λ _ _, S.mul_mem }
instance {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) :
is_subring (S : set A) :=
{ neg_mem := λ _, S.neg_mem }
instance : inhabited S := ⟨0⟩
instance (R : Type u) (A : Type v) [comm_semiring R] [semiring A]
[algebra R A] (S : subalgebra R A) : semiring S := subsemiring.to_semiring S
instance (R : Type u) (A : Type v) [comm_semiring R] [comm_semiring A]
[algebra R A] (S : subalgebra R A) : comm_semiring S := subsemiring.to_comm_semiring S
instance (R : Type u) (A : Type v) [comm_ring R] [ring A]
[algebra R A] (S : subalgebra R A) : ring S := @@subtype.ring _ S.is_subring
instance (R : Type u) (A : Type v) [comm_ring R] [comm_ring A]
[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, S.smul_mem x.2 c⟩,
commutes' := λ c x, subtype.eq $ algebra.commutes _ _,
smul_def' := λ c x, subtype.eq $ algebra.smul_def _ _,
.. (algebra_map R A).cod_srestrict S $ λ x, S.range_le ⟨x, rfl⟩ }
instance to_algebra {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A]
[algebra R A] (S : subalgebra R A) : algebra S A :=
algebra.of_subsemiring _
-- todo: standardize on the names these morphisms
-- compare with submodule.subtype
/-- Embedding of a subalgebra into the algebra. -/
def val : S →ₐ[R] A :=
by refine_struct { to_fun := (coe : S → A) }; intros; refl
@[simp]
lemma val_apply (x : S) : S.val x = (x : A) := rfl
/-- Convert a `subalgebra` to `submodule` -/
def to_submodule : submodule R A :=
{ carrier := S,
zero_mem' := (0:S).2,
add_mem' := λ x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2,
smul_mem' := λ c x hx, (algebra.smul_def c x).symm ▸
(⟨algebra_map R 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 {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A]
(S : subalgebra R A) : is_subring ((S : submodule R A) : set A) := S.is_subring
@[simp] lemma mem_to_submodule {x} : x ∈ (S : submodule R A) ↔ x ∈ S := iff.rfl
theorem to_submodule_injective {S U : subalgebra R A} (h : (S : submodule R A) = U) : S = U :=
ext $ λ x, by rw [← mem_to_submodule, ← mem_to_submodule, h]
theorem to_submodule_inj {S U : subalgebra R A} : (S : submodule R A) = U ↔ S = U :=
⟨to_submodule_injective, congr_arg _⟩
instance : partial_order (subalgebra R A) :=
{ le := λ S T, (S : set A) ⊆ (T : set A),
le_refl := λ S, set.subset.refl S,
le_trans := λ _ _ _, set.subset.trans,
le_antisymm := λ S T hst hts, ext $ λ x, ⟨@hst x, @hts x⟩ }
/-- Reinterpret an `S`-subalgebra as an `R`-subalgebra in `comap R S A`. -/
def comap {R : Type u} {S : Type v} {A : Type w}
[comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A]
(iSB : subalgebra S A) : subalgebra R (algebra.comap R S A) :=
{ carrier := (iSB : subsemiring A),
algebra_map_mem' := λ r, iSB.algebra_map_mem (algebra_map R S r) }
/-- If `S` is an `R`-subalgebra of `A` and `T` is an `S`-subalgebra of `A`,
then `T` is an `R`-subalgebra of `A`. -/
def under {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A]
{i : algebra R A} (S : subalgebra R A)
(T : subalgebra S A) : subalgebra R A :=
{ carrier := T,
algebra_map_mem' := λ r, T.algebra_map_mem ⟨algebra_map R A r, S.algebra_map_mem r⟩ }
/-- Transport a subalgebra via an algebra homomorphism. -/
def map (S : subalgebra R A) (f : A →ₐ[R] B) : subalgebra R B :=
{ carrier := subsemiring.map (f : A →+* B) S,
algebra_map_mem' := λ r, f.commutes r ▸ set.mem_image_of_mem _ (S.algebra_map_mem r) }
/-- Preimage of a subalgebra under an algebra homomorphism. -/
def comap' (S : subalgebra R B) (f : A →ₐ[R] B) : subalgebra R A :=
{ carrier := subsemiring.comap (f : A →+* B) S,
algebra_map_mem' := λ r, show f (algebra_map R A r) ∈ S,
from (f.commutes r).symm ▸ S.algebra_map_mem r }
theorem map_le {S : subalgebra R A} {f : A →ₐ[R] B} {U : subalgebra R B} :
map S f ≤ U ↔ S ≤ comap' U f :=
set.image_subset_iff
end subalgebra
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w}
variables [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
variables (φ : A →ₐ[R] B)
/-- Range of an `alg_hom` as a subalgebra. -/
protected def range (φ : A →ₐ[R] B) : subalgebra R B :=
{ carrier :=
{ carrier := set.range φ,
one_mem' := ⟨1, φ.map_one⟩,
mul_mem' := λ _ _ ⟨x, hx⟩ ⟨y, hy⟩, ⟨x * y, by rw [φ.map_mul, hx, hy]⟩,
zero_mem' := ⟨0, φ.map_zero⟩,
add_mem' := λ _ _ ⟨x, hx⟩ ⟨y, hy⟩, ⟨x + y, by rw [φ.map_add, hx, hy]⟩ },
algebra_map_mem' := λ r, ⟨algebra_map R A r, φ.commutes r⟩ }
end alg_hom
namespace algebra
variables (R : Type u) (A : Type v)
variables [comm_semiring R] [semiring A] [algebra R A]
/-- `algebra_map` as an `alg_hom`. -/
def of_id : R →ₐ[R] A :=
{ commutes' := λ _, rfl, .. algebra_map R A }
variables {R}
theorem of_id_apply (r) : of_id R A r = algebra_map R A r := rfl
end algebra
namespace algebra
variables (R : Type u) {A : Type v} {B : Type w}
variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B]
/-- The minimal subalgebra that includes `s`. -/
def adjoin (s : set A) : subalgebra R A :=
{ carrier := subsemiring.closure (set.range (algebra_map R A) ∪ s),
algebra_map_mem' := λ r, subsemiring.subset_closure $ or.inl ⟨r, rfl⟩ }
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 _ _) subsemiring.subset_closure) H,
λ H, subsemiring.closure_le.2 $ set.union_subset S.range_subset H⟩
/-- Galois insertion between `adjoin` and `coe`. -/
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
instance : inhabited (subalgebra R A) := ⟨⊥⟩
theorem mem_bot {x : A} : x ∈ (⊥ : subalgebra R A) ↔ x ∈ set.range (algebra_map 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) :=
subsemiring.subset_closure $ or.inr trivial
theorem eq_top_iff {S : subalgebra R A} :
S = ⊤ ↔ ∀ x : A, x ∈ S :=
⟨λ h x, by rw h; exact mem_top, λ h, by ext x; exact ⟨λ _, mem_top, λ _, h x⟩⟩
@[simp] theorem map_top (f : A →ₐ[R] B) : subalgebra.map (⊤ : subalgebra R A) f = f.range :=
subalgebra.ext $ λ x, ⟨λ ⟨y, _, hy⟩, ⟨y, hy⟩, λ ⟨y, hy⟩, ⟨y, algebra.mem_top, hy⟩⟩
@[simp] theorem map_bot (f : A →ₐ[R] B) : subalgebra.map (⊥ : subalgebra R A) f = ⊥ :=
eq_bot_iff.2 $ λ x ⟨y, hy, hfy⟩, let ⟨r, hr⟩ := mem_bot.1 hy in subalgebra.range_le _
⟨r, by rwa [← f.commutes, hr]⟩
@[simp] theorem comap_top (f : A →ₐ[R] B) : subalgebra.comap' (⊤ : subalgebra R B) f = ⊤ :=
eq_top_iff.2 $ λ x, mem_top
/-- `alg_hom` to `⊤ : subalgebra R A`. -/
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 nat
variables (R : Type*) [semiring R]
/-- Reinterpret a `ring_hom` as an `ℕ`-algebra homomorphism. -/
def alg_hom_nat
{R : Type u} [semiring R] [algebra ℕ R]
{S : Type v} [semiring S] [algebra ℕ S]
(f : R →+* S) : R →ₐ[ℕ] S :=
{ commutes' := λ i, show f _ = _, by simp, .. f }
/-- Semiring ⥤ ℕ-Alg -/
instance algebra_nat : algebra ℕ R :=
{ commutes' := nat.cast_commute,
smul_def' := λ _ _, nsmul_eq_mul _ _,
.. nat.cast_ring_hom R }
variables {R}
/-- A subsemiring is a `ℕ`-subalgebra. -/
def subalgebra_of_subsemiring (S : subsemiring R) : subalgebra ℕ R :=
{ carrier := S,
algebra_map_mem' := λ i, S.coe_nat_mem i }
@[simp] lemma mem_subalgebra_of_subsemiring {x : R} {S : subsemiring R} :
x ∈ subalgebra_of_subsemiring S ↔ x ∈ S :=
iff.rfl
section span_nat
open submodule
lemma span_nat_eq_add_group_closure (s : set R) :
(span ℕ s).to_add_submonoid = add_submonoid.closure s :=
eq.symm $ add_submonoid.closure_eq_of_le subset_span $ λ x hx, span_induction hx
(λ x hx, add_submonoid.subset_closure hx) (add_submonoid.zero_mem _)
(λ _ _, add_submonoid.add_mem _) (λ _ _ _, add_submonoid.nsmul_mem _ ‹_› _)
@[simp] lemma span_nat_eq (s : add_submonoid R) : (span ℕ (s : set R)).to_add_submonoid = s :=
by rw [span_nat_eq_add_group_closure, s.closure_eq]
end span_nat
end nat
section int
variables (R : Type*) [ring R]
/-- Reinterpret a `ring_hom` as a `ℤ`-algebra homomorphism. -/
def alg_hom_int
{R : Type u} [comm_ring R] [algebra ℤ R]
{S : Type v} [comm_ring S] [algebra ℤ S]
(f : R →+* S) : R →ₐ[ℤ] S :=
{ commutes' := λ i, show f _ = _, by simp, .. f }
/-- Ring ⥤ ℤ-Alg -/
instance algebra_int : algebra ℤ R :=
{ commutes' := int.cast_commute,
smul_def' := λ _ _, gsmul_eq_mul _ _,
.. int.cast_ring_hom R }
/--
Promote a ring homomorphisms to a `ℤ`-algebra homomorphism.
-/
def ring_hom.to_int_alg_hom {R S : Type*} [ring R] [ring S] (f : R →+* S) : R →ₐ[ℤ] S :=
{ commutes' := λ n, by simp,
.. f }
variables {R}
/-- A subring is a `ℤ`-subalgebra. -/
def subalgebra_of_subring (S : set R) [is_subring S] : subalgebra ℤ R :=
{ carrier :=
{ carrier := S,
one_mem' := is_submonoid.one_mem,
mul_mem' := λ _ _, is_submonoid.mul_mem,
zero_mem' := is_add_submonoid.zero_mem,
add_mem' := λ _ _, is_add_submonoid.add_mem, },
algebra_map_mem' := λ i, int.induction_on i (show (0 : R) ∈ S, from is_add_submonoid.zero_mem)
(λ i ih, show (i + 1 : R) ∈ S, from is_add_submonoid.add_mem ih is_submonoid.one_mem)
(λ i ih, show ((-i - 1 : ℤ) : R) ∈ S, by { rw [int.cast_sub, int.cast_one],
exact is_add_subgroup.sub_mem S _ _ ih is_submonoid.one_mem }) }
section
variables {S : Type*} [ring S]
instance int_algebra_subsingleton : subsingleton (algebra ℤ S) :=
⟨λ P Q, by { ext, simp, }⟩
end
section
variables {S : Type*} [semiring S]
instance nat_algebra_subsingleton : subsingleton (algebra ℕ S) :=
⟨λ P Q, by { ext, simp, }⟩
end
@[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).to_add_subgroup = add_subgroup.closure s :=
eq.symm $ add_subgroup.closure_eq_of_le _ subset_span $ λ x hx, span_induction hx
(λ x hx, add_subgroup.subset_closure hx) (add_subgroup.zero_mem _)
(λ _ _, add_subgroup.add_mem _) (λ _ _ _, add_subgroup.gsmul_mem _ ‹_› _)
@[simp] lemma span_int_eq (s : add_subgroup R) : (span ℤ (s : set R)).to_add_subgroup = s :=
by rw [span_int_eq_add_group_closure, s.closure_eq]
end span_int
end int
section restrict_scalars
/- In this section, we describe restriction of scalars: if `S` is an algebra over `R`, then
`S`-modules are also `R`-modules. -/
variables (R : Type*) [comm_ring R] (S : Type*) [ring S] [algebra R S]
variables (E : Type*) [add_comm_group E] [module S E] {F : Type*} [add_comm_group F] [module S F]
/--
When `E` is a module over a ring `S`, and `S` is an algebra over `R`, then `E` inherits a
module structure over `R`, called `module.restrict_scalars' R S E`.
We do not register this as an instance as `S` can not be inferred.
-/
def module.restrict_scalars' : module R E :=
{ smul := λ c x, (algebra_map R S c) • x,
one_smul := by simp,
mul_smul := by simp [mul_smul],
smul_add := by simp [smul_add],
smul_zero := by simp [smul_zero],
add_smul := by simp [add_smul],
zero_smul := by simp [zero_smul] }
/--
When `E` is a module over a ring `S`, and `S` is an algebra over `R`, then `E` inherits a
module structure over `R`, provided as a type synonym `module.restrict_scalars R S E := E`.
-/
@[nolint unused_arguments]
def module.restrict_scalars (R : Type*) (S : Type*) (E : Type*) : Type* := E
instance (R : Type*) (S : Type*) (E : Type*) [I : inhabited E] :
inhabited (module.restrict_scalars R S E) := I
instance (R : Type*) (S : Type*) (E : Type*) [I : add_comm_group E] :
add_comm_group (module.restrict_scalars R S E) := I
instance : module R (module.restrict_scalars R S E) :=
(module.restrict_scalars' R S E : module R E)
lemma module.restrict_scalars_smul_def (c : R) (x : module.restrict_scalars R S E) :
c • x = ((algebra_map R S c) • x : E) := rfl
/--
`module.restrict_scalars R S S` is `R`-linearly equivalent to the original algebra `S`.
Unfortunately these structures are not generally definitionally equal:
the `R`-module structure on `S` is part of the data of `S`,
while the `R`-module structure on `module.restrict_scalars R S S`
comes from the ring homomorphism `R →+* S`, which is a separate part of the data of `S`.
The field `algebra.smul_def'` gives the equation we need here.
-/
def algebra.restrict_scalars_equiv :
(module.restrict_scalars R S S) ≃ₗ[R] S :=
{ to_fun := λ s, s,
inv_fun := λ s, s,
left_inv := λ s, rfl,
right_inv := λ s, rfl,
map_add' := λ x y, rfl,
map_smul' := λ c x, (algebra.smul_def' _ _).symm, }
@[simp]
lemma algebra.restrict_scalars_equiv_apply (s : S) :
algebra.restrict_scalars_equiv R S s = s := rfl
@[simp]
lemma algebra.restrict_scalars_equiv_symm_apply (s : S) :
(algebra.restrict_scalars_equiv R S).symm s = s := rfl
variables {S E}
open module
/--
`V.restrict_scalars R` is the `R`-submodule of the `R`-module given by restriction of scalars,
corresponding to `V`, an `S`-submodule of the original `S`-module.
-/
@[simps]
def submodule.restrict_scalars (V : submodule S E) : submodule R (restrict_scalars R S E) :=
{ carrier := V.carrier,
zero_mem' := V.zero_mem,
smul_mem' := λ c e h, V.smul_mem _ h,
add_mem' := λ x y hx hy, V.add_mem hx hy, }
@[simp]
lemma submodule.restrict_scalars_mem (V : submodule S E) (e : E) :
e ∈ V.restrict_scalars R ↔ e ∈ V :=
iff.refl _
@[simp]
lemma submodule.restrict_scalars_bot :
submodule.restrict_scalars R (⊥ : submodule S E) = ⊥ :=
rfl
@[simp]
lemma submodule.restrict_scalars_top :
submodule.restrict_scalars R (⊤ : submodule S E) = ⊤ :=
rfl
/-- The `R`-linear map induced by an `S`-linear map when `S` is an algebra over `R`. -/
def linear_map.restrict_scalars (f : E →ₗ[S] F) :
(restrict_scalars R S E) →ₗ[R] (restrict_scalars R S F) :=
{ to_fun := f.to_fun,
map_add' := λx y, f.map_add x y,
map_smul' := λc x, f.map_smul (algebra_map R S c) x }
@[simp, norm_cast squash] lemma linear_map.coe_restrict_scalars_eq_coe (f : E →ₗ[S] F) :
(f.restrict_scalars R : E → F) = f := rfl
@[simp]
lemma restrict_scalars_ker (f : E →ₗ[S] F) :
(f.restrict_scalars R).ker = submodule.restrict_scalars R f.ker :=
rfl
variables (𝕜 : Type*) [field 𝕜] (𝕜' : Type*) [field 𝕜'] [algebra 𝕜 𝕜']
variables (W : Type*) [add_comm_group W] [vector_space 𝕜' W]
/--
`V.restrict_scalars 𝕜` is the `𝕜`-subspace of the `𝕜`-vector space given by restriction of scalars,
corresponding to `V`, a `𝕜'`-subspace of the original `𝕜'`-vector space.
-/
def subspace.restrict_scalars (V : subspace 𝕜' W) : subspace 𝕜 (restrict_scalars 𝕜 𝕜' W) :=
{ ..submodule.restrict_scalars 𝕜 (V : submodule 𝕜' W) }
end restrict_scalars
/-!
When `V` and `W` are `S`-modules, for some `R`-algebra `S`,
the collection of `S`-linear maps from `V` to `W` forms an `R`-module.
(But not generally an `S`-module, because `S` may be non-commutative.)
-/
section module_of_linear_maps
variables (R : Type*) [comm_ring R] (S : Type*) [ring S] [algebra R S]
(V : Type*) [add_comm_group V] [module S V]
(W : Type*) [add_comm_group W] [module S W]
/--
For `r : R`, and `f : V →ₗ[S] W` (where `S` is an `R`-algebra) we define
`(r • f) v = f (r • v)`.
-/
def linear_map_algebra_has_scalar : has_scalar R (V →ₗ[S] W) :=
{ smul := λ r f,
{ to_fun := λ v, f ((algebra_map R S r) • v),
map_add' := λ x y, by simp [smul_add],
map_smul' := λ s v, by simp [smul_smul, algebra.commutes], } }
local attribute [instance] linear_map_algebra_has_scalar
/-- The `R`-module structure on `S`-linear maps, for `S` an `R`-algebra. -/
def linear_map_algebra_module : module R (V →ₗ[S] W) :=
{ one_smul := λ f, begin ext v, dsimp [(•)], simp, end,
mul_smul := λ r r' f,
begin
ext v, dsimp [(•)],
rw [linear_map.map_smul, linear_map.map_smul, linear_map.map_smul, ring_hom.map_mul,
smul_smul, algebra.commutes],
end,
smul_zero := λ r, by { ext v, dsimp [(•)], refl, },
smul_add := λ r f g, by { ext v, dsimp [(•)], simp [linear_map.map_add], },
zero_smul := λ f, by { ext v, dsimp [(•)], simp, },
add_smul := λ r r' f, by { ext v, dsimp [(•)], simp [add_smul], }, }
local attribute [instance] linear_map_algebra_module
variables {R S V W}
@[simp]
lemma linear_map_algebra_module.smul_apply (c : R) (f : V →ₗ[S] W) (v : V) :
(c • f) v = (c • (f v) : module.restrict_scalars R S W) :=
begin
erw [linear_map.map_smul],
refl,
end
end module_of_linear_maps
|
a99165a1216bc084d47b6353393c79eadff9ec0d | 4727251e0cd73359b15b664c3170e5d754078599 | /src/tactic/localized.lean | f33bf775deffe6478f400043ced578c9ae933d10 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 4,567 | lean | /-
Copyright (c) 2019 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import meta.rb_map
import tactic.core
/-!
# Localized notation
This consists of two user-commands which allow you to declare notation and commands localized to a
locale.
See the tactic doc entry below for more information.
The code is inspired by code from Gabriel Ebner from the
[hott3 repository](https://github.com/gebner/hott3).
-/
open lean lean.parser interactive tactic native
@[user_attribute]
meta def localized_attr : user_attribute (rb_lmap name string) unit :=
{ name := "_localized",
descr := "(interal) attribute that flags localized commands",
parser := failed,
cache_cfg := ⟨λ ns, (do dcls ← ns.mmap (λ n, mk_const n >>= eval_expr (name × string)),
return $ rb_lmap.of_list dcls), []⟩ }
/-- Get all commands in the given locale and return them as a list of strings -/
meta def get_localized (ns : list name) : tactic (list string) :=
do m ← localized_attr.get_cache,
ns.mfoldl (λ l nm, match m.find nm with
| [] := fail format!"locale {nm} does not exist"
| new_l := return $ l.append new_l
end) []
/-- Execute all commands in the given locale -/
@[user_command] meta def open_locale_cmd (_ : parse $ tk "open_locale") : parser unit :=
do ns ← many ident,
cmds ← get_localized ns,
cmds.mmap' emit_code_here
/-- Add a new command to a locale and execute it right now.
The new command is added as a declaration to the environment with name `_localized_decl.<number>`.
This declaration has attribute `_localized` and as value a name-string pair. -/
@[user_command] meta def localized_cmd (_ : parse $ tk "localized") : parser unit :=
do cmd ← parser.pexpr, cmd ← i_to_expr cmd, cmd ← eval_expr string cmd,
let cmd := "local " ++ cmd,
emit_code_here cmd,
tk "in",
nm ← ident,
env ← get_env,
let dummy_decl_name := mk_num_name `_localized_decl
((string.hash (cmd ++ nm.to_string) + env.fingerprint) % unsigned_sz),
add_decl (declaration.defn dummy_decl_name [] `(name × string)
(reflect (⟨nm, cmd⟩ : name × string)) (reducibility_hints.regular 1 tt) ff),
localized_attr.set dummy_decl_name unit.star tt
/--
This consists of two user-commands which allow you to declare notation and commands localized to a
locale.
* Declare notation which is localized to a locale using:
```lean
localized "infix ` ⊹ `:60 := my_add" in my.add
```
* After this command it will be available in the same section/namespace/file, just as if you wrote
`local infix ` ⊹ `:60 := my_add`
* You can open it in other places. The following command will declare the notation again as local
notation in that section/namespace/file:
```lean
open_locale my.add
```
* More generally, the following will declare all localized notation in the specified locales.
```lean
open_locale locale1 locale2 ...
```
* You can also declare other localized commands, like local attributes
```lean
localized "attribute [simp] le_refl" in le
```
* To see all localized commands in a given locale, run:
```lean
run_cmd print_localized_commands [`my.add].
```
* To see a list of all locales with localized commands, run:
```lean
run_cmd do
m ← localized_attr.get_cache,
tactic.trace m.keys -- change to `tactic.trace m.to_list` to list all the commands in each locale
```
* Warning: You have to give full names of all declarations used in localized notation,
so that the localized notation also works when the appropriate namespaces are not opened.
-/
add_tactic_doc
{ name := "localized notation",
category := doc_category.cmd,
decl_names := [`localized_cmd, `open_locale_cmd],
tags := ["notation", "type classes"] }
/-- Print all commands in a given locale -/
meta def print_localized_commands (ns : list name) : tactic unit :=
do cmds ← get_localized ns, cmds.mmap' trace
-- you can run `open_locale classical` to get the decidability of all propositions, and downgrade
-- the priority of decidability instances that make Lean run through all the algebraic hierarchy
-- whenever it wants to solve a decidability question
localized "attribute [instance, priority 9] classical.prop_decidable" in classical
localized "attribute [instance, priority 8] eq.decidable decidable_eq_of_decidable_le" in classical
localized "postfix `?`:9001 := optional" in parser
localized "postfix *:9001 := lean.parser.many" in parser
|
b0bee12a8e4508c9314f9f06b39a4da2d27a9734 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /src/Lean/Meta/CollectFVars.lean | 7ebaab9772b4193befb9f86e1fca59073077ac6a | [
"Apache-2.0"
] | permissive | WojciechKarpiel/lean4 | 7f89706b8e3c1f942b83a2c91a3a00b05da0e65b | f6e1314fa08293dea66a329e05b6c196a0189163 | refs/heads/master | 1,686,633,402,214 | 1,625,821,189,000 | 1,625,821,258,000 | 384,640,886 | 0 | 0 | Apache-2.0 | 1,625,903,617,000 | 1,625,903,026,000 | null | UTF-8 | Lean | false | false | 1,402 | 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
-/
import Lean.Util.CollectFVars
import Lean.Elab.Term
namespace Lean.Elab.Term
open Meta
def collectUsedFVars (e : Expr) : StateRefT CollectFVars.State MetaM Unit := do
let e ← instantiateMVars e
modify fun used => collectFVars used e
def collectUsedFVarsAtFVars (fvars : Array Expr) : StateRefT CollectFVars.State MetaM Unit :=
fvars.forM fun fvar => do
let fvarType ← inferType fvar
collectUsedFVars fvarType
def removeUnused (vars : Array Expr) (used : CollectFVars.State) : MetaM (LocalContext × LocalInstances × Array Expr) := do
let localInsts ← getLocalInstances
let lctx ← getLCtx
let (lctx, localInsts, newVars, _) ← vars.foldrM
(fun var (result : LocalContext × LocalInstances × Array Expr × CollectFVars.State) => do
let (lctx, localInsts, newVars, used) := result
if used.fvarSet.contains var.fvarId! then
let varType ← inferType var
let (_, used) ← (collectUsedFVars varType).run used
pure (lctx, localInsts, newVars.push var, used)
else
pure (lctx.erase var.fvarId!, localInsts.erase var.fvarId!, newVars, used))
(lctx, localInsts, #[], used)
pure (lctx, localInsts, newVars.reverse)
end Lean.Elab.Term
|
9c2d7fed9d3ad02c938f9edd4818678f306ad60a | 99b5e6372af1f404777312358869f95be7de84a3 | /src/hott/types/equiv.lean | 994fb72559d8ab689160d31cef34b7d3dbaebd7e | [
"Apache-2.0"
] | permissive | forked-from-1kasper/hott3 | 8fa064ab5e8c9d6752a783d74ab226ddc5b5232a | 2db24de7a361a7793b0eae4ca5c3fd4d4a0fc691 | refs/heads/master | 1,584,867,131,028 | 1,530,766,841,000 | 1,530,766,841,000 | 139,797,034 | 0 | 0 | Apache-2.0 | 1,530,766,961,000 | 1,530,766,961,000 | null | UTF-8 | Lean | false | false | 13,738 | 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
Ported from Coq HoTT
Theorems about the types equiv and is_equiv
-/
import .fiber .arrow ..arity ..prop_trunc ..cubical.square .pointed
universes u v w
hott_theory
namespace hott
open is_trunc hott.sigma hott.pi fiber hott.equiv
namespace is_equiv
variables {A : Type _} {B : Type _} (f : A → B) [H : is_equiv f]
include H
/- is_equiv f is a mere proposition -/
@[hott, instance] def is_contr_fiber_of_is_equiv (b : B) : is_contr (fiber f b) :=
is_contr.mk
(fiber.mk (f⁻¹ᶠ b) (right_inv f b))
(λz, fiber.rec_on z (λa p,
fiber_eq ((ap f⁻¹ᶠ p)⁻¹ ⬝ left_inv f a) (calc
right_inv f b = (ap (f ∘ f⁻¹ᶠ) p)⁻¹ ⬝ ((ap (f ∘ f⁻¹ᶠ) p) ⬝ right_inv f b)
: by rwr inv_con_cancel_left
... = (ap (f ∘ f⁻¹ᶠ) p)⁻¹ ⬝ (right_inv f (f a) ⬝ p) : by sorry --rwr ap_con_eq_con
... = (ap (f ∘ f⁻¹ᶠ) p)⁻¹ ⬝ (ap f (left_inv f a) ⬝ p) : by rwr [adj f]
... = (ap (f ∘ f⁻¹ᶠ) p)⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rwr con.assoc
... = (ap f (ap f⁻¹ᶠ p))⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rwr ap_compose
... = ap f (ap f⁻¹ᶠ p)⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rwr ap_inv
... = ap f ((ap f⁻¹ᶠ p)⁻¹ ⬝ left_inv f a) ⬝ p : by rwr ap_con)))
@[hott] def is_contr_right_inverse : is_contr (Σ(g : B → A), f ∘ g ~ id) :=
begin
fapply is_trunc_equiv_closed,
{exact Σ(g : B → A), f ∘ g = id},
{apply sigma_equiv_sigma_right, intro g, apply eq_equiv_homotopy},
fapply is_trunc_equiv_closed,
{exact fiber (λ(g : B → A), f ∘ g) id},
{apply fiber.sigma_char},
have : is_equiv (λ(g : B → A), f ∘ g) :=
(to_is_equiv (arrow_equiv_arrow_right B (equiv.mk f H))),
exactI is_contr_fiber_of_is_equiv _ _
end
@[hott] def is_contr_right_coherence (u : Σ(g : B → A), f ∘ g ~ id)
: is_contr (Σ(η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a)) :=
begin
fapply is_trunc_equiv_closed_rev -2
(sigma_pi_equiv_pi_sigma (λa (ηa : (u.1 ∘ f) a = a), u.2 (f a) = ap f ηa)),
fapply is_trunc_equiv_closed -2,
{apply pi_equiv_pi_right, intro a,
apply (fiber_eq_equiv (fiber.mk (u.1 (f a)) (u.2 (f a))) (fiber.mk a idp))},
apply_instance
end
omit H
@[hott] protected def sigma_char : (is_equiv f) ≃
(Σ(g : B → A) (ε : f ∘ g ~ id) (η : g ∘ f ~ id), Π(a : A), ε (f a) = ap f (η a)) :=
equiv.MK (λH, by exactI ⟨inv f, right_inv f, left_inv f, adj f⟩)
(λp, is_equiv.mk f p.1 p.2.1 p.2.2.1 p.2.2.2)
(λp, begin
induction p with p1 p2,
induction p2 with p21 p22,
induction p22 with p221 p222,
reflexivity
end)
(λH, by induction H; reflexivity)
@[hott] protected def sigma_char' : (is_equiv f) ≃
(Σ(u : Σ(g : B → A), f ∘ g ~ id) (η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a)) :=
calc
(is_equiv f) ≃
(Σ(g : B → A) (ε : f ∘ g ~ id) (η : g ∘ f ~ id), Π(a : A), ε (f a) = ap f (η a))
: is_equiv.sigma_char _
... ≃ (Σ(u : Σ(g : B → A), f ∘ g ~ id), Σ(η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a))
: sigma_assoc_equiv (λ(u : Σ(g : B → A), f ∘ g ~ id), Σ(η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a))
local attribute [instance] [priority 1600] is_contr_right_inverse
local attribute [instance] [priority 1600] is_contr_right_coherence
@[hott, instance] theorem is_prop_is_equiv : is_prop (is_equiv f) :=
begin
apply is_prop_of_imp_is_contr,
intro H, apply is_trunc_equiv_closed_rev -2 (is_equiv.sigma_char' _),
exactI is_trunc_sigma _ _,
end
@[hott] def inv_eq_inv {A B : Type _} {f f' : A → B} {Hf : is_equiv f} {Hf' : is_equiv f'}
(p : f = f') : f⁻¹ᶠ = f'⁻¹ᶠ :=
apd011 inv p (is_prop.elimo _ _ _)
/- contractible fibers -/
@[hott] def is_contr_fun_of_is_equiv [H : is_equiv f] : is_contr_fun f :=
is_contr_fiber_of_is_equiv f
@[hott] def is_prop_is_contr_fun (f : A → B) : is_prop (is_contr_fun f) := by apply_instance
@[hott] def is_equiv_of_is_contr_fun (f : A → B) [H : is_contr_fun f] : is_equiv f :=
adjointify _ (λb, point (center (fiber f b)))
(λb, point_eq (center (fiber f b)))
(λa, ap point (center_eq (fiber.mk a idp)))
@[hott] def is_equiv_of_imp_is_equiv (H : B → is_equiv f) : is_equiv f :=
@is_equiv_of_is_contr_fun _ _ f (λb, @is_contr_fiber_of_is_equiv _ _ _ (H b) _)
@[hott] def is_equiv_equiv_is_contr_fun (f : A → B) : is_equiv f ≃ is_contr_fun f :=
begin
apply equiv_of_is_prop,
intro H, exactI is_contr_fun_of_is_equiv f,
intro H, exactI is_equiv_of_is_contr_fun f,
end
@[hott] theorem inv_commute'_fn {A : Type _} {B C : A → Type _} (f : Π{a}, B a → C a) [H : Πa, is_equiv (@f a)]
{g : A → A} (h : Π{a}, B a → B (g a)) (h' : Π{a}, C a → C (g a))
(p : Π⦃a : A⦄ (b : B a), f (h b) = h' (f b)) {a : A} (b : B a) :
inv_commute' @f @h @h' p (f b)
= (ap f⁻¹ᶠ (p b))⁻¹ ⬝ left_inv f (h b) ⬝ (ap h (left_inv f b))⁻¹ :=
sorry
-- begin
-- rwr [↑[inv_commute',eq_of_fn_eq_fn'],+ap_con,-adj_inv f,+con.assoc,inv_con_cancel_left,
-- adj f,+ap_inv,-+ap_compose,
-- eq_bot_of_square (natural_square_tr (λb, (left_inv f (h b))⁻¹ ⬝ ap f⁻¹ (p b)) (left_inv f b))⁻¹ʰ,
-- con_inv,inv_inv,+con.assoc],
-- do 3 apply whisker_left,
-- rwr [con_inv_cancel_left,con.left_inv]
-- end
end is_equiv
/- Moving equivalences around in homotopies -/
namespace is_equiv
variables {A : Type _} {B : Type _} {C : Type _} (f : A → B) [Hf : is_equiv f]
include Hf
section pre_compose
variables (α : A → C) (β : B → C)
-- homotopy_inv_of_homotopy_pre is in init.equiv
@[hott] protected def inv_homotopy_of_homotopy_pre.is_equiv
: is_equiv (inv_homotopy_of_homotopy_pre f α β) :=
sorry
-- adjointify _ (homotopy_of_inv_homotopy_pre f α β)
-- begin abstract {
-- intro q, apply eq_of_homotopy, intro b,
-- unfold inv_homotopy_of_homotopy_pre,
-- unfold homotopy_of_inv_homotopy_pre,
-- apply inverse, apply eq_bot_of_square,
-- apply eq_hconcat (ap02 α (adj_inv f b)),
-- apply eq_hconcat (ap_compose α f⁻¹ (right_inv f b))⁻¹,
-- apply natural_square q (right_inv f b)
-- } end
-- begin abstract {
-- intro p, apply eq_of_homotopy, intro a,
-- unfold inv_homotopy_of_homotopy_pre,
-- unfold homotopy_of_inv_homotopy_pre,
-- apply trans (con.assoc
-- (ap α (left_inv f a))⁻¹
-- (p (f⁻¹ (f a)))
-- (ap β (right_inv f (f a))))⁻¹,
-- apply inverse, apply eq_bot_of_square,
-- refine hconcat_eq _ (ap02 β (adj f a))⁻¹,
-- refine hconcat_eq _ (ap_compose β f (left_inv f a)),
-- apply natural_square p (left_inv f a)
-- } end
end pre_compose
section post_compose
variables (α : C → A) (β : C → B)
-- homotopy_inv_of_homotopy_post is in init.equiv
@[hott] protected def inv_homotopy_of_homotopy_post.is_equiv
: is_equiv (inv_homotopy_of_homotopy_post f α β) :=
sorry
-- adjointify _ (homotopy_of_inv_homotopy_post f α β)
-- begin abstract {
-- intro q, apply eq_of_homotopy, intro c,
-- unfold inv_homotopy_of_homotopy_post,
-- unfold homotopy_of_inv_homotopy_post,
-- apply trans (whisker_right (left_inv f (α c))
-- (ap_con f⁻¹ (right_inv f (β c))⁻¹ (ap f (q c))
-- ⬝ whisker_right (ap f⁻¹ (ap f (q c)))
-- (ap_inv f⁻¹ (right_inv f (β c))))),
-- apply inverse, apply eq_bot_of_square,
-- apply eq_hconcat (adj_inv f (β c))⁻¹,
-- apply eq_vconcat (ap_compose f⁻¹ f (q c))⁻¹,
-- refine vconcat_eq _ (ap_id (q c)),
-- apply natural_square_tr (left_inv f) (q c)
-- } end
-- begin abstract {
-- intro p, apply eq_of_homotopy, intro c,
-- unfold inv_homotopy_of_homotopy_post,
-- unfold homotopy_of_inv_homotopy_post,
-- apply trans (whisker_left (right_inv f (β c))⁻¹
-- (ap_con f (ap f⁻¹ (p c)) (left_inv f (α c)))),
-- apply trans (con.assoc (right_inv f (β c))⁻¹ (ap f (ap f⁻¹ (p c)))
-- (ap f (left_inv f (α c))))⁻¹,
-- apply inverse, apply eq_bot_of_square,
-- refine hconcat_eq _ (adj f (α c)),
-- apply eq_vconcat (ap_compose f f⁻¹ (p c))⁻¹,
-- refine vconcat_eq _ (ap_id (p c)),
-- apply natural_square_tr (right_inv f) (p c)
-- } end
end post_compose
end is_equiv
namespace is_equiv
/- @[hott] theorem 4.7.7 -/
variables {A : Type _} {P : A → Type _} {Q : A → Type _}
variable (f : Πa, P a → Q a)
@[hott, reducible] def is_fiberwise_equiv := Πa, is_equiv (f a)
@[hott] def is_equiv_total_of_is_fiberwise_equiv [H : is_fiberwise_equiv f] : is_equiv (total f) :=
is_equiv_sigma_functor id f
@[hott] def is_fiberwise_equiv_of_is_equiv_total [H : is_equiv (total f)]
: is_fiberwise_equiv f :=
begin
intro a,
have : is_contr_fun (f a),
{ intro q, apply is_trunc_equiv_closed, exact (fiber_total_equiv f q), apply_instance },
exactI is_equiv_of_is_contr_fun _,
end
end is_equiv
namespace equiv
open hott.is_equiv
variables {A : Type _} {B : Type _} {C : Type _}
@[hott] def equiv_mk_eq {f f' : A → B} [H : is_equiv f] [H' : is_equiv f'] (p : f = f')
: equiv.mk f H = equiv.mk f' H' :=
apd011 equiv.mk p (is_prop.elimo _ _ _)
@[hott] def equiv_eq' {f f' : A ≃ B} (p : to_fun f = to_fun f') : f = f' :=
by induction f; induction f'; apply (equiv_mk_eq p)
@[hott] def equiv_eq {f f' : A ≃ B} (p : to_fun f ~ to_fun f') : f = f' :=
by apply equiv_eq'; apply eq_of_homotopy p
@[hott] def trans_symm (f : A ≃ B) (g : B ≃ C) : (f ⬝e g)⁻¹ᵉ = g⁻¹ᵉ ⬝e f⁻¹ᵉ :> (C ≃ A) :=
equiv_eq' idp
@[hott] def symm_symm (f : A ≃ B) : f⁻¹ᵉ⁻¹ᵉ = f :> (A ≃ B) :=
equiv_eq' idp
@[hott] protected def equiv.sigma_char
(A : Type u) (B : Type v) : (A ≃ B) ≃ Σ(f : A → B), is_equiv f :=
begin
fapply equiv.MK,
{intro F, exact ⟨to_fun F, to_is_equiv F⟩},
{intro p, cases p with f H, exact (equiv.mk f H)},
{intro p, cases p, exact idp},
{intro F, cases F, exact idp},
end
@[hott] def equiv_eq_char (f f' : A ≃ B) : (f = f') ≃ (to_fun f = to_fun f') :=
calc
(f = f') ≃ (to_fun (equiv.sigma_char _ _) f = to_fun (equiv.sigma_char _ _) f')
: eq_equiv_fn_eq (to_fun (equiv.sigma_char _ _)) _ _
... ≃ ((to_fun (equiv.sigma_char _ _) f).1 = (to_fun (equiv.sigma_char _ _) f').1 ) : (equiv_subtype _ _)⁻¹ᵉ
... ≃ (to_fun f = to_fun f') : equiv.rfl
@[hott] def is_equiv_ap_to_fun (f f' : A ≃ B)
: is_equiv (ap to_fun : f = f' → to_fun f = to_fun f') :=
begin
fapply adjointify,
{intro p, cases f with f H, cases f' with f' H', cases p, apply ap (mk f), apply is_prop.elim},
{intro p, cases f with f H, cases f' with f' H', cases p,
apply @concat _ _ (ap to_fun (ap (equiv.mk f) (is_prop.elim H H'))), {apply idp},
hgeneralize : is_prop.elim H H' = q, cases q, apply idp},
{intro p, cases p, cases f with f H, dsimp, refine ap02 (equiv.mk f) (is_set.elim _ idp) }
end
@[hott] def equiv_pathover {A : Type _} {a a' : A} (p : a = a')
{B : A → Type _} {C : A → Type _} (f : B a ≃ C a) (g : B a' ≃ C a')
(r : Π(b : B a) (b' : B a') (q : b =[p] b'), f b =[p] g b') : f =[p; λa, B a ≃ C a] g :=
begin
refine pathover_of_fn_pathover_fn (λa, equiv.sigma_char _ _) _,
fapply sigma_pathover, apply arrow_pathover, exact r, apply is_prop.elimo
end
@[hott] def is_contr_equiv (A B : Type _) [HA : is_contr A] [HB : is_contr B] : is_contr (A ≃ B) :=
begin
apply is_contr_of_inhabited_prop _, apply is_prop.mk,
intros x y, cases x with fx Hx, cases y with fy Hy,
have : fx = fy, apply eq_of_homotopy, intro a, apply is_prop.elim, induction this,
have : Hx = Hy, apply is_prop.elim, induction this, refl,
apply equiv_of_is_contr_of_is_contr
end
@[hott] def is_trunc_succ_equiv (n : ℕ₋₂) (A B : Type _)
[HA : is_trunc n.+1 A] [HB : is_trunc n.+1 B] : is_trunc n.+1 (A ≃ B) :=
@is_trunc_equiv_closed _ _ n.+1 (equiv.symm (equiv.sigma_char _ _))
(@is_trunc_sigma _ _ _ _ (λ f, is_trunc_succ_of_is_prop _ _))
@[hott] def is_trunc_equiv (n : ℕ₋₂) (A B : Type _)
[HA : is_trunc n A] [HB : is_trunc n B] : is_trunc n (A ≃ B) :=
begin unfreezeI, cases n, exactI is_contr_equiv _ _, apply is_trunc_succ_equiv _ _ end
@[hott] def eq_of_fn_eq_fn'_idp {A B : Type _} (f : A → B) [is_equiv f] (x : A)
: eq_of_fn_eq_fn' f (idpath (f x)) = idpath x :=
con.left_inv _
@[hott] def eq_of_fn_eq_fn'_con {A B : Type _} (f : A → B) [is_equiv f] {x y z : A}
(p : f x = f y) (q : f y = f z)
: eq_of_fn_eq_fn' f (p ⬝ q) = eq_of_fn_eq_fn' f p ⬝ eq_of_fn_eq_fn' f q :=
begin
unfold eq_of_fn_eq_fn',
refine _ ⬝ con.assoc _ _ _, apply whisker_right,
refine _ ⬝ (con.assoc _ _ _)⁻¹, refine _ ⬝ (con.assoc _ _ _)⁻¹, apply whisker_left,
refine ap_con _ _ _ ⬝ _, apply whisker_left,
refine (con_inv_cancel_left _ _)⁻¹
end
end equiv
end hott |
2a8a05b174a709aac9ee3e480008ce6a23488b28 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/topology/metric_space/closeds.lean | bad5ff62afd01860e815ffa478a1c579e58bad2b | [
"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 | 21,168 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import analysis.specific_limits.basic
import topology.metric_space.hausdorff_distance
import topology.sets.compacts
/-!
# Closed subsets
This file defines the metric and emetric space structure on the types of closed subsets and nonempty
compact subsets of a metric or emetric space.
The Hausdorff distance induces an emetric space structure on the type of closed subsets
of an emetric space, called `closeds`. Its completeness, resp. compactness, resp.
second-countability, follow from the corresponding properties of the original space.
In a metric space, the type of nonempty compact subsets (called `nonempty_compacts`) also
inherits a metric space structure from the Hausdorff distance, as the Hausdorff edistance is
always finite in this context.
-/
noncomputable theory
open_locale classical topological_space ennreal
universe u
open classical set function topological_space filter
namespace emetric
section
variables {α : Type u} [emetric_space α] {s : set α}
/-- In emetric spaces, the Hausdorff edistance defines an emetric space structure
on the type of closed subsets -/
instance closeds.emetric_space : emetric_space (closeds α) :=
{ edist := λs t, Hausdorff_edist (s : set α) t,
edist_self := λs, Hausdorff_edist_self,
edist_comm := λs t, Hausdorff_edist_comm,
edist_triangle := λs t u, Hausdorff_edist_triangle,
eq_of_edist_eq_zero :=
λ s t h, closeds.ext $ (Hausdorff_edist_zero_iff_eq_of_closed s.closed t.closed).1 h }
/-- The edistance to a closed set depends continuously on the point and the set -/
lemma continuous_inf_edist_Hausdorff_edist :
continuous (λ p : α × (closeds α), inf_edist p.1 p.2) :=
begin
refine continuous_of_le_add_edist 2 (by simp) _,
rintros ⟨x, s⟩ ⟨y, t⟩,
calc inf_edist x s ≤ inf_edist x t + Hausdorff_edist (t : set α) s :
inf_edist_le_inf_edist_add_Hausdorff_edist
... ≤ inf_edist y t + edist x y + Hausdorff_edist (t : set α) s :
add_le_add_right inf_edist_le_inf_edist_add_edist _
... = inf_edist y t + (edist x y + Hausdorff_edist (s : set α) t)
: by rw [add_assoc, Hausdorff_edist_comm]
... ≤ inf_edist y t + (edist (x, s) (y, t) + edist (x, s) (y, t)) :
add_le_add_left (add_le_add (le_max_left _ _) (le_max_right _ _)) _
... = inf_edist y t + 2 * edist (x, s) (y, t) :
by rw [← mul_two, mul_comm]
end
/-- Subsets of a given closed subset form a closed set -/
lemma is_closed_subsets_of_is_closed (hs : is_closed s) :
is_closed {t : closeds α | (t : set α) ⊆ s} :=
begin
refine is_closed_of_closure_subset (λt ht x hx, _),
-- t : closeds α, ht : t ∈ closure {t : closeds α | t ⊆ s},
-- x : α, hx : x ∈ t
-- goal : x ∈ s
have : x ∈ closure s,
{ refine mem_closure_iff.2 (λε εpos, _),
rcases mem_closure_iff.1 ht ε εpos with ⟨u, hu, Dtu⟩,
-- u : closeds α, hu : u ∈ {t : closeds α | t ⊆ s}, hu' : edist t u < ε
rcases exists_edist_lt_of_Hausdorff_edist_lt hx Dtu with ⟨y, hy, Dxy⟩,
-- y : α, hy : y ∈ u, Dxy : edist x y < ε
exact ⟨y, hu hy, Dxy⟩ },
rwa hs.closure_eq at this,
end
/-- By definition, the edistance on `closeds α` is given by the Hausdorff edistance -/
lemma closeds.edist_eq {s t : closeds α} : edist s t = Hausdorff_edist (s : set α) t := rfl
/-- In a complete space, the type of closed subsets is complete for the
Hausdorff edistance. -/
instance closeds.complete_space [complete_space α] : complete_space (closeds α) :=
begin
/- We will show that, if a sequence of sets `s n` satisfies
`edist (s n) (s (n+1)) < 2^{-n}`, then it converges. This is enough to guarantee
completeness, by a standard completeness criterion.
We use the shorthand `B n = 2^{-n}` in ennreal. -/
let B : ℕ → ℝ≥0∞ := λ n, (2⁻¹)^n,
have B_pos : ∀ n, (0:ℝ≥0∞) < B n,
by simp [B, ennreal.pow_pos],
have B_ne_top : ∀ n, B n ≠ ⊤,
by simp [B, ennreal.pow_ne_top],
/- Consider a sequence of closed sets `s n` with `edist (s n) (s (n+1)) < B n`.
We will show that it converges. The limit set is t0 = ⋂n, closure (⋃m≥n, s m).
We will have to show that a point in `s n` is close to a point in `t0`, and a point
in `t0` is close to a point in `s n`. The completeness then follows from a
standard criterion. -/
refine complete_of_convergent_controlled_sequences B B_pos (λs hs, _),
let t0 := ⋂ n, closure (⋃ m ≥ n, s m : set α),
let t : closeds α := ⟨t0, is_closed_Inter (λ_, is_closed_closure)⟩,
use t,
-- The inequality is written this way to agree with `edist_le_of_edist_le_geometric_of_tendsto₀`
have I1 : ∀ n, ∀ x ∈ s n, ∃ y ∈ t0, edist x y ≤ 2 * B n,
{ /- This is the main difficulty of the proof. Starting from `x ∈ s n`, we want
to find a point in `t0` which is close to `x`. Define inductively a sequence of
points `z m` with `z n = x` and `z m ∈ s m` and `edist (z m) (z (m+1)) ≤ B m`. This is
possible since the Hausdorff distance between `s m` and `s (m+1)` is at most `B m`.
This sequence is a Cauchy sequence, therefore converging as the space is complete, to
a limit which satisfies the required properties. -/
assume n x hx,
obtain ⟨z, hz₀, hz⟩ : ∃ z : Π l, s (n + l), (z 0 : α) = x ∧
∀ k, edist (z k:α) (z (k+1):α) ≤ B n / 2^k,
{ -- We prove existence of the sequence by induction.
have : ∀ l (z : s (n + l)), ∃ z' : s (n + l + 1), edist (z : α) z' ≤ B n / 2^l,
{ assume l z,
obtain ⟨z', z'_mem, hz'⟩ : ∃ z' ∈ s (n + l + 1), edist (z : α) z' < B n / 2^l,
{ refine exists_edist_lt_of_Hausdorff_edist_lt _ _,
{ exact s (n + l) },
{ exact z.2 },
simp only [B, ennreal.inv_pow, div_eq_mul_inv],
rw [← pow_add],
apply hs; simp },
exact ⟨⟨z', z'_mem⟩, le_of_lt hz'⟩ },
use [λ k, nat.rec_on k ⟨x, hx⟩ (λl z, some (this l z)), rfl],
exact λ k, some_spec (this k _) },
-- it follows from the previous bound that `z` is a Cauchy sequence
have : cauchy_seq (λ k, ((z k):α)),
from cauchy_seq_of_edist_le_geometric_two (B n) (B_ne_top n) hz,
-- therefore, it converges
rcases cauchy_seq_tendsto_of_complete this with ⟨y, y_lim⟩,
use y,
-- the limit point `y` will be the desired point, in `t0` and close to our initial point `x`.
-- First, we check it belongs to `t0`.
have : y ∈ t0 := mem_Inter.2 (λk, mem_closure_of_tendsto y_lim
begin
simp only [exists_prop, set.mem_Union, filter.eventually_at_top, set.mem_preimage,
set.preimage_Union],
exact ⟨k, λ m hm, ⟨n+m, zero_add k ▸ add_le_add (zero_le n) hm, (z m).2⟩⟩
end),
use this,
-- Then, we check that `y` is close to `x = z n`. This follows from the fact that `y`
-- is the limit of `z k`, and the distance between `z n` and `z k` has already been estimated.
rw [← hz₀],
exact edist_le_of_edist_le_geometric_two_of_tendsto₀ (B n) hz y_lim },
have I2 : ∀ n, ∀ x ∈ t0, ∃ y ∈ s n, edist x y ≤ 2 * B n,
{ /- For the (much easier) reverse inequality, we start from a point `x ∈ t0` and we want
to find a point `y ∈ s n` which is close to `x`.
`x` belongs to `t0`, the intersection of the closures. In particular, it is well
approximated by a point `z` in `⋃m≥n, s m`, say in `s m`. Since `s m` and
`s n` are close, this point is itself well approximated by a point `y` in `s n`,
as required. -/
assume n x xt0,
have : x ∈ closure (⋃ m ≥ n, s m : set α), by apply mem_Inter.1 xt0 n,
rcases mem_closure_iff.1 this (B n) (B_pos n) with ⟨z, hz, Dxz⟩,
-- z : α, Dxz : edist x z < B n,
simp only [exists_prop, set.mem_Union] at hz,
rcases hz with ⟨m, ⟨m_ge_n, hm⟩⟩,
-- m : ℕ, m_ge_n : m ≥ n, hm : z ∈ s m
have : Hausdorff_edist (s m : set α) (s n) < B n := hs n m n m_ge_n (le_refl n),
rcases exists_edist_lt_of_Hausdorff_edist_lt hm this with ⟨y, hy, Dzy⟩,
-- y : α, hy : y ∈ s n, Dzy : edist z y < B n
exact ⟨y, hy, calc
edist x y ≤ edist x z + edist z y : edist_triangle _ _ _
... ≤ B n + B n : add_le_add (le_of_lt Dxz) (le_of_lt Dzy)
... = 2 * B n : (two_mul _).symm ⟩ },
-- Deduce from the above inequalities that the distance between `s n` and `t0` is at most `2 B n`.
have main : ∀n:ℕ, edist (s n) t ≤ 2 * B n := λn, Hausdorff_edist_le_of_mem_edist (I1 n) (I2 n),
-- from this, the convergence of `s n` to `t0` follows.
refine tendsto_at_top.2 (λε εpos, _),
have : tendsto (λn, 2 * B n) at_top (𝓝 (2 * 0)),
from ennreal.tendsto.const_mul
(ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 $ by simp [ennreal.one_lt_two])
(or.inr $ by simp),
rw mul_zero at this,
obtain ⟨N, hN⟩ : ∃ N, ∀ b ≥ N, ε > 2 * B b,
from ((tendsto_order.1 this).2 ε εpos).exists_forall_of_at_top,
exact ⟨N, λn hn, lt_of_le_of_lt (main n) (hN n hn)⟩
end
/-- In a compact space, the type of closed subsets is compact. -/
instance closeds.compact_space [compact_space α] : compact_space (closeds α) :=
⟨begin
/- by completeness, it suffices to show that it is totally bounded,
i.e., for all ε>0, there is a finite set which is ε-dense.
start from a set `s` which is ε-dense in α. Then the subsets of `s`
are finitely many, and ε-dense for the Hausdorff distance. -/
refine compact_of_totally_bounded_is_closed (emetric.totally_bounded_iff.2 (λε εpos, _))
is_closed_univ,
rcases exists_between εpos with ⟨δ, δpos, δlt⟩,
rcases emetric.totally_bounded_iff.1
(compact_iff_totally_bounded_complete.1 (@compact_univ α _ _)).1 δ δpos with ⟨s, fs, hs⟩,
-- s : set α, fs : s.finite, hs : univ ⊆ ⋃ (y : α) (H : y ∈ s), eball y δ
-- we first show that any set is well approximated by a subset of `s`.
have main : ∀ u : set α, ∃v ⊆ s, Hausdorff_edist u v ≤ δ,
{ assume u,
let v := {x : α | x ∈ s ∧ ∃y∈u, edist x y < δ},
existsi [v, ((λx hx, hx.1) : v ⊆ s)],
refine Hausdorff_edist_le_of_mem_edist _ _,
{ assume x hx,
have : x ∈ ⋃y ∈ s, ball y δ := hs (by simp),
rcases mem_Union₂.1 this with ⟨y, ys, dy⟩,
have : edist y x < δ := by simp at dy; rwa [edist_comm] at dy,
exact ⟨y, ⟨ys, ⟨x, hx, this⟩⟩, le_of_lt dy⟩ },
{ rintros x ⟨hx1, ⟨y, yu, hy⟩⟩,
exact ⟨y, yu, le_of_lt hy⟩ }},
-- introduce the set F of all subsets of `s` (seen as members of `closeds α`).
let F := {f : closeds α | (f : set α) ⊆ s},
refine ⟨F, _, λ u _, _⟩,
-- `F` is finite
{ apply @finite.of_finite_image _ _ F coe,
{ apply fs.finite_subsets.subset (λb, _),
simp only [and_imp, set.mem_image, set.mem_set_of_eq, exists_imp_distrib],
assume x hx hx',
rwa hx' at hx },
{ exact set_like.coe_injective.inj_on F } },
-- `F` is ε-dense
{ obtain ⟨t0, t0s, Dut0⟩ := main u,
have : is_closed t0 := (fs.subset t0s).is_compact.is_closed,
let t : closeds α := ⟨t0, this⟩,
have : t ∈ F := t0s,
have : edist u t < ε := lt_of_le_of_lt Dut0 δlt,
apply mem_Union₂.2,
exact ⟨t, ‹t ∈ F›, this⟩ }
end⟩
/-- In an emetric space, the type of non-empty compact subsets is an emetric space,
where the edistance is the Hausdorff edistance -/
instance nonempty_compacts.emetric_space : emetric_space (nonempty_compacts α) :=
{ edist := λ s t, Hausdorff_edist (s : set α) t,
edist_self := λs, Hausdorff_edist_self,
edist_comm := λs t, Hausdorff_edist_comm,
edist_triangle := λs t u, Hausdorff_edist_triangle,
eq_of_edist_eq_zero := λ s t h, nonempty_compacts.ext $ begin
have : closure (s : set α) = closure t := Hausdorff_edist_zero_iff_closure_eq_closure.1 h,
rwa [s.compact.is_closed.closure_eq, t.compact.is_closed.closure_eq] at this,
end }
/-- `nonempty_compacts.to_closeds` is a uniform embedding (as it is an isometry) -/
lemma nonempty_compacts.to_closeds.uniform_embedding :
uniform_embedding (@nonempty_compacts.to_closeds α _ _) :=
isometry.uniform_embedding $ λx y, rfl
/-- The range of `nonempty_compacts.to_closeds` is closed in a complete space -/
lemma nonempty_compacts.is_closed_in_closeds [complete_space α] :
is_closed (range $ @nonempty_compacts.to_closeds α _ _) :=
begin
have : range nonempty_compacts.to_closeds =
{s : closeds α | (s : set α).nonempty ∧ is_compact (s : set α) },
{ ext s,
refine ⟨_, λ h, ⟨⟨⟨s, h.2⟩, h.1⟩, closeds.ext rfl⟩⟩,
rintro ⟨s, hs, rfl⟩,
exact ⟨s.nonempty, s.compact⟩ },
rw this,
refine is_closed_of_closure_subset (λs hs, ⟨_, _⟩),
{ -- take a set set t which is nonempty and at a finite distance of s
rcases mem_closure_iff.1 hs ⊤ ennreal.coe_lt_top with ⟨t, ht, Dst⟩,
rw edist_comm at Dst,
-- since `t` is nonempty, so is `s`
exact nonempty_of_Hausdorff_edist_ne_top ht.1 (ne_of_lt Dst) },
{ refine compact_iff_totally_bounded_complete.2 ⟨_, s.closed.is_complete⟩,
refine totally_bounded_iff.2 (λε (εpos : 0 < ε), _),
-- we have to show that s is covered by finitely many eballs of radius ε
-- pick a nonempty compact set t at distance at most ε/2 of s
rcases mem_closure_iff.1 hs (ε/2) (ennreal.half_pos εpos.ne') with ⟨t, ht, Dst⟩,
-- cover this space with finitely many balls of radius ε/2
rcases totally_bounded_iff.1 (compact_iff_totally_bounded_complete.1 ht.2).1 (ε/2)
(ennreal.half_pos εpos.ne') with ⟨u, fu, ut⟩,
refine ⟨u, ⟨fu, λx hx, _⟩⟩,
-- u : set α, fu : u.finite, ut : t ⊆ ⋃ (y : α) (H : y ∈ u), eball y (ε / 2)
-- then s is covered by the union of the balls centered at u of radius ε
rcases exists_edist_lt_of_Hausdorff_edist_lt hx Dst with ⟨z, hz, Dxz⟩,
rcases mem_Union₂.1 (ut hz) with ⟨y, hy, Dzy⟩,
have : edist x y < ε := calc
edist x y ≤ edist x z + edist z y : edist_triangle _ _ _
... < ε/2 + ε/2 : ennreal.add_lt_add Dxz Dzy
... = ε : ennreal.add_halves _,
exact mem_bUnion hy this },
end
/-- In a complete space, the type of nonempty compact subsets is complete. This follows
from the same statement for closed subsets -/
instance nonempty_compacts.complete_space [complete_space α] :
complete_space (nonempty_compacts α) :=
(complete_space_iff_is_complete_range
nonempty_compacts.to_closeds.uniform_embedding.to_uniform_inducing).2 $
nonempty_compacts.is_closed_in_closeds.is_complete
/-- In a compact space, the type of nonempty compact subsets is compact. This follows from
the same statement for closed subsets -/
instance nonempty_compacts.compact_space [compact_space α] : compact_space (nonempty_compacts α) :=
⟨begin
rw nonempty_compacts.to_closeds.uniform_embedding.embedding.is_compact_iff_is_compact_image,
rw [image_univ],
exact nonempty_compacts.is_closed_in_closeds.is_compact
end⟩
/-- In a second countable space, the type of nonempty compact subsets is second countable -/
instance nonempty_compacts.second_countable_topology [second_countable_topology α] :
second_countable_topology (nonempty_compacts α) :=
begin
haveI : separable_space (nonempty_compacts α) :=
begin
/- To obtain a countable dense subset of `nonempty_compacts α`, start from
a countable dense subset `s` of α, and then consider all its finite nonempty subsets.
This set is countable and made of nonempty compact sets. It turns out to be dense:
by total boundedness, any compact set `t` can be covered by finitely many small balls, and
approximations in `s` of the centers of these balls give the required finite approximation
of `t`. -/
rcases exists_countable_dense α with ⟨s, cs, s_dense⟩,
let v0 := {t : set α | t.finite ∧ t ⊆ s},
let v : set (nonempty_compacts α) := {t : nonempty_compacts α | (t : set α) ∈ v0},
refine ⟨⟨v, _, _⟩⟩,
{ have : v0.countable, from countable_set_of_finite_subset cs,
exact this.preimage set_like.coe_injective },
{ refine λt, mem_closure_iff.2 (λε εpos, _),
-- t is a compact nonempty set, that we have to approximate uniformly by a a set in `v`.
rcases exists_between εpos with ⟨δ, δpos, δlt⟩,
have δpos' : 0 < δ / 2, from ennreal.half_pos δpos.ne',
-- construct a map F associating to a point in α an approximating point in s, up to δ/2.
have Exy : ∀x, ∃y, y ∈ s ∧ edist x y < δ/2,
{ assume x,
rcases mem_closure_iff.1 (s_dense x) (δ/2) δpos' with ⟨y, ys, hy⟩,
exact ⟨y, ⟨ys, hy⟩⟩ },
let F := λx, some (Exy x),
have Fspec : ∀x, F x ∈ s ∧ edist x (F x) < δ/2 := λx, some_spec (Exy x),
-- cover `t` with finitely many balls. Their centers form a set `a`
have : totally_bounded (t : set α) := t.compact.totally_bounded,
rcases totally_bounded_iff.1 this (δ/2) δpos' with ⟨a, af, ta⟩,
-- a : set α, af : a.finite, ta : t ⊆ ⋃ (y : α) (H : y ∈ a), eball y (δ / 2)
-- replace each center by a nearby approximation in `s`, giving a new set `b`
let b := F '' a,
have : b.finite := af.image _,
have tb : ∀ x ∈ t, ∃ y ∈ b, edist x y < δ,
{ assume x hx,
rcases mem_Union₂.1 (ta hx) with ⟨z, za, Dxz⟩,
existsi [F z, mem_image_of_mem _ za],
calc edist x (F z) ≤ edist x z + edist z (F z) : edist_triangle _ _ _
... < δ/2 + δ/2 : ennreal.add_lt_add Dxz (Fspec z).2
... = δ : ennreal.add_halves _ },
-- keep only the points in `b` that are close to point in `t`, yielding a new set `c`
let c := {y ∈ b | ∃ x ∈ t, edist x y < δ},
have : c.finite := ‹b.finite›.subset (λx hx, hx.1),
-- points in `t` are well approximated by points in `c`
have tc : ∀ x ∈ t, ∃ y ∈ c, edist x y ≤ δ,
{ assume x hx,
rcases tb x hx with ⟨y, yv, Dxy⟩,
have : y ∈ c := by simp [c, -mem_image]; exact ⟨yv, ⟨x, hx, Dxy⟩⟩,
exact ⟨y, this, le_of_lt Dxy⟩ },
-- points in `c` are well approximated by points in `t`
have ct : ∀ y ∈ c, ∃ x ∈ t, edist y x ≤ δ,
{ rintro y ⟨hy1, x, xt, Dyx⟩,
have : edist y x ≤ δ := calc
edist y x = edist x y : edist_comm _ _
... ≤ δ : le_of_lt Dyx,
exact ⟨x, xt, this⟩ },
-- it follows that their Hausdorff distance is small
have : Hausdorff_edist (t :set α) c ≤ δ :=
Hausdorff_edist_le_of_mem_edist tc ct,
have Dtc : Hausdorff_edist (t : set α) c < ε := this.trans_lt δlt,
-- the set `c` is not empty, as it is well approximated by a nonempty set
have hc : c.nonempty,
from nonempty_of_Hausdorff_edist_ne_top t.nonempty (ne_top_of_lt Dtc),
-- let `d` be the version of `c` in the type `nonempty_compacts α`
let d : nonempty_compacts α := ⟨⟨c, ‹c.finite›.is_compact⟩, hc⟩,
have : c ⊆ s,
{ assume x hx,
rcases (mem_image _ _ _).1 hx.1 with ⟨y, ⟨ya, yx⟩⟩,
rw ← yx,
exact (Fspec y).1 },
have : d ∈ v := ⟨‹c.finite›, this⟩,
-- we have proved that `d` is a good approximation of `t` as requested
exact ⟨d, ‹d ∈ v›, Dtc⟩ },
end,
apply uniform_space.second_countable_of_separable,
end
end --section
end emetric --namespace
namespace metric
section
variables {α : Type u} [metric_space α]
/-- `nonempty_compacts α` inherits a metric space structure, as the Hausdorff
edistance between two such sets is finite. -/
instance nonempty_compacts.metric_space : metric_space (nonempty_compacts α) :=
emetric_space.to_metric_space $ λ x y, Hausdorff_edist_ne_top_of_nonempty_of_bounded
x.nonempty y.nonempty x.compact.bounded y.compact.bounded
/-- The distance on `nonempty_compacts α` is the Hausdorff distance, by construction -/
lemma nonempty_compacts.dist_eq {x y : nonempty_compacts α} :
dist x y = Hausdorff_dist (x : set α) y := rfl
lemma lipschitz_inf_dist_set (x : α) : lipschitz_with 1 (λ s : nonempty_compacts α, inf_dist x s) :=
lipschitz_with.of_le_add $ assume s t,
by { rw dist_comm,
exact inf_dist_le_inf_dist_add_Hausdorff_dist (edist_ne_top t s) }
lemma lipschitz_inf_dist : lipschitz_with 2 (λ p : α × (nonempty_compacts α), inf_dist p.1 p.2) :=
@lipschitz_with.uncurry _ _ _ _ _ _ (λ (x : α) (s : nonempty_compacts α), inf_dist x s) 1 1
(λ s, lipschitz_inf_dist_pt s) lipschitz_inf_dist_set
lemma uniform_continuous_inf_dist_Hausdorff_dist :
uniform_continuous (λ p : α × (nonempty_compacts α), inf_dist p.1 p.2) :=
lipschitz_inf_dist.uniform_continuous
end --section
end metric --namespace
|
0a9a864d41173629d4b417f1e20912b8d4564208 | 7a097aab717143b13a4a4f8824fe9cfe670a7883 | /src/smt2/builder.lean | 6476675d8ba3140434afccbb71dc9b9f67c8e423 | [
"Apache-2.0"
] | permissive | digama0/smt2_interface | 6b3bbc12cb90bd4bc70b07ca86918c1e9ff4ed97 | cf516ae4142f33adcf4b272758d63fee37ec9f7d | refs/heads/master | 1,611,266,770,453 | 1,498,177,713,000 | 1,498,177,713,000 | 95,167,865 | 0 | 0 | null | 1,498,177,813,000 | 1,498,177,813,000 | null | UTF-8 | Lean | false | false | 2,901 | lean | import .syntax
import .except
import init.category.transformers
@[reducible] def smt2.builder :=
except_t (state (list smt2.cmd)) string
instance smt2.builder.monad : monad smt2.builder :=
by apply_instance
meta def smt2.builder.to_format {α : Type} (build : smt2.builder α) : format :=
format.join $ list.intersperse "\n" $ (list.map to_fmt $ (build []).snd).reverse
meta def smt2.builder.run {α : Type} (build : smt2.builder α) : (except string α × list smt2.cmd) :=
build []
meta def smt2.builder.fail {α : Type} : string → smt2.builder α :=
fun msg s, (except.error msg, s)
meta instance (α : Type) : has_to_format (smt2.builder α) :=
⟨ smt2.builder.to_format ⟩
namespace smt2
namespace builder
def equals (t u : term) : term :=
term.apply "=" [t, u]
def not (t : term) : term :=
term.apply "not" [t]
def implies (t u : term) : term :=
term.apply "implies" [t, u]
def forallq (sym : symbol) (s : sort) (t : term) : term :=
term.forallq [(sym, s)] t
def and (ts : list term) : term :=
term.apply "and" ts
def and2 (t u : term) : term :=
and [t, u]
def or (ts : list term) : term :=
term.apply "or" ts
def or2 (t u : term) : term :=
or [t, u]
def iff (t u : term) : term :=
term.apply "iff" [t, u]
def lt (t u : term) : term :=
term.apply "<" [t, u]
def lte (t u : term) : term :=
term.apply "<=" [t, u]
def gt (t u : term) : term :=
term.apply ">" [t, u]
def gte (t u : term) : term :=
term.apply ">=" [t, u]
def add (t u : term) : term :=
term.apply "+" [t, u]
def sub (t u : term) : term :=
term.apply "-" [t, u]
def mul (t u : term) : term :=
term.apply "*" [t, u]
def div (t u : term) : term :=
term.apply "div" [t, u]
def int_const (i : int) : term :=
term.const $ special_constant.number i
def lift_state {α : Type} (action : state (list smt2.cmd) α) : smt2.builder α :=
λ s, let (a, s') := action s in (except.ok a, s')
def add_command (c : cmd) : builder unit := do
cs ← lift_state state.read,
lift_state $ state.write (c :: cs)
def echo (msg : string) : builder unit :=
add_command (cmd.echo msg)
def check_sat : builder unit :=
add_command cmd.check_sat
def pop (n : nat) : builder unit :=
add_command $ cmd.pop n
def push (n : nat) : builder unit :=
add_command $ cmd.push n
def scope {α} (level : nat) (action : builder α) : builder α :=
do push level,
res ← action,
pop level,
return res
def assert (t : term) : builder unit :=
add_command $ cmd.assert_cmd t
def reset : builder unit :=
add_command cmd.reset
def exit' : builder unit :=
add_command cmd.exit_cmd
def declare_const (sym : string) (s : sort) : builder unit :=
add_command $ cmd.declare_const sym s
def declare_fun (sym : string) (ps : list sort) (ret : sort) : builder unit :=
add_command $ cmd.declare_fun sym ps ret
def declare_sort (sym : string) (arity : nat) : builder unit :=
add_command $ cmd.declare_sort sym arity
end builder
end smt2
|
a91a1200de757bcabf303cbbeb4ce2ed3db6440b | 82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7 | /stage0/src/Lean/Elab/PreDefinition/Main.lean | 2577f3fd4dfec2a7e2944559f324f453b5621e53 | [
"Apache-2.0"
] | permissive | banksonian/lean4 | 3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc | 78da6b3aa2840693eea354a41e89fc5b212a5011 | refs/heads/master | 1,673,703,624,165 | 1,605,123,551,000 | 1,605,123,551,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,227 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Elab.PreDefinition.Basic
import Lean.Elab.PreDefinition.Structural
import Lean.Elab.PreDefinition.WF
namespace Lean.Elab
open Meta
open Term
private def addAndCompilePartial (preDefs : Array PreDefinition) : TermElabM Unit := do
for preDef in preDefs do
trace[Elab.definition]! "processing {preDef.declName}"
forallTelescope preDef.type fun xs type => do
let inh ← liftM $ mkInhabitantFor preDef.declName xs type
trace[Elab.definition]! "inhabitant for {preDef.declName}"
addNonRec { preDef with
kind := DefKind.«opaque»,
value := inh
}
addAndCompileUnsafeRec preDefs
private def isNonRecursive (preDef : PreDefinition) : Bool :=
Option.isNone $ preDef.value.find? fun
| Expr.const declName _ _ => preDef.declName == declName
| _ => false
private def partitionPreDefs (preDefs : Array PreDefinition) : Array (Array PreDefinition) :=
let getPreDef := fun declName => (preDefs.find? fun preDef => preDef.declName == declName).get!
let vertices := preDefs.toList.map (·.declName)
let successorsOf := fun declName => (getPreDef declName).value.foldConsts [] fun declName successors =>
if preDefs.any fun preDef => preDef.declName == declName then
declName :: successors
else
successors
let sccs := SCC.scc vertices successorsOf
sccs.toArray.map fun scc => scc.toArray.map getPreDef
private def collectMVarsAtPreDef (preDef : PreDefinition) : StateRefT CollectMVars.State MetaM Unit := do
collectMVars preDef.value
collectMVars preDef.type
private def getMVarsAtPreDef (preDef : PreDefinition) : MetaM (Array MVarId) := do
let (_, s) ← (collectMVarsAtPreDef preDef).run {}
pure s.result
private def ensureNoUnassignedMVarsAtPreDef (preDef : PreDefinition) : TermElabM Unit := do
let pendingMVarIds ← liftMetaM $ getMVarsAtPreDef preDef
if ← logUnassignedUsingErrorInfos pendingMVarIds then
throwAbort
def addPreDefinitions (preDefs : Array PreDefinition) : TermElabM Unit := do
for preDef in preDefs do
trace[Elab.definition.body]! "{preDef.declName} : {preDef.type} :=\n{preDef.value}"
for preDef in preDefs do
ensureNoUnassignedMVarsAtPreDef preDef
for preDefs in partitionPreDefs preDefs do
trace[Elab.definition.scc]! "{preDefs.map (·.declName)}"
if preDefs.size == 1 && isNonRecursive preDefs[0] then
let preDef := preDefs[0]
if preDef.modifiers.isNoncomputable then
addNonRec preDef
else
addAndCompileNonRec preDef
else if preDefs.any (·.modifiers.isUnsafe) then
addAndCompileUnsafe preDefs
else if preDefs.any (·.modifiers.isPartial) then
addAndCompilePartial preDefs
else
mapError
(orelseMergeErrors
(structuralRecursion preDefs)
(WFRecursion preDefs))
(fun msg =>
let preDefMsgs := preDefs.toList.map (MessageData.ofExpr $ mkConst ·.declName)
msg!"fail to show termination for{indentD (MessageData.joinSep preDefMsgs Format.line)}\nwith errors\n{msg}")
end Lean.Elab
|
c7d821bc4fa3228c716d4c4935fc2696a6c1ec58 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/abelian/projective.lean | 2404efdbfe31774b15e448ab7091b3de23b5df15 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 2,648 | lean | /-
Copyright (c) 2020 Markus Himmel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Markus Himmel, Scott Morrison
-/
import category_theory.abelian.exact
import category_theory.preadditive.projective_resolution
/-!
# Abelian categories with enough projectives have projective resolutions
When `C` is abelian `projective.d f` and `f` are exact.
Hence, starting from an epimorphism `P ⟶ X`, where `P` is projective,
we can apply `projective.d` repeatedly to obtain a projective resolution of `X`.
-/
noncomputable theory
open category_theory
open category_theory.limits
universes v u
namespace category_theory
open category_theory.projective
variables {C : Type u} [category.{v} C]
section
variables [enough_projectives C] [abelian C]
/--
When `C` is abelian, `projective.d f` and `f` are exact.
-/
lemma exact_d_f {X Y : C} (f : X ⟶ Y) : exact (d f) f :=
(abelian.exact_iff _ _).2 $
⟨by simp, zero_of_epi_comp (π _) $ by rw [←category.assoc, cokernel.condition]⟩
end
namespace ProjectiveResolution
/-!
Our goal is to define `ProjectiveResolution.of Z : ProjectiveResolution Z`.
The `0`-th object in this resolution will just be `projective.over Z`,
i.e. an arbitrarily chosen projective object with a map to `Z`.
After that, we build the `n+1`-st object as `projective.syzygies`
applied to the previously constructed morphism,
and the map to the `n`-th object as `projective.d`.
-/
variables [abelian C] [enough_projectives C]
/-- Auxiliary definition for `ProjectiveResolution.of`. -/
@[simps]
def of_complex (Z : C) : chain_complex C ℕ :=
chain_complex.mk'
(projective.over Z) (projective.syzygies (projective.π Z)) (projective.d (projective.π Z))
(λ ⟨X, Y, f⟩, ⟨projective.syzygies f, projective.d f, (exact_d_f f).w⟩)
/--
In any abelian category with enough projectives,
`ProjectiveResolution.of Z` constructs a projective resolution of the object `Z`.
-/
@[irreducible] def of (Z : C) : ProjectiveResolution Z :=
{ complex := of_complex Z,
π := chain_complex.mk_hom _ _ (projective.π Z) 0
(by { simp, exact (exact_d_f (projective.π Z)).w.symm, })
(λ n _, ⟨0, by ext⟩),
projective := by { rintros (_|_|_|n); apply projective.projective_over, },
exact₀ := by simpa using exact_d_f (projective.π Z),
exact := by { rintros (_|n); { simp, apply exact_d_f, }, },
epi := projective.π_epi Z, }
@[priority 100]
instance (Z : C) : has_projective_resolution Z :=
{ out := ⟨of Z⟩ }
@[priority 100]
instance : has_projective_resolutions C :=
{ out := λ Z, by apply_instance }
end ProjectiveResolution
end category_theory
|
1c4006b75f17a714e8f49eab155bc615924206d5 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/match_pattern1.lean | 1b9aa0f0e884f6f60c4bccf63f8add4e2e7c14b8 | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 495 | lean | open tactic expr
set_option pp.all true
example (a b c x y : nat) (H : nat.add (nat.add x y) y = 0) : true :=
by do
a ← get_local `a, b ← get_local `b, c ← get_local `c,
nat_add : expr ← mk_const `nat.add,
p : pattern ← mk_pattern [] [a, b] (app_of_list nat_add [a, b]) [] [app_of_list nat_add [b, a], a, b],
trace (pattern.moutput p),
H ← get_local `H >>= infer_type,
lhs_rhs ← match_eq H,
r ← match_pattern p (prod.fst lhs_rhs),
trace r,
constructor
|
dd1cb476610e903e69294a274835705e64f3b08f | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /stage0/src/Lean/Elab/Tactic/Conv/Delta.lean | 60202eb0037190cf21c1eb5a6dfb4a90566c8fa2 | [
"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 | 583 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Elab.Tactic.Delta
import Lean.Elab.Tactic.Conv.Basic
namespace Lean.Elab.Tactic.Conv
open Meta
@[builtinTactic Lean.Parser.Tactic.Conv.delta] def evalDelta : Tactic := fun stx => withMainContext do
let declNames ← stx[1].getArgs.mapM resolveGlobalConstNoOverloadWithInfo
let lhsNew ← deltaExpand (← instantiateMVars (← getLhs)) declNames.contains
changeLhs lhsNew
end Lean.Elab.Tactic.Conv
|
1b1e0c713584a9ead0cdd0c4865cd4e845d99203 | 5d166a16ae129621cb54ca9dde86c275d7d2b483 | /tests/lean/run/simp_match_reducibility_issue.lean | 1ea1c8256d884ca2f49ada1f7850273a3f90c5c5 | [
"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 | 607 | lean | namespace test
universes u v
def vector (α : Type u) (n : ℕ) := { l : list α // list.length l = n }
namespace vector
open subtype list
variable {α : Type u}
def nil : vector α 0 := ⟨[], rfl⟩
def append {m n : ℕ} : vector α m → vector α n → vector α (m + n)
| ⟨v, _⟩ ⟨w, _⟩ := ⟨v ++ w, by abstract {simph}⟩
end vector
section
open vector
variable α : Type u
variables m n k : ℕ
variable v : vector α m
variable w : vector α n
theorem append_nil : append v nil = v :=
by cases v; simp [vector.append, vector.nil]; reflexivity
end
end test
|
35d15c45e847e52d613a7400ea620eaada47e08e | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/topology/uniform_space/separation.lean | 32fda3d944457859998d8a228249cd4820c35d44 | [
"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 | 19,995 | 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, Patrick Massot
-/
import tactic.apply_fun
import topology.uniform_space.basic
import topology.separation
/-!
# Hausdorff properties of uniform spaces. Separation quotient.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file studies uniform spaces whose underlying topological spaces are separated
(also known as Hausdorff or T₂).
This turns out to be equivalent to asking that the intersection of all entourages
is the diagonal only. This condition actually implies the stronger separation property
that the space is T₃, hence those conditions are equivalent for topologies coming from
a uniform structure.
More generally, the intersection `𝓢 X` of all entourages of `X`, which has type `set (X × X)` is an
equivalence relation on `X`. Points which are equivalent under the relation are basically
undistinguishable from the point of view of the uniform structure. For instance any uniformly
continuous function will send equivalent points to the same value.
The quotient `separation_quotient X` of `X` by `𝓢 X` has a natural uniform structure which is
separated, and satisfies a universal property: every uniformly continuous function
from `X` to a separated uniform space uniquely factors through `separation_quotient X`.
As usual, this allows to turn `separation_quotient` into a functor (but we don't use the
category theory library in this file).
These notions admit relative versions, one can ask that `s : set X` is separated, this
is equivalent to asking that the uniform structure induced on `s` is separated.
## Main definitions
* `separation_relation X : set (X × X)`: the separation relation
* `separated_space X`: a predicate class asserting that `X` is separated
* `separation_quotient X`: the maximal separated quotient of `X`.
* `separation_quotient.lift f`: factors a map `f : X → Y` through the separation quotient of `X`.
* `separation_quotient.map f`: turns a map `f : X → Y` into a map between the separation quotients
of `X` and `Y`.
## Main results
* `separated_iff_t2`: the equivalence between being separated and being Hausdorff for uniform
spaces.
* `separation_quotient.uniform_continuous_lift`: factoring a uniformly continuous map through the
separation quotient gives a uniformly continuous map.
* `separation_quotient.uniform_continuous_map`: maps induced between separation quotients are
uniformly continuous.
## Notations
Localized in `uniformity`, we have the notation `𝓢 X` for the separation relation
on a uniform space `X`,
## Implementation notes
The separation setoid `separation_setoid` is not declared as a global instance.
It is made a local instance while building the theory of `separation_quotient`.
The factored map `separation_quotient.lift f` is defined without imposing any condition on
`f`, but returns junk if `f` is not uniformly continuous (constant junk hence it is always
uniformly continuous).
-/
open filter topological_space set classical function uniform_space
open_locale classical topology uniformity filter
noncomputable theory
set_option eqn_compiler.zeta true
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
variables [uniform_space α] [uniform_space β] [uniform_space γ]
/-!
### Separated uniform spaces
-/
@[priority 100]
instance uniform_space.to_regular_space : regular_space α :=
regular_space.of_basis
(λ a, by { rw [nhds_eq_comap_uniformity], exact uniformity_has_basis_closed.comap _ })
(λ a V hV, hV.2.preimage $ continuous_const.prod_mk continuous_id)
/-- The separation relation is the intersection of all entourages.
Two points which are related by the separation relation are "indistinguishable"
according to the uniform structure. -/
protected def separation_rel (α : Type u) [u : uniform_space α] :=
⋂₀ (𝓤 α).sets
localized "notation (name := separation_rel) `𝓢` := separation_rel" in uniformity
lemma separated_equiv : equivalence (λx y, (x, y) ∈ 𝓢 α) :=
⟨assume x, assume s, refl_mem_uniformity,
assume x y, assume h (s : set (α×α)) hs,
have preimage prod.swap s ∈ 𝓤 α,
from symm_le_uniformity hs,
h _ this,
assume x y z (hxy : (x, y) ∈ 𝓢 α) (hyz : (y, z) ∈ 𝓢 α)
s (hs : s ∈ 𝓤 α),
let ⟨t, ht, (h_ts : comp_rel t t ⊆ s)⟩ := comp_mem_uniformity_sets hs in
h_ts $ show (x, z) ∈ comp_rel t t,
from ⟨y, hxy t ht, hyz t ht⟩⟩
lemma filter.has_basis.mem_separation_rel {ι : Sort*} {p : ι → Prop} {s : ι → set (α × α)}
(h : (𝓤 α).has_basis p s) {a : α × α} :
a ∈ 𝓢 α ↔ ∀ i, p i → a ∈ s i :=
h.forall_mem_mem
theorem separation_rel_iff_specializes {a b : α} : (a, b) ∈ 𝓢 α ↔ a ⤳ b :=
by simp only [(𝓤 α).basis_sets.mem_separation_rel, id, mem_set_of_eq,
(nhds_basis_uniformity (𝓤 α).basis_sets).specializes_iff]
theorem separation_rel_iff_inseparable {a b : α} : (a, b) ∈ 𝓢 α ↔ inseparable a b :=
separation_rel_iff_specializes.trans specializes_iff_inseparable
/-- A uniform space is separated if its separation relation is trivial (each point
is related only to itself). -/
class separated_space (α : Type u) [uniform_space α] : Prop := (out : 𝓢 α = id_rel)
theorem separated_space_iff {α : Type u} [uniform_space α] :
separated_space α ↔ 𝓢 α = id_rel :=
⟨λ h, h.1, λ h, ⟨h⟩⟩
theorem separated_def {α : Type u} [uniform_space α] :
separated_space α ↔ ∀ x y, (∀ r ∈ 𝓤 α, (x, y) ∈ r) → x = y :=
by simp [separated_space_iff, id_rel_subset.2 separated_equiv.1, subset.antisymm_iff];
simp [subset_def, separation_rel]
theorem separated_def' {α : Type u} [uniform_space α] :
separated_space α ↔ ∀ x y, x ≠ y → ∃ r ∈ 𝓤 α, (x, y) ∉ r :=
separated_def.trans $ forall₂_congr $ λ x y, by rw ← not_imp_not; simp [not_forall]
lemma eq_of_uniformity {α : Type*} [uniform_space α] [separated_space α] {x y : α}
(h : ∀ {V}, V ∈ 𝓤 α → (x, y) ∈ V) : x = y :=
separated_def.mp ‹separated_space α› x y (λ _, h)
lemma eq_of_uniformity_basis {α : Type*} [uniform_space α] [separated_space α] {ι : Type*}
{p : ι → Prop} {s : ι → set (α × α)} (hs : (𝓤 α).has_basis p s) {x y : α}
(h : ∀ {i}, p i → (x, y) ∈ s i) : x = y :=
eq_of_uniformity (λ V V_in, let ⟨i, hi, H⟩ := hs.mem_iff.mp V_in in H (h hi))
lemma eq_of_forall_symmetric {α : Type*} [uniform_space α] [separated_space α] {x y : α}
(h : ∀ {V}, V ∈ 𝓤 α → symmetric_rel V → (x, y) ∈ V) : x = y :=
eq_of_uniformity_basis has_basis_symmetric (by simpa [and_imp] using λ _, h)
lemma eq_of_cluster_pt_uniformity [separated_space α] {x y : α} (h : cluster_pt (x, y) (𝓤 α)) :
x = y :=
eq_of_uniformity_basis uniformity_has_basis_closed $ λ V ⟨hV, hVc⟩,
is_closed_iff_cluster_pt.1 hVc _ $ h.mono $ le_principal_iff.2 hV
lemma id_rel_sub_separation_relation (α : Type*) [uniform_space α] : id_rel ⊆ 𝓢 α :=
begin
unfold separation_rel,
rw id_rel_subset,
intros x,
suffices : ∀ t ∈ 𝓤 α, (x, x) ∈ t, by simpa only [refl_mem_uniformity],
exact λ t, refl_mem_uniformity,
end
lemma separation_rel_comap {f : α → β}
(h : ‹uniform_space α› = uniform_space.comap f ‹uniform_space β›) :
𝓢 α = (prod.map f f) ⁻¹' 𝓢 β :=
begin
unfreezingI { subst h },
dsimp [separation_rel],
simp_rw [uniformity_comap, (filter.comap_has_basis (prod.map f f) (𝓤 β)).sInter_sets,
← preimage_Inter, sInter_eq_bInter],
refl,
end
protected lemma filter.has_basis.separation_rel {ι : Sort*} {p : ι → Prop} {s : ι → set (α × α)}
(h : has_basis (𝓤 α) p s) :
𝓢 α = ⋂ i (hi : p i), s i :=
by { unfold separation_rel, rw h.sInter_sets }
lemma separation_rel_eq_inter_closure : 𝓢 α = ⋂₀ (closure '' (𝓤 α).sets) :=
by simp [uniformity_has_basis_closure.separation_rel]
lemma is_closed_separation_rel : is_closed (𝓢 α) :=
begin
rw separation_rel_eq_inter_closure,
apply is_closed_sInter,
rintros _ ⟨t, t_in, rfl⟩,
exact is_closed_closure,
end
lemma separated_iff_t2 : separated_space α ↔ t2_space α :=
begin
classical,
split ; introI h,
{ rw [t2_iff_is_closed_diagonal, ← show 𝓢 α = diagonal α, from h.1],
exact is_closed_separation_rel },
{ rw separated_def',
intros x y hxy,
rcases t2_separation hxy with ⟨u, v, uo, vo, hx, hy, h⟩,
rcases is_open_iff_ball_subset.1 uo x hx with ⟨r, hrU, hr⟩,
exact ⟨r, hrU, λ H, h.le_bot ⟨hr H, hy⟩⟩ }
end
@[priority 100] -- see Note [lower instance priority]
instance separated_t3 [separated_space α] : t3_space α :=
by { haveI := separated_iff_t2.mp ‹_›, exact ⟨⟩ }
instance subtype.separated_space [separated_space α] (s : set α) : separated_space s :=
separated_iff_t2.mpr subtype.t2_space
lemma is_closed_of_spaced_out [separated_space α] {V₀ : set (α × α)} (V₀_in : V₀ ∈ 𝓤 α)
{s : set α} (hs : s.pairwise (λ x y, (x, y) ∉ V₀)) : is_closed s :=
begin
rcases comp_symm_mem_uniformity_sets V₀_in with ⟨V₁, V₁_in, V₁_symm, h_comp⟩,
apply is_closed_of_closure_subset,
intros x hx,
rw mem_closure_iff_ball at hx,
rcases hx V₁_in with ⟨y, hy, hy'⟩,
suffices : x = y, by rwa this,
apply eq_of_forall_symmetric,
intros V V_in V_symm,
rcases hx (inter_mem V₁_in V_in) with ⟨z, hz, hz'⟩,
obtain rfl : z = y,
{ by_contra hzy,
exact hs hz' hy' hzy (h_comp $ mem_comp_of_mem_ball V₁_symm (ball_inter_left x _ _ hz) hy) },
exact ball_inter_right x _ _ hz
end
lemma is_closed_range_of_spaced_out {ι} [separated_space α] {V₀ : set (α × α)} (V₀_in : V₀ ∈ 𝓤 α)
{f : ι → α} (hf : pairwise (λ x y, (f x, f y) ∉ V₀)) : is_closed (range f) :=
is_closed_of_spaced_out V₀_in $
by { rintro _ ⟨x, rfl⟩ _ ⟨y, rfl⟩ h, exact hf (ne_of_apply_ne f h) }
/-!
### Separation quotient
-/
namespace uniform_space
/-- The separation relation of a uniform space seen as a setoid. -/
def separation_setoid (α : Type u) [uniform_space α] : setoid α :=
⟨λx y, (x, y) ∈ 𝓢 α, separated_equiv⟩
local attribute [instance] separation_setoid
instance separation_setoid.uniform_space {α : Type u} [u : uniform_space α] :
uniform_space (quotient (separation_setoid α)) :=
{ to_topological_space := u.to_topological_space.coinduced (λx, ⟦x⟧),
uniformity := map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) u.uniformity,
refl := le_trans (by simp [quotient.exists_rep]) (filter.map_mono refl_le_uniformity),
symm := tendsto_map' $
by simp [prod.swap, (∘)]; exact tendsto_map.comp tendsto_swap_uniformity,
comp := calc (map (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) u.uniformity).lift' (λs, comp_rel s s) =
u.uniformity.lift' ((λs, comp_rel s s) ∘ image (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧))) :
map_lift'_eq2 $ monotone_id.comp_rel monotone_id
... ≤ u.uniformity.lift' (image (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) ∘
(λs:set (α×α), comp_rel s (comp_rel s s))) :
lift'_mono' $ assume s hs ⟨a, b⟩ ⟨c, ⟨⟨a₁, a₂⟩, ha, a_eq⟩, ⟨⟨b₁, b₂⟩, hb, b_eq⟩⟩,
begin
simp at a_eq,
simp at b_eq,
have h : ⟦a₂⟧ = ⟦b₁⟧, { rw [a_eq.right, b_eq.left] },
have h : (a₂, b₁) ∈ 𝓢 α := quotient.exact h,
simp [function.comp, set.image, comp_rel, and.comm, and.left_comm, and.assoc],
exact ⟨a₁, a_eq.left, b₂, b_eq.right, a₂, ha, b₁, h s hs, hb⟩
end
... = map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧))
(u.uniformity.lift' (λs:set (α×α), comp_rel s (comp_rel s s))) :
by rw [map_lift'_eq];
exact monotone_id.comp_rel (monotone_id.comp_rel monotone_id)
... ≤ map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) u.uniformity :
map_mono comp_le_uniformity3,
is_open_uniformity := assume s,
have ∀a, ⟦a⟧ ∈ s →
({p:α×α | p.1 = a → ⟦p.2⟧ ∈ s} ∈ 𝓤 α ↔
{p:α×α | p.1 ≈ a → ⟦p.2⟧ ∈ s} ∈ 𝓤 α),
from assume a ha,
⟨assume h,
let ⟨t, ht, hts⟩ := comp_mem_uniformity_sets h in
have hts : ∀{a₁ a₂}, (a, a₁) ∈ t → (a₁, a₂) ∈ t → ⟦a₂⟧ ∈ s,
from assume a₁ a₂ ha₁ ha₂, @hts (a, a₂) ⟨a₁, ha₁, ha₂⟩ rfl,
have ht' : ∀{a₁ a₂}, a₁ ≈ a₂ → (a₁, a₂) ∈ t,
from assume a₁ a₂ h, sInter_subset_of_mem ht h,
u.uniformity.sets_of_superset ht $ assume ⟨a₁, a₂⟩ h₁ h₂, hts (ht' $ setoid.symm h₂) h₁,
assume h, u.uniformity.sets_of_superset h $ by simp {contextual := tt}⟩,
begin
simp only [is_open_coinduced, is_open_uniformity, uniformity, forall_quotient_iff,
mem_preimage, mem_map, preimage_set_of_eq, quotient.eq],
exact ⟨λh a ha, (this a ha).mp $ h a ha, λh a ha, (this a ha).mpr $ h a ha⟩
end }
lemma uniformity_quotient :
𝓤 (quotient (separation_setoid α)) = (𝓤 α).map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) :=
rfl
lemma uniform_continuous_quotient_mk :
uniform_continuous (quotient.mk : α → quotient (separation_setoid α)) :=
le_rfl
lemma uniform_continuous_quotient {f : quotient (separation_setoid α) → β}
(hf : uniform_continuous (λx, f ⟦x⟧)) : uniform_continuous f :=
hf
lemma uniform_continuous_quotient_lift
{f : α → β} {h : ∀a b, (a, b) ∈ 𝓢 α → f a = f b}
(hf : uniform_continuous f) : uniform_continuous (λa, quotient.lift f h a) :=
uniform_continuous_quotient hf
lemma uniform_continuous_quotient_lift₂
{f : α → β → γ} {h : ∀a c b d, (a, b) ∈ 𝓢 α → (c, d) ∈ 𝓢 β → f a c = f b d}
(hf : uniform_continuous (λp:α×β, f p.1 p.2)) :
uniform_continuous (λp:_×_, quotient.lift₂ f h p.1 p.2) :=
begin
rw [uniform_continuous, uniformity_prod_eq_prod, uniformity_quotient, uniformity_quotient,
filter.prod_map_map_eq, filter.tendsto_map'_iff, filter.tendsto_map'_iff],
rwa [uniform_continuous, uniformity_prod_eq_prod, filter.tendsto_map'_iff] at hf
end
lemma comap_quotient_le_uniformity :
(𝓤 $ quotient $ separation_setoid α).comap (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) ≤ (𝓤 α) :=
assume t' ht',
let ⟨t, ht, tt_t'⟩ := comp_mem_uniformity_sets ht' in
let ⟨s, hs, ss_t⟩ := comp_mem_uniformity_sets ht in
⟨(λp:α×α, (⟦p.1⟧, ⟦p.2⟧)) '' s,
(𝓤 α).sets_of_superset hs $ assume x hx, ⟨x, hx, rfl⟩,
assume ⟨a₁, a₂⟩ ⟨⟨b₁, b₂⟩, hb, ab_eq⟩,
have ⟦b₁⟧ = ⟦a₁⟧ ∧ ⟦b₂⟧ = ⟦a₂⟧, from prod.mk.inj ab_eq,
have b₁ ≈ a₁ ∧ b₂ ≈ a₂, from and.imp quotient.exact quotient.exact this,
have ab₁ : (a₁, b₁) ∈ t, from (setoid.symm this.left) t ht,
have ba₂ : (b₂, a₂) ∈ s, from this.right s hs,
tt_t' ⟨b₁, show ((a₁, a₂).1, b₁) ∈ t, from ab₁,
ss_t ⟨b₂, show ((b₁, a₂).1, b₂) ∈ s, from hb, ba₂⟩⟩⟩
lemma comap_quotient_eq_uniformity :
(𝓤 $ quotient $ separation_setoid α).comap (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) = 𝓤 α :=
le_antisymm comap_quotient_le_uniformity le_comap_map
instance separated_separation : separated_space (quotient (separation_setoid α)) :=
⟨set.ext $ assume ⟨a, b⟩, quotient.induction_on₂ a b $ assume a b,
⟨assume h,
have a ≈ b, from assume s hs,
have s ∈ (𝓤 $ quotient $ separation_setoid α).comap (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)),
from comap_quotient_le_uniformity hs,
let ⟨t, ht, hts⟩ := this in
hts begin dsimp [preimage], exact h t ht end,
show ⟦a⟧ = ⟦b⟧, from quotient.sound this,
assume heq : ⟦a⟧ = ⟦b⟧, assume h hs,
heq ▸ refl_mem_uniformity hs⟩⟩
lemma separated_of_uniform_continuous {f : α → β} {x y : α}
(H : uniform_continuous f) (h : x ≈ y) : f x ≈ f y :=
assume _ h', h _ (H h')
lemma eq_of_separated_of_uniform_continuous [separated_space β] {f : α → β} {x y : α}
(H : uniform_continuous f) (h : x ≈ y) : f x = f y :=
separated_def.1 (by apply_instance) _ _ $ separated_of_uniform_continuous H h
/-- The maximal separated quotient of a uniform space `α`. -/
def separation_quotient (α : Type*) [uniform_space α] := quotient (separation_setoid α)
namespace separation_quotient
instance : uniform_space (separation_quotient α) := separation_setoid.uniform_space
instance : separated_space (separation_quotient α) := uniform_space.separated_separation
instance [inhabited α] : inhabited (separation_quotient α) :=
quotient.inhabited (separation_setoid α)
lemma mk_eq_mk {x y : α} : (⟦x⟧ : separation_quotient α) = ⟦y⟧ ↔ inseparable x y :=
quotient.eq'.trans separation_rel_iff_inseparable
/-- Factoring functions to a separated space through the separation quotient. -/
def lift [separated_space β] (f : α → β) : (separation_quotient α → β) :=
if h : uniform_continuous f then
quotient.lift f (λ x y, eq_of_separated_of_uniform_continuous h)
else
λ x, f (nonempty.some ⟨x.out⟩)
lemma lift_mk [separated_space β] {f : α → β} (h : uniform_continuous f) (a : α) :
lift f ⟦a⟧ = f a :=
by rw [lift, dif_pos h]; refl
lemma uniform_continuous_lift [separated_space β] (f : α → β) : uniform_continuous (lift f) :=
begin
by_cases hf : uniform_continuous f,
{ rw [lift, dif_pos hf], exact uniform_continuous_quotient_lift hf },
{ rw [lift, dif_neg hf], exact uniform_continuous_of_const (assume a b, rfl) }
end
/-- The separation quotient functor acting on functions. -/
def map (f : α → β) : separation_quotient α → separation_quotient β :=
lift (quotient.mk ∘ f)
lemma map_mk {f : α → β} (h : uniform_continuous f) (a : α) : map f ⟦a⟧ = ⟦f a⟧ :=
by rw [map, lift_mk (uniform_continuous_quotient_mk.comp h)]
lemma uniform_continuous_map (f : α → β) : uniform_continuous (map f) :=
uniform_continuous_lift (quotient.mk ∘ f)
lemma map_unique {f : α → β} (hf : uniform_continuous f)
{g : separation_quotient α → separation_quotient β}
(comm : quotient.mk ∘ f = g ∘ quotient.mk) : map f = g :=
by ext ⟨a⟩;
calc map f ⟦a⟧ = ⟦f a⟧ : map_mk hf a
... = g ⟦a⟧ : congr_fun comm a
lemma map_id : map (@id α) = id :=
map_unique uniform_continuous_id rfl
lemma map_comp {f : α → β} {g : β → γ} (hf : uniform_continuous f) (hg : uniform_continuous g) :
map g ∘ map f = map (g ∘ f) :=
(map_unique (hg.comp hf) $ by simp only [(∘), map_mk, hf, hg]).symm
end separation_quotient
lemma separation_prod {a₁ a₂ : α} {b₁ b₂ : β} : (a₁, b₁) ≈ (a₂, b₂) ↔ a₁ ≈ a₂ ∧ b₁ ≈ b₂ :=
begin
split,
{ assume h,
exact ⟨separated_of_uniform_continuous uniform_continuous_fst h,
separated_of_uniform_continuous uniform_continuous_snd h⟩ },
{ rintros ⟨eqv_α, eqv_β⟩ r r_in,
rw uniformity_prod at r_in,
rcases r_in with ⟨t_α, ⟨r_α, r_α_in, h_α⟩, t_β, ⟨r_β, r_β_in, h_β⟩, rfl⟩,
let p_α := λ(p : (α × β) × (α × β)), (p.1.1, p.2.1),
let p_β := λ(p : (α × β) × (α × β)), (p.1.2, p.2.2),
have key_α : p_α ((a₁, b₁), (a₂, b₂)) ∈ r_α, { simp [p_α, eqv_α r_α r_α_in] },
have key_β : p_β ((a₁, b₁), (a₂, b₂)) ∈ r_β, { simp [p_β, eqv_β r_β r_β_in] },
exact ⟨h_α key_α, h_β key_β⟩ },
end
instance separated.prod [separated_space α] [separated_space β] : separated_space (α × β) :=
separated_def.2 $ assume x y H, prod.ext
(eq_of_separated_of_uniform_continuous uniform_continuous_fst H)
(eq_of_separated_of_uniform_continuous uniform_continuous_snd H)
end uniform_space
|
02200be9fcb087cda1779669d58c3ac90d6cd8ce | d6124c8dbe5661dcc5b8c9da0a56fbf1f0480ad6 | /Papyrus/Init.lean | 1234373ad22e33253d1a9952445c7a72f02ca08f | [
"Apache-2.0"
] | permissive | xubaiw/lean4-papyrus | c3fbbf8ba162eb5f210155ae4e20feb2d32c8182 | 02e82973a5badda26fc0f9fd15b3d37e2eb309e0 | refs/heads/master | 1,691,425,756,824 | 1,632,122,825,000 | 1,632,123,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,704 | lean | namespace Papyrus
-- # All Target Initialization
/-- Initializes all LLVM supported targets. -/
@[extern "papyrus_init_all_targets"]
constant initAllTargets : IO PUnit
/-- Initializes all LLVM supported target MCs. -/
@[extern "papyrus_init_all_target_mcs"]
constant initAllTargetMCs : IO PUnit
/-- Initializes all LLVM supported target infos. -/
@[extern "papyrus_init_all_target_infos"]
constant initAllTargetInfos : IO PUnit
/-- Initializes all LLVM supported target assembly parsers. -/
@[extern "papyrus_init_all_asm_parsers"]
constant initAllAsmParsers : IO PUnit
/-- Initializes all LLVM supported target assembly printers. -/
@[extern "papyrus_init_all_asm_printers"]
constant initAllAsmPrinters : IO PUnit
/-- Initializes all LLVM supported target disassemblers. -/
@[extern "papyrus_init_all_disassemblers"]
constant initAllDisassemblers : IO PUnit
-- # Native Target Initialization
/--
Initializes the native target along with its MC and Info.
Returns true if no native target exists, false otherwise.
-/
@[extern "papyrus_init_native_target"]
constant initNativeTarget : IO Bool
/--
Initializes the native target's ASM parser.
Returns true if no native target exists, false otherwise.
-/
@[extern "papyrus_init_native_asm_parser"]
constant initNativeAsmParser : IO Bool
/--
Initializes the native target's ASM printer.
Returns true if no native target exists, false otherwise.
-/
@[extern "papyrus_init_native_asm_printer"]
constant initNativeAsmPrinter : IO Bool
/--
Initializes the native target's disassembler.
Returns true if no native target exists, false otherwise.
-/
@[extern "papyrus_init_native_disassembler"]
constant initNativeDisassembler : IO Bool
|
73a21a0ff71ff3b30586c9861156f5065fc6ffad | 367134ba5a65885e863bdc4507601606690974c1 | /src/category_theory/monad/algebra.lean | 923043ea1934158fb1eaf4230fbc84dd92ecd0e8 | [
"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 | 11,620 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Bhavik Mehta
-/
import category_theory.monad.basic
import category_theory.adjunction.basic
import category_theory.reflects_isomorphisms
/-!
# Eilenberg-Moore (co)algebras for a (co)monad
This file defines Eilenberg-Moore (co)algebras for a (co)monad,
and provides the category instance for them.
Further it defines the adjoint pair of free and forgetful functors, respectively
from and to the original category, as well as the adjoint pair of forgetful and
cofree functors, respectively from and to the original category.
## References
* [Riehl, *Category theory in context*, Section 5.2.4][riehl2017]
-/
namespace category_theory
open category
universes v₁ u₁ -- declare the `v`'s first; see `category_theory.category` for an explanation
variables {C : Type u₁} [category.{v₁} C]
namespace monad
/-- An Eilenberg-Moore algebra for a monad `T`.
cf Definition 5.2.3 in [Riehl][riehl2017]. -/
structure algebra (T : monad C) : Type (max u₁ v₁) :=
(A : C)
(a : (T : C ⥤ C).obj A ⟶ A)
(unit' : T.η.app A ≫ a = 𝟙 A . obviously)
(assoc' : T.μ.app A ≫ a = (T : C ⥤ C).map a ≫ a . obviously)
restate_axiom algebra.unit'
restate_axiom algebra.assoc'
attribute [reassoc] algebra.unit algebra.assoc
namespace algebra
variables {T : monad C}
/-- A morphism of Eilenberg–Moore algebras for the monad `T`. -/
@[ext] structure hom (A B : algebra T) :=
(f : A.A ⟶ B.A)
(h' : T.map f ≫ B.a = A.a ≫ f . obviously)
restate_axiom hom.h'
attribute [simp, reassoc] hom.h
namespace hom
/-- The identity homomorphism for an Eilenberg–Moore algebra. -/
def id (A : algebra T) : hom A A :=
{ f := 𝟙 A.A }
instance (A : algebra T) : inhabited (hom A A) := ⟨{ f := 𝟙 _ }⟩
/-- Composition of Eilenberg–Moore algebra homomorphisms. -/
def comp {P Q R : algebra T} (f : hom P Q) (g : hom Q R) : hom P R :=
{ f := f.f ≫ g.f }
end hom
instance : category_struct (algebra T) :=
{ hom := hom,
id := hom.id,
comp := @hom.comp _ _ _ }
@[simp] lemma comp_eq_comp {A A' A'' : algebra T} (f : A ⟶ A') (g : A' ⟶ A'') :
algebra.hom.comp f g = f ≫ g := rfl
@[simp] lemma id_eq_id (A : algebra T) :
algebra.hom.id A = 𝟙 A := rfl
@[simp] lemma id_f (A : algebra T) : (𝟙 A : A ⟶ A).f = 𝟙 A.A := rfl
@[simp] lemma comp_f {A A' A'' : algebra T} (f : A ⟶ A') (g : A' ⟶ A'') :
(f ≫ g).f = f.f ≫ g.f := rfl
/-- The category of Eilenberg-Moore algebras for a monad.
cf Definition 5.2.4 in [Riehl][riehl2017]. -/
instance EilenbergMoore : category (algebra T) := {}.
/--
To construct an isomorphism of algebras, it suffices to give an isomorphism of the carriers which
commutes with the structure morphisms.
-/
@[simps]
def iso_mk {A B : algebra T} (h : A.A ≅ B.A) (w : T.map h.hom ≫ B.a = A.a ≫ h.hom) : A ≅ B :=
{ hom := { f := h.hom },
inv :=
{ f := h.inv,
h' := by { rw [h.eq_comp_inv, category.assoc, ←w, ←functor.map_comp_assoc], simp } } }
end algebra
variables (T : monad C)
/-- The forgetful functor from the Eilenberg-Moore category, forgetting the algebraic structure. -/
@[simps] def forget : algebra T ⥤ C :=
{ obj := λ A, A.A,
map := λ A B f, f.f }
/-- The free functor from the Eilenberg-Moore category, constructing an algebra for any object. -/
@[simps] def free : C ⥤ algebra T :=
{ obj := λ X,
{ A := T.obj X,
a := T.μ.app X,
assoc' := (T.assoc _).symm },
map := λ X Y f,
{ f := T.map f,
h' := T.μ.naturality _ } }
instance [inhabited C] : inhabited (algebra T) :=
⟨(free T).obj (default C)⟩
/-- The adjunction between the free and forgetful constructions for Eilenberg-Moore algebras for
a monad. cf Lemma 5.2.8 of [Riehl][riehl2017]. -/
-- The other two `simps` projection lemmas can be derived from these two, so `simp_nf` complains if
-- those are added too
@[simps unit counit {rhs_md := semireducible}]
def adj : T.free ⊣ T.forget :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ X Y,
{ to_fun := λ f, T.η.app X ≫ f.f,
inv_fun := λ f,
{ f := T.map f ≫ Y.a,
h' := by { dsimp, simp [←Y.assoc, ←T.μ.naturality_assoc] } },
left_inv := λ f, by { ext, dsimp, simp },
right_inv := λ f,
begin
dsimp only [forget_obj, monad_to_functor_eq_coe],
rw [←T.η.naturality_assoc, Y.unit],
apply category.comp_id,
end }}
/--
Given an algebra morphism whose carrier part is an isomorphism, we get an algebra isomorphism.
-/
def algebra_iso_of_iso {A B : algebra T} (f : A ⟶ B) [is_iso f.f] : is_iso f :=
{ inv :=
{ f := inv f.f,
h' := by { rw [is_iso.eq_comp_inv f.f, category.assoc, ← f.h], dsimp, simp } } }
instance forget_reflects_iso : reflects_isomorphisms (forget T) :=
{ reflects := λ A B, algebra_iso_of_iso T }
instance forget_faithful : faithful (forget T) := {}
/--
Given a monad morphism from `T₂` to `T₁`, we get a functor from the algebras of `T₁` to algebras of
`T₂`.
-/
@[simps]
def algebra_functor_of_monad_hom {T₁ T₂ : monad C} (h : T₂ ⟶ T₁) :
algebra T₁ ⥤ algebra T₂ :=
{ obj := λ A,
{ A := A.A,
a := h.app A.A ≫ A.a,
unit' := by { dsimp, simp [A.unit] },
assoc' := by { dsimp, simp [A.assoc] } },
map := λ A₁ A₂ f,
{ f := f.f } }
/--
The identity monad morphism induces the identity functor from the category of algebras to itself.
-/
@[simps {rhs_md := semireducible}]
def algebra_functor_of_monad_hom_id {T₁ : monad C} :
algebra_functor_of_monad_hom (𝟙 T₁) ≅ 𝟭 _ :=
nat_iso.of_components
(λ X, algebra.iso_mk (iso.refl _) (by { dsimp, simp, }))
(λ X Y f, by { ext, dsimp, simp })
/--
A composition of monad morphisms gives the composition of corresponding functors.
-/
@[simps {rhs_md := semireducible}]
def algebra_functor_of_monad_hom_comp {T₁ T₂ T₃ : monad C} (f : T₁ ⟶ T₂) (g : T₂ ⟶ T₃) :
algebra_functor_of_monad_hom (f ≫ g) ≅
algebra_functor_of_monad_hom g ⋙ algebra_functor_of_monad_hom f :=
nat_iso.of_components
(λ X, algebra.iso_mk (iso.refl _) (by { dsimp, simp }))
(λ X Y f, by { ext, dsimp, simp })
/--
If `f` and `g` are two equal morphisms of monads, then the functors of algebras induced by them
are isomorphic.
We define it like this as opposed to using `eq_to_iso` so that the components are nicer to prove
lemmas about.
-/
@[simps {rhs_md := semireducible}]
def algebra_functor_of_monad_hom_eq {T₁ T₂ : monad C} {f g : T₁ ⟶ T₂} (h : f = g) :
algebra_functor_of_monad_hom f ≅ algebra_functor_of_monad_hom g :=
nat_iso.of_components
(λ X, algebra.iso_mk (iso.refl _) (by { dsimp, simp [h] }))
(λ X Y f, by { ext, dsimp, simp })
/--
Isomorphic monads give equivalent categories of algebras. Furthermore, they are equivalent as
categories over `C`, that is, we have `algebra_equiv_of_iso_monads h ⋙ forget = forget`.
-/
@[simps]
def algebra_equiv_of_iso_monads {T₁ T₂ : monad C} (h : T₁ ≅ T₂) :
algebra T₁ ≌ algebra T₂ :=
{ functor := algebra_functor_of_monad_hom h.inv,
inverse := algebra_functor_of_monad_hom h.hom,
unit_iso :=
algebra_functor_of_monad_hom_id.symm ≪≫
algebra_functor_of_monad_hom_eq (by simp) ≪≫
algebra_functor_of_monad_hom_comp _ _,
counit_iso :=
(algebra_functor_of_monad_hom_comp _ _).symm ≪≫
algebra_functor_of_monad_hom_eq (by simp) ≪≫
algebra_functor_of_monad_hom_id }
@[simp] lemma algebra_equiv_of_iso_monads_comp_forget {T₁ T₂ : monad C} (h : T₁ ⟶ T₂) :
algebra_functor_of_monad_hom h ⋙ forget _ = forget _ :=
rfl
end monad
namespace comonad
/-- An Eilenberg-Moore coalgebra for a comonad `T`. -/
@[nolint has_inhabited_instance]
structure coalgebra (G : comonad C) : Type (max u₁ v₁) :=
(A : C)
(a : A ⟶ G.obj A)
(counit' : a ≫ G.ε.app A = 𝟙 A . obviously)
(coassoc' : a ≫ G.δ.app A = a ≫ G.map a . obviously)
restate_axiom coalgebra.counit'
restate_axiom coalgebra.coassoc'
attribute [reassoc] coalgebra.counit coalgebra.coassoc
namespace coalgebra
variables {G : comonad C}
/-- A morphism of Eilenberg-Moore coalgebras for the comonad `G`. -/
@[ext, nolint has_inhabited_instance] structure hom (A B : coalgebra G) :=
(f : A.A ⟶ B.A)
(h' : A.a ≫ G.map f = f ≫ B.a . obviously)
restate_axiom hom.h'
attribute [simp, reassoc] hom.h
namespace hom
/-- The identity homomorphism for an Eilenberg–Moore coalgebra. -/
def id (A : coalgebra G) : hom A A :=
{ f := 𝟙 A.A }
/-- Composition of Eilenberg–Moore coalgebra homomorphisms. -/
def comp {P Q R : coalgebra G} (f : hom P Q) (g : hom Q R) : hom P R :=
{ f := f.f ≫ g.f }
end hom
/-- The category of Eilenberg-Moore coalgebras for a comonad. -/
instance : category_struct (coalgebra G) :=
{ hom := hom,
id := hom.id,
comp := @hom.comp _ _ _ }
@[simp] lemma comp_eq_comp {A A' A'' : coalgebra G} (f : A ⟶ A') (g : A' ⟶ A'') :
coalgebra.hom.comp f g = f ≫ g := rfl
@[simp] lemma id_eq_id (A : coalgebra G) :
coalgebra.hom.id A = 𝟙 A := rfl
@[simp] lemma id_f (A : coalgebra G) : (𝟙 A : A ⟶ A).f = 𝟙 A.A := rfl
@[simp] lemma comp_f {A A' A'' : coalgebra G} (f : A ⟶ A') (g : A' ⟶ A'') :
(f ≫ g).f = f.f ≫ g.f := rfl
/-- The category of Eilenberg-Moore coalgebras for a comonad. -/
instance EilenbergMoore : category (coalgebra G) := {}.
/--
To construct an isomorphism of coalgebras, it suffices to give an isomorphism of the carriers which
commutes with the structure morphisms.
-/
@[simps]
def iso_mk {A B : coalgebra G} (h : A.A ≅ B.A) (w : A.a ≫ G.map h.hom = h.hom ≫ B.a) : A ≅ B :=
{ hom := { f := h.hom },
inv :=
{ f := h.inv,
h' := by { rw [h.eq_inv_comp, ←reassoc_of w, ←functor.map_comp], simp } } }
end coalgebra
variables (G : comonad C)
/-- The forgetful functor from the Eilenberg-Moore category, forgetting the coalgebraic
structure. -/
@[simps] def forget : coalgebra G ⥤ C :=
{ obj := λ A, A.A,
map := λ A B f, f.f }
/--
Given a coalgebra morphism whose carrier part is an isomorphism, we get a coalgebra isomorphism.
-/
def coalgebra_iso_of_iso {A B : coalgebra G} (f : A ⟶ B) [is_iso f.f] : is_iso f :=
{ inv :=
{ f := inv f.f,
h' := by { rw [is_iso.eq_inv_comp f.f, ←f.h_assoc], dsimp, simp } } }
instance forget_reflects_iso : reflects_isomorphisms (forget G) :=
{ reflects := λ A B, coalgebra_iso_of_iso G }
/-- The cofree functor from the Eilenberg-Moore category, constructing a coalgebra for any
object. -/
@[simps] def cofree : C ⥤ coalgebra G :=
{ obj := λ X,
{ A := G.obj X,
a := G.δ.app X,
coassoc' := (G.coassoc _).symm },
map := λ X Y f,
{ f := G.map f,
h' := (G.δ.naturality _).symm } }
/--
The adjunction between the cofree and forgetful constructions for Eilenberg-Moore coalgebras
for a comonad.
-/
-- The other two `simps` projection lemmas can be derived from these two, so `simp_nf` complains if
-- those are added too
@[simps unit counit]
def adj : forget G ⊣ cofree G :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ X Y,
{ to_fun := λ f,
{ f := X.a ≫ G.map f,
h' := by { dsimp, simp [←coalgebra.coassoc_assoc] } },
inv_fun := λ g, g.f ≫ G.ε.app Y,
left_inv := λ f,
by { dsimp, rw [category.assoc, G.ε.naturality, functor.id_map, X.counit_assoc] },
right_inv := λ g,
begin
ext1, dsimp,
rw [functor.map_comp, g.h_assoc, cofree_obj_a, comonad.right_counit],
apply comp_id,
end }}
instance forget_faithful : faithful (forget G) := {}
end comonad
end category_theory
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.