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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2fad65e0ef5c0e02074b729d698f535278d2fa12 | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/algebra/order/absolute_value.lean | 716987c4048d15624fd580aac5aba18140cb94dd | [
"Apache-2.0"
] | permissive | waynemunro/mathlib | e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552 | 065a70810b5480d584033f7bbf8e0409480c2118 | refs/heads/master | 1,693,417,182,397 | 1,634,644,781,000 | 1,634,644,781,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,876 | 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 mul_hom 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 : 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*} [field 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_field
variables {S : Type*} [linear_ordered_field 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] : monoid_with_zero_hom 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
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*} [field 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
|
1a9a1e710f435f82f6b5a5e0963d5aacf59e23cc | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/partial_explicit.lean | 9fd9462c6c60bb631956d7cae6bbb4e9947ed2c3 | [
"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 | 462 | lean | section
variables a b c : nat
variable H1 : a = b
variable H2 : a + b + a + b = 0
example : b + b + a + b = 0 :=
@eq.rec_on _ _ (λ x, x + b + a + b = 0) _ H1 H2
end
section
variables (f : Π {T : Type} {a : T} {P : T → Prop}, P a → Π {b : T} {Q : T → Prop}, Q b → Prop)
variables (T : Type) (a : T) (P : T → Prop) (pa : P a)
variables (b : T) (Q : T → Prop) (qb : Q b)
#check @f T a P pa b Q qb -- Prop
#check f pa qb -- Prop
end
|
0e408a5b3f679d224c5aa0c45c913726583bbc4b | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/category/Group/epi_mono.lean | 4cf1e4732ee70df608d2ae83055eb4bc1f5318fa | [
"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 | 13,373 | lean | /-
Copyright (c) 2022 Jujian Zhang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jujian Zhang
-/
import algebra.category.Group.equivalence_Group_AddGroup
import group_theory.quotient_group
/-!
# Monomorphisms and epimorphisms in `Group`
In this file, we prove monomorphisms in category of group are injective homomorphisms and
epimorphisms are surjective homomorphisms.
-/
noncomputable theory
universes u v
namespace monoid_hom
open quotient_group
variables {A : Type u} {B : Type v}
section
variables [group A] [group B]
@[to_additive add_monoid_hom.ker_eq_bot_of_cancel]
lemma ker_eq_bot_of_cancel {f : A →* B} (h : ∀ (u v : f.ker →* A), f.comp u = f.comp v → u = v) :
f.ker = ⊥ :=
by simpa using _root_.congr_arg range (h f.ker.subtype 1 (by tidy))
end
section
variables [comm_group A] [comm_group B]
@[to_additive add_monoid_hom.range_eq_top_of_cancel]
lemma range_eq_top_of_cancel {f : A →* B}
(h : ∀ (u v : B →* B ⧸ f.range), u.comp f = v.comp f → u = v) :
f.range = ⊤ :=
begin
specialize h 1 (quotient_group.mk' _) _,
{ ext1,
simp only [one_apply, coe_comp, coe_mk', function.comp_app],
rw [show (1 : B ⧸ f.range) = (1 : B), from quotient_group.coe_one _, quotient_group.eq,
inv_one, one_mul],
exact ⟨x, rfl⟩, },
replace h : (quotient_group.mk' _).ker = (1 : B →* B ⧸ f.range).ker := by rw h,
rwa [ker_one, quotient_group.ker_mk] at h,
end
end
end monoid_hom
section
open category_theory
namespace Group
variables {A B : Group.{u}} (f : A ⟶ B)
@[to_additive AddGroup.ker_eq_bot_of_mono]
lemma ker_eq_bot_of_mono [mono f] : f.ker = ⊥ :=
monoid_hom.ker_eq_bot_of_cancel $ λ u v,
(@cancel_mono _ _ _ _ _ f _ (show Group.of f.ker ⟶ A, from u) _).1
@[to_additive AddGroup.mono_iff_ker_eq_bot]
lemma mono_iff_ker_eq_bot : mono f ↔ f.ker = ⊥ :=
⟨λ h, @@ker_eq_bot_of_mono f h,
λ h, concrete_category.mono_of_injective _ $ (monoid_hom.ker_eq_bot_iff f).1 h⟩
@[to_additive AddGroup.mono_iff_injective]
lemma mono_iff_injective : mono f ↔ function.injective f :=
iff.trans (mono_iff_ker_eq_bot f) $ monoid_hom.ker_eq_bot_iff f
namespace surjective_of_epi_auxs
local notation `X` := set.range (function.swap left_coset f.range.carrier)
/--
Define `X'` to be the set of all left cosets with an extra point at "infinity".
-/
@[nolint has_nonempty_instance]
inductive X_with_infinity
| from_coset : set.range (function.swap left_coset f.range.carrier) → X_with_infinity
| infinity : X_with_infinity
open X_with_infinity equiv.perm
open_locale coset
local notation `X'` := X_with_infinity f
local notation `∞` := X_with_infinity.infinity
local notation `SX'` := equiv.perm X'
instance : has_smul B X' :=
{ smul := λ b x, match x with
| from_coset y := from_coset ⟨b *l y,
begin
rw [←subtype.val_eq_coe, ←y.2.some_spec, left_coset_assoc],
use b * y.2.some,
end⟩
| ∞ := ∞
end }
lemma mul_smul (b b' : B) (x : X') : (b * b') • x = b • b' • x :=
match x with
| from_coset y :=
begin
change from_coset _ = from_coset _,
simp only [←subtype.val_eq_coe, left_coset_assoc],
end
| ∞ := rfl
end
lemma one_smul (x : X') : (1 : B) • x = x :=
match x with
| from_coset y :=
begin
change from_coset _ = from_coset _,
simp only [←subtype.val_eq_coe, one_left_coset, subtype.ext_iff_val],
end
| ∞ := rfl
end
lemma from_coset_eq_of_mem_range {b : B} (hb : b ∈ f.range) :
from_coset ⟨b *l f.range.carrier, ⟨b, rfl⟩⟩ =
from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩ :=
begin
congr,
change b *l f.range = f.range,
nth_rewrite 1 [show (f.range : set B) = 1 *l f.range, from (one_left_coset _).symm],
rw [left_coset_eq_iff, mul_one],
exact subgroup.inv_mem _ hb,
end
lemma from_coset_ne_of_nin_range {b : B} (hb : b ∉ f.range) :
from_coset ⟨b *l f.range.carrier, ⟨b, rfl⟩⟩ ≠
from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩ :=
begin
intros r,
simp only [subtype.mk_eq_mk] at r,
change b *l f.range = f.range at r,
nth_rewrite 1 [show (f.range : set B) = 1 *l f.range, from (one_left_coset _).symm] at r,
rw [left_coset_eq_iff, mul_one] at r,
exact hb (inv_inv b ▸ (subgroup.inv_mem _ r)),
end
instance : decidable_eq X' := classical.dec_eq _
/--
Let `τ` be the permutation on `X'` exchanging `f.range` and the point at infinity.
-/
noncomputable def tau : SX' :=
equiv.swap (from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩) ∞
local notation `τ` := tau f
lemma τ_apply_infinity :
τ ∞ = from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩ :=
equiv.swap_apply_right _ _
lemma τ_apply_from_coset :
τ (from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩) = ∞ :=
equiv.swap_apply_left _ _
lemma τ_apply_from_coset' (x : B) (hx : x ∈ f.range) :
τ (from_coset ⟨x *l f.range.carrier, ⟨x, rfl⟩⟩) = ∞ :=
(from_coset_eq_of_mem_range _ hx).symm ▸ τ_apply_from_coset _
lemma τ_symm_apply_from_coset :
(equiv.symm τ) (from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩) = ∞ :=
by rw [tau, equiv.symm_swap, equiv.swap_apply_left]
lemma τ_symm_apply_infinity :
(equiv.symm τ) ∞ = from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩ :=
by rw [tau, equiv.symm_swap, equiv.swap_apply_right]
/--
Let `g : B ⟶ S(X')` be defined as such that, for any `β : B`, `g(β)` is the function sending
point at infinity to point at infinity and sending coset `y` to `β *l y`.
-/
def G : B →* SX' :=
{ to_fun := λ β,
{ to_fun := λ x, β • x,
inv_fun := λ x, β⁻¹ • x,
left_inv := λ x, by { dsimp only, rw [←mul_smul, mul_left_inv, one_smul] },
right_inv := λ x, by { dsimp only, rw [←mul_smul, mul_right_inv, one_smul] } },
map_one' := by { ext, simp [one_smul] },
map_mul' := λ b1 b2, by { ext, simp [mul_smul] } }
local notation `g` := G f
/--
Define `h : B ⟶ S(X')` to be `τ g τ⁻¹`
-/
def H : B →* SX':=
{ to_fun := λ β, ((τ).symm.trans (g β)).trans τ,
map_one' := by { ext, simp },
map_mul' := λ b1 b2, by { ext, simp } }
local notation `h` := H f
/-!
The strategy is the following: assuming `epi f`
* prove that `f.range = {x | h x = g x}`;
* thus `f ≫ h = f ≫ g` so that `h = g`;
* but if `f` is not surjective, then some `x ∉ f.range`, then `h x ≠ g x` at the coset `f.range`.
-/
lemma g_apply_from_coset (x : B) (y : X) : (g x) (from_coset y) = from_coset ⟨x *l y, by tidy⟩ :=
rfl
lemma g_apply_infinity (x : B) : (g x) ∞ = ∞ := rfl
lemma h_apply_infinity (x : B) (hx : x ∈ f.range) : (h x) ∞ = ∞ :=
begin
simp only [H, monoid_hom.coe_mk, equiv.to_fun_as_coe, equiv.coe_trans, function.comp_app],
rw [τ_symm_apply_infinity, g_apply_from_coset],
simpa only [←subtype.val_eq_coe] using τ_apply_from_coset' f x hx,
end
lemma h_apply_from_coset (x : B) :
(h x) (from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩) =
from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩ :=
by simp [H, τ_symm_apply_from_coset, g_apply_infinity, τ_apply_infinity]
lemma h_apply_from_coset' (x : B) (b : B) (hb : b ∈ f.range):
(h x) (from_coset ⟨b *l f.range.carrier, ⟨b, rfl⟩⟩) =
from_coset ⟨b *l f.range.carrier, ⟨b, rfl⟩⟩ :=
(from_coset_eq_of_mem_range _ hb).symm ▸ h_apply_from_coset f x
lemma h_apply_from_coset_nin_range (x : B) (hx : x ∈ f.range) (b : B) (hb : b ∉ f.range) :
(h x) (from_coset ⟨b *l f.range.carrier, ⟨b, rfl⟩⟩) =
from_coset ⟨(x * b) *l f.range.carrier, ⟨x * b, rfl⟩⟩ :=
begin
simp only [H, tau, monoid_hom.coe_mk, equiv.to_fun_as_coe, equiv.coe_trans, function.comp_app],
rw [equiv.symm_swap, @equiv.swap_apply_of_ne_of_ne X' _
(from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩) ∞
(from_coset ⟨b *l f.range.carrier, ⟨b, rfl⟩⟩) (from_coset_ne_of_nin_range _ hb) (by simp)],
simp only [g_apply_from_coset, ←subtype.val_eq_coe, left_coset_assoc],
refine equiv.swap_apply_of_ne_of_ne (from_coset_ne_of_nin_range _ (λ r, hb _)) (by simp),
convert subgroup.mul_mem _ (subgroup.inv_mem _ hx) r,
rw [←mul_assoc, mul_left_inv, one_mul],
end
lemma agree : f.range.carrier = {x | h x = g x} :=
begin
refine set.ext (λ b, ⟨_, λ (hb : h b = g b), classical.by_contradiction (λ r, _)⟩),
{ rintros ⟨a, rfl⟩,
change h (f a) = g (f a),
ext ⟨⟨_, ⟨y, rfl⟩⟩⟩,
{ rw [g_apply_from_coset],
by_cases m : y ∈ f.range,
{ rw [h_apply_from_coset' _ _ _ m, from_coset_eq_of_mem_range _ m],
change from_coset _ = from_coset ⟨f a *l (y *l _), _⟩,
simpa only [←from_coset_eq_of_mem_range _ (subgroup.mul_mem _ ⟨a, rfl⟩ m),
left_coset_assoc] },
{ rw [h_apply_from_coset_nin_range _ _ ⟨_, rfl⟩ _ m],
simpa only [←subtype.val_eq_coe, left_coset_assoc], }, },
{ rw [g_apply_infinity, h_apply_infinity _ _ ⟨_, rfl⟩], } },
{ have eq1 : (h b) (from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩) =
(from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩) := by simp [H, tau, g_apply_infinity],
have eq2 : (g b) (from_coset ⟨f.range.carrier, ⟨1, one_left_coset _⟩⟩) =
(from_coset ⟨b *l f.range.carrier, ⟨b, rfl⟩⟩) := rfl,
exact (from_coset_ne_of_nin_range _ r).symm (by rw [←eq1, ←eq2, fun_like.congr_fun hb]) }
end
lemma comp_eq : f ≫ (show B ⟶ Group.of SX', from g) = f ≫ h :=
fun_like.ext _ _ $ λ a,
by simp only [comp_apply, show h (f a) = _, from (by simp [←agree] : f a ∈ {b | h b = g b})]
lemma g_ne_h (x : B) (hx : x ∉ f.range) : g ≠ h :=
begin
intros r,
replace r := fun_like.congr_fun (fun_like.congr_fun r x)
((from_coset ⟨f.range, ⟨1, one_left_coset _⟩⟩)),
rw [H, g_apply_from_coset, monoid_hom.coe_mk, tau] at r,
simp only [monoid_hom.coe_range, subtype.coe_mk, equiv.symm_swap,
equiv.to_fun_as_coe, equiv.coe_trans, function.comp_app] at r,
erw [equiv.swap_apply_left, g_apply_infinity, equiv.swap_apply_right] at r,
exact from_coset_ne_of_nin_range _ hx r,
end
end surjective_of_epi_auxs
lemma surjective_of_epi [epi f] : function.surjective f :=
begin
by_contra r,
push_neg at r,
rcases r with ⟨b, hb⟩,
exact surjective_of_epi_auxs.g_ne_h f b (λ ⟨c, hc⟩, hb _ hc)
((cancel_epi f).1 (surjective_of_epi_auxs.comp_eq f)),
end
lemma epi_iff_surjective : epi f ↔ function.surjective f :=
⟨λ h, @@surjective_of_epi f h, concrete_category.epi_of_surjective _⟩
lemma epi_iff_range_eq_top : epi f ↔ f.range = ⊤ :=
iff.trans (epi_iff_surjective _) (subgroup.eq_top_iff' f.range).symm
end Group
namespace AddGroup
variables {A B : AddGroup.{u}} (f : A ⟶ B)
lemma epi_iff_surjective : epi f ↔ function.surjective f :=
begin
have i1 : epi f ↔ epi (Group_AddGroup_equivalence.inverse.map f),
{ refine ⟨_, Group_AddGroup_equivalence.inverse.epi_of_epi_map⟩,
introsI e',
apply Group_AddGroup_equivalence.inverse.map_epi },
rwa Group.epi_iff_surjective at i1,
end
lemma epi_iff_range_eq_top : epi f ↔ f.range = ⊤ :=
iff.trans (epi_iff_surjective _) (add_subgroup.eq_top_iff' f.range).symm
end AddGroup
namespace Group
variables {A B : Group.{u}} (f : A ⟶ B)
@[to_additive]
instance forget_Group_preserves_mono : (forget Group).preserves_monomorphisms :=
{ preserves := λ X Y f e, by rwa [mono_iff_injective, ←category_theory.mono_iff_injective] at e }
@[to_additive]
instance forget_Group_preserves_epi : (forget Group).preserves_epimorphisms :=
{ preserves := λ X Y f e, by rwa [epi_iff_surjective, ←category_theory.epi_iff_surjective] at e }
end Group
namespace CommGroup
variables {A B : CommGroup.{u}} (f : A ⟶ B)
@[to_additive AddCommGroup.ker_eq_bot_of_mono]
lemma ker_eq_bot_of_mono [mono f] : f.ker = ⊥ :=
monoid_hom.ker_eq_bot_of_cancel $ λ u v,
(@cancel_mono _ _ _ _ _ f _ (show CommGroup.of f.ker ⟶ A, from u) _).1
@[to_additive AddCommGroup.mono_iff_ker_eq_bot]
lemma mono_iff_ker_eq_bot : mono f ↔ f.ker = ⊥ :=
⟨λ h, @@ker_eq_bot_of_mono f h,
λ h, concrete_category.mono_of_injective _ $ (monoid_hom.ker_eq_bot_iff f).1 h⟩
@[to_additive AddCommGroup.mono_iff_injective]
lemma mono_iff_injective : mono f ↔ function.injective f :=
iff.trans (mono_iff_ker_eq_bot f) $ monoid_hom.ker_eq_bot_iff f
@[to_additive]
lemma range_eq_top_of_epi [epi f] : f.range = ⊤ :=
monoid_hom.range_eq_top_of_cancel $ λ u v h,
(@cancel_epi _ _ _ _ _ f _ (show B ⟶ ⟨B ⧸ monoid_hom.range f⟩, from u) v).1 h
@[to_additive]
lemma epi_iff_range_eq_top : epi f ↔ f.range = ⊤ :=
⟨λ hf, by exactI range_eq_top_of_epi _,
λ hf, concrete_category.epi_of_surjective _ $ monoid_hom.range_top_iff_surjective.mp hf⟩
@[to_additive]
lemma epi_iff_surjective : epi f ↔ function.surjective f :=
by rw [epi_iff_range_eq_top, monoid_hom.range_top_iff_surjective]
@[to_additive]
instance forget_CommGroup_preserves_mono : (forget CommGroup).preserves_monomorphisms :=
{ preserves := λ X Y f e, by rwa [mono_iff_injective, ←category_theory.mono_iff_injective] at e }
@[to_additive]
instance forget_CommGroup_preserves_epi : (forget CommGroup).preserves_epimorphisms :=
{ preserves := λ X Y f e, by rwa [epi_iff_surjective, ←category_theory.epi_iff_surjective] at e }
end CommGroup
end
|
6185979c2edc081460fcdf6cc3a93ab4ab0c00e5 | 2d34dfb0a1cc250584282618dc10ea03d3fa858e | /src/Mbar/bounded.lean | 63f8c8f866f5ed779825c9d17caaf3e53109bf9f | [] | no_license | zeta1999/lean-liquid | 61e294ec5adae959d8ee1b65d015775484ff58c2 | 96bb0fa3afc3b451bcd1fb7d974348de2f290541 | refs/heads/master | 1,676,579,150,248 | 1,610,771,445,000 | 1,610,771,445,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,062 | lean | import data.fintype.intervals
import data.real.basic
import algebra.big_operators.ring
import data.fintype.card
import category_theory.Fintype
import topology.order
import topology.separation
import topology.subset_properties
import data.real.nnreal
noncomputable theory
open_locale big_operators classical nnreal
-- Thanks to Ruben Van de Velde and Yury G. Kudryashov for help with
-- the Ico_finite and Icc_finite lemmas.
-- TODO: Move these somewhere...
open set
lemma Ico_finite (a b : ℤ) : set.finite (Ico a b) := ⟨set.Ico_ℤ_fintype a b⟩
lemma Icc_finite (a b : ℤ) : set.finite (Icc a b) :=
begin
convert Ico_finite a (b+1),
ext,
simp [int.lt_add_one_iff],
end
instance (a b : ℤ) : fintype (Icc a b) := nonempty.some (Icc_finite a b)
structure Mbar_bdd (r : ℝ≥0) (S : Fintype) (c : ℝ≥0) (M : ℕ) :=
(to_fun : S → fin (M + 1) → ℤ)
(coeff_zero' : ∀ s, to_fun s 0 = 0)
(sum_le' : (∑ s i, (↑(to_fun s i).nat_abs * r^(i : ℕ))) ≤ c)
namespace Mbar_bdd
variables {r : ℝ≥0} {S : Fintype} {c c₁ c₂ : ℝ≥0} {M : ℕ}
instance has_coe_to_fun : has_coe_to_fun (Mbar_bdd r S c M) := ⟨_, Mbar_bdd.to_fun⟩
@[simp] lemma coe_mk (x h₁ h₂) : ((⟨x, h₁, h₂⟩ : Mbar_bdd r S c M) : S → ℕ → ℤ) = x := rfl
@[simp] protected lemma coeff_zero (x : Mbar_bdd r S c M) (s : S) : x s 0 = 0 := x.coeff_zero' s
protected lemma sum_le (x : Mbar_bdd r S c M) :
(∑ s i, ((↑(x s i).nat_abs * r^(i:ℕ)))) ≤ c := x.sum_le'
protected def cast_le [hc : fact (c₁ ≤ c₂)] (x : Mbar_bdd r S c₁ M) : Mbar_bdd r S c₂ M :=
⟨x.1, x.coeff_zero, x.sum_le.trans hc⟩
def mk' (x : S → fin (M + 1) → ℤ)
(h : (∀ s, x s 0 = 0) ∧
(∑ s i, ((↑(x s i).nat_abs * r^(i:ℕ)))) ≤ c) :
Mbar_bdd r S c M :=
{ to_fun := x, coeff_zero' := h.1, sum_le' := h.2 }
@[ext] lemma ext (x y : Mbar_bdd r S c M) (h : ⇑x = y) : x = y :=
by { cases x, cases y, congr, exact h }
instance : has_zero (Mbar_bdd r S c M) :=
{ zero :=
{ to_fun := 0,
coeff_zero' := λ s, rfl,
sum_le' :=
by simp only [zero_mul, pi.zero_apply, finset.sum_const_zero,
nat.cast_zero, zero_le', int.nat_abs_zero] } }
open finset
lemma coeff_bound [h0r : fact (0 < r)] (F : S → fin (M + 1) → ℤ)
(hF : ∑ s i, (↑(F s i).nat_abs * r^(i : ℕ)) ≤ c) (n : fin (M + 1)) (s : S) :
↑(F s n).nat_abs ≤ c / min (r ^ M) 1 :=
begin
rw [div_eq_mul_inv],
apply le_mul_inv_of_mul_le ((lt_min (pow_pos h0r _) zero_lt_one).ne.symm),
calc ↑(F s n).nat_abs * min (r ^ M) 1 ≤ ↑(F s n).nat_abs * r ^ (n:ℕ) : begin
refine mul_le_mul_of_nonneg_left _ (subtype.property (_ : ℝ≥0)),
cases le_or_lt r 1 with hr1 hr1,
{ refine le_trans (min_le_left _ _) _,
exact pow_le_pow_of_le_one (le_of_lt h0r) hr1 (nat.lt_add_one_iff.1 n.2) },
{ exact le_trans (min_le_right _ _) (one_le_pow_of_one_le (le_of_lt hr1) _) },
end
-- ... = (↑(F s n).nat_abs * r ^ (n:ℕ)) : by { } --rw [abs_mul, abs_of_pos (pow_pos h0r _)]
... ≤ ∑ i, (↑(F s i).nat_abs * r ^ (i:ℕ)) :
single_le_sum (λ (i : fin (M + 1)) _, _) (mem_univ n)
... ≤ ∑ s i, (↑(F s i).nat_abs * r^(i : ℕ)) : begin
refine single_le_sum (λ _ _, _) (mem_univ s),
exact sum_nonneg (λ _ _, (subtype.property (_ : ℝ≥0))),
end
... ≤ c : hF,
apply subtype.property (_ : ℝ≥0)
end
-- move this
lemma cast_nat_abs {R : Type*} [linear_ordered_ring R] : ∀ (n : ℤ), (n.nat_abs : R) = abs n
| (n : ℕ) := by simp only [int.nat_abs_of_nat, int.cast_coe_nat, nat.abs_cast]
| -[1+n] := by simp only [int.nat_abs, int.cast_neg_succ_of_nat, abs_neg,
← nat.cast_succ, nat.abs_cast]
lemma cast_nat_abs_eq_nnabs_cast (n : ℤ) :
(n.nat_abs : ℝ≥0) = real.nnabs n :=
by { ext, rw [nnreal.coe_nat_cast, cast_nat_abs, nnreal.coe_nnabs] }
private def temp_map [fact (0 < r)] (F : Mbar_bdd r S c M) (n : fin (M + 1)) (s : S) :
Icc (ceil (-(c / min (r ^ M) 1) : ℝ)) (floor (c / min (r ^ M) 1 : ℝ)) :=
begin
have h : (-(c / min (r ^ M) 1) : ℝ) ≤ F s n ∧ (F s n : ℝ) ≤ (c / min (r ^ M) 1 : ℝ),
{ rw [← abs_le, ← nnreal.coe_nnabs, ← cast_nat_abs_eq_nnabs_cast, nnreal.coe_nat_cast],
norm_cast, exact coeff_bound F F.sum_le n s },
exact ⟨F s n, ceil_le.2 $ h.1, le_floor.2 h.2⟩
end
instance [fact (0 < r)] : fintype (Mbar_bdd r S c M) :=
fintype.of_injective temp_map begin
rintros ⟨f1, hf1, hf1'⟩ ⟨f2, hf2, hf2'⟩ h,
ext s n,
change (temp_map ⟨f1, hf1, hf1'⟩ n s).1 = (temp_map ⟨f2, hf2, hf2'⟩ n s).1,
rw h,
end
def ι {M N : ℕ} (h : M ≤ N) : fin M ↪ fin N := (fin.cast_le h).to_embedding
-- Should this be in mathlib?
lemma sum_eq_sum_map_ι {M N : ℕ} (h : M ≤ N) (f : fin N → ℝ≥0) :
∑ i, f (ι h i) = ∑ j in finset.map (ι h) finset.univ, f j :=
finset.sum_bij' (λ a _, ι h a) (λ a ha, by {rw mem_map, exact ⟨a, ha, rfl⟩})
(λ a ha, rfl) (λ a ha, ⟨a.1, begin
rcases finset.mem_map.mp ha with ⟨⟨w,ww⟩,hw,rfl⟩,
change w < M,
linarith,
end ⟩)
(λ a ha, finset.mem_univ _) (λ a ha, by tidy) (λ a ha, by tidy)
/-- The transition maps between the Mbar_bdd sets. -/
def transition (r : ℝ≥0) {S : Fintype} {c : ℝ≥0} {M N : ℕ} (h : M ≤ N) (x : Mbar_bdd r S c N) :
Mbar_bdd r S c M :=
{ to_fun := λ s i, x s (ι (add_le_add_right h 1) i),
coeff_zero' := λ s, x.coeff_zero _,
sum_le' :=
begin
refine le_trans _ x.sum_le,
apply finset.sum_le_sum,
intros s hs,
let I := finset.map (ι (by linarith : M+1 ≤ N+1))
(finset.univ : finset (fin (M+1))),
refine le_trans _
(finset.sum_le_sum_of_subset_of_nonneg (finset.subset_univ I) _),
{ rw ← sum_eq_sum_map_ι,
apply le_of_eq,
congr },
{ intros, exact subtype.property (_ : ℝ≥0) }
end }
lemma transition_eq {r : ℝ≥0} {S : Fintype} {c : ℝ≥0} {M N : ℕ} (h : M ≤ N)
(F : Mbar_bdd r S c N) (s : S) (i : fin (M+1)) :
(transition r h F).1 s i = F.1 s (ι (by linarith) i) := by tidy
lemma transition_transition {r : ℝ≥0} {S : Fintype} {c : ℝ≥0}
{M N K : ℕ} (h : M ≤ N) (hh : N ≤ K) (x : Mbar_bdd r S c K) :
transition r h (transition r hh x) = transition r (le_trans h hh) x := by tidy
lemma transition_cast_le {N : ℕ} (h : M ≤ N) [hc : fact (c₁ ≤ c₂)] (x : Mbar_bdd r S c₁ N) :
transition r h (@Mbar_bdd.cast_le r S c₁ c₂ N _ x) =
Mbar_bdd.cast_le (transition r h x) :=
by { ext, refl }
@[reducible] def limit (r S c) :=
{ F : Π (M : ℕ), Mbar_bdd r S c M // ∀ (M N : ℕ) (h : M ≤ N), transition r h (F N) = F M }
def emb_aux : limit r S c → (Π (M : ℕ), Mbar_bdd r S c M) := coe
section topological_structure
instance : topological_space (Mbar_bdd r S c M) := ⊥
instance : discrete_topology (Mbar_bdd r S c M) := ⟨rfl⟩
-- sanity check
example : t2_space (limit r S c) := by apply_instance
example : totally_disconnected_space (limit r S c) := by apply_instance
example [fact (0 < r)] : compact_space (Mbar_bdd r S c M) := by apply_instance
def Γ : Π (m n : ℕ) (h : m ≤ n), set (Π (M : ℕ), Mbar_bdd r S c M) := λ m n h,
{ F | transition r h (F n) = F m }
def Γ₀ : Π (m n : ℕ) (h : m ≤ n), set (Mbar_bdd r S c m × Mbar_bdd r S c n) := λ m n h,
{ a | transition r h a.2 = a.1 }
def π : Π (m : ℕ), (Π (M : ℕ), Mbar_bdd r S c M) → Mbar_bdd r S c m := λ m F, F m
def π₂ : Π (m n : ℕ) (h : m ≤ n), (Π (M : ℕ), Mbar_bdd r S c M) → Mbar_bdd r S c m × Mbar_bdd r S c n :=
λ m n h F, ⟨F m, F n⟩
lemma range_emb_aux_eq : range (@emb_aux r S c) = ⋂ (x : {y : ℕ × ℕ // y.1 ≤ y.2}), Γ x.1.1 x.1.2 x.2 :=
set.ext $ λ x, iff.intro (λ ⟨w,hx⟩ y ⟨z,hz⟩, hz ▸ hx ▸ w.2 _ _ _)
(λ h0, ⟨⟨x,λ m n h1, h0 _ ⟨⟨⟨m,n⟩,h1⟩,rfl⟩⟩, rfl⟩)
lemma π_continuous {m : ℕ} : continuous (π m : _ → Mbar_bdd r S c m) := continuous_apply _
lemma π₂_eq {m n : ℕ} {h : m ≤ n} : (π₂ m n h : _ → Mbar_bdd r S c m × _) = (λ x, ⟨x m, x n⟩) :=
by {ext; refl}
lemma π₂_continuous {m n : ℕ} {h : m ≤ n} : continuous (π₂ m n h : _ → Mbar_bdd r S c _ × _) :=
by {rw π₂_eq, exact continuous.prod_mk π_continuous π_continuous}
def emb (r S c) : closed_embedding (@emb_aux r S c) :=
{ induced := rfl,
inj := by tidy,
closed_range := begin
rw range_emb_aux_eq,
apply is_closed_Inter,
rintros ⟨⟨m,n⟩,h0⟩,
rw (show (Γ m n h0 : set (Π M, Mbar_bdd r S c M)) = π₂ m n h0 ⁻¹' (Γ₀ m n h0), by tauto),
refine is_closed.preimage π₂_continuous (is_closed_discrete _),
end }
instance [fact (0 < r)] : compact_space (limit r S c) :=
begin
erw [← compact_iff_compact_space, compact_iff_compact_univ, compact_iff_compact_in_subtype],
apply is_closed.compact,
exact embedding_is_closed (emb r S c).to_embedding (emb r S c).closed_range is_closed_univ,
end
def proj (M : ℕ) : Mbar_bdd.limit r S c → Mbar_bdd r S c M := λ F, F.1 M
lemma proj_eq (M : ℕ) : (proj M : _ → Mbar_bdd r S c _) = (π M) ∘ emb_aux := rfl
lemma continuous_iff {α : Type*} [topological_space α] (f : α → Mbar_bdd.limit r S c) :
continuous f ↔ (∀ (M : ℕ), continuous ((proj M) ∘ f)) :=
begin
split,
{ intros hf M,
refine continuous.comp _ hf,
rw proj_eq,
exact continuous.comp π_continuous (emb _ _ _).continuous },
{ intros h,
rw [embedding.continuous_iff (emb r S c).to_embedding],
exact continuous_pi h }
end
end topological_structure
section addition
def add (F : Mbar_bdd r S c₁ M) (G : Mbar_bdd r S c₂ M) : Mbar_bdd r S (c₁ + c₂) M :=
{ to_fun := F + G,
coeff_zero' := λ s, by simp,
sum_le' :=
begin
refine le_trans _ (add_le_add F.sum_le G.sum_le),
rw ← finset.sum_add_distrib,
refine finset.sum_le_sum _,
rintro s -,
rw ← sum_add_distrib,
refine finset.sum_le_sum _,
rintro i -,
rw ← add_mul,
apply mul_le_mul_right',
norm_cast,
apply int.nat_abs_add_le
end }
end addition
end Mbar_bdd
|
9628144800f7435e6968ff55bfe7e9964cdddb73 | e61a235b8468b03aee0120bf26ec615c045005d2 | /src/Init/Lean/Meta/ExprDefEq.lean | bf9c3663d7a046cd0079d884fbb524938475a71f | [
"Apache-2.0"
] | permissive | SCKelemen/lean4 | 140dc63a80539f7c61c8e43e1c174d8500ec3230 | e10507e6615ddbef73d67b0b6c7f1e4cecdd82bc | refs/heads/master | 1,660,973,595,917 | 1,590,278,033,000 | 1,590,278,033,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 41,901 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Lean.ProjFns
import Init.Lean.Meta.WHNF
import Init.Lean.Meta.InferType
import Init.Lean.Meta.FunInfo
import Init.Lean.Meta.LevelDefEq
import Init.Lean.Meta.Check
import Init.Lean.Meta.Offset
namespace Lean
namespace Meta
/--
Try to solve `a := (fun x => t) =?= b` by eta-expanding `b`.
Remark: eta-reduction is not a good alternative even in a system without universe cumulativity like Lean.
Example:
```
(fun x : A => f ?m) =?= f
```
The left-hand side of the constraint above it not eta-reduced because `?m` is a metavariable. -/
private def isDefEqEta (a b : Expr) : MetaM Bool :=
if a.isLambda && !b.isLambda then do
bType ← inferType b;
bType ← whnfD bType;
match bType with
| Expr.forallE n d _ c =>
let b' := Lean.mkLambda n c.binderInfo d (mkApp b (mkBVar 0));
commitWhen $ isExprDefEqAux a b'
| _ => pure false
else
pure false
/-- Support for `Lean.reduceBool` and `Lean.reduceNat` -/
def isDefEqNative (s t : Expr) : MetaM LBool := do
let isDefEq (s t) : MetaM LBool := toLBoolM $ isExprDefEqAux s t;
s? ← reduceNative? s;
t? ← reduceNative? t;
match s?, t? with
| some s, some t => isDefEq s t
| some s, none => isDefEq s t
| none, some t => isDefEq s t
| none, none => pure LBool.undef
/-- Support for reducing Nat basic operations. -/
def isDefEqNat (s t : Expr) : MetaM LBool := do
let isDefEq (s t) : MetaM LBool := toLBoolM $ isExprDefEqAux s t;
if s.hasFVar || s.hasMVar || t.hasFVar || t.hasMVar then
pure LBool.undef
else do
s? ← reduceNat? s;
t? ← reduceNat? t;
match s?, t? with
| some s, some t => isDefEq s t
| some s, none => isDefEq s t
| none, some t => isDefEq s t
| none, none => pure LBool.undef
/-- Support for constraints of the form `("..." =?= String.mk cs)` -/
def isDefEqStringLit (s t : Expr) : MetaM LBool := do
let isDefEq (s t) : MetaM LBool := toLBoolM $ isExprDefEqAux s t;
if s.isStringLit && t.isAppOf `String.mk then
isDefEq (WHNF.toCtorIfLit s) t
else if s.isAppOf `String.mk && t.isStringLit then
isDefEq s (WHNF.toCtorIfLit t)
else
pure LBool.undef
/--
Return `true` if `e` is of the form `fun (x_1 ... x_n) => ?m x_1 ... x_n)`, and `?m` is unassigned.
Remark: `n` may be 0. -/
def isEtaUnassignedMVar (e : Expr) : MetaM Bool :=
match e.etaExpanded? with
| some (Expr.mvar mvarId _) =>
condM (isReadOnlyOrSyntheticOpaqueExprMVar mvarId)
(pure false)
(condM (isExprMVarAssigned mvarId)
(pure false)
(pure true))
| _ => pure false
/-
First pass for `isDefEqArgs`. We unify explicit arguments, *and* easy cases
Here, we say a case is easy if it is of the form
?m =?= t
or
t =?= ?m
where `?m` is unassigned.
These easy cases are not just an optimization. When
`?m` is a function, by assigning it to t, we make sure
a unification constraint (in the explicit part)
```
?m t =?= f s
```
is not higher-order.
We also handle the eta-expanded cases:
```
fun x₁ ... xₙ => ?m x₁ ... xₙ =?= t
t =?= fun x₁ ... xₙ => ?m x₁ ... xₙ
This is important because type inference often produces
eta-expanded terms, and without this extra case, we could
introduce counter intuitive behavior.
Pre: `paramInfo.size <= args₁.size = args₂.size`
-/
private partial def isDefEqArgsFirstPass
(paramInfo : Array ParamInfo) (args₁ args₂ : Array Expr) : Nat → Array Nat → MetaM (Option (Array Nat))
| i, postponed =>
if h : i < paramInfo.size then
let info := paramInfo.get ⟨i, h⟩;
let a₁ := args₁.get! i;
let a₂ := args₂.get! i;
if info.implicit || info.instImplicit then
condM (isEtaUnassignedMVar a₁ <||> isEtaUnassignedMVar a₂)
(condM (isExprDefEqAux a₁ a₂)
(isDefEqArgsFirstPass (i+1) postponed)
(pure none))
(isDefEqArgsFirstPass (i+1) (postponed.push i))
else
condM (isExprDefEqAux a₁ a₂)
(isDefEqArgsFirstPass (i+1) postponed)
(pure none)
else
pure (some postponed)
private partial def isDefEqArgsAux (args₁ args₂ : Array Expr) (h : args₁.size = args₂.size) : Nat → MetaM Bool
| i =>
if h₁ : i < args₁.size then
let a₁ := args₁.get ⟨i, h₁⟩;
let a₂ := args₂.get ⟨i, h ▸ h₁⟩;
condM (isExprDefEqAux a₁ a₂)
(isDefEqArgsAux (i+1))
(pure false)
else
pure true
@[specialize] private def trySynthPending (e : Expr) : MetaM Bool := do
mvarId? ← getStuckMVar? e;
match mvarId? with
| some mvarId => synthPending mvarId
| none => pure false
private def isDefEqArgs (f : Expr) (args₁ args₂ : Array Expr) : MetaM Bool :=
if h : args₁.size = args₂.size then do
finfo ← getFunInfoNArgs f args₁.size;
(some postponed) ← isDefEqArgsFirstPass finfo.paramInfo args₁ args₂ 0 #[] | pure false;
(isDefEqArgsAux args₁ args₂ h finfo.paramInfo.size)
<&&>
(postponed.allM $ fun i => do
/- Second pass: unify implicit arguments.
In the second pass, we make sure we are unfolding at
least non reducible definitions (default setting). -/
let a₁ := args₁.get! i;
let a₂ := args₂.get! i;
let info := finfo.paramInfo.get! i;
when info.instImplicit $ do {
_ ← trySynthPending a₁;
_ ← trySynthPending a₂;
pure ()
};
withAtLeastTransparency TransparencyMode.default $ isExprDefEqAux a₁ a₂)
else
pure false
/--
Check whether the types of the free variables at `fvars` are
definitionally equal to the types at `ds₂`.
Pre: `fvars.size == ds₂.size`
This method also updates the set of local instances, and invokes
the continuation `k` with the updated set.
We can't use `withNewLocalInstances` because the `isDeq fvarType d₂`
may use local instances. -/
@[specialize] partial def isDefEqBindingDomain (fvars : Array Expr) (ds₂ : Array Expr) : Nat → MetaM Bool → MetaM Bool
| i, k =>
if h : i < fvars.size then do
let fvar := fvars.get ⟨i, h⟩;
fvarDecl ← getFVarLocalDecl fvar;
let fvarType := fvarDecl.type;
let d₂ := ds₂.get! i;
condM (isExprDefEqAux fvarType d₂)
(do c? ← isClass fvarType;
match c? with
| some className => withNewLocalInstance className fvar $ isDefEqBindingDomain (i+1) k
| none => isDefEqBindingDomain (i+1) k)
(pure false)
else
k
/- Auxiliary function for `isDefEqBinding` for handling binders `forall/fun`.
It accumulates the new free variables in `fvars`, and declare them at `lctx`.
We use the domain types of `e₁` to create the new free variables.
We store the domain types of `e₂` at `ds₂`. -/
private partial def isDefEqBindingAux : LocalContext → Array Expr → Expr → Expr → Array Expr → MetaM Bool
| lctx, fvars, e₁, e₂, ds₂ =>
let process (n : Name) (d₁ d₂ b₁ b₂ : Expr) : MetaM Bool := do {
let d₁ := d₁.instantiateRev fvars;
let d₂ := d₂.instantiateRev fvars;
fvarId ← mkFreshId;
let lctx := lctx.mkLocalDecl fvarId n d₁;
let fvars := fvars.push (mkFVar fvarId);
isDefEqBindingAux lctx fvars b₁ b₂ (ds₂.push d₂)
};
match e₁, e₂ with
| Expr.forallE n d₁ b₁ _, Expr.forallE _ d₂ b₂ _ => process n d₁ d₂ b₁ b₂
| Expr.lam n d₁ b₁ _, Expr.lam _ d₂ b₂ _ => process n d₁ d₂ b₁ b₂
| _, _ =>
adaptReader (fun (ctx : Context) => { ctx with lctx := lctx }) $
isDefEqBindingDomain fvars ds₂ 0 $
isExprDefEqAux (e₁.instantiateRev fvars) (e₂.instantiateRev fvars)
@[inline] private def isDefEqBinding (a b : Expr) : MetaM Bool := do
lctx ← getLCtx;
isDefEqBindingAux lctx #[] a b #[]
private def checkTypesAndAssign (mvar : Expr) (v : Expr) : MetaM Bool :=
traceCtx `Meta.isDefEq.assign.checkTypes $ do
-- must check whether types are definitionally equal or not, before assigning and returning true
mvarType ← inferType mvar;
vType ← inferType v;
condM (withTransparency TransparencyMode.default $ isExprDefEqAux mvarType vType)
(do trace! `Meta.isDefEq.assign.final (mvar ++ " := " ++ v);
assignExprMVar mvar.mvarId! v; pure true)
(do trace `Meta.isDefEq.assign.typeMismatch $ fun _ => mvar ++ " : " ++ mvarType ++ " := " ++ v ++ " : " ++ vType;
pure false)
/-
Each metavariable is declared in a particular local context.
We use the notation `C |- ?m : t` to denote a metavariable `?m` that
was declared at the local context `C` with type `t` (see `MetavarDecl`).
We also use `?m@C` as a shorthand for `C |- ?m : t` where `t` is the type of `?m`.
The following method process the unification constraint
?m@C a₁ ... aₙ =?= t
We say the unification constraint is a pattern IFF
1) `a₁ ... aₙ` are pairwise distinct free variables that are *not* let-variables.
2) `a₁ ... aₙ` are not in `C`
3) `t` only contains free variables in `C` and/or `{a₁, ..., aₙ}`
4) For every metavariable `?m'@C'` occurring in `t`, `C'` is a subprefix of `C`
5) `?m` does not occur in `t`
Claim: we don't have to check free variable declarations. That is,
if `t` contains a reference to `x : A := v`, we don't need to check `v`.
Reason: The reference to `x` is a free variable, and it must be in `C` (by 1 and 3).
If `x` is in `C`, then any metavariable occurring in `v` must have been defined in a strict subprefix of `C`.
So, condition 4 and 5 are satisfied.
If the conditions above have been satisfied, then the
solution for the unification constrain is
?m := fun a₁ ... aₙ => t
Now, we consider some workarounds/approximations.
A1) Suppose `t` contains a reference to `x : A := v` and `x` is not in `C` (failed condition 3)
(precise) solution: unfold `x` in `t`.
A2) Suppose some `aᵢ` is in `C` (failed condition 2)
(approximated) solution (when `config.foApprox` is set to true) :
ignore condition and also use
?m := fun a₁ ... aₙ => t
Here is an example where this approximation fails:
Given `C` containing `a : nat`, consider the following two constraints
?m@C a =?= a
?m@C b =?= a
If we use the approximation in the first constraint, we get
?m := fun x => x
when we apply this solution to the second one we get a failure.
IMPORTANT: When applying this approximation we need to make sure the
abstracted term `fun a₁ ... aₙ => t` is type correct. The check
can only be skipped in the pattern case described above. Consider
the following example. Given the local context
(α : Type) (a : α)
we try to solve
?m α =?= @id α a
If we use the approximation above we obtain:
?m := (fun α' => @id α' a)
which is a type incorrect term. `a` has type `α` but it is expected to have
type `α'`.
The problem occurs because the right hand side contains a free variable
`a` that depends on the free variable `α` being abstracted. Note that
this dependency cannot occur in patterns.
We can address this by type checking
the term after abstraction. This is not a significant performance
bottleneck because this case doesn't happen very often in practice
(262 times when compiling stdlib on Jan 2018). The second example
is trickier, but it also occurs less frequently (8 times when compiling
stdlib on Jan 2018, and all occurrences were at Init/Control when
we define monads and auxiliary combinators for them).
We considered three options for the addressing the issue on the second example:
A3) `a₁ ... aₙ` are not pairwise distinct (failed condition 1).
In Lean3, we would try to approximate this case using an approach similar to A2.
However, this approximation complicates the code, and is never used in the
Lean3 stdlib and mathlib.
A4) `t` contains a metavariable `?m'@C'` where `C'` is not a subprefix of `C`.
If `?m'` is assigned, we substitute.
If not, we create an auxiliary metavariable with a smaller scope.
Actually, we let `elimMVarDeps` at `MetavarContext.lean` to perform this step.
A5) If some `aᵢ` is not a free variable,
then we use first-order unification (if `config.foApprox` is set to true)
?m a_1 ... a_i a_{i+1} ... a_{i+k} =?= f b_1 ... b_k
reduces to
?M a_1 ... a_i =?= f
a_{i+1} =?= b_1
...
a_{i+k} =?= b_k
A6) If (m =?= v) is of the form
?m a_1 ... a_n =?= ?m b_1 ... b_k
then we use first-order unification (if `config.foApprox` is set to true)
-/
namespace CheckAssignment
structure Context extends Meta.Context :=
(mvarId : MVarId)
(mvarDecl : MetavarDecl)
(fvars : Array Expr)
(hasCtxLocals : Bool)
inductive Exception
| occursCheck
| useFOApprox
| outOfScopeFVar (fvarId : FVarId)
| readOnlyMVarWithBiggerLCtx (mvarId : MVarId)
| unknownExprMVar (mvarId : MVarId)
| meta (ex : Meta.Exception)
structure State extends Meta.State :=
(checkCache : ExprStructMap Expr := {})
abbrev CheckAssignmentM := ReaderT Context (EStateM Exception State)
private def findCached? (e : Expr) : CheckAssignmentM (Option Expr) := do
s ← get; pure $ s.checkCache.find? e
private def cache (e r : Expr) : CheckAssignmentM Unit :=
modify $ fun s => { s with checkCache := s.checkCache.insert e r }
instance : MonadCache Expr Expr CheckAssignmentM :=
{ findCached? := findCached?, cache := cache }
def liftMetaM {α} (x : MetaM α) : CheckAssignmentM α :=
fun ctx s => match x ctx.toContext s.toState with
| EStateM.Result.ok a newS => EStateM.Result.ok a { s with toState := newS }
| EStateM.Result.error ex newS => EStateM.Result.error (Exception.meta ex) { s with toState := newS }
@[inline] private def visit (f : Expr → CheckAssignmentM Expr) (e : Expr) : CheckAssignmentM Expr :=
if !e.hasExprMVar && !e.hasFVar then pure e else checkCache e f
@[specialize] def checkFVar (check : Expr → CheckAssignmentM Expr) (fvar : Expr) : CheckAssignmentM Expr := do
ctx ← read;
if ctx.mvarDecl.lctx.containsFVar fvar then pure fvar
else do
let lctx := ctx.lctx;
match lctx.findFVar? fvar with
| some (LocalDecl.ldecl _ _ _ _ v) => visit check v
| _ =>
if ctx.fvars.contains fvar then pure fvar
else throw $ Exception.outOfScopeFVar fvar.fvarId!
@[inline] def getMCtx : CheckAssignmentM MetavarContext := do
s ← get; pure s.mctx
def mkAuxMVar (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) : CheckAssignmentM Expr := do
s ← get;
let mvarId := s.ngen.curr;
modify $ fun s => { s with ngen := s.ngen.next, mctx := s.mctx.addExprMVarDecl mvarId Name.anonymous lctx localInsts type };
pure (mkMVar mvarId)
@[specialize] def checkMVar (check : Expr → CheckAssignmentM Expr) (mvar : Expr) : CheckAssignmentM Expr := do
let mvarId := mvar.mvarId!;
ctx ← read;
mctx ← getMCtx;
if mvarId == ctx.mvarId then throw Exception.occursCheck
else match mctx.getExprAssignment? mvarId with
| some v => check v
| none => match mctx.findDecl? mvarId with
| none => throw $ Exception.unknownExprMVar mvarId
| some mvarDecl =>
if ctx.hasCtxLocals then
throw $ Exception.useFOApprox -- It is not a pattern, then we fail and fall back to FO unification
else if mvarDecl.lctx.isSubPrefixOf ctx.mvarDecl.lctx ctx.fvars then
/- The local context of `mvar` - free variables being abstracted is a subprefix of the metavariable being assigned.
We "substract" variables being abstracted because we use `elimMVarDeps` -/
pure mvar
else if mvarDecl.depth != mctx.depth || mvarDecl.kind.isSyntheticOpaque then throw $ Exception.readOnlyMVarWithBiggerLCtx mvarId
else if ctx.config.ctxApprox && ctx.mvarDecl.lctx.isSubPrefixOf mvarDecl.lctx then do
mvarType ← check mvarDecl.type;
/- Create an auxiliary metavariable with a smaller context and "checked" type.
Note that `mvarType` may be different from `mvarDecl.type`. Example: `mvarType` contains
a metavariable that we also need to reduce the context. -/
newMVar ← mkAuxMVar ctx.mvarDecl.lctx ctx.mvarDecl.localInstances mvarType;
modify $ fun s => { s with mctx := s.mctx.assignExpr mvarId newMVar };
pure newMVar
else
pure mvar
/-
Auxiliary function used to "fix" subterms of the form `?m x_1 ... x_n` where `x_i`s are free variables,
and one of them is out-of-scope.
See `Expr.app` case at `check`.
If `ctxApprox` is true, then we solve this case by creating a fresh metavariable ?n with the correct scope,
an assigning `?m := fun _ ... _ => ?n` -/
def assignToConstFun (mvar : Expr) (numArgs : Nat) (newMVar : Expr) : MetaM Bool := do
mvarType ← inferType mvar;
forallBoundedTelescope mvarType numArgs $ fun xs _ =>
if xs.size != numArgs then pure false
else do
v ← mkLambda xs newMVar;
checkTypesAndAssign mvar v
partial def check : Expr → CheckAssignmentM Expr
| e@(Expr.mdata _ b _) => do b ← visit check b; pure $ e.updateMData! b
| e@(Expr.proj _ _ s _) => do s ← visit check s; pure $ e.updateProj! s
| e@(Expr.lam _ d b _) => do d ← visit check d; b ← visit check b; pure $ e.updateLambdaE! d b
| e@(Expr.forallE _ d b _) => do d ← visit check d; b ← visit check b; pure $ e.updateForallE! d b
| e@(Expr.letE _ t v b _) => do t ← visit check t; v ← visit check v; b ← visit check b; pure $ e.updateLet! t v b
| e@(Expr.bvar _ _) => pure e
| e@(Expr.sort _ _) => pure e
| e@(Expr.const _ _ _) => pure e
| e@(Expr.lit _ _) => pure e
| e@(Expr.fvar _ _) => visit (checkFVar check) e
| e@(Expr.mvar _ _) => visit (checkMVar check) e
| Expr.localE _ _ _ _ => unreachable!
| e@(Expr.app _ _ _) => e.withApp $ fun f args => do
ctx ← read;
if f.isMVar && ctx.config.ctxApprox && args.all Expr.isFVar then do
f ← visit (checkMVar check) f;
catch
(do
args ← args.mapM (visit check);
pure $ mkAppN f args)
(fun ex => match ex with
| Exception.outOfScopeFVar _ =>
condM (liftMetaM $ isDelayedAssigned f.mvarId!) (throw ex) $ do
eType ← liftMetaM $ inferType e;
mvarType ← check eType;
/- Create an auxiliary metavariable with a smaller context and "checked" type, assign `?f := fun _ => ?newMVar`
Note that `mvarType` may be different from `eType`. -/
newMVar ← mkAuxMVar ctx.mvarDecl.lctx ctx.mvarDecl.localInstances mvarType;
condM (liftMetaM $ assignToConstFun f args.size newMVar)
(pure newMVar)
(throw ex)
| _ => throw ex)
else do
f ← visit check f;
args ← args.mapM (visit check);
pure $ mkAppN f args
end CheckAssignment
private def checkAssignmentFailure (mvarId : MVarId) (fvars : Array Expr) (v : Expr) (ex : CheckAssignment.Exception) : MetaM (Option Expr) :=
match ex with
| CheckAssignment.Exception.occursCheck => do
trace! `Meta.isDefEq.assign.occursCheck (mkMVar mvarId ++ " " ++ fvars ++ " := " ++ v);
pure none
| CheckAssignment.Exception.useFOApprox =>
pure none
| CheckAssignment.Exception.outOfScopeFVar fvarId => do
trace! `Meta.isDefEq.assign.outOfScopeFVar (mkFVar fvarId ++ " @ " ++ mkMVar mvarId ++ " " ++ fvars ++ " := " ++ v);
pure none
| CheckAssignment.Exception.readOnlyMVarWithBiggerLCtx nestedMVarId => do
trace! `Meta.isDefEq.assign.readOnlyMVarWithBiggerLCtx (mkMVar nestedMVarId ++ " @ " ++ mkMVar mvarId ++ " " ++ fvars ++ " := " ++ v);
pure none
| CheckAssignment.Exception.unknownExprMVar mvarId =>
-- This case can only happen if the MetaM API is being misused
throwEx $ Exception.unknownExprMVar mvarId
| CheckAssignment.Exception.meta ex => throw ex
namespace CheckAssignmentQuick
@[inline] private def visit (f : Expr → Bool) (e : Expr) : Bool :=
if !e.hasExprMVar && !e.hasFVar then true else f e
partial def check
(hasCtxLocals ctxApprox : Bool)
(mctx : MetavarContext) (lctx : LocalContext) (mvarDecl : MetavarDecl) (mvarId : MVarId) (fvars : Array Expr) : Expr → Bool
| e@(Expr.mdata _ b _) => check b
| e@(Expr.proj _ _ s _) => check s
| e@(Expr.app f a _) => visit check f && visit check a
| e@(Expr.lam _ d b _) => visit check d && visit check b
| e@(Expr.forallE _ d b _) => visit check d && visit check b
| e@(Expr.letE _ t v b _) => visit check t && visit check v && visit check b
| e@(Expr.bvar _ _) => true
| e@(Expr.sort _ _) => true
| e@(Expr.const _ _ _) => true
| e@(Expr.lit _ _) => true
| e@(Expr.fvar fvarId _) =>
if mvarDecl.lctx.contains fvarId then true
else match lctx.find? fvarId with
| some (LocalDecl.ldecl _ _ _ _ v) => false -- need expensive CheckAssignment.check
| _ =>
if fvars.any $ fun x => x.fvarId! == fvarId then true
else false -- We could throw an exception here, but we would have to use ExceptM. So, we let CheckAssignment.check do it
| e@(Expr.mvar mvarId' _) => do
match mctx.getExprAssignment? mvarId' with
| some _ => false -- use CheckAssignment.check to instantiate
| none =>
if mvarId' == mvarId then false -- occurs check failed, use CheckAssignment.check to throw exception
else match mctx.findDecl? mvarId' with
| none => false
| some mvarDecl' =>
if hasCtxLocals then false -- use CheckAssignment.check
else if mvarDecl'.lctx.isSubPrefixOf mvarDecl.lctx fvars then true
else if mvarDecl'.depth != mctx.depth || mvarDecl'.kind.isSyntheticOpaque then false -- use CheckAssignment.check
else if ctxApprox && mvarDecl.lctx.isSubPrefixOf mvarDecl'.lctx then false -- use CheckAssignment.check
else true
| Expr.localE _ _ _ _ => unreachable!
end CheckAssignmentQuick
-- See checkAssignment
def checkAssignmentAux (mvarId : MVarId) (mvarDecl : MetavarDecl) (fvars : Array Expr) (hasCtxLocals : Bool) (v : Expr) : MetaM (Option Expr) :=
fun ctx s =>
let checkCtx : CheckAssignment.Context := {
mvarId := mvarId,
mvarDecl := s.mctx.getDecl mvarId,
fvars := fvars,
hasCtxLocals := hasCtxLocals,
toContext := ctx
};
match (CheckAssignment.check v checkCtx).run { toState := s } with
| EStateM.Result.ok e newS => EStateM.Result.ok (some e) newS.toState
| EStateM.Result.error ex newS => checkAssignmentFailure mvarId fvars v ex ctx newS.toState
/--
Auxiliary function for handling constraints of the form `?m a₁ ... aₙ =?= v`.
It will check whether we can perform the assignment
```
?m := fun fvars => t
```
The result is `none` if the assignment can't be performed.
The result is `some newV` where `newV` is a possibly updated `v`. This method may need
to unfold let-declarations. -/
def checkAssignment (mvarId : MVarId) (fvars : Array Expr) (v : Expr) : MetaM (Option Expr) := do
if !v.hasExprMVar && !v.hasFVar then
pure (some v)
else do
mvarDecl ← getMVarDecl mvarId;
let hasCtxLocals := fvars.any $ fun fvar => mvarDecl.lctx.containsFVar fvar;
ctx ← read;
mctx ← getMCtx;
if CheckAssignmentQuick.check hasCtxLocals ctx.config.ctxApprox mctx ctx.lctx mvarDecl mvarId fvars v then
pure (some v)
else do
v ← instantiateMVars v;
checkAssignmentAux mvarId mvarDecl fvars hasCtxLocals v
private def processAssignmentFOApproxAux (mvar : Expr) (args : Array Expr) (v : Expr) : MetaM Bool :=
match v with
| Expr.app f a _ => isExprDefEqAux args.back a <&&> isExprDefEqAux (mkAppRange mvar 0 (args.size - 1) args) f
| _ => pure false
/-
Auxiliary method for applying first-order unification. It is an approximation.
Remark: this method is trying to solve the unification constraint:
?m a₁ ... aₙ =?= v
It is uses processAssignmentFOApproxAux, if it fails, it tries to unfold `v`.
We have added support for unfolding here because we want to be able to solve unification problems such as
?m Unit =?= ITactic
where `ITactic` is defined as
def ITactic := Tactic Unit
-/
private partial def processAssignmentFOApprox (mvar : Expr) (args : Array Expr) : Expr → MetaM Bool
| v => do
cfg ← getConfig;
if !cfg.foApprox then pure false
else do
trace! `Meta.isDefEq.foApprox (mvar ++ " " ++ args ++ " := " ++ v);
condM (commitWhen $ processAssignmentFOApproxAux mvar args v)
(pure true)
(do v? ← unfoldDefinition? v;
match v? with
| none => pure false
| some v => processAssignmentFOApprox v)
private partial def simpAssignmentArgAux : Expr → MetaM Expr
| Expr.mdata _ e _ => simpAssignmentArgAux e
| e@(Expr.fvar fvarId _) => do
decl ← getLocalDecl fvarId;
match decl.value? with
| some value => simpAssignmentArgAux value
| _ => pure e
| e => pure e
/- Auxiliary procedure for processing `?m a₁ ... aₙ =?= v`.
We apply it to each `aᵢ`. It instantiates assigned metavariables if `aᵢ` is of the form `f[?n] b₁ ... bₘ`,
and then removes metadata, and zeta-expand let-decls. -/
private def simpAssignmentArg (arg : Expr) : MetaM Expr := do
arg ← if arg.getAppFn.hasExprMVar then instantiateMVars arg else pure arg;
simpAssignmentArgAux arg
private def processConstApprox (mvar : Expr) (numArgs : Nat) (v : Expr) : MetaM Bool := do
cfg ← getConfig;
if cfg.constApprox then do
let mvarId := mvar.mvarId!;
v? ← checkAssignment mvarId #[] v;
match v? with
| none => pure false
| some v => do
mvarDecl ← getMVarDecl mvarId;
forallBoundedTelescope mvarDecl.type numArgs $ fun xs _ =>
if xs.size != numArgs then pure false
else do
v ← mkLambda xs v;
checkTypesAndAssign mvar v
else
pure false
private partial def processAssignmentAux (mvar : Expr) (mvarDecl : MetavarDecl) : Nat → Array Expr → Expr → MetaM Bool
| i, args, v => do
cfg ← getConfig;
let useFOApprox (args : Array Expr) : MetaM Bool :=
processAssignmentFOApprox mvar args v <||> processConstApprox mvar args.size v;
if h : i < args.size then do
let arg := args.get ⟨i, h⟩;
arg ← simpAssignmentArg arg;
let args := args.set ⟨i, h⟩ arg;
match arg with
| Expr.fvar fvarId _ =>
if args.anyRange 0 i (fun prevArg => prevArg == arg) then
useFOApprox args
else if mvarDecl.lctx.contains fvarId && !cfg.quasiPatternApprox then
useFOApprox args
else
processAssignmentAux (i+1) args v
| _ =>
useFOApprox args
else do
v ← instantiateMVars v; -- enforce A4
if v.getAppFn == mvar then
-- using A6
useFOApprox args
else do
let mvarId := mvar.mvarId!;
v? ← checkAssignment mvarId args v;
match v? with
| none => useFOApprox args
| some v => do
trace `Meta.isDefEq.assign.beforeMkLambda $ fun _ => mvar ++ " " ++ args ++ " := " ++ v;
v ← mkLambda args v;
if args.any (fun arg => mvarDecl.lctx.containsFVar arg) then
/- We need to type check `v` because abstraction using `mkLambda` may have produced
a type incorrect term. See discussion at A2 -/
condM (isTypeCorrect v)
(checkTypesAndAssign mvar v)
(do trace `Meta.isDefEq.assign.typeError $ fun _ => mvar ++ " := " ++ v;
useFOApprox args)
else
checkTypesAndAssign mvar v
/-- Tries to solve `?m a₁ ... aₙ =?= v` by assigning `?m`.
It assumes `?m` is unassigned. -/
private def processAssignment (mvarApp : Expr) (v : Expr) : MetaM Bool :=
traceCtx `Meta.isDefEq.assign $ do
trace! `Meta.isDefEq.assign (mvarApp ++ " := " ++ v);
let mvar := mvarApp.getAppFn;
mvarDecl ← getMVarDecl mvar.mvarId!;
processAssignmentAux mvar mvarDecl 0 mvarApp.getAppArgs v
private def isDeltaCandidate (t : Expr) : MetaM (Option ConstantInfo) :=
match t.getAppFn with
| Expr.const c _ _ => getConst c
| _ => pure none
/-- Auxiliary method for isDefEqDelta -/
private def isListLevelDefEq (us vs : List Level) : MetaM LBool :=
toLBoolM $ isListLevelDefEqAux us vs
/-- Auxiliary method for isDefEqDelta -/
private def isDefEqLeft (fn : Name) (t s : Expr) : MetaM LBool := do
trace! `Meta.isDefEq.delta.unfoldLeft fn;
toLBoolM $ isExprDefEqAux t s
/-- Auxiliary method for isDefEqDelta -/
private def isDefEqRight (fn : Name) (t s : Expr) : MetaM LBool := do
trace! `Meta.isDefEq.delta.unfoldRight fn;
toLBoolM $ isExprDefEqAux t s
/-- Auxiliary method for isDefEqDelta -/
private def isDefEqLeftRight (fn : Name) (t s : Expr) : MetaM LBool := do
trace! `Meta.isDefEq.delta.unfoldLeftRight fn;
toLBoolM $ isExprDefEqAux t s
/-- Try to solve `f a₁ ... aₙ =?= f b₁ ... bₙ` by solving `a₁ =?= b₁, ..., aₙ =?= bₙ`.
Auxiliary method for isDefEqDelta -/
private def tryHeuristic (t s : Expr) : MetaM Bool :=
let tFn := t.getAppFn;
let sFn := s.getAppFn;
traceCtx `Meta.isDefEq.delta $
commitWhen $ do
b ← isDefEqArgs tFn t.getAppArgs s.getAppArgs
<&&>
isListLevelDefEqAux tFn.constLevels! sFn.constLevels!;
unless b $ trace! `Meta.isDefEq.delta ("heuristic failed " ++ t ++ " =?= " ++ s);
pure b
/-- Auxiliary method for isDefEqDelta -/
private abbrev unfold {α} (e : Expr) (failK : MetaM α) (successK : Expr → MetaM α) : MetaM α := do
e? ← unfoldDefinition? e;
match e? with
| some e => successK e
| none => failK
/-- Auxiliary method for isDefEqDelta -/
private def unfoldBothDefEq (fn : Name) (t s : Expr) : MetaM LBool :=
match t, s with
| Expr.const _ ls₁ _, Expr.const _ ls₂ _ => isListLevelDefEq ls₁ ls₂
| Expr.app _ _ _, Expr.app _ _ _ =>
condM (tryHeuristic t s)
(pure LBool.true)
(unfold t
(unfold s (pure LBool.false) (fun s => isDefEqRight fn t s))
(fun t => unfold s (isDefEqLeft fn t s) (fun s => isDefEqLeftRight fn t s)))
| _, _ => pure LBool.false
private def sameHeadSymbol (t s : Expr) : Bool :=
match t.getAppFn, s.getAppFn with
| Expr.const c₁ _ _, Expr.const c₂ _ _ => true
| _, _ => false
/--
- If headSymbol (unfold t) == headSymbol s, then unfold t
- If headSymbol (unfold s) == headSymbol t, then unfold s
- Otherwise unfold t and s if possible.
Auxiliary method for isDefEqDelta -/
private def unfoldComparingHeadsDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool :=
unfold t
(unfold s
(pure LBool.undef) -- `t` and `s` failed to be unfolded
(fun s => isDefEqRight sInfo.name t s))
(fun tNew =>
if sameHeadSymbol tNew s then
isDefEqLeft tInfo.name tNew s
else
unfold s
(isDefEqLeft tInfo.name tNew s)
(fun sNew =>
if sameHeadSymbol t sNew then
isDefEqRight sInfo.name t sNew
else
isDefEqLeftRight tInfo.name tNew sNew))
/-- If `t` and `s` do not contain metavariables, then use
kernel definitional equality heuristics.
Otherwise, use `unfoldComparingHeadsDefEq`.
Auxiliary method for isDefEqDelta -/
private def unfoldDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool :=
if !t.hasExprMVar && !s.hasExprMVar then
/- If `t` and `s` do not contain metavariables,
we simulate strategy used in the kernel. -/
if tInfo.hints.lt sInfo.hints then
unfold t (unfoldComparingHeadsDefEq tInfo sInfo t s) $ fun t => isDefEqLeft tInfo.name t s
else if sInfo.hints.lt tInfo.hints then
unfold s (unfoldComparingHeadsDefEq tInfo sInfo t s) $ fun s => isDefEqRight sInfo.name t s
else
unfoldComparingHeadsDefEq tInfo sInfo t s
else
unfoldComparingHeadsDefEq tInfo sInfo t s
/--
When `TransparencyMode` is set to `default` or `all`.
If `t` is reducible and `s` is not ==> `isDefEqLeft (unfold t) s`
If `s` is reducible and `t` is not ==> `isDefEqRight t (unfold s)`
Otherwise, use `unfoldDefEq`
Auxiliary method for isDefEqDelta -/
private def unfoldReducibeDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool :=
condM shouldReduceReducibleOnly
(unfoldDefEq tInfo sInfo t s)
(do tReducible ← isReducible tInfo.name;
sReducible ← isReducible sInfo.name;
if tReducible && !sReducible then
unfold t (unfoldDefEq tInfo sInfo t s) $ fun t => isDefEqLeft tInfo.name t s
else if !tReducible && sReducible then
unfold s (unfoldDefEq tInfo sInfo t s) $ fun s => isDefEqRight sInfo.name t s
else
unfoldDefEq tInfo sInfo t s)
/--
If `t` is a projection function application and `s` is not ==> `isDefEqRight t (unfold s)`
If `s` is a projection function application and `t` is not ==> `isDefEqRight (unfold t) s`
Otherwise, use `unfoldReducibeDefEq`
Auxiliary method for isDefEqDelta -/
private def unfoldNonProjFnDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool := do
env ← getEnv;
let tProj? := env.isProjectionFn tInfo.name;
let sProj? := env.isProjectionFn sInfo.name;
if tProj? && !sProj? then
unfold s (unfoldDefEq tInfo sInfo t s) $ fun s => isDefEqRight sInfo.name t s
else if !tProj? && sProj? then
unfold t (unfoldDefEq tInfo sInfo t s) $ fun t => isDefEqLeft tInfo.name t s
else
unfoldReducibeDefEq tInfo sInfo t s
/--
isDefEq by lazy delta reduction.
This method implements many different heuristics:
1- If only `t` can be unfolded => then unfold `t` and continue
2- If only `s` can be unfolded => then unfold `s` and continue
3- If `t` and `s` can be unfolded and they have the same head symbol, then
a) First try to solve unification by unifying arguments.
b) If it fails, unfold both and continue.
Implemented by `unfoldBothDefEq`
4- If `t` is a projection function application and `s` is not => then unfold `s` and continue.
5- If `s` is a projection function application and `t` is not => then unfold `t` and continue.
Remark: 4&5 are implemented by `unfoldNonProjFnDefEq`
6- If `t` is reducible and `s` is not => then unfold `t` and continue.
7- If `s` is reducible and `t` is not => then unfold `s` and continue
Remark: 6&7 are implemented by `unfoldReducibeDefEq`
8- If `t` and `s` do not contain metavariables, then use heuristic used in the Kernel.
Implemented by `unfoldDefEq`
9- If `headSymbol (unfold t) == headSymbol s`, then unfold t and continue.
10- If `headSymbol (unfold s) == headSymbol t`, then unfold s
11- Otherwise, unfold `t` and `s` and continue.
Remark: 9&10&11 are implemented by `unfoldComparingHeadsDefEq` -/
private def isDefEqDelta (t s : Expr) : MetaM LBool := do
tInfo? ← isDeltaCandidate t.getAppFn;
sInfo? ← isDeltaCandidate s.getAppFn;
match tInfo?, sInfo? with
| none, none => pure LBool.undef
| some tInfo, none => unfold t (pure LBool.undef) $ fun t => isDefEqLeft tInfo.name t s
| none, some sInfo => unfold s (pure LBool.undef) $ fun s => isDefEqRight sInfo.name t s
| some tInfo, some sInfo =>
if tInfo.name == sInfo.name then
unfoldBothDefEq tInfo.name t s
else
unfoldNonProjFnDefEq tInfo sInfo t s
private def isAssigned : Expr → MetaM Bool
| Expr.mvar mvarId _ => isExprMVarAssigned mvarId
| _ => pure false
private def isDelayedAssignedHead (tFn : Expr) (t : Expr) : MetaM Bool :=
match tFn with
| Expr.mvar mvarId _ => do
condM (isDelayedAssigned mvarId)
(do tNew ← instantiateMVars t;
pure $ tNew != t)
(pure false)
| _ => pure false
private def isSynthetic : Expr → MetaM Bool
| Expr.mvar mvarId _ => do
mvarDecl ← getMVarDecl mvarId;
match mvarDecl.kind with
| MetavarKind.synthetic => pure true
| MetavarKind.syntheticOpaque => pure true
| MetavarKind.natural => pure false
| _ => pure false
private def isAssignable : Expr → MetaM Bool
| Expr.mvar mvarId _ => do b ← isReadOnlyOrSyntheticOpaqueExprMVar mvarId; pure (!b)
| _ => pure false
private def etaEq (t s : Expr) : Bool :=
match t.etaExpanded? with
| some t => t == s
| none => false
private def isLetFVar (fvarId : FVarId) : MetaM Bool := do
decl ← getLocalDecl fvarId;
pure decl.isLet
private partial def isDefEqQuick : Expr → Expr → MetaM LBool
| Expr.lit l₁ _, Expr.lit l₂ _ => pure (l₁ == l₂).toLBool
| Expr.sort u _, Expr.sort v _ => toLBoolM $ isLevelDefEqAux u v
| t@(Expr.lam _ _ _ _), s@(Expr.lam _ _ _ _) => if t == s then pure LBool.true else toLBoolM $ isDefEqBinding t s
| t@(Expr.forallE _ _ _ _), s@(Expr.forallE _ _ _ _) => if t == s then pure LBool.true else toLBoolM $ isDefEqBinding t s
| Expr.mdata _ t _, s => isDefEqQuick t s
| t, Expr.mdata _ s _ => isDefEqQuick t s
| Expr.fvar fvarId₁ _, Expr.fvar fvarId₂ _ =>
condM (isLetFVar fvarId₁ <||> isLetFVar fvarId₂)
(pure LBool.undef)
(pure (fvarId₁ == fvarId₂).toLBool)
| t, s =>
cond (t == s) (pure LBool.true) $
cond (etaEq t s || etaEq s t) (pure LBool.true) $ -- t =?= (fun xs => t xs)
let tFn := t.getAppFn;
let sFn := s.getAppFn;
cond (!tFn.isMVar && !sFn.isMVar) (pure LBool.undef) $
condM (isAssigned tFn) (do t ← instantiateMVars t; isDefEqQuick t s) $
condM (isAssigned sFn) (do s ← instantiateMVars s; isDefEqQuick t s) $
condM (isDelayedAssignedHead tFn t) (do t ← instantiateMVars t; isDefEqQuick t s) $
condM (isDelayedAssignedHead sFn s) (do s ← instantiateMVars s; isDefEqQuick t s) $
condM (isSynthetic tFn <&&> trySynthPending tFn) (do t ← instantiateMVars t; isDefEqQuick t s) $
condM (isSynthetic sFn <&&> trySynthPending sFn) (do s ← instantiateMVars s; isDefEqQuick t s) $ do
tAssign? ← isAssignable tFn;
sAssign? ← isAssignable sFn;
trace! `Meta.isDefEq
(t ++ (if tAssign? then " [assignable]" else " [nonassignable]") ++ " =?= " ++ s ++ (if sAssign? then " [assignable]" else " [nonassignable]"));
let assign (t s : Expr) : MetaM LBool := toLBoolM $ processAssignment t s;
cond (tAssign? && !sAssign?) (assign t s) $
cond (!tAssign? && sAssign?) (assign s t) $
cond (!tAssign? && !sAssign?)
(if tFn.isMVar || sFn.isMVar then do
ctx ← read;
if ctx.config.isDefEqStuckEx then do
trace! `Meta.isDefEq.stuck (t ++ " =?= " ++ s);
throwEx $ Exception.isExprDefEqStuck t s
else pure LBool.false
else pure LBool.undef) $ do
-- Both `t` and `s` are terms of the form `?m ...`
tMVarDecl ← getMVarDecl tFn.mvarId!;
sMVarDecl ← getMVarDecl sFn.mvarId!;
if s.isMVar && !t.isMVar then
/- Solve `?m t =?= ?n` by trying first `?n := ?m t`.
Reason: this assignment is precise. -/
condM (commitWhen (processAssignment s t)) (pure LBool.true) $
assign t s
else
condM (commitWhen (processAssignment t s)) (pure LBool.true) $
assign s t
private def isDefEqProofIrrel (t s : Expr) : MetaM LBool := do
status ← isProofQuick t;
match status with
| LBool.false =>
pure LBool.undef
| LBool.true => do
tType ← inferType t;
sType ← inferType s;
toLBoolM $ isExprDefEqAux tType sType
| LBool.undef => do
tType ← inferType t;
condM (isProp tType)
(do sType ← inferType s; toLBoolM $ isExprDefEqAux tType sType)
(pure LBool.undef)
@[inline] def whenUndefDo (x : MetaM LBool) (k : MetaM Bool) : MetaM Bool := do
status ← x;
match status with
| LBool.true => pure true
| LBool.false => pure false
| LBool.undef => k
@[specialize] private partial def isDefEqWHNF
(t s : Expr)
(k : Expr → Expr → MetaM Bool) : MetaM Bool := do
t' ← whnfCore t;
s' ← whnfCore s;
if t == t' && s == s' then
k t' s'
else
whenUndefDo (isDefEqQuick t' s') $
k t' s'
@[specialize] private def unstuckMVar
(e : Expr)
(successK : Expr → MetaM Bool) (failK : MetaM Bool): MetaM Bool := do
mvarId? ← getStuckMVar? e;
match mvarId? with
| some mvarId =>
condM (synthPending mvarId)
(do e ← instantiateMVars e; successK e)
failK
| none => failK
private def isDefEqOnFailure (t s : Expr) : MetaM Bool :=
unstuckMVar t (fun t => isExprDefEqAux t s) $
unstuckMVar s (fun s => isExprDefEqAux t s) $
pure false
/- Remove unnecessary let-decls -/
private def consumeLet : Expr → Expr
| e@(Expr.letE _ _ _ b _) => if b.hasLooseBVars then e else consumeLet b
| e => e
partial def isExprDefEqAuxImpl : Expr → Expr → MetaM Bool
| t, s => do
let t := consumeLet t;
let s := consumeLet s;
trace `Meta.isDefEq.step $ fun _ => t ++ " =?= " ++ s;
whenUndefDo (isDefEqQuick t s) $
whenUndefDo (isDefEqProofIrrel t s) $
isDefEqWHNF t s $ fun t s => do
condM (isDefEqEta t s <||> isDefEqEta s t) (pure true) $
whenUndefDo (isDefEqNative t s) $ do
whenUndefDo (isDefEqNat t s) $ do
whenUndefDo (isDefEqOffset t s) $ do
whenUndefDo (isDefEqDelta t s) $
match t, s with
| Expr.const c us _, Expr.const d vs _ => if c == d then isListLevelDefEqAux us vs else pure false
| Expr.app _ _ _, Expr.app _ _ _ =>
let tFn := t.getAppFn;
condM (commitWhen (isExprDefEqAux tFn s.getAppFn <&&> isDefEqArgs tFn t.getAppArgs s.getAppArgs))
(pure true)
(isDefEqOnFailure t s)
| _, _ =>
whenUndefDo (isDefEqStringLit t s) $
isDefEqOnFailure t s
@[init] def setIsExprDefEqAuxRef : IO Unit :=
isExprDefEqAuxRef.set isExprDefEqAuxImpl
@[init] private def regTraceClasses : IO Unit := do
registerTraceClass `Meta.isDefEq;
registerTraceClass `Meta.isDefEq.foApprox;
registerTraceClass `Meta.isDefEq.delta;
registerTraceClass `Meta.isDefEq.step;
registerTraceClass `Meta.isDefEq.assign
end Meta
end Lean
|
195421f47000c1d1c7fa77f38af1568f29dbac2e | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/field_theory/perfect_closure.lean | 4543e789eb773afeabf51e2a138955d068f4896b | [
"Apache-2.0"
] | permissive | abentkamp/mathlib | d9a75d291ec09f4637b0f30cc3880ffb07549ee5 | 5360e476391508e092b5a1e5210bd0ed22dc0755 | refs/heads/master | 1,682,382,954,948 | 1,622,106,077,000 | 1,622,106,077,000 | 149,285,665 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 17,399 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Yury Kudryashov
-/
import algebra.char_p.basic
import data.equiv.ring
import algebra.group_with_zero.power
import algebra.iterate_hom
/-!
# The perfect closure of a field
-/
universes u v
open function
section defs
variables (R : Type u) [comm_semiring R] (p : ℕ) [fact p.prime] [char_p R p]
/-- A perfect ring is a ring of characteristic p that has p-th root. -/
class perfect_ring : Type u :=
(pth_root' : R → R)
(frobenius_pth_root' : ∀ x, frobenius R p (pth_root' x) = x)
(pth_root_frobenius' : ∀ x, pth_root' (frobenius R p x) = x)
/-- Frobenius automorphism of a perfect ring. -/
def frobenius_equiv [perfect_ring R p] : R ≃+* R :=
{ inv_fun := perfect_ring.pth_root' p,
left_inv := perfect_ring.pth_root_frobenius',
right_inv := perfect_ring.frobenius_pth_root',
.. frobenius R p }
/-- `p`-th root of an element in a `perfect_ring` as a `ring_hom`. -/
def pth_root [perfect_ring R p] : R →+* R :=
(frobenius_equiv R p).symm
end defs
section
variables {R : Type u} [comm_semiring R] {S : Type v} [comm_semiring S] (f : R →* S) (g : R →+* S)
{p : ℕ} [fact p.prime] [char_p R p] [perfect_ring R p] [char_p S p] [perfect_ring S p]
@[simp] lemma coe_frobenius_equiv : ⇑(frobenius_equiv R p) = frobenius R p := rfl
@[simp] lemma coe_frobenius_equiv_symm : ⇑(frobenius_equiv R p).symm = pth_root R p := rfl
@[simp] theorem frobenius_pth_root (x : R) : frobenius R p (pth_root R p x) = x :=
(frobenius_equiv R p).apply_symm_apply x
@[simp] theorem pth_root_pow_p (x : R) : pth_root R p x ^ p = x :=
frobenius_pth_root x
@[simp] theorem pth_root_frobenius (x : R) : pth_root R p (frobenius R p x) = x :=
(frobenius_equiv R p).symm_apply_apply x
@[simp] theorem pth_root_pow_p' (x : R) : pth_root R p (x ^ p) = x :=
pth_root_frobenius x
theorem left_inverse_pth_root_frobenius : left_inverse (pth_root R p) (frobenius R p) :=
pth_root_frobenius
theorem right_inverse_pth_root_frobenius : function.right_inverse (pth_root R p) (frobenius R p) :=
frobenius_pth_root
theorem commute_frobenius_pth_root : function.commute (frobenius R p) (pth_root R p) :=
λ x, (frobenius_pth_root x).trans (pth_root_frobenius x).symm
theorem eq_pth_root_iff {x y : R} : x = pth_root R p y ↔ frobenius R p x = y :=
(frobenius_equiv R p).to_equiv.eq_symm_apply
theorem pth_root_eq_iff {x y : R} : pth_root R p x = y ↔ x = frobenius R p y :=
(frobenius_equiv R p).to_equiv.symm_apply_eq
theorem monoid_hom.map_pth_root (x : R) : f (pth_root R p x) = pth_root S p (f x) :=
eq_pth_root_iff.2 $ by rw [← f.map_frobenius, frobenius_pth_root]
theorem monoid_hom.map_iterate_pth_root (x : R) (n : ℕ) :
f (pth_root R p^[n] x) = (pth_root S p^[n] (f x)) :=
semiconj.iterate_right f.map_pth_root n x
theorem ring_hom.map_pth_root (x : R) :
g (pth_root R p x) = pth_root S p (g x) :=
g.to_monoid_hom.map_pth_root x
theorem ring_hom.map_iterate_pth_root (x : R) (n : ℕ) :
g (pth_root R p^[n] x) = (pth_root S p^[n] (g x)) :=
g.to_monoid_hom.map_iterate_pth_root x n
variables (p)
lemma injective_pow_p {x y : R} (hxy : x ^ p = y ^ p) : x = y :=
left_inverse_pth_root_frobenius.injective hxy
end
section
variables (K : Type u) [comm_ring K] (p : ℕ) [fact p.prime] [char_p K p]
/-- `perfect_closure K p` is the quotient by this relation. -/
@[mk_iff] inductive perfect_closure.r : (ℕ × K) → (ℕ × K) → Prop
| intro : ∀ n x, perfect_closure.r (n, x) (n+1, frobenius K p x)
/-- The perfect closure is the smallest extension that makes frobenius surjective. -/
def perfect_closure : Type u := quot (perfect_closure.r K p)
end
namespace perfect_closure
variables (K : Type u)
section ring
variables [comm_ring K] (p : ℕ) [fact p.prime] [char_p K p]
/-- Constructor for `perfect_closure`. -/
def mk (x : ℕ × K) : perfect_closure K p := quot.mk (r K p) x
@[simp] lemma quot_mk_eq_mk (x : ℕ × K) :
(quot.mk (r K p) x : perfect_closure K p) = mk K p x := rfl
variables {K p}
/-- Lift a function `ℕ × K → L` to a function on `perfect_closure K p`. -/
@[elab_as_eliminator]
def lift_on {L : Type*} (x : perfect_closure K p) (f : ℕ × K → L)
(hf : ∀ x y, r K p x y → f x = f y) : L :=
quot.lift_on x f hf
@[simp] lemma lift_on_mk {L : Sort*} (f : ℕ × K → L)
(hf : ∀ x y, r K p x y → f x = f y) (x : ℕ × K) :
(mk K p x).lift_on f hf = f x :=
rfl
@[elab_as_eliminator]
lemma induction_on (x : perfect_closure K p) {q : perfect_closure K p → Prop}
(h : ∀ x, q (mk K p x)) : q x :=
quot.induction_on x h
variables (K p)
private lemma mul_aux_left (x1 x2 y : ℕ × K) (H : r K p x1 x2) :
mk K p (x1.1 + y.1, ((frobenius K p)^[y.1] x1.2) * ((frobenius K p)^[x1.1] y.2)) =
mk K p (x2.1 + y.1, ((frobenius K p)^[y.1] x2.2) * ((frobenius K p)^[x2.1] y.2)) :=
match x1, x2, H with
| _, _, r.intro n x := quot.sound $ by rw [← iterate_succ_apply, iterate_succ',
iterate_succ', ← frobenius_mul, nat.succ_add]; apply r.intro
end
private lemma mul_aux_right (x y1 y2 : ℕ × K) (H : r K p y1 y2) :
mk K p (x.1 + y1.1, ((frobenius K p)^[y1.1] x.2) * ((frobenius K p)^[x.1] y1.2)) =
mk K p (x.1 + y2.1, ((frobenius K p)^[y2.1] x.2) * ((frobenius K p)^[x.1] y2.2)) :=
match y1, y2, H with
| _, _, r.intro n y := quot.sound $ by rw [← iterate_succ_apply, iterate_succ',
iterate_succ', ← frobenius_mul]; apply r.intro
end
instance : has_mul (perfect_closure K p) :=
⟨quot.lift (λ x:ℕ×K, quot.lift (λ y:ℕ×K, mk K p
(x.1 + y.1, ((frobenius K p)^[y.1] x.2) * ((frobenius K p)^[x.1] y.2))) (mul_aux_right K p x))
(λ x1 x2 (H : r K p x1 x2), funext $ λ e, quot.induction_on e $ λ y,
mul_aux_left K p x1 x2 y H)⟩
@[simp] lemma mk_mul_mk (x y : ℕ × K) :
mk K p x * mk K p y = mk K p
(x.1 + y.1, ((frobenius K p)^[y.1] x.2) * ((frobenius K p)^[x.1] y.2)) :=
rfl
instance : comm_monoid (perfect_closure K p) :=
{ mul_assoc := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩,
quot.induction_on g $ λ ⟨s, z⟩, congr_arg (quot.mk _) $
by simp only [add_assoc, mul_assoc, ring_hom.iterate_map_mul,
← iterate_add_apply, add_comm, add_left_comm],
one := mk K p (0, 1),
one_mul := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $
by simp only [ring_hom.iterate_map_one, iterate_zero_apply, one_mul, zero_add]),
mul_one := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $
by simp only [ring_hom.iterate_map_one, iterate_zero_apply, mul_one, add_zero]),
mul_comm := λ e f, quot.induction_on e (λ ⟨m, x⟩, quot.induction_on f (λ ⟨n, y⟩,
congr_arg (quot.mk _) $ by simp only [add_comm, mul_comm])),
.. (infer_instance : has_mul (perfect_closure K p)) }
lemma one_def : (1 : perfect_closure K p) = mk K p (0, 1) := rfl
instance : inhabited (perfect_closure K p) := ⟨1⟩
private lemma add_aux_left (x1 x2 y : ℕ × K) (H : r K p x1 x2) :
mk K p (x1.1 + y.1, ((frobenius K p)^[y.1] x1.2) + ((frobenius K p)^[x1.1] y.2)) =
mk K p (x2.1 + y.1, ((frobenius K p)^[y.1] x2.2) + ((frobenius K p)^[x2.1] y.2)) :=
match x1, x2, H with
| _, _, r.intro n x := quot.sound $ by rw [← iterate_succ_apply, iterate_succ',
iterate_succ', ← frobenius_add, nat.succ_add]; apply r.intro
end
private lemma add_aux_right (x y1 y2 : ℕ × K) (H : r K p y1 y2) :
mk K p (x.1 + y1.1, ((frobenius K p)^[y1.1] x.2) + ((frobenius K p)^[x.1] y1.2)) =
mk K p (x.1 + y2.1, ((frobenius K p)^[y2.1] x.2) + ((frobenius K p)^[x.1] y2.2)) :=
match y1, y2, H with
| _, _, r.intro n y := quot.sound $ by rw [← iterate_succ_apply, iterate_succ',
iterate_succ', ← frobenius_add]; apply r.intro
end
instance : has_add (perfect_closure K p) :=
⟨quot.lift (λ x:ℕ×K, quot.lift (λ y:ℕ×K, mk K p
(x.1 + y.1, ((frobenius K p)^[y.1] x.2) + ((frobenius K p)^[x.1] y.2))) (add_aux_right K p x))
(λ x1 x2 (H : r K p x1 x2), funext $ λ e, quot.induction_on e $ λ y,
add_aux_left K p x1 x2 y H)⟩
@[simp] lemma mk_add_mk (x y : ℕ × K) :
mk K p x + mk K p y =
mk K p (x.1 + y.1, ((frobenius K p)^[y.1] x.2) + ((frobenius K p)^[x.1] y.2)) := rfl
instance : has_neg (perfect_closure K p) :=
⟨quot.lift (λ x:ℕ×K, mk K p (x.1, -x.2)) (λ x y (H : r K p x y), match x, y, H with
| _, _, r.intro n x := quot.sound $ by rw ← frobenius_neg; apply r.intro
end)⟩
@[simp] lemma neg_mk (x : ℕ × K) : - mk K p x = mk K p (x.1, -x.2) := rfl
instance : has_zero (perfect_closure K p) := ⟨mk K p (0, 0)⟩
lemma zero_def : (0 : perfect_closure K p) = mk K p (0, 0) := rfl
theorem mk_zero (n : ℕ) : mk K p (n, 0) = 0 :=
by induction n with n ih; [refl, rw ← ih]; symmetry; apply quot.sound;
have := r.intro n (0:K); rwa [frobenius_zero K p] at this
theorem r.sound (m n : ℕ) (x y : K) (H : frobenius K p^[m] x = y) :
mk K p (n, x) = mk K p (m + n, y) :=
by subst H; induction m with m ih; [simp only [zero_add, iterate_zero_apply],
rw [ih, nat.succ_add, iterate_succ']]; apply quot.sound; apply r.intro
instance : comm_ring (perfect_closure K p) :=
{ add_assoc := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩,
quot.induction_on g $ λ ⟨s, z⟩, congr_arg (quot.mk _) $
by simp only [add_assoc, ring_hom.iterate_map_add,
← iterate_add_apply, add_comm, add_left_comm],
zero := 0,
zero_add := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $
by simp only [ring_hom.iterate_map_zero, iterate_zero_apply, zero_add]),
add_zero := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $
by simp only [ring_hom.iterate_map_zero, iterate_zero_apply, add_zero]),
sub_eq_add_neg := λ a b, rfl,
add_left_neg := λ e, by exact quot.induction_on e (λ ⟨n, x⟩,
by simp only [quot_mk_eq_mk, neg_mk, mk_add_mk,
ring_hom.iterate_map_neg, add_left_neg, mk_zero]),
add_comm := λ e f, quot.induction_on e (λ ⟨m, x⟩, quot.induction_on f (λ ⟨n, y⟩,
congr_arg (quot.mk _) $ by simp only [add_comm])),
left_distrib := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩,
quot.induction_on g $ λ ⟨s, z⟩, show quot.mk _ _ = quot.mk _ _,
by simp only [add_assoc, add_comm, add_left_comm]; apply r.sound;
simp only [ring_hom.iterate_map_mul, ring_hom.iterate_map_add,
← iterate_add_apply, mul_add, add_comm, add_left_comm],
right_distrib := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩,
quot.induction_on g $ λ ⟨s, z⟩, show quot.mk _ _ = quot.mk _ _,
by simp only [add_assoc, add_comm _ s, add_left_comm _ s]; apply r.sound;
simp only [ring_hom.iterate_map_mul, ring_hom.iterate_map_add,
← iterate_add_apply, add_mul, add_comm, add_left_comm],
.. (infer_instance : has_add (perfect_closure K p)),
.. (infer_instance : has_neg (perfect_closure K p)),
.. (infer_instance : comm_monoid (perfect_closure K p)) }
theorem eq_iff' (x y : ℕ × K) : mk K p x = mk K p y ↔
∃ z, (frobenius K p^[y.1 + z] x.2) = (frobenius K p^[x.1 + z] y.2) :=
begin
split,
{ intro H,
replace H := quot.exact _ H,
induction H,
case eqv_gen.rel : x y H
{ cases H with n x, exact ⟨0, rfl⟩ },
case eqv_gen.refl : H
{ exact ⟨0, rfl⟩ },
case eqv_gen.symm : x y H ih
{ cases ih with w ih, exact ⟨w, ih.symm⟩ },
case eqv_gen.trans : x y z H1 H2 ih1 ih2
{ cases ih1 with z1 ih1,
cases ih2 with z2 ih2,
existsi z2+(y.1+z1),
rw [← add_assoc, iterate_add_apply, ih1],
rw [← iterate_add_apply, add_comm, iterate_add_apply, ih2],
rw [← iterate_add_apply],
simp only [add_comm, add_left_comm] } },
intro H,
cases x with m x,
cases y with n y,
cases H with z H, dsimp only at H,
rw [r.sound K p (n+z) m x _ rfl, r.sound K p (m+z) n y _ rfl, H],
rw [add_assoc, add_comm, add_comm z]
end
theorem nat_cast (n x : ℕ) : (x : perfect_closure K p) = mk K p (n, x) :=
begin
induction n with n ih,
{ induction x with x ih, {refl},
rw [nat.cast_succ, nat.cast_succ, ih], refl },
rw ih, apply quot.sound,
conv {congr, skip, skip, rw ← frobenius_nat_cast K p x},
apply r.intro
end
theorem int_cast (x : ℤ) : (x : perfect_closure K p) = mk K p (0, x) :=
by induction x; simp only [int.cast_of_nat, int.cast_neg_succ_of_nat, nat_cast K p 0]; refl
theorem nat_cast_eq_iff (x y : ℕ) : (x : perfect_closure K p) = y ↔ (x : K) = y :=
begin
split; intro H,
{ rw [nat_cast K p 0, nat_cast K p 0, eq_iff'] at H,
cases H with z H,
simpa only [zero_add, iterate_fixed (frobenius_nat_cast K p _)] using H },
rw [nat_cast K p 0, nat_cast K p 0, H]
end
instance : char_p (perfect_closure K p) p :=
begin
constructor, intro x, rw ← char_p.cast_eq_zero_iff K,
rw [← nat.cast_zero, nat_cast_eq_iff, nat.cast_zero]
end
theorem frobenius_mk (x : ℕ × K) :
(frobenius (perfect_closure K p) p : perfect_closure K p → perfect_closure K p)
(mk K p x) = mk _ _ (x.1, x.2^p) :=
begin
simp only [frobenius_def], cases x with n x, dsimp only,
suffices : ∀ p':ℕ, mk K p (n, x) ^ p' = mk K p (n, x ^ p'),
{ apply this },
intro p, induction p with p ih,
case nat.zero { apply r.sound, rw [(frobenius _ _).iterate_map_one, pow_zero] },
case nat.succ {
rw [pow_succ, ih],
symmetry,
apply r.sound,
simp only [pow_succ, (frobenius _ _).iterate_map_mul]
}
end
/-- Embedding of `K` into `perfect_closure K p` -/
def of : K →+* perfect_closure K p :=
{ to_fun := λ x, mk _ _ (0, x),
map_one' := rfl,
map_mul' := λ x y, rfl,
map_zero' := rfl,
map_add' := λ x y, rfl }
lemma of_apply (x : K) : of K p x = mk _ _ (0, x) := rfl
end ring
theorem eq_iff [integral_domain K] (p : ℕ) [fact p.prime] [char_p K p]
(x y : ℕ × K) : quot.mk (r K p) x = quot.mk (r K p) y ↔
(frobenius K p^[y.1] x.2) = (frobenius K p^[x.1] y.2) :=
(eq_iff' K p x y).trans ⟨λ ⟨z, H⟩, (frobenius_inj K p).iterate z $
by simpa only [add_comm, iterate_add] using H,
λ H, ⟨0, H⟩⟩
section field
variables [field K] (p : ℕ) [fact p.prime] [char_p K p]
instance : has_inv (perfect_closure K p) :=
⟨quot.lift (λ x:ℕ×K, quot.mk (r K p) (x.1, x.2⁻¹)) (λ x y (H : r K p x y), match x, y, H with
| _, _, r.intro n x := quot.sound $ by { simp only [frobenius_def], rw ← inv_pow', apply r.intro }
end)⟩
instance : field (perfect_closure K p) :=
{ exists_pair_ne := ⟨0, 1, λ H, zero_ne_one ((eq_iff _ _ _ _).1 H)⟩,
mul_inv_cancel := λ e, induction_on e $ λ ⟨m, x⟩ H,
have _ := mt (eq_iff _ _ _ _).2 H, (eq_iff _ _ _ _).2
(by simp only [(frobenius _ _).iterate_map_one, (frobenius K p).iterate_map_zero,
iterate_zero_apply, ← (frobenius _ p).iterate_map_mul] at this ⊢;
rw [mul_inv_cancel this, (frobenius _ _).iterate_map_one]),
inv_zero := congr_arg (quot.mk (r K p)) (by rw [inv_zero]),
.. (infer_instance : has_inv (perfect_closure K p)),
.. (infer_instance : comm_ring (perfect_closure K p)) }
instance : perfect_ring (perfect_closure K p) p :=
{ pth_root' := λ e, lift_on e (λ x, mk K p (x.1 + 1, x.2)) (λ x y H,
match x, y, H with
| _, _, r.intro n x := quot.sound (r.intro _ _)
end),
frobenius_pth_root' := λ e, induction_on e (λ ⟨n, x⟩,
by { simp only [lift_on_mk, frobenius_mk], exact (quot.sound $ r.intro _ _).symm }),
pth_root_frobenius' := λ e, induction_on e (λ ⟨n, x⟩,
by { simp only [lift_on_mk, frobenius_mk], exact (quot.sound $ r.intro _ _).symm }) }
theorem eq_pth_root (x : ℕ × K) :
mk K p x = (pth_root (perfect_closure K p) p^[x.1] (of K p x.2)) :=
begin
rcases x with ⟨m, x⟩,
induction m with m ih, {refl},
rw [iterate_succ_apply', ← ih]; refl
end
/-- Given a field `K` of characteristic `p` and a perfect ring `L` of the same characteristic,
any homomorphism `K →+* L` can be lifted to `perfect_closure K p`. -/
def lift (L : Type v) [comm_semiring L] [char_p L p] [perfect_ring L p] :
(K →+* L) ≃ (perfect_closure K p →+* L) :=
begin
have := left_inverse_pth_root_frobenius.iterate,
refine_struct { .. },
field to_fun { intro f,
refine_struct { .. },
field to_fun { refine λ e, lift_on e (λ x, pth_root L p^[x.1] (f x.2)) _,
rintro a b ⟨n⟩,
simp only [f.map_frobenius, iterate_succ_apply, pth_root_frobenius] },
field map_one' { exact f.map_one },
field map_zero' { exact f.map_zero },
field map_mul' { rintro ⟨x⟩ ⟨y⟩,
simp only [quot_mk_eq_mk, lift_on_mk, mk_mul_mk, ring_hom.map_iterate_frobenius,
ring_hom.iterate_map_mul, ring_hom.map_mul],
rw [iterate_add_apply, this _ _, add_comm, iterate_add_apply, this _ _] },
field map_add' { rintro ⟨x⟩ ⟨y⟩,
simp only [quot_mk_eq_mk, lift_on_mk, mk_add_mk, ring_hom.map_iterate_frobenius,
ring_hom.iterate_map_add, ring_hom.map_add],
rw [iterate_add_apply, this _ _, add_comm x.1, iterate_add_apply, this _ _] } },
field inv_fun { exact λ f, f.comp (of K p) },
field left_inv { intro f, ext x, refl },
field right_inv { intro f, ext ⟨x⟩,
simp only [ring_hom.coe_mk, quot_mk_eq_mk, ring_hom.comp_apply, lift_on_mk],
rw [eq_pth_root, ring_hom.map_iterate_pth_root] }
end
end field
end perfect_closure
|
b183d9c74772998d29a93b42dca16b0a202d1d7a | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/data/real/irrational.lean | ebd9346a16ec08c4b518edfe65ba7c7ab37e072e | [
"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 | 9,829 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Yury Kudryashov.
-/
import data.real.basic
import data.rat.sqrt
import algebra.gcd_monoid
import ring_theory.multiplicity
import data.polynomial.eval
import data.polynomial.degree
import tactic.interval_cases
/-!
# Irrational real numbers
In this file we define a predicate `irrational` on `ℝ`, prove that the `n`-th root of an integer
number is irrational if it is not integer, and that `sqrt q` is irrational if and only if
`rat.sqrt q * rat.sqrt q ≠ q ∧ 0 ≤ q`.
We also provide dot-style constructors like `irrational.add_rat`, `irrational.rat_sub` etc.
-/
open rat real multiplicity
/-- A real number is irrational if it is not equal to any rational number. -/
def irrational (x : ℝ) := x ∉ set.range (coe : ℚ → ℝ)
lemma irrational_iff_ne_rational (x : ℝ) : irrational x ↔ ∀ a b : ℤ, x ≠ a / b :=
by simp only [irrational, rat.forall, cast_mk, not_exists, set.mem_range, cast_coe_int, cast_div,
eq_comm]
/-!
### Irrationality of roots of integer and rational numbers
-/
/-- If `x^n`, `n > 0`, is integer and is not the `n`-th power of an integer, then
`x` is irrational. -/
theorem irrational_nrt_of_notint_nrt {x : ℝ} (n : ℕ) (m : ℤ)
(hxr : x ^ n = m) (hv : ¬ ∃ y : ℤ, x = y) (hnpos : 0 < n) :
irrational x :=
begin
rintros ⟨⟨N, D, P, C⟩, rfl⟩,
rw [← cast_pow] at hxr,
have c1 : ((D : ℤ) : ℝ) ≠ 0,
{ rw [int.cast_ne_zero, int.coe_nat_ne_zero], exact ne_of_gt P },
have c2 : ((D : ℤ) : ℝ) ^ n ≠ 0 := pow_ne_zero _ c1,
rw [num_denom', cast_pow, cast_mk, div_pow, div_eq_iff_mul_eq c2,
← int.cast_pow, ← int.cast_pow, ← int.cast_mul, int.cast_inj] at hxr,
have hdivn : ↑D ^ n ∣ N ^ n := dvd.intro_left m hxr,
rw [← int.dvd_nat_abs, ← int.coe_nat_pow, int.coe_nat_dvd, int.nat_abs_pow,
nat.pow_dvd_pow_iff hnpos] at hdivn,
have hD : D = 1 := by rw [← nat.gcd_eq_right hdivn, C.gcd_eq_one],
subst D,
refine hv ⟨N, _⟩,
rw [num_denom', int.coe_nat_one, mk_eq_div, int.cast_one, div_one, cast_coe_int]
end
/-- If `x^n = m` is an integer and `n` does not divide the `multiplicity p m`, then `x`
is irrational. -/
theorem irrational_nrt_of_n_not_dvd_multiplicity {x : ℝ} (n : ℕ) {m : ℤ} (hm : m ≠ 0) (p : ℕ)
[hp : fact p.prime] (hxr : x ^ n = m)
(hv : (multiplicity (p : ℤ) m).get (finite_int_iff.2 ⟨hp.ne_one, hm⟩) % n ≠ 0) :
irrational x :=
begin
rcases nat.eq_zero_or_pos n with rfl | hnpos,
{ rw [eq_comm, pow_zero, ← int.cast_one, int.cast_inj] at hxr,
simpa [hxr, multiplicity.one_right (mt is_unit_iff_dvd_one.1
(mt int.coe_nat_dvd.1 hp.not_dvd_one)), nat.zero_mod] using hv },
refine irrational_nrt_of_notint_nrt _ _ hxr _ hnpos,
rintro ⟨y, rfl⟩,
rw [← int.cast_pow, int.cast_inj] at hxr, subst m,
have : y ≠ 0, { rintro rfl, rw zero_pow hnpos at hm, exact hm rfl },
erw [multiplicity.pow' (nat.prime_iff_prime_int.1 hp)
(finite_int_iff.2 ⟨hp.ne_one, this⟩), nat.mul_mod_right] at hv,
exact hv rfl
end
theorem irrational_sqrt_of_multiplicity_odd (m : ℤ) (hm : 0 < m)
(p : ℕ) [hp : fact p.prime]
(Hpv : (multiplicity (p : ℤ) m).get (finite_int_iff.2 ⟨hp.ne_one, (ne_of_lt hm).symm⟩) % 2 = 1) :
irrational (sqrt m) :=
@irrational_nrt_of_n_not_dvd_multiplicity _ 2 _ (ne.symm (ne_of_lt hm)) p hp
(sqr_sqrt (int.cast_nonneg.2 $ le_of_lt hm))
(by rw Hpv; exact one_ne_zero)
theorem nat.prime.irrational_sqrt {p : ℕ} (hp : nat.prime p) : irrational (sqrt p) :=
@irrational_sqrt_of_multiplicity_odd p (int.coe_nat_pos.2 hp.pos) p hp $
by simp [multiplicity_self (mt is_unit_iff_dvd_one.1 (mt int.coe_nat_dvd.1 hp.not_dvd_one) : _)];
refl
theorem irrational_sqrt_two : irrational (sqrt 2) :=
by simpa using nat.prime_two.irrational_sqrt
theorem irrational_sqrt_rat_iff (q : ℚ) : irrational (sqrt q) ↔
rat.sqrt q * rat.sqrt q ≠ q ∧ 0 ≤ q :=
if H1 : rat.sqrt q * rat.sqrt q = q
then iff_of_false (not_not_intro ⟨rat.sqrt q,
by rw [← H1, cast_mul, sqrt_mul_self (cast_nonneg.2 $ rat.sqrt_nonneg q),
sqrt_eq, abs_of_nonneg (rat.sqrt_nonneg q)]⟩) (λ h, h.1 H1)
else if H2 : 0 ≤ q
then iff_of_true (λ ⟨r, hr⟩, H1 $ (exists_mul_self _).1 ⟨r,
by rwa [eq_comm, sqrt_eq_iff_mul_self_eq (cast_nonneg.2 H2), ← cast_mul, cast_inj] at hr;
rw [← hr]; exact real.sqrt_nonneg _⟩) ⟨H1, H2⟩
else iff_of_false (not_not_intro ⟨0,
by rw cast_zero; exact (sqrt_eq_zero_of_nonpos (rat.cast_nonpos.2 $ le_of_not_le H2)).symm⟩)
(λ h, H2 h.2)
instance (q : ℚ) : decidable (irrational (sqrt q)) :=
decidable_of_iff' _ (irrational_sqrt_rat_iff q)
/-!
### Adding/subtracting/multiplying by rational numbers
-/
lemma rat.not_irrational (q : ℚ) : ¬irrational q := λ h, h ⟨q, rfl⟩
namespace irrational
variables (q : ℚ) {x y : ℝ}
open_locale classical
theorem add_cases : irrational (x + y) → irrational x ∨ irrational y :=
begin
delta irrational,
contrapose!,
rintros ⟨⟨rx, rfl⟩, ⟨ry, rfl⟩⟩,
exact ⟨rx + ry, cast_add rx ry⟩
end
theorem of_rat_add (h : irrational (q + x)) : irrational x :=
h.add_cases.elim (λ h, absurd h q.not_irrational) id
theorem rat_add (h : irrational x) : irrational (q + x) :=
of_rat_add (-q) $ by rwa [cast_neg, neg_add_cancel_left]
theorem of_add_rat : irrational (x + q) → irrational x :=
add_comm ↑q x ▸ of_rat_add q
theorem add_rat (h : irrational x) : irrational (x + q) :=
add_comm ↑q x ▸ h.rat_add q
theorem of_neg (h : irrational (-x)) : irrational x :=
λ ⟨q, hx⟩, h ⟨-q, by rw [cast_neg, hx]⟩
protected theorem neg (h : irrational x) : irrational (-x) :=
of_neg $ by rwa neg_neg
theorem sub_rat (h : irrational x) : irrational (x - q) :=
by simpa only [cast_neg] using h.add_rat (-q)
theorem rat_sub (h : irrational x) : irrational (q - x) :=
h.neg.rat_add q
theorem of_sub_rat (h : irrational (x - q)) : irrational x :=
of_add_rat (-q) $ by simpa only [cast_neg]
theorem of_rat_sub (h : irrational (q - x)) : irrational x :=
(h.of_rat_add _).of_neg
theorem mul_cases : irrational (x * y) → irrational x ∨ irrational y :=
begin
delta irrational,
contrapose!,
rintros ⟨⟨rx, rfl⟩, ⟨ry, rfl⟩⟩,
exact ⟨rx * ry, cast_mul rx ry⟩
end
theorem of_mul_rat (h : irrational (x * q)) : irrational x :=
h.mul_cases.elim id (λ h, absurd h q.not_irrational)
theorem mul_rat (h : irrational x) {q : ℚ} (hq : q ≠ 0) : irrational (x * q) :=
of_mul_rat q⁻¹ $ by rwa [mul_assoc, ← cast_mul, mul_inv_cancel hq, cast_one, mul_one]
theorem of_rat_mul : irrational (q * x) → irrational x :=
mul_comm x q ▸ of_mul_rat q
theorem rat_mul (h : irrational x) {q : ℚ} (hq : q ≠ 0) : irrational (q * x) :=
mul_comm x q ▸ h.mul_rat hq
theorem of_mul_self (h : irrational (x * x)) : irrational x :=
h.mul_cases.elim id id
theorem of_inv (h : irrational x⁻¹) : irrational x :=
λ ⟨q, hq⟩, h $ hq ▸ ⟨q⁻¹, q.cast_inv⟩
protected theorem inv (h : irrational x) : irrational x⁻¹ :=
of_inv $ by rwa inv_inv'
theorem div_cases (h : irrational (x / y)) : irrational x ∨ irrational y :=
h.mul_cases.imp id of_inv
theorem of_rat_div (h : irrational (q / x)) : irrational x :=
(h.of_rat_mul q).of_inv
theorem of_one_div (h : irrational (1 / x)) : irrational x :=
of_rat_div 1 $ by rwa [cast_one]
theorem of_pow : ∀ n : ℕ, irrational (x^n) → irrational x
| 0 := λ h, (h ⟨1, cast_one⟩).elim
| (n+1) := λ h, h.mul_cases.elim id (of_pow n)
theorem of_fpow : ∀ m : ℤ, irrational (x^m) → irrational x
| (n:ℕ) := of_pow n
| -[1+n] := λ h, by { rw fpow_neg_succ_of_nat at h, exact h.of_inv.of_pow _ }
end irrational
section polynomial
open polynomial
variables (x : ℝ) (p : polynomial ℤ)
lemma nat_degree_gt_one_of_irrational_root (hx : irrational x) (p_nonzero : p ≠ 0)
(x_is_root : (p.map (algebra_map ℤ ℝ)).is_root x) : 1 < p.nat_degree :=
begin
have degree_eq : p.nat_degree = (p.map (algebra_map ℤ ℝ)).nat_degree,
{ rw nat_degree_map', exact int.cast_injective },
by_contra rid,
push_neg at rid,
interval_cases p.nat_degree with h_degree,
{ have hp := eq_C_of_nat_degree_eq_zero h_degree,
have hpx := x_is_root,
rw [hp, is_root.def, eval_map, eval₂_C, ring_hom.eq_int_cast, int.cast_eq_zero] at hpx,
rw [hpx, C_0] at hp,
exact p_nonzero hp },
{ rw irrational_iff_ne_rational at hx,
apply hx (-(p.coeff 0)) (p.coeff 1),
rw [as_sum_range p] at x_is_root,
simp only [is_root.def, h_degree, eval_map, finset.sum_range_succ, finset.sum_range_one,
eval₂_mul, eval₂_add, eval₂_X, eval₂_C, eval₂_one, pow_one, pow_zero, mul_one] at x_is_root,
simp only [ring_hom.eq_int_cast, add_eq_zero_iff_eq_neg] at x_is_root,
suffices : (p.coeff 1 : ℝ) ≠ 0,
{ rw [eq_div_iff this, mul_comm, x_is_root, int.cast_neg] },
norm_cast,
rwa [← h_degree, ← leading_coeff, leading_coeff_eq_zero] }
end
end polynomial
section
variables {q : ℚ} {x : ℝ}
open irrational
@[simp] theorem irrational_rat_add_iff : irrational (q + x) ↔ irrational x :=
⟨of_rat_add q, rat_add q⟩
@[simp] theorem irrational_add_rat_iff : irrational (x + q) ↔ irrational x :=
⟨of_add_rat q, add_rat q⟩
@[simp] theorem irrational_rat_sub_iff : irrational (q - x) ↔ irrational x :=
⟨of_rat_sub q, rat_sub q⟩
@[simp] theorem irrational_sub_rat_iff : irrational (x - q) ↔ irrational x :=
⟨of_sub_rat q, sub_rat q⟩
@[simp] theorem irrational_neg_iff : irrational (-x) ↔ irrational x :=
⟨of_neg, irrational.neg⟩
@[simp] theorem irrational_inv_iff : irrational x⁻¹ ↔ irrational x :=
⟨of_inv, irrational.inv⟩
end
|
470f8ba5b8f04c341bba870e2df4c96cc5e8ded6 | 54c9ed381c63410c9b6af3b0a1722c41152f037f | /Lib4/PrePort/Numbers.lean | 371246908c71705500b9c99fb3cbadcd4a57dea8 | [
"Apache-2.0"
] | permissive | dselsam/binport | 0233f1aa961a77c4fc96f0dccc780d958c5efc6c | aef374df0e169e2c3f1dc911de240c076315805c | refs/heads/master | 1,687,453,448,108 | 1,627,483,296,000 | 1,627,483,296,000 | 333,825,622 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,850 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Daniel Selsam
In Lean3, numerals are encoded using 0, 1, bit0, and bit1,
whereas in Lean4, nats are encoded as kernel literals
and numerals of type α are encoded using the following class:
class OfNat (α : Type u) (n : Nat) where
ofNat : α
Specifically, the numeral (17 : α) is notation for
@OfNat.ofNat α 17 (inst : OfNat α 17)
We automatically wrap the bit-representation with the OfNat representation
during porting (the two are definitionally equal).
The non-kernel-computing `nat2bits` instance cannot be used during porting,
since mathlib currently relies on e.g. 2+2=4 computing in the kernel.
We keep it in PrePort to support various typeclass experiments.
-/
namespace Mathlib
-- We define these classes here so that we can align Mathlib's
-- classes to them.
class HasZero (α : Type u) := (zero : α)
class HasOne (α : Type u) := (one : α)
universe u
variable {α : Type u} [HasZero α] [HasOne α] [Add α] [Inhabited α]
def bit0 (x : α) : α := x + x
def bit1 (x : α) : α := bit0 x + HasOne.one
-- TODO: these should be nat-lits, but currently the auto-porter
-- is sometimes creating terms with `OfNat.ofNat Nat ...` instead
instance instZero2Nat : OfNat α (no_index 0) /- (nat_lit 0) -/ := ⟨HasZero.zero⟩
instance instOne2Nat : OfNat α (no_index 1) /- (nat_lit 1) -/ := ⟨HasOne.one⟩
-- TODO: well-founded
partial def nat2bits (n : Nat) : α :=
if n == 0 then arbitrary
else if n == 1 then HasOne.one
else if n % 2 == 1 then bit1 (nat2bits (n / 2))
else bit0 (nat2bits (n / 2))
instance instBits2Nat (n : Nat) : OfNat α (no_index (n+1)) /- (n+1) -/ := ⟨nat2bits (n+1)⟩
#print instZero2Nat
#print instOne2Nat
#print instBits2Nat
end Mathlib
|
1a969868129b9bcd0bf62cafdbc13e29157e5014 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/order/upper_lower.lean | 8ae3cb28d4a41c9071c843d14e6d5a220f7c64a0 | [
"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 | 42,651 | lean | /-
Copyright (c) 2022 Yaël Dillies, Sara Rousta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies, Sara Rousta
-/
import data.set_like.basic
import data.set.intervals.ord_connected
import data.set.intervals.order_iso
import order.hom.complete_lattice
/-!
# Up-sets and down-sets
This file defines upper and lower sets in an order.
## Main declarations
* `is_upper_set`: Predicate for a set to be an upper set. This means every element greater than a
member of the set is in the set itself.
* `is_lower_set`: Predicate for a set to be a lower set. This means every element less than a member
of the set is in the set itself.
* `upper_set`: The type of upper sets.
* `lower_set`: The type of lower sets.
* `upper_closure`: The greatest upper set containing a set.
* `lower_closure`: The least lower set containing a set.
* `upper_set.Ici`: Principal upper set. `set.Ici` as an upper set.
* `upper_set.Ioi`: Strict principal upper set. `set.Ioi` as an upper set.
* `lower_set.Iic`: Principal lower set. `set.Iic` as an lower set.
* `lower_set.Iio`: Strict principal lower set. `set.Iio` as an lower set.
## Notation
`×ˢ` is notation for `upper_set.prod`/`lower_set.prod`.
## Notes
Upper sets are ordered by **reverse** inclusion. This convention is motivated by the fact that this
makes them order-isomorphic to lower sets and antichains, and matches the convention on `filter`.
## TODO
Lattice structure on antichains. Order equivalence between upper/lower sets and antichains.
-/
open order_dual set
variables {α β γ : Type*} {ι : Sort*} {κ : ι → Sort*}
/-! ### Unbundled upper/lower sets -/
section has_le
variables [has_le α] [has_le β] {s t : set α}
/-- An upper set in an order `α` is a set such that any element greater than one of its members is
also a member. Also called up-set, upward-closed set. -/
def is_upper_set (s : set α) : Prop := ∀ ⦃a b : α⦄, a ≤ b → a ∈ s → b ∈ s
/-- A lower set in an order `α` is a set such that any element less than one of its members is also
a member. Also called down-set, downward-closed set. -/
def is_lower_set (s : set α) : Prop := ∀ ⦃a b : α⦄, b ≤ a → a ∈ s → b ∈ s
lemma is_upper_set_empty : is_upper_set (∅ : set α) := λ _ _ _, id
lemma is_lower_set_empty : is_lower_set (∅ : set α) := λ _ _ _, id
lemma is_upper_set_univ : is_upper_set (univ : set α) := λ _ _ _, id
lemma is_lower_set_univ : is_lower_set (univ : set α) := λ _ _ _, id
lemma is_upper_set.compl (hs : is_upper_set s) : is_lower_set sᶜ := λ a b h hb ha, hb $ hs h ha
lemma is_lower_set.compl (hs : is_lower_set s) : is_upper_set sᶜ := λ a b h hb ha, hb $ hs h ha
@[simp] lemma is_upper_set_compl : is_upper_set sᶜ ↔ is_lower_set s :=
⟨λ h, by { convert h.compl, rw compl_compl }, is_lower_set.compl⟩
@[simp] lemma is_lower_set_compl : is_lower_set sᶜ ↔ is_upper_set s :=
⟨λ h, by { convert h.compl, rw compl_compl }, is_upper_set.compl⟩
lemma is_upper_set.union (hs : is_upper_set s) (ht : is_upper_set t) : is_upper_set (s ∪ t) :=
λ a b h, or.imp (hs h) (ht h)
lemma is_lower_set.union (hs : is_lower_set s) (ht : is_lower_set t) : is_lower_set (s ∪ t) :=
λ a b h, or.imp (hs h) (ht h)
lemma is_upper_set.inter (hs : is_upper_set s) (ht : is_upper_set t) : is_upper_set (s ∩ t) :=
λ a b h, and.imp (hs h) (ht h)
lemma is_lower_set.inter (hs : is_lower_set s) (ht : is_lower_set t) : is_lower_set (s ∩ t) :=
λ a b h, and.imp (hs h) (ht h)
lemma is_upper_set_Union {f : ι → set α} (hf : ∀ i, is_upper_set (f i)) : is_upper_set (⋃ i, f i) :=
λ a b h, Exists₂.imp $ forall_range_iff.2 $ λ i, hf i h
lemma is_lower_set_Union {f : ι → set α} (hf : ∀ i, is_lower_set (f i)) : is_lower_set (⋃ i, f i) :=
λ a b h, Exists₂.imp $ forall_range_iff.2 $ λ i, hf i h
lemma is_upper_set_Union₂ {f : Π i, κ i → set α} (hf : ∀ i j, is_upper_set (f i j)) :
is_upper_set (⋃ i j, f i j) :=
is_upper_set_Union $ λ i, is_upper_set_Union $ hf i
lemma is_lower_set_Union₂ {f : Π i, κ i → set α} (hf : ∀ i j, is_lower_set (f i j)) :
is_lower_set (⋃ i j, f i j) :=
is_lower_set_Union $ λ i, is_lower_set_Union $ hf i
lemma is_upper_set_sUnion {S : set (set α)} (hf : ∀ s ∈ S, is_upper_set s) : is_upper_set (⋃₀ S) :=
λ a b h, Exists₂.imp $ λ s hs, hf s hs h
lemma is_lower_set_sUnion {S : set (set α)} (hf : ∀ s ∈ S, is_lower_set s) : is_lower_set (⋃₀ S) :=
λ a b h, Exists₂.imp $ λ s hs, hf s hs h
lemma is_upper_set_Inter {f : ι → set α} (hf : ∀ i, is_upper_set (f i)) : is_upper_set (⋂ i, f i) :=
λ a b h, forall₂_imp $ forall_range_iff.2 $ λ i, hf i h
lemma is_lower_set_Inter {f : ι → set α} (hf : ∀ i, is_lower_set (f i)) : is_lower_set (⋂ i, f i) :=
λ a b h, forall₂_imp $ forall_range_iff.2 $ λ i, hf i h
lemma is_upper_set_Inter₂ {f : Π i, κ i → set α} (hf : ∀ i j, is_upper_set (f i j)) :
is_upper_set (⋂ i j, f i j) :=
is_upper_set_Inter $ λ i, is_upper_set_Inter $ hf i
lemma is_lower_set_Inter₂ {f : Π i, κ i → set α} (hf : ∀ i j, is_lower_set (f i j)) :
is_lower_set (⋂ i j, f i j) :=
is_lower_set_Inter $ λ i, is_lower_set_Inter $ hf i
lemma is_upper_set_sInter {S : set (set α)} (hf : ∀ s ∈ S, is_upper_set s) : is_upper_set (⋂₀ S) :=
λ a b h, forall₂_imp $ λ s hs, hf s hs h
lemma is_lower_set_sInter {S : set (set α)} (hf : ∀ s ∈ S, is_lower_set s) : is_lower_set (⋂₀ S) :=
λ a b h, forall₂_imp $ λ s hs, hf s hs h
@[simp] lemma is_lower_set_preimage_of_dual_iff : is_lower_set (of_dual ⁻¹' s) ↔ is_upper_set s :=
iff.rfl
@[simp] lemma is_upper_set_preimage_of_dual_iff : is_upper_set (of_dual ⁻¹' s) ↔ is_lower_set s :=
iff.rfl
@[simp] lemma is_lower_set_preimage_to_dual_iff {s : set αᵒᵈ} :
is_lower_set (to_dual ⁻¹' s) ↔ is_upper_set s := iff.rfl
@[simp] lemma is_upper_set_preimage_to_dual_iff {s : set αᵒᵈ} :
is_upper_set (to_dual ⁻¹' s) ↔ is_lower_set s := iff.rfl
alias is_lower_set_preimage_of_dual_iff ↔ _ is_upper_set.of_dual
alias is_upper_set_preimage_of_dual_iff ↔ _ is_lower_set.of_dual
alias is_lower_set_preimage_to_dual_iff ↔ _ is_upper_set.to_dual
alias is_upper_set_preimage_to_dual_iff ↔ _ is_lower_set.to_dual
end has_le
section preorder
variables [preorder α] [preorder β] {s : set α} {p : α → Prop} (a : α)
lemma is_upper_set_Ici : is_upper_set (Ici a) := λ _ _, ge_trans
lemma is_lower_set_Iic : is_lower_set (Iic a) := λ _ _, le_trans
lemma is_upper_set_Ioi : is_upper_set (Ioi a) := λ _ _, flip lt_of_lt_of_le
lemma is_lower_set_Iio : is_lower_set (Iio a) := λ _ _, lt_of_le_of_lt
lemma is_upper_set_iff_Ici_subset : is_upper_set s ↔ ∀ ⦃a⦄, a ∈ s → Ici a ⊆ s :=
by simp [is_upper_set, subset_def, @forall_swap (_ ∈ s)]
lemma is_lower_set_iff_Iic_subset : is_lower_set s ↔ ∀ ⦃a⦄, a ∈ s → Iic a ⊆ s :=
by simp [is_lower_set, subset_def, @forall_swap (_ ∈ s)]
alias is_upper_set_iff_Ici_subset ↔ is_upper_set.Ici_subset _
alias is_lower_set_iff_Iic_subset ↔ is_lower_set.Iic_subset _
lemma is_upper_set.ord_connected (h : is_upper_set s) : s.ord_connected :=
⟨λ a ha b _, Icc_subset_Ici_self.trans $ h.Ici_subset ha⟩
lemma is_lower_set.ord_connected (h : is_lower_set s) : s.ord_connected :=
⟨λ a _ b hb, Icc_subset_Iic_self.trans $ h.Iic_subset hb⟩
lemma is_upper_set.preimage (hs : is_upper_set s) {f : β → α} (hf : monotone f) :
is_upper_set (f ⁻¹' s : set β) :=
λ x y hxy, hs $ hf hxy
lemma is_lower_set.preimage (hs : is_lower_set s) {f : β → α} (hf : monotone f) :
is_lower_set (f ⁻¹' s : set β) :=
λ x y hxy, hs $ hf hxy
lemma is_upper_set.image (hs : is_upper_set s) (f : α ≃o β) : is_upper_set (f '' s : set β) :=
by { change is_upper_set ((f : α ≃ β) '' s), rw set.image_equiv_eq_preimage_symm,
exact hs.preimage f.symm.monotone }
lemma is_lower_set.image (hs : is_lower_set s) (f : α ≃o β) : is_lower_set (f '' s : set β) :=
by { change is_lower_set ((f : α ≃ β) '' s), rw set.image_equiv_eq_preimage_symm,
exact hs.preimage f.symm.monotone }
@[simp] lemma set.monotone_mem : monotone (∈ s) ↔ is_upper_set s := iff.rfl
@[simp] lemma set.antitone_mem : antitone (∈ s) ↔ is_lower_set s := forall_swap
@[simp] lemma is_upper_set_set_of : is_upper_set {a | p a} ↔ monotone p := iff.rfl
@[simp] lemma is_lower_set_set_of : is_lower_set {a | p a} ↔ antitone p := forall_swap
section order_top
variables [order_top α]
lemma is_lower_set.top_mem (hs : is_lower_set s) : ⊤ ∈ s ↔ s = univ :=
⟨λ h, eq_univ_of_forall $ λ a, hs le_top h, λ h, h.symm ▸ mem_univ _⟩
lemma is_upper_set.top_mem (hs : is_upper_set s) : ⊤ ∈ s ↔ s.nonempty :=
⟨λ h, ⟨_, h⟩, λ ⟨a, ha⟩, hs le_top ha⟩
lemma is_upper_set.not_top_mem (hs : is_upper_set s) : ⊤ ∉ s ↔ s = ∅ :=
hs.top_mem.not.trans not_nonempty_iff_eq_empty
end order_top
section order_bot
variables [order_bot α]
lemma is_upper_set.bot_mem (hs : is_upper_set s) : ⊥ ∈ s ↔ s = univ :=
⟨λ h, eq_univ_of_forall $ λ a, hs bot_le h, λ h, h.symm ▸ mem_univ _⟩
lemma is_lower_set.bot_mem (hs : is_lower_set s) : ⊥ ∈ s ↔ s.nonempty :=
⟨λ h, ⟨_, h⟩, λ ⟨a, ha⟩, hs bot_le ha⟩
lemma is_lower_set.not_bot_mem (hs : is_lower_set s) : ⊥ ∉ s ↔ s = ∅ :=
hs.bot_mem.not.trans not_nonempty_iff_eq_empty
end order_bot
section no_max_order
variables [no_max_order α] (a)
lemma is_upper_set.not_bdd_above (hs : is_upper_set s) : s.nonempty → ¬ bdd_above s :=
begin
rintro ⟨a, ha⟩ ⟨b, hb⟩,
obtain ⟨c, hc⟩ := exists_gt b,
exact hc.not_le (hb $ hs ((hb ha).trans hc.le) ha),
end
lemma not_bdd_above_Ici : ¬ bdd_above (Ici a) := (is_upper_set_Ici _).not_bdd_above nonempty_Ici
lemma not_bdd_above_Ioi : ¬ bdd_above (Ioi a) := (is_upper_set_Ioi _).not_bdd_above nonempty_Ioi
end no_max_order
section no_min_order
variables [no_min_order α] (a)
lemma is_lower_set.not_bdd_below (hs : is_lower_set s) : s.nonempty → ¬ bdd_below s :=
begin
rintro ⟨a, ha⟩ ⟨b, hb⟩,
obtain ⟨c, hc⟩ := exists_lt b,
exact hc.not_le (hb $ hs (hc.le.trans $ hb ha) ha),
end
lemma not_bdd_below_Iic : ¬ bdd_below (Iic a) := (is_lower_set_Iic _).not_bdd_below nonempty_Iic
lemma not_bdd_below_Iio : ¬ bdd_below (Iio a) := (is_lower_set_Iio _).not_bdd_below nonempty_Iio
end no_min_order
end preorder
section partial_order
variables [partial_order α] {s : set α}
lemma is_upper_set_iff_forall_lt : is_upper_set s ↔ ∀ ⦃a b : α⦄, a < b → a ∈ s → b ∈ s :=
forall_congr $ λ a, by simp [le_iff_eq_or_lt, or_imp_distrib, forall_and_distrib]
lemma is_lower_set_iff_forall_lt : is_lower_set s ↔ ∀ ⦃a b : α⦄, b < a → a ∈ s → b ∈ s :=
forall_congr $ λ a, by simp [le_iff_eq_or_lt, or_imp_distrib, forall_and_distrib]
lemma is_upper_set_iff_Ioi_subset : is_upper_set s ↔ ∀ ⦃a⦄, a ∈ s → Ioi a ⊆ s :=
by simp [is_upper_set_iff_forall_lt, subset_def, @forall_swap (_ ∈ s)]
lemma is_lower_set_iff_Iio_subset : is_lower_set s ↔ ∀ ⦃a⦄, a ∈ s → Iio a ⊆ s :=
by simp [is_lower_set_iff_forall_lt, subset_def, @forall_swap (_ ∈ s)]
alias is_upper_set_iff_Ioi_subset ↔ is_upper_set.Ioi_subset _
alias is_lower_set_iff_Iio_subset ↔ is_lower_set.Iio_subset _
end partial_order
/-! ### Bundled upper/lower sets -/
section has_le
variables [has_le α]
/-- The type of upper sets of an order. -/
structure upper_set (α : Type*) [has_le α] :=
(carrier : set α)
(upper' : is_upper_set carrier)
/-- The type of lower sets of an order. -/
structure lower_set (α : Type*) [has_le α] :=
(carrier : set α)
(lower' : is_lower_set carrier)
namespace upper_set
instance : set_like (upper_set α) α :=
{ coe := upper_set.carrier,
coe_injective' := λ s t h, by { cases s, cases t, congr' } }
@[ext] lemma ext {s t : upper_set α} : (s : set α) = t → s = t := set_like.ext'
@[simp] lemma carrier_eq_coe (s : upper_set α) : s.carrier = s := rfl
protected lemma upper (s : upper_set α) : is_upper_set (s : set α) := s.upper'
@[simp] lemma mem_mk (carrier : set α) (upper') {a : α} : a ∈ mk carrier upper' ↔ a ∈ carrier :=
iff.rfl
end upper_set
namespace lower_set
instance : set_like (lower_set α) α :=
{ coe := lower_set.carrier,
coe_injective' := λ s t h, by { cases s, cases t, congr' } }
@[ext] lemma ext {s t : lower_set α} : (s : set α) = t → s = t := set_like.ext'
@[simp] lemma carrier_eq_coe (s : lower_set α) : s.carrier = s := rfl
protected lemma lower (s : lower_set α) : is_lower_set (s : set α) := s.lower'
@[simp] lemma mem_mk (carrier : set α) (lower') {a : α} : a ∈ mk carrier lower' ↔ a ∈ carrier :=
iff.rfl
end lower_set
/-! #### Order -/
namespace upper_set
variables {S : set (upper_set α)} {s t : upper_set α} {a : α}
instance : has_sup (upper_set α) := ⟨λ s t, ⟨s ∩ t, s.upper.inter t.upper⟩⟩
instance : has_inf (upper_set α) := ⟨λ s t, ⟨s ∪ t, s.upper.union t.upper⟩⟩
instance : has_top (upper_set α) := ⟨⟨∅, is_upper_set_empty⟩⟩
instance : has_bot (upper_set α) := ⟨⟨univ, is_upper_set_univ⟩⟩
instance : has_Sup (upper_set α) :=
⟨λ S, ⟨⋂ s ∈ S, ↑s, is_upper_set_Inter₂ $ λ s _, s.upper⟩⟩
instance : has_Inf (upper_set α) :=
⟨λ S, ⟨⋃ s ∈ S, ↑s, is_upper_set_Union₂ $ λ s _, s.upper⟩⟩
instance : complete_distrib_lattice (upper_set α) :=
(to_dual.injective.comp $ set_like.coe_injective).complete_distrib_lattice _
(λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) rfl rfl
instance : inhabited (upper_set α) := ⟨⊥⟩
@[simp, norm_cast] lemma coe_subset_coe : (s : set α) ⊆ t ↔ t ≤ s := iff.rfl
@[simp, norm_cast] lemma coe_top : ((⊤ : upper_set α) : set α) = ∅ := rfl
@[simp, norm_cast] lemma coe_bot : ((⊥ : upper_set α) : set α) = univ := rfl
@[simp, norm_cast] lemma coe_sup (s t : upper_set α) : (↑(s ⊔ t) : set α) = s ∩ t := rfl
@[simp, norm_cast] lemma coe_inf (s t : upper_set α) : (↑(s ⊓ t) : set α) = s ∪ t := rfl
@[simp, norm_cast] lemma coe_Sup (S : set (upper_set α)) : (↑(Sup S) : set α) = ⋂ s ∈ S, ↑s := rfl
@[simp, norm_cast] lemma coe_Inf (S : set (upper_set α)) : (↑(Inf S) : set α) = ⋃ s ∈ S, ↑s := rfl
@[simp, norm_cast] lemma coe_supr (f : ι → upper_set α) : (↑(⨆ i, f i) : set α) = ⋂ i, f i :=
by simp [supr]
@[simp, norm_cast] lemma coe_infi (f : ι → upper_set α) : (↑(⨅ i, f i) : set α) = ⋃ i, f i :=
by simp [infi]
@[simp, norm_cast] lemma coe_supr₂ (f : Π i, κ i → upper_set α) :
(↑(⨆ i j, f i j) : set α) = ⋂ i j, f i j := by simp_rw coe_supr
@[simp, norm_cast] lemma coe_infi₂ (f : Π i, κ i → upper_set α) :
(↑(⨅ i j, f i j) : set α) = ⋃ i j, f i j := by simp_rw coe_infi
@[simp] lemma not_mem_top : a ∉ (⊤ : upper_set α) := id
@[simp] lemma mem_bot : a ∈ (⊥ : upper_set α) := trivial
@[simp] lemma mem_sup_iff : a ∈ s ⊔ t ↔ a ∈ s ∧ a ∈ t := iff.rfl
@[simp] lemma mem_inf_iff : a ∈ s ⊓ t ↔ a ∈ s ∨ a ∈ t := iff.rfl
@[simp] lemma mem_Sup_iff : a ∈ Sup S ↔ ∀ s ∈ S, a ∈ s := mem_Inter₂
@[simp] lemma mem_Inf_iff : a ∈ Inf S ↔ ∃ s ∈ S, a ∈ s := mem_Union₂
@[simp] lemma mem_supr_iff {f : ι → upper_set α} : a ∈ (⨆ i, f i) ↔ ∀ i, a ∈ f i :=
by { rw [←set_like.mem_coe, coe_supr], exact mem_Inter }
@[simp] lemma mem_infi_iff {f : ι → upper_set α} : a ∈ (⨅ i, f i) ↔ ∃ i, a ∈ f i :=
by { rw [←set_like.mem_coe, coe_infi], exact mem_Union }
@[simp] lemma mem_supr₂_iff {f : Π i, κ i → upper_set α} : a ∈ (⨆ i j, f i j) ↔ ∀ i j, a ∈ f i j :=
by simp_rw mem_supr_iff
@[simp] lemma mem_infi₂_iff {f : Π i, κ i → upper_set α} : a ∈ (⨅ i j, f i j) ↔ ∃ i j, a ∈ f i j :=
by simp_rw mem_infi_iff
end upper_set
namespace lower_set
variables {S : set (lower_set α)} {s t : lower_set α} {a : α}
instance : has_sup (lower_set α) := ⟨λ s t, ⟨s ∪ t, λ a b h, or.imp (s.lower h) (t.lower h)⟩⟩
instance : has_inf (lower_set α) := ⟨λ s t, ⟨s ∩ t, λ a b h, and.imp (s.lower h) (t.lower h)⟩⟩
instance : has_top (lower_set α) := ⟨⟨univ, λ a b h, id⟩⟩
instance : has_bot (lower_set α) := ⟨⟨∅, λ a b h, id⟩⟩
instance : has_Sup (lower_set α) := ⟨λ S, ⟨⋃ s ∈ S, ↑s, is_lower_set_Union₂ $ λ s _, s.lower⟩⟩
instance : has_Inf (lower_set α) := ⟨λ S, ⟨⋂ s ∈ S, ↑s, is_lower_set_Inter₂ $ λ s _, s.lower⟩⟩
instance : complete_distrib_lattice (lower_set α) :=
set_like.coe_injective.complete_distrib_lattice _
(λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) rfl rfl
instance : inhabited (lower_set α) := ⟨⊥⟩
@[simp, norm_cast] lemma coe_subset_coe : (s : set α) ⊆ t ↔ s ≤ t := iff.rfl
@[simp, norm_cast] lemma coe_top : ((⊤ : lower_set α) : set α) = univ := rfl
@[simp, norm_cast] lemma coe_bot : ((⊥ : lower_set α) : set α) = ∅ := rfl
@[simp, norm_cast] lemma coe_sup (s t : lower_set α) : (↑(s ⊔ t) : set α) = s ∪ t := rfl
@[simp, norm_cast] lemma coe_inf (s t : lower_set α) : (↑(s ⊓ t) : set α) = s ∩ t := rfl
@[simp, norm_cast] lemma coe_Sup (S : set (lower_set α)) : (↑(Sup S) : set α) = ⋃ s ∈ S, ↑s := rfl
@[simp, norm_cast] lemma coe_Inf (S : set (lower_set α)) : (↑(Inf S) : set α) = ⋂ s ∈ S, ↑s := rfl
@[simp, norm_cast] lemma coe_supr (f : ι → lower_set α) : (↑(⨆ i, f i) : set α) = ⋃ i, f i :=
by simp_rw [supr, coe_Sup, mem_range, Union_exists, Union_Union_eq']
@[simp, norm_cast] lemma coe_infi (f : ι → lower_set α) : (↑(⨅ i, f i) : set α) = ⋂ i, f i :=
by simp_rw [infi, coe_Inf, mem_range, Inter_exists, Inter_Inter_eq']
@[simp, norm_cast] lemma coe_supr₂ (f : Π i, κ i → lower_set α) :
(↑(⨆ i j, f i j) : set α) = ⋃ i j, f i j := by simp_rw coe_supr
@[simp, norm_cast] lemma coe_infi₂ (f : Π i, κ i → lower_set α) :
(↑(⨅ i j, f i j) : set α) = ⋂ i j, f i j := by simp_rw coe_infi
@[simp] lemma mem_top : a ∈ (⊤ : lower_set α) := trivial
@[simp] lemma not_mem_bot : a ∉ (⊥ : lower_set α) := id
@[simp] lemma mem_sup_iff : a ∈ s ⊔ t ↔ a ∈ s ∨ a ∈ t := iff.rfl
@[simp] lemma mem_inf_iff : a ∈ s ⊓ t ↔ a ∈ s ∧ a ∈ t := iff.rfl
@[simp] lemma mem_Sup_iff : a ∈ Sup S ↔ ∃ s ∈ S, a ∈ s := mem_Union₂
@[simp] lemma mem_Inf_iff : a ∈ Inf S ↔ ∀ s ∈ S, a ∈ s := mem_Inter₂
@[simp] lemma mem_supr_iff {f : ι → lower_set α} : a ∈ (⨆ i, f i) ↔ ∃ i, a ∈ f i :=
by { rw [←set_like.mem_coe, coe_supr], exact mem_Union }
@[simp] lemma mem_infi_iff {f : ι → lower_set α} : a ∈ (⨅ i, f i) ↔ ∀ i, a ∈ f i :=
by { rw [←set_like.mem_coe, coe_infi], exact mem_Inter }
@[simp] lemma mem_supr₂_iff {f : Π i, κ i → lower_set α} : a ∈ (⨆ i j, f i j) ↔ ∃ i j, a ∈ f i j :=
by simp_rw mem_supr_iff
@[simp] lemma mem_infi₂_iff {f : Π i, κ i → lower_set α} : a ∈ (⨅ i j, f i j) ↔ ∀ i j, a ∈ f i j :=
by simp_rw mem_infi_iff
end lower_set
/-! #### Complement -/
/-- The complement of a lower set as an upper set. -/
def upper_set.compl (s : upper_set α) : lower_set α := ⟨sᶜ, s.upper.compl⟩
/-- The complement of a lower set as an upper set. -/
def lower_set.compl (s : lower_set α) : upper_set α := ⟨sᶜ, s.lower.compl⟩
namespace upper_set
variables {s t : upper_set α} {a : α}
@[simp] lemma coe_compl (s : upper_set α) : (s.compl : set α) = sᶜ := rfl
@[simp] lemma mem_compl_iff : a ∈ s.compl ↔ a ∉ s := iff.rfl
@[simp] lemma compl_compl (s : upper_set α) : s.compl.compl = s := upper_set.ext $ compl_compl _
@[simp] lemma compl_le_compl : s.compl ≤ t.compl ↔ s ≤ t := compl_subset_compl
@[simp] protected lemma compl_sup (s t : upper_set α) : (s ⊔ t).compl = s.compl ⊔ t.compl :=
lower_set.ext compl_inf
@[simp] protected lemma compl_inf (s t : upper_set α) : (s ⊓ t).compl = s.compl ⊓ t.compl :=
lower_set.ext compl_sup
@[simp] protected lemma compl_top : (⊤ : upper_set α).compl = ⊤ := lower_set.ext compl_empty
@[simp] protected lemma compl_bot : (⊥ : upper_set α).compl = ⊥ := lower_set.ext compl_univ
@[simp] protected lemma compl_Sup (S : set (upper_set α)) :
(Sup S).compl = ⨆ s ∈ S, upper_set.compl s :=
lower_set.ext $ by simp only [coe_compl, coe_Sup, compl_Inter₂, lower_set.coe_supr₂]
@[simp] protected lemma compl_Inf (S : set (upper_set α)) :
(Inf S).compl = ⨅ s ∈ S, upper_set.compl s :=
lower_set.ext $ by simp only [coe_compl, coe_Inf, compl_Union₂, lower_set.coe_infi₂]
@[simp] protected lemma compl_supr (f : ι → upper_set α) : (⨆ i, f i).compl = ⨆ i, (f i).compl :=
lower_set.ext $ by simp only [coe_compl, coe_supr, compl_Inter, lower_set.coe_supr]
@[simp] protected lemma compl_infi (f : ι → upper_set α) : (⨅ i, f i).compl = ⨅ i, (f i).compl :=
lower_set.ext $ by simp only [coe_compl, coe_infi, compl_Union, lower_set.coe_infi]
@[simp] lemma compl_supr₂ (f : Π i, κ i → upper_set α) :
(⨆ i j, f i j).compl = ⨆ i j, (f i j).compl :=
by simp_rw upper_set.compl_supr
@[simp] lemma compl_infi₂ (f : Π i, κ i → upper_set α) :
(⨅ i j, f i j).compl = ⨅ i j, (f i j).compl :=
by simp_rw upper_set.compl_infi
end upper_set
namespace lower_set
variables {s t : lower_set α} {a : α}
@[simp] lemma coe_compl (s : lower_set α) : (s.compl : set α) = sᶜ := rfl
@[simp] lemma mem_compl_iff : a ∈ s.compl ↔ a ∉ s := iff.rfl
@[simp] lemma compl_compl (s : lower_set α) : s.compl.compl = s := lower_set.ext $ compl_compl _
@[simp] lemma compl_le_compl : s.compl ≤ t.compl ↔ s ≤ t := compl_subset_compl
protected lemma compl_sup (s t : lower_set α) : (s ⊔ t).compl = s.compl ⊔ t.compl :=
upper_set.ext compl_sup
protected lemma compl_inf (s t : lower_set α) : (s ⊓ t).compl = s.compl ⊓ t.compl :=
upper_set.ext compl_inf
protected lemma compl_top : (⊤ : lower_set α).compl = ⊤ := upper_set.ext compl_univ
protected lemma compl_bot : (⊥ : lower_set α).compl = ⊥ := upper_set.ext compl_empty
protected lemma compl_Sup (S : set (lower_set α)) : (Sup S).compl = ⨆ s ∈ S, lower_set.compl s :=
upper_set.ext $ by simp only [coe_compl, coe_Sup, compl_Union₂, upper_set.coe_supr₂]
protected lemma compl_Inf (S : set (lower_set α)) : (Inf S).compl = ⨅ s ∈ S, lower_set.compl s :=
upper_set.ext $ by simp only [coe_compl, coe_Inf, compl_Inter₂, upper_set.coe_infi₂]
protected lemma compl_supr (f : ι → lower_set α) : (⨆ i, f i).compl = ⨆ i, (f i).compl :=
upper_set.ext $ by simp only [coe_compl, coe_supr, compl_Union, upper_set.coe_supr]
protected lemma compl_infi (f : ι → lower_set α) : (⨅ i, f i).compl = ⨅ i, (f i).compl :=
upper_set.ext $ by simp only [coe_compl, coe_infi, compl_Inter, upper_set.coe_infi]
@[simp] lemma compl_supr₂ (f : Π i, κ i → lower_set α) :
(⨆ i j, f i j).compl = ⨆ i j, (f i j).compl :=
by simp_rw lower_set.compl_supr
@[simp] lemma compl_infi₂ (f : Π i, κ i → lower_set α) :
(⨅ i j, f i j).compl = ⨅ i j, (f i j).compl :=
by simp_rw lower_set.compl_infi
end lower_set
/-- Upper sets are order-isomorphic to lower sets under complementation. -/
@[simps] def upper_set_iso_lower_set : upper_set α ≃o lower_set α :=
{ to_fun := upper_set.compl,
inv_fun := lower_set.compl,
left_inv := upper_set.compl_compl,
right_inv := lower_set.compl_compl,
map_rel_iff' := λ _ _, upper_set.compl_le_compl }
end has_le
/-! #### Map -/
section
variables [preorder α] [preorder β] [preorder γ]
namespace upper_set
variables {f : α ≃o β} {s t : upper_set α} {a : α} {b : β}
/-- An order isomorphism of preorders induces an order isomorphism of their upper sets. -/
def map (f : α ≃o β) : upper_set α ≃o upper_set β :=
{ to_fun := λ s, ⟨f '' s, s.upper.image f⟩,
inv_fun := λ t, ⟨f ⁻¹' t, t.upper.preimage f.monotone⟩,
left_inv := λ _, ext $ f.preimage_image _,
right_inv := λ _, ext $ f.image_preimage _,
map_rel_iff' := λ s t, image_subset_image_iff f.injective }
@[simp] lemma symm_map (f : α ≃o β) : (map f).symm = map f.symm :=
fun_like.ext _ _ $ λ s, ext $ set.preimage_equiv_eq_image_symm _ _
@[simp] lemma mem_map : b ∈ map f s ↔ f.symm b ∈ s :=
by { rw [←f.symm_symm, ←symm_map, f.symm_symm], refl }
@[simp] lemma map_refl : map (order_iso.refl α) = order_iso.refl _ := by { ext, simp }
@[simp] lemma map_map (g : β ≃o γ) (f : α ≃o β) : map g (map f s) = map (f.trans g) s :=
by { ext, simp }
variables (f s t)
@[simp, norm_cast] lemma coe_map : (map f s : set β) = f '' s := rfl
@[simp] protected lemma map_sup : map f (s ⊔ t) = map f s ⊔ map f t :=
ext $ (image_inter f.injective).symm
@[simp] protected lemma map_inf : map f (s ⊓ t) = map f s ⊓ map f t := ext $ image_union _ _ _
@[simp] protected lemma map_top : map f ⊤ = ⊤ := ext $ image_empty _
@[simp] protected lemma map_bot : map f ⊥ = ⊥ := ext $ image_univ_of_surjective f.surjective
@[simp] protected lemma map_Sup (S : set (upper_set α)) : map f (Sup S) = ⨆ s ∈ S, map f s :=
ext $ by { push_cast, exact image_Inter₂ f.bijective _ }
@[simp] protected lemma map_Inf (S : set (upper_set α)) : map f (Inf S) = ⨅ s ∈ S, map f s :=
ext $ by { push_cast, exact image_Union₂ _ _ }
@[simp] protected lemma map_supr (g : ι → upper_set α) : map f (⨆ i, g i) = ⨆ i, map f (g i) :=
ext $ by { push_cast, exact image_Inter f.bijective _ }
@[simp] protected lemma map_infi (g : ι → upper_set α) : map f (⨅ i, g i) = ⨅ i, map f (g i) :=
ext $ by { push_cast, exact image_Union }
end upper_set
namespace lower_set
variables {f : α ≃o β} {s t : lower_set α} {a : α} {b : β}
/-- An order isomorphism of preorders induces an order isomorphism of their lower sets. -/
def map (f : α ≃o β) : lower_set α ≃o lower_set β :=
{ to_fun := λ s, ⟨f '' s, s.lower.image f⟩,
inv_fun := λ t, ⟨f ⁻¹' t, t.lower.preimage f.monotone⟩,
left_inv := λ _, set_like.coe_injective $ f.preimage_image _,
right_inv := λ _, set_like.coe_injective $ f.image_preimage _,
map_rel_iff' := λ s t, image_subset_image_iff f.injective }
@[simp] lemma symm_map (f : α ≃o β) : (map f).symm = map f.symm :=
fun_like.ext _ _ $ λ s, set_like.coe_injective $ set.preimage_equiv_eq_image_symm _ _
@[simp] lemma mem_map {f : α ≃o β} {b : β} : b ∈ map f s ↔ f.symm b ∈ s :=
by { rw [←f.symm_symm, ←symm_map, f.symm_symm], refl }
@[simp] lemma map_refl : map (order_iso.refl α) = order_iso.refl _ := by { ext, simp }
@[simp] lemma map_map (g : β ≃o γ) (f : α ≃o β) : map g (map f s) = map (f.trans g) s :=
by { ext, simp }
variables (f s t)
@[simp, norm_cast] lemma coe_map : (map f s : set β) = f '' s := rfl
@[simp] protected lemma map_sup : map f (s ⊔ t) = map f s ⊔ map f t := ext $ image_union _ _ _
@[simp] protected lemma map_inf : map f (s ⊓ t) = map f s ⊓ map f t :=
ext $ (image_inter f.injective).symm
@[simp] protected lemma map_top : map f ⊤ = ⊤ := ext $ image_univ_of_surjective f.surjective
@[simp] protected lemma map_bot : map f ⊥ = ⊥ := ext $ image_empty _
@[simp] protected lemma map_Sup (S : set (lower_set α)) : map f (Sup S) = ⨆ s ∈ S, map f s :=
ext $ by { push_cast, exact image_Union₂ _ _ }
protected lemma map_Inf (S : set (lower_set α)) : map f (Inf S) = ⨅ s ∈ S, map f s :=
ext $ by { push_cast, exact image_Inter₂ f.bijective _ }
protected lemma map_supr (g : ι → lower_set α) : map f (⨆ i, g i) = ⨆ i, map f (g i) :=
ext $ by { push_cast, exact image_Union }
protected lemma map_infi (g : ι → lower_set α) : map f (⨅ i, g i) = ⨅ i, map f (g i) :=
ext $ by { push_cast, exact image_Inter f.bijective _ }
end lower_set
namespace upper_set
@[simp] lemma compl_map (f : α ≃o β) (s : upper_set α) :
(map f s).compl = lower_set.map f s.compl :=
set_like.coe_injective (set.image_compl_eq f.bijective).symm
end upper_set
namespace lower_set
@[simp] lemma compl_map (f : α ≃o β) (s : lower_set α) :
(map f s).compl = upper_set.map f s.compl :=
set_like.coe_injective (set.image_compl_eq f.bijective).symm
end lower_set
end
/-! #### Principal sets -/
namespace upper_set
section preorder
variables [preorder α] [preorder β] {s : upper_set α} {a b : α}
/-- The smallest upper set containing a given element. -/
def Ici (a : α) : upper_set α := ⟨Ici a, is_upper_set_Ici a⟩
/-- The smallest upper set containing a given element. -/
def Ioi (a : α) : upper_set α := ⟨Ioi a, is_upper_set_Ioi a⟩
@[simp] lemma coe_Ici (a : α) : ↑(Ici a) = set.Ici a := rfl
@[simp] lemma coe_Ioi (a : α) : ↑(Ioi a) = set.Ioi a := rfl
@[simp] lemma mem_Ici_iff : b ∈ Ici a ↔ a ≤ b := iff.rfl
@[simp] lemma mem_Ioi_iff : b ∈ Ioi a ↔ a < b := iff.rfl
@[simp] lemma map_Ici (f : α ≃o β) (a : α) : map f (Ici a) = Ici (f a) := by { ext, simp }
@[simp] lemma map_Ioi (f : α ≃o β) (a : α) : map f (Ioi a) = Ioi (f a) := by { ext, simp }
lemma Ici_le_Ioi (a : α) : Ici a ≤ Ioi a := Ioi_subset_Ici_self
@[simp] lemma Ioi_top [order_top α] : Ioi (⊤ : α) = ⊤ := set_like.coe_injective Ioi_top
@[simp] lemma Ici_bot [order_bot α] : Ici (⊥ : α) = ⊥ := set_like.coe_injective Ici_bot
end preorder
section semilattice_sup
variables [semilattice_sup α]
@[simp] lemma Ici_sup (a b : α) : Ici (a ⊔ b) = Ici a ⊔ Ici b := ext Ici_inter_Ici.symm
/-- `upper_set.Ici` as a `sup_hom`. -/
def Ici_sup_hom : sup_hom α (upper_set α) := ⟨Ici, Ici_sup⟩
@[simp] lemma Ici_sup_hom_apply (a : α) : Ici_sup_hom a = (Ici a) := rfl
end semilattice_sup
section complete_lattice
variables [complete_lattice α]
@[simp] lemma Ici_Sup (S : set α) : Ici (Sup S) = ⨆ a ∈ S, Ici a :=
set_like.ext $ λ c, by simp only [mem_Ici_iff, mem_supr_iff, Sup_le_iff]
@[simp] lemma Ici_supr (f : ι → α) : Ici (⨆ i, f i) = ⨆ i, Ici (f i) :=
set_like.ext $ λ c, by simp only [mem_Ici_iff, mem_supr_iff, supr_le_iff]
@[simp] lemma Ici_supr₂ (f : Π i, κ i → α) : Ici (⨆ i j, f i j) = ⨆ i j, Ici (f i j) :=
by simp_rw Ici_supr
/-- `upper_set.Ici` as a `Sup_hom`. -/
def Ici_Sup_hom : Sup_hom α (upper_set α) := ⟨Ici, λ s, (Ici_Sup s).trans Sup_image.symm⟩
@[simp] lemma Ici_Sup_hom_apply (a : α) : Ici_Sup_hom a = to_dual (Ici a) := rfl
end complete_lattice
end upper_set
namespace lower_set
section preorder
variables [preorder α] [preorder β] {s : lower_set α} {a b : α}
/-- Principal lower set. `set.Iic` as a lower set. The smallest lower set containing a given
element. -/
def Iic (a : α) : lower_set α := ⟨Iic a, is_lower_set_Iic a⟩
/-- Strict principal lower set. `set.Iio` as a lower set. -/
def Iio (a : α) : lower_set α := ⟨Iio a, is_lower_set_Iio a⟩
@[simp] lemma coe_Iic (a : α) : ↑(Iic a) = set.Iic a := rfl
@[simp] lemma coe_Iio (a : α) : ↑(Iio a) = set.Iio a := rfl
@[simp] lemma mem_Iic_iff : b ∈ Iic a ↔ b ≤ a := iff.rfl
@[simp] lemma mem_Iio_iff : b ∈ Iio a ↔ b < a := iff.rfl
@[simp] lemma map_Iic (f : α ≃o β) (a : α) : map f (Iic a) = Iic (f a) := by { ext, simp }
@[simp] lemma map_Iio (f : α ≃o β) (a : α) : map f (Iio a) = Iio (f a) := by { ext, simp }
lemma Ioi_le_Ici (a : α) : Ioi a ≤ Ici a := Ioi_subset_Ici_self
@[simp] lemma Iic_top [order_top α] : Iic (⊤ : α) = ⊤ := set_like.coe_injective Iic_top
@[simp] lemma Iio_bot [order_bot α] : Iio (⊥ : α) = ⊥ := set_like.coe_injective Iio_bot
end preorder
section semilattice_inf
variables [semilattice_inf α]
@[simp] lemma Iic_inf (a b : α) : Iic (a ⊓ b) = Iic a ⊓ Iic b :=
set_like.coe_injective Iic_inter_Iic.symm
/-- `lower_set.Iic` as an `inf_hom`. -/
def Iic_inf_hom : inf_hom α (lower_set α) := ⟨Iic, Iic_inf⟩
@[simp] lemma coe_Iic_inf_hom : (Iic_inf_hom : α → lower_set α) = Iic := rfl
@[simp] lemma Iic_inf_hom_apply (a : α) : Iic_inf_hom a = Iic a := rfl
end semilattice_inf
section complete_lattice
variables [complete_lattice α]
@[simp] lemma Iic_Inf (S : set α) : Iic (Inf S) = ⨅ a ∈ S, Iic a :=
set_like.ext $ λ c, by simp only [mem_Iic_iff, mem_infi₂_iff, le_Inf_iff]
@[simp] lemma Iic_infi (f : ι → α) : Iic (⨅ i, f i) = ⨅ i, Iic (f i) :=
set_like.ext $ λ c, by simp only [mem_Iic_iff, mem_infi_iff, le_infi_iff]
@[simp] lemma Iic_infi₂ (f : Π i, κ i → α) : Iic (⨅ i j, f i j) = ⨅ i j, Iic (f i j) :=
by simp_rw Iic_infi
/-- `lower_set.Iic` as an `Inf_hom`. -/
def Iic_Inf_hom : Inf_hom α (lower_set α) := ⟨Iic, λ s, (Iic_Inf s).trans Inf_image.symm⟩
@[simp] lemma coe_Iic_Inf_hom : (Iic_Inf_hom : α → lower_set α) = Iic := rfl
@[simp] lemma Iic_Inf_hom_apply (a : α) : Iic_Inf_hom a = Iic a := rfl
end complete_lattice
end lower_set
section closure
variables [preorder α] [preorder β] {s t : set α} {x : α}
/-- The greatest upper set containing a given set. -/
def upper_closure (s : set α) : upper_set α :=
⟨{x | ∃ a ∈ s, a ≤ x}, λ x y h, Exists₂.imp $ λ a _, h.trans'⟩
/-- The least lower set containing a given set. -/
def lower_closure (s : set α) : lower_set α :=
⟨{x | ∃ a ∈ s, x ≤ a}, λ x y h, Exists₂.imp $ λ a _, h.trans⟩
-- We do not tag those two as `simp` to respect the abstraction.
@[norm_cast] lemma coe_upper_closure (s : set α) : ↑(upper_closure s) = {x | ∃ a ∈ s, a ≤ x} := rfl
@[norm_cast] lemma coe_lower_closure (s : set α) : ↑(lower_closure s) = {x | ∃ a ∈ s, x ≤ a} := rfl
@[simp] lemma mem_upper_closure : x ∈ upper_closure s ↔ ∃ a ∈ s, a ≤ x := iff.rfl
@[simp] lemma mem_lower_closure : x ∈ lower_closure s ↔ ∃ a ∈ s, x ≤ a := iff.rfl
lemma subset_upper_closure : s ⊆ upper_closure s := λ x hx, ⟨x, hx, le_rfl⟩
lemma subset_lower_closure : s ⊆ lower_closure s := λ x hx, ⟨x, hx, le_rfl⟩
lemma upper_closure_min (h : s ⊆ t) (ht : is_upper_set t) : ↑(upper_closure s) ⊆ t :=
λ a ⟨b, hb, hba⟩, ht hba $ h hb
lemma lower_closure_min (h : s ⊆ t) (ht : is_lower_set t) : ↑(lower_closure s) ⊆ t :=
λ a ⟨b, hb, hab⟩, ht hab $ h hb
protected lemma is_upper_set.upper_closure (hs : is_upper_set s) : ↑(upper_closure s) = s :=
(upper_closure_min subset.rfl hs).antisymm subset_upper_closure
protected lemma is_lower_set.lower_closure (hs : is_lower_set s) : ↑(lower_closure s) = s :=
(lower_closure_min subset.rfl hs).antisymm subset_lower_closure
@[simp] protected lemma upper_set.upper_closure (s : upper_set α) : upper_closure (s : set α) = s :=
set_like.coe_injective s.2.upper_closure
@[simp] protected lemma lower_set.lower_closure (s : lower_set α) : lower_closure (s : set α) = s :=
set_like.coe_injective s.2.lower_closure
@[simp] lemma upper_closure_image (f : α ≃o β) :
upper_closure (f '' s) = upper_set.map f (upper_closure s) :=
begin
rw [←f.symm_symm, ←upper_set.symm_map, f.symm_symm],
ext,
simp [-upper_set.symm_map, upper_set.map, order_iso.symm, ←f.le_symm_apply],
end
@[simp] lemma lower_closure_image (f : α ≃o β) :
lower_closure (f '' s) = lower_set.map f (lower_closure s) :=
begin
rw [←f.symm_symm, ←lower_set.symm_map, f.symm_symm],
ext,
simp [-lower_set.symm_map, lower_set.map, order_iso.symm, ←f.symm_apply_le],
end
@[simp] lemma upper_set.infi_Ici (s : set α) : (⨅ a ∈ s, upper_set.Ici a) = upper_closure s :=
by { ext, simp }
@[simp] lemma lower_set.supr_Iic (s : set α) : (⨆ a ∈ s, lower_set.Iic a) = lower_closure s :=
by { ext, simp }
lemma gc_upper_closure_coe :
galois_connection (to_dual ∘ upper_closure : set α → (upper_set α)ᵒᵈ) (coe ∘ of_dual) :=
λ s t, ⟨λ h, subset_upper_closure.trans $ upper_set.coe_subset_coe.2 h,
λ h, upper_closure_min h t.upper⟩
lemma gc_lower_closure_coe : galois_connection (lower_closure : set α → lower_set α) coe :=
λ s t, ⟨λ h, subset_lower_closure.trans $ lower_set.coe_subset_coe.2 h,
λ h, lower_closure_min h t.lower⟩
/-- `upper_closure` forms a reversed Galois insertion with the coercion from upper sets to sets. -/
def gi_upper_closure_coe :
galois_insertion (to_dual ∘ upper_closure : set α → (upper_set α)ᵒᵈ) (coe ∘ of_dual) :=
{ choice := λ s hs, to_dual (⟨s, λ a b hab ha, hs ⟨a, ha, hab⟩⟩ : upper_set α),
gc := gc_upper_closure_coe,
le_l_u := λ _, subset_upper_closure,
choice_eq := λ s hs,
of_dual.injective $ set_like.coe_injective $ subset_upper_closure.antisymm hs }
/-- `lower_closure` forms a Galois insertion with the coercion from lower sets to sets. -/
def gi_lower_closure_coe : galois_insertion (lower_closure : set α → lower_set α) coe :=
{ choice := λ s hs, ⟨s, λ a b hba ha, hs ⟨a, ha, hba⟩⟩,
gc := gc_lower_closure_coe,
le_l_u := λ _, subset_lower_closure,
choice_eq := λ s hs, set_like.coe_injective $ subset_lower_closure.antisymm hs }
lemma upper_closure_anti : antitone (upper_closure : set α → upper_set α) :=
gc_upper_closure_coe.monotone_l
lemma lower_closure_mono : monotone (lower_closure : set α → lower_set α) :=
gc_lower_closure_coe.monotone_l
@[simp] lemma upper_closure_empty : upper_closure (∅ : set α) = ⊤ := by { ext, simp }
@[simp] lemma lower_closure_empty : lower_closure (∅ : set α) = ⊥ := by { ext, simp }
@[simp] lemma upper_closure_singleton (a : α) : upper_closure ({a} : set α) = upper_set.Ici a :=
by { ext, simp }
@[simp] lemma lower_closure_singleton (a : α) : lower_closure ({a} : set α) = lower_set.Iic a :=
by { ext, simp }
@[simp] lemma upper_closure_univ : upper_closure (univ : set α) = ⊥ :=
le_bot_iff.1 subset_upper_closure
@[simp] lemma lower_closure_univ : lower_closure (univ : set α) = ⊤ :=
top_le_iff.1 subset_lower_closure
@[simp] lemma upper_closure_eq_top_iff : upper_closure s = ⊤ ↔ s = ∅ :=
⟨λ h, subset_empty_iff.1 $ subset_upper_closure.trans (congr_arg coe h).subset,
by { rintro rfl, exact upper_closure_empty }⟩
@[simp] lemma lower_closure_eq_bot_iff : lower_closure s = ⊥ ↔ s = ∅ :=
⟨λ h, subset_empty_iff.1 $ subset_lower_closure.trans (congr_arg coe h).subset,
by { rintro rfl, exact lower_closure_empty }⟩
@[simp] lemma upper_closure_union (s t : set α) :
upper_closure (s ∪ t) = upper_closure s ⊓ upper_closure t :=
by { ext, simp [or_and_distrib_right, exists_or_distrib] }
@[simp] lemma lower_closure_union (s t : set α) :
lower_closure (s ∪ t) = lower_closure s ⊔ lower_closure t :=
by { ext, simp [or_and_distrib_right, exists_or_distrib] }
@[simp] lemma upper_closure_Union (f : ι → set α) :
upper_closure (⋃ i, f i) = ⨅ i, upper_closure (f i) :=
by { ext, simp [←exists_and_distrib_right, @exists_comm α] }
@[simp] lemma lower_closure_Union (f : ι → set α) :
lower_closure (⋃ i, f i) = ⨆ i, lower_closure (f i) :=
by { ext, simp [←exists_and_distrib_right, @exists_comm α] }
@[simp] lemma upper_closure_sUnion (S : set (set α)) :
upper_closure (⋃₀ S) = ⨅ s ∈ S, upper_closure s :=
by simp_rw [sUnion_eq_bUnion, upper_closure_Union]
@[simp] lemma lower_closure_sUnion (S : set (set α)) :
lower_closure (⋃₀ S) = ⨆ s ∈ S, lower_closure s :=
by simp_rw [sUnion_eq_bUnion, lower_closure_Union]
lemma set.ord_connected.upper_closure_inter_lower_closure (h : s.ord_connected) :
↑(upper_closure s) ∩ ↑(lower_closure s) = s :=
(subset_inter subset_upper_closure subset_lower_closure).antisymm' $ λ a ⟨⟨b, hb, hba⟩, c, hc, hac⟩,
h.out hb hc ⟨hba, hac⟩
lemma ord_connected_iff_upper_closure_inter_lower_closure :
s.ord_connected ↔ ↑(upper_closure s) ∩ ↑(lower_closure s) = s :=
begin
refine ⟨set.ord_connected.upper_closure_inter_lower_closure, λ h, _⟩,
rw ←h,
exact (upper_set.upper _).ord_connected.inter (lower_set.lower _).ord_connected,
end
end closure
/-! ### Product -/
section preorder
variables [preorder α] [preorder β] {s : set α} {t : set β} {x : α × β}
lemma is_upper_set.prod (hs : is_upper_set s) (ht : is_upper_set t) : is_upper_set (s ×ˢ t) :=
λ a b h ha, ⟨hs h.1 ha.1, ht h.2 ha.2⟩
lemma is_lower_set.prod (hs : is_lower_set s) (ht : is_lower_set t) : is_lower_set (s ×ˢ t) :=
λ a b h ha, ⟨hs h.1 ha.1, ht h.2 ha.2⟩
namespace upper_set
/-- The product of two upper sets as an upper set. -/
def prod (s : upper_set α) (t : upper_set β) : upper_set (α × β) := ⟨s ×ˢ t, s.2.prod t.2⟩
infixr (name := upper_set.prod) ` ×ˢ `:82 := prod
@[simp] lemma coe_prod (s : upper_set α) (t : upper_set β) : (↑(s ×ˢ t) : set (α × β)) = s ×ˢ t :=
rfl
@[simp] lemma mem_prod {s : upper_set α} {t : upper_set β} : x ∈ s ×ˢ t ↔ x.1 ∈ s ∧ x.2 ∈ t :=
iff.rfl
lemma Ici_prod (x : α × β) : Ici x = Ici x.1 ×ˢ Ici x.2 := rfl
@[simp] lemma Ici_prod_Ici (a : α) (b : β) : Ici a ×ˢ Ici b = Ici (a, b) := rfl
@[simp] lemma prod_top (s : upper_set α) : s ×ˢ (⊤ : upper_set β) = ⊤ := ext prod_empty
@[simp] lemma top_prod (t : upper_set β) : (⊤ : upper_set α) ×ˢ t = ⊤ := ext empty_prod
@[simp] lemma bot_prod_bot : (⊥ : upper_set α) ×ˢ (⊥ : upper_set β) = ⊥ := ext univ_prod_univ
end upper_set
namespace lower_set
/-- The product of two lower sets as a lower set. -/
def prod (s : lower_set α) (t : lower_set β) : lower_set (α × β) := ⟨s ×ˢ t, s.2.prod t.2⟩
infixr (name := lower_set.prod) ` ×ˢ `:82 := lower_set.prod
@[simp] lemma coe_prod (s : lower_set α) (t : lower_set β) : (↑(s ×ˢ t) : set (α × β)) = s ×ˢ t :=
rfl
@[simp] lemma mem_prod {s : lower_set α} {t : lower_set β} : x ∈ s ×ˢ t ↔ x.1 ∈ s ∧ x.2 ∈ t :=
iff.rfl
lemma Iic_prod (x : α × β) : Iic x = Iic x.1 ×ˢ Iic x.2 := rfl
@[simp] lemma Ici_prod_Ici (a : α) (b : β) : Iic a ×ˢ Iic b = Iic (a, b) := rfl
@[simp] lemma prod_bot (s : lower_set α) : s ×ˢ (⊥ : lower_set β) = ⊥ := ext prod_empty
@[simp] lemma bot_prod (t : lower_set β) : (⊥ : lower_set α) ×ˢ t = ⊥ := ext empty_prod
@[simp] lemma top_prod_top : (⊤ : lower_set α) ×ˢ (⊤ : lower_set β) = ⊤ := ext univ_prod_univ
end lower_set
@[simp] lemma upper_closure_prod (s : set α) (t : set β) :
upper_closure (s ×ˢ t) = upper_closure s ×ˢ upper_closure t :=
by { ext, simp [prod.le_def, and_and_and_comm _ (_ ∈ t)] }
@[simp] lemma lower_closure_prod (s : set α) (t : set β) :
lower_closure (s ×ˢ t) = lower_closure s ×ˢ lower_closure t :=
by { ext, simp [prod.le_def, and_and_and_comm _ (_ ∈ t)] }
end preorder
|
02c8be7a5a33f59db2b9318b1cc8c8841c5c6f03 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/dynamics/circle/rotation_number/translation_number.lean | 08b8371ff5ff9647fce269daf81053ceed5b0bfe | [
"Apache-2.0"
] | permissive | dupuisf/mathlib | 62de4ec6544bf3b79086afd27b6529acfaf2c1bb | 8582b06b0a5d06c33ee07d0bdf7c646cae22cf36 | refs/heads/master | 1,669,494,854,016 | 1,595,692,409,000 | 1,595,692,409,000 | 272,046,630 | 0 | 0 | Apache-2.0 | 1,592,066,143,000 | 1,592,066,142,000 | null | UTF-8 | Lean | false | false | 28,225 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Yury G. Kudryashov
-/
import analysis.specific_limits
import order.iterate
import algebra.iterate_hom
/-!
# Translation number of a monotone real map that commutes with `x ↦ x + 1`
Let `f : ℝ → ℝ` be a monotone map such that `f (x + 1) = f x + 1` for all `x`. Then the limit
$$
\tau(f)=\lim_{n\to\infty}{f^n(x)-x}{n}
$$
exists and does not depend on `x`. This number is called the *translation number* of `f`.
Different authors use different notation for this number: `τ`, `ρ`, `rot`, etc
In this file we define a structure `circle_deg1_lift` for bundled maps with these properties, define
translation number of `f : circle_deg1_lift`, prove some estimates relating `f^n(x)-x` to `τ(f)`. In
case of a continuous map `f` we also prove that `f` admits a point `x` such that `f^n(x)=x+m` if and
only if `τ(f)=m/n`.
Maps of this type naturally appear as lifts of orientation preserving circle homeomorphisms. More
precisely, let `f` be an orientation preserving homeomorphism of the circle $S^1=ℝ/ℤ$, and
consider a real number `a` such that
`⟦a⟧ = f 0`, where `⟦⟧` means the natural projection `ℝ → ℝ/ℤ`. Then there exists a unique
continuous function `F : ℝ → ℝ` such that `F 0 = a` and `⟦F x⟧ = f ⟦x⟧` for all `x` (this fact is
not formalized yet). This function is strictly monotone, continuous, and satisfies
`F (x + 1) = F x + 1`. The number `⟦τ F⟧ : ℝ / ℤ` is called the *rotation number* of `f`.
It does not depend on the choice of `a`.
We chose to define translation number for a wider class of maps `f : ℝ → ℝ` for two reasons:
* non-strictly monotone circle self-maps with discontinuities naturally appear as Poincaré maps
for some flows on the two-torus (e.g., one can take a constant flow and glue in a few Cherry
cells);
* definition and some basic properties still work for this class.
## Notation
We use a local notation `τ` for the translation number of `f : circle_deg1_lift`.
## Tags
circle homeomorphism, rotation number
-/
open filter set
open_locale topological_space classical
/-!
### Definition and monoid structure
-/
/-- A lift of a monotone degree one map `S¹ → S¹`. -/
structure circle_deg1_lift : Type :=
(to_fun : ℝ → ℝ)
(monotone' : monotone to_fun)
(map_add_one' : ∀ x, to_fun (x + 1) = to_fun x + 1)
namespace circle_deg1_lift
instance : has_coe_to_fun circle_deg1_lift := ⟨λ _, ℝ → ℝ, circle_deg1_lift.to_fun⟩
@[simp] lemma coe_mk (f h₁ h₂) : ⇑(mk f h₁ h₂) = f := rfl
variables (f g : circle_deg1_lift)
protected lemma monotone : monotone f := f.monotone'
@[mono] lemma mono {x y} (h : x ≤ y) : f x ≤ f y := f.monotone h
@[simp] lemma map_add_one : ∀ x, f (x + 1) = f x + 1 := f.map_add_one'
@[simp] lemma map_one_add (x : ℝ) : f (1 + x) = 1 + f x := by rw [add_comm, map_add_one, add_comm]
theorem coe_inj : ∀ ⦃f g : circle_deg1_lift ⦄, (f : ℝ → ℝ) = g → f = g :=
assume ⟨f, fm, fd⟩ ⟨g, gm, gd⟩ h, by congr; exact h
@[ext] theorem ext ⦃f g : circle_deg1_lift ⦄ (h : ∀ x, f x = g x) : f = g :=
coe_inj $ funext h
theorem ext_iff {f g : circle_deg1_lift} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, ext h⟩
instance : monoid circle_deg1_lift :=
{ mul := λ f g,
{ to_fun := f ∘ g,
monotone' := f.monotone.comp g.monotone,
map_add_one' := λ x, by simp [map_add_one] },
one := ⟨id, monotone_id, λ _, rfl⟩,
mul_one := λ f, coe_inj $ function.comp.right_id f,
one_mul := λ f, coe_inj $ function.comp.left_id f,
mul_assoc := λ f₁ f₂ f₃, coe_inj rfl }
instance : inhabited circle_deg1_lift := ⟨1⟩
@[simp] lemma coe_mul : ⇑(f * g) = f ∘ g := rfl
lemma mul_apply (x) : (f * g) x = f (g x) := rfl
@[simp] lemma coe_one : ⇑(1 : circle_deg1_lift) = id := rfl
instance units_has_coe_to_fun : has_coe_to_fun (units circle_deg1_lift) :=
⟨λ _, ℝ → ℝ, λ f, ⇑(f : circle_deg1_lift)⟩
@[simp, norm_cast] lemma units_coe (f : units circle_deg1_lift) : ⇑(f : circle_deg1_lift) = f := rfl
lemma coe_pow : ∀ n : ℕ, ⇑(f^n) = (f^[n])
| 0 := rfl
| (n+1) := by {ext x, simp [coe_pow n, pow_succ'] }
lemma semiconj_by_iff_semiconj {f g₁ g₂ : circle_deg1_lift} :
semiconj_by f g₁ g₂ ↔ function.semiconj f g₁ g₂ :=
ext_iff
lemma commute_iff_commute {f g : circle_deg1_lift} :
commute f g ↔ function.commute f g :=
ext_iff
/-!
### Translate by a constant
-/
/-- The map `y ↦ x + y` as a `circle_deg1_lift`. More precisely, we define a homomorphism from
`multiplicative ℝ` to `units circle_deg1_lift`, so the translation by `x` is
`translation (multiplicative.of_add x)`. -/
def translate : multiplicative ℝ →* units circle_deg1_lift :=
by refine (units.map _).comp (to_units $ multiplicative ℝ).to_monoid_hom; exact
{ to_fun := λ x, ⟨λ y, x.to_add + y, λ y₁ y₂ h, add_le_add_left h _, λ y, (add_assoc _ _ _).symm⟩,
map_one' := ext $ zero_add,
map_mul' := λ x y, ext $ add_assoc _ _ }
@[simp] lemma translate_apply (x y : ℝ) : translate (multiplicative.of_add x) y = x + y := rfl
@[simp]
lemma translate_inv_apply (x y : ℝ) : (translate $ multiplicative.of_add x)⁻¹ y = -x + y := rfl
@[simp] lemma translate_gpow (x : ℝ) (n : ℤ) :
(translate (multiplicative.of_add x))^n = translate (multiplicative.of_add $ n * x) :=
by simp only [← gsmul_eq_mul, of_add_gsmul, monoid_hom.map_gpow]
@[simp] lemma translate_pow (x : ℝ) (n : ℕ) :
(translate (multiplicative.of_add x))^n = translate (multiplicative.of_add $ n * x) :=
translate_gpow x n
@[simp] lemma translate_iterate (x : ℝ) (n : ℕ) :
(translate (multiplicative.of_add x))^[n] = translate (multiplicative.of_add $ n * x) :=
by rw [← units_coe, ← coe_pow, ← units.coe_pow, translate_pow, units_coe]
/-!
### Commutativity with integer translations
In this section we prove that `f` commutes with translations by an integer number. First we formulate
these statements (for a natural or an integer number, addition on the left or on the right, addition
or subtraction) using `function.commute`, then reformulate as `simp` lemmas `map_int_add` etc.
-/
lemma commute_nat_add (n : ℕ) : function.commute f ((+) n) :=
by simpa only [nsmul_one, add_left_iterate] using function.commute.iterate_right f.map_one_add n
lemma commute_add_nat (n : ℕ) : function.commute f (λ x, x + n) :=
by simp only [add_comm _ (n:ℝ), f.commute_nat_add n]
lemma commute_sub_nat (n : ℕ) : function.commute f (λ x, x - n) :=
(f.commute_add_nat n).inverses_right (equiv.add_right _).right_inv (equiv.add_right _).left_inv
lemma commute_add_int : ∀ n : ℤ, function.commute f (λ x, x + n)
| (n:ℕ) := f.commute_add_nat n
| -[1+n] := f.commute_sub_nat (n + 1)
lemma commute_int_add (n : ℤ) : function.commute f ((+) n) :=
by simpa only [add_comm _ (n:ℝ)] using f.commute_add_int n
lemma commute_sub_int (n : ℤ) : function.commute f (λ x, x - n) :=
(f.commute_add_int n).inverses_right (equiv.add_right _).right_inv (equiv.add_right _).left_inv
@[simp] lemma map_int_add (m : ℤ) (x : ℝ) : f (m + x) = m + f x :=
f.commute_int_add m x
@[simp] lemma map_add_int (x : ℝ) (m : ℤ) : f (x + m) = f x + m :=
f.commute_add_int m x
@[simp] lemma map_sub_int (x : ℝ) (n : ℤ) : f (x - n) = f x - n :=
f.commute_sub_int n x
@[simp] lemma map_add_nat (x : ℝ) (n : ℕ) : f (x + n) = f x + n :=
f.map_add_int x n
@[simp] lemma map_nat_add (n : ℕ) (x : ℝ) : f (n + x) = n + f x :=
f.map_int_add n x
@[simp] lemma map_sub_nat (x : ℝ) (n : ℕ) : f (x - n) = f x - n :=
f.map_sub_int x n
lemma map_int_of_map_zero (n : ℤ) : f n = f 0 + n :=
by rw [← f.map_add_int, zero_add]
@[simp] lemma map_fract_sub_fract_eq (x : ℝ) :
f (fract x) - fract x = f x - x :=
by conv_rhs { rw [← fract_add_floor x, f.map_add_int, add_sub_comm, sub_self, add_zero] }
/-!
### Pointwise order on circle maps
-/
/-- Monotone circle maps form a lattice with respect to the pointwise order -/
noncomputable instance : lattice circle_deg1_lift :=
{ sup := λ f g,
{ to_fun := λ x, max (f x) (g x),
monotone' := λ x y h, max_le_max (f.mono h) (g.mono h), -- TODO: generalize to `monotone.max`
map_add_one' := λ x, by simp [max_add_add_right] },
le := λ f g, ∀ x, f x ≤ g x,
le_refl := λ f x, le_refl (f x),
le_trans := λ f₁ f₂ f₃ h₁₂ h₂₃ x, le_trans (h₁₂ x) (h₂₃ x),
le_antisymm := λ f₁ f₂ h₁₂ h₂₁, ext $ λ x, le_antisymm (h₁₂ x) (h₂₁ x),
le_sup_left := λ f g x, le_max_left (f x) (g x),
le_sup_right := λ f g x, le_max_right (f x) (g x),
sup_le := λ f₁ f₂ f₃ h₁ h₂ x, max_le (h₁ x) (h₂ x),
inf := λ f g,
{ to_fun := λ x, min (f x) (g x),
monotone' := λ x y h, min_le_min (f.mono h) (g.mono h),
map_add_one' := λ x, by simp [min_add_add_right] },
inf_le_left := λ f g x, min_le_left (f x) (g x),
inf_le_right := λ f g x, min_le_right (f x) (g x),
le_inf := λ f₁ f₂ f₃ h₂ h₃ x, le_min (h₂ x) (h₃ x) }
@[simp] lemma sup_apply (x : ℝ) : (f ⊔ g) x = max (f x) (g x) := rfl
@[simp] lemma inf_apply (x : ℝ) : (f ⊓ g) x = min (f x) (g x) := rfl
lemma iterate_monotone (n : ℕ) : monotone (λ f : circle_deg1_lift, f^[n]) :=
λ f g h, f.monotone.iterate_le_of_le h _
lemma iterate_mono {f g : circle_deg1_lift} (h : f ≤ g) (n : ℕ) : f^[n] ≤ (g^[n]) :=
iterate_monotone n h
lemma pow_mono {f g : circle_deg1_lift} (h : f ≤ g) (n : ℕ) : f^n ≤ g^n :=
λ x, by simp only [coe_pow, iterate_mono h n x]
lemma pow_monotone (n : ℕ) : monotone (λ f : circle_deg1_lift, f^n) :=
λ f g h, pow_mono h n
/-!
### Estimates on `(f * g) 0`
We prove the estimates `f 0 + ⌊g 0⌋ ≤ f (g 0) ≤ f 0 + ⌈g 0⌉` and some corollaries with added/removed
floors and ceils.
We also prove that for two semiconjugate maps `g₁`, `g₂`, the distance between `g₁ 0` and `g₂ 0`
is less than two.
-/
lemma map_le_of_map_zero (x : ℝ) : f x ≤ f 0 + ⌈x⌉ :=
calc f x ≤ f ⌈x⌉ : f.monotone $ le_ceil _
... = f 0 + ⌈x⌉ : f.map_int_of_map_zero _
lemma map_map_zero_le : f (g 0) ≤ f 0 + ⌈g 0⌉ := f.map_le_of_map_zero (g 0)
lemma floor_map_map_zero_le : ⌊f (g 0)⌋ ≤ ⌊f 0⌋ + ⌈g 0⌉ :=
calc ⌊f (g 0)⌋ ≤ ⌊f 0 + ⌈g 0⌉⌋ : floor_mono $ f.map_map_zero_le g
... = ⌊f 0⌋ + ⌈g 0⌉ : floor_add_int _ _
lemma ceil_map_map_zero_le : ⌈f (g 0)⌉ ≤ ⌈f 0⌉ + ⌈g 0⌉ :=
calc ⌈f (g 0)⌉ ≤ ⌈f 0 + ⌈g 0⌉⌉ : ceil_mono $ f.map_map_zero_le g
... = ⌈f 0⌉ + ⌈g 0⌉ : ceil_add_int _ _
lemma map_map_zero_lt : f (g 0) < f 0 + g 0 + 1 :=
calc f (g 0) ≤ f 0 + ⌈g 0⌉ : f.map_map_zero_le g
... < f 0 + (g 0 + 1) : add_lt_add_left (ceil_lt_add_one _) _
... = f 0 + g 0 + 1 : (add_assoc _ _ _).symm
lemma le_map_of_map_zero (x : ℝ) : f 0 + ⌊x⌋ ≤ f x :=
calc f 0 + ⌊x⌋ = f ⌊x⌋ : (f.map_int_of_map_zero _).symm
... ≤ f x : f.monotone $ floor_le _
lemma le_map_map_zero : f 0 + ⌊g 0⌋ ≤ f (g 0) := f.le_map_of_map_zero (g 0)
lemma le_floor_map_map_zero : ⌊f 0⌋ + ⌊g 0⌋ ≤ ⌊f (g 0)⌋ :=
calc ⌊f 0⌋ + ⌊g 0⌋ = ⌊f 0 + ⌊g 0⌋⌋ : (floor_add_int _ _).symm
... ≤ ⌊f (g 0)⌋ : floor_mono $ f.le_map_map_zero g
lemma le_ceil_map_map_zero : ⌈f 0⌉ + ⌊g 0⌋ ≤ ⌈(f * g) 0⌉ :=
calc ⌈f 0⌉ + ⌊g 0⌋ = ⌈f 0 + ⌊g 0⌋⌉ : (ceil_add_int _ _).symm
... ≤ ⌈f (g 0)⌉ : ceil_mono $ f.le_map_map_zero g
lemma lt_map_map_zero : f 0 + g 0 - 1 < f (g 0) :=
calc f 0 + g 0 - 1 = f 0 + (g 0 - 1) : add_assoc _ _ _
... < f 0 + ⌊g 0⌋ : add_lt_add_left (sub_one_lt_floor _) _
... ≤ f (g 0) : f.le_map_map_zero g
lemma dist_map_map_zero_lt : dist (f 0 + g 0) (f (g 0)) < 1 :=
begin
rw [dist_comm, real.dist_eq, abs_lt, lt_sub_iff_add_lt', sub_lt_iff_lt_add'],
exact ⟨f.lt_map_map_zero g, f.map_map_zero_lt g⟩
end
lemma dist_map_zero_lt_of_semiconj {f g₁ g₂ : circle_deg1_lift} (h : function.semiconj f g₁ g₂) :
dist (g₁ 0) (g₂ 0) < 2 :=
calc dist (g₁ 0) (g₂ 0) ≤ dist (g₁ 0) (f (g₁ 0) - f 0) + dist _ (g₂ 0) : dist_triangle _ _ _
... = dist (f 0 + g₁ 0) (f (g₁ 0)) + dist (g₂ 0 + f 0) (g₂ (f 0)) :
by simp only [h.eq, real.dist_eq, sub_sub, add_comm (f 0), sub_sub_assoc_swap, abs_sub (g₂ (f 0))]
... < 2 : add_lt_add (f.dist_map_map_zero_lt g₁) (g₂.dist_map_map_zero_lt f)
lemma dist_map_zero_lt_of_semiconj_by {f g₁ g₂ : circle_deg1_lift} (h : semiconj_by f g₁ g₂) :
dist (g₁ 0) (g₂ 0) < 2 :=
dist_map_zero_lt_of_semiconj $ semiconj_by_iff_semiconj.1 h
/-!
### Estimates on `(f^n) x`
If we know that `f x` is `≤`/`<`/`≥`/`>`/`=` to `x + m`, then we have a similar estimate on
`f^[n] x` and `x + n * m`.
For `≤`, `≥`, and `=` we formulate both `of` (implication) and `iff` versions because implications
work for `n = 0`. For `<` and `>` we formulate only `iff` versions.
-/
lemma iterate_le_of_map_le_add_int {x : ℝ} {m : ℤ} (h : f x ≤ x + m) (n : ℕ) :
f^[n] x ≤ x + n * m :=
by simpa only [nsmul_eq_mul, add_right_iterate]
using (f.commute_add_int m).iterate_le_of_map_le f.monotone (monotone_id.add_const m) h n
lemma le_iterate_of_add_int_le_map {x : ℝ} {m : ℤ} (h : x + m ≤ f x) (n : ℕ) :
x + n * m ≤ (f^[n]) x :=
by simpa only [nsmul_eq_mul, add_right_iterate]
using (f.commute_add_int m).symm.iterate_le_of_map_le (monotone_id.add_const m) f.monotone h n
lemma iterate_eq_of_map_eq_add_int {x : ℝ} {m : ℤ} (h : f x = x + m) (n : ℕ) :
f^[n] x = x + n * m :=
by simpa only [nsmul_eq_mul, add_right_iterate]
using (f.commute_add_int m).iterate_eq_of_map_eq n h
lemma iterate_pos_le_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) :
f^[n] x ≤ x + n * m ↔ f x ≤ x + m :=
by simpa only [nsmul_eq_mul, add_right_iterate]
using (f.commute_add_int m).iterate_pos_le_iff_map_le f.monotone (strict_mono_id.add_const m) hn
lemma iterate_pos_lt_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) :
f^[n] x < x + n * m ↔ f x < x + m :=
by simpa only [nsmul_eq_mul, add_right_iterate]
using (f.commute_add_int m).iterate_pos_lt_iff_map_lt f.monotone (strict_mono_id.add_const m) hn
lemma iterate_pos_eq_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) :
f^[n] x = x + n * m ↔ f x = x + m :=
by simpa only [nsmul_eq_mul, add_right_iterate]
using (f.commute_add_int m).iterate_pos_eq_iff_map_eq f.monotone (strict_mono_id.add_const m) hn
lemma le_iterate_pos_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) :
x + n * m ≤ (f^[n]) x ↔ x + m ≤ f x :=
by simpa only [not_lt] using not_congr (f.iterate_pos_lt_iff hn)
lemma lt_iterate_pos_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) :
x + n * m < (f^[n]) x ↔ x + m < f x :=
by simpa only [not_le] using not_congr (f.iterate_pos_le_iff hn)
lemma mul_floor_map_zero_le_floor_iterate_zero (n : ℕ) : ↑n * ⌊f 0⌋ ≤ ⌊(f^[n] 0)⌋ :=
begin
rw [le_floor, int.cast_mul, int.cast_coe_nat, ← zero_add ((n : ℝ) * _)],
apply le_iterate_of_add_int_le_map,
simp [floor_le]
end
/-!
### Definition of translation number
-/
noncomputable theory
/-- An auxiliary sequence used to define the translation number. -/
def transnum_aux_seq (n : ℕ) : ℝ := (f^(2^n)) 0 / 2^n
/-- The translation number of a `circle_deg1_lift`, $τ(f)=\lim_{n→∞}\frac{f^n(x)-x}{n}$. We use
an auxiliary sequence `\frac{f^{2^n}(0)}{2^n}` to define `τ(f)` because some proofs are simpler
this way. -/
def translation_number : ℝ :=
lim at_top f.transnum_aux_seq
-- TODO: choose two different symbols for `circle_deg1_lift.translation_number` and the future
-- `circle_mono_homeo.rotation_number`, then make them `localized notation`s
local notation `τ` := translation_number
lemma transnum_aux_seq_def : f.transnum_aux_seq = λ n : ℕ, (f^(2^n)) 0 / 2^n := rfl
lemma translation_number_eq_of_tendsto_aux {τ' : ℝ}
(h : tendsto f.transnum_aux_seq at_top (𝓝 τ')) :
τ f = τ' :=
h.lim_eq
lemma translation_number_eq_of_tendsto₀ {τ' : ℝ}
(h : tendsto (λ n:ℕ, f^[n] 0 / n) at_top (𝓝 τ')) :
τ f = τ' :=
f.translation_number_eq_of_tendsto_aux $
by simpa [(∘), transnum_aux_seq_def, coe_pow]
using h.comp (nat.tendsto_pow_at_top_at_top_of_one_lt one_lt_two)
lemma translation_number_eq_of_tendsto₀' {τ' : ℝ}
(h : tendsto (λ n:ℕ, f^[n + 1] 0 / (n + 1)) at_top (𝓝 τ')) :
τ f = τ' :=
f.translation_number_eq_of_tendsto₀ $ (tendsto_add_at_top_iff_nat 1).1 h
lemma transnum_aux_seq_zero : f.transnum_aux_seq 0 = f 0 := by simp [transnum_aux_seq]
lemma transnum_aux_seq_dist_lt (n : ℕ) :
dist (f.transnum_aux_seq n) (f.transnum_aux_seq (n+1)) < (1 / 2) / (2^n) :=
begin
have : 0 < (2^(n+1):ℝ) := pow_pos zero_lt_two _,
rw [div_div_eq_div_mul, ← pow_succ, ← abs_of_pos this],
replace := abs_pos_iff.2 (ne_of_gt this),
convert (div_lt_div_right this).2 ((f^(2^n)).dist_map_map_zero_lt (f^(2^n))),
simp_rw [transnum_aux_seq, real.dist_eq],
rw [← abs_div, sub_div, pow_succ, ← two_mul, mul_div_mul_left _ _ (@two_ne_zero ℝ _),
nat.pow_succ, pow_mul, pow_two, mul_apply]
end
lemma tendsto_translation_number_aux : tendsto f.transnum_aux_seq at_top (𝓝 $ τ f) :=
(cauchy_seq_of_le_geometric_two 1 (λ n, le_of_lt $ f.transnum_aux_seq_dist_lt n)).tendsto_lim
lemma dist_map_zero_translation_number_le : dist (f 0) (τ f) ≤ 1 :=
f.transnum_aux_seq_zero ▸ dist_le_of_le_geometric_two_of_tendsto₀ 1
(λ n, le_of_lt $ f.transnum_aux_seq_dist_lt n) f.tendsto_translation_number_aux
lemma tendsto_translation_number_of_dist_bounded_aux (x : ℕ → ℝ) (C : ℝ)
(H : ∀ n : ℕ, dist ((f^n) 0) (x n) ≤ C) :
tendsto (λ n : ℕ, x (2^n) / (2^n)) at_top (𝓝 $ τ f) :=
begin
refine f.tendsto_translation_number_aux.congr_dist (squeeze_zero (λ _, dist_nonneg) _ _),
{ exact λ n, C / 2^n },
{ intro n,
have : 0 < (2^n:ℝ) := pow_pos zero_lt_two _,
convert (div_le_div_right this).2 (H (2^n)),
rw [transnum_aux_seq, real.dist_eq, ← sub_div, abs_div, abs_of_pos this, real.dist_eq] },
{ exact mul_zero C ▸ tendsto_const_nhds.mul (tendsto_inv_at_top_zero.comp $
tendsto_pow_at_top_at_top_of_one_lt one_lt_two) }
end
lemma translation_number_eq_of_dist_bounded {f g : circle_deg1_lift} (C : ℝ)
(H : ∀ n : ℕ, dist ((f^n) 0) ((g^n) 0) ≤ C) :
τ f = τ g :=
eq.symm $ g.translation_number_eq_of_tendsto_aux $
f.tendsto_translation_number_of_dist_bounded_aux _ C H
@[simp] lemma translation_number_map_id : τ 1 = 0 :=
translation_number_eq_of_tendsto₀ _ $ by simp [tendsto_const_nhds]
lemma translation_number_eq_of_semiconj_by {f g₁ g₂ : circle_deg1_lift} (H : semiconj_by f g₁ g₂) :
τ g₁ = τ g₂ :=
translation_number_eq_of_dist_bounded 2 $ λ n, le_of_lt $
dist_map_zero_lt_of_semiconj_by $ H.pow_right n
lemma translation_number_eq_of_semiconj {f g₁ g₂ : circle_deg1_lift}
(H : function.semiconj f g₁ g₂) :
τ g₁ = τ g₂ :=
translation_number_eq_of_semiconj_by $ semiconj_by_iff_semiconj.2 H
lemma translation_number_mul_of_commute {f g : circle_deg1_lift} (h : commute f g) :
τ (f * g) = τ f + τ g :=
begin
have : tendsto (λ n : ℕ, ((λ k, (f^k) 0 + (g^k) 0) (2^n)) / (2^n)) at_top (𝓝 $ τ f + τ g) :=
((f.tendsto_translation_number_aux.add g.tendsto_translation_number_aux).congr $
λ n, (add_div ((f^(2^n)) 0) ((g^(2^n)) 0) ((2:ℝ)^n)).symm),
refine tendsto_nhds_unique
((f * g).tendsto_translation_number_of_dist_bounded_aux _ 1 (λ n, _))
this,
rw [h.mul_pow, dist_comm],
exact le_of_lt ((f^n).dist_map_map_zero_lt (g^n))
end
@[simp] lemma translation_number_pow :
∀ n : ℕ, τ (f^n) = n * τ f
| 0 := by simp
| (n+1) := by rw [pow_succ', translation_number_mul_of_commute (commute.pow_self f n),
translation_number_pow n, nat.cast_add_one, add_mul, one_mul]
@[simp] lemma translation_number_conj_eq (f : units circle_deg1_lift) (g : circle_deg1_lift) :
τ (↑f * g * ↑(f⁻¹)) = τ g :=
(translation_number_eq_of_semiconj_by (f.mk_semiconj_by g)).symm
@[simp] lemma translation_number_conj_eq' (f : units circle_deg1_lift) (g : circle_deg1_lift) :
τ (↑(f⁻¹) * g * f) = τ g :=
translation_number_conj_eq f⁻¹ g
lemma dist_pow_map_zero_mul_translation_number_le (n:ℕ) :
dist ((f^n) 0) (n * f.translation_number) ≤ 1 :=
f.translation_number_pow n ▸ (f^n).dist_map_zero_translation_number_le
lemma tendsto_translation_number₀' :
tendsto (λ n:ℕ, (f^(n+1)) 0 / (n+1)) at_top (𝓝 $ τ f) :=
begin
refine (tendsto_iff_dist_tendsto_zero.2 $ squeeze_zero (λ _, dist_nonneg) (λ n, _)
((tendsto_const_div_at_top_nhds_0_nat 1).comp (tendsto_add_at_top_nat 1))),
dsimp,
have : (0:ℝ) < n + 1 := n.cast_add_one_pos,
rw [real.dist_eq, div_sub' _ _ _ (ne_of_gt this), abs_div, ← real.dist_eq, abs_of_pos this,
div_le_div_right this, ← nat.cast_add_one],
apply dist_pow_map_zero_mul_translation_number_le
end
lemma tendsto_translation_number₀ :
tendsto (λ n:ℕ, ((f^n) 0) / n) at_top (𝓝 $ τ f) :=
(tendsto_add_at_top_iff_nat 1).1 f.tendsto_translation_number₀'
/-- For any `x : ℝ` the sequence $\frac{f^n(x)-x}{n}$ tends to the translation number of `f`.
In particular, this limit does not depend on `x`. -/
lemma tendsto_translation_number (x : ℝ) :
tendsto (λ n:ℕ, ((f^n) x - x) / n) at_top (𝓝 $ τ f) :=
begin
rw [← translation_number_conj_eq' (translate $ multiplicative.of_add x)],
convert tendsto_translation_number₀ _,
ext n,
simp [sub_eq_neg_add, units.conj_pow']
end
lemma tendsto_translation_number' (x : ℝ) :
tendsto (λ n:ℕ, ((f^(n+1)) x - x) / (n+1)) at_top (𝓝 $ τ f) :=
(tendsto_add_at_top_iff_nat 1).2 (f.tendsto_translation_number x)
lemma translation_number_mono : monotone τ :=
λ f g h, le_of_tendsto_of_tendsto' f.tendsto_translation_number₀
g.tendsto_translation_number₀ $ λ n, div_le_div_of_le_of_nonneg (pow_mono h n 0) n.cast_nonneg
lemma translation_number_translate (x : ℝ) :
τ (translate $ multiplicative.of_add x) = x :=
translation_number_eq_of_tendsto₀' _ $
by simp [nat.cast_add_one_ne_zero, mul_div_cancel_left, tendsto_const_nhds]
lemma translation_number_le_of_le_add {z : ℝ} (hz : ∀ x, f x ≤ x + z) : τ f ≤ z :=
translation_number_translate z ▸ translation_number_mono
(λ x, trans_rel_left _ (hz x) (add_comm _ _))
lemma le_translation_number_of_add_le {z : ℝ} (hz : ∀ x, x + z ≤ f x) : z ≤ τ f :=
translation_number_translate z ▸ translation_number_mono
(λ x, trans_rel_right _ (add_comm _ _) (hz x))
lemma translation_number_le_of_le_add_int {x : ℝ} {m : ℤ} (h : f x ≤ x + m) : τ f ≤ m :=
le_of_tendsto' (f.tendsto_translation_number' x) $ λ n,
div_le_of_le_mul n.cast_add_one_pos $ sub_le_iff_le_add'.2 $
(coe_pow f (n + 1)).symm ▸ f.iterate_le_of_map_le_add_int h (n + 1)
lemma translation_number_le_of_le_add_nat {x : ℝ} {m : ℕ} (h : f x ≤ x + m) : τ f ≤ m :=
@translation_number_le_of_le_add_int f x m h
lemma le_translation_number_of_add_int_le {x : ℝ} {m : ℤ} (h : x + m ≤ f x) : ↑m ≤ τ f :=
ge_of_tendsto' (f.tendsto_translation_number' x) $ λ n,
le_div_of_mul_le n.cast_add_one_pos $ le_sub_iff_add_le'.2 $
by simp only [coe_pow, mul_comm (m:ℝ), ← nat.cast_add_one, f.le_iterate_of_add_int_le_map h]
lemma le_translation_number_of_add_nat_le {x : ℝ} {m : ℕ} (h : x + m ≤ f x) : ↑m ≤ τ f :=
@le_translation_number_of_add_int_le f x m h
/-- If `f x - x` is an integer number `m` for some point `x`, then `τ f = m`.
On the circle this means that a map with a fixed point has rotation number zero. -/
lemma translation_number_of_eq_add_int {x : ℝ} {m : ℤ} (h : f x = x + m) : τ f = m :=
le_antisymm (translation_number_le_of_le_add_int f $ le_of_eq h)
(le_translation_number_of_add_int_le f $ le_of_eq h.symm)
lemma floor_sub_le_translation_number (x : ℝ) : ↑⌊f x - x⌋ ≤ τ f :=
le_translation_number_of_add_int_le f $ le_sub_iff_add_le'.1 (floor_le $ f x - x)
lemma translation_number_le_ceil_sub (x : ℝ) : τ f ≤ ⌈f x - x⌉ :=
translation_number_le_of_le_add_int f $ sub_le_iff_le_add'.1 (le_ceil $ f x - x)
lemma map_lt_of_translation_number_lt_int {n : ℤ} (h : τ f < n) (x : ℝ) : f x < x + n :=
not_le.1 $ mt f.le_translation_number_of_add_int_le $ not_le.2 h
lemma map_lt_of_translation_number_lt_nat {n : ℕ} (h : τ f < n) (x : ℝ) : f x < x + n :=
@map_lt_of_translation_number_lt_int f n h x
lemma lt_map_of_int_lt_translation_number {n : ℤ} (h : ↑n < τ f) (x : ℝ) : x + n < f x :=
not_le.1 $ mt f.translation_number_le_of_le_add_int $ not_le.2 h
lemma lt_map_of_nat_lt_translation_number {n : ℕ} (h : ↑n < τ f) (x : ℝ) : x + n < f x :=
@lt_map_of_int_lt_translation_number f n h x
/-- If `f^n x - x`, `n > 0`, is an integer number `m` for some point `x`, then
`τ f = m / n`. On the circle this means that a map with a periodic orbit has
a rational rotation number. -/
lemma translation_number_of_map_pow_eq_add_int {x : ℝ} {n : ℕ} {m : ℤ}
(h : (f^n) x = x + m) (hn : 0 < n) :
τ f = m / n :=
begin
have := (f^n).translation_number_of_eq_add_int h,
rwa [translation_number_pow, mul_comm, ← eq_div_iff] at this,
exact nat.cast_ne_zero.2 (ne_of_gt hn)
end
/-- If a predicate depends only on `f x - x` and holds for all `0 ≤ x ≤ 1`,
then it holds for all `x`. -/
lemma forall_map_sub_of_Icc (P : ℝ → Prop)
(h : ∀ x ∈ Icc (0:ℝ) 1, P (f x - x)) (x : ℝ) : P (f x - x) :=
f.map_fract_sub_fract_eq x ▸ h _ ⟨fract_nonneg _, le_of_lt (fract_lt_one _)⟩
lemma translation_number_lt_of_forall_lt_add (hf : continuous f) {z : ℝ}
(hz : ∀ x, f x < x + z) : τ f < z :=
begin
obtain ⟨x, xmem, hx⟩ : ∃ x ∈ Icc (0:ℝ) 1, ∀ y ∈ Icc (0:ℝ) 1, f y - y ≤ f x - x,
from compact_Icc.exists_forall_ge (nonempty_Icc.2 zero_le_one)
(hf.sub continuous_id).continuous_on,
refine lt_of_le_of_lt _ (sub_lt_iff_lt_add'.2 $ hz x),
apply translation_number_le_of_le_add,
simp only [← sub_le_iff_le_add'],
exact f.forall_map_sub_of_Icc (λ a, a ≤ f x - x) hx
end
lemma lt_translation_number_of_forall_add_lt (hf : continuous f) {z : ℝ}
(hz : ∀ x, x + z < f x) : z < τ f :=
begin
obtain ⟨x, xmem, hx⟩ : ∃ x ∈ Icc (0:ℝ) 1, ∀ y ∈ Icc (0:ℝ) 1, f x - x ≤ f y - y,
from compact_Icc.exists_forall_le (nonempty_Icc.2 zero_le_one)
(hf.sub continuous_id).continuous_on,
refine lt_of_lt_of_le (lt_sub_iff_add_lt'.2 $ hz x) _,
apply le_translation_number_of_add_le,
simp only [← le_sub_iff_add_le'],
exact f.forall_map_sub_of_Icc _ hx
end
/-- If `f` is a continuous monotone map `ℝ → ℝ`, `f (x + 1) = f x + 1`, then there exists `x`
such that `f x = x + τ f`. -/
lemma exists_eq_add_translation_number (hf : continuous f) :
∃ x, f x = x + τ f :=
begin
obtain ⟨a, ha⟩ : ∃ x, f x ≤ x + f.translation_number,
{ by_contradiction H,
push_neg at H,
exact lt_irrefl _ (f.lt_translation_number_of_forall_add_lt hf H) },
obtain ⟨b, hb⟩ : ∃ x, x + τ f ≤ f x,
{ by_contradiction H,
push_neg at H,
exact lt_irrefl _ (f.translation_number_lt_of_forall_lt_add hf H) },
exact intermediate_value_univ₂ hf (continuous_id.add continuous_const) ha hb
end
lemma translation_number_eq_int_iff (hf : continuous f) {m : ℤ} :
τ f = m ↔ ∃ x, f x = x + m :=
begin
refine ⟨λ h, h ▸ f.exists_eq_add_translation_number hf, _⟩,
rintros ⟨x, hx⟩,
exact f.translation_number_of_eq_add_int hx
end
lemma continuous_pow (hf : continuous f) (n : ℕ) :
continuous ⇑(f^n : circle_deg1_lift) :=
by { rw coe_pow, exact hf.iterate n }
lemma translation_number_eq_rat_iff (hf : continuous f) {m : ℤ}
{n : ℕ} (hn : 0 < n) :
τ f = m / n ↔ ∃ x, (f^n) x = x + m :=
begin
rw [eq_div_iff, mul_comm, ← translation_number_pow]; [skip, exact ne_of_gt (nat.cast_pos.2 hn)],
exact (f^n).translation_number_eq_int_iff (f.continuous_pow hf n)
end
end circle_deg1_lift
|
88669b092d3ae1558b4a2c209bbf036fbea5a96b | 94e33a31faa76775069b071adea97e86e218a8ee | /src/order/minimal.lean | 60050cdc2e8a200bb26d770e81e463315b080df0 | [
"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 | 5,733 | lean | /-
Copyright (c) 2022 Yaël Dillies. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies
-/
import order.antichain
/-!
# Minimal/maximal elements of a set
This file defines minimal and maximal of a set with respect to an arbitrary relation.
## Main declarations
* `maximals r s`: Maximal elements of `s` with respect to `r`.
* `minimals r s`: Minimal elements of `s` with respect to `r`.
## TODO
Do we need a `finset` version?
-/
open function set
variables {α : Type*} (r r₁ r₂ : α → α → Prop) (s t : set α) (a : α)
/-- Turns a set into an antichain by keeping only the "maximal" elements. -/
def maximals : set α := {a ∈ s | ∀ ⦃b⦄, b ∈ s → r a b → a = b}
/-- Turns a set into an antichain by keeping only the "minimal" elements. -/
def minimals : set α := {a ∈ s | ∀ ⦃b⦄, b ∈ s → r b a → a = b}
lemma maximals_subset : maximals r s ⊆ s := sep_subset _ _
lemma minimals_subset : minimals r s ⊆ s := sep_subset _ _
@[simp] lemma maximals_empty : maximals r ∅ = ∅ := sep_empty _
@[simp] lemma minimals_empty : minimals r ∅ = ∅ := sep_empty _
@[simp] lemma maximals_singleton : maximals r {a} = {a} :=
(maximals_subset _ _).antisymm $ singleton_subset_iff.2 $ ⟨rfl, λ b hb _, hb.symm⟩
@[simp] lemma minimals_singleton : minimals r {a} = {a} := maximals_singleton _ _
lemma maximals_swap : maximals (swap r) s = minimals r s := rfl
lemma minimals_swap : minimals (swap r) s = maximals r s := rfl
lemma maximals_antichain : is_antichain r (maximals r s) := λ a ha b hb hab h, hab $ ha.2 hb.1 h
lemma minimals_antichain : is_antichain r (minimals r s) := (maximals_antichain _ _).swap
lemma maximals_eq_minimals [is_symm α r] : maximals r s = minimals r s :=
by { congr, ext a b, exact comm }
variables {r r₁ r₂ s t a}
lemma set.subsingleton.maximals_eq (h : s.subsingleton) : maximals r s = s :=
h.induction_on (minimals_empty _) (maximals_singleton _)
lemma set.subsingleton.minimals_eq (h : s.subsingleton) : minimals r s = s := h.maximals_eq
lemma maximals_mono (h : ∀ a b, r₁ a b → r₂ a b) : maximals r₂ s ⊆ maximals r₁ s :=
λ a ha, ⟨ha.1, λ b hb, ha.2 hb ∘ h _ _⟩
lemma minimals_mono (h : ∀ a b, r₁ a b → r₂ a b) : minimals r₂ s ⊆ minimals r₁ s :=
λ a ha, ⟨ha.1, λ b hb, ha.2 hb ∘ h _ _⟩
lemma maximals_union : maximals r (s ∪ t) ⊆ maximals r s ∪ maximals r t :=
begin
intros a ha,
obtain h | h := ha.1,
{ exact or.inl ⟨h, λ b hb, ha.2 $ or.inl hb⟩ },
{ exact or.inr ⟨h, λ b hb, ha.2 $ or.inr hb⟩ }
end
lemma minimals_union : minimals r (s ∪ t) ⊆ minimals r s ∪ minimals r t := maximals_union
lemma maximals_inter_subset : maximals r s ∩ t ⊆ maximals r (s ∩ t) :=
λ a ha, ⟨⟨ha.1.1, ha.2⟩, λ b hb, ha.1.2 hb.1⟩
lemma minimals_inter_subset : minimals r s ∩ t ⊆ minimals r (s ∩ t) := maximals_inter_subset
lemma inter_maximals_subset : s ∩ maximals r t ⊆ maximals r (s ∩ t) :=
λ a ha, ⟨⟨ha.1, ha.2.1⟩, λ b hb, ha.2.2 hb.2⟩
lemma inter_minimals_subset : s ∩ minimals r t ⊆ minimals r (s ∩ t) := inter_maximals_subset
lemma _root_.is_antichain.maximals_eq (h : is_antichain r s) : maximals r s = s :=
(maximals_subset _ _).antisymm $ λ a ha, ⟨ha, λ b, h.eq ha⟩
lemma _root_.is_antichain.minimals_eq (h : is_antichain r s) : minimals r s = s :=
(minimals_subset _ _).antisymm $ λ a ha, ⟨ha, λ b, h.eq' ha⟩
@[simp] lemma maximals_idem : maximals r (maximals r s) = maximals r s :=
(maximals_antichain _ _).maximals_eq
@[simp] lemma minimals_idem : minimals r (minimals r s) = minimals r s := maximals_idem
/-- If `maximals r s` is included in but *shadows* the antichain `t`, then it is actually
equal to `t`. -/
lemma is_antichain.max_maximals (ht : is_antichain r t) (h : maximals r s ⊆ t)
(hs : ∀ ⦃a⦄, a ∈ t → ∃ b ∈ maximals r s, r b a) :
maximals r s = t :=
begin
refine h.antisymm (λ a ha, _),
obtain ⟨b, hb, hr⟩ := hs ha,
rwa of_not_not (λ hab, ht (h hb) ha (ne.symm hab) hr),
end
/-- If `minimals r s` is included in but *shadows* the antichain `t`, then it is actually
equal to `t`. -/
lemma is_antichain.max_minimals (ht : is_antichain r t) (h : minimals r s ⊆ t)
(hs : ∀ ⦃a⦄, a ∈ t → ∃ b ∈ minimals r s, r a b) :
minimals r s = t :=
begin
refine h.antisymm (λ a ha, _),
obtain ⟨b, hb, hr⟩ := hs ha,
rwa of_not_not (λ hab, ht ha (h hb) hab hr),
end
variables [partial_order α]
lemma is_least.mem_minimals (h : is_least s a) : a ∈ minimals (≤) s :=
⟨h.1, λ b hb, (h.2 hb).antisymm⟩
lemma is_greatest.mem_maximals (h : is_greatest s a) : a ∈ maximals (≤) s :=
⟨h.1, λ b hb, (h.2 hb).antisymm'⟩
lemma is_least.minimals_eq (h : is_least s a) : minimals (≤) s = {a} :=
eq_singleton_iff_unique_mem.2 ⟨h.mem_minimals, λ b hb, hb.2 h.1 $ h.2 hb.1⟩
lemma is_greatest.maximals_eq (h : is_greatest s a) : maximals (≤) s = {a} :=
eq_singleton_iff_unique_mem.2 ⟨h.mem_maximals, λ b hb, hb.2 h.1 $ h.2 hb.1⟩
lemma is_antichain.max_lower_set_of (hs : is_antichain (≤) s) :
maximals (≤) {x | ∃ y ∈ s, x ≤ y} = s :=
begin
ext x,
simp only [maximals, exists_prop, mem_set_of_eq, forall_exists_index, and_imp, sep_set_of],
refine ⟨λ h, exists.elim h.1 (λ y hy, ((h.2 _ hy.1 rfl.le hy.2).symm.subst hy.1)),
λ h, ⟨⟨x,h,rfl.le⟩,λ b y hy hby hxy, _⟩⟩,
have : x = y := by_contra (λ h_eq, (hs h hy h_eq (hxy.trans hby)).elim),
exact hxy.antisymm (this.symm.subst hby),
end
lemma is_antichain.min_upper_set_of (hs : is_antichain (≤) s) :
minimals (≤) {x | ∃ y ∈ s, y ≤ x} = s :=
hs.to_dual.max_lower_set_of
|
d0bd478d31c648833ac17aee7172ee4fe0c7a376 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/topology/algebra/group_with_zero.lean | 02e40080d1cb3f705551b7c3f195ad7385bc2622 | [
"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 | 12,385 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import topology.algebra.monoid
import algebra.group.pi
import topology.homeomorph
/-!
# Topological group with zero
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we define `has_continuous_inv₀` to be a mixin typeclass a type with `has_inv` and
`has_zero` (e.g., a `group_with_zero`) such that `λ x, x⁻¹` is continuous at all nonzero points. Any
normed (semi)field has this property. Currently the only example of `has_continuous_inv₀` in
`mathlib` which is not a normed field is the type `nnnreal` (a.k.a. `ℝ≥0`) of nonnegative real
numbers.
Then we prove lemmas about continuity of `x ↦ x⁻¹` and `f / g` providing dot-style `*.inv'` and
`*.div` operations on `filter.tendsto`, `continuous_at`, `continuous_within_at`, `continuous_on`,
and `continuous`. As a special case, we provide `*.div_const` operations that require only
`group_with_zero` and `has_continuous_mul` instances.
All lemmas about `(⁻¹)` use `inv'` in their names because lemmas without `'` are used for
`topological_group`s. We also use `'` in the typeclass name `has_continuous_inv₀` for the sake of
consistency of notation.
On a `group_with_zero` with continuous multiplication, we also define left and right multiplication
as homeomorphisms.
-/
open_locale topology filter
open filter function
/-!
### A group with zero with continuous multiplication
If `G₀` is a group with zero with continuous `(*)`, then `(/y)` is continuous for any `y`. In this
section we prove lemmas that immediately follow from this fact providing `*.div_const` dot-style
operations on `filter.tendsto`, `continuous_at`, `continuous_within_at`, `continuous_on`, and
`continuous`.
-/
variables {α β G₀ : Type*}
section div_const
variables [group_with_zero G₀] [topological_space G₀] [has_continuous_mul G₀]
{f : α → G₀} {s : set α} {l : filter α}
lemma filter.tendsto.div_const {x : G₀} (hf : tendsto f l (𝓝 x)) (y : G₀) :
tendsto (λa, f a / y) l (𝓝 (x / y)) :=
by simpa only [div_eq_mul_inv] using hf.mul tendsto_const_nhds
variables [topological_space α]
lemma continuous_at.div_const {a : α} (hf : continuous_at f a) (y : G₀) :
continuous_at (λ x, f x / y) a :=
by simpa only [div_eq_mul_inv] using hf.mul continuous_at_const
lemma continuous_within_at.div_const {a} (hf : continuous_within_at f s a) (y : G₀) :
continuous_within_at (λ x, f x / y) s a :=
hf.div_const _
lemma continuous_on.div_const (hf : continuous_on f s) (y : G₀) : continuous_on (λ x, f x / y) s :=
by simpa only [div_eq_mul_inv] using hf.mul continuous_on_const
@[continuity] lemma continuous.div_const (hf : continuous f) (y : G₀) :
continuous (λ x, f x / y) :=
by simpa only [div_eq_mul_inv] using hf.mul continuous_const
end div_const
/-- A type with `0` and `has_inv` such that `λ x, x⁻¹` is continuous at all nonzero points. Any
normed (semi)field has this property. -/
class has_continuous_inv₀ (G₀ : Type*) [has_zero G₀] [has_inv G₀] [topological_space G₀] : Prop :=
(continuous_at_inv₀ : ∀ ⦃x : G₀⦄, x ≠ 0 → continuous_at has_inv.inv x)
export has_continuous_inv₀ (continuous_at_inv₀)
section inv₀
variables [has_zero G₀] [has_inv G₀] [topological_space G₀] [has_continuous_inv₀ G₀]
{l : filter α} {f : α → G₀} {s : set α} {a : α}
/-!
### Continuity of `λ x, x⁻¹` at a non-zero point
We define `topological_group_with_zero` to be a `group_with_zero` such that the operation `x ↦ x⁻¹`
is continuous at all nonzero points. In this section we prove dot-style `*.inv'` lemmas for
`filter.tendsto`, `continuous_at`, `continuous_within_at`, `continuous_on`, and `continuous`.
-/
lemma tendsto_inv₀ {x : G₀} (hx : x ≠ 0) : tendsto has_inv.inv (𝓝 x) (𝓝 x⁻¹) :=
continuous_at_inv₀ hx
lemma continuous_on_inv₀ : continuous_on (has_inv.inv : G₀ → G₀) {0}ᶜ :=
λ x hx, (continuous_at_inv₀ hx).continuous_within_at
/-- If a function converges to a nonzero value, its inverse converges to the inverse of this value.
We use the name `tendsto.inv₀` as `tendsto.inv` is already used in multiplicative topological
groups. -/
lemma filter.tendsto.inv₀ {a : G₀} (hf : tendsto f l (𝓝 a))
(ha : a ≠ 0) :
tendsto (λ x, (f x)⁻¹) l (𝓝 a⁻¹) :=
(tendsto_inv₀ ha).comp hf
variables [topological_space α]
lemma continuous_within_at.inv₀ (hf : continuous_within_at f s a) (ha : f a ≠ 0) :
continuous_within_at (λ x, (f x)⁻¹) s a :=
hf.inv₀ ha
lemma continuous_at.inv₀ (hf : continuous_at f a) (ha : f a ≠ 0) :
continuous_at (λ x, (f x)⁻¹) a :=
hf.inv₀ ha
@[continuity] lemma continuous.inv₀ (hf : continuous f) (h0 : ∀ x, f x ≠ 0) :
continuous (λ x, (f x)⁻¹) :=
continuous_iff_continuous_at.2 $ λ x, (hf.tendsto x).inv₀ (h0 x)
lemma continuous_on.inv₀ (hf : continuous_on f s) (h0 : ∀ x ∈ s, f x ≠ 0) :
continuous_on (λ x, (f x)⁻¹) s :=
λ x hx, (hf x hx).inv₀ (h0 x hx)
end inv₀
/-- If `G₀` is a group with zero with topology such that `x ↦ x⁻¹` is continuous at all nonzero
points. Then the coercion `Mˣ → M` is a topological embedding. -/
theorem units.embedding_coe₀ [group_with_zero G₀] [topological_space G₀] [has_continuous_inv₀ G₀] :
embedding (coe : G₀ˣ → G₀) :=
units.embedding_coe_mk $ continuous_on_inv₀.mono $ λ x, is_unit.ne_zero
/-!
### Continuity of division
If `G₀` is a `group_with_zero` with `x ↦ x⁻¹` continuous at all nonzero points and `(*)`, then
division `(/)` is continuous at any point where the denominator is continuous.
-/
section div
variables [group_with_zero G₀] [topological_space G₀] [has_continuous_inv₀ G₀]
[has_continuous_mul G₀] {f g : α → G₀}
lemma filter.tendsto.div {l : filter α} {a b : G₀} (hf : tendsto f l (𝓝 a))
(hg : tendsto g l (𝓝 b)) (hy : b ≠ 0) :
tendsto (f / g) l (𝓝 (a / b)) :=
by simpa only [div_eq_mul_inv] using hf.mul (hg.inv₀ hy)
lemma filter.tendsto_mul_iff_of_ne_zero [t1_space G₀]
{f g : α → G₀} {l : filter α} {x y : G₀}
(hg : tendsto g l (𝓝 y)) (hy : y ≠ 0) :
tendsto (λ n, f n * g n) l (𝓝 $ x * y) ↔ tendsto f l (𝓝 x) :=
begin
refine ⟨λ hfg, _, λ hf, hf.mul hg⟩,
rw ←mul_div_cancel x hy,
refine tendsto.congr' _ (hfg.div hg hy),
refine eventually.mp (hg.eventually_ne hy) (eventually_of_forall (λ n hn, mul_div_cancel _ hn)),
end
variables [topological_space α] [topological_space β] {s : set α} {a : α}
lemma continuous_within_at.div (hf : continuous_within_at f s a) (hg : continuous_within_at g s a)
(h₀ : g a ≠ 0) :
continuous_within_at (f / g) s a :=
hf.div hg h₀
lemma continuous_on.div (hf : continuous_on f s) (hg : continuous_on g s) (h₀ : ∀ x ∈ s, g x ≠ 0) :
continuous_on (f / g) s :=
λ x hx, (hf x hx).div (hg x hx) (h₀ x hx)
/-- Continuity at a point of the result of dividing two functions continuous at that point, where
the denominator is nonzero. -/
lemma continuous_at.div (hf : continuous_at f a) (hg : continuous_at g a) (h₀ : g a ≠ 0) :
continuous_at (f / g) a :=
hf.div hg h₀
@[continuity] lemma continuous.div (hf : continuous f) (hg : continuous g) (h₀ : ∀ x, g x ≠ 0) :
continuous (f / g) :=
by simpa only [div_eq_mul_inv] using hf.mul (hg.inv₀ h₀)
lemma continuous_on_div : continuous_on (λ p : G₀ × G₀, p.1 / p.2) {p | p.2 ≠ 0} :=
continuous_on_fst.div continuous_on_snd $ λ _, id
/-- The function `f x / g x` is discontinuous when `g x = 0`.
However, under appropriate conditions, `h x (f x / g x)` is still continuous.
The condition is that if `g a = 0` then `h x y` must tend to `h a 0` when `x` tends to `a`,
with no information about `y`. This is represented by the `⊤` filter.
Note: `filter.tendsto_prod_top_iff` characterizes this convergence in uniform spaces.
See also `filter.prod_top` and `filter.mem_prod_top`. -/
lemma continuous_at.comp_div_cases {f g : α → G₀} (h : α → G₀ → β)
(hf : continuous_at f a) (hg : continuous_at g a)
(hh : g a ≠ 0 → continuous_at ↿h (a, f a / g a))
(h2h : g a = 0 → tendsto ↿h (𝓝 a ×ᶠ ⊤) (𝓝 (h a 0))) :
continuous_at (λ x, h x (f x / g x)) a :=
begin
show continuous_at (↿h ∘ (λ x, (x, f x / g x))) a,
by_cases hga : g a = 0,
{ rw [continuous_at], simp_rw [comp_app, hga, div_zero],
exact (h2h hga).comp (continuous_at_id.prod_mk tendsto_top) },
{ exact continuous_at.comp (hh hga) (continuous_at_id.prod (hf.div hg hga)) }
end
/-- `h x (f x / g x)` is continuous under certain conditions, even if the denominator is sometimes
`0`. See docstring of `continuous_at.comp_div_cases`. -/
lemma continuous.comp_div_cases {f g : α → G₀} (h : α → G₀ → β)
(hf : continuous f) (hg : continuous g)
(hh : ∀ a, g a ≠ 0 → continuous_at ↿h (a, f a / g a))
(h2h : ∀ a, g a = 0 → tendsto ↿h (𝓝 a ×ᶠ ⊤) (𝓝 (h a 0))) :
continuous (λ x, h x (f x / g x)) :=
continuous_iff_continuous_at.mpr $
λ a, hf.continuous_at.comp_div_cases _ hg.continuous_at (hh a) (h2h a)
end div
/-! ### Left and right multiplication as homeomorphisms -/
namespace homeomorph
variables [topological_space α] [group_with_zero α] [has_continuous_mul α]
/-- Left multiplication by a nonzero element in a `group_with_zero` with continuous multiplication
is a homeomorphism of the underlying type. -/
protected def mul_left₀ (c : α) (hc : c ≠ 0) : α ≃ₜ α :=
{ continuous_to_fun := continuous_mul_left _,
continuous_inv_fun := continuous_mul_left _,
.. equiv.mul_left₀ c hc }
/-- Right multiplication by a nonzero element in a `group_with_zero` with continuous multiplication
is a homeomorphism of the underlying type. -/
protected def mul_right₀ (c : α) (hc : c ≠ 0) : α ≃ₜ α :=
{ continuous_to_fun := continuous_mul_right _,
continuous_inv_fun := continuous_mul_right _,
.. equiv.mul_right₀ c hc }
@[simp] lemma coe_mul_left₀ (c : α) (hc : c ≠ 0) : ⇑(homeomorph.mul_left₀ c hc) = (*) c := rfl
@[simp] lemma mul_left₀_symm_apply (c : α) (hc : c ≠ 0) :
((homeomorph.mul_left₀ c hc).symm : α → α) = (*) c⁻¹ := rfl
@[simp] lemma coe_mul_right₀ (c : α) (hc : c ≠ 0) :
⇑(homeomorph.mul_right₀ c hc) = λ x, x * c := rfl
@[simp] lemma mul_right₀_symm_apply (c : α) (hc : c ≠ 0) :
((homeomorph.mul_right₀ c hc).symm : α → α) = λ x, x * c⁻¹ := rfl
end homeomorph
section zpow
variables [group_with_zero G₀] [topological_space G₀] [has_continuous_inv₀ G₀]
[has_continuous_mul G₀]
lemma continuous_at_zpow₀ (x : G₀) (m : ℤ) (h : x ≠ 0 ∨ 0 ≤ m) : continuous_at (λ x, x ^ m) x :=
begin
cases m,
{ simpa only [zpow_of_nat] using continuous_at_pow x m },
{ simp only [zpow_neg_succ_of_nat],
have hx : x ≠ 0, from h.resolve_right (int.neg_succ_of_nat_lt_zero m).not_le,
exact (continuous_at_pow x (m + 1)).inv₀ (pow_ne_zero _ hx) }
end
lemma continuous_on_zpow₀ (m : ℤ) : continuous_on (λ x : G₀, x ^ m) {0}ᶜ :=
λ x hx, (continuous_at_zpow₀ _ _ (or.inl hx)).continuous_within_at
lemma filter.tendsto.zpow₀ {f : α → G₀} {l : filter α} {a : G₀} (hf : tendsto f l (𝓝 a)) (m : ℤ)
(h : a ≠ 0 ∨ 0 ≤ m) :
tendsto (λ x, (f x) ^ m) l (𝓝 (a ^ m)) :=
(continuous_at_zpow₀ _ m h).tendsto.comp hf
variables {X : Type*} [topological_space X] {a : X} {s : set X} {f : X → G₀}
lemma continuous_at.zpow₀ (hf : continuous_at f a) (m : ℤ) (h : f a ≠ 0 ∨ 0 ≤ m) :
continuous_at (λ x, (f x) ^ m) a :=
hf.zpow₀ m h
lemma continuous_within_at.zpow₀ (hf : continuous_within_at f s a) (m : ℤ) (h : f a ≠ 0 ∨ 0 ≤ m) :
continuous_within_at (λ x, f x ^ m) s a :=
hf.zpow₀ m h
lemma continuous_on.zpow₀ (hf : continuous_on f s) (m : ℤ) (h : ∀ a ∈ s, f a ≠ 0 ∨ 0 ≤ m) :
continuous_on (λ x, f x ^ m) s :=
λ a ha, (hf a ha).zpow₀ m (h a ha)
@[continuity] lemma continuous.zpow₀ (hf : continuous f) (m : ℤ) (h0 : ∀ a, f a ≠ 0 ∨ 0 ≤ m) :
continuous (λ x, (f x) ^ m) :=
continuous_iff_continuous_at.2 $ λ x, (hf.tendsto x).zpow₀ m (h0 x)
end zpow
|
6dc2c2b34047b42eca726d6f98bf6cec80108308 | 9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e | /src/undergraduate/MAS114/Semester 1/Q19.lean | d62e5fa9737cfd192deae9cb7fa22335a042e5a3 | [] | no_license | agusakov/lean_lib | c0e9cc29fc7d2518004e224376adeb5e69b5cc1a | f88d162da2f990b87c4d34f5f46bbca2bbc5948e | refs/heads/master | 1,642,141,461,087 | 1,557,395,798,000 | 1,557,395,798,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,711 | lean | import data.int.modeq data.fintype data.zmod.basic
import tactic.linarith
namespace MAS114
namespace exercises_1
namespace Q19
def f (n : ℤ) : ℤ := n * (n + 1) * (n + 2) * (n + 3)
lemma f_mod (p n₀ n₁ : ℤ) (h0 : n₀ ≡ n₁ [ZMOD p]) : f n₀ ≡ f n₁ [ZMOD p] :=
begin
have h1 : n₀ + 1 ≡ n₁ + 1 [ZMOD p] := int.modeq.modeq_add h0 rfl,
have h2 : n₀ + 2 ≡ n₁ + 2 [ZMOD p] := int.modeq.modeq_add h0 rfl,
have h3 : n₀ + 3 ≡ n₁ + 3 [ZMOD p] := int.modeq.modeq_add h0 rfl,
have h01 : n₀ * (n₀ + 1) ≡ n₁ * (n₁ + 1) [ZMOD p] :=
int.modeq.modeq_mul h0 h1,
have h02 : n₀ * (n₀ + 1) * (n₀ + 2) ≡ n₁ * (n₁ + 1) * (n₁ + 2) [ZMOD p] :=
int.modeq.modeq_mul h01 h2,
exact int.modeq.modeq_mul h02 h3,
end
lemma f_mod_3 (n : ℕ) : f n ≡ 0 [ZMOD 3] :=
begin
have three_pos : (3 : ℤ) > 0 := dec_trivial,
rcases int.modeq.exists_unique_equiv_nat n three_pos with ⟨r,⟨r_is_lt,r_equiv⟩⟩,
let e := (f_mod 3 r n r_equiv).symm,
suffices : f r ≡ 0 [ZMOD 3],
{exact e.trans this,},
rcases r with _ | _ | _ | r0; rw[f]; try {refl},
{exfalso,
have : (3 : ℤ) = ((3 : ℕ) : ℤ) := rfl,
rw[this] at r_is_lt,
let h0 := int.coe_nat_lt.mp r_is_lt,
replace h0 := nat.lt_of_succ_lt_succ h0,
replace h0 := nat.lt_of_succ_lt_succ h0,
replace h0 := nat.lt_of_succ_lt_succ h0,
exact nat.not_lt_zero r0 h0,
}
end
lemma f_mod_8 (n : ℕ) : f n ≡ 0 [ZMOD 8] :=
begin
have eight_pos : (8 : ℤ) > 0 := dec_trivial,
rcases int.modeq.exists_unique_equiv_nat n eight_pos with ⟨r,⟨r_is_lt,r_equiv⟩⟩,
let e := (f_mod 8 r n r_equiv).symm,
suffices : f r ≡ 0 [ZMOD 8],
{exact e.trans this,},
rcases r with _ | _ | _ | _ | _ | _ | _ | _ | r0;
rw[int.modeq,f]; try {norm_num},
{exfalso,
have : (8 : ℤ) = ((8 : ℕ) : ℤ) := rfl,
rw[this] at r_is_lt,
let h0 := int.coe_nat_lt.mp r_is_lt,
repeat { replace h0 := nat.lt_of_succ_lt_succ h0 },
exact nat.not_lt_zero r0 h0,
}
end
lemma f_mod_24 (n : ℕ) : f n ≡ 0 [ZMOD 24] :=
begin
let i3 : ℤ := 3,
let i8 : ℤ := 8,
have : (24 : ℤ) = i3 * i8 := rfl,
rw[this],
have cp : nat.coprime i3.nat_abs i8.nat_abs := dec_trivial,
exact (int.modeq.modeq_and_modeq_iff_modeq_mul cp).mp ⟨f_mod_3 n,f_mod_8 n⟩,
end
/- ----------- Part (ii) ------------- -/
def h {R : Type} [comm_ring R] (a b c d : R) : R :=
(a - b) * (a - c) * (a - d) * (b - c) * (b - d) * (c - d)
lemma h_shift {R : Type} [comm_ring R] (a b c d : R) :
h a b c d = h (a - d) (b - d) (c - d) 0 :=
begin
have e : ∀ x y z : R, (x + -z) + -(y + -z) = x + -y :=
by {intros, ring},
dsimp[h],
rw[neg_zero,add_zero,add_zero,add_zero],
rw[e,e,e],
end
lemma h_zero_shift {R : Type} [comm_ring R] :
(∀ a b c : R, h a b c 0 = 0) → (∀ a b c d : R, h a b c d = 0) :=
λ p a b c d, (h_shift a b c d).trans (p (a - d) (b - d) (c - d))
lemma h_zero_3 : ∀ a b c d : zmod 3, h a b c d = 0 :=
h_zero_shift dec_trivial
lemma h_zero_4 : ∀ a b c d : zmod 4, h a b c d = 0 :=
h_zero_shift dec_trivial
lemma h_map {R S : Type} [comm_ring R] [comm_ring S]
(φ : R → S) [is_ring_hom φ] (a b c d : R) :
φ (h a b c d) = h (φ a) (φ b) (φ c) (φ d) :=
begin
dsimp[h],
let em := @is_ring_hom.map_mul R S _ _ φ _,
let ea := @is_ring_hom.map_add R S _ _ φ _,
let en := @is_ring_hom.map_neg R S _ _ φ _,
rw[em,em,em,em,em],
rw[ea,ea,ea,ea,ea,ea],
rw[en,en,en],
end
lemma h_zero_mod (p : ℕ+) :
(∀ a b c d : zmod p, h a b c d = 0) →
(∀ a b c d : ℤ, h a b c d ≡ 0 [ZMOD p]) :=
begin
intros e a b c d,
apply (@zmod.eq_iff_modeq_int p _ _).mp,
let π : ℤ → (zmod p) := int.cast,
exact calc
π (h a b c d) = h (π a) (π b) (π c) (π d) :
by rw[@h_map ℤ (zmod p) _ _ π _]
... = 0 : e (π a) (π b) (π c) (π d),
end
lemma h_zero_12 : ∀ (a b c d : ℤ), h a b c d ≡ 0 [ZMOD 12] :=
begin
intros,
let i3 : ℤ := 3,
let i4 : ℤ := 4,
let h3 := h_zero_mod 3 h_zero_3 a b c d,
let h4 := h_zero_mod 4 h_zero_4 a b c d,
have : (12 : ℤ) = i3 * i4 := rfl,
rw[this],
have cp : nat.coprime i3.nat_abs i4.nat_abs := dec_trivial,
exact (int.modeq.modeq_and_modeq_iff_modeq_mul cp).mp ⟨h3,h4⟩,
end
/-
Here are partial results for a more general case
-/
def π (m : ℕ+) : ℤ → (zmod m) := int.cast
instance π_hom (m : ℕ+) : is_ring_hom (π m) :=
by { dsimp[π], apply_instance }
def F (n : ℕ) := { ij : (fin n) × (fin n) // ij.1.val < ij.2.val }
instance (n : ℕ) : fintype (F n) := by { dsimp[F], apply_instance,}
def g (n : ℕ) (u : (fin n) → ℤ) : ℤ :=
(@finset.univ (F n) _).prod (λ ij, (u ij.val.2) - (u ij.val.1))
def g_mod (n : ℕ) (m : ℕ+) (u : (fin n) → ℤ) : zmod m :=
(@finset.univ (F n) _).prod (λ ij, (π m (u ij.val.2)) - (π m (u ij.val.1)))
lemma g_mod_spec (n : ℕ) (m : ℕ+) (u : (fin n) → ℤ) :
π m (g n u) = g_mod n m u :=
begin
dsimp[g,g_mod],
let mn := @is_ring_hom.map_neg ℤ (zmod m) _ _ (π m) _,
let ma := @is_ring_hom.map_add ℤ (zmod m) _ _ (π m) _,
conv
begin
to_rhs,congr,skip,funext,rw[← mn,← ma],
end,
apply (finset.prod_hom (π m)).symm,
end
lemma must_repeat (n : ℕ) (m : ℕ+) (m_lt_n : m.val < n)
(u : fin n → ℤ) : ∃ i j : fin n, (i.val < j.val ∧ (π m (u i)) = (π m (u j))) :=
begin
let P := { ij : (fin n) × (fin n) // ij.1 ≠ ij.2 },
let p : P → Prop := λ ij, π m (u ij.val.1) = π m (u ij.val.2),
let q := exists ij, p ij,
by_cases h : q,
{
rcases h with ⟨⟨⟨i,j⟩,i_ne_j⟩,eq_mod⟩,
change i ≠ j at i_ne_j,
let i_ne_j_val : i.val ≠ j.val := λ e,i_ne_j (fin.eq_of_veq e),
change (π m (u i)) = (π m (u j)) at eq_mod,
by_cases hij : i.val < j.val,
{use i,use j,exact ⟨hij,eq_mod⟩},
{let hij' := lt_of_le_of_ne (le_of_not_gt hij) i_ne_j_val.symm,
use j,use i,exact ⟨hij',eq_mod.symm⟩
}
},{
exfalso,
let v : fin n → zmod m := λ i, π m (u i),
have v_inj : function.injective v :=
begin
intros i j ev,
by_cases hij : i = j,
{exact hij},
{exfalso,exact h ⟨⟨⟨i,j⟩,hij⟩,ev⟩,
}
end,
let e := calc
n = fintype.card (fin n) : (fintype.card_fin n).symm
... ≤ fintype.card (zmod m) : fintype.card_le_of_injective v v_inj
... = m.val : fintype.card_fin m
... < n : m_lt_n,
exact lt_irrefl _ e,
}
end
lemma g_mod_zero (n : ℕ) (m : ℕ+) (m_lt_n : m.val < n)
(u : fin n → ℤ) : g_mod n m u = 0 :=
begin
rcases must_repeat n m m_lt_n u with ⟨i,j,i_lt_j,e⟩,
dsimp[π] at e,
let ij : F n := ⟨⟨i,j⟩,i_lt_j⟩,
dsimp[g_mod],
apply finset.prod_eq_zero (finset.mem_univ ij),
dsimp[ij],
change π m (u i) = π m (u j) at e,
rw[e,add_right_neg],
end
end Q19
end exercises_1
end MAS114 |
f4715a4f9a2a8a237cb6cf2e4ad6f119473368eb | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /07_Induction_and_Recursion.org.16.lean | 1a974143c3d97b6892840ffea02a63ee63b1a713 | [] | no_license | cjmazey/lean-tutorial | ba559a49f82aa6c5848b9bf17b7389bf7f4ba645 | 381f61c9fcac56d01d959ae0fa6e376f2c4e3b34 | refs/heads/master | 1,610,286,098,832 | 1,447,124,923,000 | 1,447,124,923,000 | 43,082,433 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 320 | lean | /- page 106 -/
import standard
open nat
definition f : nat → nat → nat
| f 0 y := 1
| f x 0 := 2
| f (x+1) (y+1) := 3
-- BEGIN
variables (a b : nat)
example : f 0 0 = 1 := rfl
example : f 0 (a+1) = 1 := rfl
example : f (a+1) 0 = 2 := rfl
example : f (a+1) (b+1) = 3 := rfl
-- END
|
0fe6de5336a5fc5f53157b3210815883e078b610 | 367134ba5a65885e863bdc4507601606690974c1 | /src/data/polynomial/monomial.lean | 39a3a6122d67292f8f39a8de0df8ed97bd3873e8 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 3,406 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import data.polynomial.basic
/-!
# Univariate monomials
Preparatory lemmas for degree_basic.
-/
noncomputable theory
open finsupp
namespace polynomial
universes u
variables {R : Type u} {a b : R} {m n : ℕ}
variables [semiring R] {p q r : polynomial R}
/--
`C a` is the constant polynomial `a`.
`C` is provided as a ring homomorphism.
-/
def C : R →+* polynomial R := add_monoid_algebra.single_zero_ring_hom
@[simp] lemma monomial_zero_left (a : R) : monomial 0 a = C a := rfl
lemma C_0 : C (0 : R) = 0 := single_zero
lemma C_1 : C (1 : R) = 1 := rfl
lemma C_mul : C (a * b) = C a * C b := C.map_mul a b
lemma C_add : C (a + b) = C a + C b := C.map_add a b
@[simp] lemma C_bit0 : C (bit0 a) = bit0 (C a) := C_add
@[simp] lemma C_bit1 : C (bit1 a) = bit1 (C a) := by simp [bit1, C_bit0]
lemma C_pow : C (a ^ n) = C a ^ n := C.map_pow a n
@[simp] lemma C_mul_monomial : C a * monomial n b = monomial n (a * b) :=
by simp only [←monomial_zero_left, monomial_mul_monomial, zero_add]
@[simp] lemma monomial_mul_C : monomial n a * C b = monomial n (a * b) :=
by simp only [←monomial_zero_left, monomial_mul_monomial, add_zero]
@[simp]
lemma C_eq_nat_cast (n : ℕ) : C (n : R) = (n : polynomial R) :=
C.map_nat_cast n
@[simp]
lemma sum_C_index {a} {β} [add_comm_monoid β] {f : ℕ → R → β} (h : f 0 0 = 0) :
(C a).sum f = f 0 a :=
sum_single_index h
lemma coeff_C : coeff (C a) n = ite (n = 0) a 0 :=
by { convert coeff_monomial using 2, simp [eq_comm], }
@[simp] lemma coeff_C_zero : coeff (C a) 0 = a := coeff_monomial
lemma coeff_C_ne_zero (h : n ≠ 0) : (C a).coeff n = 0 :=
by rw [coeff_C, if_neg h]
theorem nontrivial.of_polynomial_ne (h : p ≠ q) : nontrivial R :=
⟨⟨0, 1, λ h01 : 0 = 1, h $
by rw [← mul_one p, ← mul_one q, ← C_1, ← h01, C_0, mul_zero, mul_zero] ⟩⟩
lemma single_eq_C_mul_X : ∀{n}, monomial n a = C a * X^n
| 0 := (mul_one _).symm
| (n+1) :=
calc monomial (n + 1) a = monomial n a * X : by { rw [X, monomial_mul_monomial, mul_one], }
... = (C a * X^n) * X : by rw [single_eq_C_mul_X]
... = C a * X^(n+1) : by simp only [pow_add, mul_assoc, pow_one]
@[simp] lemma C_inj : C a = C b ↔ a = b :=
⟨λ h, coeff_C_zero.symm.trans (h.symm ▸ coeff_C_zero), congr_arg C⟩
@[simp] lemma C_eq_zero : C a = 0 ↔ a = 0 :=
calc C a = 0 ↔ C a = C 0 : by rw C_0
... ↔ a = 0 : C_inj
instance [nontrivial R] : infinite (polynomial R) :=
infinite.of_injective (λ i, monomial i 1)
begin
intros m n h,
have := (single_eq_single_iff _ _ _ _).mp h,
simpa only [and_true, eq_self_iff_true, or_false, one_ne_zero, and_self],
end
lemma monomial_eq_smul_X {n} : monomial n (a : R) = a • X^n :=
calc monomial n a = monomial n (a * 1) : by simp
... = a • monomial n 1 : (smul_single' _ _ _).symm
... = a • X^n : by rw X_pow_eq_monomial
lemma ring_hom_ext {S} [semiring S] {f g : polynomial R →+* S}
(h₁ : ∀ a, f (C a) = g (C a)) (h₂ : f X = g X) : f = g :=
by { ext, exacts [h₁ _, h₂] }
@[ext] lemma ring_hom_ext' {S} [semiring S] {f g : polynomial R →+* S}
(h₁ : f.comp C = g.comp C) (h₂ : f X = g X) : f = g :=
ring_hom_ext (ring_hom.congr_fun h₁) h₂
end polynomial
|
ce4b411f570bcc732b98393d3ad2e501b51a7c49 | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/data/int/basic.lean | 54cdafb3aa2e83a964cd0e66992e4e7a5908ab1a | [
"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 | 52,193 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
The integers, with addition, multiplication, and subtraction.
-/
import data.nat.basic algebra.char_zero algebra.order_functions
open nat
namespace int
instance : inhabited ℤ := ⟨int.zero⟩
@[simp] lemma default_eq_zero : default ℤ = 0 := rfl
meta instance : has_to_format ℤ := ⟨λ z, to_string z⟩
meta instance : has_reflect ℤ := by tactic.mk_has_reflect_instance
attribute [simp] int.coe_nat_add int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_succ
attribute [simp] int.of_nat_eq_coe int.bodd
@[simp] theorem add_def {a b : ℤ} : int.add a b = a + b := rfl
@[simp] theorem mul_def {a b : ℤ} : int.mul a b = a * b := rfl
@[simp] theorem coe_nat_mul_neg_succ (m n : ℕ) : (m : ℤ) * -[1+ n] = -(m * succ n) := rfl
@[simp] theorem neg_succ_mul_coe_nat (m n : ℕ) : -[1+ m] * n = -(succ m * n) := rfl
@[simp] theorem neg_succ_mul_neg_succ (m n : ℕ) : -[1+ m] * -[1+ n] = succ m * succ n := rfl
@[simp, elim_cast] theorem coe_nat_le {m n : ℕ} : (↑m : ℤ) ≤ ↑n ↔ m ≤ n := coe_nat_le_coe_nat_iff m n
@[simp, elim_cast] theorem coe_nat_lt {m n : ℕ} : (↑m : ℤ) < ↑n ↔ m < n := coe_nat_lt_coe_nat_iff m n
@[simp, elim_cast] theorem coe_nat_inj' {m n : ℕ} : (↑m : ℤ) = ↑n ↔ m = n := int.coe_nat_eq_coe_nat_iff m n
@[simp] theorem coe_nat_pos {n : ℕ} : (0 : ℤ) < n ↔ 0 < n :=
by rw [← int.coe_nat_zero, coe_nat_lt]
@[simp] theorem coe_nat_eq_zero {n : ℕ} : (n : ℤ) = 0 ↔ n = 0 :=
by rw [← int.coe_nat_zero, coe_nat_inj']
theorem coe_nat_ne_zero {n : ℕ} : (n : ℤ) ≠ 0 ↔ n ≠ 0 :=
not_congr coe_nat_eq_zero
lemma coe_nat_nonneg (n : ℕ) : 0 ≤ (n : ℤ) := coe_nat_le.2 (nat.zero_le _)
lemma coe_nat_ne_zero_iff_pos {n : ℕ} : (n : ℤ) ≠ 0 ↔ 0 < n :=
⟨λ h, nat.pos_of_ne_zero (coe_nat_ne_zero.1 h),
λ h, (ne_of_lt (coe_nat_lt.2 h)).symm⟩
lemma coe_nat_succ_pos (n : ℕ) : 0 < (n.succ : ℤ) := int.coe_nat_pos.2 (succ_pos n)
@[simp, elim_cast] theorem coe_nat_abs (n : ℕ) : abs (n : ℤ) = n :=
abs_of_nonneg (coe_nat_nonneg n)
/- succ and pred -/
/-- Immediate successor of an integer: `succ n = n + 1` -/
def succ (a : ℤ) := a + 1
/-- Immediate predecessor of an integer: `pred n = n - 1` -/
def pred (a : ℤ) := a - 1
theorem nat_succ_eq_int_succ (n : ℕ) : (nat.succ n : ℤ) = int.succ n := rfl
theorem pred_succ (a : ℤ) : pred (succ a) = a := add_sub_cancel _ _
theorem succ_pred (a : ℤ) : succ (pred a) = a := sub_add_cancel _ _
theorem neg_succ (a : ℤ) : -succ a = pred (-a) := neg_add _ _
theorem succ_neg_succ (a : ℤ) : succ (-succ a) = -a :=
by rw [neg_succ, succ_pred]
theorem neg_pred (a : ℤ) : -pred a = succ (-a) :=
by rw [eq_neg_of_eq_neg (neg_succ (-a)).symm, neg_neg]
theorem pred_neg_pred (a : ℤ) : pred (-pred a) = -a :=
by rw [neg_pred, pred_succ]
theorem pred_nat_succ (n : ℕ) : pred (nat.succ n) = n := pred_succ n
theorem neg_nat_succ (n : ℕ) : -(nat.succ n : ℤ) = pred (-n) := neg_succ n
theorem succ_neg_nat_succ (n : ℕ) : succ (-nat.succ n) = -n := succ_neg_succ n
theorem lt_succ_self (a : ℤ) : a < succ a :=
lt_add_of_pos_right _ zero_lt_one
theorem pred_self_lt (a : ℤ) : pred a < a :=
sub_lt_self _ zero_lt_one
theorem add_one_le_iff {a b : ℤ} : a + 1 ≤ b ↔ a < b := iff.rfl
theorem lt_add_one_iff {a b : ℤ} : a < b + 1 ↔ a ≤ b :=
@add_le_add_iff_right _ _ a b 1
theorem sub_one_lt_iff {a b : ℤ} : a - 1 < b ↔ a ≤ b :=
sub_lt_iff_lt_add.trans lt_add_one_iff
theorem le_sub_one_iff {a b : ℤ} : a ≤ b - 1 ↔ a < b :=
le_sub_iff_add_le
@[elab_as_eliminator] protected lemma induction_on {p : ℤ → Prop}
(i : ℤ) (hz : p 0) (hp : ∀i : ℕ, p i → p (i + 1)) (hn : ∀i : ℕ, p (-i) → p (-i - 1)) : p i :=
begin
induction i,
{ induction i,
{ exact hz },
{ exact hp _ i_ih } },
{ have : ∀n:ℕ, p (- n),
{ intro n, induction n,
{ simp [hz] },
{ convert hn _ n_ih using 1, simp [sub_eq_neg_add] } },
exact this (i + 1) }
end
protected def induction_on' {C : ℤ → Sort*} (z : ℤ) (b : ℤ) :
C b → (∀ k, b ≤ k → C k → C (k + 1)) → (∀ k ≤ b, C k → C (k - 1)) → C z :=
λ H0 Hs Hp,
begin
rw ←sub_add_cancel z b,
induction (z - b),
{ induction a with n ih, { rwa [of_nat_zero, zero_add] },
rw [of_nat_succ, add_assoc, add_comm 1 b, ←add_assoc],
exact Hs _ (le_add_of_nonneg_left (of_nat_nonneg _)) ih },
{ induction a with n ih,
{ rw [neg_succ_of_nat_eq, ←of_nat_eq_coe, of_nat_zero, zero_add, neg_add_eq_sub],
exact Hp _ (le_refl _) H0 },
{ rw [neg_succ_of_nat_coe', nat.succ_eq_add_one, ←neg_succ_of_nat_coe, sub_add_eq_add_sub],
exact Hp _ (le_of_lt (add_lt_of_neg_of_le (neg_succ_lt_zero _) (le_refl _))) ih } }
end
/- nat abs -/
attribute [simp] nat_abs nat_abs_of_nat nat_abs_zero nat_abs_one
theorem nat_abs_add_le (a b : ℤ) : nat_abs (a + b) ≤ nat_abs a + nat_abs b :=
begin
have : ∀ (a b : ℕ), nat_abs (sub_nat_nat a (nat.succ b)) ≤ nat.succ (a + b),
{ refine (λ a b : ℕ, sub_nat_nat_elim a b.succ
(λ m n i, n = b.succ → nat_abs i ≤ (m + b).succ) _ _ rfl);
intros i n e,
{ subst e, rw [add_comm _ i, add_assoc],
exact nat.le_add_right i (b.succ + b).succ },
{ apply succ_le_succ,
rw [← succ_inj e, ← add_assoc, add_comm],
apply nat.le_add_right } },
cases a; cases b with b b; simp [nat_abs, nat.succ_add];
try {refl}; [skip, rw add_comm a b]; apply this
end
theorem nat_abs_neg_of_nat (n : ℕ) : nat_abs (neg_of_nat n) = n :=
by cases n; refl
theorem nat_abs_mul (a b : ℤ) : nat_abs (a * b) = (nat_abs a) * (nat_abs b) :=
by cases a; cases b; simp only [(*), int.mul, nat_abs_neg_of_nat, eq_self_iff_true, int.nat_abs]
@[simp] lemma nat_abs_mul_self' (a : ℤ) : (nat_abs a * nat_abs a : ℤ) = a * a :=
by rw [← int.coe_nat_mul, nat_abs_mul_self]
theorem neg_succ_of_nat_eq' (m : ℕ) : -[1+ m] = -m - 1 :=
by simp [neg_succ_of_nat_eq, sub_eq_neg_add]
lemma nat_abs_ne_zero_of_ne_zero {z : ℤ} (hz : z ≠ 0) : z.nat_abs ≠ 0 :=
λ h, hz $ int.eq_zero_of_nat_abs_eq_zero h
@[simp] lemma nat_abs_eq_zero {a : ℤ} : a.nat_abs = 0 ↔ a = 0 :=
⟨int.eq_zero_of_nat_abs_eq_zero, λ h, h.symm ▸ rfl⟩
/- / -/
@[simp] theorem of_nat_div (m n : ℕ) : of_nat (m / n) = (of_nat m) / (of_nat n) := rfl
@[simp, move_cast] theorem coe_nat_div (m n : ℕ) : ((m / n : ℕ) : ℤ) = m / n := rfl
theorem neg_succ_of_nat_div (m : ℕ) {b : ℤ} (H : 0 < b) :
-[1+m] / b = -(m / b + 1) :=
match b, eq_succ_of_zero_lt H with ._, ⟨n, rfl⟩ := rfl end
@[simp] protected theorem div_neg : ∀ (a b : ℤ), a / -b = -(a / b)
| (m : ℕ) 0 := show of_nat (m / 0) = -(m / 0 : ℕ), by rw nat.div_zero; refl
| (m : ℕ) (n+1:ℕ) := rfl
| 0 -[1+ n] := rfl
| (m+1:ℕ) -[1+ n] := (neg_neg _).symm
| -[1+ m] 0 := rfl
| -[1+ m] (n+1:ℕ) := rfl
| -[1+ m] -[1+ n] := rfl
theorem div_of_neg_of_pos {a b : ℤ} (Ha : a < 0) (Hb : 0 < b) : a / b = -((-a - 1) / b + 1) :=
match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with
| ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ :=
by change (- -[1+ m] : ℤ) with (m+1 : ℤ); rw add_sub_cancel; refl
end
protected theorem div_nonneg {a b : ℤ} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a / b :=
match a, b, eq_coe_of_zero_le Ha, eq_coe_of_zero_le Hb with
| ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := coe_zero_le _
end
protected theorem div_nonpos {a b : ℤ} (Ha : 0 ≤ a) (Hb : b ≤ 0) : a / b ≤ 0 :=
nonpos_of_neg_nonneg $ by rw [← int.div_neg]; exact int.div_nonneg Ha (neg_nonneg_of_nonpos Hb)
theorem div_neg' {a b : ℤ} (Ha : a < 0) (Hb : 0 < b) : a / b < 0 :=
match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with
| ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := neg_succ_lt_zero _
end
-- Will be generalized to Euclidean domains.
protected theorem zero_div : ∀ (b : ℤ), 0 / b = 0
| 0 := rfl
| (n+1:ℕ) := rfl
| -[1+ n] := rfl
local attribute [simp] -- Will be generalized to Euclidean domains.
protected theorem div_zero : ∀ (a : ℤ), a / 0 = 0
| 0 := rfl
| (n+1:ℕ) := rfl
| -[1+ n] := rfl
@[simp] protected theorem div_one : ∀ (a : ℤ), a / 1 = a
| 0 := rfl
| (n+1:ℕ) := congr_arg of_nat (nat.div_one _)
| -[1+ n] := congr_arg neg_succ_of_nat (nat.div_one _)
theorem div_eq_zero_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a / b = 0 :=
match a, b, eq_coe_of_zero_le H1, eq_succ_of_zero_lt (lt_of_le_of_lt H1 H2), H2 with
| ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 :=
congr_arg of_nat $ nat.div_eq_of_lt $ lt_of_coe_nat_lt_coe_nat H2
end
theorem div_eq_zero_of_lt_abs {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < abs b) : a / b = 0 :=
match b, abs b, abs_eq_nat_abs b, H2 with
| (n : ℕ), ._, rfl, H2 := div_eq_zero_of_lt H1 H2
| -[1+ n], ._, rfl, H2 := neg_inj $ by rw [← int.div_neg]; exact div_eq_zero_of_lt H1 H2
end
protected theorem add_mul_div_right (a b : ℤ) {c : ℤ} (H : c ≠ 0) :
(a + b * c) / c = a / c + b :=
have ∀ {k n : ℕ} {a : ℤ}, (a + n * k.succ) / k.succ = a / k.succ + n, from
λ k n a, match a with
| (m : ℕ) := congr_arg of_nat $ nat.add_mul_div_right _ _ k.succ_pos
| -[1+ m] := show ((n * k.succ:ℕ) - m.succ : ℤ) / k.succ =
n - (m / k.succ + 1 : ℕ), begin
cases lt_or_ge m (n*k.succ) with h h,
{ rw [← int.coe_nat_sub h,
← int.coe_nat_sub ((nat.div_lt_iff_lt_mul _ _ k.succ_pos).2 h)],
apply congr_arg of_nat,
rw [mul_comm, nat.mul_sub_div], rwa mul_comm },
{ change (↑(n * nat.succ k) - (m + 1) : ℤ) / ↑(nat.succ k) =
↑n - ((m / nat.succ k : ℕ) + 1),
rw [← sub_sub, ← sub_sub, ← neg_sub (m:ℤ), ← neg_sub _ (n:ℤ),
← int.coe_nat_sub h,
← int.coe_nat_sub ((nat.le_div_iff_mul_le _ _ k.succ_pos).2 h),
← neg_succ_of_nat_coe', ← neg_succ_of_nat_coe'],
{ apply congr_arg neg_succ_of_nat,
rw [mul_comm, nat.sub_mul_div], rwa mul_comm } }
end
end,
have ∀ {a b c : ℤ}, 0 < c → (a + b * c) / c = a / c + b, from
λ a b c H, match c, eq_succ_of_zero_lt H, b with
| ._, ⟨k, rfl⟩, (n : ℕ) := this
| ._, ⟨k, rfl⟩, -[1+ n] :=
show (a - n.succ * k.succ) / k.succ = (a / k.succ) - n.succ, from
eq_sub_of_add_eq $ by rw [← this, sub_add_cancel]
end,
match lt_trichotomy c 0 with
| or.inl hlt := neg_inj $ by rw [← int.div_neg, neg_add, ← int.div_neg, ← neg_mul_neg];
apply this (neg_pos_of_neg hlt)
| or.inr (or.inl heq) := absurd heq H
| or.inr (or.inr hgt) := this hgt
end
protected theorem add_mul_div_left (a : ℤ) {b : ℤ} (c : ℤ) (H : b ≠ 0) :
(a + b * c) / b = a / b + c :=
by rw [mul_comm, int.add_mul_div_right _ _ H]
@[simp] protected theorem mul_div_cancel (a : ℤ) {b : ℤ} (H : b ≠ 0) : a * b / b = a :=
by have := int.add_mul_div_right 0 a H;
rwa [zero_add, int.zero_div, zero_add] at this
@[simp] protected theorem mul_div_cancel_left {a : ℤ} (b : ℤ) (H : a ≠ 0) : a * b / a = b :=
by rw [mul_comm, int.mul_div_cancel _ H]
@[simp] protected theorem div_self {a : ℤ} (H : a ≠ 0) : a / a = 1 :=
by have := int.mul_div_cancel 1 H; rwa one_mul at this
/- mod -/
theorem of_nat_mod (m n : nat) : (m % n : ℤ) = of_nat (m % n) := rfl
@[simp] theorem coe_nat_mod (m n : ℕ) : (↑(m % n) : ℤ) = ↑m % ↑n := rfl
theorem neg_succ_of_nat_mod (m : ℕ) {b : ℤ} (bpos : 0 < b) :
-[1+m] % b = b - 1 - m % b :=
by rw [sub_sub, add_comm]; exact
match b, eq_succ_of_zero_lt bpos with ._, ⟨n, rfl⟩ := rfl end
@[simp] theorem mod_neg : ∀ (a b : ℤ), a % -b = a % b
| (m : ℕ) n := @congr_arg ℕ ℤ _ _ (λ i, ↑(m % i)) (nat_abs_neg _)
| -[1+ m] n := @congr_arg ℕ ℤ _ _ (λ i, sub_nat_nat i (nat.succ (m % i))) (nat_abs_neg _)
@[simp] theorem mod_abs (a b : ℤ) : a % (abs b) = a % b :=
abs_by_cases (λ i, a % i = a % b) rfl (mod_neg _ _)
local attribute [simp] -- Will be generalized to Euclidean domains.
theorem zero_mod (b : ℤ) : 0 % b = 0 :=
congr_arg of_nat $ nat.zero_mod _
local attribute [simp] -- Will be generalized to Euclidean domains.
theorem mod_zero : ∀ (a : ℤ), a % 0 = a
| (m : ℕ) := congr_arg of_nat $ nat.mod_zero _
| -[1+ m] := congr_arg neg_succ_of_nat $ nat.mod_zero _
local attribute [simp] -- Will be generalized to Euclidean domains.
theorem mod_one : ∀ (a : ℤ), a % 1 = 0
| (m : ℕ) := congr_arg of_nat $ nat.mod_one _
| -[1+ m] := show (1 - (m % 1).succ : ℤ) = 0, by rw nat.mod_one; refl
theorem mod_eq_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a % b = a :=
match a, b, eq_coe_of_zero_le H1, eq_coe_of_zero_le (le_trans H1 (le_of_lt H2)), H2 with
| ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 :=
congr_arg of_nat $ nat.mod_eq_of_lt (lt_of_coe_nat_lt_coe_nat H2)
end
theorem mod_nonneg : ∀ (a : ℤ) {b : ℤ}, b ≠ 0 → 0 ≤ a % b
| (m : ℕ) n H := coe_zero_le _
| -[1+ m] n H :=
sub_nonneg_of_le $ coe_nat_le_coe_nat_of_le $ nat.mod_lt _ (nat_abs_pos_of_ne_zero H)
theorem mod_lt_of_pos (a : ℤ) {b : ℤ} (H : 0 < b) : a % b < b :=
match a, b, eq_succ_of_zero_lt H with
| (m : ℕ), ._, ⟨n, rfl⟩ := coe_nat_lt_coe_nat_of_lt (nat.mod_lt _ (nat.succ_pos _))
| -[1+ m], ._, ⟨n, rfl⟩ := sub_lt_self _ (coe_nat_lt_coe_nat_of_lt $ nat.succ_pos _)
end
theorem mod_lt (a : ℤ) {b : ℤ} (H : b ≠ 0) : a % b < abs b :=
by rw [← mod_abs]; exact mod_lt_of_pos _ (abs_pos_of_ne_zero H)
theorem mod_add_div_aux (m n : ℕ) : (n - (m % n + 1) - (n * (m / n) + n) : ℤ) = -[1+ m] :=
begin
rw [← sub_sub, neg_succ_of_nat_coe, sub_sub (n:ℤ)],
apply eq_neg_of_eq_neg,
rw [neg_sub, sub_sub_self, add_right_comm],
exact @congr_arg ℕ ℤ _ _ (λi, (i + 1 : ℤ)) (nat.mod_add_div _ _).symm
end
theorem mod_add_div : ∀ (a b : ℤ), a % b + b * (a / b) = a
| (m : ℕ) 0 := congr_arg of_nat (nat.mod_add_div _ _)
| (m : ℕ) (n+1:ℕ) := congr_arg of_nat (nat.mod_add_div _ _)
| 0 -[1+ n] := rfl
| (m+1:ℕ) -[1+ n] := show (_ + -(n+1) * -((m + 1) / (n + 1) : ℕ) : ℤ) = _,
by rw [neg_mul_neg]; exact congr_arg of_nat (nat.mod_add_div _ _)
| -[1+ m] 0 := by rw [mod_zero, int.div_zero]; refl
| -[1+ m] (n+1:ℕ) := mod_add_div_aux m n.succ
| -[1+ m] -[1+ n] := mod_add_div_aux m n.succ
theorem mod_def (a b : ℤ) : a % b = a - b * (a / b) :=
eq_sub_of_add_eq (mod_add_div _ _)
@[simp] theorem add_mul_mod_self {a b c : ℤ} : (a + b * c) % c = a % c :=
if cz : c = 0 then by rw [cz, mul_zero, add_zero] else
by rw [mod_def, mod_def, int.add_mul_div_right _ _ cz,
mul_add, mul_comm, add_sub_add_right_eq_sub]
@[simp] theorem add_mul_mod_self_left (a b c : ℤ) : (a + b * c) % b = a % b :=
by rw [mul_comm, add_mul_mod_self]
@[simp] theorem add_mod_self {a b : ℤ} : (a + b) % b = a % b :=
by have := add_mul_mod_self_left a b 1; rwa mul_one at this
@[simp] theorem add_mod_self_left {a b : ℤ} : (a + b) % a = b % a :=
by rw [add_comm, add_mod_self]
@[simp] theorem mod_add_mod (m n k : ℤ) : (m % n + k) % n = (m + k) % n :=
by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm;
rwa [add_right_comm, mod_add_div] at this
@[simp] theorem add_mod_mod (m n k : ℤ) : (m + n % k) % k = (m + n) % k :=
by rw [add_comm, mod_add_mod, add_comm]
theorem add_mod_eq_add_mod_right {m n k : ℤ} (i : ℤ) (H : m % n = k % n) :
(m + i) % n = (k + i) % n :=
by rw [← mod_add_mod, ← mod_add_mod k, H]
theorem add_mod_eq_add_mod_left {m n k : ℤ} (i : ℤ) (H : m % n = k % n) :
(i + m) % n = (i + k) % n :=
by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm]
theorem mod_add_cancel_right {m n k : ℤ} (i) : (m + i) % n = (k + i) % n ↔
m % n = k % n :=
⟨λ H, by have := add_mod_eq_add_mod_right (-i) H;
rwa [add_neg_cancel_right, add_neg_cancel_right] at this,
add_mod_eq_add_mod_right _⟩
theorem mod_add_cancel_left {m n k i : ℤ} :
(i + m) % n = (i + k) % n ↔ m % n = k % n :=
by rw [add_comm, add_comm i, mod_add_cancel_right]
theorem mod_sub_cancel_right {m n k : ℤ} (i) : (m - i) % n = (k - i) % n ↔
m % n = k % n :=
mod_add_cancel_right _
theorem mod_eq_mod_iff_mod_sub_eq_zero {m n k : ℤ} : m % n = k % n ↔ (m - k) % n = 0 :=
(mod_sub_cancel_right k).symm.trans $ by simp
@[simp] theorem mul_mod_left (a b : ℤ) : (a * b) % b = 0 :=
by rw [← zero_add (a * b), add_mul_mod_self, zero_mod]
@[simp] theorem mul_mod_right (a b : ℤ) : (a * b) % a = 0 :=
by rw [mul_comm, mul_mod_left]
local attribute [simp] -- Will be generalized to Euclidean domains.
theorem mod_self {a : ℤ} : a % a = 0 :=
by have := mul_mod_left 1 a; rwa one_mul at this
@[simp] theorem mod_mod_of_dvd (n : int) {m k : int} (h : m ∣ k) : n % k % m = n % m :=
begin
conv { to_rhs, rw ←mod_add_div n k },
rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left]
end
@[simp] theorem mod_mod (a b : ℤ) : a % b % b = a % b :=
by conv {to_rhs, rw [← mod_add_div a b, add_mul_mod_self_left]}
/- properties of / and % -/
@[simp] theorem mul_div_mul_of_pos {a : ℤ} (b c : ℤ) (H : 0 < a) : a * b / (a * c) = b / c :=
suffices ∀ (m k : ℕ) (b : ℤ), (m.succ * b / (m.succ * k) : ℤ) = b / k, from
match a, eq_succ_of_zero_lt H, c, eq_coe_or_neg c with
| ._, ⟨m, rfl⟩, ._, ⟨k, or.inl rfl⟩ := this _ _ _
| ._, ⟨m, rfl⟩, ._, ⟨k, or.inr rfl⟩ :=
by rw [← neg_mul_eq_mul_neg, int.div_neg, int.div_neg];
apply congr_arg has_neg.neg; apply this
end,
λ m k b, match b, k with
| (n : ℕ), k := congr_arg of_nat (nat.mul_div_mul _ _ m.succ_pos)
| -[1+ n], 0 := by rw [int.coe_nat_zero, mul_zero, int.div_zero, int.div_zero]
| -[1+ n], k+1 := congr_arg neg_succ_of_nat $
show (m.succ * n + m) / (m.succ * k.succ) = n / k.succ, begin
apply nat.div_eq_of_lt_le,
{ refine le_trans _ (nat.le_add_right _ _),
rw [← nat.mul_div_mul _ _ m.succ_pos],
apply nat.div_mul_le_self },
{ change m.succ * n.succ ≤ _,
rw [mul_left_comm],
apply nat.mul_le_mul_left,
apply (nat.div_lt_iff_lt_mul _ _ k.succ_pos).1,
apply nat.lt_succ_self }
end
end
@[simp] theorem mul_div_mul_of_pos_left (a : ℤ) {b : ℤ} (c : ℤ) (H : 0 < b) :
a * b / (c * b) = a / c :=
by rw [mul_comm, mul_comm c, mul_div_mul_of_pos _ _ H]
@[simp] theorem mul_mod_mul_of_pos {a : ℤ} (b c : ℤ) (H : 0 < a) : a * b % (a * c) = a * (b % c) :=
by rw [mod_def, mod_def, mul_div_mul_of_pos _ _ H, mul_sub_left_distrib, mul_assoc]
theorem lt_div_add_one_mul_self (a : ℤ) {b : ℤ} (H : 0 < b) : a < (a / b + 1) * b :=
by rw [add_mul, one_mul, mul_comm]; apply lt_add_of_sub_left_lt;
rw [← mod_def]; apply mod_lt_of_pos _ H
theorem abs_div_le_abs : ∀ (a b : ℤ), abs (a / b) ≤ abs a :=
suffices ∀ (a : ℤ) (n : ℕ), abs (a / n) ≤ abs a, from
λ a b, match b, eq_coe_or_neg b with
| ._, ⟨n, or.inl rfl⟩ := this _ _
| ._, ⟨n, or.inr rfl⟩ := by rw [int.div_neg, abs_neg]; apply this
end,
λ a n, by rw [abs_eq_nat_abs, abs_eq_nat_abs]; exact
coe_nat_le_coe_nat_of_le (match a, n with
| (m : ℕ), n := nat.div_le_self _ _
| -[1+ m], 0 := nat.zero_le _
| -[1+ m], n+1 := nat.succ_le_succ (nat.div_le_self _ _)
end)
theorem div_le_self {a : ℤ} (b : ℤ) (Ha : 0 ≤ a) : a / b ≤ a :=
by have := le_trans (le_abs_self _) (abs_div_le_abs a b);
rwa [abs_of_nonneg Ha] at this
theorem mul_div_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : b * (a / b) = a :=
by have := mod_add_div a b; rwa [H, zero_add] at this
theorem div_mul_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : a / b * b = a :=
by rw [mul_comm, mul_div_cancel_of_mod_eq_zero H]
lemma mod_two_eq_zero_or_one (n : ℤ) : n % 2 = 0 ∨ n % 2 = 1 :=
have h : n % 2 < 2 := abs_of_nonneg (show 0 ≤ (2 : ℤ), from dec_trivial) ▸ int.mod_lt _ dec_trivial,
have h₁ : 0 ≤ n % 2 := int.mod_nonneg _ dec_trivial,
match (n % 2), h, h₁ with
| (0 : ℕ) := λ _ _, or.inl rfl
| (1 : ℕ) := λ _ _, or.inr rfl
| (k + 2 : ℕ) := λ h _, absurd h dec_trivial
| -[1+ a] := λ _ h₁, absurd h₁ dec_trivial
end
/- dvd -/
@[elim_cast] theorem coe_nat_dvd {m n : ℕ} : (↑m : ℤ) ∣ ↑n ↔ m ∣ n :=
⟨λ ⟨a, ae⟩, m.eq_zero_or_pos.elim
(λm0, by simp [m0] at ae; simp [ae, m0])
(λm0l, by {
cases eq_coe_of_zero_le (@nonneg_of_mul_nonneg_left ℤ _ m a
(by simp [ae.symm]) (by simpa using m0l)) with k e,
subst a, exact ⟨k, int.coe_nat_inj ae⟩ }),
λ ⟨k, e⟩, dvd.intro k $ by rw [e, int.coe_nat_mul]⟩
theorem coe_nat_dvd_left {n : ℕ} {z : ℤ} : (↑n : ℤ) ∣ z ↔ n ∣ z.nat_abs :=
by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd]
theorem coe_nat_dvd_right {n : ℕ} {z : ℤ} : z ∣ (↑n : ℤ) ↔ z.nat_abs ∣ n :=
by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd]
theorem dvd_antisymm {a b : ℤ} (H1 : 0 ≤ a) (H2 : 0 ≤ b) : a ∣ b → b ∣ a → a = b :=
begin
rw [← abs_of_nonneg H1, ← abs_of_nonneg H2, abs_eq_nat_abs, abs_eq_nat_abs],
rw [coe_nat_dvd, coe_nat_dvd, coe_nat_inj'],
apply nat.dvd_antisymm
end
theorem dvd_of_mod_eq_zero {a b : ℤ} (H : b % a = 0) : a ∣ b :=
⟨b / a, (mul_div_cancel_of_mod_eq_zero H).symm⟩
theorem mod_eq_zero_of_dvd : ∀ {a b : ℤ}, a ∣ b → b % a = 0
| a ._ ⟨c, rfl⟩ := mul_mod_right _ _
theorem dvd_iff_mod_eq_zero (a b : ℤ) : a ∣ b ↔ b % a = 0 :=
⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩
theorem nat_abs_dvd {a b : ℤ} : (a.nat_abs : ℤ) ∣ b ↔ a ∣ b :=
(nat_abs_eq a).elim (λ e, by rw ← e) (λ e, by rw [← neg_dvd_iff_dvd, ← e])
theorem dvd_nat_abs {a b : ℤ} : a ∣ b.nat_abs ↔ a ∣ b :=
(nat_abs_eq b).elim (λ e, by rw ← e) (λ e, by rw [← dvd_neg_iff_dvd, ← e])
instance decidable_dvd : @decidable_rel ℤ (∣) :=
assume a n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm
protected theorem div_mul_cancel {a b : ℤ} (H : b ∣ a) : a / b * b = a :=
div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H)
protected theorem mul_div_cancel' {a b : ℤ} (H : a ∣ b) : a * (b / a) = b :=
by rw [mul_comm, int.div_mul_cancel H]
protected theorem mul_div_assoc (a : ℤ) : ∀ {b c : ℤ}, c ∣ b → (a * b) / c = a * (b / c)
| ._ c ⟨d, rfl⟩ := if cz : c = 0 then by simp [cz] else
by rw [mul_left_comm, int.mul_div_cancel_left _ cz, int.mul_div_cancel_left _ cz]
theorem div_dvd_div : ∀ {a b c : ℤ} (H1 : a ∣ b) (H2 : b ∣ c), b / a ∣ c / a
| a ._ ._ ⟨b, rfl⟩ ⟨c, rfl⟩ := if az : a = 0 then by simp [az] else
by rw [int.mul_div_cancel_left _ az, mul_assoc, int.mul_div_cancel_left _ az];
apply dvd_mul_right
protected theorem eq_mul_of_div_eq_right {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) :
a = b * c :=
by rw [← H2, int.mul_div_cancel' H1]
protected theorem div_eq_of_eq_mul_right {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = b * c) :
a / b = c :=
by rw [H2, int.mul_div_cancel_left _ H1]
protected theorem div_eq_iff_eq_mul_right {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) :
a / b = c ↔ a = b * c :=
⟨int.eq_mul_of_div_eq_right H', int.div_eq_of_eq_mul_right H⟩
protected theorem div_eq_iff_eq_mul_left {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) :
a / b = c ↔ a = c * b :=
by rw mul_comm; exact int.div_eq_iff_eq_mul_right H H'
protected theorem eq_mul_of_div_eq_left {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) :
a = c * b :=
by rw [mul_comm, int.eq_mul_of_div_eq_right H1 H2]
protected theorem div_eq_of_eq_mul_left {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = c * b) :
a / b = c :=
int.div_eq_of_eq_mul_right H1 (by rw [mul_comm, H2])
theorem neg_div_of_dvd : ∀ {a b : ℤ} (H : b ∣ a), -a / b = -(a / b)
| ._ b ⟨c, rfl⟩ := if bz : b = 0 then by simp [bz] else
by rw [neg_mul_eq_mul_neg, int.mul_div_cancel_left _ bz, int.mul_div_cancel_left _ bz]
lemma add_div_of_dvd {a b c : ℤ} :
c ∣ a → c ∣ b → (a + b) / c = a / c + b / c :=
begin
intros h1 h2,
by_cases h3 : c = 0,
{ rw [h3, zero_dvd_iff] at *,
rw [h1, h2, h3], refl },
{ apply eq_of_mul_eq_mul_right h3,
rw add_mul, repeat {rw [int.div_mul_cancel]};
try {apply dvd_add}; assumption }
end
theorem div_sign : ∀ a b, a / sign b = a * sign b
| a (n+1:ℕ) := by unfold sign; simp
| a 0 := by simp [sign]
| a -[1+ n] := by simp [sign]
@[simp] theorem sign_mul : ∀ a b, sign (a * b) = sign a * sign b
| a 0 := by simp
| 0 b := by simp
| (m+1:ℕ) (n+1:ℕ) := rfl
| (m+1:ℕ) -[1+ n] := rfl
| -[1+ m] (n+1:ℕ) := rfl
| -[1+ m] -[1+ n] := rfl
protected theorem sign_eq_div_abs (a : ℤ) : sign a = a / (abs a) :=
if az : a = 0 then by simp [az] else
(int.div_eq_of_eq_mul_left (mt eq_zero_of_abs_eq_zero az)
(sign_mul_abs _).symm).symm
theorem mul_sign : ∀ (i : ℤ), i * sign i = nat_abs i
| (n+1:ℕ) := mul_one _
| 0 := mul_zero _
| -[1+ n] := mul_neg_one _
theorem le_of_dvd {a b : ℤ} (bpos : 0 < b) (H : a ∣ b) : a ≤ b :=
match a, b, eq_succ_of_zero_lt bpos, H with
| (m : ℕ), ._, ⟨n, rfl⟩, H := coe_nat_le_coe_nat_of_le $
nat.le_of_dvd n.succ_pos $ coe_nat_dvd.1 H
| -[1+ m], ._, ⟨n, rfl⟩, _ :=
le_trans (le_of_lt $ neg_succ_lt_zero _) (coe_zero_le _)
end
theorem eq_one_of_dvd_one {a : ℤ} (H : 0 ≤ a) (H' : a ∣ 1) : a = 1 :=
match a, eq_coe_of_zero_le H, H' with
| ._, ⟨n, rfl⟩, H' := congr_arg coe $
nat.eq_one_of_dvd_one $ coe_nat_dvd.1 H'
end
theorem eq_one_of_mul_eq_one_right {a b : ℤ} (H : 0 ≤ a) (H' : a * b = 1) : a = 1 :=
eq_one_of_dvd_one H ⟨b, H'.symm⟩
theorem eq_one_of_mul_eq_one_left {a b : ℤ} (H : 0 ≤ b) (H' : a * b = 1) : b = 1 :=
eq_one_of_mul_eq_one_right H (by rw [mul_comm, H'])
lemma of_nat_dvd_of_dvd_nat_abs {a : ℕ} : ∀ {z : ℤ} (haz : a ∣ z.nat_abs), ↑a ∣ z
| (int.of_nat _) haz := int.coe_nat_dvd.2 haz
| -[1+k] haz :=
begin
change ↑a ∣ -(k+1 : ℤ),
apply dvd_neg_of_dvd,
apply int.coe_nat_dvd.2,
exact haz
end
lemma dvd_nat_abs_of_of_nat_dvd {a : ℕ} : ∀ {z : ℤ} (haz : ↑a ∣ z), a ∣ z.nat_abs
| (int.of_nat _) haz := int.coe_nat_dvd.1 (int.dvd_nat_abs.2 haz)
| -[1+k] haz :=
have haz' : (↑a:ℤ) ∣ (↑(k+1):ℤ), from dvd_of_dvd_neg haz,
int.coe_nat_dvd.1 haz'
lemma pow_dvd_of_le_of_pow_dvd {p m n : ℕ} {k : ℤ} (hmn : m ≤ n) (hdiv : ↑(p ^ n) ∣ k) :
↑(p ^ m) ∣ k :=
begin
induction k,
{ apply int.coe_nat_dvd.2,
apply pow_dvd_of_le_of_pow_dvd hmn,
apply int.coe_nat_dvd.1 hdiv },
{ change -[1+k] with -(↑(k+1) : ℤ),
apply dvd_neg_of_dvd,
apply int.coe_nat_dvd.2,
apply pow_dvd_of_le_of_pow_dvd hmn,
apply int.coe_nat_dvd.1,
apply dvd_of_dvd_neg,
exact hdiv }
end
lemma dvd_of_pow_dvd {p k : ℕ} {m : ℤ} (hk : 1 ≤ k) (hpk : ↑(p^k) ∣ m) : ↑p ∣ m :=
by rw ←nat.pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk
/- / and ordering -/
protected theorem div_mul_le (a : ℤ) {b : ℤ} (H : b ≠ 0) : a / b * b ≤ a :=
le_of_sub_nonneg $ by rw [mul_comm, ← mod_def]; apply mod_nonneg _ H
protected theorem div_le_of_le_mul {a b c : ℤ} (H : 0 < c) (H' : a ≤ b * c) : a / c ≤ b :=
le_of_mul_le_mul_right (le_trans (int.div_mul_le _ (ne_of_gt H)) H') H
protected theorem mul_lt_of_lt_div {a b c : ℤ} (H : 0 < c) (H3 : a < b / c) : a * c < b :=
lt_of_not_ge $ mt (int.div_le_of_le_mul H) (not_le_of_gt H3)
protected theorem mul_le_of_le_div {a b c : ℤ} (H1 : 0 < c) (H2 : a ≤ b / c) : a * c ≤ b :=
le_trans (mul_le_mul_of_nonneg_right H2 (le_of_lt H1)) (int.div_mul_le _ (ne_of_gt H1))
protected theorem le_div_of_mul_le {a b c : ℤ} (H1 : 0 < c) (H2 : a * c ≤ b) : a ≤ b / c :=
le_of_lt_add_one $ lt_of_mul_lt_mul_right
(lt_of_le_of_lt H2 (lt_div_add_one_mul_self _ H1)) (le_of_lt H1)
protected theorem le_div_iff_mul_le {a b c : ℤ} (H : 0 < c) : a ≤ b / c ↔ a * c ≤ b :=
⟨int.mul_le_of_le_div H, int.le_div_of_mul_le H⟩
protected theorem div_le_div {a b c : ℤ} (H : 0 < c) (H' : a ≤ b) : a / c ≤ b / c :=
int.le_div_of_mul_le H (le_trans (int.div_mul_le _ (ne_of_gt H)) H')
protected theorem div_lt_of_lt_mul {a b c : ℤ} (H : 0 < c) (H' : a < b * c) : a / c < b :=
lt_of_not_ge $ mt (int.mul_le_of_le_div H) (not_le_of_gt H')
protected theorem lt_mul_of_div_lt {a b c : ℤ} (H1 : 0 < c) (H2 : a / c < b) : a < b * c :=
lt_of_not_ge $ mt (int.le_div_of_mul_le H1) (not_le_of_gt H2)
protected theorem div_lt_iff_lt_mul {a b c : ℤ} (H : 0 < c) : a / c < b ↔ a < b * c :=
⟨int.lt_mul_of_div_lt H, int.div_lt_of_lt_mul H⟩
protected theorem le_mul_of_div_le {a b c : ℤ} (H1 : 0 ≤ b) (H2 : b ∣ a) (H3 : a / b ≤ c) :
a ≤ c * b :=
by rw [← int.div_mul_cancel H2]; exact mul_le_mul_of_nonneg_right H3 H1
protected theorem lt_div_of_mul_lt {a b c : ℤ} (H1 : 0 ≤ b) (H2 : b ∣ c) (H3 : a * b < c) :
a < c / b :=
lt_of_not_ge $ mt (int.le_mul_of_div_le H1 H2) (not_le_of_gt H3)
protected theorem lt_div_iff_mul_lt {a b : ℤ} (c : ℤ) (H : 0 < c) (H' : c ∣ b) :
a < b / c ↔ a * c < b :=
⟨int.mul_lt_of_lt_div H, int.lt_div_of_mul_lt (le_of_lt H) H'⟩
theorem div_pos_of_pos_of_dvd {a b : ℤ} (H1 : 0 < a) (H2 : 0 ≤ b) (H3 : b ∣ a) : 0 < a / b :=
int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul)
theorem div_eq_div_of_mul_eq_mul {a b c d : ℤ} (H1 : b ∣ a) (H2 : d ∣ c) (H3 : b ≠ 0)
(H4 : d ≠ 0) (H5 : a * d = b * c) :
a / b = c / d :=
int.div_eq_of_eq_mul_right H3 $
by rw [← int.mul_div_assoc _ H2]; exact
(int.div_eq_of_eq_mul_left H4 H5.symm).symm
theorem eq_mul_div_of_mul_eq_mul_of_dvd_left {a b c d : ℤ} (hb : b ≠ 0) (hd : d ≠ 0) (hbc : b ∣ c)
(h : b * a = c * d) : a = c / b * d :=
begin
cases hbc with k hk,
subst hk,
rw int.mul_div_cancel_left, rw mul_assoc at h,
apply _root_.eq_of_mul_eq_mul_left _ h,
repeat {assumption}
end
theorem of_nat_add_neg_succ_of_nat_of_lt {m n : ℕ}
(h : m < n.succ) : of_nat m + -[1+n] = -[1+ n - m] :=
begin
change sub_nat_nat _ _ = _,
have h' : n.succ - m = (n - m).succ,
apply succ_sub,
apply le_of_lt_succ h,
simp [*, sub_nat_nat]
end
theorem of_nat_add_neg_succ_of_nat_of_ge {m n : ℕ}
(h : n.succ ≤ m) : of_nat m + -[1+n] = of_nat (m - n.succ) :=
begin
change sub_nat_nat _ _ = _,
have h' : n.succ - m = 0,
apply sub_eq_zero_of_le h,
simp [*, sub_nat_nat]
end
@[simp] theorem neg_add_neg (m n : ℕ) : -[1+m] + -[1+n] = -[1+nat.succ(m+n)] := rfl
/- to_nat -/
theorem to_nat_eq_max : ∀ (a : ℤ), (to_nat a : ℤ) = max a 0
| (n : ℕ) := (max_eq_left (coe_zero_le n)).symm
| -[1+ n] := (max_eq_right (le_of_lt (neg_succ_lt_zero n))).symm
@[simp] theorem to_nat_of_nonneg {a : ℤ} (h : 0 ≤ a) : (to_nat a : ℤ) = a :=
by rw [to_nat_eq_max, max_eq_left h]
@[simp] lemma to_nat_sub_of_le (a b : ℤ) (h : b ≤ a) : (to_nat (a + -b) : ℤ) = a + - b :=
int.to_nat_of_nonneg (sub_nonneg_of_le h)
@[simp] theorem to_nat_coe_nat (n : ℕ) : to_nat ↑n = n := rfl
theorem le_to_nat (a : ℤ) : a ≤ to_nat a :=
by rw [to_nat_eq_max]; apply le_max_left
@[simp] theorem to_nat_le {a : ℤ} {n : ℕ} : to_nat a ≤ n ↔ a ≤ n :=
by rw [(coe_nat_le_coe_nat_iff _ _).symm, to_nat_eq_max, max_le_iff];
exact and_iff_left (coe_zero_le _)
@[simp] theorem lt_to_nat {n : ℕ} {a : ℤ} : n < to_nat a ↔ (n : ℤ) < a :=
le_iff_le_iff_lt_iff_lt.1 to_nat_le
theorem to_nat_le_to_nat {a b : ℤ} (h : a ≤ b) : to_nat a ≤ to_nat b :=
by rw to_nat_le; exact le_trans h (le_to_nat b)
theorem to_nat_lt_to_nat {a b : ℤ} (hb : 0 < b) : to_nat a < to_nat b ↔ a < b :=
⟨λ h, begin cases a, exact lt_to_nat.1 h, exact lt_trans (neg_succ_of_nat_lt_zero a) hb, end,
λ h, begin rw lt_to_nat, cases a, exact h, exact hb end⟩
theorem lt_of_to_nat_lt {a b : ℤ} (h : to_nat a < to_nat b) : a < b :=
(to_nat_lt_to_nat $ lt_to_nat.1 $ lt_of_le_of_lt (nat.zero_le _) h).1 h
def to_nat' : ℤ → option ℕ
| (n : ℕ) := some n
| -[1+ n] := none
theorem mem_to_nat' : ∀ (a : ℤ) (n : ℕ), n ∈ to_nat' a ↔ a = n
| (m : ℕ) n := option.some_inj.trans coe_nat_inj'.symm
| -[1+ m] n := by split; intro h; cases h
/- units -/
@[simp] theorem units_nat_abs (u : units ℤ) : nat_abs u = 1 :=
units.ext_iff.1 $ nat.units_eq_one ⟨nat_abs u, nat_abs ↑u⁻¹,
by rw [← nat_abs_mul, units.mul_inv]; refl,
by rw [← nat_abs_mul, units.inv_mul]; refl⟩
theorem units_eq_one_or (u : units ℤ) : u = 1 ∨ u = -1 :=
by simpa [units.ext_iff, units_nat_abs] using nat_abs_eq u
lemma units_inv_eq_self (u : units ℤ) : u⁻¹ = u :=
(units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl)
/- bitwise ops -/
@[simp] lemma bodd_zero : bodd 0 = ff := rfl
@[simp] lemma bodd_one : bodd 1 = tt := rfl
@[simp] lemma bodd_two : bodd 2 = ff := rfl
@[simp, elim_cast] lemma bodd_coe (n : ℕ) : int.bodd n = nat.bodd n := rfl
@[simp] lemma bodd_sub_nat_nat (m n : ℕ) : bodd (sub_nat_nat m n) = bxor m.bodd n.bodd :=
by apply sub_nat_nat_elim m n (λ m n i, bodd i = bxor m.bodd n.bodd); intros;
simp; cases i.bodd; simp
@[simp] lemma bodd_neg_of_nat (n : ℕ) : bodd (neg_of_nat n) = n.bodd :=
by cases n; simp; refl
@[simp] lemma bodd_neg (n : ℤ) : bodd (-n) = bodd n :=
by cases n; simp [has_neg.neg, int.coe_nat_eq, int.neg, bodd, -of_nat_eq_coe]
@[simp] lemma bodd_add (m n : ℤ) : bodd (m + n) = bxor (bodd m) (bodd n) :=
by cases m with m m; cases n with n n; unfold has_add.add;
simp [int.add, -of_nat_eq_coe, bool.bxor_comm]
@[simp] lemma bodd_mul (m n : ℤ) : bodd (m * n) = bodd m && bodd n :=
by cases m with m m; cases n with n n; unfold has_mul.mul;
simp [int.mul, -of_nat_eq_coe, bool.bxor_comm]
theorem bodd_add_div2 : ∀ n, cond (bodd n) 1 0 + 2 * div2 n = n
| (n : ℕ) :=
by rw [show (cond (bodd n) 1 0 : ℤ) = (cond (bodd n) 1 0 : ℕ),
by cases bodd n; refl]; exact congr_arg of_nat n.bodd_add_div2
| -[1+ n] := begin
refine eq.trans _ (congr_arg neg_succ_of_nat n.bodd_add_div2),
dsimp [bodd], cases nat.bodd n; dsimp [cond, bnot, div2, int.mul],
{ change -[1+ 2 * nat.div2 n] = _, rw zero_add },
{ rw [zero_add, add_comm], refl }
end
theorem div2_val : ∀ n, div2 n = n / 2
| (n : ℕ) := congr_arg of_nat n.div2_val
| -[1+ n] := congr_arg neg_succ_of_nat n.div2_val
lemma bit0_val (n : ℤ) : bit0 n = 2 * n := (two_mul _).symm
lemma bit1_val (n : ℤ) : bit1 n = 2 * n + 1 := congr_arg (+(1:ℤ)) (bit0_val _)
lemma bit_val (b n) : bit b n = 2 * n + cond b 1 0 :=
by { cases b, apply (bit0_val n).trans (add_zero _).symm, apply bit1_val }
lemma bit_decomp (n : ℤ) : bit (bodd n) (div2 n) = n :=
(bit_val _ _).trans $ (add_comm _ _).trans $ bodd_add_div2 _
def {u} bit_cases_on {C : ℤ → Sort u} (n) (h : ∀ b n, C (bit b n)) : C n :=
by rw [← bit_decomp n]; apply h
@[simp] lemma bit_zero : bit ff 0 = 0 := rfl
@[simp] lemma bit_coe_nat (b) (n : ℕ) : bit b n = nat.bit b n :=
by rw [bit_val, nat.bit_val]; cases b; refl
@[simp] lemma bit_neg_succ (b) (n : ℕ) : bit b -[1+ n] = -[1+ nat.bit (bnot b) n] :=
by rw [bit_val, nat.bit_val]; cases b; refl
@[simp] lemma bodd_bit (b n) : bodd (bit b n) = b :=
by rw bit_val; simp; cases b; cases bodd n; refl
@[simp] lemma div2_bit (b n) : div2 (bit b n) = n :=
begin
rw [bit_val, div2_val, add_comm, int.add_mul_div_left, (_ : (_/2:ℤ) = 0), zero_add],
cases b, all_goals {exact dec_trivial}
end
@[simp] lemma test_bit_zero (b) : ∀ n, test_bit (bit b n) 0 = b
| (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_zero
| -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_zero];
clear test_bit_zero; cases b; refl
@[simp] lemma test_bit_succ (m b) : ∀ n, test_bit (bit b n) (nat.succ m) = test_bit n m
| (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_succ
| -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_succ]
private meta def bitwise_tac : tactic unit := `[
funext m,
funext n,
cases m with m m; cases n with n n; try {refl},
all_goals {
apply congr_arg of_nat <|> apply congr_arg neg_succ_of_nat,
try {dsimp [nat.land, nat.ldiff, nat.lor]},
try {rw [
show nat.bitwise (λ a b, a && bnot b) n m =
nat.bitwise (λ a b, b && bnot a) m n, from
congr_fun (congr_fun (@nat.bitwise_swap (λ a b, b && bnot a) rfl) n) m]},
apply congr_arg (λ f, nat.bitwise f m n),
funext a,
funext b,
cases a; cases b; refl
},
all_goals {unfold nat.land nat.ldiff nat.lor}
]
theorem bitwise_or : bitwise bor = lor := by bitwise_tac
theorem bitwise_and : bitwise band = land := by bitwise_tac
theorem bitwise_diff : bitwise (λ a b, a && bnot b) = ldiff := by bitwise_tac
theorem bitwise_xor : bitwise bxor = lxor := by bitwise_tac
@[simp] lemma bitwise_bit (f : bool → bool → bool) (a m b n) :
bitwise f (bit a m) (bit b n) = bit (f a b) (bitwise f m n) :=
begin
cases m with m m; cases n with n n;
repeat { rw [← int.coe_nat_eq] <|> rw bit_coe_nat <|> rw bit_neg_succ };
unfold bitwise nat_bitwise bnot;
[ induction h : f ff ff,
induction h : f ff tt,
induction h : f tt ff,
induction h : f tt tt ],
all_goals {
unfold cond, rw nat.bitwise_bit,
repeat { rw bit_coe_nat <|> rw bit_neg_succ <|> rw bnot_bnot } },
all_goals { unfold bnot {fail_if_unchanged := ff}; rw h; refl }
end
@[simp] lemma lor_bit (a m b n) : lor (bit a m) (bit b n) = bit (a || b) (lor m n) :=
by rw [← bitwise_or, bitwise_bit]
@[simp] lemma land_bit (a m b n) : land (bit a m) (bit b n) = bit (a && b) (land m n) :=
by rw [← bitwise_and, bitwise_bit]
@[simp] lemma ldiff_bit (a m b n) : ldiff (bit a m) (bit b n) = bit (a && bnot b) (ldiff m n) :=
by rw [← bitwise_diff, bitwise_bit]
@[simp] lemma lxor_bit (a m b n) : lxor (bit a m) (bit b n) = bit (bxor a b) (lxor m n) :=
by rw [← bitwise_xor, bitwise_bit]
@[simp] lemma lnot_bit (b) : ∀ n, lnot (bit b n) = bit (bnot b) (lnot n)
| (n : ℕ) := by simp [lnot]
| -[1+ n] := by simp [lnot]
@[simp] lemma test_bit_bitwise (f : bool → bool → bool) (m n k) :
test_bit (bitwise f m n) k = f (test_bit m k) (test_bit n k) :=
begin
induction k with k IH generalizing m n;
apply bit_cases_on m; intros a m';
apply bit_cases_on n; intros b n';
rw bitwise_bit,
{ simp [test_bit_zero] },
{ simp [test_bit_succ, IH] }
end
@[simp] lemma test_bit_lor (m n k) : test_bit (lor m n) k = test_bit m k || test_bit n k :=
by rw [← bitwise_or, test_bit_bitwise]
@[simp] lemma test_bit_land (m n k) : test_bit (land m n) k = test_bit m k && test_bit n k :=
by rw [← bitwise_and, test_bit_bitwise]
@[simp] lemma test_bit_ldiff (m n k) : test_bit (ldiff m n) k = test_bit m k && bnot (test_bit n k) :=
by rw [← bitwise_diff, test_bit_bitwise]
@[simp] lemma test_bit_lxor (m n k) : test_bit (lxor m n) k = bxor (test_bit m k) (test_bit n k) :=
by rw [← bitwise_xor, test_bit_bitwise]
@[simp] lemma test_bit_lnot : ∀ n k, test_bit (lnot n) k = bnot (test_bit n k)
| (n : ℕ) k := by simp [lnot, test_bit]
| -[1+ n] k := by simp [lnot, test_bit]
lemma shiftl_add : ∀ (m : ℤ) (n : ℕ) (k : ℤ), shiftl m (n + k) = shiftl (shiftl m n) k
| (m : ℕ) n (k:ℕ) := congr_arg of_nat (nat.shiftl_add _ _ _)
| -[1+ m] n (k:ℕ) := congr_arg neg_succ_of_nat (nat.shiftl'_add _ _ _ _)
| (m : ℕ) n -[1+k] := sub_nat_nat_elim n k.succ
(λ n k i, shiftl ↑m i = nat.shiftr (nat.shiftl m n) k)
(λ i n, congr_arg coe $
by rw [← nat.shiftl_sub, nat.add_sub_cancel_left]; apply nat.le_add_right)
(λ i n, congr_arg coe $
by rw [add_assoc, nat.shiftr_add, ← nat.shiftl_sub, nat.sub_self]; refl)
| -[1+ m] n -[1+k] := sub_nat_nat_elim n k.succ
(λ n k i, shiftl -[1+ m] i = -[1+ nat.shiftr (nat.shiftl' tt m n) k])
(λ i n, congr_arg neg_succ_of_nat $
by rw [← nat.shiftl'_sub, nat.add_sub_cancel_left]; apply nat.le_add_right)
(λ i n, congr_arg neg_succ_of_nat $
by rw [add_assoc, nat.shiftr_add, ← nat.shiftl'_sub, nat.sub_self]; refl)
lemma shiftl_sub (m : ℤ) (n : ℕ) (k : ℤ) : shiftl m (n - k) = shiftr (shiftl m n) k :=
shiftl_add _ _ _
@[simp] lemma shiftl_neg (m n : ℤ) : shiftl m (-n) = shiftr m n := rfl
@[simp] lemma shiftr_neg (m n : ℤ) : shiftr m (-n) = shiftl m n := by rw [← shiftl_neg, neg_neg]
@[simp] lemma shiftl_coe_nat (m n : ℕ) : shiftl m n = nat.shiftl m n := rfl
@[simp] lemma shiftr_coe_nat (m n : ℕ) : shiftr m n = nat.shiftr m n := by cases n; refl
@[simp] lemma shiftl_neg_succ (m n : ℕ) : shiftl -[1+ m] n = -[1+ nat.shiftl' tt m n] := rfl
@[simp] lemma shiftr_neg_succ (m n : ℕ) : shiftr -[1+ m] n = -[1+ nat.shiftr m n] := by cases n; refl
lemma shiftr_add : ∀ (m : ℤ) (n k : ℕ), shiftr m (n + k) = shiftr (shiftr m n) k
| (m : ℕ) n k := by rw [shiftr_coe_nat, shiftr_coe_nat,
← int.coe_nat_add, shiftr_coe_nat, nat.shiftr_add]
| -[1+ m] n k := by rw [shiftr_neg_succ, shiftr_neg_succ,
← int.coe_nat_add, shiftr_neg_succ, nat.shiftr_add]
lemma shiftl_eq_mul_pow : ∀ (m : ℤ) (n : ℕ), shiftl m n = m * ↑(2 ^ n)
| (m : ℕ) n := congr_arg coe (nat.shiftl_eq_mul_pow _ _)
| -[1+ m] n := @congr_arg ℕ ℤ _ _ (λi, -i) (nat.shiftl'_tt_eq_mul_pow _ _)
lemma shiftr_eq_div_pow : ∀ (m : ℤ) (n : ℕ), shiftr m n = m / ↑(2 ^ n)
| (m : ℕ) n := by rw shiftr_coe_nat; exact congr_arg coe (nat.shiftr_eq_div_pow _ _)
| -[1+ m] n := begin
rw [shiftr_neg_succ, neg_succ_of_nat_div, nat.shiftr_eq_div_pow], refl,
exact coe_nat_lt_coe_nat_of_lt (nat.pos_pow_of_pos _ dec_trivial)
end
lemma one_shiftl (n : ℕ) : shiftl 1 n = (2 ^ n : ℕ) :=
congr_arg coe (nat.one_shiftl _)
@[simp] lemma zero_shiftl : ∀ n : ℤ, shiftl 0 n = 0
| (n : ℕ) := congr_arg coe (nat.zero_shiftl _)
| -[1+ n] := congr_arg coe (nat.zero_shiftr _)
@[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := zero_shiftl _
/- Least upper bound property for integers -/
theorem exists_least_of_bdd {P : ℤ → Prop} [HP : decidable_pred P]
(Hbdd : ∃ b : ℤ, ∀ z : ℤ, P z → b ≤ z)
(Hinh : ∃ z : ℤ, P z) : ∃ lb : ℤ, P lb ∧ (∀ z : ℤ, P z → lb ≤ z) :=
let ⟨b, Hb⟩ := Hbdd in
have EX : ∃ n : ℕ, P (b + n), from
let ⟨elt, Helt⟩ := Hinh in
match elt, le.dest (Hb _ Helt), Helt with
| ._, ⟨n, rfl⟩, Hn := ⟨n, Hn⟩
end,
⟨b + (nat.find EX : ℤ), nat.find_spec EX, λ z h,
match z, le.dest (Hb _ h), h with
| ._, ⟨n, rfl⟩, h := add_le_add_left
(int.coe_nat_le.2 $ nat.find_min' _ h) _
end⟩
theorem exists_greatest_of_bdd {P : ℤ → Prop} [HP : decidable_pred P]
(Hbdd : ∃ b : ℤ, ∀ z : ℤ, P z → z ≤ b)
(Hinh : ∃ z : ℤ, P z) : ∃ ub : ℤ, P ub ∧ (∀ z : ℤ, P z → z ≤ ub) :=
have Hbdd' : ∃ (b : ℤ), ∀ (z : ℤ), P (-z) → b ≤ z, from
let ⟨b, Hb⟩ := Hbdd in ⟨-b, λ z h, neg_le.1 (Hb _ h)⟩,
have Hinh' : ∃ z : ℤ, P (-z), from
let ⟨elt, Helt⟩ := Hinh in ⟨-elt, by rw [neg_neg]; exact Helt⟩,
let ⟨lb, Plb, al⟩ := exists_least_of_bdd Hbdd' Hinh' in
⟨-lb, Plb, λ z h, le_neg.1 $ al _ $ by rwa neg_neg⟩
/- cast (injection into groups with one) -/
-- We use the int.has_coe instance for the simp-normal form.
-- Increase the priority so that it is used preferentially.
attribute [priority 1001] int.has_coe
@[simp] theorem nat_cast_eq_coe_nat : ∀ n,
@coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ nat.cast_coe)) n =
@coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ int.has_coe)) n
| 0 := rfl
| (n+1) := congr_arg (+(1:ℤ)) (nat_cast_eq_coe_nat n)
section cast
variables {α : Type*}
section
variables [has_zero α] [has_one α] [has_add α] [has_neg α]
/-- Canonical homomorphism from the integers to any ring(-like) structure `α` -/
protected def cast : ℤ → α
| (n : ℕ) := n
| -[1+ n] := -(n+1)
@[priority 10] instance cast_coe : has_coe ℤ α := ⟨int.cast⟩
@[simp, squash_cast] theorem cast_zero : ((0 : ℤ) : α) = 0 := rfl
theorem cast_of_nat (n : ℕ) : (of_nat n : α) = n := rfl
@[simp, squash_cast] theorem cast_coe_nat (n : ℕ) : ((n : ℤ) : α) = n := rfl
theorem cast_coe_nat' (n : ℕ) :
(@coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ nat.cast_coe)) n : α) = n :=
by simp
@[simp, move_cast] theorem cast_neg_succ_of_nat (n : ℕ) : (-[1+ n] : α) = -(n + 1) := rfl
end
@[simp, squash_cast] theorem cast_one [add_monoid α] [has_one α] [has_neg α] : ((1 : ℤ) : α) = 1 := nat.cast_one
@[simp, move_cast] theorem cast_sub_nat_nat [add_group α] [has_one α] (m n) : ((int.sub_nat_nat m n : ℤ) : α) = m - n :=
begin
unfold sub_nat_nat, cases e : n - m,
{ simp [sub_nat_nat, e, nat.le_of_sub_eq_zero e] },
{ rw [sub_nat_nat, cast_neg_succ_of_nat, ← nat.cast_succ, ← e,
nat.cast_sub $ _root_.le_of_lt $ nat.lt_of_sub_eq_succ e, neg_sub] },
end
@[simp, move_cast] theorem cast_neg_of_nat [add_group α] [has_one α] : ∀ n, ((neg_of_nat n : ℤ) : α) = -n
| 0 := neg_zero.symm
| (n+1) := rfl
@[simp, move_cast] theorem cast_add [add_group α] [has_one α] : ∀ m n, ((m + n : ℤ) : α) = m + n
| (m : ℕ) (n : ℕ) := nat.cast_add _ _
| (m : ℕ) -[1+ n] := cast_sub_nat_nat _ _
| -[1+ m] (n : ℕ) := (cast_sub_nat_nat _ _).trans $ sub_eq_of_eq_add $
show (n:α) = -(m+1) + n + (m+1),
by rw [add_assoc, ← cast_succ, ← nat.cast_add, add_comm,
nat.cast_add, cast_succ, neg_add_cancel_left]
| -[1+ m] -[1+ n] := show -((m + n + 1 + 1 : ℕ) : α) = -(m + 1) + -(n + 1),
begin
rw [← neg_add_rev, ← nat.cast_add_one, ← nat.cast_add_one, ← nat.cast_add],
apply congr_arg (λ x:ℕ, -(x:α)),
ac_refl
end
@[simp, move_cast] theorem cast_neg [add_group α] [has_one α] : ∀ n, ((-n : ℤ) : α) = -n
| (n : ℕ) := cast_neg_of_nat _
| -[1+ n] := (neg_neg _).symm
@[move_cast] theorem cast_sub [add_group α] [has_one α] (m n) : ((m - n : ℤ) : α) = m - n :=
by simp [sub_eq_add_neg]
@[simp] theorem cast_eq_zero [add_group α] [has_one α] [char_zero α] {n : ℤ} : (n : α) = 0 ↔ n = 0 :=
⟨λ h, begin cases n,
{ exact congr_arg coe (nat.cast_eq_zero.1 h) },
{ rw [cast_neg_succ_of_nat, neg_eq_zero, ← cast_succ, nat.cast_eq_zero] at h,
contradiction }
end, λ h, by rw [h, cast_zero]⟩
@[simp, elim_cast] theorem cast_inj [add_group α] [has_one α] [char_zero α] {m n : ℤ} : (m : α) = n ↔ m = n :=
by rw [← sub_eq_zero, ← cast_sub, cast_eq_zero, sub_eq_zero]
theorem cast_injective [add_group α] [has_one α] [char_zero α] : function.injective (coe : ℤ → α)
| m n := cast_inj.1
theorem cast_ne_zero [add_group α] [has_one α] [char_zero α] {n : ℤ} : (n : α) ≠ 0 ↔ n ≠ 0 :=
not_congr cast_eq_zero
@[simp, move_cast] theorem cast_mul [ring α] : ∀ m n, ((m * n : ℤ) : α) = m * n
| (m : ℕ) (n : ℕ) := nat.cast_mul _ _
| (m : ℕ) -[1+ n] := (cast_neg_of_nat _).trans $
show (-(m * (n + 1) : ℕ) : α) = m * -(n + 1),
by rw [nat.cast_mul, nat.cast_add_one, neg_mul_eq_mul_neg]
| -[1+ m] (n : ℕ) := (cast_neg_of_nat _).trans $
show (-((m + 1) * n : ℕ) : α) = -(m + 1) * n,
by rw [nat.cast_mul, nat.cast_add_one, neg_mul_eq_neg_mul]
| -[1+ m] -[1+ n] := show (((m + 1) * (n + 1) : ℕ) : α) = -(m + 1) * -(n + 1),
by rw [nat.cast_mul, nat.cast_add_one, nat.cast_add_one, neg_mul_neg]
instance cast.is_ring_hom [ring α] :
is_ring_hom (int.cast : ℤ → α) :=
⟨cast_one, cast_mul, cast_add⟩
instance coe.is_ring_hom [ring α] : is_ring_hom (coe : ℤ → α) := cast.is_ring_hom
theorem mul_cast_comm [ring α] (a : α) (n : ℤ) : a * n = n * a :=
by cases n; simp [nat.mul_cast_comm, left_distrib, right_distrib, *]
@[simp, squash_cast, move_cast] theorem coe_nat_bit0 (n : ℕ) : (↑(bit0 n) : ℤ) = bit0 ↑n := by {unfold bit0, simp}
@[simp, squash_cast, move_cast] theorem coe_nat_bit1 (n : ℕ) : (↑(bit1 n) : ℤ) = bit1 ↑n := by {unfold bit1, unfold bit0, simp}
@[simp, squash_cast, move_cast] theorem cast_bit0 [ring α] (n : ℤ) : ((bit0 n : ℤ) : α) = bit0 n := cast_add _ _
@[simp, squash_cast, move_cast] theorem cast_bit1 [ring α] (n : ℤ) : ((bit1 n : ℤ) : α) = bit1 n :=
by rw [bit1, cast_add, cast_one, cast_bit0]; refl
lemma cast_two [ring α] : ((2 : ℤ) : α) = 2 := by simp
theorem cast_nonneg [linear_ordered_ring α] : ∀ {n : ℤ}, (0 : α) ≤ n ↔ 0 ≤ n
| (n : ℕ) := by simp
| -[1+ n] := by simpa [not_le_of_gt (neg_succ_lt_zero n)] using
show -(n:α) < 1, from lt_of_le_of_lt (by simp) zero_lt_one
@[simp, elim_cast] theorem cast_le [linear_ordered_ring α] {m n : ℤ} : (m : α) ≤ n ↔ m ≤ n :=
by rw [← sub_nonneg, ← cast_sub, cast_nonneg, sub_nonneg]
@[simp, elim_cast] theorem cast_lt [linear_ordered_ring α] {m n : ℤ} : (m : α) < n ↔ m < n :=
by simpa [-cast_le] using not_congr (@cast_le α _ n m)
@[simp] theorem cast_nonpos [linear_ordered_ring α] {n : ℤ} : (n : α) ≤ 0 ↔ n ≤ 0 :=
by rw [← cast_zero, cast_le]
@[simp] theorem cast_pos [linear_ordered_ring α] {n : ℤ} : (0 : α) < n ↔ 0 < n :=
by rw [← cast_zero, cast_lt]
@[simp] theorem cast_lt_zero [linear_ordered_ring α] {n : ℤ} : (n : α) < 0 ↔ n < 0 :=
by rw [← cast_zero, cast_lt]
theorem eq_cast [add_group α] [has_one α] (f : ℤ → α)
(H1 : f 1 = 1) (Hadd : ∀ x y, f (x + y) = f x + f y) (n : ℤ) : f n = n :=
begin
have H : ∀ (n : ℕ), f n = n :=
nat.eq_cast' (λ n, f n) H1 (λ x y, Hadd x y),
cases n, {apply H},
apply eq_neg_of_add_eq_zero,
rw [← nat.cast_zero, ← H 0, int.coe_nat_zero,
← show -[1+ n] + (↑n + 1) = 0, from neg_add_self (↑n+1),
Hadd, show f (n+1) = n+1, from H (n+1)]
end
lemma eq_cast' [ring α] (f : ℤ → α) [is_ring_hom f] : f = int.cast :=
funext $ int.eq_cast f (is_ring_hom.map_one f) (λ _ _, is_ring_hom.map_add f)
@[simp, squash_cast] theorem cast_id (n : ℤ) : ↑n = n :=
(eq_cast id rfl (λ _ _, rfl) n).symm
@[simp, move_cast] theorem cast_min [decidable_linear_ordered_comm_ring α] {a b : ℤ} :
(↑(min a b) : α) = min a b :=
by by_cases a ≤ b; simp [h, min]
@[simp, move_cast] theorem cast_max [decidable_linear_ordered_comm_ring α] {a b : ℤ} :
(↑(max a b) : α) = max a b :=
by by_cases a ≤ b; simp [h, max]
@[simp, move_cast] theorem cast_abs [decidable_linear_ordered_comm_ring α] {q : ℤ} :
((abs q : ℤ) : α) = abs q :=
by simp [abs]
end cast
section decidable
def range (m n : ℤ) : list ℤ :=
(list.range (to_nat (n-m))).map $ λ r, m+r
theorem mem_range_iff {m n r : ℤ} : r ∈ range m n ↔ m ≤ r ∧ r < n :=
⟨λ H, let ⟨s, h1, h2⟩ := list.mem_map.1 H in h2 ▸
⟨le_add_of_nonneg_right trivial,
add_lt_of_lt_sub_left $ match n-m, h1 with
| (k:ℕ), h1 := by rwa [list.mem_range, to_nat_coe_nat, ← coe_nat_lt] at h1
end⟩,
λ ⟨h1, h2⟩, list.mem_map.2 ⟨to_nat (r-m),
list.mem_range.2 $ by rw [← coe_nat_lt, to_nat_of_nonneg (sub_nonneg_of_le h1),
to_nat_of_nonneg (sub_nonneg_of_le (le_of_lt (lt_of_le_of_lt h1 h2)))];
exact sub_lt_sub_right h2 _,
show m + _ = _, by rw [to_nat_of_nonneg (sub_nonneg_of_le h1), add_sub_cancel'_right]⟩⟩
instance decidable_le_lt (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m ≤ r → r < n → P r) :=
decidable_of_iff (∀ r ∈ range m n, P r) $ by simp only [mem_range_iff, and_imp]
instance decidable_le_le (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m ≤ r → r ≤ n → P r) :=
decidable_of_iff (∀ r ∈ range m (n+1), P r) $ by simp only [mem_range_iff, and_imp, lt_add_one_iff]
instance decidable_lt_lt (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m < r → r < n → P r) :=
int.decidable_le_lt P _ _
instance decidable_lt_le (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m < r → r ≤ n → P r) :=
int.decidable_le_le P _ _
end decidable
end int
section ring_hom
variables {α : Type*} {β : Type*} [ring α] [ring β]
lemma is_ring_hom.map_int_cast (f : α → β) [is_ring_hom f] (n : ℤ) : f n = n :=
int.eq_cast (λ n : ℤ, f n) (by simp [is_ring_hom.map_one f])
(by simp [is_ring_hom.map_add f]) _
lemma ring_hom.map_int_cast (f : α →+* β) (n : ℤ) : f n = n :=
is_ring_hom.map_int_cast _ _
end ring_hom
|
1033145a8e4a3c19dfd5df935ddc79d9028fb22d | 1abd1ed12aa68b375cdef28959f39531c6e95b84 | /src/field_theory/splitting_field.lean | 2805f90bf7b6eb24a3e7e919279ddc69b59fcdff | [
"Apache-2.0"
] | permissive | jumpy4/mathlib | d3829e75173012833e9f15ac16e481e17596de0f | af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13 | refs/heads/master | 1,693,508,842,818 | 1,636,203,271,000 | 1,636,203,271,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 37,554 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import ring_theory.adjoin_root
import ring_theory.algebra_tower
import ring_theory.algebraic
import ring_theory.polynomial
import field_theory.minpoly
import linear_algebra.finite_dimensional
import tactic.field_simp
import algebra.polynomial.big_operators
/-!
# Splitting fields
This file introduces the notion of a splitting field of a polynomial and provides an embedding from
a splitting field to any field that splits the polynomial. A polynomial `f : polynomial K` splits
over a field extension `L` of `K` if it is zero or all of its irreducible factors over `L` have
degree `1`. A field extension of `K` of a polynomial `f : polynomial K` is called a splitting field
if it is the smallest field extension of `K` such that `f` splits.
## Main definitions
* `polynomial.splits i f`: A predicate on a field homomorphism `i : K → L` and a polynomial `f`
saying that `f` is zero or all of its irreducible factors over `L` have degree `1`.
* `polynomial.splitting_field f`: A fixed splitting field of the polynomial `f`.
* `polynomial.is_splitting_field`: A predicate on a field to be a splitting field of a polynomial
`f`.
## Main statements
* `polynomial.C_leading_coeff_mul_prod_multiset_X_sub_C`: If a polynomial has as many roots as its
degree, it can be written as the product of its leading coefficient with `∏ (X - a)` where `a`
ranges through its roots.
* `lift_of_splits`: If `K` and `L` are field extensions of a field `F` and for some finite subset
`S` of `K`, the minimal polynomial of every `x ∈ K` splits as a polynomial with coefficients in
`L`, then `algebra.adjoin F S` embeds into `L`.
* `polynomial.is_splitting_field.lift`: An embedding of a splitting field of the polynomial `f` into
another field such that `f` splits.
* `polynomial.is_splitting_field.alg_equiv`: Every splitting field of a polynomial `f` is isomorpic
to `splitting_field f` and thus, being a splitting field is unique up to isomorphism.
-/
noncomputable theory
open_locale classical big_operators
universes u v w
variables {F : Type u} {K : Type v} {L : Type w}
namespace polynomial
variables [field K] [field L] [field F]
open polynomial
section splits
variables (i : K →+* L)
/-- A polynomial `splits` iff it is zero or all of its irreducible factors have `degree` 1. -/
def splits (f : polynomial K) : Prop :=
f = 0 ∨ ∀ {g : polynomial L}, irreducible g → g ∣ f.map i → degree g = 1
@[simp] lemma splits_zero : splits i (0 : polynomial K) := or.inl rfl
@[simp] lemma splits_C (a : K) : splits i (C a) :=
if ha : a = 0 then ha.symm ▸ (@C_0 K _).symm ▸ splits_zero i
else
have hia : i a ≠ 0, from mt ((i.injective_iff).1
i.injective _) ha,
or.inr $ λ g hg ⟨p, hp⟩, absurd hg.1 (not_not.2 (is_unit_iff_degree_eq_zero.2 $
by have := congr_arg degree hp;
simp [degree_C hia, @eq_comm (with_bot ℕ) 0,
nat.with_bot.add_eq_zero_iff] at this; clear _fun_match; tauto))
lemma splits_of_degree_eq_one {f : polynomial K} (hf : degree f = 1) : splits i f :=
or.inr $ λ g hg ⟨p, hp⟩,
by have := congr_arg degree hp;
simp [nat.with_bot.add_eq_one_iff, hf, @eq_comm (with_bot ℕ) 1,
mt is_unit_iff_degree_eq_zero.2 hg.1] at this;
clear _fun_match; tauto
lemma splits_of_degree_le_one {f : polynomial K} (hf : degree f ≤ 1) : splits i f :=
begin
cases h : degree f with n,
{ rw [degree_eq_bot.1 h]; exact splits_zero i },
{ cases n with n,
{ rw [eq_C_of_degree_le_zero (trans_rel_right (≤) h (le_refl _))];
exact splits_C _ _ },
{ have hn : n = 0,
{ rw h at hf,
cases n, { refl }, { exact absurd hf dec_trivial } },
exact splits_of_degree_eq_one _ (by rw [h, hn]; refl) } }
end
lemma splits_of_nat_degree_le_one {f : polynomial K} (hf : nat_degree f ≤ 1) : splits i f :=
splits_of_degree_le_one i (degree_le_of_nat_degree_le hf)
lemma splits_of_nat_degree_eq_one {f : polynomial K} (hf : nat_degree f = 1) : splits i f :=
splits_of_nat_degree_le_one i (le_of_eq hf)
lemma splits_mul {f g : polynomial K} (hf : splits i f) (hg : splits i g) : splits i (f * g) :=
if h : f * g = 0 then by simp [h]
else or.inr $ λ p hp hpf, ((principal_ideal_ring.irreducible_iff_prime.1 hp).2.2 _ _
(show p ∣ map i f * map i g, by convert hpf; rw polynomial.map_mul)).elim
(hf.resolve_left (λ hf, by simpa [hf] using h) hp)
(hg.resolve_left (λ hg, by simpa [hg] using h) hp)
lemma splits_of_splits_mul {f g : polynomial K} (hfg : f * g ≠ 0) (h : splits i (f * g)) :
splits i f ∧ splits i g :=
⟨or.inr $ λ g hgi hg, or.resolve_left h hfg hgi
(by rw map_mul; exact hg.trans (dvd_mul_right _ _)),
or.inr $ λ g hgi hg, or.resolve_left h hfg hgi
(by rw map_mul; exact hg.trans (dvd_mul_left _ _))⟩
lemma splits_of_splits_of_dvd {f g : polynomial K} (hf0 : f ≠ 0) (hf : splits i f) (hgf : g ∣ f) :
splits i g :=
by { obtain ⟨f, rfl⟩ := hgf, exact (splits_of_splits_mul i hf0 hf).1 }
lemma splits_of_splits_gcd_left {f g : polynomial K} (hf0 : f ≠ 0) (hf : splits i f) :
splits i (euclidean_domain.gcd f g) :=
polynomial.splits_of_splits_of_dvd i hf0 hf (euclidean_domain.gcd_dvd_left f g)
lemma splits_of_splits_gcd_right {f g : polynomial K} (hg0 : g ≠ 0) (hg : splits i g) :
splits i (euclidean_domain.gcd f g) :=
polynomial.splits_of_splits_of_dvd i hg0 hg (euclidean_domain.gcd_dvd_right f g)
lemma splits_map_iff (j : L →+* F) {f : polynomial K} :
splits j (f.map i) ↔ splits (j.comp i) f :=
by simp [splits, polynomial.map_map]
theorem splits_one : splits i 1 :=
splits_C i 1
theorem splits_of_is_unit {u : polynomial K} (hu : is_unit u) : u.splits i :=
splits_of_splits_of_dvd i one_ne_zero (splits_one _) $ is_unit_iff_dvd_one.1 hu
theorem splits_X_sub_C {x : K} : (X - C x).splits i :=
splits_of_degree_eq_one _ $ degree_X_sub_C x
theorem splits_X : X.splits i :=
splits_of_degree_eq_one _ $ degree_X
theorem splits_id_iff_splits {f : polynomial K} :
(f.map i).splits (ring_hom.id L) ↔ f.splits i :=
by rw [splits_map_iff, ring_hom.id_comp]
theorem splits_mul_iff {f g : polynomial K} (hf : f ≠ 0) (hg : g ≠ 0) :
(f * g).splits i ↔ f.splits i ∧ g.splits i :=
⟨splits_of_splits_mul i (mul_ne_zero hf hg), λ ⟨hfs, hgs⟩, splits_mul i hfs hgs⟩
theorem splits_prod {ι : Type u} {s : ι → polynomial K} {t : finset ι} :
(∀ j ∈ t, (s j).splits i) → (∏ x in t, s x).splits i :=
begin
refine finset.induction_on t (λ _, splits_one i) (λ a t hat ih ht, _),
rw finset.forall_mem_insert at ht, rw finset.prod_insert hat,
exact splits_mul i ht.1 (ih ht.2)
end
lemma splits_pow {f : polynomial K} (hf : f.splits i) (n : ℕ) : (f ^ n).splits i :=
begin
rw [←finset.card_range n, ←finset.prod_const],
exact splits_prod i (λ j hj, hf),
end
lemma splits_X_pow (n : ℕ) : (X ^ n).splits i := splits_pow i (splits_X i) n
theorem splits_prod_iff {ι : Type u} {s : ι → polynomial K} {t : finset ι} :
(∀ j ∈ t, s j ≠ 0) → ((∏ x in t, s x).splits i ↔ ∀ j ∈ t, (s j).splits i) :=
begin
refine finset.induction_on t (λ _, ⟨λ _ _ h, h.elim, λ _, splits_one i⟩) (λ a t hat ih ht, _),
rw finset.forall_mem_insert at ht ⊢,
rw [finset.prod_insert hat, splits_mul_iff i ht.1 (finset.prod_ne_zero_iff.2 ht.2), ih ht.2]
end
lemma degree_eq_one_of_irreducible_of_splits {p : polynomial L}
(h_nz : p ≠ 0) (hp : irreducible p) (hp_splits : splits (ring_hom.id L) p) :
p.degree = 1 :=
begin
rcases hp_splits,
{ contradiction },
{ apply hp_splits hp, simp }
end
lemma exists_root_of_splits {f : polynomial K} (hs : splits i f) (hf0 : degree f ≠ 0) :
∃ x, eval₂ i x f = 0 :=
if hf0 : f = 0 then ⟨37, by simp [hf0]⟩
else
let ⟨g, hg⟩ := wf_dvd_monoid.exists_irreducible_factor
(show ¬ is_unit (f.map i), from mt is_unit_iff_degree_eq_zero.1 (by rwa degree_map))
(map_ne_zero hf0) in
let ⟨x, hx⟩ := exists_root_of_degree_eq_one (hs.resolve_left hf0 hg.1 hg.2) in
let ⟨i, hi⟩ := hg.2 in
⟨x, by rw [← eval_map, hi, eval_mul, show _ = _, from hx, zero_mul]⟩
lemma exists_multiset_of_splits {f : polynomial K} : splits i f →
∃ (s : multiset L), f.map i = C (i f.leading_coeff) *
(s.map (λ a : L, (X : polynomial L) - C a)).prod :=
suffices splits (ring_hom.id _) (f.map i) → ∃ s : multiset L, f.map i =
(C (f.map i).leading_coeff) * (s.map (λ a : L, (X : polynomial L) - C a)).prod,
by rwa [splits_map_iff, leading_coeff_map i] at this,
wf_dvd_monoid.induction_on_irreducible (f.map i)
(λ _, ⟨{37}, by simp [i.map_zero]⟩)
(λ u hu _, ⟨0,
by conv_lhs { rw eq_C_of_degree_eq_zero (is_unit_iff_degree_eq_zero.1 hu) };
simp [leading_coeff, nat_degree_eq_of_degree_eq_some (is_unit_iff_degree_eq_zero.1 hu)]⟩)
(λ f p hf0 hp ih hfs,
have hpf0 : p * f ≠ 0, from mul_ne_zero hp.ne_zero hf0,
let ⟨s, hs⟩ := ih (splits_of_splits_mul _ hpf0 hfs).2 in
⟨-(p * norm_unit p).coeff 0 ::ₘ s,
have hp1 : degree p = 1, from hfs.resolve_left hpf0 hp (by simp),
begin
rw [multiset.map_cons, multiset.prod_cons, leading_coeff_mul, C_mul, mul_assoc,
mul_left_comm (C f.leading_coeff), ← hs, ← mul_assoc, mul_left_inj' hf0],
conv_lhs {rw eq_X_add_C_of_degree_eq_one hp1},
simp only [mul_add, coe_norm_unit_of_ne_zero hp.ne_zero, mul_comm p, coeff_neg,
C_neg, sub_eq_add_neg, neg_neg, coeff_C_mul, (mul_assoc _ _ _).symm, C_mul.symm,
mul_inv_cancel (show p.leading_coeff ≠ 0, from mt leading_coeff_eq_zero.1
hp.ne_zero), one_mul],
end⟩)
/-- Pick a root of a polynomial that splits. -/
def root_of_splits {f : polynomial K} (hf : f.splits i) (hfd : f.degree ≠ 0) : L :=
classical.some $ exists_root_of_splits i hf hfd
theorem map_root_of_splits {f : polynomial K} (hf : f.splits i) (hfd) :
f.eval₂ i (root_of_splits i hf hfd) = 0 :=
classical.some_spec $ exists_root_of_splits i hf hfd
theorem roots_map {f : polynomial K} (hf : f.splits $ ring_hom.id K) :
(f.map i).roots = (f.roots).map i :=
if hf0 : f = 0 then by rw [hf0, map_zero, roots_zero, roots_zero, multiset.map_zero] else
have hmf0 : f.map i ≠ 0 := map_ne_zero hf0,
let ⟨m, hm⟩ := exists_multiset_of_splits _ hf in
have h1 : (0 : polynomial K) ∉ m.map (λ r, X - C r),
from zero_nmem_multiset_map_X_sub_C _ _,
have h2 : (0 : polynomial L) ∉ m.map (λ r, X - C (i r)),
from zero_nmem_multiset_map_X_sub_C _ _,
begin
rw map_id at hm, rw hm at hf0 hmf0 ⊢, rw map_mul at hmf0 ⊢,
rw [roots_mul hf0, roots_mul hmf0, map_C, roots_C, zero_add, roots_C, zero_add,
map_multiset_prod, multiset.map_map], simp_rw [(∘), map_sub, map_X, map_C],
rw [roots_multiset_prod _ h2, multiset.bind_map,
roots_multiset_prod _ h1, multiset.bind_map],
simp_rw roots_X_sub_C,
rw [multiset.bind_singleton, multiset.bind_singleton, multiset.map_id']
end
lemma eq_prod_roots_of_splits {p : polynomial K} {i : K →+* L}
(hsplit : splits i p) :
p.map i = C (i p.leading_coeff) * ((p.map i).roots.map (λ a, X - C a)).prod :=
begin
by_cases p_eq_zero : p = 0,
{ rw [p_eq_zero, map_zero, leading_coeff_zero, i.map_zero, C.map_zero, zero_mul] },
obtain ⟨s, hs⟩ := exists_multiset_of_splits i hsplit,
have map_ne_zero : p.map i ≠ 0 := map_ne_zero (p_eq_zero),
have prod_ne_zero : C (i p.leading_coeff) * (multiset.map (λ a, X - C a) s).prod ≠ 0 :=
by rwa hs at map_ne_zero,
have zero_nmem : (0 : polynomial L) ∉ s.map (λ a, X - C a),
from zero_nmem_multiset_map_X_sub_C _ _,
have map_bind_roots_eq : (s.map (λ a, X - C a)).bind (λ a, a.roots) = s,
{ refine multiset.induction_on s (by rw [multiset.map_zero, multiset.zero_bind]) _,
intros a s ih,
rw [multiset.map_cons, multiset.cons_bind, ih, roots_X_sub_C, multiset.singleton_add] },
rw [hs, roots_mul prod_ne_zero, roots_C, zero_add,
roots_multiset_prod _ zero_nmem,
map_bind_roots_eq]
end
lemma eq_prod_roots_of_splits_id {p : polynomial K}
(hsplit : splits (ring_hom.id K) p) :
p = C (p.leading_coeff) * (p.roots.map (λ a, X - C a)).prod :=
by simpa using eq_prod_roots_of_splits hsplit
lemma eq_prod_roots_of_monic_of_splits_id {p : polynomial K}
(m : monic p) (hsplit : splits (ring_hom.id K) p) :
p = (p.roots.map (λ a, X - C a)).prod :=
begin
convert eq_prod_roots_of_splits_id hsplit,
simp [m],
end
lemma eq_X_sub_C_of_splits_of_single_root {x : K} {h : polynomial K} (h_splits : splits i h)
(h_roots : (h.map i).roots = {i x}) : h = (C (leading_coeff h)) * (X - C x) :=
begin
apply polynomial.map_injective _ i.injective,
rw [eq_prod_roots_of_splits h_splits, h_roots],
simp,
end
lemma nat_degree_eq_card_roots {p : polynomial K} {i : K →+* L}
(hsplit : splits i p) : p.nat_degree = (p.map i).roots.card :=
begin
by_cases p_eq_zero : p = 0,
{ rw [p_eq_zero, nat_degree_zero, map_zero, roots_zero, multiset.card_zero] },
have map_ne_zero : p.map i ≠ 0 := map_ne_zero (p_eq_zero),
rw eq_prod_roots_of_splits hsplit at map_ne_zero,
conv_lhs { rw [← nat_degree_map i, eq_prod_roots_of_splits hsplit] },
have : (0 : polynomial L) ∉ (map i p).roots.map (λ a, X - C a),
from zero_nmem_multiset_map_X_sub_C _ _,
simp [nat_degree_mul (left_ne_zero_of_mul map_ne_zero) (right_ne_zero_of_mul map_ne_zero),
nat_degree_multiset_prod _ this]
end
lemma degree_eq_card_roots {p : polynomial K} {i : K →+* L} (p_ne_zero : p ≠ 0)
(hsplit : splits i p) : p.degree = (p.map i).roots.card :=
by rw [degree_eq_nat_degree p_ne_zero, nat_degree_eq_card_roots hsplit]
section UFD
local attribute [instance, priority 10] principal_ideal_ring.to_unique_factorization_monoid
local infix ` ~ᵤ ` : 50 := associated
open unique_factorization_monoid associates
lemma splits_of_exists_multiset {f : polynomial K} {s : multiset L}
(hs : f.map i = C (i f.leading_coeff) * (s.map (λ a : L, (X : polynomial L) - C a)).prod) :
splits i f :=
if hf0 : f = 0 then or.inl hf0
else
or.inr $ λ p hp hdp,
have ht : multiset.rel associated
(normalized_factors (f.map i)) (s.map (λ a : L, (X : polynomial L) - C a)) :=
factors_unique
(λ p hp, irreducible_of_normalized_factor _ hp)
(λ p' m, begin
obtain ⟨a,m,rfl⟩ := multiset.mem_map.1 m,
exact irreducible_of_degree_eq_one (degree_X_sub_C _),
end)
(associated.symm $ calc _ ~ᵤ f.map i :
⟨(units.map C.to_monoid_hom : units L →* units (polynomial L))
(units.mk0 (f.map i).leading_coeff
(mt leading_coeff_eq_zero.1 (map_ne_zero hf0))),
by conv_rhs { rw [hs, ← leading_coeff_map i, mul_comm] }; refl⟩
... ~ᵤ _ : (unique_factorization_monoid.normalized_factors_prod (by simpa using hf0)).symm),
let ⟨q, hq, hpq⟩ := exists_mem_normalized_factors_of_dvd (by simpa) hp hdp in
let ⟨q', hq', hqq'⟩ := multiset.exists_mem_of_rel_of_mem ht hq in
let ⟨a, ha⟩ := multiset.mem_map.1 hq' in
by rw [← degree_X_sub_C a, ha.2];
exact degree_eq_degree_of_associated (hpq.trans hqq')
lemma splits_of_splits_id {f : polynomial K} : splits (ring_hom.id _) f → splits i f :=
unique_factorization_monoid.induction_on_prime f (λ _, splits_zero _)
(λ _ hu _, splits_of_degree_le_one _
((is_unit_iff_degree_eq_zero.1 hu).symm ▸ dec_trivial))
(λ a p ha0 hp ih hfi, splits_mul _
(splits_of_degree_eq_one _
((splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).1.resolve_left
hp.1 hp.irreducible (by rw map_id)))
(ih (splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).2))
end UFD
lemma splits_iff_exists_multiset {f : polynomial K} : splits i f ↔
∃ (s : multiset L), f.map i = C (i f.leading_coeff) *
(s.map (λ a : L, (X : polynomial L) - C a)).prod :=
⟨exists_multiset_of_splits i, λ ⟨s, hs⟩, splits_of_exists_multiset i hs⟩
lemma splits_comp_of_splits (j : L →+* F) {f : polynomial K}
(h : splits i f) : splits (j.comp i) f :=
begin
change i with ((ring_hom.id _).comp i) at h,
rw [← splits_map_iff],
rw [← splits_map_iff i] at h,
exact splits_of_splits_id _ h
end
/-- A monic polynomial `p` that has as many roots as its degree
can be written `p = ∏(X - a)`, for `a` in `p.roots`. -/
lemma prod_multiset_X_sub_C_of_monic_of_roots_card_eq {p : polynomial K}
(hmonic : p.monic) (hroots : p.roots.card = p.nat_degree) :
(multiset.map (λ (a : K), X - C a) p.roots).prod = p :=
begin
have hprodmonic : (multiset.map (λ (a : K), X - C a) p.roots).prod.monic,
{ simp only [prod_multiset_root_eq_finset_root (ne_zero_of_monic hmonic),
monic_prod_of_monic, monic_X_sub_C, monic_pow, forall_true_iff] },
have hdegree : (multiset.map (λ (a : K), X - C a) p.roots).prod.nat_degree = p.nat_degree,
{ rw [← hroots, nat_degree_multiset_prod _ (zero_nmem_multiset_map_X_sub_C _ (λ a : K, a))],
simp only [eq_self_iff_true, mul_one, nat.cast_id, nsmul_eq_mul, multiset.sum_repeat,
multiset.map_const,nat_degree_X_sub_C, function.comp, multiset.map_map] },
obtain ⟨q, hq⟩ := prod_multiset_X_sub_C_dvd p,
have qzero : q ≠ 0,
{ rintro rfl, apply hmonic.ne_zero, simpa only [mul_zero] using hq },
have degp :
p.nat_degree = (multiset.map (λ (a : K), X - C a) p.roots).prod.nat_degree + q.nat_degree,
{ nth_rewrite 0 [hq],
simp only [nat_degree_mul (ne_zero_of_monic hprodmonic) qzero] },
have degq : q.nat_degree = 0,
{ rw hdegree at degp,
exact (add_right_inj p.nat_degree).mp (tactic.ring_exp.add_pf_sum_z degp rfl).symm },
obtain ⟨u, hu⟩ := is_unit_iff_degree_eq_zero.2 ((degree_eq_iff_nat_degree_eq qzero).2 degq),
have hassoc : associated (multiset.map (λ (a : K), X - C a) p.roots).prod p,
{ rw associated, use u, rw [hu, ← hq] },
exact eq_of_monic_of_associated hprodmonic hmonic hassoc
end
/-- A polynomial `p` that has as many roots as its degree
can be written `p = p.leading_coeff * ∏(X - a)`, for `a` in `p.roots`. -/
lemma C_leading_coeff_mul_prod_multiset_X_sub_C {p : polynomial K}
(hroots : p.roots.card = p.nat_degree) :
(C p.leading_coeff) * (multiset.map (λ (a : K), X - C a) p.roots).prod = p :=
begin
by_cases hzero : p = 0,
{ rw [hzero, leading_coeff_zero, ring_hom.map_zero, zero_mul], },
{ have hcoeff : p.leading_coeff ≠ 0,
{ intro h, exact hzero (leading_coeff_eq_zero.1 h) },
have hrootsnorm : (normalize p).roots.card = (normalize p).nat_degree,
{ rw [roots_normalize, normalize_apply, nat_degree_mul hzero (units.ne_zero _), hroots,
coe_norm_unit, nat_degree_C, add_zero], },
have hprod := prod_multiset_X_sub_C_of_monic_of_roots_card_eq (monic_normalize hzero)
hrootsnorm,
rw [roots_normalize, normalize_apply, coe_norm_unit_of_ne_zero hzero] at hprod,
calc (C p.leading_coeff) * (multiset.map (λ (a : K), X - C a) p.roots).prod
= p * C ((p.leading_coeff)⁻¹ * p.leading_coeff) :
by rw [hprod, mul_comm, mul_assoc, ← C_mul]
... = p * C 1 : by field_simp
... = p : by simp only [mul_one, ring_hom.map_one], },
end
/-- A polynomial splits if and only if it has as many roots as its degree. -/
lemma splits_iff_card_roots {p : polynomial K} :
splits (ring_hom.id K) p ↔ p.roots.card = p.nat_degree :=
begin
split,
{ intro H, rw [nat_degree_eq_card_roots H, map_id] },
{ intro hroots,
apply (splits_iff_exists_multiset (ring_hom.id K)).2,
use p.roots,
simp only [ring_hom.id_apply, map_id],
exact (C_leading_coeff_mul_prod_multiset_X_sub_C hroots).symm },
end
end splits
end polynomial
section embeddings
variables (F) [field F]
/-- If `p` is the minimal polynomial of `a` over `F` then `F[a] ≃ₐ[F] F[x]/(p)` -/
def alg_equiv.adjoin_singleton_equiv_adjoin_root_minpoly
{R : Type*} [comm_ring R] [algebra F R] (x : R) :
algebra.adjoin F ({x} : set R) ≃ₐ[F] adjoin_root (minpoly F x) :=
alg_equiv.symm $ alg_equiv.of_bijective
(alg_hom.cod_restrict
(adjoin_root.lift_hom _ x $ minpoly.aeval F x) _
(λ p, adjoin_root.induction_on _ p $ λ p,
(algebra.adjoin_singleton_eq_range_aeval F x).symm ▸
(polynomial.aeval _).mem_range.mpr ⟨p, rfl⟩))
⟨(alg_hom.injective_cod_restrict _ _ _).2 $ (alg_hom.injective_iff _).2 $ λ p,
adjoin_root.induction_on _ p $ λ p hp, ideal.quotient.eq_zero_iff_mem.2 $
ideal.mem_span_singleton.2 $ minpoly.dvd F x hp,
λ y,
let ⟨p, hp⟩ := (set_like.ext_iff.1
(algebra.adjoin_singleton_eq_range_aeval F x) (y : R)).1 y.2 in
⟨adjoin_root.mk _ p, subtype.eq hp⟩⟩
open finset
/-- If a `subalgebra` is finite_dimensional as a submodule then it is `finite_dimensional`. -/
lemma finite_dimensional.of_subalgebra_to_submodule
{K V : Type*} [field K] [ring V] [algebra K V] {s : subalgebra K V}
(h : finite_dimensional K s.to_submodule) : finite_dimensional K s := h
/-- If `K` and `L` are field extensions of `F` and we have `s : finset K` such that
the minimal polynomial of each `x ∈ s` splits in `L` then `algebra.adjoin F s` embeds in `L`. -/
theorem lift_of_splits {F K L : Type*} [field F] [field K] [field L]
[algebra F K] [algebra F L] (s : finset K) :
(∀ x ∈ s, is_integral F x ∧ polynomial.splits (algebra_map F L) (minpoly F x)) →
nonempty (algebra.adjoin F (↑s : set K) →ₐ[F] L) :=
begin
refine finset.induction_on s (λ H, _) (λ a s has ih H, _),
{ rw [coe_empty, algebra.adjoin_empty],
exact ⟨(algebra.of_id F L).comp (algebra.bot_equiv F K)⟩ },
rw forall_mem_insert at H, rcases H with ⟨⟨H1, H2⟩, H3⟩, cases ih H3 with f,
choose H3 H4 using H3,
rw [coe_insert, set.insert_eq, set.union_comm, algebra.adjoin_union_eq_adjoin_adjoin],
letI := (f : algebra.adjoin F (↑s : set K) →+* L).to_algebra,
haveI : finite_dimensional F (algebra.adjoin F (↑s : set K)) := (
(submodule.fg_iff_finite_dimensional _).1
(fg_adjoin_of_finite (set.finite_mem_finset s) H3)).of_subalgebra_to_submodule,
letI := field_of_finite_dimensional F (algebra.adjoin F (↑s : set K)),
have H5 : is_integral (algebra.adjoin F (↑s : set K)) a := is_integral_of_is_scalar_tower a H1,
have H6 : (minpoly (algebra.adjoin F (↑s : set K)) a).splits
(algebra_map (algebra.adjoin F (↑s : set K)) L),
{ refine polynomial.splits_of_splits_of_dvd _
(polynomial.map_ne_zero $ minpoly.ne_zero H1 :
polynomial.map (algebra_map _ _) _ ≠ 0)
((polynomial.splits_map_iff _ _).2 _)
(minpoly.dvd _ _ _),
{ rw ← is_scalar_tower.algebra_map_eq, exact H2 },
{ rw [← is_scalar_tower.aeval_apply, minpoly.aeval] } },
obtain ⟨y, hy⟩ := polynomial.exists_root_of_splits _ H6 (ne_of_lt (minpoly.degree_pos H5)).symm,
refine ⟨subalgebra.of_restrict_scalars _ _ _⟩,
refine (adjoin_root.lift_hom (minpoly (algebra.adjoin F (↑s : set K)) a) y hy).comp _,
exact alg_equiv.adjoin_singleton_equiv_adjoin_root_minpoly (algebra.adjoin F (↑s : set K)) a
end
end embeddings
namespace polynomial
variables [field K] [field L] [field F]
open polynomial
section splitting_field
/-- Non-computably choose an irreducible factor from a polynomial. -/
def factor (f : polynomial K) : polynomial K :=
if H : ∃ g, irreducible g ∧ g ∣ f then classical.some H else X
instance irreducible_factor (f : polynomial K) : irreducible (factor f) :=
begin
rw factor, split_ifs with H, { exact (classical.some_spec H).1 }, { exact irreducible_X }
end
theorem factor_dvd_of_not_is_unit {f : polynomial K} (hf1 : ¬is_unit f) : factor f ∣ f :=
begin
by_cases hf2 : f = 0, { rw hf2, exact dvd_zero _ },
rw [factor, dif_pos (wf_dvd_monoid.exists_irreducible_factor hf1 hf2)],
exact (classical.some_spec $ wf_dvd_monoid.exists_irreducible_factor hf1 hf2).2
end
theorem factor_dvd_of_degree_ne_zero {f : polynomial K} (hf : f.degree ≠ 0) : factor f ∣ f :=
factor_dvd_of_not_is_unit (mt degree_eq_zero_of_is_unit hf)
theorem factor_dvd_of_nat_degree_ne_zero {f : polynomial K} (hf : f.nat_degree ≠ 0) :
factor f ∣ f :=
factor_dvd_of_degree_ne_zero (mt nat_degree_eq_of_degree_eq_some hf)
/-- Divide a polynomial f by X - C r where r is a root of f in a bigger field extension. -/
def remove_factor (f : polynomial K) : polynomial (adjoin_root $ factor f) :=
map (adjoin_root.of f.factor) f /ₘ (X - C (adjoin_root.root f.factor))
theorem X_sub_C_mul_remove_factor (f : polynomial K) (hf : f.nat_degree ≠ 0) :
(X - C (adjoin_root.root f.factor)) * f.remove_factor = map (adjoin_root.of f.factor) f :=
let ⟨g, hg⟩ := factor_dvd_of_nat_degree_ne_zero hf in
mul_div_by_monic_eq_iff_is_root.2 $ by rw [is_root.def, eval_map, hg, eval₂_mul, ← hg,
adjoin_root.eval₂_root, zero_mul]
theorem nat_degree_remove_factor (f : polynomial K) :
f.remove_factor.nat_degree = f.nat_degree - 1 :=
by rw [remove_factor, nat_degree_div_by_monic _ (monic_X_sub_C _), nat_degree_map,
nat_degree_X_sub_C]
theorem nat_degree_remove_factor' {f : polynomial K} {n : ℕ} (hfn : f.nat_degree = n+1) :
f.remove_factor.nat_degree = n :=
by rw [nat_degree_remove_factor, hfn, n.add_sub_cancel]
/-- Auxiliary construction to a splitting field of a polynomial. Uses induction on the degree. -/
def splitting_field_aux (n : ℕ) : Π {K : Type u} [field K], by exactI Π (f : polynomial K),
f.nat_degree = n → Type u :=
nat.rec_on n (λ K _ _ _, K) $ λ n ih K _ f hf, by exactI
ih f.remove_factor (nat_degree_remove_factor' hf)
namespace splitting_field_aux
theorem succ (n : ℕ) (f : polynomial K) (hfn : f.nat_degree = n + 1) :
splitting_field_aux (n+1) f hfn =
splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn) := rfl
instance field (n : ℕ) : Π {K : Type u} [field K], by exactI
Π {f : polynomial K} (hfn : f.nat_degree = n), field (splitting_field_aux n f hfn) :=
nat.rec_on n (λ K _ _ _, ‹field K›) $ λ n ih K _ f hf, ih _
instance inhabited {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n) :
inhabited (splitting_field_aux n f hfn) := ⟨37⟩
instance algebra (n : ℕ) : Π {K : Type u} [field K], by exactI
Π {f : polynomial K} (hfn : f.nat_degree = n), algebra K (splitting_field_aux n f hfn) :=
nat.rec_on n (λ K _ _ _, by exactI algebra.id K) $ λ n ih K _ f hfn,
by exactI @@restrict_scalars.algebra _ _ _ _ _ (ih _) _ _
instance algebra' {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) :
algebra (adjoin_root f.factor) (splitting_field_aux _ _ hfn) :=
splitting_field_aux.algebra n _
instance algebra'' {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) :
algebra K (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) :=
splitting_field_aux.algebra (n+1) hfn
instance algebra''' {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) :
algebra (adjoin_root f.factor)
(splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) :=
splitting_field_aux.algebra n _
instance scalar_tower {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) :
is_scalar_tower K (adjoin_root f.factor) (splitting_field_aux _ _ hfn) :=
is_scalar_tower.of_algebra_map_eq $ λ x, rfl
instance scalar_tower' {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) :
is_scalar_tower K (adjoin_root f.factor)
(splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) :=
is_scalar_tower.of_algebra_map_eq $ λ x, rfl
theorem algebra_map_succ (n : ℕ) (f : polynomial K) (hfn : f.nat_degree = n + 1) :
by exact algebra_map K (splitting_field_aux _ _ hfn) =
(algebra_map (adjoin_root f.factor)
(splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn))).comp
(adjoin_root.of f.factor) :=
rfl
protected theorem splits (n : ℕ) : ∀ {K : Type u} [field K], by exactI
∀ (f : polynomial K) (hfn : f.nat_degree = n),
splits (algebra_map K $ splitting_field_aux n f hfn) f :=
nat.rec_on n (λ K _ _ hf, by exactI splits_of_degree_le_one _
(le_trans degree_le_nat_degree $ hf.symm ▸ with_bot.coe_le_coe.2 zero_le_one)) $ λ n ih K _ f hf,
by { resetI, rw [← splits_id_iff_splits, algebra_map_succ, ← map_map, splits_id_iff_splits,
← X_sub_C_mul_remove_factor f (λ h, by { rw h at hf, cases hf })],
exact splits_mul _ (splits_X_sub_C _) (ih _ _) }
theorem exists_lift (n : ℕ) : ∀ {K : Type u} [field K], by exactI
∀ (f : polynomial K) (hfn : f.nat_degree = n) {L : Type*} [field L], by exactI
∀ (j : K →+* L) (hf : splits j f), ∃ k : splitting_field_aux n f hfn →+* L,
k.comp (algebra_map _ _) = j :=
nat.rec_on n (λ K _ _ _ L _ j _, by exactI ⟨j, j.comp_id⟩) $ λ n ih K _ f hf L _ j hj, by exactI
have hndf : f.nat_degree ≠ 0, by { intro h, rw h at hf, cases hf },
have hfn0 : f ≠ 0, by { intro h, rw h at hndf, exact hndf rfl },
let ⟨r, hr⟩ := exists_root_of_splits _ (splits_of_splits_of_dvd j hfn0 hj
(factor_dvd_of_nat_degree_ne_zero hndf))
(mt is_unit_iff_degree_eq_zero.2 f.irreducible_factor.1) in
have hmf0 : map (adjoin_root.of f.factor) f ≠ 0, from map_ne_zero hfn0,
have hsf : splits (adjoin_root.lift j r hr) f.remove_factor,
by { rw ← X_sub_C_mul_remove_factor _ hndf at hmf0, refine (splits_of_splits_mul _ hmf0 _).2,
rwa [X_sub_C_mul_remove_factor _ hndf, ← splits_id_iff_splits, map_map, adjoin_root.lift_comp_of,
splits_id_iff_splits] },
let ⟨k, hk⟩ := ih f.remove_factor (nat_degree_remove_factor' hf) (adjoin_root.lift j r hr) hsf in
⟨k, by rw [algebra_map_succ, ← ring_hom.comp_assoc, hk, adjoin_root.lift_comp_of]⟩
theorem adjoin_roots (n : ℕ) : ∀ {K : Type u} [field K], by exactI
∀ (f : polynomial K) (hfn : f.nat_degree = n),
algebra.adjoin K (↑(f.map $ algebra_map K $ splitting_field_aux n f hfn).roots.to_finset :
set (splitting_field_aux n f hfn)) = ⊤ :=
nat.rec_on n (λ K _ f hf, by exactI algebra.eq_top_iff.2 (λ x, subalgebra.range_le _ ⟨x, rfl⟩)) $
λ n ih K _ f hfn, by exactI
have hndf : f.nat_degree ≠ 0, by { intro h, rw h at hfn, cases hfn },
have hfn0 : f ≠ 0, by { intro h, rw h at hndf, exact hndf rfl },
have hmf0 : map (algebra_map K (splitting_field_aux n.succ f hfn)) f ≠ 0 := map_ne_zero hfn0,
by { rw [algebra_map_succ, ← map_map, ← X_sub_C_mul_remove_factor _ hndf, map_mul] at hmf0 ⊢,
rw [roots_mul hmf0, map_sub, map_X, map_C, roots_X_sub_C, multiset.to_finset_add, finset.coe_union,
multiset.to_finset_singleton, finset.coe_singleton,
algebra.adjoin_union_eq_adjoin_adjoin, ← set.image_singleton,
algebra.adjoin_algebra_map K (adjoin_root f.factor)
(splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)),
adjoin_root.adjoin_root_eq_top, algebra.map_top,
is_scalar_tower.adjoin_range_to_alg_hom K (adjoin_root f.factor)
(splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)),
ih, subalgebra.restrict_scalars_top] }
end splitting_field_aux
/-- A splitting field of a polynomial. -/
def splitting_field (f : polynomial K) :=
splitting_field_aux _ f rfl
namespace splitting_field
variables (f : polynomial K)
instance : field (splitting_field f) :=
splitting_field_aux.field _ _
instance inhabited : inhabited (splitting_field f) := ⟨37⟩
instance : algebra K (splitting_field f) :=
splitting_field_aux.algebra _ _
protected theorem splits : splits (algebra_map K (splitting_field f)) f :=
splitting_field_aux.splits _ _ _
variables [algebra K L] (hb : splits (algebra_map K L) f)
/-- Embeds the splitting field into any other field that splits the polynomial. -/
def lift : splitting_field f →ₐ[K] L :=
{ commutes' := λ r, by { have := classical.some_spec (splitting_field_aux.exists_lift _ _ _ _ hb),
exact ring_hom.ext_iff.1 this r },
.. classical.some (splitting_field_aux.exists_lift _ _ _ _ hb) }
theorem adjoin_roots : algebra.adjoin K
(↑(f.map (algebra_map K $ splitting_field f)).roots.to_finset : set (splitting_field f)) = ⊤ :=
splitting_field_aux.adjoin_roots _ _ _
theorem adjoin_root_set : algebra.adjoin K (f.root_set f.splitting_field) = ⊤ :=
adjoin_roots f
end splitting_field
variables (K L) [algebra K L]
/-- Typeclass characterising splitting fields. -/
class is_splitting_field (f : polynomial K) : Prop :=
(splits [] : splits (algebra_map K L) f)
(adjoin_roots [] : algebra.adjoin K (↑(f.map (algebra_map K L)).roots.to_finset : set L) = ⊤)
namespace is_splitting_field
variables {K}
instance splitting_field (f : polynomial K) : is_splitting_field K (splitting_field f) f :=
⟨splitting_field.splits f, splitting_field.adjoin_roots f⟩
section scalar_tower
variables {K L F} [algebra F K] [algebra F L] [is_scalar_tower F K L]
variables {K}
instance map (f : polynomial F) [is_splitting_field F L f] :
is_splitting_field K L (f.map $ algebra_map F K) :=
⟨by { rw [splits_map_iff, ← is_scalar_tower.algebra_map_eq], exact splits L f },
subalgebra.restrict_scalars_injective F $
by { rw [map_map, ← is_scalar_tower.algebra_map_eq, subalgebra.restrict_scalars_top,
eq_top_iff, ← adjoin_roots L f, algebra.adjoin_le_iff],
exact λ x hx, @algebra.subset_adjoin K _ _ _ _ _ _ hx }⟩
variables {K} (L)
theorem splits_iff (f : polynomial K) [is_splitting_field K L f] :
polynomial.splits (ring_hom.id K) f ↔ (⊤ : subalgebra K L) = ⊥ :=
⟨λ h, eq_bot_iff.2 $ adjoin_roots L f ▸ (roots_map (algebra_map K L) h).symm ▸
algebra.adjoin_le_iff.2 (λ y hy,
let ⟨x, hxs, hxy⟩ := finset.mem_image.1 (by rwa multiset.to_finset_map at hy) in
hxy ▸ set_like.mem_coe.2 $ subalgebra.algebra_map_mem _ _),
λ h, @ring_equiv.to_ring_hom_refl K _ ▸
ring_equiv.trans_symm (ring_equiv.of_bijective _ $ algebra.bijective_algebra_map_iff.2 h) ▸
by { rw ring_equiv.to_ring_hom_trans, exact splits_comp_of_splits _ _ (splits L f) }⟩
theorem mul (f g : polynomial F) (hf : f ≠ 0) (hg : g ≠ 0) [is_splitting_field F K f]
[is_splitting_field K L (g.map $ algebra_map F K)] :
is_splitting_field F L (f * g) :=
⟨(is_scalar_tower.algebra_map_eq F K L).symm ▸ splits_mul _
(splits_comp_of_splits _ _ (splits K f))
((splits_map_iff _ _).1 (splits L $ g.map $ algebra_map F K)),
by rw [map_mul, roots_mul (mul_ne_zero (map_ne_zero hf : f.map (algebra_map F L) ≠ 0)
(map_ne_zero hg)), multiset.to_finset_add, finset.coe_union,
algebra.adjoin_union_eq_adjoin_adjoin,
is_scalar_tower.algebra_map_eq F K L, ← map_map,
roots_map (algebra_map K L) ((splits_id_iff_splits $ algebra_map F K).2 $ splits K f),
multiset.to_finset_map, finset.coe_image, algebra.adjoin_algebra_map, adjoin_roots,
algebra.map_top, is_scalar_tower.adjoin_range_to_alg_hom, ← map_map, adjoin_roots,
subalgebra.restrict_scalars_top]⟩
end scalar_tower
/-- Splitting field of `f` embeds into any field that splits `f`. -/
def lift [algebra K F] (f : polynomial K) [is_splitting_field K L f]
(hf : polynomial.splits (algebra_map K F) f) : L →ₐ[K] F :=
if hf0 : f = 0 then (algebra.of_id K F).comp $
(algebra.bot_equiv K L : (⊥ : subalgebra K L) →ₐ[K] K).comp $
by { rw ← (splits_iff L f).1 (show f.splits (ring_hom.id K), from hf0.symm ▸ splits_zero _),
exact algebra.to_top } else
alg_hom.comp (by { rw ← adjoin_roots L f, exact classical.choice (lift_of_splits _ $ λ y hy,
have aeval y f = 0, from (eval₂_eq_eval_map _).trans $
(mem_roots $ by exact map_ne_zero hf0).1 (multiset.mem_to_finset.mp hy),
⟨(is_algebraic_iff_is_integral _).1 ⟨f, hf0, this⟩,
splits_of_splits_of_dvd _ hf0 hf $ minpoly.dvd _ _ this⟩) })
algebra.to_top
theorem finite_dimensional (f : polynomial K) [is_splitting_field K L f] : finite_dimensional K L :=
⟨@algebra.top_to_submodule K L _ _ _ ▸ adjoin_roots L f ▸
fg_adjoin_of_finite (set.finite_mem_finset _) (λ y hy,
if hf : f = 0
then by { rw [hf, map_zero, roots_zero] at hy, cases hy }
else (is_algebraic_iff_is_integral _).1 ⟨f, hf, (eval₂_eq_eval_map _).trans $
(mem_roots $ by exact map_ne_zero hf).1 (multiset.mem_to_finset.mp hy)⟩)⟩
instance (f : polynomial K) : _root_.finite_dimensional K f.splitting_field :=
finite_dimensional f.splitting_field f
/-- Any splitting field is isomorphic to `splitting_field f`. -/
def alg_equiv (f : polynomial K) [is_splitting_field K L f] : L ≃ₐ[K] splitting_field f :=
begin
refine alg_equiv.of_bijective (lift L f $ splits (splitting_field f) f)
⟨ring_hom.injective (lift L f $ splits (splitting_field f) f).to_ring_hom, _⟩,
haveI := finite_dimensional (splitting_field f) f,
haveI := finite_dimensional L f,
have : finite_dimensional.finrank K L = finite_dimensional.finrank K (splitting_field f) :=
le_antisymm
(linear_map.finrank_le_finrank_of_injective
(show function.injective (lift L f $ splits (splitting_field f) f).to_linear_map, from
ring_hom.injective (lift L f $ splits (splitting_field f) f : L →+* f.splitting_field)))
(linear_map.finrank_le_finrank_of_injective
(show function.injective (lift (splitting_field f) f $ splits L f).to_linear_map, from
ring_hom.injective (lift (splitting_field f) f $ splits L f : f.splitting_field →+* L))),
change function.surjective (lift L f $ splits (splitting_field f) f).to_linear_map,
refine (linear_map.injective_iff_surjective_of_finrank_eq_finrank this).1 _,
exact ring_hom.injective (lift L f $ splits (splitting_field f) f : L →+* f.splitting_field)
end
end is_splitting_field
end splitting_field
end polynomial
|
b6f0fabdd3512349bbaf9c897e8adbfc68dfc0bf | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/Lean3Lib/init/data/char/classes_auto.lean | 7220c6662397b5faada3c9335e1b62b0231884c1 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,151 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Gabriel Ebner
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.data.char.basic
import Mathlib.Lean3Lib.init.data.char.lemmas
import Mathlib.Lean3Lib.init.meta.default
import Mathlib.Lean3Lib.init.data.int.default
namespace Mathlib
namespace char
def is_whitespace (c : char) :=
c ∈
[of_nat (bit0 (bit0 (bit0 (bit0 (bit0 1))))), of_nat (bit1 (bit0 (bit0 1))),
of_nat (bit0 (bit1 (bit0 1)))]
def is_upper (c : char) :=
val c ≥ bit1 (bit0 (bit0 (bit0 (bit0 (bit0 1))))) ∧
val c ≤ bit0 (bit1 (bit0 (bit1 (bit1 (bit0 1)))))
def is_lower (c : char) :=
val c ≥ bit1 (bit0 (bit0 (bit0 (bit0 (bit1 1))))) ∧
val c ≤ bit0 (bit1 (bit0 (bit1 (bit1 (bit1 1)))))
def is_alpha (c : char) := is_upper c ∨ is_lower c
def is_digit (c : char) :=
val c ≥ bit0 (bit0 (bit0 (bit0 (bit1 1)))) ∧ val c ≤ bit1 (bit0 (bit0 (bit1 (bit1 1))))
def is_alphanum (c : char) := is_alpha c ∨ is_digit c
def is_punctuation (c : char) :=
c ∈
[of_nat (bit0 (bit0 (bit0 (bit0 (bit0 1))))), of_nat (bit0 (bit0 (bit1 (bit1 (bit0 1))))),
of_nat (bit0 (bit1 (bit1 (bit1 (bit0 1))))), of_nat (bit1 (bit1 (bit1 (bit1 (bit1 1))))),
of_nat (bit1 (bit0 (bit0 (bit0 (bit0 1))))), of_nat (bit1 (bit1 (bit0 (bit1 (bit1 1))))),
of_nat (bit1 (bit0 (bit1 (bit1 (bit0 1))))), of_nat (bit1 (bit1 (bit1 (bit0 (bit0 1)))))]
def to_lower (c : char) : char :=
let n : ℕ := to_nat c;
ite
(n ≥ bit1 (bit0 (bit0 (bit0 (bit0 (bit0 1))))) ∧ n ≤ bit0 (bit1 (bit0 (bit1 (bit1 (bit0 1))))))
(of_nat (n + bit0 (bit0 (bit0 (bit0 (bit0 1)))))) c
protected instance decidable_is_whitespace : decidable_pred is_whitespace :=
id
fun (c : char) =>
id
(list.decidable_mem c
[of_nat (bit0 (bit0 (bit0 (bit0 (bit0 1))))), of_nat (bit1 (bit0 (bit0 1))),
of_nat (bit0 (bit1 (bit0 1)))])
protected instance decidable_is_upper : decidable_pred is_upper :=
id fun (c : char) => id and.decidable
protected instance decidable_is_lower : decidable_pred is_lower :=
id fun (c : char) => id and.decidable
protected instance decidable_is_alpha : decidable_pred is_alpha :=
id fun (c : char) => id or.decidable
protected instance decidable_is_digit : decidable_pred is_digit :=
id fun (c : char) => id and.decidable
protected instance decidable_is_alphanum : decidable_pred is_alphanum :=
id fun (c : char) => id or.decidable
protected instance decidable_is_punctuation : decidable_pred is_punctuation :=
id
fun (c : char) =>
id
(list.decidable_mem c
[of_nat (bit0 (bit0 (bit0 (bit0 (bit0 1))))), of_nat (bit0 (bit0 (bit1 (bit1 (bit0 1))))),
of_nat (bit0 (bit1 (bit1 (bit1 (bit0 1))))),
of_nat (bit1 (bit1 (bit1 (bit1 (bit1 1))))),
of_nat (bit1 (bit0 (bit0 (bit0 (bit0 1))))),
of_nat (bit1 (bit1 (bit0 (bit1 (bit1 1))))),
of_nat (bit1 (bit0 (bit1 (bit1 (bit0 1))))),
of_nat (bit1 (bit1 (bit1 (bit0 (bit0 1)))))])
end Mathlib |
23159ee633fca047baafa4a18ce9b17a40128f54 | 76df16d6c3760cb415f1294caee997cc4736e09b | /lean/src/cs/mrg.lean | e2ca2edd08ff9df9cd3d2a6dc2b55f0d4c122a77 | [
"MIT"
] | permissive | uw-unsat/leanette-popl22-artifact | 70409d9cbd8921d794d27b7992bf1d9a4087e9fe | 80fea2519e61b45a283fbf7903acdf6d5528dbe7 | refs/heads/master | 1,681,592,449,670 | 1,637,037,431,000 | 1,637,037,431,000 | 414,331,908 | 6 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 11,059 | lean | import tactic.basic
import tactic.split_ifs
import tactic.linarith
import tactic.apply_fun
import .svm
import .lib
namespace sym
variables
{Model SymB SymV D O : Type}
[inhabited Model] [inhabited SymV]
(f : factory Model SymB SymV D O) {m : Model}
lemma factory.merge_φ_eqp {σ : state SymB} {grs : choices SymB (result SymB SymV)} {field : state SymB → SymB} :
(field = state.assumes ∨ field = state.asserts) →
(∀ (i : ℕ) (hi : i < grs.length), state.eqv f.to_has_eval m (grs.nth_le i hi).value.state σ) →
f.to_has_eval.evalB m (f.merge_φ σ grs field) = f.to_has_eval.evalB m (field σ) :=
begin
intros hf hg,
simp only [factory.merge_φ, f.and_sound, bool.to_bool_and, bool.to_bool_coe],
rewrite [←bool.coe_bool_iff],
simp only [and_iff_left_iff_imp, band_coe_iff],
intro ha,
rewrite f.all_iff_forall,
intros i hi,
specialize hg i hi,
simp only [state.eqv] at hg,
cases hf;
simp only [hf] at ha;
simp only [hf, f.imp_sound, bool.of_to_bool_iff, hg];
intro h;
exact ha,
end
lemma factory.merge_σ_eqp {σ : state SymB} {grs : choices SymB (result SymB SymV)} :
(∀ (i : ℕ) (hi : i < grs.length), state.eqv f.to_has_eval m (grs.nth_le i hi).value.state σ) →
state.eqv f.to_has_eval m (f.merge_σ σ grs) σ :=
begin
intros hg,
simp only [factory.merge_σ, state.eqv],
constructor;
apply f.merge_φ_eqp _ hg;
simp only [true_or, eq_self_iff_true, or_true],
end
lemma factory.merge_ρ_eqp {σ : state SymB} {grs : choices SymB (result SymB SymV)} :
(∀ (i : ℕ) (hi : i < grs.length), state.eqv f.to_has_eval m (grs.nth_le i hi).value.state σ) →
state.eqv f.to_has_eval m (f.merge_ρ σ grs).state σ :=
begin
intros hg,
simp only [factory.merge_ρ],
cases (list.all grs (λ (gr : choice SymB (result SymB SymV)), gr.value.is_halt)),
{ simp only [factory.halt_or_ans, bool.coe_sort_ff, if_false],
cases (f.halted (f.merge_σ σ grs)),
{ simp only [result.state, bool.coe_sort_ff, if_false],
apply f.merge_σ_eqp hg, },
{ simp only [result.state, if_true, bool.coe_sort_tt],
apply f.merge_σ_eqp hg, } },
{ simp only [result.state, if_true, bool.coe_sort_tt],
apply f.merge_σ_eqp hg, }
end
lemma factory.merge_υ_map_filter_results {grs : choices SymB (result SymB SymV)} :
(list.map
(λ (gv : choice SymB (result SymB SymV)), choice.mk gv.guard (result.value (default SymV) gv.value))
(list.filter (λ (gv : choice SymB (result SymB SymV)), ↥(f.to_has_eval.evalB m gv.guard)) grs)) =
(list.filter (λ (gv : choice SymB SymV), ↥(f.to_has_eval.evalB m gv.guard))
(list.map (λ (gv : choice SymB (result SymB SymV)), choice.mk gv.guard (result.value (default SymV) gv.value)) grs)) :=
begin
induction grs,
{ simp only [list.filter_nil, list.map], },
{ simp only [list.map, list.filter],
split_ifs,
{ simp only [true_and, eq_self_iff_true, list.map],
apply grs_ih, },
{ apply grs_ih, } }
end
lemma factory.merge_υ_normal {grs : choices SymB (result SymB SymV)}
{gv : choice SymB (result SymB SymV)} :
grs.filter (λ gv, f.evalB m gv.guard) = [gv] →
f.evalV m (f.merge_υ grs) = f.evalV m ((gv).value.value (default SymV)) :=
begin
intros hu,
have hlen : grs.filter (λ gv, f.evalB m gv.guard) = [gv] := hu,
rcases (choices.filter_one_guard (has_eval_result f.to_has_eval) hu) with hg,
simp only [has_eval_result] at hg,
apply_fun (list.map (λ (gv : choice SymB (result SymB SymV)), choice.mk gv.guard (result.value (default SymV) gv.value))) at hu,
simp only [list.map] at hu,
apply_fun (choices.eval f.to_has_eval m (f.evalV m (default SymV))) at hu,
simp only [choices.eval, hg, if_true] at hu,
simp only [factory.merge_υ],
rewrite f.merge_sound,
{ rewrite ←hu,
rewrite factory.merge_υ_map_filter_results,
apply choices.eval_filter, },
{ simp only [choices.one, choices.true, list.map_map],
rewrite list.map_filter,
simp only [list.length_map],
apply_fun list.length at hlen,
simp only [list.length_singleton] at hlen,
have hf : ((λ (g : SymB), ↥(f.to_has_eval.evalB m g)) ∘ choice.guard ∘ λ (gr : choice SymB (result SymB SymV)), {guard := gr.guard, value := result.value (default SymV) gr.value}) =
(λ (gv : choice SymB (result SymB SymV)), ↥(f.to_has_eval.evalB m gv.guard)) :=
by { apply funext, intro x, simp only [eq_iff_iff], },
simp only [hf],
exact hlen, }
end
lemma factory.merge_φ_all_filter_results {grs : choices SymB (result SymB SymV)} {field : state SymB → SymB} :
list.all grs (λ (x : choice SymB (result SymB SymV)), f.evalB m (f.imp x.guard (field x.value.state))) =
list.all (list.filter (λ (gv : choice SymB (result SymB SymV)), (f.evalB m gv.guard)) grs) (λ (x : choice SymB (result SymB SymV)), f.evalB m (f.imp x.guard (field x.value.state))) :=
begin
induction grs,
{ simp only [list.filter_nil], },
{ simp only [list.filter, list.all_cons],
split_ifs,
{ simp only [list.all_cons],
congr, exact grs_ih, },
{ rewrite f.imp_sound,
simp only [h, grs_ih, forall_false_left, to_bool_true_eq_tt, band], } }
end
lemma factory.merge_φ_normal {σ : state SymB} {grs : choices SymB (result SymB SymV)}
{gv : choice SymB (result SymB SymV)} {field : state SymB → SymB} :
(field = state.assumes ∨ field = state.asserts) →
σ.normal f.to_has_eval m →
grs.filter (λ gv, f.evalB m gv.guard) = [gv] →
f.to_has_eval.evalB m (f.merge_φ σ grs field) = f.to_has_eval.evalB m (field gv.value.state) :=
begin
intros hf hn hu,
simp only [state.normal, bool.to_bool_and, bool.to_bool_coe, band_coe_iff] at hn,
simp only [factory.merge_φ, f.and_sound, bool.to_bool_and, bool.to_bool_coe],
have ht : (f.to_has_eval.evalB m (field σ)) = tt := by { rewrite bool.tt_eq_true, cases hf; simp only [hf, hn], },
simp only [ht, band],
rewrite f.all_eq_all,
rewrite [factory.merge_φ_all_filter_results, hu],
rcases (choices.filter_one_guard (has_eval_result f.to_has_eval) hu) with hg,
simp only [has_eval_result] at hg,
simp only [f.imp_sound, hg, bool.to_bool_coe, forall_true_left, list.all_nil, list.all_cons, band_tt],
end
lemma factory.merge_σ_normal {σ : state SymB} {grs : choices SymB (result SymB SymV)} {gv : choice SymB (result SymB SymV)} :
σ.normal f.to_has_eval m →
grs.filter (λ gv, f.evalB m gv.guard) = [gv] →
(f.merge_σ σ grs).eqv f.to_has_eval m gv.value.state :=
begin
intros hn hu,
simp only [factory.merge_σ, state.eqv],
constructor;
apply f.merge_φ_normal _ hn hu;
simp only [true_or, or_true, eq_self_iff_true],
end
lemma factory.merge_ρ_normal_eqv {σ : state SymB} {grs : choices SymB (result SymB SymV)} {gv : choice SymB (result SymB SymV)} :
σ.normal f.to_has_eval m →
grs.filter (λ gv, f.evalB m gv.guard) = [gv] →
(f.merge_ρ σ grs).state.eqv f.to_has_eval m gv.value.state :=
begin
intros hn hu,
generalize hr : (f.merge_ρ σ grs) = r,
simp only [factory.merge_ρ] at hr,
rcases (f.merge_σ_normal hn hu) with hσ,
split_ifs at hr,
{ rewrite list.all_iff_forall at h,
specialize h gv,
have hgv : gv ∈ [gv] := by { simp only [list.mem_singleton], },
rewrite [←hu, list.mem_filter] at hgv,
simp only [hgv, forall_true_left] at h,
repeat { rewrite ←hr, },
simp only [result.state, hσ, result.value, true_and], },
{ simp only [factory.halt_or_ans] at hr,
split_ifs at hr;
repeat { rewrite ←hr, };
simp only [result.state, hσ, true_and], }
end
lemma factory.merge_ρ_normal_eval {σ : state SymB} {grs : choices SymB (result SymB SymV)} {gr : choice SymB (result SymB SymV)} :
σ.normal f.to_has_eval m →
grs.filter (λ gv, f.evalB m gv.guard) = [gr] →
(gr.value.legal f.to_has_eval m) →
(f.merge_ρ σ grs).eval f.to_has_eval m = gr.value.eval f.to_has_eval m :=
begin
intros hn hu hl,
generalize hr : (f.merge_ρ σ grs) = r,
simp only [factory.merge_ρ] at hr,
rcases (f.merge_σ_normal hn hu) with hσ,
split_ifs at hr,
{ rewrite list.all_iff_forall at h,
specialize h gr,
have hgr : gr ∈ [gr] := by { simp only [list.mem_singleton], },
rewrite [←hu, list.mem_filter] at hgr,
simp only [hgr, forall_true_left] at h,
repeat { rewrite ←hr, },
cases gr.value with σ' v' σ',
{ simp only [result.is_halt, to_bool_false_eq_ff, coe_sort_ff] at h,
contradiction, },
{ simp only [result.eval],
simp only [result.state] at hσ,
apply state.eqv_aborted f.to_has_eval hσ, } },
{ simp only [factory.halt_or_ans] at hr,
split_ifs at hr;
repeat { rewrite ←hr, },
{ cases gr.value with σ' v' σ',
{ simp only [result.state] at hσ,
simp only [factory.halted, bor_coe_iff] at h_1,
cases h_1,
all_goals {
simp only [f.is_ff_sound] at h_1,
apply_fun (f.evalB m) at h_1,
simp only [f.mk_ff_sound] at h_1,
have hnσ : ¬ ((f.merge_σ σ grs).normal f.to_has_eval m) := by { simp only [state.normal, h_1, to_bool_false_eq_ff, not_false_iff, coe_sort_ff, false_and, and_false], },
rcases (state.eqv_abnormal f.to_has_eval hnσ hσ) with hnσ',
simp only [result.eval, hnσ', if_false],
apply state.eqv_aborted f.to_has_eval hσ, } },
{ simp only [result.eval],
apply state.eqv_aborted f.to_has_eval hσ, } },
{ rcases (f.merge_υ_normal hu) with hv,
cases gr.value with σ' v' σ',
{ simp only [result.eval],
cases hnσ : (state.normal f.to_has_eval m (f.merge_σ σ grs)),
{ rewrite ←bool_iff_false at hnσ,
rcases (state.eqv_abnormal f.to_has_eval hnσ hσ) with hnσ',
simp only [result.state] at hnσ',
simp only [hnσ', if_false, coe_sort_ff],
apply state.eqv_aborted f.to_has_eval hσ, },
{ rewrite bool.tt_eq_true at hnσ,
rcases (state.eqv_normal f.to_has_eval hnσ hσ) with hnσ',
simp only [result.state] at hnσ',
simp only [hnσ', if_true, coe_sort_tt],
simp only [result.value] at hv,
exact hv, } },
{ simp only [result.legal, eq_ff_eq_not_eq_tt, bool.of_to_bool_iff] at hl,
cases hl with hl hnσ',
rewrite ←bool_iff_false at hnσ',
simp only [result.state] at hσ,
rcases (state.eqv_abnormal f.to_has_eval hnσ' (state.eqv.symm f.to_has_eval m hσ)) with hnσ,
simp only [result.eval, hnσ, if_false],
apply state.eqv_aborted f.to_has_eval hσ, } } }
end
end sym |
6ec11b1d2d510a11e0265a7f2b45fdd4bc5f7565 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /archive/imo/imo2019_q4.lean | db5eb427c78caddc45b94673f6a4e6b5bfd1a3e8 | [
"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 | 4,183 | lean | /-
Copyright (c) 2020 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import tactic.interval_cases
import algebra.big_operators.order
import algebra.big_operators.enat
import data.nat.multiplicity
/-!
# IMO 2019 Q4
Find all pairs `(k, n)` of positive integers such that
```
k! = (2 ^ n − 1)(2 ^ n − 2)(2 ^ n − 4)···(2 ^ n − 2 ^ (n − 1))
```
We show in this file that this property holds iff `(k, n) = (1, 1) ∨ (k, n) = (3, 2)`.
Proof sketch:
The idea of the proof is to count the factors of 2 on both sides. The LHS has less than `k` factors
of 2, and the RHS has exactly `n * (n - 1) / 2` factors of 2. So we know that `n * (n - 1) / 2 < k`.
Now for `n ≥ 6` we have `RHS < 2 ^ (n ^ 2) < (n(n-1)/2)! < k!`. We then treat the cases `n < 6`
individually.
-/
open_locale nat big_operators
open finset multiplicity nat (hiding zero_le prime)
theorem imo2019_q4_upper_bound {k n : ℕ} (hk : k > 0)
(h : (k! : ℤ) = ∏ i in range n, (2 ^ n - 2 ^ i)) : n < 6 :=
begin
have prime_2 : prime (2 : ℤ),
{ exact prime_iff_prime_int.mp prime_two },
have h2 : n * (n - 1) / 2 < k,
{ suffices : multiplicity 2 (k! : ℤ) = (n * (n - 1) / 2 : ℕ),
{ rw [← enat.coe_lt_coe, ← this], change multiplicity ((2 : ℕ) : ℤ) _ < _,
simp_rw [int.coe_nat_multiplicity, multiplicity_two_factorial_lt hk.lt.ne.symm] },
rw [h, multiplicity.finset.prod prime_2, ← sum_range_id, ← sum_nat_coe_enat],
apply sum_congr rfl, intros i hi,
rw [multiplicity_sub_of_gt, multiplicity_pow_self_of_prime prime_2],
rwa [multiplicity_pow_self_of_prime prime_2, multiplicity_pow_self_of_prime prime_2,
enat.coe_lt_coe, ←mem_range] },
rw [←not_le], intro hn,
apply ne_of_lt _ h.symm,
suffices : (∏ i in range n, 2 ^ n : ℤ) < ↑k!,
{ apply lt_of_le_of_lt _ this, apply prod_le_prod,
{ intros, rw [sub_nonneg], apply pow_le_pow, norm_num, apply le_of_lt, rwa [← mem_range] },
{ intros, apply sub_le_self, apply pow_nonneg, norm_num } },
suffices : 2 ^ (n * n) < (n * (n - 1) / 2)!,
{ rw [prod_const, card_range, ← pow_mul], rw [← int.coe_nat_lt_coe_nat_iff] at this,
convert this.trans _, norm_cast, rwa [int.coe_nat_lt_coe_nat_iff, factorial_lt],
refine nat.div_pos _ (by norm_num),
refine le_trans _ (mul_le_mul hn (pred_le_pred hn) (zero_le _) (zero_le _)),
norm_num },
refine le_induction _ _ n hn, { norm_num },
intros n' hn' ih,
have h5 : 1 ≤ 2 * n', { linarith },
have : 2 ^ (2 + 2) ≤ (n' * (n' - 1) / 2).succ,
{ change succ (6 * (6 - 1) / 2) ≤ _,
apply succ_le_succ, apply nat.div_le_div_right,
exact mul_le_mul hn' (pred_le_pred hn') (zero_le _) (zero_le _) },
rw [triangle_succ], apply lt_of_lt_of_le _ factorial_mul_pow_le_factorial,
refine lt_of_le_of_lt _ (mul_lt_mul ih (nat.pow_le_pow_of_le_left this _)
(pow_pos (by norm_num) _) (zero_le _)),
rw [← pow_mul, ← pow_add], apply pow_le_pow_of_le_right, norm_num,
rw [add_mul 2 2],
convert add_le_add_left (add_le_add_left h5 (2 * n')) (n' * n') using 1, ring
end
theorem imo2019_q4 {k n : ℕ} (hk : k > 0) (hn : n > 0) :
(k! : ℤ) = (∏ i in range n, (2 ^ n - 2 ^ i)) ↔ (k, n) = (1, 1) ∨ (k, n) = (3, 2) :=
begin
/- The implication `←` holds. -/
split, swap,
{ rintro (h|h); simp [prod.ext_iff] at h; rcases h with ⟨rfl, rfl⟩;
norm_num [prod_range_succ, succ_mul] },
intro h,
/- We know that n < 6. -/
have := imo2019_q4_upper_bound hk h,
interval_cases n,
/- n = 1 -/
{ left, congr, norm_num at h, norm_cast at h, rw [factorial_eq_one] at h, apply antisymm h,
apply succ_le_of_lt hk },
/- n = 2 -/
{ right, congr, norm_num [prod_range_succ] at h, norm_cast at h, rw [← factorial_inj],
exact h, rw [h], norm_num },
all_goals { exfalso, norm_num [prod_range_succ] at h, norm_cast at h, },
/- n = 3 -/
{ refine monotone_factorial.ne_of_lt_of_lt_nat 5 _ _ _ h; norm_num },
/- n = 4 -/
{ refine monotone_factorial.ne_of_lt_of_lt_nat 7 _ _ _ h; norm_num },
/- n = 5 -/
{ refine monotone_factorial.ne_of_lt_of_lt_nat 10 _ _ _ h; norm_num },
end
|
90b28b8ca7b69beb9bbd10d0a864d81868837d95 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/zmod/parity.lean | c1f3e3d41e7056f6577702e26de7caba4b26fd8b | [] | 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 | 768 | lean | /-
Copyright (c) 2020 Kyle Miller. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Kyle Miller
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.nat.parity
import Mathlib.data.zmod.basic
import Mathlib.PostPort
namespace Mathlib
/-!
# Relating parity to natural numbers mod 2
This module provides lemmas relating `zmod 2` to `even` and `odd`.
## Tags
parity, zmod, even, odd
-/
namespace zmod
theorem eq_zero_iff_even {n : ℕ} : ↑n = 0 ↔ even n :=
iff.trans (char_p.cast_eq_zero_iff (zmod (bit0 1)) (bit0 1) n) (iff.symm even_iff_two_dvd)
theorem eq_one_iff_odd {n : ℕ} : ↑n = 1 ↔ odd n := sorry
theorem ne_zero_iff_odd {n : ℕ} : ↑n ≠ 0 ↔ odd n := sorry
|
469ff8ad6662e0c850a9c72d39b03673f66a38a6 | 5719a16e23dfc08cdea7a5bf035b81690f307965 | /stage0/src/Init/Lean/Parser/Command.lean | 694cba02d7a6c6993db56842fb3e3ee6ade22c1e | [
"Apache-2.0"
] | permissive | postmasters/lean4 | 488b03969a371e1507e1e8a4df9ebf63c7cbe7ac | f3976fc53a883ac7606fc59357d43f4b51016ca7 | refs/heads/master | 1,655,582,707,480 | 1,588,682,595,000 | 1,588,682,595,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,415 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Sebastian Ullrich
-/
prelude
import Init.Lean.Parser.Term
namespace Lean
namespace Parser
@[init] def regBuiltinCommandParserAttr : IO Unit :=
registerBuiltinParserAttribute `builtinCommandParser `command
@[init] def regCommandParserAttribute : IO Unit :=
registerBuiltinDynamicParserAttribute `commandParser `command
@[inline] def commandParser (rbp : Nat := 0) : Parser :=
categoryParser `command rbp
/--
Syntax quotation for terms and (lists of) commands. We prefer terms, so ambiguous quotations like
`($x $y) will be parsed as an application, not two commands. Use `($x:command $y:command) instead.
Multiple command will be put in a `null node, but a single command will not (so that you can directly
match against a quotation in a command kind's elaborator). -/
@[builtinTermParser] def Term.stxQuot := parser! symbol "`(" appPrec >> (termParser <|> many1 commandParser true) >> ")"
namespace Command
def commentBody : Parser :=
{ fn := rawFn (finishCommentBlock 1) true }
def docComment := parser! "/--" >> commentBody
def attrArg : Parser := ident <|> strLit <|> numLit
-- use `rawIdent` because of attribute names such as `instance`
def attrInstance := parser! rawIdent >> many attrArg
def attributes := parser! "@[" >> sepBy1 attrInstance ", " >> "]"
def «private» := parser! "private "
def «protected» := parser! "protected "
def visibility := «private» <|> «protected»
def «noncomputable» := parser! "noncomputable "
def «unsafe» := parser! "unsafe "
def «partial» := parser! "partial "
def declModifiers := parser! optional docComment >> optional «attributes» >> optional visibility >> optional «noncomputable» >> optional «unsafe» >> optional «partial»
def declId := parser! ident >> optional (".{" >> sepBy1 ident ", " >> "}")
def declSig := parser! many Term.bracketedBinder >> Term.typeSpec
def optDeclSig := parser! many Term.bracketedBinder >> Term.optType
def declValSimple := parser! " := " >> termParser
def declValEqns := parser! Term.matchAlts false
def declVal := declValSimple <|> declValEqns
def «abbrev» := parser! "abbrev " >> declId >> optDeclSig >> declVal
def «def» := parser! "def " >> declId >> optDeclSig >> declVal
def «theorem» := parser! "theorem " >> declId >> declSig >> declVal
def «constant» := parser! "constant " >> declId >> declSig >> optional declValSimple
def «instance» := parser! "instance " >> optional declId >> declSig >> declVal
def «axiom» := parser! "axiom " >> declId >> declSig
def «example» := parser! "example " >> declSig >> declVal
def relaxedInferMod := parser! try ("{" >> "}")
def strictInferMod := parser! try ("(" >> ")")
def inferMod := relaxedInferMod <|> strictInferMod
def introRule := parser! " | " >> ident >> optional inferMod >> optDeclSig
def «inductive» := parser! "inductive " >> declId >> optDeclSig >> many introRule
def classInductive := parser! try ("class " >> "inductive ") >> declId >> optDeclSig >> many introRule
def structExplicitBinder := parser! "(" >> many ident >> optional inferMod >> optDeclSig >> optional Term.binderDefault >> ")"
def structImplicitBinder := parser! "{" >> many ident >> optional inferMod >> optDeclSig >> "}"
def structInstBinder := parser! "[" >> many ident >> optional inferMod >> optDeclSig >> "]"
def structFields := parser! many (structExplicitBinder <|> structImplicitBinder <|> structInstBinder)
def structCtor := parser! ident >> optional inferMod >> " :: "
def structureTk := parser! "structure "
def classTk := parser! "class "
def «extends» := parser! " extends " >> sepBy1 termParser ", "
def «structure» := parser! (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> optional «extends» >> Term.optType >> " := " >> optional structCtor >> structFields
@[builtinCommandParser] def declaration := parser!
declModifiers >> («abbrev» <|> «def» <|> «theorem» <|> «constant» <|> «instance» <|> «axiom» <|> «example» <|> «inductive» <|> classInductive <|> «structure»)
@[builtinCommandParser] def «section» := parser! "section " >> optional ident
@[builtinCommandParser] def «namespace» := parser! "namespace " >> ident
@[builtinCommandParser] def «end» := parser! "end " >> optional ident
@[builtinCommandParser] def «variable» := parser! "variable " >> Term.bracketedBinder
@[builtinCommandParser] def «variables» := parser! "variables " >> many1 Term.bracketedBinder
@[builtinCommandParser] def «universe» := parser! "universe " >> ident
@[builtinCommandParser] def «universes» := parser! "universes " >> many1 ident
@[builtinCommandParser] def check := parser! "#check " >> termParser
@[builtinCommandParser] def check_failure := parser! "#check_failure " >> termParser -- Like `#check`, but succeeds only if term does not type check
@[builtinCommandParser] def synth := parser! "#synth " >> termParser
@[builtinCommandParser] def exit := parser! "#exit"
@[builtinCommandParser] def «resolve_name» := parser! "#resolve_name " >> ident
@[builtinCommandParser] def «init_quot» := parser! "init_quot"
@[builtinCommandParser] def «set_option» := parser! "set_option " >> ident >> (nonReservedSymbol "true" <|> nonReservedSymbol "false" <|> strLit <|> numLit)
@[builtinCommandParser] def «attribute» := parser! optional "local " >> "attribute " >> "[" >> sepBy1 attrInstance ", " >> "]" >> many1 ident
@[builtinCommandParser] def «export» := parser! "export " >> ident >> "(" >> many1 ident >> ")"
def openHiding := parser! try (ident >> "hiding") >> many1 ident
def openRenamingItem := parser! ident >> unicodeSymbol "→" "->" >> ident
def openRenaming := parser! try (ident >> "renaming") >> sepBy1 openRenamingItem ", "
def openOnly := parser! try (ident >> "(") >> many1 ident >> ")"
def openSimple := parser! many1 ident
@[builtinCommandParser] def «open» := parser! "open " >> (openHiding <|> openRenaming <|> openOnly <|> openSimple)
end Command
end Parser
end Lean
|
06c156ac8485bbd5f787ddf38168febf41cbe581 | 3863d2564418bccb1859e057bf5a4ef240e75fd7 | /hott/homotopy/connectedness.hlean | 222ef8616b6b0cfb7e53f690d1cbeb26af0aa112 | [
"Apache-2.0"
] | permissive | JacobGross/lean | 118bbb067ff4d4af48a266face2c7eb9868fa91c | eb26087df940c54337cb807b4bc6d345d1fc1085 | refs/heads/master | 1,582,735,011,532 | 1,462,557,826,000 | 1,462,557,826,000 | 46,451,196 | 0 | 0 | null | 1,462,557,826,000 | 1,447,885,161,000 | C++ | UTF-8 | Lean | false | false | 11,416 | hlean | /-
Copyright (c) 2015 Ulrik Buchholtz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Ulrik Buchholtz, Floris van Doorn
-/
import types.trunc types.arrow_2 types.fiber
open eq is_trunc is_equiv nat equiv trunc function fiber funext pi
namespace is_conn
definition is_conn [reducible] (n : ℕ₋₂) (A : Type) : Type :=
is_contr (trunc n A)
definition is_conn_equiv_closed (n : ℕ₋₂) {A B : Type}
: A ≃ B → is_conn n A → is_conn n B :=
begin
intros H C,
fapply @is_contr_equiv_closed (trunc n A) _,
apply trunc_equiv_trunc,
assumption
end
definition is_conn_fun [reducible] (n : ℕ₋₂) {A B : Type} (f : A → B) : Type :=
Πb : B, is_conn n (fiber f b)
theorem is_conn_of_le (A : Type) {n k : ℕ₋₂} (H : n ≤ k) [is_conn k A] : is_conn n A :=
begin
apply is_contr_equiv_closed,
apply trunc_trunc_equiv_left _ H
end
theorem is_conn_fun_of_le {A B : Type} (f : A → B) {n k : ℕ₋₂} (H : n ≤ k)
[is_conn_fun k f] : is_conn_fun n f :=
λb, is_conn_of_le _ H
namespace is_conn_fun
section
parameters (n : ℕ₋₂) {A B : Type} {h : A → B}
(H : is_conn_fun n h) (P : B → Type) [Πb, is_trunc n (P b)]
private definition rec.helper : (Πa : A, P (h a)) → Πb : B, trunc n (fiber h b) → P b :=
λt b, trunc.rec (λx, point_eq x ▸ t (point x))
private definition rec.g : (Πa : A, P (h a)) → (Πb : B, P b) :=
λt b, rec.helper t b (@center (trunc n (fiber h b)) (H b))
-- induction principle for n-connected maps (Lemma 7.5.7)
protected definition rec : is_equiv (λs : Πb : B, P b, λa : A, s (h a)) :=
adjointify (λs a, s (h a)) rec.g
begin
intro t, apply eq_of_homotopy, intro a, unfold rec.g, unfold rec.helper,
rewrite [@center_eq _ (H (h a)) (tr (fiber.mk a idp))],
end
begin
intro k, apply eq_of_homotopy, intro b, unfold rec.g,
generalize (@center _ (H b)), apply trunc.rec, apply fiber.rec,
intros a p, induction p, reflexivity
end
protected definition elim : (Πa : A, P (h a)) → (Πb : B, P b) :=
@is_equiv.inv _ _ (λs a, s (h a)) rec
protected definition elim_β : Πf : (Πa : A, P (h a)), Πa : A, elim f (h a) = f a :=
λf, apd10 (@is_equiv.right_inv _ _ (λs a, s (h a)) rec f)
end
section
parameters (n k : ℕ₋₂) {A B : Type} {f : A → B}
(H : is_conn_fun n f) (P : B → Type) [HP : Πb, is_trunc (n +2+ k) (P b)]
include H HP
-- Lemma 8.6.1
proposition elim_general : is_trunc_fun k (pi_functor_left f P) :=
begin
revert P HP,
induction k with k IH: intro P HP t,
{ apply is_contr_fiber_of_is_equiv, apply is_conn_fun.rec, exact H, exact HP},
{ apply is_trunc_succ_intro,
intros x y, cases x with g p, cases y with h q,
have e : fiber (λr : g ~ h, (λa, r (f a))) (apd10 (p ⬝ q⁻¹))
≃ (fiber.mk g p = fiber.mk h q
:> fiber (λs : (Πb, P b), (λa, s (f a))) t),
begin
apply equiv.trans !fiber.sigma_char,
have e' : Πr : g ~ h,
((λa, r (f a)) = apd10 (p ⬝ q⁻¹))
≃ (ap (λv, (λa, v (f a))) (eq_of_homotopy r) ⬝ q = p),
begin
intro r,
refine equiv.trans _ (eq_con_inv_equiv_con_eq q p
(ap (λv a, v (f a)) (eq_of_homotopy r))),
rewrite [-(ap (λv a, v (f a)) (apd10_eq_of_homotopy r))],
rewrite [-(apd10_ap_precompose_dependent f (eq_of_homotopy r))],
apply equiv.symm,
apply eq_equiv_fn_eq (@apd10 A (λa, P (f a)) (λa, g (f a)) (λa, h (f a)))
end,
apply equiv.trans (sigma.sigma_equiv_sigma_right e'), clear e',
apply equiv.trans (equiv.symm (sigma.sigma_equiv_sigma_left
eq_equiv_homotopy)),
apply equiv.symm, apply equiv.trans !fiber_eq_equiv,
apply sigma.sigma_equiv_sigma_right, intro r,
apply eq_equiv_eq_symm
end,
apply @is_trunc_equiv_closed _ _ k e, clear e,
apply IH (λb : B, (g b = h b)) (λb, @is_trunc_eq (P b) (n +2+ k) (HP b) (g b) (h b))}
end
end
section
universe variables u v
parameters (n : ℕ₋₂) {A : Type.{u}} {B : Type.{v}} {h : A → B}
parameter sec : ΠP : B → trunctype.{max u v} n,
is_retraction (λs : (Πb : B, P b), λ a, s (h a))
private definition s := sec (λb, trunctype.mk' n (trunc n (fiber h b)))
include sec
-- the other half of Lemma 7.5.7
definition intro : is_conn_fun n h :=
begin
intro b,
apply is_contr.mk (@is_retraction.sect _ _ _ s (λa, tr (fiber.mk a idp)) b),
esimp, apply trunc.rec, apply fiber.rec, intros a p,
apply transport
(λz : (Σy, h a = y), @sect _ _ _ s (λa, tr (mk a idp)) (sigma.pr1 z) =
tr (fiber.mk a (sigma.pr2 z)))
(@center_eq _ (is_contr_sigma_eq (h a)) (sigma.mk b p)),
exact apd10 (@right_inverse _ _ _ s (λa, tr (fiber.mk a idp))) a
end
end
end is_conn_fun
-- Connectedness is related to maps to and from the unit type, first to
section
parameters (n : ℕ₋₂) (A : Type)
definition is_conn_of_map_to_unit
: is_conn_fun n (const A unit.star) → is_conn n A :=
begin
intro H, unfold is_conn_fun at H,
rewrite [-(ua (fiber.fiber_star_equiv A))],
exact (H unit.star)
end
-- now maps from unit
definition is_conn_of_map_from_unit (a₀ : A) (H : is_conn_fun n (const unit a₀))
: is_conn n .+1 A :=
is_contr.mk (tr a₀)
begin
apply trunc.rec, intro a,
exact trunc.elim (λz : fiber (const unit a₀) a, ap tr (point_eq z))
(@center _ (H a))
end
definition is_conn_fun_from_unit (a₀ : A) [H : is_conn n .+1 A]
: is_conn_fun n (const unit a₀) :=
begin
intro a,
apply is_conn_equiv_closed n (equiv.symm (fiber_const_equiv A a₀ a)),
apply @is_contr_equiv_closed _ _ (tr_eq_tr_equiv n a₀ a),
end
end
-- as special case we get elimination principles for pointed connected types
namespace is_conn
open pointed unit
section
parameters (n : ℕ₋₂) {A : Type*}
[H : is_conn n .+1 A] (P : A → Type) [Πa, is_trunc n (P a)]
include H
protected definition rec : is_equiv (λs : Πa : A, P a, s (Point A)) :=
@is_equiv_compose
(Πa : A, P a) (unit → P (Point A)) (P (Point A))
(λs x, s (Point A)) (λf, f unit.star)
(is_conn_fun.rec n (is_conn_fun_from_unit n A (Point A)) P)
(to_is_equiv (arrow_unit_left (P (Point A))))
protected definition elim : P (Point A) → (Πa : A, P a) :=
@is_equiv.inv _ _ (λs, s (Point A)) rec
protected definition elim_β (p : P (Point A)) : elim p (Point A) = p :=
@is_equiv.right_inv _ _ (λs, s (Point A)) rec p
end
section
parameters (n k : ℕ₋₂) {A : Type*}
[H : is_conn n .+1 A] (P : A → Type) [Πa, is_trunc (n +2+ k) (P a)]
include H
proposition elim_general (p : P (Point A))
: is_trunc k (fiber (λs : (Πa : A, P a), s (Point A)) p) :=
@is_trunc_equiv_closed
(fiber (λs x, s (Point A)) (λx, p))
(fiber (λs, s (Point A)) p)
k
(equiv.symm (fiber.equiv_postcompose (to_fun (arrow_unit_left (P (Point A))))))
(is_conn_fun.elim_general n k (is_conn_fun_from_unit n A (Point A)) P (λx, p))
end
end is_conn
-- Lemma 7.5.2
definition minus_one_conn_of_surjective {A B : Type} (f : A → B)
: is_surjective f → is_conn_fun -1 f :=
begin
intro H, intro b,
exact @is_contr_of_inhabited_prop (∥fiber f b∥) (is_trunc_trunc -1 (fiber f b)) (H b),
end
definition is_surjection_of_minus_one_conn {A B : Type} (f : A → B)
: is_conn_fun -1 f → is_surjective f :=
begin
intro H, intro b,
exact @center (∥fiber f b∥) (H b),
end
definition merely_of_minus_one_conn {A : Type} : is_conn -1 A → ∥A∥ :=
λH, @center (∥A∥) H
definition minus_one_conn_of_merely {A : Type} : ∥A∥ → is_conn -1 A :=
@is_contr_of_inhabited_prop (∥A∥) (is_trunc_trunc -1 A)
section
open arrow
variables {f g : arrow}
-- Lemma 7.5.4
definition retract_of_conn_is_conn [instance] (r : arrow_hom f g) [H : is_retraction r]
(n : ℕ₋₂) [K : is_conn_fun n f] : is_conn_fun n g :=
begin
intro b, unfold is_conn,
apply is_contr_retract (trunc_functor n (retraction_on_fiber r b)),
exact K (on_cod (arrow.is_retraction.sect r) b)
end
end
-- Corollary 7.5.5
definition is_conn_homotopy (n : ℕ₋₂) {A B : Type} {f g : A → B}
(p : f ~ g) (H : is_conn_fun n f) : is_conn_fun n g :=
@retract_of_conn_is_conn _ _
(arrow.arrow_hom_of_homotopy p) (arrow.is_retraction_arrow_hom_of_homotopy p) n H
-- all types are -2-connected
definition is_conn_minus_two (A : Type) : is_conn -2 A :=
_
-- the following trivial cases are solved by type class inference
definition is_conn_of_is_contr (k : ℕ₋₂) (A : Type) [is_contr A] : is_conn k A := _
definition is_conn_fun_of_is_equiv (k : ℕ₋₂) {A B : Type} (f : A → B) [is_equiv f] :
is_conn_fun k f :=
_
-- Lemma 7.5.14
theorem is_equiv_trunc_functor_of_is_conn_fun [instance] {A B : Type} (n : ℕ₋₂) (f : A → B)
[H : is_conn_fun n f] : is_equiv (trunc_functor n f) :=
begin
fapply adjointify,
{ intro b, induction b with b, exact trunc_functor n point (center (trunc n (fiber f b)))},
{ intro b, induction b with b, esimp, generalize center (trunc n (fiber f b)), intro v,
induction v with v, induction v with a p, esimp, exact ap tr p},
{ intro a, induction a with a, esimp, rewrite [center_eq (tr (fiber.mk a idp))]}
end
theorem trunc_equiv_trunc_of_is_conn_fun {A B : Type} (n : ℕ₋₂) (f : A → B)
[H : is_conn_fun n f] : trunc n A ≃ trunc n B :=
equiv.mk (trunc_functor n f) (is_equiv_trunc_functor_of_is_conn_fun n f)
definition is_conn_fun_trunc_functor_of_le {n k : ℕ₋₂} {A B : Type} (f : A → B) (H : k ≤ n)
[H2 : is_conn_fun k f] : is_conn_fun k (trunc_functor n f) :=
begin
apply is_conn_fun.intro,
intro P, have Πb, is_trunc n (P b), from (λb, is_trunc_of_le _ H),
fconstructor,
{ intro f' b,
induction b with b,
refine is_conn_fun.elim k H2 _ _ b, intro a, exact f' (tr a)},
{ intro f', apply eq_of_homotopy, intro a,
induction a with a, esimp, rewrite [is_conn_fun.elim_β]}
end
definition is_conn_fun_trunc_functor_of_ge {n k : ℕ₋₂} {A B : Type} (f : A → B) (H : n ≤ k)
[H2 : is_conn_fun k f] : is_conn_fun k (trunc_functor n f) :=
begin
apply is_conn_fun_of_is_equiv,
apply is_equiv_trunc_functor_of_le f H
end
-- Exercise 7.18
definition is_conn_fun_trunc_functor {n k : ℕ₋₂} {A B : Type} (f : A → B)
[H2 : is_conn_fun k f] : is_conn_fun k (trunc_functor n f) :=
begin
eapply algebra.le_by_cases k n: intro H,
{ exact is_conn_fun_trunc_functor_of_le f H},
{ exact is_conn_fun_trunc_functor_of_ge f H}
end
end is_conn
|
dd8af25802bf2a1d65033c2ab154b2dc0ea51d6d | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /tests/lean/run/class_bug1.lean | 4f1755ab3dbab1e1c5d9ec99ae622add862323a8 | [
"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 | 962 | lean | import logic
inductive category (ob : Type) (mor : ob → ob → Type) : Type :=
mk : Π (comp : Π⦃A B C : ob⦄, mor B C → mor A B → mor A C)
(id : Π {A : ob}, mor A A),
(Π {A B C D : ob} {f : mor A B} {g : mor B C} {h : mor C D},
comp h (comp g f) = comp (comp h g) f) →
(Π {A B : ob} {f : mor A B}, comp f id = f) →
(Π {A B : ob} {f : mor A B}, comp id f = f) →
category ob mor
attribute category [class]
namespace category
context sec_cat
parameter A : Type
inductive foo :=
mk : A → foo
attribute foo [class]
parameters {ob : Type} {mor : ob → ob → Type} {Cat : category ob mor}
definition compose := rec (λ comp id assoc idr idl, comp) Cat
definition id := rec (λ comp id assoc idr idl, id) Cat
infixr ∘ := compose
inductive is_section {A B : ob} (f : mor A B) : Type :=
mk : ∀g, g ∘ f = id → is_section f
end sec_cat
end category
|
824da96ad2adcda746336623077bb477b33ed260 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/category_theory/isomorphism.lean | 988691650f79b2c81cff2ef4659ea12190c44809 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 10,502 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn
-/
import category_theory.functor
import tactic.reassoc_axiom
/-!
# Isomorphisms
This file defines isomorphisms between objects of a category.
## Main definitions
- `structure iso` : a bundled isomorphism between two objects of a category;
- `class is_iso` : an unbundled version of `iso`; note that `is_iso f` is usually *not* a `Prop`,
because it holds the inverse morphism;
- `as_iso` : convert from `is_iso` to `iso`;
- `of_iso` : convert from `iso` to `is_iso`;
- standard operations on isomorphisms (composition, inverse etc)
## Notations
- `X ≅ Y` : same as `iso X Y`;
- `α ≪≫ β` : composition of two isomorphisms; it is called `iso.trans`
## Tags
category, category theory, isomorphism
-/
universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation
namespace category_theory
open category
structure iso {C : Type u} [category.{v} C] (X Y : C) :=
(hom : X ⟶ Y)
(inv : Y ⟶ X)
(hom_inv_id' : hom ≫ inv = 𝟙 X . obviously)
(inv_hom_id' : inv ≫ hom = 𝟙 Y . obviously)
restate_axiom iso.hom_inv_id'
restate_axiom iso.inv_hom_id'
attribute [simp, reassoc] iso.hom_inv_id iso.inv_hom_id
infixr ` ≅ `:10 := iso -- type as \cong or \iso
variables {C : Type u} [𝒞 : category.{v} C]
include 𝒞
variables {X Y Z : C}
namespace iso
@[ext] lemma ext ⦃α β : X ≅ Y⦄ (w : α.hom = β.hom) : α = β :=
suffices α.inv = β.inv, by cases α; cases β; cc,
calc α.inv
= α.inv ≫ (β.hom ≫ β.inv) : by rw [iso.hom_inv_id, category.comp_id]
... = (α.inv ≫ α.hom) ≫ β.inv : by rw [category.assoc, ←w]
... = β.inv : by rw [iso.inv_hom_id, category.id_comp]
@[symm] def symm (I : X ≅ Y) : Y ≅ X :=
{ hom := I.inv,
inv := I.hom,
hom_inv_id' := I.inv_hom_id',
inv_hom_id' := I.hom_inv_id' }
@[simp] lemma symm_hom (α : X ≅ Y) : α.symm.hom = α.inv := rfl
@[simp] lemma symm_inv (α : X ≅ Y) : α.symm.inv = α.hom := rfl
@[simp] lemma symm_mk {X Y : C} (hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id) (inv_hom_id) :
iso.symm {hom := hom, inv := inv, hom_inv_id' := hom_inv_id, inv_hom_id' := inv_hom_id} =
{hom := inv, inv := hom, hom_inv_id' := inv_hom_id, inv_hom_id' := hom_inv_id} := rfl
@[simp] lemma symm_symm_eq {X Y : C} (α : X ≅ Y) : α.symm.symm = α :=
by cases α; refl
@[simp] lemma symm_eq_iff {X Y : C} {α β : X ≅ Y} : α.symm = β.symm ↔ α = β :=
⟨λ h, symm_symm_eq α ▸ symm_symm_eq β ▸ congr_arg symm h, congr_arg symm⟩
@[refl] def refl (X : C) : X ≅ X :=
{ hom := 𝟙 X,
inv := 𝟙 X }
@[simp] lemma refl_hom (X : C) : (iso.refl X).hom = 𝟙 X := rfl
@[simp] lemma refl_inv (X : C) : (iso.refl X).inv = 𝟙 X := rfl
@[simp] lemma refl_symm (X : C) : (iso.refl X).symm = iso.refl X := rfl
@[trans] def trans (α : X ≅ Y) (β : Y ≅ Z) : X ≅ Z :=
{ hom := α.hom ≫ β.hom,
inv := β.inv ≫ α.inv }
infixr ` ≪≫ `:80 := iso.trans -- type as `\ll \gg`.
@[simp] lemma trans_hom (α : X ≅ Y) (β : Y ≅ Z) : (α ≪≫ β).hom = α.hom ≫ β.hom := rfl
@[simp] lemma trans_inv (α : X ≅ Y) (β : Y ≅ Z) : (α ≪≫ β).inv = β.inv ≫ α.inv := rfl
@[simp] lemma trans_mk {X Y Z : C}
(hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id) (inv_hom_id)
(hom' : Y ⟶ Z) (inv' : Z ⟶ Y) (hom_inv_id') (inv_hom_id') (hom_inv_id'') (inv_hom_id'') :
iso.trans
{hom := hom, inv := inv, hom_inv_id' := hom_inv_id, inv_hom_id' := inv_hom_id}
{hom := hom', inv := inv', hom_inv_id' := hom_inv_id', inv_hom_id' := inv_hom_id'} =
{hom := hom ≫ hom', inv := inv' ≫ inv, hom_inv_id' := hom_inv_id'', inv_hom_id' := inv_hom_id''} :=
rfl
@[simp] lemma trans_symm (α : X ≅ Y) (β : Y ≅ Z) : (α ≪≫ β).symm = β.symm ≪≫ α.symm := rfl
@[simp] lemma trans_assoc {Z' : C} (α : X ≅ Y) (β : Y ≅ Z) (γ : Z ≅ Z') :
(α ≪≫ β) ≪≫ γ = α ≪≫ β ≪≫ γ :=
by ext; simp only [trans_hom, category.assoc]
@[simp] lemma refl_trans (α : X ≅ Y) : (iso.refl X) ≪≫ α = α := by ext; apply category.id_comp
@[simp] lemma trans_refl (α : X ≅ Y) : α ≪≫ (iso.refl Y) = α := by ext; apply category.comp_id
@[simp] lemma symm_self_id (α : X ≅ Y) : α.symm ≪≫ α = iso.refl Y := ext α.inv_hom_id
@[simp] lemma self_symm_id (α : X ≅ Y) : α ≪≫ α.symm = iso.refl X := ext α.hom_inv_id
@[simp] lemma symm_self_id_assoc (α : X ≅ Y) (β : Y ≅ Z) : α.symm ≪≫ α ≪≫ β = β :=
by rw [← trans_assoc, symm_self_id, refl_trans]
@[simp] lemma self_symm_id_assoc (α : X ≅ Y) (β : X ≅ Z) : α ≪≫ α.symm ≪≫ β = β :=
by rw [← trans_assoc, self_symm_id, refl_trans]
lemma inv_comp_eq (α : X ≅ Y) {f : X ⟶ Z} {g : Y ⟶ Z} : α.inv ≫ f = g ↔ f = α.hom ≫ g :=
⟨λ H, by simp [H.symm], λ H, by simp [H]⟩
lemma eq_inv_comp (α : X ≅ Y) {f : X ⟶ Z} {g : Y ⟶ Z} : g = α.inv ≫ f ↔ α.hom ≫ g = f :=
(inv_comp_eq α.symm).symm
lemma comp_inv_eq (α : X ≅ Y) {f : Z ⟶ Y} {g : Z ⟶ X} : f ≫ α.inv = g ↔ f = g ≫ α.hom :=
⟨λ H, by simp [H.symm], λ H, by simp [H]⟩
lemma eq_comp_inv (α : X ≅ Y) {f : Z ⟶ Y} {g : Z ⟶ X} : g = f ≫ α.inv ↔ g ≫ α.hom = f :=
(comp_inv_eq α.symm).symm
lemma inv_eq_inv (f g : X ≅ Y) : f.inv = g.inv ↔ f.hom = g.hom :=
have ∀{X Y : C} (f g : X ≅ Y), f.hom = g.hom → f.inv = g.inv, from λ X Y f g h, by rw [ext h],
⟨this f.symm g.symm, this f g⟩
lemma hom_comp_eq_id (α : X ≅ Y) {f : Y ⟶ X} : α.hom ≫ f = 𝟙 X ↔ f = α.inv :=
by rw [←eq_inv_comp, comp_id]
lemma comp_hom_eq_id (α : X ≅ Y) {f : Y ⟶ X} : f ≫ α.hom = 𝟙 Y ↔ f = α.inv :=
by rw [←eq_comp_inv, id_comp]
lemma hom_eq_inv (α : X ≅ Y) (β : Y ≅ X) : α.hom = β.inv ↔ β.hom = α.inv :=
by { erw [inv_eq_inv α.symm β, eq_comm], refl }
end iso
/-- `is_iso` typeclass expressing that a morphism is invertible.
This contains the data of the inverse, but is a subsingleton type. -/
class is_iso (f : X ⟶ Y) :=
(inv : Y ⟶ X)
(hom_inv_id' : f ≫ inv = 𝟙 X . obviously)
(inv_hom_id' : inv ≫ f = 𝟙 Y . obviously)
export is_iso (inv)
def as_iso (f : X ⟶ Y) [h : is_iso f] : X ≅ Y := { hom := f, ..h }
@[simp] lemma as_iso_hom (f : X ⟶ Y) [is_iso f] : (as_iso f).hom = f := rfl
@[simp] lemma as_iso_inv (f : X ⟶ Y) [is_iso f] : (as_iso f).inv = inv f := rfl
namespace is_iso
@[simp] lemma hom_inv_id (f : X ⟶ Y) [is_iso f] : f ≫ inv f = 𝟙 X :=
is_iso.hom_inv_id' f
@[simp] lemma inv_hom_id (f : X ⟶ Y) [is_iso f] : inv f ≫ f = 𝟙 Y :=
is_iso.inv_hom_id' f
@[simp] lemma hom_inv_id_assoc {Z} (f : X ⟶ Y) [is_iso f] (g : X ⟶ Z) :
f ≫ inv f ≫ g = g :=
(as_iso f).hom_inv_id_assoc g
@[simp] lemma inv_hom_id_assoc {Z} (f : X ⟶ Y) [is_iso f] (g : Y ⟶ Z) :
inv f ≫ f ≫ g = g :=
(as_iso f).inv_hom_id_assoc g
instance (X : C) : is_iso (𝟙 X) :=
{ inv := 𝟙 X }
instance of_iso (f : X ≅ Y) : is_iso f.hom :=
{ .. f }
instance of_iso_inverse (f : X ≅ Y) : is_iso f.inv :=
is_iso.of_iso f.symm
variables {f g : X ⟶ Y} {h : Y ⟶ Z}
instance inv_is_iso [is_iso f] : is_iso (inv f) :=
is_iso.of_iso_inverse (as_iso f)
instance comp_is_iso [is_iso f] [is_iso h] : is_iso (f ≫ h) :=
is_iso.of_iso $ (as_iso f) ≪≫ (as_iso h)
@[simp] lemma inv_id : inv (𝟙 X) = 𝟙 X := rfl
@[simp] lemma inv_comp [is_iso f] [is_iso h] : inv (f ≫ h) = inv h ≫ inv f := rfl
@[simp] lemma inv_inv [is_iso f] : inv (inv f) = f := rfl
@[simp] lemma iso.inv_inv (f : X ≅ Y) : inv (f.inv) = f.hom := rfl
@[simp] lemma iso.inv_hom (f : X ≅ Y) : inv (f.hom) = f.inv := rfl
@[priority 100] -- see Note [lower instance priority]
instance epi_of_iso (f : X ⟶ Y) [is_iso f] : epi f :=
{ left_cancellation := λ Z g h w,
-- This is an interesting test case for better rewrite automation.
by rw [← is_iso.inv_hom_id_assoc f g, w, is_iso.inv_hom_id_assoc f h] }
@[priority 100] -- see Note [lower instance priority]
instance mono_of_iso (f : X ⟶ Y) [is_iso f] : mono f :=
{ right_cancellation := λ Z g h w,
by rw [←category.comp_id C g, ←category.comp_id C h, ←is_iso.hom_inv_id f, ←category.assoc, w, ←category.assoc] }
end is_iso
open is_iso
lemma eq_of_inv_eq_inv {f g : X ⟶ Y} [is_iso f] [is_iso g] (p : inv f = inv g) : f = g :=
begin
apply (cancel_epi (inv f)).1,
erw [inv_hom_id, p, inv_hom_id],
end
instance (f : X ⟶ Y) : subsingleton (is_iso f) :=
⟨λ a b,
suffices a.inv = b.inv, by cases a; cases b; congr; exact this,
show (@as_iso C _ _ _ f a).inv = (@as_iso C _ _ _ f b).inv,
by congr' 1; ext; refl⟩
lemma is_iso.inv_eq_inv {f g : X ⟶ Y} [is_iso f] [is_iso g] : inv f = inv g ↔ f = g :=
iso.inv_eq_inv (as_iso f) (as_iso g)
namespace functor
universes u₁ v₁ u₂ v₂
variables {D : Type u₂}
variables [𝒟 : category.{v₂} D]
include 𝒟
def map_iso (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : F.obj X ≅ F.obj Y :=
{ hom := F.map i.hom,
inv := F.map i.inv,
hom_inv_id' := by rw [←map_comp, iso.hom_inv_id, ←map_id],
inv_hom_id' := by rw [←map_comp, iso.inv_hom_id, ←map_id] }
@[simp] lemma map_iso_hom (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : (F.map_iso i).hom = F.map i.hom := rfl
@[simp] lemma map_iso_inv (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : (F.map_iso i).inv = F.map i.inv := rfl
@[simp] lemma map_iso_symm (F : C ⥤ D) {X Y : C} (i : X ≅ Y) :
F.map_iso i.symm = (F.map_iso i).symm :=
rfl
@[simp] lemma map_iso_trans (F : C ⥤ D) {X Y Z : C} (i : X ≅ Y) (j : Y ≅ Z) :
F.map_iso (i ≪≫ j) = (F.map_iso i) ≪≫ (F.map_iso j) :=
by ext; apply functor.map_comp
@[simp] lemma map_iso_refl (F : C ⥤ D) (X : C) : F.map_iso (iso.refl X) = iso.refl (F.obj X) :=
iso.ext $ F.map_id X
instance map_is_iso (F : C ⥤ D) (f : X ⟶ Y) [is_iso f] : is_iso (F.map f) :=
is_iso.of_iso $ F.map_iso (as_iso f)
@[simp] lemma map_inv (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] :
F.map (inv f) = inv (F.map f) :=
rfl
@[simp] lemma map_hom_inv (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] :
F.map f ≫ F.map (inv f) = 𝟙 (F.obj X) :=
by rw [map_inv, is_iso.hom_inv_id]
@[simp] lemma map_inv_hom (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] :
F.map (inv f) ≫ F.map f = 𝟙 (F.obj Y) :=
by rw [map_inv, is_iso.inv_hom_id]
end functor
end category_theory
|
19fb3bcada646c2e5d2c7135889ed8e07b0721fe | 969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb | /src/ring_theory/fractional_ideal.lean | 85a596412bca142b9e8e1543d768e54bfc5edda8 | [
"Apache-2.0"
] | permissive | SAAluthwela/mathlib | 62044349d72dd63983a8500214736aa7779634d3 | 83a4b8b990907291421de54a78988c024dc8a552 | refs/heads/master | 1,679,433,873,417 | 1,615,998,031,000 | 1,615,998,031,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 39,596 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen, Filippo A. E. Nuccio
-/
import ring_theory.localization
import ring_theory.noetherian
import ring_theory.principal_ideal_domain
import tactic.field_simp
/-!
# Fractional ideals
This file defines fractional ideals of an integral domain and proves basic facts about them.
## Main definitions
Let `S` be a submonoid of an integral domain `R`, `P` the localization of `R` at `S`, and `f` the
natural ring hom from `R` to `P`.
* `is_fractional` defines which `R`-submodules of `P` are fractional ideals
* `fractional_ideal f` is the type of fractional ideals in `P`
* `has_coe (ideal R) (fractional_ideal f)` instance
* `comm_semiring (fractional_ideal f)` instance:
the typical ideal operations generalized to fractional ideals
* `lattice (fractional_ideal f)` instance
* `map` is the pushforward of a fractional ideal along an algebra morphism
Let `K` be the localization of `R` at `R \ {0}` and `g` the natural ring hom from `R` to `K`.
* `has_div (fractional_ideal g)` instance:
the ideal quotient `I / J` (typically written $I : J$, but a `:` operator cannot be defined)
## Main statements
* `mul_left_mono` and `mul_right_mono` state that ideal multiplication is monotone
* `prod_one_self_div_eq` states that `1 / I` is the inverse of `I` if one exists
* `is_noetherian` states that very fractional ideal of a noetherian integral domain is noetherian
## Implementation notes
Fractional ideals are considered equal when they contain the same elements,
independent of the denominator `a : R` such that `a I ⊆ R`.
Thus, we define `fractional_ideal` to be the subtype of the predicate `is_fractional`,
instead of having `fractional_ideal` be a structure of which `a` is a field.
Most definitions in this file specialize operations from submodules to fractional ideals,
proving that the result of this operation is fractional if the input is fractional.
Exceptions to this rule are defining `(+) := (⊔)` and `⊥ := 0`,
in order to re-use their respective proof terms.
We can still use `simp` to show `I.1 + J.1 = (I + J).1` and `⊥.1 = 0.1`.
In `ring_theory.localization`, we define a copy of the localization map `f`'s codomain `P`
(`f.codomain`) so that the `R`-algebra instance on `P` can 'know' the map needed to induce
the `R`-algebra structure.
We don't assume that the localization is a field until we need it to define ideal quotients.
When this assumption is needed, we replace `S` with `non_zero_divisors R`, making the localization
a field.
## References
* https://en.wikipedia.org/wiki/Fractional_ideal
## Tags
fractional ideal, fractional ideals, invertible ideal
-/
open localization_map
namespace ring
section defs
variables {R : Type*} [comm_ring R] {S : submonoid R} {P : Type*} [comm_ring P]
(f : localization_map S P)
/-- A submodule `I` is a fractional ideal if `a I ⊆ R` for some `a ≠ 0`. -/
def is_fractional (I : submodule R f.codomain) :=
∃ a ∈ S, ∀ b ∈ I, f.is_integer (f.to_map a * b)
/-- The fractional ideals of a domain `R` are ideals of `R` divided by some `a ∈ R`.
More precisely, let `P` be a localization of `R` at some submonoid `S`,
then a fractional ideal `I ⊆ P` is an `R`-submodule of `P`,
such that there is a nonzero `a : R` with `a I ⊆ R`.
-/
def fractional_ideal :=
{I : submodule R f.codomain // is_fractional f I}
end defs
namespace fractional_ideal
open set
open submodule
variables {R : Type*} [comm_ring R] {S : submonoid R} {P : Type*} [comm_ring P]
{f : localization_map S P}
instance : has_coe (fractional_ideal f) (submodule R f.codomain) := ⟨λ I, I.val⟩
@[simp] lemma val_eq_coe (I : fractional_ideal f) : I.val = I := rfl
@[simp, norm_cast] lemma coe_mk (I : submodule R f.codomain) (hI : is_fractional f I) :
(subtype.mk I hI : submodule R f.codomain) = I := rfl
instance : has_mem P (fractional_ideal f) := ⟨λ x I, x ∈ (I : submodule R f.codomain)⟩
/-- Fractional ideals are equal if their submodules are equal.
Combined with `submodule.ext` this gives that fractional ideals are equal if
they have the same elements.
-/
@[ext]
lemma ext {I J : fractional_ideal f} : (I : submodule R f.codomain) = J → I = J :=
subtype.ext_iff_val.mpr
lemma ext_iff {I J : fractional_ideal f} : (∀ x, (x ∈ I ↔ x ∈ J)) ↔ I = J :=
⟨ λ h, ext (submodule.ext h), λ h x, h ▸ iff.rfl ⟩
lemma fractional_of_subset_one (I : submodule R f.codomain)
(h : I ≤ (submodule.span R {1})) :
is_fractional f I :=
begin
use [1, S.one_mem],
intros b hb,
rw [f.to_map.map_one, one_mul],
rw ←submodule.one_eq_span at h,
obtain ⟨b', b'_mem, b'_eq_b⟩ := h hb,
rw (show b = f.to_map b', from b'_eq_b.symm),
exact set.mem_range_self b',
end
lemma is_fractional_of_le {I : submodule R f.codomain} {J : fractional_ideal f}
(hIJ : I ≤ J) : is_fractional f I :=
begin
obtain ⟨a, a_mem, ha⟩ := J.2,
use [a, a_mem],
intros b b_mem,
exact ha b (hIJ b_mem)
end
instance coe_to_fractional_ideal : has_coe (ideal R) (fractional_ideal f) :=
⟨ λ I, ⟨f.coe_submodule I, fractional_of_subset_one _ $ λ x ⟨y, hy, h⟩,
submodule.mem_span_singleton.2 ⟨y, by rw ←h; exact mul_one _⟩⟩ ⟩
@[simp, norm_cast] lemma coe_coe_ideal (I : ideal R) :
((I : fractional_ideal f) : submodule R f.codomain) = f.coe_submodule I := rfl
@[simp] lemma mem_coe_ideal {x : f.codomain} {I : ideal R} :
x ∈ (I : fractional_ideal f) ↔ ∃ (x' ∈ I), f.to_map x' = x :=
⟨ λ ⟨x', hx', hx⟩, ⟨x', hx', hx⟩,
λ ⟨x', hx', hx⟩, ⟨x', hx', hx⟩ ⟩
instance : has_zero (fractional_ideal f) := ⟨(0 : ideal R)⟩
@[simp] lemma mem_zero_iff {x : P} : x ∈ (0 : fractional_ideal f) ↔ x = 0 :=
⟨ (λ ⟨x', x'_mem_zero, x'_eq_x⟩,
have x'_eq_zero : x' = 0 := x'_mem_zero,
by simp [x'_eq_x.symm, x'_eq_zero]),
(λ hx, ⟨0, rfl, by simp [hx]⟩) ⟩
@[simp, norm_cast] lemma coe_zero : ↑(0 : fractional_ideal f) = (⊥ : submodule R f.codomain) :=
submodule.ext $ λ _, mem_zero_iff
@[simp, norm_cast] lemma coe_to_fractional_ideal_bot : ((⊥ : ideal R) : fractional_ideal f) = 0 :=
rfl
@[simp] lemma exists_mem_to_map_eq {x : R} {I : ideal R} (h : S ≤ non_zero_divisors R) :
(∃ x', x' ∈ I ∧ f.to_map x' = f.to_map x) ↔ x ∈ I :=
⟨λ ⟨x', hx', eq⟩, f.injective h eq ▸ hx', λ h, ⟨x, h, rfl⟩⟩
lemma coe_to_fractional_ideal_injective (h : S ≤ non_zero_divisors R) :
function.injective (coe : ideal R → fractional_ideal f) :=
λ I J heq, have
∀ (x : R), f.to_map x ∈ (I : fractional_ideal f) ↔ f.to_map x ∈ (J : fractional_ideal f) :=
λ x, heq ▸ iff.rfl,
ideal.ext (by { simpa only [mem_coe_ideal, exists_prop, exists_mem_to_map_eq h] using this })
lemma coe_to_fractional_ideal_eq_zero {I : ideal R} (hS : S ≤ non_zero_divisors R) :
(I : fractional_ideal f) = 0 ↔ I = (⊥ : ideal R) :=
⟨λ h, coe_to_fractional_ideal_injective hS h,
λ h, by rw [h, coe_to_fractional_ideal_bot]⟩
lemma coe_to_fractional_ideal_ne_zero {I : ideal R} (hS : S ≤ non_zero_divisors R) :
(I : fractional_ideal f) ≠ 0 ↔ I ≠ (⊥ : ideal R) :=
not_iff_not.mpr (coe_to_fractional_ideal_eq_zero hS)
lemma coe_to_submodule_eq_bot {I : fractional_ideal f} :
(I : submodule R f.codomain) = ⊥ ↔ I = 0 :=
⟨λ h, ext (by simp [h]),
λ h, by simp [h] ⟩
lemma coe_to_submodule_ne_bot {I : fractional_ideal f} :
↑I ≠ (⊥ : submodule R f.codomain) ↔ I ≠ 0 :=
not_iff_not.mpr coe_to_submodule_eq_bot
instance : inhabited (fractional_ideal f) := ⟨0⟩
instance : has_one (fractional_ideal f) :=
⟨(1 : ideal R)⟩
lemma mem_one_iff {x : P} : x ∈ (1 : fractional_ideal f) ↔ ∃ x' : R, f.to_map x' = x :=
iff.intro (λ ⟨x', _, h⟩, ⟨x', h⟩) (λ ⟨x', h⟩, ⟨x', ⟨x', set.mem_univ _, rfl⟩, h⟩)
lemma coe_mem_one (x : R) : f.to_map x ∈ (1 : fractional_ideal f) :=
mem_one_iff.mpr ⟨x, rfl⟩
lemma one_mem_one : (1 : P) ∈ (1 : fractional_ideal f) :=
mem_one_iff.mpr ⟨1, f.to_map.map_one⟩
/-- `(1 : fractional_ideal f)` is defined as the R-submodule `f(R) ≤ K`.
However, this is not definitionally equal to `1 : submodule R K`,
which is proved in the actual `simp` lemma `coe_one`. -/
lemma coe_one_eq_coe_submodule_one :
↑(1 : fractional_ideal f) = f.coe_submodule (1 : ideal R) :=
rfl
@[simp, norm_cast] lemma coe_one :
(↑(1 : fractional_ideal f) : submodule R f.codomain) = 1 :=
begin
simp only [coe_one_eq_coe_submodule_one, ideal.one_eq_top],
convert (submodule.one_eq_map_top).symm,
end
section lattice
/-!
### `lattice` section
Defines the order on fractional ideals as inclusion of their underlying sets,
and ports the lattice structure on submodules to fractional ideals.
-/
instance : partial_order (fractional_ideal f) :=
{ le := λ I J, I.1 ≤ J.1,
le_refl := λ I, le_refl I.1,
le_antisymm := λ ⟨I, hI⟩ ⟨J, hJ⟩ hIJ hJI, by { congr, exact le_antisymm hIJ hJI },
le_trans := λ _ _ _ hIJ hJK, le_trans hIJ hJK }
lemma le_iff_mem {I J : fractional_ideal f} : I ≤ J ↔ (∀ x ∈ I, x ∈ J) :=
iff.rfl
@[simp] lemma coe_le_coe {I J : fractional_ideal f} :
(I : submodule R f.codomain) ≤ (J : submodule R f.codomain) ↔ I ≤ J :=
iff.rfl
lemma zero_le (I : fractional_ideal f) : 0 ≤ I :=
begin
intros x hx,
convert submodule.zero_mem _,
simpa using hx
end
instance order_bot : order_bot (fractional_ideal f) :=
{ bot := 0,
bot_le := zero_le,
..fractional_ideal.partial_order }
@[simp] lemma bot_eq_zero : (⊥ : fractional_ideal f) = 0 :=
rfl
@[simp] lemma le_zero_iff {I : fractional_ideal f} : I ≤ 0 ↔ I = 0 :=
le_bot_iff
lemma eq_zero_iff {I : fractional_ideal f} : I = 0 ↔ (∀ x ∈ I, x = (0 : P)) :=
⟨ (λ h x hx, by simpa [h, mem_zero_iff] using hx),
(λ h, le_bot_iff.mp (λ x hx, mem_zero_iff.mpr (h x hx))) ⟩
lemma fractional_sup (I J : fractional_ideal f) : is_fractional f (I.1 ⊔ J.1) :=
begin
rcases I.2 with ⟨aI, haI, hI⟩,
rcases J.2 with ⟨aJ, haJ, hJ⟩,
use aI * aJ,
use S.mul_mem haI haJ,
intros b hb,
rcases mem_sup.mp hb with
⟨bI, hbI, bJ, hbJ, hbIJ⟩,
rw [←hbIJ, mul_add],
apply is_integer_add,
{ rw [mul_comm aI, f.to_map.map_mul, mul_assoc],
apply is_integer_smul (hI bI hbI), },
{ rw [f.to_map.map_mul, mul_assoc],
apply is_integer_smul (hJ bJ hbJ) }
end
lemma fractional_inf (I J : fractional_ideal f) : is_fractional f (I.1 ⊓ J.1) :=
begin
rcases I.2 with ⟨aI, haI, hI⟩,
use aI,
use haI,
intros b hb,
rcases mem_inf.mp hb with ⟨hbI, hbJ⟩,
exact (hI b hbI)
end
instance lattice : lattice (fractional_ideal f) :=
{ inf := λ I J, ⟨I.1 ⊓ J.1, fractional_inf I J⟩,
sup := λ I J, ⟨I.1 ⊔ J.1, fractional_sup I J⟩,
inf_le_left := λ I J, show I.1 ⊓ J.1 ≤ I.1, from inf_le_left,
inf_le_right := λ I J, show I.1 ⊓ J.1 ≤ J.1, from inf_le_right,
le_inf := λ I J K hIJ hIK, show I.1 ≤ (J.1 ⊓ K.1), from le_inf hIJ hIK,
le_sup_left := λ I J, show I.1 ≤ I.1 ⊔ J.1, from le_sup_left,
le_sup_right := λ I J, show J.1 ≤ I.1 ⊔ J.1, from le_sup_right,
sup_le := λ I J K hIK hJK, show (I.1 ⊔ J.1) ≤ K.1, from sup_le hIK hJK,
..fractional_ideal.partial_order }
instance : semilattice_sup_bot (fractional_ideal f) :=
{ ..fractional_ideal.order_bot, ..fractional_ideal.lattice }
end lattice
section semiring
instance : has_add (fractional_ideal f) := ⟨(⊔)⟩
@[simp]
lemma sup_eq_add (I J : fractional_ideal f) : I ⊔ J = I + J := rfl
@[simp, norm_cast]
lemma coe_add (I J : fractional_ideal f) : (↑(I + J) : submodule R f.codomain) = I + J := rfl
lemma fractional_mul (I J : fractional_ideal f) : is_fractional f (I.1 * J.1) :=
begin
rcases I with ⟨I, aI, haI, hI⟩,
rcases J with ⟨J, aJ, haJ, hJ⟩,
use aI * aJ,
use S.mul_mem haI haJ,
intros b hb,
apply submodule.mul_induction_on hb,
{ intros m hm n hn,
obtain ⟨n', hn'⟩ := hJ n hn,
rw [f.to_map.map_mul, mul_comm m, ←mul_assoc, mul_assoc _ _ n],
erw ←hn', rw mul_assoc,
apply hI,
exact submodule.smul_mem _ _ hm },
{ rw [mul_zero],
exact ⟨0, f.to_map.map_zero⟩ },
{ intros x y hx hy,
rw [mul_add],
apply is_integer_add hx hy },
{ intros r x hx,
show f.is_integer (_ * (f.to_map r * x)),
rw [←mul_assoc, ←f.to_map.map_mul, mul_comm _ r, f.to_map.map_mul, mul_assoc],
apply is_integer_smul hx },
end
/-- `fractional_ideal.mul` is the product of two fractional ideals,
used to define the `has_mul` instance.
This is only an auxiliary definition: the preferred way of writing `I.mul J` is `I * J`.
Elaborated terms involving `fractional_ideal` tend to grow quite large,
so by making definitions irreducible, we hope to avoid deep unfolds.
-/
@[irreducible]
def mul (I J : fractional_ideal f) : fractional_ideal f :=
⟨I.1 * J.1, fractional_mul I J⟩
local attribute [semireducible] mul
instance : has_mul (fractional_ideal f) := ⟨λ I J, mul I J⟩
@[simp] lemma mul_eq_mul (I J : fractional_ideal f) : mul I J = I * J := rfl
@[simp, norm_cast]
lemma coe_mul (I J : fractional_ideal f) : (↑(I * J) : submodule R f.codomain) = I * J := rfl
lemma mul_left_mono (I : fractional_ideal f) : monotone ((*) I) :=
λ J J' h, mul_le.mpr (λ x hx y hy, mul_mem_mul hx (h hy))
lemma mul_right_mono (I : fractional_ideal f) : monotone (λ J, J * I) :=
λ J J' h, mul_le.mpr (λ x hx y hy, mul_mem_mul (h hx) hy)
lemma mul_mem_mul {I J : fractional_ideal f} {i j : f.codomain} (hi : i ∈ I) (hj : j ∈ J) :
i * j ∈ I * J := submodule.mul_mem_mul hi hj
lemma mul_le {I J K : fractional_ideal f} :
I * J ≤ K ↔ (∀ (i ∈ I) (j ∈ J), i * j ∈ K) :=
submodule.mul_le
@[elab_as_eliminator] protected theorem mul_induction_on
{I J : fractional_ideal f}
{C : f.codomain → Prop} {r : f.codomain} (hr : r ∈ I * J)
(hm : ∀ (i ∈ I) (j ∈ J), C (i * j))
(h0 : C 0) (ha : ∀ x y, C x → C y → C (x + y))
(hs : ∀ (r : R) x, C x → C (r • x)) : C r :=
submodule.mul_induction_on hr hm h0 ha hs
instance comm_semiring : comm_semiring (fractional_ideal f) :=
{ add_assoc := λ I J K, sup_assoc,
add_comm := λ I J, sup_comm,
add_zero := λ I, sup_bot_eq,
zero_add := λ I, bot_sup_eq,
mul_assoc := λ I J K, ext (submodule.mul_assoc _ _ _),
mul_comm := λ I J, ext (submodule.mul_comm _ _),
mul_one := λ I, begin
ext,
split; intro h,
{ apply mul_le.mpr _ h,
rintros x hx y ⟨y', y'_mem_R, y'_eq_y⟩,
rw [←y'_eq_y, mul_comm],
exact submodule.smul_mem _ _ hx },
{ have : x * 1 ∈ (I * 1) := mul_mem_mul h one_mem_one,
rwa [mul_one] at this }
end,
one_mul := λ I, begin
ext,
split; intro h,
{ apply mul_le.mpr _ h,
rintros x ⟨x', x'_mem_R, x'_eq_x⟩ y hy,
rw ←x'_eq_x,
exact submodule.smul_mem _ _ hy },
{ have : 1 * x ∈ (1 * I) := mul_mem_mul one_mem_one h,
rwa [one_mul] at this }
end,
mul_zero := λ I, eq_zero_iff.mpr (λ x hx, submodule.mul_induction_on hx
(λ x hx y hy, by simp [mem_zero_iff.mp hy])
rfl
(λ x y hx hy, by simp [hx, hy])
(λ r x hx, by simp [hx])),
zero_mul := λ I, eq_zero_iff.mpr (λ x hx, submodule.mul_induction_on hx
(λ x hx y hy, by simp [mem_zero_iff.mp hx])
rfl
(λ x y hx hy, by simp [hx, hy])
(λ r x hx, by simp [hx])),
left_distrib := λ I J K, ext (mul_add _ _ _),
right_distrib := λ I J K, ext (add_mul _ _ _),
..fractional_ideal.has_zero,
..fractional_ideal.has_add,
..fractional_ideal.has_one,
..fractional_ideal.has_mul }
section order
lemma add_le_add_left {I J : fractional_ideal f} (hIJ : I ≤ J) (J' : fractional_ideal f) :
J' + I ≤ J' + J :=
sup_le_sup_left hIJ J'
lemma mul_le_mul_left {I J : fractional_ideal f} (hIJ : I ≤ J) (J' : fractional_ideal f) :
J' * I ≤ J' * J :=
mul_le.mpr (λ k hk j hj, mul_mem_mul hk (hIJ hj))
lemma le_self_mul_self {I : fractional_ideal f} (hI: 1 ≤ I) : I ≤ I * I :=
begin
convert mul_left_mono I hI,
exact (mul_one I).symm
end
lemma mul_self_le_self {I : fractional_ideal f} (hI: I ≤ 1) : I * I ≤ I :=
begin
convert mul_left_mono I hI,
exact (mul_one I).symm
end
lemma coe_ideal_le_one {I : ideal R} : (I : fractional_ideal f) ≤ 1 :=
λ x hx, let ⟨y, _, hy⟩ := fractional_ideal.mem_coe_ideal.mp hx
in fractional_ideal.mem_one_iff.mpr ⟨y, hy⟩
lemma le_one_iff_exists_coe_ideal {J : fractional_ideal f} :
J ≤ (1 : fractional_ideal f) ↔ ∃ (I : ideal R), ↑I = J :=
begin
split,
{ intro hJ,
refine ⟨⟨{x : R | f.to_map x ∈ J}, _, _, _⟩, _⟩,
{ rw [mem_set_of_eq, ring_hom.map_zero],
exact J.val.zero_mem },
{ intros a b ha hb,
rw [mem_set_of_eq, ring_hom.map_add],
exact J.val.add_mem ha hb },
{ intros c x hx,
rw [smul_eq_mul, mem_set_of_eq, ring_hom.map_mul],
exact J.val.smul_mem c hx },
{ ext x,
split,
{ rintros ⟨y, hy, eq_y⟩,
rwa ← eq_y },
{ intro hx,
obtain ⟨y, eq_x⟩ := fractional_ideal.mem_one_iff.mp (hJ hx),
rw ← eq_x at *,
exact ⟨y, hx, rfl⟩ } } },
{ rintro ⟨I, hI⟩,
rw ← hI,
apply coe_ideal_le_one },
end
end order
variables {P' : Type*} [comm_ring P'] {f' : localization_map S P'}
variables {P'' : Type*} [comm_ring P''] {f'' : localization_map S P''}
lemma fractional_map (g : f.codomain →ₐ[R] f'.codomain) (I : fractional_ideal f) :
is_fractional f' (submodule.map g.to_linear_map I.1) :=
begin
rcases I with ⟨I, a, a_nonzero, hI⟩,
use [a, a_nonzero],
intros b hb,
obtain ⟨b', b'_mem, hb'⟩ := submodule.mem_map.mp hb,
obtain ⟨x, hx⟩ := hI b' b'_mem,
use x,
erw [←g.commutes, hx, g.map_smul, hb'],
refl
end
/-- `I.map g` is the pushforward of the fractional ideal `I` along the algebra morphism `g` -/
def map (g : f.codomain →ₐ[R] f'.codomain) :
fractional_ideal f → fractional_ideal f' :=
λ I, ⟨submodule.map g.to_linear_map I.1, fractional_map g I⟩
@[simp, norm_cast] lemma coe_map (g : f.codomain →ₐ[R] f'.codomain) (I : fractional_ideal f) :
↑(map g I) = submodule.map g.to_linear_map I := rfl
@[simp] lemma mem_map {I : fractional_ideal f} {g : f.codomain →ₐ[R] f'.codomain}
{y : f'.codomain} : y ∈ I.map g ↔ ∃ x, x ∈ I ∧ g x = y :=
submodule.mem_map
variables (I J : fractional_ideal f) (g : f.codomain →ₐ[R] f'.codomain)
@[simp] lemma map_id : I.map (alg_hom.id _ _) = I :=
ext (submodule.map_id I.1)
@[simp] lemma map_comp (g' : f'.codomain →ₐ[R] f''.codomain) :
I.map (g'.comp g) = (I.map g).map g' :=
ext (submodule.map_comp g.to_linear_map g'.to_linear_map I.1)
@[simp, norm_cast] lemma map_coe_ideal (I : ideal R) :
(I : fractional_ideal f).map g = I :=
begin
ext x,
simp only [coe_coe_ideal, mem_coe_submodule],
split,
{ rintro ⟨_, ⟨y, hy, rfl⟩, rfl⟩,
exact ⟨y, hy, (g.commutes y).symm⟩ },
{ rintro ⟨y, hy, rfl⟩,
exact ⟨_, ⟨y, hy, rfl⟩, g.commutes y⟩ },
end
@[simp] lemma map_one :
(1 : fractional_ideal f).map g = 1 :=
map_coe_ideal g 1
@[simp] lemma map_zero :
(0 : fractional_ideal f).map g = 0 :=
map_coe_ideal g 0
@[simp] lemma map_add : (I + J).map g = I.map g + J.map g :=
ext (submodule.map_sup _ _ _)
@[simp] lemma map_mul : (I * J).map g = I.map g * J.map g :=
ext (submodule.map_mul _ _ _)
@[simp] lemma map_map_symm (g : f.codomain ≃ₐ[R] f'.codomain) :
(I.map (g : f.codomain →ₐ[R] f'.codomain)).map (g.symm : f'.codomain →ₐ[R] f.codomain) = I :=
by rw [←map_comp, g.symm_comp, map_id]
@[simp] lemma map_symm_map (I : fractional_ideal f') (g : f.codomain ≃ₐ[R] f'.codomain) :
(I.map (g.symm : f'.codomain →ₐ[R] f.codomain)).map (g : f.codomain →ₐ[R] f'.codomain) = I :=
by rw [←map_comp, g.comp_symm, map_id]
/-- If `g` is an equivalence, `map g` is an isomorphism -/
def map_equiv (g : f.codomain ≃ₐ[R] f'.codomain) :
fractional_ideal f ≃+* fractional_ideal f' :=
{ to_fun := map g,
inv_fun := map g.symm,
map_add' := λ I J, map_add I J _,
map_mul' := λ I J, map_mul I J _,
left_inv := λ I, by { rw [←map_comp, alg_equiv.symm_comp, map_id] },
right_inv := λ I, by { rw [←map_comp, alg_equiv.comp_symm, map_id] } }
@[simp] lemma coe_fun_map_equiv (g : f.codomain ≃ₐ[R] f'.codomain) :
⇑(map_equiv g) = map g :=
rfl
@[simp] lemma map_equiv_apply (g : f.codomain ≃ₐ[R] f'.codomain) (I : fractional_ideal f) :
map_equiv g I = map ↑g I := rfl
@[simp] lemma map_equiv_symm (g : f.codomain ≃ₐ[R] f'.codomain) :
(map_equiv g).symm = map_equiv g.symm := rfl
@[simp] lemma map_equiv_refl :
map_equiv alg_equiv.refl = ring_equiv.refl (fractional_ideal f) :=
ring_equiv.ext (λ x, by simp)
lemma is_fractional_span_iff {s : set f.codomain} :
is_fractional f (span R s) ↔ ∃ a ∈ S, ∀ (b : P), b ∈ s → f.is_integer (f.to_map a * b) :=
⟨ λ ⟨a, a_mem, h⟩, ⟨a, a_mem, λ b hb, h b (subset_span hb)⟩,
λ ⟨a, a_mem, h⟩, ⟨a, a_mem, λ b hb, span_induction hb
h
(by { rw mul_zero, exact f.is_integer_zero })
(λ x y hx hy, by { rw mul_add, exact is_integer_add hx hy })
(λ s x hx, by { rw algebra.mul_smul_comm, exact is_integer_smul hx }) ⟩ ⟩
lemma is_fractional_of_fg {I : submodule R f.codomain} (hI : I.fg) :
is_fractional f I :=
begin
rcases hI with ⟨I, rfl⟩,
rcases localization_map.exist_integer_multiples_of_finset f I with ⟨⟨s, hs1⟩, hs⟩,
rw is_fractional_span_iff,
exact ⟨s, hs1, hs⟩,
end
/-- `canonical_equiv f f'` is the canonical equivalence between the fractional
ideals in `f.codomain` and in `f'.codomain` -/
@[irreducible]
noncomputable def canonical_equiv (f : localization_map S P) (f' : localization_map S P') :
fractional_ideal f ≃+* fractional_ideal f' :=
map_equiv
{ commutes' := λ r, ring_equiv_of_ring_equiv_eq _ _ _,
..ring_equiv_of_ring_equiv f f' (ring_equiv.refl R)
(by rw [ring_equiv.to_monoid_hom_refl, submonoid.map_id]) }
@[simp] lemma mem_canonical_equiv_apply {I : fractional_ideal f} {x : f'.codomain} :
x ∈ canonical_equiv f f' I ↔
∃ y ∈ I, @localization_map.map _ _ _ _ _ _ _ f (ring_hom.id _) _ (λ ⟨y, hy⟩, hy) _ _ f' y = x :=
begin
rw [canonical_equiv, map_equiv_apply, mem_map],
exact ⟨λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩, λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩⟩
end
@[simp] lemma canonical_equiv_symm (f : localization_map S P) (f' : localization_map S P') :
(canonical_equiv f f').symm = canonical_equiv f' f :=
ring_equiv.ext $ λ I, fractional_ideal.ext_iff.mp $ λ x,
by { erw [mem_canonical_equiv_apply, canonical_equiv, map_equiv_symm, map_equiv, mem_map],
exact ⟨λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩, λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩⟩ }
@[simp] lemma canonical_equiv_flip (f : localization_map S P) (f' : localization_map S P') (I) :
canonical_equiv f f' (canonical_equiv f' f I) = I :=
by rw [←canonical_equiv_symm, ring_equiv.symm_apply_apply]
end semiring
section fraction_map
/-!
### `fraction_map` section
This section concerns fractional ideals in the field of fractions,
i.e. the type `fractional_ideal g` when `g` is a `fraction_map R K`.
-/
variables {K K' : Type*} [field K] [field K'] {g : fraction_map R K} {g' : fraction_map R K'}
variables {I J : fractional_ideal g} (h : g.codomain →ₐ[R] g'.codomain)
/-- Nonzero fractional ideals contain a nonzero integer. -/
lemma exists_ne_zero_mem_is_integer [nontrivial R] (hI : I ≠ 0) :
∃ x ≠ (0 : R), g.to_map x ∈ I :=
begin
obtain ⟨y, y_mem, y_not_mem⟩ := submodule.exists_of_lt (bot_lt_iff_ne_bot.mpr hI),
have y_ne_zero : y ≠ 0 := by simpa using y_not_mem,
obtain ⟨z, ⟨x, hx⟩⟩ := g.exists_integer_multiple y,
refine ⟨x, _, _⟩,
{ rw [ne.def, ← g.to_map_eq_zero_iff, hx],
exact mul_ne_zero (g.to_map_ne_zero_of_mem_non_zero_divisors _) y_ne_zero },
{ rw hx,
exact smul_mem _ _ y_mem }
end
lemma map_ne_zero [nontrivial R] (hI : I ≠ 0) : I.map h ≠ 0 :=
begin
obtain ⟨x, x_ne_zero, hx⟩ := exists_ne_zero_mem_is_integer hI,
contrapose! x_ne_zero with map_eq_zero,
refine g'.to_map_eq_zero_iff.mp (eq_zero_iff.mp map_eq_zero _ (mem_map.mpr _)),
exact ⟨g.to_map x, hx, h.commutes x⟩,
end
@[simp] lemma map_eq_zero_iff [nontrivial R] : I.map h = 0 ↔ I = 0 :=
⟨imp_of_not_imp_not _ _ (map_ne_zero _),
λ hI, hI.symm ▸ map_zero h⟩
end fraction_map
section quotient
/-!
### `quotient` section
This section defines the ideal quotient of fractional ideals.
In this section we need that each non-zero `y : R` has an inverse in
the localization, i.e. that the localization is a field. We satisfy this
assumption by taking `S = non_zero_divisors R`, `R`'s localization at which
is a field because `R` is a domain.
-/
open_locale classical
variables {R₁ : Type*} [integral_domain R₁] {K : Type*} [field K] {g : fraction_map R₁ K}
instance : nontrivial (fractional_ideal g) :=
⟨⟨0, 1, λ h,
have this : (1 : K) ∈ (0 : fractional_ideal g) :=
by rw ←g.to_map.map_one; convert coe_mem_one _,
one_ne_zero (mem_zero_iff.mp this) ⟩⟩
lemma fractional_div_of_nonzero {I J : fractional_ideal g} (h : J ≠ 0) :
is_fractional g (I.1 / J.1) :=
begin
rcases I with ⟨I, aI, haI, hI⟩,
rcases J with ⟨J, aJ, haJ, hJ⟩,
obtain ⟨y, mem_J, not_mem_zero⟩ := exists_of_lt (bot_lt_iff_ne_bot.mpr h),
obtain ⟨y', hy'⟩ := hJ y mem_J,
use (aI * y'),
split,
{ apply (non_zero_divisors R₁).mul_mem haI (mem_non_zero_divisors_iff_ne_zero.mpr _),
intro y'_eq_zero,
have : g.to_map aJ * y = 0 := by rw [←hy', y'_eq_zero, g.to_map.map_zero],
obtain aJ_zero | y_zero := mul_eq_zero.mp this,
{ have : aJ = 0 := g.to_map.injective_iff.1 g.injective _ aJ_zero,
have : aJ ≠ 0 := mem_non_zero_divisors_iff_ne_zero.mp haJ,
contradiction },
{ exact not_mem_zero (mem_zero_iff.mpr y_zero) } },
intros b hb,
rw [g.to_map.map_mul, mul_assoc, mul_comm _ b, hy'],
exact hI _ (hb _ (submodule.smul_mem _ aJ mem_J)),
end
noncomputable instance fractional_ideal_has_div :
has_div (fractional_ideal g) :=
⟨ λ I J, if h : J = 0 then 0 else ⟨I.1 / J.1, fractional_div_of_nonzero h⟩ ⟩
variables {I J : fractional_ideal g} [ J ≠ 0 ]
@[simp] lemma div_zero {I : fractional_ideal g} :
I / 0 = 0 :=
dif_pos rfl
lemma div_nonzero {I J : fractional_ideal g} (h : J ≠ 0) :
(I / J) = ⟨I.1 / J.1, fractional_div_of_nonzero h⟩ :=
dif_neg h
@[simp] lemma coe_div {I J : fractional_ideal g} (hJ : J ≠ 0) :
(↑(I / J) : submodule R₁ g.codomain) = ↑I / (↑J : submodule R₁ g.codomain) :=
begin
unfold has_div.div,
simp only [dif_neg hJ, coe_mk, val_eq_coe],
end
lemma mem_div_iff_of_nonzero {I J : fractional_ideal g} (h : J ≠ 0) {x} :
x ∈ I / J ↔ ∀ y ∈ J, x * y ∈ I :=
by { rw div_nonzero h, exact submodule.mem_div_iff_forall_mul_mem }
lemma mul_one_div_le_one {I : fractional_ideal g} : I * (1 / I) ≤ 1 :=
begin
by_cases hI : I = 0,
{ rw [hI, div_zero, mul_zero],
exact zero_le 1 },
{ rw [← coe_le_coe, coe_mul, coe_div hI, coe_one],
apply submodule.mul_one_div_le_one },
end
lemma le_self_mul_one_div {I : fractional_ideal g} (hI : I ≤ (1 : fractional_ideal g)) :
I ≤ I * (1 / I) :=
begin
by_cases hI_nz : I = 0,
{ rw [hI_nz, div_zero, mul_zero], exact zero_le 0 },
{ rw [← coe_le_coe, coe_mul, coe_div hI_nz, coe_one],
rw [← coe_le_coe, coe_one] at hI,
exact submodule.le_self_mul_one_div hI },
end
lemma le_div_iff_of_nonzero {I J J' : fractional_ideal g} (hJ' : J' ≠ 0) :
I ≤ J / J' ↔ ∀ (x ∈ I) (y ∈ J'), x * y ∈ J :=
⟨ λ h x hx, (mem_div_iff_of_nonzero hJ').mp (h hx),
λ h x hx, (mem_div_iff_of_nonzero hJ').mpr (h x hx) ⟩
lemma le_div_iff_mul_le {I J J' : fractional_ideal g} (hJ' : J' ≠ 0) : I ≤ J / J' ↔ I * J' ≤ J :=
begin
rw div_nonzero hJ',
convert submodule.le_div_iff_mul_le using 1,
rw [val_eq_coe, val_eq_coe, ←coe_mul],
refl,
end
@[simp] lemma div_one {I : fractional_ideal g} : I / 1 = I :=
begin
rw [div_nonzero (@one_ne_zero (fractional_ideal g) _ _)],
ext,
split; intro h,
{ convert mem_div_iff_forall_mul_mem.mp h 1
(g.to_map.map_one ▸ coe_mem_one 1), simp },
{ apply mem_div_iff_forall_mul_mem.mpr,
rintros y ⟨y', _, y_eq_y'⟩,
rw mul_comm,
convert submodule.smul_mem _ y' h,
rw ←y_eq_y',
refl }
end
lemma ne_zero_of_mul_eq_one (I J : fractional_ideal g) (h : I * J = 1) : I ≠ 0 :=
λ hI, @zero_ne_one (fractional_ideal g) _ _ (by { convert h, simp [hI], })
theorem eq_one_div_of_mul_eq_one (I J : fractional_ideal g) (h : I * J = 1) :
J = 1 / I :=
begin
have hI : I ≠ 0 := ne_zero_of_mul_eq_one I J h,
suffices h' : I * (1 / I) = 1,
{ exact (congr_arg units.inv $
@units.ext _ _ (units.mk_of_mul_eq_one _ _ h) (units.mk_of_mul_eq_one _ _ h') rfl) },
apply le_antisymm,
{ apply mul_le.mpr _,
intros x hx y hy,
rw mul_comm,
exact (mem_div_iff_of_nonzero hI).mp hy x hx },
rw ← h,
apply mul_left_mono I,
apply (le_div_iff_of_nonzero hI).mpr _,
intros y hy x hx,
rw mul_comm,
exact mul_mem_mul hx hy,
end
theorem mul_div_self_cancel_iff {I : fractional_ideal g} :
I * (1 / I) = 1 ↔ ∃ J, I * J = 1 :=
⟨λ h, ⟨(1 / I), h⟩, λ ⟨J, hJ⟩, by rwa [← eq_one_div_of_mul_eq_one I J hJ]⟩
variables {K' : Type*} [field K'] {g' : fraction_map R₁ K'}
@[simp] lemma map_div (I J : fractional_ideal g) (h : g.codomain ≃ₐ[R₁] g'.codomain) :
(I / J).map (h : g.codomain →ₐ[R₁] g'.codomain) = I.map h / J.map h :=
begin
by_cases H : J = 0,
{ rw [H, div_zero, map_zero, div_zero] },
{ ext x,
simp [div_nonzero H, div_nonzero (map_ne_zero _ H), submodule.map_div] }
end
@[simp] lemma map_one_div (I : fractional_ideal g) (h : g.codomain ≃ₐ[R₁] g'.codomain) :
(1 / I).map (h : g.codomain →ₐ[R₁] g'.codomain) = 1 / I.map h :=
by rw [map_div, map_one]
end quotient
section principal_ideal_ring
variables {R₁ : Type*} [integral_domain R₁] {K : Type*} [field K] {g : fraction_map R₁ K}
open_locale classical
open submodule submodule.is_principal
lemma is_fractional_span_singleton (x : f.codomain) : is_fractional f (span R {x}) :=
let ⟨a, ha⟩ := f.exists_integer_multiple x in
is_fractional_span_iff.mpr ⟨ a.1, a.2, λ x hx, (mem_singleton_iff.mp hx).symm ▸ ha⟩
/-- `span_singleton x` is the fractional ideal generated by `x` if `0 ∉ S` -/
@[irreducible]
def span_singleton (x : f.codomain) : fractional_ideal f :=
⟨span R {x}, is_fractional_span_singleton x⟩
local attribute [semireducible] span_singleton
@[simp] lemma coe_span_singleton (x : f.codomain) :
(span_singleton x : submodule R f.codomain) = span R {x} := rfl
@[simp] lemma mem_span_singleton {x y : f.codomain} :
x ∈ span_singleton y ↔ ∃ (z : R), z • y = x :=
submodule.mem_span_singleton
lemma mem_span_singleton_self (x : f.codomain) :
x ∈ span_singleton x :=
mem_span_singleton.mpr ⟨1, one_smul _ _⟩
lemma eq_span_singleton_of_principal (I : fractional_ideal f)
[is_principal (I : submodule R f.codomain)] :
I = span_singleton (generator (I : submodule R f.codomain)) :=
ext (span_singleton_generator I.1).symm
lemma is_principal_iff (I : fractional_ideal f) :
is_principal (I : submodule R f.codomain) ↔ ∃ x, I = span_singleton x :=
⟨λ h, ⟨@generator _ _ _ _ _ I.1 h, @eq_span_singleton_of_principal _ _ _ _ _ _ I h⟩,
λ ⟨x, hx⟩, { principal := ⟨x, trans (congr_arg _ hx) (coe_span_singleton x)⟩ } ⟩
@[simp] lemma span_singleton_zero : span_singleton (0 : f.codomain) = 0 :=
by { ext, simp [submodule.mem_span_singleton, eq_comm] }
lemma span_singleton_eq_zero_iff {y : f.codomain} : span_singleton y = 0 ↔ y = 0 :=
⟨λ h, span_eq_bot.mp (by simpa using congr_arg subtype.val h : span R {y} = ⊥) y (mem_singleton y),
λ h, by simp [h] ⟩
lemma span_singleton_ne_zero_iff {y : f.codomain} : span_singleton y ≠ 0 ↔ y ≠ 0 :=
not_congr span_singleton_eq_zero_iff
@[simp] lemma span_singleton_one : span_singleton (1 : f.codomain) = 1 :=
begin
ext,
refine mem_span_singleton.trans ((exists_congr _).trans mem_one_iff.symm),
intro x',
refine eq.congr (mul_one _) rfl,
end
@[simp]
lemma span_singleton_mul_span_singleton (x y : f.codomain) :
span_singleton x * span_singleton y = span_singleton (x * y) :=
begin
ext,
simp_rw [coe_mul, coe_span_singleton, span_mul_span, singleton.is_mul_hom.map_mul]
end
@[simp]
lemma coe_ideal_span_singleton (x : R) :
(↑(span R {x} : ideal R) : fractional_ideal f) = span_singleton (f.to_map x) :=
begin
ext y,
refine mem_coe_ideal.trans (iff.trans _ mem_span_singleton.symm),
split,
{ rintros ⟨y', hy', rfl⟩,
obtain ⟨x', rfl⟩ := submodule.mem_span_singleton.mp hy',
use x',
rw [smul_eq_mul, f.to_map.map_mul],
refl },
{ rintros ⟨y', rfl⟩,
exact ⟨y' * x, submodule.mem_span_singleton.mpr ⟨y', rfl⟩, f.to_map.map_mul _ _⟩ }
end
@[simp]
lemma canonical_equiv_span_singleton (f : localization_map S P) {P'} [comm_ring P']
(f' : localization_map S P') (x : f.codomain) :
canonical_equiv f f' (span_singleton x) =
span_singleton (f.map (show ∀ (y : S), ring_hom.id _ y.1 ∈ S, from λ y, y.2) f' x) :=
begin
apply ext_iff.mp,
intro y,
split; intro h,
{ apply mem_span_singleton.mpr,
obtain ⟨x', hx', rfl⟩ := mem_canonical_equiv_apply.mp h,
obtain ⟨z, rfl⟩ := mem_span_singleton.mp hx',
use z,
rw localization_map.map_smul,
refl },
{ apply mem_canonical_equiv_apply.mpr,
obtain ⟨z, rfl⟩ := mem_span_singleton.mp h,
use f.to_map z * x,
use mem_span_singleton.mpr ⟨z, rfl⟩,
rw [ring_hom.map_mul, localization_map.map_eq],
refl }
end
lemma mem_singleton_mul {x y : f.codomain} {I : fractional_ideal f} :
y ∈ span_singleton x * I ↔ ∃ y' ∈ I, y = x * y' :=
begin
split,
{ intro h,
apply fractional_ideal.mul_induction_on h,
{ intros x' hx' y' hy',
obtain ⟨a, ha⟩ := mem_span_singleton.mp hx',
use [a • y', I.1.smul_mem a hy'],
rw [←ha, algebra.mul_smul_comm, algebra.smul_mul_assoc] },
{ exact ⟨0, I.1.zero_mem, (mul_zero x).symm⟩ },
{ rintros _ _ ⟨y, hy, rfl⟩ ⟨y', hy', rfl⟩,
exact ⟨y + y', I.1.add_mem hy hy', (mul_add _ _ _).symm⟩ },
{ rintros r _ ⟨y', hy', rfl⟩,
exact ⟨r • y', I.1.smul_mem r hy', (algebra.mul_smul_comm _ _ _).symm ⟩ } },
{ rintros ⟨y', hy', rfl⟩,
exact mul_mem_mul (mem_span_singleton.mpr ⟨1, one_smul _ _⟩) hy' }
end
lemma one_div_span_singleton (x : g.codomain) :
1 / span_singleton x = span_singleton (x⁻¹) :=
if h : x = 0 then by simp [h] else (eq_one_div_of_mul_eq_one _ _ (by simp [h])).symm
@[simp] lemma div_span_singleton (J : fractional_ideal g) (d : g.codomain) :
J / span_singleton d = span_singleton (d⁻¹) * J :=
begin
rw ← one_div_span_singleton,
by_cases hd : d = 0,
{ simp only [hd, span_singleton_zero, div_zero, zero_mul] },
have h_spand : span_singleton d ≠ 0 := mt span_singleton_eq_zero_iff.mp hd,
apply le_antisymm,
{ intros x hx,
rw [val_eq_coe, coe_div h_spand, submodule.mem_div_iff_forall_mul_mem] at hx,
specialize hx d (mem_span_singleton_self d),
have h_xd : x = d⁻¹ * (x * d), { field_simp },
rw [val_eq_coe, coe_mul, one_div_span_singleton, h_xd],
exact submodule.mul_mem_mul (mem_span_singleton_self _) hx },
{ rw [le_div_iff_mul_le h_spand, mul_assoc, mul_left_comm, one_div_span_singleton,
span_singleton_mul_span_singleton, inv_mul_cancel hd, span_singleton_one, mul_one],
exact le_refl J },
end
lemma exists_eq_span_singleton_mul (I : fractional_ideal g) :
∃ (a : R₁) (aI : ideal R₁), a ≠ 0 ∧ I = span_singleton (g.to_map a)⁻¹ * aI :=
begin
obtain ⟨a_inv, nonzero, ha⟩ := I.2,
have nonzero := mem_non_zero_divisors_iff_ne_zero.mp nonzero,
have map_a_nonzero := mt g.to_map_eq_zero_iff.mp nonzero,
use a_inv,
use (span_singleton (g.to_map a_inv) * I).1.comap g.lin_coe,
split, exact nonzero,
ext,
refine iff.trans _ mem_singleton_mul.symm,
split,
{ intro hx,
obtain ⟨x', hx'⟩ := ha x hx,
refine ⟨g.to_map x', mem_coe_ideal.mpr ⟨x', (mem_singleton_mul.mpr ⟨x, hx, hx'⟩), rfl⟩, _⟩,
erw [hx', ←mul_assoc, inv_mul_cancel map_a_nonzero, one_mul] },
{ rintros ⟨y, hy, rfl⟩,
obtain ⟨x', hx', rfl⟩ := mem_coe_ideal.mp hy,
obtain ⟨y', hy', hx'⟩ := mem_singleton_mul.mp hx',
rw lin_coe_apply at hx',
erw [hx', ←mul_assoc, inv_mul_cancel map_a_nonzero, one_mul],
exact hy' }
end
instance is_principal {R} [integral_domain R] [is_principal_ideal_ring R] {f : fraction_map R K}
(I : fractional_ideal f) : (I : submodule R f.codomain).is_principal :=
begin
obtain ⟨a, aI, -, ha⟩ := exists_eq_span_singleton_mul I,
use (f.to_map a)⁻¹ * f.to_map (generator aI),
suffices : I = span_singleton ((f.to_map a)⁻¹ * f.to_map (generator aI)),
{ exact congr_arg subtype.val this },
conv_lhs { rw [ha, ←span_singleton_generator aI] },
rw [coe_ideal_span_singleton (generator aI), span_singleton_mul_span_singleton]
end
end principal_ideal_ring
variables {R₁ : Type*} [integral_domain R₁]
variables {K : Type*} [field K] {g : fraction_map R₁ K}
local attribute [instance] classical.prop_decidable
lemma is_noetherian_zero : is_noetherian R₁ (0 : fractional_ideal g) :=
is_noetherian_submodule.mpr (λ I (hI : I ≤ (0 : fractional_ideal g)),
by { rw coe_zero at hI, rw le_bot_iff.mp hI, exact fg_bot })
lemma is_noetherian_iff {I : fractional_ideal g} :
is_noetherian R₁ I ↔ ∀ J ≤ I, (J : submodule R₁ g.codomain).fg :=
is_noetherian_submodule.trans ⟨λ h J hJ, h _ hJ, λ h J hJ, h ⟨J, is_fractional_of_le hJ⟩ hJ⟩
lemma is_noetherian_coe_to_fractional_ideal [is_noetherian_ring R₁] (I : ideal R₁) :
is_noetherian R₁ (I : fractional_ideal g) :=
begin
rw is_noetherian_iff,
intros J hJ,
obtain ⟨J, rfl⟩ := le_one_iff_exists_coe_ideal.mp (le_trans hJ coe_ideal_le_one),
exact fg_map (is_noetherian.noetherian J),
end
lemma is_noetherian_span_singleton_inv_to_map_mul (x : R₁) {I : fractional_ideal g}
(hI : is_noetherian R₁ I) :
is_noetherian R₁ (span_singleton (g.to_map x)⁻¹ * I : fractional_ideal g) :=
begin
by_cases hx : x = 0,
{ rw [hx, g.to_map.map_zero, _root_.inv_zero, span_singleton_zero, zero_mul],
exact is_noetherian_zero },
have h_gx : g.to_map x ≠ 0,
from mt (g.to_map.injective_iff.mp (fraction_map.injective g) x) hx,
have h_spanx : span_singleton (g.to_map x) ≠ (0 : fractional_ideal g),
from span_singleton_ne_zero_iff.mpr h_gx,
rw is_noetherian_iff at ⊢ hI,
intros J hJ,
rw [← div_span_singleton, le_div_iff_mul_le h_spanx] at hJ,
obtain ⟨s, hs⟩ := hI _ hJ,
use s * {(g.to_map x)⁻¹},
rw [finset.coe_mul, finset.coe_singleton, ← span_mul_span, hs, ← coe_span_singleton, ← coe_mul,
mul_assoc, span_singleton_mul_span_singleton, mul_inv_cancel h_gx,
span_singleton_one, mul_one],
end
/-- Every fractional ideal of a noetherian integral domain is noetherian. -/
theorem is_noetherian [is_noetherian_ring R₁] (I : fractional_ideal g) : is_noetherian R₁ I :=
begin
obtain ⟨d, J, h_nzd, rfl⟩ := exists_eq_span_singleton_mul I,
apply is_noetherian_span_singleton_inv_to_map_mul,
apply is_noetherian_coe_to_fractional_ideal,
end
end fractional_ideal
end ring
|
01046305a8dc27c4bfa032acb81a124d2d5fc34a | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/topology/category/Profinite.lean | 2bdb6e34f734474f27cf2a2caf25610e6e423e45 | [
"Apache-2.0"
] | permissive | hjvromen/lewis | 40b035973df7c77ebf927afab7878c76d05ff758 | 105b675f73630f028ad5d890897a51b3c1146fb0 | refs/heads/master | 1,677,944,636,343 | 1,676,555,301,000 | 1,676,555,301,000 | 327,553,599 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,286 | 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 topology.category.CompHaus
/-!
# The category of Profinite Types
We construct the category of profinite topological spaces,
often called profinite sets -- perhaps they could be called
profinite types in Lean.
The type of profinite topological spaces is called `Profinite`. It has a category
instance and is a fully faithful subcategory of `Top`. The fully faithful functor
is called `Profinite_to_Top`.
## Implementation notes
A profinite type is defined to be a topological space which is
compact, Hausdorff and totally disconnected.
## TODO
0. Link to category of projective limits of finite discrete sets.
1. existence of products, limits(?), finite coproducts
2. `Profinite_to_Top` creates limits?
3. Clausen/Scholze topology on the category `Profinite`.
## Tags
profinite
-/
open category_theory
/-- The type of profinite topological spaces. -/
structure Profinite :=
(to_Top : Top)
[is_compact : compact_space to_Top]
[is_t2 : t2_space to_Top]
[is_totally_disconnected : totally_disconnected_space to_Top]
namespace Profinite
instance : inhabited Profinite := ⟨{to_Top := { α := pempty }}⟩
instance : has_coe_to_sort Profinite := ⟨Type*, λ X, X.to_Top⟩
instance {X : Profinite} : compact_space X := X.is_compact
instance {X : Profinite} : t2_space X := X.is_t2
instance {X : Profinite} : totally_disconnected_space X := X.is_totally_disconnected
instance category : category Profinite := induced_category.category to_Top
@[simp]
lemma coe_to_Top {X : Profinite} : (X.to_Top : Type*) = X :=
rfl
end Profinite
/-- The fully faithful embedding of `Profinite` in `Top`. -/
@[simps, derive [full, faithful]]
def Profinite_to_Top : Profinite ⥤ Top := induced_functor _
/-- The fully faithful embedding of `Profinite` in `CompHaus`. -/
@[simps] def Profinite_to_CompHaus : Profinite ⥤ CompHaus :=
{ obj := λ X, { to_Top := X.to_Top },
map := λ _ _ f, f }
instance : full Profinite_to_CompHaus := { preimage := λ _ _ f, f }
instance : faithful Profinite_to_CompHaus := {}
@[simp] lemma Profinite_to_CompHaus_to_Top :
Profinite_to_CompHaus ⋙ CompHaus_to_Top = Profinite_to_Top :=
rfl
|
b58142b06d841099a0abb5cccb849e4070f5dd1c | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/data/sum/basic.lean | 028460ba4a31d3cf6978c4d7610e23c576b2c56d | [
"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,894 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Yury G. Kudryashov
-/
import data.option.basic
/-!
# Disjoint union of types
This file proves basic results about the sum type `α ⊕ β`.
`α ⊕ β` is the type made of a copy of `α` and a copy of `β`. It is also called *disjoint union*.
## Main declarations
* `sum.get_left`: Retrieves the left content of `x : α ⊕ β` or returns `none` if it's coming from
the right.
* `sum.get_right`: Retrieves the right content of `x : α ⊕ β` or returns `none` if it's coming from
the left.
* `sum.is_left`: Returns whether `x : α ⊕ β` comes from the left component or not.
* `sum.is_right`: Returns whether `x : α ⊕ β` comes from the right component or not.
* `sum.map`: Maps `α ⊕ β` to `γ ⊕ δ` component-wise.
* `sum.elim`: Nondependent eliminator/induction principle for `α ⊕ β`.
* `sum.swap`: Maps `α ⊕ β` to `β ⊕ α` by swapping components.
* `sum.lex`: Lexicographic order on `α ⊕ β` induced by a relation on `α` and a relation on `β`.
## Notes
The definition of `sum` takes values in `Type*`. This effectively forbids `Prop`- valued sum types.
To this effect, we have `psum`, which takes value in `Sort*` and carries a more complicated
universe signature in consequence. The `Prop` version is `or`.
-/
universes u v w x
variables {α : Type u} {α' : Type w} {β : Type v} {β' : Type x} {γ δ : Type*}
namespace sum
attribute [derive decidable_eq] sum
@[simp] lemma «forall» {p : α ⊕ β → Prop} : (∀ x, p x) ↔ (∀ a, p (inl a)) ∧ ∀ b, p (inr b) :=
⟨λ h, ⟨λ a, h _, λ b, h _⟩, λ ⟨h₁, h₂⟩, sum.rec h₁ h₂⟩
@[simp] lemma «exists» {p : α ⊕ β → Prop} : (∃ x, p x) ↔ (∃ a, p (inl a)) ∨ ∃ b, p (inr b) :=
⟨λ h, match h with
| ⟨inl a, h⟩ := or.inl ⟨a, h⟩
| ⟨inr b, h⟩ := or.inr ⟨b, h⟩
end, λ h, match h with
| or.inl ⟨a, h⟩ := ⟨inl a, h⟩
| or.inr ⟨b, h⟩ := ⟨inr b, h⟩
end⟩
lemma inl_injective : function.injective (inl : α → α ⊕ β) := λ x y, inl.inj
lemma inr_injective : function.injective (inr : β → α ⊕ β) := λ x y, inr.inj
section get
/-- Check if a sum is `inl` and if so, retrieve its contents. -/
@[simp] def get_left : α ⊕ β → option α
| (inl a) := some a
| (inr _) := none
/-- Check if a sum is `inr` and if so, retrieve its contents. -/
@[simp] def get_right : α ⊕ β → option β
| (inr b) := some b
| (inl _) := none
/-- Check if a sum is `inl`. -/
@[simp] def is_left : α ⊕ β → bool
| (inl _) := tt
| (inr _) := ff
/-- Check if a sum is `inr`. -/
@[simp] def is_right : α ⊕ β → bool
| (inl _) := ff
| (inr _) := tt
variables {x y : α ⊕ β}
lemma get_left_eq_none_iff : x.get_left = none ↔ x.is_right :=
by cases x; simp only [get_left, is_right, coe_sort_tt, coe_sort_ff, eq_self_iff_true]
lemma get_right_eq_none_iff : x.get_right = none ↔ x.is_left :=
by cases x; simp only [get_right, is_left, coe_sort_tt, coe_sort_ff, eq_self_iff_true]
end get
/-- Map `α ⊕ β` to `α' ⊕ β'` sending `α` to `α'` and `β` to `β'`. -/
protected def map (f : α → α') (g : β → β') : α ⊕ β → α' ⊕ β'
| (inl x) := inl (f x)
| (inr x) := inr (g x)
@[simp] lemma map_inl (f : α → α') (g : β → β') (x : α) : (inl x).map f g = inl (f x) := rfl
@[simp] lemma map_inr (f : α → α') (g : β → β') (x : β) : (inr x).map f g = inr (g x) := rfl
@[simp] lemma map_map {α'' β''} (f' : α' → α'') (g' : β' → β'') (f : α → α') (g : β → β') :
∀ x : α ⊕ β, (x.map f g).map f' g' = x.map (f' ∘ f) (g' ∘ g)
| (inl a) := rfl
| (inr b) := rfl
@[simp] lemma map_comp_map {α'' β''} (f' : α' → α'') (g' : β' → β'') (f : α → α') (g : β → β') :
(sum.map f' g') ∘ (sum.map f g) = sum.map (f' ∘ f) (g' ∘ g) :=
funext $ map_map f' g' f g
@[simp] lemma map_id_id (α β) : sum.map (@id α) (@id β) = id :=
funext $ λ x, sum.rec_on x (λ _, rfl) (λ _, rfl)
theorem inl.inj_iff {a b} : (inl a : α ⊕ β) = inl b ↔ a = b :=
⟨inl.inj, congr_arg _⟩
theorem inr.inj_iff {a b} : (inr a : α ⊕ β) = inr b ↔ a = b :=
⟨inr.inj, congr_arg _⟩
theorem inl_ne_inr {a : α} {b : β} : inl a ≠ inr b.
theorem inr_ne_inl {a : α} {b : β} : inr b ≠ inl a.
/-- Define a function on `α ⊕ β` by giving separate definitions on `α` and `β`. -/
protected def elim {α β γ : Sort*} (f : α → γ) (g : β → γ) : α ⊕ β → γ := λ x, sum.rec_on x f g
@[simp] lemma elim_inl {α β γ : Sort*} (f : α → γ) (g : β → γ) (x : α) :
sum.elim f g (inl x) = f x := rfl
@[simp] lemma elim_inr {α β γ : Sort*} (f : α → γ) (g : β → γ) (x : β) :
sum.elim f g (inr x) = g x := rfl
@[simp] lemma elim_comp_inl {α β γ : Sort*} (f : α → γ) (g : β → γ) :
sum.elim f g ∘ inl = f := rfl
@[simp] lemma elim_comp_inr {α β γ : Sort*} (f : α → γ) (g : β → γ) :
sum.elim f g ∘ inr = g := rfl
@[simp] lemma elim_inl_inr {α β : Sort*} :
@sum.elim α β _ inl inr = id :=
funext $ λ x, sum.cases_on x (λ _, rfl) (λ _, rfl)
lemma comp_elim {α β γ δ : Sort*} (f : γ → δ) (g : α → γ) (h : β → γ):
f ∘ sum.elim g h = sum.elim (f ∘ g) (f ∘ h) :=
funext $ λ x, sum.cases_on x (λ _, rfl) (λ _, rfl)
@[simp] lemma elim_comp_inl_inr {α β γ : Sort*} (f : α ⊕ β → γ) :
sum.elim (f ∘ inl) (f ∘ inr) = f :=
funext $ λ x, sum.cases_on x (λ _, rfl) (λ _, rfl)
open function (update update_eq_iff update_comp_eq_of_injective update_comp_eq_of_forall_ne)
@[simp] lemma update_elim_inl [decidable_eq α] [decidable_eq (α ⊕ β)] {f : α → γ} {g : β → γ}
{i : α} {x : γ} :
update (sum.elim f g) (inl i) x = sum.elim (update f i x) g :=
update_eq_iff.2 ⟨by simp, by simp { contextual := tt }⟩
@[simp] lemma update_elim_inr [decidable_eq β] [decidable_eq (α ⊕ β)] {f : α → γ} {g : β → γ}
{i : β} {x : γ} :
update (sum.elim f g) (inr i) x = sum.elim f (update g i x) :=
update_eq_iff.2 ⟨by simp, by simp { contextual := tt }⟩
@[simp] lemma update_inl_comp_inl [decidable_eq α] [decidable_eq (α ⊕ β)] {f : α ⊕ β → γ} {i : α}
{x : γ} :
update f (inl i) x ∘ inl = update (f ∘ inl) i x :=
update_comp_eq_of_injective _ inl_injective _ _
@[simp] lemma update_inl_apply_inl [decidable_eq α] [decidable_eq (α ⊕ β)] {f : α ⊕ β → γ}
{i j : α} {x : γ} :
update f (inl i) x (inl j) = update (f ∘ inl) i x j :=
by rw ← update_inl_comp_inl
@[simp] lemma update_inl_comp_inr [decidable_eq (α ⊕ β)] {f : α ⊕ β → γ} {i : α} {x : γ} :
update f (inl i) x ∘ inr = f ∘ inr :=
update_comp_eq_of_forall_ne _ _ $ λ _, inr_ne_inl
@[simp] lemma update_inl_apply_inr [decidable_eq (α ⊕ β)] {f : α ⊕ β → γ} {i : α} {j : β} {x : γ} :
update f (inl i) x (inr j) = f (inr j) :=
function.update_noteq inr_ne_inl _ _
@[simp] lemma update_inr_comp_inl [decidable_eq (α ⊕ β)] {f : α ⊕ β → γ} {i : β} {x : γ} :
update f (inr i) x ∘ inl = f ∘ inl :=
update_comp_eq_of_forall_ne _ _ $ λ _, inl_ne_inr
@[simp] lemma update_inr_apply_inl [decidable_eq (α ⊕ β)] {f : α ⊕ β → γ} {i : α} {j : β} {x : γ} :
update f (inr j) x (inl i) = f (inl i) :=
function.update_noteq inl_ne_inr _ _
@[simp] lemma update_inr_comp_inr [decidable_eq β] [decidable_eq (α ⊕ β)] {f : α ⊕ β → γ} {i : β}
{x : γ} :
update f (inr i) x ∘ inr = update (f ∘ inr) i x :=
update_comp_eq_of_injective _ inr_injective _ _
@[simp] lemma update_inr_apply_inr [decidable_eq β] [decidable_eq (α ⊕ β)] {f : α ⊕ β → γ}
{i j : β} {x : γ} :
update f (inr i) x (inr j) = update (f ∘ inr) i x j :=
by rw ← update_inr_comp_inr
/-- Swap the factors of a sum type -/
@[simp] def swap : α ⊕ β → β ⊕ α
| (inl a) := inr a
| (inr b) := inl b
@[simp] lemma swap_swap (x : α ⊕ β) : swap (swap x) = x := by cases x; refl
@[simp] lemma swap_swap_eq : swap ∘ swap = @id (α ⊕ β) := funext $ swap_swap
@[simp] lemma swap_left_inverse : function.left_inverse (@swap α β) swap := swap_swap
@[simp] lemma swap_right_inverse : function.right_inverse (@swap α β) swap := swap_swap
section lift_rel
/-- Lifts pointwise two relations between `α` and `γ` and between `β` and `δ` to a relation between
`α ⊕ β` and `γ ⊕ δ`. -/
inductive lift_rel (r : α → γ → Prop) (s : β → δ → Prop) : α ⊕ β → γ ⊕ δ → Prop
| inl {a c} : r a c → lift_rel (inl a) (inl c)
| inr {b d} : s b d → lift_rel (inr b) (inr d)
attribute [protected] lift_rel.inl lift_rel.inr
variables {r r₁ r₂ : α → γ → Prop} {s s₁ s₂ : β → δ → Prop} {a : α} {b : β} {c : γ} {d : δ}
{x : α ⊕ β} {y : γ ⊕ δ}
@[simp] lemma lift_rel_inl_inl : lift_rel r s (inl a) (inl c) ↔ r a c :=
⟨λ h, by { cases h, assumption }, lift_rel.inl⟩
@[simp] lemma not_lift_rel_inl_inr : ¬ lift_rel r s (inl a) (inr d) .
@[simp] lemma not_lift_rel_inr_inl : ¬ lift_rel r s (inr b) (inl c) .
@[simp] lemma lift_rel_inr_inr : lift_rel r s (inr b) (inr d) ↔ s b d :=
⟨λ h, by { cases h, assumption }, lift_rel.inr⟩
instance [Π a c, decidable (r a c)] [Π b d, decidable (s b d)] :
Π (ab : α ⊕ β) (cd : γ ⊕ δ), decidable (lift_rel r s ab cd)
| (inl a) (inl c) := decidable_of_iff' _ lift_rel_inl_inl
| (inl a) (inr d) := decidable.is_false not_lift_rel_inl_inr
| (inr b) (inl c) := decidable.is_false not_lift_rel_inr_inl
| (inr b) (inr d) := decidable_of_iff' _ lift_rel_inr_inr
lemma lift_rel.mono (hr : ∀ a b, r₁ a b → r₂ a b) (hs : ∀ a b, s₁ a b → s₂ a b)
(h : lift_rel r₁ s₁ x y) :
lift_rel r₂ s₂ x y :=
by { cases h, exacts [lift_rel.inl (hr _ _ ‹_›), lift_rel.inr (hs _ _ ‹_›)] }
lemma lift_rel.mono_left (hr : ∀ a b, r₁ a b → r₂ a b) (h : lift_rel r₁ s x y) :
lift_rel r₂ s x y :=
h.mono hr $ λ _ _, id
lemma lift_rel.mono_right (hs : ∀ a b, s₁ a b → s₂ a b) (h : lift_rel r s₁ x y) :
lift_rel r s₂ x y :=
h.mono (λ _ _, id) hs
protected lemma lift_rel.swap (h : lift_rel r s x y) : lift_rel s r x.swap y.swap :=
by { cases h, exacts [lift_rel.inr ‹_›, lift_rel.inl ‹_›] }
@[simp] lemma lift_rel_swap_iff : lift_rel s r x.swap y.swap ↔ lift_rel r s x y :=
⟨λ h, by { rw [←swap_swap x, ←swap_swap y], exact h.swap }, lift_rel.swap⟩
end lift_rel
section lex
/-- Lexicographic order for sum. Sort all the `inl a` before the `inr b`, otherwise use the
respective order on `α` or `β`. -/
inductive lex (r : α → α → Prop) (s : β → β → Prop) : α ⊕ β → α ⊕ β → Prop
| inl {a₁ a₂} (h : r a₁ a₂) : lex (inl a₁) (inl a₂)
| inr {b₁ b₂} (h : s b₁ b₂) : lex (inr b₁) (inr b₂)
| sep (a b) : lex (inl a) (inr b)
attribute [protected] sum.lex.inl sum.lex.inr
attribute [simp] lex.sep
variables {r r₁ r₂ : α → α → Prop} {s s₁ s₂ : β → β → Prop} {a a₁ a₂ : α} {b b₁ b₂ : β}
{x y : α ⊕ β}
@[simp] lemma lex_inl_inl : lex r s (inl a₁) (inl a₂) ↔ r a₁ a₂ :=
⟨λ h, by { cases h, assumption }, lex.inl⟩
@[simp] lemma lex_inr_inr : lex r s (inr b₁) (inr b₂) ↔ s b₁ b₂ :=
⟨λ h, by { cases h, assumption }, lex.inr⟩
@[simp] lemma lex_inr_inl : ¬ lex r s (inr b) (inl a) .
instance [decidable_rel r] [decidable_rel s] : decidable_rel (lex r s)
| (inl a) (inl c) := decidable_of_iff' _ lex_inl_inl
| (inl a) (inr d) := decidable.is_true (lex.sep _ _)
| (inr b) (inl c) := decidable.is_false lex_inr_inl
| (inr b) (inr d) := decidable_of_iff' _ lex_inr_inr
protected lemma lift_rel.lex {a b : α ⊕ β} (h : lift_rel r s a b) : lex r s a b :=
by { cases h, exacts [lex.inl ‹_›, lex.inr ‹_›] }
lemma lex.mono (hr : ∀ a b, r₁ a b → r₂ a b) (hs : ∀ a b, s₁ a b → s₂ a b) (h : lex r₁ s₁ x y) :
lex r₂ s₂ x y :=
by { cases h, exacts [lex.inl (hr _ _ ‹_›), lex.inr (hs _ _ ‹_›), lex.sep _ _] }
lemma lex.mono_left (hr : ∀ a b, r₁ a b → r₂ a b) (h : lex r₁ s x y) : lex r₂ s x y :=
h.mono hr $ λ _ _, id
lemma lex.mono_right (hs : ∀ a b, s₁ a b → s₂ a b) (h : lex r s₁ x y) : lex r s₂ x y :=
h.mono (λ _ _, id) hs
lemma lex_acc_inl {a} (aca : acc r a) : acc (lex r s) (inl a) :=
begin
induction aca with a H IH,
constructor, intros y h,
cases h with a' _ h',
exact IH _ h'
end
lemma lex_acc_inr (aca : ∀ a, acc (lex r s) (inl a)) {b} (acb : acc s b) : acc (lex r s) (inr b) :=
begin
induction acb with b H IH,
constructor, intros y h,
cases h with _ _ _ b' _ h' a,
{ exact IH _ h' },
{ exact aca _ }
end
lemma lex_wf (ha : well_founded r) (hb : well_founded s) : well_founded (lex r s) :=
have aca : ∀ a, acc (lex r s) (inl a), from λ a, lex_acc_inl (ha.apply a),
⟨λ x, sum.rec_on x aca (λ b, lex_acc_inr aca (hb.apply b))⟩
end lex
end sum
namespace function
open sum
lemma injective.sum_elim {f : α → γ} {g : β → γ}
(hf : injective f) (hg : injective g) (hfg : ∀ a b, f a ≠ g b) :
injective (sum.elim f g)
| (inl x) (inl y) h := congr_arg inl $ hf h
| (inl x) (inr y) h := (hfg x y h).elim
| (inr x) (inl y) h := (hfg y x h.symm).elim
| (inr x) (inr y) h := congr_arg inr $ hg h
lemma injective.sum_map {f : α → β} {g : α' → β'} (hf : injective f) (hg : injective g) :
injective (sum.map f g)
| (inl x) (inl y) h := congr_arg inl $ hf $ inl.inj h
| (inr x) (inr y) h := congr_arg inr $ hg $ inr.inj h
lemma surjective.sum_map {f : α → β} {g : α' → β'} (hf : surjective f) (hg : surjective g) :
surjective (sum.map f g)
| (inl y) := let ⟨x, hx⟩ := hf y in ⟨inl x, congr_arg inl hx⟩
| (inr y) := let ⟨x, hx⟩ := hg y in ⟨inr x, congr_arg inr hx⟩
end function
|
417a5cd3370279f82aae1cea376c235a67fe8553 | b00eb947a9c4141624aa8919e94ce6dcd249ed70 | /stage0/src/Lean/Meta/Basic.lean | cd6a7fc920c04edc452492beafd2ffb19963b5bf | [
"Apache-2.0"
] | permissive | gebner/lean4-old | a4129a041af2d4d12afb3a8d4deedabde727719b | ee51cdfaf63ee313c914d83264f91f414a0e3b6e | refs/heads/master | 1,683,628,606,745 | 1,622,651,300,000 | 1,622,654,405,000 | 142,608,821 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 46,863 | 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.Data.LOption
import Lean.Environment
import Lean.Class
import Lean.ReducibilityAttrs
import Lean.Util.Trace
import Lean.Util.RecDepth
import Lean.Util.PPExt
import Lean.Util.OccursCheck
import Lean.Util.MonadBacktrack
import Lean.Compiler.InlineAttrs
import Lean.Meta.TransparencyMode
import Lean.Meta.DiscrTreeTypes
import Lean.Eval
import Lean.CoreM
/-
This module provides four (mutually dependent) goodies that are needed for building the elaborator and tactic frameworks.
1- Weak head normal form computation with support for metavariables and transparency modes.
2- Definitionally equality checking with support for metavariables (aka unification modulo definitional equality).
3- Type inference.
4- Type class resolution.
They are packed into the MetaM monad.
-/
namespace Lean.Meta
builtin_initialize isDefEqStuckExceptionId : InternalExceptionId ← registerInternalExceptionId `isDefEqStuck
structure Config where
foApprox : Bool := false
ctxApprox : Bool := false
quasiPatternApprox : Bool := false
/- When `constApprox` is set to true,
we solve `?m t =?= c` using
`?m := fun _ => c`
when `?m t` is not a higher-order pattern and `c` is not an application as -/
constApprox : Bool := false
/-
When the following flag is set,
`isDefEq` throws the exeption `Exeption.isDefEqStuck`
whenever it encounters a constraint `?m ... =?= t` where
`?m` is read only.
This feature is useful for type class resolution where
we may want to notify the caller that the TC problem may be solveable
later after it assigns `?m`. -/
isDefEqStuckEx : Bool := false
transparency : TransparencyMode := TransparencyMode.default
/- If zetaNonDep == false, then non dependent let-decls are not zeta expanded. -/
zetaNonDep : Bool := true
/- When `trackZeta == true`, we store zetaFVarIds all free variables that have been zeta-expanded. -/
trackZeta : Bool := false
unificationHints : Bool := true
structure ParamInfo where
implicit : Bool := false
instImplicit : Bool := false
hasFwdDeps : Bool := false
backDeps : Array Nat := #[]
deriving Inhabited
def ParamInfo.isExplicit (p : ParamInfo) : Bool :=
!p.implicit && !p.instImplicit
structure FunInfo where
paramInfo : Array ParamInfo := #[]
resultDeps : Array Nat := #[]
structure InfoCacheKey where
transparency : TransparencyMode
expr : Expr
nargs? : Option Nat
deriving Inhabited, BEq
namespace InfoCacheKey
instance : Hashable InfoCacheKey :=
⟨fun ⟨transparency, expr, nargs⟩ => mixHash (hash transparency) <| mixHash (hash expr) (hash nargs)⟩
end InfoCacheKey
open Std (PersistentArray PersistentHashMap)
abbrev SynthInstanceCache := PersistentHashMap Expr (Option Expr)
abbrev InferTypeCache := PersistentExprStructMap Expr
abbrev FunInfoCache := PersistentHashMap InfoCacheKey FunInfo
abbrev WhnfCache := PersistentExprStructMap Expr
structure Cache where
inferType : InferTypeCache := {}
funInfo : FunInfoCache := {}
synthInstance : SynthInstanceCache := {}
whnfDefault : WhnfCache := {} -- cache for closed terms and `TransparencyMode.default`
whnfAll : WhnfCache := {} -- cache for closed terms and `TransparencyMode.all`
deriving Inhabited
/--
"Context" for a postponed universe constraint.
`lhs` and `rhs` are the surrounding `isDefEq` call when the postponed constraint was created.
-/
structure DefEqContext where
lhs : Expr
rhs : Expr
lctx : LocalContext
localInstances : LocalInstances
/--
Auxiliary structure for representing postponed universe constraints.
Remark: the fields `ref` and `rootDefEq?` are used for error message generation only.
Remark: we may consider improving the error message generation in the future.
-/
structure PostponedEntry where
ref : Syntax -- We save the `ref` at entry creation time
lhs : Level
rhs : Level
ctx? : Option DefEqContext -- Context for the surrounding `isDefEq` call when entry was created
deriving Inhabited
structure State where
mctx : MetavarContext := {}
cache : Cache := {}
/- When `trackZeta == true`, then any let-decl free variable that is zeta expansion performed by `MetaM` is stored in `zetaFVarIds`. -/
zetaFVarIds : NameSet := {}
postponed : PersistentArray PostponedEntry := {}
deriving Inhabited
structure SavedState where
core : Core.State
meta : State
deriving Inhabited
structure Context where
config : Config := {}
lctx : LocalContext := {}
localInstances : LocalInstances := #[]
/-- Not `none` when inside of an `isDefEq` test. See `PostponedEntry`. -/
defEqCtx? : Option DefEqContext := none
abbrev MetaM := ReaderT Context $ StateRefT State CoreM
instance : Inhabited (MetaM α) where
default := fun _ _ => arbitrary
instance : MonadLCtx MetaM where
getLCtx := return (← read).lctx
instance : MonadMCtx MetaM where
getMCtx := return (← get).mctx
modifyMCtx f := modify fun s => { s with mctx := f s.mctx }
instance : AddMessageContext MetaM where
addMessageContext := addMessageContextFull
protected def saveState : MetaM SavedState :=
return { core := (← getThe Core.State), meta := (← get) }
/-- Restore backtrackable parts of the state. -/
def SavedState.restore (b : SavedState) : MetaM Unit := do
Core.restore b.core
modify fun s => { s with mctx := b.meta.mctx, zetaFVarIds := b.meta.zetaFVarIds, postponed := b.meta.postponed }
instance : MonadBacktrack SavedState MetaM where
saveState := Meta.saveState
restoreState s := s.restore
@[inline] def MetaM.run (x : MetaM α) (ctx : Context := {}) (s : State := {}) : CoreM (α × State) :=
x ctx |>.run s
@[inline] def MetaM.run' (x : MetaM α) (ctx : Context := {}) (s : State := {}) : CoreM α :=
Prod.fst <$> x.run ctx s
@[inline] def MetaM.toIO (x : MetaM α) (ctxCore : Core.Context) (sCore : Core.State) (ctx : Context := {}) (s : State := {}) : IO (α × Core.State × State) := do
let ((a, s), sCore) ← (x.run ctx s).toIO ctxCore sCore
pure (a, sCore, s)
instance [MetaEval α] : MetaEval (MetaM α) :=
⟨fun env opts x _ => MetaEval.eval env opts x.run' true⟩
protected def throwIsDefEqStuck : MetaM α :=
throw <| Exception.internal isDefEqStuckExceptionId
builtin_initialize
registerTraceClass `Meta
registerTraceClass `Meta.debug
@[inline] def liftMetaM [MonadLiftT MetaM m] (x : MetaM α) : m α :=
liftM x
@[inline] def mapMetaM [MonadControlT MetaM m] [Monad m] (f : forall {α}, MetaM α → MetaM α) {α} (x : m α) : m α :=
controlAt MetaM fun runInBase => f <| runInBase x
@[inline] def map1MetaM [MonadControlT MetaM m] [Monad m] (f : forall {α}, (β → MetaM α) → MetaM α) {α} (k : β → m α) : m α :=
controlAt MetaM fun runInBase => f fun b => runInBase <| k b
@[inline] def map2MetaM [MonadControlT MetaM m] [Monad m] (f : forall {α}, (β → γ → MetaM α) → MetaM α) {α} (k : β → γ → m α) : m α :=
controlAt MetaM fun runInBase => f fun b c => runInBase <| k b c
section Methods
variable [MonadControlT MetaM n] [Monad n]
@[inline] def modifyCache (f : Cache → Cache) : MetaM Unit :=
modify fun ⟨mctx, cache, zetaFVarIds, postponed⟩ => ⟨mctx, f cache, zetaFVarIds, postponed⟩
@[inline] def modifyInferTypeCache (f : InferTypeCache → InferTypeCache) : MetaM Unit :=
modifyCache fun ⟨ic, c1, c2, c3, c4⟩ => ⟨f ic, c1, c2, c3, c4⟩
def getLocalInstances : MetaM LocalInstances :=
return (← read).localInstances
def getConfig : MetaM Config :=
return (← read).config
def setMCtx (mctx : MetavarContext) : MetaM Unit :=
modify fun s => { s with mctx := mctx }
def resetZetaFVarIds : MetaM Unit :=
modify fun s => { s with zetaFVarIds := {} }
def getZetaFVarIds : MetaM NameSet :=
return (← get).zetaFVarIds
def getPostponed : MetaM (PersistentArray PostponedEntry) :=
return (← get).postponed
def setPostponed (postponed : PersistentArray PostponedEntry) : MetaM Unit :=
modify fun s => { s with postponed := postponed }
@[inline] def modifyPostponed (f : PersistentArray PostponedEntry → PersistentArray PostponedEntry) : MetaM Unit :=
modify fun s => { s with postponed := f s.postponed }
builtin_initialize whnfRef : IO.Ref (Expr → MetaM Expr) ← IO.mkRef fun _ => throwError "whnf implementation was not set"
builtin_initialize inferTypeRef : IO.Ref (Expr → MetaM Expr) ← IO.mkRef fun _ => throwError "inferType implementation was not set"
builtin_initialize isExprDefEqAuxRef : IO.Ref (Expr → Expr → MetaM Bool) ← IO.mkRef fun _ _ => throwError "isDefEq implementation was not set"
builtin_initialize synthPendingRef : IO.Ref (MVarId → MetaM Bool) ← IO.mkRef fun _ => pure false
def whnf (e : Expr) : MetaM Expr :=
withIncRecDepth do (← whnfRef.get) e
def whnfForall (e : Expr) : MetaM Expr := do
let e' ← whnf e
if e'.isForall then pure e' else pure e
def inferType (e : Expr) : MetaM Expr :=
withIncRecDepth do (← inferTypeRef.get) e
protected def isExprDefEqAux (t s : Expr) : MetaM Bool :=
withIncRecDepth do (← isExprDefEqAuxRef.get) t s
protected def synthPending (mvarId : MVarId) : MetaM Bool :=
withIncRecDepth do (← synthPendingRef.get) mvarId
-- withIncRecDepth for a monad `n` such that `[MonadControlT MetaM n]`
protected def withIncRecDepth (x : n α) : n α :=
mapMetaM (withIncRecDepth (m := MetaM)) x
private def mkFreshExprMVarAtCore
(mvarId : MVarId) (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (kind : MetavarKind) (userName : Name) (numScopeArgs : Nat) : MetaM Expr := do
modifyMCtx fun mctx => mctx.addExprMVarDecl mvarId userName lctx localInsts type kind numScopeArgs;
return mkMVar mvarId
def mkFreshExprMVarAt
(lctx : LocalContext) (localInsts : LocalInstances) (type : Expr)
(kind : MetavarKind := MetavarKind.natural) (userName : Name := Name.anonymous) (numScopeArgs : Nat := 0)
: MetaM Expr := do
let mvarId ← mkFreshId
mkFreshExprMVarAtCore mvarId lctx localInsts type kind userName numScopeArgs
def mkFreshLevelMVar : MetaM Level := do
let mvarId ← mkFreshId
modifyMCtx fun mctx => mctx.addLevelMVarDecl mvarId;
return mkLevelMVar mvarId
private def mkFreshExprMVarCore (type : Expr) (kind : MetavarKind) (userName : Name) : MetaM Expr := do
let lctx ← getLCtx
let localInsts ← getLocalInstances
mkFreshExprMVarAt lctx localInsts type kind userName
private def mkFreshExprMVarImpl (type? : Option Expr) (kind : MetavarKind) (userName : Name) : MetaM Expr :=
match type? with
| some type => mkFreshExprMVarCore type kind userName
| none => do
let u ← mkFreshLevelMVar
let type ← mkFreshExprMVarCore (mkSort u) MetavarKind.natural Name.anonymous
mkFreshExprMVarCore type kind userName
def mkFreshExprMVar (type? : Option Expr) (kind := MetavarKind.natural) (userName := Name.anonymous) : MetaM Expr :=
mkFreshExprMVarImpl type? kind userName
def mkFreshTypeMVar (kind := MetavarKind.natural) (userName := Name.anonymous) : MetaM Expr := do
let u ← mkFreshLevelMVar
mkFreshExprMVar (mkSort u) kind userName
/- Low-level version of `MkFreshExprMVar` which allows users to create/reserve a `mvarId` using `mkFreshId`, and then later create
the metavar using this method. -/
private def mkFreshExprMVarWithIdCore (mvarId : MVarId) (type : Expr)
(kind : MetavarKind := MetavarKind.natural) (userName : Name := Name.anonymous) (numScopeArgs : Nat := 0)
: MetaM Expr := do
let lctx ← getLCtx
let localInsts ← getLocalInstances
mkFreshExprMVarAtCore mvarId lctx localInsts type kind userName numScopeArgs
def mkFreshExprMVarWithId (mvarId : MVarId) (type? : Option Expr := none) (kind : MetavarKind := MetavarKind.natural) (userName := Name.anonymous) : MetaM Expr :=
match type? with
| some type => mkFreshExprMVarWithIdCore mvarId type kind userName
| none => do
let u ← mkFreshLevelMVar
let type ← mkFreshExprMVar (mkSort u)
mkFreshExprMVarWithIdCore mvarId type kind userName
def mkFreshLevelMVars (num : Nat) : MetaM (List Level) :=
num.foldM (init := []) fun _ us =>
return (← mkFreshLevelMVar)::us
def mkFreshLevelMVarsFor (info : ConstantInfo) : MetaM (List Level) :=
mkFreshLevelMVars info.numLevelParams
def mkConstWithFreshMVarLevels (declName : Name) : MetaM Expr := do
let info ← getConstInfo declName
return mkConst declName (← mkFreshLevelMVarsFor info)
def getTransparency : MetaM TransparencyMode :=
return (← getConfig).transparency
def shouldReduceAll : MetaM Bool :=
return (← getTransparency) == TransparencyMode.all
def shouldReduceReducibleOnly : MetaM Bool :=
return (← getTransparency) == TransparencyMode.reducible
def getMVarDecl (mvarId : MVarId) : MetaM MetavarDecl := do
let mctx ← getMCtx
match mctx.findDecl? mvarId with
| some d => pure d
| none => throwError "unknown metavariable '?{mvarId}'"
def setMVarKind (mvarId : MVarId) (kind : MetavarKind) : MetaM Unit :=
modifyMCtx fun mctx => mctx.setMVarKind mvarId kind
/- Update the type of the given metavariable. This function assumes the new type is
definitionally equal to the current one -/
def setMVarType (mvarId : MVarId) (type : Expr) : MetaM Unit := do
modifyMCtx fun mctx => mctx.setMVarType mvarId type
def isReadOnlyExprMVar (mvarId : MVarId) : MetaM Bool := do
let mvarDecl ← getMVarDecl mvarId
let mctx ← getMCtx
return mvarDecl.depth != mctx.depth
def isReadOnlyOrSyntheticOpaqueExprMVar (mvarId : MVarId) : MetaM Bool := do
let mvarDecl ← getMVarDecl mvarId
match mvarDecl.kind with
| MetavarKind.syntheticOpaque => pure true
| _ =>
let mctx ← getMCtx
return mvarDecl.depth != mctx.depth
def isReadOnlyLevelMVar (mvarId : MVarId) : MetaM Bool := do
let mctx ← getMCtx
match mctx.findLevelDepth? mvarId with
| some depth => return depth != mctx.depth
| _ => throwError "unknown universe metavariable '?{mvarId}'"
def renameMVar (mvarId : MVarId) (newUserName : Name) : MetaM Unit :=
modifyMCtx fun mctx => mctx.renameMVar mvarId newUserName
def isExprMVarAssigned (mvarId : MVarId) : MetaM Bool :=
return (← getMCtx).isExprAssigned mvarId
def getExprMVarAssignment? (mvarId : MVarId) : MetaM (Option Expr) :=
return (← getMCtx).getExprAssignment? mvarId
/-- Return true if `e` contains `mvarId` directly or indirectly -/
def occursCheck (mvarId : MVarId) (e : Expr) : MetaM Bool :=
return (← getMCtx).occursCheck mvarId e
def assignExprMVar (mvarId : MVarId) (val : Expr) : MetaM Unit :=
modifyMCtx fun mctx => mctx.assignExpr mvarId val
def isDelayedAssigned (mvarId : MVarId) : MetaM Bool :=
return (← getMCtx).isDelayedAssigned mvarId
def getDelayedAssignment? (mvarId : MVarId) : MetaM (Option DelayedMetavarAssignment) :=
return (← getMCtx).getDelayedAssignment? mvarId
def hasAssignableMVar (e : Expr) : MetaM Bool :=
return (← getMCtx).hasAssignableMVar e
def throwUnknownFVar (fvarId : FVarId) : MetaM α :=
throwError "unknown free variable '{mkFVar fvarId}'"
def findLocalDecl? (fvarId : FVarId) : MetaM (Option LocalDecl) :=
return (← getLCtx).find? fvarId
def getLocalDecl (fvarId : FVarId) : MetaM LocalDecl := do
match (← getLCtx).find? fvarId with
| some d => pure d
| none => throwUnknownFVar fvarId
def getFVarLocalDecl (fvar : Expr) : MetaM LocalDecl :=
getLocalDecl fvar.fvarId!
def getLocalDeclFromUserName (userName : Name) : MetaM LocalDecl := do
match (← getLCtx).findFromUserName? userName with
| some d => pure d
| none => throwError "unknown local declaration '{userName}'"
def instantiateLevelMVars (u : Level) : MetaM Level :=
MetavarContext.instantiateLevelMVars u
def instantiateMVars (e : Expr) : MetaM Expr :=
(MetavarContext.instantiateExprMVars e).run
def instantiateLocalDeclMVars (localDecl : LocalDecl) : MetaM LocalDecl := do
match localDecl with
| LocalDecl.cdecl idx id n type bi =>
let type ← instantiateMVars type
return LocalDecl.cdecl idx id n type bi
| LocalDecl.ldecl idx id n type val nonDep =>
let type ← instantiateMVars type
let val ← instantiateMVars val
return LocalDecl.ldecl idx id n type val nonDep
@[inline] def liftMkBindingM (x : MetavarContext.MkBindingM α) : MetaM α := do
match x (← getLCtx) { mctx := (← getMCtx), ngen := (← getNGen) } with
| EStateM.Result.ok e newS => do
setNGen newS.ngen;
setMCtx newS.mctx;
pure e
| EStateM.Result.error (MetavarContext.MkBinding.Exception.revertFailure mctx lctx toRevert decl) newS => do
setMCtx newS.mctx;
setNGen newS.ngen;
throwError "failed to create binder due to failure when reverting variable dependencies"
def mkForallFVars (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) : MetaM Expr :=
if xs.isEmpty then pure e else liftMkBindingM <| MetavarContext.mkForall xs e usedOnly usedLetOnly
def mkLambdaFVars (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) : MetaM Expr :=
if xs.isEmpty then pure e else liftMkBindingM <| MetavarContext.mkLambda xs e usedOnly usedLetOnly
def mkLetFVars (xs : Array Expr) (e : Expr) (usedLetOnly := true) : MetaM Expr :=
mkLambdaFVars xs e (usedLetOnly := usedLetOnly)
def mkArrow (d b : Expr) : MetaM Expr := do
let n ← mkFreshUserName `x
return Lean.mkForall n BinderInfo.default d b
def elimMVarDeps (xs : Array Expr) (e : Expr) (preserveOrder : Bool := false) : MetaM Expr :=
if xs.isEmpty then pure e else liftMkBindingM <| MetavarContext.elimMVarDeps xs e preserveOrder
@[inline] def withConfig (f : Config → Config) : n α → n α :=
mapMetaM <| withReader (fun ctx => { ctx with config := f ctx.config })
@[inline] def withTrackingZeta (x : n α) : n α :=
withConfig (fun cfg => { cfg with trackZeta := true }) x
@[inline] def withTransparency (mode : TransparencyMode) : n α → n α :=
mapMetaM <| withConfig (fun config => { config with transparency := mode })
@[inline] def withDefault (x : n α) : n α :=
withTransparency TransparencyMode.default x
@[inline] def withReducible (x : n α) : n α :=
withTransparency TransparencyMode.reducible x
@[inline] def withReducibleAndInstances (x : n α) : n α :=
withTransparency TransparencyMode.instances x
@[inline] def withAtLeastTransparency (mode : TransparencyMode) (x : n α) : n α :=
withConfig
(fun config =>
let oldMode := config.transparency
let mode := if oldMode.lt mode then mode else oldMode
{ config with transparency := mode })
x
/-- Save cache, execute `x`, restore cache -/
@[inline] private def savingCacheImpl (x : MetaM α) : MetaM α := do
let s ← get
let savedCache := s.cache
try x finally modify fun s => { s with cache := savedCache }
@[inline] def savingCache : n α → n α :=
mapMetaM savingCacheImpl
def getTheoremInfo (info : ConstantInfo) : MetaM (Option ConstantInfo) := do
if (← shouldReduceAll) then
return some info
else
return none
private def getDefInfoTemp (info : ConstantInfo) : MetaM (Option ConstantInfo) := do
match (← getTransparency) with
| TransparencyMode.all => return some info
| TransparencyMode.default => return some info
| _ =>
if (← isReducible info.name) then
return some info
else
return none
/- Remark: we later define `getConst?` at `GetConst.lean` after we define `Instances.lean`.
This method is only used to implement `isClassQuickConst?`.
It is very similar to `getConst?`, but it returns none when `TransparencyMode.instances` and
`constName` is an instance. This difference should be irrelevant for `isClassQuickConst?`. -/
private def getConstTemp? (constName : Name) : MetaM (Option ConstantInfo) := do
let env ← getEnv
match env.find? constName with
| some (info@(ConstantInfo.thmInfo _)) => getTheoremInfo info
| some (info@(ConstantInfo.defnInfo _)) => getDefInfoTemp info
| some info => pure (some info)
| none => throwUnknownConstant constName
private def isClassQuickConst? (constName : Name) : MetaM (LOption Name) := do
let env ← getEnv
if isClass env constName then
pure (LOption.some constName)
else
match (← getConstTemp? constName) with
| some _ => pure LOption.undef
| none => pure LOption.none
private partial def isClassQuick? : Expr → MetaM (LOption Name)
| Expr.bvar .. => pure LOption.none
| Expr.lit .. => pure LOption.none
| Expr.fvar .. => pure LOption.none
| Expr.sort .. => pure LOption.none
| Expr.lam .. => pure LOption.none
| Expr.letE .. => pure LOption.undef
| Expr.proj .. => pure LOption.undef
| Expr.forallE _ _ b _ => isClassQuick? b
| Expr.mdata _ e _ => isClassQuick? e
| Expr.const n _ _ => isClassQuickConst? n
| Expr.mvar mvarId _ => do
match (← getExprMVarAssignment? mvarId) with
| some val => isClassQuick? val
| none => pure LOption.none
| Expr.app f _ _ =>
match f.getAppFn with
| Expr.const n .. => isClassQuickConst? n
| Expr.lam .. => pure LOption.undef
| _ => pure LOption.none
def saveAndResetSynthInstanceCache : MetaM SynthInstanceCache := do
let s ← get
let savedSythInstance := s.cache.synthInstance
modifyCache fun c => { c with synthInstance := {} }
pure savedSythInstance
def restoreSynthInstanceCache (cache : SynthInstanceCache) : MetaM Unit :=
modifyCache fun c => { c with synthInstance := cache }
@[inline] private def resettingSynthInstanceCacheImpl (x : MetaM α) : MetaM α := do
let savedSythInstance ← saveAndResetSynthInstanceCache
try x finally restoreSynthInstanceCache savedSythInstance
/-- Reset `synthInstance` cache, execute `x`, and restore cache -/
@[inline] def resettingSynthInstanceCache : n α → n α :=
mapMetaM resettingSynthInstanceCacheImpl
@[inline] def resettingSynthInstanceCacheWhen (b : Bool) (x : n α) : n α :=
if b then resettingSynthInstanceCache x else x
private def withNewLocalInstanceImp (className : Name) (fvar : Expr) (k : MetaM α) : MetaM α := do
let localDecl ← getFVarLocalDecl fvar
/- Recall that we use `auxDecl` binderInfo when compiling recursive declarations. -/
match localDecl.binderInfo with
| BinderInfo.auxDecl => k
| _ =>
resettingSynthInstanceCache <|
withReader
(fun ctx => { ctx with localInstances := ctx.localInstances.push { className := className, fvar := fvar } })
k
/-- Add entry `{ className := className, fvar := fvar }` to localInstances,
and then execute continuation `k`.
It resets the type class cache using `resettingSynthInstanceCache`. -/
def withNewLocalInstance (className : Name) (fvar : Expr) : n α → n α :=
mapMetaM <| withNewLocalInstanceImp className fvar
private def fvarsSizeLtMaxFVars (fvars : Array Expr) (maxFVars? : Option Nat) : Bool :=
match maxFVars? with
| some maxFVars => fvars.size < maxFVars
| none => true
mutual
/--
`withNewLocalInstances isClassExpensive fvars j k` updates the vector or local instances
using free variables `fvars[j] ... fvars.back`, and execute `k`.
- `isClassExpensive` is defined later.
- The type class chache is reset whenever a new local instance is found.
- `isClassExpensive` uses `whnf` which depends (indirectly) on the set of local instances.
Thus, each new local instance requires a new `resettingSynthInstanceCache`. -/
private partial def withNewLocalInstancesImp
(fvars : Array Expr) (i : Nat) (k : MetaM α) : MetaM α := do
if h : i < fvars.size then
let fvar := fvars.get ⟨i, h⟩
let decl ← getFVarLocalDecl fvar
match (← isClassQuick? decl.type) with
| LOption.none => withNewLocalInstancesImp fvars (i+1) k
| LOption.undef =>
match (← isClassExpensive? decl.type) with
| none => withNewLocalInstancesImp fvars (i+1) k
| some c => withNewLocalInstance c fvar <| withNewLocalInstancesImp fvars (i+1) k
| LOption.some c => withNewLocalInstance c fvar <| withNewLocalInstancesImp fvars (i+1) k
else
k
/--
`forallTelescopeAuxAux lctx fvars j type`
Remarks:
- `lctx` is the `MetaM` local context extended with declarations for `fvars`.
- `type` is the type we are computing the telescope for. It contains only
dangling bound variables in the range `[j, fvars.size)`
- if `reducing? == true` and `type` is not `forallE`, we use `whnf`.
- when `type` is not a `forallE` nor it can't be reduced to one, we
excute the continuation `k`.
Here is an example that demonstrates the `reducing?`.
Suppose we have
```
abbrev StateM s a := s -> Prod a s
```
Now, assume we are trying to build the telescope for
```
forall (x : Nat), StateM Int Bool
```
if `reducing == true`, the function executes `k #[(x : Nat) (s : Int)] Bool`.
if `reducing == false`, the function executes `k #[(x : Nat)] (StateM Int Bool)`
if `maxFVars?` is `some max`, then we interrupt the telescope construction
when `fvars.size == max`
-/
private partial def forallTelescopeReducingAuxAux
(reducing : Bool) (maxFVars? : Option Nat)
(type : Expr)
(k : Array Expr → Expr → MetaM α) : MetaM α := do
let rec process (lctx : LocalContext) (fvars : Array Expr) (j : Nat) (type : Expr) : MetaM α := do
match type with
| Expr.forallE n d b c =>
if fvarsSizeLtMaxFVars fvars maxFVars? then
let d := d.instantiateRevRange j fvars.size fvars
let fvarId ← mkFreshId
let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo
let fvar := mkFVar fvarId
let fvars := fvars.push fvar
process lctx fvars j b
else
let type := type.instantiateRevRange j fvars.size fvars;
withReader (fun ctx => { ctx with lctx := lctx }) do
withNewLocalInstancesImp fvars j do
k fvars type
| _ =>
let type := type.instantiateRevRange j fvars.size fvars;
withReader (fun ctx => { ctx with lctx := lctx }) do
withNewLocalInstancesImp fvars j do
if reducing && fvarsSizeLtMaxFVars fvars maxFVars? then
let newType ← whnf type
if newType.isForall then
process lctx fvars fvars.size newType
else
k fvars type
else
k fvars type
process (← getLCtx) #[] 0 type
private partial def forallTelescopeReducingAux (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → MetaM α) : MetaM α := do
match maxFVars? with
| some 0 => k #[] type
| _ => do
let newType ← whnf type
if newType.isForall then
forallTelescopeReducingAuxAux true maxFVars? newType k
else
k #[] type
private partial def isClassExpensive? : Expr → MetaM (Option Name)
| type => withReducible <| -- when testing whether a type is a type class, we only unfold reducible constants.
forallTelescopeReducingAux type none fun xs type => do
let env ← getEnv
match type.getAppFn with
| Expr.const c _ _ => do
if isClass env c then
return some c
else
-- make sure abbreviations are unfolded
match (← whnf type).getAppFn with
| Expr.const c _ _ => return if isClass env c then some c else none
| _ => return none
| _ => return none
private partial def isClassImp? (type : Expr) : MetaM (Option Name) := do
match (← isClassQuick? type) with
| LOption.none => pure none
| LOption.some c => pure (some c)
| LOption.undef => isClassExpensive? type
end
def isClass? (type : Expr) : MetaM (Option Name) :=
try isClassImp? type catch _ => pure none
private def withNewLocalInstancesImpAux (fvars : Array Expr) (j : Nat) : n α → n α :=
mapMetaM <| withNewLocalInstancesImp fvars j
partial def withNewLocalInstances (fvars : Array Expr) (j : Nat) : n α → n α :=
mapMetaM <| withNewLocalInstancesImpAux fvars j
@[inline] private def forallTelescopeImp (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α := do
forallTelescopeReducingAuxAux (reducing := false) (maxFVars? := none) type k
/--
Given `type` of the form `forall xs, A`, execute `k xs A`.
This combinator will declare local declarations, create free variables for them,
execute `k` with updated local context, and make sure the cache is restored after executing `k`. -/
def forallTelescope (type : Expr) (k : Array Expr → Expr → n α) : n α :=
map2MetaM (fun k => forallTelescopeImp type k) k
private def forallTelescopeReducingImp (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α :=
forallTelescopeReducingAux type (maxFVars? := none) k
/--
Similar to `forallTelescope`, but given `type` of the form `forall xs, A`,
it reduces `A` and continues bulding the telescope if it is a `forall`. -/
def forallTelescopeReducing (type : Expr) (k : Array Expr → Expr → n α) : n α :=
map2MetaM (fun k => forallTelescopeReducingImp type k) k
private def forallBoundedTelescopeImp (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → MetaM α) : MetaM α :=
forallTelescopeReducingAux type maxFVars? k
/--
Similar to `forallTelescopeReducing`, stops constructing the telescope when
it reaches size `maxFVars`. -/
def forallBoundedTelescope (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → n α) : n α :=
map2MetaM (fun k => forallBoundedTelescopeImp type maxFVars? k) k
/-- Similar to `forallTelescopeAuxAux` but for lambda and let expressions. -/
private partial def lambdaTelescopeAux
(k : Array Expr → Expr → MetaM α)
: Bool → LocalContext → Array Expr → Nat → Expr → MetaM α
| consumeLet, lctx, fvars, j, Expr.lam n d b c => do
let d := d.instantiateRevRange j fvars.size fvars
let fvarId ← mkFreshId
let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo
let fvar := mkFVar fvarId
lambdaTelescopeAux k consumeLet lctx (fvars.push fvar) j b
| true, lctx, fvars, j, Expr.letE n t v b _ => do
let t := t.instantiateRevRange j fvars.size fvars
let v := v.instantiateRevRange j fvars.size fvars
let fvarId ← mkFreshId
let lctx := lctx.mkLetDecl fvarId n t v
let fvar := mkFVar fvarId
lambdaTelescopeAux k true lctx (fvars.push fvar) j b
| _, lctx, fvars, j, e =>
let e := e.instantiateRevRange j fvars.size fvars;
withReader (fun ctx => { ctx with lctx := lctx }) do
withNewLocalInstancesImp fvars j do
k fvars e
private partial def lambdaTelescopeImp (e : Expr) (consumeLet : Bool) (k : Array Expr → Expr → MetaM α) : MetaM α := do
let rec process (consumeLet : Bool) (lctx : LocalContext) (fvars : Array Expr) (j : Nat) (e : Expr) : MetaM α := do
match consumeLet, e with
| _, Expr.lam n d b c =>
let d := d.instantiateRevRange j fvars.size fvars
let fvarId ← mkFreshId
let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo
let fvar := mkFVar fvarId
process consumeLet lctx (fvars.push fvar) j b
| true, Expr.letE n t v b _ => do
let t := t.instantiateRevRange j fvars.size fvars
let v := v.instantiateRevRange j fvars.size fvars
let fvarId ← mkFreshId
let lctx := lctx.mkLetDecl fvarId n t v
let fvar := mkFVar fvarId
process true lctx (fvars.push fvar) j b
| _, e =>
let e := e.instantiateRevRange j fvars.size fvars
withReader (fun ctx => { ctx with lctx := lctx }) do
withNewLocalInstancesImp fvars j do
k fvars e
process consumeLet (← getLCtx) #[] 0 e
/-- Similar to `forallTelescope` but for lambda and let expressions. -/
def lambdaLetTelescope (type : Expr) (k : Array Expr → Expr → n α) : n α :=
map2MetaM (fun k => lambdaTelescopeImp type true k) k
/-- Similar to `forallTelescope` but for lambda expressions. -/
def lambdaTelescope (type : Expr) (k : Array Expr → Expr → n α) : n α :=
map2MetaM (fun k => lambdaTelescopeImp type false k) k
/-- Return the parameter names for the givel global declaration. -/
def getParamNames (declName : Name) : MetaM (Array Name) := do
let cinfo ← getConstInfo declName
forallTelescopeReducing cinfo.type fun xs _ => do
xs.mapM fun x => do
let localDecl ← getLocalDecl x.fvarId!
pure localDecl.userName
-- `kind` specifies the metavariable kind for metavariables not corresponding to instance implicit `[ ... ]` arguments.
private partial def forallMetaTelescopeReducingAux
(e : Expr) (reducing : Bool) (maxMVars? : Option Nat) (kind : MetavarKind) : MetaM (Array Expr × Array BinderInfo × Expr) :=
let rec process (mvars : Array Expr) (bis : Array BinderInfo) (j : Nat) (type : Expr) : MetaM (Array Expr × Array BinderInfo × Expr) := do
match type with
| Expr.forallE n d b c =>
let cont : Unit → MetaM (Array Expr × Array BinderInfo × Expr) := fun _ => do
let d := d.instantiateRevRange j mvars.size mvars
let k := if c.binderInfo.isInstImplicit then MetavarKind.synthetic else kind
let mvar ← mkFreshExprMVar d k n
let mvars := mvars.push mvar
let bis := bis.push c.binderInfo
process mvars bis j b
match maxMVars? with
| none => cont ()
| some maxMVars =>
if mvars.size < maxMVars then
cont ()
else
let type := type.instantiateRevRange j mvars.size mvars;
pure (mvars, bis, type)
| _ =>
let type := type.instantiateRevRange j mvars.size mvars;
if reducing then do
let newType ← whnf type;
if newType.isForall then
process mvars bis mvars.size newType
else
pure (mvars, bis, type)
else
pure (mvars, bis, type)
process #[] #[] 0 e
/-- Similar to `forallTelescope`, but creates metavariables instead of free variables. -/
def forallMetaTelescope (e : Expr) (kind := MetavarKind.natural) : MetaM (Array Expr × Array BinderInfo × Expr) :=
forallMetaTelescopeReducingAux e (reducing := false) (maxMVars? := none) kind
/-- Similar to `forallTelescopeReducing`, but creates metavariables instead of free variables. -/
def forallMetaTelescopeReducing (e : Expr) (maxMVars? : Option Nat := none) (kind := MetavarKind.natural) : MetaM (Array Expr × Array BinderInfo × Expr) :=
forallMetaTelescopeReducingAux e (reducing := true) maxMVars? kind
/-- Similar to `forallMetaTelescopeReducingAux` but for lambda expressions. -/
partial def lambdaMetaTelescope (e : Expr) (maxMVars? : Option Nat := none) : MetaM (Array Expr × Array BinderInfo × Expr) :=
let rec process (mvars : Array Expr) (bis : Array BinderInfo) (j : Nat) (type : Expr) : MetaM (Array Expr × Array BinderInfo × Expr) := do
let finalize : Unit → MetaM (Array Expr × Array BinderInfo × Expr) := fun _ => do
let type := type.instantiateRevRange j mvars.size mvars
pure (mvars, bis, type)
let cont : Unit → MetaM (Array Expr × Array BinderInfo × Expr) := fun _ => do
match type with
| Expr.lam n d b c =>
let d := d.instantiateRevRange j mvars.size mvars
let mvar ← mkFreshExprMVar d
let mvars := mvars.push mvar
let bis := bis.push c.binderInfo
process mvars bis j b
| _ => finalize ()
match maxMVars? with
| none => cont ()
| some maxMVars =>
if mvars.size < maxMVars then
cont ()
else
finalize ()
process #[] #[] 0 e
private def withNewFVar (fvar fvarType : Expr) (k : Expr → MetaM α) : MetaM α := do
match (← isClass? fvarType) with
| none => k fvar
| some c => withNewLocalInstance c fvar <| k fvar
private def withLocalDeclImp (n : Name) (bi : BinderInfo) (type : Expr) (k : Expr → MetaM α) : MetaM α := do
let fvarId ← mkFreshId
let ctx ← read
let lctx := ctx.lctx.mkLocalDecl fvarId n type bi
let fvar := mkFVar fvarId
withReader (fun ctx => { ctx with lctx := lctx }) do
withNewFVar fvar type k
def withLocalDecl (name : Name) (bi : BinderInfo) (type : Expr) (k : Expr → n α) : n α :=
map1MetaM (fun k => withLocalDeclImp name bi type k) k
def withLocalDeclD (name : Name) (type : Expr) (k : Expr → n α) : n α :=
withLocalDecl name BinderInfo.default type k
partial def withLocalDecls
[Inhabited α]
(declInfos : Array (Name × BinderInfo × (Array Expr → n Expr)))
(k : (xs : Array Expr) → n α)
: n α :=
let rec loop
[Inhabited α]
(acc : Array Expr) : n α := do
if acc.size < declInfos.size then
let (name, bi, typeCtor) := declInfos[acc.size]
withLocalDecl name bi (←typeCtor acc) fun x => loop (acc.push x)
else k acc
loop #[]
def withLocalDeclsD
[Inhabited α]
(declInfos : Array (Name × (Array Expr → n Expr)))
(k : (xs : Array Expr) → n α)
: n α :=
withLocalDecls
(declInfos.map (fun (name, typeCtor) => (name, BinderInfo.default, typeCtor))) k
private def withNewBinderInfosImp (bs : Array (FVarId × BinderInfo)) (k : MetaM α) : MetaM α := do
let lctx := bs.foldl (init := (← getLCtx)) fun lctx (fvarId, bi) =>
lctx.setBinderInfo fvarId bi
withReader (fun ctx => { ctx with lctx := lctx }) k
def withNewBinderInfos (bs : Array (FVarId × BinderInfo)) (k : n α) : n α :=
mapMetaM (fun k => withNewBinderInfosImp bs k) k
private def withLetDeclImp (n : Name) (type : Expr) (val : Expr) (k : Expr → MetaM α) : MetaM α := do
let fvarId ← mkFreshId
let ctx ← read
let lctx := ctx.lctx.mkLetDecl fvarId n type val
let fvar := mkFVar fvarId
withReader (fun ctx => { ctx with lctx := lctx }) do
withNewFVar fvar type k
def withLetDecl (name : Name) (type : Expr) (val : Expr) (k : Expr → n α) : n α :=
map1MetaM (fun k => withLetDeclImp name type val k) k
private def withExistingLocalDeclsImp (decls : List LocalDecl) (k : MetaM α) : MetaM α := do
let ctx ← read
let numLocalInstances := ctx.localInstances.size
let lctx := decls.foldl (fun (lctx : LocalContext) decl => lctx.addDecl decl) ctx.lctx
withReader (fun ctx => { ctx with lctx := lctx }) do
let newLocalInsts ← decls.foldlM
(fun (newlocalInsts : Array LocalInstance) (decl : LocalDecl) => (do {
match (← isClass? decl.type) with
| none => pure newlocalInsts
| some c => pure <| newlocalInsts.push { className := c, fvar := decl.toExpr } } : MetaM _))
ctx.localInstances;
if newLocalInsts.size == numLocalInstances then
k
else
resettingSynthInstanceCache <| withReader (fun ctx => { ctx with localInstances := newLocalInsts }) k
def withExistingLocalDecls (decls : List LocalDecl) : n α → n α :=
mapMetaM <| withExistingLocalDeclsImp decls
private def withNewMCtxDepthImp (x : MetaM α) : MetaM α := do
let saved ← get
modify fun s => { s with mctx := s.mctx.incDepth, postponed := {} }
try
x
finally
modify fun s => { s with mctx := saved.mctx, postponed := saved.postponed }
/--
Save cache and `MetavarContext`, bump the `MetavarContext` depth, execute `x`,
and restore saved data. -/
def withNewMCtxDepth : n α → n α :=
mapMetaM withNewMCtxDepthImp
private def withLocalContextImp (lctx : LocalContext) (localInsts : LocalInstances) (x : MetaM α) : MetaM α := do
let localInstsCurr ← getLocalInstances
withReader (fun ctx => { ctx with lctx := lctx, localInstances := localInsts }) do
if localInsts == localInstsCurr then
x
else
resettingSynthInstanceCache x
def withLCtx (lctx : LocalContext) (localInsts : LocalInstances) : n α → n α :=
mapMetaM <| withLocalContextImp lctx localInsts
private def withMVarContextImp (mvarId : MVarId) (x : MetaM α) : MetaM α := do
let mvarDecl ← getMVarDecl mvarId
withLocalContextImp mvarDecl.lctx mvarDecl.localInstances x
/--
Execute `x` using the given metavariable `LocalContext` and `LocalInstances`.
The type class resolution cache is flushed when executing `x` if its `LocalInstances` are
different from the current ones. -/
def withMVarContext (mvarId : MVarId) : n α → n α :=
mapMetaM <| withMVarContextImp mvarId
private def withMCtxImp (mctx : MetavarContext) (x : MetaM α) : MetaM α := do
let mctx' ← getMCtx
setMCtx mctx
try x finally setMCtx mctx'
def withMCtx (mctx : MetavarContext) : n α → n α :=
mapMetaM <| withMCtxImp mctx
@[inline] private def approxDefEqImp (x : MetaM α) : MetaM α :=
withConfig (fun config => { config with foApprox := true, ctxApprox := true, quasiPatternApprox := true}) x
/-- Execute `x` using approximate unification: `foApprox`, `ctxApprox` and `quasiPatternApprox`. -/
@[inline] def approxDefEq : n α → n α :=
mapMetaM approxDefEqImp
@[inline] private def fullApproxDefEqImp (x : MetaM α) : MetaM α :=
withConfig (fun config => { config with foApprox := true, ctxApprox := true, quasiPatternApprox := true, constApprox := true }) x
/--
Similar to `approxDefEq`, but uses all available approximations.
We don't use `constApprox` by default at `approxDefEq` because it often produces undesirable solution for monadic code.
For example, suppose we have `pure (x > 0)` which has type `?m Prop`. We also have the goal `[Pure ?m]`.
Now, assume the expected type is `IO Bool`. Then, the unification constraint `?m Prop =?= IO Bool` could be solved
as `?m := fun _ => IO Bool` using `constApprox`, but this spurious solution would generate a failure when we try to
solve `[Pure (fun _ => IO Bool)]` -/
@[inline] def fullApproxDefEq : n α → n α :=
mapMetaM fullApproxDefEqImp
def normalizeLevel (u : Level) : MetaM Level := do
let u ← instantiateLevelMVars u
pure u.normalize
def assignLevelMVar (mvarId : MVarId) (u : Level) : MetaM Unit := do
modifyMCtx fun mctx => mctx.assignLevel mvarId u
def whnfR (e : Expr) : MetaM Expr :=
withTransparency TransparencyMode.reducible <| whnf e
def whnfD (e : Expr) : MetaM Expr :=
withTransparency TransparencyMode.default <| whnf e
def whnfI (e : Expr) : MetaM Expr :=
withTransparency TransparencyMode.instances <| whnf e
def setInlineAttribute (declName : Name) (kind := Compiler.InlineAttributeKind.inline): MetaM Unit := do
let env ← getEnv
match Compiler.setInlineAttribute env declName kind with
| Except.ok env => setEnv env
| Except.error msg => throwError msg
private partial def instantiateForallAux (ps : Array Expr) (i : Nat) (e : Expr) : MetaM Expr := do
if h : i < ps.size then
let p := ps.get ⟨i, h⟩
let e ← whnf e
match e with
| Expr.forallE _ _ b _ => instantiateForallAux ps (i+1) (b.instantiate1 p)
| _ => throwError "invalid instantiateForall, too many parameters"
else
pure e
/- Given `e` of the form `forall (a_1 : A_1) ... (a_n : A_n), B[a_1, ..., a_n]` and `p_1 : A_1, ... p_n : A_n`, return `B[p_1, ..., p_n]`. -/
def instantiateForall (e : Expr) (ps : Array Expr) : MetaM Expr :=
instantiateForallAux ps 0 e
private partial def instantiateLambdaAux (ps : Array Expr) (i : Nat) (e : Expr) : MetaM Expr := do
if h : i < ps.size then
let p := ps.get ⟨i, h⟩
let e ← whnf e
match e with
| Expr.lam _ _ b _ => instantiateLambdaAux ps (i+1) (b.instantiate1 p)
| _ => throwError "invalid instantiateLambda, too many parameters"
else
pure e
/- Given `e` of the form `fun (a_1 : A_1) ... (a_n : A_n) => t[a_1, ..., a_n]` and `p_1 : A_1, ... p_n : A_n`, return `t[p_1, ..., p_n]`.
It uses `whnf` to reduce `e` if it is not a lambda -/
def instantiateLambda (e : Expr) (ps : Array Expr) : MetaM Expr :=
instantiateLambdaAux ps 0 e
/-- Return true iff `e` depends on the free variable `fvarId` -/
def dependsOn (e : Expr) (fvarId : FVarId) : MetaM Bool :=
return (← getMCtx).exprDependsOn e fvarId
def ppExpr (e : Expr) : MetaM Format := do
let env ← getEnv
let mctx ← getMCtx
let lctx ← getLCtx
let opts ← getOptions
let ctxCore ← readThe Core.Context
Lean.ppExpr { env := env, mctx := mctx, lctx := lctx, opts := opts, currNamespace := ctxCore.currNamespace, openDecls := ctxCore.openDecls } e
@[inline] protected def orelse (x y : MetaM α) : MetaM α := do
let env ← getEnv
let mctx ← getMCtx
try x catch _ => setEnv env; setMCtx mctx; y
instance : OrElse (MetaM α) := ⟨Meta.orelse⟩
@[inline] private def orelseMergeErrorsImp (x y : MetaM α)
(mergeRef : Syntax → Syntax → Syntax := fun r₁ r₂ => r₁)
(mergeMsg : MessageData → MessageData → MessageData := fun m₁ m₂ => m₁ ++ Format.line ++ m₂) : MetaM α := do
let env ← getEnv
let mctx ← getMCtx
try
x
catch ex =>
setEnv env
setMCtx mctx
match ex with
| Exception.error ref₁ m₁ =>
try
y
catch
| Exception.error ref₂ m₂ => throw <| Exception.error (mergeRef ref₁ ref₂) (mergeMsg m₁ m₂)
| ex => throw ex
| ex => throw ex
/--
Similar to `orelse`, but merge errors. Note that internal errors are not caught.
The default `mergeRef` uses the `ref` (position information) for the first message.
The default `mergeMsg` combines error messages using `Format.line ++ Format.line` as a separator. -/
@[inline] def orelseMergeErrors [MonadControlT MetaM m] [Monad m] (x y : m α)
(mergeRef : Syntax → Syntax → Syntax := fun r₁ r₂ => r₁)
(mergeMsg : MessageData → MessageData → MessageData := fun m₁ m₂ => m₁ ++ Format.line ++ Format.line ++ m₂) : m α := do
controlAt MetaM fun runInBase => orelseMergeErrorsImp (runInBase x) (runInBase y) mergeRef mergeMsg
/-- Execute `x`, and apply `f` to the produced error message -/
def mapErrorImp (x : MetaM α) (f : MessageData → MessageData) : MetaM α := do
try
x
catch
| Exception.error ref msg => throw <| Exception.error ref <| f msg
| ex => throw ex
@[inline] def mapError [MonadControlT MetaM m] [Monad m] (x : m α) (f : MessageData → MessageData) : m α :=
controlAt MetaM fun runInBase => mapErrorImp (runInBase x) f
end Methods
end Meta
export Meta (MetaM)
end Lean
|
ed95d4c8fb411d2be7c141eb35e3315c81bd8270 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/simpperf/simp500.lean | 32d9bb68dc534940111b7ba0dd4ec40909fab8c6 | [
"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 | 42,099 | lean | axiom f (x : Prop) : Prop
axiom g0 (x : Prop) : Prop
axiom g1 (x : Prop) : Prop
axiom g2 (x : Prop) : Prop
axiom g3 (x : Prop) : Prop
axiom g4 (x : Prop) : Prop
axiom g5 (x : Prop) : Prop
axiom g6 (x : Prop) : Prop
axiom g7 (x : Prop) : Prop
axiom g8 (x : Prop) : Prop
axiom g9 (x : Prop) : Prop
axiom g10 (x : Prop) : Prop
axiom g11 (x : Prop) : Prop
axiom g12 (x : Prop) : Prop
axiom g13 (x : Prop) : Prop
axiom g14 (x : Prop) : Prop
axiom g15 (x : Prop) : Prop
axiom g16 (x : Prop) : Prop
axiom g17 (x : Prop) : Prop
axiom g18 (x : Prop) : Prop
axiom g19 (x : Prop) : Prop
axiom g20 (x : Prop) : Prop
axiom g21 (x : Prop) : Prop
axiom g22 (x : Prop) : Prop
axiom g23 (x : Prop) : Prop
axiom g24 (x : Prop) : Prop
axiom g25 (x : Prop) : Prop
axiom g26 (x : Prop) : Prop
axiom g27 (x : Prop) : Prop
axiom g28 (x : Prop) : Prop
axiom g29 (x : Prop) : Prop
axiom g30 (x : Prop) : Prop
axiom g31 (x : Prop) : Prop
axiom g32 (x : Prop) : Prop
axiom g33 (x : Prop) : Prop
axiom g34 (x : Prop) : Prop
axiom g35 (x : Prop) : Prop
axiom g36 (x : Prop) : Prop
axiom g37 (x : Prop) : Prop
axiom g38 (x : Prop) : Prop
axiom g39 (x : Prop) : Prop
axiom g40 (x : Prop) : Prop
axiom g41 (x : Prop) : Prop
axiom g42 (x : Prop) : Prop
axiom g43 (x : Prop) : Prop
axiom g44 (x : Prop) : Prop
axiom g45 (x : Prop) : Prop
axiom g46 (x : Prop) : Prop
axiom g47 (x : Prop) : Prop
axiom g48 (x : Prop) : Prop
axiom g49 (x : Prop) : Prop
axiom g50 (x : Prop) : Prop
axiom g51 (x : Prop) : Prop
axiom g52 (x : Prop) : Prop
axiom g53 (x : Prop) : Prop
axiom g54 (x : Prop) : Prop
axiom g55 (x : Prop) : Prop
axiom g56 (x : Prop) : Prop
axiom g57 (x : Prop) : Prop
axiom g58 (x : Prop) : Prop
axiom g59 (x : Prop) : Prop
axiom g60 (x : Prop) : Prop
axiom g61 (x : Prop) : Prop
axiom g62 (x : Prop) : Prop
axiom g63 (x : Prop) : Prop
axiom g64 (x : Prop) : Prop
axiom g65 (x : Prop) : Prop
axiom g66 (x : Prop) : Prop
axiom g67 (x : Prop) : Prop
axiom g68 (x : Prop) : Prop
axiom g69 (x : Prop) : Prop
axiom g70 (x : Prop) : Prop
axiom g71 (x : Prop) : Prop
axiom g72 (x : Prop) : Prop
axiom g73 (x : Prop) : Prop
axiom g74 (x : Prop) : Prop
axiom g75 (x : Prop) : Prop
axiom g76 (x : Prop) : Prop
axiom g77 (x : Prop) : Prop
axiom g78 (x : Prop) : Prop
axiom g79 (x : Prop) : Prop
axiom g80 (x : Prop) : Prop
axiom g81 (x : Prop) : Prop
axiom g82 (x : Prop) : Prop
axiom g83 (x : Prop) : Prop
axiom g84 (x : Prop) : Prop
axiom g85 (x : Prop) : Prop
axiom g86 (x : Prop) : Prop
axiom g87 (x : Prop) : Prop
axiom g88 (x : Prop) : Prop
axiom g89 (x : Prop) : Prop
axiom g90 (x : Prop) : Prop
axiom g91 (x : Prop) : Prop
axiom g92 (x : Prop) : Prop
axiom g93 (x : Prop) : Prop
axiom g94 (x : Prop) : Prop
axiom g95 (x : Prop) : Prop
axiom g96 (x : Prop) : Prop
axiom g97 (x : Prop) : Prop
axiom g98 (x : Prop) : Prop
axiom g99 (x : Prop) : Prop
axiom g100 (x : Prop) : Prop
axiom g101 (x : Prop) : Prop
axiom g102 (x : Prop) : Prop
axiom g103 (x : Prop) : Prop
axiom g104 (x : Prop) : Prop
axiom g105 (x : Prop) : Prop
axiom g106 (x : Prop) : Prop
axiom g107 (x : Prop) : Prop
axiom g108 (x : Prop) : Prop
axiom g109 (x : Prop) : Prop
axiom g110 (x : Prop) : Prop
axiom g111 (x : Prop) : Prop
axiom g112 (x : Prop) : Prop
axiom g113 (x : Prop) : Prop
axiom g114 (x : Prop) : Prop
axiom g115 (x : Prop) : Prop
axiom g116 (x : Prop) : Prop
axiom g117 (x : Prop) : Prop
axiom g118 (x : Prop) : Prop
axiom g119 (x : Prop) : Prop
axiom g120 (x : Prop) : Prop
axiom g121 (x : Prop) : Prop
axiom g122 (x : Prop) : Prop
axiom g123 (x : Prop) : Prop
axiom g124 (x : Prop) : Prop
axiom g125 (x : Prop) : Prop
axiom g126 (x : Prop) : Prop
axiom g127 (x : Prop) : Prop
axiom g128 (x : Prop) : Prop
axiom g129 (x : Prop) : Prop
axiom g130 (x : Prop) : Prop
axiom g131 (x : Prop) : Prop
axiom g132 (x : Prop) : Prop
axiom g133 (x : Prop) : Prop
axiom g134 (x : Prop) : Prop
axiom g135 (x : Prop) : Prop
axiom g136 (x : Prop) : Prop
axiom g137 (x : Prop) : Prop
axiom g138 (x : Prop) : Prop
axiom g139 (x : Prop) : Prop
axiom g140 (x : Prop) : Prop
axiom g141 (x : Prop) : Prop
axiom g142 (x : Prop) : Prop
axiom g143 (x : Prop) : Prop
axiom g144 (x : Prop) : Prop
axiom g145 (x : Prop) : Prop
axiom g146 (x : Prop) : Prop
axiom g147 (x : Prop) : Prop
axiom g148 (x : Prop) : Prop
axiom g149 (x : Prop) : Prop
axiom g150 (x : Prop) : Prop
axiom g151 (x : Prop) : Prop
axiom g152 (x : Prop) : Prop
axiom g153 (x : Prop) : Prop
axiom g154 (x : Prop) : Prop
axiom g155 (x : Prop) : Prop
axiom g156 (x : Prop) : Prop
axiom g157 (x : Prop) : Prop
axiom g158 (x : Prop) : Prop
axiom g159 (x : Prop) : Prop
axiom g160 (x : Prop) : Prop
axiom g161 (x : Prop) : Prop
axiom g162 (x : Prop) : Prop
axiom g163 (x : Prop) : Prop
axiom g164 (x : Prop) : Prop
axiom g165 (x : Prop) : Prop
axiom g166 (x : Prop) : Prop
axiom g167 (x : Prop) : Prop
axiom g168 (x : Prop) : Prop
axiom g169 (x : Prop) : Prop
axiom g170 (x : Prop) : Prop
axiom g171 (x : Prop) : Prop
axiom g172 (x : Prop) : Prop
axiom g173 (x : Prop) : Prop
axiom g174 (x : Prop) : Prop
axiom g175 (x : Prop) : Prop
axiom g176 (x : Prop) : Prop
axiom g177 (x : Prop) : Prop
axiom g178 (x : Prop) : Prop
axiom g179 (x : Prop) : Prop
axiom g180 (x : Prop) : Prop
axiom g181 (x : Prop) : Prop
axiom g182 (x : Prop) : Prop
axiom g183 (x : Prop) : Prop
axiom g184 (x : Prop) : Prop
axiom g185 (x : Prop) : Prop
axiom g186 (x : Prop) : Prop
axiom g187 (x : Prop) : Prop
axiom g188 (x : Prop) : Prop
axiom g189 (x : Prop) : Prop
axiom g190 (x : Prop) : Prop
axiom g191 (x : Prop) : Prop
axiom g192 (x : Prop) : Prop
axiom g193 (x : Prop) : Prop
axiom g194 (x : Prop) : Prop
axiom g195 (x : Prop) : Prop
axiom g196 (x : Prop) : Prop
axiom g197 (x : Prop) : Prop
axiom g198 (x : Prop) : Prop
axiom g199 (x : Prop) : Prop
axiom g200 (x : Prop) : Prop
axiom g201 (x : Prop) : Prop
axiom g202 (x : Prop) : Prop
axiom g203 (x : Prop) : Prop
axiom g204 (x : Prop) : Prop
axiom g205 (x : Prop) : Prop
axiom g206 (x : Prop) : Prop
axiom g207 (x : Prop) : Prop
axiom g208 (x : Prop) : Prop
axiom g209 (x : Prop) : Prop
axiom g210 (x : Prop) : Prop
axiom g211 (x : Prop) : Prop
axiom g212 (x : Prop) : Prop
axiom g213 (x : Prop) : Prop
axiom g214 (x : Prop) : Prop
axiom g215 (x : Prop) : Prop
axiom g216 (x : Prop) : Prop
axiom g217 (x : Prop) : Prop
axiom g218 (x : Prop) : Prop
axiom g219 (x : Prop) : Prop
axiom g220 (x : Prop) : Prop
axiom g221 (x : Prop) : Prop
axiom g222 (x : Prop) : Prop
axiom g223 (x : Prop) : Prop
axiom g224 (x : Prop) : Prop
axiom g225 (x : Prop) : Prop
axiom g226 (x : Prop) : Prop
axiom g227 (x : Prop) : Prop
axiom g228 (x : Prop) : Prop
axiom g229 (x : Prop) : Prop
axiom g230 (x : Prop) : Prop
axiom g231 (x : Prop) : Prop
axiom g232 (x : Prop) : Prop
axiom g233 (x : Prop) : Prop
axiom g234 (x : Prop) : Prop
axiom g235 (x : Prop) : Prop
axiom g236 (x : Prop) : Prop
axiom g237 (x : Prop) : Prop
axiom g238 (x : Prop) : Prop
axiom g239 (x : Prop) : Prop
axiom g240 (x : Prop) : Prop
axiom g241 (x : Prop) : Prop
axiom g242 (x : Prop) : Prop
axiom g243 (x : Prop) : Prop
axiom g244 (x : Prop) : Prop
axiom g245 (x : Prop) : Prop
axiom g246 (x : Prop) : Prop
axiom g247 (x : Prop) : Prop
axiom g248 (x : Prop) : Prop
axiom g249 (x : Prop) : Prop
axiom g250 (x : Prop) : Prop
axiom g251 (x : Prop) : Prop
axiom g252 (x : Prop) : Prop
axiom g253 (x : Prop) : Prop
axiom g254 (x : Prop) : Prop
axiom g255 (x : Prop) : Prop
axiom g256 (x : Prop) : Prop
axiom g257 (x : Prop) : Prop
axiom g258 (x : Prop) : Prop
axiom g259 (x : Prop) : Prop
axiom g260 (x : Prop) : Prop
axiom g261 (x : Prop) : Prop
axiom g262 (x : Prop) : Prop
axiom g263 (x : Prop) : Prop
axiom g264 (x : Prop) : Prop
axiom g265 (x : Prop) : Prop
axiom g266 (x : Prop) : Prop
axiom g267 (x : Prop) : Prop
axiom g268 (x : Prop) : Prop
axiom g269 (x : Prop) : Prop
axiom g270 (x : Prop) : Prop
axiom g271 (x : Prop) : Prop
axiom g272 (x : Prop) : Prop
axiom g273 (x : Prop) : Prop
axiom g274 (x : Prop) : Prop
axiom g275 (x : Prop) : Prop
axiom g276 (x : Prop) : Prop
axiom g277 (x : Prop) : Prop
axiom g278 (x : Prop) : Prop
axiom g279 (x : Prop) : Prop
axiom g280 (x : Prop) : Prop
axiom g281 (x : Prop) : Prop
axiom g282 (x : Prop) : Prop
axiom g283 (x : Prop) : Prop
axiom g284 (x : Prop) : Prop
axiom g285 (x : Prop) : Prop
axiom g286 (x : Prop) : Prop
axiom g287 (x : Prop) : Prop
axiom g288 (x : Prop) : Prop
axiom g289 (x : Prop) : Prop
axiom g290 (x : Prop) : Prop
axiom g291 (x : Prop) : Prop
axiom g292 (x : Prop) : Prop
axiom g293 (x : Prop) : Prop
axiom g294 (x : Prop) : Prop
axiom g295 (x : Prop) : Prop
axiom g296 (x : Prop) : Prop
axiom g297 (x : Prop) : Prop
axiom g298 (x : Prop) : Prop
axiom g299 (x : Prop) : Prop
axiom g300 (x : Prop) : Prop
axiom g301 (x : Prop) : Prop
axiom g302 (x : Prop) : Prop
axiom g303 (x : Prop) : Prop
axiom g304 (x : Prop) : Prop
axiom g305 (x : Prop) : Prop
axiom g306 (x : Prop) : Prop
axiom g307 (x : Prop) : Prop
axiom g308 (x : Prop) : Prop
axiom g309 (x : Prop) : Prop
axiom g310 (x : Prop) : Prop
axiom g311 (x : Prop) : Prop
axiom g312 (x : Prop) : Prop
axiom g313 (x : Prop) : Prop
axiom g314 (x : Prop) : Prop
axiom g315 (x : Prop) : Prop
axiom g316 (x : Prop) : Prop
axiom g317 (x : Prop) : Prop
axiom g318 (x : Prop) : Prop
axiom g319 (x : Prop) : Prop
axiom g320 (x : Prop) : Prop
axiom g321 (x : Prop) : Prop
axiom g322 (x : Prop) : Prop
axiom g323 (x : Prop) : Prop
axiom g324 (x : Prop) : Prop
axiom g325 (x : Prop) : Prop
axiom g326 (x : Prop) : Prop
axiom g327 (x : Prop) : Prop
axiom g328 (x : Prop) : Prop
axiom g329 (x : Prop) : Prop
axiom g330 (x : Prop) : Prop
axiom g331 (x : Prop) : Prop
axiom g332 (x : Prop) : Prop
axiom g333 (x : Prop) : Prop
axiom g334 (x : Prop) : Prop
axiom g335 (x : Prop) : Prop
axiom g336 (x : Prop) : Prop
axiom g337 (x : Prop) : Prop
axiom g338 (x : Prop) : Prop
axiom g339 (x : Prop) : Prop
axiom g340 (x : Prop) : Prop
axiom g341 (x : Prop) : Prop
axiom g342 (x : Prop) : Prop
axiom g343 (x : Prop) : Prop
axiom g344 (x : Prop) : Prop
axiom g345 (x : Prop) : Prop
axiom g346 (x : Prop) : Prop
axiom g347 (x : Prop) : Prop
axiom g348 (x : Prop) : Prop
axiom g349 (x : Prop) : Prop
axiom g350 (x : Prop) : Prop
axiom g351 (x : Prop) : Prop
axiom g352 (x : Prop) : Prop
axiom g353 (x : Prop) : Prop
axiom g354 (x : Prop) : Prop
axiom g355 (x : Prop) : Prop
axiom g356 (x : Prop) : Prop
axiom g357 (x : Prop) : Prop
axiom g358 (x : Prop) : Prop
axiom g359 (x : Prop) : Prop
axiom g360 (x : Prop) : Prop
axiom g361 (x : Prop) : Prop
axiom g362 (x : Prop) : Prop
axiom g363 (x : Prop) : Prop
axiom g364 (x : Prop) : Prop
axiom g365 (x : Prop) : Prop
axiom g366 (x : Prop) : Prop
axiom g367 (x : Prop) : Prop
axiom g368 (x : Prop) : Prop
axiom g369 (x : Prop) : Prop
axiom g370 (x : Prop) : Prop
axiom g371 (x : Prop) : Prop
axiom g372 (x : Prop) : Prop
axiom g373 (x : Prop) : Prop
axiom g374 (x : Prop) : Prop
axiom g375 (x : Prop) : Prop
axiom g376 (x : Prop) : Prop
axiom g377 (x : Prop) : Prop
axiom g378 (x : Prop) : Prop
axiom g379 (x : Prop) : Prop
axiom g380 (x : Prop) : Prop
axiom g381 (x : Prop) : Prop
axiom g382 (x : Prop) : Prop
axiom g383 (x : Prop) : Prop
axiom g384 (x : Prop) : Prop
axiom g385 (x : Prop) : Prop
axiom g386 (x : Prop) : Prop
axiom g387 (x : Prop) : Prop
axiom g388 (x : Prop) : Prop
axiom g389 (x : Prop) : Prop
axiom g390 (x : Prop) : Prop
axiom g391 (x : Prop) : Prop
axiom g392 (x : Prop) : Prop
axiom g393 (x : Prop) : Prop
axiom g394 (x : Prop) : Prop
axiom g395 (x : Prop) : Prop
axiom g396 (x : Prop) : Prop
axiom g397 (x : Prop) : Prop
axiom g398 (x : Prop) : Prop
axiom g399 (x : Prop) : Prop
axiom g400 (x : Prop) : Prop
axiom g401 (x : Prop) : Prop
axiom g402 (x : Prop) : Prop
axiom g403 (x : Prop) : Prop
axiom g404 (x : Prop) : Prop
axiom g405 (x : Prop) : Prop
axiom g406 (x : Prop) : Prop
axiom g407 (x : Prop) : Prop
axiom g408 (x : Prop) : Prop
axiom g409 (x : Prop) : Prop
axiom g410 (x : Prop) : Prop
axiom g411 (x : Prop) : Prop
axiom g412 (x : Prop) : Prop
axiom g413 (x : Prop) : Prop
axiom g414 (x : Prop) : Prop
axiom g415 (x : Prop) : Prop
axiom g416 (x : Prop) : Prop
axiom g417 (x : Prop) : Prop
axiom g418 (x : Prop) : Prop
axiom g419 (x : Prop) : Prop
axiom g420 (x : Prop) : Prop
axiom g421 (x : Prop) : Prop
axiom g422 (x : Prop) : Prop
axiom g423 (x : Prop) : Prop
axiom g424 (x : Prop) : Prop
axiom g425 (x : Prop) : Prop
axiom g426 (x : Prop) : Prop
axiom g427 (x : Prop) : Prop
axiom g428 (x : Prop) : Prop
axiom g429 (x : Prop) : Prop
axiom g430 (x : Prop) : Prop
axiom g431 (x : Prop) : Prop
axiom g432 (x : Prop) : Prop
axiom g433 (x : Prop) : Prop
axiom g434 (x : Prop) : Prop
axiom g435 (x : Prop) : Prop
axiom g436 (x : Prop) : Prop
axiom g437 (x : Prop) : Prop
axiom g438 (x : Prop) : Prop
axiom g439 (x : Prop) : Prop
axiom g440 (x : Prop) : Prop
axiom g441 (x : Prop) : Prop
axiom g442 (x : Prop) : Prop
axiom g443 (x : Prop) : Prop
axiom g444 (x : Prop) : Prop
axiom g445 (x : Prop) : Prop
axiom g446 (x : Prop) : Prop
axiom g447 (x : Prop) : Prop
axiom g448 (x : Prop) : Prop
axiom g449 (x : Prop) : Prop
axiom g450 (x : Prop) : Prop
axiom g451 (x : Prop) : Prop
axiom g452 (x : Prop) : Prop
axiom g453 (x : Prop) : Prop
axiom g454 (x : Prop) : Prop
axiom g455 (x : Prop) : Prop
axiom g456 (x : Prop) : Prop
axiom g457 (x : Prop) : Prop
axiom g458 (x : Prop) : Prop
axiom g459 (x : Prop) : Prop
axiom g460 (x : Prop) : Prop
axiom g461 (x : Prop) : Prop
axiom g462 (x : Prop) : Prop
axiom g463 (x : Prop) : Prop
axiom g464 (x : Prop) : Prop
axiom g465 (x : Prop) : Prop
axiom g466 (x : Prop) : Prop
axiom g467 (x : Prop) : Prop
axiom g468 (x : Prop) : Prop
axiom g469 (x : Prop) : Prop
axiom g470 (x : Prop) : Prop
axiom g471 (x : Prop) : Prop
axiom g472 (x : Prop) : Prop
axiom g473 (x : Prop) : Prop
axiom g474 (x : Prop) : Prop
axiom g475 (x : Prop) : Prop
axiom g476 (x : Prop) : Prop
axiom g477 (x : Prop) : Prop
axiom g478 (x : Prop) : Prop
axiom g479 (x : Prop) : Prop
axiom g480 (x : Prop) : Prop
axiom g481 (x : Prop) : Prop
axiom g482 (x : Prop) : Prop
axiom g483 (x : Prop) : Prop
axiom g484 (x : Prop) : Prop
axiom g485 (x : Prop) : Prop
axiom g486 (x : Prop) : Prop
axiom g487 (x : Prop) : Prop
axiom g488 (x : Prop) : Prop
axiom g489 (x : Prop) : Prop
axiom g490 (x : Prop) : Prop
axiom g491 (x : Prop) : Prop
axiom g492 (x : Prop) : Prop
axiom g493 (x : Prop) : Prop
axiom g494 (x : Prop) : Prop
axiom g495 (x : Prop) : Prop
axiom g496 (x : Prop) : Prop
axiom g497 (x : Prop) : Prop
axiom g498 (x : Prop) : Prop
axiom g499 (x : Prop) : Prop
@[simp] axiom s0 (x : Prop) : f (g1 x) = f (g0 x)
@[simp] axiom s1 (x : Prop) : f (g2 x) = f (g1 x)
@[simp] axiom s2 (x : Prop) : f (g3 x) = f (g2 x)
@[simp] axiom s3 (x : Prop) : f (g4 x) = f (g3 x)
@[simp] axiom s4 (x : Prop) : f (g5 x) = f (g4 x)
@[simp] axiom s5 (x : Prop) : f (g6 x) = f (g5 x)
@[simp] axiom s6 (x : Prop) : f (g7 x) = f (g6 x)
@[simp] axiom s7 (x : Prop) : f (g8 x) = f (g7 x)
@[simp] axiom s8 (x : Prop) : f (g9 x) = f (g8 x)
@[simp] axiom s9 (x : Prop) : f (g10 x) = f (g9 x)
@[simp] axiom s10 (x : Prop) : f (g11 x) = f (g10 x)
@[simp] axiom s11 (x : Prop) : f (g12 x) = f (g11 x)
@[simp] axiom s12 (x : Prop) : f (g13 x) = f (g12 x)
@[simp] axiom s13 (x : Prop) : f (g14 x) = f (g13 x)
@[simp] axiom s14 (x : Prop) : f (g15 x) = f (g14 x)
@[simp] axiom s15 (x : Prop) : f (g16 x) = f (g15 x)
@[simp] axiom s16 (x : Prop) : f (g17 x) = f (g16 x)
@[simp] axiom s17 (x : Prop) : f (g18 x) = f (g17 x)
@[simp] axiom s18 (x : Prop) : f (g19 x) = f (g18 x)
@[simp] axiom s19 (x : Prop) : f (g20 x) = f (g19 x)
@[simp] axiom s20 (x : Prop) : f (g21 x) = f (g20 x)
@[simp] axiom s21 (x : Prop) : f (g22 x) = f (g21 x)
@[simp] axiom s22 (x : Prop) : f (g23 x) = f (g22 x)
@[simp] axiom s23 (x : Prop) : f (g24 x) = f (g23 x)
@[simp] axiom s24 (x : Prop) : f (g25 x) = f (g24 x)
@[simp] axiom s25 (x : Prop) : f (g26 x) = f (g25 x)
@[simp] axiom s26 (x : Prop) : f (g27 x) = f (g26 x)
@[simp] axiom s27 (x : Prop) : f (g28 x) = f (g27 x)
@[simp] axiom s28 (x : Prop) : f (g29 x) = f (g28 x)
@[simp] axiom s29 (x : Prop) : f (g30 x) = f (g29 x)
@[simp] axiom s30 (x : Prop) : f (g31 x) = f (g30 x)
@[simp] axiom s31 (x : Prop) : f (g32 x) = f (g31 x)
@[simp] axiom s32 (x : Prop) : f (g33 x) = f (g32 x)
@[simp] axiom s33 (x : Prop) : f (g34 x) = f (g33 x)
@[simp] axiom s34 (x : Prop) : f (g35 x) = f (g34 x)
@[simp] axiom s35 (x : Prop) : f (g36 x) = f (g35 x)
@[simp] axiom s36 (x : Prop) : f (g37 x) = f (g36 x)
@[simp] axiom s37 (x : Prop) : f (g38 x) = f (g37 x)
@[simp] axiom s38 (x : Prop) : f (g39 x) = f (g38 x)
@[simp] axiom s39 (x : Prop) : f (g40 x) = f (g39 x)
@[simp] axiom s40 (x : Prop) : f (g41 x) = f (g40 x)
@[simp] axiom s41 (x : Prop) : f (g42 x) = f (g41 x)
@[simp] axiom s42 (x : Prop) : f (g43 x) = f (g42 x)
@[simp] axiom s43 (x : Prop) : f (g44 x) = f (g43 x)
@[simp] axiom s44 (x : Prop) : f (g45 x) = f (g44 x)
@[simp] axiom s45 (x : Prop) : f (g46 x) = f (g45 x)
@[simp] axiom s46 (x : Prop) : f (g47 x) = f (g46 x)
@[simp] axiom s47 (x : Prop) : f (g48 x) = f (g47 x)
@[simp] axiom s48 (x : Prop) : f (g49 x) = f (g48 x)
@[simp] axiom s49 (x : Prop) : f (g50 x) = f (g49 x)
@[simp] axiom s50 (x : Prop) : f (g51 x) = f (g50 x)
@[simp] axiom s51 (x : Prop) : f (g52 x) = f (g51 x)
@[simp] axiom s52 (x : Prop) : f (g53 x) = f (g52 x)
@[simp] axiom s53 (x : Prop) : f (g54 x) = f (g53 x)
@[simp] axiom s54 (x : Prop) : f (g55 x) = f (g54 x)
@[simp] axiom s55 (x : Prop) : f (g56 x) = f (g55 x)
@[simp] axiom s56 (x : Prop) : f (g57 x) = f (g56 x)
@[simp] axiom s57 (x : Prop) : f (g58 x) = f (g57 x)
@[simp] axiom s58 (x : Prop) : f (g59 x) = f (g58 x)
@[simp] axiom s59 (x : Prop) : f (g60 x) = f (g59 x)
@[simp] axiom s60 (x : Prop) : f (g61 x) = f (g60 x)
@[simp] axiom s61 (x : Prop) : f (g62 x) = f (g61 x)
@[simp] axiom s62 (x : Prop) : f (g63 x) = f (g62 x)
@[simp] axiom s63 (x : Prop) : f (g64 x) = f (g63 x)
@[simp] axiom s64 (x : Prop) : f (g65 x) = f (g64 x)
@[simp] axiom s65 (x : Prop) : f (g66 x) = f (g65 x)
@[simp] axiom s66 (x : Prop) : f (g67 x) = f (g66 x)
@[simp] axiom s67 (x : Prop) : f (g68 x) = f (g67 x)
@[simp] axiom s68 (x : Prop) : f (g69 x) = f (g68 x)
@[simp] axiom s69 (x : Prop) : f (g70 x) = f (g69 x)
@[simp] axiom s70 (x : Prop) : f (g71 x) = f (g70 x)
@[simp] axiom s71 (x : Prop) : f (g72 x) = f (g71 x)
@[simp] axiom s72 (x : Prop) : f (g73 x) = f (g72 x)
@[simp] axiom s73 (x : Prop) : f (g74 x) = f (g73 x)
@[simp] axiom s74 (x : Prop) : f (g75 x) = f (g74 x)
@[simp] axiom s75 (x : Prop) : f (g76 x) = f (g75 x)
@[simp] axiom s76 (x : Prop) : f (g77 x) = f (g76 x)
@[simp] axiom s77 (x : Prop) : f (g78 x) = f (g77 x)
@[simp] axiom s78 (x : Prop) : f (g79 x) = f (g78 x)
@[simp] axiom s79 (x : Prop) : f (g80 x) = f (g79 x)
@[simp] axiom s80 (x : Prop) : f (g81 x) = f (g80 x)
@[simp] axiom s81 (x : Prop) : f (g82 x) = f (g81 x)
@[simp] axiom s82 (x : Prop) : f (g83 x) = f (g82 x)
@[simp] axiom s83 (x : Prop) : f (g84 x) = f (g83 x)
@[simp] axiom s84 (x : Prop) : f (g85 x) = f (g84 x)
@[simp] axiom s85 (x : Prop) : f (g86 x) = f (g85 x)
@[simp] axiom s86 (x : Prop) : f (g87 x) = f (g86 x)
@[simp] axiom s87 (x : Prop) : f (g88 x) = f (g87 x)
@[simp] axiom s88 (x : Prop) : f (g89 x) = f (g88 x)
@[simp] axiom s89 (x : Prop) : f (g90 x) = f (g89 x)
@[simp] axiom s90 (x : Prop) : f (g91 x) = f (g90 x)
@[simp] axiom s91 (x : Prop) : f (g92 x) = f (g91 x)
@[simp] axiom s92 (x : Prop) : f (g93 x) = f (g92 x)
@[simp] axiom s93 (x : Prop) : f (g94 x) = f (g93 x)
@[simp] axiom s94 (x : Prop) : f (g95 x) = f (g94 x)
@[simp] axiom s95 (x : Prop) : f (g96 x) = f (g95 x)
@[simp] axiom s96 (x : Prop) : f (g97 x) = f (g96 x)
@[simp] axiom s97 (x : Prop) : f (g98 x) = f (g97 x)
@[simp] axiom s98 (x : Prop) : f (g99 x) = f (g98 x)
@[simp] axiom s99 (x : Prop) : f (g100 x) = f (g99 x)
@[simp] axiom s100 (x : Prop) : f (g101 x) = f (g100 x)
@[simp] axiom s101 (x : Prop) : f (g102 x) = f (g101 x)
@[simp] axiom s102 (x : Prop) : f (g103 x) = f (g102 x)
@[simp] axiom s103 (x : Prop) : f (g104 x) = f (g103 x)
@[simp] axiom s104 (x : Prop) : f (g105 x) = f (g104 x)
@[simp] axiom s105 (x : Prop) : f (g106 x) = f (g105 x)
@[simp] axiom s106 (x : Prop) : f (g107 x) = f (g106 x)
@[simp] axiom s107 (x : Prop) : f (g108 x) = f (g107 x)
@[simp] axiom s108 (x : Prop) : f (g109 x) = f (g108 x)
@[simp] axiom s109 (x : Prop) : f (g110 x) = f (g109 x)
@[simp] axiom s110 (x : Prop) : f (g111 x) = f (g110 x)
@[simp] axiom s111 (x : Prop) : f (g112 x) = f (g111 x)
@[simp] axiom s112 (x : Prop) : f (g113 x) = f (g112 x)
@[simp] axiom s113 (x : Prop) : f (g114 x) = f (g113 x)
@[simp] axiom s114 (x : Prop) : f (g115 x) = f (g114 x)
@[simp] axiom s115 (x : Prop) : f (g116 x) = f (g115 x)
@[simp] axiom s116 (x : Prop) : f (g117 x) = f (g116 x)
@[simp] axiom s117 (x : Prop) : f (g118 x) = f (g117 x)
@[simp] axiom s118 (x : Prop) : f (g119 x) = f (g118 x)
@[simp] axiom s119 (x : Prop) : f (g120 x) = f (g119 x)
@[simp] axiom s120 (x : Prop) : f (g121 x) = f (g120 x)
@[simp] axiom s121 (x : Prop) : f (g122 x) = f (g121 x)
@[simp] axiom s122 (x : Prop) : f (g123 x) = f (g122 x)
@[simp] axiom s123 (x : Prop) : f (g124 x) = f (g123 x)
@[simp] axiom s124 (x : Prop) : f (g125 x) = f (g124 x)
@[simp] axiom s125 (x : Prop) : f (g126 x) = f (g125 x)
@[simp] axiom s126 (x : Prop) : f (g127 x) = f (g126 x)
@[simp] axiom s127 (x : Prop) : f (g128 x) = f (g127 x)
@[simp] axiom s128 (x : Prop) : f (g129 x) = f (g128 x)
@[simp] axiom s129 (x : Prop) : f (g130 x) = f (g129 x)
@[simp] axiom s130 (x : Prop) : f (g131 x) = f (g130 x)
@[simp] axiom s131 (x : Prop) : f (g132 x) = f (g131 x)
@[simp] axiom s132 (x : Prop) : f (g133 x) = f (g132 x)
@[simp] axiom s133 (x : Prop) : f (g134 x) = f (g133 x)
@[simp] axiom s134 (x : Prop) : f (g135 x) = f (g134 x)
@[simp] axiom s135 (x : Prop) : f (g136 x) = f (g135 x)
@[simp] axiom s136 (x : Prop) : f (g137 x) = f (g136 x)
@[simp] axiom s137 (x : Prop) : f (g138 x) = f (g137 x)
@[simp] axiom s138 (x : Prop) : f (g139 x) = f (g138 x)
@[simp] axiom s139 (x : Prop) : f (g140 x) = f (g139 x)
@[simp] axiom s140 (x : Prop) : f (g141 x) = f (g140 x)
@[simp] axiom s141 (x : Prop) : f (g142 x) = f (g141 x)
@[simp] axiom s142 (x : Prop) : f (g143 x) = f (g142 x)
@[simp] axiom s143 (x : Prop) : f (g144 x) = f (g143 x)
@[simp] axiom s144 (x : Prop) : f (g145 x) = f (g144 x)
@[simp] axiom s145 (x : Prop) : f (g146 x) = f (g145 x)
@[simp] axiom s146 (x : Prop) : f (g147 x) = f (g146 x)
@[simp] axiom s147 (x : Prop) : f (g148 x) = f (g147 x)
@[simp] axiom s148 (x : Prop) : f (g149 x) = f (g148 x)
@[simp] axiom s149 (x : Prop) : f (g150 x) = f (g149 x)
@[simp] axiom s150 (x : Prop) : f (g151 x) = f (g150 x)
@[simp] axiom s151 (x : Prop) : f (g152 x) = f (g151 x)
@[simp] axiom s152 (x : Prop) : f (g153 x) = f (g152 x)
@[simp] axiom s153 (x : Prop) : f (g154 x) = f (g153 x)
@[simp] axiom s154 (x : Prop) : f (g155 x) = f (g154 x)
@[simp] axiom s155 (x : Prop) : f (g156 x) = f (g155 x)
@[simp] axiom s156 (x : Prop) : f (g157 x) = f (g156 x)
@[simp] axiom s157 (x : Prop) : f (g158 x) = f (g157 x)
@[simp] axiom s158 (x : Prop) : f (g159 x) = f (g158 x)
@[simp] axiom s159 (x : Prop) : f (g160 x) = f (g159 x)
@[simp] axiom s160 (x : Prop) : f (g161 x) = f (g160 x)
@[simp] axiom s161 (x : Prop) : f (g162 x) = f (g161 x)
@[simp] axiom s162 (x : Prop) : f (g163 x) = f (g162 x)
@[simp] axiom s163 (x : Prop) : f (g164 x) = f (g163 x)
@[simp] axiom s164 (x : Prop) : f (g165 x) = f (g164 x)
@[simp] axiom s165 (x : Prop) : f (g166 x) = f (g165 x)
@[simp] axiom s166 (x : Prop) : f (g167 x) = f (g166 x)
@[simp] axiom s167 (x : Prop) : f (g168 x) = f (g167 x)
@[simp] axiom s168 (x : Prop) : f (g169 x) = f (g168 x)
@[simp] axiom s169 (x : Prop) : f (g170 x) = f (g169 x)
@[simp] axiom s170 (x : Prop) : f (g171 x) = f (g170 x)
@[simp] axiom s171 (x : Prop) : f (g172 x) = f (g171 x)
@[simp] axiom s172 (x : Prop) : f (g173 x) = f (g172 x)
@[simp] axiom s173 (x : Prop) : f (g174 x) = f (g173 x)
@[simp] axiom s174 (x : Prop) : f (g175 x) = f (g174 x)
@[simp] axiom s175 (x : Prop) : f (g176 x) = f (g175 x)
@[simp] axiom s176 (x : Prop) : f (g177 x) = f (g176 x)
@[simp] axiom s177 (x : Prop) : f (g178 x) = f (g177 x)
@[simp] axiom s178 (x : Prop) : f (g179 x) = f (g178 x)
@[simp] axiom s179 (x : Prop) : f (g180 x) = f (g179 x)
@[simp] axiom s180 (x : Prop) : f (g181 x) = f (g180 x)
@[simp] axiom s181 (x : Prop) : f (g182 x) = f (g181 x)
@[simp] axiom s182 (x : Prop) : f (g183 x) = f (g182 x)
@[simp] axiom s183 (x : Prop) : f (g184 x) = f (g183 x)
@[simp] axiom s184 (x : Prop) : f (g185 x) = f (g184 x)
@[simp] axiom s185 (x : Prop) : f (g186 x) = f (g185 x)
@[simp] axiom s186 (x : Prop) : f (g187 x) = f (g186 x)
@[simp] axiom s187 (x : Prop) : f (g188 x) = f (g187 x)
@[simp] axiom s188 (x : Prop) : f (g189 x) = f (g188 x)
@[simp] axiom s189 (x : Prop) : f (g190 x) = f (g189 x)
@[simp] axiom s190 (x : Prop) : f (g191 x) = f (g190 x)
@[simp] axiom s191 (x : Prop) : f (g192 x) = f (g191 x)
@[simp] axiom s192 (x : Prop) : f (g193 x) = f (g192 x)
@[simp] axiom s193 (x : Prop) : f (g194 x) = f (g193 x)
@[simp] axiom s194 (x : Prop) : f (g195 x) = f (g194 x)
@[simp] axiom s195 (x : Prop) : f (g196 x) = f (g195 x)
@[simp] axiom s196 (x : Prop) : f (g197 x) = f (g196 x)
@[simp] axiom s197 (x : Prop) : f (g198 x) = f (g197 x)
@[simp] axiom s198 (x : Prop) : f (g199 x) = f (g198 x)
@[simp] axiom s199 (x : Prop) : f (g200 x) = f (g199 x)
@[simp] axiom s200 (x : Prop) : f (g201 x) = f (g200 x)
@[simp] axiom s201 (x : Prop) : f (g202 x) = f (g201 x)
@[simp] axiom s202 (x : Prop) : f (g203 x) = f (g202 x)
@[simp] axiom s203 (x : Prop) : f (g204 x) = f (g203 x)
@[simp] axiom s204 (x : Prop) : f (g205 x) = f (g204 x)
@[simp] axiom s205 (x : Prop) : f (g206 x) = f (g205 x)
@[simp] axiom s206 (x : Prop) : f (g207 x) = f (g206 x)
@[simp] axiom s207 (x : Prop) : f (g208 x) = f (g207 x)
@[simp] axiom s208 (x : Prop) : f (g209 x) = f (g208 x)
@[simp] axiom s209 (x : Prop) : f (g210 x) = f (g209 x)
@[simp] axiom s210 (x : Prop) : f (g211 x) = f (g210 x)
@[simp] axiom s211 (x : Prop) : f (g212 x) = f (g211 x)
@[simp] axiom s212 (x : Prop) : f (g213 x) = f (g212 x)
@[simp] axiom s213 (x : Prop) : f (g214 x) = f (g213 x)
@[simp] axiom s214 (x : Prop) : f (g215 x) = f (g214 x)
@[simp] axiom s215 (x : Prop) : f (g216 x) = f (g215 x)
@[simp] axiom s216 (x : Prop) : f (g217 x) = f (g216 x)
@[simp] axiom s217 (x : Prop) : f (g218 x) = f (g217 x)
@[simp] axiom s218 (x : Prop) : f (g219 x) = f (g218 x)
@[simp] axiom s219 (x : Prop) : f (g220 x) = f (g219 x)
@[simp] axiom s220 (x : Prop) : f (g221 x) = f (g220 x)
@[simp] axiom s221 (x : Prop) : f (g222 x) = f (g221 x)
@[simp] axiom s222 (x : Prop) : f (g223 x) = f (g222 x)
@[simp] axiom s223 (x : Prop) : f (g224 x) = f (g223 x)
@[simp] axiom s224 (x : Prop) : f (g225 x) = f (g224 x)
@[simp] axiom s225 (x : Prop) : f (g226 x) = f (g225 x)
@[simp] axiom s226 (x : Prop) : f (g227 x) = f (g226 x)
@[simp] axiom s227 (x : Prop) : f (g228 x) = f (g227 x)
@[simp] axiom s228 (x : Prop) : f (g229 x) = f (g228 x)
@[simp] axiom s229 (x : Prop) : f (g230 x) = f (g229 x)
@[simp] axiom s230 (x : Prop) : f (g231 x) = f (g230 x)
@[simp] axiom s231 (x : Prop) : f (g232 x) = f (g231 x)
@[simp] axiom s232 (x : Prop) : f (g233 x) = f (g232 x)
@[simp] axiom s233 (x : Prop) : f (g234 x) = f (g233 x)
@[simp] axiom s234 (x : Prop) : f (g235 x) = f (g234 x)
@[simp] axiom s235 (x : Prop) : f (g236 x) = f (g235 x)
@[simp] axiom s236 (x : Prop) : f (g237 x) = f (g236 x)
@[simp] axiom s237 (x : Prop) : f (g238 x) = f (g237 x)
@[simp] axiom s238 (x : Prop) : f (g239 x) = f (g238 x)
@[simp] axiom s239 (x : Prop) : f (g240 x) = f (g239 x)
@[simp] axiom s240 (x : Prop) : f (g241 x) = f (g240 x)
@[simp] axiom s241 (x : Prop) : f (g242 x) = f (g241 x)
@[simp] axiom s242 (x : Prop) : f (g243 x) = f (g242 x)
@[simp] axiom s243 (x : Prop) : f (g244 x) = f (g243 x)
@[simp] axiom s244 (x : Prop) : f (g245 x) = f (g244 x)
@[simp] axiom s245 (x : Prop) : f (g246 x) = f (g245 x)
@[simp] axiom s246 (x : Prop) : f (g247 x) = f (g246 x)
@[simp] axiom s247 (x : Prop) : f (g248 x) = f (g247 x)
@[simp] axiom s248 (x : Prop) : f (g249 x) = f (g248 x)
@[simp] axiom s249 (x : Prop) : f (g250 x) = f (g249 x)
@[simp] axiom s250 (x : Prop) : f (g251 x) = f (g250 x)
@[simp] axiom s251 (x : Prop) : f (g252 x) = f (g251 x)
@[simp] axiom s252 (x : Prop) : f (g253 x) = f (g252 x)
@[simp] axiom s253 (x : Prop) : f (g254 x) = f (g253 x)
@[simp] axiom s254 (x : Prop) : f (g255 x) = f (g254 x)
@[simp] axiom s255 (x : Prop) : f (g256 x) = f (g255 x)
@[simp] axiom s256 (x : Prop) : f (g257 x) = f (g256 x)
@[simp] axiom s257 (x : Prop) : f (g258 x) = f (g257 x)
@[simp] axiom s258 (x : Prop) : f (g259 x) = f (g258 x)
@[simp] axiom s259 (x : Prop) : f (g260 x) = f (g259 x)
@[simp] axiom s260 (x : Prop) : f (g261 x) = f (g260 x)
@[simp] axiom s261 (x : Prop) : f (g262 x) = f (g261 x)
@[simp] axiom s262 (x : Prop) : f (g263 x) = f (g262 x)
@[simp] axiom s263 (x : Prop) : f (g264 x) = f (g263 x)
@[simp] axiom s264 (x : Prop) : f (g265 x) = f (g264 x)
@[simp] axiom s265 (x : Prop) : f (g266 x) = f (g265 x)
@[simp] axiom s266 (x : Prop) : f (g267 x) = f (g266 x)
@[simp] axiom s267 (x : Prop) : f (g268 x) = f (g267 x)
@[simp] axiom s268 (x : Prop) : f (g269 x) = f (g268 x)
@[simp] axiom s269 (x : Prop) : f (g270 x) = f (g269 x)
@[simp] axiom s270 (x : Prop) : f (g271 x) = f (g270 x)
@[simp] axiom s271 (x : Prop) : f (g272 x) = f (g271 x)
@[simp] axiom s272 (x : Prop) : f (g273 x) = f (g272 x)
@[simp] axiom s273 (x : Prop) : f (g274 x) = f (g273 x)
@[simp] axiom s274 (x : Prop) : f (g275 x) = f (g274 x)
@[simp] axiom s275 (x : Prop) : f (g276 x) = f (g275 x)
@[simp] axiom s276 (x : Prop) : f (g277 x) = f (g276 x)
@[simp] axiom s277 (x : Prop) : f (g278 x) = f (g277 x)
@[simp] axiom s278 (x : Prop) : f (g279 x) = f (g278 x)
@[simp] axiom s279 (x : Prop) : f (g280 x) = f (g279 x)
@[simp] axiom s280 (x : Prop) : f (g281 x) = f (g280 x)
@[simp] axiom s281 (x : Prop) : f (g282 x) = f (g281 x)
@[simp] axiom s282 (x : Prop) : f (g283 x) = f (g282 x)
@[simp] axiom s283 (x : Prop) : f (g284 x) = f (g283 x)
@[simp] axiom s284 (x : Prop) : f (g285 x) = f (g284 x)
@[simp] axiom s285 (x : Prop) : f (g286 x) = f (g285 x)
@[simp] axiom s286 (x : Prop) : f (g287 x) = f (g286 x)
@[simp] axiom s287 (x : Prop) : f (g288 x) = f (g287 x)
@[simp] axiom s288 (x : Prop) : f (g289 x) = f (g288 x)
@[simp] axiom s289 (x : Prop) : f (g290 x) = f (g289 x)
@[simp] axiom s290 (x : Prop) : f (g291 x) = f (g290 x)
@[simp] axiom s291 (x : Prop) : f (g292 x) = f (g291 x)
@[simp] axiom s292 (x : Prop) : f (g293 x) = f (g292 x)
@[simp] axiom s293 (x : Prop) : f (g294 x) = f (g293 x)
@[simp] axiom s294 (x : Prop) : f (g295 x) = f (g294 x)
@[simp] axiom s295 (x : Prop) : f (g296 x) = f (g295 x)
@[simp] axiom s296 (x : Prop) : f (g297 x) = f (g296 x)
@[simp] axiom s297 (x : Prop) : f (g298 x) = f (g297 x)
@[simp] axiom s298 (x : Prop) : f (g299 x) = f (g298 x)
@[simp] axiom s299 (x : Prop) : f (g300 x) = f (g299 x)
@[simp] axiom s300 (x : Prop) : f (g301 x) = f (g300 x)
@[simp] axiom s301 (x : Prop) : f (g302 x) = f (g301 x)
@[simp] axiom s302 (x : Prop) : f (g303 x) = f (g302 x)
@[simp] axiom s303 (x : Prop) : f (g304 x) = f (g303 x)
@[simp] axiom s304 (x : Prop) : f (g305 x) = f (g304 x)
@[simp] axiom s305 (x : Prop) : f (g306 x) = f (g305 x)
@[simp] axiom s306 (x : Prop) : f (g307 x) = f (g306 x)
@[simp] axiom s307 (x : Prop) : f (g308 x) = f (g307 x)
@[simp] axiom s308 (x : Prop) : f (g309 x) = f (g308 x)
@[simp] axiom s309 (x : Prop) : f (g310 x) = f (g309 x)
@[simp] axiom s310 (x : Prop) : f (g311 x) = f (g310 x)
@[simp] axiom s311 (x : Prop) : f (g312 x) = f (g311 x)
@[simp] axiom s312 (x : Prop) : f (g313 x) = f (g312 x)
@[simp] axiom s313 (x : Prop) : f (g314 x) = f (g313 x)
@[simp] axiom s314 (x : Prop) : f (g315 x) = f (g314 x)
@[simp] axiom s315 (x : Prop) : f (g316 x) = f (g315 x)
@[simp] axiom s316 (x : Prop) : f (g317 x) = f (g316 x)
@[simp] axiom s317 (x : Prop) : f (g318 x) = f (g317 x)
@[simp] axiom s318 (x : Prop) : f (g319 x) = f (g318 x)
@[simp] axiom s319 (x : Prop) : f (g320 x) = f (g319 x)
@[simp] axiom s320 (x : Prop) : f (g321 x) = f (g320 x)
@[simp] axiom s321 (x : Prop) : f (g322 x) = f (g321 x)
@[simp] axiom s322 (x : Prop) : f (g323 x) = f (g322 x)
@[simp] axiom s323 (x : Prop) : f (g324 x) = f (g323 x)
@[simp] axiom s324 (x : Prop) : f (g325 x) = f (g324 x)
@[simp] axiom s325 (x : Prop) : f (g326 x) = f (g325 x)
@[simp] axiom s326 (x : Prop) : f (g327 x) = f (g326 x)
@[simp] axiom s327 (x : Prop) : f (g328 x) = f (g327 x)
@[simp] axiom s328 (x : Prop) : f (g329 x) = f (g328 x)
@[simp] axiom s329 (x : Prop) : f (g330 x) = f (g329 x)
@[simp] axiom s330 (x : Prop) : f (g331 x) = f (g330 x)
@[simp] axiom s331 (x : Prop) : f (g332 x) = f (g331 x)
@[simp] axiom s332 (x : Prop) : f (g333 x) = f (g332 x)
@[simp] axiom s333 (x : Prop) : f (g334 x) = f (g333 x)
@[simp] axiom s334 (x : Prop) : f (g335 x) = f (g334 x)
@[simp] axiom s335 (x : Prop) : f (g336 x) = f (g335 x)
@[simp] axiom s336 (x : Prop) : f (g337 x) = f (g336 x)
@[simp] axiom s337 (x : Prop) : f (g338 x) = f (g337 x)
@[simp] axiom s338 (x : Prop) : f (g339 x) = f (g338 x)
@[simp] axiom s339 (x : Prop) : f (g340 x) = f (g339 x)
@[simp] axiom s340 (x : Prop) : f (g341 x) = f (g340 x)
@[simp] axiom s341 (x : Prop) : f (g342 x) = f (g341 x)
@[simp] axiom s342 (x : Prop) : f (g343 x) = f (g342 x)
@[simp] axiom s343 (x : Prop) : f (g344 x) = f (g343 x)
@[simp] axiom s344 (x : Prop) : f (g345 x) = f (g344 x)
@[simp] axiom s345 (x : Prop) : f (g346 x) = f (g345 x)
@[simp] axiom s346 (x : Prop) : f (g347 x) = f (g346 x)
@[simp] axiom s347 (x : Prop) : f (g348 x) = f (g347 x)
@[simp] axiom s348 (x : Prop) : f (g349 x) = f (g348 x)
@[simp] axiom s349 (x : Prop) : f (g350 x) = f (g349 x)
@[simp] axiom s350 (x : Prop) : f (g351 x) = f (g350 x)
@[simp] axiom s351 (x : Prop) : f (g352 x) = f (g351 x)
@[simp] axiom s352 (x : Prop) : f (g353 x) = f (g352 x)
@[simp] axiom s353 (x : Prop) : f (g354 x) = f (g353 x)
@[simp] axiom s354 (x : Prop) : f (g355 x) = f (g354 x)
@[simp] axiom s355 (x : Prop) : f (g356 x) = f (g355 x)
@[simp] axiom s356 (x : Prop) : f (g357 x) = f (g356 x)
@[simp] axiom s357 (x : Prop) : f (g358 x) = f (g357 x)
@[simp] axiom s358 (x : Prop) : f (g359 x) = f (g358 x)
@[simp] axiom s359 (x : Prop) : f (g360 x) = f (g359 x)
@[simp] axiom s360 (x : Prop) : f (g361 x) = f (g360 x)
@[simp] axiom s361 (x : Prop) : f (g362 x) = f (g361 x)
@[simp] axiom s362 (x : Prop) : f (g363 x) = f (g362 x)
@[simp] axiom s363 (x : Prop) : f (g364 x) = f (g363 x)
@[simp] axiom s364 (x : Prop) : f (g365 x) = f (g364 x)
@[simp] axiom s365 (x : Prop) : f (g366 x) = f (g365 x)
@[simp] axiom s366 (x : Prop) : f (g367 x) = f (g366 x)
@[simp] axiom s367 (x : Prop) : f (g368 x) = f (g367 x)
@[simp] axiom s368 (x : Prop) : f (g369 x) = f (g368 x)
@[simp] axiom s369 (x : Prop) : f (g370 x) = f (g369 x)
@[simp] axiom s370 (x : Prop) : f (g371 x) = f (g370 x)
@[simp] axiom s371 (x : Prop) : f (g372 x) = f (g371 x)
@[simp] axiom s372 (x : Prop) : f (g373 x) = f (g372 x)
@[simp] axiom s373 (x : Prop) : f (g374 x) = f (g373 x)
@[simp] axiom s374 (x : Prop) : f (g375 x) = f (g374 x)
@[simp] axiom s375 (x : Prop) : f (g376 x) = f (g375 x)
@[simp] axiom s376 (x : Prop) : f (g377 x) = f (g376 x)
@[simp] axiom s377 (x : Prop) : f (g378 x) = f (g377 x)
@[simp] axiom s378 (x : Prop) : f (g379 x) = f (g378 x)
@[simp] axiom s379 (x : Prop) : f (g380 x) = f (g379 x)
@[simp] axiom s380 (x : Prop) : f (g381 x) = f (g380 x)
@[simp] axiom s381 (x : Prop) : f (g382 x) = f (g381 x)
@[simp] axiom s382 (x : Prop) : f (g383 x) = f (g382 x)
@[simp] axiom s383 (x : Prop) : f (g384 x) = f (g383 x)
@[simp] axiom s384 (x : Prop) : f (g385 x) = f (g384 x)
@[simp] axiom s385 (x : Prop) : f (g386 x) = f (g385 x)
@[simp] axiom s386 (x : Prop) : f (g387 x) = f (g386 x)
@[simp] axiom s387 (x : Prop) : f (g388 x) = f (g387 x)
@[simp] axiom s388 (x : Prop) : f (g389 x) = f (g388 x)
@[simp] axiom s389 (x : Prop) : f (g390 x) = f (g389 x)
@[simp] axiom s390 (x : Prop) : f (g391 x) = f (g390 x)
@[simp] axiom s391 (x : Prop) : f (g392 x) = f (g391 x)
@[simp] axiom s392 (x : Prop) : f (g393 x) = f (g392 x)
@[simp] axiom s393 (x : Prop) : f (g394 x) = f (g393 x)
@[simp] axiom s394 (x : Prop) : f (g395 x) = f (g394 x)
@[simp] axiom s395 (x : Prop) : f (g396 x) = f (g395 x)
@[simp] axiom s396 (x : Prop) : f (g397 x) = f (g396 x)
@[simp] axiom s397 (x : Prop) : f (g398 x) = f (g397 x)
@[simp] axiom s398 (x : Prop) : f (g399 x) = f (g398 x)
@[simp] axiom s399 (x : Prop) : f (g400 x) = f (g399 x)
@[simp] axiom s400 (x : Prop) : f (g401 x) = f (g400 x)
@[simp] axiom s401 (x : Prop) : f (g402 x) = f (g401 x)
@[simp] axiom s402 (x : Prop) : f (g403 x) = f (g402 x)
@[simp] axiom s403 (x : Prop) : f (g404 x) = f (g403 x)
@[simp] axiom s404 (x : Prop) : f (g405 x) = f (g404 x)
@[simp] axiom s405 (x : Prop) : f (g406 x) = f (g405 x)
@[simp] axiom s406 (x : Prop) : f (g407 x) = f (g406 x)
@[simp] axiom s407 (x : Prop) : f (g408 x) = f (g407 x)
@[simp] axiom s408 (x : Prop) : f (g409 x) = f (g408 x)
@[simp] axiom s409 (x : Prop) : f (g410 x) = f (g409 x)
@[simp] axiom s410 (x : Prop) : f (g411 x) = f (g410 x)
@[simp] axiom s411 (x : Prop) : f (g412 x) = f (g411 x)
@[simp] axiom s412 (x : Prop) : f (g413 x) = f (g412 x)
@[simp] axiom s413 (x : Prop) : f (g414 x) = f (g413 x)
@[simp] axiom s414 (x : Prop) : f (g415 x) = f (g414 x)
@[simp] axiom s415 (x : Prop) : f (g416 x) = f (g415 x)
@[simp] axiom s416 (x : Prop) : f (g417 x) = f (g416 x)
@[simp] axiom s417 (x : Prop) : f (g418 x) = f (g417 x)
@[simp] axiom s418 (x : Prop) : f (g419 x) = f (g418 x)
@[simp] axiom s419 (x : Prop) : f (g420 x) = f (g419 x)
@[simp] axiom s420 (x : Prop) : f (g421 x) = f (g420 x)
@[simp] axiom s421 (x : Prop) : f (g422 x) = f (g421 x)
@[simp] axiom s422 (x : Prop) : f (g423 x) = f (g422 x)
@[simp] axiom s423 (x : Prop) : f (g424 x) = f (g423 x)
@[simp] axiom s424 (x : Prop) : f (g425 x) = f (g424 x)
@[simp] axiom s425 (x : Prop) : f (g426 x) = f (g425 x)
@[simp] axiom s426 (x : Prop) : f (g427 x) = f (g426 x)
@[simp] axiom s427 (x : Prop) : f (g428 x) = f (g427 x)
@[simp] axiom s428 (x : Prop) : f (g429 x) = f (g428 x)
@[simp] axiom s429 (x : Prop) : f (g430 x) = f (g429 x)
@[simp] axiom s430 (x : Prop) : f (g431 x) = f (g430 x)
@[simp] axiom s431 (x : Prop) : f (g432 x) = f (g431 x)
@[simp] axiom s432 (x : Prop) : f (g433 x) = f (g432 x)
@[simp] axiom s433 (x : Prop) : f (g434 x) = f (g433 x)
@[simp] axiom s434 (x : Prop) : f (g435 x) = f (g434 x)
@[simp] axiom s435 (x : Prop) : f (g436 x) = f (g435 x)
@[simp] axiom s436 (x : Prop) : f (g437 x) = f (g436 x)
@[simp] axiom s437 (x : Prop) : f (g438 x) = f (g437 x)
@[simp] axiom s438 (x : Prop) : f (g439 x) = f (g438 x)
@[simp] axiom s439 (x : Prop) : f (g440 x) = f (g439 x)
@[simp] axiom s440 (x : Prop) : f (g441 x) = f (g440 x)
@[simp] axiom s441 (x : Prop) : f (g442 x) = f (g441 x)
@[simp] axiom s442 (x : Prop) : f (g443 x) = f (g442 x)
@[simp] axiom s443 (x : Prop) : f (g444 x) = f (g443 x)
@[simp] axiom s444 (x : Prop) : f (g445 x) = f (g444 x)
@[simp] axiom s445 (x : Prop) : f (g446 x) = f (g445 x)
@[simp] axiom s446 (x : Prop) : f (g447 x) = f (g446 x)
@[simp] axiom s447 (x : Prop) : f (g448 x) = f (g447 x)
@[simp] axiom s448 (x : Prop) : f (g449 x) = f (g448 x)
@[simp] axiom s449 (x : Prop) : f (g450 x) = f (g449 x)
@[simp] axiom s450 (x : Prop) : f (g451 x) = f (g450 x)
@[simp] axiom s451 (x : Prop) : f (g452 x) = f (g451 x)
@[simp] axiom s452 (x : Prop) : f (g453 x) = f (g452 x)
@[simp] axiom s453 (x : Prop) : f (g454 x) = f (g453 x)
@[simp] axiom s454 (x : Prop) : f (g455 x) = f (g454 x)
@[simp] axiom s455 (x : Prop) : f (g456 x) = f (g455 x)
@[simp] axiom s456 (x : Prop) : f (g457 x) = f (g456 x)
@[simp] axiom s457 (x : Prop) : f (g458 x) = f (g457 x)
@[simp] axiom s458 (x : Prop) : f (g459 x) = f (g458 x)
@[simp] axiom s459 (x : Prop) : f (g460 x) = f (g459 x)
@[simp] axiom s460 (x : Prop) : f (g461 x) = f (g460 x)
@[simp] axiom s461 (x : Prop) : f (g462 x) = f (g461 x)
@[simp] axiom s462 (x : Prop) : f (g463 x) = f (g462 x)
@[simp] axiom s463 (x : Prop) : f (g464 x) = f (g463 x)
@[simp] axiom s464 (x : Prop) : f (g465 x) = f (g464 x)
@[simp] axiom s465 (x : Prop) : f (g466 x) = f (g465 x)
@[simp] axiom s466 (x : Prop) : f (g467 x) = f (g466 x)
@[simp] axiom s467 (x : Prop) : f (g468 x) = f (g467 x)
@[simp] axiom s468 (x : Prop) : f (g469 x) = f (g468 x)
@[simp] axiom s469 (x : Prop) : f (g470 x) = f (g469 x)
@[simp] axiom s470 (x : Prop) : f (g471 x) = f (g470 x)
@[simp] axiom s471 (x : Prop) : f (g472 x) = f (g471 x)
@[simp] axiom s472 (x : Prop) : f (g473 x) = f (g472 x)
@[simp] axiom s473 (x : Prop) : f (g474 x) = f (g473 x)
@[simp] axiom s474 (x : Prop) : f (g475 x) = f (g474 x)
@[simp] axiom s475 (x : Prop) : f (g476 x) = f (g475 x)
@[simp] axiom s476 (x : Prop) : f (g477 x) = f (g476 x)
@[simp] axiom s477 (x : Prop) : f (g478 x) = f (g477 x)
@[simp] axiom s478 (x : Prop) : f (g479 x) = f (g478 x)
@[simp] axiom s479 (x : Prop) : f (g480 x) = f (g479 x)
@[simp] axiom s480 (x : Prop) : f (g481 x) = f (g480 x)
@[simp] axiom s481 (x : Prop) : f (g482 x) = f (g481 x)
@[simp] axiom s482 (x : Prop) : f (g483 x) = f (g482 x)
@[simp] axiom s483 (x : Prop) : f (g484 x) = f (g483 x)
@[simp] axiom s484 (x : Prop) : f (g485 x) = f (g484 x)
@[simp] axiom s485 (x : Prop) : f (g486 x) = f (g485 x)
@[simp] axiom s486 (x : Prop) : f (g487 x) = f (g486 x)
@[simp] axiom s487 (x : Prop) : f (g488 x) = f (g487 x)
@[simp] axiom s488 (x : Prop) : f (g489 x) = f (g488 x)
@[simp] axiom s489 (x : Prop) : f (g490 x) = f (g489 x)
@[simp] axiom s490 (x : Prop) : f (g491 x) = f (g490 x)
@[simp] axiom s491 (x : Prop) : f (g492 x) = f (g491 x)
@[simp] axiom s492 (x : Prop) : f (g493 x) = f (g492 x)
@[simp] axiom s493 (x : Prop) : f (g494 x) = f (g493 x)
@[simp] axiom s494 (x : Prop) : f (g495 x) = f (g494 x)
@[simp] axiom s495 (x : Prop) : f (g496 x) = f (g495 x)
@[simp] axiom s496 (x : Prop) : f (g497 x) = f (g496 x)
@[simp] axiom s497 (x : Prop) : f (g498 x) = f (g497 x)
@[simp] axiom s498 (x : Prop) : f (g499 x) = f (g498 x)
def test (x : Prop) : f (g0 x) = f (g499 x) := by simp
#check test
|
7bc38a26bd432441f5469b47723d876d752467d6 | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/algebra/module/injective.lean | e260c06d61980fc4a4a9932d7b5b527631ace694 | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 16,516 | lean | /-
Copyright (c) 2022 Jujian Zhang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jujian Zhang
-/
import algebra.category.Module.abelian
import category_theory.preadditive.injective
import ring_theory.ideal.basic
/-!
# Injective modules
## Main definitions
* `module.injective`: an `R`-module `Q` is injective if and only if every injective `R`-linear
map descends to a linear map to `Q`, i.e. in the following diagram, if `f` is injective then there
is an `R`-linear map `h : Y ⟶ Q` such that `g = h ∘ f`
```
X --- f ---> Y
|
| g
v
Q
```
* `module.Baer`: an `R`-module `Q` satisfies Baer's criterion if any `R`-linear map from an
`ideal R` extends to an `R`-linear map `R ⟶ Q`
## Main statements
* `module.Baer.criterion`: an `R`-module is injective if it is Baer.
-/
noncomputable theory
universes u v
variables (R : Type u) [ring R] (Q : Type (max u v)) [add_comm_group Q] [module R Q]
/--An `R`-module `Q` is injective if and only if every injective `R`-linear map descends to a linear
map to `Q`, i.e. in the following diagram, if `f` is injective then there is an `R`-linear map
`h : Y ⟶ Q` such that `g = h ∘ f`
```
X --- f ---> Y
|
| g
v
Q
```
-/
class module.injective : Prop :=
(out : ∀ (X Y : Type (max u v)) [add_comm_group X] [add_comm_group Y] [module R X] [module R Y]
(f : X →ₗ[R] Y) (hf : function.injective f) (g : X →ₗ[R] Q),
∃ (h : Y →ₗ[R] Q), ∀ x, h (f x) = g x)
lemma module.injective_object_of_injective_module [module.injective.{u v} R Q] :
category_theory.injective.{max u v} (⟨Q⟩ : Module.{max u v} R) :=
{ factors := λ X Y g f mn, begin
rcases module.injective.out X Y f ((Module.mono_iff_injective f).mp mn) g with ⟨h, eq1⟩,
exact ⟨h, linear_map.ext eq1⟩,
end }
lemma module.injective_module_of_injective_object
[category_theory.injective.{max u v} (⟨Q⟩ : Module.{max u v} R)] :
module.injective.{u v} R Q :=
{ out := λ X Y ins1 ins2 ins3 ins4 f hf g, begin
resetI,
rcases @category_theory.injective.factors (Module R) _ ⟨Q⟩ _ ⟨X⟩ ⟨Y⟩ g f
((Module.mono_iff_injective _).mpr hf) with ⟨h, rfl⟩,
exact ⟨h, λ x, rfl⟩
end }
lemma module.injective_iff_injective_object :
module.injective.{u v} R Q ↔ category_theory.injective.{max u v} (⟨Q⟩ : Module.{max u v} R) :=
⟨λ h, @@module.injective_object_of_injective_module R _ Q _ _ h,
λ h, @@module.injective_module_of_injective_object R _ Q _ _ h⟩
/--An `R`-module `Q` satisfies Baer's criterion if any `R`-linear map from an `ideal R` extends to
an `R`-linear map `R ⟶ Q`-/
def module.Baer : Prop := ∀ (I : ideal R) (g : I →ₗ[R] Q), ∃ (g' : R →ₗ[R] Q),
∀ (x : R) (mem : x ∈ I), g' x = g ⟨x, mem⟩
namespace module.Baer
variables {R Q} {M N : Type (max u v)} [add_comm_group M] [add_comm_group N]
variables [module R M] [module R N] (i : M →ₗ[R] N) (f : M →ₗ[R] Q)
/-- If we view `M` as a submodule of `N` via the injective linear map `i : M ↪ N`, then a submodule
between `M` and `N` is a submodule `N'` of `N`. To prove Baer's criterion, we need to consider
pairs of `(N', f')` such that `M ≤ N' ≤ N` and `f'` extends `f`. -/
structure extension_of extends linear_pmap R N Q :=
(le : i.range ≤ domain)
(is_extension : ∀ (m : M), f m = to_linear_pmap ⟨i m, le ⟨m, rfl⟩⟩)
section ext
variables {i f}
@[ext] lemma extension_of.ext {a b : extension_of i f}
(domain_eq : a.domain = b.domain)
(to_fun_eq : ∀ ⦃x : a.domain⦄ ⦃y : b.domain⦄,
(x : N) = y → a.to_linear_pmap x = b.to_linear_pmap y) : a = b :=
begin
rcases a with ⟨a, a_le, e1⟩,
rcases b with ⟨b, b_le, e2⟩,
congr,
exact linear_pmap.ext domain_eq to_fun_eq,
end
lemma extension_of.ext_iff {a b : extension_of i f} :
a = b ↔ ∃ (domain_eq : a.domain = b.domain),
∀ ⦃x : a.domain⦄ ⦃y : b.domain⦄, (x : N) = y → a.to_linear_pmap x = b.to_linear_pmap y :=
⟨λ r, r ▸ ⟨rfl, λ x y h, congr_arg a.to_fun $ by exact_mod_cast h⟩,
λ ⟨h1, h2⟩, extension_of.ext h1 h2⟩
end ext
instance : has_inf (extension_of i f) :=
{ inf := λ X1 X2,
{ le := λ x hx, (begin
rcases hx with ⟨x, rfl⟩,
refine ⟨X1.le (set.mem_range_self _), X2.le (set.mem_range_self _), _⟩,
rw [← X1.is_extension x, ← X2.is_extension x],
end : x ∈ X1.to_linear_pmap.eq_locus X2.to_linear_pmap),
is_extension := λ m, X1.is_extension _,
.. (X1.to_linear_pmap ⊓ X2.to_linear_pmap)} }
instance : semilattice_inf (extension_of i f) :=
function.injective.semilattice_inf extension_of.to_linear_pmap
(λ X Y h, extension_of.ext (by rw h) $ λ x y h', by { induction h, congr, exact_mod_cast h' }) $
λ X Y, linear_pmap.ext rfl $ λ x y h, by { congr, exact_mod_cast h }
variables {R i f}
lemma chain_linear_pmap_of_chain_extension_of
{c : set (extension_of i f)} (hchain : is_chain (≤) c) :
(is_chain (≤) $ (λ x : extension_of i f, x.to_linear_pmap) '' c) :=
begin
rintro _ ⟨a, a_mem, rfl⟩ _ ⟨b, b_mem, rfl⟩ neq,
exact hchain a_mem b_mem (ne_of_apply_ne _ neq),
end
/-- The maximal element of every nonempty chain of `extension_of i f`. -/
def extension_of.max {c : set (extension_of i f)} (hchain : is_chain (≤) c)
(hnonempty : c.nonempty) :
extension_of i f :=
{ le := le_trans hnonempty.some.le $ (linear_pmap.le_Sup _ $ (set.mem_image _ _ _).mpr
⟨hnonempty.some, hnonempty.some_spec, rfl⟩).1,
is_extension := λ m, begin
refine eq.trans (hnonempty.some.is_extension m) _,
symmetry,
generalize_proofs _ h0 h1,
exact linear_pmap.Sup_apply
(is_chain.directed_on $ chain_linear_pmap_of_chain_extension_of hchain)
((set.mem_image _ _ _).mpr ⟨hnonempty.some, hnonempty.some_spec, rfl⟩) ⟨i m, h1⟩,
end,
..linear_pmap.Sup _ (is_chain.directed_on $ chain_linear_pmap_of_chain_extension_of hchain) }
lemma extension_of.le_max {c : set (extension_of i f)} (hchain : is_chain (≤) c)
(hnonempty : c.nonempty) (a : extension_of i f) (ha : a ∈ c) :
a ≤ extension_of.max hchain hnonempty :=
linear_pmap.le_Sup (is_chain.directed_on $ chain_linear_pmap_of_chain_extension_of hchain) $
(set.mem_image _ _ _).mpr ⟨a, ha, rfl⟩
variables (i f) [fact $ function.injective i]
instance extension_of.inhabited : inhabited (extension_of i f) :=
{ default :=
{ domain := i.range,
to_fun :=
{ to_fun := λ x, f x.2.some,
map_add' := λ x y, begin
have eq1 : _ + _ = (x + y).1 := congr_arg2 (+) x.2.some_spec y.2.some_spec,
rw [← map_add, ← (x + y).2.some_spec] at eq1,
rw [← fact.out (function.injective i) eq1, map_add],
end,
map_smul' := λ r x, begin
have eq1 : r • _ = (r • x).1 := congr_arg ((•) r) x.2.some_spec,
rw [← linear_map.map_smul, ← (r • x).2.some_spec] at eq1,
rw [ring_hom.id_apply, ← fact.out (function.injective i) eq1, linear_map.map_smul],
end },
le := le_refl _,
is_extension := λ m, begin
simp only [linear_pmap.mk_apply, linear_map.coe_mk],
congr,
exact fact.out (function.injective i) (⟨i m, ⟨_, rfl⟩⟩ : i.range).2.some_spec.symm,
end } }
/-- Since every nonempty chain has a maximal element, by Zorn's lemma, there is a maximal
`extension_of i f`. -/
def extension_of_max : extension_of i f :=
(@zorn_nonempty_partial_order (extension_of i f) _ ⟨inhabited.default⟩
(λ c hchain hnonempty,
⟨extension_of.max hchain hnonempty, extension_of.le_max hchain hnonempty⟩)).some
lemma extension_of_max_is_max :
∀ (a : extension_of i f), extension_of_max i f ≤ a → a = extension_of_max i f :=
(@zorn_nonempty_partial_order (extension_of i f) _ ⟨inhabited.default⟩
((λ c hchain hnonempty,
⟨extension_of.max hchain hnonempty, extension_of.le_max hchain hnonempty⟩))).some_spec
variables {f}
private lemma extension_of_max_adjoin.aux1
{y : N}
(x : (extension_of_max i f).domain ⊔ submodule.span R {y}) :
∃ (a : (extension_of_max i f).domain) (b : R), x.1 = a.1 + b • y :=
begin
have mem1 : x.1 ∈ (_ : set _) := x.2,
rw submodule.coe_sup at mem1,
rcases mem1 with ⟨a, b, a_mem, (b_mem : b ∈ (submodule.span R _ : submodule R N)), eq1⟩,
rw submodule.mem_span_singleton at b_mem,
rcases b_mem with ⟨z, eq2⟩,
exact ⟨⟨a, a_mem⟩, z, by rw [← eq1, ← eq2]⟩,
end
/--If `x ∈ M ⊔ ⟨y⟩`, then `x = m + r • y`, `fst` pick an arbitrary such `m`.-/
def extension_of_max_adjoin.fst
{y : N} (x : (extension_of_max i f).domain ⊔ submodule.span R {y}) :
(extension_of_max i f).domain :=
(extension_of_max_adjoin.aux1 i x).some
/--If `x ∈ M ⊔ ⟨y⟩`, then `x = m + r • y`, `snd` pick an arbitrary such `r`.-/
def extension_of_max_adjoin.snd
{y : N} (x : (extension_of_max i f).domain ⊔ submodule.span R {y}) : R :=
(extension_of_max_adjoin.aux1 i x).some_spec.some
lemma extension_of_max_adjoin.eqn
{y : N} (x : (extension_of_max i f).domain ⊔ submodule.span R {y}) :
↑x = ↑(extension_of_max_adjoin.fst i x) + (extension_of_max_adjoin.snd i x) • y :=
(extension_of_max_adjoin.aux1 i x).some_spec.some_spec
variables (f)
/--the ideal `I = {r | r • y ∈ N}`-/
def extension_of_max_adjoin.ideal (y : N) :
ideal R :=
(extension_of_max i f).domain.comap (linear_map.id.smul_right y)
/--A linear map `I ⟶ Q` by `x ↦ f' (x • y)` where `f'` is the maximal extension-/
def extension_of_max_adjoin.ideal_to (y : N) :
extension_of_max_adjoin.ideal i f y →ₗ[R] Q :=
{ to_fun := λ z, (extension_of_max i f).to_linear_pmap ⟨(↑z : R) • y, z.prop⟩,
map_add' := λ z1 z2, by simp [← (extension_of_max i f).to_linear_pmap.map_add, add_smul],
map_smul' := λ z1 z2, by simp [← (extension_of_max i f).to_linear_pmap.map_smul, mul_smul]; refl }
/-- Since we assumed `Q` being Baer, the linear map `x ↦ f' (x • y) : I ⟶ Q` extends to `R ⟶ Q`,
call this extended map `φ`-/
def extension_of_max_adjoin.extend_ideal_to (h : module.Baer R Q) (y : N) : R →ₗ[R] Q :=
(h (extension_of_max_adjoin.ideal i f y) (extension_of_max_adjoin.ideal_to i f y)).some
lemma extension_of_max_adjoin.extend_ideal_to_is_extension (h : module.Baer R Q) (y : N) :
∀ (x : R) (mem : x ∈ extension_of_max_adjoin.ideal i f y),
extension_of_max_adjoin.extend_ideal_to i f h y x =
extension_of_max_adjoin.ideal_to i f y ⟨x, mem⟩ :=
(h (extension_of_max_adjoin.ideal i f y) (extension_of_max_adjoin.ideal_to i f y)).some_spec
lemma extension_of_max_adjoin.extend_ideal_to_wd' (h : module.Baer R Q) {y : N} (r : R)
(eq1 : r • y = 0) :
extension_of_max_adjoin.extend_ideal_to i f h y r = 0 :=
begin
rw extension_of_max_adjoin.extend_ideal_to_is_extension i f h y r
(by rw eq1; exact submodule.zero_mem _ : r • y ∈ _),
simp only [extension_of_max_adjoin.ideal_to, linear_map.coe_mk, eq1, subtype.coe_mk,
← add_submonoid_class.zero_def, (extension_of_max i f).to_linear_pmap.map_zero]
end
lemma extension_of_max_adjoin.extend_ideal_to_wd (h : module.Baer R Q) {y : N} (r r' : R)
(eq1 : r • y = r' • y) :
extension_of_max_adjoin.extend_ideal_to i f h y r =
extension_of_max_adjoin.extend_ideal_to i f h y r' :=
begin
rw [← sub_eq_zero, ← map_sub],
convert extension_of_max_adjoin.extend_ideal_to_wd' i f h (r - r') _,
rw [sub_smul, sub_eq_zero, eq1],
end
lemma extension_of_max_adjoin.extend_ideal_to_eq (h : module.Baer R Q) {y : N} (r : R)
(hr : r • y ∈ (extension_of_max i f).domain) :
extension_of_max_adjoin.extend_ideal_to i f h y r =
(extension_of_max i f).to_linear_pmap ⟨r • y, hr⟩ :=
by simp only [extension_of_max_adjoin.extend_ideal_to_is_extension i f h _ _ hr,
extension_of_max_adjoin.ideal_to, linear_map.coe_mk, subtype.coe_mk]
/--We can finally define a linear map `M ⊔ ⟨y⟩ ⟶ Q` by `x + r • y ↦ f x + φ r`
-/
def extension_of_max_adjoin.extension_to_fun (h : module.Baer R Q)
{y : N} :
(extension_of_max i f).domain ⊔ submodule.span R {y} → Q :=
λ x, (extension_of_max i f).to_linear_pmap (extension_of_max_adjoin.fst i x) +
extension_of_max_adjoin.extend_ideal_to i f h y (extension_of_max_adjoin.snd i x)
lemma extension_of_max_adjoin.extension_to_fun_wd (h : module.Baer R Q)
{y : N} (x : (extension_of_max i f).domain ⊔ submodule.span R {y})
(a : (extension_of_max i f).domain) (r : R)
(eq1 : ↑x = ↑a + r • y) :
extension_of_max_adjoin.extension_to_fun i f h x =
(extension_of_max i f).to_linear_pmap a +
extension_of_max_adjoin.extend_ideal_to i f h y r :=
begin
cases a with a ha,
rw subtype.coe_mk at eq1,
have eq2 : (extension_of_max_adjoin.fst i x - a : N) = (r - extension_of_max_adjoin.snd i x) • y,
{ rwa [extension_of_max_adjoin.eqn, ← sub_eq_zero, ←sub_sub_sub_eq,
sub_eq_zero, ← sub_smul] at eq1 },
have eq3 := extension_of_max_adjoin.extend_ideal_to_eq i f h (r - extension_of_max_adjoin.snd i x)
(by rw ← eq2; exact submodule.sub_mem _ (extension_of_max_adjoin.fst i x).2 ha),
simp only [map_sub, sub_smul, sub_eq_iff_eq_add] at eq3,
unfold extension_of_max_adjoin.extension_to_fun,
rw [eq3, ← add_assoc, ← (extension_of_max i f).to_linear_pmap.map_add, add_mem_class.mk_add_mk],
congr,
ext,
rw [subtype.coe_mk, add_sub, ← eq1],
exact eq_sub_of_add_eq (extension_of_max_adjoin.eqn _ _).symm
end
/--The linear map `M ⊔ ⟨y⟩ ⟶ Q` by `x + r • y ↦ f x + φ r` is an extension of `f`-/
def extension_of_max_adjoin (h : module.Baer R Q) (y : N) :
extension_of i f :=
{ domain := (extension_of_max i f).domain ⊔ submodule.span R {y},
le := le_trans (extension_of_max i f).le le_sup_left,
to_fun :=
{ to_fun := extension_of_max_adjoin.extension_to_fun i f h,
map_add' := λ a b, begin
have eq1 : ↑a + ↑b =
↑((extension_of_max_adjoin.fst i a) + (extension_of_max_adjoin.fst i b)) +
(extension_of_max_adjoin.snd i a + extension_of_max_adjoin.snd i b) • y,
{ rw [extension_of_max_adjoin.eqn, extension_of_max_adjoin.eqn, add_smul],
abel, },
rw [extension_of_max_adjoin.extension_to_fun_wd i f h (a + b) _ _ eq1,
linear_pmap.map_add, map_add],
unfold extension_of_max_adjoin.extension_to_fun,
abel,
end,
map_smul' := λ r a, begin
rw [ring_hom.id_apply],
have eq1 : r • ↑a = ↑(r • extension_of_max_adjoin.fst i a) +
(r • extension_of_max_adjoin.snd i a) • y,
{ rw [extension_of_max_adjoin.eqn, smul_add, smul_eq_mul, mul_smul],
refl, },
rw [extension_of_max_adjoin.extension_to_fun_wd i f h (r • a) _ _ eq1,
linear_map.map_smul, linear_pmap.map_smul, ← smul_add],
congr',
end },
is_extension := λ m, begin
simp only [linear_pmap.mk_apply, linear_map.coe_mk],
rw [(extension_of_max i f).is_extension, extension_of_max_adjoin.extension_to_fun_wd i f h
_ ⟨i m, _⟩ 0 _, map_zero, add_zero],
simp,
end }
lemma extension_of_max_le (h : module.Baer R Q) {y : N} :
extension_of_max i f ≤ extension_of_max_adjoin i f h y :=
⟨le_sup_left, λ x x' EQ, begin
symmetry,
change extension_of_max_adjoin.extension_to_fun i f h _ = _,
rw [extension_of_max_adjoin.extension_to_fun_wd i f h x' x 0 (by simp [EQ]), map_zero, add_zero],
end⟩
lemma extension_of_max_to_submodule_eq_top (h : module.Baer R Q) :
(extension_of_max i f).domain = ⊤ :=
begin
refine submodule.eq_top_iff'.mpr (λ y, _),
rw [← extension_of_max_is_max i f _ (extension_of_max_le i f h), extension_of_max_adjoin,
submodule.mem_sup],
exact ⟨0, submodule.zero_mem _, y, submodule.mem_span_singleton_self _, zero_add _⟩
end
/--**Baer's criterion** for injective module : a Baer module is an injective module, i.e. if every
linear map from an ideal can be extended, then the module is injective.-/
protected theorem injective (h : module.Baer R Q) :
module.injective R Q :=
{ out := λ X Y ins1 ins2 ins3 ins4 i hi f, begin
haveI : fact (function.injective i) := ⟨hi⟩,
exact ⟨{ to_fun := λ y, (extension_of_max i f).to_linear_pmap
⟨y, (extension_of_max_to_submodule_eq_top i f h).symm ▸ trivial⟩,
map_add' := λ x y, by { rw ← linear_pmap.map_add, congr, },
map_smul' := λ r x, by { rw ← linear_pmap.map_smul, congr } },
λ x, ((extension_of_max i f).is_extension x).symm⟩,
end }
end module.Baer
|
d78d88705e5e2a4a8b8e4eb59f95d3e24251a113 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/category/Module/kernels.lean | 80d2b4986e2f72d9de2598f9fa878b6a0a8530b9 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 4,291 | 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 algebra.category.Module.epi_mono
import category_theory.concrete_category.elementwise
/-!
# The concrete (co)kernels in the category of modules are (co)kernels in the categorical sense.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
open category_theory
open category_theory.limits
universes u v
namespace Module
variables {R : Type u} [ring R]
section
variables {M N : Module.{v} R} (f : M ⟶ N)
/-- The kernel cone induced by the concrete kernel. -/
def kernel_cone : kernel_fork f :=
kernel_fork.of_ι (as_hom f.ker.subtype) $ by tidy
/-- The kernel of a linear map is a kernel in the categorical sense. -/
def kernel_is_limit : is_limit (kernel_cone f) :=
fork.is_limit.mk _
(λ s, linear_map.cod_restrict f.ker (fork.ι s) (λ c, linear_map.mem_ker.2 $
by { rw [←@function.comp_apply _ _ _ f (fork.ι s) c, ←coe_comp, fork.condition,
has_zero_morphisms.comp_zero (fork.ι s) N], refl }))
(λ s, linear_map.subtype_comp_cod_restrict _ _ _)
(λ s m h, linear_map.ext $ λ x, subtype.ext_iff_val.2 (by simpa [←h]))
/-- The cokernel cocone induced by the projection onto the quotient. -/
def cokernel_cocone : cokernel_cofork f :=
cokernel_cofork.of_π (as_hom f.range.mkq) $ linear_map.range_mkq_comp _
/-- The projection onto the quotient is a cokernel in the categorical sense. -/
def cokernel_is_colimit : is_colimit (cokernel_cocone f) :=
cofork.is_colimit.mk _
(λ s, f.range.liftq (cofork.π s) $ linear_map.range_le_ker_iff.2 $ cokernel_cofork.condition s)
(λ s, f.range.liftq_mkq (cofork.π s) _)
(λ s m h,
begin
haveI : epi (as_hom f.range.mkq) := (epi_iff_range_eq_top _).mpr (submodule.range_mkq _),
apply (cancel_epi (as_hom f.range.mkq)).1,
convert h,
exact submodule.liftq_mkq _ _ _
end)
end
/-- The category of R-modules has kernels, given by the inclusion of the kernel submodule. -/
lemma has_kernels_Module : has_kernels (Module R) :=
⟨λ X Y f, has_limit.mk ⟨_, kernel_is_limit f⟩⟩
/-- The category or R-modules has cokernels, given by the projection onto the quotient. -/
lemma has_cokernels_Module : has_cokernels (Module R) :=
⟨λ X Y f, has_colimit.mk ⟨_, cokernel_is_colimit f⟩⟩
open_locale Module
local attribute [instance] has_kernels_Module
local attribute [instance] has_cokernels_Module
variables {G H : Module.{v} R} (f : G ⟶ H)
/--
The categorical kernel of a morphism in `Module`
agrees with the usual module-theoretical kernel.
-/
noncomputable def kernel_iso_ker {G H : Module.{v} R} (f : G ⟶ H) :
kernel f ≅ Module.of R (f.ker) :=
limit.iso_limit_cone ⟨_, kernel_is_limit f⟩
-- We now show this isomorphism commutes with the inclusion of the kernel into the source.
@[simp, elementwise] lemma kernel_iso_ker_inv_kernel_ι :
(kernel_iso_ker f).inv ≫ kernel.ι f = f.ker.subtype :=
limit.iso_limit_cone_inv_π _ _
@[simp, elementwise] lemma kernel_iso_ker_hom_ker_subtype :
(kernel_iso_ker f).hom ≫ f.ker.subtype = kernel.ι f :=
is_limit.cone_point_unique_up_to_iso_inv_comp _ (limit.is_limit _) walking_parallel_pair.zero
/--
The categorical cokernel of a morphism in `Module`
agrees with the usual module-theoretical quotient.
-/
noncomputable def cokernel_iso_range_quotient {G H : Module.{v} R} (f : G ⟶ H) :
cokernel f ≅ Module.of R (H ⧸ f.range) :=
colimit.iso_colimit_cocone ⟨_, cokernel_is_colimit f⟩
-- We now show this isomorphism commutes with the projection of target to the cokernel.
@[simp, elementwise] lemma cokernel_π_cokernel_iso_range_quotient_hom :
cokernel.π f ≫ (cokernel_iso_range_quotient f).hom = f.range.mkq :=
by { convert colimit.iso_colimit_cocone_ι_hom _ _; refl, }
@[simp, elementwise] lemma range_mkq_cokernel_iso_range_quotient_inv :
↿f.range.mkq ≫ (cokernel_iso_range_quotient f).inv = cokernel.π f :=
by { convert colimit.iso_colimit_cocone_ι_inv ⟨_, cokernel_is_colimit f⟩ _; refl, }
lemma cokernel_π_ext {M N : Module.{u} R} (f : M ⟶ N) {x y : N} (m : M) (w : x = y + f m) :
cokernel.π f x = cokernel.π f y :=
by { subst w, simp, }
end Module
|
0ff30504aff5e42a1c5b247b9982cbd21c23b7c6 | 96e44fc78cabfc9d646dc37d0e756189b6b79181 | /library/init/meta/expr.lean | 6a8217501699470c7c1df36f24f2adb6acd14433 | [
"Apache-2.0"
] | permissive | TwoFX/lean | 23c73c10a340f5a381f6abf27a27f53f1fb7e2e3 | 7e3f336714055869690b7309b6bb651fbc67e76e | refs/heads/master | 1,612,504,908,183 | 1,594,641,622,000 | 1,594,641,622,000 | 243,750,847 | 0 | 0 | Apache-2.0 | 1,582,890,661,000 | 1,582,890,661,000 | null | UTF-8 | Lean | false | false | 22,435 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.meta.level init.control.monad init.meta.rb_map
universes u v
open native
/-- Column and line position in a Lean source file. -/
structure pos :=
(line : nat)
(column : nat)
instance : decidable_eq pos
| ⟨l₁, c₁⟩ ⟨l₂, c₂⟩ := if h₁ : l₁ = l₂ then
if h₂ : c₁ = c₂ then is_true (eq.rec_on h₁ (eq.rec_on h₂ rfl))
else is_false (λ contra, pos.no_confusion contra (λ e₁ e₂, absurd e₂ h₂))
else is_false (λ contra, pos.no_confusion contra (λ e₁ e₂, absurd e₁ h₁))
meta instance : has_to_format pos :=
⟨λ ⟨l, c⟩, "⟨" ++ l ++ ", " ++ c ++ "⟩"⟩
/-- Auxiliary annotation for binders (Lambda and Pi).
This information is only used for elaboration.
The difference between `{}` and `⦃⦄` is how implicit arguments are treated that are *not* followed by explicit arguments.
`{}` arguments are applied eagerly, while `⦃⦄` arguments are left partially applied:
```lean
def foo {x : ℕ} : ℕ := x
def bar ⦃x : ℕ⦄ : ℕ := x
#check foo -- foo : ℕ
#check bar -- bar : Π ⦃x : ℕ⦄, ℕ
```
-/
inductive binder_info
/- `(x : α)` -/
| default
/- `{x : α}` -/
| implicit
/- `⦃x:α⦄` -/
| strict_implicit
/- `[x : α]`. Should be inferred with typeclass resolution. -/
| inst_implicit
/- Auxiliary internal attribute used to mark local constants representing recursive functions
in recursive equations and `match` statements. -/
| aux_decl
instance : has_repr binder_info :=
⟨λ bi, match bi with
| binder_info.default := "default"
| binder_info.implicit := "implicit"
| binder_info.strict_implicit := "strict_implicit"
| binder_info.inst_implicit := "inst_implicit"
| binder_info.aux_decl := "aux_decl"
end⟩
/-- Macros are basically "promises" to build an expr by some C++ code, you can't build them in Lean.
You can unfold a macro and force it to evaluate.
They are used for
- `sorry`.
- Term placeholders (`_`) in `pexpr`s.
- Expression annotations. See `expr.is_annotation`.
- Meta-recursive calls. Eg:
```
meta def Y : (α → α) → α | f := f (Y f)
```
The `Y` that appears in `f (Y f)` is a macro.
- Builtin projections:
```
structure foo := (mynat : ℕ)
#print foo.mynat
-- @[reducible]
-- def foo.mynat : foo → ℕ :=
-- λ (c : foo), [foo.mynat c]
```
The thing in square brackets is a macro.
- Ephemeral structures inside certain specialised C++ implemented tactics.
-/
meta constant macro_def : Type
/-- An expression. eg ```(4+5)```.
The `elab` flag is indicates whether the `expr` has been elaborated and doesn't contain any placeholder macros.
For example the equality `x = x` is represented in `expr ff` as ``app (app (const `eq _) x) x`` while in `expr tt` it is represented as ``app (app (app (const `eq _) t) x) x`` (one more argument).
The VM replaces instances of this datatype with the C++ implementation. -/
meta inductive expr (elaborated : bool := tt)
/- A bound variable with a de-Bruijn index. -/
| var : nat → expr
/- A type universe: `Sort u` -/
| sort : level → expr
/- A global constant. These include definitions, constants and inductive type stuff present
in the environment as well as hard-coded definitions. -/
| const : name → list level → expr
/- [WARNING] Do not trust the types for `mvar` and `local_const`,
they are sometimes dummy values. Use `tactic.infer_type` instead. -/
/- An `mvar` is a 'hole' yet to be filled in by the elaborator or tactic state. -/
| mvar (unique : name) (pretty : name) (type : expr) : expr
/- A local constant. For example, if our tactic state was `h : P ⊢ Q`, `h` would be a local constant. -/
| local_const (unique : name) (pretty : name) (bi : binder_info) (type : expr) : expr
/- Function application. -/
| app : expr → expr → expr
/- Lambda abstraction. eg ```(λ a : α, x)`` -/
| lam (var_name : name) (bi : binder_info) (var_type : expr) (body : expr) : expr
/- Pi type constructor. eg ```(Π a : α, x)`` and ```(α → β)`` -/
| pi (var_name : name) (bi : binder_info) (var_type : expr) (body : expr) : expr
/- An explicit let binding. -/
| elet (var_name : name) (type : expr) (assignment : expr) (body : expr) : expr
/- A macro, see the docstring for `macro_def`.
The list of expressions are local constants and metavariables that the macro depends on.
-/
| macro : macro_def → list expr → expr
variable {elab : bool}
meta instance : inhabited expr := ⟨expr.sort level.zero⟩
/-- Get the name of the macro definition. -/
meta constant expr.macro_def_name (d : macro_def) : name
meta def expr.mk_var (n : nat) : expr := expr.var n
/-- Expressions can be annotated using an annotation macro during compilation.
For example, a `have x:X, from p, q` expression will be compiled to `(λ x:X,q)(p)`, but nested in an annotation macro with the name `"have"`.
These annotations have no real semantic meaning, but are useful for helping Lean's pretty printer. -/
meta constant expr.is_annotation : expr elab → option (name × expr elab)
/-- Remove all macro annotations from the given `expr`. -/
meta def expr.erase_annotations : expr elab → expr elab
| e :=
match e.is_annotation with
| some (_, a) := expr.erase_annotations a
| none := e
end
/-- Compares expressions, including binder names. -/
meta constant expr.has_decidable_eq : decidable_eq expr
attribute [instance] expr.has_decidable_eq
/-- Compares expressions while ignoring binder names. -/
meta constant expr.alpha_eqv : expr → expr → bool
notation a ` =ₐ `:50 b:50 := expr.alpha_eqv a b = bool.tt
protected meta constant expr.to_string : expr elab → string
meta instance : has_to_string (expr elab) := ⟨expr.to_string⟩
meta instance : has_to_format (expr elab) := ⟨λ e, e.to_string⟩
/-- Coercion for letting users write (f a) instead of (expr.app f a) -/
meta instance : has_coe_to_fun (expr elab) :=
{ F := λ e, expr elab → expr elab, coe := λ e, expr.app e }
/-- Each expression created by Lean carries a hash.
This is calculated upon creation of the expression.
Two structurally equal expressions will have the same hash. -/
meta constant expr.hash : expr → nat
/-- Compares expressions, ignoring binder names, and sorting by hash. -/
meta constant expr.lt : expr → expr → bool
/-- Compares expressions, ignoring binder names. -/
meta constant expr.lex_lt : expr → expr → bool
/-- `expr.fold e a f`: Traverses each subexpression of `e`. The `nat` passed to the folder `f` is the binder depth. -/
meta constant expr.fold {α : Type} : expr → α → (expr → nat → α → α) → α
/-- `expr.replace e f`
Traverse over an expr `e` with a function `f` which can decide to replace subexpressions or not.
For each subexpression `s` in the expression tree, `f s n` is called where `n` is how many binders are present above the given subexpression `s`.
If `f s n` returns `none`, the children of `s` will be traversed.
Otherwise if `some s'` is returned, `s'` will replace `s` and this subexpression will not be traversed further.
-/
meta constant expr.replace : expr → (expr → nat → option expr) → expr
/-- `abstract_local e n` replaces each instance of the local constant with unique (not pretty) name `n` in `e` with a de-Bruijn variable. -/
meta constant expr.abstract_local : expr → name → expr
/-- Multi version of `abstract_local`. Note that the given expression will only be traversed once, so this is not the same as `list.foldl expr.abstract_local`.-/
meta constant expr.abstract_locals : expr → list name → expr
/-- `abstract e x` Abstracts the expression `e` over the local constant `x`. -/
meta def expr.abstract : expr → expr → expr
| e (expr.local_const n m bi t) := e.abstract_local n
| e _ := e
/-- Expressions depend on `level`s, and these may depend on universe parameters which have names.
`instantiate_univ_params e [(n₁,l₁), ...]` will traverse `e` and replace any universe parameters with name `nᵢ` with the corresponding level `lᵢ`. -/
meta constant expr.instantiate_univ_params : expr → list (name × level) → expr
/-- `instantiate_nth_var n a b` takes the `n`th de-Bruijn variable in `a` and replaces each occurrence with `b`. -/
meta constant expr.instantiate_nth_var : nat → expr → expr → expr
/-- `instantiate_var a b` takes the 0th de-Bruijn variable in `a` and replaces each occurrence with `b`. -/
meta constant expr.instantiate_var : expr → expr → expr
/-- ``instantiate_vars `(#0 #1 #2) [x,y,z] = `(%%x %%y %%z)`` -/
meta constant expr.instantiate_vars : expr → list expr → expr
/-- Same as `instantiate_vars` except lifts and shifts the vars by the given amount.
``instantiate_vars_core `(#0 #1 #2 #3) 0 [x,y] = `(x y #0 #1)``
``instantiate_vars_core `(#0 #1 #2 #3) 1 [x,y] = `(#0 x y #1)``
``instantiate_vars_core `(#0 #1 #2 #3) 2 [x,y] = `(#0 #1 x y)``
-/
meta constant expr.instantiate_vars_core : expr → nat → list expr → expr
/-- Perform beta-reduction if the left expression is a lambda, or construct an application otherwise.
That is: ``expr.subst `(λ x, %%Y) Z = Y[x/Z]``, and
``expr.subst X Z = X.app Z`` otherwise -/
protected meta constant expr.subst : expr elab → expr elab → expr elab
/-- `get_free_var_range e` returns one plus the maximum de-Bruijn value in `e`. Eg `get_free_var_range `(#1 #0)` yields `2` -/
meta constant expr.get_free_var_range : expr → nat
/-- `has_var e` returns true iff e has free variables. -/
meta constant expr.has_var : expr → bool
/-- `has_var_idx e n` returns true iff `e` has a free variable with de-Bruijn index `n`. -/
meta constant expr.has_var_idx : expr → nat → bool
/-- `has_local e` returns true if `e` contains a local constant. -/
meta constant expr.has_local : expr → bool
/-- `has_meta_var e` returns true iff `e` contains a metavariable. -/
meta constant expr.has_meta_var : expr → bool
/-- `lower_vars e s d` lowers the free variables >= s in `e` by `d`. Note that this can cause variable clashes.
examples:
- ``lower_vars `(#2 #1 #0) 1 1 = `(#1 #0 #0)``
- ``lower_vars `(λ x, #2 #1 #0) 1 1 = `(λ x, #1 #1 #0 )``
-/
meta constant expr.lower_vars : expr → nat → nat → expr
/-- Lifts free variables. `lift_vars e s d` will lift all free variables with index `≥ s` in `e` by `d`. -/
meta constant expr.lift_vars : expr → nat → nat → expr
/-- Get the position of the given expression in the Lean source file, if anywhere. -/
protected meta constant expr.pos : expr elab → option pos
/-- `copy_pos_info src tgt` copies position information from `src` to `tgt`. -/
meta constant expr.copy_pos_info : expr → expr → expr
/-- Returns `some n` when the given expression is a constant with the name `..._cnstr.n`
```
is_internal_cnstr : expr → option unsigned
|(const (mk_numeral n (mk_string "_cnstr" _)) _) := some n
|_ := none
```
[NOTE] This is not used anywhere in core Lean.
-/
meta constant expr.is_internal_cnstr : expr → option unsigned
/-- There is a macro called a "nat_value_macro" holding a natural number which are used during compilation.
This function extracts that to a natural number. [NOTE] This is not used anywhere in Lean. -/
meta constant expr.get_nat_value : expr → option nat
/-- Get a list of all of the universe parameters that the given expression depends on. -/
meta constant expr.collect_univ_params : expr → list name
/-- `occurs e t` returns `tt` iff `e` occurs in `t` up to α-equivalence. Purely structural: no unification or definitional equality. -/
meta constant expr.occurs : expr → expr → bool
/-- Returns true if any of the names in the given `name_set` are present in the given `expr`. -/
meta constant expr.has_local_in : expr → name_set → bool
/-- Computes the number of sub-expressions (constant time). -/
meta constant expr.get_weight : expr → ℕ
/-- Computes the maximum depth of the expression (constant time). -/
meta constant expr.get_depth : expr → ℕ
/-- `mk_delayed_abstraction m ls` creates a delayed abstraction on the metavariable `m` with the unique names of the local constants `ls`.
If `m` is not a metavariable then this is equivalent to `abstract_locals`.
-/
meta constant expr.mk_delayed_abstraction : expr → list name → expr
/-- If the given expression is a delayed abstraction macro, return `some ls`
where `ls` is a list of unique names of locals that will be abstracted. -/
meta constant expr.get_delayed_abstraction_locals : expr → option (list name)
/-- (reflected a) is a special opaque container for a closed `expr` representing `a`.
It can only be obtained via type class inference, which will use the representation
of `a` in the calling context. Local constants in the representation are replaced
by nested inference of `reflected` instances.
The quotation expression `` `(a) `` (outside of patterns) is equivalent to `reflect a`
and thus can be used as an explicit way of inferring an instance of `reflected a`. -/
@[class] meta def reflected {α : Sort u} : α → Type :=
λ _, expr
@[inline] meta def reflected.to_expr {α : Sort u} {a : α} : reflected a → expr :=
id
@[inline] meta def reflected.subst {α : Sort v} {β : α → Sort u} {f : Π a : α, β a} {a : α} :
reflected f → reflected a → reflected (f a) :=
expr.subst
attribute [irreducible] reflected reflected.subst reflected.to_expr
@[instance] protected meta constant expr.reflect (e : expr elab) : reflected e
@[instance] protected meta constant string.reflect (s : string) : reflected s
@[inline] meta instance {α : Sort u} (a : α) : has_coe (reflected a) expr :=
⟨reflected.to_expr⟩
protected meta def reflect {α : Sort u} (a : α) [h : reflected a] : reflected a := h
meta instance {α} (a : α) : has_to_format (reflected a) :=
⟨λ h, to_fmt h.to_expr⟩
namespace expr
open decidable
meta def lt_prop (a b : expr) : Prop :=
expr.lt a b = tt
meta instance : decidable_rel expr.lt_prop :=
λ a b, bool.decidable_eq _ _
/-- Compares expressions, ignoring binder names, and sorting by hash. -/
meta instance : has_lt expr :=
⟨ expr.lt_prop ⟩
meta def mk_true : expr :=
const `true []
meta def mk_false : expr :=
const `false []
/-- Returns the sorry macro with the given type. -/
meta constant mk_sorry (type : expr) : expr
/-- Checks whether e is sorry, and returns its type. -/
meta constant is_sorry (e : expr) : option expr
/-- Replace each instance of the local constant with name `n` by the expression `s` in `e`. -/
meta def instantiate_local (n : name) (s : expr) (e : expr) : expr :=
instantiate_var (abstract_local e n) s
meta def instantiate_locals (s : list (name × expr)) (e : expr) : expr :=
instantiate_vars (abstract_locals e (list.reverse (list.map prod.fst s))) (list.map prod.snd s)
meta def is_var : expr → bool
| (var _) := tt
| _ := ff
meta def app_of_list : expr → list expr → expr
| f [] := f
| f (p::ps) := app_of_list (f p) ps
meta def is_app : expr → bool
| (app f a) := tt
| e := ff
meta def app_fn : expr → expr
| (app f a) := f
| a := a
meta def app_arg : expr → expr
| (app f a) := a
| a := a
meta def get_app_fn : expr elab → expr elab
| (app f a) := get_app_fn f
| a := a
meta def get_app_num_args : expr → nat
| (app f a) := get_app_num_args f + 1
| e := 0
meta def get_app_args_aux : list expr → expr → list expr
| r (app f a) := get_app_args_aux (a::r) f
| r e := r
meta def get_app_args : expr → list expr :=
get_app_args_aux []
meta def mk_app : expr → list expr → expr
| e [] := e
| e (x::xs) := mk_app (e x) xs
meta def mk_binding (ctor : name → binder_info → expr → expr → expr) (e : expr) : Π (l : expr), expr
| (local_const n pp_n bi ty) := ctor pp_n bi ty (e.abstract_local n)
| _ := e
/-- (bind_pi e l) abstracts and pi-binds the local `l` in `e` -/
meta def bind_pi := mk_binding pi
/-- (bind_lambda e l) abstracts and lambda-binds the local `l` in `e` -/
meta def bind_lambda := mk_binding lam
meta def ith_arg_aux : expr → nat → expr
| (app f a) 0 := a
| (app f a) (n+1) := ith_arg_aux f n
| e _ := e
meta def ith_arg (e : expr) (i : nat) : expr :=
ith_arg_aux e (get_app_num_args e - i - 1)
meta def const_name : expr elab → name
| (const n ls) := n
| e := name.anonymous
meta def is_constant : expr elab → bool
| (const n ls) := tt
| e := ff
meta def is_local_constant : expr → bool
| (local_const n m bi t) := tt
| e := ff
meta def local_uniq_name : expr → name
| (local_const n m bi t) := n
| e := name.anonymous
meta def local_pp_name : expr elab → name
| (local_const x n bi t) := n
| e := name.anonymous
meta def local_type : expr elab → expr elab
| (local_const _ _ _ t) := t
| e := e
meta def is_aux_decl : expr → bool
| (local_const _ _ binder_info.aux_decl _) := tt
| _ := ff
meta def is_constant_of : expr elab → name → bool
| (const n₁ ls) n₂ := n₁ = n₂
| e n := ff
meta def is_app_of (e : expr) (n : name) : bool :=
is_constant_of (get_app_fn e) n
/-- The same as `is_app_of` but must also have exactly `n` arguments. -/
meta def is_napp_of (e : expr) (c : name) (n : nat) : bool :=
is_app_of e c ∧ get_app_num_args e = n
meta def is_false : expr → bool
| `(false) := tt
| _ := ff
meta def is_not : expr → option expr
| `(not %%a) := some a
| `(%%a → false) := some a
| e := none
meta def is_and : expr → option (expr × expr)
| `(and %%α %%β) := some (α, β)
| _ := none
meta def is_or : expr → option (expr × expr)
| `(or %%α %%β) := some (α, β)
| _ := none
meta def is_iff : expr → option (expr × expr)
| `((%%a : Prop) ↔ %%b) := some (a, b)
| _ := none
meta def is_eq : expr → option (expr × expr)
| `((%%a : %%_) = %%b) := some (a, b)
| _ := none
meta def is_ne : expr → option (expr × expr)
| `((%%a : %%_) ≠ %%b) := some (a, b)
| _ := none
meta def is_bin_arith_app (e : expr) (op : name) : option (expr × expr) :=
if is_napp_of e op 4
then some (app_arg (app_fn e), app_arg e)
else none
meta def is_lt (e : expr) : option (expr × expr) :=
is_bin_arith_app e ``has_lt.lt
meta def is_gt (e : expr) : option (expr × expr) :=
is_bin_arith_app e ``gt
meta def is_le (e : expr) : option (expr × expr) :=
is_bin_arith_app e ``has_le.le
meta def is_ge (e : expr) : option (expr × expr) :=
is_bin_arith_app e ``ge
meta def is_heq : expr → option (expr × expr × expr × expr)
| `(@heq %%α %%a %%β %%b) := some (α, a, β, b)
| _ := none
meta def is_lambda : expr → bool
| (lam _ _ _ _) := tt
| e := ff
meta def is_pi : expr → bool
| (pi _ _ _ _) := tt
| e := ff
meta def is_arrow : expr → bool
| (pi _ _ _ b) := bnot (has_var b)
| e := ff
meta def is_let : expr → bool
| (elet _ _ _ _) := tt
| e := ff
meta def binding_name : expr → name
| (pi n _ _ _) := n
| (lam n _ _ _) := n
| e := name.anonymous
meta def binding_info : expr → binder_info
| (pi _ bi _ _) := bi
| (lam _ bi _ _) := bi
| e := binder_info.default
meta def binding_domain : expr → expr
| (pi _ _ d _) := d
| (lam _ _ d _) := d
| e := e
meta def binding_body : expr → expr
| (pi _ _ _ b) := b
| (lam _ _ _ b) := b
| e := e
meta def is_macro : expr → bool
| (macro d a) := tt
| e := ff
meta def is_numeral : expr → bool
| `(@has_zero.zero %%α %%s) := tt
| `(@has_one.one %%α %%s) := tt
| `(@bit0 %%α %%s %%v) := is_numeral v
| `(@bit1 %%α %%s₁ %%s₂ %%v) := is_numeral v
| _ := ff
meta def imp (a b : expr) : expr :=
pi `_ binder_info.default a b
/-- `lambdas cs e` lambda binds `e` with each of the local constants in `cs`. -/
meta def lambdas : list expr → expr → expr
| (local_const uniq pp info t :: es) f :=
lam pp info t (abstract_local (lambdas es f) uniq)
| _ f := f
/-- Same as `expr.lambdas` but with `pi`. -/
meta def pis : list expr → expr → expr
| (local_const uniq pp info t :: es) f :=
pi pp info t (abstract_local (pis es f) uniq)
| _ f := f
meta def extract_opt_auto_param : expr → expr
| `(@opt_param %%t _) := extract_opt_auto_param t
| `(@auto_param %%t _) := extract_opt_auto_param t
| e := e
open format
private meta def p := λ xs, paren (format.join (list.intersperse " " xs))
meta def to_raw_fmt : expr elab → format
| (var n) := p ["var", to_fmt n]
| (sort l) := p ["sort", to_fmt l]
| (const n ls) := p ["const", to_fmt n, to_fmt ls]
| (mvar n m t) := p ["mvar", to_fmt n, to_fmt m, to_raw_fmt t]
| (local_const n m bi t) := p ["local_const", to_fmt n, to_fmt m, to_raw_fmt t]
| (app e f) := p ["app", to_raw_fmt e, to_raw_fmt f]
| (lam n bi e t) := p ["lam", to_fmt n, repr bi, to_raw_fmt e, to_raw_fmt t]
| (pi n bi e t) := p ["pi", to_fmt n, repr bi, to_raw_fmt e, to_raw_fmt t]
| (elet n g e f) := p ["elet", to_fmt n, to_raw_fmt g, to_raw_fmt e, to_raw_fmt f]
| (macro d args) := sbracket (format.join (list.intersperse " " ("macro" :: to_fmt (macro_def_name d) :: args.map to_raw_fmt)))
/-- Fold an accumulator `a` over each subexpression in the expression `e`.
The `nat` passed to `fn` is the number of binders above the subexpression. -/
meta def mfold {α : Type} {m : Type → Type} [monad m] (e : expr) (a : α) (fn : expr → nat → α → m α) : m α :=
fold e (return a) (λ e n a, a >>= fn e n)
end expr
/-- An dictionary from `data` to expressions. -/
@[reducible] meta def expr_map (data : Type) := rb_map expr data
namespace expr_map
export native.rb_map (hiding mk)
meta def mk (data : Type) : expr_map data := rb_map.mk expr data
end expr_map
meta def mk_expr_map {data : Type} : expr_map data :=
expr_map.mk data
@[reducible] meta def expr_set := rb_set expr
meta def mk_expr_set : expr_set := mk_rb_set
|
d1e3e797d7346bfbc0cbf97104cf7eba843f5f75 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/algebra/group/conj.lean | 883b08d4f72d179adb45508a7032f2b3ee90f952 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 1,971 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Chris Hughes, Michael Howes
Conjugacy of group elements
-/
import tactic.basic algebra.group.hom
universes u v
variables {α : Type u} {β : Type v}
variables [group α] [group β]
def is_conj (a b : α) := ∃ c : α, c * a * c⁻¹ = b
@[refl] lemma is_conj_refl (a : α) : is_conj a a :=
⟨1, by rw [one_mul, one_inv, mul_one]⟩
@[symm] lemma is_conj_symm {a b : α} : is_conj a b → is_conj b a
| ⟨c, hc⟩ := ⟨c⁻¹, by rw [← hc, mul_assoc, mul_inv_cancel_right, inv_mul_cancel_left]⟩
@[trans] lemma is_conj_trans {a b c : α} : is_conj a b → is_conj b c → is_conj a c
| ⟨c₁, hc₁⟩ ⟨c₂, hc₂⟩ := ⟨c₂ * c₁, by rw [← hc₂, ← hc₁, mul_inv_rev]; simp only [mul_assoc]⟩
@[simp] lemma is_conj_one_right {a : α} : is_conj 1 a ↔ a = 1 :=
⟨by simp [is_conj, is_conj_refl] {contextual := tt}, by simp [is_conj_refl] {contextual := tt}⟩
@[simp] lemma is_conj_one_left {a : α} : is_conj a 1 ↔ a = 1 :=
calc is_conj a 1 ↔ is_conj 1 a : ⟨is_conj_symm, is_conj_symm⟩
... ↔ a = 1 : is_conj_one_right
@[simp] lemma conj_inv {a b : α} : (b * a * b⁻¹)⁻¹ = b * a⁻¹ * b⁻¹ :=
begin
rw [mul_inv_rev _ b⁻¹, mul_inv_rev b _, inv_inv, mul_assoc],
end
@[simp] lemma conj_mul {a b c : α} : (b * a * b⁻¹) * (b * c * b⁻¹) = b * (a * c) * b⁻¹ :=
begin
assoc_rw inv_mul_cancel_right,
repeat {rw mul_assoc},
end
@[simp] lemma is_conj_iff_eq {α : Type*} [comm_group α] {a b : α} : is_conj a b ↔ a = b :=
⟨λ ⟨c, hc⟩, by rw [← hc, mul_right_comm, mul_inv_self, one_mul], λ h, by rw h⟩
protected lemma is_group_hom.is_conj (f : α → β) [is_group_hom f] {a b : α} : is_conj a b → is_conj (f a) (f b)
| ⟨c, hc⟩ := ⟨f c, by rw [← is_mul_hom.map_mul f, ← is_group_hom.map_inv f, ← is_mul_hom.map_mul f, hc]⟩
|
f96c93885ef5493d1fa583dbacdb71e28417a649 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/analysis/complex/operator_norm.lean | a0133388f963b811c2e29d38f0c990bfee461887 | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 2,058 | lean | /-
Copyright (c) Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import analysis.complex.basic
import analysis.normed_space.operator_norm
import data.complex.determinant
/-! # The basic continuous linear maps associated to `ℂ`
The continuous linear maps `complex.re_clm` (real part), `complex.im_clm` (imaginary part),
`complex.conj_cle` (conjugation), and `complex.of_real_clm` (inclusion of `ℝ`) were introduced in
`analysis.complex.operator_norm`. This file contains a few calculations requiring more imports:
the operator norm and (for `complex.conj_cle`) the determinant.
-/
open continuous_linear_map
namespace complex
/-- The determinant of `conj_lie`, as a linear map. -/
@[simp] lemma det_conj_lie : (conj_lie.to_linear_equiv : ℂ →ₗ[ℝ] ℂ).det = -1 :=
det_conj_ae
/-- The determinant of `conj_lie`, as a linear equiv. -/
@[simp] lemma linear_equiv_det_conj_lie : conj_lie.to_linear_equiv.det = -1 :=
linear_equiv_det_conj_ae
@[simp] lemma re_clm_norm : ‖re_clm‖ = 1 :=
le_antisymm (linear_map.mk_continuous_norm_le _ zero_le_one _) $
calc 1 = ‖re_clm 1‖ : by simp
... ≤ ‖re_clm‖ : unit_le_op_norm _ _ (by simp)
@[simp] lemma re_clm_nnnorm : ‖re_clm‖₊ = 1 := subtype.ext re_clm_norm
@[simp] lemma im_clm_norm : ‖im_clm‖ = 1 :=
le_antisymm (linear_map.mk_continuous_norm_le _ zero_le_one _) $
calc 1 = ‖im_clm I‖ : by simp
... ≤ ‖im_clm‖ : unit_le_op_norm _ _ (by simp)
@[simp] lemma im_clm_nnnorm : ‖im_clm‖₊ = 1 := subtype.ext im_clm_norm
@[simp] lemma conj_cle_norm : ‖(conj_cle : ℂ →L[ℝ] ℂ)‖ = 1 :=
conj_lie.to_linear_isometry.norm_to_continuous_linear_map
@[simp] lemma conj_cle_nnorm : ‖(conj_cle : ℂ →L[ℝ] ℂ)‖₊ = 1 := subtype.ext conj_cle_norm
@[simp] lemma of_real_clm_norm : ‖of_real_clm‖ = 1 := of_real_li.norm_to_continuous_linear_map
@[simp] lemma of_real_clm_nnnorm : ‖of_real_clm‖₊ = 1 := subtype.ext $ of_real_clm_norm
end complex
|
60462b2b4d4caf232c2df03fa0cbcb44d968bb3c | 432d948a4d3d242fdfb44b81c9e1b1baacd58617 | /src/algebra/algebra/basic.lean | 96061ea81d83d07c7ca8dbb7bc6586073517fec6 | [
"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 | 57,229 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Yury Kudryashov
-/
import tactic.nth_rewrite
import data.matrix.basic
import data.equiv.ring_aut
import linear_algebra.tensor_product
import ring_theory.subring
import deprecated.subring
import algebra.opposites
/-!
# Algebra over Commutative Semiring
In this file we define `algebra`s over commutative (semi)rings, algebra homomorphisms `alg_hom`,
algebra equivalences `alg_equiv`. We also define usual operations on `alg_hom`s
(`id`, `comp`).
`subalgebra`s are defined in `algebra.algebra.subalgebra`.
If `S` is an `R`-algebra and `A` is an `S`-algebra then `algebra.comap.algebra R S A` can be used
to provide `A` with a structure of an `R`-algebra. Other than that, `algebra.comap` is now
deprecated and replaced with `is_scalar_tower`.
For the category of `R`-algebras, denoted `Algebra R`, see the file
`algebra/category/Algebra/basic.lean`.
## Notations
* `A →ₐ[R] B` : `R`-algebra homomorphism from `A` to `B`.
* `A ≃ₐ[R] B` : `R`-algebra equivalence from `A` to `B`.
-/
universes u v w u₁ v₁
open_locale tensor_product big_operators
section prio
-- We set this priority to 0 later in this file
set_option extends_priority 200 /- control priority of
`instance [algebra R A] : has_scalar R A` -/
/--
Given a commutative (semi)ring `R`, an `R`-algebra is a (possibly noncommutative)
(semi)ring `A` endowed with a morphism of rings `R →+* A` which lands in the
center of `A`.
For convenience, this typeclass extends `has_scalar R A` where the scalar action must
agree with left multiplication by the image of the structure morphism.
Given an `algebra R A` instance, the structure morphism `R →+* A` is denoted `algebra_map R A`.
-/
@[nolint has_inhabited_instance]
class algebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A]
extends has_scalar R A, R →+* A :=
(commutes' : ∀ r x, to_fun r * x = x * to_fun r)
(smul_def' : ∀ r x, r • x = to_fun r * x)
end prio
/-- Embedding `R →+* A` given by `algebra` structure. -/
def algebra_map (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] : R →+* A :=
algebra.to_ring_hom
/-- Creating an algebra from a morphism to the center of a semiring. -/
def ring_hom.to_algebra' {R S} [comm_semiring R] [semiring S] (i : R →+* S)
(h : ∀ c x, i c * x = x * i c) :
algebra R S :=
{ smul := λ c x, i c * x,
commutes' := h,
smul_def' := λ c x, rfl,
to_ring_hom := i}
/-- Creating an algebra from a morphism to a commutative semiring. -/
def ring_hom.to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) :
algebra R S :=
i.to_algebra' $ λ _, mul_comm _
lemma ring_hom.algebra_map_to_algebra {R S} [comm_semiring R] [comm_semiring S]
(i : R →+* S) :
@algebra_map R S _ _ i.to_algebra = i :=
rfl
namespace algebra
variables {R : Type u} {S : Type v} {A : Type w} {B : Type*}
/-- Let `R` be a commutative semiring, let `A` be a semiring with a `module R` structure.
If `(r • 1) * x = x * (r • 1) = r • x` for all `r : R` and `x : A`, then `A` is an `algebra`
over `R`. -/
def of_module' [comm_semiring R] [semiring A] [module R A]
(h₁ : ∀ (r : R) (x : A), (r • 1) * x = r • x)
(h₂ : ∀ (r : R) (x : A), x * (r • 1) = r • x) : algebra R A :=
{ to_fun := λ r, r • 1,
map_one' := one_smul _ _,
map_mul' := λ r₁ r₂, by rw [h₁, mul_smul],
map_zero' := zero_smul _ _,
map_add' := λ r₁ r₂, add_smul r₁ r₂ 1,
commutes' := λ r x, by simp only [h₁, h₂],
smul_def' := λ r x, by simp only [h₁] }
/-- Let `R` be a commutative semiring, let `A` be a semiring with a `module R` structure.
If `(r • x) * y = x * (r • y) = r • (x * y)` for all `r : R` and `x y : A`, then `A`
is an `algebra` over `R`. -/
def of_module [comm_semiring R] [semiring A] [module R A]
(h₁ : ∀ (r : R) (x y : A), (r • x) * y = r • (x * y))
(h₂ : ∀ (r : R) (x y : A), x * (r • y) = r • (x * y)) : algebra R A :=
of_module' (λ r x, by rw [h₁, one_mul]) (λ r x, by rw [h₂, mul_one])
section semiring
variables [comm_semiring R] [comm_semiring S]
variables [semiring A] [algebra R A] [semiring B] [algebra R B]
lemma smul_def'' (r : R) (x : A) : r • x = algebra_map R A r * x :=
algebra.smul_def' r x
/--
To prove two algebra structures on a fixed `[comm_semiring R] [semiring A]` agree,
it suffices to check the `algebra_map`s agree.
-/
-- We'll later use this to show `algebra ℤ M` is a subsingleton.
@[ext]
lemma algebra_ext {R : Type*} [comm_semiring R] {A : Type*} [semiring A] (P Q : algebra R A)
(w : ∀ (r : R), by { haveI := P, exact algebra_map R A r } =
by { haveI := Q, exact algebra_map R A r }) :
P = Q :=
begin
unfreezingI { rcases P with ⟨⟨P⟩⟩, rcases Q with ⟨⟨Q⟩⟩ },
congr,
{ funext r a,
replace w := congr_arg (λ s, s * a) (w r),
simp only [←algebra.smul_def''] at w,
apply w, },
{ ext r,
exact w r, },
{ apply proof_irrel_heq, },
{ apply proof_irrel_heq, },
end
@[priority 200] -- see Note [lower instance priority]
instance to_module : module R A :=
{ one_smul := by simp [smul_def''],
mul_smul := by simp [smul_def'', mul_assoc],
smul_add := by simp [smul_def'', mul_add],
smul_zero := by simp [smul_def''],
add_smul := by simp [smul_def'', add_mul],
zero_smul := by simp [smul_def''] }
-- from now on, we don't want to use the following instance anymore
attribute [instance, priority 0] algebra.to_has_scalar
lemma smul_def (r : R) (x : A) : r • x = algebra_map R A r * x :=
algebra.smul_def' r x
lemma algebra_map_eq_smul_one (r : R) : algebra_map R A r = r • 1 :=
calc algebra_map R A r = algebra_map R A r * 1 : (mul_one _).symm
... = r • 1 : (algebra.smul_def r 1).symm
lemma algebra_map_eq_smul_one' : ⇑(algebra_map R A) = λ r, r • (1 : A) :=
funext algebra_map_eq_smul_one
theorem commutes (r : R) (x : A) : algebra_map R A r * x = x * algebra_map R A r :=
algebra.commutes' r x
theorem left_comm (r : R) (x y : A) : x * (algebra_map R A r * y) = algebra_map R A r * (x * y) :=
by rw [← mul_assoc, ← commutes, mul_assoc]
@[simp] lemma mul_smul_comm (s : R) (x y : A) :
x * (s • y) = s • (x * y) :=
by rw [smul_def, smul_def, left_comm]
@[simp] lemma smul_mul_assoc (r : R) (x y : A) :
(r • x) * y = r • (x * y) :=
by rw [smul_def, smul_def, mul_assoc]
lemma smul_mul_smul (r s : R) (x y : A) :
(r • x) * (s • y) = (r * s) • (x * y) :=
by rw [algebra.smul_mul_assoc, algebra.mul_smul_comm, smul_smul]
section
variables {r : R} {a : A}
@[simp] lemma bit0_smul_one : bit0 r • (1 : A) = r • 2 :=
by simp [bit0, add_smul, smul_add]
@[simp] lemma bit0_smul_bit0 : bit0 r • bit0 a = r • (bit0 (bit0 a)) :=
by simp [bit0, add_smul, smul_add]
@[simp] lemma bit0_smul_bit1 : bit0 r • bit1 a = r • (bit0 (bit1 a)) :=
by simp [bit0, add_smul, smul_add]
@[simp] lemma bit1_smul_one : bit1 r • (1 : A) = r • 2 + 1 :=
by simp [bit1, add_smul, smul_add]
@[simp] lemma bit1_smul_bit0 : bit1 r • bit0 a = r • (bit0 (bit0 a)) + bit0 a :=
by simp [bit1, add_smul, smul_add]
@[simp] lemma bit1_smul_bit1 : bit1 r • bit1 a = r • (bit0 (bit1 a)) + bit1 a :=
by { simp only [bit0, bit1, add_smul, smul_add, one_smul], abel }
end
variables (R A)
/--
The canonical ring homomorphism `algebra_map R A : R →* A` for any `R`-algebra `A`,
packaged as an `R`-linear map.
-/
protected def linear_map : R →ₗ[R] A :=
{ map_smul' := λ x y, by simp [algebra.smul_def],
..algebra_map R A }
@[simp]
lemma linear_map_apply (r : R) : algebra.linear_map R A r = algebra_map R A r := rfl
instance id : algebra R R := (ring_hom.id R).to_algebra
variables {R A}
namespace id
@[simp] lemma map_eq_self (x : R) : algebra_map R R x = x := rfl
@[simp] lemma smul_eq_mul (x y : R) : x • y = x * y := rfl
end id
section prod
variables (R A B)
instance : algebra R (A × B) :=
{ commutes' := by { rintro r ⟨a, b⟩, dsimp, rw [commutes r a, commutes r b] },
smul_def' := by { rintro r ⟨a, b⟩, dsimp, rw [smul_def r a, smul_def r b] },
.. prod.module,
.. ring_hom.prod (algebra_map R A) (algebra_map R B) }
variables {R A B}
@[simp] lemma algebra_map_prod_apply (r : R) :
algebra_map R (A × B) r = (algebra_map R A r, algebra_map R B r) := rfl
end prod
/-- Algebra over a subsemiring. -/
instance of_subsemiring (S : subsemiring R) : algebra S A :=
{ smul := λ s x, (s : R) • x,
commutes' := λ r x, algebra.commutes r x,
smul_def' := λ r x, algebra.smul_def r x,
.. (algebra_map R A).comp (subsemiring.subtype S) }
/-- Algebra over a subring. -/
instance of_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A]
(S : subring R) : algebra S A :=
{ smul := λ s x, (s : R) • x,
commutes' := λ r x, algebra.commutes r x,
smul_def' := λ r x, algebra.smul_def r x,
.. (algebra_map R A).comp (subring.subtype S) }
lemma algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) :
(algebra_map S R : S →+* R) = subring.subtype S := rfl
lemma coe_algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) :
(algebra_map S R : S → R) = subtype.val := rfl
lemma algebra_map_of_subring_apply {R : Type*} [comm_ring R] (S : subring R) (x : S) :
algebra_map S R x = x := rfl
section
local attribute [instance] subset.comm_ring
/-- Algebra over a set that is closed under the ring operations. -/
local attribute [instance]
def of_is_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A]
(S : set R) [is_subring S] : algebra S A :=
algebra.of_subring S.to_subring
lemma is_subring_coe_algebra_map_hom {R : Type*} [comm_ring R] (S : set R) [is_subring S] :
(algebra_map S R : S →+* R) = is_subring.subtype S := rfl
lemma is_subring_coe_algebra_map {R : Type*} [comm_ring R] (S : set R) [is_subring S] :
(algebra_map S R : S → R) = subtype.val := rfl
lemma is_subring_algebra_map_apply {R : Type*} [comm_ring R] (S : set R) [is_subring S] (x : S) :
algebra_map S R x = x := rfl
lemma set_range_subset {R : Type*} [comm_ring R] {T₁ T₂ : set R} [is_subring T₁] (hyp : T₁ ⊆ T₂) :
set.range (algebra_map T₁ R) ⊆ T₂ :=
begin
rintros x ⟨⟨t, ht⟩, rfl⟩,
exact hyp ht,
end
end
/-- Explicit characterization of the submonoid map in the case of an algebra.
`S` is made explicit to help with type inference -/
def algebra_map_submonoid (S : Type*) [semiring S] [algebra R S]
(M : submonoid R) : (submonoid S) :=
submonoid.map (algebra_map R S : R →* S) M
lemma mem_algebra_map_submonoid_of_mem [algebra R S] {M : submonoid R} (x : M) :
(algebra_map R S x) ∈ algebra_map_submonoid S M :=
set.mem_image_of_mem (algebra_map R S) x.2
end semiring
section ring
variables [comm_ring R]
variables (R)
/-- A `semiring` that is an `algebra` over a commutative ring carries a natural `ring` structure. -/
def semiring_to_ring [semiring A] [algebra R A] : ring A := {
..module.add_comm_monoid_to_add_comm_group R,
..(infer_instance : semiring A) }
variables {R}
lemma mul_sub_algebra_map_commutes [ring A] [algebra R A] (x : A) (r : R) :
x * (x - algebra_map R A r) = (x - algebra_map R A r) * x :=
by rw [mul_sub, ←commutes, sub_mul]
lemma mul_sub_algebra_map_pow_commutes [ring A] [algebra R A] (x : A) (r : R) (n : ℕ) :
x * (x - algebra_map R A r) ^ n = (x - algebra_map R A r) ^ n * x :=
begin
induction n with n ih,
{ simp },
{ rw [pow_succ, ←mul_assoc, mul_sub_algebra_map_commutes,
mul_assoc, ih, ←mul_assoc], }
end
/-- If `algebra_map R A` is injective and `A` has no zero divisors,
`R`-multiples in `A` are zero only if one of the factors is zero.
Cannot be an instance because there is no `injective (algebra_map R A)` typeclass.
-/
lemma no_zero_smul_divisors.of_algebra_map_injective
[semiring A] [algebra R A] [no_zero_divisors A]
(h : function.injective (algebra_map R A)) : no_zero_smul_divisors R A :=
⟨λ c x hcx, (mul_eq_zero.mp ((smul_def c x).symm.trans hcx)).imp_left
((algebra_map R A).injective_iff.mp h _)⟩
end ring
section field
variables [field R] [semiring A] [algebra R A]
@[priority 100] -- see note [lower instance priority]
instance [nontrivial A] [no_zero_divisors A] : no_zero_smul_divisors R A :=
no_zero_smul_divisors.of_algebra_map_injective (algebra_map R A).injective
end field
end algebra
namespace opposite
variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A]
instance : algebra R Aᵒᵖ :=
{ to_ring_hom := (algebra_map R A).to_opposite $ λ x y, algebra.commutes _ _,
smul_def' := λ c x, unop_injective $
by { dsimp, simp only [op_mul, algebra.smul_def, algebra.commutes, op_unop] },
commutes' := λ r, op_induction $ λ x, by dsimp; simp only [← op_mul, algebra.commutes],
..opposite.has_scalar A R }
@[simp] lemma algebra_map_apply (c : R) : algebra_map R Aᵒᵖ c = op (algebra_map R A c) := rfl
end opposite
namespace module
variables (R : Type u) (M : Type v) [comm_semiring R] [add_comm_monoid M] [module R M]
instance endomorphism_algebra : algebra R (M →ₗ[R] M) :=
{ to_fun := λ r, r • linear_map.id,
map_one' := one_smul _ _,
map_zero' := zero_smul _ _,
map_add' := λ r₁ r₂, add_smul _ _ _,
map_mul' := λ r₁ r₂, by { ext x, simp [mul_smul] },
commutes' := by { intros, ext, simp },
smul_def' := by { intros, ext, simp } }
lemma algebra_map_End_eq_smul_id (a : R) :
(algebra_map R (End R M)) a = a • linear_map.id := rfl
@[simp] lemma algebra_map_End_apply (a : R) (m : M) :
(algebra_map R (End R M)) a m = a • m := rfl
@[simp] lemma ker_algebra_map_End (K : Type u) (V : Type v)
[field K] [add_comm_group V] [module K V] (a : K) (ha : a ≠ 0) :
((algebra_map K (End K V)) a).ker = ⊥ :=
linear_map.ker_smul _ _ ha
end module
instance matrix_algebra (n : Type u) (R : Type v)
[decidable_eq n] [fintype n] [comm_semiring R] : algebra R (matrix n n R) :=
{ commutes' := by { intros, simp [matrix.scalar], },
smul_def' := by { intros, simp [matrix.scalar], },
..(matrix.scalar n) }
@[simp] lemma matrix.algebra_map_eq_smul (n : Type u) {R : Type v} [decidable_eq n] [fintype n]
[comm_semiring R] (r : R) : (algebra_map R (matrix n n R)) r = r • 1 := rfl
set_option old_structure_cmd true
/-- Defining the homomorphism in the category R-Alg. -/
@[nolint has_inhabited_instance]
structure alg_hom (R : Type u) (A : Type v) (B : Type w)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends ring_hom A B :=
(commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r)
run_cmd tactic.add_doc_string `alg_hom.to_ring_hom "Reinterpret an `alg_hom` as a `ring_hom`"
infixr ` →ₐ `:25 := alg_hom _
notation A ` →ₐ[`:25 R `] ` B := alg_hom R A B
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁}
section semiring
variables [comm_semiring R] [semiring A] [semiring B] [semiring C] [semiring D]
variables [algebra R A] [algebra R B] [algebra R C] [algebra R D]
instance : has_coe_to_fun (A →ₐ[R] B) := ⟨_, λ f, f.to_fun⟩
initialize_simps_projections alg_hom (to_fun → apply)
@[simp] lemma to_fun_eq_coe (f : A →ₐ[R] B) : f.to_fun = f := rfl
instance coe_ring_hom : has_coe (A →ₐ[R] B) (A →+* B) := ⟨alg_hom.to_ring_hom⟩
instance coe_monoid_hom : has_coe (A →ₐ[R] B) (A →* B) := ⟨λ f, ↑(f : A →+* B)⟩
instance coe_add_monoid_hom : has_coe (A →ₐ[R] B) (A →+ B) := ⟨λ f, ↑(f : A →+* B)⟩
@[simp, norm_cast] lemma coe_mk {f : A → B} (h₁ h₂ h₃ h₄ h₅) :
⇑(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := rfl
@[simp, norm_cast] lemma coe_to_ring_hom (f : A →ₐ[R] B) : ⇑(f : A →+* B) = f := rfl
-- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute.
@[norm_cast] lemma coe_to_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →* B) = f := rfl
-- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute.
@[norm_cast] lemma coe_to_add_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →+ B) = f := rfl
variables (φ : A →ₐ[R] B)
theorem coe_fn_inj ⦃φ₁ φ₂ : A →ₐ[R] B⦄ (H : ⇑φ₁ = φ₂) : φ₁ = φ₂ :=
by { cases φ₁, cases φ₂, congr, exact H }
theorem coe_ring_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+* B)) :=
λ φ₁ φ₂ H, coe_fn_inj $ show ((φ₁ : (A →+* B)) : A → B) = ((φ₂ : (A →+* B)) : A → B),
from congr_arg _ H
theorem coe_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →* B)) :=
ring_hom.coe_monoid_hom_injective.comp coe_ring_hom_injective
theorem coe_add_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+ B)) :=
ring_hom.coe_add_monoid_hom_injective.comp coe_ring_hom_injective
protected lemma congr_fun {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁ = φ₂) (x : A) : φ₁ x = φ₂ x := H ▸ rfl
protected lemma congr_arg (φ : A →ₐ[R] B) {x y : A} (h : x = y) : φ x = φ y := h ▸ rfl
@[ext]
theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ :=
coe_fn_inj $ funext H
theorem ext_iff {φ₁ φ₂ : A →ₐ[R] B} : φ₁ = φ₂ ↔ ∀ x, φ₁ x = φ₂ x :=
⟨alg_hom.congr_fun, ext⟩
@[simp] theorem mk_coe {f : A →ₐ[R] B} (h₁ h₂ h₃ h₄ h₅) :
(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := ext $ λ _, rfl
@[simp]
theorem commutes (r : R) : φ (algebra_map R A r) = algebra_map R B r := φ.commutes' r
theorem comp_algebra_map : (φ : A →+* B).comp (algebra_map R A) = algebra_map R B :=
ring_hom.ext $ φ.commutes
@[simp] lemma map_add (r s : A) : φ (r + s) = φ r + φ s :=
φ.to_ring_hom.map_add r s
@[simp] lemma map_zero : φ 0 = 0 :=
φ.to_ring_hom.map_zero
@[simp] lemma map_mul (x y) : φ (x * y) = φ x * φ y :=
φ.to_ring_hom.map_mul x y
@[simp] lemma map_one : φ 1 = 1 :=
φ.to_ring_hom.map_one
@[simp] lemma map_smul (r : R) (x : A) : φ (r • x) = r • φ x :=
by simp only [algebra.smul_def, map_mul, commutes]
@[simp] lemma map_pow (x : A) (n : ℕ) : φ (x ^ n) = (φ x) ^ n :=
φ.to_ring_hom.map_pow x n
lemma map_sum {ι : Type*} (f : ι → A) (s : finset ι) :
φ (∑ x in s, f x) = ∑ x in s, φ (f x) :=
φ.to_ring_hom.map_sum f s
lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) :
φ (f.sum g) = f.sum (λ i a, φ (g i a)) :=
φ.map_sum _ _
@[simp] lemma map_nat_cast (n : ℕ) : φ n = n :=
φ.to_ring_hom.map_nat_cast n
@[simp] lemma map_bit0 (x) : φ (bit0 x) = bit0 (φ x) :=
φ.to_ring_hom.map_bit0 x
@[simp] lemma map_bit1 (x) : φ (bit1 x) = bit1 (φ x) :=
φ.to_ring_hom.map_bit1 x
/-- If a `ring_hom` is `R`-linear, then it is an `alg_hom`. -/
def mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : A →ₐ[R] B :=
{ to_fun := f,
commutes' := λ c, by simp only [algebra.algebra_map_eq_smul_one, h, f.map_one],
.. f }
@[simp] lemma coe_mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : ⇑(mk' f h) = f := rfl
section
variables (R A)
/-- Identity map as an `alg_hom`. -/
protected def id : A →ₐ[R] A :=
{ commutes' := λ _, rfl,
..ring_hom.id A }
@[simp] lemma coe_id : ⇑(alg_hom.id R A) = id := rfl
@[simp] lemma id_to_ring_hom : (alg_hom.id R A : A →+* A) = ring_hom.id _ := rfl
end
lemma id_apply (p : A) : alg_hom.id R A p = p := rfl
/-- Composition of algebra homeomorphisms. -/
def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ[R] C :=
{ commutes' := λ r : R, by rw [← φ₁.commutes, ← φ₂.commutes]; refl,
.. φ₁.to_ring_hom.comp ↑φ₂ }
@[simp] lemma coe_comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : ⇑(φ₁.comp φ₂) = φ₁ ∘ φ₂ := rfl
lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) : φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl
lemma comp_to_ring_hom (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) :
⇑(φ₁.comp φ₂ : A →+* C) = (φ₁ : B →+* C).comp ↑φ₂ := rfl
@[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ :=
ext $ λ x, rfl
@[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ :=
ext $ λ x, rfl
theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) :
(φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) :=
ext $ λ x, rfl
/-- R-Alg ⥤ R-Mod -/
def to_linear_map : A →ₗ B :=
{ to_fun := φ,
map_add' := φ.map_add,
map_smul' := φ.map_smul }
@[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl
theorem to_linear_map_inj {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁.to_linear_map = φ₂.to_linear_map) : φ₁ = φ₂ :=
ext $ λ x, show φ₁.to_linear_map x = φ₂.to_linear_map x, by rw H
@[simp] lemma comp_to_linear_map (f : A →ₐ[R] B) (g : B →ₐ[R] C) :
(g.comp f).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl
lemma map_list_prod (s : list A) :
φ s.prod = (s.map φ).prod :=
φ.to_ring_hom.map_list_prod s
end semiring
section comm_semiring
variables [comm_semiring R] [comm_semiring A] [comm_semiring B]
variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B)
lemma map_multiset_prod (s : multiset A) :
φ s.prod = (s.map φ).prod :=
φ.to_ring_hom.map_multiset_prod s
lemma map_prod {ι : Type*} (f : ι → A) (s : finset ι) :
φ (∏ x in s, f x) = ∏ x in s, φ (f x) :=
φ.to_ring_hom.map_prod f s
lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) :
φ (f.prod g) = f.prod (λ i a, φ (g i a)) :=
φ.map_prod _ _
end comm_semiring
section ring
variables [comm_semiring R] [ring A] [ring B]
variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B)
@[simp] lemma map_neg (x) : φ (-x) = -φ x :=
φ.to_ring_hom.map_neg x
@[simp] lemma map_sub (x y) : φ (x - y) = φ x - φ y :=
φ.to_ring_hom.map_sub x y
@[simp] lemma map_int_cast (n : ℤ) : φ n = n :=
φ.to_ring_hom.map_int_cast n
end ring
section division_ring
variables [comm_ring R] [division_ring A] [division_ring B]
variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B)
@[simp] lemma map_inv (x) : φ (x⁻¹) = (φ x)⁻¹ :=
φ.to_ring_hom.map_inv x
@[simp] lemma map_div (x y) : φ (x / y) = φ x / φ y :=
φ.to_ring_hom.map_div x y
end division_ring
theorem injective_iff {R A B : Type*} [comm_semiring R] [ring A] [semiring B]
[algebra R A] [algebra R B] (f : A →ₐ[R] B) :
function.injective f ↔ (∀ x, f x = 0 → x = 0) :=
ring_hom.injective_iff (f : A →+* B)
end alg_hom
@[simp] lemma rat.smul_one_eq_coe {A : Type*} [division_ring A] [algebra ℚ A] (m : ℚ) :
m • (1 : A) = ↑m :=
by rw [algebra.smul_def, mul_one, ring_hom.eq_rat_cast]
set_option old_structure_cmd true
/-- An equivalence of algebras is an equivalence of rings commuting with the actions of scalars. -/
structure alg_equiv (R : Type u) (A : Type v) (B : Type w)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
extends A ≃ B, A ≃* B, A ≃+ B, A ≃+* B :=
(commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r)
attribute [nolint doc_blame] alg_equiv.to_ring_equiv
attribute [nolint doc_blame] alg_equiv.to_equiv
attribute [nolint doc_blame] alg_equiv.to_add_equiv
attribute [nolint doc_blame] alg_equiv.to_mul_equiv
notation A ` ≃ₐ[`:50 R `] ` A' := alg_equiv R A A'
namespace alg_equiv
variables {R : Type u} {A₁ : Type v} {A₂ : Type w} {A₃ : Type u₁}
section semiring
variables [comm_semiring R] [semiring A₁] [semiring A₂] [semiring A₃]
variables [algebra R A₁] [algebra R A₂] [algebra R A₃]
variables (e : A₁ ≃ₐ[R] A₂)
instance : has_coe_to_fun (A₁ ≃ₐ[R] A₂) := ⟨_, alg_equiv.to_fun⟩
@[ext]
lemma ext {f g : A₁ ≃ₐ[R] A₂} (h : ∀ a, f a = g a) : f = g :=
begin
have h₁ : f.to_equiv = g.to_equiv := equiv.ext h,
cases f, cases g, congr,
{ exact (funext h) },
{ exact congr_arg equiv.inv_fun h₁ }
end
protected lemma congr_arg {f : A₁ ≃ₐ[R] A₂} : Π {x x' : A₁}, x = x' → f x = f x'
| _ _ rfl := rfl
protected lemma congr_fun {f g : A₁ ≃ₐ[R] A₂} (h : f = g) (x : A₁) : f x = g x := h ▸ rfl
lemma ext_iff {f g : A₁ ≃ₐ[R] A₂} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, ext⟩
lemma coe_fun_injective : @function.injective (A₁ ≃ₐ[R] A₂) (A₁ → A₂) (λ e, (e : A₁ → A₂)) :=
begin
intros f g w,
ext,
exact congr_fun w a,
end
instance has_coe_to_ring_equiv : has_coe (A₁ ≃ₐ[R] A₂) (A₁ ≃+* A₂) := ⟨alg_equiv.to_ring_equiv⟩
@[simp] lemma coe_mk {to_fun inv_fun left_inv right_inv map_mul map_add commutes} :
⇑(⟨to_fun, inv_fun, left_inv, right_inv, map_mul, map_add, commutes⟩ : A₁ ≃ₐ[R] A₂) = to_fun :=
rfl
@[simp] theorem mk_coe (e : A₁ ≃ₐ[R] A₂) (e' h₁ h₂ h₃ h₄ h₅) :
(⟨e, e', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂) = e := ext $ λ _, rfl
@[simp] lemma to_fun_eq_coe (e : A₁ ≃ₐ[R] A₂) : e.to_fun = e := rfl
-- TODO: decide on a simp-normal form so that only one of these two lemmas is needed
@[simp, norm_cast] lemma coe_ring_equiv : ((e : A₁ ≃+* A₂) : A₁ → A₂) = e := rfl
@[simp] lemma coe_ring_equiv' : (e.to_ring_equiv : A₁ → A₂) = e := rfl
lemma coe_ring_equiv_injective : function.injective (λ e : A₁ ≃ₐ[R] A₂, (e : A₁ ≃+* A₂)) :=
begin
intros f g w,
ext,
replace w : ((f : A₁ ≃+* A₂) : A₁ → A₂) = ((g : A₁ ≃+* A₂) : A₁ → A₂) :=
congr_arg (λ e : A₁ ≃+* A₂, (e : A₁ → A₂)) w,
exact congr_fun w a,
end
@[simp] lemma map_add : ∀ x y, e (x + y) = e x + e y := e.to_add_equiv.map_add
@[simp] lemma map_zero : e 0 = 0 := e.to_add_equiv.map_zero
@[simp] lemma map_mul : ∀ x y, e (x * y) = (e x) * (e y) := e.to_mul_equiv.map_mul
@[simp] lemma map_one : e 1 = 1 := e.to_mul_equiv.map_one
@[simp] lemma commutes : ∀ (r : R), e (algebra_map R A₁ r) = algebra_map R A₂ r :=
e.commutes'
lemma map_sum {ι : Type*} (f : ι → A₁) (s : finset ι) :
e (∑ x in s, f x) = ∑ x in s, e (f x) :=
e.to_add_equiv.map_sum f s
lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) :
e (f.sum g) = f.sum (λ i b, e (g i b)) :=
e.map_sum _ _
/-- Interpret an algebra equivalence as an algebra homomorphism.
This definition is included for symmetry with the other `to_*_hom` projections.
The `simp` normal form is to use the coercion of the `has_coe_to_alg_hom` instance. -/
def to_alg_hom : A₁ →ₐ[R] A₂ :=
{ map_one' := e.map_one, map_zero' := e.map_zero, ..e }
instance has_coe_to_alg_hom : has_coe (A₁ ≃ₐ[R] A₂) (A₁ →ₐ[R] A₂) :=
⟨to_alg_hom⟩
@[simp] lemma to_alg_hom_eq_coe : e.to_alg_hom = e := rfl
@[simp, norm_cast] lemma coe_alg_hom : ((e : A₁ →ₐ[R] A₂) : A₁ → A₂) = e :=
rfl
/-- The two paths coercion can take to a `ring_hom` are equivalent -/
lemma coe_ring_hom_commutes : ((e : A₁ →ₐ[R] A₂) : A₁ →+* A₂) = ((e : A₁ ≃+* A₂) : A₁ →+* A₂) :=
rfl
@[simp] lemma map_pow : ∀ (x : A₁) (n : ℕ), e (x ^ n) = (e x) ^ n := e.to_alg_hom.map_pow
lemma injective : function.injective e := e.to_equiv.injective
lemma surjective : function.surjective e := e.to_equiv.surjective
lemma bijective : function.bijective e := e.to_equiv.bijective
instance : has_one (A₁ ≃ₐ[R] A₁) := ⟨{commutes' := λ r, rfl, ..(1 : A₁ ≃+* A₁)}⟩
instance : inhabited (A₁ ≃ₐ[R] A₁) := ⟨1⟩
/-- Algebra equivalences are reflexive. -/
@[refl]
def refl : A₁ ≃ₐ[R] A₁ := 1
@[simp] lemma refl_to_alg_hom : ↑(refl : A₁ ≃ₐ[R] A₁) = alg_hom.id R A₁ := rfl
@[simp] lemma coe_refl : ⇑(refl : A₁ ≃ₐ[R] A₁) = id := rfl
/-- Algebra equivalences are symmetric. -/
@[symm]
def symm (e : A₁ ≃ₐ[R] A₂) : A₂ ≃ₐ[R] A₁ :=
{ commutes' := λ r, by { rw ←e.to_ring_equiv.symm_apply_apply (algebra_map R A₁ r), congr,
change _ = e _, rw e.commutes, },
..e.to_ring_equiv.symm, }
/-- See Note [custom simps projection] -/
def simps.symm_apply (e : A₁ ≃ₐ[R] A₂) : A₂ → A₁ := e.symm
initialize_simps_projections alg_equiv (to_fun → apply, inv_fun → symm_apply)
@[simp] lemma inv_fun_eq_symm {e : A₁ ≃ₐ[R] A₂} : e.inv_fun = e.symm := rfl
@[simp] lemma symm_symm (e : A₁ ≃ₐ[R] A₂) : e.symm.symm = e :=
by { ext, refl, }
lemma symm_bijective : function.bijective (symm : (A₁ ≃ₐ[R] A₂) → (A₂ ≃ₐ[R] A₁)) :=
equiv.bijective ⟨symm, symm, symm_symm, symm_symm⟩
@[simp] lemma mk_coe' (e : A₁ ≃ₐ[R] A₂) (f h₁ h₂ h₃ h₄ h₅) :
(⟨f, e, h₁, h₂, h₃, h₄, h₅⟩ : A₂ ≃ₐ[R] A₁) = e.symm :=
symm_bijective.injective $ ext $ λ x, rfl
@[simp] theorem symm_mk (f f') (h₁ h₂ h₃ h₄ h₅) :
(⟨f, f', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm =
{ to_fun := f', inv_fun := f,
..(⟨f, f', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm } := rfl
/-- Algebra equivalences are transitive. -/
@[trans]
def trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : A₁ ≃ₐ[R] A₃ :=
{ commutes' := λ r, show e₂.to_fun (e₁.to_fun _) = _, by rw [e₁.commutes', e₂.commutes'],
..(e₁.to_ring_equiv.trans e₂.to_ring_equiv), }
@[simp] lemma apply_symm_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e (e.symm x) = x :=
e.to_equiv.apply_symm_apply
@[simp] lemma symm_apply_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e.symm (e x) = x :=
e.to_equiv.symm_apply_apply
@[simp] lemma coe_trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) :
⇑(e₁.trans e₂) = e₂ ∘ e₁ := rfl
lemma trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₁) :
(e₁.trans e₂) x = e₂ (e₁ x) := rfl
@[simp] lemma comp_symm (e : A₁ ≃ₐ[R] A₂) :
alg_hom.comp (e : A₁ →ₐ[R] A₂) ↑e.symm = alg_hom.id R A₂ :=
by { ext, simp }
@[simp] lemma symm_comp (e : A₁ ≃ₐ[R] A₂) :
alg_hom.comp ↑e.symm (e : A₁ →ₐ[R] A₂) = alg_hom.id R A₁ :=
by { ext, simp }
theorem left_inverse_symm (e : A₁ ≃ₐ[R] A₂) : function.left_inverse e.symm e := e.left_inv
theorem right_inverse_symm (e : A₁ ≃ₐ[R] A₂) : function.right_inverse e.symm e := e.right_inv
/-- If `A₁` is equivalent to `A₁'` and `A₂` is equivalent to `A₂'`, then the type of maps
`A₁ →ₐ[R] A₂` is equivalent to the type of maps `A₁' →ₐ[R] A₂'`. -/
def arrow_congr {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂']
(e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (A₁ →ₐ[R] A₂) ≃ (A₁' →ₐ[R] A₂') :=
{ to_fun := λ f, (e₂.to_alg_hom.comp f).comp e₁.symm.to_alg_hom,
inv_fun := λ f, (e₂.symm.to_alg_hom.comp f).comp e₁.to_alg_hom,
left_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, symm_comp],
simp only [←alg_hom.comp_assoc, symm_comp, alg_hom.id_comp, alg_hom.comp_id] },
right_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, comp_symm],
simp only [←alg_hom.comp_assoc, comp_symm, alg_hom.id_comp, alg_hom.comp_id] } }
lemma arrow_congr_comp {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃']
[algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂')
(e₃ : A₃ ≃ₐ[R] A₃') (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₃) :
arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) :=
by { ext, simp only [arrow_congr, equiv.coe_fn_mk, alg_hom.comp_apply],
congr, exact (e₂.symm_apply_apply _).symm }
@[simp] lemma arrow_congr_refl :
arrow_congr alg_equiv.refl alg_equiv.refl = equiv.refl (A₁ →ₐ[R] A₂) :=
by { ext, refl }
@[simp] lemma arrow_congr_trans {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃']
[algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₂) (e₁' : A₁' ≃ₐ[R] A₂')
(e₂ : A₂ ≃ₐ[R] A₃) (e₂' : A₂' ≃ₐ[R] A₃') :
arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') :=
by { ext, refl }
@[simp] lemma arrow_congr_symm {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂']
[algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') :
(arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm :=
by { ext, refl }
/-- If an algebra morphism has an inverse, it is a algebra isomorphism. -/
def of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ : f.comp g = alg_hom.id R A₂)
(h₂ : g.comp f = alg_hom.id R A₁) : A₁ ≃ₐ[R] A₂ :=
{ inv_fun := g,
left_inv := alg_hom.ext_iff.1 h₂,
right_inv := alg_hom.ext_iff.1 h₁,
..f }
/-- Promotes a bijective algebra homomorphism to an algebra equivalence. -/
noncomputable def of_bijective (f : A₁ →ₐ[R] A₂) (hf : function.bijective f) : A₁ ≃ₐ[R] A₂ :=
{ .. ring_equiv.of_bijective (f : A₁ →+* A₂) hf, .. f }
/-- Forgetting the multiplicative structures, an equivalence of algebras is a linear equivalence. -/
def to_linear_equiv (e : A₁ ≃ₐ[R] A₂) : A₁ ≃ₗ[R] A₂ :=
{ to_fun := e.to_fun,
map_add' := λ x y, by simp,
map_smul' := λ r x, by simp [algebra.smul_def''],
inv_fun := e.symm.to_fun,
left_inv := e.left_inv,
right_inv := e.right_inv, }
@[simp] lemma to_linear_equiv_apply (e : A₁ ≃ₐ[R] A₂) (x : A₁) : e.to_linear_equiv x = e x := rfl
theorem to_linear_equiv_inj {e₁ e₂ : A₁ ≃ₐ[R] A₂} (H : e₁.to_linear_equiv = e₂.to_linear_equiv) :
e₁ = e₂ :=
ext $ λ x, show e₁.to_linear_equiv x = e₂.to_linear_equiv x, by rw H
/-- Interpret an algebra equivalence as a linear map. -/
def to_linear_map : A₁ →ₗ[R] A₂ :=
e.to_alg_hom.to_linear_map
@[simp] lemma to_alg_hom_to_linear_map :
(e : A₁ →ₐ[R] A₂).to_linear_map = e.to_linear_map := rfl
@[simp] lemma to_linear_equiv_to_linear_map :
e.to_linear_equiv.to_linear_map = e.to_linear_map := rfl
@[simp] lemma to_linear_map_apply (x : A₁) : e.to_linear_map x = e x := rfl
theorem to_linear_map_inj {e₁ e₂ : A₁ ≃ₐ[R] A₂} (H : e₁.to_linear_map = e₂.to_linear_map) :
e₁ = e₂ :=
ext $ λ x, show e₁.to_linear_map x = e₂.to_linear_map x, by rw H
@[simp] lemma trans_to_linear_map (f : A₁ ≃ₐ[R] A₂) (g : A₂ ≃ₐ[R] A₃) :
(f.trans g).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl
section of_linear_equiv
variables (l : A₁ ≃ₗ[R] A₂)
(map_mul : ∀ x y : A₁, l (x * y) = l x * l y)
(commutes : ∀ r : R, l (algebra_map R A₁ r) = algebra_map R A₂ r)
/--
Upgrade a linear equivalence to an algebra equivalence,
given that it distributes over multiplication and action of scalars.
-/
def of_linear_equiv : A₁ ≃ₐ[R] A₂ :=
{ to_fun := l,
inv_fun := l.symm,
map_mul' := map_mul,
commutes' := commutes,
..l }
@[simp] lemma of_linear_equiv_to_linear_equiv (map_mul) (commutes) :
of_linear_equiv e.to_linear_equiv map_mul commutes = e :=
by { ext, refl }
@[simp] lemma to_linear_equiv_of_linear_equiv :
to_linear_equiv (of_linear_equiv l map_mul commutes) = l :=
by { ext, refl }
@[simp] lemma of_linear_equiv_apply (x : A₁) : of_linear_equiv l map_mul commutes x = l x := rfl
end of_linear_equiv
instance aut : group (A₁ ≃ₐ[R] A₁) :=
{ mul := λ ϕ ψ, ψ.trans ϕ,
mul_assoc := λ ϕ ψ χ, rfl,
one := 1,
one_mul := λ ϕ, by { ext, refl },
mul_one := λ ϕ, by { ext, refl },
inv := symm,
mul_left_inv := λ ϕ, by { ext, exact symm_apply_apply ϕ a } }
@[simp] lemma mul_apply (e₁ e₂ : A₁ ≃ₐ[R] A₁) (x : A₁) : (e₁ * e₂) x = e₁ (e₂ x) := rfl
/-- An algebra isomorphism induces a group isomorphism between automorphism groups -/
@[simps apply]
def aut_congr (ϕ : A₁ ≃ₐ[R] A₂) : (A₁ ≃ₐ[R] A₁) ≃* (A₂ ≃ₐ[R] A₂) :=
{ to_fun := λ ψ, ϕ.symm.trans (ψ.trans ϕ),
inv_fun := λ ψ, ϕ.trans (ψ.trans ϕ.symm),
left_inv := λ ψ, by { ext, simp_rw [trans_apply, symm_apply_apply] },
right_inv := λ ψ, by { ext, simp_rw [trans_apply, apply_symm_apply] },
map_mul' := λ ψ χ, by { ext, simp only [mul_apply, trans_apply, symm_apply_apply] } }
@[simp] lemma aut_congr_refl : aut_congr (alg_equiv.refl) = mul_equiv.refl (A₁ ≃ₐ[R] A₁) :=
by { ext, refl }
@[simp] lemma aut_congr_symm (ϕ : A₁ ≃ₐ[R] A₂) : (aut_congr ϕ).symm = aut_congr ϕ.symm := rfl
@[simp] lemma aut_congr_trans (ϕ : A₁ ≃ₐ[R] A₂) (ψ : A₂ ≃ₐ[R] A₃) :
(aut_congr ϕ).trans (aut_congr ψ) = aut_congr (ϕ.trans ψ) := rfl
end semiring
section comm_semiring
variables [comm_semiring R] [comm_semiring A₁] [comm_semiring A₂]
variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂)
lemma map_prod {ι : Type*} (f : ι → A₁) (s : finset ι) :
e (∏ x in s, f x) = ∏ x in s, e (f x) :=
e.to_alg_hom.map_prod f s
lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) :
e (f.prod g) = f.prod (λ i a, e (g i a)) :=
e.to_alg_hom.map_finsupp_prod f g
end comm_semiring
section ring
variables [comm_ring R] [ring A₁] [ring A₂]
variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂)
@[simp] lemma map_neg (x) : e (-x) = -e x :=
e.to_alg_hom.map_neg x
@[simp] lemma map_sub (x y) : e (x - y) = e x - e y :=
e.to_alg_hom.map_sub x y
end ring
section division_ring
variables [comm_ring R] [division_ring A₁] [division_ring A₂]
variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂)
@[simp] lemma map_inv (x) : e (x⁻¹) = (e x)⁻¹ :=
e.to_alg_hom.map_inv x
@[simp] lemma map_div (x y) : e (x / y) = e x / e y :=
e.to_alg_hom.map_div x y
end division_ring
end alg_equiv
namespace matrix
/-! ### `matrix` section
Specialize `matrix.one_map` and `matrix.zero_map` to `alg_hom` and `alg_equiv`.
TODO: there should be a way to avoid restating these for each `foo_hom`.
-/
variables {R A₁ A₂ n : Type*} [fintype n]
section semiring
variables [comm_semiring R] [semiring A₁] [algebra R A₁] [semiring A₂] [algebra R A₂]
/-- A version of `matrix.one_map` where `f` is an `alg_hom`. -/
@[simp] lemma alg_hom_map_one [decidable_eq n]
(f : A₁ →ₐ[R] A₂) : (1 : matrix n n A₁).map f = 1 :=
one_map f.map_zero f.map_one
/-- A version of `matrix.one_map` where `f` is an `alg_equiv`. -/
@[simp] lemma alg_equiv_map_one [decidable_eq n]
(f : A₁ ≃ₐ[R] A₂) : (1 : matrix n n A₁).map f = 1 :=
one_map f.map_zero f.map_one
/-- A version of `matrix.zero_map` where `f` is an `alg_hom`. -/
@[simp] lemma alg_hom_map_zero
(f : A₁ →ₐ[R] A₂) : (0 : matrix n n A₁).map f = 0 :=
map_zero f.map_zero
/-- A version of `matrix.zero_map` where `f` is an `alg_equiv`. -/
@[simp] lemma alg_equiv_map_zero
(f : A₁ ≃ₐ[R] A₂) : (0 : matrix n n A₁).map f = 0 :=
map_zero f.map_zero
end semiring
end matrix
namespace algebra
variables (R : Type u) (S : Type v) (A : Type w)
include R S A
/-- `comap R S A` is a type alias for `A`, and has an R-algebra structure defined on it
when `algebra R S` and `algebra S A`. If `S` is an `R`-algebra and `A` is an `S`-algebra then
`algebra.comap.algebra R S A` can be used to provide `A` with a structure of an `R`-algebra.
Other than that, `algebra.comap` is now deprecated and replaced with `is_scalar_tower`. -/
/- This is done to avoid a type class search with meta-variables `algebra R ?m_1` and
`algebra ?m_1 A -/
/- The `nolint` attribute is added because it has unused arguments `R` and `S`, but these are
necessary for synthesizing the appropriate type classes -/
@[nolint unused_arguments]
def comap : Type w := A
instance comap.inhabited [h : inhabited A] : inhabited (comap R S A) := h
instance comap.semiring [h : semiring A] : semiring (comap R S A) := h
instance comap.ring [h : ring A] : ring (comap R S A) := h
instance comap.comm_semiring [h : comm_semiring A] : comm_semiring (comap R S A) := h
instance comap.comm_ring [h : comm_ring A] : comm_ring (comap R S A) := h
instance comap.algebra' [comm_semiring S] [semiring A] [h : algebra S A] :
algebra S (comap R S A) := h
/-- Identity homomorphism `A →ₐ[S] comap R S A`. -/
def comap.to_comap [comm_semiring S] [semiring A] [algebra S A] :
A →ₐ[S] comap R S A := alg_hom.id S A
/-- Identity homomorphism `comap R S A →ₐ[S] A`. -/
def comap.of_comap [comm_semiring S] [semiring A] [algebra S A] :
comap R S A →ₐ[S] A := alg_hom.id S A
variables [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A]
/-- `R ⟶ S` induces `S-Alg ⥤ R-Alg` -/
instance comap.algebra : algebra R (comap R S A) :=
{ smul := λ r x, (algebra_map R S r • x : A),
commutes' := λ r x, algebra.commutes _ _,
smul_def' := λ _ _, algebra.smul_def _ _,
.. (algebra_map S A).comp (algebra_map R S) }
/-- Embedding of `S` into `comap R S A`. -/
def to_comap : S →ₐ[R] comap R S A :=
{ commutes' := λ r, rfl,
.. algebra_map S A }
theorem to_comap_apply (x) : to_comap R S A x = algebra_map S A x := rfl
end algebra
section
variables {R : Type u} {S : Type v} {A : Type w} {B : Type u₁}
variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B]
variables [algebra R S] [algebra S A] [algebra S B]
include R
/-- R ⟶ S induces S-Alg ⥤ R-Alg.
See `alg_hom.restrict_scalars` for the version that uses `is_scalar_tower` instead of `comap`. -/
def alg_hom.comap (φ : A →ₐ[S] B) : algebra.comap R S A →ₐ[R] algebra.comap R S B :=
{ commutes' := λ r, φ.commutes (algebra_map R S r)
..φ }
/-- `alg_hom.comap` for `alg_equiv`.
See `alg_equiv.restrict_scalars` for the version that uses `is_scalar_tower` instead of `comap`. -/
def alg_equiv.comap (φ : A ≃ₐ[S] B) : algebra.comap R S A ≃ₐ[R] algebra.comap R S B :=
{ commutes' := λ r, φ.commutes (algebra_map R S r)
..φ }
end
section nat
variables {R : Type*} [semiring R]
-- Lower the priority so that `algebra.id` is picked most of the time when working with
-- `ℕ`-algebras. This is only an issue since `algebra.id` and `algebra_nat` are not yet defeq.
-- TODO: fix this by adding an `of_nat` field to semirings.
/-- Semiring ⥤ ℕ-Alg -/
@[priority 99] instance algebra_nat : algebra ℕ R :=
{ commutes' := nat.cast_commute,
smul_def' := λ _ _, nsmul_eq_mul _ _,
to_ring_hom := nat.cast_ring_hom R }
instance nat_algebra_subsingleton : subsingleton (algebra ℕ R) :=
⟨λ P Q, by { ext, simp, }⟩
end nat
namespace ring_hom
variables {R S : Type*}
/-- Reinterpret a `ring_hom` as an `ℕ`-algebra homomorphism. -/
def to_nat_alg_hom [semiring R] [semiring S] (f : R →+* S) :
R →ₐ[ℕ] S :=
{ to_fun := f, commutes' := λ n, by simp, .. f }
/-- Reinterpret a `ring_hom` as a `ℤ`-algebra homomorphism. -/
def to_int_alg_hom [ring R] [ring S] [algebra ℤ R] [algebra ℤ S] (f : R →+* S) :
R →ₐ[ℤ] S :=
{ commutes' := λ n, by simp, .. f }
@[simp] lemma map_rat_algebra_map [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] (f : R →+* S)
(r : ℚ) :
f (algebra_map ℚ R r) = algebra_map ℚ S r :=
ring_hom.ext_iff.1 (subsingleton.elim (f.comp (algebra_map ℚ R)) (algebra_map ℚ S)) r
/-- Reinterpret a `ring_hom` as a `ℚ`-algebra homomorphism. -/
def to_rat_alg_hom [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] (f : R →+* S) :
R →ₐ[ℚ] S :=
{ commutes' := f.map_rat_algebra_map, .. f }
end ring_hom
namespace rat
instance algebra_rat {α} [division_ring α] [char_zero α] : algebra ℚ α :=
(rat.cast_hom α).to_algebra' $ λ r x, r.cast_commute x
@[simp] theorem algebra_map_rat_rat : algebra_map ℚ ℚ = ring_hom.id ℚ :=
subsingleton.elim _ _
-- TODO[gh-6025]: make this an instance once safe to do so
lemma algebra_rat_subsingleton {α} [semiring α] :
subsingleton (algebra ℚ α) :=
⟨λ x y, algebra.algebra_ext x y $ ring_hom.congr_fun $ subsingleton.elim _ _⟩
end rat
namespace algebra
open module
variables (R : Type u) (A : Type v)
variables [comm_semiring R] [semiring A] [algebra R A]
/-- `algebra_map` as an `alg_hom`. -/
def of_id : R →ₐ[R] A :=
{ commutes' := λ _, rfl, .. algebra_map R A }
variables {R}
theorem of_id_apply (r) : of_id R A r = algebra_map R A r := rfl
variables (R A)
/-- The multiplication in an algebra is a bilinear map. -/
def lmul : A →ₐ[R] (End R A) :=
{ map_one' := by { ext a, exact one_mul a },
map_mul' := by { intros a b, ext c, exact mul_assoc a b c },
map_zero' := by { ext a, exact zero_mul a },
commutes' := by { intro r, ext a, dsimp, rw [smul_def] },
.. (show A →ₗ[R] A →ₗ[R] A, from linear_map.mk₂ R (*)
(λ x y z, add_mul x y z)
(λ c x y, by rw [smul_def, smul_def, mul_assoc _ x y])
(λ x y z, mul_add x y z)
(λ c x y, by rw [smul_def, smul_def, left_comm])) }
variables {A}
/-- The multiplication on the left in an algebra is a linear map. -/
def lmul_left (r : A) : A →ₗ A :=
lmul R A r
/-- The multiplication on the right in an algebra is a linear map. -/
def lmul_right (r : A) : A →ₗ A :=
(lmul R A).to_linear_map.flip r
/-- Simultaneous multiplication on the left and right is a linear map. -/
def lmul_left_right (vw: A × A) : A →ₗ[R] A :=
(lmul_right R vw.2).comp (lmul_left R vw.1)
/-- The multiplication map on an algebra, as an `R`-linear map from `A ⊗[R] A` to `A`. -/
def lmul' : A ⊗[R] A →ₗ[R] A :=
tensor_product.lift (lmul R A).to_linear_map
variables {R A}
@[simp] lemma lmul_apply (p q : A) : lmul R A p q = p * q := rfl
@[simp] lemma lmul_left_apply (p q : A) : lmul_left R p q = p * q := rfl
@[simp] lemma lmul_right_apply (p q : A) : lmul_right R p q = q * p := rfl
@[simp] lemma lmul_left_right_apply (vw : A × A) (p : A) :
lmul_left_right R vw p = vw.1 * p * vw.2 := rfl
@[simp] lemma lmul_left_one : lmul_left R (1:A) = linear_map.id :=
by { ext, simp only [linear_map.id_coe, one_mul, id.def, lmul_left_apply] }
@[simp] lemma lmul_left_mul (a b : A) :
lmul_left R (a * b) = (lmul_left R a).comp (lmul_left R b) :=
by { ext, simp only [lmul_left_apply, linear_map.comp_apply, mul_assoc] }
@[simp] lemma lmul_right_one : lmul_right R (1:A) = linear_map.id :=
by { ext, simp only [linear_map.id_coe, mul_one, id.def, lmul_right_apply] }
@[simp] lemma lmul_right_mul (a b : A) :
lmul_right R (a * b) = (lmul_right R b).comp (lmul_right R a) :=
by { ext, simp only [lmul_right_apply, linear_map.comp_apply, mul_assoc] }
@[simp] lemma lmul'_apply {x y : A} : lmul' R (x ⊗ₜ y) = x * y :=
by simp only [algebra.lmul', tensor_product.lift.tmul, alg_hom.to_linear_map_apply, lmul_apply]
instance linear_map.module' (R : Type u) [comm_semiring R]
(M : Type v) [add_comm_monoid M] [module R M]
(S : Type w) [comm_semiring S] [algebra R S] : module S (M →ₗ[R] S) :=
{ smul := λ s f, linear_map.llcomp _ _ _ _ (algebra.lmul R S s) f,
one_smul := λ f, linear_map.ext $ λ x, one_mul _,
mul_smul := λ s₁ s₂ f, linear_map.ext $ λ x, mul_assoc _ _ _,
smul_add := λ s f g, linear_map.map_add _ _ _,
smul_zero := λ s, linear_map.map_zero _,
add_smul := λ s₁ s₂ f, linear_map.ext $ λ x, add_mul _ _ _,
zero_smul := λ f, linear_map.ext $ λ x, zero_mul _ }
end algebra
section ring
namespace algebra
variables {R A : Type*} [comm_semiring R] [ring A] [algebra R A]
lemma lmul_left_injective [no_zero_divisors A] {x : A} (hx : x ≠ 0) :
function.injective (lmul_left R x) :=
by { letI : domain A := { exists_pair_ne := ⟨x, 0, hx⟩, ..‹ring A›, ..‹no_zero_divisors A› },
exact mul_right_injective' hx }
lemma lmul_right_injective [no_zero_divisors A] {x : A} (hx : x ≠ 0) :
function.injective (lmul_right R x) :=
by { letI : domain A := { exists_pair_ne := ⟨x, 0, hx⟩, ..‹ring A›, ..‹no_zero_divisors A› },
exact mul_left_injective' hx }
lemma lmul_injective [no_zero_divisors A] {x : A} (hx : x ≠ 0) :
function.injective (lmul R A x) :=
by { letI : domain A := { exists_pair_ne := ⟨x, 0, hx⟩, ..‹ring A›, ..‹no_zero_divisors A› },
exact mul_right_injective' hx }
end algebra
end ring
section int
variables (R : Type*) [ring R]
-- Lower the priority so that `algebra.id` is picked most of the time when working with
-- `ℤ`-algebras. This is only an issue since `algebra.id ℤ` and `algebra_int ℤ` are not yet defeq.
-- TODO: fix this by adding an `of_int` field to rings.
/-- Ring ⥤ ℤ-Alg -/
@[priority 99] instance algebra_int : algebra ℤ R :=
{ commutes' := int.cast_commute,
smul_def' := λ _ _, gsmul_eq_mul _ _,
to_ring_hom := int.cast_ring_hom R }
variables {R}
instance int_algebra_subsingleton : subsingleton (algebra ℤ R) :=
⟨λ P Q, by { ext, simp, }⟩
end int
/-!
The R-algebra structure on `Π i : I, A i` when each `A i` is an R-algebra.
We couldn't set this up back in `algebra.pi_instances` because this file imports it.
-/
namespace pi
variable {I : Type u} -- The indexing type
variable {R : Type*} -- The scalar type
variable {f : I → Type v} -- The family of types already equipped with instances
variables (x y : Π i, f i) (i : I)
variables (I f)
instance algebra {r : comm_semiring R}
[s : ∀ i, semiring (f i)] [∀ i, algebra R (f i)] :
algebra R (Π i : I, f i) :=
{ commutes' := λ a f, begin ext, simp [algebra.commutes], end,
smul_def' := λ a f, begin ext, simp [algebra.smul_def''], end,
..pi.ring_hom (λ i, algebra_map R (f i)) }
@[simp] lemma algebra_map_apply {r : comm_semiring R}
[s : ∀ i, semiring (f i)] [∀ i, algebra R (f i)] (a : R) (i : I) :
algebra_map R (Π i, f i) a i = algebra_map R (f i) a := rfl
-- One could also build a `Π i, R i`-algebra structure on `Π i, A i`,
-- when each `A i` is an `R i`-algebra, although I'm not sure that it's useful.
variables (R) (f)
/-- `function.eval` as an `alg_hom`. The name matches `ring_hom.apply`, `monoid_hom.apply`, etc. -/
@[simps]
def alg_hom.apply {r : comm_semiring R} [Π i, semiring (f i)] [Π i, algebra R (f i)] (i : I) :
(Π i, f i) →ₐ[R] f i :=
{ commutes' := λ r, rfl, .. ring_hom.apply f i}
end pi
section is_scalar_tower
variables {R : Type*} [comm_semiring R]
variables (A : Type*) [semiring A] [algebra R A]
variables {M : Type*} [add_comm_monoid M] [module A M] [module R M] [is_scalar_tower R A M]
variables {N : Type*} [add_comm_monoid N] [module A N] [module R N] [is_scalar_tower R A N]
lemma algebra_compatible_smul (r : R) (m : M) : r • m = ((algebra_map R A) r) • m :=
by rw [←(one_smul A m), ←smul_assoc, algebra.smul_def, mul_one, one_smul]
@[simp] lemma algebra_map_smul (r : R) (m : M) : ((algebra_map R A) r) • m = r • m :=
(algebra_compatible_smul A r m).symm
variable {A}
@[priority 100] -- see Note [lower instance priority]
instance is_scalar_tower.to_smul_comm_class : smul_comm_class R A M :=
⟨λ r a m, by rw [algebra_compatible_smul A r (a • m), smul_smul, algebra.commutes, mul_smul,
←algebra_compatible_smul]⟩
@[priority 100] -- see Note [lower instance priority]
instance is_scalar_tower.to_smul_comm_class' : smul_comm_class A R M :=
smul_comm_class.symm _ _ _
lemma smul_algebra_smul_comm (r : R) (a : A) (m : M) : a • r • m = r • a • m :=
smul_comm _ _ _
namespace linear_map
instance coe_is_scalar_tower : has_coe (M →ₗ[A] N) (M →ₗ[R] N) :=
⟨restrict_scalars R⟩
variables (R) {A M N}
@[simp, norm_cast squash] lemma coe_restrict_scalars_eq_coe (f : M →ₗ[A] N) :
(f.restrict_scalars R : M → N) = f := rfl
@[simp, norm_cast squash] lemma coe_coe_is_scalar_tower (f : M →ₗ[A] N) :
((f : M →ₗ[R] N) : M → N) = f := rfl
/-- `A`-linearly coerce a `R`-linear map from `M` to `A` to a function, given an algebra `A` over
a commutative semiring `R` and `M` a module over `R`. -/
def lto_fun (R : Type u) (M : Type v) (A : Type w)
[comm_semiring R] [add_comm_monoid M] [module R M] [comm_ring A] [algebra R A] :
(M →ₗ[R] A) →ₗ[A] (M → A) :=
{ to_fun := linear_map.to_fun,
map_add' := λ f g, rfl,
map_smul' := λ c f, rfl }
end linear_map
end is_scalar_tower
section restrict_scalars
/- In this section, we describe restriction of scalars: if `S` is an algebra over `R`, then
`S`-modules are also `R`-modules. -/
section type_synonym
variables (R A M : Type*)
/--
Warning: use this type synonym judiciously!
The preferred way of working with an `A`-module `M` as `R`-module (where `A` is an `R`-algebra),
is by `[module R M] [module A M] [is_scalar_tower R A M]`.
When `M` is a module over a ring `A`, and `A` is an algebra over `R`, then `M` inherits a
module structure over `R`, provided as a type synonym `module.restrict_scalars R A M := M`.
-/
@[nolint unused_arguments]
def restrict_scalars (R A M : Type*) : Type* := M
instance [I : inhabited M] : inhabited (restrict_scalars R A M) := I
instance [I : add_comm_monoid M] : add_comm_monoid (restrict_scalars R A M) := I
instance [I : add_comm_group M] : add_comm_group (restrict_scalars R A M) := I
instance restrict_scalars.module_orig [semiring A] [add_comm_monoid M] [I : module A M] :
module A (restrict_scalars R A M) := I
variables [comm_semiring R] [semiring A] [algebra R A]
variables [add_comm_monoid M] [module A M]
/--
When `M` is a module over a ring `A`, and `A` is an algebra over `R`, then `M` inherits a
module structure over `R`.
The preferred way of setting this up is `[module R M] [module A M] [is_scalar_tower R A M]`.
-/
instance : module R (restrict_scalars R A M) :=
module.comp_hom M (algebra_map R A)
lemma restrict_scalars_smul_def (c : R) (x : restrict_scalars R A M) :
c • x = ((algebra_map R A c) • x : M) := rfl
instance : is_scalar_tower R A (restrict_scalars R A M) :=
⟨λ r A M, by { rw [algebra.smul_def, mul_smul], refl }⟩
instance submodule.restricted_module (V : submodule A M) :
module R V :=
restrict_scalars.module R A V
instance submodule.restricted_module_is_scalar_tower (V : submodule A M) :
is_scalar_tower R A V :=
restrict_scalars.is_scalar_tower R A V
end type_synonym
/-! TODO: The following lemmas no longer involve `algebra` at all, and could be moved closer
to `algebra/module/submodule.lean`. Currently this is tricky because `ker`, `range`, `⊤`, and `⊥`
are all defined in `linear_algebra/basic.lean`. -/
section module
open module
variables (R S M N : Type*) [semiring R] [semiring S] [has_scalar R S]
variables [add_comm_monoid M] [module R M] [module S M] [is_scalar_tower R S M]
variables [add_comm_monoid N] [module R N] [module S N] [is_scalar_tower R S N]
variables {S M N}
namespace submodule
/--
`V.restrict_scalars R` is the `R`-submodule of the `R`-module given by restriction of scalars,
corresponding to `V`, an `S`-submodule of the original `S`-module.
-/
@[simps]
def restrict_scalars (V : submodule S M) : submodule R M :=
{ carrier := V.carrier,
zero_mem' := V.zero_mem,
smul_mem' := λ c m h, V.smul_of_tower_mem c h,
add_mem' := λ x y hx hy, V.add_mem hx hy }
@[simp]
lemma restrict_scalars_mem (V : submodule S M) (m : M) :
m ∈ V.restrict_scalars R ↔ m ∈ V :=
iff.refl _
variables (R S M)
lemma restrict_scalars_injective :
function.injective (restrict_scalars R : submodule S M → submodule R M) :=
λ V₁ V₂ h, ext $ by convert set.ext_iff.1 (set_like.ext'_iff.1 h); refl
@[simp] lemma restrict_scalars_inj {V₁ V₂ : submodule S M} :
restrict_scalars R V₁ = restrict_scalars R V₂ ↔ V₁ = V₂ :=
(restrict_scalars_injective R _ _).eq_iff
@[simp]
lemma restrict_scalars_bot : restrict_scalars R (⊥ : submodule S M) = ⊥ := rfl
@[simp]
lemma restrict_scalars_top : restrict_scalars R (⊤ : submodule S M) = ⊤ := rfl
/-- If `S` is an `R`-algebra, then the `R`-module generated by a set `X` is included in the
`S`-module generated by `X`. -/
lemma span_le_restrict_scalars (X : set M) : span R (X : set M) ≤ restrict_scalars R (span S X) :=
submodule.span_le.mpr submodule.subset_span
end submodule
@[simp]
lemma linear_map.ker_restrict_scalars (f : M →ₗ[S] N) :
(f.restrict_scalars R).ker = f.ker.restrict_scalars R :=
rfl
end module
end restrict_scalars
namespace submodule
variables (R A M : Type*)
variables [comm_semiring R] [semiring A] [algebra R A] [add_comm_monoid M]
variables [module R M] [module A M] [is_scalar_tower R A M]
/-- If `A` is an `R`-algebra such that the induced morhpsim `R →+* A` is surjective, then the
`R`-module generated by a set `X` equals the `A`-module generated by `X`. -/
lemma span_eq_restrict_scalars (X : set M) (hsur : function.surjective (algebra_map R A)) :
span R X = restrict_scalars R (span A X) :=
begin
apply (span_le_restrict_scalars R A M X).antisymm (λ m hm, _),
refine span_induction hm subset_span (zero_mem _) (λ _ _, add_mem _) (λ a m hm, _),
obtain ⟨r, rfl⟩ := hsur a,
simpa [algebra_map_smul] using smul_mem _ r hm
end
end submodule
|
b6f22435c742f1a1b875368f17b838af91d79329 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/algebra/category/Algebra/basic.lean | 5aef979c8b94418aea0708dab21dca89d3ea6a07 | [
"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 | 4,501 | 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.algebra.basic
import algebra.algebra.subalgebra
import algebra.free_algebra
import algebra.category.CommRing.basic
import algebra.category.Module.basic
open category_theory
open category_theory.limits
universes v u
variables (R : Type u) [comm_ring R]
/-- The category of R-modules and their morphisms. -/
structure Algebra :=
(carrier : Type v)
[is_ring : ring carrier]
[is_algebra : algebra R carrier]
attribute [instance] Algebra.is_ring Algebra.is_algebra
namespace Algebra
instance : has_coe_to_sort (Algebra R) :=
{ S := Type v, coe := Algebra.carrier }
instance : category (Algebra.{v} R) :=
{ hom := λ A B, A →ₐ[R] B,
id := λ A, alg_hom.id R A,
comp := λ A B C f g, g.comp f }
instance : concrete_category (Algebra.{v} R) :=
{ forget := { obj := λ R, R, map := λ R S f, (f : R → S) },
forget_faithful := { } }
instance has_forget_to_Ring : has_forget₂ (Algebra R) Ring.{v} :=
{ forget₂ :=
{ obj := λ A, Ring.of A,
map := λ A₁ A₂ f, alg_hom.to_ring_hom f, } }
instance has_forget_to_Module : has_forget₂ (Algebra R) (Module R) :=
{ forget₂ :=
{ obj := λ M, Module.of R M,
map := λ M₁ M₂ f, alg_hom.to_linear_map f, } }
/-- The object in the category of R-algebras associated to a type equipped with the appropriate
typeclasses. -/
def of (X : Type v) [ring X] [algebra R X] : Algebra R := ⟨X⟩
instance : inhabited (Algebra R) := ⟨of R R⟩
@[simp]
lemma coe_of (X : Type u) [ring X] [algebra R X] : (of R X : Type u) = X := rfl
variables {R}
/-- Forgetting to the underlying type and then building the bundled object returns the original
algebra. -/
@[simps]
def of_self_iso (M : Algebra R) : Algebra.of R M ≅ M :=
{ hom := 𝟙 M, inv := 𝟙 M }
variables {R} {M N U : Module.{v} R}
@[simp] lemma id_apply (m : M) : (𝟙 M : M → M) m = m := rfl
@[simp] lemma coe_comp (f : M ⟶ N) (g : N ⟶ U) :
((f ≫ g) : M → U) = g ∘ f := rfl
variables (R)
/-- The "free algebra" functor, sending a type `S` to the free algebra on `S`. -/
@[simps]
def free : Type* ⥤ Algebra R :=
{ obj := λ S,
{ carrier := free_algebra R S,
is_ring := algebra.semiring_to_ring R },
map := λ S T f, free_algebra.lift _ $ (free_algebra.ι _) ∘ f }
/-- The free/forget ajunction for `R`-algebras. -/
@[simps]
def adj : free R ⊣ forget (Algebra R) :=
{ hom_equiv := λ X A,
{ to_fun := λ f, f ∘ (free_algebra.ι _),
inv_fun := λ f, free_algebra.lift _ f,
left_inv := by tidy,
right_inv := by tidy },
unit := { app := λ S, free_algebra.ι _ },
counit :=
{ app := λ S, free_algebra.lift _ $ id,
naturality' := by {intros, ext, simp} } } -- tidy times out :(
end Algebra
variables {R}
variables {X₁ X₂ : Type u}
/-- Build an isomorphism in the category `Algebra R` from a `alg_equiv` between `algebra`s. -/
@[simps]
def alg_equiv.to_Algebra_iso
{g₁ : ring X₁} {g₂ : ring X₂} {m₁ : algebra R X₁} {m₂ : algebra R X₂} (e : X₁ ≃ₐ[R] X₂) :
Algebra.of R X₁ ≅ Algebra.of R X₂ :=
{ hom := (e : X₁ →ₐ[R] X₂),
inv := (e.symm : X₂ →ₐ[R] X₁),
hom_inv_id' := begin ext, exact e.left_inv x, end,
inv_hom_id' := begin ext, exact e.right_inv x, end, }
namespace category_theory.iso
/-- Build a `alg_equiv` from an isomorphism in the category `Algebra R`. -/
@[simps]
def to_alg_equiv {X Y : Algebra R} (i : X ≅ Y) : X ≃ₐ[R] Y :=
{ to_fun := i.hom,
inv_fun := i.inv,
left_inv := by tidy,
right_inv := by tidy,
map_add' := by tidy,
map_mul' := by tidy,
commutes' := by tidy, }.
end category_theory.iso
/-- Algebra equivalences between `algebras`s are the same as (isomorphic to) isomorphisms in
`Algebra`. -/
@[simps]
def alg_equiv_iso_Algebra_iso {X Y : Type u}
[ring X] [ring Y] [algebra R X] [algebra R Y] :
(X ≃ₐ[R] Y) ≅ (Algebra.of R X ≅ Algebra.of R Y) :=
{ hom := λ e, e.to_Algebra_iso,
inv := λ i, i.to_alg_equiv, }
instance (X : Type u) [ring X] [algebra R X] : has_coe (subalgebra R X) (Algebra R) :=
⟨ λ N, Algebra.of R N ⟩
instance Algebra.forget_reflects_isos : reflects_isomorphisms (forget (Algebra.{u} R)) :=
{ reflects := λ X Y f _,
begin
resetI,
let i := as_iso ((forget (Algebra.{u} R)).map f),
let e : X ≃ₐ[R] Y := { ..f, ..i.to_equiv },
exact { ..e.to_Algebra_iso },
end }
|
83ab5d0542553ffec576c32748291a650ba78d00 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /hott/choice.hlean | 7ccec27220fc03de44f0cf0d77e35501a808d97e | [
"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 | 3,522 | hlean | import types.trunc types.bool
open eq bool equiv sigma sigma.ops trunc is_trunc pi
namespace choice
universe variable u
-- 3.8.1. The axiom of choice.
definition AC [reducible] := Π (X : Type.{u}) (A : X -> Type.{u}) (P : Π x, A x -> Type.{u}),
is_set X -> (Π x, is_set (A x)) -> (Π x a, is_prop (P x a)) ->
(Π x, ∥ Σ a, P x a ∥) -> ∥ Σ f, Π x, P x (f x) ∥
-- 3.8.3. Corresponds to the assertion that
-- "the cartesian product of a family of nonempty sets is nonempty".
definition AC_cart [reducible] := Π (X : Type.{u}) (A : X -> Type.{u}),
is_set X -> (Π x, is_set (A x)) -> (Π x, ∥ A x ∥) -> ∥ Π x, A x ∥
-- A slight variant of AC with a modified (equivalent) codomain.
definition AC' [reducible] := Π (X : Type.{u}) (A : X -> Type.{u}) (P : Π x, A x -> Type.{u}),
is_set X -> (Π x, is_set (A x)) -> (Π x a, is_prop (P x a))
-> (Π x, ∥ Σ a, P x a ∥) -> ∥ Π x, Σ a : A x, P x a ∥
-- The equivalence of AC and AC' follows from the equivalence of their codomains.
definition AC_equiv_AC' : AC.{u} ≃ AC'.{u} :=
equiv_of_is_prop
(λ H X A P HX HA HP HI, trunc_functor _ (to_fun !sigma_pi_equiv_pi_sigma) (H X A P HX HA HP HI))
(λ H X A P HX HA HP HI, trunc_functor _ (to_inv !sigma_pi_equiv_pi_sigma) (H X A P HX HA HP HI))
-- AC_cart can be derived from AC' by setting P := λ _ _ , unit.
definition AC_cart_of_AC' : AC'.{u} -> AC_cart.{u} :=
λ H X A HX HA HI, trunc_functor _ (λ H0 x, (H0 x).1)
(H X A (λ x a, lift.{0 u} unit) HX HA (λ x a, !is_trunc_lift)
(λ x, trunc_functor _ (λ a, ⟨a, lift.up.{0 u} unit.star⟩) (HI x)))
-- And the converse, by setting A := λ x, Σ a, P x a.
definition AC'_of_AC_cart : AC_cart.{u} -> AC'.{u} :=
by intro H X A P HX HA HP HI;
apply H X (λ x, Σ a, P x a) HX (λ x, !is_trunc_sigma) HI
-- Which is enough to show AC' ≃ AC_cart, since both are props.
definition AC'_equiv_AC_cart : AC'.{u} ≃ AC_cart.{u} :=
equiv_of_is_prop AC_cart_of_AC'.{u} AC'_of_AC_cart.{u}
-- 3.8.2. AC ≃ AC_cart follows by transitivity.
definition AC_equiv_AC_cart : AC.{u} ≃ AC_cart.{u} :=
equiv.trans AC_equiv_AC' AC'_equiv_AC_cart
namespace example385
definition X : Type.{1} := Σ A : Type.{0}, ∥ A = bool ∥
definition x0 : X := ⟨bool, merely.intro _ rfl⟩
definition Y : X -> Type.{1} := λ x, x0 = x
definition not_is_set_X : ¬ is_set X :=
begin
intro H, apply not_is_prop_bool_eq_bool,
apply @is_trunc_equiv_closed (x0 = x0),
apply equiv.symm !equiv_subtype
end
definition is_set_x1 (x : X) : is_set x.1 :=
by cases x; induction a_1; cases a_1; exact _
definition is_set_Yx (x : X) : is_set (Y x) :=
begin
apply @is_trunc_equiv_closed _ _ _ !equiv_subtype,
apply @is_trunc_equiv_closed _ _ _ (equiv.symm !eq_equiv_equiv),
apply is_trunc_equiv; repeat (apply is_set_x1)
end
definition trunc_Yx (x : X) : ∥ Y x ∥ :=
begin
cases x, induction a_1, apply merely.intro,
apply to_fun !equiv_subtype, rewrite a_1
end
end example385
open example385
-- 3.8.5. There exists a type X and a family Y : X → U such that each Y(x) is a set,
-- but such that (3.8.3) is false.
definition X_must_be_set : Σ (X : Type.{1}) (Y : X -> Type.{1})
(HA : Π x : X, is_set (Y x)), ¬ ((Π x : X, ∥ Y x ∥) -> ∥ Π x : X, Y x ∥) :=
⟨X, Y, is_set_Yx, λ H, trunc.rec_on (H trunc_Yx)
(λ H0, not_is_set_X (@is_trunc_of_is_contr _ _ (is_contr.mk x0 H0)))⟩
end choice
|
d53fe02747031919ce5fdf0f8d9673592cd72c12 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/number_theory/liouville/residual.lean | a00e4c72587c303862d3a998a7ccd556c14bb524 | [
"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,821 | 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 number_theory.liouville.basic
import topology.metric_space.baire
import topology.instances.irrational
/-!
# Density of Liouville numbers
In this file we prove that the set of Liouville numbers form a dense `Gδ` set. We also prove a
similar statement about irrational numbers.
-/
open_locale filter
open filter set metric
lemma set_of_liouville_eq_Inter_Union :
{x | liouville x} =
⋂ n : ℕ, ⋃ (a b : ℤ) (hb : 1 < b), ball (a / b) (1 / b ^ n) \ {a / b} :=
begin
ext x,
simp only [mem_Inter, mem_Union, liouville, mem_set_of_eq, exists_prop, mem_diff,
mem_singleton_iff, mem_ball, real.dist_eq, and_comm]
end
lemma is_Gδ_set_of_liouville : is_Gδ {x | liouville x} :=
begin
rw set_of_liouville_eq_Inter_Union,
refine is_Gδ_Inter (λ n, is_open.is_Gδ _),
refine is_open_Union (λ a, is_open_Union $ λ b, is_open_Union $ λ hb, _),
exact is_open_ball.inter is_closed_singleton.is_open_compl
end
lemma set_of_liouville_eq_irrational_inter_Inter_Union :
{x | liouville x} =
{x | irrational x} ∩ ⋂ n : ℕ, ⋃ (a b : ℤ) (hb : 1 < b), ball (a / b) (1 / b ^ n) :=
begin
refine subset.antisymm _ _,
{ refine subset_inter (λ x hx, hx.irrational) _,
rw set_of_liouville_eq_Inter_Union,
exact Inter_mono (λ n, Union₂_mono $ λ a b, Union_mono $ λ hb, diff_subset _ _) },
{ simp only [inter_Inter, inter_Union, set_of_liouville_eq_Inter_Union],
refine Inter_mono (λ n, Union₂_mono $ λ a b, Union_mono $ λ hb, _),
rw [inter_comm],
refine diff_subset_diff subset.rfl (singleton_subset_iff.2 ⟨a / b, _⟩),
norm_cast }
end
/-- The set of Liouville numbers is a residual set. -/
lemma eventually_residual_liouville : ∀ᶠ x in residual ℝ, liouville x :=
begin
rw [filter.eventually, set_of_liouville_eq_irrational_inter_Inter_Union],
refine eventually_residual_irrational.and _,
refine eventually_residual.2 ⟨_, _, rat.dense_embedding_coe_real.dense.mono _, subset.rfl⟩,
{ exact is_Gδ_Inter (λ n, is_open.is_Gδ $ is_open_Union $ λ a, is_open_Union $
λ b, is_open_Union $ λ hb, is_open_ball) },
{ rintro _ ⟨r, rfl⟩,
simp only [mem_Inter, mem_Union],
refine λ n, ⟨r.num * 2, r.denom * 2, _, _⟩,
{ have := int.coe_nat_le.2 r.pos, rw int.coe_nat_one at this, linarith },
{ convert mem_ball_self _ using 2,
{ push_cast, norm_cast, norm_num },
{ refine one_div_pos.2 (pow_pos (int.cast_pos.2 _) _),
exact mul_pos (int.coe_nat_pos.2 r.pos) zero_lt_two } } }
end
/-- The set of Liouville numbers in dense. -/
lemma dense_liouville : dense {x | liouville x} :=
dense_of_mem_residual eventually_residual_liouville
|
0436e635ce3c3945e6e1b5d67cfdbc4b5321930a | e5c11e5a7d990ce404047c2bd848eeafac3c0a85 | /src/integral_closure.lean | 279c492a8a12465d3c2804cc8e3896aeb2761946 | [
"LPPL-1.3c"
] | permissive | lean-forward/class-number | 9ec63c24845e46efc8fa8b15324d0815918292c7 | 4fccf36d5e0e16accae84c16df77a3839ad964e4 | refs/heads/main | 1,686,927,014,542 | 1,624,886,724,000 | 1,624,886,724,000 | 327,319,245 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 26,983 | lean | /-
Copyright (c) 2019 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import ring_theory.adjoin
import ring_theory.algebra_tower
import ring_theory.polynomial.scale_roots
/-!
# Integral closure of a subring.
If A is an R-algebra then `a : A` is integral over R if it is a root of a monic polynomial
with coefficients in R. Enough theory is developed to prove that integral elements
form a sub-R-algebra of A.
## Main definitions
Let `R` be a `comm_ring` and let `A` be an R-algebra.
* `ring_hom.is_integral_elem (f : R →+* A) (x : A)` : `x` is integral with respect to the map `f`,
* `is_integral (x : A)` : `x` is integral over `R`, i.e., is a root of a monic polynomial with
coefficients in `R`.
* `integral_closure R A` : the integral closure of `R` in `A`, regarded as a sub-`R`-algebra of `A`.
-/
open_locale classical
open_locale big_operators
open polynomial submodule
section ring
variables {R S A : Type*}
variables [comm_ring R] [ring A] [ring S] (f : R →+* S)
/-- An element `x` of `A` is said to be integral over `R` with respect to `f`
if it is a root of a monic polynomial `p : polynomial R` evaluated under `f` -/
def ring_hom.is_integral_elem (f : R →+* A) (x : A) :=
∃ p : polynomial R, monic p ∧ eval₂ f x p = 0
/-- A ring homomorphism `f : R →+* A` is said to be integral
if every element `A` is integral with respect to the map `f` -/
def ring_hom.is_integral (f : R →+* A) :=
∀ x : A, f.is_integral_elem x
variables [algebra R A] (R)
/-- An element `x` of an algebra `A` over a commutative ring `R` is said to be *integral*,
if it is a root of some monic polynomial `p : polynomial R`.
Equivalently, the element is integral over `R` with respect to the induced `algebra_map` -/
def is_integral (x : A) : Prop :=
(algebra_map R A).is_integral_elem x
variable (A)
/-- An algebra is integral if every element of the extension is integral over the base ring -/
def algebra.is_integral : Prop :=
(algebra_map R A).is_integral
variables {R A}
lemma ring_hom.is_integral_map {x : R} : f.is_integral_elem (f x) :=
⟨X - C x, monic_X_sub_C _, by simp⟩
theorem is_integral_algebra_map {x : R} : is_integral R (algebra_map R A x) :=
(algebra_map R A).is_integral_map
theorem is_integral_of_noetherian (H : is_noetherian R A) (x : A) :
is_integral R x :=
begin
let leval : @linear_map R (polynomial R) A _ _ _ _ _ := (aeval x).to_linear_map,
let D : ℕ → submodule R A := λ n, (degree_le R n).map leval,
let M := well_founded.min (is_noetherian_iff_well_founded.1 H)
(set.range D) ⟨_, ⟨0, rfl⟩⟩,
have HM : M ∈ set.range D := well_founded.min_mem _ _ _,
cases HM with N HN,
have HM : ¬M < D (N+1) := well_founded.not_lt_min
(is_noetherian_iff_well_founded.1 H) (set.range D) _ ⟨N+1, rfl⟩,
rw ← HN at HM,
have HN2 : D (N+1) ≤ D N := classical.by_contradiction (λ H, HM
(lt_of_le_not_le (map_mono (degree_le_mono
(with_bot.coe_le_coe.2 (nat.le_succ N)))) H)),
have HN3 : leval (X^(N+1)) ∈ D N,
{ exact HN2 (mem_map_of_mem (mem_degree_le.2 (degree_X_pow_le _))) },
rcases HN3 with ⟨p, hdp, hpe⟩,
refine ⟨X^(N+1) - p, monic_X_pow_sub (mem_degree_le.1 hdp), _⟩,
show leval (X ^ (N + 1) - p) = 0,
rw [linear_map.map_sub, hpe, sub_self]
end
theorem is_integral_of_submodule_noetherian (S : subalgebra R A)
(H : is_noetherian R (S : submodule R A)) (x : A) (hx : x ∈ S) :
is_integral R x :=
begin
letI : algebra R S := S.algebra,
letI : ring S := S.ring R A,
suffices : is_integral R (⟨x, hx⟩ : S),
{ rcases this with ⟨p, hpm, hpx⟩,
replace hpx := congr_arg subtype.val hpx,
refine ⟨p, hpm, eq.trans _ hpx⟩,
simp only [aeval_def, eval₂, finsupp.sum],
rw ← p.support.sum_hom subtype.val,
{ refine finset.sum_congr rfl (λ n hn, _),
change _ = _ * _,
rw is_monoid_hom.map_pow coe, refl,
split; intros; refl },
refine { map_add := _, map_zero := _ }; intros; refl },
refine is_integral_of_noetherian H ⟨x, hx⟩
end
end ring
section
variables {R A B S : Type*}
variables [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring S]
variables [algebra R A] [algebra R B] (f : R →+* S)
theorem is_integral_alg_hom (f : A →ₐ[R] B) {x : A} (hx : is_integral R x) : is_integral R (f x) :=
let ⟨p, hp, hpx⟩ := hx in ⟨p, hp, by rw [← aeval_def, aeval_alg_hom_apply, aeval_def, hpx, f.map_zero]⟩
theorem is_integral_of_is_scalar_tower [algebra A B] [is_scalar_tower R A B]
(x : B) (hx : is_integral R x) : is_integral A x :=
let ⟨p, hp, hpx⟩ := hx in
⟨p.map $ algebra_map R A, monic_map _ hp, by rw [← aeval_def, ← is_scalar_tower.aeval_apply, aeval_def, hpx]⟩
section
local attribute [instance] subset.comm_ring algebra.of_is_subring
theorem is_integral_of_subring {x : A} (T : set R) [is_subring T]
(hx : is_integral T x) : is_integral R x :=
is_integral_of_is_scalar_tower x hx
lemma is_integral_algebra_map_iff [algebra A B] [is_scalar_tower R A B]
{x : A} (hAB : function.injective (algebra_map A B)) :
is_integral R (algebra_map A B x) ↔ is_integral R x :=
begin
split; rintros ⟨f, hf, hx⟩; use [f, hf],
{ exact is_scalar_tower.aeval_eq_zero_of_aeval_algebra_map_eq_zero R A B hAB hx },
{ rw [is_scalar_tower.algebra_map_eq R A B, ← hom_eval₂, hx, ring_hom.map_zero] }
end
theorem is_integral_iff_is_integral_closure_finite {r : A} :
is_integral R r ↔ ∃ s : set R, s.finite ∧ is_integral (ring.closure s) r :=
begin
split; intro hr,
{ rcases hr with ⟨p, hmp, hpr⟩,
refine ⟨_, set.finite_mem_finset _, p.restriction, subtype.eq hmp, _⟩,
erw [← aeval_def, is_scalar_tower.aeval_apply _ R, map_restriction, aeval_def, hpr] },
rcases hr with ⟨s, hs, hsr⟩,
exact is_integral_of_subring _ hsr
end
end
theorem fg_adjoin_singleton_of_integral (x : A) (hx : is_integral R x) :
(algebra.adjoin R ({x} : set A) : submodule R A).fg :=
begin
rcases hx with ⟨f, hfm, hfx⟩,
existsi finset.image ((^) x) (finset.range (nat_degree f + 1)),
apply le_antisymm,
{ rw span_le, intros s hs, rw finset.mem_coe at hs,
rcases finset.mem_image.1 hs with ⟨k, hk, rfl⟩, clear hk,
exact is_submonoid.pow_mem (algebra.subset_adjoin (set.mem_singleton _)) },
intros r hr, change r ∈ algebra.adjoin R ({x} : set A) at hr,
rw algebra.adjoin_singleton_eq_range at hr,
rcases (aeval x).mem_range.mp hr with ⟨p, rfl⟩,
rw ← mod_by_monic_add_div p hfm,
rw ← aeval_def at hfx,
rw [alg_hom.map_add, alg_hom.map_mul, hfx, zero_mul, add_zero],
have : degree (p %ₘ f) ≤ degree f := degree_mod_by_monic_le p hfm,
generalize_hyp : p %ₘ f = q at this ⊢,
rw [← sum_C_mul_X_eq q, aeval_def, eval₂_sum, finsupp.sum],
refine sum_mem _ (λ k hkq, _),
rw [eval₂_mul, eval₂_C, eval₂_pow, eval₂_X, ← algebra.smul_def],
refine smul_mem _ _ (subset_span _),
rw finset.mem_coe, refine finset.mem_image.2 ⟨_, _, rfl⟩,
rw [finset.mem_range, nat.lt_succ_iff], refine le_of_not_lt (λ hk, _),
rw [degree_le_iff_coeff_zero] at this,
rw [finsupp.mem_support_iff] at hkq, apply hkq, apply this,
exact lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 hk)
end
theorem fg_adjoin_of_finite {s : set A} (hfs : s.finite)
(his : ∀ x ∈ s, is_integral R x) : (algebra.adjoin R s : submodule R A).fg :=
set.finite.induction_on hfs (λ _, ⟨{1}, submodule.ext $ λ x,
by { erw [algebra.adjoin_empty, finset.coe_singleton, ← one_eq_span, one_eq_map_top,
map_top, linear_map.mem_range, algebra.mem_bot], refl }⟩)
(λ a s has hs ih his, by rw [← set.union_singleton, algebra.adjoin_union_coe_submodule]; exact
fg_mul _ _ (ih $ λ i hi, his i $ set.mem_insert_of_mem a hi)
(fg_adjoin_singleton_of_integral _ $ his a $ set.mem_insert a s)) his
theorem is_integral_of_mem_of_fg (S : subalgebra R A)
(HS : (S : submodule R A).fg) (x : A) (hx : x ∈ S) : is_integral R x :=
begin
cases HS with y hy,
obtain ⟨lx, hlx1, hlx2⟩ :
∃ (l : A →₀ R) (H : l ∈ finsupp.supported R R ↑y), (finsupp.total A A R id) l = x,
{ rwa [←(@finsupp.mem_span_iff_total A A R _ _ _ id ↑y x), set.image_id ↑y, hy] },
have hyS : ∀ {p}, p ∈ y → p ∈ S := λ p hp, show p ∈ (S : submodule R A),
by { rw ← hy, exact subset_span hp },
have : ∀ (jk : (↑(y.product y) : set (A × A))), jk.1.1 * jk.1.2 ∈ (S : submodule R A) :=
λ jk, S.mul_mem (hyS (finset.mem_product.1 jk.2).1) (hyS (finset.mem_product.1 jk.2).2),
rw [← hy, ← set.image_id ↑y] at this, simp only [finsupp.mem_span_iff_total] at this,
choose ly hly1 hly2,
let S₀ : set R := ring.closure ↑(lx.frange ∪ finset.bUnion finset.univ (finsupp.frange ∘ ly)),
refine is_integral_of_subring S₀ _,
letI : comm_ring S₀ := @subtype.comm_ring _ _ _ ring.closure.is_subring,
letI : algebra S₀ A := algebra.of_is_subring _,
have : span S₀ (insert 1 ↑y : set A) * span S₀ (insert 1 ↑y : set A) ≤ span S₀ (insert 1 ↑y : set A),
{ rw span_mul_span, refine span_le.2 (λ z hz, _),
rcases set.mem_mul.1 hz with ⟨p, q, rfl | hp, hq, rfl⟩,
{ rw one_mul, exact subset_span hq },
rcases hq with rfl | hq,
{ rw mul_one, exact subset_span (or.inr hp) },
erw ← hly2 ⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩,
rw [finsupp.total_apply, finsupp.sum],
refine (span S₀ (insert 1 ↑y : set A)).sum_mem (λ t ht, _),
have : ly ⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩ t ∈ S₀ :=
ring.subset_closure (finset.mem_union_right _ $ finset.mem_bUnion.2
⟨⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩, finset.mem_univ _,
finsupp.mem_frange.2 ⟨finsupp.mem_support_iff.1 ht, _, rfl⟩⟩),
change (⟨_, this⟩ : S₀) • t ∈ _, exact smul_mem _ _ (subset_span $ or.inr $ hly1 _ ht) },
haveI : is_subring (span S₀ (insert 1 ↑y : set A) : set A) :=
{ one_mem := subset_span $ or.inl rfl,
mul_mem := λ p q hp hq, this $ mul_mem_mul hp hq,
zero_mem := (span S₀ (insert 1 ↑y : set A)).zero_mem,
add_mem := λ _ _, (span S₀ (insert 1 ↑y : set A)).add_mem,
neg_mem := λ _, (span S₀ (insert 1 ↑y : set A)).neg_mem },
have : span S₀ (insert 1 ↑y : set A) = algebra.adjoin S₀ (↑y : set A),
{ refine le_antisymm (span_le.2 $ set.insert_subset.2
⟨(algebra.adjoin S₀ ↑y).one_mem, algebra.subset_adjoin⟩) (λ z hz, _),
rw [subalgebra.mem_to_submodule, algebra.mem_adjoin_iff] at hz, rw ← submodule.mem_coe,
refine ring.closure_subset (set.union_subset (set.range_subset_iff.2 $ λ t, _)
(λ t ht, subset_span $ or.inr ht)) hz,
rw algebra.algebra_map_eq_smul_one,
exact smul_mem (span S₀ (insert 1 ↑y : set A)) _ (subset_span $ or.inl rfl) },
haveI : is_noetherian_ring ↥S₀ := is_noetherian_ring_closure _ (finset.finite_to_set _),
refine is_integral_of_submodule_noetherian (algebra.adjoin S₀ ↑y)
(is_noetherian_of_fg_of_noetherian _ ⟨insert 1 y, by rw [finset.coe_insert, this]⟩) _ _,
rw [← hlx2, finsupp.total_apply, finsupp.sum], refine subalgebra.sum_mem _ (λ r hr, _),
have : lx r ∈ S₀ := ring.subset_closure (finset.mem_union_left _ (finset.mem_image_of_mem _ hr)),
change (⟨_, this⟩ : S₀) • r ∈ _,
rw finsupp.mem_supported at hlx1,
exact subalgebra.smul_mem _ (algebra.subset_adjoin $ hlx1 hr) _
end
lemma ring_hom.is_integral_of_mem_closure {x y z : S}
(hx : f.is_integral_elem x) (hy : f.is_integral_elem y)
(hz : z ∈ ring.closure ({x, y} : set S)) :
f.is_integral_elem z :=
begin
letI : algebra R S := f.to_algebra,
have := fg_mul _ _ (fg_adjoin_singleton_of_integral x hx) (fg_adjoin_singleton_of_integral y hy),
rw [← algebra.adjoin_union_coe_submodule, set.singleton_union] at this,
exact is_integral_of_mem_of_fg (algebra.adjoin R {x, y}) this z
(algebra.mem_adjoin_iff.2 $ ring.closure_mono (set.subset_union_right _ _) hz),
end
theorem is_integral_of_mem_closure {x y z : A}
(hx : is_integral R x) (hy : is_integral R y)
(hz : z ∈ ring.closure ({x, y} : set A)) :
is_integral R z :=
(algebra_map R A).is_integral_of_mem_closure hx hy hz
lemma ring_hom.is_integral_zero : f.is_integral_elem 0 :=
f.map_zero ▸ f.is_integral_map
theorem is_integral_zero : is_integral R (0:A) :=
(algebra_map R A).is_integral_zero
lemma ring_hom.is_integral_one : f.is_integral_elem 1 :=
f.map_one ▸ f.is_integral_map
theorem is_integral_one : is_integral R (1:A) :=
(algebra_map R A).is_integral_one
lemma ring_hom.is_integral_add {x y : S}
(hx : f.is_integral_elem x) (hy : f.is_integral_elem y) :
f.is_integral_elem (x + y) :=
f.is_integral_of_mem_closure hx hy (is_add_submonoid.add_mem
(ring.subset_closure (or.inl rfl)) (ring.subset_closure (or.inr rfl)))
theorem is_integral_add {x y : A}
(hx : is_integral R x) (hy : is_integral R y) :
is_integral R (x + y) :=
(algebra_map R A).is_integral_add hx hy
lemma ring_hom.is_integral_neg {x : S}
(hx : f.is_integral_elem x) : f.is_integral_elem (-x) :=
f.is_integral_of_mem_closure hx hx (is_add_subgroup.neg_mem
(ring.subset_closure (or.inl rfl)))
theorem is_integral_neg {x : A}
(hx : is_integral R x) : is_integral R (-x) :=
(algebra_map R A).is_integral_neg hx
lemma ring_hom.is_integral_sub {x y : S}
(hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x - y) :=
by simpa only [sub_eq_add_neg] using f.is_integral_add hx (f.is_integral_neg hy)
theorem is_integral_sub {x y : A}
(hx : is_integral R x) (hy : is_integral R y) : is_integral R (x - y) :=
(algebra_map R A).is_integral_sub hx hy
lemma ring_hom.is_integral_mul {x y : S}
(hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x * y) :=
f.is_integral_of_mem_closure hx hy (is_submonoid.mul_mem
(ring.subset_closure (or.inl rfl)) (ring.subset_closure (or.inr rfl)))
theorem is_integral_mul {x y : A}
(hx : is_integral R x) (hy : is_integral R y) : is_integral R (x * y) :=
(algebra_map R A).is_integral_mul hx hy
theorem ring_hom.is_integral_pow {x : S} :
Π (n : ℕ) (hx : f.is_integral_elem x), f.is_integral_elem (x ^ n)
| 0 hx := by simpa using f.is_integral_one
| (n + 1) hx := by simpa using f.is_integral_mul hx (ring_hom.is_integral_pow n hx)
theorem is_integral_pow {x : A} (n : ℕ) (hx : is_integral R x) : is_integral R (x ^ n) :=
(algebra_map R A).is_integral_pow n hx
variables (R A)
/-- The integral closure of R in an R-algebra A. -/
def integral_closure : subalgebra R A :=
{ carrier := { r | is_integral R r },
zero_mem' := is_integral_zero,
one_mem' := is_integral_one,
add_mem' := λ _ _, is_integral_add,
mul_mem' := λ _ _, is_integral_mul,
algebra_map_mem' := λ x, is_integral_algebra_map }
theorem mem_integral_closure_iff_mem_fg {r : A} :
r ∈ integral_closure R A ↔ ∃ M : subalgebra R A, (M : submodule R A).fg ∧ r ∈ M :=
⟨λ hr, ⟨algebra.adjoin R {r}, fg_adjoin_singleton_of_integral _ hr, algebra.subset_adjoin rfl⟩,
λ ⟨M, Hf, hrM⟩, is_integral_of_mem_of_fg M Hf _ hrM⟩
variables {R} {A}
/-- Mapping an integral closure along an `alg_equiv` gives the integral closure. -/
lemma integral_closure_map_alg_equiv (f : A ≃ₐ[R] B) :
(integral_closure R A).map (f : A →ₐ[R] B) = integral_closure R B :=
begin
ext y,
rw subalgebra.mem_map,
split,
{ rintros ⟨x, hx, rfl⟩,
exact is_integral_alg_hom f hx },
{ intro hy,
use [f.symm y, is_integral_alg_hom (f.symm : B →ₐ[R] A) hy],
simp }
end
lemma integral_closure.is_integral (x : integral_closure R A) : is_integral R x :=
let ⟨p, hpm, hpx⟩ := x.2 in ⟨p, hpm, subtype.eq $
by rwa [← aeval_def, subtype.val_eq_coe, ← subalgebra.val_apply, aeval_alg_hom_apply] at hpx⟩
lemma ring_hom.is_integral_of_is_integral_mul_unit (x y : S) (r : R) (hr : f r * y = 1)
(hx : f.is_integral_elem (x * y)) : f.is_integral_elem x :=
begin
obtain ⟨p, ⟨p_monic, hp⟩⟩ := hx,
refine ⟨scale_roots p r, ⟨(monic_scale_roots_iff r).2 p_monic, _⟩⟩,
convert scale_roots_eval₂_eq_zero f hp,
rw [mul_comm x y, ← mul_assoc, hr, one_mul],
end
theorem is_integral_of_is_integral_mul_unit {x y : A} {r : R} (hr : algebra_map R A r * y = 1)
(hx : is_integral R (x * y)) : is_integral R x :=
(algebra_map R A).is_integral_of_is_integral_mul_unit x y r hr hx
/-- Generalization of `is_integral_of_mem_closure` bootstrapped up from that lemma -/
lemma is_integral_of_mem_closure' (G : set A) (hG : ∀ x ∈ G, is_integral R x) :
∀ x ∈ (subring.closure G), is_integral R x :=
λ x hx, subring.closure_induction hx hG is_integral_zero is_integral_one
(λ _ _, is_integral_add) (λ _, is_integral_neg) (λ _ _, is_integral_mul)
lemma is_integral_of_mem_closure'' {S : Type*} [comm_ring S] {f : R →+* S} (G : set S)
(hG : ∀ x ∈ G, f.is_integral_elem x) : ∀ x ∈ (subring.closure G), f.is_integral_elem x :=
λ x hx, @is_integral_of_mem_closure' R S _ _ f.to_algebra G hG x hx
lemma algebra_map_injective (h : function.injective (algebra_map R A)) :
function.injective (algebra_map R (integral_closure R A)) :=
λ x y hxy, h $
show algebra_map (integral_closure R A) A (algebra_map R _ x) = _,
from congr_arg (algebra_map (integral_closure R A) A) hxy
end
section algebra
open algebra
variables {R A B S T : Type*}
variables [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring S] [comm_ring T]
variables [algebra A B] [algebra R B] (f : R →+* S) (g : S →+* T)
lemma is_integral_trans_aux (x : B) {p : polynomial A} (pmonic : monic p) (hp : aeval x p = 0) :
is_integral (adjoin R (↑(p.map $ algebra_map A B).frange : set B)) x :=
begin
generalize hS : (↑(p.map $ algebra_map A B).frange : set B) = S,
have coeffs_mem : ∀ i, (p.map $ algebra_map A B).coeff i ∈ adjoin R S,
{ intro i, by_cases hi : (p.map $ algebra_map A B).coeff i = 0,
{ rw hi, exact subalgebra.zero_mem _ },
rw ← hS, exact subset_adjoin (finsupp.mem_frange.2 ⟨hi, i, rfl⟩) },
obtain ⟨q, hq⟩ : ∃ q : polynomial (adjoin R S), q.map (algebra_map (adjoin R S) B) =
(p.map $ algebra_map A B),
{ rw ← set.mem_range, exact (polynomial.mem_map_range _).2 (λ i, ⟨⟨_, coeffs_mem i⟩, rfl⟩) },
use q,
split,
{ suffices h : (q.map (algebra_map (adjoin R S) B)).monic,
{ refine monic_of_injective _ h,
exact subtype.val_injective },
{ rw hq, exact monic_map _ pmonic } },
{ convert hp using 1,
replace hq := congr_arg (eval x) hq,
convert hq using 1; symmetry; apply eval_map },
end
variables [algebra R A] [is_scalar_tower R A B]
/-- If A is an R-algebra all of whose elements are integral over R,
and x is an element of an A-algebra that is integral over A, then x is integral over R.-/
lemma is_integral_trans (A_int : is_integral R A) (x : B) (hx : is_integral A x) :
is_integral R x :=
begin
rcases hx with ⟨p, pmonic, hp⟩,
let S : set B := ↑(p.map $ algebra_map A B).frange,
refine is_integral_of_mem_of_fg (adjoin R (S ∪ {x})) _ _ (subset_adjoin $ or.inr rfl),
refine fg_trans (fg_adjoin_of_finite (finset.finite_to_set _) (λ x hx, _)) _,
{ rw [finset.mem_coe, finsupp.mem_frange] at hx, rcases hx with ⟨_, i, rfl⟩,
show is_integral R ((p.map $ algebra_map A B).coeff i), rw coeff_map,
convert is_integral_alg_hom (is_scalar_tower.to_alg_hom R A B) (A_int _) },
{ apply fg_adjoin_singleton_of_integral,
exact is_integral_trans_aux _ pmonic hp }
end
/-- If A is an R-algebra all of whose elements are integral over R,
and B is an A-algebra all of whose elements are integral over A,
then all elements of B are integral over R.-/
lemma algebra.is_integral_trans (hA : is_integral R A) (hB : is_integral A B) : is_integral R B :=
λ x, is_integral_trans hA x (hB x)
lemma ring_hom.is_integral_trans (hf : f.is_integral) (hg : g.is_integral) :
(g.comp f).is_integral :=
@algebra.is_integral_trans R S T _ _ _ g.to_algebra (g.comp f).to_algebra f.to_algebra
(@is_scalar_tower.of_algebra_map_eq R S T _ _ _ f.to_algebra g.to_algebra (g.comp f).to_algebra
(ring_hom.comp_apply g f)) hf hg
lemma ring_hom.is_integral_of_surjective (hf : function.surjective f) : f.is_integral :=
λ x, (hf x).rec_on (λ y hy, (hy ▸ f.is_integral_map : f.is_integral_elem x))
lemma is_integral_of_surjective (h : function.surjective (algebra_map R A)) : is_integral R A :=
(algebra_map R A).is_integral_of_surjective h
/-- If `R → A → B` is an algebra tower with `A → B` injective,
then if the entire tower is an integral extension so is `R → A` -/
lemma is_integral_tower_bot_of_is_integral (H : function.injective (algebra_map A B))
{x : A} (h : is_integral R (algebra_map A B x)) : is_integral R x :=
begin
rcases h with ⟨p, ⟨hp, hp'⟩⟩,
refine ⟨p, ⟨hp, _⟩⟩,
rw [is_scalar_tower.algebra_map_eq R A B, ← eval₂_map,
eval₂_hom, ← ring_hom.map_zero (algebra_map A B)] at hp',
rw [eval₂_eq_eval_map],
exact H hp',
end
lemma ring_hom.is_integral_tower_bot_of_is_integral (hg : function.injective g)
(hfg : (g.comp f).is_integral) : f.is_integral :=
λ x, @is_integral_tower_bot_of_is_integral R S T _ _ _ g.to_algebra (g.comp f).to_algebra f.to_algebra
(@is_scalar_tower.of_algebra_map_eq R S T _ _ _ f.to_algebra g.to_algebra (g.comp f).to_algebra
(ring_hom.comp_apply g f)) hg x (hfg (g x))
lemma is_integral_tower_bot_of_is_integral_field {R A B : Type*} [comm_ring R] [field A]
[comm_ring B] [nontrivial B] [algebra R A] [algebra A B] [algebra R B] [is_scalar_tower R A B]
{x : A} (h : is_integral R (algebra_map A B x)) : is_integral R x :=
is_integral_tower_bot_of_is_integral (algebra_map A B).injective h
lemma ring_hom.is_integral_elem_of_is_integral_elem_comp {x : T}
(h : (g.comp f).is_integral_elem x) : g.is_integral_elem x :=
let ⟨p, ⟨hp, hp'⟩⟩ := h in ⟨p.map f, monic_map f hp, by rwa ← eval₂_map at hp'⟩
lemma ring_hom.is_integral_tower_top_of_is_integral (h : (g.comp f).is_integral) : g.is_integral :=
λ x, ring_hom.is_integral_elem_of_is_integral_elem_comp f g (h x)
/-- If `R → A → B` is an algebra tower,
then if the entire tower is an integral extension so is `A → B`. -/
lemma is_integral_tower_top_of_is_integral {x : B} (h : is_integral R x) : is_integral A x :=
begin
rcases h with ⟨p, ⟨hp, hp'⟩⟩,
refine ⟨p.map (algebra_map R A), ⟨monic_map (algebra_map R A) hp, _⟩⟩,
rw [is_scalar_tower.algebra_map_eq R A B, ← eval₂_map] at hp',
exact hp',
end
lemma ring_hom.is_integral_quotient_of_is_integral {I : ideal S} (hf : f.is_integral) :
(ideal.quotient_map I f le_rfl).is_integral :=
begin
rintros ⟨x⟩,
obtain ⟨p, ⟨p_monic, hpx⟩⟩ := hf x,
refine ⟨p.map (ideal.quotient.mk _), ⟨monic_map _ p_monic, _⟩⟩,
simpa only [hom_eval₂, eval₂_map] using congr_arg (ideal.quotient.mk I) hpx
end
lemma is_integral_quotient_of_is_integral {I : ideal A} (hRA : is_integral R A) :
is_integral (I.comap (algebra_map R A)).quotient I.quotient :=
(algebra_map R A).is_integral_quotient_of_is_integral hRA
lemma is_integral_quotient_map_iff {I : ideal S} :
(ideal.quotient_map I f le_rfl).is_integral ↔
((ideal.quotient.mk I).comp f : R →+* I.quotient).is_integral :=
begin
let g := ideal.quotient.mk (I.comap f),
have := ideal.quotient_map_comp_mk le_rfl,
refine ⟨λ h, _, λ h, ring_hom.is_integral_tower_top_of_is_integral g _ (this ▸ h)⟩,
refine this ▸ ring_hom.is_integral_trans g (ideal.quotient_map I f le_rfl) _ h,
exact ring_hom.is_integral_of_surjective g ideal.quotient.mk_surjective,
end
/-- If the integral extension `R → S` is injective, and `S` is a field, then `R` is also a field. -/
lemma is_field_of_is_integral_of_is_field {R S : Type*} [integral_domain R] [integral_domain S]
[algebra R S] (H : is_integral R S) (hRS : function.injective (algebra_map R S))
(hS : is_field S) : is_field R :=
begin
refine ⟨⟨0, 1, zero_ne_one⟩, mul_comm, λ a ha, _⟩,
-- Let `a_inv` be the inverse of `algebra_map R S a`,
-- then we need to show that `a_inv` is of the form `algebra_map R S b`.
obtain ⟨a_inv, ha_inv⟩ := hS.mul_inv_cancel (λ h, ha (hRS (trans h (ring_hom.map_zero _).symm))),
-- Let `p : polynomial R` be monic with root `a_inv`,
-- and `q` be `p` with coefficients reversed (so `q(a) = q'(a) * a + 1`).
-- We claim that `q(a) = 0`, so `-q'(a)` is the inverse of `a`.
obtain ⟨p, p_monic, hp⟩ := H a_inv,
use -∑ (i : ℕ) in finset.range p.nat_degree, (p.coeff i) * a ^ (p.nat_degree - i - 1),
-- `q(a) = 0`, because multiplying everything with `a_inv^n` gives `p(a_inv) = 0`.
-- TODO: this could be a lemma for `polynomial.reverse`.
have hq : ∑ (i : ℕ) in finset.range (p.nat_degree + 1), (p.coeff i) * a ^ (p.nat_degree - i) = 0,
{ apply (algebra_map R S).injective_iff.mp hRS,
have a_inv_ne_zero : a_inv ≠ 0 := right_ne_zero_of_mul (mt ha_inv.symm.trans one_ne_zero),
refine (mul_eq_zero.mp _).resolve_right (pow_ne_zero p.nat_degree a_inv_ne_zero),
rw [eval₂_eq_sum_range] at hp,
rw [ring_hom.map_sum, finset.sum_mul],
refine (finset.sum_congr rfl (λ i hi, _)).trans hp,
rw [ring_hom.map_mul, mul_assoc],
congr,
have : a_inv ^ p.nat_degree = a_inv ^ (p.nat_degree - i) * a_inv ^ i,
{ rw [← pow_add a_inv, nat.sub_add_cancel (nat.le_of_lt_succ (finset.mem_range.mp hi))] },
rw [ring_hom.map_pow, this, ← mul_assoc, ← mul_pow, ha_inv, one_pow, one_mul] },
-- Since `q(a) = 0` and `q(a) = q'(a) * a + 1`, we have `a * -q'(a) = 1`.
-- TODO: we could use a lemma for `polynomial.div_X` here.
rw [finset.sum_range_succ, p_monic.coeff_nat_degree, one_mul, nat.sub_self, pow_zero,
add_eq_zero_iff_eq_neg, eq_comm] at hq,
rw [mul_comm, ← neg_mul_eq_neg_mul, finset.sum_mul],
convert hq using 2,
refine finset.sum_congr rfl (λ i hi, _),
have : 1 ≤ p.nat_degree - i := nat.le_sub_left_of_add_le (finset.mem_range.mp hi),
rw [mul_assoc, ← pow_succ', nat.sub_add_cancel this]
end
end algebra
section
local attribute [instance] subset.comm_ring algebra.of_is_subring
theorem integral_closure_idem {R : Type*} {A : Type*} [comm_ring R] [comm_ring A] [algebra R A] :
integral_closure (integral_closure R A : set A) A = ⊥ :=
eq_bot_iff.2 $ λ x hx, algebra.mem_bot.2
⟨⟨x, @is_integral_trans _ _ _ _ _ _ _ _ (integral_closure R A).algebra
_ integral_closure.is_integral x hx⟩, rfl⟩
end
section integral_domain
variables {R S : Type*} [comm_ring R] [integral_domain S] [algebra R S]
instance : integral_domain (integral_closure R S) :=
{ exists_pair_ne := ⟨0, 1, mt subtype.ext_iff_val.mp zero_ne_one⟩,
eq_zero_or_eq_zero_of_mul_eq_zero := λ ⟨a, ha⟩ ⟨b, hb⟩ h,
or.imp subtype.ext_iff_val.mpr subtype.ext_iff_val.mpr (eq_zero_or_eq_zero_of_mul_eq_zero (subtype.ext_iff_val.mp h)),
..(integral_closure R S).comm_ring R S }
end integral_domain
|
bfbb6174afd0031b9f8fc737fdde725d44b8b334 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/limits/small_complete.lean | c61016db8abe78fdc6e8547042a92c6ba91c2bf0 | [
"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 | 2,169 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import category_theory.limits.shapes.products
import set_theory.cardinal.basic
/-!
# Any small complete category is a preorder
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
We show that any small category which has all (small) limits is a preorder: In particular, we show
that if a small category `C` in universe `u` has products of size `u`, then for any `X Y : C`
there is at most one morphism `X ⟶ Y`.
Note that in Lean, a preorder category is strictly one where the morphisms are in `Prop`, so
we instead show that the homsets are subsingleton.
## References
* https://ncatlab.org/nlab/show/complete+small+category#in_classical_logic
## Tags
small complete, preorder, Freyd
-/
namespace category_theory
open category limits
open_locale cardinal
universe u
variables {C : Type u} [small_category C] [has_products.{u} C]
/--
A small category with products is a thin category.
in Lean, a preorder category is one where the morphisms are in Prop, which is weaker than the usual
notion of a preorder/thin category which says that each homset is subsingleton; we show the latter
rather than providing a `preorder C` instance.
-/
@[priority 100] instance : quiver.is_thin C :=
λ X Y, ⟨λ r s,
begin
classical,
by_contra r_ne_s,
have z : (2 : cardinal) ≤ #(X ⟶ Y),
{ rw cardinal.two_le_iff,
exact ⟨_, _, r_ne_s⟩ },
let md := Σ (Z W : C), Z ⟶ W,
let α := #md,
apply not_le_of_lt (cardinal.cantor α),
let yp : C := ∏ (λ (f : md), Y),
transitivity (#(X ⟶ yp)),
{ apply le_trans (cardinal.power_le_power_right z),
rw cardinal.power_def,
apply le_of_eq,
rw cardinal.eq,
refine ⟨⟨pi.lift, λ f k, f ≫ pi.π _ k, _, _⟩⟩,
{ intros f,
ext k,
simp },
{ intros f,
ext ⟨j⟩,
simp } },
{ apply cardinal.mk_le_of_injective _,
{ intro f,
exact ⟨_, _, f⟩ },
{ rintro f g k,
cases k,
refl } },
end⟩
end category_theory
|
9a0334a2075714ffda23fa9c452a5762e12bace0 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/ring_theory/polynomial/rational_root.lean | 39cdfab524d69c5850f56ff549dea94bbd98b08a | [
"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 | 5,044 | 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 ring_theory.integrally_closed
import ring_theory.localization.num_denom
import ring_theory.polynomial.scale_roots
/-!
# Rational root theorem and integral root theorem
This file contains the rational root theorem and integral root theorem.
The rational root theorem for a unique factorization domain `A`
with localization `S`, states that the roots of `p : A[X]` in `A`'s
field of fractions are of the form `x / y` with `x y : A`, `x ∣ p.coeff 0` and
`y ∣ p.leading_coeff`.
The corollary is the integral root theorem `is_integer_of_is_root_of_monic`:
if `p` is monic, its roots must be integers.
Finally, we use this to show unique factorization domains are integrally closed.
## References
* https://en.wikipedia.org/wiki/Rational_root_theorem
-/
open_locale polynomial
section scale_roots
variables {A K R S : Type*} [comm_ring A] [field K] [comm_ring R] [comm_ring S]
variables {M : submonoid A} [algebra A S] [is_localization M S] [algebra A K] [is_fraction_ring A K]
open finsupp is_fraction_ring is_localization polynomial
lemma scale_roots_aeval_eq_zero_of_aeval_mk'_eq_zero {p : A[X]} {r : A} {s : M}
(hr : aeval (mk' S r s) p = 0) :
aeval (algebra_map A S r) (scale_roots p s) = 0 :=
begin
convert scale_roots_eval₂_eq_zero (algebra_map A S) hr,
rw [aeval_def, mk'_spec' _ r s]
end
variables [is_domain A]
lemma num_is_root_scale_roots_of_aeval_eq_zero
[unique_factorization_monoid A] {p : A[X]} {x : K} (hr : aeval x p = 0) :
is_root (scale_roots p (denom A x)) (num A x) :=
begin
apply is_root_of_eval₂_map_eq_zero (is_fraction_ring.injective A K),
refine scale_roots_aeval_eq_zero_of_aeval_mk'_eq_zero _,
rw mk'_num_denom,
exact hr
end
end scale_roots
section rational_root_theorem
variables {A K : Type*} [comm_ring A] [is_domain A] [unique_factorization_monoid A] [field K]
variables [algebra A K] [is_fraction_ring A K]
open is_fraction_ring is_localization polynomial unique_factorization_monoid
/-- Rational root theorem part 1:
if `r : f.codomain` is a root of a polynomial over the ufd `A`,
then the numerator of `r` divides the constant coefficient -/
theorem num_dvd_of_is_root {p : A[X]} {r : K} (hr : aeval r p = 0) :
num A r ∣ p.coeff 0 :=
begin
suffices : num A r ∣ (scale_roots p (denom A r)).coeff 0,
{ simp only [coeff_scale_roots, tsub_zero] at this,
haveI := classical.prop_decidable,
by_cases hr : num A r = 0,
{ obtain ⟨u, hu⟩ := (is_unit_denom_of_num_eq_zero hr).pow p.nat_degree,
rw ←hu at this,
exact units.dvd_mul_right.mp this },
{ refine dvd_of_dvd_mul_left_of_no_prime_factors hr _ this,
intros q dvd_num dvd_denom_pow hq,
apply hq.not_unit,
exact num_denom_reduced A r dvd_num (hq.dvd_of_dvd_pow dvd_denom_pow) } },
convert dvd_term_of_is_root_of_dvd_terms 0 (num_is_root_scale_roots_of_aeval_eq_zero hr) _,
{ rw [pow_zero, mul_one] },
intros j hj,
apply dvd_mul_of_dvd_right,
convert pow_dvd_pow (num A r) (nat.succ_le_of_lt (bot_lt_iff_ne_bot.mpr hj)),
exact (pow_one _).symm
end
/-- Rational root theorem part 2:
if `r : f.codomain` is a root of a polynomial over the ufd `A`,
then the denominator of `r` divides the leading coefficient -/
theorem denom_dvd_of_is_root {p : A[X]} {r : K} (hr : aeval r p = 0) :
(denom A r : A) ∣ p.leading_coeff :=
begin
suffices : (denom A r : A) ∣ p.leading_coeff * num A r ^ p.nat_degree,
{ refine dvd_of_dvd_mul_left_of_no_prime_factors
(mem_non_zero_divisors_iff_ne_zero.mp (denom A r).2) _ this,
intros q dvd_denom dvd_num_pow hq,
apply hq.not_unit,
exact num_denom_reduced A r (hq.dvd_of_dvd_pow dvd_num_pow) dvd_denom },
rw ←coeff_scale_roots_nat_degree,
apply dvd_term_of_is_root_of_dvd_terms _ (num_is_root_scale_roots_of_aeval_eq_zero hr),
intros j hj,
by_cases h : j < p.nat_degree,
{ rw coeff_scale_roots,
refine (dvd_mul_of_dvd_right _ _).mul_right _,
convert pow_dvd_pow _ (nat.succ_le_iff.mpr (lt_tsub_iff_left.mpr _)),
{ exact (pow_one _).symm },
simpa using h },
rw [←nat_degree_scale_roots p (denom A r)] at *,
rw [coeff_eq_zero_of_nat_degree_lt (lt_of_le_of_ne (le_of_not_gt h) hj.symm), zero_mul],
exact dvd_zero _
end
/-- Integral root theorem:
if `r : f.codomain` is a root of a monic polynomial over the ufd `A`,
then `r` is an integer -/
theorem is_integer_of_is_root_of_monic {p : A[X]} (hp : monic p) {r : K}
(hr : aeval r p = 0) : is_integer A r :=
is_integer_of_is_unit_denom (is_unit_of_dvd_one _ (hp ▸ denom_dvd_of_is_root hr))
namespace unique_factorization_monoid
lemma integer_of_integral {x : K} :
is_integral A x → is_integer A x :=
λ ⟨p, hp, hx⟩, is_integer_of_is_root_of_monic hp hx
@[priority 100] -- See library note [lower instance priority]
instance : is_integrally_closed A :=
⟨λ x, integer_of_integral⟩
end unique_factorization_monoid
end rational_root_theorem
|
f2a2bb3127fc4c1319d2de3a971cb24ccfd905bb | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /hott/homotopy/smash.hlean | 513942f03db7a44106adc9cfc155ea80967940ce | [
"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 | 3,352 | hlean | /-
Copyright (c) 2016 Jakob von Raumer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jakob von Raumer
The Smash Product of Types
-/
import hit.pushout .wedge .cofiber .susp .sphere
open eq pushout prod pointed is_trunc
definition product_of_wedge [constructor] (A B : Type*) : pwedge A B →* A ×* B :=
begin
fconstructor,
intro x, induction x with [a, b], exact (a, point B), exact (point A, b),
do 2 reflexivity
end
definition psmash (A B : Type*) := pcofiber (product_of_wedge A B)
open sphere susp unit
namespace smash
protected definition prec {X Y : Type*} {P : psmash X Y → Type}
(pxy : Π x y, P (inr (x, y))) (ps : P (inl ⋆))
(px : Π x, pathover P ps (glue (inl x)) (pxy x (point Y)))
(py : Π y, pathover P ps (glue (inr y)) (pxy (point X) y))
(pg : pathover (λ x, pathover P ps (glue x) (@prod.rec X Y (λ x, P (inr x)) pxy
(pushout.elim (λ a, (a, Point Y)) (pair (Point X)) (λ x, idp) x)))
(px (Point X)) (glue ⋆) (py (Point Y))) : Π s, P s :=
begin
intro s, induction s, induction x, exact ps,
induction x with [x, y], exact pxy x y,
induction x with [x, y, u], exact px x, exact py y,
induction u, exact pg,
end
protected definition prec_on {X Y : Type*} {P : psmash X Y → Type} (s : psmash X Y)
(pxy : Π x y, P (inr (x, y))) (ps : P (inl ⋆))
(px : Π x, pathover P ps (glue (inl x)) (pxy x (point Y)))
(py : Π y, pathover P ps (glue (inr y)) (pxy (point X) y))
(pg : pathover (λ x, pathover P ps (glue x) (@prod.rec X Y (λ x, P (inr x)) pxy
(pushout.elim (λ a, (a, Point Y)) (pair (Point X)) (λ x, idp) x)))
(px (Point X)) (glue ⋆) (py (Point Y))) : P s :=
smash.prec pxy ps px py pg s
/- definition smash_bool (X : Type*) : psmash X pbool ≃* X :=
begin
fconstructor,
{ fconstructor,
{ intro x, fapply cofiber.pelim_on x, clear x, exact point X, intro p,
cases p with [x', b], cases b with [x, x'], exact point X, exact x',
clear x, intro w, induction w with [y, b], reflexivity,
cases b, reflexivity, reflexivity, esimp,
apply eq_pathover, refine !ap_constant ⬝ph _, cases x, esimp, apply hdeg_square,
apply inverse, apply concat, apply ap_compose (λ a, prod.cases_on a _),
apply concat, apply ap _ !elim_glue, reflexivity },
reflexivity },
{ fapply is_equiv.adjointify,
{ intro x, apply inr, exact pair x bool.tt },
{ intro x, reflexivity },
{ intro s, esimp, induction s,
{ cases x, apply (glue (inr bool.tt))⁻¹ },
{ cases x with [x, b], cases b,
apply inverse, apply concat, apply (glue (inl x))⁻¹, apply (glue (inr bool.tt)),
reflexivity },
{ esimp, apply eq_pathover, induction x,
esimp, apply hinverse, krewrite ap_id, apply move_bot_of_left,
krewrite con.right_inv,
refine _ ⬝hp !(ap_compose (λ a, inr (pair a _)))⁻¹,
apply transpose, apply square_of_eq_bot, rewrite [con_idp, con.left_inv],
apply inverse, apply concat, apply ap (ap _),
} } }
definition susp_equiv_circle_smash (X : Type*) : psusp X ≃* psmash (psphere 1) X :=
begin
fconstructor,
{ fconstructor, intro x, induction x, },
end-/
end smash
|
d65d0834e63b133cbbe596cb42764c428c64eeeb | 3af272061d36e7f3f0521cceaa3a847ed4e03af9 | /src/modular_group.lean | 69d53227b861f3e6a141823d8bb4c75b5a5c4474 | [] | no_license | semorrison/kbb | fdab0929d21dca880d835081814225a95f946187 | 229bd06e840bc7a7438b8fee6802a4f8024419e3 | refs/heads/master | 1,585,351,834,355 | 1,539,848,241,000 | 1,539,848,241,000 | 147,323,315 | 2 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 6,844 | lean | import tactic.ring
import tactic.tidy
import group_theory.group_action
import .matrix_groups
run_cmd mk_simp_attr `SL2Z
@[tidy] meta def tidy_ring := `[ring]
@[elab_as_eliminator]
def fin2.rec_on {C : fin 2 → Sort*} : ∀ (n : fin 2), C 0 → C 1 → C n
| ⟨0, _⟩ C0 _ := C0
| ⟨1, _⟩ _ C1 := C1
| ⟨n+2, H⟩ _ _ := false.elim $ by cases H with H H; cases H with H H; cases H
@[elab_as_eliminator]
theorem fin2.induction_on {C : fin 2 → Prop} (n : fin 2) (H0 : C 0) (H1 : C 1) : C n :=
fin2.rec_on n H0 H1
@[derive decidable_eq]
structure integral_matrices_with_determinant (m : ℤ) :=
(a b c d : ℤ)
(det : a * d - b * c = m)
@[extensionality]
theorem integral_matrices_with_determinant.ext (m : ℤ) :
∀ (A B : integral_matrices_with_determinant m)
(H1 : A.a = B.a) (H2 : A.b = B.b) (H3 : A.c = B.c) (H4 : A.d = B.d),
A = B
| ⟨_, _, _, _, _⟩ ⟨_, _, _, _, _⟩ rfl rfl rfl rfl := rfl
@[reducible]
def SL2Z := integral_matrices_with_determinant 1
instance : group SL2Z :=
{ mul := λ A B, ⟨A.a * B.a + A.b * B.c,
A.a * B.b + A.b * B.d,
A.c * B.a + A.d * B.c,
A.c * B.b + A.d * B.d,
calc (A.a * B.a + A.b * B.c) * (A.c * B.b + A.d * B.d) - (A.a * B.b + A.b * B.d) * (A.c * B.a + A.d * B.c)
= (A.a * A.d - A.b * A.c) * (B.a * B.d - B.b * B.c) : by ring
... = 1 : by rw [A.det, B.det, mul_one]⟩,
mul_assoc := λ A B C, by cases A; cases B; cases C; ext; dsimp; ring,
one := ⟨1, 0, 0, 1, rfl⟩,
one_mul := λ A, by cases A; ext; change _ + _ = _; simp,
mul_one := λ A, by cases A; ext; change _ + _ = _; simp,
inv := λ A, ⟨A.d, -A.b, -A.c, A.a, by simpa [mul_comm] using A.det⟩,
mul_left_inv := λ A, by cases A; ext; change _ + _ = _; simp at A_det; simp [mul_comm, A_det]; refl }
@[simp, SL2Z] lemma SL2Z_mul_a (A B : SL2Z) : (A * B).a = A.a * B.a + A.b * B.c := rfl
@[simp, SL2Z] lemma SL2Z_mul_b (A B : SL2Z) : (A * B).b = A.a * B.b + A.b * B.d := rfl
@[simp, SL2Z] lemma SL2Z_mul_c (A B : SL2Z) : (A * B).c = A.c * B.a + A.d * B.c := rfl
@[simp, SL2Z] lemma SL2Z_mul_d (A B : SL2Z) : (A * B).d = A.c * B.b + A.d * B.d := rfl
@[simp, SL2Z] lemma SL2Z_one_a : (1 : SL2Z).a = 1 := rfl
@[simp, SL2Z] lemma SL2Z_one_b : (1 : SL2Z).b = 0 := rfl
@[simp, SL2Z] lemma SL2Z_one_c : (1 : SL2Z).c = 0 := rfl
@[simp, SL2Z] lemma SL2Z_one_d : (1 : SL2Z).d = 1 := rfl
@[simp, SL2Z] lemma SL2Z_inv_a (A : SL2Z) : (A⁻¹).a = A.d := rfl
@[simp, SL2Z] lemma SL2Z_inv_b (A : SL2Z) : (A⁻¹).b = -A.b := rfl
@[simp, SL2Z] lemma SL2Z_inv_c (A : SL2Z) : (A⁻¹).c = -A.c := rfl
@[simp, SL2Z] lemma SL2Z_inv_d (A : SL2Z) : (A⁻¹).d = A.a := rfl
def SL2Z_M (m : ℤ) : SL2Z → integral_matrices_with_determinant m → integral_matrices_with_determinant m :=
λ X Y, { a := X.a * Y.a + X.b * Y.c,
b := X.a * Y.b + X.b * Y.d,
c := X.c * Y.a + X.d * Y.c,
d := X.c * Y.b + X.d * Y.d,
det := begin
conv { to_rhs, rw ← one_mul m, congr, rw ← X.det, skip, rw ← Y.det },
ring
end }
instance (m : ℤ) : is_group_action (SL2Z_M m) :=
{ mul := λ ⟨_, _, _, _, _⟩ ⟨_, _, _, _, _⟩ ⟨_, _, _, _, _⟩,
by ext; simp [SL2Z_M, add_mul, mul_add, mul_assoc],
one := λ ⟨_, _, _, _, _⟩, by ext; simp [SL2Z_M], }
section
variables (m : ℤ) (A : SL2Z) (M : integral_matrices_with_determinant m)
@[simp, SL2Z] lemma SL2Z_M_a : (SL2Z_M m A M).a = A.a * M.a + A.b * M.c := rfl
@[simp, SL2Z] lemma SL2Z_M_b : (SL2Z_M m A M).b = A.a * M.b + A.b * M.d := rfl
@[simp, SL2Z] lemma SL2Z_M_c : (SL2Z_M m A M).c = A.c * M.a + A.d * M.c := rfl
@[simp, SL2Z] lemma SL2Z_M_d : (SL2Z_M m A M).d = A.c * M.b + A.d * M.d := rfl
end
def SL2Z_SL_2_Z : SL2Z ≃ SL 2 ℤ :=
{ to_fun := λ A, ⟨units.mk
(λ i j, fin2.rec_on i (fin2.rec_on j A.a A.b) (fin2.rec_on j A.c A.d))
(λ i j, fin2.rec_on i (fin2.rec_on j A.d (-A.b)) (fin2.rec_on j (-A.c) A.a))
(matrix.ext' $ λ i j, fin2.induction_on i
(fin2.induction_on j
(show A.a * A.d + (A.b * (-A.c) + 0) = 1, by rw [add_zero, mul_neg_eq_neg_mul_symm, ← sub_eq_add_neg, A.det])
(show A.a * (-A.b) + (A.b * A.a + 0) = 0, by rw [add_zero, mul_comm, ← add_mul, neg_add_self, zero_mul]))
(fin2.induction_on j
(show A.c * A.d + (A.d * (-A.c) + 0) = 0, by rw [add_zero, mul_comm, ← mul_add, add_neg_self, mul_zero])
(show A.c * (-A.b) + (A.d * A.a + 0) = 1, by rw [add_zero, mul_comm, add_comm, mul_comm, ← neg_mul_eq_neg_mul, ← sub_eq_add_neg, A.det])))
(matrix.ext' $ λ i j, fin2.induction_on i
(fin2.induction_on j
(show A.d * A.a + (-A.b * A.c + 0) = 1, by rw [add_zero, ← neg_mul_eq_neg_mul, mul_comm, ← sub_eq_add_neg, A.det])
(show A.d * A.b + (-A.b * A.d + 0) = 0, by rw [add_zero, mul_comm, ← add_mul, add_neg_self, zero_mul]))
(fin2.induction_on j
(show -A.c * A.a + (A.a * A.c + 0) = 0, by rw [add_zero, mul_comm, ← mul_add, neg_add_self, mul_zero])
(show -A.c * A.b + (A.a * A.d + 0) = 1, by rw [add_zero, ← neg_mul_eq_neg_mul, mul_comm, add_comm, ← sub_eq_add_neg, A.det]))),
is_subgroup.mem_trivial.2 $ units.ext $
show ((1:ℤ) * (A.a * (A.d * 1))) + (((-1:ℤ) * (A.c * (A.b * 1))) + (0:ℤ)) = 1,
by rw [one_mul, mul_one, mul_one, add_zero, neg_one_mul, mul_comm A.c]; from A.det⟩,
inv_fun := λ M, ⟨M.1.1 0 0, M.1.1 0 1, M.1.1 1 0, M.1.1 1 1,
have H : ((1:ℤ) * (M.1.1 0 0 * (M.1.1 1 1 * 1))) + (((-1:ℤ) * (M.1.1 1 0 * (M.1.1 0 1 * 1))) + (0:ℤ)) = 1,
from units.ext_iff.1 (is_subgroup.mem_trivial.1 M.2),
by rwa [one_mul, mul_one, mul_one, add_zero, neg_one_mul, mul_comm (M.1.1 1 0)] at H⟩,
left_inv := λ A, integral_matrices_with_determinant.ext 1 _ _
rfl rfl rfl rfl,
right_inv := λ M, subtype.eq $ units.ext $ matrix.ext' $ λ i j,
fin2.induction_on i
(fin2.induction_on j rfl rfl)
(fin2.induction_on j rfl rfl) }
namespace integral_matrices_with_determinant
variables (m : ℤ) (A B : integral_matrices_with_determinant m)
instance : has_neg (integral_matrices_with_determinant m) :=
⟨λ A, ⟨-A.a, -A.b, -A.c, -A.d, by rw [neg_mul_neg, neg_mul_neg, A.det]⟩⟩
@[simp, SL2Z] lemma neg_a : (-A).a = -A.a := rfl
@[simp, SL2Z] lemma neg_b : (-A).b = -A.b := rfl
@[simp, SL2Z] lemma neg_c : (-A).c = -A.c := rfl
@[simp, SL2Z] lemma neg_d : (-A).d = -A.d := rfl
@[simp, SL2Z] protected lemma neg_neg : -(-A) = A := by ext; simp
end integral_matrices_with_determinant
namespace SL2Z
variables (A B : SL2Z)
@[simp, SL2Z] protected lemma neg_one_mul : -1 * A = -A := by ext; simp
@[simp, SL2Z] protected lemma neg_mul_neg : -A * -B = A * B := by ext; simp
@[simp, SL2Z] protected lemma neg_mul : -(A * B) = -A * B := by ext; simp
@[simp, SL2Z] protected lemma neg_neg : -(-A) = A := by ext; simp
end SL2Z |
c5e7018661298cc79c3debbf7a28dddc01ca2add | 6dc0c8ce7a76229dd81e73ed4474f15f88a9e294 | /src/Lean/Data/Lsp/Extra.lean | 0e5fe053d32e87728c7c2037f6600683b30a84f7 | [
"Apache-2.0"
] | permissive | williamdemeo/lean4 | 72161c58fe65c3ad955d6a3050bb7d37c04c0d54 | 6d00fcf1d6d873e195f9220c668ef9c58e9c4a35 | refs/heads/master | 1,678,305,356,877 | 1,614,708,995,000 | 1,614,708,995,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,275 | lean | /-
Copyright (c) 2020 Marc Huisinga. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Marc Huisinga
-/
import Lean.Data.Json
import Lean.Data.JsonRpc
import Lean.Data.Lsp.Basic
/-!
This file contains Lean-specific extensions to LSP.
The following additional packets are supported:
- "textDocument/waitForDiagnostics": Yields a response when all the diagnostics for a version of the document
greater or equal to the specified one have been emitted. If the request specifies a version above the most
recently processed one, the server will delay the response until it does receive the specified version.
Exists for synchronization purposes, e.g. during testing or when external tools might want to use our LSP server.
-/
namespace Lean.Lsp
open Json
structure WaitForDiagnosticsParams where
uri : DocumentUri
version : Nat
deriving ToJson, FromJson
structure WaitForDiagnostics
instance : FromJson WaitForDiagnostics :=
⟨fun j => WaitForDiagnostics.mk⟩
instance : ToJson WaitForDiagnostics :=
⟨fun o => mkObj []⟩
structure PlainGoalParams extends TextDocumentPositionParams
deriving FromJson, ToJson
structure PlainGoal where
rendered : String
deriving FromJson, ToJson
end Lean.Lsp
|
403bf6c47bfd189ecda41e8e65f0a98608c7d691 | ce89339993655da64b6ccb555c837ce6c10f9ef4 | /zeptometer/topprover/39.lean | a186cb2411fefae20d079eb504a3036b141e208c | [] | no_license | zeptometer/LearnLean | ef32dc36a22119f18d843f548d0bb42f907bff5d | bb84d5dbe521127ba134d4dbf9559b294a80b9f7 | refs/heads/master | 1,625,710,824,322 | 1,601,382,570,000 | 1,601,382,570,000 | 195,228,870 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 723 | lean | lemma pohe : ∀P, ¬(P ↔ ¬P) := begin
intro P,
intro w,
have np : ¬ P, {
intro hp,
exact w.1 hp hp
},
exact np (w.2 np)
end
theorem boolean_hole :
(∀ P Q R, (P ↔ Q) ∨ (Q ↔ R) ∨ (R ↔ P)) → ∀ P, P ∨ ¬ P := begin
intros asm P,
cases asm (P ∨ ¬P) ¬(P ∨ ¬P) ¬¬(P ∨ ¬P), {
exfalso,
apply pohe,
assumption,
}, cases h, {
exfalso,
apply pohe,
assumption
}, {
apply h.1,
intro n_pnp,
have np : ¬ P, {
intro p,
apply n_pnp,
left,
assumption
},
apply n_pnp,
right,
assumption
}
end
|
dfc0a5c8a8821b53d347ce929fc4aa35a9e37405 | ea11767c9c6a467c4b7710ec6f371c95cfc023fd | /src/monoidal_categories/internal_objects/free_modules.lean | f4bfa0798b8901956882fd6b86831af162ea6d16 | [] | no_license | RitaAhmadi/lean-monoidal-categories | 68a23f513e902038e44681336b87f659bbc281e0 | 81f43e1e0d623a96695aa8938951d7422d6d7ba6 | refs/heads/master | 1,651,458,686,519 | 1,529,824,613,000 | 1,529,824,613,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,939 | lean | -- Copyright (c) 2017 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Stephen Morgan, Scott Morrison
import .monoids
open categories
open categories.functor
open categories.monoidal_category
namespace categories.internal_objects
universes u v
variables {C : Type u} [𝒞 : monoidal_category.{u v} C]
include 𝒞
def fmod (A : C) [MonoidObject A] := C
open SemigroupObject
open MonoidObject
definition CategoryOfFreeModules (A : C) [MonoidObject A] : category (fmod A) :=
{ Hom := λ X Y : C, X ⟶ (A ⊗ Y),
identity := λ X : C, (inverse_left_unitor X) ≫ ((ι A) ⊗ (𝟙 X)),
compose := λ _ _ Z f g, f ≫ ((𝟙 A) ⊗ g) ≫ (inverse_associator A A Z) ≫ ((μ A) ⊗ (𝟙 Z)),
left_identity := begin
-- PROJECT dealing with associativity here is quite tedious.
-- PROJECT this is a great example problem for clever automation.
-- A human quickly sees that we need to combine A.unit and A.multiplication to make them cancel,
-- and then performs the necessary rewrites to get there.
intros,
conv {
to_lhs,
rewrite category.associativity,
congr, skip,
rewrite ← category.associativity,
rewrite ← interchange_identities,
rewrite category.associativity,
congr, skip,
rewrite ← category.associativity,
rewrite ← tensor_identities,
rewrite inverse_associator_naturality_0,
rewrite category.associativity,
congr, skip,
rewrite interchange_left_identity,
congr,
rewrite [MonoidObject.left_identity] {tactic.rewrite_cfg . md := semireducible},
},
simp,
conv {
to_lhs,
rewrite ← category.associativity,
congr,
rewrite [← 𝒞.left_unitor_transformation.inverse.naturality] {tactic.rewrite_cfg . md := semireducible},
},
simp,
dunfold IdentityFunctor, dsimp,
-- PROJECT this needs Proposition 2.2.4 of Etingof's "Tensor Categories" to finish; and that seems awkward to prove in our setup!
exact sorry
end,
right_identity := sorry,
associativity := sorry
}
-- PROJECT show that after idempotent completing the category of free modules we get the category of modules??
-- PROJECT bimodules
-- PROJECT commutative algebras; modules give bimodules
end categories.internal_objects |
1c75fd281634929c263b012c5462eb18807c812e | 41ebf3cb010344adfa84907b3304db00e02db0a6 | /uexp/src/uexp/examples.lean | 243930b876925b16822e55d438c79d2d79af888b | [
"BSD-2-Clause"
] | permissive | ReinierKoops/Cosette | e061b2ba58b26f4eddf4cd052dcf7abd16dfe8fb | eb8dadd06ee05fe7b6b99de431dd7c4faef5cb29 | refs/heads/master | 1,686,483,953,198 | 1,624,293,498,000 | 1,624,293,498,000 | 378,997,885 | 0 | 0 | BSD-2-Clause | 1,624,293,485,000 | 1,624,293,484,000 | null | UTF-8 | Lean | false | false | 1,134 | lean | import .u_semiring
import .sql
import .tactics
-- set_option trace.simp_lemmas.invalid true
set_option trace.simplify true
infix `≃`:50 := usr.ueq
-- these two lemmas are just to try the new encoding
-- this one works, hoory!
lemma lem_jared :
forall (v : Type) (e1 e2 e3 : usr),
(e1 + e2) * e3 = (e2 * e3) + (e3 * e1) :=
begin
intros,
simp,
end
lemma eq_mixed_congruence :
forall {s: Schema} (t₁ t₂: Tuple s) (R: Tuple s → usr),
(R t₁) * (t₁ ≃ t₂) = (t₁ ≃ t₂) * (R t₂) :=
begin
intros,
simp,
end
-- this one breaks something
lemma eq_sigma_subst:
forall {s: Schema} (R: Tuple s → usr) (t : Tuple s),
(∑ t₁ , (t₁ ≃ t) * (R t₁)) = (R t) :=
begin
intros,
simp,
end
lemma commutativeSelect:
forall Γ s a slct0 slct1,
denoteSQL ((SELECT * FROM1 (SELECT * FROM1 a WHERE slct1) WHERE slct0): SQL Γ s) =
denoteSQL ((SELECT * FROM1 (SELECT * FROM1 a WHERE slct0) WHERE slct1): SQL Γ s) :=
begin
intros,
unfold_all_denotations,
funext, funext,
-- simp should work here, but it seems require ac refl now
ac_refl,
end |
b4f1adeca59856ae69924b9aa7be9543b0dd84a8 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /tests/lean/inductionErrors.lean | 608c8b9681e42037898b976e27ae26b8d9728f1b | [
"Apache-2.0"
] | permissive | WojciechKarpiel/lean4 | 7f89706b8e3c1f942b83a2c91a3a00b05da0e65b | f6e1314fa08293dea66a329e05b6c196a0189163 | refs/heads/master | 1,686,633,402,214 | 1,625,821,189,000 | 1,625,821,258,000 | 384,640,886 | 0 | 0 | Apache-2.0 | 1,625,903,617,000 | 1,625,903,026,000 | null | UTF-8 | Lean | false | false | 2,474 | lean | universe u
axiom elimEx (motive : Nat → Nat → Sort u) (x y : Nat)
(diag : (a : Nat) → motive a a)
(upper : (delta a : Nat) → motive a (a + delta.succ))
(lower : (delta a : Nat) → motive (a + delta.succ) a)
: motive y x
theorem ex1 (p q : Nat) : p ≤ q ∨ p > q := by
cases p, q using elimEx with
| lower d => apply Or.inl -- Error
| upper d => apply Or.inr -- Error
| diag => apply Or.inl; apply Nat.leRefl
theorem ex2 (p q : Nat) : p ≤ q ∨ p > q := by
cases p, q using elimEx2 with -- Error
| lower d => apply Or.inl
| upper d => apply Or.inr
| diag => apply Or.inl; apply Nat.leRefl
theorem ex3 (p q : Nat) : p ≤ q ∨ p > q := by
cases p /- Error -/ using elimEx with
| lower d => apply Or.inl
| upper d => apply Or.inr
| diag => apply Or.inl; apply Nat.leRefl
theorem ex4 (p q : Nat) : p ≤ q ∨ p > q := by
cases p using Nat.add with -- Error
| lower d => apply Or.inl
| upper d => apply Or.inr
| diag => apply Or.inl; apply Nat.leRefl
theorem ex5 (x : Nat) : 0 + x = x := by
match x with
| 0 => done -- Error
| y+1 => done -- Error
theorem ex5b (x : Nat) : 0 + x = x := by
cases x with
| zero => done -- Error
| succ y => done -- Error
inductive Vec : Nat → Type
| nil : Vec 0
| cons : Bool → {n : Nat} → Vec n → Vec (n+1)
theorem ex6 (x : Vec 0) : x = Vec.nil := by
cases x using Vec.casesOn with
| nil => rfl
| cons => done -- Error
theorem ex7 (x : Vec 0) : x = Vec.nil := by
cases x with -- Error: TODO: improve error location
| nil => rfl
| cons => done
theorem ex8 (p q : Nat) : p ≤ q ∨ p > q := by
cases p, q using elimEx with
| lower d => apply Or.inl; admit
| upper2 /- Error -/ d => apply Or.inr
| diag => apply Or.inl; apply Nat.leRefl
theorem ex9 (p q : Nat) : p ≤ q ∨ p > q := by
cases p, q using elimEx with
| lower d => apply Or.inl; admit
| _ => apply Or.inr; admit
| diag => apply Or.inl; apply Nat.leRefl
theorem ex10 (p q : Nat) : p ≤ q ∨ p > q := by
cases p, q using elimEx with
| lower d => apply Or.inl; admit
| upper d => apply Or.inr; admit
| diag => apply Or.inl; apply Nat.leRefl
| _ /- error unused -/ => admit
theorem ex11 (p q : Nat) : p ≤ q ∨ p > q := by
cases p, q using elimEx with
| lower d => apply Or.inl; admit
| upper d => apply Or.inr; admit
| lower d /- error unused -/ => apply Or.inl; admit
| diag => apply Or.inl; apply Nat.leRefl
|
a741f16cfe0bb5bcfffed989a231c24a2f7b79c7 | 0845ae2ca02071debcfd4ac24be871236c01784f | /library/init/lean/compiler/default.lean | 5b1732fe7f91022b76917ba59daa6ec4cf452d44 | [
"Apache-2.0"
] | permissive | GaloisInc/lean4 | 74c267eb0e900bfaa23df8de86039483ecbd60b7 | 228ddd5fdcd98dd4e9c009f425284e86917938aa | refs/heads/master | 1,643,131,356,301 | 1,562,715,572,000 | 1,562,715,572,000 | 192,390,898 | 0 | 0 | null | 1,560,792,750,000 | 1,560,792,749,000 | null | UTF-8 | Lean | false | false | 438 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.lean.compiler.inlineattrs
import init.lean.compiler.specialize
import init.lean.compiler.constfolding
import init.lean.compiler.closedtermcache
import init.lean.compiler.externattr
import init.lean.compiler.implementedbyattr
import init.lean.compiler.ir
|
68d961fc9ba7ed846b986a872fa247313cfe58ef | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/equiv/nat.lean | 06e7d76329f04b0ce45ccc4246443f6df9a4e479 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,917 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
Additional facts about equiv and encodable using the
pairing function on nat.
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.nat.pairing
import Mathlib.data.pnat.basic
import Mathlib.PostPort
universes u_1 u_2
namespace Mathlib
namespace equiv
/--
An equivalence between `ℕ × ℕ` and `ℕ`, using the `mkpair` and `unpair` functions in
`data.nat.pairing`.
-/
@[simp] def nat_prod_nat_equiv_nat : ℕ × ℕ ≃ ℕ :=
mk (fun (p : ℕ × ℕ) => nat.mkpair (prod.fst p) (prod.snd p)) nat.unpair sorry nat.mkpair_unpair
/--
An equivalence between `bool × ℕ` and `ℕ`, by mapping `(tt, x)` to `2 * x + 1` and `(ff, x)` to
`2 * x`.
-/
@[simp] def bool_prod_nat_equiv_nat : Bool × ℕ ≃ ℕ :=
mk (fun (_x : Bool × ℕ) => sorry) nat.bodd_div2 sorry sorry
/--
An equivalence between `ℕ ⊕ ℕ` and `ℕ`, by mapping `(sum.inl x)` to `2 * x` and `(sum.inr x)` to
`2 * x + 1`.
-/
@[simp] def nat_sum_nat_equiv_nat : ℕ ⊕ ℕ ≃ ℕ :=
equiv.trans (equiv.symm (bool_prod_equiv_sum ℕ)) bool_prod_nat_equiv_nat
/--
An equivalence between `ℤ` and `ℕ`, through `ℤ ≃ ℕ ⊕ ℕ` and `ℕ ⊕ ℕ ≃ ℕ`.
-/
def int_equiv_nat : ℤ ≃ ℕ :=
equiv.trans int_equiv_nat_sum_nat nat_sum_nat_equiv_nat
/--
An equivalence between `α × α` and `α`, given that there is an equivalence between `α` and `ℕ`.
-/
def prod_equiv_of_equiv_nat {α : Type (max u_1 u_2)} (e : α ≃ ℕ) : α × α ≃ α :=
equiv.trans (equiv.trans (prod_congr e e) nat_prod_nat_equiv_nat) (equiv.symm e)
/--
An equivalence between `ℕ+` and `ℕ`, by mapping `x` in `ℕ+` to `x - 1` in `ℕ`.
-/
def pnat_equiv_nat : ℕ+ ≃ ℕ :=
mk (fun (n : ℕ+) => Nat.pred (subtype.val n)) nat.succ_pnat sorry sorry
|
1e7f5100cc3ca87e8c85e0325e597c7fb6b9b76e | 7a0854479980a89e813e3c93d127f09a8e2c3a7e | /src/subgroup/theorems.lean | 6bafbc14c82965ae0ec5be6d3556a54ba813e4d9 | [] | no_license | cfbolz/group-theory-game | 020e382df58bf9a510dce38304f27400e4ef0b80 | b5282ce72a2a22e9ba1b48cee432ff3d77496040 | refs/heads/master | 1,668,643,052,237 | 1,594,820,769,000 | 1,594,820,769,000 | 279,899,736 | 0 | 0 | null | 1,594,825,474,000 | 1,594,825,473,000 | null | UTF-8 | Lean | false | false | 6,325 | lean | import subgroup.definitions
/-
An API for subgroups
Mathematician-friendly
Let G be a group. The type of subgroups of G is `subgroup G`.
In other words, if `H : subgroup G` then H is a subgroup of G.
The three basic facts you need to know about H are:
H.one_mem : (1 : G) ∈ H
H.mul_mem {x y : G} : x ∈ H → y ∈ H → x * y ∈ H
H.inv_mem {x : G} : x ∈ H → x⁻¹ ∈ H
Subgroups of a group form what is known as a *lattice*.
This is a partially ordered set with a sensible notion of
max and min (and even sup and inf).
-/
-- This entire project takes place in the `mygroup` namespace
namespace mygroup
-- TODO: prove subgroups are a lattice/semilattice-sup-bot/complete lattice/ whatever
namespace subgroup
variables {G : Type}
variables [group G]
-- The intersect of two subgroups is also a subgroup
def inter_subgroup (H K : subgroup G) : subgroup G :=
{ carrier := H ∩ K,
one_mem' := ⟨H.one_mem, K.one_mem⟩,
mul_mem' := λ _ _ ⟨hhx, hkx⟩ ⟨hhy, hky⟩,
⟨H.mul_mem hhx hhy, K.mul_mem hkx hky⟩,
inv_mem' := λ x ⟨hhx, hhy⟩,
⟨H.inv_mem hhx, K.inv_mem hhy⟩}
open set
variable {ι : Sort}
-- The intersect of a set of subgroups is a subgroup
def Inter_subgroup (H : ι → subgroup G) : subgroup G :=
{ carrier := ⋂ i, H i,
one_mem' := mem_Inter.mpr $ λ i, (H i).one_mem,
mul_mem' := λ _ _ hx hy, mem_Inter.mpr $ λ i,
by {rw mem_Inter at *, from mul_mem (H i) (hx i) (hy i)},
inv_mem' := λ x hx, mem_Inter.mpr $ λ i, (H i).inv_mem $ by apply mem_Inter.mp hx }
-- Some equivalent definitions for normal groups from wikipedia
-- Any two elements commute regarding the normal subgroup membership relation
lemma in_normal_to_comm {K : subgroup G} [normal K] : ∀ g k : G, g * k ∈ K → k * g ∈ K :=
begin
intros g k hgk,
suffices : g⁻¹ * (g * k) * g ∈ K,
{rwa [←group.mul_assoc, group.mul_left_inv, group.one_mul] at this},
convert normal.conjugate g⁻¹ (g * k) hgk, rw group.inv_inv
end
instance comm_to_in_normal {K : subgroup G} (h : ∀ g k : G, g * k ∈ K → k * g ∈ K) : normal K :=
begin
split,
intros g k hk,
suffices : g * (k * g⁻¹) ∈ K,
{rwa ←group.mul_assoc at this},
apply h (k * g⁻¹) g,
rwa [group.mul_assoc, group.mul_left_inv, group.mul_one]
end
-- If K is a normal subgroup of the group G, then the sets of left and right cosets of K in the G coincide
lemma nomal_coset_eq {K : subgroup G} [normal K] :
∀ g : G, left_coset g K = right_coset g K :=
begin
intros g,
ext, split,
all_goals {repeat {rw set.mem_set_of_eq}, intros hx, rcases hx with ⟨k, ⟨hk₁, hk₂⟩⟩},
{use (g * k * g⁻¹), split,
all_goals {try {apply normal.conjugate, assumption}},
rwa [←hk₂, group.mul_assoc, group.mul_left_inv, group.mul_one]
},
use (g⁻¹ * k * g), split,
convert normal.conjugate g⁻¹ k hk₁, rwa group.inv_inv,
rwa [←group.mul_assoc, ←group.mul_assoc, group.mul_right_inv, group.one_mul, hk₂]
end
instance coset_eq_normal {K : subgroup G} (h : ∀ g : G, left_coset g K = right_coset g K) : normal K :=
begin
split,
intros g k hk,
replace h : {s : G | ∃ (k : G) (H : k ∈ K), s = g * k} = {s : G | ∃ (k : G) (H : k ∈ K), s = k * g} := h g,
have : ∃ s ∈ {s : G | ∃ k ∈ K, s = k * g}, s = g * k :=
by {rw ←h, use (g * k),
simp only [group.mul_right_cancel_iff, exists_prop, and_true, set.mem_set_of_eq],
split, use k, from ⟨hk, rfl⟩, refl
},
rcases this with ⟨s, ⟨hs₁, hs₂⟩⟩,
rw set.mem_set_of_eq at hs₁,
rcases hs₁ with ⟨l, ⟨hl₁, hl₂⟩⟩,
rw [←hs₂, hl₂, group.mul_assoc, group.mul_right_inv, group.mul_one],
assumption
end
-- If K is a normal subgroup of the group G then the product of an element of the left coset of K with respect to g ∈ G and an element of the left coset of N with respect to h ∈ G is an element of the left coset of K with respect to gh
lemma normal_to_prod_in_coset {K : subgroup G} [normal K] :
∀ x y g h : G, x ∈ left_coset g K ∧ y ∈ left_coset h K → x * y ∈ left_coset (g * h) K :=
begin
rintros x y g h ⟨hx, hy⟩,
rw set.mem_set_of_eq at hx hy,
rcases hx with ⟨k₀, ⟨hx₁, hx₂⟩⟩,
rcases hy with ⟨k₁, ⟨hy₁, hy₂⟩⟩,
rw [hx₂, hy₂],
suffices : h⁻¹ * k₀ * h * k₁ ∈ K,
{rw set.mem_set_of_eq,
use h⁻¹ * k₀ * h * k₁,
split, assumption,
apply group.mul_left_cancel g⁻¹,
rw [←@group.mul_assoc _ _ g⁻¹ (g * k₀) (h * k₁), ←@group.mul_assoc _ _ g⁻¹ g k₀,
group.mul_left_inv, group.one_mul,
←@group.mul_assoc _ _ g⁻¹ (g * h) (h⁻¹ * k₀ * h * k₁), ←@group.mul_assoc _ _ g⁻¹ g h,
group.mul_left_inv, group.one_mul],
apply group.mul_left_cancel h⁻¹,
rw [←@group.mul_assoc _ _ h⁻¹ h (h⁻¹ * k₀ * h * k₁), group.mul_left_inv, group.one_mul,
←@group.mul_assoc _ _ h⁻¹ k₀ (h * k₁), ←@group.mul_assoc _ _ (h⁻¹ * k₀) h k₁]
},
apply @mul_mem _ _ K (h⁻¹ * k₀ * h) k₁,
convert normal.conjugate h⁻¹ k₀ hx₁,
rw group.inv_inv,
assumption
end
instance prod_in_coset_to_normal {K : subgroup G}
(h : ∀ x y g h : G, x ∈ left_coset g K ∧ y ∈ left_coset h K → x * y ∈ left_coset (g * h) K) : normal K :=
begin
split, intros g k hk,
let x := g * k,
let y := g⁻¹ * k,
suffices : g * k * g⁻¹ * k ∈ K,
{rw [←group.mul_one (g * k * g⁻¹), ←group.mul_right_inv k, ←group.mul_assoc],
apply mul_mem, assumption,
apply inv_mem, assumption
},
suffices : g * k * g⁻¹ * k ∈ left_coset (g * g⁻¹) K,
{rw [set.mem_set_of_eq, group.mul_right_inv] at this,
rcases this with ⟨l, ⟨hl₁, hl₂⟩⟩,
rw [hl₂, group.one_mul], assumption
},
rw group.mul_assoc,
show x * y ∈ left_coset (g * g⁻¹) K,
apply h x y g g⁻¹,
split, {use k, from ⟨hk, rfl⟩},
{use k, from ⟨hk, rfl⟩}
end
/-
TODO : Normal K equivalent to
- K is a union of conjugate classes
-/
-- Trivial central subgroups
end subgroup
end mygroup |
abb85648042c99a76101db535d2384ba2fb3f0ff | 07c76fbd96ea1786cc6392fa834be62643cea420 | /hott/homotopy/connectedness.hlean | e375511a8ee96cef42e926b0bde52d0a4779d621 | [
"Apache-2.0"
] | permissive | fpvandoorn/lean2 | 5a430a153b570bf70dc8526d06f18fc000a60ad9 | 0889cf65b7b3cebfb8831b8731d89c2453dd1e9f | refs/heads/master | 1,592,036,508,364 | 1,545,093,958,000 | 1,545,093,958,000 | 75,436,854 | 0 | 0 | null | 1,480,718,780,000 | 1,480,718,780,000 | null | UTF-8 | Lean | false | false | 31,010 | hlean | /-
Copyright (c) 2015 Ulrik Buchholtz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Ulrik Buchholtz, Floris van Doorn
Connectedness of types and functions
-/
import types.trunc types.arrow_2 types.lift
open eq is_trunc is_equiv nat equiv trunc function fiber funext pi pointed
definition is_conn [reducible] (n : ℕ₋₂) (A : Type) : Type :=
is_contr (trunc n A)
definition is_conn_fun [reducible] (n : ℕ₋₂) {A B : Type} (f : A → B) : Type :=
Πb : B, is_conn n (fiber f b)
definition is_conn_inf [reducible] (A : Type) : Type := Πn, is_conn n A
definition is_conn_fun_inf [reducible] {A B : Type} (f : A → B) : Type := Πn, is_conn_fun n f
namespace is_conn
definition is_conn_equiv_closed (n : ℕ₋₂) {A B : Type}
: A ≃ B → is_conn n A → is_conn n B :=
begin
intros H C,
exact is_contr_equiv_closed (trunc_equiv_trunc n H) C,
end
definition is_conn_equiv_closed_rev (n : ℕ₋₂) {A B : Type} (f : A ≃ B) (H : is_conn n B) :
is_conn n A :=
is_conn_equiv_closed n f⁻¹ᵉ _
definition is_conn_of_eq {n m : ℕ₋₂} (p : n = m) {A : Type} (H : is_conn n A) : is_conn m A :=
transport (λk, is_conn k A) p H
theorem is_conn_of_le (A : Type) {n k : ℕ₋₂} (H : n ≤ k) [is_conn k A] : is_conn n A :=
is_contr_equiv_closed (trunc_trunc_equiv_left _ H) _
theorem is_conn_fun_of_le {A B : Type} (f : A → B) {n k : ℕ₋₂} (H : n ≤ k)
[is_conn_fun k f] : is_conn_fun n f :=
λb, is_conn_of_le _ H
definition is_conn_of_is_conn_succ (n : ℕ₋₂) (A : Type) [is_conn (n.+1) A] : is_conn n A :=
is_trunc_trunc_of_le A -2 (trunc_index.self_le_succ n)
namespace is_conn_fun
section
parameters (n : ℕ₋₂) {A B : Type} {h : A → B}
(H : is_conn_fun n h) (P : B → Type) [Πb, is_trunc n (P b)]
private definition rec.helper : (Πa : A, P (h a)) → Πb : B, trunc n (fiber h b) → P b :=
λt b, trunc.rec (λx, point_eq x ▸ t (point x))
private definition rec.g : (Πa : A, P (h a)) → (Πb : B, P b) :=
λt b, rec.helper t b (@center (trunc n (fiber h b)) (H b))
-- induction principle for n-connected maps (Lemma 7.5.7)
protected definition rec : is_equiv (λs : Πb : B, P b, λa : A, s (h a)) :=
adjointify (λs a, s (h a)) rec.g
begin
intro t, apply eq_of_homotopy, intro a, unfold rec.g, unfold rec.helper,
rewrite [@center_eq _ (H (h a)) (tr (fiber.mk a idp))],
end
begin
intro k, apply eq_of_homotopy, intro b, unfold rec.g,
generalize (@center _ (H b)), apply trunc.rec, apply fiber.rec,
intros a p, induction p, reflexivity
end
protected definition elim : (Πa : A, P (h a)) → (Πb : B, P b) :=
@is_equiv.inv _ _ (λs a, s (h a)) rec
protected definition elim_β : Πf : (Πa : A, P (h a)), Πa : A, elim f (h a) = f a :=
λf, apd10 (@is_equiv.right_inv _ _ (λs a, s (h a)) rec f)
end
section
parameters (n k : ℕ₋₂) {A B : Type} {f : A → B}
(H : is_conn_fun n f) (P : B → Type) [HP : Πb, is_trunc (n +2+ k) (P b)]
include H HP
-- Lemma 8.6.1
proposition elim_general : is_trunc_fun k (pi_functor_left f P) :=
begin
revert P HP,
induction k with k IH: intro P HP t,
{ apply is_contr_fiber_of_is_equiv, apply is_conn_fun.rec, exact H, exact HP},
{ apply is_trunc_succ_intro,
intros x y, cases x with g p, cases y with h q,
have e : fiber (λr : g ~ h, (λa, r (f a))) (apd10 (p ⬝ q⁻¹))
≃ (fiber.mk g p = fiber.mk h q
:> fiber (λs : (Πb, P b), (λa, s (f a))) t),
begin
apply equiv.trans !fiber.sigma_char,
have e' : Πr : g ~ h,
((λa, r (f a)) = apd10 (p ⬝ q⁻¹))
≃ (ap (λv, (λa, v (f a))) (eq_of_homotopy r) ⬝ q = p),
begin
intro r,
refine equiv.trans _ (eq_con_inv_equiv_con_eq q p
(ap (λv a, v (f a)) (eq_of_homotopy r))),
rewrite [-(ap (λv a, v (f a)) (apd10_eq_of_homotopy_fn r))],
rewrite [-(apd10_ap_precompose_dependent f (eq_of_homotopy r))],
apply equiv.symm,
apply eq_equiv_fn_eq_of_is_equiv (@apd10 A (λa, P (f a)) (λa, g (f a)) (λa, h (f a)))
end,
apply equiv.trans (sigma.sigma_equiv_sigma_right e'), clear e',
apply equiv.trans (equiv.symm (sigma.sigma_equiv_sigma_left
!eq_equiv_homotopy)),
apply equiv.symm, apply equiv.trans !fiber_eq_equiv,
apply sigma.sigma_equiv_sigma_right, intro r,
apply eq_equiv_eq_symm
end,
apply @is_trunc_equiv_closed _ _ k e, clear e,
apply IH (λb : B, (g b = h b)) (λb, @is_trunc_eq (P b) (n +2+ k) (HP b) (g b) (h b)) }
end
end
section
universe variables u v
parameters (n : ℕ₋₂) {A : Type.{u}} {B : Type.{v}} {h : A → B}
parameter sec : ΠP : B → trunctype.{max u v} n,
is_retraction (λs : (Πb : B, P b), λ a, s (h a))
private definition s := sec (λb, trunctype.mk' n (trunc n (fiber h b)))
include sec
-- the other half of Lemma 7.5.7
definition intro : is_conn_fun n h :=
begin
intro b,
apply is_contr.mk (@is_retraction.sect _ _ _ s (λa, tr (fiber.mk a idp)) b),
esimp, apply trunc.rec, apply fiber.rec, intros a p,
apply transport
(λz : (Σy, h a = y), @sect _ _ _ s (λa, tr (mk a idp)) (sigma.pr1 z) =
tr (fiber.mk a (sigma.pr2 z)))
(@center_eq _ (is_contr_sigma_eq (h a)) (sigma.mk b p)),
exact apd10 (@right_inverse _ _ _ s (λa, tr (fiber.mk a idp))) a
end
end
end is_conn_fun
-- Connectedness is related to maps to and from the unit type, first to
section
parameters (n : ℕ₋₂) (A : Type)
definition is_conn_of_map_to_unit
: is_conn_fun n (const A unit.star) → is_conn n A :=
begin
intro H, unfold is_conn_fun at H,
exact is_conn_equiv_closed n (fiber.fiber_star_equiv A) _,
end
definition is_conn_fun_to_unit_of_is_conn [H : is_conn n A] :
is_conn_fun n (const A unit.star) :=
begin
intro u, induction u,
exact is_conn_equiv_closed n (fiber.fiber_star_equiv A)⁻¹ᵉ _,
end
-- now maps from unit
definition is_conn_of_map_from_unit (a₀ : A) (H : is_conn_fun n (const unit a₀))
: is_conn n .+1 A :=
is_contr.mk (tr a₀)
begin
apply trunc.rec, intro a,
exact trunc.elim (λz : fiber (const unit a₀) a, ap tr (point_eq z))
(@center _ (H a))
end
definition is_conn_fun_from_unit (a₀ : A) [H : is_conn n .+1 A]
: is_conn_fun n (const unit a₀) :=
begin
intro a,
apply is_conn_equiv_closed n (equiv.symm (fiber_const_equiv A a₀ a)),
apply is_contr_equiv_closed (tr_eq_tr_equiv n a₀ a) _,
end
end
-- as special case we get elimination principles for pointed connected types
namespace is_conn
open pointed unit
section
parameters (n : ℕ₋₂) {A : Type*}
[H : is_conn n .+1 A] (P : A → Type) [Πa, is_trunc n (P a)]
include H
protected definition rec : is_equiv (λs : Πa : A, P a, s (Point A)) :=
@is_equiv_compose
(Πa : A, P a) (unit → P (Point A)) (P (Point A))
(λf, f unit.star) (λs x, s (Point A))
(is_conn_fun.rec n (is_conn_fun_from_unit n A (Point A)) P)
(to_is_equiv (arrow_unit_left (P (Point A))))
protected definition elim : P (Point A) → (Πa : A, P a) :=
@is_equiv.inv _ _ (λs, s (Point A)) rec
protected definition elim_β (p : P (Point A)) : elim p (Point A) = p :=
@is_equiv.right_inv _ _ (λs, s (Point A)) rec p
end
section
parameters (n k : ℕ₋₂) {A : Type*}
[H : is_conn n .+1 A] (P : A → Type) [Πa, is_trunc (n +2+ k) (P a)]
include H
proposition elim_general (p : P (Point A))
: is_trunc k (fiber (λs : (Πa : A, P a), s (Point A)) p) :=
@is_trunc_equiv_closed
(fiber (λs x, s (Point A)) (λx, p))
(fiber (λs, s (Point A)) p)
k
(equiv.symm (fiber.equiv_postcompose _ (arrow_unit_left (P (Point A))) _))
(is_conn_fun.elim_general n k (is_conn_fun_from_unit n A (Point A)) P (λx, p))
end
end is_conn
-- Lemma 7.5.2
definition minus_one_conn_of_surjective {A B : Type} (f : A → B)
: is_surjective f → is_conn_fun -1 f :=
begin
intro H, intro b,
exact is_contr_of_inhabited_prop (H b) _,
end
definition is_surjection_of_minus_one_conn {A B : Type} (f : A → B)
: is_conn_fun -1 f → is_surjective f :=
begin
intro H, intro b,
exact @center (∥fiber f b∥) (H b),
end
definition merely_of_minus_one_conn {A : Type} : is_conn -1 A → ∥A∥ :=
λH, @center (∥A∥) H
definition minus_one_conn_of_merely {A : Type} : ∥A∥ → is_conn -1 A :=
λx, is_contr_of_inhabited_prop x _
section
open arrow
variables {f g : arrow}
-- Lemma 7.5.4
definition retract_of_conn_is_conn [instance] (r : arrow_hom f g) [H : is_retraction r]
(n : ℕ₋₂) [K : is_conn_fun n f] : is_conn_fun n g :=
begin
intro b, unfold is_conn,
apply is_contr_retract (trunc_functor n (retraction_on_fiber r b)),
exact K (on_cod (arrow.is_retraction.sect r) b)
end
end
-- Corollary 7.5.5
definition is_conn_homotopy (n : ℕ₋₂) {A B : Type} {f g : A → B}
(p : f ~ g) (H : is_conn_fun n f) : is_conn_fun n g :=
@retract_of_conn_is_conn _ _
(arrow.arrow_hom_of_homotopy p) (arrow.is_retraction_arrow_hom_of_homotopy p) n H
/- introduction rules for connectedness -/
-- all types are -2-connected
definition is_conn_minus_two (A : Type) : is_conn -2 A :=
_
-- merely inhabited types are -1-connected
definition is_conn_minus_one (A : Type) (a : ∥ A ∥) : is_conn -1 A :=
is_contr.mk a (is_prop.elim _)
definition is_conn_minus_one_pointed [instance] (A : Type*) : is_conn -1 A :=
is_conn_minus_one A (tr pt)
definition is_conn_succ_intro {n : ℕ₋₂} {A : Type} (a : trunc (n.+1) A)
(H2 : Π(a a' : A), is_conn n (a = a')) : is_conn (n.+1) A :=
begin
refine is_contr_of_inhabited_prop _ _,
{ exact a },
{ apply is_trunc_succ_intro,
refine trunc.rec _, intro a, refine trunc.rec _, intro a',
exact is_contr_equiv_closed !tr_eq_tr_equiv⁻¹ᵉ _ }
end
definition is_conn_zero {A : Type} (a₀ : trunc 0 A) (p : Πa a' : A, ∥ a = a' ∥) : is_conn 0 A :=
is_conn_succ_intro a₀ (λa a', is_conn_minus_one _ (p a a'))
definition is_conn_zero_pointed {A : Type*} (p : Πa a' : A, ∥ a = a' ∥) : is_conn 0 A :=
is_conn_zero (tr pt) p
definition is_conn_zero_pointed' {A : Type*} (p : Πa : A, ∥ a = pt ∥) : is_conn 0 A :=
is_conn_zero_pointed (λa a', tconcat (p a) (tinverse (p a')))
/- connectedness of certain types -/
definition is_conn_trunc [instance] (A : Type) (n k : ℕ₋₂) [H : is_conn n A]
: is_conn n (trunc k A) :=
is_contr_equiv_closed !trunc_trunc_equiv_trunc_trunc _
definition is_conn_eq [instance] (n : ℕ₋₂) {A : Type} (a a' : A) [is_conn (n.+1) A] :
is_conn n (a = a') :=
is_contr_equiv_closed !tr_eq_tr_equiv _
definition is_conn_loop [instance] (n : ℕ₋₂) (A : Type*) [is_conn (n.+1) A] : is_conn n (Ω A) :=
!is_conn_eq
open pointed
definition is_conn_ptrunc [instance] (A : Type*) (n k : ℕ₋₂) [H : is_conn n A]
: is_conn n (ptrunc k A) :=
is_conn_trunc A n k
definition is_conn_pathover (n : ℕ₋₂) {A : Type} {B : A → Type} {a a' : A} (p : a = a') (b : B a)
(b' : B a') [is_conn (n.+1) (B a')] : is_conn n (b =[p] b') :=
is_conn_equiv_closed_rev n !pathover_equiv_tr_eq _
open sigma
lemma is_conn_sigma [instance] {A : Type} (B : A → Type) (n : ℕ₋₂)
[HA : is_conn n A] [HB : Πa, is_conn n (B a)] : is_conn n (Σa, B a) :=
begin
revert A B HA HB, induction n with n IH: intro A B HA HB,
{ apply is_conn_minus_two },
apply is_conn_succ_intro,
{ induction center (trunc (n.+1) A) with a, induction center (trunc (n.+1) (B a)) with b,
exact tr ⟨a, b⟩ },
intro a a', refine is_conn_equiv_closed_rev n !sigma_eq_equiv _,
apply IH, apply is_conn_eq, intro p, apply is_conn_pathover
/- an alternative proof of the successor case -/
-- induction center (trunc (n.+1) A) with a₀,
-- induction center (trunc (n.+1) (B a₀)) with b₀,
-- apply is_contr.mk (tr ⟨a₀, b₀⟩),
-- intro ab, induction ab with ab, induction ab with a b,
-- induction tr_eq_tr_equiv n a₀ a !is_prop.elim with p, induction p,
-- induction tr_eq_tr_equiv n b₀ b !is_prop.elim with q, induction q,
-- reflexivity
end
lemma is_conn_prod [instance] (A B : Type) (n : ℕ₋₂) [is_conn n A] [is_conn n B] :
is_conn n (A × B) :=
is_conn_equiv_closed n !sigma.equiv_prod _
lemma is_conn_fun_of_is_conn {A B : Type} (n : ℕ₋₂) (f : A → B)
[HA : is_conn n A] [HB : is_conn (n.+1) B] : is_conn_fun n f :=
λb, is_conn_equiv_closed_rev n !fiber.sigma_char _
definition is_conn_fiber_of_is_conn (n : ℕ₋₂) {A B : Type} (f : A → B) (b : B) [is_conn n A]
[is_conn (n.+1) B] : is_conn n (fiber f b) :=
is_conn_fun_of_is_conn n f b
lemma is_conn_pfiber_of_is_conn {A B : Type*} (n : ℕ₋₂) (f : A →* B)
[HA : is_conn n A] [HB : is_conn (n.+1) B] : is_conn n (pfiber f) :=
is_conn_fun_of_is_conn n f pt
definition is_conn_of_is_contr (k : ℕ₋₂) (A : Type) [is_contr A] : is_conn k A := _
definition is_conn_succ_of_is_conn_loop {n : ℕ₋₂} {A : Type*}
(H : is_conn 0 A) (H2 : is_conn n (Ω A)) : is_conn (n.+1) A :=
begin
apply is_conn_succ_intro, exact tr pt,
intros a a',
induction merely_of_minus_one_conn (is_conn_eq -1 a a') with p, induction p,
induction merely_of_minus_one_conn (is_conn_eq -1 pt a) with p, induction p,
exact H2
end
/- connected functions -/
definition is_conn_fun_of_is_equiv (k : ℕ₋₂) {A B : Type} (f : A → B) [is_equiv f] :
is_conn_fun k f :=
_
definition is_conn_fun_id (k : ℕ₋₂) (A : Type) : is_conn_fun k (@id A) :=
λa, _
definition is_conn_fun_compose (k : ℕ₋₂) {A B C : Type} {g : B → C} {f : A → B}
(Hg : is_conn_fun k g) (Hf : is_conn_fun k f) : is_conn_fun k (g ∘ f) :=
λc, is_conn_equiv_closed_rev k (fiber_compose_equiv g f c) _
-- Lemma 7.5.14
theorem is_equiv_trunc_functor_of_is_conn_fun [instance] {A B : Type} (n : ℕ₋₂) (f : A → B)
[H : is_conn_fun n f] : is_equiv (trunc_functor n f) :=
begin
fapply adjointify,
{ intro b, induction b with b, exact trunc_functor n point (center (trunc n (fiber f b)))},
{ intro b, induction b with b, esimp, generalize center (trunc n (fiber f b)), intro v,
induction v with v, induction v with a p, esimp, exact ap tr p},
{ intro a, induction a with a, esimp, rewrite [center_eq (tr (fiber.mk a idp))]}
end
definition trunc_equiv_trunc_of_is_conn_fun {A B : Type} (n : ℕ₋₂) (f : A → B)
[H : is_conn_fun n f] : trunc n A ≃ trunc n B :=
equiv.mk (trunc_functor n f) (is_equiv_trunc_functor_of_is_conn_fun n f)
definition ptrunc_pequiv_ptrunc_of_is_conn_fun {A B : Type*} (n : ℕ₋₂) (f : A →* B)
[H : is_conn_fun n f] : ptrunc n A ≃* ptrunc n B :=
pequiv_of_pmap (ptrunc_functor n f) (is_equiv_trunc_functor_of_is_conn_fun n f)
definition is_conn_fun_trunc_functor_of_le {n k : ℕ₋₂} {A B : Type} (f : A → B) (H : k ≤ n)
[H2 : is_conn_fun k f] : is_conn_fun k (trunc_functor n f) :=
begin
apply is_conn_fun.intro,
intro P, have Πb, is_trunc n (P b), from (λb, is_trunc_of_le _ H _),
fconstructor,
{ intro f' b,
induction b with b,
refine is_conn_fun.elim k H2 _ _ b, intro a, exact f' (tr a)},
{ intro f', apply eq_of_homotopy, intro a,
induction a with a, esimp, rewrite [is_conn_fun.elim_β]}
end
definition is_conn_fun_trunc_functor_of_ge {n k : ℕ₋₂} {A B : Type} (f : A → B) (H : n ≤ k)
[H2 : is_conn_fun k f] : is_conn_fun k (trunc_functor n f) :=
begin
apply is_conn_fun_of_is_equiv,
exact is_equiv_trunc_functor_of_le f H _
end
-- Exercise 7.18
definition is_conn_fun_trunc_functor {n k : ℕ₋₂} {A B : Type} (f : A → B)
[H2 : is_conn_fun k f] : is_conn_fun k (trunc_functor n f) :=
begin
eapply algebra.le_by_cases k n: intro H,
{ exact is_conn_fun_trunc_functor_of_le f H},
{ exact is_conn_fun_trunc_functor_of_ge f H}
end
open lift
definition is_conn_fun_lift_functor (n : ℕ₋₂) {A B : Type} (f : A → B) [is_conn_fun n f] :
is_conn_fun n (lift_functor f) :=
begin
intro b, cases b with b,
exact is_contr_equiv_closed_rev (trunc_equiv_trunc _ !fiber_lift_functor) _
end
open trunc_index
definition is_conn_fun_inf.mk_nat {A B : Type} {f : A → B} (H : Π(n : ℕ), is_conn_fun n f)
: is_conn_fun_inf f :=
begin
intro n,
cases n with n, { exact _},
cases n with n, { have -1 ≤ of_nat 0, from dec_star, apply is_conn_fun_of_le f this},
rewrite -of_nat_add_two, exact _
end
definition is_conn_inf.mk_nat {A : Type} (H : Π(n : ℕ), is_conn n A) : is_conn_inf A :=
begin
intro n,
cases n with n, { exact _},
cases n with n, { have -1 ≤ of_nat 0, from dec_star, apply is_conn_of_le A this},
rewrite -of_nat_add_two, exact _
end
definition is_conn_fun_trunc_elim_of_le {n k : ℕ₋₂} {A B : Type} [is_trunc n B] (f : A → B)
(H : k ≤ n) [H2 : is_conn_fun k f] : is_conn_fun k (trunc.elim f : trunc n A → B) :=
begin
apply is_conn_fun.intro,
intro P, have Πb, is_trunc n (P b), from (λb, is_trunc_of_le _ H _),
fconstructor,
{ intro f' b,
refine is_conn_fun.elim k H2 _ _ b, intro a, exact f' (tr a) },
{ intro f', apply eq_of_homotopy, intro a,
induction a with a, esimp, rewrite [is_conn_fun.elim_β] }
end
definition is_conn_fun_trunc_elim_of_ge {n k : ℕ₋₂} {A B : Type} [is_trunc n B] (f : A → B)
(H : n ≤ k) [H2 : is_conn_fun k f] : is_conn_fun k (trunc.elim f : trunc n A → B) :=
begin
apply is_conn_fun_of_is_equiv,
have H3 : is_equiv (trunc_functor k f), from !is_equiv_trunc_functor_of_is_conn_fun,
have H4 : is_equiv (trunc_functor n f), from is_equiv_trunc_functor_of_le _ H _,
apply is_equiv_of_equiv_of_homotopy (equiv.mk (trunc_functor n f) _ ⬝e !trunc_equiv),
intro x, induction x, reflexivity
end
definition is_conn_fun_trunc_elim {n k : ℕ₋₂} {A B : Type} [is_trunc n B] (f : A → B)
[H2 : is_conn_fun k f] : is_conn_fun k (trunc.elim f : trunc n A → B) :=
begin
eapply algebra.le_by_cases k n: intro H,
{ exact is_conn_fun_trunc_elim_of_le f H },
{ exact is_conn_fun_trunc_elim_of_ge f H }
end
lemma is_conn_fun_tr (n : ℕ₋₂) (A : Type) : is_conn_fun n (tr : A → trunc n A) :=
begin
apply is_conn_fun.intro,
intro P,
fconstructor,
{ intro f' b, induction b with a, exact f' a },
{ intro f', reflexivity }
end
definition is_contr_of_is_conn_of_is_trunc {n : ℕ₋₂} {A : Type} (H : is_trunc n A)
(K : is_conn n A) : is_contr A :=
is_contr_equiv_closed (trunc_equiv n A) _
definition is_trunc_succ_succ_of_is_trunc_loop (n : ℕ₋₂) (A : Type*) (H : is_trunc (n.+1) (Ω A))
(H2 : is_conn 0 A) : is_trunc (n.+2) A :=
begin
apply is_trunc_succ_of_is_trunc_loop, apply minus_one_le_succ,
refine is_conn.elim -1 _ _, exact H
end
lemma is_trunc_of_is_trunc_loopn (m n : ℕ) (A : Type*) (H : is_trunc n (Ω[m] A))
(H2 : is_conn (m.-1) A) : is_trunc (m + n) A :=
begin
revert A H H2; induction m with m IH: intro A H H2,
{ rewrite [nat.zero_add], exact H },
rewrite [succ_add],
apply is_trunc_succ_succ_of_is_trunc_loop,
{ apply IH,
{ exact is_trunc_equiv_closed _ !loopn_succ_in _ },
apply is_conn_loop },
exact is_conn_of_le _ (zero_le_of_nat m)
end
lemma is_trunc_of_is_set_loopn (m : ℕ) (A : Type*) (H : is_set (Ω[m] A))
(H2 : is_conn (m.-1) A) : is_trunc m A :=
is_trunc_of_is_trunc_loopn m 0 A H H2
end is_conn
/-
(bundled) connected types, possibly also truncated or with a point
The notation is n-Type*[k] for k-connected n-truncated pointed types, and you can remove
`n-`, `[k]` or `*` in any combination to remove some conditions
-/
structure conntype (n : ℕ₋₂) : Type :=
(carrier : Type)
(struct : is_conn n carrier)
notation `Type[`:95 n:0 `]`:0 := conntype n
attribute conntype.carrier [coercion]
attribute conntype.struct [instance] [priority 1300]
section
universe variable u
structure pconntype (n : ℕ₋₂) extends conntype.{u} n, pType.{u}
notation `Type*[`:95 n:0 `]`:0 := pconntype n
/-
There are multiple coercions from pconntype to Type. Type class inference doesn't recognize
that all of them are definitionally equal (for performance reasons). One instance is
automatically generated, and we manually add the missing instances.
-/
definition is_conn_pconntype [instance] {n : ℕ₋₂} (X : Type*[n]) : is_conn n X :=
conntype.struct X
structure truncconntype (n k : ℕ₋₂) extends trunctype.{u} n,
conntype.{u} k renaming struct→conn_struct
notation n `-Type[`:95 k:0 `]`:0 := truncconntype n k
definition is_conn_truncconntype [instance] {n k : ℕ₋₂} (X : n-Type[k]) :
is_conn k (truncconntype._trans_of_to_trunctype X) :=
conntype.struct X
definition is_trunc_truncconntype [instance] {n k : ℕ₋₂} (X : n-Type[k]) : is_trunc n X :=
trunctype.struct X
structure ptruncconntype (n k : ℕ₋₂) extends ptrunctype.{u} n,
pconntype.{u} k renaming struct→conn_struct
notation n `-Type*[`:95 k:0 `]`:0 := ptruncconntype n k
attribute ptruncconntype._trans_of_to_pconntype ptruncconntype._trans_of_to_ptrunctype
ptruncconntype._trans_of_to_pconntype_1 ptruncconntype._trans_of_to_ptrunctype_1
ptruncconntype._trans_of_to_pconntype_2 ptruncconntype._trans_of_to_ptrunctype_2
ptruncconntype.to_pconntype ptruncconntype.to_ptrunctype
truncconntype._trans_of_to_conntype truncconntype._trans_of_to_trunctype
truncconntype.to_conntype truncconntype.to_trunctype [unfold 3]
attribute pconntype._trans_of_to_conntype pconntype._trans_of_to_pType
pconntype.to_pType pconntype.to_conntype [unfold 2]
definition is_conn_ptruncconntype [instance] {n k : ℕ₋₂} (X : n-Type*[k]) :
is_conn k (ptruncconntype._trans_of_to_ptrunctype X) :=
conntype.struct X
definition is_trunc_ptruncconntype [instance] {n k : ℕ₋₂} (X : n-Type*[k]) :
is_trunc n (ptruncconntype._trans_of_to_pconntype X) :=
trunctype.struct X
end
namespace is_conn
open sigma sigma.ops prod prod.ops
definition pconntype.sigma_char [constructor] (k : ℕ₋₂) :
Type*[k] ≃ Σ(X : Type*), is_conn k X :=
equiv.MK (λX, ⟨pconntype.to_pType X, _⟩)
(λX, pconntype.mk (carrier X.1) X.2 pt)
begin intro X, induction X with X HX, induction X, reflexivity end
begin intro X, induction X, reflexivity end
definition is_embedding_pconntype_to_pType (k : ℕ₋₂) : is_embedding (@pconntype.to_pType k) :=
begin
intro X Y, fapply is_equiv_of_equiv_of_homotopy,
{ exact eq_equiv_fn_eq (pconntype.sigma_char k) _ _ ⬝e subtype_eq_equiv _ _ },
intro p, induction p, reflexivity
end
definition pconntype_eq_equiv {k : ℕ₋₂} (X Y : Type*[k]) : (X = Y) ≃ (X ≃* Y) :=
equiv.mk _ (is_embedding_pconntype_to_pType k X Y) ⬝e pType_eq_equiv X Y
definition pconntype_eq {k : ℕ₋₂} {X Y : Type*[k]} (e : X ≃* Y) : X = Y :=
(pconntype_eq_equiv X Y)⁻¹ᵉ e
definition ptruncconntype.sigma_char [constructor] (n k : ℕ₋₂) :
n-Type*[k] ≃ Σ(X : Type*), is_trunc n X × is_conn k X :=
equiv.MK (λX, ⟨ptruncconntype._trans_of_to_pconntype_1 X, (_, _)⟩)
(λX, ptruncconntype.mk (carrier X.1) X.2.1 pt X.2.2)
begin intro X, induction X with X HX, induction HX, induction X, reflexivity end
begin intro X, induction X, reflexivity end
definition ptruncconntype.sigma_char_pconntype [constructor] (n k : ℕ₋₂) :
n-Type*[k] ≃ Σ(X : Type*[k]), is_trunc n X :=
equiv.MK (λX, ⟨ptruncconntype.to_pconntype X, _⟩)
(λX, ptruncconntype.mk (pconntype._trans_of_to_pType X.1) X.2 pt _)
begin intro X, induction X with X HX, induction HX, induction X, reflexivity end
begin intro X, induction X, reflexivity end
definition is_embedding_ptruncconntype_to_pconntype (n k : ℕ₋₂) :
is_embedding (@ptruncconntype.to_pconntype n k) :=
begin
intro X Y, fapply is_equiv_of_equiv_of_homotopy,
{ exact eq_equiv_fn_eq (ptruncconntype.sigma_char_pconntype n k) _ _ ⬝e subtype_eq_equiv _ _ },
intro p, induction p, reflexivity
end
definition ptruncconntype_eq_equiv {n k : ℕ₋₂} (X Y : n-Type*[k]) : (X = Y) ≃ (X ≃* Y) :=
equiv.mk _ (is_embedding_ptruncconntype_to_pconntype n k X Y) ⬝e pconntype_eq_equiv X Y
definition ptruncconntype_eq {n k : ℕ₋₂} {X Y : n-Type*[k]} (e : X ≃* Y) : X = Y :=
(ptruncconntype_eq_equiv X Y)⁻¹ᵉ e
definition ptruncconntype_functor [constructor] {n n' k k' : ℕ₋₂} (p : n = n') (q : k = k')
(X : n-Type*[k]) : n'-Type*[k'] :=
ptruncconntype.mk X (is_trunc_of_eq p _) pt (is_conn_of_eq q _)
definition ptruncconntype_equiv [constructor] {n n' k k' : ℕ₋₂} (p : n = n') (q : k = k') :
n-Type*[k] ≃ n'-Type*[k'] :=
equiv.MK (ptruncconntype_functor p q) (ptruncconntype_functor p⁻¹ q⁻¹)
(λX, ptruncconntype_eq pequiv.rfl) (λX, ptruncconntype_eq pequiv.rfl)
/- the k-connected cover of X, the fiber of the map X → ∥X∥ₖ. -/
open trunc_index
definition connect (k : ℕ) (X : Type*) : Type* :=
pfiber (ptr k X)
definition is_conn_connect (k : ℕ) (X : Type*) : is_conn k (connect k X) :=
is_conn_fun_tr k X (tr pt)
definition connconnect [constructor] (k : ℕ) (X : Type*) : Type*[k] :=
pconntype.mk (connect k X) (is_conn_connect k X) pt
definition connect_intro [constructor] {k : ℕ} {X : Type*} {Y : Type*} (H : is_conn k X)
(f : X →* Y) : X →* connect k Y :=
pmap.mk (λx, fiber.mk (f x) (is_conn.elim (k.-1) _ (ap tr (respect_pt f)) x))
begin
fapply fiber_eq, exact respect_pt f, apply is_conn.elim_β
end
definition ppoint_connect_intro [constructor] {k : ℕ} {X : Type*} {Y : Type*} (H : is_conn k X)
(f : X →* Y) : ppoint (ptr k Y) ∘* connect_intro H f ~* f :=
begin
induction f with f f₀, induction Y with Y y₀, esimp at (f,f₀), induction f₀,
fapply phomotopy.mk,
{ intro x, reflexivity },
{ symmetry, esimp, apply point_fiber_eq }
end
definition connect_intro_ppoint [constructor] {k : ℕ} {X : Type*} {Y : Type*} (H : is_conn k X)
(f : X →* connect k Y) : connect_intro H (ppoint (ptr k Y) ∘* f) ~* f :=
begin
cases f with f f₀,
fapply phomotopy.mk,
{ intro x, fapply fiber_eq, reflexivity,
refine @is_conn.elim (k.-1) _ _ _ (λx', !is_trunc_eq) _ x,
refine !is_conn.elim_β ⬝ _,
refine _ ⬝ !idp_con⁻¹,
symmetry, refine _ ⬝ !con_idp, exact fiber_eq_pr2 f₀ },
{ esimp, refine whisker_left _ !fiber_eq_eta ⬝ !fiber_eq_con ⬝ apd011 fiber_eq !idp_con _, esimp,
apply eq_pathover_constant_left,
refine whisker_right _ (whisker_right _ (whisker_right _ !is_conn.elim_β)) ⬝pv _,
esimp [connect], refine _ ⬝vp !con_idp,
apply move_bot_of_left, refine !idp_con ⬝ !con_idp⁻¹ ⬝ph _,
refine !con.assoc ⬝ !con.assoc ⬝pv _, apply whisker_tl,
note r := eq_bot_of_square (transpose (whisker_left_idp_square (fiber_eq_pr2 f₀))⁻¹ᵛ),
refine !con.assoc⁻¹ ⬝ whisker_right _ r⁻¹ ⬝pv _, clear r,
apply move_top_of_left,
refine whisker_right_idp (ap_con tr idp (ap point f₀))⁻¹ᵖ ⬝pv _,
exact (ap_con_idp_left tr (ap point f₀))⁻¹ʰ }
end
definition connect_intro_equiv [constructor] {k : ℕ} {X : Type*} (Y : Type*) (H : is_conn k X) :
(X →* connect k Y) ≃ (X →* Y) :=
begin
fapply equiv.MK,
{ intro f, exact ppoint (ptr k Y) ∘* f },
{ intro g, exact connect_intro H g },
{ intro g, apply eq_of_phomotopy, exact ppoint_connect_intro H g },
{ intro f, apply eq_of_phomotopy, exact connect_intro_ppoint H f }
end
definition connect_intro_pequiv [constructor] {k : ℕ} {X : Type*} (Y : Type*) (H : is_conn k X) :
ppmap X (connect k Y) ≃* ppmap X Y :=
pequiv_of_equiv (connect_intro_equiv Y H) (eq_of_phomotopy !pcompose_pconst)
definition connect_pequiv {k : ℕ} {X : Type*} (H : is_conn k X) : connect k X ≃* X :=
@pfiber_pequiv_of_is_contr _ _ (ptr k X) H
definition loop_connect (k : ℕ) (X : Type*) : Ω (connect (k+1) X) ≃* connect k (Ω X) :=
loop_pfiber (ptr (k+1) X) ⬝e*
pfiber_pequiv_of_square pequiv.rfl (loop_ptrunc_pequiv k X)
(phomotopy_of_phomotopy_pinv_left (ap1_ptr k X))
definition loopn_connect (k : ℕ) (X : Type*) : Ω[k+1] (connect k X) ≃* Ω[k+1] X :=
loopn_pfiber (k+1) (ptr k X) ⬝e*
@pfiber_pequiv_of_is_contr _ _ _ (@is_contr_loop_of_is_trunc (k+1) _ !is_trunc_trunc)
definition is_conn_of_is_conn_succ_nat (n : ℕ) (A : Type) [is_conn (n+1) A] : is_conn n A :=
is_conn_of_is_conn_succ n A
definition connect_functor (k : ℕ) {X Y : Type*} (f : X →* Y) : connect k X →* connect k Y :=
pfiber_functor f (ptrunc_functor k f) (ptr_natural k f)⁻¹*
definition connect_intro_pequiv_natural {k : ℕ} {X X' : Type*} {Y Y' : Type*} (f : X' →* X)
(g : Y →* Y') (H : is_conn k X) (H' : is_conn k X') :
psquare (connect_intro_pequiv Y H) (connect_intro_pequiv Y' H')
(ppcompose_left (connect_functor k g) ∘* ppcompose_right f)
(ppcompose_left g ∘* ppcompose_right f) :=
begin
refine _ ⬝v* _, exact connect_intro_pequiv Y H',
{ fapply phomotopy.mk,
{ intro h, apply eq_of_phomotopy, apply passoc },
{ xrewrite [▸*, pcompose_right_eq_of_phomotopy, pcompose_left_eq_of_phomotopy,
-+eq_of_phomotopy_trans],
apply ap eq_of_phomotopy, apply passoc_pconst_middle }},
{ fapply phomotopy.mk,
{ intro h, apply eq_of_phomotopy,
refine !passoc⁻¹* ⬝* pwhisker_right h (ppoint_natural _ _ _) ⬝* !passoc },
{ xrewrite [▸*, +pcompose_left_eq_of_phomotopy, -+eq_of_phomotopy_trans],
apply ap eq_of_phomotopy,
refine !trans_assoc ⬝ idp ◾** !passoc_pconst_right ⬝ _,
refine !trans_assoc ⬝ idp ◾** !pcompose_pconst_phomotopy ⬝ _,
apply symm_trans_eq_of_eq_trans, symmetry, apply passoc_pconst_right }}
end
end is_conn
|
dc3d4bfa2f7800ff29da3717c63b0408db93655d | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/urec.lean | bc040f07d800286f65192085f8b10f18e573ffbf | [
"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 | 924 | lean | import data.nat data.examples.vector data.list.basic
attribute nat [recursor]
attribute nat.rec [recursor]
attribute nat.rec_on [recursor]
attribute nat.strong_induction_on [recursor]
attribute nat.cases_on [recursor]
attribute vector.cases_on [recursor]
attribute vector.brec_on [recursor]
axiom badrec1 : Π (A : Type) (C : A → Type) (a : A) (l : list A), C a
attribute badrec1 [recursor]
axiom badrec2 : Π (A : Type) (M : list A → Type) (l : list A) (a : nat), M l
attribute badrec2 [recursor]
open list
axiom myrec : Π (A : Type) (M : list A → Type) (l : list A), M [] → (∀ a, M [a]) → (∀ a₁ a₂, M [a₁, a₂]) → M l
attribute myrec [recursor]
set_option pp.implicit true
set_option pp.universes true
check @myrec
print [recursor] myrec
print [recursor] nat.induction_on
check @vector.induction_on
print [recursor] vector.induction_on
check @Exists.rec
print [recursor] Exists.rec
|
547d281d230347cd1f81dea523e7931ea463511b | efce24474b28579aba3272fdb77177dc2b11d7aa | /src/homotopy_theory/topological_spaces/weak_equivalences.lean | 0d53e4208ca31a9d029dd6f39d4685dbfbe08a79 | [
"Apache-2.0"
] | permissive | rwbarton/lean-homotopy-theory | cff499f24268d60e1c546e7c86c33f58c62888ed | 39e1b4ea1ed1b0eca2f68bc64162dde6a6396dee | refs/heads/lean-3.4.2 | 1,622,711,883,224 | 1,598,550,958,000 | 1,598,550,958,000 | 136,023,667 | 12 | 6 | Apache-2.0 | 1,573,187,573,000 | 1,528,116,262,000 | Lean | UTF-8 | Lean | false | false | 4,050 | lean | import homotopy_theory.formal.i_category.homotopy_equivalences
import .pi_n
open function
noncomputable theory
open category_theory (hiding is_iso)
local notation f ` ∘ `:80 g:80 := g ≫ f
namespace homotopy_theory.topological_spaces
open homotopy_theory.cofibrations
open homotopy_theory.weak_equivalences
open homotopy_theory.topological_spaces.Top
local notation `Top` := Top.{0}
local notation `Set` := Type 0
def is_weak_equivalence {X Y : Top} (f : X ⟶ Y) : Prop :=
is_iso (π₀ &> f) ∧ ∀ n x, is_iso (π_induced n x f)
lemma is_weak_equivalence_iso {X Y : Top} (i : homeomorphism X Y) :
is_weak_equivalence i.hom :=
⟨⟨π₀.map_iso i, rfl⟩,
assume n x, ⟨(π n).map_iso (Top_ptd.mk_iso' i x), rfl⟩⟩
lemma is_weak_equivalence_comp {X Y Z : Top} {f : X ⟶ Y} {g : Y ⟶ Z}
(hf : is_weak_equivalence f) (hg : is_weak_equivalence g) :
is_weak_equivalence (g ∘ f) :=
⟨by rw π₀.map_comp; exact iso_comp hf.1 hg.1,
assume n x, by rw π_induced_comp; exact iso_comp (hf.2 n x) (hg.2 n (f x))⟩
instance : replete_wide_subcategory.{0} Top @is_weak_equivalence :=
replete_wide_subcategory.mk' @is_weak_equivalence_iso @is_weak_equivalence_comp
-- I don't know why this changed, but `⟦Top.const y⟧` in the proof below
-- started using `subtype.setoid` and `fun_setoid` (from core) by default.
local attribute [instance, priority 1000] Hom.setoid
-- I don't know what this means, but lean told me to turn it on
set_option eqn_compiler.zeta true
def Top_weak_equivalences : category_with_weak_equivalences Top :=
{ is_weq := @is_weak_equivalence,
weq_of_comp_weq_left := assume X Y Z f g hf hgf,
⟨iso_of_comp_iso_left hf.1 (by rw ←π₀.map_comp; exact hgf.1),
assume n y,
-- This is the nontrivial case: y may not be in the image of f.
-- But we know that f is an isomorphism on π₀, so y is at least
-- connected by a path to f x' for some x' : X.
let ⟨i, hi⟩ := hf.1 in
let x'_class := i.inv ⟦Top.const y⟧ in
let ⟨x'_map, hx'⟩ := quotient.exists_rep x'_class in
let x' := x'_map punit.star in
have (π₀ &> f) ⟦Top.const x'⟧ = ⟦Top.const y⟧, from
have Top.const x' = x'_map, by ext p; cases p; refl,
begin
rw [this, ←hi, hx'], dsimp [x'_class],
change (i.hom ∘ i.inv) _ = _,
erw i.inv_hom_id, refl
end,
have ⟦Top.const (f x')⟧ = ⟦Top.const y⟧, from this,
let ⟨(γ : path (f x') y)⟩ := quotient.exact this in
have is_iso (π_induced n (f x') g), from
iso_of_comp_iso_left (hf.2 n x')
(by rw ←π_induced_comp; exact hgf.2 n x'),
let ⟨i', hi'⟩ := this in
⟨(change_of_basepoint n γ).symm.trans $
i'.trans (change_of_basepoint n (γ.induced g)),
show (change_of_basepoint n (γ.induced g)).hom ∘
i'.hom ∘ (change_of_basepoint n γ).inv =
π_induced n y g,
by rw [hi', ←change_of_basepoint_induced, iso.inv_hom_id_assoc]⟩⟩,
weq_of_comp_weq_right := assume X Y Z f g hg hgf,
⟨iso_of_comp_iso_right hg.1 (by rw ←π₀.map_comp; exact hgf.1),
assume n x, iso_of_comp_iso_right (hg.2 n (f x))
(by rw ←π_induced_comp; exact hgf.2 n x)⟩ }
lemma is_weak_equivalence_of_heq {X Y : Top} (f : X ⟶ Y)
(h : homotopy_equivalence f) : is_weak_equivalence f :=
let ⟨g, gf1, fg1⟩ := homotopy_equivalence_iff.mp h in
⟨⟨⟨π₀ &> f, π₀ &> g,
by rw [←π₀.map_comp, ←π₀.map_id]; exact π₀_induced_homotopic gf1,
by rw [←π₀.map_comp, ←π₀.map_id]; exact π₀_induced_homotopic fg1⟩,
rfl⟩,
assume n x,
have gf : _ := π_induced_homotopic_id n x gf1.symm,
have fg : _ := π_induced_homotopic_id n (f x) fg1.symm,
begin
rw π_induced_comp at gf fg,
letI : homotopical_category Set := isomorphisms_as_homotopical_category,
change is_weq (π_induced n x f),
exact weq_two_out_of_six_f fg gf
end⟩
end homotopy_theory.topological_spaces
|
6b55715a52a8d09b3e45e2ba0dc13825ae77d77b | 367134ba5a65885e863bdc4507601606690974c1 | /src/category_theory/functor.lean | 7d4516efda2e59a5de72ec762e59907b4264659f | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 3,403 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Tim Baumann, Stephen Morgan, Scott Morrison
Defines a functor between categories.
(As it is a 'bundled' object rather than the `is_functorial` typeclass parametrised
by the underlying function on objects, the name is capitalised.)
Introduces notations
`C ⥤ D` for the type of all functors from `C` to `D`.
(I would like a better arrow here, unfortunately ⇒ (`\functor`) is taken by core.)
-/
import tactic.reassoc_axiom
import tactic.monotonicity
namespace category_theory
-- declare the `v`'s first; see `category_theory.category` for an explanation
universes v v₁ v₂ v₃ u u₁ u₂ u₃
/--
`functor C D` represents a functor between categories `C` and `D`.
To apply a functor `F` to an object use `F.obj X`, and to a morphism use `F.map f`.
The axiom `map_id` expresses preservation of identities, and
`map_comp` expresses functoriality.
See https://stacks.math.columbia.edu/tag/001B.
-/
structure functor (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] :
Type (max v₁ v₂ u₁ u₂) :=
(obj [] : C → D)
(map : Π {X Y : C}, (X ⟶ Y) → ((obj X) ⟶ (obj Y)))
(map_id' : ∀ (X : C), map (𝟙 X) = 𝟙 (obj X) . obviously)
(map_comp' : ∀ {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z), map (f ≫ g) = (map f) ≫ (map g) . obviously)
-- A functor is basically a function, so give ⥤ a similar precedence to → (25).
-- For example, `C × D ⥤ E` should parse as `(C × D) ⥤ E` not `C × (D ⥤ E)`.
infixr ` ⥤ `:26 := functor -- type as \func --
restate_axiom functor.map_id'
attribute [simp] functor.map_id
restate_axiom functor.map_comp'
attribute [reassoc, simp] functor.map_comp
namespace functor
section
variables (C : Type u₁) [category.{v₁} C]
/-- `𝟭 C` is the identity functor on a category `C`. -/
protected def id : C ⥤ C :=
{ obj := λ X, X,
map := λ _ _ f, f }
notation `𝟭` := functor.id -- Type this as `\sb1`
instance : inhabited (C ⥤ C) := ⟨functor.id C⟩
variable {C}
@[simp] lemma id_obj (X : C) : (𝟭 C).obj X = X := rfl
@[simp] lemma id_map {X Y : C} (f : X ⟶ Y) : (𝟭 C).map f = f := rfl
end
section
variables {C : Type u₁} [category.{v₁} C]
{D : Type u₂} [category.{v₂} D]
{E : Type u₃} [category.{v₃} E]
/--
`F ⋙ G` is the composition of a functor `F` and a functor `G` (`F` first, then `G`).
-/
def comp (F : C ⥤ D) (G : D ⥤ E) : C ⥤ E :=
{ obj := λ X, G.obj (F.obj X),
map := λ _ _ f, G.map (F.map f) }
infixr ` ⋙ `:80 := comp
@[simp] lemma comp_obj (F : C ⥤ D) (G : D ⥤ E) (X : C) : (F ⋙ G).obj X = G.obj (F.obj X) := rfl
@[simp] lemma comp_map (F : C ⥤ D) (G : D ⥤ E) {X Y : C} (f : X ⟶ Y) :
(F ⋙ G).map f = G.map (F.map f) := rfl
-- These are not simp lemmas because rewriting along equalities between functors
-- is not necessarily a good idea.
-- Natural isomorphisms are also provided in `whiskering.lean`.
protected lemma comp_id (F : C ⥤ D) : F ⋙ (𝟭 D) = F := by cases F; refl
protected lemma id_comp (F : C ⥤ D) : (𝟭 C) ⋙ F = F := by cases F; refl
end
@[mono] lemma monotone {α β : Type*} [preorder α] [preorder β] (F : α ⥤ β) :
monotone F.obj :=
λ a b h, le_of_hom (F.map (hom_of_le h))
end functor
end category_theory
|
4a9417201d8a5c8e9577beadc2f6c9262f60904e | 1d02a718c550dba762f0c3d2ad13d16a43649ca1 | /src/compiler.lean | 5b1b9f5351302b2aa62b55e29977936c4098e346 | [
"Apache-2.0"
] | permissive | mhuisi/rc-correctness | 48488dfbbe18e222399b0c5252d2803a9dd1be74 | 2b7878ac594ba285b0b5cdabe96f41c6e3bbcc87 | refs/heads/master | 1,590,988,773,033 | 1,585,334,858,000 | 1,585,334,858,000 | 190,653,803 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 2,209 | lean | import type_system
namespace rc_correctness
open rc_correctness.expr
open rc_correctness.fn_body
open rc_correctness.lin_type
def inc_𝕆_var (x : var) (V : finset var) (F : fn_body) (βₗ : var → lin_type) : fn_body :=
if βₗ x = 𝕆 ∧ x ∉ V then F else inc x; F
def dec_𝕆_var (x : var) (F : fn_body) (βₗ : var → lin_type) : fn_body :=
if βₗ x = 𝕆 ∧ x ∉ FV F then dec x; F else F
def dec_𝕆 (xs : list var) (F : fn_body) (βₗ : var → lin_type) : fn_body :=
xs.foldr (λ x acc, dec_𝕆_var x acc βₗ) F
def dec_𝕆' (xs : list var) (F : fn_body) (βₗ : var → lin_type) : fn_body :=
xs.foldr (λ x acc, if βₗ x = 𝕆 ∧ x ∉ FV F then dec x; acc else acc) F
def C_app : list (var × lin_type) → fn_body → (var → lin_type) → fn_body
| [] (z ≔ e; F) βₗ := z ≔ e; F
| ((y, t)::xs) (z ≔ e; F) βₗ :=
if t = 𝕆 then
inc_𝕆_var y ((xs.map prod.fst).to_finset ∪ FV F) (C_app xs (z ≔ e; F) βₗ) βₗ
else
C_app xs (z ≔ e; dec_𝕆_var y F βₗ) βₗ
| xs F βₗ := F
def C (β : const → var → lin_type) : fn_body → (var → lin_type) → fn_body
| (ret x) βₗ := inc_𝕆_var x finset.empty (ret x) βₗ
| (case x of Fs) βₗ :=
case x of Fs.map_wf (λ F h, dec_𝕆 ((FV (case x of Fs)).sort var_le) (C F βₗ) βₗ)
| (y ≔ x[i]; F) βₗ :=
if βₗ x = 𝕆 then
y ≔ x[i]; inc y; dec_𝕆_var x (C F (βₗ[y ↦ 𝕆])) βₗ
else
y ≔ x[i]; C F (βₗ[y ↦ 𝔹])
| (z ≔ c⟦ys…⟧; F) βₗ :=
C_app (ys.map (λ y, ⟨y, β c y⟩)) (z ≔ c⟦ys…⟧; C F (βₗ[z ↦ 𝕆])) βₗ
| (z ≔ c⟦ys…, _⟧; F) βₗ :=
C_app (ys.map (λ y, ⟨y, β c y⟩)) (z ≔ c⟦ys…, _⟧; C F (βₗ[z ↦ 𝕆])) βₗ
| (z ≔ x⟦y⟧; F) βₗ :=
C_app ([⟨x, 𝕆⟩, ⟨y, 𝕆⟩]) (z ≔ x⟦y⟧; C F (βₗ[z ↦ 𝕆])) βₗ
| (z ≔ ⟪ys⟫i; F) βₗ :=
C_app (ys.map (λ y, ⟨y, 𝕆⟩)) (z ≔ ⟪ys⟫i; C F (βₗ[z ↦ 𝕆])) βₗ
| F βₗ := F
def C_prog (β : const → var → lin_type) (δ : program) (c : const) : fn :=
let (βₗ, f) := (β c, δ c) in ⟨f.ys, dec_𝕆 f.ys (C β f.F βₗ) βₗ⟩
end rc_correctness |
c6d40b1473b026779d70e17a2adc2d18e8e684fb | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /tests/lean/varBinderUpdate.lean | 94c6942cee9da32fbd2347e6d91937a3b3776576 | [
"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 | 811 | lean | namespace Ex1
variable {α : Type}
variable [Add α]
variable (α)
def f (a : α) := a + a
#check f Nat 5
variable {α}
def g (b : α) := b
#check g 5
#check @f
#check @g
end Ex1
namespace Ex2
variable {α β : Type}
variable (α)
def f (a : α) := a
def g (b : β) := b
#check f Nat 5
#check g 5
#check @f
#check @g
variable (α)
end Ex2
namespace Ex3
variable {α : Type}
variable (f : α → α)
variable (α)
def g (a : α) := f a
#check @g
variable {f}
def h (a : α) := f a
#check @h
end Ex3
namespace Ex4
variable {α β : Type}
variable (α γ)
def g (a : α) (b : β) (c : γ) := (a, b, c)
#check g Nat Bool 10 "hello" true
end Ex4
namespace Ex5
variable [i : Add α]
variable (i) -- Error
end Ex5
namespace Ex6
variable (a : Nat)
variable (h : a = a := rfl)
variable {h} -- Error
end Ex6
|
1912ed690dfbc8b3799de7b7778beeacfab8963d | c777c32c8e484e195053731103c5e52af26a25d1 | /src/linear_algebra/tensor_product.lean | 7467afbf19bf247ee10bf16de58aaefc4365e0ee | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 43,675 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Mario Carneiro
-/
import group_theory.congruence
import algebra.module.submodule.bilinear
/-!
# Tensor product of modules over commutative semirings.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file constructs the tensor product of modules over commutative semirings. Given a semiring
`R` and modules over it `M` and `N`, the standard construction of the tensor product is
`tensor_product R M N`. It is also a module over `R`.
It comes with a canonical bilinear map `M → N → tensor_product R M N`.
Given any bilinear map `M → N → P`, there is a unique linear map `tensor_product R M N → P` whose
composition with the canonical bilinear map `M → N → tensor_product R M N` is the given bilinear
map `M → N → P`.
We start by proving basic lemmas about bilinear maps.
## Notations
This file uses the localized notation `M ⊗ N` and `M ⊗[R] N` for `tensor_product R M N`, as well
as `m ⊗ₜ n` and `m ⊗ₜ[R] n` for `tensor_product.tmul R m n`.
## Tags
bilinear, tensor, tensor product
-/
section semiring
variables {R : Type*} [comm_semiring R]
variables {R' : Type*} [monoid R']
variables {R'' : Type*} [semiring R'']
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*}
variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] [add_comm_monoid Q]
[add_comm_monoid S]
variables [module R M] [module R N] [module R P] [module R Q] [module R S]
variables [distrib_mul_action R' M]
variables [module R'' M]
include R
variables (M N)
namespace tensor_product
section
-- open free_add_monoid
variables (R)
/-- The relation on `free_add_monoid (M × N)` that generates a congruence whose quotient is
the tensor product. -/
inductive eqv : free_add_monoid (M × N) → free_add_monoid (M × N) → Prop
| of_zero_left : ∀ n : N, eqv (free_add_monoid.of (0, n)) 0
| of_zero_right : ∀ m : M, eqv (free_add_monoid.of (m, 0)) 0
| of_add_left : ∀ (m₁ m₂ : M) (n : N), eqv
(free_add_monoid.of (m₁, n) + free_add_monoid.of (m₂, n)) (free_add_monoid.of (m₁ + m₂, n))
| of_add_right : ∀ (m : M) (n₁ n₂ : N), eqv
(free_add_monoid.of (m, n₁) + free_add_monoid.of (m, n₂)) (free_add_monoid.of (m, n₁ + n₂))
| of_smul : ∀ (r : R) (m : M) (n : N), eqv
(free_add_monoid.of (r • m, n)) (free_add_monoid.of (m, r • n))
| add_comm : ∀ x y, eqv (x + y) (y + x)
end
end tensor_product
variables (R)
/-- The tensor product of two modules `M` and `N` over the same commutative semiring `R`.
The localized notations are `M ⊗ N` and `M ⊗[R] N`, accessed by `open_locale tensor_product`. -/
def tensor_product : Type* :=
(add_con_gen (tensor_product.eqv R M N)).quotient
variables {R}
localized "infix (name := tensor_product.infer)
` ⊗ `:100 := tensor_product hole!" in tensor_product
localized "notation (name := tensor_product)
M ` ⊗[`:100 R `] `:0 N:100 := tensor_product R M N" in tensor_product
namespace tensor_product
section module
instance : add_zero_class (M ⊗[R] N) :=
{ .. (add_con_gen (tensor_product.eqv R M N)).add_monoid }
instance : add_comm_semigroup (M ⊗[R] N) :=
{ add_comm := λ x y, add_con.induction_on₂ x y $ λ x y, quotient.sound' $
add_con_gen.rel.of _ _ $ eqv.add_comm _ _,
.. (add_con_gen (tensor_product.eqv R M N)).add_monoid }
instance : inhabited (M ⊗[R] N) := ⟨0⟩
variables (R) {M N}
/-- The canonical function `M → N → M ⊗ N`. The localized notations are `m ⊗ₜ n` and `m ⊗ₜ[R] n`,
accessed by `open_locale tensor_product`. -/
def tmul (m : M) (n : N) : M ⊗[R] N := add_con.mk' _ $ free_add_monoid.of (m, n)
variables {R}
infix ` ⊗ₜ `:100 := tmul _
notation x ` ⊗ₜ[`:100 R `] `:0 y:100 := tmul R x y
@[elab_as_eliminator]
protected theorem induction_on
{C : (M ⊗[R] N) → Prop}
(z : M ⊗[R] N)
(C0 : C 0)
(C1 : ∀ {x y}, C $ x ⊗ₜ[R] y)
(Cp : ∀ {x y}, C x → C y → C (x + y)) : C z :=
add_con.induction_on z $ λ x, free_add_monoid.rec_on x C0 $ λ ⟨m, n⟩ y ih,
by { rw add_con.coe_add, exact Cp C1 ih }
variables (M)
@[simp] lemma zero_tmul (n : N) : (0 : M) ⊗ₜ[R] n = 0 :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_left _
variables {M}
lemma add_tmul (m₁ m₂ : M) (n : N) : (m₁ + m₂) ⊗ₜ n = m₁ ⊗ₜ n + m₂ ⊗ₜ[R] n :=
eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_left _ _ _
variables (N)
@[simp] lemma tmul_zero (m : M) : m ⊗ₜ[R] (0 : N) = 0 :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_right _
variables {N}
lemma tmul_add (m : M) (n₁ n₂ : N) : m ⊗ₜ (n₁ + n₂) = m ⊗ₜ n₁ + m ⊗ₜ[R] n₂ :=
eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_right _ _ _
section
variables (R R' M N)
/--
A typeclass for `has_smul` structures which can be moved across a tensor product.
This typeclass is generated automatically from a `is_scalar_tower` instance, but exists so that
we can also add an instance for `add_comm_group.int_module`, allowing `z •` to be moved even if
`R` does not support negation.
Note that `module R' (M ⊗[R] N)` is available even without this typeclass on `R'`; it's only
needed if `tensor_product.smul_tmul`, `tensor_product.smul_tmul'`, or `tensor_product.tmul_smul` is
used.
-/
class compatible_smul [distrib_mul_action R' N] :=
(smul_tmul : ∀ (r : R') (m : M) (n : N), (r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n))
end
/-- Note that this provides the default `compatible_smul R R M N` instance through
`mul_action.is_scalar_tower.left`. -/
@[priority 100]
instance compatible_smul.is_scalar_tower
[has_smul R' R] [is_scalar_tower R' R M] [distrib_mul_action R' N] [is_scalar_tower R' R N] :
compatible_smul R R' M N :=
⟨λ r m n, begin
conv_lhs {rw ← one_smul R m},
conv_rhs {rw ← one_smul R n},
rw [←smul_assoc, ←smul_assoc],
exact (quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_smul _ _ _),
end⟩
/-- `smul` can be moved from one side of the product to the other .-/
lemma smul_tmul [distrib_mul_action R' N] [compatible_smul R R' M N] (r : R') (m : M) (n : N) :
(r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n) :=
compatible_smul.smul_tmul _ _ _
/-- Auxiliary function to defining scalar multiplication on tensor product. -/
def smul.aux {R' : Type*} [has_smul R' M] (r : R') : free_add_monoid (M × N) →+ M ⊗[R] N :=
free_add_monoid.lift $ λ p : M × N, (r • p.1) ⊗ₜ p.2
theorem smul.aux_of {R' : Type*} [has_smul R' M] (r : R') (m : M) (n : N) :
smul.aux r (free_add_monoid.of (m, n)) = (r • m) ⊗ₜ[R] n :=
rfl
variables [smul_comm_class R R' M]
variables [smul_comm_class R R'' M]
/-- Given two modules over a commutative semiring `R`, if one of the factors carries a
(distributive) action of a second type of scalars `R'`, which commutes with the action of `R`, then
the tensor product (over `R`) carries an action of `R'`.
This instance defines this `R'` action in the case that it is the left module which has the `R'`
action. Two natural ways in which this situation arises are:
* Extension of scalars
* A tensor product of a group representation with a module not carrying an action
Note that in the special case that `R = R'`, since `R` is commutative, we just get the usual scalar
action on a tensor product of two modules. This special case is important enough that, for
performance reasons, we define it explicitly below. -/
instance left_has_smul : has_smul R' (M ⊗[R] N) :=
⟨λ r, (add_con_gen (tensor_product.eqv R M N)).lift (smul.aux r : _ →+ M ⊗[R] N) $
add_con.add_con_gen_le $ λ x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, smul.aux_of, smul_zero, zero_tmul]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, smul.aux_of, tmul_zero]
| _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, smul.aux_of, smul_add, add_tmul]
| _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, smul.aux_of, tmul_add]
| _, _, (eqv.of_smul s m n) := (add_con.ker_rel _).2 $
by rw [smul.aux_of, smul.aux_of, ←smul_comm, smul_tmul]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end⟩
instance : has_smul R (M ⊗[R] N) := tensor_product.left_has_smul
protected theorem smul_zero (r : R') : (r • 0 : M ⊗[R] N) = 0 :=
add_monoid_hom.map_zero _
protected theorem smul_add (r : R') (x y : M ⊗[R] N) :
r • (x + y) = r • x + r • y :=
add_monoid_hom.map_add _ _ _
protected theorem zero_smul (x : M ⊗[R] N) : (0 : R'') • x = 0 :=
have ∀ (r : R'') (m : M) (n : N), r • (m ⊗ₜ[R] n) = (r • m) ⊗ₜ n := λ _ _ _, rfl,
tensor_product.induction_on x
(by rw tensor_product.smul_zero)
(λ m n, by rw [this, zero_smul, zero_tmul])
(λ x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy, add_zero])
protected theorem one_smul (x : M ⊗[R] N) : (1 : R') • x = x :=
have ∀ (r : R') (m : M) (n : N), r • (m ⊗ₜ[R] n) = (r • m) ⊗ₜ n := λ _ _ _, rfl,
tensor_product.induction_on x
(by rw tensor_product.smul_zero)
(λ m n, by rw [this, one_smul])
(λ x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy])
protected theorem add_smul (r s : R'') (x : M ⊗[R] N) : (r + s) • x = r • x + s • x :=
have ∀ (r : R'') (m : M) (n : N), r • (m ⊗ₜ[R] n) = (r • m) ⊗ₜ n := λ _ _ _, rfl,
tensor_product.induction_on x
(by simp_rw [tensor_product.smul_zero, add_zero])
(λ m n, by simp_rw [this, add_smul, add_tmul])
(λ x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy, add_add_add_comm] })
instance : add_comm_monoid (M ⊗[R] N) :=
{ nsmul := λ n v, n • v,
nsmul_zero' := by simp [tensor_product.zero_smul],
nsmul_succ' := by simp [nat.succ_eq_one_add, tensor_product.one_smul, tensor_product.add_smul],
.. tensor_product.add_comm_semigroup _ _, .. tensor_product.add_zero_class _ _}
instance left_distrib_mul_action : distrib_mul_action R' (M ⊗[R] N) :=
have ∀ (r : R') (m : M) (n : N), r • (m ⊗ₜ[R] n) = (r • m) ⊗ₜ n := λ _ _ _, rfl,
{ smul := (•),
smul_add := λ r x y, tensor_product.smul_add r x y,
mul_smul := λ r s x, tensor_product.induction_on x
(by simp_rw tensor_product.smul_zero)
(λ m n, by simp_rw [this, mul_smul])
(λ x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy] }),
one_smul := tensor_product.one_smul,
smul_zero := tensor_product.smul_zero }
instance : distrib_mul_action R (M ⊗[R] N) := tensor_product.left_distrib_mul_action
theorem smul_tmul' (r : R') (m : M) (n : N) :
r • (m ⊗ₜ[R] n) = (r • m) ⊗ₜ n :=
rfl
@[simp] lemma tmul_smul
[distrib_mul_action R' N] [compatible_smul R R' M N] (r : R') (x : M) (y : N) :
x ⊗ₜ (r • y) = r • (x ⊗ₜ[R] y) :=
(smul_tmul _ _ _).symm
lemma smul_tmul_smul (r s : R) (m : M) (n : N) : (r • m) ⊗ₜ[R] (s • n) = (r * s) • (m ⊗ₜ[R] n) :=
by simp only [tmul_smul, smul_tmul, mul_smul]
instance left_module : module R'' (M ⊗[R] N) :=
{ smul := (•),
add_smul := tensor_product.add_smul,
zero_smul := tensor_product.zero_smul,
..tensor_product.left_distrib_mul_action }
instance : module R (M ⊗[R] N) := tensor_product.left_module
instance [module R''ᵐᵒᵖ M] [is_central_scalar R'' M] : is_central_scalar R'' (M ⊗[R] N) :=
{ op_smul_eq_smul := λ r x,
tensor_product.induction_on x
(by rw [smul_zero, smul_zero])
(λ x y, by rw [smul_tmul', smul_tmul', op_smul_eq_smul])
(λ x y hx hy, by rw [smul_add, smul_add, hx, hy]) }
section
-- Like `R'`, `R'₂` provides a `distrib_mul_action R'₂ (M ⊗[R] N)`
variables {R'₂ : Type*} [monoid R'₂] [distrib_mul_action R'₂ M]
variables [smul_comm_class R R'₂ M] [has_smul R'₂ R']
/-- `is_scalar_tower R'₂ R' M` implies `is_scalar_tower R'₂ R' (M ⊗[R] N)` -/
instance is_scalar_tower_left [is_scalar_tower R'₂ R' M] :
is_scalar_tower R'₂ R' (M ⊗[R] N) :=
⟨λ s r x, tensor_product.induction_on x
(by simp)
(λ m n, by rw [smul_tmul', smul_tmul', smul_tmul', smul_assoc])
(λ x y ihx ihy, by rw [smul_add, smul_add, smul_add, ihx, ihy])⟩
variables [distrib_mul_action R'₂ N] [distrib_mul_action R' N]
variables [compatible_smul R R'₂ M N] [compatible_smul R R' M N]
/-- `is_scalar_tower R'₂ R' N` implies `is_scalar_tower R'₂ R' (M ⊗[R] N)` -/
instance is_scalar_tower_right [is_scalar_tower R'₂ R' N] :
is_scalar_tower R'₂ R' (M ⊗[R] N) :=
⟨λ s r x, tensor_product.induction_on x
(by simp)
(λ m n, by rw [←tmul_smul, ←tmul_smul, ←tmul_smul, smul_assoc])
(λ x y ihx ihy, by rw [smul_add, smul_add, smul_add, ihx, ihy])⟩
end
/-- A short-cut instance for the common case, where the requirements for the `compatible_smul`
instances are sufficient. -/
instance is_scalar_tower [has_smul R' R] [is_scalar_tower R' R M] :
is_scalar_tower R' R (M ⊗[R] N) :=
tensor_product.is_scalar_tower_left -- or right
variables (R M N)
/-- The canonical bilinear map `M → N → M ⊗[R] N`. -/
def mk : M →ₗ[R] N →ₗ[R] M ⊗[R] N :=
linear_map.mk₂ R (⊗ₜ) add_tmul (λ c m n, by rw [smul_tmul, tmul_smul]) tmul_add tmul_smul
variables {R M N}
@[simp] lemma mk_apply (m : M) (n : N) : mk R M N m n = m ⊗ₜ n := rfl
lemma ite_tmul (x₁ : M) (x₂ : N) (P : Prop) [decidable P] :
(if P then x₁ else 0) ⊗ₜ[R] x₂ = if P then x₁ ⊗ₜ x₂ else 0 :=
by { split_ifs; simp }
lemma tmul_ite (x₁ : M) (x₂ : N) (P : Prop) [decidable P] :
x₁ ⊗ₜ[R] (if P then x₂ else 0) = if P then x₁ ⊗ₜ x₂ else 0 :=
by { split_ifs; simp }
section
open_locale big_operators
lemma sum_tmul {α : Type*} (s : finset α) (m : α → M) (n : N) :
(∑ a in s, m a) ⊗ₜ[R] n = ∑ a in s, m a ⊗ₜ[R] n :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp, },
{ simp [finset.sum_insert has, add_tmul, ih], },
end
lemma tmul_sum (m : M) {α : Type*} (s : finset α) (n : α → N) :
m ⊗ₜ[R] (∑ a in s, n a) = ∑ a in s, m ⊗ₜ[R] n a :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp, },
{ simp [finset.sum_insert has, tmul_add, ih], },
end
end
variables (R M N)
/-- The simple (aka pure) elements span the tensor product. -/
lemma span_tmul_eq_top :
submodule.span R { t : M ⊗[R] N | ∃ m n, m ⊗ₜ n = t } = ⊤ :=
begin
ext t, simp only [submodule.mem_top, iff_true],
apply t.induction_on,
{ exact submodule.zero_mem _, },
{ intros m n, apply submodule.subset_span, use [m, n], },
{ intros t₁ t₂ ht₁ ht₂, exact submodule.add_mem _ ht₁ ht₂, },
end
@[simp] lemma map₂_mk_top_top_eq_top : submodule.map₂ (mk R M N) ⊤ ⊤ = ⊤ :=
begin
rw [← top_le_iff, ← span_tmul_eq_top, submodule.map₂_eq_span_image2],
exact submodule.span_mono (λ _ ⟨m, n, h⟩, ⟨m, n, trivial, trivial, h⟩),
end
end module
section UMP
variables {M N P Q}
variables (f : M →ₗ[R] N →ₗ[R] P)
/-- Auxiliary function to constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P`
with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def lift_aux : (M ⊗[R] N) →+ P :=
(add_con_gen (tensor_product.eqv R M N)).lift (free_add_monoid.lift $ λ p : M × N, f p.1 p.2) $
add_con.add_con_gen_le $ λ x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, f.map_zero₂]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, (f m).map_zero]
| _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, f.map_add₂]
| _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, (f m).map_add]
| _, _, (eqv.of_smul r m n) := (add_con.ker_rel _).2 $
by simp_rw [free_add_monoid.lift_eval_of, f.map_smul₂, (f m).map_smul]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end
lemma lift_aux_tmul (m n) : lift_aux f (m ⊗ₜ n) = f m n := rfl
variable {f}
@[simp] lemma lift_aux.smul (r : R) (x) : lift_aux f (r • x) = r • lift_aux f x :=
tensor_product.induction_on x (smul_zero _).symm
(λ p q, by rw [← tmul_smul, lift_aux_tmul, lift_aux_tmul, (f p).map_smul])
(λ p q ih1 ih2, by rw [smul_add, (lift_aux f).map_add, ih1, ih2, (lift_aux f).map_add, smul_add])
variable (f)
/-- Constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P` with the property that
its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def lift : M ⊗ N →ₗ[R] P :=
{ map_smul' := lift_aux.smul,
.. lift_aux f }
variable {f}
@[simp] lemma lift.tmul (x y) : lift f (x ⊗ₜ y) = f x y := rfl
@[simp] lemma lift.tmul' (x y) : (lift f).1 (x ⊗ₜ y) = f x y := rfl
theorem ext' {g h : (M ⊗[R] N) →ₗ[R] P}
(H : ∀ x y, g (x ⊗ₜ y) = h (x ⊗ₜ y)) : g = h :=
linear_map.ext $ λ z, tensor_product.induction_on z (by simp_rw linear_map.map_zero) H $
λ x y ihx ihy, by rw [g.map_add, h.map_add, ihx, ihy]
theorem lift.unique {g : (M ⊗[R] N) →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = f x y) :
g = lift f :=
ext' $ λ m n, by rw [H, lift.tmul]
theorem lift_mk : lift (mk R M N) = linear_map.id :=
eq.symm $ lift.unique $ λ x y, rfl
theorem lift_compr₂ (g : P →ₗ[R] Q) : lift (f.compr₂ g) = g.comp (lift f) :=
eq.symm $ lift.unique $ λ x y, by simp
theorem lift_mk_compr₂ (f : M ⊗ N →ₗ[R] P) : lift ((mk R M N).compr₂ f) = f :=
by rw [lift_compr₂ f, lift_mk, linear_map.comp_id]
/--
This used to be an `@[ext]` lemma, but it fails very slowly when the `ext` tactic tries to apply
it in some cases, notably when one wants to show equality of two linear maps. The `@[ext]`
attribute is now added locally where it is needed. Using this as the `@[ext]` lemma instead of
`tensor_product.ext'` allows `ext` to apply lemmas specific to `M →ₗ _` and `N →ₗ _`.
See note [partially-applied ext lemmas]. -/
theorem ext {g h : M ⊗ N →ₗ[R] P}
(H : (mk R M N).compr₂ g = (mk R M N).compr₂ h) : g = h :=
by rw [← lift_mk_compr₂ g, H, lift_mk_compr₂]
local attribute [ext] ext
example : M → N → (M → N → P) → P :=
λ m, flip $ λ f, f m
variables (R M N P)
/-- Linearly constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P`
with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def uncurry : (M →ₗ[R] N →ₗ[R] P) →ₗ[R] M ⊗[R] N →ₗ[R] P :=
linear_map.flip $ lift $ (linear_map.lflip _ _ _ _).comp (linear_map.flip linear_map.id)
variables {R M N P}
@[simp] theorem uncurry_apply (f : M →ₗ[R] N →ₗ[R] P) (m : M) (n : N) :
uncurry R M N P f (m ⊗ₜ n) = f m n :=
by rw [uncurry, linear_map.flip_apply, lift.tmul]; refl
variables (R M N P)
/-- A linear equivalence constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P`
with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def lift.equiv : (M →ₗ[R] N →ₗ[R] P) ≃ₗ[R] (M ⊗ N →ₗ[R] P) :=
{ inv_fun := λ f, (mk R M N).compr₂ f,
left_inv := λ f, linear_map.ext₂ $ λ m n, lift.tmul _ _,
right_inv := λ f, ext' $ λ m n, lift.tmul _ _,
.. uncurry R M N P }
@[simp] lemma lift.equiv_apply (f : M →ₗ[R] N →ₗ[R] P) (m : M) (n : N) :
lift.equiv R M N P f (m ⊗ₜ n) = f m n :=
uncurry_apply f m n
@[simp] lemma lift.equiv_symm_apply (f : M ⊗[R] N →ₗ[R] P) (m : M) (n : N) :
(lift.equiv R M N P).symm f m n = f (m ⊗ₜ n) :=
rfl
/-- Given a linear map `M ⊗ N → P`, compose it with the canonical bilinear map `M → N → M ⊗ N` to
form a bilinear map `M → N → P`. -/
def lcurry : (M ⊗[R] N →ₗ[R] P) →ₗ[R] M →ₗ[R] N →ₗ[R] P :=
(lift.equiv R M N P).symm
variables {R M N P}
@[simp] theorem lcurry_apply (f : M ⊗[R] N →ₗ[R] P) (m : M) (n : N) :
lcurry R M N P f m n = f (m ⊗ₜ n) := rfl
/-- Given a linear map `M ⊗ N → P`, compose it with the canonical bilinear map `M → N → M ⊗ N` to
form a bilinear map `M → N → P`. -/
def curry (f : M ⊗ N →ₗ[R] P) : M →ₗ[R] N →ₗ[R] P := lcurry R M N P f
@[simp] theorem curry_apply (f : M ⊗ N →ₗ[R] P) (m : M) (n : N) :
curry f m n = f (m ⊗ₜ n) := rfl
lemma curry_injective : function.injective (curry : (M ⊗[R] N →ₗ[R] P) → (M →ₗ[R] N →ₗ[R] P)) :=
λ g h H, ext H
theorem ext_threefold {g h : (M ⊗[R] N) ⊗[R] P →ₗ[R] Q}
(H : ∀ x y z, g ((x ⊗ₜ y) ⊗ₜ z) = h ((x ⊗ₜ y) ⊗ₜ z)) : g = h :=
begin
ext x y z,
exact H x y z
end
-- We'll need this one for checking the pentagon identity!
theorem ext_fourfold {g h : ((M ⊗[R] N) ⊗[R] P) ⊗[R] Q →ₗ[R] S}
(H : ∀ w x y z, g (((w ⊗ₜ x) ⊗ₜ y) ⊗ₜ z) = h (((w ⊗ₜ x) ⊗ₜ y) ⊗ₜ z)) : g = h :=
begin
ext w x y z,
exact H w x y z,
end
/-- Two linear maps (M ⊗ N) ⊗ (P ⊗ Q) → S which agree on all elements of the
form (m ⊗ₜ n) ⊗ₜ (p ⊗ₜ q) are equal. -/
theorem ext_fourfold' {φ ψ : (M ⊗[R] N) ⊗[R] (P ⊗[R] Q) →ₗ[R] S}
(H : ∀ w x y z, φ ((w ⊗ₜ x) ⊗ₜ (y ⊗ₜ z)) = ψ ((w ⊗ₜ x) ⊗ₜ (y ⊗ₜ z))) : φ = ψ :=
begin
ext m n p q,
exact H m n p q,
end
end UMP
variables {M N}
section
variables (R M)
/--
The base ring is a left identity for the tensor product of modules, up to linear equivalence.
-/
protected def lid : R ⊗ M ≃ₗ[R] M :=
linear_equiv.of_linear (lift $ linear_map.lsmul R M) (mk R R M 1)
(linear_map.ext $ λ _, by simp)
(ext' $ λ r m, by simp; rw [← tmul_smul, ← smul_tmul, smul_eq_mul, mul_one])
end
@[simp] theorem lid_tmul (m : M) (r : R) :
((tensor_product.lid R M) : (R ⊗ M → M)) (r ⊗ₜ m) = r • m :=
begin
dsimp [tensor_product.lid],
simp,
end
@[simp] lemma lid_symm_apply (m : M) :
(tensor_product.lid R M).symm m = 1 ⊗ₜ m := rfl
section
variables (R M N)
/--
The tensor product of modules is commutative, up to linear equivalence.
-/
protected def comm : M ⊗ N ≃ₗ[R] N ⊗ M :=
linear_equiv.of_linear (lift (mk R N M).flip) (lift (mk R M N).flip)
(ext' $ λ m n, rfl)
(ext' $ λ m n, rfl)
@[simp] theorem comm_tmul (m : M) (n : N) :
(tensor_product.comm R M N) (m ⊗ₜ n) = n ⊗ₜ m := rfl
@[simp] theorem comm_symm_tmul (m : M) (n : N) :
(tensor_product.comm R M N).symm (n ⊗ₜ m) = m ⊗ₜ n := rfl
end
section
variables (R M)
/--
The base ring is a right identity for the tensor product of modules, up to linear equivalence.
-/
protected def rid : M ⊗[R] R ≃ₗ[R] M :=
linear_equiv.trans (tensor_product.comm R M R) (tensor_product.lid R M)
end
@[simp] theorem rid_tmul (m : M) (r : R) :
(tensor_product.rid R M) (m ⊗ₜ r) = r • m :=
begin
dsimp [tensor_product.rid, tensor_product.comm, tensor_product.lid],
simp,
end
@[simp] lemma rid_symm_apply (m : M) :
(tensor_product.rid R M).symm m = m ⊗ₜ 1 := rfl
open linear_map
section
variables (R M N P)
/-- The associator for tensor product of R-modules, as a linear equivalence. -/
protected def assoc : (M ⊗[R] N) ⊗[R] P ≃ₗ[R] M ⊗[R] (N ⊗[R] P) :=
begin
refine linear_equiv.of_linear
(lift $ lift $ comp (lcurry R _ _ _) $ mk _ _ _)
(lift $ comp (uncurry R _ _ _) $ curry $ mk _ _ _)
(ext $ linear_map.ext $ λ m, ext' $ λ n p, _)
(ext $ flip_inj $ linear_map.ext $ λ p, ext' $ λ m n, _);
repeat { rw lift.tmul <|> rw compr₂_apply <|> rw comp_apply <|>
rw mk_apply <|> rw flip_apply <|> rw lcurry_apply <|>
rw uncurry_apply <|> rw curry_apply <|> rw id_apply }
end
end
@[simp] theorem assoc_tmul (m : M) (n : N) (p : P) :
(tensor_product.assoc R M N P) ((m ⊗ₜ n) ⊗ₜ p) = m ⊗ₜ (n ⊗ₜ p) := rfl
@[simp] theorem assoc_symm_tmul (m : M) (n : N) (p : P) :
(tensor_product.assoc R M N P).symm (m ⊗ₜ (n ⊗ₜ p)) = (m ⊗ₜ n) ⊗ₜ p := rfl
/-- The tensor product of a pair of linear maps between modules. -/
def map (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : M ⊗ N →ₗ[R] P ⊗ Q :=
lift $ comp (compl₂ (mk _ _ _) g) f
@[simp] theorem map_tmul (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (m : M) (n : N) :
map f g (m ⊗ₜ n) = f m ⊗ₜ g n :=
rfl
lemma map_range_eq_span_tmul (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(map f g).range = submodule.span R { t | ∃ m n, (f m) ⊗ₜ (g n) = t } :=
begin
simp only [← submodule.map_top, ← span_tmul_eq_top, submodule.map_span, set.mem_image,
set.mem_set_of_eq],
congr, ext t,
split,
{ rintros ⟨_, ⟨⟨m, n, rfl⟩, rfl⟩⟩, use [m, n], simp only [map_tmul], },
{ rintros ⟨m, n, rfl⟩, use [m ⊗ₜ n, m, n], simp only [map_tmul], },
end
/-- Given submodules `p ⊆ P` and `q ⊆ Q`, this is the natural map: `p ⊗ q → P ⊗ Q`. -/
@[simp] def map_incl (p : submodule R P) (q : submodule R Q) : p ⊗[R] q →ₗ[R] P ⊗[R] Q :=
map p.subtype q.subtype
section
variables {P' Q' : Type*}
variables [add_comm_monoid P'] [module R P']
variables [add_comm_monoid Q'] [module R Q']
lemma map_comp (f₂ : P →ₗ[R] P') (f₁ : M →ₗ[R] P) (g₂ : Q →ₗ[R] Q') (g₁ : N →ₗ[R] Q) :
map (f₂.comp f₁) (g₂.comp g₁) = (map f₂ g₂).comp (map f₁ g₁) :=
ext' $ λ _ _, rfl
lemma lift_comp_map (i : P →ₗ[R] Q →ₗ[R] Q') (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(lift i).comp (map f g) = lift ((i.comp f).compl₂ g) :=
ext' $ λ _ _, rfl
local attribute [ext] ext
@[simp] lemma map_id : map (id : M →ₗ[R] M) (id : N →ₗ[R] N) = id :=
by { ext, simp only [mk_apply, id_coe, compr₂_apply, id.def, map_tmul], }
@[simp] lemma map_one : map (1 : M →ₗ[R] M) (1 : N →ₗ[R] N) = 1 := map_id
lemma map_mul (f₁ f₂ : M →ₗ[R] M) (g₁ g₂ : N →ₗ[R] N) :
map (f₁ * f₂) (g₁ * g₂) = (map f₁ g₁) * (map f₂ g₂) :=
map_comp f₁ f₂ g₁ g₂
@[simp] protected lemma map_pow (f : M →ₗ[R] M) (g : N →ₗ[R] N) (n : ℕ) :
(map f g)^n = map (f^n) (g^n) :=
begin
induction n with n ih,
{ simp only [pow_zero, map_one], },
{ simp only [pow_succ', ih, map_mul], },
end
lemma map_add_left (f₁ f₂ : M →ₗ[R] P) (g : N →ₗ[R] Q) : map (f₁ + f₂) g = map f₁ g + map f₂ g :=
by {ext, simp only [add_tmul, compr₂_apply, mk_apply, map_tmul, add_apply]}
lemma map_add_right (f : M →ₗ[R] P) (g₁ g₂ : N →ₗ[R] Q) : map f (g₁ + g₂) = map f g₁ + map f g₂ :=
by {ext, simp only [tmul_add, compr₂_apply, mk_apply, map_tmul, add_apply]}
lemma map_smul_left (r : R) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : map (r • f) g = r • map f g :=
by {ext, simp only [smul_tmul, compr₂_apply, mk_apply, map_tmul, smul_apply, tmul_smul]}
lemma map_smul_right (r : R) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : map f (r • g) = r • map f g :=
by {ext, simp only [smul_tmul, compr₂_apply, mk_apply, map_tmul, smul_apply, tmul_smul]}
variables (R M N P Q)
/-- The tensor product of a pair of linear maps between modules, bilinear in both maps. -/
def map_bilinear : (M →ₗ[R] P) →ₗ[R] (N →ₗ[R] Q) →ₗ[R] (M ⊗[R] N →ₗ[R] P ⊗[R] Q) :=
linear_map.mk₂ R map map_add_left map_smul_left map_add_right map_smul_right
/-- The canonical linear map from `P ⊗[R] (M →ₗ[R] Q)` to `(M →ₗ[R] P ⊗[R] Q)` -/
def ltensor_hom_to_hom_ltensor : P ⊗[R] (M →ₗ[R] Q) →ₗ[R] (M →ₗ[R] P ⊗[R] Q) :=
tensor_product.lift (llcomp R M Q _ ∘ₗ mk R P Q)
/-- The canonical linear map from `(M →ₗ[R] P) ⊗[R] Q` to `(M →ₗ[R] P ⊗[R] Q)` -/
def rtensor_hom_to_hom_rtensor : (M →ₗ[R] P) ⊗[R] Q →ₗ[R] (M →ₗ[R] P ⊗[R] Q) :=
tensor_product.lift (llcomp R M P _ ∘ₗ (mk R P Q).flip).flip
/-- The linear map from `(M →ₗ P) ⊗ (N →ₗ Q)` to `(M ⊗ N →ₗ P ⊗ Q)` sending `f ⊗ₜ g` to
the `tensor_product.map f g`, the tensor product of the two maps. -/
def hom_tensor_hom_map : (M →ₗ[R] P) ⊗[R] (N →ₗ[R] Q) →ₗ[R] (M ⊗[R] N →ₗ[R] P ⊗[R] Q) :=
lift (map_bilinear R M N P Q)
variables {R M N P Q}
@[simp]
lemma map_bilinear_apply (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
map_bilinear R M N P Q f g = map f g := rfl
@[simp]
lemma ltensor_hom_to_hom_ltensor_apply (p : P) (f : M →ₗ[R] Q) (m : M) :
ltensor_hom_to_hom_ltensor R M P Q (p ⊗ₜ f) m = p ⊗ₜ f m := rfl
@[simp]
lemma rtensor_hom_to_hom_rtensor_apply (f : M →ₗ[R] P) (q : Q) (m : M) :
rtensor_hom_to_hom_rtensor R M P Q (f ⊗ₜ q) m = f m ⊗ₜ q := rfl
@[simp]
lemma hom_tensor_hom_map_apply (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
hom_tensor_hom_map R M N P Q (f ⊗ₜ g) = map f g := rfl
end
/-- If `M` and `P` are linearly equivalent and `N` and `Q` are linearly equivalent
then `M ⊗ N` and `P ⊗ Q` are linearly equivalent. -/
def congr (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) : M ⊗ N ≃ₗ[R] P ⊗ Q :=
linear_equiv.of_linear (map f g) (map f.symm g.symm)
(ext' $ λ m n, by simp; simp only [linear_equiv.apply_symm_apply])
(ext' $ λ m n, by simp; simp only [linear_equiv.symm_apply_apply])
@[simp] theorem congr_tmul (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) (m : M) (n : N) :
congr f g (m ⊗ₜ n) = f m ⊗ₜ g n :=
rfl
@[simp] theorem congr_symm_tmul (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) (p : P) (q : Q) :
(congr f g).symm (p ⊗ₜ q) = f.symm p ⊗ₜ g.symm q :=
rfl
variables (R M N P Q)
/-- A tensor product analogue of `mul_left_comm`. -/
def left_comm : M ⊗[R] (N ⊗[R] P) ≃ₗ[R] N ⊗[R] (M ⊗[R] P) :=
let e₁ := (tensor_product.assoc R M N P).symm,
e₂ := congr (tensor_product.comm R M N) (1 : P ≃ₗ[R] P),
e₃ := (tensor_product.assoc R N M P) in
e₁ ≪≫ₗ (e₂ ≪≫ₗ e₃)
variables {M N P Q}
@[simp] lemma left_comm_tmul (m : M) (n : N) (p : P) :
left_comm R M N P (m ⊗ₜ (n ⊗ₜ p)) = n ⊗ₜ (m ⊗ₜ p) :=
rfl
@[simp] lemma left_comm_symm_tmul (m : M) (n : N) (p : P) :
(left_comm R M N P).symm (n ⊗ₜ (m ⊗ₜ p)) = m ⊗ₜ (n ⊗ₜ p) :=
rfl
variables (M N P Q)
/-- This special case is worth defining explicitly since it is useful for defining multiplication
on tensor products of modules carrying multiplications (e.g., associative rings, Lie rings, ...).
E.g., suppose `M = P` and `N = Q` and that `M` and `N` carry bilinear multiplications:
`M ⊗ M → M` and `N ⊗ N → N`. Using `map`, we can define `(M ⊗ M) ⊗ (N ⊗ N) → M ⊗ N` which, when
combined with this definition, yields a bilinear multiplication on `M ⊗ N`:
`(M ⊗ N) ⊗ (M ⊗ N) → M ⊗ N`. In particular we could use this to define the multiplication in
the `tensor_product.semiring` instance (currently defined "by hand" using `tensor_product.mul`).
See also `mul_mul_mul_comm`. -/
def tensor_tensor_tensor_comm : (M ⊗[R] N) ⊗[R] (P ⊗[R] Q) ≃ₗ[R] (M ⊗[R] P) ⊗[R] (N ⊗[R] Q) :=
let e₁ := tensor_product.assoc R M N (P ⊗[R] Q),
e₂ := congr (1 : M ≃ₗ[R] M) (left_comm R N P Q),
e₃ := (tensor_product.assoc R M P (N ⊗[R] Q)).symm in
e₁ ≪≫ₗ (e₂ ≪≫ₗ e₃)
variables {M N P Q}
@[simp] lemma tensor_tensor_tensor_comm_tmul (m : M) (n : N) (p : P) (q : Q) :
tensor_tensor_tensor_comm R M N P Q ((m ⊗ₜ n) ⊗ₜ (p ⊗ₜ q)) = (m ⊗ₜ p) ⊗ₜ (n ⊗ₜ q) :=
rfl
@[simp] lemma tensor_tensor_tensor_comm_symm :
(tensor_tensor_tensor_comm R M N P Q).symm = tensor_tensor_tensor_comm R M P N Q :=
rfl
variables (M N P Q)
/-- This special case is useful for describing the interplay between `dual_tensor_hom_equiv` and
composition of linear maps.
E.g., composition of linear maps gives a map `(M → N) ⊗ (N → P) → (M → P)`, and applying
`dual_tensor_hom_equiv.symm` to the three hom-modules gives a map
`(M.dual ⊗ N) ⊗ (N.dual ⊗ P) → (M.dual ⊗ P)`, which agrees with the application of `contract_right`
on `N ⊗ N.dual` after the suitable rebracketting.
-/
def tensor_tensor_tensor_assoc : (M ⊗[R] N) ⊗[R] (P ⊗[R] Q) ≃ₗ[R] M ⊗[R] (N ⊗[R] P) ⊗[R] Q :=
(tensor_product.assoc R (M ⊗[R] N) P Q).symm ≪≫ₗ
congr (tensor_product.assoc R M N P) (1 : Q ≃ₗ[R] Q)
variables {M N P Q}
@[simp] lemma tensor_tensor_tensor_assoc_tmul (m : M) (n : N) (p : P) (q : Q) :
tensor_tensor_tensor_assoc R M N P Q ((m ⊗ₜ n) ⊗ₜ (p ⊗ₜ q)) = m ⊗ₜ (n ⊗ₜ p) ⊗ₜ q := rfl
@[simp] lemma tensor_tensor_tensor_assoc_symm_tmul (m : M) (n : N) (p : P) (q : Q) :
(tensor_tensor_tensor_assoc R M N P Q).symm (m ⊗ₜ (n ⊗ₜ p) ⊗ₜ q) = (m ⊗ₜ n) ⊗ₜ (p ⊗ₜ q) :=
rfl
end tensor_product
namespace linear_map
variables {R} (M) {N P Q}
/-- `ltensor M f : M ⊗ N →ₗ M ⊗ P` is the natural linear map induced by `f : N →ₗ P`. -/
def ltensor (f : N →ₗ[R] P) : M ⊗ N →ₗ[R] M ⊗ P :=
tensor_product.map id f
/-- `rtensor f M : N₁ ⊗ M →ₗ N₂ ⊗ M` is the natural linear map induced by `f : N₁ →ₗ N₂`. -/
def rtensor (f : N →ₗ[R] P) : N ⊗ M →ₗ[R] P ⊗ M :=
tensor_product.map f id
variables (g : P →ₗ[R] Q) (f : N →ₗ[R] P)
@[simp] lemma ltensor_tmul (m : M) (n : N) : f.ltensor M (m ⊗ₜ n) = m ⊗ₜ (f n) := rfl
@[simp] lemma rtensor_tmul (m : M) (n : N) : f.rtensor M (n ⊗ₜ m) = (f n) ⊗ₜ m := rfl
open tensor_product
local attribute [ext] tensor_product.ext
/-- `ltensor_hom M` is the natural linear map that sends a linear map `f : N →ₗ P` to `M ⊗ f`. -/
def ltensor_hom : (N →ₗ[R] P) →ₗ[R] (M ⊗[R] N →ₗ[R] M ⊗[R] P) :=
{ to_fun := ltensor M,
map_add' := λ f g, by
{ ext x y, simp only [compr₂_apply, mk_apply, add_apply, ltensor_tmul, tmul_add] },
map_smul' := λ r f, by
{ dsimp, ext x y, simp only [compr₂_apply, mk_apply, tmul_smul, smul_apply, ltensor_tmul] } }
/-- `rtensor_hom M` is the natural linear map that sends a linear map `f : N →ₗ P` to `M ⊗ f`. -/
def rtensor_hom : (N →ₗ[R] P) →ₗ[R] (N ⊗[R] M →ₗ[R] P ⊗[R] M) :=
{ to_fun := λ f, f.rtensor M,
map_add' := λ f g, by
{ ext x y, simp only [compr₂_apply, mk_apply, add_apply, rtensor_tmul, add_tmul] },
map_smul' := λ r f, by
{ dsimp, ext x y, simp only [compr₂_apply, mk_apply, smul_tmul, tmul_smul, smul_apply,
rtensor_tmul] } }
@[simp] lemma coe_ltensor_hom :
(ltensor_hom M : (N →ₗ[R] P) → (M ⊗[R] N →ₗ[R] M ⊗[R] P)) = ltensor M := rfl
@[simp] lemma coe_rtensor_hom :
(rtensor_hom M : (N →ₗ[R] P) → (N ⊗[R] M →ₗ[R] P ⊗[R] M)) = rtensor M := rfl
@[simp] lemma ltensor_add (f g : N →ₗ[R] P) : (f + g).ltensor M = f.ltensor M + g.ltensor M :=
(ltensor_hom M).map_add f g
@[simp] lemma rtensor_add (f g : N →ₗ[R] P) : (f + g).rtensor M = f.rtensor M + g.rtensor M :=
(rtensor_hom M).map_add f g
@[simp] lemma ltensor_zero : ltensor M (0 : N →ₗ[R] P) = 0 :=
(ltensor_hom M).map_zero
@[simp] lemma rtensor_zero : rtensor M (0 : N →ₗ[R] P) = 0 :=
(rtensor_hom M).map_zero
@[simp] lemma ltensor_smul (r : R) (f : N →ₗ[R] P) : (r • f).ltensor M = r • (f.ltensor M) :=
(ltensor_hom M).map_smul r f
@[simp] lemma rtensor_smul (r : R) (f : N →ₗ[R] P) : (r • f).rtensor M = r • (f.rtensor M) :=
(rtensor_hom M).map_smul r f
lemma ltensor_comp : (g.comp f).ltensor M = (g.ltensor M).comp (f.ltensor M) :=
by { ext m n, simp only [compr₂_apply, mk_apply, comp_apply, ltensor_tmul] }
lemma ltensor_comp_apply (x : M ⊗[R] N) :
(g.comp f).ltensor M x = (g.ltensor M) ((f.ltensor M) x) :=
by { rw [ltensor_comp, coe_comp], }
lemma rtensor_comp : (g.comp f).rtensor M = (g.rtensor M).comp (f.rtensor M) :=
by { ext m n, simp only [compr₂_apply, mk_apply, comp_apply, rtensor_tmul] }
lemma rtensor_comp_apply (x : N ⊗[R] M) :
(g.comp f).rtensor M x = (g.rtensor M) ((f.rtensor M) x) :=
by { rw [rtensor_comp, coe_comp], }
lemma ltensor_mul (f g : module.End R N) : (f * g).ltensor M = (f.ltensor M) * (g.ltensor M) :=
ltensor_comp M f g
lemma rtensor_mul (f g : module.End R N) : (f * g).rtensor M = (f.rtensor M) * (g.rtensor M) :=
rtensor_comp M f g
variables (N)
@[simp] lemma ltensor_id : (id : N →ₗ[R] N).ltensor M = id := map_id
-- `simp` can prove this.
lemma ltensor_id_apply (x : M ⊗[R] N) : (linear_map.id : N →ₗ[R] N).ltensor M x = x :=
by {rw [ltensor_id, id_coe, id.def], }
@[simp] lemma rtensor_id : (id : N →ₗ[R] N).rtensor M = id := map_id
-- `simp` can prove this.
lemma rtensor_id_apply (x : N ⊗[R] M) : (linear_map.id : N →ₗ[R] N).rtensor M x = x :=
by { rw [rtensor_id, id_coe, id.def], }
variables {N}
@[simp] lemma ltensor_comp_rtensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(g.ltensor P).comp (f.rtensor N) = map f g :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma rtensor_comp_ltensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(f.rtensor Q).comp (g.ltensor M) = map f g :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma map_comp_rtensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (f' : S →ₗ[R] M) :
(map f g).comp (f'.rtensor _) = map (f.comp f') g :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma map_comp_ltensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (g' : S →ₗ[R] N) :
(map f g).comp (g'.ltensor _) = map f (g.comp g') :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma rtensor_comp_map (f' : P →ₗ[R] S) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(f'.rtensor _).comp (map f g) = map (f'.comp f) g :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma ltensor_comp_map (g' : Q →ₗ[R] S) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(g'.ltensor _).comp (map f g) = map f (g'.comp g) :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
variables {M}
@[simp] lemma rtensor_pow (f : M →ₗ[R] M) (n : ℕ) : (f.rtensor N)^n = (f^n).rtensor N :=
by { have h := tensor_product.map_pow f (id : N →ₗ[R] N) n, rwa id_pow at h, }
@[simp] lemma ltensor_pow (f : N →ₗ[R] N) (n : ℕ) : (f.ltensor M)^n = (f^n).ltensor M :=
by { have h := tensor_product.map_pow (id : M →ₗ[R] M) f n, rwa id_pow at h, }
end linear_map
end semiring
section ring
variables {R : Type*} [comm_semiring R]
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*}
variables [add_comm_group M] [add_comm_group N] [add_comm_group P] [add_comm_group Q]
[add_comm_group S]
variables [module R M] [module R N] [module R P] [module R Q] [module R S]
namespace tensor_product
open_locale tensor_product
open linear_map
variables (R)
/-- Auxiliary function to defining negation multiplication on tensor product. -/
def neg.aux : free_add_monoid (M × N) →+ M ⊗[R] N :=
free_add_monoid.lift $ λ p : M × N, (-p.1) ⊗ₜ p.2
variables {R}
theorem neg.aux_of (m : M) (n : N) :
neg.aux R (free_add_monoid.of (m, n)) = (-m) ⊗ₜ[R] n :=
rfl
instance : has_neg (M ⊗[R] N) :=
{ neg := (add_con_gen (tensor_product.eqv R M N)).lift (neg.aux R) $ add_con.add_con_gen_le $
λ x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, neg.aux_of, neg_zero, zero_tmul]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, neg.aux_of, tmul_zero]
| _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, neg.aux_of, neg_add, add_tmul]
| _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, neg.aux_of, tmul_add]
| _, _, (eqv.of_smul s m n) := (add_con.ker_rel _).2 $
by simp_rw [neg.aux_of, tmul_smul s, smul_tmul', smul_neg]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end }
protected theorem add_left_neg (x : M ⊗[R] N) : -x + x = 0 :=
tensor_product.induction_on x
(by { rw [add_zero], apply (neg.aux R).map_zero, })
(λ x y, by { convert (add_tmul (-x) x y).symm, rw [add_left_neg, zero_tmul], })
(λ x y hx hy, by
{ unfold has_neg.neg sub_neg_monoid.neg,
rw add_monoid_hom.map_add,
ac_change (-x + x) + (-y + y) = 0,
rw [hx, hy, add_zero], })
instance : add_comm_group (M ⊗[R] N) :=
{ neg := has_neg.neg,
sub := _,
sub_eq_add_neg := λ _ _, rfl,
add_left_neg := λ x, by exact tensor_product.add_left_neg x,
zsmul := λ n v, n • v,
zsmul_zero' := by simp [tensor_product.zero_smul],
zsmul_succ' := by simp [nat.succ_eq_one_add, tensor_product.one_smul, tensor_product.add_smul],
zsmul_neg' := λ n x, begin
change (- n.succ : ℤ) • x = - (((n : ℤ) + 1) • x),
rw [← zero_add (-↑(n.succ) • x), ← tensor_product.add_left_neg (↑(n.succ) • x), add_assoc,
← add_smul, ← sub_eq_add_neg, sub_self, zero_smul, add_zero],
refl,
end,
.. tensor_product.add_comm_monoid }
lemma neg_tmul (m : M) (n : N) : (-m) ⊗ₜ n = -(m ⊗ₜ[R] n) := rfl
lemma tmul_neg (m : M) (n : N) : m ⊗ₜ (-n) = -(m ⊗ₜ[R] n) := (mk R M N _).map_neg _
lemma tmul_sub (m : M) (n₁ n₂ : N) : m ⊗ₜ (n₁ - n₂) = (m ⊗ₜ[R] n₁) - (m ⊗ₜ[R] n₂) :=
(mk R M N _).map_sub _ _
lemma sub_tmul (m₁ m₂ : M) (n : N) : (m₁ - m₂) ⊗ₜ n = (m₁ ⊗ₜ[R] n) - (m₂ ⊗ₜ[R] n) :=
(mk R M N).map_sub₂ _ _ _
/--
While the tensor product will automatically inherit a ℤ-module structure from
`add_comm_group.int_module`, that structure won't be compatible with lemmas like `tmul_smul` unless
we use a `ℤ-module` instance provided by `tensor_product.left_module`.
When `R` is a `ring` we get the required `tensor_product.compatible_smul` instance through
`is_scalar_tower`, but when it is only a `semiring` we need to build it from scratch.
The instance diamond in `compatible_smul` doesn't matter because it's in `Prop`.
-/
instance compatible_smul.int : compatible_smul R ℤ M N :=
⟨λ r m n, int.induction_on r
(by simp)
(λ r ih, by simpa [add_smul, tmul_add, add_tmul] using ih)
(λ r ih, by simpa [sub_smul, tmul_sub, sub_tmul] using ih)⟩
instance compatible_smul.unit {S} [monoid S] [distrib_mul_action S M] [distrib_mul_action S N]
[compatible_smul R S M N] :
compatible_smul R Sˣ M N :=
⟨λ s m n, (compatible_smul.smul_tmul (s : S) m n : _)⟩
end tensor_product
namespace linear_map
@[simp] lemma ltensor_sub (f g : N →ₗ[R] P) : (f - g).ltensor M = f.ltensor M - g.ltensor M :=
by simp only [← coe_ltensor_hom, map_sub]
@[simp] lemma rtensor_sub (f g : N →ₗ[R] P) : (f - g).rtensor M = f.rtensor M - g.rtensor M :=
by simp only [← coe_rtensor_hom, map_sub]
@[simp] lemma ltensor_neg (f : N →ₗ[R] P) : (-f).ltensor M = -(f.ltensor M) :=
by simp only [← coe_ltensor_hom, map_neg]
@[simp] lemma rtensor_neg (f : N →ₗ[R] P) : (-f).rtensor M = -(f.rtensor M) :=
by simp only [← coe_rtensor_hom, map_neg]
end linear_map
end ring
|
4ff946eb06f299bf9d267c4a1acc2815dd6232ef | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/algebra/category/CommRing/instances.lean | 0e8a2a8e130006eea34e25bff5c1465f28deca0a | [
"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 | 725 | lean | /-
Copyright (c) 2021 Andrew Yang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andrew Yang
-/
import algebra.category.CommRing.basic
import ring_theory.localization
/-!
# Ring-theoretic results in terms of categorical languages
-/
open category_theory
instance localization_unit_is_iso (R : CommRing) :
is_iso (CommRing.of_hom $ algebra_map R (localization.away (1 : R))) :=
is_iso.of_iso (is_localization.at_one R (localization.away (1 : R))).to_ring_equiv.to_CommRing_iso
instance localization_unit_is_iso' (R : CommRing) :
@is_iso CommRing _ R _ (CommRing.of_hom $ algebra_map R (localization.away (1 : R))) :=
by { cases R, exact localization_unit_is_iso _ }
|
21559015c1bef40225be9e068e7ec67b14903bb2 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/category/bitraversable/basic.lean | 7517fae249c53ac1fad848ad2fdaeb05e1e36c3f | [
"Apache-2.0"
] | permissive | gebner/mathlib | eab0150cc4f79ec45d2016a8c21750244a2e7ff0 | cc6a6edc397c55118df62831e23bfbd6e6c6b4ab | refs/heads/master | 1,625,574,853,976 | 1,586,712,827,000 | 1,586,712,827,000 | 99,101,412 | 1 | 0 | Apache-2.0 | 1,586,716,389,000 | 1,501,667,958,000 | Lean | UTF-8 | Lean | false | false | 3,012 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Simon Hudon
-/
import category.functor
category.bifunctor
category.traversable.basic
tactic.basic
/-!
# Bitraversable type class
Type class for traversing bifunctors. The concepts and laws are taken from
<https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Bitraversable.html>
Simple examples of `bitraversable` are `prod` and `sum`. A more elaborate example is
to define an a-list as:
```
def alist (key val : Type) := list (key × val)
```
Then we can use `f : key → io key'` and `g : val → io val'` to manipulate the `alist`'s key
and value respectively with `bitraverse f g : alist key val → io (alist key' val')`
## Main definitions
* bitraversable - exposes the `bitraverse` function
* is_lawful_bitraversable - laws similar to is_lawful_traversable
## Tags
traversable bitraversable iterator functor bifunctor applicative
-/
universes u
section prio
set_option default_priority 100 -- see Note [default priority]
class bitraversable (t : Type u → Type u → Type u)
extends bifunctor t :=
(bitraverse : Π {m : Type u → Type u} [applicative m] {α α' β β'},
(α → m α') → (β → m β') → t α β → m (t α' β'))
end prio
export bitraversable ( bitraverse )
def bisequence {t m} [bitraversable t] [applicative m] {α β} : t (m α) (m β) → m (t α β) :=
bitraverse id id
open functor
section prio
set_option default_priority 100 -- see Note [default priority]
class is_lawful_bitraversable (t : Type u → Type u → Type u) [bitraversable t]
extends is_lawful_bifunctor t :=
(id_bitraverse : ∀ {α β} (x : t α β), bitraverse id.mk id.mk x = id.mk x )
(comp_bitraverse : ∀ {F G} [applicative F] [applicative G]
[is_lawful_applicative F] [is_lawful_applicative G]
{α α' β β' γ γ'} (f : β → F γ) (f' : β' → F γ')
(g : α → G β) (g' : α' → G β') (x : t α α'),
bitraverse (comp.mk ∘ map f ∘ g) (comp.mk ∘ map f' ∘ g') x =
comp.mk (bitraverse f f' <$> bitraverse g g' x) )
(bitraverse_eq_bimap_id : ∀ {α α' β β'} (f : α → β) (f' : α' → β') (x : t α α'),
bitraverse (id.mk ∘ f) (id.mk ∘ f') x = id.mk (bimap f f' x))
(binaturality : ∀ {F G} [applicative F] [applicative G]
[is_lawful_applicative F] [is_lawful_applicative G]
(η : applicative_transformation F G) {α α' β β'}
(f : α → F β) (f' : α' → F β') (x : t α α'),
η (bitraverse f f' x) = bitraverse (@η _ ∘ f) (@η _ ∘ f') x)
end prio
export is_lawful_bitraversable ( id_bitraverse comp_bitraverse
bitraverse_eq_bimap_id )
open is_lawful_bitraversable
attribute [higher_order bitraverse_id_id] id_bitraverse
attribute [higher_order bitraverse_comp] comp_bitraverse
attribute [higher_order] binaturality bitraverse_eq_bimap_id
export is_lawful_bitraversable (bitraverse_id_id bitraverse_comp)
|
b3682d88d5f08b17b02828338b5b82f61d2d4c73 | 367134ba5a65885e863bdc4507601606690974c1 | /src/order/filter/countable_Inter.lean | ffda65e81114be675b2a113e6ffab5899a6188c7 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 6,367 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Yury G. Kudryashov
-/
import order.filter.basic
import data.set.countable
/-!
# Filters with countable intersection property
In this file we define `countable_Inter_filter` to be the class of filters with the following
property: for any countable collection of sets `s ∈ l` their intersection belongs to `l` as well.
Two main examples are the `residual` filter defined in `topology.metric_space.baire` and
the `measure.ae` filter defined in `measure_theory.measure_space`.
-/
open set filter
open_locale filter
variables {ι α : Type*}
/-- A filter `l` has the countable intersection property if for any countable collection
of sets `s ∈ l` their intersection belongs to `l` as well. -/
class countable_Inter_filter (l : filter α) : Prop :=
(countable_sInter_mem_sets' :
∀ {S : set (set α)} (hSc : countable S) (hS : ∀ s ∈ S, s ∈ l), ⋂₀ S ∈ l)
variables {l : filter α} [countable_Inter_filter l]
lemma countable_sInter_mem_sets {S : set (set α)} (hSc : countable S) :
⋂₀ S ∈ l ↔ ∀ s ∈ S, s ∈ l :=
⟨λ hS s hs, mem_sets_of_superset hS (sInter_subset_of_mem hs),
countable_Inter_filter.countable_sInter_mem_sets' hSc⟩
lemma countable_Inter_mem_sets [encodable ι] {s : ι → set α} :
(⋂ i, s i) ∈ l ↔ ∀ i, s i ∈ l :=
sInter_range s ▸ (countable_sInter_mem_sets (countable_range _)).trans forall_range_iff
lemma countable_bInter_mem_sets {S : set ι} (hS : countable S) {s : Π i ∈ S, set α} :
(⋂ i ∈ S, s i ‹_›) ∈ l ↔ ∀ i ∈ S, s i ‹_› ∈ l :=
begin
rw [bInter_eq_Inter],
haveI := hS.to_encodable,
exact countable_Inter_mem_sets.trans subtype.forall
end
lemma eventually_countable_forall [encodable ι] {p : α → ι → Prop} :
(∀ᶠ x in l, ∀ i, p x i) ↔ ∀ i, ∀ᶠ x in l, p x i :=
by simpa only [filter.eventually, set_of_forall]
using @countable_Inter_mem_sets _ _ l _ _ (λ i, {x | p x i})
lemma eventually_countable_ball {S : set ι} (hS : countable S) {p : Π (x : α) (i ∈ S), Prop} :
(∀ᶠ x in l, ∀ i ∈ S, p x i ‹_›) ↔ ∀ i ∈ S, ∀ᶠ x in l, p x i ‹_› :=
by simpa only [filter.eventually, set_of_forall]
using @countable_bInter_mem_sets _ _ l _ _ hS (λ i hi, {x | p x i hi})
lemma eventually_le.countable_Union [encodable ι] {s t : ι → set α} (h : ∀ i, s i ≤ᶠ[l] t i) :
(⋃ i, s i) ≤ᶠ[l] ⋃ i, t i :=
(eventually_countable_forall.2 h).mono $ λ x hst hs, mem_Union.2 $
(mem_Union.1 hs).imp hst
lemma eventually_eq.countable_Union [encodable ι] {s t : ι → set α} (h : ∀ i, s i =ᶠ[l] t i) :
(⋃ i, s i) =ᶠ[l] ⋃ i, t i :=
(eventually_le.countable_Union (λ i, (h i).le)).antisymm
(eventually_le.countable_Union (λ i, (h i).symm.le))
lemma eventually_le.countable_bUnion {S : set ι} (hS : countable S) {s t : Π i ∈ S, set α}
(h : ∀ i ∈ S, s i ‹_› ≤ᶠ[l] t i ‹_›) : (⋃ i ∈ S, s i ‹_›) ≤ᶠ[l] ⋃ i ∈ S, t i ‹_› :=
begin
simp only [bUnion_eq_Union],
haveI := hS.to_encodable,
exact eventually_le.countable_Union (λ i, h i i.2)
end
lemma eventually_eq.countable_bUnion {S : set ι} (hS : countable S) {s t : Π i ∈ S, set α}
(h : ∀ i ∈ S, s i ‹_› =ᶠ[l] t i ‹_›) : (⋃ i ∈ S, s i ‹_›) =ᶠ[l] ⋃ i ∈ S, t i ‹_› :=
(eventually_le.countable_bUnion hS (λ i hi, (h i hi).le)).antisymm
(eventually_le.countable_bUnion hS (λ i hi, (h i hi).symm.le))
lemma eventually_le.countable_Inter [encodable ι] {s t : ι → set α} (h : ∀ i, s i ≤ᶠ[l] t i) :
(⋂ i, s i) ≤ᶠ[l] ⋂ i, t i :=
(eventually_countable_forall.2 h).mono $ λ x hst hs, mem_Inter.2 $ λ i, hst _ (mem_Inter.1 hs i)
lemma eventually_eq.countable_Inter [encodable ι] {s t : ι → set α} (h : ∀ i, s i =ᶠ[l] t i) :
(⋂ i, s i) =ᶠ[l] ⋂ i, t i :=
(eventually_le.countable_Inter (λ i, (h i).le)).antisymm
(eventually_le.countable_Inter (λ i, (h i).symm.le))
lemma eventually_le.countable_bInter {S : set ι} (hS : countable S) {s t : Π i ∈ S, set α}
(h : ∀ i ∈ S, s i ‹_› ≤ᶠ[l] t i ‹_›) : (⋂ i ∈ S, s i ‹_›) ≤ᶠ[l] ⋂ i ∈ S, t i ‹_› :=
begin
simp only [bInter_eq_Inter],
haveI := hS.to_encodable,
exact eventually_le.countable_Inter (λ i, h i i.2)
end
lemma eventually_eq.countable_bInter {S : set ι} (hS : countable S) {s t : Π i ∈ S, set α}
(h : ∀ i ∈ S, s i ‹_› =ᶠ[l] t i ‹_›) : (⋂ i ∈ S, s i ‹_›) =ᶠ[l] ⋂ i ∈ S, t i ‹_› :=
(eventually_le.countable_bInter hS (λ i hi, (h i hi).le)).antisymm
(eventually_le.countable_bInter hS (λ i hi, (h i hi).symm.le))
instance countable_Inter_filter_principal (s : set α) : countable_Inter_filter (𝓟 s) :=
⟨λ S hSc hS, subset_sInter hS⟩
instance countable_Inter_filter_bot : countable_Inter_filter (⊥ : filter α) :=
by { rw ← principal_empty, apply countable_Inter_filter_principal }
instance countable_Inter_filter_top : countable_Inter_filter (⊤ : filter α) :=
by { rw ← principal_univ, apply countable_Inter_filter_principal }
/-- Infimum of two `countable_Inter_filter`s is a `countable_Inter_filter`. This is useful, e.g.,
to automatically get an instance for `residual α ⊓ 𝓟 s`. -/
instance countable_Inter_filter_inf (l₁ l₂ : filter α) [countable_Inter_filter l₁]
[countable_Inter_filter l₂] :
countable_Inter_filter (l₁ ⊓ l₂) :=
begin
refine ⟨λ S hSc hS, _⟩,
choose s hs t ht hst using hS,
replace hs : (⋂ i ∈ S, s i ‹_›) ∈ l₁ := (countable_bInter_mem_sets hSc).2 hs,
replace ht : (⋂ i ∈ S, t i ‹_›) ∈ l₂ := (countable_bInter_mem_sets hSc).2 ht,
refine mem_sets_of_superset (inter_mem_inf_sets hs ht) (subset_sInter $ λ i hi, _),
refine subset.trans (inter_subset_inter _ _) (hst i hi);
exact Inter_subset_of_subset i (Inter_subset _ _)
end
/-- Supremum of two `countable_Inter_filter`s is a `countable_Inter_filter`. -/
instance countable_Inter_filter_sup (l₁ l₂ : filter α) [countable_Inter_filter l₁]
[countable_Inter_filter l₂] :
countable_Inter_filter (l₁ ⊔ l₂) :=
begin
refine ⟨λ S hSc hS, ⟨_, _⟩⟩; refine (countable_sInter_mem_sets hSc).2 (λ s hs, _),
exacts [(hS s hs).1, (hS s hs).2]
end
|
6db4e01409fa0bee635ea10d17684158cc217d8e | 4727251e0cd73359b15b664c3170e5d754078599 | /src/group_theory/subgroup/basic.lean | ed53d0e30d245639caa5de77d2535fccf67a0e89 | [
"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 | 128,697 | lean | /-
Copyright (c) 2020 Kexing Ying. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying
-/
import group_theory.submonoid.pointwise
import group_theory.submonoid.membership
import group_theory.submonoid.centralizer
import algebra.group.conj
import algebra.module.basic
import order.atoms
import order.sup_indep
/-!
# Subgroups
This file defines multiplicative and additive subgroups as an extension of submonoids, in a bundled
form (unbundled subgroups are in `deprecated/subgroups.lean`).
We prove subgroups of a group form a complete lattice, and results about images and preimages of
subgroups under group homomorphisms. The bundled subgroups use bundled monoid homomorphisms.
There are also theorems about the subgroups generated by an element or a subset of a group,
defined both inductively and as the infimum of the set of subgroups containing a given
element/subset.
Special thanks goes to Amelia Livingston and Yury Kudryashov for their help and inspiration.
## Main definitions
Notation used here:
- `G N` are `group`s
- `A` is an `add_group`
- `H K` are `subgroup`s of `G` or `add_subgroup`s of `A`
- `x` is an element of type `G` or type `A`
- `f g : N →* G` are group homomorphisms
- `s k` are sets of elements of type `G`
Definitions in the file:
* `subgroup G` : the type of subgroups of a group `G`
* `add_subgroup A` : the type of subgroups of an additive group `A`
* `complete_lattice (subgroup G)` : the subgroups of `G` form a complete lattice
* `subgroup.closure k` : the minimal subgroup that includes the set `k`
* `subgroup.subtype` : the natural group homomorphism from a subgroup of group `G` to `G`
* `subgroup.gi` : `closure` forms a Galois insertion with the coercion to set
* `subgroup.comap H f` : the preimage of a subgroup `H` along the group homomorphism `f` is also a
subgroup
* `subgroup.map f H` : the image of a subgroup `H` along the group homomorphism `f` is also a
subgroup
* `subgroup.prod H K` : the product of subgroups `H`, `K` of groups `G`, `N` respectively, `H × K`
is a subgroup of `G × N`
* `monoid_hom.range f` : the range of the group homomorphism `f` is a subgroup
* `monoid_hom.ker f` : the kernel of a group homomorphism `f` is the subgroup of elements `x : G`
such that `f x = 1`
* `monoid_hom.eq_locus f g` : given group homomorphisms `f`, `g`, the elements of `G` such that
`f x = g x` form a subgroup of `G`
* `is_simple_group G` : a class indicating that a group has exactly two normal subgroups
## Implementation notes
Subgroup inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as
membership of a subgroup's underlying set.
## Tags
subgroup, subgroups
-/
open_locale big_operators pointwise
variables {G : Type*} [group G]
variables {A : Type*} [add_group A]
section subgroup_class
/-- `inv_mem_class S G` states `S` is a type of subsets `s ⊆ G` closed under inverses. -/
class inv_mem_class (S G : Type*) [has_inv G] [set_like S G] :=
(inv_mem : ∀ {s : S} {x}, x ∈ s → x⁻¹ ∈ s)
export inv_mem_class (inv_mem)
/-- `neg_mem_class S G` states `S` is a type of subsets `s ⊆ G` closed under negation. -/
class neg_mem_class (S G : Type*) [has_neg G] [set_like S G] :=
(neg_mem : ∀ {s : S} {x}, x ∈ s → -x ∈ s)
export neg_mem_class (neg_mem)
/-- `subgroup_class S G` states `S` is a type of subsets `s ⊆ G` that are subgroups of `G`. -/
class subgroup_class (S G : Type*) [div_inv_monoid G] [set_like S G]
extends submonoid_class S G :=
(inv_mem : ∀ {s : S} {x}, x ∈ s → x⁻¹ ∈ s)
/-- `add_subgroup_class S G` states `S` is a type of subsets `s ⊆ G` that are
additive subgroups of `G`. -/
class add_subgroup_class (S G : Type*) [sub_neg_monoid G] [set_like S G]
extends add_submonoid_class S G :=
(neg_mem : ∀ {s : S} {x}, x ∈ s → -x ∈ s)
attribute [to_additive] inv_mem_class subgroup_class
variables (M S : Type*) [div_inv_monoid M] [set_like S M] [hSM : subgroup_class S M]
include hSM
@[to_additive, priority 100] -- See note [lower instance priority]
instance subgroup_class.to_inv_mem_class : inv_mem_class S M :=
{ .. hSM }
variables {S M} {H K : S}
/-- A subgroup is closed under division. -/
@[to_additive "An additive subgroup is closed under subtraction."]
theorem div_mem {x y : M} (hx : x ∈ H) (hy : y ∈ H) : x / y ∈ H :=
by rw [div_eq_mul_inv]; exact mul_mem hx (inv_mem hy)
@[to_additive]
lemma zpow_mem {x : M} (hx : x ∈ K) : ∀ n : ℤ, x ^ n ∈ K
| (n : ℕ) := by { rw [zpow_coe_nat], exact pow_mem hx n }
| -[1+ n] := by { rw [zpow_neg_succ_of_nat], exact inv_mem (pow_mem hx n.succ) }
omit hSM
variables [set_like S G] [hSG : subgroup_class S G]
include hSG
@[simp, to_additive] theorem inv_mem_iff {x : G} : x⁻¹ ∈ H ↔ x ∈ H :=
⟨λ h, inv_inv x ▸ inv_mem h, inv_mem⟩
@[to_additive] lemma div_mem_comm_iff {a b : G} : a / b ∈ H ↔ b / a ∈ H :=
by rw [← inv_mem_iff, div_eq_mul_inv, div_eq_mul_inv, mul_inv_rev, inv_inv]
@[simp, to_additive]
theorem inv_coe_set : (H : set G)⁻¹ = H :=
by { ext, simp }
@[simp, to_additive]
lemma exists_inv_mem_iff_exists_mem {P : G → Prop} :
(∃ (x : G), x ∈ H ∧ P x⁻¹) ↔ ∃ x ∈ H, P x :=
by split; { rintros ⟨x, x_in, hx⟩, exact ⟨x⁻¹, inv_mem x_in, by simp [hx]⟩ }
@[to_additive]
lemma mul_mem_cancel_right {x y : G} (h : x ∈ H) : y * x ∈ H ↔ y ∈ H :=
⟨λ hba, by simpa using mul_mem hba (inv_mem h), λ hb, mul_mem hb h⟩
@[to_additive]
lemma mul_mem_cancel_left {x y : G} (h : x ∈ H) : x * y ∈ H ↔ y ∈ H :=
⟨λ hab, by simpa using mul_mem (inv_mem h) hab, mul_mem h⟩
namespace subgroup_class
omit hSG
include hSM
/-- A subgroup of a group inherits an inverse. -/
@[to_additive "An additive subgroup of a `add_group` inherits an inverse."]
instance has_inv : has_inv H := ⟨λ a, ⟨a⁻¹, inv_mem a.2⟩⟩
/-- A subgroup of a group inherits a division -/
@[to_additive "An additive subgroup of an `add_group` inherits a subtraction."]
instance has_div : has_div H := ⟨λ a b, ⟨a / b, div_mem a.2 b.2⟩⟩
omit hSM
/-- An additive subgroup of an `add_group` inherits an integer scaling. -/
instance _root_.add_subgroup_class.has_zsmul {M S} [sub_neg_monoid M] [set_like S M]
[add_subgroup_class S M] {H : S} : has_scalar ℤ H :=
⟨λ n a, ⟨n • a, zsmul_mem a.2 n⟩⟩
include hSM
/-- A subgroup of a group inherits an integer power. -/
@[to_additive]
instance has_zpow : has_pow H ℤ := ⟨λ a n, ⟨a ^ n, zpow_mem a.2 n⟩⟩
@[simp, norm_cast, to_additive] lemma coe_inv (x : H) : ↑(x⁻¹ : H) = (x⁻¹ : M) := rfl
@[simp, norm_cast, to_additive] lemma coe_div (x y : H) : (↑(x / y) : M) = ↑x / ↑y := rfl
omit hSM
variables (H)
include hSG
/-- A subgroup of a group inherits a group structure. -/
@[to_additive "An additive subgroup of an `add_group` inherits an `add_group` structure.",
priority 75] -- Prefer subclasses of `group` over subclasses of `subgroup_class`.
instance to_group : group H :=
subtype.coe_injective.group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
omit hSG
/-- A subgroup of a `comm_group` is a `comm_group`. -/
@[to_additive "An additive subgroup of an `add_comm_group` is an `add_comm_group`.",
priority 75] -- Prefer subclasses of `comm_group` over subclasses of `subgroup_class`.
instance to_comm_group {G : Type*} [comm_group G] [set_like S G] [subgroup_class S G] :
comm_group H :=
subtype.coe_injective.comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
(λ _ _, rfl)
/-- A subgroup of an `ordered_comm_group` is an `ordered_comm_group`. -/
@[to_additive "An additive subgroup of an `add_ordered_comm_group` is an `add_ordered_comm_group`.",
priority 75] -- Prefer subclasses of `group` over subclasses of `subgroup_class`.
instance to_ordered_comm_group {G : Type*} [ordered_comm_group G] [set_like S G]
[subgroup_class S G] : ordered_comm_group H :=
subtype.coe_injective.ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
(λ _ _, rfl)
/-- A subgroup of a `linear_ordered_comm_group` is a `linear_ordered_comm_group`. -/
@[to_additive "An additive subgroup of a `linear_ordered_add_comm_group` is a
`linear_ordered_add_comm_group`.",
priority 75] -- Prefer subclasses of `group` over subclasses of `subgroup_class`.
instance to_linear_ordered_comm_group {G : Type*} [linear_ordered_comm_group G] [set_like S G]
[subgroup_class S G] : linear_ordered_comm_group H :=
subtype.coe_injective.linear_ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl)
(λ _ _, rfl) (λ _ _, rfl)
include hSG
/-- The natural group hom from a subgroup of group `G` to `G`. -/
@[to_additive "The natural group hom from an additive subgroup of `add_group` `G` to `G`."]
def subtype : H →* G := ⟨coe, rfl, λ _ _, rfl⟩
@[simp, to_additive] theorem coe_subtype : (subtype H : H → G) = coe := rfl
variables {H}
@[simp, norm_cast, to_additive coe_smul]
lemma coe_pow (x : H) (n : ℕ) : ((x ^ n : H) : G) = x ^ n :=
(subtype H : H →* G).map_pow _ _
@[simp, norm_cast, to_additive] lemma coe_zpow (x : H) (n : ℤ) : ((x ^ n : H) : G) = x ^ n :=
(subtype H : H →* G).map_zpow _ _
/-- The inclusion homomorphism from a subgroup `H` contained in `K` to `K`. -/
@[to_additive "The inclusion homomorphism from a additive subgroup `H` contained in `K` to `K`."]
def inclusion {H K : S} (h : H ≤ K) : H →* K :=
monoid_hom.mk' (λ x, ⟨x, h x.prop⟩) (λ ⟨a, ha⟩ ⟨b, hb⟩, rfl)
@[simp, to_additive] lemma inclusion_self (x : H) : inclusion le_rfl x = x := by { cases x, refl }
@[simp, to_additive] lemma inclusion_mk {h : H ≤ K} (x : G) (hx : x ∈ H) :
inclusion h ⟨x, hx⟩ = ⟨x, h hx⟩ := rfl
@[to_additive]
lemma inclusion_right (h : H ≤ K) (x : K) (hx : (x : G) ∈ H) : inclusion h ⟨x, hx⟩ = x :=
by { cases x, refl }
@[simp] lemma inclusion_inclusion {L : S} (hHK : H ≤ K) (hKL : K ≤ L) (x : H) :
inclusion hKL (inclusion hHK x) = inclusion (hHK.trans hKL) x :=
by { cases x, refl }
@[simp, to_additive]
lemma coe_inclusion {H K : S} {h : H ≤ K} (a : H) : (inclusion h a : G) = a :=
by { cases a, simp only [inclusion, set_like.coe_mk, monoid_hom.mk'_apply] }
@[simp, to_additive]
lemma subtype_comp_inclusion {H K : S} (hH : H ≤ K) :
(subtype K).comp (inclusion hH) = subtype H :=
by { ext, simp only [monoid_hom.comp_apply, coe_subtype, coe_inclusion] }
end subgroup_class
end subgroup_class
set_option old_structure_cmd true
/-- A subgroup of a group `G` is a subset containing 1, closed under multiplication
and closed under multiplicative inverse. -/
structure subgroup (G : Type*) [group G] extends submonoid G :=
(inv_mem' {x} : x ∈ carrier → x⁻¹ ∈ carrier)
/-- An additive subgroup of an additive group `G` is a subset containing 0, closed
under addition and additive inverse. -/
structure add_subgroup (G : Type*) [add_group G] extends add_submonoid G:=
(neg_mem' {x} : x ∈ carrier → -x ∈ carrier)
attribute [to_additive] subgroup
attribute [to_additive add_subgroup.to_add_submonoid] subgroup.to_submonoid
/-- Reinterpret a `subgroup` as a `submonoid`. -/
add_decl_doc subgroup.to_submonoid
/-- Reinterpret an `add_subgroup` as an `add_submonoid`. -/
add_decl_doc add_subgroup.to_add_submonoid
namespace subgroup
@[to_additive]
instance : set_like (subgroup G) G :=
{ coe := subgroup.carrier,
coe_injective' := λ p q h, by cases p; cases q; congr' }
@[to_additive]
instance : subgroup_class (subgroup G) G :=
{ mul_mem := subgroup.mul_mem',
one_mem := subgroup.one_mem',
inv_mem := subgroup.inv_mem' }
@[simp, to_additive]
lemma mem_carrier {s : subgroup G} {x : G} : x ∈ s.carrier ↔ x ∈ s := iff.rfl
@[simp, to_additive]
lemma mem_mk {s : set G} {x : G} (h_one) (h_mul) (h_inv) :
x ∈ mk s h_one h_mul h_inv ↔ x ∈ s := iff.rfl
@[simp, to_additive]
lemma coe_set_mk {s : set G} (h_one) (h_mul) (h_inv) :
(mk s h_one h_mul h_inv : set G) = s := rfl
@[simp, to_additive]
lemma mk_le_mk {s t : set G} (h_one) (h_mul) (h_inv) (h_one') (h_mul') (h_inv') :
mk s h_one h_mul h_inv ≤ mk t h_one' h_mul' h_inv' ↔ s ⊆ t := iff.rfl
/-- See Note [custom simps projection] -/
@[to_additive "See Note [custom simps projection]"]
def simps.coe (S : subgroup G) : set G := S
initialize_simps_projections subgroup (carrier → coe)
initialize_simps_projections add_subgroup (carrier → coe)
@[simp, to_additive]
lemma coe_to_submonoid (K : subgroup G) : (K.to_submonoid : set G) = K := rfl
@[simp, to_additive]
lemma mem_to_submonoid (K : subgroup G) (x : G) : x ∈ K.to_submonoid ↔ x ∈ K := iff.rfl
@[to_additive]
instance (K : subgroup G) [d : decidable_pred (∈ K)] [fintype G] : fintype K :=
show fintype {g : G // g ∈ K}, from infer_instance
@[to_additive]
theorem to_submonoid_injective :
function.injective (to_submonoid : subgroup G → submonoid G) :=
λ p q h, set_like.ext'_iff.2 (show _, from set_like.ext'_iff.1 h)
@[simp, to_additive]
theorem to_submonoid_eq {p q : subgroup G} : p.to_submonoid = q.to_submonoid ↔ p = q :=
to_submonoid_injective.eq_iff
@[to_additive, mono] lemma to_submonoid_strict_mono :
strict_mono (to_submonoid : subgroup G → submonoid G) := λ _ _, id
attribute [mono] add_subgroup.to_add_submonoid_strict_mono
@[to_additive, mono]
lemma to_submonoid_mono : monotone (to_submonoid : subgroup G → submonoid G) :=
to_submonoid_strict_mono.monotone
attribute [mono] add_subgroup.to_add_submonoid_mono
@[simp, to_additive]
lemma to_submonoid_le {p q : subgroup G} : p.to_submonoid ≤ q.to_submonoid ↔ p ≤ q :=
iff.rfl
end subgroup
/-!
### Conversion to/from `additive`/`multiplicative`
-/
section mul_add
/-- Supgroups of a group `G` are isomorphic to additive subgroups of `additive G`. -/
@[simps]
def subgroup.to_add_subgroup : subgroup G ≃o add_subgroup (additive G) :=
{ to_fun := λ S,
{ neg_mem' := S.inv_mem',
..S.to_submonoid.to_add_submonoid },
inv_fun := λ S,
{ inv_mem' := S.neg_mem',
..S.to_add_submonoid.to_submonoid' },
left_inv := λ x, by cases x; refl,
right_inv := λ x, by cases x; refl,
map_rel_iff' := λ a b, iff.rfl, }
/-- Additive subgroup of an additive group `additive G` are isomorphic to subgroup of `G`. -/
abbreviation add_subgroup.to_subgroup' : add_subgroup (additive G) ≃o subgroup G :=
subgroup.to_add_subgroup.symm
/-- Additive supgroups of an additive group `A` are isomorphic to subgroups of `multiplicative A`.
-/
@[simps]
def add_subgroup.to_subgroup : add_subgroup A ≃o subgroup (multiplicative A) :=
{ to_fun := λ S,
{ inv_mem' := S.neg_mem',
..S.to_add_submonoid.to_submonoid },
inv_fun := λ S,
{ neg_mem' := S.inv_mem',
..S.to_submonoid.to_add_submonoid' },
left_inv := λ x, by cases x; refl,
right_inv := λ x, by cases x; refl,
map_rel_iff' := λ a b, iff.rfl, }
/-- Subgroups of an additive group `multiplicative A` are isomorphic to additive subgroups of `A`.
-/
abbreviation subgroup.to_add_subgroup' : subgroup (multiplicative A) ≃o add_subgroup A :=
add_subgroup.to_subgroup.symm
end mul_add
namespace subgroup
variables (H K : subgroup G)
/-- Copy of a subgroup with a new `carrier` equal to the old one. Useful to fix definitional
equalities.-/
@[to_additive "Copy of an additive subgroup with a new `carrier` equal to the old one.
Useful to fix definitional equalities"]
protected def copy (K : subgroup G) (s : set G) (hs : s = K) : subgroup G :=
{ carrier := s,
one_mem' := hs.symm ▸ K.one_mem',
mul_mem' := hs.symm ▸ K.mul_mem',
inv_mem' := hs.symm ▸ K.inv_mem' }
@[simp, to_additive] lemma coe_copy (K : subgroup G) (s : set G) (hs : s = ↑K) :
(K.copy s hs : set G) = s := rfl
@[to_additive]
lemma copy_eq (K : subgroup G) (s : set G) (hs : s = ↑K) : K.copy s hs = K :=
set_like.coe_injective hs
/-- Two subgroups are equal if they have the same elements. -/
@[ext, to_additive "Two `add_subgroup`s are equal if they have the same elements."]
theorem ext {H K : subgroup G} (h : ∀ x, x ∈ H ↔ x ∈ K) : H = K := set_like.ext h
/-- A subgroup contains the group's 1. -/
@[to_additive "An `add_subgroup` contains the group's 0."]
protected theorem one_mem : (1 : G) ∈ H := one_mem _
/-- A subgroup is closed under multiplication. -/
@[to_additive "An `add_subgroup` is closed under addition."]
protected theorem mul_mem {x y : G} : x ∈ H → y ∈ H → x * y ∈ H := mul_mem
/-- A subgroup is closed under inverse. -/
@[to_additive "An `add_subgroup` is closed under inverse."]
protected theorem inv_mem {x : G} : x ∈ H → x⁻¹ ∈ H := inv_mem
/-- A subgroup is closed under division. -/
@[to_additive "An `add_subgroup` is closed under subtraction."]
protected theorem div_mem {x y : G} (hx : x ∈ H) (hy : y ∈ H) : x / y ∈ H := div_mem hx hy
@[to_additive] protected theorem inv_mem_iff {x : G} : x⁻¹ ∈ H ↔ x ∈ H := inv_mem_iff
@[to_additive] protected lemma div_mem_comm_iff {a b : G} : a / b ∈ H ↔ b / a ∈ H :=
div_mem_comm_iff
@[to_additive] protected theorem inv_coe_set : (H : set G)⁻¹ = H := by { ext, simp }
@[to_additive] protected lemma exists_inv_mem_iff_exists_mem (K : subgroup G) {P : G → Prop} :
(∃ (x : G), x ∈ K ∧ P x⁻¹) ↔ ∃ x ∈ K, P x :=
exists_inv_mem_iff_exists_mem
@[to_additive] protected lemma mul_mem_cancel_right {x y : G} (h : x ∈ H) : y * x ∈ H ↔ y ∈ H :=
mul_mem_cancel_right h
@[to_additive] protected lemma mul_mem_cancel_left {x y : G} (h : x ∈ H) : x * y ∈ H ↔ y ∈ H :=
mul_mem_cancel_left h
/-- Product of a list of elements in a subgroup is in the subgroup. -/
@[to_additive "Sum of a list of elements in an `add_subgroup` is in the `add_subgroup`."]
protected lemma list_prod_mem {l : list G} : (∀ x ∈ l, x ∈ K) → l.prod ∈ K :=
list_prod_mem
/-- Product of a multiset of elements in a subgroup of a `comm_group` is in the subgroup. -/
@[to_additive "Sum of a multiset of elements in an `add_subgroup` of an `add_comm_group`
is in the `add_subgroup`."]
protected lemma multiset_prod_mem {G} [comm_group G] (K : subgroup G) (g : multiset G) :
(∀ a ∈ g, a ∈ K) → g.prod ∈ K := multiset_prod_mem g
@[to_additive]
lemma multiset_noncomm_prod_mem (K : subgroup G) (g : multiset G)
(comm : ∀ (x ∈ g) (y ∈ g), commute x y) :
(∀ a ∈ g, a ∈ K) → g.noncomm_prod comm ∈ K :=
K.to_submonoid.multiset_noncomm_prod_mem g comm
/-- Product of elements of a subgroup of a `comm_group` indexed by a `finset` is in the
subgroup. -/
@[to_additive "Sum of elements in an `add_subgroup` of an `add_comm_group` indexed by a `finset`
is in the `add_subgroup`."]
protected lemma prod_mem {G : Type*} [comm_group G] (K : subgroup G)
{ι : Type*} {t : finset ι} {f : ι → G} (h : ∀ c ∈ t, f c ∈ K) :
∏ c in t, f c ∈ K :=
prod_mem h
@[to_additive]
lemma noncomm_prod_mem (K : subgroup G)
{ι : Type*} {t : finset ι} {f : ι → G} (comm : ∀ (x ∈ t) (y ∈ t), commute (f x) (f y)) :
(∀ c ∈ t, f c ∈ K) → t.noncomm_prod f comm ∈ K :=
K.to_submonoid.noncomm_prod_mem t f comm
@[to_additive add_subgroup.nsmul_mem]
protected lemma pow_mem {x : G} (hx : x ∈ K) : ∀ n : ℕ, x ^ n ∈ K := pow_mem hx
@[to_additive]
protected lemma zpow_mem {x : G} (hx : x ∈ K) : ∀ n : ℤ, x ^ n ∈ K := zpow_mem hx
/-- Construct a subgroup from a nonempty set that is closed under division. -/
@[to_additive "Construct a subgroup from a nonempty set that is closed under subtraction"]
def of_div (s : set G) (hsn : s.nonempty) (hs : ∀ x y ∈ s, x * y⁻¹ ∈ s) : subgroup G :=
have one_mem : (1 : G) ∈ s, from let ⟨x, hx⟩ := hsn in by simpa using hs x hx x hx,
have inv_mem : ∀ x, x ∈ s → x⁻¹ ∈ s, from λ x hx, by simpa using hs 1 one_mem x hx,
{ carrier := s,
one_mem' := one_mem,
inv_mem' := inv_mem,
mul_mem' := λ x y hx hy, by simpa using hs x hx y⁻¹ (inv_mem y hy) }
/-- A subgroup of a group inherits a multiplication. -/
@[to_additive "An `add_subgroup` of an `add_group` inherits an addition."]
instance has_mul : has_mul H := H.to_submonoid.has_mul
/-- A subgroup of a group inherits a 1. -/
@[to_additive "An `add_subgroup` of an `add_group` inherits a zero."]
instance has_one : has_one H := H.to_submonoid.has_one
/-- A subgroup of a group inherits an inverse. -/
@[to_additive "A `add_subgroup` of a `add_group` inherits an inverse."]
instance has_inv : has_inv H := ⟨λ a, ⟨a⁻¹, H.inv_mem a.2⟩⟩
/-- A subgroup of a group inherits a division -/
@[to_additive "An `add_subgroup` of an `add_group` inherits a subtraction."]
instance has_div : has_div H := ⟨λ a b, ⟨a / b, H.div_mem a.2 b.2⟩⟩
/-- An `add_subgroup` of an `add_group` inherits a natural scaling. -/
instance _root_.add_subgroup.has_nsmul {G} [add_group G] {H : add_subgroup G} : has_scalar ℕ H :=
⟨λ n a, ⟨n • a, H.nsmul_mem a.2 n⟩⟩
/-- A subgroup of a group inherits a natural power -/
@[to_additive]
instance has_npow : has_pow H ℕ := ⟨λ a n, ⟨a ^ n, H.pow_mem a.2 n⟩⟩
/-- An `add_subgroup` of an `add_group` inherits an integer scaling. -/
instance _root_.add_subgroup.has_zsmul {G} [add_group G] {H : add_subgroup G} : has_scalar ℤ H :=
⟨λ n a, ⟨n • a, H.zsmul_mem a.2 n⟩⟩
/-- A subgroup of a group inherits an integer power -/
@[to_additive]
instance has_zpow : has_pow H ℤ := ⟨λ a n, ⟨a ^ n, H.zpow_mem a.2 n⟩⟩
@[simp, norm_cast, to_additive] lemma coe_mul (x y : H) : (↑(x * y) : G) = ↑x * ↑y := rfl
@[simp, norm_cast, to_additive] lemma coe_one : ((1 : H) : G) = 1 := rfl
@[simp, norm_cast, to_additive] lemma coe_inv (x : H) : ↑(x⁻¹ : H) = (x⁻¹ : G) := rfl
@[simp, norm_cast, to_additive] lemma coe_div (x y : H) : (↑(x / y) : G) = ↑x / ↑y := rfl
@[simp, norm_cast, to_additive] lemma coe_mk (x : G) (hx : x ∈ H) : ((⟨x, hx⟩ : H) : G) = x := rfl
@[simp, norm_cast, to_additive] lemma coe_pow (x : H) (n : ℕ) : ((x ^ n : H) : G) = x ^ n := rfl
@[simp, norm_cast, to_additive] lemma coe_zpow (x : H) (n : ℤ) : ((x ^ n : H) : G) = x ^ n := rfl
/-- A subgroup of a group inherits a group structure. -/
@[to_additive "An `add_subgroup` of an `add_group` inherits an `add_group` structure."]
instance to_group {G : Type*} [group G] (H : subgroup G) : group H :=
subtype.coe_injective.group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
/-- A subgroup of a `comm_group` is a `comm_group`. -/
@[to_additive "An `add_subgroup` of an `add_comm_group` is an `add_comm_group`."]
instance to_comm_group {G : Type*} [comm_group G] (H : subgroup G) : comm_group H :=
subtype.coe_injective.comm_group _
rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
/-- A subgroup of an `ordered_comm_group` is an `ordered_comm_group`. -/
@[to_additive "An `add_subgroup` of an `add_ordered_comm_group` is an `add_ordered_comm_group`."]
instance to_ordered_comm_group {G : Type*} [ordered_comm_group G] (H : subgroup G) :
ordered_comm_group H :=
subtype.coe_injective.ordered_comm_group _
rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
/-- A subgroup of a `linear_ordered_comm_group` is a `linear_ordered_comm_group`. -/
@[to_additive "An `add_subgroup` of a `linear_ordered_add_comm_group` is a
`linear_ordered_add_comm_group`."]
instance to_linear_ordered_comm_group {G : Type*} [linear_ordered_comm_group G]
(H : subgroup G) : linear_ordered_comm_group H :=
subtype.coe_injective.linear_ordered_comm_group _
rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
/-- The natural group hom from a subgroup of group `G` to `G`. -/
@[to_additive "The natural group hom from an `add_subgroup` of `add_group` `G` to `G`."]
def subtype : H →* G := ⟨coe, rfl, λ _ _, rfl⟩
@[simp, to_additive] theorem coe_subtype : ⇑H.subtype = coe := rfl
@[simp, norm_cast, to_additive] theorem coe_list_prod (l : list H) :
(l.prod : G) = (l.map coe).prod :=
submonoid_class.coe_list_prod l
@[simp, norm_cast, to_additive] theorem coe_multiset_prod {G} [comm_group G] (H : subgroup G)
(m : multiset H) : (m.prod : G) = (m.map coe).prod :=
submonoid_class.coe_multiset_prod m
@[simp, norm_cast, to_additive] theorem coe_finset_prod {ι G} [comm_group G] (H : subgroup G)
(f : ι → H) (s : finset ι) :
↑(∏ i in s, f i) = (∏ i in s, f i : G) :=
submonoid_class.coe_finset_prod f s
/-- The inclusion homomorphism from a subgroup `H` contained in `K` to `K`. -/
@[to_additive "The inclusion homomorphism from a additive subgroup `H` contained in `K` to `K`."]
def inclusion {H K : subgroup G} (h : H ≤ K) : H →* K :=
monoid_hom.mk' (λ x, ⟨x, h x.prop⟩) (λ ⟨a, ha⟩ ⟨b, hb⟩, rfl)
@[simp, to_additive]
lemma coe_inclusion {H K : subgroup G} {h : H ≤ K} (a : H) : (inclusion h a : G) = a :=
by { cases a, simp only [inclusion, coe_mk, monoid_hom.mk'_apply] }
@[to_additive] lemma inclusion_injective {H K : subgroup G} (h : H ≤ K) :
function.injective $ inclusion h :=
set.inclusion_injective h
@[simp, to_additive]
lemma subtype_comp_inclusion {H K : subgroup G} (hH : H ≤ K) :
K.subtype.comp (inclusion hH) = H.subtype :=
by { ext, simp }
/-- The subgroup `G` of the group `G`. -/
@[to_additive "The `add_subgroup G` of the `add_group G`."]
instance : has_top (subgroup G) :=
⟨{ inv_mem' := λ _ _, set.mem_univ _ , .. (⊤ : submonoid G) }⟩
/-- The top subgroup is isomorphic to the group.
This is the group version of `submonoid.top_equiv`. -/
@[to_additive "The top additive subgroup is isomorphic to the additive group.
This is the additive group version of `add_submonoid.top_equiv`.", simps]
def top_equiv : (⊤ : subgroup G) ≃* G := submonoid.top_equiv
/-- The trivial subgroup `{1}` of an group `G`. -/
@[to_additive "The trivial `add_subgroup` `{0}` of an `add_group` `G`."]
instance : has_bot (subgroup G) :=
⟨{ inv_mem' := λ _, by simp *, .. (⊥ : submonoid G) }⟩
@[to_additive]
instance : inhabited (subgroup G) := ⟨⊥⟩
@[simp, to_additive] lemma mem_bot {x : G} : x ∈ (⊥ : subgroup G) ↔ x = 1 := iff.rfl
@[simp, to_additive] lemma mem_top (x : G) : x ∈ (⊤ : subgroup G) := set.mem_univ x
@[simp, to_additive] lemma coe_top : ((⊤ : subgroup G) : set G) = set.univ := rfl
@[simp, to_additive] lemma coe_bot : ((⊥ : subgroup G) : set G) = {1} := rfl
@[to_additive] instance : unique (⊥ : subgroup G) := ⟨⟨1⟩, λ g, subtype.ext g.2⟩
@[to_additive] lemma eq_bot_iff_forall : H = ⊥ ↔ ∀ x ∈ H, x = (1 : G) :=
begin
rw set_like.ext'_iff,
simp only [coe_bot, set.eq_singleton_iff_unique_mem, set_like.mem_coe, H.one_mem, true_and],
end
@[to_additive] lemma eq_bot_of_subsingleton [subsingleton H] : H = ⊥ :=
begin
rw subgroup.eq_bot_iff_forall,
intros y hy,
rw [← subgroup.coe_mk H y hy, subsingleton.elim (⟨y, hy⟩ : H) 1, subgroup.coe_one],
end
@[to_additive] lemma coe_eq_univ {H : subgroup G} : (H : set G) = set.univ ↔ H = ⊤ :=
(set_like.ext'_iff.trans (by refl)).symm
@[to_additive] lemma coe_eq_singleton {H : subgroup G} : (∃ g : G, (H : set G) = {g}) ↔ H = ⊥ :=
⟨λ ⟨g, hg⟩, by { haveI : subsingleton (H : set G) := by { rw hg, apply_instance },
exact H.eq_bot_of_subsingleton }, λ h, ⟨1, set_like.ext'_iff.mp h⟩⟩
@[to_additive] instance fintype_bot : fintype (⊥ : subgroup G) := ⟨{1},
by {rintro ⟨x, ⟨hx⟩⟩, exact finset.mem_singleton_self _}⟩
/- curly brackets `{}` are used here instead of instance brackets `[]` because
the instance in a goal is often not the same as the one inferred by type class inference. -/
@[simp, to_additive] lemma card_bot {_ : fintype ↥(⊥ : subgroup G)} :
fintype.card (⊥ : subgroup G) = 1 :=
fintype.card_eq_one_iff.2
⟨⟨(1 : G), set.mem_singleton 1⟩, λ ⟨y, hy⟩, subtype.eq $ subgroup.mem_bot.1 hy⟩
@[to_additive] lemma eq_top_of_card_eq [fintype H] [fintype G]
(h : fintype.card H = fintype.card G) : H = ⊤ :=
begin
haveI : fintype (H : set G) := ‹fintype H›,
rw [set_like.ext'_iff, coe_top, ← finset.coe_univ, ← (H : set G).coe_to_finset, finset.coe_inj,
← finset.card_eq_iff_eq_univ, ← h, set.to_finset_card],
congr
end
@[to_additive] lemma eq_top_of_le_card [fintype H] [fintype G]
(h : fintype.card G ≤ fintype.card H) : H = ⊤ :=
eq_top_of_card_eq H (le_antisymm (fintype.card_le_of_injective coe subtype.coe_injective) h)
@[to_additive] lemma eq_bot_of_card_le [fintype H] (h : fintype.card H ≤ 1) : H = ⊥ :=
let _ := fintype.card_le_one_iff_subsingleton.mp h in by exactI eq_bot_of_subsingleton H
@[to_additive] lemma eq_bot_of_card_eq [fintype H] (h : fintype.card H = 1) : H = ⊥ :=
H.eq_bot_of_card_le (le_of_eq h)
@[to_additive] lemma nontrivial_iff_exists_ne_one (H : subgroup G) :
nontrivial H ↔ ∃ x ∈ H, x ≠ (1:G) :=
subtype.nontrivial_iff_exists_ne (λ x, x ∈ H) (1 : H)
/-- A subgroup is either the trivial subgroup or nontrivial. -/
@[to_additive] lemma bot_or_nontrivial (H : subgroup G) : H = ⊥ ∨ nontrivial H :=
begin
classical,
by_cases h : ∀ x ∈ H, x = (1 : G),
{ left,
exact H.eq_bot_iff_forall.mpr h },
{ right,
simp only [not_forall] at h,
simpa only [nontrivial_iff_exists_ne_one] }
end
/-- A subgroup is either the trivial subgroup or contains a nonzero element. -/
@[to_additive] lemma bot_or_exists_ne_one (H : subgroup G) : H = ⊥ ∨ ∃ x ∈ H, x ≠ (1:G) :=
begin
convert H.bot_or_nontrivial,
rw nontrivial_iff_exists_ne_one
end
@[to_additive] lemma card_le_one_iff_eq_bot [fintype H] : fintype.card H ≤ 1 ↔ H = ⊥ :=
⟨λ h, (eq_bot_iff_forall _).2
(λ x hx, by simpa [subtype.ext_iff] using fintype.card_le_one_iff.1 h ⟨x, hx⟩ 1),
λ h, by simp [h]⟩
@[to_additive] lemma one_lt_card_iff_ne_bot [fintype H] : 1 < fintype.card H ↔ H ≠ ⊥ :=
lt_iff_not_le.trans H.card_le_one_iff_eq_bot.not
/-- The inf of two subgroups is their intersection. -/
@[to_additive "The inf of two `add_subgroups`s is their intersection."]
instance : has_inf (subgroup G) :=
⟨λ H₁ H₂,
{ inv_mem' := λ _ ⟨hx, hx'⟩, ⟨H₁.inv_mem hx, H₂.inv_mem hx'⟩,
.. H₁.to_submonoid ⊓ H₂.to_submonoid }⟩
@[simp, to_additive]
lemma coe_inf (p p' : subgroup G) : ((p ⊓ p' : subgroup G) : set G) = p ∩ p' := rfl
@[simp, to_additive]
lemma mem_inf {p p' : subgroup G} {x : G} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl
@[to_additive]
instance : has_Inf (subgroup G) :=
⟨λ s,
{ inv_mem' := λ x hx, set.mem_bInter $ λ i h, i.inv_mem (by apply set.mem_Inter₂.1 hx i h),
.. (⨅ S ∈ s, subgroup.to_submonoid S).copy (⋂ S ∈ s, ↑S) (by simp) }⟩
@[simp, norm_cast, to_additive]
lemma coe_Inf (H : set (subgroup G)) : ((Inf H : subgroup G) : set G) = ⋂ s ∈ H, ↑s := rfl
@[simp, to_additive]
lemma mem_Inf {S : set (subgroup G)} {x : G} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_Inter₂
@[to_additive]
lemma mem_infi {ι : Sort*} {S : ι → subgroup G} {x : G} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i :=
by simp only [infi, mem_Inf, set.forall_range_iff]
@[simp, norm_cast, to_additive]
lemma coe_infi {ι : Sort*} {S : ι → subgroup G} : (↑(⨅ i, S i) : set G) = ⋂ i, S i :=
by simp only [infi, coe_Inf, set.bInter_range]
/-- Subgroups of a group form a complete lattice. -/
@[to_additive "The `add_subgroup`s of an `add_group` form a complete lattice."]
instance : complete_lattice (subgroup G) :=
{ bot := (⊥),
bot_le := λ S x hx, (mem_bot.1 hx).symm ▸ S.one_mem,
top := (⊤),
le_top := λ S x hx, mem_top x,
inf := (⊓),
le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩,
inf_le_left := λ a b x, and.left,
inf_le_right := λ a b x, and.right,
.. complete_lattice_of_Inf (subgroup G) $ λ s, is_glb.of_image
(λ H K, show (H : set G) ≤ K ↔ H ≤ K, from set_like.coe_subset_coe) is_glb_binfi }
@[to_additive]
lemma mem_sup_left {S T : subgroup G} : ∀ {x : G}, x ∈ S → x ∈ S ⊔ T :=
show S ≤ S ⊔ T, from le_sup_left
@[to_additive]
lemma mem_sup_right {S T : subgroup G} : ∀ {x : G}, x ∈ T → x ∈ S ⊔ T :=
show T ≤ S ⊔ T, from le_sup_right
@[to_additive]
lemma mul_mem_sup {S T : subgroup G} {x y : G} (hx : x ∈ S) (hy : y ∈ T) : x * y ∈ S ⊔ T :=
(S ⊔ T).mul_mem (mem_sup_left hx) (mem_sup_right hy)
@[to_additive]
lemma mem_supr_of_mem {ι : Sort*} {S : ι → subgroup G} (i : ι) :
∀ {x : G}, x ∈ S i → x ∈ supr S :=
show S i ≤ supr S, from le_supr _ _
@[to_additive]
lemma mem_Sup_of_mem {S : set (subgroup G)} {s : subgroup G}
(hs : s ∈ S) : ∀ {x : G}, x ∈ s → x ∈ Sup S :=
show s ≤ Sup S, from le_Sup hs
@[simp, to_additive]
lemma subsingleton_iff : subsingleton (subgroup G) ↔ subsingleton G :=
⟨ λ h, by exactI ⟨λ x y,
have ∀ i : G, i = 1 := λ i, mem_bot.mp $ subsingleton.elim (⊤ : subgroup G) ⊥ ▸ mem_top i,
(this x).trans (this y).symm⟩,
λ h, by exactI ⟨λ x y, subgroup.ext $ λ i, subsingleton.elim 1 i ▸ by simp [subgroup.one_mem]⟩⟩
@[simp, to_additive]
lemma nontrivial_iff : nontrivial (subgroup G) ↔ nontrivial G :=
not_iff_not.mp (
(not_nontrivial_iff_subsingleton.trans subsingleton_iff).trans
not_nontrivial_iff_subsingleton.symm)
@[to_additive]
instance [subsingleton G] : unique (subgroup G) :=
⟨⟨⊥⟩, λ a, @subsingleton.elim _ (subsingleton_iff.mpr ‹_›) a _⟩
@[to_additive]
instance [nontrivial G] : nontrivial (subgroup G) := nontrivial_iff.mpr ‹_›
@[to_additive] lemma eq_top_iff' : H = ⊤ ↔ ∀ x : G, x ∈ H :=
eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩
/-- The `subgroup` generated by a set. -/
@[to_additive "The `add_subgroup` generated by a set"]
def closure (k : set G) : subgroup G := Inf {K | k ⊆ K}
variable {k : set G}
@[to_additive]
lemma mem_closure {x : G} : x ∈ closure k ↔ ∀ K : subgroup G, k ⊆ K → x ∈ K :=
mem_Inf
/-- The subgroup generated by a set includes the set. -/
@[simp, to_additive "The `add_subgroup` generated by a set includes the set."]
lemma subset_closure : k ⊆ closure k := λ x hx, mem_closure.2 $ λ K hK, hK hx
@[to_additive]
lemma not_mem_of_not_mem_closure {P : G} (hP : P ∉ closure k) : P ∉ k := λ h, hP (subset_closure h)
open set
/-- A subgroup `K` includes `closure k` if and only if it includes `k`. -/
@[simp, to_additive "An additive subgroup `K` includes `closure k` if and only if it includes `k`"]
lemma closure_le : closure k ≤ K ↔ k ⊆ K :=
⟨subset.trans subset_closure, λ h, Inf_le h⟩
@[to_additive]
lemma closure_eq_of_le (h₁ : k ⊆ K) (h₂ : K ≤ closure k) : closure k = K :=
le_antisymm ((closure_le $ K).2 h₁) h₂
/-- An induction principle for closure membership. If `p` holds for `1` and all elements of `k`, and
is preserved under multiplication and inverse, then `p` holds for all elements of the closure
of `k`. -/
@[elab_as_eliminator, to_additive "An induction principle for additive closure membership. If `p`
holds for `0` and all elements of `k`, and is preserved under addition and inverses, then `p` holds
for all elements of the additive closure of `k`."]
lemma closure_induction {p : G → Prop} {x} (h : x ∈ closure k)
(Hk : ∀ x ∈ k, p x) (H1 : p 1)
(Hmul : ∀ x y, p x → p y → p (x * y))
(Hinv : ∀ x, p x → p x⁻¹) : p x :=
(@closure_le _ _ ⟨p, Hmul, H1, Hinv⟩ _).2 Hk h
/-- A dependent version of `subgroup.closure_induction`. -/
@[elab_as_eliminator, to_additive "A dependent version of `add_subgroup.closure_induction`. "]
lemma closure_induction' {p : Π x, x ∈ closure k → Prop}
(Hs : ∀ x (h : x ∈ k), p x (subset_closure h))
(H1 : p 1 (one_mem _))
(Hmul : ∀ x hx y hy, p x hx → p y hy → p (x * y) (mul_mem hx hy))
(Hinv : ∀ x hx, p x hx → p x⁻¹ (inv_mem hx))
{x} (hx : x ∈ closure k) :
p x hx :=
begin
refine exists.elim _ (λ (hx : x ∈ closure k) (hc : p x hx), hc),
exact closure_induction hx
(λ x hx, ⟨_, Hs x hx⟩) ⟨_, H1⟩ (λ x y ⟨hx', hx⟩ ⟨hy', hy⟩, ⟨_, Hmul _ _ _ _ hx hy⟩)
(λ x ⟨hx', hx⟩, ⟨_, Hinv _ _ hx⟩),
end
/-- An induction principle for closure membership for predicates with two arguments. -/
@[elab_as_eliminator, to_additive "An induction principle for additive closure membership, for
predicates with two arguments."]
lemma closure_induction₂ {p : G → G → Prop} {x} {y : G} (hx : x ∈ closure k) (hy : y ∈ closure k)
(Hk : ∀ (x ∈ k) (y ∈ k), p x y)
(H1_left : ∀ x, p 1 x)
(H1_right : ∀ x, p x 1)
(Hmul_left : ∀ x₁ x₂ y, p x₁ y → p x₂ y → p (x₁ * x₂) y)
(Hmul_right : ∀ x y₁ y₂, p x y₁ → p x y₂ → p x (y₁ * y₂))
(Hinv_left : ∀ x y, p x y → p x⁻¹ y)
(Hinv_right : ∀ x y, p x y → p x y⁻¹) : p x y :=
closure_induction hx
(λ x xk, closure_induction hy (Hk x xk) (H1_right x) (Hmul_right x) (Hinv_right x))
(H1_left y) (λ z z', Hmul_left z z' y) (λ z, Hinv_left z y)
@[simp, to_additive]
lemma closure_closure_coe_preimage {k : set G} : closure ((coe : closure k → G) ⁻¹' k) = ⊤ :=
eq_top_iff.2 $ λ x, subtype.rec_on x $ λ x hx _, begin
refine closure_induction' (λ g hg, _) _ (λ g₁ g₂ hg₁ hg₂, _) (λ g hg, _) hx,
{ exact subset_closure hg },
{ exact one_mem _ },
{ exact mul_mem },
{ exact inv_mem }
end
/-- If all the elements of a set `s` commute, then `closure s` is a commutative group. -/
@[to_additive "If all the elements of a set `s` commute, then `closure s` is an additive
commutative group."]
def closure_comm_group_of_comm {k : set G} (hcomm : ∀ (x ∈ k) (y ∈ k), x * y = y * x) :
comm_group (closure k) :=
{ mul_comm := λ x y,
begin
ext,
simp only [subgroup.coe_mul],
refine closure_induction₂ x.prop y.prop hcomm
(λ x, by simp only [mul_one, one_mul])
(λ x, by simp only [mul_one, one_mul])
(λ x y z h₁ h₂, by rw [mul_assoc, h₂, ←mul_assoc, h₁, mul_assoc])
(λ x y z h₁ h₂, by rw [←mul_assoc, h₁, mul_assoc, h₂, ←mul_assoc])
(λ x y h, by rw [inv_mul_eq_iff_eq_mul, ←mul_assoc, h, mul_assoc, mul_inv_self, mul_one])
(λ x y h, by rw [mul_inv_eq_iff_eq_mul, mul_assoc, h, ←mul_assoc, inv_mul_self, one_mul])
end,
..(closure k).to_group }
variable (G)
/-- `closure` forms a Galois insertion with the coercion to set. -/
@[to_additive "`closure` forms a Galois insertion with the coercion to set."]
protected def gi : galois_insertion (@closure G _) coe :=
{ choice := λ s _, closure s,
gc := λ s t, @closure_le _ _ t s,
le_l_u := λ s, subset_closure,
choice_eq := λ s h, rfl }
variable {G}
/-- Subgroup closure of a set is monotone in its argument: if `h ⊆ k`,
then `closure h ≤ closure k`. -/
@[to_additive "Additive subgroup closure of a set is monotone in its argument: if `h ⊆ k`,
then `closure h ≤ closure k`"]
lemma closure_mono ⦃h k : set G⦄ (h' : h ⊆ k) : closure h ≤ closure k :=
(subgroup.gi G).gc.monotone_l h'
/-- Closure of a subgroup `K` equals `K`. -/
@[simp, to_additive "Additive closure of an additive subgroup `K` equals `K`"]
lemma closure_eq : closure (K : set G) = K := (subgroup.gi G).l_u_eq K
@[simp, to_additive] lemma closure_empty : closure (∅ : set G) = ⊥ :=
(subgroup.gi G).gc.l_bot
@[simp, to_additive] lemma closure_univ : closure (univ : set G) = ⊤ :=
@coe_top G _ ▸ closure_eq ⊤
@[to_additive]
lemma closure_union (s t : set G) : closure (s ∪ t) = closure s ⊔ closure t :=
(subgroup.gi G).gc.l_sup
@[to_additive]
lemma closure_Union {ι} (s : ι → set G) : closure (⋃ i, s i) = ⨆ i, closure (s i) :=
(subgroup.gi G).gc.l_supr
@[to_additive]
lemma closure_eq_bot_iff (G : Type*) [group G] (S : set G) :
closure S = ⊥ ↔ S ⊆ {1} :=
by { rw [← le_bot_iff], exact closure_le _}
@[to_additive]
lemma supr_eq_closure {ι : Sort*} (p : ι → subgroup G) :
(⨆ i, p i) = closure (⋃ i, (p i : set G)) :=
by simp_rw [closure_Union, closure_eq]
/-- The subgroup generated by an element of a group equals the set of integer number powers of
the element. -/
@[to_additive /-"The `add_subgroup` generated by an element of an `add_group` equals the set of
natural number multiples of the element."-/]
lemma mem_closure_singleton {x y : G} : y ∈ closure ({x} : set G) ↔ ∃ n : ℤ, x ^ n = y :=
begin
refine ⟨λ hy, closure_induction hy _ _ _ _,
λ ⟨n, hn⟩, hn ▸ zpow_mem (subset_closure $ mem_singleton x) n⟩,
{ intros y hy,
rw [eq_of_mem_singleton hy],
exact ⟨1, zpow_one x⟩ },
{ exact ⟨0, zpow_zero x⟩ },
{ rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩,
exact ⟨n + m, zpow_add x n m⟩ },
rintros _ ⟨n, rfl⟩,
exact ⟨-n, zpow_neg x n⟩
end
@[to_additive]
lemma closure_singleton_one : closure ({1} : set G) = ⊥ :=
by simp [eq_bot_iff_forall, mem_closure_singleton]
@[simp, to_additive] lemma inv_subset_closure (S : set G) : S⁻¹ ⊆ closure S :=
begin
intros s hs,
rw [set_like.mem_coe, ←subgroup.inv_mem_iff],
exact subset_closure (mem_inv.mp hs),
end
@[simp, to_additive] lemma closure_inv (S : set G) : closure S⁻¹ = closure S :=
begin
refine le_antisymm ((subgroup.closure_le _).2 _) ((subgroup.closure_le _).2 _),
{ exact inv_subset_closure S },
{ simpa only [inv_inv] using inv_subset_closure S⁻¹ },
end
@[to_additive]
lemma closure_to_submonoid (S : set G) :
(closure S).to_submonoid = submonoid.closure (S ∪ S⁻¹) :=
begin
refine le_antisymm _ (submonoid.closure_le.2 _),
{ intros x hx,
refine closure_induction hx (λ x hx, submonoid.closure_mono (subset_union_left S S⁻¹)
(submonoid.subset_closure hx)) (submonoid.one_mem _) (λ x y hx hy, submonoid.mul_mem _ hx hy)
(λ x hx, _),
rwa [←submonoid.mem_closure_inv, set.union_inv, inv_inv, set.union_comm] },
{ simp only [true_and, coe_to_submonoid, union_subset_iff, subset_closure, inv_subset_closure] }
end
@[to_additive] lemma closure_induction_left {p : G → Prop} {x : G}
(h : x ∈ closure k) (H1 : p 1) (Hmul : ∀ (x ∈ k) y, p y → p (x * y))
(Hinv : ∀ (x ∈ k) y, p y → p (x⁻¹ * y)) : p x :=
let key := le_of_eq (closure_to_submonoid k) in submonoid.closure_induction_left (key h) H1
(λ x hx, hx.elim (Hmul x) (λ hx y hy, (congr_arg _ (inv_inv x)).mp (Hinv x⁻¹ hx y hy)))
@[to_additive] lemma closure_induction_right {p : G → Prop} {x : G}
(h : x ∈ closure k) (H1 : p 1) (Hmul : ∀ x (y ∈ k), p x → p (x * y))
(Hinv : ∀ x (y ∈ k), p x → p (x * y⁻¹)) : p x :=
let key := le_of_eq (closure_to_submonoid k) in submonoid.closure_induction_right (key h) H1
(λ x y hy, hy.elim (Hmul x y) (λ hy hx, (congr_arg _ (inv_inv y)).mp (Hinv x y⁻¹ hy hx)))
/-- An induction principle for closure membership. If `p` holds for `1` and all elements of
`k` and their inverse, and is preserved under multiplication, then `p` holds for all elements of
the closure of `k`. -/
@[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all
elements of `k` and their negation, and is preserved under addition, then `p` holds for all
elements of the additive closure of `k`."]
lemma closure_induction'' {p : G → Prop} {x} (h : x ∈ closure k)
(Hk : ∀ x ∈ k, p x) (Hk_inv : ∀ x ∈ k, p x⁻¹) (H1 : p 1)
(Hmul : ∀ x y, p x → p y → p (x * y)) : p x :=
closure_induction_left h H1 (λ x hx y hy, Hmul x y (Hk x hx) hy)
(λ x hx y hy, Hmul x⁻¹ y (Hk_inv x hx) hy)
/-- An induction principle for elements of `⨆ i, S i`.
If `C` holds for `1` and all elements of `S i` for all `i`, and is preserved under multiplication,
then it holds for all elements of the supremum of `S`. -/
@[elab_as_eliminator, to_additive /-" An induction principle for elements of `⨆ i, S i`.
If `C` holds for `0` and all elements of `S i` for all `i`, and is preserved under addition,
then it holds for all elements of the supremum of `S`. "-/]
lemma supr_induction {ι : Sort*} (S : ι → subgroup G) {C : G → Prop} {x : G} (hx : x ∈ ⨆ i, S i)
(hp : ∀ i (x ∈ S i), C x)
(h1 : C 1)
(hmul : ∀ x y, C x → C y → C (x * y)) : C x :=
begin
rw supr_eq_closure at hx,
refine closure_induction'' hx (λ x hx, _) (λ x hx, _) h1 hmul,
{ obtain ⟨i, hi⟩ := set.mem_Union.mp hx,
exact hp _ _ hi, },
{ obtain ⟨i, hi⟩ := set.mem_Union.mp hx,
exact hp _ _ (inv_mem hi), },
end
/-- A dependent version of `subgroup.supr_induction`. -/
@[elab_as_eliminator, to_additive /-"A dependent version of `add_subgroup.supr_induction`. "-/]
lemma supr_induction' {ι : Sort*} (S : ι → subgroup G) {C : Π x, (x ∈ ⨆ i, S i) → Prop}
(hp : ∀ i (x ∈ S i), C x (mem_supr_of_mem i ‹_›))
(h1 : C 1 (one_mem _))
(hmul : ∀ x y hx hy, C x hx → C y hy → C (x * y) (mul_mem ‹_› ‹_›))
{x : G} (hx : x ∈ ⨆ i, S i) : C x hx :=
begin
refine exists.elim _ (λ (hx : x ∈ ⨆ i, S i) (hc : C x hx), hc),
refine supr_induction S hx (λ i x hx, _) _ (λ x y, _),
{ exact ⟨_, hp _ _ hx⟩ },
{ exact ⟨_, h1⟩ },
{ rintro ⟨_, Cx⟩ ⟨_, Cy⟩,
refine ⟨_, hmul _ _ _ _ Cx Cy⟩ },
end
@[to_additive]
lemma mem_supr_of_directed {ι} [hι : nonempty ι] {K : ι → subgroup G} (hK : directed (≤) K)
{x : G} :
x ∈ (supr K : subgroup G) ↔ ∃ i, x ∈ K i :=
begin
refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr K i) hi⟩,
suffices : x ∈ closure (⋃ i, (K i : set G)) → ∃ i, x ∈ K i,
by simpa only [closure_Union, closure_eq (K _)] using this,
refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _ _),
{ exact hι.elim (λ i, ⟨i, (K i).one_mem⟩) },
{ rintros x y ⟨i, hi⟩ ⟨j, hj⟩,
rcases hK i j with ⟨k, hki, hkj⟩,
exact ⟨k, mul_mem (hki hi) (hkj hj)⟩ },
rintros _ ⟨i, hi⟩, exact ⟨i, inv_mem hi⟩
end
@[to_additive]
lemma coe_supr_of_directed {ι} [nonempty ι] {S : ι → subgroup G} (hS : directed (≤) S) :
((⨆ i, S i : subgroup G) : set G) = ⋃ i, ↑(S i) :=
set.ext $ λ x, by simp [mem_supr_of_directed hS]
@[to_additive]
lemma mem_Sup_of_directed_on {K : set (subgroup G)} (Kne : K.nonempty)
(hK : directed_on (≤) K) {x : G} :
x ∈ Sup K ↔ ∃ s ∈ K, x ∈ s :=
begin
haveI : nonempty K := Kne.to_subtype,
simp only [Sup_eq_supr', mem_supr_of_directed hK.directed_coe, set_coe.exists, subtype.coe_mk]
end
variables {N : Type*} [group N] {P : Type*} [group P]
/-- The preimage of a subgroup along a monoid homomorphism is a subgroup. -/
@[to_additive "The preimage of an `add_subgroup` along an `add_monoid` homomorphism
is an `add_subgroup`."]
def comap {N : Type*} [group N] (f : G →* N)
(H : subgroup N) : subgroup G :=
{ carrier := (f ⁻¹' H),
inv_mem' := λ a ha,
show f a⁻¹ ∈ H, by rw f.map_inv; exact H.inv_mem ha,
.. H.to_submonoid.comap f }
@[simp, to_additive]
lemma coe_comap (K : subgroup N) (f : G →* N) : (K.comap f : set G) = f ⁻¹' K := rfl
@[simp, to_additive]
lemma mem_comap {K : subgroup N} {f : G →* N} {x : G} : x ∈ K.comap f ↔ f x ∈ K := iff.rfl
@[to_additive]
lemma comap_mono {f : G →* N} {K K' : subgroup N} : K ≤ K' → comap f K ≤ comap f K' :=
preimage_mono
@[to_additive]
lemma comap_comap (K : subgroup P) (g : N →* P) (f : G →* N) :
(K.comap g).comap f = K.comap (g.comp f) :=
rfl
/-- The image of a subgroup along a monoid homomorphism is a subgroup. -/
@[to_additive "The image of an `add_subgroup` along an `add_monoid` homomorphism
is an `add_subgroup`."]
def map (f : G →* N) (H : subgroup G) : subgroup N :=
{ carrier := (f '' H),
inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, H.inv_mem hx, f.map_inv x⟩ },
.. H.to_submonoid.map f }
@[simp, to_additive]
lemma coe_map (f : G →* N) (K : subgroup G) :
(K.map f : set N) = f '' K := rfl
@[simp, to_additive]
lemma mem_map {f : G →* N} {K : subgroup G} {y : N} :
y ∈ K.map f ↔ ∃ x ∈ K, f x = y :=
mem_image_iff_bex
@[to_additive]
lemma mem_map_of_mem (f : G →* N) {K : subgroup G} {x : G} (hx : x ∈ K) : f x ∈ K.map f :=
mem_image_of_mem f hx
@[to_additive]
lemma apply_coe_mem_map (f : G →* N) (K : subgroup G) (x : K) : f x ∈ K.map f :=
mem_map_of_mem f x.prop
@[to_additive]
lemma map_mono {f : G →* N} {K K' : subgroup G} : K ≤ K' → map f K ≤ map f K' :=
image_subset _
@[simp, to_additive]
lemma map_id : K.map (monoid_hom.id G) = K :=
set_like.coe_injective $ image_id _
@[to_additive]
lemma map_map (g : N →* P) (f : G →* N) : (K.map f).map g = K.map (g.comp f) :=
set_like.coe_injective $ image_image _ _ _
@[simp, to_additive]
lemma map_one_eq_bot : K.map (1 : G →* N) = ⊥ :=
eq_bot_iff.mpr $ by { rintros x ⟨y, _ , rfl⟩, simp }
@[to_additive]
lemma mem_map_equiv {f : G ≃* N} {K : subgroup G} {x : N} :
x ∈ K.map f.to_monoid_hom ↔ f.symm x ∈ K :=
@set.mem_image_equiv _ _ ↑K f.to_equiv x
@[to_additive]
lemma mem_map_iff_mem {f : G →* N} (hf : function.injective f) {K : subgroup G} {x : G} :
f x ∈ K.map f ↔ x ∈ K :=
hf.mem_set_image
@[to_additive]
lemma map_equiv_eq_comap_symm (f : G ≃* N) (K : subgroup G) :
K.map f.to_monoid_hom = K.comap f.symm.to_monoid_hom :=
set_like.coe_injective (f.to_equiv.image_eq_preimage K)
@[to_additive]
lemma comap_equiv_eq_map_symm (f : N ≃* G) (K : subgroup G) :
K.comap f.to_monoid_hom = K.map f.symm.to_monoid_hom :=
(map_equiv_eq_comap_symm f.symm K).symm
@[to_additive]
lemma map_le_iff_le_comap {f : G →* N} {K : subgroup G} {H : subgroup N} :
K.map f ≤ H ↔ K ≤ H.comap f :=
image_subset_iff
@[to_additive]
lemma gc_map_comap (f : G →* N) : galois_connection (map f) (comap f) :=
λ _ _, map_le_iff_le_comap
@[to_additive]
lemma map_sup (H K : subgroup G) (f : G →* N) : (H ⊔ K).map f = H.map f ⊔ K.map f :=
(gc_map_comap f).l_sup
@[to_additive]
lemma map_supr {ι : Sort*} (f : G →* N) (s : ι → subgroup G) :
(supr s).map f = ⨆ i, (s i).map f :=
(gc_map_comap f).l_supr
@[to_additive] lemma comap_sup_comap_le
(H K : subgroup N) (f : G →* N) : comap f H ⊔ comap f K ≤ comap f (H ⊔ K) :=
monotone.le_map_sup (λ _ _, comap_mono) H K
@[to_additive] lemma supr_comap_le {ι : Sort*} (f : G →* N) (s : ι → subgroup N) :
(⨆ i, (s i).comap f) ≤ (supr s).comap f :=
monotone.le_map_supr (λ _ _, comap_mono)
@[to_additive]
lemma comap_inf (H K : subgroup N) (f : G →* N) : (H ⊓ K).comap f = H.comap f ⊓ K.comap f :=
(gc_map_comap f).u_inf
@[to_additive]
lemma comap_infi {ι : Sort*} (f : G →* N) (s : ι → subgroup N) :
(infi s).comap f = ⨅ i, (s i).comap f :=
(gc_map_comap f).u_infi
@[to_additive] lemma map_inf_le (H K : subgroup G) (f : G →* N) :
map f (H ⊓ K) ≤ map f H ⊓ map f K :=
le_inf (map_mono inf_le_left) (map_mono inf_le_right)
@[to_additive] lemma map_inf_eq (H K : subgroup G) (f : G →* N) (hf : function.injective f) :
map f (H ⊓ K) = map f H ⊓ map f K :=
begin
rw ← set_like.coe_set_eq,
simp [set.image_inter hf],
end
@[simp, to_additive] lemma map_bot (f : G →* N) : (⊥ : subgroup G).map f = ⊥ :=
(gc_map_comap f).l_bot
@[simp, to_additive] lemma map_top_of_surjective (f : G →* N) (h : function.surjective f) :
subgroup.map f ⊤ = ⊤ :=
by {rw eq_top_iff, intros x hx, obtain ⟨y, hy⟩ := (h x), exact ⟨y, trivial, hy⟩ }
@[simp, to_additive] lemma comap_top (f : G →* N) : (⊤ : subgroup N).comap f = ⊤ :=
(gc_map_comap f).u_top
@[simp, to_additive] lemma comap_subtype_self_eq_top {G : Type*} [group G] {H : subgroup G} :
comap H.subtype H = ⊤ := by { ext, simp }
@[simp, to_additive]
lemma comap_subtype_inf_left {H K : subgroup G} : comap H.subtype (H ⊓ K) = comap H.subtype K :=
ext $ λ x, and_iff_right_of_imp (λ _, x.prop)
@[simp, to_additive]
lemma comap_subtype_inf_right {H K : subgroup G} : comap K.subtype (H ⊓ K) = comap K.subtype H :=
ext $ λ x, and_iff_left_of_imp (λ _, x.prop)
/-- If `H ≤ K`, then `H` as a subgroup of `K` is isomorphic to `H`. -/
@[to_additive "If `H ≤ K`, then `H` as a subgroup of `K` is isomorphic to `H`.", simps]
def comap_subtype_equiv_of_le {G : Type*} [group G] {H K : subgroup G} (h : H ≤ K) :
H.comap K.subtype ≃* H :=
{ to_fun := λ g, ⟨g.1, g.2⟩,
inv_fun := λ g, ⟨⟨g.1, h g.2⟩, g.2⟩,
left_inv := λ g, subtype.ext (subtype.ext rfl),
right_inv := λ g, subtype.ext rfl,
map_mul' := λ g h, rfl }
/-- For any subgroups `H` and `K`, view `H ⊓ K` as a subgroup of `K`. -/
@[to_additive "For any subgroups `H` and `K`, view `H ⊓ K` as a subgroup of `K`."]
def subgroup_of (H K : subgroup G) : subgroup K := H.comap K.subtype
@[to_additive] lemma coe_subgroup_of (H K : subgroup G) :
(H.subgroup_of K : set K) = K.subtype ⁻¹' H := rfl
@[to_additive] lemma mem_subgroup_of {H K : subgroup G} {h : K} :
h ∈ H.subgroup_of K ↔ (h : G) ∈ H :=
iff.rfl
@[to_additive] lemma subgroup_of_map_subtype (H K : subgroup G) :
(H.subgroup_of K).map K.subtype = H ⊓ K := set_like.ext'
begin
convert set.image_preimage_eq_inter_range,
simp only [subtype.range_coe_subtype, coe_subtype, coe_inf],
refl,
end
@[simp, to_additive] lemma bot_subgroup_of : (⊥ : subgroup G).subgroup_of H = ⊥ :=
eq.symm (subgroup.ext (λ g, subtype.ext_iff))
@[simp, to_additive] lemma top_subgroup_of : (⊤ : subgroup G).subgroup_of H = ⊤ :=
rfl
@[to_additive] lemma subgroup_of_bot_eq_bot : H.subgroup_of ⊥ = ⊥ :=
subsingleton.elim _ _
@[to_additive] lemma subgroup_of_bot_eq_top : H.subgroup_of ⊥ = ⊤ :=
subsingleton.elim _ _
@[simp, to_additive] lemma subgroup_of_self : H.subgroup_of H = ⊤ :=
top_le_iff.mp (λ g hg, g.2)
/-- Given `subgroup`s `H`, `K` of groups `G`, `N` respectively, `H × K` as a subgroup of `G × N`. -/
@[to_additive prod "Given `add_subgroup`s `H`, `K` of `add_group`s `A`, `B` respectively, `H × K`
as an `add_subgroup` of `A × B`."]
def prod (H : subgroup G) (K : subgroup N) : subgroup (G × N) :=
{ inv_mem' := λ _ hx, ⟨H.inv_mem' hx.1, K.inv_mem' hx.2⟩,
.. submonoid.prod H.to_submonoid K.to_submonoid}
@[to_additive coe_prod]
lemma coe_prod (H : subgroup G) (K : subgroup N) :
(H.prod K : set (G × N)) = (H : set G) ×ˢ (K : set N) := rfl
@[to_additive mem_prod]
lemma mem_prod {H : subgroup G} {K : subgroup N} {p : G × N} :
p ∈ H.prod K ↔ p.1 ∈ H ∧ p.2 ∈ K := iff.rfl
@[to_additive prod_mono]
lemma prod_mono : ((≤) ⇒ (≤) ⇒ (≤)) (@prod G _ N _) (@prod G _ N _) :=
λ s s' hs t t' ht, set.prod_mono hs ht
@[to_additive prod_mono_right]
lemma prod_mono_right (K : subgroup G) : monotone (λ t : subgroup N, K.prod t) :=
prod_mono (le_refl K)
@[to_additive prod_mono_left]
lemma prod_mono_left (H : subgroup N) : monotone (λ K : subgroup G, K.prod H) :=
λ s₁ s₂ hs, prod_mono hs (le_refl H)
@[to_additive prod_top]
lemma prod_top (K : subgroup G) :
K.prod (⊤ : subgroup N) = K.comap (monoid_hom.fst G N) :=
ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst]
@[to_additive top_prod]
lemma top_prod (H : subgroup N) :
(⊤ : subgroup G).prod H = H.comap (monoid_hom.snd G N) :=
ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd]
@[simp, to_additive top_prod_top]
lemma top_prod_top : (⊤ : subgroup G).prod (⊤ : subgroup N) = ⊤ :=
(top_prod _).trans $ comap_top _
@[to_additive] lemma bot_prod_bot : (⊥ : subgroup G).prod (⊥ : subgroup N) = ⊥ :=
set_like.coe_injective $ by simp [coe_prod, prod.one_eq_mk]
@[to_additive le_prod_iff]
lemma le_prod_iff {H : subgroup G} {K : subgroup N} {J : subgroup (G × N)} :
J ≤ H.prod K ↔ map (monoid_hom.fst G N) J ≤ H ∧ map (monoid_hom.snd G N) J ≤ K :=
by simpa only [← subgroup.to_submonoid_le] using submonoid.le_prod_iff
@[to_additive prod_le_iff]
lemma prod_le_iff {H : subgroup G} {K : subgroup N} {J : subgroup (G × N)} :
H.prod K ≤ J ↔ map (monoid_hom.inl G N) H ≤ J ∧ map (monoid_hom.inr G N) K ≤ J :=
by simpa only [← subgroup.to_submonoid_le] using submonoid.prod_le_iff
@[simp, to_additive prod_eq_bot_iff]
lemma prod_eq_bot_iff {H : subgroup G} {K : subgroup N} :
H.prod K = ⊥ ↔ H = ⊥ ∧ K = ⊥ :=
by simpa only [← subgroup.to_submonoid_eq] using submonoid.prod_eq_bot_iff
/-- Product of subgroups is isomorphic to their product as groups. -/
@[to_additive prod_equiv "Product of additive subgroups is isomorphic to their product
as additive groups"]
def prod_equiv (H : subgroup G) (K : subgroup N) : H.prod K ≃* H × K :=
{ map_mul' := λ x y, rfl, .. equiv.set.prod ↑H ↑K }
section pi
variables {η : Type*} {f : η → Type*}
-- defined here and not in group_theory.submonoid.operations to have access to algebra.group.pi
/-- A version of `set.pi` for submonoids. Given an index set `I` and a family of submodules
`s : Π i, submonoid f i`, `pi I s` is the submonoid of dependent functions `f : Π i, f i` such that
`f i` belongs to `pi I s` whenever `i ∈ I`. -/
@[to_additive " A version of `set.pi` for `add_submonoid`s. Given an index set `I` and a family
of submodules `s : Π i, add_submonoid f i`, `pi I s` is the `add_submonoid` of dependent functions
`f : Π i, f i` such that `f i` belongs to `pi I s` whenever `i ∈ I`. -/ "]
def _root_.submonoid.pi [∀ i, mul_one_class (f i)] (I : set η) (s : Π i, submonoid (f i)) :
submonoid (Π i, f i) :=
{ carrier := I.pi (λ i, (s i).carrier),
one_mem' := λ i _ , (s i).one_mem,
mul_mem' := λ p q hp hq i hI, (s i).mul_mem (hp i hI) (hq i hI) }
variables [∀ i, group (f i)]
/-- A version of `set.pi` for subgroups. Given an index set `I` and a family of submodules
`s : Π i, subgroup f i`, `pi I s` is the subgroup of dependent functions `f : Π i, f i` such that
`f i` belongs to `pi I s` whenever `i ∈ I`. -/
@[to_additive " A version of `set.pi` for `add_subgroup`s. Given an index set `I` and a family
of submodules `s : Π i, add_subgroup f i`, `pi I s` is the `add_subgroup` of dependent functions
`f : Π i, f i` such that `f i` belongs to `pi I s` whenever `i ∈ I`. -/ "]
def pi (I : set η) (H : Π i, subgroup (f i)) : subgroup (Π i, f i) :=
{ submonoid.pi I (λ i, (H i).to_submonoid) with
inv_mem' := λ p hp i hI, (H i).inv_mem (hp i hI) }
@[to_additive] lemma coe_pi (I : set η) (H : Π i, subgroup (f i)) :
(pi I H : set (Π i, f i)) = set.pi I (λ i, (H i : set (f i))) := rfl
@[to_additive] lemma mem_pi (I : set η) {H : Π i, subgroup (f i)} {p : Π i, f i} :
p ∈ pi I H ↔ (∀ i : η, i ∈ I → p i ∈ H i) := iff.rfl
@[to_additive] lemma pi_top (I : set η) : pi I (λ i, (⊤ : subgroup (f i))) = ⊤ :=
ext $ λ x, by simp [mem_pi]
@[to_additive] lemma pi_empty (H : Π i, subgroup (f i)): pi ∅ H = ⊤ :=
ext $ λ x, by simp [mem_pi]
@[to_additive] lemma pi_bot : pi set.univ (λ i, (⊥ : subgroup (f i))) = ⊥ :=
(eq_bot_iff_forall _).mpr $ λ p hp,
by { simp only [mem_pi, mem_bot] at *, ext j, exact hp j trivial, }
@[to_additive]
lemma le_pi_iff {I : set η} {H : Π i, subgroup (f i)} {J : subgroup (Π i, f i)} :
J ≤ pi I H ↔ (∀ i : η , i ∈ I → map (pi.eval_monoid_hom f i) J ≤ H i) :=
begin
split,
{ intros h i hi, rintros _ ⟨x, hx, rfl⟩, exact (h hx) _ hi, },
{ intros h x hx i hi, refine h i hi ⟨_, hx, rfl⟩, }
end
@[simp, to_additive]
lemma mul_single_mem_pi [decidable_eq η] {I : set η} {H : Π i, subgroup (f i)}
(i : η) (x : f i) :
pi.mul_single i x ∈ pi I H ↔ (i ∈ I → x ∈ H i) :=
begin
split,
{ intros h hi, simpa using h i hi, },
{ intros h j hj,
by_cases heq : j = i,
{ subst heq, simpa using h hj, },
{ simp [heq, one_mem], }, }
end
@[to_additive]
lemma pi_mem_of_mul_single_mem_aux [decidable_eq η] (I : finset η) {H : subgroup (Π i, f i) }
(x : Π i, f i) (h1 : ∀ i, i ∉ I → x i = 1) (h2 : ∀ i, i ∈ I → pi.mul_single i (x i) ∈ H ) :
x ∈ H :=
begin
induction I using finset.induction_on with i I hnmem ih generalizing x,
{ convert one_mem H,
ext i,
exact (h1 i (not_mem_empty i)) },
{ have : x = function.update x i 1 * pi.mul_single i (x i),
{ ext j,
by_cases heq : j = i,
{ subst heq, simp, },
{ simp [heq], }, },
rw this, clear this,
apply mul_mem,
{ apply ih; clear ih,
{ intros j hj,
by_cases heq : j = i,
{ subst heq, simp, },
{ simp [heq], apply h1 j, simpa [heq] using hj, } },
{ intros j hj,
have : j ≠ i, by { rintro rfl, contradiction },
simp [this],
exact h2 _ (finset.mem_insert_of_mem hj), }, },
{ apply h2, simp, } }
end
@[to_additive]
lemma pi_mem_of_mul_single_mem [fintype η] [decidable_eq η] {H : subgroup (Π i, f i)}
(x : Π i, f i) (h : ∀ i, pi.mul_single i (x i) ∈ H) : x ∈ H :=
pi_mem_of_mul_single_mem_aux finset.univ x (by simp) (λ i _, h i)
/-- For finite index types, the `subgroup.pi` is generated by the embeddings of the groups. -/
@[to_additive "For finite index types, the `subgroup.pi` is generated by the embeddings of the
additive groups."]
lemma pi_le_iff [decidable_eq η] [fintype η] {H : Π i, subgroup (f i)} {J : subgroup (Π i, f i)} :
pi univ H ≤ J ↔ (∀ i : η, map (monoid_hom.single f i) (H i) ≤ J) :=
begin
split,
{ rintros h i _ ⟨x, hx, rfl⟩, apply h, simpa using hx },
{ exact λ h x hx, pi_mem_of_mul_single_mem x (λ i, h i (mem_map_of_mem _ (hx i trivial))), }
end
@[to_additive]
lemma pi_eq_bot_iff (H : Π i, subgroup (f i)) :
pi set.univ H = ⊥ ↔ ∀ i, H i = ⊥ :=
begin
classical,
simp only [eq_bot_iff_forall],
split,
{ intros h i x hx,
have : monoid_hom.single f i x = 1 :=
h (monoid_hom.single f i x) ((mul_single_mem_pi i x).mpr (λ _, hx)),
simpa using congr_fun this i, },
{ exact λ h x hx, funext (λ i, h _ _ (hx i trivial)), },
end
end pi
/-- A subgroup is normal if whenever `n ∈ H`, then `g * n * g⁻¹ ∈ H` for every `g : G` -/
structure normal : Prop :=
(conj_mem : ∀ n, n ∈ H → ∀ g : G, g * n * g⁻¹ ∈ H)
attribute [class] normal
end subgroup
namespace add_subgroup
/-- An add_subgroup is normal if whenever `n ∈ H`, then `g + n - g ∈ H` for every `g : G` -/
structure normal (H : add_subgroup A) : Prop :=
(conj_mem [] : ∀ n, n ∈ H → ∀ g : A, g + n + -g ∈ H)
attribute [to_additive add_subgroup.normal] subgroup.normal
attribute [class] normal
end add_subgroup
namespace subgroup
variables {H K : subgroup G}
@[priority 100, to_additive]
instance normal_of_comm {G : Type*} [comm_group G] (H : subgroup G) : H.normal :=
⟨by simp [mul_comm, mul_left_comm]⟩
namespace normal
variable (nH : H.normal)
@[to_additive] lemma mem_comm {a b : G} (h : a * b ∈ H) : b * a ∈ H :=
have a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ H, from nH.conj_mem (a * b) h a⁻¹, by simpa
@[to_additive] lemma mem_comm_iff {a b : G} : a * b ∈ H ↔ b * a ∈ H :=
⟨nH.mem_comm, nH.mem_comm⟩
end normal
variables (H)
/-- A subgroup is characteristic if it is fixed by all automorphisms.
Several equivalent conditions are provided by lemmas of the form `characteristic.iff...` -/
structure characteristic : Prop :=
(fixed : ∀ ϕ : G ≃* G, H.comap ϕ.to_monoid_hom = H)
attribute [class] characteristic
@[priority 100] instance normal_of_characteristic [h : H.characteristic] : H.normal :=
⟨λ a ha b, (set_like.ext_iff.mp (h.fixed (mul_aut.conj b)) a).mpr ha⟩
end subgroup
namespace add_subgroup
variables (H : add_subgroup A)
/-- A add_subgroup is characteristic if it is fixed by all automorphisms.
Several equivalent conditions are provided by lemmas of the form `characteristic.iff...` -/
structure characteristic : Prop :=
(fixed : ∀ ϕ : A ≃+ A, H.comap ϕ.to_add_monoid_hom = H)
attribute [to_additive add_subgroup.characteristic] subgroup.characteristic
attribute [class] characteristic
@[priority 100] instance normal_of_characteristic [h : H.characteristic] : H.normal :=
⟨λ a ha b, (set_like.ext_iff.mp (h.fixed (add_aut.conj b)) a).mpr ha⟩
end add_subgroup
namespace subgroup
variables {H K : subgroup G}
@[to_additive] lemma characteristic_iff_comap_eq :
H.characteristic ↔ ∀ ϕ : G ≃* G, H.comap ϕ.to_monoid_hom = H :=
⟨characteristic.fixed, characteristic.mk⟩
@[to_additive] lemma characteristic_iff_comap_le :
H.characteristic ↔ ∀ ϕ : G ≃* G, H.comap ϕ.to_monoid_hom ≤ H :=
characteristic_iff_comap_eq.trans ⟨λ h ϕ, le_of_eq (h ϕ),
λ h ϕ, le_antisymm (h ϕ) (λ g hg, h ϕ.symm ((congr_arg (∈ H) (ϕ.symm_apply_apply g)).mpr hg))⟩
@[to_additive] lemma characteristic_iff_le_comap :
H.characteristic ↔ ∀ ϕ : G ≃* G, H ≤ H.comap ϕ.to_monoid_hom :=
characteristic_iff_comap_eq.trans ⟨λ h ϕ, ge_of_eq (h ϕ),
λ h ϕ, le_antisymm (λ g hg, (congr_arg (∈ H) (ϕ.symm_apply_apply g)).mp (h ϕ.symm hg)) (h ϕ)⟩
@[to_additive] lemma characteristic_iff_map_eq :
H.characteristic ↔ ∀ ϕ : G ≃* G, H.map ϕ.to_monoid_hom = H :=
begin
simp_rw map_equiv_eq_comap_symm,
exact characteristic_iff_comap_eq.trans ⟨λ h ϕ, h ϕ.symm, λ h ϕ, h ϕ.symm⟩,
end
@[to_additive] lemma characteristic_iff_map_le :
H.characteristic ↔ ∀ ϕ : G ≃* G, H.map ϕ.to_monoid_hom ≤ H :=
begin
simp_rw map_equiv_eq_comap_symm,
exact characteristic_iff_comap_le.trans ⟨λ h ϕ, h ϕ.symm, λ h ϕ, h ϕ.symm⟩,
end
@[to_additive] lemma characteristic_iff_le_map :
H.characteristic ↔ ∀ ϕ : G ≃* G, H ≤ H.map ϕ.to_monoid_hom :=
begin
simp_rw map_equiv_eq_comap_symm,
exact characteristic_iff_le_comap.trans ⟨λ h ϕ, h ϕ.symm, λ h ϕ, h ϕ.symm⟩,
end
@[to_additive] instance bot_characteristic : characteristic (⊥ : subgroup G) :=
characteristic_iff_le_map.mpr (λ ϕ, bot_le)
@[to_additive] instance top_characteristic : characteristic (⊤ : subgroup G) :=
characteristic_iff_map_le.mpr (λ ϕ, le_top)
variable (G)
/-- The center of a group `G` is the set of elements that commute with everything in `G` -/
@[to_additive "The center of an additive group `G` is the set of elements that commute with
everything in `G`"]
def center : subgroup G :=
{ carrier := set.center G,
inv_mem' := λ a, set.inv_mem_center,
.. submonoid.center G }
@[to_additive]
lemma coe_center : ↑(center G) = set.center G := rfl
@[simp, to_additive]
lemma center_to_submonoid : (center G).to_submonoid = submonoid.center G := rfl
variable {G}
@[to_additive] lemma mem_center_iff {z : G} : z ∈ center G ↔ ∀ g, g * z = z * g := iff.rfl
instance decidable_mem_center [decidable_eq G] [fintype G] : decidable_pred (∈ center G) :=
λ _, decidable_of_iff' _ mem_center_iff
@[to_additive] instance center_characteristic : (center G).characteristic :=
begin
refine characteristic_iff_comap_le.mpr (λ ϕ g hg h, _),
rw [←ϕ.injective.eq_iff, ϕ.map_mul, ϕ.map_mul],
exact hg (ϕ h),
end
lemma _root_.comm_group.center_eq_top {G : Type*} [comm_group G] : center G = ⊤ :=
by { rw [eq_top_iff'], intros x y, exact mul_comm y x }
/-- A group is commutative if the center is the whole group -/
def _root_.group.comm_group_of_center_eq_top (h : center G = ⊤) : comm_group G :=
{ mul_comm := by { rw eq_top_iff' at h, intros x y, exact h y x },
.. (_ : group G) }
variables {G} (H)
section normalizer
/-- The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal. -/
@[to_additive "The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal."]
def normalizer : subgroup G :=
{ carrier := {g : G | ∀ n, n ∈ H ↔ g * n * g⁻¹ ∈ H},
one_mem' := by simp,
mul_mem' := λ a b (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) (hb : ∀ n, n ∈ H ↔ b * n * b⁻¹ ∈ H) n,
by { rw [hb, ha], simp [mul_assoc] },
inv_mem' := λ a (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) n,
by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } }
-- variant for sets.
-- TODO should this replace `normalizer`?
/-- The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy `g*S*g⁻¹=S` -/
@[to_additive "The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy
`g+S-g=S`."]
def set_normalizer (S : set G) : subgroup G :=
{ carrier := {g : G | ∀ n, n ∈ S ↔ g * n * g⁻¹ ∈ S},
one_mem' := by simp,
mul_mem' := λ a b (ha : ∀ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) (hb : ∀ n, n ∈ S ↔ b * n * b⁻¹ ∈ S) n,
by { rw [hb, ha], simp [mul_assoc] },
inv_mem' := λ a (ha : ∀ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) n,
by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } }
lemma mem_normalizer_fintype {S : set G} [fintype S] {x : G}
(h : ∀ n, n ∈ S → x * n * x⁻¹ ∈ S) : x ∈ subgroup.set_normalizer S :=
by haveI := classical.prop_decidable;
haveI := set.fintype_image S (λ n, x * n * x⁻¹); exact
λ n, ⟨h n, λ h₁,
have heq : (λ n, x * n * x⁻¹) '' S = S := set.eq_of_subset_of_card_le
(λ n ⟨y, hy⟩, hy.2 ▸ h y hy.1) (by rw set.card_image_of_injective S conj_injective),
have x * n * x⁻¹ ∈ (λ n, x * n * x⁻¹) '' S := heq.symm ▸ h₁,
let ⟨y, hy⟩ := this in conj_injective hy.2 ▸ hy.1⟩
variable {H}
@[to_additive] lemma mem_normalizer_iff {g : G} :
g ∈ H.normalizer ↔ ∀ h, h ∈ H ↔ g * h * g⁻¹ ∈ H :=
iff.rfl
@[to_additive] lemma mem_normalizer_iff'' {g : G} :
g ∈ H.normalizer ↔ ∀ h : G, h ∈ H ↔ g⁻¹ * h * g ∈ H :=
by rw [←inv_mem_iff, mem_normalizer_iff, inv_inv]
@[to_additive] lemma mem_normalizer_iff' {g : G} : g ∈ H.normalizer ↔ ∀ n, n * g ∈ H ↔ g * n ∈ H :=
⟨λ h n, by rw [h, mul_assoc, mul_inv_cancel_right],
λ h n, by rw [mul_assoc, ←h, inv_mul_cancel_right]⟩
@[to_additive] lemma le_normalizer : H ≤ normalizer H :=
λ x xH n, by rw [H.mul_mem_cancel_right (H.inv_mem xH), H.mul_mem_cancel_left xH]
@[priority 100, to_additive]
instance normal_in_normalizer : (H.comap H.normalizer.subtype).normal :=
⟨λ x xH g, by simpa using (g.2 x).1 xH⟩
@[to_additive] lemma normalizer_eq_top : H.normalizer = ⊤ ↔ H.normal :=
eq_top_iff.trans ⟨λ h, ⟨λ a ha b, (h (mem_top b) a).mp ha⟩, λ h a ha b,
⟨λ hb, h.conj_mem b hb a, λ hb, by rwa [h.mem_comm_iff, inv_mul_cancel_left] at hb⟩⟩
@[to_additive] lemma center_le_normalizer : center G ≤ H.normalizer :=
λ x hx y, by simp [← mem_center_iff.mp hx y, mul_assoc]
open_locale classical
@[to_additive]
lemma le_normalizer_of_normal [hK : (H.comap K.subtype).normal] (HK : H ≤ K) : K ≤ H.normalizer :=
λ x hx y, ⟨λ yH, hK.conj_mem ⟨y, HK yH⟩ yH ⟨x, hx⟩,
λ yH, by simpa [mem_comap, mul_assoc] using
hK.conj_mem ⟨x * y * x⁻¹, HK yH⟩ yH ⟨x⁻¹, K.inv_mem hx⟩⟩
variables {N : Type*} [group N]
/-- The preimage of the normalizer is contained in the normalizer of the preimage. -/
@[to_additive "The preimage of the normalizer is contained in the normalizer of the preimage."]
lemma le_normalizer_comap (f : N →* G) :
H.normalizer.comap f ≤ (H.comap f).normalizer :=
λ x, begin
simp only [mem_normalizer_iff, mem_comap],
assume h n,
simp [h (f n)]
end
/-- The image of the normalizer is contained in the normalizer of the image. -/
@[to_additive "The image of the normalizer is contained in the normalizer of the image."]
lemma le_normalizer_map (f : G →* N) :
H.normalizer.map f ≤ (H.map f).normalizer :=
λ _, begin
simp only [and_imp, exists_prop, mem_map, exists_imp_distrib, mem_normalizer_iff],
rintros x hx rfl n,
split,
{ rintros ⟨y, hy, rfl⟩,
use [x * y * x⁻¹, (hx y).1 hy],
simp },
{ rintros ⟨y, hyH, hy⟩,
use [x⁻¹ * y * x],
rw [hx],
simp [hy, hyH, mul_assoc] }
end
variable (G)
/-- Every proper subgroup `H` of `G` is a proper normal subgroup of the normalizer of `H` in `G`. -/
def _root_.normalizer_condition := ∀ (H : subgroup G), H < ⊤ → H < normalizer H
variable {G}
/-- Alternative phrasing of the normalizer condition: Only the full group is self-normalizing.
This may be easier to work with, as it avoids inequalities and negations. -/
lemma _root_.normalizer_condition_iff_only_full_group_self_normalizing :
normalizer_condition G ↔ ∀ (H : subgroup G), H.normalizer = H → H = ⊤ :=
begin
apply forall_congr, intro H,
simp only [lt_iff_le_and_ne, le_normalizer, true_and, le_top, ne.def],
tauto!,
end
variable (H)
/-- In a group that satisifes the normalizer condition, every maximal subgroup is normal -/
lemma normalizer_condition.normal_of_coatom
(hnc : normalizer_condition G) (hmax : is_coatom H) : H.normal :=
normalizer_eq_top.mp (hmax.2 _ (hnc H (lt_top_iff_ne_top.mpr hmax.1)))
end normalizer
section centralizer
/-- The `centralizer` of `H` is the subgroup of `g : G` commuting with every `h : H`. -/
@[to_additive "The `centralizer` of `H` is the additive subgroup of `g : G` commuting with
every `h : H`."]
def centralizer : subgroup G :=
{ carrier := set.centralizer H,
inv_mem' := λ g, set.inv_mem_centralizer,
.. submonoid.centralizer ↑H }
@[to_additive] lemma mem_centralizer_iff {g : G} : g ∈ H.centralizer ↔ ∀ h ∈ H, h * g = g * h :=
iff.rfl
@[to_additive] lemma mem_centralizer_iff_commutator_eq_one {g : G} :
g ∈ H.centralizer ↔ ∀ h ∈ H, h * g * h⁻¹ * g⁻¹ = 1 :=
by simp only [mem_centralizer_iff, mul_inv_eq_iff_eq_mul, one_mul]
@[to_additive] lemma centralizer_top : centralizer ⊤ = center G :=
set_like.ext' (set.centralizer_univ G)
@[to_additive] instance subgroup.centralizer.characteristic [hH : H.characteristic] :
H.centralizer.characteristic :=
begin
refine subgroup.characteristic_iff_comap_le.mpr (λ ϕ g hg h hh, ϕ.injective _),
rw [map_mul, map_mul],
exact hg (ϕ h) (subgroup.characteristic_iff_le_comap.mp hH ϕ hh),
end
end centralizer
/-- Commutivity of a subgroup -/
structure is_commutative : Prop :=
(is_comm : _root_.is_commutative H (*))
attribute [class] is_commutative
/-- Commutivity of an additive subgroup -/
structure _root_.add_subgroup.is_commutative (H : add_subgroup A) : Prop :=
(is_comm : _root_.is_commutative H (+))
attribute [to_additive add_subgroup.is_commutative] subgroup.is_commutative
attribute [class] add_subgroup.is_commutative
/-- A commutative subgroup is commutative -/
@[to_additive] instance is_commutative.comm_group [h : H.is_commutative] : comm_group H :=
{ mul_comm := h.is_comm.comm, .. H.to_group }
instance center.is_commutative : (center G).is_commutative :=
⟨⟨λ a b, subtype.ext (b.2 a)⟩⟩
end subgroup
namespace group
variables {s : set G}
/-- Given a set `s`, `conjugates_of_set s` is the set of all conjugates of
the elements of `s`. -/
def conjugates_of_set (s : set G) : set G := ⋃ a ∈ s, conjugates_of a
lemma mem_conjugates_of_set_iff {x : G} : x ∈ conjugates_of_set s ↔ ∃ a ∈ s, is_conj a x :=
set.mem_Union₂
theorem subset_conjugates_of_set : s ⊆ conjugates_of_set s :=
λ (x : G) (h : x ∈ s), mem_conjugates_of_set_iff.2 ⟨x, h, is_conj.refl _⟩
theorem conjugates_of_set_mono {s t : set G} (h : s ⊆ t) :
conjugates_of_set s ⊆ conjugates_of_set t :=
set.bUnion_subset_bUnion_left h
lemma conjugates_subset_normal {N : subgroup G} [tn : N.normal] {a : G} (h : a ∈ N) :
conjugates_of a ⊆ N :=
by { rintros a hc, obtain ⟨c, rfl⟩ := is_conj_iff.1 hc, exact tn.conj_mem a h c }
theorem conjugates_of_set_subset {s : set G} {N : subgroup G} [N.normal] (h : s ⊆ N) :
conjugates_of_set s ⊆ N :=
set.Union₂_subset (λ x H, conjugates_subset_normal (h H))
/-- The set of conjugates of `s` is closed under conjugation. -/
lemma conj_mem_conjugates_of_set {x c : G} :
x ∈ conjugates_of_set s → (c * x * c⁻¹ ∈ conjugates_of_set s) :=
λ H,
begin
rcases (mem_conjugates_of_set_iff.1 H) with ⟨a,h₁,h₂⟩,
exact mem_conjugates_of_set_iff.2 ⟨a, h₁, h₂.trans (is_conj_iff.2 ⟨c,rfl⟩)⟩,
end
end group
namespace subgroup
open group
variable {s : set G}
/-- The normal closure of a set `s` is the subgroup closure of all the conjugates of
elements of `s`. It is the smallest normal subgroup containing `s`. -/
def normal_closure (s : set G) : subgroup G := closure (conjugates_of_set s)
theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s ⊆ normal_closure s :=
subset_closure
theorem subset_normal_closure : s ⊆ normal_closure s :=
set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure
theorem le_normal_closure {H : subgroup G} : H ≤ normal_closure ↑H :=
λ _ h, subset_normal_closure h
/-- The normal closure of `s` is a normal subgroup. -/
instance normal_closure_normal : (normal_closure s).normal :=
⟨λ n h g,
begin
refine subgroup.closure_induction h (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _),
{ exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx)) },
{ simpa using (normal_closure s).one_mem },
{ rw ← conj_mul,
exact mul_mem ihx ihy },
{ rw ← conj_inv,
exact inv_mem ihx }
end⟩
/-- The normal closure of `s` is the smallest normal subgroup containing `s`. -/
theorem normal_closure_le_normal {N : subgroup G} [N.normal]
(h : s ⊆ N) : normal_closure s ≤ N :=
begin
assume a w,
refine closure_induction w (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _),
{ exact (conjugates_of_set_subset h hx) },
{ exact one_mem _ },
{ exact mul_mem ihx ihy },
{ exact inv_mem ihx }
end
lemma normal_closure_subset_iff {N : subgroup G} [N.normal] : s ⊆ N ↔ normal_closure s ≤ N :=
⟨normal_closure_le_normal, set.subset.trans (subset_normal_closure)⟩
theorem normal_closure_mono {s t : set G} (h : s ⊆ t) : normal_closure s ≤ normal_closure t :=
normal_closure_le_normal (set.subset.trans h subset_normal_closure)
theorem normal_closure_eq_infi : normal_closure s =
⨅ (N : subgroup G) (_ : normal N) (hs : s ⊆ N), N :=
le_antisymm
(le_infi (λ N, le_infi (λ hN, by exactI le_infi (normal_closure_le_normal))))
(infi_le_of_le (normal_closure s) (infi_le_of_le (by apply_instance)
(infi_le_of_le subset_normal_closure le_rfl)))
@[simp] theorem normal_closure_eq_self (H : subgroup G) [H.normal] : normal_closure ↑H = H :=
le_antisymm (normal_closure_le_normal rfl.subset) (le_normal_closure)
@[simp] theorem normal_closure_idempotent : normal_closure ↑(normal_closure s) = normal_closure s :=
normal_closure_eq_self _
theorem closure_le_normal_closure {s : set G} : closure s ≤ normal_closure s :=
by simp only [subset_normal_closure, closure_le]
@[simp] theorem normal_closure_closure_eq_normal_closure {s : set G} :
normal_closure ↑(closure s) = normal_closure s :=
le_antisymm (normal_closure_le_normal closure_le_normal_closure)
(normal_closure_mono subset_closure)
/-- The normal core of a subgroup `H` is the largest normal subgroup of `G` contained in `H`,
as shown by `subgroup.normal_core_eq_supr`. -/
def normal_core (H : subgroup G) : subgroup G :=
{ carrier := {a : G | ∀ b : G, b * a * b⁻¹ ∈ H},
one_mem' := λ a, by rw [mul_one, mul_inv_self]; exact H.one_mem,
inv_mem' := λ a h b, (congr_arg (∈ H) conj_inv).mp (H.inv_mem (h b)),
mul_mem' := λ a b ha hb c, (congr_arg (∈ H) conj_mul).mp (H.mul_mem (ha c) (hb c)) }
lemma normal_core_le (H : subgroup G) : H.normal_core ≤ H :=
λ a h, by { rw [←mul_one a, ←inv_one, ←one_mul a], exact h 1 }
instance normal_core_normal (H : subgroup G) : H.normal_core.normal :=
⟨λ a h b c, by rw [mul_assoc, mul_assoc, ←mul_inv_rev, ←mul_assoc, ←mul_assoc]; exact h (c * b)⟩
lemma normal_le_normal_core {H : subgroup G} {N : subgroup G} [hN : N.normal] :
N ≤ H.normal_core ↔ N ≤ H :=
⟨ge_trans H.normal_core_le, λ h_le n hn g, h_le (hN.conj_mem n hn g)⟩
lemma normal_core_mono {H K : subgroup G} (h : H ≤ K) : H.normal_core ≤ K.normal_core :=
normal_le_normal_core.mpr (H.normal_core_le.trans h)
lemma normal_core_eq_supr (H : subgroup G) :
H.normal_core = ⨆ (N : subgroup G) (_ : normal N) (hs : N ≤ H), N :=
le_antisymm (le_supr_of_le H.normal_core
(le_supr_of_le H.normal_core_normal (le_supr_of_le H.normal_core_le le_rfl)))
(supr_le (λ N, supr_le (λ hN, supr_le (by exactI normal_le_normal_core.mpr))))
@[simp] lemma normal_core_eq_self (H : subgroup G) [H.normal] : H.normal_core = H :=
le_antisymm H.normal_core_le (normal_le_normal_core.mpr le_rfl)
@[simp] theorem normal_core_idempotent (H : subgroup G) :
H.normal_core.normal_core = H.normal_core :=
H.normal_core.normal_core_eq_self
end subgroup
namespace monoid_hom
variables {N : Type*} {P : Type*} [group N] [group P] (K : subgroup G)
open subgroup
/-- The range of a monoid homomorphism from a group is a subgroup. -/
@[to_additive "The range of an `add_monoid_hom` from an `add_group` is an `add_subgroup`."]
def range (f : G →* N) : subgroup N :=
subgroup.copy ((⊤ : subgroup G).map f) (set.range f) (by simp [set.ext_iff])
@[to_additive]
instance decidable_mem_range (f : G →* N) [fintype G] [decidable_eq N] :
decidable_pred (∈ f.range) :=
λ x, fintype.decidable_exists_fintype
@[simp, to_additive] lemma coe_range (f : G →* N) :
(f.range : set N) = set.range f := rfl
@[simp, to_additive] lemma mem_range {f : G →* N} {y : N} :
y ∈ f.range ↔ ∃ x, f x = y :=
iff.rfl
@[to_additive] lemma range_eq_map (f : G →* N) : f.range = (⊤ : subgroup G).map f :=
by ext; simp
/-- The canonical surjective group homomorphism `G →* f(G)` induced by a group
homomorphism `G →* N`. -/
@[to_additive "The canonical surjective `add_group` homomorphism `G →+ f(G)` induced by a group
homomorphism `G →+ N`."]
def range_restrict (f : G →* N) : G →* f.range :=
monoid_hom.mk' (λ g, ⟨f g, ⟨g, rfl⟩⟩) $ λ a b, by {ext, exact f.map_mul' _ _}
@[simp, to_additive]
lemma coe_range_restrict (f : G →* N) (g : G) : (f.range_restrict g : N) = f g := rfl
@[to_additive]
lemma range_restrict_surjective (f : G →* N) : function.surjective f.range_restrict :=
λ ⟨_, g, rfl⟩, ⟨g, rfl⟩
@[to_additive]
lemma map_range (g : N →* P) (f : G →* N) : f.range.map g = (g.comp f).range :=
by rw [range_eq_map, range_eq_map]; exact (⊤ : subgroup G).map_map g f
@[to_additive]
lemma range_top_iff_surjective {N} [group N] {f : G →* N} :
f.range = (⊤ : subgroup N) ↔ function.surjective f :=
set_like.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective
/-- The range of a surjective monoid homomorphism is the whole of the codomain. -/
@[to_additive "The range of a surjective `add_monoid` homomorphism is the whole of the codomain."]
lemma range_top_of_surjective {N} [group N] (f : G →* N) (hf : function.surjective f) :
f.range = (⊤ : subgroup N) :=
range_top_iff_surjective.2 hf
@[simp, to_additive] lemma _root_.subgroup.subtype_range (H : subgroup G) : H.subtype.range = H :=
by { rw [range_eq_map, ← set_like.coe_set_eq, coe_map, subgroup.coe_subtype], ext, simp }
@[simp, to_additive] lemma _root_.subgroup.inclusion_range {H K : subgroup G} (h_le : H ≤ K) :
(inclusion h_le).range = H.subgroup_of K :=
subgroup.ext (λ g, set.ext_iff.mp (set.range_inclusion h_le) g)
/-- Restriction of a group hom to a subgroup of the domain. -/
@[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the domain."]
def restrict (f : G →* N) (H : subgroup G) : H →* N :=
f.comp H.subtype
@[simp, to_additive]
lemma restrict_apply {H : subgroup G} (f : G →* N) (x : H) :
f.restrict H x = f (x : G) := rfl
/-- Restriction of a group hom to a subgroup of the codomain. -/
@[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the codomain."]
def cod_restrict (f : G →* N) (S : subgroup N) (h : ∀ x, f x ∈ S) : G →* S :=
{ to_fun := λ n, ⟨f n, h n⟩,
map_one' := subtype.eq f.map_one,
map_mul' := λ x y, subtype.eq (f.map_mul x y) }
@[simp, to_additive]
lemma cod_restrict_apply {G : Type*} [group G] {N : Type*} [group N] (f : G →* N)
(S : subgroup N) (h : ∀ (x : G), f x ∈ S) {x : G} :
f.cod_restrict S h x = ⟨f x, h x⟩ := rfl
@[to_additive] lemma subgroup_of_range_eq_of_le {G₁ G₂ : Type*} [group G₁] [group G₂]
{K : subgroup G₂} (f : G₁ →* G₂) (h : f.range ≤ K) :
f.range.subgroup_of K = (f.cod_restrict K (λ x, h ⟨x, rfl⟩)).range :=
begin
ext k,
refine exists_congr _,
simp [subtype.ext_iff],
end
/-- Computable alternative to `monoid_hom.of_injective`. -/
@[to_additive /-"Computable alternative to `add_monoid_hom.of_injective`."-/]
def of_left_inverse {f : G →* N} {g : N →* G} (h : function.left_inverse g f) : G ≃* f.range :=
{ to_fun := f.range_restrict,
inv_fun := g ∘ f.range.subtype,
left_inv := h,
right_inv := by
{ rintros ⟨x, y, rfl⟩,
apply subtype.ext,
rw [coe_range_restrict, function.comp_apply, subgroup.coe_subtype, subtype.coe_mk, h] },
.. f.range_restrict }
@[simp, to_additive] lemma of_left_inverse_apply {f : G →* N} {g : N →* G}
(h : function.left_inverse g f) (x : G) :
↑(of_left_inverse h x) = f x := rfl
@[simp, to_additive] lemma of_left_inverse_symm_apply {f : G →* N} {g : N →* G}
(h : function.left_inverse g f) (x : f.range) :
(of_left_inverse h).symm x = g x := rfl
/-- The range of an injective group homomorphism is isomorphic to its domain. -/
@[to_additive /-"The range of an injective additive group homomorphism is isomorphic to its
domain."-/ ]
noncomputable def of_injective {f : G →* N} (hf : function.injective f) : G ≃* f.range :=
(mul_equiv.of_bijective (f.cod_restrict f.range (λ x, ⟨x, rfl⟩))
⟨λ x y h, hf (subtype.ext_iff.mp h), by { rintros ⟨x, y, rfl⟩, exact ⟨y, rfl⟩ }⟩)
@[to_additive]
lemma of_injective_apply {f : G →* N} (hf : function.injective f) {x : G} :
↑(of_injective hf x) = f x := rfl
section ker
variables {M : Type*} [mul_one_class M]
/-- The multiplicative kernel of a monoid homomorphism is the subgroup of elements `x : G` such that
`f x = 1` -/
@[to_additive "The additive kernel of an `add_monoid` homomorphism is the `add_subgroup` of elements
such that `f x = 0`"]
def ker (f : G →* M) : subgroup G :=
{ inv_mem' := λ x (hx : f x = 1),
calc f x⁻¹ = f x * f x⁻¹ : by rw [hx, one_mul]
... = f (x * x⁻¹) : by rw [f.map_mul]
... = f 1 : by rw [mul_right_inv]
... = 1 : f.map_one,
..f.mker }
@[to_additive]
lemma mem_ker (f : G →* M) {x : G} : x ∈ f.ker ↔ f x = 1 := iff.rfl
@[to_additive]
lemma coe_ker (f : G →* M) : (f.ker : set G) = (f : G → M) ⁻¹' {1} := rfl
@[to_additive]
lemma eq_iff (f : G →* N) {x y : G} : f x = f y ↔ y⁻¹ * x ∈ f.ker :=
by rw [f.mem_ker, f.map_mul, f.map_inv, inv_mul_eq_one, eq_comm]
@[to_additive]
instance decidable_mem_ker [decidable_eq M] (f : G →* M) :
decidable_pred (∈ f.ker) :=
λ x, decidable_of_iff (f x = 1) f.mem_ker
@[to_additive]
lemma comap_ker (g : N →* P) (f : G →* N) : g.ker.comap f = (g.comp f).ker := rfl
@[simp, to_additive] lemma comap_bot (f : G →* N) :
(⊥ : subgroup N).comap f = f.ker := rfl
@[to_additive] lemma range_restrict_ker (f : G →* N) : ker (range_restrict f) = ker f :=
begin
ext,
change (⟨f x, _⟩ : range f) = ⟨1, _⟩ ↔ f x = 1,
simp only [],
end
@[simp, to_additive]
lemma ker_one : (1 : G →* M).ker = ⊤ :=
by { ext, simp [mem_ker] }
@[to_additive] lemma ker_eq_bot_iff (f : G →* N) : f.ker = ⊥ ↔ function.injective f :=
begin
split,
{ intros h x y hxy,
rwa [←mul_inv_eq_one, ←map_inv, ←map_mul, ←mem_ker, h, mem_bot, mul_inv_eq_one] at hxy },
{ exact λ h, le_bot_iff.mp (λ x hx, h (hx.trans f.map_one.symm)) },
end
@[simp, to_additive] lemma _root_.subgroup.ker_subtype (H : subgroup G) : H.subtype.ker = ⊥ :=
H.subtype.ker_eq_bot_iff.mpr subtype.coe_injective
@[simp, to_additive] lemma _root_.subgroup.ker_inclusion {H K : subgroup G} (h : H ≤ K) :
(inclusion h).ker = ⊥ :=
(inclusion h).ker_eq_bot_iff.mpr (set.inclusion_injective h)
@[to_additive]
lemma prod_map_comap_prod {G' : Type*} {N' : Type*} [group G'] [group N']
(f : G →* N) (g : G' →* N') (S : subgroup N) (S' : subgroup N') :
(S.prod S').comap (prod_map f g) = (S.comap f).prod (S'.comap g) :=
set_like.coe_injective $ set.preimage_prod_map_prod f g _ _
@[to_additive]
lemma ker_prod_map {G' : Type*} {N' : Type*} [group G'] [group N'] (f : G →* N) (g : G' →* N') :
(prod_map f g).ker = f.ker.prod g.ker :=
by rw [←comap_bot, ←comap_bot, ←comap_bot, ←prod_map_comap_prod, bot_prod_bot]
end ker
/-- The subgroup of elements `x : G` such that `f x = g x` -/
@[to_additive "The additive subgroup of elements `x : G` such that `f x = g x`"]
def eq_locus (f g : G →* N) : subgroup G :=
{ inv_mem' := λ x (hx : f x = g x), show f x⁻¹ = g x⁻¹, by rw [f.map_inv, g.map_inv, hx],
.. eq_mlocus f g}
/-- If two monoid homomorphisms are equal on a set, then they are equal on its subgroup closure. -/
@[to_additive]
lemma eq_on_closure {f g : G →* N} {s : set G} (h : set.eq_on f g s) :
set.eq_on f g (closure s) :=
show closure s ≤ f.eq_locus g, from (closure_le _).2 h
@[to_additive]
lemma eq_of_eq_on_top {f g : G →* N} (h : set.eq_on f g (⊤ : subgroup G)) :
f = g :=
ext $ λ x, h trivial
@[to_additive]
lemma eq_of_eq_on_dense {s : set G} (hs : closure s = ⊤) {f g : G →* N} (h : s.eq_on f g) :
f = g :=
eq_of_eq_on_top $ hs ▸ eq_on_closure h
@[to_additive]
lemma gclosure_preimage_le (f : G →* N) (s : set N) :
closure (f ⁻¹' s) ≤ (closure s).comap f :=
(closure_le _).2 $ λ x hx, by rw [set_like.mem_coe, mem_comap]; exact subset_closure hx
/-- The image under a monoid homomorphism of the subgroup generated by a set equals the subgroup
generated by the image of the set. -/
@[to_additive "The image under an `add_monoid` hom of the `add_subgroup` generated by a set equals
the `add_subgroup` generated by the image of the set."]
lemma map_closure (f : G →* N) (s : set G) :
(closure s).map f = closure (f '' s) :=
set.image_preimage.l_comm_of_u_comm
(gc_map_comap f) (subgroup.gi N).gc (subgroup.gi G).gc (λ t, rfl)
-- this instance can't go just after the definition of `mrange` because `fintype` is
-- not imported at that stage
/-- The range of a finite monoid under a monoid homomorphism is finite.
Note: this instance can form a diamond with `subtype.fintype` in the
presence of `fintype N`. -/
@[to_additive "The range of a finite additive monoid under an additive monoid homomorphism is
finite.
Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the
presence of `fintype N`."]
instance fintype_mrange {M N : Type*} [monoid M] [monoid N] [fintype M] [decidable_eq N]
(f : M →* N) : fintype (mrange f) :=
set.fintype_range f
/-- The range of a finite group under a group homomorphism is finite.
Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the
presence of `fintype N`. -/
@[to_additive "The range of a finite additive group under an additive group homomorphism is finite.
Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the
presence of `fintype N`."]
instance fintype_range [fintype G] [decidable_eq N] (f : G →* N) : fintype (range f) :=
set.fintype_range f
end monoid_hom
namespace subgroup
variables {N : Type*} [group N] (H : subgroup G)
@[to_additive] lemma map_eq_bot_iff {f : G →* N} : H.map f = ⊥ ↔ H ≤ f.ker :=
begin
rw eq_bot_iff,
split,
{ exact λ h x hx, h ⟨x, hx, rfl⟩ },
{ intros h x hx,
obtain ⟨y, hy, rfl⟩ := hx,
exact h hy },
end
@[to_additive]
lemma map_eq_bot_iff_of_injective {f : G →* N} (hf : function.injective f) : H.map f = ⊥ ↔ H = ⊥ :=
by rw [map_eq_bot_iff, f.ker_eq_bot_iff.mpr hf, le_bot_iff]
end subgroup
namespace subgroup
open monoid_hom
variables {N : Type*} [group N] (f : G →* N)
@[to_additive]
lemma map_le_range (H : subgroup G) : map f H ≤ f.range :=
(range_eq_map f).symm ▸ map_mono le_top
@[to_additive]
lemma map_subtype_le {H : subgroup G} (K : subgroup H) : K.map H.subtype ≤ H :=
(K.map_le_range H.subtype).trans (le_of_eq H.subtype_range)
@[to_additive]
lemma ker_le_comap (H : subgroup N) : f.ker ≤ comap f H :=
(comap_bot f) ▸ comap_mono bot_le
@[to_additive]
lemma map_comap_le (H : subgroup N) : map f (comap f H) ≤ H :=
(gc_map_comap f).l_u_le _
@[to_additive]
lemma le_comap_map (H : subgroup G) : H ≤ comap f (map f H) :=
(gc_map_comap f).le_u_l _
@[to_additive]
lemma map_comap_eq (H : subgroup N) :
map f (comap f H) = f.range ⊓ H :=
set_like.ext' begin
convert set.image_preimage_eq_inter_range,
simp [set.inter_comm],
end
@[to_additive]
lemma comap_map_eq (H : subgroup G) : comap f (map f H) = H ⊔ f.ker :=
begin
refine le_antisymm _ (sup_le (le_comap_map _ _) (ker_le_comap _ _)),
intros x hx, simp only [exists_prop, mem_map, mem_comap] at hx,
rcases hx with ⟨y, hy, hy'⟩,
rw ← mul_inv_cancel_left y x,
exact mul_mem_sup hy (by simp [mem_ker, hy']),
end
@[to_additive]
lemma map_comap_eq_self {f : G →* N} {H : subgroup N} (h : H ≤ f.range) :
map f (comap f H) = H :=
by rwa [map_comap_eq, inf_eq_right]
@[to_additive]
lemma map_comap_eq_self_of_surjective {f : G →* N} (h : function.surjective f) (H : subgroup N) :
map f (comap f H) = H :=
map_comap_eq_self ((range_top_of_surjective _ h).symm ▸ le_top)
@[to_additive]
lemma comap_injective {f : G →* N} (h : function.surjective f) : function.injective (comap f) :=
λ K L hKL, by { apply_fun map f at hKL, simpa [map_comap_eq_self_of_surjective h] using hKL }
@[to_additive]
lemma comap_map_eq_self {f : G →* N} {H : subgroup G} (h : f.ker ≤ H) :
comap f (map f H) = H :=
by rwa [comap_map_eq, sup_eq_left]
@[to_additive]
lemma comap_map_eq_self_of_injective {f : G →* N} (h : function.injective f) (H : subgroup G) :
comap f (map f H) = H :=
comap_map_eq_self (((ker_eq_bot_iff _).mpr h).symm ▸ bot_le)
@[to_additive]
lemma map_le_map_iff_of_injective {f : G →* N} (hf : function.injective f) {H K : subgroup G} :
H.map f ≤ K.map f ↔ H ≤ K :=
⟨(congr_arg2 (≤) (H.comap_map_eq_self_of_injective hf)
(K.comap_map_eq_self_of_injective hf)).mp ∘ comap_mono, map_mono⟩
@[simp, to_additive]
lemma map_subtype_le_map_subtype {G' : subgroup G} {H K : subgroup G'} :
H.map G'.subtype ≤ K.map G'.subtype ↔ H ≤ K :=
map_le_map_iff_of_injective subtype.coe_injective
@[to_additive]
lemma map_injective {f : G →* N} (h : function.injective f) : function.injective (map f) :=
λ K L hKL, by { apply_fun comap f at hKL, simpa [comap_map_eq_self_of_injective h] using hKL }
@[to_additive]
lemma map_eq_comap_of_inverse {f : G →* N} {g : N →* G} (hl : function.left_inverse g f)
(hr : function.right_inverse g f) (H : subgroup G) : map f H = comap g H :=
set_like.ext' $ by rw [coe_map, coe_comap, set.image_eq_preimage_of_inverse hl hr]
/-- Given `f(A) = f(B)`, `ker f ≤ A`, and `ker f ≤ B`, deduce that `A = B` -/
@[to_additive] lemma map_injective_of_ker_le
{H K : subgroup G} (hH : f.ker ≤ H) (hK : f.ker ≤ K) (hf : map f H = map f K) :
H = K :=
begin
apply_fun comap f at hf,
rwa [comap_map_eq, comap_map_eq, sup_of_le_left hH, sup_of_le_left hK] at hf,
end
@[to_additive] lemma comap_sup_eq_of_le_range
{H K : subgroup N} (hH : H ≤ f.range) (hK : K ≤ f.range) :
comap f H ⊔ comap f K = comap f (H ⊔ K) :=
map_injective_of_ker_le f ((ker_le_comap f H).trans le_sup_left) (ker_le_comap f (H ⊔ K))
(by rw [map_comap_eq, map_sup, map_comap_eq, map_comap_eq, inf_eq_right.mpr hH,
inf_eq_right.mpr hK, inf_eq_right.mpr (sup_le hH hK)])
@[to_additive] lemma comap_sup_eq (H K : subgroup N) (hf : function.surjective f) :
comap f H ⊔ comap f K = comap f (H ⊔ K) :=
comap_sup_eq_of_le_range f (le_top.trans (ge_of_eq (f.range_top_of_surjective hf)))
(le_top.trans (ge_of_eq (f.range_top_of_surjective hf)))
@[to_additive] lemma sup_subgroup_of_eq {H K L : subgroup G} (hH : H ≤ L) (hK : K ≤ L) :
H.subgroup_of L ⊔ K.subgroup_of L = (H ⊔ K).subgroup_of L :=
comap_sup_eq_of_le_range L.subtype (hH.trans (ge_of_eq L.subtype_range))
(hK.trans (ge_of_eq L.subtype_range))
/-- A subgroup is isomorphic to its image under an injective function -/
@[to_additive "An additive subgroup is isomorphic to its image under an injective function"]
noncomputable def equiv_map_of_injective (H : subgroup G)
(f : G →* N) (hf : function.injective f) : H ≃* H.map f :=
{ map_mul' := λ _ _, subtype.ext (f.map_mul _ _), ..equiv.set.image f H hf }
@[simp, to_additive] lemma coe_equiv_map_of_injective_apply (H : subgroup G)
(f : G →* N) (hf : function.injective f) (h : H) :
(equiv_map_of_injective H f hf h : N) = f h := rfl
/-- The preimage of the normalizer is equal to the normalizer of the preimage of a surjective
function. -/
@[to_additive "The preimage of the normalizer is equal to the normalizer of the preimage of
a surjective function."]
lemma comap_normalizer_eq_of_surjective (H : subgroup G)
{f : N →* G} (hf : function.surjective f) :
H.normalizer.comap f = (H.comap f).normalizer :=
le_antisymm (le_normalizer_comap f)
begin
assume x hx,
simp only [mem_comap, mem_normalizer_iff] at *,
assume n,
rcases hf n with ⟨y, rfl⟩,
simp [hx y]
end
@[to_additive]
lemma comap_normalizer_eq_of_injective_of_le_range {N : Type*} [group N] (H : subgroup G)
{f : N →* G} (hf : function.injective f) (h : H.normalizer ≤ f.range) :
comap f H.normalizer = (comap f H).normalizer :=
begin
apply (subgroup.map_injective hf),
rw map_comap_eq_self h,
apply le_antisymm,
{ refine (le_trans (le_of_eq _) (map_mono (le_normalizer_comap _))),
rewrite map_comap_eq_self h, },
{ refine (le_trans (le_normalizer_map f) (le_of_eq _)),
rewrite map_comap_eq_self (le_trans le_normalizer h), }
end
@[to_additive]
lemma comap_subtype_normalizer_eq {H N : subgroup G} (h : H.normalizer ≤ N) :
comap N.subtype H.normalizer = (comap N.subtype H).normalizer :=
begin
apply comap_normalizer_eq_of_injective_of_le_range,
exact subtype.coe_injective,
simpa,
end
/-- The image of the normalizer is equal to the normalizer of the image of an isomorphism. -/
@[to_additive "The image of the normalizer is equal to the normalizer of the image of an
isomorphism."]
lemma map_equiv_normalizer_eq (H : subgroup G)
(f : G ≃* N) : H.normalizer.map f.to_monoid_hom = (H.map f.to_monoid_hom).normalizer :=
begin
ext x,
simp only [mem_normalizer_iff, mem_map_equiv],
rw [f.to_equiv.forall_congr],
simp
end
/-- The image of the normalizer is equal to the normalizer of the image of a bijective
function. -/
@[to_additive "The image of the normalizer is equal to the normalizer of the image of a bijective
function."]
lemma map_normalizer_eq_of_bijective (H : subgroup G)
{f : G →* N} (hf : function.bijective f) :
H.normalizer.map f = (H.map f).normalizer :=
map_equiv_normalizer_eq H (mul_equiv.of_bijective f hf)
end subgroup
namespace monoid_hom
variables {G₁ G₂ G₃ : Type*} [group G₁] [group G₂] [group G₃]
variables (f : G₁ →* G₂) (f_inv : G₂ → G₁)
/-- Auxiliary definition used to define `lift_of_right_inverse` -/
@[to_additive "Auxiliary definition used to define `lift_of_right_inverse`"]
def lift_of_right_inverse_aux
(hf : function.right_inverse f_inv f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) :
G₂ →* G₃ :=
{ to_fun := λ b, g (f_inv b),
map_one' := hg (hf 1),
map_mul' :=
begin
intros x y,
rw [← g.map_mul, ← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker],
apply hg,
rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one, f.map_mul],
simp only [hf _],
end }
@[simp, to_additive]
lemma lift_of_right_inverse_aux_comp_apply
(hf : function.right_inverse f_inv f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) (x : G₁) :
(f.lift_of_right_inverse_aux f_inv hf g hg) (f x) = g x :=
begin
dsimp [lift_of_right_inverse_aux],
rw [← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker],
apply hg,
rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one],
simp only [hf _],
end
/-- `lift_of_right_inverse f hf g hg` is the unique group homomorphism `φ`
* such that `φ.comp f = g` (`monoid_hom.lift_of_right_inverse_comp`),
* where `f : G₁ →+* G₂` has a right_inverse `f_inv` (`hf`),
* and `g : G₂ →+* G₃` satisfies `hg : f.ker ≤ g.ker`.
See `monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma.
```
G₁.
| \
f | \ g
| \
v \⌟
G₂----> G₃
∃!φ
```
-/
@[to_additive "`lift_of_right_inverse f f_inv hf g hg` is the unique additive group homomorphism `φ`
* such that `φ.comp f = g` (`add_monoid_hom.lift_of_right_inverse_comp`),
* where `f : G₁ →+ G₂` has a right_inverse `f_inv` (`hf`),
* and `g : G₂ →+ G₃` satisfies `hg : f.ker ≤ g.ker`.
See `add_monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma.
```
G₁.
| \\
f | \\ g
| \\
v \\⌟
G₂----> G₃
∃!φ
```"]
def lift_of_right_inverse
(hf : function.right_inverse f_inv f) : {g : G₁ →* G₃ // f.ker ≤ g.ker} ≃ (G₂ →* G₃) :=
{ to_fun := λ g, f.lift_of_right_inverse_aux f_inv hf g.1 g.2,
inv_fun := λ φ, ⟨φ.comp f, λ x hx, (mem_ker _).mpr $ by simp [(mem_ker _).mp hx]⟩,
left_inv := λ g, by
{ ext,
simp only [comp_apply, lift_of_right_inverse_aux_comp_apply, subtype.coe_mk,
subtype.val_eq_coe], },
right_inv := λ φ, by
{ ext b,
simp [lift_of_right_inverse_aux, hf b], } }
/-- A non-computable version of `monoid_hom.lift_of_right_inverse` for when no computable right
inverse is available, that uses `function.surj_inv`. -/
@[simp, to_additive "A non-computable version of `add_monoid_hom.lift_of_right_inverse` for when no
computable right inverse is available."]
noncomputable abbreviation lift_of_surjective
(hf : function.surjective f) : {g : G₁ →* G₃ // f.ker ≤ g.ker} ≃ (G₂ →* G₃) :=
f.lift_of_right_inverse (function.surj_inv hf) (function.right_inverse_surj_inv hf)
@[simp, to_additive]
lemma lift_of_right_inverse_comp_apply
(hf : function.right_inverse f_inv f) (g : {g : G₁ →* G₃ // f.ker ≤ g.ker}) (x : G₁) :
(f.lift_of_right_inverse f_inv hf g) (f x) = g x :=
f.lift_of_right_inverse_aux_comp_apply f_inv hf g.1 g.2 x
@[simp, to_additive]
lemma lift_of_right_inverse_comp (hf : function.right_inverse f_inv f)
(g : {g : G₁ →* G₃ // f.ker ≤ g.ker}) :
(f.lift_of_right_inverse f_inv hf g).comp f = g :=
monoid_hom.ext $ f.lift_of_right_inverse_comp_apply f_inv hf g
@[to_additive]
lemma eq_lift_of_right_inverse (hf : function.right_inverse f_inv f) (g : G₁ →* G₃)
(hg : f.ker ≤ g.ker) (h : G₂ →* G₃) (hh : h.comp f = g) :
h = (f.lift_of_right_inverse f_inv hf ⟨g, hg⟩) :=
begin
simp_rw ←hh,
exact ((f.lift_of_right_inverse f_inv hf).apply_symm_apply _).symm,
end
end monoid_hom
variables {N : Type*} [group N]
-- Here `H.normal` is an explicit argument so we can use dot notation with `comap`.
@[to_additive]
lemma subgroup.normal.comap {H : subgroup N} (hH : H.normal) (f : G →* N) :
(H.comap f).normal :=
⟨λ _, by simp [subgroup.mem_comap, hH.conj_mem] {contextual := tt}⟩
@[priority 100, to_additive]
instance subgroup.normal_comap {H : subgroup N}
[nH : H.normal] (f : G →* N) : (H.comap f).normal := nH.comap _
@[priority 100, to_additive]
instance monoid_hom.normal_ker (f : G →* N) : f.ker.normal :=
by { rw [←f.comap_bot], apply_instance }
@[priority 100, to_additive]
instance subgroup.normal_inf (H N : subgroup G) [hN : N.normal] :
((H ⊓ N).comap H.subtype).normal :=
⟨λ x hx g, begin
simp only [subgroup.mem_inf, coe_subtype, subgroup.mem_comap] at hx,
simp only [subgroup.coe_mul, subgroup.mem_inf, coe_subtype, subgroup.coe_inv, subgroup.mem_comap],
exact ⟨H.mul_mem (H.mul_mem g.2 hx.1) (H.inv_mem g.2), hN.1 x hx.2 g⟩,
end⟩
namespace subgroup
/-- The subgroup generated by an element. -/
def zpowers (g : G) : subgroup G :=
subgroup.copy (zpowers_hom G g).range (set.range ((^) g : ℤ → G)) rfl
@[simp] lemma mem_zpowers (g : G) : g ∈ zpowers g := ⟨1, zpow_one _⟩
lemma zpowers_eq_closure (g : G) : zpowers g = closure {g} :=
by { ext, exact mem_closure_singleton.symm }
@[simp] lemma range_zpowers_hom (g : G) : (zpowers_hom G g).range = zpowers g := rfl
lemma zpowers_subset {a : G} {K : subgroup G} (h : a ∈ K) : zpowers a ≤ K :=
λ x hx, match x, hx with _, ⟨i, rfl⟩ := K.zpow_mem h i end
lemma mem_zpowers_iff {g h : G} :
h ∈ zpowers g ↔ ∃ (k : ℤ), g ^ k = h :=
iff.rfl
@[simp] lemma forall_zpowers {x : G} {p : zpowers x → Prop} :
(∀ g, p g) ↔ ∀ m : ℤ, p ⟨x ^ m, m, rfl⟩ :=
set.forall_subtype_range_iff
@[simp] lemma exists_zpowers {x : G} {p : zpowers x → Prop} :
(∃ g, p g) ↔ ∃ m : ℤ, p ⟨x ^ m, m, rfl⟩ :=
set.exists_subtype_range_iff
lemma forall_mem_zpowers {x : G} {p : G → Prop} :
(∀ g ∈ zpowers x, p g) ↔ ∀ m : ℤ, p (x ^ m) :=
set.forall_range_iff
lemma exists_mem_zpowers {x : G} {p : G → Prop} :
(∃ g ∈ zpowers x, p g) ↔ ∃ m : ℤ, p (x ^ m) :=
set.exists_range_iff
end subgroup
namespace add_subgroup
/-- The subgroup generated by an element. -/
def zmultiples (a : A) : add_subgroup A :=
add_subgroup.copy (zmultiples_hom A a).range (set.range ((• a) : ℤ → A)) rfl
@[simp] lemma range_zmultiples_hom (a : A) : (zmultiples_hom A a).range = zmultiples a := rfl
attribute [to_additive add_subgroup.zmultiples] subgroup.zpowers
attribute [to_additive add_subgroup.mem_zmultiples] subgroup.mem_zpowers
attribute [to_additive add_subgroup.zmultiples_eq_closure] subgroup.zpowers_eq_closure
attribute [to_additive add_subgroup.range_zmultiples_hom] subgroup.range_zpowers_hom
attribute [to_additive add_subgroup.zmultiples_subset] subgroup.zpowers_subset
attribute [to_additive add_subgroup.mem_zmultiples_iff] subgroup.mem_zpowers_iff
attribute [to_additive add_subgroup.forall_zmultiples] subgroup.forall_zpowers
attribute [to_additive add_subgroup.forall_mem_zmultiples] subgroup.forall_mem_zpowers
attribute [to_additive add_subgroup.exists_zmultiples] subgroup.exists_zpowers
attribute [to_additive add_subgroup.exists_mem_zmultiples] subgroup.exists_mem_zpowers
end add_subgroup
lemma int.mem_zmultiples_iff {a b : ℤ} :
b ∈ add_subgroup.zmultiples a ↔ a ∣ b :=
exists_congr (λ k, by rw [mul_comm, eq_comm, ← smul_eq_mul])
lemma of_mul_image_zpowers_eq_zmultiples_of_mul { x : G } :
additive.of_mul '' ((subgroup.zpowers x) : set G) = add_subgroup.zmultiples (additive.of_mul x) :=
begin
ext y,
split,
{ rintro ⟨z, ⟨m, hm⟩, hz2⟩,
use m,
simp only,
rwa [← of_mul_zpow, hm] },
{ rintros ⟨n, hn⟩,
refine ⟨x ^ n, ⟨n, rfl⟩, _⟩,
rwa of_mul_zpow }
end
lemma of_add_image_zmultiples_eq_zpowers_of_add {x : A} :
multiplicative.of_add '' ((add_subgroup.zmultiples x) : set A) =
subgroup.zpowers (multiplicative.of_add x) :=
begin
symmetry,
rw equiv.eq_image_iff_symm_image_eq,
exact of_mul_image_zpowers_eq_zmultiples_of_mul,
end
namespace subgroup
@[to_additive zmultiples_is_commutative]
instance zpowers_is_commutative (g : G) : (zpowers g).is_commutative :=
⟨⟨λ ⟨_, _, h₁⟩ ⟨_, _, h₂⟩, by rw [subtype.ext_iff, coe_mul, coe_mul,
subtype.coe_mk, subtype.coe_mk, ←h₁, ←h₂, zpow_mul_comm]⟩⟩
@[simp, to_additive zmultiples_le, simp]
lemma zpowers_le {g : G} {H : subgroup G} : zpowers g ≤ H ↔ g ∈ H :=
by rw [zpowers_eq_closure, closure_le, set.singleton_subset_iff, set_like.mem_coe]
end subgroup
namespace monoid_hom
variables {G' : Type*} [group G']
/-- The `monoid_hom` from the preimage of a subgroup to itself. -/
@[to_additive "the `add_monoid_hom` from the preimage of an additive subgroup to itself.", simps]
def subgroup_comap (f : G →* G') (H' : subgroup G') : H'.comap f →* H' :=
f.submonoid_comap H'.to_submonoid
/-- The `monoid_hom` from a subgroup to its image. -/
@[to_additive "the `add_monoid_hom` from an additive subgroup to its image", simps]
def subgroup_map (f : G →* G') (H : subgroup G) : H →* H.map f :=
f.submonoid_map H.to_submonoid
@[to_additive]
lemma subgroup_map_surjective (f : G →* G') (H : subgroup G) :
function.surjective (f.subgroup_map H) :=
f.submonoid_map_surjective H.to_submonoid
end monoid_hom
namespace mul_equiv
variables {H K : subgroup G}
/-- Makes the identity isomorphism from a proof two subgroups of a multiplicative
group are equal. -/
@[to_additive "Makes the identity additive isomorphism from a proof
two subgroups of an additive group are equal."]
def subgroup_congr (h : H = K) : H ≃* K :=
{ map_mul' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h }
/-- A `mul_equiv` `φ` between two groups `G` and `G'` induces a `mul_equiv` between
a subgroup `H ≤ G` and the subgroup `φ(H) ≤ G'`. -/
@[to_additive "An `add_equiv` `φ` between two additive groups `G` and `G'` induces an `add_equiv`
between a subgroup `H ≤ G` and the subgroup `φ(H) ≤ G'`. "]
def subgroup_map {G'} [group G'] (e : G ≃* G') (H : subgroup G) :
H ≃* H.map e.to_monoid_hom :=
e.submonoid_map H.to_submonoid
end mul_equiv
-- TODO : ↥(⊤ : subgroup H) ≃* H ?
namespace subgroup
variables {C : Type*} [comm_group C] {s t : subgroup C} {x : C}
@[to_additive]
lemma mem_sup : x ∈ s ⊔ t ↔ ∃ (y ∈ s) (z ∈ t), y * z = x :=
⟨λ h, begin
rw [← closure_eq s, ← closure_eq t, ← closure_union] at h,
apply closure_induction h,
{ rintro y (h | h),
{ exact ⟨y, h, 1, t.one_mem, by simp⟩ },
{ exact ⟨1, s.one_mem, y, h, by simp⟩ } },
{ exact ⟨1, s.one_mem, 1, ⟨t.one_mem, mul_one 1⟩⟩ },
{ rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩,
exact ⟨_, mul_mem hy₁ hy₂, _, mul_mem hz₁ hz₂, by simp [mul_assoc]; cc⟩ },
{ rintro _ ⟨y, hy, z, hz, rfl⟩,
exact ⟨_, inv_mem hy, _, inv_mem hz, mul_comm z y ▸ (mul_inv_rev z y).symm⟩ }
end, by rintro ⟨y, hy, z, hz, rfl⟩; exact mul_mem_sup hy hz⟩
@[to_additive]
lemma mem_sup' : x ∈ s ⊔ t ↔ ∃ (y : s) (z : t), (y:C) * z = x :=
mem_sup.trans $ by simp only [set_like.exists, coe_mk]
@[to_additive]
lemma mem_closure_pair {x y z : C} : z ∈ closure ({x, y} : set C) ↔ ∃ m n : ℤ, x ^ m * y ^ n = z :=
begin
rw [←set.singleton_union, subgroup.closure_union, mem_sup],
simp_rw [exists_prop, mem_closure_singleton, exists_exists_eq_and],
end
@[to_additive]
instance : is_modular_lattice (subgroup C) :=
⟨λ x y z xz a ha, begin
rw [mem_inf, mem_sup] at ha,
rcases ha with ⟨⟨b, hb, c, hc, rfl⟩, haz⟩,
rw mem_sup,
refine ⟨b, hb, c, mem_inf.2 ⟨hc, _⟩, rfl⟩,
rw ← inv_mul_cancel_left b c,
apply z.mul_mem (z.inv_mem (xz hb)) haz,
end⟩
end subgroup
section
variables (G) (A)
/-- A `group` is simple when it has exactly two normal `subgroup`s. -/
class is_simple_group extends nontrivial G : Prop :=
(eq_bot_or_eq_top_of_normal : ∀ H : subgroup G, H.normal → H = ⊥ ∨ H = ⊤)
/-- An `add_group` is simple when it has exactly two normal `add_subgroup`s. -/
class is_simple_add_group extends nontrivial A : Prop :=
(eq_bot_or_eq_top_of_normal : ∀ H : add_subgroup A, H.normal → H = ⊥ ∨ H = ⊤)
attribute [to_additive] is_simple_group
variables {G} {A}
@[to_additive]
lemma subgroup.normal.eq_bot_or_eq_top [is_simple_group G] {H : subgroup G} (Hn : H.normal) :
H = ⊥ ∨ H = ⊤ :=
is_simple_group.eq_bot_or_eq_top_of_normal H Hn
namespace is_simple_group
@[to_additive]
instance {C : Type*} [comm_group C] [is_simple_group C] :
is_simple_order (subgroup C) :=
⟨λ H, H.normal_of_comm.eq_bot_or_eq_top⟩
open _root_.subgroup
@[to_additive]
lemma is_simple_group_of_surjective {H : Type*} [group H] [is_simple_group G]
[nontrivial H] (f : G →* H) (hf : function.surjective f) :
is_simple_group H :=
⟨nontrivial.exists_pair_ne, λ H iH, begin
refine ((iH.comap f).eq_bot_or_eq_top).imp (λ h, _) (λ h, _),
{ rw [←map_bot f, ←h, map_comap_eq_self_of_surjective hf] },
{ rw [←comap_top f] at h, exact comap_injective hf h }
end⟩
end is_simple_group
end
namespace subgroup
section pointwise
@[to_additive]
lemma closure_mul_le (S T : set G) : closure (S * T) ≤ closure S ⊔ closure T :=
Inf_le $ λ x ⟨s, t, hs, ht, hx⟩, hx ▸ (closure S ⊔ closure T).mul_mem
(set_like.le_def.mp le_sup_left $ subset_closure hs)
(set_like.le_def.mp le_sup_right $ subset_closure ht)
@[to_additive]
lemma sup_eq_closure (H K : subgroup G) : H ⊔ K = closure (H * K) :=
le_antisymm
(sup_le
(λ h hh, subset_closure ⟨h, 1, hh, K.one_mem, mul_one h⟩)
(λ k hk, subset_closure ⟨1, k, H.one_mem, hk, one_mul k⟩))
(by conv_rhs { rw [← closure_eq H, ← closure_eq K] }; apply closure_mul_le)
@[to_additive]
private def mul_normal_aux (H N : subgroup G) [hN : N.normal] : subgroup G :=
{ carrier := (H : set G) * N,
one_mem' := ⟨1, 1, H.one_mem, N.one_mem, by rw mul_one⟩,
mul_mem' := λ a b ⟨h, n, hh, hn, ha⟩ ⟨h', n', hh', hn', hb⟩,
⟨h * h', h'⁻¹ * n * h' * n',
H.mul_mem hh hh', N.mul_mem (by simpa using hN.conj_mem _ hn h'⁻¹) hn',
by simp [← ha, ← hb, mul_assoc]⟩,
inv_mem' := λ x ⟨h, n, hh, hn, hx⟩,
⟨h⁻¹, h * n⁻¹ * h⁻¹, H.inv_mem hh, hN.conj_mem _ (N.inv_mem hn) h,
by rw [mul_assoc h, inv_mul_cancel_left, ← hx, mul_inv_rev]⟩ }
/-- The carrier of `H ⊔ N` is just `↑H * ↑N` (pointwise set product) when `N` is normal. -/
@[to_additive "The carrier of `H ⊔ N` is just `↑H + ↑N` (pointwise set addition)
when `N` is normal."]
lemma mul_normal (H N : subgroup G) [N.normal] : (↑(H ⊔ N) : set G) = H * N :=
set.subset.antisymm
(show H ⊔ N ≤ mul_normal_aux H N,
by { rw sup_eq_closure, apply Inf_le _, dsimp, refl })
((sup_eq_closure H N).symm ▸ subset_closure)
@[to_additive]
private def normal_mul_aux (N H : subgroup G) [hN : N.normal] : subgroup G :=
{ carrier := (N : set G) * H,
one_mem' := ⟨1, 1, N.one_mem, H.one_mem, by rw mul_one⟩,
mul_mem' := λ a b ⟨n, h, hn, hh, ha⟩ ⟨n', h', hn', hh', hb⟩,
⟨n * (h * n' * h⁻¹), h * h',
N.mul_mem hn (hN.conj_mem _ hn' _), H.mul_mem hh hh',
by simp [← ha, ← hb, mul_assoc]⟩,
inv_mem' := λ x ⟨n, h, hn, hh, hx⟩,
⟨h⁻¹ * n⁻¹ * h, h⁻¹,
by simpa using hN.conj_mem _ (N.inv_mem hn) h⁻¹, H.inv_mem hh,
by rw [mul_inv_cancel_right, ← mul_inv_rev, hx]⟩ }
/-- The carrier of `N ⊔ H` is just `↑N * ↑H` (pointwise set product) when `N` is normal. -/
@[to_additive "The carrier of `N ⊔ H` is just `↑N + ↑H` (pointwise set addition)
when `N` is normal."]
lemma normal_mul (N H : subgroup G) [N.normal] : (↑(N ⊔ H) : set G) = N * H :=
set.subset.antisymm
(show N ⊔ H ≤ normal_mul_aux N H,
by { rw sup_eq_closure, apply Inf_le _, dsimp, refl })
((sup_eq_closure N H).symm ▸ subset_closure)
@[to_additive] lemma mul_inf_assoc (A B C : subgroup G) (h : A ≤ C) :
(A : set G) * ↑(B ⊓ C) = (A * B) ⊓ C :=
begin
ext,
simp only [coe_inf, set.inf_eq_inter, set.mem_mul, set.mem_inter_iff],
split,
{ rintros ⟨y, z, hy, ⟨hzB, hzC⟩, rfl⟩,
refine ⟨_, mul_mem (h hy) hzC⟩,
exact ⟨y, z, hy, hzB, rfl⟩ },
rintros ⟨⟨y, z, hy, hz, rfl⟩, hyz⟩,
refine ⟨y, z, hy, ⟨hz, _⟩, rfl⟩,
suffices : y⁻¹ * (y * z) ∈ C, { simpa },
exact mul_mem (inv_mem (h hy)) hyz
end
@[to_additive] lemma inf_mul_assoc (A B C : subgroup G) (h : C ≤ A) :
((A ⊓ B : subgroup G) : set G) * C = A ⊓ (B * C) :=
begin
ext,
simp only [coe_inf, set.inf_eq_inter, set.mem_mul, set.mem_inter_iff],
split,
{ rintros ⟨y, z, ⟨hyA, hyB⟩, hz, rfl⟩,
refine ⟨A.mul_mem hyA (h hz), _⟩,
exact ⟨y, z, hyB, hz, rfl⟩ },
rintros ⟨hyz, y, z, hy, hz, rfl⟩,
refine ⟨y, z, ⟨_, hy⟩, hz, rfl⟩,
suffices : (y * z) * z⁻¹ ∈ A, { simpa },
exact mul_mem hyz (inv_mem (h hz))
end
end pointwise
section subgroup_normal
@[to_additive] lemma normal_subgroup_of_iff {H K : subgroup G} (hHK : H ≤ K) :
(H.subgroup_of K).normal ↔ ∀ h k, h ∈ H → k ∈ K → k * h * k⁻¹ ∈ H :=
⟨λ hN h k hH hK, hN.conj_mem ⟨h, hHK hH⟩ hH ⟨k, hK⟩,
λ hN, { conj_mem := λ h hm k, (hN h.1 k.1 hm k.2) }⟩
@[to_additive] instance prod_subgroup_of_prod_normal
{H₁ K₁ : subgroup G} {H₂ K₂ : subgroup N}
[h₁ : (H₁.subgroup_of K₁).normal] [h₂ : (H₂.subgroup_of K₂).normal] :
((H₁.prod H₂).subgroup_of (K₁.prod K₂)).normal :=
{ conj_mem := λ n hgHK g,
⟨h₁.conj_mem ⟨(n : G × N).fst, (mem_prod.mp n.2).1⟩
hgHK.1 ⟨(g : G × N).fst, (mem_prod.mp g.2).1⟩,
h₂.conj_mem ⟨(n : G × N).snd, (mem_prod.mp n.2).2⟩
hgHK.2 ⟨(g : G × N).snd, (mem_prod.mp g.2).2⟩⟩ }
@[to_additive] instance prod_normal
(H : subgroup G) (K : subgroup N) [hH : H.normal] [hK : K.normal] :
(H.prod K).normal :=
{ conj_mem := λ n hg g,
⟨hH.conj_mem n.fst (subgroup.mem_prod.mp hg).1 g.fst,
hK.conj_mem n.snd (subgroup.mem_prod.mp hg).2 g.snd⟩ }
@[to_additive] lemma inf_subgroup_of_inf_normal_of_right
(A B' B : subgroup G) (hB : B' ≤ B) [hN : (B'.subgroup_of B).normal] :
((A ⊓ B').subgroup_of (A ⊓ B)).normal :=
{ conj_mem := λ n hn g,
⟨mul_mem (mul_mem (mem_inf.1 g.2).1 (mem_inf.1 n.2).1) (inv_mem (mem_inf.1 g.2).1),
(normal_subgroup_of_iff hB).mp hN n g hn.2 (mem_inf.mp g.2).2⟩ }
@[to_additive] lemma inf_subgroup_of_inf_normal_of_left
{A' A : subgroup G} (B : subgroup G) (hA : A' ≤ A) [hN : (A'.subgroup_of A).normal] :
((A' ⊓ B).subgroup_of (A ⊓ B)).normal :=
{ conj_mem := λ n hn g,
⟨(normal_subgroup_of_iff hA).mp hN n g hn.1 (mem_inf.mp g.2).1,
mul_mem (mul_mem (mem_inf.1 g.2).2 (mem_inf.1 n.2).2) (inv_mem (mem_inf.1 g.2).2)⟩ }
instance sup_normal (H K : subgroup G) [hH : H.normal] [hK : K.normal] : (H ⊔ K).normal :=
{ conj_mem := λ n hmem g,
begin
change n ∈ ↑(H ⊔ K) at hmem,
change g * n * g⁻¹ ∈ ↑(H ⊔ K),
rw [normal_mul, set.mem_mul] at *,
rcases hmem with ⟨h, k, hh, hk, rfl⟩,
refine ⟨g * h * g⁻¹, g * k * g⁻¹, hH.conj_mem h hh g, hK.conj_mem k hk g, _⟩,
simp
end }
@[to_additive] instance normal_inf_normal (H K : subgroup G) [hH : H.normal] [hK : K.normal] :
(H ⊓ K).normal :=
{ conj_mem := λ n hmem g,
by { rw mem_inf at *, exact ⟨hH.conj_mem n hmem.1 g, hK.conj_mem n hmem.2 g⟩ } }
@[to_additive] lemma subgroup_of_sup (A A' B : subgroup G) (hA : A ≤ B) (hA' : A' ≤ B) :
(A ⊔ A').subgroup_of B = A.subgroup_of B ⊔ A'.subgroup_of B :=
begin
refine map_injective_of_ker_le B.subtype
(ker_le_comap _ _) (le_trans (ker_le_comap B.subtype _) le_sup_left) _,
{ simp only [subgroup_of, map_comap_eq, map_sup, subtype_range],
rw [inf_of_le_right (sup_le hA hA'), inf_of_le_right hA', inf_of_le_right hA] },
end
@[to_additive] lemma subgroup_normal.mem_comm {H K : subgroup G}
(hK : H ≤ K) [hN : (H.subgroup_of K).normal] {a b : G} (hb : b ∈ K) (h : a * b ∈ H) :
b * a ∈ H :=
begin
have := (normal_subgroup_of_iff hK).mp hN (a * b) b h hb,
rwa [mul_assoc, mul_assoc, mul_right_inv, mul_one] at this,
end
/-- Elements of disjoint, normal subgroups commute -/
@[to_additive] lemma commute_of_normal_of_disjoint
(H₁ H₂ : subgroup G) (hH₁ : H₁.normal) (hH₂ : H₂.normal) (hdis : disjoint H₁ H₂)
(x y : G) (hx : x ∈ H₁) (hy : y ∈ H₂) :
commute x y :=
begin
suffices : x * y * x⁻¹ * y⁻¹ = 1,
{ show x * y = y * x, by { rw [mul_assoc, mul_eq_one_iff_eq_inv] at this, simpa } },
apply hdis, split,
{ suffices : x * (y * x⁻¹ * y⁻¹) ∈ H₁, by simpa [mul_assoc],
exact H₁.mul_mem hx (hH₁.conj_mem _ (H₁.inv_mem hx) _) },
{ show x * y * x⁻¹ * y⁻¹ ∈ H₂,
apply H₂.mul_mem _ (H₂.inv_mem hy),
apply (hH₂.conj_mem _ hy), }
end
end subgroup_normal
@[to_additive]
lemma disjoint_def {H₁ H₂ : subgroup G} :
disjoint H₁ H₂ ↔ ∀ {x : G}, x ∈ H₁ → x ∈ H₂ → x = 1 :=
show (∀ x, x ∈ H₁ ∧ x ∈ H₂ → x ∈ ({1} : set G)) ↔ _, by simp
@[to_additive]
lemma disjoint_def' {H₁ H₂ : subgroup G} :
disjoint H₁ H₂ ↔ ∀ {x y : G}, x ∈ H₁ → y ∈ H₂ → x = y → x = 1 :=
disjoint_def.trans ⟨λ h x y hx hy hxy, h hx $ hxy.symm ▸ hy,
λ h x hx hx', h hx hx' rfl⟩
@[to_additive]
lemma disjoint_iff_mul_eq_one {H₁ H₂ : subgroup G} :
disjoint H₁ H₂ ↔ ∀ {x y : G}, x ∈ H₁ → y ∈ H₂ → x * y = 1 → x = 1 ∧ y = 1 :=
disjoint_def'.trans ⟨λ h x y hx hy hxy,
let hx1 : x = 1 := h hx (H₂.inv_mem hy) (eq_inv_iff_mul_eq_one.mpr hxy) in
⟨hx1, by simpa [hx1] using hxy⟩,
λ h x y hx hy hxy, (h hx (H₂.inv_mem hy) (mul_inv_eq_one.mpr hxy)).1 ⟩
/-- `finset.noncomm_prod` is “injective” in `f` if `f` maps into independent subgroups. This
generalizes (one direction of) `subgroup.disjoint_iff_mul_eq_one`. -/
@[to_additive "`finset.noncomm_sum` is “injective” in `f` if `f` maps into independent subgroups.
This generalizes (one direction of) `add_subgroup.disjoint_iff_add_eq_zero`. "]
lemma eq_one_of_noncomm_prod_eq_one_of_independent {ι : Type*}
(s : finset ι) (f : ι → G) (comm : ∀ (x ∈ s) (y ∈ s), commute (f x) (f y))
(K : ι → subgroup G) (hind : complete_lattice.independent K) (hmem : ∀ (x ∈ s), f x ∈ K x)
(heq1 : s.noncomm_prod f comm = 1) : ∀ (i ∈ s), f i = 1 :=
begin
classical,
revert heq1,
induction s using finset.induction_on with i s hnmem ih,
{ simp, },
{ simp only [finset.forall_mem_insert] at comm hmem,
specialize ih (λ x hx, (comm.2 x hx).2) hmem.2,
have hmem_bsupr: s.noncomm_prod f (λ x hx, (comm.2 x hx).2) ∈ ⨆ (i ∈ (s : set ι)), K i,
{ refine subgroup.noncomm_prod_mem _ _ _,
intros x hx,
have : K x ≤ ⨆ (i ∈ (s : set ι)), K i := le_supr₂ x hx,
exact this (hmem.2 x hx), },
intro heq1,
rw finset.noncomm_prod_insert_of_not_mem _ _ _ _ hnmem at heq1,
have hnmem' : i ∉ (s : set ι), by simpa,
obtain ⟨heq1i : f i = 1, heq1S : s.noncomm_prod f _ = 1⟩ :=
subgroup.disjoint_iff_mul_eq_one.mp (hind.disjoint_bsupr hnmem') hmem.1 hmem_bsupr heq1,
specialize ih heq1S,
intros i h,
simp only [finset.mem_insert] at h,
rcases h with ⟨rfl | _⟩,
{ exact heq1i },
{ exact (ih _ h), } }
end
end subgroup
namespace is_conj
open subgroup
lemma normal_closure_eq_top_of {N : subgroup G} [hn : N.normal]
{g g' : G} {hg : g ∈ N} {hg' : g' ∈ N} (hc : is_conj g g')
(ht : normal_closure ({⟨g, hg⟩} : set N) = ⊤) :
normal_closure ({⟨g', hg'⟩} : set N) = ⊤ :=
begin
obtain ⟨c, rfl⟩ := is_conj_iff.1 hc,
have h : ∀ x : N, (mul_aut.conj c) x ∈ N,
{ rintro ⟨x, hx⟩,
exact hn.conj_mem _ hx c },
have hs : function.surjective (((mul_aut.conj c).to_monoid_hom.restrict N).cod_restrict _ h),
{ rintro ⟨x, hx⟩,
refine ⟨⟨c⁻¹ * x * c, _⟩, _⟩,
{ have h := hn.conj_mem _ hx c⁻¹,
rwa [inv_inv] at h },
simp only [monoid_hom.cod_restrict_apply, mul_equiv.coe_to_monoid_hom, mul_aut.conj_apply,
coe_mk, monoid_hom.restrict_apply, subtype.mk_eq_mk, ← mul_assoc, mul_inv_self, one_mul],
rw [mul_assoc, mul_inv_self, mul_one] },
have ht' := map_mono (eq_top_iff.1 ht),
rw [← monoid_hom.range_eq_map, monoid_hom.range_top_of_surjective _ hs] at ht',
refine eq_top_iff.2 (le_trans ht' (map_le_iff_le_comap.2 (normal_closure_le_normal _))),
rw [set.singleton_subset_iff, set_like.mem_coe],
simp only [monoid_hom.cod_restrict_apply, mul_equiv.coe_to_monoid_hom, mul_aut.conj_apply, coe_mk,
monoid_hom.restrict_apply, mem_comap],
exact subset_normal_closure (set.mem_singleton _),
end
end is_conj
/-! ### Actions by `subgroup`s
These are just copies of the definitions about `submonoid` starting from `submonoid.mul_action`.
-/
section actions
namespace subgroup
variables {α β : Type*}
/-- The action by a subgroup is the action by the underlying group. -/
@[to_additive /-"The additive action by an add_subgroup is the action by the underlying
add_group. "-/]
instance [mul_action G α] (S : subgroup G) : mul_action S α :=
S.to_submonoid.mul_action
@[to_additive]
lemma smul_def [mul_action G α] {S : subgroup G} (g : S) (m : α) : g • m = (g : G) • m := rfl
@[to_additive]
instance smul_comm_class_left
[mul_action G β] [has_scalar α β] [smul_comm_class G α β] (S : subgroup G) :
smul_comm_class S α β :=
S.to_submonoid.smul_comm_class_left
@[to_additive]
instance smul_comm_class_right
[has_scalar α β] [mul_action G β] [smul_comm_class α G β] (S : subgroup G) :
smul_comm_class α S β :=
S.to_submonoid.smul_comm_class_right
/-- Note that this provides `is_scalar_tower S G G` which is needed by `smul_mul_assoc`. -/
instance
[has_scalar α β] [mul_action G α] [mul_action G β] [is_scalar_tower G α β] (S : subgroup G) :
is_scalar_tower S α β :=
S.to_submonoid.is_scalar_tower
instance [mul_action G α] [has_faithful_scalar G α] (S : subgroup G) :
has_faithful_scalar S α :=
S.to_submonoid.has_faithful_scalar
/-- The action by a subgroup is the action by the underlying group. -/
instance [add_monoid α] [distrib_mul_action G α] (S : subgroup G) : distrib_mul_action S α :=
S.to_submonoid.distrib_mul_action
/-- The action by a subgroup is the action by the underlying group. -/
instance [monoid α] [mul_distrib_mul_action G α] (S : subgroup G) : mul_distrib_mul_action S α :=
S.to_submonoid.mul_distrib_mul_action
end subgroup
end actions
/-! ### Mul-opposite subgroups -/
section mul_opposite
namespace subgroup
/-- A subgroup `H` of `G` determines a subgroup `H.opposite` of the opposite group `Gᵐᵒᵖ`. -/
@[to_additive "An additive subgroup `H` of `G` determines an additive subgroup `H.opposite` of the
opposite additive group `Gᵃᵒᵖ`."]
def opposite (H : subgroup G) : subgroup Gᵐᵒᵖ :=
{ carrier := mul_opposite.unop ⁻¹' (H : set G),
one_mem' := H.one_mem,
mul_mem' := λ a b ha hb, H.mul_mem hb ha,
inv_mem' := λ a, H.inv_mem }
/-- Bijection between a subgroup `H` and its opposite. -/
@[to_additive "Bijection between an additive subgroup `H` and its opposite.", simps]
def opposite_equiv (H : subgroup G) : H ≃ H.opposite :=
mul_opposite.op_equiv.subtype_equiv $ λ _, iff.rfl
@[to_additive] instance (H : subgroup G) [encodable H] : encodable H.opposite :=
encodable.of_equiv H H.opposite_equiv.symm
@[to_additive] lemma smul_opposite_mul {H : subgroup G} (x g : G) (h : H.opposite) :
h • (g * x) = g * (h • x) :=
begin
cases h,
simp [(•), mul_assoc],
end
@[to_additive] lemma smul_opposite_image_mul_preimage {H : subgroup G} (g : G) (h : H.opposite)
(s : set G) : (λ y, h • y) '' (has_mul.mul g ⁻¹' s) = has_mul.mul g ⁻¹' ((λ y, h • y) '' s) :=
by { ext x, cases h, simp [(•), mul_assoc] }
end subgroup
end mul_opposite
/-! ### Saturated subgroups -/
section saturated
namespace subgroup
/-- A subgroup `H` of `G` is *saturated* if for all `n : ℕ` and `g : G` with `g^n ∈ H`
we have `n = 0` or `g ∈ H`. -/
@[to_additive "An additive subgroup `H` of `G` is *saturated* if
for all `n : ℕ` and `g : G` with `n•g ∈ H` we have `n = 0` or `g ∈ H`."]
def saturated (H : subgroup G) : Prop := ∀ ⦃n g⦄, g ^ n ∈ H → n = 0 ∨ g ∈ H
@[to_additive] lemma saturated_iff_npow {H : subgroup G} :
saturated H ↔ (∀ (n : ℕ) (g : G), g ^ n ∈ H → n = 0 ∨ g ∈ H) := iff.rfl
@[to_additive] lemma saturated_iff_zpow {H : subgroup G} :
saturated H ↔ (∀ (n : ℤ) (g : G), g ^ n ∈ H → n = 0 ∨ g ∈ H) :=
begin
split,
{ rintros hH ⟨n⟩ g hgn,
{ simp only [int.coe_nat_eq_zero, int.of_nat_eq_coe, zpow_coe_nat] at hgn ⊢,
exact hH hgn },
{ suffices : g ^ (n+1) ∈ H,
{ refine (hH this).imp _ id, simp only [forall_false_left, nat.succ_ne_zero], },
simpa only [inv_mem_iff, zpow_neg_succ_of_nat] using hgn, } },
{ intros h n g hgn,
specialize h n g,
simp only [int.coe_nat_eq_zero, zpow_coe_nat] at h,
apply h hgn }
end
end subgroup
namespace add_subgroup
lemma ker_saturated {A₁ A₂ : Type*} [add_comm_group A₁] [add_comm_group A₂]
[no_zero_smul_divisors ℕ A₂] (f : A₁ →+ A₂) :
(f.ker).saturated :=
begin
intros n g hg,
simpa only [f.mem_ker, nsmul_eq_smul, f.map_nsmul, smul_eq_zero] using hg
end
end add_subgroup
end saturated
|
f2cb250ec0e36aaf302c985c6d7dfced29e19752 | a959f48a0621edea632487cf2130bbf70d301e05 | /src/continuous_linear_maps.lean | 9715413637d5ab4d27f00f56442fe6d9e0a192f8 | [] | no_license | cipher1024/lean-differential-topology | cf441b36af9fdb022f10afff6a2fdc5aa4afa379 | 1938b0a5d9e89faff89dac4bc51598698cae6dbb | refs/heads/master | 1,619,477,568,536 | 1,527,790,354,000 | 1,527,790,354,000 | 124,159,851 | 0 | 0 | null | 1,520,385,485,000 | 1,520,385,485,000 | null | UTF-8 | Lean | false | false | 5,918 | lean | import algebra.field
import tactic.norm_num
import norms
noncomputable theory
local attribute [instance] classical.prop_decidable
local notation f `→_{`:50 a `}`:0 b := filter.tendsto f (nhds a) (nhds b)
variables {k : Type*} [normed_field k]
variables {E : Type*} [normed_space k E]
variables {F : Type*} [normed_space k F]
variables {G : Type*} [normed_space k G]
include k
def is_bounded_linear_map (L : E → F) := (is_linear_map L) ∧ ∃ M, M > 0 ∧ ∀ x : E, ∥ L x ∥ ≤ M *∥ x ∥
namespace is_bounded_linear_map
lemma zero : is_bounded_linear_map (λ (x:E), (0:F)) :=
⟨is_linear_map.map_zero, exists.intro (1:ℝ) $ by norm_num⟩
lemma id : is_bounded_linear_map (λ (x:E), x) :=
⟨is_linear_map.id, exists.intro (1:ℝ) $ by { norm_num, finish }⟩
lemma smul {L : E → F} (H : is_bounded_linear_map L) (c : k) :
is_bounded_linear_map (λ e, c•L e) :=
begin
by_cases h : c = 0,
{ simp[h], exact zero },
rcases H with ⟨lin , M, Mpos, ineq⟩,
split,
{ exact is_linear_map.map_smul_right lin },
{ existsi ∥c∥*M,
split,
{ exact mul_pos (norm_pos_iff.2 h) Mpos },
intro x,
simp,
exact calc ∥c • L x∥ = ∥c∥*∥L x∥ : norm_smul c (L x)
... ≤ ∥c∥ * M * ∥x∥ : by {simp[mul_assoc, mul_le_mul_of_nonneg_left (ineq x) (show ∥c∥ ≥ 0, from norm_nonneg)]} }
end
lemma neg {L : E → F} (H : is_bounded_linear_map L) :
is_bounded_linear_map (λ e, -L e) :=
begin
rw [show (λ e, -L e) = (λ e, (-1)•L e), by { funext, simp }],
exact smul H (-1)
end
lemma add {L : E → F} {P : E → F} (HL : is_bounded_linear_map L) (HP :is_bounded_linear_map P) :
is_bounded_linear_map (λ e, L e + P e) :=
begin
rcases HL with ⟨lin_L , M, Mpos, ineq_L⟩,
rcases HP with ⟨lin_P , M', M'pos, ineq_P⟩,
split, exact (is_linear_map.map_add lin_L lin_P),
existsi (M+M'),
split, exact add_pos Mpos M'pos,
intro x, simp,
exact calc
∥L x + P x∥ ≤ ∥L x∥ + ∥P x∥ : norm_triangle _ _
... ≤ M * ∥x∥ + M' * ∥x∥ : add_le_add (ineq_L x) (ineq_P x)
... ≤ (M + M') * ∥x∥ : by rw ←add_mul
end
lemma sub {L : E → F} {P : E → F} (HL : is_bounded_linear_map L) (HP :is_bounded_linear_map P) :
is_bounded_linear_map (λ e, L e - P e) := add HL (neg HP)
lemma comp {L : E → F} {P : F → G} (HL : is_bounded_linear_map L) (HP :is_bounded_linear_map P) : is_bounded_linear_map (P ∘ L) :=
begin
rcases HL with ⟨lin_L , M, Mpos, ineq_L⟩,
rcases HP with ⟨lin_P , M', M'pos, ineq_P⟩,
split,
{ exact is_linear_map.comp lin_P lin_L },
{ existsi M'*M,
split,
{ exact mul_pos M'pos Mpos },
{ intro x,
exact calc
∥P (L x)∥ ≤ M' * ∥L x∥ : ineq_P (L x)
... ≤ M'*M*∥x∥ : by simp[mul_assoc, mul_le_mul_of_nonneg_left (ineq_L x) (le_of_lt M'pos)] } }
end
lemma continuous {L : E → F} (H : is_bounded_linear_map L) : continuous L :=
begin
rcases H with ⟨lin, M, Mpos, ineq⟩,
apply continuous_iff_tendsto.2,
intro x,
apply tendsto_iff_norm_tendsto_zero.2,
replace ineq := λ e, calc ∥L e - L x∥ = ∥L (e - x)∥ : by rw [←(lin.sub e x)]
... ≤ M*∥e-x∥ : ineq (e-x),
have lim1 : (λ (x:E), M) →_{x} M := tendsto_const_nhds,
have lim2 : (λ e, e-x) →_{x} 0 :=
begin
have limId := continuous_iff_tendsto.1 continuous_id x,
have limx : (λ (e : E), -x) →_{x} -x := tendsto_const_nhds,
have := tendsto_add limId limx,
simp at this,
simpa using this,
end,
replace lim2 := filter.tendsto.comp lim2 lim_norm_zero,
apply squeeze_zero,
{ simp[norm_nonneg] },
{ exact ineq },
{ simpa using tendsto_mul lim1 lim2 }
end
lemma lim_zero_bounded_linear_map {L : E → F} (H : is_bounded_linear_map L) : (L →_{0} 0) :=
by simpa [H.left.zero] using continuous_iff_tendsto.1 H.continuous 0
end is_bounded_linear_map
-- Next lemma is stated for real normed space but it would work as soon as the base field is an extension of ℝ
lemma bounded_continuous_linear_map {E : Type*} [normed_space ℝ E] {F : Type*} [normed_space ℝ F] {L : E → F}
(lin : is_linear_map L) (cont : continuous L ) : is_bounded_linear_map L :=
begin
split,
exact lin,
replace cont := continuous_of_metric.1 cont 1 (by norm_num),
swap, exact 0,
rw[lin.zero] at cont,
rcases cont with ⟨δ, δ_pos, H⟩,
revert H,
repeat { conv in (_ < _ ) { rw norm_dist } },
intro H,
existsi (δ/2)⁻¹,
have half_δ_pos := half_pos δ_pos,
split,
exact (inv_pos half_δ_pos),
intro x,
by_cases h : x = 0,
{ simp [h, lin.zero] }, -- case x = 0
{ -- case x ≠ 0
have norm_x_pos : ∥x∥ > 0 := norm_pos_iff.2 h,
have norm_x : ∥x∥ ≠ 0 := mt norm_zero_iff_zero.1 h,
let p := ∥x∥*(δ/2)⁻¹,
have p_pos : p > 0 := mul_pos norm_x_pos (inv_pos $ half_δ_pos),
have p0 := ne_of_gt p_pos,
let q := (δ/2)*∥x∥⁻¹,
have q_pos : q > 0 := div_pos half_δ_pos norm_x_pos,
have q0 := ne_of_gt q_pos,
have triv := calc
p*q = ∥x∥*((δ/2)⁻¹*(δ/2))*∥x∥⁻¹ : by simp[mul_assoc]
... = 1 : by simp [(inv_mul_cancel $ ne_of_gt half_δ_pos), mul_inv_cancel norm_x],
have norm_calc := calc ∥q•x∥ = abs(q)*∥x∥ : by {rw norm_smul, refl}
... = q*∥x∥ : by rw [abs_of_nonneg $ le_of_lt q_pos]
... = δ/2 : by simp [mul_assoc, inv_mul_cancel norm_x]
... < δ : half_lt_self δ_pos,
exact calc
∥L x∥ = ∥L (1•x)∥: by simp
... = ∥L ((p*q)•x) ∥ : by {rw [←triv] }
... = ∥L (p•q•x) ∥ : by rw mul_smul
... = ∥p•L (q•x) ∥ : by rw lin.smul
... = abs(p)*∥L (q•x) ∥ : by { rw norm_smul, refl}
... = p*∥L (q•x) ∥ : by rw [abs_of_nonneg $ le_of_lt $ p_pos]
... ≤ p*1 : le_of_lt $ mul_lt_mul_of_pos_left (H norm_calc) p_pos
... = p : by simp
... = (δ/2)⁻¹*∥x∥ : by simp[mul_comm] }
end |
4ad93b645599751ce9b3304a18823fe57f9ba771 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /stage0/src/Lean/Meta/RecursorInfo.lean | b543ff795f346e535acb0303c35a1e4ac2d53a55 | [
"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 | 13,466 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.AuxRecursor
import Lean.Util.FindExpr
import Lean.Meta.Basic
namespace Lean.Meta
inductive RecursorUnivLevelPos where
| motive -- marks where the universe of the motive should go
| majorType (idx : Nat) -- marks where the #idx universe of the major premise type goes
instance : ToString RecursorUnivLevelPos := ⟨fun
| RecursorUnivLevelPos.motive => "<motive-univ>"
| RecursorUnivLevelPos.majorType idx => toString idx⟩
structure RecursorInfo where
recursorName : Name
typeName : Name
univLevelPos : List RecursorUnivLevelPos
depElim : Bool
recursive : Bool
numArgs : Nat -- Total number of arguments
majorPos : Nat
paramsPos : List (Option Nat) -- Position of the recursor parameters in the major premise, instance implicit arguments are `none`
indicesPos : List Nat -- Position of the recursor indices in the major premise
produceMotive : List Bool -- If the i-th element is true then i-th minor premise produces the motive
namespace RecursorInfo
def numParams (info : RecursorInfo) : Nat := info.paramsPos.length
def numIndices (info : RecursorInfo) : Nat := info.indicesPos.length
def motivePos (info : RecursorInfo) : Nat := info.numParams
def firstIndexPos (info : RecursorInfo) : Nat := info.majorPos - info.numIndices
def isMinor (info : RecursorInfo) (pos : Nat) : Bool :=
if pos ≤ info.motivePos then false
else if info.firstIndexPos ≤ pos && pos ≤ info.majorPos then false
else true
def numMinors (info : RecursorInfo) : Nat :=
let r := info.numArgs
let r := r - info.motivePos - 1
r - (info.majorPos + 1 - info.firstIndexPos)
instance : ToString RecursorInfo := ⟨fun info =>
"{\n" ++
" name := " ++ toString info.recursorName ++ "\n" ++
" type := " ++ toString info.typeName ++ "\n" ++
" univs := " ++ toString info.univLevelPos ++ "\n" ++
" depElim := " ++ toString info.depElim ++ "\n" ++
" recursive := " ++ toString info.recursive ++ "\n" ++
" numArgs := " ++ toString info.numArgs ++ "\n" ++
" numParams := " ++ toString info.numParams ++ "\n" ++
" numIndices := " ++ toString info.numIndices ++ "\n" ++
" numMinors := " ++ toString info.numMinors ++ "\n" ++
" major := " ++ toString info.majorPos ++ "\n" ++
" motive := " ++ toString info.motivePos ++ "\n" ++
" paramsAtMajor := " ++ toString info.paramsPos ++ "\n" ++
" indicesAtMajor := " ++ toString info.indicesPos ++ "\n" ++
" produceMotive := " ++ toString info.produceMotive ++ "\n" ++
"}"⟩
end RecursorInfo
private def mkRecursorInfoForKernelRec (declName : Name) (val : RecursorVal) : MetaM RecursorInfo := do
let ival ← getConstInfoInduct val.getInduct
let numLParams := ival.levelParams.length
let univLevelPos := (List.range numLParams).map RecursorUnivLevelPos.majorType
let univLevelPos := if val.levelParams.length == numLParams then univLevelPos else RecursorUnivLevelPos.motive :: univLevelPos
let produceMotive := List.replicate val.numMinors true
let paramsPos := (List.range val.numParams).map some
let indicesPos := (List.range val.numIndices).map fun pos => val.numParams + pos
let numArgs := val.numIndices + val.numParams + val.numMinors + val.numMotives + 1
pure {
recursorName := declName,
typeName := val.getInduct,
univLevelPos := univLevelPos,
majorPos := val.getMajorIdx,
depElim := true,
recursive := ival.isRec,
produceMotive := produceMotive,
paramsPos := paramsPos,
indicesPos := indicesPos,
numArgs := numArgs
}
private def getMajorPosIfAuxRecursor? (declName : Name) (majorPos? : Option Nat) : MetaM (Option Nat) :=
if majorPos?.isSome then pure majorPos?
else do
let env ← getEnv
if !isAuxRecursor env declName then pure none
else match declName with
| .str p s =>
if s != recOnSuffix && s != casesOnSuffix && s != brecOnSuffix then
pure none
else do
let val ← getConstInfoRec (mkRecName p)
pure $ some (val.numParams + val.numIndices + (if s == casesOnSuffix then 1 else val.numMotives))
| _ => pure none
private def checkMotive (declName : Name) (motive : Expr) (motiveArgs : Array Expr) : MetaM Unit :=
unless motive.isFVar && motiveArgs.all Expr.isFVar do
throwError "invalid user defined recursor '{declName}', result type must be of the form (C t), where C is a bound variable, and t is a (possibly empty) sequence of bound variables"
/-- Compute number of parameters for (user-defined) recursor.
We assume a parameter is anything that occurs before the motive -/
private partial def getNumParams (xs : Array Expr) (motive : Expr) (i : Nat) : Nat :=
if h : i < xs.size then
let x := xs.get ⟨i, h⟩
if motive == x then i
else getNumParams xs motive (i+1)
else
i
private def getMajorPosDepElim (declName : Name) (majorPos? : Option Nat) (xs : Array Expr) (motiveArgs : Array Expr)
: MetaM (Expr × Nat × Bool) := do
match majorPos? with
| some majorPos =>
if h : majorPos < xs.size then
let major := xs.get ⟨majorPos, h⟩
let depElim := motiveArgs.contains major
pure (major, majorPos, depElim)
else throwError "invalid major premise position for user defined recursor, recursor has only {xs.size} arguments"
| none => do
if motiveArgs.isEmpty then
throwError "invalid user defined recursor, '{declName}' does not support dependent elimination, and position of the major premise was not specified (solution: set attribute '[recursor <pos>]', where <pos> is the position of the major premise)"
let major := motiveArgs.back
match xs.getIdx? major with
| some majorPos => pure (major, majorPos, true)
| none => throwError "ill-formed recursor '{declName}'"
private def getParamsPos (declName : Name) (xs : Array Expr) (numParams : Nat) (Iargs : Array Expr) : MetaM (List (Option Nat)) := do
let mut paramsPos := #[]
for i in [:numParams] do
let x := xs[i]!
match (← Iargs.findIdxM? fun Iarg => isDefEq Iarg x) with
| some j => paramsPos := paramsPos.push (some j)
| none => do
let localDecl ← x.fvarId!.getDecl
if localDecl.binderInfo.isInstImplicit then
paramsPos := paramsPos.push none
else
throwError"invalid user defined recursor '{declName}', type of the major premise does not contain the recursor parameter"
pure paramsPos.toList
private def getIndicesPos (declName : Name) (xs : Array Expr) (majorPos numIndices : Nat) (Iargs : Array Expr) : MetaM (List Nat) := do
let mut indicesPos := #[]
for i in [:numIndices] do
let i := majorPos - numIndices + i
let x := xs[i]!
match (← Iargs.findIdxM? fun Iarg => isDefEq Iarg x) with
| some j => indicesPos := indicesPos.push j
| none => throwError "invalid user defined recursor '{declName}', type of the major premise does not contain the recursor index"
pure indicesPos.toList
private def getMotiveLevel (declName : Name) (motiveResultType : Expr) : MetaM Level :=
match motiveResultType with
| Expr.sort u@(Level.zero) => pure u
| Expr.sort u@(Level.param _) => pure u
| _ =>
throwError "invalid user defined recursor '{declName}', motive result sort must be Prop or (Sort u) where u is a universe level parameter"
private def getUnivLevelPos (declName : Name) (lparams : List Name) (motiveLvl : Level) (Ilevels : List Level) : MetaM (List RecursorUnivLevelPos) := do
let Ilevels := Ilevels.toArray
let mut univLevelPos := #[]
for p in lparams do
if motiveLvl == mkLevelParam p then
univLevelPos := univLevelPos.push RecursorUnivLevelPos.motive
else
match Ilevels.findIdx? fun u => u == mkLevelParam p with
| some i => univLevelPos := univLevelPos.push (RecursorUnivLevelPos.majorType i)
| none =>
throwError "invalid user defined recursor '{declName}', major premise type does not contain universe level parameter '{p}'"
pure univLevelPos.toList
private def getProduceMotiveAndRecursive (xs : Array Expr) (numParams numIndices majorPos : Nat) (motive : Expr) : MetaM (List Bool × Bool) := do
let mut produceMotive := #[]
let mut recursor := false
for i in [:xs.size] do
if i < numParams + 1 then
continue --skip parameters and motive
if majorPos - numIndices ≤ i && i ≤ majorPos then
continue -- skip indices and major premise
-- process minor premise
let x := xs[i]!
let xType ← inferType x
(produceMotive, recursor) ← forallTelescopeReducing xType fun minorArgs minorResultType => minorResultType.withApp fun res _ => do
let produceMotive := produceMotive.push (res == motive)
let recursor ← if recursor then pure recursor else minorArgs.anyM fun minorArg => do
let minorArgType ← inferType minorArg
pure (minorArgType.find? fun e => e == motive).isSome
pure (produceMotive, recursor)
pure (produceMotive.toList, recursor)
private def checkMotiveResultType (declName : Name) (motiveArgs : Array Expr) (motiveResultType : Expr) (motiveTypeParams : Array Expr) : MetaM Unit := do
if !motiveResultType.isSort || motiveArgs.size != motiveTypeParams.size then
throwError "invalid user defined recursor '{declName}', motive must have a type of the form (C : Pi (i : B A), I A i -> Type), where A is (possibly empty) sequence of variables (aka parameters), (i : B A) is a (possibly empty) telescope (aka indices), and I is a constant"
private def mkRecursorInfoAux (cinfo : ConstantInfo) (majorPos? : Option Nat) : MetaM RecursorInfo := do
let declName := cinfo.name
let majorPos? ← getMajorPosIfAuxRecursor? declName majorPos?
forallTelescopeReducing cinfo.type fun xs type => type.withApp fun motive motiveArgs => do
checkMotive declName motive motiveArgs
let numParams := getNumParams xs motive 0
let (major, majorPos, depElim) ← getMajorPosDepElim declName majorPos? xs motiveArgs
let numIndices := if depElim then motiveArgs.size - 1 else motiveArgs.size
if majorPos < numIndices then
throwError "invalid user defined recursor '{declName}', indices must occur before major premise"
let majorType ← inferType major
majorType.withApp fun I Iargs =>
match I with
| Expr.const Iname Ilevels => do
let paramsPos ← getParamsPos declName xs numParams Iargs
let indicesPos ← getIndicesPos declName xs majorPos numIndices Iargs
let motiveType ← inferType motive
forallTelescopeReducing motiveType fun motiveTypeParams motiveResultType => do
checkMotiveResultType declName motiveArgs motiveResultType motiveTypeParams
let motiveLvl ← getMotiveLevel declName motiveResultType
let univLevelPos ← getUnivLevelPos declName cinfo.levelParams motiveLvl Ilevels
let (produceMotive, recursive) ← getProduceMotiveAndRecursive xs numParams numIndices majorPos motive
pure {
recursorName := declName,
typeName := Iname,
univLevelPos := univLevelPos,
majorPos := majorPos,
depElim := depElim,
recursive := recursive,
produceMotive := produceMotive,
paramsPos := paramsPos,
indicesPos := indicesPos,
numArgs := xs.size
}
| _ => throwError "invalid user defined recursor '{declName}', type of the major premise must be of the form (I ...), where I is a constant"
/-
@[builtinAttrParser] def «recursor» := leading_parser "recursor " >> numLit
-/
def Attribute.Recursor.getMajorPos (stx : Syntax) : AttrM Nat := do
if stx.getKind == `Lean.Parser.Attr.recursor then
let pos := stx[1].isNatLit?.getD 0
if pos == 0 then
throwErrorAt stx "major premise position must be greater than zero"
return pos - 1
else
throwErrorAt stx "unexpected attribute argument, numeral expected"
private def mkRecursorInfoCore (declName : Name) (majorPos? : Option Nat := none) : MetaM RecursorInfo := do
let cinfo ← getConstInfo declName
match cinfo with
| ConstantInfo.recInfo val => mkRecursorInfoForKernelRec declName val
| _ => mkRecursorInfoAux cinfo majorPos?
builtin_initialize recursorAttribute : ParametricAttribute Nat ←
registerParametricAttribute {
name := `recursor,
descr := "user defined recursor, numerical parameter specifies position of the major premise",
getParam := fun _ stx => Attribute.Recursor.getMajorPos stx
afterSet := fun declName majorPos => do
discard <| mkRecursorInfoCore declName (some majorPos) |>.run'
}
def getMajorPos? (env : Environment) (declName : Name) : Option Nat :=
recursorAttribute.getParam? env declName
def mkRecursorInfo (declName : Name) (majorPos? : Option Nat := none) : MetaM RecursorInfo := do
let cinfo ← getConstInfo declName
match cinfo with
| ConstantInfo.recInfo val => mkRecursorInfoForKernelRec declName val
| _ => match majorPos? with
| none => do mkRecursorInfoAux cinfo (getMajorPos? (← getEnv) declName)
| _ => mkRecursorInfoAux cinfo majorPos?
end Lean.Meta
|
91e8ea1ea13c199c5ca3d2c37bd4c01325aced4c | c777c32c8e484e195053731103c5e52af26a25d1 | /src/geometry/manifold/mfderiv.lean | e80c4ec556fb49f31edd3dfb82d48d5f374a78ee | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 83,504 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel, Floris van Doorn
-/
import geometry.manifold.vector_bundle.tangent
/-!
# The derivative of functions between smooth manifolds
Let `M` and `M'` be two smooth manifolds with corners over a field `𝕜` (with respective models with
corners `I` on `(E, H)` and `I'` on `(E', H')`), and let `f : M → M'`. We define the
derivative of the function at a point, within a set or along the whole space, mimicking the API
for (Fréchet) derivatives. It is denoted by `mfderiv I I' f x`, where "m" stands for "manifold" and
"f" for "Fréchet" (as in the usual derivative `fderiv 𝕜 f x`).
## Main definitions
* `unique_mdiff_on I s` : predicate saying that, at each point of the set `s`, a function can have
at most one derivative. This technical condition is important when we define
`mfderiv_within` below, as otherwise there is an arbitrary choice in the derivative,
and many properties will fail (for instance the chain rule). This is analogous to
`unique_diff_on 𝕜 s` in a vector space.
Let `f` be a map between smooth manifolds. The following definitions follow the `fderiv` API.
* `mfderiv I I' f x` : the derivative of `f` at `x`, as a continuous linear map from the tangent
space at `x` to the tangent space at `f x`. If the map is not differentiable, this is `0`.
* `mfderiv_within I I' f s x` : the derivative of `f` at `x` within `s`, as a continuous linear map
from the tangent space at `x` to the tangent space at `f x`. If the map is not differentiable
within `s`, this is `0`.
* `mdifferentiable_at I I' f x` : Prop expressing whether `f` is differentiable at `x`.
* `mdifferentiable_within_at 𝕜 f s x` : Prop expressing whether `f` is differentiable within `s`
at `x`.
* `has_mfderiv_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative at `x`.
* `has_mfderiv_within_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative
within `s` at `x`.
* `mdifferentiable_on I I' f s` : Prop expressing that `f` is differentiable on the set `s`.
* `mdifferentiable I I' f` : Prop expressing that `f` is differentiable everywhere.
* `tangent_map I I' f` : the derivative of `f`, as a map from the tangent bundle of `M` to the
tangent bundle of `M'`.
We also establish results on the differential of the identity, constant functions, charts, extended
charts. For functions between vector spaces, we show that the usual notions and the manifold notions
coincide.
## Implementation notes
The tangent bundle is constructed using the machinery of topological fiber bundles, for which one
can define bundled morphisms and construct canonically maps from the total space of one bundle to
the total space of another one. One could use this mechanism to construct directly the derivative
of a smooth map. However, we want to define the derivative of any map (and let it be zero if the map
is not differentiable) to avoid proof arguments everywhere. This means we have to go back to the
details of the definition of the total space of a fiber bundle constructed from core, to cook up a
suitable definition of the derivative. It is the following: at each point, we have a preferred chart
(used to identify the fiber above the point with the model vector space in fiber bundles). Then one
should read the function using these preferred charts at `x` and `f x`, and take the derivative
of `f` in these charts.
Due to the fact that we are working in a model with corners, with an additional embedding `I` of the
model space `H` in the model vector space `E`, the charts taking values in `E` are not the original
charts of the manifold, but those ones composed with `I`, called extended charts. We define
`written_in_ext_chart I I' x f` for the function `f` written in the preferred extended charts. Then
the manifold derivative of `f`, at `x`, is just the usual derivative of
`written_in_ext_chart I I' x f`, at the point `(ext_chart_at I x) x`.
There is a subtelty with respect to continuity: if the function is not continuous, then the image
of a small open set around `x` will not be contained in the source of the preferred chart around
`f x`, which means that when reading `f` in the chart one is losing some information. To avoid this,
we include continuity in the definition of differentiablity (which is reasonable since with any
definition, differentiability implies continuity).
*Warning*: the derivative (even within a subset) is a linear map on the whole tangent space. Suppose
that one is given a smooth submanifold `N`, and a function which is smooth on `N` (i.e., its
restriction to the subtype `N` is smooth). Then, in the whole manifold `M`, the property
`mdifferentiable_on I I' f N` holds. However, `mfderiv_within I I' f N` is not uniquely defined
(what values would one choose for vectors that are transverse to `N`?), which can create issues down
the road. The problem here is that knowing the value of `f` along `N` does not determine the
differential of `f` in all directions. This is in contrast to the case where `N` would be an open
subset, or a submanifold with boundary of maximal dimension, where this issue does not appear.
The predicate `unique_mdiff_on I N` indicates that the derivative along `N` is unique if it exists,
and is an assumption in most statements requiring a form of uniqueness.
On a vector space, the manifold derivative and the usual derivative are equal. This means in
particular that they live on the same space, i.e., the tangent space is defeq to the original vector
space. To get this property is a motivation for our definition of the tangent space as a single
copy of the vector space, instead of more usual definitions such as the space of derivations, or
the space of equivalence classes of smooth curves in the manifold.
## Tags
Derivative, manifold
-/
noncomputable theory
open_locale classical topology manifold bundle
open set
universe u
section derivatives_definitions
/-!
### Derivative of maps between manifolds
The derivative of a smooth map `f` between smooth manifold `M` and `M'` at `x` is a bounded linear
map from the tangent space to `M` at `x`, to the tangent space to `M'` at `f x`. Since we defined
the tangent space using one specific chart, the formula for the derivative is written in terms of
this specific chart.
We use the names `mdifferentiable` and `mfderiv`, where the prefix letter `m` means "manifold".
-/
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
{M : Type*} [topological_space M] [charted_space H M]
{E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H')
{M' : Type*} [topological_space M'] [charted_space H' M']
/-- Property in the model space of a model with corners of being differentiable within at set at a
point, when read in the model vector space. This property will be lifted to manifolds to define
differentiable functions between manifolds. -/
def differentiable_within_at_prop (f : H → H') (s : set H) (x : H) : Prop :=
differentiable_within_at 𝕜 (I' ∘ f ∘ (I.symm)) (⇑(I.symm) ⁻¹' s ∩ set.range I) (I x)
/-- Being differentiable in the model space is a local property, invariant under smooth maps.
Therefore, it will lift nicely to manifolds. -/
lemma differentiable_within_at_local_invariant_prop :
(cont_diff_groupoid ⊤ I).local_invariant_prop (cont_diff_groupoid ⊤ I')
(differentiable_within_at_prop I I') :=
{ is_local :=
begin
assume s x u f u_open xu,
have : I.symm ⁻¹' (s ∩ u) ∩ set.range I = (I.symm ⁻¹' s ∩ set.range I) ∩ I.symm ⁻¹' u,
by simp only [set.inter_right_comm, set.preimage_inter],
rw [differentiable_within_at_prop, differentiable_within_at_prop, this],
symmetry,
apply differentiable_within_at_inter,
have : u ∈ 𝓝 (I.symm (I x)),
by { rw [model_with_corners.left_inv], exact is_open.mem_nhds u_open xu },
apply continuous_at.preimage_mem_nhds I.continuous_symm.continuous_at this,
end,
right_invariance' :=
begin
assume s x f e he hx h,
rw differentiable_within_at_prop at h ⊢,
have : I x = (I ∘ e.symm ∘ I.symm) (I (e x)), by simp only [hx] with mfld_simps,
rw this at h,
have : I (e x) ∈ (I.symm) ⁻¹' e.target ∩ set.range I, by simp only [hx] with mfld_simps,
have := ((mem_groupoid_of_pregroupoid.2 he).2.cont_diff_within_at this),
convert (h.comp' _ (this.differentiable_within_at le_top)).mono_of_mem _ using 1,
{ ext y, simp only with mfld_simps },
refine mem_nhds_within.mpr ⟨I.symm ⁻¹' e.target, e.open_target.preimage I.continuous_symm,
by simp_rw [set.mem_preimage, I.left_inv, e.maps_to hx], _⟩,
mfld_set_tac
end,
congr_of_forall :=
begin
assume s x f g h hx hf,
apply hf.congr,
{ assume y hy,
simp only with mfld_simps at hy,
simp only [h, hy] with mfld_simps },
{ simp only [hx] with mfld_simps }
end,
left_invariance' :=
begin
assume s x f e' he' hs hx h,
rw differentiable_within_at_prop at h ⊢,
have A : (I' ∘ f ∘ I.symm) (I x) ∈ (I'.symm ⁻¹' e'.source ∩ set.range I'),
by simp only [hx] with mfld_simps,
have := ((mem_groupoid_of_pregroupoid.2 he').1.cont_diff_within_at A),
convert (this.differentiable_within_at le_top).comp _ h _,
{ ext y, simp only with mfld_simps },
{ assume y hy, simp only with mfld_simps at hy, simpa only [hy] with mfld_simps using hs hy.1 }
end }
/-- Predicate ensuring that, at a point and within a set, a function can have at most one
derivative. This is expressed using the preferred chart at the considered point. -/
def unique_mdiff_within_at (s : set M) (x : M) :=
unique_diff_within_at 𝕜 ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x)
/-- Predicate ensuring that, at all points of a set, a function can have at most one derivative. -/
def unique_mdiff_on (s : set M) :=
∀x∈s, unique_mdiff_within_at I s x
/-- `mdifferentiable_within_at I I' f s x` indicates that the function `f` between manifolds
has a derivative at the point `x` within the set `s`.
This is a generalization of `differentiable_within_at` to manifolds.
We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by
`f` outside of the chart domain around `f x`. Then the chart could do anything to the image points,
and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while
this would not mean anything relevant. -/
def mdifferentiable_within_at (f : M → M') (s : set M) (x : M) :=
continuous_within_at f s x ∧
differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f)
((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x)
lemma mdifferentiable_within_at_iff_lift_prop_within_at (f : M → M') (s : set M) (x : M) :
mdifferentiable_within_at I I' f s x
↔ lift_prop_within_at (differentiable_within_at_prop I I') f s x :=
by refl
/-- `mdifferentiable_at I I' f x` indicates that the function `f` between manifolds
has a derivative at the point `x`.
This is a generalization of `differentiable_at` to manifolds.
We require continuity in the definition, as otherwise points close to `x` could be sent by
`f` outside of the chart domain around `f x`. Then the chart could do anything to the image points,
and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while
this would not mean anything relevant. -/
def mdifferentiable_at (f : M → M') (x : M) :=
continuous_at f x ∧
differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f) (range I)
((ext_chart_at I x) x)
lemma mdifferentiable_at_iff_lift_prop_at (f : M → M') (x : M) :
mdifferentiable_at I I' f x
↔ lift_prop_at (differentiable_within_at_prop I I') f x :=
begin
congrm _ ∧ _,
{ rw continuous_within_at_univ },
{ simp [differentiable_within_at_prop, set.univ_inter] }
end
/-- `mdifferentiable_on I I' f s` indicates that the function `f` between manifolds
has a derivative within `s` at all points of `s`.
This is a generalization of `differentiable_on` to manifolds. -/
def mdifferentiable_on (f : M → M') (s : set M) :=
∀x ∈ s, mdifferentiable_within_at I I' f s x
/-- `mdifferentiable I I' f` indicates that the function `f` between manifolds
has a derivative everywhere.
This is a generalization of `differentiable` to manifolds. -/
def mdifferentiable (f : M → M') :=
∀x, mdifferentiable_at I I' f x
/-- Prop registering if a local homeomorphism is a local diffeomorphism on its source -/
def local_homeomorph.mdifferentiable (f : local_homeomorph M M') :=
(mdifferentiable_on I I' f f.source) ∧ (mdifferentiable_on I' I f.symm f.target)
variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M']
/-- `has_mfderiv_within_at I I' f s x f'` indicates that the function `f` between manifolds
has, at the point `x` and within the set `s`, the derivative `f'`. Here, `f'` is a continuous linear
map from the tangent space at `x` to the tangent space at `f x`.
This is a generalization of `has_fderiv_within_at` to manifolds (as indicated by the prefix `m`).
The order of arguments is changed as the type of the derivative `f'` depends on the choice of `x`.
We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by
`f` outside of the chart domain around `f x`. Then the chart could do anything to the image points,
and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while
this would not mean anything relevant. -/
def has_mfderiv_within_at (f : M → M') (s : set M) (x : M)
(f' : tangent_space I x →L[𝕜] tangent_space I' (f x)) :=
continuous_within_at f s x ∧
has_fderiv_within_at (written_in_ext_chart_at I I' x f : E → E') f'
((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x)
/-- `has_mfderiv_at I I' f x f'` indicates that the function `f` between manifolds
has, at the point `x`, the derivative `f'`. Here, `f'` is a continuous linear
map from the tangent space at `x` to the tangent space at `f x`.
We require continuity in the definition, as otherwise points close to `x` `s` could be sent by
`f` outside of the chart domain around `f x`. Then the chart could do anything to the image points,
and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while
this would not mean anything relevant. -/
def has_mfderiv_at (f : M → M') (x : M)
(f' : tangent_space I x →L[𝕜] tangent_space I' (f x)) :=
continuous_at f x ∧
has_fderiv_within_at (written_in_ext_chart_at I I' x f : E → E') f' (range I)
((ext_chart_at I x) x)
/-- Let `f` be a function between two smooth manifolds. Then `mfderiv_within I I' f s x` is the
derivative of `f` at `x` within `s`, as a continuous linear map from the tangent space at `x` to the
tangent space at `f x`. -/
def mfderiv_within (f : M → M') (s : set M) (x : M) :
tangent_space I x →L[𝕜] tangent_space I' (f x) :=
if mdifferentiable_within_at I I' f s x then
(fderiv_within 𝕜 (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).symm ⁻¹' s ∩ range I)
((ext_chart_at I x) x) : _)
else 0
/-- Let `f` be a function between two smooth manifolds. Then `mfderiv I I' f x` is the derivative of
`f` at `x`, as a continuous linear map from the tangent space at `x` to the tangent space at
`f x`. -/
def mfderiv (f : M → M') (x : M) : tangent_space I x →L[𝕜] tangent_space I' (f x) :=
if mdifferentiable_at I I' f x then
(fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : E → E') (range I)
((ext_chart_at I x) x) : _)
else 0
/-- The derivative within a set, as a map between the tangent bundles -/
def tangent_map_within (f : M → M') (s : set M) : tangent_bundle I M → tangent_bundle I' M' :=
λp, ⟨f p.1, (mfderiv_within I I' f s p.1 : tangent_space I p.1 → tangent_space I' (f p.1)) p.2⟩
/-- The derivative, as a map between the tangent bundles -/
def tangent_map (f : M → M') : tangent_bundle I M → tangent_bundle I' M' :=
λp, ⟨f p.1, (mfderiv I I' f p.1 : tangent_space I p.1 → tangent_space I' (f p.1)) p.2⟩
end derivatives_definitions
section derivatives_properties
/-! ### Unique differentiability sets in manifolds -/
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
{M : Type*} [topological_space M] [charted_space H M] --
{E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{M' : Type*} [topological_space M'] [charted_space H' M']
{E'' : Type*} [normed_add_comm_group E''] [normed_space 𝕜 E'']
{H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''}
{M'' : Type*} [topological_space M''] [charted_space H'' M'']
{f f₀ f₁ : M → M'}
{x : M}
{s t : set M}
{g : M' → M''}
{u : set M'}
lemma unique_mdiff_within_at_univ : unique_mdiff_within_at I univ x :=
begin
unfold unique_mdiff_within_at,
simp only [preimage_univ, univ_inter],
exact I.unique_diff _ (mem_range_self _)
end
variable {I}
lemma unique_mdiff_within_at_iff {s : set M} {x : M} :
unique_mdiff_within_at I s x ↔
unique_diff_within_at 𝕜 ((ext_chart_at I x).symm ⁻¹' s ∩ (ext_chart_at I x).target)
((ext_chart_at I x) x) :=
begin
apply unique_diff_within_at_congr,
rw [nhds_within_inter, nhds_within_inter, nhds_within_ext_chart_at_target_eq]
end
lemma unique_mdiff_within_at.mono (h : unique_mdiff_within_at I s x) (st : s ⊆ t) :
unique_mdiff_within_at I t x :=
unique_diff_within_at.mono h $ inter_subset_inter (preimage_mono st) (subset.refl _)
lemma unique_mdiff_within_at.inter' (hs : unique_mdiff_within_at I s x) (ht : t ∈ 𝓝[s] x) :
unique_mdiff_within_at I (s ∩ t) x :=
begin
rw [unique_mdiff_within_at, ext_chart_at_preimage_inter_eq],
exact unique_diff_within_at.inter' hs (ext_chart_at_preimage_mem_nhds_within I x ht)
end
lemma unique_mdiff_within_at.inter (hs : unique_mdiff_within_at I s x) (ht : t ∈ 𝓝 x) :
unique_mdiff_within_at I (s ∩ t) x :=
begin
rw [unique_mdiff_within_at, ext_chart_at_preimage_inter_eq],
exact unique_diff_within_at.inter hs (ext_chart_at_preimage_mem_nhds I x ht)
end
lemma is_open.unique_mdiff_within_at (xs : x ∈ s) (hs : is_open s) : unique_mdiff_within_at I s x :=
begin
have := unique_mdiff_within_at.inter (unique_mdiff_within_at_univ I) (is_open.mem_nhds hs xs),
rwa univ_inter at this
end
lemma unique_mdiff_on.inter (hs : unique_mdiff_on I s) (ht : is_open t) :
unique_mdiff_on I (s ∩ t) :=
λx hx, unique_mdiff_within_at.inter (hs _ hx.1) (is_open.mem_nhds ht hx.2)
lemma is_open.unique_mdiff_on (hs : is_open s) : unique_mdiff_on I s :=
λx hx, is_open.unique_mdiff_within_at hx hs
lemma unique_mdiff_on_univ : unique_mdiff_on I (univ : set M) :=
is_open_univ.unique_mdiff_on
/- We name the typeclass variables related to `smooth_manifold_with_corners` structure as they are
necessary in lemmas mentioning the derivative, but not in lemmas about differentiability, so we
want to include them or omit them when necessary. -/
variables [Is : smooth_manifold_with_corners I M] [I's : smooth_manifold_with_corners I' M']
[I''s : smooth_manifold_with_corners I'' M'']
{f' f₀' f₁' : tangent_space I x →L[𝕜] tangent_space I' (f x)}
{g' : tangent_space I' (f x) →L[𝕜] tangent_space I'' (g (f x))}
/-- `unique_mdiff_within_at` achieves its goal: it implies the uniqueness of the derivative. -/
theorem unique_mdiff_within_at.eq (U : unique_mdiff_within_at I s x)
(h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') :
f' = f₁' :=
U.eq h.2 h₁.2
theorem unique_mdiff_on.eq (U : unique_mdiff_on I s) (hx : x ∈ s)
(h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') :
f' = f₁' :=
unique_mdiff_within_at.eq (U _ hx) h h₁
/-!
### General lemmas on derivatives of functions between manifolds
We mimick the API for functions between vector spaces
-/
lemma mdifferentiable_within_at_iff {f : M → M'} {s : set M} {x : M} :
mdifferentiable_within_at I I' f s x ↔
continuous_within_at f s x ∧
differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f)
((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' s) ((ext_chart_at I x) x) :=
begin
refine and_congr iff.rfl (exists_congr $ λ f', _),
rw [inter_comm],
simp only [has_fderiv_within_at, nhds_within_inter, nhds_within_ext_chart_at_target_eq]
end
include Is I's
/-- One can reformulate differentiability within a set at a point as continuity within this set at
this point, and differentiability in any chart containing that point. -/
lemma mdifferentiable_within_at_iff_of_mem_source
{x' : M} {y : M'}
(hx : x' ∈ (charted_space.chart_at H x).source)
(hy : f x' ∈ (charted_space.chart_at H' y).source) :
mdifferentiable_within_at I I' f s x'
↔ continuous_within_at f s x'
∧ differentiable_within_at 𝕜
((ext_chart_at I' y) ∘ f ∘ ((ext_chart_at I x).symm))
(((ext_chart_at I x).symm) ⁻¹' s ∩ set.range I)
((ext_chart_at I x) x') :=
(differentiable_within_at_local_invariant_prop I I').lift_prop_within_at_indep_chart
(structure_groupoid.chart_mem_maximal_atlas _ x) hx
(structure_groupoid.chart_mem_maximal_atlas _ y) hy
lemma mfderiv_within_zero_of_not_mdifferentiable_within_at
(h : ¬ mdifferentiable_within_at I I' f s x) : mfderiv_within I I' f s x = 0 :=
by simp only [mfderiv_within, h, if_neg, not_false_iff]
lemma mfderiv_zero_of_not_mdifferentiable_at
(h : ¬ mdifferentiable_at I I' f x) : mfderiv I I' f x = 0 :=
by simp only [mfderiv, h, if_neg, not_false_iff]
theorem has_mfderiv_within_at.mono (h : has_mfderiv_within_at I I' f t x f') (hst : s ⊆ t) :
has_mfderiv_within_at I I' f s x f' :=
⟨ continuous_within_at.mono h.1 hst,
has_fderiv_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩
theorem has_mfderiv_at.has_mfderiv_within_at
(h : has_mfderiv_at I I' f x f') : has_mfderiv_within_at I I' f s x f' :=
⟨ continuous_at.continuous_within_at h.1, has_fderiv_within_at.mono h.2 (inter_subset_right _ _) ⟩
lemma has_mfderiv_within_at.mdifferentiable_within_at (h : has_mfderiv_within_at I I' f s x f') :
mdifferentiable_within_at I I' f s x :=
⟨h.1, ⟨f', h.2⟩⟩
lemma has_mfderiv_at.mdifferentiable_at (h : has_mfderiv_at I I' f x f') :
mdifferentiable_at I I' f x :=
⟨h.1, ⟨f', h.2⟩⟩
@[simp, mfld_simps] lemma has_mfderiv_within_at_univ :
has_mfderiv_within_at I I' f univ x f' ↔ has_mfderiv_at I I' f x f' :=
by simp only [has_mfderiv_within_at, has_mfderiv_at, continuous_within_at_univ] with mfld_simps
theorem has_mfderiv_at_unique
(h₀ : has_mfderiv_at I I' f x f₀') (h₁ : has_mfderiv_at I I' f x f₁') : f₀' = f₁' :=
begin
rw ← has_mfderiv_within_at_univ at h₀ h₁,
exact (unique_mdiff_within_at_univ I).eq h₀ h₁
end
lemma has_mfderiv_within_at_inter' (h : t ∈ 𝓝[s] x) :
has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' :=
begin
rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_at_preimage_inter_eq,
has_fderiv_within_at_inter', continuous_within_at_inter' h],
exact ext_chart_at_preimage_mem_nhds_within I x h,
end
lemma has_mfderiv_within_at_inter (h : t ∈ 𝓝 x) :
has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' :=
begin
rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_at_preimage_inter_eq,
has_fderiv_within_at_inter, continuous_within_at_inter h],
exact ext_chart_at_preimage_mem_nhds I x h,
end
lemma has_mfderiv_within_at.union
(hs : has_mfderiv_within_at I I' f s x f') (ht : has_mfderiv_within_at I I' f t x f') :
has_mfderiv_within_at I I' f (s ∪ t) x f' :=
begin
split,
{ exact continuous_within_at.union hs.1 ht.1 },
{ convert has_fderiv_within_at.union hs.2 ht.2,
simp only [union_inter_distrib_right, preimage_union] }
end
lemma has_mfderiv_within_at.nhds_within (h : has_mfderiv_within_at I I' f s x f')
(ht : s ∈ 𝓝[t] x) : has_mfderiv_within_at I I' f t x f' :=
(has_mfderiv_within_at_inter' ht).1 (h.mono (inter_subset_right _ _))
lemma has_mfderiv_within_at.has_mfderiv_at (h : has_mfderiv_within_at I I' f s x f')
(hs : s ∈ 𝓝 x) :
has_mfderiv_at I I' f x f' :=
by rwa [← univ_inter s, has_mfderiv_within_at_inter hs, has_mfderiv_within_at_univ] at h
lemma mdifferentiable_within_at.has_mfderiv_within_at (h : mdifferentiable_within_at I I' f s x) :
has_mfderiv_within_at I I' f s x (mfderiv_within I I' f s x) :=
begin
refine ⟨h.1, _⟩,
simp only [mfderiv_within, h, if_pos] with mfld_simps,
exact differentiable_within_at.has_fderiv_within_at h.2
end
lemma mdifferentiable_within_at.mfderiv_within (h : mdifferentiable_within_at I I' f s x) :
(mfderiv_within I I' f s x) =
fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : _) ((ext_chart_at I x).symm ⁻¹' s ∩ range I)
((ext_chart_at I x) x) :=
by simp only [mfderiv_within, h, if_pos]
lemma mdifferentiable_at.has_mfderiv_at (h : mdifferentiable_at I I' f x) :
has_mfderiv_at I I' f x (mfderiv I I' f x) :=
begin
refine ⟨h.1, _⟩,
simp only [mfderiv, h, if_pos] with mfld_simps,
exact differentiable_within_at.has_fderiv_within_at h.2
end
lemma mdifferentiable_at.mfderiv (h : mdifferentiable_at I I' f x) :
(mfderiv I I' f x) =
fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : _) (range I) ((ext_chart_at I x) x) :=
by simp only [mfderiv, h, if_pos]
lemma has_mfderiv_at.mfderiv (h : has_mfderiv_at I I' f x f') :
mfderiv I I' f x = f' :=
(has_mfderiv_at_unique h h.mdifferentiable_at.has_mfderiv_at).symm
lemma has_mfderiv_within_at.mfderiv_within
(h : has_mfderiv_within_at I I' f s x f') (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I' f s x = f' :=
by { ext, rw hxs.eq h h.mdifferentiable_within_at.has_mfderiv_within_at }
lemma mdifferentiable.mfderiv_within
(h : mdifferentiable_at I I' f x) (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I' f s x = mfderiv I I' f x :=
begin
apply has_mfderiv_within_at.mfderiv_within _ hxs,
exact h.has_mfderiv_at.has_mfderiv_within_at
end
lemma mfderiv_within_subset (st : s ⊆ t) (hs : unique_mdiff_within_at I s x)
(h : mdifferentiable_within_at I I' f t x) :
mfderiv_within I I' f s x = mfderiv_within I I' f t x :=
((mdifferentiable_within_at.has_mfderiv_within_at h).mono st).mfderiv_within hs
omit Is I's
lemma mdifferentiable_within_at.mono (hst : s ⊆ t)
(h : mdifferentiable_within_at I I' f t x) : mdifferentiable_within_at I I' f s x :=
⟨ continuous_within_at.mono h.1 hst,
differentiable_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩
lemma mdifferentiable_within_at_univ :
mdifferentiable_within_at I I' f univ x ↔ mdifferentiable_at I I' f x :=
by simp only [mdifferentiable_within_at, mdifferentiable_at, continuous_within_at_univ]
with mfld_simps
lemma mdifferentiable_within_at_inter (ht : t ∈ 𝓝 x) :
mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x :=
begin
rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_at_preimage_inter_eq,
differentiable_within_at_inter, continuous_within_at_inter ht],
exact ext_chart_at_preimage_mem_nhds I x ht
end
lemma mdifferentiable_within_at_inter' (ht : t ∈ 𝓝[s] x) :
mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x :=
begin
rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_at_preimage_inter_eq,
differentiable_within_at_inter', continuous_within_at_inter' ht],
exact ext_chart_at_preimage_mem_nhds_within I x ht
end
lemma mdifferentiable_at.mdifferentiable_within_at
(h : mdifferentiable_at I I' f x) : mdifferentiable_within_at I I' f s x :=
mdifferentiable_within_at.mono (subset_univ _) (mdifferentiable_within_at_univ.2 h)
lemma mdifferentiable_within_at.mdifferentiable_at
(h : mdifferentiable_within_at I I' f s x) (hs : s ∈ 𝓝 x) : mdifferentiable_at I I' f x :=
begin
have : s = univ ∩ s, by rw univ_inter,
rwa [this, mdifferentiable_within_at_inter hs, mdifferentiable_within_at_univ] at h,
end
lemma mdifferentiable_on.mono
(h : mdifferentiable_on I I' f t) (st : s ⊆ t) : mdifferentiable_on I I' f s :=
λx hx, (h x (st hx)).mono st
lemma mdifferentiable_on_univ :
mdifferentiable_on I I' f univ ↔ mdifferentiable I I' f :=
by { simp only [mdifferentiable_on, mdifferentiable_within_at_univ] with mfld_simps, refl }
lemma mdifferentiable.mdifferentiable_on
(h : mdifferentiable I I' f) : mdifferentiable_on I I' f s :=
(mdifferentiable_on_univ.2 h).mono (subset_univ _)
lemma mdifferentiable_on_of_locally_mdifferentiable_on
(h : ∀x∈s, ∃u, is_open u ∧ x ∈ u ∧ mdifferentiable_on I I' f (s ∩ u)) :
mdifferentiable_on I I' f s :=
begin
assume x xs,
rcases h x xs with ⟨t, t_open, xt, ht⟩,
exact (mdifferentiable_within_at_inter (is_open.mem_nhds t_open xt)).1 (ht x ⟨xs, xt⟩)
end
include Is I's
@[simp, mfld_simps] lemma mfderiv_within_univ : mfderiv_within I I' f univ = mfderiv I I' f :=
begin
ext x : 1,
simp only [mfderiv_within, mfderiv] with mfld_simps,
rw mdifferentiable_within_at_univ
end
lemma mfderiv_within_inter (ht : t ∈ 𝓝 x) (hs : unique_mdiff_within_at I s x) :
mfderiv_within I I' f (s ∩ t) x = mfderiv_within I I' f s x :=
by rw [mfderiv_within, mfderiv_within, ext_chart_at_preimage_inter_eq,
mdifferentiable_within_at_inter ht,
fderiv_within_inter (ext_chart_at_preimage_mem_nhds I x ht) hs]
lemma mdifferentiable_at_iff_of_mem_source {x' : M} {y : M'}
(hx : x' ∈ (charted_space.chart_at H x).source)
(hy : f x' ∈ (charted_space.chart_at H' y).source) :
mdifferentiable_at I I' f x'
↔ continuous_at f x'
∧ differentiable_within_at 𝕜
((ext_chart_at I' y) ∘ f ∘ ((ext_chart_at I x).symm))
(set.range I)
((ext_chart_at I x) x') :=
mdifferentiable_within_at_univ.symm.trans $
(mdifferentiable_within_at_iff_of_mem_source hx hy).trans $
by rw [continuous_within_at_univ, set.preimage_univ, set.univ_inter]
omit Is I's
/-! ### Deriving continuity from differentiability on manifolds -/
theorem has_mfderiv_within_at.continuous_within_at
(h : has_mfderiv_within_at I I' f s x f') : continuous_within_at f s x :=
h.1
theorem has_mfderiv_at.continuous_at (h : has_mfderiv_at I I' f x f') :
continuous_at f x :=
h.1
lemma mdifferentiable_within_at.continuous_within_at (h : mdifferentiable_within_at I I' f s x) :
continuous_within_at f s x :=
h.1
lemma mdifferentiable_at.continuous_at (h : mdifferentiable_at I I' f x) : continuous_at f x :=
h.1
lemma mdifferentiable_on.continuous_on (h : mdifferentiable_on I I' f s) : continuous_on f s :=
λx hx, (h x hx).continuous_within_at
lemma mdifferentiable.continuous (h : mdifferentiable I I' f) : continuous f :=
continuous_iff_continuous_at.2 $ λx, (h x).continuous_at
include Is I's
lemma tangent_map_within_subset {p : tangent_bundle I M}
(st : s ⊆ t) (hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_within_at I I' f t p.1) :
tangent_map_within I I' f s p = tangent_map_within I I' f t p :=
begin
simp only [tangent_map_within] with mfld_simps,
rw mfderiv_within_subset st hs h,
end
lemma tangent_map_within_univ :
tangent_map_within I I' f univ = tangent_map I I' f :=
by { ext p : 1, simp only [tangent_map_within, tangent_map] with mfld_simps }
lemma tangent_map_within_eq_tangent_map {p : tangent_bundle I M}
(hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_at I I' f p.1) :
tangent_map_within I I' f s p = tangent_map I I' f p :=
begin
rw ← mdifferentiable_within_at_univ at h,
rw ← tangent_map_within_univ,
exact tangent_map_within_subset (subset_univ _) hs h,
end
@[simp, mfld_simps] lemma tangent_map_within_proj {p : tangent_bundle I M} :
(tangent_map_within I I' f s p).proj = f p.proj := rfl
@[simp, mfld_simps] lemma tangent_map_within_fst {p : tangent_bundle I M} :
(tangent_map_within I I' f s p).1 = f p.1 := rfl
@[simp, mfld_simps] lemma tangent_map_proj {p : tangent_bundle I M} :
(tangent_map I I' f p).proj = f p.proj := rfl
@[simp, mfld_simps] lemma tangent_map_fst {p : tangent_bundle I M} :
(tangent_map I I' f p).1 = f p.1 := rfl
omit Is I's
/-! ### Congruence lemmas for derivatives on manifolds -/
lemma has_mfderiv_within_at.congr_of_eventually_eq (h : has_mfderiv_within_at I I' f s x f')
(h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : has_mfderiv_within_at I I' f₁ s x f' :=
begin
refine ⟨continuous_within_at.congr_of_eventually_eq h.1 h₁ hx, _⟩,
apply has_fderiv_within_at.congr_of_eventually_eq h.2,
{ have : (ext_chart_at I x).symm ⁻¹' {y | f₁ y = f y} ∈
𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) :=
ext_chart_at_preimage_mem_nhds_within I x h₁,
apply filter.mem_of_superset this (λy, _),
simp only [hx] with mfld_simps {contextual := tt} },
{ simp only [hx] with mfld_simps },
end
lemma has_mfderiv_within_at.congr_mono (h : has_mfderiv_within_at I I' f s x f')
(ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) :
has_mfderiv_within_at I I' f₁ t x f' :=
(h.mono h₁).congr_of_eventually_eq (filter.mem_inf_of_right ht) hx
lemma has_mfderiv_at.congr_of_eventually_eq (h : has_mfderiv_at I I' f x f')
(h₁ : f₁ =ᶠ[𝓝 x] f) : has_mfderiv_at I I' f₁ x f' :=
begin
rw ← has_mfderiv_within_at_univ at ⊢ h,
apply h.congr_of_eventually_eq _ (mem_of_mem_nhds h₁ : _),
rwa nhds_within_univ
end
include Is I's
lemma mdifferentiable_within_at.congr_of_eventually_eq
(h : mdifferentiable_within_at I I' f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f)
(hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x :=
(h.has_mfderiv_within_at.congr_of_eventually_eq h₁ hx).mdifferentiable_within_at
variables (I I')
lemma filter.eventually_eq.mdifferentiable_within_at_iff
(h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) :
mdifferentiable_within_at I I' f s x ↔ mdifferentiable_within_at I I' f₁ s x :=
begin
split,
{ assume h,
apply h.congr_of_eventually_eq h₁ hx },
{ assume h,
apply h.congr_of_eventually_eq _ hx.symm,
apply h₁.mono,
intro y,
apply eq.symm }
end
variables {I I'}
lemma mdifferentiable_within_at.congr_mono (h : mdifferentiable_within_at I I' f s x)
(ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) :
mdifferentiable_within_at I I' f₁ t x :=
(has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx h₁).mdifferentiable_within_at
lemma mdifferentiable_within_at.congr (h : mdifferentiable_within_at I I' f s x)
(ht : ∀x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x :=
(has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx
(subset.refl _)).mdifferentiable_within_at
lemma mdifferentiable_on.congr_mono (h : mdifferentiable_on I I' f s) (h' : ∀x ∈ t, f₁ x = f x)
(h₁ : t ⊆ s) : mdifferentiable_on I I' f₁ t :=
λ x hx, (h x (h₁ hx)).congr_mono h' (h' x hx) h₁
lemma mdifferentiable_at.congr_of_eventually_eq (h : mdifferentiable_at I I' f x)
(hL : f₁ =ᶠ[𝓝 x] f) : mdifferentiable_at I I' f₁ x :=
((h.has_mfderiv_at).congr_of_eventually_eq hL).mdifferentiable_at
lemma mdifferentiable_within_at.mfderiv_within_congr_mono (h : mdifferentiable_within_at I I' f s x)
(hs : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (hxt : unique_mdiff_within_at I t x) (h₁ : t ⊆ s) :
mfderiv_within I I' f₁ t x = (mfderiv_within I I' f s x : _) :=
(has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at hs hx h₁).mfderiv_within hxt
lemma filter.eventually_eq.mfderiv_within_eq (hs : unique_mdiff_within_at I s x)
(hL : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) :
mfderiv_within I I' f₁ s x = (mfderiv_within I I' f s x : _) :=
begin
by_cases h : mdifferentiable_within_at I I' f s x,
{ exact ((h.has_mfderiv_within_at).congr_of_eventually_eq hL hx).mfderiv_within hs },
{ unfold mfderiv_within,
rw [if_neg h, if_neg],
rwa ← hL.mdifferentiable_within_at_iff I I' hx }
end
lemma mfderiv_within_congr (hs : unique_mdiff_within_at I s x)
(hL : ∀ x ∈ s, f₁ x = f x) (hx : f₁ x = f x) :
mfderiv_within I I' f₁ s x = (mfderiv_within I I' f s x : _) :=
filter.eventually_eq.mfderiv_within_eq hs (filter.eventually_eq_of_mem (self_mem_nhds_within) hL) hx
lemma tangent_map_within_congr (h : ∀ x ∈ s, f x = f₁ x)
(p : tangent_bundle I M) (hp : p.1 ∈ s) (hs : unique_mdiff_within_at I s p.1) :
tangent_map_within I I' f s p = tangent_map_within I I' f₁ s p :=
begin
simp only [tangent_map_within, h p.fst hp, true_and, eq_self_iff_true, heq_iff_eq,
sigma.mk.inj_iff],
congr' 1,
exact mfderiv_within_congr hs h (h _ hp)
end
lemma filter.eventually_eq.mfderiv_eq (hL : f₁ =ᶠ[𝓝 x] f) :
mfderiv I I' f₁ x = (mfderiv I I' f x : _) :=
begin
have A : f₁ x = f x := (mem_of_mem_nhds hL : _),
rw [← mfderiv_within_univ, ← mfderiv_within_univ],
rw ← nhds_within_univ at hL,
exact hL.mfderiv_within_eq (unique_mdiff_within_at_univ I) A
end
/-! ### Composition lemmas -/
omit Is I's
lemma written_in_ext_chart_comp (h : continuous_within_at f s x) :
{y | written_in_ext_chart_at I I'' x (g ∘ f) y
= ((written_in_ext_chart_at I' I'' (f x) g) ∘ (written_in_ext_chart_at I I' x f)) y}
∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) :=
begin
apply @filter.mem_of_superset _ _
((f ∘ (ext_chart_at I x).symm)⁻¹' (ext_chart_at I' (f x)).source) _
(ext_chart_at_preimage_mem_nhds_within I x
(h.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))),
mfld_set_tac,
end
variable (x)
include Is I's I''s
theorem has_mfderiv_within_at.comp
(hg : has_mfderiv_within_at I' I'' g u (f x) g') (hf : has_mfderiv_within_at I I' f s x f')
(hst : s ⊆ f ⁻¹' u) :
has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') :=
begin
refine ⟨continuous_within_at.comp hg.1 hf.1 hst, _⟩,
have A : has_fderiv_within_at ((written_in_ext_chart_at I' I'' (f x) g) ∘
(written_in_ext_chart_at I I' x f))
(continuous_linear_map.comp g' f' : E →L[𝕜] E'')
((ext_chart_at I x).symm ⁻¹' s ∩ range (I))
((ext_chart_at I x) x),
{ have : (ext_chart_at I x).symm ⁻¹' (f ⁻¹' (ext_chart_at I' (f x)).source)
∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) :=
(ext_chart_at_preimage_mem_nhds_within I x
(hf.1.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))),
unfold has_mfderiv_within_at at *,
rw [← has_fderiv_within_at_inter' this, ← ext_chart_at_preimage_inter_eq] at hf ⊢,
have : written_in_ext_chart_at I I' x f ((ext_chart_at I x) x)
= (ext_chart_at I' (f x)) (f x),
by simp only with mfld_simps,
rw ← this at hg,
apply has_fderiv_within_at.comp ((ext_chart_at I x) x) hg.2 hf.2 _,
assume y hy,
simp only with mfld_simps at hy,
have : f (((chart_at H x).symm : H → M) (I.symm y)) ∈ u := hst hy.1.1,
simp only [hy, this] with mfld_simps },
apply A.congr_of_eventually_eq (written_in_ext_chart_comp hf.1),
simp only with mfld_simps
end
/-- The chain rule. -/
theorem has_mfderiv_at.comp
(hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_at I I' f x f') :
has_mfderiv_at I I'' (g ∘ f) x (g'.comp f') :=
begin
rw ← has_mfderiv_within_at_univ at *,
exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ
end
theorem has_mfderiv_at.comp_has_mfderiv_within_at
(hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_within_at I I' f s x f') :
has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') :=
begin
rw ← has_mfderiv_within_at_univ at *,
exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ
end
lemma mdifferentiable_within_at.comp
(hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x)
(h : s ⊆ f ⁻¹' u) : mdifferentiable_within_at I I'' (g ∘ f) s x :=
begin
rcases hf.2 with ⟨f', hf'⟩,
have F : has_mfderiv_within_at I I' f s x f' := ⟨hf.1, hf'⟩,
rcases hg.2 with ⟨g', hg'⟩,
have G : has_mfderiv_within_at I' I'' g u (f x) g' := ⟨hg.1, hg'⟩,
exact (has_mfderiv_within_at.comp x G F h).mdifferentiable_within_at
end
lemma mdifferentiable_at.comp
(hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) :
mdifferentiable_at I I'' (g ∘ f) x :=
(hg.has_mfderiv_at.comp x hf.has_mfderiv_at).mdifferentiable_at
lemma mfderiv_within_comp
(hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x)
(h : s ⊆ f ⁻¹' u) (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I'' (g ∘ f) s x =
(mfderiv_within I' I'' g u (f x)).comp (mfderiv_within I I' f s x) :=
begin
apply has_mfderiv_within_at.mfderiv_within _ hxs,
exact has_mfderiv_within_at.comp x hg.has_mfderiv_within_at hf.has_mfderiv_within_at h
end
lemma mfderiv_comp
(hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) :
mfderiv I I'' (g ∘ f) x = (mfderiv I' I'' g (f x)).comp (mfderiv I I' f x) :=
begin
apply has_mfderiv_at.mfderiv,
exact has_mfderiv_at.comp x hg.has_mfderiv_at hf.has_mfderiv_at
end
lemma mdifferentiable_on.comp
(hg : mdifferentiable_on I' I'' g u) (hf : mdifferentiable_on I I' f s) (st : s ⊆ f ⁻¹' u) :
mdifferentiable_on I I'' (g ∘ f) s :=
λx hx, mdifferentiable_within_at.comp x (hg (f x) (st hx)) (hf x hx) st
lemma mdifferentiable.comp
(hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) : mdifferentiable I I'' (g ∘ f) :=
λx, mdifferentiable_at.comp x (hg (f x)) (hf x)
lemma tangent_map_within_comp_at (p : tangent_bundle I M)
(hg : mdifferentiable_within_at I' I'' g u (f p.1)) (hf : mdifferentiable_within_at I I' f s p.1)
(h : s ⊆ f ⁻¹' u) (hps : unique_mdiff_within_at I s p.1) :
tangent_map_within I I'' (g ∘ f) s p =
tangent_map_within I' I'' g u (tangent_map_within I I' f s p) :=
begin
simp only [tangent_map_within] with mfld_simps,
rw mfderiv_within_comp p.1 hg hf h hps,
refl
end
lemma tangent_map_comp_at (p : tangent_bundle I M)
(hg : mdifferentiable_at I' I'' g (f p.1)) (hf : mdifferentiable_at I I' f p.1) :
tangent_map I I'' (g ∘ f) p = tangent_map I' I'' g (tangent_map I I' f p) :=
begin
simp only [tangent_map] with mfld_simps,
rw mfderiv_comp p.1 hg hf,
refl
end
lemma tangent_map_comp (hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) :
tangent_map I I'' (g ∘ f) = (tangent_map I' I'' g) ∘ (tangent_map I I' f) :=
by { ext p : 1, exact tangent_map_comp_at _ (hg _) (hf _) }
end derivatives_properties
section mfderiv_fderiv
/-!
### Relations between vector space derivative and manifold derivative
The manifold derivative `mfderiv`, when considered on the model vector space with its trivial
manifold structure, coincides with the usual Frechet derivative `fderiv`. In this section, we prove
this and related statements.
-/
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
{E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E']
{f : E → E'} {s : set E} {x : E}
lemma unique_mdiff_within_at_iff_unique_diff_within_at :
unique_mdiff_within_at (𝓘(𝕜, E)) s x ↔ unique_diff_within_at 𝕜 s x :=
by simp only [unique_mdiff_within_at] with mfld_simps
alias unique_mdiff_within_at_iff_unique_diff_within_at ↔
unique_mdiff_within_at.unique_diff_within_at unique_diff_within_at.unique_mdiff_within_at
lemma unique_mdiff_on_iff_unique_diff_on :
unique_mdiff_on (𝓘(𝕜, E)) s ↔ unique_diff_on 𝕜 s :=
by simp [unique_mdiff_on, unique_diff_on, unique_mdiff_within_at_iff_unique_diff_within_at]
alias unique_mdiff_on_iff_unique_diff_on ↔
unique_mdiff_on.unique_diff_on unique_diff_on.unique_mdiff_on
@[simp, mfld_simps] lemma written_in_ext_chart_model_space :
written_in_ext_chart_at (𝓘(𝕜, E)) (𝓘(𝕜, E')) x f = f :=
rfl
lemma has_mfderiv_within_at_iff_has_fderiv_within_at {f'} :
has_mfderiv_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x f' ↔
has_fderiv_within_at f f' s x :=
by simpa only [has_mfderiv_within_at, and_iff_right_iff_imp] with mfld_simps
using has_fderiv_within_at.continuous_within_at
alias has_mfderiv_within_at_iff_has_fderiv_within_at ↔
has_mfderiv_within_at.has_fderiv_within_at has_fderiv_within_at.has_mfderiv_within_at
lemma has_mfderiv_at_iff_has_fderiv_at {f'} :
has_mfderiv_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x f' ↔ has_fderiv_at f f' x :=
by rw [← has_mfderiv_within_at_univ, has_mfderiv_within_at_iff_has_fderiv_within_at,
has_fderiv_within_at_univ]
alias has_mfderiv_at_iff_has_fderiv_at ↔ has_mfderiv_at.has_fderiv_at has_fderiv_at.has_mfderiv_at
/-- For maps between vector spaces, `mdifferentiable_within_at` and `fdifferentiable_within_at`
coincide -/
theorem mdifferentiable_within_at_iff_differentiable_within_at :
mdifferentiable_within_at (𝓘(𝕜, E)) (𝓘(𝕜, E')) f s x
↔ differentiable_within_at 𝕜 f s x :=
begin
simp only [mdifferentiable_within_at] with mfld_simps,
exact ⟨λH, H.2, λH, ⟨H.continuous_within_at, H⟩⟩
end
alias mdifferentiable_within_at_iff_differentiable_within_at ↔
mdifferentiable_within_at.differentiable_within_at
differentiable_within_at.mdifferentiable_within_at
/-- For maps between vector spaces, `mdifferentiable_at` and `differentiable_at` coincide -/
theorem mdifferentiable_at_iff_differentiable_at :
mdifferentiable_at (𝓘(𝕜, E)) (𝓘(𝕜, E')) f x ↔ differentiable_at 𝕜 f x :=
begin
simp only [mdifferentiable_at, differentiable_within_at_univ] with mfld_simps,
exact ⟨λH, H.2, λH, ⟨H.continuous_at, H⟩⟩
end
alias mdifferentiable_at_iff_differentiable_at ↔
mdifferentiable_at.differentiable_at differentiable_at.mdifferentiable_at
/-- For maps between vector spaces, `mdifferentiable_on` and `differentiable_on` coincide -/
theorem mdifferentiable_on_iff_differentiable_on :
mdifferentiable_on (𝓘(𝕜, E)) (𝓘(𝕜, E')) f s ↔ differentiable_on 𝕜 f s :=
by simp only [mdifferentiable_on, differentiable_on,
mdifferentiable_within_at_iff_differentiable_within_at]
alias mdifferentiable_on_iff_differentiable_on ↔
mdifferentiable_on.differentiable_on differentiable_on.mdifferentiable_on
/-- For maps between vector spaces, `mdifferentiable` and `differentiable` coincide -/
theorem mdifferentiable_iff_differentiable :
mdifferentiable (𝓘(𝕜, E)) (𝓘(𝕜, E')) f ↔ differentiable 𝕜 f :=
by simp only [mdifferentiable, differentiable, mdifferentiable_at_iff_differentiable_at]
alias mdifferentiable_iff_differentiable ↔
mdifferentiable.differentiable differentiable.mdifferentiable
/-- For maps between vector spaces, `mfderiv_within` and `fderiv_within` coincide -/
@[simp] theorem mfderiv_within_eq_fderiv_within :
mfderiv_within (𝓘(𝕜, E)) (𝓘(𝕜, E')) f s x = fderiv_within 𝕜 f s x :=
begin
by_cases h : mdifferentiable_within_at (𝓘(𝕜, E)) (𝓘(𝕜, E')) f s x,
{ simp only [mfderiv_within, h, if_pos] with mfld_simps },
{ simp only [mfderiv_within, h, if_neg, not_false_iff],
rw [mdifferentiable_within_at_iff_differentiable_within_at] at h,
exact (fderiv_within_zero_of_not_differentiable_within_at h).symm }
end
/-- For maps between vector spaces, `mfderiv` and `fderiv` coincide -/
@[simp] theorem mfderiv_eq_fderiv :
mfderiv (𝓘(𝕜, E)) (𝓘(𝕜, E')) f x = fderiv 𝕜 f x :=
begin
rw [← mfderiv_within_univ, ← fderiv_within_univ],
exact mfderiv_within_eq_fderiv_within
end
end mfderiv_fderiv
section specific_functions
/-! ### Differentiability of specific functions -/
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
{M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M]
{E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H')
{M' : Type*} [topological_space M'] [charted_space H' M'] [smooth_manifold_with_corners I' M']
namespace continuous_linear_map
variables (f : E →L[𝕜] E') {s : set E} {x : E}
protected lemma has_mfderiv_within_at : has_mfderiv_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x f :=
f.has_fderiv_within_at.has_mfderiv_within_at
protected lemma has_mfderiv_at : has_mfderiv_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x f :=
f.has_fderiv_at.has_mfderiv_at
protected lemma mdifferentiable_within_at : mdifferentiable_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x :=
f.differentiable_within_at.mdifferentiable_within_at
protected lemma mdifferentiable_on : mdifferentiable_on 𝓘(𝕜, E) 𝓘(𝕜, E') f s :=
f.differentiable_on.mdifferentiable_on
protected lemma mdifferentiable_at : mdifferentiable_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x :=
f.differentiable_at.mdifferentiable_at
protected lemma mdifferentiable : mdifferentiable 𝓘(𝕜, E) 𝓘(𝕜, E') f :=
f.differentiable.mdifferentiable
lemma mfderiv_eq : mfderiv 𝓘(𝕜, E) 𝓘(𝕜, E') f x = f :=
f.has_mfderiv_at.mfderiv
lemma mfderiv_within_eq (hs : unique_mdiff_within_at 𝓘(𝕜, E) s x) :
mfderiv_within 𝓘(𝕜, E) 𝓘(𝕜, E') f s x = f :=
f.has_mfderiv_within_at.mfderiv_within hs
end continuous_linear_map
namespace continuous_linear_equiv
variables (f : E ≃L[𝕜] E') {s : set E} {x : E}
protected lemma has_mfderiv_within_at :
has_mfderiv_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x (f : E →L[𝕜] E') :=
f.has_fderiv_within_at.has_mfderiv_within_at
protected lemma has_mfderiv_at : has_mfderiv_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x (f : E →L[𝕜] E') :=
f.has_fderiv_at.has_mfderiv_at
protected lemma mdifferentiable_within_at : mdifferentiable_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') f s x :=
f.differentiable_within_at.mdifferentiable_within_at
protected lemma mdifferentiable_on : mdifferentiable_on 𝓘(𝕜, E) 𝓘(𝕜, E') f s :=
f.differentiable_on.mdifferentiable_on
protected lemma mdifferentiable_at : mdifferentiable_at 𝓘(𝕜, E) 𝓘(𝕜, E') f x :=
f.differentiable_at.mdifferentiable_at
protected lemma mdifferentiable : mdifferentiable 𝓘(𝕜, E) 𝓘(𝕜, E') f :=
f.differentiable.mdifferentiable
lemma mfderiv_eq : mfderiv 𝓘(𝕜, E) 𝓘(𝕜, E') f x = (f : E →L[𝕜] E') :=
f.has_mfderiv_at.mfderiv
lemma mfderiv_within_eq (hs : unique_mdiff_within_at 𝓘(𝕜, E) s x) :
mfderiv_within 𝓘(𝕜, E) 𝓘(𝕜, E') f s x = (f : E →L[𝕜] E') :=
f.has_mfderiv_within_at.mfderiv_within hs
end continuous_linear_equiv
variables {s : set M} {x : M}
section id
/-! #### Identity -/
lemma has_mfderiv_at_id (x : M) :
has_mfderiv_at I I (@_root_.id M) x (continuous_linear_map.id 𝕜 (tangent_space I x)) :=
begin
refine ⟨continuous_id.continuous_at, _⟩,
have : ∀ᶠ y in 𝓝[range I] ((ext_chart_at I x) x),
((ext_chart_at I x) ∘ (ext_chart_at I x).symm) y = id y,
{ apply filter.mem_of_superset (ext_chart_at_target_mem_nhds_within I x),
mfld_set_tac },
apply has_fderiv_within_at.congr_of_eventually_eq (has_fderiv_within_at_id _ _) this,
simp only with mfld_simps
end
theorem has_mfderiv_within_at_id (s : set M) (x : M) :
has_mfderiv_within_at I I (@_root_.id M) s x (continuous_linear_map.id 𝕜 (tangent_space I x)) :=
(has_mfderiv_at_id I x).has_mfderiv_within_at
lemma mdifferentiable_at_id : mdifferentiable_at I I (@_root_.id M) x :=
(has_mfderiv_at_id I x).mdifferentiable_at
lemma mdifferentiable_within_at_id : mdifferentiable_within_at I I (@_root_.id M) s x :=
(mdifferentiable_at_id I).mdifferentiable_within_at
lemma mdifferentiable_id : mdifferentiable I I (@_root_.id M) :=
λx, mdifferentiable_at_id I
lemma mdifferentiable_on_id : mdifferentiable_on I I (@_root_.id M) s :=
(mdifferentiable_id I).mdifferentiable_on
@[simp, mfld_simps] lemma mfderiv_id :
mfderiv I I (@_root_.id M) x = (continuous_linear_map.id 𝕜 (tangent_space I x)) :=
has_mfderiv_at.mfderiv (has_mfderiv_at_id I x)
lemma mfderiv_within_id (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I (@_root_.id M) s x = (continuous_linear_map.id 𝕜 (tangent_space I x)) :=
begin
rw mdifferentiable.mfderiv_within (mdifferentiable_at_id I) hxs,
exact mfderiv_id I
end
@[simp, mfld_simps] lemma tangent_map_id : tangent_map I I (id : M → M) = id :=
by { ext1 ⟨x, v⟩, simp [tangent_map] }
lemma tangent_map_within_id {p : tangent_bundle I M}
(hs : unique_mdiff_within_at I s p.proj) :
tangent_map_within I I (id : M → M) s p = p :=
begin
simp only [tangent_map_within, id.def],
rw mfderiv_within_id,
{ rcases p, refl },
{ exact hs }
end
end id
section const
/-! #### Constants -/
variables {c : M'}
lemma has_mfderiv_at_const (c : M') (x : M) :
has_mfderiv_at I I' (λy : M, c) x
(0 : tangent_space I x →L[𝕜] tangent_space I' c) :=
begin
refine ⟨continuous_const.continuous_at, _⟩,
simp only [written_in_ext_chart_at, (∘), has_fderiv_within_at_const]
end
theorem has_mfderiv_within_at_const (c : M') (s : set M) (x : M) :
has_mfderiv_within_at I I' (λy : M, c) s x
(0 : tangent_space I x →L[𝕜] tangent_space I' c) :=
(has_mfderiv_at_const I I' c x).has_mfderiv_within_at
lemma mdifferentiable_at_const : mdifferentiable_at I I' (λy : M, c) x :=
(has_mfderiv_at_const I I' c x).mdifferentiable_at
lemma mdifferentiable_within_at_const : mdifferentiable_within_at I I' (λy : M, c) s x :=
(mdifferentiable_at_const I I').mdifferentiable_within_at
lemma mdifferentiable_const : mdifferentiable I I' (λy : M, c) :=
λx, mdifferentiable_at_const I I'
lemma mdifferentiable_on_const : mdifferentiable_on I I' (λy : M, c) s :=
(mdifferentiable_const I I').mdifferentiable_on
@[simp, mfld_simps] lemma mfderiv_const : mfderiv I I' (λy : M, c) x =
(0 : tangent_space I x →L[𝕜] tangent_space I' c) :=
has_mfderiv_at.mfderiv (has_mfderiv_at_const I I' c x)
lemma mfderiv_within_const (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I' (λy : M, c) s x =
(0 : tangent_space I x →L[𝕜] tangent_space I' c) :=
(has_mfderiv_within_at_const _ _ _ _ _).mfderiv_within hxs
end const
section arithmetic
/-! #### Arithmetic
Note that in the in `has_mfderiv_at` lemmas there is an abuse of the defeq between `E'` and
`tangent_space 𝓘(𝕜, E') (f z)` (similarly for `g',F',p',q'`). In general this defeq is not
canonical, but in this case (the tangent space of a vector space) it is canonical.
-/
section group
variables {I} {z : M} {f g : M → E'} {f' g' : tangent_space I z →L[𝕜] E'}
lemma has_mfderiv_at.add (hf : has_mfderiv_at I 𝓘(𝕜, E') f z f')
(hg : has_mfderiv_at I 𝓘(𝕜, E') g z g') : has_mfderiv_at I 𝓘(𝕜, E') (f + g) z (f' + g') :=
⟨hf.1.add hg.1, hf.2.add hg.2⟩
lemma mdifferentiable_at.add (hf : mdifferentiable_at I 𝓘(𝕜, E') f z)
(hg : mdifferentiable_at I 𝓘(𝕜, E') g z) : mdifferentiable_at I 𝓘(𝕜, E') (f + g) z :=
(hf.has_mfderiv_at.add hg.has_mfderiv_at).mdifferentiable_at
lemma mdifferentiable.add (hf : mdifferentiable I 𝓘(𝕜, E') f) (hg : mdifferentiable I 𝓘(𝕜, E') g) :
mdifferentiable I 𝓘(𝕜, E') (f + g) :=
λ x, (hf x).add (hg x)
lemma mfderiv_add (hf : mdifferentiable_at I 𝓘(𝕜, E') f z)
(hg : mdifferentiable_at I 𝓘(𝕜, E') g z) :
(mfderiv I 𝓘(𝕜, E') (f + g) z : tangent_space I z →L[𝕜] E') =
(mfderiv I 𝓘(𝕜, E') f z + mfderiv I 𝓘(𝕜, E') g z : tangent_space I z →L[𝕜] E') :=
(hf.has_mfderiv_at.add hg.has_mfderiv_at).mfderiv
lemma has_mfderiv_at.const_smul (hf : has_mfderiv_at I 𝓘(𝕜, E') f z f') (s : 𝕜) :
has_mfderiv_at I 𝓘(𝕜, E') (s • f) z (s • f') :=
⟨hf.1.const_smul s, hf.2.const_smul s⟩
lemma mdifferentiable_at.const_smul (hf : mdifferentiable_at I 𝓘(𝕜, E') f z) (s : 𝕜) :
mdifferentiable_at I 𝓘(𝕜, E') (s • f) z :=
(hf.has_mfderiv_at.const_smul s).mdifferentiable_at
lemma mdifferentiable.const_smul (s : 𝕜) (hf : mdifferentiable I 𝓘(𝕜, E') f) :
mdifferentiable I 𝓘(𝕜, E') (s • f) :=
λ x, (hf x).const_smul s
lemma const_smul_mfderiv (hf : mdifferentiable_at I 𝓘(𝕜, E') f z) (s : 𝕜) :
(mfderiv I 𝓘(𝕜, E') (s • f) z : tangent_space I z →L[𝕜] E') =
(s • mfderiv I 𝓘(𝕜, E') f z : tangent_space I z →L[𝕜] E') :=
(hf.has_mfderiv_at.const_smul s).mfderiv
lemma has_mfderiv_at.neg (hf : has_mfderiv_at I 𝓘(𝕜, E') f z f') :
has_mfderiv_at I 𝓘(𝕜, E') (-f) z (-f') :=
⟨hf.1.neg, hf.2.neg⟩
lemma has_mfderiv_at_neg :
has_mfderiv_at I 𝓘(𝕜, E') (-f) z (-f') ↔ has_mfderiv_at I 𝓘(𝕜, E') f z f' :=
⟨λ hf, by { convert hf.neg; rw [neg_neg] }, λ hf, hf.neg⟩
lemma mdifferentiable_at.neg (hf : mdifferentiable_at I 𝓘(𝕜, E') f z) :
mdifferentiable_at I 𝓘(𝕜, E') (-f) z :=
hf.has_mfderiv_at.neg.mdifferentiable_at
lemma mdifferentiable_at_neg :
mdifferentiable_at I 𝓘(𝕜, E') (-f) z ↔ mdifferentiable_at I 𝓘(𝕜, E') f z :=
⟨λ hf, by { convert hf.neg; rw [neg_neg] }, λ hf, hf.neg⟩
lemma mdifferentiable.neg (hf : mdifferentiable I 𝓘(𝕜, E') f) :
mdifferentiable I 𝓘(𝕜, E') (-f) :=
λ x, (hf x).neg
lemma mfderiv_neg (f : M → E') (x : M) :
(mfderiv I 𝓘(𝕜, E') (-f) x : tangent_space I x →L[𝕜] E') =
(- mfderiv I 𝓘(𝕜, E') f x : tangent_space I x →L[𝕜] E') :=
begin
simp_rw [mfderiv],
by_cases hf : mdifferentiable_at I 𝓘(𝕜, E') f x,
{ exact hf.has_mfderiv_at.neg.mfderiv },
{ rw [if_neg hf], rw [← mdifferentiable_at_neg] at hf, rw [if_neg hf, neg_zero] },
end
lemma has_mfderiv_at.sub (hf : has_mfderiv_at I 𝓘(𝕜, E') f z f')
(hg : has_mfderiv_at I 𝓘(𝕜, E') g z g') : has_mfderiv_at I 𝓘(𝕜, E') (f - g) z (f'- g') :=
⟨hf.1.sub hg.1, hf.2.sub hg.2⟩
lemma mdifferentiable_at.sub (hf : mdifferentiable_at I 𝓘(𝕜, E') f z)
(hg : mdifferentiable_at I 𝓘(𝕜, E') g z) : mdifferentiable_at I 𝓘(𝕜, E') (f - g) z :=
(hf.has_mfderiv_at.sub hg.has_mfderiv_at).mdifferentiable_at
lemma mdifferentiable.sub (hf : mdifferentiable I 𝓘(𝕜, E') f)
(hg : mdifferentiable I 𝓘(𝕜, E') g) : mdifferentiable I 𝓘(𝕜, E') (f - g) :=
λ x, (hf x).sub (hg x)
lemma mfderiv_sub (hf : mdifferentiable_at I 𝓘(𝕜, E') f z)
(hg : mdifferentiable_at I 𝓘(𝕜, E') g z) :
(mfderiv I 𝓘(𝕜, E') (f - g) z : tangent_space I z →L[𝕜] E') =
(mfderiv I 𝓘(𝕜, E') f z - mfderiv I 𝓘(𝕜, E') g z : tangent_space I z →L[𝕜] E') :=
(hf.has_mfderiv_at.sub hg.has_mfderiv_at).mfderiv
end group
section algebra_over_ring
variables {I} {z : M} {F' : Type*} [normed_ring F'] [normed_algebra 𝕜 F']
{p q : M → F'} {p' q' : tangent_space I z →L[𝕜] F'}
lemma has_mfderiv_within_at.mul' (hp : has_mfderiv_within_at I 𝓘(𝕜, F') p s z p')
(hq : has_mfderiv_within_at I 𝓘(𝕜, F') q s z q') :
has_mfderiv_within_at I 𝓘(𝕜, F') (p * q) s z (p z • q' + p'.smul_right (q z) : E →L[𝕜] F') :=
⟨hp.1.mul hq.1, by simpa only with mfld_simps using hp.2.mul' hq.2⟩
lemma has_mfderiv_at.mul' (hp : has_mfderiv_at I 𝓘(𝕜, F') p z p')
(hq : has_mfderiv_at I 𝓘(𝕜, F') q z q') :
has_mfderiv_at I 𝓘(𝕜, F') (p * q) z (p z • q' + p'.smul_right (q z) : E →L[𝕜] F') :=
has_mfderiv_within_at_univ.mp $ hp.has_mfderiv_within_at.mul' hq.has_mfderiv_within_at
lemma mdifferentiable_within_at.mul (hp : mdifferentiable_within_at I 𝓘(𝕜, F') p s z)
(hq : mdifferentiable_within_at I 𝓘(𝕜, F') q s z) :
mdifferentiable_within_at I 𝓘(𝕜, F') (p * q) s z :=
(hp.has_mfderiv_within_at.mul' hq.has_mfderiv_within_at).mdifferentiable_within_at
lemma mdifferentiable_at.mul (hp : mdifferentiable_at I 𝓘(𝕜, F') p z)
(hq : mdifferentiable_at I 𝓘(𝕜, F') q z) : mdifferentiable_at I 𝓘(𝕜, F') (p * q) z :=
(hp.has_mfderiv_at.mul' hq.has_mfderiv_at).mdifferentiable_at
lemma mdifferentiable_on.mul (hp : mdifferentiable_on I 𝓘(𝕜, F') p s)
(hq : mdifferentiable_on I 𝓘(𝕜, F') q s) : mdifferentiable_on I 𝓘(𝕜, F') (p * q) s :=
λ x hx, (hp x hx).mul $ hq x hx
lemma mdifferentiable.mul (hp : mdifferentiable I 𝓘(𝕜, F') p)
(hq : mdifferentiable I 𝓘(𝕜, F') q) : mdifferentiable I 𝓘(𝕜, F') (p * q) :=
λ x, (hp x).mul (hq x)
end algebra_over_ring
section algebra_over_comm_ring
variables {I} {z : M} {F' : Type*} [normed_comm_ring F'] [normed_algebra 𝕜 F']
{p q : M → F'} {p' q' : tangent_space I z →L[𝕜] F'}
lemma has_mfderiv_within_at.mul (hp : has_mfderiv_within_at I 𝓘(𝕜, F') p s z p')
(hq : has_mfderiv_within_at I 𝓘(𝕜, F') q s z q') :
has_mfderiv_within_at I 𝓘(𝕜, F') (p * q) s z (p z • q' + q z • p' : E →L[𝕜] F') :=
by { convert hp.mul' hq, ext z, apply mul_comm }
lemma has_mfderiv_at.mul (hp : has_mfderiv_at I 𝓘(𝕜, F') p z p')
(hq : has_mfderiv_at I 𝓘(𝕜, F') q z q') :
has_mfderiv_at I 𝓘(𝕜, F') (p * q) z (p z • q' + q z • p' : E →L[𝕜] F') :=
has_mfderiv_within_at_univ.mp $ hp.has_mfderiv_within_at.mul hq.has_mfderiv_within_at
end algebra_over_comm_ring
end arithmetic
namespace model_with_corners
/-! #### Model with corners -/
protected lemma has_mfderiv_at {x} :
has_mfderiv_at I 𝓘(𝕜, E) I x (continuous_linear_map.id _ _) :=
⟨I.continuous_at, (has_fderiv_within_at_id _ _).congr' I.right_inv_on (mem_range_self _)⟩
protected lemma has_mfderiv_within_at {s x} :
has_mfderiv_within_at I 𝓘(𝕜, E) I s x (continuous_linear_map.id _ _) :=
I.has_mfderiv_at.has_mfderiv_within_at
protected lemma mdifferentiable_within_at {s x} :
mdifferentiable_within_at I 𝓘(𝕜, E) I s x :=
I.has_mfderiv_within_at.mdifferentiable_within_at
protected lemma mdifferentiable_at {x} :
mdifferentiable_at I 𝓘(𝕜, E) I x :=
I.has_mfderiv_at.mdifferentiable_at
protected lemma mdifferentiable_on {s} :
mdifferentiable_on I 𝓘(𝕜, E) I s :=
λ x hx, I.mdifferentiable_within_at
protected lemma mdifferentiable :
mdifferentiable I (𝓘(𝕜, E)) I :=
λ x, I.mdifferentiable_at
lemma has_mfderiv_within_at_symm {x} (hx : x ∈ range I) :
has_mfderiv_within_at 𝓘(𝕜, E) I I.symm (range I) x (continuous_linear_map.id _ _) :=
⟨I.continuous_within_at_symm, (has_fderiv_within_at_id _ _).congr'
(λ y hy, I.right_inv_on hy.1) ⟨hx, mem_range_self _⟩⟩
lemma mdifferentiable_on_symm :
mdifferentiable_on (𝓘(𝕜, E)) I I.symm (range I) :=
λ x hx, (I.has_mfderiv_within_at_symm hx).mdifferentiable_within_at
end model_with_corners
section charts
variable {e : local_homeomorph M H}
lemma mdifferentiable_at_atlas (h : e ∈ atlas H M) {x : M} (hx : x ∈ e.source) :
mdifferentiable_at I I e x :=
begin
refine ⟨(e.continuous_on x hx).continuous_at (is_open.mem_nhds e.open_source hx), _⟩,
have mem : I ((chart_at H x : M → H) x) ∈
I.symm ⁻¹' ((chart_at H x).symm ≫ₕ e).source ∩ range I,
by simp only [hx] with mfld_simps,
have : (chart_at H x).symm.trans e ∈ cont_diff_groupoid ∞ I :=
has_groupoid.compatible _ (chart_mem_atlas H x) h,
have A : cont_diff_on 𝕜 ∞
(I ∘ ((chart_at H x).symm.trans e) ∘ I.symm)
(I.symm ⁻¹' ((chart_at H x).symm.trans e).source ∩ range I) :=
this.1,
have B := A.differentiable_on le_top (I ((chart_at H x : M → H) x)) mem,
simp only with mfld_simps at B,
rw [inter_comm, differentiable_within_at_inter] at B,
{ simpa only with mfld_simps },
{ apply is_open.mem_nhds ((local_homeomorph.open_source _).preimage I.continuous_symm) mem.1 }
end
lemma mdifferentiable_on_atlas (h : e ∈ atlas H M) :
mdifferentiable_on I I e e.source :=
λx hx, (mdifferentiable_at_atlas I h hx).mdifferentiable_within_at
lemma mdifferentiable_at_atlas_symm (h : e ∈ atlas H M) {x : H} (hx : x ∈ e.target) :
mdifferentiable_at I I e.symm x :=
begin
refine ⟨(e.continuous_on_symm x hx).continuous_at (is_open.mem_nhds e.open_target hx), _⟩,
have mem : I x ∈ I.symm ⁻¹' (e.symm ≫ₕ chart_at H (e.symm x)).source ∩ range (I),
by simp only [hx] with mfld_simps,
have : e.symm.trans (chart_at H (e.symm x)) ∈ cont_diff_groupoid ∞ I :=
has_groupoid.compatible _ h (chart_mem_atlas H _),
have A : cont_diff_on 𝕜 ∞
(I ∘ (e.symm.trans (chart_at H (e.symm x))) ∘ I.symm)
(I.symm ⁻¹' (e.symm.trans (chart_at H (e.symm x))).source ∩ range I) :=
this.1,
have B := A.differentiable_on le_top (I x) mem,
simp only with mfld_simps at B,
rw [inter_comm, differentiable_within_at_inter] at B,
{ simpa only with mfld_simps },
{ apply (is_open.mem_nhds ((local_homeomorph.open_source _).preimage I.continuous_symm) mem.1) }
end
lemma mdifferentiable_on_atlas_symm (h : e ∈ atlas H M) :
mdifferentiable_on I I e.symm e.target :=
λx hx, (mdifferentiable_at_atlas_symm I h hx).mdifferentiable_within_at
lemma mdifferentiable_of_mem_atlas (h : e ∈ atlas H M) : e.mdifferentiable I I :=
⟨mdifferentiable_on_atlas I h, mdifferentiable_on_atlas_symm I h⟩
lemma mdifferentiable_chart (x : M) : (chart_at H x).mdifferentiable I I :=
mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)
/-- The derivative of the chart at a base point is the chart of the tangent bundle, composed with
the identification between the tangent bundle of the model space and the product space. -/
lemma tangent_map_chart {p q : tangent_bundle I M} (h : q.1 ∈ (chart_at H p.1).source) :
tangent_map I I (chart_at H p.1) q =
(equiv.sigma_equiv_prod _ _).symm
((chart_at (model_prod H E) p : tangent_bundle I M → model_prod H E) q) :=
begin
dsimp [tangent_map],
rw mdifferentiable_at.mfderiv,
{ refl },
{ exact mdifferentiable_at_atlas _ (chart_mem_atlas _ _) h }
end
/-- The derivative of the inverse of the chart at a base point is the inverse of the chart of the
tangent bundle, composed with the identification between the tangent bundle of the model space and
the product space. -/
lemma tangent_map_chart_symm {p : tangent_bundle I M} {q : tangent_bundle I H}
(h : q.1 ∈ (chart_at H p.1).target) :
tangent_map I I (chart_at H p.1).symm q =
((chart_at (model_prod H E) p).symm : model_prod H E → tangent_bundle I M)
((equiv.sigma_equiv_prod H E) q) :=
begin
dsimp only [tangent_map],
rw mdifferentiable_at.mfderiv (mdifferentiable_at_atlas_symm _ (chart_mem_atlas _ _) h),
-- a trivial instance is needed after the rewrite, handle it right now.
rotate, { apply_instance },
simp only [continuous_linear_map.coe_coe, tangent_bundle.chart_at, h,
tangent_bundle_core, chart_at, sigma.mk.inj_iff] with mfld_simps,
end
end charts
end specific_functions
/-! ### Differentiable local homeomorphisms -/
namespace local_homeomorph.mdifferentiable
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H}
{M : Type*} [topological_space M] [charted_space H M]
{E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{M' : Type*} [topological_space M'] [charted_space H' M']
{E'' : Type*} [normed_add_comm_group E''] [normed_space 𝕜 E'']
{H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''}
{M'' : Type*} [topological_space M''] [charted_space H'' M'']
{e : local_homeomorph M M'} (he : e.mdifferentiable I I')
{e' : local_homeomorph M' M''}
include he
lemma symm : e.symm.mdifferentiable I' I :=
⟨he.2, he.1⟩
protected lemma mdifferentiable_at {x : M} (hx : x ∈ e.source) :
mdifferentiable_at I I' e x :=
(he.1 x hx).mdifferentiable_at (is_open.mem_nhds e.open_source hx)
lemma mdifferentiable_at_symm {x : M'} (hx : x ∈ e.target) :
mdifferentiable_at I' I e.symm x :=
(he.2 x hx).mdifferentiable_at (is_open.mem_nhds e.open_target hx)
variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M']
[smooth_manifold_with_corners I'' M'']
lemma symm_comp_deriv {x : M} (hx : x ∈ e.source) :
(mfderiv I' I e.symm (e x)).comp (mfderiv I I' e x) =
continuous_linear_map.id 𝕜 (tangent_space I x) :=
begin
have : (mfderiv I I (e.symm ∘ e) x) =
(mfderiv I' I e.symm (e x)).comp (mfderiv I I' e x) :=
mfderiv_comp x (he.mdifferentiable_at_symm (e.map_source hx)) (he.mdifferentiable_at hx),
rw ← this,
have : mfderiv I I (_root_.id : M → M) x = continuous_linear_map.id _ _ := mfderiv_id I,
rw ← this,
apply filter.eventually_eq.mfderiv_eq,
have : e.source ∈ 𝓝 x := is_open.mem_nhds e.open_source hx,
exact filter.mem_of_superset this (by mfld_set_tac)
end
lemma comp_symm_deriv {x : M'} (hx : x ∈ e.target) :
(mfderiv I I' e (e.symm x)).comp (mfderiv I' I e.symm x) =
continuous_linear_map.id 𝕜 (tangent_space I' x) :=
he.symm.symm_comp_deriv hx
/-- The derivative of a differentiable local homeomorphism, as a continuous linear equivalence
between the tangent spaces at `x` and `e x`. -/
protected def mfderiv {x : M} (hx : x ∈ e.source) :
tangent_space I x ≃L[𝕜] tangent_space I' (e x) :=
{ inv_fun := (mfderiv I' I e.symm (e x)),
continuous_to_fun := (mfderiv I I' e x).cont,
continuous_inv_fun := (mfderiv I' I e.symm (e x)).cont,
left_inv := λy, begin
have : (continuous_linear_map.id _ _ : tangent_space I x →L[𝕜] tangent_space I x) y = y := rfl,
conv_rhs { rw [← this, ← he.symm_comp_deriv hx] },
refl
end,
right_inv := λy, begin
have : (continuous_linear_map.id 𝕜 _ :
tangent_space I' (e x) →L[𝕜] tangent_space I' (e x)) y = y := rfl,
conv_rhs { rw [← this, ← he.comp_symm_deriv (e.map_source hx)] },
rw e.left_inv hx,
refl
end,
.. mfderiv I I' e x }
lemma mfderiv_bijective {x : M} (hx : x ∈ e.source) :
function.bijective (mfderiv I I' e x) :=
(he.mfderiv hx).bijective
lemma mfderiv_injective {x : M} (hx : x ∈ e.source) :
function.injective (mfderiv I I' e x) :=
(he.mfderiv hx).injective
lemma mfderiv_surjective {x : M} (hx : x ∈ e.source) :
function.surjective (mfderiv I I' e x) :=
(he.mfderiv hx).surjective
lemma ker_mfderiv_eq_bot {x : M} (hx : x ∈ e.source) :
linear_map.ker (mfderiv I I' e x) = ⊥ :=
(he.mfderiv hx).to_linear_equiv.ker
lemma range_mfderiv_eq_top {x : M} (hx : x ∈ e.source) :
linear_map.range (mfderiv I I' e x) = ⊤ :=
(he.mfderiv hx).to_linear_equiv.range
lemma range_mfderiv_eq_univ {x : M} (hx : x ∈ e.source) :
range (mfderiv I I' e x) = univ :=
(he.mfderiv_surjective hx).range_eq
lemma trans (he': e'.mdifferentiable I' I'') : (e.trans e').mdifferentiable I I'' :=
begin
split,
{ assume x hx,
simp only with mfld_simps at hx,
exact ((he'.mdifferentiable_at hx.2).comp _
(he.mdifferentiable_at hx.1)).mdifferentiable_within_at },
{ assume x hx,
simp only with mfld_simps at hx,
exact ((he.symm.mdifferentiable_at hx.2).comp _
(he'.symm.mdifferentiable_at hx.1)).mdifferentiable_within_at }
end
end local_homeomorph.mdifferentiable
/-! ### Differentiability of `ext_chart_at` -/
section ext_chart_at
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
{M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M]
{s : set M} {x y : M}
lemma has_mfderiv_at_ext_chart_at (h : y ∈ (chart_at H x).source) :
has_mfderiv_at I 𝓘(𝕜, E) (ext_chart_at I x) y (mfderiv I I (chart_at H x) y : _) :=
I.has_mfderiv_at.comp y ((mdifferentiable_chart I x).mdifferentiable_at h).has_mfderiv_at
lemma has_mfderiv_within_at_ext_chart_at (h : y ∈ (chart_at H x).source) :
has_mfderiv_within_at I 𝓘(𝕜, E) (ext_chart_at I x) s y (mfderiv I I (chart_at H x) y : _) :=
(has_mfderiv_at_ext_chart_at I h).has_mfderiv_within_at
lemma mdifferentiable_at_ext_chart_at (h : y ∈ (chart_at H x).source) :
mdifferentiable_at I 𝓘(𝕜, E) (ext_chart_at I x) y :=
(has_mfderiv_at_ext_chart_at I h).mdifferentiable_at
lemma mdifferentiable_on_ext_chart_at :
mdifferentiable_on I 𝓘(𝕜, E) (ext_chart_at I x) (chart_at H x).source :=
λ y hy, (has_mfderiv_within_at_ext_chart_at I hy).mdifferentiable_within_at
end ext_chart_at
/-! ### Unique derivative sets in manifolds -/
section unique_mdiff
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H}
{M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M]
{E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{M' : Type*} [topological_space M'] [charted_space H' M']
{s : set M}
/-- If a set has the unique differential property, then its image under a local
diffeomorphism also has the unique differential property. -/
lemma unique_mdiff_on.unique_mdiff_on_preimage [smooth_manifold_with_corners I' M']
(hs : unique_mdiff_on I s) {e : local_homeomorph M M'} (he : e.mdifferentiable I I') :
unique_mdiff_on I' (e.target ∩ e.symm ⁻¹' s) :=
begin
/- Start from a point `x` in the image, and let `z` be its preimage. Then the unique
derivative property at `x` is expressed through `ext_chart_at I' x`, and the unique
derivative property at `z` is expressed through `ext_chart_at I z`. We will argue that
the composition of these two charts with `e` is a local diffeomorphism in vector spaces,
and therefore preserves the unique differential property thanks to lemma
`has_fderiv_within_at.unique_diff_within_at`, saying that a differentiable function with onto
derivative preserves the unique derivative property.-/
assume x hx,
let z := e.symm x,
have z_source : z ∈ e.source, by simp only [hx.1] with mfld_simps,
have zx : e z = x, by simp only [z, hx.1] with mfld_simps,
let F := ext_chart_at I z,
-- the unique derivative property at `z` is expressed through its preferred chart,
-- that we call `F`.
have B : unique_diff_within_at 𝕜
(F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F z),
{ have : unique_mdiff_within_at I s z := hs _ hx.2,
have S : e.source ∩ e ⁻¹' ((ext_chart_at I' x).source) ∈ 𝓝 z,
{ apply is_open.mem_nhds,
apply e.continuous_on.preimage_open_of_open e.open_source (is_open_ext_chart_at_source I' x),
simp only [z_source, zx] with mfld_simps },
have := this.inter S,
rw [unique_mdiff_within_at_iff] at this,
exact this },
-- denote by `G` the change of coordinate, i.e., the composition of the two extended charts and
-- of `e`
let G := F.symm ≫ e.to_local_equiv ≫ (ext_chart_at I' x),
-- `G` is differentiable
have Diff : ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).mdifferentiable I I',
{ have A := mdifferentiable_of_mem_atlas I (chart_mem_atlas H z),
have B := mdifferentiable_of_mem_atlas I' (chart_mem_atlas H' x),
exact A.symm.trans (he.trans B) },
have Mmem : (chart_at H z : M → H) z ∈ ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).source,
by simp only [z_source, zx] with mfld_simps,
have A : differentiable_within_at 𝕜 G (range I) (F z),
{ refine (Diff.mdifferentiable_at Mmem).2.congr (λp hp, _) _;
simp only [G, F] with mfld_simps },
-- let `G'` be its derivative
let G' := fderiv_within 𝕜 G (range I) (F z),
have D₁ : has_fderiv_within_at G G' (range I) (F z) :=
A.has_fderiv_within_at,
have D₂ : has_fderiv_within_at G G'
(F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F z) :=
D₁.mono (by mfld_set_tac),
-- The derivative `G'` is onto, as it is the derivative of a local diffeomorphism, the composition
-- of the two charts and of `e`.
have C : dense_range (G' : E → E'),
{ have : G' = mfderiv I I' ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x))
((chart_at H z : M → H) z),
by { rw (Diff.mdifferentiable_at Mmem).mfderiv, refl },
rw this,
exact (Diff.mfderiv_surjective Mmem).dense_range },
-- key step: thanks to what we have proved about it, `G` preserves the unique derivative property
have key : unique_diff_within_at 𝕜
(G '' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target))
(G (F z)) := D₂.unique_diff_within_at B C,
have : G (F z) = (ext_chart_at I' x) x, by { dsimp [G, F], simp only [hx.1] with mfld_simps },
rw this at key,
apply key.mono,
show G '' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) ⊆
(ext_chart_at I' x).symm ⁻¹' e.target ∩ (ext_chart_at I' x).symm ⁻¹' (e.symm ⁻¹' s) ∩
range (I'),
rw image_subset_iff,
mfld_set_tac
end
/-- If a set in a manifold has the unique derivative property, then its pullback by any extended
chart, in the vector space, also has the unique derivative property. -/
lemma unique_mdiff_on.unique_diff_on_target_inter (hs : unique_mdiff_on I s) (x : M) :
unique_diff_on 𝕜 ((ext_chart_at I x).target ∩ ((ext_chart_at I x).symm ⁻¹' s)) :=
begin
-- this is just a reformulation of `unique_mdiff_on.unique_mdiff_on_preimage`, using as `e`
-- the local chart at `x`.
assume z hz,
simp only with mfld_simps at hz,
have : (chart_at H x).mdifferentiable I I := mdifferentiable_chart _ _,
have T := (hs.unique_mdiff_on_preimage this) (I.symm z),
simp only [hz.left.left, hz.left.right, hz.right, unique_mdiff_within_at] with mfld_simps at ⊢ T,
convert T using 1,
rw @preimage_comp _ _ _ _ (chart_at H x).symm,
mfld_set_tac
end
/-- When considering functions between manifolds, this statement shows up often. It entails
the unique differential of the pullback in extended charts of the set where the function can
be read in the charts. -/
lemma unique_mdiff_on.unique_diff_on_inter_preimage (hs : unique_mdiff_on I s) (x : M) (y : M')
{f : M → M'} (hf : continuous_on f s) :
unique_diff_on 𝕜 ((ext_chart_at I x).target
∩ ((ext_chart_at I x).symm ⁻¹' (s ∩ f⁻¹' (ext_chart_at I' y).source))) :=
begin
have : unique_mdiff_on I (s ∩ f ⁻¹' (ext_chart_at I' y).source),
{ assume z hz,
apply (hs z hz.1).inter',
apply (hf z hz.1).preimage_mem_nhds_within,
exact (is_open_ext_chart_at_source I' y).mem_nhds hz.2 },
exact this.unique_diff_on_target_inter _
end
open bundle
variables {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F]
(Z : M → Type*) [topological_space (total_space Z)] [∀ b, topological_space (Z b)]
[∀ b, add_comm_monoid (Z b)] [∀ b, module 𝕜 (Z b)]
[fiber_bundle F Z] [vector_bundle 𝕜 F Z] [smooth_vector_bundle F Z I]
/-- In a smooth fiber bundle, the preimage under the projection of a set with
unique differential in the basis also has unique differential. -/
lemma unique_mdiff_on.smooth_bundle_preimage (hs : unique_mdiff_on I s) :
unique_mdiff_on (I.prod (𝓘(𝕜, F))) (π Z ⁻¹' s) :=
begin
/- Using a chart (and the fact that unique differentiability is invariant under charts), we
reduce the situation to the model space, where we can use the fact that products respect
unique differentiability. -/
assume p hp,
replace hp : p.fst ∈ s, by simpa only with mfld_simps using hp,
let e₀ := chart_at H p.1,
let e := chart_at (model_prod H F) p,
have h2s : ∀ x, x ∈ e.target ∩ e.symm ⁻¹' (π Z ⁻¹' s) ↔
(x.1 ∈ e₀.target ∧ (e₀.symm) x.1 ∈ (trivialization_at F Z p.1).base_set) ∧ (e₀.symm) x.1 ∈ s,
{ intro x,
have A : x ∈ e.target ↔ x.1 ∈ e₀.target ∧
(e₀.symm) x.1 ∈ (trivialization_at F Z p.1).base_set,
{ simp only [e, fiber_bundle.charted_space_chart_at, trivialization.mem_target,
bundle.total_space.proj] with mfld_simps },
rw [← A, mem_inter_iff, and.congr_right_iff],
intro hx,
simp only [fiber_bundle.charted_space_chart_at_symm_fst p x hx] with mfld_simps },
-- It suffices to prove unique differentiability in a chart
suffices h : unique_mdiff_on (I.prod (𝓘(𝕜, F)))
(e.target ∩ e.symm ⁻¹' (π Z ⁻¹' s)),
{ have A : unique_mdiff_on (I.prod (𝓘(𝕜, F))) (e.symm.target ∩
e.symm.symm ⁻¹' (e.target ∩ e.symm⁻¹' (π Z ⁻¹' s))),
{ apply h.unique_mdiff_on_preimage,
exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)).symm,
apply_instance },
have : p ∈ e.symm.target ∩ e.symm.symm ⁻¹' (e.target ∩ e.symm⁻¹' (π Z ⁻¹' s)),
{ simp only [e, hp] with mfld_simps },
apply (A _ this).mono,
assume q hq,
simp only [e, local_homeomorph.left_inv _ hq.1] with mfld_simps at hq,
simp only [hq] with mfld_simps },
assume q hq,
simp only [unique_mdiff_within_at, model_with_corners.prod, -preimage_inter] with mfld_simps,
have : 𝓝[(I.symm ⁻¹' (e₀.target ∩ e₀.symm⁻¹' s) ∩ range I) ×ˢ univ] (I q.1, q.2) ≤
𝓝[(λ (p : E × F), (I.symm p.1, p.snd)) ⁻¹' (e.target ∩ e.symm ⁻¹' (π Z ⁻¹' s)) ∩
(range I ×ˢ univ)] (I q.1, q.2),
{ rw [nhds_within_le_iff, mem_nhds_within],
refine ⟨(λ (p : E × F), (I.symm p.1, p.snd)) ⁻¹' e.target, _, _, _⟩,
{ exact e.open_target.preimage (I.continuous_symm.prod_map continuous_id) },
{ simp only [prod.mk.eta] with mfld_simps at hq,
simp only [prod.mk.eta, hq] with mfld_simps },
rintro x hx,
simp only with mfld_simps at hx,
have h2x := hx,
simp only [e, fiber_bundle.charted_space_chart_at, trivialization.mem_target]
with mfld_simps at h2x,
simp only [h2s, hx, h2x, -preimage_inter] with mfld_simps },
refine unique_diff_within_at.mono_nhds _ this,
rw [h2s] at hq,
-- apply unique differentiability of products to conclude
apply unique_diff_on.prod _ unique_diff_on_univ,
{ simp only [hq] with mfld_simps },
{ assume x hx,
have A : unique_mdiff_on I (e₀.target ∩ e₀.symm⁻¹' s),
{ apply hs.unique_mdiff_on_preimage,
exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)),
apply_instance },
simp only [unique_mdiff_on, unique_mdiff_within_at, preimage_inter] with mfld_simps at A,
have B := A (I.symm x) hx.1.1 hx.1.2,
rwa [← preimage_inter, model_with_corners.right_inv _ hx.2] at B }
end
lemma unique_mdiff_on.tangent_bundle_proj_preimage (hs : unique_mdiff_on I s):
unique_mdiff_on I.tangent (π (tangent_space I) ⁻¹' s) :=
hs.smooth_bundle_preimage _
end unique_mdiff
|
b19424a066120e437fa62b18c55bd40b750bc2c5 | 367134ba5a65885e863bdc4507601606690974c1 | /src/tactic/explode.lean | 09e5d821f8d8c071a3b8a9e3ed30cc1abe9dc7ec | [
"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 | 9,406 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro, Minchao Wu
-/
import tactic.core
/-!
# `#explode` command
Displays a proof term in a line by line format somewhat akin to a Fitch style
proof or the Metamath proof style.
-/
open expr tactic
namespace tactic
namespace explode
@[derive inhabited]
inductive status : Type | reg | intro | lam | sintro
/--
A type to distinguish introduction or elimination rules represented as
strings from theorems referred to by their names.
-/
meta inductive thm : Type
| expr (e : expr)
| name (n : name)
| string (s : string)
/--
Turn a thm into a string.
-/
meta def thm.to_string : thm → string
| (thm.expr e) := e.to_string
| (thm.name n) := n.to_string
| (thm.string s) := s
meta structure entry : Type :=
(expr : expr)
(line : nat)
(depth : nat)
(status : status)
(thm : thm)
(deps : list nat)
meta def pad_right (l : list string) : list string :=
let n := l.foldl (λ r (s:string), max r s.length) 0 in
l.map $ λ s, nat.iterate (λ s, s.push ' ') (n - s.length) s
@[derive inhabited]
meta structure entries : Type := mk' ::
(s : expr_map entry)
(l : list entry)
meta def entries.find (es : entries) (e : expr) : option entry := es.s.find e
meta def entries.size (es : entries) : ℕ := es.s.size
meta def entries.add : entries → entry → entries
| es@⟨s, l⟩ e := if s.contains e.expr then es else ⟨s.insert e.expr e, e :: l⟩
meta def entries.head (es : entries) : option entry := es.l.head'
meta def format_aux : list string → list string → list string → list entry → tactic format
| (line :: lines) (dep :: deps) (thm :: thms) (en :: es) := do
fmt ← do {
let margin := string.join (list.repeat " │" en.depth),
let margin := match en.status with
| status.sintro := " ├" ++ margin
| status.intro := " │" ++ margin ++ " ┌"
| status.reg := " │" ++ margin ++ ""
| status.lam := " │" ++ margin ++ ""
end,
p ← infer_type en.expr >>= pp,
let lhs := line ++ "│" ++ dep ++ "│ " ++ thm ++ margin ++ " ",
return $ format.of_string lhs ++ (p.nest lhs.length).group ++ format.line },
(++ fmt) <$> format_aux lines deps thms es
| _ _ _ _ := return format.nil
meta instance : has_to_tactic_format entries :=
⟨λ es : entries,
let lines := pad_right $ es.l.map (λ en, to_string en.line),
deps := pad_right $ es.l.map (λ en, string.intercalate "," (en.deps.map to_string)),
thms := pad_right $ es.l.map (λ en, (entry.thm en).to_string) in
format_aux lines deps thms es.l⟩
meta def append_dep (filter : expr → tactic unit)
(es : entries) (e : expr) (deps : list nat) : tactic (list nat) :=
do { ei ← es.find e,
filter ei.expr,
return (ei.line :: deps) }
<|> return deps
meta def may_be_proof (e : expr) : tactic bool :=
do expr.sort u ← infer_type e >>= infer_type,
return $ bnot u.nonzero
end explode
open explode
meta mutual def explode.core, explode.args (filter : expr → tactic unit)
with explode.core : expr → bool → nat → entries → tactic entries
| e@(lam n bi d b) si depth es := do
m ← mk_fresh_name,
let l := local_const m n bi d,
let b' := instantiate_var b l,
if si then
let en : entry := ⟨l, es.size, depth, status.sintro, thm.name n, []⟩ in do
es' ← explode.core b' si depth (es.add en),
return $ es'.add ⟨e, es'.size, depth, status.lam, thm.string "∀I", [es.size, es'.size - 1]⟩
else do
let en : entry := ⟨l, es.size, depth, status.intro, thm.name n, []⟩,
es' ← explode.core b' si (depth + 1) (es.add en),
-- in case of a "have" clause, the b' here has an annotation
deps' ← explode.append_dep filter es' b'.erase_annotations [],
deps' ← explode.append_dep filter es' l deps',
return $ es'.add ⟨e, es'.size, depth, status.lam, thm.string "∀I", deps'⟩
| e@(elet n t a b) si depth es := explode.core (reduce_lets e) si depth es
| e@(macro n l) si depth es := explode.core l.head si depth es
| e si depth es := filter e >>
match get_app_fn_args e with
| (nm@(const n _), args) :=
explode.args e args depth es (thm.expr nm) []
| (fn, []) := do
let en : entry := ⟨fn, es.size, depth, status.reg, thm.expr fn, []⟩,
return (es.add en)
| (fn, args) := do
es' ← explode.core fn ff depth es,
-- in case of a "have" clause, the fn here has an annotation
deps ← explode.append_dep filter es' fn.erase_annotations [],
explode.args e args depth es' (thm.string "∀E") deps
end
with explode.args : expr → list expr → nat → entries → thm → list nat → tactic entries
| e (arg :: args) depth es thm deps := do
es' ← explode.core arg ff depth es <|> return es,
deps' ← explode.append_dep filter es' arg deps,
explode.args e args depth es' thm deps'
| e [] depth es thm deps :=
return (es.add ⟨e, es.size, depth, status.reg, thm, deps.reverse⟩)
meta def explode_expr (e : expr) (hide_non_prop := tt) : tactic entries :=
let filter := if hide_non_prop then λ e, may_be_proof e >>= guardb else λ _, skip in
tactic.explode.core filter e tt 0 (default _)
meta def explode (n : name) : tactic unit :=
do const n _ ← resolve_name n | fail "cannot resolve name",
d ← get_decl n,
v ← match d with
| (declaration.defn _ _ _ v _ _) := return v
| (declaration.thm _ _ _ v) := return v.get
| _ := fail "not a definition"
end,
t ← pp d.type,
explode_expr v <* trace (to_fmt n ++ " : " ++ t) >>= trace
open interactive lean lean.parser interaction_monad.result
/--
`#explode decl_name` displays a proof term in a line-by-line format somewhat akin to a Fitch-style
proof or the Metamath proof style.
`#explode_widget decl_name` renders a widget that displays an `#explode` proof.
`#explode iff_true_intro` produces
```lean
iff_true_intro : ∀ {a : Prop}, a → (a ↔ true)
0│ │ a ├ Prop
1│ │ h ├ a
2│ │ hl │ ┌ a
3│ │ trivial │ │ true
4│2,3│ ∀I │ a → true
5│ │ hr │ ┌ true
6│5,1│ ∀I │ true → a
7│4,6│ iff.intro │ a ↔ true
8│1,7│ ∀I │ a → (a ↔ true)
9│0,8│ ∀I │ ∀ {a : Prop}, a → (a ↔ true)
```
In more detail:
The output of `#explode` is a Fitch-style proof in a four-column diagram modeled after Metamath
proof displays like [this](http://us.metamath.org/mpeuni/ru.html). The headers of the columns are
"Step", "Hyp", "Ref", "Type" (or "Expression" in the case of Metamath):
* Step: An increasing sequence of numbers to number each step in the proof, used in the Hyp field.
* Hyp: The direct children of the current step. Most theorems are implications like `A -> B -> C`,
and so on the step proving `C` the Hyp field will refer to the steps that prove `A` and `B`.
* Ref: The name of the theorem being applied. This is well-defined in Metamath, but in Lean there
are some special steps that may have long names because the structure of proof terms doesn't
exactly match this mold.
* If the theorem is `foo (x y : Z) : A x -> B y -> C x y`:
* the Ref field will contain `foo`,
* `x` and `y` will be suppressed, because term construction is not interesting, and
* the Hyp field will reference steps proving `A x` and `B y`. This corresponds to a proof term
like `@foo x y pA pB` where `pA` and `pB` are subproofs.
* If the head of the proof term is a local constant or lambda, then in this case the Ref will
say `∀E` for forall-elimination. This happens when you have for example `h : A -> B` and
`ha : A` and prove `b` by `h ha`; we reinterpret this as if it said `∀E h ha` where `∀E` is
(n-ary) modus ponens.
* If the proof term is a lambda, we will also use `∀I` for forall-introduction, referencing the
body of the lambda. The indentation level will increase, and a bracket will surround the proof
of the body of the lambda, starting at a proof step labeled with the name of the lambda variable
and its type, and ending with the `∀I` step. Metamath doesn't have steps like this, but the
style is based on Fitch proofs in first-order logic.
* Type: This contains the type of the proof term, the theorem being proven at the current step.
This proof layout differs from `#print` in using lots of intermediate step displays so that you
can follow along and don't have to see term construction steps because they are implicitly in the
intermediate step displays.
Also, it is common for a Lean theorem to begin with a sequence of lambdas introducing local
constants of the theorem. In order to minimize the indentation level, the `∀I` steps at the end of
the proof will be introduced in a group and the indentation will stay fixed. (The indentation
brackets are only needed in order to delimit the scope of assumptions, and these assumptions
have global scope anyway so detailed tracking is not necessary.)
-/
@[user_command]
meta def explode_cmd (_ : parse $ tk "#explode") : parser unit :=
do n ← ident,
explode n
.
add_tactic_doc
{ name := "#explode / #explode_widget",
category := doc_category.cmd,
decl_names := [`tactic.explode_cmd, `tactic.explode_widget_cmd],
inherit_description_from := `tactic.explode_cmd,
tags := ["proof display", "widgets"] }
end tactic
|
34b5f7a6a15a0908d3d2e035beb888a9eb32f4d7 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/data/rbtree/find.lean | 2b0817909acaae40945979ed264764e962ed3251 | [
"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 | 5,976 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import data.rbtree.basic
universe u
namespace rbnode
variables {α : Type u}
@[elab_simple]
lemma find.induction {p : rbnode α → Prop} (lt) [decidable_rel lt]
(t x)
(h₁ : p leaf)
(h₂ : ∀ l y r (h : cmp_using lt x y = ordering.lt) (ih : p l), p (red_node l y r))
(h₃ : ∀ l y r (h : cmp_using lt x y = ordering.eq), p (red_node l y r))
(h₄ : ∀ l y r (h : cmp_using lt x y = ordering.gt) (ih : p r), p (red_node l y r))
(h₅ : ∀ l y r (h : cmp_using lt x y = ordering.lt) (ih : p l), p (black_node l y r))
(h₆ : ∀ l y r (h : cmp_using lt x y = ordering.eq), p (black_node l y r))
(h₇ : ∀ l y r (h : cmp_using lt x y = ordering.gt) (ih : p r), p (black_node l y r))
: p t :=
begin
induction t,
case leaf { assumption },
case red_node : l y r {
cases h : cmp_using lt x y,
case ordering.lt { apply h₂, assumption, assumption },
case ordering.eq { apply h₃, assumption },
case ordering.gt { apply h₄, assumption, assumption },
},
case black_node : l y r {
cases h : cmp_using lt x y,
case ordering.lt { apply h₅, assumption, assumption },
case ordering.eq { apply h₆, assumption },
case ordering.gt { apply h₇, assumption, assumption },
}
end
lemma find_correct {t : rbnode α} {lt x} [decidable_rel lt] [is_strict_weak_order α lt] :
∀ {lo hi} (hs : is_searchable lt t lo hi), mem lt x t ↔ ∃ y, find lt t x = some y ∧ x ≈[lt] y :=
begin
apply find.induction lt t x; intros; simp only [mem, find, *],
{ simp },
iterate 2 { -- red and black cases are identical
{
cases hs,
apply iff.intro,
{
intro hm, blast_disjs,
{ exact iff.mp (ih hs_hs₁) hm },
{ simp at h, cases hm, contradiction },
{
have hyx : lift lt (some y) (some x) := (range hs_hs₂ hm).1,
simp [lift] at hyx,
have hxy : lt x y, { simp [cmp_using] at h, assumption },
exact absurd (trans_of lt hxy hyx) (irrefl_of lt x)
}
},
{ intro hc, left, exact iff.mpr (ih hs_hs₁) hc },
},
{ simp at h, simp [h, strict_weak_order.equiv] },
{
cases hs,
apply iff.intro,
{
intro hm, blast_disjs,
{
have hxy : lift lt (some x) (some y) := (range hs_hs₁ hm).2,
simp [lift] at hxy,
have hyx : lt y x, { simp [cmp_using] at h, exact h.2 },
exact absurd (trans_of lt hxy hyx) (irrefl_of lt x)
},
{ simp at h, cases hm, contradiction },
{ exact iff.mp (ih hs_hs₂) hm }
},
{ intro hc, right, right, exact iff.mpr (ih hs_hs₂) hc },
} }
end
lemma mem_of_mem_exact {lt} [is_irrefl α lt] {x t} : mem_exact x t → mem lt x t :=
begin
induction t; simp [mem_exact, mem, false_implies_iff]; intro h,
all_goals { blast_disjs, simp [t_ih_lchild h], simp [h, irrefl_of lt t_val],
simp [t_ih_rchild h] }
end
lemma find_correct_exact {t : rbnode α} {lt x} [decidable_rel lt] [is_strict_weak_order α lt] :
∀ {lo hi} (hs : is_searchable lt t lo hi), mem_exact x t ↔ find lt t x = some x :=
begin
apply find.induction lt t x; intros; simp only [mem_exact, find, *],
iterate 2 {
{
cases hs,
apply iff.intro,
{
intro hm, blast_disjs,
{ exact iff.mp (ih hs_hs₁) hm },
{ simp at h, subst x, exact absurd h (irrefl y) },
{ have hyx : lift lt (some y) (some x) := (range hs_hs₂ (mem_of_mem_exact hm)).1,
simp [lift] at hyx,
have hxy : lt x y, { simp [cmp_using] at h, assumption },
exact absurd (trans_of lt hxy hyx) (irrefl_of lt x)
}
},
{ intro hc, left, exact iff.mpr (ih hs_hs₁) hc },
},
{ simp at h,
cases hs,
apply iff.intro,
{
intro hm, blast_disjs,
{ have hxy : lift lt (some x) (some y) := (range hs_hs₁ (mem_of_mem_exact hm)).2,
simp [lift] at hxy,
exact absurd hxy h.1 },
{ subst hm },
{ have hyx : lift lt (some y) (some x) := (range hs_hs₂ (mem_of_mem_exact hm)).1,
simp [lift] at hyx,
exact absurd hyx h.2 } },
{ intro hm, simp [*] } },
{
cases hs,
apply iff.intro,
{
intro hm, blast_disjs,
{
have hxy : lift lt (some x) (some y) := (range hs_hs₁ (mem_of_mem_exact hm)).2,
simp [lift] at hxy,
have hyx : lt y x, { simp [cmp_using] at h, exact h.2 },
exact absurd (trans_of lt hxy hyx) (irrefl_of lt x)
},
{ simp at h, subst x, exact absurd h (irrefl y) },
{ exact iff.mp (ih hs_hs₂) hm }
},
{ intro hc, right, right, exact iff.mpr (ih hs_hs₂) hc } } }
end
lemma eqv_of_find_some {t : rbnode α} {lt x y} [decidable_rel lt] :
∀ {lo hi} (hs : is_searchable lt t lo hi) (he : find lt t x = some y), x ≈[lt] y :=
begin
apply find.induction lt t x; intros; simp only [mem, find, *] at *,
iterate 2 {
{ cases hs, exact ih hs_hs₁ rfl },
{ subst y, simp at h, exact h },
{ cases hs, exact ih hs_hs₂ rfl } }
end
lemma find_eq_find_of_eqv {lt a b} [decidable_rel lt] [is_strict_weak_order α lt] {t : rbnode α} :
∀ {lo hi} (hs : is_searchable lt t lo hi) (heqv : a ≈[lt] b), find lt t a = find lt t b :=
begin
apply find.induction lt t a; intros;
simp [mem, find, strict_weak_order.equiv, *, true_implies_iff] at *,
iterate 2 {
{ have : lt b y := lt_of_incomp_of_lt heqv.swap h,
simp [cmp_using, find, *], cases hs, apply ih hs_hs₁ },
{ have := incomp_trans_of lt heqv.swap h, simp [cmp_using, find, *] },
{ have := lt_of_lt_of_incomp h heqv,
have := not_lt_of_lt this,
simp [cmp_using, find, *], cases hs, apply ih hs_hs₂ } }
end
end rbnode
|
8092bf53499be5f72067da1c36fb7a2bead752c0 | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /stage0/src/Init/Lean/Meta/Tactic/Target.lean | 14448439a5108f4efe0a92378a626c9dcda2b7b3 | [
"Apache-2.0"
] | permissive | mhuisi/lean4 | 28d35a4febc2e251c7f05492e13f3b05d6f9b7af | dda44bc47f3e5d024508060dac2bcb59fd12e4c0 | refs/heads/master | 1,621,225,489,283 | 1,585,142,689,000 | 1,585,142,689,000 | 250,590,438 | 0 | 2 | Apache-2.0 | 1,602,443,220,000 | 1,585,327,814,000 | C | UTF-8 | Lean | false | false | 1,666 | 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.Lean.Meta.AppBuilder
import Init.Lean.Meta.Tactic.Util
namespace Lean
namespace Meta
/--
Convert the given goal `Ctx |- target` into `Ctx |- newTarget` using an equality proof `eqProof : target = newTarget`.
It assumes `eqProof` has type `target = newTarget` -/
def replaceTargetEq (mvarId : MVarId) (newTarget : Expr) (eqProof : Expr) : MetaM MVarId := do
withMVarContext mvarId $ do
checkNotAssigned mvarId `replaceTarget;
tag ← getMVarTag mvarId;
newMVar ← mkFreshExprSyntheticOpaqueMVar newTarget tag;
target ← getMVarType mvarId;
u ← getLevel target;
eq ← mkEq target newTarget;
newProof ← mkExpectedTypeHint eqProof eq;
let newVal := mkAppN (Lean.mkConst `Eq.mpr [u]) #[target, newTarget, eqProof, newMVar];
assignExprMVar mvarId newMVar;
pure newMVar.mvarId!
/--
Convert the given goal `Ctx | target` into `Ctx |- newTarget`. It assumes the goals are definitionally equal.
We use the proof term
```
@id target newMVar
```
to create a checkpoint. -/
def replaceTargetDefEq (mvarId : MVarId) (newTarget : Expr) : MetaM MVarId :=
withMVarContext mvarId $ do
checkNotAssigned mvarId `change;
target ← getMVarType mvarId;
if target == newTarget then pure mvarId
else do
tag ← getMVarTag mvarId;
newMVar ← mkFreshExprSyntheticOpaqueMVar newTarget tag;
newVal ← mkExpectedTypeHint newMVar target;
assignExprMVar mvarId newMVar;
pure newMVar.mvarId!
end Meta
end Lean
|
c7efc33432129acc88df3b059bbdc63933d3ff97 | ea5678cc400c34ff95b661fa26d15024e27ea8cd | /even_odd.lean | bb72505fa01e1434d2bebf5819f5ef0b0319be75 | [] | no_license | ChrisHughes24/leanstuff | dca0b5349c3ed893e8792ffbd98cbcadaff20411 | 9efa85f72efaccd1d540385952a6acc18fce8687 | refs/heads/master | 1,654,883,241,759 | 1,652,873,885,000 | 1,652,873,885,000 | 134,599,537 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 119 | lean |
def even : nat → Prop :=
λ n, ∃ m, m * 2 = n
def odd : nat → Prop :=
λ n, ∃ m, nat.succ (m * 2) = n
lemma |
1c9e1ac6bf9c7521047d913690567de8a3811310 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/analysis/normed_space/real_inner_product.lean | 14f40c7943f74f32994ac1dc36daf5eafd7fcd7b | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 21,765 | lean | /-
Copyright (c) 2019 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou
-/
import analysis.convex algebra.quadratic_discriminant analysis.complex.exponential
analysis.specific_limits
import tactic.monotonicity
/-!
# Inner Product Space
This file defines real inner product space and proves its basic properties.
An inner product space is a vector space endowed with an inner product. It generalizes the notion of
dot product in `ℝ^n` and provides the means of defining the length of a vector and the angle between
two vectors. In particular vectors `x` and `y` are orthogonal if their inner product equals zero.
## Main statements
Existence of orthogonal projection onto nonempty complete subspace:
Let `u` be a point in an inner product space, and let `K` be a nonempty complete subspace.
Then there exists a unique `v` in `K` that minimizes the distance `∥u - v∥` to `u`.
The point `v` is usually called the orthogonal projection of `u` onto `K`.
## Implementation notes
We decide to develop the theory of real inner product spaces and that of complex inner product
spaces separately.
## Tags
inner product space, norm, orthogonal projection
## References
* [Clément & Martin, *The Lax-Milgram Theorem. A detailed proof to be formalized in Coq*]
* [Clément & Martin, *A Coq formal proof of the Lax–Milgram theorem*]
The Coq code is available at the following address: <http://www.lri.fr/~sboldo/elfic/index.html>
-/
noncomputable theory
open real set lattice
open_locale topological_space
universes u v w
variables {α : Type u} {F : Type v} {G : Type w}
set_option class.instance_max_depth 40
class has_inner (α : Type*) := (inner : α → α → ℝ)
export has_inner (inner)
section prio
set_option default_priority 100 -- see Note [default priority]
-- see Note[vector space definition] for why we extend `module`.
/--
An inner product space is a real vector space with an additional operation called inner product.
Inner product spaces over complex vector space will be defined in another file.
-/
class inner_product_space (α : Type*) extends add_comm_group α, module ℝ α, has_inner α :=
(comm : ∀ x y, inner x y = inner y x)
(nonneg : ∀ x, 0 ≤ inner x x)
(definite : ∀ x, inner x x = 0 → x = 0)
(add_left : ∀ x y z, inner (x + y) z = inner x z + inner y z)
(smul_left : ∀ x y r, inner (r • x) y = r * inner x y)
end prio
variable [inner_product_space α]
section basic_properties
lemma inner_comm (x y : α) : inner x y = inner y x := inner_product_space.comm x y
lemma inner_self_nonneg {x : α} : 0 ≤ inner x x := inner_product_space.nonneg _
lemma inner_add_left {x y z : α} : inner (x + y) z = inner x z + inner y z :=
inner_product_space.add_left _ _ _
lemma inner_add_right {x y z : α} : inner x (y + z) = inner x y + inner x z :=
by { rw [inner_comm, inner_add_left], simp [inner_comm] }
lemma inner_smul_left {x y : α} {r : ℝ} : inner (r • x) y = r * inner x y :=
inner_product_space.smul_left _ _ _
lemma inner_smul_right {x y : α} {r : ℝ} : inner x (r • y) = r * inner x y :=
by { rw [inner_comm, inner_smul_left, inner_comm] }
@[simp] lemma inner_zero_left {x : α} : inner 0 x = 0 :=
by { rw [← zero_smul ℝ (0:α), inner_smul_left, zero_mul] }
@[simp] lemma inner_zero_right {x : α} : inner x 0 = 0 :=
by { rw [inner_comm, inner_zero_left] }
lemma inner_self_eq_zero (x : α) : inner x x = 0 ↔ x = 0 :=
iff.intro (inner_product_space.definite _) (by { rintro rfl, exact inner_zero_left })
@[simp] lemma inner_neg_left {x y : α} : inner (-x) y = -inner x y :=
by { rw [← neg_one_smul ℝ x, inner_smul_left], simp }
@[simp] lemma inner_neg_right {x y : α} : inner x (-y) = -inner x y :=
by { rw [inner_comm, inner_neg_left, inner_comm] }
@[simp] lemma inner_neg_neg {x y : α} : inner (-x) (-y) = inner x y := by simp
lemma inner_sub_left {x y z : α} : inner (x - y) z = inner x z - inner y z :=
by { simp [sub_eq_add_neg, inner_add_left] }
lemma inner_sub_right {x y z : α} : inner x (y - z) = inner x y - inner x z :=
by { simp [sub_eq_add_neg, inner_add_right] }
/-- Expand `inner (x + y) (x + y)` -/
lemma inner_add_add_self {x y : α} : inner (x + y) (x + y) = inner x x + 2 * inner x y + inner y y :=
by { simpa [inner_add_left, inner_add_right, two_mul] using inner_comm _ _ }
/-- Expand `inner (x - y) (x - y)` -/
lemma inner_sub_sub_self {x y : α} : inner (x - y) (x - y) = inner x x - 2 * inner x y + inner y y :=
by { simp only [inner_sub_left, inner_sub_right, two_mul], simpa using inner_comm _ _ }
/-- Parallelogram law -/
lemma parallelogram_law {x y : α} :
inner (x + y) (x + y) + inner (x - y) (x - y) = 2 * (inner x x + inner y y) :=
by { simp [inner_add_add_self, inner_sub_sub_self, two_mul] }
/-- Cauchy–Schwarz inequality -/
lemma inner_mul_inner_self_le (x y : α) : inner x y * inner x y ≤ inner x x * inner y y :=
begin
have : ∀ t, 0 ≤ inner y y * t * t + 2 * inner x y * t + inner x x, from
assume t, calc
0 ≤ inner (x+t•y) (x+t•y) : inner_self_nonneg
... = inner y y * t * t + 2 * inner x y * t + inner x x :
by { simp only [inner_add_add_self, inner_smul_right, inner_smul_left], ring },
have := discriminant_le_zero this, rw discrim at this,
have h : (2 * inner x y)^2 - 4 * inner y y * inner x x =
4 * (inner x y * inner x y - inner x x * inner y y) := by ring,
rw h at this,
linarith
end
end basic_properties
section norm
/-- An inner product naturally induces a norm. -/
@[priority 100] -- see Note [lower instance priority]
instance inner_product_space_has_norm : has_norm α := ⟨λx, sqrt (inner x x)⟩
lemma norm_eq_sqrt_inner {x : α} : ∥x∥ = sqrt (inner x x) := rfl
lemma inner_self_eq_norm_square (x : α) : inner x x = ∥x∥ * ∥x∥ := (mul_self_sqrt inner_self_nonneg).symm
/-- Expand the square -/
lemma norm_add_pow_two {x y : α} : ∥x + y∥^2 = ∥x∥^2 + 2 * inner x y + ∥y∥^2 :=
by { repeat {rw [pow_two, ← inner_self_eq_norm_square]}, exact inner_add_add_self }
/-- Same lemma as above but in a different form -/
lemma norm_add_mul_self {x y : α} : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + 2 * inner x y + ∥y∥ * ∥y∥ :=
by { repeat {rw [← pow_two]}, exact norm_add_pow_two }
/-- Expand the square -/
lemma norm_sub_pow_two {x y : α} : ∥x - y∥^2 = ∥x∥^2 - 2 * inner x y + ∥y∥^2 :=
by { repeat {rw [pow_two, ← inner_self_eq_norm_square]}, exact inner_sub_sub_self }
/-- Same lemma as above but in a different form -/
lemma norm_sub_mul_self {x y : α} : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ - 2 * inner x y + ∥y∥ * ∥y∥ :=
by { repeat {rw [← pow_two]}, exact norm_sub_pow_two }
/-- Cauchy–Schwarz inequality with norm -/
lemma abs_inner_le_norm (x y : α) : abs (inner x y) ≤ ∥x∥ * ∥y∥ :=
nonneg_le_nonneg_of_squares_le (mul_nonneg (sqrt_nonneg _) (sqrt_nonneg _))
begin
rw abs_mul_abs_self,
have : ∥x∥ * ∥y∥ * (∥x∥ * ∥y∥) = inner x x * inner y y,
simp only [inner_self_eq_norm_square], ring,
rw this,
exact inner_mul_inner_self_le _ _
end
lemma parallelogram_law_with_norm {x y : α} :
∥x + y∥ * ∥x + y∥ + ∥x - y∥ * ∥x - y∥ = 2 * (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥) :=
by { simp only [(inner_self_eq_norm_square _).symm], exact parallelogram_law }
/-- An inner product space forms a normed group w.r.t. its associated norm. -/
@[priority 100] -- see Note [lower instance priority]
instance inner_product_space_is_normed_group : normed_group α :=
normed_group.of_core α
{ norm_eq_zero_iff := assume x, iff.intro
(λ h : sqrt (inner x x) = 0, (inner_self_eq_zero x).1 $ (sqrt_eq_zero inner_self_nonneg).1 h )
(by {rintro rfl, show sqrt (inner (0:α) 0) = 0, simp }),
triangle := assume x y,
begin
have := calc
∥x + y∥ * ∥x + y∥ = inner (x + y) (x + y) : (inner_self_eq_norm_square _).symm
... = inner x x + 2 * inner x y + inner y y : inner_add_add_self
... ≤ inner x x + 2 * (∥x∥ * ∥y∥) + inner y y :
by linarith [abs_inner_le_norm x y, le_abs_self (inner x y)]
... = (∥x∥ + ∥y∥) * (∥x∥ + ∥y∥) : by { simp only [inner_self_eq_norm_square], ring },
exact nonneg_le_nonneg_of_squares_le (add_nonneg (sqrt_nonneg _) (sqrt_nonneg _)) this
end,
norm_neg := λx, show sqrt (inner (-x) (-x)) = sqrt (inner x x), by simp }
/-- An inner product space forms a normed space over reals w.r.t. its associated norm. -/
instance inner_product_space_is_normed_space : normed_space ℝ α :=
{ norm_smul := assume r x,
begin
rw [norm_eq_sqrt_inner, sqrt_eq_iff_mul_self_eq,
inner_smul_left, inner_smul_right, inner_self_eq_norm_square],
exact calc
abs(r) * ∥x∥ * (abs(r) * ∥x∥) = (abs(r) * abs(r)) * (∥x∥ * ∥x∥) : by ring
... = r * (r * (∥x∥ * ∥x∥)) : by { rw abs_mul_abs_self, ring },
exact inner_self_nonneg,
exact mul_nonneg (abs_nonneg _) (sqrt_nonneg _)
end }
end norm
section orthogonal
open filter
/--
Existence of minimizers
Let `u` be a point in an inner product space, and let `K` be a nonempty complete convex subset.
Then there exists a unique `v` in `K` that minimizes the distance `∥u - v∥` to `u`.
-/
theorem exists_norm_eq_infi_of_complete_convex {K : set α} (ne : nonempty K) (h₁ : is_complete K)
(h₂ : convex K) : ∀ u : α, ∃ v ∈ K, ∥u - v∥ = ⨅ w : K, ∥u - w∥ := assume u,
begin
let δ := ⨅ w : K, ∥u - w∥,
have zero_le_δ : 0 ≤ δ,
apply le_cinfi, intro, exact norm_nonneg _,
have δ_le : ∀ w : K, δ ≤ ∥u - w∥,
assume w, apply cinfi_le, use (0:ℝ), rintros _ ⟨_, rfl⟩, exact norm_nonneg _,
have δ_le' : ∀ w ∈ K, δ ≤ ∥u - w∥ := assume w hw, δ_le ⟨w, hw⟩,
-- Step 1: since `δ` is the infimum, can find a sequence `w : ℕ → K` in `K`
-- such that `∥u - w n∥ < δ + 1 / (n + 1)` (which implies `∥u - w n∥ --> δ`);
-- maybe this should be a separate lemma
have exists_seq : ∃ w : ℕ → K, ∀ n, ∥u - w n∥ < δ + 1 / (n + 1),
have hδ : ∀n:ℕ, δ < δ + 1 / (n + 1), from
λ n, lt_add_of_le_of_pos (le_refl _) nat.one_div_pos_of_nat,
have h := λ n, exists_lt_of_cinfi_lt ne (hδ n),
let w : ℕ → K := λ n, classical.some (h n),
exact ⟨w, λ n, classical.some_spec (h n)⟩,
rcases exists_seq with ⟨w, hw⟩,
have norm_tendsto : tendsto (λ n, ∥u - w n∥) at_top (𝓝 δ),
have h : tendsto (λ n:ℕ, δ) at_top (𝓝 δ),
exact tendsto_const_nhds,
have h' : tendsto (λ n:ℕ, δ + 1 / (n + 1)) at_top (𝓝 δ),
convert h.add tendsto_one_div_add_at_top_nhds_0_nat, simp only [add_zero],
exact tendsto_of_tendsto_of_tendsto_of_le_of_le h h'
(by { rw mem_at_top_sets, use 0, assume n hn, exact δ_le _ })
(by { rw mem_at_top_sets, use 0, assume n hn, exact le_of_lt (hw _) }),
-- Step 2: Prove that the sequence `w : ℕ → K` is a Cauchy sequence
have seq_is_cauchy : cauchy_seq (λ n, ((w n):α)),
rw cauchy_seq_iff_le_tendsto_0, -- splits into three goals
let b := λ n:ℕ, (8 * δ * (1/(n+1)) + 4 * (1/(n+1)) * (1/(n+1))),
use (λn, sqrt (b n)),
split,
-- first goal : `∀ (n : ℕ), 0 ≤ sqrt (b n)`
assume n, exact sqrt_nonneg _,
split,
-- second goal : `∀ (n m N : ℕ), N ≤ n → N ≤ m → dist ↑(w n) ↑(w m) ≤ sqrt (b N)`
assume p q N hp hq,
let wp := ((w p):α), let wq := ((w q):α),
let a := u - wq, let b := u - wp,
let half := 1 / (2:ℝ), let div := 1 / ((N:ℝ) + 1),
have : 4 * ∥u - half • (wq + wp)∥ * ∥u - half • (wq + wp)∥ + ∥wp - wq∥ * ∥wp - wq∥ =
2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) :=
calc
4 * ∥u - half•(wq + wp)∥ * ∥u - half•(wq + wp)∥ + ∥wp - wq∥ * ∥wp - wq∥
= (2*∥u - half•(wq + wp)∥) * (2 * ∥u - half•(wq + wp)∥) + ∥wp-wq∥*∥wp-wq∥ : by ring
... = (abs((2:ℝ)) * ∥u - half•(wq + wp)∥) * (abs((2:ℝ)) * ∥u - half•(wq+wp)∥) + ∥wp-wq∥*∥wp-wq∥ :
by { rw abs_of_nonneg, exact add_nonneg zero_le_one zero_le_one }
... = ∥(2:ℝ) • (u - half • (wq + wp))∥ * ∥(2:ℝ) • (u - half • (wq + wp))∥ + ∥wp-wq∥ * ∥wp-wq∥ :
by { rw [norm_smul], refl }
... = ∥a + b∥ * ∥a + b∥ + ∥a - b∥ * ∥a - b∥ :
begin
rw [smul_sub, smul_smul, mul_one_div_cancel two_ne_zero, ← one_add_one_eq_two, add_smul],
simp only [one_smul],
have eq₁ : wp - wq = a - b, show wp - wq = (u - wq) - (u - wp), abel,
have eq₂ : u + u - (wq + wp) = a + b, show u + u - (wq + wp) = (u - wq) + (u - wp), abel,
rw [eq₁, eq₂],
end
... = 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) : parallelogram_law_with_norm,
have eq : δ ≤ ∥u - half • (wq + wp)∥,
rw smul_add,
apply δ_le', apply h₂,
repeat {exact subtype.mem _},
repeat {exact le_of_lt one_half_pos},
exact add_halves 1,
have eq₁ : 4 * δ * δ ≤ 4 * ∥u - half • (wq + wp)∥ * ∥u - half • (wq + wp)∥,
mono, mono, norm_num, apply mul_nonneg, norm_num, exact norm_nonneg _,
have eq₂ : ∥a∥ * ∥a∥ ≤ (δ + div) * (δ + div) :=
mul_self_le_mul_self (norm_nonneg _)
(le_trans (le_of_lt $ hw q) (add_le_add_left (nat.one_div_le_one_div hq) _)),
have eq₂' : ∥b∥ * ∥b∥ ≤ (δ + div) * (δ + div) :=
mul_self_le_mul_self (norm_nonneg _)
(le_trans (le_of_lt $ hw p) (add_le_add_left (nat.one_div_le_one_div hp) _)),
rw dist_eq_norm,
apply nonneg_le_nonneg_of_squares_le, { exact sqrt_nonneg _ },
rw mul_self_sqrt,
exact calc
∥wp - wq∥ * ∥wp - wq∥ = 2 * (∥a∥*∥a∥ + ∥b∥*∥b∥) - 4 * ∥u - half • (wq+wp)∥ * ∥u - half • (wq+wp)∥ :
by { rw ← this, simp }
... ≤ 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) - 4 * δ * δ : sub_le_sub_left eq₁ _
... ≤ 2 * ((δ + div) * (δ + div) + (δ + div) * (δ + div)) - 4 * δ * δ :
sub_le_sub_right (mul_le_mul_of_nonneg_left (add_le_add eq₂ eq₂') (by norm_num)) _
... = 8 * δ * div + 4 * div * div : by ring,
exact add_nonneg (mul_nonneg (mul_nonneg (by norm_num) zero_le_δ) (le_of_lt nat.one_div_pos_of_nat))
(mul_nonneg (mul_nonneg (by norm_num) (le_of_lt nat.one_div_pos_of_nat)) (le_of_lt nat.one_div_pos_of_nat)),
-- third goal : `tendsto (λ (n : ℕ), sqrt (b n)) at_top (𝓝 0)`
apply tendsto.comp,
{ convert continuous_sqrt.continuous_at, exact sqrt_zero.symm },
have eq₁ : tendsto (λ (n : ℕ), 8 * δ * (1 / (n + 1))) at_top (𝓝 (0:ℝ)),
convert (@tendsto_const_nhds _ _ _ (8 * δ) _).mul tendsto_one_div_add_at_top_nhds_0_nat,
simp only [mul_zero],
have : tendsto (λ (n : ℕ), (4:ℝ) * (1 / (n + 1))) at_top (𝓝 (0:ℝ)),
convert (@tendsto_const_nhds _ _ _ (4:ℝ) _).mul tendsto_one_div_add_at_top_nhds_0_nat,
simp only [mul_zero],
have eq₂ : tendsto (λ (n : ℕ), (4:ℝ) * (1 / (n + 1)) * (1 / (n + 1))) at_top (𝓝 (0:ℝ)),
convert this.mul tendsto_one_div_add_at_top_nhds_0_nat,
simp only [mul_zero],
convert eq₁.add eq₂, simp only [add_zero],
-- Step 3: By completeness of `K`, let `w : ℕ → K` converge to some `v : K`.
-- Prove that it satisfies all requirements.
rcases cauchy_seq_tendsto_of_is_complete h₁ (λ n, _) seq_is_cauchy with ⟨v, hv, w_tendsto⟩,
use v, use hv,
have h_cont : continuous (λ v, ∥u - v∥) :=
continuous.comp continuous_norm (continuous.sub continuous_const continuous_id),
have : tendsto (λ n, ∥u - w n∥) at_top (𝓝 ∥u - v∥),
convert (tendsto.comp h_cont.continuous_at w_tendsto),
exact tendsto_nhds_unique at_top_ne_bot this norm_tendsto,
exact subtype.mem _
end
/-- Characterization of minimizers in the above theorem -/
theorem norm_eq_infi_iff_inner_le_zero {K : set α} (ne : nonempty K) (h : convex K) {u : α} {v : α}
(hv : v ∈ K) : ∥u - v∥ = (⨅ w : K, ∥u - w∥) ↔ ∀ w ∈ K, inner (u - v) (w - v) ≤ 0 :=
iff.intro
begin
assume eq w hw,
let δ := ⨅ w : K, ∥u - w∥, let p := inner (u - v) (w - v), let q := ∥w - v∥^2,
have zero_le_δ : 0 ≤ δ,
apply le_cinfi, intro, exact norm_nonneg _,
have δ_le : ∀ w : K, δ ≤ ∥u - w∥,
assume w, apply cinfi_le, use (0:ℝ), rintros _ ⟨_, rfl⟩, exact norm_nonneg _,
have δ_le' : ∀ w ∈ K, δ ≤ ∥u - w∥ := assume w hw, δ_le ⟨w, hw⟩,
have : ∀θ:ℝ, 0 < θ → θ ≤ 1 → 2 * p ≤ θ * q,
assume θ hθ₁ hθ₂,
have : ∥u - v∥^2 ≤ ∥u - v∥^2 - 2 * θ * inner (u - v) (w - v) + θ*θ*∥w - v∥^2 :=
calc
∥u - v∥^2 ≤ ∥u - (θ•w + (1-θ)•v)∥^2 :
begin
simp only [pow_two], apply mul_self_le_mul_self (norm_nonneg _),
rw eq, apply δ_le',
apply convex_iff.1 h hw hv,
repeat { exact subtype.mem _ },
exact ⟨le_of_lt hθ₁, hθ₂⟩,
end
... = ∥(u - v) - θ • (w - v)∥^2 :
begin
have : u - (θ•w + (1-θ)•v) = (u - v) - θ • (w - v),
rw [smul_sub, sub_smul, one_smul], simp,
rw this
end
... = ∥u - v∥^2 - 2 * θ * inner (u - v) (w - v) + θ*θ*∥w - v∥^2 :
begin
rw [norm_sub_pow_two, inner_smul_right, norm_smul],
simp only [pow_two],
show ∥u-v∥*∥u-v∥-2*(θ*inner(u-v)(w-v))+abs(θ)*∥w-v∥*(abs(θ)*∥w-v∥)=
∥u-v∥*∥u-v∥-2*θ*inner(u-v)(w-v)+θ*θ*(∥w-v∥*∥w-v∥),
rw abs_of_pos hθ₁, ring
end,
have eq₁ : ∥u-v∥^2-2*θ*inner(u-v)(w-v)+θ*θ*∥w-v∥^2=∥u-v∥^2+(θ*θ*∥w-v∥^2-2*θ*inner(u-v)(w-v)), abel,
rw [eq₁, le_add_iff_nonneg_right] at this,
have eq₂ : θ*θ*∥w-v∥^2-2*θ*inner(u-v)(w-v)=θ*(θ*∥w-v∥^2-2*inner(u-v)(w-v)), ring,
rw eq₂ at this,
have := le_of_sub_nonneg (nonneg_of_mul_nonneg_left this hθ₁),
exact this,
by_cases hq : q = 0,
{ rw hq at this,
have : p ≤ 0,
have := this (1:ℝ) (by norm_num) (by norm_num),
linarith,
exact this },
{ have q_pos : 0 < q,
apply lt_of_le_of_ne, exact pow_two_nonneg _, intro h, exact hq h.symm,
by_contradiction hp, rw not_le at hp,
let θ := min (1:ℝ) (p / q),
have eq₁ : θ*q ≤ p := calc
θ*q ≤ (p/q) * q : mul_le_mul_of_nonneg_right (min_le_right _ _) (pow_two_nonneg _)
... = p : div_mul_cancel _ hq,
have : 2 * p ≤ p := calc
2 * p ≤ θ*q : by { refine this θ (lt_min (by norm_num) (div_pos hp q_pos)) (by norm_num) }
... ≤ p : eq₁,
linarith }
end
begin
assume h,
apply le_antisymm,
{ apply le_cinfi, assume w,
apply nonneg_le_nonneg_of_squares_le (norm_nonneg _),
have := h w w.2,
exact calc
∥u - v∥ * ∥u - v∥ ≤ ∥u - v∥ * ∥u - v∥ - 2 * inner (u - v) ((w:α) - v) : by linarith
... ≤ ∥u - v∥^2 - 2 * inner (u - v) ((w:α) - v) + ∥(w:α) - v∥^2 :
by { rw pow_two, refine le_add_of_nonneg_right _, exact pow_two_nonneg _ }
... = ∥(u - v) - (w - v)∥^2 : norm_sub_pow_two.symm
... = ∥u - w∥ * ∥u - w∥ :
by { have : (u - v) - (w - v) = u - w, abel, rw [this, pow_two] } },
{ show (⨅ (w : K), ∥u - w∥) ≤ (λw:K, ∥u - w∥) ⟨v, hv⟩,
apply cinfi_le, use 0, rintros y ⟨z, rfl⟩, exact norm_nonneg _ }
end
/--
Existence of minimizers.
Let `u` be a point in an inner product space, and let `K` be a nonempty complete subspace.
Then there exists a unique `v` in `K` that minimizes the distance `∥u - v∥` to `u`.
This point `v` is usually called the orthogonal projection of `u` onto `K`.
-/
theorem exists_norm_eq_infi_of_complete_subspace (K : subspace ℝ α) (ne : nonempty K)
(h : is_complete (↑K : set α)) : ∀ u : α, ∃ v ∈ K, ∥u - v∥ = ⨅ w : (↑K : set α), ∥u - w∥ :=
exists_norm_eq_infi_of_complete_convex ne h K.convex
/--
Characterization of minimizers in the above theorem.
Let `u` be a point in an inner product space, and let `K` be a nonempty subspace.
Then point `v` minimizes the distance `∥u - v∥` if and only if
for all `w ∈ K`, `inner (u - v) w = 0` (i.e., `u - v` is orthogonal to the subspace `K`)
-/
theorem norm_eq_infi_iff_inner_eq_zero (K : subspace ℝ α) (ne : nonempty K) {u : α} {v : α}
(hv : v ∈ K) : ∥u - v∥ = (⨅ w : (↑K : set α), ∥u - w∥) ↔ ∀ w ∈ K, inner (u - v) w = 0 :=
iff.intro
begin
assume h,
have h : ∀ w ∈ K, inner (u - v) (w - v) ≤ 0,
{ rwa [norm_eq_infi_iff_inner_le_zero] at h, exacts [ne, K.convex, hv] },
assume w hw,
have le : inner (u - v) w ≤ 0,
let w' := w + v,
have : w' ∈ K := submodule.add_mem _ hw hv,
have h₁ := h w' this,
have h₂ : w' - v = w, simp only [add_neg_cancel_right, sub_eq_add_neg],
rw h₂ at h₁, exact h₁,
have ge : inner (u - v) w ≥ 0,
let w'' := -w + v,
have : w'' ∈ K := submodule.add_mem _ (submodule.neg_mem _ hw) hv,
have h₁ := h w'' this,
have h₂ : w'' - v = -w, simp only [neg_inj', add_neg_cancel_right, sub_eq_add_neg],
rw [h₂, inner_neg_right] at h₁,
linarith,
exact le_antisymm le ge
end
begin
assume h,
have : ∀ w ∈ K, inner (u - v) (w - v) ≤ 0,
assume w hw,
let w' := w - v,
have : w' ∈ K := submodule.sub_mem _ hw hv,
have h₁ := h w' this,
exact le_of_eq h₁,
rwa norm_eq_infi_iff_inner_le_zero,
exact ne, exact submodule.convex _, exact hv
end
end orthogonal
|
5697bb721f09f6f53863f9e8985680fe701c87d6 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/updateLevelIssues.lean | 0149b1699d0a61ac3483aa0a321139a3804431a6 | [
"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 | 609 | lean | import Lean
open Lean
@[noinline] def noinline (a : α) := a
#eval
let b := levelZero
let a1 := mkLevelParam `a
let a2 := mkLevelParam (noinline `a)
let l := mkLevelMax a1 b
(l.updateMax! a1 b).isMax == (l.updateMax! a2 b).isMax
#eval
let b := levelZero
let a1 := mkLevelParam `a
let l := mkLevelMax a1 b
assert! (l.updateMax! a1 b) == a1
toString (l.updateMax! a1 b)
#eval
let b := mkLevelParam `b
let a1 := mkLevelParam `a
let l := mkLevelMax a1 b
assert! (l.updateMax! a1 b) == l
assert! ptrAddrUnsafe (l.updateMax! a1 b) == ptrAddrUnsafe l
toString (l.updateMax! a1 b)
|
2ec8902e40e7e9e157b228d282cd7e6a0e73f92b | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/algebra/group/defs.lean | 3c2ff09f705d69cdd7321e5e21dad56f477a16e4 | [
"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 | 26,316 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura, Simon Hudon, Mario Carneiro
-/
import algebra.group.to_additive
import tactic.basic
/-!
# Typeclasses for (semi)groups and monoid
In this file we define typeclasses for algebraic structures with one binary operation.
The classes are named `(add_)?(comm_)?(semigroup|monoid|group)`, where `add_` means that
the class uses additive notation and `comm_` means that the class assumes that the binary
operation is commutative.
The file does not contain any lemmas except for
* axioms of typeclasses restated in the root namespace;
* lemmas required for instances.
For basic lemmas about these classes see `algebra.group.basic`.
-/
set_option old_structure_cmd true
universe u
/- Additive "sister" structures.
Example, add_semigroup mirrors semigroup.
These structures exist just to help automation.
In an alternative design, we could have the binary operation as an
extra argument for semigroup, monoid, group, etc. However, the lemmas
would be hard to index since they would not contain any constant.
For example, mul_assoc would be
lemma mul_assoc {α : Type u} {op : α → α → α} [semigroup α op] :
∀ a b c : α, op (op a b) c = op a (op b c) :=
semigroup.mul_assoc
The simplifier cannot effectively use this lemma since the pattern for
the left-hand-side would be
?op (?op ?a ?b) ?c
Remark: we use a tactic for transporting theorems from the multiplicative fragment
to the additive one.
-/
section has_mul
variables {G : Type u} [has_mul G]
/-- `left_mul g` denotes left multiplication by `g` -/
@[to_additive "`left_add g` denotes left addition by `g`"]
def left_mul : G → G → G := λ g : G, λ x : G, g * x
/-- `right_mul g` denotes right multiplication by `g` -/
@[to_additive "`right_add g` denotes right addition by `g`"]
def right_mul : G → G → G := λ g : G, λ x : G, x * g
end has_mul
/-- A semigroup is a type with an associative `(*)`. -/
@[protect_proj, ancestor has_mul] class semigroup (G : Type u) extends has_mul G :=
(mul_assoc : ∀ a b c : G, a * b * c = a * (b * c))
/-- An additive semigroup is a type with an associative `(+)`. -/
@[protect_proj, ancestor has_add] class add_semigroup (G : Type u) extends has_add G :=
(add_assoc : ∀ a b c : G, a + b + c = a + (b + c))
attribute [to_additive] semigroup
section semigroup
variables {G : Type u} [semigroup G]
@[no_rsimp, to_additive]
lemma mul_assoc : ∀ a b c : G, a * b * c = a * (b * c) :=
semigroup.mul_assoc
attribute [no_rsimp] add_assoc -- TODO(Mario): find out why this isn't copying
@[to_additive]
instance semigroup.to_is_associative : is_associative G (*) :=
⟨mul_assoc⟩
end semigroup
/-- A commutative semigroup is a type with an associative commutative `(*)`. -/
@[protect_proj, ancestor semigroup]
class comm_semigroup (G : Type u) extends semigroup G :=
(mul_comm : ∀ a b : G, a * b = b * a)
/-- A commutative additive semigroup is a type with an associative commutative `(+)`. -/
@[protect_proj, ancestor add_semigroup]
class add_comm_semigroup (G : Type u) extends add_semigroup G :=
(add_comm : ∀ a b : G, a + b = b + a)
attribute [to_additive] comm_semigroup
section comm_semigroup
variables {G : Type u} [comm_semigroup G]
@[no_rsimp, to_additive]
lemma mul_comm : ∀ a b : G, a * b = b * a :=
comm_semigroup.mul_comm
attribute [no_rsimp] add_comm
@[to_additive]
instance comm_semigroup.to_is_commutative : is_commutative G (*) :=
⟨mul_comm⟩
end comm_semigroup
/-- A `left_cancel_semigroup` is a semigroup such that `a * b = a * c` implies `b = c`. -/
@[protect_proj, ancestor semigroup]
class left_cancel_semigroup (G : Type u) extends semigroup G :=
(mul_left_cancel : ∀ a b c : G, a * b = a * c → b = c)
/-- An `add_left_cancel_semigroup` is an additive semigroup such that
`a + b = a + c` implies `b = c`. -/
@[protect_proj, ancestor add_semigroup]
class add_left_cancel_semigroup (G : Type u) extends add_semigroup G :=
(add_left_cancel : ∀ a b c : G, a + b = a + c → b = c)
attribute [to_additive add_left_cancel_semigroup] left_cancel_semigroup
section left_cancel_semigroup
variables {G : Type u} [left_cancel_semigroup G] {a b c : G}
@[to_additive]
lemma mul_left_cancel : a * b = a * c → b = c :=
left_cancel_semigroup.mul_left_cancel a b c
@[to_additive]
lemma mul_left_cancel_iff : a * b = a * c ↔ b = c :=
⟨mul_left_cancel, congr_arg _⟩
@[to_additive]
theorem mul_right_injective (a : G) : function.injective ((*) a) :=
λ b c, mul_left_cancel
@[simp, to_additive]
theorem mul_right_inj (a : G) {b c : G} : a * b = a * c ↔ b = c :=
(mul_right_injective a).eq_iff
@[to_additive]
theorem mul_ne_mul_right (a : G) {b c : G} : a * b ≠ a * c ↔ b ≠ c :=
(mul_right_injective a).ne_iff
end left_cancel_semigroup
/-- A `right_cancel_semigroup` is a semigroup such that `a * b = c * b` implies `a = c`. -/
@[protect_proj, ancestor semigroup]
class right_cancel_semigroup (G : Type u) extends semigroup G :=
(mul_right_cancel : ∀ a b c : G, a * b = c * b → a = c)
/-- An `add_right_cancel_semigroup` is an additive semigroup such that
`a + b = c + b` implies `a = c`. -/
@[protect_proj, ancestor add_semigroup]
class add_right_cancel_semigroup (G : Type u) extends add_semigroup G :=
(add_right_cancel : ∀ a b c : G, a + b = c + b → a = c)
attribute [to_additive add_right_cancel_semigroup] right_cancel_semigroup
section right_cancel_semigroup
variables {G : Type u} [right_cancel_semigroup G] {a b c : G}
@[to_additive]
lemma mul_right_cancel : a * b = c * b → a = c :=
right_cancel_semigroup.mul_right_cancel a b c
@[to_additive]
lemma mul_right_cancel_iff : b * a = c * a ↔ b = c :=
⟨mul_right_cancel, congr_arg _⟩
@[to_additive]
theorem mul_left_injective (a : G) : function.injective (λ x, x * a) :=
λ b c, mul_right_cancel
@[simp, to_additive]
theorem mul_left_inj (a : G) {b c : G} : b * a = c * a ↔ b = c :=
(mul_left_injective a).eq_iff
@[to_additive]
theorem mul_ne_mul_left (a : G) {b c : G} : b * a ≠ c * a ↔ b ≠ c :=
(mul_left_injective a).ne_iff
end right_cancel_semigroup
/-- Typeclass for expressing that a type `M` with multiplication and a one satisfies
`1 * a = a` and `a * 1 = a` for all `a : M`. -/
@[ancestor has_one has_mul]
class mul_one_class (M : Type u) extends has_one M, has_mul M :=
(one_mul : ∀ (a : M), 1 * a = a)
(mul_one : ∀ (a : M), a * 1 = a)
/-- Typeclass for expressing that a type `M` with addition and a zero satisfies
`0 + a = a` and `a + 0 = a` for all `a : M`. -/
@[ancestor has_zero has_add]
class add_zero_class (M : Type u) extends has_zero M, has_add M :=
(zero_add : ∀ (a : M), 0 + a = a)
(add_zero : ∀ (a : M), a + 0 = a)
attribute [to_additive] mul_one_class
section mul_one_class
variables {M : Type u} [mul_one_class M]
@[ematch, simp, to_additive]
lemma one_mul : ∀ a : M, 1 * a = a :=
mul_one_class.one_mul
@[ematch, simp, to_additive]
lemma mul_one : ∀ a : M, a * 1 = a :=
mul_one_class.mul_one
attribute [ematch] add_zero zero_add -- TODO(Mario): Make to_additive transfer this
@[to_additive]
instance mul_one_class.to_is_left_id : is_left_id M (*) 1 :=
⟨ mul_one_class.one_mul ⟩
@[to_additive]
instance mul_one_class.to_is_right_id : is_right_id M (*) 1 :=
⟨ mul_one_class.mul_one ⟩
end mul_one_class
section
variables {M : Type u}
-- use `x * npow_rec n x` and not `npow_rec n x * x` in the definition to make sure that
-- definitional unfolding of `npow_rec` is blocked, to avoid deep recursion issues.
/-- The fundamental power operation in a monoid. `npow_rec n a = a*a*...*a` n times.
Use instead `a ^ n`, which has better definitional behavior. -/
def npow_rec [has_one M] [has_mul M] : ℕ → M → M
| 0 a := 1
| (n+1) a := a * npow_rec n a
/-- The fundamental scalar multiplication in an additive monoid. `nsmul_rec n a = a+a+...+a` n
times. Use instead `n • a`, which has better definitional behavior. -/
def nsmul_rec [has_zero M] [has_add M] : ℕ → M → M
| 0 a := 0
| (n+1) a := a + nsmul_rec n a
attribute [to_additive] npow_rec
end
/-- Suppose that one can put two mathematical structures on a type, a rich one `R` and a poor one
`P`, and that one can deduce the poor structure from the rich structure through a map `F` (called a
forgetful functor) (think `R = metric_space` and `P = topological_space`). A possible
implementation would be to have a type class `rich` containing a field `R`, a type class `poor`
containing a field `P`, and an instance from `rich` to `poor`. However, this creates diamond
problems, and a better approach is to let `rich` extend `poor` and have a field saying that
`F R = P`.
To illustrate this, consider the pair `metric_space` / `topological_space`. Consider the topology
on a product of two metric spaces. With the first approach, it could be obtained by going first from
each metric space to its topology, and then taking the product topology. But it could also be
obtained by considering the product metric space (with its sup distance) and then the topology
coming from this distance. These would be the same topology, but not definitionally, which means
that from the point of view of Lean's kernel, there would be two different `topological_space`
instances on the product. This is not compatible with the way instances are designed and used:
there should be at most one instance of a kind on each type. This approach has created an instance
diamond that does not commute definitionally.
The second approach solves this issue. Now, a metric space contains both a distance, a topology, and
a proof that the topology coincides with the one coming from the distance. When one defines the
product of two metric spaces, one uses the sup distance and the product topology, and one has to
give the proof that the sup distance induces the product topology. Following both sides of the
instance diamond then gives rise (definitionally) to the product topology on the product space.
Another approach would be to have the rich type class take the poor type class as an instance
parameter. It would solve the diamond problem, but it would lead to a blow up of the number
of type classes one would need to declare to work with complicated classes, say a real inner
product space, and would create exponential complexity when working with products of
such complicated spaces, that are avoided by bundling things carefully as above.
Note that this description of this specific case of the product of metric spaces is oversimplified
compared to mathlib, as there is an intermediate typeclass between `metric_space` and
`topological_space` called `uniform_space`. The above scheme is used at both levels, embedding a
topology in the uniform space structure, and a uniform structure in the metric space structure.
Note also that, when `P` is a proposition, there is no such issue as any two proofs of `P` are
definitionally equivalent in Lean.
To avoid boilerplate, there are some designs that can automatically fill the poor fields when
creating a rich structure if one doesn't want to do something special about them. For instance,
in the definition of metric spaces, default tactics fill the uniform space fields if they are
not given explicitly. One can also have a helper function creating the rich structure from a
structure with fewer fields, where the helper function fills the remaining fields. See for instance
`uniform_space.of_core` or `real_inner_product.of_core`.
For more details on this question, called the forgetful inheritance pattern, see [Competing
inheritance paths in dependent type theory: a case study in functional
analysis](https://hal.inria.fr/hal-02463336).
-/
library_note "forgetful inheritance"
/-- `try_refl_tac` solves goals of the form `∀ a b, f a b = g a b`,
if they hold by definition. -/
meta def try_refl_tac : tactic unit := `[intros; refl]
/-!
### Design note on `add_monoid` and `monoid`
An `add_monoid` has a natural `ℕ`-action, defined by `n • a = a + ... + a`, that we want to declare
as an instance as it makes it possible to use the language of linear algebra. However, there are
often other natural `ℕ`-actions. For instance, for any semiring `R`, the space of polynomials
`polynomial R` has a natural `R`-action defined by multiplication on the coefficients. This means
that `polynomial ℕ` would have two natural `ℕ`-actions, which are equal but not defeq. The same
goes for linear maps, tensor products, and so on (and even for `ℕ` itself).
To solve this issue, we embed an `ℕ`-action in the definition of an `add_monoid` (which is by
default equal to the naive action `a + ... + a`, but can be adjusted when needed), and declare
a `has_scalar ℕ α` instance using this action. See Note [forgetful inheritance] for more
explanations on this pattern.
For example, when we define `polynomial R`, then we declare the `ℕ`-action to be by multiplication
on each coefficient (using the `ℕ`-action on `R` that comes from the fact that `R` is
an `add_monoid`). In this way, the two natural `has_scalar ℕ (polynomial ℕ)` instances are defeq.
The tactic `to_additive` transfers definitions and results from multiplicative monoids to additive
monoids. To work, it has to map fields to fields. This means that we should also add corresponding
fields to the multiplicative structure `monoid`, which could solve defeq problems for powers if
needed. These problems do not come up in practice, so most of the time we will not need to adjust
the `npow` field when defining multiplicative objects.
Nice notation and a basic theory for the power function on monoids and the `ℕ`-action on additive
monoids are built in the file `algebra.group_power.basic`. For now, we only register the most basic
properties that we need right away.
In the definition, we use `n.succ` instead of `n + 1` in the `nsmul_succ'` and `npow_succ'` fields
to make sure that `to_additive` is not confused (otherwise, it would try to convert `1 : ℕ`
to `0 : ℕ`).
-/
/-- An `add_monoid` is an `add_semigroup` with an element `0` such that `0 + a = a + 0 = a`. -/
@[ancestor add_semigroup add_zero_class]
class add_monoid (M : Type u) extends add_semigroup M, add_zero_class M :=
(nsmul : ℕ → M → M := nsmul_rec)
(nsmul_zero' : ∀ x, nsmul 0 x = 0 . try_refl_tac)
(nsmul_succ' : ∀ (n : ℕ) x, nsmul n.succ x = x + nsmul n x . try_refl_tac)
export add_monoid (nsmul)
/-- A `monoid` is a `semigroup` with an element `1` such that `1 * a = a * 1 = a`. -/
@[ancestor semigroup mul_one_class, to_additive]
class monoid (M : Type u) extends semigroup M, mul_one_class M :=
(npow : ℕ → M → M := npow_rec)
(npow_zero' : ∀ x, npow 0 x = 1 . try_refl_tac)
(npow_succ' : ∀ (n : ℕ) x, npow n.succ x = x * npow n x . try_refl_tac)
export monoid (npow)
section monoid
variables {M : Type u} [monoid M]
@[to_additive]
lemma left_inv_eq_right_inv {a b c : M} (hba : b * a = 1) (hac : a * c = 1) : b = c :=
by rw [←one_mul c, ←hba, mul_assoc, hac, mul_one b]
end monoid
lemma npow_one {M : Type u} [monoid M] (x : M) :
npow 1 x = x :=
by simp [monoid.npow_succ', monoid.npow_zero']
lemma nsmul_one' {M : Type u} [add_monoid M] (x : M) :
nsmul 1 x = x :=
by simp [add_monoid.nsmul_succ', add_monoid.nsmul_zero']
attribute [to_additive nsmul_one'] npow_one
@[to_additive nsmul_add']
lemma npow_add {M : Type u} [monoid M] (m n : ℕ) (x : M) :
npow (m + n) x = npow m x * npow n x :=
begin
induction m with m ih,
{ rw [nat.zero_add, monoid.npow_zero', one_mul], },
{ rw [nat.succ_add, monoid.npow_succ', monoid.npow_succ', ih, ← mul_assoc] }
end
/-- An additive commutative monoid is an additive monoid with commutative `(+)`. -/
@[protect_proj, ancestor add_monoid add_comm_semigroup]
class add_comm_monoid (M : Type u) extends add_monoid M, add_comm_semigroup M
/-- A commutative monoid is a monoid with commutative `(*)`. -/
@[protect_proj, ancestor monoid comm_semigroup, to_additive]
class comm_monoid (M : Type u) extends monoid M, comm_semigroup M
section left_cancel_monoid
/-- An additive monoid in which addition is left-cancellative.
Main examples are `ℕ` and groups. This is the right typeclass for many sum lemmas, as having a zero
is useful to define the sum over the empty set, so `add_left_cancel_semigroup` is not enough. -/
@[protect_proj, ancestor add_left_cancel_semigroup add_monoid]
class add_left_cancel_monoid (M : Type u) extends add_left_cancel_semigroup M, add_monoid M
/-- A monoid in which multiplication is left-cancellative. -/
@[protect_proj, ancestor left_cancel_semigroup monoid, to_additive add_left_cancel_monoid]
class left_cancel_monoid (M : Type u) extends left_cancel_semigroup M, monoid M
end left_cancel_monoid
section right_cancel_monoid
/-- An additive monoid in which addition is right-cancellative.
Main examples are `ℕ` and groups. This is the right typeclass for many sum lemmas, as having a zero
is useful to define the sum over the empty set, so `add_right_cancel_semigroup` is not enough. -/
@[protect_proj, ancestor add_right_cancel_semigroup add_monoid]
class add_right_cancel_monoid (M : Type u) extends add_right_cancel_semigroup M, add_monoid M
/-- A monoid in which multiplication is right-cancellative. -/
@[protect_proj, ancestor right_cancel_semigroup monoid, to_additive add_right_cancel_monoid]
class right_cancel_monoid (M : Type u) extends right_cancel_semigroup M, monoid M
end right_cancel_monoid
section cancel_monoid
/-- An additive monoid in which addition is cancellative on both sides.
Main examples are `ℕ` and groups. This is the right typeclass for many sum lemmas, as having a zero
is useful to define the sum over the empty set, so `add_right_cancel_semigroup` is not enough. -/
@[protect_proj, ancestor add_left_cancel_monoid add_right_cancel_monoid]
class add_cancel_monoid (M : Type u)
extends add_left_cancel_monoid M, add_right_cancel_monoid M
/-- A monoid in which multiplication is cancellative. -/
@[protect_proj, ancestor left_cancel_monoid right_cancel_monoid, to_additive add_cancel_monoid]
class cancel_monoid (M : Type u) extends left_cancel_monoid M, right_cancel_monoid M
/-- Commutative version of add_cancel_monoid. -/
@[protect_proj, ancestor add_left_cancel_monoid add_comm_monoid]
class add_cancel_comm_monoid (M : Type u) extends add_left_cancel_monoid M, add_comm_monoid M
/-- Commutative version of cancel_monoid. -/
@[protect_proj, ancestor left_cancel_monoid comm_monoid, to_additive add_cancel_comm_monoid]
class cancel_comm_monoid (M : Type u) extends left_cancel_monoid M, comm_monoid M
@[priority 100, to_additive] -- see Note [lower instance priority]
instance cancel_comm_monoid.to_cancel_monoid (M : Type u) [cancel_comm_monoid M] :
cancel_monoid M :=
{ mul_right_cancel := λ a b c h, mul_left_cancel $ by rw [mul_comm, h, mul_comm],
.. ‹cancel_comm_monoid M› }
end cancel_monoid
/-- The fundamental power operation in a group. `gpow_rec n a = a*a*...*a` n times, for integer `n`.
Use instead `a ^ n`, which has better definitional behavior. -/
def gpow_rec {M : Type*} [has_one M] [has_mul M] [has_inv M] : ℤ → M → M
| (int.of_nat n) a := npow_rec n a
| -[1+ n] a := (npow_rec n.succ a) ⁻¹
/-- The fundamental scalar multiplication in an additive group. `gsmul_rec n a = a+a+...+a` n
times, for integer `n`. Use instead `n • a`, which has better definitional behavior. -/
def gsmul_rec {M : Type*} [has_zero M] [has_add M] [has_neg M]: ℤ → M → M
| (int.of_nat n) a := nsmul_rec n a
| -[1+ n] a := - (nsmul_rec n.succ a)
attribute [to_additive] gpow_rec
/-- A `div_inv_monoid` is a `monoid` with operations `/` and `⁻¹` satisfying
`div_eq_mul_inv : ∀ a b, a / b = a * b⁻¹`.
This is the immediate common ancestor of `group` and `group_with_zero`,
in order to deduplicate the name `div_eq_mul_inv`.
The default for `div` is such that `a / b = a * b⁻¹` holds by definition.
Adding `div` as a field rather than defining `a / b := a * b⁻¹` allows us to
avoid certain classes of unification failures, for example:
Let `foo X` be a type with a `∀ X, has_div (foo X)` instance but no
`∀ X, has_inv (foo X)`, e.g. when `foo X` is a `euclidean_domain`. Suppose we
also have an instance `∀ X [cromulent X], group_with_zero (foo X)`. Then the
`(/)` coming from `group_with_zero_has_div` cannot be definitionally equal to
the `(/)` coming from `foo.has_div`.
In the same way, adding a `gpow` field makes it possible to avoid definitional failures
in diamonds. See the definition of `monoid` and Note [forgetful inheritance] for more
explanations on this.
-/
@[protect_proj, ancestor monoid has_inv has_div]
class div_inv_monoid (G : Type u) extends monoid G, has_inv G, has_div G :=
(div := λ a b, a * b⁻¹)
(div_eq_mul_inv : ∀ a b : G, a / b = a * b⁻¹ . try_refl_tac)
(gpow : ℤ → G → G := gpow_rec)
(gpow_zero' : ∀ (a : G), gpow 0 a = 1 . try_refl_tac)
(gpow_succ' :
∀ (n : ℕ) (a : G), gpow (int.of_nat n.succ) a = a * gpow (int.of_nat n) a . try_refl_tac)
(gpow_neg' :
∀ (n : ℕ) (a : G), gpow (-[1+ n]) a = (gpow n.succ a) ⁻¹ . try_refl_tac)
export div_inv_monoid (gpow)
/-- A `sub_neg_monoid` is an `add_monoid` with unary `-` and binary `-` operations
satisfying `sub_eq_add_neg : ∀ a b, a - b = a + -b`.
The default for `sub` is such that `a - b = a + -b` holds by definition.
Adding `sub` as a field rather than defining `a - b := a + -b` allows us to
avoid certain classes of unification failures, for example:
Let `foo X` be a type with a `∀ X, has_sub (foo X)` instance but no
`∀ X, has_neg (foo X)`. Suppose we also have an instance
`∀ X [cromulent X], add_group (foo X)`. Then the `(-)` coming from
`add_group.has_sub` cannot be definitionally equal to the `(-)` coming from
`foo.has_sub`.
In the same way, adding a `gsmul` field makes it possible to avoid definitional failures
in diamonds. See the definition of `add_monoid` and Note [forgetful inheritance] for more
explanations on this.
-/
@[protect_proj, ancestor add_monoid has_neg has_sub]
class sub_neg_monoid (G : Type u) extends add_monoid G, has_neg G, has_sub G :=
(sub := λ a b, a + -b)
(sub_eq_add_neg : ∀ a b : G, a - b = a + -b . try_refl_tac)
(gsmul : ℤ → G → G := gsmul_rec)
(gsmul_zero' : ∀ (a : G), gsmul 0 a = 0 . try_refl_tac)
(gsmul_succ' :
∀ (n : ℕ) (a : G), gsmul (int.of_nat n.succ) a = a + gsmul (int.of_nat n) a . try_refl_tac)
(gsmul_neg' :
∀ (n : ℕ) (a : G), gsmul (-[1+ n]) a = - (gsmul n.succ a) . try_refl_tac)
export sub_neg_monoid (gsmul)
attribute [to_additive sub_neg_monoid] div_inv_monoid
@[to_additive]
lemma div_eq_mul_inv {G : Type u} [div_inv_monoid G] :
∀ a b : G, a / b = a * b⁻¹ :=
div_inv_monoid.div_eq_mul_inv
/-- A `group` is a `monoid` with an operation `⁻¹` satisfying `a⁻¹ * a = 1`.
There is also a division operation `/` such that `a / b = a * b⁻¹`,
with a default so that `a / b = a * b⁻¹` holds by definition.
-/
@[protect_proj, ancestor div_inv_monoid]
class group (G : Type u) extends div_inv_monoid G :=
(mul_left_inv : ∀ a : G, a⁻¹ * a = 1)
/-- An `add_group` is an `add_monoid` with a unary `-` satisfying `-a + a = 0`.
There is also a binary operation `-` such that `a - b = a + -b`,
with a default so that `a - b = a + -b` holds by definition.
-/
@[protect_proj, ancestor sub_neg_monoid]
class add_group (A : Type u) extends sub_neg_monoid A :=
(add_left_neg : ∀ a : A, -a + a = 0)
attribute [to_additive] group
/-- Abbreviation for `@div_inv_monoid.to_monoid _ (@group.to_div_inv_monoid _ _)`.
Useful because it corresponds to the fact that `Grp` is a subcategory of `Mon`.
Not an instance since it duplicates `@div_inv_monoid.to_monoid _ (@group.to_div_inv_monoid _ _)`.
-/
@[to_additive
"Abbreviation for `@sub_neg_monoid.to_add_monoid _ (@add_group.to_sub_neg_monoid _ _)`.
Useful because it corresponds to the fact that `AddGroup` is a subcategory of `AddMon`.
Not an instance since it duplicates
`@sub_neg_monoid.to_add_monoid _ (@add_group.to_sub_neg_monoid _ _)`."]
def group.to_monoid (G : Type u) [group G] : monoid G :=
@div_inv_monoid.to_monoid _ (@group.to_div_inv_monoid _ _)
section group
variables {G : Type u} [group G] {a b c : G}
@[simp, to_additive]
lemma mul_left_inv : ∀ a : G, a⁻¹ * a = 1 :=
group.mul_left_inv
@[to_additive] lemma inv_mul_self (a : G) : a⁻¹ * a = 1 := mul_left_inv a
@[simp, to_additive]
lemma inv_mul_cancel_left (a b : G) : a⁻¹ * (a * b) = b :=
by rw [← mul_assoc, mul_left_inv, one_mul]
@[simp, to_additive]
lemma inv_eq_of_mul_eq_one (h : a * b = 1) : a⁻¹ = b :=
left_inv_eq_right_inv (inv_mul_self a) h
@[simp, to_additive]
lemma inv_inv (a : G) : (a⁻¹)⁻¹ = a :=
inv_eq_of_mul_eq_one (mul_left_inv a)
@[simp, to_additive]
lemma mul_right_inv (a : G) : a * a⁻¹ = 1 :=
have a⁻¹⁻¹ * a⁻¹ = 1 := mul_left_inv a⁻¹,
by rwa [inv_inv] at this
@[to_additive] lemma mul_inv_self (a : G) : a * a⁻¹ = 1 := mul_right_inv a
@[simp, to_additive]
lemma mul_inv_cancel_right (a b : G) : a * b * b⁻¹ = a :=
by rw [mul_assoc, mul_right_inv, mul_one]
@[priority 100, to_additive] -- see Note [lower instance priority]
instance group.to_cancel_monoid : cancel_monoid G :=
{ mul_right_cancel := λ a b c h, by rw [← mul_inv_cancel_right a b, h, mul_inv_cancel_right],
mul_left_cancel := λ a b c h, by rw [← inv_mul_cancel_left a b, h, inv_mul_cancel_left],
..‹group G› }
end group
/-- A commutative group is a group with commutative `(*)`. -/
@[protect_proj, ancestor group comm_monoid]
class comm_group (G : Type u) extends group G, comm_monoid G
/-- An additive commutative group is an additive group with commutative `(+)`. -/
@[protect_proj, ancestor add_group add_comm_monoid]
class add_comm_group (G : Type u) extends add_group G, add_comm_monoid G
attribute [to_additive] comm_group
attribute [instance, priority 300] add_comm_group.to_add_comm_monoid
section comm_group
variables {G : Type u} [comm_group G]
@[priority 100, to_additive] -- see Note [lower instance priority]
instance comm_group.to_cancel_comm_monoid : cancel_comm_monoid G :=
{ ..‹comm_group G›,
..group.to_cancel_monoid }
end comm_group
|
9ae38159044d85afb92ac680cfffb06387805783 | ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5 | /stage0/src/Lean/PrettyPrinter/Formatter.lean | 5fc7711df070567a86ae6a321541ba6c6e09c44f | [
"Apache-2.0"
] | permissive | dupuisf/lean4 | d082d13b01243e1de29ae680eefb476961221eef | 6a39c65bd28eb0e28c3870188f348c8914502718 | refs/heads/master | 1,676,948,755,391 | 1,610,665,114,000 | 1,610,665,114,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 21,182 | lean | /-
Copyright (c) 2020 Sebastian Ullrich. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
/-!
The formatter turns a `Syntax` tree into a `Format` object, inserting both mandatory whitespace (to separate adjacent
tokens) as well as "pretty" optional whitespace.
The basic approach works much like the parenthesizer: A right-to-left traversal over the syntax tree, driven by
parser-specific handlers registered via attributes. The traversal is right-to-left so that when emitting a token, we
already know the text following it and can decide whether or not whitespace between the two is necessary.
-/
import Lean.CoreM
import Lean.Parser.Extension
import Lean.KeyedDeclsAttribute
import Lean.ParserCompiler.Attribute
import Lean.PrettyPrinter.Basic
namespace Lean
namespace PrettyPrinter
namespace Formatter
structure Context where
options : Options
table : Parser.TokenTable
structure State where
stxTrav : Syntax.Traverser
-- Textual content of `stack` up to the first whitespace (not enclosed in an escaped ident). We assume that the textual
-- content of `stack` is modified only by `pushText` and `pushLine`, so `leadWord` is adjusted there accordingly.
leadWord : String := ""
-- Stack of generated Format objects, analogous to the Syntax stack in the parser.
-- Note, however, that the stack is reversed because of the right-to-left traversal.
stack : Array Format := #[]
end Formatter
abbrev FormatterM := ReaderT Formatter.Context $ StateRefT Formatter.State CoreM
@[inline] def FormatterM.orelse {α} (p₁ p₂ : FormatterM α) : FormatterM α := do
let s ← get
catchInternalId backtrackExceptionId
p₁
(fun _ => do set s; p₂)
instance {α} : OrElse (FormatterM α) := ⟨FormatterM.orelse⟩
abbrev Formatter := FormatterM Unit
unsafe def mkFormatterAttribute : IO (KeyedDeclsAttribute Formatter) :=
KeyedDeclsAttribute.init {
builtinName := `builtinFormatter,
name := `formatter,
descr := "Register a formatter for a parser.
[formatter k] registers a declaration of type `Lean.PrettyPrinter.Formatter` for the `SyntaxNodeKind` `k`.",
valueTypeName := `Lean.PrettyPrinter.Formatter,
evalKey := fun builtin stx => do
let env ← getEnv
let id ← Attribute.Builtin.getId stx
-- `isValidSyntaxNodeKind` is updated only in the next stage for new `[builtin*Parser]`s, but we try to
-- synthesize a formatter for it immediately, so we just check for a declaration in this case
if (builtin && (env.find? id).isSome) || Parser.isValidSyntaxNodeKind env id then pure id
else throwError! "invalid [formatter] argument, unknown syntax kind '{id}'"
} `Lean.PrettyPrinter.formatterAttribute
@[builtinInit mkFormatterAttribute] constant formatterAttribute : KeyedDeclsAttribute Formatter
unsafe def mkCombinatorFormatterAttribute : IO ParserCompiler.CombinatorAttribute :=
ParserCompiler.registerCombinatorAttribute
`combinatorFormatter
"Register a formatter for a parser combinator.
[combinatorFormatter c] registers a declaration of type `Lean.PrettyPrinter.Formatter` for the `Parser` declaration `c`.
Note that, unlike with [formatter], this is not a node kind since combinators usually do not introduce their own node kinds.
The tagged declaration may optionally accept parameters corresponding to (a prefix of) those of `c`, where `Parser` is replaced
with `Formatter` in the parameter types."
@[builtinInit mkCombinatorFormatterAttribute] constant combinatorFormatterAttribute : ParserCompiler.CombinatorAttribute
namespace Formatter
open Lean.Core
open Lean.Parser
def throwBacktrack {α} : FormatterM α :=
throw $ Exception.internal backtrackExceptionId
instance : Syntax.MonadTraverser FormatterM := ⟨{
get := State.stxTrav <$> get,
set := fun t => modify (fun st => { st with stxTrav := t }),
modifyGet := fun f => modifyGet (fun st => let (a, t) := f st.stxTrav; (a, { st with stxTrav := t }))
}⟩
open Syntax.MonadTraverser
def getStack : FormatterM (Array Format) := do
let st ← get
pure st.stack
def getStackSize : FormatterM Nat := do
let stack ← getStack;
pure stack.size
def setStack (stack : Array Format) : FormatterM Unit :=
modify fun st => { st with stack := stack }
def push (f : Format) : FormatterM Unit :=
modify fun st => { st with stack := st.stack.push f }
def pushLine : FormatterM Unit := do
push Format.line;
modify fun st => { st with leadWord := "" }
/-- Execute `x` at the right-most child of the current node, if any, then advance to the left. -/
def visitArgs (x : FormatterM Unit) : FormatterM Unit := do
let stx ← getCur
if stx.getArgs.size > 0 then
goDown (stx.getArgs.size - 1) *> x <* goUp
goLeft
/-- Execute `x`, pass array of generated Format objects to `fn`, and push result. -/
def fold (fn : Array Format → Format) (x : FormatterM Unit) : FormatterM Unit := do
let sp ← getStackSize
x
let stack ← getStack
let f := fn $ stack.extract sp stack.size
setStack $ (stack.shrink sp).push f
/-- Execute `x` and concatenate generated Format objects. -/
def concat (x : FormatterM Unit) : FormatterM Unit := do
fold (Array.foldl (fun acc f => if acc.isNil then f else f ++ acc) Format.nil) x
def indent (x : Formatter) (indent : Option Int := none) : Formatter := do
concat x
let ctx ← read
let indent := indent.getD $ Std.Format.getIndent ctx.options
modify fun st => { st with stack := st.stack.pop.push (Format.nest indent st.stack.back) }
def group (x : Formatter) : Formatter := do
concat x
modify fun st => { st with stack := st.stack.pop.push (Format.fill st.stack.back) }
@[combinatorFormatter Lean.Parser.orelse] def orelse.formatter (p1 p2 : Formatter) : Formatter :=
-- HACK: We have no (immediate) information on which side of the orelse could have produced the current node, so try
-- them in turn. Uses the syntax traverser non-linearly!
p1 <|> p2
-- `mkAntiquot` is quite complex, so we'd rather have its formatter synthesized below the actual parser definition.
-- Note that there is a mutual recursion
-- `categoryParser -> mkAntiquot -> termParser -> categoryParser`, so we need to introduce an indirection somewhere
-- anyway.
@[extern "lean_mk_antiquot_formatter"]
constant mkAntiquot.formatter' (name : String) (kind : Option SyntaxNodeKind) (anonymous := true) : Formatter
-- break up big mutual recursion
@[extern "lean_pretty_printer_formatter_interpret_parser_descr"]
constant interpretParserDescr' : ParserDescr → CoreM Formatter
unsafe def formatterForKindUnsafe (k : SyntaxNodeKind) : Formatter := do
(← liftM $ runForNodeKind formatterAttribute k interpretParserDescr')
@[implementedBy formatterForKindUnsafe]
constant formatterForKind (k : SyntaxNodeKind) : Formatter
@[combinatorFormatter Lean.Parser.withAntiquot]
def withAntiquot.formatter (antiP p : Formatter) : Formatter :=
-- TODO: could be optimized using `isAntiquot` (which would have to be moved), but I'd rather
-- fix the backtracking hack outright.
orelse.formatter antiP p
@[combinatorFormatter Lean.Parser.withAntiquotSuffixSplice]
def withAntiquotSuffixSplice.formatter (k : SyntaxNodeKind) (p suffix : Formatter) : Formatter := do
if (← getCur).isAntiquotSuffixSplice then
visitArgs <| suffix *> p
else
p
@[combinatorFormatter Lean.Parser.tokenWithAntiquot]
def tokenWithAntiquot.formatter (p : Formatter) : Formatter := do
if (← getCur).isTokenAntiquot then
visitArgs p
else
p
@[combinatorFormatter Lean.Parser.categoryParser]
def categoryParser.formatter (cat : Name) : Formatter := group $ indent do
let stx ← getCur
trace[PrettyPrinter.format]! "formatting {indentD (fmt stx)}"
if stx.getKind == `choice then
visitArgs do
-- format only last choice
-- TODO: We could use elaborator data here to format the chosen child when available
formatterForKind (← getCur).getKind
else
withAntiquot.formatter (mkAntiquot.formatter' cat.toString none) (formatterForKind stx.getKind)
@[combinatorFormatter Lean.Parser.categoryParserOfStack]
def categoryParserOfStack.formatter (offset : Nat) : Formatter := do
let st ← get
let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset)
categoryParser.formatter stx.getId
@[combinatorFormatter Lean.Parser.parserOfStack]
def parserOfStack.formatter (offset : Nat) (prec : Nat := 0) : Formatter := do
let st ← get
let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset)
formatterForKind stx.getKind
@[combinatorFormatter Lean.Parser.error]
def error.formatter (msg : String) : Formatter := pure ()
@[combinatorFormatter Lean.Parser.errorAtSavedPos]
def errorAtSavedPos.formatter (msg : String) (delta : Bool) : Formatter := pure ()
@[combinatorFormatter Lean.Parser.atomic]
def atomic.formatter (p : Formatter) : Formatter := p
@[combinatorFormatter Lean.Parser.lookahead]
def lookahead.formatter (p : Formatter) : Formatter := pure ()
@[combinatorFormatter Lean.Parser.notFollowedBy]
def notFollowedBy.formatter (p : Formatter) : Formatter := pure ()
@[combinatorFormatter Lean.Parser.andthen]
def andthen.formatter (p1 p2 : Formatter) : Formatter := p2 *> p1
def checkKind (k : SyntaxNodeKind) : FormatterM Unit := do
let stx ← getCur
if k != stx.getKind then
trace[PrettyPrinter.format.backtrack]! "unexpected node kind '{stx.getKind}', expected '{k}'"
throwBacktrack
@[combinatorFormatter Lean.Parser.node]
def node.formatter (k : SyntaxNodeKind) (p : Formatter) : Formatter := do
checkKind k;
visitArgs p
@[combinatorFormatter Lean.Parser.trailingNode]
def trailingNode.formatter (k : SyntaxNodeKind) (_ : Nat) (p : Formatter) : Formatter := do
checkKind k
visitArgs do
p;
-- leading term, not actually produced by `p`
categoryParser.formatter `foo
def parseToken (s : String) : FormatterM ParserState := do
Parser.tokenFn {
input := s,
fileName := "",
fileMap := FileMap.ofString "",
prec := 0,
env := ← getEnv,
options := ← getOptions,
tokens := (← read).table } (Parser.mkParserState s)
def pushTokenCore (tk : String) : FormatterM Unit := do
if tk.toSubstring.dropRightWhile (fun s => s == ' ') == tk.toSubstring then
push tk
else
pushLine
push tk.trimRight
def pushToken (info : SourceInfo) (tk : String) : FormatterM Unit := do
match info.trailing with
| some ss =>
-- preserve non-whitespace content (i.e. comments)
let ss' := ss.trim
if !ss'.isEmpty then
let ws := { ss with startPos := ss'.stopPos }
if ws.contains '\n' then
push s!"\n{ss'}"
else
push s!" {ss'}"
modify fun st => { st with leadWord := "" }
| none => pure ()
let st ← get
-- If there is no space between `tk` and the next word, see if we would parse more than `tk` as a single token
if st.leadWord != "" && tk.trimRight == tk then
let tk' := tk.trimLeft
let t ← parseToken $ tk' ++ st.leadWord
if t.pos <= tk'.bsize then
-- stopped within `tk` => use it as is, extend `leadWord` if not prefixed by whitespace
pushTokenCore tk
modify fun st => { st with leadWord := if tk.trimLeft == tk then tk ++ st.leadWord else "" }
else
-- stopped after `tk` => add space
pushTokenCore $ tk ++ " "
modify fun st => { st with leadWord := if tk.trimLeft == tk then tk else "" }
else
-- already separated => use `tk` as is
pushTokenCore tk
modify fun st => { st with leadWord := if tk.trimLeft == tk then tk else "" }
match info.leading with
| some ss =>
-- preserve non-whitespace content (i.e. comments)
let ss' := ss.trim
if !ss'.isEmpty then
let ws := { ss with startPos := ss'.stopPos }
if ws.contains '\n' then do
-- Indentation is automatically increased when entering a category, but comments should be aligned
-- with the actual token, so dedent
indent (push s!"{ss'}\n") (some ((0:Int) - Std.Format.getIndent (← getOptions)))
else
push s!"{ss'} "
modify fun st => { st with leadWord := "" }
| none => pure ()
@[combinatorFormatter Lean.Parser.symbolNoAntiquot]
def symbolNoAntiquot.formatter (sym : String) : Formatter := do
let stx ← getCur
if stx.isToken sym then do
let (Syntax.atom info _) ← pure stx | unreachable!
pushToken info sym
goLeft
else do
trace[PrettyPrinter.format.backtrack]! "unexpected syntax '{fmt stx}', expected symbol '{sym}'"
throwBacktrack
@[combinatorFormatter Lean.Parser.nonReservedSymbolNoAntiquot] def nonReservedSymbolNoAntiquot.formatter := symbolNoAntiquot.formatter
@[combinatorFormatter Lean.Parser.unicodeSymbolNoAntiquot]
def unicodeSymbolNoAntiquot.formatter (sym asciiSym : String) : Formatter := do
let Syntax.atom info val ← getCur
| throwError m!"not an atom: {← getCur}"
if val == sym.trim then
pushToken info sym
else
pushToken info asciiSym;
goLeft
@[combinatorFormatter Lean.Parser.identNoAntiquot]
def identNoAntiquot.formatter : Formatter := do
checkKind identKind;
let Syntax.ident info _ id _ ← getCur
| throwError m!"not an ident: {← getCur}"
let id := id.simpMacroScopes
let s := id.toString;
if id.isAnonymous then
pushToken info "[anonymous]"
else if isInaccessibleUserName id || id.components.any Name.isNum ||
-- loose bvar
"#".isPrefixOf s then
-- not parsable anyway, output as-is
pushToken info s
else
-- try to parse `s` as-is; if it fails, escape
let pst ← parseToken s
if pst.pos == s.bsize then
pushToken info s
else
-- TODO: do something better than escaping all parts
let id := (id.components.map fun c => "«" ++ toString c ++ "»").foldl Name.mkStr Name.anonymous
pushToken info id.toString
goLeft
@[combinatorFormatter Lean.Parser.rawIdentNoAntiquot] def rawIdentNoAntiquot.formatter : Formatter := do
checkKind identKind
let Syntax.ident info _ id _ ← getCur
| throwError m!"not an ident: {← getCur}"
pushToken info id.toString
goLeft
@[combinatorFormatter Lean.Parser.identEq] def identEq.formatter (id : Name) := rawIdentNoAntiquot.formatter
def visitAtom (k : SyntaxNodeKind) : Formatter := do
let stx ← getCur
if k != Name.anonymous then
checkKind k
let Syntax.atom info val ← pure $ stx.ifNode (fun n => n.getArg 0) (fun _ => stx)
| throwError m!"not an atom: {stx}"
pushToken info val
goLeft
@[combinatorFormatter Lean.Parser.charLitNoAntiquot] def charLitNoAntiquot.formatter := visitAtom charLitKind
@[combinatorFormatter Lean.Parser.strLitNoAntiquot] def strLitNoAntiquot.formatter := visitAtom strLitKind
@[combinatorFormatter Lean.Parser.nameLitNoAntiquot] def nameLitNoAntiquot.formatter := visitAtom nameLitKind
@[combinatorFormatter Lean.Parser.numLitNoAntiquot] def numLitNoAntiquot.formatter := visitAtom numLitKind
@[combinatorFormatter Lean.Parser.scientificLitNoAntiquot] def scientificLitNoAntiquot.formatter := visitAtom scientificLitKind
@[combinatorFormatter Lean.Parser.fieldIdx] def fieldIdx.formatter := visitAtom fieldIdxKind
@[combinatorFormatter Lean.Parser.manyNoAntiquot]
def manyNoAntiquot.formatter (p : Formatter) : Formatter := do
let stx ← getCur
visitArgs $ stx.getArgs.size.forM fun _ => p
@[combinatorFormatter Lean.Parser.many1NoAntiquot] def many1NoAntiquot.formatter (p : Formatter) : Formatter := manyNoAntiquot.formatter p
@[combinatorFormatter Lean.Parser.optionalNoAntiquot]
def optionalNoAntiquot.formatter (p : Formatter) : Formatter := visitArgs p
@[combinatorFormatter Lean.Parser.many1Unbox]
def many1Unbox.formatter (p : Formatter) : Formatter := do
let stx ← getCur
if stx.getKind == nullKind then do
manyNoAntiquot.formatter p
else
p
@[combinatorFormatter Lean.Parser.sepByNoAntiquot]
def sepByNoAntiquot.formatter (p pSep : Formatter) : Formatter := do
let stx ← getCur
visitArgs $ (List.range stx.getArgs.size).reverse.forM $ fun i => if i % 2 == 0 then p else pSep
@[combinatorFormatter Lean.Parser.sepBy1NoAntiquot] def sepBy1NoAntiquot.formatter := sepByNoAntiquot.formatter
@[combinatorFormatter Lean.Parser.withPosition] def withPosition.formatter (p : Formatter) : Formatter := p
@[combinatorFormatter Lean.Parser.withoutPosition] def withoutPosition.formatter (p : Formatter) : Formatter := p
@[combinatorFormatter Lean.Parser.withForbidden] def withForbidden.formatter (tk : Token) (p : Formatter) : Formatter := p
@[combinatorFormatter Lean.Parser.withoutForbidden] def withoutForbidden.formatter (p : Formatter) : Formatter := p
@[combinatorFormatter Lean.Parser.withoutInfo] def withoutInfo.formatter (p : Formatter) : Formatter := p
@[combinatorFormatter Lean.Parser.setExpected]
def setExpected.formatter (expected : List String) (p : Formatter) : Formatter := p
@[combinatorFormatter Lean.Parser.toggleInsideQuot] def toggleInsideQuot.formatter (p : Formatter) : Formatter := p
@[combinatorFormatter Lean.Parser.suppressInsideQuot] def suppressInsideQuot.formatter (p : Formatter) : Formatter := p
@[combinatorFormatter Lean.Parser.evalInsideQuot] def evalInsideQuot.formatter (declName : Name) (p : Formatter) : Formatter := p
@[combinatorFormatter Lean.Parser.checkWsBefore] def checkWsBefore.formatter : Formatter := do
let st ← get
if st.leadWord != "" then
pushLine
@[combinatorFormatter Lean.Parser.checkPrec] def checkPrec.formatter : Formatter := pure ()
@[combinatorFormatter Lean.Parser.checkStackTop] def checkStackTop.formatter : Formatter := pure ()
@[combinatorFormatter Lean.Parser.checkNoWsBefore] def checkNoWsBefore.formatter : Formatter :=
-- prevent automatic whitespace insertion
modify fun st => { st with leadWord := "" }
@[combinatorFormatter Lean.Parser.checkTailWs] def checkTailWs.formatter : Formatter := pure ()
@[combinatorFormatter Lean.Parser.checkColGe] def checkColGe.formatter : Formatter := pure ()
@[combinatorFormatter Lean.Parser.checkColGt] def checkColGt.formatter : Formatter := pure ()
@[combinatorFormatter Lean.Parser.checkLineEq] def checkLineEq.formatter : Formatter := pure ()
@[combinatorFormatter Lean.Parser.eoi] def eoi.formatter : Formatter := pure ()
@[combinatorFormatter Lean.Parser.notFollowedByCategoryToken] def notFollowedByCategoryToken.formatter : Formatter := pure ()
@[combinatorFormatter Lean.Parser.checkNoImmediateColon] def checkNoImmediateColon.formatter : Formatter := pure ()
@[combinatorFormatter Lean.Parser.checkInsideQuot] def checkInsideQuot.formatter : Formatter := pure ()
@[combinatorFormatter Lean.Parser.checkOutsideQuot] def checkOutsideQuot.formatter : Formatter := pure ()
@[combinatorFormatter Lean.Parser.skip] def skip.formatter : Formatter := pure ()
@[combinatorFormatter Lean.Parser.pushNone] def pushNone.formatter : Formatter := goLeft
@[combinatorFormatter Lean.Parser.interpolatedStr]
def interpolatedStr.formatter (p : Formatter) : Formatter := do
visitArgs $ (← getCur).getArgs.reverse.forM fun chunk =>
match chunk.isLit? interpolatedStrLitKind with
| some str => push str *> goLeft
| none => p
@[combinatorFormatter ite, macroInline] def ite {α : Type} (c : Prop) [h : Decidable c] (t e : Formatter) : Formatter :=
if c then t else e
abbrev FormatterAliasValue := AliasValue Formatter
builtin_initialize formatterAliasesRef : IO.Ref (NameMap FormatterAliasValue) ← IO.mkRef {}
def registerAlias (aliasName : Name) (v : FormatterAliasValue) : IO Unit := do
Parser.registerAliasCore formatterAliasesRef aliasName v
instance : Coe Formatter FormatterAliasValue := { coe := AliasValue.const }
instance : Coe (Formatter → Formatter) FormatterAliasValue := { coe := AliasValue.unary }
instance : Coe (Formatter → Formatter → Formatter) FormatterAliasValue := { coe := AliasValue.binary }
builtin_initialize
registerAlias "ws" checkWsBefore.formatter
registerAlias "noWs" checkNoWsBefore.formatter
registerAlias "colGt" checkColGt.formatter
registerAlias "colGe" checkColGe.formatter
registerAlias "lookahead" lookahead.formatter
registerAlias "atomic" atomic.formatter
registerAlias "notFollowedBy" notFollowedBy.formatter
registerAlias "withPosition" withPosition.formatter
registerAlias "interpolatedStr" interpolatedStr.formatter
registerAlias "orelse" orelse.formatter
registerAlias "andthen" andthen.formatter
end Formatter
open Formatter
def format (formatter : Formatter) (stx : Syntax) : CoreM Format := do
trace[PrettyPrinter.format.input]! "{fmt stx}"
let options ← getOptions
let table ← Parser.builtinTokenTable.get
catchInternalId backtrackExceptionId
(do
let (_, st) ← (concat formatter { table := table, options := options }).run { stxTrav := Syntax.Traverser.fromSyntax stx };
pure $ Format.fill $ st.stack.get! 0)
(fun _ => throwError "format: uncaught backtrack exception")
def formatTerm := format $ categoryParser.formatter `term
def formatCommand := format $ categoryParser.formatter `command
builtin_initialize registerTraceClass `PrettyPrinter.format;
end PrettyPrinter
end Lean
|
a8a169848d310b1a0538445f2dd7480f06a56272 | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/field_theory/separable.lean | bb0461503d3e6de138f41bc4ff1bc80f36009f01 | [
"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 | 22,182 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau.
-/
import algebra.polynomial.big_operators
import field_theory.minimal_polynomial
import field_theory.splitting_field
/-!
# Separable polynomials
We define a polynomial to be separable if it is coprime with its derivative. We prove basic
properties about separable polynomials here.
## Main definitions
* `polynomial.separable f`: a polynomial `f` is separable iff it is coprime with its derivative.
* `polynomial.expand R p f`: expand the polynomial `f` with coefficients in a
commutative semiring `R` by a factor of p, so `expand R p (∑ aₙ xⁿ)` is `∑ aₙ xⁿᵖ`.
* `polynomial.contract p f`: the opposite of `expand`, so it sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`.
-/
universes u v w
open_locale classical big_operators
open finset
namespace polynomial
section comm_semiring
variables {R : Type u} [comm_semiring R] {S : Type v} [comm_semiring S]
/-- A polynomial is separable iff it is coprime with its derivative. -/
def separable (f : polynomial R) : Prop :=
is_coprime f f.derivative
lemma separable_def (f : polynomial R) :
f.separable ↔ is_coprime f f.derivative :=
iff.rfl
lemma separable_def' (f : polynomial R) :
f.separable ↔ ∃ a b : polynomial R, a * f + b * f.derivative = 1 :=
iff.rfl
lemma separable_one : (1 : polynomial R).separable :=
is_coprime_one_left
lemma separable_X_add_C (a : R) : (X + C a).separable :=
by { rw [separable_def, derivative_add, derivative_X, derivative_C, add_zero],
exact is_coprime_one_right }
lemma separable_X : (X : polynomial R).separable :=
by { rw [separable_def, derivative_X], exact is_coprime_one_right }
lemma separable_C (r : R) : (C r).separable ↔ is_unit r :=
by rw [separable_def, derivative_C, is_coprime_zero_right, is_unit_C]
lemma separable.of_mul_left {f g : polynomial R} (h : (f * g).separable) : f.separable :=
begin
have := h.of_mul_left_left, rw derivative_mul at this,
exact is_coprime.of_mul_right_left (is_coprime.of_add_mul_left_right this)
end
lemma separable.of_mul_right {f g : polynomial R} (h : (f * g).separable) : g.separable :=
by { rw mul_comm at h, exact h.of_mul_left }
lemma separable.of_dvd {f g : polynomial R} (hf : f.separable) (hfg : g ∣ f) : g.separable :=
by { rcases hfg with ⟨f', rfl⟩, exact separable.of_mul_left hf }
lemma separable.is_coprime {f g : polynomial R} (h : (f * g).separable) : is_coprime f g :=
begin
have := h.of_mul_left_left, rw derivative_mul at this,
exact is_coprime.of_mul_right_right (is_coprime.of_add_mul_left_right this)
end
theorem separable.of_pow' {f : polynomial R} :
∀ {n : ℕ} (h : (f ^ n).separable), is_unit f ∨ (f.separable ∧ n = 1) ∨ n = 0
| 0 := λ h, or.inr $ or.inr rfl
| 1 := λ h, or.inr $ or.inl ⟨pow_one f ▸ h, rfl⟩
| (n+2) := λ h, or.inl $ is_coprime_self.1 h.is_coprime.of_mul_right_left
theorem separable.of_pow {f : polynomial R} (hf : ¬is_unit f) {n : ℕ} (hn : n ≠ 0)
(hfs : (f ^ n).separable) : f.separable ∧ n = 1 :=
(hfs.of_pow'.resolve_left hf).resolve_right hn
theorem separable.map {p : polynomial R} (h : p.separable) {f : R →+* S} : (p.map f).separable :=
let ⟨a, b, H⟩ := h in ⟨a.map f, b.map f,
by rw [derivative_map, ← map_mul, ← map_mul, ← map_add, H, map_one]⟩
variables (R) (p q : ℕ)
/-- Expand the polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`. -/
noncomputable def expand : polynomial R →ₐ[R] polynomial R :=
{ commutes' := λ r, eval₂_C _ _,
.. (eval₂_ring_hom C (X ^ p) : polynomial R →+* polynomial R) }
lemma coe_expand : (expand R p : polynomial R → polynomial R) = eval₂ C (X ^ p) := rfl
variables {R}
lemma expand_eq_sum {f : polynomial R} :
expand R p f = f.sum (λ e a, C a * (X ^ p) ^ e) :=
by { dsimp [expand, eval₂], refl, }
@[simp] lemma expand_C (r : R) : expand R p (C r) = C r := eval₂_C _ _
@[simp] lemma expand_X : expand R p X = X ^ p := eval₂_X _ _
@[simp] lemma expand_monomial (r : R) : expand R p (monomial q r) = monomial (q * p) r :=
by simp_rw [monomial_eq_smul_X, alg_hom.map_smul, alg_hom.map_pow, expand_X, mul_comm, pow_mul]
theorem expand_expand (f : polynomial R) : expand R p (expand R q f) = expand R (p * q) f :=
polynomial.induction_on f (λ r, by simp_rw expand_C)
(λ f g ihf ihg, by simp_rw [alg_hom.map_add, ihf, ihg])
(λ n r ih, by simp_rw [alg_hom.map_mul, expand_C, alg_hom.map_pow, expand_X,
alg_hom.map_pow, expand_X, pow_mul])
theorem expand_mul (f : polynomial R) : expand R (p * q) f = expand R p (expand R q f) :=
(expand_expand p q f).symm
@[simp] theorem expand_one (f : polynomial R) : expand R 1 f = f :=
polynomial.induction_on f
(λ r, by rw expand_C)
(λ f g ihf ihg, by rw [alg_hom.map_add, ihf, ihg])
(λ n r ih, by rw [alg_hom.map_mul, expand_C, alg_hom.map_pow, expand_X, pow_one])
theorem expand_pow (f : polynomial R) : expand R (p ^ q) f = (expand R p ^[q] f) :=
nat.rec_on q (by rw [nat.pow_zero, expand_one, function.iterate_zero, id]) $ λ n ih,
by rw [function.iterate_succ_apply', nat.pow_succ, mul_comm, expand_mul, ih]
theorem derivative_expand (f : polynomial R) :
(expand R p f).derivative = expand R p f.derivative * (p * X ^ (p - 1)) :=
by rw [coe_expand, derivative_eval₂_C, derivative_pow, derivative_X, mul_one]
theorem coeff_expand {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) :
(expand R p f).coeff n = if p ∣ n then f.coeff (n / p) else 0 :=
begin
simp only [expand_eq_sum],
change (show ℕ →₀ R, from (f.sum (λ e a, C a * (X ^ p) ^ e) : polynomial R)) n = _,
simp_rw [finsupp.sum_apply, finsupp.sum, ← pow_mul, C_mul', ← monomial_eq_smul_X,
monomial, finsupp.single_apply],
split_ifs with h,
{ rw [finset.sum_eq_single (n/p), nat.mul_div_cancel' h, if_pos rfl], refl,
{ intros b hb1 hb2, rw if_neg, intro hb3, apply hb2, rw [← hb3, nat.mul_div_cancel_left b hp] },
{ intro hn, rw finsupp.not_mem_support_iff.1 hn, split_ifs; refl } },
{ rw finset.sum_eq_zero, intros k hk, rw if_neg, exact λ hkn, h ⟨k, hkn.symm⟩, },
end
@[simp] theorem coeff_expand_mul {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) :
(expand R p f).coeff (n * p) = f.coeff n :=
by rw [coeff_expand hp, if_pos (dvd_mul_left _ _), nat.mul_div_cancel _ hp]
@[simp] theorem coeff_expand_mul' {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) :
(expand R p f).coeff (p * n) = f.coeff n :=
by rw [mul_comm, coeff_expand_mul hp]
theorem expand_eq_map_domain (p : ℕ) (f : polynomial R) :
expand R p f = f.map_domain (*p) :=
finsupp.induction f (by { simp only [expand_eq_sum], refl }) $ λ n r f hf hr ih,
by rw [finsupp.map_domain_add, finsupp.map_domain_single, alg_hom.map_add, ← monomial,
expand_monomial, ← monomial, ih]
theorem expand_inj {p : ℕ} (hp : 0 < p) {f g : polynomial R} :
expand R p f = expand R p g ↔ f = g :=
⟨λ H, ext $ λ n, by rw [← coeff_expand_mul hp, H, coeff_expand_mul hp], congr_arg _⟩
theorem expand_eq_zero {p : ℕ} (hp : 0 < p) {f : polynomial R} : expand R p f = 0 ↔ f = 0 :=
by rw [← (expand R p).map_zero, expand_inj hp, alg_hom.map_zero]
theorem expand_eq_C {p : ℕ} (hp : 0 < p) {f : polynomial R} {r : R} :
expand R p f = C r ↔ f = C r :=
by rw [← expand_C, expand_inj hp, expand_C]
theorem nat_degree_expand (p : ℕ) (f : polynomial R) :
(expand R p f).nat_degree = f.nat_degree * p :=
begin
cases p.eq_zero_or_pos with hp hp,
{ rw [hp, coe_expand, pow_zero, mul_zero, ← C_1, eval₂_hom, nat_degree_C] },
by_cases hf : f = 0,
{ rw [hf, alg_hom.map_zero, nat_degree_zero, zero_mul] },
have hf1 : expand R p f ≠ 0 := mt (expand_eq_zero hp).1 hf,
rw [← with_bot.coe_eq_coe, ← degree_eq_nat_degree hf1],
refine le_antisymm ((degree_le_iff_coeff_zero _ _).2 $ λ n hn, _) _,
{ rw coeff_expand hp, split_ifs with hpn,
{ rw coeff_eq_zero_of_nat_degree_lt, contrapose! hn,
rw [with_bot.coe_le_coe, ← nat.div_mul_cancel hpn], exact nat.mul_le_mul_right p hn },
{ refl } },
{ refine le_degree_of_ne_zero _,
rw [coeff_expand_mul hp, ← leading_coeff], exact mt leading_coeff_eq_zero.1 hf }
end
theorem map_expand {p : ℕ} (hp : 0 < p) {f : R →+* S} {q : polynomial R} :
map f (expand R p q) = expand S p (map f q) :=
by { ext, rw [coeff_map, coeff_expand hp, coeff_expand hp], split_ifs; simp, }
end comm_semiring
section comm_ring
variables {R : Type u} [comm_ring R]
lemma separable_X_sub_C {x : R} : separable (X - C x) :=
by simpa only [C_neg] using separable_X_add_C (-x)
lemma separable.mul {f g : polynomial R} (hf : f.separable) (hg : g.separable)
(h : is_coprime f g) : (f * g).separable :=
by { rw [separable_def, derivative_mul], exact ((hf.mul_right h).add_mul_left_right _).mul_left
((h.symm.mul_right hg).mul_add_right_right _) }
lemma separable_prod' {ι : Sort*} {f : ι → polynomial R} {s : finset ι} :
(∀x∈s, ∀y∈s, x ≠ y → is_coprime (f x) (f y)) → (∀x∈s, (f x).separable) → (∏ x in s, f x).separable :=
finset.induction_on s (λ _ _, separable_one) $ λ a s has ih h1 h2, begin
simp_rw [finset.forall_mem_insert, forall_and_distrib] at h1 h2, rw prod_insert has,
exact h2.1.mul (ih h1.2.2 h2.2) (is_coprime.prod_right $ λ i his, h1.1.2 i his $
ne.symm $ ne_of_mem_of_not_mem his has)
end
lemma separable_prod {ι : Sort*} [fintype ι] {f : ι → polynomial R}
(h1 : pairwise (is_coprime on f)) (h2 : ∀ x, (f x).separable) : (∏ x, f x).separable :=
separable_prod' (λ x hx y hy hxy, h1 x y hxy) (λ x hx, h2 x)
lemma separable.inj_of_prod_X_sub_C [nontrivial R] {ι : Sort*} {f : ι → R} {s : finset ι}
(hfs : (∏ i in s, (X - C (f i))).separable)
{x y : ι} (hx : x ∈ s) (hy : y ∈ s) (hfxy : f x = f y) : x = y :=
begin
by_contra hxy,
rw [← insert_erase hx, prod_insert (not_mem_erase _ _),
← insert_erase (mem_erase_of_ne_of_mem (ne.symm hxy) hy),
prod_insert (not_mem_erase _ _), ← mul_assoc, hfxy, ← pow_two] at hfs,
cases (hfs.of_mul_left.of_pow (by exact not_is_unit_X_sub_C) two_ne_zero).2
end
lemma separable.injective_of_prod_X_sub_C [nontrivial R] {ι : Sort*} [fintype ι] {f : ι → R}
(hfs : (∏ i, (X - C (f i))).separable) : function.injective f :=
λ x y hfxy, hfs.inj_of_prod_X_sub_C (mem_univ _) (mem_univ _) hfxy
lemma is_unit_of_self_mul_dvd_separable {p q : polynomial R}
(hp : p.separable) (hq : q * q ∣ p) : is_unit q :=
begin
obtain ⟨p, rfl⟩ := hq,
apply is_coprime_self.mp,
have : is_coprime (q * (q * p)) (q * (q.derivative * p + q.derivative * p + q * p.derivative)),
{ simp only [← mul_assoc, mul_add],
convert hp,
rw [derivative_mul, derivative_mul],
ring },
exact is_coprime.of_mul_right_left (is_coprime.of_mul_left_left this)
end
end comm_ring
section integral_domain
variables (R : Type u) [integral_domain R]
theorem is_local_ring_hom_expand {p : ℕ} (hp : 0 < p) :
is_local_ring_hom (↑(expand R p) : polynomial R →+* polynomial R) :=
begin
refine ⟨λ f hf1, _⟩, rw ← coe_fn_coe_base at hf1,
have hf2 := eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit hf1),
rw [coeff_expand hp, if_pos (dvd_zero _), p.zero_div] at hf2,
rw [hf2, is_unit_C] at hf1, rw expand_eq_C hp at hf2, rwa [hf2, is_unit_C]
end
end integral_domain
section field
variables {F : Type u} [field F] {K : Type v} [field K]
theorem separable_iff_derivative_ne_zero {f : polynomial F} (hf : irreducible f) :
f.separable ↔ f.derivative ≠ 0 :=
⟨λ h1 h2, hf.1 $ is_coprime_zero_right.1 $ h2 ▸ h1,
λ h, is_coprime_of_dvd (mt and.right h) $ λ g hg1 hg2 ⟨p, hg3⟩ hg4,
let ⟨u, hu⟩ := (hf.2 _ _ hg3).resolve_left hg1 in
have f ∣ f.derivative, by { conv_lhs { rw [hg3, ← hu] }, rwa units.mul_right_dvd },
not_lt_of_le (nat_degree_le_of_dvd this h) $ nat_degree_derivative_lt h⟩
theorem separable_map (f : F →+* K) {p : polynomial F} : (p.map f).separable ↔ p.separable :=
by simp_rw [separable_def, derivative_map, is_coprime_map]
section char_p
variables (p : ℕ) [hp : fact p.prime]
include hp
/-- The opposite of `expand`: sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`. -/
noncomputable def contract (f : polynomial F) : polynomial F :=
⟨f.support.preimage (*p) $ λ _ _ _ _, (nat.mul_left_inj hp.pos).1,
λ n, f.coeff (n * p),
λ n, by { rw [finset.mem_preimage, finsupp.mem_support_iff], refl }⟩
theorem coeff_contract (f : polynomial F) (n : ℕ) : (contract p f).coeff n = f.coeff (n * p) := rfl
theorem of_irreducible_expand {f : polynomial F} (hf : irreducible (expand F p f)) :
irreducible f :=
@@of_irreducible_map _ _ _ (is_local_ring_hom_expand F hp.pos) hf
theorem of_irreducible_expand_pow {f : polynomial F} {n : ℕ} :
irreducible (expand F (p ^ n) f) → irreducible f :=
nat.rec_on n (λ hf, by rwa [nat.pow_zero, expand_one] at hf) $ λ n ih hf,
ih $ of_irreducible_expand p $ by rwa [expand_expand, mul_comm]
variables [HF : char_p F p]
include HF
theorem expand_char (f : polynomial F) :
map (frobenius F p) (expand F p f) = f ^ p :=
begin
refine f.induction_on' (λ a b ha hb, _) (λ n a, _),
{ rw [alg_hom.map_add, map_add, ha, hb, add_pow_char], },
{ rw [expand_monomial, map_monomial, single_eq_C_mul_X, single_eq_C_mul_X,
mul_pow, ← C.map_pow, frobenius_def],
ring_exp }
end
theorem map_expand_pow_char (f : polynomial F) (n : ℕ) :
map ((frobenius F p) ^ n) (expand F (p ^ n) f) = f ^ (p ^ n) :=
begin
induction n, {simp [ring_hom.one_def]},
symmetry,
rw [nat.pow_succ, pow_mul, ← n_ih, ← expand_char, pow_succ, ring_hom.mul_def, ← map_map, mul_comm,
expand_mul, ← map_expand (nat.prime.pos hp)],
end
theorem expand_contract {f : polynomial F} (hf : f.derivative = 0) :
expand F p (contract p f) = f :=
begin
ext n, rw [coeff_expand hp.pos, coeff_contract], split_ifs with h,
{ rw nat.div_mul_cancel h },
{ cases n, { exact absurd (dvd_zero p) h },
have := coeff_derivative f n, rw [hf, coeff_zero, zero_eq_mul] at this, cases this, { rw this },
rw [← nat.cast_succ, char_p.cast_eq_zero_iff F p] at this,
exact absurd this h }
end
theorem separable_or {f : polynomial F} (hf : irreducible f) : f.separable ∨
¬f.separable ∧ ∃ g : polynomial F, irreducible g ∧ expand F p g = f :=
if H : f.derivative = 0 then or.inr
⟨by rw [separable_iff_derivative_ne_zero hf, not_not, H],
contract p f,
by haveI := is_local_ring_hom_expand F hp.pos; exact
of_irreducible_map ↑(expand F p) (by rwa ← expand_contract p H at hf),
expand_contract p H⟩
else or.inl $ (separable_iff_derivative_ne_zero hf).2 H
theorem exists_separable_of_irreducible {f : polynomial F} (hf : irreducible f) (hf0 : f ≠ 0) :
∃ (n : ℕ) (g : polynomial F), g.separable ∧ expand F (p ^ n) g = f :=
begin
generalize hn : f.nat_degree = N, unfreezingI { revert f },
apply nat.strong_induction_on N, intros N ih f hf hf0 hn,
rcases separable_or p hf with h | ⟨h1, g, hg, hgf⟩,
{ refine ⟨0, f, h, _⟩, rw [nat.pow_zero, expand_one] },
{ cases N with N,
{ rw [nat_degree_eq_zero_iff_degree_le_zero, degree_le_zero_iff] at hn,
rw [hn, separable_C, is_unit_iff_ne_zero, not_not] at h1,
rw [h1, C_0] at hn, exact absurd hn hf0 },
have hg1 : g.nat_degree * p = N.succ,
{ rwa [← nat_degree_expand, hgf] },
have hg2 : g.nat_degree ≠ 0,
{ intro this, rw [this, zero_mul] at hg1, cases hg1 },
have hg3 : g.nat_degree < N.succ,
{ rw [← mul_one g.nat_degree, ← hg1],
exact nat.mul_lt_mul_of_pos_left hp.one_lt (nat.pos_of_ne_zero hg2) },
have hg4 : g ≠ 0,
{ rintro rfl, exact hg2 nat_degree_zero },
rcases ih _ hg3 hg hg4 rfl with ⟨n, g, hg5, rfl⟩, refine ⟨n+1, g, hg5, _⟩,
rw [← hgf, expand_expand, nat.pow_succ, mul_comm] }
end
theorem is_unit_or_eq_zero_of_separable_expand {f : polynomial F} (n : ℕ)
(hf : (expand F (p ^ n) f).separable) : is_unit f ∨ n = 0 :=
begin
rw or_iff_not_imp_right, intro hn,
have hf2 : (expand F (p ^ n) f).derivative = 0,
{ by rw [derivative_expand, nat.cast_pow, char_p.cast_eq_zero,
zero_pow (nat.pos_of_ne_zero hn), zero_mul, mul_zero] },
rw [separable_def, hf2, is_coprime_zero_right, is_unit_iff] at hf, rcases hf with ⟨r, hr, hrf⟩,
rw [eq_comm, expand_eq_C (nat.pow_pos hp.pos _)] at hrf,
rwa [hrf, is_unit_C]
end
theorem unique_separable_of_irreducible {f : polynomial F} (hf : irreducible f) (hf0 : f ≠ 0)
(n₁ : ℕ) (g₁ : polynomial F) (hg₁ : g₁.separable) (hgf₁ : expand F (p ^ n₁) g₁ = f)
(n₂ : ℕ) (g₂ : polynomial F) (hg₂ : g₂.separable) (hgf₂ : expand F (p ^ n₂) g₂ = f) :
n₁ = n₂ ∧ g₁ = g₂ :=
begin
revert g₁ g₂, wlog hn : n₁ ≤ n₂ := le_total n₁ n₂ using [n₁ n₂, n₂ n₁] tactic.skip,
unfreezingI { intros, rw le_iff_exists_add at hn, rcases hn with ⟨k, rfl⟩,
rw [← hgf₁, nat.pow_add, expand_mul, expand_inj (nat.pow_pos hp.pos n₁)] at hgf₂, subst hgf₂,
subst hgf₁,
rcases is_unit_or_eq_zero_of_separable_expand p k hg₁ with h | rfl,
{ rw is_unit_iff at h, rcases h with ⟨r, hr, rfl⟩,
simp_rw expand_C at hf, exact absurd (is_unit_C.2 hr) hf.1 },
{ rw [add_zero, nat.pow_zero, expand_one], split; refl } },
exact λ g₁ g₂ hg₁ hgf₁ hg₂ hgf₂, let ⟨hn, hg⟩ := this g₂ g₁ hg₂ hgf₂ hg₁ hgf₁ in ⟨hn.symm, hg.symm⟩
end
end char_p
lemma separable_prod_X_sub_C_iff' {ι : Sort*} {f : ι → F} {s : finset ι} :
(∏ i in s, (X - C (f i))).separable ↔ (∀ (x ∈ s) (y ∈ s), f x = f y → x = y) :=
⟨λ hfs x hx y hy hfxy, hfs.inj_of_prod_X_sub_C hx hy hfxy,
λ H, by { rw ← prod_attach, exact separable_prod' (λ x hx y hy hxy,
@pairwise_coprime_X_sub _ _ { x // x ∈ s } (λ x, f x)
(λ x y hxy, subtype.eq $ H x.1 x.2 y.1 y.2 hxy) _ _ hxy)
(λ _ _, separable_X_sub_C) }⟩
lemma separable_prod_X_sub_C_iff {ι : Sort*} [fintype ι] {f : ι → F} :
(∏ i, (X - C (f i))).separable ↔ function.injective f :=
separable_prod_X_sub_C_iff'.trans $ by simp_rw [mem_univ, true_implies_iff]
section splits
open_locale big_operators
variables {i : F →+* K}
lemma not_unit_X_sub_C (a : F) : ¬ is_unit (X - C a) :=
λ h, have one_eq_zero : (1 : with_bot ℕ) = 0, by simpa using degree_eq_zero_of_is_unit h,
one_ne_zero (option.some_injective _ one_eq_zero)
lemma nodup_of_separable_prod {s : multiset F}
(hs : separable (multiset.map (λ a, X - C a) s).prod) : s.nodup :=
begin
rw multiset.nodup_iff_ne_cons_cons,
rintros a t rfl,
refine not_unit_X_sub_C a (is_unit_of_self_mul_dvd_separable hs _),
simpa only [multiset.map_cons, multiset.prod_cons] using mul_dvd_mul_left _ (dvd_mul_right _ _)
end
lemma eq_prod_roots_of_separable {p : polynomial F} {i : F →+* K}
(hsep : separable p) (hsplit : splits i p) :
p.map i = C (i p.leading_coeff) * ∏ a in roots (p.map i), (X - C a : polynomial K) :=
begin
by_cases p_eq_zero : p = 0,
{ rw [p_eq_zero, map_zero, leading_coeff_zero, i.map_zero, C.map_zero, zero_mul] },
obtain ⟨s, hs⟩ := exists_multiset_of_splits i hsplit,
have map_ne_zero : p.map i ≠ 0 := map_ne_zero (p_eq_zero),
have prod_ne_zero : C (i p.leading_coeff) * (multiset.map (λ a, X - C a) s).prod ≠ 0 :=
by rwa hs at map_ne_zero,
have map_sep : separable (map i p) := (separable_map i).mpr hsep,
rw hs at map_sep,
have nodup_s : s.nodup := nodup_of_separable_prod (separable.of_mul_right map_sep),
have nodup_map_s : (s.map (λ a, X - C a)).nodup :=
multiset.nodup_map (λ a b h, C_inj.mp (sub_right_inj.mp h)) nodup_s,
have ne_zero_of_mem : ∀ (p : polynomial K), p ∈ s.map (λ a, X - C a) → p ≠ 0,
{ intros p mem,
obtain ⟨a, _, rfl⟩ := multiset.mem_map.mp mem,
apply X_sub_C_ne_zero },
have map_bind_roots_eq : (s.map (λ a, X - C a)).bind (λ a, a.roots.val) = s,
{ refine multiset.induction_on s (by rw [multiset.map_zero, multiset.zero_bind]) _,
intros a s ih,
rw [multiset.map_cons, multiset.cons_bind, ih, roots_X_sub_C, singleton_val,
multiset.cons_add, zero_add] },
rw [hs, finset.prod_eq_multiset_prod, roots_mul prod_ne_zero, roots_C, empty_union,
roots_multiset_prod _ ne_zero_of_mem, ← multiset.to_finset_eq nodup_map_s,
bind_val, map_bind_roots_eq, multiset.erase_dup_eq_self.mpr nodup_s],
end
lemma nat_degree_separable_eq_card_roots {p : polynomial F} {i : F →+* K}
(hsep : separable p) (hsplit : splits i p) : p.nat_degree = (p.map i).roots.card :=
begin
by_cases p_eq_zero : p = 0,
{ rw [p_eq_zero, nat_degree_zero, map_zero, roots_zero, card_empty] },
have map_ne_zero : p.map i ≠ 0 := map_ne_zero (p_eq_zero),
rw eq_prod_roots_of_separable hsep hsplit at map_ne_zero,
conv_lhs { rw [← nat_degree_map i, eq_prod_roots_of_separable hsep hsplit] },
simp [nat_degree_mul (left_ne_zero_of_mul map_ne_zero) (right_ne_zero_of_mul map_ne_zero),
nat_degree_prod (roots (p.map i)) (λ a, X - C a) (λ a _, X_sub_C_ne_zero a)]
end
lemma degree_separable_eq_card_roots {p : polynomial F} {i : F →+* K} (p_ne_zero : p ≠ 0)
(hsep : separable p) (hsplit : splits i p) : p.degree = (p.map i).roots.card :=
by rw [degree_eq_nat_degree p_ne_zero, nat_degree_separable_eq_card_roots hsep hsplit]
end splits
end field
end polynomial
open polynomial
theorem irreducible.separable {F : Type u} [field F] [char_zero F] {f : polynomial F}
(hf : irreducible f) (hf0 : f ≠ 0) : f.separable :=
begin
rw [separable_iff_derivative_ne_zero hf, ne, ← degree_eq_bot, degree_derivative_eq], rintro ⟨⟩,
rw [nat.pos_iff_ne_zero, ne, nat_degree_eq_zero_iff_degree_le_zero, degree_le_zero_iff],
refine λ hf1, hf.1 _, rw [hf1, is_unit_C, is_unit_iff_ne_zero],
intro hf2, rw [hf2, C_0] at hf1, exact absurd hf1 hf0
end
/-- Typeclass for separable field extension: `K` is a separable field extension of `F` iff
the minimal polynomial of every `x : K` is separable. -/
@[class] def is_separable (F K : Sort*) [field F] [field K] [algebra F K] : Prop :=
∀ x : K, ∃ H : is_integral F x, (minimal_polynomial H).separable
|
96f06dd75531c5835f6c54641254c83f8bb7d97d | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /tests/lean/resolveGlobalName.lean | 267d9531f653672b470dd72c1cf667e06941e501 | [
"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 | 580 | lean | import Lean
def Boo.x := 1
def Foo.x := 2
def Foo.x.y := 3
def Bla.x := 4
namespace Test
export Bla (x)
end Test
open Lean
open Lean.Elab.Term
open Lean.Elab.Command
syntax (name := resolveKind) "#resolve " ident : command
@[commandElab resolveKind] def elabResolve : CommandElab :=
fun stx => liftTermElabM do
let cs ← resolveGlobalName $ stx.getIdAt 1;
Lean.logInfo $ toString cs;
pure ()
#resolve x.y
#resolve x
open Foo
#resolve x
#resolve x.y
#resolve x.z.w
open Boo
#resolve x
#resolve x.y
#resolve x.z.w
open Test
#resolve x
#resolve x.w.h.r
#resolve x.y
|
b89cd6be1b42043a4214c7fa1be74d92b69fb26a | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/data/equiv/encodable/basic.lean | bb123591557386488e269b0ee659b40aab6e1651 | [
"Apache-2.0"
] | permissive | dupuisf/mathlib | 62de4ec6544bf3b79086afd27b6529acfaf2c1bb | 8582b06b0a5d06c33ee07d0bdf7c646cae22cf36 | refs/heads/master | 1,669,494,854,016 | 1,595,692,409,000 | 1,595,692,409,000 | 272,046,630 | 0 | 0 | Apache-2.0 | 1,592,066,143,000 | 1,592,066,142,000 | null | UTF-8 | Lean | false | false | 14,745 | 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, Mario Carneiro
Type class for encodable Types.
Note that every encodable Type is countable.
-/
import data.equiv.nat
import order.order_iso
import order.directed
open option list nat function
/-- An encodable type is a "constructively countable" type. This is where
we have an explicit injection `encode : α → nat` and a partial inverse
`decode : nat → option α`. This makes the range of `encode` decidable,
although it is not decidable if `α` is finite or not. -/
class encodable (α : Type*) :=
(encode : α → nat)
(decode [] : nat → option α)
(encodek : ∀ a, decode (encode a) = some a)
attribute [simp] encodable.encodek
namespace encodable
variables {α : Type*} {β : Type*}
universe u
open encodable
theorem encode_injective [encodable α] : function.injective (@encode α _)
| x y e := option.some.inj $ by rw [← encodek, e, encodek]
/- This is not set as an instance because this is usually not the best way
to infer decidability. -/
def decidable_eq_of_encodable (α) [encodable α] : decidable_eq α
| a b := decidable_of_iff _ encode_injective.eq_iff
def of_left_injection [encodable α]
(f : β → α) (finv : α → option β) (linv : ∀ b, finv (f b) = some b) : encodable β :=
⟨λ b, encode (f b),
λ n, (decode α n).bind finv,
λ b, by simp [encodable.encodek, linv]⟩
def of_left_inverse [encodable α]
(f : β → α) (finv : α → β) (linv : ∀ b, finv (f b) = b) : encodable β :=
of_left_injection f (some ∘ finv) (λ b, congr_arg some (linv b))
/-- If `α` is encodable and `β ≃ α`, then so is `β` -/
def of_equiv (α) [encodable α] (e : β ≃ α) : encodable β :=
of_left_inverse e e.symm e.left_inv
@[simp] theorem encode_of_equiv {α β} [encodable α] (e : β ≃ α) (b : β) :
@encode _ (of_equiv _ e) b = encode (e b) := rfl
@[simp] theorem decode_of_equiv {α β} [encodable α] (e : β ≃ α) (n : ℕ) :
@decode _ (of_equiv _ e) n = (decode α n).map e.symm := rfl
instance nat : encodable nat :=
⟨id, some, λ a, rfl⟩
@[simp] theorem encode_nat (n : ℕ) : encode n = n := rfl
@[simp] theorem decode_nat (n : ℕ) : decode ℕ n = some n := rfl
instance empty : encodable empty :=
⟨λ a, a.rec _, λ n, none, λ a, a.rec _⟩
instance unit : encodable punit :=
⟨λ_, zero, λn, nat.cases_on n (some punit.star) (λ _, none), λ⟨⟩, by simp⟩
@[simp] theorem encode_star : encode punit.star = 0 := rfl
@[simp] theorem decode_unit_zero : decode punit 0 = some punit.star := rfl
@[simp] theorem decode_unit_succ (n) : decode punit (succ n) = none := rfl
instance option {α : Type*} [h : encodable α] : encodable (option α) :=
⟨λ o, option.cases_on o nat.zero (λ a, succ (encode a)),
λ n, nat.cases_on n (some none) (λ m, (decode α m).map some),
λ o, by cases o; dsimp; simp [encodek, nat.succ_ne_zero]⟩
@[simp] theorem encode_none [encodable α] : encode (@none α) = 0 := rfl
@[simp] theorem encode_some [encodable α] (a : α) :
encode (some a) = succ (encode a) := rfl
@[simp] theorem decode_option_zero [encodable α] : decode (option α) 0 = some none := rfl
@[simp] theorem decode_option_succ [encodable α] (n) :
decode (option α) (succ n) = (decode α n).map some := rfl
def decode2 (α) [encodable α] (n : ℕ) : option α :=
(decode α n).bind (option.guard (λ a, encode a = n))
theorem mem_decode2' [encodable α] {n : ℕ} {a : α} :
a ∈ decode2 α n ↔ a ∈ decode α n ∧ encode a = n :=
by simp [decode2]; exact
⟨λ ⟨_, h₁, rfl, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨_, h₁, rfl, h₂⟩⟩
theorem mem_decode2 [encodable α] {n : ℕ} {a : α} :
a ∈ decode2 α n ↔ encode a = n :=
mem_decode2'.trans (and_iff_right_of_imp $ λ e, e ▸ encodek _)
theorem decode2_is_partial_inv [encodable α] : is_partial_inv encode (decode2 α) :=
λ a n, mem_decode2
theorem decode2_inj [encodable α] {n : ℕ} {a₁ a₂ : α}
(h₁ : a₁ ∈ decode2 α n) (h₂ : a₂ ∈ decode2 α n) : a₁ = a₂ :=
encode_injective $ (mem_decode2.1 h₁).trans (mem_decode2.1 h₂).symm
theorem encodek2 [encodable α] (a : α) : decode2 α (encode a) = some a :=
mem_decode2.2 rfl
def decidable_range_encode (α : Type*) [encodable α] : decidable_pred (set.range (@encode α _)) :=
λ x, decidable_of_iff (option.is_some (decode2 α x))
⟨λ h, ⟨option.get h, by rw [← decode2_is_partial_inv (option.get h), option.some_get]⟩,
λ ⟨n, hn⟩, by rw [← hn, encodek2]; exact rfl⟩
def equiv_range_encode (α : Type*) [encodable α] : α ≃ set.range (@encode α _) :=
{ to_fun := λ a : α, ⟨encode a, set.mem_range_self _⟩,
inv_fun := λ n, option.get (show is_some (decode2 α n.1),
by cases n.2 with x hx; rw [← hx, encodek2]; exact rfl),
left_inv := λ a, by dsimp;
rw [← option.some_inj, option.some_get, encodek2],
right_inv := λ ⟨n, x, hx⟩, begin
apply subtype.eq,
dsimp,
conv {to_rhs, rw ← hx},
rw [encode_injective.eq_iff, ← option.some_inj, option.some_get, ← hx, encodek2],
end }
section sum
variables [encodable α] [encodable β]
def encode_sum : α ⊕ β → nat
| (sum.inl a) := bit0 $ encode a
| (sum.inr b) := bit1 $ encode b
def decode_sum (n : nat) : option (α ⊕ β) :=
match bodd_div2 n with
| (ff, m) := (decode α m).map sum.inl
| (tt, m) := (decode β m).map sum.inr
end
instance sum : encodable (α ⊕ β) :=
⟨encode_sum, decode_sum, λ s,
by cases s; simp [encode_sum, decode_sum, encodek]; refl⟩
@[simp] theorem encode_inl (a : α) :
@encode (α ⊕ β) _ (sum.inl a) = bit0 (encode a) := rfl
@[simp] theorem encode_inr (b : β) :
@encode (α ⊕ β) _ (sum.inr b) = bit1 (encode b) := rfl
@[simp] theorem decode_sum_val (n : ℕ) :
decode (α ⊕ β) n = decode_sum n := rfl
end sum
instance bool : encodable bool :=
of_equiv (unit ⊕ unit) equiv.bool_equiv_punit_sum_punit
@[simp] theorem encode_tt : encode tt = 1 := rfl
@[simp] theorem encode_ff : encode ff = 0 := rfl
@[simp] theorem decode_zero : decode bool 0 = some ff := rfl
@[simp] theorem decode_one : decode bool 1 = some tt := rfl
theorem decode_ge_two (n) (h : 2 ≤ n) : decode bool n = none :=
begin
suffices : decode_sum n = none,
{ change (decode_sum n).map _ = none, rw this, refl },
have : 1 ≤ div2 n,
{ rw [div2_val, nat.le_div_iff_mul_le],
exacts [h, dec_trivial] },
cases exists_eq_succ_of_ne_zero (ne_of_gt this) with m e,
simp [decode_sum]; cases bodd n; simp [decode_sum]; rw e; refl
end
section sigma
variables {γ : α → Type*} [encodable α] [∀ a, encodable (γ a)]
def encode_sigma : sigma γ → ℕ
| ⟨a, b⟩ := mkpair (encode a) (encode b)
def decode_sigma (n : ℕ) : option (sigma γ) :=
let (n₁, n₂) := unpair n in
(decode α n₁).bind $ λ a, (decode (γ a) n₂).map $ sigma.mk a
instance sigma : encodable (sigma γ) :=
⟨encode_sigma, decode_sigma, λ ⟨a, b⟩,
by simp [encode_sigma, decode_sigma, unpair_mkpair, encodek]⟩
@[simp] theorem decode_sigma_val (n : ℕ) : decode (sigma γ) n =
(decode α n.unpair.1).bind (λ a, (decode (γ a) n.unpair.2).map $ sigma.mk a) :=
show decode_sigma._match_1 _ = _, by cases n.unpair; refl
@[simp] theorem encode_sigma_val (a b) : @encode (sigma γ) _ ⟨a, b⟩ =
mkpair (encode a) (encode b) := rfl
end sigma
section prod
variables [encodable α] [encodable β]
instance prod : encodable (α × β) :=
of_equiv _ (equiv.sigma_equiv_prod α β).symm
@[simp] theorem decode_prod_val (n : ℕ) : decode (α × β) n =
(decode α n.unpair.1).bind (λ a, (decode β n.unpair.2).map $ prod.mk a) :=
show (decode (sigma (λ _, β)) n).map (equiv.sigma_equiv_prod α β) = _,
by simp; cases decode α n.unpair.1; simp;
cases decode β n.unpair.2; refl
@[simp] theorem encode_prod_val (a b) : @encode (α × β) _ (a, b) =
mkpair (encode a) (encode b) := rfl
end prod
section subtype
open subtype decidable
variable {P : α → Prop}
variable [encA : encodable α]
variable [decP : decidable_pred P]
include encA
def encode_subtype : {a : α // P a} → nat
| ⟨v, h⟩ := encode v
include decP
def decode_subtype (v : nat) : option {a : α // P a} :=
(decode α v).bind $ λ a,
if h : P a then some ⟨a, h⟩ else none
instance subtype : encodable {a : α // P a} :=
⟨encode_subtype, decode_subtype,
λ ⟨v, h⟩, by simp [encode_subtype, decode_subtype, encodek, h]⟩
lemma subtype.encode_eq (a : subtype P) : encode a = encode a.val :=
by cases a; refl
end subtype
instance fin (n) : encodable (fin n) :=
of_equiv _ (equiv.fin_equiv_subtype _)
instance int : encodable ℤ :=
of_equiv _ equiv.int_equiv_nat
instance ulift [encodable α] : encodable (ulift α) :=
of_equiv _ equiv.ulift
instance plift [encodable α] : encodable (plift α) :=
of_equiv _ equiv.plift
noncomputable def of_inj [encodable β] (f : α → β) (hf : injective f) : encodable α :=
of_left_injection f (partial_inv f) (λ x, (partial_inv_of_injective hf _ _).2 rfl)
end encodable
section ulower
local attribute [instance, priority 100] encodable.decidable_range_encode
/--
`ulower α : Type 0` is an equivalent type in the lowest universe, given `encodable α`.
-/
@[derive decidable_eq, derive encodable]
def ulower (α : Type*) [encodable α] : Type :=
set.range (encodable.encode : α → ℕ)
end ulower
namespace ulower
variables (α : Type*) [encodable α]
/--
The equivalence between the encodable type `α` and `ulower α : Type 0`.
-/
def equiv : α ≃ ulower α :=
encodable.equiv_range_encode α
variables {α}
/--
Lowers an `a : α` into `ulower α`.
-/
def down (a : α) : ulower α := equiv α a
instance [inhabited α] : inhabited (ulower α) := ⟨down (default _)⟩
/--
Lifts an `a : ulower α` into `α`.
-/
def up (a : ulower α) : α := (equiv α).symm a
@[simp] lemma down_up {a : ulower α} : down a.up = a := equiv.right_inv _ _
@[simp] lemma up_down {a : α} : (down a).up = a := equiv.left_inv _ _
@[simp] lemma up_eq_up {a b : ulower α} : a.up = b.up ↔ a = b :=
equiv.apply_eq_iff_eq _ _ _
@[simp] lemma down_eq_down {a b : α} : down a = down b ↔ a = b :=
equiv.apply_eq_iff_eq _ _ _
@[ext] protected lemma ext {a b : ulower α} : a.up = b.up → a = b :=
up_eq_up.1
end ulower
/-
Choice function for encodable types and decidable predicates.
We provide the following API
choose {α : Type*} {p : α → Prop} [c : encodable α] [d : decidable_pred p] : (∃ x, p x) → α :=
choose_spec {α : Type*} {p : α → Prop} [c : encodable α] [d : decidable_pred p] (ex : ∃ x, p x) :
p (choose ex) :=
-/
namespace encodable
section find_a
variables {α : Type*} (p : α → Prop) [encodable α] [decidable_pred p]
private def good : option α → Prop
| (some a) := p a
| none := false
private def decidable_good : decidable_pred (good p)
| n := by cases n; unfold good; apply_instance
local attribute [instance] decidable_good
open encodable
variable {p}
def choose_x (h : ∃ x, p x) : {a:α // p a} :=
have ∃ n, good p (decode α n), from
let ⟨w, pw⟩ := h in ⟨encode w, by simp [good, encodek, pw]⟩,
match _, nat.find_spec this : ∀ o, good p o → {a // p a} with
| some a, h := ⟨a, h⟩
end
def choose (h : ∃ x, p x) : α := (choose_x h).1
lemma choose_spec (h : ∃ x, p x) : p (choose h) := (choose_x h).2
end find_a
theorem axiom_of_choice {α : Type*} {β : α → Type*} {R : Π x, β x → Prop}
[Π a, encodable (β a)] [∀ x y, decidable (R x y)]
(H : ∀x, ∃y, R x y) : ∃f:Πa, β a, ∀x, R x (f x) :=
⟨λ x, choose (H x), λ x, choose_spec (H x)⟩
theorem skolem {α : Type*} {β : α → Type*} {P : Π x, β x → Prop}
[c : Π a, encodable (β a)] [d : ∀ x y, decidable (P x y)] :
(∀x, ∃y, P x y) ↔ ∃f : Π a, β a, (∀x, P x (f x)) :=
⟨axiom_of_choice, λ ⟨f, H⟩ x, ⟨_, H x⟩⟩
/-
There is a total ordering on the elements of an encodable type, induced by the map to ℕ.
-/
/-- The `encode` function, viewed as an embedding. -/
def encode' (α) [encodable α] : α ↪ nat :=
⟨encodable.encode, encodable.encode_injective⟩
instance {α} [encodable α] : is_trans _ (encode' α ⁻¹'o (≤)) :=
(order_embedding.preimage _ _).is_trans
instance {α} [encodable α] : is_antisymm _ (encodable.encode' α ⁻¹'o (≤)) :=
(order_embedding.preimage _ _).is_antisymm
instance {α} [encodable α] : is_total _ (encodable.encode' α ⁻¹'o (≤)) :=
(order_embedding.preimage _ _).is_total
end encodable
namespace directed
open encodable
variables {α : Type*} {β : Type*} [encodable α] [inhabited α]
/-- Given a `directed r` function `f : α → β` defined on an encodable inhabited type,
construct a noncomputable sequence such that `r (f (x n)) (f (x (n + 1)))`
and `r (f a) (f (x (encode a + 1))`. -/
protected noncomputable def sequence {r : β → β → Prop} (f : α → β) (hf : directed r f) : ℕ → α
| 0 := default α
| (n + 1) :=
let p := sequence n in
match decode α n with
| none := classical.some (hf p p)
| (some a) := classical.some (hf p a)
end
lemma sequence_mono_nat {r : β → β → Prop} {f : α → β} (hf : directed r f) (n : ℕ) :
r (f (hf.sequence f n)) (f (hf.sequence f (n+1))) :=
begin
dsimp [directed.sequence],
generalize eq : hf.sequence f n = p,
cases h : decode α n with a,
{ exact (classical.some_spec (hf p p)).1 },
{ exact (classical.some_spec (hf p a)).1 }
end
lemma rel_sequence {r : β → β → Prop} {f : α → β} (hf : directed r f) (a : α) :
r (f a) (f (hf.sequence f (encode a + 1))) :=
begin
simp only [directed.sequence, encodek],
exact (classical.some_spec (hf _ a)).2
end
variables [preorder β] {f : α → β} (hf : directed (≤) f)
lemma sequence_mono : monotone (f ∘ (hf.sequence f)) :=
monotone_of_monotone_nat $ hf.sequence_mono_nat
lemma le_sequence (a : α) : f a ≤ f (hf.sequence f (encode a + 1)) :=
hf.rel_sequence a
end directed
section quotient
open encodable quotient
variables {α : Type*} {s : setoid α} [@decidable_rel α (≈)] [encodable α]
/-- Representative of an equivalence class. This is a computable version of `quot.out` for a setoid
on an encodable type. -/
def quotient.rep (q : quotient s) : α :=
choose (exists_rep q)
theorem quotient.rep_spec (q : quotient s) : ⟦q.rep⟧ = q :=
choose_spec (exists_rep q)
/-- The quotient of an encodable space by a decidable equivalence relation is encodable. -/
def encodable_quotient : encodable (quotient s) :=
⟨λ q, encode q.rep,
λ n, quotient.mk <$> decode α n,
by rintros ⟨l⟩; rw encodek; exact congr_arg some ⟦l⟧.rep_spec⟩
end quotient
|
49f84bc810e30f18787e1be3140e5c1af190d3dc | 57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d | /src/Lean/Compiler/IR/Borrow.lean | 3f1c88ab657f53ebe66217167da20db4b548ca54 | [
"Apache-2.0"
] | permissive | collares/lean4 | 861a9269c4592bce49b71059e232ff0bfe4594cc | 52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee | refs/heads/master | 1,691,419,031,324 | 1,618,678,138,000 | 1,618,678,138,000 | 358,989,750 | 0 | 0 | Apache-2.0 | 1,618,696,333,000 | 1,618,696,333,000 | null | UTF-8 | Lean | false | false | 11,061 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Compiler.ExportAttr
import Lean.Compiler.IR.CompilerM
import Lean.Compiler.IR.NormIds
namespace Lean
namespace IR
namespace Borrow
namespace OwnedSet
abbrev Key := FunId × Index
def beq : Key → Key → Bool
| (f₁, x₁), (f₂, x₂) => f₁ == f₂ && x₁ == x₂
instance : BEq Key := ⟨beq⟩
def getHash : Key → USize
| (f, x) => mixHash (hash f) (hash x)
instance : Hashable Key := ⟨getHash⟩
end OwnedSet
open OwnedSet (Key) in
abbrev OwnedSet := Std.HashMap Key Unit
def OwnedSet.insert (s : OwnedSet) (k : OwnedSet.Key) : OwnedSet := Std.HashMap.insert s k ()
def OwnedSet.contains (s : OwnedSet) (k : OwnedSet.Key) : Bool := Std.HashMap.contains s k
/- We perform borrow inference in a block of mutually recursive functions.
Join points are viewed as local functions, and are identified using
their local id + the name of the surrounding function.
We keep a mapping from function and joint points to parameters (`Array Param`).
Recall that `Param` contains the field `borrow`. -/
namespace ParamMap
inductive Key where
| decl (name : FunId)
| jp (name : FunId) (jpid : JoinPointId)
deriving BEq
def getHash : Key → USize
| Key.decl n => hash n
| Key.jp n id => mixHash (hash n) (hash id)
instance : Hashable Key := ⟨getHash⟩
end ParamMap
open ParamMap (Key)
abbrev ParamMap := Std.HashMap Key (Array Param)
def ParamMap.fmt (map : ParamMap) : Format :=
let fmts := map.fold (fun fmt k ps =>
let k := match k with
| ParamMap.Key.decl n => format n
| ParamMap.Key.jp n id => format n ++ ":" ++ format id
fmt ++ Format.line ++ k ++ " -> " ++ formatParams ps)
Format.nil
"{" ++ (Format.nest 1 fmts) ++ "}"
instance : ToFormat ParamMap := ⟨ParamMap.fmt⟩
instance : ToString ParamMap := ⟨fun m => Format.pretty (format m)⟩
namespace InitParamMap
/- Mark parameters that take a reference as borrow -/
def initBorrow (ps : Array Param) : Array Param :=
ps.map $ fun p => { p with borrow := p.ty.isObj }
/- We do perform borrow inference for constants marked as `export`.
Reason: we current write wrappers in C++ for using exported functions.
These wrappers use smart pointers such as `object_ref`.
When writing a new wrapper we need to know whether an argument is a borrow
inference or not.
We can revise this decision when we implement code for generating
the wrappers automatically. -/
def initBorrowIfNotExported (exported : Bool) (ps : Array Param) : Array Param :=
if exported then ps else initBorrow ps
partial def visitFnBody (fnid : FunId) : FnBody → StateM ParamMap Unit
| FnBody.jdecl j xs v b => do
modify fun m => m.insert (ParamMap.Key.jp fnid j) (initBorrow xs)
visitFnBody fnid v
visitFnBody fnid b
| FnBody.case _ _ _ alts => alts.forM fun alt => visitFnBody fnid alt.body
| e => do
unless e.isTerminal do
let (instr, b) := e.split
visitFnBody fnid b
def visitDecls (env : Environment) (decls : Array Decl) : StateM ParamMap Unit :=
decls.forM fun decl => match decl with
| Decl.fdecl (f := f) (xs := xs) (body := b) .. => do
let exported := isExport env f
modify fun m => m.insert (ParamMap.Key.decl f) (initBorrowIfNotExported exported xs)
visitFnBody f b
| _ => pure ()
end InitParamMap
def mkInitParamMap (env : Environment) (decls : Array Decl) : ParamMap :=
(InitParamMap.visitDecls env decls *> get).run' {}
/- Apply the inferred borrow annotations stored at `ParamMap` to a block of mutually
recursive functions. -/
namespace ApplyParamMap
partial def visitFnBody (fn : FunId) (paramMap : ParamMap) : FnBody → FnBody
| FnBody.jdecl j xs v b =>
let v := visitFnBody fn paramMap v
let b := visitFnBody fn paramMap b
match paramMap.find? (ParamMap.Key.jp fn j) with
| some ys => FnBody.jdecl j ys v b
| none => unreachable!
| FnBody.case tid x xType alts =>
FnBody.case tid x xType $ alts.map $ fun alt => alt.modifyBody (visitFnBody fn paramMap)
| e =>
if e.isTerminal then e
else
let (instr, b) := e.split
let b := visitFnBody fn paramMap b
instr.setBody b
def visitDecls (decls : Array Decl) (paramMap : ParamMap) : Array Decl :=
decls.map fun decl => match decl with
| Decl.fdecl f xs ty b info =>
let b := visitFnBody f paramMap b
match paramMap.find? (ParamMap.Key.decl f) with
| some xs => Decl.fdecl f xs ty b info
| none => unreachable!
| other => other
end ApplyParamMap
def applyParamMap (decls : Array Decl) (map : ParamMap) : Array Decl :=
-- dbgTrace ("applyParamMap " ++ toString map) $ fun _ =>
ApplyParamMap.visitDecls decls map
structure BorrowInfCtx where
env : Environment
currFn : FunId := arbitrary -- Function being analyzed.
paramSet : IndexSet := {} -- Set of all function parameters in scope. This is used to implement the heuristic at `ownArgsUsingParams`
structure BorrowInfState where
/- Set of variables that must be `owned`. -/
owned : OwnedSet := {}
modified : Bool := false
paramMap : ParamMap
abbrev M := ReaderT BorrowInfCtx (StateM BorrowInfState)
def getCurrFn : M FunId := do
let ctx ← read
pure ctx.currFn
def markModified : M Unit :=
modify fun s => { s with modified := true }
def ownVar (x : VarId) : M Unit := do
-- dbgTrace ("ownVar " ++ toString x) $ fun _ =>
let currFn ← getCurrFn
modify fun s =>
if s.owned.contains (currFn, x.idx) then s
else { s with owned := s.owned.insert (currFn, x.idx), modified := true }
def ownArg (x : Arg) : M Unit :=
match x with
| Arg.var x => ownVar x
| _ => pure ()
def ownArgs (xs : Array Arg) : M Unit :=
xs.forM ownArg
def isOwned (x : VarId) : M Bool := do
let currFn ← getCurrFn
let s ← get
pure $ s.owned.contains (currFn, x.idx)
/- Updates `map[k]` using the current set of `owned` variables. -/
def updateParamMap (k : ParamMap.Key) : M Unit := do
let currFn ← getCurrFn
let s ← get
match s.paramMap.find? k with
| some ps => do
let ps ← ps.mapM fun (p : Param) => do
if !p.borrow then pure p
else if (← isOwned p.x) then
markModified
pure { p with borrow := false }
else
pure p
modify fun s => { s with paramMap := s.paramMap.insert k ps }
| none => pure ()
def getParamInfo (k : ParamMap.Key) : M (Array Param) := do
let s ← get
match s.paramMap.find? k with
| some ps => pure ps
| none =>
match k with
| ParamMap.Key.decl fn => do
let ctx ← read
match findEnvDecl ctx.env fn with
| some decl => pure decl.params
| none => unreachable!
| _ => unreachable!
/- For each ps[i], if ps[i] is owned, then mark xs[i] as owned. -/
def ownArgsUsingParams (xs : Array Arg) (ps : Array Param) : M Unit :=
xs.size.forM fun i => do
let x := xs[i]
let p := ps[i]
unless p.borrow do ownArg x
/- For each xs[i], if xs[i] is owned, then mark ps[i] as owned.
We use this action to preserve tail calls. That is, if we have
a tail call `f xs`, if the i-th parameter is borrowed, but `xs[i]` is owned
we would have to insert a `dec xs[i]` after `f xs` and consequently
"break" the tail call. -/
def ownParamsUsingArgs (xs : Array Arg) (ps : Array Param) : M Unit :=
xs.size.forM fun i => do
let x := xs[i]
let p := ps[i]
match x with
| Arg.var x => if (← isOwned x) then ownVar p.x
| _ => pure ()
/- Mark `xs[i]` as owned if it is one of the parameters `ps`.
We use this action to mark function parameters that are being "packed" inside constructors.
This is a heuristic, and is not related with the effectiveness of the reset/reuse optimization.
It is useful for code such as
```
def f (x y : obj) :=
let z := ctor_1 x y;
ret z
```
-/
def ownArgsIfParam (xs : Array Arg) : M Unit := do
let ctx ← read
xs.forM fun x => do
match x with
| Arg.var x => if ctx.paramSet.contains x.idx then ownVar x
| _ => pure ()
def collectExpr (z : VarId) : Expr → M Unit
| Expr.reset _ x => ownVar z *> ownVar x
| Expr.reuse x _ _ ys => ownVar z *> ownVar x *> ownArgsIfParam ys
| Expr.ctor _ xs => ownVar z *> ownArgsIfParam xs
| Expr.proj _ x => do
if (← isOwned x) then ownVar z
if (← isOwned z) then ownVar x
| Expr.fap g xs => do
let ps ← getParamInfo (ParamMap.Key.decl g)
ownVar z *> ownArgsUsingParams xs ps
| Expr.ap x ys => ownVar z *> ownVar x *> ownArgs ys
| Expr.pap _ xs => ownVar z *> ownArgs xs
| other => pure ()
def preserveTailCall (x : VarId) (v : Expr) (b : FnBody) : M Unit := do
let ctx ← read
match v, b with
| (Expr.fap g ys), (FnBody.ret (Arg.var z)) =>
if ctx.currFn == g && x == z then
-- dbgTrace ("preserveTailCall " ++ toString b) $ fun _ => do
let ps ← getParamInfo (ParamMap.Key.decl g)
ownParamsUsingArgs ys ps
| _, _ => pure ()
def updateParamSet (ctx : BorrowInfCtx) (ps : Array Param) : BorrowInfCtx :=
{ ctx with paramSet := ps.foldl (fun s p => s.insert p.x.idx) ctx.paramSet }
partial def collectFnBody : FnBody → M Unit
| FnBody.jdecl j ys v b => do
withReader (fun ctx => updateParamSet ctx ys) (collectFnBody v)
let ctx ← read
updateParamMap (ParamMap.Key.jp ctx.currFn j)
collectFnBody b
| FnBody.vdecl x _ v b => collectFnBody b *> collectExpr x v *> preserveTailCall x v b
| FnBody.jmp j ys => do
let ctx ← read
let ps ← getParamInfo (ParamMap.Key.jp ctx.currFn j)
ownArgsUsingParams ys ps -- for making sure the join point can reuse
ownParamsUsingArgs ys ps -- for making sure the tail call is preserved
| FnBody.case _ _ _ alts => alts.forM fun alt => collectFnBody alt.body
| e => do unless e.isTerminal do collectFnBody e.body
partial def collectDecl : Decl → M Unit
| Decl.fdecl (f := f) (xs := ys) (body := b) .. =>
withReader (fun ctx => let ctx := updateParamSet ctx ys; { ctx with currFn := f }) do
collectFnBody b
updateParamMap (ParamMap.Key.decl f)
| _ => pure ()
/- Keep executing `x` until it reaches a fixpoint -/
@[inline] partial def whileModifing (x : M Unit) : M Unit := do
modify fun s => { s with modified := false }
x
let s ← get
if s.modified then
whileModifing x
else
pure ()
def collectDecls (decls : Array Decl) : M ParamMap := do
whileModifing (decls.forM collectDecl)
let s ← get
pure s.paramMap
def infer (env : Environment) (decls : Array Decl) : ParamMap :=
collectDecls decls { env := env } |>.run' { paramMap := mkInitParamMap env decls }
end Borrow
def inferBorrow (decls : Array Decl) : CompilerM (Array Decl) := do
let env ← getEnv
let paramMap := Borrow.infer env decls
pure (Borrow.applyParamMap decls paramMap)
end IR
end Lean
|
b73d4bfcd3e503303b28410ea03c346e8be93165 | ff5230333a701471f46c57e8c115a073ebaaa448 | /library/init/meta/mk_dec_eq_instance.lean | 4c5235abd41f7410c576bb8772f99e05619d0e04 | [
"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 | 5,620 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
Helper tactic for showing that a type has decidable equality.
-/
prelude
import init.meta.contradiction_tactic init.meta.constructor_tactic
import init.meta.injection_tactic init.meta.relation_tactics
import init.meta.rec_util init.meta.interactive
namespace tactic
open expr environment list
/- Retrieve the name of the type we are building a decidable equality proof for. -/
private meta def get_dec_eq_type_name : tactic name :=
do {
(pi x1 i1 d1 (pi x2 i2 d2 b)) ← target >>= whnf,
(const n ls) ← return (get_app_fn b),
when (n ≠ `decidable) failed,
(const I ls) ← return (get_app_fn d1),
return I }
<|>
fail "mk_dec_eq_instance tactic failed, target type is expected to be of the form (decidable_eq ...)"
/- Extract (lhs, rhs) from a goal (decidable (lhs = rhs)) -/
private meta def get_lhs_rhs : tactic (expr × expr) :=
do
(app dec lhs_eq_rhs) ← target | fail "mk_dec_eq_instance failed, unexpected case",
match_eq lhs_eq_rhs
private meta def find_next_target : list expr → list expr → tactic (expr × expr)
| (t::ts) (r::rs) := if t = r then find_next_target ts rs else return (t, r)
| l1 l2 := failed
/- Create an inhabitant of (decidable (lhs = rhs)) -/
private meta def mk_dec_eq_for (lhs : expr) (rhs : expr) : tactic expr :=
do lhs_type ← infer_type lhs,
dec_type ← mk_app `decidable_eq [lhs_type] >>= whnf,
do {
inst ← mk_instance dec_type,
return $ inst lhs rhs }
<|>
do {
f ← pp dec_type,
fail $ to_fmt "mk_dec_eq_instance failed, failed to generate instance for" ++ format.nest 2 (format.line ++ f) }
private meta def apply_eq_of_heq (h : expr) : tactic unit :=
do pr ← mk_app `eq_of_heq [h],
ty ← infer_type pr,
assertv `h' ty pr >> skip
/- Target is of the form (decidable (C ... = C ...)) where C is a constructor -/
private meta def dec_eq_same_constructor : name → name → nat → tactic unit
| I_name F_name num_rec :=
do
(lhs, rhs) ← get_lhs_rhs,
-- Try easy case first, where the proof is just reflexivity
(unify lhs rhs >> right >> reflexivity)
<|>
do {
let lhs_list := get_app_args lhs,
let rhs_list := get_app_args rhs,
when (length lhs_list ≠ length rhs_list) (fail "mk_dec_eq_instance failed, constructor applications have different number of arguments"),
(lhs_arg, rhs_arg) ← find_next_target lhs_list rhs_list,
rec ← is_type_app_of lhs_arg I_name,
inst ← if rec then do {
inst_fn ← mk_brec_on_rec_value F_name num_rec,
return $ app inst_fn rhs_arg }
else do {
mk_dec_eq_for lhs_arg rhs_arg
},
`[eapply @decidable.by_cases _ _ %%inst],
-- discharge first (positive) case by recursion
intro1 >>= subst >> dec_eq_same_constructor I_name F_name (if rec then num_rec + 1 else num_rec),
-- discharge second (negative) case by contradiction
intro1, left, -- decidable.is_false
intro1 >>= injection,
intros,
contradiction <|> do {
lc ← local_context,
lc.mmap' (λ h, try (apply_eq_of_heq h) <|> skip),
contradiction },
return () }
/- Easy case: target is of the form (decidable (C_1 ... = C_2 ...)) where C_1 and C_2 are distinct constructors -/
private meta def dec_eq_diff_constructor : tactic unit :=
left >> intron 1 >> contradiction
/- This tactic is invoked for each case of decidable_eq. There n^2 cases, where n is the number
of constructors. -/
private meta def dec_eq_case_2 (I_name : name) (F_name : name) : tactic unit :=
do
(lhs, rhs) ← get_lhs_rhs,
let lhs_fn := get_app_fn lhs,
let rhs_fn := get_app_fn rhs,
if lhs_fn = rhs_fn
then dec_eq_same_constructor I_name F_name 0
else dec_eq_diff_constructor
private meta def dec_eq_case_1 (I_name : name) (F_name : name) : tactic unit :=
intro `w >>= cases >> all_goals (dec_eq_case_2 I_name F_name)
meta def mk_dec_eq_instance_core : tactic unit :=
do I_name ← get_dec_eq_type_name,
env ← get_env,
let v_name := `_v,
let F_name := `_F,
let num_indices := inductive_num_indices env I_name,
let idx_names := list.map (λ (p : name × nat), mk_num_name p.fst p.snd) (list.zip (list.repeat `idx num_indices) (list.iota num_indices)),
-- Use brec_on if type is recursive.
-- We store the functional in the variable F.
if is_recursive env I_name
then intro1 >>= (λ x, induction x (idx_names ++ [v_name, F_name]) (some $ I_name <.> "brec_on") >> return ())
else intro v_name >> return (),
-- Apply cases to first element of type (I ...)
get_local v_name >>= cases,
all_goals (dec_eq_case_1 I_name F_name)
meta def mk_dec_eq_instance : tactic unit :=
do env ← get_env,
(pi x1 i1 d1 (pi x2 i2 d2 b)) ← target >>= whnf,
(const I_name ls) ← return (get_app_fn d1),
when (is_ginductive env I_name ∧ ¬ is_inductive env I_name) $
do { d1' ← whnf d1,
(app I_basic_const I_idx) ← return d1',
I_idx_type ← infer_type I_idx,
new_goal ← to_expr ``(∀ (_idx : %%I_idx_type), decidable_eq (%%I_basic_const _idx)),
assert `_basic_dec_eq new_goal,
swap,
`[exact _basic_dec_eq %%I_idx],
intro1,
return () },
mk_dec_eq_instance_core
meta instance binder_info.has_decidable_eq : decidable_eq binder_info :=
by mk_dec_eq_instance
@[derive_handler] meta def decidable_eq_derive_handler :=
instance_derive_handler ``decidable_eq tactic.mk_dec_eq_instance
end tactic
|
aab5014fe25e9a33f2e0549f08ef21b9e49a5477 | 6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf | /src/game/world3/level3.lean | c221724b19785147805be3ba0887f734fcfc2c0f | [
"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 | 686 | lean | import game.world3.level2 -- hide
import mynat.mul -- hide
namespace mynat -- hide
/-
# Multiplication World
## Level 3: `one_mul`
These proofs from addition world might be useful here:
* `one_eq_succ_zero : 1 = succ(0)`
* `succ_eq_add_one a : succ(a) = a + 1`
We just proved `mul_one`, now let's prove `one_mul`.
Then we will have proved, in fancy terms,
that 1 is a "left and right identity"
for multiplication (just like we showed that
0 is a left and right identity for addition
with `add_zero` and `zero_add`).
-/
/- Lemma
For any natural number $m$, we have
$$ 1 \times m = m. $$
-/
lemma one_mul (m : mynat) : 1 * m = m :=
begin [nat_num_game]
end
end mynat -- hide
|
0e34d649f26ec5b1d41fba70bb10154dc6db6d46 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/polynomial/laurent.lean | 62ada63f0225dfd47bde55a6d525ecd66a31d04f | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 7,763 | lean | /-
Copyright (c) 2022 Damiano Testa. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Damiano Testa
-/
import data.polynomial.algebra_map
/-! # Laurent polynomials
We introduce Laurent polynomials over a semiring `R`. Mathematically, they are expressions of the
form
$$
\sum_{i \in \mathbb{Z}} a_i T ^ i
$$
where the sum extends over a finite subset of `ℤ`. Thus, negative exponents are allowed. The
coefficients come from the semiring `R` and the variable `T` commutes with everything.
Since we are going to convert back and forth between polynomials and Laurent polynomials, we
decided to maintain some distinction by using the symbol `T`, rather than `X`, as the variable for
Laurent polynomials
## Notation
The symbol `R[T;T⁻¹]` stands for `laurent_polynomial R`. We also define
* `C : R →+* R[T;T⁻¹]` the inclusion of constant polynomials, analogous to the one for `R[X]`;
* `T : ℤ → R[T;T⁻¹]` the sequence of powers of the variable `T`.
## Implementation notes
We define Laurent polynomials as `add_monoid_algebra R ℤ`.
Thus, they are essentially `finsupp`s `ℤ →₀ R`.
This choice differs from the current irreducible design of `polynomial`, that instead shields away
the implementation via `finsupp`s. It is closer to the original definition of polynomials.
As a consequence, `laurent_polynomial` plays well with polynomials, but there is a little roughness
in establishing the API, since the `finsupp` implementation of `polynomial R` is well-shielded.
Unlike the case of polynomials, I felt that the exponent notation was not too easy to use, as only
natural exponents would be allowed. Moreover, in the end, it seems likely that we should aim to
perform computations on exponents in `ℤ` anyway and separating this via the symbol `T` seems
convenient.
I made a *heavy* use of `simp` lemmas, aiming to bring Laurent polynomials to the form `C a * T n`.
Any comments or suggestions for improvements is greatly appreciated!
## Future work
Lots is missing! I would certainly like to show that `R[T;T⁻¹]` is the localization of `R[X]`
inverting `X`. This should be mostly in place, given `exists_T_pow` (which is part of PR #13415).
(Riccardo) giving a morphism (as `R`-alg, so in the commutative case)
from `R[T,T⁻¹]` to `S` is the same as choosing a unit of `S`.
-/
open_locale polynomial big_operators
open polynomial add_monoid_algebra finsupp
noncomputable theory
variables {R : Type*}
/-- The semiring of Laurent polynomials with coefficients in the semiring `R`.
We denote it by `R[T;T⁻¹]`.
The ring homomorphism `C : R →+* R[T;T⁻¹]` includes `R` as the constant polynomials. -/
abbreviation laurent_polynomial (R : Type*) [semiring R] := add_monoid_algebra R ℤ
local notation R`[T;T⁻¹]`:9000 := laurent_polynomial R
/-- The ring homomorphism, taking a polynomial with coefficients in `R` to a Laurent polynomial
with coefficients in `R`. -/
def polynomial.to_laurent [semiring R] : R[X] →+* R[T;T⁻¹] :=
(map_domain_ring_hom R int.of_nat_hom).comp (to_finsupp_iso R)
/-- This is not a simp lemma, as it is usually preferable to use the lemmas about `C` and `X`
instead. -/
lemma polynomial.to_laurent_apply [semiring R] (p : R[X]) :
p.to_laurent = p.to_finsupp.map_domain coe := rfl
/-- The `R`-algebra map, taking a polynomial with coefficients in `R` to a Laurent polynomial
with coefficients in `R`. -/
def polynomial.to_laurent_alg [comm_semiring R] :
R[X] →ₐ[R] R[T;T⁻¹] :=
begin
refine alg_hom.comp _ (to_finsupp_iso_alg R).to_alg_hom,
exact (map_domain_alg_hom R R int.of_nat_hom),
end
@[simp]
lemma polynomial.to_laurent_alg_apply [comm_semiring R] (f : R[X]) :
f.to_laurent_alg = f.to_laurent := rfl
namespace laurent_polynomial
section semiring
variables [semiring R]
lemma single_zero_one_eq_one : (single 0 1 : R[T;T⁻¹]) = (1 : R[T;T⁻¹]) := rfl
/-! ### The functions `C` and `T`. -/
/-- The ring homomorphism `C`, including `R` into the ring of Laurent polynomials over `R` as
the constant Laurent polynomials. -/
def C : R →+* R[T;T⁻¹] :=
single_zero_ring_hom
lemma algebra_map_apply {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] (r : R) :
algebra_map R (laurent_polynomial A) r = C (algebra_map R A r) :=
rfl
/--
When we have `[comm_semiring R]`, the function `C` is the same as `algebra_map R R[T;T⁻¹]`.
(But note that `C` is defined when `R` is not necessarily commutative, in which case
`algebra_map` is not available.)
-/
lemma C_eq_algebra_map {R : Type*} [comm_semiring R] (r : R) :
C r = algebra_map R R[T;T⁻¹] r :=
rfl
lemma single_eq_C (r : R) : single 0 r = C r := rfl
/-- The function `n ↦ T ^ n`, implemented as a sequence `ℤ → R[T;T⁻¹]`.
Using directly `T ^ n` does not work, since we want the exponents to be of Type `ℤ` and there
is no `ℤ`-power defined on `R[T;T⁻¹]`. Using that `T` is a unit introduces extra coercions.
For these reasons, the definition of `T` is as a sequence. -/
def T (n : ℤ) : R[T;T⁻¹] := single n 1
@[simp]
lemma T_zero : (T 0 : R[T;T⁻¹]) = 1 := rfl
lemma T_add (m n : ℤ) : (T (m + n) : R[T;T⁻¹]) = T m * T n :=
by { convert single_mul_single.symm, simp [T] }
@[simp]
lemma T_pow (m : ℤ) (n : ℕ) : (T m ^ n : R[T;T⁻¹]) = T (n * m) :=
by rw [T, T, single_pow n, one_pow, nsmul_eq_mul, int.nat_cast_eq_coe_nat]
/-- The `simp` version of `mul_assoc`, in the presence of `T`'s. -/
@[simp]
lemma mul_T_assoc (f : R[T;T⁻¹]) (m n : ℤ) : f * T m * T n = f * T (m + n) :=
by simp [← T_add, mul_assoc]
@[simp]
lemma single_eq_C_mul_T (r : R) (n : ℤ) :
(single n r : R[T;T⁻¹]) = (C r * T n : R[T;T⁻¹]) :=
by convert single_mul_single.symm; simp
-- This lemma locks in the right changes and is what Lean proved directly.
-- The actual `simp`-normal form of a Laurent monomial is `C a * T n`, whenever it can be reached.
@[simp]
lemma _root_.polynomial.to_laurent_C_mul_T (n : ℕ) (r : R) :
((polynomial.monomial n r).to_laurent : R[T;T⁻¹]) = C r * T n :=
show map_domain coe (monomial n r).to_finsupp = (C r * T n : R[T;T⁻¹]),
by rw [to_finsupp_monomial, map_domain_single, single_eq_C_mul_T]
@[simp]
lemma _root_.polynomial.to_laurent_C (r : R) :
(polynomial.C r).to_laurent = C r :=
begin
convert polynomial.to_laurent_C_mul_T 0 r,
simp only [int.coe_nat_zero, T_zero, mul_one],
end
@[simp]
lemma _root_.polynomial.to_laurent_X :
(polynomial.X.to_laurent : R[T;T⁻¹]) = T 1 :=
begin
have : (polynomial.X : R[X]) = monomial 1 1,
{ simp [monomial_eq_C_mul_X] },
simp [this, polynomial.to_laurent_C_mul_T],
end
@[simp] lemma _root_.polynomial.to_laurent_one : (polynomial.to_laurent : R[X] → R[T;T⁻¹]) 1 = 1 :=
map_one polynomial.to_laurent
@[simp]
lemma _root_.polynomial.to_laurent_C_mul_eq (r : R) (f : R[X]):
(polynomial.C r * f).to_laurent = C r * f.to_laurent :=
by simp only [_root_.map_mul, polynomial.to_laurent_C]
@[simp]
lemma _root_.polynomial.to_laurent_X_pow (n : ℕ) :
(X ^ n : R[X]).to_laurent = T n :=
by simp only [map_pow, polynomial.to_laurent_X, T_pow, mul_one]
@[simp]
lemma _root_.polynomial.to_laurent_C_mul_X_pow (n : ℕ) (r : R) :
(polynomial.C r * X ^ n).to_laurent = C r * T n :=
by simp only [_root_.map_mul, polynomial.to_laurent_C, polynomial.to_laurent_X_pow]
instance invertible_T (n : ℤ) : invertible (T n : R[T;T⁻¹]) :=
{ inv_of := T (- n),
inv_of_mul_self := by rw [← T_add, add_left_neg, T_zero],
mul_inv_of_self := by rw [← T_add, add_right_neg, T_zero] }
@[simp]
lemma inv_of_T (n : ℤ) : ⅟ (T n : R[T;T⁻¹]) = T (- n) := rfl
lemma is_unit_T (n : ℤ) : is_unit (T n : R[T;T⁻¹]) :=
is_unit_of_invertible _
end semiring
end laurent_polynomial
|
10019a67c9b6cf27c5f136a0e1077561abb3350c | 36c7a18fd72e5b57229bd8ba36493daf536a19ce | /hott/algebra/binary.hlean | f6b4327b5a8a933b14c62909b131f04c242f10bd | [
"Apache-2.0"
] | permissive | YHVHvx/lean | 732bf0fb7a298cd7fe0f15d82f8e248c11db49e9 | 038369533e0136dd395dc252084d3c1853accbf2 | refs/heads/master | 1,610,701,080,210 | 1,449,128,595,000 | 1,449,128,595,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,785 | hlean | /-
Copyright (c) 2014-15 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad
General properties of binary operations.
-/
open eq.ops function equiv
namespace binary
section
variable {A : Type}
variables (op₁ : A → A → A) (inv : A → A) (one : A)
local notation a * b := op₁ a b
local notation a ⁻¹ := inv a
local notation 1 := one
definition commutative [reducible] := ∀a b, a * b = b * a
definition associative [reducible] := ∀a b c, (a * b) * c = a * (b * c)
definition left_identity [reducible] := ∀a, 1 * a = a
definition right_identity [reducible] := ∀a, a * 1 = a
definition left_inverse [reducible] := ∀a, a⁻¹ * a = 1
definition right_inverse [reducible] := ∀a, a * a⁻¹ = 1
definition left_cancelative [reducible] := ∀a b c, a * b = a * c → b = c
definition right_cancelative [reducible] := ∀a b c, a * b = c * b → a = c
definition inv_op_cancel_left [reducible] := ∀a b, a⁻¹ * (a * b) = b
definition op_inv_cancel_left [reducible] := ∀a b, a * (a⁻¹ * b) = b
definition inv_op_cancel_right [reducible] := ∀a b, a * b⁻¹ * b = a
definition op_inv_cancel_right [reducible] := ∀a b, a * b * b⁻¹ = a
variable (op₂ : A → A → A)
local notation a + b := op₂ a b
definition left_distributive [reducible] := ∀a b c, a * (b + c) = a * b + a * c
definition right_distributive [reducible] := ∀a b c, (a + b) * c = a * c + b * c
definition right_commutative [reducible] {B : Type} (f : B → A → B) := ∀ b a₁ a₂, f (f b a₁) a₂ = f (f b a₂) a₁
definition left_commutative [reducible] {B : Type} (f : A → B → B) := ∀ a₁ a₂ b, f a₁ (f a₂ b) = f a₂ (f a₁ b)
end
section
variable {A : Type}
variable {f : A → A → A}
variable H_comm : commutative f
variable H_assoc : associative f
local infixl * := f
theorem left_comm : left_commutative f :=
take a b c, calc
a*(b*c) = (a*b)*c : H_assoc
... = (b*a)*c : H_comm
... = b*(a*c) : H_assoc
theorem right_comm : right_commutative f :=
take a b c, calc
(a*b)*c = a*(b*c) : H_assoc
... = a*(c*b) : H_comm
... = (a*c)*b : H_assoc
theorem comm4 (a b c d : A) : a*b*(c*d) = a*c*(b*d) :=
calc
a*b*(c*d) = a*b*c*d : H_assoc
... = a*c*b*d : right_comm H_comm H_assoc
... = a*c*(b*d) : H_assoc
end
section
variable {A : Type}
variable {f : A → A → A}
variable H_assoc : associative f
local infixl * := f
theorem assoc4helper (a b c d) : (a*b)*(c*d) = a*((b*c)*d) :=
calc
(a*b)*(c*d) = a*(b*(c*d)) : H_assoc
... = a*((b*c)*d) : H_assoc
end
definition right_commutative_compose_right [reducible]
{A B : Type} (f : A → A → A) (g : B → A) (rcomm : right_commutative f) : right_commutative (compose_right f g) :=
λ a b₁ b₂, !rcomm
definition left_commutative_compose_left [reducible]
{A B : Type} (f : A → A → A) (g : B → A) (lcomm : left_commutative f) : left_commutative (compose_left f g) :=
λ a b₁ b₂, !lcomm
end binary
open eq
namespace is_equiv
definition inv_preserve_binary {A B : Type} (f : A → B) [H : is_equiv f]
(mA : A → A → A) (mB : B → B → B) (H : Π(a a' : A), mB (f a) (f a') = f (mA a a'))
(b b' : B) : f⁻¹ (mB b b') = mA (f⁻¹ b) (f⁻¹ b') :=
begin
have H2 : f⁻¹ (mB (f (f⁻¹ b)) (f (f⁻¹ b'))) = f⁻¹ (f (mA (f⁻¹ b) (f⁻¹ b'))), from ap f⁻¹ !H,
rewrite [+right_inv f at H2,left_inv f at H2,▸* at H2,H2]
end
definition preserve_binary_of_inv_preserve {A B : Type} (f : A → B) [H : is_equiv f]
(mA : A → A → A) (mB : B → B → B) (H : Π(b b' : B), mA (f⁻¹ b) (f⁻¹ b') = f⁻¹ (mB b b'))
(a a' : A) : f (mA a a') = mB (f a) (f a') :=
begin
have H2 : f (mA (f⁻¹ (f a)) (f⁻¹ (f a'))) = f (f⁻¹ (mB (f a) (f a'))), from ap f !H,
rewrite [right_inv f at H2,+left_inv f at H2,▸* at H2,H2]
end
end is_equiv
namespace equiv
open is_equiv equiv.ops
definition inv_preserve_binary {A B : Type} (f : A ≃ B)
(mA : A → A → A) (mB : B → B → B) (H : Π(a a' : A), mB (f a) (f a') = f (mA a a'))
(b b' : B) : f⁻¹ (mB b b') = mA (f⁻¹ b) (f⁻¹ b') :=
inv_preserve_binary f mA mB H b b'
definition preserve_binary_of_inv_preserve {A B : Type} (f : A ≃ B)
(mA : A → A → A) (mB : B → B → B) (H : Π(b b' : B), mA (f⁻¹ b) (f⁻¹ b') = f⁻¹ (mB b b'))
(a a' : A) : f (mA a a') = mB (f a) (f a') :=
preserve_binary_of_inv_preserve f mA mB H a a'
end equiv
|
035c73e792c750911f7842d6246c74f931bd6543 | 07c76fbd96ea1786cc6392fa834be62643cea420 | /library/data/tuple.lean | b03130c2b1ec3087545148ac7a599c759302023d | [
"Apache-2.0"
] | permissive | fpvandoorn/lean2 | 5a430a153b570bf70dc8526d06f18fc000a60ad9 | 0889cf65b7b3cebfb8831b8731d89c2453dd1e9f | refs/heads/master | 1,592,036,508,364 | 1,545,093,958,000 | 1,545,093,958,000 | 75,436,854 | 0 | 0 | null | 1,480,718,780,000 | 1,480,718,780,000 | null | UTF-8 | Lean | false | false | 12,961 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
Tuples are lists of a fixed size.
It is implemented as a subtype.
-/
import logic data.list data.fin
open nat list subtype function
definition tuple [reducible] (A : Type) (n : nat) := {l : list A | length l = n}
namespace tuple
variables {A B C : Type}
theorem induction_on [recursor 4] {P : ∀ {n}, tuple A n → Prop}
: ∀ {n} (v : tuple A n), (∀ (l : list A) {n : nat} (h : length l = n), P (tag l h)) → P v
| n (tag l h) H := @H l n h
definition nil : tuple A 0 :=
tag [] rfl
lemma length_succ {n : nat} {l : list A} (a : A) : length l = n → length (a::l) = succ n :=
λ h, congr_arg succ h
definition cons {n : nat} : A → tuple A n → tuple A (succ n)
| a (tag v h) := tag (a::v) (length_succ a h)
notation a :: b := cons a b
protected definition is_inhabited [instance] [h : inhabited A] : ∀ (n : nat), inhabited (tuple A n)
| 0 := inhabited.mk nil
| (succ n) := inhabited.mk (inhabited.value h :: inhabited.value (is_inhabited n))
protected definition has_decidable_eq [instance] [h : decidable_eq A] : ∀ (n : nat), decidable_eq (tuple A n) :=
λ n, subtype.has_decidable_eq
definition head {n : nat} : tuple A (succ n) → A
| (tag [] h) := by contradiction
| (tag (a::v) h) := a
definition tail {n : nat} : tuple A (succ n) → tuple A n
| (tag [] h) := by contradiction
| (tag (a::v) h) := tag v (succ.inj h)
theorem head_cons {n : nat} (a : A) (v : tuple A n) : head (a :: v) = a :=
by induction v; reflexivity
theorem tail_cons {n : nat} (a : A) (v : tuple A n) : tail (a :: v) = v :=
by induction v; reflexivity
theorem head_lcons {n : nat} (a : A) (l : list A) (h : length (a::l) = succ n) : head (tag (a::l) h) = a :=
rfl
theorem tail_lcons {n : nat} (a : A) (l : list A) (h : length (a::l) = succ n) : tail (tag (a::l) h) = tag l (succ.inj h) :=
rfl
definition last {n : nat} : tuple A (succ n) → A
| (tag l h) := list.last l (ne_nil_of_length_eq_succ h)
theorem eta : ∀ {n : nat} (v : tuple A (succ n)), head v :: tail v = v
| 0 (tag [] h) := by contradiction
| 0 (tag (a::l) h) := rfl
| (n+1) (tag [] h) := by contradiction
| (n+1) (tag (a::l) h) := rfl
definition of_list (l : list A) : tuple A (list.length l) :=
tag l rfl
definition to_list {n : nat} : tuple A n → list A
| (tag l h) := l
theorem to_list_of_list (l : list A) : to_list (of_list l) = l :=
rfl
theorem to_list_nil : to_list nil = ([] : list A) :=
rfl
theorem length_to_list {n : nat} : ∀ (v : tuple A n), list.length (to_list v) = n
| (tag l h) := h
theorem heq_of_list_eq {n m} : ∀ {v₁ : tuple A n} {v₂ : tuple A m}, to_list v₁ = to_list v₂ → n = m → v₁ == v₂
| (tag l₁ h₁) (tag l₂ h₂) e₁ e₂ := begin
clear heq_of_list_eq,
subst e₂, subst h₁,
unfold to_list at e₁,
subst l₁
end
theorem list_eq_of_heq {n m} {v₁ : tuple A n} {v₂ : tuple A m} : v₁ == v₂ → n = m → to_list v₁ = to_list v₂ :=
begin
intro h₁ h₂, revert v₁ v₂ h₁,
subst n, intro v₁ v₂ h₁, rewrite [eq_of_heq h₁]
end
theorem of_list_to_list {n : nat} (v : tuple A n) : of_list (to_list v) == v :=
begin
apply heq_of_list_eq, rewrite to_list_of_list, rewrite length_to_list
end
/- append -/
definition append {n m : nat} : tuple A n → tuple A m → tuple A (n + m)
| (tag l₁ h₁) (tag l₂ h₂) := tag (list.append l₁ l₂) (by rewrite [length_append, h₁, h₂])
infix ++ := append
open eq.ops
lemma push_eq_rec : ∀ {n m : nat} {l : list A} (h₁ : n = m) (h₂ : length l = n), h₁ ▹ (tag l h₂) = tag l (h₁ ▹ h₂)
| n n l (eq.refl n) h₂ := rfl
theorem append_nil_right {n : nat} (v : tuple A n) : v ++ nil = v :=
induction_on v (λ l n h, by unfold [tuple.append, tuple.nil]; congruence; apply list.append_nil_right)
theorem append_nil_left {n : nat} (v : tuple A n) : !zero_add ▹ (nil ++ v) = v :=
induction_on v (λ l n h, begin unfold [tuple.append, tuple.nil], rewrite [push_eq_rec] end)
theorem append_nil_left_heq {n : nat} (v : tuple A n) : nil ++ v == v :=
heq_of_eq_rec_left !zero_add (append_nil_left v)
theorem append.assoc {n₁ n₂ n₃} : ∀ (v₁ : tuple A n₁) (v₂ : tuple A n₂) (v₃ : tuple A n₃), !add.assoc ▹ ((v₁ ++ v₂) ++ v₃) = v₁ ++ (v₂ ++ v₃)
| (tag l₁ h₁) (tag l₂ h₂) (tag l₃ h₃) := begin
unfold tuple.append, rewrite push_eq_rec,
congruence,
apply list.append.assoc
end
theorem append.assoc_heq {n₁ n₂ n₃} (v₁ : tuple A n₁) (v₂ : tuple A n₂) (v₃ : tuple A n₃) : (v₁ ++ v₂) ++ v₃ == v₁ ++ (v₂ ++ v₃) :=
heq_of_eq_rec_left !add.assoc (append.assoc v₁ v₂ v₃)
/- reverse -/
definition reverse {n : nat} : tuple A n → tuple A n
| (tag l h) := tag (list.reverse l) (by rewrite [length_reverse, h])
theorem reverse_reverse {n : nat} (v : tuple A n) : reverse (reverse v) = v :=
induction_on v (λ l n h, begin unfold reverse, congruence, apply list.reverse_reverse end)
theorem tuple0_eq_nil : ∀ (v : tuple A 0), v = nil
| (tag [] h) := rfl
| (tag (a::l) h) := by contradiction
/- mem -/
definition mem {n : nat} (a : A) (v : tuple A n) : Prop :=
a ∈ elt_of v
notation e ∈ s := mem e s
notation e ∉ s := ¬ e ∈ s
theorem not_mem_nil (a : A) : a ∉ nil :=
list.not_mem_nil a
theorem mem_cons [simp] {n : nat} (a : A) (v : tuple A n) : a ∈ a :: v :=
induction_on v (λ l n h, !list.mem_cons)
theorem mem_cons_of_mem {n : nat} (y : A) {x : A} {v : tuple A n} : x ∈ v → x ∈ y :: v :=
induction_on v (λ l n h₁ h₂, list.mem_cons_of_mem y h₂)
theorem eq_or_mem_of_mem_cons {n : nat} {x y : A} {v : tuple A n} : x ∈ y::v → x = y ∨ x ∈ v :=
induction_on v (λ l n h₁ h₂, eq_or_mem_of_mem_cons h₂)
theorem mem_singleton {n : nat} {x a : A} : x ∈ (a::nil : tuple A 1) → x = a :=
assume h, list.mem_singleton h
/- map -/
definition map {n : nat} (f : A → B) : tuple A n → tuple B n
| (tag l h) := tag (list.map f l) (by clear map; substvars; rewrite length_map)
theorem map_nil (f : A → B) : map f nil = nil :=
rfl
theorem map_cons {n : nat} (f : A → B) (a : A) (v : tuple A n) : map f (a::v) = f a :: map f v :=
by induction v; reflexivity
theorem map_tag {n : nat} (f : A → B) (l : list A) (h : length l = n)
: map f (tag l h) = tag (list.map f l) (by substvars; rewrite length_map) :=
by reflexivity
theorem map_map {n : nat} (g : B → C) (f : A → B) (v : tuple A n) : map g (map f v) = map (g ∘ f) v :=
begin cases v, rewrite *map_tag, apply subtype.eq, apply list.map_map end
theorem map_id {n : nat} (v : tuple A n) : map id v = v :=
begin induction v, unfold map, congruence, apply list.map_id end
theorem mem_map {n : nat} {a : A} {v : tuple A n} (f : A → B) : a ∈ v → f a ∈ map f v :=
begin induction v, unfold map, apply list.mem_map end
theorem exists_of_mem_map {n : nat} {f : A → B} {b : B} {v : tuple A n} : b ∈ map f v → ∃a, a ∈ v ∧ f a = b :=
begin induction v, unfold map, apply list.exists_of_mem_map end
theorem eq_of_map_const {n : nat} {b₁ b₂ : B} {v : tuple A n} : b₁ ∈ map (const A b₂) v → b₁ = b₂ :=
begin induction v, unfold map, apply list.eq_of_map_const end
/- product -/
definition product {n m : nat} : tuple A n → tuple B m → tuple (A × B) (n * m)
| (tag l₁ h₁) (tag l₂ h₂) := tag (list.product l₁ l₂) (by rewrite [length_product, h₁, h₂])
theorem nil_product {m : nat} (v : tuple B m) : !zero_mul ▹ product (@nil A) v = nil :=
begin induction v, unfold [nil, product], rewrite push_eq_rec end
theorem nil_product_heq {m : nat} (v : tuple B m) : product (@nil A) v == (@nil (A × B)) :=
heq_of_eq_rec_left _ (nil_product v)
theorem product_nil {n : nat} (v : tuple A n) : product v (@nil B) = nil :=
begin induction v, unfold [nil, product], congruence, apply list.product_nil end
theorem mem_product {n m : nat} {a : A} {b : B} {v₁ : tuple A n} {v₂ : tuple B m} : a ∈ v₁ → b ∈ v₂ → (a, b) ∈ product v₁ v₂ :=
begin cases v₁, cases v₂, unfold product, apply list.mem_product end
theorem mem_of_mem_product_left {n m : nat} {a : A} {b : B} {v₁ : tuple A n} {v₂ : tuple B m} : (a, b) ∈ product v₁ v₂ → a ∈ v₁ :=
begin cases v₁, cases v₂, unfold product, apply list.mem_of_mem_product_left end
theorem mem_of_mem_product_right {n m : nat} {a : A} {b : B} {v₁ : tuple A n} {v₂ : tuple B m} : (a, b) ∈ product v₁ v₂ → b ∈ v₂ :=
begin cases v₁, cases v₂, unfold product, apply list.mem_of_mem_product_right end
/- ith -/
open fin
definition ith {n : nat} : tuple A n → fin n → A
| (tag l h₁) (mk i h₂) := list.ith l i (by rewrite h₁; exact h₂)
lemma ith_zero {n : nat} (a : A) (v : tuple A n) (h : 0 < succ n) : ith (a::v) (mk 0 h) = a :=
by induction v; reflexivity
lemma ith_fin_zero {n : nat} (a : A) (v : tuple A n) : ith (a::v) (fin.zero n) = a :=
by unfold fin.zero; apply ith_zero
lemma ith_succ {n : nat} (a : A) (v : tuple A n) (i : nat) (h : succ i < succ n)
: ith (a::v) (mk (succ i) h) = ith v (mk_pred i h) :=
by induction v; reflexivity
lemma ith_fin_succ {n : nat} (a : A) (v : tuple A n) (i : fin n)
: ith (a::v) (succ i) = ith v i :=
begin cases i, unfold fin.succ, rewrite ith_succ end
lemma ith_zero_eq_head {n : nat} (v : tuple A (nat.succ n)) : ith v (fin.zero n) = head v :=
by rewrite [-eta v, ith_fin_zero, head_cons]
lemma ith_succ_eq_ith_tail {n : nat} (v : tuple A (nat.succ n)) (i : fin n) : ith v (succ i) = ith (tail v) i :=
by rewrite [-eta v, ith_fin_succ, tail_cons]
protected lemma ext {n : nat} (v₁ v₂ : tuple A n) (h : ∀ i : fin n, ith v₁ i = ith v₂ i) : v₁ = v₂ :=
begin
induction n with n ih,
rewrite [tuple0_eq_nil v₁, tuple0_eq_nil v₂],
rewrite [-eta v₁, -eta v₂], congruence,
show head v₁ = head v₂, by rewrite [-ith_zero_eq_head, -ith_zero_eq_head]; apply h,
have ∀ i : fin n, ith (tail v₁) i = ith (tail v₂) i, from
take i, by rewrite [-ith_succ_eq_ith_tail, -ith_succ_eq_ith_tail]; apply h,
show tail v₁ = tail v₂, from ih _ _ this
end
/- tabulate -/
definition tabulate : Π {n : nat}, (fin n → A) → tuple A n
| 0 f := nil
| (n+1) f := f (fin.zero n) :: tabulate (λ i : fin n, f (succ i))
theorem ith_tabulate {n : nat} (f : fin n → A) (i : fin n) : ith (tabulate f) i = f i :=
begin
induction n with n ih,
apply elim0 i,
cases i with v hlt, cases v,
{unfold tabulate, rewrite ith_zero},
{unfold tabulate, rewrite [ith_succ, ih]}
end
variable {n : ℕ}
definition replicate (n : ℕ) : A → tuple A n
| a := tag (list.replicate n a) (length_replicate n a)
definition dropn : Π (i:ℕ), tuple A n → tuple A (n - i)
| i (tag l p) := tag (list.dropn i l) (p ▸ list.length_dropn i l)
definition firstn : Π (i:ℕ), tuple A n → tuple A (min i n)
| i (tag l p) :=
let q := calc list.length (list.firstn i l)
= min i (list.length l) : list.length_firstn_eq
... = min i n : p in
tag (list.firstn i l) q
definition map₂ : (A → B → C) → tuple A n → tuple B n → tuple C n
| f (tag x px) (tag y py) :=
let z : list C := list.map₂ f x y in
let p : list.length z = n := calc
list.length z = min (list.length x) (list.length y) : list.length_map₂
... = min n n : by rewrite [px, py]
... = n : min_self in
tag z p
section accum
open prod
variable {S : Type}
definition mapAccumR
: (A → S → S × B) → tuple A n → S → S × tuple B n
| f (tag x px) c :=
let z := list.mapAccumR f x c in
let p := calc
list.length (pr₂ (list.mapAccumR f x c))
= length x : length_mapAccumR
... = n : px in
(pr₁ z, tag (pr₂ z) p)
definition mapAccumR₂
: (A → B → S → S × C) → tuple A n → tuple B n → S → S × tuple C n
| f (tag x px) (tag y py) c :=
let z := list.mapAccumR₂ f x y c in
let p := calc
list.length (pr₂ (list.mapAccumR₂ f x y c))
= min (length x) (length y) : length_mapAccumR₂
... = n : by rewrite [ px, py, min_self ] in
(pr₁ z, tag (pr₂ z) p)
end accum
end tuple
|
c1cbd4855bb9c961c4cc35c46cf1c8f435f89d4d | 11e28114d9553ecd984ac4819661ffce3068bafe | /src/examples/rat.lean | 1a60023d9f98579d754af1f5951e2c1ffddfc99c | [
"MIT"
] | permissive | EdAyers/lean-subtask | 9a26eb81f0c8576effed4ca94342ae1281445c59 | 04ac5a6c3bc3bfd190af4d6dcce444ddc8914e4b | refs/heads/master | 1,586,516,665,621 | 1,558,701,948,000 | 1,558,701,948,000 | 160,983,035 | 4 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 2,106 | lean | /- Author: E.W.Ayers © 2019 -/
import ..equate
namespace rats
open robot
/- Example within the context of defining the rationals as ordered pairs of integers
quotiented by the relation (⟨a,b⟩ ~ ⟨c,d⟩) ↔ (a * d = c * b).
-/
meta def blast : tactic unit :=
tactic.timetac "blast" $ (using_smt_with {cc_cfg := {ac:=ff}} $ tactic.intros >> smt_tactic.iterate (smt_tactic.ematch >> smt_tactic.try smt_tactic.close))
attribute [ematch] mul_comm mul_assoc
universes u
structure q (α : Type u) [integral_domain α] := (n : α) (d : α ) (nz : d ≠ 0)
lemma q.ext {α : Type u} [integral_domain α] : Π (q1 q2 : q α), q1.n = q2.n → q1.d = q2.d → q1 = q2
|⟨n,d,nz⟩ ⟨_,_,_⟩ rfl rfl := rfl
instance (α : Type u) [integral_domain α] : setoid (q α) :=
{ r := (λ a b, a.1 * b.2 = b.1 * a.2)
, iseqv :=
⟨ λ a, rfl
, λ a b, eq.symm
, λ ⟨a,b,_⟩ ⟨c,d,h⟩ ⟨e,f,_⟩
(p : a * d = c * b)
(q : c * f = e * d),
suffices d * (a * f) = d * (e * b), from eq_of_mul_eq_mul_left h this,
-- by blast -- takes about 2 seconds
by equate -- also about 2 seconds, but much slower because implemented in Lean VM
⟩
}
def free (α : Type u) [integral_domain α] : Type* := @quotient (q α) (by apply_instance)
variables {α : Type u} [integral_domain α]
-- [TODO]
-- namespace free
-- def add : free α → free α → free α
-- := λ x y, quotient.lift_on₂ x y
-- (λ x y, ⟦(⟨x.1 * y.2 + y.1 * x.2, x.2 * y.2, mul_ne_zero x.nz y.nz⟩ : q α)⟧)
-- (λ a1 a2 b1 b2,
-- assume p : a1.n * b1.d = b1.1 * a1.2,
-- assume q : a2.1 * b2.2 = b2.1 * a2.2,
-- suffices (a1.1 * a2.2 + a2.1 * a1.2) * (b1.2 * b2.2)
-- = (b1.1 * b2.2 + b2.1 * b1.2) * (a1.2 * a2.2),
-- from quotient.sound this,
-- calc ((a1.1 * a2.2) + (a2.1 * a1.2)) * (b1.2 * b2.2)
-- = ((b1.1 * a1.2) * (a2.2 * b2.2) + (b1.2 * a1.2) * (b2.1 * a2.2))
-- : by equate
-- ... = (b1.1 * b2.2 + b2.1 * b1.2) * (a1.2 * a2.2)
-- : by symmetry; clear p q; equate
-- )
-- end free
end rats |
ad472b39eeb0e8bbb01cab775f0836e8fef15947 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/linear_algebra/tensor_algebra/basic.lean | fd4eb9a2214b94c2f8aec0f0c6113f8fb81f3c98 | [
"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 | 9,315 | lean | /-
Copyright (c) 2020 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz
-/
import algebra.free_algebra
import algebra.ring_quot
import algebra.triv_sq_zero_ext
/-!
# Tensor Algebras
Given a commutative semiring `R`, and an `R`-module `M`, we construct the tensor algebra of `M`.
This is the free `R`-algebra generated (`R`-linearly) by the module `M`.
## Notation
1. `tensor_algebra R M` is the tensor algebra itself. It is endowed with an R-algebra structure.
2. `tensor_algebra.ι R` is the canonical R-linear map `M → tensor_algebra R M`.
3. Given a linear map `f : M → A` to an R-algebra `A`, `lift R f` is the lift of `f` to an
`R`-algebra morphism `tensor_algebra R M → A`.
## Theorems
1. `ι_comp_lift` states that the composition `(lift R f) ∘ (ι R)` is identical to `f`.
2. `lift_unique` states that whenever an R-algebra morphism `g : tensor_algebra R M → A` is
given whose composition with `ι R` is `f`, then one has `g = lift R f`.
3. `hom_ext` is a variant of `lift_unique` in the form of an extensionality theorem.
4. `lift_comp_ι` is a combination of `ι_comp_lift` and `lift_unique`. It states that the lift
of the composition of an algebra morphism with `ι` is the algebra morphism itself.
## Implementation details
As noted above, the tensor algebra of `M` is constructed as the free `R`-algebra generated by `M`,
modulo the additional relations making the inclusion of `M` into an `R`-linear map.
-/
variables (R : Type*) [comm_semiring R]
variables (M : Type*) [add_comm_monoid M] [module R M]
namespace tensor_algebra
/--
An inductively defined relation on `pre R M` used to force the initial algebra structure on
the associated quotient.
-/
inductive rel : free_algebra R M → free_algebra R M → Prop
-- force `ι` to be linear
| add {a b : M} :
rel (free_algebra.ι R (a+b)) (free_algebra.ι R a + free_algebra.ι R b)
| smul {r : R} {a : M} :
rel (free_algebra.ι R (r • a)) (algebra_map R (free_algebra R M) r * free_algebra.ι R a)
end tensor_algebra
/--
The tensor algebra of the module `M` over the commutative semiring `R`.
-/
@[derive [inhabited, semiring, algebra R]]
def tensor_algebra := ring_quot (tensor_algebra.rel R M)
namespace tensor_algebra
instance {S : Type*} [comm_ring S] [module S M] : ring (tensor_algebra S M) :=
ring_quot.ring (rel S M)
variables {M}
/--
The canonical linear map `M →ₗ[R] tensor_algebra R M`.
-/
def ι : M →ₗ[R] (tensor_algebra R M) :=
{ to_fun := λ m, (ring_quot.mk_alg_hom R _ (free_algebra.ι R m)),
map_add' := λ x y, by { rw [←alg_hom.map_add], exact ring_quot.mk_alg_hom_rel R rel.add, },
map_smul' := λ r x, by { rw [←alg_hom.map_smul], exact ring_quot.mk_alg_hom_rel R rel.smul, } }
lemma ring_quot_mk_alg_hom_free_algebra_ι_eq_ι (m : M) :
ring_quot.mk_alg_hom R (rel R M) (free_algebra.ι R m) = ι R m := rfl
/--
Given a linear map `f : M → A` where `A` is an `R`-algebra, `lift R f` is the unique lift
of `f` to a morphism of `R`-algebras `tensor_algebra R M → A`.
-/
@[simps symm_apply]
def lift {A : Type*} [semiring A] [algebra R A] : (M →ₗ[R] A) ≃ (tensor_algebra R M →ₐ[R] A) :=
{ to_fun := ring_quot.lift_alg_hom R ∘ λ f,
⟨free_algebra.lift R ⇑f, λ x y (h : rel R M x y), by induction h; simp [algebra.smul_def]⟩,
inv_fun := λ F, F.to_linear_map.comp (ι R),
left_inv := λ f, by { ext, simp [ι], },
right_inv := λ F, by { ext, simp [ι], } }
variables {R}
@[simp]
theorem ι_comp_lift {A : Type*} [semiring A] [algebra R A] (f : M →ₗ[R] A) :
(lift R f).to_linear_map.comp (ι R) = f := (lift R).symm_apply_apply f
@[simp]
theorem lift_ι_apply {A : Type*} [semiring A] [algebra R A] (f : M →ₗ[R] A) (x) :
lift R f (ι R x) = f x := by { dsimp [lift, ι], refl, }
@[simp]
theorem lift_unique {A : Type*} [semiring A] [algebra R A] (f : M →ₗ[R] A)
(g : tensor_algebra R M →ₐ[R] A) : g.to_linear_map.comp (ι R) = f ↔ g = lift R f :=
(lift R).symm_apply_eq
-- Marking `tensor_algebra` irreducible makes `ring` instances inaccessible on quotients.
-- https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/algebra.2Esemiring_to_ring.20breaks.20semimodule.20typeclass.20lookup/near/212580241
-- For now, we avoid this by not marking it irreducible.
attribute [irreducible] ι lift
@[simp]
theorem lift_comp_ι {A : Type*} [semiring A] [algebra R A] (g : tensor_algebra R M →ₐ[R] A) :
lift R (g.to_linear_map.comp (ι R)) = g :=
by { rw ←lift_symm_apply, exact (lift R).apply_symm_apply g }
/-- See note [partially-applied ext lemmas]. -/
@[ext]
theorem hom_ext {A : Type*} [semiring A] [algebra R A] {f g : tensor_algebra R M →ₐ[R] A}
(w : f.to_linear_map.comp (ι R) = g.to_linear_map.comp (ι R)) : f = g :=
begin
rw [←lift_symm_apply, ←lift_symm_apply] at w,
exact (lift R).symm.injective w,
end
/-- If `C` holds for the `algebra_map` of `r : R` into `tensor_algebra R M`, the `ι` of `x : M`,
and is preserved under addition and muliplication, then it holds for all of `tensor_algebra R M`.
-/
-- This proof closely follows `free_algebra.induction`
@[elab_as_eliminator]
lemma induction {C : tensor_algebra R M → Prop}
(h_grade0 : ∀ r, C (algebra_map R (tensor_algebra R M) r))
(h_grade1 : ∀ x, C (ι R x))
(h_mul : ∀ a b, C a → C b → C (a * b))
(h_add : ∀ a b, C a → C b → C (a + b))
(a : tensor_algebra R M) :
C a :=
begin
-- the arguments are enough to construct a subalgebra, and a mapping into it from M
let s : subalgebra R (tensor_algebra R M) :=
{ carrier := C,
mul_mem' := h_mul,
add_mem' := h_add,
algebra_map_mem' := h_grade0, },
let of : M →ₗ[R] s := (ι R).cod_restrict s.to_submodule h_grade1,
-- the mapping through the subalgebra is the identity
have of_id : alg_hom.id R (tensor_algebra R M) = s.val.comp (lift R of),
{ ext,
simp [of], },
-- finding a proof is finding an element of the subalgebra
convert subtype.prop (lift R of a),
exact alg_hom.congr_fun of_id a,
end
/-- The left-inverse of `algebra_map`. -/
def algebra_map_inv : tensor_algebra R M →ₐ[R] R :=
lift R (0 : M →ₗ[R] R)
variables (M)
lemma algebra_map_left_inverse :
function.left_inverse algebra_map_inv (algebra_map R $ tensor_algebra R M) :=
λ x, by simp [algebra_map_inv]
@[simp] lemma algebra_map_inj (x y : R) :
algebra_map R (tensor_algebra R M) x = algebra_map R (tensor_algebra R M) y ↔ x = y :=
(algebra_map_left_inverse M).injective.eq_iff
@[simp] lemma algebra_map_eq_zero_iff (x : R) : algebra_map R (tensor_algebra R M) x = 0 ↔ x = 0 :=
map_eq_zero_iff (algebra_map _ _) (algebra_map_left_inverse _).injective
@[simp] lemma algebra_map_eq_one_iff (x : R) : algebra_map R (tensor_algebra R M) x = 1 ↔ x = 1 :=
map_eq_one_iff (algebra_map _ _) (algebra_map_left_inverse _).injective
variables {M}
/-- The canonical map from `tensor_algebra R M` into `triv_sq_zero_ext R M` that sends
`tensor_algebra.ι` to `triv_sq_zero_ext.inr`. -/
def to_triv_sq_zero_ext : tensor_algebra R M →ₐ[R] triv_sq_zero_ext R M :=
lift R (triv_sq_zero_ext.inr_hom R M)
@[simp] lemma to_triv_sq_zero_ext_ι (x : M) :
to_triv_sq_zero_ext (ι R x) = triv_sq_zero_ext.inr x :=
lift_ι_apply _ _
/-- The left-inverse of `ι`.
As an implementation detail, we implement this using `triv_sq_zero_ext` which has a suitable
algebra structure. -/
def ι_inv : tensor_algebra R M →ₗ[R] M :=
(triv_sq_zero_ext.snd_hom R M).comp to_triv_sq_zero_ext.to_linear_map
lemma ι_left_inverse : function.left_inverse ι_inv (ι R : M → tensor_algebra R M) :=
λ x, by simp [ι_inv]
variables (R)
@[simp] lemma ι_inj (x y : M) : ι R x = ι R y ↔ x = y :=
ι_left_inverse.injective.eq_iff
@[simp] lemma ι_eq_zero_iff (x : M) : ι R x = 0 ↔ x = 0 :=
by rw [←ι_inj R x 0, linear_map.map_zero]
variables {R}
@[simp] lemma ι_eq_algebra_map_iff (x : M) (r : R) : ι R x = algebra_map R _ r ↔ x = 0 ∧ r = 0 :=
begin
refine ⟨λ h, _, _⟩,
{ have hf0 : to_triv_sq_zero_ext (ι R x) = (0, x), from lift_ι_apply _ _,
rw [h, alg_hom.commutes] at hf0,
have : r = 0 ∧ 0 = x := prod.ext_iff.1 hf0,
exact this.symm.imp_left eq.symm, },
{ rintro ⟨rfl, rfl⟩,
rw [linear_map.map_zero, ring_hom.map_zero] }
end
@[simp] lemma ι_ne_one [nontrivial R] (x : M) : ι R x ≠ 1 :=
begin
rw [←(algebra_map R (tensor_algebra R M)).map_one, ne.def, ι_eq_algebra_map_iff],
exact one_ne_zero ∘ and.right,
end
/-- The generators of the tensor algebra are disjoint from its scalars. -/
lemma ι_range_disjoint_one : disjoint (ι R).range (1 : submodule R (tensor_algebra R M)) :=
begin
rw submodule.disjoint_def,
rintros _ ⟨x, hx⟩ ⟨r, (rfl : algebra_map _ _ _ = _)⟩,
rw ι_eq_algebra_map_iff x at hx,
rw [hx.2, ring_hom.map_zero]
end
end tensor_algebra
namespace free_algebra
variables {R M}
/-- The canonical image of the `free_algebra` in the `tensor_algebra`, which maps
`free_algebra.ι R x` to `tensor_algebra.ι R x`. -/
def to_tensor : free_algebra R M →ₐ[R] tensor_algebra R M :=
free_algebra.lift R (tensor_algebra.ι R)
@[simp] lemma to_tensor_ι (m : M) : (free_algebra.ι R m).to_tensor = tensor_algebra.ι R m :=
by simp [to_tensor]
end free_algebra
|
d31e604750d927e3fd2ef7b749c267de14acc217 | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/analysis/special_functions/trigonometric.lean | 6cb9fd077c3d30d27e45d25a8a4f77542a3aab19 | [
"Apache-2.0"
] | permissive | abentkamp/mathlib | d9a75d291ec09f4637b0f30cc3880ffb07549ee5 | 5360e476391508e092b5a1e5210bd0ed22dc0755 | refs/heads/master | 1,682,382,954,948 | 1,622,106,077,000 | 1,622,106,077,000 | 149,285,665 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 141,197 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson
-/
import analysis.special_functions.exp_log
import data.set.intervals.infinite
import algebra.quadratic_discriminant
import ring_theory.polynomial.chebyshev
import analysis.calculus.times_cont_diff
/-!
# Trigonometric functions
## Main definitions
This file contains the following definitions:
* π, arcsin, arccos, arctan
* argument of a complex number
* logarithm on complex numbers
## Main statements
Many basic inequalities on trigonometric functions are established.
The continuity and differentiability of the usual trigonometric functions are proved, and their
derivatives are computed.
* `polynomial.chebyshev.T_complex_cos`: the `n`-th Chebyshev polynomial evaluates on `complex.cos θ`
to the value `n * complex.cos θ`.
## Tags
log, sin, cos, tan, arcsin, arccos, arctan, angle, argument
-/
noncomputable theory
open_locale classical topological_space filter
open set filter
namespace complex
/-- The complex sine function is everywhere strictly differentiable, with the derivative `cos x`. -/
lemma has_strict_deriv_at_sin (x : ℂ) : has_strict_deriv_at sin (cos x) x :=
begin
simp only [cos, div_eq_mul_inv],
convert ((((has_strict_deriv_at_id x).neg.mul_const I).cexp.sub
((has_strict_deriv_at_id x).mul_const I).cexp).mul_const I).mul_const (2:ℂ)⁻¹,
simp only [function.comp, id],
rw [sub_mul, mul_assoc, mul_assoc, I_mul_I, neg_one_mul, neg_neg, mul_one, one_mul, mul_assoc,
I_mul_I, mul_neg_one, sub_neg_eq_add, add_comm]
end
/-- The complex sine function is everywhere differentiable, with the derivative `cos x`. -/
lemma has_deriv_at_sin (x : ℂ) : has_deriv_at sin (cos x) x :=
(has_strict_deriv_at_sin x).has_deriv_at
lemma times_cont_diff_sin {n} : times_cont_diff ℂ n sin :=
(((times_cont_diff_neg.mul times_cont_diff_const).cexp.sub
(times_cont_diff_id.mul times_cont_diff_const).cexp).mul times_cont_diff_const).div_const
lemma differentiable_sin : differentiable ℂ sin :=
λx, (has_deriv_at_sin x).differentiable_at
lemma differentiable_at_sin {x : ℂ} : differentiable_at ℂ sin x :=
differentiable_sin x
@[simp] lemma deriv_sin : deriv sin = cos :=
funext $ λ x, (has_deriv_at_sin x).deriv
@[continuity]
lemma continuous_sin : continuous sin :=
differentiable_sin.continuous
lemma continuous_on_sin {s : set ℂ} : continuous_on sin s := continuous_sin.continuous_on
lemma measurable_sin : measurable sin := continuous_sin.measurable
/-- The complex cosine function is everywhere strictly differentiable, with the derivative
`-sin x`. -/
lemma has_strict_deriv_at_cos (x : ℂ) : has_strict_deriv_at cos (-sin x) x :=
begin
simp only [sin, div_eq_mul_inv, neg_mul_eq_neg_mul],
convert (((has_strict_deriv_at_id x).mul_const I).cexp.add
((has_strict_deriv_at_id x).neg.mul_const I).cexp).mul_const (2:ℂ)⁻¹,
simp only [function.comp, id],
ring
end
/-- The complex cosine function is everywhere differentiable, with the derivative `-sin x`. -/
lemma has_deriv_at_cos (x : ℂ) : has_deriv_at cos (-sin x) x :=
(has_strict_deriv_at_cos x).has_deriv_at
lemma times_cont_diff_cos {n} : times_cont_diff ℂ n cos :=
((times_cont_diff_id.mul times_cont_diff_const).cexp.add
(times_cont_diff_neg.mul times_cont_diff_const).cexp).div_const
lemma differentiable_cos : differentiable ℂ cos :=
λx, (has_deriv_at_cos x).differentiable_at
lemma differentiable_at_cos {x : ℂ} : differentiable_at ℂ cos x :=
differentiable_cos x
lemma deriv_cos {x : ℂ} : deriv cos x = -sin x :=
(has_deriv_at_cos x).deriv
@[simp] lemma deriv_cos' : deriv cos = (λ x, -sin x) :=
funext $ λ x, deriv_cos
@[continuity]
lemma continuous_cos : continuous cos :=
differentiable_cos.continuous
lemma continuous_on_cos {s : set ℂ} : continuous_on cos s := continuous_cos.continuous_on
lemma measurable_cos : measurable cos := continuous_cos.measurable
/-- The complex hyperbolic sine function is everywhere strictly differentiable, with the derivative
`cosh x`. -/
lemma has_strict_deriv_at_sinh (x : ℂ) : has_strict_deriv_at sinh (cosh x) x :=
begin
simp only [cosh, div_eq_mul_inv],
convert ((has_strict_deriv_at_exp x).sub (has_strict_deriv_at_id x).neg.cexp).mul_const (2:ℂ)⁻¹,
rw [id, mul_neg_one, sub_eq_add_neg, neg_neg]
end
/-- The complex hyperbolic sine function is everywhere differentiable, with the derivative
`cosh x`. -/
lemma has_deriv_at_sinh (x : ℂ) : has_deriv_at sinh (cosh x) x :=
(has_strict_deriv_at_sinh x).has_deriv_at
lemma times_cont_diff_sinh {n} : times_cont_diff ℂ n sinh :=
(times_cont_diff_exp.sub times_cont_diff_neg.cexp).div_const
lemma differentiable_sinh : differentiable ℂ sinh :=
λx, (has_deriv_at_sinh x).differentiable_at
lemma differentiable_at_sinh {x : ℂ} : differentiable_at ℂ sinh x :=
differentiable_sinh x
@[simp] lemma deriv_sinh : deriv sinh = cosh :=
funext $ λ x, (has_deriv_at_sinh x).deriv
@[continuity]
lemma continuous_sinh : continuous sinh :=
differentiable_sinh.continuous
lemma measurable_sinh : measurable sinh := continuous_sinh.measurable
/-- The complex hyperbolic cosine function is everywhere strictly differentiable, with the
derivative `sinh x`. -/
lemma has_strict_deriv_at_cosh (x : ℂ) : has_strict_deriv_at cosh (sinh x) x :=
begin
simp only [sinh, div_eq_mul_inv],
convert ((has_strict_deriv_at_exp x).add (has_strict_deriv_at_id x).neg.cexp).mul_const (2:ℂ)⁻¹,
rw [id, mul_neg_one, sub_eq_add_neg]
end
/-- The complex hyperbolic cosine function is everywhere differentiable, with the derivative
`sinh x`. -/
lemma has_deriv_at_cosh (x : ℂ) : has_deriv_at cosh (sinh x) x :=
(has_strict_deriv_at_cosh x).has_deriv_at
lemma times_cont_diff_cosh {n} : times_cont_diff ℂ n cosh :=
(times_cont_diff_exp.add times_cont_diff_neg.cexp).div_const
lemma differentiable_cosh : differentiable ℂ cosh :=
λx, (has_deriv_at_cosh x).differentiable_at
lemma differentiable_at_cosh {x : ℂ} : differentiable_at ℂ cos x :=
differentiable_cos x
@[simp] lemma deriv_cosh : deriv cosh = sinh :=
funext $ λ x, (has_deriv_at_cosh x).deriv
@[continuity]
lemma continuous_cosh : continuous cosh :=
differentiable_cosh.continuous
lemma measurable_cosh : measurable cosh := continuous_cosh.measurable
end complex
section
/-! ### Simp lemmas for derivatives of `λ x, complex.cos (f x)` etc., `f : ℂ → ℂ` -/
variables {f : ℂ → ℂ} {f' x : ℂ} {s : set ℂ}
/-! #### `complex.cos` -/
lemma measurable.ccos {α : Type*} [measurable_space α] {f : α → ℂ} (hf : measurable f) :
measurable (λ x, complex.cos (f x)) :=
complex.measurable_cos.comp hf
lemma has_strict_deriv_at.ccos (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, complex.cos (f x)) (- complex.sin (f x) * f') x :=
(complex.has_strict_deriv_at_cos (f x)).comp x hf
lemma has_deriv_at.ccos (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, complex.cos (f x)) (- complex.sin (f x) * f') x :=
(complex.has_deriv_at_cos (f x)).comp x hf
lemma has_deriv_within_at.ccos (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, complex.cos (f x)) (- complex.sin (f x) * f') s x :=
(complex.has_deriv_at_cos (f x)).comp_has_deriv_within_at x hf
lemma deriv_within_ccos (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
deriv_within (λx, complex.cos (f x)) s x = - complex.sin (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.ccos.deriv_within hxs
@[simp] lemma deriv_ccos (hc : differentiable_at ℂ f x) :
deriv (λx, complex.cos (f x)) x = - complex.sin (f x) * (deriv f x) :=
hc.has_deriv_at.ccos.deriv
/-! #### `complex.sin` -/
lemma measurable.csin {α : Type*} [measurable_space α] {f : α → ℂ} (hf : measurable f) :
measurable (λ x, complex.sin (f x)) :=
complex.measurable_sin.comp hf
lemma has_strict_deriv_at.csin (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, complex.sin (f x)) (complex.cos (f x) * f') x :=
(complex.has_strict_deriv_at_sin (f x)).comp x hf
lemma has_deriv_at.csin (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, complex.sin (f x)) (complex.cos (f x) * f') x :=
(complex.has_deriv_at_sin (f x)).comp x hf
lemma has_deriv_within_at.csin (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, complex.sin (f x)) (complex.cos (f x) * f') s x :=
(complex.has_deriv_at_sin (f x)).comp_has_deriv_within_at x hf
lemma deriv_within_csin (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
deriv_within (λx, complex.sin (f x)) s x = complex.cos (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.csin.deriv_within hxs
@[simp] lemma deriv_csin (hc : differentiable_at ℂ f x) :
deriv (λx, complex.sin (f x)) x = complex.cos (f x) * (deriv f x) :=
hc.has_deriv_at.csin.deriv
/-! #### `complex.cosh` -/
lemma measurable.ccosh {α : Type*} [measurable_space α] {f : α → ℂ} (hf : measurable f) :
measurable (λ x, complex.cosh (f x)) :=
complex.measurable_cosh.comp hf
lemma has_strict_deriv_at.ccosh (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, complex.cosh (f x)) (complex.sinh (f x) * f') x :=
(complex.has_strict_deriv_at_cosh (f x)).comp x hf
lemma has_deriv_at.ccosh (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, complex.cosh (f x)) (complex.sinh (f x) * f') x :=
(complex.has_deriv_at_cosh (f x)).comp x hf
lemma has_deriv_within_at.ccosh (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, complex.cosh (f x)) (complex.sinh (f x) * f') s x :=
(complex.has_deriv_at_cosh (f x)).comp_has_deriv_within_at x hf
lemma deriv_within_ccosh (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
deriv_within (λx, complex.cosh (f x)) s x = complex.sinh (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.ccosh.deriv_within hxs
@[simp] lemma deriv_ccosh (hc : differentiable_at ℂ f x) :
deriv (λx, complex.cosh (f x)) x = complex.sinh (f x) * (deriv f x) :=
hc.has_deriv_at.ccosh.deriv
/-! #### `complex.sinh` -/
lemma measurable.csinh {α : Type*} [measurable_space α] {f : α → ℂ} (hf : measurable f) :
measurable (λ x, complex.sinh (f x)) :=
complex.measurable_sinh.comp hf
lemma has_strict_deriv_at.csinh (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, complex.sinh (f x)) (complex.cosh (f x) * f') x :=
(complex.has_strict_deriv_at_sinh (f x)).comp x hf
lemma has_deriv_at.csinh (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, complex.sinh (f x)) (complex.cosh (f x) * f') x :=
(complex.has_deriv_at_sinh (f x)).comp x hf
lemma has_deriv_within_at.csinh (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, complex.sinh (f x)) (complex.cosh (f x) * f') s x :=
(complex.has_deriv_at_sinh (f x)).comp_has_deriv_within_at x hf
lemma deriv_within_csinh (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
deriv_within (λx, complex.sinh (f x)) s x = complex.cosh (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.csinh.deriv_within hxs
@[simp] lemma deriv_csinh (hc : differentiable_at ℂ f x) :
deriv (λx, complex.sinh (f x)) x = complex.cosh (f x) * (deriv f x) :=
hc.has_deriv_at.csinh.deriv
end
section
/-! ### Simp lemmas for derivatives of `λ x, complex.cos (f x)` etc., `f : E → ℂ` -/
variables {E : Type*} [normed_group E] [normed_space ℂ E] {f : E → ℂ} {f' : E →L[ℂ] ℂ}
{x : E} {s : set E}
/-! #### `complex.cos` -/
lemma has_strict_fderiv_at.ccos (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, complex.cos (f x)) (- complex.sin (f x) • f') x :=
(complex.has_strict_deriv_at_cos (f x)).comp_has_strict_fderiv_at x hf
lemma has_fderiv_at.ccos (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, complex.cos (f x)) (- complex.sin (f x) • f') x :=
(complex.has_deriv_at_cos (f x)).comp_has_fderiv_at x hf
lemma has_fderiv_within_at.ccos (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, complex.cos (f x)) (- complex.sin (f x) • f') s x :=
(complex.has_deriv_at_cos (f x)).comp_has_fderiv_within_at x hf
lemma differentiable_within_at.ccos (hf : differentiable_within_at ℂ f s x) :
differentiable_within_at ℂ (λ x, complex.cos (f x)) s x :=
hf.has_fderiv_within_at.ccos.differentiable_within_at
@[simp] lemma differentiable_at.ccos (hc : differentiable_at ℂ f x) :
differentiable_at ℂ (λx, complex.cos (f x)) x :=
hc.has_fderiv_at.ccos.differentiable_at
lemma differentiable_on.ccos (hc : differentiable_on ℂ f s) :
differentiable_on ℂ (λx, complex.cos (f x)) s :=
λx h, (hc x h).ccos
@[simp] lemma differentiable.ccos (hc : differentiable ℂ f) :
differentiable ℂ (λx, complex.cos (f x)) :=
λx, (hc x).ccos
lemma fderiv_within_ccos (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
fderiv_within ℂ (λx, complex.cos (f x)) s x = - complex.sin (f x) • (fderiv_within ℂ f s x) :=
hf.has_fderiv_within_at.ccos.fderiv_within hxs
@[simp] lemma fderiv_ccos (hc : differentiable_at ℂ f x) :
fderiv ℂ (λx, complex.cos (f x)) x = - complex.sin (f x) • (fderiv ℂ f x) :=
hc.has_fderiv_at.ccos.fderiv
lemma times_cont_diff.ccos {n} (h : times_cont_diff ℂ n f) :
times_cont_diff ℂ n (λ x, complex.cos (f x)) :=
complex.times_cont_diff_cos.comp h
lemma times_cont_diff_at.ccos {n} (hf : times_cont_diff_at ℂ n f x) :
times_cont_diff_at ℂ n (λ x, complex.cos (f x)) x :=
complex.times_cont_diff_cos.times_cont_diff_at.comp x hf
lemma times_cont_diff_on.ccos {n} (hf : times_cont_diff_on ℂ n f s) :
times_cont_diff_on ℂ n (λ x, complex.cos (f x)) s :=
complex.times_cont_diff_cos.comp_times_cont_diff_on hf
lemma times_cont_diff_within_at.ccos {n} (hf : times_cont_diff_within_at ℂ n f s x) :
times_cont_diff_within_at ℂ n (λ x, complex.cos (f x)) s x :=
complex.times_cont_diff_cos.times_cont_diff_at.comp_times_cont_diff_within_at x hf
/-! #### `complex.sin` -/
lemma has_strict_fderiv_at.csin (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, complex.sin (f x)) (complex.cos (f x) • f') x :=
(complex.has_strict_deriv_at_sin (f x)).comp_has_strict_fderiv_at x hf
lemma has_fderiv_at.csin (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, complex.sin (f x)) (complex.cos (f x) • f') x :=
(complex.has_deriv_at_sin (f x)).comp_has_fderiv_at x hf
lemma has_fderiv_within_at.csin (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, complex.sin (f x)) (complex.cos (f x) • f') s x :=
(complex.has_deriv_at_sin (f x)).comp_has_fderiv_within_at x hf
lemma differentiable_within_at.csin (hf : differentiable_within_at ℂ f s x) :
differentiable_within_at ℂ (λ x, complex.sin (f x)) s x :=
hf.has_fderiv_within_at.csin.differentiable_within_at
@[simp] lemma differentiable_at.csin (hc : differentiable_at ℂ f x) :
differentiable_at ℂ (λx, complex.sin (f x)) x :=
hc.has_fderiv_at.csin.differentiable_at
lemma differentiable_on.csin (hc : differentiable_on ℂ f s) :
differentiable_on ℂ (λx, complex.sin (f x)) s :=
λx h, (hc x h).csin
@[simp] lemma differentiable.csin (hc : differentiable ℂ f) :
differentiable ℂ (λx, complex.sin (f x)) :=
λx, (hc x).csin
lemma fderiv_within_csin (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
fderiv_within ℂ (λx, complex.sin (f x)) s x = complex.cos (f x) • (fderiv_within ℂ f s x) :=
hf.has_fderiv_within_at.csin.fderiv_within hxs
@[simp] lemma fderiv_csin (hc : differentiable_at ℂ f x) :
fderiv ℂ (λx, complex.sin (f x)) x = complex.cos (f x) • (fderiv ℂ f x) :=
hc.has_fderiv_at.csin.fderiv
lemma times_cont_diff.csin {n} (h : times_cont_diff ℂ n f) :
times_cont_diff ℂ n (λ x, complex.sin (f x)) :=
complex.times_cont_diff_sin.comp h
lemma times_cont_diff_at.csin {n} (hf : times_cont_diff_at ℂ n f x) :
times_cont_diff_at ℂ n (λ x, complex.sin (f x)) x :=
complex.times_cont_diff_sin.times_cont_diff_at.comp x hf
lemma times_cont_diff_on.csin {n} (hf : times_cont_diff_on ℂ n f s) :
times_cont_diff_on ℂ n (λ x, complex.sin (f x)) s :=
complex.times_cont_diff_sin.comp_times_cont_diff_on hf
lemma times_cont_diff_within_at.csin {n} (hf : times_cont_diff_within_at ℂ n f s x) :
times_cont_diff_within_at ℂ n (λ x, complex.sin (f x)) s x :=
complex.times_cont_diff_sin.times_cont_diff_at.comp_times_cont_diff_within_at x hf
/-! #### `complex.cosh` -/
lemma has_strict_fderiv_at.ccosh (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, complex.cosh (f x)) (complex.sinh (f x) • f') x :=
(complex.has_strict_deriv_at_cosh (f x)).comp_has_strict_fderiv_at x hf
lemma has_fderiv_at.ccosh (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, complex.cosh (f x)) (complex.sinh (f x) • f') x :=
(complex.has_deriv_at_cosh (f x)).comp_has_fderiv_at x hf
lemma has_fderiv_within_at.ccosh (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, complex.cosh (f x)) (complex.sinh (f x) • f') s x :=
(complex.has_deriv_at_cosh (f x)).comp_has_fderiv_within_at x hf
lemma differentiable_within_at.ccosh (hf : differentiable_within_at ℂ f s x) :
differentiable_within_at ℂ (λ x, complex.cosh (f x)) s x :=
hf.has_fderiv_within_at.ccosh.differentiable_within_at
@[simp] lemma differentiable_at.ccosh (hc : differentiable_at ℂ f x) :
differentiable_at ℂ (λx, complex.cosh (f x)) x :=
hc.has_fderiv_at.ccosh.differentiable_at
lemma differentiable_on.ccosh (hc : differentiable_on ℂ f s) :
differentiable_on ℂ (λx, complex.cosh (f x)) s :=
λx h, (hc x h).ccosh
@[simp] lemma differentiable.ccosh (hc : differentiable ℂ f) :
differentiable ℂ (λx, complex.cosh (f x)) :=
λx, (hc x).ccosh
lemma fderiv_within_ccosh (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
fderiv_within ℂ (λx, complex.cosh (f x)) s x = complex.sinh (f x) • (fderiv_within ℂ f s x) :=
hf.has_fderiv_within_at.ccosh.fderiv_within hxs
@[simp] lemma fderiv_ccosh (hc : differentiable_at ℂ f x) :
fderiv ℂ (λx, complex.cosh (f x)) x = complex.sinh (f x) • (fderiv ℂ f x) :=
hc.has_fderiv_at.ccosh.fderiv
lemma times_cont_diff.ccosh {n} (h : times_cont_diff ℂ n f) :
times_cont_diff ℂ n (λ x, complex.cosh (f x)) :=
complex.times_cont_diff_cosh.comp h
lemma times_cont_diff_at.ccosh {n} (hf : times_cont_diff_at ℂ n f x) :
times_cont_diff_at ℂ n (λ x, complex.cosh (f x)) x :=
complex.times_cont_diff_cosh.times_cont_diff_at.comp x hf
lemma times_cont_diff_on.ccosh {n} (hf : times_cont_diff_on ℂ n f s) :
times_cont_diff_on ℂ n (λ x, complex.cosh (f x)) s :=
complex.times_cont_diff_cosh.comp_times_cont_diff_on hf
lemma times_cont_diff_within_at.ccosh {n} (hf : times_cont_diff_within_at ℂ n f s x) :
times_cont_diff_within_at ℂ n (λ x, complex.cosh (f x)) s x :=
complex.times_cont_diff_cosh.times_cont_diff_at.comp_times_cont_diff_within_at x hf
/-! #### `complex.sinh` -/
lemma has_strict_fderiv_at.csinh (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, complex.sinh (f x)) (complex.cosh (f x) • f') x :=
(complex.has_strict_deriv_at_sinh (f x)).comp_has_strict_fderiv_at x hf
lemma has_fderiv_at.csinh (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, complex.sinh (f x)) (complex.cosh (f x) • f') x :=
(complex.has_deriv_at_sinh (f x)).comp_has_fderiv_at x hf
lemma has_fderiv_within_at.csinh (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, complex.sinh (f x)) (complex.cosh (f x) • f') s x :=
(complex.has_deriv_at_sinh (f x)).comp_has_fderiv_within_at x hf
lemma differentiable_within_at.csinh (hf : differentiable_within_at ℂ f s x) :
differentiable_within_at ℂ (λ x, complex.sinh (f x)) s x :=
hf.has_fderiv_within_at.csinh.differentiable_within_at
@[simp] lemma differentiable_at.csinh (hc : differentiable_at ℂ f x) :
differentiable_at ℂ (λx, complex.sinh (f x)) x :=
hc.has_fderiv_at.csinh.differentiable_at
lemma differentiable_on.csinh (hc : differentiable_on ℂ f s) :
differentiable_on ℂ (λx, complex.sinh (f x)) s :=
λx h, (hc x h).csinh
@[simp] lemma differentiable.csinh (hc : differentiable ℂ f) :
differentiable ℂ (λx, complex.sinh (f x)) :=
λx, (hc x).csinh
lemma fderiv_within_csinh (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
fderiv_within ℂ (λx, complex.sinh (f x)) s x = complex.cosh (f x) • (fderiv_within ℂ f s x) :=
hf.has_fderiv_within_at.csinh.fderiv_within hxs
@[simp] lemma fderiv_csinh (hc : differentiable_at ℂ f x) :
fderiv ℂ (λx, complex.sinh (f x)) x = complex.cosh (f x) • (fderiv ℂ f x) :=
hc.has_fderiv_at.csinh.fderiv
lemma times_cont_diff.csinh {n} (h : times_cont_diff ℂ n f) :
times_cont_diff ℂ n (λ x, complex.sinh (f x)) :=
complex.times_cont_diff_sinh.comp h
lemma times_cont_diff_at.csinh {n} (hf : times_cont_diff_at ℂ n f x) :
times_cont_diff_at ℂ n (λ x, complex.sinh (f x)) x :=
complex.times_cont_diff_sinh.times_cont_diff_at.comp x hf
lemma times_cont_diff_on.csinh {n} (hf : times_cont_diff_on ℂ n f s) :
times_cont_diff_on ℂ n (λ x, complex.sinh (f x)) s :=
complex.times_cont_diff_sinh.comp_times_cont_diff_on hf
lemma times_cont_diff_within_at.csinh {n} (hf : times_cont_diff_within_at ℂ n f s x) :
times_cont_diff_within_at ℂ n (λ x, complex.sinh (f x)) s x :=
complex.times_cont_diff_sinh.times_cont_diff_at.comp_times_cont_diff_within_at x hf
end
namespace real
variables {x y z : ℝ}
lemma has_strict_deriv_at_sin (x : ℝ) : has_strict_deriv_at sin (cos x) x :=
(complex.has_strict_deriv_at_sin x).real_of_complex
lemma has_deriv_at_sin (x : ℝ) : has_deriv_at sin (cos x) x :=
(has_strict_deriv_at_sin x).has_deriv_at
lemma times_cont_diff_sin {n} : times_cont_diff ℝ n sin :=
complex.times_cont_diff_sin.real_of_complex
lemma differentiable_sin : differentiable ℝ sin :=
λx, (has_deriv_at_sin x).differentiable_at
lemma differentiable_at_sin : differentiable_at ℝ sin x :=
differentiable_sin x
@[simp] lemma deriv_sin : deriv sin = cos :=
funext $ λ x, (has_deriv_at_sin x).deriv
@[continuity]
lemma continuous_sin : continuous sin :=
differentiable_sin.continuous
lemma continuous_on_sin {s} : continuous_on sin s :=
continuous_sin.continuous_on
lemma measurable_sin : measurable sin := continuous_sin.measurable
lemma has_strict_deriv_at_cos (x : ℝ) : has_strict_deriv_at cos (-sin x) x :=
(complex.has_strict_deriv_at_cos x).real_of_complex
lemma has_deriv_at_cos (x : ℝ) : has_deriv_at cos (-sin x) x :=
(complex.has_deriv_at_cos x).real_of_complex
lemma times_cont_diff_cos {n} : times_cont_diff ℝ n cos :=
complex.times_cont_diff_cos.real_of_complex
lemma differentiable_cos : differentiable ℝ cos :=
λx, (has_deriv_at_cos x).differentiable_at
lemma differentiable_at_cos : differentiable_at ℝ cos x :=
differentiable_cos x
lemma deriv_cos : deriv cos x = - sin x :=
(has_deriv_at_cos x).deriv
@[simp] lemma deriv_cos' : deriv cos = (λ x, - sin x) :=
funext $ λ _, deriv_cos
@[continuity]
lemma continuous_cos : continuous cos :=
differentiable_cos.continuous
lemma continuous_on_cos {s} : continuous_on cos s := continuous_cos.continuous_on
lemma measurable_cos : measurable cos := continuous_cos.measurable
lemma has_strict_deriv_at_sinh (x : ℝ) : has_strict_deriv_at sinh (cosh x) x :=
(complex.has_strict_deriv_at_sinh x).real_of_complex
lemma has_deriv_at_sinh (x : ℝ) : has_deriv_at sinh (cosh x) x :=
(complex.has_deriv_at_sinh x).real_of_complex
lemma times_cont_diff_sinh {n} : times_cont_diff ℝ n sinh :=
complex.times_cont_diff_sinh.real_of_complex
lemma differentiable_sinh : differentiable ℝ sinh :=
λx, (has_deriv_at_sinh x).differentiable_at
lemma differentiable_at_sinh : differentiable_at ℝ sinh x :=
differentiable_sinh x
@[simp] lemma deriv_sinh : deriv sinh = cosh :=
funext $ λ x, (has_deriv_at_sinh x).deriv
@[continuity]
lemma continuous_sinh : continuous sinh :=
differentiable_sinh.continuous
lemma measurable_sinh : measurable sinh := continuous_sinh.measurable
lemma has_strict_deriv_at_cosh (x : ℝ) : has_strict_deriv_at cosh (sinh x) x :=
(complex.has_strict_deriv_at_cosh x).real_of_complex
lemma has_deriv_at_cosh (x : ℝ) : has_deriv_at cosh (sinh x) x :=
(complex.has_deriv_at_cosh x).real_of_complex
lemma times_cont_diff_cosh {n} : times_cont_diff ℝ n cosh :=
complex.times_cont_diff_cosh.real_of_complex
lemma differentiable_cosh : differentiable ℝ cosh :=
λx, (has_deriv_at_cosh x).differentiable_at
lemma differentiable_at_cosh : differentiable_at ℝ cosh x :=
differentiable_cosh x
@[simp] lemma deriv_cosh : deriv cosh = sinh :=
funext $ λ x, (has_deriv_at_cosh x).deriv
@[continuity]
lemma continuous_cosh : continuous cosh :=
differentiable_cosh.continuous
lemma measurable_cosh : measurable cosh := continuous_cosh.measurable
/-- `sinh` is strictly monotone. -/
lemma sinh_strict_mono : strict_mono sinh :=
strict_mono_of_deriv_pos differentiable_sinh (by { rw [real.deriv_sinh], exact cosh_pos })
end real
section
/-! ### Simp lemmas for derivatives of `λ x, real.cos (f x)` etc., `f : ℝ → ℝ` -/
variables {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ}
/-! #### `real.cos` -/
lemma measurable.cos {α : Type*} [measurable_space α] {f : α → ℝ} (hf : measurable f) :
measurable (λ x, real.cos (f x)) :=
real.measurable_cos.comp hf
lemma has_strict_deriv_at.cos (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, real.cos (f x)) (- real.sin (f x) * f') x :=
(real.has_strict_deriv_at_cos (f x)).comp x hf
lemma has_deriv_at.cos (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, real.cos (f x)) (- real.sin (f x) * f') x :=
(real.has_deriv_at_cos (f x)).comp x hf
lemma has_deriv_within_at.cos (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, real.cos (f x)) (- real.sin (f x) * f') s x :=
(real.has_deriv_at_cos (f x)).comp_has_deriv_within_at x hf
lemma deriv_within_cos (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λx, real.cos (f x)) s x = - real.sin (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.cos.deriv_within hxs
@[simp] lemma deriv_cos (hc : differentiable_at ℝ f x) :
deriv (λx, real.cos (f x)) x = - real.sin (f x) * (deriv f x) :=
hc.has_deriv_at.cos.deriv
/-! #### `real.sin` -/
lemma measurable.sin {α : Type*} [measurable_space α] {f : α → ℝ} (hf : measurable f) :
measurable (λ x, real.sin (f x)) :=
real.measurable_sin.comp hf
lemma has_strict_deriv_at.sin (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, real.sin (f x)) (real.cos (f x) * f') x :=
(real.has_strict_deriv_at_sin (f x)).comp x hf
lemma has_deriv_at.sin (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, real.sin (f x)) (real.cos (f x) * f') x :=
(real.has_deriv_at_sin (f x)).comp x hf
lemma has_deriv_within_at.sin (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, real.sin (f x)) (real.cos (f x) * f') s x :=
(real.has_deriv_at_sin (f x)).comp_has_deriv_within_at x hf
lemma deriv_within_sin (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λx, real.sin (f x)) s x = real.cos (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.sin.deriv_within hxs
@[simp] lemma deriv_sin (hc : differentiable_at ℝ f x) :
deriv (λx, real.sin (f x)) x = real.cos (f x) * (deriv f x) :=
hc.has_deriv_at.sin.deriv
/-! #### `real.cosh` -/
lemma measurable.cosh {α : Type*} [measurable_space α] {f : α → ℝ} (hf : measurable f) :
measurable (λ x, real.cosh (f x)) :=
real.measurable_cosh.comp hf
lemma has_strict_deriv_at.cosh (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, real.cosh (f x)) (real.sinh (f x) * f') x :=
(real.has_strict_deriv_at_cosh (f x)).comp x hf
lemma has_deriv_at.cosh (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, real.cosh (f x)) (real.sinh (f x) * f') x :=
(real.has_deriv_at_cosh (f x)).comp x hf
lemma has_deriv_within_at.cosh (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, real.cosh (f x)) (real.sinh (f x) * f') s x :=
(real.has_deriv_at_cosh (f x)).comp_has_deriv_within_at x hf
lemma deriv_within_cosh (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λx, real.cosh (f x)) s x = real.sinh (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.cosh.deriv_within hxs
@[simp] lemma deriv_cosh (hc : differentiable_at ℝ f x) :
deriv (λx, real.cosh (f x)) x = real.sinh (f x) * (deriv f x) :=
hc.has_deriv_at.cosh.deriv
/-! #### `real.sinh` -/
lemma measurable.sinh {α : Type*} [measurable_space α] {f : α → ℝ} (hf : measurable f) :
measurable (λ x, real.sinh (f x)) :=
real.measurable_sinh.comp hf
lemma has_strict_deriv_at.sinh (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, real.sinh (f x)) (real.cosh (f x) * f') x :=
(real.has_strict_deriv_at_sinh (f x)).comp x hf
lemma has_deriv_at.sinh (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, real.sinh (f x)) (real.cosh (f x) * f') x :=
(real.has_deriv_at_sinh (f x)).comp x hf
lemma has_deriv_within_at.sinh (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, real.sinh (f x)) (real.cosh (f x) * f') s x :=
(real.has_deriv_at_sinh (f x)).comp_has_deriv_within_at x hf
lemma deriv_within_sinh (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λx, real.sinh (f x)) s x = real.cosh (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.sinh.deriv_within hxs
@[simp] lemma deriv_sinh (hc : differentiable_at ℝ f x) :
deriv (λx, real.sinh (f x)) x = real.cosh (f x) * (deriv f x) :=
hc.has_deriv_at.sinh.deriv
end
section
/-! ### Simp lemmas for derivatives of `λ x, real.cos (f x)` etc., `f : E → ℝ` -/
variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {f' : E →L[ℝ] ℝ}
{x : E} {s : set E}
/-! #### `real.cos` -/
lemma has_strict_fderiv_at.cos (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, real.cos (f x)) (- real.sin (f x) • f') x :=
(real.has_strict_deriv_at_cos (f x)).comp_has_strict_fderiv_at x hf
lemma has_fderiv_at.cos (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, real.cos (f x)) (- real.sin (f x) • f') x :=
(real.has_deriv_at_cos (f x)).comp_has_fderiv_at x hf
lemma has_fderiv_within_at.cos (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, real.cos (f x)) (- real.sin (f x) • f') s x :=
(real.has_deriv_at_cos (f x)).comp_has_fderiv_within_at x hf
lemma differentiable_within_at.cos (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ x, real.cos (f x)) s x :=
hf.has_fderiv_within_at.cos.differentiable_within_at
@[simp] lemma differentiable_at.cos (hc : differentiable_at ℝ f x) :
differentiable_at ℝ (λx, real.cos (f x)) x :=
hc.has_fderiv_at.cos.differentiable_at
lemma differentiable_on.cos (hc : differentiable_on ℝ f s) :
differentiable_on ℝ (λx, real.cos (f x)) s :=
λx h, (hc x h).cos
@[simp] lemma differentiable.cos (hc : differentiable ℝ f) :
differentiable ℝ (λx, real.cos (f x)) :=
λx, (hc x).cos
lemma fderiv_within_cos (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
fderiv_within ℝ (λx, real.cos (f x)) s x = - real.sin (f x) • (fderiv_within ℝ f s x) :=
hf.has_fderiv_within_at.cos.fderiv_within hxs
@[simp] lemma fderiv_cos (hc : differentiable_at ℝ f x) :
fderiv ℝ (λx, real.cos (f x)) x = - real.sin (f x) • (fderiv ℝ f x) :=
hc.has_fderiv_at.cos.fderiv
lemma times_cont_diff.cos {n} (h : times_cont_diff ℝ n f) :
times_cont_diff ℝ n (λ x, real.cos (f x)) :=
real.times_cont_diff_cos.comp h
lemma times_cont_diff_at.cos {n} (hf : times_cont_diff_at ℝ n f x) :
times_cont_diff_at ℝ n (λ x, real.cos (f x)) x :=
real.times_cont_diff_cos.times_cont_diff_at.comp x hf
lemma times_cont_diff_on.cos {n} (hf : times_cont_diff_on ℝ n f s) :
times_cont_diff_on ℝ n (λ x, real.cos (f x)) s :=
real.times_cont_diff_cos.comp_times_cont_diff_on hf
lemma times_cont_diff_within_at.cos {n} (hf : times_cont_diff_within_at ℝ n f s x) :
times_cont_diff_within_at ℝ n (λ x, real.cos (f x)) s x :=
real.times_cont_diff_cos.times_cont_diff_at.comp_times_cont_diff_within_at x hf
/-! #### `real.sin` -/
lemma has_strict_fderiv_at.sin (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, real.sin (f x)) (real.cos (f x) • f') x :=
(real.has_strict_deriv_at_sin (f x)).comp_has_strict_fderiv_at x hf
lemma has_fderiv_at.sin (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, real.sin (f x)) (real.cos (f x) • f') x :=
(real.has_deriv_at_sin (f x)).comp_has_fderiv_at x hf
lemma has_fderiv_within_at.sin (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, real.sin (f x)) (real.cos (f x) • f') s x :=
(real.has_deriv_at_sin (f x)).comp_has_fderiv_within_at x hf
lemma differentiable_within_at.sin (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ x, real.sin (f x)) s x :=
hf.has_fderiv_within_at.sin.differentiable_within_at
@[simp] lemma differentiable_at.sin (hc : differentiable_at ℝ f x) :
differentiable_at ℝ (λx, real.sin (f x)) x :=
hc.has_fderiv_at.sin.differentiable_at
lemma differentiable_on.sin (hc : differentiable_on ℝ f s) :
differentiable_on ℝ (λx, real.sin (f x)) s :=
λx h, (hc x h).sin
@[simp] lemma differentiable.sin (hc : differentiable ℝ f) :
differentiable ℝ (λx, real.sin (f x)) :=
λx, (hc x).sin
lemma fderiv_within_sin (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
fderiv_within ℝ (λx, real.sin (f x)) s x = real.cos (f x) • (fderiv_within ℝ f s x) :=
hf.has_fderiv_within_at.sin.fderiv_within hxs
@[simp] lemma fderiv_sin (hc : differentiable_at ℝ f x) :
fderiv ℝ (λx, real.sin (f x)) x = real.cos (f x) • (fderiv ℝ f x) :=
hc.has_fderiv_at.sin.fderiv
lemma times_cont_diff.sin {n} (h : times_cont_diff ℝ n f) :
times_cont_diff ℝ n (λ x, real.sin (f x)) :=
real.times_cont_diff_sin.comp h
lemma times_cont_diff_at.sin {n} (hf : times_cont_diff_at ℝ n f x) :
times_cont_diff_at ℝ n (λ x, real.sin (f x)) x :=
real.times_cont_diff_sin.times_cont_diff_at.comp x hf
lemma times_cont_diff_on.sin {n} (hf : times_cont_diff_on ℝ n f s) :
times_cont_diff_on ℝ n (λ x, real.sin (f x)) s :=
real.times_cont_diff_sin.comp_times_cont_diff_on hf
lemma times_cont_diff_within_at.sin {n} (hf : times_cont_diff_within_at ℝ n f s x) :
times_cont_diff_within_at ℝ n (λ x, real.sin (f x)) s x :=
real.times_cont_diff_sin.times_cont_diff_at.comp_times_cont_diff_within_at x hf
/-! #### `real.cosh` -/
lemma has_strict_fderiv_at.cosh (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, real.cosh (f x)) (real.sinh (f x) • f') x :=
(real.has_strict_deriv_at_cosh (f x)).comp_has_strict_fderiv_at x hf
lemma has_fderiv_at.cosh (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, real.cosh (f x)) (real.sinh (f x) • f') x :=
(real.has_deriv_at_cosh (f x)).comp_has_fderiv_at x hf
lemma has_fderiv_within_at.cosh (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, real.cosh (f x)) (real.sinh (f x) • f') s x :=
(real.has_deriv_at_cosh (f x)).comp_has_fderiv_within_at x hf
lemma differentiable_within_at.cosh (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ x, real.cosh (f x)) s x :=
hf.has_fderiv_within_at.cosh.differentiable_within_at
@[simp] lemma differentiable_at.cosh (hc : differentiable_at ℝ f x) :
differentiable_at ℝ (λx, real.cosh (f x)) x :=
hc.has_fderiv_at.cosh.differentiable_at
lemma differentiable_on.cosh (hc : differentiable_on ℝ f s) :
differentiable_on ℝ (λx, real.cosh (f x)) s :=
λx h, (hc x h).cosh
@[simp] lemma differentiable.cosh (hc : differentiable ℝ f) :
differentiable ℝ (λx, real.cosh (f x)) :=
λx, (hc x).cosh
lemma fderiv_within_cosh (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
fderiv_within ℝ (λx, real.cosh (f x)) s x = real.sinh (f x) • (fderiv_within ℝ f s x) :=
hf.has_fderiv_within_at.cosh.fderiv_within hxs
@[simp] lemma fderiv_cosh (hc : differentiable_at ℝ f x) :
fderiv ℝ (λx, real.cosh (f x)) x = real.sinh (f x) • (fderiv ℝ f x) :=
hc.has_fderiv_at.cosh.fderiv
lemma times_cont_diff.cosh {n} (h : times_cont_diff ℝ n f) :
times_cont_diff ℝ n (λ x, real.cosh (f x)) :=
real.times_cont_diff_cosh.comp h
lemma times_cont_diff_at.cosh {n} (hf : times_cont_diff_at ℝ n f x) :
times_cont_diff_at ℝ n (λ x, real.cosh (f x)) x :=
real.times_cont_diff_cosh.times_cont_diff_at.comp x hf
lemma times_cont_diff_on.cosh {n} (hf : times_cont_diff_on ℝ n f s) :
times_cont_diff_on ℝ n (λ x, real.cosh (f x)) s :=
real.times_cont_diff_cosh.comp_times_cont_diff_on hf
lemma times_cont_diff_within_at.cosh {n} (hf : times_cont_diff_within_at ℝ n f s x) :
times_cont_diff_within_at ℝ n (λ x, real.cosh (f x)) s x :=
real.times_cont_diff_cosh.times_cont_diff_at.comp_times_cont_diff_within_at x hf
/-! #### `real.sinh` -/
lemma has_strict_fderiv_at.sinh (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, real.sinh (f x)) (real.cosh (f x) • f') x :=
(real.has_strict_deriv_at_sinh (f x)).comp_has_strict_fderiv_at x hf
lemma has_fderiv_at.sinh (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, real.sinh (f x)) (real.cosh (f x) • f') x :=
(real.has_deriv_at_sinh (f x)).comp_has_fderiv_at x hf
lemma has_fderiv_within_at.sinh (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, real.sinh (f x)) (real.cosh (f x) • f') s x :=
(real.has_deriv_at_sinh (f x)).comp_has_fderiv_within_at x hf
lemma differentiable_within_at.sinh (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ x, real.sinh (f x)) s x :=
hf.has_fderiv_within_at.sinh.differentiable_within_at
@[simp] lemma differentiable_at.sinh (hc : differentiable_at ℝ f x) :
differentiable_at ℝ (λx, real.sinh (f x)) x :=
hc.has_fderiv_at.sinh.differentiable_at
lemma differentiable_on.sinh (hc : differentiable_on ℝ f s) :
differentiable_on ℝ (λx, real.sinh (f x)) s :=
λx h, (hc x h).sinh
@[simp] lemma differentiable.sinh (hc : differentiable ℝ f) :
differentiable ℝ (λx, real.sinh (f x)) :=
λx, (hc x).sinh
lemma fderiv_within_sinh (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
fderiv_within ℝ (λx, real.sinh (f x)) s x = real.cosh (f x) • (fderiv_within ℝ f s x) :=
hf.has_fderiv_within_at.sinh.fderiv_within hxs
@[simp] lemma fderiv_sinh (hc : differentiable_at ℝ f x) :
fderiv ℝ (λx, real.sinh (f x)) x = real.cosh (f x) • (fderiv ℝ f x) :=
hc.has_fderiv_at.sinh.fderiv
lemma times_cont_diff.sinh {n} (h : times_cont_diff ℝ n f) :
times_cont_diff ℝ n (λ x, real.sinh (f x)) :=
real.times_cont_diff_sinh.comp h
lemma times_cont_diff_at.sinh {n} (hf : times_cont_diff_at ℝ n f x) :
times_cont_diff_at ℝ n (λ x, real.sinh (f x)) x :=
real.times_cont_diff_sinh.times_cont_diff_at.comp x hf
lemma times_cont_diff_on.sinh {n} (hf : times_cont_diff_on ℝ n f s) :
times_cont_diff_on ℝ n (λ x, real.sinh (f x)) s :=
real.times_cont_diff_sinh.comp_times_cont_diff_on hf
lemma times_cont_diff_within_at.sinh {n} (hf : times_cont_diff_within_at ℝ n f s x) :
times_cont_diff_within_at ℝ n (λ x, real.sinh (f x)) s x :=
real.times_cont_diff_sinh.times_cont_diff_at.comp_times_cont_diff_within_at x hf
end
namespace real
lemma exists_cos_eq_zero : 0 ∈ cos '' Icc (1:ℝ) 2 :=
intermediate_value_Icc' (by norm_num) continuous_on_cos
⟨le_of_lt cos_two_neg, le_of_lt cos_one_pos⟩
/-- The number π = 3.14159265... Defined here using choice as twice a zero of cos in [1,2], from
which one can derive all its properties. For explicit bounds on π, see `data.real.pi`. -/
protected noncomputable def pi : ℝ := 2 * classical.some exists_cos_eq_zero
localized "notation `π` := real.pi" in real
@[simp] lemma cos_pi_div_two : cos (π / 2) = 0 :=
by rw [real.pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)];
exact (classical.some_spec exists_cos_eq_zero).2
lemma one_le_pi_div_two : (1 : ℝ) ≤ π / 2 :=
by rw [real.pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)];
exact (classical.some_spec exists_cos_eq_zero).1.1
lemma pi_div_two_le_two : π / 2 ≤ 2 :=
by rw [real.pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)];
exact (classical.some_spec exists_cos_eq_zero).1.2
lemma two_le_pi : (2 : ℝ) ≤ π :=
(div_le_div_right (show (0 : ℝ) < 2, by norm_num)).1
(by rw div_self (@two_ne_zero' ℝ _ _ _); exact one_le_pi_div_two)
lemma pi_le_four : π ≤ 4 :=
(div_le_div_right (show (0 : ℝ) < 2, by norm_num)).1
(calc π / 2 ≤ 2 : pi_div_two_le_two
... = 4 / 2 : by norm_num)
lemma pi_pos : 0 < π :=
lt_of_lt_of_le (by norm_num) two_le_pi
lemma pi_ne_zero : π ≠ 0 :=
ne_of_gt pi_pos
lemma pi_div_two_pos : 0 < π / 2 :=
half_pos pi_pos
lemma two_pi_pos : 0 < 2 * π :=
by linarith [pi_pos]
end real
namespace nnreal
open real
open_locale real nnreal
/-- `π` considered as a nonnegative real. -/
noncomputable def pi : ℝ≥0 := ⟨π, real.pi_pos.le⟩
@[simp] lemma coe_real_pi : (pi : ℝ) = π := rfl
lemma pi_pos : 0 < pi := by exact_mod_cast real.pi_pos
lemma pi_ne_zero : pi ≠ 0 := pi_pos.ne'
end nnreal
namespace real
open_locale real
@[simp] lemma sin_pi : sin π = 0 :=
by rw [← mul_div_cancel_left π (@two_ne_zero ℝ _ _), two_mul, add_div,
sin_add, cos_pi_div_two]; simp
@[simp] lemma cos_pi : cos π = -1 :=
by rw [← mul_div_cancel_left π (@two_ne_zero ℝ _ _), mul_div_assoc,
cos_two_mul, cos_pi_div_two];
simp [bit0, pow_add]
@[simp] lemma sin_two_pi : sin (2 * π) = 0 :=
by simp [two_mul, sin_add]
@[simp] lemma cos_two_pi : cos (2 * π) = 1 :=
by simp [two_mul, cos_add]
lemma sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 :=
by induction n; simp [add_mul, sin_add, *]
lemma sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 :=
by cases n; simp [add_mul, sin_add, *, sin_nat_mul_pi]
lemma cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 :=
by induction n; simp [*, mul_add, cos_add, add_mul, cos_two_pi, sin_two_pi]
lemma cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 :=
by cases n; simp only [cos_nat_mul_two_pi, int.of_nat_eq_coe, int.neg_succ_of_nat_coe,
int.cast_coe_nat, int.cast_neg, ← neg_mul_eq_neg_mul, cos_neg]
lemma sin_add_pi (x : ℝ) : sin (x + π) = -sin x :=
by simp [sin_add]
lemma sin_add_int_mul_two_pi (x : ℝ) (n : ℤ) : sin (x + n * (2 * π)) = sin x :=
begin
rw [sin_add, cos_int_mul_two_pi, ← mul_assoc],
rw_mod_cast sin_int_mul_pi (n*2),
simp,
end
lemma sin_sub_int_mul_two_pi (x : ℝ) (n : ℤ) : sin (x - n * (2 * π)) = sin x :=
by simpa using sin_add_int_mul_two_pi x (-n)
lemma sin_add_nat_mul_two_pi (x : ℝ) (n : ℕ) : sin (x + n * (2 * π)) = sin x :=
by convert sin_add_int_mul_two_pi x n
lemma sin_sub_nat_mul_two_pi (x : ℝ) (n : ℕ) : sin (x - n * (2 * π)) = sin x :=
by convert sin_sub_int_mul_two_pi x n
lemma sin_add_two_pi (x : ℝ) : sin (x + 2 * π) = sin x :=
by simp [sin_add]
lemma sin_sub_two_pi (x : ℝ) : sin (x - 2 * π) = sin x :=
by simp [sin_sub]
lemma cos_add_int_mul_two_pi (x : ℝ) (n : ℤ) : cos (x + n * (2 * π)) = cos x :=
begin
rw [cos_add, cos_int_mul_two_pi, ← mul_assoc],
rw_mod_cast sin_int_mul_pi (n*2),
simp,
end
lemma cos_sub_int_mul_two_pi (x : ℝ) (n : ℤ) : cos (x - n * (2 * π)) = cos x :=
by simpa using cos_add_int_mul_two_pi x (-n)
lemma cos_add_nat_mul_two_pi (x : ℝ) (n : ℕ) : cos (x + n * (2 * π)) = cos x :=
by convert cos_add_int_mul_two_pi x n
lemma cos_sub_nat_mul_two_pi (x : ℝ) (n : ℕ) : cos (x - n * (2 * π)) = cos x :=
by convert cos_sub_int_mul_two_pi x n
lemma cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 :=
by simp [add_comm, cos_add_int_mul_two_pi]
lemma cos_int_mul_two_pi_sub_pi (n : ℤ) : cos (n * (2 * π) - π) = -1 :=
by simp [sub_eq_neg_add, cos_add_int_mul_two_pi]
lemma cos_nat_mul_two_pi_add_pi (n : ℕ) : cos (n * (2 * π) + π) = -1 :=
by convert cos_int_mul_two_pi_add_pi n
lemma cos_nat_mul_two_pi_sub_pi (n : ℕ) : cos (n * (2 * π) - π) = -1 :=
by convert cos_int_mul_two_pi_sub_pi n
lemma cos_add_two_pi (x : ℝ) : cos (x + 2 * π) = cos x :=
by simp [cos_add]
lemma cos_sub_two_pi (x : ℝ) : cos (x - 2 * π) = cos x :=
by simp [cos_sub]
lemma sin_pi_sub (x : ℝ) : sin (π - x) = sin x :=
by simp [sub_eq_add_neg, sin_add]
lemma cos_add_pi (x : ℝ) : cos (x + π) = -cos x :=
by simp [cos_add]
lemma cos_sub_pi (x : ℝ) : cos (x - π) = -cos x :=
by simp [cos_sub]
lemma cos_pi_sub (x : ℝ) : cos (π - x) = -cos x :=
by simp [cos_sub]
lemma sin_pos_of_pos_of_lt_pi {x : ℝ} (h0x : 0 < x) (hxp : x < π) : 0 < sin x :=
if hx2 : x ≤ 2 then sin_pos_of_pos_of_le_two h0x hx2
else
have (2 : ℝ) + 2 = 4, from rfl,
have π - x ≤ 2, from sub_le_iff_le_add.2
(le_trans pi_le_four (this ▸ add_le_add_left (le_of_not_ge hx2) _)),
sin_pi_sub x ▸ sin_pos_of_pos_of_le_two (sub_pos.2 hxp) this
lemma sin_pos_of_mem_Ioo {x : ℝ} (hx : x ∈ Ioo 0 π) : 0 < sin x :=
sin_pos_of_pos_of_lt_pi hx.1 hx.2
lemma sin_nonneg_of_mem_Icc {x : ℝ} (hx : x ∈ Icc 0 π) : 0 ≤ sin x :=
begin
rw ← closure_Ioo pi_pos at hx,
exact closure_lt_subset_le continuous_const continuous_sin
(closure_mono (λ y, sin_pos_of_mem_Ioo) hx)
end
lemma sin_nonneg_of_nonneg_of_le_pi {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π) : 0 ≤ sin x :=
sin_nonneg_of_mem_Icc ⟨h0x, hxp⟩
lemma sin_neg_of_neg_of_neg_pi_lt {x : ℝ} (hx0 : x < 0) (hpx : -π < x) : sin x < 0 :=
neg_pos.1 $ sin_neg x ▸ sin_pos_of_pos_of_lt_pi (neg_pos.2 hx0) (neg_lt.1 hpx)
lemma sin_nonpos_of_nonnpos_of_neg_pi_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -π ≤ x) : sin x ≤ 0 :=
neg_nonneg.1 $ sin_neg x ▸ sin_nonneg_of_nonneg_of_le_pi (neg_nonneg.2 hx0) (neg_le.1 hpx)
@[simp] lemma sin_pi_div_two : sin (π / 2) = 1 :=
have sin (π / 2) = 1 ∨ sin (π / 2) = -1 :=
by simpa [sq, mul_self_eq_one_iff] using sin_sq_add_cos_sq (π / 2),
this.resolve_right
(λ h, (show ¬(0 : ℝ) < -1, by norm_num) $
h ▸ sin_pos_of_pos_of_lt_pi pi_div_two_pos (half_lt_self pi_pos))
lemma sin_add_pi_div_two (x : ℝ) : sin (x + π / 2) = cos x :=
by simp [sin_add]
lemma sin_sub_pi_div_two (x : ℝ) : sin (x - π / 2) = -cos x :=
by simp [sub_eq_add_neg, sin_add]
lemma sin_pi_div_two_sub (x : ℝ) : sin (π / 2 - x) = cos x :=
by simp [sub_eq_add_neg, sin_add]
lemma cos_add_pi_div_two (x : ℝ) : cos (x + π / 2) = -sin x :=
by simp [cos_add]
lemma cos_sub_pi_div_two (x : ℝ) : cos (x - π / 2) = sin x :=
by simp [sub_eq_add_neg, cos_add]
lemma cos_pi_div_two_sub (x : ℝ) : cos (π / 2 - x) = sin x :=
by rw [← cos_neg, neg_sub, cos_sub_pi_div_two]
lemma cos_pos_of_mem_Ioo {x : ℝ} (hx : x ∈ Ioo (-(π / 2)) (π / 2)) : 0 < cos x :=
sin_add_pi_div_two x ▸ sin_pos_of_mem_Ioo ⟨by linarith [hx.1], by linarith [hx.2]⟩
lemma cos_nonneg_of_mem_Icc {x : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) : 0 ≤ cos x :=
sin_add_pi_div_two x ▸ sin_nonneg_of_mem_Icc ⟨by linarith [hx.1], by linarith [hx.2]⟩
lemma cos_nonneg_of_neg_pi_div_two_le_of_le {x : ℝ} (hl : -(π / 2) ≤ x) (hu : x ≤ π / 2) :
0 ≤ cos x :=
cos_nonneg_of_mem_Icc ⟨hl, hu⟩
lemma cos_neg_of_pi_div_two_lt_of_lt {x : ℝ} (hx₁ : π / 2 < x) (hx₂ : x < π + π / 2) : cos x < 0 :=
neg_pos.1 $ cos_pi_sub x ▸ cos_pos_of_mem_Ioo ⟨by linarith, by linarith⟩
lemma cos_nonpos_of_pi_div_two_le_of_le {x : ℝ} (hx₁ : π / 2 ≤ x) (hx₂ : x ≤ π + π / 2) :
cos x ≤ 0 :=
neg_nonneg.1 $ cos_pi_sub x ▸ cos_nonneg_of_mem_Icc ⟨by linarith, by linarith⟩
lemma sin_eq_sqrt_one_sub_cos_sq {x : ℝ} (hl : 0 ≤ x) (hu : x ≤ π) :
sin x = sqrt (1 - cos x ^ 2) :=
by rw [← abs_sin_eq_sqrt_one_sub_cos_sq, abs_of_nonneg (sin_nonneg_of_nonneg_of_le_pi hl hu)]
lemma cos_eq_sqrt_one_sub_sin_sq {x : ℝ} (hl : -(π / 2) ≤ x) (hu : x ≤ π / 2) :
cos x = sqrt (1 - sin x ^ 2) :=
by rw [← abs_cos_eq_sqrt_one_sub_sin_sq, abs_of_nonneg (cos_nonneg_of_mem_Icc ⟨hl, hu⟩)]
lemma sin_eq_zero_iff_of_lt_of_lt {x : ℝ} (hx₁ : -π < x) (hx₂ : x < π) :
sin x = 0 ↔ x = 0 :=
⟨λ h, le_antisymm
(le_of_not_gt (λ h0, lt_irrefl (0 : ℝ) $
calc 0 < sin x : sin_pos_of_pos_of_lt_pi h0 hx₂
... = 0 : h))
(le_of_not_gt (λ h0, lt_irrefl (0 : ℝ) $
calc 0 = sin x : h.symm
... < 0 : sin_neg_of_neg_of_neg_pi_lt h0 hx₁)),
λ h, by simp [h]⟩
lemma sin_eq_zero_iff {x : ℝ} : sin x = 0 ↔ ∃ n : ℤ, (n : ℝ) * π = x :=
⟨λ h, ⟨⌊x / π⌋, le_antisymm (sub_nonneg.1 (sub_floor_div_mul_nonneg _ pi_pos))
(sub_nonpos.1 $ le_of_not_gt $ λ h₃,
(sin_pos_of_pos_of_lt_pi h₃ (sub_floor_div_mul_lt _ pi_pos)).ne
(by simp [sub_eq_add_neg, sin_add, h, sin_int_mul_pi]))⟩,
λ ⟨n, hn⟩, hn ▸ sin_int_mul_pi _⟩
lemma sin_ne_zero_iff {x : ℝ} : sin x ≠ 0 ↔ ∀ n : ℤ, (n : ℝ) * π ≠ x :=
by rw [← not_exists, not_iff_not, sin_eq_zero_iff]
lemma sin_eq_zero_iff_cos_eq {x : ℝ} : sin x = 0 ↔ cos x = 1 ∨ cos x = -1 :=
by rw [← mul_self_eq_one_iff, ← sin_sq_add_cos_sq x,
sq, sq, ← sub_eq_iff_eq_add, sub_self];
exact ⟨λ h, by rw [h, mul_zero], eq_zero_of_mul_self_eq_zero ∘ eq.symm⟩
lemma cos_eq_one_iff (x : ℝ) : cos x = 1 ↔ ∃ n : ℤ, (n : ℝ) * (2 * π) = x :=
⟨λ h, let ⟨n, hn⟩ := sin_eq_zero_iff.1 (sin_eq_zero_iff_cos_eq.2 (or.inl h)) in
⟨n / 2, (int.mod_two_eq_zero_or_one n).elim
(λ hn0, by rwa [← mul_assoc, ← @int.cast_two ℝ, ← int.cast_mul, int.div_mul_cancel
((int.dvd_iff_mod_eq_zero _ _).2 hn0)])
(λ hn1, by rw [← int.mod_add_div n 2, hn1, int.cast_add, int.cast_one, add_mul,
one_mul, add_comm, mul_comm (2 : ℤ), int.cast_mul, mul_assoc, int.cast_two] at hn;
rw [← hn, cos_int_mul_two_pi_add_pi] at h;
exact absurd h (by norm_num))⟩,
λ ⟨n, hn⟩, hn ▸ cos_int_mul_two_pi _⟩
lemma cos_eq_one_iff_of_lt_of_lt {x : ℝ} (hx₁ : -(2 * π) < x) (hx₂ : x < 2 * π) :
cos x = 1 ↔ x = 0 :=
⟨λ h,
begin
rcases (cos_eq_one_iff _).1 h with ⟨n, rfl⟩,
rw [mul_lt_iff_lt_one_left two_pi_pos] at hx₂,
rw [neg_lt, neg_mul_eq_neg_mul, mul_lt_iff_lt_one_left two_pi_pos] at hx₁,
norm_cast at hx₁ hx₂,
obtain rfl : n = 0 := le_antisymm (by linarith) (by linarith),
simp
end,
λ h, by simp [h]⟩
lemma cos_lt_cos_of_nonneg_of_le_pi_div_two {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y ≤ π / 2)
(hxy : x < y) :
cos y < cos x :=
begin
rw [← sub_lt_zero, cos_sub_cos],
have : 0 < sin ((y + x) / 2),
{ refine sin_pos_of_pos_of_lt_pi _ _; linarith },
have : 0 < sin ((y - x) / 2),
{ refine sin_pos_of_pos_of_lt_pi _ _; linarith },
nlinarith,
end
lemma cos_lt_cos_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y ≤ π) (hxy : x < y) :
cos y < cos x :=
match (le_total x (π / 2) : x ≤ π / 2 ∨ π / 2 ≤ x), le_total y (π / 2) with
| or.inl hx, or.inl hy := cos_lt_cos_of_nonneg_of_le_pi_div_two hx₁ hy hxy
| or.inl hx, or.inr hy := (lt_or_eq_of_le hx).elim
(λ hx, calc cos y ≤ 0 : cos_nonpos_of_pi_div_two_le_of_le hy (by linarith [pi_pos])
... < cos x : cos_pos_of_mem_Ioo ⟨by linarith, hx⟩)
(λ hx, calc cos y < 0 : cos_neg_of_pi_div_two_lt_of_lt (by linarith) (by linarith [pi_pos])
... = cos x : by rw [hx, cos_pi_div_two])
| or.inr hx, or.inl hy := by linarith
| or.inr hx, or.inr hy := neg_lt_neg_iff.1 (by rw [← cos_pi_sub, ← cos_pi_sub];
apply cos_lt_cos_of_nonneg_of_le_pi_div_two; linarith)
end
lemma strict_mono_decr_on_cos : strict_mono_decr_on cos (Icc 0 π) :=
λ x hx y hy hxy, cos_lt_cos_of_nonneg_of_le_pi hx.1 hy.2 hxy
lemma cos_le_cos_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y ≤ π) (hxy : x ≤ y) :
cos y ≤ cos x :=
(strict_mono_decr_on_cos.le_iff_le ⟨hx₁.trans hxy, hy₂⟩ ⟨hx₁, hxy.trans hy₂⟩).2 hxy
lemma sin_lt_sin_of_lt_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x)
(hy₂ : y ≤ π / 2) (hxy : x < y) : sin x < sin y :=
by rw [← cos_sub_pi_div_two, ← cos_sub_pi_div_two, ← cos_neg (x - _), ← cos_neg (y - _)];
apply cos_lt_cos_of_nonneg_of_le_pi; linarith
lemma strict_mono_incr_on_sin : strict_mono_incr_on sin (Icc (-(π / 2)) (π / 2)) :=
λ x hx y hy hxy, sin_lt_sin_of_lt_of_le_pi_div_two hx.1 hy.2 hxy
lemma sin_le_sin_of_le_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x)
(hy₂ : y ≤ π / 2) (hxy : x ≤ y) : sin x ≤ sin y :=
(strict_mono_incr_on_sin.le_iff_le ⟨hx₁, hxy.trans hy₂⟩ ⟨hx₁.trans hxy, hy₂⟩).2 hxy
lemma inj_on_sin : inj_on sin (Icc (-(π / 2)) (π / 2)) :=
strict_mono_incr_on_sin.inj_on
lemma inj_on_cos : inj_on cos (Icc 0 π) := strict_mono_decr_on_cos.inj_on
lemma surj_on_sin : surj_on sin (Icc (-(π / 2)) (π / 2)) (Icc (-1) 1) :=
by simpa only [sin_neg, sin_pi_div_two]
using intermediate_value_Icc (neg_le_self pi_div_two_pos.le) continuous_sin.continuous_on
lemma surj_on_cos : surj_on cos (Icc 0 π) (Icc (-1) 1) :=
by simpa only [cos_zero, cos_pi]
using intermediate_value_Icc' pi_pos.le continuous_cos.continuous_on
lemma sin_mem_Icc (x : ℝ) : sin x ∈ Icc (-1 : ℝ) 1 := ⟨neg_one_le_sin x, sin_le_one x⟩
lemma cos_mem_Icc (x : ℝ) : cos x ∈ Icc (-1 : ℝ) 1 := ⟨neg_one_le_cos x, cos_le_one x⟩
lemma maps_to_sin (s : set ℝ) : maps_to sin s (Icc (-1 : ℝ) 1) := λ x _, sin_mem_Icc x
lemma maps_to_cos (s : set ℝ) : maps_to cos s (Icc (-1 : ℝ) 1) := λ x _, cos_mem_Icc x
lemma bij_on_sin : bij_on sin (Icc (-(π / 2)) (π / 2)) (Icc (-1) 1) :=
⟨maps_to_sin _, inj_on_sin, surj_on_sin⟩
lemma bij_on_cos : bij_on cos (Icc 0 π) (Icc (-1) 1) :=
⟨maps_to_cos _, inj_on_cos, surj_on_cos⟩
@[simp] lemma range_cos : range cos = (Icc (-1) 1 : set ℝ) :=
subset.antisymm (range_subset_iff.2 cos_mem_Icc) surj_on_cos.subset_range
@[simp] lemma range_sin : range sin = (Icc (-1) 1 : set ℝ) :=
subset.antisymm (range_subset_iff.2 sin_mem_Icc) surj_on_sin.subset_range
lemma range_cos_infinite : (range real.cos).infinite :=
by { rw real.range_cos, exact Icc.infinite (by norm_num) }
lemma range_sin_infinite : (range real.sin).infinite :=
by { rw real.range_sin, exact Icc.infinite (by norm_num) }
lemma sin_lt {x : ℝ} (h : 0 < x) : sin x < x :=
begin
cases le_or_gt x 1 with h' h',
{ have hx : abs x = x := abs_of_nonneg (le_of_lt h),
have : abs x ≤ 1, rwa [hx],
have := sin_bound this, rw [abs_le] at this,
have := this.2, rw [sub_le_iff_le_add', hx] at this,
apply lt_of_le_of_lt this, rw [sub_add], apply lt_of_lt_of_le _ (le_of_eq (sub_zero x)),
apply sub_lt_sub_left, rw [sub_pos, div_eq_mul_inv (x ^ 3)], apply mul_lt_mul',
{ rw [pow_succ x 3], refine le_trans _ (le_of_eq (one_mul _)),
rw mul_le_mul_right, exact h', apply pow_pos h },
norm_num, norm_num, apply pow_pos h },
exact lt_of_le_of_lt (sin_le_one x) h'
end
/- note 1: this inequality is not tight, the tighter inequality is sin x > x - x ^ 3 / 6.
note 2: this is also true for x > 1, but it's nontrivial for x just above 1. -/
lemma sin_gt_sub_cube {x : ℝ} (h : 0 < x) (h' : x ≤ 1) : x - x ^ 3 / 4 < sin x :=
begin
have hx : abs x = x := abs_of_nonneg (le_of_lt h),
have : abs x ≤ 1, rwa [hx],
have := sin_bound this, rw [abs_le] at this,
have := this.1, rw [le_sub_iff_add_le, hx] at this,
refine lt_of_lt_of_le _ this,
rw [add_comm, sub_add, sub_neg_eq_add], apply sub_lt_sub_left,
apply add_lt_of_lt_sub_left,
rw (show x ^ 3 / 4 - x ^ 3 / 6 = x ^ 3 * 12⁻¹,
by simp [div_eq_mul_inv, ← mul_sub]; norm_num),
apply mul_lt_mul',
{ rw [pow_succ x 3], refine le_trans _ (le_of_eq (one_mul _)),
rw mul_le_mul_right, exact h', apply pow_pos h },
norm_num, norm_num, apply pow_pos h
end
section cos_div_sq
variable (x : ℝ)
/-- the series `sqrt_two_add_series x n` is `sqrt(2 + sqrt(2 + ... ))` with `n` square roots,
starting with `x`. We define it here because `cos (pi / 2 ^ (n+1)) = sqrt_two_add_series 0 n / 2`
-/
@[simp, pp_nodot] noncomputable def sqrt_two_add_series (x : ℝ) : ℕ → ℝ
| 0 := x
| (n+1) := sqrt (2 + sqrt_two_add_series n)
lemma sqrt_two_add_series_zero : sqrt_two_add_series x 0 = x := by simp
lemma sqrt_two_add_series_one : sqrt_two_add_series 0 1 = sqrt 2 := by simp
lemma sqrt_two_add_series_two : sqrt_two_add_series 0 2 = sqrt (2 + sqrt 2) := by simp
lemma sqrt_two_add_series_zero_nonneg : ∀(n : ℕ), 0 ≤ sqrt_two_add_series 0 n
| 0 := le_refl 0
| (n+1) := sqrt_nonneg _
lemma sqrt_two_add_series_nonneg {x : ℝ} (h : 0 ≤ x) : ∀(n : ℕ), 0 ≤ sqrt_two_add_series x n
| 0 := h
| (n+1) := sqrt_nonneg _
lemma sqrt_two_add_series_lt_two : ∀(n : ℕ), sqrt_two_add_series 0 n < 2
| 0 := by norm_num
| (n+1) :=
begin
refine lt_of_lt_of_le _ (le_of_eq $ sqrt_sq $ le_of_lt zero_lt_two),
rw [sqrt_two_add_series, sqrt_lt, ← lt_sub_iff_add_lt'],
{ refine (sqrt_two_add_series_lt_two n).trans_le _, norm_num },
{ exact add_nonneg zero_le_two (sqrt_two_add_series_zero_nonneg n) }
end
lemma sqrt_two_add_series_succ (x : ℝ) :
∀(n : ℕ), sqrt_two_add_series x (n+1) = sqrt_two_add_series (sqrt (2 + x)) n
| 0 := rfl
| (n+1) := by rw [sqrt_two_add_series, sqrt_two_add_series_succ, sqrt_two_add_series]
lemma sqrt_two_add_series_monotone_left {x y : ℝ} (h : x ≤ y) :
∀(n : ℕ), sqrt_two_add_series x n ≤ sqrt_two_add_series y n
| 0 := h
| (n+1) :=
begin
rw [sqrt_two_add_series, sqrt_two_add_series],
exact sqrt_le_sqrt (add_le_add_left (sqrt_two_add_series_monotone_left _) _)
end
@[simp] lemma cos_pi_over_two_pow : ∀(n : ℕ), cos (π / 2 ^ (n+1)) = sqrt_two_add_series 0 n / 2
| 0 := by simp
| (n+1) :=
begin
have : (2 : ℝ) ≠ 0 := two_ne_zero,
symmetry, rw [div_eq_iff_mul_eq this], symmetry,
rw [sqrt_two_add_series, sqrt_eq_iff_sq_eq, mul_pow, cos_sq, ←mul_div_assoc,
nat.add_succ, pow_succ, mul_div_mul_left _ _ this, cos_pi_over_two_pow, add_mul],
congr, { norm_num },
rw [mul_comm, sq, mul_assoc, ←mul_div_assoc, mul_div_cancel_left, ←mul_div_assoc,
mul_div_cancel_left]; try { exact this },
apply add_nonneg, norm_num, apply sqrt_two_add_series_zero_nonneg, norm_num,
apply le_of_lt, apply cos_pos_of_mem_Ioo ⟨_, _⟩,
{ transitivity (0 : ℝ), rw neg_lt_zero, apply pi_div_two_pos,
apply div_pos pi_pos, apply pow_pos, norm_num },
apply div_lt_div' (le_refl π) _ pi_pos _,
refine lt_of_le_of_lt (le_of_eq (pow_one _).symm) _,
apply pow_lt_pow, norm_num, apply nat.succ_lt_succ, apply nat.succ_pos, all_goals {norm_num}
end
lemma sin_sq_pi_over_two_pow (n : ℕ) :
sin (π / 2 ^ (n+1)) ^ 2 = 1 - (sqrt_two_add_series 0 n / 2) ^ 2 :=
by rw [sin_sq, cos_pi_over_two_pow]
lemma sin_sq_pi_over_two_pow_succ (n : ℕ) :
sin (π / 2 ^ (n+2)) ^ 2 = 1 / 2 - sqrt_two_add_series 0 n / 4 :=
begin
rw [sin_sq_pi_over_two_pow, sqrt_two_add_series, div_pow, sq_sqrt, add_div, ←sub_sub],
congr, norm_num, norm_num, apply add_nonneg, norm_num, apply sqrt_two_add_series_zero_nonneg,
end
@[simp] lemma sin_pi_over_two_pow_succ (n : ℕ) :
sin (π / 2 ^ (n+2)) = sqrt (2 - sqrt_two_add_series 0 n) / 2 :=
begin
symmetry, rw [div_eq_iff_mul_eq], symmetry,
rw [sqrt_eq_iff_sq_eq, mul_pow, sin_sq_pi_over_two_pow_succ, sub_mul],
{ congr, norm_num, rw [mul_comm], convert mul_div_cancel' _ _, norm_num, norm_num },
{ rw [sub_nonneg], apply le_of_lt, apply sqrt_two_add_series_lt_two },
apply le_of_lt, apply mul_pos, apply sin_pos_of_pos_of_lt_pi,
{ apply div_pos pi_pos, apply pow_pos, norm_num },
refine lt_of_lt_of_le _ (le_of_eq (div_one _)), rw [div_lt_div_left],
refine lt_of_le_of_lt (le_of_eq (pow_zero 2).symm) _,
apply pow_lt_pow, norm_num, apply nat.succ_pos, apply pi_pos,
apply pow_pos, all_goals {norm_num}
end
@[simp] lemma cos_pi_div_four : cos (π / 4) = sqrt 2 / 2 :=
by { transitivity cos (π / 2 ^ 2), congr, norm_num, simp }
@[simp] lemma sin_pi_div_four : sin (π / 4) = sqrt 2 / 2 :=
by { transitivity sin (π / 2 ^ 2), congr, norm_num, simp }
@[simp] lemma cos_pi_div_eight : cos (π / 8) = sqrt (2 + sqrt 2) / 2 :=
by { transitivity cos (π / 2 ^ 3), congr, norm_num, simp }
@[simp] lemma sin_pi_div_eight : sin (π / 8) = sqrt (2 - sqrt 2) / 2 :=
by { transitivity sin (π / 2 ^ 3), congr, norm_num, simp }
@[simp] lemma cos_pi_div_sixteen : cos (π / 16) = sqrt (2 + sqrt (2 + sqrt 2)) / 2 :=
by { transitivity cos (π / 2 ^ 4), congr, norm_num, simp }
@[simp] lemma sin_pi_div_sixteen : sin (π / 16) = sqrt (2 - sqrt (2 + sqrt 2)) / 2 :=
by { transitivity sin (π / 2 ^ 4), congr, norm_num, simp }
@[simp] lemma cos_pi_div_thirty_two : cos (π / 32) = sqrt (2 + sqrt (2 + sqrt (2 + sqrt 2))) / 2 :=
by { transitivity cos (π / 2 ^ 5), congr, norm_num, simp }
@[simp] lemma sin_pi_div_thirty_two : sin (π / 32) = sqrt (2 - sqrt (2 + sqrt (2 + sqrt 2))) / 2 :=
by { transitivity sin (π / 2 ^ 5), congr, norm_num, simp }
-- This section is also a convenient location for other explicit values of `sin` and `cos`.
/-- The cosine of `π / 3` is `1 / 2`. -/
@[simp] lemma cos_pi_div_three : cos (π / 3) = 1 / 2 :=
begin
have h₁ : (2 * cos (π / 3) - 1) ^ 2 * (2 * cos (π / 3) + 2) = 0,
{ have : cos (3 * (π / 3)) = cos π := by { congr' 1, ring },
linarith [cos_pi, cos_three_mul (π / 3)] },
cases mul_eq_zero.mp h₁ with h h,
{ linarith [pow_eq_zero h] },
{ have : cos π < cos (π / 3),
{ refine cos_lt_cos_of_nonneg_of_le_pi _ rfl.ge _;
linarith [pi_pos] },
linarith [cos_pi] }
end
/-- The square of the cosine of `π / 6` is `3 / 4` (this is sometimes more convenient than the
result for cosine itself). -/
lemma sq_cos_pi_div_six : cos (π / 6) ^ 2 = 3 / 4 :=
begin
have h1 : cos (π / 6) ^ 2 = 1 / 2 + 1 / 2 / 2,
{ convert cos_sq (π / 6),
have h2 : 2 * (π / 6) = π / 3 := by cancel_denoms,
rw [h2, cos_pi_div_three] },
rw ← sub_eq_zero at h1 ⊢,
convert h1 using 1,
ring
end
/-- The cosine of `π / 6` is `√3 / 2`. -/
@[simp] lemma cos_pi_div_six : cos (π / 6) = (sqrt 3) / 2 :=
begin
suffices : sqrt 3 = cos (π / 6) * 2,
{ field_simp [(by norm_num : 0 ≠ 2)], exact this.symm },
rw sqrt_eq_iff_sq_eq,
{ have h1 := (mul_right_inj' (by norm_num : (4:ℝ) ≠ 0)).mpr sq_cos_pi_div_six,
rw ← sub_eq_zero at h1 ⊢,
convert h1 using 1,
ring },
{ norm_num },
{ have : 0 < cos (π / 6) := by { apply cos_pos_of_mem_Ioo; split; linarith [pi_pos] },
linarith },
end
/-- The sine of `π / 6` is `1 / 2`. -/
@[simp] lemma sin_pi_div_six : sin (π / 6) = 1 / 2 :=
begin
rw [← cos_pi_div_two_sub, ← cos_pi_div_three],
congr,
ring
end
/-- The square of the sine of `π / 3` is `3 / 4` (this is sometimes more convenient than the
result for cosine itself). -/
lemma sq_sin_pi_div_three : sin (π / 3) ^ 2 = 3 / 4 :=
begin
rw [← cos_pi_div_two_sub, ← sq_cos_pi_div_six],
congr,
ring
end
/-- The sine of `π / 3` is `√3 / 2`. -/
@[simp] lemma sin_pi_div_three : sin (π / 3) = (sqrt 3) / 2 :=
begin
rw [← cos_pi_div_two_sub, ← cos_pi_div_six],
congr,
ring
end
end cos_div_sq
/-- The type of angles -/
def angle : Type :=
quotient_add_group.quotient (add_subgroup.gmultiples (2 * π))
namespace angle
instance angle.add_comm_group : add_comm_group angle :=
quotient_add_group.add_comm_group _
instance : inhabited angle := ⟨0⟩
instance angle.has_coe : has_coe ℝ angle :=
⟨quotient.mk'⟩
@[simp] lemma coe_zero : ↑(0 : ℝ) = (0 : angle) := rfl
@[simp] lemma coe_add (x y : ℝ) : ↑(x + y : ℝ) = (↑x + ↑y : angle) := rfl
@[simp] lemma coe_neg (x : ℝ) : ↑(-x : ℝ) = -(↑x : angle) := rfl
@[simp] lemma coe_sub (x y : ℝ) : ↑(x - y : ℝ) = (↑x - ↑y : angle) :=
by rw [sub_eq_add_neg, sub_eq_add_neg, coe_add, coe_neg]
@[simp, norm_cast] lemma coe_nat_mul_eq_nsmul (x : ℝ) (n : ℕ) :
↑((n : ℝ) * x) = n • (↑x : angle) :=
by simpa using add_monoid_hom.map_nsmul ⟨coe, coe_zero, coe_add⟩ _ _
@[simp, norm_cast] lemma coe_int_mul_eq_gsmul (x : ℝ) (n : ℤ) :
↑((n : ℝ) * x : ℝ) = n • (↑x : angle) :=
by simpa using add_monoid_hom.map_gsmul ⟨coe, coe_zero, coe_add⟩ _ _
@[simp] lemma coe_two_pi : ↑(2 * π : ℝ) = (0 : angle) :=
quotient.sound' ⟨-1, show (-1 : ℤ) • (2 * π) = _, by rw [neg_one_gsmul, add_zero]⟩
lemma angle_eq_iff_two_pi_dvd_sub {ψ θ : ℝ} : (θ : angle) = ψ ↔ ∃ k : ℤ, θ - ψ = 2 * π * k :=
by simp only [quotient_add_group.eq, add_subgroup.gmultiples_eq_closure,
add_subgroup.mem_closure_singleton, gsmul_eq_mul', (sub_eq_neg_add _ _).symm, eq_comm]
theorem cos_eq_iff_eq_or_eq_neg {θ ψ : ℝ} : cos θ = cos ψ ↔ (θ : angle) = ψ ∨ (θ : angle) = -ψ :=
begin
split,
{ intro Hcos,
rw [← sub_eq_zero, cos_sub_cos, mul_eq_zero, mul_eq_zero, neg_eq_zero,
eq_false_intro two_ne_zero, false_or, sin_eq_zero_iff, sin_eq_zero_iff] at Hcos,
rcases Hcos with ⟨n, hn⟩ | ⟨n, hn⟩,
{ right,
rw [eq_div_iff_mul_eq (@two_ne_zero ℝ _ _), ← sub_eq_iff_eq_add] at hn,
rw [← hn, coe_sub, eq_neg_iff_add_eq_zero, sub_add_cancel, mul_assoc,
coe_int_mul_eq_gsmul, mul_comm, coe_two_pi, gsmul_zero] },
{ left,
rw [eq_div_iff_mul_eq (@two_ne_zero ℝ _ _), eq_sub_iff_add_eq] at hn,
rw [← hn, coe_add, mul_assoc,
coe_int_mul_eq_gsmul, mul_comm, coe_two_pi, gsmul_zero, zero_add] },
apply_instance, },
{ rw [angle_eq_iff_two_pi_dvd_sub, ← coe_neg, angle_eq_iff_two_pi_dvd_sub],
rintro (⟨k, H⟩ | ⟨k, H⟩),
rw [← sub_eq_zero, cos_sub_cos, H, mul_assoc 2 π k,
mul_div_cancel_left _ (@two_ne_zero ℝ _ _), mul_comm π _, sin_int_mul_pi, mul_zero],
rw [← sub_eq_zero, cos_sub_cos, ← sub_neg_eq_add, H, mul_assoc 2 π k,
mul_div_cancel_left _ (@two_ne_zero ℝ _ _), mul_comm π _, sin_int_mul_pi, mul_zero,
zero_mul] }
end
theorem sin_eq_iff_eq_or_add_eq_pi {θ ψ : ℝ} :
sin θ = sin ψ ↔ (θ : angle) = ψ ∨ (θ : angle) + ψ = π :=
begin
split,
{ intro Hsin, rw [← cos_pi_div_two_sub, ← cos_pi_div_two_sub] at Hsin,
cases cos_eq_iff_eq_or_eq_neg.mp Hsin with h h,
{ left, rw [coe_sub, coe_sub] at h, exact sub_right_inj.1 h },
right, rw [coe_sub, coe_sub, eq_neg_iff_add_eq_zero, add_sub,
sub_add_eq_add_sub, ← coe_add, add_halves, sub_sub, sub_eq_zero] at h,
exact h.symm },
{ rw [angle_eq_iff_two_pi_dvd_sub, ←eq_sub_iff_add_eq, ←coe_sub, angle_eq_iff_two_pi_dvd_sub],
rintro (⟨k, H⟩ | ⟨k, H⟩),
rw [← sub_eq_zero, sin_sub_sin, H, mul_assoc 2 π k,
mul_div_cancel_left _ (@two_ne_zero ℝ _ _), mul_comm π _, sin_int_mul_pi, mul_zero,
zero_mul],
have H' : θ + ψ = (2 * k) * π + π := by rwa [←sub_add, sub_add_eq_add_sub, sub_eq_iff_eq_add,
mul_assoc, mul_comm π _, ←mul_assoc] at H,
rw [← sub_eq_zero, sin_sub_sin, H', add_div, mul_assoc 2 _ π,
mul_div_cancel_left _ (@two_ne_zero ℝ _ _), cos_add_pi_div_two, sin_int_mul_pi, neg_zero,
mul_zero] }
end
theorem cos_sin_inj {θ ψ : ℝ} (Hcos : cos θ = cos ψ) (Hsin : sin θ = sin ψ) : (θ : angle) = ψ :=
begin
cases cos_eq_iff_eq_or_eq_neg.mp Hcos with hc hc, { exact hc },
cases sin_eq_iff_eq_or_add_eq_pi.mp Hsin with hs hs, { exact hs },
rw [eq_neg_iff_add_eq_zero, hs] at hc,
cases quotient.exact' hc with n hn, change n • _ = _ at hn,
rw [← neg_one_mul, add_zero, ← sub_eq_zero, gsmul_eq_mul, ← mul_assoc, ← sub_mul,
mul_eq_zero, eq_false_intro (ne_of_gt pi_pos), or_false, sub_neg_eq_add,
← int.cast_zero, ← int.cast_one, ← int.cast_bit0, ← int.cast_mul, ← int.cast_add,
int.cast_inj] at hn,
have : (n * 2 + 1) % (2:ℤ) = 0 % (2:ℤ) := congr_arg (%(2:ℤ)) hn,
rw [add_comm, int.add_mul_mod_self] at this,
exact absurd this one_ne_zero
end
end angle
/-- `real.sin` as an `order_iso` between `[-(π / 2), π / 2]` and `[-1, 1]`. -/
def sin_order_iso : Icc (-(π / 2)) (π / 2) ≃o Icc (-1:ℝ) 1 :=
(strict_mono_incr_on_sin.order_iso _ _).trans $ order_iso.set_congr _ _ bij_on_sin.image_eq
@[simp] lemma coe_sin_order_iso_apply (x : Icc (-(π / 2)) (π / 2)) :
(sin_order_iso x : ℝ) = sin x := rfl
lemma sin_order_iso_apply (x : Icc (-(π / 2)) (π / 2)) :
sin_order_iso x = ⟨sin x, sin_mem_Icc x⟩ := rfl
/-- Inverse of the `sin` function, returns values in the range `-π / 2 ≤ arcsin x ≤ π / 2`.
It defaults to `-π / 2` on `(-∞, -1)` and to `π / 2` to `(1, ∞)`. -/
@[pp_nodot] noncomputable def arcsin : ℝ → ℝ :=
coe ∘ Icc_extend (neg_le_self zero_le_one) sin_order_iso.symm
lemma arcsin_mem_Icc (x : ℝ) : arcsin x ∈ Icc (-(π / 2)) (π / 2) := subtype.coe_prop _
@[simp] lemma range_arcsin : range arcsin = Icc (-(π / 2)) (π / 2) :=
by { rw [arcsin, range_comp coe], simp [Icc] }
lemma arcsin_le_pi_div_two (x : ℝ) : arcsin x ≤ π / 2 := (arcsin_mem_Icc x).2
lemma neg_pi_div_two_le_arcsin (x : ℝ) : -(π / 2) ≤ arcsin x := (arcsin_mem_Icc x).1
lemma arcsin_proj_Icc (x : ℝ) :
arcsin (proj_Icc (-1) 1 (neg_le_self $ @zero_le_one ℝ _) x) = arcsin x :=
by rw [arcsin, function.comp_app, Icc_extend_coe, function.comp_app, Icc_extend]
lemma sin_arcsin' {x : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) : sin (arcsin x) = x :=
by simpa [arcsin, Icc_extend_of_mem _ _ hx, -order_iso.apply_symm_apply]
using subtype.ext_iff.1 (sin_order_iso.apply_symm_apply ⟨x, hx⟩)
lemma sin_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arcsin x) = x :=
sin_arcsin' ⟨hx₁, hx₂⟩
lemma arcsin_sin' {x : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) : arcsin (sin x) = x :=
inj_on_sin (arcsin_mem_Icc _) hx $ by rw [sin_arcsin (neg_one_le_sin _) (sin_le_one _)]
lemma arcsin_sin {x : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) : arcsin (sin x) = x :=
arcsin_sin' ⟨hx₁, hx₂⟩
lemma strict_mono_incr_on_arcsin : strict_mono_incr_on arcsin (Icc (-1) 1) :=
(subtype.strict_mono_coe _).comp_strict_mono_incr_on $
sin_order_iso.symm.strict_mono.strict_mono_incr_on_Icc_extend _
lemma monotone_arcsin : monotone arcsin :=
(subtype.mono_coe _).comp $ sin_order_iso.symm.monotone.Icc_extend _
lemma inj_on_arcsin : inj_on arcsin (Icc (-1) 1) := strict_mono_incr_on_arcsin.inj_on
lemma arcsin_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) :
arcsin x = arcsin y ↔ x = y :=
inj_on_arcsin.eq_iff ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩
@[continuity]
lemma continuous_arcsin : continuous arcsin :=
continuous_subtype_coe.comp sin_order_iso.symm.continuous.Icc_extend
lemma continuous_at_arcsin {x : ℝ} : continuous_at arcsin x :=
continuous_arcsin.continuous_at
lemma arcsin_eq_of_sin_eq {x y : ℝ} (h₁ : sin x = y) (h₂ : x ∈ Icc (-(π / 2)) (π / 2)) :
arcsin y = x :=
begin
subst y,
exact inj_on_sin (arcsin_mem_Icc _) h₂ (sin_arcsin' (sin_mem_Icc x))
end
@[simp] lemma arcsin_zero : arcsin 0 = 0 :=
arcsin_eq_of_sin_eq sin_zero ⟨neg_nonpos.2 pi_div_two_pos.le, pi_div_two_pos.le⟩
@[simp] lemma arcsin_one : arcsin 1 = π / 2 :=
arcsin_eq_of_sin_eq sin_pi_div_two $ right_mem_Icc.2 (neg_le_self pi_div_two_pos.le)
lemma arcsin_of_one_le {x : ℝ} (hx : 1 ≤ x) : arcsin x = π / 2 :=
by rw [← arcsin_proj_Icc, proj_Icc_of_right_le _ hx, subtype.coe_mk, arcsin_one]
lemma arcsin_neg_one : arcsin (-1) = -(π / 2) :=
arcsin_eq_of_sin_eq (by rw [sin_neg, sin_pi_div_two]) $
left_mem_Icc.2 (neg_le_self pi_div_two_pos.le)
lemma arcsin_of_le_neg_one {x : ℝ} (hx : x ≤ -1) : arcsin x = -(π / 2) :=
by rw [← arcsin_proj_Icc, proj_Icc_of_le_left _ hx, subtype.coe_mk, arcsin_neg_one]
@[simp] lemma arcsin_neg (x : ℝ) : arcsin (-x) = -arcsin x :=
begin
cases le_total x (-1) with hx₁ hx₁,
{ rw [arcsin_of_le_neg_one hx₁, neg_neg, arcsin_of_one_le (le_neg.2 hx₁)] },
cases le_total 1 x with hx₂ hx₂,
{ rw [arcsin_of_one_le hx₂, arcsin_of_le_neg_one (neg_le_neg hx₂)] },
refine arcsin_eq_of_sin_eq _ _,
{ rw [sin_neg, sin_arcsin hx₁ hx₂] },
{ exact ⟨neg_le_neg (arcsin_le_pi_div_two _), neg_le.2 (neg_pi_div_two_le_arcsin _)⟩ }
end
lemma arcsin_le_iff_le_sin {x y : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) (hy : y ∈ Icc (-(π / 2)) (π / 2)) :
arcsin x ≤ y ↔ x ≤ sin y :=
by rw [← arcsin_sin' hy, strict_mono_incr_on_arcsin.le_iff_le hx (sin_mem_Icc _), arcsin_sin' hy]
lemma arcsin_le_iff_le_sin' {x y : ℝ} (hy : y ∈ Ico (-(π / 2)) (π / 2)) :
arcsin x ≤ y ↔ x ≤ sin y :=
begin
cases le_total x (-1) with hx₁ hx₁,
{ simp [arcsin_of_le_neg_one hx₁, hy.1, hx₁.trans (neg_one_le_sin _)] },
cases lt_or_le 1 x with hx₂ hx₂,
{ simp [arcsin_of_one_le hx₂.le, hy.2.not_le, (sin_le_one y).trans_lt hx₂] },
exact arcsin_le_iff_le_sin ⟨hx₁, hx₂⟩ (mem_Icc_of_Ico hy)
end
lemma le_arcsin_iff_sin_le {x y : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) (hy : y ∈ Icc (-1 : ℝ) 1) :
x ≤ arcsin y ↔ sin x ≤ y :=
by rw [← neg_le_neg_iff, ← arcsin_neg,
arcsin_le_iff_le_sin ⟨neg_le_neg hy.2, neg_le.2 hy.1⟩ ⟨neg_le_neg hx.2, neg_le.2 hx.1⟩,
sin_neg, neg_le_neg_iff]
lemma le_arcsin_iff_sin_le' {x y : ℝ} (hx : x ∈ Ioc (-(π / 2)) (π / 2)) :
x ≤ arcsin y ↔ sin x ≤ y :=
by rw [← neg_le_neg_iff, ← arcsin_neg, arcsin_le_iff_le_sin' ⟨neg_le_neg hx.2, neg_lt.2 hx.1⟩,
sin_neg, neg_le_neg_iff]
lemma arcsin_lt_iff_lt_sin {x y : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) (hy : y ∈ Icc (-(π / 2)) (π / 2)) :
arcsin x < y ↔ x < sin y :=
not_le.symm.trans $ (not_congr $ le_arcsin_iff_sin_le hy hx).trans not_le
lemma arcsin_lt_iff_lt_sin' {x y : ℝ} (hy : y ∈ Ioc (-(π / 2)) (π / 2)) :
arcsin x < y ↔ x < sin y :=
not_le.symm.trans $ (not_congr $ le_arcsin_iff_sin_le' hy).trans not_le
lemma lt_arcsin_iff_sin_lt {x y : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) (hy : y ∈ Icc (-1 : ℝ) 1) :
x < arcsin y ↔ sin x < y :=
not_le.symm.trans $ (not_congr $ arcsin_le_iff_le_sin hy hx).trans not_le
lemma lt_arcsin_iff_sin_lt' {x y : ℝ} (hx : x ∈ Ico (-(π / 2)) (π / 2)) :
x < arcsin y ↔ sin x < y :=
not_le.symm.trans $ (not_congr $ arcsin_le_iff_le_sin' hx).trans not_le
lemma arcsin_eq_iff_eq_sin {x y : ℝ} (hy : y ∈ Ioo (-(π / 2)) (π / 2)) :
arcsin x = y ↔ x = sin y :=
by simp only [le_antisymm_iff, arcsin_le_iff_le_sin' (mem_Ico_of_Ioo hy),
le_arcsin_iff_sin_le' (mem_Ioc_of_Ioo hy)]
@[simp] lemma arcsin_nonneg {x : ℝ} : 0 ≤ arcsin x ↔ 0 ≤ x :=
(le_arcsin_iff_sin_le' ⟨neg_lt_zero.2 pi_div_two_pos, pi_div_two_pos.le⟩).trans $ by rw [sin_zero]
@[simp] lemma arcsin_nonpos {x : ℝ} : arcsin x ≤ 0 ↔ x ≤ 0 :=
neg_nonneg.symm.trans $ arcsin_neg x ▸ arcsin_nonneg.trans neg_nonneg
@[simp] lemma arcsin_eq_zero_iff {x : ℝ} : arcsin x = 0 ↔ x = 0 :=
by simp [le_antisymm_iff]
@[simp] lemma zero_eq_arcsin_iff {x} : 0 = arcsin x ↔ x = 0 :=
eq_comm.trans arcsin_eq_zero_iff
@[simp] lemma arcsin_pos {x : ℝ} : 0 < arcsin x ↔ 0 < x :=
lt_iff_lt_of_le_iff_le arcsin_nonpos
@[simp] lemma arcsin_lt_zero {x : ℝ} : arcsin x < 0 ↔ x < 0 :=
lt_iff_lt_of_le_iff_le arcsin_nonneg
@[simp] lemma arcsin_lt_pi_div_two {x : ℝ} : arcsin x < π / 2 ↔ x < 1 :=
(arcsin_lt_iff_lt_sin' (right_mem_Ioc.2 $ neg_lt_self pi_div_two_pos)).trans $
by rw sin_pi_div_two
@[simp] lemma neg_pi_div_two_lt_arcsin {x : ℝ} : -(π / 2) < arcsin x ↔ -1 < x :=
(lt_arcsin_iff_sin_lt' $ left_mem_Ico.2 $ neg_lt_self pi_div_two_pos).trans $
by rw [sin_neg, sin_pi_div_two]
@[simp] lemma arcsin_eq_pi_div_two {x : ℝ} : arcsin x = π / 2 ↔ 1 ≤ x :=
⟨λ h, not_lt.1 $ λ h', (arcsin_lt_pi_div_two.2 h').ne h, arcsin_of_one_le⟩
@[simp] lemma pi_div_two_eq_arcsin {x} : π / 2 = arcsin x ↔ 1 ≤ x :=
eq_comm.trans arcsin_eq_pi_div_two
@[simp] lemma pi_div_two_le_arcsin {x} : π / 2 ≤ arcsin x ↔ 1 ≤ x :=
(arcsin_le_pi_div_two x).le_iff_eq.trans pi_div_two_eq_arcsin
@[simp] lemma arcsin_eq_neg_pi_div_two {x : ℝ} : arcsin x = -(π / 2) ↔ x ≤ -1 :=
⟨λ h, not_lt.1 $ λ h', (neg_pi_div_two_lt_arcsin.2 h').ne' h, arcsin_of_le_neg_one⟩
@[simp] lemma neg_pi_div_two_eq_arcsin {x} : -(π / 2) = arcsin x ↔ x ≤ -1 :=
eq_comm.trans arcsin_eq_neg_pi_div_two
@[simp] lemma arcsin_le_neg_pi_div_two {x} : arcsin x ≤ -(π / 2) ↔ x ≤ -1 :=
(neg_pi_div_two_le_arcsin x).le_iff_eq.trans arcsin_eq_neg_pi_div_two
lemma maps_to_sin_Ioo : maps_to sin (Ioo (-(π / 2)) (π / 2)) (Ioo (-1) 1) :=
λ x h, by rwa [mem_Ioo, ← arcsin_lt_pi_div_two, ← neg_pi_div_two_lt_arcsin,
arcsin_sin h.1.le h.2.le]
/-- `real.sin` as a `local_homeomorph` between `(-π / 2, π / 2)` and `(-1, 1)`. -/
@[simp] def sin_local_homeomorph : local_homeomorph ℝ ℝ :=
{ to_fun := sin,
inv_fun := arcsin,
source := Ioo (-(π / 2)) (π / 2),
target := Ioo (-1) 1,
map_source' := maps_to_sin_Ioo,
map_target' := λ y hy, ⟨neg_pi_div_two_lt_arcsin.2 hy.1, arcsin_lt_pi_div_two.2 hy.2⟩,
left_inv' := λ x hx, arcsin_sin hx.1.le hx.2.le,
right_inv' := λ y hy, sin_arcsin hy.1.le hy.2.le,
open_source := is_open_Ioo,
open_target := is_open_Ioo,
continuous_to_fun := continuous_sin.continuous_on,
continuous_inv_fun := continuous_arcsin.continuous_on }
lemma cos_arcsin_nonneg (x : ℝ) : 0 ≤ cos (arcsin x) :=
cos_nonneg_of_mem_Icc ⟨neg_pi_div_two_le_arcsin _, arcsin_le_pi_div_two _⟩
lemma cos_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arcsin x) = sqrt (1 - x ^ 2) :=
have sin (arcsin x) ^ 2 + cos (arcsin x) ^ 2 = 1 := sin_sq_add_cos_sq (arcsin x),
begin
rw [← eq_sub_iff_add_eq', ← sqrt_inj (sq_nonneg _) (sub_nonneg.2 (sin_sq_le_one (arcsin x))),
sq, sqrt_mul_self (cos_arcsin_nonneg _)] at this,
rw [this, sin_arcsin hx₁ hx₂],
end
lemma deriv_arcsin_aux {x : ℝ} (h₁ : x ≠ -1) (h₂ : x ≠ 1) :
has_strict_deriv_at arcsin (1 / sqrt (1 - x ^ 2)) x ∧ times_cont_diff_at ℝ ⊤ arcsin x :=
begin
cases h₁.lt_or_lt with h₁ h₁,
{ have : 1 - x ^ 2 < 0, by nlinarith [h₁],
rw [sqrt_eq_zero'.2 this.le, div_zero],
have : arcsin =ᶠ[𝓝 x] λ _, -(π / 2) :=
(gt_mem_nhds h₁).mono (λ y hy, arcsin_of_le_neg_one hy.le),
exact ⟨(has_strict_deriv_at_const _ _).congr_of_eventually_eq this.symm,
times_cont_diff_at_const.congr_of_eventually_eq this⟩ },
cases h₂.lt_or_lt with h₂ h₂,
{ have : 0 < sqrt (1 - x ^ 2) := sqrt_pos.2 (by nlinarith [h₁, h₂]),
simp only [← cos_arcsin h₁.le h₂.le, one_div] at this ⊢,
exact ⟨sin_local_homeomorph.has_strict_deriv_at_symm ⟨h₁, h₂⟩ this.ne'
(has_strict_deriv_at_sin _),
sin_local_homeomorph.times_cont_diff_at_symm_deriv this.ne' ⟨h₁, h₂⟩
(has_deriv_at_sin _) times_cont_diff_sin.times_cont_diff_at⟩ },
{ have : 1 - x ^ 2 < 0, by nlinarith [h₂],
rw [sqrt_eq_zero'.2 this.le, div_zero],
have : arcsin =ᶠ[𝓝 x] λ _, π / 2 := (lt_mem_nhds h₂).mono (λ y hy, arcsin_of_one_le hy.le),
exact ⟨(has_strict_deriv_at_const _ _).congr_of_eventually_eq this.symm,
times_cont_diff_at_const.congr_of_eventually_eq this⟩ }
end
lemma has_strict_deriv_at_arcsin {x : ℝ} (h₁ : x ≠ -1) (h₂ : x ≠ 1) :
has_strict_deriv_at arcsin (1 / sqrt (1 - x ^ 2)) x :=
(deriv_arcsin_aux h₁ h₂).1
lemma has_deriv_at_arcsin {x : ℝ} (h₁ : x ≠ -1) (h₂ : x ≠ 1) :
has_deriv_at arcsin (1 / sqrt (1 - x ^ 2)) x :=
(has_strict_deriv_at_arcsin h₁ h₂).has_deriv_at
lemma times_cont_diff_at_arcsin {x : ℝ} (h₁ : x ≠ -1) (h₂ : x ≠ 1) {n : with_top ℕ} :
times_cont_diff_at ℝ n arcsin x :=
(deriv_arcsin_aux h₁ h₂).2.of_le le_top
lemma has_deriv_within_at_arcsin_Ici {x : ℝ} (h : x ≠ -1) :
has_deriv_within_at arcsin (1 / sqrt (1 - x ^ 2)) (Ici x) x :=
begin
rcases em (x = 1) with (rfl|h'),
{ convert (has_deriv_within_at_const _ _ (π / 2)).congr _ _;
simp [arcsin_of_one_le] { contextual := tt } },
{ exact (has_deriv_at_arcsin h h').has_deriv_within_at }
end
lemma has_deriv_within_at_arcsin_Iic {x : ℝ} (h : x ≠ 1) :
has_deriv_within_at arcsin (1 / sqrt (1 - x ^ 2)) (Iic x) x :=
begin
rcases em (x = -1) with (rfl|h'),
{ convert (has_deriv_within_at_const _ _ (-(π / 2))).congr _ _;
simp [arcsin_of_le_neg_one] { contextual := tt } },
{ exact (has_deriv_at_arcsin h' h).has_deriv_within_at }
end
lemma differentiable_within_at_arcsin_Ici {x : ℝ} :
differentiable_within_at ℝ arcsin (Ici x) x ↔ x ≠ -1 :=
begin
refine ⟨_, λ h, (has_deriv_within_at_arcsin_Ici h).differentiable_within_at⟩,
rintro h rfl,
have : sin ∘ arcsin =ᶠ[𝓝[Ici (-1:ℝ)] (-1)] id,
{ filter_upwards [Icc_mem_nhds_within_Ici ⟨le_rfl, neg_lt_self (@zero_lt_one ℝ _ _)⟩],
exact λ x, sin_arcsin' },
have := h.has_deriv_within_at.sin.congr_of_eventually_eq this.symm (by simp),
simpa using (unique_diff_on_Ici _ _ left_mem_Ici).eq_deriv _ this (has_deriv_within_at_id _ _)
end
lemma differentiable_within_at_arcsin_Iic {x : ℝ} :
differentiable_within_at ℝ arcsin (Iic x) x ↔ x ≠ 1 :=
begin
refine ⟨λ h, _, λ h, (has_deriv_within_at_arcsin_Iic h).differentiable_within_at⟩,
rw [← neg_neg x, ← image_neg_Ici] at h,
have := (h.comp (-x) differentiable_within_at_id.neg (maps_to_image _ _)).neg,
simpa [(∘), differentiable_within_at_arcsin_Ici] using this
end
lemma differentiable_at_arcsin {x : ℝ} :
differentiable_at ℝ arcsin x ↔ x ≠ -1 ∧ x ≠ 1 :=
⟨λ h, ⟨differentiable_within_at_arcsin_Ici.1 h.differentiable_within_at,
differentiable_within_at_arcsin_Iic.1 h.differentiable_within_at⟩,
λ h, (has_deriv_at_arcsin h.1 h.2).differentiable_at⟩
@[simp] lemma deriv_arcsin : deriv arcsin = λ x, 1 / sqrt (1 - x ^ 2) :=
begin
funext x,
by_cases h : x ≠ -1 ∧ x ≠ 1,
{ exact (has_deriv_at_arcsin h.1 h.2).deriv },
{ rw [deriv_zero_of_not_differentiable_at (mt differentiable_at_arcsin.1 h)],
simp only [not_and_distrib, ne.def, not_not] at h,
rcases h with (rfl|rfl); simp }
end
lemma differentiable_on_arcsin : differentiable_on ℝ arcsin {-1, 1}ᶜ :=
λ x hx, (differentiable_at_arcsin.2
⟨λ h, hx (or.inl h), λ h, hx (or.inr h)⟩).differentiable_within_at
lemma times_cont_diff_on_arcsin {n : with_top ℕ} :
times_cont_diff_on ℝ n arcsin {-1, 1}ᶜ :=
λ x hx, (times_cont_diff_at_arcsin (mt or.inl hx) (mt or.inr hx)).times_cont_diff_within_at
lemma times_cont_diff_at_arcsin_iff {x : ℝ} {n : with_top ℕ} :
times_cont_diff_at ℝ n arcsin x ↔ n = 0 ∨ (x ≠ -1 ∧ x ≠ 1) :=
⟨λ h, or_iff_not_imp_left.2 $ λ hn, differentiable_at_arcsin.1 $ h.differentiable_at $
with_top.one_le_iff_pos.2 (pos_iff_ne_zero.2 hn),
λ h, h.elim (λ hn, hn.symm ▸ (times_cont_diff_zero.2 continuous_arcsin).times_cont_diff_at) $
λ hx, times_cont_diff_at_arcsin hx.1 hx.2⟩
lemma measurable_arcsin : measurable arcsin := continuous_arcsin.measurable
/-- Inverse of the `cos` function, returns values in the range `0 ≤ arccos x` and `arccos x ≤ π`.
If the argument is not between `-1` and `1` it defaults to `π / 2` -/
@[pp_nodot] noncomputable def arccos (x : ℝ) : ℝ :=
π / 2 - arcsin x
lemma arccos_eq_pi_div_two_sub_arcsin (x : ℝ) : arccos x = π / 2 - arcsin x := rfl
lemma arcsin_eq_pi_div_two_sub_arccos (x : ℝ) : arcsin x = π / 2 - arccos x :=
by simp [arccos]
lemma arccos_le_pi (x : ℝ) : arccos x ≤ π :=
by unfold arccos; linarith [neg_pi_div_two_le_arcsin x]
lemma arccos_nonneg (x : ℝ) : 0 ≤ arccos x :=
by unfold arccos; linarith [arcsin_le_pi_div_two x]
lemma cos_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arccos x) = x :=
by rw [arccos, cos_pi_div_two_sub, sin_arcsin hx₁ hx₂]
lemma arccos_cos {x : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π) : arccos (cos x) = x :=
by rw [arccos, ← sin_pi_div_two_sub, arcsin_sin]; simp [sub_eq_add_neg]; linarith
lemma strict_mono_decr_on_arccos : strict_mono_decr_on arccos (Icc (-1) 1) :=
λ x hx y hy h, sub_lt_sub_left (strict_mono_incr_on_arcsin hx hy h) _
lemma arccos_inj_on : inj_on arccos (Icc (-1) 1) := strict_mono_decr_on_arccos.inj_on
lemma arccos_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) :
arccos x = arccos y ↔ x = y :=
arccos_inj_on.eq_iff ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩
@[simp] lemma arccos_zero : arccos 0 = π / 2 := by simp [arccos]
@[simp] lemma arccos_one : arccos 1 = 0 := by simp [arccos]
@[simp] lemma arccos_neg_one : arccos (-1) = π := by simp [arccos, add_halves]
@[simp] lemma arccos_eq_zero {x} : arccos x = 0 ↔ 1 ≤ x :=
by simp [arccos, sub_eq_zero]
@[simp] lemma arccos_eq_pi_div_two {x} : arccos x = π / 2 ↔ x = 0 :=
by simp [arccos, sub_eq_iff_eq_add]
@[simp] lemma arccos_eq_pi {x} : arccos x = π ↔ x ≤ -1 :=
by rw [arccos, sub_eq_iff_eq_add, ← sub_eq_iff_eq_add', div_two_sub_self, neg_pi_div_two_eq_arcsin]
lemma arccos_neg (x : ℝ) : arccos (-x) = π - arccos x :=
by rw [← add_halves π, arccos, arcsin_neg, arccos, add_sub_assoc, sub_sub_self, sub_neg_eq_add]
lemma sin_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arccos x) = sqrt (1 - x ^ 2) :=
by rw [arccos_eq_pi_div_two_sub_arcsin, sin_pi_div_two_sub, cos_arcsin hx₁ hx₂]
@[continuity]
lemma continuous_arccos : continuous arccos := continuous_const.sub continuous_arcsin
lemma has_strict_deriv_at_arccos {x : ℝ} (h₁ : x ≠ -1) (h₂ : x ≠ 1) :
has_strict_deriv_at arccos (-(1 / sqrt (1 - x ^ 2))) x :=
(has_strict_deriv_at_arcsin h₁ h₂).const_sub (π / 2)
lemma has_deriv_at_arccos {x : ℝ} (h₁ : x ≠ -1) (h₂ : x ≠ 1) :
has_deriv_at arccos (-(1 / sqrt (1 - x ^ 2))) x :=
(has_deriv_at_arcsin h₁ h₂).const_sub (π / 2)
lemma times_cont_diff_at_arccos {x : ℝ} (h₁ : x ≠ -1) (h₂ : x ≠ 1) {n : with_top ℕ} :
times_cont_diff_at ℝ n arccos x :=
times_cont_diff_at_const.sub (times_cont_diff_at_arcsin h₁ h₂)
lemma has_deriv_within_at_arccos_Ici {x : ℝ} (h : x ≠ -1) :
has_deriv_within_at arccos (-(1 / sqrt (1 - x ^ 2))) (Ici x) x :=
(has_deriv_within_at_arcsin_Ici h).const_sub _
lemma has_deriv_within_at_arccos_Iic {x : ℝ} (h : x ≠ 1) :
has_deriv_within_at arccos (-(1 / sqrt (1 - x ^ 2))) (Iic x) x :=
(has_deriv_within_at_arcsin_Iic h).const_sub _
lemma differentiable_within_at_arccos_Ici {x : ℝ} :
differentiable_within_at ℝ arccos (Ici x) x ↔ x ≠ -1 :=
(differentiable_within_at_const_sub_iff _).trans differentiable_within_at_arcsin_Ici
lemma differentiable_within_at_arccos_Iic {x : ℝ} :
differentiable_within_at ℝ arccos (Iic x) x ↔ x ≠ 1 :=
(differentiable_within_at_const_sub_iff _).trans differentiable_within_at_arcsin_Iic
lemma differentiable_at_arccos {x : ℝ} :
differentiable_at ℝ arccos x ↔ x ≠ -1 ∧ x ≠ 1 :=
(differentiable_at_const_sub_iff _).trans differentiable_at_arcsin
@[simp] lemma deriv_arccos : deriv arccos = λ x, -(1 / sqrt (1 - x ^ 2)) :=
funext $ λ x, (deriv_const_sub _).trans $ by simp only [deriv_arcsin]
lemma differentiable_on_arccos : differentiable_on ℝ arccos {-1, 1}ᶜ :=
differentiable_on_arcsin.const_sub _
lemma times_cont_diff_on_arccos {n : with_top ℕ} :
times_cont_diff_on ℝ n arccos {-1, 1}ᶜ :=
times_cont_diff_on_const.sub times_cont_diff_on_arcsin
lemma times_cont_diff_at_arccos_iff {x : ℝ} {n : with_top ℕ} :
times_cont_diff_at ℝ n arccos x ↔ n = 0 ∨ (x ≠ -1 ∧ x ≠ 1) :=
by refine iff.trans ⟨λ h, _, λ h, _⟩ times_cont_diff_at_arcsin_iff;
simpa [arccos] using (@times_cont_diff_at_const _ _ _ _ _ _ _ _ _ _ (π / 2)).sub h
lemma measurable_arccos : measurable arccos := continuous_arccos.measurable
@[simp] lemma tan_pi_div_four : tan (π / 4) = 1 :=
begin
rw [tan_eq_sin_div_cos, cos_pi_div_four, sin_pi_div_four],
have h : (sqrt 2) / 2 > 0 := by cancel_denoms,
exact div_self (ne_of_gt h),
end
@[simp] lemma tan_pi_div_two : tan (π / 2) = 0 := by simp [tan_eq_sin_div_cos]
lemma tan_pos_of_pos_of_lt_pi_div_two {x : ℝ} (h0x : 0 < x) (hxp : x < π / 2) : 0 < tan x :=
by rw tan_eq_sin_div_cos; exact div_pos (sin_pos_of_pos_of_lt_pi h0x (by linarith))
(cos_pos_of_mem_Ioo ⟨by linarith, hxp⟩)
lemma tan_nonneg_of_nonneg_of_le_pi_div_two {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π / 2) : 0 ≤ tan x :=
match lt_or_eq_of_le h0x, lt_or_eq_of_le hxp with
| or.inl hx0, or.inl hxp := le_of_lt (tan_pos_of_pos_of_lt_pi_div_two hx0 hxp)
| or.inl hx0, or.inr hxp := by simp [hxp, tan_eq_sin_div_cos]
| or.inr hx0, _ := by simp [hx0.symm]
end
lemma tan_neg_of_neg_of_pi_div_two_lt {x : ℝ} (hx0 : x < 0) (hpx : -(π / 2) < x) : tan x < 0 :=
neg_pos.1 (tan_neg x ▸ tan_pos_of_pos_of_lt_pi_div_two (by linarith) (by linarith [pi_pos]))
lemma tan_nonpos_of_nonpos_of_neg_pi_div_two_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -(π / 2) ≤ x) :
tan x ≤ 0 :=
neg_nonneg.1 (tan_neg x ▸ tan_nonneg_of_nonneg_of_le_pi_div_two (by linarith) (by linarith))
lemma tan_lt_tan_of_nonneg_of_lt_pi_div_two {x y : ℝ}
(hx₁ : 0 ≤ x) (hy₂ : y < π / 2) (hxy : x < y) :
tan x < tan y :=
begin
rw [tan_eq_sin_div_cos, tan_eq_sin_div_cos],
exact div_lt_div
(sin_lt_sin_of_lt_of_le_pi_div_two (by linarith) (le_of_lt hy₂) hxy)
(cos_le_cos_of_nonneg_of_le_pi hx₁ (by linarith) (le_of_lt hxy))
(sin_nonneg_of_nonneg_of_le_pi (by linarith) (by linarith))
(cos_pos_of_mem_Ioo ⟨by linarith, hy₂⟩)
end
lemma tan_lt_tan_of_lt_of_lt_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) < x)
(hy₂ : y < π / 2) (hxy : x < y) : tan x < tan y :=
match le_total x 0, le_total y 0 with
| or.inl hx0, or.inl hy0 := neg_lt_neg_iff.1 $ by rw [← tan_neg, ← tan_neg]; exact
tan_lt_tan_of_nonneg_of_lt_pi_div_two (neg_nonneg.2 hy0)
(neg_lt.2 hx₁) (neg_lt_neg hxy)
| or.inl hx0, or.inr hy0 := (lt_or_eq_of_le hy0).elim
(λ hy0, calc tan x ≤ 0 : tan_nonpos_of_nonpos_of_neg_pi_div_two_le hx0 (le_of_lt hx₁)
... < tan y : tan_pos_of_pos_of_lt_pi_div_two hy0 hy₂)
(λ hy0, by rw [← hy0, tan_zero]; exact
tan_neg_of_neg_of_pi_div_two_lt (hy0.symm ▸ hxy) hx₁)
| or.inr hx0, or.inl hy0 := by linarith
| or.inr hx0, or.inr hy0 := tan_lt_tan_of_nonneg_of_lt_pi_div_two hx0 hy₂ hxy
end
lemma strict_mono_incr_on_tan : strict_mono_incr_on tan (Ioo (-(π / 2)) (π / 2)) :=
λ x hx y hy, tan_lt_tan_of_lt_of_lt_pi_div_two hx.1 hy.2
lemma inj_on_tan : inj_on tan (Ioo (-(π / 2)) (π / 2)) :=
strict_mono_incr_on_tan.inj_on
lemma tan_inj_of_lt_of_lt_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2)
(hy₁ : -(π / 2) < y) (hy₂ : y < π / 2) (hxy : tan x = tan y) : x = y :=
inj_on_tan ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ hxy
end real
namespace complex
open_locale real
/-- `arg` returns values in the range (-π, π], such that for `x ≠ 0`,
`sin (arg x) = x.im / x.abs` and `cos (arg x) = x.re / x.abs`,
`arg 0` defaults to `0` -/
noncomputable def arg (x : ℂ) : ℝ :=
if 0 ≤ x.re
then real.arcsin (x.im / x.abs)
else if 0 ≤ x.im
then real.arcsin ((-x).im / x.abs) + π
else real.arcsin ((-x).im / x.abs) - π
lemma measurable_arg : measurable arg :=
have A : measurable (λ x : ℂ, real.arcsin (x.im / x.abs)),
from real.measurable_arcsin.comp (measurable_im.div measurable_norm),
have B : measurable (λ x : ℂ, real.arcsin ((-x).im / x.abs)),
from real.measurable_arcsin.comp ((measurable_im.comp measurable_neg).div measurable_norm),
measurable.ite (is_closed_le continuous_const continuous_re).measurable_set A $
measurable.ite (is_closed_le continuous_const continuous_im).measurable_set
(B.add_const _) (B.sub_const _)
lemma arg_le_pi (x : ℂ) : arg x ≤ π :=
if hx₁ : 0 ≤ x.re
then by rw [arg, if_pos hx₁];
exact le_trans (real.arcsin_le_pi_div_two _) (le_of_lt (half_lt_self real.pi_pos))
else
if hx₂ : 0 ≤ x.im
then by rw [arg, if_neg hx₁, if_pos hx₂, ← le_sub_iff_add_le, sub_self, real.arcsin_nonpos,
neg_im, neg_div, neg_nonpos];
exact div_nonneg hx₂ (abs_nonneg _)
else by rw [arg, if_neg hx₁, if_neg hx₂];
exact sub_le_iff_le_add.2 (le_trans (real.arcsin_le_pi_div_two _)
(by linarith [real.pi_pos]))
lemma neg_pi_lt_arg (x : ℂ) : -π < arg x :=
if hx₁ : 0 ≤ x.re
then by rw [arg, if_pos hx₁];
exact lt_of_lt_of_le (neg_lt_neg (half_lt_self real.pi_pos)) (real.neg_pi_div_two_le_arcsin _)
else
have hx : x ≠ 0, from λ h, by simpa [h, lt_irrefl] using hx₁,
if hx₂ : 0 ≤ x.im
then by rw [arg, if_neg hx₁, if_pos hx₂, ← sub_lt_iff_lt_add];
exact (lt_of_lt_of_le (by linarith [real.pi_pos]) (real.neg_pi_div_two_le_arcsin _))
else by rw [arg, if_neg hx₁, if_neg hx₂, lt_sub_iff_add_lt, neg_add_self, real.arcsin_pos,
neg_im];
exact div_pos (neg_pos.2 (lt_of_not_ge hx₂)) (abs_pos.2 hx)
lemma arg_eq_arg_neg_add_pi_of_im_nonneg_of_re_neg {x : ℂ} (hxr : x.re < 0) (hxi : 0 ≤ x.im) :
arg x = arg (-x) + π :=
have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos],
by rw [arg, arg, if_neg (not_le.2 hxr), if_pos this, if_pos hxi, abs_neg]
lemma arg_eq_arg_neg_sub_pi_of_im_neg_of_re_neg {x : ℂ} (hxr : x.re < 0) (hxi : x.im < 0) :
arg x = arg (-x) - π :=
have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos],
by rw [arg, arg, if_neg (not_le.2 hxr), if_neg (not_le.2 hxi), if_pos this, abs_neg]
@[simp] lemma arg_zero : arg 0 = 0 :=
by simp [arg, le_refl]
@[simp] lemma arg_one : arg 1 = 0 :=
by simp [arg, zero_le_one]
@[simp] lemma arg_neg_one : arg (-1) = π :=
by simp [arg, le_refl, not_le.2 (@zero_lt_one ℝ _ _)]
@[simp] lemma arg_I : arg I = π / 2 :=
by simp [arg, le_refl]
@[simp] lemma arg_neg_I : arg (-I) = -(π / 2) :=
by simp [arg, le_refl]
lemma sin_arg (x : ℂ) : real.sin (arg x) = x.im / x.abs :=
by unfold arg; split_ifs;
simp [sub_eq_add_neg, arg, real.sin_arcsin (abs_le.1 (abs_im_div_abs_le_one x)).1
(abs_le.1 (abs_im_div_abs_le_one x)).2, real.sin_add, neg_div, real.arcsin_neg,
real.sin_neg]
private lemma cos_arg_of_re_nonneg {x : ℂ} (hx : x ≠ 0) (hxr : 0 ≤ x.re) :
real.cos (arg x) = x.re / x.abs :=
have 0 ≤ 1 - (x.im / abs x) ^ 2,
from sub_nonneg.2 $ by rw [sq, ← _root_.abs_mul_self, _root_.abs_mul, ← sq];
exact pow_le_one _ (_root_.abs_nonneg _) (abs_im_div_abs_le_one _),
by rw [eq_div_iff_mul_eq (mt abs_eq_zero.1 hx), ← real.mul_self_sqrt (abs_nonneg x),
arg, if_pos hxr, real.cos_arcsin (abs_le.1 (abs_im_div_abs_le_one x)).1
(abs_le.1 (abs_im_div_abs_le_one x)).2, ← real.sqrt_mul (abs_nonneg _), ← real.sqrt_mul this,
sub_mul, div_pow, ← sq, div_mul_cancel _ (pow_ne_zero 2 (mt abs_eq_zero.1 hx)),
one_mul, sq, mul_self_abs, norm_sq_apply, sq, add_sub_cancel, real.sqrt_mul_self hxr]
lemma cos_arg {x : ℂ} (hx : x ≠ 0) : real.cos (arg x) = x.re / x.abs :=
if hxr : 0 ≤ x.re then cos_arg_of_re_nonneg hx hxr
else
have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos] using hxr,
if hxi : 0 ≤ x.im
then have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos] using hxr,
by rw [arg_eq_arg_neg_add_pi_of_im_nonneg_of_re_neg (not_le.1 hxr) hxi, real.cos_add_pi,
cos_arg_of_re_nonneg (neg_ne_zero.2 hx) this];
simp [neg_div]
else by rw [arg_eq_arg_neg_sub_pi_of_im_neg_of_re_neg (not_le.1 hxr) (not_le.1 hxi)];
simp [sub_eq_add_neg, real.cos_add, neg_div, cos_arg_of_re_nonneg (neg_ne_zero.2 hx) this]
lemma tan_arg {x : ℂ} : real.tan (arg x) = x.im / x.re :=
begin
by_cases h : x = 0,
{ simp only [h, zero_div, complex.zero_im, complex.arg_zero, real.tan_zero, complex.zero_re] },
rw [real.tan_eq_sin_div_cos, sin_arg, cos_arg h,
div_div_div_cancel_right _ (mt abs_eq_zero.1 h)]
end
lemma arg_cos_add_sin_mul_I {x : ℝ} (hx₁ : -π < x) (hx₂ : x ≤ π) :
arg (cos x + sin x * I) = x :=
if hx₃ : -(π / 2) ≤ x ∧ x ≤ π / 2
then
have hx₄ : 0 ≤ (cos x + sin x * I).re,
by simp; exact real.cos_nonneg_of_mem_Icc hx₃,
by rw [arg, if_pos hx₄];
simp [abs_cos_add_sin_mul_I, sin_of_real_re, real.arcsin_sin hx₃.1 hx₃.2]
else if hx₄ : x < -(π / 2)
then
have hx₅ : ¬0 ≤ (cos x + sin x * I).re :=
suffices ¬ 0 ≤ real.cos x, by simpa,
not_le.2 $ by rw ← real.cos_neg;
apply real.cos_neg_of_pi_div_two_lt_of_lt; linarith,
have hx₆ : ¬0 ≤ (cos ↑x + sin ↑x * I).im :=
suffices real.sin x < 0, by simpa,
by apply real.sin_neg_of_neg_of_neg_pi_lt; linarith,
suffices -π + -real.arcsin (real.sin x) = x,
by rw [arg, if_neg hx₅, if_neg hx₆];
simpa [sub_eq_add_neg, add_comm, abs_cos_add_sin_mul_I, sin_of_real_re],
by rw [← real.arcsin_neg, ← real.sin_add_pi, real.arcsin_sin]; try {simp [add_left_comm]};
linarith
else
have hx₅ : π / 2 < x, by cases not_and_distrib.1 hx₃; linarith,
have hx₆ : ¬0 ≤ (cos x + sin x * I).re :=
suffices ¬0 ≤ real.cos x, by simpa,
not_le.2 $ by apply real.cos_neg_of_pi_div_two_lt_of_lt; linarith,
have hx₇ : 0 ≤ (cos x + sin x * I).im :=
suffices 0 ≤ real.sin x, by simpa,
by apply real.sin_nonneg_of_nonneg_of_le_pi; linarith,
suffices π - real.arcsin (real.sin x) = x,
by rw [arg, if_neg hx₆, if_pos hx₇];
simpa [sub_eq_add_neg, add_comm, abs_cos_add_sin_mul_I, sin_of_real_re],
by rw [← real.sin_pi_sub, real.arcsin_sin]; simp [sub_eq_add_neg]; linarith
lemma arg_eq_arg_iff {x y : ℂ} (hx : x ≠ 0) (hy : y ≠ 0) :
arg x = arg y ↔ (abs y / abs x : ℂ) * x = y :=
have hax : abs x ≠ 0, from (mt abs_eq_zero.1 hx),
have hay : abs y ≠ 0, from (mt abs_eq_zero.1 hy),
⟨λ h,
begin
have hcos := congr_arg real.cos h,
rw [cos_arg hx, cos_arg hy, div_eq_div_iff hax hay] at hcos,
have hsin := congr_arg real.sin h,
rw [sin_arg, sin_arg, div_eq_div_iff hax hay] at hsin,
apply complex.ext,
{ rw [mul_re, ← of_real_div, of_real_re, of_real_im, zero_mul, sub_zero, mul_comm,
← mul_div_assoc, hcos, mul_div_cancel _ hax] },
{ rw [mul_im, ← of_real_div, of_real_re, of_real_im, zero_mul, add_zero,
mul_comm, ← mul_div_assoc, hsin, mul_div_cancel _ hax] }
end,
λ h,
have hre : abs (y / x) * x.re = y.re,
by rw ← of_real_div at h;
simpa [-of_real_div, -is_R_or_C.of_real_div] using congr_arg re h,
have hre' : abs (x / y) * y.re = x.re,
by rw [← hre, abs_div, abs_div, ← mul_assoc, div_mul_div,
mul_comm (abs _), div_self (mul_ne_zero hay hax), one_mul],
have him : abs (y / x) * x.im = y.im,
by rw ← of_real_div at h;
simpa [-of_real_div, -is_R_or_C.of_real_div] using congr_arg im h,
have him' : abs (x / y) * y.im = x.im,
by rw [← him, abs_div, abs_div, ← mul_assoc, div_mul_div,
mul_comm (abs _), div_self (mul_ne_zero hay hax), one_mul],
have hxya : x.im / abs x = y.im / abs y,
by rw [← him, abs_div, mul_comm, ← mul_div_comm, mul_div_cancel_left _ hay],
have hnxya : (-x).im / abs x = (-y).im / abs y,
by rw [neg_im, neg_im, neg_div, neg_div, hxya],
if hxr : 0 ≤ x.re
then
have hyr : 0 ≤ y.re, from hre ▸ mul_nonneg (abs_nonneg _) hxr,
by simp [arg, *] at *
else
have hyr : ¬ 0 ≤ y.re, from λ hyr, hxr $ hre' ▸ mul_nonneg (abs_nonneg _) hyr,
if hxi : 0 ≤ x.im
then
have hyi : 0 ≤ y.im, from him ▸ mul_nonneg (abs_nonneg _) hxi,
by simp [arg, *] at *
else
have hyi : ¬ 0 ≤ y.im, from λ hyi, hxi $ him' ▸ mul_nonneg (abs_nonneg _) hyi,
by simp [arg, *] at *⟩
lemma arg_real_mul (x : ℂ) {r : ℝ} (hr : 0 < r) : arg (r * x) = arg x :=
if hx : x = 0 then by simp [hx]
else (arg_eq_arg_iff (mul_ne_zero (of_real_ne_zero.2 (ne_of_lt hr).symm) hx) hx).2 $
by rw [abs_mul, abs_of_nonneg (le_of_lt hr), ← mul_assoc,
of_real_mul, mul_comm (r : ℂ), ← div_div_eq_div_mul,
div_mul_cancel _ (of_real_ne_zero.2 (ne_of_lt hr).symm),
div_self (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), one_mul]
lemma ext_abs_arg {x y : ℂ} (h₁ : x.abs = y.abs) (h₂ : x.arg = y.arg) : x = y :=
if hy : y = 0 then by simp * at *
else have hx : x ≠ 0, from λ hx, by simp [*, eq_comm] at *,
by rwa [arg_eq_arg_iff hx hy, h₁, div_self (of_real_ne_zero.2 (mt abs_eq_zero.1 hy)), one_mul]
at h₂
lemma arg_of_real_of_nonneg {x : ℝ} (hx : 0 ≤ x) : arg x = 0 :=
by simp [arg, hx]
lemma arg_eq_pi_iff {z : ℂ} : arg z = π ↔ z.re < 0 ∧ z.im = 0 :=
begin
by_cases h₀ : z = 0, { simp [h₀, lt_irrefl, real.pi_ne_zero.symm] },
have h₀' : (abs z : ℂ) ≠ 0, by simpa,
rw [← arg_neg_one, arg_eq_arg_iff h₀ (neg_ne_zero.2 one_ne_zero), abs_neg, abs_one,
of_real_one, one_div, ← div_eq_inv_mul, div_eq_iff_mul_eq h₀', neg_one_mul,
ext_iff, neg_im, of_real_im, neg_zero, @eq_comm _ z.im, and.congr_left_iff],
rcases z with ⟨x, y⟩, simp only,
rintro rfl,
simp only [← of_real_def, of_real_eq_zero] at *,
simp [← ne.le_iff_lt h₀, @neg_eq_iff_neg_eq _ _ _ x, @eq_comm _ (-x)]
end
lemma arg_of_real_of_neg {x : ℝ} (hx : x < 0) : arg x = π :=
arg_eq_pi_iff.2 ⟨hx, rfl⟩
/-- Inverse of the `exp` function. Returns values such that `(log x).im > - π` and `(log x).im ≤ π`.
`log 0 = 0`-/
@[pp_nodot] noncomputable def log (x : ℂ) : ℂ := x.abs.log + arg x * I
lemma measurable_log : measurable log :=
(measurable_of_real.comp $ real.measurable_log.comp measurable_norm).add $
(measurable_of_real.comp measurable_arg).mul_const I
lemma log_re (x : ℂ) : x.log.re = x.abs.log := by simp [log]
lemma log_im (x : ℂ) : x.log.im = x.arg := by simp [log]
lemma neg_pi_lt_log_im (x : ℂ) : -π < (log x).im := by simp only [log_im, neg_pi_lt_arg]
lemma log_im_le_pi (x : ℂ) : (log x).im ≤ π := by simp only [log_im, arg_le_pi]
lemma exp_log {x : ℂ} (hx : x ≠ 0) : exp (log x) = x :=
by rw [log, exp_add_mul_I, ← of_real_sin, sin_arg, ← of_real_cos, cos_arg hx,
← of_real_exp, real.exp_log (abs_pos.2 hx), mul_add, of_real_div, of_real_div,
mul_div_cancel' _ (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), ← mul_assoc,
mul_div_cancel' _ (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), re_add_im]
lemma range_exp : range exp = {x | x ≠ 0} :=
set.ext $ λ x, ⟨by { rintro ⟨x, rfl⟩, exact exp_ne_zero x }, λ hx, ⟨log x, exp_log hx⟩⟩
lemma exp_inj_of_neg_pi_lt_of_le_pi {x y : ℂ} (hx₁ : -π < x.im) (hx₂ : x.im ≤ π)
(hy₁ : - π < y.im) (hy₂ : y.im ≤ π) (hxy : exp x = exp y) : x = y :=
by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y] at hxy;
exact complex.ext
(real.exp_injective $
by simpa [abs_mul, abs_cos_add_sin_mul_I] using congr_arg complex.abs hxy)
(by simpa [(of_real_exp _).symm, - of_real_exp, arg_real_mul _ (real.exp_pos _),
arg_cos_add_sin_mul_I hx₁ hx₂, arg_cos_add_sin_mul_I hy₁ hy₂] using congr_arg arg hxy)
lemma log_exp {x : ℂ} (hx₁ : -π < x.im) (hx₂: x.im ≤ π) : log (exp x) = x :=
exp_inj_of_neg_pi_lt_of_le_pi
(by rw log_im; exact neg_pi_lt_arg _)
(by rw log_im; exact arg_le_pi _)
hx₁ hx₂ (by rw [exp_log (exp_ne_zero _)])
lemma of_real_log {x : ℝ} (hx : 0 ≤ x) : (x.log : ℂ) = log x :=
complex.ext
(by rw [log_re, of_real_re, abs_of_nonneg hx])
(by rw [of_real_im, log_im, arg_of_real_of_nonneg hx])
lemma log_of_real_re (x : ℝ) : (log (x : ℂ)).re = real.log x := by simp [log_re]
@[simp] lemma log_zero : log 0 = 0 := by simp [log]
@[simp] lemma log_one : log 1 = 0 := by simp [log]
lemma log_neg_one : log (-1) = π * I := by simp [log]
lemma log_I : log I = π / 2 * I := by simp [log]
lemma log_neg_I : log (-I) = -(π / 2) * I := by simp [log]
lemma exists_pow_nat_eq (x : ℂ) {n : ℕ} (hn : 0 < n) : ∃ z, z ^ n = x :=
begin
by_cases hx : x = 0,
{ use 0, simp only [hx, zero_pow_eq_zero, hn] },
{ use exp (log x / n),
rw [← exp_nat_mul, mul_div_cancel', exp_log hx],
exact_mod_cast (pos_iff_ne_zero.mp hn) }
end
lemma exists_eq_mul_self (x : ℂ) : ∃ z, x = z * z :=
begin
obtain ⟨z, rfl⟩ := exists_pow_nat_eq x zero_lt_two,
exact ⟨z, sq z⟩
end
lemma two_pi_I_ne_zero : (2 * π * I : ℂ) ≠ 0 :=
by norm_num [real.pi_ne_zero, I_ne_zero]
lemma exp_eq_one_iff {x : ℂ} : exp x = 1 ↔ ∃ n : ℤ, x = n * ((2 * π) * I) :=
have real.exp (x.re) * real.cos (x.im) = 1 → real.cos x.im ≠ -1,
from λ h₁ h₂, begin
rw [h₂, mul_neg_eq_neg_mul_symm, mul_one, neg_eq_iff_neg_eq] at h₁,
have := real.exp_pos x.re,
rw ← h₁ at this,
exact absurd this (by norm_num)
end,
calc exp x = 1 ↔ (exp x).re = 1 ∧ (exp x).im = 0 : by simp [complex.ext_iff]
... ↔ real.cos x.im = 1 ∧ real.sin x.im = 0 ∧ x.re = 0 :
begin
rw exp_eq_exp_re_mul_sin_add_cos,
simp [complex.ext_iff, cos_of_real_re, sin_of_real_re, exp_of_real_re,
real.exp_ne_zero],
split; finish [real.sin_eq_zero_iff_cos_eq]
end
... ↔ (∃ n : ℤ, ↑n * (2 * π) = x.im) ∧ (∃ n : ℤ, ↑n * π = x.im) ∧ x.re = 0 :
by rw [real.sin_eq_zero_iff, real.cos_eq_one_iff]
... ↔ ∃ n : ℤ, x = n * ((2 * π) * I) :
⟨λ ⟨⟨n, hn⟩, ⟨m, hm⟩, h⟩, ⟨n, by simp [complex.ext_iff, hn.symm, h]⟩,
λ ⟨n, hn⟩, ⟨⟨n, by simp [hn]⟩, ⟨2 * n, by simp [hn, mul_comm, mul_assoc, mul_left_comm]⟩,
by simp [hn]⟩⟩
lemma exp_eq_exp_iff_exp_sub_eq_one {x y : ℂ} : exp x = exp y ↔ exp (x - y) = 1 :=
by rw [exp_sub, div_eq_one_iff_eq (exp_ne_zero _)]
lemma exp_eq_exp_iff_exists_int {x y : ℂ} : exp x = exp y ↔ ∃ n : ℤ, x = y + n * ((2 * π) * I) :=
by simp only [exp_eq_exp_iff_exp_sub_eq_one, exp_eq_one_iff, sub_eq_iff_eq_add']
/-- `complex.exp` as a `local_homeomorph` with `source = {z | -π < im z < π}` and
`target = {z | 0 < re z} ∪ {z | im z ≠ 0}`. This definition is used to prove that `complex.log`
is complex differentiable at all points but the negative real semi-axis. -/
def exp_local_homeomorph : local_homeomorph ℂ ℂ :=
local_homeomorph.of_continuous_open
{ to_fun := exp,
inv_fun := log,
source := {z : ℂ | z.im ∈ Ioo (- π) π},
target := {z : ℂ | 0 < z.re} ∪ {z : ℂ | z.im ≠ 0},
map_source' :=
begin
rintro ⟨x, y⟩ ⟨h₁ : -π < y, h₂ : y < π⟩,
refine (not_or_of_imp $ λ hz, _).symm,
obtain rfl : y = 0,
{ rw exp_im at hz,
simpa [(real.exp_pos _).ne', real.sin_eq_zero_iff_of_lt_of_lt h₁ h₂] using hz },
rw [mem_set_of_eq, ← of_real_def, exp_of_real_re],
exact real.exp_pos x
end,
map_target' := λ z h,
suffices 0 ≤ z.re ∨ z.im ≠ 0,
by simpa [log_im, neg_pi_lt_arg, (arg_le_pi _).lt_iff_ne, arg_eq_pi_iff, not_and_distrib],
h.imp (λ h, le_of_lt h) id,
left_inv' := λ x hx, log_exp hx.1 (le_of_lt hx.2),
right_inv' := λ x hx, exp_log $ by { rintro rfl, simpa [lt_irrefl] using hx } }
continuous_exp.continuous_on is_open_map_exp (is_open_Ioo.preimage continuous_im)
lemma has_strict_deriv_at_log {x : ℂ} (h : 0 < x.re ∨ x.im ≠ 0) :
has_strict_deriv_at log x⁻¹ x :=
have h0 : x ≠ 0, by { rintro rfl, simpa [lt_irrefl] using h },
exp_local_homeomorph.has_strict_deriv_at_symm h h0 $
by simpa [exp_log h0] using has_strict_deriv_at_exp (log x)
lemma times_cont_diff_at_log {x : ℂ} (h : 0 < x.re ∨ x.im ≠ 0) {n : with_top ℕ} :
times_cont_diff_at ℂ n log x :=
exp_local_homeomorph.times_cont_diff_at_symm_deriv (exp_ne_zero $ log x) h
(has_deriv_at_exp _) times_cont_diff_exp.times_cont_diff_at
@[simp] lemma cos_pi_div_two : cos (π / 2) = 0 :=
calc cos (π / 2) = real.cos (π / 2) : by rw [of_real_cos]; simp
... = 0 : by simp
@[simp] lemma sin_pi_div_two : sin (π / 2) = 1 :=
calc sin (π / 2) = real.sin (π / 2) : by rw [of_real_sin]; simp
... = 1 : by simp
@[simp] lemma sin_pi : sin π = 0 :=
by rw [← of_real_sin, real.sin_pi]; simp
@[simp] lemma cos_pi : cos π = -1 :=
by rw [← of_real_cos, real.cos_pi]; simp
@[simp] lemma sin_two_pi : sin (2 * π) = 0 :=
by simp [two_mul, sin_add]
@[simp] lemma cos_two_pi : cos (2 * π) = 1 :=
by simp [two_mul, cos_add]
lemma sin_add_pi (x : ℂ) : sin (x + π) = -sin x :=
by simp [sin_add]
lemma sin_add_two_pi (x : ℂ) : sin (x + 2 * π) = sin x :=
by simp [sin_add]
lemma cos_add_two_pi (x : ℂ) : cos (x + 2 * π) = cos x :=
by simp [cos_add]
lemma sin_pi_sub (x : ℂ) : sin (π - x) = sin x :=
by simp [sub_eq_add_neg, sin_add]
lemma cos_add_pi (x : ℂ) : cos (x + π) = -cos x :=
by simp [cos_add]
lemma cos_pi_sub (x : ℂ) : cos (π - x) = -cos x :=
by simp [sub_eq_add_neg, cos_add]
lemma sin_add_pi_div_two (x : ℂ) : sin (x + π / 2) = cos x :=
by simp [sin_add]
lemma sin_sub_pi_div_two (x : ℂ) : sin (x - π / 2) = -cos x :=
by simp [sub_eq_add_neg, sin_add]
lemma sin_pi_div_two_sub (x : ℂ) : sin (π / 2 - x) = cos x :=
by simp [sub_eq_add_neg, sin_add]
lemma cos_add_pi_div_two (x : ℂ) : cos (x + π / 2) = -sin x :=
by simp [cos_add]
lemma cos_sub_pi_div_two (x : ℂ) : cos (x - π / 2) = sin x :=
by simp [sub_eq_add_neg, cos_add]
lemma cos_pi_div_two_sub (x : ℂ) : cos (π / 2 - x) = sin x :=
by rw [← cos_neg, neg_sub, cos_sub_pi_div_two]
lemma sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 :=
by induction n; simp [add_mul, sin_add, *]
lemma sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 :=
by cases n; simp [add_mul, sin_add, *, sin_nat_mul_pi]
lemma cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 :=
by induction n; simp [*, mul_add, cos_add, add_mul, cos_two_pi, sin_two_pi]
lemma cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 :=
by cases n; simp only [cos_nat_mul_two_pi, int.of_nat_eq_coe,
int.neg_succ_of_nat_coe, int.cast_coe_nat, int.cast_neg,
(neg_mul_eq_neg_mul _ _).symm, cos_neg]
lemma cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 :=
by simp [cos_add, sin_add, cos_int_mul_two_pi]
lemma exp_pi_mul_I : exp (π * I) = -1 :=
by rw exp_mul_I; simp
theorem cos_eq_zero_iff {θ : ℂ} : cos θ = 0 ↔ ∃ k : ℤ, θ = (2 * k + 1) * π / 2 :=
begin
have h : (exp (θ * I) + exp (-θ * I)) / 2 = 0 ↔ exp (2 * θ * I) = -1,
{ rw [@div_eq_iff _ _ (exp (θ * I) + exp (-θ * I)) 2 0 two_ne_zero', zero_mul,
add_eq_zero_iff_eq_neg, neg_eq_neg_one_mul, ← div_eq_iff (exp_ne_zero _), ← exp_sub],
field_simp only, congr' 3, ring },
rw [cos, h, ← exp_pi_mul_I, exp_eq_exp_iff_exists_int, mul_right_comm],
refine exists_congr (λ x, _),
refine (iff_of_eq $ congr_arg _ _).trans (mul_right_inj' $ mul_ne_zero two_ne_zero' I_ne_zero),
ring,
end
theorem cos_ne_zero_iff {θ : ℂ} : cos θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ (2 * k + 1) * π / 2 :=
by rw [← not_exists, not_iff_not, cos_eq_zero_iff]
theorem sin_eq_zero_iff {θ : ℂ} : sin θ = 0 ↔ ∃ k : ℤ, θ = k * π :=
begin
rw [← complex.cos_sub_pi_div_two, cos_eq_zero_iff],
split,
{ rintros ⟨k, hk⟩,
use k + 1,
field_simp [eq_add_of_sub_eq hk],
ring },
{ rintros ⟨k, rfl⟩,
use k - 1,
field_simp,
ring }
end
theorem sin_ne_zero_iff {θ : ℂ} : sin θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π :=
by rw [← not_exists, not_iff_not, sin_eq_zero_iff]
lemma sin_eq_zero_iff_cos_eq {z : ℂ} : sin z = 0 ↔ cos z = 1 ∨ cos z = -1 :=
by rw [← mul_self_eq_one_iff, ← sin_sq_add_cos_sq, sq, sq, ← sub_eq_iff_eq_add, sub_self];
exact ⟨λ h, by rw [h, mul_zero], eq_zero_of_mul_self_eq_zero ∘ eq.symm⟩
lemma tan_eq_zero_iff {θ : ℂ} : tan θ = 0 ↔ ∃ k : ℤ, θ = k * π / 2 :=
begin
have h := (sin_two_mul θ).symm,
rw mul_assoc at h,
rw [tan, div_eq_zero_iff, ← mul_eq_zero, ← zero_mul ((1/2):ℂ), mul_one_div,
cancel_factors.cancel_factors_eq_div h two_ne_zero', mul_comm],
simpa only [zero_div, zero_mul, ne.def, not_false_iff] with field_simps using sin_eq_zero_iff,
end
lemma tan_ne_zero_iff {θ : ℂ} : tan θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π / 2 :=
by rw [← not_exists, not_iff_not, tan_eq_zero_iff]
lemma tan_int_mul_pi_div_two (n : ℤ) : tan (n * π/2) = 0 :=
tan_eq_zero_iff.mpr (by use n)
lemma tan_int_mul_pi (n : ℤ) : tan (n * π) = 0 :=
by simp [tan, add_mul, sin_add, sin_int_mul_pi]
lemma cos_eq_cos_iff {x y : ℂ} :
cos x = cos y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x :=
calc cos x = cos y ↔ cos x - cos y = 0 : sub_eq_zero.symm
... ↔ -2 * sin((x + y)/2) * sin((x - y)/2) = 0 : by rw cos_sub_cos
... ↔ sin((x + y)/2) = 0 ∨ sin((x - y)/2) = 0 : by simp [(by norm_num : (2:ℂ) ≠ 0)]
... ↔ sin((x - y)/2) = 0 ∨ sin((x + y)/2) = 0 : or.comm
... ↔ (∃ k : ℤ, y = 2 * k * π + x) ∨ (∃ k :ℤ, y = 2 * k * π - x) :
begin
apply or_congr;
field_simp [sin_eq_zero_iff, (by norm_num : -(2:ℂ) ≠ 0), eq_sub_iff_add_eq',
sub_eq_iff_eq_add, mul_comm (2:ℂ), mul_right_comm _ (2:ℂ)],
split; { rintros ⟨k, rfl⟩, use -k, simp, },
end
... ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x : exists_or_distrib.symm
lemma sin_eq_sin_iff {x y : ℂ} :
sin x = sin y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = (2 * k + 1) * π - x :=
begin
simp only [← complex.cos_sub_pi_div_two, cos_eq_cos_iff, sub_eq_iff_eq_add],
refine exists_congr (λ k, or_congr _ _); refine eq.congr rfl _; field_simp; ring
end
lemma tan_add {x y : ℂ}
(h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2)
∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y = (2 * l + 1) * π / 2)) :
tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) :=
begin
rcases h with ⟨h1, h2⟩ | ⟨⟨k, rfl⟩, ⟨l, rfl⟩⟩,
{ rw [tan, sin_add, cos_add,
← div_div_div_cancel_right (sin x * cos y + cos x * sin y)
(mul_ne_zero (cos_ne_zero_iff.mpr h1) (cos_ne_zero_iff.mpr h2)),
add_div, sub_div],
simp only [←div_mul_div, ←tan, mul_one, one_mul,
div_self (cos_ne_zero_iff.mpr h1), div_self (cos_ne_zero_iff.mpr h2)] },
{ obtain ⟨t, hx, hy, hxy⟩ := ⟨tan_int_mul_pi_div_two, t (2*k+1), t (2*l+1), t (2*k+1+(2*l+1))⟩,
simp only [int.cast_add, int.cast_bit0, int.cast_mul, int.cast_one, hx, hy] at hx hy hxy,
rw [hx, hy, add_zero, zero_div,
mul_div_assoc, mul_div_assoc, ← add_mul (2*(k:ℂ)+1) (2*l+1) (π/2), ← mul_div_assoc, hxy] },
end
lemma tan_add' {x y : ℂ}
(h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2)) :
tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) :=
tan_add (or.inl h)
lemma tan_two_mul {z : ℂ} : tan (2 * z) = 2 * tan z / (1 - tan z ^ 2) :=
begin
by_cases h : ∀ k : ℤ, z ≠ (2 * k + 1) * π / 2,
{ rw [two_mul, two_mul, sq, tan_add (or.inl ⟨h, h⟩)] },
{ rw not_forall_not at h,
rw [two_mul, two_mul, sq, tan_add (or.inr ⟨h, h⟩)] },
end
lemma tan_add_mul_I {x y : ℂ}
(h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y * I ≠ (2 * l + 1) * π / 2)
∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y * I = (2 * l + 1) * π / 2)) :
tan (x + y*I) = (tan x + tanh y * I) / (1 - tan x * tanh y * I) :=
by rw [tan_add h, tan_mul_I, mul_assoc]
lemma tan_eq {z : ℂ}
(h : ((∀ k : ℤ, (z.re:ℂ) ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, (z.im:ℂ) * I ≠ (2 * l + 1) * π / 2)
∨ ((∃ k : ℤ, (z.re:ℂ) = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, (z.im:ℂ) * I = (2 * l + 1) * π / 2)) :
tan z = (tan z.re + tanh z.im * I) / (1 - tan z.re * tanh z.im * I) :=
by convert tan_add_mul_I h; exact (re_add_im z).symm
lemma has_strict_deriv_at_tan {x : ℂ} (h : cos x ≠ 0) :
has_strict_deriv_at tan (1 / (cos x)^2) x :=
begin
convert (has_strict_deriv_at_sin x).div (has_strict_deriv_at_cos x) h,
rw ← sin_sq_add_cos_sq x,
ring,
end
lemma has_deriv_at_tan {x : ℂ} (h : cos x ≠ 0) :
has_deriv_at tan (1 / (cos x)^2) x :=
(has_strict_deriv_at_tan h).has_deriv_at
lemma tendsto_abs_tan_of_cos_eq_zero {x : ℂ} (hx : cos x = 0) :
tendsto (λ x, abs (tan x)) (𝓝[{x}ᶜ] x) at_top :=
begin
simp only [tan_eq_sin_div_cos, ← norm_eq_abs, normed_field.norm_div],
have A : sin x ≠ 0 := λ h, by simpa [*, sq] using sin_sq_add_cos_sq x,
have B : tendsto cos (𝓝[{x}ᶜ] (x)) (𝓝[{0}ᶜ] 0),
{ refine tendsto_inf.2 ⟨tendsto.mono_left _ inf_le_left, tendsto_principal.2 _⟩,
exacts [continuous_cos.tendsto' x 0 hx,
hx ▸ (has_deriv_at_cos _).eventually_ne (neg_ne_zero.2 A)] },
exact continuous_sin.continuous_within_at.norm.mul_at_top (norm_pos_iff.2 A)
(tendsto_norm_nhds_within_zero.comp B).inv_tendsto_zero,
end
lemma tendsto_abs_tan_at_top (k : ℤ) :
tendsto (λ x, abs (tan x)) (𝓝[{(2 * k + 1) * π / 2}ᶜ] ((2 * k + 1) * π / 2)) at_top :=
tendsto_abs_tan_of_cos_eq_zero $ cos_eq_zero_iff.2 ⟨k, rfl⟩
@[simp] lemma continuous_at_tan {x : ℂ} : continuous_at tan x ↔ cos x ≠ 0 :=
begin
refine ⟨λ hc h₀, _, λ h, (has_deriv_at_tan h).continuous_at⟩,
exact not_tendsto_nhds_of_tendsto_at_top (tendsto_abs_tan_of_cos_eq_zero h₀) _
(hc.norm.tendsto.mono_left inf_le_left)
end
@[simp] lemma differentiable_at_tan {x : ℂ} : differentiable_at ℂ tan x ↔ cos x ≠ 0:=
⟨λ h, continuous_at_tan.1 h.continuous_at, λ h, (has_deriv_at_tan h).differentiable_at⟩
@[simp] lemma deriv_tan (x : ℂ) : deriv tan x = 1 / (cos x)^2 :=
if h : cos x = 0 then
have ¬differentiable_at ℂ tan x := mt differentiable_at_tan.1 (not_not.2 h),
by simp [deriv_zero_of_not_differentiable_at this, h, sq]
else (has_deriv_at_tan h).deriv
lemma continuous_on_tan : continuous_on tan {x | cos x ≠ 0} :=
continuous_on_sin.div continuous_on_cos $ λ x, id
@[continuity]
lemma continuous_tan : continuous (λ x : {x | cos x ≠ 0}, tan x) :=
continuous_on_iff_continuous_restrict.1 continuous_on_tan
@[simp] lemma times_cont_diff_at_tan {x : ℂ} {n : with_top ℕ} :
times_cont_diff_at ℂ n tan x ↔ cos x ≠ 0 :=
⟨λ h, continuous_at_tan.1 h.continuous_at,
times_cont_diff_sin.times_cont_diff_at.div times_cont_diff_cos.times_cont_diff_at⟩
lemma cos_eq_iff_quadratic {z w : ℂ} :
cos z = w ↔ (exp (z * I)) ^ 2 - 2 * w * exp (z * I) + 1 = 0 :=
begin
rw ← sub_eq_zero,
field_simp [cos, exp_neg, exp_ne_zero],
refine eq.congr _ rfl,
ring
end
lemma cos_surjective : function.surjective cos :=
begin
intro x,
obtain ⟨w, w₀, hw⟩ : ∃ w ≠ 0, 1 * w * w + (-2 * x) * w + 1 = 0,
{ rcases exists_quadratic_eq_zero one_ne_zero (exists_eq_mul_self _) with ⟨w, hw⟩,
refine ⟨w, _, hw⟩,
rintro rfl,
simpa only [zero_add, one_ne_zero, mul_zero] using hw },
refine ⟨log w / I, cos_eq_iff_quadratic.2 _⟩,
rw [div_mul_cancel _ I_ne_zero, exp_log w₀],
convert hw,
ring
end
@[simp] lemma range_cos : range cos = set.univ :=
cos_surjective.range_eq
lemma sin_surjective : function.surjective sin :=
begin
intro x,
rcases cos_surjective x with ⟨z, rfl⟩,
exact ⟨z + π / 2, sin_add_pi_div_two z⟩
end
@[simp] lemma range_sin : range sin = set.univ :=
sin_surjective.range_eq
end complex
section log_deriv
open complex
variables {α : Type*}
lemma measurable.carg [measurable_space α] {f : α → ℂ} (h : measurable f) :
measurable (λ x, arg (f x)) :=
measurable_arg.comp h
lemma measurable.clog [measurable_space α] {f : α → ℂ} (h : measurable f) :
measurable (λ x, log (f x)) :=
measurable_log.comp h
lemma filter.tendsto.clog {l : filter α} {f : α → ℂ} {x : ℂ} (h : tendsto f l (𝓝 x))
(hx : 0 < x.re ∨ x.im ≠ 0) :
tendsto (λ t, log (f t)) l (𝓝 $ log x) :=
(has_strict_deriv_at_log hx).continuous_at.tendsto.comp h
variables [topological_space α]
lemma continuous_at.clog {f : α → ℂ} {x : α} (h₁ : continuous_at f x)
(h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) :
continuous_at (λ t, log (f t)) x :=
h₁.clog h₂
lemma continuous_within_at.clog {f : α → ℂ} {s : set α} {x : α} (h₁ : continuous_within_at f s x)
(h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) :
continuous_within_at (λ t, log (f t)) s x :=
h₁.clog h₂
lemma continuous_on.clog {f : α → ℂ} {s : set α} (h₁ : continuous_on f s)
(h₂ : ∀ x ∈ s, 0 < (f x).re ∨ (f x).im ≠ 0) :
continuous_on (λ t, log (f t)) s :=
λ x hx, (h₁ x hx).clog (h₂ x hx)
lemma continuous.clog {f : α → ℂ} (h₁ : continuous f) (h₂ : ∀ x, 0 < (f x).re ∨ (f x).im ≠ 0) :
continuous (λ t, log (f t)) :=
continuous_iff_continuous_at.2 $ λ x, h₁.continuous_at.clog (h₂ x)
variables {E : Type*} [normed_group E] [normed_space ℂ E]
lemma has_strict_fderiv_at.clog {f : E → ℂ} {f' : E →L[ℂ] ℂ} {x : E}
(h₁ : has_strict_fderiv_at f f' x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) :
has_strict_fderiv_at (λ t, log (f t)) ((f x)⁻¹ • f') x :=
(has_strict_deriv_at_log h₂).comp_has_strict_fderiv_at x h₁
lemma has_strict_deriv_at.clog {f : ℂ → ℂ} {f' x : ℂ} (h₁ : has_strict_deriv_at f f' x)
(h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) :
has_strict_deriv_at (λ t, log (f t)) (f' / f x) x :=
by { rw div_eq_inv_mul, exact (has_strict_deriv_at_log h₂).comp x h₁ }
lemma has_fderiv_at.clog {f : E → ℂ} {f' : E →L[ℂ] ℂ} {x : E}
(h₁ : has_fderiv_at f f' x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) :
has_fderiv_at (λ t, log (f t)) ((f x)⁻¹ • f') x :=
(has_strict_deriv_at_log h₂).has_deriv_at.comp_has_fderiv_at x h₁
lemma has_deriv_at.clog {f : ℂ → ℂ} {f' x : ℂ} (h₁ : has_deriv_at f f' x)
(h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) :
has_deriv_at (λ t, log (f t)) (f' / f x) x :=
by { rw div_eq_inv_mul, exact (has_strict_deriv_at_log h₂).has_deriv_at.comp x h₁ }
lemma differentiable_at.clog {f : E → ℂ} {x : E} (h₁ : differentiable_at ℂ f x)
(h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) :
differentiable_at ℂ (λ t, log (f t)) x :=
(h₁.has_fderiv_at.clog h₂).differentiable_at
lemma has_fderiv_within_at.clog {f : E → ℂ} {f' : E →L[ℂ] ℂ} {s : set E} {x : E}
(h₁ : has_fderiv_within_at f f' s x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) :
has_fderiv_within_at (λ t, log (f t)) ((f x)⁻¹ • f') s x :=
(has_strict_deriv_at_log h₂).has_deriv_at.comp_has_fderiv_within_at x h₁
lemma has_deriv_within_at.clog {f : ℂ → ℂ} {f' x : ℂ} {s : set ℂ}
(h₁ : has_deriv_within_at f f' s x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) :
has_deriv_within_at (λ t, log (f t)) (f' / f x) s x :=
by { rw div_eq_inv_mul,
exact (has_strict_deriv_at_log h₂).has_deriv_at.comp_has_deriv_within_at x h₁ }
lemma differentiable_within_at.clog {f : E → ℂ} {s : set E} {x : E}
(h₁ : differentiable_within_at ℂ f s x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) :
differentiable_within_at ℂ (λ t, log (f t)) s x :=
(h₁.has_fderiv_within_at.clog h₂).differentiable_within_at
lemma differentiable_on.clog {f : E → ℂ} {s : set E}
(h₁ : differentiable_on ℂ f s) (h₂ : ∀ x ∈ s, 0 < (f x).re ∨ (f x).im ≠ 0) :
differentiable_on ℂ (λ t, log (f t)) s :=
λ x hx, (h₁ x hx).clog (h₂ x hx)
lemma differentiable.clog {f : E → ℂ} (h₁ : differentiable ℂ f)
(h₂ : ∀ x, 0 < (f x).re ∨ (f x).im ≠ 0) :
differentiable ℂ (λ t, log (f t)) :=
λ x, (h₁ x).clog (h₂ x)
end log_deriv
namespace polynomial.chebyshev
open polynomial complex
/-- The `n`-th Chebyshev polynomial of the first kind evaluates on `cos θ` to the
value `cos (n * θ)`. -/
lemma T_complex_cos (θ : ℂ) :
∀ n, (T ℂ n).eval (cos θ) = cos (n * θ)
| 0 := by simp only [T_zero, eval_one, nat.cast_zero, zero_mul, cos_zero]
| 1 := by simp only [eval_X, one_mul, T_one, nat.cast_one]
| (n + 2) :=
begin
simp only [eval_X, eval_one, T_add_two, eval_sub, eval_bit0, nat.cast_succ, eval_mul],
rw [T_complex_cos (n + 1), T_complex_cos n],
have aux : sin θ * sin θ = 1 - cos θ * cos θ,
{ rw ← sin_sq_add_cos_sq θ, ring, },
simp only [nat.cast_add, nat.cast_one, add_mul, cos_add, one_mul, sin_add, mul_assoc, aux],
ring,
end
/-- `cos (n * θ)` is equal to the `n`-th Chebyshev polynomial of the first kind evaluated
on `cos θ`. -/
lemma cos_nat_mul (n : ℕ) (θ : ℂ) :
cos (n * θ) = (T ℂ n).eval (cos θ) :=
(T_complex_cos θ n).symm
/-- The `n`-th Chebyshev polynomial of the second kind evaluates on `cos θ` to the
value `sin ((n+1) * θ) / sin θ`. -/
lemma U_complex_cos (θ : ℂ) (n : ℕ) :
(U ℂ n).eval (cos θ) * sin θ = sin ((n+1) * θ) :=
begin
induction n with d hd,
{ simp only [U_zero, nat.cast_zero, eval_one, mul_one, zero_add, one_mul] },
{ rw U_eq_X_mul_U_add_T,
simp only [eval_add, eval_mul, eval_X, T_complex_cos, add_mul, mul_assoc, hd, one_mul],
conv_rhs { rw [sin_add, mul_comm] },
push_cast,
simp only [add_mul, one_mul] }
end
/-- `sin ((n + 1) * θ)` is equal to `sin θ` multiplied with the `n`-th Chebyshev polynomial of the
second kind evaluated on `cos θ`. -/
lemma sin_nat_succ_mul (n : ℕ) (θ : ℂ) :
sin ((n + 1) * θ) = (U ℂ n).eval (cos θ) * sin θ :=
(U_complex_cos θ n).symm
end polynomial.chebyshev
namespace real
open_locale real
lemma tan_add {x y : ℝ}
(h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2)
∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y = (2 * l + 1) * π / 2)) :
tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) :=
by simpa only [← complex.of_real_inj, complex.of_real_sub, complex.of_real_add, complex.of_real_div,
complex.of_real_mul, complex.of_real_tan]
using @complex.tan_add (x:ℂ) (y:ℂ) (by convert h; norm_cast)
lemma tan_add' {x y : ℝ}
(h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2)) :
tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) :=
tan_add (or.inl h)
lemma tan_two_mul {x:ℝ} : tan (2 * x) = 2 * tan x / (1 - tan x ^ 2) :=
by simpa only [← complex.of_real_inj, complex.of_real_sub, complex.of_real_div, complex.of_real_pow,
complex.of_real_mul, complex.of_real_tan, complex.of_real_bit0, complex.of_real_one]
using complex.tan_two_mul
theorem cos_eq_zero_iff {θ : ℝ} : cos θ = 0 ↔ ∃ k : ℤ, θ = (2 * k + 1) * π / 2 :=
by exact_mod_cast @complex.cos_eq_zero_iff θ
theorem cos_ne_zero_iff {θ : ℝ} : cos θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ (2 * k + 1) * π / 2 :=
by rw [← not_exists, not_iff_not, cos_eq_zero_iff]
lemma tan_ne_zero_iff {θ : ℝ} : tan θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π / 2 :=
by rw [← complex.of_real_ne_zero, complex.of_real_tan, complex.tan_ne_zero_iff]; norm_cast
lemma tan_eq_zero_iff {θ : ℝ} : tan θ = 0 ↔ ∃ k : ℤ, θ = k * π / 2 :=
by rw [← not_iff_not, not_exists, ← ne, tan_ne_zero_iff]
lemma tan_int_mul_pi_div_two (n : ℤ) : tan (n * π/2) = 0 :=
tan_eq_zero_iff.mpr (by use n)
lemma tan_int_mul_pi (n : ℤ) : tan (n * π) = 0 :=
by rw tan_eq_zero_iff; use (2*n); field_simp [mul_comm ((n:ℝ)*(π:ℝ)) 2, ← mul_assoc]
lemma cos_eq_cos_iff {x y : ℝ} :
cos x = cos y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x :=
by exact_mod_cast @complex.cos_eq_cos_iff x y
lemma sin_eq_sin_iff {x y : ℝ} :
sin x = sin y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = (2 * k + 1) * π - x :=
by exact_mod_cast @complex.sin_eq_sin_iff x y
lemma has_strict_deriv_at_tan {x : ℝ} (h : cos x ≠ 0) :
has_strict_deriv_at tan (1 / (cos x)^2) x :=
by exact_mod_cast (complex.has_strict_deriv_at_tan (by exact_mod_cast h)).real_of_complex
lemma has_deriv_at_tan {x : ℝ} (h : cos x ≠ 0) :
has_deriv_at tan (1 / (cos x)^2) x :=
by exact_mod_cast (complex.has_deriv_at_tan (by exact_mod_cast h)).real_of_complex
lemma tendsto_abs_tan_of_cos_eq_zero {x : ℝ} (hx : cos x = 0) :
tendsto (λ x, abs (tan x)) (𝓝[{x}ᶜ] x) at_top :=
begin
have hx : complex.cos x = 0, by exact_mod_cast hx,
simp only [← complex.abs_of_real, complex.of_real_tan],
refine (complex.tendsto_abs_tan_of_cos_eq_zero hx).comp _,
refine tendsto.inf complex.continuous_of_real.continuous_at _,
exact tendsto_principal_principal.2 (λ y, mt complex.of_real_inj.1)
end
lemma tendsto_abs_tan_at_top (k : ℤ) :
tendsto (λ x, abs (tan x)) (𝓝[{(2 * k + 1) * π / 2}ᶜ] ((2 * k + 1) * π / 2)) at_top :=
tendsto_abs_tan_of_cos_eq_zero $ cos_eq_zero_iff.2 ⟨k, rfl⟩
lemma continuous_at_tan {x : ℝ} : continuous_at tan x ↔ cos x ≠ 0 :=
begin
refine ⟨λ hc h₀, _, λ h, (has_deriv_at_tan h).continuous_at⟩,
exact not_tendsto_nhds_of_tendsto_at_top (tendsto_abs_tan_of_cos_eq_zero h₀) _
(hc.norm.tendsto.mono_left inf_le_left)
end
lemma differentiable_at_tan {x : ℝ} : differentiable_at ℝ tan x ↔ cos x ≠ 0 :=
⟨λ h, continuous_at_tan.1 h.continuous_at, λ h, (has_deriv_at_tan h).differentiable_at⟩
@[simp] lemma deriv_tan (x : ℝ) : deriv tan x = 1 / (cos x)^2 :=
if h : cos x = 0 then
have ¬differentiable_at ℝ tan x := mt differentiable_at_tan.1 (not_not.2 h),
by simp [deriv_zero_of_not_differentiable_at this, h, sq]
else (has_deriv_at_tan h).deriv
@[simp] lemma times_cont_diff_at_tan {n x} : times_cont_diff_at ℝ n tan x ↔ cos x ≠ 0 :=
⟨λ h, continuous_at_tan.1 h.continuous_at,
λ h, (complex.times_cont_diff_at_tan.2 $ by exact_mod_cast h).real_of_complex⟩
lemma continuous_on_tan : continuous_on tan {x | cos x ≠ 0} :=
λ x hx, (continuous_at_tan.2 hx).continuous_within_at
@[continuity]
lemma continuous_tan : continuous (λ x : {x | cos x ≠ 0}, tan x) :=
continuous_on_iff_continuous_restrict.1 continuous_on_tan
lemma has_deriv_at_tan_of_mem_Ioo {x : ℝ} (h : x ∈ Ioo (-(π/2):ℝ) (π/2)) :
has_deriv_at tan (1 / (cos x)^2) x :=
has_deriv_at_tan (cos_pos_of_mem_Ioo h).ne'
lemma differentiable_at_tan_of_mem_Ioo {x : ℝ} (h : x ∈ Ioo (-(π/2):ℝ) (π/2)) :
differentiable_at ℝ tan x :=
(has_deriv_at_tan_of_mem_Ioo h).differentiable_at
lemma continuous_on_tan_Ioo : continuous_on tan (Ioo (-(π/2)) (π/2)) :=
λ x hx, (differentiable_at_tan_of_mem_Ioo hx).continuous_at.continuous_within_at
lemma tendsto_sin_pi_div_two : tendsto sin (𝓝[Iio (π/2)] (π/2)) (𝓝 1) :=
by { convert continuous_sin.continuous_within_at, simp }
lemma tendsto_cos_pi_div_two : tendsto cos (𝓝[Iio (π/2)] (π/2)) (𝓝[Ioi 0] 0) :=
begin
apply tendsto_nhds_within_of_tendsto_nhds_of_eventually_within,
{ convert continuous_cos.continuous_within_at, simp },
{ filter_upwards [Ioo_mem_nhds_within_Iio (right_mem_Ioc.mpr (norm_num.lt_neg_pos
_ _ pi_div_two_pos pi_div_two_pos))] λ x hx, cos_pos_of_mem_Ioo hx },
end
lemma tendsto_tan_pi_div_two : tendsto tan (𝓝[Iio (π/2)] (π/2)) at_top :=
begin
convert tendsto_cos_pi_div_two.inv_tendsto_zero.at_top_mul zero_lt_one
tendsto_sin_pi_div_two,
simp only [pi.inv_apply, ← div_eq_inv_mul, ← tan_eq_sin_div_cos]
end
lemma tendsto_sin_neg_pi_div_two : tendsto sin (𝓝[Ioi (-(π/2))] (-(π/2))) (𝓝 (-1)) :=
by { convert continuous_sin.continuous_within_at, simp }
lemma tendsto_cos_neg_pi_div_two : tendsto cos (𝓝[Ioi (-(π/2))] (-(π/2))) (𝓝[Ioi 0] 0) :=
begin
apply tendsto_nhds_within_of_tendsto_nhds_of_eventually_within,
{ convert continuous_cos.continuous_within_at, simp },
{ filter_upwards [Ioo_mem_nhds_within_Ioi (left_mem_Ico.mpr (norm_num.lt_neg_pos
_ _ pi_div_two_pos pi_div_two_pos))] λ x hx, cos_pos_of_mem_Ioo hx },
end
lemma tendsto_tan_neg_pi_div_two : tendsto tan (𝓝[Ioi (-(π/2))] (-(π/2))) at_bot :=
begin
convert tendsto_cos_neg_pi_div_two.inv_tendsto_zero.at_top_mul_neg (by norm_num)
tendsto_sin_neg_pi_div_two,
simp only [pi.inv_apply, ← div_eq_inv_mul, ← tan_eq_sin_div_cos]
end
lemma surj_on_tan : surj_on tan (Ioo (-(π / 2)) (π / 2)) univ :=
have _ := neg_lt_self pi_div_two_pos,
continuous_on_tan_Ioo.surj_on_of_tendsto (nonempty_Ioo.2 this)
(by simp [tendsto_tan_neg_pi_div_two, this]) (by simp [tendsto_tan_pi_div_two, this])
lemma tan_surjective : function.surjective tan :=
λ x, surj_on_tan.subset_range trivial
lemma image_tan_Ioo : tan '' (Ioo (-(π / 2)) (π / 2)) = univ :=
univ_subset_iff.1 surj_on_tan
/-- `real.tan` as an `order_iso` between `(-(π / 2), π / 2)` and `ℝ`. -/
def tan_order_iso : Ioo (-(π / 2)) (π / 2) ≃o ℝ :=
(strict_mono_incr_on_tan.order_iso _ _).trans $ (order_iso.set_congr _ _ image_tan_Ioo).trans
order_iso.set.univ
/-- Inverse of the `tan` function, returns values in the range `-π / 2 < arctan x` and
`arctan x < π / 2` -/
@[pp_nodot] noncomputable def arctan (x : ℝ) : ℝ :=
tan_order_iso.symm x
@[simp] lemma tan_arctan (x : ℝ) : tan (arctan x) = x :=
tan_order_iso.apply_symm_apply x
lemma arctan_mem_Ioo (x : ℝ) : arctan x ∈ Ioo (-(π / 2)) (π / 2) :=
subtype.coe_prop _
lemma arctan_tan {x : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2) : arctan (tan x) = x :=
subtype.ext_iff.1 $ tan_order_iso.symm_apply_apply ⟨x, hx₁, hx₂⟩
lemma cos_arctan_pos (x : ℝ) : 0 < cos (arctan x) :=
cos_pos_of_mem_Ioo $ arctan_mem_Ioo x
lemma cos_sq_arctan (x : ℝ) : cos (arctan x) ^ 2 = 1 / (1 + x ^ 2) :=
by rw [one_div, ← inv_one_add_tan_sq (cos_arctan_pos x).ne', tan_arctan]
lemma sin_arctan (x : ℝ) : sin (arctan x) = x / sqrt (1 + x ^ 2) :=
by rw [← tan_div_sqrt_one_add_tan_sq (cos_arctan_pos x), tan_arctan]
lemma cos_arctan (x : ℝ) : cos (arctan x) = 1 / sqrt (1 + x ^ 2) :=
by rw [one_div, ← inv_sqrt_one_add_tan_sq (cos_arctan_pos x), tan_arctan]
lemma arctan_lt_pi_div_two (x : ℝ) : arctan x < π / 2 :=
(arctan_mem_Ioo x).2
lemma neg_pi_div_two_lt_arctan (x : ℝ) : -(π / 2) < arctan x :=
(arctan_mem_Ioo x).1
lemma arctan_eq_arcsin (x : ℝ) : arctan x = arcsin (x / sqrt (1 + x ^ 2)) :=
eq.symm $ arcsin_eq_of_sin_eq (sin_arctan x) (mem_Icc_of_Ioo $ arctan_mem_Ioo x)
lemma arcsin_eq_arctan {x : ℝ} (h : x ∈ Ioo (-(1:ℝ)) 1) :
arcsin x = arctan (x / sqrt (1 - x ^ 2)) :=
begin
rw [arctan_eq_arcsin, div_pow, sq_sqrt, one_add_div, div_div_eq_div_mul,
← sqrt_mul, mul_div_cancel', sub_add_cancel, sqrt_one, div_one];
nlinarith [h.1, h.2],
end
@[simp] lemma arctan_zero : arctan 0 = 0 :=
by simp [arctan_eq_arcsin]
lemma arctan_eq_of_tan_eq {x y : ℝ} (h : tan x = y) (hx : x ∈ Ioo (-(π / 2)) (π / 2)) :
arctan y = x :=
inj_on_tan (arctan_mem_Ioo _) hx (by rw [tan_arctan, h])
@[simp] lemma arctan_one : arctan 1 = π / 4 :=
arctan_eq_of_tan_eq tan_pi_div_four $ by split; linarith [pi_pos]
@[simp] lemma arctan_neg (x : ℝ) : arctan (-x) = - arctan x :=
by simp [arctan_eq_arcsin, neg_div]
@[continuity]
lemma continuous_arctan : continuous arctan :=
continuous_subtype_coe.comp tan_order_iso.to_homeomorph.continuous_inv_fun
lemma continuous_at_arctan {x : ℝ} : continuous_at arctan x := continuous_arctan.continuous_at
/-- `real.tan` as a `local_homeomorph` between `(-(π / 2), π / 2)` and the whole line. -/
def tan_local_homeomorph : local_homeomorph ℝ ℝ :=
{ to_fun := tan,
inv_fun := arctan,
source := Ioo (-(π / 2)) (π / 2),
target := univ,
map_source' := maps_to_univ _ _,
map_target' := λ y hy, arctan_mem_Ioo y,
left_inv' := λ x hx, arctan_tan hx.1 hx.2,
right_inv' := λ y hy, tan_arctan y,
open_source := is_open_Ioo,
open_target := is_open_univ,
continuous_to_fun := continuous_on_tan_Ioo,
continuous_inv_fun := continuous_arctan.continuous_on }
@[simp] lemma coe_tan_local_homeomorph : ⇑tan_local_homeomorph = tan := rfl
@[simp] lemma coe_tan_local_homeomorph_symm : ⇑tan_local_homeomorph.symm = arctan := rfl
lemma has_strict_deriv_at_arctan (x : ℝ) : has_strict_deriv_at arctan (1 / (1 + x^2)) x :=
have A : cos (arctan x) ≠ 0 := (cos_arctan_pos x).ne',
by simpa [cos_sq_arctan]
using tan_local_homeomorph.has_strict_deriv_at_symm trivial (by simpa) (has_strict_deriv_at_tan A)
lemma has_deriv_at_arctan (x : ℝ) : has_deriv_at arctan (1 / (1 + x^2)) x :=
(has_strict_deriv_at_arctan x).has_deriv_at
lemma differentiable_at_arctan (x : ℝ) : differentiable_at ℝ arctan x :=
(has_deriv_at_arctan x).differentiable_at
lemma differentiable_arctan : differentiable ℝ arctan := differentiable_at_arctan
@[simp] lemma deriv_arctan : deriv arctan = (λ x, 1 / (1 + x^2)) :=
funext $ λ x, (has_deriv_at_arctan x).deriv
lemma times_cont_diff_arctan {n : with_top ℕ} : times_cont_diff ℝ n arctan :=
times_cont_diff_iff_times_cont_diff_at.2 $ λ x,
have cos (arctan x) ≠ 0 := (cos_arctan_pos x).ne',
tan_local_homeomorph.times_cont_diff_at_symm_deriv (by simpa) trivial (has_deriv_at_tan this)
(times_cont_diff_at_tan.2 this)
lemma measurable_arctan : measurable arctan := continuous_arctan.measurable
end real
section
/-!
### Lemmas for derivatives of the composition of `real.arctan` with a differentiable function
In this section we register lemmas for the derivatives of the composition of `real.arctan` with a
differentiable function, for standalone use and use with `simp`. -/
open real
lemma measurable.arctan {α : Type*} [measurable_space α] {f : α → ℝ} (hf : measurable f) :
measurable (λ x, arctan (f x)) :=
measurable_arctan.comp hf
section deriv
variables {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ}
lemma has_strict_deriv_at.arctan (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) * f') x :=
(real.has_strict_deriv_at_arctan (f x)).comp x hf
lemma has_deriv_at.arctan (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) * f') x :=
(real.has_deriv_at_arctan (f x)).comp x hf
lemma has_deriv_within_at.arctan (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) * f') s x :=
(real.has_deriv_at_arctan (f x)).comp_has_deriv_within_at x hf
lemma deriv_within_arctan (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λ x, arctan (f x)) s x = (1 / (1 + (f x)^2)) * (deriv_within f s x) :=
hf.has_deriv_within_at.arctan.deriv_within hxs
@[simp] lemma deriv_arctan (hc : differentiable_at ℝ f x) :
deriv (λ x, arctan (f x)) x = (1 / (1 + (f x)^2)) * (deriv f x) :=
hc.has_deriv_at.arctan.deriv
end deriv
section fderiv
variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {f' : E →L[ℝ] ℝ} {x : E}
{s : set E} {n : with_top ℕ}
lemma has_strict_fderiv_at.arctan (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) • f') x :=
(has_strict_deriv_at_arctan (f x)).comp_has_strict_fderiv_at x hf
lemma has_fderiv_at.arctan (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) • f') x :=
(has_deriv_at_arctan (f x)).comp_has_fderiv_at x hf
lemma has_fderiv_within_at.arctan (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) • f') s x :=
(has_deriv_at_arctan (f x)).comp_has_fderiv_within_at x hf
lemma fderiv_within_arctan (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
fderiv_within ℝ (λ x, arctan (f x)) s x = (1 / (1 + (f x)^2)) • (fderiv_within ℝ f s x) :=
hf.has_fderiv_within_at.arctan.fderiv_within hxs
@[simp] lemma fderiv_arctan (hc : differentiable_at ℝ f x) :
fderiv ℝ (λ x, arctan (f x)) x = (1 / (1 + (f x)^2)) • (fderiv ℝ f x) :=
hc.has_fderiv_at.arctan.fderiv
lemma differentiable_within_at.arctan (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ x, real.arctan (f x)) s x :=
hf.has_fderiv_within_at.arctan.differentiable_within_at
@[simp] lemma differentiable_at.arctan (hc : differentiable_at ℝ f x) :
differentiable_at ℝ (λ x, arctan (f x)) x :=
hc.has_fderiv_at.arctan.differentiable_at
lemma differentiable_on.arctan (hc : differentiable_on ℝ f s) :
differentiable_on ℝ (λ x, arctan (f x)) s :=
λ x h, (hc x h).arctan
@[simp] lemma differentiable.arctan (hc : differentiable ℝ f) :
differentiable ℝ (λ x, arctan (f x)) :=
λ x, (hc x).arctan
lemma times_cont_diff_at.arctan (h : times_cont_diff_at ℝ n f x) :
times_cont_diff_at ℝ n (λ x, arctan (f x)) x :=
times_cont_diff_arctan.times_cont_diff_at.comp x h
lemma times_cont_diff.arctan (h : times_cont_diff ℝ n f) :
times_cont_diff ℝ n (λ x, arctan (f x)) :=
times_cont_diff_arctan.comp h
lemma times_cont_diff_within_at.arctan (h : times_cont_diff_within_at ℝ n f s x) :
times_cont_diff_within_at ℝ n (λ x, arctan (f x)) s x :=
times_cont_diff_arctan.comp_times_cont_diff_within_at h
lemma times_cont_diff_on.arctan (h : times_cont_diff_on ℝ n f s) :
times_cont_diff_on ℝ n (λ x, arctan (f x)) s :=
times_cont_diff_arctan.comp_times_cont_diff_on h
end fderiv
end
|
b811a4097813b8d03e9a919e374313f9ef1d518f | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/monoidal/unitors_auto.lean | 6bec3751855902eed4733b33e8d2f1d4dd306d48 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,522 | 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 Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.monoidal.category
import Mathlib.PostPort
universes v u
namespace Mathlib
/-!
# The two morphisms `λ_ (𝟙_ C)` and `ρ_ (𝟙_ C)` from `𝟙_ C ⊗ 𝟙_ C` to `𝟙_ C` are equal.
This is suprisingly difficult to prove directly from the usual axioms for a monoidal category!
This proof follows the diagram given at
https://people.math.osu.edu/penneys.2/QS2019/VicaryHandout.pdf
It should be a consequence of the coherence theorem for monoidal categories
(although quite possibly it is a necessary building block of any proof).
-/
namespace category_theory.monoidal_category
namespace unitors_equal
theorem cells_1_2 {C : Type u} [category C] [monoidal_category C] :
iso.hom ρ_ = iso.inv λ_ ≫ (𝟙 ⊗ iso.hom ρ_) ≫ iso.hom λ_ :=
sorry
theorem cells_4 {C : Type u} [category C] [monoidal_category C] :
iso.inv λ_ ≫ (𝟙 ⊗ iso.hom λ_) = iso.hom λ_ ≫ iso.inv λ_ :=
sorry
theorem cells_4' {C : Type u} [category C] [monoidal_category C] :
iso.inv λ_ = iso.hom λ_ ≫ iso.inv λ_ ≫ (𝟙 ⊗ iso.inv λ_) :=
sorry
theorem cells_3_4 {C : Type u} [category C] [monoidal_category C] : iso.inv λ_ = 𝟙 ⊗ iso.inv λ_ :=
sorry
theorem cells_1_4 {C : Type u} [category C] [monoidal_category C] :
iso.hom ρ_ = (𝟙 ⊗ iso.inv λ_) ≫ (𝟙 ⊗ iso.hom ρ_) ≫ iso.hom λ_ :=
sorry
theorem cells_6 {C : Type u} [category C] [monoidal_category C] :
(iso.inv ρ_ ⊗ 𝟙) ≫ iso.hom ρ_ = iso.hom ρ_ ≫ iso.inv ρ_ :=
sorry
theorem cells_6' {C : Type u} [category C] [monoidal_category C] :
iso.inv ρ_ ⊗ 𝟙 = iso.hom ρ_ ≫ iso.inv ρ_ ≫ iso.inv ρ_ :=
sorry
theorem cells_5_6 {C : Type u} [category C] [monoidal_category C] : iso.inv ρ_ ⊗ 𝟙 = iso.inv ρ_ :=
sorry
theorem cells_7 {C : Type u} [category C] [monoidal_category C] :
𝟙 ⊗ iso.inv λ_ = (iso.inv ρ_ ⊗ 𝟙) ≫ iso.hom α_ :=
sorry
theorem cells_1_7 {C : Type u} [category C] [monoidal_category C] :
iso.hom ρ_ = iso.inv ρ_ ≫ iso.hom α_ ≫ (𝟙 ⊗ iso.hom ρ_) ≫ iso.hom λ_ :=
sorry
theorem cells_8 {C : Type u} [category C] [monoidal_category C] :
iso.hom α_ = iso.inv ρ_ ≫ (iso.hom α_ ⊗ 𝟙) ≫ iso.hom ρ_ :=
sorry
theorem cells_14 {C : Type u} [category C] [monoidal_category C] :
iso.inv ρ_ ≫ iso.inv ρ_ = iso.inv ρ_ ≫ (iso.inv ρ_ ⊗ 𝟙) :=
sorry
theorem cells_9 {C : Type u} [category C] [monoidal_category C] :
iso.hom α_ ⊗ 𝟙 = iso.hom α_ ≫ iso.hom α_ ≫ (𝟙 ⊗ iso.inv α_) ≫ iso.inv α_ :=
sorry
theorem cells_10_13 {C : Type u} [category C] [monoidal_category C] :
(iso.inv ρ_ ⊗ 𝟙) ≫ iso.hom α_ ≫ iso.hom α_ ≫ (𝟙 ⊗ iso.inv α_) ≫ iso.inv α_ =
(𝟙 ⊗ iso.inv ρ_) ⊗ 𝟙 :=
sorry
theorem cells_9_13 {C : Type u} [category C] [monoidal_category C] :
(iso.inv ρ_ ⊗ 𝟙) ≫ (iso.hom α_ ⊗ 𝟙) = (𝟙 ⊗ iso.inv ρ_) ⊗ 𝟙 :=
sorry
theorem cells_15 {C : Type u} [category C] [monoidal_category C] :
iso.inv ρ_ ≫ ((𝟙 ⊗ iso.inv ρ_) ⊗ 𝟙) ≫ iso.hom ρ_ ≫ (𝟙 ⊗ iso.hom ρ_) = 𝟙 :=
sorry
end unitors_equal
theorem unitors_equal {C : Type u} [category C] [monoidal_category C] : iso.hom λ_ = iso.hom ρ_ :=
sorry
end Mathlib |
51b92d09cd53d01f95dcfeca7bd068ef54b95d7c | 437dc96105f48409c3981d46fb48e57c9ac3a3e4 | /src/analysis/normed_space/reflection.lean | 29cbb54da275efc3cdc98b2d691e0ff5f205ff5b | [
"Apache-2.0"
] | permissive | dan-c-k/mathlib | 08efec79bd7481ee6da9cc44c24a653bff4fbe0d | 96efc220f6225bc7a5ed8349900391a33a38cc56 | refs/heads/master | 1,658,082,847,093 | 1,589,013,201,000 | 1,589,013,201,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,147 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Yury Kudryashov
-/
import algebra.midpoint
import topology.metric_space.isometry
/-!
# Reflection in a point as an `isometric` homeomorphism
Given a `normed_group E` and `x : E`, we define `isometric.reflection x` to be
the reflection in `x` interpreted as an `isometric` homeomorphism.
Reflection is defined as an `equiv.perm` in `data.equiv.mul_add`. In this file
we restate results about `equiv.reflection` for an `isometric.reflection`, and
add a few results about `dist`.
## Tags
reflection, isometric
-/
variables (R : Type*) {E : Type*}
lemma equiv.reflection_fixed_iff_of_module [ring R] [invertible (2:R)]
[add_comm_group E] [module R E] {x y : E} :
(equiv.reflection x : E → E) y = y ↔ y = x :=
equiv.reflection_fixed_iff_of_bit0_inj $ λ x y h,
by rw [← one_smul R x, ← one_smul R y, ← inv_of_mul_self (2:R), mul_smul, mul_smul, two_smul,
two_smul, ← bit0, ← bit0, h]
namespace isometric
section normed_group
variables [normed_group E]
/-- Reflection in `x` as an `isometric` homeomorphism. -/
def reflection (x : E) : E ≃ᵢ E :=
(isometric.neg E).trans (isometric.add_left (x + x))
lemma reflection_apply (x y : E) : (reflection x : E → E) y = x + x - y := rfl
@[simp] lemma reflection_to_equiv (x : E) : (reflection x).to_equiv = equiv.reflection x := rfl
@[simp] lemma reflection_self (x : E) : (reflection x : E → E) x = x := add_sub_cancel _ _
lemma reflection_involutive (x : E) : function.involutive (reflection x : E → E) :=
equiv.reflection_involutive x
@[simp] lemma reflection_symm (x : E) : (reflection x).symm = reflection x :=
to_equiv_inj $ equiv.reflection_symm x
@[simp] lemma reflection_dist_fixed (x y : E) :
dist ((reflection x : E → E) y) x = dist y x :=
by rw [← (reflection x).dist_eq y x, reflection_self]
lemma reflection_dist_self' (x y : E) :
dist ((reflection x : E → E) y) y = dist (x + x) (y + y) :=
by { simp only [reflection_apply, dist_eq_norm], congr' 1, abel }
end normed_group
section module
variables [ring R] [invertible (2:R)] [normed_group E] [module R E]
@[simp] lemma reflection_midpoint_left (x y : E) : (reflection (midpoint R x y) : E → E) x = y :=
equiv.reflection_midpoint_left R x y
@[simp] lemma reflection_midpoint_right (x y : E) : (reflection (midpoint R x y) : E → E) y = x :=
equiv.reflection_midpoint_right R x y
variable (R)
include R
lemma reflection_fixed_iff {x y : E} : (reflection x : E → E) y = y ↔ y = x :=
equiv.reflection_fixed_iff_of_module R
end module
section normed_space
variables (R) [normed_field R] [normed_group E] [normed_space R E]
lemma reflection_dist_self (x y : E) :
dist ((reflection x : E → E) y) y = ∥(2:R)∥ * dist x y :=
by simp only [reflection_dist_self', ← two_smul R x, ← two_smul R y, dist_smul]
end normed_space
lemma reflection_dist_self_real [normed_group E] [normed_space ℝ E] (x y : E) :
dist ((reflection x : E → E) y) y = 2 * dist x y :=
by simp [reflection_dist_self ℝ x y, real.norm_two]
end isometric
|
78d53e086cc09271b92749f942215bca1812d67f | 8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3 | /src/topology/algebra/monoid.lean | 09e53dd7f9c2f51574a1fcef753d68da5f28c63e | [
"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 | 16,859 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import topology.continuous_on
import group_theory.submonoid.operations
import algebra.group.prod
import algebra.pointwise
import algebra.big_operators.finprod
/-!
# Theory of topological monoids
In this file we define mixin classes `has_continuous_mul` and `has_continuous_add`. While in many
applications the underlying type is a monoid (multiplicative or additive), we do not require this in
the definitions.
-/
universe variables u v
open classical set filter topological_space
open_locale classical topological_space big_operators pointwise
variables {ι α X M N : Type*} [topological_space X]
@[to_additive]
lemma continuous_one [topological_space M] [has_one M] : continuous (1 : X → M) :=
@continuous_const _ _ _ _ 1
/-- Basic hypothesis to talk about a topological additive monoid or a topological additive
semigroup. A topological additive monoid over `M`, for example, is obtained by requiring both the
instances `add_monoid M` and `has_continuous_add M`. -/
class has_continuous_add (M : Type u) [topological_space M] [has_add M] : Prop :=
(continuous_add : continuous (λ p : M × M, p.1 + p.2))
/-- Basic hypothesis to talk about a topological monoid or a topological semigroup.
A topological monoid over `M`, for example, is obtained by requiring both the instances `monoid M`
and `has_continuous_mul M`. -/
@[to_additive]
class has_continuous_mul (M : Type u) [topological_space M] [has_mul M] : Prop :=
(continuous_mul : continuous (λ p : M × M, p.1 * p.2))
section has_continuous_mul
variables [topological_space M] [has_mul M] [has_continuous_mul M]
@[to_additive]
lemma continuous_mul : continuous (λp:M×M, p.1 * p.2) :=
has_continuous_mul.continuous_mul
@[continuity, to_additive]
lemma continuous.mul {f g : X → M} (hf : continuous f) (hg : continuous g) :
continuous (λx, f x * g x) :=
continuous_mul.comp (hf.prod_mk hg : _)
-- should `to_additive` be doing this?
attribute [continuity] continuous.add
@[to_additive]
lemma continuous_mul_left (a : M) : continuous (λ b:M, a * b) :=
continuous_const.mul continuous_id
@[to_additive]
lemma continuous_mul_right (a : M) : continuous (λ b:M, b * a) :=
continuous_id.mul continuous_const
@[to_additive]
lemma continuous_on.mul {f g : X → M} {s : set X} (hf : continuous_on f s)
(hg : continuous_on g s) :
continuous_on (λx, f x * g x) s :=
(continuous_mul.comp_continuous_on (hf.prod hg) : _)
@[to_additive]
lemma tendsto_mul {a b : M} : tendsto (λp:M×M, p.fst * p.snd) (𝓝 (a, b)) (𝓝 (a * b)) :=
continuous_iff_continuous_at.mp has_continuous_mul.continuous_mul (a, b)
@[to_additive]
lemma filter.tendsto.mul {f g : α → M} {x : filter α} {a b : M}
(hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) :
tendsto (λx, f x * g x) x (𝓝 (a * b)) :=
tendsto_mul.comp (hf.prod_mk_nhds hg)
@[to_additive]
lemma filter.tendsto.const_mul (b : M) {c : M} {f : α → M} {l : filter α}
(h : tendsto (λ (k:α), f k) l (𝓝 c)) : tendsto (λ (k:α), b * f k) l (𝓝 (b * c)) :=
tendsto_const_nhds.mul h
@[to_additive]
lemma filter.tendsto.mul_const (b : M) {c : M} {f : α → M} {l : filter α}
(h : tendsto (λ (k:α), f k) l (𝓝 c)) : tendsto (λ (k:α), f k * b) l (𝓝 (c * b)) :=
h.mul tendsto_const_nhds
@[to_additive]
lemma continuous_at.mul {f g : X → M} {x : X} (hf : continuous_at f x) (hg : continuous_at g x) :
continuous_at (λx, f x * g x) x :=
hf.mul hg
@[to_additive]
lemma continuous_within_at.mul {f g : X → M} {s : set X} {x : X} (hf : continuous_within_at f s x)
(hg : continuous_within_at g s x) :
continuous_within_at (λx, f x * g x) s x :=
hf.mul hg
@[to_additive]
instance [topological_space N] [has_mul N] [has_continuous_mul N] : has_continuous_mul (M × N) :=
⟨((continuous_fst.comp continuous_fst).mul (continuous_fst.comp continuous_snd)).prod_mk
((continuous_snd.comp continuous_fst).mul (continuous_snd.comp continuous_snd))⟩
@[to_additive]
instance pi.has_continuous_mul {C : ι → Type*} [∀ i, topological_space (C i)]
[∀ i, has_mul (C i)] [∀ i, has_continuous_mul (C i)] : has_continuous_mul (Π i, C i) :=
{ continuous_mul := continuous_pi (λ i, continuous.mul
((continuous_apply i).comp continuous_fst) ((continuous_apply i).comp continuous_snd)) }
@[priority 100, to_additive]
instance has_continuous_mul_of_discrete_topology [topological_space N]
[has_mul N] [discrete_topology N] : has_continuous_mul N :=
⟨continuous_of_discrete_topology⟩
open_locale filter
open function
@[to_additive]
lemma has_continuous_mul.of_nhds_one {M : Type u} [monoid M] [topological_space M]
(hmul : tendsto (uncurry ((*) : M → M → M)) (𝓝 1 ×ᶠ 𝓝 1) $ 𝓝 1)
(hleft : ∀ x₀ : M, 𝓝 x₀ = map (λ x, x₀*x) (𝓝 1))
(hright : ∀ x₀ : M, 𝓝 x₀ = map (λ x, x*x₀) (𝓝 1)) : has_continuous_mul M :=
⟨begin
rw continuous_iff_continuous_at,
rintros ⟨x₀, y₀⟩,
have key : (λ p : M × M, x₀ * p.1 * (p.2 * y₀)) = ((λ x, x₀*x) ∘ (λ x, x*y₀)) ∘ (uncurry (*)),
{ ext p, simp [uncurry, mul_assoc] },
have key₂ : (λ x, x₀*x) ∘ (λ x, y₀*x) = λ x, (x₀ *y₀)*x,
{ ext x, simp },
calc map (uncurry (*)) (𝓝 (x₀, y₀))
= map (uncurry (*)) (𝓝 x₀ ×ᶠ 𝓝 y₀) : by rw nhds_prod_eq
... = map (λ (p : M × M), x₀ * p.1 * (p.2 * y₀)) ((𝓝 1) ×ᶠ (𝓝 1))
: by rw [uncurry, hleft x₀, hright y₀, prod_map_map_eq, filter.map_map]
... = map ((λ x, x₀ * x) ∘ λ x, x * y₀) (map (uncurry (*)) (𝓝 1 ×ᶠ 𝓝 1))
: by { rw [key, ← filter.map_map], }
... ≤ map ((λ (x : M), x₀ * x) ∘ λ x, x * y₀) (𝓝 1) : map_mono hmul
... = 𝓝 (x₀*y₀) : by rw [← filter.map_map, ← hright, hleft y₀, filter.map_map, key₂, ← hleft]
end⟩
@[to_additive]
lemma has_continuous_mul_of_comm_of_nhds_one (M : Type u) [comm_monoid M] [topological_space M]
(hmul : tendsto (uncurry ((*) : M → M → M)) (𝓝 1 ×ᶠ 𝓝 1) (𝓝 1))
(hleft : ∀ x₀ : M, 𝓝 x₀ = map (λ x, x₀*x) (𝓝 1)) : has_continuous_mul M :=
begin
apply has_continuous_mul.of_nhds_one hmul hleft,
intros x₀,
simp_rw [mul_comm, hleft x₀]
end
end has_continuous_mul
section has_continuous_mul
variables [topological_space M] [monoid M] [has_continuous_mul M]
@[to_additive]
lemma submonoid.top_closure_mul_self_subset (s : submonoid M) :
(closure (s : set M)) * closure (s : set M) ⊆ closure (s : set M) :=
calc
(closure (s : set M)) * closure (s : set M)
= (λ p : M × M, p.1 * p.2) '' (closure ((s : set M).prod s)) : by simp [closure_prod_eq]
... ⊆ closure ((λ p : M × M, p.1 * p.2) '' ((s : set M).prod s)) :
image_closure_subset_closure_image continuous_mul
... = closure s : by simp [s.coe_mul_self_eq]
@[to_additive]
lemma submonoid.top_closure_mul_self_eq (s : submonoid M) :
(closure (s : set M)) * closure (s : set M) = closure (s : set M) :=
subset.antisymm
s.top_closure_mul_self_subset
(λ x hx, ⟨x, 1, hx, subset_closure s.one_mem, mul_one _⟩)
/-- The (topological-space) closure of a submonoid of a space `M` with `has_continuous_mul` is
itself a submonoid. -/
@[to_additive "The (topological-space) closure of an additive submonoid of a space `M` with
`has_continuous_add` is itself an additive submonoid."]
def submonoid.topological_closure (s : submonoid M) : submonoid M :=
{ carrier := closure (s : set M),
one_mem' := subset_closure s.one_mem,
mul_mem' := λ a b ha hb, s.top_closure_mul_self_subset ⟨a, b, ha, hb, rfl⟩ }
@[to_additive]
instance submonoid.topological_closure_has_continuous_mul (s : submonoid M) :
has_continuous_mul (s.topological_closure) :=
{ continuous_mul :=
begin
apply continuous_induced_rng,
change continuous (λ p : s.topological_closure × s.topological_closure, (p.1 : M) * (p.2 : M)),
continuity,
end }
lemma submonoid.submonoid_topological_closure (s : submonoid M) :
s ≤ s.topological_closure :=
subset_closure
lemma submonoid.is_closed_topological_closure (s : submonoid M) :
is_closed (s.topological_closure : set M) :=
by convert is_closed_closure
lemma submonoid.topological_closure_minimal
(s : submonoid M) {t : submonoid M} (h : s ≤ t) (ht : is_closed (t : set M)) :
s.topological_closure ≤ t :=
closure_minimal h ht
@[to_additive exists_open_nhds_zero_half]
lemma exists_open_nhds_one_split {s : set M} (hs : s ∈ 𝓝 (1 : M)) :
∃ V : set M, is_open V ∧ (1 : M) ∈ V ∧ ∀ (v ∈ V) (w ∈ V), v * w ∈ s :=
have ((λa:M×M, a.1 * a.2) ⁻¹' s) ∈ 𝓝 ((1, 1) : M × M),
from tendsto_mul (by simpa only [one_mul] using hs),
by simpa only [prod_subset_iff] using exists_nhds_square this
@[to_additive exists_nhds_zero_half]
lemma exists_nhds_one_split {s : set M} (hs : s ∈ 𝓝 (1 : M)) :
∃ V ∈ 𝓝 (1 : M), ∀ (v ∈ V) (w ∈ V), v * w ∈ s :=
let ⟨V, Vo, V1, hV⟩ := exists_open_nhds_one_split hs
in ⟨V, is_open.mem_nhds Vo V1, hV⟩
@[to_additive exists_nhds_zero_quarter]
lemma exists_nhds_one_split4 {u : set M} (hu : u ∈ 𝓝 (1 : M)) :
∃ V ∈ 𝓝 (1 : M),
∀ {v w s t}, v ∈ V → w ∈ V → s ∈ V → t ∈ V → v * w * s * t ∈ u :=
begin
rcases exists_nhds_one_split hu with ⟨W, W1, h⟩,
rcases exists_nhds_one_split W1 with ⟨V, V1, h'⟩,
use [V, V1],
intros v w s t v_in w_in s_in t_in,
simpa only [mul_assoc] using h _ (h' v v_in w w_in) _ (h' s s_in t t_in)
end
/-- Given a neighborhood `U` of `1` there is an open neighborhood `V` of `1`
such that `VV ⊆ U`. -/
@[to_additive "Given a open neighborhood `U` of `0` there is a open neighborhood `V` of `0`
such that `V + V ⊆ U`."]
lemma exists_open_nhds_one_mul_subset {U : set M} (hU : U ∈ 𝓝 (1 : M)) :
∃ V : set M, is_open V ∧ (1 : M) ∈ V ∧ V * V ⊆ U :=
begin
rcases exists_open_nhds_one_split hU with ⟨V, Vo, V1, hV⟩,
use [V, Vo, V1],
rintros _ ⟨x, y, hx, hy, rfl⟩,
exact hV _ hx _ hy
end
@[to_additive]
lemma tendsto_list_prod {f : ι → α → M} {x : filter α} {a : ι → M} :
∀ l:list ι, (∀i∈l, tendsto (f i) x (𝓝 (a i))) →
tendsto (λb, (l.map (λc, f c b)).prod) x (𝓝 ((l.map a).prod))
| [] _ := by simp [tendsto_const_nhds]
| (f :: l) h :=
begin
simp only [list.map_cons, list.prod_cons],
exact (h f (list.mem_cons_self _ _)).mul
(tendsto_list_prod l (assume c hc, h c (list.mem_cons_of_mem _ hc)))
end
@[to_additive]
lemma continuous_list_prod {f : ι → X → M} (l : list ι)
(h : ∀i∈l, continuous (f i)) :
continuous (λa, (l.map (λi, f i a)).prod) :=
continuous_iff_continuous_at.2 $ assume x, tendsto_list_prod l $ assume c hc,
continuous_iff_continuous_at.1 (h c hc) x
-- @[to_additive continuous_smul]
@[continuity]
lemma continuous_pow : ∀ n : ℕ, continuous (λ a : M, a ^ n)
| 0 := by simpa using continuous_const
| (k+1) := by { simp only [pow_succ], exact continuous_id.mul (continuous_pow _) }
@[continuity]
lemma continuous.pow {f : X → M} (h : continuous f) (n : ℕ) :
continuous (λ b, (f b) ^ n) :=
(continuous_pow n).comp h
lemma continuous_on_pow {s : set M} (n : ℕ) : continuous_on (λ x, x ^ n) s :=
(continuous_pow n).continuous_on
lemma continuous_at_pow (x : M) (n : ℕ) : continuous_at (λ x, x ^ n) x :=
(continuous_pow n).continuous_at
lemma filter.tendsto.pow {l : filter α} {f : α → M} {x : M} (hf : tendsto f l (𝓝 x)) (n : ℕ) :
tendsto (λ x, f x ^ n) l (𝓝 (x ^ n)) :=
(continuous_at_pow _ _).tendsto.comp hf
lemma continuous_within_at.pow {f : X → M} {x : X} {s : set X} (hf : continuous_within_at f s x)
(n : ℕ) : continuous_within_at (λ x, f x ^ n) s x :=
hf.pow n
lemma continuous_at.pow {f : X → M} {x : X} (hf : continuous_at f x) (n : ℕ) :
continuous_at (λ x, f x ^ n) x :=
hf.pow n
lemma continuous_on.pow {f : X → M} {s : set X} (hf : continuous_on f s) (n : ℕ) :
continuous_on (λ x, f x ^ n) s :=
λ x hx, (hf x hx).pow n
end has_continuous_mul
section op
open opposite
/-- Put the same topological space structure on the opposite monoid as on the original space. -/
instance [_i : topological_space α] : topological_space αᵒᵖ :=
topological_space.induced (unop : αᵒᵖ → α) _i
variables [topological_space α]
lemma continuous_unop : continuous (unop : αᵒᵖ → α) := continuous_induced_dom
lemma continuous_op : continuous (op : α → αᵒᵖ) := continuous_induced_rng continuous_id
variables [monoid α] [has_continuous_mul α]
/-- If multiplication is continuous in the monoid `α`, then it also is in the monoid `αᵒᵖ`. -/
instance : has_continuous_mul αᵒᵖ :=
⟨ let h₁ := @continuous_mul α _ _ _ in
let h₂ : continuous (λ p : α × α, _) := continuous_snd.prod_mk continuous_fst in
continuous_induced_rng $ (h₁.comp h₂).comp (continuous_unop.prod_map continuous_unop) ⟩
end op
namespace units
open opposite
variables [topological_space α] [monoid α]
/-- The units of a monoid are equipped with a topology, via the embedding into `α × α`. -/
instance : topological_space (units α) :=
topological_space.induced (embed_product α) (by apply_instance)
lemma continuous_embed_product : continuous (embed_product α) :=
continuous_induced_dom
lemma continuous_coe : continuous (coe : units α → α) :=
by convert continuous_fst.comp continuous_induced_dom
variables [has_continuous_mul α]
/-- If multiplication on a monoid is continuous, then multiplication on the units of the monoid,
with respect to the induced topology, is continuous.
Inversion is also continuous, but we register this in a later file, `topology.algebra.group`,
because the predicate `has_continuous_inv` has not yet been defined. -/
instance : has_continuous_mul (units α) :=
⟨ let h := @continuous_mul (α × αᵒᵖ) _ _ _ in
continuous_induced_rng $ h.comp $ continuous_embed_product.prod_map continuous_embed_product ⟩
end units
section
variables [topological_space M] [comm_monoid M]
@[to_additive]
lemma submonoid.mem_nhds_one (S : submonoid M) (oS : is_open (S : set M)) :
(S : set M) ∈ 𝓝 (1 : M) :=
is_open.mem_nhds oS S.one_mem
variable [has_continuous_mul M]
@[to_additive]
lemma tendsto_multiset_prod {f : ι → α → M} {x : filter α} {a : ι → M} (s : multiset ι) :
(∀ i ∈ s, tendsto (f i) x (𝓝 (a i))) →
tendsto (λb, (s.map (λc, f c b)).prod) x (𝓝 ((s.map a).prod)) :=
by { rcases s with ⟨l⟩, simpa using tendsto_list_prod l }
@[to_additive]
lemma tendsto_finset_prod {f : ι → α → M} {x : filter α} {a : ι → M} (s : finset ι) :
(∀ i ∈ s, tendsto (f i) x (𝓝 (a i))) → tendsto (λb, ∏ c in s, f c b) x (𝓝 (∏ c in s, a c)) :=
tendsto_multiset_prod _
@[to_additive, continuity]
lemma continuous_multiset_prod {f : ι → X → M} (s : multiset ι) :
(∀i ∈ s, continuous (f i)) → continuous (λ a, (s.map (λ i, f i a)).prod) :=
by { rcases s with ⟨l⟩, simpa using continuous_list_prod l }
attribute [continuity] continuous_multiset_sum
@[continuity, to_additive]
lemma continuous_finset_prod {f : ι → X → M} (s : finset ι) :
(∀ i ∈ s, continuous (f i)) → continuous (λa, ∏ i in s, f i a) :=
continuous_multiset_prod _
-- should `to_additive` be doing this?
attribute [continuity] continuous_finset_sum
open function
@[to_additive] lemma continuous_finprod {f : ι → X → M} (hc : ∀ i, continuous (f i))
(hf : locally_finite (λ i, mul_support (f i))) :
continuous (λ x, ∏ᶠ i, f i x) :=
begin
refine continuous_iff_continuous_at.2 (λ x, _),
rcases hf x with ⟨U, hxU, hUf⟩,
have : continuous_at (λ x, ∏ i in hUf.to_finset, f i x) x,
from tendsto_finset_prod _ (λ i hi, (hc i).continuous_at),
refine this.congr (mem_of_superset hxU $ λ y hy, _),
refine (finprod_eq_prod_of_mul_support_subset _ (λ i hi, _)).symm,
rw [hUf.coe_to_finset],
exact ⟨y, hi, hy⟩
end
@[to_additive] lemma continuous_finprod_cond {f : ι → X → M} {p : ι → Prop}
(hc : ∀ i, p i → continuous (f i)) (hf : locally_finite (λ i, mul_support (f i))) :
continuous (λ x, ∏ᶠ i (hi : p i), f i x) :=
begin
simp only [← finprod_subtype_eq_finprod_cond],
exact continuous_finprod (λ i, hc i i.2) (hf.comp_injective subtype.coe_injective)
end
end
instance additive.has_continuous_add {M} [h : topological_space M] [has_mul M]
[has_continuous_mul M] : @has_continuous_add (additive M) h _ :=
{ continuous_add := @continuous_mul M _ _ _ }
instance multiplicative.has_continuous_mul {M} [h : topological_space M] [has_add M]
[has_continuous_add M] : @has_continuous_mul (multiplicative M) h _ :=
{ continuous_mul := @continuous_add M _ _ _ }
|
65537349207681bcb63e7f297c0e325ae8ded632 | 3dc4623269159d02a444fe898d33e8c7e7e9461b | /.github/workflows/project_1_a_decrire/lean-scheme-submission/src/to_mathlib/ring_hom.lean | 238a58af1a84c3922ce06dee330a6740c916feb4 | [] | no_license | Or7ando/lean | cc003e6c41048eae7c34aa6bada51c9e9add9e66 | d41169cf4e416a0d42092fb6bdc14131cee9dd15 | refs/heads/master | 1,650,600,589,722 | 1,587,262,906,000 | 1,587,262,906,000 | 255,387,160 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,337 | lean | /-
A ring homomorphism is injective iff the kernel is trivial.
-/
import algebra.ring
import ring_theory.ideal_operations
import linear_algebra.basic
import to_mathlib.ideals
universes u v
variables {α : Type u} {β : Type v} [comm_ring α] [comm_ring β] (f : α → β) [is_ring_hom f]
lemma is_ring_hom.trivial_ker_def
: ker f = ⊥ ↔ (∀ {x}, f x = 0 → x = 0) :=
begin
split,
{ intros Hker x Hx,
replace Hx : x ∈ ker f := Hx,
rw Hker at Hx,
rw ←set.mem_singleton_iff,
exact Hx, },
{ intros Hfx,
apply ideal.ext,
intros x,
split,
{ intros Hx,
replace Hx : f x = 0 := Hx,
erw set.mem_singleton_iff,
exact (Hfx Hx), },
{ intros Hx,
erw set.mem_singleton_iff at Hx,
rw Hx,
exact is_ring_hom.map_zero f, } },
end
lemma is_ring_hom.inj_iff_trivial_ker
: (∀ {x}, f x = 0 → x = 0) ↔ function.injective f :=
begin
split,
{ intros H x y Hxy,
rw [←sub_eq_zero_iff_eq, ←is_ring_hom.map_sub f] at Hxy,
exact sub_eq_zero_iff_eq.1 (H Hxy), },
{ intros Hinj,
intros x Hx,
have Hfx : f x = f 0 := (is_ring_hom.map_zero f).symm ▸ Hx,
exact Hinj Hfx, }
end
lemma is_ring_hom.inj_iff_trivial_ker'
: ker f = ⊥ ↔ function.injective f :=
iff.trans (is_ring_hom.trivial_ker_def f) (is_ring_hom.inj_iff_trivial_ker f)
|
9731cf2543e6340ae6c130cc4beb887e205fc435 | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/linear_algebra/matrix/nonsingular_inverse.lean | aac6e67d1edb2cdf1277490e5be7b5def5f2a5b6 | [
"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 | 30,308 | lean | /-
Copyright (c) 2019 Tim Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Tim Baanen, Lu-Ming Zhang
-/
import algebra.associated
import algebra.regular.smul
import linear_algebra.matrix.polynomial
import tactic.linarith
import tactic.ring_exp
/-!
# Nonsingular inverses
In this file, we define an inverse for square matrices of invertible
determinant. For matrices that are not square or not of full rank, there is a
more general notion of pseudoinverses which we do not consider here.
The definition of inverse used in this file is the adjugate divided by the determinant.
The adjugate is calculated with Cramer's rule, which we introduce first.
The vectors returned by Cramer's rule are given by the linear map `cramer`,
which sends a matrix `A` and vector `b` to the vector consisting of the
determinant of replacing the `i`th column of `A` with `b` at index `i`
(written as `(A.update_column i b).det`).
Using Cramer's rule, we can compute for each matrix `A` the matrix `adjugate A`.
The entries of the adjugate are the determinants of each minor of `A`.
Instead of defining a minor to be `A` with row `i` and column `j` deleted, we
replace the `i`th row of `A` with the `j`th basis vector; this has the same
determinant as the minor but more importantly equals Cramer's rule applied
to `A` and the `j`th basis vector, simplifying the subsequent proofs.
We prove the adjugate behaves like `det A • A⁻¹`. Finally, we show that dividing
the adjugate by `det A` (if possible), giving a matrix `nonsing_inv A`, will
result in a multiplicative inverse to `A`.
## References
* https://en.wikipedia.org/wiki/Cramer's_rule#Finding_inverse_matrix
## Tags
matrix inverse, cramer, cramer's rule, adjugate
-/
namespace matrix
universes u v
variables {n : Type u} [decidable_eq n] [fintype n] {α : Type v} [comm_ring α]
open_locale matrix big_operators
open equiv equiv.perm finset
section cramer
/-!
### `cramer` section
Introduce the linear map `cramer` with values defined by `cramer_map`.
After defining `cramer_map` and showing it is linear,
we will restrict our proofs to using `cramer`.
-/
variables (A : matrix n n α) (b : n → α)
/--
`cramer_map A b i` is the determinant of the matrix `A` with column `i` replaced with `b`,
and thus `cramer_map A b` is the vector output by Cramer's rule on `A` and `b`.
If `A ⬝ x = b` has a unique solution in `x`, `cramer_map A` sends the vector `b` to `A.det • x`.
Otherwise, the outcome of `cramer_map` is well-defined but not necessarily useful.
-/
def cramer_map (i : n) : α := (A.update_column i b).det
lemma cramer_map_is_linear (i : n) : is_linear_map α (λ b, cramer_map A b i) :=
{ map_add := det_update_column_add _ _,
map_smul := det_update_column_smul _ _ }
lemma cramer_is_linear : is_linear_map α (cramer_map A) :=
begin
split; intros; ext i,
{ apply (cramer_map_is_linear A i).1 },
{ apply (cramer_map_is_linear A i).2 }
end
/--
`cramer A b i` is the determinant of the matrix `A` with column `i` replaced with `b`,
and thus `cramer A b` is the vector output by Cramer's rule on `A` and `b`.
If `A ⬝ x = b` has a unique solution in `x`, `cramer A` sends the vector `b` to `A.det • x`.
Otherwise, the outcome of `cramer` is well-defined but not necessarily useful.
-/
def cramer (A : matrix n n α) : (n → α) →ₗ[α] (n → α) :=
is_linear_map.mk' (cramer_map A) (cramer_is_linear A)
lemma cramer_apply (i : n) : cramer A b i = (A.update_column i b).det := rfl
lemma cramer_transpose_row_self (i : n) :
Aᵀ.cramer (A i) = pi.single i A.det :=
begin
ext j,
rw [cramer_apply, pi.single_apply],
split_ifs with h,
{ -- i = j: this entry should be `A.det`
subst h,
simp only [update_column_transpose, det_transpose, update_row, function.update_eq_self] },
{ -- i ≠ j: this entry should be 0
rw [update_column_transpose, det_transpose],
apply det_zero_of_row_eq h,
rw [update_row_self, update_row_ne (ne.symm h)] }
end
lemma cramer_row_self (i : n) (h : ∀ j, b j = A j i) :
A.cramer b = pi.single i A.det :=
begin
rw [← transpose_transpose A, det_transpose],
convert cramer_transpose_row_self Aᵀ i,
exact funext h
end
@[simp] lemma cramer_one : cramer (1 : matrix n n α) = 1 :=
begin
ext i j,
convert congr_fun (cramer_row_self (1 : matrix n n α) (pi.single i 1) i _) j,
{ simp },
{ intros j, rw [matrix.one_eq_pi_single, pi.single_comm] }
end
@[simp] lemma cramer_subsingleton_apply [subsingleton n] (A : matrix n n α) (b : n → α) (i : n) :
cramer A b i = b i :=
by rw [cramer_apply, det_eq_elem_of_subsingleton _ i, update_column_self]
lemma cramer_zero [nontrivial n] : cramer (0 : matrix n n α) = 0 :=
begin
ext i j,
obtain ⟨j', hj'⟩ : ∃ j', j' ≠ j := exists_ne j,
apply det_eq_zero_of_column_eq_zero j',
intro j'',
simp [update_column_ne hj'],
end
/-- Use linearity of `cramer` to take it out of a summation. -/
lemma sum_cramer {β} (s : finset β) (f : β → n → α) :
∑ x in s, cramer A (f x) = cramer A (∑ x in s, f x) :=
(linear_map.map_sum (cramer A)).symm
/-- Use linearity of `cramer` and vector evaluation to take `cramer A _ i` out of a summation. -/
lemma sum_cramer_apply {β} (s : finset β) (f : n → β → α) (i : n) :
∑ x in s, cramer A (λ j, f j x) i = cramer A (λ (j : n), ∑ x in s, f j x) i :=
calc ∑ x in s, cramer A (λ j, f j x) i
= (∑ x in s, cramer A (λ j, f j x)) i : (finset.sum_apply i s _).symm
... = cramer A (λ (j : n), ∑ x in s, f j x) i :
by { rw [sum_cramer, cramer_apply], congr' with j, apply finset.sum_apply }
end cramer
section adjugate
/-!
### `adjugate` section
Define the `adjugate` matrix and a few equations.
These will hold for any matrix over a commutative ring,
while the `inv` section is specifically for invertible matrices.
-/
/-- The adjugate matrix is the transpose of the cofactor matrix.
Typically, the cofactor matrix is defined by taking the determinant of minors,
i.e. the matrix with a row and column removed.
However, the proof of `mul_adjugate` becomes a lot easier if we define the
minor as replacing a column with a basis vector, since it allows us to use
facts about the `cramer` map.
-/
def adjugate (A : matrix n n α) : matrix n n α := λ i, cramer Aᵀ (λ j, if i = j then 1 else 0)
lemma adjugate_def (A : matrix n n α) :
adjugate A = λ i, cramer Aᵀ (λ j, if i = j then 1 else 0) := rfl
lemma adjugate_apply (A : matrix n n α) (i j : n) :
adjugate A i j = (A.update_row j (λ j, if i = j then 1 else 0)).det :=
by { rw adjugate_def, simp only, rw [cramer_apply, update_column_transpose, det_transpose], }
lemma adjugate_transpose (A : matrix n n α) : (adjugate A)ᵀ = adjugate (Aᵀ) :=
begin
ext i j,
rw [transpose_apply, adjugate_apply, adjugate_apply, update_row_transpose, det_transpose],
rw [det_apply', det_apply'],
apply finset.sum_congr rfl,
intros σ _,
congr' 1,
by_cases i = σ j,
{ -- Everything except `(i , j)` (= `(σ j , j)`) is given by A, and the rest is a single `1`.
congr; ext j',
have := (@equiv.injective _ _ σ j j' : σ j = σ j' → j = j'),
rw [update_row_apply, update_column_apply],
finish },
{ -- Otherwise, we need to show that there is a `0` somewhere in the product.
have : (∏ j' : n, update_column A j (λ (i' : n), ite (i = i') 1 0) (σ j') j') = 0,
{ apply prod_eq_zero (mem_univ j),
rw [update_column_self],
exact if_neg h },
rw this,
apply prod_eq_zero (mem_univ (σ⁻¹ i)),
erw [apply_symm_apply σ i, update_row_self],
apply if_neg,
intro h',
exact h ((symm_apply_eq σ).mp h'.symm) }
end
/-- Since the map `b ↦ cramer A b` is linear in `b`, it must be multiplication by some matrix. This
matrix is `A.adjugate`. -/
lemma cramer_eq_adjugate_mul_vec (A : matrix n n α) (b : n → α) :
cramer A b = A.adjugate.mul_vec b :=
begin
nth_rewrite 1 ← A.transpose_transpose,
rw [← adjugate_transpose, adjugate_def],
have : b = ∑ i, (b i) • (λ j, if i = j then 1 else 0), { ext i, simp, },
rw this, ext k,
simp [mul_vec, dot_product, mul_comm],
end
lemma mul_adjugate_apply (A : matrix n n α) (i j k) :
A i k * adjugate A k j = cramer Aᵀ (λ j, if k = j then A i k else 0) j :=
begin
erw [←smul_eq_mul, ←pi.smul_apply, ←linear_map.map_smul],
congr' with l,
rw [pi.smul_apply, smul_eq_mul, mul_boole],
end
lemma mul_adjugate (A : matrix n n α) : A ⬝ adjugate A = A.det • 1 :=
begin
ext i j,
rw [mul_apply, pi.smul_apply, pi.smul_apply, one_apply, smul_eq_mul, mul_boole],
simp [mul_adjugate_apply, sum_cramer_apply, cramer_transpose_row_self, pi.single_apply, eq_comm]
end
lemma adjugate_mul (A : matrix n n α) : adjugate A ⬝ A = A.det • 1 :=
calc adjugate A ⬝ A = (Aᵀ ⬝ (adjugate Aᵀ))ᵀ :
by rw [←adjugate_transpose, ←transpose_mul, transpose_transpose]
... = A.det • 1 : by rw [mul_adjugate (Aᵀ), det_transpose, transpose_smul, transpose_one]
/-- `det_adjugate_of_cancel` is an auxiliary lemma for computing `(adjugate A).det`,
used in `det_adjugate_eq_one` and `det_adjugate_of_is_unit`.
The formula for the determinant of the adjugate of an `n` by `n` matrix `A`
is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases.
This lemma `det_adjugate_of_cancel` covers the case that `det A` cancels
on the left of the equation `A.det * b = A.det ^ n`.
-/
lemma det_adjugate_of_cancel {A : matrix n n α}
(h : ∀ b, A.det * b = A.det ^ fintype.card n → b = A.det ^ (fintype.card n - 1)) :
(adjugate A).det = A.det ^ (fintype.card n - 1) :=
h (adjugate A).det (calc A.det * (adjugate A).det = (A ⬝ adjugate A).det : (det_mul _ _).symm
... = A.det ^ fintype.card n : by simp [mul_adjugate])
lemma adjugate_subsingleton [subsingleton n] (A : matrix n n α) : adjugate A = 1 :=
begin
ext i j,
simp [subsingleton.elim i j, adjugate_apply, det_eq_elem_of_subsingleton _ i]
end
lemma adjugate_eq_one_of_card_eq_one {A : matrix n n α} (h : fintype.card n = 1) : adjugate A = 1 :=
begin
haveI : subsingleton n := fintype.card_le_one_iff_subsingleton.mp h.le,
exact adjugate_subsingleton _
end
@[simp] lemma adjugate_zero (h : 1 < fintype.card n) : adjugate (0 : matrix n n α) = 0 :=
begin
ext i j,
obtain ⟨j', hj'⟩ : ∃ j', j' ≠ j := fintype.exists_ne_of_one_lt_card h j,
apply det_eq_zero_of_column_eq_zero j',
intro j'',
simp [update_column_ne hj'],
end
@[simp] lemma adjugate_one : adjugate (1 : matrix n n α) = 1 :=
by { ext, simp [adjugate_def, matrix.one_apply] }
lemma det_adjugate_eq_one {A : matrix n n α} (h : A.det = 1) : (adjugate A).det = 1 :=
calc (adjugate A).det
= A.det ^ (fintype.card n - 1) : det_adjugate_of_cancel (λ b hb, by simpa [h] using hb)
... = 1 : by rw [h, one_pow]
/-- `det_adjugate_of_is_unit` gives the formula for `(adjugate A).det` if `A.det` has an inverse.
The formula for the determinant of the adjugate of an `n` by `n` matrix `A`
is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases.
This lemma `det_adjugate_of_is_unit` covers the case that `det A` has an inverse.
-/
lemma det_adjugate_of_is_unit {A : matrix n n α} (h : is_unit A.det) :
(adjugate A).det = A.det ^ (fintype.card n - 1) :=
begin
rcases is_unit_iff_exists_inv'.mp h with ⟨a, ha⟩,
by_cases card_lt_zero : fintype.card n ≤ 0,
{ have h : fintype.card n = 0 := by linarith,
simp [det_eq_one_of_card_eq_zero h] },
have zero_lt_card : 0 < fintype.card n := by linarith,
have n_nonempty : nonempty n := fintype.card_pos_iff.mp zero_lt_card,
by_cases card_lt_one : fintype.card n ≤ 1,
{ have h : fintype.card n = 1 := by linarith,
simp [h, adjugate_eq_one_of_card_eq_one h] },
have one_lt_card : 1 < fintype.card n := by linarith,
have zero_lt_card_sub_one : 0 < fintype.card n - 1 :=
(nat.sub_lt_sub_right_iff (refl 1)).mpr one_lt_card,
apply det_adjugate_of_cancel,
intros b hb,
calc b = a * (det A ^ (fintype.card n - 1 + 1)) :
by rw [←one_mul b, ←ha, mul_assoc, hb, nat.sub_add_cancel zero_lt_card]
... = a * det A * det A ^ (fintype.card n - 1) : by ring_exp
... = det A ^ (fintype.card n - 1) : by rw [ha, one_mul]
end
end adjugate
section inv
/-!
### `inv` section
Defines the matrix `nonsing_inv A` and proves it is the inverse matrix
of a square matrix `A` as long as `det A` has a multiplicative inverse.
-/
variables (A : matrix n n α) (B : matrix n n α)
open_locale classical
lemma is_unit_det_transpose (h : is_unit A.det) : is_unit Aᵀ.det :=
by { rw det_transpose, exact h, }
/-- The inverse of a square matrix, when it is invertible (and zero otherwise).-/
noncomputable def nonsing_inv : matrix n n α :=
if h : is_unit A.det then h.unit⁻¹ • A.adjugate else 0
noncomputable instance : has_inv (matrix n n α) := ⟨matrix.nonsing_inv⟩
lemma inv_def (A : matrix n n α) : A⁻¹ = A.nonsing_inv := rfl
lemma nonsing_inv_apply_not_is_unit (h : ¬ is_unit A.det) :
A⁻¹ = 0 :=
by rw [inv_def, nonsing_inv, dif_neg h]
lemma nonsing_inv_apply (h : is_unit A.det) :
A⁻¹ = h.unit⁻¹ • A.adjugate :=
by rw [inv_def, nonsing_inv, dif_pos h]
lemma transpose_nonsing_inv (h : is_unit A.det) :
(A⁻¹)ᵀ = (Aᵀ)⁻¹ :=
begin
have h' := A.is_unit_det_transpose h,
have dets_eq : h.unit = h'.unit := units.ext (by rw [h.unit_spec, h'.unit_spec, det_transpose]),
rw [A.nonsing_inv_apply h, Aᵀ.nonsing_inv_apply h', dets_eq, A.adjugate_transpose.symm],
refl,
end
/-- The `nonsing_inv` of `A` is a right inverse. -/
@[simp] lemma mul_nonsing_inv (h : is_unit A.det) : A ⬝ A⁻¹ = 1 :=
by rw [A.nonsing_inv_apply h, units.smul_def, mul_smul, mul_adjugate, smul_smul,
units.inv_mul_of_eq h.unit_spec, one_smul]
/-- The `nonsing_inv` of `A` is a left inverse. -/
@[simp] lemma nonsing_inv_mul (h : is_unit A.det) : A⁻¹ ⬝ A = 1 :=
calc A⁻¹ ⬝ A = (Aᵀ ⬝ (Aᵀ)⁻¹)ᵀ : by { rw [transpose_mul,
Aᵀ.transpose_nonsing_inv (A.is_unit_det_transpose h),
transpose_transpose], }
... = 1ᵀ : by { rw Aᵀ.mul_nonsing_inv, exact A.is_unit_det_transpose h, }
... = 1 : transpose_one
@[simp] lemma nonsing_inv_det (h : is_unit A.det) : A⁻¹.det * A.det = 1 :=
by rw [←det_mul, A.nonsing_inv_mul h, det_one]
lemma is_unit_nonsing_inv_det (h : is_unit A.det) : is_unit A⁻¹.det :=
is_unit_of_mul_eq_one _ _ (A.nonsing_inv_det h)
@[simp] lemma nonsing_inv_nonsing_inv (h : is_unit A.det) : (A⁻¹)⁻¹ = A :=
calc (A⁻¹)⁻¹ = 1 ⬝ (A⁻¹)⁻¹ : by rw matrix.one_mul
... = A ⬝ A⁻¹ ⬝ (A⁻¹)⁻¹ : by rw A.mul_nonsing_inv h
... = A : by { rw [matrix.mul_assoc,
(A⁻¹).mul_nonsing_inv (A.is_unit_nonsing_inv_det h),
matrix.mul_one], }
@[simp] lemma is_unit_nonsing_inv_det_iff {A : matrix n n α} :
is_unit A⁻¹.det ↔ is_unit A.det :=
begin
refine ⟨λ h, _, is_unit_nonsing_inv_det _⟩,
nontriviality α,
casesI is_empty_or_nonempty n,
{ simp },
contrapose! h,
rw [nonsing_inv_apply_not_is_unit _ h, det_zero],
{ simp },
{ apply_instance }
end
/-- If `A.det` has a constructive inverse, produce one for `A`. -/
def invertible_of_det_invertible [invertible A.det] : invertible A :=
{ inv_of := ⅟A.det • A.adjugate,
mul_inv_of_self :=
by rw [mul_smul_comm, matrix.mul_eq_mul, mul_adjugate, smul_smul, inv_of_mul_self, one_smul],
inv_of_mul_self :=
by rw [smul_mul_assoc, matrix.mul_eq_mul, adjugate_mul, smul_smul, inv_of_mul_self, one_smul] }
/-- `A.det` is invertible if `A` has a left inverse. -/
def det_invertible_of_left_inverse (h : B ⬝ A = 1) : invertible A.det :=
{ inv_of := B.det,
mul_inv_of_self := by rw [mul_comm, ← det_mul, h, det_one],
inv_of_mul_self := by rw [← det_mul, h, det_one] }
/-- `A.det` is invertible if `A` has a right inverse. -/
def det_invertible_of_right_inverse (h : A ⬝ B = 1) : invertible A.det :=
{ inv_of := B.det,
mul_inv_of_self := by rw [← det_mul, h, det_one],
inv_of_mul_self := by rw [mul_comm, ← det_mul, h, det_one] }
/-- If `A` has a constructive inverse, produce one for `A.det`. -/
def det_invertible_of_invertible [invertible A] : invertible A.det :=
det_invertible_of_left_inverse A (⅟A) (inv_of_mul_self _)
/-- Given a proof that `A.det` has a constructive inverse, lift `A` to `units (matrix n n α)`-/
def unit_of_det_invertible [invertible A.det] : units (matrix n n α) :=
@unit_of_invertible _ _ A (invertible_of_det_invertible A)
/-- A matrix whose determinant is a unit is itself a unit. This is a noncomputable version of
`matrix.units_of_det_invertible`, with the inverse defeq to `matrix.nonsing_inv`. -/
noncomputable def nonsing_inv_unit (h : is_unit A.det) : units (matrix n n α) :=
{ val := A,
inv := A⁻¹,
val_inv := by { rw matrix.mul_eq_mul, apply A.mul_nonsing_inv h, },
inv_val := by { rw matrix.mul_eq_mul, apply A.nonsing_inv_mul h, } }
lemma unit_of_det_invertible_eq_nonsing_inv_unit [invertible A.det] :
unit_of_det_invertible A = nonsing_inv_unit A (is_unit_of_invertible _) :=
by { ext, refl }
/-- When lowered to a prop, `matrix.det_invertible_of_invertible` and
`matrix.invertible_of_det_invertible` form an `iff`. -/
lemma is_unit_iff_is_unit_det : is_unit A ↔ is_unit A.det :=
begin
split; rintros ⟨x, hx⟩; refine @is_unit_of_invertible _ _ _ (id _),
{ haveI : invertible A := hx.rec x.invertible,
apply det_invertible_of_invertible, },
{ haveI : invertible A.det := hx.rec x.invertible,
apply invertible_of_det_invertible, },
end
/- `is_unit_of_invertible A`
converts the "stronger" condition `invertible A` to proposition `is_unit A`. -/
/-- `matrix.is_unit_det_of_invertible` converts `invertible A` to `is_unit A.det`. -/
lemma is_unit_det_of_invertible [invertible A] : is_unit A.det :=
@is_unit_of_invertible _ _ _(det_invertible_of_invertible A)
@[simp]
lemma inv_eq_nonsing_inv_of_invertible [invertible A] : ⅟ A = A⁻¹ :=
begin
suffices : is_unit A,
{ rw [←this.mul_left_inj, inv_of_mul_self, matrix.mul_eq_mul, nonsing_inv_mul],
rwa ←is_unit_iff_is_unit_det },
exact is_unit_of_invertible _
end
variables {A} {B}
/- `is_unit.invertible` lifts the proposition `is_unit A` to a constructive inverse of `A`. -/
/-- "Lift" the proposition `is_unit A.det` to a constructive inverse of `A`. -/
noncomputable def invertible_of_is_unit_det (h : is_unit A.det) : invertible A :=
⟨A⁻¹, nonsing_inv_mul A h, mul_nonsing_inv A h⟩
lemma is_unit_det_of_left_inverse (h : B ⬝ A = 1) : is_unit A.det :=
@is_unit_of_invertible _ _ _ (det_invertible_of_left_inverse _ _ h)
lemma is_unit_det_of_right_inverse (h : A ⬝ B = 1) : is_unit A.det :=
@is_unit_of_invertible _ _ _ (det_invertible_of_right_inverse _ _ h)
lemma det_ne_zero_of_left_inverse [nontrivial α] (h : B ⬝ A = 1) : A.det ≠ 0 :=
is_unit.ne_zero (matrix.is_unit_det_of_left_inverse h)
lemma det_ne_zero_of_right_inverse [nontrivial α] (h : A ⬝ B = 1) : A.det ≠ 0 :=
is_unit.ne_zero (matrix.is_unit_det_of_right_inverse h)
lemma nonsing_inv_left_right (h : A ⬝ B = 1) : B ⬝ A = 1 :=
begin
have h' : is_unit B.det := is_unit_det_of_left_inverse h,
calc B ⬝ A = (B ⬝ A) ⬝ (B ⬝ B⁻¹) : by simp only [h', matrix.mul_one, mul_nonsing_inv]
... = B ⬝ ((A ⬝ B) ⬝ B⁻¹) : by simp only [matrix.mul_assoc]
... = B ⬝ B⁻¹ : by simp only [h, matrix.one_mul]
... = 1 : mul_nonsing_inv B h',
end
lemma nonsing_inv_right_left (h : B ⬝ A = 1) : A ⬝ B = 1 :=
nonsing_inv_left_right h
/-- If matrix A is left invertible, then its inverse equals its left inverse. -/
lemma inv_eq_left_inv (h : B ⬝ A = 1) : A⁻¹ = B :=
begin
have h1 := (is_unit_det_of_left_inverse h),
have h2 := matrix.invertible_of_is_unit_det h1,
have := @inv_of_eq_left_inv (matrix n n α) (infer_instance) A B h2 h,
simp* at *,
end
/-- If matrix A is right invertible, then its inverse equals its right inverse. -/
lemma inv_eq_right_inv (h : A ⬝ B = 1) : A⁻¹ = B :=
begin
have h1 := (is_unit_det_of_right_inverse h),
have h2 := matrix.invertible_of_is_unit_det h1,
have := @inv_of_eq_right_inv (matrix n n α) (infer_instance) A B h2 h,
simp* at *,
end
/-- We can construct an instance of invertible A if A has a left inverse. -/
def invertible_of_left_inverse (h: B ⬝ A = 1) : invertible A :=
⟨B, h, nonsing_inv_right_left h⟩
/-- We can construct an instance of invertible A if A has a right inverse. -/
def invertible_of_right_inverse (h: A ⬝ B = 1) : invertible A :=
⟨B, nonsing_inv_left_right h, h⟩
section inv_eq_inv
variables {C : matrix n n α}
/-- The left inverse of matrix A is unique when existing. -/
lemma left_inv_eq_left_inv (h: B ⬝ A = 1) (g: C ⬝ A = 1) : B = C :=
by rw [←(inv_eq_left_inv h), ←(inv_eq_left_inv g)]
/-- The right inverse of matrix A is unique when existing. -/
lemma right_inv_eq_right_inv (h: A ⬝ B = 1) (g: A ⬝ C = 1) : B = C :=
by rw [←(inv_eq_right_inv h), ←(inv_eq_right_inv g)]
/-- The right inverse of matrix A equals the left inverse of A when they exist. -/
lemma right_inv_eq_left_inv (h: A ⬝ B = 1) (g: C ⬝ A = 1) : B = C :=
by rw [←(inv_eq_right_inv h), ←(inv_eq_left_inv g)]
lemma inv_inj (h : A⁻¹ = B⁻¹) (h' : is_unit A.det) : A = B :=
begin
refine left_inv_eq_left_inv (mul_nonsing_inv _ h') _,
rw h,
refine mul_nonsing_inv _ _,
rwa [←is_unit_nonsing_inv_det_iff, ←h, is_unit_nonsing_inv_det_iff]
end
end inv_eq_inv
variable (A)
@[simp] lemma mul_inv_of_invertible [invertible A] : A ⬝ A⁻¹ = 1 :=
mul_nonsing_inv A (is_unit_det_of_invertible A)
@[simp] lemma inv_mul_of_invertible [invertible A] : A⁻¹ ⬝ A = 1 :=
nonsing_inv_mul A (is_unit_det_of_invertible A)
@[simp] lemma inv_zero : (0 : matrix n n α)⁻¹ = 0 :=
begin
casesI (subsingleton_or_nontrivial α) with ht ht,
{ simp },
cases (fintype.card n).zero_le.eq_or_lt with hc hc,
{ rw [eq_comm, fintype.card_eq_zero_iff] at hc,
haveI := hc,
ext i,
exact (is_empty.false i).elim },
{ have hn : nonempty n := fintype.card_pos_iff.mp hc,
refine nonsing_inv_apply_not_is_unit _ _,
simp [hn] },
end
@[simp] lemma inv_one : (1 : matrix n n α)⁻¹ = 1 :=
inv_eq_left_inv (by simp)
lemma inv_smul (k : α) [invertible k] (h : is_unit A.det) : (k • A)⁻¹ = ⅟k • A⁻¹ :=
inv_eq_left_inv (by simp [h, smul_smul])
lemma inv_smul' (k : units α) (h : is_unit A.det) : (k • A)⁻¹ = k⁻¹ • A⁻¹ :=
inv_eq_left_inv (by simp [h, smul_smul])
lemma _root_.is_unit.coe_inv_mul {α : Type*} [monoid α] {a : α} (h : is_unit a) :
↑(h.unit)⁻¹ * a = 1 :=
units.mul_inv _
lemma _root_.is_unit.mul_coe_inv {α : Type*} [monoid α] {a : α} (h : is_unit a) :
a * ↑(h.unit)⁻¹ = 1 :=
begin
convert units.mul_inv _,
simp [h.unit_spec]
end
lemma _root_.is_unit.inv_smul {α : Type*} [monoid α] {a : α} (h : is_unit a) :
(h.unit)⁻¹ • a = 1 :=
h.coe_inv_mul
lemma inv_adjugate (A : matrix n n α) (h : is_unit A.det) :
(adjugate A)⁻¹ = h.unit⁻¹ • A :=
begin
refine inv_eq_left_inv _,
rw [smul_mul, mul_adjugate, units.smul_def, smul_smul, h.coe_inv_mul, one_smul]
end
@[simp] lemma inv_inv_inv (A : matrix n n α) : A⁻¹⁻¹⁻¹ = A⁻¹ :=
begin
by_cases h : is_unit A.det,
{ rw [nonsing_inv_nonsing_inv _ h] },
{ simp [nonsing_inv_apply_not_is_unit _ h] }
end
lemma mul_inv_rev (A B : matrix n n α) : (A ⬝ B)⁻¹ = B⁻¹ ⬝ A⁻¹ :=
begin
by_cases h : is_unit (A ⬝ B).det,
{ refine inv_eq_left_inv _,
rw det_mul at h,
rw [←matrix.mul_assoc, matrix.mul_assoc _ _ A,
nonsing_inv_mul _ (is_unit_of_mul_is_unit_left h),
matrix.mul_one, nonsing_inv_mul _ (is_unit_of_mul_is_unit_right h)] },
{ rw nonsing_inv_apply_not_is_unit _ h,
rw det_mul at h,
have : ¬ is_unit A.det ∨ ¬ is_unit B.det,
{ contrapose! h,
exact h.left.mul h.right },
cases this with h' h';
simp [nonsing_inv_apply_not_is_unit _ h'] }
end
lemma ring_hom.map_adjugate {R S : Type*} [comm_ring R] [comm_ring S] (f : R →+* S)
(M : matrix n n R) : f.map_matrix M.adjugate = matrix.adjugate (f.map_matrix M) :=
begin
ext i k,
have : (λ (j : n), ite (i = j) (1 : S) 0) = f ∘ (λ (j : n), ite (i = j) 1 0),
{ ext,
simp [apply_ite f] },
rw [adjugate_apply, ring_hom.map_matrix_apply, map_apply, ring_hom.map_matrix_apply,
this, ←map_update_row, ←ring_hom.map_matrix_apply, ←ring_hom.map_det, ←adjugate_apply]
end
lemma is_regular_of_is_left_regular_det {A : matrix n n α} (hA : is_left_regular A.det) :
is_regular A :=
begin
split,
{ intros B C h,
refine hA.matrix _,
rw [←matrix.one_mul B, ←matrix.one_mul C, ←matrix.smul_mul, ←matrix.smul_mul, ←adjugate_mul,
matrix.mul_assoc, matrix.mul_assoc, ←mul_eq_mul A, h, mul_eq_mul] },
{ intros B C h,
simp only [mul_eq_mul] at h,
refine hA.matrix _,
rw [←matrix.mul_one B, ←matrix.mul_one C, ←matrix.mul_smul, ←matrix.mul_smul, ←mul_adjugate,
←matrix.mul_assoc, ←matrix.mul_assoc, h] }
end
lemma adjugate_mul_distrib_aux (A B : matrix n n α)
(hA : is_left_regular A.det)
(hB : is_left_regular B.det) :
adjugate (A ⬝ B) = adjugate B ⬝ adjugate A :=
begin
have hAB : is_left_regular (A ⬝ B).det,
{ rw [det_mul],
exact hA.mul hB },
refine (is_regular_of_is_left_regular_det hAB).left _,
rw [mul_eq_mul, mul_adjugate, mul_eq_mul, matrix.mul_assoc, ←matrix.mul_assoc B, mul_adjugate,
smul_mul, matrix.one_mul, mul_smul, mul_adjugate, smul_smul, mul_comm, ←det_mul]
end
/--
Proof follows from "The trace Cayley-Hamilton theorem" by Darij Grinberg, Section 5.3
-/
lemma adjugate_mul_distrib (A B : matrix n n α) : adjugate (A ⬝ B) = adjugate B ⬝ adjugate A :=
begin
casesI subsingleton_or_nontrivial α,
{ simp },
let g : matrix n n α → matrix n n (polynomial α) :=
λ M, M.map polynomial.C + (polynomial.X : polynomial α) • 1,
let f' : matrix n n (polynomial α) →+* matrix n n α := (polynomial.eval_ring_hom 0).map_matrix,
have f'_inv : ∀ M, f' (g M) = M,
{ intro,
ext,
simp [f', g], },
have f'_adj : ∀ (M : matrix n n α), f' (adjugate (g M)) = adjugate M,
{ intro,
rw [ring_hom.map_adjugate, f'_inv] },
have f'_g_mul : ∀ (M N : matrix n n α), f' (g M ⬝ g N) = M ⬝ N,
{ intros,
rw [←mul_eq_mul, ring_hom.map_mul, f'_inv, f'_inv, mul_eq_mul] },
have hu : ∀ (M : matrix n n α), is_regular (g M).det,
{ intros M,
refine polynomial.monic.is_regular _,
simp only [g, polynomial.monic.def, ←polynomial.leading_coeff_det_X_one_add_C M, add_comm] },
rw [←f'_adj, ←f'_adj, ←f'_adj, ←mul_eq_mul (f' (adjugate (g B))), ←f'.map_mul, mul_eq_mul,
←adjugate_mul_distrib_aux _ _ (hu A).left (hu B).left, ring_hom.map_adjugate,
ring_hom.map_adjugate, f'_inv, f'_g_mul]
end
@[simp] lemma adjugate_pow (A : matrix n n α) (k : ℕ) :
adjugate (A ^ k) = (adjugate A) ^ k :=
begin
induction k with k IH,
{ simp },
{ rw [pow_succ', mul_eq_mul, adjugate_mul_distrib, IH, ←mul_eq_mul, pow_succ] }
end
end inv
/-- One form of Cramer's rule -/
@[simp] lemma det_smul_inv_mul_vec_eq_cramer (A : matrix n n α) (b : n → α) (h : is_unit A.det) :
A.det • A⁻¹.mul_vec b = cramer A b :=
begin
rw [cramer_eq_adjugate_mul_vec, A.nonsing_inv_apply h, ← smul_mul_vec_assoc, units.smul_def,
smul_smul, h.mul_coe_inv, one_smul]
end
/-- A stronger form of **Cramer's rule** that allows us to solve some instances of `A ⬝ x = b` even
if the determinant is not a unit. A sufficient (but still not necessary) condition is that `A.det`
divides `b`. -/
@[simp] lemma mul_vec_cramer (A : matrix n n α) (b : n → α) :
A.mul_vec (cramer A b) = A.det • b :=
by rw [cramer_eq_adjugate_mul_vec, mul_vec_mul_vec, mul_adjugate, smul_mul_vec_assoc, one_mul_vec]
section nondegenerate
variables {m R A : Type*} [fintype m] [comm_ring R] [integral_domain A]
/-- A matrix `M` is nondegenerate if for all `v ≠ 0`, there is a `w ≠ 0` with `w ⬝ M ⬝ v ≠ 0`. -/
def nondegenerate (M : matrix m m R) :=
∀ v, (∀ w, matrix.dot_product v (mul_vec M w) = 0) → v = 0
/-- If `M` is nondegenerate and `w ⬝ M ⬝ v = 0` for all `w`, then `v = 0`. -/
lemma nondegenerate.eq_zero_of_ortho {M : matrix m m R} (hM : nondegenerate M)
{v : m → R} (hv : ∀ w, matrix.dot_product v (mul_vec M w) = 0) : v = 0 :=
hM v hv
/-- If `M` is nondegenerate and `v ≠ 0`, then there is some `w` such that `w ⬝ M ⬝ v ≠ 0`. -/
lemma nondegenerate.exists_not_ortho_of_ne_zero {M : matrix m m R} (hM : nondegenerate M)
{v : m → R} (hv : v ≠ 0) : ∃ w, matrix.dot_product v (mul_vec M w) ≠ 0 :=
not_forall.mp (mt hM.eq_zero_of_ortho hv)
/-- If `M` has a nonzero determinant, then `M` as a bilinear form on `n → A` is nondegenerate.
See also `bilin_form.nondegenerate_of_det_ne_zero'` and `bilin_form.nondegenerate_of_det_ne_zero`.
-/
theorem nondegenerate_of_det_ne_zero {M : matrix n n A} (hM : M.det ≠ 0) :
nondegenerate M :=
begin
intros v hv,
ext i,
specialize hv (M.cramer (pi.single i 1)),
refine (mul_eq_zero.mp _).resolve_right hM,
convert hv,
simp only [mul_vec_cramer M (pi.single i 1), dot_product, pi.smul_apply, smul_eq_mul],
rw [finset.sum_eq_single i, pi.single_eq_same, mul_one],
{ intros j _ hj, simp [hj] },
{ intros, have := finset.mem_univ i, contradiction }
end
theorem eq_zero_of_vec_mul_eq_zero {M : matrix n n A} (hM : M.det ≠ 0) {v : n → A}
(hv : M.vec_mul v = 0) : v = 0 :=
(nondegenerate_of_det_ne_zero hM).eq_zero_of_ortho
(λ w, by rw [dot_product_mul_vec, hv, zero_dot_product])
theorem eq_zero_of_mul_vec_eq_zero {M : matrix n n A} (hM : M.det ≠ 0) {v : n → A}
(hv : M.mul_vec v = 0) :
v = 0 :=
eq_zero_of_vec_mul_eq_zero (by rwa det_transpose) ((vec_mul_transpose M v).trans hv)
end nondegenerate
end matrix
|
adef7914cca3facecd726b8d13ac175b4983a2c4 | 08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4 | /src/Lean/Meta/SynthInstance.lean | 6b479c80802f7d8f528bd84cea02afbcbbb67f6f | [
"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",
"Apache-2.0",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | gebner/lean4 | d51c4922640a52a6f7426536ea669ef18a1d9af5 | 8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f | refs/heads/master | 1,685,732,780,391 | 1,672,962,627,000 | 1,673,459,398,000 | 373,307,283 | 0 | 0 | Apache-2.0 | 1,691,316,730,000 | 1,622,669,271,000 | Lean | UTF-8 | Lean | false | false | 35,208 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Daniel Selsam, Leonardo de Moura
Type class instance synthesizer using tabled resolution.
-/
import Lean.Meta.Basic
import Lean.Meta.Instances
import Lean.Meta.AbstractMVars
import Lean.Meta.WHNF
import Lean.Meta.Check
import Lean.Util.Profile
namespace Lean.Meta
register_builtin_option synthInstance.maxHeartbeats : Nat := {
defValue := 20000
descr := "maximum amount of heartbeats per typeclass resolution problem. A heartbeat is number of (small) memory allocations (in thousands), 0 means no limit"
}
register_builtin_option synthInstance.maxSize : Nat := {
defValue := 128
descr := "maximum number of instances used to construct a solution in the type class instance synthesis procedure"
}
namespace SynthInstance
def getMaxHeartbeats (opts : Options) : Nat :=
synthInstance.maxHeartbeats.get opts * 1000
builtin_initialize inferTCGoalsRLAttr : TagAttribute ←
registerTagAttribute `infer_tc_goals_rl "instruct type class resolution procedure to solve goals from right to left for this instance"
def hasInferTCGoalsRLAttribute (env : Environment) (constName : Name) : Bool :=
inferTCGoalsRLAttr.hasTag env constName
structure GeneratorNode where
mvar : Expr
key : Expr
mctx : MetavarContext
instances : Array Expr
currInstanceIdx : Nat
deriving Inhabited
structure ConsumerNode where
mvar : Expr
key : Expr
mctx : MetavarContext
subgoals : List Expr
size : Nat -- instance size so far
deriving Inhabited
inductive Waiter where
| consumerNode : ConsumerNode → Waiter
| root : Waiter
def Waiter.isRoot : Waiter → Bool
| Waiter.consumerNode _ => false
| Waiter.root => true
/-!
In tabled resolution, we creating a mapping from goals (e.g., `Coe Nat ?x`) to
answers and waiters. Waiters are consumer nodes that are waiting for answers for a
particular node.
We implement this mapping using a `HashMap` where the keys are
normalized expressions. That is, we replace assignable metavariables
with auxiliary free variables of the form `_tc.<idx>`. We do
not declare these free variables in any local context, and we should
view them as "normalized names" for metavariables. For example, the
term `f ?m ?m ?n` is normalized as
`f _tc.0 _tc.0 _tc.1`.
This approach is structural, and we may visit the same goal more
than once if the different occurrences are just definitionally
equal, but not structurally equal.
Remark: a metavariable is assignable only if its depth is equal to
the metavar context depth.
-/
namespace MkTableKey
structure State where
nextIdx : Nat := 0
lmap : HashMap LMVarId Level := {}
emap : HashMap MVarId Expr := {}
mctx : MetavarContext
abbrev M := StateM State
@[always_inline]
instance : MonadMCtx M where
getMCtx := return (← get).mctx
modifyMCtx f := modify fun s => { s with mctx := f s.mctx }
partial def normLevel (u : Level) : M Level := do
if !u.hasMVar then
return u
else match u with
| Level.succ v => return u.updateSucc! (← normLevel v)
| Level.max v w => return u.updateMax! (← normLevel v) (← normLevel w)
| Level.imax v w => return u.updateIMax! (← normLevel v) (← normLevel w)
| Level.mvar mvarId =>
if (← getMCtx).getLevelDepth mvarId != (← getMCtx).depth then
return u
else
let s ← get
match (← get).lmap.find? mvarId with
| some u' => pure u'
| none =>
let u' := mkLevelParam <| Name.mkNum `_tc s.nextIdx
modify fun s => { s with nextIdx := s.nextIdx + 1, lmap := s.lmap.insert mvarId u' }
return u'
| u => return u
partial def normExpr (e : Expr) : M Expr := do
if !e.hasMVar then
pure e
else match e with
| Expr.const _ us => return e.updateConst! (← us.mapM normLevel)
| Expr.sort u => return e.updateSort! (← normLevel u)
| Expr.app f a => return e.updateApp! (← normExpr f) (← normExpr a)
| Expr.letE _ t v b _ => return e.updateLet! (← normExpr t) (← normExpr v) (← normExpr b)
| Expr.forallE _ d b _ => return e.updateForallE! (← normExpr d) (← normExpr b)
| Expr.lam _ d b _ => return e.updateLambdaE! (← normExpr d) (← normExpr b)
| Expr.mdata _ b => return e.updateMData! (← normExpr b)
| Expr.proj _ _ b => return e.updateProj! (← normExpr b)
| Expr.mvar mvarId =>
if !(← mvarId.isAssignable) then
return e
else
let s ← get
match s.emap.find? mvarId with
| some e' => pure e'
| none => do
let e' := mkFVar { name := Name.mkNum `_tc s.nextIdx }
modify fun s => { s with nextIdx := s.nextIdx + 1, emap := s.emap.insert mvarId e' }
return e'
| _ => return e
end MkTableKey
/-- Remark: `mkTableKey` assumes `e` does not contain assigned metavariables. -/
def mkTableKey [Monad m] [MonadMCtx m] (e : Expr) : m Expr := do
let (r, s) := MkTableKey.normExpr e |>.run { mctx := (← getMCtx) }
setMCtx s.mctx
return r
structure Answer where
result : AbstractMVarsResult
resultType : Expr
size : Nat
deriving Inhabited
structure TableEntry where
waiters : Array Waiter
answers : Array Answer := #[]
structure Context where
maxResultSize : Nat
maxHeartbeats : Nat
/--
Remark: the SynthInstance.State is not really an extension of `Meta.State`.
The field `postponed` is not needed, and the field `mctx` is misleading since
`synthInstance` methods operate over different `MetavarContext`s simultaneously.
That being said, we still use `extends` because it makes it simpler to move from
`M` to `MetaM`.
-/
structure State where
result? : Option AbstractMVarsResult := none
generatorStack : Array GeneratorNode := #[]
resumeStack : Array (ConsumerNode × Answer) := #[]
tableEntries : HashMap Expr TableEntry := {}
abbrev SynthM := ReaderT Context $ StateRefT State MetaM
def checkMaxHeartbeats : SynthM Unit := do
Core.checkMaxHeartbeatsCore "typeclass" `synthInstance.maxHeartbeats (← read).maxHeartbeats
@[inline] def mapMetaM (f : forall {α}, MetaM α → MetaM α) {α} : SynthM α → SynthM α :=
monadMap @f
instance : Inhabited (SynthM α) where
default := fun _ _ => default
/-- Return globals and locals instances that may unify with `type` -/
def getInstances (type : Expr) : MetaM (Array Expr) := do
-- We must retrieve `localInstances` before we use `forallTelescopeReducing` because it will update the set of local instances
let localInstances ← getLocalInstances
forallTelescopeReducing type fun _ type => do
let className? ← isClass? type
match className? with
| none => throwError "type class instance expected{indentExpr type}"
| some className =>
let globalInstances ← getGlobalInstancesIndex
let result ← globalInstances.getUnify type
-- Using insertion sort because it is stable and the array `result` should be mostly sorted.
-- Most instances have default priority.
let result := result.insertionSort fun e₁ e₂ => e₁.priority < e₂.priority
let erasedInstances ← getErasedInstances
let result ← result.filterMapM fun e => match e.val with
| Expr.const constName us =>
if erasedInstances.contains constName then
return none
else
return some <| e.val.updateConst! (← us.mapM (fun _ => mkFreshLevelMVar))
| _ => panic! "global instance is not a constant"
let result := localInstances.foldl (init := result) fun (result : Array Expr) linst =>
if linst.className == className then result.push linst.fvar else result
trace[Meta.synthInstance.instances] result
return result
def mkGeneratorNode? (key mvar : Expr) : MetaM (Option GeneratorNode) := do
let mvarType ← inferType mvar
let mvarType ← instantiateMVars mvarType
let instances ← getInstances mvarType
if instances.isEmpty then
return none
else
let mctx ← getMCtx
return some {
mvar, key, mctx, instances
currInstanceIdx := instances.size
}
/--
Create a new generator node for `mvar` and add `waiter` as its waiter.
`key` must be `mkTableKey mctx mvarType`. -/
def newSubgoal (mctx : MetavarContext) (key : Expr) (mvar : Expr) (waiter : Waiter) : SynthM Unit :=
withMCtx mctx do withTraceNode' `Meta.synthInstance do
match (← mkGeneratorNode? key mvar) with
| none => pure ((), m!"no instances for {key}")
| some node =>
let entry : TableEntry := { waiters := #[waiter] }
modify fun s =>
{ s with
generatorStack := s.generatorStack.push node
tableEntries := s.tableEntries.insert key entry }
pure ((), m!"new goal {key}")
def findEntry? (key : Expr) : SynthM (Option TableEntry) := do
return (← get).tableEntries.find? key
def getEntry (key : Expr) : SynthM TableEntry := do
match (← findEntry? key) with
| none => panic! "invalid key at synthInstance"
| some entry => pure entry
/--
Create a `key` for the goal associated with the given metavariable.
That is, we create a key for the type of the metavariable.
We must instantiate assigned metavariables before we invoke `mkTableKey`. -/
def mkTableKeyFor (mctx : MetavarContext) (mvar : Expr) : SynthM Expr :=
withMCtx mctx do
let mvarType ← inferType mvar
let mvarType ← instantiateMVars mvarType
mkTableKey mvarType
/-- See `getSubgoals` and `getSubgoalsAux`
We use the parameter `j` to reduce the number of `instantiate*` invocations.
It is the same approach we use at `forallTelescope` and `lambdaTelescope`.
Given `getSubgoalsAux args j subgoals instVal type`,
we have that `type.instantiateRevRange j args.size args` does not have loose bound variables. -/
structure SubgoalsResult where
subgoals : List Expr
instVal : Expr
instTypeBody : Expr
private partial def getSubgoalsAux (lctx : LocalContext) (localInsts : LocalInstances) (xs : Array Expr)
: Array Expr → Nat → List Expr → Expr → Expr → MetaM SubgoalsResult
| args, j, subgoals, instVal, Expr.forallE _ d b bi => do
let d := d.instantiateRevRange j args.size args
let mvarType ← mkForallFVars xs d
let mvar ← mkFreshExprMVarAt lctx localInsts mvarType
let arg := mkAppN mvar xs
let instVal := mkApp instVal arg
let subgoals := if bi.isInstImplicit then mvar::subgoals else subgoals
let args := args.push (mkAppN mvar xs)
getSubgoalsAux lctx localInsts xs args j subgoals instVal b
| args, j, subgoals, instVal, type => do
let type := type.instantiateRevRange j args.size args
let type ← whnf type
if type.isForall then
getSubgoalsAux lctx localInsts xs args args.size subgoals instVal type
else
return ⟨subgoals, instVal, type⟩
/--
`getSubgoals lctx localInsts xs inst` creates the subgoals for the instance `inst`.
The subgoals are in the context of the free variables `xs`, and
`(lctx, localInsts)` is the local context and instances before we added the free variables to it.
This extra complication is required because
1- We want all metavariables created by `synthInstance` to share the same local context.
2- We want to ensure that applications such as `mvar xs` are higher order patterns.
The method `getGoals` create a new metavariable for each parameter of `inst`.
For example, suppose the type of `inst` is `forall (x_1 : A_1) ... (x_n : A_n), B x_1 ... x_n`.
Then, we create the metavariables `?m_i : forall xs, A_i`, and return the subset of these
metavariables that are instance implicit arguments, and the expressions:
- `inst (?m_1 xs) ... (?m_n xs)` (aka `instVal`)
- `B (?m_1 xs) ... (?m_n xs)` -/
def getSubgoals (lctx : LocalContext) (localInsts : LocalInstances) (xs : Array Expr) (inst : Expr) : MetaM SubgoalsResult := do
let instType ← inferType inst
let result ← getSubgoalsAux lctx localInsts xs #[] 0 [] inst instType
match inst.getAppFn with
| Expr.const constName _ =>
let env ← getEnv
if hasInferTCGoalsRLAttribute env constName then
return result
else
return { result with subgoals := result.subgoals.reverse }
| _ => return result
/--
Try to synthesize metavariable `mvar` using the instance `inst`.
Remark: `mctx` is set using `withMCtx`.
If it succeeds, the result is a new updated metavariable context and a new list of subgoals.
A subgoal is created for each instance implicit parameter of `inst`. -/
def tryResolve (mvar : Expr) (inst : Expr) : MetaM (Option (MetavarContext × List Expr)) := do
if ← mvar.mvarId!.isAssigned then
/- The metavariable `mvar` may have been assigned when solving typing constraints.
This may happen when a local instance type depends on other local instances.
For example, in Mathlib, we have
```
@Submodule.setLike : {R : Type u_1} → {M : Type u_2} →
[_inst_1 : Semiring R] →
[_inst_2 : AddCommMonoid M] →
[_inst_3 : @ModuleS R M _inst_1 _inst_2] →
SetLike (@Submodule R M _inst_1 _inst_2 _inst_3) M
```
TODO: discuss what is the correct behavior here. There are other possibilities.
1) We could try to synthesize the instances `_inst_1` and `_inst_2` and check
whether it is defeq to the one inferred by typing constraints. That is, we
remove this `if`-statement. We discarded this one because some Mathlib theorems
failed to be elaborated using it.
2) Generate an error/warning message when instances such as `Submodule.setLike` are declared,
and instruct user to use `{}` binder annotation for `_inst_1` `_inst_2`.
It is important to check here whether `mvar` is *assigned*, it might
still contain metavariables (such as universe mvars etc.).
-/
return some ((← getMCtx), [])
let mvarType ← inferType mvar
let lctx ← getLCtx
let localInsts ← getLocalInstances
forallTelescopeReducing mvarType fun xs mvarTypeBody => do
let ⟨subgoals, instVal, instTypeBody⟩ ← getSubgoals lctx localInsts xs inst
withTraceNode `Meta.synthInstance.tryResolve (withMCtx (← getMCtx) do
return m!"{exceptOptionEmoji ·} {← instantiateMVars mvarTypeBody} ≟ {← instantiateMVars instTypeBody}") do
if (← isDefEq mvarTypeBody instTypeBody) then
let instVal ← mkLambdaFVars xs instVal
if (← isDefEq mvar instVal) then
return some ((← getMCtx), subgoals)
return none
/--
Assign a precomputed answer to `mvar`.
If it succeeds, the result is a new updated metavariable context and a new list of subgoals. -/
def tryAnswer (mctx : MetavarContext) (mvar : Expr) (answer : Answer) : SynthM (Option MetavarContext) :=
withMCtx mctx do
let (_, _, val) ← openAbstractMVarsResult answer.result
if (← isDefEq mvar val) then
return some (← getMCtx)
else
return none
/-- Move waiters that are waiting for the given answer to the resume stack. -/
def wakeUp (answer : Answer) : Waiter → SynthM Unit
| Waiter.root => do
/- Recall that we now use `ignoreLevelMVarDepth := true`. Thus, we should allow solutions
containing universe metavariables, and not check `answer.result.paramNames.isEmpty`.
We use `openAbstractMVarsResult` to construct the universe metavariables
at the correct depth. -/
if answer.result.numMVars == 0 then
modify fun s => { s with result? := answer.result }
else
let (_, _, answerExpr) ← openAbstractMVarsResult answer.result
trace[Meta.synthInstance] "skip answer containing metavariables {answerExpr}"
| Waiter.consumerNode cNode =>
modify fun s => { s with resumeStack := s.resumeStack.push (cNode, answer) }
def isNewAnswer (oldAnswers : Array Answer) (answer : Answer) : Bool :=
oldAnswers.all fun oldAnswer =>
-- Remark: isDefEq here is too expensive. TODO: if `==` is too imprecise, add some light normalization to `resultType` at `addAnswer`
-- iseq ← isDefEq oldAnswer.resultType answer.resultType; pure (!iseq)
oldAnswer.resultType != answer.resultType
private def mkAnswer (cNode : ConsumerNode) : MetaM Answer :=
withMCtx cNode.mctx do
let val ← instantiateMVars cNode.mvar
trace[Meta.synthInstance.newAnswer] "size: {cNode.size}, val: {val}"
let result ← abstractMVars val -- assignable metavariables become parameters
let resultType ← inferType result.expr
return { result, resultType, size := cNode.size + 1 }
/--
Create a new answer after `cNode` resolved all subgoals.
That is, `cNode.subgoals == []`.
And then, store it in the tabled entries map, and wakeup waiters. -/
def addAnswer (cNode : ConsumerNode) : SynthM Unit := do
withMCtx cNode.mctx do
if cNode.size ≥ (← read).maxResultSize then
trace[Meta.synthInstance.answer] "{crossEmoji} {← instantiateMVars (← inferType cNode.mvar)}{Format.line}(size: {cNode.size} ≥ {(← read).maxResultSize})"
else
withTraceNode `Meta.synthInstance.answer
(fun _ => return m!"{checkEmoji} {← instantiateMVars (← inferType cNode.mvar)}") do
let answer ← mkAnswer cNode
-- Remark: `answer` does not contain assignable or assigned metavariables.
let key := cNode.key
let entry ← getEntry key
if isNewAnswer entry.answers answer then
let newEntry := { entry with answers := entry.answers.push answer }
modify fun s => { s with tableEntries := s.tableEntries.insert key newEntry }
entry.waiters.forM (wakeUp answer)
/--
Return `true` if a type of the form `(a_1 : A_1) → ... → (a_n : A_n) → B` has an unused argument `a_i`.
Remark: This is syntactic check and no reduction is performed.
-/
private def hasUnusedArguments : Expr → Bool
| Expr.forallE _ _ b _ => !b.hasLooseBVar 0 || hasUnusedArguments b
| _ => false
/--
If the type of the metavariable `mvar` has unused argument, return a pair `(α, transformer)`
where `α` is a new type without the unused arguments and the `transformer` is a function for coverting a
solution with type `α` into a value that can be assigned to `mvar`.
Example: suppose `mvar` has type `(a : A) → (b : B a) → (c : C a) → D a c`, the result is the pair
```
((a : A) → (c : C a) → D a c,
fun (f : (a : A) → (c : C a) → D a c) (a : A) (b : B a) (c : C a) => f a c
)
```
This method is used to improve the effectiveness of the TC resolution procedure. It was suggested and prototyped by
Tomas Skrivan. It improves the support for instances of type `a : A → C` where `a` does not appear in class `C`.
When we look for such an instance it is enough to look for an instance `c : C` and then return `fun _ => c`.
Tomas' approach makes sure that instance of a type like `a : A → C` never gets tabled/cached. More on that later.
At the core is this method. it takes an expression E and does two things:
The modification to TC resolution works this way: We are looking for an instance of `E`, if it is tabled
just get it as normal, but if not first remove all unused arguments producing `E'`. Now we look up the table again but
for `E'`. If it exists, use the transformer to create E. If it does not exists, create a new goal `E'`.
-/
private def removeUnusedArguments? (mctx : MetavarContext) (mvar : Expr) : MetaM (Option (Expr × Expr)) :=
withMCtx mctx do
let mvarType ← instantiateMVars (← inferType mvar)
if !hasUnusedArguments mvarType then
return none
else
forallTelescope mvarType fun xs body => do
let ys ← xs.foldrM (init := []) fun x ys => do
if body.containsFVar x.fvarId! then
return x :: ys
else if (← ys.anyM fun y => return (← inferType y).containsFVar x.fvarId!) then
return x :: ys
else
return ys
let ys := ys.toArray
let mvarType' ← mkForallFVars ys body
withLocalDeclD `redf mvarType' fun f => do
let transformer ← mkLambdaFVars #[f] (← mkLambdaFVars xs (mkAppN f ys))
trace[Meta.synthInstance.unusedArgs] "{mvarType}\nhas unused arguments, reduced type{indentExpr mvarType'}\nTransformer{indentExpr transformer}"
return some (mvarType', transformer)
/-- Process the next subgoal in the given consumer node. -/
def consume (cNode : ConsumerNode) : SynthM Unit := do
match cNode.subgoals with
| [] => addAnswer cNode
| mvar::_ =>
let waiter := Waiter.consumerNode cNode
let key ← mkTableKeyFor cNode.mctx mvar
let entry? ← findEntry? key
match entry? with
| none =>
-- Remove unused arguments and try again, see comment at `removeUnusedArguments?`
match (← removeUnusedArguments? cNode.mctx mvar) with
| none => newSubgoal cNode.mctx key mvar waiter
| some (mvarType', transformer) =>
let key' ← withMCtx cNode.mctx <| mkTableKey mvarType'
match (← findEntry? key') with
| none =>
let (mctx', mvar') ← withMCtx cNode.mctx do
let mvar' ← mkFreshExprMVar mvarType'
return (← getMCtx, mvar')
newSubgoal mctx' key' mvar' (Waiter.consumerNode { cNode with mctx := mctx', subgoals := mvar'::cNode.subgoals })
| some entry' =>
let answers' ← entry'.answers.mapM fun a => withMCtx cNode.mctx do
let trAnswr := Expr.betaRev transformer #[← instantiateMVars a.result.expr]
let trAnswrType ← inferType trAnswr
pure { a with result.expr := trAnswr, resultType := trAnswrType }
modify fun s =>
{ s with
resumeStack := answers'.foldl (fun s answer => s.push (cNode, answer)) s.resumeStack,
tableEntries := s.tableEntries.insert key' { entry' with waiters := entry'.waiters.push waiter } }
| some entry => modify fun s =>
{ s with
resumeStack := entry.answers.foldl (fun s answer => s.push (cNode, answer)) s.resumeStack,
tableEntries := s.tableEntries.insert key { entry with waiters := entry.waiters.push waiter } }
def getTop : SynthM GeneratorNode :=
return (← get).generatorStack.back
@[inline] def modifyTop (f : GeneratorNode → GeneratorNode) : SynthM Unit :=
modify fun s => { s with generatorStack := s.generatorStack.modify (s.generatorStack.size - 1) f }
/-- Try the next instance in the node on the top of the generator stack. -/
def generate : SynthM Unit := do
let gNode ← getTop
if gNode.currInstanceIdx == 0 then
modify fun s => { s with generatorStack := s.generatorStack.pop }
else
let key := gNode.key
let idx := gNode.currInstanceIdx - 1
let inst := gNode.instances.get! idx
let mctx := gNode.mctx
let mvar := gNode.mvar
discard do withMCtx mctx do
withTraceNode `Meta.synthInstance
(return m!"{exceptOptionEmoji ·} apply {inst} to {← instantiateMVars (← inferType mvar)}") do
modifyTop fun gNode => { gNode with currInstanceIdx := idx }
if let some (mctx, subgoals) ← tryResolve mvar inst then
consume { key, mvar, subgoals, mctx, size := 0 }
return some ()
return none
def getNextToResume : SynthM (ConsumerNode × Answer) := do
let r := (← get).resumeStack.back
modify fun s => { s with resumeStack := s.resumeStack.pop }
return r
/--
Given `(cNode, answer)` on the top of the resume stack, continue execution by using `answer` to solve the
next subgoal. -/
def resume : SynthM Unit := do
let (cNode, answer) ← getNextToResume
match cNode.subgoals with
| [] => panic! "resume found no remaining subgoals"
| mvar::rest =>
match (← tryAnswer cNode.mctx mvar answer) with
| none => return ()
| some mctx =>
withMCtx mctx do
let goal ← inferType cNode.mvar
let subgoal ← inferType mvar
withTraceNode `Meta.synthInstance.resume
(fun _ => withMCtx cNode.mctx do
return m!"propagating {← instantiateMVars answer.resultType} to subgoal {← instantiateMVars subgoal} of {← instantiateMVars goal}") do
trace[Meta.synthInstance.resume] "size: {cNode.size + answer.size}"
consume { key := cNode.key, mvar := cNode.mvar, subgoals := rest, mctx, size := cNode.size + answer.size }
def step : SynthM Bool := do
checkMaxHeartbeats
let s ← get
if !s.resumeStack.isEmpty then
resume
return true
else if !s.generatorStack.isEmpty then
generate
return true
else
return false
def getResult : SynthM (Option AbstractMVarsResult) :=
return (← get).result?
partial def synth : SynthM (Option AbstractMVarsResult) := do
if (← step) then
match (← getResult) with
| none => synth
| some result => return result
else
return none
def main (type : Expr) (maxResultSize : Nat) : MetaM (Option AbstractMVarsResult) :=
withCurrHeartbeats do
let mvar ← mkFreshExprMVar type
let key ← mkTableKey type
let action : SynthM (Option AbstractMVarsResult) := do
newSubgoal (← getMCtx) key mvar Waiter.root
synth
try
action.run { maxResultSize := maxResultSize, maxHeartbeats := getMaxHeartbeats (← getOptions) } |>.run' {}
catch ex =>
if ex.isMaxHeartbeat then
throwError "failed to synthesize{indentExpr type}\n{ex.toMessageData}"
else
throw ex
end SynthInstance
/-!
Type class parameters can be annotated with `outParam` annotations.
Given `C a_1 ... a_n`, we replace `a_i` with a fresh metavariable `?m_i` IF
`a_i` is an `outParam`.
The result is type correct because we reject type class declarations IF
it contains a regular parameter X that depends on an `out` parameter Y.
Then, we execute type class resolution as usual.
If it succeeds, and metavariables ?m_i have been assigned, we try to unify
the original type `C a_1 ... a_n` witht the normalized one.
-/
private def preprocess (type : Expr) : MetaM Expr :=
forallTelescopeReducing type fun xs type => do
let type ← whnf type
mkForallFVars xs type
private def preprocessLevels (us : List Level) : MetaM (List Level × Bool) := do
let mut r := #[]
let mut modified := false
for u in us do
let u ← instantiateLevelMVars u
if u.hasMVar then
r := r.push (← mkFreshLevelMVar)
modified := true
else
r := r.push u
return (r.toList, modified)
private partial def preprocessArgs (type : Expr) (i : Nat) (args : Array Expr) (outParamsPos : Array Nat) : MetaM (Array Expr) := do
if h : i < args.size then
let type ← whnf type
match type with
| .forallE _ d b _ => do
let arg := args.get ⟨i, h⟩
/-
We should not simply check `d.isOutParam`. See `checkOutParam` and issue #1852.
If an instance implicit argument depends on an `outParam`, it is treated as an `outParam` too.
-/
let arg ← if outParamsPos.contains i then mkFreshExprMVar d else pure arg
let args := args.set ⟨i, h⟩ arg
preprocessArgs (b.instantiate1 arg) (i+1) args outParamsPos
| _ =>
throwError "type class resolution failed, insufficient number of arguments" -- TODO improve error message
else
return args
private def preprocessOutParam (type : Expr) : MetaM Expr :=
forallTelescope type fun xs typeBody => do
match typeBody.getAppFn with
| c@(Expr.const declName _) =>
let env ← getEnv
if let some outParamsPos := getOutParamPositions? env declName then
unless outParamsPos.isEmpty do
let args := typeBody.getAppArgs
let cType ← inferType c
let args ← preprocessArgs cType 0 args outParamsPos
return (← mkForallFVars xs (mkAppN c args))
return type
| _ =>
return type
/-!
Remark: when `maxResultSize? == none`, the configuration option `synthInstance.maxResultSize` is used.
Remark: we use a different option for controlling the maximum result size for coercions.
-/
def synthInstance? (type : Expr) (maxResultSize? : Option Nat := none) : MetaM (Option Expr) := do profileitM Exception "typeclass inference" (← getOptions) do
let opts ← getOptions
let maxResultSize := maxResultSize?.getD (synthInstance.maxSize.get opts)
/-
We disable eta for structures that are not classes during TC resolution because it allows us to find unintended solutions.
See discussion at
https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/.60constructor.60.20and.20.60Applicative.60/near/279984801
-/
withConfig (fun config => { config with isDefEqStuckEx := true, transparency := TransparencyMode.instances,
foApprox := true, ctxApprox := true, constApprox := false,
etaStruct := .notClasses }) do
let type ← instantiateMVars type
let type ← preprocess type
let s ← get
match s.cache.synthInstance.find? type with
| some result => pure result
| none =>
withTraceNode `Meta.synthInstance
(return m!"{exceptOptionEmoji ·} {← instantiateMVars type}") do
let result? ← withNewMCtxDepth (allowLevelAssignments := true) do
let normType ← preprocessOutParam type
SynthInstance.main normType maxResultSize
let resultHasUnivMVars := if let some result := result? then !result.paramNames.isEmpty else false
let result? ← match result? with
| none => pure none
| some result => do
let (_, _, result) ← openAbstractMVarsResult result
trace[Meta.synthInstance] "result {result}"
let resultType ← inferType result
/- Output parameters of local instances may be marked as `syntheticOpaque` by the application-elaborator.
We use `withAssignableSyntheticOpaque` to make sure this kind of parameter can be assigned by the following `isDefEq`.
TODO: rewrite this check to avoid `withAssignableSyntheticOpaque`. -/
if (← withDefault <| withAssignableSyntheticOpaque <| isDefEq type resultType) then
let result ← instantiateMVars result
/- We use `check` to propogate universe constraints implied by the `result`.
Recall that we use `allowLevelAssignments := true` which allows universe metavariables in the current depth to be assigned,
but these assignments are discarded by `withNewMCtxDepth`.
TODO: If this `check` is a performance bottleneck, we can improve performance by tracking whether
a universe metavariable from previous universe levels have been assigned or not during TC resolution.
We only need to perform the `check` if this kind of assignment have been performed.
The example in the issue #796 exposed this issue.
```
structure A
class B (a : outParam A) (α : Sort u)
class C {a : A} (α : Sort u) [B a α]
class D {a : A} (α : Sort u) [B a α] [c : C α]
class E (a : A) where [c (α : Sort u) [B a α] : C α]
instance c {a : A} [e : E a] (α : Sort u) [B a α] : C α := e.c α
def d {a : A} [e : E a] (α : Sort u) [b : B a α] : D α := ⟨⟩
```
The term `D α` has two instance implicit arguments. The second one has type `C α`, and TC
resolution produces the result `@c.{u} a e α b`.
Note that the `e` has type `E.{?v} a`, and `E` is universe polymorphic,
but the universe does not occur in the parameter `a`. We have that `?v := u` is implied by `@c.{u} a e α b`,
but this assignment is lost.
-/
check result
pure (some result)
else
trace[Meta.synthInstance] "{crossEmoji} result type{indentExpr resultType}\nis not definitionally equal to{indentExpr type}"
pure none
if type.hasMVar || resultHasUnivMVars then
pure result?
else do
modify fun s => { s with cache.synthInstance := s.cache.synthInstance.insert type result? }
pure result?
/--
Return `LOption.some r` if succeeded, `LOption.none` if it failed, and `LOption.undef` if
instance cannot be synthesized right now because `type` contains metavariables. -/
def trySynthInstance (type : Expr) (maxResultSize? : Option Nat := none) : MetaM (LOption Expr) := do
catchInternalId isDefEqStuckExceptionId
(toLOptionM <| synthInstance? type maxResultSize?)
(fun _ => pure LOption.undef)
def synthInstance (type : Expr) (maxResultSize? : Option Nat := none) : MetaM Expr :=
catchInternalId isDefEqStuckExceptionId
(do
let result? ← synthInstance? type maxResultSize?
match result? with
| some result => pure result
| none => throwError "failed to synthesize{indentExpr type}")
(fun _ => throwError "failed to synthesize{indentExpr type}")
@[export lean_synth_pending]
private def synthPendingImp (mvarId : MVarId) : MetaM Bool := withIncRecDepth <| mvarId.withContext do
let mvarDecl ← mvarId.getDecl
match mvarDecl.kind with
| MetavarKind.syntheticOpaque =>
return false
| _ =>
/- Check whether the type of the given metavariable is a class or not. If yes, then try to synthesize
it using type class resolution. We only do it for `synthetic` and `natural` metavariables. -/
match (← isClass? mvarDecl.type) with
| none =>
return false
| some _ =>
/- TODO: use a configuration option instead of the hard-coded limit `1`. -/
if (← read).synthPendingDepth > 1 then
trace[Meta.synthPending] "too many nested synthPending invocations"
return false
else
withReader (fun ctx => { ctx with synthPendingDepth := ctx.synthPendingDepth + 1 }) do
trace[Meta.synthPending] "synthPending {mkMVar mvarId}"
let val? ← catchInternalId isDefEqStuckExceptionId (synthInstance? mvarDecl.type (maxResultSize? := none)) (fun _ => pure none)
match val? with
| none =>
return false
| some val =>
if (← mvarId.isAssigned) then
return false
else
mvarId.assign val
return true
builtin_initialize
registerTraceClass `Meta.synthPending
registerTraceClass `Meta.synthInstance
registerTraceClass `Meta.synthInstance.instances (inherited := true)
registerTraceClass `Meta.synthInstance.tryResolve (inherited := true)
registerTraceClass `Meta.synthInstance.resume (inherited := true)
registerTraceClass `Meta.synthInstance.unusedArgs
registerTraceClass `Meta.synthInstance.newAnswer
end Lean.Meta
|
7b137401ca8d9cf5ab4a1837b3eba467c3c1b66e | 367134ba5a65885e863bdc4507601606690974c1 | /src/tactic/linarith/verification.lean | b1a95705429252c18e5d71066619e86f66ce30e9 | [
"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 | 7,236 | lean | /-
Copyright (c) 2020 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis
-/
import tactic.linarith.elimination
import tactic.linarith.parsing
/-!
# Deriving a proof of false
`linarith` uses an untrusted oracle to produce a certificate of unsatisfiability.
It needs to do some proof reconstruction work to turn this into a proof term.
This file implements the reconstruction.
## Main declarations
The public facing declaration in this file is `prove_false_by_linarith`.
-/
namespace linarith
open ineq tactic native
/-! ### Auxiliary functions for assembling proofs -/
/--
`mul_expr n e` creates a `pexpr` representing `n*e`.
When elaborated, the coefficient will be a native numeral of the same type as `e`.
-/
meta def mul_expr (n : ℕ) (e : expr) : pexpr :=
if n = 1 then ``(%%e) else
``(%%(nat.to_pexpr n) * %%e)
private meta def add_exprs_aux : pexpr → list pexpr → pexpr
| p [] := p
| p [a] := ``(%%p + %%a)
| p (h::t) := add_exprs_aux ``(%%p + %%h) t
/--
`add_exprs l` creates a `pexpr` representing the sum of the elements of `l`, associated left.
If `l` is empty, it will be the `pexpr` 0. Otherwise, it does not include 0 in the sum.
-/
meta def add_exprs : list pexpr → pexpr
| [] := ``(0)
| (h::t) := add_exprs_aux h t
/--
If our goal is to add together two inequalities `t1 R1 0` and `t2 R2 0`,
`ineq_const_nm R1 R2` produces the strength of the inequality in the sum `R`,
along with the name of a lemma to apply in order to conclude `t1 + t2 R 0`.
-/
meta def ineq_const_nm : ineq → ineq → (name × ineq)
| eq eq := (``eq_of_eq_of_eq, eq)
| eq le := (``le_of_eq_of_le, le)
| eq lt := (``lt_of_eq_of_lt, lt)
| le eq := (``le_of_le_of_eq, le)
| le le := (`add_nonpos, le)
| le lt := (`add_neg_of_nonpos_of_neg, lt)
| lt eq := (``lt_of_lt_of_eq, lt)
| lt le := (`add_neg_of_neg_of_nonpos, lt)
| lt lt := (`add_neg, lt)
/--
`mk_lt_zero_pf_aux c pf npf coeff` assumes that `pf` is a proof of `t1 R1 0` and `npf` is a proof
of `t2 R2 0`. It uses `mk_single_comp_zero_pf` to prove `t1 + coeff*t2 R 0`, and returns `R`
along with this proof.
-/
meta def mk_lt_zero_pf_aux (c : ineq) (pf npf : expr) (coeff : ℕ) : tactic (ineq × expr) :=
do (iq, h') ← mk_single_comp_zero_pf coeff npf,
let (nm, niq) := ineq_const_nm c iq,
prod.mk niq <$> mk_app nm [pf, h']
/--
`mk_lt_zero_pf coeffs pfs` takes a list of proofs of the form `tᵢ Rᵢ 0`,
paired with coefficients `cᵢ`.
It produces a proof that `∑cᵢ * tᵢ R 0`, where `R` is as strong as possible.
-/
meta def mk_lt_zero_pf : list (expr × ℕ) → tactic expr
| [] := fail "no linear hypotheses found"
| [(h, c)] := prod.snd <$> mk_single_comp_zero_pf c h
| ((h, c)::t) :=
do (iq, h') ← mk_single_comp_zero_pf c h,
prod.snd <$> t.mfoldl (λ pr ce, mk_lt_zero_pf_aux pr.1 pr.2 ce.1 ce.2) (iq, h')
/-- If `prf` is a proof of `t R s`, `term_of_ineq_prf prf` returns `t`. -/
meta def term_of_ineq_prf (prf : expr) : tactic expr :=
prod.fst <$> (infer_type prf >>= get_rel_sides)
/-- If `prf` is a proof of `t R s`, `ineq_prf_tp prf` returns the type of `t`. -/
meta def ineq_prf_tp (prf : expr) : tactic expr :=
term_of_ineq_prf prf >>= infer_type
/--
`mk_neg_one_lt_zero_pf tp` returns a proof of `-1 < 0`,
where the numerals are natively of type `tp`.
-/
meta def mk_neg_one_lt_zero_pf (tp : expr) : tactic expr :=
do zero_lt_one ← mk_mapp `zero_lt_one [tp, none, none],
mk_app `neg_neg_of_pos [zero_lt_one]
/--
If `e` is a proof that `t = 0`, `mk_neg_eq_zero_pf e` returns a proof that `-t = 0`.
-/
meta def mk_neg_eq_zero_pf (e : expr) : tactic expr :=
to_expr ``(neg_eq_zero.mpr %%e)
/--
`prove_eq_zero_using tac e` tries to use `tac` to construct a proof of `e = 0`.
-/
meta def prove_eq_zero_using (tac : tactic unit) (e : expr) : tactic expr :=
do tgt ← to_expr ``(%%e = 0),
prod.snd <$> solve_aux tgt (tac >> done)
/--
`add_neg_eq_pfs l` inspects the list of proofs `l` for proofs of the form `t = 0`. For each such
proof, it adds a proof of `-t = 0` to the list.
-/
meta def add_neg_eq_pfs : list expr → tactic (list expr)
| [] := return []
| (h::t) :=
do some (iq, tp) ← parse_into_comp_and_expr <$> infer_type h,
match iq with
| ineq.eq := do nep ← mk_neg_eq_zero_pf h, tl ← add_neg_eq_pfs t, return $ h::nep::tl
| _ := list.cons h <$> add_neg_eq_pfs t
end
/-! #### The main method -/
/--
`prove_false_by_linarith` is the main workhorse of `linarith`.
Given a list `l` of proofs of `tᵢ Rᵢ 0`,
it tries to derive a contradiction from `l` and use this to produce a proof of `false`.
An oracle is used to search for a certificate of unsatisfiability.
In the current implementation, this is the Fourier Motzkin elimination routine in
`elimination.lean`, but other oracles could easily be swapped in.
The returned certificate is a map `m` from hypothesis indices to natural number coefficients.
If our set of hypotheses has the form `{tᵢ Rᵢ 0}`,
then the elimination process should have guaranteed that
1.\ `∑ (m i)*tᵢ = 0`,
with at least one `i` such that `m i > 0` and `Rᵢ` is `<`.
We have also that
2.\ `∑ (m i)*tᵢ < 0`,
since for each `i`, `(m i)*tᵢ ≤ 0` and at least one is strictly negative.
So we conclude a contradiction `0 < 0`.
It remains to produce proofs of (1) and (2). (1) is verified by calling the `discharger` tactic
of the `linarith_config` object, which is typically `ring`. We prove (2) by folding over the
set of hypotheses.
-/
meta def prove_false_by_linarith (cfg : linarith_config) : list expr → tactic expr
| [] := fail "no args to linarith"
| l@(h::t) := do
-- for the elimination to work properly, we must add a proof of `-1 < 0` to the list,
-- along with negated equality proofs.
l' ← add_neg_eq_pfs l,
hz ← ineq_prf_tp h >>= mk_neg_one_lt_zero_pf,
let inputs := hz::l',
-- perform the elimination and fail if no contradiction is found.
(comps, max_var) ← linear_forms_and_max_var cfg.transparency inputs,
certificate ← cfg.oracle.get_or_else fourier_motzkin.produce_certificate comps max_var
<|> fail "linarith failed to find a contradiction",
linarith_trace "linarith has found a contradiction",
let enum_inputs := inputs.enum,
-- construct a list pairing nonzero coeffs with the proof of their corresponding comparison
let zip := enum_inputs.filter_map $ λ ⟨n, e⟩, prod.mk e <$> certificate.find n,
mls ← zip.mmap (λ ⟨e, n⟩, do e ← term_of_ineq_prf e, return (mul_expr n e)),
-- `sm` is the sum of input terms, scaled to cancel out all variables.
sm ← to_expr $ add_exprs mls,
pformat! "The expression\n {sm}\nshould be both 0 and negative" >>= linarith_trace,
-- we prove that `sm = 0`, typically with `ring`.
sm_eq_zero ← prove_eq_zero_using cfg.discharger sm,
linarith_trace "We have proved that it is zero",
-- we also prove that `sm < 0`.
sm_lt_zero ← mk_lt_zero_pf zip,
linarith_trace "We have proved that it is negative",
-- this is a contradiction.
pftp ← infer_type sm_lt_zero,
(_, nep, _) ← rewrite_core sm_eq_zero pftp,
pf' ← mk_eq_mp nep sm_lt_zero,
mk_app `lt_irrefl [pf']
end linarith
|
96b17a88630c3cc925b9fe93e619f827cdc50d27 | 0845ae2ca02071debcfd4ac24be871236c01784f | /library/init/lean/elaborator/default.lean | 1f64ddcd051dfaefb25abd0e69cedb97ae215458 | [
"Apache-2.0"
] | permissive | GaloisInc/lean4 | 74c267eb0e900bfaa23df8de86039483ecbd60b7 | 228ddd5fdcd98dd4e9c009f425284e86917938aa | refs/heads/master | 1,643,131,356,301 | 1,562,715,572,000 | 1,562,715,572,000 | 192,390,898 | 0 | 0 | null | 1,560,792,750,000 | 1,560,792,749,000 | null | UTF-8 | Lean | false | false | 218 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.lean.elaborator.elabstrategyattrs
|
d59b95045747cf2afeee70d2bd06ef7a5556ef2c | 36c7a18fd72e5b57229bd8ba36493daf536a19ce | /library/data/list/set.lean | 6d45bd3de1be8ebab9d6e1db3debe5f696093c87 | [
"Apache-2.0"
] | permissive | YHVHvx/lean | 732bf0fb7a298cd7fe0f15d82f8e248c11db49e9 | 038369533e0136dd395dc252084d3c1853accbf2 | refs/heads/master | 1,610,701,080,210 | 1,449,128,595,000 | 1,449,128,595,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 37,089 | lean | /-
Copyright (c) 2015 Leonardo de Moura. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
Set-like operations on lists
-/
import data.list.basic data.list.comb
open nat function decidable helper_tactics eq.ops
namespace list
section erase
variable {A : Type}
variable [H : decidable_eq A]
include H
definition erase (a : A) : list A → list A
| [] := []
| (b::l) :=
match H a b with
| inl e := l
| inr n := b :: erase l
end
lemma erase_nil (a : A) : erase a [] = [] :=
rfl
lemma erase_cons_head (a : A) (l : list A) : erase a (a :: l) = l :=
show match H a a with | inl e := l | inr n := a :: erase a l end = l,
by rewrite decidable_eq_inl_refl
lemma erase_cons_tail {a b : A} (l : list A) : a ≠ b → erase a (b::l) = b :: erase a l :=
assume h : a ≠ b,
show match H a b with | inl e := l | inr n₁ := b :: erase a l end = b :: erase a l,
by rewrite (decidable_eq_inr_neg h)
lemma length_erase_of_mem {a : A} : ∀ {l}, a ∈ l → length (erase a l) = pred (length l)
| [] h := rfl
| [x] h := by rewrite [mem_singleton h, erase_cons_head]
| (x::y::xs) h :=
by_cases
(suppose a = x, by rewrite [this, erase_cons_head])
(suppose a ≠ x,
assert ainyxs : a ∈ y::xs, from or_resolve_right h this,
by rewrite [erase_cons_tail _ this, *length_cons, length_erase_of_mem ainyxs])
lemma length_erase_of_not_mem {a : A} : ∀ {l}, a ∉ l → length (erase a l) = length l
| [] h := rfl
| (x::xs) h :=
assert anex : a ≠ x, from λ aeqx : a = x, absurd (or.inl aeqx) h,
assert aninxs : a ∉ xs, from λ ainxs : a ∈ xs, absurd (or.inr ainxs) h,
by rewrite [erase_cons_tail _ anex, length_cons, length_erase_of_not_mem aninxs]
lemma erase_append_left {a : A} : ∀ {l₁} (l₂), a ∈ l₁ → erase a (l₁++l₂) = erase a l₁ ++ l₂
| [] l₂ h := absurd h !not_mem_nil
| (x::xs) l₂ h :=
by_cases
(λ aeqx : a = x, by rewrite [aeqx, append_cons, *erase_cons_head])
(λ anex : a ≠ x,
assert ainxs : a ∈ xs, from mem_of_ne_of_mem anex h,
by rewrite [append_cons, *erase_cons_tail _ anex, erase_append_left l₂ ainxs])
lemma erase_append_right {a : A} : ∀ {l₁} (l₂), a ∉ l₁ → erase a (l₁++l₂) = l₁ ++ erase a l₂
| [] l₂ h := rfl
| (x::xs) l₂ h :=
by_cases
(λ aeqx : a = x, by rewrite aeqx at h; exact (absurd !mem_cons h))
(λ anex : a ≠ x,
assert nainxs : a ∉ xs, from not_mem_of_not_mem_cons h,
by rewrite [append_cons, *erase_cons_tail _ anex, erase_append_right l₂ nainxs])
lemma erase_sub (a : A) : ∀ l, erase a l ⊆ l
| [] := λ x xine, xine
| (x::xs) := λ y xine,
by_cases
(λ aeqx : a = x, by rewrite [aeqx at xine, erase_cons_head at xine]; exact (or.inr xine))
(λ anex : a ≠ x,
assert yinxe : y ∈ x :: erase a xs, by rewrite [erase_cons_tail _ anex at xine]; exact xine,
assert subxs : erase a xs ⊆ xs, from erase_sub xs,
by_cases
(λ yeqx : y = x, by rewrite yeqx; apply mem_cons)
(λ ynex : y ≠ x,
assert yine : y ∈ erase a xs, from mem_of_ne_of_mem ynex yinxe,
assert yinxs : y ∈ xs, from subxs yine,
or.inr yinxs))
theorem mem_erase_of_ne_of_mem {a b : A} : ∀ {l : list A}, a ≠ b → a ∈ l → a ∈ erase b l
| [] n i := absurd i !not_mem_nil
| (c::l) n i := by_cases
(λ beqc : b = c,
assert ainl : a ∈ l, from or.elim (eq_or_mem_of_mem_cons i)
(λ aeqc : a = c, absurd aeqc (beqc ▸ n))
(λ ainl : a ∈ l, ainl),
by rewrite [beqc, erase_cons_head]; exact ainl)
(λ bnec : b ≠ c, by_cases
(λ aeqc : a = c,
assert aux : a ∈ c :: erase b l, by rewrite [aeqc]; exact !mem_cons,
by rewrite [erase_cons_tail _ bnec]; exact aux)
(λ anec : a ≠ c,
have ainl : a ∈ l, from mem_of_ne_of_mem anec i,
have ainel : a ∈ erase b l, from mem_erase_of_ne_of_mem n ainl,
assert aux : a ∈ c :: erase b l, from mem_cons_of_mem _ ainel,
by rewrite [erase_cons_tail _ bnec]; exact aux)) --
theorem mem_of_mem_erase {a b : A} : ∀ {l}, a ∈ erase b l → a ∈ l
| [] i := absurd i !not_mem_nil
| (c::l) i := by_cases
(λ beqc : b = c, by rewrite [beqc at i, erase_cons_head at i]; exact (mem_cons_of_mem _ i))
(λ bnec : b ≠ c,
have i₁ : a ∈ c :: erase b l, by rewrite [erase_cons_tail _ bnec at i]; exact i,
or.elim (eq_or_mem_of_mem_cons i₁)
(λ aeqc : a = c, by rewrite [aeqc]; exact !mem_cons)
(λ ainel : a ∈ erase b l,
have ainl : a ∈ l, from mem_of_mem_erase ainel,
mem_cons_of_mem _ ainl))
theorem all_erase_of_all {p : A → Prop} (a : A) : ∀ {l}, all l p → all (erase a l) p
| [] h := by rewrite [erase_nil]; exact h
| (b::l) h :=
assert h₁ : all l p, from all_of_all_cons h,
have h₂ : all (erase a l) p, from all_erase_of_all h₁,
have pb : p b, from of_all_cons h,
assert h₃ : all (b :: erase a l) p, from all_cons_of_all pb h₂,
by_cases
(λ aeqb : a = b, by rewrite [aeqb, erase_cons_head]; exact h₁)
(λ aneb : a ≠ b, by rewrite [erase_cons_tail _ aneb]; exact h₃)
end erase
/- disjoint -/
section disjoint
variable {A : Type}
definition disjoint (l₁ l₂ : list A) : Prop := ∀ ⦃a⦄, (a ∈ l₁ → a ∈ l₂ → false)
lemma disjoint_left {l₁ l₂ : list A} : disjoint l₁ l₂ → ∀ {a}, a ∈ l₁ → a ∉ l₂ :=
λ d a, d a
lemma disjoint_right {l₁ l₂ : list A} : disjoint l₁ l₂ → ∀ {a}, a ∈ l₂ → a ∉ l₁ :=
λ d a i₂ i₁, d a i₁ i₂
lemma disjoint.comm {l₁ l₂ : list A} : disjoint l₁ l₂ → disjoint l₂ l₁ :=
λ d a i₂ i₁, d a i₁ i₂
lemma disjoint_of_disjoint_cons_left {a : A} {l₁ l₂} : disjoint (a::l₁) l₂ → disjoint l₁ l₂ :=
λ d x xinl₁, disjoint_left d (or.inr xinl₁)
lemma disjoint_of_disjoint_cons_right {a : A} {l₁ l₂} : disjoint l₁ (a::l₂) → disjoint l₁ l₂ :=
λ d, disjoint.comm (disjoint_of_disjoint_cons_left (disjoint.comm d))
lemma disjoint_nil_left (l : list A) : disjoint [] l :=
λ a ab, absurd ab !not_mem_nil
lemma disjoint_nil_right (l : list A) : disjoint l [] :=
disjoint.comm (disjoint_nil_left l)
lemma disjoint_cons_of_not_mem_of_disjoint {a : A} {l₁ l₂} : a ∉ l₂ → disjoint l₁ l₂ → disjoint (a::l₁) l₂ :=
λ nainl₂ d x (xinal₁ : x ∈ a::l₁),
or.elim (eq_or_mem_of_mem_cons xinal₁)
(λ xeqa : x = a, xeqa⁻¹ ▸ nainl₂)
(λ xinl₁ : x ∈ l₁, disjoint_left d xinl₁)
lemma disjoint_of_disjoint_append_left_left : ∀ {l₁ l₂ l : list A}, disjoint (l₁++l₂) l → disjoint l₁ l
| [] l₂ l d := disjoint_nil_left l
| (x::xs) l₂ l d :=
have nxinl : x ∉ l, from disjoint_left d !mem_cons,
have d₁ : disjoint (xs++l₂) l, from disjoint_of_disjoint_cons_left d,
have d₂ : disjoint xs l, from disjoint_of_disjoint_append_left_left d₁,
disjoint_cons_of_not_mem_of_disjoint nxinl d₂
lemma disjoint_of_disjoint_append_left_right : ∀ {l₁ l₂ l : list A}, disjoint (l₁++l₂) l → disjoint l₂ l
| [] l₂ l d := d
| (x::xs) l₂ l d :=
have d₁ : disjoint (xs++l₂) l, from disjoint_of_disjoint_cons_left d,
disjoint_of_disjoint_append_left_right d₁
lemma disjoint_of_disjoint_append_right_left : ∀ {l₁ l₂ l : list A}, disjoint l (l₁++l₂) → disjoint l l₁ :=
λ l₁ l₂ l d, disjoint.comm (disjoint_of_disjoint_append_left_left (disjoint.comm d))
lemma disjoint_of_disjoint_append_right_right : ∀ {l₁ l₂ l : list A}, disjoint l (l₁++l₂) → disjoint l l₂ :=
λ l₁ l₂ l d, disjoint.comm (disjoint_of_disjoint_append_left_right (disjoint.comm d))
end disjoint
/- no duplicates predicate -/
inductive nodup {A : Type} : list A → Prop :=
| ndnil : nodup []
| ndcons : ∀ {a l}, a ∉ l → nodup l → nodup (a::l)
section nodup
open nodup
variables {A B : Type}
theorem nodup_nil : @nodup A [] :=
ndnil
theorem nodup_cons {a : A} {l : list A} : a ∉ l → nodup l → nodup (a::l) :=
λ i n, ndcons i n
theorem nodup_singleton (a : A) : nodup [a] :=
nodup_cons !not_mem_nil nodup_nil
theorem nodup_of_nodup_cons : ∀ {a : A} {l : list A}, nodup (a::l) → nodup l
| a xs (ndcons i n) := n
theorem not_mem_of_nodup_cons : ∀ {a : A} {l : list A}, nodup (a::l) → a ∉ l
| a xs (ndcons i n) := i
theorem not_nodup_cons_of_mem {a : A} {l : list A} : a ∈ l → ¬ nodup (a :: l) :=
λ ainl d, absurd ainl (not_mem_of_nodup_cons d)
theorem not_nodup_cons_of_not_nodup {a : A} {l : list A} : ¬ nodup l → ¬ nodup (a :: l) :=
λ nd d, absurd (nodup_of_nodup_cons d) nd
theorem nodup_of_nodup_append_left : ∀ {l₁ l₂ : list A}, nodup (l₁++l₂) → nodup l₁
| [] l₂ n := nodup_nil
| (x::xs) l₂ n :=
have ndxs : nodup xs, from nodup_of_nodup_append_left (nodup_of_nodup_cons n),
have nxinxsl₂ : x ∉ xs++l₂, from not_mem_of_nodup_cons n,
have nxinxs : x ∉ xs, from not_mem_of_not_mem_append_left nxinxsl₂,
nodup_cons nxinxs ndxs
theorem nodup_of_nodup_append_right : ∀ {l₁ l₂ : list A}, nodup (l₁++l₂) → nodup l₂
| [] l₂ n := n
| (x::xs) l₂ n := nodup_of_nodup_append_right (nodup_of_nodup_cons n)
theorem disjoint_of_nodup_append : ∀ {l₁ l₂ : list A}, nodup (l₁++l₂) → disjoint l₁ l₂
| [] l₂ d := disjoint_nil_left l₂
| (x::xs) l₂ d :=
have nodup (x::(xs++l₂)), from d,
have x ∉ xs++l₂, from not_mem_of_nodup_cons this,
have nxinl₂ : x ∉ l₂, from not_mem_of_not_mem_append_right this,
take a, suppose a ∈ x::xs,
or.elim (eq_or_mem_of_mem_cons this)
(suppose a = x, this⁻¹ ▸ nxinl₂)
(suppose ainxs : a ∈ xs,
have nodup (x::(xs++l₂)), from d,
have nodup (xs++l₂), from nodup_of_nodup_cons this,
have disjoint xs l₂, from disjoint_of_nodup_append this,
disjoint_left this ainxs)
theorem nodup_append_of_nodup_of_nodup_of_disjoint : ∀ {l₁ l₂ : list A}, nodup l₁ → nodup l₂ → disjoint l₁ l₂ → nodup (l₁++l₂)
| [] l₂ d₁ d₂ dsj := by rewrite [append_nil_left]; exact d₂
| (x::xs) l₂ d₁ d₂ dsj :=
have ndxs : nodup xs, from nodup_of_nodup_cons d₁,
have disjoint xs l₂, from disjoint_of_disjoint_cons_left dsj,
have ndxsl₂ : nodup (xs++l₂), from nodup_append_of_nodup_of_nodup_of_disjoint ndxs d₂ this,
have nxinxs : x ∉ xs, from not_mem_of_nodup_cons d₁,
have x ∉ l₂, from disjoint_left dsj !mem_cons,
have x ∉ xs++l₂, from not_mem_append nxinxs this,
nodup_cons this ndxsl₂
theorem nodup_app_comm {l₁ l₂ : list A} (d : nodup (l₁++l₂)) : nodup (l₂++l₁) :=
have d₁ : nodup l₁, from nodup_of_nodup_append_left d,
have d₂ : nodup l₂, from nodup_of_nodup_append_right d,
have dsj : disjoint l₁ l₂, from disjoint_of_nodup_append d,
nodup_append_of_nodup_of_nodup_of_disjoint d₂ d₁ (disjoint.comm dsj)
theorem nodup_head {a : A} {l₁ l₂ : list A} (d : nodup (l₁++(a::l₂))) : nodup (a::(l₁++l₂)) :=
have d₁ : nodup (a::(l₂++l₁)), from nodup_app_comm d,
have d₂ : nodup (l₂++l₁), from nodup_of_nodup_cons d₁,
have d₃ : nodup (l₁++l₂), from nodup_app_comm d₂,
have nain : a ∉ l₂++l₁, from not_mem_of_nodup_cons d₁,
have nain₂ : a ∉ l₂, from not_mem_of_not_mem_append_left nain,
have nain₁ : a ∉ l₁, from not_mem_of_not_mem_append_right nain,
nodup_cons (not_mem_append nain₁ nain₂) d₃
theorem nodup_middle {a : A} {l₁ l₂ : list A} (d : nodup (a::(l₁++l₂))) : nodup (l₁++(a::l₂)) :=
have d₁ : nodup (l₁++l₂), from nodup_of_nodup_cons d,
have nain : a ∉ l₁++l₂, from not_mem_of_nodup_cons d,
have disj : disjoint l₁ l₂, from disjoint_of_nodup_append d₁,
have d₂ : nodup l₁, from nodup_of_nodup_append_left d₁,
have d₃ : nodup l₂, from nodup_of_nodup_append_right d₁,
have nain₂ : a ∉ l₂, from not_mem_of_not_mem_append_right nain,
have nain₁ : a ∉ l₁, from not_mem_of_not_mem_append_left nain,
have d₄ : nodup (a::l₂), from nodup_cons nain₂ d₃,
have disj₂ : disjoint l₁ (a::l₂), from disjoint.comm (disjoint_cons_of_not_mem_of_disjoint nain₁ (disjoint.comm disj)),
nodup_append_of_nodup_of_nodup_of_disjoint d₂ d₄ disj₂
theorem nodup_map {f : A → B} (inj : injective f) : ∀ {l : list A}, nodup l → nodup (map f l)
| [] n := begin rewrite [map_nil], apply nodup_nil end
| (x::xs) n :=
assert nxinxs : x ∉ xs, from not_mem_of_nodup_cons n,
assert ndxs : nodup xs, from nodup_of_nodup_cons n,
assert ndmfxs : nodup (map f xs), from nodup_map ndxs,
assert nfxinm : f x ∉ map f xs, from
λ ab : f x ∈ map f xs,
obtain (y : A) (yinxs : y ∈ xs) (fyfx : f y = f x), from exists_of_mem_map ab,
assert yeqx : y = x, from inj fyfx,
by subst y; contradiction,
nodup_cons nfxinm ndmfxs
theorem nodup_erase_of_nodup [h : decidable_eq A] (a : A) : ∀ {l}, nodup l → nodup (erase a l)
| [] n := nodup_nil
| (b::l) n := by_cases
(λ aeqb : a = b, by rewrite [aeqb, erase_cons_head]; exact (nodup_of_nodup_cons n))
(λ aneb : a ≠ b,
have nbinl : b ∉ l, from not_mem_of_nodup_cons n,
have ndl : nodup l, from nodup_of_nodup_cons n,
have ndeal : nodup (erase a l), from nodup_erase_of_nodup ndl,
have nbineal : b ∉ erase a l, from λ i, absurd (erase_sub _ _ i) nbinl,
assert aux : nodup (b :: erase a l), from nodup_cons nbineal ndeal,
by rewrite [erase_cons_tail _ aneb]; exact aux)
theorem mem_erase_of_nodup [h : decidable_eq A] (a : A) : ∀ {l}, nodup l → a ∉ erase a l
| [] n := !not_mem_nil
| (b::l) n :=
have ndl : nodup l, from nodup_of_nodup_cons n,
have naineal : a ∉ erase a l, from mem_erase_of_nodup ndl,
assert nbinl : b ∉ l, from not_mem_of_nodup_cons n,
by_cases
(λ aeqb : a = b, by rewrite [aeqb, erase_cons_head]; exact nbinl)
(λ aneb : a ≠ b,
assert aux : a ∉ b :: erase a l, from
assume ainbeal : a ∈ b :: erase a l, or.elim (eq_or_mem_of_mem_cons ainbeal)
(λ aeqb : a = b, absurd aeqb aneb)
(λ aineal : a ∈ erase a l, absurd aineal naineal),
by rewrite [erase_cons_tail _ aneb]; exact aux)
definition erase_dup [H : decidable_eq A] : list A → list A
| [] := []
| (x :: xs) := if x ∈ xs then erase_dup xs else x :: erase_dup xs
theorem erase_dup_nil [H : decidable_eq A] : erase_dup [] = ([] : list A)
theorem erase_dup_cons_of_mem [H : decidable_eq A] {a : A} {l : list A} : a ∈ l → erase_dup (a::l) = erase_dup l :=
assume ainl, calc
erase_dup (a::l) = if a ∈ l then erase_dup l else a :: erase_dup l : rfl
... = erase_dup l : if_pos ainl
theorem erase_dup_cons_of_not_mem [H : decidable_eq A] {a : A} {l : list A} : a ∉ l → erase_dup (a::l) = a :: erase_dup l :=
assume nainl, calc
erase_dup (a::l) = if a ∈ l then erase_dup l else a :: erase_dup l : rfl
... = a :: erase_dup l : if_neg nainl
theorem mem_erase_dup [H : decidable_eq A] {a : A} : ∀ {l}, a ∈ l → a ∈ erase_dup l
| [] h := absurd h !not_mem_nil
| (b::l) h := by_cases
(λ binl : b ∈ l, or.elim (eq_or_mem_of_mem_cons h)
(λ aeqb : a = b, by rewrite [erase_dup_cons_of_mem binl, -aeqb at binl]; exact (mem_erase_dup binl))
(λ ainl : a ∈ l, by rewrite [erase_dup_cons_of_mem binl]; exact (mem_erase_dup ainl)))
(λ nbinl : b ∉ l, or.elim (eq_or_mem_of_mem_cons h)
(λ aeqb : a = b, by rewrite [erase_dup_cons_of_not_mem nbinl, aeqb]; exact !mem_cons)
(λ ainl : a ∈ l, by rewrite [erase_dup_cons_of_not_mem nbinl]; exact (or.inr (mem_erase_dup ainl))))
theorem mem_of_mem_erase_dup [H : decidable_eq A] {a : A} : ∀ {l}, a ∈ erase_dup l → a ∈ l
| [] h := by rewrite [erase_dup_nil at h]; exact h
| (b::l) h := by_cases
(λ binl : b ∈ l,
have h₁ : a ∈ erase_dup l, by rewrite [erase_dup_cons_of_mem binl at h]; exact h,
or.inr (mem_of_mem_erase_dup h₁))
(λ nbinl : b ∉ l,
have h₁ : a ∈ b :: erase_dup l, by rewrite [erase_dup_cons_of_not_mem nbinl at h]; exact h,
or.elim (eq_or_mem_of_mem_cons h₁)
(λ aeqb : a = b, by rewrite aeqb; exact !mem_cons)
(λ ainel : a ∈ erase_dup l, or.inr (mem_of_mem_erase_dup ainel)))
theorem erase_dup_sub [H : decidable_eq A] (l : list A) : erase_dup l ⊆ l :=
λ a i, mem_of_mem_erase_dup i
theorem sub_erase_dup [H : decidable_eq A] (l : list A) : l ⊆ erase_dup l :=
λ a i, mem_erase_dup i
theorem nodup_erase_dup [H : decidable_eq A] : ∀ l : list A, nodup (erase_dup l)
| [] := by rewrite erase_dup_nil; exact nodup_nil
| (a::l) := by_cases
(λ ainl : a ∈ l, by rewrite [erase_dup_cons_of_mem ainl]; exact (nodup_erase_dup l))
(λ nainl : a ∉ l,
assert r : nodup (erase_dup l), from nodup_erase_dup l,
assert nin : a ∉ erase_dup l, from
assume ab : a ∈ erase_dup l, absurd (mem_of_mem_erase_dup ab) nainl,
by rewrite [erase_dup_cons_of_not_mem nainl]; exact (nodup_cons nin r))
theorem erase_dup_eq_of_nodup [H : decidable_eq A] : ∀ {l : list A}, nodup l → erase_dup l = l
| [] d := rfl
| (a::l) d :=
assert nainl : a ∉ l, from not_mem_of_nodup_cons d,
assert dl : nodup l, from nodup_of_nodup_cons d,
by rewrite [erase_dup_cons_of_not_mem nainl, erase_dup_eq_of_nodup dl]
definition decidable_nodup [instance] [h : decidable_eq A] : ∀ (l : list A), decidable (nodup l)
| [] := inl nodup_nil
| (a::l) :=
match decidable_mem a l with
| inl p := inr (not_nodup_cons_of_mem p)
| inr n :=
match decidable_nodup l with
| inl nd := inl (nodup_cons n nd)
| inr d := inr (not_nodup_cons_of_not_nodup d)
end
end
theorem nodup_product : ∀ {l₁ : list A} {l₂ : list B}, nodup l₁ → nodup l₂ → nodup (product l₁ l₂)
| [] l₂ n₁ n₂ := nodup_nil
| (a::l₁) l₂ n₁ n₂ :=
have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons n₁,
have n₃ : nodup l₁, from nodup_of_nodup_cons n₁,
have n₄ : nodup (product l₁ l₂), from nodup_product n₃ n₂,
have dgen : ∀ l, nodup l → nodup (map (λ b, (a, b)) l)
| [] h := nodup_nil
| (x::l) h :=
have dl : nodup l, from nodup_of_nodup_cons h,
have dm : nodup (map (λ b, (a, b)) l), from dgen l dl,
have nxin : x ∉ l, from not_mem_of_nodup_cons h,
have npin : (a, x) ∉ map (λ b, (a, b)) l, from
assume pin, absurd (mem_of_mem_map_pair₁ pin) nxin,
nodup_cons npin dm,
have dm : nodup (map (λ b, (a, b)) l₂), from dgen l₂ n₂,
have dsj : disjoint (map (λ b, (a, b)) l₂) (product l₁ l₂), from
λ p, match p with
| (a₁, b₁) :=
λ (i₁ : (a₁, b₁) ∈ map (λ b, (a, b)) l₂) (i₂ : (a₁, b₁) ∈ product l₁ l₂),
have a₁inl₁ : a₁ ∈ l₁, from mem_of_mem_product_left i₂,
have a₁eqa : a₁ = a, from eq_of_mem_map_pair₁ i₁,
absurd (a₁eqa ▸ a₁inl₁) nainl₁
end,
nodup_append_of_nodup_of_nodup_of_disjoint dm n₄ dsj
theorem nodup_filter (p : A → Prop) [h : decidable_pred p] : ∀ {l : list A}, nodup l → nodup (filter p l)
| [] nd := nodup_nil
| (a::l) nd :=
have nainl : a ∉ l, from not_mem_of_nodup_cons nd,
have ndl : nodup l, from nodup_of_nodup_cons nd,
assert ndf : nodup (filter p l), from nodup_filter ndl,
assert nainf : a ∉ filter p l, from
assume ainf, absurd (mem_of_mem_filter ainf) nainl,
by_cases
(λ pa : p a, by rewrite [filter_cons_of_pos _ pa]; exact (nodup_cons nainf ndf))
(λ npa : ¬ p a, by rewrite [filter_cons_of_neg _ npa]; exact ndf)
lemma dmap_nodup_of_dinj {p : A → Prop} [h : decidable_pred p] {f : Π a, p a → B} (Pdi : dinj p f):
∀ {l : list A}, nodup l → nodup (dmap p f l)
| [] := take P, nodup.ndnil
| (a::l) := take Pnodup,
decidable.rec_on (h a)
(λ Pa,
begin
rewrite [dmap_cons_of_pos Pa],
apply nodup_cons,
apply (not_mem_dmap_of_dinj_of_not_mem Pdi Pa),
exact not_mem_of_nodup_cons Pnodup,
exact dmap_nodup_of_dinj (nodup_of_nodup_cons Pnodup)
end)
(λ nPa,
begin
rewrite [dmap_cons_of_neg nPa],
exact dmap_nodup_of_dinj (nodup_of_nodup_cons Pnodup)
end)
end nodup
/- upto -/
definition upto : nat → list nat
| 0 := []
| (n+1) := n :: upto n
theorem upto_nil : upto 0 = nil
theorem upto_succ (n : nat) : upto (succ n) = n :: upto n
theorem length_upto : ∀ n, length (upto n) = n
| 0 := rfl
| (succ n) := by rewrite [upto_succ, length_cons, length_upto]
theorem upto_less : ∀ n, all (upto n) (λ i, i < n)
| 0 := trivial
| (succ n) :=
have alln : all (upto n) (λ i, i < n), from upto_less n,
all_cons_of_all (lt.base n) (all_implies alln (λ x h, lt.step h))
theorem nodup_upto : ∀ n, nodup (upto n)
| 0 := nodup_nil
| (n+1) :=
have d : nodup (upto n), from nodup_upto n,
have n : n ∉ upto n, from
assume i : n ∈ upto n, absurd (of_mem_of_all i (upto_less n)) (nat.lt_irrefl n),
nodup_cons n d
theorem lt_of_mem_upto {n i : nat} : i ∈ upto n → i < n :=
assume i, of_mem_of_all i (upto_less n)
theorem mem_upto_succ_of_mem_upto {n i : nat} : i ∈ upto n → i ∈ upto (succ n) :=
assume i, mem_cons_of_mem _ i
theorem mem_upto_of_lt : ∀ {n i : nat}, i < n → i ∈ upto n
| 0 i h := absurd h !not_lt_zero
| (succ n) i h :=
begin
cases h with m h',
{ rewrite upto_succ, apply mem_cons},
{ exact mem_upto_succ_of_mem_upto (mem_upto_of_lt h')}
end
lemma upto_step : ∀ {n : nat}, upto (succ n) = (map succ (upto n))++[0]
| 0 := rfl
| (succ n) := begin rewrite [upto_succ n, map_cons, append_cons, -upto_step] end
/- union -/
section union
variable {A : Type}
variable [H : decidable_eq A]
include H
definition union : list A → list A → list A
| [] l₂ := l₂
| (a::l₁) l₂ := if a ∈ l₂ then union l₁ l₂ else a :: union l₁ l₂
theorem nil_union (l : list A) : union [] l = l
theorem union_cons_of_mem {a : A} {l₂} : ∀ (l₁), a ∈ l₂ → union (a::l₁) l₂ = union l₁ l₂ :=
take l₁, assume ainl₂, calc
union (a::l₁) l₂ = if a ∈ l₂ then union l₁ l₂ else a :: union l₁ l₂ : rfl
... = union l₁ l₂ : if_pos ainl₂
theorem union_cons_of_not_mem {a : A} {l₂} : ∀ (l₁), a ∉ l₂ → union (a::l₁) l₂ = a :: union l₁ l₂ :=
take l₁, assume nainl₂, calc
union (a::l₁) l₂ = if a ∈ l₂ then union l₁ l₂ else a :: union l₁ l₂ : rfl
... = a :: union l₁ l₂ : if_neg nainl₂
theorem union_nil : ∀ (l : list A), union l [] = l
| [] := !nil_union
| (a::l) := by rewrite [union_cons_of_not_mem _ !not_mem_nil, union_nil]
theorem mem_or_mem_of_mem_union : ∀ {l₁ l₂} {a : A}, a ∈ union l₁ l₂ → a ∈ l₁ ∨ a ∈ l₂
| [] l₂ a ainl₂ := by rewrite nil_union at ainl₂; exact (or.inr (ainl₂))
| (b::l₁) l₂ a ainbl₁l₂ := by_cases
(λ binl₂ : b ∈ l₂,
have ainl₁l₂ : a ∈ union l₁ l₂, by rewrite [union_cons_of_mem l₁ binl₂ at ainbl₁l₂]; exact ainbl₁l₂,
or.elim (mem_or_mem_of_mem_union ainl₁l₂)
(λ ainl₁, or.inl (mem_cons_of_mem _ ainl₁))
(λ ainl₂, or.inr ainl₂))
(λ nbinl₂ : b ∉ l₂,
have ainb_l₁l₂ : a ∈ b :: union l₁ l₂, by rewrite [union_cons_of_not_mem l₁ nbinl₂ at ainbl₁l₂]; exact ainbl₁l₂,
or.elim (eq_or_mem_of_mem_cons ainb_l₁l₂)
(λ aeqb, by rewrite aeqb; exact (or.inl !mem_cons))
(λ ainl₁l₂,
or.elim (mem_or_mem_of_mem_union ainl₁l₂)
(λ ainl₁, or.inl (mem_cons_of_mem _ ainl₁))
(λ ainl₂, or.inr ainl₂)))
theorem mem_union_right {a : A} : ∀ (l₁) {l₂}, a ∈ l₂ → a ∈ union l₁ l₂
| [] l₂ h := by rewrite nil_union; exact h
| (b::l₁) l₂ h := by_cases
(λ binl₂ : b ∈ l₂, by rewrite [union_cons_of_mem _ binl₂]; exact (mem_union_right _ h))
(λ nbinl₂ : b ∉ l₂, by rewrite [union_cons_of_not_mem _ nbinl₂]; exact (mem_cons_of_mem _ (mem_union_right _ h)))
theorem mem_union_left {a : A} : ∀ {l₁} (l₂), a ∈ l₁ → a ∈ union l₁ l₂
| [] l₂ h := absurd h !not_mem_nil
| (b::l₁) l₂ h := by_cases
(λ binl₂ : b ∈ l₂, or.elim (eq_or_mem_of_mem_cons h)
(λ aeqb : a = b,
by rewrite [union_cons_of_mem l₁ binl₂, -aeqb at binl₂]; exact (mem_union_right _ binl₂))
(λ ainl₁ : a ∈ l₁,
by rewrite [union_cons_of_mem l₁ binl₂]; exact (mem_union_left _ ainl₁)))
(λ nbinl₂ : b ∉ l₂, or.elim (eq_or_mem_of_mem_cons h)
(λ aeqb : a = b,
by rewrite [union_cons_of_not_mem l₁ nbinl₂, aeqb]; exact !mem_cons)
(λ ainl₁ : a ∈ l₁,
by rewrite [union_cons_of_not_mem l₁ nbinl₂]; exact (mem_cons_of_mem _ (mem_union_left _ ainl₁))))
theorem mem_union_cons (a : A) (l₁ : list A) (l₂ : list A) : a ∈ union (a::l₁) l₂ :=
by_cases
(λ ainl₂ : a ∈ l₂, mem_union_right _ ainl₂)
(λ nainl₂ : a ∉ l₂, by rewrite [union_cons_of_not_mem _ nainl₂]; exact !mem_cons)
theorem nodup_union_of_nodup_of_nodup : ∀ {l₁ l₂ : list A}, nodup l₁ → nodup l₂ → nodup (union l₁ l₂)
| [] l₂ n₁ nl₂ := by rewrite nil_union; exact nl₂
| (a::l₁) l₂ nal₁ nl₂ :=
assert nl₁ : nodup l₁, from nodup_of_nodup_cons nal₁,
assert nl₁l₂ : nodup (union l₁ l₂), from nodup_union_of_nodup_of_nodup nl₁ nl₂,
by_cases
(λ ainl₂ : a ∈ l₂,
by rewrite [union_cons_of_mem l₁ ainl₂]; exact nl₁l₂)
(λ nainl₂ : a ∉ l₂,
have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons nal₁,
assert nainl₁l₂ : a ∉ union l₁ l₂, from
assume ainl₁l₂ : a ∈ union l₁ l₂, or.elim (mem_or_mem_of_mem_union ainl₁l₂)
(λ ainl₁, absurd ainl₁ nainl₁)
(λ ainl₂, absurd ainl₂ nainl₂),
by rewrite [union_cons_of_not_mem l₁ nainl₂]; exact (nodup_cons nainl₁l₂ nl₁l₂))
theorem union_eq_append : ∀ {l₁ l₂ : list A}, disjoint l₁ l₂ → union l₁ l₂ = append l₁ l₂
| [] l₂ d := rfl
| (a::l₁) l₂ d :=
assert nainl₂ : a ∉ l₂, from disjoint_left d !mem_cons,
assert d₁ : disjoint l₁ l₂, from disjoint_of_disjoint_cons_left d,
by rewrite [union_cons_of_not_mem _ nainl₂, append_cons, union_eq_append d₁]
theorem all_union {p : A → Prop} : ∀ {l₁ l₂ : list A}, all l₁ p → all l₂ p → all (union l₁ l₂) p
| [] l₂ h₁ h₂ := h₂
| (a::l₁) l₂ h₁ h₂ :=
have h₁' : all l₁ p, from all_of_all_cons h₁,
have pa : p a, from of_all_cons h₁,
assert au : all (union l₁ l₂) p, from all_union h₁' h₂,
assert au' : all (a :: union l₁ l₂) p, from all_cons_of_all pa au,
by_cases
(λ ainl₂ : a ∈ l₂, by rewrite [union_cons_of_mem _ ainl₂]; exact au)
(λ nainl₂ : a ∉ l₂, by rewrite [union_cons_of_not_mem _ nainl₂]; exact au')
theorem all_of_all_union_left {p : A → Prop} : ∀ {l₁ l₂ : list A}, all (union l₁ l₂) p → all l₁ p
| [] l₂ h := trivial
| (a::l₁) l₂ h :=
have ain : a ∈ union (a::l₁) l₂, from !mem_union_cons,
have pa : p a, from of_mem_of_all ain h,
by_cases
(λ ainl₂ : a ∈ l₂,
have al₁l₂ : all (union l₁ l₂) p, by rewrite [union_cons_of_mem _ ainl₂ at h]; exact h,
have al₁ : all l₁ p, from all_of_all_union_left al₁l₂,
all_cons_of_all pa al₁)
(λ nainl₂ : a ∉ l₂,
have aal₁l₂ : all (a::union l₁ l₂) p, by rewrite [union_cons_of_not_mem _ nainl₂ at h]; exact h,
have al₁l₂ : all (union l₁ l₂) p, from all_of_all_cons aal₁l₂,
have al₁ : all l₁ p, from all_of_all_union_left al₁l₂,
all_cons_of_all pa al₁)
theorem all_of_all_union_right {p : A → Prop} : ∀ {l₁ l₂ : list A}, all (union l₁ l₂) p → all l₂ p
| [] l₂ h := by rewrite [nil_union at h]; exact h
| (a::l₁) l₂ h := by_cases
(λ ainl₂ : a ∈ l₂, by rewrite [union_cons_of_mem _ ainl₂ at h]; exact (all_of_all_union_right h))
(λ nainl₂ : a ∉ l₂,
have h₁ : all (a :: union l₁ l₂) p, by rewrite [union_cons_of_not_mem _ nainl₂ at h]; exact h,
all_of_all_union_right (all_of_all_cons h₁))
variable {B : Type}
theorem foldl_union_of_disjoint (f : B → A → B) (b : B) {l₁ l₂ : list A} (d : disjoint l₁ l₂)
: foldl f b (union l₁ l₂) = foldl f (foldl f b l₁) l₂ :=
by rewrite [union_eq_append d, foldl_append]
theorem foldr_union_of_dijoint (f : A → B → B) (b : B) {l₁ l₂ : list A} (d : disjoint l₁ l₂)
: foldr f b (union l₁ l₂) = foldr f (foldr f b l₂) l₁ :=
by rewrite [union_eq_append d, foldr_append]
end union
/- insert -/
section insert
variable {A : Type}
variable [H : decidable_eq A]
include H
definition insert (a : A) (l : list A) : list A :=
if a ∈ l then l else a::l
theorem insert_eq_of_mem {a : A} {l : list A} : a ∈ l → insert a l = l :=
assume ainl, if_pos ainl
theorem insert_eq_of_not_mem {a : A} {l : list A} : a ∉ l → insert a l = a::l :=
assume nainl, if_neg nainl
theorem mem_insert (a : A) (l : list A) : a ∈ insert a l :=
by_cases
(λ ainl : a ∈ l, by rewrite [insert_eq_of_mem ainl]; exact ainl)
(λ nainl : a ∉ l, by rewrite [insert_eq_of_not_mem nainl]; exact !mem_cons)
theorem mem_insert_of_mem {a : A} (b : A) {l : list A} : a ∈ l → a ∈ insert b l :=
assume ainl, by_cases
(λ binl : b ∈ l, by rewrite [insert_eq_of_mem binl]; exact ainl)
(λ nbinl : b ∉ l, by rewrite [insert_eq_of_not_mem nbinl]; exact (mem_cons_of_mem _ ainl))
theorem eq_or_mem_of_mem_insert {x a : A} {l : list A} (H : x ∈ insert a l) : x = a ∨ x ∈ l :=
decidable.by_cases
(assume H3: a ∈ l, or.inr (insert_eq_of_mem H3 ▸ H))
(assume H3: a ∉ l,
have H4: x ∈ a :: l, from insert_eq_of_not_mem H3 ▸ H,
iff.mp !mem_cons_iff H4)
theorem mem_insert_iff (x a : A) (l : list A) : x ∈ insert a l ↔ x = a ∨ x ∈ l :=
iff.intro
(!eq_or_mem_of_mem_insert)
(assume H, or.elim H
(assume H' : x = a, H'⁻¹ ▸ !mem_insert)
(assume H' : x ∈ l, !mem_insert_of_mem H'))
theorem nodup_insert (a : A) {l : list A} : nodup l → nodup (insert a l) :=
assume n, by_cases
(λ ainl : a ∈ l, by rewrite [insert_eq_of_mem ainl]; exact n)
(λ nainl : a ∉ l, by rewrite [insert_eq_of_not_mem nainl]; exact (nodup_cons nainl n))
theorem length_insert_of_mem {a : A} {l : list A} : a ∈ l → length (insert a l) = length l :=
assume ainl, by rewrite [insert_eq_of_mem ainl]
theorem length_insert_of_not_mem {a : A} {l : list A} : a ∉ l → length (insert a l) = length l + 1 :=
assume nainl, by rewrite [insert_eq_of_not_mem nainl]
theorem all_insert_of_all {p : A → Prop} {a : A} {l} : p a → all l p → all (insert a l) p :=
assume h₁ h₂, by_cases
(λ ainl : a ∈ l, by rewrite [insert_eq_of_mem ainl]; exact h₂)
(λ nainl : a ∉ l, by rewrite [insert_eq_of_not_mem nainl]; exact (all_cons_of_all h₁ h₂))
end insert
/- inter -/
section inter
variable {A : Type}
variable [H : decidable_eq A]
include H
definition inter : list A → list A → list A
| [] l₂ := []
| (a::l₁) l₂ := if a ∈ l₂ then a :: inter l₁ l₂ else inter l₁ l₂
theorem inter_nil (l : list A) : inter [] l = []
theorem inter_cons_of_mem {a : A} (l₁ : list A) {l₂} : a ∈ l₂ → inter (a::l₁) l₂ = a :: inter l₁ l₂ :=
assume i, if_pos i
theorem inter_cons_of_not_mem {a : A} (l₁ : list A) {l₂} : a ∉ l₂ → inter (a::l₁) l₂ = inter l₁ l₂ :=
assume i, if_neg i
theorem mem_of_mem_inter_left : ∀ {l₁ l₂} {a : A}, a ∈ inter l₁ l₂ → a ∈ l₁
| [] l₂ a i := absurd i !not_mem_nil
| (b::l₁) l₂ a i := by_cases
(λ binl₂ : b ∈ l₂,
have aux : a ∈ b :: inter l₁ l₂, by rewrite [inter_cons_of_mem _ binl₂ at i]; exact i,
or.elim (eq_or_mem_of_mem_cons aux)
(λ aeqb : a = b, by rewrite [aeqb]; exact !mem_cons)
(λ aini, mem_cons_of_mem _ (mem_of_mem_inter_left aini)))
(λ nbinl₂ : b ∉ l₂,
have ainl₁ : a ∈ l₁, by rewrite [inter_cons_of_not_mem _ nbinl₂ at i]; exact (mem_of_mem_inter_left i),
mem_cons_of_mem _ ainl₁)
theorem mem_of_mem_inter_right : ∀ {l₁ l₂} {a : A}, a ∈ inter l₁ l₂ → a ∈ l₂
| [] l₂ a i := absurd i !not_mem_nil
| (b::l₁) l₂ a i := by_cases
(λ binl₂ : b ∈ l₂,
have aux : a ∈ b :: inter l₁ l₂, by rewrite [inter_cons_of_mem _ binl₂ at i]; exact i,
or.elim (eq_or_mem_of_mem_cons aux)
(λ aeqb : a = b, by rewrite [aeqb]; exact binl₂)
(λ aini : a ∈ inter l₁ l₂, mem_of_mem_inter_right aini))
(λ nbinl₂ : b ∉ l₂,
by rewrite [inter_cons_of_not_mem _ nbinl₂ at i]; exact (mem_of_mem_inter_right i))
theorem mem_inter_of_mem_of_mem : ∀ {l₁ l₂} {a : A}, a ∈ l₁ → a ∈ l₂ → a ∈ inter l₁ l₂
| [] l₂ a i₁ i₂ := absurd i₁ !not_mem_nil
| (b::l₁) l₂ a i₁ i₂ := by_cases
(λ binl₂ : b ∈ l₂,
or.elim (eq_or_mem_of_mem_cons i₁)
(λ aeqb : a = b,
by rewrite [inter_cons_of_mem _ binl₂, aeqb]; exact !mem_cons)
(λ ainl₁ : a ∈ l₁,
by rewrite [inter_cons_of_mem _ binl₂];
apply mem_cons_of_mem;
exact (mem_inter_of_mem_of_mem ainl₁ i₂)))
(λ nbinl₂ : b ∉ l₂,
or.elim (eq_or_mem_of_mem_cons i₁)
(λ aeqb : a = b, absurd (aeqb ▸ i₂) nbinl₂)
(λ ainl₁ : a ∈ l₁,
by rewrite [inter_cons_of_not_mem _ nbinl₂]; exact (mem_inter_of_mem_of_mem ainl₁ i₂)))
theorem nodup_inter_of_nodup : ∀ {l₁ : list A} (l₂), nodup l₁ → nodup (inter l₁ l₂)
| [] l₂ d := nodup_nil
| (a::l₁) l₂ d :=
have d₁ : nodup l₁, from nodup_of_nodup_cons d,
assert d₂ : nodup (inter l₁ l₂), from nodup_inter_of_nodup _ d₁,
have nainl₁ : a ∉ l₁, from not_mem_of_nodup_cons d,
assert naini : a ∉ inter l₁ l₂, from λ i, absurd (mem_of_mem_inter_left i) nainl₁,
by_cases
(λ ainl₂ : a ∈ l₂, by rewrite [inter_cons_of_mem _ ainl₂]; exact (nodup_cons naini d₂))
(λ nainl₂ : a ∉ l₂, by rewrite [inter_cons_of_not_mem _ nainl₂]; exact d₂)
theorem inter_eq_nil_of_disjoint : ∀ {l₁ l₂ : list A}, disjoint l₁ l₂ → inter l₁ l₂ = []
| [] l₂ d := rfl
| (a::l₁) l₂ d :=
assert aux_eq : inter l₁ l₂ = [], from inter_eq_nil_of_disjoint (disjoint_of_disjoint_cons_left d),
assert nainl₂ : a ∉ l₂, from disjoint_left d !mem_cons,
by rewrite [inter_cons_of_not_mem _ nainl₂, aux_eq]
theorem all_inter_of_all_left {p : A → Prop} : ∀ {l₁} (l₂), all l₁ p → all (inter l₁ l₂) p
| [] l₂ h := trivial
| (a::l₁) l₂ h :=
have h₁ : all l₁ p, from all_of_all_cons h,
assert h₂ : all (inter l₁ l₂) p, from all_inter_of_all_left _ h₁,
have pa : p a, from of_all_cons h,
assert h₃ : all (a :: inter l₁ l₂) p, from all_cons_of_all pa h₂,
by_cases
(λ ainl₂ : a ∈ l₂, by rewrite [inter_cons_of_mem _ ainl₂]; exact h₃)
(λ nainl₂ : a ∉ l₂, by rewrite [inter_cons_of_not_mem _ nainl₂]; exact h₂)
theorem all_inter_of_all_right {p : A → Prop} : ∀ (l₁) {l₂}, all l₂ p → all (inter l₁ l₂) p
| [] l₂ h := trivial
| (a::l₁) l₂ h :=
assert h₁ : all (inter l₁ l₂) p, from all_inter_of_all_right _ h,
by_cases
(λ ainl₂ : a ∈ l₂,
have pa : p a, from of_mem_of_all ainl₂ h,
assert h₂ : all (a :: inter l₁ l₂) p, from all_cons_of_all pa h₁,
by rewrite [inter_cons_of_mem _ ainl₂]; exact h₂)
(λ nainl₂ : a ∉ l₂, by rewrite [inter_cons_of_not_mem _ nainl₂]; exact h₁)
end inter
end list
|
769343e544fa24bb200c538a567f65d36b421295 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/set/intervals/ord_connected_auto.lean | a4fb1a83c0ea4399197b64ca463b0cec279695ae | [] | 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,337 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Yury G. Kudryashov
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.set.intervals.unordered_interval
import Mathlib.data.set.lattice
import Mathlib.PostPort
universes u_1 u_2
namespace Mathlib
/-!
# Order-connected sets
We say that a set `s : set α` is `ord_connected` if for all `x y ∈ s` it includes the
interval `[x, y]`. If `α` is a `densely_ordered` `conditionally_complete_linear_order` with
the `order_topology`, then this condition is equivalent to `is_preconnected s`. If `α = ℝ`, then
this condition is also equivalent to `convex s`.
In this file we prove that intersection of a family of `ord_connected` sets is `ord_connected` and
that all standard intervals are `ord_connected`.
-/
namespace set
/--
We say that a set `s : set α` is `ord_connected` if for all `x y ∈ s` it includes the
interval `[x, y]`. If `α` is a `densely_ordered` `conditionally_complete_linear_order` with
the `order_topology`, then this condition is equivalent to `is_preconnected s`. If `α = ℝ`, then
this condition is also equivalent to `convex s`.
-/
def ord_connected {α : Type u_1} [preorder α] (s : set α) :=
∀ {x : α}, x ∈ s → ∀ {y : α}, y ∈ s → Icc x y ⊆ s
/-- It suffices to prove `[x, y] ⊆ s` for `x y ∈ s`, `x ≤ y`. -/
theorem ord_connected_iff {α : Type u_1} [preorder α] {s : set α} :
ord_connected s ↔ ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → Icc x y ⊆ s :=
sorry
theorem ord_connected_of_Ioo {α : Type u_1} [partial_order α] {s : set α}
(hs : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x < y → Ioo x y ⊆ s) : ord_connected s :=
sorry
protected theorem Icc_subset {α : Type u_1} [preorder α] (s : set α) [hs : ord_connected s] {x : α}
{y : α} (hx : x ∈ s) (hy : y ∈ s) : Icc x y ⊆ s :=
hs hx hy
theorem ord_connected.inter {α : Type u_1} [preorder α] {s : set α} {t : set α}
(hs : ord_connected s) (ht : ord_connected t) : ord_connected (s ∩ t) :=
fun (x : α) (hx : x ∈ s ∩ t) (y : α) (hy : y ∈ s ∩ t) =>
subset_inter (hs (and.left hx) (and.left hy)) (ht (and.right hx) (and.right hy))
protected instance ord_connected.inter' {α : Type u_1} [preorder α] {s : set α} {t : set α}
[ord_connected s] [ord_connected t] : ord_connected (s ∩ t) :=
ord_connected.inter _inst_2 _inst_3
theorem ord_connected.dual {α : Type u_1} [preorder α] {s : set α} (hs : ord_connected s) :
ord_connected s :=
fun (x : order_dual α) (hx : x ∈ s) (y : order_dual α) (hy : y ∈ s) (z : order_dual α)
(hz : z ∈ Icc x y) => hs hy hx { left := and.right hz, right := and.left hz }
theorem ord_connected_dual {α : Type u_1} [preorder α] {s : set α} :
ord_connected s ↔ ord_connected s :=
{ mp := fun (h : ord_connected s) => ord_connected.dual h,
mpr := fun (h : ord_connected s) => ord_connected.dual h }
theorem ord_connected_sInter {α : Type u_1} [preorder α] {S : set (set α)}
(hS : ∀ (s : set α), s ∈ S → ord_connected s) : ord_connected (⋂₀S) :=
fun (x : α) (hx : x ∈ ⋂₀S) (y : α) (hy : y ∈ ⋂₀S) =>
subset_sInter fun (s : set α) (hs : s ∈ S) => hS s hs (hx s hs) (hy s hs)
theorem ord_connected_Inter {α : Type u_1} [preorder α] {ι : Sort u_2} {s : ι → set α}
(hs : ∀ (i : ι), ord_connected (s i)) : ord_connected (Inter fun (i : ι) => s i) :=
ord_connected_sInter (iff.mpr forall_range_iff hs)
protected instance ord_connected_Inter' {α : Type u_1} [preorder α] {ι : Sort u_2} {s : ι → set α}
[∀ (i : ι), ord_connected (s i)] : ord_connected (Inter fun (i : ι) => s i) :=
ord_connected_Inter _inst_2
theorem ord_connected_bInter {α : Type u_1} [preorder α] {ι : Sort u_2} {p : ι → Prop}
{s : (i : ι) → p i → set α} (hs : ∀ (i : ι) (hi : p i), ord_connected (s i hi)) :
ord_connected (Inter fun (i : ι) => Inter fun (hi : p i) => s i hi) :=
ord_connected_Inter fun (i : ι) => ord_connected_Inter (hs i)
theorem ord_connected_pi {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → preorder (α i)] {s : set ι}
{t : (i : ι) → set (α i)} (h : ∀ (i : ι), i ∈ s → ord_connected (t i)) :
ord_connected (pi s t) :=
fun (x : (i : ι) → α i) (hx : x ∈ pi s t) (y : (i : ι) → α i) (hy : y ∈ pi s t)
(z : (i : ι) → α i) (hz : z ∈ Icc x y) (i : ι) (hi : i ∈ s) =>
h i hi (hx i hi) (hy i hi) { left := and.left hz i, right := and.right hz i }
protected instance ord_connected_pi' {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → preorder (α i)]
{s : set ι} {t : (i : ι) → set (α i)} [h : ∀ (i : ι), ord_connected (t i)] :
ord_connected (pi s t) :=
ord_connected_pi fun (i : ι) (hi : i ∈ s) => h i
instance ord_connected_Ici {α : Type u_1} [preorder α] {a : α} : ord_connected (Ici a) :=
fun (x : α) (hx : x ∈ Ici a) (y : α) (hy : y ∈ Ici a) (z : α) (hz : z ∈ Icc x y) =>
le_trans hx (and.left hz)
instance ord_connected_Iic {α : Type u_1} [preorder α] {a : α} : ord_connected (Iic a) :=
fun (x : α) (hx : x ∈ Iic a) (y : α) (hy : y ∈ Iic a) (z : α) (hz : z ∈ Icc x y) =>
le_trans (and.right hz) hy
instance ord_connected_Ioi {α : Type u_1} [preorder α] {a : α} : ord_connected (Ioi a) :=
fun (x : α) (hx : x ∈ Ioi a) (y : α) (hy : y ∈ Ioi a) (z : α) (hz : z ∈ Icc x y) =>
lt_of_lt_of_le hx (and.left hz)
instance ord_connected_Iio {α : Type u_1} [preorder α] {a : α} : ord_connected (Iio a) :=
fun (x : α) (hx : x ∈ Iio a) (y : α) (hy : y ∈ Iio a) (z : α) (hz : z ∈ Icc x y) =>
lt_of_le_of_lt (and.right hz) hy
instance ord_connected_Icc {α : Type u_1} [preorder α] {a : α} {b : α} : ord_connected (Icc a b) :=
ord_connected.inter ord_connected_Ici ord_connected_Iic
instance ord_connected_Ico {α : Type u_1} [preorder α] {a : α} {b : α} : ord_connected (Ico a b) :=
ord_connected.inter ord_connected_Ici ord_connected_Iio
instance ord_connected_Ioc {α : Type u_1} [preorder α] {a : α} {b : α} : ord_connected (Ioc a b) :=
ord_connected.inter ord_connected_Ioi ord_connected_Iic
instance ord_connected_Ioo {α : Type u_1} [preorder α] {a : α} {b : α} : ord_connected (Ioo a b) :=
ord_connected.inter ord_connected_Ioi ord_connected_Iio
instance ord_connected_singleton {α : Type u_1} [partial_order α] {a : α} :
ord_connected (singleton a) :=
eq.mpr (id (Eq._oldrec (Eq.refl (ord_connected (singleton a))) (Eq.symm (Icc_self a))))
ord_connected_Icc
instance ord_connected_empty {α : Type u_1} [preorder α] : ord_connected ∅ :=
fun (x : α) => false.elim
instance ord_connected_univ {α : Type u_1} [preorder α] : ord_connected univ :=
fun (_x : α) (_x_1 : _x ∈ univ) (_x_2 : α) (_x_3 : _x_2 ∈ univ) => subset_univ (Icc _x _x_2)
/-- In a dense order `α`, the subtype from an `ord_connected` set is also densely ordered. -/
protected instance densely_ordered {α : Type u_1} [preorder α] [densely_ordered α] {s : set α}
[hs : ord_connected s] : densely_ordered ↥s :=
densely_ordered.mk
fun (a₁ a₂ : ↥s) (ha : a₁ < a₂) =>
Exists.dcases_on (exists_between ha)
fun (x : α) (h : ↑a₁ < x ∧ x < ↑a₂) =>
and.dcases_on h
fun (ha₁x : ↑a₁ < x) (hxa₂ : x < ↑a₂) =>
Exists.intro
{ val := x,
property :=
hs (subtype.property a₁) (subtype.property a₂)
(Ioo_subset_Icc_self { left := ha₁x, right := hxa₂ }) }
{ left := ha₁x, right := hxa₂ }
instance ord_connected_interval {β : Type u_2} [linear_order β] {a : β} {b : β} :
ord_connected (interval a b) :=
ord_connected_Icc
theorem ord_connected.interval_subset {β : Type u_2} [linear_order β] {s : set β}
(hs : ord_connected s) {x : β} (hx : x ∈ s) {y : β} (hy : y ∈ s) : interval x y ⊆ s :=
sorry
theorem ord_connected_iff_interval_subset {β : Type u_2} [linear_order β] {s : set β} :
ord_connected s ↔ ∀ {x : β}, x ∈ s → ∀ {y : β}, y ∈ s → interval x y ⊆ s :=
sorry
end Mathlib |
ebbce79dd80830dfe8418b08d71a0a58ab217ecc | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/measure_theory/borel_space.lean | add45c76c1d9722a0ac36699e8bcf76c7b48c457 | [
"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 | 33,551 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Yury Kudryashov
-/
import measure_theory.measure_space
import topology.instances.ennreal
import analysis.normed_space.basic
/-!
# Borel (measurable) space
## Main definitions
* `borel α` : the least `σ`-algebra that contains all open sets;
* `class borel_space` : a space with `topological_space` and `measurable_space` structures
such that `‹measurable_space α› = borel α`;
* `class opens_measurable_space` : a space with `topological_space` and `measurable_space`
structures such that all open sets are measurable; equivalently, `borel α ≤ ‹measurable_space α›`.
* `borel_space` instances on `empty`, `unit`, `bool`, `nat`, `int`, `rat`;
* `measurable` and `borel_space` instances on `ℝ`, `ℝ≥0`, `ennreal`.
* A measure is `regular` if it is finite on compact sets, inner regular and outer regular.
## Main statements
* `is_open.is_measurable`, `is_closed.is_measurable`: open and closed sets are measurable;
* `continuous.measurable` : a continuous function is measurable;
* `continuous.measurable2` : if `f : α → β` and `g : α → γ` are measurable and `op : β × γ → δ`
is continuous, then `λ x, op (f x, g y)` is measurable;
* `measurable.add` etc : dot notation for arithmetic operations on `measurable` predicates,
and similarly for `dist` and `edist`;
* `measurable.ennreal*` : special cases for arithmetic operations on `ennreal`s.
-/
noncomputable theory
open classical set
open_locale classical big_operators topological_space
universes u v w x y
variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ι : Sort y} {s t u : set α}
open measurable_space topological_space
/-- `measurable_space` structure generated by `topological_space`. -/
def borel (α : Type u) [topological_space α] : measurable_space α :=
generate_from {s : set α | is_open s}
lemma borel_eq_top_of_discrete [topological_space α] [discrete_topology α] :
borel α = ⊤ :=
top_le_iff.1 $ λ s hs, generate_measurable.basic s (is_open_discrete s)
lemma borel_eq_top_of_encodable [topological_space α] [t1_space α] [encodable α] :
borel α = ⊤ :=
begin
refine (top_le_iff.1 $ λ s hs, bUnion_of_singleton s ▸ _),
apply is_measurable.bUnion s.countable_encodable,
intros x hx,
apply is_measurable.of_compl,
apply generate_measurable.basic,
exact is_closed_singleton
end
lemma borel_eq_generate_from_of_subbasis {s : set (set α)}
[t : topological_space α] [second_countable_topology α] (hs : t = generate_from s) :
borel α = generate_from s :=
le_antisymm
(generate_from_le $ assume u (hu : t.is_open u),
begin
rw [hs] at hu,
induction hu,
case generate_open.basic : u hu
{ exact generate_measurable.basic u hu },
case generate_open.univ
{ exact @is_measurable.univ α (generate_from s) },
case generate_open.inter : s₁ s₂ _ _ hs₁ hs₂
{ exact @is_measurable.inter α (generate_from s) _ _ hs₁ hs₂ },
case generate_open.sUnion : f hf ih {
rcases is_open_sUnion_countable f (by rwa hs) with ⟨v, hv, vf, vu⟩,
rw ← vu,
exact @is_measurable.sUnion α (generate_from s) _ hv
(λ x xv, ih _ (vf xv)) }
end)
(generate_from_le $ assume u hu, generate_measurable.basic _ $
show t.is_open u, by rw [hs]; exact generate_open.basic _ hu)
lemma borel_eq_generate_Iio (α)
[topological_space α] [second_countable_topology α]
[linear_order α] [order_topology α] :
borel α = generate_from (range Iio) :=
begin
refine le_antisymm _ (generate_from_le _),
{ rw borel_eq_generate_from_of_subbasis (@order_topology.topology_eq_generate_intervals α _ _ _),
have H : ∀ a:α, is_measurable (measurable_space.generate_from (range Iio)) (Iio a) :=
λ a, generate_measurable.basic _ ⟨_, rfl⟩,
refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩; [skip, apply H],
by_cases h : ∃ a', ∀ b, a < b ↔ a' ≤ b,
{ rcases h with ⟨a', ha'⟩,
rw (_ : Ioi a = (Iio a')ᶜ), {exact (H _).compl _},
simp [set.ext_iff, ha'] },
{ rcases is_open_Union_countable
(λ a' : {a' : α // a < a'}, {b | a'.1 < b})
(λ a', is_open_lt' _) with ⟨v, ⟨hv⟩, vu⟩,
simp [set.ext_iff] at vu,
have : Ioi a = ⋃ x : v, (Iio x.1.1)ᶜ,
{ simp [set.ext_iff],
refine λ x, ⟨λ ax, _, λ ⟨a', ⟨h, av⟩, ax⟩, lt_of_lt_of_le h ax⟩,
rcases (vu x).2 _ with ⟨a', h₁, h₂⟩,
{ exact ⟨a', h₁, le_of_lt h₂⟩ },
refine not_imp_comm.1 (λ h, _) h,
exact ⟨x, λ b, ⟨λ ab, le_of_not_lt (λ h', h ⟨b, ab, h'⟩),
lt_of_lt_of_le ax⟩⟩ },
rw this, resetI,
apply is_measurable.Union,
exact λ _, (H _).compl _ } },
{ simp, rintro _ a rfl,
exact generate_measurable.basic _ is_open_Iio }
end
lemma borel_eq_generate_Ioi (α)
[topological_space α] [h : second_countable_topology α]
[linear_order α] [order_topology α] :
borel α = generate_from (range Ioi) :=
@borel_eq_generate_Iio (order_dual α) _ h _ _
lemma borel_comap {f : α → β} {t : topological_space β} :
@borel α (t.induced f) = (@borel β t).comap f :=
comap_generate_from.symm
lemma continuous.borel_measurable [topological_space α] [topological_space β]
{f : α → β} (hf : continuous f) :
@measurable α β (borel α) (borel β) f :=
measurable.of_le_map $ generate_from_le $
λ s hs, generate_measurable.basic (f ⁻¹' s) (hf s hs)
/-- A space with `measurable_space` and `topological_space` structures such that
all open sets are measurable. -/
class opens_measurable_space (α : Type*) [topological_space α] [h : measurable_space α] : Prop :=
(borel_le : borel α ≤ h)
/-- A space with `measurable_space` and `topological_space` structures such that
the `σ`-algebra of measurable sets is exactly the `σ`-algebra generated by open sets. -/
class borel_space (α : Type*) [topological_space α] [measurable_space α] : Prop :=
(measurable_eq : ‹measurable_space α› = borel α)
/-- In a `borel_space` all open sets are measurable. -/
@[priority 100]
instance borel_space.opens_measurable {α : Type*} [topological_space α] [measurable_space α]
[borel_space α] : opens_measurable_space α :=
⟨ge_of_eq $ borel_space.measurable_eq⟩
instance subtype.borel_space {α : Type*} [topological_space α] [measurable_space α]
[hα : borel_space α] (s : set α) :
borel_space s :=
⟨by { rw [hα.1, subtype.measurable_space, ← borel_comap], refl }⟩
instance subtype.opens_measurable_space {α : Type*} [topological_space α] [measurable_space α]
[h : opens_measurable_space α] (s : set α) :
opens_measurable_space s :=
⟨by { rw [borel_comap], exact comap_mono h.1 }⟩
section
variables [topological_space α] [measurable_space α] [opens_measurable_space α]
[topological_space β] [measurable_space β] [opens_measurable_space β]
[topological_space γ] [measurable_space γ] [borel_space γ]
[measurable_space δ]
lemma is_open.is_measurable (h : is_open s) : is_measurable s :=
opens_measurable_space.borel_le _ $ generate_measurable.basic _ h
lemma is_measurable_interior : is_measurable (interior s) := is_open_interior.is_measurable
lemma is_closed.is_measurable (h : is_closed s) : is_measurable s :=
is_measurable.compl_iff.1 $ h.is_measurable
lemma is_compact.is_measurable [t2_space α] (h : is_compact s) : is_measurable s :=
h.is_closed.is_measurable
lemma is_measurable_closure : is_measurable (closure s) :=
is_closed_closure.is_measurable
instance nhds_is_measurably_generated (a : α) : (𝓝 a).is_measurably_generated :=
begin
rw [nhds, infi_subtype'],
refine @filter.infi_is_measurably_generated _ _ _ _ (λ i, _),
exact i.2.2.is_measurable.principal_is_measurably_generated
end
/-- If `s` is a measurable set, then `𝓝[s] a` is a measurably generated filter for
each `a`. This cannot be an `instance` because it depends on a non-instance `hs : is_measurable s`.
-/
lemma is_measurable.nhds_within_is_measurably_generated {s : set α} (hs : is_measurable s) (a : α) :
(𝓝[s] a).is_measurably_generated :=
by haveI := hs.principal_is_measurably_generated; exact filter.inf_is_measurably_generated _ _
@[priority 100] -- see Note [lower instance priority]
instance opens_measurable_space.to_measurable_singleton_class [t1_space α] :
measurable_singleton_class α :=
⟨λ x, is_closed_singleton.is_measurable⟩
section order_closed_topology
variables [preorder α] [order_closed_topology α] {a b : α}
lemma is_measurable_Ici : is_measurable (Ici a) := is_closed_Ici.is_measurable
lemma is_measurable_Iic : is_measurable (Iic a) := is_closed_Iic.is_measurable
lemma is_measurable_Icc : is_measurable (Icc a b) := is_closed_Icc.is_measurable
instance nhds_within_Ici_is_measurably_generated :
(𝓝[Ici b] a).is_measurably_generated :=
is_measurable_Ici.nhds_within_is_measurably_generated _
instance nhds_within_Iic_is_measurably_generated :
(𝓝[Iic b] a).is_measurably_generated :=
is_measurable_Iic.nhds_within_is_measurably_generated _
instance at_top_is_measurably_generated : (filter.at_top : filter α).is_measurably_generated :=
@filter.infi_is_measurably_generated _ _ _ _ $
λ a, (is_measurable_Ici : is_measurable (Ici a)).principal_is_measurably_generated
instance at_bot_is_measurably_generated : (filter.at_bot : filter α).is_measurably_generated :=
@filter.infi_is_measurably_generated _ _ _ _ $
λ a, (is_measurable_Iic : is_measurable (Iic a)).principal_is_measurably_generated
end order_closed_topology
section order_closed_topology
variables [linear_order α] [order_closed_topology α] {a b : α}
lemma is_measurable_Iio : is_measurable (Iio a) := is_open_Iio.is_measurable
lemma is_measurable_Ioi : is_measurable (Ioi a) := is_open_Ioi.is_measurable
lemma is_measurable_Ioo : is_measurable (Ioo a b) := is_open_Ioo.is_measurable
lemma is_measurable_Ioc : is_measurable (Ioc a b) := is_measurable_Ioi.inter is_measurable_Iic
lemma is_measurable_Ico : is_measurable (Ico a b) := is_measurable_Ici.inter is_measurable_Iio
instance nhds_within_Ioi_is_measurably_generated :
(𝓝[Ioi b] a).is_measurably_generated :=
is_measurable_Ioi.nhds_within_is_measurably_generated _
instance nhds_within_Iio_is_measurably_generated :
(𝓝[Iio b] a).is_measurably_generated :=
is_measurable_Iio.nhds_within_is_measurably_generated _
end order_closed_topology
lemma is_measurable_interval [decidable_linear_order α] [order_closed_topology α] {a b : α} :
is_measurable (interval a b) :=
is_measurable_Icc
instance prod.opens_measurable_space [second_countable_topology α] [second_countable_topology β] :
opens_measurable_space (α × β) :=
begin
refine ⟨_⟩,
rcases is_open_generated_countable_inter α with ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩,
rcases is_open_generated_countable_inter β with ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩,
have : prod.topological_space = generate_from {g | ∃u∈a, ∃v∈b, g = set.prod u v},
{ rw [ha₅, hb₅], exact prod_generate_from_generate_from_eq ha₄ hb₄ },
rw [borel_eq_generate_from_of_subbasis this],
apply generate_from_le,
rintros _ ⟨u, hu, v, hv, rfl⟩,
have hu : is_open u, by { rw [ha₅], exact generate_open.basic _ hu },
have hv : is_open v, by { rw [hb₅], exact generate_open.basic _ hv },
exact hu.is_measurable.prod hv.is_measurable
end
/-- A continuous function from an `opens_measurable_space` to a `borel_space`
is measurable. -/
lemma continuous.measurable {f : α → γ} (hf : continuous f) :
measurable f :=
hf.borel_measurable.mono opens_measurable_space.borel_le
(le_of_eq $ borel_space.measurable_eq)
/-- A homeomorphism between two Borel spaces is a measurable equivalence.-/
def homeomorph.to_measurable_equiv {α : Type*} {β : Type*} [topological_space α]
[measurable_space α] [borel_space α] [topological_space β] [measurable_space β]
[borel_space β] (h : α ≃ₜ β) :
measurable_equiv α β :=
{ measurable_to_fun := h.continuous_to_fun.measurable,
measurable_inv_fun := h.continuous_inv_fun.measurable,
.. h }
lemma measurable_of_continuous_on_compl_singleton [t1_space α] {f : α → γ} (a : α)
(hf : continuous_on f {x | x ≠ a}) :
measurable f :=
measurable_of_measurable_on_compl_singleton a
(continuous_on_iff_continuous_restrict.1 hf).measurable
lemma continuous.measurable2 [second_countable_topology α] [second_countable_topology β]
{f : δ → α} {g : δ → β} {c : α → β → γ}
(h : continuous (λp:α×β, c p.1 p.2)) (hf : measurable f) (hg : measurable g) :
measurable (λa, c (f a) (g a)) :=
h.measurable.comp (hf.prod_mk hg)
lemma measurable.smul [semiring α] [second_countable_topology α]
[add_comm_monoid γ] [second_countable_topology γ]
[semimodule α γ] [topological_semimodule α γ]
{f : δ → α} {g : δ → γ} (hf : measurable f) (hg : measurable g) :
measurable (λ c, f c • g c) :=
continuous_smul.measurable2 hf hg
lemma measurable.const_smul {α : Type*} [topological_space α] [semiring α]
[add_comm_monoid γ] [semimodule α γ] [topological_semimodule α γ]
{f : δ → γ} (hf : measurable f) (c : α) :
measurable (λ x, c • f x) :=
(continuous_const.smul continuous_id).measurable.comp hf
lemma measurable_const_smul_iff {α : Type*} [topological_space α]
[division_ring α] [add_comm_monoid γ]
[semimodule α γ] [topological_semimodule α γ]
{f : δ → γ} {c : α} (hc : c ≠ 0) :
measurable (λ x, c • f x) ↔ measurable f :=
⟨λ h, by simpa only [smul_smul, inv_mul_cancel hc, one_smul] using h.const_smul c⁻¹,
λ h, h.const_smul c⟩
lemma is_measurable_le' [partial_order α] [order_closed_topology α] [second_countable_topology α] :
is_measurable {p : α × α | p.1 ≤ p.2} :=
order_closed_topology.is_closed_le'.is_measurable
lemma is_measurable_le [partial_order α] [order_closed_topology α] [second_countable_topology α]
{f g : δ → α} (hf : measurable f) (hg : measurable g) :
is_measurable {a | f a ≤ g a} :=
hf.prod_mk hg is_measurable_le'
lemma measurable.max [decidable_linear_order α] [order_closed_topology α] [second_countable_topology α]
{f g : δ → α} (hf : measurable f) (hg : measurable g) :
measurable (λa, max (f a) (g a)) :=
hf.piecewise (is_measurable_le hg hf) hg
lemma measurable.min [decidable_linear_order α] [order_closed_topology α] [second_countable_topology α]
{f g : δ → α} (hf : measurable f) (hg : measurable g) :
measurable (λa, min (f a) (g a)) :=
hf.piecewise (is_measurable_le hf hg) hg
end
section borel_space
variables [topological_space α] [measurable_space α] [borel_space α]
[topological_space β] [measurable_space β] [borel_space β]
[topological_space γ] [measurable_space γ] [borel_space γ]
[measurable_space δ]
lemma prod_le_borel_prod : prod.measurable_space ≤ borel (α × β) :=
begin
rw [‹borel_space α›.measurable_eq, ‹borel_space β›.measurable_eq],
refine sup_le _ _,
{ exact comap_le_iff_le_map.mpr continuous_fst.borel_measurable },
{ exact comap_le_iff_le_map.mpr continuous_snd.borel_measurable }
end
instance prod.borel_space [second_countable_topology α] [second_countable_topology β] :
borel_space (α × β) :=
⟨le_antisymm prod_le_borel_prod opens_measurable_space.borel_le⟩
@[to_additive]
lemma measurable_mul [has_mul α] [has_continuous_mul α] [second_countable_topology α] :
measurable (λ p : α × α, p.1 * p.2) :=
continuous_mul.measurable
@[to_additive]
lemma measurable.mul [has_mul α] [has_continuous_mul α] [second_countable_topology α]
{f : δ → α} {g : δ → α} : measurable f → measurable g → measurable (λa, f a * g a) :=
continuous_mul.measurable2
@[to_additive]
lemma measurable_mul_left [has_mul α] [has_continuous_mul α] (x : α) :
measurable (λ y : α, x * y) :=
continuous.measurable $ continuous_const.mul continuous_id
@[to_additive]
lemma measurable_mul_right [has_mul α] [has_continuous_mul α] (x : α) :
measurable (λ y : α, y * x) :=
continuous.measurable $ continuous_id.mul continuous_const
@[to_additive]
lemma finset.measurable_prod {ι : Type*} [comm_monoid α] [has_continuous_mul α]
[second_countable_topology α] {f : ι → δ → α} (s : finset ι) (hf : ∀i, measurable (f i)) :
measurable (λa, ∏ i in s, f i a) :=
finset.induction_on s
(by simp only [finset.prod_empty, measurable_const])
(assume i s his ih, by simpa [his] using (hf i).mul ih)
@[to_additive]
lemma measurable_inv [group α] [topological_group α] : measurable (has_inv.inv : α → α) :=
continuous_inv.measurable
@[to_additive]
lemma measurable.inv [group α] [topological_group α] {f : δ → α} (hf : measurable f) :
measurable (λa, (f a)⁻¹) :=
measurable_inv.comp hf
lemma measurable_inv' {α : Type*} [normed_field α] [measurable_space α] [borel_space α] :
measurable (has_inv.inv : α → α) :=
measurable_of_continuous_on_compl_singleton 0 normed_field.continuous_on_inv
lemma measurable.inv' {α : Type*} [normed_field α] [measurable_space α] [borel_space α]
{f : δ → α} (hf : measurable f) :
measurable (λa, (f a)⁻¹) :=
measurable_inv'.comp hf
@[to_additive]
lemma measurable.of_inv [group α] [topological_group α] {f : δ → α}
(hf : measurable (λ a, (f a)⁻¹)) : measurable f :=
by simpa only [inv_inv] using hf.inv
@[simp, to_additive]
lemma measurable_inv_iff [group α] [topological_group α] {f : δ → α} :
measurable (λ a, (f a)⁻¹) ↔ measurable f :=
⟨measurable.of_inv, measurable.inv⟩
lemma measurable.sub [add_group α] [topological_add_group α] [second_countable_topology α]
{f g : δ → α} (hf : measurable f) (hg : measurable g) :
measurable (λ x, f x - g x) :=
hf.add hg.neg
lemma measurable.is_lub [linear_order α] [order_topology α] [second_countable_topology α]
{ι} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, measurable (f i))
(hg : ∀ b, is_lub {a | ∃ i, f i b = a} (g b)) :
measurable g :=
begin
change ∀ b, is_lub (range $ λ i, f i b) (g b) at hg,
rw [‹borel_space α›.measurable_eq, borel_eq_generate_Ioi α],
apply measurable_generate_from,
rintro _ ⟨a, rfl⟩,
simp only [set.preimage, mem_Ioi, lt_is_lub_iff (hg _), exists_range_iff, set_of_exists],
exact is_measurable.Union (λ i, hf i (is_open_lt' _).is_measurable)
end
lemma measurable.is_glb [linear_order α] [order_topology α] [second_countable_topology α]
{ι} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, measurable (f i))
(hg : ∀ b, is_glb {a | ∃ i, f i b = a} (g b)) :
measurable g :=
begin
change ∀ b, is_glb (range $ λ i, f i b) (g b) at hg,
rw [‹borel_space α›.measurable_eq, borel_eq_generate_Iio α],
apply measurable_generate_from,
rintro _ ⟨a, rfl⟩,
simp only [set.preimage, mem_Iio, is_glb_lt_iff (hg _), exists_range_iff, set_of_exists],
exact is_measurable.Union (λ i, hf i (is_open_gt' _).is_measurable)
end
lemma measurable_supr [complete_linear_order α] [order_topology α] [second_countable_topology α]
{ι} [encodable ι] {f : ι → δ → α} (hf : ∀ i, measurable (f i)) :
measurable (λ b, ⨆ i, f i b) :=
measurable.is_lub hf $ λ b, is_lub_supr
lemma measurable_infi [complete_linear_order α] [order_topology α] [second_countable_topology α]
{ι} [encodable ι] {f : ι → δ → α} (hf : ∀ i, measurable (f i)) :
measurable (λ b, ⨅ i, f i b) :=
measurable.is_glb hf $ λ b, is_glb_infi
lemma measurable.supr_Prop {α} [measurable_space α] [complete_lattice α]
(p : Prop) {f : δ → α} (hf : measurable f) :
measurable (λ b, ⨆ h : p, f b) :=
classical.by_cases
(assume h : p, begin convert hf, funext, exact supr_pos h end)
(assume h : ¬p, begin convert measurable_const, funext, exact supr_neg h end)
lemma measurable.infi_Prop {α} [measurable_space α] [complete_lattice α]
(p : Prop) {f : δ → α} (hf : measurable f) :
measurable (λ b, ⨅ h : p, f b) :=
classical.by_cases
(assume h : p, begin convert hf, funext, exact infi_pos h end )
(assume h : ¬p, begin convert measurable_const, funext, exact infi_neg h end)
lemma measurable_bsupr [complete_linear_order α] [order_topology α] [second_countable_topology α]
{ι} [encodable ι] (p : ι → Prop) {f : ι → δ → α} (hf : ∀ i, measurable (f i)) :
measurable (λ b, ⨆ i (hi : p i), f i b) :=
measurable_supr $ λ i, (hf i).supr_Prop (p i)
lemma measurable_binfi [complete_linear_order α] [order_topology α] [second_countable_topology α]
{ι} [encodable ι] (p : ι → Prop) {f : ι → δ → α} (hf : ∀ i, measurable (f i)) :
measurable (λ b, ⨅ i (hi : p i), f i b) :=
measurable_infi $ λ i, (hf i).infi_Prop (p i)
/-- Convert a `homeomorph` to a `measurable_equiv`. -/
def homemorph.to_measurable_equiv (h : α ≃ₜ β) :
measurable_equiv α β :=
{ to_equiv := h.to_equiv,
measurable_to_fun := h.continuous_to_fun.measurable,
measurable_inv_fun := h.continuous_inv_fun.measurable }
end borel_space
instance empty.borel_space : borel_space empty := ⟨borel_eq_top_of_discrete.symm⟩
instance unit.borel_space : borel_space unit := ⟨borel_eq_top_of_discrete.symm⟩
instance bool.borel_space : borel_space bool := ⟨borel_eq_top_of_discrete.symm⟩
instance nat.borel_space : borel_space ℕ := ⟨borel_eq_top_of_discrete.symm⟩
instance int.borel_space : borel_space ℤ := ⟨borel_eq_top_of_discrete.symm⟩
instance rat.borel_space : borel_space ℚ := ⟨borel_eq_top_of_encodable.symm⟩
instance real.measurable_space : measurable_space ℝ := borel ℝ
instance real.borel_space : borel_space ℝ := ⟨rfl⟩
instance nnreal.measurable_space : measurable_space nnreal := borel nnreal
instance nnreal.borel_space : borel_space nnreal := ⟨rfl⟩
instance ennreal.measurable_space : measurable_space ennreal := borel ennreal
instance ennreal.borel_space : borel_space ennreal := ⟨rfl⟩
section metric_space
variables [metric_space α] [measurable_space α] [opens_measurable_space α] {x : α} {ε : ℝ}
lemma is_measurable_ball : is_measurable (metric.ball x ε) :=
metric.is_open_ball.is_measurable
lemma is_measurable_closed_ball : is_measurable (metric.closed_ball x ε) :=
metric.is_closed_ball.is_measurable
lemma measurable_dist [second_countable_topology α] :
measurable (λp:α×α, dist p.1 p.2) :=
continuous_dist.measurable
lemma measurable.dist [second_countable_topology α] [measurable_space β] {f g : β → α}
(hf : measurable f) (hg : measurable g) : measurable (λ b, dist (f b) (g b)) :=
continuous_dist.measurable2 hf hg
lemma measurable_nndist [second_countable_topology α] : measurable (λp:α×α, nndist p.1 p.2) :=
continuous_nndist.measurable
lemma measurable.nndist [second_countable_topology α] [measurable_space β] {f g : β → α} :
measurable f → measurable g → measurable (λ b, nndist (f b) (g b)) :=
continuous_nndist.measurable2
end metric_space
section emetric_space
variables [emetric_space α] [measurable_space α] [opens_measurable_space α] {x : α} {ε : ennreal}
lemma is_measurable_eball : is_measurable (emetric.ball x ε) :=
emetric.is_open_ball.is_measurable
lemma measurable_edist [second_countable_topology α] :
measurable (λp:α×α, edist p.1 p.2) :=
continuous_edist.measurable
lemma measurable.edist [second_countable_topology α] [measurable_space β] {f g : β → α} :
measurable f → measurable g → measurable (λ b, edist (f b) (g b)) :=
continuous_edist.measurable2
end emetric_space
namespace real
open measurable_space
lemma borel_eq_generate_from_Ioo_rat :
borel ℝ = generate_from (⋃(a b : ℚ) (h : a < b), {Ioo a b}) :=
borel_eq_generate_from_of_subbasis is_topological_basis_Ioo_rat.2.2
lemma borel_eq_generate_from_Iio_rat :
borel ℝ = generate_from (⋃a:ℚ, {Iio a}) :=
begin
let g, swap,
apply le_antisymm (_ : _ ≤ g) (measurable_space.generate_from_le (λ t, _)),
{ rw borel_eq_generate_from_Ioo_rat,
refine generate_from_le (λ t, _),
simp only [mem_Union], rintro ⟨a, b, h, H⟩,
rw [mem_singleton_iff.1 H],
rw (set.ext (λ x, _) : Ioo (a:ℝ) b = (⋃c>a, (Iio c)ᶜ) ∩ Iio b),
{ have hg : ∀q:ℚ, g.is_measurable (Iio q) :=
λ q, generate_measurable.basic _ (by simp; exact ⟨_, rfl⟩),
refine @is_measurable.inter _ g _ _ _ (hg _),
refine @is_measurable.bUnion _ _ g _ _ (countable_encodable _) (λ c h, _),
exact @is_measurable.compl _ _ g (hg _) },
{ simp [Ioo, Iio],
refine and_congr _ iff.rfl,
exact ⟨λ h,
let ⟨c, ac, cx⟩ := exists_rat_btwn h in
⟨c, rat.cast_lt.1 ac, le_of_lt cx⟩,
λ ⟨c, ac, cx⟩, lt_of_lt_of_le (rat.cast_lt.2 ac) cx⟩ } },
{ simp, rintro r rfl,
exact is_open_Iio.is_measurable }
end
end real
lemma measurable.sub_nnreal [measurable_space α] {f g : α → nnreal} :
measurable f → measurable g → measurable (λ a, f a - g a) :=
nnreal.continuous_sub.measurable2
lemma measurable.nnreal_of_real [measurable_space α] {f : α → ℝ} (hf : measurable f) :
measurable (λ x, nnreal.of_real (f x)) :=
nnreal.continuous_of_real.measurable.comp hf
lemma measurable.nnreal_coe [measurable_space α] {f : α → nnreal} (hf : measurable f) :
measurable (λ x, (f x : ℝ)) :=
nnreal.continuous_coe.measurable.comp hf
lemma measurable.ennreal_coe [measurable_space α] {f : α → nnreal} (hf : measurable f) :
measurable (λ x, (f x : ennreal)) :=
(ennreal.continuous_coe.2 continuous_id).measurable.comp hf
lemma measurable.ennreal_of_real [measurable_space α] {f : α → ℝ} (hf : measurable f) :
measurable (λ x, ennreal.of_real (f x)) :=
ennreal.continuous_of_real.measurable.comp hf
/-- The set of finite `ennreal` numbers is `measurable_equiv` to `nnreal`. -/
def measurable_equiv.ennreal_equiv_nnreal : measurable_equiv {r : ennreal | r ≠ ⊤} nnreal :=
ennreal.ne_top_homeomorph_nnreal.to_measurable_equiv
namespace ennreal
open filter
lemma measurable_coe : measurable (coe : nnreal → ennreal) :=
measurable_id.ennreal_coe
lemma measurable_of_measurable_nnreal [measurable_space α] {f : ennreal → α}
(h : measurable (λp:nnreal, f p)) : measurable f :=
measurable_of_measurable_on_compl_singleton ⊤
(measurable_equiv.ennreal_equiv_nnreal.symm.measurable_coe_iff.1 h)
/-- `ennreal` is `measurable_equiv` to `nnreal ⊕ unit`. -/
def ennreal_equiv_sum :
measurable_equiv ennreal (nnreal ⊕ unit) :=
{ measurable_to_fun := measurable_of_measurable_nnreal measurable_inl,
measurable_inv_fun := measurable_sum measurable_coe (@measurable_const ennreal unit _ _ ⊤),
.. equiv.option_equiv_sum_punit nnreal }
lemma measurable_of_measurable_nnreal_nnreal [measurable_space α] [measurable_space β]
(f : ennreal → ennreal → β) {g : α → ennreal} {h : α → ennreal}
(h₁ : measurable (λp:nnreal × nnreal, f p.1 p.2))
(h₂ : measurable (λr:nnreal, f ⊤ r))
(h₃ : measurable (λr:nnreal, f r ⊤))
(hg : measurable g) (hh : measurable h) : measurable (λa, f (g a) (h a)) :=
let e : measurable_equiv (ennreal × ennreal)
(((nnreal × nnreal) ⊕ (nnreal × unit)) ⊕ ((unit × nnreal) ⊕ (unit × unit))) :=
(measurable_equiv.prod_congr ennreal_equiv_sum ennreal_equiv_sum).trans
(measurable_equiv.sum_prod_sum _ _ _ _) in
have measurable (λp:ennreal×ennreal, f p.1 p.2),
begin
refine e.symm.measurable_coe_iff.1 (measurable_sum (measurable_sum _ _) (measurable_sum _ _)),
{ show measurable (λp:nnreal × nnreal, f p.1 p.2),
exact h₁ },
{ show measurable (λp:nnreal × unit, f p.1 ⊤),
exact h₃.comp (measurable.fst measurable_id) },
{ show measurable ((λp:nnreal, f ⊤ p) ∘ (λp:unit × nnreal, p.2)),
exact h₂.comp (measurable.snd measurable_id) },
{ show measurable (λp:unit × unit, f ⊤ ⊤),
exact measurable_const }
end,
this.comp (measurable.prod_mk hg hh)
lemma measurable_of_real : measurable ennreal.of_real :=
ennreal.continuous_of_real.measurable
end ennreal
lemma measurable.ennreal_mul {α : Type*} [measurable_space α] {f g : α → ennreal} :
measurable f → measurable g → measurable (λa, f a * g a) :=
begin
refine ennreal.measurable_of_measurable_nnreal_nnreal (*) _ _ _,
{ simp only [ennreal.coe_mul.symm],
exact ennreal.measurable_coe.comp measurable_mul },
{ simp [ennreal.top_mul],
exact measurable_const.piecewise
(is_closed_eq continuous_id continuous_const).is_measurable
measurable_const },
{ simp [ennreal.mul_top],
exact measurable_const.piecewise
(is_closed_eq continuous_id continuous_const).is_measurable
measurable_const }
end
lemma measurable.ennreal_add {α : Type*} [measurable_space α] {f g : α → ennreal} :
measurable f → measurable g → measurable (λa, f a + g a) :=
begin
refine ennreal.measurable_of_measurable_nnreal_nnreal (+) _ _ _,
{ simp only [ennreal.coe_add.symm],
exact ennreal.measurable_coe.comp measurable_add },
{ simp [measurable_const] },
{ simp [measurable_const] }
end
lemma measurable.ennreal_sub {α : Type*} [measurable_space α] {f g : α → ennreal} :
measurable f → measurable g → measurable (λa, f a - g a) :=
begin
refine ennreal.measurable_of_measurable_nnreal_nnreal (has_sub.sub) _ _ _,
{ simp only [ennreal.coe_sub.symm],
exact ennreal.measurable_coe.comp nnreal.continuous_sub.measurable },
{ simp [measurable_const] },
{ simp [measurable_const] }
end
section normed_group
variables [measurable_space α] [normed_group α] [opens_measurable_space α] [measurable_space β]
lemma measurable_norm : measurable (norm : α → ℝ) :=
continuous_norm.measurable
lemma measurable.norm {f : β → α} (hf : measurable f) : measurable (λa, norm (f a)) :=
measurable_norm.comp hf
lemma measurable_nnnorm : measurable (nnnorm : α → nnreal) :=
continuous_nnnorm.measurable
lemma measurable.nnnorm {f : β → α} (hf : measurable f) : measurable (λa, nnnorm (f a)) :=
measurable_nnnorm.comp hf
lemma measurable.ennnorm {f : β → α} (hf : measurable f) :
measurable (λa, (nnnorm (f a) : ennreal)) :=
hf.nnnorm.ennreal_coe
end normed_group
namespace measure_theory
namespace measure
variables [measurable_space α] [topological_space α]
/-- A measure `μ` is regular if
- it is finite on all compact sets;
- it is outer regular: `μ(A) = inf { μ(U) | A ⊆ U open }` for `A` measurable;
- it is inner regular: `μ(U) = sup { μ(K) | K ⊆ U compact }` for `U` open. -/
structure regular (μ : measure α) : Prop :=
(lt_top_of_is_compact : ∀ {{K : set α}}, is_compact K → μ K < ⊤)
(outer_regular : ∀ {{A : set α}}, is_measurable A →
(⨅ (U : set α) (h : is_open U) (h2 : A ⊆ U), μ U) ≤ μ A)
(inner_regular : ∀ {{U : set α}}, is_open U →
μ U ≤ ⨆ (K : set α) (h : is_compact K) (h2 : K ⊆ U), μ K)
namespace regular
lemma outer_regular_eq {μ : measure α} (hμ : μ.regular) {{A : set α}}
(hA : is_measurable A) : (⨅ (U : set α) (h : is_open U) (h2 : A ⊆ U), μ U) = μ A :=
le_antisymm (hμ.outer_regular hA) $ le_infi $ λ s, le_infi $ λ hs, le_infi $ λ h2s, μ.mono h2s
lemma inner_regular_eq {μ : measure α} (hμ : μ.regular) {{U : set α}}
(hU : is_open U) : (⨆ (K : set α) (h : is_compact K) (h2 : K ⊆ U), μ K) = μ U :=
le_antisymm (supr_le $ λ s, supr_le $ λ hs, supr_le $ λ h2s, μ.mono h2s) (hμ.inner_regular hU)
protected lemma map [opens_measurable_space α] [measurable_space β] [topological_space β]
[t2_space β] [borel_space β] {μ : measure α} (hμ : μ.regular) (f : α ≃ₜ β) :
(measure.map f μ).regular :=
begin
have hf := f.continuous.measurable,
have h2f := f.to_equiv.injective.preimage_surjective,
have h3f := f.to_equiv.surjective,
split,
{ intros K hK, rw [map_apply hf hK.is_measurable],
apply hμ.lt_top_of_is_compact, rwa f.compact_preimage },
{ intros A hA, rw [map_apply hf hA, ← hμ.outer_regular_eq (hf hA)],
refine le_of_eq _, apply infi_congr (preimage f) h2f,
intro U, apply infi_congr_Prop f.is_open_preimage, intro hU,
apply infi_congr_Prop h3f.preimage_subset_preimage_iff, intro h2U,
rw [map_apply hf hU.is_measurable], },
{ intros U hU, rw [map_apply hf hU.is_measurable, ← hμ.inner_regular_eq (f.continuous U hU)],
refine ge_of_eq _, apply supr_congr (preimage f) h2f,
intro K, apply supr_congr_Prop f.compact_preimage, intro hK,
apply supr_congr_Prop h3f.preimage_subset_preimage_iff, intro h2U,
rw [map_apply hf hK.is_measurable] }
end
protected lemma smul {μ : measure α} (hμ : μ.regular) {x : ennreal} (hx : x < ⊤) :
(x • μ).regular :=
begin
split,
{ intros K hK, exact ennreal.mul_lt_top hx (hμ.lt_top_of_is_compact hK) },
{ intros A hA, rw [coe_smul],
refine le_trans _ (ennreal.mul_left_mono $ hμ.outer_regular hA),
simp only [infi_and'], simp only [infi_subtype'],
haveI : nonempty {s : set α // is_open s ∧ A ⊆ s} := ⟨⟨set.univ, is_open_univ, subset_univ _⟩⟩,
rw [ennreal.mul_infi], refl', exact ne_of_lt hx },
{ intros U hU, rw [coe_smul], refine le_trans (ennreal.mul_left_mono $ hμ.inner_regular hU) _,
simp only [supr_and'], simp only [supr_subtype'],
rw [ennreal.mul_supr], refl' }
end
end regular
end measure
end measure_theory
|
1b3c2d3bc1852fbb2cfe724cce621ddefd587526 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/data/setoid.lean | 062b4793feacf46bc11456be07e6cadfb89ba74e | [
"Apache-2.0"
] | permissive | gebner/mathlib | eab0150cc4f79ec45d2016a8c21750244a2e7ff0 | cc6a6edc397c55118df62831e23bfbd6e6c6b4ab | refs/heads/master | 1,625,574,853,976 | 1,586,712,827,000 | 1,586,712,827,000 | 99,101,412 | 1 | 0 | Apache-2.0 | 1,586,716,389,000 | 1,501,667,958,000 | Lean | UTF-8 | Lean | false | false | 24,486 | lean | /-
Copyright (c) 2019 Amelia Livingston. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Amelia Livingston, Bryan Gin-ge Chen
-/
import data.quot data.set.lattice order.galois_connection
/-!
# Equivalence relations
The first section of the file defines the complete lattice of equivalence relations
on a type, results about the inductively defined equivalence closure of a binary relation,
and the analogues of some isomorphism theorems for quotients of arbitrary types.
The second section comprises properties of equivalence relations viewed as partitions.
## Implementation notes
The function `rel` and lemmas ending in ' make it easier to talk about different
equivalence relations on the same type.
The complete lattice instance for equivalence relations could have been defined by lifting
the Galois insertion of equivalence relations on α into binary relations on α, and then using
`complete_lattice.copy` to define a complete lattice instance with more appropriate
definitional equalities (a similar example is `filter.complete_lattice` in
`order/filter/basic.lean`). This does not save space, however, and is less clear.
Partitions are not defined as a separate structure here; users are encouraged to
reason about them using the existing `setoid` and its infrastructure.
## Tags
setoid, equivalence, iseqv, relation, equivalence relation, partition, equivalence
class
-/
variables {α : Type*} {β : Type*}
/-- A version of `setoid.r` that takes the equivalence relation as an explicit argument. -/
def setoid.rel (r : setoid α) : α → α → Prop := @setoid.r _ r
/-- A version of `quotient.eq'` compatible with `setoid.rel`, to make rewriting possible. -/
lemma quotient.eq_rel {r : setoid α} {x y} : ⟦x⟧ = ⟦y⟧ ↔ r.rel x y := quotient.eq'
namespace setoid
@[ext] lemma ext' {r s : setoid α} (H : ∀ a b, r.rel a b ↔ s.rel a b) :
r = s := ext H
lemma ext_iff {r s : setoid α} : r = s ↔ ∀ a b, r.rel a b ↔ s.rel a b :=
⟨λ h a b, h ▸ iff.rfl, ext'⟩
/-- Two equivalence relations are equal iff their underlying binary operations are equal. -/
theorem eq_iff_rel_eq {r₁ r₂ : setoid α} : r₁ = r₂ ↔ r₁.rel = r₂.rel :=
⟨λ h, h ▸ rfl, λ h, setoid.ext' $ λ x y, h ▸ iff.rfl⟩
/-- Defining `≤` for equivalence relations. -/
instance : has_le (setoid α) := ⟨λ r s, ∀ ⦃x y⦄, r.rel x y → s.rel x y⟩
theorem le_def {r s : setoid α} : r ≤ s ↔ ∀ {x y}, r.rel x y → s.rel x y := iff.rfl
@[refl] lemma refl' (r : setoid α) (x) : r.rel x x := r.2.1 x
@[symm] lemma symm' (r : setoid α) : ∀ {x y}, r.rel x y → r.rel y x := λ _ _ h, r.2.2.1 h
@[trans] lemma trans' (r : setoid α) : ∀ {x y z}, r.rel x y → r.rel y z → r.rel x z :=
λ _ _ _ hx, r.2.2.2 hx
/-- The kernel of a function is an equivalence relation. -/
def ker (f : α → β) : setoid α :=
⟨λ x y, f x = f y, ⟨λ _, rfl, λ _ _ h, h.symm, λ _ _ _ h, h.trans⟩⟩
/-- The kernel of the quotient map induced by an equivalence relation r equals r. -/
@[simp] lemma ker_mk_eq (r : setoid α) : ker (@quotient.mk _ r) = r :=
ext' $ λ x y, quotient.eq
/-- Given types `α, β`, the product of two equivalence relations `r` on `α` and `s` on `β`:
`(x₁, x₂), (y₁, y₂) ∈ α × β` are related by `r.prod s` iff `x₁` is related to `y₁`
by `r` and `x₂` is related to `y₂` by `s`. -/
protected def prod (r : setoid α) (s : setoid β) : setoid (α × β) :=
{ r := λ x y, r.rel x.1 y.1 ∧ s.rel x.2 y.2,
iseqv := ⟨λ x, ⟨r.refl' x.1, s.refl' x.2⟩, λ _ _ h, ⟨r.symm' h.1, s.symm' h.2⟩,
λ _ _ _ h1 h2, ⟨r.trans' h1.1 h2.1, s.trans' h1.2 h2.2⟩⟩ }
/-- The infimum of two equivalence relations. -/
instance : has_inf (setoid α) :=
⟨λ r s, ⟨λ x y, r.rel x y ∧ s.rel x y, ⟨λ x, ⟨r.refl' x, s.refl' x⟩,
λ _ _ h, ⟨r.symm' h.1, s.symm' h.2⟩,
λ _ _ _ h1 h2, ⟨r.trans' h1.1 h2.1, s.trans' h1.2 h2.2⟩⟩⟩⟩
/-- The infimum of 2 equivalence relations r and s is the same relation as the infimum
of the underlying binary operations. -/
lemma inf_def {r s : setoid α} : (r ⊓ s).rel = r.rel ⊓ s.rel := rfl
theorem inf_iff_and {r s : setoid α} {x y} :
(r ⊓ s).rel x y ↔ r.rel x y ∧ s.rel x y := iff.rfl
/-- The infimum of a set of equivalence relations. -/
instance : has_Inf (setoid α) :=
⟨λ S, ⟨λ x y, ∀ r ∈ S, rel r x y,
⟨λ x r hr, r.refl' x, λ _ _ h r hr, r.symm' $ h r hr,
λ _ _ _ h1 h2 r hr, r.trans' (h1 r hr) $ h2 r hr⟩⟩⟩
/-- The underlying binary operation of the infimum of a set of equivalence relations
is the infimum of the set's image under the map to the underlying binary operation. -/
theorem Inf_def {s : set (setoid α)} : (Inf s).rel = Inf (rel '' s) :=
by { ext, simp only [Inf_image, infi_apply, infi_Prop_eq], refl }
instance : partial_order (setoid α) :=
{ le := (≤),
lt := λ r s, r ≤ s ∧ ¬s ≤ r,
le_refl := λ _ _ _, id,
le_trans := λ _ _ _ hr hs _ _ h, hs $ hr h,
lt_iff_le_not_le := λ _ _, iff.rfl,
le_antisymm := λ r s h1 h2, setoid.ext' $ λ x y, ⟨λ h, h1 h, λ h, h2 h⟩ }
/-- The complete lattice of equivalence relations on a type, with bottom element `=`
and top element the trivial equivalence relation. -/
instance complete_lattice : complete_lattice (setoid α) :=
{ inf := has_inf.inf,
inf_le_left := λ _ _ _ _ h, h.1,
inf_le_right := λ _ _ _ _ h, h.2,
le_inf := λ _ _ _ h1 h2 _ _ h, ⟨h1 h, h2 h⟩,
top := ⟨λ _ _, true, ⟨λ _, trivial, λ _ _ h, h, λ _ _ _ h1 h2, h1⟩⟩,
le_top := λ _ _ _ _, trivial,
bot := ⟨(=), ⟨λ _, rfl, λ _ _ h, h.symm, λ _ _ _ h1 h2, h1.trans h2⟩⟩,
bot_le := λ r x y h, h ▸ r.2.1 x,
.. complete_lattice_of_Inf (setoid α) $ assume s,
⟨λ r hr x y h, h _ hr, λ r hr x y h r' hr', hr hr' h⟩ }
/-- The inductively defined equivalence closure of a binary relation r is the infimum
of the set of all equivalence relations containing r. -/
theorem eqv_gen_eq (r : α → α → Prop) :
eqv_gen.setoid r = Inf {s : setoid α | ∀ ⦃x y⦄, r x y → s.rel x y} :=
le_antisymm
(λ _ _ H, eqv_gen.rec (λ _ _ h _ hs, hs h) (refl' _)
(λ _ _ _, symm' _) (λ _ _ _ _ _, trans' _) H)
(Inf_le $ λ _ _ h, eqv_gen.rel _ _ h)
/-- The supremum of two equivalence relations r and s is the equivalence closure of the binary
relation `x is related to y by r or s`. -/
lemma sup_eq_eqv_gen (r s : setoid α) :
r ⊔ s = eqv_gen.setoid (λ x y, r.rel x y ∨ s.rel x y) :=
begin
rw eqv_gen_eq,
apply congr_arg Inf,
simp only [le_def, or_imp_distrib, ← forall_and_distrib]
end
/-- The supremum of 2 equivalence relations r and s is the equivalence closure of the
supremum of the underlying binary operations. -/
lemma sup_def {r s : setoid α} : r ⊔ s = eqv_gen.setoid (r.rel ⊔ s.rel) :=
by rw sup_eq_eqv_gen; refl
/-- The supremum of a set S of equivalence relations is the equivalence closure of the binary
relation `there exists r ∈ S relating x and y`. -/
lemma Sup_eq_eqv_gen (S : set (setoid α)) :
Sup S = eqv_gen.setoid (λ x y, ∃ r : setoid α, r ∈ S ∧ r.rel x y) :=
begin
rw eqv_gen_eq,
apply congr_arg Inf,
simp only [upper_bounds, le_def, and_imp, exists_imp_distrib],
ext,
exact ⟨λ H x y r hr, H hr, λ H r hr x y, H r hr⟩
end
/-- The supremum of a set of equivalence relations is the equivalence closure of the
supremum of the set's image under the map to the underlying binary operation. -/
lemma Sup_def {s : set (setoid α)} : Sup s = eqv_gen.setoid (Sup (rel '' s)) :=
begin
rw Sup_eq_eqv_gen,
congr,
ext x y,
erw [Sup_image, supr_apply, supr_apply, supr_Prop_eq],
simp only [Sup_image, supr_Prop_eq, supr_apply, supr_Prop_eq, exists_prop]
end
/-- The equivalence closure of an equivalence relation r is r. -/
@[simp] lemma eqv_gen_of_setoid (r : setoid α) : eqv_gen.setoid r.r = r :=
le_antisymm (by rw eqv_gen_eq; exact Inf_le (λ _ _, id)) eqv_gen.rel
/-- Equivalence closure is idempotent. -/
@[simp] lemma eqv_gen_idem (r : α → α → Prop) :
eqv_gen.setoid (eqv_gen.setoid r).rel = eqv_gen.setoid r :=
eqv_gen_of_setoid _
/-- The equivalence closure of a binary relation r is contained in any equivalence
relation containing r. -/
theorem eqv_gen_le {r : α → α → Prop} {s : setoid α} (h : ∀ x y, r x y → s.rel x y) :
eqv_gen.setoid r ≤ s :=
by rw eqv_gen_eq; exact Inf_le h
/-- Equivalence closure of binary relations is monotonic. -/
theorem eqv_gen_mono {r s : α → α → Prop} (h : ∀ x y, r x y → s x y) :
eqv_gen.setoid r ≤ eqv_gen.setoid s :=
eqv_gen_le $ λ _ _ hr, eqv_gen.rel _ _ $ h _ _ hr
/-- There is a Galois insertion of equivalence relations on α into binary relations
on α, with equivalence closure the lower adjoint. -/
def gi : @galois_insertion (α → α → Prop) (setoid α) _ _ eqv_gen.setoid rel :=
{ choice := λ r h, eqv_gen.setoid r,
gc := λ r s, ⟨λ H _ _ h, H $ eqv_gen.rel _ _ h, λ H, eqv_gen_of_setoid s ▸ eqv_gen_mono H⟩,
le_l_u := λ x, (eqv_gen_of_setoid x).symm ▸ le_refl x,
choice_eq := λ _ _, rfl }
open function
/-- A function from α to β is injective iff its kernel is the bottom element of the complete lattice
of equivalence relations on α. -/
theorem injective_iff_ker_bot (f : α → β) :
injective f ↔ ker f = ⊥ :=
⟨λ hf, setoid.ext' $ λ x y, ⟨λ h, hf h, λ h, h ▸ rfl⟩,
λ hk x y h, show rel ⊥ x y, from hk ▸ (show (ker f).rel x y, from h)⟩
/-- The elements related to x ∈ α by the kernel of f are those in the preimage of f(x) under f. -/
lemma ker_apply_eq_preimage (f : α → β) (x) : (ker f).rel x = f ⁻¹' {f x} :=
set.ext $ λ x,
⟨λ h, set.mem_preimage.2 (set.mem_singleton_iff.2 h.symm),
λ h, (set.mem_singleton_iff.1 (set.mem_preimage.1 h)).symm⟩
/-- The uniqueness part of the universal property for quotients of an arbitrary type. -/
theorem lift_unique {r : setoid α} {f : α → β} (H : r ≤ ker f) (g : quotient r → β)
(Hg : f = g ∘ quotient.mk) : quotient.lift f H = g :=
begin
ext,
rcases x,
erw [quotient.lift_beta f H, Hg],
refl
end
/-- Given a map f from α to β, the natural map from the quotient of α by the kernel of f is
injective. -/
lemma injective_ker_lift (f : α → β) : injective (@quotient.lift _ _ (ker f) f (λ _ _ h, h)) :=
λ x y, quotient.induction_on₂' x y $ λ a b h, quotient.sound' h
/-- Given a map f from α to β, the kernel of f is the unique equivalence relation on α whose
induced map from the quotient of α to β is injective. -/
lemma ker_eq_lift_of_injective {r : setoid α} (f : α → β) (H : ∀ x y, r.rel x y → f x = f y)
(h : injective (quotient.lift f H)) : ker f = r :=
le_antisymm
(λ x y hk, quotient.exact $ h $ show quotient.lift f H ⟦x⟧ = quotient.lift f H ⟦y⟧, from hk)
H
variables (r : setoid α) (f : α → β)
/-- The first isomorphism theorem for sets: the quotient of α by the kernel of a function f
bijects with f's image. -/
noncomputable def quotient_ker_equiv_range :
quotient (ker f) ≃ set.range f :=
@equiv.of_bijective _ (set.range f) (@quotient.lift _ (set.range f) (ker f)
(λ x, ⟨f x, set.mem_range_self x⟩) $ λ _ _ h, subtype.eq' h)
⟨λ x y h, injective_ker_lift f $ by rcases x; rcases y; injections,
λ ⟨w, z, hz⟩, ⟨@quotient.mk _ (ker f) z, by rw quotient.lift_beta; exact subtype.ext.2 hz⟩⟩
/-- The quotient of α by the kernel of a surjective function f bijects with f's codomain. -/
noncomputable def quotient_ker_equiv_of_surjective (hf : surjective f) :
quotient (ker f) ≃ β :=
@equiv.of_bijective _ _ (@quotient.lift _ _ (ker f) f (λ _ _, id))
⟨injective_ker_lift f, λ y, exists.elim (hf y) $ λ w hw, ⟨quotient.mk' w, hw⟩⟩
variables {r f}
/-- Given a function `f : α → β` and equivalence relation `r` on `α`, the equivalence
closure of the relation on `f`'s image defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are
related to the elements of `f⁻¹(y)` by `r`.' -/
def map (r : setoid α) (f : α → β) : setoid β :=
eqv_gen.setoid $ λ x y, ∃ a b, f a = x ∧ f b = y ∧ r.rel a b
/-- Given a surjective function f whose kernel is contained in an equivalence relation r, the
equivalence relation on f's codomain defined by x ≈ y ↔ the elements of f⁻¹(x) are related to
the elements of f⁻¹(y) by r. -/
def map_of_surjective (r) (f : α → β) (h : ker f ≤ r) (hf : surjective f) :
setoid β :=
⟨λ x y, ∃ a b, f a = x ∧ f b = y ∧ r.rel a b,
⟨λ x, let ⟨y, hy⟩ := hf x in ⟨y, y, hy, hy, r.refl' y⟩,
λ _ _ ⟨x, y, hx, hy, h⟩, ⟨y, x, hy, hx, r.symm' h⟩,
λ _ _ _ ⟨x, y, hx, hy, h₁⟩ ⟨y', z, hy', hz, h₂⟩,
⟨x, z, hx, hz, r.trans' h₁ $ r.trans' (h $ by rwa ←hy' at hy) h₂⟩⟩⟩
/-- A special case of the equivalence closure of an equivalence relation r equalling r. -/
lemma map_of_surjective_eq_map (h : ker f ≤ r) (hf : surjective f) :
map r f = map_of_surjective r f h hf :=
by rw ←eqv_gen_of_setoid (map_of_surjective r f h hf); refl
/-- Given a function `f : α → β`, an equivalence relation `r` on `β` induces an equivalence
relation on `α` defined by '`x ≈ y` iff `f(x)` is related to `f(y)` by `r`'. -/
def comap (f : α → β) (r : setoid β) : setoid α :=
⟨λ x y, r.rel (f x) (f y), ⟨λ _, r.refl' _, λ _ _ h, r.symm' h, λ _ _ _ h1, r.trans' h1⟩⟩
/-- Given a map `f : N → M` and an equivalence relation `r` on `β`, the equivalence relation
induced on `α` by `f` equals the kernel of `r`'s quotient map composed with `f`. -/
lemma comap_eq {f : α → β} {r : setoid β} : comap f r = ker (@quotient.mk _ r ∘ f) :=
ext $ λ x y, show _ ↔ ⟦_⟧ = ⟦_⟧, by rw quotient.eq; refl
/-- The second isomorphism theorem for sets. -/
noncomputable def comap_quotient_equiv (f : α → β) (r : setoid β) :
quotient (comap f r) ≃ set.range (@quotient.mk _ r ∘ f) :=
(quotient.congr_right $ ext_iff.1 comap_eq).trans $ quotient_ker_equiv_range $ quotient.mk ∘ f
variables (r f)
/-- The third isomorphism theorem for sets. -/
def quotient_quotient_equiv_quotient (s : setoid α) (h : r ≤ s) :
quotient (ker (quot.map_right h)) ≃ quotient s :=
{ to_fun := λ x, quotient.lift_on' x (λ w, quotient.lift_on' w (@quotient.mk _ s) $
λ x y H, quotient.sound $ h H) $ λ x y, quotient.induction_on₂' x y $ λ w z H,
show @quot.mk _ _ _ = @quot.mk _ _ _, from H,
inv_fun := λ x, quotient.lift_on' x
(λ w, @quotient.mk _ (ker $ quot.map_right h) $ @quotient.mk _ r w) $
λ x y H, quotient.sound' $ show @quot.mk _ _ _ = @quot.mk _ _ _, from quotient.sound H,
left_inv := λ x, quotient.induction_on' x $ λ y, quotient.induction_on' y $
λ w, by show ⟦_⟧ = _; refl,
right_inv := λ x, quotient.induction_on' x $ λ y, by show ⟦_⟧ = _; refl }
variables {r f}
section
open quotient
/-- Given an equivalence relation r on α, the order-preserving bijection between the set of
equivalence relations containing r and the equivalence relations on the quotient of α by r. -/
def correspondence (r : setoid α) : ((≤) : {s // r ≤ s} → {s // r ≤ s} → Prop) ≃o
((≤) : setoid (quotient r) → setoid (quotient r) → Prop) :=
{ to_fun := λ s, map_of_surjective s.1 quotient.mk ((ker_mk_eq r).symm ▸ s.2) exists_rep,
inv_fun := λ s, ⟨comap quotient.mk s, λ x y h, show s.rel ⟦x⟧ ⟦y⟧, by rw eq_rel.2 h⟩,
left_inv := λ s, subtype.ext.2 $ ext' $ λ _ _,
⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in
s.1.trans' (s.1.symm' $ s.2 $ eq_rel.1 hx) $ s.1.trans' H $ s.2 $ eq_rel.1 hy,
λ h, ⟨_, _, rfl, rfl, h⟩⟩,
right_inv := λ s, let Hm : ker quotient.mk ≤ comap quotient.mk s :=
λ x y h, show s.rel ⟦x⟧ ⟦y⟧, by rw (@eq_rel _ r x y).2 ((ker_mk_eq r) ▸ h) in
ext' $ λ x y, ⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in hx ▸ hy ▸ H,
quotient.induction_on₂ x y $ λ w z h, ⟨w, z, rfl, rfl, h⟩⟩,
ord := λ s t, ⟨λ h x y hs, let ⟨a, b, hx, hy, Hs⟩ := hs in ⟨a, b, hx, hy, h Hs⟩,
λ h x y hs, let ⟨a, b, hx, hy, ht⟩ := h ⟨x, y, rfl, rfl, hs⟩ in
t.1.trans' (t.1.symm' $ t.2 $ eq_rel.1 hx) $ t.1.trans' ht $ t.2 $ eq_rel.1 hy⟩ }
end
/-!
### Partitions
-/
/-- If x ∈ α is in 2 elements of a set of sets partitioning α, those 2 sets are equal. -/
lemma eq_of_mem_eqv_class {c : set (set α)} (H : ∀ a, ∃! b ∈ c, a ∈ b)
{x b b'} (hc : b ∈ c) (hb : x ∈ b) (hc' : b' ∈ c) (hb' : x ∈ b') :
b = b' :=
(H x).unique2 hc hb hc' hb'
/-- Makes an equivalence relation from a set of sets partitioning α. -/
def mk_classes (c : set (set α)) (H : ∀ a, ∃! b ∈ c, a ∈ b) :
setoid α :=
⟨λ x y, ∀ s ∈ c, x ∈ s → y ∈ s, ⟨λ _ _ _ hx, hx,
λ x y h s hs hy, (H x).elim2 $ λ t ht hx _,
have s = t, from eq_of_mem_eqv_class H hs hy ht (h t ht hx),
this.symm ▸ hx,
λ x y z h1 h2 s hs hx, (H y).elim2 $ λ t ht hy _, (H z).elim2 $ λ t' ht' hz _,
have hst : s = t, from eq_of_mem_eqv_class H hs (h1 _ hs hx) ht hy,
have htt' : t = t', from eq_of_mem_eqv_class H ht (h2 _ ht hy) ht' hz,
(hst.trans htt').symm ▸ hz⟩⟩
/-- Makes the equivalence classes of an equivalence relation. -/
def classes (r : setoid α) : set (set α) :=
{s | ∃ y, s = {x | r.rel x y}}
lemma mem_classes (r : setoid α) (y) : {x | r.rel x y} ∈ r.classes := ⟨y, rfl⟩
/-- Two equivalence relations are equal iff all their equivalence classes are equal. -/
lemma eq_iff_classes_eq {r₁ r₂ : setoid α} :
r₁ = r₂ ↔ ∀ x, {y | r₁.rel x y} = {y | r₂.rel x y} :=
⟨λ h x, h ▸ rfl, λ h, ext' $ λ x, (set.ext_iff _ _).1 $ h x⟩
lemma rel_iff_exists_classes (r : setoid α) {x y} :
r.rel x y ↔ ∃ c ∈ r.classes, x ∈ c ∧ y ∈ c :=
⟨λ h, ⟨_, r.mem_classes y, h, r.refl' y⟩,
λ ⟨c, ⟨z, hz⟩, hx, hy⟩, by { subst c, exact r.trans' hx (r.symm' hy) }⟩
/-- Two equivalence relations are equal iff their equivalence classes are equal. -/
lemma classes_inj {r₁ r₂ : setoid α} :
r₁ = r₂ ↔ r₁.classes = r₂.classes :=
⟨λ h, h ▸ rfl, λ h, ext' $ λ a b, by simp only [rel_iff_exists_classes, exists_prop, h] ⟩
/-- The empty set is not an equivalence class. -/
lemma empty_not_mem_classes {r : setoid α} : ∅ ∉ r.classes :=
λ ⟨y, hy⟩, set.not_mem_empty y $ hy.symm ▸ r.refl' y
/-- Equivalence classes partition the type. -/
lemma classes_eqv_classes {r : setoid α} (a) : ∃! b ∈ r.classes, a ∈ b :=
exists_unique.intro2 {x | r.rel x a} (r.mem_classes a) (r.refl' _) $
begin
rintros _ ⟨y, rfl⟩ ha,
ext x,
exact ⟨λ hx, r.trans' hx (r.symm' ha), λ hx, r.trans' hx ha⟩
end
/-- If x ∈ α is in 2 equivalence classes, the equivalence classes are equal. -/
lemma eq_of_mem_classes {r : setoid α} {x b} (hc : b ∈ r.classes)
(hb : x ∈ b) {b'} (hc' : b' ∈ r.classes) (hb' : x ∈ b') : b = b' :=
eq_of_mem_eqv_class classes_eqv_classes hc hb hc' hb'
/-- The elements of a set of sets partitioning α are the equivalence classes of the
equivalence relation defined by the set of sets. -/
lemma eq_eqv_class_of_mem {c : set (set α)}
(H : ∀ a, ∃! b ∈ c, a ∈ b) {s y} (hs : s ∈ c) (hy : y ∈ s) :
s = {x | (mk_classes c H).rel x y} :=
set.ext $ λ x,
⟨λ hs', symm' (mk_classes c H) $ λ b' hb' h', eq_of_mem_eqv_class H hs hy hb' h' ▸ hs',
λ hx, (H x).elim2 $ λ b' hc' hb' h',
(eq_of_mem_eqv_class H hs hy hc' $ hx b' hc' hb').symm ▸ hb'⟩
/-- The equivalence classes of the equivalence relation defined by a set of sets
partitioning α are elements of the set of sets. -/
lemma eqv_class_mem {c : set (set α)} (H : ∀ a, ∃! b ∈ c, a ∈ b) {y} :
{x | (mk_classes c H).rel x y} ∈ c :=
(H y).elim2 $ λ b hc hy hb, eq_eqv_class_of_mem H hc hy ▸ hc
/-- Distinct elements of a set of sets partitioning α are disjoint. -/
lemma eqv_classes_disjoint {c : set (set α)} (H : ∀ a, ∃! b ∈ c, a ∈ b) :
set.pairwise_disjoint c :=
λ b₁ h₁ b₂ h₂ h, set.disjoint_left.2 $
λ x hx1 hx2, (H x).elim2 $ λ b hc hx hb, h $ eq_of_mem_eqv_class H h₁ hx1 h₂ hx2
/-- A set of disjoint sets covering α partition α (classical). -/
lemma eqv_classes_of_disjoint_union {c : set (set α)}
(hu : set.sUnion c = @set.univ α) (H : set.pairwise_disjoint c) (a) :
∃! b ∈ c, a ∈ b :=
let ⟨b, hc, ha⟩ := set.mem_sUnion.1 $ show a ∈ _, by rw hu; exact set.mem_univ a in
exists_unique.intro2 b hc ha $ λ b' hc' ha', H.elim hc' hc a ha' ha
/-- Makes an equivalence relation from a set of disjoints sets covering α. -/
def setoid_of_disjoint_union {c : set (set α)} (hu : set.sUnion c = @set.univ α)
(H : set.pairwise_disjoint c) : setoid α :=
setoid.mk_classes c $ eqv_classes_of_disjoint_union hu H
/-- The equivalence relation made from the equivalence classes of an equivalence
relation r equals r. -/
theorem mk_classes_classes (r : setoid α) :
mk_classes r.classes classes_eqv_classes = r :=
ext' $ λ x y, ⟨λ h, r.symm' (h {z | r.rel z x} (r.mem_classes x) $ r.refl' x),
λ h b hb hx, eq_of_mem_classes (r.mem_classes x) (r.refl' x) hb hx ▸ r.symm' h⟩
section partition
/-- A collection `c : set (set α)` of sets is a partition of `α` into pairwise
disjoint sets if `∅ ∉ c` and each element `a : α` belongs to a unique set `b ∈ c`. -/
def is_partition (c : set (set α)) :=
∅ ∉ c ∧ ∀ a, ∃! b ∈ c, a ∈ b
/-- A partition of `α` does not contain the empty set. -/
lemma nonempty_of_mem_partition {c : set (set α)} (hc : is_partition c) {s} (h : s ∈ c) :
s.nonempty :=
set.ne_empty_iff_nonempty.1 $ λ hs0, hc.1 $ hs0 ▸ h
/-- All elements of a partition of α are the equivalence class of some y ∈ α. -/
lemma exists_of_mem_partition {c : set (set α)} (hc : is_partition c) {s} (hs : s ∈ c) :
∃ y, s = {x | (mk_classes c hc.2).rel x y} :=
let ⟨y, hy⟩ := nonempty_of_mem_partition hc hs in
⟨y, eq_eqv_class_of_mem hc.2 hs hy⟩
/-- The equivalence classes of the equivalence relation defined by a partition of α equal
the original partition. -/
theorem classes_mk_classes (c : set (set α)) (hc : is_partition c) :
(mk_classes c hc.2).classes = c :=
set.ext $ λ s,
⟨λ ⟨y, hs⟩, (hc.2 y).elim2 $ λ b hm hb hy,
by rwa (show s = b, from hs.symm ▸ set.ext
(λ x, ⟨λ hx, symm' (mk_classes c hc.2) hx b hm hb,
λ hx b' hc' hx', eq_of_mem_eqv_class hc.2 hm hx hc' hx' ▸ hb⟩)),
exists_of_mem_partition hc⟩
/-- Defining `≤` on partitions as the `≤` defined on their induced equivalence relations. -/
instance partition.le : has_le (subtype (@is_partition α)) :=
⟨λ x y, mk_classes x.1 x.2.2 ≤ mk_classes y.1 y.2.2⟩
/-- Defining a partial order on partitions as the partial order on their induced
equivalence relations. -/
instance partition.partial_order : partial_order (subtype (@is_partition α)) :=
{ le := (≤),
lt := λ x y, x ≤ y ∧ ¬y ≤ x,
le_refl := λ _, @le_refl (setoid α) _ _,
le_trans := λ _ _ _, @le_trans (setoid α) _ _ _ _,
lt_iff_le_not_le := λ _ _, iff.rfl,
le_antisymm := λ x y hx hy, let h := @le_antisymm (setoid α) _ _ _ hx hy in by
rw [subtype.ext, ←classes_mk_classes x.1 x.2, ←classes_mk_classes y.1 y.2, h] }
variables (α)
/-- The order-preserving bijection between equivalence relations and partitions of sets. -/
def partition.order_iso :
((≤) : setoid α → setoid α → Prop) ≃o (@setoid.partition.partial_order α).le :=
{ to_fun := λ r, ⟨r.classes, empty_not_mem_classes, classes_eqv_classes⟩,
inv_fun := λ x, mk_classes x.1 x.2.2,
left_inv := mk_classes_classes,
right_inv := λ x, by rw [subtype.ext, ←classes_mk_classes x.1 x.2],
ord := λ x y, by conv {to_lhs, rw [←mk_classes_classes x, ←mk_classes_classes y]}; refl }
variables {α}
/-- A complete lattice instance for partitions; there is more infrastructure for the
equivalent complete lattice on equivalence relations. -/
instance partition.complete_lattice : complete_lattice (subtype (@is_partition α)) :=
galois_insertion.lift_complete_lattice $ @order_iso.to_galois_insertion
_ (subtype (@is_partition α)) _ (partial_order.to_preorder _) $ partition.order_iso α
end partition
end setoid
|
fb06b2eae5fac9ebfe765d5a166c9cb77855dcf4 | 4727251e0cd73359b15b664c3170e5d754078599 | /archive/100-theorems-list/70_perfect_numbers.lean | e399fc20d8cdadbe95eadeee6d665fb7b4881472 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 5,008 | lean | /-
Copyright (c) 2020 Aaron Anderson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson
-/
import number_theory.arithmetic_function
import number_theory.lucas_lehmer
import algebra.geom_sum
import ring_theory.multiplicity
/-!
# Perfect Numbers
This file proves Theorem 70 from the [100 Theorems List](https://www.cs.ru.nl/~freek/100/).
The theorem characterizes even perfect numbers.
Euclid proved that if `2 ^ (k + 1) - 1` is prime (these primes are known as Mersenne primes),
then `2 ^ k * 2 ^ (k + 1) - 1` is perfect.
Euler proved the converse, that if `n` is even and perfect, then there exists `k` such that
`n = 2 ^ k * 2 ^ (k + 1) - 1` and `2 ^ (k + 1) - 1` is prime.
## References
https://en.wikipedia.org/wiki/Euclid%E2%80%93Euler_theorem
-/
lemma odd_mersenne_succ (k : ℕ) : ¬ 2 ∣ mersenne (k + 1) :=
by simp [← even_iff_two_dvd, ← nat.even_succ, nat.succ_eq_add_one] with parity_simps
namespace nat
open arithmetic_function finset
open_locale arithmetic_function
lemma sigma_two_pow_eq_mersenne_succ (k : ℕ) : σ 1 (2 ^ k) = mersenne (k + 1) :=
by simpa [mersenne, prime_two, ← geom_sum_mul_add 1 (k+1)]
/-- Euclid's theorem that Mersenne primes induce perfect numbers -/
theorem perfect_two_pow_mul_mersenne_of_prime (k : ℕ) (pr : (mersenne (k + 1)).prime) :
perfect ((2 ^ k) * mersenne (k + 1)) :=
begin
rw [perfect_iff_sum_divisors_eq_two_mul, ← mul_assoc, ← pow_succ, ← sigma_one_apply, mul_comm,
is_multiplicative_sigma.map_mul_of_coprime
(nat.prime_two.coprime_pow_of_not_dvd (odd_mersenne_succ _)),
sigma_two_pow_eq_mersenne_succ],
{ simp [pr, nat.prime_two] },
{ apply mul_pos (pow_pos _ k) (mersenne_pos (nat.succ_pos k)),
norm_num }
end
lemma ne_zero_of_prime_mersenne (k : ℕ) (pr : (mersenne (k + 1)).prime) :
k ≠ 0 :=
begin
intro H,
simpa [H, mersenne, not_prime_one] using pr,
end
theorem even_two_pow_mul_mersenne_of_prime (k : ℕ) (pr : (mersenne (k + 1)).prime) :
even ((2 ^ k) * mersenne (k + 1)) :=
by simp [ne_zero_of_prime_mersenne k pr] with parity_simps
lemma eq_two_pow_mul_odd {n : ℕ} (hpos : 0 < n) :
∃ (k m : ℕ), n = 2 ^ k * m ∧ ¬ even m :=
begin
have h := (multiplicity.finite_nat_iff.2 ⟨nat.prime_two.ne_one, hpos⟩),
cases multiplicity.pow_multiplicity_dvd h with m hm,
use [(multiplicity 2 n).get h, m],
refine ⟨hm, _⟩,
rw even_iff_two_dvd,
have hg := multiplicity.is_greatest' h (nat.lt_succ_self _),
contrapose! hg,
rcases hg with ⟨k, rfl⟩,
apply dvd.intro k,
rw [pow_succ', mul_assoc, ← hm],
end
/-- **Perfect Number Theorem**: Euler's theorem that even perfect numbers can be factored as a
power of two times a Mersenne prime. -/
theorem eq_two_pow_mul_prime_mersenne_of_even_perfect {n : ℕ} (ev : even n) (perf : perfect n) :
∃ (k : ℕ), prime (mersenne (k + 1)) ∧ n = 2 ^ k * mersenne (k + 1) :=
begin
have hpos := perf.2,
rcases eq_two_pow_mul_odd hpos with ⟨k, m, rfl, hm⟩,
use k,
rw even_iff_two_dvd at hm,
rw [perfect_iff_sum_divisors_eq_two_mul hpos, ← sigma_one_apply,
is_multiplicative_sigma.map_mul_of_coprime (nat.prime_two.coprime_pow_of_not_dvd hm).symm,
sigma_two_pow_eq_mersenne_succ, ← mul_assoc, ← pow_succ] at perf,
rcases nat.coprime.dvd_of_dvd_mul_left
(nat.prime_two.coprime_pow_of_not_dvd (odd_mersenne_succ _)) (dvd.intro _ perf) with ⟨j, rfl⟩,
rw [← mul_assoc, mul_comm _ (mersenne _), mul_assoc] at perf,
have h := mul_left_cancel₀ (ne_of_gt (mersenne_pos (nat.succ_pos _))) perf,
rw [sigma_one_apply, sum_divisors_eq_sum_proper_divisors_add_self, ← succ_mersenne, add_mul,
one_mul, add_comm] at h,
have hj := add_left_cancel h,
cases sum_proper_divisors_dvd (by { rw hj, apply dvd.intro_left (mersenne (k + 1)) rfl }),
{ have j1 : j = 1 := eq.trans hj.symm h_1,
rw [j1, mul_one, sum_proper_divisors_eq_one_iff_prime] at h_1,
simp [h_1, j1] },
{ have jcon := eq.trans hj.symm h_1,
rw [← one_mul j, ← mul_assoc, mul_one] at jcon,
have jcon2 := mul_right_cancel₀ _ jcon,
{ exfalso,
cases k,
{ apply hm,
rw [← jcon2, pow_zero, one_mul, one_mul] at ev,
rw [← jcon2, one_mul],
exact even_iff_two_dvd.mp ev },
apply ne_of_lt _ jcon2,
rw [mersenne, ← nat.pred_eq_sub_one, lt_pred_iff, ← pow_one (nat.succ 1)],
apply pow_lt_pow (nat.lt_succ_self 1) (nat.succ_lt_succ (nat.succ_pos k)) },
contrapose! hm,
simp [hm] }
end
/-- The Euclid-Euler theorem characterizing even perfect numbers -/
theorem even_and_perfect_iff {n : ℕ} :
(even n ∧ perfect n) ↔ ∃ (k : ℕ), prime (mersenne (k + 1)) ∧ n = 2 ^ k * mersenne (k + 1) :=
begin
split,
{ rintro ⟨ev, perf⟩,
exact eq_two_pow_mul_prime_mersenne_of_even_perfect ev perf },
{ rintro ⟨k, pr, rfl⟩,
exact ⟨even_two_pow_mul_mersenne_of_prime k pr, perfect_two_pow_mul_mersenne_of_prime k pr⟩ }
end
end nat
|
c85ab419fea4b36257bede624642673c523a0ae2 | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/analysis/normed/group/ball_sphere.lean | 6ff647c77a40085b1626ec6f7dffe49b7c64cba4 | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 1,876 | lean | /-
Copyright (c) 2022 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Heather Macbeth
-/
import analysis.normed.group.basic
/-!
# Negation on spheres and balls
In this file we define `has_involutive_neg` instances for spheres, open balls, and closed balls in a
semi normed group.
-/
open metric set
variables {E : Type*} [seminormed_add_comm_group E] {r : ℝ}
/-- We equip the sphere, in a seminormed group, with a formal operation of negation, namely the
antipodal map. -/
instance : has_involutive_neg (sphere (0 : E) r) :=
{ neg := λ w, ⟨-↑w, by simp⟩,
neg_neg := λ x, subtype.ext $ neg_neg x }
@[simp] lemma coe_neg_sphere {r : ℝ} (v : sphere (0 : E) r) :
↑(-v) = (-v : E) :=
rfl
instance : has_continuous_neg (sphere (0 : E) r) :=
⟨continuous_subtype_mk _ continuous_subtype_coe.neg⟩
/-- We equip the ball, in a seminormed group, with a formal operation of negation, namely the
antipodal map. -/
instance {r : ℝ} : has_involutive_neg (ball (0 : E) r) :=
{ neg := λ w, ⟨-↑w, by simpa using w.coe_prop⟩,
neg_neg := λ x, subtype.ext $ neg_neg x }
@[simp] lemma coe_neg_ball {r : ℝ} (v : ball (0 : E) r) :
↑(-v) = (-v : E) :=
rfl
instance : has_continuous_neg (ball (0 : E) r) :=
⟨continuous_subtype_mk _ continuous_subtype_coe.neg⟩
/-- We equip the closed ball, in a seminormed group, with a formal operation of negation, namely the
antipodal map. -/
instance {r : ℝ} : has_involutive_neg (closed_ball (0 : E) r) :=
{ neg := λ w, ⟨-↑w, by simpa using w.coe_prop⟩,
neg_neg := λ x, subtype.ext $ neg_neg x }
@[simp] lemma coe_neg_closed_ball {r : ℝ} (v : closed_ball (0 : E) r) :
↑(-v) = (-v : E) :=
rfl
instance : has_continuous_neg (closed_ball (0 : E) r) :=
⟨continuous_subtype_mk _ continuous_subtype_coe.neg⟩
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.