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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
81d493fc136f42c10edad9bb550debaa584a1a0b | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /hott/algebra/category/constructions/hset.hlean | 27bdef664a9031e004ea42ea97fe034ff36be3fe | [
"Apache-2.0"
] | permissive | davidmueller13/lean | 65a3ed141b4088cd0a268e4de80eb6778b21a0e9 | c626e2e3c6f3771e07c32e82ee5b9e030de5b050 | refs/heads/master | 1,611,278,313,401 | 1,444,021,177,000 | 1,444,021,177,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,324 | 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
Category of hsets
-/
import ..category types.equiv ..functor types.lift ..limits
open eq category equiv iso is_equiv is_trunc function sigma
namespace category
definition precategory_hset.{u} [reducible] [constructor] : precategory hset.{u} :=
precategory.mk (λx y : hset, x → y)
(λx y z g f a, g (f a))
(λx a, a)
(λx y z w h g f, eq_of_homotopy (λa, idp))
(λx y f, eq_of_homotopy (λa, idp))
(λx y f, eq_of_homotopy (λa, idp))
definition Precategory_hset [reducible] [constructor] : Precategory :=
Precategory.mk hset precategory_hset
namespace set
local attribute is_equiv_subtype_eq [instance]
definition iso_of_equiv [constructor] {A B : Precategory_hset} (f : A ≃ B) : A ≅ B :=
iso.MK (to_fun f)
(to_inv f)
(eq_of_homotopy (left_inv (to_fun f)))
(eq_of_homotopy (right_inv (to_fun f)))
definition equiv_of_iso [constructor] {A B : Precategory_hset} (f : A ≅ B) : A ≃ B :=
begin
apply equiv.MK (to_hom f) (iso.to_inv f),
exact ap10 (to_right_inverse f),
exact ap10 (to_left_inverse f)
end
definition is_equiv_iso_of_equiv [constructor] (A B : Precategory_hset)
: is_equiv (@iso_of_equiv A B) :=
adjointify _ (λf, equiv_of_iso f)
(λf, proof iso_eq idp qed)
(λf, equiv_eq idp)
local attribute is_equiv_iso_of_equiv [instance]
definition iso_of_eq_eq_compose (A B : hset) : @iso_of_eq _ _ A B =
@iso_of_equiv A B ∘ @equiv_of_eq A B ∘ subtype_eq_inv _ _ ∘
@ap _ _ (to_fun (trunctype.sigma_char 0)) A B :=
eq_of_homotopy (λp, eq.rec_on p idp)
definition equiv_equiv_iso (A B : Precategory_hset) : (A ≃ B) ≃ (A ≅ B) :=
equiv.MK (λf, iso_of_equiv f)
(λf, proof equiv.MK (to_hom f)
(iso.to_inv f)
(ap10 (to_right_inverse f))
(ap10 (to_left_inverse f)) qed)
(λf, proof iso_eq idp qed)
(λf, proof equiv_eq idp qed)
definition equiv_eq_iso (A B : Precategory_hset) : (A ≃ B) = (A ≅ B) :=
ua !equiv_equiv_iso
definition is_univalent_hset (A B : Precategory_hset) : is_equiv (iso_of_eq : A = B → A ≅ B) :=
assert H₁ : is_equiv (@iso_of_equiv A B ∘ @equiv_of_eq A B ∘ subtype_eq_inv _ _ ∘
@ap _ _ (to_fun (trunctype.sigma_char 0)) A B), from
@is_equiv_compose _ _ _ _ _
(@is_equiv_compose _ _ _ _ _
(@is_equiv_compose _ _ _ _ _
_
(@is_equiv_subtype_eq_inv _ _ _ _ _))
!univalence)
!is_equiv_iso_of_equiv,
let H₂ := (iso_of_eq_eq_compose A B)⁻¹ in
begin
rewrite H₂ at H₁,
assumption
end
end set
definition category_hset [instance] [constructor] : category hset :=
category.mk precategory_hset set.is_univalent_hset
definition Category_hset [reducible] [constructor] : Category :=
Category.mk hset category_hset
abbreviation set [constructor] := Category_hset
open functor lift
definition lift_functor.{u v} [constructor] : set.{u} ⇒ set.{max u v} :=
functor.mk tlift
(λa b, lift_functor)
(λa, eq_of_homotopy (λx, by induction x; reflexivity))
(λa b c g f, eq_of_homotopy (λx, by induction x; reflexivity))
open pi sigma.ops
local attribute Category.to.precategory [unfold 1]
local attribute category.to_precategory [unfold 2]
definition is_complete_set_cone.{u v w} [constructor]
(I : Precategory.{v w}) (F : I ⇒ set.{max u v w}) : cone_obj F :=
begin
fapply cone_obj.mk,
{ fapply trunctype.mk,
{ exact Σ(s : Π(i : I), trunctype.carrier (F i)),
Π{i j : I} (f : i ⟶ j), F f (s i) = (s j)},
{ exact abstract begin apply is_trunc_sigma, intro s,
apply is_trunc_pi, intro i,
apply is_trunc_pi, intro j,
apply is_trunc_pi, intro f,
apply is_trunc_eq end end}},
{ fapply nat_trans.mk,
{ intro i x, esimp at x, exact x.1 i},
{ intro i j f, esimp, apply eq_of_homotopy, intro x, esimp at x, induction x with s p,
esimp, apply p}}
end
definition is_complete_set.{u v w} [instance] : is_complete.{(max u v w)+1 (max u v w) v w} set :=
begin
intro I F, fapply has_terminal_object.mk,
{ exact is_complete_set_cone.{u v w} I F},
{ intro c, esimp at *, induction c with X η, induction η with η p, esimp at *,
fapply is_contr.mk,
{ fapply cone_hom.mk,
{ intro x, esimp at *, fapply sigma.mk,
{ intro i, exact η i x},
{ intro i j f, exact ap10 (p f) x}},
{ intro i, reflexivity}},
{ esimp, intro h, induction h with f q, apply cone_hom_eq, esimp at *,
apply eq_of_homotopy, intro x, fapply sigma_eq: esimp,
{ apply eq_of_homotopy, intro i, exact (ap10 (q i) x)⁻¹},
{ apply is_hprop.elimo,
apply is_trunc_pi, intro i,
apply is_trunc_pi, intro j,
apply is_trunc_pi, intro f,
apply is_trunc_eq}}}
end
end category
|
b58a378b33ede54d397149e078847202160881ff | a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7 | /src/data/indicator_function.lean | e083524b14fe5bc6c5a09f61fec97505f69c9fc2 | [
"Apache-2.0"
] | permissive | kmill/mathlib | ea5a007b67ae4e9e18dd50d31d8aa60f650425ee | 1a419a9fea7b959317eddd556e1bb9639f4dcc05 | refs/heads/master | 1,668,578,197,719 | 1,593,629,163,000 | 1,593,629,163,000 | 276,482,939 | 0 | 0 | null | 1,593,637,960,000 | 1,593,637,959,000 | null | UTF-8 | Lean | false | false | 8,373 | lean | /-
Copyright (c) 2020 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou
-/
import algebra.pi_instances
import data.set.disjointed
import data.support
/-!
# Indicator function
`indicator (s : set α) (f : α → β) (a : α)` is `f a` if `a ∈ s` and is `0` otherwise.
## Implementation note
In mathematics, an indicator function or a characteristic function is a function used to indicate
membership of an element in a set `s`, having the value `1` for all elements of `s` and the value `0`
otherwise. But since it is usually used to restrict a function to a certain set `s`, we let the
indicator function take the value `f x` for some function `f`, instead of `1`. If the usual indicator
function is needed, just set `f` to be the constant function `λx, 1`.
## Tags
indicator, characteristic
-/
noncomputable theory
open_locale classical big_operators
namespace set
universes u v
variables {α : Type u} {β : Type v}
section has_zero
variables [has_zero β] {s t : set α} {f g : α → β} {a : α}
/-- `indicator s f a` is `f a` if `a ∈ s`, `0` otherwise. -/
@[reducible]
def indicator (s : set α) (f : α → β) : α → β := λ x, if x ∈ s then f x else 0
lemma indicator_apply (s : set α) (f : α → β) (a : α) :
indicator s f a = if a ∈ s then f a else 0 := rfl
@[simp] lemma indicator_of_mem (h : a ∈ s) (f : α → β) : indicator s f a = f a := if_pos h
@[simp] lemma indicator_of_not_mem (h : a ∉ s) (f : α → β) : indicator s f a = 0 := if_neg h
lemma eq_on_indicator : eq_on (indicator s f) f s := λ x hx, indicator_of_mem hx f
lemma support_indicator : function.support (s.indicator f) ⊆ s :=
λ x hx, hx.imp_symm (λ h, indicator_of_not_mem h f)
lemma indicator_congr (h : ∀ a ∈ s, f a = g a) : indicator s f = indicator s g :=
funext $ λx, by { simp only [indicator], split_ifs, { exact h _ h_1 }, refl }
@[simp] lemma indicator_univ (f : α → β) : indicator (univ : set α) f = f :=
funext $ λx, indicator_of_mem (mem_univ _) f
@[simp] lemma indicator_empty (f : α → β) : indicator (∅ : set α) f = λa, 0 :=
funext $ λx, indicator_of_not_mem (not_mem_empty _) f
variable (β)
@[simp] lemma indicator_zero (s : set α) : indicator s (λx, (0:β)) = λx, (0:β) :=
funext $ λx, by { simp only [indicator], split_ifs, refl, refl }
variable {β}
lemma indicator_indicator (s t : set α) (f : α → β) : indicator s (indicator t f) = indicator (s ∩ t) f :=
funext $ λx, by { simp only [indicator], split_ifs, repeat {simp * at * {contextual := tt}} }
lemma indicator_comp_of_zero {γ} [has_zero γ] {g : β → γ} (hg : g 0 = 0) :
indicator s (g ∘ f) = λ a, indicator (f '' s) g (indicator s f a) :=
begin
funext, simp only [indicator],
split_ifs with h h',
{ refl },
{ have := mem_image_of_mem _ h, contradiction },
{ rwa eq_comm },
refl
end
lemma indicator_preimage (s : set α) (f : α → β) (B : set β) :
(indicator s f)⁻¹' B = s ∩ f ⁻¹' B ∪ sᶜ ∩ (λa:α, (0:β)) ⁻¹' B :=
by { rw [indicator, if_preimage] }
lemma indicator_preimage_of_not_mem (s : set α) (f : α → β) {t : set β} (ht : (0:β) ∉ t) :
(indicator s f)⁻¹' t = s ∩ f ⁻¹' t :=
by simp [indicator_preimage, set.preimage_const_of_not_mem ht]
lemma mem_range_indicator {r : β} {s : set α} {f : α → β} :
r ∈ range (indicator s f) ↔ (r = 0 ∧ s ≠ univ) ∨ (r ∈ f '' s) :=
by simp [indicator, ite_eq_iff, exists_or_distrib, eq_univ_iff_forall, and_comm, or_comm,
@eq_comm _ r 0]
end has_zero
section add_monoid
variables [add_monoid β] {s t : set α} {f g : α → β} {a : α}
lemma indicator_union_of_not_mem_inter (h : a ∉ s ∩ t) (f : α → β) :
indicator (s ∪ t) f a = indicator s f a + indicator t f a :=
by { simp only [indicator], split_ifs, repeat {simp * at * {contextual := tt}} }
lemma indicator_union_of_disjoint (h : disjoint s t) (f : α → β) :
indicator (s ∪ t) f = λa, indicator s f a + indicator t f a :=
funext $ λa, indicator_union_of_not_mem_inter
(by { convert not_mem_empty a, have := disjoint.eq_bot h, assumption })
_
lemma indicator_add (s : set α) (f g : α → β) :
indicator s (λa, f a + g a) = λa, indicator s f a + indicator s g a :=
by { funext, simp only [indicator], split_ifs, { refl }, rw add_zero }
variables (β)
instance is_add_monoid_hom.indicator (s : set α) : is_add_monoid_hom (λf:α → β, indicator s f) :=
{ map_add := λ _ _, indicator_add _ _ _,
map_zero := indicator_zero _ _ }
variables {β} {𝕜 : Type*} [monoid 𝕜] [distrib_mul_action 𝕜 β]
lemma indicator_smul (s : set α) (r : 𝕜) (f : α → β) :
indicator s (λ (x : α), r • f x) = λ (x : α), r • indicator s f x :=
by { simp only [indicator], funext, split_ifs, refl, exact (smul_zero r).symm }
end add_monoid
section add_group
variables [add_group β] {s t : set α} {f g : α → β} {a : α}
variables (β)
instance is_add_group_hom.indicator (s : set α) : is_add_group_hom (λf:α → β, indicator s f) :=
{ .. is_add_monoid_hom.indicator β s }
variables {β}
lemma indicator_neg (s : set α) (f : α → β) : indicator s (λa, - f a) = λa, - indicator s f a :=
show indicator s (- f) = - indicator s f, from is_add_group_hom.map_neg _ _
lemma indicator_sub (s : set α) (f g : α → β) :
indicator s (λa, f a - g a) = λa, indicator s f a - indicator s g a :=
show indicator s (f - g) = indicator s f - indicator s g, from is_add_group_hom.map_sub _ _ _
lemma indicator_compl (s : set α) (f : α → β) : indicator sᶜ f = λ a, f a - indicator s f a :=
begin
funext,
simp only [indicator],
split_ifs with h₁ h₂,
{ rw sub_zero },
{ rw sub_self },
{ rw ← mem_compl_iff at h₂, contradiction }
end
lemma indicator_finset_sum {β} [add_comm_monoid β] {ι : Type*} (I : finset ι) (s : set α) (f : ι → α → β) :
indicator s (∑ i in I, f i) = ∑ i in I, indicator s (f i) :=
begin
convert (finset.sum_hom _ _).symm,
split,
exact indicator_zero _ _
end
lemma indicator_finset_bUnion {β} [add_comm_monoid β] {ι} (I : finset ι)
(s : ι → set α) {f : α → β} : (∀ (i ∈ I) (j ∈ I), i ≠ j → s i ∩ s j = ∅) →
indicator (⋃ i ∈ I, s i) f = λ a, ∑ i in I, indicator (s i) f a :=
begin
refine finset.induction_on I _ _,
assume h,
{ funext, simp },
assume a I haI ih hI,
funext,
simp only [haI, finset.sum_insert, not_false_iff],
rw [finset.bUnion_insert, indicator_union_of_not_mem_inter, ih _],
{ assume i hi j hj hij,
exact hI i (finset.mem_insert_of_mem hi) j (finset.mem_insert_of_mem hj) hij },
simp only [not_exists, exists_prop, mem_Union, mem_inter_eq, not_and],
assume hx a' ha',
have := hI a (finset.mem_insert_self _ _) a' (finset.mem_insert_of_mem ha') _,
{ assume h, have h := mem_inter hx h, rw this at h, exact not_mem_empty _ h },
{ assume h, rw h at haI, contradiction }
end
end add_group
section mul_zero_class
variables [mul_zero_class β] {s t : set α} {f g : α → β} {a : α}
lemma indicator_mul (s : set α) (f g : α → β) :
indicator s (λa, f a * g a) = λa, indicator s f a * indicator s g a :=
by { funext, simp only [indicator], split_ifs, { refl }, rw mul_zero }
end mul_zero_class
section order
variables [has_zero β] [preorder β] {s t : set α} {f g : α → β} {a : α}
lemma indicator_nonneg' (h : a ∈ s → 0 ≤ f a) : 0 ≤ indicator s f a :=
by { rw indicator_apply, split_ifs with as, { exact h as }, refl }
lemma indicator_nonneg (h : ∀ a ∈ s, 0 ≤ f a) : ∀ a, 0 ≤ indicator s f a :=
λ a, indicator_nonneg' (h a)
lemma indicator_nonpos' (h : a ∈ s → f a ≤ 0) : indicator s f a ≤ 0 :=
by { rw indicator_apply, split_ifs with as, { exact h as }, refl }
lemma indicator_nonpos (h : ∀ a ∈ s, f a ≤ 0) : ∀ a, indicator s f a ≤ 0 :=
λ a, indicator_nonpos' (h a)
lemma indicator_le_indicator (h : f a ≤ g a) : indicator s f a ≤ indicator s g a :=
by { simp only [indicator], split_ifs with ha, { exact h }, refl }
lemma indicator_le_indicator_of_subset (h : s ⊆ t) (hf : ∀a, 0 ≤ f a) (a : α) :
indicator s f a ≤ indicator t f a :=
begin
simp only [indicator],
split_ifs with h₁,
{ refl },
{ have := h h₁, contradiction },
{ exact hf a },
{ refl }
end
end order
end set
|
9a98905926c9f8abe1b38028ec768e2f67083214 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /stage0/src/Std/Dynamic.lean | 8d6d390cf12fb6db69df129e8180e93209f8fc12 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 2,831 | lean | /-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gabriel Ebner
-/
namespace Std
open Lean
-- Implementation detail of TypeName, since classes cannot be opaque
private opaque TypeNameData (α : Type u) : NonemptyType.{0} :=
⟨Name, inferInstance⟩
/--
Dynamic type name information.
Types with an instance of `TypeName` can be stored in an `Dynamic`.
The type class contains the declaration name of the type,
which must not have any universe parameters
and be of type `Sort ..` (i.e., monomorphic).
The preferred way to declare instances of this type is using the derive
handler, which will internally use the unsafe `TypeName.mk` function.
Morally, this is the same as:
```lean
class TypeName (α : Type) where unsafe mk ::
typeName : Name
```
-/
@[nospecialize]
class TypeName (α : Type u) where private mk' ::
private data : (TypeNameData α).type
instance : Nonempty (TypeName α) := (TypeNameData α).property.elim (⟨⟨·⟩⟩)
/--
Creates a `TypeName` instance.
For safety, it is required that the constant `typeName` is definitionally equal
to `α`.
-/
unsafe def TypeName.mk (α : Type u) (typeName : Name) : TypeName α :=
⟨unsafeCast typeName⟩
private unsafe def TypeName.typeNameImpl (α) [TypeName α] : Name :=
unsafeCast (@TypeName.data α _)
/--
Returns a declaration name of the type.
-/
@[implementedBy TypeName.typeNameImpl]
opaque TypeName.typeName (α) [TypeName α] : Name
private opaque DynamicPointed : NonemptyType.{0} :=
⟨Name × NonScalar, inferInstance⟩
/--
Type-tagged union that can store any type with a `TypeName` instance.
This is roughly equivalent to `(α : Type) × TypeName α × α` but without the
universe bump.
-/
def Dynamic : Type := DynamicPointed.type
instance : Nonempty Dynamic := DynamicPointed.property
private unsafe def Dynamic.typeNameImpl (any : Dynamic) : Name :=
(unsafeCast any : Name × NonScalar).1
/--
The name of the type of the value stored in the `Dynamic`.
-/
@[implementedBy Dynamic.typeNameImpl]
opaque Dynamic.typeName (any : Dynamic) : Name
private unsafe def Dynamic.get?Impl (α) (any : Dynamic) [TypeName α] : Option α :=
let ((typeName, obj) : Name × NonScalar) := unsafeCast any
if typeName == TypeName.typeName α then
some (unsafeCast obj)
else
none
/--
Retrieves the value stored in the `Dynamic`.
Returns `some a` if the value has the right type, and `none` otherwise.
-/
@[implementedBy Dynamic.get?Impl]
opaque Dynamic.get? (α) (any : Dynamic) [TypeName α] : Option α
private unsafe def Dynamic.mkImpl [TypeName α] (obj : α) : Dynamic :=
unsafeCast (TypeName.typeName α, (unsafeCast obj : NonScalar))
@[implementedBy Dynamic.mkImpl]
opaque Dynamic.mk [TypeName α] (obj : α) : Dynamic
|
8f94e740d47cf83d84a0c9f47ba9c1f76821aa3f | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/linear_algebra/dual.lean | 4c7a3587f969191dc66f97fb97e7d8b74f285976 | [
"Apache-2.0"
] | permissive | ilitzroth/mathlib | ea647e67f1fdfd19a0f7bdc5504e8acec6180011 | 5254ef14e3465f6504306132fe3ba9cec9ffff16 | refs/heads/master | 1,680,086,661,182 | 1,617,715,647,000 | 1,617,715,647,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 24,560 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Fabian Glöckle
-/
import linear_algebra.finite_dimensional
import linear_algebra.projection
/-!
# Dual vector spaces
The dual space of an R-module M is the R-module of linear maps `M → R`.
## Main definitions
* `dual R M` defines the dual space of M over R.
* Given a basis for a K-vector space `V`, `is_basis.to_dual` produces a map from `V` to `dual K V`.
* Given families of vectors `e` and `ε`, `dual_pair e ε` states that these families have the
characteristic properties of a basis and a dual.
* `dual_annihilator W` is the submodule of `dual R M` where every element annihilates `W`.
## Main results
* `to_dual_equiv` : the dual space is linearly equivalent to the primal space.
* `dual_pair.is_basis` and `dual_pair.eq_dual`: if `e` and `ε` form a dual pair, `e` is a basis and
`ε` is its dual basis.
* `quot_equiv_annihilator`: the quotient by a subspace is isomorphic to its dual annihilator.
## Notation
We sometimes use `V'` as local notation for `dual K V`.
## TODO
Erdös-Kaplansky theorem about the dimension of a dual vector space in case of infinite dimension.
-/
noncomputable theory
namespace module
variables (R : Type*) (M : Type*)
variables [comm_semiring R] [add_comm_monoid M] [semimodule R M]
/-- The dual space of an R-module M is the R-module of linear maps `M → R`. -/
@[derive [add_comm_monoid, semimodule R]] def dual := M →ₗ[R] R
instance {S : Type*} [comm_ring S] {N : Type*} [add_comm_group N] [module S N] :
add_comm_group (dual S N) := by {unfold dual, apply_instance}
namespace dual
instance : inhabited (dual R M) := by dunfold dual; apply_instance
instance : has_coe_to_fun (dual R M) := ⟨_, linear_map.to_fun⟩
/-- Maps a module M to the dual of the dual of M. See `vector_space.erange_coe` and
`vector_space.eval_equiv`. -/
def eval : M →ₗ[R] (dual R (dual R M)) := linear_map.flip linear_map.id
@[simp] lemma eval_apply (v : M) (a : dual R M) : eval R M v a = a v :=
begin
dunfold eval,
rw [linear_map.flip_apply, linear_map.id_apply]
end
variables {R M} {M' : Type*} [add_comm_monoid M'] [semimodule R M']
/-- The transposition of linear maps, as a linear map from `M →ₗ[R] M'` to
`dual R M' →ₗ[R] dual R M`. -/
def transpose : (M →ₗ[R] M') →ₗ[R] (dual R M' →ₗ[R] dual R M) :=
(linear_map.llcomp R M M' R).flip
lemma transpose_apply (u : M →ₗ[R] M') (l : dual R M') : transpose u l = l.comp u := rfl
variables {M'' : Type*} [add_comm_monoid M''] [semimodule R M'']
lemma transpose_comp (u : M' →ₗ[R] M'') (v : M →ₗ[R] M') :
transpose (u.comp v) = (transpose v).comp (transpose u) := rfl
end dual
end module
namespace is_basis
universes u v w
variables {K : Type u} {V : Type v} {ι : Type w}
variables [field K] [add_comm_group V] [vector_space K V]
open vector_space module module.dual submodule linear_map cardinal function
variables [de : decidable_eq ι]
variables (B : ι → V) (h : is_basis K B)
include de h
/-- The linear map from a vector space equipped with basis to its dual vector space,
taking basis elements to corresponding dual basis elements. -/
def to_dual : V →ₗ[K] module.dual K V :=
h.constr $ λ v, h.constr $ λ w, if w = v then (1 : K) else 0
variable {B}
lemma to_dual_apply (i j : ι) :
h.to_dual B (B i) (B j) = if i = j then 1 else 0 :=
by { erw [constr_basis h, constr_basis h], ac_refl }
@[simp] lemma to_dual_total_left (f : ι →₀ K) (i : ι) :
h.to_dual B (finsupp.total ι V K B f) (B i) = f i :=
begin
rw [finsupp.total_apply, finsupp.sum, linear_map.map_sum, linear_map.sum_apply],
simp_rw [linear_map.map_smul, linear_map.smul_apply, to_dual_apply, smul_eq_mul,
mul_boole, finset.sum_ite_eq'],
split_ifs with h,
{ refl },
{ rw finsupp.not_mem_support_iff.mp h }
end
@[simp] lemma to_dual_total_right (f : ι →₀ K) (i : ι) :
h.to_dual B (B i) (finsupp.total ι V K B f) = f i :=
begin
rw [finsupp.total_apply, finsupp.sum, linear_map.map_sum],
simp_rw [linear_map.map_smul, to_dual_apply, smul_eq_mul, mul_boole, finset.sum_ite_eq],
split_ifs with h,
{ refl },
{ rw finsupp.not_mem_support_iff.mp h }
end
lemma to_dual_apply_left (v : V) (i : ι) : h.to_dual B v (B i) = h.repr v i :=
by rw [← h.to_dual_total_left, h.total_repr]
lemma to_dual_apply_right (i : ι) (v : V) : h.to_dual B (B i) v = h.repr v i :=
by rw [← h.to_dual_total_right, h.total_repr]
variable (B)
/-- `h.to_dual_flip v` is the linear map sending `w` to `h.to_dual w v`. -/
def to_dual_flip (v : V) : (V →ₗ[K] K) := (h.to_dual B).flip v
variable {B}
omit de
/-- `h.coord_fun i` sends vectors to their `i`'th coordinate with respect to the basis `h`. -/
def coord_fun (i : ι) : (V →ₗ[K] K) := (finsupp.lapply i).comp h.repr
lemma coord_fun_eq_repr (v : V) (i : ι) : h.coord_fun i v = h.repr v i := rfl
include de
-- TODO: this lemma should be called something like `to_dual_flip_apply`
lemma to_dual_swap_eq_to_dual (v w : V) : h.to_dual_flip B v w = h.to_dual B w v := rfl
lemma to_dual_eq_repr (v : V) (i : ι) : h.to_dual B v (B i) = h.repr v i :=
h.to_dual_apply_left v i
lemma to_dual_eq_equiv_fun [fintype ι] (v : V) (i : ι) : h.to_dual B v (B i) = h.equiv_fun v i :=
by rw [h.equiv_fun_apply, to_dual_eq_repr]
lemma to_dual_inj (v : V) (a : h.to_dual B v = 0) : v = 0 :=
begin
rw [← mem_bot K, ← h.repr_ker, mem_ker],
apply finsupp.ext,
intro b,
rw [←to_dual_eq_repr _ _ _, a],
refl
end
theorem to_dual_ker : (h.to_dual B).ker = ⊥ :=
ker_eq_bot'.mpr h.to_dual_inj
theorem to_dual_range [fin : fintype ι] : (h.to_dual B).range = ⊤ :=
begin
rw eq_top_iff',
intro f,
rw linear_map.mem_range,
let lin_comb : ι →₀ K := finsupp.on_finset fin.elems (λ i, f.to_fun (B i)) _,
{ use finsupp.total ι V K B lin_comb,
apply h.ext,
{ intros i,
rw [h.to_dual_eq_repr _ i, repr_total h],
{ refl },
{ rw [finsupp.mem_supported],
exact λ _ _, set.mem_univ _ } } },
{ intros a _,
apply fin.complete }
end
/-- Maps a basis for `V` to a basis for the dual space. -/
def dual_basis : ι → dual K V := λ i, h.to_dual B (B i)
theorem dual_lin_independent : linear_independent K h.dual_basis :=
h.1.map' _ (to_dual_ker _)
@[simp] lemma dual_basis_apply_self (i j : ι) :
h.dual_basis i (B j) = if i = j then 1 else 0 :=
h.to_dual_apply i j
variable (B)
/-- A vector space is linearly equivalent to its dual space. -/
def to_dual_equiv [fintype ι] : V ≃ₗ[K] (dual K V) :=
linear_equiv.of_bijective (h.to_dual B) h.to_dual_ker h.to_dual_range
variable {B}
theorem dual_basis_is_basis [fintype ι] : is_basis K h.dual_basis :=
(h.to_dual_equiv B).is_basis h
@[simp] lemma total_dual_basis [fintype ι] (f : ι →₀ K) (i : ι) :
finsupp.total ι (dual K V) K h.dual_basis f (B i) = f i :=
begin
rw [finsupp.total_apply, finsupp.sum_fintype, linear_map.sum_apply],
{ simp_rw [smul_apply, smul_eq_mul, dual_basis_apply_self, mul_boole,
finset.sum_ite_eq', if_pos (finset.mem_univ i)] },
{ intro, rw zero_smul },
end
lemma dual_basis_repr [fintype ι] (l : dual K V) (i : ι) :
h.dual_basis_is_basis.repr l i = l (B i) :=
by rw [← total_dual_basis h, is_basis.total_repr h.dual_basis_is_basis l ]
lemma dual_basis_equiv_fun [fintype ι] (l : dual K V) (i : ι) :
h.dual_basis_is_basis.equiv_fun l i = l (B i) :=
by rw [is_basis.equiv_fun_apply, dual_basis_repr]
lemma dual_basis_apply [fintype ι] (i : ι) (v : V) : h.dual_basis i v = h.equiv_fun v i :=
h.to_dual_apply_right i v
@[simp] lemma to_dual_to_dual [fintype ι] :
(h.dual_basis_is_basis.to_dual _).comp (h.to_dual B) = eval K V :=
begin
refine h.ext (λ i, h.dual_basis_is_basis.ext (λ j, _)),
suffices : @ite K _ (classical.prop_decidable _) 1 0 = @ite K _ (de j i) 1 0,
by simpa [h.dual_basis_is_basis.to_dual_apply_left, h.dual_basis_repr, h.to_dual_apply_right],
split_ifs; refl
end
omit de
theorem dual_dim_eq [fintype ι] :
cardinal.lift.{v u} (dim K V) = dim K (dual K V) :=
begin
classical,
have := linear_equiv.dim_eq_lift (h.to_dual_equiv B),
simp only [cardinal.lift_umax] at this,
rw [this, ← cardinal.lift_umax],
apply cardinal.lift_id,
end
end is_basis
namespace vector_space
universes u v
variables {K : Type u} {V : Type v}
variables [field K] [add_comm_group V] [vector_space K V]
open module module.dual submodule linear_map cardinal is_basis finite_dimensional
theorem eval_ker : (eval K V).ker = ⊥ :=
begin
classical,
rw ker_eq_bot',
intros v h,
rw linear_map.ext_iff at h,
by_contradiction H,
rcases exists_subset_is_basis (linear_independent_singleton H) with ⟨b, hv, hb⟩,
swap 4, assumption,
have hv' : v = (coe : b → V) ⟨v, hv (set.mem_singleton v)⟩ := rfl,
let hx := h (hb.to_dual _ v),
rw [eval_apply, hv', to_dual_apply, if_pos rfl, zero_apply] at hx,
exact one_ne_zero hx
end
theorem dual_dim_eq [finite_dimensional K V] :
cardinal.lift.{v u} (dim K V) = dim K (dual K V) :=
begin
classical,
rcases exists_is_basis_fintype (dim_lt_omega K V) with ⟨b, hb, ⟨hf⟩⟩,
resetI,
exact hb.dual_dim_eq
end
lemma erange_coe [finite_dimensional K V] : (eval K V).range = ⊤ :=
begin
classical,
rcases exists_is_basis_fintype (dim_lt_omega K V) with ⟨b, hb, ⟨hf⟩⟩,
unfreezingI { rw [← hb.to_dual_to_dual, range_comp, hb.to_dual_range, map_top, to_dual_range _] },
apply_instance
end
/-- A vector space is linearly equivalent to the dual of its dual space. -/
def eval_equiv [finite_dimensional K V] : V ≃ₗ[K] dual K (dual K V) :=
linear_equiv.of_bijective (eval K V) eval_ker (erange_coe)
lemma eval_equiv_to_linear_map [finite_dimensional K V] :
eval_equiv.to_linear_map = dual.eval K V := rfl
end vector_space
section dual_pair
open vector_space module module.dual linear_map function
universes u v w
variables {K : Type u} {V : Type v} {ι : Type w} [decidable_eq ι]
variables [field K] [add_comm_group V] [vector_space K V]
local notation `V'` := dual K V
/-- `e` and `ε` have characteristic properties of a basis and its dual -/
@[nolint has_inhabited_instance]
structure dual_pair (e : ι → V) (ε : ι → V') :=
(eval : ∀ i j : ι, ε i (e j) = if i = j then 1 else 0)
(total : ∀ {v : V}, (∀ i, ε i v = 0) → v = 0)
[finite : ∀ v : V, fintype {i | ε i v ≠ 0}]
end dual_pair
namespace dual_pair
open vector_space module module.dual linear_map function
universes u v w
variables {K : Type u} {V : Type v} {ι : Type w} [dι : decidable_eq ι]
variables [field K] [add_comm_group V] [vector_space K V]
variables {e : ι → V} {ε : ι → dual K V} (h : dual_pair e ε)
include h
/-- The coefficients of `v` on the basis `e` -/
def coeffs (v : V) : ι →₀ K :=
{ to_fun := λ i, ε i v,
support := by { haveI := h.finite v, exact {i : ι | ε i v ≠ 0}.to_finset },
mem_support_to_fun := by {intro i, rw set.mem_to_finset, exact iff.rfl } }
@[simp] lemma coeffs_apply (v : V) (i : ι) : h.coeffs v i = ε i v := rfl
omit h
/-- linear combinations of elements of `e`.
This is a convenient abbreviation for `finsupp.total _ V K e l` -/
def lc (e : ι → V) (l : ι →₀ K) : V := l.sum (λ (i : ι) (a : K), a • (e i))
include h
lemma dual_lc (l : ι →₀ K) (i : ι) : ε i (dual_pair.lc e l) = l i :=
begin
erw linear_map.map_sum,
simp only [h.eval, map_smul, smul_eq_mul],
rw finset.sum_eq_single i,
{ simp },
{ intros q q_in q_ne,
simp [q_ne.symm] },
{ intro p_not_in,
simp [finsupp.not_mem_support_iff.1 p_not_in] },
end
@[simp]
lemma coeffs_lc (l : ι →₀ K) : h.coeffs (dual_pair.lc e l) = l :=
by { ext i, rw [h.coeffs_apply, h.dual_lc] }
/-- For any v : V n, \sum_{p ∈ Q n} (ε p v) • e p = v -/
lemma decomposition (v : V) : dual_pair.lc e (h.coeffs v) = v :=
begin
refine eq_of_sub_eq_zero (h.total _),
intros i,
simp [-sub_eq_add_neg, linear_map.map_sub, h.dual_lc, sub_eq_zero]
end
lemma mem_of_mem_span {H : set ι} {x : V} (hmem : x ∈ submodule.span K (e '' H)) :
∀ i : ι, ε i x ≠ 0 → i ∈ H :=
begin
intros i hi,
rcases (finsupp.mem_span_iff_total _).mp hmem with ⟨l, supp_l, sum_l⟩,
change dual_pair.lc e l = x at sum_l,
rw finsupp.mem_supported' at supp_l,
apply classical.by_contradiction,
intro i_not,
apply hi,
rw ← sum_l,
simpa [h.dual_lc] using supp_l i i_not
end
lemma is_basis : is_basis K e :=
begin
split,
{ rw linear_independent_iff,
intros l H,
change dual_pair.lc e l = 0 at H,
ext i,
apply_fun ε i at H,
simpa [h.dual_lc] using H },
{ rw submodule.eq_top_iff',
intro v,
rw [← set.image_univ, finsupp.mem_span_iff_total],
exact ⟨h.coeffs v, by simp, h.decomposition v⟩ },
end
lemma eq_dual : ε = is_basis.dual_basis h.is_basis :=
begin
funext i,
refine h.is_basis.ext (λ _, _),
erw [is_basis.to_dual_apply, h.eval]
end
end dual_pair
namespace submodule
universes u v w
variables {R : Type u} {M : Type v} [comm_ring R] [add_comm_group M] [module R M]
variable {W : submodule R M}
/-- The `dual_restrict` of a submodule `W` of `M` is the linear map from the
dual of `M` to the dual of `W` such that the domain of each linear map is
restricted to `W`. -/
def dual_restrict (W : submodule R M) :
module.dual R M →ₗ[R] module.dual R W :=
linear_map.dom_restrict' W
@[simp] lemma dual_restrict_apply
(W : submodule R M) (φ : module.dual R M) (x : W) :
W.dual_restrict φ x = φ (x : M) := rfl
/-- The `dual_annihilator` of a submodule `W` is the set of linear maps `φ` such
that `φ w = 0` for all `w ∈ W`. -/
def dual_annihilator {R : Type u} {M : Type v} [comm_ring R] [add_comm_group M]
[module R M] (W : submodule R M) : submodule R $ module.dual R M :=
W.dual_restrict.ker
@[simp] lemma mem_dual_annihilator (φ : module.dual R M) :
φ ∈ W.dual_annihilator ↔ ∀ w ∈ W, φ w = 0 :=
begin
refine linear_map.mem_ker.trans _,
simp_rw [linear_map.ext_iff, dual_restrict_apply],
exact ⟨λ h w hw, h ⟨w, hw⟩, λ h w, h w.1 w.2⟩
end
lemma dual_restrict_ker_eq_dual_annihilator (W : submodule R M) :
W.dual_restrict.ker = W.dual_annihilator :=
rfl
lemma dual_annihilator_sup_eq_inf_dual_annihilator (U V : submodule R M) :
(U ⊔ V).dual_annihilator = U.dual_annihilator ⊓ V.dual_annihilator :=
begin
ext φ,
rw [mem_inf, mem_dual_annihilator, mem_dual_annihilator, mem_dual_annihilator],
split; intro h,
{ refine ⟨_, _⟩;
intros x hx,
exact h x (mem_sup.2 ⟨x, hx, 0, zero_mem _, add_zero _⟩),
exact h x (mem_sup.2 ⟨0, zero_mem _, x, hx, zero_add _⟩) },
{ simp_rw mem_sup,
rintro _ ⟨x, hx, y, hy, rfl⟩,
rw [linear_map.map_add, h.1 _ hx, h.2 _ hy, add_zero] }
end
/-- The pullback of a submodule in the dual space along the evaluation map. -/
def dual_annihilator_comap (Φ : submodule R (module.dual R M)) : submodule R M :=
Φ.dual_annihilator.comap (module.dual.eval R M)
lemma mem_dual_annihilator_comap_iff {Φ : submodule R (module.dual R M)} (x : M) :
x ∈ Φ.dual_annihilator_comap ↔ ∀ φ ∈ Φ, (φ x : R) = 0 :=
by simp_rw [dual_annihilator_comap, mem_comap, mem_dual_annihilator, module.dual.eval_apply]
end submodule
namespace subspace
open submodule linear_map
universes u v w
-- We work in vector spaces because `exists_is_compl` only hold for vector spaces
variables {K : Type u} {V : Type v} [field K] [add_comm_group V] [vector_space K V]
/-- Given a subspace `W` of `V` and an element of its dual `φ`, `dual_lift W φ` is
the natural extension of `φ` to an element of the dual of `V`.
That is, `dual_lift W φ` sends `w ∈ W` to `φ x` and `x` in the complement of `W` to `0`. -/
noncomputable def dual_lift (W : subspace K V) :
module.dual K W →ₗ[K] module.dual K V :=
let h := classical.indefinite_description _ W.exists_is_compl in
(linear_map.of_is_compl_prod h.2).comp (linear_map.inl _ _ _)
variable {W : subspace K V}
@[simp] lemma dual_lift_of_subtype {φ : module.dual K W} (w : W) :
W.dual_lift φ (w : V) = φ w :=
by { erw of_is_compl_left_apply _ w, refl }
lemma dual_lift_of_mem {φ : module.dual K W} {w : V} (hw : w ∈ W) :
W.dual_lift φ w = φ ⟨w, hw⟩ :=
dual_lift_of_subtype ⟨w, hw⟩
@[simp] lemma dual_restrict_comp_dual_lift (W : subspace K V) :
W.dual_restrict.comp W.dual_lift = 1 :=
by { ext φ x, simp }
lemma dual_restrict_left_inverse (W : subspace K V) :
function.left_inverse W.dual_restrict W.dual_lift :=
λ x, show W.dual_restrict.comp W.dual_lift x = x,
by { rw [dual_restrict_comp_dual_lift], refl }
lemma dual_lift_right_inverse (W : subspace K V) :
function.right_inverse W.dual_lift W.dual_restrict :=
W.dual_restrict_left_inverse
lemma dual_restrict_surjective :
function.surjective W.dual_restrict :=
W.dual_lift_right_inverse.surjective
lemma dual_lift_injective : function.injective W.dual_lift :=
W.dual_restrict_left_inverse.injective
/-- The quotient by the `dual_annihilator` of a subspace is isomorphic to the
dual of that subspace. -/
noncomputable def quot_annihilator_equiv (W : subspace K V) :
W.dual_annihilator.quotient ≃ₗ[K] module.dual K W :=
(quot_equiv_of_eq _ _ W.dual_restrict_ker_eq_dual_annihilator).symm.trans $
W.dual_restrict.quot_ker_equiv_of_surjective dual_restrict_surjective
/-- The natural isomorphism forom the dual of a subspace `W` to `W.dual_lift.range`. -/
noncomputable def dual_equiv_dual (W : subspace K V) :
module.dual K W ≃ₗ[K] W.dual_lift.range :=
linear_equiv.of_injective _ $ ker_eq_bot.2 dual_lift_injective
lemma dual_equiv_dual_def (W : subspace K V) :
W.dual_equiv_dual.to_linear_map = W.dual_lift.range_restrict := rfl
@[simp] lemma dual_equiv_dual_apply (φ : module.dual K W) :
W.dual_equiv_dual φ = ⟨W.dual_lift φ, mem_range.2 ⟨φ, rfl⟩⟩ := rfl
section
open_locale classical
open finite_dimensional
variables {V₁ : Type*} [add_comm_group V₁] [vector_space K V₁]
instance [H : finite_dimensional K V] : finite_dimensional K (module.dual K V) :=
begin
refine @linear_equiv.finite_dimensional _ _ _ _ _ _ _ _ _ H,
have hB := classical.some_spec (exists_is_basis_finite K V),
haveI := classical.choice hB.2,
exact is_basis.to_dual_equiv _ hB.1
end
variables [finite_dimensional K V] [finite_dimensional K V₁]
@[simp] lemma dual_findim_eq :
findim K (module.dual K V) = findim K V :=
begin
obtain ⟨n, hn, hf⟩ := exists_is_basis_finite K V,
refine linear_equiv.findim_eq _,
haveI : fintype n := set.finite.fintype hf,
refine (hn.to_dual_equiv _).symm,
end
/-- The quotient by the dual is isomorphic to its dual annihilator. -/
noncomputable def quot_dual_equiv_annihilator (W : subspace K V) :
W.dual_lift.range.quotient ≃ₗ[K] W.dual_annihilator :=
linear_equiv.quot_equiv_of_quot_equiv $
linear_equiv.trans W.quot_annihilator_equiv W.dual_equiv_dual
/-- The quotient by a subspace is isomorphic to its dual annihilator. -/
noncomputable def quot_equiv_annihilator (W : subspace K V) :
W.quotient ≃ₗ[K] W.dual_annihilator :=
begin
refine linear_equiv.trans _ W.quot_dual_equiv_annihilator,
refine linear_equiv.quot_equiv_of_equiv _ _,
{ refine linear_equiv.trans _ W.dual_equiv_dual,
have hB := classical.some_spec (exists_is_basis_finite K W),
haveI := classical.choice hB.2,
exact is_basis.to_dual_equiv _ hB.1 },
{ have hB := classical.some_spec (exists_is_basis_finite K V),
haveI := classical.choice hB.2,
exact is_basis.to_dual_equiv _ hB.1 },
end
open finite_dimensional
@[simp]
lemma findim_dual_annihilator_comap_eq {Φ : subspace K (module.dual K V)} :
findim K Φ.dual_annihilator_comap = findim K Φ.dual_annihilator :=
begin
rw [submodule.dual_annihilator_comap, ← vector_space.eval_equiv_to_linear_map],
exact linear_equiv.findim_eq (linear_equiv.of_submodule' _ _),
end
lemma findim_add_findim_dual_annihilator_comap_eq
(W : subspace K (module.dual K V)) :
findim K W + findim K W.dual_annihilator_comap = findim K V :=
begin
rw [findim_dual_annihilator_comap_eq, W.quot_equiv_annihilator.findim_eq.symm, add_comm,
submodule.findim_quotient_add_findim, subspace.dual_findim_eq],
end
end
end subspace
variables {R : Type*} [comm_ring R] {M₁ : Type*} {M₂ : Type*}
variables [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂]
open module
/-- Given a linear map `f : M₁ →ₗ[R] M₂`, `f.dual_map` is the linear map between the dual of
`M₂` and `M₁` such that it maps the functional `φ` to `φ ∘ f`. -/
def linear_map.dual_map (f : M₁ →ₗ[R] M₂) : dual R M₂ →ₗ[R] dual R M₁ :=
linear_map.lcomp R R f
@[simp] lemma linear_map.dual_map_apply (f : M₁ →ₗ[R] M₂) (g : dual R M₂) (x : M₁) :
f.dual_map g x = g (f x) :=
linear_map.lcomp_apply f g x
@[simp] lemma linear_map.dual_map_id :
(linear_map.id : M₁ →ₗ[R] M₁).dual_map = linear_map.id :=
by { ext, refl }
lemma linear_map.dual_map_comp_dual_map {M₃ : Type*} [add_comm_group M₃] [module R M₃]
(f : M₁ →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) :
f.dual_map.comp g.dual_map = (g.comp f).dual_map :=
rfl
/-- The `linear_equiv` version of `linear_map.dual_map`. -/
def linear_equiv.dual_map (f : M₁ ≃ₗ[R] M₂) : dual R M₂ ≃ₗ[R] dual R M₁ :=
{ inv_fun := f.symm.to_linear_map.dual_map,
left_inv :=
begin
intro φ, ext x,
simp only [linear_map.dual_map_apply, linear_equiv.coe_to_linear_map,
linear_map.to_fun_eq_coe, linear_equiv.apply_symm_apply]
end,
right_inv :=
begin
intro φ, ext x,
simp only [linear_map.dual_map_apply, linear_equiv.coe_to_linear_map,
linear_map.to_fun_eq_coe, linear_equiv.symm_apply_apply]
end,
.. f.to_linear_map.dual_map }
@[simp] lemma linear_equiv.dual_map_apply (f : M₁ ≃ₗ[R] M₂) (g : dual R M₂) (x : M₁) :
f.dual_map g x = g (f x) :=
linear_map.lcomp_apply f g x
@[simp] lemma linear_equiv.dual_map_refl :
(linear_equiv.refl R M₁).dual_map = linear_equiv.refl R (dual R M₁) :=
by { ext, refl }
@[simp] lemma linear_equiv.dual_map_symm {f : M₁ ≃ₗ[R] M₂} :
(linear_equiv.dual_map f).symm = linear_equiv.dual_map f.symm := rfl
lemma linear_equiv.dual_map_trans {M₃ : Type*} [add_comm_group M₃] [module R M₃]
(f : M₁ ≃ₗ[R] M₂) (g : M₂ ≃ₗ[R] M₃) :
g.dual_map.trans f.dual_map = (f.trans g).dual_map :=
rfl
namespace linear_map
variable (f : M₁ →ₗ[R] M₂)
lemma ker_dual_map_eq_dual_annihilator_range :
f.dual_map.ker = f.range.dual_annihilator :=
begin
ext φ, split; intro hφ,
{ rw mem_ker at hφ,
rw submodule.mem_dual_annihilator,
rintro y ⟨x, _, rfl⟩,
rw [← dual_map_apply, hφ, zero_apply] },
{ ext x,
rw dual_map_apply,
rw submodule.mem_dual_annihilator at hφ,
exact hφ (f x) ⟨x, set_like.mem_coe.mpr submodule.mem_top, rfl⟩ }
end
lemma range_dual_map_le_dual_annihilator_ker :
f.dual_map.range ≤ f.ker.dual_annihilator :=
begin
rintro _ ⟨ψ, _, rfl⟩,
simp_rw [submodule.mem_dual_annihilator, mem_ker],
rintro x hx,
rw [dual_map_apply, hx, map_zero]
end
section finite_dimensional
variables {K : Type*} [field K] {V₁ : Type*} {V₂ : Type*}
variables [add_comm_group V₁] [vector_space K V₁] [add_comm_group V₂] [vector_space K V₂]
open finite_dimensional
variable [finite_dimensional K V₂]
@[simp] lemma findim_range_dual_map_eq_findim_range (f : V₁ →ₗ[K] V₂) :
findim K f.dual_map.range = findim K f.range :=
begin
have := submodule.findim_quotient_add_findim f.range,
rw [(subspace.quot_equiv_annihilator f.range).findim_eq,
← ker_dual_map_eq_dual_annihilator_range] at this,
conv_rhs at this { rw ← subspace.dual_findim_eq },
refine add_left_injective (findim K f.dual_map.ker) _,
change _ + _ = _ + _,
rw [findim_range_add_findim_ker f.dual_map, add_comm, this],
end
lemma range_dual_map_eq_dual_annihilator_ker [finite_dimensional K V₁] (f : V₁ →ₗ[K] V₂) :
f.dual_map.range = f.ker.dual_annihilator :=
begin
refine eq_of_le_of_findim_eq f.range_dual_map_le_dual_annihilator_ker _,
have := submodule.findim_quotient_add_findim f.ker,
rw (subspace.quot_equiv_annihilator f.ker).findim_eq at this,
refine add_left_injective (findim K f.ker) _,
simp_rw [this, findim_range_dual_map_eq_findim_range],
exact findim_range_add_findim_ker f,
end
end finite_dimensional
end linear_map
|
4a4fa2b95182df1d43db911ba35142ee812f172c | e09201d437062e1f95e6e5360aab0c9f947901aa | /src/regular/list_lemmas.lean | ff13171a0d3633921024b48f318d2f13556872e1 | [] | no_license | VArtem/lean-regular-languages | 34f4b093f28ef2f09ba7e684e642a0f97c901560 | e877243188253d0ac17ccf0ae2da7bf608686ff0 | refs/heads/master | 1,683,590,111,306 | 1,622,307,234,000 | 1,622,307,234,000 | 284,232,653 | 7 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,910 | lean | import data.list.basic
import tactic
open list
variables {S : Type*} {w : list S}
lemma drop_of_take_of_lt_ne_nil {x y : ℕ} :
x < y → y <= w.length → (drop x (take y w)) ≠ nil :=
begin
rintro xy yw,
apply ne_nil_of_length_pos,
rw [length_drop, length_take, min_eq_left yw],
exact nat.sub_pos_of_lt xy,
end
lemma take_append_drop_of_lt {x y : ℕ} :
x < y → take x w ++ drop x (take y w) = take y w :=
begin
intro xy,
have hmin : min x y = x, from min_eq_left_of_lt xy,
nth_rewrite 0 ←hmin,
rw ← take_take,
apply take_append_drop,
end
lemma prefix_of_append_of_le_length {a left right: list S}
(ha : a.length ≤ left.length) (hpref : a <+: (left ++ right)) : a <+: left :=
begin
exact prefix_of_prefix_length_le hpref (prefix_append _ _) ha,
end
lemma prefix_of_prefix_append {a left right : list S}
(ha : a <+: left) : a <+: (left ++ right) :=
begin
cases ha,
exact ⟨ha_w ++ right, by rw [←append_assoc, ha_h]⟩,
end
lemma prefix_of_repeat {a : list S} {c : S} {n : ℕ}
(ha : a <+: repeat c n) : a = repeat c a.length :=
begin
rw eq_repeat',
intros b hb,
cases ha,
apply eq_of_mem_repeat,
rw ← ha_h,
apply mem_append_left _ hb,
end
lemma suffix_of_repeat {a : list S} {c : S} {n : ℕ}
(ha : a <:+ repeat c n) : a = repeat c a.length :=
begin
rw eq_repeat',
intros b hb,
cases ha,
apply eq_of_mem_repeat,
rw ← ha_h,
apply mem_append_right _ hb,
end
lemma repeat_suffix_of_append_repeat {A : Type*} {n m k : ℕ} {a b : A} :
k < n → repeat a n <:+ repeat b m ++ repeat a k → a = b :=
begin
rintro hnk ⟨beg, happend⟩,
rw append_eq_append_iff at happend,
rcases happend with ⟨a', ⟨h1, h2⟩⟩ | ⟨c', ⟨h1, h2⟩⟩, {
have repeat_a : a' = repeat a a'.length := prefix_of_repeat ⟨repeat a k, h2.symm⟩,
have repeat_b : a' = repeat b a'.length := suffix_of_repeat ⟨beg, h1.symm⟩,
apply_fun length at h2,
have a'_length_pos : 0 < a'.length, from by {
simp at h2,
linarith,
},
cases a', {
rw length_pos_iff_ne_nil at a'_length_pos,
contradiction,
}, {
nth_rewrite 0 repeat_a at repeat_b,
simp only [length, repeat_succ] at repeat_b,
exact repeat_b.1,
}
}, {
apply_fun length at h2,
rw [length_append, length_repeat, length_repeat] at h2,
exfalso,
linarith,
},
end
lemma suffix_of_append_eq_append {a b c d : list S} :
a.length ≤ c.length → a ++ b = c ++ d → d <:+ b :=
begin
intros ac ab_cd,
obtain ⟨a', ⟨h1, h2⟩⟩ | ⟨c', ⟨h1, h2⟩⟩ := append_eq_append_iff.1 ab_cd, {
apply_fun length at h1,
simp at h1,
use [a', h2.symm],
}, {
apply_fun length at h1,
rw [h1, length_append] at ac,
simp only [add_le_iff_nonpos_right, nonpos_iff_eq_zero] at ac,
rw [length_eq_zero] at ac,
subst ac,
rw [nil_append] at h2,
subst h2,
}
end
|
1a7901b4f74e11881d942c89340096579ebc6b61 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/param.lean | 73d20fbf7ad3fa514a84ee1a2a59016ed01c2716 | [
"Apache-2.0"
] | permissive | soonhokong/lean-osx | 4a954262c780e404c1369d6c06516161d07fcb40 | 3670278342d2f4faa49d95b46d86642d7875b47c | refs/heads/master | 1,611,410,334,552 | 1,474,425,686,000 | 1,474,425,686,000 | 12,043,103 | 5 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 504 | lean | --
open num
definition foo1 a b c := a + b + (c:num)
definition foo2 (a : num) b c := a + b + c
definition foo3 a b (c : num) := a + b + c
definition foo4 (a b c : num) := a + b + c
definition foo5 (a b c) : num := a + b + c
definition foo6 {a b c} : num := a + b + c
-- definition foo7 a b c : num := a + b + c -- Error
definition foo8 (a b c : num) : num := a + b + c
definition foo9 (a : num) (b : num) (c : num) : num := a + b + c
definition foo10 (a : num) b (c : num) : num := a + b + c
|
5247b58f0f1433689b3f79dfcc9d35297983ad28 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/record10.lean | 04b6d0dd62fe96945165fc22850b82f21de88407 | [
"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 | 439 | lean | set_option old_structure_cmd true
#print prefix semigroup
#print "======================="
class has_two_muls (A : Type) extends has_mul A renaming mul→mul1,
private has_mul A renaming mul→mul2
#print prefix has_two_muls
#print "======================="
class another_two_muls (A : Type) extends has_mul A renaming mul→mul1,
has_mul A renaming mul→mul2
|
e26edaac0aef16230eb48cb2428ca534930b0135 | f10d66a159ce037d07005bd6021cee6bbd6d5ff0 | /codeSnippitsReport.lean | 1f94d72ace47d16f439b4ee2087419dbe3d21069 | [] | no_license | johoelzl/mason-stother | 0c78bca183eb729d7f0f93e87ce073bc8cd8808d | 573ecfaada288176462c03c87b80ad05bdab4644 | refs/heads/master | 1,631,751,973,492 | 1,528,923,934,000 | 1,528,923,934,000 | 109,133,224 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 806 | lean | #exit
example : (2 + 3) * 4 + 5 = 2 * 4 + 3 * 4 + 5 :=
begin
rewrite [add_mul],
end
#check @eq.subst ℕ _ _ _ (add_mul 2 3 4) (eq.refl ((2 + 3) * 4 + 5))
example : (2 + 3) * 4 + 5 = 2 * 4 + 3 * 4 + 5 :=
have h1 : (2 + 3) * 4 + 5 = (2 + 3) * 4 + 5,
from eq.refl _,
have h2 : (2 + 3) * 4 = 2 * 4 + 3 * 4,
from add_mul _ _ _,
eq.subst (add_mul 2 3 4) (eq.refl ((2 + 3) * 4 + 5))
#check (eq.refl ((2 + 3) * 4 + 5))
#check (add_mul 2 3 4)
example : (2 + 3) * 4 + 5 = 2 * 4 + 3 * 4 + 5 :=
have h1 : (2 + 3) * 4 + 5 = (2 + 3) * 4 + 5,
from eq.refl _,
eq.subst (add_mul 2 3 4) h1
example : (2 + 3) * 4 + 5 = 2 * 4 + 3 * 4 + 5 :=
have h1 : (2 + 3) * 4 + 5 = (2 + 3) * 4 + 5,
from eq.refl _,
have h2 : (2 + 3) * 4 = 2 * 4 + 3 * 4,
from add_mul _ _ _,
eq.subst h2 h1
|
f11fd5b18f5601e740ad015d977298152765dac2 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/run/match_anonymous_constructor.lean | aa479911454848d0b9700a380c63e9badffce2e6 | [
"Apache-2.0"
] | permissive | soonhokong/lean-osx | 4a954262c780e404c1369d6c06516161d07fcb40 | 3670278342d2f4faa49d95b46d86642d7875b47c | refs/heads/master | 1,611,410,334,552 | 1,474,425,686,000 | 1,474,425,686,000 | 12,043,103 | 5 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 115 | lean |
definition p1 := (10, 20, 30)
definition v2 : nat :=
match p1 with
⟨a, b⟩ := a
end
example : v2 = 10 := rfl
|
2cd0b37902b7bc1e5f55fa155d5da31f1d8335b1 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/impbug1.lean | ffb5bfc5656a5d7d4b9b379900d5d7868005eafe | [
"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 | 734 | lean | prelude
-- category
definition Prop := Type.{0}
constant eq {A : Type} : A → A → Prop
infix `=`:50 := eq
inductive category (ob : Type) (mor : ob → ob → Type) : Type :=
mk : Π (id : Π (A : ob), mor A A),
(Π (A B : ob) (f : mor A A), id A = f) → category ob mor
definition id (ob : Type) (mor : ob → ob → Type) (Cat : category ob mor) := category.rec (λ id idl, id) Cat
attribute id [reducible]
theorem id_left (ob : Type) (mor : ob → ob → Type) (Cat : category ob mor) (A : ob) (f : mor A A) :
@eq (mor A A) (id ob mor Cat A) f :=
@category.rec ob mor (λ (C : category ob mor), @eq (mor A A) (id ob mor C A) f)
(λ (id : Π (A : ob), mor A A)
(idl : Π (A : ob), _),
idl A A f)
Cat
|
e089924d01849f59e4bcab9a2a9add772cad1984 | a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7 | /src/algebra/category/Group/biproducts.lean | c37079b30b7e8ad0d1dfe79e0ae1f3bb6e5a735e | [
"Apache-2.0"
] | permissive | kmill/mathlib | ea5a007b67ae4e9e18dd50d31d8aa60f650425ee | 1a419a9fea7b959317eddd556e1bb9639f4dcc05 | refs/heads/master | 1,668,578,197,719 | 1,593,629,163,000 | 1,593,629,163,000 | 276,482,939 | 0 | 0 | null | 1,593,637,960,000 | 1,593,637,959,000 | null | UTF-8 | Lean | false | false | 5,041 | 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.Group.basic
import algebra.category.Group.preadditive
import category_theory.limits.shapes.biproducts
import algebra.pi_instances
/-!
# The category of abelian groups has finite biproducts
-/
open category_theory
open category_theory.limits
open_locale big_operators
universe u
namespace AddCommGroup
instance has_binary_product (G H : AddCommGroup.{u}) : has_binary_product G H :=
{ cone :=
{ X := AddCommGroup.of (G × H),
π := { app := λ j, walking_pair.cases_on j (add_monoid_hom.fst G H) (add_monoid_hom.snd G H) }},
is_limit :=
{ lift := λ s, add_monoid_hom.prod (s.π.app walking_pair.left) (s.π.app walking_pair.right),
fac' := begin rintros s (⟨⟩|⟨⟩); { ext x, dsimp, simp, }, end,
uniq' := λ s m w,
begin
ext; [rw ← w walking_pair.left, rw ← w walking_pair.right]; refl,
end, } }
instance (G H : AddCommGroup.{u}) : has_binary_biproduct G H :=
has_binary_biproduct.of_has_binary_product _ _
-- We verify that the underlying type of the biproduct we've just defined is definitionally
-- the cartesian product of the underlying types:
example (G H : AddCommGroup.{u}) : ((G ⊞ H : AddCommGroup) : Type u) = (G × H) := rfl
-- Furthermore, our biproduct will automatically function as a coproduct.
example (G H : AddCommGroup.{u}) : has_colimit (pair G H) := by apply_instance
variables {J : Type u} (F : (discrete J) ⥤ AddCommGroup.{u})
namespace has_limit
/--
The map from an arbitrary cone over a indexed family of abelian groups
to the cartesian product of those groups.
-/
def lift (s : cone F) :
s.X ⟶ AddCommGroup.of (Π j, F.obj j) :=
{ to_fun := λ x j, s.π.app j x,
map_zero' := by { ext, simp },
map_add' := λ x y, by { ext, simp }, }
@[simp] lemma lift_apply (s : cone F) (x : s.X) (j : J) : (lift F s) x j = s.π.app j x := rfl
instance has_limit_discrete : has_limit F :=
{ cone :=
{ X := AddCommGroup.of (Π j, F.obj j),
π := discrete.nat_trans (λ j, add_monoid_hom.apply (λ j, F.obj j) j), },
is_limit :=
{ lift := lift F,
fac' := λ s j, by { ext, dsimp, simp, },
uniq' := λ s m w,
begin
ext x j,
dsimp only [has_limit.lift],
simp only [add_monoid_hom.coe_mk],
exact congr_arg (λ f : s.X ⟶ F.obj j, (f : s.X → F.obj j) x) (w j),
end, }, }
end has_limit
namespace has_colimit
variables [fintype J]
/--
The map from the cartesian product of a finite family of abelian groups
to any cocone over that family.
-/
def desc (s : cocone F) :
AddCommGroup.of (Π j, F.obj j) ⟶ s.X :=
{ to_fun := λ f, ∑ j, s.ι.app j (f j),
map_zero' :=
begin
conv_lhs { apply_congr, skip, simp [@pi.zero_apply _ (λ j, F.obj j) x _], },
simp,
end,
map_add' := λ x y,
begin
conv_lhs { apply_congr, skip, simp [pi.add_apply x y _], },
simp [finset.sum_add_distrib],
end, }
@[simp] lemma desc_apply (s : cocone F) (f : Π j, F.obj j) :
(desc F s) f = ∑ j, s.ι.app j (f j) := rfl
variables [decidable_eq J]
instance has_colimit_discrete : has_colimit F :=
{ cocone :=
{ X := AddCommGroup.of (Π j, F.obj j),
ι := discrete.nat_trans (λ j, add_monoid_hom.single (λ j, F.obj j) j), },
is_colimit :=
{ desc := desc F,
fac' := λ s j,
begin
dsimp, ext,
dsimp [add_monoid_hom.single],
simp only [pi.single, add_monoid_hom.coe_mk, desc_apply, coe_comp],
rw finset.sum_eq_single j,
{ simp, },
{ intros b _ h, simp only [dif_neg h, add_monoid_hom.map_zero], },
{ simp, },
end,
uniq' := λ s m w,
begin
dsimp at *,
convert @add_monoid_hom.functions_ext
(discrete J) _ (λ j, F.obj j) _ _ s.X _ m (eq_to_hom rfl ≫ desc F s) _,
intros j x,
dsimp [desc],
simp,
rw finset.sum_eq_single j,
{ -- FIXME what prevents either of these `erw`s working by `simp`?
erw [pi.single_eq_same], rw ←w, simp,
erw add_monoid_hom.single_apply, },
{ intros j' _ h, simp only [pi.single_eq_of_ne h, add_monoid_hom.map_zero], },
{ intros h, exfalso, simpa using h, },
end, }, }.
end has_colimit
open has_limit has_colimit
variables [decidable_eq J] [fintype J]
instance (f : J → AddCommGroup.{u}) : has_biproduct f :=
{ bicone :=
{ X := AddCommGroup.of (Π j, f j),
ι := λ j, add_monoid_hom.single (λ j, f j) j,
π := λ j, add_monoid_hom.apply (λ j, f j) j,
ι_π := λ j j',
begin
ext, split_ifs,
{ subst h, simp, },
{ rw [eq_comm] at h, simp [h], },
end, },
is_limit := limit.is_limit (discrete.functor f),
is_colimit := colimit.is_colimit (discrete.functor f), }.
-- We verify that the underlying type of the biproduct we've just defined is definitionally
-- the dependent function type:
example (f : J → AddCommGroup.{u}) : ((⨁ f : AddCommGroup) : Type u) = (Π j, f j) := rfl
end AddCommGroup
|
ca4bdc5c9f5a66fd0f70870429fd886f6e6384fd | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/algebra/ring/pi_auto.lean | 45519e4907f227dfea00f0ab09667cf66f0ddc3c | [] | 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,990 | 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 Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.tactic.pi_instances
import Mathlib.algebra.group.pi
import Mathlib.algebra.ring.basic
import Mathlib.PostPort
universes u v w u_1 u_2
namespace Mathlib
/-!
# Pi instances for ring
This file defines instances for ring, semiring and related structures on Pi Types
-/
namespace pi
protected instance distrib {I : Type u} {f : I → Type v} [(i : I) → distrib (f i)] :
distrib ((i : I) → f i) :=
distrib.mk Mul.mul Add.add sorry sorry
protected instance semiring {I : Type u} {f : I → Type v} [(i : I) → semiring (f i)] :
semiring ((i : I) → f i) :=
semiring.mk Add.add sorry 0 sorry sorry sorry Mul.mul sorry 1 sorry sorry sorry sorry sorry sorry
protected instance comm_semiring {I : Type u} {f : I → Type v} [(i : I) → comm_semiring (f i)] :
comm_semiring ((i : I) → f i) :=
comm_semiring.mk Add.add sorry 0 sorry sorry sorry Mul.mul sorry 1 sorry sorry sorry sorry sorry
sorry sorry
protected instance ring {I : Type u} {f : I → Type v} [(i : I) → ring (f i)] :
ring ((i : I) → f i) :=
ring.mk Add.add sorry 0 sorry sorry Neg.neg
(fun (ᾰ ᾰ_1 : (i : I) → f i) (i : I) => ring.sub (ᾰ i) (ᾰ_1 i)) sorry sorry Mul.mul sorry 1
sorry sorry sorry sorry
protected instance comm_ring {I : Type u} {f : I → Type v} [(i : I) → comm_ring (f i)] :
comm_ring ((i : I) → f i) :=
comm_ring.mk Add.add sorry 0 sorry sorry Neg.neg
(fun (ᾰ ᾰ_1 : (i : I) → f i) (i : I) => comm_ring.sub (ᾰ i) (ᾰ_1 i)) sorry sorry Mul.mul sorry 1
sorry sorry sorry sorry sorry
/-- A family of ring homomorphisms `f a : γ →+* β a` defines a ring homomorphism
`pi.ring_hom f : γ →+* Π a, β a` given by `pi.ring_hom f x b = f b x`. -/
protected def ring_hom {α : Type u} {β : α → Type v} [R : (a : α) → semiring (β a)] {γ : Type w}
[semiring γ] (f : (a : α) → γ →+* β a) : γ →+* (a : α) → β a :=
ring_hom.mk (fun (x : γ) (b : α) => coe_fn (f b) x) sorry sorry sorry sorry
@[simp] theorem ring_hom_apply {α : Type u} {β : α → Type v} [R : (a : α) → semiring (β a)]
{γ : Type w} [semiring γ] (f : (a : α) → γ →+* β a) (g : γ) (a : α) :
coe_fn (pi.ring_hom f) g a = coe_fn (f a) g :=
rfl
end pi
/-- Evaluation of functions into an indexed collection of monoids at a point is a monoid
homomorphism. -/
def ring_hom.apply {I : Type u_1} (f : I → Type u_2) [(i : I) → semiring (f i)] (i : I) :
((i : I) → f i) →+* f i :=
ring_hom.mk (monoid_hom.to_fun (monoid_hom.apply f i)) sorry sorry sorry sorry
@[simp] theorem ring_hom.apply_apply {I : Type u_1} (f : I → Type u_2) [(i : I) → semiring (f i)]
(i : I) (g : (i : I) → f i) : coe_fn (ring_hom.apply f i) g = g i :=
rfl
end Mathlib |
1000ecf6b6b7dc294987bb586a2343ccb14cfa73 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/set/enumerate.lean | a78a8b3564fd476957cec8319da0ea4e71968ab5 | [
"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,833 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import data.set.lattice
import tactic.wlog
/-!
# Set enumeration
This file allows enumeration of sets given a choice function.
The definition does not assume `sel` actually is a choice function, i.e. `sel s ∈ s` and
`sel s = none ↔ s = ∅`. These assumptions are added to the lemmas needing them.
-/
noncomputable theory
open function
namespace set
section enumerate
parameters {α : Type*} (sel : set α → option α)
/-- Given a choice function `sel`, enumerates the elements of a set in the order
`a 0 = sel s`, `a 1 = sel (s \ {a 0})`, `a 2 = sel (s \ {a 0, a 1})`, ... and stops when
`sel (s \ {a 0, ..., a n}) = none`. Note that we don't require `sel` to be a choice function. -/
def enumerate : set α → ℕ → option α
| s 0 := sel s
| s (n + 1) := do a ← sel s, enumerate (s \ {a}) n
lemma enumerate_eq_none_of_sel {s : set α} (h : sel s = none) : ∀ {n}, enumerate s n = none
| 0 := by simp [h, enumerate]; refl
| (n + 1) := by simp [h, enumerate]; refl
lemma enumerate_eq_none : ∀ {s n₁ n₂}, enumerate s n₁ = none → n₁ ≤ n₂ → enumerate s n₂ = none
| s 0 m := λ h _, enumerate_eq_none_of_sel h
| s (n + 1) m := λ h hm,
begin
cases hs : sel s,
{ exact enumerate_eq_none_of_sel sel hs },
{ cases m,
case nat.zero
{ have : n + 1 = 0, from nat.eq_zero_of_le_zero hm,
contradiction },
case nat.succ : m'
{ simp [hs, enumerate] at h ⊢,
have hm : n ≤ m', from nat.le_of_succ_le_succ hm,
exact enumerate_eq_none h hm } }
end
lemma enumerate_mem (h_sel : ∀ s a, sel s = some a → a ∈ s) :
∀ {s n a}, enumerate s n = some a → a ∈ s
| s 0 a := h_sel s a
| s (n+1) a :=
begin
cases h : sel s,
case none { simp [enumerate_eq_none_of_sel, h] },
case some : a'
{ simp [enumerate, h],
exact λ h' : enumerate _ (s \ {a'}) n = some a,
have a ∈ s \ {a'}, from enumerate_mem h',
this.left }
end
lemma enumerate_inj {n₁ n₂ : ℕ} {a : α} {s : set α} (h_sel : ∀ s a, sel s = some a → a ∈ s)
(h₁ : enumerate s n₁ = some a) (h₂ : enumerate s n₂ = some a) : n₁ = n₂ :=
begin
wlog hn : n₁ ≤ n₂,
{ rcases nat.le.dest hn with ⟨m, rfl⟩, clear hn,
induction n₁ generalizing s,
case nat.zero
{ cases m,
case nat.zero { refl },
case nat.succ : m
{ have : enumerate sel (s \ {a}) m = some a, { simp [enumerate, *] at * },
have : a ∈ s \ {a}, from enumerate_mem _ h_sel this,
by simpa } },
case nat.succ { cases h : sel s; simp [enumerate, nat.add_succ, add_comm, *] at *; tauto } }
end
end enumerate
end set
|
e60c6865baebdbd8c23cadedeea9aed69f393494 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/analysis/calculus/implicit.lean | 2a50efb0c3174c771dcf91950bb2d42f10615a30 | [
"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 | 20,804 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import analysis.calculus.inverse
import analysis.normed_space.complemented
/-!
# Implicit function theorem
We prove three versions of the implicit function theorem. First we define a structure
`implicit_function_data` that holds arguments for the most general version of the implicit function
theorem, see `implicit_function_data.implicit_function`
and `implicit_function_data.to_implicit_function`. This version allows a user to choose
a specific implicit function but provides only a little convenience over the inverse function
theorem.
Then we define `implicit_function_of_complemented`: implicit function defined by `f (g z y) = z`,
where `f : E → F` is a function strictly differentiable at `a` such that its derivative `f'`
is surjective and has a `complemented` kernel.
Finally, if the codomain of `f` is a finite dimensional space, then we can automatically prove
that the kernel of `f'` is complemented, hence the only assumptions are `has_strict_fderiv_at`
and `f'.range = ⊤`. This version is named `implicit_function`.
## TODO
* Add a version for a function `f : E × F → G` such that $$\frac{\partial f}{\partial y}$$ is
invertible.
* Add a version for `f : 𝕜 × 𝕜 → 𝕜` proving `has_strict_deriv_at` and `deriv φ = ...`.
* Prove that in a real vector space the implicit function has the same smoothness as the original
one.
* If the original function is differentiable in a neighborhood, then the implicit function is
differentiable in a neighborhood as well. Current setup only proves differentiability at one
point for the implicit function constructed in this file (as opposed to an unspecified implicit
function). One of the ways to overcome this difficulty is to use uniqueness of the implicit
function in the general version of the theorem. Another way is to prove that *any* implicit
function satisfying some predicate is strictly differentiable.
## Tags
implicit function, inverse function
-/
noncomputable theory
open_locale topological_space
open filter
open continuous_linear_map (fst snd smul_right ker_prod)
open continuous_linear_equiv (of_bijective)
open linear_map (ker range)
/-!
### General version
Consider two functions `f : E → F` and `g : E → G` and a point `a` such that
* both functions are strictly differentiable at `a`;
* the derivatives are surjective;
* the kernels of the derivatives are complementary subspaces of `E`.
Note that the map `x ↦ (f x, g x)` has a bijective derivative, hence it is a local homeomorphism
between `E` and `F × G`. We use this fact to define a function `φ : F → G → E`
(see `implicit_function_data.implicit_function`) such that for `(y, z)` close enough to `(f a, g a)`
we have `f (φ y z) = y` and `g (φ y z) = z`.
We also prove a formula for $$\frac{\partial\varphi}{\partial z}.$$
Though this statement is almost symmetric with respect to `F`, `G`, we interpret it in the following
way. Consider a family of surfaces `{x | f x = y}`, `y ∈ 𝓝 (f a)`. Each of these surfaces is
parametrized by `φ y`.
There are many ways to choose a (differentiable) function `φ` such that `f (φ y z) = y` but the
extra condition `g (φ y z) = z` allows a user to select one of these functions. If we imagine
that the level surfaces `f = const` form a local horizontal foliation, then the choice of
`g` fixes a transverse foliation `g = const`, and `φ` is the inverse function of the projection
of `{x | f x = y}` along this transverse foliation.
This version of the theorem is used to prove the other versions and can be used if a user
needs to have a complete control over the choice of the implicit function.
-/
/-- Data for the general version of the implicit function theorem. It holds two functions
`f : E → F` and `g : E → G` (named `left_fun` and `right_fun`) and a point `a` (named `pt`)
such that
* both functions are strictly differentiable at `a`;
* the derivatives are surjective;
* the kernels of the derivatives are complementary subspaces of `E`. -/
@[nolint has_nonempty_instance]
structure implicit_function_data (𝕜 : Type*) [nontrivially_normed_field 𝕜]
(E : Type*) [normed_add_comm_group E] [normed_space 𝕜 E] [complete_space E]
(F : Type*) [normed_add_comm_group F] [normed_space 𝕜 F] [complete_space F]
(G : Type*) [normed_add_comm_group G] [normed_space 𝕜 G] [complete_space G] :=
(left_fun : E → F)
(left_deriv : E →L[𝕜] F)
(right_fun : E → G)
(right_deriv : E →L[𝕜] G)
(pt : E)
(left_has_deriv : has_strict_fderiv_at left_fun left_deriv pt)
(right_has_deriv : has_strict_fderiv_at right_fun right_deriv pt)
(left_range : range left_deriv = ⊤)
(right_range : range right_deriv = ⊤)
(is_compl_ker : is_compl (ker left_deriv) (ker right_deriv))
namespace implicit_function_data
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] [complete_space E]
{F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] [complete_space F]
{G : Type*} [normed_add_comm_group G] [normed_space 𝕜 G] [complete_space G]
(φ : implicit_function_data 𝕜 E F G)
/-- The function given by `x ↦ (left_fun x, right_fun x)`. -/
def prod_fun (x : E) : F × G := (φ.left_fun x, φ.right_fun x)
@[simp] lemma prod_fun_apply (x : E) : φ.prod_fun x = (φ.left_fun x, φ.right_fun x) := rfl
protected lemma has_strict_fderiv_at :
has_strict_fderiv_at φ.prod_fun
(φ.left_deriv.equiv_prod_of_surjective_of_is_compl φ.right_deriv φ.left_range φ.right_range
φ.is_compl_ker : E →L[𝕜] F × G) φ.pt :=
φ.left_has_deriv.prod φ.right_has_deriv
/-- Implicit function theorem. If `f : E → F` and `g : E → G` are two maps strictly differentiable
at `a`, their derivatives `f'`, `g'` are surjective, and the kernels of these derivatives are
complementary subspaces of `E`, then `x ↦ (f x, g x)` defines a local homeomorphism between
`E` and `F × G`. In particular, `{x | f x = f a}` is locally homeomorphic to `G`. -/
def to_local_homeomorph : local_homeomorph E (F × G) :=
φ.has_strict_fderiv_at.to_local_homeomorph _
/-- Implicit function theorem. If `f : E → F` and `g : E → G` are two maps strictly differentiable
at `a`, their derivatives `f'`, `g'` are surjective, and the kernels of these derivatives are
complementary subspaces of `E`, then `implicit_function_of_is_compl_ker` is the unique (germ of a)
map `φ : F → G → E` such that `f (φ y z) = y` and `g (φ y z) = z`. -/
def implicit_function : F → G → E := function.curry $ φ.to_local_homeomorph.symm
@[simp] lemma to_local_homeomorph_coe : ⇑(φ.to_local_homeomorph) = φ.prod_fun := rfl
lemma to_local_homeomorph_apply (x : E) :
φ.to_local_homeomorph x = (φ.left_fun x, φ.right_fun x) :=
rfl
lemma pt_mem_to_local_homeomorph_source :
φ.pt ∈ φ.to_local_homeomorph.source :=
φ.has_strict_fderiv_at.mem_to_local_homeomorph_source
lemma map_pt_mem_to_local_homeomorph_target :
(φ.left_fun φ.pt, φ.right_fun φ.pt) ∈ φ.to_local_homeomorph.target :=
φ.to_local_homeomorph.map_source $ φ.pt_mem_to_local_homeomorph_source
lemma prod_map_implicit_function :
∀ᶠ (p : F × G) in 𝓝 (φ.prod_fun φ.pt), φ.prod_fun (φ.implicit_function p.1 p.2) = p :=
φ.has_strict_fderiv_at.eventually_right_inverse.mono $ λ ⟨z, y⟩ h, h
lemma left_map_implicit_function :
∀ᶠ (p : F × G) in 𝓝 (φ.prod_fun φ.pt), φ.left_fun (φ.implicit_function p.1 p.2) = p.1 :=
φ.prod_map_implicit_function.mono $ λ z, congr_arg prod.fst
lemma right_map_implicit_function :
∀ᶠ (p : F × G) in 𝓝 (φ.prod_fun φ.pt), φ.right_fun (φ.implicit_function p.1 p.2) = p.2 :=
φ.prod_map_implicit_function.mono $ λ z, congr_arg prod.snd
lemma implicit_function_apply_image :
∀ᶠ x in 𝓝 φ.pt, φ.implicit_function (φ.left_fun x) (φ.right_fun x) = x :=
φ.has_strict_fderiv_at.eventually_left_inverse
lemma map_nhds_eq : map φ.left_fun (𝓝 φ.pt) = 𝓝 (φ.left_fun φ.pt) :=
show map (prod.fst ∘ φ.prod_fun) (𝓝 φ.pt) = 𝓝 (φ.prod_fun φ.pt).1,
by rw [← map_map, φ.has_strict_fderiv_at.map_nhds_eq_of_equiv, map_fst_nhds]
lemma implicit_function_has_strict_fderiv_at
(g'inv : G →L[𝕜] E) (hg'inv : φ.right_deriv.comp g'inv = continuous_linear_map.id 𝕜 G)
(hg'invf : φ.left_deriv.comp g'inv = 0) :
has_strict_fderiv_at (φ.implicit_function (φ.left_fun φ.pt)) g'inv (φ.right_fun φ.pt) :=
begin
have := φ.has_strict_fderiv_at.to_local_inverse,
simp only [prod_fun] at this,
convert this.comp (φ.right_fun φ.pt)
((has_strict_fderiv_at_const _ _).prod (has_strict_fderiv_at_id _)),
simp only [continuous_linear_map.ext_iff, continuous_linear_map.coe_comp', function.comp_app]
at hg'inv hg'invf ⊢,
simp [continuous_linear_equiv.eq_symm_apply, *]
end
end implicit_function_data
namespace has_strict_fderiv_at
section complemented
/-!
### Case of a complemented kernel
In this section we prove the following version of the implicit function theorem. Consider a map
`f : E → F` and a point `a : E` such that `f` is strictly differentiable at `a`, its derivative `f'`
is surjective and the kernel of `f'` is a complemented subspace of `E` (i.e., it has a closed
complementary subspace). Then there exists a function `φ : F → ker f' → E` such that for `(y, z)`
close to `(f a, 0)` we have `f (φ y z) = y` and the derivative of `φ (f a)` at zero is the
embedding `ker f' → E`.
Note that a map with these properties is not unique. E.g., different choices of a subspace
complementary to `ker f'` lead to different maps `φ`.
-/
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] [complete_space E]
{F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] [complete_space F]
{f : E → F} {f' : E →L[𝕜] F} {a : E}
section defs
variables (f f')
/-- Data used to apply the generic implicit function theorem to the case of a strictly
differentiable map such that its derivative is surjective and has a complemented kernel. -/
@[simp] def implicit_function_data_of_complemented (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) (hker : (ker f').closed_complemented) :
implicit_function_data 𝕜 E F (ker f') :=
{ left_fun := f,
left_deriv := f',
right_fun := λ x, classical.some hker (x - a),
right_deriv := classical.some hker,
pt := a,
left_has_deriv := hf,
right_has_deriv := (classical.some hker).has_strict_fderiv_at.comp a
((has_strict_fderiv_at_id a).sub_const a),
left_range := hf',
right_range := linear_map.range_eq_of_proj (classical.some_spec hker),
is_compl_ker := linear_map.is_compl_of_proj (classical.some_spec hker) }
/-- A local homeomorphism between `E` and `F × f'.ker` sending level surfaces of `f`
to vertical subspaces. -/
def implicit_to_local_homeomorph_of_complemented (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) (hker : (ker f').closed_complemented) :
local_homeomorph E (F × (ker f')) :=
(implicit_function_data_of_complemented f f' hf hf' hker).to_local_homeomorph
/-- Implicit function `g` defined by `f (g z y) = z`. -/
def implicit_function_of_complemented (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) (hker : (ker f').closed_complemented) :
F → (ker f') → E :=
(implicit_function_data_of_complemented f f' hf hf' hker).implicit_function
end defs
@[simp] lemma implicit_to_local_homeomorph_of_complemented_fst (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) (hker : (ker f').closed_complemented) (x : E) :
(hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker x).fst = f x :=
rfl
lemma implicit_to_local_homeomorph_of_complemented_apply
(hf : has_strict_fderiv_at f f' a) (hf' : range f' = ⊤)
(hker : (ker f').closed_complemented) (y : E) :
hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker y =
(f y, classical.some hker (y - a)) :=
rfl
@[simp] lemma implicit_to_local_homeomorph_of_complemented_apply_ker
(hf : has_strict_fderiv_at f f' a) (hf' : range f' = ⊤)
(hker : (ker f').closed_complemented) (y : ker f') :
hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker (y + a) = (f (y + a), y) :=
by simp only [implicit_to_local_homeomorph_of_complemented_apply, add_sub_cancel,
classical.some_spec hker]
@[simp] lemma implicit_to_local_homeomorph_of_complemented_self
(hf : has_strict_fderiv_at f f' a) (hf' : range f' = ⊤) (hker : (ker f').closed_complemented) :
hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker a = (f a, 0) :=
by simp [hf.implicit_to_local_homeomorph_of_complemented_apply]
lemma mem_implicit_to_local_homeomorph_of_complemented_source (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) (hker : (ker f').closed_complemented) :
a ∈ (hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker).source :=
mem_to_local_homeomorph_source _
lemma mem_implicit_to_local_homeomorph_of_complemented_target (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) (hker : (ker f').closed_complemented) :
(f a, (0 : ker f')) ∈ (hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker).target :=
by simpa only [implicit_to_local_homeomorph_of_complemented_self] using
((hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker).map_source $
(hf.mem_implicit_to_local_homeomorph_of_complemented_source hf' hker))
/-- `implicit_function_of_complemented` sends `(z, y)` to a point in `f ⁻¹' z`. -/
lemma map_implicit_function_of_complemented_eq (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) (hker : (ker f').closed_complemented) :
∀ᶠ (p : F × (ker f')) in 𝓝 (f a, 0),
f (hf.implicit_function_of_complemented f f' hf' hker p.1 p.2) = p.1 :=
((hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker).eventually_right_inverse $
hf.mem_implicit_to_local_homeomorph_of_complemented_target hf' hker).mono $ λ ⟨z, y⟩ h,
congr_arg prod.fst h
/-- Any point in some neighborhood of `a` can be represented as `implicit_function`
of some point. -/
lemma eq_implicit_function_of_complemented (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) (hker : (ker f').closed_complemented) :
∀ᶠ x in 𝓝 a, hf.implicit_function_of_complemented f f' hf' hker (f x)
(hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker x).snd = x :=
(implicit_function_data_of_complemented f f' hf hf' hker).implicit_function_apply_image
@[simp] lemma implicit_function_of_complemented_apply_image (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) (hker : (ker f').closed_complemented) :
hf.implicit_function_of_complemented f f' hf' hker (f a) 0 = a :=
begin
convert (hf.implicit_to_local_homeomorph_of_complemented f f' hf' hker).left_inv
(hf.mem_implicit_to_local_homeomorph_of_complemented_source hf' hker),
exact congr_arg prod.snd (hf.implicit_to_local_homeomorph_of_complemented_self hf' hker).symm
end
lemma to_implicit_function_of_complemented (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) (hker : (ker f').closed_complemented) :
has_strict_fderiv_at (hf.implicit_function_of_complemented f f' hf' hker (f a))
(ker f').subtypeL 0 :=
begin
convert (implicit_function_data_of_complemented f f' hf hf'
hker).implicit_function_has_strict_fderiv_at (ker f').subtypeL _ _,
swap,
{ ext, simp only [classical.some_spec hker, implicit_function_data_of_complemented,
continuous_linear_map.coe_comp', submodule.coe_subtypeL', submodule.coe_subtype,
function.comp_app, continuous_linear_map.coe_id', id.def] },
swap,
{ ext, simp only [continuous_linear_map.coe_comp', submodule.coe_subtypeL', submodule.coe_subtype,
function.comp_app, linear_map.map_coe_ker, continuous_linear_map.zero_apply] },
simp only [implicit_function_data_of_complemented, map_sub, sub_self],
end
end complemented
/-!
### Finite dimensional case
In this section we prove the following version of the implicit function theorem. Consider a map
`f : E → F` from a Banach normed space to a finite dimensional space.
Take a point `a : E` such that `f` is strictly differentiable at `a` and its derivative `f'`
is surjective. Then there exists a function `φ : F → ker f' → E` such that for `(y, z)`
close to `(f a, 0)` we have `f (φ y z) = y` and the derivative of `φ (f a)` at zero is the
embedding `ker f' → E`.
This version deduces that `ker f'` is a complemented subspace from the fact that `F` is a finite
dimensional space, then applies the previous version.
Note that a map with these properties is not unique. E.g., different choices of a subspace
complementary to `ker f'` lead to different maps `φ`.
-/
section finite_dimensional
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] [complete_space 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] [complete_space E]
{F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] [finite_dimensional 𝕜 F]
(f : E → F) (f' : E →L[𝕜] F) {a : E}
/-- Given a map `f : E → F` to a finite dimensional space with a surjective derivative `f'`,
returns a local homeomorphism between `E` and `F × ker f'`. -/
def implicit_to_local_homeomorph (hf : has_strict_fderiv_at f f' a) (hf' : range f' = ⊤) :
local_homeomorph E (F × (ker f')) :=
by haveI := finite_dimensional.complete 𝕜 F; exact
hf.implicit_to_local_homeomorph_of_complemented f f' hf'
f'.ker_closed_complemented_of_finite_dimensional_range
/-- Implicit function `g` defined by `f (g z y) = z`. -/
def implicit_function (hf : has_strict_fderiv_at f f' a) (hf' : range f' = ⊤) :
F → (ker f') → E :=
function.curry $ (hf.implicit_to_local_homeomorph f f' hf').symm
variables {f f'}
@[simp] lemma implicit_to_local_homeomorph_fst (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) (x : E) :
(hf.implicit_to_local_homeomorph f f' hf' x).fst = f x :=
rfl
@[simp] lemma implicit_to_local_homeomorph_apply_ker
(hf : has_strict_fderiv_at f f' a) (hf' : range f' = ⊤) (y : ker f') :
hf.implicit_to_local_homeomorph f f' hf' (y + a) = (f (y + a), y) :=
by apply implicit_to_local_homeomorph_of_complemented_apply_ker
@[simp] lemma implicit_to_local_homeomorph_self
(hf : has_strict_fderiv_at f f' a) (hf' : range f' = ⊤) :
hf.implicit_to_local_homeomorph f f' hf' a = (f a, 0) :=
by apply implicit_to_local_homeomorph_of_complemented_self
lemma mem_implicit_to_local_homeomorph_source (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) :
a ∈ (hf.implicit_to_local_homeomorph f f' hf').source :=
mem_to_local_homeomorph_source _
lemma mem_implicit_to_local_homeomorph_target (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) :
(f a, (0 : ker f')) ∈ (hf.implicit_to_local_homeomorph f f' hf').target :=
by apply mem_implicit_to_local_homeomorph_of_complemented_target
lemma tendsto_implicit_function (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) {α : Type*} {l : filter α} {g₁ : α → F} {g₂ : α → ker f'}
(h₁ : tendsto g₁ l (𝓝 $ f a)) (h₂ : tendsto g₂ l (𝓝 0)) :
tendsto (λ t, hf.implicit_function f f' hf' (g₁ t) (g₂ t)) l (𝓝 a) :=
begin
refine ((hf.implicit_to_local_homeomorph f f' hf').tendsto_symm
(hf.mem_implicit_to_local_homeomorph_source hf')).comp _,
rw [implicit_to_local_homeomorph_self],
exact h₁.prod_mk_nhds h₂
end
alias tendsto_implicit_function ← _root_.filter.tendsto.implicit_function
/-- `implicit_function` sends `(z, y)` to a point in `f ⁻¹' z`. -/
lemma map_implicit_function_eq (hf : has_strict_fderiv_at f f' a) (hf' : range f' = ⊤) :
∀ᶠ (p : F × (ker f')) in 𝓝 (f a, 0), f (hf.implicit_function f f' hf' p.1 p.2) = p.1 :=
by apply map_implicit_function_of_complemented_eq
@[simp] lemma implicit_function_apply_image (hf : has_strict_fderiv_at f f' a)
(hf' : range f' = ⊤) :
hf.implicit_function f f' hf' (f a) 0 = a :=
by apply implicit_function_of_complemented_apply_image
/-- Any point in some neighborhood of `a` can be represented as `implicit_function`
of some point. -/
lemma eq_implicit_function (hf : has_strict_fderiv_at f f' a) (hf' : range f' = ⊤) :
∀ᶠ x in 𝓝 a, hf.implicit_function f f' hf' (f x)
(hf.implicit_to_local_homeomorph f f' hf' x).snd = x :=
by apply eq_implicit_function_of_complemented
lemma to_implicit_function (hf : has_strict_fderiv_at f f' a) (hf' : range f' = ⊤) :
has_strict_fderiv_at (hf.implicit_function f f' hf' (f a))
(ker f').subtypeL 0 :=
by apply to_implicit_function_of_complemented
end finite_dimensional
end has_strict_fderiv_at
|
3dd28c4c25ab9fad5a526d17b63bc0dab4bff22a | 36938939954e91f23dec66a02728db08a7acfcf9 | /lean4/deps/llvm-tablegen-support/lean/decodex86/default.lean | 9fd7a7fd5b9c67082ccf7a40a842fae8c7c719fd | [] | no_license | pnwamk/reopt-vcg | f8b56dd0279392a5e1c6aee721be8138e6b558d3 | c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d | refs/heads/master | 1,631,145,017,772 | 1,593,549,019,000 | 1,593,549,143,000 | 254,191,418 | 0 | 0 | null | 1,586,377,077,000 | 1,586,377,077,000 | null | UTF-8 | Lean | false | false | 15 | lean |
import .basic
|
e6e72996df49e00c3ed059b7746b39e33e5fedd4 | a9e33f9c83301c461f3c3ebc6799d9de1f6d4d20 | /notes/2019.10.14.prop_logic/prop_logic.lean | b92ee5ae276436ab025d1362e4b02247479bfa1e | [] | no_license | yl4df/Discrete-Mathematics | f1c9a6cf8cfb4686fb617637f69a481e1522f0c2 | c93ce9f6a6e36d194e350d9fa0a0360191e97fa0 | refs/heads/master | 1,598,714,938,443 | 1,572,275,647,000 | 1,572,275,647,000 | 218,074,726 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,230 | lean | namespace prop_logic
/-
**************
*** SYNTAX ***
**************
-/
inductive var : Type
| mkVar : ℕ → var
inductive unOp : Type
| notOp
inductive binOp : Type
| andOp
| orOp
inductive pExp : Type
| litExp : bool → pExp
| varExp : var → pExp
| unOpExp : unOp → pExp → pExp
| binOpExp : binOp → pExp → pExp → pExp
open var
open pExp
open unOp
open binOp
-- Shorthand notations
def pTrue := litExp tt
def pFalse := litExp ff
def pNot := unOpExp notOp
def pAnd := binOpExp andOp
def pOr := binOpExp orOp
-- conventional notation
notation e1 ∧ e2 := pAnd e1 e2
notation e1 ∨ e2 := pOr e1 e2
notation ¬ e := pNot e
/-
*****************
*** SEMANTICS ***
*****************
-/
def interpUnOp : unOp → (bool → bool)
| notOp := bnot
def interpBinOp : binOp → (bool → bool → bool)
| andOp := band
| orOp := bor
/-
Given a pExp and an interpretation
for the variables, evaluate the pExp
to determine its Boolean truth value.
-/
def pEval : pExp → (var → bool) → bool
| (litExp b) i := b
| (varExp v) i := i v
| (unOpExp op e) i :=
(interpUnOp op) (pEval e i)
| (binOpExp op e1 e2) i :=
(interpBinOp op)(pEval e1 i) (pEval e2 i)
end prop_logic |
cf95d95051c9b9f3e0db298a41af90f4ea81ff76 | f3a5af2927397cf346ec0e24312bfff077f00425 | /src/game/world4/level3.lean | 43a7bd27973ba8ad700803a00858bc0845a51ba5 | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/natural_number_game | 05c39e1586408cfb563d1a12e1085a90726ab655 | f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd | refs/heads/master | 1,688,570,964,990 | 1,636,908,242,000 | 1,636,908,242,000 | 195,403,790 | 277 | 84 | Apache-2.0 | 1,694,547,955,000 | 1,562,328,792,000 | Lean | UTF-8 | Lean | false | false | 323 | lean | import game.world4.level2 -- hide
namespace mynat -- hide
/-
# Power World
## Level 3: `pow_one`
-/
/- Lemma
For all naturals $a$, $a ^ 1 = a$.
-/
lemma pow_one (a : mynat) : a ^ (1 : mynat) = a :=
begin [nat_num_game]
rw one_eq_succ_zero,
rw pow_succ,
rw pow_zero,
rw one_mul,
refl,
end
end mynat -- hide
|
8020245881fcf0d3e5d66a3d235d2c90efbdc328 | 618003631150032a5676f229d13a079ac875ff77 | /src/data/analysis/filter.lean | d728e1b420e77a661ad7267dabc4f8ce1fb18a83 | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 11,841 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
Computational realization of filters (experimental).
-/
import order.filter.basic
open set filter
/-- A `cfilter α σ` is a realization of a filter (base) on `α`,
represented by a type `σ` together with operations for the top element and
the binary inf operation. -/
structure cfilter (α σ : Type*) [partial_order α] :=
(f : σ → α)
(pt : σ)
(inf : σ → σ → σ)
(inf_le_left : ∀ a b : σ, f (inf a b) ≤ f a)
(inf_le_right : ∀ a b : σ, f (inf a b) ≤ f b)
variables {α : Type*} {β : Type*} {σ : Type*} {τ : Type*}
namespace cfilter
section
variables [partial_order α] (F : cfilter α σ)
instance : has_coe_to_fun (cfilter α σ) := ⟨_, cfilter.f⟩
@[simp] theorem coe_mk (f pt inf h₁ h₂ a) : (@cfilter.mk α σ _ f pt inf h₁ h₂) a = f a := rfl
/-- Map a cfilter to an equivalent representation type. -/
def of_equiv (E : σ ≃ τ) : cfilter α σ → cfilter α τ
| ⟨f, p, g, h₁, h₂⟩ :=
{ f := λ a, f (E.symm a),
pt := E p,
inf := λ a b, E (g (E.symm a) (E.symm b)),
inf_le_left := λ a b, by simpa using h₁ (E.symm a) (E.symm b),
inf_le_right := λ a b, by simpa using h₂ (E.symm a) (E.symm b) }
@[simp] theorem of_equiv_val (E : σ ≃ τ) (F : cfilter α σ) (a : τ) :
F.of_equiv E a = F (E.symm a) := by cases F; refl
end
/-- The filter represented by a `cfilter` is the collection of supersets of
elements of the filter base. -/
def to_filter (F : cfilter (set α) σ) : filter α :=
{ sets := {a | ∃ b, F b ⊆ a},
univ_sets := ⟨F.pt, subset_univ _⟩,
sets_of_superset := λ x y ⟨b, h⟩ s, ⟨b, subset.trans h s⟩,
inter_sets := λ x y ⟨a, h₁⟩ ⟨b, h₂⟩, ⟨F.inf a b,
subset_inter (subset.trans (F.inf_le_left _ _) h₁) (subset.trans (F.inf_le_right _ _) h₂)⟩ }
@[simp] theorem mem_to_filter_sets (F : cfilter (set α) σ) {a : set α} :
a ∈ F.to_filter ↔ ∃ b, F b ⊆ a := iff.rfl
end cfilter
/-- A realizer for filter `f` is a cfilter which generates `f`. -/
structure filter.realizer (f : filter α) :=
(σ : Type*)
(F : cfilter (set α) σ)
(eq : F.to_filter = f)
protected def cfilter.to_realizer (F : cfilter (set α) σ) : F.to_filter.realizer := ⟨σ, F, rfl⟩
namespace filter.realizer
theorem mem_sets {f : filter α} (F : f.realizer) {a : set α} : a ∈ f ↔ ∃ b, F.F b ⊆ a :=
by cases F; subst f; simp
-- Used because it has better definitional equalities than the eq.rec proof
def of_eq {f g : filter α} (e : f = g) (F : f.realizer) : g.realizer :=
⟨F.σ, F.F, F.eq.trans e⟩
/-- A filter realizes itself. -/
def of_filter (f : filter α) : f.realizer := ⟨f.sets,
{ f := subtype.val,
pt := ⟨univ, univ_mem_sets⟩,
inf := λ ⟨x, h₁⟩ ⟨y, h₂⟩, ⟨_, inter_mem_sets h₁ h₂⟩,
inf_le_left := λ ⟨x, h₁⟩ ⟨y, h₂⟩, inter_subset_left x y,
inf_le_right := λ ⟨x, h₁⟩ ⟨y, h₂⟩, inter_subset_right x y },
filter_eq $ set.ext $ λ x, set_coe.exists.trans exists_sets_subset_iff⟩
/-- Transfer a filter realizer to another realizer on a different base type. -/
def of_equiv {f : filter α} (F : f.realizer) (E : F.σ ≃ τ) : f.realizer :=
⟨τ, F.F.of_equiv E, by refine eq.trans _ F.eq; exact filter_eq (set.ext $ λ x,
⟨λ ⟨s, h⟩, ⟨E.symm s, by simpa using h⟩, λ ⟨t, h⟩, ⟨E t, by simp [h]⟩⟩)⟩
@[simp] theorem of_equiv_σ {f : filter α} (F : f.realizer) (E : F.σ ≃ τ) : (F.of_equiv E).σ = τ := rfl
@[simp] theorem of_equiv_F {f : filter α} (F : f.realizer) (E : F.σ ≃ τ) (s : τ) :
(F.of_equiv E).F s = F.F (E.symm s) := by delta of_equiv; simp
/-- `unit` is a realizer for the principal filter -/
protected def principal (s : set α) : (principal s).realizer := ⟨unit,
{ f := λ _, s,
pt := (),
inf := λ _ _, (),
inf_le_left := λ _ _, le_refl _,
inf_le_right := λ _ _, le_refl _ },
filter_eq $ set.ext $ λ x,
⟨λ ⟨_, s⟩, s, λ h, ⟨(), h⟩⟩⟩
@[simp] theorem principal_σ (s : set α) : (realizer.principal s).σ = unit := rfl
@[simp] theorem principal_F (s : set α) (u : unit) : (realizer.principal s).F u = s := rfl
/-- `unit` is a realizer for the top filter -/
protected def top : (⊤ : filter α).realizer :=
(realizer.principal _).of_eq principal_univ
@[simp] theorem top_σ : (@realizer.top α).σ = unit := rfl
@[simp] theorem top_F (u : unit) : (@realizer.top α).F u = univ := rfl
/-- `unit` is a realizer for the bottom filter -/
protected def bot : (⊥ : filter α).realizer :=
(realizer.principal _).of_eq principal_empty
@[simp] theorem bot_σ : (@realizer.bot α).σ = unit := rfl
@[simp] theorem bot_F (u : unit) : (@realizer.bot α).F u = ∅ := rfl
/-- Construct a realizer for `map m f` given a realizer for `f` -/
protected def map (m : α → β) {f : filter α} (F : f.realizer) : (map m f).realizer := ⟨F.σ,
{ f := λ s, image m (F.F s),
pt := F.F.pt,
inf := F.F.inf,
inf_le_left := λ a b, image_subset _ (F.F.inf_le_left _ _),
inf_le_right := λ a b, image_subset _ (F.F.inf_le_right _ _) },
filter_eq $ set.ext $ λ x, by simp [cfilter.to_filter]; rw F.mem_sets; exact
exists_congr (λ s, image_subset_iff)⟩
@[simp] theorem map_σ (m : α → β) {f : filter α} (F : f.realizer) : (F.map m).σ = F.σ := rfl
@[simp] theorem map_F (m : α → β) {f : filter α} (F : f.realizer) (s) :
(F.map m).F s = image m (F.F s) := rfl
/-- Construct a realizer for `comap m f` given a realizer for `f` -/
protected def comap (m : α → β) {f : filter β} (F : f.realizer) : (comap m f).realizer := ⟨F.σ,
{ f := λ s, preimage m (F.F s),
pt := F.F.pt,
inf := F.F.inf,
inf_le_left := λ a b, preimage_mono (F.F.inf_le_left _ _),
inf_le_right := λ a b, preimage_mono (F.F.inf_le_right _ _) },
filter_eq $ set.ext $ λ x, by cases F; subst f; simp [cfilter.to_filter, mem_comap_sets]; exact
⟨λ ⟨s, h⟩, ⟨_, ⟨s, subset.refl _⟩, h⟩,
λ ⟨y, ⟨s, h⟩, h₂⟩, ⟨s, subset.trans (preimage_mono h) h₂⟩⟩⟩
/-- Construct a realizer for the sup of two filters -/
protected def sup {f g : filter α} (F : f.realizer) (G : g.realizer) : (f ⊔ g).realizer := ⟨F.σ × G.σ,
{ f := λ ⟨s, t⟩, F.F s ∪ G.F t,
pt := (F.F.pt, G.F.pt),
inf := λ ⟨a, a'⟩ ⟨b, b'⟩, (F.F.inf a b, G.F.inf a' b'),
inf_le_left := λ ⟨a, a'⟩ ⟨b, b'⟩, union_subset_union (F.F.inf_le_left _ _) (G.F.inf_le_left _ _),
inf_le_right := λ ⟨a, a'⟩ ⟨b, b'⟩, union_subset_union (F.F.inf_le_right _ _) (G.F.inf_le_right _ _) },
filter_eq $ set.ext $ λ x, by cases F; cases G; substs f g; simp [cfilter.to_filter]; exact
⟨λ ⟨s, t, h⟩, ⟨⟨s, subset.trans (subset_union_left _ _) h⟩,
⟨t, subset.trans (subset_union_right _ _) h⟩⟩,
λ ⟨⟨s, h₁⟩, ⟨t, h₂⟩⟩, ⟨s, t, union_subset h₁ h₂⟩⟩⟩
/-- Construct a realizer for the inf of two filters -/
protected def inf {f g : filter α} (F : f.realizer) (G : g.realizer) : (f ⊓ g).realizer := ⟨F.σ × G.σ,
{ f := λ ⟨s, t⟩, F.F s ∩ G.F t,
pt := (F.F.pt, G.F.pt),
inf := λ ⟨a, a'⟩ ⟨b, b'⟩, (F.F.inf a b, G.F.inf a' b'),
inf_le_left := λ ⟨a, a'⟩ ⟨b, b'⟩, inter_subset_inter (F.F.inf_le_left _ _) (G.F.inf_le_left _ _),
inf_le_right := λ ⟨a, a'⟩ ⟨b, b'⟩, inter_subset_inter (F.F.inf_le_right _ _) (G.F.inf_le_right _ _) },
filter_eq $ set.ext $ λ x, by cases F; cases G; substs f g; simp [cfilter.to_filter]; exact
⟨λ ⟨s, t, h⟩, ⟨_, ⟨s, subset.refl _⟩, _, ⟨t, subset.refl _⟩, h⟩,
λ ⟨y, ⟨s, h₁⟩, z, ⟨t, h₂⟩, h⟩, ⟨s, t, subset.trans (inter_subset_inter h₁ h₂) h⟩⟩⟩
/-- Construct a realizer for the cofinite filter -/
protected def cofinite [decidable_eq α] : (@cofinite α).realizer := ⟨finset α,
{ f := λ s, {a | a ∉ s},
pt := ∅,
inf := (∪),
inf_le_left := λ s t a, mt (finset.mem_union_left _),
inf_le_right := λ s t a, mt (finset.mem_union_right _) },
filter_eq $ set.ext $ λ x, by simp [cfilter.to_filter]; exactI
⟨λ ⟨s, h⟩, finite_subset (finite_mem_finset s) (compl_subset_comm.1 h),
λ ⟨fs⟩, ⟨(-x).to_finset, λ a (h : a ∉ (-x).to_finset),
classical.by_contradiction $ λ h', h (mem_to_finset.2 h')⟩⟩⟩
/-- Construct a realizer for filter bind -/
protected def bind {f : filter α} {m : α → filter β} (F : f.realizer) (G : ∀ i, (m i).realizer) :
(f.bind m).realizer :=
⟨Σ s : F.σ, Π i ∈ F.F s, (G i).σ,
{ f := λ ⟨s, f⟩, ⋃ i ∈ F.F s, (G i).F (f i H),
pt := ⟨F.F.pt, λ i H, (G i).F.pt⟩,
inf := λ ⟨a, f⟩ ⟨b, f'⟩, ⟨F.F.inf a b, λ i h,
(G i).F.inf (f i (F.F.inf_le_left _ _ h)) (f' i (F.F.inf_le_right _ _ h))⟩,
inf_le_left := λ ⟨a, f⟩ ⟨b, f'⟩ x,
show (x ∈ ⋃ (i : α) (H : i ∈ F.F (F.F.inf a b)), _) →
x ∈ ⋃ i (H : i ∈ F.F a), ((G i).F) (f i H), by simp; exact
λ i h₁ h₂, ⟨i, F.F.inf_le_left _ _ h₁, (G i).F.inf_le_left _ _ h₂⟩,
inf_le_right := λ ⟨a, f⟩ ⟨b, f'⟩ x,
show (x ∈ ⋃ (i : α) (H : i ∈ F.F (F.F.inf a b)), _) →
x ∈ ⋃ i (H : i ∈ F.F b), ((G i).F) (f' i H), by simp; exact
λ i h₁ h₂, ⟨i, F.F.inf_le_right _ _ h₁, (G i).F.inf_le_right _ _ h₂⟩ },
filter_eq $ set.ext $ λ x, by cases F with _ F _; subst f; simp [cfilter.to_filter, mem_bind_sets]; exact
⟨λ ⟨s, f, h⟩, ⟨F s, ⟨s, subset.refl _⟩, λ i H, (G i).mem_sets.2
⟨f i H, λ a h', h ⟨_, ⟨i, rfl⟩, _, ⟨H, rfl⟩, h'⟩⟩⟩,
λ ⟨y, ⟨s, h⟩, f⟩,
let ⟨f', h'⟩ := classical.axiom_of_choice (λ i:F s, (G i).mem_sets.1 (f i (h i.2))) in
⟨s, λ i h, f' ⟨i, h⟩, λ a ⟨_, ⟨i, rfl⟩, _, ⟨H, rfl⟩, m⟩, h' ⟨_, H⟩ m⟩⟩⟩
/-- Construct a realizer for indexed supremum -/
protected def Sup {f : α → filter β} (F : ∀ i, (f i).realizer) : (⨆ i, f i).realizer :=
let F' : (⨆ i, f i).realizer :=
((realizer.bind realizer.top F).of_eq $
filter_eq $ set.ext $ by simp [filter.bind, eq_univ_iff_forall, supr_sets_eq]) in
F'.of_equiv $ show (Σ u:unit, Π (i : α), true → (F i).σ) ≃ Π i, (F i).σ, from
⟨λ⟨_,f⟩ i, f i ⟨⟩, λ f, ⟨(), λ i _, f i⟩,
λ ⟨⟨⟩, f⟩, by dsimp; congr; simp, λ f, rfl⟩
/-- Construct a realizer for the product of filters -/
protected def prod {f g : filter α} (F : f.realizer) (G : g.realizer) : (f.prod g).realizer :=
(F.comap _).inf (G.comap _)
theorem le_iff {f g : filter α} (F : f.realizer) (G : g.realizer) :
f ≤ g ↔ ∀ b : G.σ, ∃ a : F.σ, F.F a ≤ G.F b :=
⟨λ H t, F.mem_sets.1 (H (G.mem_sets.2 ⟨t, subset.refl _⟩)),
λ H x h, F.mem_sets.2 $
let ⟨s, h₁⟩ := G.mem_sets.1 h, ⟨t, h₂⟩ := H s in ⟨t, subset.trans h₂ h₁⟩⟩
theorem tendsto_iff (f : α → β) {l₁ : filter α} {l₂ : filter β} (L₁ : l₁.realizer) (L₂ : l₂.realizer) :
tendsto f l₁ l₂ ↔ ∀ b, ∃ a, ∀ x ∈ L₁.F a, f x ∈ L₂.F b :=
(le_iff (L₁.map f) L₂).trans $ forall_congr $ λ b, exists_congr $ λ a, image_subset_iff
theorem ne_bot_iff {f : filter α} (F : f.realizer) :
f ≠ ⊥ ↔ ∀ a : F.σ, (F.F a).nonempty :=
begin
classical,
rw [not_iff_comm, ← le_bot_iff, F.le_iff realizer.bot, not_forall],
simp only [set.not_nonempty_iff_eq_empty],
exact ⟨λ ⟨x, e⟩ _, ⟨x, le_of_eq e⟩,
λ h, let ⟨x, h⟩ := h () in ⟨x, le_bot_iff.1 h⟩⟩
end
end filter.realizer
|
a458a620fdaec87180c46e886a8bfa532e2575bb | 94e33a31faa76775069b071adea97e86e218a8ee | /src/set_theory/game/nim.lean | e6c3083d66144879c3c83623ac9572a7eb69d67f | [
"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 | 14,073 | lean | /-
Copyright (c) 2020 Fox Thomson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Fox Thomson, Markus Himmel
-/
import data.nat.bitwise
import set_theory.game.birthday
import set_theory.game.impartial
/-!
# Nim and the Sprague-Grundy theorem
This file contains the definition for nim for any ordinal `o`. In the game of `nim o₁` both players
may move to `nim o₂` for any `o₂ < o₁`.
We also define a Grundy value for an impartial game `G` and prove the Sprague-Grundy theorem, that
`G` is equivalent to `nim (grundy_value G)`.
Finally, we compute the sum of finite Grundy numbers: if `G` and `H` have Grundy values `n` and `m`,
where `n` and `m` are natural numbers, then `G + H` has the Grundy value `n xor m`.
## Implementation details
The pen-and-paper definition of nim defines the possible moves of `nim o` to be `{o' | o' < o}`.
However, this definition does not work for us because it would make the type of nim
`ordinal.{u} → pgame.{u + 1}`, which would make it impossible for us to state the Sprague-Grundy
theorem, since that requires the type of `nim` to be `ordinal.{u} → pgame.{u}`. For this reason, we
instead use `o.out.α` for the possible moves, which makes proofs significantly more messy and
tedious, but avoids the universe bump.
The lemma `nim_def` is somewhat prone to produce "motive is not type correct" errors. If you run
into this problem, you may find the lemmas `exists_ordinal_move_left_eq` and `exists_move_left_eq`
useful.
-/
universes u
/-- `ordinal.out'` has the sole purpose of making `nim` computable. It performs the same job as
`quotient.out` but is specific to ordinals. -/
def ordinal.out' (o : ordinal) : Well_order :=
⟨o.out.α, (<), o.out.wo⟩
/-- The definition of single-heap nim, which can be viewed as a pile of stones where each player can
take a positive number of stones from it on their turn. -/
def nim : ordinal → pgame
| o₁ := let f := λ o₂, have hwf : ordinal.typein o₁.out'.r o₂ < o₁ := ordinal.typein_lt_self o₂,
nim (ordinal.typein o₁.out'.r o₂) in ⟨o₁.out'.α, o₁.out'.α, f, f⟩
using_well_founded { dec_tac := tactic.assumption }
namespace pgame
local infix ` ⧏ `:50 := lf
local infix ` ≈ ` := equiv
local infix ` ∥ `:50 := fuzzy
local infix ` ≡r `:50 := relabelling
namespace nim
open ordinal
lemma nim_def (o : ordinal) : nim o = pgame.mk o.out.α o.out.α
(λ o₂, nim (ordinal.typein (<) o₂))
(λ o₂, nim (ordinal.typein (<) o₂)) :=
by { rw nim, refl }
instance : is_empty (nim 0).left_moves :=
by { rw nim_def, exact ordinal.is_empty_out_zero }
instance : is_empty (nim 0).right_moves :=
by { rw nim_def, exact ordinal.is_empty_out_zero }
noncomputable instance : unique (nim 1).left_moves :=
by { rw nim_def, exact ordinal.unique_out_one }
noncomputable instance : unique (nim 1).right_moves :=
by { rw nim_def, exact ordinal.unique_out_one }
/-- `nim 0` has exactly the same moves as `0`. -/
def nim_zero_relabelling : nim 0 ≡r 0 := relabelling.is_empty _
@[simp] theorem nim_zero_equiv : nim 0 ≈ 0 := equiv.is_empty _
/-- `nim 1` has exactly the same moves as `star`. -/
noncomputable def nim_one_relabelling : nim 1 ≡r star :=
begin
rw nim_def,
refine ⟨_, _, λ i, _, λ j, _⟩,
any_goals { dsimp, apply equiv.equiv_of_unique },
all_goals { simp, exact nim_zero_relabelling }
end
@[simp] theorem nim_one_equiv : nim 1 ≈ star := nim_one_relabelling.equiv
@[simp] lemma nim_birthday (o : ordinal) : (nim o).birthday = o :=
begin
induction o using ordinal.induction with o IH,
rw [nim_def, birthday_def],
dsimp,
rw max_eq_right le_rfl,
convert lsub_typein o,
exact funext (λ i, IH _ (typein_lt_self i))
end
lemma left_moves_nim (o : ordinal) : (nim o).left_moves = o.out.α :=
by { rw nim_def, refl }
lemma right_moves_nim (o : ordinal) : (nim o).right_moves = o.out.α :=
by { rw nim_def, refl }
lemma move_left_nim_heq (o : ordinal) : (nim o).move_left == λ i : o.out.α, nim (typein (<) i) :=
by { rw nim_def, refl }
lemma move_right_nim_heq (o : ordinal) : (nim o).move_right == λ i : o.out.α, nim (typein (<) i) :=
by { rw nim_def, refl }
/-- Turns an ordinal less than `o` into a left move for `nim o` and viceversa. -/
noncomputable def to_left_moves_nim {o : ordinal} : set.Iio o ≃ (nim o).left_moves :=
(enum_iso_out o).to_equiv.trans (equiv.cast (left_moves_nim o).symm)
/-- Turns an ordinal less than `o` into a right move for `nim o` and viceversa. -/
noncomputable def to_right_moves_nim {o : ordinal} : set.Iio o ≃ (nim o).right_moves :=
(enum_iso_out o).to_equiv.trans (equiv.cast (right_moves_nim o).symm)
@[simp] theorem to_left_moves_nim_symm_lt {o : ordinal} (i : (nim o).left_moves) :
↑(to_left_moves_nim.symm i) < o :=
(to_left_moves_nim.symm i).prop
@[simp] theorem to_right_moves_nim_symm_lt {o : ordinal} (i : (nim o).right_moves) :
↑(to_right_moves_nim.symm i) < o :=
(to_right_moves_nim.symm i).prop
@[simp] lemma move_left_nim' {o : ordinal.{u}} (i) :
(nim o).move_left i = nim (to_left_moves_nim.symm i).val :=
(congr_heq (move_left_nim_heq o).symm (cast_heq _ i)).symm
lemma move_left_nim {o : ordinal} (i) :
(nim o).move_left (to_left_moves_nim i) = nim i :=
by simp
@[simp] lemma move_right_nim' {o : ordinal} (i) :
(nim o).move_right i = nim (to_right_moves_nim.symm i).val :=
(congr_heq (move_right_nim_heq o).symm (cast_heq _ i)).symm
lemma move_right_nim {o : ordinal} (i) :
(nim o).move_right (to_right_moves_nim i) = nim i :=
by simp
@[simp] lemma neg_nim (o : ordinal) : -nim o = nim o :=
begin
induction o using ordinal.induction with o IH,
rw nim_def, dsimp; congr;
funext i;
exact IH _ (ordinal.typein_lt_self i)
end
instance nim_impartial (o : ordinal) : impartial (nim o) :=
begin
induction o using ordinal.induction with o IH,
rw [impartial_def, neg_nim],
refine ⟨equiv_rfl, λ i, _, λ i, _⟩;
simpa using IH _ (typein_lt_self _)
end
lemma exists_ordinal_move_left_eq {o : ordinal} (i) : ∃ o' < o, (nim o).move_left i = nim o' :=
⟨_, typein_lt_self _, move_left_nim' i⟩
lemma exists_move_left_eq {o o' : ordinal} (h : o' < o) : ∃ i, (nim o).move_left i = nim o' :=
⟨to_left_moves_nim ⟨o', h⟩, by simp⟩
lemma non_zero_first_wins {o : ordinal} (ho : o ≠ 0) : nim o ∥ 0 :=
begin
rw [impartial.fuzzy_zero_iff_lf, nim_def, lf_zero_le],
rw ←ordinal.pos_iff_ne_zero at ho,
exact ⟨(ordinal.principal_seg_out ho).top, by simp⟩
end
@[simp] lemma add_equiv_zero_iff_eq (o₁ o₂ : ordinal) : nim o₁ + nim o₂ ≈ 0 ↔ o₁ = o₂ :=
begin
split,
{ contrapose,
intro h,
rw [impartial.not_equiv_zero_iff],
wlog h' : o₁ ≤ o₂ using [o₁ o₂, o₂ o₁],
{ exact le_total o₁ o₂ },
{ have h : o₁ < o₂ := lt_of_le_of_ne h' h,
rw [impartial.fuzzy_zero_iff_gf, zero_lf_le, nim_def o₂],
refine ⟨to_left_moves_add (sum.inr _), _⟩,
{ exact (ordinal.principal_seg_out h).top },
{ simpa using (impartial.add_self (nim o₁)).2 } },
{ exact (fuzzy_congr_left add_comm_equiv).1 (this (ne.symm h)) } },
{ rintro rfl,
exact impartial.add_self (nim o₁) }
end
@[simp] lemma add_fuzzy_zero_iff_ne (o₁ o₂ : ordinal) : nim o₁ + nim o₂ ∥ 0 ↔ o₁ ≠ o₂ :=
by rw [iff_not_comm, impartial.not_fuzzy_zero_iff, add_equiv_zero_iff_eq]
@[simp] lemma equiv_iff_eq (o₁ o₂ : ordinal) : nim o₁ ≈ nim o₂ ↔ o₁ = o₂ :=
by rw [impartial.equiv_iff_add_equiv_zero, add_equiv_zero_iff_eq]
end nim
/-- The Grundy value of an impartial game, the ordinal which corresponds to the game of nim that the
game is equivalent to -/
noncomputable def grundy_value : Π (G : pgame.{u}), ordinal.{u}
| G := ordinal.mex.{u u} (λ i, grundy_value (G.move_left i))
using_well_founded { dec_tac := pgame_wf_tac }
lemma grundy_value_def (G : pgame) :
grundy_value G = ordinal.mex.{u u} (λ i, grundy_value (G.move_left i)) :=
by rw grundy_value
/-- The Sprague-Grundy theorem which states that every impartial game is equivalent to a game of
nim, namely the game of nim corresponding to the games Grundy value -/
theorem equiv_nim_grundy_value : ∀ (G : pgame.{u}) [G.impartial], G ≈ nim (grundy_value G)
| G :=
begin
introI hG,
rw [impartial.equiv_iff_add_equiv_zero, ←impartial.forall_left_moves_fuzzy_iff_equiv_zero],
intro i,
apply left_moves_add_cases i,
{ intro i₁,
rw add_move_left_inl,
apply (fuzzy_congr_left (add_congr_left (equiv_nim_grundy_value (G.move_left i₁)).symm)).1,
rw nim.add_fuzzy_zero_iff_ne,
intro heq,
rw [eq_comm, grundy_value_def G] at heq,
have h := ordinal.ne_mex _,
rw heq at h,
exact (h i₁).irrefl },
{ intro i₂,
rw [add_move_left_inr, ←impartial.exists_left_move_equiv_iff_fuzzy_zero],
revert i₂,
rw nim.nim_def,
intro i₂,
have h' : ∃ i : G.left_moves, (grundy_value (G.move_left i)) =
ordinal.typein (quotient.out (grundy_value G)).r i₂,
{ revert i₂,
rw grundy_value_def,
intros i₂,
have hnotin : _ ∉ _ := λ hin, (le_not_le_of_lt (ordinal.typein_lt_self i₂)).2 (cInf_le' hin),
simpa using hnotin},
cases h' with i hi,
use to_left_moves_add (sum.inl i),
rw [add_move_left_inl, move_left_mk],
apply (add_congr_left (equiv_nim_grundy_value (G.move_left i))).trans,
simpa only [hi] using impartial.add_self (nim (grundy_value (G.move_left i))) }
end
using_well_founded { dec_tac := pgame_wf_tac }
@[simp] lemma grundy_value_eq_iff_equiv_nim (G : pgame) [G.impartial] (o : ordinal) :
grundy_value G = o ↔ G ≈ nim o :=
⟨by { rintro rfl, exact equiv_nim_grundy_value G },
by { intro h, rw ←nim.equiv_iff_eq, exact (equiv_nim_grundy_value G).symm.trans h }⟩
lemma nim.grundy_value (o : ordinal.{u}) : grundy_value (nim o) = o :=
by simp
@[simp] lemma grundy_value_eq_iff_equiv (G H : pgame) [G.impartial] [H.impartial] :
grundy_value G = grundy_value H ↔ G ≈ H :=
(grundy_value_eq_iff_equiv_nim _ _).trans (equiv_congr_left.1 (equiv_nim_grundy_value H) _).symm
@[simp] lemma grundy_value_zero : grundy_value 0 = 0 := by simp [nim.nim_zero_equiv.symm]
@[simp] lemma grundy_value_iff_equiv_zero (G : pgame) [G.impartial] : grundy_value G = 0 ↔ G ≈ 0 :=
by rw [←grundy_value_eq_iff_equiv, grundy_value_zero]
lemma grundy_value_star : grundy_value star = 1 := by simp [nim.nim_one_equiv.symm]
@[simp] lemma grundy_value_nim_add_nim (n m : ℕ) :
grundy_value (nim.{u} n + nim.{u} m) = nat.lxor n m :=
begin
induction n using nat.strong_induction_on with n hn generalizing m,
induction m using nat.strong_induction_on with m hm,
rw [grundy_value_def],
-- We want to show that `n xor m` is the smallest unreachable Grundy value. We will do this in two
-- steps:
-- h₀: `n xor m` is not a reachable grundy number.
-- h₁: every Grundy number strictly smaller than `n xor m` is reachable.
have h₀ : ∀ i, grundy_value ((nim n + nim m).move_left i) ≠ (nat.lxor n m : ordinal),
{ -- To show that `n xor m` is unreachable, we show that every move produces a Grundy number
-- different from `n xor m`.
intro i,
-- The move operates either on the left pile or on the right pile.
apply left_moves_add_cases i,
all_goals
{ -- One of the piles is reduced to `k` stones, with `k < n` or `k < m`.
intro a,
obtain ⟨ok, hk, hk'⟩ := nim.exists_ordinal_move_left_eq a,
obtain ⟨k, rfl⟩ := ordinal.lt_omega.1 (lt_trans hk (ordinal.nat_lt_omega _)),
replace hk := ordinal.nat_cast_lt.1 hk,
-- Thus, the problem is reduced to computing the Grundy value of `nim n + nim k` or
-- `nim k + nim m`, both of which can be dealt with using an inductive hypothesis.
simp only [hk', add_move_left_inl, add_move_left_inr, id],
rw hn _ hk <|> rw hm _ hk,
-- But of course xor is injective, so if we change one of the arguments, we will not get the
-- same value again.
intro h,
rw ordinal.nat_cast_inj at h,
try { rw [nat.lxor_comm n k, nat.lxor_comm n m] at h },
exact hk.ne (nat.lxor_left_injective h) } },
have h₁ : ∀ (u : ordinal), u < nat.lxor n m →
u ∈ set.range (λ i, grundy_value ((nim n + nim m).move_left i)),
{ -- Take any natural number `u` less than `n xor m`.
intros ou hu,
obtain ⟨u, rfl⟩ := ordinal.lt_omega.1 (lt_trans hu (ordinal.nat_lt_omega _)),
replace hu := ordinal.nat_cast_lt.1 hu,
-- Our goal is to produce a move that gives the Grundy value `u`.
rw set.mem_range,
-- By a lemma about xor, either `u xor m < n` or `u xor n < m`.
cases nat.lt_lxor_cases hu with h h,
-- Therefore, we can play the corresponding move, and by the inductive hypothesis the new state
-- is `(u xor m) xor m = u` or `n xor (u xor n) = u` as required.
{ obtain ⟨i, hi⟩ := nim.exists_move_left_eq (ordinal.nat_cast_lt.2 h),
refine ⟨to_left_moves_add (sum.inl i), _⟩,
simp only [hi, add_move_left_inl],
rw [hn _ h, nat.lxor_assoc, nat.lxor_self, nat.lxor_zero] },
{ obtain ⟨i, hi⟩ := nim.exists_move_left_eq (ordinal.nat_cast_lt.2 h),
refine ⟨to_left_moves_add (sum.inr i), _⟩,
simp only [hi, add_move_left_inr],
rw [hm _ h, nat.lxor_comm, nat.lxor_assoc, nat.lxor_self, nat.lxor_zero] } },
-- We are done!
apply (ordinal.mex_le_of_ne.{u u} h₀).antisymm,
contrapose! h₁,
exact ⟨_, ⟨h₁, ordinal.mex_not_mem_range _⟩⟩,
end
lemma nim_add_nim_equiv {n m : ℕ} : nim n + nim m ≈ nim (nat.lxor n m) :=
by rw [←grundy_value_eq_iff_equiv_nim, grundy_value_nim_add_nim]
lemma grundy_value_add (G H : pgame) [G.impartial] [H.impartial] {n m : ℕ} (hG : grundy_value G = n)
(hH : grundy_value H = m) : grundy_value (G + H) = nat.lxor n m :=
begin
rw [←nim.grundy_value (nat.lxor n m), grundy_value_eq_iff_equiv],
refine equiv.trans _ nim_add_nim_equiv,
convert add_congr (equiv_nim_grundy_value G) (equiv_nim_grundy_value H);
simp only [hG, hH]
end
end pgame
|
20f614f8b2e58ad07a83e01c6727e866f59bfbe4 | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/data/equiv/basic.lean | 0574fe2cf9a581ca5dcc1559e9bff6c6fa5035af | [
"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 | 35,822 | 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
In the standard library we cannot assume the univalence axiom.
We say two types are equivalent if they are isomorphic.
Two equivalent types have the same cardinality.
-/
import tactic.split_ifs logic.function logic.unique data.set.basic data.bool data.quot
open function
universes u v w
variables {α : Sort u} {β : Sort v} {γ : Sort w}
/-- `α ≃ β` is the type of functions from `α → β` with a two-sided inverse. -/
structure equiv (α : Sort*) (β : Sort*) :=
(to_fun : α → β)
(inv_fun : β → α)
(left_inv : left_inverse inv_fun to_fun)
(right_inv : right_inverse inv_fun to_fun)
namespace equiv
/-- `perm α` is the type of bijections from `α` to itself. -/
@[reducible] def perm (α : Sort*) := equiv α α
infix ` ≃ `:50 := equiv
instance : has_coe_to_fun (α ≃ β) :=
⟨_, to_fun⟩
@[simp] theorem coe_fn_mk (f : α → β) (g l r) : (equiv.mk f g l r : α → β) = f :=
rfl
theorem eq_of_to_fun_eq : ∀ {e₁ e₂ : equiv α β}, (e₁ : α → β) = e₂ → e₁ = e₂
| ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ h :=
have f₁ = f₂, from h,
have g₁ = g₂, from funext $ assume x,
have f₁ (g₁ x) = f₂ (g₂ x), from (r₁ x).trans (r₂ x).symm,
have f₁ (g₁ x) = f₁ (g₂ x), by subst f₂; exact this,
show g₁ x = g₂ x, from injective_of_left_inverse l₁ this,
by simp *
@[extensionality] lemma ext (f g : equiv α β) (H : ∀ x, f x = g x) : f = g :=
eq_of_to_fun_eq (funext H)
@[extensionality] lemma perm.ext (σ τ : equiv.perm α) (H : ∀ x, σ x = τ x) : σ = τ :=
equiv.ext _ _ H
@[refl] protected def refl (α : Sort*) : α ≃ α := ⟨id, id, λ x, rfl, λ x, rfl⟩
@[symm] protected def symm (e : α ≃ β) : β ≃ α := ⟨e.inv_fun, e.to_fun, e.right_inv, e.left_inv⟩
@[trans] protected def trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ :=
⟨e₂.to_fun ∘ e₁.to_fun, e₁.inv_fun ∘ e₂.inv_fun,
e₂.left_inv.comp e₁.left_inv, e₂.right_inv.comp e₁.right_inv⟩
protected theorem injective : ∀ f : α ≃ β, injective f
| ⟨f, g, h₁, h₂⟩ := injective_of_left_inverse h₁
protected theorem surjective : ∀ f : α ≃ β, surjective f
| ⟨f, g, h₁, h₂⟩ := surjective_of_has_right_inverse ⟨_, h₂⟩
protected theorem bijective (f : α ≃ β) : bijective f :=
⟨f.injective, f.surjective⟩
protected theorem subsingleton (e : α ≃ β) : ∀ [subsingleton β], subsingleton α
| ⟨H⟩ := ⟨λ a b, e.injective (H _ _)⟩
protected def decidable_eq (e : α ≃ β) [H : decidable_eq β] : decidable_eq α
| a b := decidable_of_iff _ e.injective.eq_iff
protected def cast {α β : Sort*} (h : α = β) : α ≃ β :=
⟨cast h, cast h.symm, λ x, by cases h; refl, λ x, by cases h; refl⟩
@[simp] theorem coe_fn_symm_mk (f : α → β) (g l r) : ((equiv.mk f g l r).symm : β → α) = g :=
rfl
@[simp] theorem refl_apply (x : α) : equiv.refl α x = x := rfl
@[simp] theorem trans_apply (f : α ≃ β) (g : β ≃ γ) (a : α) : (f.trans g) a = g (f a) := rfl
@[simp] theorem apply_symm_apply : ∀ (e : α ≃ β) (x : β), e (e.symm x) = x
| ⟨f₁, g₁, l₁, r₁⟩ x := by simp [equiv.symm]; rw r₁
@[simp] theorem symm_apply_apply : ∀ (e : α ≃ β) (x : α), e.symm (e x) = x
| ⟨f₁, g₁, l₁, r₁⟩ x := by simp [equiv.symm]; rw l₁
@[simp] lemma symm_trans_apply (f : α ≃ β) (g : β ≃ γ) (a : γ) :
(f.trans g).symm a = f.symm (g.symm a) := rfl
@[simp] theorem apply_eq_iff_eq : ∀ (f : α ≃ β) (x y : α), f x = f y ↔ x = y
| ⟨f₁, g₁, l₁, r₁⟩ x y := (injective_of_left_inverse l₁).eq_iff
@[simp] theorem cast_apply {α β} (h : α = β) (x : α) : equiv.cast h x = cast h x := rfl
lemma symm_apply_eq {α β} (e : α ≃ β) {x y} : e.symm x = y ↔ x = e y :=
⟨λ H, by simp [H.symm], λ H, by simp [H]⟩
lemma eq_symm_apply {α β} (e : α ≃ β) {x y} : y = e.symm x ↔ e y = x :=
(eq_comm.trans e.symm_apply_eq).trans eq_comm
@[simp] theorem symm_symm (e : α ≃ β) : e.symm.symm = e := by cases e; refl
@[simp] theorem symm_symm_apply (e : α ≃ β) (a : α) : e.symm.symm a = e a := by cases e; refl
@[simp] theorem trans_refl (e : α ≃ β) : e.trans (equiv.refl β) = e := by cases e; refl
@[simp] theorem refl_trans (e : α ≃ β) : (equiv.refl α).trans e = e := by cases e; refl
@[simp] theorem symm_trans (e : α ≃ β) : e.symm.trans e = equiv.refl β := ext _ _ (by simp)
@[simp] theorem trans_symm (e : α ≃ β) : e.trans e.symm = equiv.refl α := ext _ _ (by simp)
lemma trans_assoc {δ} (ab : α ≃ β) (bc : β ≃ γ) (cd : γ ≃ δ) :
(ab.trans bc).trans cd = ab.trans (bc.trans cd) :=
equiv.ext _ _ $ assume a, rfl
theorem left_inverse_symm (f : equiv α β) : left_inverse f.symm f := f.left_inv
theorem right_inverse_symm (f : equiv α β) : function.right_inverse f.symm f := f.right_inv
def equiv_congr {δ} (ab : α ≃ β) (cd : γ ≃ δ) : (α ≃ γ) ≃ (β ≃ δ) :=
⟨ λac, (ab.symm.trans ac).trans cd, λbd, ab.trans $ bd.trans $ cd.symm,
assume ac, begin simp [trans_assoc], rw [← trans_assoc], simp end,
assume ac, begin simp [trans_assoc], rw [← trans_assoc], simp end, ⟩
def perm_congr {α : Type*} {β : Type*} (e : α ≃ β) : perm α ≃ perm β :=
equiv_congr e e
protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s :=
set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv
protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) : t ⊆ e '' s ↔ e.symm '' t ⊆ s :=
by rw [set.image_subset_iff, e.image_eq_preimage]
lemma symm_image_image {α β} (f : equiv α β) (s : set α) : f.symm '' (f '' s) = s :=
by { rw [← set.image_comp], simp }
protected lemma image_compl {α β} (f : equiv α β) (s : set α) :
f '' -s = -(f '' s) :=
set.image_compl_eq f.bijective
/- The group of permutations (self-equivalences) of a type `α` -/
namespace perm
instance perm_group {α : Type u} : group (perm α) :=
begin
refine { mul := λ f g, equiv.trans g f, one := equiv.refl α, inv:= equiv.symm, ..};
intros; apply equiv.ext; try { apply trans_apply },
apply symm_apply_apply
end
@[simp] theorem mul_apply {α : Type u} (f g : perm α) (x) : (f * g) x = f (g x) :=
equiv.trans_apply _ _ _
@[simp] theorem one_apply {α : Type u} (x) : (1 : perm α) x = x := rfl
@[simp] lemma inv_apply_self {α : Type u} (f : perm α) (x) :
f⁻¹ (f x) = x := equiv.symm_apply_apply _ _
@[simp] lemma apply_inv_self {α : Type u} (f : perm α) (x) :
f (f⁻¹ x) = x := equiv.apply_symm_apply _ _
lemma one_def {α : Type u} : (1 : perm α) = equiv.refl α := rfl
lemma mul_def {α : Type u} (f g : perm α) : f * g = g.trans f := rfl
lemma inv_def {α : Type u} (f : perm α) : f⁻¹ = f.symm := rfl
end perm
def equiv_empty (h : α → false) : α ≃ empty :=
⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩
def false_equiv_empty : false ≃ empty :=
equiv_empty _root_.id
def equiv_pempty (h : α → false) : α ≃ pempty :=
⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩
def false_equiv_pempty : false ≃ pempty :=
equiv_pempty _root_.id
def empty_equiv_pempty : empty ≃ pempty :=
equiv_pempty $ empty.rec _
def pempty_equiv_pempty : pempty.{v} ≃ pempty.{w} :=
equiv_pempty pempty.elim
def empty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ empty :=
equiv_empty $ assume a, h ⟨a⟩
def pempty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ pempty :=
equiv_pempty $ assume a, h ⟨a⟩
def prop_equiv_punit {p : Prop} (h : p) : p ≃ punit :=
⟨λ x, (), λ x, h, λ _, rfl, λ ⟨⟩, rfl⟩
def true_equiv_punit : true ≃ punit := prop_equiv_punit trivial
protected def ulift {α : Type u} : ulift α ≃ α :=
⟨ulift.down, ulift.up, ulift.up_down, λ a, rfl⟩
protected def plift : plift α ≃ α :=
⟨plift.down, plift.up, plift.up_down, plift.down_up⟩
@[congr] def arrow_congr {α₁ β₁ α₂ β₂ : Sort*} : α₁ ≃ α₂ → β₁ ≃ β₂ → (α₁ → β₁) ≃ (α₂ → β₂)
| ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ :=
⟨λ (h : α₁ → β₁) (a : α₂), f₂ (h (g₁ a)),
λ (h : α₂ → β₂) (a : α₁), g₂ (h (f₁ a)),
λ h, by funext a; dsimp; rw [l₁, l₂],
λ h, by funext a; dsimp; rw [r₁, r₂]⟩
def punit_equiv_punit : punit.{v} ≃ punit.{w} :=
⟨λ _, punit.star, λ _, punit.star, λ u, by cases u; refl, λ u, by cases u; reflexivity⟩
section
@[simp] def arrow_punit_equiv_punit (α : Sort*) : (α → punit.{v}) ≃ punit.{w} :=
⟨λ f, punit.star, λ u f, punit.star, λ f, by funext x; cases f x; refl, λ u, by cases u; reflexivity⟩
@[simp] def punit_arrow_equiv (α : Sort*) : (punit.{u} → α) ≃ α :=
⟨λ f, f punit.star, λ a u, a, λ f, by funext x; cases x; refl, λ u, rfl⟩
@[simp] def empty_arrow_equiv_punit (α : Sort*) : (empty → α) ≃ punit.{u} :=
⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by cases u; refl⟩
@[simp] def pempty_arrow_equiv_punit (α : Sort*) : (pempty → α) ≃ punit.{u} :=
⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by cases u; refl⟩
@[simp] def false_arrow_equiv_punit (α : Sort*) : (false → α) ≃ punit.{u} :=
calc (false → α) ≃ (empty → α) : arrow_congr false_equiv_empty (equiv.refl _)
... ≃ punit : empty_arrow_equiv_punit _
end
@[congr] def prod_congr {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ :β₁ ≃ β₂) : (α₁ × β₁) ≃ (α₂ × β₂) :=
⟨λp, (e₁ p.1, e₂ p.2), λp, (e₁.symm p.1, e₂.symm p.2),
λ ⟨a, b⟩, show (e₁.symm (e₁ a), e₂.symm (e₂ b)) = (a, b), by rw [symm_apply_apply, symm_apply_apply],
λ ⟨a, b⟩, show (e₁ (e₁.symm a), e₂ (e₂.symm b)) = (a, b), by rw [apply_symm_apply, apply_symm_apply]⟩
@[simp] theorem prod_congr_apply {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (a : α₁) (b : β₁) :
prod_congr e₁ e₂ (a, b) = (e₁ a, e₂ b) :=
rfl
@[simp] def prod_comm (α β : Sort*) : (α × β) ≃ (β × α) :=
⟨λ p, (p.2, p.1), λ p, (p.2, p.1), λ⟨a, b⟩, rfl, λ⟨a, b⟩, rfl⟩
@[simp] def prod_assoc (α β γ : Sort*) : ((α × β) × γ) ≃ (α × (β × γ)) :=
⟨λ p, ⟨p.1.1, ⟨p.1.2, p.2⟩⟩, λp, ⟨⟨p.1, p.2.1⟩, p.2.2⟩, λ ⟨⟨a, b⟩, c⟩, rfl, λ ⟨a, ⟨b, c⟩⟩, rfl⟩
@[simp] theorem prod_assoc_apply {α β γ : Sort*} (p : (α × β) × γ) :
prod_assoc α β γ p = ⟨p.1.1, ⟨p.1.2, p.2⟩⟩ := rfl
section
@[simp] def prod_punit (α : Sort*) : (α × punit.{u+1}) ≃ α :=
⟨λ p, p.1, λ a, (a, punit.star), λ ⟨_, punit.star⟩, rfl, λ a, rfl⟩
@[simp] theorem prod_punit_apply {α : Sort*} (a : α × punit.{u+1}) : prod_punit α a = a.1 := rfl
@[simp] def punit_prod (α : Sort*) : (punit.{u+1} × α) ≃ α :=
calc (punit × α) ≃ (α × punit) : prod_comm _ _
... ≃ α : prod_punit _
@[simp] theorem punit_prod_apply {α : Sort*} (a : punit.{u+1} × α) : punit_prod α a = a.2 := rfl
@[simp] def prod_empty (α : Sort*) : (α × empty) ≃ empty :=
equiv_empty (λ ⟨_, e⟩, e.rec _)
@[simp] def empty_prod (α : Sort*) : (empty × α) ≃ empty :=
equiv_empty (λ ⟨e, _⟩, e.rec _)
@[simp] def prod_pempty (α : Sort*) : (α × pempty) ≃ pempty :=
equiv_pempty (λ ⟨_, e⟩, e.rec _)
@[simp] def pempty_prod (α : Sort*) : (pempty × α) ≃ pempty :=
equiv_pempty (λ ⟨e, _⟩, e.rec _)
end
section
open sum
def psum_equiv_sum (α β : Sort*) : psum α β ≃ (α ⊕ β) :=
⟨λ s, psum.cases_on s inl inr,
λ s, sum.cases_on s psum.inl psum.inr,
λ s, by cases s; refl,
λ s, by cases s; refl⟩
def sum_congr {α₁ β₁ α₂ β₂ : Sort*} : α₁ ≃ α₂ → β₁ ≃ β₂ → (α₁ ⊕ β₁) ≃ (α₂ ⊕ β₂)
| ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ :=
⟨λ s, match s with inl a₁ := inl (f₁ a₁) | inr b₁ := inr (f₂ b₁) end,
λ s, match s with inl a₂ := inl (g₁ a₂) | inr b₂ := inr (g₂ b₂) end,
λ s, match s with inl a := congr_arg inl (l₁ a) | inr a := congr_arg inr (l₂ a) end,
λ s, match s with inl a := congr_arg inl (r₁ a) | inr a := congr_arg inr (r₂ a) end⟩
@[simp] theorem sum_congr_apply_inl {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (a : α₁) :
sum_congr e₁ e₂ (inl a) = inl (e₁ a) :=
by cases e₁; cases e₂; refl
@[simp] theorem sum_congr_apply_inr {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (b : β₁) :
sum_congr e₁ e₂ (inr b) = inr (e₂ b) :=
by cases e₁; cases e₂; refl
def bool_equiv_punit_sum_punit : bool ≃ (punit.{u+1} ⊕ punit.{v+1}) :=
⟨λ b, cond b (inr punit.star) (inl punit.star),
λ s, sum.rec_on s (λ_, ff) (λ_, tt),
λ b, by cases b; refl,
λ s, by rcases s with ⟨⟨⟩⟩ | ⟨⟨⟩⟩; refl⟩
noncomputable def Prop_equiv_bool : Prop ≃ bool :=
⟨λ p, @to_bool p (classical.prop_decidable _),
λ b, b, λ p, by simp, λ b, by simp⟩
@[simp] def sum_comm (α β : Sort*) : (α ⊕ β) ≃ (β ⊕ α) :=
⟨λ s, match s with inl a := inr a | inr b := inl b end,
λ s, match s with inl b := inr b | inr a := inl a end,
λ s, by cases s; refl,
λ s, by cases s; refl⟩
@[simp] def sum_assoc (α β γ : Sort*) : ((α ⊕ β) ⊕ γ) ≃ (α ⊕ (β ⊕ γ)) :=
⟨λ s, match s with inl (inl a) := inl a | inl (inr b) := inr (inl b) | inr c := inr (inr c) end,
λ s, match s with inl a := inl (inl a) | inr (inl b) := inl (inr b) | inr (inr c) := inr c end,
λ s, by rcases s with ⟨_ | _⟩ | _; refl,
λ s, by rcases s with _ | _ | _; refl⟩
@[simp] theorem sum_assoc_apply_in1 {α β γ} (a) : sum_assoc α β γ (inl (inl a)) = inl a := rfl
@[simp] theorem sum_assoc_apply_in2 {α β γ} (b) : sum_assoc α β γ (inl (inr b)) = inr (inl b) := rfl
@[simp] theorem sum_assoc_apply_in3 {α β γ} (c) : sum_assoc α β γ (inr c) = inr (inr c) := rfl
@[simp] def sum_empty (α : Sort*) : (α ⊕ empty) ≃ α :=
⟨λ s, match s with inl a := a | inr e := empty.rec _ e end,
inl,
λ s, by rcases s with _ | ⟨⟨⟩⟩; refl,
λ a, rfl⟩
@[simp] def empty_sum (α : Sort*) : (empty ⊕ α) ≃ α :=
(sum_comm _ _).trans $ sum_empty _
@[simp] def sum_pempty (α : Sort*) : (α ⊕ pempty) ≃ α :=
⟨λ s, match s with inl a := a | inr e := pempty.rec _ e end,
inl,
λ s, by rcases s with _ | ⟨⟨⟩⟩; refl,
λ a, rfl⟩
@[simp] def pempty_sum (α : Sort*) : (pempty ⊕ α) ≃ α :=
(sum_comm _ _).trans $ sum_pempty _
@[simp] def option_equiv_sum_punit (α : Sort*) : option α ≃ (α ⊕ punit.{u+1}) :=
⟨λ o, match o with none := inr punit.star | some a := inl a end,
λ s, match s with inr _ := none | inl a := some a end,
λ o, by cases o; refl,
λ s, by rcases s with _ | ⟨⟨⟩⟩; refl⟩
def sum_equiv_sigma_bool (α β : Sort*) : (α ⊕ β) ≃ (Σ b: bool, cond b α β) :=
⟨λ s, match s with inl a := ⟨tt, a⟩ | inr b := ⟨ff, b⟩ end,
λ s, match s with ⟨tt, a⟩ := inl a | ⟨ff, b⟩ := inr b end,
λ s, by cases s; refl,
λ s, by rcases s with ⟨_|_, _⟩; refl⟩
def equiv_fib {α β : Type*} (f : α → β) :
α ≃ Σ y : β, {x // f x = y} :=
⟨λ x, ⟨f x, x, rfl⟩, λ x, x.2.1, λ x, rfl, λ ⟨y, x, rfl⟩, rfl⟩
end
section
def Pi_congr_right {α} {β₁ β₂ : α → Sort*} (F : ∀ a, β₁ a ≃ β₂ a) : (Π a, β₁ a) ≃ (Π a, β₂ a) :=
⟨λ H a, F a (H a), λ H a, (F a).symm (H a),
λ H, funext $ by simp, λ H, funext $ by simp⟩
end
section
def psigma_equiv_sigma {α} (β : α → Sort*) : psigma β ≃ sigma β :=
⟨λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩
def sigma_congr_right {α} {β₁ β₂ : α → Sort*} (F : ∀ a, β₁ a ≃ β₂ a) : sigma β₁ ≃ sigma β₂ :=
⟨λ ⟨a, b⟩, ⟨a, F a b⟩, λ ⟨a, b⟩, ⟨a, (F a).symm b⟩,
λ ⟨a, b⟩, congr_arg (sigma.mk a) $ symm_apply_apply (F a) b,
λ ⟨a, b⟩, congr_arg (sigma.mk a) $ apply_symm_apply (F a) b⟩
def sigma_congr_left {α₁ α₂} {β : α₂ → Sort*} : ∀ f : α₁ ≃ α₂, (Σ a:α₁, β (f a)) ≃ (Σ a:α₂, β a)
| ⟨f, g, l, r⟩ :=
⟨λ ⟨a, b⟩, ⟨f a, b⟩, λ ⟨a, b⟩, ⟨g a, @@eq.rec β b (r a).symm⟩,
λ ⟨a, b⟩, match g (f a), l a : ∀ a' (h : a' = a),
@sigma.mk _ (β ∘ f) _ (@@eq.rec β b (congr_arg f h.symm)) = ⟨a, b⟩ with
| _, rfl := rfl end,
λ ⟨a, b⟩, match f (g a), _ : ∀ a' (h : a' = a), sigma.mk a' (@@eq.rec β b h.symm) = ⟨a, b⟩ with
| _, rfl := rfl end⟩
def sigma_equiv_prod (α β : Sort*) : (Σ_:α, β) ≃ (α × β) :=
⟨λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩
def sigma_equiv_prod_of_equiv {α β} {β₁ : α → Sort*} (F : ∀ a, β₁ a ≃ β) : sigma β₁ ≃ (α × β) :=
(sigma_congr_right F).trans (sigma_equiv_prod α β)
end
section
def arrow_prod_equiv_prod_arrow (α β γ : Type*) : (γ → α × β) ≃ ((γ → α) × (γ → β)) :=
⟨λ f, (λ c, (f c).1, λ c, (f c).2),
λ p c, (p.1 c, p.2 c),
λ f, funext $ λ c, prod.mk.eta,
λ p, by cases p; refl⟩
def arrow_arrow_equiv_prod_arrow (α β γ : Sort*) : (α → β → γ) ≃ (α × β → γ) :=
⟨λ f, λ p, f p.1 p.2,
λ f, λ a b, f (a, b),
λ f, rfl,
λ f, by funext p; cases p; refl⟩
open sum
def sum_arrow_equiv_prod_arrow (α β γ : Type*) : ((α ⊕ β) → γ) ≃ ((α → γ) × (β → γ)) :=
⟨λ f, (f ∘ inl, f ∘ inr),
λ p s, sum.rec_on s p.1 p.2,
λ f, by funext s; cases s; refl,
λ p, by cases p; refl⟩
def sum_prod_distrib (α β γ : Sort*) : ((α ⊕ β) × γ) ≃ ((α × γ) ⊕ (β × γ)) :=
⟨λ p, match p with (inl a, c) := inl (a, c) | (inr b, c) := inr (b, c) end,
λ s, match s with inl (a, c) := (inl a, c) | inr (b, c) := (inr b, c) end,
λ p, by rcases p with ⟨_ | _, _⟩; refl,
λ s, by rcases s with ⟨_, _⟩ | ⟨_, _⟩; refl⟩
@[simp] theorem sum_prod_distrib_apply_left {α β γ} (a : α) (c : γ) :
sum_prod_distrib α β γ (sum.inl a, c) = sum.inl (a, c) := rfl
@[simp] theorem sum_prod_distrib_apply_right {α β γ} (b : β) (c : γ) :
sum_prod_distrib α β γ (sum.inr b, c) = sum.inr (b, c) := rfl
def prod_sum_distrib (α β γ : Sort*) : (α × (β ⊕ γ)) ≃ ((α × β) ⊕ (α × γ)) :=
calc (α × (β ⊕ γ)) ≃ ((β ⊕ γ) × α) : prod_comm _ _
... ≃ ((β × α) ⊕ (γ × α)) : sum_prod_distrib _ _ _
... ≃ ((α × β) ⊕ (α × γ)) : sum_congr (prod_comm _ _) (prod_comm _ _)
@[simp] theorem prod_sum_distrib_apply_left {α β γ} (a : α) (b : β) :
prod_sum_distrib α β γ (a, sum.inl b) = sum.inl (a, b) := rfl
@[simp] theorem prod_sum_distrib_apply_right {α β γ} (a : α) (c : γ) :
prod_sum_distrib α β γ (a, sum.inr c) = sum.inr (a, c) := rfl
def bool_prod_equiv_sum (α : Type u) : (bool × α) ≃ (α ⊕ α) :=
calc (bool × α) ≃ ((unit ⊕ unit) × α) : prod_congr bool_equiv_punit_sum_punit (equiv.refl _)
... ≃ (α × (unit ⊕ unit)) : prod_comm _ _
... ≃ ((α × unit) ⊕ (α × unit)) : prod_sum_distrib _ _ _
... ≃ (α ⊕ α) : sum_congr (prod_punit _) (prod_punit _)
end
section
open sum nat
def nat_equiv_nat_sum_punit : ℕ ≃ (ℕ ⊕ punit.{u+1}) :=
⟨λ n, match n with zero := inr punit.star | succ a := inl a end,
λ s, match s with inl n := succ n | inr punit.star := zero end,
λ n, begin cases n, repeat { refl } end,
λ s, begin cases s with a u, { refl }, {cases u, { refl }} end⟩
@[simp] def nat_sum_punit_equiv_nat : (ℕ ⊕ punit.{u+1}) ≃ ℕ :=
nat_equiv_nat_sum_punit.symm
def int_equiv_nat_sum_nat : ℤ ≃ (ℕ ⊕ ℕ) :=
by refine ⟨_, _, _, _⟩; intro z; {cases z; [left, right]; assumption} <|> {cases z; refl}
end
def list_equiv_of_equiv {α β : Type*} : α ≃ β → list α ≃ list β
| ⟨f, g, l, r⟩ :=
by refine ⟨list.map f, list.map g, λ x, _, λ x, _⟩;
simp [id_of_left_inverse l, id_of_right_inverse r]
def fin_equiv_subtype (n : ℕ) : fin n ≃ {m // m < n} :=
⟨λ x, ⟨x.1, x.2⟩, λ x, ⟨x.1, x.2⟩, λ ⟨a, b⟩, rfl,λ ⟨a, b⟩, rfl⟩
def decidable_eq_of_equiv [decidable_eq β] (e : α ≃ β) : decidable_eq α
| a₁ a₂ := decidable_of_iff (e a₁ = e a₂) e.injective.eq_iff
def inhabited_of_equiv [inhabited β] (e : α ≃ β) : inhabited α :=
⟨e.symm (default _)⟩
def unique_of_equiv (e : α ≃ β) (h : unique β) : unique α :=
unique.of_surjective e.symm.surjective
def unique_congr (e : α ≃ β) : unique α ≃ unique β :=
{ to_fun := e.symm.unique_of_equiv,
inv_fun := e.unique_of_equiv,
left_inv := λ _, subsingleton.elim _ _,
right_inv := λ _, subsingleton.elim _ _ }
section
open subtype
def subtype_congr {p : α → Prop} {q : β → Prop}
(e : α ≃ β) (h : ∀ a, p a ↔ q (e a)) : {a : α // p a} ≃ {b : β // q b} :=
⟨λ x, ⟨e x.1, (h _).1 x.2⟩,
λ y, ⟨e.symm y.1, (h _).2 (by simp; exact y.2)⟩,
λ ⟨x, h⟩, subtype.eq' $ by simp,
λ ⟨y, h⟩, subtype.eq' $ by simp⟩
def subtype_congr_right {p q : α → Prop} (e : ∀x, p x ↔ q x) : subtype p ≃ subtype q :=
subtype_congr (equiv.refl _) e
@[simp] lemma subtype_congr_right_mk {p q : α → Prop} (e : ∀x, p x ↔ q x)
{x : α} (h : p x) : subtype_congr_right e ⟨x, h⟩ = ⟨x, (e x).1 h⟩ := rfl
def subtype_equiv_of_subtype' {p : α → Prop} (e : α ≃ β) :
{a : α // p a} ≃ {b : β // p (e.symm b)} :=
subtype_congr e $ by simp
def subtype_congr_prop {α : Type*} {p q : α → Prop} (h : p = q) : subtype p ≃ subtype q :=
subtype_congr (equiv.refl α) (assume a, h ▸ iff.refl _)
def set_congr {α : Type*} {s t : set α} (h : s = t) : s ≃ t :=
subtype_congr_prop h
def subtype_subtype_equiv_subtype {α : Type u} (p : α → Prop) (q : subtype p → Prop) :
subtype q ≃ {a : α // ∃h:p a, q ⟨a, h⟩ } :=
⟨λ⟨⟨a, ha⟩, ha'⟩, ⟨a, ha, ha'⟩,
λ⟨a, ha⟩, ⟨⟨a, ha.cases_on $ assume h _, h⟩, by cases ha; exact ha_h⟩,
assume ⟨⟨a, ha⟩, h⟩, rfl, assume ⟨a, h₁, h₂⟩, rfl⟩
/-- aka coimage -/
def equiv_sigma_subtype {α : Type u} {β : Type v} (f : α → β) : α ≃ Σ b, {x : α // f x = b} :=
⟨λ x, ⟨f x, x, rfl⟩, λ x, x.2.1, λ x, rfl, λ ⟨b, x, H⟩, sigma.eq H $ eq.drec_on H $ subtype.eq rfl⟩
def pi_equiv_subtype_sigma (ι : Type*) (π : ι → Type*) :
(Πi, π i) ≃ {f : ι → Σi, π i | ∀i, (f i).1 = i } :=
⟨ λf, ⟨λi, ⟨i, f i⟩, assume i, rfl⟩, λf i, begin rw ← f.2 i, exact (f.1 i).2 end,
assume f, funext $ assume i, rfl,
assume ⟨f, hf⟩, subtype.eq $ funext $ assume i, sigma.eq (hf i).symm $
eq_of_heq $ rec_heq_of_heq _ $ rec_heq_of_heq _ $ heq.refl _⟩
def subtype_pi_equiv_pi {α : Sort u} {β : α → Sort v} {p : Πa, β a → Prop} :
{f : Πa, β a // ∀a, p a (f a) } ≃ Πa, { b : β a // p a b } :=
⟨λf a, ⟨f.1 a, f.2 a⟩, λf, ⟨λa, (f a).1, λa, (f a).2⟩,
by rintro ⟨f, h⟩; refl,
by rintro f; funext a; exact subtype.eq' rfl⟩
end
section
local attribute [elab_with_expected_type] quot.lift
def quot_equiv_of_quot' {r : α → α → Prop} {s : β → β → Prop} (e : α ≃ β)
(h : ∀ a a', r a a' ↔ s (e a) (e a')) : quot r ≃ quot s :=
⟨quot.lift (λ a, quot.mk _ (e a)) (λ a a' H, quot.sound ((h a a').mp H)),
quot.lift (λ b, quot.mk _ (e.symm b)) (λ b b' H, quot.sound ((h _ _).mpr (by convert H; simp))),
quot.ind $ by simp,
quot.ind $ by simp⟩
def quot_equiv_of_quot {r : α → α → Prop} (e : α ≃ β) :
quot r ≃ quot (λ b b', r (e.symm b) (e.symm b')) :=
quot_equiv_of_quot' e (by simp)
end
namespace set
open set
protected def univ (α) : @univ α ≃ α :=
⟨subtype.val, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩
@[simp] lemma univ_apply {α : Type u} (x : @univ α) :
equiv.set.univ α x = x := rfl
@[simp] lemma univ_symm_apply {α : Type u} (x : α) :
(equiv.set.univ α).symm x = ⟨x, trivial⟩ := rfl
protected def empty (α) : (∅ : set α) ≃ empty :=
equiv_empty $ λ ⟨x, h⟩, not_mem_empty x h
protected def pempty (α) : (∅ : set α) ≃ pempty :=
equiv_pempty $ λ ⟨x, h⟩, not_mem_empty x h
protected def union' {α} {s t : set α}
(p : α → Prop) [decidable_pred p]
(hs : ∀ x ∈ s, p x)
(ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ (s ⊕ t) :=
{ to_fun := λ x, if hp : p x.1
then sum.inl ⟨_, x.2.resolve_right (λ xt, ht _ xt hp)⟩
else sum.inr ⟨_, x.2.resolve_left (λ xs, hp (hs _ xs))⟩,
inv_fun := λ o, match o with
| (sum.inl x) := ⟨x.1, or.inl x.2⟩
| (sum.inr x) := ⟨x.1, or.inr x.2⟩
end,
left_inv := λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr,
right_inv := λ o, begin
rcases o with ⟨x, h⟩ | ⟨x, h⟩;
dsimp [union'._match_1];
[simp [hs _ h], simp [ht _ h]]
end }
protected def union {α} {s t : set α} [decidable_pred s] (H : s ∩ t = ∅) :
(s ∪ t : set α) ≃ (s ⊕ t) :=
set.union' s (λ _, id) (λ x xt xs, subset_empty_iff.2 H ⟨xs, xt⟩)
lemma union_apply_left {α} {s t : set α} [decidable_pred s] (H : s ∩ t = ∅)
{a : (s ∪ t : set α)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ :=
dif_pos ha
lemma union_apply_right {α} {s t : set α} [decidable_pred s] (H : s ∩ t = ∅)
{a : (s ∪ t : set α)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ :=
dif_neg (show ↑a ∉ s, by finish [set.ext_iff])
protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} :=
⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩,
λ ⟨x, h⟩, by simp at h; subst x,
λ ⟨⟩, rfl⟩
protected def of_eq {α : Type u} {s t : set α} (h : s = t) : s ≃ t :=
{ to_fun := λ x, ⟨x.1, h ▸ x.2⟩,
inv_fun := λ x, ⟨x.1, h.symm ▸ x.2⟩,
left_inv := λ _, subtype.eq rfl,
right_inv := λ _, subtype.eq rfl }
@[simp] lemma of_eq_apply {α : Type u} {s t : set α} (h : s = t) (a : s) :
equiv.set.of_eq h a = ⟨a, h ▸ a.2⟩ := rfl
@[simp] lemma of_eq_symm_apply {α : Type u} {s t : set α} (h : s = t) (a : t) :
(equiv.set.of_eq h).symm a = ⟨a, h.symm ▸ a.2⟩ := rfl
protected def insert {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) :
(insert a s : set α) ≃ (s ⊕ punit.{u+1}) :=
calc (insert a s : set α) ≃ ↥(s ∪ {a}) : equiv.set.of_eq (by simp)
... ≃ (s ⊕ ({a} : set α)) : equiv.set.union (by finish [set.ext_iff])
... ≃ (s ⊕ punit.{u+1}) : sum_congr (equiv.refl _) (equiv.set.singleton _)
protected def sum_compl {α} (s : set α) [decidable_pred s] :
(s ⊕ (-s : set α)) ≃ α :=
calc (s ⊕ (-s : set α)) ≃ ↥(s ∪ -s) : (equiv.set.union (by simp [set.ext_iff])).symm
... ≃ @univ α : equiv.set.of_eq (by simp)
... ≃ α : equiv.set.univ _
@[simp] lemma sum_compl_apply_inl {α : Type u} (s : set α) [decidable_pred s] (x : s) :
equiv.set.sum_compl s (sum.inl x) = x := rfl
@[simp] lemma sum_compl_apply_inr {α : Type u} (s : set α) [decidable_pred s] (x : -s) :
equiv.set.sum_compl s (sum.inr x) = x := rfl
lemma sum_compl_symm_apply_of_mem {α : Type u} {s : set α} [decidable_pred s] {x : α}
(hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ :=
have ↑(⟨x, or.inl hx⟩ : (s ∪ -s : set α)) ∈ s, from hx,
by rw [equiv.set.sum_compl]; simpa using set.union_apply_left _ this
lemma sum_compl_symm_apply_of_not_mem {α : Type u} {s : set α} [decidable_pred s] {x : α}
(hx : x ∉ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ :=
have ↑(⟨x, or.inr hx⟩ : (s ∪ -s : set α)) ∈ -s, from hx,
by rw [equiv.set.sum_compl]; simpa using set.union_apply_right _ this
protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred s] :
((s ∪ t : set α) ⊕ (s ∩ t : set α)) ≃ (s ⊕ t) :=
calc ((s ∪ t : set α) ⊕ (s ∩ t : set α))
≃ ((s ∪ t \ s : set α) ⊕ (s ∩ t : set α)) : by rw [union_diff_self]
... ≃ ((s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α)) :
sum_congr (set.union (inter_diff_self _ _)) (equiv.refl _)
... ≃ (s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α)) : sum_assoc _ _ _
... ≃ (s ⊕ (t \ s ∪ s ∩ t : set α)) : sum_congr (equiv.refl _) begin
refine (set.union' (∉ s) _ _).symm,
exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1]
end
... ≃ (s ⊕ t) : by rw (_ : t \ s ∪ s ∩ t = t);
rw [union_comm, inter_comm, inter_union_diff]
protected def prod {α β} (s : set α) (t : set β) :
(s.prod t) ≃ (s × t) :=
⟨λp, ⟨⟨p.1.1, p.2.1⟩, ⟨p.1.2, p.2.2⟩⟩,
λp, ⟨⟨p.1.1, p.2.1⟩, ⟨p.1.2, p.2.2⟩⟩,
λ ⟨⟨x, y⟩, ⟨h₁, h₂⟩⟩, rfl,
λ ⟨⟨x, h₁⟩, ⟨y, h₂⟩⟩, rfl⟩
protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) :
s ≃ (f '' s) :=
⟨λ ⟨x, h⟩, ⟨f x, mem_image_of_mem _ h⟩,
λ ⟨y, h⟩, ⟨classical.some h, (classical.some_spec h).1⟩,
λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).2),
λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩
@[simp] theorem image_apply {α β} (f : α → β) (s : set α) (H : injective f) (a h) :
set.image f s H ⟨a, h⟩ = ⟨f a, mem_image_of_mem _ h⟩ := rfl
protected noncomputable def range {α β} (f : α → β) (H : injective f) :
α ≃ range f :=
{ to_fun := λ x, ⟨f x, mem_range_self _⟩,
inv_fun := λ x, classical.some x.2,
left_inv := λ x, H (classical.some_spec (show f x ∈ range f, from mem_range_self _)),
right_inv := λ x, subtype.eq $ classical.some_spec x.2 }
@[simp] theorem range_apply {α β} (f : α → β) (H : injective f) (a) :
set.range f H a = ⟨f a, set.mem_range_self _⟩ := rfl
protected def congr {α β : Type*} (e : α ≃ β) : set α ≃ set β :=
⟨λ s, e '' s, λ t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩
end set
noncomputable def of_bijective {α β} {f : α → β} (hf : bijective f) : α ≃ β :=
⟨f, λ x, classical.some (hf.2 x), λ x, hf.1 (classical.some_spec (hf.2 (f x))),
λ x, classical.some_spec (hf.2 x)⟩
@[simp] theorem of_bijective_to_fun {α β} {f : α → β} (hf : bijective f) : (of_bijective hf : α → β) = f := rfl
lemma subtype_quotient_equiv_quotient_subtype (p₁ : α → Prop) [s₁ : setoid α]
[s₂ : setoid (subtype p₁)] (p₂ : quotient s₁ → Prop) (hp₂ : ∀ a, p₁ a ↔ p₂ ⟦a⟧)
(h : ∀ x y : subtype p₁, @setoid.r _ s₂ x y ↔ (x : α) ≈ y) :
{x // p₂ x} ≃ quotient s₂ :=
{ to_fun := λ a, quotient.hrec_on a.1 (λ a h, ⟦⟨a, (hp₂ _).2 h⟩⟧)
(λ a b hab, hfunext (by rw quotient.sound hab)
(λ h₁ h₂ _, heq_of_eq (quotient.sound ((h _ _).2 hab)))) a.2,
inv_fun := λ a, quotient.lift_on a (λ a, (⟨⟦a.1⟧, (hp₂ _).1 a.2⟩ : {x // p₂ x}))
(λ a b hab, subtype.eq' (quotient.sound ((h _ _).1 hab))),
left_inv := λ ⟨a, ha⟩, quotient.induction_on a (λ a ha, rfl) ha,
right_inv := λ a, quotient.induction_on a (λ ⟨a, ha⟩, rfl) }
section swap
variable [decidable_eq α]
open decidable
def swap_core (a b r : α) : α :=
if r = a then b
else if r = b then a
else r
theorem swap_core_self (r a : α) : swap_core a a r = r :=
by unfold swap_core; split_ifs; cc
theorem swap_core_swap_core (r a b : α) : swap_core a b (swap_core a b r) = r :=
by unfold swap_core; split_ifs; cc
theorem swap_core_comm (r a b : α) : swap_core a b r = swap_core b a r :=
by unfold swap_core; split_ifs; cc
/-- `swap a b` is the permutation that swaps `a` and `b` and
leaves other values as is. -/
def swap (a b : α) : perm α :=
⟨swap_core a b, swap_core a b, λr, swap_core_swap_core r a b, λr, swap_core_swap_core r a b⟩
theorem swap_self (a : α) : swap a a = equiv.refl _ :=
eq_of_to_fun_eq $ funext $ λ r, swap_core_self r a
theorem swap_comm (a b : α) : swap a b = swap b a :=
eq_of_to_fun_eq $ funext $ λ r, swap_core_comm r _ _
theorem swap_apply_def (a b x : α) : swap a b x = if x = a then b else if x = b then a else x :=
rfl
@[simp] theorem swap_apply_left (a b : α) : swap a b a = b :=
if_pos rfl
@[simp] theorem swap_apply_right (a b : α) : swap a b b = a :=
by by_cases b = a; simp [swap_apply_def, *]
theorem swap_apply_of_ne_of_ne {a b x : α} : x ≠ a → x ≠ b → swap a b x = x :=
by simp [swap_apply_def] {contextual := tt}
@[simp] theorem swap_swap (a b : α) : (swap a b).trans (swap a b) = equiv.refl _ :=
eq_of_to_fun_eq $ funext $ λ x, swap_core_swap_core _ _ _
theorem swap_comp_apply {a b x : α} (π : perm α) :
π.trans (swap a b) x = if π x = a then b else if π x = b then a else π x :=
by cases π; refl
@[simp] lemma swap_inv {α : Type*} [decidable_eq α] (x y : α) :
(swap x y)⁻¹ = swap x y := rfl
@[simp] lemma symm_trans_swap_trans [decidable_eq α] [decidable_eq β] (a b : α)
(e : α ≃ β) : (e.symm.trans (swap a b)).trans e = swap (e a) (e b) :=
equiv.ext _ _ (λ x, begin
have : ∀ a, e.symm x = a ↔ x = e a :=
λ a, by rw @eq_comm _ (e.symm x); split; intros; simp * at *,
simp [swap_apply_def, this],
split_ifs; simp
end)
@[simp] lemma swap_mul_self {α : Type*} [decidable_eq α] (i j : α) : swap i j * swap i j = 1 :=
equiv.swap_swap i j
@[simp] lemma swap_apply_self {α : Type*} [decidable_eq α] (i j a : α) : swap i j (swap i j a) = a :=
by rw [← perm.mul_apply, swap_mul_self, perm.one_apply]
/-- Augment an equivalence with a prescribed mapping `f a = b` -/
def set_value (f : α ≃ β) (a : α) (b : β) : α ≃ β :=
(swap a (f.symm b)).trans f
@[simp] theorem set_value_eq (f : α ≃ β) (a : α) (b : β) : set_value f a b a = b :=
by dsimp [set_value]; simp [swap_apply_left]
end swap
end equiv
instance {α} [subsingleton α] : subsingleton (ulift α) := equiv.ulift.subsingleton
instance {α} [subsingleton α] : subsingleton (plift α) := equiv.plift.subsingleton
instance {α} [decidable_eq α] : decidable_eq (ulift α) := equiv.ulift.decidable_eq
instance {α} [decidable_eq α] : decidable_eq (plift α) := equiv.plift.decidable_eq
def unique_unique_equiv : unique (unique α) ≃ unique α :=
{ to_fun := λ h, h.default,
inv_fun := λ h, { default := h, uniq := λ _, subsingleton.elim _ _ },
left_inv := λ _, subsingleton.elim _ _,
right_inv := λ _, subsingleton.elim _ _ }
def equiv_of_unique_of_unique [unique α] [unique β] : α ≃ β :=
{ to_fun := λ _, default β,
inv_fun := λ _, default α,
left_inv := λ _, subsingleton.elim _ _,
right_inv := λ _, subsingleton.elim _ _ }
def equiv_punit_of_unique [unique α] : α ≃ punit.{v} :=
equiv_of_unique_of_unique
namespace quot
/-- Quotients are congruent on equivalences under equality of their relation.
An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/
protected def congr {α} {r r' : α → α → Prop} (eq : ∀a b, r a b ↔ r' a b) : quot r ≃ quot r' :=
⟨quot.map r r' (assume a b, (eq a b).1), quot.map r' r (assume a b, (eq a b).2),
by rintros ⟨a⟩; refl, by rintros ⟨a⟩; refl⟩
end quot
namespace quotient
protected def congr {α} {r r' : setoid α} (eq : ∀a b, @setoid.r α r a b ↔ @setoid.r α r' a b) :
quotient r ≃ quotient r' :=
quot.congr eq
end quotient
|
e919b46e90042cb7ad1ce9f831ed0a1720a11813 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/kha_inst_bug.lean | ccef183d4e19d10e417a8e0200acedb3ff8a8d64 | [
"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 | 194 | lean | open nat
example (a b : ℕ) : a - b = a - min a b :=
if h : a ≥ b then by rw [min_eq_right h]
else by rw [nat.sub_eq_zero_of_le (le_of_not_ge h), min_eq_left (le_of_not_ge h), nat.sub_self]
|
ca3c49bfdceedf856fdfa4f93ca739042729d0e0 | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /library/init/logic.lean | 556c9a111468c11fe1fe5a5395f64ae80756373a | [
"Apache-2.0"
] | permissive | tjiaqi/lean | 4634d729795c164664d10d093f3545287c76628f | d0ce4cf62f4246b0600c07e074d86e51f2195e30 | refs/heads/master | 1,622,323,796,480 | 1,422,643,069,000 | 1,422,643,069,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,627 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Module: init.logic
Authors: Leonardo de Moura, Jeremy Avigad, Floris van Doorn
-/
prelude
import init.datatypes init.reserved_notation
/- implication -/
definition trivial := true.intro
definition not (a : Prop) := a → false
prefix `¬` := not
definition absurd {a : Prop} {b : Type} (H1 : a) (H2 : ¬a) : b :=
false.rec b (H2 H1)
/- not -/
theorem not_false : ¬false :=
assume H : false, H
/- eq -/
notation a = b := eq a b
definition rfl {A : Type} {a : A} := eq.refl a
-- proof irrelevance is built in
theorem proof_irrel {a : Prop} (H₁ H₂ : a) : H₁ = H₂ :=
rfl
namespace eq
variables {A : Type}
variables {a b c a': A}
theorem subst {P : A → Prop} (H₁ : a = b) (H₂ : P a) : P b :=
rec H₂ H₁
theorem trans (H₁ : a = b) (H₂ : b = c) : a = c :=
subst H₂ H₁
definition symm (H : a = b) : b = a :=
rec (refl a) H
namespace ops
notation H `⁻¹` := symm H --input with \sy or \-1 or \inv
notation H1 ⬝ H2 := trans H1 H2
notation H1 ▸ H2 := subst H1 H2
end ops
end eq
section
variable {p : Prop}
open eq.ops
theorem of_eq_true (H : p = true) : p :=
H⁻¹ ▸ trivial
theorem not_of_eq_false (H : p = false) : ¬p :=
assume Hp, H ▸ Hp
end
calc_subst eq.subst
calc_refl eq.refl
calc_trans eq.trans
calc_symm eq.symm
/- ne -/
definition ne {A : Type} (a b : A) := ¬(a = b)
notation a ≠ b := ne a b
namespace ne
open eq.ops
variable {A : Type}
variables {a b : A}
theorem intro : (a = b → false) → a ≠ b :=
assume H, H
theorem elim : a ≠ b → a = b → false :=
assume H₁ H₂, H₁ H₂
theorem irrefl : a ≠ a → false :=
assume H, H rfl
theorem symm : a ≠ b → b ≠ a :=
assume (H : a ≠ b) (H₁ : b = a), H (H₁⁻¹)
end ne
section
open eq.ops
variables {A : Type} {a b c : A}
theorem false.of_ne : a ≠ a → false :=
assume H, H rfl
theorem ne.of_eq_of_ne : a = b → b ≠ c → a ≠ c :=
assume H₁ H₂, H₁⁻¹ ▸ H₂
theorem ne.of_ne_of_eq : a ≠ b → b = c → a ≠ c :=
assume H₁ H₂, H₂ ▸ H₁
end
calc_trans ne.of_eq_of_ne
calc_trans ne.of_ne_of_eq
infixl `==`:50 := heq
namespace heq
universe variable u
variables {A B C : Type.{u}} {a a' : A} {b b' : B} {c : C}
definition to_eq (H : a == a') : a = a' :=
have H₁ : ∀ (Ht : A = A), eq.rec_on Ht a = a, from
λ Ht, eq.refl (eq.rec_on Ht a),
heq.rec_on H H₁ (eq.refl A)
definition elim {A : Type} {a : A} {P : A → Type} {b : A} (H₁ : a == b) (H₂ : P a) : P b :=
eq.rec_on (to_eq H₁) H₂
theorem subst {P : ∀T : Type, T → Prop} (H₁ : a == b) (H₂ : P A a) : P B b :=
rec_on H₁ H₂
theorem symm (H : a == b) : b == a :=
rec_on H (refl a)
theorem of_eq (H : a = a') : a == a' :=
eq.subst H (refl a)
theorem trans (H₁ : a == b) (H₂ : b == c) : a == c :=
subst H₂ H₁
theorem of_heq_of_eq (H₁ : a == b) (H₂ : b = b') : a == b' :=
trans H₁ (of_eq H₂)
theorem of_eq_of_heq (H₁ : a = a') (H₂ : a' == b) : a == b :=
trans (of_eq H₁) H₂
end heq
theorem of_heq_true {a : Prop} (H : a == true) : a :=
of_eq_true (heq.to_eq H)
calc_trans heq.trans
calc_trans heq.of_heq_of_eq
calc_trans heq.of_eq_of_heq
calc_symm heq.symm
/- and -/
notation a /\ b := and a b
notation a ∧ b := and a b
variables {a b c d : Prop}
theorem and.elim (H₁ : a ∧ b) (H₂ : a → b → c) : c :=
and.rec H₂ H₁
/- or -/
notation a `\/` b := or a b
notation a ∨ b := or a b
namespace or
theorem elim (H₁ : a ∨ b) (H₂ : a → c) (H₃ : b → c) : c :=
rec H₂ H₃ H₁
end or
/- iff -/
definition iff (a b : Prop) := (a → b) ∧ (b → a)
notation a <-> b := iff a b
notation a ↔ b := iff a b
namespace iff
definition intro (H₁ : a → b) (H₂ : b → a) : a ↔ b :=
and.intro H₁ H₂
definition elim (H₁ : (a → b) → (b → a) → c) (H₂ : a ↔ b) : c :=
and.rec H₁ H₂
definition elim_left (H : a ↔ b) : a → b :=
elim (assume H₁ H₂, H₁) H
definition mp := @elim_left
definition elim_right (H : a ↔ b) : b → a :=
elim (assume H₁ H₂, H₂) H
definition mp' := @elim_right
definition refl (a : Prop) : a ↔ a :=
intro (assume H, H) (assume H, H)
definition rfl {a : Prop} : a ↔ a :=
refl a
theorem trans (H₁ : a ↔ b) (H₂ : b ↔ c) : a ↔ c :=
intro
(assume Ha, elim_left H₂ (elim_left H₁ Ha))
(assume Hc, elim_right H₁ (elim_right H₂ Hc))
theorem symm (H : a ↔ b) : b ↔ a :=
intro
(assume Hb, elim_right H Hb)
(assume Ha, elim_left H Ha)
open eq.ops
theorem of_eq {a b : Prop} (H : a = b) : a ↔ b :=
iff.intro (λ Ha, H ▸ Ha) (λ Hb, H⁻¹ ▸ Hb)
end iff
definition not_iff_not_of_iff (H₁ : a ↔ b) : ¬a ↔ ¬b :=
iff.intro
(assume (Hna : ¬ a) (Hb : b), absurd (iff.elim_right H₁ Hb) Hna)
(assume (Hnb : ¬ b) (Ha : a), absurd (iff.elim_left H₁ Ha) Hnb)
theorem of_iff_true (H : a ↔ true) : a :=
iff.mp (iff.symm H) trivial
theorem not_of_iff_false (H : a ↔ false) : ¬a :=
assume Ha : a, iff.mp H Ha
calc_refl iff.refl
calc_trans iff.trans
inductive Exists {A : Type} (P : A → Prop) : Prop :=
intro : ∀ (a : A), P a → Exists P
definition exists.intro := @Exists.intro
notation `exists` binders `,` r:(scoped P, Exists P) := r
notation `∃` binders `,` r:(scoped P, Exists P) := r
theorem exists.elim {A : Type} {p : A → Prop} {B : Prop} (H1 : ∃x, p x) (H2 : ∀ (a : A) (H : p a), B) : B :=
Exists.rec H2 H1
inductive decidable [class] (p : Prop) : Type :=
inl : p → decidable p,
inr : ¬p → decidable p
definition true.decidable [instance] : decidable true :=
decidable.inl trivial
definition false.decidable [instance] : decidable false :=
decidable.inr not_false
namespace decidable
variables {p q : Prop}
definition rec_on_true [H : decidable p] {H1 : p → Type} {H2 : ¬p → Type} (H3 : p) (H4 : H1 H3)
: rec_on H H1 H2 :=
rec_on H (λh, H4) (λh, !false.rec (h H3))
definition rec_on_false [H : decidable p] {H1 : p → Type} {H2 : ¬p → Type} (H3 : ¬p) (H4 : H2 H3)
: rec_on H H1 H2 :=
rec_on H (λh, false.rec _ (H3 h)) (λh, H4)
definition by_cases {q : Type} [C : decidable p] (Hpq : p → q) (Hnpq : ¬p → q) : q :=
rec_on C (assume Hp, Hpq Hp) (assume Hnp, Hnpq Hnp)
theorem em (p : Prop) [H : decidable p] : p ∨ ¬p :=
by_cases (λ Hp, or.inl Hp) (λ Hnp, or.inr Hnp)
theorem by_contradiction [Hp : decidable p] (H : ¬p → false) : p :=
by_cases
(assume H1 : p, H1)
(assume H1 : ¬p, false.rec _ (H H1))
end decidable
section
variables {p q : Prop}
open decidable
definition decidable_of_decidable_of_iff (Hp : decidable p) (H : p ↔ q) : decidable q :=
decidable.rec_on Hp
(assume Hp : p, inl (iff.elim_left H Hp))
(assume Hnp : ¬p, inr (iff.elim_left (not_iff_not_of_iff H) Hnp))
definition decidable_of_decidable_of_eq (Hp : decidable p) (H : p = q) : decidable q :=
decidable_of_decidable_of_iff Hp (iff.of_eq H)
end
section
variables {p q : Prop}
open decidable (rec_on inl inr)
definition and.decidable [instance] (Hp : decidable p) (Hq : decidable q) : decidable (p ∧ q) :=
rec_on Hp
(assume Hp : p, rec_on Hq
(assume Hq : q, inl (and.intro Hp Hq))
(assume Hnq : ¬q, inr (assume H : p ∧ q, and.rec_on H (assume Hp Hq, absurd Hq Hnq))))
(assume Hnp : ¬p, inr (assume H : p ∧ q, and.rec_on H (assume Hp Hq, absurd Hp Hnp)))
definition or.decidable [instance] (Hp : decidable p) (Hq : decidable q) : decidable (p ∨ q) :=
rec_on Hp
(assume Hp : p, inl (or.inl Hp))
(assume Hnp : ¬p, rec_on Hq
(assume Hq : q, inl (or.inr Hq))
(assume Hnq : ¬q, inr (assume H : p ∨ q, or.elim H (assume Hp, absurd Hp Hnp) (assume Hq, absurd Hq Hnq))))
definition not.decidable [instance] (Hp : decidable p) : decidable (¬p) :=
rec_on Hp
(assume Hp, inr (λ Hnp, absurd Hp Hnp))
(assume Hnp, inl Hnp)
definition implies.decidable [instance] (Hp : decidable p) (Hq : decidable q) : decidable (p → q) :=
rec_on Hp
(assume Hp : p, rec_on Hq
(assume Hq : q, inl (assume H, Hq))
(assume Hnq : ¬q, inr (assume H : p → q, absurd (H Hp) Hnq)))
(assume Hnp : ¬p, inl (assume Hp, absurd Hp Hnp))
definition iff.decidable [instance] (Hp : decidable p) (Hq : decidable q) : decidable (p ↔ q) := _
end
definition decidable_pred {A : Type} (R : A → Prop) := Π (a : A), decidable (R a)
definition decidable_rel {A : Type} (R : A → A → Prop) := Π (a b : A), decidable (R a b)
definition decidable_eq (A : Type) := decidable_rel (@eq A)
inductive inhabited [class] (A : Type) : Type :=
mk : A → inhabited A
protected definition inhabited.value {A : Type} (h : inhabited A) : A :=
inhabited.rec (λa, a) h
protected definition inhabited.destruct {A : Type} {B : Type} (H1 : inhabited A) (H2 : A → B) : B :=
inhabited.rec H2 H1
definition default (A : Type) [H : inhabited A] : A :=
inhabited.rec (λa, a) H
opaque definition arbitrary (A : Type) [H : inhabited A] : A :=
inhabited.rec (λa, a) H
definition Prop_inhabited [instance] : inhabited Prop :=
inhabited.mk true
definition fun_inhabited [instance] (A : Type) {B : Type} (H : inhabited B) : inhabited (A → B) :=
inhabited.rec_on H (λb, inhabited.mk (λa, b))
definition dfun_inhabited [instance] (A : Type) {B : A → Type} (H : Πx, inhabited (B x)) :
inhabited (Πx, B x) :=
inhabited.mk (λa, inhabited.rec_on (H a) (λb, b))
inductive nonempty [class] (A : Type) : Prop :=
intro : A → nonempty A
protected definition nonempty.elim {A : Type} {B : Prop} (H1 : nonempty A) (H2 : A → B) : B :=
nonempty.rec H2 H1
theorem inhabited_imp_nonempty [instance] {A : Type} (H : inhabited A) : nonempty A :=
nonempty.intro (default A)
definition ite (c : Prop) [H : decidable c] {A : Type} (t e : A) : A :=
decidable.rec_on H (λ Hc, t) (λ Hnc, e)
definition if_pos {c : Prop} [H : decidable c] (Hc : c) {A : Type} {t e : A} : (if c then t else e) = t :=
decidable.rec
(λ Hc : c, eq.refl (@ite c (decidable.inl Hc) A t e))
(λ Hnc : ¬c, absurd Hc Hnc)
H
definition if_neg {c : Prop} [H : decidable c] (Hnc : ¬c) {A : Type} {t e : A} : (if c then t else e) = e :=
decidable.rec
(λ Hc : c, absurd Hc Hnc)
(λ Hnc : ¬c, eq.refl (@ite c (decidable.inr Hnc) A t e))
H
definition if_t_t (c : Prop) [H : decidable c] {A : Type} (t : A) : (if c then t else t) = t :=
decidable.rec
(λ Hc : c, eq.refl (@ite c (decidable.inl Hc) A t t))
(λ Hnc : ¬c, eq.refl (@ite c (decidable.inr Hnc) A t t))
H
-- We use "dependent" if-then-else to be able to communicate the if-then-else condition
-- to the branches
definition dite (c : Prop) [H : decidable c] {A : Type} (t : c → A) (e : ¬ c → A) : A :=
decidable.rec_on H (λ Hc, t Hc) (λ Hnc, e Hnc)
definition dif_pos {c : Prop} [H : decidable c] (Hc : c) {A : Type} {t : c → A} {e : ¬ c → A} : (if H : c then t H else e H) = t Hc :=
decidable.rec
(λ Hc : c, eq.refl (@dite c (decidable.inl Hc) A t e))
(λ Hnc : ¬c, absurd Hc Hnc)
H
definition dif_neg {c : Prop} [H : decidable c] (Hnc : ¬c) {A : Type} {t : c → A} {e : ¬ c → A} : (if H : c then t H else e H) = e Hnc :=
decidable.rec
(λ Hc : c, absurd Hc Hnc)
(λ Hnc : ¬c, eq.refl (@dite c (decidable.inr Hnc) A t e))
H
-- Remark: dite and ite are "definitionally equal" when we ignore the proofs.
theorem dite_ite_eq (c : Prop) [H : decidable c] {A : Type} (t : A) (e : A) : dite c (λh, t) (λh, e) = ite c t e :=
rfl
definition is_true (c : Prop) [H : decidable c] : Prop :=
if c then true else false
definition is_false (c : Prop) [H : decidable c] : Prop :=
if c then false else true
theorem of_is_true {c : Prop} [H₁ : decidable c] (H₂ : is_true c) : c :=
decidable.rec_on H₁ (λ Hc, Hc) (λ Hnc, !false.rec (if_neg Hnc ▸ H₂))
notation `dec_trivial` := of_is_true trivial
theorem not_of_not_is_true {c : Prop} [H₁ : decidable c] (H₂ : ¬ is_true c) : ¬ c :=
decidable.rec_on H₁ (λ Hc, absurd true.intro (if_pos Hc ▸ H₂)) (λ Hnc, Hnc)
theorem not_of_is_false {c : Prop} [H₁ : decidable c] (H₂ : is_false c) : ¬ c :=
decidable.rec_on H₁ (λ Hc, !false.rec (if_pos Hc ▸ H₂)) (λ Hnc, Hnc)
theorem of_not_is_false {c : Prop} [H₁ : decidable c] (H₂ : ¬ is_false c) : c :=
decidable.rec_on H₁ (λ Hc, Hc) (λ Hnc, absurd true.intro (if_neg Hnc ▸ H₂))
|
f07315776277f2abfed62b6590dd8a173f3463b8 | bb31430994044506fa42fd667e2d556327e18dfe | /src/data/quot.lean | eb8bc111569c60c6146109d88943b9fc74dea9b6 | [
"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,265 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import logic.relator
/-!
# Quotient types
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This module extends the core library's treatment of quotient types (`init.data.quot`).
## Tags
quotient
-/
variables {α : Sort*} {β : Sort*}
open function
namespace setoid
lemma ext {α : Sort*} :
∀{s t : setoid α}, (∀a b, @setoid.r α s a b ↔ @setoid.r α t a b) → s = t
| ⟨r, _⟩ ⟨p, _⟩ eq :=
have r = p, from funext $ assume a, funext $ assume b, propext $ eq a b,
by subst this
end setoid
namespace quot
variables {ra : α → α → Prop} {rb : β → β → Prop} {φ : quot ra → quot rb → Sort*}
local notation (name := mk) `⟦`:max a `⟧` := quot.mk _ a
instance (r : α → α → Prop) [inhabited α] : inhabited (quot r) := ⟨⟦default⟧⟩
instance [subsingleton α] : subsingleton (quot ra) :=
⟨λ x, quot.induction_on x (λ y, quot.ind (λ b, congr_arg _ (subsingleton.elim _ _)))⟩
/-- Recursion on two `quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/
protected def hrec_on₂ (qa : quot ra) (qb : quot rb) (f : Π a b, φ ⟦a⟧ ⟦b⟧)
(ca : ∀ {b a₁ a₂}, ra a₁ a₂ → f a₁ b == f a₂ b)
(cb : ∀ {a b₁ b₂}, rb b₁ b₂ → f a b₁ == f a b₂) : φ qa qb :=
quot.hrec_on qa (λ a, quot.hrec_on qb (f a) (λ b₁ b₂ pb, cb pb)) $ λ a₁ a₂ pa,
quot.induction_on qb $ λ b,
calc @quot.hrec_on _ _ (φ _) ⟦b⟧ (f a₁) (@cb _)
== f a₁ b : by simp [heq_self_iff_true]
... == f a₂ b : ca pa
... == @quot.hrec_on _ _ (φ _) ⟦b⟧ (f a₂) (@cb _) : by simp [heq_self_iff_true]
/-- Map a function `f : α → β` such that `ra x y` implies `rb (f x) (f y)`
to a map `quot ra → quot rb`. -/
protected def map (f : α → β) (h : (ra ⇒ rb) f f) : quot ra → quot rb :=
quot.lift (λ x, ⟦f x⟧) $ assume x y (h₁ : ra x y), quot.sound $ h h₁
/-- If `ra` is a subrelation of `ra'`, then we have a natural map `quot ra → quot ra'`. -/
protected def map_right {ra' : α → α → Prop} (h : ∀a₁ a₂, ra a₁ a₂ → ra' a₁ a₂) :
quot ra → quot ra' :=
quot.map id h
/-- Weaken the relation of a quotient. This is the same as `quot.map id`. -/
def factor {α : Type*} (r s : α → α → Prop) (h : ∀ x y, r x y → s x y) :
quot r → quot s :=
quot.lift (quot.mk s) (λ x y rxy, quot.sound (h x y rxy))
lemma factor_mk_eq {α : Type*} (r s : α → α → Prop) (h : ∀ x y, r x y → s x y) :
factor r s h ∘ quot.mk _ = quot.mk _ := rfl
variables {γ : Sort*} {r : α → α → Prop} {s : β → β → Prop}
/-- **Alias** of `quot.lift_beta`. -/
lemma lift_mk (f : α → γ) (h : ∀ a₁ a₂, r a₁ a₂ → f a₁ = f a₂) (a : α) :
quot.lift f h (quot.mk r a) = f a := rfl
@[simp]
lemma lift_on_mk (a : α) (f : α → γ) (h : ∀ a₁ a₂, r a₁ a₂ → f a₁ = f a₂) :
quot.lift_on (quot.mk r a) f h = f a := rfl
@[simp] lemma surjective_lift {f : α → γ} (h : ∀ a₁ a₂, r a₁ a₂ → f a₁ = f a₂) :
surjective (lift f h) ↔ surjective f :=
⟨λ hf, hf.comp quot.exists_rep, λ hf y, let ⟨x, hx⟩ := hf y in ⟨quot.mk _ x, hx⟩⟩
/-- Descends a function `f : α → β → γ` to quotients of `α` and `β`. -/
attribute [reducible, elab_as_eliminator]
protected def lift₂
(f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b)
(q₁ : quot r) (q₂ : quot s) : γ :=
quot.lift (λ a, quot.lift (f a) (hr a))
(λ a₁ a₂ ha, funext (λ q, quot.induction_on q (λ b, hs a₁ a₂ b ha)))
q₁ q₂
@[simp]
lemma lift₂_mk (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) (a : α) (b : β) :
quot.lift₂ f hr hs (quot.mk r a) (quot.mk s b) = f a b := rfl
/-- Descends a function `f : α → β → γ` to quotients of `α` and `β` and applies it. -/
attribute [reducible, elab_as_eliminator]
protected def lift_on₂ (p : quot r) (q : quot s) (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) : γ := quot.lift₂ f hr hs p q
@[simp]
lemma lift_on₂_mk (a : α) (b : β) (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) :
quot.lift_on₂ (quot.mk r a) (quot.mk s b) f hr hs = f a b := rfl
variables {t : γ → γ → Prop}
/-- Descends a function `f : α → β → γ` to quotients of `α` and `β` wih values in a quotient of
`γ`. -/
protected def map₂ (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → t (f a b₁) (f a b₂))
(hs : ∀ a₁ a₂ b, r a₁ a₂ → t (f a₁ b) (f a₂ b))
(q₁ : quot r) (q₂ : quot s) : quot t :=
quot.lift₂ (λ a b, quot.mk t $ f a b) (λ a b₁ b₂ hb, quot.sound (hr a b₁ b₂ hb))
(λ a₁ a₂ b ha, quot.sound (hs a₁ a₂ b ha)) q₁ q₂
@[simp]
lemma map₂_mk (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → t (f a b₁) (f a b₂))
(hs : ∀ a₁ a₂ b, r a₁ a₂ → t (f a₁ b) (f a₂ b))
(a : α) (b : β) : quot.map₂ f hr hs (quot.mk r a) (quot.mk s b) = quot.mk t (f a b) := rfl
/-- A binary version of `quot.rec_on_subsingleton`. -/
@[reducible, elab_as_eliminator]
protected def rec_on_subsingleton₂ {φ : quot r → quot s → Sort*}
[h : ∀ a b, subsingleton (φ ⟦a⟧ ⟦b⟧)] (q₁ : quot r) (q₂ : quot s) (f : Π a b, φ ⟦a⟧ ⟦b⟧) :
φ q₁ q₂ :=
@quot.rec_on_subsingleton _ r (λ q, φ q q₂) (λ a, quot.ind (h a) q₂) q₁ $
λ a, quot.rec_on_subsingleton q₂ $ λ b, f a b
attribute [elab_as_eliminator]
protected lemma induction_on₂
{δ : quot r → quot s → Prop} (q₁ : quot r) (q₂ : quot s)
(h : ∀ a b, δ (quot.mk r a) (quot.mk s b)) : δ q₁ q₂ :=
quot.ind (λ a₁, quot.ind (λ a₂, h a₁ a₂) q₂) q₁
attribute [elab_as_eliminator]
protected lemma induction_on₃
{δ : quot r → quot s → quot t → Prop} (q₁ : quot r) (q₂ : quot s) (q₃ : quot t)
(h : ∀ a b c, δ (quot.mk r a) (quot.mk s b) (quot.mk t c)) : δ q₁ q₂ q₃ :=
quot.ind (λ a₁, quot.ind (λ a₂, quot.ind (λ a₃, h a₁ a₂ a₃) q₃) q₂) q₁
instance (r : α → α → Prop) (f : α → Prop) (h : ∀ a b, r a b → f a = f b) [hf : decidable_pred f] :
decidable_pred (quot.lift f h) :=
λ q, quot.rec_on_subsingleton q hf
/-- Note that this provides `decidable_rel (quot.lift₂ f ha hb)` when `α = β`. -/
instance (r : α → α → Prop) (s : β → β → Prop) (f : α → β → Prop)
(ha : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂) (hb : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b)
[hf : Π a, decidable_pred (f a)] (q₁ : quot r) :
decidable_pred (quot.lift₂ f ha hb q₁) :=
λ q₂, quot.rec_on_subsingleton₂ q₁ q₂ hf
instance (r : α → α → Prop) (q : quot r) (f : α → Prop) (h : ∀ a b, r a b → f a = f b)
[decidable_pred f] :
decidable (quot.lift_on q f h) :=
quot.lift.decidable_pred _ _ _ _
instance (r : α → α → Prop) (s : β → β → Prop) (q₁ : quot r) (q₂ : quot s) (f : α → β → Prop)
(ha : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂) (hb : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b)
[Π a, decidable_pred (f a)] :
decidable (quot.lift_on₂ q₁ q₂ f ha hb) :=
quot.lift₂.decidable_pred _ _ _ _ _ _ _
end quot
namespace quotient
variables [sa : setoid α] [sb : setoid β]
variables {φ : quotient sa → quotient sb → Sort*}
instance (s : setoid α) [inhabited α] : inhabited (quotient s) := ⟨⟦default⟧⟩
instance (s : setoid α) [subsingleton α] : subsingleton (quotient s) :=
quot.subsingleton
instance {α : Type*} [setoid α] : is_equiv α (≈) :=
{ refl := setoid.refl,
symm := λ a b, setoid.symm,
trans := λ a b c, setoid.trans }
/-- Induction on two `quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/
protected def hrec_on₂ (qa : quotient sa) (qb : quotient sb) (f : Π a b, φ ⟦a⟧ ⟦b⟧)
(c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) : φ qa qb :=
quot.hrec_on₂ qa qb f
(λ _ _ _ p, c _ _ _ _ p (setoid.refl _))
(λ _ _ _ p, c _ _ _ _ (setoid.refl _) p)
/-- Map a function `f : α → β` that sends equivalent elements to equivalent elements
to a function `quotient sa → quotient sb`. Useful to define unary operations on quotients. -/
protected def map (f : α → β) (h : ((≈) ⇒ (≈)) f f) : quotient sa → quotient sb :=
quot.map f h
@[simp] lemma map_mk (f : α → β) (h : ((≈) ⇒ (≈)) f f) (x : α) :
quotient.map f h (⟦x⟧ : quotient sa) = (⟦f x⟧ : quotient sb) :=
rfl
variables {γ : Sort*} [sc : setoid γ]
/-- Map a function `f : α → β → γ` that sends equivalent elements to equivalent elements
to a function `f : quotient sa → quotient sb → quotient sc`.
Useful to define binary operations on quotients. -/
protected def map₂ (f : α → β → γ) (h : ((≈) ⇒ (≈) ⇒ (≈)) f f) :
quotient sa → quotient sb → quotient sc :=
quotient.lift₂ (λ x y, ⟦f x y⟧) (λ x₁ y₁ x₂ y₂ h₁ h₂, quot.sound $ h h₁ h₂)
@[simp] lemma map₂_mk (f : α → β → γ) (h : ((≈) ⇒ (≈) ⇒ (≈)) f f) (x : α) (y : β) :
quotient.map₂ f h (⟦x⟧ : quotient sa) (⟦y⟧ : quotient sb) = (⟦f x y⟧ : quotient sc) := rfl
include sa
instance (f : α → Prop) (h : ∀ a b, a ≈ b → f a = f b) [decidable_pred f] :
decidable_pred (quotient.lift f h) :=
quot.lift.decidable_pred _ _ _
include sb
/-- Note that this provides `decidable_rel (quotient.lift₂ f h)` when `α = β`. -/
instance (f : α → β → Prop) (h : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ = f a₂ b₂)
[hf : Π a, decidable_pred (f a)] (q₁ : quotient sa) :
decidable_pred (quotient.lift₂ f h q₁) :=
λ q₂, quotient.rec_on_subsingleton₂ q₁ q₂ hf
omit sb
instance (q : quotient sa) (f : α → Prop) (h : ∀ a b, a ≈ b → f a = f b) [decidable_pred f] :
decidable (quotient.lift_on q f h) :=
quotient.lift.decidable_pred _ _ _
instance (q₁ : quotient sa) (q₂ : quotient sb) (f : α → β → Prop)
(h : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ = f a₂ b₂) [Π a, decidable_pred (f a)] :
decidable (quotient.lift_on₂ q₁ q₂ f h) :=
quotient.lift₂.decidable_pred _ _ _ _
end quotient
lemma quot.eq {α : Type*} {r : α → α → Prop} {x y : α} :
quot.mk r x = quot.mk r y ↔ eqv_gen r x y :=
⟨quot.exact r, quot.eqv_gen_sound⟩
@[simp] theorem quotient.eq [r : setoid α] {x y : α} : ⟦x⟧ = ⟦y⟧ ↔ x ≈ y :=
⟨quotient.exact, quotient.sound⟩
theorem forall_quotient_iff {α : Type*} [r : setoid α] {p : quotient r → Prop} :
(∀a:quotient r, p a) ↔ (∀a:α, p ⟦a⟧) :=
⟨assume h x, h _, assume h a, a.induction_on h⟩
@[simp] lemma quotient.lift_mk [s : setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b)
(x : α) :
quotient.lift f h (quotient.mk x) = f x := rfl
@[simp] lemma quotient.lift_comp_mk [setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b) :
quotient.lift f h ∘ quotient.mk = f :=
rfl
@[simp] lemma quotient.lift₂_mk {α : Sort*} {β : Sort*} {γ : Sort*} [setoid α] [setoid β]
(f : α → β → γ)
(h : ∀ (a₁ : α) (a₂ : β) (b₁ : α) (b₂ : β), a₁ ≈ b₁ → a₂ ≈ b₂ → f a₁ a₂ = f b₁ b₂)
(a : α) (b : β) :
quotient.lift₂ f h (quotient.mk a) (quotient.mk b) = f a b := rfl
@[simp] lemma quotient.lift_on_mk [s : setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b)
(x : α) :
quotient.lift_on (quotient.mk x) f h = f x := rfl
@[simp] theorem quotient.lift_on₂_mk {α : Sort*} {β : Sort*} [setoid α] (f : α → α → β)
(h : ∀ (a₁ a₂ b₁ b₂ : α), a₁ ≈ b₁ → a₂ ≈ b₂ → f a₁ a₂ = f b₁ b₂) (x y : α) :
quotient.lift_on₂ (quotient.mk x) (quotient.mk y) f h = f x y := rfl
/-- `quot.mk r` is a surjective function. -/
lemma surjective_quot_mk (r : α → α → Prop) : surjective (quot.mk r) := quot.exists_rep
/-- `quotient.mk` is a surjective function. -/
lemma surjective_quotient_mk (α : Sort*) [s : setoid α] :
surjective (quotient.mk : α → quotient s) :=
quot.exists_rep
/-- Choose an element of the equivalence class using the axiom of choice.
Sound but noncomputable. -/
noncomputable def quot.out {r : α → α → Prop} (q : quot r) : α :=
classical.some (quot.exists_rep q)
/-- Unwrap the VM representation of a quotient to obtain an element of the equivalence class.
Computable but unsound. -/
meta def quot.unquot {r : α → α → Prop} : quot r → α := unchecked_cast
@[simp] theorem quot.out_eq {r : α → α → Prop} (q : quot r) : quot.mk r q.out = q :=
classical.some_spec (quot.exists_rep q)
/-- Choose an element of the equivalence class using the axiom of choice.
Sound but noncomputable. -/
noncomputable def quotient.out [s : setoid α] : quotient s → α := quot.out
@[simp] theorem quotient.out_eq [s : setoid α] (q : quotient s) : ⟦q.out⟧ = q := q.out_eq
theorem quotient.mk_out [s : setoid α] (a : α) : ⟦a⟧.out ≈ a :=
quotient.exact (quotient.out_eq _)
lemma quotient.mk_eq_iff_out [s : setoid α] {x : α} {y : quotient s} :
⟦x⟧ = y ↔ x ≈ quotient.out y :=
begin
refine iff.trans _ quotient.eq,
rw quotient.out_eq y,
end
lemma quotient.eq_mk_iff_out [s : setoid α] {x : quotient s} {y : α} :
x = ⟦y⟧ ↔ quotient.out x ≈ y :=
begin
refine iff.trans _ quotient.eq,
rw quotient.out_eq x,
end
@[simp] lemma quotient.out_equiv_out {s : setoid α} {x y : quotient s} :
x.out ≈ y.out ↔ x = y :=
by rw [← quotient.eq_mk_iff_out, quotient.out_eq]
lemma quotient.out_injective {s : setoid α} : injective (@quotient.out α s) :=
λ a b h, quotient.out_equiv_out.1 $ h ▸ setoid.refl _
@[simp] lemma quotient.out_inj {s : setoid α} {x y : quotient s} :
x.out = y.out ↔ x = y :=
⟨λ h, quotient.out_injective h, λ h, h ▸ rfl⟩
section pi
instance pi_setoid {ι : Sort*} {α : ι → Sort*} [∀ i, setoid (α i)] : setoid (Π i, α i) :=
{ r := λ a b, ∀ i, a i ≈ b i,
iseqv := ⟨
λ a i, setoid.refl _,
λ a b h i, setoid.symm (h _),
λ a b c h₁ h₂ i, setoid.trans (h₁ _) (h₂ _)⟩ }
/-- Given a function `f : Π i, quotient (S i)`, returns the class of functions `Π i, α i` sending
each `i` to an element of the class `f i`. -/
noncomputable def quotient.choice {ι : Type*} {α : ι → Type*} [S : Π i, setoid (α i)]
(f : Π i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) :=
⟦λ i, (f i).out⟧
@[simp] theorem quotient.choice_eq {ι : Type*} {α : ι → Type*} [Π i, setoid (α i)]
(f : Π i, α i) : quotient.choice (λ i, ⟦f i⟧) = ⟦f⟧ :=
quotient.sound $ λ i, quotient.mk_out _
@[elab_as_eliminator] lemma quotient.induction_on_pi
{ι : Type*} {α : ι → Sort*} [s : ∀ i, setoid (α i)]
{p : (Π i, quotient (s i)) → Prop} (f : Π i, quotient (s i))
(h : ∀ a : Π i, α i, p (λ i, ⟦a i⟧)) : p f :=
begin
rw ← (funext (λ i, quotient.out_eq (f i)) : (λ i, ⟦(f i).out⟧) = f),
apply h,
end
end pi
lemma nonempty_quotient_iff (s : setoid α) : nonempty (quotient s) ↔ nonempty α :=
⟨assume ⟨a⟩, quotient.induction_on a nonempty.intro, assume ⟨a⟩, ⟨⟦a⟧⟩⟩
/-! ### Truncation -/
/-- `trunc α` is the quotient of `α` by the always-true relation. This
is related to the propositional truncation in HoTT, and is similar
in effect to `nonempty α`, but unlike `nonempty α`, `trunc α` is data,
so the VM representation is the same as `α`, and so this can be used to
maintain computability. -/
def {u} trunc (α : Sort u) : Sort u := @quot α (λ _ _, true)
theorem true_equivalence : @equivalence α (λ _ _, true) :=
⟨λ _, trivial, λ _ _ _, trivial, λ _ _ _ _ _, trivial⟩
namespace trunc
/-- Constructor for `trunc α` -/
def mk (a : α) : trunc α := quot.mk _ a
instance [inhabited α] : inhabited (trunc α) := ⟨mk default⟩
/-- Any constant function lifts to a function out of the truncation -/
def lift (f : α → β) (c : ∀ a b : α, f a = f b) : trunc α → β :=
quot.lift f (λ a b _, c a b)
theorem ind {β : trunc α → Prop} : (∀ a : α, β (mk a)) → ∀ q : trunc α, β q := quot.ind
protected theorem lift_mk (f : α → β) (c) (a : α) : lift f c (mk a) = f a := rfl
/-- Lift a constant function on `q : trunc α`. -/
@[reducible, elab_as_eliminator]
protected def lift_on (q : trunc α) (f : α → β)
(c : ∀ a b : α, f a = f b) : β := lift f c q
@[elab_as_eliminator]
protected theorem induction_on {β : trunc α → Prop} (q : trunc α)
(h : ∀ a, β (mk a)) : β q := ind h q
theorem exists_rep (q : trunc α) : ∃ a : α, mk a = q := quot.exists_rep q
attribute [elab_as_eliminator]
protected theorem induction_on₂ {C : trunc α → trunc β → Prop} (q₁ : trunc α) (q₂ : trunc β)
(h : ∀ a b, C (mk a) (mk b)) : C q₁ q₂ :=
trunc.induction_on q₁ $ λ a₁, trunc.induction_on q₂ (h a₁)
protected theorem eq (a b : trunc α) : a = b :=
trunc.induction_on₂ a b (λ x y, quot.sound trivial)
instance : subsingleton (trunc α) := ⟨trunc.eq⟩
/-- The `bind` operator for the `trunc` monad. -/
def bind (q : trunc α) (f : α → trunc β) : trunc β :=
trunc.lift_on q f (λ a b, trunc.eq _ _)
/-- A function `f : α → β` defines a function `map f : trunc α → trunc β`. -/
def map (f : α → β) (q : trunc α) : trunc β := bind q (trunc.mk ∘ f)
instance : monad trunc :=
{ pure := @trunc.mk,
bind := @trunc.bind }
instance : is_lawful_monad trunc :=
{ id_map := λ α q, trunc.eq _ _,
pure_bind := λ α β q f, rfl,
bind_assoc := λ α β γ x f g, trunc.eq _ _ }
variable {C : trunc α → Sort*}
/-- Recursion/induction principle for `trunc`. -/
@[reducible, elab_as_eliminator]
protected def rec
(f : Π a, C (mk a)) (h : ∀ (a b : α), (eq.rec (f a) (trunc.eq (mk a) (mk b)) : C (mk b)) = f b)
(q : trunc α) : C q :=
quot.rec f (λ a b _, h a b) q
/-- A version of `trunc.rec` taking `q : trunc α` as the first argument. -/
@[reducible, elab_as_eliminator]
protected def rec_on (q : trunc α) (f : Π a, C (mk a))
(h : ∀ (a b : α), (eq.rec (f a) (trunc.eq (mk a) (mk b)) : C (mk b)) = f b) : C q :=
trunc.rec f h q
/-- A version of `trunc.rec_on` assuming the codomain is a `subsingleton`. -/
@[reducible, elab_as_eliminator]
protected def rec_on_subsingleton
[∀ a, subsingleton (C (mk a))] (q : trunc α) (f : Π a, C (mk a)) : C q :=
trunc.rec f (λ a b, subsingleton.elim _ (f b)) q
/-- Noncomputably extract a representative of `trunc α` (using the axiom of choice). -/
noncomputable def out : trunc α → α := quot.out
@[simp] theorem out_eq (q : trunc α) : mk q.out = q := trunc.eq _ _
protected theorem nonempty (q : trunc α) : nonempty α :=
nonempty_of_exists q.exists_rep
end trunc
/-! ### `quotient` with implicit `setoid` -/
namespace quotient
variables {γ : Sort*} {φ : Sort*}
{s₁ : setoid α} {s₂ : setoid β} {s₃ : setoid γ}
/-! Versions of quotient definitions and lemmas ending in `'` use unification instead
of typeclass inference for inferring the `setoid` argument. This is useful when there are
several different quotient relations on a type, for example quotient groups, rings and modules. -/
/-- A version of `quotient.mk` taking `{s : setoid α}` as an implicit argument instead of an
instance argument. -/
protected def mk' (a : α) : quotient s₁ := quot.mk s₁.1 a
/-- `quotient.mk'` is a surjective function. -/
lemma surjective_quotient_mk' : surjective (quotient.mk' : α → quotient s₁) :=
quot.exists_rep
/-- A version of `quotient.lift_on` taking `{s : setoid α}` as an implicit argument instead of an
instance argument. -/
@[elab_as_eliminator, reducible]
protected def lift_on' (q : quotient s₁) (f : α → φ)
(h : ∀ a b, @setoid.r α s₁ a b → f a = f b) : φ := quotient.lift_on q f h
@[simp]
protected lemma lift_on'_mk' (f : α → φ) (h) (x : α) :
quotient.lift_on' (@quotient.mk' _ s₁ x) f h = f x := rfl
@[simp] lemma surjective_lift_on' {f : α → φ} (h : ∀ a b, @setoid.r α s₁ a b → f a = f b) :
surjective (λ x, quotient.lift_on' x f h) ↔ surjective f :=
quot.surjective_lift _
/-- A version of `quotient.lift_on₂` taking `{s₁ : setoid α} {s₂ : setoid β}` as implicit arguments
instead of instance arguments. -/
@[elab_as_eliminator, reducible]
protected def lift_on₂' (q₁ : quotient s₁) (q₂ : quotient s₂) (f : α → β → γ)
(h : ∀ a₁ a₂ b₁ b₂, @setoid.r α s₁ a₁ b₁ → @setoid.r β s₂ a₂ b₂ → f a₁ a₂ = f b₁ b₂) : γ :=
quotient.lift_on₂ q₁ q₂ f h
@[simp]
protected lemma lift_on₂'_mk' (f : α → β → γ) (h) (a : α) (b : β) :
quotient.lift_on₂' (@quotient.mk' _ s₁ a) (@quotient.mk' _ s₂ b) f h = f a b := rfl
/-- A version of `quotient.ind` taking `{s : setoid α}` as an implicit argument instead of an
instance argument. -/
@[elab_as_eliminator]
protected lemma ind' {p : quotient s₁ → Prop}
(h : ∀ a, p (quotient.mk' a)) (q : quotient s₁) : p q :=
quotient.ind h q
/-- A version of `quotient.ind₂` taking `{s₁ : setoid α} {s₂ : setoid β}` as implicit arguments
instead of instance arguments. -/
@[elab_as_eliminator]
protected lemma ind₂' {p : quotient s₁ → quotient s₂ → Prop}
(h : ∀ a₁ a₂, p (quotient.mk' a₁) (quotient.mk' a₂))
(q₁ : quotient s₁) (q₂ : quotient s₂) : p q₁ q₂ :=
quotient.ind₂ h q₁ q₂
/-- A version of `quotient.induction_on` taking `{s : setoid α}` as an implicit argument instead
of an instance argument. -/
@[elab_as_eliminator]
protected lemma induction_on' {p : quotient s₁ → Prop} (q : quotient s₁)
(h : ∀ a, p (quotient.mk' a)) : p q := quotient.induction_on q h
/-- A version of `quotient.induction_on₂` taking `{s₁ : setoid α} {s₂ : setoid β}` as implicit
arguments instead of instance arguments. -/
@[elab_as_eliminator]
protected lemma induction_on₂' {p : quotient s₁ → quotient s₂ → Prop} (q₁ : quotient s₁)
(q₂ : quotient s₂) (h : ∀ a₁ a₂, p (quotient.mk' a₁) (quotient.mk' a₂)) : p q₁ q₂ :=
quotient.induction_on₂ q₁ q₂ h
/-- A version of `quotient.induction_on₃` taking `{s₁ : setoid α} {s₂ : setoid β} {s₃ : setoid γ}`
as implicit arguments instead of instance arguments. -/
@[elab_as_eliminator]
protected lemma induction_on₃' {p : quotient s₁ → quotient s₂ → quotient s₃ → Prop}
(q₁ : quotient s₁) (q₂ : quotient s₂) (q₃ : quotient s₃)
(h : ∀ a₁ a₂ a₃, p (quotient.mk' a₁) (quotient.mk' a₂) (quotient.mk' a₃)) : p q₁ q₂ q₃ :=
quotient.induction_on₃ q₁ q₂ q₃ h
/-- A version of `quotient.rec_on_subsingleton` taking `{s₁ : setoid α}` as an implicit argument
instead of an instance argument. -/
@[elab_as_eliminator]
protected def rec_on_subsingleton' {φ : quotient s₁ → Sort*}
[h : ∀ a, subsingleton (φ ⟦a⟧)] (q : quotient s₁) (f : Π a, φ (quotient.mk' a)) : φ q :=
quotient.rec_on_subsingleton q f
/-- A version of `quotient.rec_on_subsingleton₂` taking `{s₁ : setoid α} {s₂ : setoid α}`
as implicit arguments instead of instance arguments. -/
attribute [reducible, elab_as_eliminator]
protected def rec_on_subsingleton₂'
{φ : quotient s₁ → quotient s₂ → Sort*} [h : ∀ a b, subsingleton (φ ⟦a⟧ ⟦b⟧)]
(q₁ : quotient s₁) (q₂ : quotient s₂) (f : Π a₁ a₂, φ (quotient.mk' a₁) (quotient.mk' a₂)) :
φ q₁ q₂ :=
quotient.rec_on_subsingleton₂ q₁ q₂ f
/-- Recursion on a `quotient` argument `a`, result type depends on `⟦a⟧`. -/
protected def hrec_on' {φ : quotient s₁ → Sort*} (qa : quotient s₁) (f : Π a, φ (quotient.mk' a))
(c : ∀ a₁ a₂, a₁ ≈ a₂ → f a₁ == f a₂) : φ qa :=
quot.hrec_on qa f c
@[simp] lemma hrec_on'_mk' {φ : quotient s₁ → Sort*} (f : Π a, φ (quotient.mk' a))
(c : ∀ a₁ a₂, a₁ ≈ a₂ → f a₁ == f a₂) (x : α) :
(quotient.mk' x).hrec_on' f c = f x :=
rfl
/-- Recursion on two `quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/
protected def hrec_on₂' {φ : quotient s₁ → quotient s₂ → Sort*} (qa : quotient s₁)
(qb : quotient s₂) (f : ∀ a b, φ (quotient.mk' a) (quotient.mk' b))
(c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) : φ qa qb :=
quotient.hrec_on₂ qa qb f c
@[simp] lemma hrec_on₂'_mk' {φ : quotient s₁ → quotient s₂ → Sort*}
(f : ∀ a b, φ (quotient.mk' a) (quotient.mk' b))
(c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) (x : α) (qb : quotient s₂) :
(quotient.mk' x).hrec_on₂' qb f c = qb.hrec_on' (f x) (λ b₁ b₂, c _ _ _ _ (setoid.refl _)) :=
rfl
/-- Map a function `f : α → β` that sends equivalent elements to equivalent elements
to a function `quotient sa → quotient sb`. Useful to define unary operations on quotients. -/
protected def map' (f : α → β) (h : (s₁.r ⇒ s₂.r) f f) :
quotient s₁ → quotient s₂ :=
quot.map f h
@[simp] lemma map'_mk' (f : α → β) (h) (x : α) :
(quotient.mk' x : quotient s₁).map' f h = (quotient.mk' (f x) : quotient s₂) :=
rfl
/-- A version of `quotient.map₂` using curly braces and unification. -/
protected def map₂' (f : α → β → γ) (h : (s₁.r ⇒ s₂.r ⇒ s₃.r) f f) :
quotient s₁ → quotient s₂ → quotient s₃ :=
quotient.map₂ f h
@[simp] lemma map₂'_mk' (f : α → β → γ) (h) (x : α) :
(quotient.mk' x : quotient s₁).map₂' f h =
(quotient.map' (f x) (h (setoid.refl x)) : quotient s₂ → quotient s₃) :=
rfl
lemma exact' {a b : α} :
(quotient.mk' a : quotient s₁) = quotient.mk' b → @setoid.r _ s₁ a b :=
quotient.exact
lemma sound' {a b : α} : @setoid.r _ s₁ a b → @quotient.mk' α s₁ a = quotient.mk' b :=
quotient.sound
@[simp]
protected lemma eq' {a b : α} : @quotient.mk' α s₁ a = quotient.mk' b ↔ @setoid.r _ s₁ a b :=
quotient.eq
/-- A version of `quotient.out` taking `{s₁ : setoid α}` as an implicit argument instead of an
instance argument. -/
noncomputable def out' (a : quotient s₁) : α := quotient.out a
@[simp] theorem out_eq' (q : quotient s₁) : quotient.mk' q.out' = q := q.out_eq
theorem mk_out' (a : α) : @setoid.r α s₁ (quotient.mk' a : quotient s₁).out' a :=
quotient.exact (quotient.out_eq _)
section
variables [setoid α]
protected lemma mk'_eq_mk (x : α) : quotient.mk' x = ⟦x⟧ := rfl
@[simp] protected lemma lift_on'_mk (x : α) (f : α → β) (h) : ⟦x⟧.lift_on' f h = f x := rfl
@[simp] protected lemma lift_on₂'_mk [setoid β] (f : α → β → γ) (h) (a : α) (b : β) :
quotient.lift_on₂' ⟦a⟧ ⟦b⟧ f h = f a b := quotient.lift_on₂'_mk' _ _ _ _
@[simp] lemma map'_mk [setoid β] (f : α → β) (h) (x : α) : ⟦x⟧.map' f h = ⟦f x⟧ := rfl
end
instance (q : quotient s₁) (f : α → Prop) (h : ∀ a b, @setoid.r α s₁ a b → f a = f b)
[decidable_pred f] :
decidable (quotient.lift_on' q f h) :=
quotient.lift.decidable_pred _ _ q
instance (q₁ : quotient s₁) (q₂ : quotient s₂) (f : α → β → Prop)
(h : ∀ a₁ b₁ a₂ b₂, @setoid.r α s₁ a₁ a₂ → @setoid.r β s₂ b₁ b₂ → f a₁ b₁ = f a₂ b₂)
[Π a, decidable_pred (f a)] :
decidable (quotient.lift_on₂' q₁ q₂ f h) :=
quotient.lift₂.decidable_pred _ _ _ _
end quotient
|
6725a8c28dc4380c5cd01cc06634355e17193799 | 12dabd587ce2621d9a4eff9f16e354d02e206c8e | /world08/level12.lean | c3e0a96a3282ac3274b493448b7202772725f222 | [] | no_license | abdelq/natural-number-game | a1b5b8f1d52625a7addcefc97c966d3f06a48263 | bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2 | refs/heads/master | 1,668,606,478,691 | 1,594,175,058,000 | 1,594,175,058,000 | 278,673,209 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 87 | lean | theorem add_one_eq_succ (d : mynat) : d + 1 = succ d :=
begin
rwa succ_eq_add_one,
end
|
46794d403fd930aa993a9c72bc46fafdf9b94092 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/tactic/monotonicity/interactive.lean | 818356f944723b2d85e5a3a2fcf5a347ed720113 | [] | 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,373 | lean | /-
Copyright (c) 2019 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Simon Hudon
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.tactic.monotonicity.basic
import Mathlib.control.traversable.default
import Mathlib.control.traversable.derive
import Mathlib.Lean3Lib.data.dlist
import Mathlib.PostPort
universes u v u_1 u_2 l
namespace Mathlib
namespace tactic.interactive
/--
`(prefix,left,right,suffix) ← match_assoc unif l r` finds the
longest prefix and suffix common to `l` and `r` and
returns them along with the differences -/
def apply_rel {α : Sort u} (R : α → α → Sort v) {x : α} {y : α} (x' : α) (y' : α) (h : R x y) (hx : x = x') (hy : y = y') : R x' y' :=
eq.mpr sorry (eq.mpr sorry h)
/-- tactic-facing function, similar to `interactive.tactic.generalize` with the
exception that meta variables -/
def list.minimum_on {α : Type u_1} {β : Type u_2} [linear_order β] (f : α → β) : List α → List α :=
sorry
/--
- `mono` applies a monotonicity rule.
- `mono*` applies monotonicity rules repetitively.
- `mono with x ≤ y` or `mono with [0 ≤ x,0 ≤ y]` creates an assertion for the listed
propositions. Those help to select the right monotonicity rule.
- `mono left` or `mono right` is useful when proving strict orderings:
for `x + y < w + z` could be broken down into either
- left: `x ≤ w` and `y < z` or
- right: `x < w` and `y ≤ z`
- `mono using [rule1,rule2]` calls `simp [rule1,rule2]` before applying mono.
- The general syntax is `mono '*'? ('with' hyp | 'with' [hyp1,hyp2])? ('using' [hyp1,hyp2])? mono_cfg?
To use it, first import `tactic.monotonicity`.
Here is an example of mono:
```lean
example (x y z k : ℤ)
(h : 3 ≤ (4 : ℤ))
(h' : z ≤ y) :
(k + 3 + x) - y ≤ (k + 4 + x) - z :=
begin
mono, -- unfold `(-)`, apply add_le_add
{ -- ⊢ k + 3 + x ≤ k + 4 + x
mono, -- apply add_le_add, refl
-- ⊢ k + 3 ≤ k + 4
mono },
{ -- ⊢ -y ≤ -z
mono /- apply neg_le_neg -/ }
end
```
More succinctly, we can prove the same goal as:
```lean
example (x y z k : ℤ)
(h : 3 ≤ (4 : ℤ))
(h' : z ≤ y) :
(k + 3 + x) - y ≤ (k + 4 + x) - z :=
by mono*
```
-/
/--
transforms a goal of the form `f x ≼ f y` into `x ≤ y` using lemmas
marked as `monotonic`.
Special care is taken when `f` is the repeated application of an
associative operator and if the operator is commutative
-/
/-- (repeat_until_or_at_most n t u): repeat tactic `t` at most n times or until u succeeds -/
inductive rep_arity
where
| one : rep_arity
| exactly : ℕ → rep_arity
| many : rep_arity
/--
`ac_mono` reduces the `f x ⊑ f y`, for some relation `⊑` and a
monotonic function `f` to `x ≺ y`.
`ac_mono*` unwraps monotonic functions until it can't.
`ac_mono^k`, for some literal number `k` applies monotonicity `k`
times.
`ac_mono h`, with `h` a hypothesis, unwraps monotonic functions and
uses `h` to solve the remaining goal. Can be combined with `*` or `^k`:
`ac_mono* h`
`ac_mono : p` asserts `p` and uses it to discharge the goal result
unwrapping a series of monotonic functions. Can be combined with * or
^k: `ac_mono* : p`
In the case where `f` is an associative or commutative operator,
`ac_mono` will consider any possible permutation of its arguments and
use the one the minimizes the difference between the left-hand side
and the right-hand side.
To use it, first import `tactic.monotonicity`.
`ac_mono` can be used as follows:
```lean
example (x y z k m n : ℕ)
(h₀ : z ≥ 0)
(h₁ : x ≤ y) :
(m + x + n) * z + k ≤ z * (y + n + m) + k :=
begin
ac_mono,
-- ⊢ (m + x + n) * z ≤ z * (y + n + m)
ac_mono,
-- ⊢ m + x + n ≤ y + n + m
ac_mono,
end
```
As with `mono*`, `ac_mono*` solves the goal in one go and so does
`ac_mono* h₁`. The latter syntax becomes especially interesting in the
following example:
```lean
example (x y z k m n : ℕ)
(h₀ : z ≥ 0)
(h₁ : m + x + n ≤ y + n + m) :
(m + x + n) * z + k ≤ z * (y + n + m) + k :=
by ac_mono* h₁.
```
By giving `ac_mono` the assumption `h₁`, we are asking `ac_refl` to
stop earlier than it would normally would.
-/
/-
TODO(Simon): with `ac_mono h` and `ac_mono : p` split the remaining
gaol if the provided rule does not solve it completely.
-/
|
23e921ec01401b9769806f55da089a1631970f59 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /library/data/rat/countable.lean | 1fada522d0c454566731dd0250f6ae3e1ad0c3fc | [
"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 | 1,393 | 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
-/
import data.rat.basic data.countable data.encodable data.int.countable
open encodable nat int option quot
namespace rat
definition encode_prerat : prerat → nat
| (prerat.mk n d h) := mkpair (encode n) (encode d)
definition decode_prerat (a : nat) : option prerat :=
match unpair a with
| (cn, cd) :=
match decode int cn with
| some n :=
match decode int cd with
| some d := if h : d > 0 then some (prerat.mk n d h) else none
| none := none
end
| none := none
end
end
lemma decode_encode_prerat (p : prerat) : decode_prerat (encode_prerat p) = some p :=
begin
cases p with n d h, unfold [encode_prerat, decode_prerat],
rewrite unpair_mkpair, esimp, rewrite [*encodek, dif_pos h]
end
definition encodable_prerat [instance] : encodable prerat :=
encodable.mk
encode_prerat
decode_prerat
decode_encode_prerat
definition decidable_equiv [instance] : ∀ a b : prerat, decidable (prerat.equiv a b)
| (prerat.mk n₁ d₁ h₁) (prerat.mk n₂ d₂ h₂) := begin unfold prerat.equiv, exact _ end
definition encodable_rat [instance] : encodable rat :=
@encodable_quot _ _ decidable_equiv encodable_prerat
lemma countable_rat : countable rat :=
countable_of_encodable encodable_rat
end rat
|
e11abe22eec5049e8441385ca2af7abc468be565 | 626e312b5c1cb2d88fca108f5933076012633192 | /src/linear_algebra/linear_independent.lean | 84327f00adb4993a0bb3615dad23b3cbfb3f9ca3 | [
"Apache-2.0"
] | permissive | Bioye97/mathlib | 9db2f9ee54418d29dd06996279ba9dc874fd6beb | 782a20a27ee83b523f801ff34efb1a9557085019 | refs/heads/master | 1,690,305,956,488 | 1,631,067,774,000 | 1,631,067,774,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 56,473 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Alexander Bentkamp, Anne Baanen
-/
import linear_algebra.finsupp
import linear_algebra.prod
import data.equiv.fin
import set_theory.cardinal
/-!
# Linear independence
This file defines linear independence in a module or vector space.
It is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light.
We define `linear_independent R v` as `ker (finsupp.total ι M R v) = ⊥`. Here `finsupp.total` is the
linear map sending a function `f : ι →₀ R` with finite support to the linear combination of vectors
from `v` with these coefficients. Then we prove that several other statements are equivalent to this
one, including injectivity of `finsupp.total ι M R v` and some versions with explicitly written
linear combinations.
## Main definitions
All definitions are given for families of vectors, i.e. `v : ι → M` where `M` is the module or
vector space and `ι : Type*` is an arbitrary indexing type.
* `linear_independent R v` states that the elements of the family `v` are linearly independent.
* `linear_independent.repr hv x` returns the linear combination representing `x : span R (range v)`
on the linearly independent vectors `v`, given `hv : linear_independent R v`
(using classical choice). `linear_independent.repr hv` is provided as a linear map.
## Main statements
We prove several specialized tests for linear independence of families of vectors and of sets of
vectors.
* `fintype.linear_independent_iff`: if `ι` is a finite type, then any function `f : ι → R` has
finite support, so we can reformulate the statement using `∑ i : ι, f i • v i` instead of a sum
over an auxiliary `s : finset ι`;
* `linear_independent_empty_type`: a family indexed by an empty type is linearly independent;
* `linear_independent_unique_iff`: if `ι` is a singleton, then `linear_independent K v` is
equivalent to `v (default ι) ≠ 0`;
* linear_independent_option`, `linear_independent_sum`, `linear_independent_fin_cons`,
`linear_independent_fin_succ`: type-specific tests for linear independence of families of vector
fields;
* `linear_independent_insert`, `linear_independent_union`, `linear_independent_pair`,
`linear_independent_singleton`: linear independence tests for set operations.
In many cases we additionally provide dot-style operations (e.g., `linear_independent.union`) to
make the linear independence tests usable as `hv.insert ha` etc.
We also prove that, when working over a field,
any family of vectors includes a linear independent subfamily spanning the same subspace.
## Implementation notes
We use families instead of sets because it allows us to say that two identical vectors are linearly
dependent.
If you want to use sets, use the family `(λ x, x : s → M)` given a set `s : set M`. The lemmas
`linear_independent.to_subtype_range` and `linear_independent.of_subtype_range` connect those two
worlds.
## Tags
linearly dependent, linear dependence, linearly independent, linear independence
-/
noncomputable theory
open function set submodule
open_locale classical big_operators
universes u
variables {ι : Type*} {ι' : Type*} {R : Type*} {K : Type*}
variables {M : Type*} {M' M'' : Type*} {V : Type u} {V' : Type*}
section module
variables {v : ι → M}
variables [semiring R] [add_comm_monoid M] [add_comm_monoid M'] [add_comm_monoid M'']
variables [module R M] [module R M'] [module R M'']
variables {a b : R} {x y : M}
variables (R) (v)
/-- `linear_independent R v` states the family of vectors `v` is linearly independent over `R`. -/
def linear_independent : Prop := (finsupp.total ι M R v).ker = ⊥
variables {R} {v}
theorem linear_independent_iff : linear_independent R v ↔
∀l, finsupp.total ι M R v l = 0 → l = 0 :=
by simp [linear_independent, linear_map.ker_eq_bot']
theorem linear_independent_iff' : linear_independent R v ↔
∀ s : finset ι, ∀ g : ι → R, ∑ i in s, g i • v i = 0 → ∀ i ∈ s, g i = 0 :=
linear_independent_iff.trans
⟨λ hf s g hg i his, have h : _ := hf (∑ i in s, finsupp.single i (g i)) $
by simpa only [linear_map.map_sum, finsupp.total_single] using hg, calc
g i = (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single i (g i)) :
by rw [finsupp.lapply_apply, finsupp.single_eq_same]
... = ∑ j in s, (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single j (g j)) :
eq.symm $ finset.sum_eq_single i
(λ j hjs hji, by rw [finsupp.lapply_apply, finsupp.single_eq_of_ne hji])
(λ hnis, hnis.elim his)
... = (∑ j in s, finsupp.single j (g j)) i : (finsupp.lapply i : (ι →₀ R) →ₗ[R] R).map_sum.symm
... = 0 : finsupp.ext_iff.1 h i,
λ hf l hl, finsupp.ext $ λ i, classical.by_contradiction $ λ hni, hni $ hf _ _ hl _ $
finsupp.mem_support_iff.2 hni⟩
theorem linear_independent_iff'' :
linear_independent R v ↔ ∀ (s : finset ι) (g : ι → R) (hg : ∀ i ∉ s, g i = 0),
∑ i in s, g i • v i = 0 → ∀ i, g i = 0 :=
linear_independent_iff'.trans ⟨λ H s g hg hv i, if his : i ∈ s then H s g hv i his else hg i his,
λ H s g hg i hi, by { convert H s (λ j, if j ∈ s then g j else 0) (λ j hj, if_neg hj)
(by simp_rw [ite_smul, zero_smul, finset.sum_extend_by_zero, hg]) i,
exact (if_pos hi).symm }⟩
theorem linear_dependent_iff : ¬ linear_independent R v ↔
∃ s : finset ι, ∃ g : ι → R, (∑ i in s, g i • v i) = 0 ∧ (∃ i ∈ s, g i ≠ 0) :=
begin
rw linear_independent_iff',
simp only [exists_prop, not_forall],
end
theorem fintype.linear_independent_iff [fintype ι] :
linear_independent R v ↔ ∀ g : ι → R, ∑ i, g i • v i = 0 → ∀ i, g i = 0 :=
begin
refine ⟨λ H g, by simpa using linear_independent_iff'.1 H finset.univ g,
λ H, linear_independent_iff''.2 $ λ s g hg hs i, H _ _ _⟩,
rw ← hs,
refine (finset.sum_subset (finset.subset_univ _) (λ i _ hi, _)).symm,
rw [hg i hi, zero_smul]
end
/-- A finite family of vectors `v i` is linear independent iff the linear map that sends
`c : ι → R` to `∑ i, c i • v i` has the trivial kernel. -/
theorem fintype.linear_independent_iff' [fintype ι] :
linear_independent R v ↔
(linear_map.lsum R (λ i : ι, R) ℕ (λ i, linear_map.id.smul_right (v i))).ker = ⊥ :=
by simp [fintype.linear_independent_iff, linear_map.ker_eq_bot', funext_iff]
lemma linear_independent_empty_type [is_empty ι] : linear_independent R v :=
linear_independent_iff.mpr $ λ v hv, subsingleton.elim v 0
lemma linear_independent.ne_zero [nontrivial R]
(i : ι) (hv : linear_independent R v) : v i ≠ 0 :=
λ h, @zero_ne_one R _ _ $ eq.symm begin
suffices : (finsupp.single i 1 : ι →₀ R) i = 0, {simpa},
rw linear_independent_iff.1 hv (finsupp.single i 1),
{ simp },
{ simp [h] }
end
/-- A subfamily of a linearly independent family (i.e., a composition with an injective map) is a
linearly independent family. -/
lemma linear_independent.comp
(h : linear_independent R v) (f : ι' → ι) (hf : injective f) : linear_independent R (v ∘ f) :=
begin
rw [linear_independent_iff, finsupp.total_comp],
intros l hl,
have h_map_domain : ∀ x, (finsupp.map_domain f l) (f x) = 0,
by rw linear_independent_iff.1 h (finsupp.map_domain f l) hl; simp,
ext x,
convert h_map_domain x,
rw [finsupp.map_domain_apply hf]
end
lemma linear_independent.coe_range (i : linear_independent R v) :
linear_independent R (coe : range v → M) :=
by simpa using i.comp _ (range_splitting_injective v)
/-- If `v` is a linearly independent family of vectors and the kernel of a linear map `f` is
disjoint with the submodule spanned by the vectors of `v`, then `f ∘ v` is a linearly independent
family of vectors. See also `linear_independent.map'` for a special case assuming `ker f = ⊥`. -/
lemma linear_independent.map (hv : linear_independent R v) {f : M →ₗ[R] M'}
(hf_inj : disjoint (span R (range v)) f.ker) : linear_independent R (f ∘ v) :=
begin
rw [disjoint, ← set.image_univ, finsupp.span_image_eq_map_total, map_inf_eq_map_inf_comap,
map_le_iff_le_comap, comap_bot, finsupp.supported_univ, top_inf_eq] at hf_inj,
unfold linear_independent at hv ⊢,
rw [hv, le_bot_iff] at hf_inj,
haveI : inhabited M := ⟨0⟩,
rw [finsupp.total_comp, @finsupp.lmap_domain_total _ _ R _ _ _ _ _ _ _ _ _ _ f,
linear_map.ker_comp, hf_inj],
exact λ _, rfl,
end
/-- An injective linear map sends linearly independent families of vectors to linearly independent
families of vectors. See also `linear_independent.map` for a more general statement. -/
lemma linear_independent.map' (hv : linear_independent R v) (f : M →ₗ[R] M')
(hf_inj : f.ker = ⊥) : linear_independent R (f ∘ v) :=
hv.map $ by simp [hf_inj]
/-- If the image of a family of vectors under a linear map is linearly independent, then so is
the original family. -/
lemma linear_independent.of_comp (f : M →ₗ[R] M') (hfv : linear_independent R (f ∘ v)) :
linear_independent R v :=
linear_independent_iff'.2 $ λ s g hg i his,
have ∑ (i : ι) in s, g i • f (v i) = 0,
by simp_rw [← f.map_smul, ← f.map_sum, hg, f.map_zero],
linear_independent_iff'.1 hfv s g this i his
/-- If `f` is an injective linear map, then the family `f ∘ v` is linearly independent
if and only if the family `v` is linearly independent. -/
protected lemma linear_map.linear_independent_iff (f : M →ₗ[R] M') (hf_inj : f.ker = ⊥) :
linear_independent R (f ∘ v) ↔ linear_independent R v :=
⟨λ h, h.of_comp f, λ h, h.map $ by simp only [hf_inj, disjoint_bot_right]⟩
@[nontriviality]
lemma linear_independent_of_subsingleton [subsingleton R] : linear_independent R v :=
linear_independent_iff.2 (λ l hl, subsingleton.elim _ _)
theorem linear_independent_equiv (e : ι ≃ ι') {f : ι' → M} :
linear_independent R (f ∘ e) ↔ linear_independent R f :=
⟨λ h, function.comp.right_id f ▸ e.self_comp_symm ▸ h.comp _ e.symm.injective,
λ h, h.comp _ e.injective⟩
theorem linear_independent_equiv' (e : ι ≃ ι') {f : ι' → M} {g : ι → M} (h : f ∘ e = g) :
linear_independent R g ↔ linear_independent R f :=
h ▸ linear_independent_equiv e
theorem linear_independent_subtype_range {ι} {f : ι → M} (hf : injective f) :
linear_independent R (coe : range f → M) ↔ linear_independent R f :=
iff.symm $ linear_independent_equiv' (equiv.of_injective f hf) rfl
alias linear_independent_subtype_range ↔ linear_independent.of_subtype_range _
theorem linear_independent_image {ι} {s : set ι} {f : ι → M} (hf : set.inj_on f s) :
linear_independent R (λ x : s, f x) ↔ linear_independent R (λ x : f '' s, (x : M)) :=
linear_independent_equiv' (equiv.set.image_of_inj_on _ _ hf) rfl
lemma linear_independent_span (hs : linear_independent R v) :
@linear_independent ι R (span R (range v))
(λ i : ι, ⟨v i, subset_span (mem_range_self i)⟩) _ _ _ :=
linear_independent.of_comp (span R (range v)).subtype hs
/-- See `linear_independent.fin_cons` for a family of elements in a vector space. -/
lemma linear_independent.fin_cons' {m : ℕ} (x : M) (v : fin m → M)
(hli : linear_independent R v)
(x_ortho : (∀ (c : R) (y : submodule.span R (set.range v)), c • x + y = (0 : M) → c = 0)) :
linear_independent R (fin.cons x v : fin m.succ → M) :=
begin
rw fintype.linear_independent_iff at hli ⊢,
rintros g total_eq j,
have zero_not_mem : (0 : fin m.succ) ∉ finset.univ.image (fin.succ : fin m → fin m.succ),
{ rw finset.mem_image,
rintro ⟨x, hx, succ_eq⟩,
exact fin.succ_ne_zero _ succ_eq },
simp only [submodule.coe_mk, fin.univ_succ, finset.sum_insert zero_not_mem,
fin.cons_zero, fin.cons_succ,
forall_true_iff, imp_self, fin.succ_inj, finset.sum_image] at total_eq,
have : g 0 = 0,
{ refine x_ortho (g 0) ⟨∑ (i : fin m), g i.succ • v i, _⟩ total_eq,
exact sum_mem _ (λ i _, smul_mem _ _ (subset_span ⟨i, rfl⟩)) },
refine fin.cases this (λ j, _) j,
apply hli (λ i, g i.succ),
simpa only [this, zero_smul, zero_add] using total_eq
end
/-- A set of linearly independent vectors in a module `M` over a semiring `K` is also linearly
independent over a subring `R` of `K`.
The implementation uses minimal assumptions about the relationship between `R`, `K` and `M`.
The version where `K` is an `R`-algebra is `linear_independent.restrict_scalars_algebras`.
-/
lemma linear_independent.restrict_scalars [semiring K] [smul_with_zero R K] [module K M]
[is_scalar_tower R K M]
(hinj : function.injective (λ r : R, r • (1 : K))) (li : linear_independent K v) :
linear_independent R v :=
begin
refine linear_independent_iff'.mpr (λ s g hg i hi, hinj (eq.trans _ (zero_smul _ _).symm)),
refine (linear_independent_iff'.mp li : _) _ _ _ i hi,
simp_rw [smul_assoc, one_smul],
exact hg,
end
/-- Every finite subset of a linearly independent set is linearly independent. -/
lemma linear_independent_finset_map_embedding_subtype
(s : set M) (li : linear_independent R (coe : s → M)) (t : finset s) :
linear_independent R (coe : (finset.map (embedding.subtype s) t) → M) :=
begin
let f : t.map (embedding.subtype s) → s := λ x, ⟨x.1, begin
obtain ⟨x, h⟩ := x,
rw [finset.mem_map] at h,
obtain ⟨a, ha, rfl⟩ := h,
simp only [subtype.coe_prop, embedding.coe_subtype],
end⟩,
convert linear_independent.comp li f _,
rintros ⟨x, hx⟩ ⟨y, hy⟩,
rw [finset.mem_map] at hx hy,
obtain ⟨a, ha, rfl⟩ := hx,
obtain ⟨b, hb, rfl⟩ := hy,
simp only [imp_self, subtype.mk_eq_mk],
end
/--
If every finite set of linearly independent vectors has cardinality at most `n`,
then the same is true for arbitrary sets of linearly independent vectors.
-/
lemma linear_independent_bounded_of_finset_linear_independent_bounded {n : ℕ}
(H : ∀ s : finset M, linear_independent R (λ i : s, (i : M)) → s.card ≤ n) :
∀ s : set M, linear_independent R (coe : s → M) → cardinal.mk s ≤ n :=
begin
intros s li,
apply cardinal.card_le_of,
intro t,
rw ← finset.card_map (embedding.subtype s),
apply H,
apply linear_independent_finset_map_embedding_subtype _ li,
end
section subtype
/-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/
theorem linear_independent_comp_subtype {s : set ι} :
linear_independent R (v ∘ coe : s → M) ↔
∀ l ∈ (finsupp.supported R R s), (finsupp.total ι M R v) l = 0 → l = 0 :=
begin
simp only [linear_independent_iff, (∘), finsupp.mem_supported, finsupp.total_apply,
set.subset_def, finset.mem_coe],
split,
{ intros h l hl₁ hl₂,
have := h (l.subtype_domain s) ((finsupp.sum_subtype_domain_index hl₁).trans hl₂),
exact (finsupp.subtype_domain_eq_zero_iff hl₁).1 this },
{ intros h l hl,
refine finsupp.emb_domain_eq_zero.1 (h (l.emb_domain $ function.embedding.subtype s) _ _),
{ suffices : ∀ i hi, ¬l ⟨i, hi⟩ = 0 → i ∈ s, by simpa,
intros, assumption },
{ rwa [finsupp.emb_domain_eq_map_domain, finsupp.sum_map_domain_index],
exacts [λ _, zero_smul _ _, λ _ _ _, add_smul _ _ _] } }
end
lemma linear_dependent_comp_subtype' {s : set ι} :
¬ linear_independent R (v ∘ coe : s → M) ↔
∃ f : ι →₀ R, f ∈ finsupp.supported R R s ∧ finsupp.total ι M R v f = 0 ∧ f ≠ 0 :=
by simp [linear_independent_comp_subtype]
/-- A version of `linear_dependent_comp_subtype'` with `finsupp.total` unfolded. -/
lemma linear_dependent_comp_subtype {s : set ι} :
¬ linear_independent R (v ∘ coe : s → M) ↔
∃ f : ι →₀ R, f ∈ finsupp.supported R R s ∧ ∑ i in f.support, f i • v i = 0 ∧ f ≠ 0 :=
linear_dependent_comp_subtype'
theorem linear_independent_subtype {s : set M} :
linear_independent R (λ x, x : s → M) ↔
∀ l ∈ (finsupp.supported R R s), (finsupp.total M M R id) l = 0 → l = 0 :=
by apply @linear_independent_comp_subtype _ _ _ id
theorem linear_independent_comp_subtype_disjoint {s : set ι} :
linear_independent R (v ∘ coe : s → M) ↔
disjoint (finsupp.supported R R s) (finsupp.total ι M R v).ker :=
by rw [linear_independent_comp_subtype, linear_map.disjoint_ker]
theorem linear_independent_subtype_disjoint {s : set M} :
linear_independent R (λ x, x : s → M) ↔
disjoint (finsupp.supported R R s) (finsupp.total M M R id).ker :=
by apply @linear_independent_comp_subtype_disjoint _ _ _ id
theorem linear_independent_iff_total_on {s : set M} :
linear_independent R (λ x, x : s → M) ↔ (finsupp.total_on M M R id s).ker = ⊥ :=
by rw [finsupp.total_on, linear_map.ker, linear_map.comap_cod_restrict, map_bot, comap_bot,
linear_map.ker_comp, linear_independent_subtype_disjoint, disjoint, ← map_comap_subtype,
map_le_iff_le_comap, comap_bot, ker_subtype, le_bot_iff]
lemma linear_independent.restrict_of_comp_subtype {s : set ι}
(hs : linear_independent R (v ∘ coe : s → M)) :
linear_independent R (s.restrict v) :=
hs
variables (R M)
lemma linear_independent_empty : linear_independent R (λ x, x : (∅ : set M) → M) :=
by simp [linear_independent_subtype_disjoint]
variables {R M}
lemma linear_independent.mono {t s : set M} (h : t ⊆ s) :
linear_independent R (λ x, x : s → M) → linear_independent R (λ x, x : t → M) :=
begin
simp only [linear_independent_subtype_disjoint],
exact (disjoint.mono_left (finsupp.supported_mono h))
end
lemma linear_independent_of_finite (s : set M)
(H : ∀ t ⊆ s, finite t → linear_independent R (λ x, x : t → M)) :
linear_independent R (λ x, x : s → M) :=
linear_independent_subtype.2 $
λ l hl, linear_independent_subtype.1 (H _ hl (finset.finite_to_set _)) l (subset.refl _)
lemma linear_independent_Union_of_directed {η : Type*}
{s : η → set M} (hs : directed (⊆) s)
(h : ∀ i, linear_independent R (λ x, x : s i → M)) :
linear_independent R (λ x, x : (⋃ i, s i) → M) :=
begin
by_cases hη : nonempty η,
{ resetI,
refine linear_independent_of_finite (⋃ i, s i) (λ t ht ft, _),
rcases finite_subset_Union ft ht with ⟨I, fi, hI⟩,
rcases hs.finset_le fi.to_finset with ⟨i, hi⟩,
exact (h i).mono (subset.trans hI $ bUnion_subset $
λ j hj, hi j (fi.mem_to_finset.2 hj)) },
{ refine (linear_independent_empty _ _).mono _,
rintro _ ⟨_, ⟨i, _⟩, _⟩, exact hη ⟨i⟩ }
end
lemma linear_independent_sUnion_of_directed {s : set (set M)}
(hs : directed_on (⊆) s)
(h : ∀ a ∈ s, linear_independent R (λ x, x : (a : set M) → M)) :
linear_independent R (λ x, x : (⋃₀ s) → M) :=
by rw sUnion_eq_Union; exact
linear_independent_Union_of_directed hs.directed_coe (by simpa using h)
lemma linear_independent_bUnion_of_directed {η} {s : set η} {t : η → set M}
(hs : directed_on (t ⁻¹'o (⊆)) s) (h : ∀a∈s, linear_independent R (λ x, x : t a → M)) :
linear_independent R (λ x, x : (⋃a∈s, t a) → M) :=
by rw bUnion_eq_Union; exact
linear_independent_Union_of_directed (directed_comp.2 $ hs.directed_coe) (by simpa using h)
end subtype
end module
/-! ### Properties which require `ring R` -/
section module
variables {v : ι → M}
variables [ring R] [add_comm_group M] [add_comm_group M'] [add_comm_group M'']
variables [module R M] [module R M'] [module R M'']
variables {a b : R} {x y : M}
theorem linear_independent_iff_injective_total : linear_independent R v ↔
function.injective (finsupp.total ι M R v) :=
linear_independent_iff.trans (finsupp.total ι M R v).to_add_monoid_hom.injective_iff.symm
alias linear_independent_iff_injective_total ↔ linear_independent.injective_total _
lemma linear_independent.injective [nontrivial R] (hv : linear_independent R v) :
injective v :=
begin
intros i j hij,
let l : ι →₀ R := finsupp.single i (1 : R) - finsupp.single j 1,
have h_total : finsupp.total ι M R v l = 0,
{ simp_rw [linear_map.map_sub, finsupp.total_apply],
simp [hij] },
have h_single_eq : finsupp.single i (1 : R) = finsupp.single j 1,
{ rw linear_independent_iff at hv,
simp [eq_add_of_sub_eq' (hv l h_total)] },
simpa [finsupp.single_eq_single_iff] using h_single_eq
end
theorem linear_independent.to_subtype_range {ι} {f : ι → M} (hf : linear_independent R f) :
linear_independent R (coe : range f → M) :=
begin
nontriviality R,
exact (linear_independent_subtype_range hf.injective).2 hf
end
theorem linear_independent.to_subtype_range' {ι} {f : ι → M} (hf : linear_independent R f)
{t} (ht : range f = t) :
linear_independent R (coe : t → M) :=
ht ▸ hf.to_subtype_range
theorem linear_independent.image_of_comp {ι ι'} (s : set ι) (f : ι → ι') (g : ι' → M)
(hs : linear_independent R (λ x : s, g (f x))) :
linear_independent R (λ x : f '' s, g x) :=
begin
nontriviality R,
have : inj_on f s, from inj_on_iff_injective.2 hs.injective.of_comp,
exact (linear_independent_equiv' (equiv.set.image_of_inj_on f s this) rfl).1 hs
end
theorem linear_independent.image {ι} {s : set ι} {f : ι → M}
(hs : linear_independent R (λ x : s, f x)) : linear_independent R (λ x : f '' s, (x : M)) :=
by convert linear_independent.image_of_comp s f id hs
lemma linear_independent.group_smul
{G : Type*} [hG : group G] [distrib_mul_action G R] [distrib_mul_action G M]
[is_scalar_tower G R M] [smul_comm_class G R M] {v : ι → M} (hv : linear_independent R v)
(w : ι → G) : linear_independent R (w • v) :=
begin
rw linear_independent_iff'' at hv ⊢,
intros s g hgs hsum i,
refine (smul_eq_zero_iff_eq (w i)).1 _,
refine hv s (λ i, w i • g i) (λ i hi, _) _ i,
{ dsimp only,
exact (hgs i hi).symm ▸ smul_zero _ },
{ rw [← hsum, finset.sum_congr rfl _],
intros, erw [pi.smul_apply, smul_assoc, smul_comm] },
end
-- This lemma cannot be proved with `linear_independent.group_smul` since the action of
-- `units R` on `R` is not commutative.
lemma linear_independent.units_smul {v : ι → M} (hv : linear_independent R v)
(w : ι → units R) : linear_independent R (w • v) :=
begin
rw linear_independent_iff'' at hv ⊢,
intros s g hgs hsum i,
rw ← (w i).mul_left_eq_zero,
refine hv s (λ i, g i • w i) (λ i hi, _) _ i,
{ dsimp only,
exact (hgs i hi).symm ▸ zero_smul _ _ },
{ rw [← hsum, finset.sum_congr rfl _],
intros,
erw [pi.smul_apply, smul_assoc],
refl }
end
section maximal
universes v w
/--
A linearly independent family is maximal if there is no strictly larger linearly independent family.
-/
@[nolint unused_arguments]
def linear_independent.maximal {ι : Type w} {R : Type u} [semiring R]
{M : Type v} [add_comm_monoid M] [module R M] {v : ι → M} (i : linear_independent R v) : Prop :=
∀ (s : set M) (i' : linear_independent R (coe : s → M)) (h : range v ≤ s), range v = s
/--
An alternative characterization of a maximal linearly independent family,
quantifying over types (in the same universe as `M`) into which the indexing family injects.
-/
lemma linear_independent.maximal_iff {ι : Type w} {R : Type u} [ring R] [nontrivial R]
{M : Type v} [add_comm_group M] [module R M] {v : ι → M} (i : linear_independent R v) :
i.maximal ↔ ∀ (κ : Type v) (w : κ → M) (i' : linear_independent R w)
(j : ι → κ) (h : w ∘ j = v), surjective j :=
begin
fsplit,
{ rintros p κ w i' j rfl,
specialize p (range w) i'.coe_range (range_comp_subset_range _ _),
rw [range_comp, ←@image_univ _ _ w] at p,
exact range_iff_surjective.mp (image_injective.mpr i'.injective p), },
{ intros p w i' h,
specialize p w (coe : w → M) i'
(λ i, ⟨v i, range_subset_iff.mp h i⟩)
(by { ext, simp, }),
have q := congr_arg (λ s, (coe : w → M) '' s) p.range_eq,
dsimp at q,
rw [←image_univ, image_image] at q,
simpa using q, },
end
end maximal
/-- Linear independent families are injective, even if you multiply either side. -/
lemma linear_independent.eq_of_smul_apply_eq_smul_apply {M : Type*} [add_comm_group M] [module R M]
{v : ι → M} (li : linear_independent R v) (c d : R) (i j : ι)
(hc : c ≠ 0) (h : c • v i = d • v j) : i = j :=
begin
let l : ι →₀ R := finsupp.single i c - finsupp.single j d,
have h_total : finsupp.total ι M R v l = 0,
{ simp_rw [linear_map.map_sub, finsupp.total_apply],
simp [h] },
have h_single_eq : finsupp.single i c = finsupp.single j d,
{ rw linear_independent_iff at li,
simp [eq_add_of_sub_eq' (li l h_total)] },
rcases (finsupp.single_eq_single_iff _ _ _ _).mp h_single_eq with ⟨this, _⟩ | ⟨hc, _⟩,
{ exact this },
{ contradiction },
end
section subtype
/-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/
lemma linear_independent.disjoint_span_image (hv : linear_independent R v) {s t : set ι}
(hs : disjoint s t) :
disjoint (submodule.span R $ v '' s) (submodule.span R $ v '' t) :=
begin
simp only [disjoint_def, finsupp.mem_span_image_iff_total],
rintros _ ⟨l₁, hl₁, rfl⟩ ⟨l₂, hl₂, H⟩,
rw [hv.injective_total.eq_iff] at H, subst l₂,
have : l₁ = 0 := finsupp.disjoint_supported_supported hs (submodule.mem_inf.2 ⟨hl₁, hl₂⟩),
simp [this]
end
lemma linear_independent.not_mem_span_image [nontrivial R] (hv : linear_independent R v) {s : set ι}
{x : ι} (h : x ∉ s) :
v x ∉ submodule.span R (v '' s) :=
begin
have h' : v x ∈ submodule.span R (v '' {x}),
{ rw set.image_singleton,
exact mem_span_singleton_self (v x), },
intro w,
apply linear_independent.ne_zero x hv,
refine disjoint_def.1 (hv.disjoint_span_image _) (v x) h' w,
simpa using h,
end
lemma linear_independent.total_ne_of_not_mem_support [nontrivial R] (hv : linear_independent R v)
{x : ι} (f : ι →₀ R) (h : x ∉ f.support) :
finsupp.total ι M R v f ≠ v x :=
begin
replace h : x ∉ (f.support : set ι) := h,
have p := hv.not_mem_span_image h,
intro w,
rw ←w at p,
rw finsupp.span_image_eq_map_total at p,
simp only [not_exists, not_and, mem_map] at p,
exact p f (f.mem_supported_support R) rfl,
end
lemma linear_independent_sum {v : ι ⊕ ι' → M} :
linear_independent R v ↔ linear_independent R (v ∘ sum.inl) ∧
linear_independent R (v ∘ sum.inr) ∧
disjoint (submodule.span R (range (v ∘ sum.inl))) (submodule.span R (range (v ∘ sum.inr))) :=
begin
rw [range_comp v, range_comp v],
refine ⟨λ h, ⟨h.comp _ sum.inl_injective, h.comp _ sum.inr_injective,
h.disjoint_span_image is_compl_range_inl_range_inr.1⟩, _⟩,
rintro ⟨hl, hr, hlr⟩,
rw [linear_independent_iff'] at *,
intros s g hg i hi,
have : ∑ i in s.preimage sum.inl (sum.inl_injective.inj_on _), (λ x, g x • v x) (sum.inl i) +
∑ i in s.preimage sum.inr (sum.inr_injective.inj_on _), (λ x, g x • v x) (sum.inr i) = 0,
{ rw [finset.sum_preimage', finset.sum_preimage', ← finset.sum_union, ← finset.filter_or],
{ simpa only [← mem_union, range_inl_union_range_inr, mem_univ, finset.filter_true] },
{ exact finset.disjoint_filter.2 (λ x hx, disjoint_left.1 is_compl_range_inl_range_inr.1) } },
{ rw ← eq_neg_iff_add_eq_zero at this,
rw [disjoint_def'] at hlr,
have A := hlr _ (sum_mem _ $ λ i hi, _) _ (neg_mem _ $ sum_mem _ $ λ i hi, _) this,
{ cases i with i i,
{ exact hl _ _ A i (finset.mem_preimage.2 hi) },
{ rw [this, neg_eq_zero] at A,
exact hr _ _ A i (finset.mem_preimage.2 hi) } },
{ exact smul_mem _ _ (subset_span ⟨sum.inl i, mem_range_self _, rfl⟩) },
{ exact smul_mem _ _ (subset_span ⟨sum.inr i, mem_range_self _, rfl⟩) } }
end
lemma linear_independent.sum_type {v' : ι' → M} (hv : linear_independent R v)
(hv' : linear_independent R v')
(h : disjoint (submodule.span R (range v)) (submodule.span R (range v'))) :
linear_independent R (sum.elim v v') :=
linear_independent_sum.2 ⟨hv, hv', h⟩
lemma linear_independent.union {s t : set M}
(hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M))
(hst : disjoint (span R s) (span R t)) :
linear_independent R (λ x, x : (s ∪ t) → M) :=
(hs.sum_type ht $ by simpa).to_subtype_range' $ by simp
lemma linear_independent_Union_finite_subtype {ι : Type*} {f : ι → set M}
(hl : ∀i, linear_independent R (λ x, x : f i → M))
(hd : ∀i, ∀t:set ι, finite t → i ∉ t → disjoint (span R (f i)) (⨆i∈t, span R (f i))) :
linear_independent R (λ x, x : (⋃i, f i) → M) :=
begin
rw [Union_eq_Union_finset f],
apply linear_independent_Union_of_directed,
{ apply directed_of_sup,
exact (λ t₁ t₂ ht, Union_subset_Union $ λ i, Union_subset_Union_const $ λ h, ht h) },
assume t,
induction t using finset.induction_on with i s his ih,
{ refine (linear_independent_empty _ _).mono _,
simp },
{ rw [finset.set_bUnion_insert],
refine (hl _).union ih _,
refine (hd i s s.finite_to_set his).mono_right _,
simp only [(span_Union _).symm],
refine span_mono (@supr_le_supr2 (set M) _ _ _ _ _ _),
exact λ i, ⟨i, le_rfl⟩ }
end
lemma linear_independent_Union_finite {η : Type*} {ιs : η → Type*}
{f : Π j : η, ιs j → M}
(hindep : ∀j, linear_independent R (f j))
(hd : ∀i, ∀t:set η, finite t → i ∉ t →
disjoint (span R (range (f i))) (⨆i∈t, span R (range (f i)))) :
linear_independent R (λ ji : Σ j, ιs j, f ji.1 ji.2) :=
begin
nontriviality R,
apply linear_independent.of_subtype_range,
{ rintros ⟨x₁, x₂⟩ ⟨y₁, y₂⟩ hxy,
by_cases h_cases : x₁ = y₁,
subst h_cases,
{ apply sigma.eq,
rw linear_independent.injective (hindep _) hxy,
refl },
{ have h0 : f x₁ x₂ = 0,
{ apply disjoint_def.1 (hd x₁ {y₁} (finite_singleton y₁)
(λ h, h_cases (eq_of_mem_singleton h))) (f x₁ x₂) (subset_span (mem_range_self _)),
rw supr_singleton,
simp only at hxy,
rw hxy,
exact (subset_span (mem_range_self y₂)) },
exact false.elim ((hindep x₁).ne_zero _ h0) } },
rw range_sigma_eq_Union_range,
apply linear_independent_Union_finite_subtype (λ j, (hindep j).to_subtype_range) hd,
end
end subtype
section repr
variables (hv : linear_independent R v)
/-- Canonical isomorphism between linear combinations and the span of linearly independent vectors.
-/
@[simps] def linear_independent.total_equiv (hv : linear_independent R v) :
(ι →₀ R) ≃ₗ[R] span R (range v) :=
begin
apply linear_equiv.of_bijective
(linear_map.cod_restrict (span R (range v)) (finsupp.total ι M R v) _),
{ rw linear_map.ker_cod_restrict,
apply hv },
{ rw [linear_map.range_eq_map, linear_map.map_cod_restrict, ← linear_map.range_le_iff_comap,
range_subtype, map_top],
rw finsupp.range_total,
apply le_refl (span R (range v)) },
{ intro l,
rw ← finsupp.range_total,
rw linear_map.mem_range,
apply mem_range_self l }
end
/-- Linear combination representing a vector in the span of linearly independent vectors.
Given a family of linearly independent vectors, we can represent any vector in their span as
a linear combination of these vectors. These are provided by this linear map.
It is simply one direction of `linear_independent.total_equiv`. -/
def linear_independent.repr (hv : linear_independent R v) :
span R (range v) →ₗ[R] ι →₀ R := hv.total_equiv.symm
@[simp] lemma linear_independent.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x :=
subtype.ext_iff.1 (linear_equiv.apply_symm_apply hv.total_equiv x)
lemma linear_independent.total_comp_repr :
(finsupp.total ι M R v).comp hv.repr = submodule.subtype _ :=
linear_map.ext $ hv.total_repr
lemma linear_independent.repr_ker : hv.repr.ker = ⊥ :=
by rw [linear_independent.repr, linear_equiv.ker]
lemma linear_independent.repr_range : hv.repr.range = ⊤ :=
by rw [linear_independent.repr, linear_equiv.range]
lemma linear_independent.repr_eq
{l : ι →₀ R} {x} (eq : finsupp.total ι M R v l = ↑x) :
hv.repr x = l :=
begin
have : ↑((linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l)
= finsupp.total ι M R v l := rfl,
have : (linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l = x,
{ rw eq at this,
exact subtype.ext_iff.2 this },
rw ←linear_equiv.symm_apply_apply hv.total_equiv l,
rw ←this,
refl,
end
lemma linear_independent.repr_eq_single (i) (x) (hx : ↑x = v i) :
hv.repr x = finsupp.single i 1 :=
begin
apply hv.repr_eq,
simp [finsupp.total_single, hx]
end
lemma linear_independent.span_repr_eq [nontrivial R] (x) :
span.repr R (set.range v) x = (hv.repr x).equiv_map_domain (equiv.of_injective _ hv.injective) :=
begin
have p : (span.repr R (set.range v) x).equiv_map_domain (equiv.of_injective _ hv.injective).symm =
hv.repr x,
{ apply (linear_independent.total_equiv hv).injective,
ext,
simp, },
ext ⟨_, ⟨i, rfl⟩⟩,
simp [←p],
end
-- TODO: why is this so slow?
lemma linear_independent_iff_not_smul_mem_span :
linear_independent R v ↔ (∀ (i : ι) (a : R), a • (v i) ∈ span R (v '' (univ \ {i})) → a = 0) :=
⟨ λ hv i a ha, begin
rw [finsupp.span_image_eq_map_total, mem_map] at ha,
rcases ha with ⟨l, hl, e⟩,
rw sub_eq_zero.1 (linear_independent_iff.1 hv (l - finsupp.single i a) (by simp [e])) at hl,
by_contra hn,
exact (not_mem_of_mem_diff (hl $ by simp [hn])) (mem_singleton _),
end, λ H, linear_independent_iff.2 $ λ l hl, begin
ext i, simp only [finsupp.zero_apply],
by_contra hn,
refine hn (H i _ _),
refine (finsupp.mem_span_image_iff_total _).2 ⟨finsupp.single i (l i) - l, _, _⟩,
{ rw finsupp.mem_supported',
intros j hj,
have hij : j = i :=
not_not.1
(λ hij : j ≠ i, hj ((mem_diff _).2 ⟨mem_univ _, λ h, hij (eq_of_mem_singleton h)⟩)),
simp [hij] },
{ simp [hl] }
end⟩
variable (R)
lemma exists_maximal_independent' (s : ι → M) :
∃ I : set ι, linear_independent R (λ x : I, s x) ∧
∀ J : set ι, I ⊆ J → linear_independent R (λ x : J, s x) → I = J :=
begin
let indep : set ι → Prop := λ I, linear_independent R (s ∘ coe : I → M),
let X := { I : set ι // indep I },
let r : X → X → Prop := λ I J, I.1 ⊆ J.1,
have key : ∀ c : set X, zorn.chain r c → indep (⋃ (I : X) (H : I ∈ c), I),
{ intros c hc,
dsimp [indep],
rw [linear_independent_comp_subtype],
intros f hsupport hsum,
rcases eq_empty_or_nonempty c with rfl | ⟨a, hac⟩,
{ simpa using hsupport },
haveI : is_refl X r := ⟨λ _, set.subset.refl _⟩,
obtain ⟨I, I_mem, hI⟩ : ∃ I ∈ c, (f.support : set ι) ⊆ I :=
finset.exists_mem_subset_of_subset_bUnion_of_directed_on hac hc.directed_on hsupport,
exact linear_independent_comp_subtype.mp I.2 f hI hsum },
have trans : transitive r := λ I J K, set.subset.trans,
obtain ⟨⟨I, hli : indep I⟩, hmax : ∀ a, r ⟨I, hli⟩ a → r a ⟨I, hli⟩⟩ :=
@zorn.exists_maximal_of_chains_bounded _ r
(λ c hc, ⟨⟨⋃ I ∈ c, (I : set ι), key c hc⟩, λ I, set.subset_bUnion_of_mem⟩) trans,
exact ⟨I, hli, λ J hsub hli, set.subset.antisymm hsub (hmax ⟨J, hli⟩ hsub)⟩,
end
lemma exists_maximal_independent (s : ι → M) : ∃ I : set ι, linear_independent R (λ x : I, s x) ∧
∀ i ∉ I, ∃ a : R, a ≠ 0 ∧ a • s i ∈ span R (s '' I) :=
begin
classical,
rcases exists_maximal_independent' R s with ⟨I, hIlinind, hImaximal⟩,
use [I, hIlinind],
intros i hi,
specialize hImaximal (I ∪ {i}) (by simp),
set J := I ∪ {i} with hJ,
have memJ : ∀ {x}, x ∈ J ↔ x = i ∨ x ∈ I, by simp [hJ],
have hiJ : i ∈ J := by simp,
have h := mt hImaximal _, swap,
{ intro h2,
rw h2 at hi,
exact absurd hiJ hi },
obtain ⟨f, supp_f, sum_f, f_ne⟩ := linear_dependent_comp_subtype.mp h,
have hfi : f i ≠ 0,
{ contrapose hIlinind,
refine linear_dependent_comp_subtype.mpr ⟨f, _, sum_f, f_ne⟩,
simp only [finsupp.mem_supported, hJ] at ⊢ supp_f,
rintro x hx,
refine (memJ.mp (supp_f hx)).resolve_left _,
rintro rfl,
exact hIlinind (finsupp.mem_support_iff.mp hx) },
use [f i, hfi],
have hfi' : i ∈ f.support := finsupp.mem_support_iff.mpr hfi,
rw [← finset.insert_erase hfi', finset.sum_insert (finset.not_mem_erase _ _),
add_eq_zero_iff_eq_neg] at sum_f,
rw sum_f,
refine neg_mem _ (sum_mem _ (λ c hc, smul_mem _ _ (subset_span ⟨c, _, rfl⟩))),
exact (memJ.mp (supp_f (finset.erase_subset _ _ hc))).resolve_left (finset.ne_of_mem_erase hc),
end
end repr
lemma surjective_of_linear_independent_of_span [nontrivial R]
(hv : linear_independent R v) (f : ι' ↪ ι)
(hss : range v ⊆ span R (range (v ∘ f))) :
surjective f :=
begin
intros i,
let repr : (span R (range (v ∘ f)) : Type*) → ι' →₀ R := (hv.comp f f.injective).repr,
let l := (repr ⟨v i, hss (mem_range_self i)⟩).map_domain f,
have h_total_l : finsupp.total ι M R v l = v i,
{ dsimp only [l],
rw finsupp.total_map_domain,
rw (hv.comp f f.injective).total_repr,
{ refl },
{ exact f.injective } },
have h_total_eq : (finsupp.total ι M R v) l = (finsupp.total ι M R v) (finsupp.single i 1),
by rw [h_total_l, finsupp.total_single, one_smul],
have l_eq : l = _ := linear_map.ker_eq_bot.1 hv h_total_eq,
dsimp only [l] at l_eq,
rw ←finsupp.emb_domain_eq_map_domain at l_eq,
rcases finsupp.single_of_emb_domain_single (repr ⟨v i, _⟩) f i (1 : R) zero_ne_one.symm l_eq
with ⟨i', hi'⟩,
use i',
exact hi'.2
end
lemma eq_of_linear_independent_of_span_subtype [nontrivial R] {s t : set M}
(hs : linear_independent R (λ x, x : s → M)) (h : t ⊆ s) (hst : s ⊆ span R t) : s = t :=
begin
let f : t ↪ s := ⟨λ x, ⟨x.1, h x.2⟩, λ a b hab, subtype.coe_injective (subtype.mk.inj hab)⟩,
have h_surj : surjective f,
{ apply surjective_of_linear_independent_of_span hs f _,
convert hst; simp [f, comp], },
show s = t,
{ apply subset.antisymm _ h,
intros x hx,
rcases h_surj ⟨x, hx⟩ with ⟨y, hy⟩,
convert y.mem,
rw ← subtype.mk.inj hy,
refl }
end
open linear_map
lemma linear_independent.image_subtype {s : set M} {f : M →ₗ[R] M'}
(hs : linear_independent R (λ x, x : s → M))
(hf_inj : disjoint (span R s) f.ker) : linear_independent R (λ x, x : f '' s → M') :=
begin
rw [← @subtype.range_coe _ s] at hf_inj,
refine (hs.map hf_inj).to_subtype_range' _,
simp [set.range_comp f]
end
lemma linear_independent.inl_union_inr {s : set M} {t : set M'}
(hs : linear_independent R (λ x, x : s → M))
(ht : linear_independent R (λ x, x : t → M')) :
linear_independent R (λ x, x : inl R M M' '' s ∪ inr R M M' '' t → M × M') :=
begin
refine (hs.image_subtype _).union (ht.image_subtype _) _; [simp, simp, skip],
simp only [span_image],
simp [disjoint_iff, prod_inf_prod]
end
lemma linear_independent_inl_union_inr' {v : ι → M} {v' : ι' → M'}
(hv : linear_independent R v) (hv' : linear_independent R v') :
linear_independent R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) :=
(hv.map' (inl R M M') ker_inl).sum_type (hv'.map' (inr R M M') ker_inr) $
begin
refine is_compl_range_inl_inr.disjoint.mono _ _;
simp only [span_le, range_coe, range_comp_subset_range],
end
/-- Dedekind's linear independence of characters -/
-- See, for example, Keith Conrad's note
-- <https://kconrad.math.uconn.edu/blurbs/galoistheory/linearchar.pdf>
theorem linear_independent_monoid_hom (G : Type*) [monoid G] (L : Type*) [comm_ring L]
[no_zero_divisors L] :
@linear_independent _ L (G → L) (λ f, f : (G →* L) → (G → L)) _ _ _ :=
by letI := classical.dec_eq (G →* L);
letI : mul_action L L := distrib_mul_action.to_mul_action;
-- We prove linear independence by showing that only the trivial linear combination vanishes.
exact linear_independent_iff'.2
-- To do this, we use `finset` induction,
(λ s, finset.induction_on s (λ g hg i, false.elim) $ λ a s has ih g hg,
-- Here
-- * `a` is a new character we will insert into the `finset` of characters `s`,
-- * `ih` is the fact that only the trivial linear combination of characters in `s` is zero
-- * `hg` is the fact that `g` are the coefficients of a linear combination summing to zero
-- and it remains to prove that `g` vanishes on `insert a s`.
-- We now make the key calculation:
-- For any character `i` in the original `finset`, we have `g i • i = g i • a` as functions on the
-- monoid `G`.
have h1 : ∀ i ∈ s, (g i • i : G → L) = g i • a, from λ i his, funext $ λ x : G,
-- We prove these expressions are equal by showing
-- the differences of their values on each monoid element `x` is zero
eq_of_sub_eq_zero $ ih (λ j, g j * j x - g j * a x)
(funext $ λ y : G, calc
-- After that, it's just a chase scene.
(∑ i in s, ((g i * i x - g i * a x) • i : G → L)) y
= ∑ i in s, (g i * i x - g i * a x) * i y : finset.sum_apply _ _ _
... = ∑ i in s, (g i * i x * i y - g i * a x * i y) : finset.sum_congr rfl
(λ _ _, sub_mul _ _ _)
... = ∑ i in s, g i * i x * i y - ∑ i in s, g i * a x * i y : finset.sum_sub_distrib
... = (g a * a x * a y + ∑ i in s, g i * i x * i y)
- (g a * a x * a y + ∑ i in s, g i * a x * i y) : by rw add_sub_add_left_eq_sub
... = ∑ i in insert a s, g i * i x * i y - ∑ i in insert a s, g i * a x * i y :
by rw [finset.sum_insert has, finset.sum_insert has]
... = ∑ i in insert a s, g i * i (x * y) - ∑ i in insert a s, a x * (g i * i y) :
congr (congr_arg has_sub.sub (finset.sum_congr rfl $ λ i _, by rw [i.map_mul, mul_assoc]))
(finset.sum_congr rfl $ λ _ _, by rw [mul_assoc, mul_left_comm])
... = (∑ i in insert a s, (g i • i : G → L)) (x * y)
- a x * (∑ i in insert a s, (g i • i : G → L)) y :
by rw [finset.sum_apply, finset.sum_apply, finset.mul_sum]; refl
... = 0 - a x * 0 : by rw hg; refl
... = 0 : by rw [mul_zero, sub_zero])
i
his,
-- On the other hand, since `a` is not already in `s`, for any character `i ∈ s`
-- there is some element of the monoid on which it differs from `a`.
have h2 : ∀ i : G →* L, i ∈ s → ∃ y, i y ≠ a y, from λ i his,
classical.by_contradiction $ λ h,
have hia : i = a, from monoid_hom.ext $ λ y, classical.by_contradiction $ λ hy, h ⟨y, hy⟩,
has $ hia ▸ his,
-- From these two facts we deduce that `g` actually vanishes on `s`,
have h3 : ∀ i ∈ s, g i = 0, from λ i his, let ⟨y, hy⟩ := h2 i his in
have h : g i • i y = g i • a y, from congr_fun (h1 i his) y,
or.resolve_right (mul_eq_zero.1 $ by rw [mul_sub, sub_eq_zero]; exact h) (sub_ne_zero_of_ne hy),
-- And so, using the fact that the linear combination over `s` and over `insert a s` both vanish,
-- we deduce that `g a = 0`.
have h4 : g a = 0, from calc
g a = g a * 1 : (mul_one _).symm
... = (g a • a : G → L) 1 : by rw ← a.map_one; refl
... = (∑ i in insert a s, (g i • i : G → L)) 1 : begin
rw finset.sum_eq_single a,
{ intros i his hia, rw finset.mem_insert at his,
rw [h3 i (his.resolve_left hia), zero_smul] },
{ intros haas, exfalso, apply haas, exact finset.mem_insert_self a s }
end
... = 0 : by rw hg; refl,
-- Now we're done; the last two facts together imply that `g` vanishes on every element
-- of `insert a s`.
(finset.forall_mem_insert _ _ _).2 ⟨h4, h3⟩)
lemma le_of_span_le_span [nontrivial R] {s t u: set M}
(hl : linear_independent R (coe : u → M )) (hsu : s ⊆ u) (htu : t ⊆ u)
(hst : span R s ≤ span R t) : s ⊆ t :=
begin
have := eq_of_linear_independent_of_span_subtype
(hl.mono (set.union_subset hsu htu))
(set.subset_union_right _ _)
(set.union_subset (set.subset.trans subset_span hst) subset_span),
rw ← this, apply set.subset_union_left
end
lemma span_le_span_iff [nontrivial R] {s t u: set M}
(hl : linear_independent R (coe : u → M)) (hsu : s ⊆ u) (htu : t ⊆ u) :
span R s ≤ span R t ↔ s ⊆ t :=
⟨le_of_span_le_span hl hsu htu, span_mono⟩
end module
section nontrivial
variables [ring R] [nontrivial R] [add_comm_group M] [add_comm_group M']
variables [module R M] [no_zero_smul_divisors R M] [module R M']
variables {v : ι → M} {s t : set M} {x y z : M}
lemma linear_independent_unique_iff
(v : ι → M) [unique ι] :
linear_independent R v ↔ v (default ι) ≠ 0 :=
begin
simp only [linear_independent_iff, finsupp.total_unique, smul_eq_zero],
refine ⟨λ h hv, _, λ hv l hl, finsupp.unique_ext $ hl.resolve_right hv⟩,
have := h (finsupp.single (default ι) 1) (or.inr hv),
exact one_ne_zero (finsupp.single_eq_zero.1 this)
end
alias linear_independent_unique_iff ↔ _ linear_independent_unique
lemma linear_independent_singleton {x : M} (hx : x ≠ 0) :
linear_independent R (λ x, x : ({x} : set M) → M) :=
linear_independent_unique coe hx
end nontrivial
/-!
### Properties which require `division_ring K`
These can be considered generalizations of properties of linear independence in vector spaces.
-/
section module
variables [division_ring K] [add_comm_group V] [add_comm_group V']
variables [module K V] [module K V']
variables {v : ι → V} {s t : set V} {x y z : V}
open submodule
/- TODO: some of the following proofs can generalized with a zero_ne_one predicate type class
(instead of a data containing type class) -/
lemma mem_span_insert_exchange : x ∈ span K (insert y s) → x ∉ span K s → y ∈ span K (insert x s) :=
begin
simp [mem_span_insert],
rintro a z hz rfl h,
refine ⟨a⁻¹, -a⁻¹ • z, smul_mem _ _ hz, _⟩,
have a0 : a ≠ 0, {rintro rfl, simp * at *},
simp [a0, smul_add, smul_smul]
end
lemma linear_independent_iff_not_mem_span :
linear_independent K v ↔ (∀i, v i ∉ span K (v '' (univ \ {i}))) :=
begin
apply linear_independent_iff_not_smul_mem_span.trans,
split,
{ intros h i h_in_span,
apply one_ne_zero (h i 1 (by simp [h_in_span])) },
{ intros h i a ha,
by_contradiction ha',
exact false.elim (h _ ((smul_mem_iff _ ha').1 ha)) }
end
lemma linear_independent.insert (hs : linear_independent K (λ b, b : s → V)) (hx : x ∉ span K s) :
linear_independent K (λ b, b : insert x s → V) :=
begin
rw ← union_singleton,
have x0 : x ≠ 0 := mt (by rintro rfl; apply zero_mem _) hx,
apply hs.union (linear_independent_singleton x0),
rwa [disjoint_span_singleton' x0]
end
lemma linear_independent_option' :
linear_independent K (λ o, option.cases_on' o x v : option ι → V) ↔
linear_independent K v ∧ (x ∉ submodule.span K (range v)) :=
begin
rw [← linear_independent_equiv (equiv.option_equiv_sum_punit ι).symm, linear_independent_sum,
@range_unique _ punit, @linear_independent_unique_iff punit, disjoint_span_singleton],
dsimp [(∘)],
refine ⟨λ h, ⟨h.1, λ hx, h.2.1 $ h.2.2 hx⟩, λ h, ⟨h.1, _, λ hx, (h.2 hx).elim⟩⟩,
rintro rfl,
exact h.2 (zero_mem _)
end
lemma linear_independent.option (hv : linear_independent K v)
(hx : x ∉ submodule.span K (range v)) :
linear_independent K (λ o, option.cases_on' o x v : option ι → V) :=
linear_independent_option'.2 ⟨hv, hx⟩
lemma linear_independent_option {v : option ι → V} :
linear_independent K v ↔
linear_independent K (v ∘ coe : ι → V) ∧ v none ∉ submodule.span K (range (v ∘ coe : ι → V)) :=
by simp only [← linear_independent_option', option.cases_on'_none_coe]
theorem linear_independent_insert' {ι} {s : set ι} {a : ι} {f : ι → V} (has : a ∉ s) :
linear_independent K (λ x : insert a s, f x) ↔
linear_independent K (λ x : s, f x) ∧ f a ∉ submodule.span K (f '' s) :=
by { rw [← linear_independent_equiv ((equiv.option_equiv_sum_punit _).trans
(equiv.set.insert has).symm), linear_independent_option], simp [(∘), range_comp f] }
theorem linear_independent_insert (hxs : x ∉ s) :
linear_independent K (λ b : insert x s, (b : V)) ↔
linear_independent K (λ b : s, (b : V)) ∧ x ∉ submodule.span K s :=
(@linear_independent_insert' _ _ _ _ _ _ _ _ id hxs).trans $ by simp
lemma linear_independent_pair {x y : V} (hx : x ≠ 0) (hy : ∀ a : K, a • x ≠ y) :
linear_independent K (coe : ({x, y} : set V) → V) :=
pair_comm y x ▸ (linear_independent_singleton hx).insert $ mt mem_span_singleton.1
(not_exists.2 hy)
lemma linear_independent_fin_cons {n} {v : fin n → V} :
linear_independent K (fin.cons x v : fin (n + 1) → V) ↔
linear_independent K v ∧ x ∉ submodule.span K (range v) :=
begin
rw [← linear_independent_equiv (fin_succ_equiv n).symm, linear_independent_option],
convert iff.rfl,
{ ext,
-- TODO: why doesn't simp use `fin_succ_equiv_symm_coe` here?
rw [comp_app, comp_app, fin_succ_equiv_symm_coe, fin.cons_succ] },
{ ext,
rw [comp_app, comp_app, fin_succ_equiv_symm_coe, fin.cons_succ] }
end
lemma linear_independent_fin_snoc {n} {v : fin n → V} :
linear_independent K (fin.snoc v x : fin (n + 1) → V) ↔
linear_independent K v ∧ x ∉ submodule.span K (range v) :=
by rw [fin.snoc_eq_cons_rotate, linear_independent_equiv, linear_independent_fin_cons]
/-- See `linear_independent.fin_cons'` for an uglier version that works if you
only have a module over a semiring. -/
lemma linear_independent.fin_cons {n} {v : fin n → V} (hv : linear_independent K v)
(hx : x ∉ submodule.span K (range v)) :
linear_independent K (fin.cons x v : fin (n + 1) → V) :=
linear_independent_fin_cons.2 ⟨hv, hx⟩
lemma linear_independent_fin_succ {n} {v : fin (n + 1) → V} :
linear_independent K v ↔
linear_independent K (fin.tail v) ∧ v 0 ∉ submodule.span K (range $ fin.tail v) :=
by rw [← linear_independent_fin_cons, fin.cons_self_tail]
lemma linear_independent_fin_succ' {n} {v : fin (n + 1) → V} :
linear_independent K v ↔
linear_independent K (fin.init v) ∧ v (fin.last _) ∉ submodule.span K (range $ fin.init v) :=
by rw [← linear_independent_fin_snoc, fin.snoc_init_self]
lemma linear_independent_fin2 {f : fin 2 → V} :
linear_independent K f ↔ f 1 ≠ 0 ∧ ∀ a : K, a • f 1 ≠ f 0 :=
by rw [linear_independent_fin_succ, linear_independent_unique_iff, range_unique,
mem_span_singleton, not_exists,
show fin.tail f (default (fin 1)) = f 1, by rw ← fin.succ_zero_eq_one; refl]
lemma exists_linear_independent (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) :
∃b⊆t, s ⊆ b ∧ t ⊆ span K b ∧ linear_independent K (λ x, x : b → V) :=
begin
rcases zorn.zorn_subset_nonempty {b | b ⊆ t ∧ linear_independent K (λ x, x : b → V)} _ _
⟨hst, hs⟩ with ⟨b, ⟨bt, bi⟩, sb, h⟩,
{ refine ⟨b, bt, sb, λ x xt, _, bi⟩,
by_contra hn,
apply hn,
rw ← h _ ⟨insert_subset.2 ⟨xt, bt⟩, bi.insert hn⟩ (subset_insert _ _),
exact subset_span (mem_insert _ _) },
{ refine λ c hc cc c0, ⟨⋃₀ c, ⟨_, _⟩, λ x, _⟩,
{ exact sUnion_subset (λ x xc, (hc xc).1) },
{ exact linear_independent_sUnion_of_directed cc.directed_on (λ x xc, (hc xc).2) },
{ exact subset_sUnion_of_mem } }
end
/-- `linear_independent.extend` adds vectors to a linear independent set `s ⊆ t` until it spans
all elements of `t`. -/
noncomputable def linear_independent.extend (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : set V :=
classical.some (exists_linear_independent hs hst)
lemma linear_independent.extend_subset (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : hs.extend hst ⊆ t :=
let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent hs hst) in hbt
lemma linear_independent.subset_extend (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : s ⊆ hs.extend hst :=
let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent hs hst) in hsb
lemma linear_independent.subset_span_extend (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : t ⊆ span K (hs.extend hst) :=
let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent hs hst) in htb
lemma linear_independent.linear_independent_extend (hs : linear_independent K (λ x, x : s → V))
(hst : s ⊆ t) : linear_independent K (coe : hs.extend hst → V) :=
let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent hs hst) in hli
variables {K V}
-- TODO(Mario): rewrite?
lemma exists_of_linear_independent_of_finite_span {t : finset V}
(hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ (span K ↑t : submodule K V)) :
∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = t.card :=
have ∀t, ∀(s' : finset V), ↑s' ⊆ s → s ∩ ↑t = ∅ → s ⊆ (span K ↑(s' ∪ t) : submodule K V) →
∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = (s' ∪ t).card :=
assume t, finset.induction_on t
(assume s' hs' _ hss',
have s = ↑s',
from eq_of_linear_independent_of_span_subtype hs hs' $
by simpa using hss',
⟨s', by simp [this]⟩)
(assume b₁ t hb₁t ih s' hs' hst hss',
have hb₁s : b₁ ∉ s,
from assume h,
have b₁ ∈ s ∩ ↑(insert b₁ t), from ⟨h, finset.mem_insert_self _ _⟩,
by rwa [hst] at this,
have hb₁s' : b₁ ∉ s', from assume h, hb₁s $ hs' h,
have hst : s ∩ ↑t = ∅,
from eq_empty_of_subset_empty $ subset.trans
(by simp [inter_subset_inter, subset.refl]) (le_of_eq hst),
classical.by_cases
(assume : s ⊆ (span K ↑(s' ∪ t) : submodule K V),
let ⟨u, hust, hsu, eq⟩ := ih _ hs' hst this in
have hb₁u : b₁ ∉ u, from assume h, (hust h).elim hb₁s hb₁t,
⟨insert b₁ u, by simp [insert_subset_insert hust],
subset.trans hsu (by simp), by simp [eq, hb₁t, hb₁s', hb₁u]⟩)
(assume : ¬ s ⊆ (span K ↑(s' ∪ t) : submodule K V),
let ⟨b₂, hb₂s, hb₂t⟩ := not_subset.mp this in
have hb₂t' : b₂ ∉ s' ∪ t, from assume h, hb₂t $ subset_span h,
have s ⊆ (span K ↑(insert b₂ s' ∪ t) : submodule K V), from
assume b₃ hb₃,
have ↑(s' ∪ insert b₁ t) ⊆ insert b₁ (insert b₂ ↑(s' ∪ t) : set V),
by simp [insert_eq, -singleton_union, -union_singleton, union_subset_union, subset.refl,
subset_union_right],
have hb₃ : b₃ ∈ span K (insert b₁ (insert b₂ ↑(s' ∪ t) : set V)),
from span_mono this (hss' hb₃),
have s ⊆ (span K (insert b₁ ↑(s' ∪ t)) : submodule K V),
by simpa [insert_eq, -singleton_union, -union_singleton] using hss',
have hb₁ : b₁ ∈ span K (insert b₂ ↑(s' ∪ t)),
from mem_span_insert_exchange (this hb₂s) hb₂t,
by rw [span_insert_eq_span hb₁] at hb₃; simpa using hb₃,
let ⟨u, hust, hsu, eq⟩ := ih _ (by simp [insert_subset, hb₂s, hs']) hst this in
⟨u, subset.trans hust $ union_subset_union (subset.refl _) (by simp [subset_insert]),
hsu, by simp [eq, hb₂t', hb₁t, hb₁s']⟩)),
begin
have eq : t.filter (λx, x ∈ s) ∪ t.filter (λx, x ∉ s) = t,
{ ext1 x,
by_cases x ∈ s; simp * },
apply exists.elim (this (t.filter (λx, x ∉ s)) (t.filter (λx, x ∈ s))
(by simp [set.subset_def]) (by simp [set.ext_iff] {contextual := tt}) (by rwa [eq])),
intros u h,
exact ⟨u, subset.trans h.1 (by simp [subset_def, and_imp, or_imp_distrib] {contextual:=tt}),
h.2.1, by simp only [h.2.2, eq]⟩
end
lemma exists_finite_card_le_of_finite_of_linear_independent_of_span
(ht : finite t) (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ span K t) :
∃h : finite s, h.to_finset.card ≤ ht.to_finset.card :=
have s ⊆ (span K ↑(ht.to_finset) : submodule K V), by simp; assumption,
let ⟨u, hust, hsu, eq⟩ := exists_of_linear_independent_of_finite_span hs this in
have finite s, from u.finite_to_set.subset hsu,
⟨this, by rw [←eq]; exact (finset.card_le_of_subset $ finset.coe_subset.mp $ by simp [hsu])⟩
end module
|
cfe931bd73d650afbe58c078783c5c5d74dbbb3f | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /tests/lean/run/e12.lean | 0d3bb20b80e54b37ad1ec4ca1d631672c963049c | [
"Apache-2.0"
] | permissive | codyroux/lean | 7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3 | 0cca265db19f7296531e339192e9b9bae4a31f8b | refs/heads/master | 1,610,909,964,159 | 1,407,084,399,000 | 1,416,857,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 957 | lean | precedence `+`:65
namespace nat
constant nat : Type.{1}
constant add : nat → nat → nat
infixl + := add
end nat
namespace int
open nat (nat)
constant int : Type.{1}
constant add : int → int → int
infixl + := add
constant of_nat : nat → int
coercion of_nat
end int
constants n m : nat.nat
constants i j : int.int
section
open [notation] nat
open [notation] int
open [decls] nat
open [decls] int
check n+m
check i+j
-- check i+n -- Error
end
namespace int
open [decls] nat (nat)
-- Here is a possible trick for this kind of configuration
definition add_ni (a : nat) (b : int) := (of_nat a) + b
definition add_in (a : int) (b : nat) := a + (of_nat b)
infixl + := add_ni
infixl + := add_in
end int
section
open [notation] nat
open [notation] int
open [declarations] nat
open [declarations] int
check n+m
check i+n
check n+i
end
section
open nat
open int
check n+m
check i+n
end
|
ff1ce989a91c959676cce04e3cd8ad111368fb19 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/section2.lean | c825d23bdbce05508293d6cb7f37f3ec10c7965e | [
"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 | 203 | lean | import data.nat
section foo
variable A : Type
variable a : nat
end foo
namespace n1
section foo
variable A : Type
definition id (a : A) := a
variable a : nat
check n1.id _ a
end foo
end n1
|
8d445de635f39e62c532bf34a3b20cd4aba93e2d | f1b175e38ffc5cc1c7c5551a72d0dbaf70786f83 | /analysis/measure_theory/lebesgue_measure.lean | 9fe2facab3c21b3d4300a06628c81fb091c6456c | [
"Apache-2.0"
] | permissive | mjendrusch/mathlib | df3ae884dd5ce38c7edf452bcbfd3baf4e3a6214 | 5c209edb7eb616a26f64efe3500f2b1ba95b8d55 | refs/heads/master | 1,585,663,284,800 | 1,539,062,055,000 | 1,539,062,055,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,796 | 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
Lebesgue measure on the real line
-/
import analysis.measure_theory.measure_space analysis.measure_theory.borel_space
noncomputable theory
open classical set lattice filter
open nnreal (of_real)
namespace measure_theory
/-- Length of an interval. This is the largest monotonic function which correctly
measures all intervals. -/
def lebesgue_length (s : set ℝ) : ennreal := ⨅a b (h : s ⊆ Ico a b), of_real (b - a)
@[simp] lemma lebesgue_length_empty : lebesgue_length ∅ = 0 :=
le_zero_iff_eq.1 $ infi_le_of_le 0 $ infi_le_of_le 0 $ by simp
@[simp] lemma lebesgue_length_Ico (a b : ℝ) :
lebesgue_length (Ico a b) = of_real (b - a) :=
begin
refine le_antisymm (infi_le_of_le a $ infi_le_of_le b $ infi_le _ (by refl))
(le_infi $ λ a', le_infi $ λ b', le_infi $ λ h, ennreal.coe_le_coe.2 _),
cases le_or_lt b a with ab ab,
{ rw nnreal.of_real_of_nonpos (sub_nonpos.2 ab), simp },
cases (Ico_subset_Ico_iff ab).1 h with h₁ h₂,
exact nnreal.of_real_le_of_real (sub_le_sub h₂ h₁)
end
lemma lebesgue_length_mono {s₁ s₂ : set ℝ} (h : s₁ ⊆ s₂) : lebesgue_length s₁ ≤ lebesgue_length s₂ :=
infi_le_infi $ λ a, infi_le_infi $ λ b, infi_le_infi2 $ λ h', ⟨subset.trans h h', le_refl _⟩
lemma lebesgue_length_eq_infi_Ioo (s) : lebesgue_length s = ⨅a b (h : s ⊆ Ioo a b), of_real (b - a) :=
begin
refine le_antisymm
(infi_le_infi $ λ a, infi_le_infi $ λ b, infi_le_infi2 $ λ h,
⟨subset.trans h Ioo_subset_Ico_self, le_refl _⟩) _,
refine le_infi (λ a, le_infi $ λ b, le_infi $ λ h, _),
refine ennreal.le_of_forall_epsilon_le (λ ε ε0 _, _),
refine infi_le_of_le (a - ε) (infi_le_of_le b $ infi_le_of_le
(subset.trans h $ Ico_subset_Ioo_left $ (sub_lt_self_iff _).2 ε0) _),
rw [← sub_add, ← ennreal.coe_add, ennreal.coe_le_coe],
apply le_trans nnreal.of_real_add_le _,
simp,
end
@[simp] lemma lebesgue_length_Ioo (a b : ℝ) :
lebesgue_length (Ioo a b) = of_real (b - a) :=
begin
rw ← lebesgue_length_Ico,
refine le_antisymm (lebesgue_length_mono Ioo_subset_Ico_self) _,
rw lebesgue_length_eq_infi_Ioo (Ioo a b),
refine (le_infi $ λ a', le_infi $ λ b', le_infi $ λ h, _),
cases le_or_lt b a with ab ab, {simp [ab]},
cases (Ioo_subset_Ioo_iff ab).1 h with h₁ h₂,
rw [lebesgue_length_Ico, ennreal.coe_le_coe],
exact nnreal.of_real_le_of_real (sub_le_sub h₂ h₁)
end
lemma lebesgue_length_eq_infi_Icc (s) : lebesgue_length s = ⨅a b (h : s ⊆ Icc a b), of_real (b - a) :=
begin
refine le_antisymm _
(infi_le_infi $ λ a, infi_le_infi $ λ b, infi_le_infi2 $ λ h,
⟨subset.trans h Ico_subset_Icc_self, le_refl _⟩),
refine le_infi (λ a, le_infi $ λ b, le_infi $ λ h, _),
refine ennreal.le_of_forall_epsilon_le (λ ε ε0 _, _),
refine infi_le_of_le a (infi_le_of_le (b + ε) $ infi_le_of_le
(subset.trans h $ Icc_subset_Ico_right $ (lt_add_iff_pos_right _).2 ε0) _),
rw [sub_eq_add_neg, add_right_comm, ←ennreal.coe_add, ennreal.coe_le_coe],
apply le_trans nnreal.of_real_add_le,
simp
end
@[simp] lemma lebesgue_length_Icc (a b : ℝ) :
lebesgue_length (Icc a b) = of_real (b - a) :=
begin
rw ← lebesgue_length_Ico,
refine le_antisymm _ (lebesgue_length_mono Ico_subset_Icc_self),
rw lebesgue_length_eq_infi_Icc (Icc a b),
exact infi_le_of_le a (infi_le_of_le b $ infi_le_of_le (by refl) (by simp))
end
/-- The Lebesgue outer measure, as an outer measure of ℝ. -/
def lebesgue_outer : outer_measure ℝ :=
outer_measure.of_function lebesgue_length lebesgue_length_empty
lemma lebesgue_outer_le_length (s : set ℝ) : lebesgue_outer s ≤ lebesgue_length s :=
outer_measure.of_function_le _ _ _
lemma lebesgue_length_subadditive {a b : ℝ} {c d : ℕ → ℝ}
(ss : Icc a b ⊆ ⋃i, Ioo (c i) (d i)) :
(of_real (b - a) : ennreal) ≤ ∑ i, of_real (d i - c i) :=
begin
suffices : ∀ (s:finset ℕ) b
(cv : Icc a b ⊆ ⋃ i ∈ (↑s:set ℕ), Ioo (c i) (d i)),
(of_real (b - a) : ennreal) ≤ s.sum (λ i, of_real (d i - c i)),
{ rcases @compact_elim_finite_subcover_image _ _
_ (Icc a b) univ (λ i, Ioo (c i) (d i)) compact_Icc
(λ i _, is_open_Ioo) (by simpa using ss) with ⟨s, su, hf, hs⟩,
have e : (⋃ i ∈ (↑hf.to_finset:set ℕ),
Ioo (c i) (d i)) = (⋃ i ∈ s, Ioo (c i) (d i)), {simp [set.ext_iff]},
rw ennreal.tsum_eq_supr_sum,
refine le_trans _ (le_supr _ hf.to_finset),
exact this hf.to_finset _ (by simpa [e]) },
clear ss b,
refine λ s, finset.strong_induction_on s (λ s IH b cv, _),
cases le_total b a with ab ab,
{ rw nnreal.of_real_of_nonpos (sub_nonpos.2 ab), simp },
have := cv ⟨ab, le_refl _⟩, simp at this,
rcases this with ⟨i, is, cb, bd⟩,
rw [← finset.insert_erase is] at cv ⊢,
rw [finset.coe_insert, bUnion_insert] at cv,
rw [finset.sum_insert (finset.not_mem_erase _ _)],
refine le_trans _ (add_le_add_left' (IH _ (finset.erase_ssubset is) (c i) _)),
{ rw [← ennreal.coe_add, ennreal.coe_le_coe],
refine le_trans (nnreal.of_real_le_of_real _) nnreal.of_real_add_le,
rw sub_add_sub_cancel,
exact sub_le_sub_right (le_of_lt bd) _ },
{ rintro x ⟨h₁, h₂⟩,
refine (cv ⟨h₁, le_trans h₂ (le_of_lt cb)⟩).resolve_left
(mt and.left (not_lt_of_le h₂)) }
end
@[simp] lemma lebesgue_outer_Icc (a b : ℝ) :
lebesgue_outer (Icc a b) = of_real (b - a) :=
begin
refine le_antisymm (by rw ← lebesgue_length_Icc; apply lebesgue_outer_le_length)
(le_infi $ λ f, le_infi $ λ hf,
ennreal.le_of_forall_epsilon_le $ λ ε ε0 h, _),
rcases ennreal.exists_pos_sum_of_encodable
(ennreal.zero_lt_coe_iff.2 ε0) ℕ with ⟨ε', ε'0, hε⟩,
refine le_trans _ (add_le_add_left' (le_of_lt hε)),
rw ← ennreal.tsum_add,
choose g hg using show
∀ i, ∃ p:ℝ×ℝ, f i ⊆ Ioo p.1 p.2 ∧ (of_real (p.2 - p.1) : ennreal) < lebesgue_length (f i) + ε' i,
{ intro i,
have := (ennreal.lt_add_right (lt_of_le_of_lt (ennreal.le_tsum i) h)
(ennreal.zero_lt_coe_iff.2 (ε'0 i))),
conv at this {to_lhs, rw lebesgue_length_eq_infi_Ioo},
simpa [infi_lt_iff] },
refine le_trans _ (ennreal.tsum_le_tsum $ λ i, le_of_lt (hg i).2),
exact lebesgue_length_subadditive (subset.trans hf $
Union_subset_Union $ λ i, (hg i).1)
end
@[simp] lemma lebesgue_outer_singleton (a : ℝ) : lebesgue_outer {a} = 0 :=
by simpa using lebesgue_outer_Icc a a
@[simp] lemma lebesgue_outer_Ico (a b : ℝ) :
lebesgue_outer (Ico a b) = of_real (b - a) :=
begin
refine le_antisymm (by rw ← lebesgue_length_Ico; apply lebesgue_outer_le_length)
(ennreal.le_of_forall_epsilon_le $ λ ε ε0 h, _),
have := @nnreal.of_real_add_le (b - a - ε) ε,
rw [← ennreal.coe_le_coe, ennreal.coe_add, sub_add_cancel, sub_right_comm,
← lebesgue_outer_Icc a (b-ε), nnreal.of_real_coe] at this,
exact le_trans this (add_le_add_right' $ lebesgue_outer.mono $
Icc_subset_Ico_right $ (sub_lt_self_iff _).2 ε0)
end
@[simp] lemma lebesgue_outer_Ioo (a b : ℝ) :
lebesgue_outer (Ioo a b) = of_real (b - a) :=
begin
refine le_antisymm (by rw ← lebesgue_length_Ioo; apply lebesgue_outer_le_length)
(ennreal.le_of_forall_epsilon_le $ λ ε ε0 h, _),
have := @nnreal.of_real_add_le (b - a - ε) ε,
rw [← ennreal.coe_le_coe, ennreal.coe_add, sub_add_cancel, sub_sub,
← lebesgue_outer_Ico (a+ε) b, nnreal.of_real_coe] at this,
exact le_trans this (add_le_add_right' $ lebesgue_outer.mono $
Ico_subset_Ioo_left $ (lt_add_iff_pos_right _).2 ε0)
end
lemma is_lebesgue_measurable_Iio {c : ℝ} :
lebesgue_outer.caratheodory.is_measurable (Iio c) :=
outer_measure.caratheodory_is_measurable $ λ t,
le_infi $ λ a, le_infi $ λ b, le_infi $ λ h, begin
refine le_trans (add_le_add'
(lebesgue_length_mono $ inter_subset_inter_left _ h)
(lebesgue_length_mono $ diff_subset_diff_left h)) _,
cases le_total a c with hac hca; cases le_total b c with hbc hcb;
simp [*, -sub_eq_add_neg, sub_add_sub_cancel'];
rw [← ennreal.coe_add, ennreal.coe_le_coe],
{ simp [*, nnreal.of_real_add_of_real, -sub_eq_add_neg, sub_add_sub_cancel'] },
{ rw nnreal.of_real_of_nonpos,
{ simp },
exact sub_nonpos.2 (le_trans hbc hca) }
end
theorem lebesgue_outer_trim : lebesgue_outer.trim = lebesgue_outer :=
begin
refine le_antisymm (λ s, _) (outer_measure.trim_ge _),
rw outer_measure.trim_eq_infi,
refine le_infi (λ f, le_infi $ λ hf,
ennreal.le_of_forall_epsilon_le $ λ ε ε0 h, _),
rcases ennreal.exists_pos_sum_of_encodable
(ennreal.zero_lt_coe_iff.2 ε0) ℕ with ⟨ε', ε'0, hε⟩,
refine le_trans _ (add_le_add_left' (le_of_lt hε)),
rw ← ennreal.tsum_add,
choose g hg using show
∀ i, ∃ s, f i ⊆ s ∧ is_measurable s ∧ lebesgue_outer s ≤ lebesgue_length (f i) + of_real (ε' i),
{ intro i,
have := (ennreal.lt_add_right (lt_of_le_of_lt (ennreal.le_tsum i) h)
(ennreal.zero_lt_coe_iff.2 (ε'0 i))),
conv at this {to_lhs, rw lebesgue_length},
simp only [infi_lt_iff] at this,
rcases this with ⟨a, b, h₁, h₂⟩,
rw ← lebesgue_outer_Ico at h₂,
exact ⟨_, h₁, is_measurable_Ico, le_of_lt $ by simpa using h₂⟩ },
simp at hg,
apply infi_le_of_le (Union g) _,
apply infi_le_of_le (subset.trans hf $ Union_subset_Union (λ i, (hg i).1)) _,
apply infi_le_of_le (is_measurable.Union (λ i, (hg i).2.1)) _,
exact le_trans (lebesgue_outer.Union _) (ennreal.tsum_le_tsum $ λ i, (hg i).2.2)
end
/-- Lebesgue measure on the Borel sets
The outer Lebesgue measure is the completion of this measure. (TODO: proof this)
-/
def lebesgue : measure ℝ :=
lebesgue_outer.to_measure $
calc borel ℝ = measurable_space.generate_from (⋃a:ℚ, {Iio a}) :
real.borel_eq_generate_from_Iio_rat
... ≤ lebesgue_outer.caratheodory :
measurable_space.generate_from_le $ by simp [is_lebesgue_measurable_Iio] {contextual := tt}
@[simp] theorem lebesgue_to_outer_measure : lebesgue.to_outer_measure = lebesgue_outer :=
(to_measure_to_outer_measure _ _).trans lebesgue_outer_trim
theorem lebesgue_val (s) : lebesgue s = lebesgue_outer s :=
(congr_arg (λ m:outer_measure ℝ, m s) lebesgue_to_outer_measure : _)
@[simp] lemma lebesgue_Ico {a b : ℝ} : lebesgue (Ico a b) = of_real (b - a) :=
by simp [lebesgue_val]
@[simp] lemma lebesgue_Icc {a b : ℝ} : lebesgue (Icc a b) = of_real (b - a) :=
by simp [lebesgue_val]
@[simp] lemma lebesgue_Ioo {a b : ℝ} : lebesgue (Ioo a b) = of_real (b - a) :=
by simp [lebesgue_val]
@[simp] lemma lebesgue_singleton {a : ℝ} : lebesgue {a} = 0 :=
by simp [lebesgue_val]
end measure_theory
|
b128a08051ec520d301e1fc625f1f94f39d10517 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /library/logic/eq.lean | 52d0a292fb32e9b3d01cb0bfc093569c96b59ee7 | [
"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 | 3,570 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad, Floris van Doorn
Additional declarations/theorems about equality. See also init.datatypes and init.logic.
-/
open eq.ops
namespace eq
variables {A B : Type} {a a' a₁ a₂ a₃ a₄ : A}
theorem irrel (H₁ H₂ : a = a') : H₁ = H₂ :=
!proof_irrel
theorem id_refl (H₁ : a = a) : H₁ = (eq.refl a) :=
rfl
theorem rec_on_id {B : A → Type} (H : a = a) (b : B a) : eq.rec_on H b = b :=
rfl
theorem rec_on_constant (H : a = a') {B : Type} (b : B) : eq.rec_on H b = b :=
eq.drec_on H rfl
theorem rec_on_constant2 (H₁ : a₁ = a₂) (H₂ : a₃ = a₄) (b : B) : eq.rec_on H₁ b = eq.rec_on H₂ b :=
rec_on_constant H₁ b ⬝ (rec_on_constant H₂ b)⁻¹
theorem rec_on_irrel_arg {f : A → B} {D : B → Type} (H : a = a') (H' : f a = f a') (b : D (f a)) :
eq.rec_on H b = eq.rec_on H' b :=
eq.drec_on H (λ(H' : f a = f a), !rec_on_id⁻¹) H'
theorem rec_on_irrel {a a' : A} {D : A → Type} (H H' : a = a') (b : D a) :
eq.drec_on H b = eq.drec_on H' b :=
proof_irrel H H' ▸ rfl
theorem rec_on_comp {a b c : A} {P : A → Type} (H₁ : a = b) (H₂ : b = c)
(u : P a) : eq.rec_on H₂ (eq.rec_on H₁ u) = eq.rec_on (trans H₁ H₂) u :=
(show ∀ H₂ : b = c, eq.rec_on H₂ (eq.rec_on H₁ u) = eq.rec_on (trans H₁ H₂) u,
from eq.drec_on H₂ (take (H₂ : b = b), rec_on_id H₂ _))
H₂
end eq
open eq
section
variables {A B C D E F : Type}
variables {a a' : A} {b b' : B} {c c' : C} {d d' : D} {e e' : E}
theorem congr_arg2 (f : A → B → C) (Ha : a = a') (Hb : b = b') : f a b = f a' b' :=
by substvars
theorem congr_arg3 (f : A → B → C → D) (Ha : a = a') (Hb : b = b') (Hc : c = c')
: f a b c = f a' b' c' :=
by substvars
theorem congr_arg4 (f : A → B → C → D → E) (Ha : a = a') (Hb : b = b') (Hc : c = c') (Hd : d = d')
: f a b c d = f a' b' c' d' :=
by substvars
theorem congr_arg5 (f : A → B → C → D → E → F)
(Ha : a = a') (Hb : b = b') (Hc : c = c') (Hd : d = d') (He : e = e')
: f a b c d e = f a' b' c' d' e' :=
by substvars
theorem congr2 (f f' : A → B → C) (Hf : f = f') (Ha : a = a') (Hb : b = b') : f a b = f' a' b' :=
by substvars
theorem congr3 (f f' : A → B → C → D) (Hf : f = f') (Ha : a = a') (Hb : b = b') (Hc : c = c')
: f a b c = f' a' b' c' :=
by substvars
theorem congr4 (f f' : A → B → C → D → E)
(Hf : f = f') (Ha : a = a') (Hb : b = b') (Hc : c = c') (Hd : d = d')
: f a b c d = f' a' b' c' d' :=
by substvars
theorem congr5 (f f' : A → B → C → D → E → F)
(Hf : f = f') (Ha : a = a') (Hb : b = b') (Hc : c = c') (Hd : d = d') (He : e = e')
: f a b c d e = f' a' b' c' d' e' :=
by substvars
end
theorem equal_f {A : Type} {B : A → Type} {f g : Π x, B x} (H : f = g) : ∀x, f x = g x :=
take x, congr_fun H x
section
variables {a b c : Prop}
theorem eqmp (H₁ : a = b) (H₂ : a) : b :=
H₁ ▸ H₂
theorem eqmpr (H₁ : a = b) (H₂ : b) : a :=
H₁⁻¹ ▸ H₂
theorem imp_trans (H₁ : a → b) (H₂ : b → c) : a → c :=
assume Ha, H₂ (H₁ Ha)
theorem imp_eq_trans (H₁ : a → b) (H₂ : b = c) : a → c :=
assume Ha, H₂ ▸ (H₁ Ha)
theorem eq_imp_trans (H₁ : a = b) (H₂ : b → c) : a → c :=
assume Ha, H₂ (H₁ ▸ Ha)
end
|
1b5d7e12b8b3db446779c052d7dd2c1f726b9a71 | f57749ca63d6416f807b770f67559503fdb21001 | /hott/types/equiv.hlean | fbf1e87c32bc5f4745387fd0b72c4eb7853d90c1 | [
"Apache-2.0"
] | permissive | aliassaf/lean | bd54e85bed07b1ff6f01396551867b2677cbc6ac | f9b069b6a50756588b309b3d716c447004203152 | refs/heads/master | 1,610,982,152,948 | 1,438,916,029,000 | 1,438,916,029,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,779 | hlean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Floris van Doorn
Ported from Coq HoTT
Theorems about the types equiv and is_equiv
-/
import .fiber .arrow arity .hprop_trunc
open eq is_trunc sigma sigma.ops pi fiber function equiv equiv.ops
namespace is_equiv
variables {A B : Type} (f : A → B) [H : is_equiv f]
include H
/- is_equiv f is a mere proposition -/
definition is_contr_fiber_of_is_equiv [instance] (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 rewrite inv_con_cancel_left
... = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ (right_inv f (f a) ⬝ p) : by rewrite ap_con_eq_con
... = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ (ap f (left_inv f a) ⬝ p) : by rewrite adj
... = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rewrite con.assoc
... = (ap f (ap f⁻¹ p))⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rewrite ap_compose
... = ap f (ap f⁻¹ p)⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rewrite ap_inv
... = ap f ((ap f⁻¹ p)⁻¹ ⬝ left_inv f a) ⬝ p : by rewrite ap_con)))
definition is_contr_right_inverse : is_contr (Σ(g : B → A), f ∘ g ~ id) :=
begin
fapply is_trunc_equiv_closed,
{apply sigma_equiv_sigma_id, intro g, apply eq_equiv_homotopy},
fapply is_trunc_equiv_closed,
{apply fiber.sigma_char},
fapply is_contr_fiber_of_is_equiv,
apply (to_is_equiv (arrow_equiv_arrow_right (equiv.mk f H))),
end
definition 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,
{apply equiv.symm, apply sigma_pi_equiv_pi_sigma},
fapply is_trunc_equiv_closed,
{apply pi_equiv_pi_id, intro a,
apply (fiber_eq_equiv (fiber.mk (u.1 (f a)) (u.2 (f a))) (fiber.mk a idp))},
end
omit H
protected definition sigma_char : (is_equiv f) ≃
(Σ(g : B → A) (ε : f ∘ g ~ id) (η : g ∘ f ~ id), Π(a : A), ε (f a) = ap f (η a)) :=
equiv.MK (λH, ⟨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)
protected definition 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, Σ(η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a))}
local attribute is_contr_right_inverse [instance] [priority 1600]
local attribute is_contr_right_coherence [instance] [priority 1600]
theorem is_hprop_is_equiv [instance] : is_hprop (is_equiv f) :=
is_hprop_of_imp_is_contr (λ(H : is_equiv f), is_trunc_equiv_closed -2 (equiv.symm !is_equiv.sigma_char'))
definition 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_hprop.elim
/- contractible fibers -/
definition is_contr_fun [reducible] (f : A → B) := Π(b : B), is_contr (fiber f b)
definition is_contr_fun_of_is_equiv [H : is_equiv f] : is_contr_fun f :=
is_contr_fiber_of_is_equiv f
definition is_hprop_is_contr_fun (f : A → B) : is_hprop (is_contr_fun f) := _
/-
we cannot make the next theorem an instance, because it loops together with
is_contr_fiber_of_is_equiv
-/
definition is_equiv_of_is_contr_fun [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)))
definition 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) _)
definition is_equiv_equiv_is_contr_fun : is_equiv f ≃ is_contr_fun f :=
equiv_of_is_hprop _ (λH, !is_equiv_of_is_contr_fun)
end is_equiv
namespace equiv
open is_equiv
variables {A B : Type}
definition 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_hprop.elim
definition equiv_eq {f f' : A ≃ B} (p : to_fun f = to_fun f') : f = f' :=
by (cases f; cases f'; apply (equiv_mk_eq p))
protected definition equiv.sigma_char [constructor]
(A B : Type) : (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
definition 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.refl
definition 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_hprop.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_hprop.elim H H'))), {apply idp},
generalize is_hprop.elim H H', intro q, cases q, apply idp},
{intro p, cases p, cases f with f H, apply ap (ap (equiv.mk f)), apply is_hset.elim}
end
definition 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] g :=
begin
fapply change_path_equiv',
{ intro a, apply equiv.sigma_char},
{ fapply sigma_pathover,
esimp, apply arrow_pathover, exact r,
apply is_hprop.elimo}
end
end equiv
|
28399e31b082023c62b0a976fc4461023bc3fb0f | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/topology/algebra/group_completion.lean | 6eea2c29bb689517815919b015a1e9e400d035bf | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,007 | 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
Completion of topological groups:
-/
import algebra.group.hom_instances
import topology.uniform_space.completion
import topology.algebra.uniform_group
noncomputable theory
universes u v
section group
open uniform_space Cauchy filter set
variables {α : Type u} [uniform_space α]
instance [has_zero α] : has_zero (completion α) := ⟨(0 : α)⟩
instance [has_neg α] : has_neg (completion α) := ⟨completion.map (λa, -a : α → α)⟩
instance [has_add α] : has_add (completion α) := ⟨completion.map₂ (+)⟩
instance [has_sub α] : has_sub (completion α) := ⟨completion.map₂ has_sub.sub⟩
@[norm_cast]
lemma uniform_space.completion.coe_zero [has_zero α] : ((0 : α) : completion α) = 0 := rfl
end group
namespace uniform_space.completion
section uniform_add_group
open uniform_space uniform_space.completion
variables {α : Type*} [uniform_space α] [add_group α] [uniform_add_group α]
@[norm_cast]
lemma coe_neg (a : α) : ((- a : α) : completion α) = - a :=
(map_coe uniform_continuous_neg a).symm
@[norm_cast]
lemma coe_sub (a b : α) : ((a - b : α) : completion α) = a - b :=
(map₂_coe_coe a b has_sub.sub uniform_continuous_sub).symm
@[norm_cast]
lemma coe_add (a b : α) : ((a + b : α) : completion α) = a + b :=
(map₂_coe_coe a b (+) uniform_continuous_add).symm
instance : add_monoid (completion α) :=
{ zero_add := assume a, completion.induction_on a
(is_closed_eq (continuous_map₂ continuous_const continuous_id) continuous_id)
(assume a, show 0 + (a : completion α) = a, by rw_mod_cast zero_add),
add_zero := assume a, completion.induction_on a
(is_closed_eq (continuous_map₂ continuous_id continuous_const) continuous_id)
(assume a, show (a : completion α) + 0 = a, by rw_mod_cast add_zero),
add_assoc := assume a b c, completion.induction_on₃ a b c
(is_closed_eq
(continuous_map₂
(continuous_map₂ continuous_fst (continuous_fst.comp continuous_snd))
(continuous_snd.comp continuous_snd))
(continuous_map₂ continuous_fst
(continuous_map₂
(continuous_fst.comp continuous_snd)
(continuous_snd.comp continuous_snd))))
(assume a b c, show (a : completion α) + b + c = a + (b + c),
by repeat { rw_mod_cast add_assoc }),
.. completion.has_zero, .. completion.has_neg, ..completion.has_add, .. completion.has_sub }
instance : sub_neg_monoid (completion α) :=
{ sub_eq_add_neg := λ a b, completion.induction_on₂ a b
(is_closed_eq (continuous_map₂ continuous_fst continuous_snd)
(continuous_map₂ continuous_fst (continuous_map.comp continuous_snd)))
(λ a b, by exact_mod_cast congr_arg coe (sub_eq_add_neg a b)),
.. completion.add_monoid, .. completion.has_neg, .. completion.has_sub }
instance : add_group (completion α) :=
{ add_left_neg := assume a, completion.induction_on a
(is_closed_eq (continuous_map₂ completion.continuous_map continuous_id) continuous_const)
(assume a, show - (a : completion α) + a = 0, by { rw_mod_cast add_left_neg, refl }),
.. completion.sub_neg_monoid }
instance : uniform_add_group (completion α) :=
⟨uniform_continuous_map₂ has_sub.sub⟩
/-- The map from a group to its completion as a group hom. -/
@[simps] def to_compl : α →+ completion α :=
{ to_fun := coe,
map_add' := coe_add,
map_zero' := coe_zero }
lemma continuous_to_compl : continuous (to_compl : α → completion α) :=
continuous_coe α
variables {β : Type v} [uniform_space β] [add_group β] [uniform_add_group β]
instance {α : Type u} [uniform_space α] [add_comm_group α] [uniform_add_group α] :
add_comm_group (completion α) :=
{ add_comm := assume a b, completion.induction_on₂ a b
(is_closed_eq (continuous_map₂ continuous_fst continuous_snd)
(continuous_map₂ continuous_snd continuous_fst))
(assume x y, by { change ↑x + ↑y = ↑y + ↑x, rw [← coe_add, ← coe_add, add_comm]}),
.. completion.add_group }
end uniform_add_group
end uniform_space.completion
section add_monoid_hom
variables {α β : Type*} [uniform_space α] [add_group α] [uniform_add_group α]
[uniform_space β] [add_group β] [uniform_add_group β]
open uniform_space uniform_space.completion
/-- Extension to the completion of a continuous group hom. -/
def add_monoid_hom.extension [complete_space β] [separated_space β] (f : α →+ β)
(hf : continuous f) : completion α →+ β :=
have hf : uniform_continuous f, from uniform_continuous_of_continuous hf,
{ to_fun := completion.extension f,
map_zero' := by rw [← coe_zero, extension_coe hf, f.map_zero],
map_add' := assume a b, completion.induction_on₂ a b
(is_closed_eq
(continuous_extension.comp continuous_add)
((continuous_extension.comp continuous_fst).add (continuous_extension.comp continuous_snd)))
(λ a b, by rw_mod_cast [extension_coe hf, extension_coe hf, extension_coe hf,
f.map_add]) }
lemma add_monoid_hom.extension_coe [complete_space β] [separated_space β] (f : α →+ β)
(hf : continuous f) (a : α) : f.extension hf a = f a :=
extension_coe (uniform_continuous_of_continuous hf) a
@[continuity]
lemma add_monoid_hom.continuous_extension [complete_space β] [separated_space β] (f : α →+ β)
(hf : continuous f) : continuous (f.extension hf) :=
continuous_extension
/-- Completion of a continuous group hom, as a group hom. -/
def add_monoid_hom.completion (f : α →+ β) (hf : continuous f) : completion α →+ completion β :=
(to_compl.comp f).extension (continuous_to_compl.comp hf)
@[continuity]
lemma add_monoid_hom.continuous_completion (f : α →+ β)
(hf : continuous f) : continuous (f.completion hf : completion α → completion β) :=
continuous_map
lemma add_monoid_hom.completion_coe (f : α →+ β)
(hf : continuous f) (a : α) : f.completion hf a = f a :=
map_coe (uniform_continuous_of_continuous hf) a
lemma add_monoid_hom.completion_zero : (0 : α →+ β).completion continuous_const = 0 :=
begin
ext x,
apply completion.induction_on x,
{ apply is_closed_eq ((0 : α →+ β).continuous_completion continuous_const),
simp [continuous_const] },
{ intro a,
simp [(0 : α →+ β).completion_coe continuous_const, coe_zero] }
end
lemma add_monoid_hom.completion_add {γ : Type*} [add_comm_group γ] [uniform_space γ]
[uniform_add_group γ] (f g : α →+ γ) (hf : continuous f) (hg : continuous g) :
(f + g).completion (hf.add hg) = f.completion hf + g.completion hg :=
begin
have hfg := hf.add hg,
ext x,
apply completion.induction_on x,
{ exact is_closed_eq ((f+g).continuous_completion hfg)
((f.continuous_completion hf).add (g.continuous_completion hg)) },
{ intro a,
simp [(f+g).completion_coe hfg, coe_add, f.completion_coe hf, g.completion_coe hg] }
end
end add_monoid_hom
|
773f768472f06ad3055b8d1e48b678f745248de2 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /src/Lean/Meta/ACLt.lean | d7dd4d3808dbf6b38f31ebd5c4df8df7ba847d58 | [
"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 | 5,211 | lean | /-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Basic
import Lean.Meta.FunInfo
namespace Lean
def Expr.ctorWeight : Expr → UInt8
| bvar .. => 0
| fvar .. => 1
| mvar .. => 2
| sort .. => 3
| const .. => 4
| lit .. => 5
| mdata .. => 6
| proj .. => 7
| app .. => 8
| lam .. => 9
| forallE .. => 10
| letE .. => 11
namespace Meta
namespace ACLt
mutual
/--
An AC-compatible ordering.
Recall that an AC-compatible ordering if it is monotonic, well-founded, and total.
Both KBO and LPO are AC-compatible. KBO is faster, but we do not cache the weight of
each expression in Lean 4. Even if we did, we would need to have a weight where implicit instace arguments are ignored.
So, we use a LPO-like term ordering.
Remark: this method is used to implement ordered rewriting. We ignore implicit instance
arguments to address an issue reported at issue #972.
Remark: the order is not really total on terms since
- We instance implicit arguments.
- We ignore metadata.
- We ignore universe parameterst at constants.
-/
unsafe def lt (a b : Expr) : MetaM Bool :=
if ptrAddrUnsafe a == ptrAddrUnsafe b then
return false
-- We ignore metadata
else if a.isMData then
lt a.mdataExpr! b
else if b.isMData then
lt a b.mdataExpr!
else
lpo a b
where
ltPair (a₁ a₂ b₁ b₂ : Expr) : MetaM Bool := do
if (← lt a₁ b₁) then
return true
else if (← lt b₁ a₁) then
return false
else
lt a₂ b₂
ltApp (a b : Expr) : MetaM Bool := do
let aFn := a.getAppFn
let bFn := b.getAppFn
if (← lt aFn bFn) then
return true
else if (← lt bFn aFn) then
return false
else
let aArgs := a.getAppArgs
let bArgs := b.getAppArgs
if aArgs.size < bArgs.size then
return true
else if aArgs.size > bArgs.size then
return false
else
let infos := (← getFunInfoNArgs aFn aArgs.size).paramInfo
for i in [:infos.size] do
-- We ignore instance implicit arguments during comparison
if !infos[i]!.isInstImplicit then
if (← lt aArgs[i]! bArgs[i]!) then
return true
else if (← lt bArgs[i]! aArgs[i]!) then
return false
for i in [infos.size:aArgs.size] do
if (← lt aArgs[i]! bArgs[i]!) then
return true
else if (← lt bArgs[i]! aArgs[i]!) then
return false
return false
lexSameCtor (a b : Expr) : MetaM Bool :=
match a with
-- Atomic
| Expr.bvar i .. => return i < b.bvarIdx!
| Expr.fvar id .. => return Name.lt id.name b.fvarId!.name
| Expr.mvar id .. => return Name.lt id.name b.mvarId!.name
| Expr.sort u .. => return Level.normLt u b.sortLevel!
| Expr.const n .. => return Name.lt n b.constName! -- We igore the levels
| Expr.lit v .. => return Literal.lt v b.litValue!
-- Composite
| Expr.proj _ i e .. => if i != b.projIdx! then return i < b.projIdx! else lt e b.projExpr!
| Expr.app .. => ltApp a b
| Expr.lam _ d e .. => ltPair d e b.bindingDomain! b.bindingBody!
| Expr.forallE _ d e .. => ltPair d e b.bindingDomain! b.bindingBody!
| Expr.letE _ _ v e .. => ltPair v e b.letValue! b.letBody!
-- See main function
| Expr.mdata .. => unreachable!
allChildrenLt (a b : Expr) : MetaM Bool :=
match a with
| Expr.proj _ _ e .. => lt e b
| Expr.app .. =>
a.withApp fun f args => do
let infos := (← getFunInfoNArgs f args.size).paramInfo
for i in [:infos.size] do
-- We ignore instance implicit arguments during comparison
if !infos[i]!.isInstImplicit then
if !(← lt args[i]! b) then
return false
for i in [infos.size:args.size] do
if !(← lt args[i]! b) then
return false
return true
| Expr.lam _ d e .. => lt d b <&&> lt e b
| Expr.forallE _ d e .. => lt d b <&&> lt e b
| Expr.letE _ _ v e .. => lt v b <&&> lt e b
| _ => return true
someChildGe (a b : Expr) : MetaM Bool :=
return !(← allChildrenLt a b)
lpo (a b : Expr) : MetaM Bool := do
-- Case 1: `a < b` if for some child `b_i` of `b`, we have `b_i >= a`
if (← someChildGe b a) then
return true
else if a.ctorWeight > b.ctorWeight then
return false
else
-- Case 2: `a < b` if `a.ctorWeight < b.ctorWeight` and for all children `a_i` of `a`, `a_i < b`
-- Case 3: `a < b` if `a` & `b` have the same ctor, and `a` is lexicographically smaller, and for all children `a_i` of a, `a_i < b`
if !(← allChildrenLt a b) then
return false -- Cases 2 and 3 can't be true
else if a.ctorWeight < b.ctorWeight then
return true -- Case 2
else
-- `a.ctorWeight == b.ctorWeight`
lexSameCtor a b -- Case 3
end
end ACLt
@[implementedBy ACLt.lt]
opaque Expr.acLt : Expr → Expr → MetaM Bool
end Lean.Meta
|
cd2446e0ed1b247bd97b8de8ec38451b1713d129 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/ring_theory/discrete_valuation_ring.lean | 128b56062228a1d643417ad8417e931f6dd8a7bb | [] | 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,662 | lean | /-
Copyright (c) 2020 Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.ring_theory.principal_ideal_domain
import Mathlib.order.conditionally_complete_lattice
import Mathlib.ring_theory.multiplicity
import Mathlib.ring_theory.valuation.basic
import Mathlib.PostPort
universes u l u_1
namespace Mathlib
/-!
# Discrete valuation rings
This file defines discrete valuation rings (DVRs) and develops a basic interface
for them.
## Important definitions
There are various definitions of a DVR in the literature; we define a DVR to be a local PID
which is not a field (the first definition in Wikipedia) and prove that this is equivalent
to being a PID with a unique non-zero prime ideal (the definition in Serre's
book "Local Fields").
Let R be an integral domain, assumed to be a principal ideal ring and a local ring.
* `discrete_valuation_ring R` : a predicate expressing that R is a DVR
### Definitions
* `add_val R : R → ℕ` : the additive valuation on a DVR (sending 0 to 0 rather than the
mathematically correct +∞).
TODO -- the multiplicative valuation, taking values in something
like `with_zero (multiplicative ℤ)`?
## Implementation notes
It's a theorem that an element of a DVR is a uniformizer if and only if it's irreducible.
We do not hence define `uniformizer` at all, because we can use `irreducible` instead.
## Tags
discrete valuation ring
-/
/-- An integral domain is a *discrete valuation ring* (DVR) if it's a local PID which
is not a field. -/
class discrete_valuation_ring (R : Type u) [integral_domain R]
extends is_principal_ideal_ring R, local_ring R
where
not_a_field' : local_ring.maximal_ideal R ≠ ⊥
namespace discrete_valuation_ring
theorem not_a_field (R : Type u) [integral_domain R] [discrete_valuation_ring R] : local_ring.maximal_ideal R ≠ ⊥ :=
not_a_field'
/-- An element of a DVR is irreducible iff it is a uniformizer, that is, generates the
maximal ideal of R -/
theorem irreducible_iff_uniformizer {R : Type u} [integral_domain R] [discrete_valuation_ring R] (ϖ : R) : irreducible ϖ ↔ local_ring.maximal_ideal R = ideal.span (singleton ϖ) := sorry
/-- Uniformisers exist in a DVR -/
theorem exists_irreducible (R : Type u) [integral_domain R] [discrete_valuation_ring R] : ∃ (ϖ : R), irreducible ϖ := sorry
/-- an integral domain is a DVR iff it's a PID with a unique non-zero prime ideal -/
theorem iff_pid_with_one_nonzero_prime (R : Type u) [integral_domain R] : discrete_valuation_ring R ↔ is_principal_ideal_ring R ∧ exists_unique fun (P : ideal R) => P ≠ ⊥ ∧ ideal.is_prime P := sorry
theorem associated_of_irreducible (R : Type u) [integral_domain R] [discrete_valuation_ring R] {a : R} {b : R} (ha : irreducible a) (hb : irreducible b) : associated a b := sorry
end discrete_valuation_ring
namespace discrete_valuation_ring
/-- Alternative characterisation of discrete valuation rings. -/
def has_unit_mul_pow_irreducible_factorization (R : Type u_1) [integral_domain R] :=
∃ (p : R), irreducible p ∧ ∀ {x : R}, x ≠ 0 → ∃ (n : ℕ), associated (p ^ n) x
namespace has_unit_mul_pow_irreducible_factorization
theorem unique_irreducible {R : Type u_1} [integral_domain R] (hR : has_unit_mul_pow_irreducible_factorization R) {p : R} {q : R} (hp : irreducible p) (hq : irreducible q) : associated p q := sorry
/-- An integral domain in which there is an irreducible element `p`
such that every nonzero element is associated to a power of `p` is a unique factorization domain.
See `discrete_valuation_ring.of_has_unit_mul_pow_irreducible_factorization`. -/
theorem to_unique_factorization_monoid {R : Type u_1} [integral_domain R] (hR : has_unit_mul_pow_irreducible_factorization R) : unique_factorization_monoid R := sorry
theorem of_ufd_of_unique_irreducible {R : Type u_1} [integral_domain R] [unique_factorization_monoid R] (h₁ : ∃ (p : R), irreducible p) (h₂ : ∀ {p q : R}, irreducible p → irreducible q → associated p q) : has_unit_mul_pow_irreducible_factorization R := sorry
end has_unit_mul_pow_irreducible_factorization
theorem aux_pid_of_ufd_of_unique_irreducible (R : Type u) [integral_domain R] [unique_factorization_monoid R] (h₁ : ∃ (p : R), irreducible p) (h₂ : ∀ {p q : R}, irreducible p → irreducible q → associated p q) : is_principal_ideal_ring R := sorry
/--
A unique factorization domain with at least one irreducible element
in which all irreducible elements are associated
is a discrete valuation ring.
-/
theorem of_ufd_of_unique_irreducible {R : Type u} [integral_domain R] [unique_factorization_monoid R] (h₁ : ∃ (p : R), irreducible p) (h₂ : ∀ {p q : R}, irreducible p → irreducible q → associated p q) : discrete_valuation_ring R := sorry
/--
An integral domain in which there is an irreducible element `p`
such that every nonzero element is associated to a power of `p`
is a discrete valuation ring.
-/
theorem of_has_unit_mul_pow_irreducible_factorization {R : Type u} [integral_domain R] (hR : has_unit_mul_pow_irreducible_factorization R) : discrete_valuation_ring R := sorry
theorem associated_pow_irreducible {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] {x : R} (hx : x ≠ 0) {ϖ : R} (hirr : irreducible ϖ) : ∃ (n : ℕ), associated x (ϖ ^ n) := sorry
theorem eq_unit_mul_pow_irreducible {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] {x : R} (hx : x ≠ 0) {ϖ : R} (hirr : irreducible ϖ) : ∃ (n : ℕ), ∃ (u : units R), x = ↑u * ϖ ^ n := sorry
theorem ideal_eq_span_pow_irreducible {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] {s : ideal R} (hs : s ≠ ⊥) {ϖ : R} (hirr : irreducible ϖ) : ∃ (n : ℕ), s = ideal.span (singleton (ϖ ^ n)) := sorry
theorem unit_mul_pow_congr_pow {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] {p : R} {q : R} (hp : irreducible p) (hq : irreducible q) (u : units R) (v : units R) (m : ℕ) (n : ℕ) (h : ↑u * p ^ m = ↑v * q ^ n) : m = n := sorry
theorem unit_mul_pow_congr_unit {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] {ϖ : R} (hirr : irreducible ϖ) (u : units R) (v : units R) (m : ℕ) (n : ℕ) (h : ↑u * ϖ ^ m = ↑v * ϖ ^ n) : u = v := sorry
/-!
## The additive valuation on a DVR
-/
/-- The `ℕ`-valued additive valuation on a DVR (returns junk at `0` rather than `+∞`) -/
def add_val (R : Type u) [integral_domain R] [discrete_valuation_ring R] : R → ℕ :=
fun (r : R) => dite (r = 0) (fun (hr : r = 0) => 0) fun (hr : ¬r = 0) => classical.some sorry
theorem add_val_spec {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] {r : R} (hr : r ≠ 0) : let ϖ : R := classical.some (exists_irreducible R);
let n : ℕ := classical.some (associated_pow_irreducible hr (classical.some_spec (exists_irreducible R)));
associated r (ϖ ^ n) :=
classical.some_spec (associated_pow_irreducible hr (classical.some_spec (exists_irreducible R)))
theorem add_val_def {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] (r : R) (u : units R) {ϖ : R} (hϖ : irreducible ϖ) (n : ℕ) (hr : r = ↑u * ϖ ^ n) : add_val R r = n := sorry
theorem add_val_def' {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] (u : units R) {ϖ : R} (hϖ : irreducible ϖ) (n : ℕ) : add_val R (↑u * ϖ ^ n) = n :=
add_val_def (↑u * ϖ ^ n) u hϖ n rfl
@[simp] theorem add_val_zero {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] : add_val R 0 = 0 :=
dif_pos rfl
@[simp] theorem add_val_one {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] : add_val R 1 = 0 := sorry
@[simp] theorem add_val_uniformizer {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] {ϖ : R} (hϖ : irreducible ϖ) : add_val R ϖ = 1 := sorry
@[simp] theorem add_val_mul {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] {a : R} {b : R} (ha : a ≠ 0) (hb : b ≠ 0) : add_val R (a * b) = add_val R a + add_val R b := sorry
theorem add_val_pow {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] (a : R) (n : ℕ) : add_val R (a ^ n) = n * add_val R a := sorry
theorem add_val_le_iff_dvd {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] {a : R} {b : R} (ha : a ≠ 0) (hb : b ≠ 0) : add_val R a ≤ add_val R b ↔ a ∣ b := sorry
theorem add_val_add {R : Type u_1} [integral_domain R] [discrete_valuation_ring R] {a : R} {b : R} (ha : a ≠ 0) (hb : b ≠ 0) (hab : a + b ≠ 0) : min (add_val R a) (add_val R b) ≤ add_val R (a + b) := sorry
|
73803d17e4dc8a232bdbbfd69266faf76feaa687 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/no_confusion_bug.lean | 6e266f8e532b7b20b0baa558d52e686e47a82e76 | [
"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 | 330 | lean | open nat
inductive Fin : nat → Type
| fz : Π {n : nat}, Fin (succ n)
| fs : Π {n : nat}, Fin n → Fin (succ n)
namespace Fin
inductive le : ∀ {n : nat}, Fin n → Fin n → Prop
| lez : ∀ {n : nat} (j : Fin (succ n)), le fz j
| les : ∀ {n : nat} {i j : Fin n}, le i j → le (fs i) (fs j)
end Fin
|
70d641cf5157f63bb261a9c23fe06a9490a76aa4 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/algebra/order/absolute_value.lean | e17a13b6e4dd630c1d2719fbf8da29ff8672bfd0 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 8,983 | lean | /-
Copyright (c) 2021 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Anne Baanen
-/
import algebra.order.field
/-!
# Absolute values
This file defines a bundled type of absolute values `absolute_value R S`.
## Main definitions
* `absolute_value R S` is the type of absolute values on `R` mapping to `S`.
* `absolute_value.abs` is the "standard" absolute value on `S`, mapping negative `x` to `-x`.
* `absolute_value.to_monoid_with_zero_hom`: absolute values mapping to a
linear ordered field preserve `0`, `*` and `1`
* `is_absolute_value`: a type class stating that `f : β → α` satisfies the axioms of an abs val
-/
/-- `absolute_value R S` is the type of absolute values on `R` mapping to `S`:
the maps that preserve `*`, are nonnegative, positive definite and satisfy the triangle equality. -/
structure absolute_value (R S : Type*) [semiring R] [ordered_semiring S]
extends R →ₙ* S :=
(nonneg' : ∀ x, 0 ≤ to_fun x)
(eq_zero' : ∀ x, to_fun x = 0 ↔ x = 0)
(add_le' : ∀ x y, to_fun (x + y) ≤ to_fun x + to_fun y)
namespace absolute_value
attribute [nolint doc_blame] absolute_value.to_mul_hom
initialize_simps_projections absolute_value (to_mul_hom_to_fun → apply)
section ordered_semiring
variables {R S : Type*} [semiring R] [ordered_semiring S] (abv : absolute_value R S)
instance : has_coe_to_fun (absolute_value R S) (λ f, R → S) := ⟨λ f, f.to_fun⟩
@[simp] lemma coe_to_mul_hom : ⇑abv.to_mul_hom = abv := rfl
protected theorem nonneg (x : R) : 0 ≤ abv x := abv.nonneg' x
@[simp] protected theorem eq_zero {x : R} : abv x = 0 ↔ x = 0 := abv.eq_zero' x
protected theorem add_le (x y : R) : abv (x + y) ≤ abv x + abv y := abv.add_le' x y
@[simp] protected theorem map_mul (x y : R) : abv (x * y) = abv x * abv y := abv.map_mul' x y
protected theorem pos {x : R} (hx : x ≠ 0) : 0 < abv x :=
lt_of_le_of_ne (abv.nonneg x) (ne.symm $ mt abv.eq_zero.mp hx)
@[simp] protected theorem pos_iff {x : R} : 0 < abv x ↔ x ≠ 0 :=
⟨λ h₁, mt abv.eq_zero.mpr h₁.ne', abv.pos⟩
protected theorem ne_zero {x : R} (hx : x ≠ 0) : abv x ≠ 0 := (abv.pos hx).ne'
@[simp] protected theorem map_zero : abv 0 = 0 := abv.eq_zero.2 rfl
end ordered_semiring
section ordered_ring
variables {R S : Type*} [ring R] [ordered_ring S] (abv : absolute_value R S)
protected lemma sub_le (a b c : R) : abv (a - c) ≤ abv (a - b) + abv (b - c) :=
by simpa [sub_eq_add_neg, add_assoc] using abv.add_le (a - b) (b - c)
protected lemma le_sub (a b : R) : abv a - abv b ≤ abv (a - b) :=
sub_le_iff_le_add.2 $ by simpa using abv.add_le (a - b) b
@[simp] lemma map_sub_eq_zero_iff (a b : R) : abv (a - b) = 0 ↔ a = b :=
abv.eq_zero.trans sub_eq_zero
end ordered_ring
section linear_ordered_ring
variables {R S : Type*} [semiring R] [linear_ordered_ring S] (abv : absolute_value R S)
/-- `absolute_value.abs` is `abs` as a bundled `absolute_value`. -/
@[simps]
protected def abs : absolute_value S S :=
{ to_fun := abs,
nonneg' := abs_nonneg,
eq_zero' := λ _, abs_eq_zero,
add_le' := abs_add,
map_mul' := abs_mul }
instance : inhabited (absolute_value S S) := ⟨absolute_value.abs⟩
variables [nontrivial R]
@[simp] protected theorem map_one : abv 1 = 1 :=
(mul_right_inj' $ abv.ne_zero one_ne_zero).1 $
by rw [← abv.map_mul, mul_one, mul_one]
/-- Absolute values from a nontrivial `R` to a linear ordered ring preserve `*`, `0` and `1`. -/
def to_monoid_with_zero_hom : R →*₀ S :=
{ to_fun := abv,
map_zero' := abv.map_zero,
map_one' := abv.map_one,
.. abv }
@[simp] lemma coe_to_monoid_with_zero_hom : ⇑abv.to_monoid_with_zero_hom = abv := rfl
/-- Absolute values from a nontrivial `R` to a linear ordered ring preserve `*` and `1`. -/
def to_monoid_hom : monoid_hom R S :=
{ to_fun := abv,
map_one' := abv.map_one,
.. abv }
@[simp] lemma coe_to_monoid_hom : ⇑abv.to_monoid_hom = abv := rfl
@[simp] protected lemma map_pow (a : R) (n : ℕ) : abv (a ^ n) = abv a ^ n :=
abv.to_monoid_hom.map_pow a n
end linear_ordered_ring
section linear_ordered_comm_ring
section ring
variables {R S : Type*} [ring R] [linear_ordered_comm_ring S] (abv : absolute_value R S)
@[simp] protected theorem map_neg (a : R) : abv (-a) = abv a :=
begin
by_cases ha : a = 0, { simp [ha] },
refine (mul_self_eq_mul_self_iff.mp
(by rw [← abv.map_mul, neg_mul_neg, abv.map_mul])).resolve_right _,
exact ((neg_lt_zero.mpr (abv.pos ha)).trans (abv.pos (neg_ne_zero.mpr ha))).ne'
end
protected theorem map_sub (a b : R) : abv (a - b) = abv (b - a) :=
by rw [← neg_sub, abv.map_neg]
lemma abs_abv_sub_le_abv_sub (a b : R) :
abs (abv a - abv b) ≤ abv (a - b) :=
abs_sub_le_iff.2 ⟨abv.le_sub _ _, by rw abv.map_sub; apply abv.le_sub⟩
end ring
end linear_ordered_comm_ring
section linear_ordered_field
section field
variables {R S : Type*} [division_ring R] [linear_ordered_field S] (abv : absolute_value R S)
@[simp] protected theorem map_inv (a : R) : abv a⁻¹ = (abv a)⁻¹ :=
abv.to_monoid_with_zero_hom.map_inv a
@[simp] protected theorem map_div (a b : R) : abv (a / b) = abv a / abv b :=
abv.to_monoid_with_zero_hom.map_div a b
end field
end linear_ordered_field
end absolute_value
section is_absolute_value
/-- A function `f` is an absolute value if it is nonnegative, zero only at 0, additive, and
multiplicative.
See also the type `absolute_value` which represents a bundled version of absolute values.
-/
class is_absolute_value {S} [ordered_semiring S]
{R} [semiring R] (f : R → S) : Prop :=
(abv_nonneg [] : ∀ x, 0 ≤ f x)
(abv_eq_zero [] : ∀ {x}, f x = 0 ↔ x = 0)
(abv_add [] : ∀ x y, f (x + y) ≤ f x + f y)
(abv_mul [] : ∀ x y, f (x * y) = f x * f y)
namespace is_absolute_value
section ordered_semiring
variables {S : Type*} [ordered_semiring S]
variables {R : Type*} [semiring R] (abv : R → S) [is_absolute_value abv]
/-- A bundled absolute value is an absolute value. -/
instance absolute_value.is_absolute_value
(abv : absolute_value R S) : is_absolute_value abv :=
{ abv_nonneg := abv.nonneg,
abv_eq_zero := λ _, abv.eq_zero,
abv_add := abv.add_le,
abv_mul := abv.map_mul }
/-- Convert an unbundled `is_absolute_value` to a bundled `absolute_value`. -/
@[simps]
def to_absolute_value : absolute_value R S :=
{ to_fun := abv,
add_le' := abv_add abv,
eq_zero' := λ _, abv_eq_zero abv,
nonneg' := abv_nonneg abv,
map_mul' := abv_mul abv }
theorem abv_zero : abv 0 = 0 := (abv_eq_zero abv).2 rfl
theorem abv_pos {a : R} : 0 < abv a ↔ a ≠ 0 :=
by rw [lt_iff_le_and_ne, ne, eq_comm]; simp [abv_eq_zero abv, abv_nonneg abv]
end ordered_semiring
section linear_ordered_ring
variables {S : Type*} [linear_ordered_ring S]
variables {R : Type*} [semiring R] (abv : R → S) [is_absolute_value abv]
instance abs_is_absolute_value {S} [linear_ordered_ring S] :
is_absolute_value (abs : S → S) :=
{ abv_nonneg := abs_nonneg,
abv_eq_zero := λ _, abs_eq_zero,
abv_add := abs_add,
abv_mul := abs_mul }
end linear_ordered_ring
section linear_ordered_comm_ring
variables {S : Type*} [linear_ordered_comm_ring S]
section semiring
variables {R : Type*} [semiring R] (abv : R → S) [is_absolute_value abv]
theorem abv_one [nontrivial R] : abv 1 = 1 :=
(mul_right_inj' $ mt (abv_eq_zero abv).1 one_ne_zero).1 $
by rw [← abv_mul abv, mul_one, mul_one]
/-- `abv` as a `monoid_with_zero_hom`. -/
def abv_hom [nontrivial R] : R →*₀ S := ⟨abv, abv_zero abv, abv_one abv, abv_mul abv⟩
lemma abv_pow [nontrivial R] (abv : R → S) [is_absolute_value abv]
(a : R) (n : ℕ) : abv (a ^ n) = abv a ^ n :=
(abv_hom abv).to_monoid_hom.map_pow a n
end semiring
end linear_ordered_comm_ring
section linear_ordered_field
variables {S : Type*} [linear_ordered_field S]
section ring
variables {R : Type*} [ring R] (abv : R → S) [is_absolute_value abv]
theorem abv_neg (a : R) : abv (-a) = abv a :=
by rw [← mul_self_inj_of_nonneg (abv_nonneg abv _) (abv_nonneg abv _),
← abv_mul abv, ← abv_mul abv]; simp
theorem abv_sub (a b : R) : abv (a - b) = abv (b - a) :=
by rw [← neg_sub, abv_neg abv]
lemma abv_sub_le (a b c : R) : abv (a - c) ≤ abv (a - b) + abv (b - c) :=
by simpa [sub_eq_add_neg, add_assoc] using abv_add abv (a - b) (b - c)
lemma sub_abv_le_abv_sub (a b : R) : abv a - abv b ≤ abv (a - b) :=
sub_le_iff_le_add.2 $ by simpa using abv_add abv (a - b) b
lemma abs_abv_sub_le_abv_sub (a b : R) :
abs (abv a - abv b) ≤ abv (a - b) :=
abs_sub_le_iff.2 ⟨sub_abv_le_abv_sub abv _ _,
by rw abv_sub abv; apply sub_abv_le_abv_sub abv⟩
end ring
section field
variables {R : Type*} [division_ring R] (abv : R → S) [is_absolute_value abv]
theorem abv_inv (a : R) : abv a⁻¹ = (abv a)⁻¹ :=
(abv_hom abv).map_inv a
theorem abv_div (a b : R) : abv (a / b) = abv a / abv b :=
(abv_hom abv).map_div a b
end field
end linear_ordered_field
end is_absolute_value
end is_absolute_value
|
7ad651cd25139675f098b1a6f8a9bcaf2426b4c9 | d1a52c3f208fa42c41df8278c3d280f075eb020c | /stage0/src/Lean/Data/Trie.lean | 9f62b47311297f9970cd3bc57c67a243fb8a1ce5 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | cipher1024/lean4 | 6e1f98bb58e7a92b28f5364eb38a14c8d0aae393 | 69114d3b50806264ef35b57394391c3e738a9822 | refs/heads/master | 1,642,227,983,603 | 1,642,011,696,000 | 1,642,011,696,000 | 228,607,691 | 0 | 0 | Apache-2.0 | 1,576,584,269,000 | 1,576,584,268,000 | null | UTF-8 | Lean | false | false | 3,323 | lean | /-
Copyright (c) 2018 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Sebastian Ullrich, Leonardo de Moura
Trie for tokenizing the Lean language
-/
import Lean.Data.Format
namespace Lean
namespace Parser
open Std (RBNode RBNode.leaf RBNode.singleton RBNode.find RBNode.insert)
inductive Trie (α : Type) where
| Node : Option α → RBNode Char (fun _ => Trie α) → Trie α
namespace Trie
variable {α : Type}
def empty : Trie α :=
⟨none, RBNode.leaf⟩
instance : EmptyCollection (Trie α) :=
⟨empty⟩
instance : Inhabited (Trie α) where
default := Node none RBNode.leaf
partial def insert (t : Trie α) (s : String) (val : α) : Trie α :=
let rec insertEmpty (i : String.Pos) : Trie α :=
match s.atEnd i with
| true => Trie.Node (some val) RBNode.leaf
| false =>
let c := s.get i
let t := insertEmpty (s.next i)
Trie.Node none (RBNode.singleton c t)
let rec loop
| Trie.Node v m, i =>
match s.atEnd i with
| true => Trie.Node (some val) m -- overrides old value
| false =>
let c := s.get i
let i := s.next i
let t := match RBNode.find compare m c with
| none => insertEmpty i
| some t => loop t i
Trie.Node v (RBNode.insert compare m c t)
loop t 0
partial def find? (t : Trie α) (s : String) : Option α :=
let rec loop
| Trie.Node val m, i =>
match s.atEnd i with
| true => val
| false =>
let c := s.get i
let i := s.next i
match RBNode.find compare m c with
| none => none
| some t => loop t i
loop t 0
/-- Return values that match the given `prefix` -/
partial def findPrefix (t : Trie α) (pre : String) : Array α :=
go t 0 |>.run #[] |>.2
where
go (t : Trie α) (i : String.Pos) : StateM (Array α) Unit :=
if pre.atEnd i then
collect t
else
let k := pre.get i
let i := pre.next i
let ⟨_, cs⟩ := t
cs.forM fun k' c => do
if k == k' then go c i
collect (t : Trie α) : StateM (Array α) Unit := do
let ⟨a?, cs⟩ := t
if let some a := a? then
modify (·.push a)
cs.forM fun _ c => collect c
private def updtAcc (v : Option α) (i : String.Pos) (acc : String.Pos × Option α) : String.Pos × Option α :=
match v, acc with
| some v, (j, w) => (i, some v) -- we pattern match on `acc` to enable memory reuse
| none, acc => acc
partial def matchPrefix (s : String) (t : Trie α) (i : String.Pos) : String.Pos × Option α :=
let rec loop
| Trie.Node v m, i, acc =>
match s.atEnd i with
| true => updtAcc v i acc
| false =>
let acc := updtAcc v i acc
let c := s.get i
let i := s.next i
match RBNode.find compare m c with
| some t => loop t i acc
| none => acc
loop t i (i, none)
private partial def toStringAux {α : Type} : Trie α → List Format
| Trie.Node val map => map.fold (fun Fs c t =>
format (repr c) :: (Format.group $ Format.nest 2 $ flip Format.joinSep Format.line $ toStringAux t) :: Fs) []
instance {α : Type} : ToString (Trie α) :=
⟨fun t => (flip Format.joinSep Format.line $ toStringAux t).pretty⟩
end Trie
end Parser
end Lean
|
d15e794ff1b028ee2709fd245826ce65ca9ba6a1 | d436468d80b739ba7e06843c4d0d2070e43448e5 | /src/algebra/gcd_domain.lean | f6169c2eb9c92d37feb5e9bfd46af16c0d328279 | [
"Apache-2.0"
] | permissive | roro47/mathlib | 761fdc002aef92f77818f3fef06bf6ec6fc1a28e | 80aa7d52537571a2ca62a3fdf71c9533a09422cf | refs/heads/master | 1,599,656,410,625 | 1,573,649,488,000 | 1,573,649,488,000 | 221,452,951 | 0 | 0 | Apache-2.0 | 1,573,647,693,000 | 1,573,647,692,000 | null | UTF-8 | Lean | false | false | 26,082 | lean | /-
Copyright (c) 2018 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Jens Wagemaker
GCD domain and integral domains with normalization functions
TODO: abstract the domains to to semi domains (i.e. domains on semirings) to include ℕ and ℕ[X] etc.
-/
import algebra.associated data.int.gcd
variables {α : Type*}
set_option old_structure_cmd true
/-- Normalization domain: multiplying with `norm_unit` gives a normal form for associated elements. -/
class normalization_domain (α : Type*) extends integral_domain α :=
(norm_unit : α → units α)
(norm_unit_zero : norm_unit 0 = 1)
(norm_unit_mul : ∀{a b}, a ≠ 0 → b ≠ 0 → norm_unit (a * b) = norm_unit a * norm_unit b)
(norm_unit_coe_units : ∀(u : units α), norm_unit u = u⁻¹)
export normalization_domain (norm_unit norm_unit_zero norm_unit_mul norm_unit_coe_units)
attribute [simp] norm_unit_coe_units norm_unit_zero norm_unit_mul
section normalization_domain
variable [normalization_domain α]
def normalize (x : α) : α :=
x * norm_unit x
theorem associated_normalize {x : α} : associated x (normalize x) :=
⟨_, rfl⟩
theorem normalize_associated {x : α} : associated (normalize x) x :=
associated_normalize.symm
@[simp] theorem norm_unit_one : norm_unit (1:α) = 1 :=
norm_unit_coe_units 1
@[simp] lemma normalize_zero : normalize (0 : α) = 0 :=
by rw [normalize, zero_mul]
@[simp] lemma normalize_one : normalize (1 : α) = 1 :=
by rw [normalize, norm_unit_one, units.coe_one, mul_one]
lemma normalize_coe_units (u : units α) : normalize (u : α) = 1 :=
by rw [normalize, norm_unit_coe_units, ← units.coe_mul, mul_inv_self, units.coe_one]
theorem normalize_mul (x y : α) : normalize (x * y) = normalize x * normalize y :=
classical.by_cases (λ hx : x = 0, by rw [hx, zero_mul, normalize_zero, zero_mul]) $ λ hx,
classical.by_cases (λ hy : y = 0, by rw [hy, mul_zero, normalize_zero, mul_zero]) $ λ hy,
by simp only [normalize, norm_unit_mul hx hy, units.coe_mul]; simp only [mul_assoc, mul_left_comm y]
lemma normalize_eq_zero {x : α} : normalize x = 0 ↔ x = 0 :=
⟨λ hx, (associated_zero_iff_eq_zero x).1 $ hx ▸ associated_normalize, by rintro rfl; exact normalize_zero⟩
lemma normalize_eq_one {x : α} : normalize x = 1 ↔ is_unit x :=
⟨λ hx, is_unit_iff_exists_inv.2 ⟨_, hx⟩, λ ⟨u, hu⟩, hu.symm ▸ normalize_coe_units u⟩
theorem norm_unit_mul_norm_unit (a : α) : norm_unit (a * norm_unit a) = 1 :=
classical.by_cases (assume : a = 0, by simp only [this, norm_unit_zero, zero_mul]) $
assume h, by rw [norm_unit_mul h (units.ne_zero _), norm_unit_coe_units, mul_inv_eq_one]
theorem normalize_idem (x : α) : normalize (normalize x) = normalize x :=
by rw [normalize, normalize, norm_unit_mul_norm_unit, units.coe_one, mul_one]
theorem normalize_eq_normalize {a b : α}
(hab : a ∣ b) (hba : b ∣ a) : normalize a = normalize b :=
begin
rcases associated_of_dvd_dvd hab hba with ⟨u, rfl⟩,
refine classical.by_cases (by rintro rfl; simp only [zero_mul]) (assume ha : a ≠ 0, _),
suffices : a * ↑(norm_unit a) = a * ↑u * ↑(norm_unit a) * ↑u⁻¹,
by simpa only [normalize, mul_assoc, norm_unit_mul ha u.ne_zero, norm_unit_coe_units],
calc a * ↑(norm_unit a) = a * ↑(norm_unit a) * ↑u * ↑u⁻¹:
(units.mul_inv_cancel_right _ _).symm
... = a * ↑u * ↑(norm_unit a) * ↑u⁻¹ : by rw mul_right_comm a
end
lemma normalize_eq_normalize_iff {x y : α} : normalize x = normalize y ↔ x ∣ y ∧ y ∣ x :=
⟨λ h, ⟨dvd_mul_unit_iff.1 ⟨_, h.symm⟩, dvd_mul_unit_iff.1 ⟨_, h⟩⟩,
λ ⟨hxy, hyx⟩, normalize_eq_normalize hxy hyx⟩
theorem dvd_antisymm_of_normalize_eq {a b : α}
(ha : normalize a = a) (hb : normalize b = b) (hab : a ∣ b) (hba : b ∣ a) :
a = b :=
ha ▸ hb ▸ normalize_eq_normalize hab hba
@[simp] lemma dvd_normalize_iff {a b : α} : a ∣ normalize b ↔ a ∣ b :=
dvd_mul_unit_iff
@[simp] lemma normalize_dvd_iff {a b : α} : normalize a ∣ b ↔ a ∣ b :=
mul_unit_dvd_iff
end normalization_domain
namespace associates
variable [normalization_domain α]
local attribute [instance] associated.setoid
protected def out : associates α → α :=
quotient.lift (normalize : α → α) $ λ a b ⟨u, hu⟩, hu ▸
normalize_eq_normalize ⟨_, rfl⟩ (mul_unit_dvd_iff.2 $ dvd_refl a)
lemma out_mk (a : α) : (associates.mk a).out = normalize a := rfl
@[simp] lemma out_one : (1 : associates α).out = 1 :=
normalize_one
lemma out_mul (a b : associates α) : (a * b).out = a.out * b.out :=
quotient.induction_on₂ a b $ assume a b,
by simp only [associates.quotient_mk_eq_mk, out_mk, mk_mul_mk, normalize_mul]
lemma dvd_out_iff (a : α) (b : associates α) : a ∣ b.out ↔ associates.mk a ≤ b :=
quotient.induction_on b $ by simp [associates.out_mk, associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd_iff]
lemma out_dvd_iff (a : α) (b : associates α) : b.out ∣ a ↔ b ≤ associates.mk a :=
quotient.induction_on b $ by simp [associates.out_mk, associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd_iff]
@[simp] lemma out_top : (⊤ : associates α).out = 0 :=
normalize_zero
@[simp] lemma normalize_out (a : associates α) : normalize a.out = a.out :=
quotient.induction_on a normalize_idem
end associates
/-- GCD domain: an integral domain with normalization and `gcd` (greatest common divisor) and
`lcm` (least common multiple) operations. In this setting `gcd` and `lcm` form a bounded lattice on
the associated elements where `gcd` is the infimum, `lcm` is the supremum, `1` is bottom, and
`0` is top. The type class focuses on `gcd` and we derive the correpsonding `lcm` facts from `gcd`.
-/
class gcd_domain (α : Type*) extends normalization_domain α :=
(gcd : α → α → α)
(lcm : α → α → α)
(gcd_dvd_left : ∀a b, gcd a b ∣ a)
(gcd_dvd_right : ∀a b, gcd a b ∣ b)
(dvd_gcd : ∀{a b c}, a ∣ c → a ∣ b → a ∣ gcd c b)
(normalize_gcd : ∀a b, normalize (gcd a b) = gcd a b)
(gcd_mul_lcm : ∀a b, gcd a b * lcm a b = normalize (a * b))
(lcm_zero_left : ∀a, lcm 0 a = 0)
(lcm_zero_right : ∀a, lcm a 0 = 0)
export gcd_domain (gcd lcm gcd_dvd_left gcd_dvd_right dvd_gcd lcm_zero_left lcm_zero_right)
attribute [simp] lcm_zero_left lcm_zero_right
section gcd_domain
variables [gcd_domain α]
@[simp] theorem normalize_gcd : ∀a b:α, normalize (gcd a b) = gcd a b :=
gcd_domain.normalize_gcd
@[simp] theorem gcd_mul_lcm : ∀a b:α, gcd a b * lcm a b = normalize (a * b) :=
gcd_domain.gcd_mul_lcm
section gcd
theorem dvd_gcd_iff (a b c : α) : a ∣ gcd b c ↔ (a ∣ b ∧ a ∣ c) :=
iff.intro
(assume h, ⟨dvd_trans h (gcd_dvd_left _ _), dvd_trans h (gcd_dvd_right _ _)⟩)
(assume ⟨hab, hac⟩, dvd_gcd hab hac)
theorem gcd_comm (a b : α) : gcd a b = gcd b a :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _)
(dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _))
(dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _))
theorem gcd_assoc (m n k : α) : gcd (gcd m n) k = gcd m (gcd n k) :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _)
(dvd_gcd
(dvd.trans (gcd_dvd_left (gcd m n) k) (gcd_dvd_left m n))
(dvd_gcd (dvd.trans (gcd_dvd_left (gcd m n) k) (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k)))
(dvd_gcd
(dvd_gcd (gcd_dvd_left m (gcd n k)) (dvd.trans (gcd_dvd_right m (gcd n k)) (gcd_dvd_left n k)))
(dvd.trans (gcd_dvd_right m (gcd n k)) (gcd_dvd_right n k)))
instance : is_commutative α gcd := ⟨gcd_comm⟩
instance : is_associative α gcd := ⟨gcd_assoc⟩
theorem gcd_eq_normalize {a b c : α} (habc : gcd a b ∣ c) (hcab : c ∣ gcd a b) :
gcd a b = normalize c :=
normalize_gcd a b ▸ normalize_eq_normalize habc hcab
@[simp] theorem gcd_zero_left (a : α) : gcd 0 a = normalize a :=
gcd_eq_normalize (gcd_dvd_right 0 a) (dvd_gcd (dvd_zero _) (dvd_refl a))
@[simp] theorem gcd_zero_right (a : α) : gcd a 0 = normalize a :=
gcd_eq_normalize (gcd_dvd_left a 0) (dvd_gcd (dvd_refl a) (dvd_zero _))
@[simp] theorem gcd_eq_zero_iff (a b : α) : gcd a b = 0 ↔ a = 0 ∧ b = 0 :=
iff.intro
(assume h, let ⟨ca, ha⟩ := gcd_dvd_left a b, ⟨cb, hb⟩ := gcd_dvd_right a b in
by rw [h, zero_mul] at ha hb; exact ⟨ha, hb⟩)
(assume ⟨ha, hb⟩, by rw [ha, hb, gcd_zero_left, normalize_zero])
@[simp] theorem gcd_one_left (a : α) : gcd 1 a = 1 :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_left _ _) (one_dvd _)
@[simp] theorem gcd_one_right (a : α) : gcd a 1 = 1 :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_right _ _) (one_dvd _)
theorem gcd_dvd_gcd {a b c d: α} (hab : a ∣ b) (hcd : c ∣ d) : gcd a c ∣ gcd b d :=
dvd_gcd (dvd.trans (gcd_dvd_left _ _) hab) (dvd.trans (gcd_dvd_right _ _) hcd)
@[simp] theorem gcd_same (a : α) : gcd a a = normalize a :=
gcd_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_refl a))
@[simp] theorem gcd_mul_left (a b c : α) : gcd (a * b) (a * c) = normalize a * gcd b c :=
classical.by_cases (by rintro rfl; simp only [zero_mul, gcd_zero_left, normalize_zero]) $ assume ha : a ≠ 0,
suffices gcd (a * b) (a * c) = normalize (a * gcd b c),
by simpa only [normalize_mul, normalize_gcd],
let ⟨d, eq⟩ := dvd_gcd (dvd_mul_right a b) (dvd_mul_right a c) in
gcd_eq_normalize
(eq.symm ▸ mul_dvd_mul_left a $ show d ∣ gcd b c, from
dvd_gcd
((mul_dvd_mul_iff_left ha).1 $ eq ▸ gcd_dvd_left _ _)
((mul_dvd_mul_iff_left ha).1 $ eq ▸ gcd_dvd_right _ _))
(dvd_gcd
(mul_dvd_mul_left a $ gcd_dvd_left _ _)
(mul_dvd_mul_left a $ gcd_dvd_right _ _))
@[simp] theorem gcd_mul_right (a b c : α) : gcd (b * a) (c * a) = gcd b c * normalize a :=
by simp only [mul_comm, gcd_mul_left]
theorem gcd_eq_left_iff (a b : α) (h : normalize a = a) : gcd a b = a ↔ a ∣ b :=
iff.intro (assume eq, eq ▸ gcd_dvd_right _ _) $
assume hab, dvd_antisymm_of_normalize_eq (normalize_gcd _ _) h (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) hab)
theorem gcd_eq_right_iff (a b : α) (h : normalize b = b) : gcd a b = b ↔ b ∣ a :=
by simpa only [gcd_comm a b] using gcd_eq_left_iff b a h
theorem gcd_dvd_gcd_mul_left (m n k : α) : gcd m n ∣ gcd (k * m) n :=
gcd_dvd_gcd (dvd_mul_left _ _) (dvd_refl _)
theorem gcd_dvd_gcd_mul_right (m n k : α) : gcd m n ∣ gcd (m * k) n :=
gcd_dvd_gcd (dvd_mul_right _ _) (dvd_refl _)
theorem gcd_dvd_gcd_mul_left_right (m n k : α) : gcd m n ∣ gcd m (k * n) :=
gcd_dvd_gcd (dvd_refl _) (dvd_mul_left _ _)
theorem gcd_dvd_gcd_mul_right_right (m n k : α) : gcd m n ∣ gcd m (n * k) :=
gcd_dvd_gcd (dvd_refl _) (dvd_mul_right _ _)
end gcd
section lcm
lemma lcm_dvd_iff {a b c : α} : lcm a b ∣ c ↔ a ∣ c ∧ b ∣ c :=
classical.by_cases
(assume : a = 0 ∨ b = 0, by rcases this with rfl | rfl;
simp only [iff_def, lcm_zero_left, lcm_zero_right, zero_dvd_iff, dvd_zero,
eq_self_iff_true, and_true, imp_true_iff] {contextual:=tt})
(assume this : ¬ (a = 0 ∨ b = 0),
let ⟨h1, h2⟩ := not_or_distrib.1 this in
have h : gcd a b ≠ 0, from λ H, h1 ((gcd_eq_zero_iff _ _).1 H).1,
by rw [← mul_dvd_mul_iff_left h, gcd_mul_lcm, normalize_dvd_iff, ← dvd_normalize_iff,
normalize_mul, normalize_gcd, ← gcd_mul_right, dvd_gcd_iff,
mul_comm b c, mul_dvd_mul_iff_left h1, mul_dvd_mul_iff_right h2, and_comm])
lemma dvd_lcm_left (a b : α) : a ∣ lcm a b := (lcm_dvd_iff.1 (dvd_refl _)).1
lemma dvd_lcm_right (a b : α) : b ∣ lcm a b := (lcm_dvd_iff.1 (dvd_refl _)).2
lemma lcm_dvd {a b c : α} (hab : a ∣ b) (hcb : c ∣ b) : lcm a c ∣ b :=
lcm_dvd_iff.2 ⟨hab, hcb⟩
@[simp] theorem lcm_eq_zero_iff (a b : α) : lcm a b = 0 ↔ a = 0 ∨ b = 0 :=
iff.intro
(assume h : lcm a b = 0,
have normalize (a * b) = 0,
by rw [← gcd_mul_lcm _ _, h, mul_zero],
by simpa only [normalize_eq_zero, mul_eq_zero, units.ne_zero, or_false])
(by rintro (rfl | rfl); [apply lcm_zero_left, apply lcm_zero_right])
@[simp] lemma normalize_lcm (a b : α) : normalize (lcm a b) = lcm a b :=
classical.by_cases (assume : lcm a b = 0, by rw [this, normalize_zero]) $
assume h_lcm : lcm a b ≠ 0,
have h1 : gcd a b ≠ 0, from mt (by rw [gcd_eq_zero_iff, lcm_eq_zero_iff];
rintros ⟨rfl, rfl⟩; left; refl) h_lcm,
have h2 : normalize (gcd a b * lcm a b) = gcd a b * lcm a b,
by rw [gcd_mul_lcm, normalize_idem],
by simpa only [normalize_mul, normalize_gcd, one_mul, domain.mul_left_inj h1] using h2
theorem lcm_comm (a b : α) : lcm a b = lcm b a :=
dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _)
(lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _))
(lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _))
theorem lcm_assoc (m n k : α) : lcm (lcm m n) k = lcm m (lcm n k) :=
dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _)
(lcm_dvd
(lcm_dvd (dvd_lcm_left _ _) (dvd.trans (dvd_lcm_left _ _) (dvd_lcm_right _ _)))
(dvd.trans (dvd_lcm_right _ _) (dvd_lcm_right _ _)))
(lcm_dvd
(dvd.trans (dvd_lcm_left _ _) (dvd_lcm_left _ _))
(lcm_dvd (dvd.trans (dvd_lcm_right _ _) (dvd_lcm_left _ _)) (dvd_lcm_right _ _)))
instance : is_commutative α lcm := ⟨lcm_comm⟩
instance : is_associative α lcm := ⟨lcm_assoc⟩
lemma lcm_eq_normalize {a b c : α} (habc : lcm a b ∣ c) (hcab : c ∣ lcm a b) :
lcm a b = normalize c :=
normalize_lcm a b ▸ normalize_eq_normalize habc hcab
theorem lcm_dvd_lcm {a b c d : α} (hab : a ∣ b) (hcd : c ∣ d) : lcm a c ∣ lcm b d :=
lcm_dvd (dvd.trans hab (dvd_lcm_left _ _)) (dvd.trans hcd (dvd_lcm_right _ _))
@[simp] theorem lcm_units_coe_left (u : units α) (a : α) : lcm ↑u a = normalize a :=
lcm_eq_normalize (lcm_dvd (units.coe_dvd _ _) (dvd_refl _)) (dvd_lcm_right _ _)
@[simp] theorem lcm_units_coe_right (a : α) (u : units α) : lcm a ↑u = normalize a :=
(lcm_comm a u).trans $ lcm_units_coe_left _ _
@[simp] theorem lcm_one_left (a : α) : lcm 1 a = normalize a :=
lcm_units_coe_left 1 a
@[simp] theorem lcm_one_right (a : α) : lcm a 1 = normalize a :=
lcm_units_coe_right a 1
@[simp] theorem lcm_same (a : α) : lcm a a = normalize a :=
lcm_eq_normalize (lcm_dvd (dvd_refl _) (dvd_refl _)) (dvd_lcm_left _ _)
@[simp] theorem lcm_eq_one_iff (a b : α) : lcm a b = 1 ↔ a ∣ 1 ∧ b ∣ 1 :=
iff.intro
(assume eq, eq ▸ ⟨dvd_lcm_left _ _, dvd_lcm_right _ _⟩)
(assume ⟨⟨c, hc⟩, ⟨d, hd⟩⟩,
show lcm (units.mk_of_mul_eq_one a c hc.symm : α) (units.mk_of_mul_eq_one b d hd.symm) = 1,
by rw [lcm_units_coe_left, normalize_coe_units])
@[simp] theorem lcm_mul_left (a b c : α) : lcm (a * b) (a * c) = normalize a * lcm b c :=
classical.by_cases (by rintro rfl; simp only [zero_mul, lcm_zero_left, normalize_zero]) $ assume ha : a ≠ 0,
suffices lcm (a * b) (a * c) = normalize (a * lcm b c),
by simpa only [normalize_mul, normalize_lcm],
have a ∣ lcm (a * b) (a * c), from dvd.trans (dvd_mul_right _ _) (dvd_lcm_left _ _),
let ⟨d, eq⟩ := this in
lcm_eq_normalize
(lcm_dvd (mul_dvd_mul_left a (dvd_lcm_left _ _)) (mul_dvd_mul_left a (dvd_lcm_right _ _)))
(eq.symm ▸ (mul_dvd_mul_left a $ lcm_dvd
((mul_dvd_mul_iff_left ha).1 $ eq ▸ dvd_lcm_left _ _)
((mul_dvd_mul_iff_left ha).1 $ eq ▸ dvd_lcm_right _ _)))
@[simp] theorem lcm_mul_right (a b c : α) : lcm (b * a) (c * a) = lcm b c * normalize a :=
by simp only [mul_comm, lcm_mul_left]
theorem lcm_eq_left_iff (a b : α) (h : normalize a = a) : lcm a b = a ↔ b ∣ a :=
iff.intro (assume eq, eq ▸ dvd_lcm_right _ _) $
assume hab, dvd_antisymm_of_normalize_eq (normalize_lcm _ _) h (lcm_dvd (dvd_refl a) hab) (dvd_lcm_left _ _)
theorem lcm_eq_right_iff (a b : α) (h : normalize b = b) : lcm a b = b ↔ a ∣ b :=
by simpa only [lcm_comm b a] using lcm_eq_left_iff b a h
theorem lcm_dvd_lcm_mul_left (m n k : α) : lcm m n ∣ lcm (k * m) n :=
lcm_dvd_lcm (dvd_mul_left _ _) (dvd_refl _)
theorem lcm_dvd_lcm_mul_right (m n k : α) : lcm m n ∣ lcm (m * k) n :=
lcm_dvd_lcm (dvd_mul_right _ _) (dvd_refl _)
theorem lcm_dvd_lcm_mul_left_right (m n k : α) : lcm m n ∣ lcm m (k * n) :=
lcm_dvd_lcm (dvd_refl _) (dvd_mul_left _ _)
theorem lcm_dvd_lcm_mul_right_right (m n k : α) : lcm m n ∣ lcm m (n * k) :=
lcm_dvd_lcm (dvd_refl _) (dvd_mul_right _ _)
end lcm
end gcd_domain
namespace int
section normalization_domain
instance : normalization_domain ℤ :=
{ norm_unit := λa:ℤ, if 0 ≤ a then 1 else -1,
norm_unit_zero := if_pos (le_refl _),
norm_unit_mul := assume a b hna hnb,
begin
by_cases ha : 0 ≤ a; by_cases hb : 0 ≤ b; simp [ha, hb],
exact if_pos (mul_nonneg ha hb),
exact if_neg (assume h, hb $ nonneg_of_mul_nonneg_left h $ lt_of_le_of_ne ha hna.symm),
exact if_neg (assume h, ha $ nonneg_of_mul_nonneg_right h $ lt_of_le_of_ne hb hnb.symm),
exact if_pos (mul_nonneg_of_nonpos_of_nonpos (le_of_not_ge ha) (le_of_not_ge hb))
end,
norm_unit_coe_units := assume u, (units_eq_one_or u).elim
(assume eq, eq.symm ▸ if_pos zero_le_one)
(assume eq, eq.symm ▸ if_neg (not_le_of_gt $ show (-1:ℤ) < 0, by simp [@neg_lt ℤ _ 1 0])),
.. (infer_instance : integral_domain ℤ) }
lemma normalize_of_nonneg {z : ℤ} (h : 0 ≤ z) : normalize z = z :=
show z * ↑(ite _ _ _) = z, by rw [if_pos h, units.coe_one, mul_one]
lemma normalize_of_neg {z : ℤ} (h : z < 0) : normalize z = -z :=
show z * ↑(ite _ _ _) = -z, by rw [if_neg (not_le_of_gt h), units.coe_neg, units.coe_one, mul_neg_one]
lemma normalize_coe_nat (n : ℕ) : normalize (n : ℤ) = n :=
normalize_of_nonneg (coe_nat_le_coe_nat_of_le $ nat.zero_le n)
theorem coe_nat_abs_eq_normalize (z : ℤ) : (z.nat_abs : ℤ) = normalize z :=
begin
by_cases 0 ≤ z,
{ simp [nat_abs_of_nonneg h, normalize_of_nonneg h] },
{ simp [of_nat_nat_abs_of_nonpos (le_of_not_ge h), normalize_of_neg (lt_of_not_ge h)] }
end
end normalization_domain
/-- ℤ specific version of least common multiple. -/
def lcm (i j : ℤ) : ℕ := nat.lcm (nat_abs i) (nat_abs j)
theorem lcm_def (i j : ℤ) : lcm i j = nat.lcm (nat_abs i) (nat_abs j) := rfl
section gcd_domain
theorem gcd_dvd_left (i j : ℤ) : (gcd i j : ℤ) ∣ i :=
dvd_nat_abs.mp $ coe_nat_dvd.mpr $ nat.gcd_dvd_left _ _
theorem gcd_dvd_right (i j : ℤ) : (gcd i j : ℤ) ∣ j :=
dvd_nat_abs.mp $ coe_nat_dvd.mpr $ nat.gcd_dvd_right _ _
theorem dvd_gcd {i j k : ℤ} (h1 : k ∣ i) (h2 : k ∣ j) : k ∣ gcd i j :=
nat_abs_dvd.1 $ coe_nat_dvd.2 $ nat.dvd_gcd (nat_abs_dvd_abs_iff.2 h1) (nat_abs_dvd_abs_iff.2 h2)
theorem gcd_mul_lcm (i j : ℤ) : gcd i j * lcm i j = nat_abs (i * j) :=
by rw [int.gcd, int.lcm, nat.gcd_mul_lcm, nat_abs_mul]
instance : gcd_domain ℤ :=
{ gcd := λa b, int.gcd a b,
lcm := λa b, int.lcm a b,
gcd_dvd_left := assume a b, int.gcd_dvd_left _ _,
gcd_dvd_right := assume a b, int.gcd_dvd_right _ _,
dvd_gcd := assume a b c, dvd_gcd,
normalize_gcd := assume a b, normalize_coe_nat _,
gcd_mul_lcm := by intros; rw [← int.coe_nat_mul, gcd_mul_lcm, coe_nat_abs_eq_normalize],
lcm_zero_left := assume a, coe_nat_eq_zero.2 $ nat.lcm_zero_left _,
lcm_zero_right := assume a, coe_nat_eq_zero.2 $ nat.lcm_zero_right _,
.. int.normalization_domain }
lemma coe_gcd (i j : ℤ) : ↑(int.gcd i j) = gcd_domain.gcd i j := rfl
lemma coe_lcm (i j : ℤ) : ↑(int.lcm i j) = gcd_domain.lcm i j := rfl
lemma nat_abs_gcd (i j : ℤ) : nat_abs (gcd_domain.gcd i j) = int.gcd i j := rfl
lemma nat_abs_lcm (i j : ℤ) : nat_abs (gcd_domain.lcm i j) = int.lcm i j := rfl
end gcd_domain
theorem gcd_comm (i j : ℤ) : gcd i j = gcd j i := nat.gcd_comm _ _
theorem gcd_assoc (i j k : ℤ) : gcd (gcd i j) k = gcd i (gcd j k) := nat.gcd_assoc _ _ _
@[simp] theorem gcd_self (i : ℤ) : gcd i i = nat_abs i := by simp [gcd]
@[simp] theorem gcd_zero_left (i : ℤ) : gcd 0 i = nat_abs i := by simp [gcd]
@[simp] theorem gcd_zero_right (i : ℤ) : gcd i 0 = nat_abs i := by simp [gcd]
@[simp] theorem gcd_one_left (i : ℤ) : gcd 1 i = 1 := nat.gcd_one_left _
@[simp] theorem gcd_one_right (i : ℤ) : gcd i 1 = 1 := nat.gcd_one_right _
theorem gcd_mul_left (i j k : ℤ) : gcd (i * j) (i * k) = nat_abs i * gcd j k :=
by simp [(int.coe_nat_eq_coe_nat_iff _ _).symm, coe_gcd, coe_nat_abs_eq_normalize]
theorem gcd_mul_right (i j k : ℤ) : gcd (i * j) (k * j) = gcd i k * nat_abs j :=
by simp [(int.coe_nat_eq_coe_nat_iff _ _).symm, coe_gcd, coe_nat_abs_eq_normalize]
theorem gcd_pos_of_non_zero_left {i : ℤ} (j : ℤ) (i_non_zero : i ≠ 0) : 0 < gcd i j :=
nat.gcd_pos_of_pos_left (nat_abs j) (nat_abs_pos_of_ne_zero i_non_zero)
theorem gcd_pos_of_non_zero_right (i : ℤ) {j : ℤ} (j_non_zero : j ≠ 0) : 0 < gcd i j :=
nat.gcd_pos_of_pos_right (nat_abs i) (nat_abs_pos_of_ne_zero j_non_zero)
theorem gcd_eq_zero_iff {i j : ℤ} : gcd i j = 0 ↔ i = 0 ∧ j = 0 :=
by rw [← int.coe_nat_eq_coe_nat_iff, int.coe_nat_zero, coe_gcd, gcd_eq_zero_iff]
theorem gcd_div {i j k : ℤ} (H1 : k ∣ i) (H2 : k ∣ j) :
gcd (i / k) (j / k) = gcd i j / nat_abs k :=
by rw [gcd, nat_abs_div i k H1, nat_abs_div j k H2];
exact nat.gcd_div (nat_abs_dvd_abs_iff.mpr H1) (nat_abs_dvd_abs_iff.mpr H2)
theorem gcd_dvd_gcd_of_dvd_left {i k : ℤ} (j : ℤ) (H : i ∣ k) : gcd i j ∣ gcd k j :=
int.coe_nat_dvd.1 $ dvd_gcd (dvd.trans (gcd_dvd_left i j) H) (gcd_dvd_right i j)
theorem gcd_dvd_gcd_of_dvd_right {i k : ℤ} (j : ℤ) (H : i ∣ k) : gcd j i ∣ gcd j k :=
int.coe_nat_dvd.1 $ dvd_gcd (gcd_dvd_left j i) (dvd.trans (gcd_dvd_right j i) H)
theorem gcd_dvd_gcd_mul_left (i j k : ℤ) : gcd i j ∣ gcd (k * i) j :=
gcd_dvd_gcd_of_dvd_left _ (dvd_mul_left _ _)
theorem gcd_dvd_gcd_mul_right (i j k : ℤ) : gcd i j ∣ gcd (i * k) j :=
gcd_dvd_gcd_of_dvd_left _ (dvd_mul_right _ _)
theorem gcd_dvd_gcd_mul_left_right (i j k : ℤ) : gcd i j ∣ gcd i (k * j) :=
gcd_dvd_gcd_of_dvd_right _ (dvd_mul_left _ _)
theorem gcd_dvd_gcd_mul_right_right (i j k : ℤ) : gcd i j ∣ gcd i (j * k) :=
gcd_dvd_gcd_of_dvd_right _ (dvd_mul_right _ _)
theorem gcd_eq_left {i j : ℤ} (H : i ∣ j) : gcd i j = nat_abs i :=
nat.dvd_antisymm (by unfold gcd; exact nat.gcd_dvd_left _ _)
(by unfold gcd; exact nat.dvd_gcd (dvd_refl _) (nat_abs_dvd_abs_iff.mpr H))
theorem gcd_eq_right {i j : ℤ} (H : j ∣ i) : gcd i j = nat_abs j :=
by rw [gcd_comm, gcd_eq_left H]
/- lcm -/
theorem lcm_comm (i j : ℤ) : lcm i j = lcm j i :=
by simp [(int.coe_nat_eq_coe_nat_iff _ _).symm, coe_lcm, lcm_comm]
theorem lcm_assoc (i j k : ℤ) : lcm (lcm i j) k = lcm i (lcm j k) :=
by simp [(int.coe_nat_eq_coe_nat_iff _ _).symm, coe_lcm, lcm_assoc]
@[simp] theorem lcm_zero_left (i : ℤ) : lcm 0 i = 0 :=
by simp [(int.coe_nat_eq_coe_nat_iff _ _).symm, coe_lcm]
@[simp] theorem lcm_zero_right (i : ℤ) : lcm i 0 = 0 :=
by simp [(int.coe_nat_eq_coe_nat_iff _ _).symm, coe_lcm]
@[simp] theorem lcm_one_left (i : ℤ) : lcm 1 i = nat_abs i :=
by simp [(int.coe_nat_eq_coe_nat_iff _ _).symm, coe_lcm, coe_nat_abs_eq_normalize]
@[simp] theorem lcm_one_right (i : ℤ) : lcm i 1 = nat_abs i :=
by simp [(int.coe_nat_eq_coe_nat_iff _ _).symm, coe_lcm, coe_nat_abs_eq_normalize]
@[simp] theorem lcm_self (i : ℤ) : lcm i i = nat_abs i :=
by simp [(int.coe_nat_eq_coe_nat_iff _ _).symm, coe_lcm, coe_nat_abs_eq_normalize]
theorem dvd_lcm_left (i j : ℤ) : i ∣ lcm i j :=
by rw [coe_lcm]; exact dvd_lcm_left _ _
theorem dvd_lcm_right (i j : ℤ) : j ∣ lcm i j :=
by rw [coe_lcm]; exact dvd_lcm_right _ _
theorem lcm_dvd {i j k : ℤ} : i ∣ k → j ∣ k → (lcm i j : ℤ) ∣ k :=
by rw [coe_lcm]; exact lcm_dvd
end int
theorem irreducible_iff_nat_prime : ∀(a : ℕ), irreducible a ↔ nat.prime a
| 0 := by simp [nat.not_prime_zero]
| 1 := by simp [nat.prime, one_lt_two]
| (n + 2) :=
have h₁ : ¬n + 2 = 1, from dec_trivial,
begin
simp [h₁, nat.prime, irreducible, (≥), nat.le_add_left 2 n, (∣)],
refine forall_congr (assume a, forall_congr $ assume b, forall_congr $ assume hab, _),
by_cases a = 1; simp [h],
split,
{ assume hb, simpa [hb] using hab.symm },
{ assume ha, subst ha,
have : n + 2 > 0, from dec_trivial,
refine nat.eq_of_mul_eq_mul_left this _,
rw [← hab, mul_one] }
end
lemma nat.prime_iff_prime {p : ℕ} : p.prime ↔ _root_.prime (p : ℕ) :=
⟨λ hp, ⟨nat.pos_iff_ne_zero.1 hp.pos, mt is_unit_iff_dvd_one.1 hp.not_dvd_one,
λ a b, hp.dvd_mul.1⟩,
λ hp, ⟨nat.one_lt_iff_ne_zero_and_ne_one.2 ⟨hp.1, λ h1, hp.2.1 $ h1.symm ▸ is_unit_one⟩,
λ a h, let ⟨b, hab⟩ := h in
(hp.2.2 a b (hab ▸ dvd_refl _)).elim
(λ ha, or.inr (nat.dvd_antisymm h ha))
(λ hb, or.inl (have hpb : p = b, from nat.dvd_antisymm hb
(hab.symm ▸ dvd_mul_left _ _),
(nat.mul_left_inj (show 0 < p, from
nat.pos_of_ne_zero hp.1)).1 $
by rw [hpb, mul_comm, ← hab, hpb, mul_one]))⟩⟩
lemma nat.prime_iff_prime_int {p : ℕ} : p.prime ↔ _root_.prime (p : ℤ) :=
⟨λ hp, ⟨int.coe_nat_ne_zero_iff_pos.2 hp.pos, mt is_unit_int.1 hp.ne_one,
λ a b h, by rw [← int.dvd_nat_abs, int.coe_nat_dvd, int.nat_abs_mul, hp.dvd_mul] at h;
rwa [← int.dvd_nat_abs, int.coe_nat_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd]⟩,
λ hp, nat.prime_iff_prime.2 ⟨int.coe_nat_ne_zero.1 hp.1,
mt is_unit_nat.1 $ λ h, by simpa [h, not_prime_one] using hp,
λ a b, by simpa only [int.coe_nat_dvd, (int.coe_nat_mul _ _).symm] using hp.2.2 a b⟩⟩
def associates_int_equiv_nat : associates ℤ ≃ ℕ :=
begin
refine ⟨λz, z.out.nat_abs, λn, associates.mk n, _, _⟩,
{ refine (assume a, quotient.induction_on' a $ assume a,
associates.mk_eq_mk_iff_associated.2 $ associated.symm $ ⟨norm_unit a, _⟩),
show normalize a = int.nat_abs (normalize a),
rw [int.coe_nat_abs_eq_normalize, normalize_idem] },
{ assume n, show int.nat_abs (normalize n) = n,
rw [← int.coe_nat_abs_eq_normalize, int.nat_abs_of_nat, int.nat_abs_of_nat] }
end
|
4845d69c03695fd2eb368a1b21973a1b84a8183a | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/elabissues/ImplicitLambdas.lean | 3bc5669f2d747117b55d3e7f7c29020a66b221f5 | [
"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,325 | lean | /- Arguments to implicit binders are inserted automatically. It would be nice if *parameters* in the definition of a term of such a type would
be inserted automatically as well.
For a good overview over (and one solution to) this topic, see https://github.com/AndrasKovacs/elaboration-zoo/tree/master/experimental/poly-instantiation. -/
-- works
example {m σ} [Alternative m] [Monad m] : Alternative (StateT σ m) :=
{ failure := @StateT.failure _ _ _ _,
orelse := @StateT.orelse _ _ _ _,
.. StateT.Monad }
-- works
example {m σ} [Alternative m] [Monad m] : Alternative (StateT σ m) :=
{ failure := fun α => StateT.failure,
orelse := fun α => StateT.orelse,
.. StateT.Monad }
-- fails
example {m σ} [Alternative m] [Monad m] : Alternative (StateT σ m) :=
{ failure := StateT.failure,
orelse := StateT.orelse,
.. StateT.Monad }
-- works
universes u v
example {m m' σ σ'} {n n' : Type u → Type v} [MonadFunctor m m' n n'] [MonadStateAdapter σ σ' m m'] : MonadStateAdapter σ σ' n n' :=
⟨fun σ'' α split join => monadMap (fun α => (adaptState split join : m α → m' α))⟩
-- fails
example {m m' σ σ'} {n n' : Type u → Type v} [MonadFunctor m m' n n'] [MonadStateAdapter σ σ' m m'] : MonadStateAdapter σ σ' n n' :=
⟨fun split join => monadMap (adaptState split join)⟩
|
8b3dae2e5c80e13b465dd087b059fe755aab1881 | e07b1aca72e83a272dd59d24c6e0fa246034d774 | /src/surreal/ipl.lean | 12c560a6ecdafaf12b2935cf2dd91320ccf14904 | [] | no_license | pedrominicz/learn | 637a343bd4f8669d76819ac660a2d2d3e0958710 | b79b802a9846c86c21d4b6f3e17af36e7382f0ef | refs/heads/master | 1,671,746,990,402 | 1,670,778,113,000 | 1,670,778,113,000 | 265,735,177 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,015 | lean | import tactic.basic
-- Lean version 3.45.0
@[derive decidable_eq] inductive prop : Type
| and : prop → prop → prop
| true : prop
| impl : prop → prop → prop
| other : ℕ → prop
abbreviation context : Type := list prop
inductive entails₁ : context → prop → Prop
| refl₁ {Γ A} : entails₁ (A :: Γ) A
| trans {Γ A C} : entails₁ (A :: Γ) C → entails₁ Γ A → entails₁ Γ C
| weak {Γ A C} : entails₁ Γ C → entails₁ (A :: Γ) C
| contr {Γ A C} : entails₁ (A :: A :: Γ) C → entails₁ (A :: Γ) C
| exch {Γ A B C} : entails₁ (B :: A :: Γ) C → entails₁ (A :: B :: Γ) C
| and_intro {Γ A B} : entails₁ Γ A → entails₁ Γ B → entails₁ Γ (prop.and A B)
| and_elim₁ {Γ A B} : entails₁ Γ (prop.and A B) → entails₁ Γ A
| and_elim₂ {Γ A B} : entails₁ Γ (prop.and A B) → entails₁ Γ B
| true_intro {Γ} : entails₁ Γ prop.true
| impl_intro {Γ A B} : entails₁ (A :: Γ) B → entails₁ Γ (prop.impl A B)
| impl_elim {Γ A B} : entails₁ Γ (prop.impl A B) → entails₁ Γ A → entails₁ Γ B
inductive entails₂ : context → prop → Prop
| refl₂ {Γ A} : list.mem A Γ → entails₂ Γ A
| and_intro {Γ A B} : entails₂ Γ A → entails₂ Γ B → entails₂ Γ (prop.and A B)
| and_elim₁ {Γ A B} : entails₂ Γ (prop.and A B) → entails₂ Γ A
| and_elim₂ {Γ A B} : entails₂ Γ (prop.and A B) → entails₂ Γ B
| true_intro {Γ} : entails₂ Γ prop.true
| impl_intro {Γ A B} : entails₂ (A :: Γ) B → entails₂ Γ (prop.impl A B)
| impl_elim {Γ A B} : entails₂ Γ (prop.impl A B) → entails₂ Γ A → entails₂ Γ B
lemma entails₁.refl₂ {Γ A} : list.mem A Γ → entails₁ Γ A :=
begin
intro h,
induction Γ with B Γ ih,
{ cases h },
{ by_cases h' : A = B,
{ subst h',
exact entails₁.refl₁ },
{ replace h : list.mem A Γ,
{ cases h, exact false.elim (h' h), exact h },
specialize ih h, clear h h',
exact entails₁.weak ih } }
end
lemma entails₂.refl₁ {Γ A} : entails₂ (A :: Γ) A :=
begin
exact entails₂.refl₂ (list.mem_cons_self A Γ)
end
lemma aux {Γ₁ Γ₂ C} (hΓ : ∀ (A : prop), list.mem A Γ₁ → list.mem A Γ₂) :
entails₂ Γ₁ C → entails₂ Γ₂ C :=
begin
intro h,
induction h with Γ₁ C h Γ₁ A B h₁ h₂ ih₁ ih₂ Γ₁ A B h ih Γ₁ A B h ih Γ₁ Γ₁ A B h ih Γ₁ A B h₁ h₂ ih₁ ih₂ generalizing Γ₂,
any_goals { specialize ih₁ hΓ, specialize ih₂ hΓ },
{ exact entails₂.refl₂ (hΓ C h) },
{ exact entails₂.and_intro ih₁ ih₂ },
{ exact entails₂.and_elim₁ (ih hΓ) },
{ exact entails₂.and_elim₂ (ih hΓ) },
{ exact entails₂.true_intro },
{ replace hΓ : ∀ B, list.mem B (A :: Γ₁) → list.mem B (A :: Γ₂),
{ intro B,
by_cases h : B = A,
{ subst h,
intro h',
exact or.inl rfl },
{ intro h',
cases h', exact false.elim (h h'),
exact or.inr (hΓ B h') } },
exact entails₂.impl_intro (ih hΓ) },
{ exact entails₂.impl_elim ih₁ ih₂ }
end
lemma entails₂.exch {Γ A B C} :
entails₂ (B :: A :: Γ) C → entails₂ (A :: B :: Γ) C :=
begin
apply aux; clear C,
intro C,
by_cases h₁ : C = A,
{ subst h₁,
intro h,
exact or.inl rfl },
{ by_cases h₂ : C = B,
{ subst h₂,
intro h,
exact or.inr (or.inl rfl) },
{ intro h,
cases h, exact false.elim (h₂ h),
cases h, exact false.elim (h₁ h),
exact or.inr (or.inr h) } }
end
lemma entails₂.weak {Γ A C} : entails₂ Γ C → entails₂ (A :: Γ) C :=
begin
intro h,
induction h with Γ C h Γ B C h₁ h₂ ih₁ ih₂ Γ C B h ih Γ B C h ih Γ Γ B C h ih Γ B C h₁ h₂ ih₁ ih₂,
{ exact entails₂.refl₂ (or.inr h) },
{ exact entails₂.and_intro ih₁ ih₂ },
{ exact entails₂.and_elim₁ ih },
{ exact entails₂.and_elim₂ ih },
{ exact entails₂.true_intro },
{ exact entails₂.impl_intro (entails₂.exch ih) },
{ exact entails₂.impl_elim ih₁ ih₂ }
end
lemma entails₂.trans {Γ A C} :
entails₂ (A :: Γ) C → entails₂ Γ A → entails₂ Γ C :=
begin
intros H₁ H₂,
have hΓ : ∀ (B : prop), list.mem B (A :: Γ) → B = A ∨ list.mem B Γ,
{ intros B h, exact h },
induction H₁ with Γ' C h Γ' A' B h₁ h₂ ih₁ ih₂ Γ' A' B h ih Γ' A' B h ih Γ' Γ' A' B h ih Γ' A' B h₁ h₂ ih₁ ih₂ generalizing Γ,
any_goals { specialize ih₁ H₂ hΓ, specialize ih₂ H₂ hΓ },
{ by_cases h' : C = A,
{ subst h',
exact H₂ },
{ replace hΓ : list.mem C Γ,
{ specialize hΓ C h,
cases hΓ,
{ exact false.elim (h' hΓ) },
{ exact hΓ } },
exact entails₂.refl₂ hΓ } },
{ exact entails₂.and_intro ih₁ ih₂ },
{ exact entails₂.and_elim₁ (ih H₂ hΓ) },
{ exact entails₂.and_elim₂ (ih H₂ hΓ) },
{ exact entails₂.true_intro },
{ replace H₂ : entails₂ (A' :: Γ) A := entails₂.weak H₂,
replace hΓ : ∀ (B : prop), list.mem B (A' :: Γ') → B = A ∨ list.mem B (A' :: Γ),
{ clear_dependent B,
intros B h,
cases h,
{ subst h,
exact or.inr (or.inl rfl) },
{ specialize hΓ B h,
cases hΓ,
{ subst hΓ,
exact or.inl rfl },
{ exact or.inr (or.inr hΓ) } } },
exact entails₂.impl_intro (ih H₂ hΓ) },
{ exact entails₂.impl_elim ih₁ ih₂ }
end
lemma entails₂.contr {Γ A C} :
entails₂ (A :: A :: Γ) C → entails₂ (A :: Γ) C :=
begin
apply aux; clear C,
intro B,
by_cases h : B = A,
{ subst h,
intro h',
exact or.inl rfl },
{ intro h',
cases h', exact false.elim (h h'),
cases h', exact false.elim (h h'),
exact or.inr h' }
end
theorem entails₁_iff_entails₂ {Γ C} :
entails₁ Γ C ↔ entails₂ Γ C :=
begin
split; intro h,
{ induction h with Γ A Γ A C h₁ h₂ ih₁ ih₂ Γ A C h ih Γ A C h ih Γ A B C h ih Γ A B h₁ h₂ ih₁ ih₂ Γ A B h ih Γ A B h ih Γ Γ A B h ih Γ A B h₁ h₂ ih₁ ih₂,
{ exact entails₂.refl₁ },
{ exact entails₂.trans ih₁ ih₂ },
{ exact entails₂.weak ih },
{ exact entails₂.contr ih },
{ exact entails₂.exch ih },
{ exact entails₂.and_intro ih₁ ih₂ },
{ exact entails₂.and_elim₁ ih },
{ exact entails₂.and_elim₂ ih },
{ exact entails₂.true_intro },
{ exact entails₂.impl_intro ih },
{ exact entails₂.impl_elim ih₁ ih₂ } },
{ induction h with Γ C h Γ A B h₁ h₂ ih₁ ih₂ Γ A B h ih Γ A B h ih Γ Γ A B h ih Γ A B h₁ h₂ ih₁ ih₂,
{ exact entails₁.refl₂ h },
{ exact entails₁.and_intro ih₁ ih₂ },
{ exact entails₁.and_elim₁ ih },
{ exact entails₁.and_elim₂ ih },
{ exact entails₁.true_intro },
{ exact entails₁.impl_intro ih },
{ exact entails₁.impl_elim ih₁ ih₂ } }
end
#print axioms entails₁_iff_entails₂ -- no axioms
|
c79915cfc7aa776b41c0e1a636c5af442de09cca | 4fa118f6209450d4e8d058790e2967337811b2b5 | /src/for_mathlib/integral_closure.lean | 4e2315863b181908eba3199e4b57c1c94aebb9f6 | [
"Apache-2.0"
] | permissive | leanprover-community/lean-perfectoid-spaces | 16ab697a220ed3669bf76311daa8c466382207f7 | 95a6520ce578b30a80b4c36e36ab2d559a842690 | refs/heads/master | 1,639,557,829,139 | 1,638,797,866,000 | 1,638,797,866,000 | 135,769,296 | 96 | 10 | Apache-2.0 | 1,638,797,866,000 | 1,527,892,754,000 | Lean | UTF-8 | Lean | false | false | 534 | lean |
import ring_theory.integral_closure
variables (R : Type*) (A : Type*) [decidable_eq R] [decidable_eq A]
variables [comm_ring R] [comm_ring A] [algebra R A]
open function
/--An R-algebra A is integrally closed if every element of A that is integral over R is contained in
the image of the canonical map R → A. This algebra_map is required to be injective.-/
structure is_integrally_closed : Prop :=
(inj : injective (algebra_map A : R → A))
(closed : ∀ a : A, (is_integral R a) → a ∈ set.range (algebra_map A : R → A))
|
bc4ac711cfc50fbe344c3241eb272bd47f99c9e2 | cbb1957fc3e28e502582c54cbce826d666350eda | /fabstract/Roux_C_and_vanDoorn_F_PTSs/fabstract.lean | 2f6fdfbbf2921f63938cf9813b4012a3757a9c3c | [
"CC-BY-4.0"
] | permissive | andrejbauer/formalabstracts | 9040b172da080406448ad1b0260d550122dcad74 | a3b84fd90901ccf4b63eb9f95d4286a8775864d0 | refs/heads/master | 1,609,476,417,918 | 1,501,541,742,000 | 1,501,541,760,000 | 97,241,872 | 1 | 0 | null | 1,500,042,191,000 | 1,500,042,191,000 | null | UTF-8 | Lean | false | false | 4,322 | lean | import meta_data
.pure_type_systems
namespace Roux_C_and_vanDoorn_F_PTSs
open pure_type_system sum
-- noncomputable theory
/- We investigate combinations and extensions of Pure Type Systems
and their normalization properties. -/
variables ⦃P Q : pure_type_system⦄
/- We define a morphism between PTSs -/
definition morphism (P Q : pure_type_system) : Type :=
Σ'(f : P.Sorts → Q.Sorts), (∀s₁ s₂, P.Axioms s₁ s₂ → Q.Axioms (f s₁) (f s₂)) ∧
(∀s₁ s₂ s₃, P.Relations s₁ s₂ s₃ → Q.Relations (f s₁) (f s₂) (f s₃))
/- It is easy to see that the domain of a morphism is weakly normalizing if the codomain is -/
unfinished is_weakly_normalizing_domain :
(Π P Q (f : morphism P Q) (HQ : is_weakly_normalizing Q), is_weakly_normalizing P) :=
{ description := "It is easy to see that the domain of a morphism is weakly normalizing if the codomain is",
references := [cite.Ibidem] }
/- The direct sum of PTSs -/
definition direct_sum (P Q : pure_type_system) : pure_type_system :=
⟨P.Sorts ⊕ Q.Sorts,
λs₁ s₂, match s₁, s₂ with
| (inl s₁), (inl s₂) := P.Axioms s₁ s₂
| (inr s₁), (inr s₂) := Q.Axioms s₁ s₂
| _, _ := false
end,
λs₁ s₂ s₃, match s₁, s₂, s₃ with
| (inl s₁), (inl s₂), (inl s₃) := P.Relations s₁ s₂ s₃
| (inr s₁), (inr s₂), (inr s₃) := Q.Relations s₁ s₂ s₃
| _, _, _ := false
end⟩
/- It is normalizing if the original ones were -/
unfinished is_weakly_normalizing_direct_sum :
(Π P Q(HP : is_weakly_normalizing P) (HQ : is_weakly_normalizing Q),
is_weakly_normalizing (direct_sum P Q)) :=
{ description := "It is normalizing if the original ones were",
references := [cite.Ibidem] }
/- The direct sum with quantification over sorts in P added. This can be interpreted as the
Q-logic of P-terms -/
definition forall_PTS (P Q : pure_type_system) : pure_type_system :=
⟨P.Sorts ⊕ Q.Sorts,
λs₁ s₂, match s₁, s₂ with
| (inl s₁), (inl s₂) := P.Axioms s₁ s₂
| (inr s₁), (inr s₂) := Q.Axioms s₁ s₂
| _, _ := false
end,
λs₁ s₂ s₃, match s₁, s₂, s₃ with
| (inl s₁), (inl s₂), (inl s₃) := P.Relations s₁ s₂ s₃
| (inr s₁), (inr s₂), (inr s₃) := Q.Relations s₁ s₂ s₃
| (inl s₁), (inr s₂), (inr s₃) := true
| _, _, _ := false
end⟩
/- It is normalizing if the original ones were -/
unfinished is_weakly_normalizing_forall :
(Π P Q (HP : is_weakly_normalizing P) (HQ : is_weakly_normalizing Q),
is_weakly_normalizing (forall_PTS P Q)) :=
{ description := "It is normalizing if the original ones were",
references := [cite.Ibidem] }
/- The predicative polymorphic extension of P -/
definition poly (P : pure_type_system) : pure_type_system :=
⟨P.Sorts ⊕ P.Sorts × P.Sorts,
λs₁ s₂, match s₁, s₂ with
| (inl s₁), (inl s₂) := P.Axioms s₁ s₂
| _, _ := false
end,
λs₁ s₂ s₃, match s₁, s₂, s₃ with
| (inl s₁), (inl s₂), (inl s₃) := P.Relations s₁ s₂ s₃
| (inl s₁), (inl s₂), (inr (s₁', s₂')) := s₁ = s₁' ∧ s₂ = s₂'
| (inl s₁), (inr (s₁', s₂')), (inr (s₁'', s₂'')) := s₁ = s₁' ∧ s₁' = s₁'' ∧ s₂' = s₂''
| _, _, _ := false
end⟩
/- It is normalizing if the original ones was -/
unfinished is_weakly_normalizing_poly :
(Π P (HP : is_weakly_normalizing P), is_weakly_normalizing (poly P)) :=
{ description := "It is normalizing if the original ones was",
references := [cite.Ibidem] }
def fabstract : meta_data := {
description := "We investigate possible extensions of arbitrary given Pure Type Systems with additional sorts and rules which preserve the normalization property.",
authors := [
{name := "Cody Roux"},
{name := "Floris van Doorn"}
],
primary := cite.DOI "10.1007/978-3-319-08918-8_25",
secondary := [],
results := [result.Proof is_weakly_normalizing_domain,
result.Proof is_weakly_normalizing_direct_sum,
result.Proof is_weakly_normalizing_forall,
result.Proof is_weakly_normalizing_poly],
}
end Roux_C_and_vanDoorn_F_PTSs
|
d00029a129633a05c043de7d9828a6a4e9319303 | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/set_theory/cofinality.lean | eb4a9a06fd43116f24030cb9fa02d644bb7de843 | [
"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 | 25,471 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Floris van Doorn
-/
import set_theory.cardinal_ordinal
/-!
# Cofinality
This file contains the definition of cofinality of a ordinal number and regular cardinals
## Main Definitions
* `ordinal.cof o` is the cofinality of the ordinal `o`.
If `o` is the order type of the relation `<` on `α`, then `o.cof` is the smallest cardinality of a
subset `s` of α that is *cofinal* in `α`, i.e. `∀ x : α, ∃ y ∈ s, ¬ y < x`.
* `cardinal.is_limit c` means that `c` is a (weak) limit cardinal: `c ≠ 0 ∧ ∀ x < c, succ x < c`.
* `cardinal.is_strong_limit c` means that `c` is a strong limit cardinal:
`c ≠ 0 ∧ ∀ x < c, 2 ^ x < c`.
* `cardinal.is_regular c` means that `c` is a regular cardinal: `omega ≤ c ∧ c.ord.cof = c`.
* `cardinal.is_inaccessible c` means that `c` is strongly inaccessible:
`omega < c ∧ is_regular c ∧ is_strong_limit c`.
## Main Statements
* `ordinal.infinite_pigeonhole_card`: the infinite pigeonhole principle
* `cardinal.lt_power_cof`: A consequence of König's theorem stating that `c < c ^ c.ord.cof` for
`c ≥ cardinal.omega`
* `cardinal.univ_inaccessible`: The type of ordinals in `Type u` form an inaccessible cardinal
(in `Type v` with `v > u`). This shows (externally) that in `Type u` there are at least `u`
inaccessible cardinals.
## Implementation Notes
* The cofinality is defined for ordinals.
If `c` is a cardinal number, its cofinality is `c.ord.cof`.
## Tags
cofinality, regular cardinals, limits cardinals, inaccessible cardinals,
infinite pigeonhole principle
-/
noncomputable theory
open function cardinal set
open_locale classical
universes u v w
variables {α : Type*} {r : α → α → Prop}
namespace order
/-- Cofinality of a reflexive order `≼`. This is the smallest cardinality
of a subset `S : set α` such that `∀ a, ∃ b ∈ S, a ≼ b`. -/
def cof (r : α → α → Prop) [is_refl α r] : cardinal :=
@cardinal.min {S : set α // ∀ a, ∃ b ∈ S, r a b}
⟨⟨set.univ, λ a, ⟨a, ⟨⟩, refl _⟩⟩⟩
(λ S, mk S)
lemma cof_le (r : α → α → Prop) [is_refl α r] {S : set α} (h : ∀a, ∃(b ∈ S), r a b) :
order.cof r ≤ mk S :=
le_trans (cardinal.min_le _ ⟨S, h⟩) (le_refl _)
lemma le_cof {r : α → α → Prop} [is_refl α r] (c : cardinal) :
c ≤ order.cof r ↔ ∀ {S : set α} (h : ∀a, ∃(b ∈ S), r a b) , c ≤ mk S :=
by { rw [order.cof, cardinal.le_min], exact ⟨λ H S h, H ⟨S, h⟩, λ H ⟨S, h⟩, H h ⟩ }
end order
theorem rel_iso.cof.aux {α : Type u} {β : Type v} {r s}
[is_refl α r] [is_refl β s] (f : r ≃r s) :
cardinal.lift.{u (max u v)} (order.cof r) ≤
cardinal.lift.{v (max u v)} (order.cof s) :=
begin
rw [order.cof, order.cof, lift_min, lift_min, cardinal.le_min],
intro S, cases S with S H, simp [(∘)],
refine le_trans (min_le _ _) _,
{ exact ⟨f ⁻¹' S, λ a,
let ⟨b, bS, h⟩ := H (f a) in ⟨f.symm b, by simp [bS, ← f.map_rel_iff, h,
-coe_fn_coe_base, -coe_fn_coe_trans, principal_seg.coe_coe_fn', initial_seg.coe_coe_fn]⟩⟩ },
{ exact lift_mk_le.{u v (max u v)}.2
⟨⟨λ ⟨x, h⟩, ⟨f x, h⟩, λ ⟨x, h₁⟩ ⟨y, h₂⟩ h₃,
by congr; injection h₃ with h'; exact f.to_equiv.injective h'⟩⟩ }
end
theorem rel_iso.cof {α : Type u} {β : Type v} {r s}
[is_refl α r] [is_refl β s] (f : r ≃r s) :
cardinal.lift.{u (max u v)} (order.cof r) =
cardinal.lift.{v (max u v)} (order.cof s) :=
le_antisymm (rel_iso.cof.aux f) (rel_iso.cof.aux f.symm)
def strict_order.cof (r : α → α → Prop) [h : is_irrefl α r] : cardinal :=
@order.cof α (λ x y, ¬ r y x) ⟨h.1⟩
namespace ordinal
/-- Cofinality of an ordinal. This is the smallest cardinal of a
subset `S` of the ordinal which is unbounded, in the sense
`∀ a, ∃ b ∈ S, ¬(b > a)`. It is defined for all ordinals, but
`cof 0 = 0` and `cof (succ o) = 1`, so it is only really
interesting on limit ordinals (when it is an infinite cardinal). -/
def cof (o : ordinal.{u}) : cardinal.{u} :=
quot.lift_on o (λ ⟨α, r, _⟩, by exactI strict_order.cof r)
begin
rintros ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨⟨f, hf⟩⟩,
rw ← cardinal.lift_inj,
apply rel_iso.cof ⟨f, _⟩,
simp [hf]
end
lemma cof_type (r : α → α → Prop) [is_well_order α r] : (type r).cof = strict_order.cof r := rfl
theorem le_cof_type [is_well_order α r] {c} : c ≤ cof (type r) ↔
∀ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) → c ≤ mk S :=
by dsimp [cof, strict_order.cof, order.cof, type, quotient.mk, quot.lift_on];
rw [cardinal.le_min, subtype.forall]; refl
theorem cof_type_le [is_well_order α r] (S : set α) (h : ∀ a, ∃ b ∈ S, ¬ r b a) :
cof (type r) ≤ mk S :=
le_cof_type.1 (le_refl _) S h
theorem lt_cof_type [is_well_order α r] (S : set α) (hl : mk S < cof (type r)) :
∃ a, ∀ b ∈ S, r b a :=
not_forall_not.1 $ λ h, not_le_of_lt hl $ cof_type_le S (λ a, not_ball.1 (h a))
theorem cof_eq (r : α → α → Prop) [is_well_order α r] :
∃ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) ∧ mk S = cof (type r) :=
begin
have : ∃ i, cof (type r) = _,
{ dsimp [cof, order.cof, type, quotient.mk, quot.lift_on],
apply cardinal.min_eq },
exact let ⟨⟨S, hl⟩, e⟩ := this in ⟨S, hl, e.symm⟩,
end
theorem ord_cof_eq (r : α → α → Prop) [is_well_order α r] :
∃ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) ∧ type (subrel r S) = (cof (type r)).ord :=
let ⟨S, hS, e⟩ := cof_eq r, ⟨s, _, e'⟩ := cardinal.ord_eq S,
T : set α := {a | ∃ aS : a ∈ S, ∀ b : S, s b ⟨_, aS⟩ → r b a} in
begin
resetI, suffices,
{ refine ⟨T, this,
le_antisymm _ (cardinal.ord_le.2 $ cof_type_le T this)⟩,
rw [← e, e'],
refine type_le'.2 ⟨rel_embedding.of_monotone
(λ a, ⟨a, let ⟨aS, _⟩ := a.2 in aS⟩) (λ a b h, _)⟩,
rcases a with ⟨a, aS, ha⟩, rcases b with ⟨b, bS, hb⟩,
change s ⟨a, _⟩ ⟨b, _⟩,
refine ((trichotomous_of s _ _).resolve_left (λ hn, _)).resolve_left _,
{ exact asymm h (ha _ hn) },
{ intro e, injection e with e, subst b,
exact irrefl _ h } },
{ intro a,
have : {b : S | ¬ r b a}.nonempty := let ⟨b, bS, ba⟩ := hS a in ⟨⟨b, bS⟩, ba⟩,
let b := (is_well_order.wf).min _ this,
have ba : ¬r b a := (is_well_order.wf).min_mem _ this,
refine ⟨b, ⟨b.2, λ c, not_imp_not.1 $ λ h, _⟩, ba⟩,
rw [show ∀b:S, (⟨b, b.2⟩:S) = b, by intro b; cases b; refl],
exact (is_well_order.wf).not_lt_min _ this
(is_order_connected.neg_trans h ba) }
end
theorem lift_cof (o) : (cof o).lift = cof o.lift :=
induction_on o $ begin introsI α r _,
cases lift_type r with _ e, rw e,
apply le_antisymm,
{ unfreezingI { refine le_cof_type.2 (λ S H, _) },
have : (mk (ulift.up ⁻¹' S)).lift ≤ mk S :=
⟨⟨λ ⟨⟨x, h⟩⟩, ⟨⟨x⟩, h⟩,
λ ⟨⟨x, h₁⟩⟩ ⟨⟨y, h₂⟩⟩ e, by simp at e; congr; injection e⟩⟩,
refine le_trans (cardinal.lift_le.2 $ cof_type_le _ _) this,
exact λ a, let ⟨⟨b⟩, bs, br⟩ := H ⟨a⟩ in ⟨b, bs, br⟩ },
{ rcases cof_eq r with ⟨S, H, e'⟩,
have : mk (ulift.down ⁻¹' S) ≤ (mk S).lift :=
⟨⟨λ ⟨⟨x⟩, h⟩, ⟨⟨x, h⟩⟩,
λ ⟨⟨x⟩, h₁⟩ ⟨⟨y⟩, h₂⟩ e, by simp at e; congr; injections⟩⟩,
rw e' at this,
unfreezingI { refine le_trans (cof_type_le _ _) this },
exact λ ⟨a⟩, let ⟨b, bs, br⟩ := H a in ⟨⟨b⟩, bs, br⟩ }
end
theorem cof_le_card (o) : cof o ≤ card o :=
induction_on o $ λ α r _, begin
resetI,
have : mk (@set.univ α) = card (type r) :=
quotient.sound ⟨equiv.set.univ _⟩,
rw ← this, exact cof_type_le set.univ (λ a, ⟨a, ⟨⟩, irrefl a⟩)
end
theorem cof_ord_le (c : cardinal) : cof c.ord ≤ c :=
by simpa using cof_le_card c.ord
@[simp] theorem cof_zero : cof 0 = 0 :=
le_antisymm (by simpa using cof_le_card 0) (cardinal.zero_le _)
@[simp] theorem cof_eq_zero {o} : cof o = 0 ↔ o = 0 :=
⟨induction_on o $ λ α r _ z, by exactI
let ⟨S, hl, e⟩ := cof_eq r in type_eq_zero_iff_is_empty.2 $
⟨λ a, let ⟨b, h, _⟩ := hl a in
(eq_zero_iff_is_empty.1 (e.trans z)).elim' ⟨_, h⟩⟩,
λ e, by simp [e]⟩
@[simp] theorem cof_succ (o) : cof (succ o) = 1 :=
begin
apply le_antisymm,
{ refine induction_on o (λ α r _, _),
change cof (type _) ≤ _,
rw [← (_ : mk _ = 1)], apply cof_type_le,
{ refine λ a, ⟨sum.inr punit.star, set.mem_singleton _, _⟩,
rcases a with a|⟨⟨⟨⟩⟩⟩; simp [empty_relation] },
{ rw [cardinal.fintype_card, set.card_singleton], simp } },
{ rw [← cardinal.succ_zero, cardinal.succ_le],
simpa [lt_iff_le_and_ne, cardinal.zero_le] using
λ h, succ_ne_zero o (cof_eq_zero.1 (eq.symm h)) }
end
@[simp] theorem cof_eq_one_iff_is_succ {o} : cof.{u} o = 1 ↔ ∃ a, o = succ a :=
⟨induction_on o $ λ α r _ z, begin
resetI,
rcases cof_eq r with ⟨S, hl, e⟩, rw z at e,
cases ne_zero_iff_nonempty.1 (by rw e; exact one_ne_zero) with a,
refine ⟨typein r a, eq.symm $ quotient.sound
⟨rel_iso.of_surjective (rel_embedding.of_monotone _
(λ x y, _)) (λ x, _)⟩⟩,
{ apply sum.rec; [exact subtype.val, exact λ _, a] },
{ rcases x with x|⟨⟨⟨⟩⟩⟩; rcases y with y|⟨⟨⟨⟩⟩⟩;
simp [subrel, order.preimage, empty_relation],
exact x.2 },
{ suffices : r x a ∨ ∃ (b : punit), ↑a = x, {simpa},
rcases trichotomous_of r x a with h|h|h,
{ exact or.inl h },
{ exact or.inr ⟨punit.star, h.symm⟩ },
{ rcases hl x with ⟨a', aS, hn⟩,
rw (_ : ↑a = a') at h, {exact absurd h hn},
refine congr_arg subtype.val (_ : a = ⟨a', aS⟩),
haveI := le_one_iff_subsingleton.1 (le_of_eq e),
apply subsingleton.elim } }
end, λ ⟨a, e⟩, by simp [e]⟩
@[simp] theorem cof_add (a b : ordinal) : b ≠ 0 → cof (a + b) = cof b :=
induction_on a $ λ α r _, induction_on b $ λ β s _ b0, begin
resetI,
change cof (type _) = _,
refine eq_of_forall_le_iff (λ c, _),
rw [le_cof_type, le_cof_type],
split; intros H S hS,
{ refine le_trans (H {a | sum.rec_on a (∅:set α) S} (λ a, _)) ⟨⟨_, _⟩⟩,
{ cases a with a b,
{ cases type_ne_zero_iff_nonempty.1 b0 with b,
rcases hS b with ⟨b', bs, _⟩,
exact ⟨sum.inr b', bs, by simp⟩ },
{ rcases hS b with ⟨b', bs, h⟩,
exact ⟨sum.inr b', bs, by simp [h]⟩ } },
{ exact λ a, match a with ⟨sum.inr b, h⟩ := ⟨b, h⟩ end },
{ exact λ a b, match a, b with
⟨sum.inr a, h₁⟩, ⟨sum.inr b, h₂⟩, h := by congr; injection h
end } },
{ refine le_trans (H (sum.inr ⁻¹' S) (λ a, _)) ⟨⟨_, _⟩⟩,
{ rcases hS (sum.inr a) with ⟨a'|b', bs, h⟩; simp at h,
{ cases h }, { exact ⟨b', bs, h⟩ } },
{ exact λ ⟨a, h⟩, ⟨_, h⟩ },
{ exact λ ⟨a, h₁⟩ ⟨b, h₂⟩ h,
by injection h with h; congr; injection h } }
end
@[simp] theorem cof_cof (o : ordinal) : cof (cof o).ord= cof o :=
le_antisymm (le_trans (cof_le_card _) (by simp)) $
induction_on o $ λ α r _, by exactI
let ⟨S, hS, e₁⟩ := ord_cof_eq r,
⟨T, hT, e₂⟩ := cof_eq (subrel r S) in begin
rw e₁ at e₂, rw ← e₂,
refine le_trans (cof_type_le {a | ∃ h, (subtype.mk a h : S) ∈ T} (λ a, _)) ⟨⟨_, _⟩⟩,
{ rcases hS a with ⟨b, bS, br⟩,
rcases hT ⟨b, bS⟩ with ⟨⟨c, cS⟩, cT, cs⟩,
exact ⟨c, ⟨cS, cT⟩, is_order_connected.neg_trans cs br⟩ },
{ exact λ ⟨a, h⟩, ⟨⟨a, h.fst⟩, h.snd⟩ },
{ exact λ ⟨a, ha⟩ ⟨b, hb⟩ h,
by injection h with h; congr; injection h },
end
theorem omega_le_cof {o} : cardinal.omega ≤ cof o ↔ is_limit o :=
begin
rcases zero_or_succ_or_limit o with rfl|⟨o,rfl⟩|l,
{ simp [not_zero_is_limit, cardinal.omega_ne_zero] },
{ simp [not_succ_is_limit, cardinal.one_lt_omega] },
{ simp [l], refine le_of_not_lt (λ h, _),
cases cardinal.lt_omega.1 h with n e,
have := cof_cof o,
rw [e, ord_nat] at this,
cases n,
{ simp at e, simpa [e, not_zero_is_limit] using l },
{ rw [← nat_cast_succ, cof_succ] at this,
rw [← this, cof_eq_one_iff_is_succ] at e,
rcases e with ⟨a, rfl⟩,
exact not_succ_is_limit _ l } }
end
@[simp] theorem cof_omega : cof omega = cardinal.omega :=
le_antisymm
(by rw ← card_omega; apply cof_le_card)
(omega_le_cof.2 omega_is_limit)
theorem cof_eq' (r : α → α → Prop) [is_well_order α r] (h : is_limit (type r)) :
∃ S : set α, (∀ a, ∃ b ∈ S, r a b) ∧ mk S = cof (type r) :=
let ⟨S, H, e⟩ := cof_eq r in
⟨S, λ a,
let a' := enum r _ (h.2 _ (typein_lt_type r a)) in
let ⟨b, h, ab⟩ := H a' in
⟨b, h, (is_order_connected.conn a b a' $ (typein_lt_typein r).1
(by rw typein_enum; apply ordinal.lt_succ_self)).resolve_right ab⟩,
e⟩
theorem cof_sup_le_lift {ι} (f : ι → ordinal) (H : ∀ i, f i < sup f) :
cof (sup f) ≤ (mk ι).lift :=
begin
generalize e : sup f = o,
refine ordinal.induction_on o _ e, introsI α r _ e',
rw e' at H,
refine le_trans (cof_type_le (set.range (λ i, enum r _ (H i))) _)
⟨embedding.of_surjective _ _⟩,
{ intro a, by_contra h,
apply not_le_of_lt (typein_lt_type r a),
rw [← e', sup_le],
intro i,
have h : ∀ (x : ι), r (enum r (f x) _) a, { simpa using h },
simpa only [typein_enum] using le_of_lt ((typein_lt_typein r).2 (h i)) },
{ exact λ i, ⟨_, set.mem_range_self i.1⟩ },
{ intro a, rcases a with ⟨_, i, rfl⟩, exact ⟨⟨i⟩, by simp⟩ }
end
theorem cof_sup_le {ι} (f : ι → ordinal) (H : ∀ i, f i < sup.{u u} f) :
cof (sup.{u u} f) ≤ mk ι :=
by simpa using cof_sup_le_lift.{u u} f H
theorem cof_bsup_le_lift {o : ordinal} : ∀ (f : Π a < o, ordinal), (∀ i h, f i h < bsup o f) →
cof (bsup o f) ≤ o.card.lift :=
induction_on o $ λ α r _ f H,
by rw bsup_type; refine cof_sup_le_lift _ _;
rw ← bsup_type; intro a; apply H
theorem cof_bsup_le {o : ordinal} : ∀ (f : Π a < o, ordinal), (∀ i h, f i h < bsup.{u u} o f) →
cof (bsup.{u u} o f) ≤ o.card :=
induction_on o $ λ α r _ f H,
by simpa using cof_bsup_le_lift.{u u} f H
@[simp] theorem cof_univ : cof univ.{u v} = cardinal.univ :=
le_antisymm (cof_le_card _) begin
refine le_of_forall_lt (λ c h, _),
rcases lt_univ'.1 h with ⟨c, rfl⟩,
rcases @cof_eq ordinal.{u} (<) _ with ⟨S, H, Se⟩,
rw [univ, ← lift_cof, ← cardinal.lift_lift, cardinal.lift_lt, ← Se],
refine lt_of_not_ge (λ h, _),
cases cardinal.lift_down h with a e,
refine quotient.induction_on a (λ α e, _) e,
cases quotient.exact e with f,
have f := equiv.ulift.symm.trans f,
let g := λ a, (f a).1,
let o := succ (sup.{u u} g),
rcases H o with ⟨b, h, l⟩,
refine l (lt_succ.2 _),
rw ← show g (f.symm ⟨b, h⟩) = b, by dsimp [g]; simp,
apply le_sup
end
theorem sup_lt_ord {ι} (f : ι → ordinal) {c : ordinal} (H1 : cardinal.mk ι < c.cof)
(H2 : ∀ i, f i < c) : sup.{u u} f < c :=
begin
apply lt_of_le_of_ne,
{ rw [sup_le], exact λ i, le_of_lt (H2 i) },
rintro h, apply not_le_of_lt H1,
simpa [sup_ord, H2, h] using cof_sup_le.{u} f
end
theorem sup_lt {ι} (f : ι → cardinal) {c : cardinal} (H1 : cardinal.mk ι < c.ord.cof)
(H2 : ∀ i, f i < c) : cardinal.sup.{u u} f < c :=
by { rw [←ord_lt_ord, ←sup_ord], apply sup_lt_ord _ H1, intro i, rw ord_lt_ord, apply H2 }
/-- If the union of s is unbounded and s is smaller than the cofinality,
then s has an unbounded member -/
theorem unbounded_of_unbounded_sUnion (r : α → α → Prop) [wo : is_well_order α r] {s : set (set α)}
(h₁ : unbounded r $ ⋃₀ s) (h₂ : mk s < strict_order.cof r) : ∃(x ∈ s), unbounded r x :=
begin
by_contra h, simp only [not_exists, exists_prop, not_and, not_unbounded_iff] at h,
apply not_le_of_lt h₂,
let f : s → α := λ x : s, wo.wf.sup x (h x.1 x.2),
let t : set α := range f,
have : mk t ≤ mk s, exact mk_range_le, refine le_trans _ this,
have : unbounded r t,
{ intro x, rcases h₁ x with ⟨y, ⟨c, hc, hy⟩, hxy⟩,
refine ⟨f ⟨c, hc⟩, mem_range_self _, _⟩, intro hxz, apply hxy,
refine trans (wo.wf.lt_sup _ hy) hxz },
exact cardinal.min_le _ (subtype.mk t this)
end
/-- If the union of s is unbounded and s is smaller than the cofinality,
then s has an unbounded member -/
theorem unbounded_of_unbounded_Union {α β : Type u} (r : α → α → Prop) [wo : is_well_order α r]
(s : β → set α)
(h₁ : unbounded r $ ⋃x, s x) (h₂ : mk β < strict_order.cof r) : ∃x : β, unbounded r (s x) :=
begin
rw [← sUnion_range] at h₁,
have : mk ↥(range (λ (i : β), s i)) < strict_order.cof r := lt_of_le_of_lt mk_range_le h₂,
rcases unbounded_of_unbounded_sUnion r h₁ this with ⟨_, ⟨x, rfl⟩, u⟩, exact ⟨x, u⟩
end
/-- The infinite pigeonhole principle -/
theorem infinite_pigeonhole {β α : Type u} (f : β → α) (h₁ : cardinal.omega ≤ mk β)
(h₂ : mk α < (mk β).ord.cof) : ∃a : α, mk (f ⁻¹' {a}) = mk β :=
begin
have : ¬∀a, mk (f ⁻¹' {a}) < mk β,
{ intro h,
apply not_lt_of_ge (ge_of_eq $ mk_univ),
rw [←@preimage_univ _ _ f, ←Union_of_singleton, preimage_Union],
apply lt_of_le_of_lt mk_Union_le_sum_mk,
apply lt_of_le_of_lt (sum_le_sup _),
apply mul_lt_of_lt h₁ (lt_of_lt_of_le h₂ $ cof_ord_le _),
exact sup_lt _ h₂ h },
rw [not_forall] at this, cases this with x h,
use x, apply le_antisymm _ (le_of_not_gt h),
rw [le_mk_iff_exists_set], exact ⟨_, rfl⟩
end
/-- pigeonhole principle for a cardinality below the cardinality of the domain -/
theorem infinite_pigeonhole_card {β α : Type u} (f : β → α) (θ : cardinal) (hθ : θ ≤ mk β)
(h₁ : cardinal.omega ≤ θ) (h₂ : mk α < θ.ord.cof) : ∃a : α, θ ≤ mk (f ⁻¹' {a}) :=
begin
rcases le_mk_iff_exists_set.1 hθ with ⟨s, rfl⟩,
cases infinite_pigeonhole (f ∘ subtype.val : s → α) h₁ h₂ with a ha,
use a, rw [←ha, @preimage_comp _ _ _ subtype.val f],
apply mk_preimage_of_injective _ _ subtype.val_injective
end
theorem infinite_pigeonhole_set {β α : Type u} {s : set β} (f : s → α) (θ : cardinal)
(hθ : θ ≤ mk s) (h₁ : cardinal.omega ≤ θ) (h₂ : mk α < θ.ord.cof) :
∃(a : α) (t : set β) (h : t ⊆ s), θ ≤ mk t ∧ ∀{{x}} (hx : x ∈ t), f ⟨x, h hx⟩ = a :=
begin
cases infinite_pigeonhole_card f θ hθ h₁ h₂ with a ha,
refine ⟨a, {x | ∃(h : x ∈ s), f ⟨x, h⟩ = a}, _, _, _⟩,
{ rintro x ⟨hx, hx'⟩, exact hx },
{ refine le_trans ha _, apply ge_of_eq, apply quotient.sound, constructor,
refine equiv.trans _ (equiv.subtype_subtype_equiv_subtype_exists _ _).symm,
simp only [set_coe_eq_subtype, mem_singleton_iff, mem_preimage, mem_set_of_eq] },
rintro x ⟨hx, hx'⟩, exact hx'
end
end ordinal
namespace cardinal
open ordinal
local infixr ^ := @pow cardinal.{u} cardinal cardinal.has_pow
/-- A cardinal is a limit if it is not zero or a successor
cardinal. Note that `ω` is a limit cardinal by this definition. -/
def is_limit (c : cardinal) : Prop :=
c ≠ 0 ∧ ∀ x < c, succ x < c
/-- A cardinal is a strong limit if it is not zero and it is
closed under powersets. Note that `ω` is a strong limit by this definition. -/
def is_strong_limit (c : cardinal) : Prop :=
c ≠ 0 ∧ ∀ x < c, 2 ^ x < c
theorem is_strong_limit.is_limit {c} (H : is_strong_limit c) : is_limit c :=
⟨H.1, λ x h, lt_of_le_of_lt (succ_le.2 $ cantor _) (H.2 _ h)⟩
/-- A cardinal is regular if it is infinite and it equals its own cofinality. -/
def is_regular (c : cardinal) : Prop :=
omega ≤ c ∧ c.ord.cof = c
theorem cof_is_regular {o : ordinal} (h : o.is_limit) : is_regular o.cof :=
⟨omega_le_cof.2 h, cof_cof _⟩
theorem omega_is_regular : is_regular omega :=
⟨le_refl _, by simp⟩
theorem succ_is_regular {c : cardinal.{u}} (h : omega ≤ c) : is_regular (succ c) :=
⟨le_trans h (le_of_lt $ lt_succ_self _), begin
refine le_antisymm (cof_ord_le _) (succ_le.2 _),
cases quotient.exists_rep (succ c) with α αe, simp at αe,
rcases ord_eq α with ⟨r, wo, re⟩, resetI,
have := ord_is_limit (le_trans h $ le_of_lt $ lt_succ_self _),
rw [← αe, re] at this ⊢,
rcases cof_eq' r this with ⟨S, H, Se⟩,
rw [← Se],
apply lt_imp_lt_of_le_imp_le
(λ (h : mk S ≤ c), mul_le_mul_right' h c),
rw [mul_eq_self h, ← succ_le, ← αe, ← sum_const],
refine le_trans _ (sum_le_sum (λ x:S, card (typein r x)) _ _),
{ simp [typein, sum_mk (λ x:S, {a//r a x})],
refine ⟨embedding.of_surjective _ _⟩,
{ exact λ x, x.2.1 },
{ exact λ a, let ⟨b, h, ab⟩ := H a in ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ } },
{ intro i,
rw [← lt_succ, ← lt_ord, ← αe, re],
apply typein_lt_type }
end⟩
/--
A function whose codomain's cardinality is infinite but strictly smaller than its domain's
has a fiber with cardinality strictly great than the codomain.
-/
theorem infinite_pigeonhole_card_lt {β α : Type u} (f : β → α)
(w : mk α < mk β) (w' : omega ≤ mk α) :
∃ a : α, mk α < mk (f ⁻¹' {a}) :=
begin
simp_rw [← succ_le],
exact ordinal.infinite_pigeonhole_card f (mk α).succ (succ_le.mpr w)
(w'.trans (lt_succ_self _).le)
((lt_succ_self _).trans_le (succ_is_regular w').2.ge),
end
/--
A function whose codomain's cardinality is infinite but strictly smaller than its domain's
has an infinite fiber.
-/
theorem exists_infinite_fiber {β α : Type*} (f : β → α)
(w : mk α < mk β) (w' : _root_.infinite α) :
∃ a : α, _root_.infinite (f ⁻¹' {a}) :=
begin
simp_rw [cardinal.infinite_iff] at ⊢ w',
cases infinite_pigeonhole_card_lt f w w' with a ha,
exact ⟨a, w'.trans ha.le⟩,
end
/--
If an infinite type `β` can be expressed as a union of finite sets,
then the cardinality of the collection of those finite sets
must be at least the cardinality of `β`.
-/
lemma le_range_of_union_finset_eq_top
{α β : Type*} [infinite β] (f : α → finset β) (w : (⋃ a, (f a : set β)) = ⊤) :
mk β ≤ mk (range f) :=
begin
have k : _root_.infinite (range f),
{ rw infinite_coe_iff,
apply mt (union_finset_finite_of_range_finite f),
rw w,
exact infinite_univ, },
by_contradiction h,
simp only [not_le] at h,
let u : Π b, ∃ a, b ∈ f a := λ b, by simpa using (w.ge : _) (set.mem_univ b),
let u' : β → range f := λ b, ⟨f (u b).some, by simp⟩,
have v' : ∀ a, u' ⁻¹' {⟨f a, by simp⟩} ≤ f a, begin rintros a p m,
simp at m,
rw ←m,
apply (λ b, (u b).some_spec),
end,
obtain ⟨⟨-, ⟨a, rfl⟩⟩, p⟩ := exists_infinite_fiber u' h k,
exact (@infinite.of_injective _ _ p (inclusion (v' a)) (inclusion_injective _)).false,
end
theorem sup_lt_ord_of_is_regular {ι} (f : ι → ordinal)
{c} (hc : is_regular c) (H1 : cardinal.mk ι < c)
(H2 : ∀ i, f i < c.ord) : ordinal.sup.{u u} f < c.ord :=
by { apply sup_lt_ord _ _ H2, rw [hc.2], exact H1 }
theorem sup_lt_of_is_regular {ι} (f : ι → cardinal)
{c} (hc : is_regular c) (H1 : cardinal.mk ι < c)
(H2 : ∀ i, f i < c) : sup.{u u} f < c :=
by { apply sup_lt _ _ H2, rwa [hc.2] }
theorem sum_lt_of_is_regular {ι} (f : ι → cardinal)
{c} (hc : is_regular c) (H1 : cardinal.mk ι < c)
(H2 : ∀ i, f i < c) : sum.{u u} f < c :=
lt_of_le_of_lt (sum_le_sup _) $ mul_lt_of_lt hc.1 H1 $
sup_lt_of_is_regular f hc H1 H2
/-- A cardinal is inaccessible if it is an uncountable regular strong limit cardinal. -/
def is_inaccessible (c : cardinal) :=
omega < c ∧ is_regular c ∧ is_strong_limit c
theorem is_inaccessible.mk {c}
(h₁ : omega < c) (h₂ : c ≤ c.ord.cof) (h₃ : ∀ x < c, 2 ^ x < c) :
is_inaccessible c :=
⟨h₁, ⟨le_of_lt h₁, le_antisymm (cof_ord_le _) h₂⟩,
ne_of_gt (lt_trans omega_pos h₁), h₃⟩
/- Lean's foundations prove the existence of ω many inaccessible cardinals -/
theorem univ_inaccessible : is_inaccessible (univ.{u v}) :=
is_inaccessible.mk
(by simpa using lift_lt_univ' omega)
(by simp)
(λ c h, begin
rcases lt_univ'.1 h with ⟨c, rfl⟩,
rw ← lift_two_power.{u (max (u+1) v)},
apply lift_lt_univ'
end)
theorem lt_power_cof {c : cardinal.{u}} : omega ≤ c → c < c ^ cof c.ord :=
quotient.induction_on c $ λ α h, begin
rcases ord_eq α with ⟨r, wo, re⟩, resetI,
have := ord_is_limit h,
rw [mk_def, re] at this ⊢,
rcases cof_eq' r this with ⟨S, H, Se⟩,
have := sum_lt_prod (λ a:S, mk {x // r x a}) (λ _, mk α) (λ i, _),
{ simp [Se.symm] at this ⊢,
refine lt_of_le_of_lt _ this,
refine ⟨embedding.of_surjective _ _⟩,
{ exact λ x, x.2.1 },
{ exact λ a, let ⟨b, h, ab⟩ := H a in ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ } },
{ have := typein_lt_type r i,
rwa [← re, lt_ord] at this }
end
theorem lt_cof_power {a b : cardinal} (ha : omega ≤ a) (b1 : 1 < b) :
a < cof (b ^ a).ord :=
begin
have b0 : b ≠ 0 := ne_of_gt (lt_trans zero_lt_one b1),
apply lt_imp_lt_of_le_imp_le (power_le_power_left $ power_ne_zero a b0),
rw [← power_mul, mul_eq_self ha],
exact lt_power_cof (le_trans ha $ le_of_lt $ cantor' _ b1),
end
end cardinal
|
6822aa80e81300da7d0bfd8a58913c586c5ebb37 | 82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7 | /src/Init/Data/Range.lean | 436d86493c31f92d6b4e6d964801130420818bb7 | [
"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 | 1,465 | 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
-/
prelude
import Init.LeanInit
namespace Std
-- We put `Range` in `Init` because we want the notation `[i:j]` without importing `Std`
-- We don't put `Range` in the top-level namespace to avoid collisions with user defined types
structure Range :=
(start : Nat := 0)
(stop : Nat)
(step : Nat := 1)
namespace Range
universes u v
@[inline] def forIn {β : Type u} {m : Type u → Type v} [Monad m] (range : Range) (init : β) (f : Nat → β → m (ForInStep β)) : m β :=
let rec @[specialize] loop (i : Nat) (j : Nat) (b : β) : m β := do
if j ≥ range.stop then
pure b
else match i with
| 0 => pure b
| i+1 => match ← f j b with
| ForInStep.done b => pure b
| ForInStep.yield b => loop i (j + range.step) b
loop range.stop range.start init
syntax:max "[" ":" term "]" : term
syntax:max "[" term ":" term "]" : term
syntax:max "[" ":" term ":" term "]" : term
syntax:max "[" term ":" term ":" term "]" : term
macro_rules
| `([ : $stop]) => `({ stop := $stop : Range })
| `([ $start : $stop ]) => `({ start := $start, stop := $stop : Range })
| `([ $start : $stop : $step ]) => `({ start := $start, stop := $stop, step := $step : Range })
| `([ : $stop : $step ]) => `({ stop := $stop, step := $step : Range })
end Range
end Std
|
1c34742626f8873a46f307fbae2c45342783e252 | 59aed81a2ce7741e690907fc374be338f4f88b6f | /src/math-688/lectures/lec-27.lean | ee4c87bd6b9fd34b30f1febeba7f2b5b5514682a | [] | no_license | agusakov/math-688-lean | c84d5e1423eb208a0281135f0214b91b30d0ef48 | 67dc27ebff55a74c6b5a1c469ba04e7981d2e550 | refs/heads/main | 1,679,699,340,788 | 1,616,602,782,000 | 1,616,602,782,000 | 332,894,454 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 148 | lean | /- 4 Nov 2019 -/
-- tutte
-- petersen (3-regular graph perfect matching)
-- edge-connectivity -> generalization of petersen
-- k-factor
-- f-factor |
290e04d855d078ff14305e664697d6eb737f8d9c | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/data/polynomial/reverse.lean | 9031da8f4d1acb1f78b91177cf4f34a76eb7b1d1 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 11,199 | lean | /-
Copyright (c) 2020 Damiano Testa. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Damiano Testa
-/
import data.polynomial.erase_lead
import data.polynomial.degree
/-!
# Reverse of a univariate polynomial
The main definition is `reverse`. Applying `reverse` to a polynomial `f : polynomial R` produces
the polynomial with a reversed list of coefficients, equivalent to `X^f.nat_degree * f(1/X)`.
The main result is that `reverse (f * g) = reverse (f) * reverse (g)`, provided the leading
coefficients of `f` and `g` do not multiply to zero.
-/
namespace polynomial
open polynomial finsupp finset
open_locale classical
section semiring
variables {R : Type*} [semiring R] {f : polynomial R}
/-- If `i ≤ N`, then `rev_at_fun N i` returns `N - i`, otherwise it returns `i`.
This is the map used by the embedding `rev_at`.
-/
def rev_at_fun (N i : ℕ) : ℕ := ite (i ≤ N) (N-i) i
lemma rev_at_fun_invol {N i : ℕ} : rev_at_fun N (rev_at_fun N i) = i :=
begin
unfold rev_at_fun,
split_ifs with h j,
{ exact nat.sub_sub_self h, },
{ exfalso,
apply j,
exact nat.sub_le N i, },
{ refl, },
end
lemma rev_at_fun_inj {N : ℕ} : function.injective (rev_at_fun N) :=
begin
intros a b hab,
rw [← @rev_at_fun_invol N a, hab, rev_at_fun_invol],
end
/-- If `i ≤ N`, then `rev_at N i` returns `N - i`, otherwise it returns `i`.
Essentially, this embedding is only used for `i ≤ N`.
The advantage of `rev_at N i` over `N - i` is that `rev_at` is an involution.
-/
def rev_at (N : ℕ) : function.embedding ℕ ℕ :=
{ to_fun := λ i , (ite (i ≤ N) (N-i) i),
inj' := rev_at_fun_inj }
/-- We prefer to use the bundled `rev_at` over unbundled `rev_at_fun`. -/
@[simp] lemma rev_at_fun_eq (N i : ℕ) : rev_at_fun N i = rev_at N i := rfl
@[simp] lemma rev_at_invol {N i : ℕ} : (rev_at N) (rev_at N i) = i :=
rev_at_fun_invol
@[simp] lemma rev_at_le {N i : ℕ} (H : i ≤ N) : rev_at N i = N - i :=
if_pos H
lemma rev_at_add {N O n o : ℕ} (hn : n ≤ N) (ho : o ≤ O) :
rev_at (N + O) (n + o) = rev_at N n + rev_at O o :=
begin
rcases nat.le.dest hn with ⟨n', rfl⟩,
rcases nat.le.dest ho with ⟨o', rfl⟩,
repeat { rw rev_at_le (le_add_right rfl.le) },
rw [add_assoc, add_left_comm n' o, ← add_assoc, rev_at_le (le_add_right rfl.le)],
repeat {rw nat.add_sub_cancel_left},
end
/-- `reflect N f` is the polynomial such that `(reflect N f).coeff i = f.coeff (rev_at N i)`.
In other words, the terms with exponent `[0, ..., N]` now have exponent `[N, ..., 0]`.
In practice, `reflect` is only used when `N` is at least as large as the degree of `f`.
Eventually, it will be used with `N` exactly equal to the degree of `f`. -/
noncomputable def reflect (N : ℕ) : polynomial R → polynomial R
| ⟨f⟩ := ⟨finsupp.emb_domain (rev_at N) f⟩
lemma reflect_support (N : ℕ) (f : polynomial R) :
(reflect N f).support = image (rev_at N) f.support :=
begin
rcases f,
ext1,
rw [reflect, mem_image, support, support, support_emb_domain, mem_map],
end
@[simp] lemma coeff_reflect (N : ℕ) (f : polynomial R) (i : ℕ) :
coeff (reflect N f) i = f.coeff (rev_at N i) :=
begin
rcases f,
simp only [reflect, coeff],
calc finsupp.emb_domain (rev_at N) f i
= finsupp.emb_domain (rev_at N) f (rev_at N (rev_at N i)) : by rw rev_at_invol
... = f (rev_at N i) : finsupp.emb_domain_apply _ _ _
end
@[simp] lemma reflect_zero {N : ℕ} : reflect N (0 : polynomial R) = 0 := rfl
@[simp] lemma reflect_eq_zero_iff {N : ℕ} {f : polynomial R} :
reflect N (f : polynomial R) = 0 ↔ f = 0 :=
by { rcases f, simp [reflect, ← zero_to_finsupp] }
@[simp] lemma reflect_add (f g : polynomial R) (N : ℕ) :
reflect N (f + g) = reflect N f + reflect N g :=
by { ext, simp only [coeff_add, coeff_reflect], }
@[simp] lemma reflect_C_mul (f : polynomial R) (r : R) (N : ℕ) :
reflect N (C r * f) = C r * (reflect N f) :=
by { ext, simp only [coeff_reflect, coeff_C_mul], }
@[simp] lemma reflect_C_mul_X_pow (N n : ℕ) {c : R} :
reflect N (C c * X ^ n) = C c * X ^ (rev_at N n) :=
begin
ext,
rw [reflect_C_mul, coeff_C_mul, coeff_C_mul, coeff_X_pow, coeff_reflect],
split_ifs with h j,
{ rw [h, rev_at_invol, coeff_X_pow_self], },
{ rw [not_mem_support_iff.mp],
intro a,
rw [← one_mul (X ^ n), ← C_1] at a,
apply h,
rw [← (mem_support_C_mul_X_pow a), rev_at_invol], },
end
@[simp] lemma reflect_monomial (N n : ℕ) : reflect N ((X : polynomial R) ^ n) = X ^ (rev_at N n) :=
by rw [← one_mul (X ^ n), ← one_mul (X ^ (rev_at N n)), ← C_1, reflect_C_mul_X_pow]
lemma reflect_mul_induction (cf cg : ℕ) :
∀ N O : ℕ, ∀ f g : polynomial R,
f.support.card ≤ cf.succ → g.support.card ≤ cg.succ → f.nat_degree ≤ N → g.nat_degree ≤ O →
(reflect (N + O) (f * g)) = (reflect N f) * (reflect O g) :=
begin
induction cf with cf hcf,
--first induction (left): base case
{ induction cg with cg hcg,
-- second induction (right): base case
{ intros N O f g Cf Cg Nf Og,
rw [← C_mul_X_pow_eq_self Cf, ← C_mul_X_pow_eq_self Cg],
simp only [mul_assoc, X_pow_mul, ← pow_add X, reflect_C_mul, reflect_monomial,
add_comm, rev_at_add Nf Og] },
-- second induction (right): induction step
{ intros N O f g Cf Cg Nf Og,
by_cases g0 : g = 0,
{ rw [g0, reflect_zero, mul_zero, mul_zero, reflect_zero], },
rw [← erase_lead_add_C_mul_X_pow g, mul_add, reflect_add, reflect_add, mul_add, hcg, hcg];
try { assumption },
{ exact le_add_left card_support_C_mul_X_pow_le_one },
{ exact (le_trans (nat_degree_C_mul_X_pow_le g.leading_coeff g.nat_degree) Og) },
{ exact nat.lt_succ_iff.mp (gt_of_ge_of_gt Cg (erase_lead_support_card_lt g0)) },
{ exact le_trans erase_lead_nat_degree_le Og } } },
--first induction (left): induction step
{ intros N O f g Cf Cg Nf Og,
by_cases f0 : f = 0,
{ rw [f0, reflect_zero, zero_mul, zero_mul, reflect_zero], },
rw [← erase_lead_add_C_mul_X_pow f, add_mul, reflect_add, reflect_add, add_mul, hcf, hcf];
try { assumption },
{ exact le_add_left card_support_C_mul_X_pow_le_one },
{ exact (le_trans (nat_degree_C_mul_X_pow_le f.leading_coeff f.nat_degree) Nf) },
{ exact nat.lt_succ_iff.mp (gt_of_ge_of_gt Cf (erase_lead_support_card_lt f0)) },
{ exact (le_trans erase_lead_nat_degree_le Nf) } },
end
@[simp] theorem reflect_mul
(f g : polynomial R) {F G : ℕ} (Ff : f.nat_degree ≤ F) (Gg : g.nat_degree ≤ G) :
reflect (F + G) (f * g) = reflect F f * reflect G g :=
reflect_mul_induction _ _ F G f g f.support.card.le_succ g.support.card.le_succ Ff Gg
/-- The reverse of a polynomial f is the polynomial obtained by "reading f backwards".
Even though this is not the actual definition, reverse f = f (1/X) * X ^ f.nat_degree. -/
noncomputable def reverse (f : polynomial R) : polynomial R := reflect f.nat_degree f
lemma coeff_reverse (f : polynomial R) (n : ℕ) :
f.reverse.coeff n = f.coeff (rev_at f.nat_degree n) :=
by rw [reverse, coeff_reflect]
@[simp] lemma coeff_zero_reverse (f : polynomial R) : coeff (reverse f) 0 = leading_coeff f :=
by rw [coeff_reverse, rev_at_le (zero_le f.nat_degree), nat.sub_zero, leading_coeff]
@[simp] lemma reverse_zero : reverse (0 : polynomial R) = 0 := rfl
@[simp] lemma reverse_eq_zero : f.reverse = 0 ↔ f = 0 :=
by simp [reverse]
lemma reverse_nat_degree_le (f : polynomial R) : f.reverse.nat_degree ≤ f.nat_degree :=
begin
rw [nat_degree_le_iff_degree_le, degree_le_iff_coeff_zero],
intros n hn,
rw with_bot.coe_lt_coe at hn,
rw [coeff_reverse, rev_at, function.embedding.coe_fn_mk,
if_neg (not_le_of_gt hn), coeff_eq_zero_of_nat_degree_lt hn],
end
lemma nat_degree_eq_reverse_nat_degree_add_nat_trailing_degree (f : polynomial R) :
f.nat_degree = f.reverse.nat_degree + f.nat_trailing_degree :=
begin
by_cases hf : f = 0,
{ rw [hf, reverse_zero, nat_degree_zero, nat_trailing_degree_zero] },
apply le_antisymm,
{ apply nat.le_add_of_sub_le_right,
apply le_nat_degree_of_ne_zero,
rw [reverse, coeff_reflect, ←rev_at_le f.nat_trailing_degree_le_nat_degree, rev_at_invol],
exact trailing_coeff_nonzero_iff_nonzero.mpr hf },
{ rw ← nat.le_sub_left_iff_add_le f.reverse_nat_degree_le,
apply nat_trailing_degree_le_of_ne_zero,
have key := mt leading_coeff_eq_zero.mp (mt reverse_eq_zero.mp hf),
rwa [leading_coeff, coeff_reverse, rev_at_le f.reverse_nat_degree_le] at key },
end
lemma reverse_nat_degree (f : polynomial R) :
f.reverse.nat_degree = f.nat_degree - f.nat_trailing_degree :=
by rw [f.nat_degree_eq_reverse_nat_degree_add_nat_trailing_degree, nat.add_sub_cancel]
lemma reverse_leading_coeff (f : polynomial R) : f.reverse.leading_coeff = f.trailing_coeff :=
by rw [leading_coeff, reverse_nat_degree, ←rev_at_le f.nat_trailing_degree_le_nat_degree,
coeff_reverse, rev_at_invol, trailing_coeff]
lemma reverse_nat_trailing_degree (f : polynomial R) : f.reverse.nat_trailing_degree = 0 :=
begin
by_cases hf : f = 0,
{ rw [hf, reverse_zero, nat_trailing_degree_zero] },
{ rw ← nat.le_zero_iff,
apply nat_trailing_degree_le_of_ne_zero,
rw [coeff_zero_reverse],
exact mt leading_coeff_eq_zero.mp hf },
end
lemma reverse_trailing_coeff (f : polynomial R) : f.reverse.trailing_coeff = f.leading_coeff :=
by rw [trailing_coeff, reverse_nat_trailing_degree, coeff_zero_reverse]
theorem reverse_mul {f g : polynomial R} (fg : f.leading_coeff * g.leading_coeff ≠ 0) :
reverse (f * g) = reverse f * reverse g :=
begin
unfold reverse,
rw [nat_degree_mul' fg, reflect_mul f g rfl.le rfl.le],
end
@[simp] lemma reverse_mul_of_domain {R : Type*} [domain R] (f g : polynomial R) :
reverse (f * g) = reverse f * reverse g :=
begin
by_cases f0 : f=0,
{ simp only [f0, zero_mul, reverse_zero], },
by_cases g0 : g=0,
{ rw [g0, mul_zero, reverse_zero, mul_zero], },
simp [reverse_mul, *],
end
lemma trailing_coeff_mul {R : Type*} [integral_domain R] (p q : polynomial R) :
(p * q).trailing_coeff = p.trailing_coeff * q.trailing_coeff :=
by rw [←reverse_leading_coeff, reverse_mul_of_domain, leading_coeff_mul,
reverse_leading_coeff, reverse_leading_coeff]
@[simp] lemma coeff_one_reverse (f : polynomial R) : coeff (reverse f) 1 = next_coeff f :=
begin
rw [coeff_reverse, next_coeff],
split_ifs with hf,
{ have : coeff f 1 = 0 := coeff_eq_zero_of_nat_degree_lt (by simp only [hf, zero_lt_one]),
simp [*, rev_at] },
{ rw rev_at_le,
exact nat.succ_le_iff.2 (pos_iff_ne_zero.2 hf) }
end
end semiring
section ring
variables {R : Type*} [ring R]
@[simp] lemma reflect_neg (f : polynomial R) (N : ℕ) :
reflect N (- f) = - reflect N f :=
by rw [neg_eq_neg_one_mul, ←C_1, ←C_neg, reflect_C_mul, C_neg, C_1, ←neg_eq_neg_one_mul]
@[simp] lemma reflect_sub (f g : polynomial R) (N : ℕ) :
reflect N (f - g) = reflect N f - reflect N g :=
by rw [sub_eq_add_neg, sub_eq_add_neg, reflect_add, reflect_neg]
@[simp] lemma reverse_neg (f : polynomial R) :
reverse (- f) = - reverse f :=
by rw [reverse, reverse, reflect_neg, nat_degree_neg]
end ring
end polynomial
|
1657f7f23236074179f9556ab1c1cd8aaf57371a | 2bafba05c98c1107866b39609d15e849a4ca2bb8 | /src/week_6/Part_A_tendsto.lean | f4321ebf88b58845890c6766e2822025fb434b1a | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/formalising-mathematics | b54c83c94b5c315024ff09997fcd6b303892a749 | 7cf1d51c27e2038d2804561d63c74711924044a1 | refs/heads/master | 1,651,267,046,302 | 1,638,888,459,000 | 1,638,888,459,000 | 331,592,375 | 284 | 24 | Apache-2.0 | 1,669,593,705,000 | 1,611,224,849,000 | Lean | UTF-8 | Lean | false | false | 11,443 | lean | import order.filter.basic
/-
# tendsto
Here's an overview of the main definition we're learning today.
If `X` and `Y` are types, `φ : X → Y` is a function,
and `F : filter X` and `G : filter Y` are filters, then
`filter.tendsto φ F G`
is a true-false statement, which is pronounced something like
"`F` tends to `G` along `φ`". Of course we will `open filter`
in this file, so you can just write `tendsto φ F G`, or if
you like the dot notation you can even write `F.tendsto φ G`.
## Geometric meaning of `tendsto`.
Let's start by thinking about the easy case where `F` and `G`
are actually subsets of `X` and `Y` (that is, principal filters,
associated to sets which we will also call `F` and `G`). In this case,
`tendsto φ F G` simply means "`φ` restricts to a function
from `F` to `G`", or in other words `∀ x ∈ F, φ(x) ∈ G`.
There are two other ways of writing this predicate. The first
involves pushing a set forward along a map. If `F` is a subset of `X`
then let `φ(F)` denote the image of `F` under `φ`, that
is, the subset `{y : Y | ∃ x : X, φ x = y}` of `Y`.
Then `tendsto φ F G` simply means `φ(F) ⊆ G`.
The second involves pulling a set back along a map. If `G` is a subset
of `Y` then let `φ⁻¹(G)` denote the preimage of `G` under `φ`,
that is, the subset `{x : X | φ x ∈ G}` of `Y`. Then `tendsto φ F G`
simply means `F ⊆ φ⁻¹(G)`.
This is how it all works in the case of sets. What we need to
do today is to figure out how to push forward and pull back
filters along a map `φ`. Once we have done this, then we can
prove `φ(F) ≤ G ↔ F ≤ φ⁻¹(G)` and use either one of these
as our definition of `tendsto φ F G` -- it doesn't matter which.
## Digression : adjoint functors.
The discussion below is not needed to be able to do this week's
problems, but it might provide some helpful background for some.
Also note that anyone who still doens't like the word "type" can
literally just change it for the word "set" (and change "term of
type" to "element of set"), which is how arguments
of the below kind would appear in the traditional mathematical
literature.
Partially ordered types, such as the type of subsets of a fixed
type `X` or the type of filters on `X`, are actually very simple
examples of categories. In general if `P` is a partially ordered type
and `x,y` are terms of type `P` then the idea is that we can
define `Hom(x,y)` to have exactly one element if `x ≤ y` is true,
and no elements at all if `x ≤ y` is false. The structure/axioms for
a category are that `Hom(x,x)` is supposed to have an identity
element, which follows from reflexivity of `≤`, and that one can
compose morphisms, which follows from transitivity of `≤`.
Antisymmetry states that if two objects are isomorphic (i.e.,
in this case, if `Hom(x,y)` and `Hom(y,x)` are both nonempty),
then they are equal. If `φ : X → Y` is a map of types, then
pushing forward subsets and pulling back subsets are both
functors from `set X` to `set Y`, because `S ⊆ T → φ(S) ⊆ φ(T)`
and `U ⊆ V → φ⁻¹(U) ⊆ φ⁻¹(V)`. The statement that
`φ(S) ≤ U ↔ S ≤ φ⁻¹(U)` is simply the statement that these functors
are adjoint to each other. Today we will define pushforward and
pullback of filters, and show that they are also a pair of
adjoint functors, but we will not use this language. In fact there
is a special language for adjoint functors in this simple situation:
we will say that pushforward and pullback form a Galois connection.
-/
/-
## Warm-up: pushing forward and pulling back subsets.
Say `X` and `Y` are types, and `f : X → Y`.
-/
variables (X Y : Type) (f : X → Y)
/-
### images
In Lean, the image `f(S)` of a subset `S : set X` cannot
be denoted `f S`, because `f` expects an _element_ of `X` as
an input, not a subset of `X`, so we need new notation.
Notation : `f '' S` is the image of `S` under `f`. Let's
check this.
-/
example (S : set X) : f '' S = {y : Y | ∃ x : X, x ∈ S ∧ f x = y} :=
begin
-- true by definition
refl
end
/-
### preimages
In Lean, the preimage `f⁻¹(T)` of a subset `T : set Y` cannot
be denoted `f⁻¹ T` because `⁻¹` is the inverse notation in group
theory, so if anything would be a function from `Y` to `X`,
not a function on subsets of `Y`.
Notation : `f ⁻¹' T` is the preimage of `T` under `f`. Let's
check this.
Pro shortcut: `\-'` for `⁻¹'`
-/
example (T : set Y) : f ⁻¹' T = {x : X | f x ∈ T} :=
begin
-- true by definition
refl
end
/-
I claim that the following conditions on `S : set X` and `T : set Y`
are equivalent:
1) `f '' S ⊆ T`
2) `S ⊆ f⁻¹' T`
Indeed, they both say that `f` restricts to a function from `S` to `T`.
Let's check this. You might find
`mem_preimage : a ∈ f ⁻¹' s ↔ f a ∈ s`
and
-/
open set
example (S : set X) (T : set Y) : f '' S ⊆ T ↔ S ⊆ f⁻¹' T :=
begin
sorry,
end
/-
## Pushing forward filters.
Pushing forward is easy, so let's do that first.
It's called `filter.map` in Lean.
We define the pushforward filter `map f F` on `Y` to be the
obvious thing: a subset of `Y` is in the filter iff `f⁻¹(Y)`
is in `F`. Let's check this is a filter.
Reminder of some helpful lemmas:
In `set`:
`mem_set_of_eq : a ∈ {x : α | p x} = p a` -- definitional
In `filter`:
`univ_mem_sets : univ ∈ F`
`mem_sets_of_superset : S ∈ F → S ⊆ T → T ∈ F`
`inter_mem_sets : S ∈ F → T ∈ F → S ∩ T ∈ F`
-/
open filter
-- this is called `F.map f` or `filter.map f F`
-- or just `map f F` if `filter` is open.
example (F : filter X) : filter Y :=
{ sets := {T : set Y | f ⁻¹' T ∈ F },
univ_sets := begin
sorry
end,
sets_of_superset := begin
sorry,
end,
inter_sets := begin
sorry
end, }
-- this is `filter.mem_map` and it's true by definition.
-- It's useful in the form `rw mem_map` if you want to figure out
-- what's going on in a proof, but often you'll find you can
-- delete it at the end.
example (F : filter X) (T : set Y) : T ∈ F.map f ↔ f ⁻¹' T ∈ F :=
begin
-- true by definition
refl
end
-- Let's check that `map` satisfies some basic functorialities.
-- Recall that if your goal is to check two filters are
-- equal then you can use the `ext` tactic, e.g. with `ext S`.
-- pushing along the identity map id : X → X doesn't change the filter.
-- this is `filter.map_id` but see if you can prove it yourself.
example (F : filter X) : F.map id = F :=
begin
sorry
end
-- pushing along g ∘ f is the same as pushing along f and then g
-- for some reason this isn't in mathlib, instead they have `map_map` which
-- has the equality the other way.
variables (Z : Type) (g : Y → Z)
-- this isn't in mathlib, but `filter.map_map` is the equality the other
-- way around. See if you can prove it yourself.
example (F : filter X) : F.map (g ∘ f) = (F.map f).map g :=
begin
sorry,
end
open_locale filter -- for 𝓟 notation
-- pushing the principal filter `𝓟 S` along `f` gives `𝓟 (f '' S)`
-- this is `filter.map_principal` but see if you can prove it yourself.
example (S : set X) : (𝓟 S).map f = 𝓟 (f '' S) :=
begin
sorry,
end
/-
## tendsto
The definition: if `f : X → Y` and `F : filter X` and `G : filter Y`
then `tendsto f F G : Prop := map f F ≤ G`. It's pronounced something
like "`F` tends to `G` along `f`". This is a *definition* (it
has type `Prop`), not the proof of a theorem. It is a true-false statement
attached to `f`, `F` and `G`, it's a bit like saying "f is continuous at x"
or something like that, it might be true and it might be false.
The mental model you might want to have of the definition is that
`tendsto f F G` means that the function `f` restricts to a function
from the generalized set `F` to the generalized set `G`.
-/
-- this is `filter.tendsto_def`
example (F : filter X) (G : filter Y) :
tendsto f F G ↔ ∀ T : set Y, T ∈ G → f ⁻¹' T ∈ F :=
begin
-- true by definition
refl
end
-- Let's make a basic API for `tendsto`
-- this is `tendsto_id` but see if you can prove it yourself.
example (F : filter X) : tendsto id F F :=
begin
sorry,
end
-- this is `tendsto.comp` but see if you can prove it yourself
example (F : filter X) (G : filter Y) (H : filter Z)
(f : X → Y) (g : Y → Z)
(hf : tendsto f F G) (hg : tendsto g G H) : tendsto (g ∘ f) F H :=
begin
sorry,
end
-- I would recommend looking at the model answer to this one if
-- you get stuck.
lemma tendsto_comp_map (g : Y → Z) (F : filter X) (G : filter Z) :
tendsto (g ∘ f) F G ↔ tendsto g (F.map f) G :=
begin
sorry,
end
/-
## Pulling back filters
We don't use this in the next part.
Say `f : X → Y` and `G : filter Y`, and we want a filter on `X`. Let's make a
naive definition. We want a collection of subsets of `X` corresponding to the
filter obtained by pulling back `G` along `f`. When should `S : set X` be
in this filter? Perhaps it is when `f '' S ∈ G`. However, there is no reason
that the collection of `S` satisfying this property should be a filter
on `X`. For example, there is no reason to espect that `f '' univ ∈ G`
if `f` is not surjective. Our naive guess doesn't work.
Here's a way of fixing this, by coming up with a less naive guess which
is informed by our mental model. Remember that our model of a filter `G` is some
kind of generalised notion of a set. If `T : set Y` then `T ∈ G` is supposed to
mean that the "set" `G` is a subset of `T`. So this should imply
that `f⁻¹(G) ⊆ f⁻¹(T)`. In particular, if `T ∈ G` and `f⁻¹(T) ⊆ S` then this
should mean `f⁻¹(G) ⊆ S` and hence `S ∈ f⁻¹(G)`. Let's try this condition
(defining `S ∈ f⁻¹(G)` to mean `∃ T ∈ G, f⁻¹(T) ⊆ S`) and see if it works.
Random useful lemmas (you might be getting to the point where you can
guess the names of the lemmas):
`subset_univ S : S ⊆ univ`
`subset.trans : A ⊆ B → B ⊆ C → A ⊆ C`
-/
-- this is called filter.comap
example (G : filter Y) : filter X :=
{ sets := {S : set X | ∃ T ∈ G, f ⁻¹' T ⊆ S},
univ_sets := begin
sorry
end,
sets_of_superset := begin
sorry
end,
inter_sets := begin
sorry
end }
-- Let's call this mem_comap
lemma mem_comap (f : X → Y) (G : filter Y) (S : set X) :
S ∈ comap f G ↔ ∃ T ∈ G, f ⁻¹' T ⊆ S :=
begin
-- true by definition
refl
end
-- If you want to, you can check some preliminary properties of `comap`.
-- this is comap_id
example (G : filter Y) : comap id G = G :=
begin
sorry
end
-- this is comap_comap but the other way around
lemma comap_comp (H : filter Z) : comap (g ∘ f) H = comap f (comap g H) :=
begin
sorry
end
-- this is comap_principal. Remember `mem_principal_sets`! It's true by definition...
example (T : set Y) : comap f (𝓟 T) = 𝓟 (f ⁻¹' T) :=
begin
sorry
end
-- This is the proof that `map f` and `comap f` are adjoint functors,
-- or in other words form a Galois connection. It is the "generalised set"
-- analogue of the assertion that if S is a subset of X and T is a subset of Y
-- then f(S) ⊆ T ↔ S ⊆ f⁻¹(T), these both being ways to say that `f` restricts
-- to a function from `S` to `T`.
lemma filter.galois_connection (F : filter X) (G : filter Y) :
map f F ≤ G ↔ F ≤ comap f G :=
begin
sorry,
end
-- indeed, `map f` and `comap f` form a Galois connection.
example : galois_connection (map f) (comap f) :=
filter.galois_connection X Y f
|
b9bdc2b727e0243ec5af3a93f1c4afe63cf9bdcc | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/set_theory/cardinal/cofinality.lean | af6a6652218cebf5295ba33dcdae536bfcd25832 | [
"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 | 45,772 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Floris van Doorn, Violeta Hernández Palacios
-/
import set_theory.cardinal.ordinal
import set_theory.ordinal.fixed_point
/-!
# Cofinality
This file contains the definition of cofinality of an ordinal number and regular cardinals
## Main Definitions
* `ordinal.cof o` is the cofinality of the ordinal `o`.
If `o` is the order type of the relation `<` on `α`, then `o.cof` is the smallest cardinality of a
subset `s` of α that is *cofinal* in `α`, i.e. `∀ x : α, ∃ y ∈ s, ¬ y < x`.
* `cardinal.is_limit c` means that `c` is a (weak) limit cardinal: `c ≠ 0 ∧ ∀ x < c, succ x < c`.
* `cardinal.is_strong_limit c` means that `c` is a strong limit cardinal:
`c ≠ 0 ∧ ∀ x < c, 2 ^ x < c`.
* `cardinal.is_regular c` means that `c` is a regular cardinal: `ℵ₀ ≤ c ∧ c.ord.cof = c`.
* `cardinal.is_inaccessible c` means that `c` is strongly inaccessible:
`ℵ₀ < c ∧ is_regular c ∧ is_strong_limit c`.
## Main Statements
* `ordinal.infinite_pigeonhole_card`: the infinite pigeonhole principle
* `cardinal.lt_power_cof`: A consequence of König's theorem stating that `c < c ^ c.ord.cof` for
`c ≥ ℵ₀`
* `cardinal.univ_inaccessible`: The type of ordinals in `Type u` form an inaccessible cardinal
(in `Type v` with `v > u`). This shows (externally) that in `Type u` there are at least `u`
inaccessible cardinals.
## Implementation Notes
* The cofinality is defined for ordinals.
If `c` is a cardinal number, its cofinality is `c.ord.cof`.
## Tags
cofinality, regular cardinals, limits cardinals, inaccessible cardinals,
infinite pigeonhole principle
-/
noncomputable theory
open function cardinal set order
open_locale classical cardinal ordinal
universes u v w
variables {α : Type*} {r : α → α → Prop}
/-! ### Cofinality of orders -/
namespace order
/-- Cofinality of a reflexive order `≼`. This is the smallest cardinality
of a subset `S : set α` such that `∀ a, ∃ b ∈ S, a ≼ b`. -/
def cof (r : α → α → Prop) : cardinal :=
Inf {c | ∃ S : set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = c}
/-- The set in the definition of `order.cof` is nonempty. -/
theorem cof_nonempty (r : α → α → Prop) [is_refl α r] :
{c | ∃ S : set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = c}.nonempty :=
⟨_, set.univ, λ a, ⟨a, ⟨⟩, refl _⟩, rfl⟩
lemma cof_le (r : α → α → Prop) {S : set α} (h : ∀ a, ∃ b ∈ S, r a b) : cof r ≤ #S :=
cInf_le' ⟨S, h, rfl⟩
lemma le_cof {r : α → α → Prop} [is_refl α r] (c : cardinal) :
c ≤ cof r ↔ ∀ {S : set α}, (∀ a, ∃ b ∈ S, r a b) → c ≤ #S :=
begin
rw [cof, le_cInf_iff'' (cof_nonempty r)],
use λ H S h, H _ ⟨S, h, rfl⟩,
rintro H d ⟨S, h, rfl⟩,
exact H h
end
end order
theorem rel_iso.cof_le_lift {α : Type u} {β : Type v} {r : α → α → Prop} {s}
[is_refl β s] (f : r ≃r s) :
cardinal.lift.{max u v} (order.cof r) ≤ cardinal.lift.{max u v} (order.cof s) :=
begin
rw [order.cof, order.cof, lift_Inf, lift_Inf,
le_cInf_iff'' (nonempty_image_iff.2 (order.cof_nonempty s))],
rintros - ⟨-, ⟨u, H, rfl⟩, rfl⟩,
apply cInf_le',
refine ⟨_, ⟨f.symm '' u, λ a, _, rfl⟩,
lift_mk_eq.{u v (max u v)}.2 ⟨((f.symm).to_equiv.image u).symm⟩⟩,
rcases H (f a) with ⟨b, hb, hb'⟩,
refine ⟨f.symm b, mem_image_of_mem _ hb, f.map_rel_iff.1 _⟩,
rwa rel_iso.apply_symm_apply
end
theorem rel_iso.cof_eq_lift {α : Type u} {β : Type v} {r s}
[is_refl α r] [is_refl β s] (f : r ≃r s) :
cardinal.lift.{max u v} (order.cof r) = cardinal.lift.{max u v} (order.cof s) :=
(rel_iso.cof_le_lift f).antisymm (rel_iso.cof_le_lift f.symm)
theorem rel_iso.cof_le {α β : Type u} {r : α → α → Prop} {s} [is_refl β s] (f : r ≃r s) :
order.cof r ≤ order.cof s :=
lift_le.1 (rel_iso.cof_le_lift f)
theorem rel_iso.cof_eq {α β : Type u} {r s} [is_refl α r] [is_refl β s] (f : r ≃r s) :
order.cof r = order.cof s :=
lift_inj.1 (rel_iso.cof_eq_lift f)
/-- Cofinality of a strict order `≺`. This is the smallest cardinality of a set `S : set α` such
that `∀ a, ∃ b ∈ S, ¬ b ≺ a`. -/
def strict_order.cof (r : α → α → Prop) : cardinal :=
order.cof (swap r)ᶜ
/-- The set in the definition of `order.strict_order.cof` is nonempty. -/
theorem strict_order.cof_nonempty (r : α → α → Prop) [is_irrefl α r] :
{c | ∃ S : set α, unbounded r S ∧ #S = c}.nonempty :=
@order.cof_nonempty α _ (is_refl.swap rᶜ)
/-! ### Cofinality of ordinals -/
namespace ordinal
/-- Cofinality of an ordinal. This is the smallest cardinal of a
subset `S` of the ordinal which is unbounded, in the sense
`∀ a, ∃ b ∈ S, a ≤ b`. It is defined for all ordinals, but
`cof 0 = 0` and `cof (succ o) = 1`, so it is only really
interesting on limit ordinals (when it is an infinite cardinal). -/
def cof (o : ordinal.{u}) : cardinal.{u} :=
o.lift_on (λ a, strict_order.cof a.r)
begin
rintros ⟨α, r, wo₁⟩ ⟨β, s, wo₂⟩ ⟨⟨f, hf⟩⟩,
haveI := wo₁, haveI := wo₂,
apply @rel_iso.cof_eq _ _ _ _ _ _ ,
{ split, exact λ a b, not_iff_not.2 hf },
{ exact ⟨(is_well_order.is_irrefl r).1⟩ },
{ exact ⟨(is_well_order.is_irrefl s).1⟩ }
end
lemma cof_type (r : α → α → Prop) [is_well_order α r] : (type r).cof = strict_order.cof r := rfl
theorem le_cof_type [is_well_order α r] {c} : c ≤ cof (type r) ↔ ∀ S, unbounded r S → c ≤ #S :=
(le_cInf_iff'' (strict_order.cof_nonempty r)).trans ⟨λ H S h, H _ ⟨S, h, rfl⟩,
by { rintros H d ⟨S, h, rfl⟩, exact H _ h }⟩
theorem cof_type_le [is_well_order α r] {S : set α} (h : unbounded r S) : cof (type r) ≤ #S :=
le_cof_type.1 le_rfl S h
theorem lt_cof_type [is_well_order α r] {S : set α} : #S < cof (type r) → bounded r S :=
by simpa using not_imp_not.2 cof_type_le
theorem cof_eq (r : α → α → Prop) [is_well_order α r] : ∃ S, unbounded r S ∧ #S = cof (type r) :=
Inf_mem (strict_order.cof_nonempty r)
theorem ord_cof_eq (r : α → α → Prop) [is_well_order α r] :
∃ S, unbounded r S ∧ type (subrel r S) = (cof (type r)).ord :=
let ⟨S, hS, e⟩ := cof_eq r, ⟨s, _, e'⟩ := cardinal.ord_eq S,
T : set α := {a | ∃ aS : a ∈ S, ∀ b : S, s b ⟨_, aS⟩ → r b a} in
begin
resetI, suffices,
{ refine ⟨T, this,
le_antisymm _ (cardinal.ord_le.2 $ cof_type_le this)⟩,
rw [← e, e'],
refine (rel_embedding.of_monotone (λ a : T, (⟨a, let ⟨aS, _⟩ := a.2 in aS⟩ : S)) (λ a b h, _))
.ordinal_type_le,
rcases a with ⟨a, aS, ha⟩, rcases b with ⟨b, bS, hb⟩,
change s ⟨a, _⟩ ⟨b, _⟩,
refine ((trichotomous_of s _ _).resolve_left (λ hn, _)).resolve_left _,
{ exact asymm h (ha _ hn) },
{ intro e, injection e with e, subst b,
exact irrefl _ h } },
{ intro a,
have : {b : S | ¬ r b a}.nonempty := let ⟨b, bS, ba⟩ := hS a in ⟨⟨b, bS⟩, ba⟩,
let b := (is_well_founded.wf).min _ this,
have ba : ¬r b a := (is_well_founded.wf).min_mem _ this,
refine ⟨b, ⟨b.2, λ c, not_imp_not.1 $ λ h, _⟩, ba⟩,
rw [show ∀b:S, (⟨b, b.2⟩:S) = b, by intro b; cases b; refl],
exact (is_well_founded.wf).not_lt_min _ this
(is_order_connected.neg_trans h ba) }
end
/-! ### Cofinality of suprema and least strict upper bounds -/
private theorem card_mem_cof {o} : ∃ {ι} (f : ι → ordinal), lsub.{u u} f = o ∧ #ι = o.card :=
⟨_, _, lsub_typein o, mk_ordinal_out o⟩
/-- The set in the `lsub` characterization of `cof` is nonempty. -/
theorem cof_lsub_def_nonempty (o) :
{a : cardinal | ∃ {ι} (f : ι → ordinal), lsub.{u u} f = o ∧ #ι = a}.nonempty :=
⟨_, card_mem_cof⟩
theorem cof_eq_Inf_lsub (o : ordinal.{u}) :
cof o = Inf {a : cardinal | ∃ {ι : Type u} (f : ι → ordinal), lsub.{u u} f = o ∧ #ι = a} :=
begin
refine le_antisymm (le_cInf (cof_lsub_def_nonempty o) _) (cInf_le' _),
{ rintros a ⟨ι, f, hf, rfl⟩,
rw ←type_lt o,
refine (cof_type_le (λ a, _)).trans (@mk_le_of_injective _ _
(λ s : (typein ((<) : o.out.α → o.out.α → Prop))⁻¹' (set.range f), classical.some s.prop)
(λ s t hst, let H := congr_arg f hst in by rwa [classical.some_spec s.prop,
classical.some_spec t.prop, typein_inj, subtype.coe_inj] at H)),
have := typein_lt_self a,
simp_rw [←hf, lt_lsub_iff] at this,
cases this with i hi,
refine ⟨enum (<) (f i) _, _, _⟩,
{ rw [type_lt, ←hf], apply lt_lsub },
{ rw [mem_preimage, typein_enum], exact mem_range_self i },
{ rwa [←typein_le_typein, typein_enum] } },
{ rcases cof_eq (<) with ⟨S, hS, hS'⟩,
let f : S → ordinal := λ s, typein (<) s.val,
refine ⟨S, f, le_antisymm (lsub_le (λ i, typein_lt_self i)) (le_of_forall_lt (λ a ha, _)),
by rwa type_lt o at hS'⟩,
rw ←type_lt o at ha,
rcases hS (enum (<) a ha) with ⟨b, hb, hb'⟩,
rw [←typein_le_typein, typein_enum] at hb',
exact hb'.trans_lt (lt_lsub.{u u} f ⟨b, hb⟩) }
end
@[simp] theorem lift_cof (o) : (cof o).lift = cof o.lift :=
begin
refine induction_on o _,
introsI α r _,
apply le_antisymm,
{ refine le_cof_type.2 (λ S H, _),
have : (#(ulift.up ⁻¹' S)).lift ≤ #S,
{ rw [← cardinal.lift_umax, ← cardinal.lift_id' (#S)],
exact mk_preimage_of_injective_lift ulift.up _ ulift.up_injective },
refine (cardinal.lift_le.2 $ cof_type_le _).trans this,
exact λ a, let ⟨⟨b⟩, bs, br⟩ := H ⟨a⟩ in ⟨b, bs, br⟩ },
{ rcases cof_eq r with ⟨S, H, e'⟩,
have : #(ulift.down ⁻¹' S) ≤ (#S).lift :=
⟨⟨λ ⟨⟨x⟩, h⟩, ⟨⟨x, h⟩⟩,
λ ⟨⟨x⟩, h₁⟩ ⟨⟨y⟩, h₂⟩ e, by simp at e; congr; injections⟩⟩,
rw e' at this,
unfreezingI { refine (cof_type_le _).trans this },
exact λ ⟨a⟩, let ⟨b, bs, br⟩ := H a in ⟨⟨b⟩, bs, br⟩ }
end
theorem cof_le_card (o) : cof o ≤ card o :=
by { rw cof_eq_Inf_lsub, exact cInf_le' card_mem_cof }
theorem cof_ord_le (c : cardinal) : c.ord.cof ≤ c :=
by simpa using cof_le_card c.ord
theorem ord_cof_le (o : ordinal.{u}) : o.cof.ord ≤ o :=
(ord_le_ord.2 (cof_le_card o)).trans (ord_card_le o)
theorem exists_lsub_cof (o : ordinal) : ∃ {ι} (f : ι → ordinal), lsub.{u u} f = o ∧ #ι = cof o :=
by { rw cof_eq_Inf_lsub, exact Inf_mem (cof_lsub_def_nonempty o) }
theorem cof_lsub_le {ι} (f : ι → ordinal) : cof (lsub.{u u} f) ≤ #ι :=
by { rw cof_eq_Inf_lsub, exact cInf_le' ⟨ι, f, rfl, rfl⟩ }
theorem cof_lsub_le_lift {ι} (f : ι → ordinal) : cof (lsub f) ≤ cardinal.lift.{v u} (#ι) :=
begin
rw ←mk_ulift,
convert cof_lsub_le (λ i : ulift ι, f i.down),
exact lsub_eq_of_range_eq.{u (max u v) max u v}
(set.ext (λ x, ⟨λ ⟨i, hi⟩, ⟨ulift.up i, hi⟩, λ ⟨i, hi⟩, ⟨_, hi⟩⟩))
end
theorem le_cof_iff_lsub {o : ordinal} {a : cardinal} :
a ≤ cof o ↔ ∀ {ι} (f : ι → ordinal), lsub.{u u} f = o → a ≤ #ι :=
begin
rw cof_eq_Inf_lsub,
exact (le_cInf_iff'' (cof_lsub_def_nonempty o)).trans ⟨λ H ι f hf, H _ ⟨ι, f, hf, rfl⟩,
λ H b ⟨ι, f, hf, hb⟩, ( by { rw ←hb, exact H _ hf} )⟩
end
theorem lsub_lt_ord_lift {ι} {f : ι → ordinal} {c : ordinal} (hι : cardinal.lift (#ι) < c.cof)
(hf : ∀ i, f i < c) : lsub.{u v} f < c :=
lt_of_le_of_ne (lsub_le hf) (λ h, by { subst h, exact (cof_lsub_le_lift f).not_lt hι })
theorem lsub_lt_ord {ι} {f : ι → ordinal} {c : ordinal} (hι : #ι < c.cof) :
(∀ i, f i < c) → lsub.{u u} f < c :=
lsub_lt_ord_lift (by rwa (#ι).lift_id)
theorem cof_sup_le_lift {ι} {f : ι → ordinal} (H : ∀ i, f i < sup f) : cof (sup f) ≤ (#ι).lift :=
by { rw ←sup_eq_lsub_iff_lt_sup at H, rw H, exact cof_lsub_le_lift f }
theorem cof_sup_le {ι} {f : ι → ordinal} (H : ∀ i, f i < sup.{u u} f) : cof (sup.{u u} f) ≤ #ι :=
by { rw ←(#ι).lift_id, exact cof_sup_le_lift H }
theorem sup_lt_ord_lift {ι} {f : ι → ordinal} {c : ordinal} (hι : cardinal.lift (#ι) < c.cof)
(hf : ∀ i, f i < c) : sup.{u v} f < c :=
(sup_le_lsub.{u v} f).trans_lt (lsub_lt_ord_lift hι hf)
theorem sup_lt_ord {ι} {f : ι → ordinal} {c : ordinal} (hι : #ι < c.cof) :
(∀ i, f i < c) → sup.{u u} f < c :=
sup_lt_ord_lift (by rwa (#ι).lift_id)
theorem supr_lt_lift {ι} {f : ι → cardinal} {c : cardinal} (hι : cardinal.lift (#ι) < c.ord.cof)
(hf : ∀ i, f i < c) : supr f < c :=
begin
rw [←ord_lt_ord, supr_ord (cardinal.bdd_above_range _)],
refine sup_lt_ord_lift hι (λ i, _),
rw ord_lt_ord,
apply hf
end
theorem supr_lt {ι} {f : ι → cardinal} {c : cardinal} (hι : #ι < c.ord.cof) :
(∀ i, f i < c) → supr f < c :=
supr_lt_lift (by rwa (#ι).lift_id)
theorem nfp_family_lt_ord_lift {ι} {f : ι → ordinal → ordinal} {c} (hc : ℵ₀ < cof c)
(hc' : (#ι).lift < cof c) (hf : ∀ i (b < c), f i b < c) {a} (ha : a < c) :
nfp_family.{u v} f a < c :=
begin
refine sup_lt_ord_lift ((cardinal.lift_le.2 (mk_list_le_max ι)).trans_lt _) (λ l, _),
{ rw lift_max,
apply max_lt _ hc',
rwa cardinal.lift_aleph_0 },
{ induction l with i l H,
{ exact ha },
{ exact hf _ _ H } }
end
theorem nfp_family_lt_ord {ι} {f : ι → ordinal → ordinal} {c} (hc : ℵ₀ < cof c)
(hc' : #ι < cof c) (hf : ∀ i (b < c), f i b < c) {a} : a < c → nfp_family.{u u} f a < c :=
nfp_family_lt_ord_lift hc (by rwa (#ι).lift_id) hf
theorem nfp_bfamily_lt_ord_lift {o : ordinal} {f : Π a < o, ordinal → ordinal} {c} (hc : ℵ₀ < cof c)
(hc' : o.card.lift < cof c) (hf : ∀ i hi (b < c), f i hi b < c) {a} :
a < c → nfp_bfamily.{u v} o f a < c :=
nfp_family_lt_ord_lift hc (by rwa mk_ordinal_out) (λ i, hf _ _)
theorem nfp_bfamily_lt_ord {o : ordinal} {f : Π a < o, ordinal → ordinal} {c} (hc : ℵ₀ < cof c)
(hc' : o.card < cof c) (hf : ∀ i hi (b < c), f i hi b < c) {a} :
a < c → nfp_bfamily.{u u} o f a < c :=
nfp_bfamily_lt_ord_lift hc (by rwa o.card.lift_id) hf
theorem nfp_lt_ord {f : ordinal → ordinal} {c} (hc : ℵ₀ < cof c) (hf : ∀ i < c, f i < c) {a} :
a < c → nfp f a < c :=
nfp_family_lt_ord_lift hc (by simpa using cardinal.one_lt_aleph_0.trans hc) (λ _, hf)
theorem exists_blsub_cof (o : ordinal) : ∃ (f : Π a < (cof o).ord, ordinal), blsub.{u u} _ f = o :=
begin
rcases exists_lsub_cof o with ⟨ι, f, hf, hι⟩,
rcases cardinal.ord_eq ι with ⟨r, hr, hι'⟩,
rw ←@blsub_eq_lsub' ι r hr at hf,
rw [←hι, hι'],
exact ⟨_, hf⟩
end
theorem le_cof_iff_blsub {b : ordinal} {a : cardinal} :
a ≤ cof b ↔ ∀ {o} (f : Π a < o, ordinal), blsub.{u u} o f = b → a ≤ o.card :=
le_cof_iff_lsub.trans ⟨λ H o f hf, by simpa using H _ hf, λ H ι f hf, begin
rcases cardinal.ord_eq ι with ⟨r, hr, hι'⟩,
rw ←@blsub_eq_lsub' ι r hr at hf,
simpa using H _ hf
end⟩
theorem cof_blsub_le_lift {o} (f : Π a < o, ordinal) :
cof (blsub o f) ≤ cardinal.lift.{v u} (o.card) :=
by { convert cof_lsub_le_lift _, exact (mk_ordinal_out o).symm }
theorem cof_blsub_le {o} (f : Π a < o, ordinal) : cof (blsub.{u u} o f) ≤ o.card :=
by { rw ←(o.card).lift_id, exact cof_blsub_le_lift f }
theorem blsub_lt_ord_lift {o : ordinal} {f : Π a < o, ordinal} {c : ordinal}
(ho : o.card.lift < c.cof) (hf : ∀ i hi, f i hi < c) : blsub.{u v} o f < c :=
lt_of_le_of_ne (blsub_le hf) (λ h, ho.not_le
(by simpa [←supr_ord, hf, h] using cof_blsub_le_lift.{u} f))
theorem blsub_lt_ord {o : ordinal} {f : Π a < o, ordinal} {c : ordinal} (ho : o.card < c.cof)
(hf : ∀ i hi, f i hi < c) : blsub.{u u} o f < c :=
blsub_lt_ord_lift (by rwa (o.card).lift_id) hf
theorem cof_bsup_le_lift {o : ordinal} {f : Π a < o, ordinal} (H : ∀ i h, f i h < bsup o f) :
cof (bsup o f) ≤ o.card.lift :=
by { rw ←bsup_eq_blsub_iff_lt_bsup at H, rw H, exact cof_blsub_le_lift f }
theorem cof_bsup_le {o : ordinal} {f : Π a < o, ordinal} :
(∀ i h, f i h < bsup.{u u} o f) → cof (bsup.{u u} o f) ≤ o.card :=
by { rw ←(o.card).lift_id, exact cof_bsup_le_lift }
theorem bsup_lt_ord_lift {o : ordinal} {f : Π a < o, ordinal} {c : ordinal}
(ho : o.card.lift < c.cof) (hf : ∀ i hi, f i hi < c) : bsup.{u v} o f < c :=
(bsup_le_blsub f).trans_lt (blsub_lt_ord_lift ho hf)
theorem bsup_lt_ord {o : ordinal} {f : Π a < o, ordinal} {c : ordinal} (ho : o.card < c.cof) :
(∀ i hi, f i hi < c) → bsup.{u u} o f < c :=
bsup_lt_ord_lift (by rwa (o.card).lift_id)
/-! ### Basic results -/
@[simp] theorem cof_zero : cof 0 = 0 :=
(cof_le_card 0).antisymm (cardinal.zero_le _)
@[simp] theorem cof_eq_zero {o} : cof o = 0 ↔ o = 0 :=
⟨induction_on o $ λ α r _ z, by exactI
let ⟨S, hl, e⟩ := cof_eq r in type_eq_zero_iff_is_empty.2 $
⟨λ a, let ⟨b, h, _⟩ := hl a in
(mk_eq_zero_iff.1 (e.trans z)).elim' ⟨_, h⟩⟩,
λ e, by simp [e]⟩
theorem cof_ne_zero {o} : cof o ≠ 0 ↔ o ≠ 0 := cof_eq_zero.not
@[simp] theorem cof_succ (o) : cof (succ o) = 1 :=
begin
apply le_antisymm,
{ refine induction_on o (λ α r _, _),
change cof (type _) ≤ _,
rw [← (_ : #_ = 1)], apply cof_type_le,
{ refine λ a, ⟨sum.inr punit.star, set.mem_singleton _, _⟩,
rcases a with a|⟨⟨⟨⟩⟩⟩; simp [empty_relation] },
{ rw [cardinal.mk_fintype, set.card_singleton], simp } },
{ rw [← cardinal.succ_zero, succ_le_iff],
simpa [lt_iff_le_and_ne, cardinal.zero_le] using
λ h, succ_ne_zero o (cof_eq_zero.1 (eq.symm h)) }
end
@[simp] theorem cof_eq_one_iff_is_succ {o} : cof.{u} o = 1 ↔ ∃ a, o = succ a :=
⟨induction_on o $ λ α r _ z, begin
resetI,
rcases cof_eq r with ⟨S, hl, e⟩, rw z at e,
cases mk_ne_zero_iff.1 (by rw e; exact one_ne_zero) with a,
refine ⟨typein r a, eq.symm $ quotient.sound
⟨rel_iso.of_surjective (rel_embedding.of_monotone _
(λ x y, _)) (λ x, _)⟩⟩,
{ apply sum.rec; [exact subtype.val, exact λ _, a] },
{ rcases x with x|⟨⟨⟨⟩⟩⟩; rcases y with y|⟨⟨⟨⟩⟩⟩;
simp [subrel, order.preimage, empty_relation],
exact x.2 },
{ suffices : r x a ∨ ∃ (b : punit), ↑a = x, {simpa},
rcases trichotomous_of r x a with h|h|h,
{ exact or.inl h },
{ exact or.inr ⟨punit.star, h.symm⟩ },
{ rcases hl x with ⟨a', aS, hn⟩,
rw (_ : ↑a = a') at h, {exact absurd h hn},
refine congr_arg subtype.val (_ : a = ⟨a', aS⟩),
haveI := le_one_iff_subsingleton.1 (le_of_eq e),
apply subsingleton.elim } }
end, λ ⟨a, e⟩, by simp [e]⟩
/-- A fundamental sequence for `a` is an increasing sequence of length `o = cof a` that converges at
`a`. We provide `o` explicitly in order to avoid type rewrites. -/
def is_fundamental_sequence (a o : ordinal.{u}) (f : Π b < o, ordinal.{u}) : Prop :=
o ≤ a.cof.ord ∧ (∀ {i j} (hi hj), i < j → f i hi < f j hj) ∧ blsub.{u u} o f = a
namespace is_fundamental_sequence
variables {a o : ordinal.{u}} {f : Π b < o, ordinal.{u}}
protected theorem cof_eq (hf : is_fundamental_sequence a o f) : a.cof.ord = o :=
hf.1.antisymm' $ by { rw ←hf.2.2, exact (ord_le_ord.2 (cof_blsub_le f)).trans (ord_card_le o) }
protected theorem strict_mono (hf : is_fundamental_sequence a o f) {i j} :
∀ hi hj, i < j → f i hi < f j hj :=
hf.2.1
theorem blsub_eq (hf : is_fundamental_sequence a o f) : blsub.{u u} o f = a :=
hf.2.2
theorem ord_cof (hf : is_fundamental_sequence a o f) :
is_fundamental_sequence a a.cof.ord (λ i hi, f i (hi.trans_le (by rw hf.cof_eq))) :=
by { have H := hf.cof_eq, subst H, exact hf }
theorem id_of_le_cof (h : o ≤ o.cof.ord) : is_fundamental_sequence o o (λ a _, a) :=
⟨h, λ _ _ _ _, id, blsub_id o⟩
protected theorem zero {f : Π b < (0 : ordinal), ordinal} :
is_fundamental_sequence 0 0 f :=
⟨by rw [cof_zero, ord_zero], λ i j hi, (ordinal.not_lt_zero i hi).elim, blsub_zero f⟩
protected theorem succ : is_fundamental_sequence (succ o) 1 (λ _ _, o) :=
begin
refine ⟨_, λ i j hi hj h, _, blsub_const ordinal.one_ne_zero o⟩,
{ rw [cof_succ, ord_one] },
{ rw lt_one_iff_zero at hi hj,
rw [hi, hj] at h,
exact h.false.elim }
end
protected theorem monotone (hf : is_fundamental_sequence a o f) {i j : ordinal} (hi : i < o)
(hj : j < o) (hij : i ≤ j) : f i hi ≤ f j hj :=
begin
rcases lt_or_eq_of_le hij with hij | rfl,
{ exact (hf.2.1 hi hj hij).le },
{ refl }
end
theorem trans {a o o' : ordinal.{u}} {f : Π b < o, ordinal.{u}}
(hf : is_fundamental_sequence a o f) {g : Π b < o', ordinal.{u}}
(hg : is_fundamental_sequence o o' g) :
is_fundamental_sequence a o' (λ i hi, f (g i hi) (by { rw ←hg.2.2, apply lt_blsub })) :=
begin
refine ⟨_, λ i j _ _ h, hf.2.1 _ _ (hg.2.1 _ _ h), _⟩,
{ rw hf.cof_eq,
exact hg.1.trans (ord_cof_le o) },
{ rw @blsub_comp.{u u u} o _ f (@is_fundamental_sequence.monotone _ _ f hf),
exact hf.2.2 }
end
end is_fundamental_sequence
/-- Every ordinal has a fundamental sequence. -/
theorem exists_fundamental_sequence (a : ordinal.{u}) :
∃ f, is_fundamental_sequence a a.cof.ord f :=
begin
rsuffices ⟨o, f, hf⟩ : ∃ o f, is_fundamental_sequence a o f,
{ exact ⟨_, hf.ord_cof⟩ },
rcases exists_lsub_cof a with ⟨ι, f, hf, hι⟩,
rcases ord_eq ι with ⟨r, wo, hr⟩,
haveI := wo,
let r' := subrel r {i | ∀ j, r j i → f j < f i},
let hrr' : r' ↪r r := subrel.rel_embedding _ _,
haveI := hrr'.is_well_order,
refine ⟨_, _, hrr'.ordinal_type_le.trans _, λ i j _ h _, (enum r' j h).prop _ _,
le_antisymm (blsub_le (λ i hi, lsub_le_iff.1 hf.le _)) _⟩,
{ rw [←hι, hr] },
{ change r (hrr'.1 _ ) (hrr'.1 _ ),
rwa [hrr'.2, @enum_lt_enum _ r'] },
{ rw [←hf, lsub_le_iff],
intro i,
rsuffices ⟨i', hi', hfg⟩ : ∃ i' hi', f i ≤ bfamily_of_family' r' (λ i, f i) i' hi',
{ exact hfg.trans_lt (lt_blsub _ _ _) },
by_cases h : ∀ j, r j i → f j < f i,
{ refine ⟨typein r' ⟨i, h⟩, typein_lt_type _ _, _⟩,
rw bfamily_of_family'_typein,
refl },
{ push_neg at h,
cases wo.wf.min_mem _ h with hji hij,
refine ⟨typein r' ⟨_, λ k hkj, lt_of_lt_of_le _ hij⟩, typein_lt_type _ _, _⟩,
{ by_contra' H,
exact (wo.wf.not_lt_min _ h ⟨is_trans.trans _ _ _ hkj hji, H⟩) hkj },
{ rwa bfamily_of_family'_typein } } }
end
@[simp] theorem cof_cof (a : ordinal.{u}) : cof (cof a).ord = cof a :=
begin
cases exists_fundamental_sequence a with f hf,
cases exists_fundamental_sequence a.cof.ord with g hg,
exact ord_injective ((hf.trans hg).cof_eq.symm)
end
protected theorem is_normal.is_fundamental_sequence {f : ordinal.{u} → ordinal.{u}}
(hf : is_normal f) {a o} (ha : is_limit a) {g} (hg : is_fundamental_sequence a o g) :
is_fundamental_sequence (f a) o (λ b hb, f (g b hb)) :=
begin
refine ⟨_, λ i j _ _ h, hf.strict_mono (hg.2.1 _ _ h), _⟩,
{ rcases exists_lsub_cof (f a) with ⟨ι, f', hf', hι⟩,
rw [←hg.cof_eq, ord_le_ord, ←hι],
suffices : lsub.{u u} (λ i, (Inf {b : ordinal | f' i ≤ f b})) = a,
{ rw ←this,
apply cof_lsub_le },
have H : ∀ i, ∃ b < a, f' i ≤ f b := λ i, begin
have := lt_lsub.{u u} f' i,
rwa [hf', ←is_normal.blsub_eq.{u u} hf ha, lt_blsub_iff] at this
end,
refine (lsub_le (λ i, _)).antisymm (le_of_forall_lt (λ b hb, _)),
{ rcases H i with ⟨b, hb, hb'⟩,
exact lt_of_le_of_lt (cInf_le' hb') hb },
{ have := hf.strict_mono hb,
rw [←hf', lt_lsub_iff] at this,
cases this with i hi,
rcases H i with ⟨b, _, hb⟩,
exact ((le_cInf_iff'' ⟨b, hb⟩).2 (λ c hc, hf.strict_mono.le_iff_le.1 (hi.trans hc))).trans_lt
(lt_lsub _ i) } },
{ rw @blsub_comp.{u u u} a _ (λ b _, f b) (λ i j hi hj h, hf.strict_mono.monotone h) g hg.2.2,
exact is_normal.blsub_eq.{u u} hf ha }
end
theorem is_normal.cof_eq {f} (hf : is_normal f) {a} (ha : is_limit a) : cof (f a) = cof a :=
let ⟨g, hg⟩ := exists_fundamental_sequence a in
ord_injective (hf.is_fundamental_sequence ha hg).cof_eq
theorem is_normal.cof_le {f} (hf : is_normal f) (a) : cof a ≤ cof (f a) :=
begin
rcases zero_or_succ_or_limit a with rfl | ⟨b, rfl⟩ | ha,
{ rw cof_zero,
exact zero_le _ },
{ rw [cof_succ, cardinal.one_le_iff_ne_zero, cof_ne_zero, ←ordinal.pos_iff_ne_zero],
exact (ordinal.zero_le (f b)).trans_lt (hf.1 b) },
{ rw hf.cof_eq ha }
end
@[simp] theorem cof_add (a b : ordinal) : b ≠ 0 → cof (a + b) = cof b :=
λ h, begin
rcases zero_or_succ_or_limit b with rfl | ⟨c, rfl⟩ | hb,
{ contradiction },
{ rw [add_succ, cof_succ, cof_succ] },
{ exact (add_is_normal a).cof_eq hb }
end
theorem aleph_0_le_cof {o} : ℵ₀ ≤ cof o ↔ is_limit o :=
begin
rcases zero_or_succ_or_limit o with rfl|⟨o,rfl⟩|l,
{ simp [not_zero_is_limit, cardinal.aleph_0_ne_zero] },
{ simp [not_succ_is_limit, cardinal.one_lt_aleph_0] },
{ simp [l], refine le_of_not_lt (λ h, _),
cases cardinal.lt_aleph_0.1 h with n e,
have := cof_cof o,
rw [e, ord_nat] at this,
cases n,
{ simp at e, simpa [e, not_zero_is_limit] using l },
{ rw [nat_cast_succ, cof_succ] at this,
rw [← this, cof_eq_one_iff_is_succ] at e,
rcases e with ⟨a, rfl⟩,
exact not_succ_is_limit _ l } }
end
@[simp] theorem aleph'_cof {o : ordinal} (ho : o.is_limit) : (aleph' o).ord.cof = o.cof :=
aleph'_is_normal.cof_eq ho
@[simp] theorem aleph_cof {o : ordinal} (ho : o.is_limit) : (aleph o).ord.cof = o.cof :=
aleph_is_normal.cof_eq ho
@[simp] theorem cof_omega : cof ω = ℵ₀ :=
(aleph_0_le_cof.2 omega_is_limit).antisymm' $ by { rw ←card_omega, apply cof_le_card }
theorem cof_eq' (r : α → α → Prop) [is_well_order α r] (h : is_limit (type r)) :
∃ S : set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = cof (type r) :=
let ⟨S, H, e⟩ := cof_eq r in
⟨S, λ a,
let a' := enum r _ (h.2 _ (typein_lt_type r a)) in
let ⟨b, h, ab⟩ := H a' in
⟨b, h, (is_order_connected.conn a b a' $ (typein_lt_typein r).1
(by { rw typein_enum, exact lt_succ (typein _ _) })).resolve_right ab⟩,
e⟩
@[simp] theorem cof_univ : cof univ.{u v} = cardinal.univ :=
le_antisymm (cof_le_card _) begin
refine le_of_forall_lt (λ c h, _),
rcases lt_univ'.1 h with ⟨c, rfl⟩,
rcases @cof_eq ordinal.{u} (<) _ with ⟨S, H, Se⟩,
rw [univ, ← lift_cof, ← cardinal.lift_lift, cardinal.lift_lt, ← Se],
refine lt_of_not_ge (λ h, _),
cases cardinal.lift_down h with a e,
refine quotient.induction_on a (λ α e, _) e,
cases quotient.exact e with f,
have f := equiv.ulift.symm.trans f,
let g := λ a, (f a).1,
let o := succ (sup.{u u} g),
rcases H o with ⟨b, h, l⟩,
refine l (lt_succ_iff.2 _),
rw ← show g (f.symm ⟨b, h⟩) = b, by dsimp [g]; simp,
apply le_sup
end
/-! ### Infinite pigeonhole principle -/
/-- If the union of s is unbounded and s is smaller than the cofinality,
then s has an unbounded member -/
theorem unbounded_of_unbounded_sUnion (r : α → α → Prop) [wo : is_well_order α r] {s : set (set α)}
(h₁ : unbounded r $ ⋃₀ s) (h₂ : #s < strict_order.cof r) : ∃ x ∈ s, unbounded r x :=
begin
by_contra' h,
simp_rw not_unbounded_iff at h,
let f : s → α := λ x : s, wo.wf.sup x (h x.1 x.2),
refine h₂.not_le (le_trans (cInf_le' ⟨range f, λ x, _, rfl⟩) mk_range_le),
rcases h₁ x with ⟨y, ⟨c, hc, hy⟩, hxy⟩,
exact ⟨f ⟨c, hc⟩, mem_range_self _, λ hxz, hxy (trans (wo.wf.lt_sup _ hy) hxz)⟩
end
/-- If the union of s is unbounded and s is smaller than the cofinality,
then s has an unbounded member -/
theorem unbounded_of_unbounded_Union {α β : Type u} (r : α → α → Prop) [wo : is_well_order α r]
(s : β → set α)
(h₁ : unbounded r $ ⋃ x, s x) (h₂ : #β < strict_order.cof r) : ∃ x : β, unbounded r (s x) :=
begin
rw ←sUnion_range at h₁,
rcases unbounded_of_unbounded_sUnion r h₁ (mk_range_le.trans_lt h₂) with ⟨_, ⟨x, rfl⟩, u⟩,
exact ⟨x, u⟩
end
/-- The infinite pigeonhole principle -/
theorem infinite_pigeonhole {β α : Type u} (f : β → α) (h₁ : ℵ₀ ≤ #β)
(h₂ : #α < (#β).ord.cof) : ∃ a : α, #(f ⁻¹' {a}) = #β :=
begin
have : ∃ a, #β ≤ #(f ⁻¹' {a}),
{ by_contra' h,
apply mk_univ.not_lt,
rw [←preimage_univ, ←Union_of_singleton, preimage_Union],
exact mk_Union_le_sum_mk.trans_lt ((sum_le_supr _).trans_lt $ mul_lt_of_lt h₁
(h₂.trans_le $ cof_ord_le _) (supr_lt h₂ h)) },
cases this with x h,
refine ⟨x, h.antisymm' _⟩,
rw le_mk_iff_exists_set,
exact ⟨_, rfl⟩
end
/-- Pigeonhole principle for a cardinality below the cardinality of the domain -/
theorem infinite_pigeonhole_card {β α : Type u} (f : β → α) (θ : cardinal) (hθ : θ ≤ #β)
(h₁ : ℵ₀ ≤ θ) (h₂ : #α < θ.ord.cof) : ∃ a : α, θ ≤ #(f ⁻¹' {a}) :=
begin
rcases le_mk_iff_exists_set.1 hθ with ⟨s, rfl⟩,
cases infinite_pigeonhole (f ∘ subtype.val : s → α) h₁ h₂ with a ha,
use a, rw [←ha, @preimage_comp _ _ _ subtype.val f],
exact mk_preimage_of_injective _ _ subtype.val_injective
end
theorem infinite_pigeonhole_set {β α : Type u} {s : set β} (f : s → α) (θ : cardinal)
(hθ : θ ≤ #s) (h₁ : ℵ₀ ≤ θ) (h₂ : #α < θ.ord.cof) :
∃ (a : α) (t : set β) (h : t ⊆ s), θ ≤ #t ∧ ∀ {{x}} (hx : x ∈ t), f ⟨x, h hx⟩ = a :=
begin
cases infinite_pigeonhole_card f θ hθ h₁ h₂ with a ha,
refine ⟨a, {x | ∃ h, f ⟨x, h⟩ = a}, _, _, _⟩,
{ rintro x ⟨hx, hx'⟩, exact hx },
{ refine ha.trans (ge_of_eq $ quotient.sound ⟨equiv.trans _
(equiv.subtype_subtype_equiv_subtype_exists _ _).symm⟩),
simp only [coe_eq_subtype, mem_singleton_iff, mem_preimage, mem_set_of_eq] },
rintro x ⟨hx, hx'⟩, exact hx'
end
end ordinal
/-! ### Regular and inaccessible cardinals -/
namespace cardinal
open ordinal
local infixr (name := cardinal.pow) ^ := @pow cardinal.{u} cardinal cardinal.has_pow
/-- A cardinal is a limit if it is not zero or a successor
cardinal. Note that `ℵ₀` is a limit cardinal by this definition. -/
def is_limit (c : cardinal) : Prop :=
c ≠ 0 ∧ ∀ x < c, succ x < c
theorem is_limit.ne_zero {c} (h : is_limit c) : c ≠ 0 :=
h.1
theorem is_limit.succ_lt {x c} (h : is_limit c) : x < c → succ x < c :=
h.2 x
theorem is_limit.aleph_0_le {c} (h : is_limit c) : ℵ₀ ≤ c :=
begin
by_contra' h',
rcases lt_aleph_0.1 h' with ⟨_ | n, rfl⟩,
{ exact h.1.irrefl },
{ simpa using h.2 n }
end
/-- A cardinal is a strong limit if it is not zero and it is
closed under powersets. Note that `ℵ₀` is a strong limit by this definition. -/
def is_strong_limit (c : cardinal) : Prop :=
c ≠ 0 ∧ ∀ x < c, 2 ^ x < c
theorem is_strong_limit.ne_zero {c} (h : is_strong_limit c) : c ≠ 0 :=
h.1
theorem is_strong_limit.two_power_lt {x c} (h : is_strong_limit c) : x < c → 2 ^ x < c :=
h.2 x
theorem is_strong_limit_aleph_0 : is_strong_limit ℵ₀ :=
⟨aleph_0_ne_zero, λ x hx, begin
rcases lt_aleph_0.1 hx with ⟨n, rfl⟩,
exact_mod_cast nat_lt_aleph_0 (pow 2 n)
end⟩
theorem is_strong_limit.is_limit {c} (H : is_strong_limit c) : is_limit c :=
⟨H.1, λ x h, (succ_le_of_lt $ cantor x).trans_lt (H.2 _ h)⟩
theorem is_limit_aleph_0 : is_limit ℵ₀ :=
is_strong_limit_aleph_0.is_limit
theorem is_strong_limit_beth {o : ordinal} (H : ∀ a < o, succ a < o) : is_strong_limit (beth o) :=
begin
rcases eq_or_ne o 0 with rfl | h,
{ rw beth_zero,
exact is_strong_limit_aleph_0 },
{ refine ⟨beth_ne_zero o, λ a ha, _⟩,
rw beth_limit ⟨h, H⟩ at ha,
rcases exists_lt_of_lt_csupr' ha with ⟨⟨i, hi⟩, ha⟩,
have := power_le_power_left two_ne_zero ha.le,
rw ←beth_succ at this,
exact this.trans_lt (beth_lt.2 (H i hi)) }
end
theorem mk_bounded_subset {α : Type*} (h : ∀ x < #α, 2 ^ x < #α) {r : α → α → Prop}
[is_well_order α r] (hr : (#α).ord = type r) : #{s : set α // bounded r s} = #α :=
begin
rcases eq_or_ne (#α) 0 with ha | ha,
{ rw ha,
haveI := mk_eq_zero_iff.1 ha,
rw mk_eq_zero_iff,
split,
rintro ⟨s, hs⟩,
exact (not_unbounded_iff s).2 hs (unbounded_of_is_empty s) },
have h' : is_strong_limit (#α) := ⟨ha, h⟩,
have ha := h'.is_limit.aleph_0_le,
apply le_antisymm,
{ have : {s : set α | bounded r s} = ⋃ i, 𝒫 {j | r j i} := set_of_exists _,
rw [←coe_set_of, this],
convert mk_Union_le_sum_mk.trans ((sum_le_supr _).trans (mul_le_max_of_aleph_0_le_left ha)),
apply (max_eq_left _).symm, apply csupr_le' (λ i, _),
rw mk_powerset,
apply (h'.two_power_lt _).le,
rw [coe_set_of, card_typein, ←lt_ord, hr],
apply typein_lt_type },
{ refine @mk_le_of_injective α _ (λ x, subtype.mk {x} _) _,
{ apply bounded_singleton,
rw ←hr,
apply ord_is_limit ha },
{ intros a b hab,
simpa only [singleton_eq_singleton_iff] using hab } }
end
theorem mk_subset_mk_lt_cof {α : Type*} (h : ∀ x < #α, 2 ^ x < #α) :
#{s : set α // #s < cof (#α).ord} = #α :=
begin
rcases eq_or_ne (#α) 0 with ha | ha,
{ rw ha,
simp [λ s, (cardinal.zero_le s).not_lt] },
have h' : is_strong_limit (#α) := ⟨ha, h⟩,
rcases ord_eq α with ⟨r, wo, hr⟩,
haveI := wo,
apply le_antisymm,
{ nth_rewrite_rhs 0 ←mk_bounded_subset h hr,
apply mk_le_mk_of_subset (λ s hs, _),
rw hr at hs,
exact lt_cof_type hs },
{ refine @mk_le_of_injective α _ (λ x, subtype.mk {x} _) _,
{ rw mk_singleton,
exact one_lt_aleph_0.trans_le (aleph_0_le_cof.2 (ord_is_limit h'.is_limit.aleph_0_le)) },
{ intros a b hab,
simpa only [singleton_eq_singleton_iff] using hab } }
end
/-- A cardinal is regular if it is infinite and it equals its own cofinality. -/
def is_regular (c : cardinal) : Prop :=
ℵ₀ ≤ c ∧ c ≤ c.ord.cof
lemma is_regular.aleph_0_le {c : cardinal} (H : c.is_regular) : ℵ₀ ≤ c :=
H.1
lemma is_regular.cof_eq {c : cardinal} (H : c.is_regular) : c.ord.cof = c :=
(cof_ord_le c).antisymm H.2
lemma is_regular.pos {c : cardinal} (H : c.is_regular) : 0 < c :=
aleph_0_pos.trans_le H.1
lemma is_regular.ord_pos {c : cardinal} (H : c.is_regular) : 0 < c.ord :=
by { rw cardinal.lt_ord, exact H.pos }
theorem is_regular_cof {o : ordinal} (h : o.is_limit) : is_regular o.cof :=
⟨aleph_0_le_cof.2 h, (cof_cof o).ge⟩
theorem is_regular_aleph_0 : is_regular ℵ₀ :=
⟨le_rfl, by simp⟩
theorem is_regular_succ {c : cardinal.{u}} (h : ℵ₀ ≤ c) : is_regular (succ c) :=
⟨h.trans (le_succ c), succ_le_of_lt begin
cases quotient.exists_rep (@succ cardinal _ _ c) with α αe, simp at αe,
rcases ord_eq α with ⟨r, wo, re⟩, resetI,
have := ord_is_limit (h.trans (le_succ _)),
rw [← αe, re] at this ⊢,
rcases cof_eq' r this with ⟨S, H, Se⟩,
rw [← Se],
apply lt_imp_lt_of_le_imp_le (λ h, mul_le_mul_right' h c),
rw [mul_eq_self h, ← succ_le_iff, ← αe, ← sum_const'],
refine le_trans _ (sum_le_sum (λ x, card (typein r x)) _ (λ i, _)),
{ simp only [← card_typein, ← mk_sigma],
exact ⟨embedding.of_surjective (λ x, x.2.1)
(λ a, let ⟨b, h, ab⟩ := H a in ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩)⟩ },
{ rw [← lt_succ_iff, ← lt_ord, ← αe, re],
apply typein_lt_type }
end⟩
theorem is_regular_aleph_one : is_regular (aleph 1) :=
by { rw ←succ_aleph_0, exact is_regular_succ le_rfl }
theorem is_regular_aleph'_succ {o : ordinal} (h : ω ≤ o) : is_regular (aleph' (succ o)) :=
by { rw aleph'_succ, exact is_regular_succ (aleph_0_le_aleph'.2 h) }
theorem is_regular_aleph_succ (o : ordinal) : is_regular (aleph (succ o)) :=
by { rw aleph_succ, exact is_regular_succ (aleph_0_le_aleph o) }
/--
A function whose codomain's cardinality is infinite but strictly smaller than its domain's
has a fiber with cardinality strictly great than the codomain.
-/
theorem infinite_pigeonhole_card_lt {β α : Type u} (f : β → α)
(w : #α < #β) (w' : ℵ₀ ≤ #α) :
∃ a : α, #α < #(f ⁻¹' {a}) :=
begin
simp_rw [← succ_le_iff],
exact ordinal.infinite_pigeonhole_card f (succ (#α)) (succ_le_of_lt w)
(w'.trans (lt_succ _).le)
((lt_succ _).trans_le (is_regular_succ w').2.ge),
end
/--
A function whose codomain's cardinality is infinite but strictly smaller than its domain's
has an infinite fiber.
-/
theorem exists_infinite_fiber {β α : Type*} (f : β → α)
(w : #α < #β) (w' : _root_.infinite α) :
∃ a : α, _root_.infinite (f ⁻¹' {a}) :=
begin
simp_rw [cardinal.infinite_iff] at ⊢ w',
cases infinite_pigeonhole_card_lt f w w' with a ha,
exact ⟨a, w'.trans ha.le⟩,
end
/--
If an infinite type `β` can be expressed as a union of finite sets,
then the cardinality of the collection of those finite sets
must be at least the cardinality of `β`.
-/
lemma le_range_of_union_finset_eq_top
{α β : Type*} [infinite β] (f : α → finset β) (w : (⋃ a, (f a : set β)) = ⊤) :
#β ≤ #(range f) :=
begin
have k : _root_.infinite (range f),
{ rw infinite_coe_iff,
apply mt (union_finset_finite_of_range_finite f),
rw w,
exact infinite_univ, },
by_contradiction h,
simp only [not_le] at h,
let u : Π b, ∃ a, b ∈ f a := λ b, by simpa using (w.ge : _) (set.mem_univ b),
let u' : β → range f := λ b, ⟨f (u b).some, by simp⟩,
have v' : ∀ a, u' ⁻¹' {⟨f a, by simp⟩} ≤ f a, begin rintros a p m,
simp at m,
rw ←m,
apply (λ b, (u b).some_spec),
end,
obtain ⟨⟨-, ⟨a, rfl⟩⟩, p⟩ := exists_infinite_fiber u' h k,
exact (@infinite.of_injective _ _ p (inclusion (v' a)) (inclusion_injective _)).false,
end
theorem lsub_lt_ord_lift_of_is_regular {ι} {f : ι → ordinal} {c} (hc : is_regular c)
(hι : cardinal.lift (#ι) < c) : (∀ i, f i < c.ord) → ordinal.lsub f < c.ord :=
lsub_lt_ord_lift (by rwa hc.cof_eq)
theorem lsub_lt_ord_of_is_regular {ι} {f : ι → ordinal} {c} (hc : is_regular c) (hι : #ι < c) :
(∀ i, f i < c.ord) → ordinal.lsub f < c.ord :=
lsub_lt_ord (by rwa hc.cof_eq)
theorem sup_lt_ord_lift_of_is_regular {ι} {f : ι → ordinal} {c} (hc : is_regular c)
(hι : cardinal.lift (#ι) < c) : (∀ i, f i < c.ord) → ordinal.sup f < c.ord :=
sup_lt_ord_lift (by rwa hc.cof_eq)
theorem sup_lt_ord_of_is_regular {ι} {f : ι → ordinal} {c} (hc : is_regular c) (hι : #ι < c) :
(∀ i, f i < c.ord) → ordinal.sup f < c.ord :=
sup_lt_ord (by rwa hc.cof_eq)
theorem blsub_lt_ord_lift_of_is_regular {o : ordinal} {f : Π a < o, ordinal} {c} (hc : is_regular c)
(ho : cardinal.lift o.card < c) : (∀ i hi, f i hi < c.ord) → ordinal.blsub o f < c.ord :=
blsub_lt_ord_lift (by rwa hc.cof_eq)
theorem blsub_lt_ord_of_is_regular {o : ordinal} {f : Π a < o, ordinal} {c} (hc : is_regular c)
(ho : o.card < c) : (∀ i hi, f i hi < c.ord) → ordinal.blsub o f < c.ord :=
blsub_lt_ord (by rwa hc.cof_eq)
theorem bsup_lt_ord_lift_of_is_regular {o : ordinal} {f : Π a < o, ordinal} {c} (hc : is_regular c)
(hι : cardinal.lift o.card < c) : (∀ i hi, f i hi < c.ord) → ordinal.bsup o f < c.ord :=
bsup_lt_ord_lift (by rwa hc.cof_eq)
theorem bsup_lt_ord_of_is_regular {o : ordinal} {f : Π a < o, ordinal} {c} (hc : is_regular c)
(hι : o.card < c) : (∀ i hi, f i hi < c.ord) → ordinal.bsup o f < c.ord :=
bsup_lt_ord (by rwa hc.cof_eq)
theorem supr_lt_lift_of_is_regular {ι} {f : ι → cardinal} {c} (hc : is_regular c)
(hι : cardinal.lift (#ι) < c) : (∀ i, f i < c) → supr f < c :=
supr_lt_lift (by rwa hc.cof_eq)
theorem supr_lt_of_is_regular {ι} {f : ι → cardinal} {c} (hc : is_regular c) (hι : #ι < c) :
(∀ i, f i < c) → supr f < c :=
supr_lt (by rwa hc.cof_eq)
theorem sum_lt_lift_of_is_regular {ι : Type u} {f : ι → cardinal} {c : cardinal} (hc : is_regular c)
(hι : cardinal.lift.{v u} (#ι) < c) (hf : ∀ i, f i < c) : sum f < c :=
(sum_le_supr_lift _).trans_lt $ mul_lt_of_lt hc.1 hι (supr_lt_lift_of_is_regular hc hι hf)
theorem sum_lt_of_is_regular {ι : Type u} {f : ι → cardinal} {c : cardinal} (hc : is_regular c)
(hι : #ι < c) : (∀ i, f i < c) → sum f < c :=
sum_lt_lift_of_is_regular.{u u} hc (by rwa lift_id)
theorem nfp_family_lt_ord_lift_of_is_regular {ι} {f : ι → ordinal → ordinal} {c} (hc : is_regular c)
(hι : (#ι).lift < c) (hc' : c ≠ ℵ₀) (hf : ∀ i (b < c.ord), f i b < c.ord) {a} (ha : a < c.ord) :
nfp_family.{u v} f a < c.ord :=
by { apply nfp_family_lt_ord_lift _ _ hf ha; rwa hc.cof_eq, exact lt_of_le_of_ne hc.1 hc'.symm }
theorem nfp_family_lt_ord_of_is_regular {ι} {f : ι → ordinal → ordinal} {c} (hc : is_regular c)
(hι : #ι < c) (hc' : c ≠ ℵ₀) {a} (hf : ∀ i (b < c.ord), f i b < c.ord) :
a < c.ord → nfp_family.{u u} f a < c.ord :=
nfp_family_lt_ord_lift_of_is_regular hc (by rwa lift_id) hc' hf
theorem nfp_bfamily_lt_ord_lift_of_is_regular {o : ordinal} {f : Π a < o, ordinal → ordinal} {c}
(hc : is_regular c) (ho : o.card.lift < c) (hc' : c ≠ ℵ₀)
(hf : ∀ i hi (b < c.ord), f i hi b < c.ord) {a} : a < c.ord → nfp_bfamily.{u v} o f a < c.ord :=
nfp_family_lt_ord_lift_of_is_regular hc (by rwa mk_ordinal_out) hc' (λ i, hf _ _)
theorem nfp_bfamily_lt_ord_of_is_regular {o : ordinal} {f : Π a < o, ordinal → ordinal} {c}
(hc : is_regular c) (ho : o.card < c) (hc' : c ≠ ℵ₀) (hf : ∀ i hi (b < c.ord), f i hi b < c.ord)
{a} : a < c.ord → nfp_bfamily.{u u} o f a < c.ord :=
nfp_bfamily_lt_ord_lift_of_is_regular hc (by rwa lift_id) hc' hf
theorem nfp_lt_ord_of_is_regular {f : ordinal → ordinal} {c} (hc : is_regular c) (hc' : c ≠ ℵ₀)
(hf : ∀ i < c.ord, f i < c.ord) {a} : (a < c.ord) → nfp f a < c.ord :=
nfp_lt_ord (by { rw hc.cof_eq, exact lt_of_le_of_ne hc.1 hc'.symm }) hf
theorem deriv_family_lt_ord_lift {ι} {f : ι → ordinal → ordinal} {c} (hc : is_regular c)
(hι : (#ι).lift < c) (hc' : c ≠ ℵ₀) (hf : ∀ i (b < c.ord), f i b < c.ord) {a} :
a < c.ord → deriv_family.{u v} f a < c.ord :=
begin
have hω : ℵ₀ < c.ord.cof,
{ rw hc.cof_eq, exact lt_of_le_of_ne hc.1 hc'.symm },
apply a.limit_rec_on,
{ rw deriv_family_zero,
exact nfp_family_lt_ord_lift hω (by rwa hc.cof_eq) hf },
{ intros b hb hb',
rw deriv_family_succ,
exact nfp_family_lt_ord_lift hω (by rwa hc.cof_eq) hf
((ord_is_limit hc.1).2 _ (hb ((lt_succ b).trans hb'))) },
{ intros b hb H hb',
rw deriv_family_limit f hb,
exact bsup_lt_ord_of_is_regular hc (ord_lt_ord.1 ((ord_card_le b).trans_lt hb'))
(λ o' ho', H o' ho' (ho'.trans hb')) }
end
theorem deriv_family_lt_ord {ι} {f : ι → ordinal → ordinal} {c} (hc : is_regular c)
(hι : #ι < c) (hc' : c ≠ ℵ₀) (hf : ∀ i (b < c.ord), f i b < c.ord) {a} :
a < c.ord → deriv_family.{u u} f a < c.ord :=
deriv_family_lt_ord_lift hc (by rwa lift_id) hc' hf
theorem deriv_bfamily_lt_ord_lift {o : ordinal} {f : Π a < o, ordinal → ordinal} {c}
(hc : is_regular c) (hι : o.card.lift < c) (hc' : c ≠ ℵ₀)
(hf : ∀ i hi (b < c.ord), f i hi b < c.ord) {a} :
a < c.ord → deriv_bfamily.{u v} o f a < c.ord :=
deriv_family_lt_ord_lift hc (by rwa mk_ordinal_out) hc' (λ i, hf _ _)
theorem deriv_bfamily_lt_ord {o : ordinal} {f : Π a < o, ordinal → ordinal} {c} (hc : is_regular c)
(hι : o.card < c) (hc' : c ≠ ℵ₀) (hf : ∀ i hi (b < c.ord), f i hi b < c.ord)
{a} : a < c.ord → deriv_bfamily.{u u} o f a < c.ord :=
deriv_bfamily_lt_ord_lift hc (by rwa lift_id) hc' hf
theorem deriv_lt_ord {f : ordinal.{u} → ordinal} {c} (hc : is_regular c) (hc' : c ≠ ℵ₀)
(hf : ∀ i < c.ord, f i < c.ord) {a} : a < c.ord → deriv f a < c.ord :=
deriv_family_lt_ord_lift hc
(by simpa using cardinal.one_lt_aleph_0.trans (lt_of_le_of_ne hc.1 hc'.symm))
hc' (λ _, hf)
/-- A cardinal is inaccessible if it is an uncountable regular strong limit cardinal. -/
def is_inaccessible (c : cardinal) :=
ℵ₀ < c ∧ is_regular c ∧ is_strong_limit c
theorem is_inaccessible.mk {c} (h₁ : ℵ₀ < c) (h₂ : c ≤ c.ord.cof) (h₃ : ∀ x < c, 2 ^ x < c) :
is_inaccessible c :=
⟨h₁, ⟨h₁.le, h₂⟩, (aleph_0_pos.trans h₁).ne', h₃⟩
/- Lean's foundations prove the existence of ℵ₀ many inaccessible cardinals -/
theorem univ_inaccessible : is_inaccessible (univ.{u v}) :=
is_inaccessible.mk
(by simpa using lift_lt_univ' ℵ₀)
(by simp)
(λ c h, begin
rcases lt_univ'.1 h with ⟨c, rfl⟩,
rw ← lift_two_power.{u (max (u+1) v)},
apply lift_lt_univ'
end)
theorem lt_power_cof {c : cardinal.{u}} : ℵ₀ ≤ c → c < c ^ cof c.ord :=
quotient.induction_on c $ λ α h, begin
rcases ord_eq α with ⟨r, wo, re⟩, resetI,
have := ord_is_limit h,
rw [mk_def, re] at this ⊢,
rcases cof_eq' r this with ⟨S, H, Se⟩,
have := sum_lt_prod (λ a:S, #{x // r x a}) (λ _, #α) (λ i, _),
{ simp only [cardinal.prod_const, cardinal.lift_id, ← Se, ← mk_sigma, power_def] at this ⊢,
refine lt_of_le_of_lt _ this,
refine ⟨embedding.of_surjective _ _⟩,
{ exact λ x, x.2.1 },
{ exact λ a, let ⟨b, h, ab⟩ := H a in ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ } },
{ have := typein_lt_type r i,
rwa [← re, lt_ord] at this }
end
theorem lt_cof_power {a b : cardinal} (ha : ℵ₀ ≤ a) (b1 : 1 < b) :
a < cof (b ^ a).ord :=
begin
have b0 : b ≠ 0 := (zero_lt_one.trans b1).ne',
apply lt_imp_lt_of_le_imp_le (power_le_power_left $ power_ne_zero a b0),
rw [←power_mul, mul_eq_self ha],
exact lt_power_cof (ha.trans $ (cantor' _ b1).le),
end
end cardinal
|
53d4c82a9273274d78e00dabb3f13c0a2ee29bef | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/ring_theory/jacobson.lean | fd5f5e0ae5ad931c279270d97ed3c417979dbedd | [
"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 | 34,437 | lean | /-
Copyright (c) 2020 Devon Tuma. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Devon Tuma
-/
import ring_theory.localization.away
import ring_theory.ideal.over
import ring_theory.jacobson_ideal
/-!
# Jacobson Rings
The following conditions are equivalent for a ring `R`:
1. Every radical ideal `I` is equal to its Jacobson radical
2. Every radical ideal `I` can be written as an intersection of maximal ideals
3. Every prime ideal `I` is equal to its Jacobson radical
Any ring satisfying any of these equivalent conditions is said to be Jacobson.
Some particular examples of Jacobson rings are also proven.
`is_jacobson_quotient` says that the quotient of a Jacobson ring is Jacobson.
`is_jacobson_localization` says the localization of a Jacobson ring to a single element is Jacobson.
`is_jacobson_polynomial_iff_is_jacobson` says polynomials over a Jacobson ring form a Jacobson ring.
## Main definitions
Let `R` be a commutative ring. Jacobson Rings are defined using the first of the above conditions
* `is_jacobson R` is the proposition that `R` is a Jacobson ring. It is a class,
implemented as the predicate that for any ideal, `I.radical = I` implies `I.jacobson = I`.
## Main statements
* `is_jacobson_iff_prime_eq` is the equivalence between conditions 1 and 3 above.
* `is_jacobson_iff_Inf_maximal` is the equivalence between conditions 1 and 2 above.
* `is_jacobson_of_surjective` says that if `R` is a Jacobson ring and `f : R →+* S` is surjective,
then `S` is also a Jacobson ring
* `is_jacobson_mv_polynomial` says that multi-variate polynomials over a Jacobson ring are Jacobson.
## Tags
Jacobson, Jacobson Ring
-/
namespace ideal
open polynomial
open_locale polynomial
section is_jacobson
variables {R S : Type*} [comm_ring R] [comm_ring S] {I : ideal R}
/-- A ring is a Jacobson ring if for every radical ideal `I`,
the Jacobson radical of `I` is equal to `I`.
See `is_jacobson_iff_prime_eq` and `is_jacobson_iff_Inf_maximal` for equivalent definitions. -/
class is_jacobson (R : Type*) [comm_ring R] : Prop :=
(out' : ∀ (I : ideal R), I.radical = I → I.jacobson = I)
theorem is_jacobson_iff {R} [comm_ring R] :
is_jacobson R ↔ ∀ (I : ideal R), I.radical = I → I.jacobson = I :=
⟨λ h, h.1, λ h, ⟨h⟩⟩
theorem is_jacobson.out {R} [comm_ring R] :
is_jacobson R → ∀ {I : ideal R}, I.radical = I → I.jacobson = I := is_jacobson_iff.1
/-- A ring is a Jacobson ring if and only if for all prime ideals `P`,
the Jacobson radical of `P` is equal to `P`. -/
lemma is_jacobson_iff_prime_eq : is_jacobson R ↔ ∀ P : ideal R, is_prime P → P.jacobson = P :=
begin
refine is_jacobson_iff.trans ⟨λ h I hI, h I (is_prime.radical hI), _⟩,
refine λ h I hI, le_antisymm (λ x hx, _) (λ x hx, mem_Inf.mpr (λ _ hJ, hJ.left hx)),
rw [← hI, radical_eq_Inf I, mem_Inf],
intros P hP,
rw set.mem_set_of_eq at hP,
erw mem_Inf at hx,
erw [← h P hP.right, mem_Inf],
exact λ J hJ, hx ⟨le_trans hP.left hJ.left, hJ.right⟩
end
/-- A ring `R` is Jacobson if and only if for every prime ideal `I`,
`I` can be written as the infimum of some collection of maximal ideals.
Allowing ⊤ in the set `M` of maximal ideals is equivalent, but makes some proofs cleaner. -/
lemma is_jacobson_iff_Inf_maximal : is_jacobson R ↔
∀ {I : ideal R}, I.is_prime → ∃ M : set (ideal R), (∀ J ∈ M, is_maximal J ∨ J = ⊤) ∧ I = Inf M :=
⟨λ H I h, eq_jacobson_iff_Inf_maximal.1 (H.out (is_prime.radical h)),
λ H, is_jacobson_iff_prime_eq.2 (λ P hP, eq_jacobson_iff_Inf_maximal.2 (H hP))⟩
lemma is_jacobson_iff_Inf_maximal' : is_jacobson R ↔
∀ {I : ideal R}, I.is_prime → ∃ M : set (ideal R),
(∀ (J ∈ M) (K : ideal R), J < K → K = ⊤) ∧ I = Inf M :=
⟨λ H I h, eq_jacobson_iff_Inf_maximal'.1 (H.out (is_prime.radical h)),
λ H, is_jacobson_iff_prime_eq.2 (λ P hP, eq_jacobson_iff_Inf_maximal'.2 (H hP))⟩
lemma radical_eq_jacobson [H : is_jacobson R] (I : ideal R) : I.radical = I.jacobson :=
le_antisymm (le_Inf (λ J ⟨hJ, hJ_max⟩, (is_prime.radical_le_iff hJ_max.is_prime).mpr hJ))
((H.out (radical_idem I)) ▸ (jacobson_mono le_radical))
/-- Fields have only two ideals, and the condition holds for both of them. -/
@[priority 100]
instance is_jacobson_field {K : Type*} [field K] : is_jacobson K :=
⟨λ I hI, or.rec_on (eq_bot_or_top I)
(λ h, le_antisymm
(Inf_le ⟨le_of_eq rfl, (eq.symm h) ▸ bot_is_maximal⟩)
((eq.symm h) ▸ bot_le))
(λ h, by rw [h, jacobson_eq_top_iff])⟩
theorem is_jacobson_of_surjective [H : is_jacobson R] :
(∃ (f : R →+* S), function.surjective f) → is_jacobson S :=
begin
rintros ⟨f, hf⟩,
rw is_jacobson_iff_Inf_maximal,
intros p hp,
use map f '' {J : ideal R | comap f p ≤ J ∧ J.is_maximal },
use λ j ⟨J, hJ, hmap⟩, hmap ▸ or.symm (map_eq_top_or_is_maximal_of_surjective f hf hJ.right),
have : p = map f ((comap f p).jacobson),
from (is_jacobson.out' (comap f p) (by rw [← comap_radical, is_prime.radical hp])).symm
▸ (map_comap_of_surjective f hf p).symm,
exact eq.trans this (map_Inf hf (λ J ⟨hJ, _⟩, le_trans (ideal.ker_le_comap f) hJ)),
end
@[priority 100]
instance is_jacobson_quotient [is_jacobson R] : is_jacobson (R ⧸ I) :=
is_jacobson_of_surjective ⟨quotient.mk I, (by rintro ⟨x⟩; use x; refl)⟩
lemma is_jacobson_iso (e : R ≃+* S) : is_jacobson R ↔ is_jacobson S :=
⟨λ h, @is_jacobson_of_surjective _ _ _ _ h ⟨(e : R →+* S), e.surjective⟩,
λ h, @is_jacobson_of_surjective _ _ _ _ h ⟨(e.symm : S →+* R), e.symm.surjective⟩⟩
lemma is_jacobson_of_is_integral [algebra R S] (hRS : algebra.is_integral R S)
(hR : is_jacobson R) : is_jacobson S :=
begin
rw is_jacobson_iff_prime_eq,
introsI P hP,
by_cases hP_top : comap (algebra_map R S) P = ⊤,
{ simp [comap_eq_top_iff.1 hP_top] },
{ haveI : nontrivial (R ⧸ comap (algebra_map R S) P) := quotient.nontrivial hP_top,
rw jacobson_eq_iff_jacobson_quotient_eq_bot,
refine eq_bot_of_comap_eq_bot (is_integral_quotient_of_is_integral hRS) _,
rw [eq_bot_iff, ← jacobson_eq_iff_jacobson_quotient_eq_bot.1 ((is_jacobson_iff_prime_eq.1 hR)
(comap (algebra_map R S) P) (comap_is_prime _ _)), comap_jacobson],
refine Inf_le_Inf (λ J hJ, _),
simp only [true_and, set.mem_image, bot_le, set.mem_set_of_eq],
haveI : J.is_maximal, { simpa using hJ },
exact exists_ideal_over_maximal_of_is_integral (is_integral_quotient_of_is_integral hRS) J
(comap_bot_le_of_injective _ algebra_map_quotient_injective) }
end
lemma is_jacobson_of_is_integral' (f : R →+* S) (hf : f.is_integral)
(hR : is_jacobson R) : is_jacobson S :=
@is_jacobson_of_is_integral _ _ _ _ f.to_algebra hf hR
end is_jacobson
section localization
open is_localization submonoid
variables {R S : Type*} [comm_ring R] [comm_ring S] {I : ideal R}
variables (y : R) [algebra R S] [is_localization.away y S]
lemma disjoint_powers_iff_not_mem (hI : I.radical = I) :
disjoint ((submonoid.powers y) : set R) ↑I ↔ y ∉ I.1 :=
begin
refine ⟨λ h, set.disjoint_left.1 h (mem_powers _), λ h, (disjoint_iff).mpr (eq_bot_iff.mpr _)⟩,
rintros x ⟨⟨n, rfl⟩, hx'⟩,
rw [← hI] at hx',
exact absurd (hI ▸ mem_radical_of_pow_mem hx' : y ∈ I.carrier) h
end
variables (S)
/-- If `R` is a Jacobson ring, then maximal ideals in the localization at `y`
correspond to maximal ideals in the original ring `R` that don't contain `y`.
This lemma gives the correspondence in the particular case of an ideal and its comap.
See `le_rel_iso_of_maximal` for the more general relation isomorphism -/
lemma is_maximal_iff_is_maximal_disjoint [H : is_jacobson R] (J : ideal S) :
J.is_maximal ↔ (comap (algebra_map R S) J).is_maximal ∧ y ∉ ideal.comap (algebra_map R S) J :=
begin
split,
{ refine λ h, ⟨_, λ hy, h.ne_top (ideal.eq_top_of_is_unit_mem _ hy
(map_units _ ⟨y, submonoid.mem_powers _⟩))⟩,
have hJ : J.is_prime := is_maximal.is_prime h,
rw is_prime_iff_is_prime_disjoint (submonoid.powers y) at hJ,
have : y ∉ (comap (algebra_map R S) J).1 :=
set.disjoint_left.1 hJ.right (submonoid.mem_powers _),
erw [← H.out (is_prime.radical hJ.left), mem_Inf] at this,
push_neg at this,
rcases this with ⟨I, hI, hI'⟩,
convert hI.right,
by_cases hJ : J = map (algebra_map R S) I,
{ rw [hJ, comap_map_of_is_prime_disjoint (powers y) S I (is_maximal.is_prime hI.right)],
rwa disjoint_powers_iff_not_mem y (is_maximal.is_prime hI.right).radical },
{ have hI_p : (map (algebra_map R S) I).is_prime,
{ refine is_prime_of_is_prime_disjoint (powers y) _ I hI.right.is_prime _,
rwa disjoint_powers_iff_not_mem y (is_maximal.is_prime hI.right).radical },
have : J ≤ map (algebra_map R S) I :=
(map_comap (submonoid.powers y) S J) ▸ (map_mono hI.left),
exact absurd (h.1.2 _ (lt_of_le_of_ne this hJ)) hI_p.1 } },
{ refine λ h, ⟨⟨λ hJ, h.1.ne_top (eq_top_iff.2 _), λ I hI, _⟩⟩,
{ rwa [eq_top_iff, ← (is_localization.order_embedding (powers y) S).le_iff_le] at hJ },
{ have := congr_arg (map (algebra_map R S)) (h.1.1.2 _ ⟨comap_mono (le_of_lt hI), _⟩),
rwa [map_comap (powers y) S I, map_top] at this,
refine λ hI', hI.right _,
rw [← map_comap (powers y) S I, ← map_comap (powers y) S J],
exact map_mono hI' } }
end
variables {S}
/-- If `R` is a Jacobson ring, then maximal ideals in the localization at `y`
correspond to maximal ideals in the original ring `R` that don't contain `y`.
This lemma gives the correspondence in the particular case of an ideal and its map.
See `le_rel_iso_of_maximal` for the more general statement, and the reverse of this implication -/
lemma is_maximal_of_is_maximal_disjoint [is_jacobson R] (I : ideal R) (hI : I.is_maximal)
(hy : y ∉ I) : (map (algebra_map R S) I).is_maximal :=
begin
rw [is_maximal_iff_is_maximal_disjoint S y,
comap_map_of_is_prime_disjoint (powers y) S I (is_maximal.is_prime hI)
((disjoint_powers_iff_not_mem y (is_maximal.is_prime hI).radical).2 hy)],
exact ⟨hI, hy⟩
end
/-- If `R` is a Jacobson ring, then maximal ideals in the localization at `y`
correspond to maximal ideals in the original ring `R` that don't contain `y` -/
def order_iso_of_maximal [is_jacobson R] :
{p : ideal S // p.is_maximal} ≃o {p : ideal R // p.is_maximal ∧ y ∉ p} :=
{ to_fun := λ p,
⟨ideal.comap (algebra_map R S) p.1, (is_maximal_iff_is_maximal_disjoint S y p.1).1 p.2⟩,
inv_fun := λ p,
⟨ideal.map (algebra_map R S) p.1, is_maximal_of_is_maximal_disjoint y p.1 p.2.1 p.2.2⟩,
left_inv := λ J, subtype.eq (map_comap (powers y) S J),
right_inv := λ I, subtype.eq (comap_map_of_is_prime_disjoint _ _ I.1 (is_maximal.is_prime I.2.1)
((disjoint_powers_iff_not_mem y I.2.1.is_prime.radical).2 I.2.2)),
map_rel_iff' := λ I I', ⟨λ h, (show I.val ≤ I'.val,
from (map_comap (powers y) S I.val) ▸ (map_comap (powers y) S I'.val) ▸ (ideal.map_mono h)),
λ h x hx, h hx⟩ }
include y
/-- If `S` is the localization of the Jacobson ring `R` at the submonoid generated by `y : R`, then
`S` is Jacobson. -/
lemma is_jacobson_localization [H : is_jacobson R] : is_jacobson S :=
begin
rw is_jacobson_iff_prime_eq,
refine λ P' hP', le_antisymm _ le_jacobson,
obtain ⟨hP', hPM⟩ := (is_localization.is_prime_iff_is_prime_disjoint (powers y) S P').mp hP',
have hP := H.out (is_prime.radical hP'),
refine (le_of_eq (is_localization.map_comap (powers y) S P'.jacobson).symm).trans
((map_mono _).trans (le_of_eq (is_localization.map_comap (powers y) S P'))),
have : Inf { I : ideal R | comap (algebra_map R S) P' ≤ I ∧ I.is_maximal ∧ y ∉ I } ≤
comap (algebra_map R S) P',
{ intros x hx,
have hxy : x * y ∈ (comap (algebra_map R S) P').jacobson,
{ rw [ideal.jacobson, mem_Inf],
intros J hJ,
by_cases y ∈ J,
{ exact J.mul_mem_left x h },
{ exact J.mul_mem_right y ((mem_Inf.1 hx) ⟨hJ.left, ⟨hJ.right, h⟩⟩) } },
rw hP at hxy,
cases hP'.mem_or_mem hxy with hxy hxy,
{ exact hxy },
{ exact (hPM ⟨submonoid.mem_powers _, hxy⟩).elim } },
refine le_trans _ this,
rw [ideal.jacobson, comap_Inf', Inf_eq_infi],
refine infi_le_infi_of_subset (λ I hI, ⟨map (algebra_map R S) I, ⟨_, _⟩⟩),
{ exact ⟨le_trans (le_of_eq ((is_localization.map_comap (powers y) S P').symm)) (map_mono hI.1),
is_maximal_of_is_maximal_disjoint y _ hI.2.1 hI.2.2⟩ },
{ exact is_localization.comap_map_of_is_prime_disjoint _ S I (is_maximal.is_prime hI.2.1)
((disjoint_powers_iff_not_mem y hI.2.1.is_prime.radical).2 hI.2.2) }
end
end localization
namespace polynomial
open polynomial
section comm_ring
variables {R S : Type*} [comm_ring R] [comm_ring S] [is_domain S]
variables {Rₘ Sₘ : Type*} [comm_ring Rₘ] [comm_ring Sₘ]
/-- If `I` is a prime ideal of `polynomial R` and `pX ∈ I` is a non-constant polynomial,
then the map `R →+* R[x]/I` descends to an integral map when localizing at `pX.leading_coeff`.
In particular `X` is integral because it satisfies `pX`, and constants are trivially integral,
so integrality of the entire extension follows by closure under addition and multiplication. -/
lemma is_integral_is_localization_polynomial_quotient
(P : ideal R[X]) (pX : R[X]) (hpX : pX ∈ P)
[algebra (R ⧸ P.comap (C : R →+* _)) Rₘ]
[is_localization.away (pX.map (quotient.mk (P.comap (C : R →+* R[X])))).leading_coeff Rₘ]
[algebra (R[X] ⧸ P) Sₘ]
[is_localization ((submonoid.powers (pX.map
(quotient.mk (P.comap (C : R →+* R[X])))).leading_coeff).map
(quotient_map P C le_rfl) : submonoid (R[X] ⧸ P)) Sₘ] :
(is_localization.map Sₘ (quotient_map P C le_rfl)
((submonoid.powers
(pX.map (quotient.mk (P.comap (C : R →+* R[X])))).leading_coeff).le_comap_map) : Rₘ →+* _)
.is_integral :=
begin
let P' : ideal R := P.comap C,
let M : submonoid (R ⧸ P') :=
submonoid.powers (pX.map (quotient.mk (P.comap (C : R →+* R[X])))).leading_coeff,
let M' : submonoid (R[X] ⧸ P) :=
(submonoid.powers (pX.map (quotient.mk (P.comap (C : R →+* R[X])))).leading_coeff).map
(quotient_map P C le_rfl),
let φ : R ⧸ P' →+* R[X] ⧸ P := quotient_map P C le_rfl,
let φ' : Rₘ →+* Sₘ := is_localization.map Sₘ φ M.le_comap_map,
have hφ' : φ.comp (quotient.mk P') = (quotient.mk P).comp C := rfl,
intro p,
obtain ⟨⟨p', ⟨q, hq⟩⟩, hp⟩ := is_localization.surj M' p,
suffices : φ'.is_integral_elem (algebra_map _ _ p'),
{ obtain ⟨q', hq', rfl⟩ := hq,
obtain ⟨q'', hq''⟩ := is_unit_iff_exists_inv'.1 (is_localization.map_units Rₘ (⟨q', hq'⟩ : M)),
refine φ'.is_integral_of_is_integral_mul_unit p (algebra_map _ _ (φ q')) q'' _ (hp.symm ▸ this),
convert trans (trans (φ'.map_mul _ _).symm (congr_arg φ' hq'')) φ'.map_one using 2,
rw [← φ'.comp_apply, is_localization.map_comp, ring_hom.comp_apply, subtype.coe_mk] },
refine is_integral_of_mem_closure''
(((algebra_map _ Sₘ).comp (quotient.mk P)) '' (insert X {p | p.degree ≤ 0})) _ _ _,
{ rintros x ⟨p, hp, rfl⟩,
refine hp.rec_on (λ hy, _) (λ hy, _),
{ refine hy.symm ▸ (φ.is_integral_elem_localization_at_leading_coeff ((quotient.mk P) X)
(pX.map (quotient.mk P')) _ M ⟨1, pow_one _⟩),
rwa [eval₂_map, hφ', ← hom_eval₂, quotient.eq_zero_iff_mem, eval₂_C_X] },
{ rw [set.mem_set_of_eq, degree_le_zero_iff] at hy,
refine hy.symm ▸ ⟨X - C (algebra_map _ _ ((quotient.mk P') (p.coeff 0))), monic_X_sub_C _, _⟩,
simp only [eval₂_sub, eval₂_C, eval₂_X],
rw [sub_eq_zero, ← φ'.comp_apply, is_localization.map_comp],
refl } },
{ obtain ⟨p, rfl⟩ := quotient.mk_surjective p',
refine polynomial.induction_on p
(λ r, subring.subset_closure $ set.mem_image_of_mem _ (or.inr degree_C_le))
(λ _ _ h1 h2, _) (λ n _ hr, _),
{ convert subring.add_mem _ h1 h2,
rw [ring_hom.map_add, ring_hom.map_add] },
{ rw [pow_succ X n, mul_comm X, ← mul_assoc, ring_hom.map_mul, ring_hom.map_mul],
exact subring.mul_mem _ hr (subring.subset_closure (set.mem_image_of_mem _ (or.inl rfl))) } },
end
/-- If `f : R → S` descends to an integral map in the localization at `x`,
and `R` is a Jacobson ring, then the intersection of all maximal ideals in `S` is trivial -/
lemma jacobson_bot_of_integral_localization
{R : Type*} [comm_ring R] [is_domain R] [is_jacobson R]
(Rₘ Sₘ : Type*) [comm_ring Rₘ] [comm_ring Sₘ]
(φ : R →+* S) (hφ : function.injective φ) (x : R) (hx : x ≠ 0)
[algebra R Rₘ] [is_localization.away x Rₘ]
[algebra S Sₘ] [is_localization ((submonoid.powers x).map φ : submonoid S) Sₘ]
(hφ' : ring_hom.is_integral
(is_localization.map Sₘ φ (submonoid.powers x).le_comap_map : Rₘ →+* Sₘ)) :
(⊥ : ideal S).jacobson = (⊥ : ideal S) :=
begin
have hM : ((submonoid.powers x).map φ : submonoid S) ≤ non_zero_divisors S :=
map_le_non_zero_divisors_of_injective φ hφ (powers_le_non_zero_divisors_of_no_zero_divisors hx),
letI : is_domain Sₘ := is_localization.is_domain_of_le_non_zero_divisors _ hM,
let φ' : Rₘ →+* Sₘ := is_localization.map _ φ (submonoid.powers x).le_comap_map,
suffices : ∀ I : ideal Sₘ, I.is_maximal → (I.comap (algebra_map S Sₘ)).is_maximal,
{ have hϕ' : comap (algebra_map S Sₘ) (⊥ : ideal Sₘ) = (⊥ : ideal S),
{ rw [← ring_hom.ker_eq_comap_bot, ← ring_hom.injective_iff_ker_eq_bot],
exact is_localization.injective Sₘ hM },
have hSₘ : is_jacobson Sₘ := is_jacobson_of_is_integral' φ' hφ' (is_jacobson_localization x),
refine eq_bot_iff.mpr (le_trans _ (le_of_eq hϕ')),
rw [← hSₘ.out radical_bot_of_is_domain, comap_jacobson],
exact Inf_le_Inf (λ j hj, ⟨bot_le, let ⟨J, hJ⟩ := hj in hJ.2 ▸ this J hJ.1.2⟩) },
introsI I hI,
-- Remainder of the proof is pulling and pushing ideals around the square and the quotient square
haveI : (I.comap (algebra_map S Sₘ)).is_prime := comap_is_prime _ I,
haveI : (I.comap φ').is_prime := comap_is_prime φ' I,
haveI : (⊥ : ideal (S ⧸ I.comap (algebra_map S Sₘ))).is_prime := bot_prime,
have hcomm: φ'.comp (algebra_map R Rₘ) = (algebra_map S Sₘ).comp φ := is_localization.map_comp _,
let f := quotient_map (I.comap (algebra_map S Sₘ)) φ le_rfl,
let g := quotient_map I (algebra_map S Sₘ) le_rfl,
have := is_maximal_comap_of_is_integral_of_is_maximal' φ' hφ' I hI,
have := ((is_maximal_iff_is_maximal_disjoint Rₘ x _).1 this).left,
have : ((I.comap (algebra_map S Sₘ)).comap φ).is_maximal,
{ rwa [comap_comap, hcomm, ← comap_comap] at this },
rw ← bot_quotient_is_maximal_iff at this ⊢,
refine is_maximal_of_is_integral_of_is_maximal_comap' f _ ⊥
((eq_bot_iff.2 (comap_bot_le_of_injective f quotient_map_injective)).symm ▸ this),
exact f.is_integral_tower_bot_of_is_integral g quotient_map_injective
((comp_quotient_map_eq_of_comp_eq hcomm I).symm ▸
(ring_hom.is_integral_trans _ _ (ring_hom.is_integral_of_surjective _
(is_localization.surjective_quotient_map_of_maximal_of_localization (submonoid.powers x) Rₘ
(by rwa [comap_comap, hcomm, ← bot_quotient_is_maximal_iff])))
(ring_hom.is_integral_quotient_of_is_integral _ hφ'))),
end
/-- Used to bootstrap the proof of `is_jacobson_polynomial_iff_is_jacobson`.
That theorem is more general and should be used instead of this one. -/
private lemma is_jacobson_polynomial_of_domain
(R : Type*) [comm_ring R] [is_domain R] [hR : is_jacobson R]
(P : ideal R[X]) [is_prime P] (hP : ∀ (x : R), C x ∈ P → x = 0) :
P.jacobson = P :=
begin
by_cases Pb : P = ⊥,
{ exact Pb.symm ▸ jacobson_bot_polynomial_of_jacobson_bot
(hR.out radical_bot_of_is_domain) },
{ rw jacobson_eq_iff_jacobson_quotient_eq_bot,
haveI : (P.comap (C : R →+* R[X])).is_prime := comap_is_prime C P,
obtain ⟨p, pP, p0⟩ := exists_nonzero_mem_of_ne_bot Pb hP,
let x := (polynomial.map (quotient.mk (comap (C : R →+* _) P)) p).leading_coeff,
have hx : x ≠ 0 := by rwa [ne.def, leading_coeff_eq_zero],
refine jacobson_bot_of_integral_localization
(localization.away x)
(localization ((submonoid.powers x).map (P.quotient_map C le_rfl) :
submonoid (R[X] ⧸ P)))
(quotient_map P C le_rfl) quotient_map_injective
x hx
_,
-- `convert` is noticeably faster than `exact` here:
convert is_integral_is_localization_polynomial_quotient P p pP }
end
lemma is_jacobson_polynomial_of_is_jacobson (hR : is_jacobson R) :
is_jacobson R[X] :=
begin
refine is_jacobson_iff_prime_eq.mpr (λ I, _),
introI hI,
let R' : subring (R[X] ⧸ I) := ((quotient.mk I).comp C).range,
let i : R →+* R' := ((quotient.mk I).comp C).range_restrict,
have hi : function.surjective (i : R → R') := ((quotient.mk I).comp C).range_restrict_surjective,
have hi' : (polynomial.map_ring_hom i : R[X] →+* R'[X]).ker ≤ I,
{ refine λ f hf, polynomial_mem_ideal_of_coeff_mem_ideal I f (λ n, _),
replace hf := congr_arg (λ (g : polynomial (((quotient.mk I).comp C).range)), g.coeff n) hf,
change (polynomial.map ((quotient.mk I).comp C).range_restrict f).coeff n = 0 at hf,
rw [coeff_map, subtype.ext_iff] at hf,
rwa [mem_comap, ← quotient.eq_zero_iff_mem, ← ring_hom.comp_apply], },
haveI := map_is_prime_of_surjective
(show function.surjective (map_ring_hom i), from map_surjective i hi) hi',
suffices : (I.map (polynomial.map_ring_hom i)).jacobson = (I.map (polynomial.map_ring_hom i)),
{ replace this := congr_arg (comap (polynomial.map_ring_hom i)) this,
rw [← map_jacobson_of_surjective _ hi',
comap_map_of_surjective _ _, comap_map_of_surjective _ _] at this,
refine le_antisymm (le_trans (le_sup_of_le_left le_rfl)
(le_trans (le_of_eq this) (sup_le le_rfl hi'))) le_jacobson,
all_goals {exact polynomial.map_surjective i hi} },
exact @is_jacobson_polynomial_of_domain R' _ _ (is_jacobson_of_surjective ⟨i, hi⟩)
(map (map_ring_hom i) I) _ (eq_zero_of_polynomial_mem_map_range I),
end
theorem is_jacobson_polynomial_iff_is_jacobson :
is_jacobson R[X] ↔ is_jacobson R :=
begin
refine ⟨_, is_jacobson_polynomial_of_is_jacobson⟩,
introI H,
exact is_jacobson_of_surjective ⟨eval₂_ring_hom (ring_hom.id _) 1, λ x,
⟨C x, by simp only [coe_eval₂_ring_hom, ring_hom.id_apply, eval₂_C]⟩⟩,
end
instance [is_jacobson R] : is_jacobson R[X] :=
is_jacobson_polynomial_iff_is_jacobson.mpr ‹is_jacobson R›
end comm_ring
section
variables {R : Type*} [comm_ring R] [is_jacobson R]
variables (P : ideal R[X]) [hP : P.is_maximal]
include P hP
lemma is_maximal_comap_C_of_is_maximal [nontrivial R] (hP' : ∀ (x : R), C x ∈ P → x = 0) :
is_maximal (comap (C : R →+* R[X]) P : ideal R) :=
begin
haveI hp'_prime : (P.comap (C : R →+* R[X]) : ideal R).is_prime := comap_is_prime C P,
obtain ⟨m, hm⟩ := submodule.nonzero_mem_of_bot_lt (bot_lt_of_maximal P polynomial_not_is_field),
have : (m : R[X]) ≠ 0, rwa [ne.def, submodule.coe_eq_zero],
let φ : R ⧸ P.comap (C : R →+* R[X]) →+* R[X] ⧸ P := quotient_map P (C : R →+* R[X]) le_rfl,
let M : submonoid (R ⧸ P.comap C) :=
submonoid.powers ((m : R[X]).map
(quotient.mk (P.comap (C : R →+* R[X]) : ideal R))).leading_coeff,
rw ← bot_quotient_is_maximal_iff,
have hp0 : ((m : R[X]).map
(quotient.mk (P.comap (C : R →+* R[X]) : ideal R))).leading_coeff ≠ 0 :=
λ hp0', this $ map_injective (quotient.mk (P.comap (C : R →+* R[X]) : ideal R))
((injective_iff_map_eq_zero (quotient.mk (P.comap (C : R →+* R[X]) : ideal R))).2 (λ x hx,
by rwa [quotient.eq_zero_iff_mem, (by rwa eq_bot_iff : (P.comap C : ideal R) = ⊥)] at hx))
(by simpa only [leading_coeff_eq_zero, polynomial.map_zero] using hp0'),
have hM : (0 : R ⧸ P.comap C) ∉ M := λ ⟨n, hn⟩, hp0 (pow_eq_zero hn),
suffices : (⊥ : ideal (localization M)).is_maximal,
{ rw ← is_localization.comap_map_of_is_prime_disjoint M (localization M) ⊥ bot_prime
(λ x hx, hM (hx.2 ▸ hx.1)),
refine ((is_maximal_iff_is_maximal_disjoint (localization M) _ _).mp (by rwa map_bot)).1,
swap, exact localization.is_localization },
let M' : submonoid (R[X] ⧸ P) := M.map φ,
have hM' : (0 : R[X] ⧸ P) ∉ M' :=
λ ⟨z, hz⟩, hM (quotient_map_injective (trans hz.2 φ.map_zero.symm) ▸ hz.1),
haveI : is_domain (localization M') :=
is_localization.is_domain_localization (le_non_zero_divisors_of_no_zero_divisors hM'),
suffices : (⊥ : ideal (localization M')).is_maximal,
{ rw le_antisymm bot_le (comap_bot_le_of_injective _ (is_localization.map_injective_of_injective
M (localization M) (localization M') quotient_map_injective )),
refine is_maximal_comap_of_is_integral_of_is_maximal' _ _ ⊥ this,
apply is_integral_is_localization_polynomial_quotient P _ (submodule.coe_mem m) },
rw (map_bot.symm : (⊥ : ideal (localization M')) =
map (algebra_map (R[X] ⧸ P) (localization M')) ⊥),
let bot_maximal := ((bot_quotient_is_maximal_iff _).mpr hP),
refine map.is_maximal (algebra_map _ _) (is_field.localization_map_bijective hM' _) bot_maximal,
rwa [← quotient.maximal_ideal_iff_is_field_quotient, ← bot_quotient_is_maximal_iff],
end
/-- Used to bootstrap the more general `quotient_mk_comp_C_is_integral_of_jacobson` -/
private lemma quotient_mk_comp_C_is_integral_of_jacobson' [nontrivial R] (hR : is_jacobson R)
(hP' : ∀ (x : R), C x ∈ P → x = 0) :
((quotient.mk P).comp C : R →+* R[X] ⧸ P).is_integral :=
begin
refine (is_integral_quotient_map_iff _).mp _,
let P' : ideal R := P.comap C,
obtain ⟨pX, hpX, hp0⟩ :=
exists_nonzero_mem_of_ne_bot (ne_of_lt (bot_lt_of_maximal P polynomial_not_is_field)).symm hP',
let M : submonoid (R ⧸ P') := submonoid.powers (pX.map (quotient.mk P')).leading_coeff,
let φ : R ⧸ P' →+* R[X] ⧸ P := quotient_map P C le_rfl,
haveI hp'_prime : P'.is_prime := comap_is_prime C P,
have hM : (0 : R ⧸ P') ∉ M := λ ⟨n, hn⟩, hp0 $ leading_coeff_eq_zero.mp (pow_eq_zero hn),
let M' : submonoid (R[X] ⧸ P) := M.map (quotient_map P C le_rfl),
refine ((quotient_map P C le_rfl).is_integral_tower_bot_of_is_integral
(algebra_map _ (localization M')) _ _),
{ refine is_localization.injective (localization M')
(show M' ≤ _, from le_non_zero_divisors_of_no_zero_divisors (λ hM', hM _)),
exact (let ⟨z, zM, z0⟩ := hM' in (quotient_map_injective (trans z0 φ.map_zero.symm)) ▸ zM) },
{ rw ← is_localization.map_comp M.le_comap_map,
refine ring_hom.is_integral_trans (algebra_map (R ⧸ P') (localization M))
(is_localization.map (localization M') _ M.le_comap_map) _ _,
{ exact (algebra_map (R ⧸ P') (localization M)).is_integral_of_surjective
(is_field.localization_map_bijective hM ((quotient.maximal_ideal_iff_is_field_quotient _).mp
(is_maximal_comap_C_of_is_maximal P hP'))).2 },
{ -- `convert` here is faster than `exact`, and this proof is near the time limit.
convert is_integral_is_localization_polynomial_quotient P pX hpX } }
end
/-- If `R` is a Jacobson ring, and `P` is a maximal ideal of `polynomial R`,
then `R → R[X]/P` is an integral map. -/
lemma quotient_mk_comp_C_is_integral_of_jacobson :
((quotient.mk P).comp C : R →+* R[X] ⧸ P).is_integral :=
begin
let P' : ideal R := P.comap C,
haveI : P'.is_prime := comap_is_prime C P,
let f : R[X] →+* polynomial (R ⧸ P') := polynomial.map_ring_hom (quotient.mk P'),
have hf : function.surjective f := map_surjective (quotient.mk P') quotient.mk_surjective,
have hPJ : P = (P.map f).comap f,
{ rw comap_map_of_surjective _ hf,
refine le_antisymm (le_sup_of_le_left le_rfl) (sup_le le_rfl _),
refine λ p hp, polynomial_mem_ideal_of_coeff_mem_ideal P p (λ n, quotient.eq_zero_iff_mem.mp _),
simpa only [coeff_map, coe_map_ring_hom] using (polynomial.ext_iff.mp hp) n },
refine ring_hom.is_integral_tower_bot_of_is_integral _ _ (injective_quotient_le_comap_map P) _,
rw ← quotient_mk_maps_eq,
refine ring_hom.is_integral_trans _ _
((quotient.mk P').is_integral_of_surjective quotient.mk_surjective) _,
apply quotient_mk_comp_C_is_integral_of_jacobson' _ _ (λ x hx, _),
any_goals { exact ideal.is_jacobson_quotient },
{ exact or.rec_on (map_eq_top_or_is_maximal_of_surjective f hf hP)
(λ h, absurd (trans (h ▸ hPJ : P = comap f ⊤) comap_top : P = ⊤) hP.ne_top) id },
{ apply_instance, },
{ obtain ⟨z, rfl⟩ := quotient.mk_surjective x,
rwa [quotient.eq_zero_iff_mem, mem_comap, hPJ, mem_comap, coe_map_ring_hom, map_C] }
end
lemma is_maximal_comap_C_of_is_jacobson :
(P.comap (C : R →+* R[X])).is_maximal :=
begin
rw [← @mk_ker _ _ P, ring_hom.ker_eq_comap_bot, comap_comap],
exact is_maximal_comap_of_is_integral_of_is_maximal' _
(quotient_mk_comp_C_is_integral_of_jacobson P) ⊥ ((bot_quotient_is_maximal_iff _).mpr hP),
end
omit P hP
lemma comp_C_integral_of_surjective_of_jacobson
{S : Type*} [field S] (f : R[X] →+* S) (hf : function.surjective f) :
(f.comp C).is_integral :=
begin
haveI : (f.ker).is_maximal := ring_hom.ker_is_maximal_of_surjective f hf,
let g : R[X] ⧸ f.ker →+* S := ideal.quotient.lift f.ker f (λ _ h, h),
have hfg : (g.comp (quotient.mk f.ker)) = f := ring_hom_ext' rfl rfl,
rw [← hfg, ring_hom.comp_assoc],
refine ring_hom.is_integral_trans _ g (quotient_mk_comp_C_is_integral_of_jacobson f.ker)
(g.is_integral_of_surjective _), --(quotient.lift_surjective f.ker f _ hf)),
rw [← hfg] at hf,
exact function.surjective.of_comp hf,
end
end
end polynomial
open mv_polynomial ring_hom
namespace mv_polynomial
lemma is_jacobson_mv_polynomial_fin {R : Type*} [comm_ring R] [H : is_jacobson R] :
∀ (n : ℕ), is_jacobson (mv_polynomial (fin n) R)
| 0 := ((is_jacobson_iso ((rename_equiv R
(equiv.equiv_pempty (fin 0))).to_ring_equiv.trans (is_empty_ring_equiv R pempty))).mpr H)
| (n+1) := (is_jacobson_iso (fin_succ_equiv R n).to_ring_equiv).2
(polynomial.is_jacobson_polynomial_iff_is_jacobson.2 (is_jacobson_mv_polynomial_fin n))
/-- General form of the nullstellensatz for Jacobson rings, since in a Jacobson ring we have
`Inf {P maximal | P ≥ I} = Inf {P prime | P ≥ I} = I.radical`. Fields are always Jacobson,
and in that special case this is (most of) the classical Nullstellensatz,
since `I(V(I))` is the intersection of maximal ideals containing `I`, which is then `I.radical` -/
instance is_jacobson {R : Type*} [comm_ring R] {ι : Type*} [finite ι] [is_jacobson R] :
is_jacobson (mv_polynomial ι R) :=
begin
casesI nonempty_fintype ι,
haveI := classical.dec_eq ι,
let e := fintype.equiv_fin ι,
rw is_jacobson_iso (rename_equiv R e).to_ring_equiv,
exact is_jacobson_mv_polynomial_fin _
end
variables {n : ℕ}
lemma quotient_mk_comp_C_is_integral_of_jacobson
{R : Type*} [comm_ring R] [is_jacobson R]
(P : ideal (mv_polynomial (fin n) R)) [P.is_maximal] :
((quotient.mk P).comp mv_polynomial.C : R →+* mv_polynomial _ R ⧸ P).is_integral :=
begin
unfreezingI {induction n with n IH},
{ refine ring_hom.is_integral_of_surjective _ (function.surjective.comp quotient.mk_surjective _),
exact C_surjective (fin 0) },
{ rw [← fin_succ_equiv_comp_C_eq_C, ← ring_hom.comp_assoc, ← ring_hom.comp_assoc,
← quotient_map_comp_mk le_rfl, ring_hom.comp_assoc (polynomial.C),
← quotient_map_comp_mk le_rfl, ring_hom.comp_assoc, ring_hom.comp_assoc,
← quotient_map_comp_mk le_rfl, ← ring_hom.comp_assoc (quotient.mk _)],
refine ring_hom.is_integral_trans _ _ _ _,
{ refine ring_hom.is_integral_trans _ _ (is_integral_of_surjective _ quotient.mk_surjective) _,
refine ring_hom.is_integral_trans _ _ _ _,
{ apply (is_integral_quotient_map_iff _).mpr (IH _),
apply polynomial.is_maximal_comap_C_of_is_jacobson _,
{ exact mv_polynomial.is_jacobson_mv_polynomial_fin n },
{ apply comap_is_maximal_of_surjective,
exact (fin_succ_equiv R n).symm.surjective } },
{ refine (is_integral_quotient_map_iff _).mpr _,
rw ← quotient_map_comp_mk le_rfl,
refine ring_hom.is_integral_trans _ _ _ ((is_integral_quotient_map_iff _).mpr _),
{ exact ring_hom.is_integral_of_surjective _ quotient.mk_surjective },
{ apply polynomial.quotient_mk_comp_C_is_integral_of_jacobson _,
{ exact mv_polynomial.is_jacobson_mv_polynomial_fin n },
{ exact comap_is_maximal_of_surjective _ (fin_succ_equiv R n).symm.surjective } } } },
{ refine (is_integral_quotient_map_iff _).mpr _,
refine ring_hom.is_integral_trans _ _ _ (is_integral_of_surjective _ quotient.mk_surjective),
exact ring_hom.is_integral_of_surjective _ (fin_succ_equiv R n).symm.surjective } }
end
lemma comp_C_integral_of_surjective_of_jacobson
{R : Type*} [comm_ring R] [is_jacobson R]
{σ : Type*} [finite σ] {S : Type*} [field S] (f : mv_polynomial σ R →+* S)
(hf : function.surjective f) : (f.comp C).is_integral :=
begin
casesI nonempty_fintype σ,
have e := (fintype.equiv_fin σ).symm,
let f' : mv_polynomial (fin _) R →+* S :=
f.comp (rename_equiv R e).to_ring_equiv.to_ring_hom,
have hf' : function.surjective f' :=
((function.surjective.comp hf (rename_equiv R e).surjective)),
have : (f'.comp C).is_integral,
{ haveI : (f'.ker).is_maximal := ker_is_maximal_of_surjective f' hf',
let g : mv_polynomial _ R ⧸ f'.ker →+* S := ideal.quotient.lift f'.ker f' (λ _ h, h),
have hfg : (g.comp (quotient.mk f'.ker)) = f' := ring_hom_ext (λ r, rfl) (λ i, rfl),
rw [← hfg, ring_hom.comp_assoc],
refine ring_hom.is_integral_trans _ g (quotient_mk_comp_C_is_integral_of_jacobson f'.ker)
(g.is_integral_of_surjective _),
rw ← hfg at hf',
exact function.surjective.of_comp hf' },
rw ring_hom.comp_assoc at this,
convert this,
refine ring_hom.ext (λ x, _),
exact ((rename_equiv R e).commutes' x).symm,
end
end mv_polynomial
end ideal
|
73bb6bc4a661c7e6b0009374930b9c0977edaab7 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /tests/lean/run/diag.lean | 628d3c16ab50255aabaa842f0d76afa994c7b95e | [
"Apache-2.0"
] | permissive | davidmueller13/lean | 65a3ed141b4088cd0a268e4de80eb6778b21a0e9 | c626e2e3c6f3771e07c32e82ee5b9e030de5b050 | refs/heads/master | 1,611,278,313,401 | 1,444,021,177,000 | 1,444,021,177,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 539 | lean | import data.examples.vector
open nat
namespace vector
definition diag {A} {n} : vector (vector A n) n → vector A n :=
nat.rec_on n
(λv, nil)
(λn₁ (r : vector (vector A n₁) n₁ → vector A n₁) (v : vector (vector A (succ n₁)) (succ n₁)),
let h₁ : A := head (head v) in
let t₁ : vector (vector A n₁) n₁ := map tail (tail v) in
h₁ :: r t₁)
example : diag ((1 :: 2 :: 3 :: nil) :: (4 :: 5 :: 6 :: nil) :: (7 :: 8 :: 9 :: nil) :: nil) = 1 :: 5 :: 9 :: nil :=
rfl
end vector
|
27ec19ccd74d71445b943aec36851d255ecca20b | 8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3 | /src/ring_theory/non_zero_divisors.lean | be552c3f952ff39bab5f8ad20d72c298db59abe7 | [
"Apache-2.0"
] | permissive | troyjlee/mathlib | e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5 | 45e7eb8447555247246e3fe91c87066506c14875 | refs/heads/master | 1,689,248,035,046 | 1,629,470,528,000 | 1,629,470,528,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,086 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Devon Tuma
-/
import group_theory.submonoid.operations
import group_theory.submonoid.membership
/-!
# Non-zero divisors
In this file we define the submonoid `non_zero_divisors` of a `monoid_with_zero`.
## Notations
This file declares the notation `R⁰` for the submonoid of non-zero-divisors of `R`,
in the locale `non_zero_divisors`. Use the statement `open_locale non_zero_divisors`
to access this notation in your own code.
-/
section non_zero_divisors
/-- The submonoid of non-zero-divisors of a `monoid_with_zero` `R`. -/
def non_zero_divisors (R : Type*) [monoid_with_zero R] : submonoid R :=
{ carrier := {x | ∀ z, z * x = 0 → z = 0},
one_mem' := λ z hz, by rwa mul_one at hz,
mul_mem' := λ x₁ x₂ hx₁ hx₂ z hz,
have z * x₁ * x₂ = 0, by rwa mul_assoc,
hx₁ z $ hx₂ (z * x₁) this }
localized "notation R`⁰`:9000 := non_zero_divisors R" in non_zero_divisors
variables {M M' M₁ : Type*} [monoid_with_zero M] [monoid_with_zero M'] [comm_monoid_with_zero M₁]
lemma mul_mem_non_zero_divisors {a b : M₁} :
a * b ∈ M₁⁰ ↔ a ∈ M₁⁰ ∧ b ∈ M₁⁰ :=
begin
split,
{ intro h,
split; intros x h'; apply h,
{ rw [←mul_assoc, h', zero_mul] },
{ rw [mul_comm a b, ←mul_assoc, h', zero_mul] } },
{ rintros ⟨ha, hb⟩ x hx,
apply ha,
apply hb,
rw [mul_assoc, hx] },
end
lemma eq_zero_of_ne_zero_of_mul_right_eq_zero [no_zero_divisors M]
{x y : M} (hnx : x ≠ 0) (hxy : y * x = 0) : y = 0 :=
or.resolve_right (eq_zero_or_eq_zero_of_mul_eq_zero hxy) hnx
lemma eq_zero_of_ne_zero_of_mul_left_eq_zero [no_zero_divisors M]
{x y : M} (hnx : x ≠ 0) (hxy : x * y = 0) : y = 0 :=
or.resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero hxy) hnx
lemma mem_non_zero_divisors_iff_ne_zero [no_zero_divisors M] [nontrivial M] {x : M} :
x ∈ M⁰ ↔ x ≠ 0 :=
⟨λ hm hz, zero_ne_one (hm 1 $ by rw [hz, one_mul]).symm,
λ hnx z, eq_zero_of_ne_zero_of_mul_right_eq_zero hnx⟩
lemma monoid_with_zero_hom.map_ne_zero_of_mem_non_zero_divisors [nontrivial M]
(g : monoid_with_zero_hom M M') (hg : function.injective g)
{x : M} (h : x ∈ M⁰) : g x ≠ 0 :=
λ h0, one_ne_zero (h 1 ((one_mul x).symm ▸ (hg (trans h0 g.map_zero.symm))))
lemma ring_hom.map_ne_zero_of_mem_non_zero_divisors {R R' : Type*} [semiring R] [semiring R']
[nontrivial R]
(g : R →+* R') (hg : function.injective g)
{x : R} (h : x ∈ R⁰) : g x ≠ 0 :=
g.to_monoid_with_zero_hom.map_ne_zero_of_mem_non_zero_divisors hg h
lemma monoid_with_zero_hom.map_mem_non_zero_divisors [nontrivial M] [no_zero_divisors M']
(g : monoid_with_zero_hom M M') (hg : function.injective g)
{x : M} (h : x ∈ M⁰) : g x ∈ M'⁰ :=
λ z hz, eq_zero_of_ne_zero_of_mul_right_eq_zero
(g.map_ne_zero_of_mem_non_zero_divisors hg h) hz
lemma ring_hom.map_mem_non_zero_divisors {R R' : Type*} [semiring R] [semiring R']
[nontrivial R] [no_zero_divisors R']
(g : R →+* R') (hg : function.injective g)
{x : R} (h : x ∈ R⁰) : g x ∈ R'⁰ :=
g.to_monoid_with_zero_hom.map_mem_non_zero_divisors hg h
lemma le_non_zero_divisors_of_no_zero_divisors [no_zero_divisors M] {S : submonoid M}
(hS : (0 : M) ∉ S) : S ≤ M⁰ :=
λ x hx y hy, or.rec_on (eq_zero_or_eq_zero_of_mul_eq_zero hy)
(λ h, h) (λ h, absurd (h ▸ hx : (0 : M) ∈ S) hS)
lemma powers_le_non_zero_divisors_of_no_zero_divisors [no_zero_divisors M]
{a : M} (ha : a ≠ 0) : submonoid.powers a ≤ M⁰ :=
le_non_zero_divisors_of_no_zero_divisors (λ h, absurd (h.rec_on (λ _ hn, pow_eq_zero hn)) ha)
lemma monoid_with_zero_hom.map_le_non_zero_divisors_of_injective
[nontrivial M] [no_zero_divisors M']
(f : monoid_with_zero_hom M M') (hf : function.injective f)
{S : submonoid M} (hS : S ≤ M⁰) : S.map ↑f ≤ M'⁰ :=
le_non_zero_divisors_of_no_zero_divisors (λ h, let ⟨x, hx, hx0⟩ := h in
zero_ne_one (hS (hf (trans hx0 (f.map_zero.symm)) ▸ hx : 0 ∈ S) 1 (mul_zero 1)).symm)
lemma ring_hom.map_le_non_zero_divisors_of_injective {R R' : Type*} [semiring R] [semiring R']
[nontrivial R] [no_zero_divisors R']
(f : R →+* R') (hf : function.injective f)
{S : submonoid R} (hS : S ≤ R⁰) : S.map ↑f ≤ R'⁰ :=
f.to_monoid_with_zero_hom.map_le_non_zero_divisors_of_injective hf hS
lemma prod_zero_iff_exists_zero [no_zero_divisors M₁] [nontrivial M₁]
{s : multiset M₁} : s.prod = 0 ↔ ∃ (r : M₁) (hr : r ∈ s), r = 0 :=
begin
split, swap,
{ rintros ⟨r, hrs, rfl⟩,
exact multiset.prod_eq_zero hrs, },
refine multiset.induction _ (λ a s ih, _) s,
{ intro habs,
simpa using habs, },
{ rw multiset.prod_cons,
intro hprod,
replace hprod := eq_zero_or_eq_zero_of_mul_eq_zero hprod,
cases hprod with ha,
{ exact ⟨a, multiset.mem_cons_self a s, ha⟩ },
{ apply (ih hprod).imp _,
rintros b ⟨hb₁, hb₂⟩,
exact ⟨multiset.mem_cons_of_mem hb₁, hb₂⟩, }, },
end
end non_zero_divisors
|
1b4f6dc7ee9cb13f7ddc0f3522b504508dd76457 | 02005f45e00c7ecf2c8ca5db60251bd1e9c860b5 | /src/category_theory/monad/limits.lean | 00a4b109bf120b691f7ade559f54c6cdf6ce97d9 | [
"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 | 13,845 | 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.adjunction
import category_theory.adjunction.limits
import category_theory.limits.preserves.shapes.terminal
namespace category_theory
open category
open category_theory.limits
universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation
namespace monad
variables {C : Type u₁} [category.{v₁} C]
variables {T : monad C}
variables {J : Type v₁} [small_category J]
namespace forget_creates_limits
variables (D : J ⥤ algebra T) (c : cone (D ⋙ forget T)) (t : is_limit c)
/-- (Impl) The natural transformation used to define the new cone -/
@[simps] def γ : (D ⋙ forget T ⋙ ↑T) ⟶ (D ⋙ forget T) := { app := λ j, (D.obj j).a }
/-- (Impl) This new cone is used to construct the algebra structure -/
@[simps] def new_cone : cone (D ⋙ forget T) :=
{ X := T.obj c.X,
π := (functor.const_comp _ _ ↑T).inv ≫ whisker_right c.π T ≫ (γ D) }
/-- The algebra structure which will be the apex of the new limit cone for `D`. -/
@[simps] def cone_point : algebra T :=
{ A := c.X,
a := t.lift (new_cone D c),
unit' :=
begin
apply t.hom_ext,
intro j,
erw [category.assoc, t.fac (new_cone D c), id_comp],
dsimp,
erw [id_comp, ← category.assoc, ← T.η.naturality, functor.id_map, category.assoc,
(D.obj j).unit, comp_id],
end,
assoc' :=
begin
apply t.hom_ext,
intro j,
rw [category.assoc, category.assoc, t.fac (new_cone D c)],
dsimp,
erw id_comp,
slice_lhs 1 2 {rw ← T.μ.naturality},
slice_lhs 2 3 {rw (D.obj j).assoc},
slice_rhs 1 2 {rw ← (T : C ⥤ C).map_comp},
rw t.fac (new_cone D c),
dsimp,
erw [id_comp, functor.map_comp, category.assoc]
end }
/-- (Impl) Construct the lifted cone in `algebra T` which will be limiting. -/
@[simps] def lifted_cone : cone D :=
{ X := cone_point D c t,
π := { app := λ j, { f := c.π.app j },
naturality' := λ X Y f, by { ext1, dsimp, erw c.w f, simp } } }
/-- (Impl) Prove that the lifted cone is limiting. -/
@[simps]
def lifted_cone_is_limit : is_limit (lifted_cone D c t) :=
{ lift := λ s,
{ f := t.lift ((forget T).map_cone s),
h' :=
begin
apply t.hom_ext, intro j,
slice_rhs 2 3 {rw t.fac ((forget T).map_cone s) j},
dsimp,
slice_lhs 2 3 {rw t.fac (new_cone D c) j},
dsimp,
rw category.id_comp,
slice_lhs 1 2 {rw ← (T : C ⥤ C).map_comp},
rw t.fac ((forget T).map_cone s) j,
exact (s.π.app j).h
end },
uniq' := λ s m J,
begin
ext1,
apply t.hom_ext,
intro j,
simpa [t.fac (functor.map_cone (forget T) s) j] using congr_arg algebra.hom.f (J j),
end }
end forget_creates_limits
-- Theorem 5.6.5 from [Riehl][riehl2017]
/-- The forgetful functor from the Eilenberg-Moore category creates limits. -/
instance forget_creates_limits : creates_limits (forget T) :=
{ creates_limits_of_shape := λ J 𝒥, by exactI
{ creates_limit := λ D,
creates_limit_of_reflects_iso (λ c t,
{ lifted_cone := forget_creates_limits.lifted_cone D c t,
valid_lift := cones.ext (iso.refl _) (λ j, (id_comp _).symm),
makes_limit := forget_creates_limits.lifted_cone_is_limit _ _ _ } ) } }
/-- `D ⋙ forget T` has a limit, then `D` has a limit. -/
lemma has_limit_of_comp_forget_has_limit (D : J ⥤ algebra T) [has_limit (D ⋙ forget T)] : has_limit D :=
has_limit_of_created D (forget T)
namespace forget_creates_colimits
-- Let's hide the implementation details in a namespace
variables {D : J ⥤ algebra T} (c : cocone (D ⋙ forget T)) (t : is_colimit c)
-- We have a diagram D of shape J in the category of algebras, and we assume that we are given a
-- colimit for its image D ⋙ forget T under the forgetful functor, say its apex is L.
-- We'll construct a colimiting coalgebra for D, whose carrier will also be L.
-- To do this, we must find a map TL ⟶ L. Since T preserves colimits, TL is also a colimit.
-- In particular, it is a colimit for the diagram `(D ⋙ forget T) ⋙ T`
-- so to construct a map TL ⟶ L it suffices to show that L is the apex of a cocone for this diagram.
-- In other words, we need a natural transformation from const L to `(D ⋙ forget T) ⋙ T`.
-- But we already know that L is the apex of a cocone for the diagram `D ⋙ forget T`, so it
-- suffices to give a natural transformation `((D ⋙ forget T) ⋙ T) ⟶ (D ⋙ forget T)`:
/--
(Impl)
The natural transformation given by the algebra structure maps, used to construct a cocone `c` with
apex `colimit (D ⋙ forget T)`.
-/
@[simps] def γ : ((D ⋙ forget T) ⋙ ↑T) ⟶ (D ⋙ forget T) := { app := λ j, (D.obj j).a }
/--
(Impl)
A cocone for the diagram `(D ⋙ forget T) ⋙ T` found by composing the natural transformation `γ`
with the colimiting cocone for `D ⋙ forget T`.
-/
@[simps]
def new_cocone : cocone ((D ⋙ forget T) ⋙ ↑T) :=
{ X := c.X,
ι := γ ≫ c.ι }
variables [preserves_colimit (D ⋙ forget T) (T : C ⥤ C)]
/--
(Impl)
Define the map `λ : TL ⟶ L`, which will serve as the structure of the coalgebra on `L`, and
we will show is the colimiting object. We use the cocone constructed by `c` and the fact that
`T` preserves colimits to produce this morphism.
-/
@[reducible]
def lambda : ((T : C ⥤ C).map_cocone c).X ⟶ c.X :=
(preserves_colimit.preserves t).desc (new_cocone c)
/-- (Impl) The key property defining the map `λ : TL ⟶ L`. -/
lemma commuting (j : J) :
T.map (c.ι.app j) ≫ lambda c t = (D.obj j).a ≫ c.ι.app j :=
is_colimit.fac (preserves_colimit.preserves t) (new_cocone c) j
variables [preserves_colimit ((D ⋙ forget T) ⋙ ↑T) (T : C ⥤ C)]
/--
(Impl)
Construct the colimiting algebra from the map `λ : TL ⟶ L` given by `lambda`. We are required to
show it satisfies the two algebra laws, which follow from the algebra laws for the image of `D` and
our `commuting` lemma.
-/
@[simps] def cocone_point :
algebra T :=
{ A := c.X,
a := lambda c t,
unit' :=
begin
apply t.hom_ext,
intro j,
erw [comp_id, ← category.assoc, T.η.naturality, category.assoc, commuting, ← category.assoc],
erw algebra.unit, apply id_comp
end,
assoc' :=
begin
apply is_colimit.hom_ext (preserves_colimit.preserves (preserves_colimit.preserves t)),
intro j,
erw [← category.assoc, T.μ.naturality, ← functor.map_cocone_ι_app, category.assoc,
is_colimit.fac _ (new_cocone c) j],
rw ← category.assoc,
erw [← functor.map_comp, commuting],
dsimp,
erw [← category.assoc, algebra.assoc, category.assoc, functor.map_comp, category.assoc, commuting],
apply_instance, apply_instance
end }
/-- (Impl) Construct the lifted cocone in `algebra T` which will be colimiting. -/
@[simps] def lifted_cocone : cocone D :=
{ X := cocone_point c t,
ι := { app := λ j, { f := c.ι.app j, h' := commuting _ _ _ },
naturality' := λ A B f, by { ext1, dsimp, erw [comp_id, c.w] } } }
/-- (Impl) Prove that the lifted cocone is colimiting. -/
@[simps]
def lifted_cocone_is_colimit : is_colimit (lifted_cocone c t) :=
{ desc := λ s,
{ f := t.desc ((forget T).map_cocone s),
h' :=
begin
dsimp,
apply is_colimit.hom_ext (preserves_colimit.preserves t),
intro j,
rw ← category.assoc, erw ← functor.map_comp,
erw t.fac',
rw ← category.assoc, erw forget_creates_colimits.commuting,
rw category.assoc, rw t.fac',
apply algebra.hom.h,
apply_instance
end },
uniq' := λ s m J, by { ext1, apply t.hom_ext, intro j, simpa using congr_arg algebra.hom.f (J j) } }
end forget_creates_colimits
open forget_creates_colimits
-- TODO: the converse of this is true as well
/--
The forgetful functor from the Eilenberg-Moore category for a monad creates any colimit
which the monad itself preserves.
-/
instance forget_creates_colimit (D : J ⥤ algebra T)
[preserves_colimit (D ⋙ forget T) (T : C ⥤ C)]
[preserves_colimit ((D ⋙ forget T) ⋙ ↑T) (T : C ⥤ C)] :
creates_colimit D (forget T) :=
creates_colimit_of_reflects_iso $ λ c t,
{ lifted_cocone :=
{ X := cocone_point c t,
ι :=
{ app := λ j, { f := c.ι.app j, h' := commuting _ _ _ },
naturality' := λ A B f, by { ext1, dsimp, erw [comp_id, c.w] } } },
valid_lift := cocones.ext (iso.refl _) (by tidy),
makes_colimit := lifted_cocone_is_colimit _ _ }
instance forget_creates_colimits_of_shape
[preserves_colimits_of_shape J (T : C ⥤ C)] :
creates_colimits_of_shape J (forget T) :=
{ creates_colimit := λ K, by apply_instance }
instance forget_creates_colimits
[preserves_colimits (T : C ⥤ C)] :
creates_colimits (forget T) :=
{ creates_colimits_of_shape := λ J 𝒥₁, by apply_instance }
/--
For `D : J ⥤ algebra T`, `D ⋙ forget T` has a colimit, then `D` has a colimit provided colimits
of shape `J` are preserved by `T`.
-/
lemma forget_creates_colimits_of_monad_preserves
[preserves_colimits_of_shape J (T : C ⥤ C)] (D : J ⥤ algebra T) [has_colimit (D ⋙ forget T)] :
has_colimit D :=
has_colimit_of_created D (forget T)
end monad
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₁} D]
variables {J : Type v₁} [small_category J]
instance comp_comparison_forget_has_limit
(F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit (F ⋙ R)] :
has_limit ((F ⋙ monad.comparison (adjunction.of_right_adjoint R)) ⋙ monad.forget _) :=
@has_limit_of_iso _ _ _ _ (F ⋙ R) _ _
(iso_whisker_left F (monad.comparison_forget (adjunction.of_right_adjoint R)).symm)
instance comp_comparison_has_limit
(F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit (F ⋙ R)] :
has_limit (F ⋙ monad.comparison (adjunction.of_right_adjoint R)) :=
monad.has_limit_of_comp_forget_has_limit (F ⋙ monad.comparison (adjunction.of_right_adjoint R))
/-- Any monadic functor creates limits. -/
def monadic_creates_limits (R : D ⥤ C) [monadic_right_adjoint R] :
creates_limits R :=
creates_limits_of_nat_iso (monad.comparison_forget (adjunction.of_right_adjoint R))
/--
The forgetful functor from the Eilenberg-Moore category for a monad creates any colimit
which the monad itself preserves.
-/
def monadic_creates_colimit_of_preserves_colimit (R : D ⥤ C) (K : J ⥤ D)
[monadic_right_adjoint R]
[preserves_colimit (K ⋙ R) (left_adjoint R ⋙ R)]
[preserves_colimit ((K ⋙ R) ⋙ left_adjoint R ⋙ R) (left_adjoint R ⋙ R)] :
creates_colimit K R :=
begin
apply creates_colimit_of_nat_iso (monad.comparison_forget (adjunction.of_right_adjoint R)),
apply category_theory.comp_creates_colimit _ _,
apply_instance,
let i : ((K ⋙ monad.comparison (adjunction.of_right_adjoint R)) ⋙ monad.forget _) ≅ K ⋙ R :=
functor.associator _ _ _ ≪≫ iso_whisker_left K (monad.comparison_forget (adjunction.of_right_adjoint R)),
apply category_theory.monad.forget_creates_colimit _,
{ dsimp,
refine preserves_colimit_of_iso_diagram _ i.symm },
{ dsimp,
refine preserves_colimit_of_iso_diagram _ (iso_whisker_right i (left_adjoint R ⋙ R)).symm },
end
/-- A monadic functor creates any colimits of shapes it preserves. -/
def monadic_creates_colimits_of_shape_of_preserves_colimits_of_shape (R : D ⥤ C)
[monadic_right_adjoint R] [preserves_colimits_of_shape J R] : creates_colimits_of_shape J R :=
begin
have : preserves_colimits_of_shape J (left_adjoint R ⋙ R),
{ apply category_theory.limits.comp_preserves_colimits_of_shape _ _,
{ haveI := adjunction.left_adjoint_preserves_colimits (adjunction.of_right_adjoint R),
apply_instance },
apply_instance },
exactI ⟨λ K, monadic_creates_colimit_of_preserves_colimit _ _⟩,
end
/-- A monadic functor creates colimits if it preserves colimits. -/
def monadic_creates_colimits_of_preserves_colimits (R : D ⥤ C) [monadic_right_adjoint R]
[preserves_colimits R] : creates_colimits R :=
{ creates_colimits_of_shape := λ J 𝒥₁,
by exactI monadic_creates_colimits_of_shape_of_preserves_colimits_of_shape _ }
section
lemma has_limit_of_reflective (F : J ⥤ D) (R : D ⥤ C) [has_limit (F ⋙ R)] [reflective R] :
has_limit F :=
by { haveI := monadic_creates_limits R, exact has_limit_of_created F R }
/-- If `C` has limits of shape `J` then any reflective subcategory has limits of shape `J`. -/
lemma has_limits_of_shape_of_reflective [has_limits_of_shape J C] (R : D ⥤ C) [reflective R] :
has_limits_of_shape J D :=
{ has_limit := λ F, has_limit_of_reflective F R }
/-- If `C` has limits then any reflective subcategory has limits. -/
lemma has_limits_of_reflective (R : D ⥤ C) [has_limits C] [reflective R] : has_limits D :=
{ has_limits_of_shape := λ J 𝒥₁, by exactI has_limits_of_shape_of_reflective R }
/--
The reflector always preserves terminal objects. Note this in general doesn't apply to any other
limit.
-/
noncomputable def left_adjoint_preserves_terminal_of_reflective
(R : D ⥤ C) [reflective R] [has_terminal C] :
preserves_limits_of_shape (discrete pempty) (left_adjoint R) :=
{ preserves_limit := λ K,
begin
letI : has_terminal D := has_limits_of_shape_of_reflective R,
letI := monadic_creates_limits R,
letI := category_theory.preserves_limit_of_creates_limit_and_has_limit (functor.empty _) R,
letI : preserves_limit (functor.empty _) (left_adjoint R),
{ apply preserves_terminal_of_iso,
apply _ ≪≫ as_iso ((adjunction.of_right_adjoint R).counit.app (⊤_ D)),
apply (left_adjoint R).map_iso (preserves_terminal.iso R).symm },
apply preserves_limit_of_iso_diagram (left_adjoint R) (functor.unique_from_empty _).symm,
end }
end
end category_theory
|
d8c2cd0553ef420d2d1684405266f814540b5c7c | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/data/multiset/antidiagonal.lean | 11de3bec9c3edeb01a2d7f4a68d76358b73d0b1c | [
"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,642 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.multiset.bind
import data.multiset.powerset
/-!
# The antidiagonal on a multiset.
The antidiagonal of a multiset `s` consists of all pairs `(t₁, t₂)`
such that `t₁ + t₂ = s`. These pairs are counted with multiplicities.
-/
namespace multiset
open list
variables {α β : Type*}
/-- The antidiagonal of a multiset `s` consists of all pairs `(t₁, t₂)`
such that `t₁ + t₂ = s`. These pairs are counted with multiplicities. -/
def antidiagonal (s : multiset α) : multiset (multiset α × multiset α) :=
quot.lift_on s
(λ l, (revzip (powerset_aux l) : multiset (multiset α × multiset α)))
(λ l₁ l₂ h, quot.sound (revzip_powerset_aux_perm h))
theorem antidiagonal_coe (l : list α) :
@antidiagonal α l = revzip (powerset_aux l) := rfl
@[simp] theorem antidiagonal_coe' (l : list α) :
@antidiagonal α l = revzip (powerset_aux' l) :=
quot.sound revzip_powerset_aux_perm_aux'
/-- A pair `(t₁, t₂)` of multisets is contained in `antidiagonal s`
if and only if `t₁ + t₂ = s`. -/
@[simp] theorem mem_antidiagonal {s : multiset α} {x : multiset α × multiset α} :
x ∈ antidiagonal s ↔ x.1 + x.2 = s :=
quotient.induction_on s $ λ l, begin
simp [antidiagonal_coe], refine ⟨λ h, revzip_powerset_aux h, λ h, _⟩,
haveI := classical.dec_eq α,
simp [revzip_powerset_aux_lemma l revzip_powerset_aux, h.symm],
cases x with x₁ x₂,
dsimp only,
exact ⟨x₁, le_add_right _ _, by rw add_tsub_cancel_left x₁ x₂⟩
end
@[simp] theorem antidiagonal_map_fst (s : multiset α) :
(antidiagonal s).map prod.fst = powerset s :=
quotient.induction_on s $ λ l,
by simp [powerset_aux']
@[simp] theorem antidiagonal_map_snd (s : multiset α) :
(antidiagonal s).map prod.snd = powerset s :=
quotient.induction_on s $ λ l,
by simp [powerset_aux']
@[simp] theorem antidiagonal_zero : @antidiagonal α 0 = {(0, 0)} := rfl
@[simp] theorem antidiagonal_cons (a : α) (s) : antidiagonal (a ::ₘ s) =
map (prod.map id (cons a)) (antidiagonal s) +
map (prod.map (cons a) id) (antidiagonal s) :=
quotient.induction_on s $ λ l, begin
simp only [revzip, reverse_append, quot_mk_to_coe, coe_eq_coe, powerset_aux'_cons, cons_coe,
coe_map, antidiagonal_coe', coe_add],
rw [← zip_map, ← zip_map, zip_append, (_ : _++_=_)],
{congr; simp}, {simp}
end
theorem antidiagonal_eq_map_powerset [decidable_eq α] (s : multiset α) :
s.antidiagonal = s.powerset.map (λ t, (s - t, t)) :=
begin
induction s using multiset.induction_on with a s hs,
{ simp only [antidiagonal_zero, powerset_zero, zero_tsub, map_singleton] },
{ simp_rw [antidiagonal_cons, powerset_cons, map_add, hs, map_map, function.comp, prod.map_mk,
id.def, sub_cons, erase_cons_head],
rw add_comm,
congr' 1,
refine multiset.map_congr rfl (λ x hx, _),
rw cons_sub_of_le _ (mem_powerset.mp hx) }
end
@[simp] theorem card_antidiagonal (s : multiset α) :
card (antidiagonal s) = 2 ^ card s :=
by have := card_powerset s;
rwa [← antidiagonal_map_fst, card_map] at this
lemma prod_map_add [comm_semiring β] {s : multiset α} {f g : α → β} :
prod (s.map (λa, f a + g a)) =
sum ((antidiagonal s).map (λp, (p.1.map f).prod * (p.2.map g).prod)) :=
begin
refine s.induction_on _ _,
{ simp },
{ assume a s ih,
have := @sum_map_mul_left α β _,
simp [ih, add_mul, mul_comm, mul_left_comm (f a), mul_left_comm (g a), mul_assoc,
sum_map_mul_left.symm],
cc },
end
end multiset
|
6e689b8c1faae8b22bd148943c7968c9b2ab5541 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/fin/tuple/nat_antidiagonal.lean | e6aa9a68a4b4bc613d59aef46d5647fed14ae626 | [
"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 | 9,025 | lean | /-
Copyright (c) 2022 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import algebra.big_operators.fin
import data.finset.nat_antidiagonal
import data.fin.vec_notation
import logic.equiv.fin
/-!
# Collections of tuples of naturals with the same sum
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file generalizes `list.nat.antidiagonal n`, `multiset.nat.antidiagonal n`, and
`finset.nat.antidiagonal n` from the pair of elements `x : ℕ × ℕ` such that `n = x.1 + x.2`, to
the sequence of elements `x : fin k → ℕ` such that `n = ∑ i, x i`.
## Main definitions
* `list.nat.antidiagonal_tuple`
* `multiset.nat.antidiagonal_tuple`
* `finset.nat.antidiagonal_tuple`
## Main results
* `antidiagonal_tuple 2 n` is analogous to `antidiagonal n`:
* `list.nat.antidiagonal_tuple_two`
* `multiset.nat.antidiagonal_tuple_two`
* `finset.nat.antidiagonal_tuple_two`
## Implementation notes
While we could implement this by filtering `(fintype.pi_finset $ λ _, range (n + 1))` or similar,
this implementation would be much slower.
In the future, we could consider generalizing `finset.nat.antidiagonal_tuple` further to
support finitely-supported functions, as is done with `cut` in
`archive/100-theorems-list/45_partition.lean`.
-/
open_locale big_operators
/-! ### Lists -/
namespace list.nat
/-- `list.antidiagonal_tuple k n` is a list of all `k`-tuples which sum to `n`.
This list contains no duplicates (`list.nat.nodup_antidiagonal_tuple`), and is sorted
lexicographically (`list.nat.antidiagonal_tuple_pairwise_pi_lex`), starting with `![0, ..., n]`
and ending with `![n, ..., 0]`.
```
#eval antidiagonal_tuple 3 2
-- [![0, 0, 2], ![0, 1, 1], ![0, 2, 0], ![1, 0, 1], ![1, 1, 0], ![2, 0, 0]]
```
-/
def antidiagonal_tuple : Π k, ℕ → list (fin k → ℕ)
| 0 0 := [![]]
| 0 (n + 1) := []
| (k + 1) n := (list.nat.antidiagonal n).bind $ λ ni,
(antidiagonal_tuple k ni.2).map $ λ x, fin.cons (ni.1) x
@[simp] lemma antidiagonal_tuple_zero_zero : antidiagonal_tuple 0 0 = [![]] := rfl
@[simp] lemma antidiagonal_tuple_zero_succ (n : ℕ) : antidiagonal_tuple 0 n.succ = [] := rfl
lemma mem_antidiagonal_tuple {n : ℕ} {k : ℕ} {x : fin k → ℕ} :
x ∈ antidiagonal_tuple k n ↔ ∑ i, x i = n :=
begin
revert n,
refine fin.cons_induction _ _ x,
{ intro n,
cases n,
{ simp },
{ simp [eq_comm] } },
{ intros k x₀ x ih n,
simp_rw [fin.sum_cons, antidiagonal_tuple, list.mem_bind, list.mem_map,
list.nat.mem_antidiagonal, fin.cons_eq_cons, exists_eq_right_right, ih,
@eq_comm _ _ (prod.snd _), and_comm (prod.snd _ = _), ←prod.mk.inj_iff, prod.mk.eta,
exists_prop, exists_eq_right] },
end
/-- The antidiagonal of `n` does not contain duplicate entries. -/
lemma nodup_antidiagonal_tuple (k n : ℕ) : list.nodup (antidiagonal_tuple k n) :=
begin
induction k with k ih generalizing n,
{ cases n,
{ simp },
{ simp [eq_comm] }, },
simp_rw [antidiagonal_tuple, list.nodup_bind],
split,
{ intros i hi,
exact (ih i.snd).map (fin.cons_right_injective (i.fst : (λ _, ℕ) 0)), },
induction n,
{ exact list.pairwise_singleton _ _ },
{ rw list.nat.antidiagonal_succ,
refine list.pairwise.cons (λ a ha x hx₁ hx₂, _) (n_ih.map _ (λ a b h x hx₁ hx₂, _)),
{ rw list.mem_map at hx₁ hx₂ ha,
obtain ⟨⟨a, -, rfl⟩, ⟨x₁, -, rfl⟩, ⟨x₂, -, h⟩⟩ := ⟨ha, hx₁, hx₂⟩,
rw fin.cons_eq_cons at h,
injection h.1, },
{ rw list.mem_map at hx₁ hx₂,
obtain ⟨⟨x₁, hx₁, rfl⟩, ⟨x₂, hx₂, h₁₂⟩⟩ := ⟨hx₁, hx₂⟩,
dsimp at h₁₂,
rw [fin.cons_eq_cons, nat.succ_inj'] at h₁₂,
obtain ⟨h₁₂, rfl⟩ := h₁₂,
rw h₁₂ at h,
exact h (list.mem_map_of_mem _ hx₁) (list.mem_map_of_mem _ hx₂) }, },
end
lemma antidiagonal_tuple_zero_right : ∀ k, antidiagonal_tuple k 0 = [0]
| 0 := congr_arg (λ x, [x]) $ subsingleton.elim _ _
| (k + 1) := begin
rw [antidiagonal_tuple, antidiagonal_zero, list.bind_singleton, antidiagonal_tuple_zero_right k,
list.map_singleton],
exact congr_arg (λ x, [x]) matrix.cons_zero_zero
end
@[simp] lemma antidiagonal_tuple_one (n : ℕ) : antidiagonal_tuple 1 n = [![n]] :=
begin
simp_rw [antidiagonal_tuple, antidiagonal, list.range_succ, list.map_append, list.map_singleton,
tsub_self, list.bind_append, list.bind_singleton, antidiagonal_tuple_zero_zero,
list.map_singleton, list.map_bind],
conv_rhs { rw ← list.nil_append [![n]]},
congr' 1,
simp_rw [list.bind_eq_nil, list.mem_range, list.map_eq_nil],
intros x hx,
obtain ⟨m, rfl⟩ := nat.exists_eq_add_of_lt hx,
rw [add_assoc, add_tsub_cancel_left, antidiagonal_tuple_zero_succ],
end
lemma antidiagonal_tuple_two (n : ℕ) :
antidiagonal_tuple 2 n = (antidiagonal n).map (λ i, ![i.1, i.2]) :=
begin
rw antidiagonal_tuple,
simp_rw [antidiagonal_tuple_one, list.map_singleton],
rw [list.map_eq_bind],
refl,
end
lemma antidiagonal_tuple_pairwise_pi_lex : ∀ k n,
(antidiagonal_tuple k n).pairwise (pi.lex (<) (λ _, (<)))
| 0 0 := list.pairwise_singleton _ _
| 0 (n + 1) := list.pairwise.nil
| (k + 1) n := begin
simp_rw [antidiagonal_tuple, list.pairwise_bind, list.pairwise_map, list.mem_map,
forall_exists_index, and_imp, forall_apply_eq_imp_iff₂],
simp only [mem_antidiagonal, prod.forall, and_imp, forall_apply_eq_imp_iff₂],
simp only [fin.pi_lex_lt_cons_cons, eq_self_iff_true, true_and, lt_self_iff_false, false_or],
refine ⟨λ _ _ _, antidiagonal_tuple_pairwise_pi_lex k _, _⟩,
induction n,
{ rw [antidiagonal_zero],
exact list.pairwise_singleton _ _ },
{ rw [antidiagonal_succ, list.pairwise_cons, list.pairwise_map],
refine ⟨λ p hp x hx y hy, _, _⟩,
{ rw [list.mem_map, prod.exists] at hp,
obtain ⟨a, b, hab, (rfl : (nat.succ a, b) = p)⟩ := hp,
exact or.inl (nat.zero_lt_succ _), },
dsimp,
simp_rw [nat.succ_inj', nat.succ_lt_succ_iff],
exact n_ih },
end
end list.nat
/-! ### Multisets -/
namespace multiset.nat
/-- `multiset.antidiagonal_tuple k n` is a multiset of `k`-tuples summing to `n` -/
def antidiagonal_tuple (k n : ℕ) : multiset (fin k → ℕ) :=
list.nat.antidiagonal_tuple k n
@[simp] lemma antidiagonal_tuple_zero_zero : antidiagonal_tuple 0 0 = { ![]} := rfl
@[simp] lemma antidiagonal_tuple_zero_succ (n : ℕ) : antidiagonal_tuple 0 n.succ = 0 := rfl
lemma mem_antidiagonal_tuple {n : ℕ} {k : ℕ} {x : fin k → ℕ} :
x ∈ antidiagonal_tuple k n ↔ ∑ i, x i = n :=
list.nat.mem_antidiagonal_tuple
lemma nodup_antidiagonal_tuple (k n : ℕ) : (antidiagonal_tuple k n).nodup :=
list.nat.nodup_antidiagonal_tuple _ _
lemma antidiagonal_tuple_zero_right (k : ℕ) : antidiagonal_tuple k 0 = {0} :=
congr_arg _ (list.nat.antidiagonal_tuple_zero_right k)
@[simp] lemma antidiagonal_tuple_one (n : ℕ) : antidiagonal_tuple 1 n = { ![n]} :=
congr_arg _ (list.nat.antidiagonal_tuple_one n)
lemma antidiagonal_tuple_two (n : ℕ) :
antidiagonal_tuple 2 n = (antidiagonal n).map (λ i, ![i.1, i.2]) :=
congr_arg _ (list.nat.antidiagonal_tuple_two n)
end multiset.nat
/-! ### Finsets -/
namespace finset.nat
/-- `finset.antidiagonal_tuple k n` is a finset of `k`-tuples summing to `n` -/
def antidiagonal_tuple (k n : ℕ) : finset (fin k → ℕ) :=
⟨multiset.nat.antidiagonal_tuple k n, multiset.nat.nodup_antidiagonal_tuple k n⟩
@[simp] lemma antidiagonal_tuple_zero_zero : antidiagonal_tuple 0 0 = { ![]} := rfl
@[simp] lemma antidiagonal_tuple_zero_succ (n : ℕ) : antidiagonal_tuple 0 n.succ = ∅ := rfl
lemma mem_antidiagonal_tuple {n : ℕ} {k : ℕ} {x : fin k → ℕ} :
x ∈ antidiagonal_tuple k n ↔ ∑ i, x i = n :=
list.nat.mem_antidiagonal_tuple
lemma antidiagonal_tuple_zero_right (k : ℕ) : antidiagonal_tuple k 0 = {0} :=
finset.eq_of_veq (multiset.nat.antidiagonal_tuple_zero_right k)
@[simp] lemma antidiagonal_tuple_one (n : ℕ) : antidiagonal_tuple 1 n = { ![n]} :=
finset.eq_of_veq (multiset.nat.antidiagonal_tuple_one n)
lemma antidiagonal_tuple_two (n : ℕ) :
antidiagonal_tuple 2 n = (antidiagonal n).map (pi_fin_two_equiv (λ _, ℕ)).symm.to_embedding :=
finset.eq_of_veq (multiset.nat.antidiagonal_tuple_two n)
section equiv_prod
/-- The disjoint union of antidiagonal tuples `Σ n, antidiagonal_tuple k n` is equivalent to the
`k`-tuple `fin k → ℕ`. This is such an equivalence, obtained by mapping `(n, x)` to `x`.
This is the tuple version of `finset.nat.sigma_antidiagonal_equiv_prod`. -/
@[simps] def sigma_antidiagonal_tuple_equiv_tuple (k : ℕ) :
(Σ n, antidiagonal_tuple k n) ≃ (fin k → ℕ) :=
{ to_fun := λ x, x.2,
inv_fun := λ x, ⟨∑ i, x i, x, mem_antidiagonal_tuple.mpr rfl⟩,
left_inv := λ ⟨n, t, h⟩, sigma.subtype_ext (mem_antidiagonal_tuple.mp h) rfl,
right_inv := λ x, rfl }
end equiv_prod
end finset.nat
|
99832050fc1fbe3c6d50fd10e5e45f48df6780b6 | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/category_theory/limits/shapes/products.lean | 850968b15327d0311e02440d2fd44d92d5099ee9 | [
"Apache-2.0"
] | permissive | mcncm/mathlib | 8d25099344d9d2bee62822cb9ed43aa3e09fa05e | fde3d78cadeec5ef827b16ae55664ef115e66f57 | refs/heads/master | 1,672,743,316,277 | 1,602,618,514,000 | 1,602,618,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,166 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.limits.limits
import category_theory.discrete_category
noncomputable theory
universes v u
open category_theory
namespace category_theory.limits
variables {β : Type v}
variables {C : Type u} [category.{v} C]
-- We don't need an analogue of `pair` (for binary products), `parallel_pair` (for equalizers),
-- or `(co)span`, since we already have `discrete.functor`.
/-- A fan over `f : β → C` consists of a collection of maps from an object `P` to every `f b`. -/
abbreviation fan (f : β → C) := cone (discrete.functor f)
/-- A cofan over `f : β → C` consists of a collection of maps from every `f b` to an object `P`. -/
abbreviation cofan (f : β → C) := cocone (discrete.functor f)
/-- A fan over `f : β → C` consists of a collection of maps from an object `P` to every `f b`. -/
def fan.mk {f : β → C} {P : C} (p : Π b, P ⟶ f b) : fan f :=
{ X := P,
π := { app := p } }
/-- A cofan over `f : β → C` consists of a collection of maps from every `f b` to an object `P`. -/
def cofan.mk {f : β → C} {P : C} (p : Π b, f b ⟶ P) : cofan f :=
{ X := P,
ι := { app := p } }
@[simp] lemma fan.mk_π_app {f : β → C} {P : C} (p : Π b, P ⟶ f b) (b : β) : (fan.mk p).π.app b = p b := rfl
@[simp] lemma cofan.mk_π_app {f : β → C} {P : C} (p : Π b, f b ⟶ P) (b : β) : (cofan.mk p).ι.app b = p b := rfl
/-- An abbreviation for `has_limit (discrete.functor f)`. -/
abbreviation has_product (f : β → C) := has_limit (discrete.functor f)
/-- An abbreviation for `has_colimit (discrete.functor f)`. -/
abbreviation has_coproduct (f : β → C) := has_colimit (discrete.functor f)
section
variables (C)
/-- An abbreviation for `has_limits_of_shape (discrete f)`. -/
abbreviation has_products_of_shape (β : Type v) := has_limits_of_shape.{v} (discrete β)
/-- An abbreviation for `has_colimits_of_shape (discrete f)`. -/
abbreviation has_coproducts_of_shape (β : Type v) := has_colimits_of_shape.{v} (discrete β)
end
/-- `pi_obj f` computes the product of a family of elements `f`. (It is defined as an abbreviation
for `limit (discrete.functor f)`, so for most facts about `pi_obj f`, you will just use general facts
about limits.) -/
abbreviation pi_obj (f : β → C) [has_product f] := limit (discrete.functor f)
/-- `sigma_obj f` computes the coproduct of a family of elements `f`. (It is defined as an abbreviation
for `colimit (discrete.functor f)`, so for most facts about `sigma_obj f`, you will just use general facts
about colimits.) -/
abbreviation sigma_obj (f : β → C) [has_coproduct f] := colimit (discrete.functor f)
notation `∏ ` f:20 := pi_obj f
notation `∐ ` f:20 := sigma_obj f
/-- The `b`-th projection from the pi object over `f` has the form `∏ f ⟶ f b`. -/
abbreviation pi.π (f : β → C) [has_product f] (b : β) : ∏ f ⟶ f b :=
limit.π (discrete.functor f) b
/-- The `b`-th inclusion into the sigma object over `f` has the form `f b ⟶ ∐ f`. -/
abbreviation sigma.ι (f : β → C) [has_coproduct f] (b : β) : f b ⟶ ∐ f :=
colimit.ι (discrete.functor f) b
/-- A collection of morphisms `P ⟶ f b` induces a morphism `P ⟶ ∏ f`. -/
abbreviation pi.lift {f : β → C} [has_product f] {P : C} (p : Π b, P ⟶ f b) : P ⟶ ∏ f :=
limit.lift _ (fan.mk p)
/-- A collection of morphisms `f b ⟶ P` induces a morphism `∐ f ⟶ P`. -/
abbreviation sigma.desc {f : β → C} [has_coproduct f] {P : C} (p : Π b, f b ⟶ P) : ∐ f ⟶ P :=
colimit.desc _ (cofan.mk p)
/--
Construct a morphism between categorical products (indexed by the same type)
from a family of morphisms between the factors.
-/
abbreviation pi.map {f g : β → C} [has_products_of_shape β C]
(p : Π b, f b ⟶ g b) : ∏ f ⟶ ∏ g :=
lim.map (discrete.nat_trans p)
/--
Construct an isomorphism between categorical products (indexed by the same type)
from a family of isomorphisms between the factors.
-/
abbreviation pi.map_iso {f g : β → C} [has_products_of_shape β C]
(p : Π b, f b ≅ g b) : ∏ f ≅ ∏ g :=
lim.map_iso (discrete.nat_iso p)
/--
Construct a morphism between categorical coproducts (indexed by the same type)
from a family of morphisms between the factors.
-/
abbreviation sigma.map {f g : β → C} [has_coproducts_of_shape β C]
(p : Π b, f b ⟶ g b) : ∐ f ⟶ ∐ g :=
colim.map (discrete.nat_trans p)
/--
Construct an isomorphism between categorical coproducts (indexed by the same type)
from a family of isomorphisms between the factors.
-/
abbreviation sigma.map_iso {f g : β → C} [has_coproducts_of_shape β C]
(p : Π b, f b ≅ g b) : ∐ f ≅ ∐ g :=
colim.map_iso (discrete.nat_iso p)
variables (C)
/-- An abbreviation for `Π J, has_limits_of_shape (discrete J) C` -/
abbreviation has_products := Π (J : Type v), has_limits_of_shape (discrete J) C
/-- An abbreviation for `Π J, has_colimits_of_shape (discrete J) C` -/
abbreviation has_coproducts := Π (J : Type v), has_colimits_of_shape (discrete J) C
end category_theory.limits
|
7a861e14f965d480fc8c840587b2773ac1d339bf | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/measure_theory/function/continuous_map_dense.lean | aa27a678c3ba631d16c88775aa6b6e2a29cf7900 | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,307 | lean | /-
Copyright (c) 2021 Heather Macbeth. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Heather Macbeth
-/
import measure_theory.measure.regular
import measure_theory.function.simple_func_dense
import topology.urysohns_lemma
/-!
# Approximation in Lᵖ by continuous functions
This file proves that bounded continuous functions are dense in `Lp E p μ`, for `1 ≤ p < ∞`, if the
domain `α` of the functions is a normal topological space and the measure `μ` is weakly regular.
The result is presented in several versions:
* `measure_theory.Lp.bounded_continuous_function_dense`: The subgroup
`measure_theory.Lp.bounded_continuous_function` of `Lp E p μ`, the additive subgroup of
`Lp E p μ` consisting of equivalence classes containing a continuous representative, is dense in
`Lp E p μ`.
* `bounded_continuous_function.to_Lp_dense_range`: For finite-measure `μ`, the continuous linear
map `bounded_continuous_function.to_Lp p μ 𝕜` from `α →ᵇ E` to `Lp E p μ` has dense range.
* `continuous_map.to_Lp_dense_range`: For compact `α` and finite-measure `μ`, the continuous linear
map `continuous_map.to_Lp p μ 𝕜` from `C(α, E)` to `Lp E p μ` has dense range.
Note that for `p = ∞` this result is not true: the characteristic function of the set `[0, ∞)` in
`ℝ` cannot be continuously approximated in `L∞`.
The proof is in three steps. First, since simple functions are dense in `Lp`, it suffices to prove
the result for a scalar multiple of a characteristic function of a measurable set `s`. Secondly,
since the measure `μ` is weakly regular, the set `s` can be approximated above by an open set and
below by a closed set. Finally, since the domain `α` is normal, we use Urysohn's lemma to find a
continuous function interpolating between these two sets.
## Related results
Are you looking for a result on "directional" approximation (above or below with respect to an
order) of functions whose codomain is `ℝ≥0∞` or `ℝ`, by semicontinuous functions? See the
Vitali-Carathéodory theorem, in the file `measure_theory.vitali_caratheodory`.
-/
open_locale ennreal nnreal topological_space bounded_continuous_function
open measure_theory topological_space continuous_map
variables {α : Type*} [measurable_space α] [topological_space α] [normal_space α] [borel_space α]
variables (E : Type*) [measurable_space E] [normed_group E] [borel_space E]
[second_countable_topology E]
variables {p : ℝ≥0∞} [_i : fact (1 ≤ p)] (hp : p ≠ ∞) (μ : measure α)
include _i hp
namespace measure_theory.Lp
variables [normed_space ℝ E]
/-- A function in `Lp` can be approximated in `Lp` by continuous functions. -/
lemma bounded_continuous_function_dense [μ.weakly_regular] :
(bounded_continuous_function E p μ).topological_closure = ⊤ :=
begin
have hp₀ : 0 < p := lt_of_lt_of_le ennreal.zero_lt_one _i.elim,
have hp₀' : 0 ≤ 1 / p.to_real := div_nonneg zero_le_one ennreal.to_real_nonneg,
have hp₀'' : 0 < p.to_real,
{ simpa [← ennreal.to_real_lt_to_real ennreal.zero_ne_top hp] using hp₀ },
-- It suffices to prove that scalar multiples of the indicator function of a finite-measure
-- measurable set can be approximated by continuous functions
suffices : ∀ (c : E) {s : set α} (hs : measurable_set s) (hμs : μ s < ⊤),
(Lp.simple_func.indicator_const p hs hμs.ne c : Lp E p μ)
∈ (bounded_continuous_function E p μ).topological_closure,
{ rw add_subgroup.eq_top_iff',
refine Lp.induction hp _ _ _ _,
{ exact this },
{ exact λ f g hf hg hfg', add_subgroup.add_mem _ },
{ exact add_subgroup.is_closed_topological_closure _ } },
-- Let `s` be a finite-measure measurable set, let's approximate `c` times its indicator function
intros c s hs hsμ,
refine mem_closure_iff_frequently.mpr _,
rw metric.nhds_basis_closed_ball.frequently_iff,
intros ε hε,
-- A little bit of pre-emptive work, to find `η : ℝ≥0` which will be a margin small enough for
-- our purposes
obtain ⟨η, hη_pos, hη_le⟩ : ∃ η, 0 < η ∧ (↑(∥bit0 (∥c∥)∥₊ * (2 * η) ^ (1 / p.to_real)) : ℝ) ≤ ε,
{ have : filter.tendsto (λ x : ℝ≥0, ∥bit0 (∥c∥)∥₊ * (2 * x) ^ (1 / p.to_real)) (𝓝 0) (𝓝 0),
{ have : filter.tendsto (λ x : ℝ≥0, 2 * x) (𝓝 0) (𝓝 (2 * 0)) := filter.tendsto_id.const_mul 2,
convert ((nnreal.continuous_at_rpow_const (or.inr hp₀')).tendsto.comp this).const_mul _,
simp [hp₀''.ne'] },
let ε' : ℝ≥0 := ⟨ε, hε.le⟩,
have hε' : 0 < ε' := by exact_mod_cast hε,
obtain ⟨δ, hδ, hδε'⟩ :=
nnreal.nhds_zero_basis.eventually_iff.mp (eventually_le_of_tendsto_lt hε' this),
obtain ⟨η, hη, hηδ⟩ := exists_between hδ,
refine ⟨η, hη, _⟩,
exact_mod_cast hδε' hηδ },
have hη_pos' : (0 : ℝ≥0∞) < ↑η := by exact_mod_cast hη_pos,
-- Use the regularity of the measure to `η`-approximate `s` by an open superset and a closed
-- subset
obtain ⟨u, u_open, su, μu⟩ : ∃ u, is_open u ∧ s ⊆ u ∧ μ u < μ s + ↑η,
{ refine hs.exists_is_open_lt_of_lt _ _,
simpa using (ennreal.add_lt_add_iff_left hsμ.ne).2 hη_pos' },
obtain ⟨F, F_closed, Fs, μF⟩ : ∃ F, is_closed F ∧ F ⊆ s ∧ μ s < μ F + ↑η :=
hs.exists_lt_is_closed_of_lt_top_of_pos hsμ hη_pos',
have : disjoint uᶜ F,
{ rw [set.disjoint_iff_inter_eq_empty, set.inter_comm, ← set.subset_compl_iff_disjoint],
simpa using Fs.trans su },
have h_μ_sdiff : μ (u \ F) ≤ 2 * η,
{ have hFμ : μ F < ⊤ := (measure_mono Fs).trans_lt hsμ,
refine ennreal.le_of_add_le_add_left hFμ _,
have : μ u < μ F + ↑η + ↑η,
{ refine μu.trans _,
rwa ennreal.add_lt_add_iff_right (ennreal.coe_ne_top : ↑η ≠ ⊤) },
convert this.le using 1,
{ rw [add_comm, ← measure_union, set.diff_union_of_subset (Fs.trans su)],
{ exact disjoint_sdiff_self_left },
{ exact (u_open.sdiff F_closed).measurable_set },
{ exact F_closed.measurable_set } },
have : (2:ℝ≥0∞) * η = η + η := by simpa using add_mul (1:ℝ≥0∞) 1 η,
rw this,
abel },
-- Apply Urysohn's lemma to get a continuous approximation to the characteristic function of
-- the set `s`
obtain ⟨g, hgu, hgF, hg_range⟩ :=
exists_continuous_zero_one_of_closed u_open.is_closed_compl F_closed this,
-- Multiply this by `c` to get a continuous approximation to the function `f`; the key point is
-- that this is pointwise bounded by the indicator of the set `u \ F`
have g_norm : ∀ x, ∥g x∥ = g x := λ x, by rw [real.norm_eq_abs, abs_of_nonneg (hg_range x).1],
have gc_bd : ∀ x, ∥g x • c - s.indicator (λ x, c) x∥ ≤ ∥(u \ F).indicator (λ x, bit0 ∥c∥) x∥,
{ intros x,
by_cases hu : x ∈ u,
{ rw ← set.diff_union_of_subset (Fs.trans su) at hu,
cases hu with hFu hF,
{ refine (norm_sub_le _ _).trans _,
refine (add_le_add_left (norm_indicator_le_norm_self (λ x, c) x) _).trans _,
have h₀ : g x * ∥c∥ + ∥c∥ ≤ 2 * ∥c∥,
{ nlinarith [(hg_range x).1, (hg_range x).2, norm_nonneg c] },
have h₁ : (2:ℝ) * ∥c∥ = bit0 (∥c∥) := by simpa using add_mul (1:ℝ) 1 (∥c∥),
simp [hFu, norm_smul, h₀, ← h₁, g_norm x] },
{ simp [hgF hF, Fs hF] } },
{ have : x ∉ s := λ h, hu (su h),
simp [hgu hu, this] } },
-- The rest is basically just `ennreal`-arithmetic
have gc_snorm : snorm ((λ x, g x • c) - s.indicator (λ x, c)) p μ
≤ (↑(∥bit0 (∥c∥)∥₊ * (2 * η) ^ (1 / p.to_real)) : ℝ≥0∞),
{ refine (snorm_mono_ae (filter.eventually_of_forall gc_bd)).trans _,
rw snorm_indicator_const (u_open.sdiff F_closed).measurable_set hp₀.ne' hp,
push_cast [← ennreal.coe_rpow_of_nonneg _ hp₀'],
exact ennreal.mul_left_mono (ennreal.rpow_left_monotone_of_nonneg hp₀' h_μ_sdiff) },
have gc_cont : continuous (λ x, g x • c) := g.continuous.smul continuous_const,
have gc_mem_ℒp : mem_ℒp (λ x, g x • c) p μ,
{ have : mem_ℒp ((λ x, g x • c) - s.indicator (λ x, c)) p μ :=
⟨(gc_cont.ae_measurable μ).sub (measurable_const.indicator hs).ae_measurable,
gc_snorm.trans_lt ennreal.coe_lt_top⟩,
simpa using this.add (mem_ℒp_indicator_const p hs c (or.inr hsμ.ne)) },
refine ⟨gc_mem_ℒp.to_Lp _, _, _⟩,
{ rw mem_closed_ball_iff_norm,
refine le_trans _ hη_le,
rw [simple_func.coe_indicator_const, indicator_const_Lp, ← mem_ℒp.to_Lp_sub, Lp.norm_to_Lp],
exact ennreal.to_real_le_coe_of_le_coe gc_snorm },
{ rw [set_like.mem_coe, mem_bounded_continuous_function_iff],
refine ⟨bounded_continuous_function.of_normed_group _ gc_cont (∥c∥) _, rfl⟩,
intros x,
have h₀ : g x * ∥c∥ ≤ ∥c∥,
{ nlinarith [(hg_range x).1, (hg_range x).2, norm_nonneg c] },
simp [norm_smul, g_norm x, h₀] },
end
end measure_theory.Lp
variables (𝕜 : Type*) [measurable_space 𝕜] [normed_field 𝕜] [opens_measurable_space 𝕜]
[normed_algebra ℝ 𝕜] [normed_space 𝕜 E]
namespace bounded_continuous_function
lemma to_Lp_dense_range [μ.weakly_regular] [is_finite_measure μ] :
dense_range ⇑(to_Lp p μ 𝕜 : (α →ᵇ E) →L[𝕜] Lp E p μ) :=
begin
haveI : normed_space ℝ E := restrict_scalars.normed_space ℝ 𝕜 E,
rw dense_range_iff_closure_range,
suffices : (to_Lp p μ 𝕜 : _ →L[𝕜] Lp E p μ).range.to_add_subgroup.topological_closure = ⊤,
{ exact congr_arg coe this },
simp [range_to_Lp p μ, measure_theory.Lp.bounded_continuous_function_dense E hp],
end
end bounded_continuous_function
namespace continuous_map
lemma to_Lp_dense_range [compact_space α] [μ.weakly_regular] [is_finite_measure μ] :
dense_range ⇑(to_Lp p μ 𝕜 : C(α, E) →L[𝕜] Lp E p μ) :=
begin
haveI : normed_space ℝ E := restrict_scalars.normed_space ℝ 𝕜 E,
rw dense_range_iff_closure_range,
suffices : (to_Lp p μ 𝕜 : _ →L[𝕜] Lp E p μ).range.to_add_subgroup.topological_closure = ⊤,
{ exact congr_arg coe this },
simp [range_to_Lp p μ, measure_theory.Lp.bounded_continuous_function_dense E hp]
end
end continuous_map
|
5c535ff76ee60430e1c82365284e9bb3bd4595ea | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/category_theory/limits/shapes/types.lean | a9c6840205d6777912f793162fa101f7a57cbc4d | [
"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 | 13,187 | 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 category_theory.limits.types
import category_theory.limits.shapes.products
import category_theory.limits.shapes.binary_products
import category_theory.limits.shapes.terminal
/-!
# Special shapes for limits in `Type`.
The general shape (co)limits defined in `category_theory.limits.types`
are intended for use through the limits API,
and the actual implementation should mostly be considered "sealed".
In this file, we provide definitions of the "standard" special shapes of limits in `Type`,
giving the expected definitional implementation:
* the terminal object is `punit`
* the binary product of `X` and `Y` is `X × Y`
* the product of a family `f : J → Type` is `Π j, f j`
* the coproduct of a family `f : J → Type` is `Σ j, f j`
* the binary coproduct of `X` and `Y` is the sum type `X ⊕ Y`
* the equalizer of a pair of maps `(g, h)` is the subtype `{x : Y // g x = h x}`
* the coequalizer of a pair of maps `(f, g)` is the quotient of `Y` by `∀ x : Y, f x ~ g x`
* the pullback of `f : X ⟶ Z` and `g : Y ⟶ Z` is the subtype `{ p : X × Y // f p.1 = g p.2 }`
of the product
Because these are not intended for use with the `has_limit` API,
we instead construct terms of `limit_data`.
As an example, when setting up the monoidal category structure on `Type`
we use the `types_has_terminal` and `types_has_binary_products` instances.
-/
universes u
open category_theory
open category_theory.limits
namespace category_theory.limits.types
/-- A restatement of `types.lift_π_apply` that uses `pi.π` and `pi.lift`. -/
@[simp]
lemma pi_lift_π_apply
{β : Type u} (f : β → Type u) {P : Type u} (s : Π b, P ⟶ f b) (b : β) (x : P) :
(pi.π f b : (∏ f) → f b) (@pi.lift β _ _ f _ P s x) = s b x :=
congr_fun (limit.lift_π (fan.mk P s) b) x
/-- A restatement of `types.map_π_apply` that uses `pi.π` and `pi.map`. -/
@[simp]
lemma pi_map_π_apply {β : Type u} {f g : β → Type u} (α : Π j, f j ⟶ g j) (b : β) (x) :
(pi.π g b : (∏ g) → g b) (pi.map α x) = α b ((pi.π f b : (∏ f) → f b) x) :=
limit.map_π_apply _ _ _
/-- The category of types has `punit` as a terminal object. -/
def terminal_limit_cone : limits.limit_cone (functor.empty (Type u)) :=
{ cone :=
{ X := punit,
π := by tidy, },
is_limit := by tidy, }
/-- The category of types has `pempty` as an initial object. -/
def initial_limit_cone : limits.colimit_cocone (functor.empty (Type u)) :=
{ cocone :=
{ X := pempty,
ι := by tidy, },
is_colimit := by tidy, }
open category_theory.limits.walking_pair
/-- The product type `X × Y` forms a cone for the binary product of `X` and `Y`. -/
-- We manually generate the other projection lemmas since the simp-normal form for the legs is
-- otherwise not created correctly.
@[simps X]
def binary_product_cone (X Y : Type u) : binary_fan X Y :=
binary_fan.mk prod.fst prod.snd
@[simp]
lemma binary_product_cone_fst (X Y : Type u) :
(binary_product_cone X Y).fst = prod.fst :=
rfl
@[simp]
lemma binary_product_cone_snd (X Y : Type u) :
(binary_product_cone X Y).snd = prod.snd :=
rfl
/-- The product type `X × Y` is a binary product for `X` and `Y`. -/
@[simps]
def binary_product_limit (X Y : Type u) : is_limit (binary_product_cone X Y) :=
{ lift := λ (s : binary_fan X Y) x, (s.fst x, s.snd x),
fac' := λ s j, walking_pair.cases_on j rfl rfl,
uniq' := λ s m w, funext $ λ x, prod.ext (congr_fun (w left) x) (congr_fun (w right) x) }
/--
The category of types has `X × Y`, the usual cartesian product,
as the binary product of `X` and `Y`.
-/
@[simps]
def binary_product_limit_cone (X Y : Type u) : limits.limit_cone (pair X Y) :=
⟨_, binary_product_limit X Y⟩
/-- The functor which sends `X, Y` to the product type `X × Y`. -/
-- We add the option `type_md` to tell `@[simps]` to not treat homomorphisms `X ⟶ Y` in `Type*` as
-- a function type
@[simps {type_md := reducible}]
def binary_product_functor : Type u ⥤ Type u ⥤ Type u :=
{ obj := λ X,
{ obj := λ Y, X × Y,
map := λ Y₁ Y₂ f, (binary_product_limit X Y₂).lift (binary_fan.mk prod.fst (prod.snd ≫ f)) },
map := λ X₁ X₂ f,
{ app := λ Y, (binary_product_limit X₂ Y).lift (binary_fan.mk (prod.fst ≫ f) prod.snd) } }
/--
The product functor given by the instance `has_binary_products (Type u)` is isomorphic to the
explicit binary product functor given by the product type.
-/
noncomputable def binary_product_iso_prod : binary_product_functor ≅ (prod.functor : Type u ⥤ _) :=
begin
apply nat_iso.of_components (λ X, _) _,
{ apply nat_iso.of_components (λ Y, _) _,
{ exact ((limit.is_limit _).cone_point_unique_up_to_iso (binary_product_limit X Y)).symm },
{ intros Y₁ Y₂ f,
ext1;
simp } },
{ intros X₁ X₂ g,
ext : 3;
simp }
end
/-- The sum type `X ⊕ Y` forms a cocone for the binary coproduct of `X` and `Y`. -/
@[simps]
def binary_coproduct_cocone (X Y : Type u) : cocone (pair X Y) :=
binary_cofan.mk sum.inl sum.inr
/-- The sum type `X ⊕ Y` is a binary coproduct for `X` and `Y`. -/
@[simps]
def binary_coproduct_colimit (X Y : Type u) : is_colimit (binary_coproduct_cocone X Y) :=
{ desc := λ (s : binary_cofan X Y), sum.elim s.inl s.inr,
fac' := λ s j, walking_pair.cases_on j rfl rfl,
uniq' := λ s m w, funext $ λ x, sum.cases_on x (congr_fun (w left)) (congr_fun (w right)) }
/--
The category of types has `X ⊕ Y`,
as the binary coproduct of `X` and `Y`.
-/
def binary_coproduct_colimit_cocone (X Y : Type u) : limits.colimit_cocone (pair X Y) :=
⟨_, binary_coproduct_colimit X Y⟩
/--
The category of types has `Π j, f j` as the product of a type family `f : J → Type`.
-/
def product_limit_cone {J : Type u} (F : J → Type u) : limits.limit_cone (discrete.functor F) :=
{ cone :=
{ X := Π j, F j,
π := { app := λ j f, f j }, },
is_limit :=
{ lift := λ s x j, s.π.app j x,
uniq' := λ s m w, funext $ λ x, funext $ λ j, (congr_fun (w j) x : _) } }
/--
The category of types has `Σ j, f j` as the coproduct of a type family `f : J → Type`.
-/
def coproduct_colimit_cocone {J : Type u} (F : J → Type u) :
limits.colimit_cocone (discrete.functor F) :=
{ cocone :=
{ X := Σ j, F j,
ι :=
{ app := λ j x, ⟨j, x⟩ }, },
is_colimit :=
{ desc := λ s x, s.ι.app x.1 x.2,
uniq' := λ s m w,
begin
ext ⟨j, x⟩,
have := congr_fun (w j) x,
exact this,
end }, }
section fork
variables {X Y Z : Type u} (f : X ⟶ Y) {g h : Y ⟶ Z} (w : f ≫ g = f ≫ h)
/--
Show the given fork in `Type u` is an equalizer given that any element in the "difference kernel"
comes from `X`.
The converse of `unique_of_type_equalizer`.
-/
noncomputable def type_equalizer_of_unique (t : ∀ (y : Y), g y = h y → ∃! (x : X), f x = y) :
is_limit (fork.of_ι _ w) :=
fork.is_limit.mk' _ $ λ s,
begin
refine ⟨λ i, _, _, _⟩,
{ apply classical.some (t (s.ι i) _),
apply congr_fun s.condition i },
{ ext i,
apply (classical.some_spec (t (s.ι i) _)).1 },
{ intros m hm,
ext i,
apply (classical.some_spec (t (s.ι i) _)).2,
apply congr_fun hm i },
end
/-- The converse of `type_equalizer_of_unique`. -/
lemma unique_of_type_equalizer (t : is_limit (fork.of_ι _ w)) (y : Y) (hy : g y = h y) :
∃! (x : X), f x = y :=
begin
let y' : punit ⟶ Y := λ _, y,
have hy' : y' ≫ g = y' ≫ h := funext (λ _, hy),
refine ⟨(fork.is_limit.lift' t _ hy').1 ⟨⟩, congr_fun (fork.is_limit.lift' t y' _).2 ⟨⟩, _⟩,
intros x' hx',
suffices : (λ (_ : punit), x') = (fork.is_limit.lift' t y' hy').1,
rw ← this,
apply fork.is_limit.hom_ext t,
ext ⟨⟩,
apply hx'.trans (congr_fun (fork.is_limit.lift' t _ hy').2 ⟨⟩).symm,
end
lemma type_equalizer_iff_unique :
nonempty (is_limit (fork.of_ι _ w)) ↔ (∀ (y : Y), g y = h y → ∃! (x : X), f x = y) :=
⟨λ i, unique_of_type_equalizer _ _ (classical.choice i), λ k, ⟨type_equalizer_of_unique f w k⟩⟩
/-- Show that the subtype `{x : Y // g x = h x}` is an equalizer for the pair `(g,h)`. -/
def equalizer_limit : limits.limit_cone (parallel_pair g h) :=
{ cone := fork.of_ι (subtype.val : {x : Y // g x = h x} → Y) (funext subtype.prop),
is_limit := fork.is_limit.mk' _ $ λ s,
⟨λ i, ⟨s.ι i, by apply congr_fun s.condition i⟩,
rfl,
λ m hm, funext $ λ x, subtype.ext (congr_fun hm x)⟩ }
end fork
section cofork
variables {X Y Z : Type u} (f g : X ⟶ Y)
/-- (Implementation) The relation to be quotiented to obtain the coequalizer. -/
inductive coequalizer_rel : Y → Y → Prop
| rel (x : X) : coequalizer_rel (f x) (g x)
/--
Show that the quotient by the relation generated by `f(x) ~ g(x)`
is a coequalizer for the pair `(f, g)`.
-/
def coequalizer_colimit : limits.colimit_cocone (parallel_pair f g) :=
{ cocone := cofork.of_π (quot.mk (coequalizer_rel f g))
(funext (λ x, quot.sound (coequalizer_rel.rel x))),
is_colimit := cofork.is_colimit.mk' _ $ λ s,
⟨ quot.lift s.π (λ a b (h : coequalizer_rel f g a b),
by { cases h, exact congr_fun s.condition h_1 }),
rfl,
λ m hm, funext $ λ x, quot.induction_on x (congr_fun hm : _) ⟩ }
/-- If `π : Y ⟶ Z` is an equalizer for `(f, g)`, and `U ⊆ Y` such that `f ⁻¹' U = g ⁻¹' U`,
then `π ⁻¹' (π '' U) = U`.
-/
lemma coequalizer_preimage_image_eq_of_preimage_eq (π : Y ⟶ Z)
(e : f ≫ π = g ≫ π) (h : is_colimit (cofork.of_π π e)) (U : set Y) (H : f ⁻¹' U = g ⁻¹' U) :
π ⁻¹' (π '' U) = U :=
begin
have lem : ∀ x y, (coequalizer_rel f g x y) → (x ∈ U ↔ y ∈ U),
{ rintros _ _ ⟨x⟩, change x ∈ f ⁻¹' U ↔ x ∈ g ⁻¹' U, congr' 2 },
have eqv : _root_.equivalence (λ x y, x ∈ U ↔ y ∈ U) := by tidy,
ext,
split,
{ rw ← (show _ = π, from h.comp_cocone_point_unique_up_to_iso_inv
(coequalizer_colimit f g).2 walking_parallel_pair.one),
rintro ⟨y, hy, e'⟩,
dsimp at e',
replace e' := (mono_iff_injective (h.cocone_point_unique_up_to_iso
(coequalizer_colimit f g).is_colimit).inv).mp infer_instance e',
exact (eqv.eqv_gen_iff.mp (eqv_gen.mono lem (quot.exact _ e'))).mp hy },
{ exact λ hx, ⟨x, hx, rfl⟩ }
end
end cofork
section pullback
open category_theory.limits.walking_pair
open category_theory.limits.walking_cospan
open category_theory.limits.walking_cospan.hom
variables {W X Y Z : Type u}
variables (f : X ⟶ Z) (g : Y ⟶ Z)
/--
The usual explicit pullback in the category of types, as a subtype of the product.
The full `limit_cone` data is bundled as `pullback_limit_cone f g`.
-/
@[nolint has_inhabited_instance]
abbreviation pullback_obj : Type u := { p : X × Y // f p.1 = g p.2 }
-- `pullback_obj f g` comes with a coercion to the product type `X × Y`.
example (p : pullback_obj f g) : X × Y := p
/--
The explicit pullback cone on `pullback_obj f g`.
This is bundled with the `is_limit` data as `pullback_limit_cone f g`.
-/
abbreviation pullback_cone : limits.pullback_cone f g :=
pullback_cone.mk (λ p : pullback_obj f g, p.1.1) (λ p, p.1.2) (funext (λ p, p.2))
/--
The explicit pullback in the category of types, bundled up as a `limit_cone`
for given `f` and `g`.
-/
@[simps]
def pullback_limit_cone (f : X ⟶ Z) (g : Y ⟶ Z) : limits.limit_cone (cospan f g) :=
{ cone := pullback_cone f g,
is_limit := pullback_cone.is_limit_aux _
(λ s x, ⟨⟨s.fst x, s.snd x⟩, congr_fun s.condition x⟩)
(by tidy)
(by tidy)
(λ s m w, funext $ λ x, subtype.ext $
prod.ext (congr_fun (w walking_cospan.left) x)
(congr_fun (w walking_cospan.right) x)) }
/--
The pullback cone given by the instance `has_pullbacks (Type u)` is isomorphic to the
explicit pullback cone given by `pullback_limit_cone`.
-/
noncomputable def pullback_cone_iso_pullback : limit.cone (cospan f g) ≅ pullback_cone f g :=
(limit.is_limit _).unique_up_to_iso (pullback_limit_cone f g).is_limit
/--
The pullback given by the instance `has_pullbacks (Type u)` is isomorphic to the
explicit pullback object given by `pullback_limit_obj`.
-/
noncomputable def pullback_iso_pullback : pullback f g ≅ pullback_obj f g :=
(cones.forget _).map_iso $ pullback_cone_iso_pullback f g
@[simp] lemma pullback_iso_pullback_hom_fst (p : pullback f g) :
((pullback_iso_pullback f g).hom p : X × Y).fst = (pullback.fst : _ ⟶ X) p :=
congr_fun ((pullback_cone_iso_pullback f g).hom.w left) p
@[simp] lemma pullback_iso_pullback_hom_snd (p : pullback f g) :
((pullback_iso_pullback f g).hom p : X × Y).snd = (pullback.snd : _ ⟶ Y) p :=
congr_fun ((pullback_cone_iso_pullback f g).hom.w right) p
@[simp] lemma pullback_iso_pullback_inv_fst :
(pullback_iso_pullback f g).inv ≫ pullback.fst = (λ p, (p : X × Y).fst) :=
(pullback_cone_iso_pullback f g).inv.w left
@[simp] lemma pullback_iso_pullback_inv_snd :
(pullback_iso_pullback f g).inv ≫ pullback.snd = (λ p, (p : X × Y).snd) :=
(pullback_cone_iso_pullback f g).inv.w right
end pullback
end category_theory.limits.types
|
baf8770c54898ca1a7c785cc57c9a150d76ea7ff | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/order/min_max.lean | 0656e40716655ed582c6793bee911089a736062b | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,253 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import order.lattice
/-!
# `max` and `min`
This file proves basic properties about maxima and minima on a `linear_order`.
## Tags
min, max
-/
universes u v
variables {α : Type u} {β : Type v}
attribute [simp] max_eq_left max_eq_right min_eq_left min_eq_right
section
variables [linear_order α] [linear_order β] {f : α → β} {s : set α} {a b c d : α}
-- translate from lattices to linear orders (sup → max, inf → min)
@[simp] lemma le_min_iff : c ≤ min a b ↔ c ≤ a ∧ c ≤ b := le_inf_iff
@[simp] lemma max_le_iff : max a b ≤ c ↔ a ≤ c ∧ b ≤ c := sup_le_iff
lemma max_le_max : a ≤ c → b ≤ d → max a b ≤ max c d := sup_le_sup
lemma min_le_min : a ≤ c → b ≤ d → min a b ≤ min c d := inf_le_inf
lemma le_max_of_le_left : a ≤ b → a ≤ max b c := le_sup_of_le_left
lemma le_max_of_le_right : a ≤ c → a ≤ max b c := le_sup_of_le_right
lemma lt_max_of_lt_left (h : a < b) : a < max b c := h.trans_le (le_max_left b c)
lemma lt_max_of_lt_right (h : a < c) : a < max b c := h.trans_le (le_max_right b c)
lemma min_le_of_left_le : a ≤ c → min a b ≤ c := inf_le_of_left_le
lemma min_le_of_right_le : b ≤ c → min a b ≤ c := inf_le_of_right_le
lemma min_lt_of_left_lt (h : a < c) : min a b < c := (min_le_left a b).trans_lt h
lemma min_lt_of_right_lt (h : b < c) : min a b < c := (min_le_right a b).trans_lt h
lemma max_min_distrib_left : max a (min b c) = min (max a b) (max a c) := sup_inf_left
lemma max_min_distrib_right : max (min a b) c = min (max a c) (max b c) := sup_inf_right
lemma min_max_distrib_left : min a (max b c) = max (min a b) (min a c) := inf_sup_left
lemma min_max_distrib_right : min (max a b) c = max (min a c) (min b c) := inf_sup_right
lemma min_le_max : min a b ≤ max a b := le_trans (min_le_left a b) (le_max_left a b)
@[simp] lemma min_eq_left_iff : min a b = a ↔ a ≤ b := inf_eq_left
@[simp] lemma min_eq_right_iff : min a b = b ↔ b ≤ a := inf_eq_right
@[simp] lemma max_eq_left_iff : max a b = a ↔ b ≤ a := sup_eq_left
@[simp] lemma max_eq_right_iff : max a b = b ↔ a ≤ b := sup_eq_right
/-- For elements `a` and `b` of a linear order, either `min a b = a` and `a ≤ b`,
or `min a b = b` and `b < a`.
Use cases on this lemma to automate linarith in inequalities -/
lemma min_cases (a b : α) : min a b = a ∧ a ≤ b ∨ min a b = b ∧ b < a :=
begin
by_cases a ≤ b,
{ left,
exact ⟨min_eq_left h, h⟩ },
{ right,
exact ⟨min_eq_right (le_of_lt (not_le.mp h)), (not_le.mp h)⟩ }
end
/-- For elements `a` and `b` of a linear order, either `max a b = a` and `b ≤ a`,
or `max a b = b` and `a < b`.
Use cases on this lemma to automate linarith in inequalities -/
lemma max_cases (a b : α) : max a b = a ∧ b ≤ a ∨ max a b = b ∧ a < b :=
@min_cases (order_dual α) _ a b
lemma min_eq_iff : min a b = c ↔ a = c ∧ a ≤ b ∨ b = c ∧ b ≤ a :=
begin
split,
{ intro h,
refine or.imp (λ h', _) (λ h', _) (le_total a b);
exact ⟨by simpa [h'] using h, h'⟩ },
{ rintro (⟨rfl, h⟩|⟨rfl, h⟩);
simp [h] }
end
lemma max_eq_iff : max a b = c ↔ a = c ∧ b ≤ a ∨ b = c ∧ a ≤ b :=
@min_eq_iff (order_dual α) _ a b c
/-- An instance asserting that `max a a = a` -/
instance max_idem : is_idempotent α max := by apply_instance -- short-circuit type class inference
/-- An instance asserting that `min a a = a` -/
instance min_idem : is_idempotent α min := by apply_instance -- short-circuit type class inference
@[simp] lemma max_lt_iff : max a b < c ↔ (a < c ∧ b < c) :=
sup_lt_iff
@[simp] lemma lt_min_iff : a < min b c ↔ (a < b ∧ a < c) :=
lt_inf_iff
@[simp] lemma lt_max_iff : a < max b c ↔ a < b ∨ a < c :=
lt_sup_iff
@[simp] lemma min_lt_iff : min a b < c ↔ a < c ∨ b < c :=
@lt_max_iff (order_dual α) _ _ _ _
@[simp] lemma min_le_iff : min a b ≤ c ↔ a ≤ c ∨ b ≤ c :=
inf_le_iff
@[simp] lemma le_max_iff : a ≤ max b c ↔ a ≤ b ∨ a ≤ c :=
@min_le_iff (order_dual α) _ _ _ _
lemma max_lt_max (h₁ : a < c) (h₂ : b < d) : max a b < max c d :=
by simp [lt_max_iff, max_lt_iff, *]
lemma min_lt_min (h₁ : a < c) (h₂ : b < d) : min a b < min c d :=
@max_lt_max (order_dual α) _ _ _ _ _ h₁ h₂
theorem min_right_comm (a b c : α) : min (min a b) c = min (min a c) b :=
right_comm min min_comm min_assoc a b c
theorem max.left_comm (a b c : α) : max a (max b c) = max b (max a c) :=
left_comm max max_comm max_assoc a b c
theorem max.right_comm (a b c : α) : max (max a b) c = max (max a c) b :=
right_comm max max_comm max_assoc a b c
lemma monotone_on.map_max (hf : monotone_on f s) (ha : a ∈ s) (hb : b ∈ s) :
f (max a b) = max (f a) (f b) :=
by cases le_total a b; simp only [max_eq_right, max_eq_left, hf ha hb, hf hb ha, h]
lemma monotone_on.map_min (hf : monotone_on f s) (ha : a ∈ s) (hb : b ∈ s) :
f (min a b) = min (f a) (f b) :=
hf.dual.map_max ha hb
lemma antitone_on.map_max (hf : antitone_on f s) (ha : a ∈ s) (hb : b ∈ s) :
f (max a b) = min (f a) (f b) :=
hf.dual_right.map_max ha hb
lemma antitone_on.map_min (hf : antitone_on f s) (ha : a ∈ s) (hb : b ∈ s) :
f (min a b) = max (f a) (f b) :=
hf.dual.map_max ha hb
lemma monotone.map_max (hf : monotone f) : f (max a b) = max (f a) (f b) :=
by cases le_total a b; simp [h, hf h]
lemma monotone.map_min (hf : monotone f) : f (min a b) = min (f a) (f b) :=
hf.dual.map_max
lemma antitone.map_max (hf : antitone f) : f (max a b) = min (f a) (f b) :=
by cases le_total a b; simp [h, hf h]
lemma antitone.map_min (hf : antitone f) : f (min a b) = max (f a) (f b) :=
hf.dual.map_max
lemma min_rec {p : α → Prop} {x y : α} (hx : x ≤ y → p x) (hy : y ≤ x → p y) : p (min x y) :=
(le_total x y).rec (λ h, (min_eq_left h).symm.subst (hx h))
(λ h, (min_eq_right h).symm.subst (hy h))
lemma max_rec {p : α → Prop} {x y : α} (hx : y ≤ x → p x) (hy : x ≤ y → p y) : p (max x y) :=
@min_rec (order_dual α) _ _ _ _ hx hy
lemma min_rec' (p : α → Prop) {x y : α} (hx : p x) (hy : p y) : p (min x y) :=
min_rec (λ _, hx) (λ _, hy)
lemma max_rec' (p : α → Prop) {x y : α} (hx : p x) (hy : p y) : p (max x y) :=
max_rec (λ _, hx) (λ _, hy)
theorem min_choice (a b : α) : min a b = a ∨ min a b = b :=
by cases le_total a b; simp *
theorem max_choice (a b : α) : max a b = a ∨ max a b = b :=
@min_choice (order_dual α) _ a b
lemma le_of_max_le_left {a b c : α} (h : max a b ≤ c) : a ≤ c :=
le_trans (le_max_left _ _) h
lemma le_of_max_le_right {a b c : α} (h : max a b ≤ c) : b ≤ c :=
le_trans (le_max_right _ _) h
lemma max_commutative : commutative (max : α → α → α) :=
max_comm
lemma max_associative : associative (max : α → α → α) :=
max_assoc
lemma max_left_commutative : left_commutative (max : α → α → α) :=
max_left_comm
lemma min_commutative : commutative (min : α → α → α) :=
min_comm
lemma min_associative : associative (min : α → α → α) :=
min_assoc
lemma min_left_commutative : left_commutative (min : α → α → α) :=
min_left_comm
end
|
379c1071e2f94a37ddbd24a436d2549a3e24aaa4 | d436468d80b739ba7e06843c4d0d2070e43448e5 | /src/category_theory/limits/shapes/binary_products.lean | c83ce9b82c9c18dcca914b1c8770aaf51c555d3e | [
"Apache-2.0"
] | permissive | roro47/mathlib | 761fdc002aef92f77818f3fef06bf6ec6fc1a28e | 80aa7d52537571a2ca62a3fdf71c9533a09422cf | refs/heads/master | 1,599,656,410,625 | 1,573,649,488,000 | 1,573,649,488,000 | 221,452,951 | 0 | 0 | Apache-2.0 | 1,573,647,693,000 | 1,573,647,692,000 | null | UTF-8 | Lean | false | false | 9,116 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.limits.shapes.finite_products
import category_theory.limits.shapes.terminal
import category_theory.discrete_category
/-!
# Binary (co)products
We define a category `walking_pair`, which is the index category
for a binary (co)product diagram. A convenience method `pair X Y`
constructs the functor from the walking pair, hitting the given objects.
We define `prod X Y` and `coprod X Y` as limits and colimits of such functors.
Typeclasses `has_binary_products` and `has_binary_coproducts` assert the existence
of (co)limits shaped as walking pairs.
-/
universes v u
open category_theory
namespace category_theory.limits
/-- The type of objects for the diagram indexing a binary (co)product. -/
@[derive decidable_eq]
inductive walking_pair : Type v
| left | right
instance fintype_walking_pair : fintype walking_pair :=
{ elems := [walking_pair.left, walking_pair.right].to_finset,
complete := λ x, by { cases x; simp } }
def pair_function {C : Type u} (X Y : C) : walking_pair → C
| walking_pair.left := X
| walking_pair.right := Y
variables {C : Type u} [𝒞 : category.{v} C]
include 𝒞
def pair (X Y : C) : discrete walking_pair ⥤ C :=
functor.of_function (pair_function X Y)
@[simp] lemma pair_obj_left (X Y : C) : (pair X Y).obj walking_pair.left = X := rfl
@[simp] lemma pair_obj_right (X Y : C) : (pair X Y).obj walking_pair.right = Y := rfl
def map_pair {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) : pair W X ⟶ pair Y Z :=
{ app := λ j, match j with
| walking_pair.left := f
| walking_pair.right := g
end }
@[simp] lemma map_pair_left {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) : (map_pair f g).app walking_pair.left = f := rfl
@[simp] lemma map_pair_right {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) : (map_pair f g).app walking_pair.right = g := rfl
abbreviation binary_fan (X Y : C) := cone (pair X Y)
abbreviation binary_cofan (X Y : C) := cocone (pair X Y)
variables {X Y : C}
def binary_fan.mk {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : binary_fan X Y :=
{ X := P,
π := { app := λ j, walking_pair.cases_on j π₁ π₂ }}
def binary_cofan.mk {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : binary_cofan X Y :=
{ X := P,
ι := { app := λ j, walking_pair.cases_on j ι₁ ι₂ }}
@[simp] lemma binary_fan.mk_π_app_left {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) :
(binary_fan.mk π₁ π₂).π.app walking_pair.left = π₁ := rfl
@[simp] lemma binary_fan.mk_π_app_right {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) :
(binary_fan.mk π₁ π₂).π.app walking_pair.right = π₂ := rfl
@[simp] lemma binary_cofan.mk_π_app_left {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) :
(binary_cofan.mk ι₁ ι₂).ι.app walking_pair.left = ι₁ := rfl
@[simp] lemma binary_cofan.mk_π_app_right {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) :
(binary_cofan.mk ι₁ ι₂).ι.app walking_pair.right = ι₂ := rfl
abbreviation prod (X Y : C) [has_limit (pair X Y)] := limit (pair X Y)
abbreviation coprod (X Y : C) [has_colimit (pair X Y)] := colimit (pair X Y)
notation X `⨯`:20 Y:20 := prod X Y
notation X `⨿`:20 Y:20 := coprod X Y
abbreviation prod.fst {X Y : C} [has_limit (pair X Y)] : X ⨯ Y ⟶ X :=
limit.π (pair X Y) walking_pair.left
abbreviation prod.snd {X Y : C} [has_limit (pair X Y)] : X ⨯ Y ⟶ Y :=
limit.π (pair X Y) walking_pair.right
abbreviation coprod.inl {X Y : C} [has_colimit (pair X Y)] : X ⟶ X ⨿ Y :=
colimit.ι (pair X Y) walking_pair.left
abbreviation coprod.inr {X Y : C} [has_colimit (pair X Y)] : Y ⟶ X ⨿ Y :=
colimit.ι (pair X Y) walking_pair.right
abbreviation prod.lift {W X Y : C} [has_limit (pair X Y)] (f : W ⟶ X) (g : W ⟶ Y) : W ⟶ X ⨯ Y :=
limit.lift _ (binary_fan.mk f g)
abbreviation coprod.desc {W X Y : C} [has_colimit (pair X Y)] (f : X ⟶ W) (g : Y ⟶ W) : X ⨿ Y ⟶ W :=
colimit.desc _ (binary_cofan.mk f g)
abbreviation prod.map {W X Y Z : C} [has_limits_of_shape.{v} (discrete walking_pair) C]
(f : W ⟶ Y) (g : X ⟶ Z) : W ⨯ X ⟶ Y ⨯ Z :=
lim.map (map_pair f g)
abbreviation coprod.map {W X Y Z : C} [has_colimits_of_shape.{v} (discrete walking_pair) C]
(f : W ⟶ Y) (g : X ⟶ Z) : W ⨿ X ⟶ Y ⨿ Z :=
colim.map (map_pair f g)
variables (C)
class has_binary_products :=
(has_limits_of_shape : has_limits_of_shape.{v} (discrete walking_pair) C)
class has_binary_coproducts :=
(has_colimits_of_shape : has_colimits_of_shape.{v} (discrete walking_pair) C)
attribute [instance] has_binary_products.has_limits_of_shape has_binary_coproducts.has_colimits_of_shape
instance [has_finite_products.{v} C] : has_binary_products.{v} C :=
{ has_limits_of_shape := by apply_instance }
instance [has_finite_coproducts.{v} C] : has_binary_coproducts.{v} C :=
{ has_colimits_of_shape := by apply_instance }
section
variables {C} [has_binary_products.{v} C]
local attribute [tidy] tactic.case_bash
/-- The braiding isomorphism which swaps a binary product. -/
@[simps] def prod.braiding (P Q : C) : P ⨯ Q ≅ Q ⨯ P :=
{ hom := prod.lift prod.snd prod.fst,
inv := prod.lift prod.snd prod.fst }
/-- The braiding isomorphism is symmetric. -/
@[simp] lemma prod.symmetry (P Q : C) :
(prod.braiding P Q).hom ≫ (prod.braiding Q P).hom = 𝟙 _ :=
by tidy
/-- The associator isomorphism for binary products. -/
@[simps] def prod.associator
(P Q R : C) : (P ⨯ Q) ⨯ R ≅ P ⨯ (Q ⨯ R) :=
{ hom :=
prod.lift
(prod.fst ≫ prod.fst)
(prod.lift (prod.fst ≫ prod.snd) prod.snd),
inv :=
prod.lift
(prod.lift prod.fst (prod.snd ≫ prod.fst))
(prod.snd ≫ prod.snd) }
lemma prod.pentagon (W X Y Z : C) :
prod.map ((prod.associator W X Y).hom) (𝟙 Z) ≫
(prod.associator W (X ⨯ Y) Z).hom ≫ prod.map (𝟙 W) ((prod.associator X Y Z).hom) =
(prod.associator (W ⨯ X) Y Z).hom ≫ (prod.associator W X (Y⨯Z)).hom :=
by tidy
lemma prod.associator_naturality {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃) :
prod.map (prod.map f₁ f₂) f₃ ≫ (prod.associator Y₁ Y₂ Y₃).hom =
(prod.associator X₁ X₂ X₃).hom ≫ prod.map f₁ (prod.map f₂ f₃) :=
by tidy
variables [has_terminal.{v} C]
/-- The left unitor isomorphism for binary products with the terminal object. -/
@[simps] def prod.left_unitor
(P : C) : ⊤_ C ⨯ P ≅ P :=
{ hom := prod.snd,
inv := prod.lift (terminal.from P) (𝟙 _) }
/-- The right unitor isomorphism for binary products with the terminal object. -/
@[simps] def prod.right_unitor
(P : C) : P ⨯ ⊤_ C ≅ P :=
{ hom := prod.fst,
inv := prod.lift (𝟙 _) (terminal.from P) }
lemma prod.triangle (X Y : C) :
(prod.associator X (⊤_ C) Y).hom ≫ prod.map (𝟙 X) ((prod.left_unitor Y).hom) =
prod.map ((prod.right_unitor X).hom) (𝟙 Y) :=
by tidy
end
section
variables {C} [has_binary_coproducts.{v} C]
local attribute [tidy] tactic.case_bash
/-- The braiding isomorphism which swaps a binary coproduct. -/
@[simps] def coprod.braiding (P Q : C) : P ⨿ Q ≅ Q ⨿ P :=
{ hom := coprod.desc coprod.inr coprod.inl,
inv := coprod.desc coprod.inr coprod.inl }
/-- The braiding isomorphism is symmetric. -/
@[simp] lemma coprod.symmetry (P Q : C) :
(coprod.braiding P Q).hom ≫ (coprod.braiding Q P).hom = 𝟙 _ :=
by tidy
/-- The associator isomorphism for binary coproducts. -/
@[simps] def coprod.associator
(P Q R : C) : (P ⨿ Q) ⨿ R ≅ P ⨿ (Q ⨿ R) :=
{ hom :=
coprod.desc
(coprod.desc coprod.inl (coprod.inl ≫ coprod.inr))
(coprod.inr ≫ coprod.inr),
inv :=
coprod.desc
(coprod.inl ≫ coprod.inl)
(coprod.desc (coprod.inr ≫ coprod.inl) coprod.inr) }
lemma coprod.pentagon (W X Y Z : C) :
coprod.map ((coprod.associator W X Y).hom) (𝟙 Z) ≫
(coprod.associator W (X⨿Y) Z).hom ≫ coprod.map (𝟙 W) ((coprod.associator X Y Z).hom) =
(coprod.associator (W⨿X) Y Z).hom ≫ (coprod.associator W X (Y⨿Z)).hom :=
by tidy
lemma coprod.associator_naturality {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃) :
coprod.map (coprod.map f₁ f₂) f₃ ≫ (coprod.associator Y₁ Y₂ Y₃).hom =
(coprod.associator X₁ X₂ X₃).hom ≫ coprod.map f₁ (coprod.map f₂ f₃) :=
by tidy
variables [has_initial.{v} C]
/-- The left unitor isomorphism for binary coproducts with the initial object. -/
@[simps] def coprod.left_unitor
(P : C) : ⊥_ C ⨿ P ≅ P :=
{ hom := coprod.desc (initial.to P) (𝟙 _),
inv := coprod.inr }
/-- The right unitor isomorphism for binary coproducts with the initial object. -/
@[simps] def coprod.right_unitor
(P : C) : P ⨿ ⊥_ C ≅ P :=
{ hom := coprod.desc (𝟙 _) (initial.to P),
inv := coprod.inl }
lemma coprod.triangle (X Y : C) :
(coprod.associator X (⊥_ C) Y).hom ≫ coprod.map (𝟙 X) ((coprod.left_unitor Y).hom) =
coprod.map ((coprod.right_unitor X).hom) (𝟙 Y) :=
by tidy
end
end category_theory.limits
|
9b155f26d1e39c2f5abee3d7d5bc405d36836edb | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/geometry/euclidean/basic.lean | df653d98ba4c23c564143903550d621b757cdb57 | [
"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 | 46,680 | lean | /-
Copyright (c) 2020 Joseph Myers. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Joseph Myers.
-/
import analysis.normed_space.real_inner_product
import analysis.normed_space.add_torsor
import data.matrix.notation
import linear_algebra.affine_space.combination
import tactic.fin_cases
noncomputable theory
open_locale big_operators
open_locale classical
open_locale real
/-!
# Euclidean spaces
This file makes some definitions and proves very basic geometrical
results about real inner product spaces and Euclidean affine spaces.
Results about real inner product spaces that involve the norm and
inner product but not angles generally go in
`analysis.normed_space.real_inner_product`. Results with longer
proofs or more geometrical content generally go in separate files.
## Main definitions
* `inner_product_geometry.angle` is the undirected angle between two
vectors.
* `euclidean_geometry.angle`, with notation `∠`, is the undirected
angle determined by three points.
* `euclidean_geometry.orthogonal_projection` is the orthogonal
projection of a point onto an affine subspace.
* `euclidean_geometry.reflection` is the reflection of a point in an
affine subspace.
## Implementation notes
To declare `P` as the type of points in a Euclidean affine space with
`V` as the type of vectors, use `[inner_product_space V] [metric_space P]
[normed_add_torsor V P]`. This works better with `out_param` to make
`V` implicit in most cases than having a separate type alias for
Euclidean affine spaces.
Rather than requiring Euclidean affine spaces to be finite-dimensional
(as in the definition on Wikipedia), this is specified only for those
theorems that need it.
## References
* https://en.wikipedia.org/wiki/Euclidean_space
-/
namespace inner_product_geometry
/-!
### Geometrical results on real inner product spaces
This section develops some geometrical definitions and results on real
inner product spaces, where those definitions and results can most
conveniently be developed in terms of vectors and then used to deduce
corresponding results for Euclidean affine spaces.
-/
variables {V : Type*} [inner_product_space V]
/-- The undirected angle between two vectors. If either vector is 0,
this is π/2. -/
def angle (x y : V) : ℝ := real.arccos (inner x y / (∥x∥ * ∥y∥))
/-- The cosine of the angle between two vectors. -/
lemma cos_angle (x y : V) : real.cos (angle x y) = inner x y / (∥x∥ * ∥y∥) :=
real.cos_arccos (abs_le.mp (abs_inner_div_norm_mul_norm_le_one x y)).1
(abs_le.mp (abs_inner_div_norm_mul_norm_le_one x y)).2
/-- The angle between two vectors does not depend on their order. -/
lemma angle_comm (x y : V) : angle x y = angle y x :=
begin
unfold angle,
rw [inner_comm, mul_comm]
end
/-- The angle between the negation of two vectors. -/
@[simp] lemma angle_neg_neg (x y : V) : angle (-x) (-y) = angle x y :=
begin
unfold angle,
rw [inner_neg_neg, norm_neg, norm_neg]
end
/-- The angle between two vectors is nonnegative. -/
lemma angle_nonneg (x y : V) : 0 ≤ angle x y :=
real.arccos_nonneg _
/-- The angle between two vectors is at most π. -/
lemma angle_le_pi (x y : V) : angle x y ≤ π :=
real.arccos_le_pi _
/-- The angle between a vector and the negation of another vector. -/
lemma angle_neg_right (x y : V) : angle x (-y) = π - angle x y :=
begin
unfold angle,
rw [←real.arccos_neg, norm_neg, inner_neg_right, neg_div]
end
/-- The angle between the negation of a vector and another vector. -/
lemma angle_neg_left (x y : V) : angle (-x) y = π - angle x y :=
by rw [←angle_neg_neg, neg_neg, angle_neg_right]
/-- The angle between the zero vector and a vector. -/
@[simp] lemma angle_zero_left (x : V) : angle 0 x = π / 2 :=
begin
unfold angle,
rw [inner_zero_left, zero_div, real.arccos_zero]
end
/-- The angle between a vector and the zero vector. -/
@[simp] lemma angle_zero_right (x : V) : angle x 0 = π / 2 :=
begin
unfold angle,
rw [inner_zero_right, zero_div, real.arccos_zero]
end
/-- The angle between a nonzero vector and itself. -/
@[simp] lemma angle_self {x : V} (hx : x ≠ 0) : angle x x = 0 :=
begin
unfold angle,
rw [←inner_self_eq_norm_square, div_self (λ h, hx (inner_self_eq_zero.1 h)),
real.arccos_one]
end
/-- The angle between a nonzero vector and its negation. -/
@[simp] lemma angle_self_neg_of_nonzero {x : V} (hx : x ≠ 0) : angle x (-x) = π :=
by rw [angle_neg_right, angle_self hx, sub_zero]
/-- The angle between the negation of a nonzero vector and that
vector. -/
@[simp] lemma angle_neg_self_of_nonzero {x : V} (hx : x ≠ 0) : angle (-x) x = π :=
by rw [angle_comm, angle_self_neg_of_nonzero hx]
/-- The angle between a vector and a positive multiple of a vector. -/
@[simp] lemma angle_smul_right_of_pos (x y : V) {r : ℝ} (hr : 0 < r) :
angle x (r • y) = angle x y :=
begin
unfold angle,
rw [inner_smul_right, norm_smul, real.norm_eq_abs, abs_of_nonneg (le_of_lt hr), ←mul_assoc,
mul_comm _ r, mul_assoc, mul_div_mul_left _ _ (ne_of_gt hr)]
end
/-- The angle between a positive multiple of a vector and a vector. -/
@[simp] lemma angle_smul_left_of_pos (x y : V) {r : ℝ} (hr : 0 < r) :
angle (r • x) y = angle x y :=
by rw [angle_comm, angle_smul_right_of_pos y x hr, angle_comm]
/-- The angle between a vector and a negative multiple of a vector. -/
@[simp] lemma angle_smul_right_of_neg (x y : V) {r : ℝ} (hr : r < 0) :
angle x (r • y) = angle x (-y) :=
by rw [←neg_neg r, neg_smul, angle_neg_right, angle_smul_right_of_pos x y (neg_pos_of_neg hr),
angle_neg_right]
/-- The angle between a negative multiple of a vector and a vector. -/
@[simp] lemma angle_smul_left_of_neg (x y : V) {r : ℝ} (hr : r < 0) :
angle (r • x) y = angle (-x) y :=
by rw [angle_comm, angle_smul_right_of_neg y x hr, angle_comm]
/-- The cosine of the angle between two vectors, multiplied by the
product of their norms. -/
lemma cos_angle_mul_norm_mul_norm (x y : V) : real.cos (angle x y) * (∥x∥ * ∥y∥) = inner x y :=
begin
rw cos_angle,
by_cases h : (∥x∥ * ∥y∥) = 0,
{ rw [h, mul_zero],
cases eq_zero_or_eq_zero_of_mul_eq_zero h with hx hy,
{ rw norm_eq_zero at hx,
rw [hx, inner_zero_left] },
{ rw norm_eq_zero at hy,
rw [hy, inner_zero_right] } },
{ exact div_mul_cancel _ h }
end
/-- The sine of the angle between two vectors, multiplied by the
product of their norms. -/
lemma sin_angle_mul_norm_mul_norm (x y : V) : real.sin (angle x y) * (∥x∥ * ∥y∥) =
real.sqrt (inner x x * inner y y - inner x y * inner x y) :=
begin
unfold angle,
rw [real.sin_arccos (abs_le.mp (abs_inner_div_norm_mul_norm_le_one x y)).1
(abs_le.mp (abs_inner_div_norm_mul_norm_le_one x y)).2,
←real.sqrt_mul_self (mul_nonneg (norm_nonneg x) (norm_nonneg y)),
←real.sqrt_mul' _ (mul_self_nonneg _), pow_two,
real.sqrt_mul_self (mul_nonneg (norm_nonneg x) (norm_nonneg y)), inner_self_eq_norm_square,
inner_self_eq_norm_square],
by_cases h : (∥x∥ * ∥y∥) = 0,
{ rw [(show ∥x∥ * ∥x∥ * (∥y∥ * ∥y∥) = (∥x∥ * ∥y∥) * (∥x∥ * ∥y∥), by ring), h, mul_zero, mul_zero,
zero_sub],
cases eq_zero_or_eq_zero_of_mul_eq_zero h with hx hy,
{ rw norm_eq_zero at hx,
rw [hx, inner_zero_left, zero_mul, neg_zero] },
{ rw norm_eq_zero at hy,
rw [hy, inner_zero_right, zero_mul, neg_zero] } },
{ field_simp [h],
ring }
end
/-- The angle between two vectors is zero if and only if they are
nonzero and one is a positive multiple of the other. -/
lemma angle_eq_zero_iff (x y : V) : angle x y = 0 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), 0 < r ∧ y = r • x) :=
begin
unfold angle,
rw [←inner_div_norm_mul_norm_eq_one_iff, ←real.arccos_one],
split,
{ intro h,
exact real.arccos_inj (abs_le.mp (abs_inner_div_norm_mul_norm_le_one x y)).1
(abs_le.mp (abs_inner_div_norm_mul_norm_le_one x y)).2
(by norm_num)
(by norm_num)
h },
{ intro h,
rw h }
end
/-- The angle between two vectors is π if and only if they are nonzero
and one is a negative multiple of the other. -/
lemma angle_eq_pi_iff (x y : V) : angle x y = π ↔ (x ≠ 0 ∧ ∃ (r : ℝ), r < 0 ∧ y = r • x) :=
begin
unfold angle,
rw [←inner_div_norm_mul_norm_eq_neg_one_iff, ←real.arccos_neg_one],
split,
{ intro h,
exact real.arccos_inj (abs_le.mp (abs_inner_div_norm_mul_norm_le_one x y)).1
(abs_le.mp (abs_inner_div_norm_mul_norm_le_one x y)).2
(by norm_num)
(by norm_num)
h },
{ intro h,
rw h }
end
/-- If the angle between two vectors is π, the angles between those
vectors and a third vector add to π. -/
lemma angle_add_angle_eq_pi_of_angle_eq_pi {x y : V} (z : V) (h : angle x y = π) :
angle x z + angle y z = π :=
begin
rw angle_eq_pi_iff at h,
rcases h with ⟨hx, ⟨r, ⟨hr, hxy⟩⟩⟩,
rw [hxy, angle_smul_left_of_neg x z hr, angle_neg_left,
add_sub_cancel'_right]
end
/-- Two vectors have inner product 0 if and only if the angle between
them is π/2. -/
lemma inner_eq_zero_iff_angle_eq_pi_div_two (x y : V) : inner x y = 0 ↔ angle x y = π / 2 :=
begin
split,
{ intro h,
unfold angle,
rw [h, zero_div, real.arccos_zero] },
{ intro h,
unfold angle at h,
rw ←real.arccos_zero at h,
have h2 : inner x y / (∥x∥ * ∥y∥) = 0 :=
real.arccos_inj (abs_le.mp (abs_inner_div_norm_mul_norm_le_one x y)).1
(abs_le.mp (abs_inner_div_norm_mul_norm_le_one x y)).2
(by norm_num)
(by norm_num)
h,
by_cases h : (∥x∥ * ∥y∥) = 0,
{ cases eq_zero_or_eq_zero_of_mul_eq_zero h with hx hy,
{ rw norm_eq_zero at hx,
rw [hx, inner_zero_left] },
{ rw norm_eq_zero at hy,
rw [hy, inner_zero_right] } },
{ simpa [h, div_eq_zero_iff] using h2 } },
end
end inner_product_geometry
namespace euclidean_geometry
/-!
### Geometrical results on Euclidean affine spaces
This section develops some geometrical definitions and results on
Euclidean affine spaces.
-/
open inner_product_geometry
variables {V : Type*} {P : Type*} [inner_product_space V] [metric_space P]
[normed_add_torsor V P]
include V
/-- The undirected angle at `p2` between the line segments to `p1` and
`p3`. If either of those points equals `p2`, this is π/2. Use
`open_locale euclidean_geometry` to access the `∠ p1 p2 p3`
notation. -/
def angle (p1 p2 p3 : P) : ℝ := angle (p1 -ᵥ p2 : V) (p3 -ᵥ p2)
localized "notation `∠` := euclidean_geometry.angle" in euclidean_geometry
/-- The angle at a point does not depend on the order of the other two
points. -/
lemma angle_comm (p1 p2 p3 : P) : ∠ p1 p2 p3 = ∠ p3 p2 p1 :=
angle_comm _ _
/-- The angle at a point is nonnegative. -/
lemma angle_nonneg (p1 p2 p3 : P) : 0 ≤ ∠ p1 p2 p3 :=
angle_nonneg _ _
/-- The angle at a point is at most π. -/
lemma angle_le_pi (p1 p2 p3 : P) : ∠ p1 p2 p3 ≤ π :=
angle_le_pi _ _
/-- The angle ∠AAB at a point. -/
lemma angle_eq_left (p1 p2 : P) : ∠ p1 p1 p2 = π / 2 :=
begin
unfold angle,
rw vsub_self,
exact angle_zero_left _
end
/-- The angle ∠ABB at a point. -/
lemma angle_eq_right (p1 p2 : P) : ∠ p1 p2 p2 = π / 2 :=
by rw [angle_comm, angle_eq_left]
/-- The angle ∠ABA at a point. -/
lemma angle_eq_of_ne {p1 p2 : P} (h : p1 ≠ p2) : ∠ p1 p2 p1 = 0 :=
angle_self (λ he, h (vsub_eq_zero_iff_eq.1 he))
/-- If the angle ∠ABC at a point is π, the angle ∠BAC is 0. -/
lemma angle_eq_zero_of_angle_eq_pi_left {p1 p2 p3 : P} (h : ∠ p1 p2 p3 = π) :
∠ p2 p1 p3 = 0 :=
begin
unfold angle at h,
rw angle_eq_pi_iff at h,
rcases h with ⟨hp1p2, ⟨r, ⟨hr, hpr⟩⟩⟩,
unfold angle,
rw angle_eq_zero_iff,
rw [←neg_vsub_eq_vsub_rev, neg_ne_zero] at hp1p2,
use [hp1p2, -r + 1, add_pos (neg_pos_of_neg hr) zero_lt_one],
rw [add_smul, ←neg_vsub_eq_vsub_rev p1 p2, smul_neg],
simp [←hpr]
end
/-- If the angle ∠ABC at a point is π, the angle ∠BCA is 0. -/
lemma angle_eq_zero_of_angle_eq_pi_right {p1 p2 p3 : P} (h : ∠ p1 p2 p3 = π) :
∠ p2 p3 p1 = 0 :=
begin
rw angle_comm at h,
exact angle_eq_zero_of_angle_eq_pi_left h
end
/-- If ∠BCD = π, then ∠ABC = ∠ABD. -/
lemma angle_eq_angle_of_angle_eq_pi (p1 : P) {p2 p3 p4 : P} (h : ∠ p2 p3 p4 = π) :
∠ p1 p2 p3 = ∠ p1 p2 p4 :=
begin
unfold angle at h,
rw angle_eq_pi_iff at h,
rcases h with ⟨hp2p3, ⟨r, ⟨hr, hpr⟩⟩⟩,
unfold angle,
symmetry,
convert angle_smul_right_of_pos _ _ (add_pos (neg_pos_of_neg hr) zero_lt_one),
rw [add_smul, ←neg_vsub_eq_vsub_rev p2 p3, smul_neg],
simp [←hpr]
end
/-- If ∠BCD = π, then ∠ACB + ∠ACD = π. -/
lemma angle_add_angle_eq_pi_of_angle_eq_pi (p1 : P) {p2 p3 p4 : P} (h : ∠ p2 p3 p4 = π) :
∠ p1 p3 p2 + ∠ p1 p3 p4 = π :=
begin
unfold angle at h,
rw [angle_comm p1 p3 p2, angle_comm p1 p3 p4],
unfold angle,
exact angle_add_angle_eq_pi_of_angle_eq_pi _ h
end
/-- The inner product of two vectors given with `weighted_vsub`, in
terms of the pairwise distances. -/
lemma inner_weighted_vsub {ι₁ : Type*} {s₁ : finset ι₁} {w₁ : ι₁ → ℝ} (p₁ : ι₁ → P)
(h₁ : ∑ i in s₁, w₁ i = 0) {ι₂ : Type*} {s₂ : finset ι₂} {w₂ : ι₂ → ℝ} (p₂ : ι₂ → P)
(h₂ : ∑ i in s₂, w₂ i = 0) :
inner (s₁.weighted_vsub p₁ w₁) (s₂.weighted_vsub p₂ w₂) =
(-∑ i₁ in s₁, ∑ i₂ in s₂,
w₁ i₁ * w₂ i₂ * (dist (p₁ i₁) (p₂ i₂) * dist (p₁ i₁) (p₂ i₂))) / 2 :=
begin
rw [finset.weighted_vsub_apply, finset.weighted_vsub_apply,
inner_sum_smul_sum_smul_of_sum_eq_zero _ h₁ _ h₂],
simp_rw [vsub_sub_vsub_cancel_right],
rcongr i₁ i₂; rw dist_eq_norm_vsub V (p₁ i₁) (p₂ i₂)
end
/-- The distance between two points given with `affine_combination`,
in terms of the pairwise distances between the points in that
combination. -/
lemma dist_affine_combination {ι : Type*} {s : finset ι} {w₁ w₂ : ι → ℝ} (p : ι → P)
(h₁ : ∑ i in s, w₁ i = 1) (h₂ : ∑ i in s, w₂ i = 1) :
dist (s.affine_combination p w₁) (s.affine_combination p w₂) *
dist (s.affine_combination p w₁) (s.affine_combination p w₂) =
(-∑ i₁ in s, ∑ i₂ in s,
(w₁ - w₂) i₁ * (w₁ - w₂) i₂ * (dist (p i₁) (p i₂) * dist (p i₁) (p i₂))) / 2 :=
begin
rw [dist_eq_norm_vsub V (s.affine_combination p w₁) (s.affine_combination p w₂),
←inner_self_eq_norm_square, finset.affine_combination_vsub],
have h : ∑ i in s, (w₁ - w₂) i = 0,
{ simp_rw [pi.sub_apply, finset.sum_sub_distrib, h₁, h₂, sub_self] },
exact inner_weighted_vsub p h p h
end
/-- Suppose that `c₁` is equidistant from `p₁` and `p₂`, and the same
applies to `c₂`. Then the vector between `c₁` and `c₂` is orthogonal
to that between `p₁` and `p₂`. (In two dimensions, this says that the
diagonals of a kite are orthogonal.) -/
lemma inner_vsub_vsub_of_dist_eq_of_dist_eq {c₁ c₂ p₁ p₂ : P} (hc₁ : dist p₁ c₁ = dist p₂ c₁)
(hc₂ : dist p₁ c₂ = dist p₂ c₂) : inner (c₂ -ᵥ c₁) (p₂ -ᵥ p₁) = 0 :=
begin
have h : inner ((c₂ -ᵥ c₁) + (c₂ -ᵥ c₁)) (p₂ -ᵥ p₁) = 0,
{ conv_lhs { congr, congr, rw ←vsub_sub_vsub_cancel_right c₂ c₁ p₁,
skip, rw ←vsub_sub_vsub_cancel_right c₂ c₁ p₂ },
rw [←add_sub_comm, inner_sub_left],
conv_lhs { congr, rw ←vsub_sub_vsub_cancel_right p₂ p₁ c₂,
skip, rw ←vsub_sub_vsub_cancel_right p₂ p₁ c₁ },
rw [dist_comm p₁, dist_comm p₂, dist_eq_norm_vsub V _ p₁,
dist_eq_norm_vsub V _ p₂, ←inner_add_sub_eq_zero_iff] at hc₁ hc₂,
simp_rw [←neg_vsub_eq_vsub_rev c₁, ←neg_vsub_eq_vsub_rev c₂, sub_neg_eq_add,
neg_add_eq_sub, hc₁, hc₂, sub_zero] },
simpa [inner_add_left, ←mul_two, (by norm_num : (2 : ℝ) ≠ 0)] using h
end
/-- The squared distance between points on a line (expressed as a
multiple of a fixed vector added to a point) and another point,
expressed as a quadratic. -/
lemma dist_smul_vadd_square (r : ℝ) (v : V) (p₁ p₂ : P) :
dist (r • v +ᵥ p₁) p₂ * dist (r • v +ᵥ p₁) p₂ =
inner v v * r * r + 2 * inner v (p₁ -ᵥ p₂) * r + inner (p₁ -ᵥ p₂) (p₁ -ᵥ p₂) :=
begin
rw [dist_eq_norm_vsub V _ p₂, ←inner_self_eq_norm_square, vadd_vsub_assoc, inner_add_add_self,
inner_smul_left, inner_smul_left, inner_smul_right],
ring
end
/-- The condition for two points on a line to be equidistant from
another point. -/
lemma dist_smul_vadd_eq_dist {v : V} (p₁ p₂ : P) (hv : v ≠ 0) (r : ℝ) :
dist (r • v +ᵥ p₁) p₂ = dist p₁ p₂ ↔ (r = 0 ∨ r = -2 * inner v (p₁ -ᵥ p₂) / inner v v) :=
begin
conv_lhs { rw [←mul_self_inj_of_nonneg dist_nonneg dist_nonneg, dist_smul_vadd_square,
←sub_eq_zero_iff_eq, add_sub_assoc, dist_eq_norm_vsub V p₁ p₂,
←inner_self_eq_norm_square, sub_self] },
have hvi : inner v v ≠ 0, by simpa using hv,
have hd : discrim (inner v v) (2 * inner v (p₁ -ᵥ p₂)) 0 =
(2 * inner v (p₁ -ᵥ p₂)) * (2 * inner v (p₁ -ᵥ p₂)),
{ rw discrim, ring },
rw [quadratic_eq_zero_iff hvi hd, add_left_neg, zero_div, neg_mul_eq_neg_mul,
←mul_sub_right_distrib, sub_eq_add_neg, ←mul_two, mul_assoc, mul_div_assoc,
mul_div_mul_left, mul_div_assoc],
norm_num
end
open affine_subspace finite_dimensional
/-- Distances `r₁` `r₂` of `p` from two different points `c₁` `c₂` determine at
most two points `p₁` `p₂` in a two-dimensional subspace containing those points
(two circles intersect in at most two points). -/
lemma eq_of_dist_eq_of_dist_eq_of_mem_of_findim_eq_two {s : affine_subspace ℝ P}
[finite_dimensional ℝ s.direction] (hd : findim ℝ s.direction = 2) {c₁ c₂ p₁ p₂ p : P}
(hc₁s : c₁ ∈ s) (hc₂s : c₂ ∈ s) (hp₁s : p₁ ∈ s) (hp₂s : p₂ ∈ s) (hps : p ∈ s) {r₁ r₂ : ℝ}
(hc : c₁ ≠ c₂) (hp : p₁ ≠ p₂) (hp₁c₁ : dist p₁ c₁ = r₁) (hp₂c₁ : dist p₂ c₁ = r₁)
(hpc₁ : dist p c₁ = r₁) (hp₁c₂ : dist p₁ c₂ = r₂) (hp₂c₂ : dist p₂ c₂ = r₂)
(hpc₂ : dist p c₂ = r₂) : p = p₁ ∨ p = p₂ :=
begin
have ho : inner (c₂ -ᵥ c₁) (p₂ -ᵥ p₁) = 0 :=
inner_vsub_vsub_of_dist_eq_of_dist_eq (by cc) (by cc),
let b : fin 2 → V := ![c₂ -ᵥ c₁, p₂ -ᵥ p₁],
have hb : linear_independent ℝ b,
{ refine linear_independent_of_ne_zero_of_inner_eq_zero _ _,
{ intro i,
fin_cases i; simp [b, hc.symm, hp.symm] },
{ intros i j hij,
fin_cases i; fin_cases j; try { exact false.elim (hij rfl) },
{ exact ho },
{ rw inner_comm, exact ho } } },
have hbs : submodule.span ℝ (set.range b) = s.direction,
{ refine eq_of_le_of_findim_eq _ _,
{ rw [submodule.span_le, set.range_subset_iff],
intro i,
fin_cases i,
{ exact vsub_mem_direction hc₂s hc₁s },
{ exact vsub_mem_direction hp₂s hp₁s } },
{ rw [findim_span_eq_card hb, fintype.card_fin, hd] } },
have hv : ∀ v ∈ s.direction, ∃ t₁ t₂ : ℝ, v = t₁ • (c₂ -ᵥ c₁) + t₂ • (p₂ -ᵥ p₁),
{ intros v hv,
have hr : set.range b = {c₂ -ᵥ c₁, p₂ -ᵥ p₁},
{ have hu : (finset.univ : finset (fin 2)) = {0, 1}, by dec_trivial,
rw [←fintype.coe_image_univ, hu],
simp,
refl },
rw [←hbs, hr, submodule.mem_span_insert] at hv,
rcases hv with ⟨t₁, v', hv', hv⟩,
rw submodule.mem_span_singleton at hv',
rcases hv' with ⟨t₂, rfl⟩,
exact ⟨t₁, t₂, hv⟩ },
rcases hv (p -ᵥ p₁) (vsub_mem_direction hps hp₁s) with ⟨t₁, t₂, hpt⟩,
have hop : inner (c₂ -ᵥ c₁) (p -ᵥ p₁) = 0 :=
inner_vsub_vsub_of_dist_eq_of_dist_eq (by cc) (by cc),
simp only [hpt, inner_add_right, inner_smul_right, ho, mul_zero, add_zero, mul_eq_zero,
inner_self_eq_zero, vsub_eq_zero_iff_eq, hc.symm, or_false] at hop,
rw [hop, zero_smul, zero_add, ←eq_vadd_iff_vsub_eq] at hpt,
subst hpt,
have hp' : (p₂ -ᵥ p₁ : V) ≠ 0, { simp [hp.symm] },
have hp₂ : dist ((1 : ℝ) • (p₂ -ᵥ p₁) +ᵥ p₁) c₁ = r₁, { simp [hp₂c₁] },
rw [←hp₁c₁, dist_smul_vadd_eq_dist _ _ hp'] at hpc₁ hp₂,
simp only [one_ne_zero, false_or] at hp₂,
rw hp₂.symm at hpc₁,
cases hpc₁; simp [hpc₁]
end
/-- Distances `r₁` `r₂` of `p` from two different points `c₁` `c₂` determine at
most two points `p₁` `p₂` in two-dimensional space (two circles intersect in at
most two points). -/
lemma eq_of_dist_eq_of_dist_eq_of_findim_eq_two [finite_dimensional ℝ V] (hd : findim ℝ V = 2)
{c₁ c₂ p₁ p₂ p : P} {r₁ r₂ : ℝ} (hc : c₁ ≠ c₂) (hp : p₁ ≠ p₂) (hp₁c₁ : dist p₁ c₁ = r₁)
(hp₂c₁ : dist p₂ c₁ = r₁) (hpc₁ : dist p c₁ = r₁) (hp₁c₂ : dist p₁ c₂ = r₂)
(hp₂c₂ : dist p₂ c₂ = r₂) (hpc₂ : dist p c₂ = r₂) : p = p₁ ∨ p = p₂ :=
begin
have hd' : findim ℝ (⊤ : affine_subspace ℝ P).direction = 2,
{ rw [direction_top, findim_top],
exact hd },
exact eq_of_dist_eq_of_dist_eq_of_mem_of_findim_eq_two hd'
(mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _)
hc hp hp₁c₁ hp₂c₁ hpc₁ hp₁c₂ hp₂c₂ hpc₂
end
variables {V}
/-- The orthogonal projection of a point onto a nonempty affine
subspace, whose direction is complete, as an unbundled function. This
definition is only intended for use in setting up the bundled version
`orthogonal_projection` and should not be used once that is
defined. -/
def orthogonal_projection_fn {s : affine_subspace ℝ P} (hn : (s : set P).nonempty)
(hc : is_complete (s.direction : set V)) (p : P) : P :=
classical.some $ inter_eq_singleton_of_nonempty_of_is_compl
hn
(mk'_nonempty p s.direction.orthogonal)
((direction_mk' p s.direction.orthogonal).symm ▸ submodule.is_compl_orthogonal_of_is_complete hc)
/-- The intersection of the subspace and the orthogonal subspace
through the given point is the `orthogonal_projection_fn` of that
point onto the subspace. This lemma is only intended for use in
setting up the bundled version and should not be used once that is
defined. -/
lemma inter_eq_singleton_orthogonal_projection_fn {s : affine_subspace ℝ P}
(hn : (s : set P).nonempty) (hc : is_complete (s.direction : set V)) (p : P) :
(s : set P) ∩ (mk' p s.direction.orthogonal) = {orthogonal_projection_fn hn hc p} :=
classical.some_spec $ inter_eq_singleton_of_nonempty_of_is_compl
hn
(mk'_nonempty p s.direction.orthogonal)
((direction_mk' p s.direction.orthogonal).symm ▸ submodule.is_compl_orthogonal_of_is_complete hc)
/-- The `orthogonal_projection_fn` lies in the given subspace. This
lemma is only intended for use in setting up the bundled version and
should not be used once that is defined. -/
lemma orthogonal_projection_fn_mem {s : affine_subspace ℝ P} (hn : (s : set P).nonempty)
(hc : is_complete (s.direction : set V)) (p : P) : orthogonal_projection_fn hn hc p ∈ s :=
begin
rw [←mem_coe, ←set.singleton_subset_iff, ←inter_eq_singleton_orthogonal_projection_fn],
exact set.inter_subset_left _ _
end
/-- The `orthogonal_projection_fn` lies in the orthogonal
subspace. This lemma is only intended for use in setting up the
bundled version and should not be used once that is defined. -/
lemma orthogonal_projection_fn_mem_orthogonal {s : affine_subspace ℝ P}
(hn : (s : set P).nonempty) (hc : is_complete (s.direction : set V)) (p : P) :
orthogonal_projection_fn hn hc p ∈ mk' p s.direction.orthogonal :=
begin
rw [←mem_coe, ←set.singleton_subset_iff, ←inter_eq_singleton_orthogonal_projection_fn],
exact set.inter_subset_right _ _
end
/-- Subtracting `p` from its `orthogonal_projection_fn` produces a
result in the orthogonal direction. This lemma is only intended for
use in setting up the bundled version and should not be used once that
is defined. -/
lemma orthogonal_projection_fn_vsub_mem_direction_orthogonal {s : affine_subspace ℝ P}
(hn : (s : set P).nonempty) (hc : is_complete (s.direction : set V)) (p : P) :
orthogonal_projection_fn hn hc p -ᵥ p ∈ s.direction.orthogonal :=
direction_mk' p s.direction.orthogonal ▸
vsub_mem_direction (orthogonal_projection_fn_mem_orthogonal hn hc p) (self_mem_mk' _ _)
/-- The orthogonal projection of a point onto a nonempty affine
subspace, whose direction is complete. The corresponding linear map
(mapping a vector to the difference between the projections of two
points whose difference is that vector) is the `orthogonal_projection`
for real inner product spaces, onto the direction of the affine
subspace being projected onto. For most purposes,
`orthogonal_projection`, which removes the `nonempty` and
`is_complete` hypotheses and is the identity map when either of those
hypotheses fails, should be used instead. -/
def orthogonal_projection_of_nonempty_of_complete {s : affine_subspace ℝ P}
(hn : (s : set P).nonempty) (hc : is_complete (s.direction : set V)) : affine_map ℝ P P :=
{ to_fun := orthogonal_projection_fn hn hc,
linear := orthogonal_projection s.direction,
map_vadd' := λ p v, begin
have hs : (orthogonal_projection s.direction) v +ᵥ orthogonal_projection_fn hn hc p ∈ s :=
vadd_mem_of_mem_direction (orthogonal_projection_mem hc _)
(orthogonal_projection_fn_mem hn hc p),
have ho : (orthogonal_projection s.direction) v +ᵥ orthogonal_projection_fn hn hc p ∈
mk' (v +ᵥ p) s.direction.orthogonal,
{ rw [←vsub_right_mem_direction_iff_mem (self_mem_mk' _ _) _, direction_mk',
vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_comm, add_sub_assoc],
refine submodule.add_mem _ (orthogonal_projection_fn_vsub_mem_direction_orthogonal hn hc p) _,
rw submodule.mem_orthogonal',
intros w hw,
rw [←neg_sub, inner_neg_left, orthogonal_projection_inner_eq_zero _ _ w hw, neg_zero] },
have hm : (orthogonal_projection s.direction) v +ᵥ orthogonal_projection_fn hn hc p ∈
({orthogonal_projection_fn hn hc (v +ᵥ p)} : set P),
{ rw ←inter_eq_singleton_orthogonal_projection_fn hn hc (v +ᵥ p),
exact set.mem_inter hs ho },
rw set.mem_singleton_iff at hm,
exact hm.symm
end }
/-- The orthogonal projection of a point onto an affine subspace,
which is expected to be nonempty and complete. The corresponding
linear map (mapping a vector to the difference between the projections
of two points whose difference is that vector) is the
`orthogonal_projection` for real inner product spaces, onto the
direction of the affine subspace being projected onto. If the
subspace is empty or not complete, this uses the identity map
instead. -/
def orthogonal_projection (s : affine_subspace ℝ P) : affine_map ℝ P P :=
if h : (s : set P).nonempty ∧ is_complete (s.direction : set V) then
orthogonal_projection_of_nonempty_of_complete h.1 h.2 else affine_map.id ℝ P
/-- The definition of `orthogonal_projection` using `if`. -/
lemma orthogonal_projection_def (s : affine_subspace ℝ P) :
orthogonal_projection s = if h : (s : set P).nonempty ∧ is_complete (s.direction : set V) then
orthogonal_projection_of_nonempty_of_complete h.1 h.2 else affine_map.id ℝ P :=
rfl
@[simp] lemma orthogonal_projection_fn_eq {s : affine_subspace ℝ P} (hn : (s : set P).nonempty)
(hc : is_complete (s.direction : set V)) (p : P) :
orthogonal_projection_fn hn hc p = orthogonal_projection s p :=
by { rw [orthogonal_projection_def, dif_pos (and.intro hn hc)], refl }
/-- The linear map corresponding to `orthogonal_projection`. -/
@[simp] lemma orthogonal_projection_linear {s : affine_subspace ℝ P} (hn : (s : set P).nonempty) :
(orthogonal_projection s).linear = _root_.orthogonal_projection s.direction :=
begin
by_cases hc : is_complete (s.direction : set V),
{ rw [orthogonal_projection_def, dif_pos (and.intro hn hc)],
refl },
{ simp [orthogonal_projection_def, _root_.orthogonal_projection_def, hn, hc] }
end
@[simp] lemma orthogonal_projection_of_nonempty_of_complete_eq {s : affine_subspace ℝ P}
(hn : (s : set P).nonempty) (hc : is_complete (s.direction : set V)) (p : P) :
orthogonal_projection_of_nonempty_of_complete hn hc p = orthogonal_projection s p :=
by rw [orthogonal_projection_def, dif_pos (and.intro hn hc)]
/-- The intersection of the subspace and the orthogonal subspace
through the given point is the `orthogonal_projection` of that point
onto the subspace. -/
lemma inter_eq_singleton_orthogonal_projection {s : affine_subspace ℝ P}
(hn : (s : set P).nonempty) (hc : is_complete (s.direction : set V)) (p : P) :
(s : set P) ∩ (mk' p s.direction.orthogonal) = {orthogonal_projection s p} :=
begin
rw ←orthogonal_projection_fn_eq hn hc,
exact inter_eq_singleton_orthogonal_projection_fn hn hc p
end
/-- The `orthogonal_projection` lies in the given subspace. -/
lemma orthogonal_projection_mem {s : affine_subspace ℝ P} (hn : (s : set P).nonempty)
(hc : is_complete (s.direction : set V)) (p : P) : orthogonal_projection s p ∈ s :=
begin
rw ←orthogonal_projection_fn_eq hn hc,
exact orthogonal_projection_fn_mem hn hc p
end
/-- The `orthogonal_projection` lies in the orthogonal subspace. -/
lemma orthogonal_projection_mem_orthogonal (s : affine_subspace ℝ P) (p : P) :
orthogonal_projection s p ∈ mk' p s.direction.orthogonal :=
begin
rw orthogonal_projection_def,
split_ifs,
{ exact orthogonal_projection_fn_mem_orthogonal h.1 h.2 p },
{ exact self_mem_mk' _ _ }
end
/-- Subtracting a point in the given subspace from the
`orthogonal_projection` produces a result in the direction of the
given subspace. -/
lemma orthogonal_projection_vsub_mem_direction {s : affine_subspace ℝ P}
(hc : is_complete (s.direction : set V)) {p1 : P} (p2 : P) (hp1 : p1 ∈ s) :
orthogonal_projection s p2 -ᵥ p1 ∈ s.direction :=
vsub_mem_direction (orthogonal_projection_mem ⟨p1, hp1⟩ hc p2) hp1
/-- Subtracting the `orthogonal_projection` from a point in the given
subspace produces a result in the direction of the given subspace. -/
lemma vsub_orthogonal_projection_mem_direction {s : affine_subspace ℝ P}
(hc : is_complete (s.direction : set V)) {p1 : P} (p2 : P) (hp1 : p1 ∈ s) :
p1 -ᵥ orthogonal_projection s p2 ∈ s.direction :=
vsub_mem_direction hp1 (orthogonal_projection_mem ⟨p1, hp1⟩ hc p2)
/-- A point equals its orthogonal projection if and only if it lies in
the subspace. -/
lemma orthogonal_projection_eq_self_iff {s : affine_subspace ℝ P}
(hn : (s : set P).nonempty) (hc : is_complete (s.direction : set V)) {p : P} :
orthogonal_projection s p = p ↔ p ∈ s :=
begin
split,
{ exact λ h, h ▸ orthogonal_projection_mem hn hc p },
{ intro h,
have hp : p ∈ ((s : set P) ∩ mk' p s.direction.orthogonal) := ⟨h, self_mem_mk' p _⟩,
rw [inter_eq_singleton_orthogonal_projection hn hc p, set.mem_singleton_iff] at hp,
exact hp.symm }
end
/-- Orthogonal projection is idempotent. -/
@[simp] lemma orthogonal_projection_orthogonal_projection (s : affine_subspace ℝ P) (p : P) :
orthogonal_projection s (orthogonal_projection s p) = orthogonal_projection s p :=
begin
by_cases h : (s : set P).nonempty ∧ is_complete (s.direction : set V),
{ rw orthogonal_projection_eq_self_iff h.1 h.2,
exact orthogonal_projection_mem h.1 h.2 p },
{ simp [orthogonal_projection_def, h] }
end
/-- The distance to a point's orthogonal projection is 0 iff it lies in the subspace. -/
lemma dist_orthogonal_projection_eq_zero_iff {s : affine_subspace ℝ P}
(hn : (s : set P).nonempty) (hc : is_complete (s.direction : set V)) {p : P} :
dist p (orthogonal_projection s p) = 0 ↔ p ∈ s :=
by rw [dist_comm, dist_eq_zero, orthogonal_projection_eq_self_iff hn hc]
/-- The distance between a point and its orthogonal projection is
nonzero if it does not lie in the subspace. -/
lemma dist_orthogonal_projection_ne_zero_of_not_mem {s : affine_subspace ℝ P}
(hn : (s : set P).nonempty) (hc : is_complete (s.direction : set V)) {p : P} (hp : p ∉ s) :
dist p (orthogonal_projection s p) ≠ 0 :=
mt (dist_orthogonal_projection_eq_zero_iff hn hc).mp hp
/-- Subtracting `p` from its `orthogonal_projection` produces a result
in the orthogonal direction. -/
lemma orthogonal_projection_vsub_mem_direction_orthogonal (s : affine_subspace ℝ P) (p : P) :
orthogonal_projection s p -ᵥ p ∈ s.direction.orthogonal :=
begin
rw orthogonal_projection_def,
split_ifs,
{ exact orthogonal_projection_fn_vsub_mem_direction_orthogonal h.1 h.2 p },
{ simp }
end
/-- Subtracting the `orthogonal_projection` from `p` produces a result
in the orthogonal direction. -/
lemma vsub_orthogonal_projection_mem_direction_orthogonal (s : affine_subspace ℝ P) (p : P) :
p -ᵥ orthogonal_projection s p ∈ s.direction.orthogonal :=
direction_mk' p s.direction.orthogonal ▸
vsub_mem_direction (self_mem_mk' _ _) (orthogonal_projection_mem_orthogonal s p)
/-- Adding a vector to a point in the given subspace, then taking the
orthogonal projection, produces the original point if the vector was
in the orthogonal direction. -/
lemma orthogonal_projection_vadd_eq_self {s : affine_subspace ℝ P}
(hc : is_complete (s.direction : set V)) {p : P} (hp : p ∈ s) {v : V}
(hv : v ∈ s.direction.orthogonal) : orthogonal_projection s (v +ᵥ p) = p :=
begin
have h := vsub_orthogonal_projection_mem_direction_orthogonal s (v +ᵥ p),
rw [vadd_vsub_assoc, submodule.add_mem_iff_right _ hv] at h,
refine (eq_of_vsub_eq_zero _).symm,
refine submodule.disjoint_def.1 s.direction.orthogonal_disjoint _ _ h,
exact vsub_mem_direction hp (orthogonal_projection_mem ⟨p, hp⟩ hc (v +ᵥ p))
end
/-- Adding a vector to a point in the given subspace, then taking the
orthogonal projection, produces the original point if the vector is a
multiple of the result of subtracting a point's orthogonal projection
from that point. -/
lemma orthogonal_projection_vadd_smul_vsub_orthogonal_projection {s : affine_subspace ℝ P}
(hc : is_complete (s.direction : set V)) {p1 : P} (p2 : P) (r : ℝ) (hp : p1 ∈ s) :
orthogonal_projection s (r • (p2 -ᵥ orthogonal_projection s p2 : V) +ᵥ p1) = p1 :=
orthogonal_projection_vadd_eq_self hc hp
(submodule.smul_mem _ _ (vsub_orthogonal_projection_mem_direction_orthogonal s _))
/-- The square of the distance from a point in `s` to `p2` equals the
sum of the squares of the distances of the two points to the
`orthogonal_projection`. -/
lemma dist_square_eq_dist_orthogonal_projection_square_add_dist_orthogonal_projection_square
{s : affine_subspace ℝ P} {p1 : P} (p2 : P) (hp1 : p1 ∈ s) :
dist p1 p2 * dist p1 p2 =
dist p1 (orthogonal_projection s p2) * dist p1 (orthogonal_projection s p2) +
dist p2 (orthogonal_projection s p2) * dist p2 (orthogonal_projection s p2) :=
begin
rw [metric_space.dist_comm p2 _, dist_eq_norm_vsub V p1 _, dist_eq_norm_vsub V p1 _,
dist_eq_norm_vsub V _ p2, ← vsub_add_vsub_cancel p1 (orthogonal_projection s p2) p2,
norm_add_square_eq_norm_square_add_norm_square_iff_inner_eq_zero],
rw orthogonal_projection_def,
split_ifs,
{ rw orthogonal_projection_of_nonempty_of_complete_eq,
exact submodule.inner_right_of_mem_orthogonal
(vsub_orthogonal_projection_mem_direction h.2 p2 hp1)
(orthogonal_projection_vsub_mem_direction_orthogonal s p2) },
{ simp }
end
/-- The square of the distance between two points constructed by
adding multiples of the same orthogonal vector to points in the same
subspace. -/
lemma dist_square_smul_orthogonal_vadd_smul_orthogonal_vadd {s : affine_subspace ℝ P}
{p1 p2 : P} (hp1 : p1 ∈ s) (hp2 : p2 ∈ s) (r1 r2 : ℝ) {v : V}
(hv : v ∈ s.direction.orthogonal) :
dist (r1 • v +ᵥ p1) (r2 • v +ᵥ p2) * dist (r1 • v +ᵥ p1) (r2 • v +ᵥ p2) =
dist p1 p2 * dist p1 p2 + (r1 - r2) * (r1 - r2) * (∥v∥ * ∥v∥) :=
calc dist (r1 • v +ᵥ p1) (r2 • v +ᵥ p2) * dist (r1 • v +ᵥ p1) (r2 • v +ᵥ p2)
= ∥(p1 -ᵥ p2) + (r1 - r2) • v∥ * ∥(p1 -ᵥ p2) + (r1 - r2) • v∥
: by { rw [dist_eq_norm_vsub V (r1 • v +ᵥ p1), vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, sub_smul],
abel }
... = ∥p1 -ᵥ p2∥ * ∥p1 -ᵥ p2∥ + ∥(r1 - r2) • v∥ * ∥(r1 - r2) • v∥
: norm_add_square_eq_norm_square_add_norm_square
(submodule.inner_right_of_mem_orthogonal (vsub_mem_direction hp1 hp2)
(submodule.smul_mem _ _ hv))
... = ∥(p1 -ᵥ p2 : V)∥ * ∥(p1 -ᵥ p2 : V)∥ + abs (r1 - r2) * abs (r1 - r2) * ∥v∥ * ∥v∥
: by { rw [norm_smul, real.norm_eq_abs], ring }
... = dist p1 p2 * dist p1 p2 + (r1 - r2) * (r1 - r2) * (∥v∥ * ∥v∥)
: by { rw [dist_eq_norm_vsub V p1, abs_mul_abs_self, mul_assoc] }
/-- Reflection in an affine subspace, which is expected to be nonempty
and complete. The word "reflection" is sometimes understood to mean
specifically reflection in a codimension-one subspace, and sometimes
more generally to cover operations such as reflection in a point. The
definition here, of reflection in an affine subspace, is a more
general sense of the word that includes both those common cases. If
the subspace is empty or not complete, `orthogonal_projection` is
defined as the identity map, which results in `reflection` being the
identity map in that case as well. -/
def reflection (s : affine_subspace ℝ P) : P ≃ᵢ P :=
{ to_fun := λ p, (orthogonal_projection s p -ᵥ p) +ᵥ orthogonal_projection s p,
inv_fun := λ p, (orthogonal_projection s p -ᵥ p) +ᵥ orthogonal_projection s p,
left_inv := λ p, by simp [vsub_vadd_eq_vsub_sub, -orthogonal_projection_linear],
right_inv := λ p, by simp [vsub_vadd_eq_vsub_sub, -orthogonal_projection_linear],
isometry_to_fun := begin
dsimp only,
rw isometry_emetric_iff_metric,
intros p₁ p₂,
rw [←mul_self_inj_of_nonneg dist_nonneg dist_nonneg, dist_eq_norm_vsub V
((orthogonal_projection s p₁ -ᵥ p₁) +ᵥ orthogonal_projection s p₁),
dist_eq_norm_vsub V p₁, ←inner_self_eq_norm_square, ←inner_self_eq_norm_square],
by_cases h : (s : set P).nonempty ∧ is_complete (s.direction : set V),
{ calc inner
(orthogonal_projection s p₁ -ᵥ p₁ +ᵥ orthogonal_projection s p₁ -ᵥ
(orthogonal_projection s p₂ -ᵥ p₂ +ᵥ orthogonal_projection s p₂))
(orthogonal_projection s p₁ -ᵥ p₁ +ᵥ orthogonal_projection s p₁ -ᵥ
(orthogonal_projection s p₂ -ᵥ p₂ +ᵥ orthogonal_projection s p₂))
= inner
((_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂)) +
_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂) -
(p₁ -ᵥ p₂))
(_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂) +
_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂) -
(p₁ -ᵥ p₂))
: by rw [vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_comm, add_sub_assoc,
←vsub_vadd_eq_vsub_sub, vsub_vadd_comm, vsub_vadd_eq_vsub_sub, ←add_sub_assoc,
←affine_map.linear_map_vsub, orthogonal_projection_linear h.1]
... = -4 * inner (p₁ -ᵥ p₂ - (_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂)))
(_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂)) +
inner (p₁ -ᵥ p₂) (p₁ -ᵥ p₂)
: by { simp [inner_sub_left, inner_sub_right, inner_add_left, inner_add_right,
inner_comm (p₁ -ᵥ p₂)],
ring }
... = -4 * 0 + inner (p₁ -ᵥ p₂) (p₁ -ᵥ p₂)
: by rw orthogonal_projection_inner_eq_zero s.direction _ _ (_root_.orthogonal_projection_mem h.2 _)
... = inner (p₁ -ᵥ p₂) (p₁ -ᵥ p₂) : by simp },
{ simp [orthogonal_projection_def, h] }
end }
/-- The result of reflecting. -/
lemma reflection_apply (s : affine_subspace ℝ P) (p : P) :
reflection s p = (orthogonal_projection s p -ᵥ p) +ᵥ orthogonal_projection s p :=
rfl
/-- Reflection is its own inverse. -/
@[simp] lemma reflection_symm (s : affine_subspace ℝ P) : (reflection s).symm = reflection s :=
rfl
/-- Reflecting twice in the same subspace. -/
@[simp] lemma reflection_reflection (s : affine_subspace ℝ P) (p : P) :
reflection s (reflection s p) = p :=
(reflection s).left_inv p
/-- Reflection is involutive. -/
lemma reflection_involutive (s : affine_subspace ℝ P) : function.involutive (reflection s) :=
reflection_reflection s
/-- A point is its own reflection if and only if it is in the
subspace. -/
lemma reflection_eq_self_iff {s : affine_subspace ℝ P} (hn : (s : set P).nonempty)
(hc : is_complete (s.direction : set V)) (p : P) : reflection s p = p ↔ p ∈ s :=
begin
rw [←orthogonal_projection_eq_self_iff hn hc, reflection_apply],
split,
{ intro h,
rw [←@vsub_eq_zero_iff_eq V, vadd_vsub_assoc,
←two_smul ℝ (orthogonal_projection s p -ᵥ p), smul_eq_zero] at h,
norm_num at h,
exact h },
{ intro h,
simp [h] }
end
/-- Reflecting a point in two subspaces produces the same result if
and only if the point has the same orthogonal projection in each of
those subspaces. -/
lemma reflection_eq_iff_orthogonal_projection_eq (s₁ s₂ : affine_subspace ℝ P) (p : P) :
reflection s₁ p = reflection s₂ p ↔
orthogonal_projection s₁ p = orthogonal_projection s₂ p :=
begin
rw [reflection_apply, reflection_apply],
split,
{ intro h,
rw [←@vsub_eq_zero_iff_eq V, vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_comm,
add_sub_assoc, vsub_sub_vsub_cancel_right,
←two_smul ℝ (orthogonal_projection s₁ p -ᵥ orthogonal_projection s₂ p),
smul_eq_zero] at h,
norm_num at h,
exact h },
{ intro h,
rw h }
end
/-- The distance between `p₁` and the reflection of `p₂` equals that
between the reflection of `p₁` and `p₂`. -/
lemma dist_reflection (s : affine_subspace ℝ P) (p₁ p₂ : P) :
dist p₁ (reflection s p₂) = dist (reflection s p₁) p₂ :=
begin
conv_lhs { rw ←reflection_reflection s p₁ },
exact (reflection s).dist_eq _ _
end
/-- A point in the subspace is equidistant from another point and its
reflection. -/
lemma dist_reflection_eq_of_mem (s : affine_subspace ℝ P) {p₁ : P} (hp₁ : p₁ ∈ s) (p₂ : P) :
dist p₁ (reflection s p₂) = dist p₁ p₂ :=
begin
by_cases h : (s : set P).nonempty ∧ is_complete (s.direction : set V),
{ rw ←reflection_eq_self_iff h.1 h.2 p₁ at hp₁,
conv_lhs { rw ←hp₁ },
exact (reflection s).dist_eq _ _ },
{ simp [reflection_apply, orthogonal_projection_def, h] }
end
/-- The reflection of a point in a subspace is contained in any larger
subspace containing both the point and the subspace reflected in. -/
lemma reflection_mem_of_le_of_mem {s₁ s₂ : affine_subspace ℝ P} (hle : s₁ ≤ s₂) {p : P}
(hp : p ∈ s₂) : reflection s₁ p ∈ s₂ :=
begin
rw [reflection_apply],
by_cases h : (s₁ : set P).nonempty ∧ is_complete (s₁.direction : set V),
{ have ho : orthogonal_projection s₁ p ∈ s₂ := hle (orthogonal_projection_mem h.1 h.2 p),
exact vadd_mem_of_mem_direction (vsub_mem_direction ho hp) ho },
{ simpa [reflection_apply, orthogonal_projection_def, h] }
end
/-- Reflecting an orthogonal vector plus a point in the subspace
produces the negation of that vector plus the point. -/
lemma reflection_orthogonal_vadd {s : affine_subspace ℝ P}
(hc : is_complete (s.direction : set V)) {p : P} (hp : p ∈ s) {v : V}
(hv : v ∈ s.direction.orthogonal) : reflection s (v +ᵥ p) = -v +ᵥ p :=
begin
rw [reflection_apply, orthogonal_projection_vadd_eq_self hc hp hv, vsub_vadd_eq_vsub_sub],
simp
end
/-- Reflecting a vector plus a point in the subspace produces the
negation of that vector plus the point if the vector is a multiple of
the result of subtracting a point's orthogonal projection from that
point. -/
lemma reflection_vadd_smul_vsub_orthogonal_projection {s : affine_subspace ℝ P}
(hc : is_complete (s.direction : set V)) {p₁ : P} (p₂ : P) (r : ℝ) (hp₁ : p₁ ∈ s) :
reflection s (r • (p₂ -ᵥ orthogonal_projection s p₂) +ᵥ p₁) =
-(r • (p₂ -ᵥ orthogonal_projection s p₂)) +ᵥ p₁ :=
reflection_orthogonal_vadd hc hp₁
(submodule.smul_mem _ _ (vsub_orthogonal_projection_mem_direction_orthogonal s _))
omit V
/-- A set of points is cospherical if they are equidistant from some
point. In two dimensions, this is the same thing as being
concyclic. -/
def cospherical (ps : set P) : Prop :=
∃ (center : P) (radius : ℝ), ∀ p ∈ ps, dist p center = radius
/-- The definition of `cospherical`. -/
lemma cospherical_def (ps : set P) :
cospherical ps ↔ ∃ (center : P) (radius : ℝ), ∀ p ∈ ps, dist p center = radius :=
iff.rfl
/-- A subset of a cospherical set is cospherical. -/
lemma cospherical_subset {ps₁ ps₂ : set P} (hs : ps₁ ⊆ ps₂) (hc : cospherical ps₂) :
cospherical ps₁ :=
begin
rcases hc with ⟨c, r, hcr⟩,
exact ⟨c, r, λ p hp, hcr p (hs hp)⟩
end
include V
/-- The empty set is cospherical. -/
lemma cospherical_empty : cospherical (∅ : set P) :=
begin
use add_torsor.nonempty.some,
simp,
end
omit V
/-- A single point is cospherical. -/
lemma cospherical_singleton (p : P) : cospherical ({p} : set P) :=
begin
use p,
simp
end
include V
/-- Two points are cospherical. -/
lemma cospherical_insert_singleton (p₁ p₂ : P) : cospherical ({p₁, p₂} : set P) :=
begin
use [(2⁻¹ : ℝ) • (p₂ -ᵥ p₁) +ᵥ p₁, (2⁻¹ : ℝ) * (dist p₂ p₁)],
intro p,
rw [set.mem_insert_iff, set.mem_singleton_iff],
rintro ⟨_|_⟩,
{ rw [dist_eq_norm_vsub V p₁, vsub_vadd_eq_vsub_sub, vsub_self, zero_sub, norm_neg, norm_smul,
dist_eq_norm_vsub V p₂],
simp },
{ rw [H, dist_eq_norm_vsub V p₂, vsub_vadd_eq_vsub_sub, dist_eq_norm_vsub V p₂],
conv_lhs { congr, congr, rw ←one_smul ℝ (p₂ -ᵥ p₁ : V) },
rw [←sub_smul, norm_smul],
norm_num }
end
end euclidean_geometry
|
0173b99a230e0620e3eda8f252f441c1e244b7fe | 4ad6af7525e674c3cdc623c1495472954f3ce34c | /src/04_fol.lean | 6562cc95f3547c915d8e68e46485ff808746cad3 | [] | no_license | alcides/lean3_tutorial | 4bf48026f7cfa5ae80e9a75f46ca23364d455810 | 7a871e00c4fd5cb000929a59e1bd804a626ef277 | refs/heads/master | 1,663,719,613,974 | 1,591,141,389,000 | 1,591,141,389,000 | 268,600,781 | 5 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,375 | lean | /-
In this file we will address how to deal with some examples from logic, namely and and or.
-/
/- Ignore this for now. But all imports are on the top of the file. -/
-- import tactic.suggest
/- Uncomment after reading the last note. -/
/- Prove that p ∧ q → p. It should be pretty straight-forward -/
theorem p_and_q_implies_p (p q:Prop):
p ∧ q → p :=
begin
intro proof_of_p_and_q,
exact proof_of_p_and_q.left, -- how did we know about this?
end
/-
If you delete "left" and ask for the autocomplete (cmd+space), you will not find left.
So how did we know that it was the solution?
If you cmd+click on the ∧ symbol, you go to its definition and you can discover that it is
a structure (similar to a struct in C or records in Haskell). You can see that it is made of
two fields: left and right. In this case, we use the one that is most useful for our proof.
-/
/- Prove that p → p ∨ q -/
theorem p_implies_p_or_q (p q:Prop) :
p → p ∨ q :=
begin
intro proof_of_p,
left,
exact proof_of_p,
end
/-
While there are other options, here we use the left (as opposed to the right) tactic.
These two tactics can be used to focus on the right or left side or an ∨-goal. We only
need to prove one of the sides.
-/
theorem q_implies_p_or_q_or_r (p q r :Prop) :
q → (p ∨ q) ∨ r :=
begin
sorry, -- it is another exercice.
end
theorem p_implies_p_or_not_p (p:Prop) : p → p ∨ (¬ p) :=
begin
sorry, -- it is yet another exercice.
end
/-
For this last theorem, the structure will be very similar to p_implies_p_or_q. In
fact, too similar! Instead of copying the proof, you should use the existing theorems
and lemmas. Tip: despite not being in the shown context, every theorem and lemma can be
used in the context of proofs.
-/
/- This next example is interesting, because we cannot go left or right. We have
to go both ways.
That can be done using induction on the structure of or. If calling it an induction bothers
you, you can replace "induction" with "cases", which works in the same way.
-/
example (n:ℕ) : n = 3 ∨ n = 4 → n = 4 ∨ n = 3 :=
begin
intros or_3_4,
induction or_3_4,
{
right,
exact or_3_4,
},
{
left,
exact or_3_4,
}
end
/-
My main complaints writing proofs was that I had to dive into the source code of
lean standard library looking for lemmas that would solve my problem, without knowing its name.
This example should be trivial to solve, if we have a theorem that shows that ∨ is commutative.
Let's use the "library_search" tactic, imported from "tactic.suggest" on the top of this file,
which you need to uncomment. Notice that it solves the goal, but has a blue wavy underline under
it. Library_search is useful when writing proofs, but does not serve as a proof that others can
read. Visual Studio Code shows a shortcut on the right to replace library_search with the found
lemma. The code shown below is the found tactic. Feel free to replace it and try library_search.
Note: library_search does not come with the lean standard library, and does not work on the webbrowser.
This tactic comes with the mathlib library, which you can install following the instructions on the Readme.
-/
example (n:ℕ) : n = 3 ∨ n = 4 → n = 4 ∨ n = 3 :=
begin
exact or.swap,
end |
c011ad35435fe95808cc71f20e6dfb90778f3f24 | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/category_theory/limits/creates.lean | e7eb9ebe2fe50f5b8d973423d26c4373e9f02900 | [
"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 | 23,124 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import category_theory.limits.preserves.basic
open category_theory category_theory.limits
noncomputable theory
namespace category_theory
universes v u₁ u₂ u₃
variables {C : Type u₁} [category.{v} C]
section creates
variables {D : Type u₂} [category.{v} D]
variables {J : Type v} [small_category J] {K : J ⥤ C}
/--
Define the lift of a cone: For a cone `c` for `K ⋙ F`, give a cone for `K`
which is a lift of `c`, i.e. the image of it under `F` is (iso) to `c`.
We will then use this as part of the definition of creation of limits:
every limit cone has a lift.
Note this definition is really only useful when `c` is a limit already.
-/
structure liftable_cone (K : J ⥤ C) (F : C ⥤ D) (c : cone (K ⋙ F)) :=
(lifted_cone : cone K)
(valid_lift : F.map_cone lifted_cone ≅ c)
/--
Define the lift of a cocone: For a cocone `c` for `K ⋙ F`, give a cocone for
`K` which is a lift of `c`, i.e. the image of it under `F` is (iso) to `c`.
We will then use this as part of the definition of creation of colimits:
every limit cocone has a lift.
Note this definition is really only useful when `c` is a colimit already.
-/
structure liftable_cocone (K : J ⥤ C) (F : C ⥤ D) (c : cocone (K ⋙ F)) :=
(lifted_cocone : cocone K)
(valid_lift : F.map_cocone lifted_cocone ≅ c)
/--
Definition 3.3.1 of [Riehl].
We say that `F` creates limits of `K` if, given any limit cone `c` for `K ⋙ F`
(i.e. below) we can lift it to a cone "above", and further that `F` reflects
limits for `K`.
If `F` reflects isomorphisms, it suffices to show only that the lifted cone is
a limit - see `creates_limit_of_reflects_iso`.
-/
class creates_limit (K : J ⥤ C) (F : C ⥤ D) extends reflects_limit K F :=
(lifts : Π c, is_limit c → liftable_cone K F c)
/--
`F` creates limits of shape `J` if `F` creates the limit of any diagram
`K : J ⥤ C`.
-/
class creates_limits_of_shape (J : Type v) [small_category J] (F : C ⥤ D) :=
(creates_limit : Π {K : J ⥤ C}, creates_limit K F . tactic.apply_instance)
/-- `F` creates limits if it creates limits of shape `J` for any small `J`. -/
class creates_limits (F : C ⥤ D) :=
(creates_limits_of_shape : Π {J : Type v} [small_category J],
creates_limits_of_shape J F . tactic.apply_instance)
/--
Dual of definition 3.3.1 of [Riehl].
We say that `F` creates colimits of `K` if, given any limit cocone `c` for
`K ⋙ F` (i.e. below) we can lift it to a cocone "above", and further that `F`
reflects limits for `K`.
If `F` reflects isomorphisms, it suffices to show only that the lifted cocone is
a limit - see `creates_limit_of_reflects_iso`.
-/
class creates_colimit (K : J ⥤ C) (F : C ⥤ D) extends reflects_colimit K F :=
(lifts : Π c, is_colimit c → liftable_cocone K F c)
/--
`F` creates colimits of shape `J` if `F` creates the colimit of any diagram
`K : J ⥤ C`.
-/
class creates_colimits_of_shape (J : Type v) [small_category J] (F : C ⥤ D) :=
(creates_colimit : Π {K : J ⥤ C}, creates_colimit K F . tactic.apply_instance)
/-- `F` creates colimits if it creates colimits of shape `J` for any small `J`. -/
class creates_colimits (F : C ⥤ D) :=
(creates_colimits_of_shape : Π {J : Type v} [small_category J],
creates_colimits_of_shape J F . tactic.apply_instance)
attribute [instance, priority 100] -- see Note [lower instance priority]
creates_limits_of_shape.creates_limit creates_limits.creates_limits_of_shape
creates_colimits_of_shape.creates_colimit creates_colimits.creates_colimits_of_shape
/- Interface to the `creates_limit` class. -/
/-- `lift_limit t` is the cone for `K` given by lifting the limit `t` for `K ⋙ F`. -/
def lift_limit {K : J ⥤ C} {F : C ⥤ D} [creates_limit K F] {c : cone (K ⋙ F)} (t : is_limit c) :
cone K :=
(creates_limit.lifts c t).lifted_cone
/-- The lifted cone has an image isomorphic to the original cone. -/
def lifted_limit_maps_to_original {K : J ⥤ C} {F : C ⥤ D}
[creates_limit K F] {c : cone (K ⋙ F)} (t : is_limit c) :
F.map_cone (lift_limit t) ≅ c :=
(creates_limit.lifts c t).valid_lift
/-- The lifted cone is a limit. -/
def lifted_limit_is_limit {K : J ⥤ C} {F : C ⥤ D}
[creates_limit K F] {c : cone (K ⋙ F)} (t : is_limit c) :
is_limit (lift_limit t) :=
reflects_limit.reflects (is_limit.of_iso_limit t (lifted_limit_maps_to_original t).symm)
/-- If `F` creates the limit of `K` and `K ⋙ F` has a limit, then `K` has a limit. -/
lemma has_limit_of_created (K : J ⥤ C) (F : C ⥤ D)
[has_limit (K ⋙ F)] [creates_limit K F] : has_limit K :=
has_limit.mk { cone := lift_limit (limit.is_limit (K ⋙ F)),
is_limit := lifted_limit_is_limit _ }
/--
If `F` creates limits of shape `J`, and `D` has limits of shape `J`, then
`C` has limits of shape `J`.
-/
lemma has_limits_of_shape_of_has_limits_of_shape_creates_limits_of_shape (F : C ⥤ D)
[has_limits_of_shape J D] [creates_limits_of_shape J F] : has_limits_of_shape J C :=
⟨λ G, has_limit_of_created G F⟩
/-- If `F` creates limits, and `D` has all limits, then `C` has all limits. -/
lemma has_limits_of_has_limits_creates_limits (F : C ⥤ D) [has_limits D] [creates_limits F] :
has_limits C :=
⟨λ J I, by exactI has_limits_of_shape_of_has_limits_of_shape_creates_limits_of_shape F⟩
/- Interface to the `creates_colimit` class. -/
/-- `lift_colimit t` is the cocone for `K` given by lifting the colimit `t` for `K ⋙ F`. -/
def lift_colimit {K : J ⥤ C} {F : C ⥤ D} [creates_colimit K F] {c : cocone (K ⋙ F)}
(t : is_colimit c) :
cocone K :=
(creates_colimit.lifts c t).lifted_cocone
/-- The lifted cocone has an image isomorphic to the original cocone. -/
def lifted_colimit_maps_to_original {K : J ⥤ C} {F : C ⥤ D}
[creates_colimit K F] {c : cocone (K ⋙ F)} (t : is_colimit c) :
F.map_cocone (lift_colimit t) ≅ c :=
(creates_colimit.lifts c t).valid_lift
/-- The lifted cocone is a colimit. -/
def lifted_colimit_is_colimit {K : J ⥤ C} {F : C ⥤ D}
[creates_colimit K F] {c : cocone (K ⋙ F)} (t : is_colimit c) :
is_colimit (lift_colimit t) :=
reflects_colimit.reflects (is_colimit.of_iso_colimit t (lifted_colimit_maps_to_original t).symm)
/-- If `F` creates the limit of `K` and `K ⋙ F` has a limit, then `K` has a limit. -/
lemma has_colimit_of_created (K : J ⥤ C) (F : C ⥤ D)
[has_colimit (K ⋙ F)] [creates_colimit K F] : has_colimit K :=
has_colimit.mk { cocone := lift_colimit (colimit.is_colimit (K ⋙ F)),
is_colimit := lifted_colimit_is_colimit _ }
/--
If `F` creates colimits of shape `J`, and `D` has colimits of shape `J`, then
`C` has colimits of shape `J`.
-/
lemma has_colimits_of_shape_of_has_colimits_of_shape_creates_colimits_of_shape (F : C ⥤ D)
[has_colimits_of_shape J D] [creates_colimits_of_shape J F] : has_colimits_of_shape J C :=
⟨λ G, has_colimit_of_created G F⟩
/-- If `F` creates colimits, and `D` has all colimits, then `C` has all colimits. -/
lemma has_colimits_of_has_colimits_creates_colimits (F : C ⥤ D) [has_colimits D]
[creates_colimits F] : has_colimits C :=
⟨λ J I, by exactI has_colimits_of_shape_of_has_colimits_of_shape_creates_colimits_of_shape F⟩
@[priority 10] instance reflects_limits_of_shape_of_creates_limits_of_shape (F : C ⥤ D)
[creates_limits_of_shape J F] : reflects_limits_of_shape J F := {}
@[priority 10] instance reflects_limits_of_creates_limits (F : C ⥤ D)
[creates_limits F] : reflects_limits F := {}
@[priority 10] instance reflects_colimits_of_shape_of_creates_colimits_of_shape (F : C ⥤ D)
[creates_colimits_of_shape J F] : reflects_colimits_of_shape J F := {}
@[priority 10] instance reflects_colimits_of_creates_colimits (F : C ⥤ D)
[creates_colimits F] : reflects_colimits F := {}
/--
A helper to show a functor creates limits. In particular, if we can show
that for any limit cone `c` for `K ⋙ F`, there is a lift of it which is
a limit and `F` reflects isomorphisms, then `F` creates limits.
Usually, `F` creating limits says that _any_ lift of `c` is a limit, but
here we only need to show that our particular lift of `c` is a limit.
-/
structure lifts_to_limit (K : J ⥤ C) (F : C ⥤ D) (c : cone (K ⋙ F)) (t : is_limit c)
extends liftable_cone K F c :=
(makes_limit : is_limit lifted_cone)
/--
A helper to show a functor creates colimits. In particular, if we can show
that for any limit cocone `c` for `K ⋙ F`, there is a lift of it which is
a limit and `F` reflects isomorphisms, then `F` creates colimits.
Usually, `F` creating colimits says that _any_ lift of `c` is a colimit, but
here we only need to show that our particular lift of `c` is a colimit.
-/
structure lifts_to_colimit (K : J ⥤ C) (F : C ⥤ D) (c : cocone (K ⋙ F)) (t : is_colimit c)
extends liftable_cocone K F c :=
(makes_colimit : is_colimit lifted_cocone)
/--
If `F` reflects isomorphisms and we can lift any limit cone to a limit cone,
then `F` creates limits.
In particular here we don't need to assume that F reflects limits.
-/
def creates_limit_of_reflects_iso {K : J ⥤ C} {F : C ⥤ D} [reflects_isomorphisms F]
(h : Π c t, lifts_to_limit K F c t) :
creates_limit K F :=
{ lifts := λ c t, (h c t).to_liftable_cone,
to_reflects_limit :=
{ reflects := λ (d : cone K) (hd : is_limit (F.map_cone d)),
begin
let d' : cone K := (h (F.map_cone d) hd).to_liftable_cone.lifted_cone,
let i : F.map_cone d' ≅ F.map_cone d := (h (F.map_cone d) hd).to_liftable_cone.valid_lift,
let hd' : is_limit d' := (h (F.map_cone d) hd).makes_limit,
let f : d ⟶ d' := hd'.lift_cone_morphism d,
have : (cones.functoriality K F).map f = i.inv := (hd.of_iso_limit i.symm).uniq_cone_morphism,
haveI : is_iso ((cones.functoriality K F).map f) := (by { rw this, apply_instance }),
haveI : is_iso f := is_iso_of_reflects_iso f (cones.functoriality K F),
exact is_limit.of_iso_limit hd' (as_iso f).symm,
end } }
/--
When `F` is fully faithful, and `has_limit (K ⋙ F)`, to show that `F` creates the limit for `K`
it suffices to exhibit a lift of the chosen limit cone for `K ⋙ F`.
-/
-- Notice however that even if the isomorphism is `iso.refl _`,
-- this construction will insert additional identity morphisms in the cone maps,
-- so the constructed limits may not be ideal, definitionally.
def creates_limit_of_fully_faithful_of_lift {K : J ⥤ C} {F : C ⥤ D}
[full F] [faithful F] [has_limit (K ⋙ F)]
(c : cone K) (i : F.map_cone c ≅ limit.cone (K ⋙ F)) : creates_limit K F :=
creates_limit_of_reflects_iso (λ c' t,
{ lifted_cone := c,
valid_lift := i.trans (is_limit.unique_up_to_iso (limit.is_limit _) t),
makes_limit := is_limit.of_faithful F (is_limit.of_iso_limit (limit.is_limit _) i.symm)
(λ s, F.preimage _) (λ s, F.image_preimage _) })
/--
When `F` is fully faithful, and `has_limit (K ⋙ F)`, to show that `F` creates the limit for `K`
it suffices to show that the chosen limit point is in the essential image of `F`.
-/
-- Notice however that even if the isomorphism is `iso.refl _`,
-- this construction will insert additional identity morphisms in the cone maps,
-- so the constructed limits may not be ideal, definitionally.
def creates_limit_of_fully_faithful_of_iso {K : J ⥤ C} {F : C ⥤ D}
[full F] [faithful F] [has_limit (K ⋙ F)]
(X : C) (i : F.obj X ≅ limit (K ⋙ F)) : creates_limit K F :=
creates_limit_of_fully_faithful_of_lift
({ X := X,
π :=
{ app := λ j, F.preimage (i.hom ≫ limit.π (K ⋙ F) j),
naturality' := λ Y Z f, F.map_injective (by { dsimp, simp, erw limit.w (K ⋙ F), }) }} : cone K)
(by { fapply cones.ext, exact i, tidy, })
/-- `F` preserves the limit of `K` if it creates the limit and `K ⋙ F` has the limit. -/
@[priority 100] -- see Note [lower instance priority]
instance preserves_limit_of_creates_limit_and_has_limit (K : J ⥤ C) (F : C ⥤ D)
[creates_limit K F] [has_limit (K ⋙ F)] :
preserves_limit K F :=
{ preserves := λ c t, is_limit.of_iso_limit (limit.is_limit _)
((lifted_limit_maps_to_original (limit.is_limit _)).symm ≪≫
((cones.functoriality K F).map_iso
((lifted_limit_is_limit (limit.is_limit _)).unique_up_to_iso t))) }
/-- `F` preserves the limit of shape `J` if it creates these limits and `D` has them. -/
@[priority 100] -- see Note [lower instance priority]
instance preserves_limit_of_shape_of_creates_limits_of_shape_and_has_limits_of_shape (F : C ⥤ D)
[creates_limits_of_shape J F] [has_limits_of_shape J D] :
preserves_limits_of_shape J F := {}
/-- `F` preserves limits if it creates limits and `D` has limits. -/
@[priority 100] -- see Note [lower instance priority]
instance preserves_limits_of_creates_limits_and_has_limits (F : C ⥤ D) [creates_limits F]
[has_limits D] :
preserves_limits F := {}
/--
If `F` reflects isomorphisms and we can lift any colimit cocone to a colimit cocone,
then `F` creates colimits.
In particular here we don't need to assume that F reflects colimits.
-/
def creates_colimit_of_reflects_iso {K : J ⥤ C} {F : C ⥤ D} [reflects_isomorphisms F]
(h : Π c t, lifts_to_colimit K F c t) :
creates_colimit K F :=
{ lifts := λ c t, (h c t).to_liftable_cocone,
to_reflects_colimit :=
{ reflects := λ (d : cocone K) (hd : is_colimit (F.map_cocone d)),
begin
let d' : cocone K := (h (F.map_cocone d) hd).to_liftable_cocone.lifted_cocone,
let i : F.map_cocone d' ≅ F.map_cocone d :=
(h (F.map_cocone d) hd).to_liftable_cocone.valid_lift,
let hd' : is_colimit d' := (h (F.map_cocone d) hd).makes_colimit,
let f : d' ⟶ d := hd'.desc_cocone_morphism d,
have : (cocones.functoriality K F).map f = i.hom :=
(hd.of_iso_colimit i.symm).uniq_cocone_morphism,
haveI : is_iso ((cocones.functoriality K F).map f) := (by { rw this, apply_instance }),
haveI := is_iso_of_reflects_iso f (cocones.functoriality K F),
exact is_colimit.of_iso_colimit hd' (as_iso f),
end } }
/-- `F` preserves the colimit of `K` if it creates the colimit and `K ⋙ F` has the colimit. -/
@[priority 100] -- see Note [lower instance priority]
instance preserves_colimit_of_creates_colimit_and_has_colimit (K : J ⥤ C) (F : C ⥤ D)
[creates_colimit K F] [has_colimit (K ⋙ F)] :
preserves_colimit K F :=
{ preserves := λ c t, is_colimit.of_iso_colimit (colimit.is_colimit _)
((lifted_colimit_maps_to_original (colimit.is_colimit _)).symm ≪≫
((cocones.functoriality K F).map_iso
((lifted_colimit_is_colimit (colimit.is_colimit _)).unique_up_to_iso t))) }
/-- `F` preserves the colimit of shape `J` if it creates these colimits and `D` has them. -/
@[priority 100] -- see Note [lower instance priority]
instance preserves_colimit_of_shape_of_creates_colimits_of_shape_and_has_colimits_of_shape
(F : C ⥤ D) [creates_colimits_of_shape J F] [has_colimits_of_shape J D] :
preserves_colimits_of_shape J F := {}
/-- `F` preserves limits if it creates limits and `D` has limits. -/
@[priority 100] -- see Note [lower instance priority]
instance preserves_colimits_of_creates_colimits_and_has_colimits (F : C ⥤ D) [creates_colimits F]
[has_colimits D] :
preserves_colimits F := {}
/-- Transfer creation of limits along a natural isomorphism in the diagram. -/
def creates_limit_of_iso_diagram {K₁ K₂ : J ⥤ C} (F : C ⥤ D) (h : K₁ ≅ K₂)
[creates_limit K₁ F] : creates_limit K₂ F :=
{ lifts := λ c t,
let t' := (is_limit.postcompose_inv_equiv (iso_whisker_right h F : _) c).symm t in
{ lifted_cone := (cones.postcompose h.hom).obj (lift_limit t'),
valid_lift :=
F.map_cone_postcompose ≪≫
(cones.postcompose (iso_whisker_right h F).hom).map_iso
(lifted_limit_maps_to_original t') ≪≫
cones.ext (iso.refl _) (λ j, by { dsimp, rw [category.assoc, ←F.map_comp], simp }) }
..reflects_limit_of_iso_diagram F h }
/-- If `F` creates the limit of `K` and `F ≅ G`, then `G` creates the limit of `K`. -/
def creates_limit_of_nat_iso {F G : C ⥤ D} (h : F ≅ G) [creates_limit K F] :
creates_limit K G :=
{ lifts := λ c t,
{ lifted_cone :=
lift_limit ((is_limit.postcompose_inv_equiv (iso_whisker_left K h : _) c).symm t),
valid_lift :=
begin
refine (is_limit.map_cone_equiv h _).unique_up_to_iso t,
apply is_limit.of_iso_limit _ ((lifted_limit_maps_to_original _).symm),
apply (is_limit.postcompose_inv_equiv _ _).symm t,
end },
to_reflects_limit := reflects_limit_of_nat_iso _ h }
/-- If `F` creates limits of shape `J` and `F ≅ G`, then `G` creates limits of shape `J`. -/
def creates_limits_of_shape_of_nat_iso {F G : C ⥤ D} (h : F ≅ G) [creates_limits_of_shape J F] :
creates_limits_of_shape J G :=
{ creates_limit := λ K, creates_limit_of_nat_iso h }
/-- If `F` creates limits and `F ≅ G`, then `G` creates limits. -/
def creates_limits_of_nat_iso {F G : C ⥤ D} (h : F ≅ G) [creates_limits F] :
creates_limits G :=
{ creates_limits_of_shape := λ J 𝒥₁, by exactI creates_limits_of_shape_of_nat_iso h }
/-- Transfer creation of colimits along a natural isomorphism in the diagram. -/
def creates_colimit_of_iso_diagram {K₁ K₂ : J ⥤ C} (F : C ⥤ D) (h : K₁ ≅ K₂)
[creates_colimit K₁ F] : creates_colimit K₂ F :=
{ lifts := λ c t,
let t' := (is_colimit.precompose_hom_equiv (iso_whisker_right h F : _) c).symm t in
{ lifted_cocone := (cocones.precompose h.inv).obj (lift_colimit t'),
valid_lift :=
F.map_cocone_precompose ≪≫
(cocones.precompose (iso_whisker_right h F).inv).map_iso
(lifted_colimit_maps_to_original t') ≪≫
cocones.ext (iso.refl _) (λ j, by { dsimp, rw ←F.map_comp_assoc, simp }) },
..reflects_colimit_of_iso_diagram F h }
/-- If `F` creates the colimit of `K` and `F ≅ G`, then `G` creates the colimit of `K`. -/
def creates_colimit_of_nat_iso {F G : C ⥤ D} (h : F ≅ G) [creates_colimit K F] :
creates_colimit K G :=
{ lifts := λ c t,
{ lifted_cocone :=
lift_colimit ((is_colimit.precompose_hom_equiv (iso_whisker_left K h : _) c).symm t),
valid_lift :=
begin
refine (is_colimit.map_cocone_equiv h _).unique_up_to_iso t,
apply is_colimit.of_iso_colimit _ ((lifted_colimit_maps_to_original _).symm),
apply (is_colimit.precompose_hom_equiv _ _).symm t,
end },
to_reflects_colimit := reflects_colimit_of_nat_iso _ h }
/-- If `F` creates colimits of shape `J` and `F ≅ G`, then `G` creates colimits of shape `J`. -/
def creates_colimits_of_shape_of_nat_iso {F G : C ⥤ D} (h : F ≅ G)
[creates_colimits_of_shape J F] : creates_colimits_of_shape J G :=
{ creates_colimit := λ K, creates_colimit_of_nat_iso h }
/-- If `F` creates colimits and `F ≅ G`, then `G` creates colimits. -/
def creates_colimits_of_nat_iso {F G : C ⥤ D} (h : F ≅ G) [creates_colimits F] :
creates_colimits G :=
{ creates_colimits_of_shape := λ J 𝒥₁, by exactI creates_colimits_of_shape_of_nat_iso h }
-- For the inhabited linter later.
/-- If F creates the limit of K, any cone lifts to a limit. -/
def lifts_to_limit_of_creates (K : J ⥤ C) (F : C ⥤ D)
[creates_limit K F] (c : cone (K ⋙ F)) (t : is_limit c) :
lifts_to_limit K F c t :=
{ lifted_cone := lift_limit t,
valid_lift := lifted_limit_maps_to_original t,
makes_limit := lifted_limit_is_limit t }
-- For the inhabited linter later.
/-- If F creates the colimit of K, any cocone lifts to a colimit. -/
def lifts_to_colimit_of_creates (K : J ⥤ C) (F : C ⥤ D)
[creates_colimit K F] (c : cocone (K ⋙ F)) (t : is_colimit c) :
lifts_to_colimit K F c t :=
{ lifted_cocone := lift_colimit t,
valid_lift := lifted_colimit_maps_to_original t,
makes_colimit := lifted_colimit_is_colimit t }
/-- Any cone lifts through the identity functor. -/
def id_lifts_cone (c : cone (K ⋙ 𝟭 C)) : liftable_cone K (𝟭 C) c :=
{ lifted_cone :=
{ X := c.X,
π := c.π ≫ K.right_unitor.hom },
valid_lift := cones.ext (iso.refl _) (by tidy) }
/-- The identity functor creates all limits. -/
instance id_creates_limits : creates_limits (𝟭 C) :=
{ creates_limits_of_shape := λ J 𝒥, by exactI
{ creates_limit := λ F, { lifts := λ c t, id_lifts_cone c } } }
/-- Any cocone lifts through the identity functor. -/
def id_lifts_cocone (c : cocone (K ⋙ 𝟭 C)) : liftable_cocone K (𝟭 C) c :=
{ lifted_cocone :=
{ X := c.X,
ι := K.right_unitor.inv ≫ c.ι },
valid_lift := cocones.ext (iso.refl _) (by tidy) }
/-- The identity functor creates all colimits. -/
instance id_creates_colimits : creates_colimits (𝟭 C) :=
{ creates_colimits_of_shape := λ J 𝒥, by exactI
{ creates_colimit := λ F, { lifts := λ c t, id_lifts_cocone c } } }
/-- Satisfy the inhabited linter -/
instance inhabited_liftable_cone (c : cone (K ⋙ 𝟭 C)) :
inhabited (liftable_cone K (𝟭 C) c) :=
⟨id_lifts_cone c⟩
instance inhabited_liftable_cocone (c : cocone (K ⋙ 𝟭 C)) :
inhabited (liftable_cocone K (𝟭 C) c) :=
⟨id_lifts_cocone c⟩
/-- Satisfy the inhabited linter -/
instance inhabited_lifts_to_limit (K : J ⥤ C) (F : C ⥤ D)
[creates_limit K F] (c : cone (K ⋙ F)) (t : is_limit c) :
inhabited (lifts_to_limit _ _ _ t) :=
⟨lifts_to_limit_of_creates K F c t⟩
instance inhabited_lifts_to_colimit (K : J ⥤ C) (F : C ⥤ D)
[creates_colimit K F] (c : cocone (K ⋙ F)) (t : is_colimit c) :
inhabited (lifts_to_colimit _ _ _ t) :=
⟨lifts_to_colimit_of_creates K F c t⟩
section comp
variables {E : Type u₃} [ℰ : category.{v} E]
variables (F : C ⥤ D) (G : D ⥤ E)
instance comp_creates_limit [creates_limit K F] [creates_limit (K ⋙ F) G] :
creates_limit K (F ⋙ G) :=
{ lifts := λ c t,
{ lifted_cone := lift_limit (lifted_limit_is_limit t),
valid_lift := (cones.functoriality (K ⋙ F) G).map_iso
(lifted_limit_maps_to_original (lifted_limit_is_limit t)) ≪≫
(lifted_limit_maps_to_original t) } }
instance comp_creates_limits_of_shape [creates_limits_of_shape J F] [creates_limits_of_shape J G] :
creates_limits_of_shape J (F ⋙ G) :=
{ creates_limit := infer_instance }
instance comp_creates_limits [creates_limits F] [creates_limits G] :
creates_limits (F ⋙ G) :=
{ creates_limits_of_shape := infer_instance }
instance comp_creates_colimit [creates_colimit K F] [creates_colimit (K ⋙ F) G] :
creates_colimit K (F ⋙ G) :=
{ lifts := λ c t,
{ lifted_cocone := lift_colimit (lifted_colimit_is_colimit t),
valid_lift := (cocones.functoriality (K ⋙ F) G).map_iso
(lifted_colimit_maps_to_original (lifted_colimit_is_colimit t)) ≪≫
(lifted_colimit_maps_to_original t) } }
instance comp_creates_colimits_of_shape
[creates_colimits_of_shape J F] [creates_colimits_of_shape J G] :
creates_colimits_of_shape J (F ⋙ G) :=
{ creates_colimit := infer_instance }
instance comp_creates_colimits [creates_colimits F] [creates_colimits G] :
creates_colimits (F ⋙ G) :=
{ creates_colimits_of_shape := infer_instance }
end comp
end creates
end category_theory
|
e2ad722f3c44256598999db7184c5264a65b3886 | 37da0369b6c03e380e057bf680d81e6c9fdf9219 | /hott/cubical/squareover.hlean | c3607ef9d9388082bd5272cce12f3a5d6511e1b8 | [
"Apache-2.0"
] | permissive | kodyvajjha/lean2 | 72b120d95c3a1d77f54433fa90c9810e14a931a4 | 227fcad22ab2bc27bb7471be7911075d101ba3f9 | refs/heads/master | 1,627,157,512,295 | 1,501,855,676,000 | 1,504,809,427,000 | 109,317,326 | 0 | 0 | null | 1,509,839,253,000 | 1,509,655,713,000 | C++ | UTF-8 | Lean | false | false | 17,443 | 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 sigma
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₂₁} {s₃₁ : square p₃₀ p₃₂ p₂₁ p₄₁}
{s₁₃ : square p₁₂ p₁₄ p₀₃ p₂₃} {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 (apd f p₁₀) (apd f p₁₂) (apd f p₀₁) (apd 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 {p₁₀'} {s : p₁₀ = p₁₀'} {q₁₀' : b₀₀ =[p₁₀'] b₂₀}
(r : change_path s q₁₀ = q₁₀')
: squareover B (vdeg_square s) q₁₀ q₁₀' idpo idpo :=
by induction s; esimp at *; induction r; exact vrflo
definition hdeg_squareover {p₀₁'} {s : p₀₁ = p₀₁'} {q₀₁' : b₀₀ =[p₀₁'] b₀₂}
(r : change_path s q₀₁ = q₀₁')
: squareover B (hdeg_square s) idpo idpo q₀₁ q₀₁' :=
by induction s; esimp at *; induction r; exact hrflo
definition hconcato
(t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) (t₃₁ : squareover B s₃₁ q₃₀ q₃₂ q₂₁ q₄₁)
: squareover B (hconcat s₁₁ s₃₁) (q₁₀ ⬝o q₃₀) (q₁₂ ⬝o q₃₂) q₀₁ q₄₁ :=
by induction t₃₁; exact t₁₁
definition vconcato
(t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) (t₁₃ : squareover B s₁₃ q₁₂ q₁₄ q₀₃ q₂₃)
: squareover B (vconcat s₁₁ s₁₃) q₁₀ q₁₄ (q₀₁ ⬝o q₀₃) (q₂₁ ⬝o q₂₃) :=
by induction t₁₃; exact t₁₁
definition hinverseo (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁)
: squareover B (hinverse s₁₁) q₁₀⁻¹ᵒ q₁₂⁻¹ᵒ q₂₁ q₀₁ :=
by induction t₁₁; constructor
definition vinverseo (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁)
: squareover B (vinverse s₁₁) q₁₂ q₁₀ q₀₁⁻¹ᵒ q₂₁⁻¹ᵒ :=
by induction t₁₁; constructor
definition eq_vconcato {q : b₀₀ =[p₁₀] b₂₀}
(r : q = q₁₀) (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) : squareover B s₁₁ q q₁₂ q₀₁ q₂₁ :=
by induction r; exact t₁₁
definition vconcato_eq {q : b₀₂ =[p₁₂] b₂₂}
(t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) (r : q₁₂ = q) : squareover B s₁₁ q₁₀ q q₀₁ q₂₁ :=
by induction r; exact t₁₁
definition eq_hconcato {q : b₀₀ =[p₀₁] b₀₂}
(r : q = q₀₁) (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) : squareover B s₁₁ q₁₀ q₁₂ q q₂₁ :=
by induction r; exact t₁₁
definition hconcato_eq {q : b₂₀ =[p₂₁] b₂₂}
(t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) (r : q₂₁ = q) : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q :=
by induction r; exact t₁₁
definition pathover_vconcato {p : a₀₀ = a₂₀} {sp : p = p₁₀} {q : b₀₀ =[p] b₂₀}
(r : change_path sp q = q₁₀) (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁)
: squareover B (sp ⬝pv s₁₁) q q₁₂ q₀₁ q₂₁ :=
by induction sp; induction r; exact t₁₁
definition vconcato_pathover {p : a₀₂ = a₂₂} {sp : p₁₂ = p} {q : b₀₂ =[p] b₂₂}
(t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) (r : change_path sp q₁₂ = q)
: squareover B (s₁₁ ⬝vp sp) q₁₀ q q₀₁ q₂₁ :=
by induction sp; induction r; exact t₁₁
definition pathover_hconcato {p : a₀₀ = a₀₂} {sp : p = p₀₁} {q : b₀₀ =[p] b₀₂}
(r : change_path sp q = q₀₁) (t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) :
squareover B (sp ⬝ph s₁₁) q₁₀ q₁₂ q q₂₁ :=
by induction sp; induction r; exact t₁₁
definition hconcato_pathover {p : a₂₀ = a₂₂} {sp : p₂₁ = p} {q : b₂₀ =[p] b₂₂}
(t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) (r : change_path sp q₂₁ = q) :
squareover B (s₁₁ ⬝hp sp) q₁₀ q₁₂ q₀₁ q :=
by induction sp; induction r; exact t₁₁
infix ` ⬝ho `:69 := hconcato --type using \tr
infix ` ⬝vo `:70 := vconcato --type using \tr
infix ` ⬝hop `:72 := hconcato_eq --type using \tr
infix ` ⬝vop `:74 := vconcato_eq --type using \tr
infix ` ⬝pho `:71 := eq_hconcato --type using \tr
infix ` ⬝pvo `:73 := eq_vconcato --type using \tr
-- 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
note 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 change_square {s₁₁'} (p : s₁₁ = s₁₁') (r : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁)
: squareover B s₁₁' q₁₀ q₁₂ q₀₁ q₂₁ :=
p ▸ r
/-
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
note 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
/- A version of eq_pathover where the type of the equality also varies -/
definition eq_pathover_dep {f g : Πa, B a} {p : a = a'} {q : f a = g a}
{r : f a' = g a'} (s : squareover B hrfl (pathover_idp_of_eq q) (pathover_idp_of_eq r)
(apd f p) (apd g p)) : q =[p] r :=
begin
induction p, apply pathover_idp_of_eq, apply eq_of_vdeg_square, exact square_of_squareover_ids s
end
/- 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 q p) r r₂
(pathover_ap B f (apd b p)) (pathover_ap B g (apd 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
definition squareover_change_path_left {p₀₁' : a₀₀ = a₀₂} (r : p₀₁' = p₀₁)
{q₀₁ : b₀₀ =[p₀₁'] b₀₂} (t : squareover B (r ⬝ph s₁₁) q₁₀ q₁₂ q₀₁ q₂₁)
: squareover B s₁₁ q₁₀ q₁₂ (change_path r q₀₁) q₂₁ :=
by induction r; exact t
definition squareover_change_path_right {p₂₁' : a₂₀ = a₂₂} (r : p₂₁' = p₂₁)
{q₂₁ : b₂₀ =[p₂₁'] b₂₂} (t : squareover B (s₁₁ ⬝hp r⁻¹) q₁₀ q₁₂ q₀₁ q₂₁)
: squareover B s₁₁ q₁₀ q₁₂ q₀₁ (change_path r q₂₁) :=
by induction r; exact t
definition squareover_change_path_right' {p₂₁' : a₂₀ = a₂₂} (r : p₂₁ = p₂₁')
{q₂₁ : b₂₀ =[p₂₁'] b₂₂} (t : squareover B (s₁₁ ⬝hp r) q₁₀ q₁₂ q₀₁ q₂₁)
: squareover B s₁₁ q₁₀ q₁₂ q₀₁ (change_path r⁻¹ q₂₁) :=
by induction r; exact t
/- You can construct a square in a sigma-type by giving a squareover -/
definition square_dpair_eq_dpair {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₀₂} {b₂₂ : B a₂₂}
{q₁₀ : b₀₀ =[p₁₀] b₂₀} {q₀₁ : b₀₀ =[p₀₁] b₀₂} {q₂₁ : b₂₀ =[p₂₁] b₂₂} {q₁₂ : b₀₂ =[p₁₂] b₂₂}
(t₁₁ : squareover B s₁₁ q₁₀ q₁₂ q₀₁ q₂₁) :
square (dpair_eq_dpair p₁₀ q₁₀) (dpair_eq_dpair p₁₂ q₁₂)
(dpair_eq_dpair p₀₁ q₀₁) (dpair_eq_dpair p₂₁ q₂₁) :=
by induction t₁₁; constructor
definition sigma_square {v₀₀ v₂₀ v₀₂ v₂₂ : Σa, B a}
{p₁₀ : v₀₀ = v₂₀} {p₀₁ : v₀₀ = v₀₂} {p₂₁ : v₂₀ = v₂₂} {p₁₂ : v₀₂ = v₂₂}
(s₁₁ : square p₁₀..1 p₁₂..1 p₀₁..1 p₂₁..1)
(t₁₁ : squareover B s₁₁ p₁₀..2 p₁₂..2 p₀₁..2 p₂₁..2) : square p₁₀ p₁₂ p₀₁ p₂₁ :=
begin
induction v₀₀, induction v₂₀, induction v₀₂, induction v₂₂,
rewrite [▸* at *, -sigma_eq_eta p₁₀, -sigma_eq_eta p₁₂, -sigma_eq_eta p₀₁, -sigma_eq_eta p₂₁],
exact square_dpair_eq_dpair s₁₁ t₁₁
end
end eq
|
fee6f9ba0b604a8dba6585c947698dffc0a42ac3 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/macro_args.lean | ca575552ee516f4f7a8860c2e5d01520a05c61cf | [
"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 | 73 | lean | #eval to_string $ ``({pos . line := has_zero.zero, col := 1}).to_raw_fmt
|
5fd1c1a4cb173df64474b63f19f4f2e59ab2bdec | 592ee40978ac7604005a4e0d35bbc4b467389241 | /Library/generated/mathscheme-lean/Zero0.lean | 674c2d4b2188ff66f4a984cf39e92e17f297a298 | [] | 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,820 | 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 Zero0
structure Zero0 (A : Type) : Type :=
(zero : A)
(times : (A → (A → A)))
(leftZero_op_zero : (∀ {x : A} , (times zero x) = zero))
(rightZero_op_zero : (∀ {x : A} , (times x zero) = zero))
open Zero0
structure Sig (AS : Type) : Type :=
(zeroS : AS)
(timesS : (AS → (AS → AS)))
structure Product (A : Type) : Type :=
(zeroP : (Prod A A))
(timesP : ((Prod A A) → ((Prod A A) → (Prod A A))))
(leftZero_op_0P : (∀ {xP : (Prod A A)} , (timesP zeroP xP) = zeroP))
(rightZero_op_0P : (∀ {xP : (Prod A A)} , (timesP xP zeroP) = zeroP))
structure Hom {A1 : Type} {A2 : Type} (Ze1 : (Zero0 A1)) (Ze2 : (Zero0 A2)) : Type :=
(hom : (A1 → A2))
(pres_zero : (hom (zero Ze1)) = (zero Ze2))
(pres_times : (∀ {x1 x2 : A1} , (hom ((times Ze1) x1 x2)) = ((times Ze2) (hom x1) (hom x2))))
structure RelInterp {A1 : Type} {A2 : Type} (Ze1 : (Zero0 A1)) (Ze2 : (Zero0 A2)) : Type 1 :=
(interp : (A1 → (A2 → Type)))
(interp_zero : (interp (zero Ze1) (zero Ze2)))
(interp_times : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((times Ze1) x1 x2) ((times Ze2) y1 y2))))))
inductive Zero0LTerm : Type
| zeroL : Zero0LTerm
| timesL : (Zero0LTerm → (Zero0LTerm → Zero0LTerm))
open Zero0LTerm
inductive ClZero0ClTerm (A : Type) : Type
| sing : (A → ClZero0ClTerm)
| zeroCl : ClZero0ClTerm
| timesCl : (ClZero0ClTerm → (ClZero0ClTerm → ClZero0ClTerm))
open ClZero0ClTerm
inductive OpZero0OLTerm (n : ℕ) : Type
| v : ((fin n) → OpZero0OLTerm)
| zeroOL : OpZero0OLTerm
| timesOL : (OpZero0OLTerm → (OpZero0OLTerm → OpZero0OLTerm))
open OpZero0OLTerm
inductive OpZero0OL2Term2 (n : ℕ) (A : Type) : Type
| v2 : ((fin n) → OpZero0OL2Term2)
| sing2 : (A → OpZero0OL2Term2)
| zeroOL2 : OpZero0OL2Term2
| timesOL2 : (OpZero0OL2Term2 → (OpZero0OL2Term2 → OpZero0OL2Term2))
open OpZero0OL2Term2
def simplifyCl {A : Type} : ((ClZero0ClTerm A) → (ClZero0ClTerm A))
| zeroCl := zeroCl
| (timesCl x1 x2) := (timesCl (simplifyCl x1) (simplifyCl x2))
| (sing x1) := (sing x1)
def simplifyOpB {n : ℕ} : ((OpZero0OLTerm n) → (OpZero0OLTerm n))
| zeroOL := zeroOL
| (timesOL x1 x2) := (timesOL (simplifyOpB x1) (simplifyOpB x2))
| (v x1) := (v x1)
def simplifyOp {n : ℕ} {A : Type} : ((OpZero0OL2Term2 n A) → (OpZero0OL2Term2 n A))
| zeroOL2 := zeroOL2
| (timesOL2 x1 x2) := (timesOL2 (simplifyOp x1) (simplifyOp x2))
| (v2 x1) := (v2 x1)
| (sing2 x1) := (sing2 x1)
def evalB {A : Type} : ((Zero0 A) → (Zero0LTerm → A))
| Ze zeroL := (zero Ze)
| Ze (timesL x1 x2) := ((times Ze) (evalB Ze x1) (evalB Ze x2))
def evalCl {A : Type} : ((Zero0 A) → ((ClZero0ClTerm A) → A))
| Ze (sing x1) := x1
| Ze zeroCl := (zero Ze)
| Ze (timesCl x1 x2) := ((times Ze) (evalCl Ze x1) (evalCl Ze x2))
def evalOpB {A : Type} {n : ℕ} : ((Zero0 A) → ((vector A n) → ((OpZero0OLTerm n) → A)))
| Ze vars (v x1) := (nth vars x1)
| Ze vars zeroOL := (zero Ze)
| Ze vars (timesOL x1 x2) := ((times Ze) (evalOpB Ze vars x1) (evalOpB Ze vars x2))
def evalOp {A : Type} {n : ℕ} : ((Zero0 A) → ((vector A n) → ((OpZero0OL2Term2 n A) → A)))
| Ze vars (v2 x1) := (nth vars x1)
| Ze vars (sing2 x1) := x1
| Ze vars zeroOL2 := (zero Ze)
| Ze vars (timesOL2 x1 x2) := ((times Ze) (evalOp Ze vars x1) (evalOp Ze vars x2))
def inductionB {P : (Zero0LTerm → Type)} : ((P zeroL) → ((∀ (x1 x2 : Zero0LTerm) , ((P x1) → ((P x2) → (P (timesL x1 x2))))) → (∀ (x : Zero0LTerm) , (P x))))
| p0l ptimesl zeroL := p0l
| p0l ptimesl (timesL x1 x2) := (ptimesl _ _ (inductionB p0l ptimesl x1) (inductionB p0l ptimesl x2))
def inductionCl {A : Type} {P : ((ClZero0ClTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((P zeroCl) → ((∀ (x1 x2 : (ClZero0ClTerm A)) , ((P x1) → ((P x2) → (P (timesCl x1 x2))))) → (∀ (x : (ClZero0ClTerm A)) , (P x)))))
| psing p0cl ptimescl (sing x1) := (psing x1)
| psing p0cl ptimescl zeroCl := p0cl
| psing p0cl ptimescl (timesCl x1 x2) := (ptimescl _ _ (inductionCl psing p0cl ptimescl x1) (inductionCl psing p0cl ptimescl x2))
def inductionOpB {n : ℕ} {P : ((OpZero0OLTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((P zeroOL) → ((∀ (x1 x2 : (OpZero0OLTerm n)) , ((P x1) → ((P x2) → (P (timesOL x1 x2))))) → (∀ (x : (OpZero0OLTerm n)) , (P x)))))
| pv p0ol ptimesol (v x1) := (pv x1)
| pv p0ol ptimesol zeroOL := p0ol
| pv p0ol ptimesol (timesOL x1 x2) := (ptimesol _ _ (inductionOpB pv p0ol ptimesol x1) (inductionOpB pv p0ol ptimesol x2))
def inductionOp {n : ℕ} {A : Type} {P : ((OpZero0OL2Term2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((P zeroOL2) → ((∀ (x1 x2 : (OpZero0OL2Term2 n A)) , ((P x1) → ((P x2) → (P (timesOL2 x1 x2))))) → (∀ (x : (OpZero0OL2Term2 n A)) , (P x))))))
| pv2 psing2 p0ol2 ptimesol2 (v2 x1) := (pv2 x1)
| pv2 psing2 p0ol2 ptimesol2 (sing2 x1) := (psing2 x1)
| pv2 psing2 p0ol2 ptimesol2 zeroOL2 := p0ol2
| pv2 psing2 p0ol2 ptimesol2 (timesOL2 x1 x2) := (ptimesol2 _ _ (inductionOp pv2 psing2 p0ol2 ptimesol2 x1) (inductionOp pv2 psing2 p0ol2 ptimesol2 x2))
def stageB : (Zero0LTerm → (Staged Zero0LTerm))
| zeroL := (Now zeroL)
| (timesL x1 x2) := (stage2 timesL (codeLift2 timesL) (stageB x1) (stageB x2))
def stageCl {A : Type} : ((ClZero0ClTerm A) → (Staged (ClZero0ClTerm A)))
| (sing x1) := (Now (sing x1))
| zeroCl := (Now zeroCl)
| (timesCl x1 x2) := (stage2 timesCl (codeLift2 timesCl) (stageCl x1) (stageCl x2))
def stageOpB {n : ℕ} : ((OpZero0OLTerm n) → (Staged (OpZero0OLTerm n)))
| (v x1) := (const (code (v x1)))
| zeroOL := (Now zeroOL)
| (timesOL x1 x2) := (stage2 timesOL (codeLift2 timesOL) (stageOpB x1) (stageOpB x2))
def stageOp {n : ℕ} {A : Type} : ((OpZero0OL2Term2 n A) → (Staged (OpZero0OL2Term2 n A)))
| (sing2 x1) := (Now (sing2 x1))
| (v2 x1) := (const (code (v2 x1)))
| zeroOL2 := (Now zeroOL2)
| (timesOL2 x1 x2) := (stage2 timesOL2 (codeLift2 timesOL2) (stageOp x1) (stageOp x2))
structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type :=
(zeroT : (Repr A))
(timesT : ((Repr A) → ((Repr A) → (Repr A))))
end Zero0 |
ee3004e46027d91ce86965da561a70a134532e18 | 556aeb81a103e9e0ac4e1fe0ce1bc6e6161c3c5e | /src/starkware/cairo/common/cairo_secp/signature_spec.lean | 1d3b0778d1367677e6579e784a2be39298fef42c | [] | permissive | starkware-libs/formal-proofs | d6b731604461bf99e6ba820e68acca62a21709e8 | f5fa4ba6a471357fd171175183203d0b437f6527 | refs/heads/master | 1,691,085,444,753 | 1,690,507,386,000 | 1,690,507,386,000 | 410,476,629 | 32 | 9 | Apache-2.0 | 1,690,506,773,000 | 1,632,639,790,000 | Lean | UTF-8 | Lean | false | false | 25,630 | lean | /-
Specifications file for signature_spec.cairo
Do not modify the constant definitions, structure definitions, or automatic specifications.
Do not change the name or arguments of the user specifications and soundness theorems.
You may freely move the definitions around in the file.
You may add definitions and theorems wherever you wish in this file.
-/
import starkware.cairo.lean.semantics.soundness.prelude
import starkware.cairo.common.cairo_secp.ec_spec
import starkware.cairo.common.cairo_secp.bigint_spec
import starkware.cairo.common.cairo_secp.constants_spec
import starkware.cairo.common.cairo_secp.field_spec
import starkware.cairo.common.math_spec
open starkware.cairo.common.cairo_secp.ec
open starkware.cairo.common.cairo_secp.bigint
open starkware.cairo.common.cairo_secp.constants
open starkware.cairo.common.cairo_secp.field
open starkware.cairo.common.math
namespace starkware.cairo.common.cairo_secp.signature
variables {F : Type} [field F] [decidable_eq F] [prelude_hyps F]
--variables {secpF : Type} [secp_field secpF]
-- End of automatically generated prelude.
/-
-- Function: get_generator_point
-/
/- get_generator_point autogenerated specification -/
def gen_point (F : Type) [field F] : EcPoint F :=
⟨⟨17117865558768631194064792,
12501176021340589225372855,
9198697782662356105779718⟩,
⟨6441780312434748884571320,
57953919405111227542741658,
5457536640262350763842127⟩⟩
def gen_point_data (F : Type) [field F] [decidable_eq F] [prelude_hyps F]
(secpF : Type) [secp_field secpF]:
BddECPointData secpF (gen_point F) :=
{ ix := ⟨17117865558768631194064792,
12501176021340589225372855,
9198697782662356105779718⟩,
iy := ⟨6441780312434748884571320,
57953919405111227542741658,
5457536640262350763842127⟩,
ixbdd := begin
rw [bigint3.bounded, BASE], simp_int_casts,
repeat { rw [abs_of_nonneg] },
repeat { norm_num }
end,
iybdd := begin
rw [bigint3.bounded, BASE], simp_int_casts,
repeat { rw [abs_of_nonneg] },
repeat { norm_num }
end,
ptxeq := by { rw [gen_point, bigint3.toBigInt3], simp_int_casts, exact ⟨rfl, rfl, rfl⟩ },
ptyeq := by { rw [gen_point, bigint3.toBigInt3], simp_int_casts, exact ⟨rfl, rfl, rfl⟩ },
onEC :=
begin
right,
rw [←int.cast_pow, ←int.cast_pow, (show (7 : secpF) = (7 : int), by simp_int_casts),
←int.cast_add, char_p.int_coe_eq_int_coe_iff secpF SECP_PRIME, bigint3.val, bigint3.val,
SECP_PRIME_eq, int.modeq, BASE, SECP_REM],
dsimp, simp_int_casts, norm_num,
end }
-- Do not change this definition.
def auto_spec_get_generator_point (mem : F → F) (κ : ℕ) (ρ_point : EcPoint F) : Prop :=
7 ≤ κ ∧
ρ_point = {
x := { d0 := 17117865558768631194064792, d1 := 12501176021340589225372855, d2 := 9198697782662356105779718 },
y := { d0 := 6441780312434748884571320, d1 := 57953919405111227542741658, d2 := 5457536640262350763842127 }
}
-- You may change anything in this definition except the name and arguments.
def spec_get_generator_point (mem : F → F) (κ : ℕ) (ρ_point : EcPoint F) : Prop :=
ρ_point = gen_point F
/- get_generator_point soundness theorem -/
-- Do not change the statement of this theorem. You may change the proof.
theorem sound_get_generator_point
{mem : F → F}
(κ : ℕ)
(ρ_point : EcPoint F)
(h_auto : auto_spec_get_generator_point mem κ ρ_point) :
spec_get_generator_point mem κ ρ_point :=
begin
exact h_auto.2
end
-- Function: div_mod_n
/- div_mod_n autogenerated specification -/
-- Do not change this definition.
def auto_spec_div_mod_n (mem : F → F) (κ : ℕ) (range_check_ptr : F) (a b : BigInt3 F) (ρ_range_check_ptr : F) (ρ_res : BigInt3 F) : Prop :=
∃ (κ₁ : ℕ) (range_check_ptr₁ : F) (res : BigInt3 F), spec_nondet_bigint3 mem κ₁ range_check_ptr range_check_ptr₁ res ∧
∃ (κ₂ : ℕ) (range_check_ptr₂ : F) (k : BigInt3 F), spec_nondet_bigint3 mem κ₂ range_check_ptr₁ range_check_ptr₂ k ∧
∃ (κ₃ : ℕ) (res_b : UnreducedBigInt5 F), spec_bigint_mul mem κ₃ res b res_b ∧
∃ n : BigInt3 F, n = {
d0 := N0,
d1 := N1,
d2 := N2
} ∧
∃ (κ₄ : ℕ) (k_n : UnreducedBigInt5 F), spec_bigint_mul mem κ₄ k n k_n ∧
∃ carry1 : F, carry1 = (res_b.d0 - k_n.d0 - a.d0) / (BASE : ℤ) ∧
mem (range_check_ptr₂ + 0) = carry1 + 2 ^ 127 ∧
is_range_checked (rc_bound F) (carry1 + 2 ^ 127) ∧
∃ carry2 : F, carry2 = (res_b.d1 - k_n.d1 - a.d1 + carry1) / (BASE : ℤ) ∧
mem (range_check_ptr₂ + 1) = carry2 + 2 ^ 127 ∧
is_range_checked (rc_bound F) (carry2 + 2 ^ 127) ∧
∃ carry3 : F, carry3 = (res_b.d2 - k_n.d2 - a.d2 + carry2) / (BASE : ℤ) ∧
mem (range_check_ptr₂ + 2) = carry3 + 2 ^ 127 ∧
is_range_checked (rc_bound F) (carry3 + 2 ^ 127) ∧
∃ carry4 : F, carry4 = (res_b.d3 - k_n.d3 + carry3) / (BASE : ℤ) ∧
mem (range_check_ptr₂ + 3) = carry4 + 2 ^ 127 ∧
is_range_checked (rc_bound F) (carry4 + 2 ^ 127) ∧
res_b.d4 - k_n.d4 + carry4 = 0 ∧
∃ range_check_ptr₃ : F, range_check_ptr₃ = range_check_ptr₂ + 4 ∧
κ₁ + κ₂ + κ₃ + κ₄ + 48 ≤ κ ∧
ρ_range_check_ptr = range_check_ptr₃ ∧
ρ_res = res
-- You may change anything in this definition except the name and arguments.
def spec_div_mod_n (mem : F → F) (κ : ℕ) (range_check_ptr : F) (a b : BigInt3 F) (ρ_range_check_ptr : F) (ρ_res : BigInt3 F) : Prop :=
∀ ia : bigint3,
ia.bounded (3 * BASE - 1) →
a = ia.toBigInt3 →
∀ ib : bigint3,
ib.bounded (3 * BASE - 1) →
b = ib.toBigInt3 →
∃ ires : bigint3,
ires.bounded (3 * BASE - 1) ∧
ρ_res = ires.toBigInt3 ∧
ires.val * ib.val ≡ ia.val [ZMOD secp_n]
/- div_mod_n soundness theorem -/
def iN0 : ℤ := N0
def iN1 : ℤ := N1
def iN2 : ℤ := N2
def secp_in : bigint3 := ⟨iN0, iN1, iN2⟩
def nBASE := BASE
--theorem secp_in_toBigInt3 : secp_in.toBigInt3 = secp_n := rfl
-- Do not change the statement of this theorem. You may change the proof.
theorem sound_div_mod_n
{mem : F → F}
(κ : ℕ)
(range_check_ptr : F) (a b : BigInt3 F) (ρ_range_check_ptr : F) (ρ_res : BigInt3 F)
(h_auto : auto_spec_div_mod_n mem κ range_check_ptr a b ρ_range_check_ptr ρ_res) :
spec_div_mod_n mem κ range_check_ptr a b ρ_range_check_ptr ρ_res :=
begin
intros ia iabdd aeq ib ibbdd beq,
rcases h_auto with ⟨_, _, res, hres, _, _, k, hk, _, res_b, hres_b, n, neq, _, k_n, hk_n,
carry1, carry1eq, _, hcarry1,
carry2, carry2eq, _, hcarry2,
carry3, carry3eq, _, hcarry3,
carry4, carry4eq, _, hcarry4,
diff_eq, _, _, _, _, ρ_res_eq⟩,
rcases nondet_bigint3_corr hres with ⟨ires, reseq, ires_bdd⟩,
rcases nondet_bigint3_corr hk with ⟨ik, keq, ik_bdd⟩,
change _ = _ at hres_b,
change _ = _ at hk_n,
rcases rc_to_int hcarry1 with ⟨icarry1, carry1eq', carry1_bdd⟩,
rcases rc_to_int hcarry2 with ⟨icarry2, carry2eq', carry2_bdd⟩,
rcases rc_to_int hcarry3 with ⟨icarry3, carry3eq', carry3_bdd⟩,
rcases rc_to_int hcarry4 with ⟨icarry4, carry4eq', carry4_bdd⟩,
have BASEnonneg : 0 ≤ (BASE : ℤ) := int.coe_zero_le BASE,
have iN0nonneg : 0 ≤ iN0 := int.coe_zero_le _,
have iN1nonneg : 0 ≤ iN1 := int.coe_zero_le _,
have iN2nonneg : 0 ≤ iN2 := int.coe_zero_le _,
have absBASEeq : abs (BASE : ℤ) = BASE := abs_of_nonneg BASEnonneg,
have BASEnz : ((BASE : ℤ) : F) ≠ 0,
{ suffices : ((2 : ℤ) : F) ≠ 0,
by simpa using this,
haveI : char_p F PRIME := prelude_hyps.charF,
rw [ne, char_p.int_cast_eq_zero_iff F PRIME, PRIME],
simp_int_casts, norm_num },
have aux3 : ∀ {x y : F}, x = y / (BASE : ℤ) → x * 2 ^ 86 = y,
{ intros x y xeq,
rw [eq_div_iff BASEnz, int.cast_coe_nat, BASE] at xeq,
simp only [nat.cast_bit0, nat.cast_one, nat.cast_pow] at xeq,
exact xeq },
have secp_in_toBigInt3 : secp_in.toBigInt3 = n,
{ rw [neq, bigint3.toBigInt3, secp_in], dsimp,
rw [iN0, iN1, iN2, int.cast_coe_nat, int.cast_coe_nat, int.cast_coe_nat] },
have : (((((ires.bigint5_mul ib).sub (ik.bigint5_mul secp_in)).sub ia.to_bigint5).add
⟨0, icarry1, icarry2, icarry3, 0⟩).toUnreducedBigInt5 : UnreducedBigInt5 F) =
bigint5.toUnreducedBigInt5
(⟨icarry1*BASE, icarry2*BASE, icarry3*BASE, icarry4*BASE, -icarry4⟩),
{ rw [bigint5.toUnreducedBigInt5_add, bigint5.toUnreducedBigInt5_sub,
bigint5.toUnreducedBigInt5_sub, bigint3.bigint5_mul_toUnreducedBigInt5,
bigint3.bigint5_mul_toUnreducedBigInt5],
conv { to_rhs, rw [bigint5.toUnreducedBigInt5], simp },
rw [←carry1eq', aux3 carry1eq, ←carry2eq', aux3 carry2eq, ←carry3eq', aux3 carry3eq,
←carry4eq', aux3 carry4eq, ←reseq, ←keq, ←beq, ←hres_b, secp_in_toBigInt3, ←hk_n,
bigint3.to_bigint5_to_Unreduced_BigInt5, ←aeq],
simp [UnreducedBigInt5.sub, UnreducedBigInt5.add,
starkware.cairo.common.cairo_secp.bigint.bigint3.to_bigint5, bigint5.toUnreducedBigInt5,
BigInt3.toUnreducedBigInt5],
simp [carry1eq', carry2eq', carry3eq', eq_sub_of_add_eq diff_eq] },
have eq : ((((ires.bigint5_mul ib).sub (ik.bigint5_mul secp_in)).sub ia.to_bigint5).add
⟨0, icarry1, icarry2, icarry3, 0⟩) =
⟨icarry1*BASE, icarry2*BASE, icarry3*BASE, icarry4*BASE, -icarry4⟩,
{ apply bigint5.toUnreducedBigInt5_eq_of_sub_bounded this,
have : (2^127 : ℤ) * BASE + (3 * (3 * BASE - 1)^2 +
(3 * (3 * (BASE - 1))^2) + (3 * BASE - 1) + 2^127) ≤ PRIME - 1,
{ rw [PRIME, BASE], simp_int_casts, norm_num },
apply bigint5.bounded_of_bounded_of_le _ this,
apply bigint5.bounded_sub,
{ simp [bigint5.bounded, abs_mul, absBASEeq],
use [carry1_bdd, carry2_bdd, carry3_bdd, carry4_bdd],
apply le_trans carry4_bdd, norm_num },
apply bigint5.bounded_add, swap,
{ simp [bigint5.bounded],
use [carry1_bdd, carry2_bdd, carry3_bdd] },
apply bigint5.bounded_sub _ (bigint3.to_bigint5_bounded iabdd),
apply bigint5.bounded_sub,
apply bigint3.bounded_bigint5_mul (bigint3.bounded_of_bounded_of_le ires_bdd bound_slack) ibbdd,
apply bigint3.bounded_bigint5_mul ik_bdd,
rw [secp_in, bigint3.bounded], dsimp,
simp [abs_of_nonneg iN0nonneg, abs_of_nonneg iN1nonneg, abs_of_nonneg iN2nonneg],
simp [iN0, iN1, iN2],
norm_num, },
have : ires.val * ib.val = ik.val * secp_n + ia.val,
{ simp only [bigint3.val, secp_n, int.cast_add, int.coe_nat_add, int.coe_nat_mul],
rw [int.coe_nat_pow BASE 2],
set iBASE := (BASE : ℤ),
simp only [bigint3.bigint5_mul, bigint5.add, bigint5.sub, bigint3.to_bigint5, secp_in, add_zero, sub_zero] at eq,
rcases eq with ⟨eq1, eq2, eq3, eq4, eq5⟩,
rw [←iN0, ←iN1, ←iN2],
transitivity (ires.i2 * ib.i2 * iBASE * iBASE * iBASE * iBASE) +
(ires.i2 * iBASE ^ 2 * (ib.i1 * iBASE + ib.i0)) +
(ires.i1 * iBASE + ires.i0) * (ib.i2 * iBASE ^ 2 + ib.i1 * iBASE + ib.i0),
rw [pow_two], ring,
rw [eq_add_of_sub_eq eq5, add_mul, add_mul, add_mul],
rw [←neg_mul_eq_neg_mul, ←eq4, neg_add],
simp only [add_mul _ _ iBASE],
rw [←neg_mul_eq_neg_mul icarry3, ←eq3, neg_add],
simp only [add_mul _ _ iBASE],
rw [←neg_mul_eq_neg_mul icarry2, ←eq2, neg_add],
simp only [add_mul _ _ iBASE],
rw [←neg_mul_eq_neg_mul icarry1, ←eq1],
ring },
use ires,
split, apply bigint3.bounded_of_bounded_of_le ires_bdd bound_slack,
use ρ_res_eq.trans reseq,
rw [this, int.modeq, add_comm, int.add_mul_mod_self]
end
/-
-- Function: get_point_from_x
-/
/- get_point_from_x autogenerated specification -/
-- Do not change this definition.
def auto_spec_get_point_from_x (mem : F → F) (κ : ℕ) (range_check_ptr : F) (x : BigInt3 F) (v ρ_range_check_ptr : F) (ρ_point : EcPoint F) : Prop :=
∃ (κ₁ : ℕ) (range_check_ptr₁ : F), spec_assert_nn mem κ₁ range_check_ptr v range_check_ptr₁ ∧
∃ (κ₂ : ℕ) (x_square : UnreducedBigInt3 F), spec_unreduced_sqr mem κ₂ x x_square ∧
∃ (κ₃ : ℕ) (range_check_ptr₂ : F) (x_square_reduced : BigInt3 F), spec_reduce mem κ₃ range_check_ptr₁ x_square range_check_ptr₂ x_square_reduced ∧
∃ (κ₄ : ℕ) (x_cube : UnreducedBigInt3 F), spec_unreduced_mul mem κ₄ x x_square_reduced x_cube ∧
∃ (κ₅ : ℕ) (range_check_ptr₃ : F) (y : BigInt3 F), spec_nondet_bigint3 mem κ₅ range_check_ptr₂ range_check_ptr₃ y ∧
∃ (κ₆ : ℕ) (range_check_ptr₄ : F), spec_validate_reduced_field_element mem κ₆ range_check_ptr₃ y range_check_ptr₄ ∧
∃ (κ₇ : ℕ) (range_check_ptr₅ : F), spec_assert_nn mem κ₇ range_check_ptr₄ ((y.d0 + v) / (2 : ℤ)) range_check_ptr₅ ∧
∃ (κ₈ : ℕ) (y_square : UnreducedBigInt3 F), spec_unreduced_sqr mem κ₈ y y_square ∧
∃ (κ₉ : ℕ) (range_check_ptr₆ : F), spec_verify_zero mem κ₉ range_check_ptr₅ {
d0 := x_cube.d0 + BETA - y_square.d0,
d1 := x_cube.d1 - y_square.d1,
d2 := x_cube.d2 - y_square.d2
} range_check_ptr₆ ∧
κ₁ + κ₂ + κ₃ + κ₄ + κ₅ + κ₆ + κ₇ + κ₈ + κ₉ + 54 ≤ κ ∧
ρ_range_check_ptr = range_check_ptr₆ ∧
ρ_point = {
x := x,
y := y
}
theorem aux {a b c : ℕ} (h : a + b = 2 * c) : a % 2 = b % 2 :=
begin
have : a + 2 * b = 2 * c + b,
{ nth_rewrite 0 (two_mul b),
rw [←add_assoc, h] },
have h' := congr_arg (λ n : ℕ, n % 2) this,
dsimp at h',
rw [nat.add_mul_mod_self_left, add_comm, nat.add_mul_mod_self_left] at h',
exact h'
end
-- You may change anything in this definition except the name and arguments.
def spec_get_point_from_x (mem : F → F) (κ : ℕ) (range_check_ptr : F) (x : BigInt3 F) (v ρ_range_check_ptr : F) (ρ_point : EcPoint F) : Prop :=
∀ (secpF : Type) [hsecp : secp_field secpF],
by exactI
x ≠ ⟨0, 0, 0⟩ →
∀ ix : bigint3,
ix.bounded (3 * BASE - 1) →
x = ix.toBigInt3 →
∃ nv : ℕ,
nv < rc_bound F ∧
v = ↑nv ∧
∃ iyval : ℕ,
iyval < SECP_PRIME ∧
nv % 2 = iyval % 2 ∧
∃ h : @on_ec secpF _ (ix.val, iyval),
∃ hres : BddECPointData secpF ρ_point,
hres.toECPoint = ECPoint.AffinePoint ⟨_, _, h⟩
/- get_point_from_x soundness theorem -/
-- Do not change the statement of this theorem. You may change the proof.
theorem sound_get_point_from_x
{mem : F → F}
(κ : ℕ)
(range_check_ptr : F) (x : BigInt3 F) (v ρ_range_check_ptr : F) (ρ_point : EcPoint F)
(h_auto : auto_spec_get_point_from_x mem κ range_check_ptr x v ρ_range_check_ptr ρ_point) :
spec_get_point_from_x mem κ range_check_ptr x v ρ_range_check_ptr ρ_point :=
begin
intros secpF _,
resetI,
intros xnez ix ixbdd xeq,
rcases h_auto with
⟨_, _, h_nn_v,
_, x_square, h_x_square,
_, _, x_square_reduced, h_x_square_reduced,
_, x_cube, h_x_cube,
_, _, y, h_nondet_bigint3_y,
_, _, h_validate_reduced_y,
_, _, h_nn_y_v_div,
_, y_square, h_unreduced_y_square,
_, _, h_verify_zero,
_, _, ρ_point_eq⟩,
rcases h_nn_v with ⟨nv, nvlt, veq⟩,
have x_square_eq := h_x_square ix xeq,
have : ix.sqr.bounded (2 ^ 249),
{ apply bigint3.bounded_of_bounded_of_le
(bigint3.bounded_sqr ixbdd),
rw [BASE, SECP_REM], simp_int_casts, norm_num },
rcases h_x_square_reduced ix.sqr this x_square_eq with
⟨ixsqr', ixsqr'bdd, hixsqr', x_square_reduced_eq⟩,
have x_cube_eq := h_x_cube ix ixsqr' xeq x_square_reduced_eq,
rcases nondet_bigint3_corr h_nondet_bigint3_y with ⟨iy, yeq, iybdd⟩,
rcases h_validate_reduced_y with ⟨n0, n1, n2, n0le, n1le, n2le, yvallt, yeq'⟩,
rcases h_nn_y_v_div with ⟨nydiv, nydivlt, ydiveq⟩,
have : y.d0 + v = ↑(2 * nydiv),
{ rw [mul_comm, nat.cast_mul],
symmetry, simp,
rw [← (eq_div_iff (PRIME.two_ne_zero F)), ← ydiveq],
simp },
have : n0 + nv = 2 * nydiv,
{ rw [yeq', veq, ← nat.cast_add] at this,
apply nat.cast_inj_of_lt_char _ _ this,
apply lt_of_le_of_lt (add_le_add n0le (le_of_lt nvlt)),
apply lt_of_le_of_lt (add_le_add_left (rc_bound_hyp F) _),
rw [BASE, PRIME.char_eq, PRIME],
norm_num,
apply lt_of_le_of_lt,
apply nat.mul_le_mul_left 2
(le_trans (le_of_lt nydivlt) (rc_bound_hyp F)),
rw [PRIME.char_eq, PRIME], norm_num },
have n02eqmv2 : n0 % 2 = nv % 2 := aux this,
let iy' : bigint3 := ⟨n0, n1, n2⟩,
have iy'bdd : iy'.bounded (3 * ↑BASE - 1),
{ suffices : iy'.bounded BASE,
{ apply bigint3.bounded_of_bounded_of_le this,
rw BASE, simp_int_casts, norm_num },
simp only [bigint3.bounded, int.coe_nat_abs, int.coe_nat_le],
split, { apply le_trans n0le tsub_le_self },
split, { apply le_trans n1le tsub_le_self },
apply le_trans n2le,
norm_num [P2, BASE] },
have yeq'' : y = iy'.toBigInt3,
{ rw [yeq', bigint3.toBigInt3], dsimp [iy'],
simp only [int.cast_coe_nat, eq_self_iff_true, and_self] },
have y_square_eq := h_unreduced_y_square iy' yeq'',
let idiff := ((ix.mul ixsqr').add ⟨7,0,0⟩).sub iy'.sqr,
have hidiff1 : idiff.toUnreducedBigInt3 = ⟨x_cube.d0 + ↑BETA - y_square.d0, x_cube.d1 - y_square.d1, x_cube.d2 - y_square.d2⟩,
{ simp [idiff, BETA, bigint3.toUnreducedBigInt3_add, bigint3.toUnreducedBigInt3_sub, ←x_cube_eq, ←y_square_eq],
simp [bigint3.toUnreducedBigInt3, UnreducedBigInt3.add, UnreducedBigInt3.sub] },
have hidiff2 : idiff.bounded (2 ^ 250),
{ suffices : idiff.bounded ((3 * BASE - 1)^2 * (8 * SECP_REM + 1) + 7 +
(3 * BASE - 1)^2 * (8 * SECP_REM + 1)),
{ apply bigint3.bounded_of_bounded_of_le this,
rw [BASE, SECP_REM], simp_int_casts, norm_num },
apply bigint3.bounded_sub,
apply bigint3.bounded_add,
apply bigint3.bounded_mul ixbdd (bigint3.bounded_of_bounded_of_le ixsqr'bdd bound_slack),
{ simp [bigint3.bounded], rw abs_of_nonneg; norm_num },
apply bigint3.bounded_sqr iy'bdd },
have := h_verify_zero idiff hidiff1.symm hidiff2,
have h_on_ec : on_ec ((ix.val : secpF), iy'.val),
{ simp [on_ec],
suffices : (↑(iy'.val ^ 2) : secpF) = ↑(ix.val ^ 3 + 7),
{ simpa using this },
rw [char_p.int_coe_eq_int_coe_iff secpF SECP_PRIME, int.modeq],
symmetry,
rw [int.mod_eq_mod_iff_mod_sub_eq_zero, ←this],
dsimp [idiff],
rw [bigint3.sub_val, bigint3.add_val],
apply int.modeq.sub,
apply int.modeq.add,
rw [pow_succ],
apply int.modeq.symm,
apply int.modeq.trans,
apply bigint3.mul_val,
apply int.modeq.mul_left,
apply int.modeq.trans hixsqr',
apply bigint3.sqr_val,
{ simp [bigint3.val] },
apply int.modeq.symm,
apply bigint3.sqr_val },
have iy'valeq : (↑(n2 * BASE ^ 2 + n1 * BASE + n0) : secpF) = iy'.val,
{ dsimp [iy'], simp [bigint3.val] },
use [nv, nvlt, veq, n2 * BASE ^ 2 + n1 * BASE + n0, yvallt],
split,
{ rw [←n02eqmv2, nat.add_mod, nat.add_mod (n2 * _), nat.mul_mod, BASE],
norm_num,
rw [nat.add_mod, nat.mul_mod],
norm_num },
rw iy'valeq,
use h_on_ec,
rw ρ_point_eq,
use ⟨ix, iy', ixbdd, iy'bdd, xeq, yeq'', or.inr h_on_ec⟩,
simp [BddECPointData.toECPoint, dif_neg xnez]
end
/-
-- Function: recover_public_key
-/
/- recover_public_key autogenerated specification -/
-- Do not change this definition.
def auto_spec_recover_public_key (mem : F → F) (κ : ℕ) (range_check_ptr : F) (msg_hash r s : BigInt3 F) (v ρ_range_check_ptr : F) (ρ_public_key_point : EcPoint F) : Prop :=
∃ (κ₁ : ℕ) (range_check_ptr₁ : F) (r_point : EcPoint F), spec_get_point_from_x mem κ₁ range_check_ptr r v range_check_ptr₁ r_point ∧
∃ (κ₂ : ℕ) (generator_point : EcPoint F), spec_get_generator_point mem κ₂ generator_point ∧
∃ (κ₃ : ℕ) (range_check_ptr₂ : F) (u1 : BigInt3 F), spec_div_mod_n mem κ₃ range_check_ptr₁ msg_hash r range_check_ptr₂ u1 ∧
∃ (κ₄ : ℕ) (range_check_ptr₃ : F) (u2 : BigInt3 F), spec_div_mod_n mem κ₄ range_check_ptr₂ s r range_check_ptr₃ u2 ∧
∃ (κ₅ : ℕ) (range_check_ptr₄ : F) (point1 : EcPoint F), spec_ec_mul mem κ₅ range_check_ptr₃ generator_point u1 range_check_ptr₄ point1 ∧
∃ (κ₆ : ℕ) (range_check_ptr₅ : F) (minus_point1 : EcPoint F), spec_ec_negate mem κ₆ range_check_ptr₄ point1 range_check_ptr₅ minus_point1 ∧
∃ (κ₇ : ℕ) (range_check_ptr₆ : F) (point2 : EcPoint F), spec_ec_mul mem κ₇ range_check_ptr₅ r_point u2 range_check_ptr₆ point2 ∧
∃ (κ₈ : ℕ) (range_check_ptr₇ : F) (public_key_point : EcPoint F), spec_ec_add mem κ₈ range_check_ptr₆ minus_point1 point2 range_check_ptr₇ public_key_point ∧
κ₁ + κ₂ + κ₃ + κ₄ + κ₅ + κ₆ + κ₇ + κ₈ + 77 ≤ κ ∧
ρ_range_check_ptr = range_check_ptr₇ ∧
ρ_public_key_point = public_key_point
-- You may change anything in this definition except the name and arguments.
def spec_recover_public_key (mem : F → F) (κ : ℕ) (range_check_ptr : F) (msg_hash r s : BigInt3 F) (v ρ_range_check_ptr : F) (ρ_public_key_point : EcPoint F) : Prop :=
∀ (secpF : Type) [secp_field secpF], by exactI
r ≠ ⟨0, 0, 0⟩ →
∀ ir : bigint3,
ir.bounded (3 * BASE - 1) →
r = ir.toBigInt3 →
∀ is : bigint3,
is.bounded (3 * BASE - 1) →
s = is.toBigInt3 →
∀ imsg : bigint3,
imsg.bounded (3 * BASE - 1) →
msg_hash = imsg.toBigInt3 →
∃ nv : ℕ,
nv < rc_bound F ∧
v = ↑nv ∧
∃ iu1 iu2 : ℤ,
iu1 * ir.val ≡ imsg.val [ZMOD secp_n] ∧
iu2 * ir.val ≡ is.val [ZMOD secp_n] ∧
∃ ny : ℕ,
ny < SECP_PRIME ∧
nv ≡ ny [MOD 2] ∧
∃ h_on_ec : @on_ec secpF _ (ir.val, ny),
∃ hpoint : BddECPointData secpF ρ_public_key_point,
hpoint.toECPoint =
-(iu1 • (gen_point_data F secpF).toECPoint) +
iu2 • ECPoint.AffinePoint ⟨ir.val, ny, h_on_ec⟩
/- recover_public_key soundness theorem -/
-- Do not change the statement of this theorem. You may change the proof.
theorem sound_recover_public_key
{mem : F → F}
(κ : ℕ)
(range_check_ptr : F) (msg_hash r s : BigInt3 F) (v ρ_range_check_ptr : F) (ρ_public_key_point : EcPoint F)
(h_auto : auto_spec_recover_public_key mem κ range_check_ptr msg_hash r s v ρ_range_check_ptr ρ_public_key_point) :
spec_recover_public_key mem κ range_check_ptr msg_hash r s v ρ_range_check_ptr ρ_public_key_point :=
begin
intros secpF _ rnez ir irbdd req is isbdd iseq imsg imsgbdd msgeq,
resetI,
rcases h_auto with ⟨_, _, r_point, hr_point,
_, generator, hgenerator,
_, _, u1, hu1,
_, _, u2, hu2,
_, _, point1, hpoint1,
_, _, minus_point1, hminus_point1,
_, _, point2, hpoint2,
_, _, public_key_point, hpublic_key_point,
_, _, rfl⟩,
rcases hr_point secpF rnez ir irbdd req with ⟨nv, nvlt, veq, iy, iybdd, iy2, hiy, hr_point', ireq⟩,
rw spec_get_generator_point at hgenerator,
subst hgenerator,
rcases hu1 imsg imsgbdd msgeq ir irbdd req with ⟨iu1, iu1bdd, u1eq, hu1'⟩,
rcases hu2 is isbdd iseq ir irbdd req with ⟨iu2, iu2bdd, u2eq, hu2'⟩,
have : (gen_point F).x ≠ ⟨0, 0, 0⟩,
{ simp [gen_point],
intros eq _ _,
suffices : (17117865558768631194064792 : ℤ) = 0,
{ norm_num at this },
haveI : char_p F PRIME := prelude_hyps.charF,
apply int.cast_eq_zero_of_lt_char F PRIME,
{ simp_int_casts, exact eq },
rw [abs_of_nonneg, PRIME],
simp_int_casts, norm_num, norm_num },
rcases hpoint1 secpF this (gen_point_data F secpF) with
⟨n10, n10lt, n10eq, n11, n11lt, n11eq, n12, n12lt, n12eq, hpoint1', hpoint1eq⟩,
rcases spec_ec_negate'_of_spec_ec_negate hminus_point1 secpF hpoint1' with
⟨hminus_point1', hminus_point1_eq⟩,
have : r_point.x ≠ ⟨0, 0, 0⟩,
{ intro hcontr,
rw [BddECPointData.toECPoint, dif_pos hcontr] at ireq,
contradiction },
rcases hpoint2 secpF this hr_point' with
⟨n20, n20lt, n20eq, n21, n21lt, n21eq, n22, n22lt, n22eq, hpoint2', hpoint2eq⟩,
rcases hpublic_key_point secpF hminus_point1' hpoint2' with ⟨hret, hreteq⟩,
refine ⟨nv, nvlt, veq, _, _, hu1', hu2', iy, iybdd, iy2, hiy, hret, _⟩,
rw [hreteq, hminus_point1_eq, hpoint1eq, hpoint2eq, ireq],
have aux : ∀ {i : ℤ} {n : ℕ}, abs i ≤ 3 * BASE - 1 → n < 2^86 → (i : F) = (n : F) → i = n,
{ intros i n hi hn hin,
have : (i : F) = ((n : ℤ) : F),
{ rw hin, simp },
apply PRIME.int_coe_inj this,
have : (2^86 : ℤ) + (3 * BASE - 1) < PRIME,
{ simp only [PRIME], simp_int_casts, norm_num },
apply lt_of_le_of_lt _ this,
apply le_trans,
apply abs_sub,
apply add_le_add _ hi,
rw abs_of_nonneg (int.coe_zero_le _),
apply le_of_lt,
norm_cast, exact hn },
simp [u1eq, u2eq, bigint3.toBigInt3] at n10eq n11eq n12eq n20eq n21eq n22eq,
have : iu1.val = ↑(2 ^ 172 * n12 + 2 ^ 86 * n11 + n10),
{ rw [bigint3.val], simp,
rw [aux iu1bdd.1 n10lt n10eq, aux iu1bdd.2.1 n11lt n11eq,
aux iu1bdd.2.2 (lt_of_lt_of_le n12lt (by norm_num)) n12eq ],
ring },
rw [this, nsmul_eq_smul_cast ℤ],
have : iu2.val = ↑(2 ^ 172 * n22 + 2 ^ 86 * n21 + n20),
{ rw [bigint3.val], simp,
rw [aux iu2bdd.1 n20lt n20eq, aux iu2bdd.2.1 n21lt n21eq,
aux iu2bdd.2.2 (lt_of_lt_of_le n22lt (by norm_num)) n22eq ],
ring },
rw [this, nsmul_eq_smul_cast ℤ]
end
end starkware.cairo.common.cairo_secp.signature
|
c5303536aafdbbfeebe975fe5708e48192d5ce61 | 649957717d58c43b5d8d200da34bf374293fe739 | /src/category_theory/comma.lean | 16f46944ec77809165ccb2d489537cb997cfb812 | [
"Apache-2.0"
] | permissive | Vtec234/mathlib | b50c7b21edea438df7497e5ed6a45f61527f0370 | fb1848bbbfce46152f58e219dc0712f3289d2b20 | refs/heads/master | 1,592,463,095,113 | 1,562,737,749,000 | 1,562,737,749,000 | 196,202,858 | 0 | 0 | Apache-2.0 | 1,562,762,338,000 | 1,562,762,337,000 | null | UTF-8 | Lean | false | false | 11,458 | lean | -- Copyright (c) 2018 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Scott Morrison, Johan Commelin
import category_theory.isomorphism
import category_theory.punit
namespace category_theory
universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation
variables {A : Type u₁} [𝒜 : category.{v₁} A]
variables {B : Type u₂} [ℬ : category.{v₂} B]
variables {T : Type u₃} [𝒯 : category.{v₃} T]
include 𝒜 ℬ 𝒯
structure comma (L : A ⥤ T) (R : B ⥤ T) : Type (max u₁ u₂ v₃) :=
(left : A . obviously)
(right : B . obviously)
(hom : L.obj left ⟶ R.obj right)
variables {L : A ⥤ T} {R : B ⥤ T}
structure comma_morphism (X Y : comma L R) :=
(left : X.left ⟶ Y.left . obviously)
(right : X.right ⟶ Y.right . obviously)
(w' : L.map left ≫ Y.hom = X.hom ≫ R.map right . obviously)
restate_axiom comma_morphism.w'
attribute [simp] comma_morphism.w
namespace comma_morphism
@[extensionality] lemma ext
{X Y : comma L R} {f g : comma_morphism X Y}
(l : f.left = g.left) (r : f.right = g.right) : f = g :=
begin
cases f, cases g,
congr; assumption
end
end comma_morphism
instance comma_category : category (comma L R) :=
{ hom := comma_morphism,
id := λ X,
{ left := 𝟙 X.left,
right := 𝟙 X.right },
comp := λ X Y Z f g,
{ left := f.left ≫ g.left,
right := f.right ≫ g.right,
w' :=
begin
rw [functor.map_comp,
category.assoc,
g.w,
←category.assoc,
f.w,
functor.map_comp,
category.assoc],
end }}
namespace comma
section
variables {X Y Z : comma L R} {f : X ⟶ Y} {g : Y ⟶ Z}
@[simp] lemma comp_left : (f ≫ g).left = f.left ≫ g.left := rfl
@[simp] lemma comp_right : (f ≫ g).right = f.right ≫ g.right := rfl
end
variables (L) (R)
def fst : comma L R ⥤ A :=
{ obj := λ X, X.left,
map := λ _ _ f, f.left }
def snd : comma L R ⥤ B :=
{ obj := λ X, X.right,
map := λ _ _ f, f.right }
@[simp] lemma fst_obj {X : comma L R} : (fst L R).obj X = X.left := rfl
@[simp] lemma snd_obj {X : comma L R} : (snd L R).obj X = X.right := rfl
@[simp] lemma fst_map {X Y : comma L R} {f : X ⟶ Y} : (fst L R).map f = f.left := rfl
@[simp] lemma snd_map {X Y : comma L R} {f : X ⟶ Y} : (snd L R).map f = f.right := rfl
def nat_trans : fst L R ⋙ L ⟶ snd L R ⋙ R :=
{ app := λ X, X.hom }
section
variables {L₁ L₂ L₃ : A ⥤ T} {R₁ R₂ R₃ : B ⥤ T}
def map_left (l : L₁ ⟶ L₂) : comma L₂ R ⥤ comma L₁ R :=
{ obj := λ X,
{ left := X.left,
right := X.right,
hom := l.app X.left ≫ X.hom },
map := λ X Y f,
{ left := f.left,
right := f.right,
w' := by tidy; rw [←category.assoc, l.naturality f.left, category.assoc]; tidy } }
section
variables {X Y : comma L₂ R} {f : X ⟶ Y} {l : L₁ ⟶ L₂}
@[simp] lemma map_left_obj_left : ((map_left R l).obj X).left = X.left := rfl
@[simp] lemma map_left_obj_right : ((map_left R l).obj X).right = X.right := rfl
@[simp] lemma map_left_obj_hom : ((map_left R l).obj X).hom = l.app X.left ≫ X.hom := rfl
@[simp] lemma map_left_map_left : ((map_left R l).map f).left = f.left := rfl
@[simp] lemma map_left_map_right : ((map_left R l).map f).right = f.right := rfl
end
def map_left_id : map_left R (𝟙 L) ≅ functor.id _ :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
section
variables {X : comma L R}
@[simp] lemma map_left_id_hom_app_left : (((map_left_id L R).hom).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_left_id_hom_app_right : (((map_left_id L R).hom).app X).right = 𝟙 (X.right) := rfl
@[simp] lemma map_left_id_inv_app_left : (((map_left_id L R).inv).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_left_id_inv_app_right : (((map_left_id L R).inv).app X).right = 𝟙 (X.right) := rfl
end
def map_left_comp (l : L₁ ⟶ L₂) (l' : L₂ ⟶ L₃) :
(map_left R (l ≫ l')) ≅ (map_left R l') ⋙ (map_left R l) :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
section
variables {X : comma L₃ R} {l : L₁ ⟶ L₂} {l' : L₂ ⟶ L₃}
@[simp] lemma map_left_comp_hom_app_left : (((map_left_comp R l l').hom).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_left_comp_hom_app_right : (((map_left_comp R l l').hom).app X).right = 𝟙 (X.right) := rfl
@[simp] lemma map_left_comp_inv_app_left : (((map_left_comp R l l').inv).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_left_comp_inv_app_right : (((map_left_comp R l l').inv).app X).right = 𝟙 (X.right) := rfl
end
def map_right (r : R₁ ⟶ R₂) : comma L R₁ ⥤ comma L R₂ :=
{ obj := λ X,
{ left := X.left,
right := X.right,
hom := X.hom ≫ r.app X.right },
map := λ X Y f,
{ left := f.left,
right := f.right,
w' := by tidy; rw [←r.naturality f.right, ←category.assoc]; tidy } }
section
variables {X Y : comma L R₁} {f : X ⟶ Y} {r : R₁ ⟶ R₂}
@[simp] lemma map_right_obj_left : ((map_right L r).obj X).left = X.left := rfl
@[simp] lemma map_right_obj_right : ((map_right L r).obj X).right = X.right := rfl
@[simp] lemma map_right_obj_hom : ((map_right L r).obj X).hom = X.hom ≫ r.app X.right := rfl
@[simp] lemma map_right_map_left : ((map_right L r).map f).left = f.left := rfl
@[simp] lemma map_right_map_right : ((map_right L r).map f).right = f.right := rfl
end
def map_right_id : map_right L (𝟙 R) ≅ functor.id _ :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
section
variables {X : comma L R}
@[simp] lemma map_right_id_hom_app_left : (((map_right_id L R).hom).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_right_id_hom_app_right : (((map_right_id L R).hom).app X).right = 𝟙 (X.right) := rfl
@[simp] lemma map_right_id_inv_app_left : (((map_right_id L R).inv).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_right_id_inv_app_right : (((map_right_id L R).inv).app X).right = 𝟙 (X.right) := rfl
end
def map_right_comp (r : R₁ ⟶ R₂) (r' : R₂ ⟶ R₃) : (map_right L (r ≫ r')) ≅ (map_right L r) ⋙ (map_right L r') :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
section
variables {X : comma L R₁} {r : R₁ ⟶ R₂} {r' : R₂ ⟶ R₃}
@[simp] lemma map_right_comp_hom_app_left : (((map_right_comp L r r').hom).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_right_comp_hom_app_right : (((map_right_comp L r r').hom).app X).right = 𝟙 (X.right) := rfl
@[simp] lemma map_right_comp_inv_app_left : (((map_right_comp L r r').inv).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_right_comp_inv_app_right : (((map_right_comp L r r').inv).app X).right = 𝟙 (X.right) := rfl
end
end
end comma
omit 𝒜 ℬ
def over (X : T) := comma.{v₃ 1 v₃} (functor.id T) (functor.of.obj X)
namespace over
variables {X : T}
instance category : category (over X) := by delta over; apply_instance
@[extensionality] lemma over_morphism.ext {X : T} {U V : over X} {f g : U ⟶ V}
(h : f.left = g.left) : f = g :=
by tidy
@[simp] lemma over_right (U : over X) : U.right = punit.star := by tidy
@[simp] lemma over_morphism_right {U V : over X} (f : U ⟶ V) : f.right = 𝟙 punit.star := by tidy
@[simp] lemma id_left (U : over X) : comma_morphism.left (𝟙 U) = 𝟙 U.left := rfl
@[simp] lemma comp_left (a b c : over X) (f : a ⟶ b) (g : b ⟶ c) :
(f ≫ g).left = f.left ≫ g.left := rfl
@[simp] lemma w {A B : over X} (f : A ⟶ B) : f.left ≫ B.hom = A.hom :=
by have := f.w; tidy
def mk {X Y : T} (f : Y ⟶ X) : over X :=
{ left := Y, hom := f }
@[simp] lemma mk_left {X Y : T} (f : Y ⟶ X) : (mk f).left = Y := rfl
@[simp] lemma mk_hom {X Y : T} (f : Y ⟶ X) : (mk f).hom = f := rfl
def hom_mk {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom . obviously) :
U ⟶ V :=
{ left := f }
@[simp] lemma hom_mk_left {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom) :
(hom_mk f).left = f :=
rfl
def forget : (over X) ⥤ T := comma.fst _ _
@[simp] lemma forget_obj {U : over X} : forget.obj U = U.left := rfl
@[simp] lemma forget_map {U V : over X} {f : U ⟶ V} : forget.map f = f.left := rfl
def map {Y : T} (f : X ⟶ Y) : over X ⥤ over Y := comma.map_right _ $ functor.of.map f
section
variables {Y : T} {f : X ⟶ Y} {U V : over X} {g : U ⟶ V}
@[simp] lemma map_obj_left : ((map f).obj U).left = U.left := rfl
@[simp] lemma map_obj_hom : ((map f).obj U).hom = U.hom ≫ f := rfl
@[simp] lemma map_map_left : ((map f).map g).left = g.left := rfl
end
section
variables {D : Type u₃} [𝒟 : category.{v₃} D]
include 𝒟
def post (F : T ⥤ D) : over X ⥤ over (F.obj X) :=
{ obj := λ Y, mk $ F.map Y.hom,
map := λ Y₁ Y₂ f,
{ left := F.map f.left,
w' := by tidy; erw [← F.map_comp, w] } }
end
end over
def under (X : T) := comma.{1 v₃ v₃} (functor.of.obj X) (functor.id T)
namespace under
variables {X : T}
instance : category (under X) := by delta under; apply_instance
@[extensionality] lemma under_morphism.ext {X : T} {U V : under X} {f g : U ⟶ V}
(h : f.right = g.right) : f = g :=
by tidy
@[simp] lemma under_left (U : under X) : U.left = punit.star := by tidy
@[simp] lemma under_morphism_left {U V : under X} (f : U ⟶ V) : f.left = 𝟙 punit.star := by tidy
@[simp] lemma id_right (U : under X) : comma_morphism.right (𝟙 U) = 𝟙 U.right := rfl
@[simp] lemma comp_right (a b c : under X) (f : a ⟶ b) (g : b ⟶ c) :
(f ≫ g).right = f.right ≫ g.right := rfl
@[simp] lemma w {A B : under X} (f : A ⟶ B) : A.hom ≫ f.right = B.hom :=
by have := f.w; tidy
def mk {X Y : T} (f : X ⟶ Y) : under X :=
{ right := Y, hom := f }
@[simp] lemma mk_right {X Y : T} (f : X ⟶ Y) : (mk f).right = Y := rfl
@[simp] lemma mk_hom {X Y : T} (f : X ⟶ Y) : (mk f).hom = f := rfl
def hom_mk {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom . obviously) :
U ⟶ V :=
{ right := f }
@[simp] lemma hom_mk_right {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom) :
(hom_mk f).right = f :=
rfl
def forget : (under X) ⥤ T := comma.snd _ _
@[simp] lemma forget_obj {U : under X} : forget.obj U = U.right := rfl
@[simp] lemma forget_map {U V : under X} {f : U ⟶ V} : forget.map f = f.right := rfl
def map {Y : T} (f : X ⟶ Y) : under Y ⥤ under X := comma.map_left _ $ functor.of.map f
section
variables {Y : T} {f : X ⟶ Y} {U V : under Y} {g : U ⟶ V}
@[simp] lemma map_obj_right : ((map f).obj U).right = U.right := rfl
@[simp] lemma map_obj_hom : ((map f).obj U).hom = f ≫ U.hom := rfl
@[simp] lemma map_map_right : ((map f).map g).right = g.right := rfl
end
section
variables {D : Type u₃} [𝒟 : category.{v₃} D]
include 𝒟
def post {X : T} (F : T ⥤ D) : under X ⥤ under (F.obj X) :=
{ obj := λ Y, mk $ F.map Y.hom,
map := λ Y₁ Y₂ f,
{ right := F.map f.right,
w' := by tidy; erw [← F.map_comp, w] } }
end
end under
end category_theory
|
370f4fad88952ee932747685e0c16afd9c9fee53 | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/topology/bases.lean | b5a3dd5fc0a66e56f80bff51c83f64b8421e10fa | [
"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 | 12,260 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
Bases of topologies. Countability axioms.
-/
import topology.constructions
open set filter classical
open_locale topological_space filter
namespace topological_space
/- countability axioms
For our applications we are interested that there exists a countable basis, but we do not need the
concrete basis itself. This allows us to declare these type classes as `Prop` to use them as mixins.
-/
universe u
variables {α : Type u} [t : topological_space α]
include t
/-- A topological basis is one that satisfies the necessary conditions so that
it suffices to take unions of the basis sets to get a topology (without taking
finite intersections as well). -/
def is_topological_basis (s : set (set α)) : Prop :=
(∀t₁∈s, ∀t₂∈s, ∀ x ∈ t₁ ∩ t₂, ∃ t₃∈s, x ∈ t₃ ∧ t₃ ⊆ t₁ ∩ t₂) ∧
(⋃₀ s) = univ ∧
t = generate_from s
lemma is_topological_basis_of_subbasis {s : set (set α)} (hs : t = generate_from s) :
is_topological_basis ((λf, ⋂₀ f) '' {f:set (set α) | finite f ∧ f ⊆ s ∧ (⋂₀ f).nonempty}) :=
let b' := (λf, ⋂₀ f) '' {f:set (set α) | finite f ∧ f ⊆ s ∧ (⋂₀ f).nonempty} in
⟨assume s₁ ⟨t₁, ⟨hft₁, ht₁b, ht₁⟩, eq₁⟩ s₂ ⟨t₂, ⟨hft₂, ht₂b, ht₂⟩, eq₂⟩,
have ie : ⋂₀(t₁ ∪ t₂) = ⋂₀ t₁ ∩ ⋂₀ t₂, from Inf_union,
eq₁ ▸ eq₂ ▸ assume x h,
⟨_, ⟨t₁ ∪ t₂, ⟨hft₁.union hft₂, union_subset ht₁b ht₂b,
ie.symm ▸ ⟨_, h⟩⟩, ie⟩, h, subset.refl _⟩,
eq_univ_iff_forall.2 $ assume a, ⟨univ, ⟨∅, ⟨finite_empty, empty_subset _,
by rw sInter_empty; exact ⟨a, mem_univ a⟩⟩, sInter_empty⟩, mem_univ _⟩,
have generate_from s = generate_from b',
from le_antisymm
(le_generate_from $ assume u ⟨t, ⟨hft, htb, ne⟩, eq⟩,
eq ▸ @is_open_sInter _ (generate_from s) _ hft (assume s hs, generate_open.basic _ $ htb hs))
(le_generate_from $ assume s hs,
s.eq_empty_or_nonempty.elim
(assume : s = ∅, by rw [this]; apply @is_open_empty _ _)
(assume : s.nonempty, generate_open.basic _ ⟨{s}, ⟨finite_singleton s, singleton_subset_iff.2 hs,
by rwa sInter_singleton⟩, sInter_singleton s⟩)),
this ▸ hs⟩
lemma is_topological_basis_of_open_of_nhds {s : set (set α)}
(h_open : ∀ u ∈ s, is_open u)
(h_nhds : ∀(a:α) (u : set α), a ∈ u → is_open u → ∃v ∈ s, a ∈ v ∧ v ⊆ u) :
is_topological_basis s :=
⟨assume t₁ ht₁ t₂ ht₂ x ⟨xt₁, xt₂⟩,
h_nhds x (t₁ ∩ t₂) ⟨xt₁, xt₂⟩
(is_open_inter (h_open _ ht₁) (h_open _ ht₂)),
eq_univ_iff_forall.2 $ assume a,
let ⟨u, h₁, h₂, _⟩ := h_nhds a univ trivial is_open_univ in
⟨u, h₁, h₂⟩,
le_antisymm
(le_generate_from h_open)
(assume u hu,
(@is_open_iff_nhds α (generate_from _) _).mpr $ assume a hau,
let ⟨v, hvs, hav, hvu⟩ := h_nhds a u hau hu in
by rw nhds_generate_from; exact infi_le_of_le v (infi_le_of_le ⟨hav, hvs⟩ $ le_principal_iff.2 hvu))⟩
lemma mem_nhds_of_is_topological_basis {a : α} {s : set α} {b : set (set α)}
(hb : is_topological_basis b) : s ∈ 𝓝 a ↔ ∃t∈b, a ∈ t ∧ t ⊆ s :=
begin
change s ∈ (𝓝 a).sets ↔ ∃t∈b, a ∈ t ∧ t ⊆ s,
rw [hb.2.2, nhds_generate_from, binfi_sets_eq],
{ simp only [mem_bUnion_iff, exists_prop, mem_set_of_eq, and_assoc, and.left_comm], refl },
{ exact assume s ⟨hs₁, hs₂⟩ t ⟨ht₁, ht₂⟩,
have a ∈ s ∩ t, from ⟨hs₁, ht₁⟩,
let ⟨u, hu₁, hu₂, hu₃⟩ := hb.1 _ hs₂ _ ht₂ _ this in
⟨u, ⟨hu₂, hu₁⟩, le_principal_iff.2 (subset.trans hu₃ (inter_subset_left _ _)),
le_principal_iff.2 (subset.trans hu₃ (inter_subset_right _ _))⟩ },
{ rcases eq_univ_iff_forall.1 hb.2.1 a with ⟨i, h1, h2⟩,
exact ⟨i, h2, h1⟩ }
end
lemma is_open_of_is_topological_basis {s : set α} {b : set (set α)}
(hb : is_topological_basis b) (hs : s ∈ b) : is_open s :=
is_open_iff_mem_nhds.2 $ λ a as,
(mem_nhds_of_is_topological_basis hb).2 ⟨s, hs, as, subset.refl _⟩
lemma mem_basis_subset_of_mem_open {b : set (set α)}
(hb : is_topological_basis b) {a:α} {u : set α} (au : a ∈ u)
(ou : is_open u) : ∃v ∈ b, a ∈ v ∧ v ⊆ u :=
(mem_nhds_of_is_topological_basis hb).1 $ mem_nhds_sets ou au
lemma sUnion_basis_of_is_open {B : set (set α)}
(hB : is_topological_basis B) {u : set α} (ou : is_open u) :
∃ S ⊆ B, u = ⋃₀ S :=
⟨{s ∈ B | s ⊆ u}, λ s h, h.1, set.ext $ λ a,
⟨λ ha, let ⟨b, hb, ab, bu⟩ := mem_basis_subset_of_mem_open hB ha ou in
⟨b, ⟨hb, bu⟩, ab⟩,
λ ⟨b, ⟨hb, bu⟩, ab⟩, bu ab⟩⟩
lemma Union_basis_of_is_open {B : set (set α)}
(hB : is_topological_basis B) {u : set α} (ou : is_open u) :
∃ (β : Type u) (f : β → set α), u = (⋃ i, f i) ∧ ∀ i, f i ∈ B :=
let ⟨S, sb, su⟩ := sUnion_basis_of_is_open hB ou in
⟨S, subtype.val, su.trans set.sUnion_eq_Union, λ ⟨b, h⟩, sb h⟩
variables (α)
/-- A separable space is one with a countable dense subset. -/
class separable_space : Prop :=
(exists_countable_closure_eq_univ : ∃s:set α, countable s ∧ closure s = univ)
/-- A first-countable space is one in which every point has a
countable neighborhood basis. -/
class first_countable_topology : Prop :=
(nhds_generated_countable : ∀a:α, (𝓝 a).is_countably_generated)
namespace first_countable_topology
variable {α}
lemma tendsto_subseq [first_countable_topology α] {u : ℕ → α} {x : α} (hx : map_cluster_pt x at_top u) :
∃ (ψ : ℕ → ℕ), (strict_mono ψ) ∧ (tendsto (u ∘ ψ) at_top (𝓝 x)) :=
(nhds_generated_countable x).subseq_tendsto hx
end first_countable_topology
/-- A second-countable space is one with a countable basis. -/
class second_countable_topology : Prop :=
(is_open_generated_countable [] : ∃b:set (set α), countable b ∧ t = topological_space.generate_from b)
@[priority 100] -- see Note [lower instance priority]
instance second_countable_topology.to_first_countable_topology
[second_countable_topology α] : first_countable_topology α :=
let ⟨b, hb, eq⟩ := second_countable_topology.is_open_generated_countable α in
⟨begin
intros,
rw [eq, nhds_generate_from],
exact is_countably_generated_binfi_principal (hb.mono (assume x, and.right)),
end⟩
lemma second_countable_topology_induced (β)
[t : topological_space β] [second_countable_topology β] (f : α → β) :
@second_countable_topology α (t.induced f) :=
begin
rcases second_countable_topology.is_open_generated_countable β with ⟨b, hb, eq⟩,
refine { is_open_generated_countable := ⟨preimage f '' b, hb.image _, _⟩ },
rw [eq, induced_generate_from_eq]
end
instance subtype.second_countable_topology
(s : set α) [second_countable_topology α] : second_countable_topology s :=
second_countable_topology_induced s α coe
lemma is_open_generated_countable_inter [second_countable_topology α] :
∃b:set (set α), countable b ∧ ∅ ∉ b ∧ is_topological_basis b :=
let ⟨b, hb₁, hb₂⟩ := second_countable_topology.is_open_generated_countable α in
let b' := (λs, ⋂₀ s) '' {s:set (set α) | finite s ∧ s ⊆ b ∧ (⋂₀ s).nonempty} in
⟨b',
((countable_set_of_finite_subset hb₁).mono
(by { simp only [← and_assoc], apply inter_subset_left })).image _,
assume ⟨s, ⟨_, _, hn⟩, hp⟩, absurd hn (not_nonempty_iff_eq_empty.2 hp),
is_topological_basis_of_subbasis hb₂⟩
/- TODO: more fine grained instances for first_countable_topology, separable_space, t2_space, ... -/
instance {β : Type*} [topological_space β]
[second_countable_topology α] [second_countable_topology β] : second_countable_topology (α × β) :=
⟨let ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩ := is_open_generated_countable_inter α in
let ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩ := is_open_generated_countable_inter β in
⟨{g | ∃u∈a, ∃v∈b, g = set.prod u v},
have {g | ∃u∈a, ∃v∈b, g = set.prod u v} = (⋃u∈a, ⋃v∈b, {set.prod u v}),
by apply set.ext; simp,
by rw [this]; exact (ha₁.bUnion $ assume u hu, hb₁.bUnion $ by simp),
by rw [ha₅, hb₅, prod_generate_from_generate_from_eq ha₄ hb₄]⟩⟩
instance second_countable_topology_fintype {ι : Type*} {π : ι → Type*}
[fintype ι] [t : ∀a, topological_space (π a)] [sc : ∀a, second_countable_topology (π a)] :
second_countable_topology (∀a, π a) :=
have ∀i, ∃b : set (set (π i)), countable b ∧ ∅ ∉ b ∧ is_topological_basis b, from
assume a, @is_open_generated_countable_inter (π a) _ (sc a),
let ⟨g, hg⟩ := classical.axiom_of_choice this in
have t = (λa, generate_from (g a)), from funext $ assume a, (hg a).2.2.2.2,
begin
constructor,
refine ⟨pi univ '' pi univ g, countable.image _ _, _⟩,
{ suffices : countable {f : Πa, set (π a) | ∀a, f a ∈ g a}, { simpa [pi] },
exact countable_pi (assume i, (hg i).1), },
rw [this, pi_generate_from_eq_fintype],
{ congr' 1 with f, simp [pi, eq_comm] },
exact assume a, (hg a).2.2.2.1
end
@[priority 100] -- see Note [lower instance priority]
instance second_countable_topology.to_separable_space
[second_countable_topology α] : separable_space α :=
begin
rcases is_open_generated_countable_inter α with ⟨b, hbc, hbne, hb, hbU, eq⟩,
set S : α → set (set α) := λ a, {s : set α | a ∈ s ∧ s ∈ b},
have nhds_eq : ∀a, 𝓝 a = (⨅ s ∈ S a, 𝓟 s),
{ intro a, rw [eq, nhds_generate_from] },
have : ∀ s ∈ b, set.nonempty s :=
assume s hs, ne_empty_iff_nonempty.1 $ λ eq, absurd hs (eq.symm ▸ hbne),
choose f hf,
refine ⟨⟨⋃ s ∈ b, {f s ‹_›}, hbc.bUnion (λ _ _, countable_singleton _), _⟩⟩,
refine eq_univ_of_forall (λ a, _),
suffices : (⨅ s ∈ S a, 𝓟 (s ∩ ⋃ t ∈ b, {f t ‹_›})).ne_bot,
{ obtain ⟨t, htb, hta⟩ : a ∈ ⋃₀ b, { simp only [hbU] },
have A : ∃ s, s ∈ S a := ⟨t, hta, htb⟩,
simpa only [← inf_principal, mem_closure_iff_cluster_pt,
cluster_pt, nhds_eq, binfi_inf A] using this },
rw [infi_subtype'],
haveI : nonempty α := ⟨a⟩,
refine infi_ne_bot_of_directed _ _,
{ rintros ⟨s₁, has₁, hs₁⟩ ⟨s₂, has₂, hs₂⟩,
obtain ⟨t, htb, hta, ht⟩ : ∃ t ∈ b, a ∈ t ∧ t ⊆ s₁ ∩ s₂,
from hb _ hs₁ _ hs₂ a ⟨has₁, has₂⟩,
refine ⟨⟨t, hta, htb⟩, _⟩,
simp only [subset_inter_iff] at ht,
simp only [principal_mono, subtype.coe_mk, (≥)],
exact ⟨inter_subset_inter_left _ ht.1, inter_subset_inter_left _ ht.2⟩ },
rintros ⟨s, hsa, hsb⟩,
suffices : (s ∩ ⋃ t ∈ b, {f t ‹_›}).nonempty, { simpa [principal_ne_bot_iff] },
refine ⟨_, hf _ hsb, _⟩,
simp only [mem_Union],
exact ⟨s, hsb, rfl⟩
end
variables {α}
lemma is_open_Union_countable [second_countable_topology α]
{ι} (s : ι → set α) (H : ∀ i, is_open (s i)) :
∃ T : set ι, countable T ∧ (⋃ i ∈ T, s i) = ⋃ i, s i :=
let ⟨B, cB, _, bB⟩ := is_open_generated_countable_inter α in
begin
let B' := {b ∈ B | ∃ i, b ⊆ s i},
choose f hf using λ b:B', b.2.2,
haveI : encodable B' := (cB.mono (sep_subset _ _)).to_encodable,
refine ⟨_, countable_range f,
subset.antisymm (bUnion_subset_Union _ _) (sUnion_subset _)⟩,
rintro _ ⟨i, rfl⟩ x xs,
rcases mem_basis_subset_of_mem_open bB xs (H _) with ⟨b, hb, xb, bs⟩,
exact ⟨_, ⟨_, rfl⟩, _, ⟨⟨⟨_, hb, _, bs⟩, rfl⟩, rfl⟩, hf _ (by exact xb)⟩
end
lemma is_open_sUnion_countable [second_countable_topology α]
(S : set (set α)) (H : ∀ s ∈ S, is_open s) :
∃ T : set (set α), countable T ∧ T ⊆ S ∧ ⋃₀ T = ⋃₀ S :=
let ⟨T, cT, hT⟩ := is_open_Union_countable (λ s:S, s.1) (λ s, H s.1 s.2) in
⟨subtype.val '' T, cT.image _,
image_subset_iff.2 $ λ ⟨x, xs⟩ xt, xs,
by rwa [sUnion_image, sUnion_eq_Union]⟩
end topological_space
|
e978492795196b8aaad0110217b67336b113ef5c | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/geometry/euclidean/angle/unoriented/conformal.lean | ed2de536eddc94a0ead386df570d8866f87d95c5 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 1,291 | lean | /-
Copyright (c) 2021 Yourong Zang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yourong Zang
-/
import analysis.calculus.conformal.normed_space
import geometry.euclidean.angle.unoriented.basic
/-!
# Angles and conformal maps
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file proves that conformal maps preserve angles.
-/
namespace inner_product_geometry
variables {E F : Type*}
variables [normed_add_comm_group E] [normed_add_comm_group F]
variables [inner_product_space ℝ E] [inner_product_space ℝ F]
lemma is_conformal_map.preserves_angle {f' : E →L[ℝ] F} (h : is_conformal_map f') (u v : E) :
angle (f' u) (f' v) = angle u v :=
begin
obtain ⟨c, hc, li, rfl⟩ := h,
exact (angle_smul_smul hc _ _).trans (li.angle_map _ _)
end
/-- If a real differentiable map `f` is conformal at a point `x`,
then it preserves the angles at that point. -/
lemma conformal_at.preserves_angle {f : E → F} {x : E} {f' : E →L[ℝ] F}
(h : has_fderiv_at f f' x) (H : conformal_at f x) (u v : E) :
angle (f' u) (f' v) = angle u v :=
let ⟨f₁, h₁, c⟩ := H in h₁.unique h ▸ is_conformal_map.preserves_angle c u v
end inner_product_geometry
|
46af78824d4a2b120198eb0cce274e1f23eb243e | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/category_theory/monoidal/functorial.lean | 3eb1633eb7902fc4e5f5ce952932b9c3ef64dd82 | [
"Apache-2.0"
] | permissive | vaibhavkarve/mathlib | a574aaf68c0a431a47fa82ce0637f0f769826bfe | 17f8340912468f49bdc30acdb9a9fa02eeb0473a | refs/heads/master | 1,621,263,802,637 | 1,585,399,588,000 | 1,585,399,588,000 | 250,833,447 | 0 | 0 | Apache-2.0 | 1,585,410,341,000 | 1,585,410,341,000 | null | UTF-8 | Lean | false | false | 3,751 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.monoidal.functor
import category_theory.functorial
/-!
# Unbundled lax monoidal functors
## Design considerations
The essential problem I've encountered that requires unbundled functors is
having an existing (non-monoidal) functor `F : C ⥤ D` between monoidal categories,
and wanting to assert that it has an extension to a lax monoidal functor.
The two options seem to be
1. Construct a separate `F' : lax_monoidal_functor C D`,
and assert `F'.to_functor ≅ F`.
2. Introduce unbundled functors and unbundled lax monoidal functors,
and construct `lax_monoidal F.obj`, then construct `F' := lax_monoidal_functor.of F.obj`.
Both have costs, but as for option 2. the cost is in library design,
while in option 1. the cost is users having to carry around additional isomorphisms forever,
I wanted to introduce unbundled functors.
TODO:
later, we may want to do this for strong monoidal functors as well,
but the immediate application, for enriched categories, only requires this notion.
-/
open category_theory
universes v₁ v₂ v₃ u₁ u₂ u₃
open category_theory.category
open category_theory.functor
namespace category_theory
open monoidal_category
variables {C : Type u₁} [category.{v₁} C] [𝒞 : monoidal_category.{v₁} C]
{D : Type u₂} [category.{v₂} D] [𝒟 : monoidal_category.{v₂} D]
include 𝒞 𝒟
/-- An unbundled description of lax monoidal functors. -/
-- Perhaps in the future we'll redefine `lax_monoidal_functor` in terms of this,
-- but that isn't the immediate plan.
class lax_monoidal (F : C → D) [functorial.{v₁ v₂} F] :=
-- unit morphism
(ε : 𝟙_ D ⟶ F (𝟙_ C))
-- tensorator
(μ : Π X Y : C, (F X) ⊗ (F Y) ⟶ F (X ⊗ Y))
(μ_natural' : ∀ {X Y X' Y' : C}
(f : X ⟶ Y) (g : X' ⟶ Y'),
((map F f) ⊗ (map F g)) ≫ μ Y Y' = μ X X' ≫ map F (f ⊗ g)
. obviously)
-- associativity of the tensorator
(associativity' : ∀ (X Y Z : C),
(μ X Y ⊗ 𝟙 (F Z)) ≫ μ (X ⊗ Y) Z ≫ map F (α_ X Y Z).hom
= (α_ (F X) (F Y) (F Z)).hom ≫ (𝟙 (F X) ⊗ μ Y Z) ≫ μ X (Y ⊗ Z)
. obviously)
-- unitality
(left_unitality' : ∀ X : C,
(λ_ (F X)).hom
= (ε ⊗ 𝟙 (F X)) ≫ μ (𝟙_ C) X ≫ map F (λ_ X).hom
. obviously)
(right_unitality' : ∀ X : C,
(ρ_ (F X)).hom
= (𝟙 (F X) ⊗ ε) ≫ μ X (𝟙_ C) ≫ map F (ρ_ X).hom
. obviously)
restate_axiom lax_monoidal.μ_natural'
attribute [simp] lax_monoidal.μ_natural
restate_axiom lax_monoidal.left_unitality'
attribute [simp] lax_monoidal.left_unitality
restate_axiom lax_monoidal.right_unitality'
attribute [simp] lax_monoidal.right_unitality
restate_axiom lax_monoidal.associativity'
attribute [simp] lax_monoidal.associativity
namespace lax_monoidal_functor
/--
Construct a bundled `lax_monoidal_functor` from the object level function
and `functorial` and `lax_monoidal` typeclasses.
-/
def of (F : C → D) [I₁ : functorial.{v₁ v₂} F] [I₂ : lax_monoidal.{v₁ v₂} F] :
lax_monoidal_functor.{v₁ v₂} C D :=
{ obj := F,
..I₁, ..I₂ }
end lax_monoidal_functor
instance (F : lax_monoidal_functor.{v₁ v₂} C D) : lax_monoidal.{v₁ v₂} (F.obj) := { .. F }
section
omit 𝒟
instance lax_monoidal_id : lax_monoidal.{v₁ v₁} (id : C → C) :=
{ ε := 𝟙 _,
μ := λ X Y, 𝟙 _ }
end
-- TODO instances for composition, as required
-- TODO `strong_monoidal`, as well as `lax_monoidal`
-- (... but it seems for enriched categories I'll only need unbundled lax monoidal functors at first)
end category_theory
|
9540c7f7136f542c5ccaeb34864a29f3af36341b | 274748215b6d042f0d9c9a505f9551fa8e0c5f38 | /src/affine_algebraic_set/V_and_I.lean | 8b432b2d5012a75901456abd7d8033df0af68f2d | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/M4P33 | 878ecb515c77d20cc799ff1ebd78f1bf4fd65c12 | 1a179372db71ad6802d11eacbc1f02f327d55f8f | refs/heads/master | 1,607,519,867,193 | 1,583,344,297,000 | 1,583,344,297,000 | 233,316,107 | 59 | 4 | Apache-2.0 | 1,579,285,778,000 | 1,578,788,367,000 | Lean | UTF-8 | Lean | false | false | 2,264 | lean | -- Some basic results about the relationship of 𝕍 and 𝕀
-- Imports results about 𝕍 and 𝕀
import affine_algebraic_set.V
import affine_algebraic_set.I
import data.mv_polynomial
open mv_polynomial
variables {k : Type*} [comm_ring k]
variable {σ : Type*}
open affine_algebraic_set
/-- For S ⊆ k[X₁,X₂,…,Xₙ] we have S ⊆ 𝕀(𝕍(S))--/
lemma 𝕀_of_𝕍_is_subset {S : set(mv_polynomial σ k)} :
S ⊆ 𝕀(𝕍(S)):=
begin
-- assume s ∈ S
intros s HS,
-- apply Definition of 𝕀
rw mem_𝕀_iff,
-- let x ∈ kⁿ be arbitrary
intro x,
--apply Definition of 𝕍
rw mem_𝕍_iff,
-- Assume Hypothesis
intro H,
-- Apply H to goal
apply H,
from HS,
end
/-- For S ⊆ k[X₁,X₂,…,Xₙ] we have 𝕍(𝕀(𝕍(S))) = 𝕍(S)
e.g. 𝕍(𝕀(U)) = U if U is an algebraic set --/
lemma 𝕀_of_𝕍 {S : set(mv_polynomial σ k)} :
𝕍(𝕀(𝕍(S))) = 𝕍(S):=
begin
-- ⊆ Inclusion: This follows from the fact that 𝕀(𝕍(S)) ⊆ S (shown above)
-- And the antimonomity of 𝕍
apply set.subset.antisymm,
apply 𝕍_antimono,
from 𝕀_of_𝕍_is_subset,
-- ⊇ Inclusion: I'm sorry, this is just a bunch of abstract nonsense
-- We show that if x ∈ kⁿ vanishes for all f ∈ S (e.g. x ∈ 𝕍(S)),
-- it also vanishes for all g ∈ k[X₁,X₂,…,Xₙ] that vanish on 𝕍(S)
-- Which is true by definition:
-- Suppose y ∈ 𝕍(S)
intros y HS,
-- Apply definitions of 𝕍 and 𝕀
rw mem_𝕍_iff,
intro f,
rw mem_𝕀_iff,
-- Rewrite Goal
intro Q,
apply Q y,
-- Use y ∈ 𝕍(S)
from HS,
end
lemma 𝕀_inj_on_𝕍 {S T : set(mv_polynomial σ k)} : 𝕀(𝕍(S)) = 𝕀(𝕍(T)) → 𝕍(S) = 𝕍(T) :=
begin
intro H,
have H1 := congr_arg 𝕍 H,
rw [𝕀_of_𝕍, 𝕀_of_𝕍] at H1,
exact H1,
end
lemma 𝕀_strantimono_on_𝕍 {S T : set(mv_polynomial σ k)} :
𝕍 S < 𝕍 T → 𝕀(𝕍(T)) < 𝕀(𝕍(S)) :=
begin
intro lt,
have H := 𝕀_antimono (𝕍 S) (𝕍 T) (le_of_lt lt),
cases (@eq_or_lt_of_le (set (mv_polynomial σ k)) _ _ _ H),
{
exfalso,
have H1 := 𝕀_inj_on_𝕍 h,
apply ne_of_lt lt,
apply H1.symm,
},
{ exact h },
end |
5c7782478ccee62d93d236fd9753693c32764546 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/secVarBug.lean | f25b33f5b9c682b194d45754cc8151c0222e8a25 | [
"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 | 101 | lean | variable {α} [ToString α] (n : Nat)
local macro "foo" : term => `(n)
def f :=
foo
#check f 10
|
1620d31b73bb9ed8deb384629973bba635820a52 | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /library/data/nat/order.lean | 013b979b1b1fdc89ed8b30891827609e30a1e57a | [
"Apache-2.0"
] | permissive | jroesch/lean | 30ef0860fa905d35b9ad6f76de1a4f65c9af6871 | 3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2 | refs/heads/master | 1,586,090,835,348 | 1,455,142,203,000 | 1,455,142,277,000 | 51,536,958 | 1 | 0 | null | 1,455,215,811,000 | 1,455,215,811,000 | null | UTF-8 | Lean | false | false | 18,647 | lean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad
The order relation on the natural numbers.
-/
import .basic algebra.ordered_ring
open eq.ops
namespace nat
/- lt and le -/
protected theorem le_of_lt_or_eq {m n : ℕ} (H : m < n ∨ m = n) : m ≤ n :=
nat.le_of_eq_or_lt (or.swap H)
protected theorem lt_or_eq_of_le {m n : ℕ} (H : m ≤ n) : m < n ∨ m = n :=
or.swap (nat.eq_or_lt_of_le H)
protected theorem le_iff_lt_or_eq (m n : ℕ) : m ≤ n ↔ m < n ∨ m = n :=
iff.intro nat.lt_or_eq_of_le nat.le_of_lt_or_eq
protected theorem lt_of_le_and_ne {m n : ℕ} (H1 : m ≤ n) : m ≠ n → m < n :=
or_resolve_right (nat.eq_or_lt_of_le H1)
protected theorem lt_iff_le_and_ne (m n : ℕ) : m < n ↔ m ≤ n ∧ m ≠ n :=
iff.intro
(take H, and.intro (nat.le_of_lt H) (take H1, !nat.lt_irrefl (H1 ▸ H)))
(and.rec nat.lt_of_le_and_ne)
theorem le_add_right (n k : ℕ) : n ≤ n + k :=
nat.rec !nat.le_refl (λ k, le_succ_of_le) k
theorem le_add_left (n m : ℕ): n ≤ m + n :=
!add.comm ▸ !le_add_right
theorem le.intro {n m k : ℕ} (h : n + k = m) : n ≤ m :=
h ▸ !le_add_right
theorem le.elim {n m : ℕ} : n ≤ m → ∃ k, n + k = m :=
le.rec (exists.intro 0 rfl) (λm h, Exists.rec
(λ k H, exists.intro (succ k) (H ▸ rfl)))
protected theorem le_total {m n : ℕ} : m ≤ n ∨ n ≤ m :=
or.imp_left nat.le_of_lt !nat.lt_or_ge
/- addition -/
protected theorem add_le_add_left {n m : ℕ} (H : n ≤ m) (k : ℕ) : k + n ≤ k + m :=
obtain l Hl, from le.elim H, le.intro (Hl ▸ !add.assoc)
protected theorem add_le_add_right {n m : ℕ} (H : n ≤ m) (k : ℕ) : n + k ≤ m + k :=
!add.comm ▸ !add.comm ▸ nat.add_le_add_left H k
protected theorem le_of_add_le_add_left {k n m : ℕ} (H : k + n ≤ k + m) : n ≤ m :=
obtain l Hl, from le.elim H, le.intro (nat.add_left_cancel (!add.assoc⁻¹ ⬝ Hl))
protected theorem lt_of_add_lt_add_left {k n m : ℕ} (H : k + n < k + m) : n < m :=
let H' := nat.le_of_lt H in
nat.lt_of_le_and_ne (nat.le_of_add_le_add_left H') (assume Heq, !nat.lt_irrefl (Heq ▸ H))
protected theorem add_lt_add_left {n m : ℕ} (H : n < m) (k : ℕ) : k + n < k + m :=
lt_of_succ_le (!add_succ ▸ nat.add_le_add_left (succ_le_of_lt H) k)
protected theorem add_lt_add_right {n m : ℕ} (H : n < m) (k : ℕ) : n + k < m + k :=
!add.comm ▸ !add.comm ▸ nat.add_lt_add_left H k
protected theorem lt_add_of_pos_right {n k : ℕ} (H : k > 0) : n < n + k :=
!add_zero ▸ nat.add_lt_add_left H n
/- multiplication -/
theorem mul_le_mul_left {n m : ℕ} (k : ℕ) (H : n ≤ m) : k * n ≤ k * m :=
obtain (l : ℕ) (Hl : n + l = m), from le.elim H,
have k * n + k * l = k * m, by rewrite [-left_distrib, Hl],
le.intro this
theorem mul_le_mul_right {n m : ℕ} (k : ℕ) (H : n ≤ m) : n * k ≤ m * k :=
!mul.comm ▸ !mul.comm ▸ !mul_le_mul_left H
protected theorem mul_le_mul {n m k l : ℕ} (H1 : n ≤ k) (H2 : m ≤ l) : n * m ≤ k * l :=
nat.le_trans (!nat.mul_le_mul_right H1) (!nat.mul_le_mul_left H2)
protected theorem mul_lt_mul_of_pos_left {n m k : ℕ} (H : n < m) (Hk : k > 0) : k * n < k * m :=
nat.lt_of_lt_of_le (nat.lt_add_of_pos_right Hk) (!mul_succ ▸ nat.mul_le_mul_left k (succ_le_of_lt H))
protected theorem mul_lt_mul_of_pos_right {n m k : ℕ} (H : n < m) (Hk : k > 0) : n * k < m * k :=
!mul.comm ▸ !mul.comm ▸ nat.mul_lt_mul_of_pos_left H Hk
/- nat is an instance of a linearly ordered semiring and a lattice -/
protected definition decidable_linear_ordered_semiring [reducible] [trans_instance] :
decidable_linear_ordered_semiring nat :=
⦃ decidable_linear_ordered_semiring, nat.comm_semiring,
add_left_cancel := @nat.add_left_cancel,
add_right_cancel := @nat.add_right_cancel,
lt := nat.lt,
le := nat.le,
le_refl := nat.le_refl,
le_trans := @nat.le_trans,
le_antisymm := @nat.le_antisymm,
le_total := @nat.le_total,
le_iff_lt_or_eq := @nat.le_iff_lt_or_eq,
le_of_lt := @nat.le_of_lt,
lt_irrefl := @nat.lt_irrefl,
lt_of_lt_of_le := @nat.lt_of_lt_of_le,
lt_of_le_of_lt := @nat.lt_of_le_of_lt,
lt_of_add_lt_add_left := @nat.lt_of_add_lt_add_left,
add_lt_add_left := @nat.add_lt_add_left,
add_le_add_left := @nat.add_le_add_left,
le_of_add_le_add_left := @nat.le_of_add_le_add_left,
zero_lt_one := zero_lt_succ 0,
mul_le_mul_of_nonneg_left := (take a b c H1 H2, nat.mul_le_mul_left c H1),
mul_le_mul_of_nonneg_right := (take a b c H1 H2, nat.mul_le_mul_right c H1),
mul_lt_mul_of_pos_left := @nat.mul_lt_mul_of_pos_left,
mul_lt_mul_of_pos_right := @nat.mul_lt_mul_of_pos_right,
decidable_lt := nat.decidable_lt ⦄
definition nat_has_dvd [reducible] [instance] [priority nat.prio] : has_dvd nat :=
has_dvd.mk has_dvd.dvd
theorem add_pos_left {a : ℕ} (H : 0 < a) (b : ℕ) : 0 < a + b :=
@add_pos_of_pos_of_nonneg _ _ a b H !zero_le
theorem add_pos_right {a : ℕ} (H : 0 < a) (b : ℕ) : 0 < b + a :=
by rewrite add.comm; apply add_pos_left H b
theorem add_eq_zero_iff_eq_zero_and_eq_zero {a b : ℕ} :
a + b = 0 ↔ a = 0 ∧ b = 0 :=
@add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg _ _ a b !zero_le !zero_le
theorem le_add_of_le_left {a b c : ℕ} (H : b ≤ c) : b ≤ a + c :=
@le_add_of_nonneg_of_le _ _ a b c !zero_le H
theorem le_add_of_le_right {a b c : ℕ} (H : b ≤ c) : b ≤ c + a :=
@le_add_of_le_of_nonneg _ _ a b c H !zero_le
theorem lt_add_of_lt_left {b c : ℕ} (H : b < c) (a : ℕ) : b < a + c :=
@lt_add_of_nonneg_of_lt _ _ a b c !zero_le H
theorem lt_add_of_lt_right {b c : ℕ} (H : b < c) (a : ℕ) : b < c + a :=
@lt_add_of_lt_of_nonneg _ _ a b c H !zero_le
theorem lt_of_mul_lt_mul_left {a b c : ℕ} (H : c * a < c * b) : a < b :=
@lt_of_mul_lt_mul_left _ _ a b c H !zero_le
theorem lt_of_mul_lt_mul_right {a b c : ℕ} (H : a * c < b * c) : a < b :=
@lt_of_mul_lt_mul_right _ _ a b c H !zero_le
theorem pos_of_mul_pos_left {a b : ℕ} (H : 0 < a * b) : 0 < b :=
@pos_of_mul_pos_left _ _ a b H !zero_le
theorem pos_of_mul_pos_right {a b : ℕ} (H : 0 < a * b) : 0 < a :=
@pos_of_mul_pos_right _ _ a b H !zero_le
theorem zero_le_one : (0:nat) ≤ 1 :=
dec_trivial
/- properties specific to nat -/
theorem lt_intro {n m k : ℕ} (H : succ n + k = m) : n < m :=
lt_of_succ_le (le.intro H)
theorem lt_elim {n m : ℕ} (H : n < m) : ∃k, succ n + k = m :=
le.elim (succ_le_of_lt H)
theorem lt_add_succ (n m : ℕ) : n < n + succ m :=
lt_intro !succ_add_eq_succ_add
theorem eq_zero_of_le_zero {n : ℕ} (H : n ≤ 0) : n = 0 :=
obtain (k : ℕ) (Hk : n + k = 0), from le.elim H,
eq_zero_of_add_eq_zero_right Hk
/- succ and pred -/
theorem le_of_lt_succ {m n : nat} : m < succ n → m ≤ n :=
le_of_succ_le_succ
theorem lt_iff_succ_le (m n : nat) : m < n ↔ succ m ≤ n :=
iff.rfl
theorem lt_succ_iff_le (m n : nat) : m < succ n ↔ m ≤ n :=
iff.intro le_of_lt_succ lt_succ_of_le
theorem self_le_succ (n : ℕ) : n ≤ succ n :=
le.intro !add_one
theorem succ_le_or_eq_of_le {n m : ℕ} : n ≤ m → succ n ≤ m ∨ n = m :=
lt_or_eq_of_le
theorem pred_le_of_le_succ {n m : ℕ} : n ≤ succ m → pred n ≤ m :=
pred_le_pred
theorem succ_le_of_le_pred {n m : ℕ} : succ n ≤ m → n ≤ pred m :=
pred_le_pred
theorem pred_le_pred_of_le {n m : ℕ} : n ≤ m → pred n ≤ pred m :=
pred_le_pred
theorem pre_lt_of_lt {n m : ℕ} : n < m → pred n < m :=
lt_of_le_of_lt !pred_le
theorem lt_of_pred_lt_pred {n m : ℕ} (H : pred n < pred m) : n < m :=
lt_of_not_ge
(suppose m ≤ n,
not_lt_of_ge (pred_le_pred_of_le this) H)
theorem le_or_eq_succ_of_le_succ {n m : ℕ} (H : n ≤ succ m) : n ≤ m ∨ n = succ m :=
or.imp_left le_of_succ_le_succ (succ_le_or_eq_of_le H)
theorem le_pred_self (n : ℕ) : pred n ≤ n :=
!pred_le
theorem succ_pos (n : ℕ) : 0 < succ n :=
!zero_lt_succ
theorem succ_pred_of_pos {n : ℕ} (H : n > 0) : succ (pred n) = n :=
(or_resolve_right (eq_zero_or_eq_succ_pred n) (ne.symm (ne_of_lt H)))⁻¹
theorem exists_eq_succ_of_lt {n : ℕ} : Π {m : ℕ}, n < m → ∃k, m = succ k
| 0 H := absurd H !not_lt_zero
| (succ k) H := exists.intro k rfl
theorem lt_succ_self (n : ℕ) : n < succ n :=
lt.base n
lemma lt_succ_of_lt {i j : nat} : i < j → i < succ j :=
assume Plt, lt.trans Plt (self_lt_succ j)
/- other forms of induction -/
protected definition strong_rec_on {P : nat → Type} (n : ℕ) (H : ∀n, (∀m, m < n → P m) → P n) : P n :=
nat.rec (λm h, absurd h !not_lt_zero)
(λn' (IH : ∀ {m : ℕ}, m < n' → P m) m l,
or.by_cases (lt_or_eq_of_le (le_of_lt_succ l))
IH (λ e, eq.rec (H n' @IH) e⁻¹)) (succ n) n !lt_succ_self
protected theorem strong_induction_on {P : nat → Prop} (n : ℕ) (H : ∀n, (∀m, m < n → P m) → P n) :
P n :=
nat.strong_rec_on n H
protected theorem case_strong_induction_on {P : nat → Prop} (a : nat) (H0 : P 0)
(Hind : ∀(n : nat), (∀m, m ≤ n → P m) → P (succ n)) : P a :=
nat.strong_induction_on a
(take n,
show (∀ m, m < n → P m) → P n, from
nat.cases_on n
(suppose (∀ m, m < 0 → P m), show P 0, from H0)
(take n,
suppose (∀ m, m < succ n → P m),
show P (succ n), from
Hind n (take m, assume H1 : m ≤ n, this _ (lt_succ_of_le H1))))
/- pos -/
theorem by_cases_zero_pos {P : ℕ → Prop} (y : ℕ) (H0 : P 0) (H1 : ∀ {y : nat}, y > 0 → P y) :
P y :=
nat.cases_on y H0 (take y, H1 !succ_pos)
theorem eq_zero_or_pos (n : ℕ) : n = 0 ∨ n > 0 :=
or_of_or_of_imp_left
(or.swap (lt_or_eq_of_le !zero_le))
(suppose 0 = n, by subst n)
theorem pos_of_ne_zero {n : ℕ} (H : n ≠ 0) : n > 0 :=
or.elim !eq_zero_or_pos (take H2 : n = 0, by contradiction) (take H2 : n > 0, H2)
theorem ne_zero_of_pos {n : ℕ} (H : n > 0) : n ≠ 0 :=
ne.symm (ne_of_lt H)
theorem exists_eq_succ_of_pos {n : ℕ} (H : n > 0) : ∃l, n = succ l :=
exists_eq_succ_of_lt H
theorem pos_of_dvd_of_pos {m n : ℕ} (H1 : m ∣ n) (H2 : n > 0) : m > 0 :=
pos_of_ne_zero
(suppose m = 0,
assert n = 0, from eq_zero_of_zero_dvd (this ▸ H1),
ne_of_lt H2 (by subst n))
/- multiplication -/
theorem mul_lt_mul_of_le_of_lt {n m k l : ℕ} (Hk : k > 0) (H1 : n ≤ k) (H2 : m < l) :
n * m < k * l :=
lt_of_le_of_lt (mul_le_mul_right m H1) (mul_lt_mul_of_pos_left H2 Hk)
theorem mul_lt_mul_of_lt_of_le {n m k l : ℕ} (Hl : l > 0) (H1 : n < k) (H2 : m ≤ l) :
n * m < k * l :=
lt_of_le_of_lt (mul_le_mul_left n H2) (mul_lt_mul_of_pos_right H1 Hl)
theorem mul_lt_mul_of_le_of_le {n m k l : ℕ} (H1 : n < k) (H2 : m < l) : n * m < k * l :=
have H3 : n * m ≤ k * m, from mul_le_mul_right m (le_of_lt H1),
have H4 : k * m < k * l, from mul_lt_mul_of_pos_left H2 (lt_of_le_of_lt !zero_le H1),
lt_of_le_of_lt H3 H4
theorem eq_of_mul_eq_mul_left {m k n : ℕ} (Hn : n > 0) (H : n * m = n * k) : m = k :=
have n * m ≤ n * k, by rewrite H,
have m ≤ k, from le_of_mul_le_mul_left this Hn,
have n * k ≤ n * m, by rewrite H,
have k ≤ m, from le_of_mul_le_mul_left this Hn,
le.antisymm `m ≤ k` this
theorem eq_of_mul_eq_mul_right {n m k : ℕ} (Hm : m > 0) (H : n * m = k * m) : n = k :=
eq_of_mul_eq_mul_left Hm (!mul.comm ▸ !mul.comm ▸ H)
theorem eq_zero_or_eq_of_mul_eq_mul_left {n m k : ℕ} (H : n * m = n * k) : n = 0 ∨ m = k :=
or_of_or_of_imp_right !eq_zero_or_pos
(assume Hn : n > 0, eq_of_mul_eq_mul_left Hn H)
theorem eq_zero_or_eq_of_mul_eq_mul_right {n m k : ℕ} (H : n * m = k * m) : m = 0 ∨ n = k :=
eq_zero_or_eq_of_mul_eq_mul_left (!mul.comm ▸ !mul.comm ▸ H)
theorem eq_one_of_mul_eq_one_right {n m : ℕ} (H : n * m = 1) : n = 1 :=
have H2 : n * m > 0, by rewrite H; apply succ_pos,
or.elim (le_or_gt n 1)
(suppose n ≤ 1,
have n > 0, from pos_of_mul_pos_right H2,
show n = 1, from le.antisymm `n ≤ 1` (succ_le_of_lt this))
(suppose n > 1,
have m > 0, from pos_of_mul_pos_left H2,
have n * m ≥ 2 * 1, from nat.mul_le_mul (succ_le_of_lt `n > 1`) (succ_le_of_lt this),
have 1 ≥ 2, from !mul_one ▸ H ▸ this,
absurd !lt_succ_self (not_lt_of_ge this))
theorem eq_one_of_mul_eq_one_left {n m : ℕ} (H : n * m = 1) : m = 1 :=
eq_one_of_mul_eq_one_right (!mul.comm ▸ H)
theorem eq_one_of_mul_eq_self_left {n m : ℕ} (Hpos : n > 0) (H : m * n = n) : m = 1 :=
eq_of_mul_eq_mul_right Hpos (H ⬝ !one_mul⁻¹)
theorem eq_one_of_mul_eq_self_right {n m : ℕ} (Hpos : m > 0) (H : m * n = m) : n = 1 :=
eq_one_of_mul_eq_self_left Hpos (!mul.comm ▸ H)
theorem eq_one_of_dvd_one {n : ℕ} (H : n ∣ 1) : n = 1 :=
dvd.elim H
(take m, suppose 1 = n * m,
eq_one_of_mul_eq_one_right this⁻¹)
/- min and max -/
open decidable
theorem min_zero [simp] (a : ℕ) : min a 0 = 0 :=
by rewrite [min_eq_right !zero_le]
theorem zero_min [simp] (a : ℕ) : min 0 a = 0 :=
by rewrite [min_eq_left !zero_le]
theorem max_zero [simp] (a : ℕ) : max a 0 = a :=
by rewrite [max_eq_left !zero_le]
theorem zero_max [simp] (a : ℕ) : max 0 a = a :=
by rewrite [max_eq_right !zero_le]
theorem min_succ_succ [simp] (a b : ℕ) : min (succ a) (succ b) = succ (min a b) :=
or.elim !lt_or_ge
(suppose a < b, by rewrite [min_eq_left_of_lt this, min_eq_left_of_lt (succ_lt_succ this)])
(suppose a ≥ b, by rewrite [min_eq_right this, min_eq_right (succ_le_succ this)])
theorem max_succ_succ [simp] (a b : ℕ) : max (succ a) (succ b) = succ (max a b) :=
or.elim !lt_or_ge
(suppose a < b, by rewrite [max_eq_right_of_lt this, max_eq_right_of_lt (succ_lt_succ this)])
(suppose a ≥ b, by rewrite [max_eq_left this, max_eq_left (succ_le_succ this)])
/- In algebra.ordered_group, these next four are only proved for additive groups, not additive
semigroups. -/
protected theorem min_add_add_left (a b c : ℕ) : min (a + b) (a + c) = a + min b c :=
decidable.by_cases
(suppose b ≤ c,
assert a + b ≤ a + c, from add_le_add_left this _,
by rewrite [min_eq_left `b ≤ c`, min_eq_left this])
(suppose ¬ b ≤ c,
assert c ≤ b, from le_of_lt (lt_of_not_ge this),
assert a + c ≤ a + b, from add_le_add_left this _,
by rewrite [min_eq_right `c ≤ b`, min_eq_right this])
protected theorem min_add_add_right (a b c : ℕ) : min (a + c) (b + c) = min a b + c :=
by rewrite [add.comm a c, add.comm b c, add.comm _ c]; apply nat.min_add_add_left
protected theorem max_add_add_left (a b c : ℕ) : max (a + b) (a + c) = a + max b c :=
decidable.by_cases
(suppose b ≤ c,
assert a + b ≤ a + c, from add_le_add_left this _,
by rewrite [max_eq_right `b ≤ c`, max_eq_right this])
(suppose ¬ b ≤ c,
assert c ≤ b, from le_of_lt (lt_of_not_ge this),
assert a + c ≤ a + b, from add_le_add_left this _,
by rewrite [max_eq_left `c ≤ b`, max_eq_left this])
protected theorem max_add_add_right (a b c : ℕ) : max (a + c) (b + c) = max a b + c :=
by rewrite [add.comm a c, add.comm b c, add.comm _ c]; apply nat.max_add_add_left
/- least and greatest -/
section least_and_greatest
variable (P : ℕ → Prop)
variable [decP : ∀ n, decidable (P n)]
include decP
-- returns the least i < n satisfying P, or n if there is none
definition least : ℕ → ℕ
| 0 := 0
| (succ n) := if P (least n) then least n else succ n
theorem least_of_bound {n : ℕ} (H : P n) : P (least P n) :=
begin
induction n with [m, ih],
rewrite ↑least,
apply H,
rewrite ↑least,
cases decidable.em (P (least P m)) with [Hlp, Hlp],
rewrite [if_pos Hlp],
apply Hlp,
rewrite [if_neg Hlp],
apply H
end
theorem least_le (n : ℕ) : least P n ≤ n:=
begin
induction n with [m, ih],
{rewrite ↑least},
rewrite ↑least,
cases decidable.em (P (least P m)) with [Psm, Pnsm],
rewrite [if_pos Psm],
apply le.trans ih !le_succ,
rewrite [if_neg Pnsm]
end
theorem least_of_lt {i n : ℕ} (ltin : i < n) (H : P i) : P (least P n) :=
begin
induction n with [m, ih],
exact absurd ltin !not_lt_zero,
rewrite ↑least,
cases decidable.em (P (least P m)) with [Psm, Pnsm],
rewrite [if_pos Psm],
apply Psm,
rewrite [if_neg Pnsm],
cases (lt_or_eq_of_le (le_of_lt_succ ltin)) with [Hlt, Heq],
exact absurd (ih Hlt) Pnsm,
rewrite Heq at H,
exact absurd (least_of_bound P H) Pnsm
end
theorem ge_least_of_lt {i n : ℕ} (ltin : i < n) (Hi : P i) : i ≥ least P n :=
begin
induction n with [m, ih],
exact absurd ltin !not_lt_zero,
rewrite ↑least,
cases decidable.em (P (least P m)) with [Psm, Pnsm],
rewrite [if_pos Psm],
cases (lt_or_eq_of_le (le_of_lt_succ ltin)) with [Hlt, Heq],
apply ih Hlt,
rewrite Heq,
apply least_le,
rewrite [if_neg Pnsm],
cases (lt_or_eq_of_le (le_of_lt_succ ltin)) with [Hlt, Heq],
apply absurd (least_of_lt P Hlt Hi) Pnsm,
rewrite Heq at Hi,
apply absurd (least_of_bound P Hi) Pnsm
end
theorem least_lt {n i : ℕ} (ltin : i < n) (Hi : P i) : least P n < n :=
lt_of_le_of_lt (ge_least_of_lt P ltin Hi) ltin
-- returns the largest i < n satisfying P, or n if there is none.
definition greatest : ℕ → ℕ
| 0 := 0
| (succ n) := if P n then n else greatest n
theorem greatest_of_lt {i n : ℕ} (ltin : i < n) (Hi : P i) : P (greatest P n) :=
begin
induction n with [m, ih],
{exact absurd ltin !not_lt_zero},
{cases (decidable.em (P m)) with [Psm, Pnsm],
{rewrite [↑greatest, if_pos Psm]; exact Psm},
{rewrite [↑greatest, if_neg Pnsm],
have neim : i ≠ m, from assume H : i = m, absurd (H ▸ Hi) Pnsm,
have ltim : i < m, from lt_of_le_of_ne (le_of_lt_succ ltin) neim,
apply ih ltim}}
end
theorem le_greatest_of_lt {i n : ℕ} (ltin : i < n) (Hi : P i) : i ≤ greatest P n :=
begin
induction n with [m, ih],
{exact absurd ltin !not_lt_zero},
{cases (decidable.em (P m)) with [Psm, Pnsm],
{rewrite [↑greatest, if_pos Psm], apply le_of_lt_succ ltin},
{rewrite [↑greatest, if_neg Pnsm],
have neim : i ≠ m, from assume H : i = m, absurd (H ▸ Hi) Pnsm,
have ltim : i < m, from lt_of_le_of_ne (le_of_lt_succ ltin) neim,
apply ih ltim}}
end
end least_and_greatest
end nat
|
092ca32ae8c3083b91909a151ad7da4d77970f23 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/control/random.lean | 26f1f24bbcc38a6661c56cfe412874f7ac5b5a62 | [
"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 | 9,741 | lean | /-
Copyright (c) 2020 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
-/
import control.monad.basic
import control.uliftable
import data.bitvec.basic
import data.int.basic
import data.stream.defs
import tactic.norm_num
/-!
# Rand Monad and Random Class
This module provides tools for formulating computations guided by randomness and for
defining objects that can be created randomly.
## Main definitions
* `rand` monad for computations guided by randomness;
* `random` class for objects that can be generated randomly;
* `random` to generate one object;
* `random_r` to generate one object inside a range;
* `random_series` to generate an infinite series of objects;
* `random_series_r` to generate an infinite series of objects inside a range;
* `io.mk_generator` to create a new random number generator;
* `io.run_rand` to run a randomized computation inside the `io` monad;
* `tactic.run_rand` to run a randomized computation inside the `tactic` monad
## Local notation
* `i .. j` : `Icc i j`, the set of values between `i` and `j` inclusively;
## Tags
random monad io
## References
* Similar library in Haskell: https://hackage.haskell.org/package/MonadRandom
-/
open list io applicative
universes u v w
/-- A monad to generate random objects using the generator type `g` -/
@[reducible]
def rand_g (g : Type) (α : Type u) : Type u := state (ulift.{u} g) α
/-- A monad to generate random objects using the generator type `std_gen` -/
@[reducible]
def rand := rand_g std_gen
instance (g : Type) : uliftable (rand_g.{u} g) (rand_g.{v} g) :=
@state_t.uliftable' _ _ _ _ _ (equiv.ulift.trans.{u u u u u} equiv.ulift.symm)
open ulift (hiding inhabited)
/-- Generate one more `ℕ` -/
def rand_g.next {g : Type} [random_gen g] : rand_g g ℕ :=
⟨ prod.map id up ∘ random_gen.next ∘ down ⟩
local infix ` .. `:41 := set.Icc
open stream
/-- `bounded_random α` gives us machinery to generate values of type `α` between certain bounds -/
class bounded_random (α : Type u) [preorder α] :=
(random_r : Π g [random_gen g] (x y : α),
(x ≤ y) → rand_g g (x .. y))
/-- `random α` gives us machinery to generate values of type `α` -/
class random (α : Type u) :=
(random [] : Π (g : Type) [random_gen g], rand_g g α)
/-- shift_31_left = 2^31; multiplying by it shifts the binary
representation of a number left by 31 bits, dividing by it shifts it
right by 31 bits -/
def shift_31_left : ℕ :=
by apply_normed 2^31
namespace rand
open stream
variables (α : Type u)
variables (g : Type) [random_gen g]
/-- create a new random number generator distinct from the one stored in the state -/
def split : rand_g g g := ⟨ prod.map id up ∘ random_gen.split ∘ down ⟩
variables {g}
section random
variables [random α]
export random (random)
/-- Generate a random value of type `α`. -/
def random : rand_g g α :=
random.random α g
/-- generate an infinite series of random values of type `α` -/
def random_series : rand_g g (stream α) :=
do gen ← uliftable.up (split g),
pure $ stream.corec_state (random.random α g) gen
end random
variables {α}
/-- Generate a random value between `x` and `y` inclusive. -/
def random_r [preorder α] [bounded_random α] (x y : α) (h : x ≤ y) : rand_g g (x .. y) :=
bounded_random.random_r g x y h
/-- generate an infinite series of random values of type `α` between `x` and `y` inclusive. -/
def random_series_r [preorder α] [bounded_random α] (x y : α) (h : x ≤ y) :
rand_g g (stream (x .. y)) :=
do gen ← uliftable.up (split g),
pure $ corec_state (bounded_random.random_r g x y h) gen
end rand
namespace io
private def accum_char (w : ℕ) (c : char) : ℕ :=
c.to_nat + 256 * w
/-- create and seed a random number generator -/
def mk_generator : io std_gen := do
seed ← io.rand 0 shift_31_left,
return $ mk_std_gen seed
variables {α : Type}
/-- Run `cmd` using a randomly seeded random number generator -/
def run_rand (cmd : _root_.rand α) : io α :=
do g ← io.mk_generator,
return $ (cmd.run ⟨g⟩).1
/-- Run `cmd` using the provided seed. -/
def run_rand_with (seed : ℕ) (cmd : _root_.rand α) : io α :=
return $ (cmd.run ⟨mk_std_gen seed⟩).1
section random
variables [random α]
/-- randomly generate a value of type α -/
def random : io α :=
io.run_rand (rand.random α)
/-- randomly generate an infinite series of value of type α -/
def random_series : io (stream α) :=
io.run_rand (rand.random_series α)
end random
section bounded_random
variables [preorder α] [bounded_random α]
/-- randomly generate a value of type α between `x` and `y` -/
def random_r (x y : α) (p : x ≤ y) : io (x .. y) :=
io.run_rand (bounded_random.random_r _ x y p)
/-- randomly generate an infinite series of value of type α between `x` and `y` -/
def random_series_r (x y : α) (h : x ≤ y) : io (stream $ x .. y) :=
io.run_rand (rand.random_series_r x y h)
end bounded_random
end io
namespace tactic
/-- create a seeded random number generator in the `tactic` monad -/
meta def mk_generator : tactic std_gen := do
tactic.unsafe_run_io @io.mk_generator
/-- run `cmd` using the a randomly seeded random number generator
in the tactic monad -/
meta def run_rand {α : Type u} (cmd : rand α) : tactic α := do
⟨g⟩ ← tactic.up mk_generator,
return (cmd.run ⟨g⟩).1
variables {α : Type u}
section bounded_random
variables [preorder α] [bounded_random α]
/-- Generate a random value between `x` and `y` inclusive. -/
meta def random_r (x y : α) (h : x ≤ y) : tactic (x .. y) :=
run_rand (rand.random_r x y h)
/-- Generate an infinite series of random values of type `α` between `x` and `y` inclusive. -/
meta def random_series_r (x y : α) (h : x ≤ y) : tactic (stream $ x .. y) :=
run_rand (rand.random_series_r x y h)
end bounded_random
section random
variables [random α]
/-- randomly generate a value of type α -/
meta def random : tactic α :=
run_rand (rand.random α)
/-- randomly generate an infinite series of value of type α -/
meta def random_series : tactic (stream α) :=
run_rand (rand.random_series α)
end random
end tactic
open nat (succ one_add mod_eq_of_lt zero_lt_succ add_one succ_le_succ)
variables {g : Type} [random_gen g]
open nat
namespace fin
variables {n : ℕ} [ne_zero n]
/-- generate a `fin` randomly -/
protected def random : rand_g g (fin n) :=
⟨ λ ⟨g⟩, prod.map of_nat' up $ rand_nat g 0 n ⟩
end fin
open nat
instance nat_bounded_random : bounded_random ℕ :=
{ random_r := λ g inst x y hxy,
do z ← @fin.random g inst (succ $ y - x) _,
pure ⟨z.val + x, nat.le_add_left _ _,
by rw ← le_tsub_iff_right hxy; apply le_of_succ_le_succ z.is_lt⟩ }
/-- This `bounded_random` interval generates integers between `x` and
`y` by first generating a natural number between `0` and `y - x` and
shifting the result appropriately. -/
instance int_bounded_random : bounded_random ℤ :=
{ random_r := λ g inst x y hxy,
do ⟨z,h₀,h₁⟩ ← @bounded_random.random_r ℕ _ _ g inst 0 (int.nat_abs $ y - x) dec_trivial,
pure ⟨z + x,
int.le_add_of_nonneg_left (int.coe_nat_nonneg _),
int.add_le_of_le_sub_right $ le_trans
(int.coe_nat_le_coe_nat_of_le h₁)
(le_of_eq $ int.of_nat_nat_abs_eq_of_nonneg (int.sub_nonneg_of_le hxy)) ⟩ }
instance fin_random (n : ℕ) [ne_zero n] : random (fin n) :=
{ random := λ g inst, @fin.random g inst _ _ }
instance fin_bounded_random (n : ℕ) : bounded_random (fin n) :=
{ random_r := λ g inst (x y : fin n) p,
do ⟨r, h, h'⟩ ← @rand.random_r ℕ g inst _ _ x.val y.val p,
pure ⟨⟨r,lt_of_le_of_lt h' y.is_lt⟩, h, h'⟩ }
/-- A shortcut for creating a `random (fin n)` instance from
a proof that `0 < n` rather than on matching on `fin (succ n)` -/
def random_fin_of_pos : ∀ {n : ℕ} (h : 0 < n), random (fin n)
| (succ n) _ := fin_random _
| 0 h := false.elim (nat.not_lt_zero _ h)
lemma bool_of_nat_mem_Icc_of_mem_Icc_to_nat (x y : bool) (n : ℕ) :
n ∈ (x.to_nat .. y.to_nat) → bool.of_nat n ∈ (x .. y) :=
begin
simp only [and_imp, set.mem_Icc], intros h₀ h₁,
split;
[ have h₂ := bool.of_nat_le_of_nat h₀, have h₂ := bool.of_nat_le_of_nat h₁ ];
rw bool.of_nat_to_nat at h₂; exact h₂,
end
instance : random bool :=
{ random := λ g inst,
(bool.of_nat ∘ subtype.val) <$> @bounded_random.random_r ℕ _ _ g inst 0 1 (nat.zero_le _) }
instance : bounded_random bool :=
{ random_r := λ g _inst x y p,
subtype.map bool.of_nat (bool_of_nat_mem_Icc_of_mem_Icc_to_nat x y) <$>
@bounded_random.random_r ℕ _ _ g _inst x.to_nat y.to_nat (bool.to_nat_le_to_nat p) }
/-- generate a random bit vector of length `n` -/
def bitvec.random (n : ℕ) : rand_g g (bitvec n) :=
bitvec.of_fin <$> rand.random (fin $ 2^n)
/-- generate a random bit vector of length `n` -/
def bitvec.random_r {n : ℕ} (x y : bitvec n) (h : x ≤ y) : rand_g g (x .. y) :=
have h' : ∀ (a : fin (2 ^ n)), a ∈ (x.to_fin .. y.to_fin) → bitvec.of_fin a ∈ (x .. y),
begin
simp only [and_imp, set.mem_Icc], intros z h₀ h₁,
replace h₀ := bitvec.of_fin_le_of_fin_of_le h₀,
replace h₁ := bitvec.of_fin_le_of_fin_of_le h₁,
rw bitvec.of_fin_to_fin at h₀ h₁, split; assumption,
end,
subtype.map bitvec.of_fin h' <$> rand.random_r x.to_fin y.to_fin (bitvec.to_fin_le_to_fin_of_le h)
open nat
instance random_bitvec (n : ℕ) : random (bitvec n) :=
{ random := λ _ inst, @bitvec.random _ inst n }
instance bounded_random_bitvec (n : ℕ) : bounded_random (bitvec n) :=
{ random_r := λ _ inst x y p, @bitvec.random_r _ inst _ _ _ p }
|
95ec7d8d5005c6d4c7829b508ca5cdfccc73cd1e | 4d7079ae603c07560e99d1ce35876f769cbb3e24 | /src/clones.lean | 4468356b8638b8615513b67a30f0b9a1295567a0 | [] | no_license | chasenorman/Formalized-Voting | 72493a08aa09d98d0fb589731b842e2d08c991d0 | de04e630b83525b042db166670ba97f9952b5691 | refs/heads/main | 1,687,282,160,284 | 1,627,155,031,000 | 1,627,155,031,000 | 370,472,025 | 13 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 126,959 | lean | import main
import split_cycle
import data.list.nodup
import tactic
open_locale classical
noncomputable theory
variables {V X : Type}
def minus_candidate (P : Prof V X) (b : X) : Prof V {x : X // x ≠ b} := λ v x y, P v x y
variables (P : Prof V X) (c : X) (D : set {x : X // x ≠ c})
def clones : Prop := D.nonempty ∧ (∀ (c' ∈ D) (x : {x : X // x ≠ c}) (i : V), x ∉ D → ((P i c x ↔ P i c' x) ∧ (P i x c ↔ P i x c')))
def non_clone_choice_ind_clones : VSCC → Prop := λ F, clones P c D → (∀ a : {x : X // x ≠ c}, a ∉ D → (a.val ∈ (F V X P) ↔ a ∈ (F V {x : X // x ≠ c} (minus_candidate P c))))
def clone_choice_ind_clones : VSCC → Prop := λ F, clones P c D → ((c ∉ (F V X P) ∧ (∀ c' : {x : X // x ≠ c}, c' ∈ D → c'.val ∉ (F V X P))) ↔ (∀ c' ∈ D, c' ∉ (F V {x : X // x ≠ c} (minus_candidate P c))))
lemma margin_eq_margin_minus_candidate [fintype V] {a b : {x : X // x ≠ c}} : margin P a b = margin (minus_candidate P c) a b :=
begin
obviously,
end
lemma margin_eq_clone_non_clone [fintype V] (b : {x : X // x ≠ c}) (e ∈ D) (p : b ∉ D) : clones P c D → margin P c b = margin (minus_candidate P c) e b :=
begin
intro clone,
unfold margin,
have m1 : ((finset.filter (λ (x_1 : V), P x_1 c ↑b) finset.univ).card) = ((finset.filter (λ (x_1 : V), minus_candidate P c x_1 e b) finset.univ).card),
apply finset.card_congr _ _ _ _,
intros a a_spec, exact a,
intro a, simp, unfold minus_candidate, intro p1,
unfold clones at clone,
cases clone with n clone,
have clone := clone e H b a p,
exact clone.left.mp p1,
intros v1 v2 hv1 hv2, simp,
intros a ha,
use a,
simp, simp at ha,
cases clone with n clone,
have clone := clone e H b a p,
exact clone.left.mpr ha,
have m2 : ((finset.filter (λ (x_1 : V), P x_1 ↑b c) finset.univ).card) = ((finset.filter (λ (x_1 : V), minus_candidate P c x_1 b e) finset.univ).card),
apply finset.card_congr _ _ _ _,
intros a a_spec, exact a,
intro a, simp, unfold minus_candidate, intro p1,
unfold clones at clone,
cases clone with n clone,
have clone := clone e H b a p,
exact clone.right.mp p1,
intros v1 v2 hv1 hv2, simp,
intros a ha,
use a,
simp, simp at ha,
cases clone with n clone,
have clone := clone e H b a p,
exact clone.right.mpr ha,
rw m1, rw m2,
end
lemma margin_eq_clone_non_clone' [fintype V] {b : {x : X // x ≠ c}} {e : {x : X // x ≠ c}} (H : e ∈ D) (p : b ∉ D) : clones P c D → margin P b c = margin (minus_candidate P c) b e :=
begin
intro clone,
have x := margin_eq_clone_non_clone P c D b e H p clone,
rw margin_antisymmetric,
rw x,
rw margin_antisymmetric,
rw int.neg_neg,
end
-- y → d → d' → x
def remove_clones : list X → list X := λ l, to_path (list.map (λ x : X, (ite (∀p : (x ≠ c), (⟨x,p⟩ : {x : X // x ≠ c}) ∈ D) c x)) l)
lemma replace_clones_helper {x : X} (h : ¬∀ (p : x ≠ c), (⟨x, p⟩ : { x : X // x ≠ c}) ∈ D) : x ≠ c :=
begin
obviously,
end
def replace_clones (d ∈ D) : list X → list {x : X // x ≠ c} := λ l, to_path (list.map (λ x, (dite (∀p : (x ≠ c), (⟨x,p⟩ : {x : X // x ≠ c}) ∈ D) (λ h, d) (λ h, ⟨x, replace_clones_helper c D h⟩)) ) l)
def remove_clones' : list {x : X // x ≠ c} → list X := λ l, to_path (l.map (λ x, (ite (x ∈ D) c x)))
lemma remove_clones_nil_iff (l : list X) : l = list.nil ↔ remove_clones c D l = list.nil :=
begin
split,
intro n,
rw n,
unfold remove_clones,
rw list.map_nil _,
rw to_path_eq_nil_iff,
intro n,
unfold remove_clones at n,
rw to_path_eq_nil_iff at n,
rw list.map_eq_nil at n,
exact n,
end
lemma remove_clones_ne_nil_iff (l : list X) : l ≠ list.nil ↔ remove_clones c D l ≠ list.nil := not_iff_not.mpr (remove_clones_nil_iff c D l)
lemma remove_clones_nodup (l : list X) : (remove_clones c D l).nodup :=
begin
unfold remove_clones,
apply to_path_nodup,
end
lemma remove_clones_first_elem (l : list X) (a : X) (e : ∀(p : a ≠ c), (⟨a, p⟩ : {x : X // x ≠ c}) ∉ D) (n : l ≠ list.nil) : ((l.nth_le 0 (list.length_pos_of_ne_nil n)) = a) → (((remove_clones c D l).nth_le 0 (list.length_pos_of_ne_nil ((remove_clones_ne_nil_iff c D l).mp n))) = a) :=
begin
unfold remove_clones,
intro m,
rw to_path_first_elem,
rw list.nth_le_map _ _ (list.length_pos_of_ne_nil n),
rw m,
simp,
intro p,
apply eq.symm,
contrapose m,
exfalso,
specialize e m,
specialize p m,
exact e p,
intro con,
rw list.map_eq_nil at con,
exact n con,
end
lemma remove_clones_last_elem (l : list X) (a : X) (e : ∀(p : a ≠ c), (⟨a, p⟩ : {x : X // x ≠ c}) ∉ D) (n : l ≠ list.nil) : ((l.last n) = a) → (((remove_clones c D l).last ((remove_clones_ne_nil_iff c D l).mp n)) = a) :=
begin
unfold remove_clones,
intro m,
rw to_path_last_elem,
rw list.last_map,
rw m,
simp,
intro con,
apply eq.symm,
contrapose m,
exfalso,
specialize e m,
specialize con m,
exact e con,
end
lemma remove_clones'_nil_iff (l : list {x : X // x ≠ c}) : l = list.nil ↔ remove_clones' c D l = list.nil :=
begin
split,
intro n,
rw n,
unfold remove_clones',
rw list.map_nil _,
rw to_path_eq_nil_iff,
intro n,
unfold remove_clones' at n,
rw to_path_eq_nil_iff at n,
rw list.map_eq_nil at n,
exact n,
end
lemma remove_clones'_ne_nil_iff (l : list {x : X // x ≠ c}) : l ≠ list.nil ↔ remove_clones' c D l ≠ list.nil := not_iff_not.mpr (remove_clones'_nil_iff c D l)
lemma remove_clones'_nodup (l : list {x : X // x ≠ c}) : (remove_clones' c D l).nodup :=
begin
unfold remove_clones',
apply to_path_nodup,
end
lemma remove_clones'_first_elem (l : list {x : X // x ≠ c}) (a : {x : X // x ≠ c}) (e : a ∉ D) (n : l ≠ list.nil) : ((l.nth_le 0 (list.length_pos_of_ne_nil n)) = a) → (((remove_clones' c D l).nth_le 0 (list.length_pos_of_ne_nil ((remove_clones'_ne_nil_iff c D l).mp n))) = a) :=
begin
unfold remove_clones',
intro m,
rw to_path_first_elem,
rw list.nth_le_map _ _ (list.length_pos_of_ne_nil n),
rw m,
simp,
intro p,
exfalso,
exact e p,
intro con,
rw list.map_eq_nil at con,
exact n con,
end
lemma remove_clones'_last_elem (l : list {x : X // x ≠ c}) (a : {x : X // x ≠ c}) (e : a ∉ D) (n : l ≠ list.nil) : ((l.last n) = a) → (((remove_clones' c D l).last ((remove_clones'_ne_nil_iff c D l).mp n)) = a) :=
begin
unfold remove_clones',
intro m,
rw to_path_last_elem,
rw list.last_map,
rw m,
simp,
intro con,
exfalso,
exact e con,
end
lemma replace_clones_nil_iff (l : list X) {d ∈ D} : l = list.nil ↔ replace_clones c D d H l = list.nil :=
begin
split,
intro n,
rw n,
unfold replace_clones,
rw list.map_nil _,
rw to_path_eq_nil_iff,
intro n,
unfold replace_clones at n,
rw to_path_eq_nil_iff at n,
rw list.map_eq_nil at n,
exact n,
end
lemma replace_clones_ne_nil_iff (d ∈ D) (l : list X) : l ≠ list.nil ↔ replace_clones c D d H l ≠ list.nil := not_iff_not.mpr (replace_clones_nil_iff c D l)
lemma replace_clones_nodup (l : list X) {d ∈ D} : (replace_clones c D d H l).nodup :=
begin
unfold replace_clones,
apply to_path_nodup,
end
lemma replace_clones_first_elem (l : list X) (a : X) (e : ¬ ∀(p : a ≠ c), (⟨a, p⟩ : {x : X // x ≠ c}) ∈ D) (n : l ≠ list.nil) {d ∈ D} : ((l.nth_le 0 (list.length_pos_of_ne_nil n)) = a) → (((replace_clones c D d H l).nth_le 0 (list.length_pos_of_ne_nil ((replace_clones_ne_nil_iff c D d H l).mp n))) = ⟨a, replace_clones_helper c D e⟩) :=
begin
unfold replace_clones,
intro m,
rw to_path_first_elem,
rw list.nth_le_map _ _ (list.length_pos_of_ne_nil n),
rw m,
split_ifs,
refl,
apply (not_iff_not.mpr list.map_eq_nil).mpr,
exact n,
end
lemma replace_clones_last_elem (l : list X) (a : X) (e : ¬∀(p : a ≠ c), (⟨a, p⟩ : {x : X // x ≠ c}) ∈ D) (n : l ≠ list.nil) {d ∈ D} : ((l.last n) = a) → (((replace_clones c D d H l).last ((replace_clones_ne_nil_iff c D d H l).mp n)) = ⟨a, replace_clones_helper c D e⟩) :=
begin
unfold replace_clones,
intro m,
rw to_path_last_elem,
rw list.last_map,
rw m,
split_ifs,
refl,
end
lemma replace_clones_last_c (l : list X) (n : l ≠ list.nil) {d ∈ D} : ((l.last n) = c) → (((replace_clones c D d H l).last ((replace_clones_ne_nil_iff c D d H l).mp n)) = d) :=
begin
unfold replace_clones,
intro m,
rw to_path_last_elem,
rw list.last_map,
rw m,
split_ifs,
refl,
push_neg at h,
cases h with contr h,
exfalso,
exact false_of_ne contr,
end
lemma remove_clones_chain'_of_chain' [fintype V] (l : list X) (b : {x : X // x ≠ c}) (e : b ∉ D) (d ∈ D) (clone : clones P c D) :
list.chain' (λ (a b_1 : X), margin P ↑d ↑b ≤ margin P a b_1) l → list.chain' (λ (a b_1 : X), margin P c ↑b ≤ margin P a b_1) (remove_clones c D l) :=
begin
intro a,
induction l,
{obviously,},
unfold remove_clones,
simp [to_path],
specialize l_ih (and.right (list.chain'_cons'.mp a)),
by_cases (list.index_of (ite (∀ (h : ¬l_hd = c), (⟨l_hd, h⟩ : {x : X // x ≠ c}) ∈ D) c l_hd) (to_path (list.map (λ (x : X), ite (∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D) c x) l_tl)) < (to_path (list.map (λ (x : X), ite (∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D) c x) l_tl)).length),
simp_rw (ite_left_if h),
exact drop_chain'_of_chain' l_ih,
simp_rw (ite_right_if h),
by_cases j : l_tl = list.nil,
rw j,
rw list.map_nil,
rw ←j,
rw (to_path_eq_nil_iff l_tl).mpr j,
exact list.chain'_singleton (ite (∀ (h : ¬l_hd = c), (⟨l_hd, _⟩ : {x : X // x ≠ c}) ∈ D) c l_hd),
rw list.chain'_iff_nth_le,
rw list.chain'_iff_nth_le at l_ih,
rw list.chain'_iff_nth_le at a,
intros i i_bounds,
have nodup := h,
by_cases i = 0,
have i_eq := h,
simp_rw h,
rw [list.nth_le], rw [list.nth_le],
specialize a 0,
have a_proof : 0 < (l_hd :: l_tl).length - 1,
rw list.length_cons,
simp only [nat.add_succ_sub_one, add_zero],
exact (list.length_pos_of_ne_nil j),
specialize a a_proof,
rw [list.nth_le] at a, rw [list.nth_le] at a,
have x := (to_path_first_elem X l_tl j),
rw to_path_first_elem',
simp,
by_cases (∀ (h : ¬l_hd = c), (⟨l_hd, h⟩ : {x : X // x ≠ c}) ∈ D),
rw ite_left_if h,
have h' := h,
by_cases (∀ (h_1 : ¬l_tl.nth_le 0 (list.length_pos_of_ne_nil j) = c), (⟨l_tl.nth_le 0 (list.length_pos_of_ne_nil j), h_1⟩ : {x : X // x ≠ c}) ∈ D),
exfalso,
contrapose nodup,
push_neg,
rw list.index_of_lt_length,
rw ite_left_if h',
rw list.mem_iff_nth_le,
use 0,
have len : 0 < (to_path (list.map (λ (x : X), ite (∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D) c x) l_tl)).length,
change (0 < (remove_clones c D l_tl).length),
apply list.length_pos_of_ne_nil,
rw ←remove_clones_ne_nil_iff,
exact j,
use len,
rw to_path_first_elem',
rw list.nth_le_map (λ (x : X), ite (∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D) c x) _ (list.length_pos_of_ne_nil j),
simp,
intro w,
specialize h w,
intro contr,
exfalso, exact contr h,
rw ite_right_if h,
push_neg at h,
cases h with not_c not_d,
by_cases l_hd = c,
rw h at a,
rw margin_eq_clone_non_clone P c D b d H e clone,
rw ←margin_eq_margin_minus_candidate,
exact a,
specialize h' h,
rw margin_eq_clone_non_clone P c D b d H e clone,
have r := margin_eq_clone_non_clone P c D ⟨l_tl.nth_le 0 _, not_c⟩ ⟨l_hd, h⟩ h' not_d clone,
simp at r,
rw r,
rw ←margin_eq_margin_minus_candidate, rw ←margin_eq_margin_minus_candidate,
exact a,
rw ite_right_if h,
have h' := h,
by_cases (∀ (h_1 : ¬l_tl.nth_le 0 (list.length_pos_of_ne_nil j) = c), (⟨l_tl.nth_le 0 (list.length_pos_of_ne_nil j), h_1⟩ : {x : X // x ≠ c}) ∈ D),
rw ite_left_if h,
push_neg at h',
cases h' with not_c not_d,
have h'' := h,
by_cases (l_tl.nth_le 0 (list.nth_le._main._proof_1 l_hd l_tl 0 (nat.lt_pred_iff.mp a_proof))) = c,
rw h at a,
rw margin_eq_clone_non_clone P c D b d H e clone,
rw ←margin_eq_margin_minus_candidate,
exact a,
specialize h'' h,
rw margin_eq_clone_non_clone P c D b d H e clone,
have r := margin_eq_clone_non_clone' P c D h'' not_d clone,
simp at r,
rw r,
rw ←margin_eq_margin_minus_candidate, rw ←margin_eq_margin_minus_candidate,
exact a,
rw ite_right_if h,
rw margin_eq_clone_non_clone P c D b d H e clone,
exact a,
rw [list.nth_le],
specialize l_ih (i - 1),
rw list.length_cons at i_bounds,
simp only [nat.add_succ_sub_one, add_zero] at i_bounds,
have o : ∀ i n, ¬ i = 0 → i < n → i - 1 < n - 1 := by omega,
specialize l_ih (o i (to_path (list.map (λ (x : X), ite (∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D) c x) l_tl)).length h i_bounds),
--change (margin P c ↑b ≤ margin P ((((ite (∀ (h : ¬l_hd = c), (⟨l_hd, h⟩ : {x : X // x ≠ c}) ∈ D) c l_hd)) :: (remove_clones c D l_tl)).nth_le i (nat.lt_of_lt_pred i_bounds)) ((remove_clones c D l_tl).nth_le i _)),
rw nth_le_cons h,
have o : ∀ i, ¬ i = 0 → i - 1 + 1 = i := by omega,
simp_rw (o i h) at l_ih,
exact l_ih,
end
lemma nodup_lift_of_nodup {l : list {x : X // x ≠ c}} : l.nodup → (lift l : list X).nodup :=
begin
intro n,
unfold lift,
unfold has_lift.lift,
rw list.nodup_map_iff_inj_on,
obviously,
end
lemma lift_ne_nil_iff {l : list {x : X // x ≠ c}} : (lift l : list X) ≠ list.nil ↔ l ≠ list.nil:=
begin
unfold lift, unfold has_lift.lift, change (¬ list.map coe l = list.nil ↔ l ≠ list.nil),
rw list.map_eq_nil,
end
lemma dite_left_if {p : Prop} {α : Type} [decidable p] {a : p → α} {b : ¬ p → α} (proof : p) : dite p a b = a proof :=
begin
split_ifs, refl,
end
lemma dite_right_if {p : Prop} {α : Type} [decidable p] {a : p → α} {b : ¬ p → α} (proof : ¬p) : ¬ p → dite p a b = b proof :=
begin
split_ifs, intro unused, refl,
end
--set_option pp.all true
lemma A6_chain [fintype V] (l : list X) {b : {x : X // x ≠ c}} {d : {x : X // x ≠ c}} {d' : {x : X // x ≠ c}} (H : d ∈ D) (clone : clones P c D) : list.chain' (λ (a b_1 : X), margin P ↑b ↑d' ≤ margin P a b_1) l
→ list.chain' (λ (a b_1 : {x // x ≠ c}), margin (minus_candidate P c) b d' ≤ margin (minus_candidate P c) a b_1) (replace_clones c D d H l) :=
begin
intro a,
induction l,
{obviously,},
unfold replace_clones,
simp [to_path],
specialize l_ih (and.right (list.chain'_cons'.mp a)),
by_cases @has_lt.lt.{0} nat nat.has_lt
(@list.index_of.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(λ (a b : @subtype.{1} X (λ (x : X), @ne.{1} X x c)),
classical.prop_decidable (@eq.{1} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)) a b))
(@dite.{1} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D)
(@forall_prop_decidable (not (@eq.{1} X l_hd c))
(λ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D)
(@ne.decidable.{1} X (λ (a b : X), classical.prop_decidable (@eq.{1} X a b)) l_hd c)
(λ (h : not (@eq.{1} X l_hd c)),
@set.decidable_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)) D
(λ (a : @subtype.{1} X (λ (x : X), @ne.{1} X x c)), classical.prop_decidable (D a))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))))
(λ
(h :
∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D), d)
(λ
(h :
not
(∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D)),
@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@replace_clones_helper X c D l_hd
(@iff.mpr
(not
(∀ (p : @ne.{1} X l_hd c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd p)
D))
(not
(∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D))
(@not_iff_not_of_iff
(∀ (p : @ne.{1} X l_hd c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd p)
D)
(∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D)
(@forall_prop_congr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(λ (h : @ne.{1} X l_hd c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd h)
D)
(λ (h : @ne.{1} X l_hd c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd h)
D)
(λ (h : @ne.{1} X l_hd c),
iff.refl
(@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd h)
D))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))))
h))))
(@to_path (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(@list.map.{0 0} X (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(λ (x : X),
@dite.{1} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@forall_prop_decidable (not (@eq.{1} X x c))
(λ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@ne.decidable.{1} X (λ (a b : X), classical.prop_decidable (@eq.{1} X a b)) x c)
(λ (h : not (@eq.{1} X x c)),
@set.decidable_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)) D
(λ (a : @subtype.{1} X (λ (x : X), @ne.{1} X x c)), classical.prop_decidable (D a))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))))
(λ
(h :
∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D), d)
(λ
(h :
not
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)),
@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@replace_clones_helper X c D x
(@iff.mpr
(not
(∀ (p : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x p)
D))
(not
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D))
(@not_iff_not_of_iff
(∀ (p : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x p)
D)
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@forall_prop_congr (@ne.{1} X x c) (not (@eq.{1} X x c))
(λ (h : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D)
(λ (h : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D)
(λ (h : @ne.{1} X x c),
iff.refl
(@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))))
h))))
l_tl)))
(@list.length.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(@to_path (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(@list.map.{0 0} X (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(λ (x : X),
@dite.{1} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@forall_prop_decidable (not (@eq.{1} X x c))
(λ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@ne.decidable.{1} X (λ (a b : X), classical.prop_decidable (@eq.{1} X a b)) x c)
(λ (h : not (@eq.{1} X x c)),
@set.decidable_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)) D
(λ (a : @subtype.{1} X (λ (x : X), @ne.{1} X x c)), classical.prop_decidable (D a))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))))
(λ
(h :
∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D), d)
(λ
(h :
not
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)),
@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@replace_clones_helper X c D x
(@iff.mpr
(not
(∀ (p : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x p)
D))
(not
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D))
(@not_iff_not_of_iff
(∀ (p : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x p)
D)
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@forall_prop_congr (@ne.{1} X x c) (not (@eq.{1} X x c))
(λ (h : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D)
(λ (h : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D)
(λ (h : @ne.{1} X x c),
iff.refl
(@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))))
h))))
l_tl))),
simp_rw (ite_left_if h),
exact drop_chain'_of_chain' l_ih,
simp_rw (ite_right_if h),
by_cases j : l_tl = list.nil,
rw j,
simp [to_path],
rw list.chain'_iff_nth_le,
rw list.chain'_iff_nth_le at l_ih,
rw list.chain'_iff_nth_le at a,
intros i i_bounds,
rename h nodup,
by_cases i = 0,
rename h i_eq,
simp_rw i_eq,
rw [list.nth_le], rw [list.nth_le],
specialize a 0,
have a_proof : 0 < (l_hd :: l_tl).length - 1,
rw list.length_cons,
simp only [nat.add_succ_sub_one, add_zero],
exact (list.length_pos_of_ne_nil j),
specialize a a_proof,
rw [list.nth_le] at a, rw [list.nth_le] at a,
have x := (to_path_first_elem X l_tl j),
rw to_path_first_elem',
simp,
split_ifs with h',
by_cases (∀ (h_1 : ¬l_tl.nth_le 0 (list.length_pos_of_ne_nil j) = c), (⟨l_tl.nth_le 0 (list.length_pos_of_ne_nil j), h_1⟩ : {x : X // x ≠ c}) ∈ D),
exfalso,
contrapose nodup,
push_neg,
rw list.index_of_lt_length,
rw dite_left_if h',
rw list.mem_iff_nth_le,
use 0,
have len : (0 < (replace_clones c D d H l_tl).length),
apply list.length_pos_of_ne_nil,
rw ←replace_clones_ne_nil_iff,
exact j,
use len,
rw to_path_first_elem',
rw list.nth_le_map (λ (x : X), dite (∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D) (λ (h : ∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D), d) (λ (h : ¬∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D), ⟨x, replace_clones_helper c D h⟩)) _ (list.length_pos_of_ne_nil j),
simp,
rw dite_left_if h,
rw dite_right_if h,
push_neg at h,
cases h with not_c not_d,
by_cases l_hd = c,
rw h at a,
rw ←margin_eq_clone_non_clone P c D ⟨l_tl.nth_le 0 _, not_c⟩ d H not_d clone,
rw ←margin_eq_margin_minus_candidate,
exact a,
specialize h' h,
rw ←margin_eq_clone_non_clone P c D ⟨l_tl.nth_le 0 _, not_c⟩ d H not_d clone,
rw margin_eq_clone_non_clone P c D ⟨l_tl.nth_le 0 _, not_c⟩ ⟨l_hd, h⟩ h' not_d clone,
rw ←margin_eq_margin_minus_candidate, rw ←margin_eq_margin_minus_candidate,
exact a,
exact h,
by_cases (∀ (h_1 : ¬l_tl.nth_le 0 (list.length_pos_of_ne_nil j) = c), (⟨l_tl.nth_le 0 (list.length_pos_of_ne_nil j), h_1⟩ : {x : X // x ≠ c}) ∈ D),
rw dite_left_if h,
push_neg at h',
cases h' with not_c not_d,
rename h h'',
by_cases (l_tl.nth_le 0 (list.nth_le._main._proof_1 l_hd l_tl 0 (nat.lt_pred_iff.mp a_proof))) = c,
rw h at a,
rw ←margin_eq_clone_non_clone' P c D H not_d clone,
rw ←margin_eq_margin_minus_candidate,
exact a,
specialize h'' h,
rw ←margin_eq_clone_non_clone' P c D H not_d clone,
rw margin_eq_clone_non_clone' P c D h'' not_d clone,
rw ←margin_eq_margin_minus_candidate, rw ←margin_eq_margin_minus_candidate,
exact a,
rw dite_right_if h,
rw ←margin_eq_margin_minus_candidate, rw ←margin_eq_margin_minus_candidate,
exact a,
exact h,
rw [list.nth_le],
specialize l_ih (i - 1),
rw list.length_cons at i_bounds,
simp only [nat.add_succ_sub_one, add_zero] at i_bounds,
have o : ∀ i n, ¬ i = 0 → i < n → i - 1 < n - 1 := by omega,
specialize l_ih (o i (replace_clones c D d H l_tl).length h i_bounds),
rw nth_le_cons h,
have o : ∀ i, ¬ i = 0 → i - 1 + 1 = i := by omega,
simp_rw (o i h) at l_ih,
exact l_ih,
end
-- A.5 might not be true if a and b are in D?
lemma clone_maintains_defeat (a b : {x : X // x ≠ c}) (H : b ∉ D) : clones P c D → ((split_cycle_VCCR V {x : X // x ≠ c} (minus_candidate P c)) a b ↔ (split_cycle_VCCR V X P) a b) :=
begin
intro clone,
rw split_cycle_definitions,
unfold split_cycle_VCCR',
simp,
split,
intro f,
introI fV,
cases f with m f,
rw margin_eq_margin_minus_candidate,
use m,
intros l n ne_nil b_mem a_mem,
have clone' := clone,
unfold clones at clone,
cases clone with n clone,
let d := (ite (a ∈ D) a n.some),
have d_spec : d ∈ D,
{change (ite (a ∈ D) a n.some) ∈ D, split_ifs, exact h, exact n.some_spec, },
specialize f (replace_clones c D d d_spec l),
specialize f (replace_clones_nodup c D l),
specialize f ((replace_clones_ne_nil_iff c D d d_spec l).mp ne_nil),
have b_not : ¬∀ (p : ↑b ≠ c), (⟨↑b, p⟩ : {x : X // x ≠ c}) ∈ D,
{push_neg, use b.property, simp, exact H,},
rw (replace_clones_first_elem c D l b b_not ne_nil b_mem) at f,
simp at f,
have last_elem : (replace_clones c D d d_spec l).last ((replace_clones_ne_nil_iff c D d d_spec l).mp ne_nil) = a,
{change (replace_clones c D (ite (a ∈ D) a n.some) d_spec l).last ((replace_clones_ne_nil_iff c D d d_spec l).mp ne_nil) = a,
split_ifs, unfold replace_clones, rw to_path_last_elem, rw list.last_map, split_ifs,
refl, push_neg at h_1, simp_rw a_mem, simp, exact ne_nil, rw replace_clones_last_elem c D l _ _ ne_nil a_mem, simp,
push_neg, use a.property, simp, exact h, },
specialize f last_elem,
contrapose f,
push_neg at f, push_neg,
exact A6_chain P c D l d_spec clone' f,
intro f,
introI fV,
cases f with m f,
rw ←margin_eq_margin_minus_candidate,
use m,
intros l l_nodup ne_nil b_mem a_mem,
specialize f ↑l,
contrapose f,
push_neg,
split,
exact nodup_lift_of_nodup c l_nodup,
use (lift_ne_nil_iff c).mpr ne_nil,
unfold coe, unfold lift_t, unfold has_lift_t.lift,
unfold lift, unfold has_lift.lift, unfold coe_t,
unfold has_coe_t.coe, unfold coe_b, unfold has_coe.coe,
split,
rw list.nth_le_map, rw b_mem, refl,
split,
have test := list.last_map (coe : {x // x ≠ c} → X) ne_nil,
rw a_mem at test,
change (list.map coe l).last _ = ↑a,
rw ←test,
refl,
push_neg at f,
apply (list.chain'_map (coe : {x // x ≠ c} → X)).mpr,
refine list.chain'.imp _ f,
intros a_1 b_1 m_1,
rw ←margin_eq_margin_minus_candidate at m_1,
exact m_1,
end
lemma clone_maintains_defeat' (a b : {x : X // x ≠ c}) (H : a ∉ D) (H' : b ∈ D) : clones P c D → ((split_cycle_VCCR V {x : X // x ≠ c} (minus_candidate P c)) a b ↔ (split_cycle_VCCR V X P) a b) :=
begin
intro clone,
rw split_cycle_definitions,
unfold split_cycle_VCCR',
simp,
split,
intro f,
introI fV,
cases f with m f,
rw margin_eq_margin_minus_candidate,
use m,
intros l n ne_nil b_mem a_mem,
have clone' := clone,
unfold clones at clone,
cases clone with n clone,
let d := b,
let d_spec := H',
specialize f (replace_clones c D d d_spec l),
specialize f (replace_clones_nodup c D l),
specialize f ((replace_clones_ne_nil_iff c D d d_spec l).mp ne_nil),
have b_not : ¬∀ (p : ↑a ≠ c), (⟨↑a, p⟩ : {x : X // x ≠ c}) ∈ D,
{push_neg, use a.property, simp, exact H,},
contrapose f, push_neg, push_neg at f,
split,
unfold replace_clones,
rw to_path_first_elem',
rw list.nth_le_map,
split_ifs,
refl,
simp_rw b_mem,
simp,
exact list.length_pos_of_ne_nil ne_nil,
split,
rw (replace_clones_last_elem c D l a b_not ne_nil a_mem),
simp,
exact A6_chain P c D l d_spec clone' f,
intro f,
introI fV,
cases f with m f,
rw ←margin_eq_margin_minus_candidate,
use m,
intros l l_nodup ne_nil b_mem a_mem,
specialize f ↑l,
contrapose f,
push_neg at f,
push_neg,
split,
exact nodup_lift_of_nodup c l_nodup,
use (lift_ne_nil_iff c).mpr ne_nil,
unfold coe, unfold lift_t, unfold has_lift_t.lift,
unfold lift, unfold has_lift.lift, unfold coe_t,
unfold has_coe_t.coe, unfold coe_b, unfold has_coe.coe,
split,
rw list.nth_le_map, rw b_mem, refl,
split,
have test := list.last_map (coe : {x // x ≠ c} → X) ne_nil,
rw a_mem at test,
change (list.map coe l).last _ = ↑a,
rw ←test,
refl,
apply (list.chain'_map (coe : {x // x ≠ c} → X)).mpr,
refine list.chain'.imp _ f,
intros a_1 b_1 m_1,
rw ←margin_eq_margin_minus_candidate at m_1,
exact m_1,
end
lemma A5_chain {l : list X} [fintype V] {d : {x : X // x ≠ c}} {b : {x : X // x ≠ c}} (H : d ∈ D) (H' : b ∉ D) (clone : clones P c D) : list.chain' (λ (a b_1 : X), margin P c ↑b ≤ margin P a b_1) l → list.chain' (λ (a b_1 : {x // x ≠ c}), margin P ↑d ↑b ≤ margin P ↑a ↑b_1) (replace_clones c D d H l) :=
begin
intro a,
induction l,
{obviously,},
unfold replace_clones,
simp [to_path],
specialize l_ih (and.right (list.chain'_cons'.mp a)),
by_cases @has_lt.lt.{0} nat nat.has_lt
(@list.index_of.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(λ (a b : @subtype.{1} X (λ (x : X), @ne.{1} X x c)),
classical.prop_decidable (@eq.{1} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)) a b))
(@dite.{1} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D)
(@forall_prop_decidable (not (@eq.{1} X l_hd c))
(λ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D)
(@ne.decidable.{1} X (λ (a b : X), classical.prop_decidable (@eq.{1} X a b)) l_hd c)
(λ (h : not (@eq.{1} X l_hd c)),
@set.decidable_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)) D
(λ (a : @subtype.{1} X (λ (x : X), @ne.{1} X x c)), classical.prop_decidable (D a))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))))
(λ
(h :
∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D), d)
(λ
(h :
not
(∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D)),
@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@replace_clones_helper X c D l_hd
(@iff.mpr
(not
(∀ (p : @ne.{1} X l_hd c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd p)
D))
(not
(∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D))
(@not_iff_not_of_iff
(∀ (p : @ne.{1} X l_hd c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd p)
D)
(∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D)
(@forall_prop_congr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(λ (h : @ne.{1} X l_hd c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd h)
D)
(λ (h : @ne.{1} X l_hd c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd h)
D)
(λ (h : @ne.{1} X l_hd c),
iff.refl
(@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd h)
D))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))))
h))))
(@to_path (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(@list.map.{0 0} X (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(λ (x : X),
@dite.{1} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@forall_prop_decidable (not (@eq.{1} X x c))
(λ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@ne.decidable.{1} X (λ (a b : X), classical.prop_decidable (@eq.{1} X a b)) x c)
(λ (h : not (@eq.{1} X x c)),
@set.decidable_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)) D
(λ (a : @subtype.{1} X (λ (x : X), @ne.{1} X x c)), classical.prop_decidable (D a))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))))
(λ
(h :
∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D), d)
(λ
(h :
not
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)),
@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@replace_clones_helper X c D x
(@iff.mpr
(not
(∀ (p : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x p)
D))
(not
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D))
(@not_iff_not_of_iff
(∀ (p : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x p)
D)
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@forall_prop_congr (@ne.{1} X x c) (not (@eq.{1} X x c))
(λ (h : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D)
(λ (h : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D)
(λ (h : @ne.{1} X x c),
iff.refl
(@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))))
h))))
l_tl)))
(@list.length.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(@to_path (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(@list.map.{0 0} X (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(λ (x : X),
@dite.{1} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@forall_prop_decidable (not (@eq.{1} X x c))
(λ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@ne.decidable.{1} X (λ (a b : X), classical.prop_decidable (@eq.{1} X a b)) x c)
(λ (h : not (@eq.{1} X x c)),
@set.decidable_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)) D
(λ (a : @subtype.{1} X (λ (x : X), @ne.{1} X x c)), classical.prop_decidable (D a))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))))
(λ
(h :
∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D), d)
(λ
(h :
not
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)),
@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@replace_clones_helper X c D x
(@iff.mpr
(not
(∀ (p : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x p)
D))
(not
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D))
(@not_iff_not_of_iff
(∀ (p : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x p)
D)
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@forall_prop_congr (@ne.{1} X x c) (not (@eq.{1} X x c))
(λ (h : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D)
(λ (h : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D)
(λ (h : @ne.{1} X x c),
iff.refl
(@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))))
h))))
l_tl))),
simp_rw (ite_left_if h),
exact drop_chain'_of_chain' l_ih,
simp_rw (ite_right_if h),
by_cases j : l_tl = list.nil,
rw j,
simp [to_path],
rw list.chain'_iff_nth_le,
rw list.chain'_iff_nth_le at l_ih,
rw list.chain'_iff_nth_le at a,
intros i i_bounds,
rename h nodup,
by_cases i = 0,
rename h i_eq,
simp_rw i_eq,
rw [list.nth_le], rw [list.nth_le],
specialize a 0,
have a_proof : 0 < (l_hd :: l_tl).length - 1,
rw list.length_cons,
simp only [nat.add_succ_sub_one, add_zero],
exact (list.length_pos_of_ne_nil j),
specialize a a_proof,
rw [list.nth_le] at a, rw [list.nth_le] at a,
have x := (to_path_first_elem X l_tl j),
rw to_path_first_elem',
simp,
split_ifs with h',
by_cases (∀ (h_1 : ¬l_tl.nth_le 0 (list.length_pos_of_ne_nil j) = c), (⟨l_tl.nth_le 0 (list.length_pos_of_ne_nil j), h_1⟩ : {x : X // x ≠ c}) ∈ D),
exfalso,
contrapose nodup,
push_neg,
rw list.index_of_lt_length,
rw dite_left_if h',
rw list.mem_iff_nth_le,
use 0,
have len : (0 < (replace_clones c D d H l_tl).length),
apply list.length_pos_of_ne_nil,
rw ←replace_clones_ne_nil_iff,
exact j,
use len,
rw to_path_first_elem',
rw list.nth_le_map (λ (x : X), dite (∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D) (λ (h : ∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D), d) (λ (h : ¬∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D), ⟨x, replace_clones_helper c D h⟩)) _ (list.length_pos_of_ne_nil j),
simp,
rw dite_left_if h,
rw dite_right_if h,
push_neg at h,
cases h with not_c not_d,
by_cases l_hd = c,
rw h at a, simp,
have test := @margin_eq_margin_minus_candidate V X P c _inst_1 d ⟨(l_tl.nth_le 0 _), not_c⟩,
simp at test,
rw margin_eq_margin_minus_candidate,
rw test,
rw ←margin_eq_clone_non_clone P c D ⟨l_tl.nth_le 0 _, not_c⟩ d H not_d clone,
simp,
rw ←margin_eq_clone_non_clone P c D b d H H' clone,
exact a,
specialize h' h,
simp,
have test := @margin_eq_margin_minus_candidate V X P c _inst_1 d ⟨(l_tl.nth_le 0 _), not_c⟩,
simp at test,
rw margin_eq_margin_minus_candidate,
rw test,
rw ←margin_eq_clone_non_clone P c D ⟨l_tl.nth_le 0 _, not_c⟩ d H not_d clone,
rw margin_eq_clone_non_clone P c D ⟨l_tl.nth_le 0 _, not_c⟩ ⟨l_hd, h⟩ h' not_d clone,
rw ←margin_eq_clone_non_clone P c D b d H H' clone,
rw ←margin_eq_margin_minus_candidate,
simp,
exact a,
exact h,
by_cases (∀ (h_1 : ¬l_tl.nth_le 0 (list.length_pos_of_ne_nil j) = c), (⟨l_tl.nth_le 0 (list.length_pos_of_ne_nil j), h_1⟩ : {x : X // x ≠ c}) ∈ D),
rw dite_left_if h,
push_neg at h',
cases h' with not_c not_d,
rename h h'',
by_cases (l_tl.nth_le 0 (list.nth_le._main._proof_1 l_hd l_tl 0 (nat.lt_pred_iff.mp a_proof))) = c,
rw h at a,
rw margin_eq_margin_minus_candidate,
rw ←margin_eq_clone_non_clone P c D b d H H' clone,
rw margin_eq_margin_minus_candidate,
rw ←margin_eq_clone_non_clone' P c D H not_d clone,
exact a,
specialize h'' h,
rw margin_eq_margin_minus_candidate, rw margin_eq_margin_minus_candidate,
rw ←margin_eq_clone_non_clone' P c D H not_d clone,
rw margin_eq_clone_non_clone' P c D h'' not_d clone,
rw ←margin_eq_clone_non_clone P c D b d H H' clone,
rw ←margin_eq_margin_minus_candidate,
exact a,
rw dite_right_if h,
rw margin_eq_margin_minus_candidate,
rw ←margin_eq_clone_non_clone P c D b d H H' clone,
exact a,
exact h,
rw [list.nth_le],
specialize l_ih (i - 1),
rw list.length_cons at i_bounds,
simp only [nat.add_succ_sub_one, add_zero] at i_bounds,
have o : ∀ i n, ¬ i = 0 → i < n → i - 1 < n - 1 := by omega,
specialize l_ih (o i (replace_clones c D d H l_tl).length h i_bounds),
rw nth_le_cons h,
have o : ∀ i, ¬ i = 0 → i - 1 + 1 = i := by omega,
simp_rw (o i h) at l_ih,
exact l_ih,
end
-- A.5, would hold both directions. Added condition
lemma every_clone_defeats (b : {x : X // x ≠ c}) (e : b ∉ D) (d ∈ D) : clones P c D → ((split_cycle_VCCR V X P) c b ↔ (split_cycle_VCCR V X P) d b) :=
begin
intro clone,
rw split_cycle_definitions,
unfold split_cycle_VCCR',
split,
intro w,
introI f,
cases w with m w,
unfold margin_pos at m,
rw margin_eq_clone_non_clone P c D b d H e clone at m,
rw ←margin_eq_margin_minus_candidate at m,
use m,
push_neg, push_neg at w,
intro l,
specialize w (remove_clones c D l),
intro ne_nil,
specialize w ((remove_clones_ne_nil_iff c D l).mp ne_nil),
contrapose w,
push_neg, push_neg at w,
use remove_clones_nodup c D l,
cases w with l_nodup w,
cases w with first w,
cases w with last w,
have f : ∀(p : ↑b ≠ c), (⟨↑b, p⟩ : {x : X // x ≠ c}) ∉ D,
simp, intro _, exact e,
use remove_clones_first_elem c D l ↑b f ne_nil first,
unfold remove_clones,
rw to_path_last_elem,
rw list.last_map,
rw last,
simp,
have t : (¬↑d = c → d ∉ D → ↑d = c),
intros x y,
exfalso, exact y H,
use t,
exact remove_clones_chain'_of_chain' P c D l b e d H clone w,
intro w,
introI f,
cases w with m w,
unfold margin_pos at m,
rw margin_eq_margin_minus_candidate at m,
rw ←margin_eq_clone_non_clone P c D b d H e clone at m,
use m,
push_neg, push_neg at w,
intros l ne_nil,
specialize w (lift (replace_clones c D d H l)),
have n1: (replace_clones c D d H l) ≠ list.nil,
rw ←replace_clones_ne_nil_iff,
exact ne_nil,
have n : (lift (replace_clones c D d H l)) ≠ list.nil,
rw lift_ne_nil_iff,
exact n1,
specialize w n,
contrapose w,
push_neg, push_neg at w,
split,
apply nodup_lift_of_nodup,
apply replace_clones_nodup,
cases w with nd w,
cases w with first w,
cases w with last w,
split,
unfold lift, unfold has_lift.lift,
rw list.nth_le_map,
rw replace_clones_first_elem c D l b,
simp,
push_neg, use b.property, simp, exact e,
exact first,
split,
unfold lift, unfold has_lift.lift,
rw list.last_eq_nth_le,
rw list.nth_le_map,
simp_rw list.length_map,
rw ←list.last_eq_nth_le,
rw replace_clones_last_c c D l ne_nil,
exact last,
rw list.length_map,
have o : ∀ x : ℕ, 0 < x → x - 1 < x := by omega,
apply o, apply list.length_pos_of_ne_nil, exact n1,
have imp : ∀ (a b_1 : {x : X // x ≠ c}), (λ a b_1, margin P ↑d ↑b ≤ margin P (coe a) (coe b_1)) a b_1 → (λ a b_1, margin P ↑d ↑b ≤ margin P a b_1) (coe a) (coe b_1),
intros a b_1 e, exact e,
apply list.chain'_map_of_chain' coe imp,
simp,
exact A5_chain P c D H e clone w,
end
lemma remove_clones'_cycle1 [fintype V] {l : list {x : X // x ≠ c}} {a' : {x : X // x ≠ c}} (H : a' ∉ D) {d : {x : X // x ≠ c}} (H' : d ∈ D) (clone : clones P c D) : list.chain' (λ (a_1 b : {x // x ≠ c}), margin (minus_candidate P c) a' d ≤ margin (minus_candidate P c) a_1 b) l
→ list.chain' (λ (a_1 b : X), margin P ↑a' c ≤ margin P a_1 b) (remove_clones' c D l) :=
begin
intro a,
induction l,
{obviously,},
unfold remove_clones',
simp [to_path],
specialize l_ih (and.right (list.chain'_cons'.mp a)),
by_cases (list.index_of (ite (l_hd ∈ D) c ↑l_hd) (to_path (list.map (λ (x : {x // x ≠ c}), ite (x ∈ D) c ↑x) l_tl)) < (to_path (list.map (λ (x : {x // x ≠ c}), ite (x ∈ D) c ↑x) l_tl)).length),
simp_rw (ite_left_if h),
exact drop_chain'_of_chain' l_ih,
simp_rw (ite_right_if h),
by_cases j : l_tl = list.nil,
rw j,
rw list.map_nil,
rw (to_path_eq_nil_iff list.nil).mpr,
exact list.chain'_singleton (ite (l_hd ∈ D) c ↑l_hd),
refl,
rw list.chain'_iff_nth_le,
rw list.chain'_iff_nth_le at l_ih,
rw list.chain'_iff_nth_le at a,
intros i i_bounds,
have nodup := h,
by_cases i = 0,
have i_eq := h,
simp_rw h,
rw [list.nth_le], rw [list.nth_le],
specialize a 0,
have a_proof : 0 < (l_hd :: l_tl).length - 1,
rw list.length_cons,
simp only [nat.add_succ_sub_one, add_zero],
exact (list.length_pos_of_ne_nil j),
specialize a a_proof,
rw [list.nth_le] at a, rw [list.nth_le] at a,
have x := (to_path_first_elem {x : X // x ≠ c} l_tl j),
rw to_path_first_elem',
simp,
by_cases (l_hd ∈ D),
rw ite_left_if h,
have h' := h,
by_cases (l_tl.nth_le 0 _ ∈ D),
exfalso,
contrapose nodup,
push_neg,
rw list.index_of_lt_length,
rw ite_left_if h',
rw list.mem_iff_nth_le,
use 0,
have len : 0 < (to_path (list.map (λ (x : {x // x ≠ c}), ite (x ∈ D) c ↑x) l_tl)).length,
change (0 < (remove_clones' c D l_tl).length),
apply list.length_pos_of_ne_nil,
rw ←remove_clones'_ne_nil_iff,
exact j,
use len,
rw to_path_first_elem',
rw list.nth_le_map (λ (x : {x // x ≠ c}), ite (x ∈ D) c ↑x) _ (list.length_pos_of_ne_nil j),
simp,
intro w,
exfalso, exact w h,
rw ite_right_if h,
rw margin_eq_clone_non_clone' P c D H' H clone,
rw margin_eq_clone_non_clone P c D (l_tl.nth_le 0 _) l_hd h' h clone,
exact a,
rw ite_right_if h,
have h' := h,
by_cases (l_tl.nth_le 0 _ ∈ D),
rw ite_left_if h,
rw margin_eq_clone_non_clone' P c D H' H clone,
rw margin_eq_clone_non_clone' P c D h h' clone,
exact a,
rw ite_right_if h,
rw margin_eq_clone_non_clone' P c D H' H clone,
rw margin_eq_margin_minus_candidate P,
exact a,
rw [list.nth_le],
specialize l_ih (i - 1),
rw list.length_cons at i_bounds,
simp only [nat.add_succ_sub_one, add_zero] at i_bounds,
have o : ∀ i n, ¬ i = 0 → i < n → i - 1 < n - 1 := by omega,
specialize l_ih (o i (to_path (list.map (λ (x : {x // x ≠ c}), ite (x ∈ D) c ↑x) l_tl)).length h i_bounds),
--change (margin P c ↑b ≤ margin P ((((ite (∀ (h : ¬l_hd = c), (⟨l_hd, h⟩ : {x : X // x ≠ c}) ∈ D) c l_hd)) :: (remove_clones c D l_tl)).nth_le i (nat.lt_of_lt_pred i_bounds)) ((remove_clones c D l_tl).nth_le i _)),
rw nth_le_cons h,
have o : ∀ i, ¬ i = 0 → i - 1 + 1 = i := by omega,
simp_rw (o i h) at l_ih,
exact l_ih,
end
lemma remove_clones'_cycle2 [fintype V] {l : list X} {a' : {x : X // x ≠ c}} (H : a' ∉ D) {d : {x : X // x ≠ c}} (H' : d ∈ D) (clone : clones P c D) : list.chain' (λ (a_1 b : X), margin P ↑a' c ≤ margin P a_1 b) l
→ list.chain' (λ (a_1 b : {x // x ≠ c}), margin (minus_candidate P c) a' d ≤ margin (minus_candidate P c) a_1 b) (replace_clones c D d H' l) :=
begin
intro a,
induction l,
{obviously,},
unfold replace_clones,
simp [to_path],
specialize l_ih (and.right (list.chain'_cons'.mp a)),
by_cases @has_lt.lt.{0} nat nat.has_lt
(@list.index_of.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(λ (a b : @subtype.{1} X (λ (x : X), @ne.{1} X x c)),
classical.prop_decidable (@eq.{1} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)) a b))
(@dite.{1} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D)
(@forall_prop_decidable (not (@eq.{1} X l_hd c))
(λ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D)
(@ne.decidable.{1} X (λ (a b : X), classical.prop_decidable (@eq.{1} X a b)) l_hd c)
(λ (h : not (@eq.{1} X l_hd c)),
@set.decidable_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)) D
(λ (a : @subtype.{1} X (λ (x : X), @ne.{1} X x c)), classical.prop_decidable (D a))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))))
(λ
(h :
∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D), d)
(λ
(h :
not
(∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D)),
@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@replace_clones_helper X c D l_hd
(@iff.mpr
(not
(∀ (p : @ne.{1} X l_hd c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd p)
D))
(not
(∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D))
(@not_iff_not_of_iff
(∀ (p : @ne.{1} X l_hd c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd p)
D)
(∀ (h : not (@eq.{1} X l_hd c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd
(@iff.mpr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))
h))
D)
(@forall_prop_congr (@ne.{1} X l_hd c) (not (@eq.{1} X l_hd c))
(λ (h : @ne.{1} X l_hd c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd h)
D)
(λ (h : @ne.{1} X l_hd c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd h)
D)
(λ (h : @ne.{1} X l_hd c),
iff.refl
(@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) l_hd h)
D))
(@eq.rec.{0 1} Prop (@ne.{1} X l_hd c) (λ (A : Prop), iff (@ne.{1} X l_hd c) A)
(iff.refl (@ne.{1} X l_hd c))
(not (@eq.{1} X l_hd c))
(@ne.def.{1} X l_hd c))))
h))))
(@to_path (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(@list.map.{0 0} X (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(λ (x : X),
@dite.{1} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@forall_prop_decidable (not (@eq.{1} X x c))
(λ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@ne.decidable.{1} X (λ (a b : X), classical.prop_decidable (@eq.{1} X a b)) x c)
(λ (h : not (@eq.{1} X x c)),
@set.decidable_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)) D
(λ (a : @subtype.{1} X (λ (x : X), @ne.{1} X x c)), classical.prop_decidable (D a))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))))
(λ
(h :
∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D), d)
(λ
(h :
not
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)),
@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@replace_clones_helper X c D x
(@iff.mpr
(not
(∀ (p : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x p)
D))
(not
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D))
(@not_iff_not_of_iff
(∀ (p : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x p)
D)
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@forall_prop_congr (@ne.{1} X x c) (not (@eq.{1} X x c))
(λ (h : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D)
(λ (h : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D)
(λ (h : @ne.{1} X x c),
iff.refl
(@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))))
h))))
l_tl)))
(@list.length.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(@to_path (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(@list.map.{0 0} X (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(λ (x : X),
@dite.{1} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@forall_prop_decidable (not (@eq.{1} X x c))
(λ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@ne.decidable.{1} X (λ (a b : X), classical.prop_decidable (@eq.{1} X a b)) x c)
(λ (h : not (@eq.{1} X x c)),
@set.decidable_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)) D
(λ (a : @subtype.{1} X (λ (x : X), @ne.{1} X x c)), classical.prop_decidable (D a))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))))
(λ
(h :
∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D), d)
(λ
(h :
not
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)),
@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@replace_clones_helper X c D x
(@iff.mpr
(not
(∀ (p : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x p)
D))
(not
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D))
(@not_iff_not_of_iff
(∀ (p : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x p)
D)
(∀ (h : not (@eq.{1} X x c)),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x
(@iff.mpr (@ne.{1} X x c) (not (@eq.{1} X x c))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))
h))
D)
(@forall_prop_congr (@ne.{1} X x c) (not (@eq.{1} X x c))
(λ (h : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D)
(λ (h : @ne.{1} X x c),
@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D)
(λ (h : @ne.{1} X x c),
iff.refl
(@has_mem.mem.{0 0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c))
(set.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@set.has_mem.{0} (@subtype.{1} X (λ (x : X), @ne.{1} X x c)))
(@subtype.mk.{1} X (λ (x : X), @ne.{1} X x c) x h)
D))
(@eq.rec.{0 1} Prop (@ne.{1} X x c) (λ (A : Prop), iff (@ne.{1} X x c) A)
(iff.refl (@ne.{1} X x c))
(not (@eq.{1} X x c))
(@ne.def.{1} X x c))))
h))))
l_tl))),
simp_rw (ite_left_if h),
exact drop_chain'_of_chain' l_ih,
simp_rw (ite_right_if h),
by_cases j : l_tl = list.nil,
rw j,
simp [to_path],
rw list.chain'_iff_nth_le,
rw list.chain'_iff_nth_le at l_ih,
rw list.chain'_iff_nth_le at a,
intros i i_bounds,
rename h nodup,
by_cases i = 0,
rename h i_eq,
simp_rw i_eq,
rw [list.nth_le], rw [list.nth_le],
specialize a 0,
have a_proof : 0 < (l_hd :: l_tl).length - 1,
rw list.length_cons,
simp only [nat.add_succ_sub_one, add_zero],
exact (list.length_pos_of_ne_nil j),
specialize a a_proof,
rw [list.nth_le] at a, rw [list.nth_le] at a,
have x := (to_path_first_elem X l_tl j),
rw to_path_first_elem',
simp,
split_ifs with h',
by_cases (∀ (h_1 : ¬l_tl.nth_le 0 (list.length_pos_of_ne_nil j) = c), (⟨l_tl.nth_le 0 (list.length_pos_of_ne_nil j), h_1⟩ : {x : X // x ≠ c}) ∈ D),
exfalso,
contrapose nodup,
push_neg,
rw list.index_of_lt_length,
rw dite_left_if h',
rw list.mem_iff_nth_le,
use 0,
have len : (0 < (replace_clones c D d H' l_tl).length),
apply list.length_pos_of_ne_nil,
rw ←replace_clones_ne_nil_iff,
exact j,
use len,
rw to_path_first_elem',
rw list.nth_le_map (λ (x : X), dite (∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D) (λ (h : ∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D), d) (λ (h : ¬∀ (h : ¬x = c), (⟨x, h⟩ : {x : X // x ≠ c}) ∈ D), ⟨x, replace_clones_helper c D h⟩)) _ (list.length_pos_of_ne_nil j),
simp,
rw dite_left_if h,
rw dite_right_if h,
push_neg at h,
cases h with not_c not_d,
by_cases l_hd = c,
rw h at a,
rw ←margin_eq_clone_non_clone' P c D H' H clone,
rw ←margin_eq_clone_non_clone P c D ⟨l_tl.nth_le 0 _, not_c⟩ d H' not_d clone,
exact a,
specialize h' h,
rw ←margin_eq_clone_non_clone' P c D H' H clone,
rw ←margin_eq_clone_non_clone P c D ⟨l_tl.nth_le 0 _, not_c⟩ d H' not_d clone,
rw margin_eq_clone_non_clone P c D ⟨l_tl.nth_le 0 _, not_c⟩ ⟨l_hd, h⟩ h' not_d clone,
rw ←margin_eq_margin_minus_candidate,
exact a,
exact h,
by_cases (∀ (h_1 : ¬l_tl.nth_le 0 (list.length_pos_of_ne_nil j) = c), (⟨l_tl.nth_le 0 (list.length_pos_of_ne_nil j), h_1⟩ : {x : X // x ≠ c}) ∈ D),
rw dite_left_if h,
push_neg at h',
cases h' with not_c not_d,
rename h h'',
by_cases (l_tl.nth_le 0 (list.nth_le._main._proof_1 l_hd l_tl 0 (nat.lt_pred_iff.mp a_proof))) = c,
rw h at a,
rw ←margin_eq_clone_non_clone' P c D H' H clone,
rw ←margin_eq_clone_non_clone' P c D H' not_d clone,
exact a,
specialize h'' h,
rw ←margin_eq_clone_non_clone' P c D H' H clone,
rw ←margin_eq_clone_non_clone' P c D H' not_d clone,
rw margin_eq_clone_non_clone' P c D h'' not_d clone,
rw ←margin_eq_margin_minus_candidate,
exact a,
rw dite_right_if h,
rw ←margin_eq_clone_non_clone' P c D H' H clone,
rw ←margin_eq_margin_minus_candidate,
exact a,
exact h,
rw [list.nth_le],
specialize l_ih (i - 1),
rw list.length_cons at i_bounds,
simp only [nat.add_succ_sub_one, add_zero] at i_bounds,
have o : ∀ i n, ¬ i = 0 → i < n → i - 1 < n - 1 := by omega,
specialize l_ih (o i (replace_clones c D d H' l_tl).length h i_bounds),
rw nth_le_cons h,
have o : ∀ i, ¬ i = 0 → i - 1 + 1 = i := by omega,
simp_rw (o i h) at l_ih,
exact l_ih,
end
lemma every_clone_defeated' (a : {x : X // x ≠ c}) (e : a ∉ D) (d ∈ D) : clones P c D → ((split_cycle_VCCR V X P) a c ↔ (split_cycle_VCCR V {x : X // x ≠ c} (minus_candidate P c)) a d) :=
begin
intro clone,
rw split_cycle_definitions,
unfold split_cycle_VCCR',
split,
intro w,
introI f,
cases w with m w,
unfold margin_pos at m,
rw margin_eq_clone_non_clone' P c D H e clone at m,
rw ←margin_eq_margin_minus_candidate at m,
use m,
push_neg, push_neg at w,
intro l,
specialize w (remove_clones' c D l),
intro ne_nil,
specialize w ((remove_clones'_ne_nil_iff c D l).mp ne_nil),
contrapose w,
push_neg, push_neg at w,
use remove_clones'_nodup c D l,
cases w with l_nodup w,
cases w with first w,
cases w with last w,
split,
unfold remove_clones',
rw to_path_first_elem',
rw list.nth_le_map,
simp_rw first,
split_ifs,
refl,
exact list.length_pos_of_ne_nil ne_nil,
use remove_clones'_last_elem c D l a e ne_nil last,
exact remove_clones'_cycle1 P c D e H clone w,
intro w,
introI f,
cases w with m w,
unfold margin_pos at m,
rw ←margin_eq_clone_non_clone' P c D H e clone at m,
use m,
push_neg, push_neg at w,
intros l ne_nil,
specialize w (replace_clones c D d H l),
have n: (replace_clones c D d H l) ≠ list.nil,
rw ←replace_clones_ne_nil_iff,
exact ne_nil,
specialize w n,
contrapose w,
push_neg, push_neg at w,
split,
apply replace_clones_nodup,
cases w with nd w,
cases w with first w,
cases w with last w,
split,
unfold replace_clones,
rw to_path_first_elem,
rw list.nth_le_map,
split_ifs,
refl,
push_neg at h,
cases h with contr h,
exfalso,
exact contr first,
simp,
exact ne_nil,
rw replace_clones_last_elem c D l a,
simp,
exact remove_clones'_cycle2 P c D e H clone w,
push_neg,
use a.property,
simp, exact e,
exact last,
end
--set_option pp.all true
theorem non_clone_choice_ind_clones_split_cyle [fintype V] : non_clone_choice_ind_clones P c D split_cycle :=
begin
unfold non_clone_choice_ind_clones,
intro clone,
intro a, intro not_d,
unfold split_cycle,
unfold max_el_VSCC,
simp,
split,
intro d,
contrapose d,
push_neg at d, push_neg,
cases d with b d,
cases d with b_c d,
use b,
have test := (clone_maintains_defeat P c D ⟨b, b_c⟩ a not_d clone).mp,
simp at test,
apply test,
introI _inst_1,
exact d,
intro d,
contrapose d,
push_neg at d, push_neg,
cases d with b d,
by_cases b = c,
have clone' := clone,
unfold clones at clone',
cases clone' with n clone',
use n.some,
use n.some.property,
simp,
apply (clone_maintains_defeat P c D n.some a not_d clone).mpr,
have test := every_clone_defeats P c D a not_d n.some n.some_spec clone,
apply test.mp,
rw h at d,
introI _inst_1,
exact d,
use b,
use h,
have test := (clone_maintains_defeat P c D ⟨b, h⟩ a not_d clone).mpr,
simp at test,
apply test,
introI _inst_1,
exact d,
end
theorem clone_choice_ind_clones_split_cyle [fintype V] [fintype X] : clone_choice_ind_clones P c D split_cycle :=
begin
unfold clone_choice_ind_clones,
intro clone,
split,
intro a,
cases a with a1 a2,
have spec : ∀ h : c ≠ c, (⟨c, h⟩ : {x // x ≠ c}) ∈ D,
{intro contr, exfalso, exact contr (eq.refl c), },
by_cases ∀ c' : {x // ∀ h : x ≠ c, (⟨x, h⟩ : {x // x ≠ c}) ∈ D}, ∃ d : {x // ∀ h : x ≠ c, (⟨x, h⟩ : {x // x ≠ c}) ∈ D}, split_cycle_VCCR V X P d c',
have clone' := clone,
cases clone' with n clone,
let f : {x // ∀ h : x ≠ c, (⟨x, h⟩ : {x // x ≠ c}) ∈ D} → {x // ∀ h : x ≠ c, (⟨x, h⟩ : {x // x ≠ c}) ∈ D} := λ x, (h x).some,
let seq := stream.iterate f ⟨c, spec⟩,
have property : ∀ x : ℕ, split_cycle_VCCR V X P (seq.nth x.succ) (seq.nth x),
{intro i,
change split_cycle_VCCR V X P ((h (seq.nth i)).some) (seq.nth i),
have test := (h (seq.nth i)).some_spec,
unfold split_cycle_VCCR, intro f,
unfold margin_pos,
simp_rw margin_eq_margin f _inst_1,
exact test,},
let seq2 : stream X := λ (x : ℕ), (seq.nth x).val,
exfalso,
exact false_of_sequence_acyclic_vccr (split_cycle_VCCR_acyclic P) seq2 property,
push_neg at h,
cases h with d h,
unfold split_cycle at a1,
unfold max_el_VSCC at a1,
unfold split_cycle at a2, unfold max_el_VSCC at a2,
simp at a2,
simp at a1,
have some_a : ∃ a, (∃ h: a ≠ c, (⟨a, h⟩ : {x // x ≠ c}) ∉ D) ∧ split_cycle_VCCR V X P a d,
{by_cases h': ↑d = c, cases a1 with x a1, use x,
split, contrapose h, push_neg, simp at h,
use ⟨x, h⟩, rw h', simp, intro f,
unfold margin_pos, simp_rw margin_eq_margin f _inst_1,
exact a1, rw h', intro f,
unfold margin_pos, simp_rw margin_eq_margin f _inst_1,
exact a1,
specialize a2 d, specialize a2 h', specialize a2 (d.property h'),
cases a2 with a a2,
use a,
split, contrapose h, simp, simp at h,
use a, use h, intro f,
unfold margin_pos, simp_rw margin_eq_margin f _inst_1,
exact a2, intro f,
unfold margin_pos, simp_rw margin_eq_margin f _inst_1,
exact a2,
},
intros e e_D,
unfold split_cycle, unfold max_el_VSCC, simp,
cases some_a with a some_a,
use a,
cases some_a with some_a defeats,
cases some_a with not_c some_a,
use not_c,
by_cases ↑d = c,
rw ←every_clone_defeated' P c D ⟨a, not_c⟩ some_a e e_D clone,
simp_rw ←h, intro f,
unfold margin_pos,
simp_rw margin_eq_margin f _inst_1,
exact defeats,
rw ←every_clone_defeated' P c D ⟨a, not_c⟩ some_a e e_D clone,
rw every_clone_defeated' P c D ⟨a, not_c⟩ some_a ⟨↑d, h⟩ (d.property h) clone,
rw clone_maintains_defeat' P c D ⟨a, not_c⟩ ⟨↑d, h⟩ some_a (d.property h) clone,
intro f,
unfold margin_pos,
simp_rw margin_eq_margin f _inst_1,
exact defeats,
intro a,
by_cases ∀ c' : {x // x ∈ D}, ∃ d : {x // x ∈ D}, split_cycle_VCCR V {x : X // x ≠ c} (minus_candidate P c) d c',
have clone' := clone,
cases clone' with n clone,
let f : {x // x ∈ D} → {x // x ∈ D} := λ x, (h x).some,
let seq := stream.iterate f ⟨n.some, n.some_spec⟩,
have property : ∀ x : ℕ, split_cycle_VCCR V {x : X // x ≠ c} (minus_candidate P c) (seq.nth x.succ) (seq.nth x),
{intro i,
change split_cycle_VCCR V {x : X // x ≠ c} (minus_candidate P c) ((h (seq.nth i)).some) (seq.nth i),
have test := (h (seq.nth i)).some_spec,
unfold split_cycle_VCCR, intro f,
unfold margin_pos,
simp_rw margin_eq_margin f _inst_1,
exact test,},
let seq2 : stream {x : X // x ≠ c} := λ (x : ℕ), (seq.nth x).val,
exfalso,
exact false_of_sequence_acyclic_vccr (split_cycle_VCCR_acyclic (minus_candidate P c)) seq2 property,
push_neg at h,
cases h with d h,
unfold split_cycle at a,
unfold max_el_VSCC at a,
simp at a,
have some_a : ∃ a ∉ D, split_cycle_VCCR V {x : X // x ≠ c} (minus_candidate P c) a d,
{
specialize a d, specialize a d.val.property, simp at a,
cases a with a' a,
use a', cases a with not_c a,
exact not_c,
split, contrapose h, simp, simp at h,
use a', cases a with not_c a, use not_c, use h, intro f,
unfold margin_pos, simp_rw margin_eq_margin f _inst_1,
exact a, intro f,
unfold margin_pos, simp_rw margin_eq_margin f _inst_1,
cases a with not_c a,
exact a,
},
unfold split_cycle, unfold max_el_VSCC, simp,
split,
cases some_a with a some_a,
cases some_a with H some_a,
use ↑a,
rw every_clone_defeated' P c D a H d.val d.property clone,
intro f, unfold margin_pos, simp_rw margin_eq_margin f _inst_1,
exact some_a,
intros a not_c in_d,
cases some_a with a' some_a,
use a',
cases some_a with not_d defeats,
have test := clone_maintains_defeat' P c D a' ⟨a, not_c⟩ not_d in_d clone,
simp at test,
rw ←test,
rw ←every_clone_defeated' P c D a' not_d ⟨a, not_c⟩ in_d clone,
rw every_clone_defeated' P c D a' not_d d.val d.property clone,
intro f, unfold margin_pos, simp_rw margin_eq_margin f _inst_1,
exact defeats,
end |
50ccca20c3fc98eed7593923f79a489899838d35 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/topology/homotopy/H_spaces.lean | be4fc947d19147a6f2943456ff89888795142668 | [
"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,823 | lean | /-
Copyright (c) 2022 Filippo A. E. Nuccio Mortarino Majno di Capriglio. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Filippo A. E. Nuccio, Junyan Xu
-/
import topology.compact_open
import topology.homotopy.path
/-!
# H-spaces
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines H-spaces mainly following the approach proposed by Serre in his paper
*Homologie singulière des espaces fibrés*. The idea beaneath `H-spaces` is that they are topological
spaces with a binary operation `⋀ : X → X → X` that is a homotopic-theoretic weakening of an
operation what would make `X` into a topological monoid. In particular, there exists a "neutral
element" `e : X` such that `λ x, e ⋀ x` and `λ x, x ⋀ e` are homotopic to the identity on `X`, see
[the Wikipedia page of H-spaces](https://en.wikipedia.org/wiki/H-space).
Some notable properties of `H-spaces` are
* Their fundamental group is always abelian (by the same argument for topological groups);
* Their cohomology ring comes equipped with a structure of a Hopf-algebra;
* The loop space based at every `x : X` carries a structure of an `H-spaces`.
## Main Results
* Every topological group `G` is an `H-space` using its operation `* : G → G → G` (this is already
true if `G` has an instance of a `mul_one_class` and `continuous_mul`);
* Given two `H-spaces` `X` and `Y`, their product is again an `H`-space. We show in an example that
starting with two topological groups `G, G'`, the `H`-space structure on `G × G'` is definitionally
equal to the product of `H-space` structures on `G` and `G'`.
* The loop space based at every `x : X` carries a structure of an `H-spaces`.
## To Do
* Prove that for every `normed_add_torsor Z` and every `z : Z`, the operation
`λ x y, midpoint x y` defines a `H-space` structure with `z` as a "neutral element".
* Prove that `S^0`, `S^1`, `S^3` and `S^7` are the unique spheres that are `H-spaces`, where the
first three inherit the structure because they are topological groups (they are Lie groups,
actually), isomorphic to the invertible elements in `ℤ`, in `ℂ` and in the quaternion; and the
fourth from the fact that `S^7` coincides with the octonions of norm 1 (it is not a group, in
particular, only has an instance of `mul_one_class`).
## References
* [J.-P. Serre, *Homologie singulière des espaces fibrés. Applications*,
Ann. of Math (2) 1951, 54, 425–505][serre1951]
-/
universes u v
noncomputable theory
open_locale unit_interval
open path continuous_map set.Icc topological_space
/--
A topological space `X` is an H-space if it behaves like a (potentially non-associative)
topological group, but where the axioms for a group only hold up to homotopy.
-/
class H_space (X : Type u) [topological_space X] :=
(Hmul : C(X × X, X))
(e : X)
(Hmul_e_e : Hmul (e, e) = e)
(e_Hmul : (Hmul.comp $ (const X e).prod_mk $ continuous_map.id X).homotopy_rel
(continuous_map.id X) {e})
(Hmul_e : (Hmul.comp $ (continuous_map.id X).prod_mk $ const X e).homotopy_rel
(continuous_map.id X) {e})
/- We use the notation `⋀`, typeset as \And, to denote the binary operation `Hmul` on a H-space -/
localized "notation (name := H_space.Hmul) x `⋀` y := H_space.Hmul (x, y) " in H_spaces
instance H_space.prod (X : Type u) (Y : Type v) [topological_space X] [topological_space Y]
[H_space X] [H_space Y] : H_space (X × Y) :=
{ Hmul := ⟨λ p, ((p.1.1 ⋀ p.2.1), p.1.2 ⋀ p.2.2), by continuity⟩,
e := (H_space.e, H_space.e),
Hmul_e_e := by {simp only [continuous_map.coe_mk, prod.mk.inj_iff],
exact ⟨H_space.Hmul_e_e, H_space.Hmul_e_e⟩},
e_Hmul :=
begin
let G : I × (X × Y) → X × Y :=
(λ p, (H_space.e_Hmul (p.1, p.2.1), H_space.e_Hmul (p.1, p.2.2))),
have hG : continuous G := (continuous.comp H_space.e_Hmul.1.1.2 (continuous_fst.prod_mk
(continuous_fst.comp continuous_snd))).prod_mk (continuous.comp H_space.e_Hmul.1.1.2
(continuous_fst.prod_mk (continuous_snd.comp continuous_snd))),
use ⟨G, hG⟩,
{ rintros ⟨x, y⟩,
exacts prod.mk.inj_iff.mpr ⟨(H_space.e_Hmul).1.2 x, (H_space.e_Hmul).1.2 y⟩ },
{ rintros ⟨x, y⟩,
exact prod.mk.inj_iff.mpr ⟨(H_space.e_Hmul).1.3 x, (H_space.e_Hmul).1.3 y⟩ },
{ rintros t ⟨x, y⟩ h,
replace h := prod.mk.inj_iff.mp (set.mem_singleton_iff.mp h),
exact ⟨prod.mk.inj_iff.mpr ⟨homotopy_rel.eq_fst (H_space.e_Hmul) t
(set.mem_singleton_iff.mpr h.1),
homotopy_rel.eq_fst (H_space.e_Hmul) t (set.mem_singleton_iff.mpr h.2)⟩,
prod.mk.inj_iff.mpr ⟨((H_space.e_Hmul).2 t x h.1).2, ((H_space.e_Hmul).2 t y h.2).2⟩⟩ },
end,
Hmul_e :=
begin
let G : I × (X × Y) → X × Y :=
(λ p, (H_space.Hmul_e (p.1, p.2.1), H_space.Hmul_e (p.1, p.2.2))),
have hG : continuous G := (continuous.comp H_space.Hmul_e.1.1.2 (continuous_fst.prod_mk
(continuous_fst.comp continuous_snd))).prod_mk (continuous.comp H_space.Hmul_e.1.1.2
(continuous_fst.prod_mk (continuous_snd.comp continuous_snd))),
use ⟨G, hG⟩,
{ rintros ⟨x, y⟩,
exacts prod.mk.inj_iff.mpr ⟨(H_space.Hmul_e).1.2 x, (H_space.Hmul_e).1.2 y⟩ },
{ rintros ⟨x, y⟩,
exact prod.mk.inj_iff.mpr ⟨(H_space.Hmul_e).1.3 x, (H_space.Hmul_e).1.3 y⟩ },
{ rintros t ⟨x, y⟩ h,
replace h := prod.mk.inj_iff.mp (set.mem_singleton_iff.mp h),
exact ⟨prod.mk.inj_iff.mpr ⟨homotopy_rel.eq_fst (H_space.Hmul_e) t
(set.mem_singleton_iff.mpr h.1), homotopy_rel.eq_fst (H_space.Hmul_e) t
(set.mem_singleton_iff.mpr h.2)⟩, prod.mk.inj_iff.mpr ⟨((H_space.Hmul_e).2 t x h.1).2,
((H_space.Hmul_e).2 t y h.2).2⟩⟩ },
end, }
namespace topological_group
/-- The definition `to_H_space` is not an instance because its `@additive` version would
lead to a diamond since a topological field would inherit two `H_space` structures, one from the
`mul_one_class` and one from the `add_zero_class`. In the case of a group, we make
`topological_group.H_space` an instance."-/
@[to_additive "The definition `to_H_space` is not an instance because it comes together with a
multiplicative version which would lead to a diamond since a topological field would inherit two
`H_space` structures, one from the `mul_one_class` and one from the `add_zero_class`. In the case
of an additive group, we make `topological_group.H_space` an instance."]
definition to_H_space (M : Type u) [mul_one_class M] [topological_space M]
[has_continuous_mul M] : H_space M :=
{ Hmul := ⟨function.uncurry has_mul.mul, continuous_mul⟩,
e := 1,
Hmul_e_e := one_mul 1,
e_Hmul := (homotopy_rel.refl _ _).cast rfl (by {ext1, apply one_mul}),
Hmul_e := (homotopy_rel.refl _ _).cast rfl (by {ext1, apply mul_one}) }
@[priority 600, to_additive] instance H_space (G : Type u)
[topological_space G] [group G] [topological_group G] : H_space G := to_H_space G
lemma one_eq_H_space_e {G : Type u} [topological_space G] [group G] [topological_group G] :
(1 : G) = H_space.e := rfl
/- In the following example we see that the `H-space` structure on the product of two topological
groups is definitionally equally to the product `H-space`-structure of the two groups.-/
example {G G' : Type u} [topological_space G] [group G] [topological_group G]
[topological_space G'] [group G'] [topological_group G'] :
to_H_space (G × G') = H_space.prod G G' := rfl
end topological_group
namespace unit_interval
/-- `Q_right` is analogous to the function `Q` defined on p. 475 of [serre1951] that helps proving
continuity of `delay_refl_right`.-/
def Q_right (p : I × I) : I := set.proj_Icc 0 1 zero_le_one (2 * p.1 / (1 + p.2))
lemma continuous_Q_right : continuous Q_right :=
continuous_proj_Icc.comp $ continuous.div (by continuity) (by continuity)
(λ x, (add_pos zero_lt_one).ne')
lemma Q_right_zero_left (θ : I) : Q_right (0, θ) = 0 :=
set.proj_Icc_of_le_left _ $ by simp only [coe_zero, mul_zero, zero_div]
lemma Q_right_one_left (θ : I) : Q_right (1, θ) = 1 :=
set.proj_Icc_of_right_le _ $ (le_div_iff $ add_pos zero_lt_one).2 $
by { dsimp only, rw [coe_one, one_mul, mul_one], apply add_le_add_left (le_one _) }
lemma Q_right_zero_right (t : I) : (Q_right (t, 0) : ℝ) = if (t : ℝ) ≤ 1 / 2 then 2 * t else 1 :=
begin
simp only [Q_right, coe_zero, add_zero, div_one],
split_ifs,
{ rw set.proj_Icc_of_mem _ ((mul_pos_mem_iff zero_lt_two).2 _), exacts [rfl, ⟨t.2.1, h⟩] },
{ rw (set.proj_Icc_eq_right _).2, { refl }, { linarith }, { exact zero_lt_one } },
end
lemma Q_right_one_right (t : I) : Q_right (t, 1) = t :=
eq.trans (by {rw Q_right, congr, apply mul_div_cancel_left, exact two_ne_zero}) $
set.proj_Icc_coe zero_le_one _
end unit_interval
namespace path
open unit_interval
variables {X : Type u} [topological_space X] {x y : X}
/-- This is the function analogous to the one on p. 475 of [serre1951], defining a homotopy from
the product path `γ ∧ e` to `γ`.-/
def delay_refl_right (θ : I) (γ : path x y) : path x y :=
{ to_fun := λ t, γ (Q_right (t, θ)),
continuous_to_fun := γ.continuous.comp (continuous_Q_right.comp $ continuous.prod.mk_left θ),
source' := by { dsimp only, rw [Q_right_zero_left, γ.source] },
target' := by { dsimp only, rw [Q_right_one_left, γ.target] } }
lemma continuous_delay_refl_right : continuous (λ p : I × path x y, delay_refl_right p.1 p.2) :=
continuous_uncurry_iff.mp $ (continuous_snd.comp continuous_fst).path_eval $
continuous_Q_right.comp $ continuous_snd.prod_mk $ continuous_fst.comp continuous_fst
lemma delay_refl_right_zero (γ : path x y) : delay_refl_right 0 γ = γ.trans (path.refl y) :=
begin
ext t,
simp only [delay_refl_right,
trans_apply, refl_extend, path.coe_mk, function.comp_app, refl_apply],
split_ifs, swap, conv_rhs { rw ← γ.target },
all_goals { apply congr_arg γ, ext1, rw Q_right_zero_right },
exacts [if_neg h, if_pos h],
end
lemma delay_refl_right_one (γ : path x y) : delay_refl_right 1 γ = γ :=
by { ext t, exact congr_arg γ (Q_right_one_right t) }
/-- This is the function on p. 475 of [serre1951], defining a homotopy from a path `γ` to the
product path `e ∧ γ`.-/
def delay_refl_left (θ : I) (γ : path x y) : path x y := (delay_refl_right θ γ.symm).symm
lemma continuous_delay_refl_left : continuous (λ p : I × path x y, delay_refl_left p.1 p.2) :=
path.continuous_symm.comp $ continuous_delay_refl_right.comp $ continuous_fst.prod_mk $
path.continuous_symm.comp continuous_snd
lemma delay_refl_left_zero (γ : path x y) : delay_refl_left 0 γ = (path.refl x).trans γ :=
by simp only [delay_refl_left, delay_refl_right_zero, trans_symm, refl_symm, path.symm_symm]
lemma delay_refl_left_one (γ : path x y) : delay_refl_left 1 γ = γ :=
by simp only [delay_refl_left, delay_refl_right_one, path.symm_symm]
/--
The loop space at x carries a structure of a `H-space`. Note that the field `e_Hmul`
(resp. `Hmul_e`) neither implies nor is implied by `path.homotopy.refl_trans`
(resp. `path.homotopy.trans_refl`).
-/
instance (x : X) : H_space (path x x) :=
{ Hmul := ⟨λ ρ, ρ.1.trans ρ.2, continuous_trans⟩,
e := refl x,
Hmul_e_e := refl_trans_refl,
e_Hmul :=
{ to_homotopy := ⟨⟨λ p : I × (path x x), delay_refl_left p.1 p.2,
continuous_delay_refl_left⟩, delay_refl_left_zero, delay_refl_left_one⟩,
prop' := by { rintro t _ (rfl : _ = _), exact ⟨refl_trans_refl.symm, rfl⟩ } },
Hmul_e :=
{ to_homotopy := ⟨⟨λ p : I × (path x x), delay_refl_right p.1 p.2,
continuous_delay_refl_right⟩, delay_refl_right_zero, delay_refl_right_one⟩,
prop' := by { rintro t _ (rfl : _ = _), exact ⟨refl_trans_refl.symm, rfl⟩ } } }
end path
|
fed456e783424413b52f422e2fea788d3c8716dc | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/ring_theory/derivation.lean | ae9d3f93e190b69d02443d30e06ae4836ca59e2d | [
"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 | 47,851 | lean | /-
Copyright © 2020 Nicolò Cavalleri. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nicolò Cavalleri, Andrew Yang
-/
import ring_theory.adjoin.basic
import algebra.lie.of_associative
import ring_theory.ideal.cotangent
import ring_theory.is_tensor_product
/-!
# Derivations
This file defines derivation. A derivation `D` from the `R`-algebra `A` to the `A`-module `M` is an
`R`-linear map that satisfy the Leibniz rule `D (a * b) = a * D b + D a * b`.
## Main results
- `derivation`: The type of `R`-derivations from `A` to `M`. This has an `A`-module structure.
- `derivation.llcomp`: We may compose linear maps and derivations to obtain a derivation,
and the composition is bilinear.
- `derivation.lie_algebra`: The `R`-derivations from `A` to `A` form an lie algebra over `R`.
- `derivation_to_square_zero_equiv_lift`: The `R`-derivations from `A` into a square-zero ideal `I`
of `B` corresponds to the lifts `A →ₐ[R] B` of the map `A →ₐ[R] B ⧸ I`.
- `kaehler_differential`: The module of kaehler differentials. For an `R`-algebra `S`, we provide
the notation `Ω[S⁄R]` for `kaehler_differential R S`.
Note that the slash is `\textfractionsolidus`.
- `kaehler_differential.D`: The derivation into the module of kaehler differentials.
- `kaehler_differential.span_range_derivation`: The image of `D` spans `Ω[S⁄R]` as an `S`-module.
- `kaehler_differential.linear_map_equiv_derivation`:
The isomorphism `Hom_R(Ω[S⁄R], M) ≃ₗ[S] Der_R(S, M)`.
- `kaehler_differential.quot_ker_total_equiv`: An alternative description of `Ω[S⁄R]` as `S` copies
of `S` with kernel (`kaehler_differential.ker_total`) generated by the relations:
1. `dx + dy = d(x + y)`
2. `x dy + y dx = d(x * y)`
3. `dr = 0` for `r ∈ R`
- `kaehler_differential.map`: Given a map between the arrows `R → A` and `S → B`, we have an
`A`-linear map `Ω[A⁄R] → Ω[B⁄S]`.
## Future project
- Generalize derivations into bimodules.
- Define a `is_kaehler_differential` predicate.
-/
open algebra
open_locale big_operators
/-- `D : derivation R A M` is an `R`-linear map from `A` to `M` that satisfies the `leibniz`
equality. We also require that `D 1 = 0`. See `derivation.mk'` for a constructor that deduces this
assumption from the Leibniz rule when `M` is cancellative.
TODO: update this when bimodules are defined. -/
@[protect_proj]
structure derivation (R : Type*) (A : Type*) [comm_semiring R] [comm_semiring A]
[algebra R A] (M : Type*) [add_comm_monoid M] [module A M] [module R M]
extends A →ₗ[R] M :=
(map_one_eq_zero' : to_linear_map 1 = 0)
(leibniz' (a b : A) : to_linear_map (a * b) = a • to_linear_map b + b • to_linear_map a)
/-- The `linear_map` underlying a `derivation`. -/
add_decl_doc derivation.to_linear_map
namespace derivation
section
variables {R : Type*} [comm_semiring R]
variables {A : Type*} [comm_semiring A] [algebra R A]
variables {M : Type*} [add_comm_monoid M] [module A M] [module R M]
variables (D : derivation R A M) {D1 D2 : derivation R A M} (r : R) (a b : A)
instance : add_monoid_hom_class (derivation R A M) A M :=
{ coe := λ D, D.to_fun,
coe_injective' := λ D1 D2 h, by { cases D1, cases D2, congr, exact fun_like.coe_injective h },
map_add := λ D, D.to_linear_map.map_add',
map_zero := λ D, D.to_linear_map.map_zero }
/-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun`
directly. -/
instance : has_coe_to_fun (derivation R A M) (λ _, A → M) := ⟨λ D, D.to_linear_map.to_fun⟩
-- Not a simp lemma because it can be proved via `coe_fn_coe` + `to_linear_map_eq_coe`
lemma to_fun_eq_coe : D.to_fun = ⇑D := rfl
instance has_coe_to_linear_map : has_coe (derivation R A M) (A →ₗ[R] M) :=
⟨λ D, D.to_linear_map⟩
@[simp] lemma to_linear_map_eq_coe : D.to_linear_map = D := rfl
@[simp] lemma mk_coe (f : A →ₗ[R] M) (h₁ h₂) :
((⟨f, h₁, h₂⟩ : derivation R A M) : A → M) = f := rfl
@[simp, norm_cast]
lemma coe_fn_coe (f : derivation R A M) : ⇑(f : A →ₗ[R] M) = f := rfl
lemma coe_injective : @function.injective (derivation R A M) (A → M) coe_fn :=
fun_like.coe_injective
@[ext] theorem ext (H : ∀ a, D1 a = D2 a) : D1 = D2 :=
fun_like.ext _ _ H
lemma congr_fun (h : D1 = D2) (a : A) : D1 a = D2 a := fun_like.congr_fun h a
protected lemma map_add : D (a + b) = D a + D b := map_add D a b
protected lemma map_zero : D 0 = 0 := map_zero D
@[simp] lemma map_smul : D (r • a) = r • D a := D.to_linear_map.map_smul r a
@[simp] lemma leibniz : D (a * b) = a • D b + b • D a := D.leibniz' _ _
lemma map_sum {ι : Type*} (s : finset ι) (f : ι → A) : D (∑ i in s, f i) = ∑ i in s, D (f i) :=
D.to_linear_map.map_sum
@[simp, priority 900] lemma map_smul_of_tower {S : Type*} [has_smul S A] [has_smul S M]
[linear_map.compatible_smul A M S R] (D : derivation R A M) (r : S) (a : A) :
D (r • a) = r • D a :=
D.to_linear_map.map_smul_of_tower r a
@[simp] lemma map_one_eq_zero : D 1 = 0 := D.map_one_eq_zero'
@[simp] lemma map_algebra_map : D (algebra_map R A r) = 0 :=
by rw [←mul_one r, ring_hom.map_mul, ring_hom.map_one, ←smul_def, map_smul, map_one_eq_zero,
smul_zero]
@[simp] lemma map_coe_nat (n : ℕ) : D (n : A) = 0 :=
by rw [← nsmul_one, D.map_smul_of_tower n, map_one_eq_zero, smul_zero]
@[simp] lemma leibniz_pow (n : ℕ) : D (a ^ n) = n • a ^ (n - 1) • D a :=
begin
induction n with n ihn,
{ rw [pow_zero, map_one_eq_zero, zero_smul] },
{ rcases (zero_le n).eq_or_lt with (rfl|hpos),
{ rw [pow_one, one_smul, pow_zero, one_smul] },
{ have : a * a ^ (n - 1) = a ^ n, by rw [← pow_succ, nat.sub_add_cancel hpos],
simp only [pow_succ, leibniz, ihn, smul_comm a n, smul_smul a, add_smul, this,
nat.succ_eq_add_one, nat.add_succ_sub_one, add_zero, one_nsmul] } }
end
lemma eq_on_adjoin {s : set A} (h : set.eq_on D1 D2 s) : set.eq_on D1 D2 (adjoin R s) :=
λ x hx, algebra.adjoin_induction hx h
(λ r, (D1.map_algebra_map r).trans (D2.map_algebra_map r).symm)
(λ x y hx hy, by simp only [map_add, *])
(λ x y hx hy, by simp only [leibniz, *])
/-- If adjoin of a set is the whole algebra, then any two derivations equal on this set are equal
on the whole algebra. -/
lemma ext_of_adjoin_eq_top (s : set A) (hs : adjoin R s = ⊤) (h : set.eq_on D1 D2 s) : D1 = D2 :=
ext $ λ a, eq_on_adjoin h $ hs.symm ▸ trivial
/- Data typeclasses -/
instance : has_zero (derivation R A M) :=
⟨{ to_linear_map := 0,
map_one_eq_zero' := rfl,
leibniz' := λ a b, by simp only [add_zero, linear_map.zero_apply, smul_zero] }⟩
@[simp] lemma coe_zero : ⇑(0 : derivation R A M) = 0 := rfl
@[simp] lemma coe_zero_linear_map : ↑(0 : derivation R A M) = (0 : A →ₗ[R] M) := rfl
lemma zero_apply (a : A) : (0 : derivation R A M) a = 0 := rfl
instance : has_add (derivation R A M) :=
⟨λ D1 D2,
{ to_linear_map := D1 + D2,
map_one_eq_zero' := by simp,
leibniz' := λ a b, by simp only [leibniz, linear_map.add_apply,
coe_fn_coe, smul_add, add_add_add_comm] }⟩
@[simp] lemma coe_add (D1 D2 : derivation R A M) : ⇑(D1 + D2) = D1 + D2 := rfl
@[simp] lemma coe_add_linear_map (D1 D2 : derivation R A M) : ↑(D1 + D2) = (D1 + D2 : A →ₗ[R] M) :=
rfl
lemma add_apply : (D1 + D2) a = D1 a + D2 a := rfl
instance : inhabited (derivation R A M) := ⟨0⟩
section scalar
variables {S : Type*} [monoid S] [distrib_mul_action S M] [smul_comm_class R S M]
[smul_comm_class S A M]
@[priority 100]
instance : has_smul S (derivation R A M) :=
⟨λ r D,
{ to_linear_map := r • D,
map_one_eq_zero' := by rw [linear_map.smul_apply, coe_fn_coe, D.map_one_eq_zero, smul_zero],
leibniz' := λ a b, by simp only [linear_map.smul_apply, coe_fn_coe, leibniz, smul_add,
smul_comm r] }⟩
@[simp] lemma coe_smul (r : S) (D : derivation R A M) : ⇑(r • D) = r • D := rfl
@[simp] lemma coe_smul_linear_map (r : S) (D : derivation R A M) :
↑(r • D) = (r • D : A →ₗ[R] M) := rfl
lemma smul_apply (r : S) (D : derivation R A M) : (r • D) a = r • D a := rfl
instance : add_comm_monoid (derivation R A M) :=
coe_injective.add_comm_monoid _ coe_zero coe_add (λ _ _, rfl)
/-- `coe_fn` as an `add_monoid_hom`. -/
def coe_fn_add_monoid_hom : derivation R A M →+ (A → M) :=
{ to_fun := coe_fn, map_zero' := coe_zero, map_add' := coe_add }
@[priority 100]
instance : distrib_mul_action S (derivation R A M) :=
function.injective.distrib_mul_action coe_fn_add_monoid_hom coe_injective coe_smul
instance [distrib_mul_action Sᵐᵒᵖ M] [is_central_scalar S M] :
is_central_scalar S (derivation R A M) :=
{ op_smul_eq_smul := λ _ _, ext $ λ _, op_smul_eq_smul _ _}
end scalar
@[priority 100]
instance {S : Type*} [semiring S] [module S M] [smul_comm_class R S M] [smul_comm_class S A M] :
module S (derivation R A M) :=
function.injective.module S coe_fn_add_monoid_hom coe_injective coe_smul
instance [is_scalar_tower R A M] : is_scalar_tower R A (derivation R A M) :=
⟨λ x y z, ext (λ a, smul_assoc _ _ _)⟩
section push_forward
variables {N : Type*} [add_comm_monoid N] [module A N] [module R N] [is_scalar_tower R A M]
[is_scalar_tower R A N]
variables (f : M →ₗ[A] N) (e : M ≃ₗ[A] N)
/-- We can push forward derivations using linear maps, i.e., the composition of a derivation with a
linear map is a derivation. Furthermore, this operation is linear on the spaces of derivations. -/
def _root_.linear_map.comp_der : derivation R A M →ₗ[R] derivation R A N :=
{ to_fun := λ D,
{ to_linear_map := (f : M →ₗ[R] N).comp (D : A →ₗ[R] M),
map_one_eq_zero' := by simp only [linear_map.comp_apply, coe_fn_coe, map_one_eq_zero, map_zero],
leibniz' := λ a b, by simp only [coe_fn_coe, linear_map.comp_apply, linear_map.map_add,
leibniz, linear_map.coe_coe_is_scalar_tower, linear_map.map_smul] },
map_add' := λ D₁ D₂, by { ext, exact linear_map.map_add _ _ _, },
map_smul' := λ r D, by { ext, exact linear_map.map_smul _ _ _, }, }
@[simp] lemma coe_to_linear_map_comp :
(f.comp_der D : A →ₗ[R] N) = (f : M →ₗ[R] N).comp (D : A →ₗ[R] M) :=
rfl
@[simp] lemma coe_comp :
(f.comp_der D : A → N) = (f : M →ₗ[R] N).comp (D : A →ₗ[R] M) :=
rfl
/-- The composition of a derivation with a linear map as a bilinear map -/
@[simps]
def llcomp : (M →ₗ[A] N) →ₗ[A] derivation R A M →ₗ[R] derivation R A N :=
{ to_fun := λ f, f.comp_der,
map_add' := λ f₁ f₂, by { ext, refl },
map_smul' := λ r D, by { ext, refl } }
/-- Pushing a derivation foward through a linear equivalence is an equivalence. -/
def _root_.linear_equiv.comp_der : derivation R A M ≃ₗ[R] derivation R A N :=
{ inv_fun := e.symm.to_linear_map.comp_der,
left_inv := λ D, by { ext a, exact e.symm_apply_apply (D a) },
right_inv := λ D, by { ext a, exact e.apply_symm_apply (D a) },
..e.to_linear_map.comp_der }
end push_forward
section restrict_scalars
variables {S : Type*} [comm_semiring S]
variables [algebra S A] [module S M] [linear_map.compatible_smul A M R S]
variables (R)
/-- If `A` is both an `R`-algebra and an `S`-algebra; `M` is both an `R`-module and an `S`-module,
then an `S`-derivation `A → M` is also an `R`-derivation if it is also `R`-linear. -/
protected
def restrict_scalars (d : derivation S A M) : derivation R A M :=
{ map_one_eq_zero' := d.map_one_eq_zero,
leibniz' := d.leibniz,
to_linear_map := d.to_linear_map.restrict_scalars R }
end restrict_scalars
end
section cancel
variables {R : Type*} [comm_semiring R] {A : Type*} [comm_semiring A] [algebra R A]
{M : Type*} [add_cancel_comm_monoid M] [module R M] [module A M]
/-- Define `derivation R A M` from a linear map when `M` is cancellative by verifying the Leibniz
rule. -/
def mk' (D : A →ₗ[R] M) (h : ∀ a b, D (a * b) = a • D b + b • D a) : derivation R A M :=
{ to_linear_map := D,
map_one_eq_zero' := add_right_eq_self.1 $ by simpa only [one_smul, one_mul] using (h 1 1).symm,
leibniz' := h }
@[simp] lemma coe_mk' (D : A →ₗ[R] M) (h) : ⇑(mk' D h) = D := rfl
@[simp] lemma coe_mk'_linear_map (D : A →ₗ[R] M) (h) : (mk' D h : A →ₗ[R] M) = D := rfl
end cancel
section
variables {R : Type*} [comm_ring R]
variables {A : Type*} [comm_ring A] [algebra R A]
section
variables {M : Type*} [add_comm_group M] [module A M] [module R M]
variables (D : derivation R A M) {D1 D2 : derivation R A M} (r : R) (a b : A)
protected lemma map_neg : D (-a) = -D a := map_neg D a
protected lemma map_sub : D (a - b) = D a - D b := map_sub D a b
@[simp] lemma map_coe_int (n : ℤ) : D (n : A) = 0 :=
by rw [← zsmul_one, D.map_smul_of_tower n, map_one_eq_zero, smul_zero]
lemma leibniz_of_mul_eq_one {a b : A} (h : a * b = 1) : D a = -a^2 • D b :=
begin
rw neg_smul,
refine eq_neg_of_add_eq_zero_left _,
calc D a + a ^ 2 • D b = a • b • D a + a • a • D b : by simp only [smul_smul, h, one_smul, sq]
... = a • D (a * b) : by rw [leibniz, smul_add, add_comm]
... = 0 : by rw [h, map_one_eq_zero, smul_zero]
end
lemma leibniz_inv_of [invertible a] : D (⅟a) = -⅟a^2 • D a :=
D.leibniz_of_mul_eq_one $ inv_of_mul_self a
lemma leibniz_inv {K : Type*} [field K] [module K M] [algebra R K] (D : derivation R K M) (a : K) :
D (a⁻¹) = -a⁻¹ ^ 2 • D a :=
begin
rcases eq_or_ne a 0 with (rfl|ha),
{ simp },
{ exact D.leibniz_of_mul_eq_one (inv_mul_cancel ha) }
end
instance : has_neg (derivation R A M) :=
⟨λ D, mk' (-D) $ λ a b,
by simp only [linear_map.neg_apply, smul_neg, neg_add_rev, leibniz, coe_fn_coe, add_comm]⟩
@[simp] lemma coe_neg (D : derivation R A M) : ⇑(-D) = -D := rfl
@[simp] lemma coe_neg_linear_map (D : derivation R A M) : ↑(-D) = (-D : A →ₗ[R] M) :=
rfl
lemma neg_apply : (-D) a = -D a := rfl
instance : has_sub (derivation R A M) :=
⟨λ D1 D2, mk' (D1 - D2 : A →ₗ[R] M) $ λ a b,
by simp only [linear_map.sub_apply, leibniz, coe_fn_coe, smul_sub, add_sub_add_comm]⟩
@[simp] lemma coe_sub (D1 D2 : derivation R A M) : ⇑(D1 - D2) = D1 - D2 := rfl
@[simp] lemma coe_sub_linear_map (D1 D2 : derivation R A M) : ↑(D1 - D2) = (D1 - D2 : A →ₗ[R] M) :=
rfl
lemma sub_apply : (D1 - D2) a = D1 a - D2 a := rfl
instance : add_comm_group (derivation R A M) :=
coe_injective.add_comm_group _ coe_zero coe_add coe_neg coe_sub (λ _ _, rfl) (λ _ _, rfl)
end
section lie_structures
/-! # Lie structures -/
variables (D : derivation R A A) {D1 D2 : derivation R A A} (r : R) (a b : A)
/-- The commutator of derivations is again a derivation. -/
instance : has_bracket (derivation R A A) (derivation R A A) :=
⟨λ D1 D2, mk' (⁅(D1 : module.End R A), (D2 : module.End R A)⁆) $ λ a b,
by { simp only [ring.lie_def, map_add, id.smul_eq_mul, linear_map.mul_apply, leibniz, coe_fn_coe,
linear_map.sub_apply], ring, }⟩
@[simp] lemma commutator_coe_linear_map :
↑⁅D1, D2⁆ = ⁅(D1 : module.End R A), (D2 : module.End R A)⁆ := rfl
lemma commutator_apply : ⁅D1, D2⁆ a = D1 (D2 a) - D2 (D1 a) := rfl
instance : lie_ring (derivation R A A) :=
{ add_lie := λ d e f, by { ext a, simp only [commutator_apply, add_apply, map_add], ring, },
lie_add := λ d e f, by { ext a, simp only [commutator_apply, add_apply, map_add], ring, },
lie_self := λ d, by { ext a, simp only [commutator_apply, add_apply, map_add], ring_nf, },
leibniz_lie := λ d e f,
by { ext a, simp only [commutator_apply, add_apply, sub_apply, map_sub], ring, } }
instance : lie_algebra R (derivation R A A) :=
{ lie_smul := λ r d e, by { ext a, simp only [commutator_apply, map_smul, smul_sub, smul_apply]},
..derivation.module }
end lie_structures
end
end derivation
section to_square_zero
universes u v w
variables {R : Type u} {A : Type v} {B : Type w} [comm_semiring R] [comm_semiring A] [comm_ring B]
variables [algebra R A] [algebra R B] (I : ideal B) (hI : I ^ 2 = ⊥)
/-- If `f₁ f₂ : A →ₐ[R] B` are two lifts of the same `A →ₐ[R] B ⧸ I`,
we may define a map `f₁ - f₂ : A →ₗ[R] I`. -/
def diff_to_ideal_of_quotient_comp_eq (f₁ f₂ : A →ₐ[R] B)
(e : (ideal.quotient.mkₐ R I).comp f₁ = (ideal.quotient.mkₐ R I).comp f₂) :
A →ₗ[R] I :=
linear_map.cod_restrict (I.restrict_scalars _) (f₁.to_linear_map - f₂.to_linear_map)
begin
intro x,
change f₁ x - f₂ x ∈ I,
rw [← ideal.quotient.eq, ← ideal.quotient.mkₐ_eq_mk R, ← alg_hom.comp_apply, e],
refl,
end
@[simp]
lemma diff_to_ideal_of_quotient_comp_eq_apply (f₁ f₂ : A →ₐ[R] B)
(e : (ideal.quotient.mkₐ R I).comp f₁ = (ideal.quotient.mkₐ R I).comp f₂) (x : A) :
((diff_to_ideal_of_quotient_comp_eq I f₁ f₂ e) x : B) = f₁ x - f₂ x :=
rfl
variables [algebra A B] [is_scalar_tower R A B]
include hI
/-- Given a tower of algebras `R → A → B`, and a square-zero `I : ideal B`, each lift `A →ₐ[R] B`
of the canonical map `A →ₐ[R] B ⧸ I` corresponds to a `R`-derivation from `A` to `I`. -/
def derivation_to_square_zero_of_lift
(f : A →ₐ[R] B) (e : (ideal.quotient.mkₐ R I).comp f = is_scalar_tower.to_alg_hom R A (B ⧸ I)) :
derivation R A I :=
begin
refine
{ map_one_eq_zero' := _,
leibniz' := _,
..(diff_to_ideal_of_quotient_comp_eq I f (is_scalar_tower.to_alg_hom R A B) _) },
{ rw e, ext, refl },
{ ext, change f 1 - algebra_map A B 1 = 0, rw [map_one, map_one, sub_self] },
{ intros x y,
let F := diff_to_ideal_of_quotient_comp_eq I f (is_scalar_tower.to_alg_hom R A B)
(by { rw e, ext, refl }),
have : (f x - algebra_map A B x) * (f y - algebra_map A B y) = 0,
{ rw [← ideal.mem_bot, ← hI, pow_two],
convert (ideal.mul_mem_mul (F x).2 (F y).2) using 1 },
ext,
dsimp only [submodule.coe_add, submodule.coe_mk, linear_map.coe_mk,
diff_to_ideal_of_quotient_comp_eq_apply, submodule.coe_smul_of_tower,
is_scalar_tower.coe_to_alg_hom', linear_map.to_fun_eq_coe],
simp only [map_mul, sub_mul, mul_sub, algebra.smul_def] at ⊢ this,
rw [sub_eq_iff_eq_add, sub_eq_iff_eq_add] at this,
rw this,
ring }
end
lemma derivation_to_square_zero_of_lift_apply (f : A →ₐ[R] B)
(e : (ideal.quotient.mkₐ R I).comp f = is_scalar_tower.to_alg_hom R A (B ⧸ I))
(x : A) : (derivation_to_square_zero_of_lift I hI f e x : B) = f x - algebra_map A B x := rfl
/-- Given a tower of algebras `R → A → B`, and a square-zero `I : ideal B`, each `R`-derivation
from `A` to `I` corresponds to a lift `A →ₐ[R] B` of the canonical map `A →ₐ[R] B ⧸ I`. -/
def lift_of_derivation_to_square_zero (f : derivation R A I) :
A →ₐ[R] B :=
{ map_one' := show (f 1 : B) + algebra_map A B 1 = 1,
by rw [map_one, f.map_one_eq_zero, submodule.coe_zero, zero_add],
map_mul' := λ x y, begin
have : (f x : B) * (f y) = 0,
{ rw [← ideal.mem_bot, ← hI, pow_two], convert (ideal.mul_mem_mul (f x).2 (f y).2) using 1 },
dsimp,
simp only [map_mul, f.leibniz, add_mul, mul_add, submodule.coe_add,
submodule.coe_smul_of_tower, algebra.smul_def, this],
ring
end,
commutes' := λ r, by { dsimp, simp [← is_scalar_tower.algebra_map_apply R A B r] },
map_zero' := ((I.restrict_scalars R).subtype.comp f.to_linear_map +
(is_scalar_tower.to_alg_hom R A B).to_linear_map).map_zero,
..((I.restrict_scalars R).subtype.comp f.to_linear_map +
(is_scalar_tower.to_alg_hom R A B).to_linear_map) }
lemma lift_of_derivation_to_square_zero_apply (f : derivation R A I) (x : A) :
lift_of_derivation_to_square_zero I hI f x = f x + algebra_map A B x := rfl
@[simp] lemma lift_of_derivation_to_square_zero_mk_apply (d : derivation R A I) (x : A) :
ideal.quotient.mk I (lift_of_derivation_to_square_zero I hI d x) = algebra_map A (B ⧸ I) x :=
by { rw [lift_of_derivation_to_square_zero_apply, map_add,
ideal.quotient.eq_zero_iff_mem.mpr (d x).prop, zero_add], refl }
/-- Given a tower of algebras `R → A → B`, and a square-zero `I : ideal B`,
there is a 1-1 correspondance between `R`-derivations from `A` to `I` and
lifts `A →ₐ[R] B` of the canonical map `A →ₐ[R] B ⧸ I`. -/
@[simps]
def derivation_to_square_zero_equiv_lift :
derivation R A I ≃
{ f : A →ₐ[R] B // (ideal.quotient.mkₐ R I).comp f = is_scalar_tower.to_alg_hom R A (B ⧸ I) } :=
begin
refine ⟨λ d, ⟨lift_of_derivation_to_square_zero I hI d, _⟩, λ f,
(derivation_to_square_zero_of_lift I hI f.1 f.2 : _), _, _⟩,
{ ext x, exact lift_of_derivation_to_square_zero_mk_apply I hI d x },
{ intro d, ext x, exact add_sub_cancel (d x : B) (algebra_map A B x) },
{ rintro ⟨f, hf⟩, ext x, exact sub_add_cancel (f x) (algebra_map A B x) }
end
end to_square_zero
section kaehler_differential
open_locale tensor_product
variables (R S : Type*) [comm_ring R] [comm_ring S] [algebra R S]
/-- The kernel of the multiplication map `S ⊗[R] S →ₐ[R] S`. -/
abbreviation kaehler_differential.ideal : ideal (S ⊗[R] S) :=
ring_hom.ker (tensor_product.lmul' R : S ⊗[R] S →ₐ[R] S)
variable {S}
lemma kaehler_differential.one_smul_sub_smul_one_mem_ideal (a : S) :
(1 : S) ⊗ₜ[R] a - a ⊗ₜ[R] (1 : S) ∈ kaehler_differential.ideal R S :=
by simp [ring_hom.mem_ker]
variables {R}
variables {M : Type*} [add_comm_group M] [module R M] [module S M] [is_scalar_tower R S M]
/-- For a `R`-derivation `S → M`, this is the map `S ⊗[R] S →ₗ[S] M` sending `s ⊗ₜ t ↦ s • D t`. -/
def derivation.tensor_product_to (D : derivation R S M) : S ⊗[R] S →ₗ[S] M :=
tensor_product.algebra_tensor_module.lift ((linear_map.lsmul S (S →ₗ[R] M)).flip D.to_linear_map)
lemma derivation.tensor_product_to_tmul (D : derivation R S M) (s t : S) :
D.tensor_product_to (s ⊗ₜ t) = s • D t :=
tensor_product.lift.tmul s t
lemma derivation.tensor_product_to_mul (D : derivation R S M) (x y : S ⊗[R] S) :
D.tensor_product_to (x * y) = tensor_product.lmul' R x • D.tensor_product_to y +
tensor_product.lmul' R y • D.tensor_product_to x :=
begin
apply tensor_product.induction_on x,
{ rw [zero_mul, map_zero, map_zero, zero_smul, smul_zero, add_zero] },
swap, { rintros, simp only [add_mul, map_add, add_smul, *, smul_add], rw add_add_add_comm },
intros x₁ x₂,
apply tensor_product.induction_on y,
{ rw [mul_zero, map_zero, map_zero, zero_smul, smul_zero, add_zero] },
swap, { rintros, simp only [mul_add, map_add, add_smul, *, smul_add], rw add_add_add_comm },
intros x y,
simp only [tensor_product.tmul_mul_tmul, derivation.tensor_product_to,
tensor_product.algebra_tensor_module.lift_apply, tensor_product.lift.tmul',
tensor_product.lmul'_apply_tmul],
dsimp,
rw D.leibniz,
simp only [smul_smul, smul_add, mul_comm (x * y) x₁, mul_right_comm x₁ x₂, ← mul_assoc],
end
variables (R S)
/-- The kernel of `S ⊗[R] S →ₐ[R] S` is generated by `1 ⊗ s - s ⊗ 1` as a `S`-module. -/
lemma kaehler_differential.submodule_span_range_eq_ideal :
submodule.span S (set.range $ λ s : S, (1 : S) ⊗ₜ[R] s - s ⊗ₜ[R] (1 : S)) =
(kaehler_differential.ideal R S).restrict_scalars S :=
begin
apply le_antisymm,
{ rw submodule.span_le,
rintros _ ⟨s, rfl⟩,
exact kaehler_differential.one_smul_sub_smul_one_mem_ideal _ _ },
{ rintros x (hx : _ = _),
have : x - (tensor_product.lmul' R x) ⊗ₜ[R] (1 : S) = x,
{ rw [hx, tensor_product.zero_tmul, sub_zero] },
rw ← this,
clear this hx,
apply tensor_product.induction_on x; clear x,
{ rw [map_zero, tensor_product.zero_tmul, sub_zero], exact zero_mem _ },
{ intros x y,
convert_to x • (1 ⊗ₜ y - y ⊗ₜ 1) ∈ _,
{ rw [tensor_product.lmul'_apply_tmul, smul_sub, tensor_product.smul_tmul',
tensor_product.smul_tmul', smul_eq_mul, smul_eq_mul, mul_one] },
{ refine submodule.smul_mem _ x _,
apply submodule.subset_span,
exact set.mem_range_self y } },
{ intros x y hx hy,
rw [map_add, tensor_product.add_tmul, ← sub_add_sub_comm],
exact add_mem hx hy } }
end
lemma kaehler_differential.span_range_eq_ideal :
ideal.span (set.range $ λ s : S, (1 : S) ⊗ₜ[R] s - s ⊗ₜ[R] (1 : S)) =
kaehler_differential.ideal R S :=
begin
apply le_antisymm,
{ rw ideal.span_le,
rintros _ ⟨s, rfl⟩,
exact kaehler_differential.one_smul_sub_smul_one_mem_ideal _ _ },
{ change (kaehler_differential.ideal R S).restrict_scalars S ≤ (ideal.span _).restrict_scalars S,
rw [← kaehler_differential.submodule_span_range_eq_ideal, ideal.span],
conv_rhs { rw ← submodule.span_span_of_tower S },
exact submodule.subset_span }
end
/--
The module of Kähler differentials (Kahler differentials, Kaehler differentials).
This is implemented as `I / I ^ 2` with `I` the kernel of the multiplication map `S ⊗[R] S →ₐ[R] S`.
To view elements as a linear combination of the form `s • D s'`, use
`kaehler_differential.tensor_product_to_surjective` and `derivation.tensor_product_to_tmul`.
We also provide the notation `Ω[S⁄R]` for `kaehler_differential R S`.
Note that the slash is `\textfractionsolidus`.
-/
@[derive [add_comm_group, module (S ⊗[R] S)]]
def kaehler_differential : Type* := (kaehler_differential.ideal R S).cotangent
notation `Ω[`:100 S `⁄`:0 R `]`:0 := kaehler_differential R S
instance : nonempty Ω[S⁄R] := ⟨0⟩
instance kaehler_differential.module' {R' : Type*} [comm_ring R'] [algebra R' S] :
module R' Ω[S⁄R] :=
(module.comp_hom (kaehler_differential.ideal R S).cotangent (algebra_map R' S) : _)
instance : is_scalar_tower S (S ⊗[R] S) Ω[S⁄R] :=
ideal.cotangent.is_scalar_tower _
instance kaehler_differential.is_scalar_tower_of_tower {R₁ R₂ : Type*} [comm_ring R₁] [comm_ring R₂]
[algebra R₁ S] [algebra R₂ S] [algebra R₁ R₂] [is_scalar_tower R₁ R₂ S] :
is_scalar_tower R₁ R₂ Ω[S⁄R] :=
begin
convert restrict_scalars.is_scalar_tower R₁ R₂ Ω[S⁄R] using 1,
ext x m,
show algebra_map R₁ S x • m = algebra_map R₂ S (algebra_map R₁ R₂ x) • m,
rw ← is_scalar_tower.algebra_map_apply,
end
instance kaehler_differential.is_scalar_tower' :
is_scalar_tower R (S ⊗[R] S) Ω[S⁄R] :=
begin
convert restrict_scalars.is_scalar_tower R (S ⊗[R] S) Ω[S⁄R] using 1,
ext x m,
show algebra_map R S x • m = algebra_map R (S ⊗[R] S) x • m,
simp_rw [is_scalar_tower.algebra_map_apply R S (S ⊗[R] S), is_scalar_tower.algebra_map_smul]
end
/-- The quotient map `I → Ω[S⁄R]` with `I` being the kernel of `S ⊗[R] S → S`. -/
def kaehler_differential.from_ideal : kaehler_differential.ideal R S →ₗ[S ⊗[R] S] Ω[S⁄R] :=
(kaehler_differential.ideal R S).to_cotangent
/-- (Implementation) The underlying linear map of the derivation into `Ω[S⁄R]`. -/
def kaehler_differential.D_linear_map : S →ₗ[R] Ω[S⁄R] :=
((kaehler_differential.from_ideal R S).restrict_scalars R).comp
((tensor_product.include_right.to_linear_map - tensor_product.include_left.to_linear_map :
S →ₗ[R] S ⊗[R] S).cod_restrict ((kaehler_differential.ideal R S).restrict_scalars R)
(kaehler_differential.one_smul_sub_smul_one_mem_ideal R) : _ →ₗ[R] _)
lemma kaehler_differential.D_linear_map_apply (s : S) :
kaehler_differential.D_linear_map R S s = (kaehler_differential.ideal R S).to_cotangent
⟨1 ⊗ₜ s - s ⊗ₜ 1, kaehler_differential.one_smul_sub_smul_one_mem_ideal R s⟩ :=
rfl
/-- The universal derivation into `Ω[S⁄R]`. -/
def kaehler_differential.D : derivation R S Ω[S⁄R] :=
{ map_one_eq_zero' := begin
dsimp [kaehler_differential.D_linear_map_apply],
rw [ideal.to_cotangent_eq_zero, subtype.coe_mk, sub_self],
exact zero_mem _
end,
leibniz' := λ a b, begin
dsimp [kaehler_differential.D_linear_map_apply],
rw [← linear_map.map_smul_of_tower, ← linear_map.map_smul_of_tower, ← map_add,
ideal.to_cotangent_eq, pow_two],
convert submodule.mul_mem_mul (kaehler_differential.one_smul_sub_smul_one_mem_ideal R a : _)
(kaehler_differential.one_smul_sub_smul_one_mem_ideal R b : _) using 1,
simp only [add_subgroup_class.coe_sub, submodule.coe_add, submodule.coe_mk,
tensor_product.tmul_mul_tmul, mul_sub, sub_mul, mul_comm b,
submodule.coe_smul_of_tower, smul_sub, tensor_product.smul_tmul', smul_eq_mul, mul_one],
ring_nf,
end,
..(kaehler_differential.D_linear_map R S) }
lemma kaehler_differential.D_apply (s : S) :
kaehler_differential.D R S s = (kaehler_differential.ideal R S).to_cotangent
⟨1 ⊗ₜ s - s ⊗ₜ 1, kaehler_differential.one_smul_sub_smul_one_mem_ideal R s⟩ :=
rfl
lemma kaehler_differential.span_range_derivation :
submodule.span S (set.range $ kaehler_differential.D R S) = ⊤ :=
begin
rw _root_.eq_top_iff,
rintros x -,
obtain ⟨⟨x, hx⟩, rfl⟩ := ideal.to_cotangent_surjective _ x,
have : x ∈ (kaehler_differential.ideal R S).restrict_scalars S := hx,
rw ← kaehler_differential.submodule_span_range_eq_ideal at this,
suffices : ∃ hx, (kaehler_differential.ideal R S).to_cotangent ⟨x, hx⟩ ∈
submodule.span S (set.range $ kaehler_differential.D R S),
{ exact this.some_spec },
apply submodule.span_induction this,
{ rintros _ ⟨x, rfl⟩,
refine ⟨kaehler_differential.one_smul_sub_smul_one_mem_ideal R x, _⟩,
apply submodule.subset_span,
exact ⟨x, kaehler_differential.D_linear_map_apply R S x⟩ },
{ exact ⟨zero_mem _, zero_mem _⟩ },
{ rintros x y ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩, exact ⟨add_mem hx₁ hy₁, add_mem hx₂ hy₂⟩ },
{ rintros r x ⟨hx₁, hx₂⟩, exact ⟨((kaehler_differential.ideal R S).restrict_scalars S).smul_mem
r hx₁, submodule.smul_mem _ r hx₂⟩ }
end
variables {R S}
/-- The linear map from `Ω[S⁄R]`, associated with a derivation. -/
def derivation.lift_kaehler_differential (D : derivation R S M) : Ω[S⁄R] →ₗ[S] M :=
begin
refine ((kaehler_differential.ideal R S • ⊤ :
submodule (S ⊗[R] S) (kaehler_differential.ideal R S)).restrict_scalars S).liftq _ _,
{ exact D.tensor_product_to.comp ((kaehler_differential.ideal R S).subtype.restrict_scalars S) },
{ intros x hx,
change _ = _,
apply submodule.smul_induction_on hx; clear hx x,
{ rintros x (hx : _ = _) ⟨y, hy : _ = _⟩ -,
dsimp,
rw [derivation.tensor_product_to_mul, hx, hy, zero_smul, zero_smul, zero_add] },
{ intros x y ex ey, rw [map_add, ex, ey, zero_add] } }
end
lemma derivation.lift_kaehler_differential_apply (D : derivation R S M) (x) :
D.lift_kaehler_differential
((kaehler_differential.ideal R S).to_cotangent x) = D.tensor_product_to x :=
rfl
lemma derivation.lift_kaehler_differential_comp (D : derivation R S M) :
D.lift_kaehler_differential.comp_der (kaehler_differential.D R S) = D :=
begin
ext a,
dsimp [kaehler_differential.D_apply],
refine (D.lift_kaehler_differential_apply _).trans _,
rw [subtype.coe_mk, map_sub, derivation.tensor_product_to_tmul,
derivation.tensor_product_to_tmul, one_smul, D.map_one_eq_zero, smul_zero, sub_zero],
end
@[simp] lemma derivation.lift_kaehler_differential_comp_D (D' : derivation R S M) (x : S) :
D'.lift_kaehler_differential (kaehler_differential.D R S x) = D' x :=
derivation.congr_fun D'.lift_kaehler_differential_comp x
@[ext]
lemma derivation.lift_kaehler_differential_unique
(f f' : Ω[S⁄R] →ₗ[S] M)
(hf : f.comp_der (kaehler_differential.D R S) =
f'.comp_der (kaehler_differential.D R S)) :
f = f' :=
begin
apply linear_map.ext,
intro x,
have : x ∈ submodule.span S (set.range $ kaehler_differential.D R S),
{ rw kaehler_differential.span_range_derivation, trivial },
apply submodule.span_induction this,
{ rintros _ ⟨x, rfl⟩, exact congr_arg (λ D : derivation R S M, D x) hf },
{ rw [map_zero, map_zero] },
{ intros x y hx hy, rw [map_add, map_add, hx, hy] },
{ intros a x e, rw [map_smul, map_smul, e] }
end
variables (R S)
lemma derivation.lift_kaehler_differential_D :
(kaehler_differential.D R S).lift_kaehler_differential = linear_map.id :=
derivation.lift_kaehler_differential_unique _ _
(kaehler_differential.D R S).lift_kaehler_differential_comp
variables {R S}
lemma kaehler_differential.D_tensor_product_to (x : kaehler_differential.ideal R S) :
(kaehler_differential.D R S).tensor_product_to x =
(kaehler_differential.ideal R S).to_cotangent x :=
begin
rw [← derivation.lift_kaehler_differential_apply, derivation.lift_kaehler_differential_D],
refl,
end
variables (R S)
lemma kaehler_differential.tensor_product_to_surjective :
function.surjective (kaehler_differential.D R S).tensor_product_to :=
begin
intro x, obtain ⟨x, rfl⟩ := (kaehler_differential.ideal R S).to_cotangent_surjective x,
exact ⟨x, kaehler_differential.D_tensor_product_to x⟩
end
/-- The `S`-linear maps from `Ω[S⁄R]` to `M` are (`S`-linearly) equivalent to `R`-derivations
from `S` to `M`. -/
def kaehler_differential.linear_map_equiv_derivation : (Ω[S⁄R] →ₗ[S] M) ≃ₗ[S] derivation R S M :=
{ inv_fun := derivation.lift_kaehler_differential,
left_inv := λ f, derivation.lift_kaehler_differential_unique _ _
(derivation.lift_kaehler_differential_comp _),
right_inv := derivation.lift_kaehler_differential_comp,
..(derivation.llcomp.flip $ kaehler_differential.D R S) }
/-- The quotient ring of `S ⊗ S ⧸ J ^ 2` by `Ω[S⁄R]` is isomorphic to `S`. -/
def kaehler_differential.quotient_cotangent_ideal_ring_equiv :
(S ⊗ S ⧸ kaehler_differential.ideal R S ^ 2) ⧸
(kaehler_differential.ideal R S).cotangent_ideal ≃+* S :=
begin
have : function.right_inverse tensor_product.include_left
(↑(tensor_product.lmul' R : S ⊗[R] S →ₐ[R] S) : S ⊗[R] S →+* S),
{ intro x, rw [alg_hom.coe_to_ring_hom, ← alg_hom.comp_apply,
tensor_product.lmul'_comp_include_left], refl },
refine (ideal.quot_cotangent _).trans _,
refine (ideal.quot_equiv_of_eq _).trans (ring_hom.quotient_ker_equiv_of_right_inverse this),
ext, refl,
end
/-- The quotient ring of `S ⊗ S ⧸ J ^ 2` by `Ω[S⁄R]` is isomorphic to `S` as an `S`-algebra. -/
def kaehler_differential.quotient_cotangent_ideal :
((S ⊗ S ⧸ kaehler_differential.ideal R S ^ 2) ⧸
(kaehler_differential.ideal R S).cotangent_ideal) ≃ₐ[S] S :=
{ commutes' := (kaehler_differential.quotient_cotangent_ideal_ring_equiv R S).apply_symm_apply,
..kaehler_differential.quotient_cotangent_ideal_ring_equiv R S }
lemma kaehler_differential.End_equiv_aux (f : S →ₐ[R] S ⊗ S ⧸ kaehler_differential.ideal R S ^ 2) :
(ideal.quotient.mkₐ R (kaehler_differential.ideal R S).cotangent_ideal).comp f =
is_scalar_tower.to_alg_hom R S _ ↔
(tensor_product.lmul' R : S ⊗[R] S →ₐ[R] S).ker_square_lift.comp f = alg_hom.id R S :=
begin
rw [alg_hom.ext_iff, alg_hom.ext_iff],
apply forall_congr,
intro x,
have e₁ : (tensor_product.lmul' R : S ⊗[R] S →ₐ[R] S).ker_square_lift (f x) =
kaehler_differential.quotient_cotangent_ideal_ring_equiv R S
(ideal.quotient.mk (kaehler_differential.ideal R S).cotangent_ideal $ f x),
{ generalize : f x = y, obtain ⟨y, rfl⟩ := ideal.quotient.mk_surjective y, refl },
have e₂ : x = kaehler_differential.quotient_cotangent_ideal_ring_equiv
R S (is_scalar_tower.to_alg_hom R S _ x),
{ exact ((tensor_product.lmul'_apply_tmul x 1).trans (mul_one x)).symm },
split,
{ intro e,
exact (e₁.trans (@ring_equiv.congr_arg _ _ _ _ _ _
(kaehler_differential.quotient_cotangent_ideal_ring_equiv R S) _ _ e)).trans e₂.symm },
{ intro e, apply (kaehler_differential.quotient_cotangent_ideal_ring_equiv R S).injective,
exact e₁.symm.trans (e.trans e₂) }
end
/-- Derivations into `Ω[S⁄R]` is equivalent to derivations
into `(kaehler_differential.ideal R S).cotangent_ideal` -/
-- This has type
-- `derivation R S Ω[ S / R ] ≃ₗ[R] derivation R S (kaehler_differential.ideal R S).cotangent_ideal`
-- But lean times-out if this is given explicitly.
noncomputable
def kaehler_differential.End_equiv_derivation' :=
@linear_equiv.comp_der R _ _ _ _ Ω[S⁄R] _ _ _ _ _ _ _ _ _
((kaehler_differential.ideal R S).cotangent_equiv_ideal.restrict_scalars S)
/-- (Implementation) An `equiv` version of `kaehler_differential.End_equiv_aux`.
Used in `kaehler_differential.End_equiv`. -/
def kaehler_differential.End_equiv_aux_equiv :
{f // (ideal.quotient.mkₐ R (kaehler_differential.ideal R S).cotangent_ideal).comp f =
is_scalar_tower.to_alg_hom R S _ } ≃
{ f // (tensor_product.lmul' R : S ⊗[R] S →ₐ[R] S).ker_square_lift.comp f = alg_hom.id R S } :=
(equiv.refl _).subtype_equiv (kaehler_differential.End_equiv_aux R S)
/--
The endomorphisms of `Ω[S⁄R]` corresponds to sections of the surjection `S ⊗[R] S ⧸ J ^ 2 →ₐ[R] S`,
with `J` being the kernel of the multiplication map `S ⊗[R] S →ₐ[R] S`.
-/
noncomputable
def kaehler_differential.End_equiv :
module.End S Ω[S⁄R] ≃
{ f // (tensor_product.lmul' R : S ⊗[R] S →ₐ[R] S).ker_square_lift.comp f = alg_hom.id R S } :=
(kaehler_differential.linear_map_equiv_derivation R S).to_equiv.trans $
(kaehler_differential.End_equiv_derivation' R S).to_equiv.trans $
(derivation_to_square_zero_equiv_lift
(kaehler_differential.ideal R S).cotangent_ideal
(kaehler_differential.ideal R S).cotangent_ideal_square).trans $
kaehler_differential.End_equiv_aux_equiv R S
section presentation
open kaehler_differential (D)
open finsupp (single)
/-- The `S`-submodule of `S →₀ S` (the direct sum of copies of `S` indexed by `S`) generated by
the relations:
1. `dx + dy = d(x + y)`
2. `x dy + y dx = d(x * y)`
3. `dr = 0` for `r ∈ R`
where `db` is the unit in the copy of `S` with index `b`.
This is the kernel of the surjection `finsupp.total S Ω[S⁄R] S (kaehler_differential.D R S)`.
See `kaehler_differential.ker_total_eq` and `kaehler_differential.total_surjective`.
-/
noncomputable
def kaehler_differential.ker_total : submodule S (S →₀ S) :=
submodule.span S
((set.range (λ (x : S × S), single x.1 1 + single x.2 1 - single (x.1 + x.2) 1)) ∪
(set.range (λ (x : S × S), single x.2 x.1 + single x.1 x.2 - single (x.1 * x.2) 1)) ∪
(set.range (λ x : R, single (algebra_map R S x) 1)))
local notation x `𝖣` y := (kaehler_differential.ker_total R S).mkq (single y x)
lemma kaehler_differential.ker_total_mkq_single_add (x y z) :
(z 𝖣 (x + y)) = (z 𝖣 x) + (z 𝖣 y) :=
begin
rw [← map_add, eq_comm, ← sub_eq_zero, ← map_sub, submodule.mkq_apply,
submodule.quotient.mk_eq_zero],
simp_rw [← finsupp.smul_single_one _ z, ← smul_add, ← smul_sub],
exact submodule.smul_mem _ _ (submodule.subset_span (or.inl $ or.inl $ ⟨⟨_, _⟩, rfl⟩)),
end
lemma kaehler_differential.ker_total_mkq_single_mul (x y z) :
(z 𝖣 (x * y)) = ((z * x) 𝖣 y) + ((z * y) 𝖣 x) :=
begin
rw [← map_add, eq_comm, ← sub_eq_zero, ← map_sub, submodule.mkq_apply,
submodule.quotient.mk_eq_zero],
simp_rw [← finsupp.smul_single_one _ z, ← @smul_eq_mul _ _ z,
← finsupp.smul_single, ← smul_add, ← smul_sub],
exact submodule.smul_mem _ _ (submodule.subset_span (or.inl $ or.inr $ ⟨⟨_, _⟩, rfl⟩)),
end
lemma kaehler_differential.ker_total_mkq_single_algebra_map (x y) :
(y 𝖣 (algebra_map R S x)) = 0 :=
begin
rw [submodule.mkq_apply, submodule.quotient.mk_eq_zero, ← finsupp.smul_single_one _ y],
exact submodule.smul_mem _ _ (submodule.subset_span (or.inr $ ⟨_, rfl⟩)),
end
lemma kaehler_differential.ker_total_mkq_single_algebra_map_one (x) :
(x 𝖣 1) = 0 :=
begin
rw [← (algebra_map R S).map_one, kaehler_differential.ker_total_mkq_single_algebra_map],
end
lemma kaehler_differential.ker_total_mkq_single_smul (r : R) (x y) :
(y 𝖣 (r • x)) = r • (y 𝖣 x) :=
begin
rw [algebra.smul_def, kaehler_differential.ker_total_mkq_single_mul,
kaehler_differential.ker_total_mkq_single_algebra_map, add_zero,
← linear_map.map_smul_of_tower, finsupp.smul_single, mul_comm, algebra.smul_def],
end
/-- The (universal) derivation into `(S →₀ S) ⧸ kaehler_differential.ker_total R S`. -/
noncomputable
def kaehler_differential.derivation_quot_ker_total :
derivation R S ((S →₀ S) ⧸ kaehler_differential.ker_total R S) :=
{ to_fun := λ x, 1 𝖣 x,
map_add' := λ x y, kaehler_differential.ker_total_mkq_single_add _ _ _ _ _,
map_smul' := λ r s, kaehler_differential.ker_total_mkq_single_smul _ _ _ _ _,
map_one_eq_zero' := kaehler_differential.ker_total_mkq_single_algebra_map_one _ _ _,
leibniz' := λ a b, (kaehler_differential.ker_total_mkq_single_mul _ _ _ _ _).trans
(by { simp_rw [← finsupp.smul_single_one _ (1 * _ : S)], dsimp, simp }) }
lemma kaehler_differential.derivation_quot_ker_total_apply (x) :
kaehler_differential.derivation_quot_ker_total R S x = (1 𝖣 x) := rfl
lemma kaehler_differential.derivation_quot_ker_total_lift_comp_total :
(kaehler_differential.derivation_quot_ker_total R S).lift_kaehler_differential.comp
(finsupp.total S Ω[S⁄R] S (kaehler_differential.D R S)) = submodule.mkq _ :=
begin
apply finsupp.lhom_ext,
intros a b,
conv_rhs { rw [← finsupp.smul_single_one a b, linear_map.map_smul] },
simp [kaehler_differential.derivation_quot_ker_total_apply],
end
lemma kaehler_differential.ker_total_eq :
(finsupp.total S Ω[S⁄R] S (kaehler_differential.D R S)).ker =
kaehler_differential.ker_total R S :=
begin
apply le_antisymm,
{ conv_rhs { rw ← (kaehler_differential.ker_total R S).ker_mkq },
rw ← kaehler_differential.derivation_quot_ker_total_lift_comp_total,
exact linear_map.ker_le_ker_comp _ _ },
{ rw [kaehler_differential.ker_total, submodule.span_le],
rintros _ ((⟨⟨x, y⟩, rfl⟩|⟨⟨x, y⟩, rfl⟩)|⟨x, rfl⟩); dsimp; simp [linear_map.mem_ker] },
end
lemma kaehler_differential.total_surjective :
function.surjective (finsupp.total S Ω[S⁄R] S (kaehler_differential.D R S)) :=
begin
rw [← linear_map.range_eq_top, finsupp.range_total, kaehler_differential.span_range_derivation],
end
/-- `Ω[S⁄R]` is isomorphic to `S` copies of `S` with kernel `kaehler_differential.ker_total`. -/
@[simps] noncomputable
def kaehler_differential.quot_ker_total_equiv :
((S →₀ S) ⧸ kaehler_differential.ker_total R S) ≃ₗ[S] Ω[S⁄R] :=
{ inv_fun := (kaehler_differential.derivation_quot_ker_total R S).lift_kaehler_differential,
left_inv := begin
intro x,
obtain ⟨x, rfl⟩ := submodule.mkq_surjective _ x,
exact linear_map.congr_fun
(kaehler_differential.derivation_quot_ker_total_lift_comp_total R S : _) x,
end,
right_inv := begin
intro x,
obtain ⟨x, rfl⟩ := kaehler_differential.total_surjective R S x,
erw linear_map.congr_fun
(kaehler_differential.derivation_quot_ker_total_lift_comp_total R S : _) x,
refl
end,
..(kaehler_differential.ker_total R S).liftq
(finsupp.total S Ω[S⁄R] S (kaehler_differential.D R S))
(kaehler_differential.ker_total_eq R S).ge }
lemma kaehler_differential.quot_ker_total_equiv_symm_comp_D :
(kaehler_differential.quot_ker_total_equiv R S).symm.to_linear_map.comp_der
(kaehler_differential.D R S) = kaehler_differential.derivation_quot_ker_total R S :=
by convert
(kaehler_differential.derivation_quot_ker_total R S).lift_kaehler_differential_comp using 0
variables (A B : Type*) [comm_ring A] [comm_ring B] [algebra R A] [algebra S B] [algebra R B]
variables [algebra A B] [is_scalar_tower R S B] [is_scalar_tower R A B]
-- The map `(A →₀ A) →ₗ[A] (B →₀ B)`
local notation `finsupp_map` := ((finsupp.map_range.linear_map (algebra.of_id A B).to_linear_map)
.comp (finsupp.lmap_domain A A (algebra_map A B)))
lemma kaehler_differential.ker_total_map (h : function.surjective (algebra_map A B)) :
(kaehler_differential.ker_total R A).map finsupp_map ⊔
submodule.span A (set.range (λ x : S, single (algebra_map S B x) (1 : B))) =
(kaehler_differential.ker_total S B).restrict_scalars _ :=
begin
rw [kaehler_differential.ker_total, submodule.map_span, kaehler_differential.ker_total,
← submodule.span_eq_restrict_scalars _ _ _ _ h],
simp_rw [set.image_union, submodule.span_union, ← set.image_univ, set.image_image,
set.image_univ, map_sub, map_add],
simp only [linear_map.comp_apply, finsupp.map_range.linear_map_apply, finsupp.map_range_single,
finsupp.lmap_domain_apply, finsupp.map_domain_single, alg_hom.to_linear_map_apply,
algebra.of_id_apply, ← is_scalar_tower.algebra_map_apply, map_one, map_add, map_mul],
simp_rw [sup_assoc, ← (h.prod_map h).range_comp],
congr' 3,
rw [sup_eq_right],
apply submodule.span_mono,
simp_rw is_scalar_tower.algebra_map_apply R S B,
exact set.range_comp_subset_range (algebra_map R S) (λ x, single (algebra_map S B x) (1 : B))
end
end presentation
section exact_sequence
local attribute [irreducible] kaehler_differential
/- We have the commutative diagram
A --→ B
↑ ↑
| |
R --→ S -/
variables (A B : Type*) [comm_ring A] [comm_ring B] [algebra R A] [algebra R B]
variables [algebra A B] [algebra S B] [is_scalar_tower R A B] [is_scalar_tower R S B]
variables {R B}
/-- For a tower `R → A → B` and an `R`-derivation `B → M`, we may compose with `A → B` to obtain an
`R`-derivation `A → M`. -/
def derivation.comp_algebra_map [module A M] [module B M] [is_scalar_tower A B M]
(d : derivation R B M) : derivation R A M :=
{ map_one_eq_zero' := by simp,
leibniz' := λ a b, by simp,
to_linear_map := d.to_linear_map.comp (is_scalar_tower.to_alg_hom R A B).to_linear_map }
variables (R B)
/-- The map `Ω[A⁄R] →ₗ[A] Ω[B⁄R]` given a square
A --→ B
↑ ↑
| |
R --→ S -/
def kaehler_differential.map : Ω[A⁄R] →ₗ[A] Ω[B⁄S] :=
derivation.lift_kaehler_differential
(((kaehler_differential.D S B).restrict_scalars R).comp_algebra_map A)
lemma kaehler_differential.map_comp_der :
(kaehler_differential.map R S A B).comp_der (kaehler_differential.D R A) =
(((kaehler_differential.D S B).restrict_scalars R).comp_algebra_map A) :=
derivation.lift_kaehler_differential_comp _
lemma kaehler_differential.map_D (x : A) :
kaehler_differential.map R S A B (kaehler_differential.D R A x) =
kaehler_differential.D S B (algebra_map A B x) :=
derivation.congr_fun (kaehler_differential.map_comp_der R S A B) x
open is_scalar_tower (to_alg_hom)
lemma kaehler_differential.map_surjective_of_surjective
(h : function.surjective (algebra_map A B)) :
function.surjective (kaehler_differential.map R S A B) :=
begin
rw [← linear_map.range_eq_top, _root_.eq_top_iff, ← @submodule.restrict_scalars_top B A,
← kaehler_differential.span_range_derivation, ← submodule.span_eq_restrict_scalars _ _ _ _ h,
submodule.span_le],
rintros _ ⟨x, rfl⟩,
obtain ⟨y, rfl⟩ := h x,
rw ← kaehler_differential.map_D R S A B,
exact ⟨_, rfl⟩,
end
/-- The lift of the map `Ω[A⁄R] →ₗ[A] Ω[B⁄R]` to the base change along `A → B`.
This is the first map in the exact sequence `B ⊗[A] Ω[A⁄R] → Ω[B⁄R] → Ω[B⁄A] → 0`. -/
noncomputable
def kaehler_differential.map_base_change : B ⊗[A] Ω[A⁄R] →ₗ[B] Ω[B⁄R] :=
(tensor_product.is_base_change A Ω[A⁄R] B).lift (kaehler_differential.map R R A B)
@[simp]
lemma kaehler_differential.map_base_change_tmul (x : B) (y : Ω[A⁄R]) :
kaehler_differential.map_base_change R A B (x ⊗ₜ y) =
x • kaehler_differential.map R R A B y :=
begin
conv_lhs { rw [← mul_one x, ← smul_eq_mul, ← tensor_product.smul_tmul', linear_map.map_smul] },
congr' 1,
exact is_base_change.lift_eq _ _ _
end
end exact_sequence
end kaehler_differential
|
7e5fa21f59e850594949d6253884fe11991bd37c | ff5230333a701471f46c57e8c115a073ebaaa448 | /library/init/data/list/lemmas.lean | 9adb58a58d28e235e59b88c1925d378a9057f9ac | [
"Apache-2.0"
] | permissive | stanford-cs242/lean | f81721d2b5d00bc175f2e58c57b710d465e6c858 | 7bd861261f4a37326dcf8d7a17f1f1f330e4548c | refs/heads/master | 1,600,957,431,849 | 1,576,465,093,000 | 1,576,465,093,000 | 225,779,423 | 0 | 3 | Apache-2.0 | 1,575,433,936,000 | 1,575,433,935,000 | null | UTF-8 | Lean | false | false | 10,426 | lean | /-
Copyright (c) 2014 Parikshit Khanna. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn
-/
prelude
import init.data.list.basic init.function init.meta init.data.nat.lemmas
import init.meta.interactive init.meta.smt.rsimp
universes u v w w₁ w₂
variables {α : Type u} {β : Type v} {γ : Type w}
namespace list
open nat
/- append -/
@[simp] lemma nil_append (s : list α) : [] ++ s = s :=
rfl
@[simp] lemma cons_append (x : α) (s t : list α) : (x::s) ++ t = x::(s ++ t) :=
rfl
@[simp] lemma append_nil (t : list α) : t ++ [] = t :=
by induction t; simp [*]
@[simp] lemma append_assoc (s t u : list α) : s ++ t ++ u = s ++ (t ++ u) :=
by induction s; simp [*]
/- length -/
lemma length_cons (a : α) (l : list α) : length (a :: l) = length l + 1 :=
rfl
@[simp] lemma length_append (s t : list α) : length (s ++ t) = length s + length t :=
by induction s; simp [*]
@[simp] lemma length_repeat (a : α) (n : ℕ) : length (repeat a n) = n :=
by induction n; simp [*]; refl
@[simp] lemma length_tail (l : list α) : length (tail l) = length l - 1 :=
by cases l; refl
-- TODO(Leo): cleanup proof after arith dec proc
@[simp] lemma length_drop : ∀ (i : ℕ) (l : list α), length (drop i l) = length l - i
| 0 l := rfl
| (succ i) [] := eq.symm (nat.zero_sub (succ i))
| (succ i) (x::l) := calc
length (drop (succ i) (x::l))
= length l - i : length_drop i l
... = succ (length l) - succ i : (nat.succ_sub_succ_eq_sub (length l) i).symm
/- map -/
lemma map_cons (f : α → β) (a l) : map f (a::l) = f a :: map f l :=
rfl
@[simp] lemma map_append (f : α → β) : ∀ l₁ l₂, map f (l₁ ++ l₂) = (map f l₁) ++ (map f l₂) :=
by intro l₁; induction l₁; intros; simp [*]
lemma map_singleton (f : α → β) (a : α) : map f [a] = [f a] :=
rfl
@[simp] lemma map_id (l : list α) : map id l = l :=
by induction l; simp [*]
@[simp] lemma map_map (g : β → γ) (f : α → β) (l : list α) : map g (map f l) = map (g ∘ f) l :=
by induction l; simp [*]
@[simp] lemma length_map (f : α → β) (l : list α) : length (map f l) = length l :=
by induction l; simp [*]
/- bind -/
@[simp] lemma nil_bind (f : α → list β) : list.bind [] f = [] :=
by simp [join, list.bind]
@[simp] lemma cons_bind (x xs) (f : α → list β) : list.bind (x :: xs) f = f x ++ list.bind xs f :=
by simp [join, list.bind]
@[simp] lemma append_bind (xs ys) (f : α → list β) : list.bind (xs ++ ys) f = list.bind xs f ++ list.bind ys f :=
by induction xs; [refl, simp [*, cons_bind]]
/- mem -/
@[simp] lemma mem_nil_iff (a : α) : a ∈ ([] : list α) ↔ false :=
iff.rfl
@[simp] lemma not_mem_nil (a : α) : a ∉ ([] : list α) :=
iff.mp $ mem_nil_iff a
@[simp] lemma mem_cons_self (a : α) (l : list α) : a ∈ a :: l :=
or.inl rfl
@[simp] lemma mem_cons_iff (a y : α) (l : list α) : a ∈ y :: l ↔ (a = y ∨ a ∈ l) :=
iff.rfl
@[rsimp] lemma mem_cons_eq (a y : α) (l : list α) : (a ∈ y :: l) = (a = y ∨ a ∈ l) :=
rfl
lemma mem_cons_of_mem (y : α) {a : α} {l : list α} : a ∈ l → a ∈ y :: l :=
assume H, or.inr H
lemma eq_or_mem_of_mem_cons {a y : α} {l : list α} : a ∈ y::l → a = y ∨ a ∈ l :=
assume h, h
@[simp] lemma mem_append {a : α} {s t : list α} : a ∈ s ++ t ↔ a ∈ s ∨ a ∈ t :=
by induction s; simp [*, or_assoc]
@[rsimp] lemma mem_append_eq (a : α) (s t : list α) : (a ∈ s ++ t) = (a ∈ s ∨ a ∈ t) :=
propext mem_append
lemma mem_append_left {a : α} {l₁ : list α} (l₂ : list α) (h : a ∈ l₁) : a ∈ l₁ ++ l₂ :=
mem_append.2 (or.inl h)
lemma mem_append_right {a : α} (l₁ : list α) {l₂ : list α} (h : a ∈ l₂) : a ∈ l₁ ++ l₂ :=
mem_append.2 (or.inr h)
@[simp] lemma not_bex_nil (p : α → Prop) : ¬ (∃ x ∈ @nil α, p x) :=
λ⟨x, hx, px⟩, hx
@[simp] lemma ball_nil (p : α → Prop) : ∀ x ∈ @nil α, p x :=
λx, false.elim
@[simp] lemma bex_cons (p : α → Prop) (a : α) (l : list α) : (∃ x ∈ (a :: l), p x) ↔ (p a ∨ ∃ x ∈ l, p x) :=
⟨λ⟨x, h, px⟩, by {
simp at h, cases h with h h, {cases h, exact or.inl px}, {exact or.inr ⟨x, h, px⟩}},
λo, o.elim (λpa, ⟨a, mem_cons_self _ _, pa⟩)
(λ⟨x, h, px⟩, ⟨x, mem_cons_of_mem _ h, px⟩)⟩
@[simp] lemma ball_cons (p : α → Prop) (a : α) (l : list α) : (∀ x ∈ (a :: l), p x) ↔ (p a ∧ ∀ x ∈ l, p x) :=
⟨λal, ⟨al a (mem_cons_self _ _), λx h, al x (mem_cons_of_mem _ h)⟩,
λ⟨pa, al⟩ x o, o.elim (λe, e.symm ▸ pa) (al x)⟩
/- list subset -/
protected def subset (l₁ l₂ : list α) := ∀ ⦃a : α⦄, a ∈ l₁ → a ∈ l₂
instance : has_subset (list α) := ⟨list.subset⟩
@[simp] lemma nil_subset (l : list α) : [] ⊆ l :=
λ b i, false.elim (iff.mp (mem_nil_iff b) i)
@[refl, simp] lemma subset.refl (l : list α) : l ⊆ l :=
λ b i, i
@[trans] lemma subset.trans {l₁ l₂ l₃ : list α} (h₁ : l₁ ⊆ l₂) (h₂ : l₂ ⊆ l₃) : l₁ ⊆ l₃ :=
λ b i, h₂ (h₁ i)
@[simp] lemma subset_cons (a : α) (l : list α) : l ⊆ a::l :=
λ b i, or.inr i
lemma subset_of_cons_subset {a : α} {l₁ l₂ : list α} : a::l₁ ⊆ l₂ → l₁ ⊆ l₂ :=
λ s b i, s (mem_cons_of_mem _ i)
lemma cons_subset_cons {l₁ l₂ : list α} (a : α) (s : l₁ ⊆ l₂) : (a::l₁) ⊆ (a::l₂) :=
λ b hin, or.elim (eq_or_mem_of_mem_cons hin)
(λ e : b = a, or.inl e)
(λ i : b ∈ l₁, or.inr (s i))
@[simp] lemma subset_append_left (l₁ l₂ : list α) : l₁ ⊆ l₁++l₂ :=
λ b, mem_append_left _
@[simp] lemma subset_append_right (l₁ l₂ : list α) : l₂ ⊆ l₁++l₂ :=
λ b, mem_append_right _
lemma subset_cons_of_subset (a : α) {l₁ l₂ : list α} : l₁ ⊆ l₂ → l₁ ⊆ (a::l₂) :=
λ (s : l₁ ⊆ l₂) (a : α) (i : a ∈ l₁), or.inr (s i)
theorem eq_nil_of_length_eq_zero {l : list α} : length l = 0 → l = [] :=
by {induction l; intros, refl, contradiction}
theorem ne_nil_of_length_eq_succ {l : list α} : ∀ {n : nat}, length l = succ n → l ≠ [] :=
by induction l; intros; contradiction
@[simp] theorem length_map₂ (f : α → β → γ) (l₁) : ∀ l₂, length (map₂ f l₁ l₂) = min (length l₁) (length l₂) :=
by {induction l₁; intro l₂; cases l₂; simp [*, add_one, min_succ_succ]}
@[simp] theorem length_take : ∀ (i : ℕ) (l : list α), length (take i l) = min i (length l)
| 0 l := by simp
| (succ n) [] := by simp
| (succ n) (a::l) := by simp [*, nat.min_succ_succ, add_one]
theorem length_take_le (n) (l : list α) : length (take n l) ≤ n :=
by simp [min_le_left]
theorem length_remove_nth : ∀ (l : list α) (i : ℕ), i < length l → length (remove_nth l i) = length l - 1
| [] _ h := rfl
| (x::xs) 0 h := by simp [remove_nth, -add_comm]
| (x::xs) (i+1) h := have i < length xs, from lt_of_succ_lt_succ h,
by dsimp [remove_nth]; rw [length_remove_nth xs i this, nat.sub_add_cancel (lt_of_le_of_lt (nat.zero_le _) this)]; refl
@[simp] lemma partition_eq_filter_filter (p : α → Prop) [decidable_pred p] : ∀ (l : list α), partition p l = (filter p l, filter (not ∘ p) l)
| [] := rfl
| (a::l) := by { by_cases pa : p a; simp [partition, filter, pa, partition_eq_filter_filter l] }
/- sublists -/
inductive sublist : list α → list α → Prop
| slnil : sublist [] []
| cons (l₁ l₂ a) : sublist l₁ l₂ → sublist l₁ (a::l₂)
| cons2 (l₁ l₂ a) : sublist l₁ l₂ → sublist (a::l₁) (a::l₂)
infix ` <+ `:50 := sublist
lemma length_le_of_sublist : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → length l₁ ≤ length l₂
| _ _ sublist.slnil := le_refl 0
| _ _ (sublist.cons l₁ l₂ a s) := le_succ_of_le (length_le_of_sublist s)
| _ _ (sublist.cons2 l₁ l₂ a s) := succ_le_succ (length_le_of_sublist s)
/- filter -/
@[simp] theorem filter_nil (p : α → Prop) [h : decidable_pred p] : filter p [] = [] := rfl
@[simp] theorem filter_cons_of_pos {p : α → Prop} [h : decidable_pred p] {a : α} :
∀ l, p a → filter p (a::l) = a :: filter p l :=
λ l pa, if_pos pa
@[simp] theorem filter_cons_of_neg {p : α → Prop} [h : decidable_pred p] {a : α} :
∀ l, ¬ p a → filter p (a::l) = filter p l :=
λ l pa, if_neg pa
@[simp] theorem filter_append {p : α → Prop} [h : decidable_pred p] :
∀ (l₁ l₂ : list α), filter p (l₁++l₂) = filter p l₁ ++ filter p l₂
| [] l₂ := rfl
| (a::l₁) l₂ := by by_cases pa : p a; simp [pa, filter_append]
@[simp] theorem filter_sublist {p : α → Prop} [h : decidable_pred p] : Π (l : list α), filter p l <+ l
| [] := sublist.slnil
| (a::l) := if pa : p a
then by simp [pa]; apply sublist.cons2; apply filter_sublist l
else by simp [pa]; apply sublist.cons; apply filter_sublist l
/- map_accumr -/
section map_accumr
variables {φ : Type w₁} {σ : Type w₂}
-- This runs a function over a list returning the intermediate results and a
-- a final result.
def map_accumr (f : α → σ → σ × β) : list α → σ → (σ × list β)
| [] c := (c, [])
| (y::yr) c :=
let r := map_accumr yr c in
let z := f y r.1 in
(z.1, z.2 :: r.2)
@[simp] theorem length_map_accumr
: ∀ (f : α → σ → σ × β) (x : list α) (s : σ),
length (map_accumr f x s).2 = length x
| f (a::x) s := congr_arg succ (length_map_accumr f x s)
| f [] s := rfl
end map_accumr
section map_accumr₂
variables {φ : Type w₁} {σ : Type w₂}
-- This runs a function over two lists returning the intermediate results and a
-- a final result.
def map_accumr₂ (f : α → β → σ → σ × φ)
: list α → list β → σ → σ × list φ
| [] _ c := (c,[])
| _ [] c := (c,[])
| (x::xr) (y::yr) c :=
let r := map_accumr₂ xr yr c in
let q := f x y r.1 in
(q.1, q.2 :: r.2)
@[simp] theorem length_map_accumr₂ : ∀ (f : α → β → σ → σ × φ) x y c,
length (map_accumr₂ f x y c).2 = min (length x) (length y)
| f (a::x) (b::y) c := calc
succ (length (map_accumr₂ f x y c).2)
= succ (min (length x) (length y))
: congr_arg succ (length_map_accumr₂ f x y c)
... = min (succ (length x)) (succ (length y))
: eq.symm (min_succ_succ (length x) (length y))
| f (a::x) [] c := rfl
| f [] (b::y) c := rfl
| f [] [] c := rfl
end map_accumr₂
end list
|
4b04fc00effe394b29b28d5046761412f643ce41 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/measure_theory/integral/circle_integral.lean | 380ee7fcb3319c0b2746c5b25a22ccdddce970d0 | [
"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 | 28,515 | lean | /-
Copyright (c) 2021 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import measure_theory.integral.interval_integral
import analysis.normed_space.pointwise
import analysis.special_functions.non_integrable
import analysis.analytic.basic
/-!
# Integral over a circle in `ℂ`
In this file we define `∮ z in C(c, R), f z` to be the integral $\oint_{|z-c|=|R|} f(z)\,dz$ and
prove some properties of this integral. We give definition and prove most lemmas for a function
`f : ℂ → E`, where `E` is a complex Banach space with second countable topology. For this reason,
some lemmas use, e.g., `(z - c)⁻¹ • f z` instead of `f z / (z - c)`.
## Main definitions
* `circle_map c R`: the exponential map $θ ↦ c + R e^{θi}$;
* `circle_integrable f c R`: a function `f : ℂ → E` is integrable on the circle with center `c` and
radius `R` if `f ∘ circle_map c R` is integrable on `[0, 2π]`;
* `circle_integral f c R`: the integral $\oint_{|z-c|=|R|} f(z)\,dz$, defined as
$\int_{0}^{2π}(c + Re^{θ i})' f(c+Re^{θ i})\,dθ$;
* `cauchy_power_series f c R`: the power series that is equal to
$\sum_{n=0}^{\infty} \oint_{|z-c|=R} \left(\frac{w-c}{z - c}\right)^n \frac{1}{z-c}f(z)\,dz$ at
`w - c`. The coefficients of this power series depend only on `f ∘ circle_map c R`, and the power
series converges to `f w` if `f` is differentiable on the closed ball `metric.closed_ball c R`
and `w` belongs to the corresponding open ball.
## Main statements
* `has_fpower_series_on_cauchy_integral`: for any circle integrable function `f`, the power series
`cauchy_power_series f c R`, `R > 0`, converges to the Cauchy integral
`(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z` on the open disc `metric.ball c R`;
* `circle_integral.integral_sub_zpow_of_undef`, `circle_integral.integral_sub_zpow_of_ne`, and
`circle_integral.integral_sub_inv_of_mem_ball`: formulas for `∮ z in C(c, R), (z - w) ^ n`,
`n : ℤ`. These lemmas cover the following cases:
- `circle_integral.integral_sub_zpow_of_undef`, `n < 0` and `|w - c| = |R|`: in this case the
function is not integrable, so the integral is equal to its default value (zero);
- `circle_integral.integral_sub_zpow_of_ne`, `n ≠ -1`: in the cases not covered by the previous
lemma, we have `(z - w) ^ n = ((z - w) ^ (n + 1) / (n + 1))'`, thus the integral equals zero;
- `circle_integral.integral_sub_inv_of_mem_ball`, `n = -1`, `|w - c| < R`: in this case the
integral is equal to `2πi`.
The case `n = -1`, `|w -c| > R` is not covered by these lemmas. While it is possible to construct
an explicit primitive, it is easier to apply Cauchy theorem, so we postpone the proof till we have
this theorem (see #10000).
## Notation
- `∮ z in C(c, R), f z`: notation for the integral $\oint_{|z-c|=|R|} f(z)\,dz$, defined as
$\int_{0}^{2π}(c + Re^{θ i})' f(c+Re^{θ i})\,dθ$.
## Tags
integral, circle, Cauchy integral
-/
variables {E : Type*} [measurable_space E] [normed_group E]
noncomputable theory
open_locale real nnreal interval pointwise topological_space
open complex measure_theory topological_space metric function set filter asymptotics
/-!
### `circle_map`, a parametrization of a circle
-/
/-- The exponential map $θ ↦ c + R e^{θi}$. The range of this map is the circle in `ℂ` with center
`c` and radius `|R|`. -/
def circle_map (c : ℂ) (R : ℝ) : ℝ → ℂ := λ θ, c + R * exp (θ * I)
/-- `circle_map` is `2π`-periodic. -/
lemma periodic_circle_map (c : ℂ) (R : ℝ) : periodic (circle_map c R) (2 * π) :=
λ θ, by simp [circle_map, add_mul, exp_periodic _]
@[simp] lemma circle_map_sub_center (c : ℂ) (R : ℝ) (θ : ℝ) :
circle_map c R θ - c = circle_map 0 R θ :=
by simp [circle_map]
@[simp] lemma abs_circle_map_zero (R : ℝ) (θ : ℝ) : abs (circle_map 0 R θ) = |R| :=
by simp [circle_map]
lemma circle_map_mem_sphere' (c : ℂ) (R : ℝ) (θ : ℝ) : circle_map c R θ ∈ sphere c (|R|) :=
by simp
lemma circle_map_mem_sphere (c : ℂ) {R : ℝ} (hR : 0 ≤ R) (θ : ℝ) : circle_map c R θ ∈ sphere c R :=
by simpa only [_root_.abs_of_nonneg hR] using circle_map_mem_sphere' c R θ
lemma circle_map_mem_closed_ball (c : ℂ) {R : ℝ} (hR : 0 ≤ R) (θ : ℝ) :
circle_map c R θ ∈ closed_ball c R :=
sphere_subset_closed_ball (circle_map_mem_sphere c hR θ)
/-- The range of `circle_map c R` is the circle with center `c` and radius `|R|`. -/
@[simp] lemma range_circle_map (c : ℂ) (R : ℝ) : range (circle_map c R) = sphere c (|R|) :=
calc range (circle_map c R) = c +ᵥ R • range (λ θ : ℝ, exp (θ * I)) :
by simp only [← image_vadd, ← image_smul, ← range_comp, vadd_eq_add, circle_map, (∘), real_smul]
... = sphere c (|R|) : by simp [smul_sphere R (0 : ℂ) zero_le_one, real.norm_eq_abs]
/-- The image of `(0, 2π]` under `circle_map c R` is the circle with center `c` and radius `|R|`. -/
@[simp] lemma image_circle_map_Ioc (c : ℂ) (R : ℝ) :
circle_map c R '' Ioc 0 (2 * π) = sphere c (|R|) :=
by rw [← range_circle_map, ← (periodic_circle_map c R).image_Ioc real.two_pi_pos 0, zero_add]
@[simp] lemma circle_map_eq_center_iff {c : ℂ} {R : ℝ} {θ : ℝ} : circle_map c R θ = c ↔ R = 0 :=
by simp [circle_map, exp_ne_zero]
@[simp] lemma circle_map_zero_radius (c : ℂ) : circle_map c 0 = const ℝ c :=
funext $ λ θ, circle_map_eq_center_iff.2 rfl
lemma circle_map_ne_center {c : ℂ} {R : ℝ} (hR : R ≠ 0) {θ : ℝ} : circle_map c R θ ≠ c :=
mt circle_map_eq_center_iff.1 hR
lemma has_deriv_at_circle_map (c : ℂ) (R : ℝ) (θ : ℝ) :
has_deriv_at (circle_map c R) (circle_map 0 R θ * I) θ :=
by simpa only [mul_assoc, one_mul, of_real_clm_apply, circle_map, of_real_one, zero_add]
using ((of_real_clm.has_deriv_at.mul_const I).cexp_real.const_mul (R : ℂ)).const_add c
/- TODO: prove `times_cont_diff ℝ (circle_map c R)`. This needs a version of `times_cont_diff.mul`
for multiplication in a normed algebra over the base field. -/
lemma differentiable_circle_map (c : ℂ) (R : ℝ) :
differentiable ℝ (circle_map c R) :=
λ θ, (has_deriv_at_circle_map c R θ).differentiable_at
@[continuity] lemma continuous_circle_map (c : ℂ) (R : ℝ) : continuous (circle_map c R) :=
(differentiable_circle_map c R).continuous
@[measurability] lemma measurable_circle_map (c : ℂ) (R : ℝ) : measurable (circle_map c R) :=
(continuous_circle_map c R).measurable
@[simp] lemma deriv_circle_map (c : ℂ) (R : ℝ) (θ : ℝ) :
deriv (circle_map c R) θ = circle_map 0 R θ * I :=
(has_deriv_at_circle_map _ _ _).deriv
lemma deriv_circle_map_eq_zero_iff {c : ℂ} {R : ℝ} {θ : ℝ} :
deriv (circle_map c R) θ = 0 ↔ R = 0 :=
by simp [I_ne_zero]
lemma deriv_circle_map_ne_zero {c : ℂ} {R : ℝ} {θ : ℝ} (hR : R ≠ 0) :
deriv (circle_map c R) θ ≠ 0 :=
mt deriv_circle_map_eq_zero_iff.1 hR
lemma lipschitz_with_circle_map (c : ℂ) (R : ℝ) :
lipschitz_with R.nnabs (circle_map c R) :=
lipschitz_with_of_nnnorm_deriv_le (differentiable_circle_map _ _) $ λ θ,
nnreal.coe_le_coe.1 $ by simp
/-!
### Integrability of a function on a circle
-/
/-- We say that a function `f : ℂ → E` is integrable on the circle with center `c` and radius `R` if
the function `f ∘ circle_map c R` is integrable on `[0, 2π]`.
Note that the actual function used in the definition of `circle_integral` is
`(deriv (circle_map c R) θ) • f (circle_map c R θ)`. Integrability of this function is equivalent
to integrability of `f ∘ circle_map c R` whenever `R ≠ 0`. -/
def circle_integrable (f : ℂ → E) (c : ℂ) (R : ℝ) : Prop :=
interval_integrable (λ θ : ℝ, f (circle_map c R θ)) volume 0 (2 * π)
@[simp] lemma circle_integrable_const (a : E) (c : ℂ) (R : ℝ) :
circle_integrable (λ _, a) c R :=
interval_integrable_const
namespace circle_integrable
variables {f g : ℂ → E} {c : ℂ} {R : ℝ}
lemma add [borel_space E] [second_countable_topology E]
(hf : circle_integrable f c R) (hg : circle_integrable g c R) :
circle_integrable (f + g) c R :=
hf.add hg
lemma neg [borel_space E] (hf : circle_integrable f c R) : circle_integrable (-f) c R := hf.neg
/-- The function we actually integrate over `[0, 2π]` in the definition of `circle_integral` is
integrable. -/
lemma out [borel_space E] [normed_space ℂ E] [second_countable_topology E]
(hf : circle_integrable f c R) :
interval_integrable (λ θ : ℝ, deriv (circle_map c R) θ • f (circle_map c R θ)) volume 0 (2 * π) :=
begin
simp only [circle_integrable, deriv_circle_map, interval_integrable_iff] at *,
refine (hf.norm.const_mul (|R|)).mono' _ _,
{ exact (((continuous_circle_map _ _).ae_measurable _).mul_const I).smul hf.ae_measurable },
{ simp [norm_smul] }
end
end circle_integrable
@[simp] lemma circle_integrable_zero_radius {f : ℂ → E} {c : ℂ} : circle_integrable f c 0 :=
by simp [circle_integrable]
lemma circle_integrable_iff [borel_space E] [normed_space ℂ E] [second_countable_topology E]
{f : ℂ → E} {c : ℂ} {R : ℝ} (h₀ : R ≠ 0) : circle_integrable f c R ↔
interval_integrable (λ θ : ℝ, deriv (circle_map c R) θ • f (circle_map c R θ)) volume 0 (2 * π) :=
begin
refine ⟨λ h, h.out, λ h, _⟩,
simp only [circle_integrable, interval_integrable_iff, deriv_circle_map] at h ⊢,
refine (h.norm.const_mul (|R|⁻¹)).mono' _ _,
{ have H : ∀ {θ}, circle_map 0 R θ * I ≠ 0 := λ θ, by simp [h₀, I_ne_zero],
simpa only [inv_smul_smul₀ H]
using (((continuous_circle_map 0 R).ae_measurable _).mul_const I).inv.smul h.ae_measurable },
{ simp [norm_smul, h₀] },
end
lemma continuous_on.circle_integrable' [borel_space E] {f : ℂ → E} {c : ℂ} {R : ℝ}
(hf : continuous_on f (sphere c (|R|))) :
circle_integrable f c R :=
(hf.comp_continuous (continuous_circle_map _ _)
(circle_map_mem_sphere' _ _)).interval_integrable _ _
lemma continuous_on.circle_integrable [borel_space E] {f : ℂ → E} {c : ℂ} {R : ℝ} (hR : 0 ≤ R)
(hf : continuous_on f (sphere c R)) :
circle_integrable f c R :=
continuous_on.circle_integrable' $ (_root_.abs_of_nonneg hR).symm ▸ hf
/-- The function `λ z, (z - w) ^ n`, `n : ℤ`, is circle integrable on the circle with center `c` and
radius `|R|` if and only if `R = 0` or `0 ≤ n`, or `w` does not belong to this circle. -/
@[simp] lemma circle_integrable_sub_zpow_iff {c w : ℂ} {R : ℝ} {n : ℤ} :
circle_integrable (λ z, (z - w) ^ n) c R ↔ R = 0 ∨ 0 ≤ n ∨ w ∉ sphere c (|R|) :=
begin
split,
{ intro h, contrapose! h, rcases h with ⟨hR, hn, hw⟩,
simp only [circle_integrable_iff hR, deriv_circle_map],
rw ← image_circle_map_Ioc at hw, rcases hw with ⟨θ, hθ, rfl⟩,
replace hθ : θ ∈ [0, 2 * π], from Icc_subset_interval (Ioc_subset_Icc_self hθ),
refine not_interval_integrable_of_sub_inv_is_O_punctured _ real.two_pi_pos.ne hθ,
set f : ℝ → ℂ := λ θ', circle_map c R θ' - circle_map c R θ,
have : ∀ᶠ θ' in 𝓝[≠] θ, f θ' ∈ ball (0 : ℂ) 1 \ {0},
{ suffices : ∀ᶠ z in 𝓝[≠] (circle_map c R θ), z - circle_map c R θ ∈ ball (0 : ℂ) 1 \ {0},
from ((differentiable_circle_map c R θ).has_deriv_at.tendsto_punctured_nhds
(deriv_circle_map_ne_zero hR)).eventually this,
filter_upwards [self_mem_nhds_within,
mem_nhds_within_of_mem_nhds (ball_mem_nhds _ zero_lt_one)],
simp [dist_eq, sub_eq_zero] { contextual := tt } },
refine ((((has_deriv_at_circle_map c R θ).is_O_sub).mono inf_le_left).inv_rev
(this.mono (λ θ', and.right))).trans _,
refine is_O.of_bound (|R|)⁻¹ (this.mono $ λ θ' hθ', _),
set x := abs (f θ'),
suffices : x⁻¹ ≤ x ^ n, by simpa [inv_mul_cancel_left₀, mt _root_.abs_eq_zero.1 hR],
have : x ∈ Ioo (0 : ℝ) 1, by simpa [and.comm, x] using hθ',
rw ← zpow_neg_one,
refine (zpow_strict_anti this.1 this.2).le_iff_le.2 (int.lt_add_one_iff.1 _), exact hn },
{ rintro (rfl|H),
exacts [circle_integrable_zero_radius,
((continuous_on_id.sub continuous_on_const).zpow _ $ λ z hz, H.symm.imp_left $
λ hw, sub_ne_zero.2 $ ne_of_mem_of_not_mem hz hw).circle_integrable'] },
end
@[simp] lemma circle_integrable_sub_inv_iff {c w : ℂ} {R : ℝ} :
circle_integrable (λ z, (z - w)⁻¹) c R ↔ R = 0 ∨ w ∉ sphere c (|R|) :=
by { simp only [← zpow_neg_one, circle_integrable_sub_zpow_iff], norm_num }
variables [normed_space ℂ E] [complete_space E] [borel_space E] [second_countable_topology E]
/-- Definition for $\oint_{|z-c|=R} f(z)\,dz$. -/
def circle_integral (f : ℂ → E) (c : ℂ) (R : ℝ) : E :=
∫ (θ : ℝ) in 0..2 * π, deriv (circle_map c R) θ • f (circle_map c R θ)
notation `∮` binders ` in ` `C(` c `, ` R `)` `, ` r:(scoped:60 f, circle_integral f c R) := r
namespace circle_integral
@[simp] lemma integral_radius_zero (f : ℂ → E) (c : ℂ) : ∮ z in C(c, 0), f z = 0 :=
by simp [circle_integral]
lemma integral_congr {f g : ℂ → E} {c : ℂ} {R : ℝ} (hR : 0 ≤ R) (h : eq_on f g (sphere c R)) :
∮ z in C(c, R), f z = ∮ z in C(c, R), g z :=
interval_integral.integral_congr $ λ θ hθ, by simp only [h (circle_map_mem_sphere _ hR _)]
lemma integral_undef {f : ℂ → E} {c : ℂ} {R : ℝ} (hf : ¬circle_integrable f c R) :
∮ z in C(c, R), f z = 0 :=
begin
rcases eq_or_ne R 0 with rfl|h0, { simp },
exact interval_integral.integral_undef (mt (circle_integrable_iff h0).mpr hf)
end
lemma integral_sub {f g : ℂ → E} {c : ℂ} {R : ℝ} (hf : circle_integrable f c R)
(hg : circle_integrable g c R) :
∮ z in C(c, R), f z - g z = (∮ z in C(c, R), f z) - ∮ z in C(c, R), g z :=
by simp only [circle_integral, smul_sub, interval_integral.integral_sub hf.out hg.out]
lemma norm_integral_le_of_norm_le_const' {f : ℂ → E} {c : ℂ} {R C : ℝ}
(hf : ∀ z ∈ sphere c (|R|), ∥f z∥ ≤ C) :
∥∮ z in C(c, R), f z∥ ≤ 2 * π * |R| * C :=
calc ∥∮ z in C(c, R), f z∥ ≤ |R| * C * |2 * π - 0| :
interval_integral.norm_integral_le_of_norm_le_const $ λ θ _,
(calc ∥deriv (circle_map c R) θ • f (circle_map c R θ)∥ = |R| * ∥f (circle_map c R θ)∥ :
by simp [norm_smul]
... ≤ |R| * C : mul_le_mul_of_nonneg_left (hf _ $ circle_map_mem_sphere' _ _ _)
(_root_.abs_nonneg _))
... = 2 * π * |R| * C :
by { rw [sub_zero, _root_.abs_of_pos real.two_pi_pos], ac_refl }
lemma norm_integral_le_of_norm_le_const {f : ℂ → E} {c : ℂ} {R C : ℝ} (hR : 0 ≤ R)
(hf : ∀ z ∈ sphere c R, ∥f z∥ ≤ C) :
∥∮ z in C(c, R), f z∥ ≤ 2 * π * R * C :=
have |R| = R, from _root_.abs_of_nonneg hR,
calc ∥∮ z in C(c, R), f z∥ ≤ 2 * π * |R| * C :
norm_integral_le_of_norm_le_const' $ by rwa this
... = 2 * π * R * C : by rw this
lemma norm_two_pi_I_inv_smul_integral_le_of_norm_le_const {f : ℂ → E} {c : ℂ} {R C : ℝ} (hR : 0 ≤ R)
(hf : ∀ z ∈ sphere c R, ∥f z∥ ≤ C) :
∥(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), f z∥ ≤ R * C :=
begin
have : ∥(2 * π * I : ℂ)⁻¹∥ = (2 * π)⁻¹, by simp [real.pi_pos.le],
rw [norm_smul, this, ← div_eq_inv_mul, div_le_iff real.two_pi_pos, mul_comm (R * C), ← mul_assoc],
exact norm_integral_le_of_norm_le_const hR hf
end
/-- If `f` is continuous on the circle `|z - c| = R`, `R > 0`, the `∥f z∥` is less than or equal to
`C : ℝ` on this circle, and this norm is strictly less than `C` at some point `z` of the circle,
then `∥∮ z in C(c, R), f z∥ < 2 * π * R * C`. -/
lemma norm_integral_lt_of_norm_le_const_of_lt {f : ℂ → E} {c : ℂ} {R C : ℝ} (hR : 0 < R)
(hc : continuous_on f (sphere c R)) (hf : ∀ z ∈ sphere c R, ∥f z∥ ≤ C)
(hlt : ∃ z ∈ sphere c R, ∥f z∥ < C) :
∥∮ z in C(c, R), f z∥ < 2 * π * R * C :=
begin
rw [← _root_.abs_of_pos hR, ← image_circle_map_Ioc] at hlt,
rcases hlt with ⟨_, ⟨θ₀, hmem, rfl⟩, hlt⟩,
calc ∥∮ z in C(c, R), f z∥ ≤ ∫ θ in 0..2 * π, ∥deriv (circle_map c R) θ • f (circle_map c R θ)∥ :
interval_integral.norm_integral_le_integral_norm real.two_pi_pos.le
... < ∫ θ in 0..2 * π, R * C :
begin
simp only [norm_smul, deriv_circle_map, norm_eq_abs, complex.abs_mul, abs_I, mul_one,
abs_circle_map_zero, abs_of_pos hR],
refine interval_integral.integral_lt_integral_of_continuous_on_of_le_of_exists_lt
real.two_pi_pos _ continuous_on_const (λ θ hθ, _) ⟨θ₀, Ioc_subset_Icc_self hmem, _⟩,
{ exact continuous_on_const.mul (hc.comp (continuous_circle_map _ _).continuous_on
(λ θ hθ, circle_map_mem_sphere _ hR.le _)).norm },
{ exact mul_le_mul_of_nonneg_left (hf _ $ circle_map_mem_sphere _ hR.le _) hR.le },
{ exact (mul_lt_mul_left hR).2 hlt }
end
... = 2 * π * R * C : by simp [mul_assoc]
end
@[simp] lemma integral_smul {𝕜 : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E] [smul_comm_class 𝕜 ℂ E]
(a : 𝕜) (f : ℂ → E) (c : ℂ) (R : ℝ) :
∮ z in C(c, R), a • f z = a • ∮ z in C(c, R), f z :=
by simp only [circle_integral, ← smul_comm a, interval_integral.integral_smul]
@[simp] lemma integral_smul_const (f : ℂ → ℂ) (a : E) (c : ℂ) (R : ℝ) :
∮ z in C(c, R), (f z • a) = (∮ z in C(c, R), f z) • a :=
by simp only [circle_integral, interval_integral.integral_smul_const, ← smul_assoc]
@[simp] lemma integral_const_mul (a : ℂ) (f : ℂ → ℂ) (c : ℂ) (R : ℝ) :
∮ z in C(c, R), a * f z = a * ∮ z in C(c, R), f z :=
integral_smul a f c R
@[simp] lemma integral_sub_center_inv (c : ℂ) {R : ℝ} (hR : R ≠ 0) :
∮ z in C(c, R), (z - c)⁻¹ = 2 * π * I :=
by simp [circle_integral, ← div_eq_mul_inv, mul_div_cancel_left _ (circle_map_ne_center hR)]
/-- If `f' : ℂ → E` is a derivative of a complex differentiable function on the circle
`metric.sphere c |R|`, then `∮ z in C(c, R), f' z = 0`. -/
lemma integral_eq_zero_of_has_deriv_within_at' {f f' : ℂ → E} {c : ℂ} {R : ℝ}
(h : ∀ z ∈ sphere c (|R|), has_deriv_within_at f (f' z) (sphere c (|R|)) z) :
∮ z in C(c, R), f' z = 0 :=
begin
by_cases hi : circle_integrable f' c R,
{ rw ← sub_eq_zero.2 ((periodic_circle_map c R).comp f).eq,
refine interval_integral.integral_eq_sub_of_has_deriv_at (λ θ hθ, _) hi.out,
exact (h _ (circle_map_mem_sphere' _ _ _)).scomp_has_deriv_at θ
(differentiable_circle_map _ _ _).has_deriv_at (circle_map_mem_sphere' _ _) },
{ exact integral_undef hi }
end
/-- If `f' : ℂ → E` is a derivative of a complex differentiable function on the circle
`metric.sphere c R`, then `∮ z in C(c, R), f' z = 0`. -/
lemma integral_eq_zero_of_has_deriv_within_at {f f' : ℂ → E} {c : ℂ} {R : ℝ} (hR : 0 ≤ R)
(h : ∀ z ∈ sphere c R, has_deriv_within_at f (f' z) (sphere c R) z) :
∮ z in C(c, R), f' z = 0 :=
integral_eq_zero_of_has_deriv_within_at' $ (_root_.abs_of_nonneg hR).symm.subst h
/-- If `n < 0` and `|w - c| = |R|`, then `(z - w) ^ n` is not circle integrable on the circle with
center `c` and radius `(|R|)`, so the integral `∮ z in C(c, R), (z - w) ^ n` is equal to zero. -/
lemma integral_sub_zpow_of_undef {n : ℤ} {c w : ℂ} {R : ℝ} (hn : n < 0) (hw : w ∈ sphere c (|R|)) :
∮ z in C(c, R), (z - w) ^ n = 0 :=
begin
rcases eq_or_ne R 0 with rfl|h0, { apply integral_radius_zero },
apply integral_undef,
simp [circle_integrable_sub_zpow_iff, *]
end
/-- If `n ≠ -1` is an integer number, then the integral of `(z - w) ^ n` over the circle equals
zero. -/
lemma integral_sub_zpow_of_ne {n : ℤ} (hn : n ≠ -1) (c w : ℂ) (R : ℝ) :
∮ z in C(c, R), (z - w) ^ n = 0 :=
begin
rcases em (w ∈ sphere c (|R|) ∧ n < -1) with ⟨hw, hn⟩|H,
{ exact integral_sub_zpow_of_undef (hn.trans dec_trivial) hw },
push_neg at H,
have hd : ∀ z, (z ≠ w ∨ -1 ≤ n) → has_deriv_at (λ z, (z - w) ^ (n + 1) / (n + 1)) ((z - w) ^ n) z,
{ intros z hne,
convert ((has_deriv_at_zpow (n + 1) _ (hne.imp _ _)).comp z
((has_deriv_at_id z).sub_const w)).div_const _ using 1,
{ have hn' : (n + 1 : ℂ) ≠ 0,
by rwa [ne, ← eq_neg_iff_add_eq_zero, ← int.cast_one, ← int.cast_neg, int.cast_inj],
simp [mul_assoc, mul_div_cancel_left _ hn'] },
exacts [sub_ne_zero.2, neg_le_iff_add_nonneg.1] },
refine integral_eq_zero_of_has_deriv_within_at' (λ z hz, (hd z _).has_deriv_within_at),
exact (ne_or_eq z w).imp_right (λ h, H $ h ▸ hz)
end
end circle_integral
/-- The power series that is equal to
$\sum_{n=0}^{\infty} \oint_{|z-c|=R} \left(\frac{w-c}{z - c}\right)^n \frac{1}{z-c}f(z)\,dz$ at
`w - c`. The coefficients of this power series depend only on `f ∘ circle_map c R`, and the power
series converges to `f w` if `f` is differentiable on the closed ball `metric.closed_ball c R` and
`w` belongs to the corresponding open ball. For any circle integrable function `f`, this power
series converges to the Cauchy integral for `f`. -/
def cauchy_power_series (f : ℂ → E) (c : ℂ) (R : ℝ) :
formal_multilinear_series ℂ ℂ E :=
λ n, continuous_multilinear_map.mk_pi_field ℂ _ $
(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - c)⁻¹ ^ n • (z - c)⁻¹ • f z
lemma cauchy_power_series_apply (f : ℂ → E) (c : ℂ) (R : ℝ) (n : ℕ) (w : ℂ) :
cauchy_power_series f c R n (λ _, w) =
(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (w / (z - c)) ^ n • (z - c)⁻¹ • f z :=
by simp only [cauchy_power_series, continuous_multilinear_map.mk_pi_field_apply, fin.prod_const,
div_eq_mul_inv, mul_pow, mul_smul, circle_integral.integral_smul, ← smul_comm (w ^ n)]
lemma norm_cauchy_power_series_le (f : ℂ → E) (c : ℂ) (R : ℝ) (n : ℕ) :
∥cauchy_power_series f c R n∥ ≤
(2 * π)⁻¹ * (∫ θ : ℝ in 0..2*π, ∥f (circle_map c R θ)∥) * (|R|⁻¹) ^ n :=
calc ∥cauchy_power_series f c R n∥
= (2 * π)⁻¹ * ∥∮ z in C(c, R), (z - c)⁻¹ ^ n • (z - c)⁻¹ • f z∥ :
by simp [cauchy_power_series, norm_smul, real.pi_pos.le]
... ≤ (2 * π)⁻¹ * ∫ θ in 0..2*π, ∥deriv (circle_map c R) θ • (circle_map c R θ - c)⁻¹ ^ n •
(circle_map c R θ - c)⁻¹ • f (circle_map c R θ)∥ :
mul_le_mul_of_nonneg_left (interval_integral.norm_integral_le_integral_norm real.two_pi_pos.le)
(by simp [real.pi_pos.le])
... = (2 * π)⁻¹ * (|R|⁻¹ ^ n * (|R| * (|R|⁻¹ * ∫ (x : ℝ) in 0..2 * π, ∥f (circle_map c R x)∥))) :
by simp [norm_smul, mul_left_comm (|R|)]
... ≤ (2 * π)⁻¹ * (∫ θ : ℝ in 0..2*π, ∥f (circle_map c R θ)∥) * |R|⁻¹ ^ n :
begin
rcases eq_or_ne R 0 with rfl|hR,
{ cases n; simp [real.two_pi_pos] },
{ rw [mul_inv_cancel_left₀, mul_assoc, mul_comm (|R|⁻¹ ^ n)],
rwa [ne.def, _root_.abs_eq_zero] }
end
lemma le_radius_cauchy_power_series (f : ℂ → E) (c : ℂ) (R : ℝ≥0) :
↑R ≤ (cauchy_power_series f c R).radius :=
begin
refine (cauchy_power_series f c R).le_radius_of_bound
((2 * π)⁻¹ * (∫ θ : ℝ in 0..2*π, ∥f (circle_map c R θ)∥)) (λ n, _),
refine (mul_le_mul_of_nonneg_right (norm_cauchy_power_series_le _ _ _ _)
(pow_nonneg R.coe_nonneg _)).trans _,
rw [_root_.abs_of_nonneg R.coe_nonneg],
cases eq_or_ne (R ^ n : ℝ) 0 with hR hR,
{ rw [hR, mul_zero],
exact mul_nonneg (inv_nonneg.2 real.two_pi_pos.le)
(interval_integral.integral_nonneg real.two_pi_pos.le (λ _ _, norm_nonneg _)) },
{ rw [inv_pow₀, inv_mul_cancel_right₀ hR] }
end
/-- For any circle integrable function `f`, the power series `cauchy_power_series f c R` multiplied
by `2πI` converges to the integral `∮ z in C(c, R), (z - w)⁻¹ • f z` on the open disc
`metric.ball c R`. -/
lemma has_sum_two_pi_I_cauchy_power_series_integral {f : ℂ → E} {c : ℂ} {R : ℝ} {w : ℂ}
(hf : circle_integrable f c R) (hw : abs w < R) :
has_sum (λ n : ℕ, ∮ z in C(c, R), (w / (z - c)) ^ n • (z - c)⁻¹ • f z)
(∮ z in C(c, R), (z - (c + w))⁻¹ • f z) :=
begin
have hR : 0 < R := (abs_nonneg w).trans_lt hw,
have hwR : abs w / R ∈ Ico (0 : ℝ) 1,
from ⟨div_nonneg (abs_nonneg w) hR.le, (div_lt_one hR).2 hw⟩,
refine interval_integral.has_sum_integral_of_dominated_convergence
(λ n θ, ∥f (circle_map c R θ)∥ * (abs w / R) ^ n) (λ n, _) (λ n, _) _ _ _,
{ simp only [deriv_circle_map], have := hf.def.1, measurability },
{ simp [norm_smul, abs_of_pos hR, mul_left_comm R, mul_inv_cancel_left₀ hR.ne', mul_comm (∥_∥)] },
{ exact eventually_of_forall (λ _ _, (summable_geometric_of_lt_1 hwR.1 hwR.2).mul_left _) },
{ simpa only [tsum_mul_left, tsum_geometric_of_lt_1 hwR.1 hwR.2]
using hf.norm.mul_continuous_on continuous_on_const },
{ refine eventually_of_forall (λ θ hθ, has_sum.const_smul _),
simp only [smul_smul],
refine has_sum.smul_const _,
have : ∥w / (circle_map c R θ - c)∥ < 1, by simpa [abs_of_pos hR] using hwR.2,
convert (has_sum_geometric_of_norm_lt_1 this).mul_right _,
simp [← sub_sub, ← mul_inv₀, sub_mul, div_mul_cancel _ (circle_map_ne_center hR.ne')] }
end
/-- For any circle integrable function `f`, the power series `cauchy_power_series f c R`, `R > 0`,
converges to the Cauchy integral `(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z` on the open
disc `metric.ball c R`. -/
lemma has_sum_cauchy_power_series_integral {f : ℂ → E} {c : ℂ} {R : ℝ} {w : ℂ}
(hf : circle_integrable f c R) (hw : abs w < R) :
has_sum (λ n, cauchy_power_series f c R n (λ _, w))
((2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - (c + w))⁻¹ • f z) :=
begin
simp only [cauchy_power_series_apply],
exact (has_sum_two_pi_I_cauchy_power_series_integral hf hw).const_smul
end
/-- For any circle integrable function `f`, the power series `cauchy_power_series f c R`, `R > 0`,
converges to the Cauchy integral `(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z` on the open
disc `metric.ball c R`. -/
lemma sum_cauchy_power_series_eq_integral {f : ℂ → E} {c : ℂ} {R : ℝ} {w : ℂ}
(hf : circle_integrable f c R) (hw : abs w < R) :
(cauchy_power_series f c R).sum w =
((2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - (c + w))⁻¹ • f z) :=
(has_sum_cauchy_power_series_integral hf hw).tsum_eq
/-- For any circle integrable function `f`, the power series `cauchy_power_series f c R`, `R > 0`,
converges to the Cauchy integral `(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z` on the open
disc `metric.ball c R`. -/
lemma has_fpower_series_on_cauchy_integral {f : ℂ → E} {c : ℂ} {R : ℝ≥0}
(hf : circle_integrable f c R) (hR : 0 < R) :
has_fpower_series_on_ball
(λ w, (2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z)
(cauchy_power_series f c R) c R :=
{ r_le := le_radius_cauchy_power_series _ _ _,
r_pos := ennreal.coe_pos.2 hR,
has_sum := λ y hy,
begin
refine has_sum_cauchy_power_series_integral hf _,
rw [← norm_eq_abs, ← coe_nnnorm, nnreal.coe_lt_coe, ← ennreal.coe_lt_coe],
exact mem_emetric_ball_zero_iff.1 hy
end }
namespace circle_integral
/-- Integral $\oint_{|z-c|=R} \frac{dz}{z-w}=2πi$ whenever $|w-c|<R$. -/
lemma integral_sub_inv_of_mem_ball {c w : ℂ} {R : ℝ} (hw : w ∈ ball c R) :
∮ z in C(c, R), (z - w)⁻¹ = 2 * π * I :=
begin
have hR : 0 < R := dist_nonneg.trans_lt hw,
suffices H : has_sum (λ n : ℕ, ∮ z in C(c, R), ((w - c) / (z - c)) ^ n * (z - c)⁻¹) (2 * π * I),
{ have A : circle_integrable (λ _, (1 : ℂ)) c R, from continuous_on_const.circle_integrable',
refine (H.unique _).symm,
simpa only [smul_eq_mul, mul_one, add_sub_cancel'_right]
using has_sum_two_pi_I_cauchy_power_series_integral A hw },
have H : ∀ n : ℕ, n ≠ 0 → ∮ z in C(c, R), (z - c) ^ (-n - 1 : ℤ) = 0,
{ refine λ n hn, integral_sub_zpow_of_ne _ _ _ _, simpa },
have : ∮ z in C(c, R), ((w - c) / (z - c)) ^ 0 * (z - c)⁻¹ = 2 * π * I, by simp [hR.ne'],
refine this ▸ has_sum_single _ (λ n hn, _),
simp only [div_eq_mul_inv, mul_pow, integral_const_mul, mul_assoc],
rw [(integral_congr hR.le (λ z hz, _)).trans (H n hn), mul_zero],
rw [← pow_succ', ← zpow_coe_nat, inv_zpow₀, ← zpow_neg₀, int.coe_nat_succ, neg_add,
sub_eq_add_neg _ (1 : ℤ)]
end
end circle_integral
|
39ee315cdf2c07a4e880e1628fb3324a397016cb | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/data/rat/init.lean | e761a4e17465b800cbf0fac57152080f130d30a3 | [
"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,410 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import tactic.ext
import logic.basic
/-!
# The definition of the Rational Numbers
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> https://github.com/leanprover-community/mathlib4/pull/575
> Any changes to this file require a corresponding PR to mathlib4.
## Summary
We define a rational number `q` as a structure `{ num, denom, pos, cop }`, where
- `num` is the numerator of `q`,
- `denom` is the denominator of `q`,
- `pos` is a proof that `denom > 0`, and
- `cop` is a proof `num` and `denom` are coprime.
Basic constructions and results are set up in `data.rat.defs`.
As we transition to Lean 4, these two files can probably be merged again,
as so much of the needed material will be in Std4 anyway.
For now, this split allows us to give the definitions of division rings and fields
without significant theory imports.
The definition of the field structure on `ℚ` will be done in `data.rat.basic` once the
`field` class has been defined.
## Main Definitions
- `rat` is the structure encoding `ℚ`.
## Notations
## Tags
rat, rationals, field, ℚ, numerator, denominator, num, denom
-/
/-- `rat`, or `ℚ`, is the type of rational numbers. It is defined
as the set of pairs ⟨n, d⟩ of integers such that `d` is positive and `n` and
`d` are coprime. This representation is preferred to the quotient
because without periodic reduction, the numerator and denominator can grow
exponentially (for example, adding 1/2 to itself repeatedly). -/
structure rat := mk' ::
(num : ℤ)
(denom : ℕ)
(pos : 0 < denom)
(cop : num.nat_abs.coprime denom)
notation `ℚ` := rat
namespace rat
/-- String representation of a rational numbers, used in `has_repr`, `has_to_string`, and
`has_to_format` instances. -/
protected def repr : ℚ → string
| ⟨n, d, _, _⟩ := if d = 1 then _root_.repr n else
_root_.repr n ++ "/" ++ _root_.repr d
instance : has_repr ℚ := ⟨rat.repr⟩
instance : has_to_string ℚ := ⟨rat.repr⟩
meta instance : has_to_format ℚ := ⟨coe ∘ rat.repr⟩
lemma ext_iff {p q : ℚ} : p = q ↔ p.num = q.num ∧ p.denom = q.denom :=
by { cases p, cases q, simp }
@[ext] lemma ext {p q : ℚ} (hn : p.num = q.num) (hd : p.denom = q.denom) : p = q :=
rat.ext_iff.mpr ⟨hn, hd⟩
end rat
|
8ea831dab083124c83e8073afae44ca1e69ba405 | e09201d437062e1f95e6e5360aab0c9f947901aa | /src/regular/regex.lean | 534778bcdec03d4cfd1d4f10efd8923edec05dba | [] | no_license | VArtem/lean-regular-languages | 34f4b093f28ef2f09ba7e684e642a0f97c901560 | e877243188253d0ac17ccf0ae2da7bf608686ff0 | refs/heads/master | 1,683,590,111,306 | 1,622,307,234,000 | 1,622,307,234,000 | 284,232,653 | 7 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,500 | lean | import data.set.basic
import data.set.finite
import data.set.lattice
import data.list.basic
import tactic
import languages.basic
import languages.star
import data.finset.basic
import data.finset.lattice
open languages
namespace regex
variables {S : Type} [fintype S]
inductive regex (S : Type)
| empty : regex
| eps : regex
| one : S → regex
| union : regex → regex → regex
| append : regex → regex → regex
| star : regex → regex
inductive regex.accepts : regex S → list S → Prop
| eps :
regex.accepts regex.eps []
| one {ch : S} :
regex.accepts (regex.one ch) [ch]
| append {l1 l2 r1 r2} :
regex.accepts r1 l1 →
regex.accepts r2 l2 →
regex.accepts (regex.append r1 r2) (l1 ++ l2)
| union_left {l r1 r2} :
regex.accepts r1 l →
regex.accepts (regex.union r1 r2) l
| union_right {l r1 r2} :
regex.accepts r2 l →
regex.accepts (regex.union r1 r2) l
| star_eps {r} :
regex.accepts (regex.star r) []
| star_append {r l1 l2} :
regex.accepts r l1 →
regex.accepts (regex.star r) l2 →
regex.accepts (regex.star r) (l1 ++ l2)
def regex_lang (l : set (list S)) := ∃ r : regex S, r.accepts = l
lemma regex_empty_not_accepts {w : list S} : ¬ regex.empty.accepts w := λ h, by cases h
theorem regex_empty_is_empty_lang : regex.empty.accepts = (∅ : set (list S)) :=
by {ext, split; rintro ⟨⟩}
theorem empty_is_regex : regex_lang (∅ : set (list S)) :=
⟨regex.empty, regex_empty_is_empty_lang⟩
theorem regex_eps_is_eps_lang : regex.eps.accepts = ({[]} : set (list S)) :=
begin
ext x, split, {
rintro ⟨_⟩,
simp only [set.mem_singleton],
}, {
intro xnil,
convert regex.accepts.eps,
}
end
theorem eps_is_regex : regex_lang ({[]} : set (list S)) :=
⟨regex.eps, regex_eps_is_eps_lang⟩
theorem regex_one_is_one_lang {ch : S} : (regex.one ch).accepts = ({[ch]} : set (list S)) :=
begin
ext x, split, {
rintro ⟨_⟩,
simp only [set.mem_singleton],
}, {
intro xone,
convert regex.accepts.one,
}
end
theorem one_is_regex {c : S} : regex_lang ({[c]} : set (list S)) :=
⟨regex.one c, regex_one_is_one_lang⟩
theorem regex_union_is_lang_union {rl rm : regex S}
: (regex.union rl rm).accepts = (rl.accepts ∪ rm.accepts : set (list S)) :=
begin
apply set.subset.antisymm,
{ rintro x (_ | _),
left, assumption,
right, assumption },
{ rintro x (hleft | hright),
exact regex.accepts.union_left hleft,
exact regex.accepts.union_right hright,
},
end
theorem union_is_regex {L M : set (list S)}: regex_lang L → regex_lang M → regex_lang (L ∪ M) :=
begin
rintro ⟨rl, rfl⟩ ⟨rm, rfl⟩,
use regex.union rl rm,
rw regex_union_is_lang_union,
end
theorem regex_append_is_lang_append {rl rm : regex S} :
(rl.append rm).accepts = (rl.accepts * rm.accepts : set (list S)) :=
begin
apply set.subset.antisymm, {
rintro x h,
rcases h with (_ | _ | ⟨left, right, _, _, hleft, hright⟩ | _ | _ | _ | _),
use [left, right, hleft, hright, rfl],
}, {
rintro _ ⟨ left, right, hleft, hright, rfl ⟩,
exact regex.accepts.append hleft hright,
}
end
theorem append_is_regex {L M : set (list S)}: regex_lang L → regex_lang M → regex_lang (L * M) :=
begin
rintro ⟨rl, rfl⟩ ⟨rm, rfl⟩,
exact ⟨regex.append rl rm, regex_append_is_lang_append⟩,
end
lemma regex_star_iff_list_join {r rs : regex S}
(hrs : rs = r.star) (w : list S) :
rs.accepts w ↔ ∃ l (h : ∀ x, x ∈ l → r.accepts x), w = list.join l :=
begin
split, {
intro rsw,
induction rsw,
repeat {contradiction},
case star_eps : {
use list.nil,
simp,
},
case star_append : _ left right rleft rright ih_left ih_right {
rcases hrs with ⟨_⟩,
rcases ih_right hrs with ⟨l, h, rfl⟩,
refine ⟨left :: l, _, rfl⟩,
rw list.ball_cons,
exact ⟨rleft, h⟩,
},
}, {
subst hrs,
rintro ⟨l, h, rfl⟩,
induction l with head tail ih, {
use regex.accepts.star_eps,
}, {
rw list.ball_cons at h,
refine regex.accepts.star_append (h.1) (ih h.2),
},
},
end
theorem regex_star_is_kleene_star {rl : regex S}
: rl.star.accepts = kleene_star (rl.accepts) :=
begin
ext w,
rw star_eq_list_join,
exact regex_star_iff_list_join rfl w,
end
theorem star_is_regex {L : set (list S)}: regex_lang L → regex_lang (kleene_star L) :=
begin
rintro ⟨rl, rfl⟩,
use regex.star rl,
rw regex_star_is_kleene_star,
end
theorem finset_bUnion_is_regex {α : Type*} [fintype α] {X : set α} {P : α → set (list S)} [decidable_eq α] :
(∀ c ∈ X, regex_lang(P c)) → regex_lang(⋃ c ∈ X, P c) :=
begin
lift X to finset α using set.finite.of_fintype X,
rw finset.set_bUnion_coe,
apply finset.induction_on X, {
simp only [empty_is_regex, finset.not_mem_empty, set.Union_empty, set.Union_neg, not_false_iff, forall_true_iff],
}, {
rintro head tail h_head ih hX,
rw finset.set_bUnion_insert,
refine union_is_regex _ _,
refine hX _ (finset.mem_insert_self _ _),
refine ih (λ c c_tail, hX c (finset.mem_insert_of_mem c_tail)),
},
end
end regex |
cba8fd5d0c8bd8a914c3e3df20e08206ec3cb227 | 86f6f4f8d827a196a32bfc646234b73328aeb306 | /examples/basics/unnamed_937.lean | 43e7522008920b9987f1b5cecc819ed330bbd99c | [] | no_license | jamescheuk91/mathematics_in_lean | 09f1f87d2b0dce53464ff0cbe592c568ff59cf5e | 4452499264e2975bca2f42565c0925506ba5dda3 | refs/heads/master | 1,679,716,410,967 | 1,613,957,947,000 | 1,613,957,947,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 431 | lean | import algebra.group
-- BEGIN
variables {G : Type*} [group G]
#check (mul_assoc : ∀ a b c : G, a * b * c = a * (b * c))
#check (one_mul : ∀ a : G, 1 * a = a)
#check (mul_left_inv : ∀ a : G, a⁻¹ * a = 1)
namespace my_group
theorem mul_right_inv (a : G) : a * a⁻¹ = 1 :=
sorry
theorem mul_one (a : G) : a * 1 = a :=
sorry
theorem mul_inv_rev (a b : G) : (a * b)⁻¹ = b⁻¹ * a ⁻¹ :=
sorry
end my_group
-- END |
67c3d71b3c9826ced14031379083c9164130f1d2 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/ring_theory/finiteness.lean | 64dac0552bfbf2e2559058e09d00be3833ae6263 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,648 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.ring_theory.noetherian
import Mathlib.ring_theory.ideal.operations
import Mathlib.ring_theory.algebra_tower
import Mathlib.PostPort
universes u_1 u_4 u_2 u_5 u_3
namespace Mathlib
/-!
# Finiteness conditions in commutative algebra
In this file we define several notions of finiteness that are common in commutative algebra.
## Main declarations
- `module.finite`, `algebra.finite`, `ring_hom.finite`, `alg_hom.finite`
all of these express that some object is finitely generated *as module* over some base ring.
- `algebra.finite_type`, `ring_hom.finite_type`, `alg_hom.finite_type`
all of these express that some object is finitely generated *as algebra* over some base ring.
-/
/-- A module over a commutative ring is `finite` if it is finitely generated as a module. -/
def module.finite (R : Type u_1) (M : Type u_4) [comm_ring R] [add_comm_group M] [module R M] :=
submodule.fg ⊤
/-- An algebra over a commutative ring is of `finite_type` if it is finitely generated
over the base ring as algebra. -/
def algebra.finite_type (R : Type u_1) (A : Type u_2) [comm_ring R] [comm_ring A] [algebra R A] :=
subalgebra.fg ⊤
/-- An algebra over a commutative ring is `finitely_presented` if it is the quotient of a
polynomial ring in `n` variables by a finitely generated ideal. -/
def algebra.finitely_presented (R : Type u_1) (A : Type u_2) [comm_ring R] [comm_ring A] [algebra R A] :=
∃ (n : ℕ),
∃ (f : alg_hom R (mv_polynomial (fin n) R) A),
function.surjective ⇑f ∧ submodule.fg (ring_hom.ker (alg_hom.to_ring_hom f))
namespace module
theorem finite_def {R : Type u_1} {M : Type u_4} [comm_ring R] [add_comm_group M] [module R M] : finite R M ↔ submodule.fg ⊤ :=
iff.rfl
protected instance is_noetherian.finite (R : Type u_1) (M : Type u_4) [comm_ring R] [add_comm_group M] [module R M] [is_noetherian R M] : finite R M :=
is_noetherian.noetherian ⊤
namespace finite
theorem of_surjective {R : Type u_1} {M : Type u_4} {N : Type u_5} [comm_ring R] [add_comm_group M] [module R M] [add_comm_group N] [module R N] [hM : finite R M] (f : linear_map R M N) (hf : function.surjective ⇑f) : finite R N := sorry
theorem of_injective {R : Type u_1} {M : Type u_4} {N : Type u_5} [comm_ring R] [add_comm_group M] [module R M] [add_comm_group N] [module R N] [is_noetherian R N] (f : linear_map R M N) (hf : function.injective ⇑f) : finite R M :=
fg_of_injective f (iff.mpr linear_map.ker_eq_bot hf)
protected instance self (R : Type u_1) [comm_ring R] : finite R R :=
Exists.intro (singleton 1)
(eq.mpr
(id
((fun (a a_1 : submodule R R) (e_1 : a = a_1) (ᾰ ᾰ_1 : submodule R R) (e_2 : ᾰ = ᾰ_1) =>
congr (congr_arg Eq e_1) e_2)
(submodule.span R ↑(singleton 1)) (submodule.span R (singleton 1))
((fun (s s_1 : set R) (e_2 : s = s_1) => congr_arg (submodule.span R) e_2) (↑(singleton 1)) (singleton 1)
(finset.coe_singleton 1))
⊤ ⊤ (Eq.refl ⊤)))
(eq.mp (Eq.refl (ideal.span 1 = ⊤)) ideal.span_singleton_one))
protected instance prod {R : Type u_1} {M : Type u_4} {N : Type u_5} [comm_ring R] [add_comm_group M] [module R M] [add_comm_group N] [module R N] [hM : finite R M] [hN : finite R N] : finite R (M × N) :=
eq.mpr (id (Eq._oldrec (Eq.refl (finite R (M × N))) (equations._eqn_1 R (M × N))))
(eq.mpr (id (Eq._oldrec (Eq.refl (submodule.fg ⊤)) (Eq.symm submodule.prod_top))) (submodule.fg_prod hM hN))
theorem equiv {R : Type u_1} {M : Type u_4} {N : Type u_5} [comm_ring R] [add_comm_group M] [module R M] [add_comm_group N] [module R N] [hM : finite R M] (e : linear_equiv R M N) : finite R N :=
of_surjective (↑e) (linear_equiv.surjective e)
theorem trans {R : Type u_1} (A : Type u_2) (B : Type u_3) [comm_ring R] [comm_ring A] [algebra R A] [comm_ring B] [algebra R B] [algebra A B] [is_scalar_tower R A B] [hRA : finite R A] [hAB : finite A B] : finite R B := sorry
protected instance finite_type {R : Type u_1} (A : Type u_2) [comm_ring R] [comm_ring A] [algebra R A] [hRA : finite R A] : algebra.finite_type R A :=
subalgebra.fg_of_submodule_fg hRA
end finite
end module
namespace algebra
namespace finite_type
theorem self (R : Type u_1) [comm_ring R] : finite_type R R :=
Exists.intro (singleton 1) (subsingleton.elim (adjoin R ↑(singleton 1)) ⊤)
protected theorem mv_polynomial (R : Type u_1) [comm_ring R] (ι : Type u_2) [fintype ι] : finite_type R (mv_polynomial ι R) := sorry
theorem of_surjective {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A] [algebra R A] [comm_ring B] [algebra R B] (hRA : finite_type R A) (f : alg_hom R A B) (hf : function.surjective ⇑f) : finite_type R B := sorry
theorem equiv {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A] [algebra R A] [comm_ring B] [algebra R B] (hRA : finite_type R A) (e : alg_equiv R A B) : finite_type R B :=
of_surjective hRA (↑e) (alg_equiv.surjective e)
theorem trans {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A] [algebra R A] [comm_ring B] [algebra R B] [algebra A B] [is_scalar_tower R A B] (hRA : finite_type R A) (hAB : finite_type A B) : finite_type R B :=
fg_trans' hRA hAB
/-- An algebra is finitely generated if and only if it is a quotient
of a polynomial ring whose variables are indexed by a finset. -/
theorem iff_quotient_mv_polynomial {R : Type u_1} {A : Type u_2} [comm_ring R] [comm_ring A] [algebra R A] : finite_type R A ↔
∃ (s : finset A), ∃ (f : alg_hom R (mv_polynomial (Subtype fun (x : A) => x ∈ s) R) A), function.surjective ⇑f := sorry
/-- An algebra is finitely generated if and only if it is a quotient
of a polynomial ring whose variables are indexed by a fintype. -/
theorem iff_quotient_mv_polynomial' {R : Type u_1} {A : Type u_2} [comm_ring R] [comm_ring A] [algebra R A] : finite_type R A ↔ ∃ (ι : Type u_2), Exists (∃ (f : alg_hom R (mv_polynomial ι R) A), function.surjective ⇑f) := sorry
/-- An algebra is finitely generated if and only if it is a quotient of a polynomial ring in `n`
variables. -/
theorem iff_quotient_mv_polynomial'' {R : Type u_1} {A : Type u_2} [comm_ring R] [comm_ring A] [algebra R A] : finite_type R A ↔ ∃ (n : ℕ), ∃ (f : alg_hom R (mv_polynomial (fin n) R) A), function.surjective ⇑f := sorry
/-- A finitely presented algebra is of finite type. -/
theorem of_finitely_presented {R : Type u_1} {A : Type u_2} [comm_ring R] [comm_ring A] [algebra R A] : finitely_presented R A → finite_type R A := sorry
end finite_type
namespace finitely_presented
/-- If `e : A ≃ₐ[R] B` and `A` is finitely presented, then so is `B`. -/
theorem equiv (R : Type u_1) (A : Type u_2) (B : Type u_3) [comm_ring R] [comm_ring A] [algebra R A] [comm_ring B] [algebra R B] (hfp : finitely_presented R A) (e : alg_equiv R A B) : finitely_presented R B := sorry
/-- The ring of polynomials in finitely many variables is finitely presented. -/
theorem mv_polynomial (R : Type u_1) [comm_ring R] (ι : Type u_2) [fintype ι] : finitely_presented R (mv_polynomial ι R) := sorry
/-- `R` is finitely presented as `R`-algebra. -/
theorem self (R : Type u_1) [comm_ring R] : finitely_presented R R :=
let hempty : finitely_presented R (mv_polynomial pempty R) := mv_polynomial R pempty;
equiv R (mv_polynomial pempty R) R hempty (mv_polynomial.pempty_alg_equiv R)
end finitely_presented
end algebra
namespace ring_hom
/-- A ring morphism `A →+* B` is `finite` if `B` is finitely generated as `A`-module. -/
def finite {A : Type u_1} {B : Type u_2} [comm_ring A] [comm_ring B] (f : A →+* B) :=
let _inst : algebra A B := to_algebra f;
module.finite A B
/-- A ring morphism `A →+* B` is of `finite_type` if `B` is finitely generated as `A`-algebra. -/
def finite_type {A : Type u_1} {B : Type u_2} [comm_ring A] [comm_ring B] (f : A →+* B) :=
algebra.finite_type A B
namespace finite
theorem id (A : Type u_1) [comm_ring A] : finite (id A) :=
module.finite.self A
theorem of_surjective {A : Type u_1} {B : Type u_2} [comm_ring A] [comm_ring B] (f : A →+* B) (hf : function.surjective ⇑f) : finite f :=
let _inst : algebra A B := to_algebra f;
module.finite.of_surjective (alg_hom.to_linear_map (algebra.of_id A B)) hf
theorem comp {A : Type u_1} {B : Type u_2} {C : Type u_3} [comm_ring A] [comm_ring B] [comm_ring C] {g : B →+* C} {f : A →+* B} (hg : finite g) (hf : finite f) : finite (comp g f) :=
module.finite.trans B C
theorem finite_type {A : Type u_1} {B : Type u_2} [comm_ring A] [comm_ring B] {f : A →+* B} (hf : finite f) : finite_type f :=
module.finite.finite_type B
end finite
namespace finite_type
theorem id (A : Type u_1) [comm_ring A] : finite_type (id A) :=
algebra.finite_type.self A
theorem comp_surjective {A : Type u_1} {B : Type u_2} {C : Type u_3} [comm_ring A] [comm_ring B] [comm_ring C] {f : A →+* B} {g : B →+* C} (hf : finite_type f) (hg : function.surjective ⇑g) : finite_type (comp g f) :=
algebra.finite_type.of_surjective hf
(alg_hom.mk (⇑g) (map_one' g) (map_mul' g) (map_zero' g) (map_add' g) fun (a : A) => rfl) hg
theorem of_surjective {A : Type u_1} {B : Type u_2} [comm_ring A] [comm_ring B] (f : A →+* B) (hf : function.surjective ⇑f) : finite_type f :=
eq.mpr (id (Eq._oldrec (Eq.refl (finite_type f)) (Eq.symm (comp_id f)))) (comp_surjective (id A) hf)
theorem comp {A : Type u_1} {B : Type u_2} {C : Type u_3} [comm_ring A] [comm_ring B] [comm_ring C] {g : B →+* C} {f : A →+* B} (hg : finite_type g) (hf : finite_type f) : finite_type (comp g f) :=
algebra.finite_type.trans hf hg
end finite_type
end ring_hom
namespace alg_hom
/-- An algebra morphism `A →ₐ[R] B` is finite if it is finite as ring morphism.
In other words, if `B` is finitely generated as `A`-module. -/
def finite {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A] [comm_ring B] [algebra R A] [algebra R B] (f : alg_hom R A B) :=
ring_hom.finite (to_ring_hom f)
/-- An algebra morphism `A →ₐ[R] B` is of `finite_type` if it is of finite type as ring morphism.
In other words, if `B` is finitely generated as `A`-algebra. -/
def finite_type {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A] [comm_ring B] [algebra R A] [algebra R B] (f : alg_hom R A B) :=
ring_hom.finite_type (to_ring_hom f)
namespace finite
theorem id (R : Type u_1) (A : Type u_2) [comm_ring R] [comm_ring A] [algebra R A] : finite (alg_hom.id R A) :=
ring_hom.finite.id A
theorem comp {R : Type u_1} {A : Type u_2} {B : Type u_3} {C : Type u_4} [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring C] [algebra R A] [algebra R B] [algebra R C] {g : alg_hom R B C} {f : alg_hom R A B} (hg : finite g) (hf : finite f) : finite (comp g f) :=
ring_hom.finite.comp hg hf
theorem of_surjective {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A] [comm_ring B] [algebra R A] [algebra R B] (f : alg_hom R A B) (hf : function.surjective ⇑f) : finite f :=
ring_hom.finite.of_surjective (↑f) hf
theorem finite_type {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A] [comm_ring B] [algebra R A] [algebra R B] {f : alg_hom R A B} (hf : finite f) : finite_type f :=
ring_hom.finite.finite_type hf
end finite
namespace finite_type
theorem id (R : Type u_1) (A : Type u_2) [comm_ring R] [comm_ring A] [algebra R A] : finite_type (alg_hom.id R A) :=
ring_hom.finite_type.id A
theorem comp {R : Type u_1} {A : Type u_2} {B : Type u_3} {C : Type u_4} [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring C] [algebra R A] [algebra R B] [algebra R C] {g : alg_hom R B C} {f : alg_hom R A B} (hg : finite_type g) (hf : finite_type f) : finite_type (comp g f) :=
ring_hom.finite_type.comp hg hf
theorem comp_surjective {R : Type u_1} {A : Type u_2} {B : Type u_3} {C : Type u_4} [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring C] [algebra R A] [algebra R B] [algebra R C] {f : alg_hom R A B} {g : alg_hom R B C} (hf : finite_type f) (hg : function.surjective ⇑g) : finite_type (comp g f) :=
ring_hom.finite_type.comp_surjective hf hg
theorem of_surjective {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [comm_ring A] [comm_ring B] [algebra R A] [algebra R B] (f : alg_hom R A B) (hf : function.surjective ⇑f) : finite_type f :=
ring_hom.finite_type.of_surjective (↑f) hf
|
852f26b943484c9e657c9422b65fb63e6bd4b965 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/algebraic_topology/topological_simplex.lean | c87ff34527d446c5260bb16166aa41d17c92d10b | [
"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 | 3,132 | lean | /-
Copyright (c) 2021 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Adam Topaz
-/
import algebraic_topology.simplex_category
import topology.category.Top
import topology.instances.nnreal
/-!
# Topological simplices
We define the natural functor from `simplex_category` to `Top` sending `[n]` to the
topological `n`-simplex.
This is used to define `Top.to_sSet` in `algebraic_topology.simpliciaL_set`.
-/
noncomputable theory
namespace simplex_category
open_locale simplicial nnreal big_operators classical
local attribute [instance]
category_theory.concrete_category.has_coe_to_sort
category_theory.concrete_category.has_coe_to_fun
/-- The topological simplex associated to `x : simplex_category`.
This is the object part of the functor `simplex_category.to_Top`. -/
def to_Top_obj (x : simplex_category) := { f : x → ℝ≥0 | ∑ i, f i = 1 }
instance (x : simplex_category) : has_coe_to_fun x.to_Top_obj (λ _, x → ℝ≥0) :=
⟨λ f, (f : x → ℝ≥0)⟩
@[ext]
lemma to_Top_obj.ext {x : simplex_category} (f g : x.to_Top_obj) :
(f : x → ℝ≥0) = g → f = g := subtype.ext
/-- A morphism in `simplex_category` induces a map on the associated topological spaces. -/
def to_Top_map {x y : simplex_category} (f : x ⟶ y) : x.to_Top_obj → y.to_Top_obj :=
λ g, ⟨λ i, ∑ j in (finset.univ.filter (λ k, f k = i)), g j,
begin
dsimp [to_Top_obj],
simp only [finset.filter_congr_decidable, finset.sum_congr],
rw ← finset.sum_bUnion,
convert g.2,
{ rw finset.eq_univ_iff_forall,
intros i,
rw finset.mem_bUnion,
exact ⟨f i, by simp, by simp⟩ },
{ intros i hi j hj h e he,
apply h,
simp only [true_and, finset.inf_eq_inter,
finset.mem_univ, finset.mem_filter, finset.mem_inter] at he,
rw [← he.1, ← he.2] }
end⟩
@[simp]
lemma coe_to_Top_map {x y : simplex_category} (f : x ⟶ y) (g : x.to_Top_obj) (i : y) :
to_Top_map f g i = ∑ j in (finset.univ.filter (λ k, f k = i)), g j := rfl
@[continuity]
lemma continuous_to_Top_map {x y : simplex_category} (f : x ⟶ y) :
continuous (to_Top_map f) :=
continuous_subtype_mk _ $ continuous_pi $ λ i, continuous_finset_sum _ $
λ j hj, continuous.comp (continuous_apply _) continuous_subtype_val
/-- The functor associating the topological `n`-simplex to `[n] : simplex_category`. -/
@[simps]
def to_Top : simplex_category ⥤ Top :=
{ obj := λ x, Top.of x.to_Top_obj,
map := λ x y f, ⟨to_Top_map f⟩,
map_id' := begin
intros x,
ext f i : 3,
change (finset.univ.filter (λ k, k = i)).sum _ = _,
simp [finset.sum_filter]
end,
map_comp' := begin
intros x y z f g,
ext h i : 3,
dsimp,
erw ← finset.sum_bUnion,
apply finset.sum_congr,
{ exact finset.ext (λ j, ⟨λ hj, by simpa using hj, λ hj, by simpa using hj⟩) },
{ tauto },
{ intros j hj k hk h e he,
apply h,
simp only [true_and, finset.inf_eq_inter,
finset.mem_univ, finset.mem_filter, finset.mem_inter] at he,
rw [← he.1, ← he.2] },
end }
end simplex_category
|
763c7f9c9edecb743bc419d90efb725c0d02860b | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/field_theory/subfield.lean | bc35108e59d88a6499d044c84bcac5ae02d39ef0 | [
"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 | 24,297 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors : Anne Baanen
-/
import algebra.algebra.basic
/-!
# Subfields
Let `K` be a field. This file defines the "bundled" subfield type `subfield K`, a type
whose terms correspond to subfields of `K`. This is the preferred way to talk
about subfields in mathlib. Unbundled subfields (`s : set K` and `is_subfield s`)
are not in this file, and they will ultimately be deprecated.
We prove that subfields are a complete lattice, and that you can `map` (pushforward) and
`comap` (pull back) them along ring homomorphisms.
We define the `closure` construction from `set R` to `subfield R`, sending a subset of `R`
to the subfield it generates, and prove that it is a Galois insertion.
## Main definitions
Notation used here:
`(K : Type u) [field K] (L : Type u) [field L] (f g : K →+* L)`
`(A : subfield K) (B : subfield L) (s : set K)`
* `subfield R` : the type of subfields of a ring `R`.
* `instance : complete_lattice (subfield R)` : the complete lattice structure on the subfields.
* `subfield.closure` : subfield closure of a set, i.e., the smallest subfield that includes the set.
* `subfield.gi` : `closure : set M → subfield M` and coercion `coe : subfield M → set M`
form a `galois_insertion`.
* `comap f B : subfield K` : the preimage of a subfield `B` along the ring homomorphism `f`
* `map f A : subfield L` : the image of a subfield `A` along the ring homomorphism `f`.
* `prod A B : subfield (K × L)` : the product of subfields
* `f.field_range : subfield B` : the range of the ring homomorphism `f`.
* `eq_locus_field f g : subfield K` : given ring homomorphisms `f g : K →+* R`,
the subfield of `K` where `f x = g x`
## Implementation notes
A subfield is implemented as a subring which is is closed under `⁻¹`.
Lattice inclusion (e.g. `≤` and `⊓`) is used rather than set notation (`⊆` and `∩`), although
`∈` is defined as membership of a subfield's underlying set.
## Tags
subfield, subfields
-/
open_locale big_operators
universes u v w
variables {K : Type u} {L : Type v} {M : Type w} [field K] [field L] [field M]
set_option old_structure_cmd true
/-- `subfield R` is the type of subfields of `R`. A subfield of `R` is a subset `s` that is a
multiplicative submonoid and an additive subgroup. Note in particular that it shares the
same 0 and 1 as R. -/
structure subfield (K : Type u) [field K] extends subring K :=
(inv_mem' : ∀ x ∈ carrier, x⁻¹ ∈ carrier)
/-- Reinterpret a `subfield` as a `subring`. -/
add_decl_doc subfield.to_subring
namespace subfield
/-- The underlying `add_subgroup` of a subfield. -/
def to_add_subgroup (s : subfield K) : add_subgroup K :=
{ ..s.to_subring.to_add_subgroup }
/-- The underlying submonoid of a subfield. -/
def to_submonoid (s : subfield K) : submonoid K :=
{ ..s.to_subring.to_submonoid }
instance : has_coe (subfield K) (set K) := ⟨subfield.carrier⟩
@[simp] lemma coe_to_subring (s : subfield K) : (s.to_subring : set K) = s :=
rfl
instance : has_coe_to_sort (subfield K) := ⟨Type*, λ S, S.carrier⟩
instance : has_mem K (subfield K) := ⟨λ m S, m ∈ (S : set K)⟩
@[simp] lemma mem_mk (s : set K) (ho hm hz ha hn hi) (x : K) :
x ∈ subfield.mk s ho hm hz ha hn hi ↔ x ∈ s := iff.rfl
@[simp] lemma mem_to_subring (s : subfield K) (x : K) :
x ∈ s.to_subring ↔ x ∈ s := iff.rfl
end subfield
protected lemma subfield.exists {s : subfield K} {p : s → Prop} :
(∃ x : s, p x) ↔ ∃ x ∈ s, p ⟨x, ‹x ∈ s›⟩ :=
set_coe.exists
protected lemma subfield.forall {s : subfield K} {p : s → Prop} :
(∀ x : s, p x) ↔ ∀ x ∈ s, p ⟨x, ‹x ∈ s›⟩ :=
set_coe.forall
/-- A `subring` containing inverses is a `subfield`. -/
def subring.to_subfield (s : subring K) (hinv : ∀ x ∈ s, x⁻¹ ∈ s) : subfield K :=
{ inv_mem' := hinv
..s }
namespace subfield
variables (s t : subfield K)
/-- Two subfields are equal if the underlying subsets are equal. -/
theorem ext' ⦃s t : subfield K⦄ (h : (s : set K) = t) : s = t :=
by { cases s, cases t, congr' }
/-- Two subfields are equal if and only if the underlying subsets are equal. -/
protected theorem ext'_iff {s t : subfield K} : s = t ↔ (s : set K) = t :=
⟨λ h, h ▸ rfl, λ h, ext' h⟩
/-- Two subfields are equal if they have the same elements. -/
@[ext] theorem ext {S T : subfield K} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := ext' $ set.ext h
/-- A subfield contains the ring's 1. -/
theorem one_mem : (1 : K) ∈ s := s.one_mem'
/-- A subfield contains the ring's 0. -/
theorem zero_mem : (0 : K) ∈ s := s.zero_mem'
/-- A subfield is closed under multiplication. -/
theorem mul_mem : ∀ {x y : K}, x ∈ s → y ∈ s → x * y ∈ s := s.mul_mem'
/-- A subfield is closed under addition. -/
theorem add_mem : ∀ {x y : K}, x ∈ s → y ∈ s → x + y ∈ s := s.add_mem'
/-- A subfield is closed under negation. -/
theorem neg_mem : ∀ {x : K}, x ∈ s → -x ∈ s := s.neg_mem'
/-- A subfield is closed under subtraction. -/
theorem sub_mem {x y : K} : x ∈ s → y ∈ s → x - y ∈ s := s.to_subring.sub_mem
/-- A subfield is closed under inverses. -/
theorem inv_mem : ∀ {x : K}, x ∈ s → x⁻¹ ∈ s := s.inv_mem'
/-- A subfield is closed under division. -/
theorem div_mem {x y : K} (hx : x ∈ s) (hy : y ∈ s) : x / y ∈ s :=
by { rw div_eq_mul_inv, exact s.mul_mem hx (s.inv_mem hy) }
/-- Product of a list of elements in a subfield is in the subfield. -/
lemma list_prod_mem {l : list K} : (∀ x ∈ l, x ∈ s) → l.prod ∈ s :=
s.to_submonoid.list_prod_mem
/-- Sum of a list of elements in a subfield is in the subfield. -/
lemma list_sum_mem {l : list K} : (∀ x ∈ l, x ∈ s) → l.sum ∈ s :=
s.to_add_subgroup.list_sum_mem
/-- Product of a multiset of elements in a subfield is in the subfield. -/
lemma multiset_prod_mem (m : multiset K) :
(∀ a ∈ m, a ∈ s) → m.prod ∈ s :=
s.to_submonoid.multiset_prod_mem m
/-- Sum of a multiset of elements in a `subfield` is in the `subfield`. -/
lemma multiset_sum_mem (m : multiset K) :
(∀ a ∈ m, a ∈ s) → m.sum ∈ s :=
s.to_add_subgroup.multiset_sum_mem m
/-- Product of elements of a subfield indexed by a `finset` is in the subfield. -/
lemma prod_mem {ι : Type*} {t : finset ι} {f : ι → K} (h : ∀ c ∈ t, f c ∈ s) :
∏ i in t, f i ∈ s :=
s.to_submonoid.prod_mem h
/-- Sum of elements in a `subfield` indexed by a `finset` is in the `subfield`. -/
lemma sum_mem {ι : Type*} {t : finset ι} {f : ι → K} (h : ∀ c ∈ t, f c ∈ s) :
∑ i in t, f i ∈ s :=
s.to_add_subgroup.sum_mem h
lemma pow_mem {x : K} (hx : x ∈ s) (n : ℕ) : x^n ∈ s := s.to_submonoid.pow_mem hx n
lemma gsmul_mem {x : K} (hx : x ∈ s) (n : ℤ) :
n •ℤ x ∈ s := s.to_add_subgroup.gsmul_mem hx n
lemma coe_int_mem (n : ℤ) : (n : K) ∈ s :=
by simp only [← gsmul_one, gsmul_mem, one_mem]
/-- A subfield inherits a field structure -/
instance to_field : field s :=
{ inv := λ x, ⟨x⁻¹, s.inv_mem x.2⟩,
inv_zero := subtype.ext inv_zero,
mul_inv_cancel := λ x hx, subtype.ext (mul_inv_cancel (mt s.to_subring.coe_eq_zero_iff.mp hx)),
exists_pair_ne := ⟨⟨0, s.zero_mem⟩, ⟨1, s.one_mem⟩, mt subtype.mk_eq_mk.mp zero_ne_one⟩,
..subring.subring.domain s.to_subring }
@[simp, norm_cast] lemma coe_add (x y : s) : (↑(x + y) : K) = ↑x + ↑y := rfl
@[simp, norm_cast] lemma coe_neg (x : s) : (↑(-x) : K) = -↑x := rfl
@[simp, norm_cast] lemma coe_mul (x y : s) : (↑(x * y) : K) = ↑x * ↑y := rfl
@[simp, norm_cast] lemma coe_inv (x : s) : (↑(x⁻¹) : K) = (↑x)⁻¹ := rfl
@[simp, norm_cast] lemma coe_zero : ((0 : s) : K) = 0 := rfl
@[simp, norm_cast] lemma coe_one : ((1 : s) : K) = 1 := rfl
/-- The embedding from a subfield of the field `K` to `K`. -/
def subtype (s : subfield K) : s →+* K :=
{ to_fun := coe,
.. s.to_submonoid.subtype, .. s.to_add_subgroup.subtype }
instance to_algebra : algebra s K := ring_hom.to_algebra s.subtype
@[simp] theorem coe_subtype : ⇑s.subtype = coe := rfl
/-! # Partial order -/
instance : partial_order (subfield K) :=
{ le := λ s t, ∀ ⦃x⦄, x ∈ s → x ∈ t,
.. partial_order.lift (coe : subfield K → set K) ext' }
variables {s t}
lemma le_def : s ≤ t ↔ ∀ ⦃x : K⦄, x ∈ s → x ∈ t := iff.rfl
@[simp, norm_cast] lemma coe_subset_coe : (s : set K) ⊆ t ↔ s ≤ t := iff.rfl
@[simp, norm_cast] lemma coe_ssubset_coe : (s : set K) ⊂ t ↔ s < t := iff.rfl
@[simp, norm_cast]
lemma mem_coe {m : K} : m ∈ (s : set K) ↔ m ∈ s := iff.rfl
variables (s t)
@[simp, norm_cast]
lemma coe_coe (s : subfield K) : ↥(s : set K) = s := rfl
@[simp] lemma mem_to_submonoid {s : subfield K} {x : K} : x ∈ s.to_submonoid ↔ x ∈ s := iff.rfl
@[simp] lemma coe_to_submonoid : (s.to_submonoid : set K) = s := rfl
@[simp] lemma mem_to_add_subgroup {s : subfield K} {x : K} :
x ∈ s.to_add_subgroup ↔ x ∈ s := iff.rfl
@[simp] lemma coe_to_add_subgroup : (s.to_add_subgroup : set K) = s := rfl
/-! # top -/
/-- The subfield of `K` containing all elements of `K`. -/
instance : has_top (subfield K) :=
⟨{ inv_mem' := λ x _, subring.mem_top x, .. (⊤ : subring K)}⟩
instance : inhabited (subfield K) := ⟨⊤⟩
@[simp] lemma mem_top (x : K) : x ∈ (⊤ : subfield K) := set.mem_univ x
@[simp] lemma coe_top : ((⊤ : subfield K) : set K) = set.univ := rfl
/-! # comap -/
variables (f : K →+* L)
/-- The preimage of a subfield along a ring homomorphism is a subfield. -/
def comap (s : subfield L) : subfield K :=
{ inv_mem' := λ x hx, show f (x⁻¹) ∈ s, by { rw f.map_inv, exact s.inv_mem hx },
.. s.to_subring.comap f }
@[simp] lemma coe_comap (s : subfield L) : (s.comap f : set K) = f ⁻¹' s := rfl
@[simp]
lemma mem_comap {s : subfield L} {f : K →+* L} {x : K} : x ∈ s.comap f ↔ f x ∈ s := iff.rfl
lemma comap_comap (s : subfield M) (g : L →+* M) (f : K →+* L) :
(s.comap g).comap f = s.comap (g.comp f) :=
rfl
/-! # map -/
/-- The image of a subfield along a ring homomorphism is a subfield. -/
def map (s : subfield K) : subfield L :=
{ inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, s.inv_mem hx, f.map_inv x⟩ },
.. s.to_subring.map f }
@[simp] lemma coe_map : (s.map f : set L) = f '' s := rfl
@[simp] lemma mem_map {f : K →+* L} {s : subfield K} {y : L} :
y ∈ s.map f ↔ ∃ x ∈ s, f x = y :=
set.mem_image_iff_bex
lemma map_map (g : L →+* M) (f : K →+* L) : (s.map f).map g = s.map (g.comp f) :=
ext' $ set.image_image _ _ _
lemma map_le_iff_le_comap {f : K →+* L} {s : subfield K} {t : subfield L} :
s.map f ≤ t ↔ s ≤ t.comap f :=
set.image_subset_iff
lemma gc_map_comap (f : K →+* L) : galois_connection (map f) (comap f) :=
λ S T, map_le_iff_le_comap
end subfield
namespace ring_hom
variables (g : L →+* M) (f : K →+* L)
/-! # range -/
/-- The range of a ring homomorphism, as a subfield of the target. -/
def field_range : subfield L := (⊤ : subfield K).map f
@[simp] lemma coe_field_range : (f.field_range : set L) = set.range f := set.image_univ
@[simp] lemma mem_field_range {f : K →+* L} {y : L} : y ∈ f.range ↔ ∃ x, f x = y :=
by simp [range]
lemma map_field_range : f.field_range.map g = (g.comp f).field_range :=
(⊤ : subfield K).map_map g f
end ring_hom
namespace subfield
/-! # inf -/
/-- The inf of two subfields is their intersection. -/
instance : has_inf (subfield K) :=
⟨λ s t,
{ inv_mem' := λ x hx, subring.mem_inf.mpr
⟨s.inv_mem (subring.mem_inf.mp hx).1,
t.inv_mem (subring.mem_inf.mp hx).2⟩,
.. s.to_subring ⊓ t.to_subring }⟩
@[simp] lemma coe_inf (p p' : subfield K) : ((p ⊓ p' : subfield K) : set K) = p ∩ p' := rfl
@[simp] lemma mem_inf {p p' : subfield K} {x : K} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl
instance : has_Inf (subfield K) :=
⟨λ S,
{ inv_mem' := begin
rintros x hx,
apply subring.mem_Inf.mpr,
rintro _ ⟨p, p_mem, rfl⟩,
exact p.inv_mem (subring.mem_Inf.mp hx p.to_subring ⟨p, p_mem, rfl⟩),
end,
.. Inf (subfield.to_subring '' S) }⟩
@[simp, norm_cast] lemma coe_Inf (S : set (subfield K)) :
((Inf S : subfield K) : set K) = ⋂ s ∈ S, ↑s :=
show ((Inf (subfield.to_subring '' S) : subring K) : set K) = ⋂ s ∈ S, ↑s,
begin
ext x,
rw [subring.coe_Inf, set.mem_Inter, set.mem_Inter],
exact ⟨λ h s s' ⟨s_mem, s'_eq⟩, h s.to_subring _ ⟨⟨s, s_mem, rfl⟩, s'_eq⟩,
λ h s s' ⟨⟨s'', s''_mem, s_eq⟩, (s'_eq : ↑s = s')⟩,
h s'' _ ⟨s''_mem, by simp [←s_eq, ← s'_eq]⟩⟩
end
lemma mem_Inf {S : set (subfield K)} {x : K} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p :=
subring.mem_Inf.trans
⟨λ h p hp, h p.to_subring ⟨p, hp, rfl⟩,
λ h p ⟨p', hp', p_eq⟩, p_eq ▸ h p' hp'⟩
@[simp] lemma Inf_to_subring (s : set (subfield K)) :
(Inf s).to_subring = ⨅ t ∈ s, subfield.to_subring t :=
begin
ext x,
rw [mem_to_subring, mem_Inf],
erw subring.mem_Inf,
exact ⟨λ h p ⟨p', hp⟩, hp ▸ subring.mem_Inf.mpr (λ p ⟨hp', hp⟩, hp ▸ h _ hp'),
λ h p hp, h p.to_subring ⟨p, subring.ext (λ x,
⟨λ hx, subring.mem_Inf.mp hx _ ⟨hp, rfl⟩,
λ hx, subring.mem_Inf.mpr (λ p' ⟨hp, p'_eq⟩, p'_eq ▸ hx)⟩)⟩⟩
end
lemma is_glb_Inf (S : set (subfield K)) : is_glb S (Inf S) :=
begin
refine is_glb.of_image (λ s t, show (s : set K) ≤ t ↔ s ≤ t, from coe_subset_coe) _,
convert is_glb_binfi,
exact coe_Inf _
end
/-- Subfields of a ring form a complete lattice. -/
instance : complete_lattice (subfield K) :=
{ top := ⊤,
le_top := λ s x hx, trivial,
inf := (⊓),
inf_le_left := λ s t x, and.left,
inf_le_right := λ s t x, and.right,
le_inf := λ s t₁ t₂ h₁ h₂ x hx, ⟨h₁ hx, h₂ hx⟩,
.. complete_lattice_of_Inf (subfield K) is_glb_Inf }
/-! # subfield closure of a subset -/
/-- The `subfield` generated by a set. -/
def closure (s : set K) : subfield K :=
{ carrier := { (x / y) | (x ∈ subring.closure s) (y ∈ subring.closure s) },
zero_mem' := ⟨0, subring.zero_mem _, 1, subring.one_mem _, div_one _⟩,
one_mem' := ⟨1, subring.one_mem _, 1, subring.one_mem _, div_one _⟩,
neg_mem' := λ x ⟨y, hy, z, hz, x_eq⟩, ⟨-y, subring.neg_mem _ hy, z, hz, x_eq ▸ neg_div _ _⟩,
inv_mem' := λ x ⟨y, hy, z, hz, x_eq⟩, ⟨z, hz, y, hy, x_eq ▸ inv_div.symm⟩,
add_mem' := λ x y x_mem y_mem, begin
obtain ⟨nx, hnx, dx, hdx, rfl⟩ := id x_mem,
obtain ⟨ny, hny, dy, hdy, rfl⟩ := id y_mem,
by_cases hx0 : dx = 0, { rwa [hx0, div_zero, zero_add] },
by_cases hy0 : dy = 0, { rwa [hy0, div_zero, add_zero] },
exact ⟨nx * dy + dx * ny,
subring.add_mem _ (subring.mul_mem _ hnx hdy) (subring.mul_mem _ hdx hny),
dx * dy, subring.mul_mem _ hdx hdy,
(div_add_div nx ny hx0 hy0).symm⟩
end,
mul_mem' := λ x y x_mem y_mem, begin
obtain ⟨nx, hnx, dx, hdx, rfl⟩ := id x_mem,
obtain ⟨ny, hny, dy, hdy, rfl⟩ := id y_mem,
exact ⟨nx * ny, subring.mul_mem _ hnx hny,
dx * dy, subring.mul_mem _ hdx hdy,
(div_mul_div _ _ _ _).symm⟩
end }
lemma mem_closure_iff {s : set K} {x} :
x ∈ closure s ↔ ∃ (y ∈ subring.closure s) (z ∈ subring.closure s), y / z = x := iff.rfl
lemma subring_closure_le (s : set K) : subring.closure s ≤ (closure s).to_subring :=
λ x hx, ⟨x, hx, 1, subring.one_mem _, div_one x⟩
/-- The subfield generated by a set includes the set. -/
@[simp] lemma subset_closure {s : set K} : s ⊆ closure s :=
set.subset.trans subring.subset_closure (subring_closure_le s)
lemma mem_closure {x : K} {s : set K} : x ∈ closure s ↔ ∀ S : subfield K, s ⊆ S → x ∈ S :=
⟨λ ⟨y, hy, z, hz, x_eq⟩ t le, x_eq ▸
t.div_mem
(subring.mem_closure.mp hy t.to_subring le)
(subring.mem_closure.mp hz t.to_subring le),
λ h, h (closure s) subset_closure⟩
/-- A subfield `t` includes `closure s` if and only if it includes `s`. -/
@[simp]
lemma closure_le {s : set K} {t : subfield K} : closure s ≤ t ↔ s ⊆ t :=
⟨set.subset.trans subset_closure, λ h x hx, mem_closure.mp hx t h⟩
/-- Subfield closure of a set is monotone in its argument: if `s ⊆ t`,
then `closure s ≤ closure t`. -/
lemma closure_mono ⦃s t : set K⦄ (h : s ⊆ t) : closure s ≤ closure t :=
closure_le.2 $ set.subset.trans h subset_closure
lemma closure_eq_of_le {s : set K} {t : subfield K} (h₁ : s ⊆ t) (h₂ : t ≤ closure s) :
closure s = t :=
le_antisymm (closure_le.2 h₁) h₂
/-- An induction principle for closure membership. If `p` holds for `1`, and all elements
of `s`, and is preserved under addition, negation, and multiplication, then `p` holds for all elements
of the closure of `s`. -/
@[elab_as_eliminator]
lemma closure_induction {s : set K} {p : K → Prop} {x} (h : x ∈ closure s)
(Hs : ∀ x ∈ s, p x) (H1 : p 1)
(Hadd : ∀ x y, p x → p y → p (x + y))
(Hneg : ∀ x, p x → p (-x))
(Hinv : ∀ x, p x → p (x⁻¹))
(Hmul : ∀ x y, p x → p y → p (x * y)) : p x :=
(@closure_le _ _ _ ⟨p, H1, Hmul,
@add_neg_self K _ 1 ▸ Hadd _ _ H1 (Hneg _ H1), Hadd, Hneg, Hinv⟩).2 Hs h
variable (K)
/-- `closure` forms a Galois insertion with the coercion to set. -/
protected def gi : galois_insertion (@closure K _) coe :=
{ choice := λ s _, closure s,
gc := λ s t, closure_le,
le_l_u := λ s, subset_closure,
choice_eq := λ s h, rfl }
variable {K}
/-- Closure of a subfield `S` equals `S`. -/
lemma closure_eq (s : subfield K) : closure (s : set K) = s := (subfield.gi K).l_u_eq s
@[simp] lemma closure_empty : closure (∅ : set K) = ⊥ := (subfield.gi K).gc.l_bot
@[simp] lemma closure_univ : closure (set.univ : set K) = ⊤ := @coe_top K _ ▸ closure_eq ⊤
lemma closure_union (s t : set K) : closure (s ∪ t) = closure s ⊔ closure t :=
(subfield.gi K).gc.l_sup
lemma closure_Union {ι} (s : ι → set K) : closure (⋃ i, s i) = ⨆ i, closure (s i) :=
(subfield.gi K).gc.l_supr
lemma closure_sUnion (s : set (set K)) : closure (⋃₀ s) = ⨆ t ∈ s, closure t :=
(subfield.gi K).gc.l_Sup
lemma map_sup (s t : subfield K) (f : K →+* L) : (s ⊔ t).map f = s.map f ⊔ t.map f :=
(gc_map_comap f).l_sup
lemma map_supr {ι : Sort*} (f : K →+* L) (s : ι → subfield K) :
(supr s).map f = ⨆ i, (s i).map f :=
(gc_map_comap f).l_supr
lemma comap_inf (s t : subfield L) (f : K →+* L) : (s ⊓ t).comap f = s.comap f ⊓ t.comap f :=
(gc_map_comap f).u_inf
lemma comap_infi {ι : Sort*} (f : K →+* L) (s : ι → subfield L) :
(infi s).comap f = ⨅ i, (s i).comap f :=
(gc_map_comap f).u_infi
@[simp] lemma map_bot (f : K →+* L) : (⊥ : subfield K).map f = ⊥ :=
(gc_map_comap f).l_bot
@[simp] lemma comap_top (f : K →+* L) : (⊤ : subfield L).comap f = ⊤ :=
(gc_map_comap f).u_top
/-- The underlying set of a non-empty directed Sup of subfields is just a union of the subfields.
Note that this fails without the directedness assumption (the union of two subfields is
typically not a subfield) -/
lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subfield K} (hS : directed (≤) S)
{x : K} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i :=
begin
refine ⟨_, λ ⟨i, hi⟩, (le_def.1 $ le_supr S i) hi⟩,
suffices : x ∈ closure (⋃ i, (S i : set K)) → ∃ i, x ∈ S i,
by simpa only [closure_Union, closure_eq],
refine λ hx, closure_induction hx (λ x, set.mem_Union.mp) _ _ _ _ _,
{ exact hι.elim (λ i, ⟨i, (S i).one_mem⟩) },
{ rintros x y ⟨i, hi⟩ ⟨j, hj⟩,
obtain ⟨k, hki, hkj⟩ := hS i j,
exact ⟨k, (S k).add_mem (hki hi) (hkj hj)⟩ },
{ rintros x ⟨i, hi⟩,
exact ⟨i, (S i).neg_mem hi⟩ },
{ rintros x ⟨i, hi⟩,
exact ⟨i, (S i).inv_mem hi⟩ },
{ rintros x y ⟨i, hi⟩ ⟨j, hj⟩,
obtain ⟨k, hki, hkj⟩ := hS i j,
exact ⟨k, (S k).mul_mem (hki hi) (hkj hj)⟩ }
end
lemma coe_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subfield K} (hS : directed (≤) S) :
((⨆ i, S i : subfield K) : set K) = ⋃ i, ↑(S i) :=
set.ext $ λ x, by simp [mem_supr_of_directed hS]
lemma mem_Sup_of_directed_on {S : set (subfield K)} (Sne : S.nonempty)
(hS : directed_on (≤) S) {x : K} :
x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s :=
begin
haveI : nonempty S := Sne.to_subtype,
simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk]
end
lemma coe_Sup_of_directed_on {S : set (subfield K)} (Sne : S.nonempty) (hS : directed_on (≤) S) :
(↑(Sup S) : set K) = ⋃ s ∈ S, ↑s :=
set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS]
end subfield
namespace ring_hom
variables {s : subfield K}
open subfield
/-- Restrict the codomain of a ring homomorphism to a subfield that includes the range. -/
def cod_restrict_field (f : K →+* L)
(s : subfield L) (h : ∀ x, f x ∈ s) : K →+* s :=
{ to_fun := λ x, ⟨f x, h x⟩,
map_add' := λ x y, subtype.eq $ f.map_add x y,
map_zero' := subtype.eq f.map_zero,
map_mul' := λ x y, subtype.eq $ f.map_mul x y,
map_one' := subtype.eq f.map_one }
/-- Restriction of a ring homomorphism to a subfield of the domain. -/
def restrict_field (f : K →+* L) (s : subfield K) : s →+* L := f.comp s.subtype
@[simp] lemma restrict_field_apply (f : K →+* L) (x : s) : f.restrict_field s x = f x := rfl
/-- Restriction of a ring homomorphism to its range interpreted as a subfield. -/
def range_restrict_field (f : K →+* L) : K →+* f.range :=
f.cod_restrict' f.range $ λ x, ⟨x, subfield.mem_top x, rfl⟩
@[simp] lemma coe_range_restrict_field (f : K →+* L) (x : K) :
(f.range_restrict_field x : L) = f x := rfl
/-- The subfield of elements `x : R` such that `f x = g x`, i.e.,
the equalizer of f and g as a subfield of R -/
def eq_locus_field (f g : K →+* L) : subfield K :=
{ inv_mem' := λ x (hx : f x = g x), show f x⁻¹ = g x⁻¹, by rw [f.map_inv, g.map_inv, hx],
carrier := {x | f x = g x}, .. (f : K →+* L).eq_locus g }
/-- If two ring homomorphisms are equal on a set, then they are equal on its subfield closure. -/
lemma eq_on_field_closure {f g : K →+* L} {s : set K} (h : set.eq_on f g s) :
set.eq_on f g (closure s) :=
show closure s ≤ f.eq_locus_field g, from closure_le.2 h
lemma eq_of_eq_on_subfield_top {f g : K →+* L} (h : set.eq_on f g (⊤ : subfield K)) :
f = g :=
ext $ λ x, h trivial
lemma eq_of_eq_on_of_field_closure_eq_top {s : set K} (hs : closure s = ⊤) {f g : K →+* L}
(h : s.eq_on f g) : f = g :=
eq_of_eq_on_subfield_top $ hs ▸ eq_on_field_closure h
lemma field_closure_preimage_le (f : K →+* L) (s : set L) :
closure (f ⁻¹' s) ≤ (closure s).comap f :=
closure_le.2 $ λ x hx, mem_coe.2 $ mem_comap.2 $ subset_closure hx
/-- The image under a ring homomorphism of the subfield generated by a set equals
the subfield generated by the image of the set. -/
lemma map_field_closure (f : K →+* L) (s : set K) :
(closure s).map f = closure (f '' s) :=
le_antisymm
(map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _)
(field_closure_preimage_le _ _))
(closure_le.2 $ set.image_subset _ subset_closure)
end ring_hom
namespace subfield
open ring_hom
/-- The ring homomorphism associated to an inclusion of subfields. -/
def inclusion {S T : subfield K} (h : S ≤ T) : S →+* T :=
S.subtype.cod_restrict_field _ (λ x, h x.2)
@[simp] lemma field_range_subtype (s : subfield K) : s.subtype.field_range = s :=
ext' $ (coe_srange _).trans subtype.range_coe
end subfield
namespace ring_equiv
variables {s t : subfield K}
/-- Makes the identity isomorphism from a proof two subfields of a multiplicative
monoid are equal. -/
def subfield_congr (h : s = t) : s ≃+* t :=
{ map_mul' := λ _ _, rfl, map_add' := λ _ _, rfl, ..equiv.set_congr $ subfield.ext'_iff.1 h }
end ring_equiv
namespace subfield
variables {s : set K}
lemma closure_preimage_le (f : K →+* L) (s : set L) :
closure (f ⁻¹' s) ≤ (closure s).comap f :=
closure_le.2 $ λ x hx, mem_coe.2 $ mem_comap.2 $ subset_closure hx
end subfield
|
8b5adee1d7d03e6d998d6319cfc7566b3f628f4d | 0c1546a496eccfb56620165cad015f88d56190c5 | /library/tools/debugger/cli.lean | fa4831f77116456a5f3b75dc017e6c04ef40feec | [
"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 | 8,345 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
Simple command line interface for debugging Lean programs and tactics.
-/
import tools.debugger.util
namespace debugger
inductive mode
| init | step | run | done
instance : decidable_eq mode :=
by tactic.mk_dec_eq_instance
structure state :=
(md : mode)
(csz : nat)
(fn_bps : list name)
(active_bps : list (nat × name))
def init_state : state :=
{ md := mode.init, csz := 0,
fn_bps := [], active_bps := [] }
meta def show_help : vm unit :=
do
vm.put_str "exit - stop debugger\n",
vm.put_str "help - display this message\n",
vm.put_str "run - continue execution\n",
vm.put_str "step - execute until another function in on the top of the stack\n",
vm.put_str "stack trace\n",
vm.put_str " up - move up in the stack trace\n",
vm.put_str " down - move down in the stack trace\n",
vm.put_str " vars - display variables in the current stack frame\n",
vm.put_str " stack - display all functions on the call stack\n",
vm.put_str " print var - display the value of variable named 'var' in the current stack frame\n",
vm.put_str " pidx idx - display the value of variable at position #idx in the current stack frame\n",
vm.put_str "breakpoints\n",
vm.put_str " break fn - add breakpoint for fn\n",
vm.put_str " rbreak fn - remove breakpoint\n",
vm.put_str " bs - show breakpoints\n"
meta def add_breakpoint (s : state) (args : list string) : vm state :=
match args with
| [arg] := do
fn ← return $ to_qualified_name arg,
ok ← is_valid_fn_prefix fn,
if ok then
return {s with fn_bps := fn :: list.filter (λ fn', fn ≠ fn') s^.fn_bps}
else
vm.put_str "invalid 'break' command, given name is not the prefix for any function\n" >>
return s
| _ :=
vm.put_str "invalid 'break <fn>' command, incorrect number of arguments\n" >>
return s
end
meta def remove_breakpoint (s : state) (args : list string) : vm state :=
match args with
| [arg] := do
fn ← return $ to_qualified_name arg,
return {s with fn_bps := list.filter (λ fn', fn ≠ fn') s^.fn_bps}
| _ :=
vm.put_str "invalid 'rbreak <fn>' command, incorrect number of arguments\n" >>
return s
end
meta def show_breakpoints : list name → vm unit
| [] := return ()
| (fn::fns) := do
vm.put_str " ",
vm.put_str fn^.to_string,
vm.put_str "\n",
show_breakpoints fns
meta def up_cmd (frame : nat) : vm nat :=
if frame = 0 then return 0
else show_frame (frame - 1) >> return (frame - 1)
meta def down_cmd (frame : nat) : vm nat :=
do sz ← vm.call_stack_size,
if frame >= sz - 1 then return frame
else show_frame (frame + 1) >> return (frame + 1)
meta def pidx_cmd : nat → list string → vm unit
| frame [arg] := do
idx ← return $ arg^.to_nat,
sz ← vm.stack_size,
(bp, ep) ← vm.call_stack_var_range frame,
if bp + idx >= ep then
vm.put_str "invalid 'pidx <idx>' command, index out of bounds\n"
else do
v ← vm.pp_stack_obj (bp+idx),
(n, t) ← vm.stack_obj_info (bp+idx),
opts ← vm.get_options,
vm.put_str n^.to_string,
vm.put_str " := ",
vm.put_str (v^.to_string opts),
vm.put_str "\n"
| _ _ :=
vm.put_str "invalid 'pidx <idx>' command, incorrect number of arguments\n"
meta def print_var : nat → nat → name → vm unit
| i ep v := do
if i = ep then vm.put_str "invalid 'print <var>', unknown variable\n"
else do
(n, t) ← vm.stack_obj_info i,
if n = v then do
v ← vm.pp_stack_obj i,
opts ← vm.get_options,
vm.put_str n^.to_string,
vm.put_str " := ",
vm.put_str (v^.to_string opts),
vm.put_str "\n"
else
print_var (i+1) ep v
meta def print_cmd : nat → list string → vm unit
| frame [arg] := do
(bp, ep) ← vm.call_stack_var_range frame,
print_var bp ep (to_qualified_name arg)
| _ _ :=
vm.put_str "invalid 'print <var>' command, incorrect number of arguments\n"
meta def cmd_loop_core : state → nat → list string → vm state
| s frame default_cmd := do
is_eof ← vm.eof,
if is_eof then do
vm.put_str "stopping debugger... 'end of file' has been found\n",
return {s with md := mode.done }
else do
vm.put_str "% ",
l ← vm.get_line,
tks ← return $ split l,
tks ← return $ if tks = [] then default_cmd else tks,
match tks with
| [] := cmd_loop_core s frame default_cmd
| (cmd::args) :=
if cmd = "help" ∨ cmd = "h" then show_help >> cmd_loop_core s frame []
else if cmd = "exit" then return {s with md := mode.done }
else if cmd = "run" ∨ cmd = "r" then return {s with md := mode.run }
else if cmd = "step" ∨ cmd = "s" then return {s with md := mode.step }
else if cmd = "break" ∨ cmd = "b" then do new_s ← add_breakpoint s args, cmd_loop_core new_s frame []
else if cmd = "rbreak" then do new_s ← remove_breakpoint s args, cmd_loop_core new_s frame []
else if cmd = "bs" then do
vm.put_str "breakpoints\n",
show_breakpoints s^.fn_bps,
cmd_loop_core s frame default_cmd
else if cmd = "up" ∨ cmd = "u" then do frame ← up_cmd frame, cmd_loop_core s frame ["u"]
else if cmd = "down" ∨ cmd = "d" then do frame ← down_cmd frame, cmd_loop_core s frame ["d"]
else if cmd = "vars" ∨ cmd = "v" then do show_vars frame, cmd_loop_core s frame []
else if cmd = "stack" then do show_stack, cmd_loop_core s frame []
else if cmd = "pidx" then do pidx_cmd frame args, cmd_loop_core s frame []
else if cmd = "print" ∨ cmd = "p" then do print_cmd frame args, cmd_loop_core s frame []
else do vm.put_str "unknown command, type 'help' for help\n", cmd_loop_core s frame default_cmd
end
meta def cmd_loop (s : state) (default_cmd : list string) : vm state :=
do csz ← vm.call_stack_size,
cmd_loop_core s (csz - 1) default_cmd
def prune_active_bps_core (csz : nat) : list (nat × name) → list (nat × name)
| [] := []
| ((csz', n)::ls) := if csz < csz' then prune_active_bps_core ls else ((csz',n)::ls)
meta def prune_active_bps (s : state) : vm state :=
do sz ← vm.call_stack_size,
return {s with active_bps := prune_active_bps_core sz s^.active_bps}
meta def updt_csz (s : state) : vm state :=
do sz ← vm.call_stack_size,
return {s with csz := sz}
meta def init_transition (s : state) : vm state :=
do opts ← vm.get_options,
if opts^.get_bool `server ff then return {s with md := mode.done}
else do
bps ← vm.get_attribute `breakpoint,
new_s ← return {s with fn_bps := bps},
if opts^.get_bool `debugger.autorun ff then
return {new_s with md := mode.run}
else do
vm.put_str "Lean debugger\n",
show_curr_fn "debugging",
vm.put_str "type 'help' for help\n",
cmd_loop new_s []
meta def step_transition (s : state) : vm state :=
do
sz ← vm.call_stack_size,
if sz = s^.csz then return s
else do
show_curr_fn "step",
cmd_loop s ["s"]
meta def bp_reached (s : state) : vm bool :=
(do fn ← vm.curr_fn,
return $ s^.fn_bps^.any (λ p, p^.is_prefix_of fn))
<|>
return ff
meta def in_active_bps (s : state) : vm bool :=
do sz ← vm.call_stack_size,
match s^.active_bps with
| [] := return ff
| (csz, _)::_ := return $ to_bool (sz = csz)
end
meta def run_transition (s : state) : vm state :=
do b1 ← in_active_bps s,
b2 ← bp_reached s,
if b1 ∨ not b2 then return s
else do
show_curr_fn "breakpoint",
fn ← vm.curr_fn,
sz ← vm.call_stack_size,
new_s ← return $ {s with active_bps := (sz, fn) :: s^.active_bps},
cmd_loop new_s ["r"]
meta def step_fn (s : state) : vm state :=
do s ← prune_active_bps s,
if s^.md = mode.init then do new_s ← init_transition s, updt_csz new_s
else if s^.md = mode.done then return s
else if s^.md = mode.step then do new_s ← step_transition s, updt_csz new_s
else if s^.md = mode.run then do new_s ← run_transition s, updt_csz new_s
else return s
meta def monitor : vm_monitor state :=
{ init := init_state, step := step_fn }
end debugger
run_command vm_monitor.register `debugger.monitor
|
837084422eb7bdd1d292bfb053e735da0bbba304 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/nat/multiplicity.lean | 916ba94714d98a8f057337f1b200bc712652a7d4 | [
"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 | 11,890 | lean | /-
Copyright (c) 2019 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import algebra.big_operators.intervals
import algebra.geom_sum
import data.nat.bitwise
import data.nat.log
import data.nat.parity
import ring_theory.int.basic
/-!
# Natural number multiplicity
This file contains lemmas about the multiplicity function (the maximum prime power dividing a
number) when applied to naturals, in particular calculating it for factorials and binomial
coefficients.
## Multiplicity calculations
* `nat.multiplicity_factorial`: Legendre's Theorem. The multiplicity of `p` in `n!` is
`n/p + ... + n/p^b` for any `b` such that `n/p^(b + 1) = 0`.
* `nat.multiplicity_factorial_mul`: The multiplicity of `p` in `(p * n)!` is `n` more than that of
`n!`.
* `nat.multiplicity_choose`: The multiplicity of `p` in `n.choose k` is the number of carries when
`k` and`n - k` are added in base `p`.
## Other declarations
* `nat.multiplicity_eq_card_pow_dvd`: The multiplicity of `m` in `n` is the number of positive
natural numbers `i` such that `m ^ i` divides `n`.
* `nat.multiplicity_two_factorial_lt`: The multiplicity of `2` in `n!` is strictly less than `n`.
* `nat.prime.multiplicity_something`: Specialization of `multiplicity.something` to a prime in the
naturals. Avoids having to provide `p ≠ 1` and other trivialities, along with translating between
`prime` and `nat.prime`.
## Tags
Legendre, p-adic
-/
open finset nat multiplicity
open_locale big_operators nat
namespace nat
/-- The multiplicity of `m` in `n` is the number of positive natural numbers `i` such that `m ^ i`
divides `n`. This set is expressed by filtering `Ico 1 b` where `b` is any bound greater than
`log m n`. -/
lemma multiplicity_eq_card_pow_dvd {m n b : ℕ} (hm : m ≠ 1) (hn : 0 < n) (hb : log m n < b):
multiplicity m n = ↑((finset.Ico 1 b).filter (λ i, m ^ i ∣ n)).card :=
calc
multiplicity m n = ↑(Ico 1 $ ((multiplicity m n).get (finite_nat_iff.2 ⟨hm, hn⟩) + 1)).card
: by simp
... = ↑((finset.Ico 1 b).filter (λ i, m ^ i ∣ n)).card
: congr_arg coe $ congr_arg card $ finset.ext $ λ i,
begin
rw [mem_filter, mem_Ico, mem_Ico, lt_succ_iff, ←@enat.coe_le_coe i, enat.coe_get,
←pow_dvd_iff_le_multiplicity, and.right_comm],
refine (and_iff_left_of_imp (λ h, _)).symm,
cases m,
{ rw [zero_pow, zero_dvd_iff] at h,
exact (hn.ne' h.2).elim,
{ exact h.1 } },
exact ((pow_le_iff_le_log (succ_lt_succ $ nat.pos_of_ne_zero $ succ_ne_succ.1 hm) hn).1 $
le_of_dvd hn h.2).trans_lt hb,
end
namespace prime
lemma multiplicity_one {p : ℕ} (hp : p.prime) : multiplicity p 1 = 0 :=
multiplicity.one_right (prime_iff.mp hp).not_unit
lemma multiplicity_mul {p m n : ℕ} (hp : p.prime) :
multiplicity p (m * n) = multiplicity p m + multiplicity p n :=
multiplicity.mul $ prime_iff.mp hp
lemma multiplicity_pow {p m n : ℕ} (hp : p.prime) :
multiplicity p (m ^ n) = n • (multiplicity p m) :=
multiplicity.pow $ prime_iff.mp hp
lemma multiplicity_self {p : ℕ} (hp : p.prime) : multiplicity p p = 1 :=
multiplicity_self (prime_iff.mp hp).not_unit hp.ne_zero
lemma multiplicity_pow_self {p n : ℕ} (hp : p.prime) : multiplicity p (p ^ n) = n :=
multiplicity_pow_self hp.ne_zero (prime_iff.mp hp).not_unit n
/-- **Legendre's Theorem**
The multiplicity of a prime in `n!` is the sum of the quotients `n / p ^ i`. This sum is expressed
over the finset `Ico 1 b` where `b` is any bound greater than `log p n`. -/
lemma multiplicity_factorial {p : ℕ} (hp : p.prime) :
∀ {n b : ℕ}, log p n < b → multiplicity p n! = (∑ i in Ico 1 b, n / p ^ i : ℕ)
| 0 b hb := by simp [Ico, hp.multiplicity_one]
| (n+1) b hb :=
calc multiplicity p (n+1)! = multiplicity p n! + multiplicity p (n+1) :
by rw [factorial_succ, hp.multiplicity_mul, add_comm]
... = (∑ i in Ico 1 b, n / p ^ i : ℕ) + ((finset.Ico 1 b).filter (λ i, p ^ i ∣ n+1)).card :
by rw [multiplicity_factorial ((log_mono_right $ le_succ _).trans_lt hb),
← multiplicity_eq_card_pow_dvd hp.ne_one (succ_pos _) hb]
... = (∑ i in Ico 1 b, (n / p ^ i + if p^i ∣ n+1 then 1 else 0) : ℕ) :
by { rw [sum_add_distrib, sum_boole], simp }
... = (∑ i in Ico 1 b, (n + 1) / p ^ i : ℕ) :
congr_arg coe $ finset.sum_congr rfl $ λ _ _, (succ_div _ _).symm
/-- The multiplicity of `p` in `(p * (n + 1))!` is one more than the sum
of the multiplicities of `p` in `(p * n)!` and `n + 1`. -/
lemma multiplicity_factorial_mul_succ {n p : ℕ} (hp : p.prime) :
multiplicity p (p * (n + 1))! = multiplicity p (p * n)! + multiplicity p (n + 1) + 1 :=
begin
have hp' := prime_iff.mp hp,
have h0 : 2 ≤ p := hp.two_le,
have h1 : 1 ≤ p * n + 1 := nat.le_add_left _ _,
have h2 : p * n + 1 ≤ p * (n + 1), linarith,
have h3 : p * n + 1 ≤ p * (n + 1) + 1, linarith,
have hm : multiplicity p (p * n)! ≠ ⊤,
{ rw [ne.def, eq_top_iff_not_finite, not_not, finite_nat_iff],
exact ⟨hp.ne_one, factorial_pos _⟩ },
revert hm,
have h4 : ∀ m ∈ Ico (p * n + 1) (p * (n + 1)), multiplicity p m = 0,
{ intros m hm, apply multiplicity_eq_zero_of_not_dvd,
rw [← exists_lt_and_lt_iff_not_dvd _ (pos_iff_ne_zero.mpr hp.ne_zero)], rw [mem_Ico] at hm,
exact ⟨n, lt_of_succ_le hm.1, hm.2⟩ },
simp_rw [← prod_Ico_id_eq_factorial, multiplicity.finset.prod hp', ← sum_Ico_consecutive _ h1 h3,
add_assoc], intro h,
rw [enat.add_left_cancel_iff h, sum_Ico_succ_top h2, multiplicity.mul hp',
hp.multiplicity_self, sum_congr rfl h4, sum_const_zero, zero_add,
add_comm (1 : enat)]
end
/-- The multiplicity of `p` in `(p * n)!` is `n` more than that of `n!`. -/
lemma multiplicity_factorial_mul {n p : ℕ} (hp : p.prime) :
multiplicity p (p * n)! = multiplicity p n! + n :=
begin
induction n with n ih,
{ simp },
{ simp only [succ_eq_add_one, multiplicity.mul, hp, prime_iff.mp hp, ih,
multiplicity_factorial_mul_succ, ←add_assoc, nat.cast_one, nat.cast_add, factorial_succ],
congr' 1,
rw [add_comm, add_assoc] }
end
/-- A prime power divides `n!` iff it is at most the sum of the quotients `n / p ^ i`.
This sum is expressed over the set `Ico 1 b` where `b` is any bound greater than `log p n` -/
lemma pow_dvd_factorial_iff {p : ℕ} {n r b : ℕ} (hp : p.prime) (hbn : log p n < b) :
p ^ r ∣ n! ↔ r ≤ ∑ i in Ico 1 b, n / p ^ i :=
by rw [← enat.coe_le_coe, ← hp.multiplicity_factorial hbn, ← pow_dvd_iff_le_multiplicity]
lemma multiplicity_factorial_le_div_pred {p : ℕ} (hp : p.prime) (n : ℕ) :
multiplicity p n! ≤ (n/(p - 1) : ℕ) :=
begin
rw [hp.multiplicity_factorial (lt_succ_self _), enat.coe_le_coe],
exact nat.geom_sum_Ico_le hp.two_le _ _,
end
lemma multiplicity_choose_aux {p n b k : ℕ} (hp : p.prime) (hkn : k ≤ n) :
∑ i in finset.Ico 1 b, n / p ^ i =
∑ i in finset.Ico 1 b, k / p ^ i + ∑ i in finset.Ico 1 b, (n - k) / p ^ i +
((finset.Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card :=
calc ∑ i in finset.Ico 1 b, n / p ^ i
= ∑ i in finset.Ico 1 b, (k + (n - k)) / p ^ i :
by simp only [add_tsub_cancel_of_le hkn]
... = ∑ i in finset.Ico 1 b, (k / p ^ i + (n - k) / p ^ i +
if p ^ i ≤ k % p ^ i + (n - k) % p ^ i then 1 else 0) :
by simp only [nat.add_div (pow_pos hp.pos _)]
... = _ : by simp [sum_add_distrib, sum_boole]
/-- The multiplicity of `p` in `choose n k` is the number of carries when `k` and `n - k`
are added in base `p`. The set is expressed by filtering `Ico 1 b` where `b`
is any bound greater than `log p n`. -/
lemma multiplicity_choose {p n k b : ℕ} (hp : p.prime) (hkn : k ≤ n) (hnb : log p n < b) :
multiplicity p (choose n k) =
((Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card :=
have h₁ : multiplicity p (choose n k) + multiplicity p (k! * (n - k)!) =
((finset.Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card +
multiplicity p (k! * (n - k)!),
begin
rw [← hp.multiplicity_mul, ← mul_assoc, choose_mul_factorial_mul_factorial hkn,
hp.multiplicity_factorial hnb, hp.multiplicity_mul,
hp.multiplicity_factorial ((log_mono_right hkn).trans_lt hnb),
hp.multiplicity_factorial (lt_of_le_of_lt (log_mono_right tsub_le_self) hnb),
multiplicity_choose_aux hp hkn],
simp [add_comm],
end,
(enat.add_right_cancel_iff
(enat.ne_top_iff_dom.2 $
by exact finite_nat_iff.2
⟨ne_of_gt hp.one_lt, mul_pos (factorial_pos k) (factorial_pos (n - k))⟩)).1
h₁
/-- A lower bound on the multiplicity of `p` in `choose n k`. -/
lemma multiplicity_le_multiplicity_choose_add {p : ℕ} (hp : p.prime) (n k : ℕ) :
multiplicity p n ≤ multiplicity p (choose n k) + multiplicity p k :=
if hkn : n < k then by simp [choose_eq_zero_of_lt hkn]
else if hk0 : k = 0 then by simp [hk0]
else if hn0 : n = 0 then by cases k; simp [hn0, *] at *
else begin
rw [multiplicity_choose hp (le_of_not_gt hkn) (lt_succ_self _),
multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) (nat.pos_of_ne_zero hk0)
(lt_succ_of_le (log_mono_right (le_of_not_gt hkn))),
multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) (nat.pos_of_ne_zero hn0) (lt_succ_self _),
← nat.cast_add, enat.coe_le_coe],
calc ((Ico 1 (log p n).succ).filter (λ i, p ^ i ∣ n)).card
≤ ((Ico 1 (log p n).succ).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i) ∪
(Ico 1 (log p n).succ).filter (λ i, p ^ i ∣ k) ).card :
card_le_of_subset $ λ i, begin
have := @le_mod_add_mod_of_dvd_add_of_not_dvd k (n - k) (p ^ i),
simp [add_tsub_cancel_of_le (le_of_not_gt hkn)] at * {contextual := tt},
tauto
end
... ≤ ((Ico 1 (log p n).succ).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card +
((Ico 1 (log p n).succ).filter (λ i, p ^ i ∣ k)).card :
card_union_le _ _
end
lemma multiplicity_choose_prime_pow {p n k : ℕ} (hp : p.prime)
(hkn : k ≤ p ^ n) (hk0 : 0 < k) :
multiplicity p (choose (p ^ n) k) + multiplicity p k = n :=
le_antisymm
(have hdisj : disjoint
((Ico 1 n.succ).filter (λ i, p ^ i ≤ k % p ^ i + (p ^ n - k) % p ^ i))
((Ico 1 n.succ).filter (λ i, p ^ i ∣ k)),
by simp [disjoint_right, *, dvd_iff_mod_eq_zero, nat.mod_lt _ (pow_pos hp.pos _)]
{contextual := tt},
begin
rw [multiplicity_choose hp hkn (lt_succ_self _),
multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) hk0
(lt_succ_of_le (log_mono_right hkn)),
← nat.cast_add, enat.coe_le_coe, log_pow hp.one_lt,
← card_disjoint_union hdisj, filter_union_right],
have filter_le_Ico := (Ico 1 n.succ).card_filter_le _,
rwa card_Ico 1 n.succ at filter_le_Ico,
end)
(by rw [← hp.multiplicity_pow_self];
exact multiplicity_le_multiplicity_choose_add hp _ _)
end prime
lemma multiplicity_two_factorial_lt : ∀ {n : ℕ} (h : n ≠ 0), multiplicity 2 n! < n :=
begin
have h2 := prime_iff.mp prime_two,
refine binary_rec _ _,
{ contradiction },
{ intros b n ih h,
by_cases hn : n = 0,
{ subst hn, simp at h, simp [h, one_right h2.not_unit, enat.zero_lt_one] },
have : multiplicity 2 (2 * n)! < (2 * n : ℕ),
{ rw [prime_two.multiplicity_factorial_mul],
refine (enat.add_lt_add_right (ih hn) (enat.coe_ne_top _)).trans_le _,
rw [two_mul], norm_cast },
cases b,
{ simpa [bit0_eq_two_mul n] },
{ suffices : multiplicity 2 (2 * n + 1) + multiplicity 2 (2 * n)! < ↑(2 * n) + 1,
{ simpa [succ_eq_add_one, multiplicity.mul, h2, prime_two, nat.bit1_eq_succ_bit0,
bit0_eq_two_mul n] },
rw [multiplicity_eq_zero_of_not_dvd (two_not_dvd_two_mul_add_one n), zero_add],
refine this.trans _, exact_mod_cast lt_succ_self _ }}
end
end nat
|
c02152226618aa910bcba05f767e5f056f9e8c8f | 6df8d5ae3acf20ad0d7f0247d2cee1957ef96df1 | /notes/9.24.19.lean | 1bbf394e5238679faa97032337f5b18aec07070f | [] | no_license | derekjohnsonva/CS2102 | 8ed45daa6658e6121bac0f6691eac6147d08246d | b3f507d4be824a2511838a1054d04fc9aef3304c | refs/heads/master | 1,648,529,162,527 | 1,578,851,859,000 | 1,578,851,859,000 | 233,433,207 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,425 | lean | /-
Domain of Discourse = "World"
the area of math that you are talking about. Ex, Boolean Algebra
Syntax Terms: Values that carry meaning
Syntax Terms are interpreted in the Domain of Discourse for which it belongs
The interpretation is known as semantics
-/
namespace mlist
open list --Lean-provided data type
/-
Lists in Lean are "polymorphic". They can contain
elements of any specified type
inductive nat : type :=
| zero
| succ: nat -> nat
-/
def e : list nat := nil
#check e
#eval e
def l1 : list nat :=
cons
1 -- head element
nil --tail list
-- cons constructor is of type list to nat
def my_list : list string :=
cons
"I"
(
cons
"Love"
(
cons
"discrete Math"
nil
)
)
#eval my_list
def my_list' : list string :=
["I", "love","DM"]
def l4 : list nat := [1,2,3,4]
/-
To build list of nat
Inductive list-nat : type :=
| nil
| cons : nat -> list-nat -> list-nat
-/
--Functions on lists
def prepend : nat → list nat → list nat
| h t := cons h t
#eval prepend 1[2,3,4]
def mhead : list nat → nat
| nil := 0
| (cons h t) := h
def mtail : list nat → list nat
| nil := nil
| (cons h t) := t
def mlength : list nat → nat
| nil := 0
| (cons h t) := 1 + (mlength t)
/-
Warmup, create a function definition that given a
natural number will return a list of natural numbers
counting down.
-/
def countdown : ℕ → list nat
| 0 := cons 0 nil
| (nat.succ n) := cons (nat.succ n) (countdown (n))
#eval countdown 10
--More Recursion
def fib : ℕ → ℕ
|nat.zero := nat.zero
|(nat.succ (nat.zero)) := (nat.succ (nat.zero))
| (nat.succ (nat.succ n)) := fib(n) + fib (n+1)
#eval fib 10
#eval fib 3
def mdouble : list ℕ → list ℕ
| [] := []
| (cons h t) := cons (h*2) (mdouble t)
def mmap : (ℕ → ℕ ) → list ℕ → list ℕ
| f [] := []
| f (cons h t) := cons (f h) (mmap f t)
#eval mmap nat.succ [1,2,3,4]
#eval mmap (λ (n:ℕ), n*n) [1,2,3,4]
def mreduce_add : list ℕ → ℕ
| [] := 0
| (cons h t) := h + (mreduce_add t)
#eval mreduce_add [1,2,3,4,5,6]
--map recude function
def mreduce : (nat → nat → nat) → nat → (list nat) → nat
| f id [] := id
| f id (cons h t) := f h (mreduce f id t)
#eval mreduce nat.add 0 [1,2,3,4,5]
#eval mreduce nat.mul 1 [1,2,3,4,5]
end mlist |
5351ee5b4dfc5a672e42587b4bb09750102935d2 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /tests/lean/640b.lean | 09f4b58343bae2f5bee43bac156a0d7388f7e55b | [
"Apache-2.0"
] | permissive | davidmueller13/lean | 65a3ed141b4088cd0a268e4de80eb6778b21a0e9 | c626e2e3c6f3771e07c32e82ee5b9e030de5b050 | refs/heads/master | 1,611,278,313,401 | 1,444,021,177,000 | 1,444,021,177,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 50 | lean | abbreviation bar [parsing-only] := @eq
check @bar
|
2b7c388447ffe7792a1d014ecccacd38f68b32cc | 367134ba5a65885e863bdc4507601606690974c1 | /src/data/pfun.lean | d14d5ec27aecab1aaf5fff705e75c0fe072c5a77 | [
"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 | 24,782 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Jeremy Avigad, Simon Hudon
-/
import data.rel
/-- `roption α` is the type of "partial values" of type `α`. It
is similar to `option α` except the domain condition can be an
arbitrary proposition, not necessarily decidable. -/
structure {u} roption (α : Type u) : Type u :=
(dom : Prop)
(get : dom → α)
namespace roption
variables {α : Type*} {β : Type*} {γ : Type*}
/-- Convert an `roption α` with a decidable domain to an option -/
def to_option (o : roption α) [decidable o.dom] : option α :=
if h : dom o then some (o.get h) else none
/-- `roption` extensionality -/
theorem ext' : ∀ {o p : roption α}
(H1 : o.dom ↔ p.dom)
(H2 : ∀h₁ h₂, o.get h₁ = p.get h₂), o = p
| ⟨od, o⟩ ⟨pd, p⟩ H1 H2 := have t : od = pd, from propext H1,
by cases t; rw [show o = p, from funext $ λp, H2 p p]
/-- `roption` eta expansion -/
@[simp] theorem eta : Π (o : roption α), (⟨o.dom, λ h, o.get h⟩ : roption α) = o
| ⟨h, f⟩ := rfl
/-- `a ∈ o` means that `o` is defined and equal to `a` -/
protected def mem (a : α) (o : roption α) : Prop := ∃ h, o.get h = a
instance : has_mem α (roption α) := ⟨roption.mem⟩
theorem mem_eq (a : α) (o : roption α) : (a ∈ o) = (∃ h, o.get h = a) :=
rfl
theorem dom_iff_mem : ∀ {o : roption α}, o.dom ↔ ∃y, y ∈ o
| ⟨p, f⟩ := ⟨λh, ⟨f h, h, rfl⟩, λ⟨_, h, rfl⟩, h⟩
theorem get_mem {o : roption α} (h) : get o h ∈ o := ⟨_, rfl⟩
/-- `roption` extensionality -/
@[ext]
theorem ext {o p : roption α} (H : ∀ a, a ∈ o ↔ a ∈ p) : o = p :=
ext' ⟨λ h, ((H _).1 ⟨h, rfl⟩).fst,
λ h, ((H _).2 ⟨h, rfl⟩).fst⟩ $
λ a b, ((H _).2 ⟨_, rfl⟩).snd
/-- The `none` value in `roption` has a `false` domain and an empty function. -/
def none : roption α := ⟨false, false.rec _⟩
instance : inhabited (roption α) := ⟨none⟩
@[simp] theorem not_mem_none (a : α) : a ∉ @none α := λ h, h.fst
/-- The `some a` value in `roption` has a `true` domain and the
function returns `a`. -/
def some (a : α) : roption α := ⟨true, λ_, a⟩
theorem mem_unique : ∀ {a b : α} {o : roption α}, a ∈ o → b ∈ o → a = b
| _ _ ⟨p, f⟩ ⟨h₁, rfl⟩ ⟨h₂, rfl⟩ := rfl
theorem mem.left_unique : relator.left_unique ((∈) : α → roption α → Prop) :=
⟨λ a o b, mem_unique⟩
theorem get_eq_of_mem {o : roption α} {a} (h : a ∈ o) (h') : get o h' = a :=
mem_unique ⟨_, rfl⟩ h
@[simp] theorem get_some {a : α} (ha : (some a).dom) : get (some a) ha = a := rfl
theorem mem_some (a : α) : a ∈ some a := ⟨trivial, rfl⟩
@[simp] theorem mem_some_iff {a b} : b ∈ (some a : roption α) ↔ b = a :=
⟨λ⟨h, e⟩, e.symm, λ e, ⟨trivial, e.symm⟩⟩
theorem eq_some_iff {a : α} {o : roption α} : o = some a ↔ a ∈ o :=
⟨λ e, e.symm ▸ mem_some _,
λ ⟨h, e⟩, e ▸ ext' (iff_true_intro h) (λ _ _, rfl)⟩
theorem eq_none_iff {o : roption α} : o = none ↔ ∀ a, a ∉ o :=
⟨λ e, e.symm ▸ not_mem_none,
λ h, ext (by simpa [not_mem_none])⟩
theorem eq_none_iff' {o : roption α} : o = none ↔ ¬ o.dom :=
⟨λ e, e.symm ▸ id, λ h, eq_none_iff.2 (λ a h', h h'.fst)⟩
lemma some_ne_none (x : α) : some x ≠ none :=
by { intro h, change none.dom, rw [← h], trivial }
lemma ne_none_iff {o : roption α} : o ≠ none ↔ ∃x, o = some x :=
begin
split,
{ rw [ne, eq_none_iff], intro h, push_neg at h, cases h with x hx, use x, rwa [eq_some_iff] },
{ rintro ⟨x, rfl⟩, apply some_ne_none }
end
lemma eq_none_or_eq_some (o : roption α) : o = none ∨ ∃ x, o = some x :=
begin
classical,
by_cases h : o.dom,
{ rw dom_iff_mem at h, right,
apply exists_imp_exists _ h,
simp [eq_some_iff] },
{ rw eq_none_iff', exact or.inl h },
end
@[simp] lemma some_inj {a b : α} : roption.some a = some b ↔ a = b :=
function.injective.eq_iff (λ a b h, congr_fun (eq_of_heq (roption.mk.inj h).2) trivial)
@[simp] lemma some_get {a : roption α} (ha : a.dom) :
roption.some (roption.get a ha) = a :=
eq.symm (eq_some_iff.2 ⟨ha, rfl⟩)
lemma get_eq_iff_eq_some {a : roption α} {ha : a.dom} {b : α} :
a.get ha = b ↔ a = some b :=
⟨λ h, by simp [h.symm], λ h, by simp [h]⟩
instance none_decidable : decidable (@none α).dom := decidable.false
instance some_decidable (a : α) : decidable (some a).dom := decidable.true
def get_or_else (a : roption α) [decidable a.dom] (d : α) :=
if ha : a.dom then a.get ha else d
@[simp] lemma get_or_else_none (d : α) : get_or_else none d = d :=
dif_neg id
@[simp] lemma get_or_else_some (a : α) (d : α) : get_or_else (some a) d = a :=
dif_pos trivial
@[simp] theorem mem_to_option {o : roption α} [decidable o.dom] {a : α} :
a ∈ to_option o ↔ a ∈ o :=
begin
unfold to_option,
by_cases h : o.dom; simp [h],
{ exact ⟨λ h, ⟨_, h⟩, λ ⟨_, h⟩, h⟩ },
{ exact mt Exists.fst h }
end
/-- Convert an `option α` into an `roption α` -/
def of_option : option α → roption α
| option.none := none
| (option.some a) := some a
@[simp] theorem mem_of_option {a : α} : ∀ {o : option α}, a ∈ of_option o ↔ a ∈ o
| option.none := ⟨λ h, h.fst.elim, λ h, option.no_confusion h⟩
| (option.some b) := ⟨λ h, congr_arg option.some h.snd,
λ h, ⟨trivial, option.some.inj h⟩⟩
@[simp] theorem of_option_dom {α} : ∀ (o : option α), (of_option o).dom ↔ o.is_some
| option.none := by simp [of_option, none]
| (option.some a) := by simp [of_option]
theorem of_option_eq_get {α} (o : option α) : of_option o = ⟨_, @option.get _ o⟩ :=
roption.ext' (of_option_dom o) $ λ h₁ h₂, by cases o; [cases h₁, refl]
instance : has_coe (option α) (roption α) := ⟨of_option⟩
@[simp] theorem mem_coe {a : α} {o : option α} :
a ∈ (o : roption α) ↔ a ∈ o := mem_of_option
@[simp] theorem coe_none : (@option.none α : roption α) = none := rfl
@[simp] theorem coe_some (a : α) : (option.some a : roption α) = some a := rfl
@[elab_as_eliminator] protected lemma induction_on {P : roption α → Prop}
(a : roption α) (hnone : P none) (hsome : ∀ a : α, P (some a)) : P a :=
(classical.em a.dom).elim
(λ h, roption.some_get h ▸ hsome _)
(λ h, (eq_none_iff'.2 h).symm ▸ hnone)
instance of_option_decidable : ∀ o : option α, decidable (of_option o).dom
| option.none := roption.none_decidable
| (option.some a) := roption.some_decidable a
@[simp] theorem to_of_option (o : option α) : to_option (of_option o) = o :=
by cases o; refl
@[simp] theorem of_to_option (o : roption α) [decidable o.dom] : of_option (to_option o) = o :=
ext $ λ a, mem_of_option.trans mem_to_option
noncomputable def equiv_option : roption α ≃ option α :=
by haveI := classical.dec; exact
⟨λ o, to_option o, of_option, λ o, of_to_option o,
λ o, eq.trans (by dsimp; congr) (to_of_option o)⟩
instance : order_bot (roption α) :=
{ le := λ x y, ∀ i, i ∈ x → i ∈ y,
le_refl := λ x y, id,
le_trans := λ x y z f g i, g _ ∘ f _,
le_antisymm := λ x y f g, roption.ext $ λ z, ⟨f _, g _⟩,
bot := none,
bot_le := by { introv x, rintro ⟨⟨_⟩,_⟩, } }
instance : preorder (roption α) :=
by apply_instance
lemma le_total_of_le_of_le {x y : roption α} (z : roption α) (hx : x ≤ z) (hy : y ≤ z) :
x ≤ y ∨ y ≤ x :=
begin
rcases roption.eq_none_or_eq_some x with h | ⟨b, h₀⟩,
{ rw h, left, apply order_bot.bot_le _ },
right, intros b' h₁,
rw roption.eq_some_iff at h₀,
replace hx := hx _ h₀, replace hy := hy _ h₁,
replace hx := roption.mem_unique hx hy, subst hx,
exact h₀
end
/-- `assert p f` is a bind-like operation which appends an additional condition
`p` to the domain and uses `f` to produce the value. -/
def assert (p : Prop) (f : p → roption α) : roption α :=
⟨∃h : p, (f h).dom, λha, (f ha.fst).get ha.snd⟩
/-- The bind operation has value `g (f.get)`, and is defined when all the
parts are defined. -/
protected def bind (f : roption α) (g : α → roption β) : roption β :=
assert (dom f) (λb, g (f.get b))
/-- The map operation for `roption` just maps the value and maintains the same domain. -/
def map (f : α → β) (o : roption α) : roption β :=
⟨o.dom, f ∘ o.get⟩
theorem mem_map (f : α → β) {o : roption α} :
∀ {a}, a ∈ o → f a ∈ map f o
| _ ⟨h, rfl⟩ := ⟨_, rfl⟩
@[simp] theorem mem_map_iff (f : α → β) {o : roption α} {b} :
b ∈ map f o ↔ ∃ a ∈ o, f a = b :=
⟨match b with _, ⟨h, rfl⟩ := ⟨_, ⟨_, rfl⟩, rfl⟩ end,
λ ⟨a, h₁, h₂⟩, h₂ ▸ mem_map f h₁⟩
@[simp] theorem map_none (f : α → β) :
map f none = none := eq_none_iff.2 $ λ a, by simp
@[simp] theorem map_some (f : α → β) (a : α) : map f (some a) = some (f a) :=
eq_some_iff.2 $ mem_map f $ mem_some _
theorem mem_assert {p : Prop} {f : p → roption α}
: ∀ {a} (h : p), a ∈ f h → a ∈ assert p f
| _ x ⟨h, rfl⟩ := ⟨⟨x, h⟩, rfl⟩
@[simp] theorem mem_assert_iff {p : Prop} {f : p → roption α} {a} :
a ∈ assert p f ↔ ∃ h : p, a ∈ f h :=
⟨match a with _, ⟨h, rfl⟩ := ⟨_, ⟨_, rfl⟩⟩ end,
λ ⟨a, h⟩, mem_assert _ h⟩
lemma assert_pos {p : Prop} {f : p → roption α} (h : p) :
assert p f = f h :=
begin
dsimp [assert],
cases h' : f h,
simp only [h', h, true_and, iff_self, exists_prop_of_true, eq_iff_iff],
apply function.hfunext,
{ simp only [h,h',exists_prop_of_true] },
{ cc }
end
lemma assert_neg {p : Prop} {f : p → roption α} (h : ¬ p) :
assert p f = none :=
begin
dsimp [assert,none], congr,
{ simp only [h, not_false_iff, exists_prop_of_false] },
{ apply function.hfunext,
{ simp only [h, not_false_iff, exists_prop_of_false] },
cc },
end
theorem mem_bind {f : roption α} {g : α → roption β} :
∀ {a b}, a ∈ f → b ∈ g a → b ∈ f.bind g
| _ _ ⟨h, rfl⟩ ⟨h₂, rfl⟩ := ⟨⟨h, h₂⟩, rfl⟩
@[simp] theorem mem_bind_iff {f : roption α} {g : α → roption β} {b} :
b ∈ f.bind g ↔ ∃ a ∈ f, b ∈ g a :=
⟨match b with _, ⟨⟨h₁, h₂⟩, rfl⟩ := ⟨_, ⟨_, rfl⟩, ⟨_, rfl⟩⟩ end,
λ ⟨a, h₁, h₂⟩, mem_bind h₁ h₂⟩
@[simp] theorem bind_none (f : α → roption β) :
none.bind f = none := eq_none_iff.2 $ λ a, by simp
@[simp] theorem bind_some (a : α) (f : α → roption β) :
(some a).bind f = f a := ext $ by simp
theorem bind_some_eq_map (f : α → β) (x : roption α) :
x.bind (some ∘ f) = map f x :=
ext $ by simp [eq_comm]
theorem bind_assoc {γ} (f : roption α) (g : α → roption β) (k : β → roption γ) :
(f.bind g).bind k = f.bind (λ x, (g x).bind k) :=
ext $ λ a, by simp; exact
⟨λ ⟨_, ⟨_, h₁, h₂⟩, h₃⟩, ⟨_, h₁, _, h₂, h₃⟩,
λ ⟨_, h₁, _, h₂, h₃⟩, ⟨_, ⟨_, h₁, h₂⟩, h₃⟩⟩
@[simp] theorem bind_map {γ} (f : α → β) (x) (g : β → roption γ) :
(map f x).bind g = x.bind (λ y, g (f y)) :=
by rw [← bind_some_eq_map, bind_assoc]; simp
@[simp] theorem map_bind {γ} (f : α → roption β) (x : roption α) (g : β → γ) :
map g (x.bind f) = x.bind (λ y, map g (f y)) :=
by rw [← bind_some_eq_map, bind_assoc]; simp [bind_some_eq_map]
theorem map_map (g : β → γ) (f : α → β) (o : roption α) :
map g (map f o) = map (g ∘ f) o :=
by rw [← bind_some_eq_map, bind_map, bind_some_eq_map]
instance : monad roption :=
{ pure := @some,
map := @map,
bind := @roption.bind }
instance : is_lawful_monad roption :=
{ bind_pure_comp_eq_map := @bind_some_eq_map,
id_map := λ β f, by cases f; refl,
pure_bind := @bind_some,
bind_assoc := @bind_assoc }
theorem map_id' {f : α → α} (H : ∀ (x : α), f x = x) (o) : map f o = o :=
by rw [show f = id, from funext H]; exact id_map o
@[simp] theorem bind_some_right (x : roption α) : x.bind some = x :=
by rw [bind_some_eq_map]; simp [map_id']
@[simp] theorem pure_eq_some (a : α) : pure a = some a := rfl
@[simp] theorem ret_eq_some (a : α) : return a = some a := rfl
@[simp] theorem map_eq_map {α β} (f : α → β) (o : roption α) :
f <$> o = map f o := rfl
@[simp] theorem bind_eq_bind {α β} (f : roption α) (g : α → roption β) :
f >>= g = f.bind g := rfl
lemma bind_le {α} (x : roption α) (f : α → roption β) (y : roption β) :
x >>= f ≤ y ↔ (∀ a, a ∈ x → f a ≤ y) :=
begin
split; intro h,
{ intros a h' b, replace h := h b,
simp only [and_imp, exists_prop, bind_eq_bind, mem_bind_iff, exists_imp_distrib] at h,
apply h _ h' },
{ intros b h',
simp only [exists_prop, bind_eq_bind, mem_bind_iff] at h',
rcases h' with ⟨a,h₀,h₁⟩, apply h _ h₀ _ h₁ },
end
instance : monad_fail roption :=
{ fail := λ_ _, none, ..roption.monad }
/- `restrict p o h` replaces the domain of `o` with `p`, and is well defined when
`p` implies `o` is defined. -/
def restrict (p : Prop) : ∀ (o : roption α), (p → o.dom) → roption α
| ⟨d, f⟩ H := ⟨p, λh, f (H h)⟩
@[simp]
theorem mem_restrict (p : Prop) (o : roption α) (h : p → o.dom) (a : α) :
a ∈ restrict p o h ↔ p ∧ a ∈ o :=
begin
cases o, dsimp [restrict, mem_eq], split,
{ rintro ⟨h₀, h₁⟩, exact ⟨h₀, ⟨_, h₁⟩⟩ },
rintro ⟨h₀, h₁, h₂⟩, exact ⟨h₀, h₂⟩
end
/-- `unwrap o` gets the value at `o`, ignoring the condition.
(This function is unsound.) -/
meta def unwrap (o : roption α) : α := o.get undefined
theorem assert_defined {p : Prop} {f : p → roption α} :
∀ (h : p), (f h).dom → (assert p f).dom := exists.intro
theorem bind_defined {f : roption α} {g : α → roption β} :
∀ (h : f.dom), (g (f.get h)).dom → (f.bind g).dom := assert_defined
@[simp] theorem bind_dom {f : roption α} {g : α → roption β} :
(f.bind g).dom ↔ ∃ h : f.dom, (g (f.get h)).dom := iff.rfl
end roption
/-- `pfun α β`, or `α →. β`, is the type of partial functions from
`α` to `β`. It is defined as `α → roption β`. -/
def pfun (α : Type*) (β : Type*) := α → roption β
infixr ` →. `:25 := pfun
namespace pfun
variables {α : Type*} {β : Type*} {γ : Type*}
instance : inhabited (α →. β) := ⟨λ a, roption.none⟩
/-- The domain of a partial function -/
def dom (f : α →. β) : set α := {a | (f a).dom}
theorem mem_dom (f : α →. β) (x : α) : x ∈ dom f ↔ ∃ y, y ∈ f x :=
by simp [dom, roption.dom_iff_mem]
theorem dom_eq (f : α →. β) : dom f = {x | ∃ y, y ∈ f x} :=
set.ext (mem_dom f)
/-- Evaluate a partial function -/
def fn (f : α →. β) (x) (h : dom f x) : β := (f x).get h
/-- Evaluate a partial function to return an `option` -/
def eval_opt (f : α →. β) [D : decidable_pred (dom f)] (x : α) : option β :=
@roption.to_option _ _ (D x)
/-- Partial function extensionality -/
theorem ext' {f g : α →. β}
(H1 : ∀ a, a ∈ dom f ↔ a ∈ dom g)
(H2 : ∀ a p q, f.fn a p = g.fn a q) : f = g :=
funext $ λ a, roption.ext' (H1 a) (H2 a)
theorem ext {f g : α →. β} (H : ∀ a b, b ∈ f a ↔ b ∈ g a) : f = g :=
funext $ λ a, roption.ext (H a)
/-- Turn a partial function into a function out of a subtype -/
def as_subtype (f : α →. β) (s : f.dom) : β := f.fn s s.2
/-- The set of partial functions `α →. β` is equivalent to
the set of pairs `(p : α → Prop, f : subtype p → β)`. -/
def equiv_subtype : (α →. β) ≃ (Σ p : α → Prop, subtype p → β) :=
⟨λ f, ⟨λ a, (f a).dom, as_subtype f⟩,
λ f x, ⟨f.1 x, λ h, f.2 ⟨x, h⟩⟩,
λ f, funext $ λ a, roption.eta _,
λ ⟨p, f⟩, by dsimp; congr; funext a; cases a; refl⟩
theorem as_subtype_eq_of_mem {f : α →. β} {x : α} {y : β} (fxy : y ∈ f x) (domx : x ∈ f.dom) :
f.as_subtype ⟨x, domx⟩ = y :=
roption.mem_unique (roption.get_mem _) fxy
/-- Turn a total function into a partial function -/
protected def lift (f : α → β) : α →. β := λ a, roption.some (f a)
instance : has_coe (α → β) (α →. β) := ⟨pfun.lift⟩
@[simp] theorem lift_eq_coe (f : α → β) : pfun.lift f = f := rfl
@[simp] theorem coe_val (f : α → β) (a : α) :
(f : α →. β) a = roption.some (f a) := rfl
/-- The graph of a partial function is the set of pairs
`(x, f x)` where `x` is in the domain of `f`. -/
def graph (f : α →. β) : set (α × β) := {p | p.2 ∈ f p.1}
def graph' (f : α →. β) : rel α β := λ x y, y ∈ f x
/-- The range of a partial function is the set of values
`f x` where `x` is in the domain of `f`. -/
def ran (f : α →. β) : set β := {b | ∃a, b ∈ f a}
/-- Restrict a partial function to a smaller domain. -/
def restrict (f : α →. β) {p : set α} (H : p ⊆ f.dom) : α →. β :=
λ x, roption.restrict (x ∈ p) (f x) (@H x)
@[simp]
theorem mem_restrict {f : α →. β} {s : set α} (h : s ⊆ f.dom) (a : α) (b : β) :
b ∈ restrict f h a ↔ a ∈ s ∧ b ∈ f a :=
by simp [restrict]
def res (f : α → β) (s : set α) : α →. β :=
restrict (pfun.lift f) (set.subset_univ s)
theorem mem_res (f : α → β) (s : set α) (a : α) (b : β) :
b ∈ res f s a ↔ (a ∈ s ∧ f a = b) :=
by simp [res, @eq_comm _ b]
theorem res_univ (f : α → β) : pfun.res f set.univ = f :=
rfl
theorem dom_iff_graph (f : α →. β) (x : α) : x ∈ f.dom ↔ ∃y, (x, y) ∈ f.graph :=
roption.dom_iff_mem
theorem lift_graph {f : α → β} {a b} : (a, b) ∈ (f : α →. β).graph ↔ f a = b :=
show (∃ (h : true), f a = b) ↔ f a = b, by simp
/-- The monad `pure` function, the total constant `x` function -/
protected def pure (x : β) : α →. β := λ_, roption.some x
/-- The monad `bind` function, pointwise `roption.bind` -/
def bind (f : α →. β) (g : β → α →. γ) : α →. γ :=
λa, roption.bind (f a) (λb, g b a)
/-- The monad `map` function, pointwise `roption.map` -/
def map (f : β → γ) (g : α →. β) : α →. γ :=
λa, roption.map f (g a)
instance : monad (pfun α) :=
{ pure := @pfun.pure _,
bind := @pfun.bind _,
map := @pfun.map _ }
instance : is_lawful_monad (pfun α) :=
{ bind_pure_comp_eq_map := λ β γ f x, funext $ λ a, roption.bind_some_eq_map _ _,
id_map := λ β f, by funext a; dsimp [functor.map, pfun.map]; cases f a; refl,
pure_bind := λ β γ x f, funext $ λ a, roption.bind_some.{u_1 u_2} _ (f x),
bind_assoc := λ β γ δ f g k,
funext $ λ a, roption.bind_assoc (f a) (λ b, g b a) (λ b, k b a) }
theorem pure_defined (p : set α) (x : β) : p ⊆ (@pfun.pure α _ x).dom := set.subset_univ p
theorem bind_defined {α β γ} (p : set α) {f : α →. β} {g : β → α →. γ}
(H1 : p ⊆ f.dom) (H2 : ∀x, p ⊆ (g x).dom) : p ⊆ (f >>= g).dom :=
λa ha, (⟨H1 ha, H2 _ ha⟩ : (f >>= g).dom a)
def fix (f : α →. β ⊕ α) : α →. β := λ a,
roption.assert (acc (λ x y, sum.inr x ∈ f y) a) $ λ h,
@well_founded.fix_F _ (λ x y, sum.inr x ∈ f y) _
(λ a IH, roption.assert (f a).dom $ λ hf,
by cases e : (f a).get hf with b a';
[exact roption.some b, exact IH _ ⟨hf, e⟩])
a h
theorem dom_of_mem_fix {f : α →. β ⊕ α} {a : α} {b : β}
(h : b ∈ fix f a) : (f a).dom :=
let ⟨h₁, h₂⟩ := roption.mem_assert_iff.1 h in
by rw well_founded.fix_F_eq at h₂; exact h₂.fst.fst
theorem mem_fix_iff {f : α →. β ⊕ α} {a : α} {b : β} :
b ∈ fix f a ↔ sum.inl b ∈ f a ∨ ∃ a', sum.inr a' ∈ f a ∧ b ∈ fix f a' :=
⟨λ h, let ⟨h₁, h₂⟩ := roption.mem_assert_iff.1 h in
begin
rw well_founded.fix_F_eq at h₂,
simp at h₂,
cases h₂ with h₂ h₃,
cases e : (f a).get h₂ with b' a'; simp [e] at h₃,
{ subst b', refine or.inl ⟨h₂, e⟩ },
{ exact or.inr ⟨a', ⟨_, e⟩, roption.mem_assert _ h₃⟩ }
end,
λ h, begin
simp [fix],
rcases h with ⟨h₁, h₂⟩ | ⟨a', h, h₃⟩,
{ refine ⟨⟨_, λ y h', _⟩, _⟩,
{ injection roption.mem_unique ⟨h₁, h₂⟩ h' },
{ rw well_founded.fix_F_eq, simp [h₁, h₂] } },
{ simp [fix] at h₃, cases h₃ with h₃ h₄,
refine ⟨⟨_, λ y h', _⟩, _⟩,
{ injection roption.mem_unique h h' with e,
exact e ▸ h₃ },
{ cases h with h₁ h₂,
rw well_founded.fix_F_eq, simp [h₁, h₂, h₄] } }
end⟩
@[elab_as_eliminator] def fix_induction
{f : α →. β ⊕ α} {b : β} {C : α → Sort*} {a : α} (h : b ∈ fix f a)
(H : ∀ a, b ∈ fix f a →
(∀ a', b ∈ fix f a' → sum.inr a' ∈ f a → C a') → C a) : C a :=
begin
replace h := roption.mem_assert_iff.1 h,
have := h.snd, revert this,
induction h.fst with a ha IH, intro h₂,
refine H a (roption.mem_assert_iff.2 ⟨⟨_, ha⟩, h₂⟩)
(λ a' ha' fa', _),
have := (roption.mem_assert_iff.1 ha').snd,
exact IH _ fa' ⟨ha _ fa', this⟩ this
end
end pfun
namespace pfun
variables {α : Type*} {β : Type*} (f : α →. β)
def image (s : set α) : set β := rel.image f.graph' s
lemma image_def (s : set α) : image f s = {y | ∃ x ∈ s, y ∈ f x} := rfl
lemma mem_image (y : β) (s : set α) : y ∈ image f s ↔ ∃ x ∈ s, y ∈ f x :=
iff.rfl
lemma image_mono {s t : set α} (h : s ⊆ t) : f.image s ⊆ f.image t :=
rel.image_mono _ h
lemma image_inter (s t : set α) : f.image (s ∩ t) ⊆ f.image s ∩ f.image t :=
rel.image_inter _ s t
lemma image_union (s t : set α) : f.image (s ∪ t) = f.image s ∪ f.image t :=
rel.image_union _ s t
def preimage (s : set β) : set α := rel.preimage (λ x y, y ∈ f x) s
lemma preimage_def (s : set β) : preimage f s = {x | ∃ y ∈ s, y ∈ f x} := rfl
lemma mem_preimage (s : set β) (x : α) : x ∈ preimage f s ↔ ∃ y ∈ s, y ∈ f x :=
iff.rfl
lemma preimage_subset_dom (s : set β) : f.preimage s ⊆ f.dom :=
assume x ⟨y, ys, fxy⟩, roption.dom_iff_mem.mpr ⟨y, fxy⟩
lemma preimage_mono {s t : set β} (h : s ⊆ t) : f.preimage s ⊆ f.preimage t :=
rel.preimage_mono _ h
lemma preimage_inter (s t : set β) : f.preimage (s ∩ t) ⊆ f.preimage s ∩ f.preimage t :=
rel.preimage_inter _ s t
lemma preimage_union (s t : set β) : f.preimage (s ∪ t) = f.preimage s ∪ f.preimage t :=
rel.preimage_union _ s t
lemma preimage_univ : f.preimage set.univ = f.dom :=
by ext; simp [mem_preimage, mem_dom]
def core (s : set β) : set α := rel.core f.graph' s
lemma core_def (s : set β) : core f s = {x | ∀ y, y ∈ f x → y ∈ s} := rfl
lemma mem_core (x : α) (s : set β) : x ∈ core f s ↔ (∀ y, y ∈ f x → y ∈ s) :=
iff.rfl
lemma compl_dom_subset_core (s : set β) : f.domᶜ ⊆ f.core s :=
assume x hx y fxy,
absurd ((mem_dom f x).mpr ⟨y, fxy⟩) hx
lemma core_mono {s t : set β} (h : s ⊆ t) : f.core s ⊆ f.core t :=
rel.core_mono _ h
lemma core_inter (s t : set β) : f.core (s ∩ t) = f.core s ∩ f.core t :=
rel.core_inter _ s t
lemma mem_core_res (f : α → β) (s : set α) (t : set β) (x : α) :
x ∈ core (res f s) t ↔ (x ∈ s → f x ∈ t) :=
by simp [mem_core, mem_res]
section
open_locale classical
lemma core_res (f : α → β) (s : set α) (t : set β) : core (res f s) t = sᶜ ∪ f ⁻¹' t :=
by { ext, rw mem_core_res, by_cases h : x ∈ s; simp [h] }
end
lemma core_restrict (f : α → β) (s : set β) : core (f : α →. β) s = set.preimage f s :=
by ext x; simp [core_def]
lemma preimage_subset_core (f : α →. β) (s : set β) : f.preimage s ⊆ f.core s :=
assume x ⟨y, ys, fxy⟩ y' fxy',
have y = y', from roption.mem_unique fxy fxy',
this ▸ ys
lemma preimage_eq (f : α →. β) (s : set β) : f.preimage s = f.core s ∩ f.dom :=
set.eq_of_subset_of_subset
(set.subset_inter (preimage_subset_core f s) (preimage_subset_dom f s))
(assume x ⟨xcore, xdom⟩,
let y := (f x).get xdom in
have ys : y ∈ s, from xcore _ (roption.get_mem _),
show x ∈ preimage f s, from ⟨(f x).get xdom, ys, roption.get_mem _⟩)
lemma core_eq (f : α →. β) (s : set β) : f.core s = f.preimage s ∪ f.domᶜ :=
by rw [preimage_eq, set.union_distrib_right, set.union_comm (dom f), set.compl_union_self,
set.inter_univ, set.union_eq_self_of_subset_right (compl_dom_subset_core f s)]
lemma preimage_as_subtype (f : α →. β) (s : set β) :
f.as_subtype ⁻¹' s = subtype.val ⁻¹' pfun.preimage f s :=
begin
ext x,
simp only [set.mem_preimage, set.mem_set_of_eq, pfun.as_subtype, pfun.mem_preimage],
show pfun.fn f (x.val) _ ∈ s ↔ ∃ y ∈ s, y ∈ f (x.val),
exact iff.intro
(assume h, ⟨_, h, roption.get_mem _⟩)
(assume ⟨y, ys, fxy⟩,
have f.fn x.val x.property ∈ f x.val := roption.get_mem _,
roption.mem_unique fxy this ▸ ys)
end
end pfun
|
b8f931496887f2cbfd493cbcd6bb3af3e4ca4032 | 97f752b44fd85ec3f635078a2dd125ddae7a82b6 | /library/data/real/division.lean | 1d995d52c92906304b5587f16175b321327d94cb | [
"Apache-2.0"
] | permissive | tectronics/lean | ab977ba6be0fcd46047ddbb3c8e16e7c26710701 | f38af35e0616f89c6e9d7e3eb1d48e47ee666efe | refs/heads/master | 1,532,358,526,384 | 1,456,276,623,000 | 1,456,276,623,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 24,024 | lean | /-
Copyright (c) 2015 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
The real numbers, constructed as equivalence classes of Cauchy sequences of rationals.
This construction follows Bishop and Bridges (1985).
At this point, we no longer proceed constructively: this file makes heavy use of decidability
and excluded middle.
-/
import data.real.basic data.real.order data.rat data.nat
open rat
open nat
open eq.ops pnat classical
namespace rat_seq
local postfix ⁻¹ := pnat.inv
-----------------------------
-- Facts about absolute values of sequences, to define inverse
definition s_abs (s : seq) : seq := λ n, abs (s n)
theorem abs_reg_of_reg {s : seq} (Hs : regular s) : regular (s_abs s) :=
begin
intros,
apply le.trans,
apply abs_abs_sub_abs_le_abs_sub,
apply Hs
end
theorem abs_pos_of_nonzero {s : seq} (Hs : regular s) (Hnz : sep s zero) :
∃ N : ℕ+, ∀ m : ℕ+, m ≥ N → abs (s m) ≥ N⁻¹ :=
begin
rewrite [↑sep at Hnz, ↑s_lt at Hnz],
apply or.elim Hnz,
intro Hnz1,
have H' : pos (sneg s), begin
apply pos_of_pos_equiv,
rotate 2,
apply Hnz1,
rotate 1,
apply s_zero_add,
repeat (assumption | apply reg_add_reg | apply reg_neg_reg | apply zero_is_reg)
end,
cases bdd_away_of_pos (reg_neg_reg Hs) H' with [N, HN],
existsi N,
intro m Hm,
apply le.trans,
apply HN m Hm,
rewrite ↑sneg,
apply neg_le_abs_self,
intro Hnz2,
let H' := pos_of_pos_equiv (reg_add_reg Hs (reg_neg_reg zero_is_reg)) (s_add_zero s Hs) Hnz2,
let H'' := bdd_away_of_pos Hs H',
cases H'' with [N, HN],
existsi N,
intro m Hm,
apply le.trans,
apply HN m Hm,
apply le_abs_self
end
theorem abs_well_defined {s t : seq} (Hs : regular s) (Ht : regular t) (Heq : s ≡ t) :
s_abs s ≡ s_abs t :=
begin
rewrite [↑equiv at *],
intro n,
rewrite ↑s_abs,
apply le.trans,
apply abs_abs_sub_abs_le_abs_sub,
apply Heq
end
theorem sep_zero_of_pos {s : seq} (Hs : regular s) (Hpos : pos s) : sep s zero :=
begin
apply or.inr,
apply pos_of_pos_equiv,
rotate 2,
apply Hpos,
apply Hs,
apply equiv.symm,
apply s_sub_zero Hs
end
------------------------
-- This section could be cleaned up.
private noncomputable definition pb {s : seq} (Hs : regular s) (Hpos : pos s) :=
some (abs_pos_of_nonzero Hs (sep_zero_of_pos Hs Hpos))
private noncomputable definition ps {s : seq} (Hs : regular s) (Hsep : sep s zero) :=
some (abs_pos_of_nonzero Hs Hsep)
private theorem pb_spec {s : seq} (Hs : regular s) (Hpos : pos s) :
∀ m : ℕ+, m ≥ (pb Hs Hpos) → abs (s m) ≥ (pb Hs Hpos)⁻¹ :=
some_spec (abs_pos_of_nonzero Hs (sep_zero_of_pos Hs Hpos))
private theorem ps_spec {s : seq} (Hs : regular s) (Hsep : sep s zero) :
∀ m : ℕ+, m ≥ (ps Hs Hsep) → abs (s m) ≥ (ps Hs Hsep)⁻¹ :=
some_spec (abs_pos_of_nonzero Hs Hsep)
noncomputable definition s_inv {s : seq} (Hs : regular s) (n : ℕ+) : ℚ :=
if H : sep s zero then
(if n < (ps Hs H) then 1 / (s ((ps Hs H) * (ps Hs H) * (ps Hs H)))
else 1 / (s ((ps Hs H) * (ps Hs H) * n)))
else 0
private theorem peq {s : seq} (Hsep : sep s zero) (Hpos : pos s) (Hs : regular s) :
pb Hs Hpos = ps Hs Hsep := rfl
private theorem s_inv_of_sep_lt_p {s : seq} (Hs : regular s) (Hsep : sep s zero) {n : ℕ+}
(Hn : n < (ps Hs Hsep)) : s_inv Hs n = 1 / s ((ps Hs Hsep) * (ps Hs Hsep) * (ps Hs Hsep)) :=
begin
apply eq.trans,
apply dif_pos Hsep,
apply dif_pos Hn
end
private theorem s_inv_of_sep_gt_p {s : seq} (Hs : regular s) (Hsep : sep s zero) {n : ℕ+}
(Hn : n ≥ (ps Hs Hsep)) : s_inv Hs n = 1 / s ((ps Hs Hsep) * (ps Hs Hsep) * n) :=
begin
apply eq.trans,
apply dif_pos Hsep,
apply dif_neg (pnat.not_lt_of_ge Hn)
end
private theorem s_inv_of_pos_lt_p {s : seq} (Hs : regular s) (Hpos : pos s) {n : ℕ+}
(Hn : n < (pb Hs Hpos)) : s_inv Hs n = 1 / s ((pb Hs Hpos) * (pb Hs Hpos) * (pb Hs Hpos)) :=
s_inv_of_sep_lt_p Hs (sep_zero_of_pos Hs Hpos) Hn
private theorem s_inv_of_pos_gt_p {s : seq} (Hs : regular s) (Hpos : pos s) {n : ℕ+}
(Hn : n ≥ (pb Hs Hpos)) : s_inv Hs n = 1 / s ((pb Hs Hpos) * (pb Hs Hpos) * n) :=
s_inv_of_sep_gt_p Hs (sep_zero_of_pos Hs Hpos) Hn
private theorem le_ps {s : seq} (Hs : regular s) (Hsep : sep s zero) (n : ℕ+) :
abs (s_inv Hs n) ≤ (rat_of_pnat (ps Hs Hsep)) :=
if Hn : n < ps Hs Hsep then
(begin
rewrite [(s_inv_of_sep_lt_p Hs Hsep Hn), abs_one_div],
apply div_le_pnat,
apply ps_spec,
apply pnat.mul_le_mul_left
end)
else
(begin
rewrite [(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hn)), abs_one_div],
apply div_le_pnat,
apply ps_spec,
rewrite pnat.mul_assoc,
apply pnat.mul_le_mul_right
end)
theorem s_inv_zero : s_inv zero_is_reg = zero :=
funext (λ n, dif_neg (!not_sep_self))
private theorem s_inv_of_zero' {s : seq} (Hs : regular s) (Hz : ¬ sep s zero) (n : ℕ+) : s_inv Hs n = 0 :=
dif_neg Hz
theorem s_inv_of_zero {s : seq} (Hs : regular s) (Hz : ¬ sep s zero) : s_inv Hs = zero :=
begin
apply funext,
intro n,
apply s_inv_of_zero' Hs Hz n
end
private theorem s_ne_zero_of_ge_p {s : seq} (Hs : regular s) (Hsep : sep s zero) {n : ℕ+}
(Hn : n ≥ (ps Hs Hsep)) : s n ≠ 0 :=
begin
let Hps := ps_spec Hs Hsep,
apply ne_zero_of_abs_ne_zero,
apply ne_of_gt,
apply gt_of_ge_of_gt,
apply Hps,
apply Hn,
apply pnat.inv_pos
end
theorem reg_inv_reg {s : seq} (Hs : regular s) (Hsep : sep s zero) : regular (s_inv Hs) :=
begin
rewrite ↑regular,
intros,
have Hsp : s ((ps Hs Hsep) * (ps Hs Hsep) * (ps Hs Hsep)) ≠ 0, from
s_ne_zero_of_ge_p Hs Hsep !pnat.mul_le_mul_left,
have Hspn : s ((ps Hs Hsep) * (ps Hs Hsep) * n) ≠ 0, from
s_ne_zero_of_ge_p Hs Hsep (show (ps Hs Hsep) * (ps Hs Hsep) * n ≥ ps Hs Hsep, by
rewrite pnat.mul_assoc; apply pnat.mul_le_mul_right),
have Hspm : s ((ps Hs Hsep) * (ps Hs Hsep) * m) ≠ 0, from
s_ne_zero_of_ge_p Hs Hsep (show (ps Hs Hsep) * (ps Hs Hsep) * m ≥ ps Hs Hsep, by
rewrite pnat.mul_assoc; apply pnat.mul_le_mul_right),
cases em (m < ps Hs Hsep) with [Hmlt, Hmlt],
cases em (n < ps Hs Hsep) with [Hnlt, Hnlt],
rewrite [(s_inv_of_sep_lt_p Hs Hsep Hmlt), (s_inv_of_sep_lt_p Hs Hsep Hnlt)],
rewrite [sub_self, abs_zero],
apply add_invs_nonneg,
rewrite [(s_inv_of_sep_lt_p Hs Hsep Hmlt),
(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hnlt))],
rewrite [(!div_sub_div Hsp Hspn), div_eq_mul_one_div, *abs_mul, *mul_one, *one_mul],
apply le.trans,
apply mul_le_mul,
apply Hs,
rewrite [-(mul_one 1), -(!field.div_mul_div Hsp Hspn), abs_mul],
apply mul_le_mul,
rewrite -(s_inv_of_sep_lt_p Hs Hsep Hmlt),
apply le_ps Hs Hsep,
rewrite -(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hnlt)),
apply le_ps Hs Hsep,
apply abs_nonneg,
apply le_of_lt !rat_of_pnat_is_pos,
apply abs_nonneg,
apply add_invs_nonneg,
rewrite [right_distrib, *pnat_cancel', add.comm],
apply add_le_add_right,
apply inv_ge_of_le,
apply pnat.le_of_lt,
apply Hmlt,
cases em (n < ps Hs Hsep) with [Hnlt, Hnlt],
rewrite [(s_inv_of_sep_lt_p Hs Hsep Hnlt),
(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hmlt))],
rewrite [(!div_sub_div Hspm Hsp), div_eq_mul_one_div, *abs_mul, *mul_one, *one_mul],
apply le.trans,
apply mul_le_mul,
apply Hs,
rewrite [-(mul_one 1), -(!field.div_mul_div Hspm Hsp), abs_mul],
apply mul_le_mul,
rewrite -(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hmlt)),
apply le_ps Hs Hsep,
rewrite -(s_inv_of_sep_lt_p Hs Hsep Hnlt),
apply le_ps Hs Hsep,
apply abs_nonneg,
apply le_of_lt !rat_of_pnat_is_pos,
apply abs_nonneg,
apply add_invs_nonneg,
rewrite [right_distrib, *pnat_cancel', add.comm],
apply rat.add_le_add_left,
apply inv_ge_of_le,
apply pnat.le_of_lt,
apply Hnlt,
rewrite [(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hnlt)),
(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hmlt))],
rewrite [(!div_sub_div Hspm Hspn), div_eq_mul_one_div, abs_mul, *one_mul, *mul_one],
apply le.trans,
apply mul_le_mul,
apply Hs,
rewrite [-(mul_one 1), -(!field.div_mul_div Hspm Hspn), abs_mul],
apply mul_le_mul,
rewrite -(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hmlt)),
apply le_ps Hs Hsep,
rewrite -(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hnlt)),
apply le_ps Hs Hsep,
apply abs_nonneg,
apply le_of_lt !rat_of_pnat_is_pos,
apply abs_nonneg,
apply add_invs_nonneg,
rewrite [right_distrib, *pnat_cancel', add.comm],
apply le.refl
end
theorem s_inv_ne_zero {s : seq} (Hs : regular s) (Hsep : sep s zero) (n : ℕ+) : s_inv Hs n ≠ 0 :=
if H : n ≥ ps Hs Hsep then
(begin
rewrite (s_inv_of_sep_gt_p Hs Hsep H),
apply one_div_ne_zero,
apply s_ne_zero_of_ge_p,
apply pnat.le_trans,
apply H,
apply pnat.mul_le_mul_left
end)
else
(begin
rewrite (s_inv_of_sep_lt_p Hs Hsep (pnat.lt_of_not_le H)),
apply one_div_ne_zero,
apply s_ne_zero_of_ge_p,
apply pnat.mul_le_mul_left
end)
protected theorem mul_inv {s : seq} (Hs : regular s) (Hsep : sep s zero) :
smul s (s_inv Hs) ≡ one :=
begin
let Rsi := reg_inv_reg Hs Hsep,
let Rssi := reg_mul_reg Hs Rsi,
apply eq_of_bdd Rssi one_is_reg,
intros,
existsi max (ps Hs Hsep) j,
intro n Hn,
have Hnz : s_inv Hs ((K₂ s (s_inv Hs)) * 2 * n) ≠ 0, from s_inv_ne_zero Hs Hsep _,
rewrite [↑smul, ↑one, mul.comm, -(mul_one_div_cancel Hnz),
-mul_sub_left_distrib, abs_mul],
apply le.trans,
apply mul_le_mul_of_nonneg_right,
apply canon_2_bound_right s,
apply Rsi,
apply abs_nonneg,
have Hp : (K₂ s (s_inv Hs)) * 2 * n ≥ ps Hs Hsep, begin
apply pnat.le_trans,
apply pnat.max_left,
rotate 1,
apply pnat.le_trans,
apply Hn,
apply pnat.mul_le_mul_left
end,
have Hnz' : s (((ps Hs Hsep) * (ps Hs Hsep)) * ((K₂ s (s_inv Hs)) * 2 * n)) ≠ 0, from
s_ne_zero_of_ge_p Hs Hsep
(show ps Hs Hsep ≤ ((ps Hs Hsep) * (ps Hs Hsep)) * ((K₂ s (s_inv Hs)) * 2 * n),
by rewrite *pnat.mul_assoc; apply pnat.mul_le_mul_right),
rewrite [(s_inv_of_sep_gt_p Hs Hsep Hp), (division_ring.one_div_one_div Hnz')],
apply rat.le_trans,
apply mul_le_mul_of_nonneg_left,
apply Hs,
apply le_of_lt,
apply rat_of_pnat_is_pos,
rewrite [left_distrib, pnat.mul_comm ((ps Hs Hsep) * (ps Hs Hsep)), *pnat.mul_assoc,
*(@pnat.inv_mul_eq_mul_inv (K₂ s (s_inv Hs))), -*mul.assoc, *pnat.inv_cancel_left,
*one_mul, -(pnat.add_halves j)],
apply add_le_add,
apply inv_ge_of_le,
apply pnat_mul_le_mul_left',
apply pnat.le_trans,
rotate 1,
apply Hn,
rotate_right 1,
apply pnat.max_right,
apply inv_ge_of_le,
apply pnat_mul_le_mul_left',
apply pnat.le_trans,
apply pnat.max_right,
rotate 1,
apply pnat.le_trans,
apply Hn,
apply pnat.mul_le_mul_right
end
protected theorem inv_mul {s : seq} (Hs : regular s) (Hsep : sep s zero) :
smul (s_inv Hs) s ≡ one :=
begin
apply equiv.trans,
rotate 3,
apply s_mul_comm,
apply rat_seq.mul_inv,
repeat (assumption | apply reg_mul_reg | apply reg_inv_reg | apply zero_is_reg)
end
theorem sep_of_equiv_sep {s t : seq} (Hs : regular s) (Ht : regular t) (Heq : s ≡ t)
(Hsep : sep s zero) : sep t zero :=
begin
apply or.elim Hsep,
intro Hslt,
apply or.inl,
rewrite ↑s_lt at *,
apply pos_of_pos_equiv,
rotate 2,
apply Hslt,
rotate_right 1,
apply add_well_defined,
rotate 4,
apply equiv.refl,
apply neg_well_defined,
apply Heq,
intro Hslt,
apply or.inr,
rewrite ↑s_lt at *,
apply pos_of_pos_equiv,
rotate 2,
apply Hslt,
rotate_right 1,
apply add_well_defined,
rotate 5,
apply equiv.refl,
repeat (assumption | apply reg_neg_reg | apply reg_add_reg | apply zero_is_reg)
end
theorem inv_unique {s t : seq} (Hs : regular s) (Ht : regular t) (Hsep : sep s zero)
(Heq : smul s t ≡ one) : s_inv Hs ≡ t :=
begin
apply equiv.trans,
rotate 3,
apply equiv.symm,
apply s_mul_one,
rotate 1,
apply equiv.trans,
rotate 3,
apply mul_well_defined,
rotate 4,
apply equiv.refl,
apply equiv.symm,
apply Heq,
apply equiv.trans,
rotate 3,
apply equiv.symm,
apply s_mul_assoc,
rotate 3,
apply equiv.trans,
rotate 3,
apply mul_well_defined,
rotate 4,
apply rat_seq.inv_mul,
rotate 1,
apply equiv.refl,
apply s_one_mul,
repeat (assumption | apply reg_inv_reg | apply reg_mul_reg | apply one_is_reg)
end
theorem inv_well_defined {s t : seq} (Hs : regular s) (Ht : regular t) (Heq : s ≡ t) :
s_inv Hs ≡ s_inv Ht :=
if Hsep : sep s zero then
(begin
note Hsept := sep_of_equiv_sep Hs Ht Heq Hsep,
have Hm : smul t (s_inv Hs) ≡ smul s (s_inv Hs), begin
apply mul_well_defined,
repeat (assumption | apply reg_inv_reg),
apply equiv.symm s t Heq,
apply equiv.refl
end,
apply equiv.symm,
apply inv_unique,
rotate 2,
apply equiv.trans,
rotate 3,
apply Hm,
apply rat_seq.mul_inv,
repeat (assumption | apply reg_inv_reg | apply reg_mul_reg),
apply one_is_reg
end)
else
(assert H : s_inv Hs = zero, from funext (λ n, dif_neg Hsep),
have Hsept : ¬ sep t zero, from
assume H', Hsep (sep_of_equiv_sep Ht Hs (equiv.symm _ _ Heq) H'),
assert H' : s_inv Ht = zero, from funext (λ n, dif_neg Hsept),
by rewrite [H', H]; apply equiv.refl)
theorem s_neg_neg {s : seq} : sneg (sneg s) ≡ s :=
begin
rewrite [↑equiv, ↑sneg],
intro n,
rewrite [neg_neg, sub_self, abs_zero],
apply add_invs_nonneg
end
theorem s_neg_sub {s t : seq} (Hs : regular s) (Ht : regular t) :
sneg (sadd s (sneg t)) ≡ sadd t (sneg s) :=
begin
apply equiv.trans,
rotate 3,
apply s_neg_add_eq_s_add_neg,
apply equiv.trans,
rotate 3,
apply add_well_defined,
rotate 4,
apply equiv.refl,
apply s_neg_neg,
apply s_add_comm,
repeat (assumption | apply reg_add_reg | apply reg_neg_reg)
end
theorem s_le_total {s t : seq} (Hs : regular s) (Ht : regular t) : s_le s t ∨ s_le t s :=
if H : s_le s t then or.inl H else or.inr begin
rewrite [↑s_le at *],
have H' : ∃ n : ℕ+, -n⁻¹ > sadd t (sneg s) n, begin
apply by_contradiction,
intro Hex,
have Hex' : ∀ n : ℕ+, -n⁻¹ ≤ sadd t (sneg s) n, begin
intro m,
apply by_contradiction,
intro Hm,
note Hm' := lt_of_not_ge Hm,
note Hex'' := exists.intro m Hm',
apply Hex Hex''
end,
apply H Hex'
end,
eapply exists.elim H',
intro m Hm,
note Hm' := neg_lt_neg Hm,
rewrite neg_neg at Hm',
apply s_nonneg_of_pos,
rotate 1,
apply pos_of_pos_equiv,
rotate 1,
apply s_neg_sub,
rotate 2,
rewrite [↑pos, ↑sneg],
existsi m,
apply Hm',
repeat (assumption | apply reg_add_reg | apply reg_neg_reg)
end
theorem s_le_of_not_lt {s t : seq} (Hle : ¬ s_lt s t) : s_le t s :=
begin
rewrite [↑s_le, ↑nonneg, ↑s_lt at Hle, ↑pos at Hle],
let Hle' := iff.mp forall_iff_not_exists Hle,
intro n,
let Hn := neg_le_neg (le_of_not_gt (Hle' n)),
rewrite [↑sadd, ↑sneg, add_neg_eq_neg_add_rev],
apply Hn
end
theorem sep_of_nequiv {s t : seq} (Hs : regular s) (Ht : regular t) (Hneq : ¬ equiv s t) :
sep s t :=
begin
rewrite ↑sep,
apply by_contradiction,
intro Hnor,
let Hand := iff.mp !not_or_iff_not_and_not Hnor,
let Hle1 := s_le_of_not_lt (and.left Hand),
let Hle2 := s_le_of_not_lt (and.right Hand),
apply Hneq (equiv_of_le_of_ge Hs Ht Hle2 Hle1)
end
theorem s_zero_inv_equiv_zero : s_inv zero_is_reg ≡ zero :=
by rewrite s_inv_zero; apply equiv.refl
theorem lt_or_equiv_of_le {s t : seq} (Hs : regular s) (Ht : regular t) (Hle : s_le s t) :
s_lt s t ∨ s ≡ t :=
if H : s ≡ t then or.inr H else
or.inl (lt_of_le_and_sep Hs Ht (and.intro Hle (sep_of_nequiv Hs Ht H)))
theorem s_le_of_equiv_le_left {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(Heq : s ≡ t) (Hle : s_le s u) : s_le t u :=
begin
rewrite ↑s_le at *,
apply nonneg_of_nonneg_equiv,
rotate 2,
apply add_well_defined,
rotate 4,
apply equiv.refl,
apply neg_well_defined,
apply Heq,
repeat (assumption | apply reg_add_reg | apply reg_neg_reg)
end
theorem s_le_of_equiv_le_right {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(Heq : t ≡ u) (Hle : s_le s t) : s_le s u :=
begin
rewrite ↑s_le at *,
apply nonneg_of_nonneg_equiv,
rotate 2,
apply add_well_defined,
rotate 4,
apply Heq,
apply equiv.refl,
repeat (assumption | apply reg_add_reg | apply reg_neg_reg)
end
-----------------------------
noncomputable definition r_inv (s : reg_seq) : reg_seq := reg_seq.mk (s_inv (reg_seq.is_reg s))
(if H : sep (reg_seq.sq s) zero then reg_inv_reg (reg_seq.is_reg s) H else
assert Hz : s_inv (reg_seq.is_reg s) = zero, from funext (λ n, dif_neg H),
by rewrite Hz; apply zero_is_reg)
theorem r_inv_zero : requiv (r_inv r_zero) r_zero :=
s_zero_inv_equiv_zero
theorem r_inv_well_defined {s t : reg_seq} (H : requiv s t) : requiv (r_inv s) (r_inv t) :=
inv_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) H
theorem r_le_total (s t : reg_seq) : r_le s t ∨ r_le t s :=
s_le_total (reg_seq.is_reg s) (reg_seq.is_reg t)
theorem r_mul_inv (s : reg_seq) (Hsep : r_sep s r_zero) : requiv (s * (r_inv s)) r_one :=
rat_seq.mul_inv (reg_seq.is_reg s) Hsep
theorem r_sep_of_nequiv (s t : reg_seq) (Hneq : ¬ requiv s t) : r_sep s t :=
sep_of_nequiv (reg_seq.is_reg s) (reg_seq.is_reg t) Hneq
theorem r_lt_or_equiv_of_le (s t : reg_seq) (Hle : r_le s t) : r_lt s t ∨ requiv s t :=
lt_or_equiv_of_le (reg_seq.is_reg s) (reg_seq.is_reg t) Hle
theorem r_le_of_equiv_le_left {s t u : reg_seq} (Heq : requiv s t) (Hle : r_le s u) : r_le t u :=
s_le_of_equiv_le_left (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) Heq Hle
theorem r_le_of_equiv_le_right {s t u : reg_seq} (Heq : requiv t u) (Hle : r_le s t) : r_le s u :=
s_le_of_equiv_le_right (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) Heq Hle
definition r_abs (s : reg_seq) : reg_seq :=
reg_seq.mk (s_abs (reg_seq.sq s)) (abs_reg_of_reg (reg_seq.is_reg s))
theorem r_abs_well_defined {s t : reg_seq} (H : requiv s t) : requiv (r_abs s) (r_abs t) :=
abs_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) H
end rat_seq
namespace real
open [class] rat_seq
noncomputable protected definition inv (x : ℝ) : ℝ :=
quot.lift_on x (λ a, quot.mk (rat_seq.r_inv a))
(λ a b H, quot.sound (rat_seq.r_inv_well_defined H))
noncomputable definition real_has_inv [instance] [reducible] [priority real.prio] : has_inv real :=
has_inv.mk real.inv
noncomputable protected definition div (x y : ℝ) : ℝ :=
x * y⁻¹
noncomputable definition real_has_div : has_div real :=
has_div.mk real.div
local attribute real_has_div [instance] [reducible] [priority real.prio]
protected theorem le_total (x y : ℝ) : x ≤ y ∨ y ≤ x :=
quot.induction_on₂ x y (λ s t, rat_seq.r_le_total s t)
protected theorem mul_inv_cancel' (x : ℝ) : x ≢ 0 → x * x⁻¹ = 1 :=
quot.induction_on x (λ s H, quot.sound (rat_seq.r_mul_inv s H))
protected theorem inv_mul_cancel' (x : ℝ) : x ≢ 0 → x⁻¹ * x = 1 :=
by rewrite real.mul_comm; apply real.mul_inv_cancel'
theorem neq_of_sep {x y : ℝ} (H : x ≢ y) : ¬ x = y :=
assume Heq, !not_sep_self (Heq ▸ H)
theorem sep_of_neq {x y : ℝ} : ¬ x = y → x ≢ y :=
quot.induction_on₂ x y (λ s t H, rat_seq.r_sep_of_nequiv s t (assume Heq, H (quot.sound Heq)))
theorem sep_is_neq (x y : ℝ) : (x ≢ y) = (¬ x = y) :=
propext (iff.intro neq_of_sep sep_of_neq)
protected theorem mul_inv_cancel (x : ℝ) : x ≠ 0 → x * x⁻¹ = 1 :=
!sep_is_neq ▸ !real.mul_inv_cancel'
protected theorem inv_mul_cancel (x : ℝ) : x ≠ 0 → x⁻¹ * x = 1 :=
!sep_is_neq ▸ !real.inv_mul_cancel'
protected theorem inv_zero : (0 : ℝ)⁻¹ = 0 := quot.sound (rat_seq.r_inv_zero)
protected theorem lt_or_eq_of_le (x y : ℝ) : x ≤ y → x < y ∨ x = y :=
quot.induction_on₂ x y (λ s t H, or.elim (rat_seq.r_lt_or_equiv_of_le s t H)
(assume H1, or.inl H1)
(assume H2, or.inr (quot.sound H2)))
protected theorem le_iff_lt_or_eq (x y : ℝ) : x ≤ y ↔ x < y ∨ x = y :=
iff.intro (real.lt_or_eq_of_le x y) (real.le_of_lt_or_eq x y)
noncomputable definition dec_lt : decidable_rel real.lt :=
begin
rewrite ↑decidable_rel,
intros,
apply prop_decidable
end
protected noncomputable definition discrete_linear_ordered_field [reducible] [trans_instance]:
discrete_linear_ordered_field ℝ :=
⦃ discrete_linear_ordered_field, real.comm_ring, real.ordered_ring,
le_total := real.le_total,
mul_inv_cancel := real.mul_inv_cancel,
inv_mul_cancel := real.inv_mul_cancel,
zero_lt_one := real.zero_lt_one,
inv_zero := real.inv_zero,
le_iff_lt_or_eq := real.le_iff_lt_or_eq,
decidable_lt := dec_lt
⦄
theorem of_rat_divide (x y : ℚ) : of_rat (x / y) = of_rat x / of_rat y :=
by_cases
(assume yz : y = 0, by krewrite [yz, div_zero, +of_rat_zero, div_zero])
(assume ynz : y ≠ 0,
have ynz' : of_rat y ≠ 0, from assume yz', ynz (of_rat.inj yz'),
!eq_div_of_mul_eq ynz' (by krewrite [-of_rat_mul, !div_mul_cancel ynz]))
open int
theorem of_int_div (x y : ℤ) (H : y ∣ x) : of_int (x / y) = of_int x / of_int y :=
by rewrite [of_int_eq, rat.of_int_div H, of_rat_divide]
theorem of_nat_div (x y : ℕ) (H : y ∣ x) : of_nat (x / y) = of_nat x / of_nat y :=
by rewrite [of_nat_eq, rat.of_nat_div H, of_rat_divide]
/- useful for proving equalities -/
theorem eq_zero_of_nonneg_of_forall_lt {x : ℝ} (xnonneg : x ≥ 0) (H : ∀ ε : ℝ, ε > 0 → x < ε) :
x = 0 :=
decidable.by_contradiction
(suppose x ≠ 0,
have x > 0, from lt_of_le_of_ne xnonneg (ne.symm this),
have x < x, from H x this,
show false, from !lt.irrefl this)
theorem eq_zero_of_nonneg_of_forall_le {x : ℝ} (xnonneg : x ≥ 0) (H : ∀ ε : ℝ, ε > 0 → x ≤ ε) :
x = 0 :=
have ∀ ε : ℝ, ε > 0 → x < ε, from
take ε, suppose ε > 0,
assert e2pos : ε / 2 > 0, from div_pos_of_pos_of_pos `ε > 0` two_pos,
assert ε / 2 < ε, from div_two_lt_of_pos `ε > 0`,
begin apply lt_of_le_of_lt, apply H _ e2pos, apply this end,
eq_zero_of_nonneg_of_forall_lt xnonneg this
theorem eq_zero_of_forall_abs_le {x : ℝ} (H : ∀ ε : ℝ, ε > 0 → abs x ≤ ε) :
x = 0 :=
by_contradiction
(suppose x ≠ 0,
have abs x = 0, from eq_zero_of_nonneg_of_forall_le !abs_nonneg H,
show false, from `x ≠ 0` (eq_zero_of_abs_eq_zero this))
theorem eq_of_forall_abs_sub_le {x y : ℝ} (H : ∀ ε : ℝ, ε > 0 → abs (x - y) ≤ ε) :
x = y :=
have x - y = 0, from eq_zero_of_forall_abs_le H,
eq_of_sub_eq_zero this
end real
|
ce10d42f1138959ed12b4f3ba15aa9ccc18fc0d4 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/topology/category/Top/limits.lean | 39b2769596412b2deaebb05630b376df4d42f95a | [
"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 | 45,790 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Scott Morrison, Mario Carneiro, Andrew Yang
-/
import topology.category.Top.epi_mono
import category_theory.category.ulift
import category_theory.limits.concrete_category
import category_theory.concrete_category.elementwise
/-!
# The category of topological spaces has all limits and colimits
Further, these limits and colimits are preserved by the forgetful functor --- that is, the
underlying types are just the limits in the category of types.
-/
open topological_space
open category_theory
open category_theory.limits
open opposite
universes u v w
noncomputable theory
namespace Top
variables {J : Type v} [small_category J]
local notation `forget` := forget Top
/--
A choice of limit cone for a functor `F : J ⥤ Top`.
Generally you should just use `limit.cone F`, unless you need the actual definition
(which is in terms of `types.limit_cone`).
-/
def limit_cone (F : J ⥤ Top.{max v u}) : cone F :=
{ X := Top.of {u : Π j : J, F.obj j | ∀ {i j : J} (f : i ⟶ j), F.map f (u i) = u j},
π :=
{ app := λ j,
{ to_fun := λ u, u.val j,
continuous_to_fun := show continuous ((λ u : Π j : J, F.obj j, u j) ∘ subtype.val),
by continuity } } }
/--
A choice of limit cone for a functor `F : J ⥤ Top` whose topology is defined as an
infimum of topologies infimum.
Generally you should just use `limit.cone F`, unless you need the actual definition
(which is in terms of `types.limit_cone`).
-/
def limit_cone_infi (F : J ⥤ Top.{max v u}) : cone F :=
{ X := ⟨(types.limit_cone (F ⋙ forget)).X, ⨅j,
(F.obj j).str.induced ((types.limit_cone (F ⋙ forget)).π.app j)⟩,
π :=
{ app := λ j, ⟨(types.limit_cone (F ⋙ forget)).π.app j,
continuous_iff_le_induced.mpr (infi_le _ _)⟩,
naturality' := λ j j' f, continuous_map.coe_injective
((types.limit_cone (F ⋙ forget)).π.naturality f) } }
/--
The chosen cone `Top.limit_cone F` for a functor `F : J ⥤ Top` is a limit cone.
Generally you should just use `limit.is_limit F`, unless you need the actual definition
(which is in terms of `types.limit_cone_is_limit`).
-/
def limit_cone_is_limit (F : J ⥤ Top.{max v u}) : is_limit (limit_cone F) :=
{ lift := λ S, { to_fun := λ x, ⟨λ j, S.π.app _ x, λ i j f, by { dsimp, erw ← S.w f, refl }⟩ },
uniq' := λ S m h, by { ext : 3, simpa [← h] } }
/--
The chosen cone `Top.limit_cone_infi F` for a functor `F : J ⥤ Top` is a limit cone.
Generally you should just use `limit.is_limit F`, unless you need the actual definition
(which is in terms of `types.limit_cone_is_limit`).
-/
def limit_cone_infi_is_limit (F : J ⥤ Top.{max v u}) : is_limit (limit_cone_infi F) :=
by { refine is_limit.of_faithful forget (types.limit_cone_is_limit _) (λ s, ⟨_, _⟩) (λ s, rfl),
exact continuous_iff_coinduced_le.mpr (le_infi $ λ j,
coinduced_le_iff_le_induced.mp $ (continuous_iff_coinduced_le.mp (s.π.app j).continuous :
_) ) }
instance Top_has_limits_of_size : has_limits_of_size.{v} Top.{max v u} :=
{ has_limits_of_shape := λ J 𝒥, by exactI
{ has_limit := λ F, has_limit.mk { cone := limit_cone F, is_limit := limit_cone_is_limit F } } }
instance Top_has_limits : has_limits Top.{u} := Top.Top_has_limits_of_size.{u u}
instance forget_preserves_limits_of_size :
preserves_limits_of_size.{v v} (forget : Top.{max v u} ⥤ Type (max v u)) :=
{ preserves_limits_of_shape := λ J 𝒥,
{ preserves_limit := λ F,
by exactI preserves_limit_of_preserves_limit_cone
(limit_cone_is_limit F) (types.limit_cone_is_limit (F ⋙ forget)) } }
instance forget_preserves_limits : preserves_limits (forget : Top.{u} ⥤ Type u) :=
Top.forget_preserves_limits_of_size.{u u}
/--
A choice of colimit cocone for a functor `F : J ⥤ Top`.
Generally you should just use `colimit.coone F`, unless you need the actual definition
(which is in terms of `types.colimit_cocone`).
-/
def colimit_cocone (F : J ⥤ Top.{max v u}) : cocone F :=
{ X := ⟨(types.colimit_cocone (F ⋙ forget)).X, ⨆ j,
(F.obj j).str.coinduced ((types.colimit_cocone (F ⋙ forget)).ι.app j)⟩,
ι :=
{ app := λ j, ⟨(types.colimit_cocone (F ⋙ forget)).ι.app j,
continuous_iff_coinduced_le.mpr (le_supr _ j)⟩,
naturality' := λ j j' f, continuous_map.coe_injective
((types.colimit_cocone (F ⋙ forget)).ι.naturality f) } }
/--
The chosen cocone `Top.colimit_cocone F` for a functor `F : J ⥤ Top` is a colimit cocone.
Generally you should just use `colimit.is_colimit F`, unless you need the actual definition
(which is in terms of `types.colimit_cocone_is_colimit`).
-/
def colimit_cocone_is_colimit (F : J ⥤ Top.{max v u}) : is_colimit (colimit_cocone F) :=
by { refine is_colimit.of_faithful forget (types.colimit_cocone_is_colimit _) (λ s, ⟨_, _⟩)
(λ s, rfl),
exact continuous_iff_le_induced.mpr (supr_le $ λ j,
coinduced_le_iff_le_induced.mp $ (continuous_iff_coinduced_le.mp (s.ι.app j).continuous :
_) ) }
instance Top_has_colimits_of_size : has_colimits_of_size.{v} Top.{max v u} :=
{ has_colimits_of_shape := λ J 𝒥, by exactI
{ has_colimit := λ F, has_colimit.mk { cocone := colimit_cocone F, is_colimit :=
colimit_cocone_is_colimit F } } }
instance Top_has_colimits : has_colimits Top.{u} := Top.Top_has_colimits_of_size.{u u}
instance forget_preserves_colimits_of_size :
preserves_colimits_of_size.{v v} (forget : Top.{max v u} ⥤ Type (max v u)) :=
{ preserves_colimits_of_shape := λ J 𝒥,
{ preserves_colimit := λ F,
by exactI preserves_colimit_of_preserves_colimit_cocone
(colimit_cocone_is_colimit F) (types.colimit_cocone_is_colimit (F ⋙ forget)) } }
instance forget_preserves_colimits : preserves_colimits (forget : Top.{u} ⥤ Type u) :=
Top.forget_preserves_colimits_of_size.{u u}
/-- The projection from the product as a bundled continous map. -/
abbreviation pi_π {ι : Type v} (α : ι → Top.{max v u}) (i : ι) : Top.of (Π i, α i) ⟶ α i :=
⟨λ f, f i, continuous_apply i⟩
/-- The explicit fan of a family of topological spaces given by the pi type. -/
@[simps X π_app]
def pi_fan {ι : Type v} (α : ι → Top.{max v u}) : fan α :=
fan.mk (Top.of (Π i, α i)) (pi_π α)
/-- The constructed fan is indeed a limit -/
def pi_fan_is_limit {ι : Type v} (α : ι → Top.{max v u}) : is_limit (pi_fan α) :=
{ lift := λ S, { to_fun := λ s i, S.π.app ⟨i⟩ s },
uniq' := by { intros S m h, ext x i, simp [← h ⟨i⟩] },
fac' := λ s j, by { cases j, tidy, }, }
/--
The product is homeomorphic to the product of the underlying spaces,
equipped with the product topology.
-/
def pi_iso_pi {ι : Type v} (α : ι → Top.{max v u}) : ∏ α ≅ Top.of (Π i, α i) :=
(limit.is_limit _).cone_point_unique_up_to_iso (pi_fan_is_limit α)
@[simp, reassoc]
lemma pi_iso_pi_inv_π {ι : Type v} (α : ι → Top.{max v u}) (i : ι) :
(pi_iso_pi α).inv ≫ pi.π α i = pi_π α i :=
by simp [pi_iso_pi]
@[simp]
lemma pi_iso_pi_inv_π_apply {ι : Type v} (α : ι → Top.{max v u}) (i : ι) (x : Π i, α i) :
(pi.π α i : _) ((pi_iso_pi α).inv x) = x i :=
concrete_category.congr_hom (pi_iso_pi_inv_π α i) x
@[simp]
lemma pi_iso_pi_hom_apply {ι : Type v} (α : ι → Top.{max v u}) (i : ι) (x : ∏ α) :
(pi_iso_pi α).hom x i = (pi.π α i : _) x :=
begin
have := pi_iso_pi_inv_π α i,
rw iso.inv_comp_eq at this,
exact concrete_category.congr_hom this x
end
/-- The inclusion to the coproduct as a bundled continous map. -/
abbreviation sigma_ι {ι : Type v} (α : ι → Top.{max v u}) (i : ι) : α i ⟶ Top.of (Σ i, α i) :=
⟨sigma.mk i⟩
/-- The explicit cofan of a family of topological spaces given by the sigma type. -/
@[simps X ι_app]
def sigma_cofan {ι : Type v} (α : ι → Top.{max v u}) : cofan α :=
cofan.mk (Top.of (Σ i, α i)) (sigma_ι α)
/-- The constructed cofan is indeed a colimit -/
def sigma_cofan_is_colimit {ι : Type v} (α : ι → Top.{max v u}) : is_colimit (sigma_cofan α) :=
{ desc := λ S, { to_fun := λ s, S.ι.app ⟨s.1⟩ s.2,
continuous_to_fun := continuous_sigma $ λ i, map_continuous (S.ι.app ⟨i⟩) },
uniq' := by { intros S m h, ext ⟨i, x⟩, simp [← h ⟨i⟩] },
fac' := λ s j, by { cases j, tidy, }, }
/--
The coproduct is homeomorphic to the disjoint union of the topological spaces.
-/
def sigma_iso_sigma {ι : Type v} (α : ι → Top.{max v u}) : ∐ α ≅ Top.of (Σ i, α i) :=
(colimit.is_colimit _).cocone_point_unique_up_to_iso (sigma_cofan_is_colimit α)
@[simp, reassoc]
lemma sigma_iso_sigma_hom_ι {ι : Type v} (α : ι → Top.{max v u}) (i : ι) :
sigma.ι α i ≫ (sigma_iso_sigma α).hom = sigma_ι α i :=
by simp [sigma_iso_sigma]
@[simp]
lemma sigma_iso_sigma_hom_ι_apply {ι : Type v} (α : ι → Top.{max v u}) (i : ι) (x : α i) :
(sigma_iso_sigma α).hom ((sigma.ι α i : _) x) = sigma.mk i x :=
concrete_category.congr_hom (sigma_iso_sigma_hom_ι α i) x
@[simp]
lemma sigma_iso_sigma_inv_apply {ι : Type v} (α : ι → Top.{max v u}) (i : ι) (x : α i) :
(sigma_iso_sigma α).inv ⟨i, x⟩ = (sigma.ι α i : _) x :=
by { rw [← sigma_iso_sigma_hom_ι_apply, ← comp_app], simp, }
lemma induced_of_is_limit {F : J ⥤ Top.{max v u}} (C : cone F) (hC : is_limit C) :
C.X.topological_space = ⨅ j, (F.obj j).topological_space.induced (C.π.app j) :=
begin
let homeo := homeo_of_iso (hC.cone_point_unique_up_to_iso (limit_cone_infi_is_limit F)),
refine homeo.inducing.induced.trans _,
change induced homeo (⨅ (j : J), _) = _,
simpa [induced_infi, induced_compose],
end
lemma limit_topology (F : J ⥤ Top.{max v u}) :
(limit F).topological_space = ⨅ j, (F.obj j).topological_space.induced (limit.π F j) :=
induced_of_is_limit _ (limit.is_limit F)
section prod
/-- The first projection from the product. -/
abbreviation prod_fst {X Y : Top.{u}} : Top.of (X × Y) ⟶ X := ⟨prod.fst⟩
/-- The second projection from the product. -/
abbreviation prod_snd {X Y : Top.{u}} : Top.of (X × Y) ⟶ Y := ⟨prod.snd⟩
/-- The explicit binary cofan of `X, Y` given by `X × Y`. -/
def prod_binary_fan (X Y : Top.{u}) : binary_fan X Y :=
binary_fan.mk prod_fst prod_snd
/-- The constructed binary fan is indeed a limit -/
def prod_binary_fan_is_limit (X Y : Top.{u}) : is_limit (prod_binary_fan X Y) :=
{ lift := λ (S : binary_fan X Y), { to_fun := λ s, (S.fst s, S.snd s) },
fac' := begin
rintros S (_|_),
tidy
end,
uniq' := begin
intros S m h,
ext x,
{ specialize h ⟨walking_pair.left⟩,
apply_fun (λ e, (e x)) at h,
exact h },
{ specialize h ⟨walking_pair.right⟩,
apply_fun (λ e, (e x)) at h,
exact h },
end }
/--
The homeomorphism between `X ⨯ Y` and the set-theoretic product of `X` and `Y`,
equipped with the product topology.
-/
def prod_iso_prod (X Y : Top.{u}) : X ⨯ Y ≅ Top.of (X × Y) :=
(limit.is_limit _).cone_point_unique_up_to_iso (prod_binary_fan_is_limit X Y)
@[simp, reassoc] lemma prod_iso_prod_hom_fst (X Y : Top.{u}) :
(prod_iso_prod X Y).hom ≫ prod_fst = limits.prod.fst :=
by simpa [← iso.eq_inv_comp, prod_iso_prod]
@[simp, reassoc] lemma prod_iso_prod_hom_snd (X Y : Top.{u}) :
(prod_iso_prod X Y).hom ≫ prod_snd = limits.prod.snd :=
by simpa [← iso.eq_inv_comp, prod_iso_prod]
@[simp] lemma prod_iso_prod_hom_apply {X Y : Top.{u}} (x : X ⨯ Y) :
(prod_iso_prod X Y).hom x =
((limits.prod.fst : X ⨯ Y ⟶ _) x, (limits.prod.snd : X ⨯ Y ⟶ _) x) :=
begin
ext,
{ exact concrete_category.congr_hom (prod_iso_prod_hom_fst X Y) x },
{ exact concrete_category.congr_hom (prod_iso_prod_hom_snd X Y) x }
end
@[simp, reassoc, elementwise] lemma prod_iso_prod_inv_fst (X Y : Top.{u}) :
(prod_iso_prod X Y).inv ≫ limits.prod.fst = prod_fst :=
by simp [iso.inv_comp_eq]
@[simp, reassoc, elementwise] lemma prod_iso_prod_inv_snd (X Y : Top.{u}) :
(prod_iso_prod X Y).inv ≫ limits.prod.snd = prod_snd :=
by simp [iso.inv_comp_eq]
lemma prod_topology {X Y : Top} :
(X ⨯ Y).topological_space =
induced (limits.prod.fst : X ⨯ Y ⟶ _) X.topological_space ⊓
induced (limits.prod.snd : X ⨯ Y ⟶ _) Y.topological_space :=
begin
let homeo := homeo_of_iso (prod_iso_prod X Y),
refine homeo.inducing.induced.trans _,
change induced homeo (_ ⊓ _) = _,
simpa [induced_compose]
end
lemma range_prod_map {W X Y Z : Top.{u}} (f : W ⟶ Y) (g : X ⟶ Z) :
set.range (limits.prod.map f g) =
(limits.prod.fst : Y ⨯ Z ⟶ _) ⁻¹' (set.range f) ∩
(limits.prod.snd : Y ⨯ Z ⟶ _) ⁻¹' (set.range g) :=
begin
ext,
split,
{ rintros ⟨y, rfl⟩,
simp only [set.mem_preimage, set.mem_range, set.mem_inter_iff, ←comp_apply],
simp only [limits.prod.map_fst, limits.prod.map_snd,
exists_apply_eq_apply, comp_apply, and_self] },
{ rintros ⟨⟨x₁, hx₁⟩, ⟨x₂, hx₂⟩⟩,
use (prod_iso_prod W X).inv (x₁, x₂),
apply concrete.limit_ext,
rintro ⟨⟨⟩⟩,
{ simp only [← comp_apply, category.assoc], erw limits.prod.map_fst, simp [hx₁] },
{ simp only [← comp_apply, category.assoc], erw limits.prod.map_snd, simp [hx₂] } }
end
lemma inducing_prod_map {W X Y Z : Top} {f : W ⟶ X} {g : Y ⟶ Z}
(hf : inducing f) (hg : inducing g) : inducing (limits.prod.map f g) :=
begin
constructor,
simp only [prod_topology, induced_compose, ←coe_comp, limits.prod.map_fst, limits.prod.map_snd,
induced_inf],
simp only [coe_comp],
rw [← @induced_compose _ _ _ _ _ f, ← @induced_compose _ _ _ _ _ g, ← hf.induced, ← hg.induced]
end
lemma embedding_prod_map {W X Y Z : Top} {f : W ⟶ X} {g : Y ⟶ Z}
(hf : embedding f) (hg : embedding g) : embedding (limits.prod.map f g) :=
⟨inducing_prod_map hf.to_inducing hg.to_inducing,
begin
haveI := (Top.mono_iff_injective _).mpr hf.inj,
haveI := (Top.mono_iff_injective _).mpr hg.inj,
exact (Top.mono_iff_injective _).mp infer_instance
end⟩
end prod
section pullback
variables {X Y Z : Top.{u}}
/-- The first projection from the pullback. -/
abbreviation pullback_fst (f : X ⟶ Z) (g : Y ⟶ Z) : Top.of { p : X × Y // f p.1 = g p.2 } ⟶ X :=
⟨prod.fst ∘ subtype.val⟩
/-- The second projection from the pullback. -/
abbreviation pullback_snd (f : X ⟶ Z) (g : Y ⟶ Z) : Top.of { p : X × Y // f p.1 = g p.2 } ⟶ Y :=
⟨prod.snd ∘ subtype.val⟩
/-- The explicit pullback cone of `X, Y` given by `{ p : X × Y // f p.1 = g p.2 }`. -/
def pullback_cone (f : X ⟶ Z) (g : Y ⟶ Z) : pullback_cone f g :=
pullback_cone.mk (pullback_fst f g) (pullback_snd f g) (by { ext ⟨x, h⟩, simp [h] })
/-- The constructed cone is a limit. -/
def pullback_cone_is_limit (f : X ⟶ Z) (g : Y ⟶ Z) :
is_limit (pullback_cone f g) := pullback_cone.is_limit_aux' _
begin
intro s,
split, swap,
exact { to_fun := λ x, ⟨⟨s.fst x, s.snd x⟩,
by simpa using concrete_category.congr_hom s.condition x⟩ },
refine ⟨_,_,_⟩,
{ ext, delta pullback_cone, simp },
{ ext, delta pullback_cone, simp },
{ intros m h₁ h₂,
ext x,
{ simpa using concrete_category.congr_hom h₁ x },
{ simpa using concrete_category.congr_hom h₂ x } }
end
/-- The pullback of two maps can be identified as a subspace of `X × Y`. -/
def pullback_iso_prod_subtype (f : X ⟶ Z) (g : Y ⟶ Z) :
pullback f g ≅ Top.of { p : X × Y // f p.1 = g p.2 } :=
(limit.is_limit _).cone_point_unique_up_to_iso (pullback_cone_is_limit f g)
@[simp, reassoc] lemma pullback_iso_prod_subtype_inv_fst (f : X ⟶ Z) (g : Y ⟶ Z) :
(pullback_iso_prod_subtype f g).inv ≫ pullback.fst = pullback_fst f g :=
by simpa [pullback_iso_prod_subtype]
@[simp] lemma pullback_iso_prod_subtype_inv_fst_apply (f : X ⟶ Z) (g : Y ⟶ Z)
(x : { p : X × Y // f p.1 = g p.2 }) :
(pullback.fst : pullback f g ⟶ _) ((pullback_iso_prod_subtype f g).inv x) = (x : X × Y).fst :=
concrete_category.congr_hom (pullback_iso_prod_subtype_inv_fst f g) x
@[simp, reassoc] lemma pullback_iso_prod_subtype_inv_snd (f : X ⟶ Z) (g : Y ⟶ Z) :
(pullback_iso_prod_subtype f g).inv ≫ pullback.snd = pullback_snd f g :=
by simpa [pullback_iso_prod_subtype]
@[simp] lemma pullback_iso_prod_subtype_inv_snd_apply (f : X ⟶ Z) (g : Y ⟶ Z)
(x : { p : X × Y // f p.1 = g p.2 }) :
(pullback.snd : pullback f g ⟶ _) ((pullback_iso_prod_subtype f g).inv x) = (x : X × Y).snd :=
concrete_category.congr_hom (pullback_iso_prod_subtype_inv_snd f g) x
lemma pullback_iso_prod_subtype_hom_fst (f : X ⟶ Z) (g : Y ⟶ Z) :
(pullback_iso_prod_subtype f g).hom ≫ pullback_fst f g = pullback.fst :=
by rw [←iso.eq_inv_comp, pullback_iso_prod_subtype_inv_fst]
lemma pullback_iso_prod_subtype_hom_snd (f : X ⟶ Z) (g : Y ⟶ Z) :
(pullback_iso_prod_subtype f g).hom ≫ pullback_snd f g = pullback.snd :=
by rw [←iso.eq_inv_comp, pullback_iso_prod_subtype_inv_snd]
@[simp] lemma pullback_iso_prod_subtype_hom_apply {f : X ⟶ Z} {g : Y ⟶ Z}
(x : pullback f g) : (pullback_iso_prod_subtype f g).hom x =
⟨⟨(pullback.fst : pullback f g ⟶ _) x, (pullback.snd : pullback f g ⟶ _) x⟩,
by simpa using concrete_category.congr_hom pullback.condition x⟩ :=
begin
ext,
exacts [concrete_category.congr_hom (pullback_iso_prod_subtype_hom_fst f g) x,
concrete_category.congr_hom (pullback_iso_prod_subtype_hom_snd f g) x]
end
lemma pullback_topology {X Y Z : Top.{u}} (f : X ⟶ Z) (g : Y ⟶ Z) :
(pullback f g).topological_space =
induced (pullback.fst : pullback f g ⟶ _) X.topological_space ⊓
induced (pullback.snd : pullback f g ⟶ _) Y.topological_space :=
begin
let homeo := homeo_of_iso (pullback_iso_prod_subtype f g),
refine homeo.inducing.induced.trans _,
change induced homeo (induced _ (_ ⊓ _)) = _,
simpa [induced_compose]
end
lemma range_pullback_to_prod {X Y Z : Top} (f : X ⟶ Z) (g : Y ⟶ Z) :
set.range (prod.lift pullback.fst pullback.snd : pullback f g ⟶ X ⨯ Y) =
{ x | (limits.prod.fst ≫ f) x = (limits.prod.snd ≫ g) x } :=
begin
ext x,
split,
{ rintros ⟨y, rfl⟩,
simp only [←comp_apply, set.mem_set_of_eq],
congr' 1,
simp [pullback.condition] },
{ intro h,
use (pullback_iso_prod_subtype f g).inv ⟨⟨_, _⟩, h⟩,
apply concrete.limit_ext,
rintro ⟨⟨⟩⟩; simp, }
end
lemma inducing_pullback_to_prod {X Y Z : Top} (f : X ⟶ Z) (g : Y ⟶ Z) :
inducing ⇑(prod.lift pullback.fst pullback.snd : pullback f g ⟶ X ⨯ Y) :=
⟨by simp [prod_topology, pullback_topology, induced_compose, ←coe_comp]⟩
lemma embedding_pullback_to_prod {X Y Z : Top} (f : X ⟶ Z) (g : Y ⟶ Z) :
embedding ⇑(prod.lift pullback.fst pullback.snd : pullback f g ⟶ X ⨯ Y) :=
⟨inducing_pullback_to_prod f g, (Top.mono_iff_injective _).mp infer_instance⟩
/-- If the map `S ⟶ T` is mono, then there is a description of the image of `W ×ₛ X ⟶ Y ×ₜ Z`. -/
lemma range_pullback_map {W X Y Z S T : Top} (f₁ : W ⟶ S) (f₂ : X ⟶ S)
(g₁ : Y ⟶ T) (g₂ : Z ⟶ T) (i₁ : W ⟶ Y) (i₂ : X ⟶ Z) (i₃ : S ⟶ T) [H₃ : mono i₃]
(eq₁ : f₁ ≫ i₃ = i₁ ≫ g₁) (eq₂ : f₂ ≫ i₃ = i₂ ≫ g₂) :
set.range (pullback.map f₁ f₂ g₁ g₂ i₁ i₂ i₃ eq₁ eq₂) =
(pullback.fst : pullback g₁ g₂ ⟶ _) ⁻¹' (set.range i₁) ∩
(pullback.snd : pullback g₁ g₂ ⟶ _) ⁻¹' (set.range i₂) :=
begin
ext,
split,
{ rintro ⟨y, rfl⟩, simp, },
rintros ⟨⟨x₁, hx₁⟩, ⟨x₂, hx₂⟩⟩,
have : f₁ x₁ = f₂ x₂,
{ apply (Top.mono_iff_injective _).mp H₃,
simp only [←comp_apply, eq₁, eq₂],
simp only [comp_apply, hx₁, hx₂],
simp only [←comp_apply, pullback.condition] },
use (pullback_iso_prod_subtype f₁ f₂).inv ⟨⟨x₁, x₂⟩, this⟩,
apply concrete.limit_ext,
rintros (_|_|_),
{ simp only [Top.comp_app, limit.lift_π_apply, category.assoc, pullback_cone.mk_π_app_one,
hx₁, pullback_iso_prod_subtype_inv_fst_apply, subtype.coe_mk],
simp only [← comp_apply],
congr,
apply limit.w _ walking_cospan.hom.inl },
{ simp [hx₁] },
{ simp [hx₂] },
end
lemma pullback_fst_range {X Y S : Top} (f : X ⟶ S) (g : Y ⟶ S) :
set.range (pullback.fst : pullback f g ⟶ _) = { x : X | ∃ y : Y, f x = g y} :=
begin
ext x,
split,
{ rintro ⟨y, rfl⟩,
use (pullback.snd : pullback f g ⟶ _) y,
exact concrete_category.congr_hom pullback.condition y },
{ rintro ⟨y, eq⟩,
use (Top.pullback_iso_prod_subtype f g).inv ⟨⟨x, y⟩, eq⟩,
simp },
end
lemma pullback_snd_range {X Y S : Top} (f : X ⟶ S) (g : Y ⟶ S) :
set.range (pullback.snd : pullback f g ⟶ _) = { y : Y | ∃ x : X, f x = g y} :=
begin
ext y,
split,
{ rintro ⟨x, rfl⟩,
use (pullback.fst : pullback f g ⟶ _) x,
exact concrete_category.congr_hom pullback.condition x },
{ rintro ⟨x, eq⟩,
use (Top.pullback_iso_prod_subtype f g).inv ⟨⟨x, y⟩, eq⟩,
simp },
end
/--
If there is a diagram where the morphisms `W ⟶ Y` and `X ⟶ Z` are embeddings,
then the induced morphism `W ×ₛ X ⟶ Y ×ₜ Z` is also an embedding.
W ⟶ Y
↘ ↘
S ⟶ T
↗ ↗
X ⟶ Z
-/
lemma pullback_map_embedding_of_embeddings {W X Y Z S T : Top}
(f₁ : W ⟶ S) (f₂ : X ⟶ S) (g₁ : Y ⟶ T) (g₂ : Z ⟶ T) {i₁ : W ⟶ Y} {i₂ : X ⟶ Z}
(H₁ : embedding i₁) (H₂ : embedding i₂) (i₃ : S ⟶ T)
(eq₁ : f₁ ≫ i₃ = i₁ ≫ g₁) (eq₂ : f₂ ≫ i₃ = i₂ ≫ g₂) :
embedding (pullback.map f₁ f₂ g₁ g₂ i₁ i₂ i₃ eq₁ eq₂) :=
begin
refine embedding_of_embedding_compose (continuous_map.continuous_to_fun _)
(show continuous (prod.lift pullback.fst pullback.snd : pullback g₁ g₂ ⟶ Y ⨯ Z), from
continuous_map.continuous_to_fun _) _,
suffices : embedding
(prod.lift pullback.fst pullback.snd ≫ limits.prod.map i₁ i₂ : pullback f₁ f₂ ⟶ _),
{ simpa [←coe_comp] using this },
rw coe_comp,
refine embedding.comp (embedding_prod_map H₁ H₂)
(embedding_pullback_to_prod _ _)
end
/--
If there is a diagram where the morphisms `W ⟶ Y` and `X ⟶ Z` are open embeddings, and `S ⟶ T`
is mono, then the induced morphism `W ×ₛ X ⟶ Y ×ₜ Z` is also an open embedding.
W ⟶ Y
↘ ↘
S ⟶ T
↗ ↗
X ⟶ Z
-/
lemma pullback_map_open_embedding_of_open_embeddings {W X Y Z S T : Top}
(f₁ : W ⟶ S) (f₂ : X ⟶ S) (g₁ : Y ⟶ T) (g₂ : Z ⟶ T) {i₁ : W ⟶ Y} {i₂ : X ⟶ Z}
(H₁ : open_embedding i₁) (H₂ : open_embedding i₂) (i₃ : S ⟶ T) [H₃ : mono i₃]
(eq₁ : f₁ ≫ i₃ = i₁ ≫ g₁) (eq₂ : f₂ ≫ i₃ = i₂ ≫ g₂) :
open_embedding (pullback.map f₁ f₂ g₁ g₂ i₁ i₂ i₃ eq₁ eq₂) :=
begin
split,
{ apply pullback_map_embedding_of_embeddings
f₁ f₂ g₁ g₂ H₁.to_embedding H₂.to_embedding i₃ eq₁ eq₂ },
{ rw range_pullback_map,
apply is_open.inter; apply continuous.is_open_preimage,
continuity,
exacts [H₁.open_range, H₂.open_range] }
end
lemma snd_embedding_of_left_embedding {X Y S : Top}
{f : X ⟶ S} (H : embedding f) (g : Y ⟶ S) :
embedding ⇑(pullback.snd : pullback f g ⟶ Y) :=
begin
convert (homeo_of_iso (as_iso (pullback.snd : pullback (𝟙 S) g ⟶ _))).embedding.comp
(pullback_map_embedding_of_embeddings f g (𝟙 _) g H
(homeo_of_iso (iso.refl _)).embedding (𝟙 _) rfl (by simp)),
erw ←coe_comp,
simp
end
lemma fst_embedding_of_right_embedding {X Y S : Top}
(f : X ⟶ S) {g : Y ⟶ S} (H : embedding g) :
embedding ⇑(pullback.fst : pullback f g ⟶ X) :=
begin
convert (homeo_of_iso (as_iso (pullback.fst : pullback f (𝟙 S) ⟶ _))).embedding.comp
(pullback_map_embedding_of_embeddings f g f (𝟙 _)
(homeo_of_iso (iso.refl _)).embedding H (𝟙 _) rfl (by simp)),
erw ←coe_comp,
simp
end
lemma embedding_of_pullback_embeddings {X Y S : Top}
{f : X ⟶ S} {g : Y ⟶ S} (H₁ : embedding f) (H₂ : embedding g) :
embedding (limit.π (cospan f g) walking_cospan.one) :=
begin
convert H₂.comp (snd_embedding_of_left_embedding H₁ g),
erw ←coe_comp,
congr,
exact (limit.w _ walking_cospan.hom.inr).symm
end
lemma snd_open_embedding_of_left_open_embedding {X Y S : Top}
{f : X ⟶ S} (H : open_embedding f) (g : Y ⟶ S) :
open_embedding ⇑(pullback.snd : pullback f g ⟶ Y) :=
begin
convert (homeo_of_iso (as_iso (pullback.snd : pullback (𝟙 S) g ⟶ _))).open_embedding.comp
(pullback_map_open_embedding_of_open_embeddings f g (𝟙 _) g H
(homeo_of_iso (iso.refl _)).open_embedding (𝟙 _) rfl (by simp)),
erw ←coe_comp,
simp
end
lemma fst_open_embedding_of_right_open_embedding {X Y S : Top}
(f : X ⟶ S) {g : Y ⟶ S} (H : open_embedding g) :
open_embedding ⇑(pullback.fst : pullback f g ⟶ X) :=
begin
convert (homeo_of_iso (as_iso (pullback.fst : pullback f (𝟙 S) ⟶ _))).open_embedding.comp
(pullback_map_open_embedding_of_open_embeddings f g f (𝟙 _)
(homeo_of_iso (iso.refl _)).open_embedding H (𝟙 _) rfl (by simp)),
erw ←coe_comp,
simp
end
/-- If `X ⟶ S`, `Y ⟶ S` are open embeddings, then so is `X ×ₛ Y ⟶ S`. -/
lemma open_embedding_of_pullback_open_embeddings {X Y S : Top}
{f : X ⟶ S} {g : Y ⟶ S} (H₁ : open_embedding f) (H₂ : open_embedding g) :
open_embedding (limit.π (cospan f g) walking_cospan.one) :=
begin
convert H₂.comp (snd_open_embedding_of_left_open_embedding H₁ g),
erw ←coe_comp,
congr,
exact (limit.w _ walking_cospan.hom.inr).symm
end
lemma fst_iso_of_right_embedding_range_subset {X Y S : Top} (f : X ⟶ S) {g : Y ⟶ S}
(hg : embedding g) (H : set.range f ⊆ set.range g) : is_iso (pullback.fst : pullback f g ⟶ X) :=
begin
let : (pullback f g : Top) ≃ₜ X :=
(homeomorph.of_embedding _ (fst_embedding_of_right_embedding f hg)).trans
{ to_fun := coe,
inv_fun := (λ x, ⟨x,
by { rw pullback_fst_range, exact ⟨_, (H (set.mem_range_self x)).some_spec.symm⟩ }⟩),
left_inv := λ ⟨_,_⟩, rfl,
right_inv := λ x, rfl },
convert is_iso.of_iso (iso_of_homeo this),
ext,
refl
end
lemma snd_iso_of_left_embedding_range_subset {X Y S : Top} {f : X ⟶ S} (hf : embedding f)
(g : Y ⟶ S) (H : set.range g ⊆ set.range f) : is_iso (pullback.snd : pullback f g ⟶ Y) :=
begin
let : (pullback f g : Top) ≃ₜ Y :=
(homeomorph.of_embedding _ (snd_embedding_of_left_embedding hf g)).trans
{ to_fun := coe,
inv_fun := (λ x, ⟨x,
by { rw pullback_snd_range, exact ⟨_, (H (set.mem_range_self x)).some_spec⟩ }⟩),
left_inv := λ ⟨_,_⟩, rfl,
right_inv := λ x, rfl },
convert is_iso.of_iso (iso_of_homeo this),
ext,
refl
end
lemma pullback_snd_image_fst_preimage (f : X ⟶ Z) (g : Y ⟶ Z) (U : set X) :
(pullback.snd : pullback f g ⟶ _) '' ((pullback.fst : pullback f g ⟶ _) ⁻¹' U) =
g ⁻¹' (f '' U) :=
begin
ext x,
split,
{ rintros ⟨y, hy, rfl⟩,
exact ⟨(pullback.fst : pullback f g ⟶ _) y, hy,
concrete_category.congr_hom pullback.condition y⟩ },
{ rintros ⟨y, hy, eq⟩,
exact ⟨(Top.pullback_iso_prod_subtype f g).inv ⟨⟨_,_⟩, eq⟩, by simpa, by simp⟩ },
end
lemma pullback_fst_image_snd_preimage (f : X ⟶ Z) (g : Y ⟶ Z) (U : set Y) :
(pullback.fst : pullback f g ⟶ _) '' ((pullback.snd : pullback f g ⟶ _) ⁻¹' U) =
f ⁻¹' (g '' U) :=
begin
ext x,
split,
{ rintros ⟨y, hy, rfl⟩,
exact ⟨(pullback.snd : pullback f g ⟶ _) y, hy,
(concrete_category.congr_hom pullback.condition y).symm⟩ },
{ rintros ⟨y, hy, eq⟩,
exact ⟨(Top.pullback_iso_prod_subtype f g).inv ⟨⟨_,_⟩,eq.symm⟩, by simpa, by simp⟩ },
end
end pullback
/-- The terminal object of `Top` is `punit`. -/
def is_terminal_punit : is_terminal (Top.of punit.{u+1}) :=
begin
haveI : ∀ X, unique (X ⟶ Top.of punit.{u+1}) :=
λ X, ⟨⟨⟨λ x, punit.star, by continuity⟩⟩, λ f, by ext⟩,
exact limits.is_terminal.of_unique _,
end
/-- The terminal object of `Top` is `punit`. -/
def terminal_iso_punit : ⊤_ Top.{u} ≅ Top.of punit :=
terminal_is_terminal.unique_up_to_iso is_terminal_punit
/-- The initial object of `Top` is `pempty`. -/
def is_initial_pempty : is_initial (Top.of pempty.{u+1}) :=
begin
haveI : ∀ X, unique (Top.of pempty.{u+1} ⟶ X) :=
λ X, ⟨⟨⟨λ x, x.elim, by continuity⟩⟩, λ f, by ext ⟨⟩⟩,
exact limits.is_initial.of_unique _,
end
/-- The initial object of `Top` is `pempty`. -/
def initial_iso_pempty : ⊥_ Top.{u} ≅ Top.of pempty :=
initial_is_initial.unique_up_to_iso is_initial_pempty
/-- The binary coproduct cofan in `Top`. -/
protected
def binary_cofan (X Y : Top.{u}) : binary_cofan X Y :=
binary_cofan.mk (⟨sum.inl⟩ : X ⟶ Top.of (X ⊕ Y)) ⟨sum.inr⟩
/-- The constructed binary coproduct cofan in `Top` is the coproduct. -/
def binary_cofan_is_colimit (X Y : Top.{u}) : is_colimit (Top.binary_cofan X Y) :=
begin
refine limits.binary_cofan.is_colimit_mk (λ s, ⟨sum.elim s.inl s.inr⟩) _ _ _,
{ intro s, ext, refl },
{ intro s, ext, refl },
{ intros s m h₁ h₂, ext (x|x),
exacts [(concrete_category.congr_hom h₁ x : _), (concrete_category.congr_hom h₂ x : _)] },
end
lemma binary_cofan_is_colimit_iff {X Y : Top} (c : binary_cofan X Y) :
nonempty (is_colimit c) ↔
open_embedding c.inl ∧ open_embedding c.inr ∧ is_compl (set.range c.inl) (set.range c.inr) :=
begin
classical,
split,
{ rintro ⟨h⟩,
rw [← show _ = c.inl, from h.comp_cocone_point_unique_up_to_iso_inv
(binary_cofan_is_colimit X Y) ⟨walking_pair.left⟩,
← show _ = c.inr, from h.comp_cocone_point_unique_up_to_iso_inv
(binary_cofan_is_colimit X Y) ⟨walking_pair.right⟩],
dsimp,
refine
⟨(homeo_of_iso $ h.cocone_point_unique_up_to_iso (binary_cofan_is_colimit X Y)).symm
.open_embedding.comp open_embedding_inl, (homeo_of_iso $ h.cocone_point_unique_up_to_iso
(binary_cofan_is_colimit X Y)).symm.open_embedding.comp open_embedding_inr, _⟩,
erw [set.range_comp, ← eq_compl_iff_is_compl, set.range_comp _ sum.inr, ← set.image_compl_eq
(homeo_of_iso $ h.cocone_point_unique_up_to_iso (binary_cofan_is_colimit X Y))
.symm.bijective],
congr' 1,
exact set.compl_range_inr.symm },
{ rintros ⟨h₁, h₂, h₃⟩,
have : ∀ x, x ∈ set.range c.inl ∨ x ∈ set.range c.inr,
{ rw [eq_compl_iff_is_compl.mpr h₃.symm], exact λ _, or_not },
refine ⟨binary_cofan.is_colimit.mk _ _ _ _ _⟩,
{ intros T f g,
refine continuous_map.mk _ _,
{ exact λ x, if h : x ∈ set.range c.inl
then f ((equiv.of_injective _ h₁.inj).symm ⟨x, h⟩)
else g ((equiv.of_injective _ h₂.inj).symm ⟨x, (this x).resolve_left h⟩) },
rw continuous_iff_continuous_at,
intro x,
by_cases x ∈ set.range c.inl,
{ revert h x,
apply (is_open.continuous_on_iff _).mp,
{ rw continuous_on_iff_continuous_restrict,
convert_to continuous (f ∘ (homeomorph.of_embedding _ h₁.to_embedding).symm),
{ ext ⟨x, hx⟩, exact dif_pos hx },
continuity },
{ exact h₁.open_range } },
{ revert h x,
apply (is_open.continuous_on_iff _).mp,
{ rw continuous_on_iff_continuous_restrict,
have : ∀ a, a ∉ set.range c.inl → a ∈ set.range c.inr,
{ rintros a (h : a ∈ (set.range c.inl)ᶜ), rwa eq_compl_iff_is_compl.mpr h₃.symm },
convert_to continuous
(g ∘ (homeomorph.of_embedding _ h₂.to_embedding).symm ∘ subtype.map _ this),
{ ext ⟨x, hx⟩, exact dif_neg hx },
continuity,
rw embedding_subtype_coe.to_inducing.continuous_iff,
exact continuous_subtype_coe },
{ change is_open (set.range c.inl)ᶜ, rw ← eq_compl_iff_is_compl.mpr h₃.symm,
exact h₂.open_range } } },
{ intros T f g, ext x, refine (dif_pos _).trans _, { exact ⟨x, rfl⟩ },
{ rw equiv.of_injective_symm_apply } },
{ intros T f g, ext x, refine (dif_neg _).trans _,
{ rintro ⟨y, e⟩, have : c.inr x ∈ set.range c.inl ⊓ set.range c.inr := ⟨⟨_, e⟩, ⟨_, rfl⟩⟩,
rwa disjoint_iff.mp h₃.1 at this },
{ exact congr_arg g (equiv.of_injective_symm_apply _ _) } },
{ rintro T _ _ m rfl rfl, ext x, change m x = dite _ _ _,
split_ifs; exact congr_arg _ (equiv.apply_of_injective_symm _ ⟨_, _⟩).symm } }
end
--TODO: Add analogous constructions for `pushout`.
lemma coinduced_of_is_colimit {F : J ⥤ Top.{max v u}} (c : cocone F) (hc : is_colimit c) :
c.X.topological_space = ⨆ j, (F.obj j).topological_space.coinduced (c.ι.app j) :=
begin
let homeo := homeo_of_iso (hc.cocone_point_unique_up_to_iso (colimit_cocone_is_colimit F)),
ext,
refine homeo.symm.is_open_preimage.symm.trans (iff.trans _ is_open_supr_iff.symm),
exact is_open_supr_iff
end
lemma colimit_topology (F : J ⥤ Top.{max v u}) :
(colimit F).topological_space = ⨆ j, (F.obj j).topological_space.coinduced (colimit.ι F j) :=
coinduced_of_is_colimit _ (colimit.is_colimit F)
lemma colimit_is_open_iff (F : J ⥤ Top.{max v u}) (U : set ((colimit F : _) : Type (max v u))) :
is_open U ↔ ∀ j, is_open (colimit.ι F j ⁻¹' U) :=
begin
conv_lhs { rw colimit_topology F },
exact is_open_supr_iff
end
lemma coequalizer_is_open_iff (F : walking_parallel_pair ⥤ Top.{u})
(U : set ((colimit F : _) : Type u)) :
is_open U ↔ is_open (colimit.ι F walking_parallel_pair.one ⁻¹' U) :=
begin
rw colimit_is_open_iff.{u},
split,
{ intro H, exact H _ },
{ intros H j,
cases j,
{ rw ←colimit.w F walking_parallel_pair_hom.left,
exact (F.map walking_parallel_pair_hom.left).continuous_to_fun.is_open_preimage _ H },
{ exact H } }
end
end Top
namespace Top
section cofiltered_limit
variables {J : Type v} [small_category J] [is_cofiltered J] (F : J ⥤ Top.{max v u})
(C : cone F) (hC : is_limit C)
include hC
/--
Given a *compatible* collection of topological bases for the factors in a cofiltered limit
which contain `set.univ` and are closed under intersections, the induced *naive* collection
of sets in the limit is, in fact, a topological basis.
-/
theorem is_topological_basis_cofiltered_limit
(T : Π j, set (set (F.obj j))) (hT : ∀ j, is_topological_basis (T j))
(univ : ∀ (i : J), set.univ ∈ T i)
(inter : ∀ i (U1 U2 : set (F.obj i)), U1 ∈ T i → U2 ∈ T i → U1 ∩ U2 ∈ T i)
(compat : ∀ (i j : J) (f : i ⟶ j) (V : set (F.obj j)) (hV : V ∈ T j), (F.map f) ⁻¹' V ∈ T i) :
is_topological_basis { U : set C.X | ∃ j (V : set (F.obj j)), V ∈ T j ∧ U = C.π.app j ⁻¹' V } :=
begin
classical,
-- The limit cone for `F` whose topology is defined as an infimum.
let D := limit_cone_infi F,
-- The isomorphism between the cone point of `C` and the cone point of `D`.
let E : C.X ≅ D.X := hC.cone_point_unique_up_to_iso (limit_cone_infi_is_limit _),
have hE : inducing E.hom := (Top.homeo_of_iso E).inducing,
-- Reduce to the assertion of the theorem with `D` instead of `C`.
suffices : is_topological_basis
{ U : set D.X | ∃ j (V : set (F.obj j)), V ∈ T j ∧ U = D.π.app j ⁻¹' V },
{ convert this.inducing hE,
ext U0,
split,
{ rintro ⟨j, V, hV, rfl⟩,
refine ⟨D.π.app j ⁻¹' V, ⟨j, V, hV, rfl⟩, rfl⟩ },
{ rintro ⟨W, ⟨j, V, hV, rfl⟩, rfl⟩,
refine ⟨j, V, hV, rfl⟩ } },
-- Using `D`, we can apply the characterization of the topological basis of a
-- topology defined as an infimum...
convert is_topological_basis_infi hT (λ j (x : D.X), D.π.app j x),
ext U0,
split,
{ rintros ⟨j, V, hV, rfl⟩,
let U : Π i, set (F.obj i) := λ i, if h : i = j then (by {rw h, exact V}) else set.univ,
refine ⟨U,{j},_,_⟩,
{ rintro i h,
rw finset.mem_singleton at h,
dsimp [U],
rw dif_pos h,
subst h,
exact hV },
{ dsimp [U],
simp } },
{ rintros ⟨U, G, h1, h2⟩,
obtain ⟨j, hj⟩ := is_cofiltered.inf_objs_exists G,
let g : ∀ e (he : e ∈ G), j ⟶ e := λ _ he, (hj he).some,
let Vs : J → set (F.obj j) := λ e, if h : e ∈ G then F.map (g e h) ⁻¹' (U e) else set.univ,
let V : set (F.obj j) := ⋂ (e : J) (he : e ∈ G), Vs e,
refine ⟨j, V, _, _⟩,
{ -- An intermediate claim used to apply induction along `G : finset J` later on.
have : ∀ (S : set (set (F.obj j))) (E : finset J) (P : J → set (F.obj j))
(univ : set.univ ∈ S)
(inter : ∀ A B : set (F.obj j), A ∈ S → B ∈ S → A ∩ B ∈ S)
(cond : ∀ (e : J) (he : e ∈ E), P e ∈ S), (⋂ e (he : e ∈ E), P e) ∈ S,
{ intros S E,
apply E.induction_on,
{ intros P he hh,
simpa },
{ intros a E ha hh1 hh2 hh3 hh4 hh5,
rw finset.set_bInter_insert,
refine hh4 _ _ (hh5 _ (finset.mem_insert_self _ _)) (hh1 _ hh3 hh4 _),
intros e he,
exact hh5 e (finset.mem_insert_of_mem he) } },
-- use the intermediate claim to finish off the goal using `univ` and `inter`.
refine this _ _ _ (univ _) (inter _) _,
intros e he,
dsimp [Vs],
rw dif_pos he,
exact compat j e (g e he) (U e) (h1 e he), },
{ -- conclude...
rw h2,
dsimp [V],
rw set.preimage_Inter,
congr' 1,
ext1 e,
rw set.preimage_Inter,
congr' 1,
ext1 he,
dsimp [Vs],
rw [dif_pos he, ← set.preimage_comp],
congr' 1,
change _ = ⇑(D.π.app j ≫ F.map (g e he)),
rw D.w } }
end
end cofiltered_limit
section topological_konig
/-!
## Topological Kőnig's lemma
A topological version of Kőnig's lemma is that the inverse limit of nonempty compact Hausdorff
spaces is nonempty. (Note: this can be generalized further to inverse limits of nonempty compact
T0 spaces, where all the maps are closed maps; see [Stone1979] --- however there is an erratum
for Theorem 4 that the element in the inverse limit can have cofinally many components that are
not closed points.)
We give this in a more general form, which is that cofiltered limits
of nonempty compact Hausdorff spaces are nonempty
(`nonempty_limit_cone_of_compact_t2_cofiltered_system`).
This also applies to inverse limits, where `{J : Type u} [preorder J] [is_directed J (≤)]` and
`F : Jᵒᵖ ⥤ Top`.
The theorem is specialized to nonempty finite types (which are compact Hausdorff with the
discrete topology) in `nonempty_sections_of_fintype_cofiltered_system` and
`nonempty_sections_of_fintype_inverse_system`.
(See <https://stacks.math.columbia.edu/tag/086J> for the Set version.)
-/
variables {J : Type u} [small_category J]
variables (F : J ⥤ Top.{u})
private abbreviation finite_diagram_arrow {J : Type u} [small_category J] (G : finset J) :=
Σ' (X Y : J) (mX : X ∈ G) (mY : Y ∈ G), X ⟶ Y
private abbreviation finite_diagram (J : Type u) [small_category J] :=
Σ (G : finset J), finset (finite_diagram_arrow G)
/--
Partial sections of a cofiltered limit are sections when restricted to
a finite subset of objects and morphisms of `J`.
-/
def partial_sections {J : Type u} [small_category J] (F : J ⥤ Top.{u})
{G : finset J} (H : finset (finite_diagram_arrow G)) : set (Π j, F.obj j) :=
{ u | ∀ {f : finite_diagram_arrow G} (hf : f ∈ H), F.map f.2.2.2.2 (u f.1) = u f.2.1 }
lemma partial_sections.nonempty [is_cofiltered J] [h : Π (j : J), nonempty (F.obj j)]
{G : finset J} (H : finset (finite_diagram_arrow G)) :
(partial_sections F H).nonempty :=
begin
classical,
use λ (j : J), if hj : j ∈ G
then F.map (is_cofiltered.inf_to G H hj) (h (is_cofiltered.inf G H)).some
else (h _).some,
rintros ⟨X, Y, hX, hY, f⟩ hf,
dsimp only,
rwa [dif_pos hX, dif_pos hY, ←comp_app, ←F.map_comp,
@is_cofiltered.inf_to_commutes _ _ _ G H],
end
lemma partial_sections.directed :
directed superset (λ (G : finite_diagram J), partial_sections F G.2) :=
begin
classical,
intros A B,
let ιA : finite_diagram_arrow A.1 → finite_diagram_arrow (A.1 ⊔ B.1) :=
λ f, ⟨f.1, f.2.1, finset.mem_union_left _ f.2.2.1, finset.mem_union_left _ f.2.2.2.1,
f.2.2.2.2⟩,
let ιB : finite_diagram_arrow B.1 → finite_diagram_arrow (A.1 ⊔ B.1) :=
λ f, ⟨f.1, f.2.1, finset.mem_union_right _ f.2.2.1, finset.mem_union_right _ f.2.2.2.1,
f.2.2.2.2⟩,
refine ⟨⟨A.1 ⊔ B.1, A.2.image ιA ⊔ B.2.image ιB⟩, _, _⟩,
{ rintro u hu f hf,
have : ιA f ∈ A.2.image ιA ⊔ B.2.image ιB,
{ apply finset.mem_union_left,
rw finset.mem_image,
refine ⟨f, hf, rfl⟩ },
exact hu this },
{ rintro u hu f hf,
have : ιB f ∈ A.2.image ιA ⊔ B.2.image ιB,
{ apply finset.mem_union_right,
rw finset.mem_image,
refine ⟨f, hf, rfl⟩ },
exact hu this }
end
lemma partial_sections.closed [Π (j : J), t2_space (F.obj j)]
{G : finset J} (H : finset (finite_diagram_arrow G)) :
is_closed (partial_sections F H) :=
begin
have : partial_sections F H =
⋂ {f : finite_diagram_arrow G} (hf : f ∈ H), { u | F.map f.2.2.2.2 (u f.1) = u f.2.1 },
{ ext1,
simp only [set.mem_Inter, set.mem_set_of_eq],
refl, },
rw this,
apply is_closed_bInter,
intros f hf,
apply is_closed_eq,
continuity,
end
/--
Cofiltered limits of nonempty compact Hausdorff spaces are nonempty topological spaces.
-/
lemma nonempty_limit_cone_of_compact_t2_cofiltered_system
[is_cofiltered J]
[Π (j : J), nonempty (F.obj j)]
[Π (j : J), compact_space (F.obj j)]
[Π (j : J), t2_space (F.obj j)] :
nonempty (Top.limit_cone.{u} F).X :=
begin
classical,
obtain ⟨u, hu⟩ := is_compact.nonempty_Inter_of_directed_nonempty_compact_closed
(λ G, partial_sections F _)
(partial_sections.directed F)
(λ G, partial_sections.nonempty F _)
(λ G, is_closed.is_compact (partial_sections.closed F _))
(λ G, partial_sections.closed F _),
use u,
intros X Y f,
let G : finite_diagram J :=
⟨{X, Y},
{⟨X, Y,
by simp only [true_or, eq_self_iff_true, finset.mem_insert],
by simp only [eq_self_iff_true, or_true, finset.mem_insert, finset.mem_singleton],
f⟩}⟩,
exact hu _ ⟨G, rfl⟩ (finset.mem_singleton_self _),
end
end topological_konig
end Top
section fintype_konig
/-- This bootstraps `nonempty_sections_of_fintype_inverse_system`. In this version,
the `F` functor is between categories of the same universe, and it is an easy
corollary to `Top.nonempty_limit_cone_of_compact_t2_inverse_system`. -/
lemma nonempty_sections_of_fintype_cofiltered_system.init
{J : Type u} [small_category J] [is_cofiltered J] (F : J ⥤ Type u)
[hf : Π (j : J), fintype (F.obj j)] [hne : Π (j : J), nonempty (F.obj j)] :
F.sections.nonempty :=
begin
let F' : J ⥤ Top := F ⋙ Top.discrete,
haveI : Π (j : J), fintype (F'.obj j) := hf,
haveI : Π (j : J), nonempty (F'.obj j) := hne,
obtain ⟨⟨u, hu⟩⟩ := Top.nonempty_limit_cone_of_compact_t2_cofiltered_system F',
exact ⟨u, λ _ _ f, hu f⟩,
end
/-- The cofiltered limit of nonempty finite types is nonempty.
See `nonempty_sections_of_fintype_inverse_system` for a specialization to inverse limits. -/
theorem nonempty_sections_of_fintype_cofiltered_system
{J : Type u} [category.{w} J] [is_cofiltered J] (F : J ⥤ Type v)
[Π (j : J), fintype (F.obj j)] [Π (j : J), nonempty (F.obj j)] :
F.sections.nonempty :=
begin
-- Step 1: lift everything to the `max u v w` universe.
let J' : Type (max w v u) := as_small.{max w v} J,
let down : J' ⥤ J := as_small.down,
let F' : J' ⥤ Type (max u v w) := down ⋙ F ⋙ ulift_functor.{(max u w) v},
haveI : ∀ i, nonempty (F'.obj i) := λ i, ⟨⟨classical.arbitrary (F.obj (down.obj i))⟩⟩,
haveI : ∀ i, fintype (F'.obj i) := λ i, fintype.of_equiv (F.obj (down.obj i)) equiv.ulift.symm,
-- Step 2: apply the bootstrap theorem
obtain ⟨u, hu⟩ := nonempty_sections_of_fintype_cofiltered_system.init F',
-- Step 3: interpret the results
use λ j, (u ⟨j⟩).down,
intros j j' f,
have h := @hu (⟨j⟩ : J') (⟨j'⟩ : J') (ulift.up f),
simp only [as_small.down, functor.comp_map, ulift_functor_map, functor.op_map] at h,
simp_rw [←h],
refl,
end
/-- The inverse limit of nonempty finite types is nonempty.
See `nonempty_sections_of_fintype_cofiltered_system` for a generalization to cofiltered limits.
That version applies in almost all cases, and the only difference is that this version
allows `J` to be empty.
This may be regarded as a generalization of Kőnig's lemma.
To specialize: given a locally finite connected graph, take `Jᵒᵖ` to be `ℕ` and
`F j` to be length-`j` paths that start from an arbitrary fixed vertex.
Elements of `F.sections` can be read off as infinite rays in the graph. -/
theorem nonempty_sections_of_fintype_inverse_system
{J : Type u} [preorder J] [is_directed J (≤)] (F : Jᵒᵖ ⥤ Type v)
[Π (j : Jᵒᵖ), fintype (F.obj j)] [Π (j : Jᵒᵖ), nonempty (F.obj j)] :
F.sections.nonempty :=
begin
casesI is_empty_or_nonempty J,
{ haveI : is_empty Jᵒᵖ := ⟨λ j, is_empty_elim j.unop⟩, -- TODO: this should be a global instance
exact ⟨is_empty_elim, is_empty_elim⟩, },
{ exact nonempty_sections_of_fintype_cofiltered_system _, },
end
end fintype_konig
|
1ca330bba196d6fd996924bfabecba0f91f47e4b | 037dba89703a79cd4a4aec5e959818147f97635d | /src/2021/logic/sheet1.lean | 089a979cbcfe37e197d09a9efce1478f335b87ef | [] | no_license | ImperialCollegeLondon/M40001_lean | 3a6a09298da395ab51bc220a535035d45bbe919b | 62a76fa92654c855af2b2fc2bef8e60acd16ccec | refs/heads/master | 1,666,750,403,259 | 1,665,771,117,000 | 1,665,771,117,000 | 209,141,835 | 115 | 12 | null | 1,640,270,596,000 | 1,568,749,174,000 | Lean | UTF-8 | Lean | false | false | 5,649 | lean | /-
Copyright (c) 2021 Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author : Kevin Buzzard
-/
import tactic -- imports all the Lean tactics
/-!
# Logic in Lean, example sheet 1 : "implies" (`→`)
We learn about propositions, and implications `P → Q` between them. You can get
this arrow by typing `\to` or `\r`. Mathematicians usually write the
implication arrow as `P ⇒ Q` but Lean prefers a single arrow.
## The absolute basics
`P : Prop` means that `P` is a true-false statement. `h : P` means
that `h` is a proof that `P` is true, or you can regard `h` as an
assumption that `P` is true; logically these are the same. Stuff above
the `⊢` symbol is your assumptions. The statement to the right of it is
the goal. Your job is to prove the goal from the assumptions.
## Tactics you will need
To solve the levels on this sheet you will need to know how to use the
following tactics:
* `intro`
* `exact`
* `apply`
### The `intro` tactic
Mathematical background: `intro h,` says "To prove `P → Q`, you can assume
that `P` is true (call this assumption `h`) and then it
suffices to prove `Q`."
Lean: If your goal is `⊢ P → Q` then `intro h,` will introduce a
hypothesis `h : P` (i.e. a hypothesis that `P` is true)
and change the goal to `⊢ Q`.
### The `exact` tactic
Mathematics: If you have an assumption `h` that `P` is true,
and your goal is to prove that `P` is true, then `exact h,`
will solve this goal.
Lean: If your goal is `⊢ P` and you have a hypothesis `h : P`
then `exact h,` will solve it.
### The `apply` tactic
Mathematics: `apply` is *arguing backwards*. It is like "it suffices to...".
If you're trying to prove `Q`, and you know `h : P → Q` is true, then it
suffices to prove `P`. So `apply h,` *changes the goal* from `Q` to `P`. The key
point to remember is that `apply h` will only work when `h` is an implication,
and it will only work when the *conclusion* of `h` *matches the goal*.
Lean: If your goal is `⊢ Q` and you have `h : P → Q` then `apply h,` will
change the goal to `⊢ P`.
-/
/-
## Worked examples
Click around in the proofs to see the tactic state (on the right) change.
The tactic is implemented and the state changes just before the comma.
I will use the following conventions: variables with capital
letters like `P`, `Q`, `R` denote propositions
(i.e. true/false statements) and variables whose names begin
with `h` like `h1` or `hP` are proofs or hypotheses.
-/
-- Throughout this sheet, `P`, `Q` and `R` will denote propositions.
variables (P Q R : Prop)
-- Here are some examples of `intro`, `exact` and `apply` being used.
-- Assume that `P` and `Q` and `R` are all true. Deduce that `P` is true.
example (hP : P) (hQ : Q) (hR : R) : P :=
begin
-- note that `exact P` does *not* work. `P` is the proposition, `hP` is the proof.
exact hP,
end
-- Assume `Q` is true. Prove that `P → Q`.
example (hQ : Q) : P → Q :=
begin
-- The goal is of the form `X → Y` so we can use `intro`
intro h, -- now `h` is the hypothesis that `P` is true.
-- Our goal is now the same as a hypothesis so we can use `exact`
exact hQ,
end
-- Assume `P → Q` and `P` is true. Deduce `Q`.
example (h : P → Q) (hP : P) : Q :=
begin
-- our goal is `⊢ Q` which matches with the conclusion of `h` so `apply` works
apply h,
-- now our goal has changed to `P` which is an assumption
exact hP,
end
/-
## Examples for you to try
Delete the `sorry`s and replace them with comma-separated tactic proofs
using `intro`, `exact` and `apply`.
-/
/-- Every proposition implies itself. -/
example : P → P :=
begin
sorry
end
/-
Note that `→` is not associative: in general `P → (Q → R)` and `(P → Q) → R`
might not be equivalent. This is like subtraction on numbers -- in general
`a - (b - c)` and `(a - b) - c` might not be equal.
So if we write `P → Q → R` then we'd better know what this means.
The convention in Lean is that it means `P → (Q → R)`. If you think
about it, this means that to deduce `R` you will need to prove both `P`
and `Q`. In general to prove `P1 → P2 → P3 → ... Pn` you can assume
`P1`, `P2`,...,`P(n-1)` and then you have to prove `Pn`.
So the next level is asking you prove that `P → (Q → P)`.
-/
example : P → Q → P :=
begin
sorry
end
/-- If we know `P`, and we also know `P → Q`, we can deduce `Q`.
This is called "Modus Ponens" by logicians. -/
example : P → (P → Q) → Q :=
begin
sorry
end
/-- `→` is transitive. That is, if `P → Q` and `Q → R` are true, then
so is `P → R`. -/
example : (P → Q) → (Q → R) → (P → R) :=
begin
sorry,
end
-- If `h : P → Q → R` with goal `⊢ R` and you `apply h`, you'll get
-- two goals! Note that tactics operate on only the first goal.
example : (P → Q → R) → (P → Q) → (P → R) :=
begin
sorry
end
-- Now they get a little harder. You can skip these if
-- you feel like you know what you're doing.
variables (S T : Prop)
example : (P → R) → (S → Q) → (R → T) → (Q → R) → S → T :=
begin
sorry
end
example : (P → Q) → ((P → Q) → P) → Q :=
begin
sorry
end
example : ((P → Q) → R) → ((Q → R) → P) → ((R → P) → Q) → P :=
begin
sorry
end
example : ((Q → P) → P) → (Q → R) → (R → P) → P :=
begin
sorry
end
example : (((P → Q) → Q) → Q) → (P → Q) :=
begin
sorry
end
example :
(((P → Q → Q) → ((P → Q) → Q)) → R) →
((((P → P) → Q) → (P → P → Q)) → R) →
(((P → P → Q) → ((P → P) → Q)) → R) → R :=
begin
sorry
end
|
5dcfb38833a0eac202bcb0eb978c76c32df6d85e | 6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf | /src/game/world10/level3.lean | 322223957abb5dd30edf16bcb3a61109d2bef77d | [
"Apache-2.0"
] | permissive | arolihas/natural_number_game | 4f0c93feefec93b8824b2b96adff8b702b8b43ce | 8e4f7b4b42888a3b77429f90cce16292bd288138 | refs/heads/master | 1,621,872,426,808 | 1,586,270,467,000 | 1,586,270,467,000 | 253,648,466 | 0 | 0 | null | 1,586,219,694,000 | 1,586,219,694,000 | null | UTF-8 | Lean | false | false | 2,170 | lean | import game.world10.level2 -- hide
namespace mynat -- hide
/-
# Inequality world.
## Level 3: `le_succ_of_le`
We have seen how the `use` tactic makes progress on goals of the form `⊢ ∃ c, ...`.
But what do we do when we have a *hypothesis* of the form `h : ∃ c, ...`?
The hypothesis claims that there exists some natural number `c` with some
property. How are we going to get to that natural number `c`? It turns out
that the `cases` tactic can be used (just like it was used to extract
information from `∧` and `∨` and `↔` hypotheses). Let me talk you through
the proof of $a\le b\implies a\le\operatorname{succ}(b)$.
The goal is an implication so we clearly want to start with
`intro h,`
. After this, if you *want*, you can do something like
`rw le_iff_exists_add at h ⊢,`
(get the sideways T with `\|-` then space). This changes the `≤` into
its `∃` form in `h` and the goal -- but if you are happy with just
*imagining* the `∃` whenever you read a `≤` then you don't need to do this line.
Our hypothesis `h` is now `∃ (c : mynat), b = a + c` (or `a ≤ b` if you
elected not to do the definitional rewriting) so
`cases h with c hc,`
gives you the natural number `c` and the hypothesis `hc : b = a + c`.
Now use `use` wisely and you're home.
-/
/- Lemma
For all naturals $a$, $b$, if $a\leq b$ then $a\leq \operatorname{succ}(b)$.
-/
theorem le_succ (a b : mynat) : a ≤ b → a ≤ (succ b) :=
begin [nat_num_game]
end
/-
Did you use `succ c` or `c + 1` or `1 + c`? Those numbers are all
equal, right? So it doesn't matter which one you use, right?
Here's an interesting question. If you copy the proof below into
the box above, and then fill in the `???`
below with `succ c`, will this proof compile? (move your cursor to
after the final comma to see what Lean thinks). What about if you
`use 1 + c`? What about if you `use c + 1`? Can you work out
what is going on? Does it help if I tell you that the *definition*
of `1` is `succ 0`?
```
theorem le_succ (a b : mynat) : a ≤ b → a ≤ (succ b) :=
begin [nat_num_game]
intro h,
cases h with c hc,
rw hc,
use ???,
refl,
end
```
-/
end mynat -- hide
|
0cded5442131ad6925908ce7a070acd7fc2f747d | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/data/list/perm.lean | 824347302b6d809862fac3ca971b984eac6f8f36 | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 54,133 | 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, Jeremy Avigad, Mario Carneiro
-/
import data.list.bag_inter
import data.list.erase_dup
import data.list.zip
import logic.relation
/-!
# List Permutations
This file introduces the `list.perm` relation, which is true if two lists are permutations of one
another.
## Notation
The notation `~` is used for permutation equivalence.
-/
open_locale nat
namespace list
universe variables uu vv
variables {α : Type uu} {β : Type vv}
/-- `perm l₁ l₂` or `l₁ ~ l₂` asserts that `l₁` and `l₂` are permutations
of each other. This is defined by induction using pairwise swaps. -/
inductive perm : list α → list α → Prop
| nil : perm [] []
| cons : Π (x : α) {l₁ l₂ : list α}, perm l₁ l₂ → perm (x::l₁) (x::l₂)
| swap : Π (x y : α) (l : list α), perm (y::x::l) (x::y::l)
| trans : Π {l₁ l₂ l₃ : list α}, perm l₁ l₂ → perm l₂ l₃ → perm l₁ l₃
open perm (swap)
infix ` ~ `:50 := perm
@[refl] protected theorem perm.refl : ∀ (l : list α), l ~ l
| [] := perm.nil
| (x::xs) := (perm.refl xs).cons x
@[symm] protected theorem perm.symm {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₂ ~ l₁ :=
perm.rec_on p
perm.nil
(λ x l₁ l₂ p₁ r₁, r₁.cons x)
(λ x y l, swap y x l)
(λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, r₂.trans r₁)
theorem perm_comm {l₁ l₂ : list α} : l₁ ~ l₂ ↔ l₂ ~ l₁ := ⟨perm.symm, perm.symm⟩
theorem perm.swap'
(x y : α) {l₁ l₂ : list α} (p : l₁ ~ l₂) : y::x::l₁ ~ x::y::l₂ :=
(swap _ _ _).trans ((p.cons _).cons _)
attribute [trans] perm.trans
theorem perm.eqv (α) : equivalence (@perm α) :=
mk_equivalence (@perm α) (@perm.refl α) (@perm.symm α) (@perm.trans α)
instance is_setoid (α) : setoid (list α) :=
setoid.mk (@perm α) (perm.eqv α)
theorem perm.subset {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₁ ⊆ l₂ :=
λ a, perm.rec_on p
(λ h, h)
(λ x l₁ l₂ p₁ r₁ i, or.elim i
(λ ax, by simp [ax])
(λ al₁, or.inr (r₁ al₁)))
(λ x y l ayxl, or.elim ayxl
(λ ay, by simp [ay])
(λ axl, or.elim axl
(λ ax, by simp [ax])
(λ al, or.inr (or.inr al))))
(λ l₁ l₂ l₃ p₁ p₂ r₁ r₂ ainl₁, r₂ (r₁ ainl₁))
theorem perm.mem_iff {a : α} {l₁ l₂ : list α} (h : l₁ ~ l₂) : a ∈ l₁ ↔ a ∈ l₂ :=
iff.intro (λ m, h.subset m) (λ m, h.symm.subset m)
theorem perm.append_right {l₁ l₂ : list α} (t₁ : list α) (p : l₁ ~ l₂) : l₁++t₁ ~ l₂++t₁ :=
perm.rec_on p
(perm.refl ([] ++ t₁))
(λ x l₁ l₂ p₁ r₁, r₁.cons x)
(λ x y l, swap x y _)
(λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, r₁.trans r₂)
theorem perm.append_left {t₁ t₂ : list α} : ∀ (l : list α), t₁ ~ t₂ → l++t₁ ~ l++t₂
| [] p := p
| (x::xs) p := (perm.append_left xs p).cons x
theorem perm.append {l₁ l₂ t₁ t₂ : list α} (p₁ : l₁ ~ l₂) (p₂ : t₁ ~ t₂) : l₁++t₁ ~ l₂++t₂ :=
(p₁.append_right t₁).trans (p₂.append_left l₂)
theorem perm.append_cons (a : α) {h₁ h₂ t₁ t₂ : list α}
(p₁ : h₁ ~ h₂) (p₂ : t₁ ~ t₂) : h₁ ++ a::t₁ ~ h₂ ++ a::t₂ :=
p₁.append (p₂.cons a)
@[simp] theorem perm_middle {a : α} : ∀ {l₁ l₂ : list α}, l₁++a::l₂ ~ a::(l₁++l₂)
| [] l₂ := perm.refl _
| (b::l₁) l₂ := ((@perm_middle l₁ l₂).cons _).trans (swap a b _)
@[simp] theorem perm_append_singleton (a : α) (l : list α) : l ++ [a] ~ a::l :=
perm_middle.trans $ by rw [append_nil]
theorem perm_append_comm : ∀ {l₁ l₂ : list α}, (l₁++l₂) ~ (l₂++l₁)
| [] l₂ := by simp
| (a::t) l₂ := (perm_append_comm.cons _).trans perm_middle.symm
theorem concat_perm (l : list α) (a : α) : concat l a ~ a :: l :=
by simp
theorem perm.length_eq {l₁ l₂ : list α} (p : l₁ ~ l₂) : length l₁ = length l₂ :=
perm.rec_on p
rfl
(λ x l₁ l₂ p r, by simp[r])
(λ x y l, by simp)
(λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, eq.trans r₁ r₂)
theorem perm.eq_nil {l : list α} (p : l ~ []) : l = [] :=
eq_nil_of_length_eq_zero p.length_eq
theorem perm.nil_eq {l : list α} (p : [] ~ l) : [] = l :=
p.symm.eq_nil.symm
@[simp]
theorem perm_nil {l₁ : list α} : l₁ ~ [] ↔ l₁ = [] :=
⟨λ p, p.eq_nil, λ e, e ▸ perm.refl _⟩
@[simp]
theorem nil_perm {l₁ : list α} : [] ~ l₁ ↔ l₁ = [] :=
perm_comm.trans perm_nil
theorem not_perm_nil_cons (x : α) (l : list α) : ¬ [] ~ x::l
| p := by injection p.symm.eq_nil
@[simp] theorem reverse_perm : ∀ (l : list α), reverse l ~ l
| [] := perm.nil
| (a::l) := by { rw reverse_cons,
exact (perm_append_singleton _ _).trans ((reverse_perm l).cons a) }
theorem perm_cons_append_cons {l l₁ l₂ : list α} (a : α) (p : l ~ l₁++l₂) :
a::l ~ l₁++(a::l₂) :=
(p.cons a).trans perm_middle.symm
@[simp] theorem perm_repeat {a : α} {n : ℕ} {l : list α} : l ~ repeat a n ↔ l = repeat a n :=
⟨λ p, (eq_repeat.2
⟨p.length_eq.trans $ length_repeat _ _,
λ b m, eq_of_mem_repeat $ p.subset m⟩),
λ h, h ▸ perm.refl _⟩
@[simp] theorem repeat_perm {a : α} {n : ℕ} {l : list α} : repeat a n ~ l ↔ repeat a n = l :=
(perm_comm.trans perm_repeat).trans eq_comm
@[simp] theorem perm_singleton {a : α} {l : list α} : l ~ [a] ↔ l = [a] :=
@perm_repeat α a 1 l
@[simp] theorem singleton_perm {a : α} {l : list α} : [a] ~ l ↔ [a] = l :=
@repeat_perm α a 1 l
theorem perm.eq_singleton {a : α} {l : list α} (p : l ~ [a]) : l = [a] :=
perm_singleton.1 p
theorem perm.singleton_eq {a : α} {l : list α} (p : [a] ~ l) : [a] = l :=
p.symm.eq_singleton.symm
theorem singleton_perm_singleton {a b : α} : [a] ~ [b] ↔ a = b :=
by simp
theorem perm_cons_erase [decidable_eq α] {a : α} {l : list α} (h : a ∈ l) :
l ~ a :: l.erase a :=
let ⟨l₁, l₂, _, e₁, e₂⟩ := exists_erase_eq h in
e₂.symm ▸ e₁.symm ▸ perm_middle
@[elab_as_eliminator] theorem perm_induction_on
{P : list α → list α → Prop} {l₁ l₂ : list α} (p : l₁ ~ l₂)
(h₁ : P [] [])
(h₂ : ∀ x l₁ l₂, l₁ ~ l₂ → P l₁ l₂ → P (x::l₁) (x::l₂))
(h₃ : ∀ x y l₁ l₂, l₁ ~ l₂ → P l₁ l₂ → P (y::x::l₁) (x::y::l₂))
(h₄ : ∀ l₁ l₂ l₃, l₁ ~ l₂ → l₂ ~ l₃ → P l₁ l₂ → P l₂ l₃ → P l₁ l₃) :
P l₁ l₂ :=
have P_refl : ∀ l, P l l, from
assume l,
list.rec_on l h₁ (λ x xs ih, h₂ x xs xs (perm.refl xs) ih),
perm.rec_on p h₁ h₂ (λ x y l, h₃ x y l l (perm.refl l) (P_refl l)) h₄
@[congr] theorem perm.filter_map (f : α → option β) {l₁ l₂ : list α} (p : l₁ ~ l₂) :
filter_map f l₁ ~ filter_map f l₂ :=
begin
induction p with x l₂ l₂' p IH x y l₂ l₂ m₂ r₂ p₁ p₂ IH₁ IH₂,
{ simp },
{ simp only [filter_map], cases f x with a; simp [filter_map, IH, perm.cons] },
{ simp only [filter_map], cases f x with a; cases f y with b; simp [filter_map, swap] },
{ exact IH₁.trans IH₂ }
end
@[congr] theorem perm.map (f : α → β) {l₁ l₂ : list α} (p : l₁ ~ l₂) :
map f l₁ ~ map f l₂ :=
filter_map_eq_map f ▸ p.filter_map _
theorem perm.pmap {p : α → Prop} (f : Π a, p a → β)
{l₁ l₂ : list α} (p : l₁ ~ l₂) {H₁ H₂} : pmap f l₁ H₁ ~ pmap f l₂ H₂ :=
begin
induction p with x l₂ l₂' p IH x y l₂ l₂ m₂ r₂ p₁ p₂ IH₁ IH₂,
{ simp },
{ simp [IH, perm.cons] },
{ simp [swap] },
{ refine IH₁.trans IH₂,
exact λ a m, H₂ a (p₂.subset m) }
end
theorem perm.filter (p : α → Prop) [decidable_pred p]
{l₁ l₂ : list α} (s : l₁ ~ l₂) : filter p l₁ ~ filter p l₂ :=
by rw ← filter_map_eq_filter; apply s.filter_map _
theorem exists_perm_sublist {l₁ l₂ l₂' : list α}
(s : l₁ <+ l₂) (p : l₂ ~ l₂') : ∃ l₁' ~ l₁, l₁' <+ l₂' :=
begin
induction p with x l₂ l₂' p IH x y l₂ l₂ m₂ r₂ p₁ p₂ IH₁ IH₂ generalizing l₁ s,
{ exact ⟨[], eq_nil_of_sublist_nil s ▸ perm.refl _, nil_sublist _⟩ },
{ cases s with _ _ _ s l₁ _ _ s,
{ exact let ⟨l₁', p', s'⟩ := IH s in ⟨l₁', p', s'.cons _ _ _⟩ },
{ exact let ⟨l₁', p', s'⟩ := IH s in ⟨x::l₁', p'.cons x, s'.cons2 _ _ _⟩ } },
{ cases s with _ _ _ s l₁ _ _ s; cases s with _ _ _ s l₁ _ _ s,
{ exact ⟨l₁, perm.refl _, (s.cons _ _ _).cons _ _ _⟩ },
{ exact ⟨x::l₁, perm.refl _, (s.cons _ _ _).cons2 _ _ _⟩ },
{ exact ⟨y::l₁, perm.refl _, (s.cons2 _ _ _).cons _ _ _⟩ },
{ exact ⟨x::y::l₁, perm.swap _ _ _, (s.cons2 _ _ _).cons2 _ _ _⟩ } },
{ exact let ⟨m₁, pm, sm⟩ := IH₁ s, ⟨r₁, pr, sr⟩ := IH₂ sm in
⟨r₁, pr.trans pm, sr⟩ }
end
theorem perm.sizeof_eq_sizeof [has_sizeof α] {l₁ l₂ : list α} (h : l₁ ~ l₂) :
l₁.sizeof = l₂.sizeof :=
begin
induction h with hd l₁ l₂ h₁₂ h_sz₁₂ a b l l₁ l₂ l₃ h₁₂ h₂₃ h_sz₁₂ h_sz₂₃,
{ refl },
{ simp only [list.sizeof, h_sz₁₂] },
{ simp only [list.sizeof, add_left_comm] },
{ simp only [h_sz₁₂, h_sz₂₃] }
end
section rel
open relator
variables {γ : Type*} {δ : Type*} {r : α → β → Prop} {p : γ → δ → Prop}
local infixr ` ∘r ` : 80 := relation.comp
lemma perm_comp_perm : (perm ∘r perm : list α → list α → Prop) = perm :=
begin
funext a c, apply propext,
split,
{ exact assume ⟨b, hab, hba⟩, perm.trans hab hba },
{ exact assume h, ⟨a, perm.refl a, h⟩ }
end
lemma perm_comp_forall₂ {l u v} (hlu : perm l u) (huv : forall₂ r u v) : (forall₂ r ∘r perm) l v :=
begin
induction hlu generalizing v,
case perm.nil { cases huv, exact ⟨[], forall₂.nil, perm.nil⟩ },
case perm.cons : a l u hlu ih {
cases huv with _ b _ v hab huv',
rcases ih huv' with ⟨l₂, h₁₂, h₂₃⟩,
exact ⟨b::l₂, forall₂.cons hab h₁₂, h₂₃.cons _⟩ },
case perm.swap : a₁ a₂ l₁ l₂ h₂₃ {
cases h₂₃ with _ b₁ _ l₂ h₁ hr_₂₃,
cases hr_₂₃ with _ b₂ _ l₂ h₂ h₁₂,
exact ⟨b₂::b₁::l₂, forall₂.cons h₂ (forall₂.cons h₁ h₁₂), perm.swap _ _ _⟩ },
case perm.trans : la₁ la₂ la₃ _ _ ih₁ ih₂ {
rcases ih₂ huv with ⟨lb₂, hab₂, h₂₃⟩,
rcases ih₁ hab₂ with ⟨lb₁, hab₁, h₁₂⟩,
exact ⟨lb₁, hab₁, perm.trans h₁₂ h₂₃⟩ }
end
lemma forall₂_comp_perm_eq_perm_comp_forall₂ : forall₂ r ∘r perm = perm ∘r forall₂ r :=
begin
funext l₁ l₃, apply propext,
split,
{ assume h, rcases h with ⟨l₂, h₁₂, h₂₃⟩,
have : forall₂ (flip r) l₂ l₁, from h₁₂.flip ,
rcases perm_comp_forall₂ h₂₃.symm this with ⟨l', h₁, h₂⟩,
exact ⟨l', h₂.symm, h₁.flip⟩ },
{ exact assume ⟨l₂, h₁₂, h₂₃⟩, perm_comp_forall₂ h₁₂ h₂₃ }
end
lemma rel_perm_imp (hr : right_unique r) : (forall₂ r ⇒ forall₂ r ⇒ implies) perm perm :=
assume a b h₁ c d h₂ h,
have (flip (forall₂ r) ∘r (perm ∘r forall₂ r)) b d, from ⟨a, h₁, c, h, h₂⟩,
have ((flip (forall₂ r) ∘r forall₂ r) ∘r perm) b d,
by rwa [← forall₂_comp_perm_eq_perm_comp_forall₂, ← relation.comp_assoc] at this,
let ⟨b', ⟨c', hbc, hcb⟩, hbd⟩ := this in
have b' = b, from right_unique_forall₂' hr hcb hbc,
this ▸ hbd
lemma rel_perm (hr : bi_unique r) : (forall₂ r ⇒ forall₂ r ⇒ (↔)) perm perm :=
assume a b hab c d hcd, iff.intro
(rel_perm_imp hr.2 hab hcd)
(rel_perm_imp (left_unique_flip hr.1) hab.flip hcd.flip)
end rel
section subperm
/-- `subperm l₁ l₂`, denoted `l₁ <+~ l₂`, means that `l₁` is a sublist of
a permutation of `l₂`. This is an analogue of `l₁ ⊆ l₂` which respects
multiplicities of elements, and is used for the `≤` relation on multisets. -/
def subperm (l₁ l₂ : list α) : Prop := ∃ l ~ l₁, l <+ l₂
infix ` <+~ `:50 := subperm
theorem nil_subperm {l : list α} : [] <+~ l :=
⟨[], perm.nil, by simp⟩
theorem perm.subperm_left {l l₁ l₂ : list α} (p : l₁ ~ l₂) : l <+~ l₁ ↔ l <+~ l₂ :=
suffices ∀ {l₁ l₂ : list α}, l₁ ~ l₂ → l <+~ l₁ → l <+~ l₂,
from ⟨this p, this p.symm⟩,
λ l₁ l₂ p ⟨u, pu, su⟩,
let ⟨v, pv, sv⟩ := exists_perm_sublist su p in
⟨v, pv.trans pu, sv⟩
theorem perm.subperm_right {l₁ l₂ l : list α} (p : l₁ ~ l₂) : l₁ <+~ l ↔ l₂ <+~ l :=
⟨λ ⟨u, pu, su⟩, ⟨u, pu.trans p, su⟩,
λ ⟨u, pu, su⟩, ⟨u, pu.trans p.symm, su⟩⟩
theorem sublist.subperm {l₁ l₂ : list α} (s : l₁ <+ l₂) : l₁ <+~ l₂ :=
⟨l₁, perm.refl _, s⟩
theorem perm.subperm {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₁ <+~ l₂ :=
⟨l₂, p.symm, sublist.refl _⟩
@[refl] theorem subperm.refl (l : list α) : l <+~ l := (perm.refl _).subperm
@[trans] theorem subperm.trans {l₁ l₂ l₃ : list α} : l₁ <+~ l₂ → l₂ <+~ l₃ → l₁ <+~ l₃
| s ⟨l₂', p₂, s₂⟩ :=
let ⟨l₁', p₁, s₁⟩ := p₂.subperm_left.2 s in ⟨l₁', p₁, s₁.trans s₂⟩
theorem subperm.length_le {l₁ l₂ : list α} : l₁ <+~ l₂ → length l₁ ≤ length l₂
| ⟨l, p, s⟩ := p.length_eq ▸ length_le_of_sublist s
theorem subperm.perm_of_length_le {l₁ l₂ : list α} : l₁ <+~ l₂ → length l₂ ≤ length l₁ → l₁ ~ l₂
| ⟨l, p, s⟩ h :=
suffices l = l₂, from this ▸ p.symm,
eq_of_sublist_of_length_le s $ p.symm.length_eq ▸ h
theorem subperm.antisymm {l₁ l₂ : list α} (h₁ : l₁ <+~ l₂) (h₂ : l₂ <+~ l₁) : l₁ ~ l₂ :=
h₁.perm_of_length_le h₂.length_le
theorem subperm.subset {l₁ l₂ : list α} : l₁ <+~ l₂ → l₁ ⊆ l₂
| ⟨l, p, s⟩ := subset.trans p.symm.subset s.subset
lemma subperm.filter (p : α → Prop) [decidable_pred p]
⦃l l' : list α⦄ (h : l <+~ l') : filter p l <+~ filter p l' :=
begin
obtain ⟨xs, hp, h⟩ := h,
exact ⟨_, hp.filter p, h.filter p⟩
end
end subperm
theorem sublist.exists_perm_append : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → ∃ l, l₂ ~ l₁ ++ l
| ._ ._ sublist.slnil := ⟨nil, perm.refl _⟩
| ._ ._ (sublist.cons l₁ l₂ a s) :=
let ⟨l, p⟩ := sublist.exists_perm_append s in
⟨a::l, (p.cons a).trans perm_middle.symm⟩
| ._ ._ (sublist.cons2 l₁ l₂ a s) :=
let ⟨l, p⟩ := sublist.exists_perm_append s in
⟨l, p.cons a⟩
theorem perm.countp_eq (p : α → Prop) [decidable_pred p]
{l₁ l₂ : list α} (s : l₁ ~ l₂) : countp p l₁ = countp p l₂ :=
by rw [countp_eq_length_filter, countp_eq_length_filter];
exact (s.filter _).length_eq
theorem subperm.countp_le (p : α → Prop) [decidable_pred p]
{l₁ l₂ : list α} : l₁ <+~ l₂ → countp p l₁ ≤ countp p l₂
| ⟨l, p', s⟩ := p'.countp_eq p ▸ countp_le_of_sublist p s
theorem perm.count_eq [decidable_eq α] {l₁ l₂ : list α}
(p : l₁ ~ l₂) (a) : count a l₁ = count a l₂ :=
p.countp_eq _
theorem subperm.count_le [decidable_eq α] {l₁ l₂ : list α}
(s : l₁ <+~ l₂) (a) : count a l₁ ≤ count a l₂ :=
s.countp_le _
theorem perm.foldl_eq' {f : β → α → β} {l₁ l₂ : list α} (p : l₁ ~ l₂) :
(∀ (x ∈ l₁) (y ∈ l₁) z, f (f z x) y = f (f z y) x) → ∀ b, foldl f b l₁ = foldl f b l₂ :=
perm_induction_on p
(λ H b, rfl)
(λ x t₁ t₂ p r H b, r (λ x hx y hy, H _ (or.inr hx) _ (or.inr hy)) _)
(λ x y t₁ t₂ p r H b,
begin
simp only [foldl],
rw [H x (or.inr $ or.inl rfl) y (or.inl rfl)],
exact r (λ x hx y hy, H _ (or.inr $ or.inr hx) _ (or.inr $ or.inr hy)) _
end)
(λ t₁ t₂ t₃ p₁ p₂ r₁ r₂ H b, eq.trans (r₁ H b)
(r₂ (λ x hx y hy, H _ (p₁.symm.subset hx) _ (p₁.symm.subset hy)) b))
theorem perm.foldl_eq {f : β → α → β} {l₁ l₂ : list α} (rcomm : right_commutative f) (p : l₁ ~ l₂) :
∀ b, foldl f b l₁ = foldl f b l₂ :=
p.foldl_eq' $ λ x hx y hy z, rcomm z x y
theorem perm.foldr_eq {f : α → β → β} {l₁ l₂ : list α} (lcomm : left_commutative f) (p : l₁ ~ l₂) :
∀ b, foldr f b l₁ = foldr f b l₂ :=
perm_induction_on p
(λ b, rfl)
(λ x t₁ t₂ p r b, by simp; rw [r b])
(λ x y t₁ t₂ p r b, by simp; rw [lcomm, r b])
(λ t₁ t₂ t₃ p₁ p₂ r₁ r₂ a, eq.trans (r₁ a) (r₂ a))
lemma perm.rec_heq {β : list α → Sort*} {f : Πa l, β l → β (a::l)} {b : β []} {l l' : list α}
(hl : perm l l')
(f_congr : ∀{a l l' b b'}, perm l l' → b == b' → f a l b == f a l' b')
(f_swap : ∀{a a' l b}, f a (a'::l) (f a' l b) == f a' (a::l) (f a l b)) :
@list.rec α β b f l == @list.rec α β b f l' :=
begin
induction hl,
case list.perm.nil { refl },
case list.perm.cons : a l l' h ih { exact f_congr h ih },
case list.perm.swap : a a' l { exact f_swap },
case list.perm.trans : l₁ l₂ l₃ h₁ h₂ ih₁ ih₂ { exact heq.trans ih₁ ih₂ }
end
section
variables {op : α → α → α} [is_associative α op] [is_commutative α op]
local notation a * b := op a b
local notation l <*> a := foldl op a l
lemma perm.fold_op_eq {l₁ l₂ : list α} {a : α} (h : l₁ ~ l₂) : l₁ <*> a = l₂ <*> a :=
h.foldl_eq (right_comm _ is_commutative.comm is_associative.assoc) _
end
section comm_monoid
/-- If elements of a list commute with each other, then their product does not
depend on the order of elements-/
@[to_additive]
lemma perm.prod_eq' [monoid α] {l₁ l₂ : list α} (h : l₁ ~ l₂)
(hc : l₁.pairwise (λ x y, x * y = y * x)) :
l₁.prod = l₂.prod :=
h.foldl_eq' (forall_of_forall_of_pairwise (λ x y h z, (h z).symm) (λ x hx z, rfl) $
hc.imp $ λ x y h z, by simp only [mul_assoc, h]) _
variable [comm_monoid α]
@[to_additive]
lemma perm.prod_eq {l₁ l₂ : list α} (h : perm l₁ l₂) : prod l₁ = prod l₂ :=
h.fold_op_eq
@[to_additive]
lemma prod_reverse (l : list α) : prod l.reverse = prod l :=
(reverse_perm l).prod_eq
end comm_monoid
theorem perm_inv_core {a : α} {l₁ l₂ r₁ r₂ : list α} : l₁++a::r₁ ~ l₂++a::r₂ → l₁++r₁ ~ l₂++r₂ :=
begin
generalize e₁ : l₁++a::r₁ = s₁, generalize e₂ : l₂++a::r₂ = s₂,
intro p, revert l₁ l₂ r₁ r₂ e₁ e₂,
refine perm_induction_on p _ (λ x t₁ t₂ p IH, _) (λ x y t₁ t₂ p IH, _)
(λ t₁ t₂ t₃ p₁ p₂ IH₁ IH₂, _); intros l₁ l₂ r₁ r₂ e₁ e₂,
{ apply (not_mem_nil a).elim, rw ← e₁, simp },
{ cases l₁ with y l₁; cases l₂ with z l₂;
dsimp at e₁ e₂; injections; subst x,
{ substs t₁ t₂, exact p },
{ substs z t₁ t₂, exact p.trans perm_middle },
{ substs y t₁ t₂, exact perm_middle.symm.trans p },
{ substs z t₁ t₂, exact (IH rfl rfl).cons y } },
{ rcases l₁ with _|⟨y, _|⟨z, l₁⟩⟩; rcases l₂ with _|⟨u, _|⟨v, l₂⟩⟩;
dsimp at e₁ e₂; injections; substs x y,
{ substs r₁ r₂, exact p.cons a },
{ substs r₁ r₂, exact p.cons u },
{ substs r₁ v t₂, exact (p.trans perm_middle).cons u },
{ substs r₁ r₂, exact p.cons y },
{ substs r₁ r₂ y u, exact p.cons a },
{ substs r₁ u v t₂, exact ((p.trans perm_middle).cons y).trans (swap _ _ _) },
{ substs r₂ z t₁, exact (perm_middle.symm.trans p).cons y },
{ substs r₂ y z t₁, exact (swap _ _ _).trans ((perm_middle.symm.trans p).cons u) },
{ substs u v t₁ t₂, exact (IH rfl rfl).swap' _ _ } },
{ substs t₁ t₃,
have : a ∈ t₂ := p₁.subset (by simp),
rcases mem_split this with ⟨l₂, r₂, e₂⟩,
subst t₂, exact (IH₁ rfl rfl).trans (IH₂ rfl rfl) }
end
theorem perm.cons_inv {a : α} {l₁ l₂ : list α} : a::l₁ ~ a::l₂ → l₁ ~ l₂ :=
@perm_inv_core _ _ [] [] _ _
@[simp] theorem perm_cons (a : α) {l₁ l₂ : list α} : a::l₁ ~ a::l₂ ↔ l₁ ~ l₂ :=
⟨perm.cons_inv, perm.cons a⟩
theorem perm_append_left_iff {l₁ l₂ : list α} : ∀ l, l++l₁ ~ l++l₂ ↔ l₁ ~ l₂
| [] := iff.rfl
| (a::l) := (perm_cons a).trans (perm_append_left_iff l)
theorem perm_append_right_iff {l₁ l₂ : list α} (l) : l₁++l ~ l₂++l ↔ l₁ ~ l₂ :=
⟨λ p, (perm_append_left_iff _).1 $ perm_append_comm.trans $ p.trans perm_append_comm,
perm.append_right _⟩
theorem perm_option_to_list {o₁ o₂ : option α} : o₁.to_list ~ o₂.to_list ↔ o₁ = o₂ :=
begin
refine ⟨λ p, _, λ e, e ▸ perm.refl _⟩,
cases o₁ with a; cases o₂ with b, {refl},
{ cases p.length_eq },
{ cases p.length_eq },
{ exact option.mem_to_list.1 (p.symm.subset $ by simp) }
end
theorem subperm_cons (a : α) {l₁ l₂ : list α} : a::l₁ <+~ a::l₂ ↔ l₁ <+~ l₂ :=
⟨λ ⟨l, p, s⟩, begin
cases s with _ _ _ s' u _ _ s',
{ exact (p.subperm_left.2 $ (sublist_cons _ _).subperm).trans s'.subperm },
{ exact ⟨u, p.cons_inv, s'⟩ }
end, λ ⟨l, p, s⟩, ⟨a::l, p.cons a, s.cons2 _ _ _⟩⟩
theorem cons_subperm_of_mem {a : α} {l₁ l₂ : list α} (d₁ : nodup l₁) (h₁ : a ∉ l₁) (h₂ : a ∈ l₂)
(s : l₁ <+~ l₂) : a :: l₁ <+~ l₂ :=
begin
rcases s with ⟨l, p, s⟩,
induction s generalizing l₁,
case list.sublist.slnil { cases h₂ },
case list.sublist.cons : r₁ r₂ b s' ih {
simp at h₂,
cases h₂ with e m,
{ subst b, exact ⟨a::r₁, p.cons a, s'.cons2 _ _ _⟩ },
{ rcases ih m d₁ h₁ p with ⟨t, p', s'⟩, exact ⟨t, p', s'.cons _ _ _⟩ } },
case list.sublist.cons2 : r₁ r₂ b s' ih {
have bm : b ∈ l₁ := (p.subset $ mem_cons_self _ _),
have am : a ∈ r₂ := h₂.resolve_left (λ e, h₁ $ e.symm ▸ bm),
rcases mem_split bm with ⟨t₁, t₂, rfl⟩,
have st : t₁ ++ t₂ <+ t₁ ++ b :: t₂ := by simp,
rcases ih am (nodup_of_sublist st d₁)
(mt (λ x, st.subset x) h₁)
(perm.cons_inv $ p.trans perm_middle) with ⟨t, p', s'⟩,
exact ⟨b::t, (p'.cons b).trans $ (swap _ _ _).trans (perm_middle.symm.cons a), s'.cons2 _ _ _⟩ }
end
theorem subperm_append_left {l₁ l₂ : list α} : ∀ l, l++l₁ <+~ l++l₂ ↔ l₁ <+~ l₂
| [] := iff.rfl
| (a::l) := (subperm_cons a).trans (subperm_append_left l)
theorem subperm_append_right {l₁ l₂ : list α} (l) : l₁++l <+~ l₂++l ↔ l₁ <+~ l₂ :=
(perm_append_comm.subperm_left.trans perm_append_comm.subperm_right).trans (subperm_append_left l)
theorem subperm.exists_of_length_lt {l₁ l₂ : list α} :
l₁ <+~ l₂ → length l₁ < length l₂ → ∃ a, a :: l₁ <+~ l₂
| ⟨l, p, s⟩ h :=
suffices length l < length l₂ → ∃ (a : α), a :: l <+~ l₂, from
(this $ p.symm.length_eq ▸ h).imp (λ a, (p.cons a).subperm_right.1),
begin
clear subperm.exists_of_length_lt p h l₁, rename l₂ u,
induction s with l₁ l₂ a s IH _ _ b s IH; intro h,
{ cases h },
{ cases lt_or_eq_of_le (nat.le_of_lt_succ h : length l₁ ≤ length l₂) with h h,
{ exact (IH h).imp (λ a s, s.trans (sublist_cons _ _).subperm) },
{ exact ⟨a, eq_of_sublist_of_length_eq s h ▸ subperm.refl _⟩ } },
{ exact (IH $ nat.lt_of_succ_lt_succ h).imp
(λ a s, (swap _ _ _).subperm_right.1 $ (subperm_cons _).2 s) }
end
theorem subperm_of_subset_nodup
{l₁ l₂ : list α} (d : nodup l₁) (H : l₁ ⊆ l₂) : l₁ <+~ l₂ :=
begin
induction d with a l₁' h d IH,
{ exact ⟨nil, perm.nil, nil_sublist _⟩ },
{ cases forall_mem_cons.1 H with H₁ H₂,
simp at h,
exact cons_subperm_of_mem d h H₁ (IH H₂) }
end
theorem perm_ext {l₁ l₂ : list α} (d₁ : nodup l₁) (d₂ : nodup l₂) :
l₁ ~ l₂ ↔ ∀a, a ∈ l₁ ↔ a ∈ l₂ :=
⟨λ p a, p.mem_iff, λ H, subperm.antisymm
(subperm_of_subset_nodup d₁ (λ a, (H a).1))
(subperm_of_subset_nodup d₂ (λ a, (H a).2))⟩
theorem nodup.sublist_ext {l₁ l₂ l : list α} (d : nodup l)
(s₁ : l₁ <+ l) (s₂ : l₂ <+ l) : l₁ ~ l₂ ↔ l₁ = l₂ :=
⟨λ h, begin
induction s₂ with l₂ l a s₂ IH l₂ l a s₂ IH generalizing l₁,
{ exact h.eq_nil },
{ simp at d,
cases s₁ with _ _ _ s₁ l₁ _ _ s₁,
{ exact IH d.2 s₁ h },
{ apply d.1.elim,
exact subperm.subset ⟨_, h.symm, s₂⟩ (mem_cons_self _ _) } },
{ simp at d,
cases s₁ with _ _ _ s₁ l₁ _ _ s₁,
{ apply d.1.elim,
exact subperm.subset ⟨_, h, s₁⟩ (mem_cons_self _ _) },
{ rw IH d.2 s₁ h.cons_inv } }
end, λ h, by rw h⟩
section
variable [decidable_eq α]
-- attribute [congr]
theorem perm.erase (a : α) {l₁ l₂ : list α} (p : l₁ ~ l₂) :
l₁.erase a ~ l₂.erase a :=
if h₁ : a ∈ l₁ then
have h₂ : a ∈ l₂, from p.subset h₁,
perm.cons_inv $ (perm_cons_erase h₁).symm.trans $ p.trans (perm_cons_erase h₂)
else
have h₂ : a ∉ l₂, from mt p.mem_iff.2 h₁,
by rw [erase_of_not_mem h₁, erase_of_not_mem h₂]; exact p
theorem subperm_cons_erase (a : α) (l : list α) : l <+~ a :: l.erase a :=
begin
by_cases h : a ∈ l,
{ exact (perm_cons_erase h).subperm },
{ rw [erase_of_not_mem h],
exact (sublist_cons _ _).subperm }
end
theorem erase_subperm (a : α) (l : list α) : l.erase a <+~ l :=
(erase_sublist _ _).subperm
theorem subperm.erase {l₁ l₂ : list α} (a : α) (h : l₁ <+~ l₂) : l₁.erase a <+~ l₂.erase a :=
let ⟨l, hp, hs⟩ := h in ⟨l.erase a, hp.erase _, hs.erase _⟩
theorem perm.diff_right {l₁ l₂ : list α} (t : list α) (h : l₁ ~ l₂) : l₁.diff t ~ l₂.diff t :=
by induction t generalizing l₁ l₂ h; simp [*, perm.erase]
theorem perm.diff_left (l : list α) {t₁ t₂ : list α} (h : t₁ ~ t₂) : l.diff t₁ = l.diff t₂ :=
by induction h generalizing l; simp [*, perm.erase, erase_comm]
<|> exact (ih_1 _).trans (ih_2 _)
theorem perm.diff {l₁ l₂ t₁ t₂ : list α} (hl : l₁ ~ l₂) (ht : t₁ ~ t₂) :
l₁.diff t₁ ~ l₂.diff t₂ :=
ht.diff_left l₂ ▸ hl.diff_right _
theorem subperm.diff_right {l₁ l₂ : list α} (h : l₁ <+~ l₂) (t : list α) :
l₁.diff t <+~ l₂.diff t :=
by induction t generalizing l₁ l₂ h; simp [*, subperm.erase]
theorem erase_cons_subperm_cons_erase (a b : α) (l : list α) :
(a :: l).erase b <+~ a :: l.erase b :=
begin
by_cases h : a = b,
{ subst b,
rw [erase_cons_head],
apply subperm_cons_erase },
{ rw [erase_cons_tail _ h] }
end
theorem subperm_cons_diff {a : α} : ∀ {l₁ l₂ : list α}, (a :: l₁).diff l₂ <+~ a :: l₁.diff l₂
| l₁ [] := ⟨a::l₁, by simp⟩
| l₁ (b::l₂) :=
begin
simp only [diff_cons],
refine ((erase_cons_subperm_cons_erase a b l₁).diff_right l₂).trans _,
apply subperm_cons_diff
end
theorem subset_cons_diff {a : α} {l₁ l₂ : list α} : (a :: l₁).diff l₂ ⊆ a :: l₁.diff l₂ :=
subperm_cons_diff.subset
theorem perm.bag_inter_right {l₁ l₂ : list α} (t : list α) (h : l₁ ~ l₂) :
l₁.bag_inter t ~ l₂.bag_inter t :=
begin
induction h with x _ _ _ _ x y _ _ _ _ _ _ ih_1 ih_2 generalizing t, {simp},
{ by_cases x ∈ t; simp [*, perm.cons] },
{ by_cases x = y, {simp [h]},
by_cases xt : x ∈ t; by_cases yt : y ∈ t,
{ simp [xt, yt, mem_erase_of_ne h, mem_erase_of_ne (ne.symm h), erase_comm, swap] },
{ simp [xt, yt, mt mem_of_mem_erase, perm.cons] },
{ simp [xt, yt, mt mem_of_mem_erase, perm.cons] },
{ simp [xt, yt] } },
{ exact (ih_1 _).trans (ih_2 _) }
end
theorem perm.bag_inter_left (l : list α) {t₁ t₂ : list α} (p : t₁ ~ t₂) :
l.bag_inter t₁ = l.bag_inter t₂ :=
begin
induction l with a l IH generalizing t₁ t₂ p, {simp},
by_cases a ∈ t₁,
{ simp [h, p.subset h, IH (p.erase _)] },
{ simp [h, mt p.mem_iff.2 h, IH p] }
end
theorem perm.bag_inter {l₁ l₂ t₁ t₂ : list α} (hl : l₁ ~ l₂) (ht : t₁ ~ t₂) :
l₁.bag_inter t₁ ~ l₂.bag_inter t₂ :=
ht.bag_inter_left l₂ ▸ hl.bag_inter_right _
theorem cons_perm_iff_perm_erase {a : α} {l₁ l₂ : list α} : a::l₁ ~ l₂ ↔ a ∈ l₂ ∧ l₁ ~ l₂.erase a :=
⟨λ h, have a ∈ l₂, from h.subset (mem_cons_self a l₁),
⟨this, (h.trans $ perm_cons_erase this).cons_inv⟩,
λ ⟨m, h⟩, (h.cons a).trans (perm_cons_erase m).symm⟩
theorem perm_iff_count {l₁ l₂ : list α} : l₁ ~ l₂ ↔ ∀ a, count a l₁ = count a l₂ :=
⟨perm.count_eq, λ H, begin
induction l₁ with a l₁ IH generalizing l₂,
{ cases l₂ with b l₂, {refl},
specialize H b, simp at H, contradiction },
{ have : a ∈ l₂ := count_pos.1 (by rw ← H; simp; apply nat.succ_pos),
refine ((IH $ λ b, _).cons a).trans (perm_cons_erase this).symm,
specialize H b,
rw (perm_cons_erase this).count_eq at H,
by_cases b = a; simp [h] at H ⊢; assumption }
end⟩
lemma subperm.cons_right {α : Type*} {l l' : list α} (x : α) (h : l <+~ l') : l <+~ x :: l' :=
h.trans (sublist_cons x l').subperm
/-- The list version of `multiset.add_sub_of_le`. -/
lemma subperm_append_diff_self_of_count_le {l₁ l₂ : list α}
(h : ∀ x ∈ l₁, count x l₁ ≤ count x l₂) : l₁ ++ l₂.diff l₁ ~ l₂ :=
begin
induction l₁ with hd tl IH generalizing l₂,
{ simp },
{ have : hd ∈ l₂,
{ rw ←count_pos,
exact lt_of_lt_of_le (count_pos.mpr (mem_cons_self _ _)) (h hd (mem_cons_self _ _)) },
replace this : l₂ ~ hd :: l₂.erase hd := perm_cons_erase this,
refine perm.trans _ this.symm,
rw [cons_append, diff_cons, perm_cons],
refine IH (λ x hx, _),
specialize h x (mem_cons_of_mem _ hx),
rw (perm_iff_count.mp this) at h,
by_cases hx : x = hd,
{ subst hd,
simpa [nat.succ_le_succ_iff] using h },
{ simpa [hx] using h } },
end
/-- The list version of `multiset.le_iff_count`. -/
lemma subperm_ext_iff {l₁ l₂ : list α} :
l₁ <+~ l₂ ↔ ∀ x ∈ l₁, count x l₁ ≤ count x l₂ :=
begin
refine ⟨λ h x hx, subperm.count_le h x, λ h, _⟩,
suffices : l₁ <+~ (l₂.diff l₁ ++ l₁),
{ refine this.trans (perm.subperm _),
exact perm_append_comm.trans (subperm_append_diff_self_of_count_le h) },
convert (subperm_append_right _).mpr nil_subperm using 1
end
lemma subperm.cons_left {l₁ l₂ : list α} (h : l₁ <+~ l₂)
(x : α) (hx : count x l₁ < count x l₂) :
x :: l₁ <+~ l₂ :=
begin
rw subperm_ext_iff at h ⊢,
intros y hy,
by_cases hy' : y = x,
{ subst x,
simpa using nat.succ_le_of_lt hx },
{ rw count_cons_of_ne hy',
refine h y _,
simpa [hy'] using hy }
end
instance decidable_perm : ∀ (l₁ l₂ : list α), decidable (l₁ ~ l₂)
| [] [] := is_true $ perm.refl _
| [] (b::l₂) := is_false $ λ h, by have := h.nil_eq; contradiction
| (a::l₁) l₂ := by haveI := decidable_perm l₁ (l₂.erase a);
exact decidable_of_iff' _ cons_perm_iff_perm_erase
-- @[congr]
theorem perm.erase_dup {l₁ l₂ : list α} (p : l₁ ~ l₂) :
erase_dup l₁ ~ erase_dup l₂ :=
perm_iff_count.2 $ λ a,
if h : a ∈ l₁
then by simp [nodup_erase_dup, h, p.subset h]
else by simp [h, mt p.mem_iff.2 h]
-- attribute [congr]
theorem perm.insert (a : α)
{l₁ l₂ : list α} (p : l₁ ~ l₂) : insert a l₁ ~ insert a l₂ :=
if h : a ∈ l₁
then by simpa [h, p.subset h] using p
else by simpa [h, mt p.mem_iff.2 h] using p.cons a
theorem perm_insert_swap (x y : α) (l : list α) :
insert x (insert y l) ~ insert y (insert x l) :=
begin
by_cases xl : x ∈ l; by_cases yl : y ∈ l; simp [xl, yl],
by_cases xy : x = y, { simp [xy] },
simp [not_mem_cons_of_ne_of_not_mem xy xl,
not_mem_cons_of_ne_of_not_mem (ne.symm xy) yl],
constructor
end
theorem perm_insert_nth {α} (x : α) (l : list α) {n} (h : n ≤ l.length) :
insert_nth n x l ~ x :: l :=
begin
induction l generalizing n,
{ cases n, refl, cases h },
cases n,
{ simp [insert_nth] },
{ simp only [insert_nth, modify_nth_tail],
transitivity,
{ apply perm.cons, apply l_ih,
apply nat.le_of_succ_le_succ h },
{ apply perm.swap } }
end
theorem perm.union_right {l₁ l₂ : list α} (t₁ : list α) (h : l₁ ~ l₂) : l₁ ∪ t₁ ~ l₂ ∪ t₁ :=
begin
induction h with a _ _ _ ih _ _ _ _ _ _ _ _ ih_1 ih_2; try {simp},
{ exact ih.insert a },
{ apply perm_insert_swap },
{ exact ih_1.trans ih_2 }
end
theorem perm.union_left (l : list α) {t₁ t₂ : list α} (h : t₁ ~ t₂) : l ∪ t₁ ~ l ∪ t₂ :=
by induction l; simp [*, perm.insert]
-- @[congr]
theorem perm.union {l₁ l₂ t₁ t₂ : list α} (p₁ : l₁ ~ l₂) (p₂ : t₁ ~ t₂) : l₁ ∪ t₁ ~ l₂ ∪ t₂ :=
(p₁.union_right t₁).trans (p₂.union_left l₂)
theorem perm.inter_right {l₁ l₂ : list α} (t₁ : list α) : l₁ ~ l₂ → l₁ ∩ t₁ ~ l₂ ∩ t₁ :=
perm.filter _
theorem perm.inter_left (l : list α) {t₁ t₂ : list α} (p : t₁ ~ t₂) : l ∩ t₁ = l ∩ t₂ :=
by { dsimp [(∩), list.inter], congr, funext a, rw [p.mem_iff] }
-- @[congr]
theorem perm.inter {l₁ l₂ t₁ t₂ : list α} (p₁ : l₁ ~ l₂) (p₂ : t₁ ~ t₂) : l₁ ∩ t₁ ~ l₂ ∩ t₂ :=
p₂.inter_left l₂ ▸ p₁.inter_right t₁
theorem perm.inter_append {l t₁ t₂ : list α} (h : disjoint t₁ t₂) :
l ∩ (t₁ ++ t₂) ~ l ∩ t₁ ++ l ∩ t₂ :=
begin
induction l,
case list.nil
{ simp },
case list.cons : x xs l_ih
{ by_cases h₁ : x ∈ t₁,
{ have h₂ : x ∉ t₂ := h h₁,
simp * },
by_cases h₂ : x ∈ t₂,
{ simp only [*, inter_cons_of_not_mem, false_or, mem_append, inter_cons_of_mem, not_false_iff],
transitivity,
{ apply perm.cons _ l_ih, },
change [x] ++ xs ∩ t₁ ++ xs ∩ t₂ ~ xs ∩ t₁ ++ ([x] ++ xs ∩ t₂),
rw [← list.append_assoc],
solve_by_elim [perm.append_right, perm_append_comm] },
{ simp * } },
end
end
theorem perm.pairwise_iff {R : α → α → Prop} (S : symmetric R) :
∀ {l₁ l₂ : list α} (p : l₁ ~ l₂), pairwise R l₁ ↔ pairwise R l₂ :=
suffices ∀ {l₁ l₂}, l₁ ~ l₂ → pairwise R l₁ → pairwise R l₂, from λ l₁ l₂ p, ⟨this p, this p.symm⟩,
λ l₁ l₂ p d, begin
induction d with a l₁ h d IH generalizing l₂,
{ rw ← p.nil_eq, constructor },
{ have : a ∈ l₂ := p.subset (mem_cons_self _ _),
rcases mem_split this with ⟨s₂, t₂, rfl⟩,
have p' := (p.trans perm_middle).cons_inv,
refine (pairwise_middle S).2 (pairwise_cons.2 ⟨λ b m, _, IH _ p'⟩),
exact h _ (p'.symm.subset m) }
end
theorem perm.nodup_iff {l₁ l₂ : list α} : l₁ ~ l₂ → (nodup l₁ ↔ nodup l₂) :=
perm.pairwise_iff $ @ne.symm α
theorem perm.bind_right {l₁ l₂ : list α} (f : α → list β) (p : l₁ ~ l₂) :
l₁.bind f ~ l₂.bind f :=
begin
induction p with a l₁ l₂ p IH a b l l₁ l₂ l₃ p₁ p₂ IH₁ IH₂, {simp},
{ simp, exact IH.append_left _ },
{ simp, rw [← append_assoc, ← append_assoc], exact perm_append_comm.append_right _ },
{ exact IH₁.trans IH₂ }
end
theorem perm.bind_left (l : list α) {f g : α → list β} (h : ∀ a, f a ~ g a) :
l.bind f ~ l.bind g :=
by induction l with a l IH; simp; exact (h a).append IH
theorem bind_append_perm (l : list α) (f g : α → list β) :
l.bind f ++ l.bind g ~ l.bind (λ x, f x ++ g x) :=
begin
induction l with a l IH; simp,
refine (perm.trans _ (IH.append_left _)).append_left _,
rw [← append_assoc, ← append_assoc],
exact perm_append_comm.append_right _
end
theorem perm.product_right {l₁ l₂ : list α} (t₁ : list β) (p : l₁ ~ l₂) :
product l₁ t₁ ~ product l₂ t₁ :=
p.bind_right _
theorem perm.product_left (l : list α) {t₁ t₂ : list β} (p : t₁ ~ t₂) :
product l t₁ ~ product l t₂ :=
perm.bind_left _ $ λ a, p.map _
@[congr] theorem perm.product {l₁ l₂ : list α} {t₁ t₂ : list β}
(p₁ : l₁ ~ l₂) (p₂ : t₁ ~ t₂) : product l₁ t₁ ~ product l₂ t₂ :=
(p₁.product_right t₁).trans (p₂.product_left l₂)
theorem sublists_cons_perm_append (a : α) (l : list α) :
sublists (a :: l) ~ sublists l ++ map (cons a) (sublists l) :=
begin
simp only [sublists, sublists_aux_cons_cons, cons_append, perm_cons],
refine (perm.cons _ _).trans perm_middle.symm,
induction sublists_aux l cons with b l IH; simp,
exact (IH.cons _).trans perm_middle.symm
end
theorem sublists_perm_sublists' : ∀ l : list α, sublists l ~ sublists' l
| [] := perm.refl _
| (a::l) := let IH := sublists_perm_sublists' l in
by rw sublists'_cons; exact
(sublists_cons_perm_append _ _).trans (IH.append (IH.map _))
theorem revzip_sublists (l : list α) :
∀ l₁ l₂, (l₁, l₂) ∈ revzip l.sublists → l₁ ++ l₂ ~ l :=
begin
rw revzip,
apply list.reverse_rec_on l,
{ intros l₁ l₂ h, simp at h, simp [h] },
{ intros l a IH l₁ l₂ h,
rw [sublists_concat, reverse_append, zip_append, ← map_reverse,
zip_map_right, zip_map_left] at h; [skip, {simp}],
simp only [prod.mk.inj_iff, mem_map, mem_append, prod.map_mk, prod.exists] at h,
rcases h with ⟨l₁, l₂', h, rfl, rfl⟩ | ⟨l₁', l₂, h, rfl, rfl⟩,
{ rw ← append_assoc,
exact (IH _ _ h).append_right _ },
{ rw append_assoc,
apply (perm_append_comm.append_left _).trans,
rw ← append_assoc,
exact (IH _ _ h).append_right _ } }
end
theorem revzip_sublists' (l : list α) :
∀ l₁ l₂, (l₁, l₂) ∈ revzip l.sublists' → l₁ ++ l₂ ~ l :=
begin
rw revzip,
induction l with a l IH; intros l₁ l₂ h,
{ simp at h, simp [h] },
{ rw [sublists'_cons, reverse_append, zip_append, ← map_reverse,
zip_map_right, zip_map_left] at h; [simp at h, simp],
rcases h with ⟨l₁, l₂', h, rfl, rfl⟩ | ⟨l₁', h, rfl⟩,
{ exact perm_middle.trans ((IH _ _ h).cons _) },
{ exact (IH _ _ h).cons _ } }
end
theorem perm_lookmap (f : α → option α) {l₁ l₂ : list α}
(H : pairwise (λ a b, ∀ (c ∈ f a) (d ∈ f b), a = b ∧ c = d) l₁)
(p : l₁ ~ l₂) : lookmap f l₁ ~ lookmap f l₂ :=
begin
let F := λ a b, ∀ (c ∈ f a) (d ∈ f b), a = b ∧ c = d,
change pairwise F l₁ at H,
induction p with a l₁ l₂ p IH a b l l₁ l₂ l₃ p₁ p₂ IH₁ IH₂, {simp},
{ cases h : f a,
{ simp [h], exact IH (pairwise_cons.1 H).2 },
{ simp [lookmap_cons_some _ _ h, p] } },
{ cases h₁ : f a with c; cases h₂ : f b with d,
{ simp [h₁, h₂], apply swap },
{ simp [h₁, lookmap_cons_some _ _ h₂], apply swap },
{ simp [lookmap_cons_some _ _ h₁, h₂], apply swap },
{ simp [lookmap_cons_some _ _ h₁, lookmap_cons_some _ _ h₂],
rcases (pairwise_cons.1 H).1 _ (or.inl rfl) _ h₂ _ h₁ with ⟨rfl, rfl⟩,
refl } },
{ refine (IH₁ H).trans (IH₂ ((p₁.pairwise_iff _).1 H)),
exact λ a b h c h₁ d h₂, (h d h₂ c h₁).imp eq.symm eq.symm }
end
theorem perm.erasep (f : α → Prop) [decidable_pred f] {l₁ l₂ : list α}
(H : pairwise (λ a b, f a → f b → false) l₁)
(p : l₁ ~ l₂) : erasep f l₁ ~ erasep f l₂ :=
begin
let F := λ a b, f a → f b → false,
change pairwise F l₁ at H,
induction p with a l₁ l₂ p IH a b l l₁ l₂ l₃ p₁ p₂ IH₁ IH₂, {simp},
{ by_cases h : f a,
{ simp [h, p] },
{ simp [h], exact IH (pairwise_cons.1 H).2 } },
{ by_cases h₁ : f a; by_cases h₂ : f b; simp [h₁, h₂],
{ cases (pairwise_cons.1 H).1 _ (or.inl rfl) h₂ h₁ },
{ apply swap } },
{ refine (IH₁ H).trans (IH₂ ((p₁.pairwise_iff _).1 H)),
exact λ a b h h₁ h₂, h h₂ h₁ }
end
lemma perm.take_inter {α} [decidable_eq α] {xs ys : list α} (n : ℕ)
(h : xs ~ ys) (h' : ys.nodup) :
xs.take n ~ ys.inter (xs.take n) :=
begin
simp only [list.inter] at *,
induction h generalizing n,
case list.perm.nil : n
{ simp only [not_mem_nil, filter_false, take_nil] },
case list.perm.cons : h_x h_l₁ h_l₂ h_a h_ih n
{ cases n; simp only [mem_cons_iff, true_or, eq_self_iff_true, filter_cons_of_pos,
perm_cons, take, not_mem_nil, filter_false],
cases h' with _ _ h₁ h₂,
convert h_ih h₂ n using 1,
apply filter_congr,
introv h, simp only [(h₁ x h).symm, false_or], },
case list.perm.swap : h_x h_y h_l n
{ cases h' with _ _ h₁ h₂,
cases h₂ with _ _ h₂ h₃,
have := h₁ _ (or.inl rfl),
cases n; simp only [mem_cons_iff, not_mem_nil, filter_false, take],
cases n; simp only [mem_cons_iff, false_or, true_or, filter, *, nat.nat_zero_eq_zero, if_true,
not_mem_nil, eq_self_iff_true, or_false, if_false, perm_cons, take],
{ rw filter_eq_nil.2, intros, solve_by_elim [ne.symm], },
{ convert perm.swap _ _ _, rw @filter_congr _ _ (∈ take n h_l),
{ clear h₁, induction n generalizing h_l; simp only [not_mem_nil, filter_false, take],
cases h_l; simp only [mem_cons_iff, true_or, eq_self_iff_true, filter_cons_of_pos,
true_and, take, not_mem_nil, filter_false, take_nil],
cases h₃ with _ _ h₃ h₄,
rwa [@filter_congr _ _ (∈ take n_n h_l_tl), n_ih],
{ introv h, apply h₂ _ (or.inr h), },
{ introv h, simp only [(h₃ x h).symm, false_or], }, },
{ introv h, simp only [(h₂ x h).symm, (h₁ x (or.inr h)).symm, false_or], } } },
case list.perm.trans : h_l₁ h_l₂ h_l₃ h₀ h₁ h_ih₀ h_ih₁ n
{ transitivity,
{ apply h_ih₀, rwa h₁.nodup_iff },
{ apply perm.filter _ h₁, } },
end
lemma perm.drop_inter {α} [decidable_eq α] {xs ys : list α} (n : ℕ)
(h : xs ~ ys) (h' : ys.nodup) :
xs.drop n ~ ys.inter (xs.drop n) :=
begin
by_cases h'' : n ≤ xs.length,
{ let n' := xs.length - n,
have h₀ : n = xs.length - n',
{ dsimp [n'], rwa nat.sub_sub_self, } ,
have h₁ : n' ≤ xs.length,
{ apply nat.sub_le_self },
have h₂ : xs.drop n = (xs.reverse.take n').reverse,
{ rw [reverse_take _ h₁, h₀, reverse_reverse], },
rw [h₂],
apply (reverse_perm _).trans,
rw inter_reverse,
apply perm.take_inter _ _ h',
apply (reverse_perm _).trans; assumption, },
{ have : drop n xs = [],
{ apply eq_nil_of_length_eq_zero,
rw [length_drop, nat.sub_eq_zero_iff_le],
apply le_of_not_ge h'' },
simp [this, list.inter], }
end
lemma perm.slice_inter {α} [decidable_eq α] {xs ys : list α} (n m : ℕ)
(h : xs ~ ys) (h' : ys.nodup) :
list.slice n m xs ~ ys ∩ (list.slice n m xs) :=
begin
simp only [slice_eq],
have : n ≤ n + m := nat.le_add_right _ _,
have := h.nodup_iff.2 h',
apply perm.trans _ (perm.inter_append _).symm;
solve_by_elim [perm.append, perm.drop_inter, perm.take_inter, disjoint_take_drop, h, h']
{ max_depth := 7 },
end
/- enumerating permutations -/
section permutations
theorem perm_of_mem_permutations_aux :
∀ {ts is l : list α}, l ∈ permutations_aux ts is → l ~ ts ++ is :=
begin
refine permutations_aux.rec (by simp) _,
introv IH1 IH2 m,
rw [permutations_aux_cons, permutations, mem_foldr_permutations_aux2] at m,
rcases m with m | ⟨l₁, l₂, m, _, e⟩,
{ exact (IH1 m).trans perm_middle },
{ subst e,
have p : l₁ ++ l₂ ~ is,
{ simp [permutations] at m,
cases m with e m, {simp [e]},
exact is.append_nil ▸ IH2 m },
exact ((perm_middle.trans (p.cons _)).append_right _).trans (perm_append_comm.cons _) }
end
theorem perm_of_mem_permutations {l₁ l₂ : list α}
(h : l₁ ∈ permutations l₂) : l₁ ~ l₂ :=
(eq_or_mem_of_mem_cons h).elim (λ e, e ▸ perm.refl _)
(λ m, append_nil l₂ ▸ perm_of_mem_permutations_aux m)
theorem length_permutations_aux : ∀ ts is : list α,
length (permutations_aux ts is) + is.length! = (length ts + length is)! :=
begin
refine permutations_aux.rec (by simp) _,
intros t ts is IH1 IH2,
have IH2 : length (permutations_aux is nil) + 1 = is.length!,
{ simpa using IH2 },
simp [-add_comm, nat.factorial, nat.add_succ, mul_comm] at IH1,
rw [permutations_aux_cons,
length_foldr_permutations_aux2' _ _ _ _ _
(λ l m, (perm_of_mem_permutations m).length_eq),
permutations, length, length, IH2,
nat.succ_add, nat.factorial_succ, mul_comm (nat.succ _), ← IH1,
add_comm (_*_), add_assoc, nat.mul_succ, mul_comm]
end
theorem length_permutations (l : list α) : length (permutations l) = (length l)! :=
length_permutations_aux l []
theorem mem_permutations_of_perm_lemma {is l : list α}
(H : l ~ [] ++ is → (∃ ts' ~ [], l = ts' ++ is) ∨ l ∈ permutations_aux is [])
: l ~ is → l ∈ permutations is :=
by simpa [permutations, perm_nil] using H
theorem mem_permutations_aux_of_perm :
∀ {ts is l : list α}, l ~ is ++ ts → (∃ is' ~ is, l = is' ++ ts) ∨ l ∈ permutations_aux ts is :=
begin
refine permutations_aux.rec (by simp) _,
intros t ts is IH1 IH2 l p,
rw [permutations_aux_cons, mem_foldr_permutations_aux2],
rcases IH1 (p.trans perm_middle) with ⟨is', p', e⟩ | m,
{ clear p, subst e,
rcases mem_split (p'.symm.subset (mem_cons_self _ _)) with ⟨l₁, l₂, e⟩,
subst is',
have p := (perm_middle.symm.trans p').cons_inv,
cases l₂ with a l₂',
{ exact or.inl ⟨l₁, by simpa using p⟩ },
{ exact or.inr (or.inr ⟨l₁, a::l₂',
mem_permutations_of_perm_lemma IH2 p, by simp⟩) } },
{ exact or.inr (or.inl m) }
end
@[simp] theorem mem_permutations {s t : list α} : s ∈ permutations t ↔ s ~ t :=
⟨perm_of_mem_permutations, mem_permutations_of_perm_lemma mem_permutations_aux_of_perm⟩
theorem perm_permutations'_aux_comm (a b : α) (l : list α) :
(permutations'_aux a l).bind (permutations'_aux b) ~
(permutations'_aux b l).bind (permutations'_aux a) :=
begin
induction l with c l ih, {simp [swap]},
simp [permutations'_aux], apply perm.swap',
have : ∀ a b,
(map (cons c) (permutations'_aux a l)).bind (permutations'_aux b) ~
map (cons b ∘ cons c) (permutations'_aux a l) ++
map (cons c) ((permutations'_aux a l).bind (permutations'_aux b)),
{ intros,
simp only [map_bind, permutations'_aux],
refine (bind_append_perm _ (λ x, [_]) _).symm.trans _,
rw [← map_eq_bind, ← bind_map] },
refine (((this _ _).append_left _).trans _).trans ((this _ _).append_left _).symm,
rw [← append_assoc, ← append_assoc],
exact perm_append_comm.append (ih.map _),
end
theorem perm.permutations' {s t : list α} (p : s ~ t) :
permutations' s ~ permutations' t :=
begin
induction p with a s t p IH a b l s t u p₁ p₂ IH₁ IH₂, {simp},
{ simp only [permutations'], exact IH.bind_right _ },
{ simp only [permutations'],
rw [bind_assoc, bind_assoc], apply perm.bind_left, apply perm_permutations'_aux_comm },
{ exact IH₁.trans IH₂ }
end
theorem permutations_perm_permutations' (ts : list α) : ts.permutations ~ ts.permutations' :=
begin
obtain ⟨n, h⟩ : ∃ n, length ts < n := ⟨_, nat.lt_succ_self _⟩,
induction n with n IH generalizing ts, {cases h},
refine list.reverse_rec_on ts (λ h, _) (λ ts t _ h, _) h, {simp [permutations]},
rw [← concat_eq_append, length_concat, nat.succ_lt_succ_iff] at h,
have IH₂ := (IH ts.reverse (by rwa [length_reverse])).trans (reverse_perm _).permutations',
simp only [permutations_append, foldr_permutations_aux2,
permutations_aux_nil, permutations_aux_cons, append_nil],
refine (perm_append_comm.trans ((IH₂.bind_right _).append ((IH _ h).map _))).trans
(perm.trans _ perm_append_comm.permutations'),
rw [map_eq_bind, singleton_append, permutations'],
convert bind_append_perm _ _ _, funext ys,
rw [permutations'_aux_eq_permutations_aux2, permutations_aux2_append]
end
@[simp] theorem mem_permutations' {s t : list α} : s ∈ permutations' t ↔ s ~ t :=
(permutations_perm_permutations' _).symm.mem_iff.trans mem_permutations
theorem perm.permutations {s t : list α} (h : s ~ t) : permutations s ~ permutations t :=
(permutations_perm_permutations' _).trans $ h.permutations'.trans
(permutations_perm_permutations' _).symm
@[simp] theorem perm_permutations_iff {s t : list α} : permutations s ~ permutations t ↔ s ~ t :=
⟨λ h, mem_permutations.1 $ h.mem_iff.1 $ mem_permutations.2 (perm.refl _), perm.permutations⟩
@[simp] theorem perm_permutations'_iff {s t : list α} : permutations' s ~ permutations' t ↔ s ~ t :=
⟨λ h, mem_permutations'.1 $ h.mem_iff.1 $ mem_permutations'.2 (perm.refl _), perm.permutations'⟩
lemma nth_le_permutations'_aux (s : list α) (x : α) (n : ℕ)
(hn : n < length (permutations'_aux x s)) :
(permutations'_aux x s).nth_le n hn = s.insert_nth n x :=
begin
induction s with y s IH generalizing n,
{ simp only [length, permutations'_aux, nat.lt_one_iff] at hn,
simp [hn] },
{ cases n,
{ simp },
{ simpa using IH _ _ } }
end
lemma count_permutations'_aux_self [decidable_eq α] (l : list α) (x : α) :
count (x :: l) (permutations'_aux x l) = length (take_while ((=) x) l) + 1 :=
begin
induction l with y l IH generalizing x,
{ simp [take_while], },
{ rw [permutations'_aux, count_cons_self],
by_cases hx : x = y,
{ subst hx,
simpa [take_while, nat.succ_inj'] using IH _ },
{ rw take_while,
rw if_neg hx,
cases permutations'_aux x l with a as,
{ simp },
{ rw [count_eq_zero_of_not_mem, length, zero_add],
simp [hx, ne.symm hx] } } }
end
@[simp] lemma length_permutations'_aux (s : list α) (x : α) :
length (permutations'_aux x s) = length s + 1 :=
begin
induction s with y s IH,
{ simp },
{ simpa using IH }
end
@[simp] lemma permutations'_aux_nth_le_zero (s : list α) (x : α)
(hn : 0 < length (permutations'_aux x s) := by simp) :
(permutations'_aux x s).nth_le 0 hn = x :: s :=
nth_le_permutations'_aux _ _ _ _
lemma injective_permutations'_aux (x : α) : function.injective (permutations'_aux x) :=
begin
intros s t h,
apply insert_nth_injective s.length x,
have hl : s.length = t.length := by simpa using congr_arg length h,
rw [←nth_le_permutations'_aux s x s.length (by simp),
←nth_le_permutations'_aux t x s.length (by simp [hl])],
simp [h, hl]
end
lemma nodup_permutations'_aux_of_not_mem (s : list α) (x : α) (hx : x ∉ s) :
nodup (permutations'_aux x s) :=
begin
induction s with y s IH,
{ simp },
{ simp only [not_or_distrib, mem_cons_iff] at hx,
simp only [not_and, exists_eq_right_right, mem_map, permutations'_aux, nodup_cons],
refine ⟨λ _, ne.symm hx.left, _⟩,
rw nodup_map_iff,
{ exact IH hx.right },
{ simp } }
end
lemma nodup_permutations'_aux_iff {s : list α} {x : α} :
nodup (permutations'_aux x s) ↔ x ∉ s :=
begin
refine ⟨λ h, _, nodup_permutations'_aux_of_not_mem _ _⟩,
intro H,
obtain ⟨k, hk, hk'⟩ := nth_le_of_mem H,
rw nodup_iff_nth_le_inj at h,
suffices : k = k + 1,
{ simpa using this },
refine h k (k + 1) _ _ _,
{ simpa [nat.lt_succ_iff] using hk.le },
{ simpa using hk },
rw [nth_le_permutations'_aux, nth_le_permutations'_aux],
have hl : length (insert_nth k x s) = length (insert_nth (k + 1) x s),
{ rw [length_insert_nth _ _ hk.le, length_insert_nth _ _ (nat.succ_le_of_lt hk)] },
refine ext_le hl (λ n hn hn', _),
rcases lt_trichotomy n k with H|rfl|H,
{ rw [nth_le_insert_nth_of_lt _ _ _ _ H (H.trans hk),
nth_le_insert_nth_of_lt _ _ _ _ (H.trans (nat.lt_succ_self _))] },
{ rw [nth_le_insert_nth_self _ _ _ hk.le,
nth_le_insert_nth_of_lt _ _ _ _ (nat.lt_succ_self _) hk, hk'] },
{ rcases (nat.succ_le_of_lt H).eq_or_lt with rfl|H',
{ rw [nth_le_insert_nth_self _ _ _ (nat.succ_le_of_lt hk)],
convert hk' using 1,
convert nth_le_insert_nth_add_succ _ _ _ 0 _,
simpa using hk },
{ obtain ⟨m, rfl⟩ := nat.exists_eq_add_of_lt H',
rw [length_insert_nth _ _ hk.le, nat.succ_lt_succ_iff, nat.succ_add] at hn,
rw nth_le_insert_nth_add_succ,
convert nth_le_insert_nth_add_succ s x k m.succ _ using 2,
{ simp [nat.add_succ, nat.succ_add] },
{ simp [add_left_comm, add_comm] },
{ simpa [nat.add_succ] using hn },
{ simpa [nat.succ_add] using hn } } }
end
lemma nodup_permutations (s : list α) (hs : nodup s) :
nodup s.permutations :=
begin
rw (permutations_perm_permutations' s).nodup_iff,
induction hs with x l h h' IH,
{ simp },
{ rw [permutations'],
rw nodup_bind,
split,
{ intros ys hy,
rw mem_permutations' at hy,
rw [nodup_permutations'_aux_iff, hy.mem_iff],
exact λ H, h x H rfl },
{ refine IH.pairwise_of_forall_ne (λ as ha bs hb H, _),
rw disjoint_iff_ne,
rintro a ha' b hb' rfl,
obtain ⟨n, hn, hn'⟩ := nth_le_of_mem ha',
obtain ⟨m, hm, hm'⟩ := nth_le_of_mem hb',
rw mem_permutations' at ha hb,
have hl : as.length = bs.length := (ha.trans hb.symm).length_eq,
simp only [nat.lt_succ_iff, length_permutations'_aux] at hn hm,
rw nth_le_permutations'_aux at hn' hm',
have hx : nth_le (insert_nth n x as) m
(by rwa [length_insert_nth _ _ hn, nat.lt_succ_iff, hl]) = x,
{ simp [hn', ←hm', hm] },
have hx' : nth_le (insert_nth m x bs) n
(by rwa [length_insert_nth _ _ hm, nat.lt_succ_iff, ←hl]) = x,
{ simp [hm', ←hn', hn] },
rcases lt_trichotomy n m with ht|ht|ht,
{ suffices : x ∈ bs,
{ exact h x (hb.subset this) rfl },
rw [←hx', nth_le_insert_nth_of_lt _ _ _ _ ht (ht.trans_le hm)],
exact nth_le_mem _ _ _ },
{ simp only [ht] at hm' hn',
rw ←hm' at hn',
exact H (insert_nth_injective _ _ hn') },
{ suffices : x ∈ as,
{ exact h x (ha.subset this) rfl },
rw [←hx, nth_le_insert_nth_of_lt _ _ _ _ ht (ht.trans_le hn)],
exact nth_le_mem _ _ _ } } }
end
-- TODO: `nodup s.permutations ↔ nodup s`
-- TODO: `count s s.permutations = (zip_with count s s.tails).prod`
end permutations
end list
|
363f0c12ce872c6fa2590f9003ced702ed116cbe | 6b02ce66658141f3e0aa3dfa88cd30bbbb24d17b | /src/Lean/Elab/Term.lean | 1b6f3193f78f065a726843c116961ad8aec559fa | [
"Apache-2.0"
] | permissive | pbrinkmeier/lean4 | d31991fd64095e64490cb7157bcc6803f9c48af4 | 32fd82efc2eaf1232299e930ec16624b370eac39 | refs/heads/master | 1,681,364,001,662 | 1,618,425,427,000 | 1,618,425,427,000 | 358,314,562 | 0 | 0 | Apache-2.0 | 1,618,504,558,000 | 1,618,501,999,000 | null | UTF-8 | Lean | false | false | 72,129 | 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.ResolveName
import Lean.Util.Sorry
import Lean.Util.ReplaceExpr
import Lean.Structure
import Lean.Meta.ExprDefEq
import Lean.Meta.AppBuilder
import Lean.Meta.SynthInstance
import Lean.Meta.CollectMVars
import Lean.Meta.Coe
import Lean.Meta.Tactic.Util
import Lean.Hygiene
import Lean.Util.RecDepth
import Lean.Elab.Log
import Lean.Elab.Level
import Lean.Elab.Attributes
import Lean.Elab.AutoBound
import Lean.Elab.InfoTree
import Lean.Elab.Open
import Lean.Elab.SetOption
namespace Lean.Elab.Term
/-
Set isDefEq configuration for the elaborator.
Note that we enable all approximations but `quasiPatternApprox`
In Lean3 and Lean 4, we used to use the quasi-pattern approximation during elaboration.
The example:
```
def ex : StateT δ (StateT σ Id) σ :=
monadLift (get : StateT σ Id σ)
```
demonstrates why it produces counterintuitive behavior.
We have the `Monad-lift` application:
```
@monadLift ?m ?n ?c ?α (get : StateT σ id σ) : ?n ?α
```
It produces the following unification problem when we process the expected type:
```
?n ?α =?= StateT δ (StateT σ id) σ
==> (approximate using first-order unification)
?n := StateT δ (StateT σ id)
?α := σ
```
Then, we need to solve:
```
?m ?α =?= StateT σ id σ
==> instantiate metavars
?m σ =?= StateT σ id σ
==> (approximate since it is a quasi-pattern unification constraint)
?m := fun σ => StateT σ id σ
```
Note that the constraint is not a Milner pattern because σ is in
the local context of `?m`. We are ignoring the other possible solutions:
```
?m := fun σ' => StateT σ id σ
?m := fun σ' => StateT σ' id σ
?m := fun σ' => StateT σ id σ'
```
We need the quasi-pattern approximation for elaborating recursor-like expressions (e.g., dependent `match with` expressions).
If we had use first-order unification, then we would have produced
the right answer: `?m := StateT σ id`
Haskell would work on this example since it always uses
first-order unification.
-/
def setElabConfig (cfg : Meta.Config) : Meta.Config :=
{ cfg with foApprox := true, ctxApprox := true, constApprox := false, quasiPatternApprox := false }
structure Context where
fileName : String
fileMap : FileMap
declName? : Option Name := none
macroStack : MacroStack := []
currMacroScope : MacroScope := firstFrontendMacroScope
/- When `mayPostpone == true`, an elaboration function may interrupt its execution by throwing `Exception.postpone`.
The function `elabTerm` catches this exception and creates fresh synthetic metavariable `?m`, stores `?m` in
the list of pending synthetic metavariables, and returns `?m`. -/
mayPostpone : Bool := true
/- When `errToSorry` is set to true, the method `elabTerm` catches
exceptions and converts them into synthetic `sorry`s.
The implementation of choice nodes and overloaded symbols rely on the fact
that when `errToSorry` is set to false for an elaboration function `F`, then
`errToSorry` remains `false` for all elaboration functions invoked by `F`.
That is, it is safe to transition `errToSorry` from `true` to `false`, but
we must not set `errToSorry` to `true` when it is currently set to `false`. -/
errToSorry : Bool := true
/- When `autoBoundImplicit` is set to true, instead of producing
an "unknown identifier" error for unbound variables, we generate an
internal exception. This exception is caught at `elabBinders` and
`elabTypeWithUnboldImplicit`. Both methods add implicit declarations
for the unbound variable and try again. -/
autoBoundImplicit : Bool := false
autoBoundImplicits : Std.PArray Expr := {}
/-- Map from user name to internal unique name -/
sectionVars : NameMap Name := {}
/-- Map from internal name to fvar -/
sectionFVars : NameMap Expr := {}
/-- Enable/disable implicit lambdas feature. -/
implicitLambda : Bool := true
/-- Saved context for postponed terms and tactics to be executed. -/
structure SavedContext where
declName? : Option Name
options : Options
openDecls : List OpenDecl
macroStack : MacroStack
errToSorry : Bool
/-- We use synthetic metavariables as placeholders for pending elaboration steps. -/
inductive SyntheticMVarKind where
-- typeclass instance search
| typeClass
/- Similar to typeClass, but error messages are different.
if `f?` is `some f`, we produce an application type mismatch error message.
Otherwise, if `header?` is `some header`, we generate the error `(header ++ "has type" ++ eType ++ "but it is expected to have type" ++ expectedType)`
Otherwise, we generate the error `("type mismatch" ++ e ++ "has type" ++ eType ++ "but it is expected to have type" ++ expectedType)` -/
| coe (header? : Option String) (eNew : Expr) (expectedType : Expr) (eType : Expr) (e : Expr) (f? : Option Expr)
-- tactic block execution
| tactic (tacticCode : Syntax) (ctx : SavedContext)
-- `elabTerm` call that threw `Exception.postpone` (input is stored at `SyntheticMVarDecl.ref`)
| postponed (ctx : SavedContext)
instance : ToString SyntheticMVarKind where
toString
| SyntheticMVarKind.typeClass => "typeclass"
| SyntheticMVarKind.coe .. => "coe"
| SyntheticMVarKind.tactic .. => "tactic"
| SyntheticMVarKind.postponed .. => "postponed"
structure SyntheticMVarDecl where
mvarId : MVarId
stx : Syntax
kind : SyntheticMVarKind
inductive MVarErrorKind where
| implicitArg (ctx : Expr)
| hole
| custom (msgData : MessageData)
instance : ToString MVarErrorKind where
toString
| MVarErrorKind.implicitArg ctx => "implicitArg"
| MVarErrorKind.hole => "hole"
| MVarErrorKind.custom msg => "custom"
structure MVarErrorInfo where
mvarId : MVarId
ref : Syntax
kind : MVarErrorKind
structure LetRecToLift where
ref : Syntax
fvarId : FVarId
attrs : Array Attribute
shortDeclName : Name
declName : Name
lctx : LocalContext
localInstances : LocalInstances
type : Expr
val : Expr
mvarId : MVarId
structure State where
levelNames : List Name := []
syntheticMVars : List SyntheticMVarDecl := []
mvarErrorInfos : List MVarErrorInfo := []
messages : MessageLog := {}
letRecsToLift : List LetRecToLift := []
infoState : InfoState := {}
deriving Inhabited
abbrev TermElabM := ReaderT Context $ StateRefT State MetaM
abbrev TermElab := Syntax → Option Expr → TermElabM Expr
open Meta
instance : Inhabited (TermElabM α) where
default := throw arbitrary
structure SavedState where
meta : Meta.SavedState
«elab» : State
deriving Inhabited
protected def saveState : TermElabM SavedState := do
pure { meta := (← Meta.saveState), «elab» := (← get) }
def SavedState.restore (s : SavedState) : TermElabM Unit := do
let traceState ← getTraceState -- We never backtrack trace message
s.meta.restore
set s.elab
setTraceState traceState
instance : MonadBacktrack SavedState TermElabM where
saveState := Term.saveState
restoreState b := b.restore
abbrev TermElabResult (α : Type) := EStateM.Result Exception SavedState α
instance [Inhabited α] : Inhabited (TermElabResult α) where
default := EStateM.Result.ok arbitrary arbitrary
def setMessageLog (messages : MessageLog) : TermElabM Unit :=
modify fun s => { s with messages := messages }
def resetMessageLog : TermElabM Unit :=
setMessageLog {}
def getMessageLog : TermElabM MessageLog :=
return (← get).messages
/--
Execute `x`, save resulting expression and new state.
If `x` fails, then it also stores exception and new state.
Remark: we do not capture `Exception.postpone`. -/
@[inline] def observing (x : TermElabM α) : TermElabM (TermElabResult α) := do
let s ← saveState
try
let e ← x
let sNew ← saveState
s.restore
pure (EStateM.Result.ok e sNew)
catch
| ex@(Exception.error _ _) =>
let sNew ← saveState
s.restore
pure (EStateM.Result.error ex sNew)
| ex@(Exception.internal id _) =>
if id == postponeExceptionId then s.restore
throw ex
/--
Apply the result/exception and state captured with `observing`.
We use this method to implement overloaded notation and symbols. -/
@[inline] def applyResult (result : TermElabResult α) : TermElabM α :=
match result with
| EStateM.Result.ok a r => do r.restore; pure a
| EStateM.Result.error ex r => do r.restore; throw ex
/--
Execute `x`, but keep state modifications only if `x` did not postpone.
This method is useful to implement elaboration functions that cannot decide whether
they need to postpone or not without updating the state. -/
def commitIfDidNotPostpone (x : TermElabM α) : TermElabM α := do
-- We just reuse the implementation of `observing` and `applyResult`.
let r ← observing x
applyResult r
/--
Execute `x` but discard changes performed at `Term.State` and `Meta.State`.
Recall that the environment is at `Core.State`. Thus, any updates to it will
be preserved. This method is useful for performing computations where all
metavariable must be resolved or discarded. -/
def withoutModifyingElabMetaState (x : TermElabM α) : TermElabM α := do
let s ← get
let sMeta ← getThe Meta.State
try
x
finally
set s
set sMeta
def getLevelNames : TermElabM (List Name) :=
return (← get).levelNames
def getFVarLocalDecl! (fvar : Expr) : TermElabM LocalDecl := do
match (← getLCtx).find? fvar.fvarId! with
| some d => pure d
| none => unreachable!
instance : AddErrorMessageContext TermElabM where
add ref msg := do
let ctx ← read
let ref := getBetterRef ref ctx.macroStack
let msg ← addMessageContext msg
let msg ← addMacroStack msg ctx.macroStack
pure (ref, msg)
instance : MonadLog TermElabM where
getRef := getRef
getFileMap := return (← read).fileMap
getFileName := return (← read).fileName
logMessage msg := do
let ctx ← readThe Core.Context
let msg := { msg with data := MessageData.withNamingContext { currNamespace := ctx.currNamespace, openDecls := ctx.openDecls } msg.data };
modify fun s => { s with messages := s.messages.add msg }
protected def getCurrMacroScope : TermElabM MacroScope := do pure (← read).currMacroScope
protected def getMainModule : TermElabM Name := do pure (← getEnv).mainModule
@[inline] protected def withFreshMacroScope (x : TermElabM α) : TermElabM α := do
let fresh ← modifyGetThe Core.State (fun st => (st.nextMacroScope, { st with nextMacroScope := st.nextMacroScope + 1 }))
withReader (fun ctx => { ctx with currMacroScope := fresh }) x
instance : MonadQuotation TermElabM where
getCurrMacroScope := Term.getCurrMacroScope
getMainModule := Term.getMainModule
withFreshMacroScope := Term.withFreshMacroScope
instance : MonadInfoTree TermElabM where
getInfoState := return (← get).infoState
modifyInfoState f := modify fun s => { s with infoState := f s.infoState }
unsafe def mkTermElabAttributeUnsafe : IO (KeyedDeclsAttribute TermElab) :=
mkElabAttribute TermElab `Lean.Elab.Term.termElabAttribute `builtinTermElab `termElab `Lean.Parser.Term `Lean.Elab.Term.TermElab "term"
@[implementedBy mkTermElabAttributeUnsafe]
constant mkTermElabAttribute : IO (KeyedDeclsAttribute TermElab)
builtin_initialize termElabAttribute : KeyedDeclsAttribute TermElab ← mkTermElabAttribute
/--
Auxiliary datatatype for presenting a Lean lvalue modifier.
We represent a unelaborated lvalue as a `Syntax` (or `Expr`) and `List LVal`.
Example: `a.foo[i].1` is represented as the `Syntax` `a` and the list
`[LVal.fieldName "foo", LVal.getOp i, LVal.fieldIdx 1]`.
Recall that the notation `a[i]` is not just for accessing arrays in Lean. -/
inductive LVal where
| fieldIdx (ref : Syntax) (i : Nat)
/- Field `suffix?` is for producing better error messages because `x.y` may be a field access or a hierachical/composite name.
`ref` is the syntax object representing the field. `targetStx` is the target object being accessed. -/
| fieldName (ref : Syntax) (name : String) (suffix? : Option Name) (targetStx : Syntax)
| getOp (ref : Syntax) (idx : Syntax)
def LVal.getRef : LVal → Syntax
| LVal.fieldIdx ref _ => ref
| LVal.fieldName ref .. => ref
| LVal.getOp ref _ => ref
def LVal.isFieldName : LVal → Bool
| LVal.fieldName .. => true
| _ => false
instance : ToString LVal where
toString
| LVal.fieldIdx _ i => toString i
| LVal.fieldName _ n .. => n
| LVal.getOp _ idx => "[" ++ toString idx ++ "]"
def getDeclName? : TermElabM (Option Name) := return (← read).declName?
def getLetRecsToLift : TermElabM (List LetRecToLift) := return (← get).letRecsToLift
def isExprMVarAssigned (mvarId : MVarId) : TermElabM Bool := return (← getMCtx).isExprAssigned mvarId
def getMVarDecl (mvarId : MVarId) : TermElabM MetavarDecl := return (← getMCtx).getDecl mvarId
def assignLevelMVar (mvarId : MVarId) (val : Level) : TermElabM Unit := modifyThe Meta.State fun s => { s with mctx := s.mctx.assignLevel mvarId val }
def withDeclName (name : Name) (x : TermElabM α) : TermElabM α :=
withReader (fun ctx => { ctx with declName? := name }) x
def setLevelNames (levelNames : List Name) : TermElabM Unit :=
modify fun s => { s with levelNames := levelNames }
def withLevelNames (levelNames : List Name) (x : TermElabM α) : TermElabM α := do
let levelNamesSaved ← getLevelNames
setLevelNames levelNames
try x finally setLevelNames levelNamesSaved
def withoutErrToSorry (x : TermElabM α) : TermElabM α :=
withReader (fun ctx => { ctx with errToSorry := false }) x
/-- For testing `TermElabM` methods. The #eval command will sign the error. -/
def throwErrorIfErrors : TermElabM Unit := do
if (← get).messages.hasErrors then
throwError "Error(s)"
@[inline] def traceAtCmdPos (cls : Name) (msg : Unit → MessageData) : TermElabM Unit :=
withRef Syntax.missing $ trace cls msg
def ppGoal (mvarId : MVarId) : TermElabM Format :=
Meta.ppGoal mvarId
open Level (LevelElabM)
def liftLevelM (x : LevelElabM α) : TermElabM α := do
let ctx ← read
let ref ← getRef
let mctx ← getMCtx
let ngen ← getNGen
let lvlCtx : Level.Context := { options := (← getOptions), ref := ref, autoBoundImplicit := ctx.autoBoundImplicit }
match (x lvlCtx).run { ngen := ngen, mctx := mctx, levelNames := (← getLevelNames) } with
| EStateM.Result.ok a newS => setMCtx newS.mctx; setNGen newS.ngen; setLevelNames newS.levelNames; pure a
| EStateM.Result.error ex _ => throw ex
def elabLevel (stx : Syntax) : TermElabM Level :=
liftLevelM $ Level.elabLevel stx
/- Elaborate `x` with `stx` on the macro stack -/
@[inline] def withMacroExpansion (beforeStx afterStx : Syntax) (x : TermElabM α) : TermElabM α :=
withMacroExpansionInfo beforeStx afterStx do
withReader (fun ctx => { ctx with macroStack := { before := beforeStx, after := afterStx } :: ctx.macroStack }) x
/-
Add the given metavariable to the list of pending synthetic metavariables.
The method `synthesizeSyntheticMVars` is used to process the metavariables on this list. -/
def registerSyntheticMVar (stx : Syntax) (mvarId : MVarId) (kind : SyntheticMVarKind) : TermElabM Unit := do
modify fun s => { s with syntheticMVars := { mvarId := mvarId, stx := stx, kind := kind } :: s.syntheticMVars }
def registerSyntheticMVarWithCurrRef (mvarId : MVarId) (kind : SyntheticMVarKind) : TermElabM Unit := do
registerSyntheticMVar (← getRef) mvarId kind
def registerMVarErrorHoleInfo (mvarId : MVarId) (ref : Syntax) : TermElabM Unit := do
modify fun s => { s with mvarErrorInfos := { mvarId := mvarId, ref := ref, kind := MVarErrorKind.hole } :: s.mvarErrorInfos }
def registerMVarErrorImplicitArgInfo (mvarId : MVarId) (ref : Syntax) (app : Expr) : TermElabM Unit := do
modify fun s => { s with mvarErrorInfos := { mvarId := mvarId, ref := ref, kind := MVarErrorKind.implicitArg app } :: s.mvarErrorInfos }
def registerMVarErrorCustomInfo (mvarId : MVarId) (ref : Syntax) (msgData : MessageData) : TermElabM Unit := do
modify fun s => { s with mvarErrorInfos := { mvarId := mvarId, ref := ref, kind := MVarErrorKind.custom msgData } :: s.mvarErrorInfos }
def registerCustomErrorIfMVar (e : Expr) (ref : Syntax) (msgData : MessageData) : TermElabM Unit :=
match e.getAppFn with
| Expr.mvar mvarId _ => registerMVarErrorCustomInfo mvarId ref msgData
| _ => pure ()
/-
Auxiliary method for reporting errors of the form "... contains metavariables ...".
This kind of error is thrown, for example, at `Match.lean` where elaboration
cannot continue if there are metavariables in patterns.
We only want to log it if we haven't logged any error so far. -/
def throwMVarError (m : MessageData) : TermElabM α := do
if (← get).messages.hasErrors then
throwAbortTerm
else
throwError m
def MVarErrorInfo.logError (mvarErrorInfo : MVarErrorInfo) (extraMsg? : Option MessageData) : TermElabM Unit := do
match mvarErrorInfo.kind with
| MVarErrorKind.implicitArg app => do
let app ← instantiateMVars app
let msg : MessageData := m!"don't know how to synthesize implicit argument{indentExpr app.setAppPPExplicitForExposingMVars}"
let msg := msg ++ Format.line ++ "context:" ++ Format.line ++ MessageData.ofGoal mvarErrorInfo.mvarId
logErrorAt mvarErrorInfo.ref (appendExtra msg)
| MVarErrorKind.hole => do
let msg : MessageData := "don't know how to synthesize placeholder"
let msg := msg ++ Format.line ++ "context:" ++ Format.line ++ MessageData.ofGoal mvarErrorInfo.mvarId
logErrorAt mvarErrorInfo.ref (appendExtra msg)
| MVarErrorKind.custom msg =>
logErrorAt mvarErrorInfo.ref (appendExtra msg)
where
appendExtra (msg : MessageData) : MessageData :=
match extraMsg? with
| none => msg
| some extraMsg => msg ++ extraMsg
/--
Try to log errors for the unassigned metavariables `pendingMVarIds`.
Return `true` if there were "unfilled holes", and we should "abort" declaration.
TODO: try to fill "all" holes using synthetic "sorry's"
Remark: We only log the "unfilled holes" as new errors if no error has been logged so far. -/
def logUnassignedUsingErrorInfos (pendingMVarIds : Array MVarId) (extraMsg? : Option MessageData := none) : TermElabM Bool := do
let s ← get
let hasOtherErrors := s.messages.hasErrors
let mut hasNewErrors := false
let mut alreadyVisited : NameSet := {}
for mvarErrorInfo in s.mvarErrorInfos do
let mvarId := mvarErrorInfo.mvarId
unless alreadyVisited.contains mvarId do
alreadyVisited := alreadyVisited.insert mvarId
let foundError ← withMVarContext mvarId do
/- The metavariable `mvarErrorInfo.mvarId` may have been assigned or
delayed assigned to another metavariable that is unassigned. -/
let mvarDeps ← getMVars (mkMVar mvarId)
if mvarDeps.any pendingMVarIds.contains then do
unless hasOtherErrors do
mvarErrorInfo.logError extraMsg?
pure true
else
pure false
if foundError then
hasNewErrors := true
return hasNewErrors
/-- Ensure metavariables registered using `registerMVarErrorInfos` (and used in the given declaration) have been assigned. -/
def ensureNoUnassignedMVars (decl : Declaration) : TermElabM Unit := do
let pendingMVarIds ← getMVarsAtDecl decl
if (← logUnassignedUsingErrorInfos pendingMVarIds) then
throwAbortCommand
/-
Execute `x` without allowing it to postpone elaboration tasks.
That is, `tryPostpone` is a noop. -/
@[inline] def withoutPostponing (x : TermElabM α) : TermElabM α :=
withReader (fun ctx => { ctx with mayPostpone := false }) x
/-- Creates syntax for `(` <ident> `:` <type> `)` -/
def mkExplicitBinder (ident : Syntax) (type : Syntax) : Syntax :=
mkNode ``Lean.Parser.Term.explicitBinder #[mkAtom "(", mkNullNode #[ident], mkNullNode #[mkAtom ":", type], mkNullNode, mkAtom ")"]
/--
Convert unassigned universe level metavariables into parameters.
The new parameter names are of the form `u_i` where `i >= nextParamIdx`.
The method returns the updated expression and new `nextParamIdx`.
Remark: we make sure the generated parameter names do not clash with the universes at `ctx.levelNames`. -/
def levelMVarToParam (e : Expr) (nextParamIdx : Nat := 1) : TermElabM (Expr × Nat) := do
let mctx ← getMCtx
let levelNames ← getLevelNames
let r := mctx.levelMVarToParam (fun n => levelNames.elem n) e `u nextParamIdx
setMCtx r.mctx
pure (r.expr, r.nextParamIdx)
/-- Variant of `levelMVarToParam` where `nextParamIdx` is stored in a state monad. -/
def levelMVarToParam' (e : Expr) : StateRefT Nat TermElabM Expr := do
let nextParamIdx ← get
let (e, nextParamIdx) ← levelMVarToParam e nextParamIdx
set nextParamIdx
pure e
/--
Auxiliary method for creating fresh binder names.
Do not confuse with the method for creating fresh free/meta variable ids. -/
def mkFreshBinderName : TermElabM Name :=
withFreshMacroScope $ MonadQuotation.addMacroScope `x
/--
Auxiliary method for creating a `Syntax.ident` containing
a fresh name. This method is intended for creating fresh binder names.
It is just a thin layer on top of `mkFreshUserName`. -/
def mkFreshIdent (ref : Syntax) : TermElabM Syntax :=
return mkIdentFrom ref (← mkFreshBinderName)
private def applyAttributesCore
(declName : Name) (attrs : Array Attribute)
(applicationTime? : Option AttributeApplicationTime) : TermElabM Unit :=
for attr in attrs do
let env ← getEnv
match getAttributeImpl env attr.name with
| Except.error errMsg => throwError errMsg
| Except.ok attrImpl =>
match applicationTime? with
| none => attrImpl.add declName attr.stx attr.kind
| some applicationTime =>
if applicationTime == attrImpl.applicationTime then
attrImpl.add declName attr.stx attr.kind
/-- Apply given attributes **at** a given application time -/
def applyAttributesAt (declName : Name) (attrs : Array Attribute) (applicationTime : AttributeApplicationTime) : TermElabM Unit :=
applyAttributesCore declName attrs applicationTime
def applyAttributes (declName : Name) (attrs : Array Attribute) : TermElabM Unit :=
applyAttributesCore declName attrs none
def mkTypeMismatchError (header? : Option String) (e : Expr) (eType : Expr) (expectedType : Expr) : TermElabM MessageData := do
let header : MessageData := match header? with
| some header => m!"{header} "
| none => m!"type mismatch{indentExpr e}\n"
return m!"{header}{← mkHasTypeButIsExpectedMsg eType expectedType}"
def throwTypeMismatchError (header? : Option String) (expectedType : Expr) (eType : Expr) (e : Expr)
(f? : Option Expr := none) (extraMsg? : Option MessageData := none) : TermElabM α := do
/-
We ignore `extraMsg?` for now. In all our tests, it contained no useful information. It was
always of the form:
```
failed to synthesize instance
CoeT <eType> <e> <expectedType>
```
We should revisit this decision in the future and decide whether it may contain useful information
or not. -/
let extraMsg := Format.nil
/-
let extraMsg : MessageData := match extraMsg? with
| none => Format.nil
| some extraMsg => Format.line ++ extraMsg;
-/
match f? with
| none => throwError "{← mkTypeMismatchError header? e eType expectedType}{extraMsg}"
| some f => Meta.throwAppTypeMismatch f e extraMsg
@[inline] def withoutMacroStackAtErr (x : TermElabM α) : TermElabM α :=
withTheReader Core.Context (fun (ctx : Core.Context) => { ctx with options := pp.macroStack.set ctx.options false }) x
/- Try to synthesize metavariable using type class resolution.
This method assumes the local context and local instances of `instMVar` coincide
with the current local context and local instances.
Return `true` if the instance was synthesized successfully, and `false` if
the instance contains unassigned metavariables that are blocking the type class
resolution procedure. Throw an exception if resolution or assignment irrevocably fails. -/
def synthesizeInstMVarCore (instMVar : MVarId) (maxResultSize? : Option Nat := none) : TermElabM Bool := do
let instMVarDecl ← getMVarDecl instMVar
let type := instMVarDecl.type
let type ← instantiateMVars type
let result ← trySynthInstance type maxResultSize?
match result with
| LOption.some val =>
if (← isExprMVarAssigned instMVar) then
let oldVal ← instantiateMVars (mkMVar instMVar)
unless (← isDefEq oldVal val) do
let oldValType ← inferType oldVal
let valType ← inferType val
unless (← isDefEq oldValType valType) do
throwError "synthesized type class instance type is not definitionally equal to expected type, synthesized{indentExpr val}\nhas type{indentExpr valType}\nexpected{indentExpr oldValType}"
throwError "synthesized type class instance is not definitionally equal to expression inferred by typing rules, synthesized{indentExpr val}\ninferred{indentExpr oldVal}"
else
unless (← isDefEq (mkMVar instMVar) val) do
throwError "failed to assign synthesized type class instance{indentExpr val}"
pure true
| LOption.undef => pure false -- we will try later
| LOption.none => throwError "failed to synthesize instance{indentExpr type}"
register_builtin_option autoLift : Bool := {
defValue := true
descr := "insert monadic lifts (i.e., `liftM` and `liftCoeM`) when needed"
}
register_builtin_option maxCoeSize : Nat := {
defValue := 16
descr := "maximum number of instances used to construct an automatic coercion"
}
def synthesizeCoeInstMVarCore (instMVar : MVarId) : TermElabM Bool := do
synthesizeInstMVarCore instMVar (some (maxCoeSize.get (← getOptions)))
/-
The coercion from `α` to `Thunk α` cannot be implemented using an instance because it would
eagerly evaluate `e` -/
def tryCoeThunk? (expectedType : Expr) (eType : Expr) (e : Expr) : TermElabM (Option Expr) := do
match expectedType with
| Expr.app (Expr.const ``Thunk u _) arg _ =>
if (← isDefEq eType arg) then
pure (some (mkApp2 (mkConst ``Thunk.mk u) arg (mkSimpleThunk e)))
else
pure none
| _ =>
pure none
/--
Try to apply coercion to make sure `e` has type `expectedType`.
Relevant definitions:
```
class CoeT (α : Sort u) (a : α) (β : Sort v)
abbrev coe {α : Sort u} {β : Sort v} (a : α) [CoeT α a β] : β
```
-/
private def tryCoe (errorMsgHeader? : Option String) (expectedType : Expr) (eType : Expr) (e : Expr) (f? : Option Expr) : TermElabM Expr := do
if (← isDefEq expectedType eType) then
return e
else match (← tryCoeThunk? expectedType eType e) with
| some r => return r
| none =>
let u ← getLevel eType
let v ← getLevel expectedType
let coeTInstType := mkAppN (mkConst ``CoeT [u, v]) #[eType, e, expectedType]
let mvar ← mkFreshExprMVar coeTInstType MetavarKind.synthetic
let eNew := mkAppN (mkConst ``coe [u, v]) #[eType, expectedType, e, mvar]
let mvarId := mvar.mvarId!
try
withoutMacroStackAtErr do
if (← synthesizeCoeInstMVarCore mvarId) then
expandCoe eNew
else
-- We create an auxiliary metavariable to represent the result, because we need to execute `expandCoe`
-- after we syntheze `mvar`
let mvarAux ← mkFreshExprMVar expectedType MetavarKind.syntheticOpaque
registerSyntheticMVarWithCurrRef mvarAux.mvarId! (SyntheticMVarKind.coe errorMsgHeader? eNew expectedType eType e f?)
return mvarAux
catch
| Exception.error _ msg => throwTypeMismatchError errorMsgHeader? expectedType eType e f? msg
| _ => throwTypeMismatchError errorMsgHeader? expectedType eType e f?
def isTypeApp? (type : Expr) : TermElabM (Option (Expr × Expr)) := do
let type ← withReducible $ whnf type
match type with
| Expr.app m α _ => pure (some ((← instantiateMVars m), (← instantiateMVars α)))
| _ => pure none
def synthesizeInst (type : Expr) : TermElabM Expr := do
let type ← instantiateMVars type
match (← trySynthInstance type) with
| LOption.some val => pure val
| LOption.undef => throwError "failed to synthesize instance{indentExpr type}"
| LOption.none => throwError "failed to synthesize instance{indentExpr type}"
def isMonadApp (type : Expr) : TermElabM Bool := do
let some (m, _) ← isTypeApp? type | pure false
return (← isMonad? m) |>.isSome
/--
Try to coerce `a : α` into `m β` by first coercing `a : α` into ‵β`, and then using `pure`.
The method is only applied if `α` is not monadic (e.g., `Nat → IO Unit`), and the head symbol
of the resulting type is not a metavariable (e.g., `?m Unit` or `Bool → ?m Nat`).
The main limitation of the approach above is polymorphic code. As usual, coercions and polymorphism
do not interact well. In the example above, the lift is successfully applied to `true`, `false` and `!y`
since none of them is polymorphic
```
def f (x : Bool) : IO Bool := do
let y ← if x == 0 then IO.println "hello"; true else false;
!y
```
On the other hand, the following fails since `+` is polymorphic
```
def f (x : Bool) : IO Nat := do
IO.prinln x
x + x -- Error: failed to synthesize `Add (IO Nat)`
```
-/
private def tryPureCoe? (errorMsgHeader? : Option String) (m β α a : Expr) : TermElabM (Option Expr) :=
commitWhenSome? do
let doIt : TermElabM (Option Expr) := do
try
let aNew ← tryCoe errorMsgHeader? β α a none
let aNew ← mkPure m aNew
pure (some aNew)
catch _ =>
pure none
forallTelescope α fun _ α => do
if (← isMonadApp α) then
pure none
else if !α.getAppFn.isMVar then
doIt
else
pure none
/-
Try coercions and monad lifts to make sure `e` has type `expectedType`.
If `expectedType` is of the form `n β`, we try monad lifts and other extensions.
Otherwise, we just use the basic `tryCoe`.
Extensions for monads.
Given an expected type of the form `n β`, if `eType` is of the form `α`, but not `m α`
1 - Try to coerce ‵α` into ‵β`, and use `pure` to lift it to `n α`.
It only works if `n` implements `Pure`
If `eType` is of the form `m α`. We use the following approaches.
1- Try to unify `n` and `m`. If it succeeds, then we use
```
coeM {m : Type u → Type v} {α β : Type u} [∀ a, CoeT α a β] [Monad m] (x : m α) : m β
```
`n` must be a `Monad` to use this one.
2- If there is monad lift from `m` to `n` and we can unify `α` and `β`, we use
```
liftM : ∀ {m : Type u_1 → Type u_2} {n : Type u_1 → Type u_3} [self : MonadLiftT m n] {α : Type u_1}, m α → n α
```
Note that `n` may not be a `Monad` in this case. This happens quite a bit in code such as
```
def g (x : Nat) : IO Nat := do
IO.println x
pure x
def f {m} [MonadLiftT IO m] : m Nat :=
g 10
```
3- If there is a monad lif from `m` to `n` and a coercion from `α` to `β`, we use
```
liftCoeM {m : Type u → Type v} {n : Type u → Type w} {α β : Type u} [MonadLiftT m n] [∀ a, CoeT α a β] [Monad n] (x : m α) : n β
```
Note that approach 3 does not subsume 1 because it is only applicable if there is a coercion from `α` to `β` for all values in `α`.
This is not the case for example for `pure $ x > 0` when the expected type is `IO Bool`. The given type is `IO Prop`, and
we only have a coercion from decidable propositions. Approach 1 works because it constructs the coercion `CoeT (m Prop) (pure $ x > 0) (m Bool)`
using the instance `pureCoeDepProp`.
Note that, approach 2 is more powerful than `tryCoe`.
Recall that type class resolution never assigns metavariables created by other modules.
Now, consider the following scenario
```lean
def g (x : Nat) : IO Nat := ...
deg h (x : Nat) : StateT Nat IO Nat := do
v ← g x;
IO.Println v;
...
```
Let's assume there is no other occurrence of `v` in `h`.
Thus, we have that the expected of `g x` is `StateT Nat IO ?α`,
and the given type is `IO Nat`. So, even if we add a coercion.
```
instance {α m n} [MonadLiftT m n] {α} : Coe (m α) (n α) := ...
```
It is not applicable because TC would have to assign `?α := Nat`.
On the other hand, TC can easily solve `[MonadLiftT IO (StateT Nat IO)]`
since this goal does not contain any metavariables. And then, we
convert `g x` into `liftM $ g x`.
-/
private def tryLiftAndCoe (errorMsgHeader? : Option String) (expectedType : Expr) (eType : Expr) (e : Expr) (f? : Option Expr) : TermElabM Expr := do
let expectedType ← instantiateMVars expectedType
let eType ← instantiateMVars eType
let throwMismatch {α} : TermElabM α := throwTypeMismatchError errorMsgHeader? expectedType eType e f?
let tryCoeSimple : TermElabM Expr :=
tryCoe errorMsgHeader? expectedType eType e f?
let some (n, β) ← isTypeApp? expectedType | tryCoeSimple
let tryPureCoeAndSimple : TermElabM Expr := do
if autoLift.get (← getOptions) then
match (← tryPureCoe? errorMsgHeader? n β eType e) with
| some eNew => pure eNew
| none => tryCoeSimple
else
tryCoeSimple
let some (m, α) ← isTypeApp? eType | tryPureCoeAndSimple
if (← isDefEq m n) then
let some monadInst ← isMonad? n | tryCoeSimple
try expandCoe (← mkAppOptM ``coeM #[m, α, β, none, monadInst, e]) catch _ => throwMismatch
else if autoLift.get (← getOptions) then
try
-- Construct lift from `m` to `n`
let monadLiftType ← mkAppM ``MonadLiftT #[m, n]
let monadLiftVal ← synthesizeInst monadLiftType
let u_1 ← getDecLevel α
let u_2 ← getDecLevel eType
let u_3 ← getDecLevel expectedType
let eNew := mkAppN (Lean.mkConst ``liftM [u_1, u_2, u_3]) #[m, n, monadLiftVal, α, e]
let eNewType ← inferType eNew
if (← isDefEq expectedType eNewType) then
return eNew -- approach 2 worked
else
let some monadInst ← isMonad? n | tryCoeSimple
let u ← getLevel α
let v ← getLevel β
let coeTInstType := Lean.mkForall `a BinderInfo.default α $ mkAppN (mkConst ``CoeT [u, v]) #[α, mkBVar 0, β]
let coeTInstVal ← synthesizeInst coeTInstType
let eNew ← expandCoe (← mkAppN (Lean.mkConst ``liftCoeM [u_1, u_2, u_3]) #[m, n, α, β, monadLiftVal, coeTInstVal, monadInst, e])
let eNewType ← inferType eNew
unless (← isDefEq expectedType eNewType) do throwMismatch
return eNew -- approach 3 worked
catch _ =>
/-
If `m` is not a monad, then we try to use `tryPureCoe?` and then `tryCoe?`.
Otherwise, we just try `tryCoe?`.
-/
match (← isMonad? m) with
| none => tryPureCoeAndSimple
| some _ => tryCoeSimple
else
tryCoeSimple
/--
If `expectedType?` is `some t`, then ensure `t` and `eType` are definitionally equal.
If they are not, then try coercions.
Argument `f?` is used only for generating error messages. -/
def ensureHasTypeAux (expectedType? : Option Expr) (eType : Expr) (e : Expr)
(f? : Option Expr := none) (errorMsgHeader? : Option String := none) : TermElabM Expr := do
match expectedType? with
| none => pure e
| some expectedType =>
if (← isDefEq eType expectedType) then
pure e
else
tryLiftAndCoe errorMsgHeader? expectedType eType e f?
/--
If `expectedType?` is `some t`, then ensure `t` and type of `e` are definitionally equal.
If they are not, then try coercions. -/
def ensureHasType (expectedType? : Option Expr) (e : Expr) (errorMsgHeader? : Option String := none) : TermElabM Expr :=
match expectedType? with
| none => pure e
| _ => do
let eType ← inferType e
ensureHasTypeAux expectedType? eType e none errorMsgHeader?
private def mkSyntheticSorryFor (expectedType? : Option Expr) : TermElabM Expr := do
let expectedType ← match expectedType? with
| none => mkFreshTypeMVar
| some expectedType => pure expectedType
mkSyntheticSorry expectedType
private def exceptionToSorry (ex : Exception) (expectedType? : Option Expr) : TermElabM Expr := do
let syntheticSorry ← mkSyntheticSorryFor expectedType?
logException ex
pure syntheticSorry
/-- If `mayPostpone == true`, throw `Expection.postpone`. -/
def tryPostpone : TermElabM Unit := do
if (← read).mayPostpone then
throwPostpone
/-- If `mayPostpone == true` and `e`'s head is a metavariable, throw `Exception.postpone`. -/
def tryPostponeIfMVar (e : Expr) : TermElabM Unit := do
if e.getAppFn.isMVar then
let e ← instantiateMVars e
if e.getAppFn.isMVar then
tryPostpone
def tryPostponeIfNoneOrMVar (e? : Option Expr) : TermElabM Unit :=
match e? with
| some e => tryPostponeIfMVar e
| none => tryPostpone
def tryPostponeIfHasMVars (expectedType? : Option Expr) (msg : String) : TermElabM Expr := do
tryPostponeIfNoneOrMVar expectedType?
let some expectedType ← pure expectedType? |
throwError "{msg}, expected type must be known"
let expectedType ← instantiateMVars expectedType
if expectedType.hasExprMVar then
tryPostpone
throwError "{msg}, expected type contains metavariables{indentExpr expectedType}"
pure expectedType
private def saveContext : TermElabM SavedContext :=
return {
macroStack := (← read).macroStack
declName? := (← read).declName?
options := (← getOptions)
openDecls := (← getOpenDecls)
errToSorry := (← read).errToSorry
}
def withSavedContext (savedCtx : SavedContext) (x : TermElabM α) : TermElabM α := do
withReader (fun ctx => { ctx with declName? := savedCtx.declName?, macroStack := savedCtx.macroStack, errToSorry := savedCtx.errToSorry }) <|
withTheReader Core.Context (fun ctx => { ctx with options := savedCtx.options, openDecls := savedCtx.openDecls })
x
private def postponeElabTerm (stx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do
trace[Elab.postpone] "{stx} : {expectedType?}"
let mvar ← mkFreshExprMVar expectedType? MetavarKind.syntheticOpaque
let ctx ← read
registerSyntheticMVar stx mvar.mvarId! (SyntheticMVarKind.postponed (← saveContext))
pure mvar
/-
Helper function for `elabTerm` is tries the registered elaboration functions for `stxNode` kind until it finds one that supports the syntax or
an error is found. -/
private def elabUsingElabFnsAux (s : SavedState) (stx : Syntax) (expectedType? : Option Expr) (catchExPostpone : Bool)
: List TermElab → TermElabM Expr
| [] => do throwError "unexpected syntax{indentD stx}"
| (elabFn::elabFns) => do
try
elabFn stx expectedType?
catch ex => match ex with
| Exception.error _ _ =>
if (← read).errToSorry then
exceptionToSorry ex expectedType?
else
throw ex
| Exception.internal id _ =>
if (← read).errToSorry && id == abortTermExceptionId then
exceptionToSorry ex expectedType?
else if id == unsupportedSyntaxExceptionId then
s.restore
elabUsingElabFnsAux s stx expectedType? catchExPostpone elabFns
else if catchExPostpone && id == postponeExceptionId then
/- If `elab` threw `Exception.postpone`, we reset any state modifications.
For example, we want to make sure pending synthetic metavariables created by `elab` before
it threw `Exception.postpone` are discarded.
Note that we are also discarding the messages created by `elab`.
For example, consider the expression.
`((f.x a1).x a2).x a3`
Now, suppose the elaboration of `f.x a1` produces an `Exception.postpone`.
Then, a new metavariable `?m` is created. Then, `?m.x a2` also throws `Exception.postpone`
because the type of `?m` is not yet known. Then another, metavariable `?n` is created, and
finally `?n.x a3` also throws `Exception.postpone`. If we did not restore the state, we would
keep "dead" metavariables `?m` and `?n` on the pending synthetic metavariable list. This is
wasteful because when we resume the elaboration of `((f.x a1).x a2).x a3`, we start it from scratch
and new metavariables are created for the nested functions. -/
s.restore
postponeElabTerm stx expectedType?
else
throw ex
private def elabUsingElabFns (stx : Syntax) (expectedType? : Option Expr) (catchExPostpone : Bool) : TermElabM Expr := do
let s ← saveState
let table := termElabAttribute.ext.getState (← getEnv) |>.table
let k := stx.getKind
match table.find? k with
| some elabFns => elabUsingElabFnsAux s stx expectedType? catchExPostpone elabFns
| none => throwError "elaboration function for '{k}' has not been implemented{indentD stx}"
instance : MonadMacroAdapter TermElabM where
getCurrMacroScope := getCurrMacroScope
getNextMacroScope := return (← getThe Core.State).nextMacroScope
setNextMacroScope next := modifyThe Core.State fun s => { s with nextMacroScope := next }
private def isExplicit (stx : Syntax) : Bool :=
match stx with
| `(@$f) => true
| _ => false
private def isExplicitApp (stx : Syntax) : Bool :=
stx.getKind == ``Lean.Parser.Term.app && isExplicit stx[0]
/--
Return true if `stx` if a lambda abstraction containing a `{}` or `[]` binder annotation.
Example: `fun {α} (a : α) => a` -/
private def isLambdaWithImplicit (stx : Syntax) : Bool :=
match stx with
| `(fun $binders* => $body) => binders.any fun b => b.isOfKind ``Lean.Parser.Term.implicitBinder || b.isOfKind `Lean.Parser.Term.instBinder
| _ => false
private partial def dropTermParens : Syntax → Syntax := fun stx =>
match stx with
| `(($stx)) => dropTermParens stx
| _ => stx
private def isHole (stx : Syntax) : Bool :=
match stx with
| `(_) => true
| `(? _) => true
| `(? $x:ident) => true
| _ => false
private def isTacticBlock (stx : Syntax) : Bool :=
match stx with
| `(by $x:tacticSeq) => true
| _ => false
private def isNoImplicitLambda (stx : Syntax) : Bool :=
match stx with
| `(noImplicitLambda% $x:term) => true
| _ => false
def mkNoImplicitLambdaAnnotation (type : Expr) : Expr :=
mkAnnotation `noImplicitLambda type
def hasNoImplicitLambdaAnnotation (type : Expr) : Bool :=
annotation? `noImplicitLambda type |>.isSome
/-- Block usage of implicit lambdas if `stx` is `@f` or `@f arg1 ...` or `fun` with an implicit binder annotation. -/
def blockImplicitLambda (stx : Syntax) : Bool :=
let stx := dropTermParens stx
-- TODO: make it extensible
isExplicit stx || isExplicitApp stx || isLambdaWithImplicit stx || isHole stx || isTacticBlock stx || isNoImplicitLambda stx
/--
Return normalized expected type if it is of the form `{a : α} → β` or `[a : α] → β` and
`blockImplicitLambda stx` is not true, else return `none`. -/
private def useImplicitLambda? (stx : Syntax) (expectedType? : Option Expr) : TermElabM (Option Expr) :=
if blockImplicitLambda stx then
pure none
else match expectedType? with
| some expectedType => do
if hasNoImplicitLambdaAnnotation expectedType then
pure none
else
let expectedType ← whnfForall expectedType
match expectedType with
| Expr.forallE _ _ _ c => if c.binderInfo.isExplicit then pure none else pure $ some expectedType
| _ => pure none
| _ => pure none
private def elabImplicitLambdaAux (stx : Syntax) (catchExPostpone : Bool) (expectedType : Expr) (fvars : Array Expr) : TermElabM Expr := do
let body ← elabUsingElabFns stx expectedType catchExPostpone
let body ← ensureHasType expectedType body
let r ← mkLambdaFVars fvars body
trace[Elab.implicitForall] r
pure r
private partial def elabImplicitLambda (stx : Syntax) (catchExPostpone : Bool) : Expr → Array Expr → TermElabM Expr
| type@(Expr.forallE n d b c), fvars =>
if c.binderInfo.isExplicit then
elabImplicitLambdaAux stx catchExPostpone type fvars
else withFreshMacroScope do
let n ← MonadQuotation.addMacroScope n
withLocalDecl n c.binderInfo d fun fvar => do
let type ← whnfForall (b.instantiate1 fvar)
elabImplicitLambda stx catchExPostpone type (fvars.push fvar)
| type, fvars =>
elabImplicitLambdaAux stx catchExPostpone type fvars
/- Main loop for `elabTerm` -/
private partial def elabTermAux (expectedType? : Option Expr) (catchExPostpone : Bool) (implicitLambda : Bool) : Syntax → TermElabM Expr
| Syntax.missing => mkSyntheticSorryFor expectedType?
| stx => withFreshMacroScope <| withIncRecDepth do
trace[Elab.step] "expected type: {expectedType?}, term\n{stx}"
checkMaxHeartbeats "elaborator"
withNestedTraces do
let env ← getEnv
let stxNew? ← catchInternalId unsupportedSyntaxExceptionId
(do let newStx ← adaptMacro (getMacros env) stx; pure (some newStx))
(fun _ => pure none)
match stxNew? with
| some stxNew => withMacroExpansion stx stxNew $ elabTermAux expectedType? catchExPostpone implicitLambda stxNew
| _ =>
let implicit? ← if implicitLambda && (← read).implicitLambda then useImplicitLambda? stx expectedType? else pure none
match implicit? with
| some expectedType => elabImplicitLambda stx catchExPostpone expectedType #[]
| none => elabUsingElabFns stx expectedType? catchExPostpone
def addTermInfo (stx : Syntax) (e : Expr) : TermElabM Unit := do
if (← getInfoState).enabled then
pushInfoLeaf <| Info.ofTermInfo { lctx := (← getLCtx), expr := e, stx := stx }
def getSyntheticMVarDecl? (mvarId : MVarId) : TermElabM (Option SyntheticMVarDecl) :=
return (← get).syntheticMVars.find? fun d => d.mvarId == mvarId
def mkTermInfo (stx : Syntax) (e : Expr) : TermElabM (Sum Info MVarId) := do
let isHole? : TermElabM (Option MVarId) := do
match e with
| Expr.mvar mvarId _ =>
match (← getSyntheticMVarDecl? mvarId) with
| some { kind := SyntheticMVarKind.tactic .., .. } => return mvarId
| some { kind := SyntheticMVarKind.postponed .., .. } => return mvarId
| _ => return none
| _ => pure none
match (← isHole?) with
| none => return Sum.inl <| Info.ofTermInfo { lctx := (← getLCtx), expr := e, stx := stx }
| some mvarId => return Sum.inr mvarId
/-- Store in the `InfoTree` that `e` is a "dot"-completion target. -/
def addDotCompletionInfo (stx : Syntax) (e : Expr) (expectedType? : Option Expr) (field? : Option Syntax := none) : TermElabM Unit := do
addCompletionInfo <| CompletionInfo.dot { expr := e, stx := stx, lctx := (← getLCtx) } (field? := field?) (expectedType? := expectedType?)
/--
Main function for elaborating terms.
It extracts the elaboration methods from the environment using the node kind.
Recall that the environment has a mapping from `SyntaxNodeKind` to `TermElab` methods.
It creates a fresh macro scope for executing the elaboration method.
All unlogged trace messages produced by the elaboration method are logged using
the position information at `stx`. If the elaboration method throws an `Exception.error` and `errToSorry == true`,
the error is logged and a synthetic sorry expression is returned.
If the elaboration throws `Exception.postpone` and `catchExPostpone == true`,
a new synthetic metavariable of kind `SyntheticMVarKind.postponed` is created, registered,
and returned.
The option `catchExPostpone == false` is used to implement `resumeElabTerm`
to prevent the creation of another synthetic metavariable when resuming the elaboration.
If `implicitLambda == true`, then disable implicit lambdas feature for the given syntax, but not for its subterms.
We use this flag to implement, for example, the `@` modifier. If `Context.implicitLambda == false`, then this parameter has no effect.
-/
def elabTerm (stx : Syntax) (expectedType? : Option Expr) (catchExPostpone := true) (implicitLambda := true) : TermElabM Expr :=
withInfoContext' (withRef stx <| elabTermAux expectedType? catchExPostpone implicitLambda stx) (mkTermInfo stx)
def elabTermEnsuringType (stx : Syntax) (expectedType? : Option Expr) (catchExPostpone := true) (implicitLambda := true) (errorMsgHeader? : Option String := none) : TermElabM Expr := do
let e ← elabTerm stx expectedType? catchExPostpone implicitLambda
withRef stx <| ensureHasType expectedType? e errorMsgHeader?
/-- Adapt a syntax transformation to a regular, term-producing elaborator. -/
def adaptExpander (exp : Syntax → TermElabM Syntax) : TermElab := fun stx expectedType? => do
let stx' ← exp stx
withMacroExpansion stx stx' $ elabTerm stx' expectedType?
def mkInstMVar (type : Expr) : TermElabM Expr := do
let mvar ← mkFreshExprMVar type MetavarKind.synthetic
let mvarId := mvar.mvarId!
unless (← synthesizeInstMVarCore mvarId) do
registerSyntheticMVarWithCurrRef mvarId SyntheticMVarKind.typeClass
pure mvar
/-
Relevant definitions:
```
class CoeSort (α : Sort u) (β : outParam (Sort v))
abbrev coeSort {α : Sort u} {β : Sort v} (a : α) [CoeSort α β] : β
```
-/
private def tryCoeSort (α : Expr) (a : Expr) : TermElabM Expr := do
let β ← mkFreshTypeMVar
let u ← getLevel α
let v ← getLevel β
let coeSortInstType := mkAppN (Lean.mkConst ``CoeSort [u, v]) #[α, β]
let mvar ← mkFreshExprMVar coeSortInstType MetavarKind.synthetic
let mvarId := mvar.mvarId!
try
withoutMacroStackAtErr do
if (← synthesizeCoeInstMVarCore mvarId) then
expandCoe <| mkAppN (Lean.mkConst ``coeSort [u, v]) #[α, β, a, mvar]
else
throwError "type expected"
catch
| Exception.error _ msg => throwError "type expected\n{msg}"
| _ => throwError "type expected"
/--
Make sure `e` is a type by inferring its type and making sure it is a `Expr.sort`
or is unifiable with `Expr.sort`, or can be coerced into one. -/
def ensureType (e : Expr) : TermElabM Expr := do
if (← isType e) then
pure e
else
let eType ← inferType e
let u ← mkFreshLevelMVar
if (← isDefEq eType (mkSort u)) then
pure e
else
tryCoeSort eType e
/-- Elaborate `stx` and ensure result is a type. -/
def elabType (stx : Syntax) : TermElabM Expr := do
let u ← mkFreshLevelMVar
let type ← elabTerm stx (mkSort u)
withRef stx $ ensureType type
/--
Enable auto-bound implicits, and execute `k` while catching auto bound implicit exceptions. When an exception is caught,
a new local declaration is created, registered, and `k` is tried to be executed again. -/
partial def withAutoBoundImplicit (k : TermElabM α) : TermElabM α := do
let flag := autoBoundImplicitLocal.get (← getOptions)
if flag then
withReader (fun ctx => { ctx with autoBoundImplicit := flag, autoBoundImplicits := {} }) do
let rec loop (s : SavedState) : TermElabM α := do
try
k
catch
| ex => match isAutoBoundImplicitLocalException? ex with
| some n =>
-- Restore state, declare `n`, and try again
s.restore
withLocalDecl n BinderInfo.implicit (← mkFreshTypeMVar) fun x =>
withReader (fun ctx => { ctx with autoBoundImplicits := ctx.autoBoundImplicits.push x } ) do
loop (← saveState)
| none => throw ex
loop (← saveState)
else
k
def withoutAutoBoundImplicit (k : TermElabM α) : TermElabM α := do
withReader (fun ctx => { ctx with autoBoundImplicit := false, autoBoundImplicits := {} }) k
/--
Return `autoBoundImplicits ++ xs.
This methoid throws an error if a variable in `autoBoundImplicits` depends on some `x` in `xs` -/
def addAutoBoundImplicits (xs : Array Expr) : TermElabM (Array Expr) := do
let autoBoundImplicits := (← read).autoBoundImplicits
for auto in autoBoundImplicits do
let localDecl ← getLocalDecl auto.fvarId!
for x in xs do
if (← getMCtx).localDeclDependsOn localDecl x.fvarId! then
throwError "invalid auto implicit argument '{auto}', it depends on explicitly provided argument '{x}'"
return autoBoundImplicits.toArray ++ xs
def mkAuxName (suffix : Name) : TermElabM Name := do
match (← read).declName? with
| none => throwError "auxiliary declaration cannot be created when declaration name is not available"
| some declName => Lean.mkAuxName (declName ++ suffix) 1
builtin_initialize registerTraceClass `Elab.letrec
/- Return true if mvarId is an auxiliary metavariable created for compiling `let rec` or it
is delayed assigned to one. -/
def isLetRecAuxMVar (mvarId : MVarId) : TermElabM Bool := do
trace[Elab.letrec] "mvarId: {mkMVar mvarId} letrecMVars: {(← get).letRecsToLift.map (mkMVar $ ·.mvarId)}"
let mvarId := (← getMCtx).getDelayedRoot mvarId
trace[Elab.letrec] "mvarId root: {mkMVar mvarId}"
return (← get).letRecsToLift.any (·.mvarId == mvarId)
/- =======================================
Builtin elaboration functions
======================================= -/
@[builtinTermElab «prop»] def elabProp : TermElab := fun _ _ =>
return mkSort levelZero
private def elabOptLevel (stx : Syntax) : TermElabM Level :=
if stx.isNone then
pure levelZero
else
elabLevel stx[0]
@[builtinTermElab «sort»] def elabSort : TermElab := fun stx _ =>
return mkSort (← elabOptLevel stx[1])
@[builtinTermElab «type»] def elabTypeStx : TermElab := fun stx _ =>
return mkSort (mkLevelSucc (← elabOptLevel stx[1]))
/-
the method `resolveName` adds a completion point for it using the given
expected type. Thus, we propagate the expected type if `stx[0]` is an identifier.
It doesn't "hurt" if the identifier can be resolved because the expected type is not used in this case.
Recall that if the name resolution fails a synthetic sorry is returned.-/
@[builtinTermElab «pipeCompletion»] def elabPipeCompletion : TermElab := fun stx expectedType? => do
let e ← elabTerm stx[0] none
unless e.isSorry do
addDotCompletionInfo stx e expectedType?
throwErrorAt stx[1] "invalid field notation, identifier or numeral expected"
@[builtinTermElab «completion»] def elabCompletion : TermElab := fun stx expectedType? => do
/- `ident.` is ambiguous in Lean, we may try to be completing a declaration name or access a "field". -/
if stx[0].isIdent then
/- If we can elaborate the identifier successfully, we assume it a dot-completion. Otherwise, we treat it as
identifier completion with a dangling `.`.
Recall that the server falls back to identifier completion when dot-completion fails. -/
let s ← saveState
try
let e ← elabTerm stx[0] none
addDotCompletionInfo stx e expectedType?
catch _ =>
s.restore
addCompletionInfo <| CompletionInfo.id stx stx[0].getId (danglingDot := true) (← getLCtx) expectedType?
throwErrorAt stx[1] "invalid field notation, identifier or numeral expected"
else
elabPipeCompletion stx expectedType?
@[builtinTermElab «hole»] def elabHole : TermElab := fun stx expectedType? => do
let mvar ← mkFreshExprMVar expectedType?
registerMVarErrorHoleInfo mvar.mvarId! stx
pure mvar
@[builtinTermElab «syntheticHole»] def elabSyntheticHole : TermElab := fun stx expectedType? => do
let arg := stx[1]
let userName := if arg.isIdent then arg.getId else Name.anonymous
let mkNewHole : Unit → TermElabM Expr := fun _ => do
let mvar ← mkFreshExprMVar expectedType? MetavarKind.syntheticOpaque userName
registerMVarErrorHoleInfo mvar.mvarId! stx
pure mvar
if userName.isAnonymous then
mkNewHole ()
else
let mctx ← getMCtx
match mctx.findUserName? userName with
| none => mkNewHole ()
| some mvarId =>
let mvar := mkMVar mvarId
let mvarDecl ← getMVarDecl mvarId
let lctx ← getLCtx
if mvarDecl.lctx.isSubPrefixOf lctx then
pure mvar
else match mctx.getExprAssignment? mvarId with
| some val =>
let val ← instantiateMVars val
if mctx.isWellFormed lctx val then
pure val
else
withLCtx mvarDecl.lctx mvarDecl.localInstances do
throwError "synthetic hole has already been defined and assigned to value incompatible with the current context{indentExpr val}"
| none =>
if mctx.isDelayedAssigned mvarId then
-- We can try to improve this case if needed.
throwError "synthetic hole has already beend defined and delayed assigned with an incompatible local context"
else if lctx.isSubPrefixOf mvarDecl.lctx then
let mvarNew ← mkNewHole ()
modifyMCtx fun mctx => mctx.assignExpr mvarId mvarNew
pure mvarNew
else
throwError "synthetic hole has already been defined with an incompatible local context"
private def mkTacticMVar (type : Expr) (tacticCode : Syntax) : TermElabM Expr := do
let mvar ← mkFreshExprMVar type MetavarKind.syntheticOpaque
let mvarId := mvar.mvarId!
let ref ← getRef
let declName? ← getDeclName?
registerSyntheticMVar ref mvarId <| SyntheticMVarKind.tactic tacticCode (← saveContext)
return mvar
@[builtinTermElab byTactic] def elabByTactic : TermElab := fun stx expectedType? =>
match expectedType? with
| some expectedType => mkTacticMVar expectedType stx
| none => throwError ("invalid 'by' tactic, expected type has not been provided")
@[builtinTermElab noImplicitLambda] def elabNoImplicitLambda : TermElab := fun stx expectedType? =>
elabTerm stx[1] (mkNoImplicitLambdaAnnotation <$> expectedType?)
def resolveLocalName (n : Name) : TermElabM (Option (Expr × List String)) := do
let lctx ← getLCtx
let view := extractMacroScopes n
let rec loop (n : Name) (projs : List String) :=
match lctx.findFromUserName? { view with name := n }.review with
| some decl => some (decl.toExpr, projs)
| none => match n with
| Name.str pre s _ => loop pre (s::projs)
| _ => none
return loop view.name []
/- Return true iff `stx` is a `Syntax.ident`, and it is a local variable. -/
def isLocalIdent? (stx : Syntax) : TermElabM (Option Expr) :=
match stx with
| Syntax.ident _ _ val _ => do
let r? ← resolveLocalName val
match r? with
| some (fvar, []) => pure (some fvar)
| _ => pure none
| _ => pure none
/--
Create an `Expr.const` using the given name and explicit levels.
Remark: fresh universe metavariables are created if the constant has more universe
parameters than `explicitLevels`. -/
def mkConst (constName : Name) (explicitLevels : List Level := []) : TermElabM Expr := do
let cinfo ← getConstInfo constName
if explicitLevels.length > cinfo.levelParams.length then
throwError "too many explicit universe levels"
else
let numMissingLevels := cinfo.levelParams.length - explicitLevels.length
let us ← mkFreshLevelMVars numMissingLevels
pure $ Lean.mkConst constName (explicitLevels ++ us)
private def mkConsts (candidates : List (Name × List String)) (explicitLevels : List Level) : TermElabM (List (Expr × List String)) := do
candidates.foldlM (init := []) fun result (constName, projs) => do
-- TODO: better suppor for `mkConst` failure. We may want to cache the failures, and report them if all candidates fail.
let const ← mkConst constName explicitLevels
return (const, projs) :: result
def resolveName (stx : Syntax) (n : Name) (preresolved : List (Name × List String)) (explicitLevels : List Level) (expectedType? : Option Expr := none) : TermElabM (List (Expr × List String)) := do
try
if let some (e, projs) ← resolveLocalName n then
unless explicitLevels.isEmpty do
throwError "invalid use of explicit universe parameters, '{e}' is a local"
return [(e, projs)]
-- check for section variable capture by a quotation
if let some (e, projs) := preresolved.findSome? fun (n, projs) => (← read).sectionFVars.find? n |>.map (·, projs) then
return [(e, projs)] -- section variables should shadow global decls
if preresolved.isEmpty then
process (← resolveGlobalName n)
else
process preresolved
catch ex =>
if preresolved.isEmpty && explicitLevels.isEmpty then
addCompletionInfo <| CompletionInfo.id stx stx.getId (danglingDot := false) (← getLCtx) expectedType?
throw ex
where process (candidates : List (Name × List String)) : TermElabM (List (Expr × List String)) := do
if candidates.isEmpty then
if (← read).autoBoundImplicit && isValidAutoBoundImplicitName n then
throwAutoBoundImplicitLocal n
else
throwError "unknown identifier '{Lean.mkConst n}'"
if preresolved.isEmpty && explicitLevels.isEmpty then
addCompletionInfo <| CompletionInfo.id stx stx.getId (danglingDot := false) (← getLCtx) expectedType?
mkConsts candidates explicitLevels
/--
Similar to `resolveName`, but creates identifiers for the main part and each projection with position information derived from `ident`.
Example: Assume resolveName `v.head.bla.boo` produces `(v.head, ["bla", "boo"])`, then this method produces
`(v.head, id, [f₁, f₂])` where `id` is an identifier for `v.head`, and `f₁` and `f₂` are identifiers for fields `"bla"` and `"boo"`. -/
def resolveName' (ident : Syntax) (explicitLevels : List Level) (expectedType? : Option Expr := none) : TermElabM (List (Expr × Syntax × List Syntax)) := do
match ident with
| Syntax.ident info rawStr n preresolved =>
let r ← resolveName ident n preresolved explicitLevels expectedType?
r.mapM fun (c, fields) => do
let (cSstr, fields) := fields.foldr (init := (rawStr, [])) fun field (restSstr, fs) =>
let fieldSstr := restSstr.takeRightWhile (· ≠ '.')
({ restSstr with stopPos := restSstr.stopPos - (fieldSstr.bsize + 1) }, (field, fieldSstr) :: fs)
let mkIdentFromPos pos rawVal val :=
let info := match info with
| SourceInfo.original .. => SourceInfo.original "".toSubstring pos "".toSubstring
| _ => SourceInfo.synthetic pos (pos + rawVal.bsize)
Syntax.ident info rawVal val []
let id := match c with
| Expr.const id _ _ => id
| Expr.fvar id _ => id
| _ => unreachable!
let id := mkIdentFromPos (ident.getPos?.getD 0) cSstr id
match info.getPos? with
| none =>
return (c, id, fields.map fun (field, _) => mkIdentFrom ident (Name.mkSimple field))
| some pos =>
let mut pos := pos + cSstr.bsize + 1
let mut newFields := #[]
for (field, fieldSstr) in fields do
newFields := newFields.push <| mkIdentFromPos pos fieldSstr (Name.mkSimple field)
pos := pos + fieldSstr.bsize + 1
return (c, id, newFields.toList)
| _ => throwError "identifier expected"
def resolveId? (stx : Syntax) (kind := "term") : TermElabM (Option Expr) :=
match stx with
| Syntax.ident _ _ val preresolved => do
let rs ← try resolveName stx val preresolved [] catch _ => pure []
let rs := rs.filter fun ⟨f, projs⟩ => projs.isEmpty
let fs := rs.map fun (f, _) => f
match fs with
| [] => pure none
| [f] => pure (some f)
| _ => throwError "ambiguous {kind}, use fully qualified name, possible interpretations {fs}"
| _ => throwError "identifier expected"
@[builtinTermElab cdot] def elabBadCDot : TermElab := fun stx _ =>
throwError "invalid occurrence of `·` notation, it must be surrounded by parentheses (e.g. `(· + 1)`)"
@[builtinTermElab strLit] def elabStrLit : TermElab := fun stx _ => do
match stx.isStrLit? with
| some val => pure $ mkStrLit val
| none => throwIllFormedSyntax
private def mkFreshTypeMVarFor (expectedType? : Option Expr) : TermElabM Expr := do
let typeMVar ← mkFreshTypeMVar MetavarKind.synthetic
match expectedType? with
| some expectedType => discard <| isDefEq expectedType typeMVar
| _ => pure ()
return typeMVar
@[builtinTermElab numLit] def elabNumLit : TermElab := fun stx expectedType? => do
let val ← match stx.isNatLit? with
| some val => pure val
| none => throwIllFormedSyntax
let typeMVar ← mkFreshTypeMVarFor expectedType?
let u ← getDecLevel typeMVar
let mvar ← mkInstMVar (mkApp2 (Lean.mkConst ``OfNat [u]) typeMVar (mkNatLit val))
let r := mkApp3 (Lean.mkConst ``OfNat.ofNat [u]) typeMVar (mkNatLit val) mvar
registerMVarErrorImplicitArgInfo mvar.mvarId! stx r
return r
@[builtinTermElab rawNatLit] def elabRawNatLit : TermElab := fun stx expectedType? => do
match stx[1].isNatLit? with
| some val => return mkNatLit val
| none => throwIllFormedSyntax
@[builtinTermElab scientificLit]
def elabScientificLit : TermElab := fun stx expectedType? => do
match stx.isScientificLit? with
| none => throwIllFormedSyntax
| some (m, sign, e) =>
let typeMVar ← mkFreshTypeMVarFor expectedType?
let u ← getDecLevel typeMVar
let mvar ← mkInstMVar (mkApp (Lean.mkConst ``OfScientific [u]) typeMVar)
return mkApp5 (Lean.mkConst ``OfScientific.ofScientific [u]) typeMVar mvar (mkNatLit m) (toExpr sign) (mkNatLit e)
@[builtinTermElab charLit] def elabCharLit : TermElab := fun stx _ => do
match stx.isCharLit? with
| some val => return mkApp (Lean.mkConst ``Char.ofNat) (mkNatLit val.toNat)
| none => throwIllFormedSyntax
@[builtinTermElab quotedName] def elabQuotedName : TermElab := fun stx _ =>
match stx[0].isNameLit? with
| some val => pure $ toExpr val
| none => throwIllFormedSyntax
@[builtinTermElab doubleQuotedName] def elabDoubleQuotedName : TermElab := fun stx _ => do
match stx[1].isNameLit? with
| some val => toExpr (← resolveGlobalConstNoOverloadWithInfo stx[1] val)
| none => throwIllFormedSyntax
@[builtinTermElab typeOf] def elabTypeOf : TermElab := fun stx _ => do
inferType (← elabTerm stx[1] none)
@[builtinTermElab ensureTypeOf] def elabEnsureTypeOf : TermElab := fun stx expectedType? =>
match stx[2].isStrLit? with
| none => throwIllFormedSyntax
| some msg => do
let refTerm ← elabTerm stx[1] none
let refTermType ← inferType refTerm
elabTermEnsuringType stx[3] refTermType (errorMsgHeader? := msg)
@[builtinTermElab ensureExpectedType] def elabEnsureExpectedType : TermElab := fun stx expectedType? =>
match stx[1].isStrLit? with
| none => throwIllFormedSyntax
| some msg => elabTermEnsuringType stx[2] expectedType? (errorMsgHeader? := msg)
@[builtinTermElab «open»] def elabOpen : TermElab := fun stx expectedType? => do
try
pushScope
let openDecls ← elabOpenDecl stx[1]
withTheReader Core.Context (fun ctx => { ctx with openDecls := openDecls }) do
elabTerm stx[3] expectedType?
finally
popScope
@[builtinTermElab «set_option»] def elabSetOption : TermElab := fun stx expectedType? => do
let options ← Elab.elabSetOption stx[1] stx[2]
withTheReader Core.Context (fun ctx => { ctx with maxRecDepth := maxRecDepth.get options, options := options }) do
elabTerm stx[4] expectedType?
private def mkSomeContext : Context := {
fileName := "<TermElabM>"
fileMap := arbitrary
}
@[inline] def TermElabM.run (x : TermElabM α) (ctx : Context := mkSomeContext) (s : State := {}) : MetaM (α × State) :=
withConfig setElabConfig (x ctx |>.run s)
@[inline] def TermElabM.run' (x : TermElabM α) (ctx : Context := mkSomeContext) (s : State := {}) : MetaM α :=
(·.1) <$> x.run ctx s
@[inline] def TermElabM.toIO (x : TermElabM α)
(ctxCore : Core.Context) (sCore : Core.State)
(ctxMeta : Meta.Context) (sMeta : Meta.State)
(ctx : Context) (s : State) : IO (α × Core.State × Meta.State × State) := do
let ((a, s), sCore, sMeta) ← (x.run ctx s).toIO ctxCore sCore ctxMeta sMeta
pure (a, sCore, sMeta, s)
instance [MetaEval α] : MetaEval (TermElabM α) where
eval env opts x _ :=
let x : TermElabM α := do
try x finally
let s ← get
s.messages.forM fun msg => do IO.println (← msg.toString)
MetaEval.eval env opts (hideUnit := true) $ x.run' mkSomeContext
unsafe def evalExpr (α) (typeName : Name) (value : Expr) : TermElabM α :=
withoutModifyingEnv do
let name ← mkFreshUserName `_tmp
let type ← inferType value
let type ← whnfD type
unless type.isConstOf typeName do
throwError "unexpected type at evalExpr{indentExpr type}"
let decl := Declaration.defnDecl {
name := name, levelParams := [], type := type,
value := value, hints := ReducibilityHints.opaque,
safety := DefinitionSafety.unsafe
}
ensureNoUnassignedMVars decl
addAndCompile decl
evalConst α name
private def throwStuckAtUniverseCnstr : TermElabM Unit := do
let entries ← getPostponed
let mut found : Std.HashSet (Level × Level) := {}
let mut uniqueEntries := #[]
for entry in entries do
let mut lhs := entry.lhs
let mut rhs := entry.rhs
if Level.normLt rhs lhs then
(lhs, rhs) := (rhs, lhs)
unless found.contains (lhs, rhs) do
found := found.insert (lhs, rhs)
uniqueEntries := uniqueEntries.push entry
for i in [1:uniqueEntries.size] do
logErrorAt uniqueEntries[i].ref (← mkMessage uniqueEntries[i])
throwErrorAt uniqueEntries[0].ref (← mkMessage uniqueEntries[0])
where
/- Annotate any constant and sort in `e` that satisfies `p` with `pp.universes true` -/
exposeRelevantUniverses (e : Expr) (p : Level → Bool) : Expr :=
e.replace fun e =>
match e with
| Expr.const _ us _ => if us.any p then some (e.setPPUniverses true) else none
| Expr.sort u _ => if p u then some (e.setPPUniverses true) else none
| _ => none
mkMessage (entry : PostponedEntry) : TermElabM MessageData := do
match entry.ctx? with
| none =>
return m!"stuck at solving universe constraints{indentD m!"{entry.lhs} =?= {entry.rhs}"}"
| some ctx =>
withLCtx ctx.lctx ctx.localInstances do
let s := entry.lhs.collectMVars entry.rhs.collectMVars
/- `p u` is true if it contains a universe metavariable in `s` -/
let p (u : Level) := u.any fun | Level.mvar m _ => s.contains m | _ => false
let lhs := exposeRelevantUniverses (← instantiateMVars ctx.lhs) p
let rhs := exposeRelevantUniverses (← instantiateMVars ctx.rhs) p
addMessageContext m!"stuck at solving universe constraints{indentD m!"{entry.lhs} =?= {entry.rhs}"}\nwhile trying to unify{indentExpr lhs}\nwith{indentExpr rhs}"
@[specialize] def withoutPostponingUniverseConstraints (x : TermElabM α) : TermElabM α := do
let postponed ← getResetPostponed
try
let a ← x
unless (← processPostponed (mayPostpone := false)) do
throwStuckAtUniverseCnstr
setPostponed postponed
return a
catch ex =>
setPostponed postponed
throw ex
end Term
builtin_initialize
registerTraceClass `Elab.postpone
registerTraceClass `Elab.coe
registerTraceClass `Elab.debug
export Term (TermElabM)
end Lean.Elab
|
24c1a12c438e8decd6aa51944bcdc1cce1e4e48d | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/ring_theory/discrete_valuation_ring/tfae.lean | 6beb45ac83a56f28fc4d529607980ccfd56204d2 | [
"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,514 | lean | /-
Copyright (c) 2022 Andrew Yang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andrew Yang
-/
import ring_theory.ideal.cotangent
import ring_theory.dedekind_domain.basic
import ring_theory.valuation.valuation_ring
import ring_theory.nakayama
/-!
# Equivalent conditions for DVR
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In `discrete_valuation_ring.tfae`, we show that the following are equivalent for a
noetherian local domain `(R, m, k)`:
- `R` is a discrete valuation ring
- `R` is a valuation ring
- `R` is a dedekind domain
- `R` is integrally closed with a unique prime ideal
- `m` is principal
- `dimₖ m/m² = 1`
- Every nonzero ideal is a power of `m`.
-/
variables (R : Type*) [comm_ring R] (K : Type*) [field K] [algebra R K] [is_fraction_ring R K]
open_locale discrete_valuation
open local_ring
open_locale big_operators
lemma exists_maximal_ideal_pow_eq_of_principal [is_noetherian_ring R] [local_ring R] [is_domain R]
(h : ¬ is_field R) (h' : (maximal_ideal R).is_principal) (I : ideal R) (hI : I ≠ ⊥) :
∃ n : ℕ, I = (maximal_ideal R) ^ n :=
begin
classical,
unfreezingI { obtain ⟨x, hx : _ = ideal.span _⟩ := h' },
by_cases hI' : I = ⊤, { use 0, rw [pow_zero, hI', ideal.one_eq_top] },
have H : ∀ r : R, ¬ (is_unit r) ↔ x ∣ r :=
λ r, (set_like.ext_iff.mp hx r).trans ideal.mem_span_singleton,
have : x ≠ 0,
{ rintro rfl,
apply ring.ne_bot_of_is_maximal_of_not_is_field (maximal_ideal.is_maximal R) h,
simp [hx] },
have hx' := discrete_valuation_ring.irreducible_of_span_eq_maximal_ideal x this hx,
have H' : ∀ r : R, r ≠ 0 → r ∈ nonunits R → ∃ (n : ℕ), associated (x ^ n) r,
{ intros r hr₁ hr₂,
obtain ⟨f, hf₁, rfl, hf₂⟩ := (wf_dvd_monoid.not_unit_iff_exists_factors_eq r hr₁).mp hr₂,
have : ∀ b ∈ f, associated x b,
{ intros b hb,
exact irreducible.associated_of_dvd hx' (hf₁ b hb) ((H b).mp (hf₁ b hb).1) },
clear hr₁ hr₂ hf₁,
induction f using multiset.induction with fa fs fh,
{ exact (hf₂ rfl).elim },
rcases eq_or_ne fs ∅ with rfl|hf',
{ use 1,
rw [pow_one, multiset.prod_cons, multiset.empty_eq_zero, multiset.prod_zero, mul_one],
exact this _ (multiset.mem_cons_self _ _) },
{ obtain ⟨n, hn⟩ := fh hf' (λ b hb, this _ (multiset.mem_cons_of_mem hb)),
use n + 1,
rw [pow_add, multiset.prod_cons, mul_comm, pow_one],
exact associated.mul_mul (this _ (multiset.mem_cons_self _ _)) hn } },
have : ∃ n : ℕ, x ^ n ∈ I,
{ obtain ⟨r, hr₁, hr₂⟩ : ∃ r : R, r ∈ I ∧ r ≠ 0,
{ by_contra h, push_neg at h, apply hI, rw eq_bot_iff, exact h },
obtain ⟨n, u, rfl⟩ := H' r hr₂ (le_maximal_ideal hI' hr₁),
use n,
rwa [← I.unit_mul_mem_iff_mem u.is_unit, mul_comm] },
use nat.find this,
apply le_antisymm,
{ change ∀ s ∈ I, s ∈ _,
by_contra hI'',
push_neg at hI'',
obtain ⟨s, hs₁, hs₂⟩ := hI'',
apply hs₂,
by_cases hs₃ : s = 0, { rw hs₃, exact zero_mem _ },
obtain ⟨n, u, rfl⟩ := H' s hs₃ (le_maximal_ideal hI' hs₁),
rw [mul_comm, ideal.unit_mul_mem_iff_mem _ u.is_unit] at ⊢ hs₁,
apply ideal.pow_le_pow (nat.find_min' this hs₁),
apply ideal.pow_mem_pow,
exact (H _).mpr (dvd_refl _) },
{ rw [hx, ideal.span_singleton_pow, ideal.span_le, set.singleton_subset_iff],
exact nat.find_spec this }
end
lemma maximal_ideal_is_principal_of_is_dedekind_domain
[local_ring R] [is_domain R] [is_dedekind_domain R] : (maximal_ideal R).is_principal :=
begin
classical,
by_cases ne_bot : maximal_ideal R = ⊥,
{ rw ne_bot, apply_instance },
obtain ⟨a, ha₁, ha₂⟩ : ∃ a ∈ maximal_ideal R, a ≠ (0 : R),
{ by_contra h', push_neg at h', apply ne_bot, rwa eq_bot_iff },
have hle : ideal.span {a} ≤ maximal_ideal R,
{ rwa [ideal.span_le, set.singleton_subset_iff] },
have : (ideal.span {a}).radical = maximal_ideal R,
{ rw ideal.radical_eq_Inf,
apply le_antisymm,
{ exact Inf_le ⟨hle, infer_instance⟩ },
{ refine le_Inf (λ I hI, (eq_maximal_ideal $
is_dedekind_domain.dimension_le_one _ (λ e, ha₂ _) hI.2).ge),
rw [← ideal.span_singleton_eq_bot, eq_bot_iff, ← e], exact hI.1 } },
have : ∃ n, maximal_ideal R ^ n ≤ ideal.span {a},
{ rw ← this, apply ideal.exists_radical_pow_le_of_fg, exact is_noetherian.noetherian _ },
cases hn : nat.find this,
{ have := nat.find_spec this,
rw [hn, pow_zero, ideal.one_eq_top] at this,
exact (ideal.is_maximal.ne_top infer_instance (eq_top_iff.mpr $ this.trans hle)).elim },
obtain ⟨b, hb₁, hb₂⟩ : ∃ b ∈ maximal_ideal R ^ n, ¬ b ∈ ideal.span {a},
{ by_contra h', push_neg at h', rw nat.find_eq_iff at hn,
exact hn.2 n n.lt_succ_self (λ x hx, not_not.mp (h' x hx)) },
have hb₃ : ∀ m ∈ maximal_ideal R, ∃ k : R, k * a = b * m,
{ intros m hm, rw ← ideal.mem_span_singleton', apply nat.find_spec this,
rw [hn, pow_succ'], exact ideal.mul_mem_mul hb₁ hm },
have hb₄ : b ≠ 0,
{ rintro rfl, apply hb₂, exact zero_mem _ },
let K := fraction_ring R,
let x : K := algebra_map R K b / algebra_map R K a,
let M := submodule.map (algebra.of_id R K).to_linear_map (maximal_ideal R),
have ha₃ : algebra_map R K a ≠ 0 := is_fraction_ring.to_map_eq_zero_iff.not.mpr ha₂,
by_cases hx : ∀ y ∈ M, x * y ∈ M,
{ have := is_integral_of_smul_mem_submodule M _ _ x hx,
{ obtain ⟨y, e⟩ := is_integrally_closed.algebra_map_eq_of_integral this,
refine (hb₂ (ideal.mem_span_singleton'.mpr ⟨y, _⟩)).elim,
apply is_fraction_ring.injective R K,
rw [map_mul, e, div_mul_cancel _ ha₃] },
{ rw submodule.ne_bot_iff, refine ⟨_, ⟨a, ha₁, rfl⟩, _⟩,
exact is_fraction_ring.to_map_eq_zero_iff.not.mpr ha₂ },
{ apply submodule.fg.map, exact is_noetherian.noetherian _ } },
{ have : (M.map (distrib_mul_action.to_linear_map R K x)).comap
(algebra.of_id R K).to_linear_map = ⊤,
{ by_contra h, apply hx,
rintros m' ⟨m, hm, (rfl : algebra_map R K m = m')⟩,
obtain ⟨k, hk⟩ := hb₃ m hm,
have hk' : x * algebra_map R K m = algebra_map R K k,
{ rw [← mul_div_right_comm, ← map_mul, ← hk, map_mul, mul_div_cancel _ ha₃] },
exact ⟨k, le_maximal_ideal h ⟨_, ⟨_, hm, rfl⟩, hk'⟩, hk'.symm⟩ },
obtain ⟨y, hy₁, hy₂⟩ : ∃ y ∈ maximal_ideal R, b * y = a,
{ rw [ideal.eq_top_iff_one, submodule.mem_comap] at this,
obtain ⟨_, ⟨y, hy, rfl⟩, hy' : x * algebra_map R K y = algebra_map R K 1⟩ := this,
rw [map_one, ← mul_div_right_comm, div_eq_one_iff_eq ha₃, ← map_mul] at hy',
exact ⟨y, hy, is_fraction_ring.injective R K hy'⟩ },
refine ⟨⟨y, _⟩⟩,
apply le_antisymm,
{ intros m hm, obtain ⟨k, hk⟩ := hb₃ m hm, rw [← hy₂, mul_comm, mul_assoc] at hk,
rw [← mul_left_cancel₀ hb₄ hk, mul_comm], exact ideal.mem_span_singleton'.mpr ⟨_, rfl⟩ },
{ rwa [submodule.span_le, set.singleton_subset_iff] } }
end
lemma discrete_valuation_ring.tfae [is_noetherian_ring R] [local_ring R] [is_domain R]
(h : ¬ is_field R) :
tfae [discrete_valuation_ring R,
valuation_ring R,
is_dedekind_domain R,
is_integrally_closed R ∧ ∃! P : ideal R, P ≠ ⊥ ∧ P.is_prime,
(maximal_ideal R).is_principal,
finite_dimensional.finrank (residue_field R) (cotangent_space R) = 1,
∀ I ≠ ⊥, ∃ n : ℕ, I = (maximal_ideal R) ^ n] :=
begin
have ne_bot := ring.ne_bot_of_is_maximal_of_not_is_field (maximal_ideal.is_maximal R) h,
classical,
rw finrank_eq_one_iff',
tfae_have : 1 → 2,
{ introI _, apply_instance },
tfae_have : 2 → 1,
{ introI _,
haveI := is_bezout.to_gcd_domain R,
haveI : unique_factorization_monoid R := ufm_of_gcd_of_wf_dvd_monoid,
apply discrete_valuation_ring.of_ufd_of_unique_irreducible,
{ obtain ⟨x, hx₁, hx₂⟩ := ring.exists_not_is_unit_of_not_is_field h,
obtain ⟨p, hp₁, hp₂⟩ := wf_dvd_monoid.exists_irreducible_factor hx₂ hx₁,
exact ⟨p, hp₁⟩ },
{ exact valuation_ring.unique_irreducible } },
tfae_have : 1 → 4,
{ introI H,
exact ⟨infer_instance, ((discrete_valuation_ring.iff_pid_with_one_nonzero_prime R).mp H).2⟩ },
tfae_have : 4 → 3,
{ rintros ⟨h₁, h₂⟩, exact ⟨infer_instance, λ I hI hI', unique_of_exists_unique h₂
⟨ne_bot, infer_instance⟩ ⟨hI, hI'⟩ ▸ maximal_ideal.is_maximal R, h₁⟩ },
tfae_have : 3 → 5,
{ introI h, exact maximal_ideal_is_principal_of_is_dedekind_domain R },
tfae_have : 5 → 6,
{ rintro ⟨x, hx⟩,
have : x ∈ maximal_ideal R := by { rw hx, exact submodule.subset_span (set.mem_singleton x) },
let x' : maximal_ideal R := ⟨x, this⟩,
use submodule.quotient.mk x',
split,
{ intro e,
rw submodule.quotient.mk_eq_zero at e,
apply ring.ne_bot_of_is_maximal_of_not_is_field (maximal_ideal.is_maximal R) h,
apply submodule.eq_bot_of_le_smul_of_le_jacobson_bot (maximal_ideal R),
{ exact ⟨{x}, (finset.coe_singleton x).symm ▸ hx.symm⟩ },
{ conv_lhs { rw hx },
rw submodule.mem_smul_top_iff at e,
rwa [submodule.span_le, set.singleton_subset_iff] },
{ rw local_ring.jacobson_eq_maximal_ideal (⊥ : ideal R) bot_ne_top, exact le_refl _ } },
{ refine λ w, quotient.induction_on' w $ λ y, _,
obtain ⟨y, hy⟩ := y,
rw [hx, submodule.mem_span_singleton] at hy,
obtain ⟨a, rfl⟩ := hy,
exact ⟨ideal.quotient.mk _ a, rfl⟩ } },
tfae_have : 6 → 5,
{ rintro ⟨x, hx, hx'⟩,
induction x using quotient.induction_on',
use x,
apply le_antisymm,
swap, { rw [submodule.span_le, set.singleton_subset_iff], exact x.prop },
have h₁ : (ideal.span {x} : ideal R) ⊔ maximal_ideal R ≤
ideal.span {x} ⊔ (maximal_ideal R) • (maximal_ideal R),
{ refine sup_le le_sup_left _,
rintros m hm,
obtain ⟨c, hc⟩ := hx' (submodule.quotient.mk ⟨m, hm⟩),
induction c using quotient.induction_on',
rw ← sub_sub_cancel (c * x) m,
apply sub_mem _ _,
{ apply_instance },
{ refine ideal.mem_sup_left (ideal.mem_span_singleton'.mpr ⟨c, rfl⟩) },
{ have := (submodule.quotient.eq _).mp hc,
rw [submodule.mem_smul_top_iff] at this,
exact ideal.mem_sup_right this } },
have h₂ : maximal_ideal R ≤ (⊥ : ideal R).jacobson,
{ rw local_ring.jacobson_eq_maximal_ideal, exacts [le_refl _, bot_ne_top] },
have := submodule.smul_sup_eq_smul_sup_of_le_smul_of_le_jacobson
(is_noetherian.noetherian _) h₂ h₁,
rw [submodule.bot_smul, sup_bot_eq] at this,
rw [← sup_eq_left, eq_comm],
exact le_sup_left.antisymm (h₁.trans $ le_of_eq this) },
tfae_have : 5 → 7,
{ exact exists_maximal_ideal_pow_eq_of_principal R h },
tfae_have : 7 → 2,
{ rw valuation_ring.iff_ideal_total,
intro H,
constructor,
intros I J,
by_cases hI : I = ⊥, { subst hI, left, exact bot_le },
by_cases hJ : J = ⊥, { subst hJ, right, exact bot_le },
obtain ⟨n, rfl⟩ := H I hI,
obtain ⟨m, rfl⟩ := H J hJ,
cases le_total m n with h' h',
{ left, exact ideal.pow_le_pow h' },
{ right, exact ideal.pow_le_pow h' } },
tfae_finish,
end
|
935c25b7707333d1b47c2df9de4a578064a9fc81 | 432d948a4d3d242fdfb44b81c9e1b1baacd58617 | /src/category_theory/arrow.lean | d2aa9207da7fc4fbfac877ac627d1ad3c367ba84 | [
"Apache-2.0"
] | permissive | JLimperg/aesop3 | 306cc6570c556568897ed2e508c8869667252e8a | a4a116f650cc7403428e72bd2e2c4cda300fe03f | refs/heads/master | 1,682,884,916,368 | 1,620,320,033,000 | 1,620,320,033,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,161 | lean | /-
Copyright (c) 2020 Markus Himmel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Markus Himmel
-/
import category_theory.comma
/-!
# The category of arrows
The category of arrows, with morphisms commutative squares.
We set this up as a specialization of the comma category `comma L R`,
where `L` and `R` are both the identity functor.
We also define the typeclass `has_lift`, representing a choice of a lift
of a commutative square (that is, a diagonal morphism making the two triangles commute).
## Tags
comma, arrow
-/
namespace category_theory
universes v u -- morphism levels before object levels. See note [category_theory universes].
variables {T : Type u} [category.{v} T]
section
variables (T)
/-- The arrow category of `T` has as objects all morphisms in `T` and as morphisms commutative
squares in `T`. -/
@[derive category]
def arrow := comma.{v v v} (𝟭 T) (𝟭 T)
-- Satisfying the inhabited linter
instance arrow.inhabited [inhabited T] : inhabited (arrow T) :=
{ default := show comma (𝟭 T) (𝟭 T), from default (comma (𝟭 T) (𝟭 T)) }
end
namespace arrow
@[simp] lemma id_left (f : arrow T) : comma_morphism.left (𝟙 f) = 𝟙 (f.left) := rfl
@[simp] lemma id_right (f : arrow T) : comma_morphism.right (𝟙 f) = 𝟙 (f.right) := rfl
/-- An object in the arrow category is simply a morphism in `T`. -/
@[simps]
def mk {X Y : T} (f : X ⟶ Y) : arrow T :=
{ left := X,
right := Y,
hom := f }
instance {X Y : T} : has_coe (X ⟶ Y) (arrow T) := ⟨mk⟩
/-- A morphism in the arrow category is a commutative square connecting two objects of the arrow
category. -/
@[simps]
def hom_mk {f g : arrow T} {u : f.left ⟶ g.left} {v : f.right ⟶ g.right}
(w : u ≫ g.hom = f.hom ≫ v) : f ⟶ g :=
{ left := u,
right := v,
w' := w }
/-- We can also build a morphism in the arrow category out of any commutative square in `T`. -/
@[simps]
def hom_mk' {X Y : T} {f : X ⟶ Y} {P Q : T} {g : P ⟶ Q} {u : X ⟶ P} {v : Y ⟶ Q}
(w : u ≫ g = f ≫ v) : arrow.mk f ⟶ arrow.mk g :=
{ left := u,
right := v,
w' := w }
@[simp, reassoc] lemma w {f g : arrow T} (sq : f ⟶ g) : sq.left ≫ g.hom = f.hom ≫ sq.right := sq.w
-- `w_mk_left` is not needed, as it is a consequence of `w` and `mk_hom`.
@[simp, reassoc] lemma w_mk_right {f : arrow T} {X Y : T} {g : X ⟶ Y} (sq : f ⟶ mk g) :
sq.left ≫ g = f.hom ≫ sq.right :=
sq.w
/-- Given a square from an arrow `i` to an isomorphism `p`, express the source part of `sq`
in terms of the inverse of `p`. -/
@[simp] lemma square_to_iso_invert (i : arrow T) {X Y : T} (p : X ≅ Y) (sq : i ⟶ arrow.mk p.hom) :
i.hom ≫ sq.right ≫ p.inv = sq.left :=
by simpa only [category.assoc] using (iso.comp_inv_eq p).mpr ((arrow.w_mk_right sq).symm)
/-- Given a square from an isomorphism `i` to an arrow `p`, express the target part of `sq`
in terms of the inverse of `i`. -/
lemma square_from_iso_invert {X Y : T} (i : X ≅ Y) (p : arrow T) (sq : arrow.mk i.hom ⟶ p) :
i.inv ≫ sq.left ≫ p.hom = sq.right :=
by simp only [iso.inv_hom_id_assoc, arrow.w, arrow.mk_hom]
/-- A lift of a commutative square is a diagonal morphism making the two triangles commute. -/
@[ext] structure lift_struct {f g : arrow T} (sq : f ⟶ g) :=
(lift : f.right ⟶ g.left)
(fac_left' : f.hom ≫ lift = sq.left . obviously)
(fac_right' : lift ≫ g.hom = sq.right . obviously)
restate_axiom lift_struct.fac_left'
restate_axiom lift_struct.fac_right'
instance lift_struct_inhabited {X : T} : inhabited (lift_struct (𝟙 (arrow.mk (𝟙 X)))) :=
⟨⟨𝟙 _, category.id_comp _, category.comp_id _⟩⟩
/-- `has_lift sq` says that there is some `lift_struct sq`, i.e., that it is possible to find a
diagonal morphism making the two triangles commute. -/
class has_lift {f g : arrow T} (sq : f ⟶ g) : Prop :=
mk' :: (exists_lift : nonempty (lift_struct sq))
lemma has_lift.mk {f g : arrow T} {sq : f ⟶ g} (s : lift_struct sq) : has_lift sq :=
⟨nonempty.intro s⟩
attribute [simp, reassoc] lift_struct.fac_left lift_struct.fac_right
/-- Given `has_lift sq`, obtain a lift. -/
noncomputable def has_lift.struct {f g : arrow T} (sq : f ⟶ g) [has_lift sq] : lift_struct sq :=
classical.choice has_lift.exists_lift
/-- If there is a lift of a commutative square `sq`, we can access it by saying `lift sq`. -/
noncomputable abbreviation lift {f g : arrow T} (sq : f ⟶ g) [has_lift sq] : f.right ⟶ g.left :=
(has_lift.struct sq).lift
lemma lift.fac_left {f g : arrow T} (sq : f ⟶ g) [has_lift sq] : f.hom ≫ lift sq = sq.left :=
by simp
lemma lift.fac_right {f g : arrow T} (sq : f ⟶ g) [has_lift sq] : lift sq ≫ g.hom = sq.right :=
by simp
@[simp, reassoc]
lemma lift.fac_right_of_to_mk {X Y : T} {f : arrow T} {g : X ⟶ Y} (sq : f ⟶ mk g) [has_lift sq] :
lift sq ≫ g = sq.right :=
by simp only [←mk_hom g, lift.fac_right]
@[simp, reassoc]
lemma lift.fac_left_of_from_mk {X Y : T} {f : X ⟶ Y} {g : arrow T} (sq : mk f ⟶ g) [has_lift sq] :
f ≫ lift sq = sq.left :=
by simp only [←mk_hom f, lift.fac_left]
@[simp, reassoc]
lemma lift_mk'_left {X Y P Q : T} {f : X ⟶ Y} {g : P ⟶ Q} {u : X ⟶ P} {v : Y ⟶ Q}
(h : u ≫ g = f ≫ v) [has_lift $ arrow.hom_mk' h] : f ≫ lift (arrow.hom_mk' h) = u :=
by simp only [←arrow.mk_hom f, lift.fac_left, arrow.hom_mk'_left]
@[simp, reassoc]
lemma lift_mk'_right {X Y P Q : T} {f : X ⟶ Y} {g : P ⟶ Q} {u : X ⟶ P} {v : Y ⟶ Q}
(h : u ≫ g = f ≫ v) [has_lift $ arrow.hom_mk' h] : lift (arrow.hom_mk' h) ≫ g = v :=
by simp only [←arrow.mk_hom g, lift.fac_right, arrow.hom_mk'_right]
section
instance subsingleton_lift_struct_of_epi {f g : arrow T} (sq : f ⟶ g) [epi f.hom] :
subsingleton (lift_struct sq) :=
subsingleton.intro $ λ a b, lift_struct.ext a b $ (cancel_epi f.hom).1 $ by simp
instance subsingleton_lift_struct_of_mono {f g : arrow T} (sq : f ⟶ g) [mono g.hom] :
subsingleton (lift_struct sq) :=
subsingleton.intro $ λ a b, lift_struct.ext a b $ (cancel_mono g.hom).1 $ by simp
end
variables {C : Type u} [category.{v} C]
/-- A helper construction: given a square between `i` and `f ≫ g`, produce a square between
`i` and `g`, whose top leg uses `f`:
A → X
↓f
↓i Y --> A → Y
↓g ↓i ↓g
B → Z B → Z
-/
@[simps] def square_to_snd {X Y Z: C} {i : arrow C} {f : X ⟶ Y} {g : Y ⟶ Z}
(sq : i ⟶ arrow.mk (f ≫ g)) :
i ⟶ arrow.mk g :=
{ left := sq.left ≫ f,
right := sq.right }
end arrow
namespace functor
universes v₁ v₂ u₁ u₂
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D]
/-- A functor `C ⥤ D` induces a functor between the corresponding arrow categories. -/
@[simps]
def map_arrow (F : C ⥤ D) : arrow C ⥤ arrow D :=
{ obj := λ a,
{ left := F.obj a.left,
right := F.obj a.right,
hom := F.map a.hom, },
map := λ a b f,
{ left := F.map f.left,
right := F.map f.right,
w' := by { have w := f.w, simp only [id_map] at w, dsimp, simp only [←F.map_comp, w], } } }
end functor
end category_theory
|
b14107f5609867c0e43c2c6846169f4d24e5aa27 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/finset/gcd_auto.lean | 55102fb70af0a4401f90f546818aa5d88b5da6b2 | [] | 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,757 | lean | /-
Copyright (c) 2020 Aaron Anderson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Aaron Anderson
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.finset.fold
import Mathlib.data.multiset.gcd
import Mathlib.PostPort
universes u_1 u_2
namespace Mathlib
/-!
# GCD and LCM operations on finsets
## Main definitions
- `finset.gcd` - the greatest common denominator of a `finset` of elements of a `gcd_monoid`
- `finset.lcm` - the least common multiple of a `finset` of elements of a `gcd_monoid`
## Implementation notes
Many of the proofs use the lemmas `gcd.def` and `lcm.def`, which relate `finset.gcd`
and `finset.lcm` to `multiset.gcd` and `multiset.lcm`.
TODO: simplify with a tactic and `data.finset.lattice`
## Tags
finset, gcd
-/
namespace finset
/-! ### lcm -/
/-- Least common multiple of a finite set -/
def lcm {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α] [gcd_monoid α]
(s : finset β) (f : β → α) : α :=
fold lcm 1 f s
theorem lcm_def {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s : finset β} {f : β → α} : lcm s f = multiset.lcm (multiset.map f (val s)) :=
rfl
@[simp] theorem lcm_empty {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α]
[nontrivial α] [gcd_monoid α] {f : β → α} : lcm ∅ f = 1 :=
fold_empty
@[simp] theorem lcm_dvd_iff {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α]
[nontrivial α] [gcd_monoid α] {s : finset β} {f : β → α} {a : α} :
lcm s f ∣ a ↔ ∀ (b : β), b ∈ s → f b ∣ a :=
sorry
theorem lcm_dvd {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s : finset β} {f : β → α} {a : α} :
(∀ (b : β), b ∈ s → f b ∣ a) → lcm s f ∣ a :=
iff.mpr lcm_dvd_iff
theorem dvd_lcm {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s : finset β} {f : β → α} {b : β} (hb : b ∈ s) : f b ∣ lcm s f :=
iff.mp lcm_dvd_iff (dvd_refl (lcm s f)) b hb
@[simp] theorem lcm_insert {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α]
[nontrivial α] [gcd_monoid α] {s : finset β} {f : β → α} [DecidableEq β] {b : β} :
lcm (insert b s) f = lcm (f b) (lcm s f) :=
sorry
@[simp] theorem lcm_singleton {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α]
[nontrivial α] [gcd_monoid α] {f : β → α} {b : β} :
lcm (singleton b) f = coe_fn normalize (f b) :=
multiset.lcm_singleton
@[simp] theorem normalize_lcm {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α]
[nontrivial α] [gcd_monoid α] {s : finset β} {f : β → α} :
coe_fn normalize (lcm s f) = lcm s f :=
sorry
theorem lcm_union {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s₁ : finset β} {s₂ : finset β} {f : β → α} [DecidableEq β] :
lcm (s₁ ∪ s₂) f = lcm (lcm s₁ f) (lcm s₂ f) :=
sorry
theorem lcm_congr {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s₁ : finset β} {s₂ : finset β} {f : β → α} {g : β → α} (hs : s₁ = s₂)
(hfg : ∀ (a : β), a ∈ s₂ → f a = g a) : lcm s₁ f = lcm s₂ g :=
Eq._oldrec (fun (hfg : ∀ (a : β), a ∈ s₁ → f a = g a) => fold_congr hfg) hs hfg
theorem lcm_mono_fun {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s : finset β} {f : β → α} {g : β → α} (h : ∀ (b : β), b ∈ s → f b ∣ g b) :
lcm s f ∣ lcm s g :=
lcm_dvd fun (b : β) (hb : b ∈ s) => dvd_trans (h b hb) (dvd_lcm hb)
theorem lcm_mono {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s₁ : finset β} {s₂ : finset β} {f : β → α} (h : s₁ ⊆ s₂) :
lcm s₁ f ∣ lcm s₂ f :=
lcm_dvd fun (b : β) (hb : b ∈ s₁) => dvd_lcm (h hb)
/-! ### gcd -/
/-- Greatest common divisor of a finite set -/
def gcd {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α] [gcd_monoid α]
(s : finset β) (f : β → α) : α :=
fold gcd 0 f s
theorem gcd_def {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s : finset β} {f : β → α} : gcd s f = multiset.gcd (multiset.map f (val s)) :=
rfl
@[simp] theorem gcd_empty {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α]
[nontrivial α] [gcd_monoid α] {f : β → α} : gcd ∅ f = 0 :=
fold_empty
theorem dvd_gcd_iff {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s : finset β} {f : β → α} {a : α} : a ∣ gcd s f ↔ ∀ (b : β), b ∈ s → a ∣ f b :=
sorry
theorem gcd_dvd {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s : finset β} {f : β → α} {b : β} (hb : b ∈ s) : gcd s f ∣ f b :=
iff.mp dvd_gcd_iff (dvd_refl (gcd s f)) b hb
theorem dvd_gcd {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s : finset β} {f : β → α} {a : α} :
(∀ (b : β), b ∈ s → a ∣ f b) → a ∣ gcd s f :=
iff.mpr dvd_gcd_iff
@[simp] theorem gcd_insert {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α]
[nontrivial α] [gcd_monoid α] {s : finset β} {f : β → α} [DecidableEq β] {b : β} :
gcd (insert b s) f = gcd (f b) (gcd s f) :=
sorry
@[simp] theorem gcd_singleton {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α]
[nontrivial α] [gcd_monoid α] {f : β → α} {b : β} :
gcd (singleton b) f = coe_fn normalize (f b) :=
multiset.gcd_singleton
@[simp] theorem normalize_gcd {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α]
[nontrivial α] [gcd_monoid α] {s : finset β} {f : β → α} :
coe_fn normalize (gcd s f) = gcd s f :=
sorry
theorem gcd_union {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s₁ : finset β} {s₂ : finset β} {f : β → α} [DecidableEq β] :
gcd (s₁ ∪ s₂) f = gcd (gcd s₁ f) (gcd s₂ f) :=
sorry
theorem gcd_congr {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s₁ : finset β} {s₂ : finset β} {f : β → α} {g : β → α} (hs : s₁ = s₂)
(hfg : ∀ (a : β), a ∈ s₂ → f a = g a) : gcd s₁ f = gcd s₂ g :=
Eq._oldrec (fun (hfg : ∀ (a : β), a ∈ s₁ → f a = g a) => fold_congr hfg) hs hfg
theorem gcd_mono_fun {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s : finset β} {f : β → α} {g : β → α} (h : ∀ (b : β), b ∈ s → f b ∣ g b) :
gcd s f ∣ gcd s g :=
dvd_gcd fun (b : β) (hb : b ∈ s) => dvd_trans (gcd_dvd hb) (h b hb)
theorem gcd_mono {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s₁ : finset β} {s₂ : finset β} {f : β → α} (h : s₁ ⊆ s₂) :
gcd s₂ f ∣ gcd s₁ f :=
dvd_gcd fun (b : β) (hb : b ∈ s₁) => gcd_dvd (h hb)
theorem gcd_eq_zero_iff {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α]
[nontrivial α] [gcd_monoid α] {s : finset β} {f : β → α} :
gcd s f = 0 ↔ ∀ (x : β), x ∈ s → f x = 0 :=
sorry
theorem gcd_eq_gcd_filter_ne_zero {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α]
[nontrivial α] [gcd_monoid α] {s : finset β} {f : β → α}
[decidable_pred fun (x : β) => f x = 0] : gcd s f = gcd (filter (fun (x : β) => f x ≠ 0) s) f :=
sorry
theorem gcd_mul_left {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s : finset β} {f : β → α} {a : α} :
(gcd s fun (x : β) => a * f x) = coe_fn normalize a * gcd s f :=
sorry
theorem gcd_mul_right {α : Type u_1} {β : Type u_2} [comm_cancel_monoid_with_zero α] [nontrivial α]
[gcd_monoid α] {s : finset β} {f : β → α} {a : α} :
(gcd s fun (x : β) => f x * a) = gcd s f * coe_fn normalize a :=
sorry
end finset
namespace finset
theorem gcd_eq_of_dvd_sub {α : Type u_1} {β : Type u_2} [nontrivial β] [integral_domain α]
[gcd_monoid α] {s : finset β} {f : β → α} {g : β → α} {a : α}
(h : ∀ (x : β), x ∈ s → a ∣ f x - g x) : gcd a (gcd s f) = gcd a (gcd s g) :=
sorry
end Mathlib |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.