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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
62bc8f1a817baf32320b592e9a7090dc66503f3a | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/data/real/cau_seq_completion.lean | c71b1028d0bfbfbf782f8144482e2a449c74ae07 | [
"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 | 12,459 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Robert Y. Lewis
-/
import data.real.cau_seq
/-!
# Cauchy completion
This file generalizes the Cauchy completion of `(ℚ, abs)` to the completion of a ring
with absolute value.
-/
namespace cau_seq.completion
open cau_seq
section
parameters {α : Type*} [linear_ordered_field α]
parameters {β : Type*} [ring β] {abv : β → α} [is_absolute_value abv]
/-- The Cauchy completion of a ring with absolute value. -/
def Cauchy := @quotient (cau_seq _ abv) cau_seq.equiv
/-- The map from Cauchy sequences into the Cauchy completion. -/
def mk : cau_seq _ abv → Cauchy := quotient.mk
@[simp] theorem mk_eq_mk (f) : @eq Cauchy ⟦f⟧ (mk f) := rfl
theorem mk_eq {f g} : mk f = mk g ↔ f ≈ g := quotient.eq
/-- The map from the original ring into the Cauchy completion. -/
def of_rat (x : β) : Cauchy := mk (const abv x)
instance : has_zero Cauchy := ⟨of_rat 0⟩
instance : has_one Cauchy := ⟨of_rat 1⟩
instance : inhabited Cauchy := ⟨0⟩
theorem of_rat_zero : of_rat 0 = 0 := rfl
theorem of_rat_one : of_rat 1 = 1 := rfl
@[simp] theorem mk_eq_zero {f} : mk f = 0 ↔ lim_zero f :=
by have : mk f = 0 ↔ lim_zero (f - 0) := quotient.eq;
rwa sub_zero at this
instance : has_add Cauchy :=
⟨quotient.map₂ (+) $ λ f₁ g₁ hf f₂ g₂ hg, add_equiv_add hf hg⟩
@[simp] theorem mk_add (f g : cau_seq β abv) : mk f + mk g = mk (f + g) := rfl
instance : has_neg Cauchy :=
⟨quotient.map has_neg.neg $ λ f₁ f₂ hf, neg_equiv_neg hf⟩
@[simp] theorem mk_neg (f : cau_seq β abv) : -mk f = mk (-f) := rfl
instance : has_mul Cauchy :=
⟨quotient.map₂ (*) $ λ f₁ g₁ hf f₂ g₂ hg, mul_equiv_mul hf hg⟩
@[simp] theorem mk_mul (f g : cau_seq β abv) : mk f * mk g = mk (f * g) := rfl
instance : has_sub Cauchy :=
⟨quotient.map₂ has_sub.sub $ λ f₁ g₁ hf f₂ g₂ hg, sub_equiv_sub hf hg⟩
@[simp] theorem mk_sub (f g : cau_seq β abv) : mk f - mk g = mk (f - g) := rfl
theorem of_rat_add (x y : β) : of_rat (x + y) = of_rat x + of_rat y :=
congr_arg mk (const_add _ _)
theorem of_rat_neg (x : β) : of_rat (-x) = -of_rat x :=
congr_arg mk (const_neg _)
theorem of_rat_mul (x y : β) : of_rat (x * y) = of_rat x * of_rat y :=
congr_arg mk (const_mul _ _)
private lemma zero_def : 0 = mk 0 := rfl
private lemma one_def : 1 = mk 1 := rfl
instance : add_group Cauchy :=
by refine { add := (+), zero := (0 : Cauchy), sub := has_sub.sub, neg := has_neg.neg,
sub_eq_add_neg := _, nsmul := nsmul_rec, zsmul := zsmul_rec, .. }; try { intros; refl };
{ repeat {refine λ a, quotient.induction_on a (λ _, _)},
simp [zero_def, add_comm, add_left_comm, sub_eq_neg_add] }
instance : add_group_with_one Cauchy :=
{ nat_cast := λ n, mk n,
nat_cast_zero := congr_arg mk nat.cast_zero,
nat_cast_succ := λ n, congr_arg mk (nat.cast_succ n),
int_cast := λ n, mk n,
int_cast_of_nat := λ n, congr_arg mk (int.cast_of_nat n),
int_cast_neg_succ_of_nat := λ n, congr_arg mk (int.cast_neg_succ_of_nat n),
one := 1,
.. Cauchy.add_group }
@[simp] theorem of_rat_nat_cast (n : ℕ) : of_rat n = n := rfl
@[simp] theorem of_rat_int_cast (z : ℤ) : of_rat z = z := rfl
instance : ring Cauchy :=
by refine { add := (+), zero := (0 : Cauchy), mul := (*), one := 1, npow := npow_rec,
.. Cauchy.add_group_with_one, .. }; try { intros; refl };
{ repeat {refine λ a, quotient.induction_on a (λ _, _)},
simp [zero_def, one_def, mul_add, add_mul, add_comm, add_left_comm, sub_eq_add_neg, ←mul_assoc] }
/-- `cau_seq.completion.of_rat` as a `ring_hom` -/
@[simps]
def of_rat_ring_hom : β →+* Cauchy :=
{ to_fun := of_rat,
map_zero' := of_rat_zero,
map_one' := of_rat_one,
map_add' := of_rat_add,
map_mul' := of_rat_mul, }
theorem of_rat_sub (x y : β) : of_rat (x - y) = of_rat x - of_rat y :=
congr_arg mk (const_sub _ _)
end
section
parameters {α : Type*} [linear_ordered_field α]
parameters {β : Type*} [comm_ring β] {abv : β → α} [is_absolute_value abv]
local notation `Cauchy` := @Cauchy _ _ _ _ abv _
instance : comm_ring Cauchy :=
{ mul_comm := quotient.ind₂ $ by exact λ a b, congr_arg quotient.mk $ mul_comm a b,
..Cauchy.ring }
end
open_locale classical
section
parameters {α : Type*} [linear_ordered_field α]
parameters {β : Type*} [division_ring β] {abv : β → α} [is_absolute_value abv]
local notation `Cauchy` := @Cauchy _ _ _ _ abv _
instance : has_rat_cast Cauchy := ⟨λ q, of_rat q⟩
@[simp] theorem of_rat_rat_cast (q : ℚ) : of_rat (↑q : β) = (q : Cauchy) := rfl
noncomputable instance : has_inv Cauchy :=
⟨λ x, quotient.lift_on x
(λ f, mk $ if h : lim_zero f then 0 else inv f h) $
λ f g fg, begin
have := lim_zero_congr fg,
by_cases hf : lim_zero f,
{ simp [hf, this.1 hf, setoid.refl] },
{ have hg := mt this.2 hf, simp [hf, hg],
have If : mk (inv f hf) * mk f = 1 := mk_eq.2 (inv_mul_cancel hf),
have Ig : mk (inv g hg) * mk g = 1 := mk_eq.2 (inv_mul_cancel hg),
have Ig' : mk g * mk (inv g hg) = 1 := mk_eq.2 (mul_inv_cancel hg),
rw [mk_eq.2 fg, ← Ig] at If,
rw [← mul_one (mk (inv f hf)), ← Ig', ← mul_assoc, If,
mul_assoc, Ig', mul_one] }
end⟩
@[simp] theorem inv_zero : (0 : Cauchy)⁻¹ = 0 :=
congr_arg mk $ by rw dif_pos; [refl, exact zero_lim_zero]
@[simp] theorem inv_mk {f} (hf) : (@mk α _ β _ abv _ f)⁻¹ = mk (inv f hf) :=
congr_arg mk $ by rw dif_neg
lemma cau_seq_zero_ne_one : ¬ (0 : cau_seq _ abv) ≈ 1 := λ h,
have lim_zero (1 - 0), from setoid.symm h,
have lim_zero 1, by simpa,
one_ne_zero $ const_lim_zero.1 this
lemma zero_ne_one : (0 : Cauchy) ≠ 1 :=
λ h, cau_seq_zero_ne_one $ mk_eq.1 h
protected theorem inv_mul_cancel {x : Cauchy} : x ≠ 0 → x⁻¹ * x = 1 :=
quotient.induction_on x $ λ f hf, begin
simp at hf, simp [hf],
exact quotient.sound (cau_seq.inv_mul_cancel hf)
end
protected theorem mul_inv_cancel {x : Cauchy} : x ≠ 0 → x * x⁻¹ = 1 :=
quotient.induction_on x $ λ f hf, begin
simp at hf, simp [hf],
exact quotient.sound (cau_seq.mul_inv_cancel hf)
end
theorem of_rat_inv (x : β) : of_rat (x⁻¹) = ((of_rat x)⁻¹ : Cauchy) :=
congr_arg mk $ by split_ifs with h; [simp [const_lim_zero.1 h], refl]
/-- The Cauchy completion forms a division ring. -/
noncomputable instance : division_ring Cauchy :=
{ inv := has_inv.inv,
mul_inv_cancel := λ x, cau_seq.completion.mul_inv_cancel,
exists_pair_ne := ⟨0, 1, zero_ne_one⟩,
inv_zero := inv_zero,
rat_cast := λ q, of_rat q,
rat_cast_mk := λ n d hd hnd,
by rw [rat.cast_mk', of_rat_mul, of_rat_int_cast, of_rat_inv, of_rat_nat_cast],
.. Cauchy.ring }
theorem of_rat_div (x y : β) : of_rat (x / y) = (of_rat x / of_rat y : Cauchy) :=
by simp only [div_eq_mul_inv, of_rat_inv, of_rat_mul]
/-- Show the first 10 items of a representative of this equivalence class of cauchy sequences.
The representative chosen is the one passed in the VM to `quot.mk`, so two cauchy sequences
converging to the same number may be printed differently.
-/
meta instance [has_repr β] : has_repr Cauchy :=
{ repr := λ r,
let N := 10, seq := r.unquot in
"(sorry /- " ++ (", ".intercalate $ (list.range N).map $ repr ∘ seq) ++ ", ... -/)" }
end
section
parameters {α : Type*} [linear_ordered_field α]
parameters {β : Type*} [field β] {abv : β → α} [is_absolute_value abv]
local notation `Cauchy` := @Cauchy _ _ _ _ abv _
/-- The Cauchy completion forms a field. -/
noncomputable instance : field Cauchy :=
{ .. Cauchy.division_ring,
.. Cauchy.comm_ring }
end
end cau_seq.completion
variables {α : Type*} [linear_ordered_field α]
namespace cau_seq
section
variables (β : Type*) [ring β] (abv : β → α) [is_absolute_value abv]
/-- A class stating that a ring with an absolute value is complete, i.e. every Cauchy
sequence has a limit. -/
class is_complete : Prop :=
(is_complete : ∀ s : cau_seq β abv, ∃ b : β, s ≈ const abv b)
end
section
variables {β : Type*} [ring β] {abv : β → α} [is_absolute_value abv]
variable [is_complete β abv]
lemma complete : ∀ s : cau_seq β abv, ∃ b : β, s ≈ const abv b :=
is_complete.is_complete
/-- The limit of a Cauchy sequence in a complete ring. Chosen non-computably. -/
noncomputable def lim (s : cau_seq β abv) : β :=
classical.some (complete s)
lemma equiv_lim (s : cau_seq β abv) : s ≈ const abv (lim s) :=
classical.some_spec (complete s)
lemma eq_lim_of_const_equiv {f : cau_seq β abv} {x : β} (h : cau_seq.const abv x ≈ f) : x = lim f :=
const_equiv.mp $ setoid.trans h $ equiv_lim f
lemma lim_eq_of_equiv_const {f : cau_seq β abv} {x : β} (h : f ≈ cau_seq.const abv x) : lim f = x :=
(eq_lim_of_const_equiv $ setoid.symm h).symm
lemma lim_eq_lim_of_equiv {f g : cau_seq β abv} (h : f ≈ g) : lim f = lim g :=
lim_eq_of_equiv_const $ setoid.trans h $ equiv_lim g
@[simp] lemma lim_const (x : β) : lim (const abv x) = x :=
lim_eq_of_equiv_const $ setoid.refl _
lemma lim_add (f g : cau_seq β abv) : lim f + lim g = lim (f + g) :=
eq_lim_of_const_equiv $ show lim_zero (const abv (lim f + lim g) - (f + g)),
by rw [const_add, add_sub_add_comm];
exact add_lim_zero (setoid.symm (equiv_lim f)) (setoid.symm (equiv_lim g))
lemma lim_mul_lim (f g : cau_seq β abv) : lim f * lim g = lim (f * g) :=
eq_lim_of_const_equiv $ show lim_zero (const abv (lim f * lim g) - f * g),
from have h : const abv (lim f * lim g) - f * g = (const abv (lim f) - f) * g
+ const abv (lim f) * (const abv (lim g) - g) :=
by simp [const_mul (lim f), mul_add, add_mul, sub_eq_add_neg, add_comm, add_left_comm],
by rw h; exact add_lim_zero (mul_lim_zero_left _ (setoid.symm (equiv_lim _)))
(mul_lim_zero_right _ (setoid.symm (equiv_lim _)))
lemma lim_mul (f : cau_seq β abv) (x : β) : lim f * x = lim (f * const abv x) :=
by rw [← lim_mul_lim, lim_const]
lemma lim_neg (f : cau_seq β abv) : lim (-f) = -lim f :=
lim_eq_of_equiv_const (show lim_zero (-f - const abv (-lim f)),
by rw [const_neg, sub_neg_eq_add, add_comm, ← sub_eq_add_neg];
exact setoid.symm (equiv_lim f))
lemma lim_eq_zero_iff (f : cau_seq β abv) : lim f = 0 ↔ lim_zero f :=
⟨assume h,
by have hf := equiv_lim f;
rw h at hf;
exact (lim_zero_congr hf).mpr (const_lim_zero.mpr rfl),
assume h,
have h₁ : f = (f - const abv 0) := ext (λ n, by simp [sub_apply, const_apply]),
by rw h₁ at h; exact lim_eq_of_equiv_const h ⟩
end
section
variables {β : Type*} [field β] {abv : β → α} [is_absolute_value abv] [is_complete β abv]
lemma lim_inv {f : cau_seq β abv} (hf : ¬ lim_zero f) : lim (inv f hf) = (lim f)⁻¹ :=
have hl : lim f ≠ 0 := by rwa ← lim_eq_zero_iff at hf,
lim_eq_of_equiv_const $ show lim_zero (inv f hf - const abv (lim f)⁻¹),
from have h₁ : ∀ (g f : cau_seq β abv) (hf : ¬ lim_zero f), lim_zero (g - f * inv f hf * g) :=
λ g f hf, by rw [← one_mul g, ← mul_assoc, ← sub_mul, mul_one, mul_comm, mul_comm f];
exact mul_lim_zero_right _ (setoid.symm (cau_seq.inv_mul_cancel _)),
have h₂ : lim_zero ((inv f hf - const abv (lim f)⁻¹) - (const abv (lim f) - f) *
(inv f hf * const abv (lim f)⁻¹)) :=
by rw [sub_mul, ← sub_add, sub_sub, sub_add_eq_sub_sub, sub_right_comm, sub_add];
exact show lim_zero (inv f hf - const abv (lim f) * (inv f hf * const abv (lim f)⁻¹)
- (const abv (lim f)⁻¹ - f * (inv f hf * const abv (lim f)⁻¹))),
from sub_lim_zero
(by rw [← mul_assoc, mul_right_comm, const_inv hl]; exact h₁ _ _ _)
(by rw [← mul_assoc]; exact h₁ _ _ _),
(lim_zero_congr h₂).mpr $ mul_lim_zero_left _ (setoid.symm (equiv_lim f))
end
section
variables [is_complete α abs]
lemma lim_le {f : cau_seq α abs} {x : α}
(h : f ≤ cau_seq.const abs x) : lim f ≤ x :=
cau_seq.const_le.1 $ cau_seq.le_of_eq_of_le (setoid.symm (equiv_lim f)) h
lemma le_lim {f : cau_seq α abs} {x : α}
(h : cau_seq.const abs x ≤ f) : x ≤ lim f :=
cau_seq.const_le.1 $ cau_seq.le_of_le_of_eq h (equiv_lim f)
lemma lt_lim {f : cau_seq α abs} {x : α}
(h : cau_seq.const abs x < f) : x < lim f :=
cau_seq.const_lt.1 $ cau_seq.lt_of_lt_of_eq h (equiv_lim f)
lemma lim_lt {f : cau_seq α abs} {x : α}
(h : f < cau_seq.const abs x) : lim f < x :=
cau_seq.const_lt.1 $ cau_seq.lt_of_eq_of_lt (setoid.symm (equiv_lim f)) h
end
end cau_seq
|
af1ab41925fd7d73e20f45725536ab4c3984dad9 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/combinatorics/young/semistandard_tableau.lean | 4c1261a411e9debb67bdd0dc3da3a96a2c3b8f49 | [
"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,905 | lean | /-
Copyright (c) 2022 Jake Levinson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jake Levinson
-/
import combinatorics.young.young_diagram
/-!
# Semistandard Young tableaux
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
A semistandard Young tableau is a filling of a Young diagram by natural numbers, such that
the entries are weakly increasing left-to-right along rows (i.e. for fixed `i`), and
strictly-increasing top-to-bottom along columns (i.e. for fixed `j`).
An example of an SSYT of shape `μ = [4, 2, 1]` is:
```text
0 0 0 2
1 1
2
```
We represent an SSYT as a function `ℕ → ℕ → ℕ`, which is required to be zero for all pairs
`(i, j) ∉ μ` and to satisfy the row-weak and column-strict conditions on `μ`.
## Main definitions
- `ssyt (μ : young_diagram)` : semistandard Young tableaux of shape `μ`. There is
a `has_coe_to_fun` instance such that `T i j` is value of the `(i, j)` entry of the SSYT `T`.
- `ssyt.highest_weight (μ : young_diagram)`: the semistandard Young tableau whose `i`th row
consists entirely of `i`s, for each `i`.
## Tags
Semistandard Young tableau
## References
<https://en.wikipedia.org/wiki/Young_tableau>
-/
/-- A semistandard Young tableau (SSYT) is a filling of the cells of a Young diagram by natural
numbers, such that the entries in each row are weakly increasing (left to right), and the entries
in each column are strictly increasing (top to bottom).
Here, an SSYT is represented as an unrestricted function `ℕ → ℕ → ℕ` that, for reasons
of extensionality, is required to vanish outside `μ`. -/
structure ssyt (μ : young_diagram) :=
(entry : ℕ → ℕ → ℕ)
(row_weak' : ∀ {i j1 j2 : ℕ}, j1 < j2 → (i, j2) ∈ μ → entry i j1 ≤ entry i j2)
(col_strict' : ∀ {i1 i2 j : ℕ}, i1 < i2 → (i2, j) ∈ μ → entry i1 j < entry i2 j)
(zeros' : ∀ {i j}, (i, j) ∉ μ → entry i j = 0)
namespace ssyt
instance fun_like {μ : young_diagram} : fun_like (ssyt μ) ℕ (λ _, ℕ → ℕ) :=
{ coe := ssyt.entry,
coe_injective' := λ T T' h, by { cases T, cases T', congr' } }
/-- Helper instance for when there's too many metavariables to apply
`fun_like.has_coe_to_fun` directly. -/
instance {μ : young_diagram} : has_coe_to_fun (ssyt μ) (λ _, ℕ → ℕ → ℕ) :=
fun_like.has_coe_to_fun
@[simp] lemma to_fun_eq_coe {μ : young_diagram} {T : ssyt μ} : T.entry = (T : ℕ → ℕ → ℕ) := rfl
@[ext] theorem ext {μ : young_diagram} {T T' : ssyt μ} (h : ∀ i j, T i j = T' i j) : T = T' :=
fun_like.ext T T' (λ x, by { funext, apply h })
/-- Copy of an `ssyt μ` with a new `entry` equal to the old one. Useful to fix definitional
equalities. -/
protected def copy {μ : young_diagram} (T : ssyt μ) (entry' : ℕ → ℕ → ℕ) (h : entry' = T) :
ssyt μ :=
{ entry := entry',
row_weak' := λ _ _ _, h.symm ▸ T.row_weak',
col_strict' := λ _ _ _, h.symm ▸ T.col_strict',
zeros' := λ _ _, h.symm ▸ T.zeros' }
@[simp] lemma coe_copy {μ : young_diagram} (T : ssyt μ) (entry' : ℕ → ℕ → ℕ) (h : entry' = T) :
⇑(T.copy entry' h) = entry' :=
rfl
lemma copy_eq {μ : young_diagram} (T : ssyt μ) (entry' : ℕ → ℕ → ℕ) (h : entry' = T) :
T.copy entry' h = T :=
fun_like.ext' h
lemma row_weak {μ : young_diagram} (T : ssyt μ) {i j1 j2 : ℕ}
(hj : j1 < j2) (hcell : (i, j2) ∈ μ) : T i j1 ≤ T i j2 :=
T.row_weak' hj hcell
lemma col_strict {μ : young_diagram} (T : ssyt μ) {i1 i2 j : ℕ}
(hi : i1 < i2) (hcell : (i2, j) ∈ μ) : T i1 j < T i2 j :=
T.col_strict' hi hcell
lemma zeros {μ : young_diagram} (T : ssyt μ)
{i j : ℕ} (not_cell : (i, j) ∉ μ) : T i j = 0 := T.zeros' not_cell
lemma row_weak_of_le {μ : young_diagram} (T : ssyt μ) {i j1 j2 : ℕ}
(hj : j1 ≤ j2) (cell : (i, j2) ∈ μ) : T i j1 ≤ T i j2 :=
by { cases eq_or_lt_of_le hj, subst h, exact T.row_weak h cell }
lemma col_weak {μ : young_diagram} (T : ssyt μ) {i1 i2 j : ℕ}
(hi : i1 ≤ i2) (cell : (i2, j) ∈ μ) : T i1 j ≤ T i2 j :=
by { cases eq_or_lt_of_le hi, subst h, exact le_of_lt (T.col_strict h cell) }
/-- The "highest weight" SSYT of a given shape is has all i's in row i, for each i. -/
def highest_weight (μ : young_diagram) : ssyt μ :=
{ entry := λ i j, if (i, j) ∈ μ then i else 0,
row_weak' := λ i j1 j2 hj hcell,
by rw [if_pos hcell, if_pos (μ.up_left_mem (by refl) (le_of_lt hj) hcell)],
col_strict' := λ i1 i2 j hi hcell,
by rwa [if_pos hcell, if_pos (μ.up_left_mem (le_of_lt hi) (by refl) hcell)],
zeros' := λ i j not_cell, if_neg not_cell }
@[simp] lemma highest_weight_apply {μ : young_diagram} {i j : ℕ} :
highest_weight μ i j = if (i, j) ∈ μ then i else 0 := rfl
instance {μ : young_diagram} : inhabited (ssyt μ) := ⟨ssyt.highest_weight μ⟩
end ssyt
|
7ce646e8a8deda074e5b5ca8b473edccdeae091b | f10d66a159ce037d07005bd6021cee6bbd6d5ff0 | /to_finsupp.lean | a40bbcdaa127f42a39f7500eb6a8c88b91cbe564 | [] | no_license | johoelzl/mason-stother | 0c78bca183eb729d7f0f93e87ce073bc8cd8808d | 573ecfaada288176462c03c87b80ad05bdab4644 | refs/heads/master | 1,631,751,973,492 | 1,528,923,934,000 | 1,528,923,934,000 | 109,133,224 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 8,002 | lean | import data.finsupp
universes u v w
open finset
lemma finset.sum_ite_general'' {α : Type u} {β : Type v} [decidable_eq α] [add_comm_monoid β] {f g : α → β} (s : finset α)
(p : α → Prop) [decidable_pred p] :
s.sum (λ z, if p z then f z else g z) = (s.filter p).sum f + (s.filter $ λx, ¬ p x).sum g :=
begin
have h1 : sum (filter p s) (λ (z : α), ite (p z) (f z) (g z)) = (s.filter p).sum f,
{
rw [finset.sum_congr],
exact rfl,
intros x h1,
rw [mem_filter] at h1,
have h2 : p x,
from and.elim_right h1,
rw [if_pos h2]
},
have h2 : sum (filter (λ (a : α), ¬p a) s) (λ (z : α), ite (p z) (f z) (g z)) = (s.filter $ λx, ¬ p x).sum g,
{
rw [finset.sum_congr],
exact rfl,
intros x h1,
rw [mem_filter] at h1,
have h2 : ¬ p x,
from and.elim_right h1,
rw [if_neg h2]
},
rw [←h2],
conv
{
to_lhs,
rw [←@finset.filter_union_filter_neg_eq _ _ _ _ s, finset.sum_union (filter_inter_filter_neg_eq s), h1],
}
end
open classical finsupp finset
local attribute [instance] prop_decidable
namespace polynomial
--This lemma should be placed in finsupp
--Maybe only keep the iff version?
lemma eq_zero_of_support_eq_empty {α : Type u} {β : Type v} [has_zero β] (s : α →₀ β):
s.support = ∅ → s = 0 :=
begin
intro h,
refine @finsupp.ext _ _ _ s 0 _,
intro q,
by_cases h1 : (q ∈ support s),
{
have : q ∉ ∅, from not_mem_empty q,
rw h at h1,
contradiction
},
{
have h2: (s q ≠ 0) → (q ∈ s.support), from iff.elim_right (mem_support_iff s),
rw [←not_imp_not,not_not] at h2,
have : s q = 0, from h2 h1,
have : (0 : α →₀ β) q = 0,
all_goals {simp * at *}
}
end
--This lemma should be placed in finsupp
lemma eq_zero_iff_support_eq_empty {α : Type u} {β : Type v} [has_zero β] (s : α →₀ β):
s = 0 ↔ s.support = ∅ :=
begin
constructor,
{
intro h1,
rw h1,
simp,
},
exact eq_zero_of_support_eq_empty s
end
--This lemma should be placed in finsupp
lemma eq_single_of_support_eq_singleton {α : Type u} {β : Type v} [has_zero β] (s : α →₀ β){q : α}:
s.support = {q} → ∃w : β, s = single q w :=
begin
intro h1,
fapply exists.intro,
apply s q,
refine @finsupp.ext _ _ _ s (single q (s q)) _,
intro w,
by_cases h3 : (w = q),
{
simp * at *,
},
{
have h2 : q ∈ support s,
rw h1,
simp,
simp * at *,
have h4: w ∉ finset.singleton q,
from iff.elim_right not_imp_not (iff.elim_left finset.mem_singleton) h3,
rw ←h1 at h4,
have h5 : ¬ s w ≠ 0,
from iff.elim_right not_imp_not (iff.elim_right (finsupp.mem_support_iff s)) h4,
rw not_not at h5,
have h6: ((single q (s q)) : α → β) w = 0,
simp [single_apply],
apply if_neg,
simp [ne.symm h3],
simp * at *
},
end
--Should be in finsupp --can't add congr attribute, is this a proper congr lemma?
lemma finsupp.sum_congr {α : Type u}{β : Type v}{γ : Type w} [has_zero β] [add_comm_monoid γ] {s₁ s₂ : α →₀ β}{f g : α → β → γ}(h : support s₁ = support s₂) :
(∀x ∈ support s₂, f x (s₁ x) = g x (s₂ x)) → s₁.sum f = s₂.sum g :=
begin
exact finset.sum_congr h
end
--can't add congr attribute, is this a proper congr lemma?
lemma finsupp.sum_congr_2 {α : Type u}{β : Type v}{γ : Type w} [has_zero β] [add_comm_monoid γ] {s₁ s₂ : α →₀ β}{f g : α → β → γ}(h : s₁ = s₂) :
(∀x ∈ support s₂, f x (s₁ x) = g x (s₂ x)) → s₁.sum f = s₂.sum g :=
begin
have h1 : s₁.support = s₂.support,
simp [h],
exact finset.sum_congr h1
end
--Should be placed in finset
lemma finset.sum_ite {α : Type u} {β : Type v} [add_comm_monoid β] {x : α} {y : β} (s : finset α) :
s.sum (λ z, if (z = x) then y else 0) = if (x ∈ s) then y else 0:=
begin
fapply finset.induction_on s,
simp,
intros a s h1a h2,
have h1: finset.sum (insert a s) (λ (z : α), ite (z = x) y 0) = (λ (z : α), ite (z = x) y 0) a + finset.sum (s) (λ (z : α), ite (z = x) y 0),
apply finset.sum_insert,
assumption,
rw h1,
rw h2,
simp,
by_cases h3 :(a = x),
{
simp [*, if_pos],
rw h3 at h1a,
simp [*, if_neg],
},
{
simp [*, if_neg],
by_cases h4 : (x ∈ s),
{
simp [*, if_pos]
},
{
simp [*, if_neg],
have : ¬ x = a,
intro h5,
rw h5 at h3,
have : a = a,
simp,
contradiction,
simp [*, if_neg]
}
}
end
--Should be placed in finset -should the more specific lemma be local?
lemma finset.sum_ite_general {α : Type u} {β : Type v} [add_comm_monoid β] {x : α} {f : α → β} (s : finset α) :
s.sum (λ z, if (z = x) then f z else 0) = if (x ∈ s) then f x else 0:=
begin
have : s.sum (λ z, if (z = x) then f z else 0) = s.sum (λ z, if (z = x) then f x else 0),
apply finset.sum_congr,
simp,
intros y h1,
by_cases h2: (y = x),
{
simp [*, if_pos]
},
{
simp [*, if_neg]
},
rw this,
apply @finset.sum_ite _ _ _ x (f x) s,
end
-- should be placed in finsupp
lemma finsupp.sum_ite {α : Type u}{β : Type v}{γ : Type w} [has_zero β] [add_comm_monoid γ] {x : α}{s : α →₀ β} {f : α → β → γ} :
s.sum (λ a b, if (a = x) then f a b else 0) = if (x ∈ s.support) then f x (s x) else 0 :=
begin
unfold finsupp.sum,
refine @finset.sum_ite_general _ _ _ x (λ y, f y (s y)) s.support
end
--should be placed in finsupp
lemma finsupp.sum_mul {α : Type u}{β : Type v}{γ : Type w} [semiring β] [semiring γ] {b : γ} {s : α →₀ β} {f : α → β → γ} :
(s.sum f) * b = s.sum (λ a c, (f a (s a)) * b) :=
by simp [finsupp.sum,finset.sum_mul]
--should be placed in finsupp
lemma finsupp.mul_sum {α : Type u}{β : Type v}{γ : Type w} [semiring β] [semiring γ] {b : γ} {s : α →₀ β} {f : α → β → γ} :
b * (s.sum f) = s.sum (λ a c, b * (f a (s a))) :=
by simp [finsupp.sum,finset.mul_sum]
--should be placed in finset
lemma erase_insert_eq_insert_erase {α : Type u} {s : finset α} {a b : α} (h : a ≠ b) :
erase (insert a s) b = insert a (erase s b) :=
finset.ext.mpr begin intro c, by_cases c = a; by_cases c = b; simp [h, *] at * end
--finsupp as multiset
open finsupp
def to_finsupp_pow_min_one {α : Type u} (p : α →₀ ℕ) : α →₀ ℕ := map_range (λ n, n - 1) (by {simp}) p
--Easily made more general
lemma support_pow_min_one_subset_support {α : Type u} {f : α →₀ ℕ} : (finsupp.support ((finsupp.map_range (λ (n : ℕ), n - 1)(by {simp}) f))) ⊆ (finsupp.support f) :=
begin
simp only [has_subset.subset],
intros a h1,
rw [mem_support_iff] at h1,
rw mem_support_iff,
simp at h1,
by_contradiction h2,
simp at h2,
rw h2 at h1,
rw [nat.zero_sub 1] at h1,
exact h1 rfl,
end
lemma mem_sdiff_support_support_pow_min_one_iff_eq_one {α : Type u} {f : α →₀ ℕ} : ∀x, x ∈ support f \ support (to_finsupp_pow_min_one f) ↔ f x = 1 :=
begin
intros x,
constructor,
{
intro h1,
rw mem_sdiff at h1,
have h2 : x ∈ support f,
exact and.elim_left h1,
have h3 : x ∉ support (to_finsupp_pow_min_one f),
exact and.elim_right h1,
have h4 : f x ≠ 0,
{rw mem_support_iff at h2, exact h2},
rw mem_support_iff at h3,
simp at h3,
rw [to_finsupp_pow_min_one, map_range_apply] at h3,
have h5 : f x ≥ 1,
{
have h5 : 0 < f x,
from nat.pos_of_ne_zero h4,
have h5 : nat.succ 0 ≤ f x,
from nat.succ_le_of_lt h5,
exact h5
},
have h6 : f x - 1 + 1 = 1,
{simp [h3]},
rw nat.sub_add_cancel h5 at h6,
exact h6
},
{
intros h1,
have h2 : x ∈ support f,
{rw finsupp.mem_support_iff, simp [*]},
have h3 : x ∉ support (to_finsupp_pow_min_one f),
{
by_contradiction h3,
rw [finsupp.mem_support_iff, to_finsupp_pow_min_one, map_range_apply] at h3,
have : f x - 1 = 0,
{simp [h1]},
contradiction
},
simp *
}
end
end polynomial
|
07113febf94c3dbbe301bd8bc1e1e2fbbd2d260c | d5bef83c55d40cb88f9a01b755c882a93348a847 | /library/init/logic.lean | 1a1e510daf4b97520b7649a40eb47064bc38846f | [
"Apache-2.0"
] | permissive | urkud/lean | 587d78216e1f0c7f651566e9e92cf8ade285d58d | 3526539070ea6268df5dd373deeb3ac8b9621952 | refs/heads/master | 1,660,171,634,921 | 1,657,873,466,000 | 1,657,873,466,000 | 249,789,456 | 0 | 0 | Apache-2.0 | 1,585,075,263,000 | 1,585,075,263,000 | null | UTF-8 | Lean | false | false | 38,878 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad, Floris van Doorn
-/
prelude
import init.core
universes u v w
@[simp] lemma opt_param_eq (α : Sort u) (default : α) : opt_param α default = α :=
rfl
@[inline] def id {α : Sort u} (a : α) : α := a
def flip {α : Sort u} {β : Sort v} {φ : Sort w} (f : α → β → φ) : β → α → φ :=
λ b a, f a b
/- implication -/
def implies (a b : Prop) := a → b
/-- Implication `→` is transitive. If `P → Q` and `Q → R` then `P → R`. -/
@[trans] lemma implies.trans {p q r : Prop} (h₁ : implies p q) (h₂ : implies q r) : implies p r :=
assume hp, h₂ (h₁ hp)
lemma trivial : true := ⟨⟩
/-- We can't have `a` and `¬a`, that would be absurd!-/
@[inline] def absurd {a : Prop} {b : Sort v} (h₁ : a) (h₂ : ¬a) : b :=
false.rec b (h₂ h₁)
lemma not.intro {a : Prop} (h : a → false) : ¬ a :=
h
/-- Modus tollens. If an implication is true, then so is its contrapositive. -/
lemma mt {a b : Prop} (h₁ : a → b) (h₂ : ¬b) : ¬a := assume ha : a, h₂ (h₁ ha)
/- not -/
lemma not_false : ¬false := id
def non_contradictory (a : Prop) : Prop := ¬¬a
lemma non_contradictory_intro {a : Prop} (ha : a) : ¬¬a :=
assume hna : ¬a, absurd ha hna
/- false -/
@[inline] def false.elim {C : Sort u} (h : false) : C :=
false.rec C h
/- eq -/
-- proof irrelevance is built in
lemma proof_irrel {a : Prop} (h₁ h₂ : a) : h₁ = h₂ := rfl
@[simp] lemma id.def {α : Sort u} (a : α) : id a = a := rfl
@[inline] def eq.mp {α β : Sort u} : (α = β) → α → β :=
eq.rec_on
@[inline] def eq.mpr {α β : Sort u} : (α = β) → β → α :=
λ h₁ h₂, eq.rec_on (eq.symm h₁) h₂
@[elab_as_eliminator]
lemma eq.substr {α : Sort u} {p : α → Prop} {a b : α} (h₁ : b = a) : p a → p b :=
eq.subst (eq.symm h₁)
lemma congr {α : Sort u} {β : Sort v} {f₁ f₂ : α → β} {a₁ a₂ : α} (h₁ : f₁ = f₂) (h₂ : a₁ = a₂) : f₁ a₁ = f₂ a₂ :=
eq.subst h₁ (eq.subst h₂ rfl)
lemma congr_fun {α : Sort u} {β : α → Sort v} {f g : Π x, β x} (h : f = g) (a : α) : f a = g a :=
eq.subst h (eq.refl (f a))
lemma congr_arg {α : Sort u} {β : Sort v} {a₁ a₂ : α} (f : α → β) : a₁ = a₂ → f a₁ = f a₂ :=
congr rfl
lemma trans_rel_left {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : r a b) (h₂ : b = c) : r a c :=
h₂ ▸ h₁
lemma trans_rel_right {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : a = b) (h₂ : r b c) : r a c :=
h₁.symm ▸ h₂
lemma of_eq_true {p : Prop} (h : p = true) : p :=
h.symm ▸ trivial
lemma not_of_eq_false {p : Prop} (h : p = false) : ¬p :=
assume hp, h ▸ hp
@[inline] def cast {α β : Sort u} (h : α = β) (a : α) : β :=
eq.rec a h
lemma cast_proof_irrel {α β : Sort u} (h₁ h₂ : α = β) (a : α) : cast h₁ a = cast h₂ a := rfl
lemma cast_eq {α : Sort u} (h : α = α) (a : α) : cast h a = a := rfl
/- ne -/
@[reducible] def ne {α : Sort u} (a b : α) := ¬(a = b)
infix ` ≠ `:50 := ne
@[simp] lemma ne.def {α : Sort u} (a b : α) : a ≠ b = ¬ (a = b) := rfl
namespace ne
variable {α : Sort u}
variables {a b : α}
lemma intro (h : a = b → false) : a ≠ b := h
lemma elim (h : a ≠ b) : a = b → false := h
lemma irrefl (h : a ≠ a) : false := h rfl
lemma symm (h : a ≠ b) : b ≠ a :=
assume (h₁ : b = a), h (h₁.symm)
end ne
lemma false_of_ne {α : Sort u} {a : α} : a ≠ a → false := ne.irrefl
section
variables {p : Prop}
lemma ne_false_of_self : p → p ≠ false :=
assume (hp : p) (heq : p = false), heq ▸ hp
lemma ne_true_of_not : ¬p → p ≠ true :=
assume (hnp : ¬p) (heq : p = true), (heq ▸ hnp) trivial
lemma true_ne_false : ¬true = false :=
ne_false_of_self trivial
end
attribute [refl] heq.refl
section
variables {α β φ : Sort u} {a a' : α} {b b' : β} {c : φ}
def heq.elim {α : Sort u} {a : α} {p : α → Sort v} {b : α} (h₁ : a == b) : p a → p b :=
eq.rec_on (eq_of_heq h₁)
lemma heq.subst {p : ∀ T : Sort u, T → Prop} : a == b → p α a → p β b :=
heq.rec_on
@[symm] lemma heq.symm (h : a == b) : b == a :=
heq.rec_on h (heq.refl a)
lemma heq_of_eq (h : a = a') : a == a' :=
eq.subst h (heq.refl a)
@[trans] lemma heq.trans (h₁ : a == b) (h₂ : b == c) : a == c :=
heq.subst h₂ h₁
@[trans] lemma heq_of_heq_of_eq (h₁ : a == b) (h₂ : b = b') : a == b' :=
heq.trans h₁ (heq_of_eq h₂)
@[trans] lemma heq_of_eq_of_heq (h₁ : a = a') (h₂ : a' == b) : a == b :=
heq.trans (heq_of_eq h₁) h₂
lemma type_eq_of_heq (h : a == b) : α = β :=
heq.rec_on h (eq.refl α)
end
lemma eq_rec_heq {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} (h : a = a') (p : φ a), (eq.rec_on h p : φ a') == p
| a _ rfl p := heq.refl p
lemma heq_of_eq_rec_left {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} {p₁ : φ a} {p₂ : φ a'} (e : a = a') (h₂ : (eq.rec_on e p₁ : φ a') = p₂), p₁ == p₂
| a _ p₁ p₂ rfl h := eq.rec_on h (heq.refl p₁)
lemma heq_of_eq_rec_right {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} {p₁ : φ a} {p₂ : φ a'} (e : a' = a) (h₂ : p₁ = eq.rec_on e p₂), p₁ == p₂
| a _ p₁ p₂ rfl h :=
have p₁ = p₂, from h,
this ▸ heq.refl p₁
lemma of_heq_true {a : Prop} (h : a == true) : a :=
of_eq_true (eq_of_heq h)
lemma eq_rec_compose : ∀ {α β φ : Sort u} (p₁ : β = φ) (p₂ : α = β) (a : α), (eq.rec_on p₁ (eq.rec_on p₂ a : β) : φ) = eq.rec_on (eq.trans p₂ p₁) a
| α _ _ rfl rfl a := rfl
lemma cast_heq : ∀ {α β : Sort u} (h : α = β) (a : α), cast h a == a
| α _ rfl a := heq.refl a
/- and -/
infixr ` /\ `:35 := and
infixr ` ∧ `:35 := and
variables {a b c d : Prop}
lemma and.elim (h₁ : a ∧ b) (h₂ : a → b → c) : c :=
and.rec h₂ h₁
lemma and.swap : a ∧ b → b ∧ a :=
assume ⟨ha, hb⟩, ⟨hb, ha⟩
lemma and.symm : a ∧ b → b ∧ a := and.swap
/- or -/
infixr ` \/ `:30 := or
infixr ` ∨ `:30 := or
namespace or
lemma elim (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → c) : c :=
or.rec h₂ h₃ h₁
end or
lemma non_contradictory_em (a : Prop) : ¬¬(a ∨ ¬a) :=
assume not_em : ¬(a ∨ ¬a),
have neg_a : ¬a, from
assume pos_a : a, absurd (or.inl pos_a) not_em,
absurd (or.inr neg_a) not_em
lemma or.swap : a ∨ b → b ∨ a := or.rec or.inr or.inl
lemma or.symm : a ∨ b → b ∨ a := or.swap
/- xor -/
def xor (a b : Prop) := (a ∧ ¬ b) ∨ (b ∧ ¬ a)
/- iff -/
/-- `iff P Q`, with notation `P ↔ Q`, is the proposition asserting that `P` and `Q` are equivalent,
that is, have the same truth value. -/
structure iff (a b : Prop) : Prop :=
intro :: (mp : a → b) (mpr : b → a)
infix ` <-> `:20 := iff
infix ` ↔ `:20 := iff
lemma iff.elim : ((a → b) → (b → a) → c) → (a ↔ b) → c := iff.rec
attribute [recursor 5] iff.elim
lemma iff.elim_left : (a ↔ b) → a → b := iff.mp
lemma iff.elim_right : (a ↔ b) → b → a := iff.mpr
lemma iff_iff_implies_and_implies (a b : Prop) : (a ↔ b) ↔ (a → b) ∧ (b → a) :=
iff.intro (λ h, and.intro h.mp h.mpr) (λ h, iff.intro h.left h.right)
@[refl]
lemma iff.refl (a : Prop) : a ↔ a :=
iff.intro (assume h, h) (assume h, h)
lemma iff.rfl {a : Prop} : a ↔ a :=
iff.refl a
@[trans]
lemma iff.trans (h₁ : a ↔ b) (h₂ : b ↔ c) : a ↔ c :=
iff.intro
(assume ha, iff.mp h₂ (iff.mp h₁ ha))
(assume hc, iff.mpr h₁ (iff.mpr h₂ hc))
@[symm]
lemma iff.symm (h : a ↔ b) : b ↔ a :=
iff.intro (iff.elim_right h) (iff.elim_left h)
lemma iff.comm : (a ↔ b) ↔ (b ↔ a) :=
iff.intro iff.symm iff.symm
lemma eq.to_iff {a b : Prop} (h : a = b) : a ↔ b :=
eq.rec_on h iff.rfl
lemma neq_of_not_iff {a b : Prop} : ¬(a ↔ b) → a ≠ b :=
λ h₁ h₂,
have a ↔ b, from eq.subst h₂ (iff.refl a),
absurd this h₁
lemma not_iff_not_of_iff (h₁ : a ↔ b) : ¬a ↔ ¬b :=
iff.intro
(assume (hna : ¬ a) (hb : b), hna (iff.elim_right h₁ hb))
(assume (hnb : ¬ b) (ha : a), hnb (iff.elim_left h₁ ha))
lemma of_iff_true (h : a ↔ true) : a :=
iff.mp (iff.symm h) trivial
lemma not_of_iff_false : (a ↔ false) → ¬a := iff.mp
lemma iff_true_intro (h : a) : a ↔ true :=
iff.intro
(λ hl, trivial)
(λ hr, h)
lemma iff_false_intro (h : ¬a) : a ↔ false :=
iff.intro h (false.rec a)
lemma not_non_contradictory_iff_absurd (a : Prop) : ¬¬¬a ↔ ¬a :=
iff.intro
(λ (hl : ¬¬¬a) (ha : a), hl (non_contradictory_intro ha))
absurd
lemma imp_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a → b) ↔ (c → d) :=
iff.intro
(λ hab hc, iff.mp h₂ (hab (iff.mpr h₁ hc)))
(λ hcd ha, iff.mpr h₂ (hcd (iff.mp h₁ ha)))
lemma imp_congr_ctx (h₁ : a ↔ c) (h₂ : c → (b ↔ d)) : (a → b) ↔ (c → d) :=
iff.intro
(λ hab hc, have ha : a, from iff.mpr h₁ hc,
have hb : b, from hab ha,
iff.mp (h₂ hc) hb)
(λ hcd ha, have hc : c, from iff.mp h₁ ha,
have hd : d, from hcd hc,
iff.mpr (h₂ hc) hd)
lemma imp_congr_right (h : a → (b ↔ c)) : (a → b) ↔ (a → c) :=
iff.intro
(assume hab ha, iff.elim_left (h ha) (hab ha))
(assume hab ha, iff.elim_right (h ha) (hab ha))
lemma not_not_intro (ha : a) : ¬¬a :=
assume hna : ¬a, hna ha
lemma not_of_not_not_not (h : ¬¬¬a) : ¬a :=
λ ha, absurd (not_not_intro ha) h
@[simp] lemma not_true : (¬ true) ↔ false :=
iff_false_intro (not_not_intro trivial)
@[simp] lemma not_false_iff : (¬ false) ↔ true :=
iff_true_intro not_false
@[congr] lemma not_congr (h : a ↔ b) : ¬a ↔ ¬b :=
iff.intro (λ h₁ h₂, h₁ (iff.mpr h h₂)) (λ h₁ h₂, h₁ (iff.mp h h₂))
lemma ne_self_iff_false {α : Sort u} (a : α) : (not (a = a)) ↔ false :=
iff.intro false_of_ne false.elim
@[simp] lemma eq_self_iff_true {α : Sort u} (a : α) : (a = a) ↔ true :=
iff_true_intro rfl
lemma heq_self_iff_true {α : Sort u} (a : α) : (a == a) ↔ true :=
iff_true_intro (heq.refl a)
@[simp] lemma iff_not_self (a : Prop) : (a ↔ ¬a) ↔ false :=
iff_false_intro (λ h,
have h' : ¬a, from (λ ha, (iff.mp h ha) ha),
h' (iff.mpr h h'))
@[simp] lemma not_iff_self (a : Prop) : (¬a ↔ a) ↔ false :=
iff_false_intro (λ h,
have h' : ¬a, from (λ ha, (iff.mpr h ha) ha),
h' (iff.mp h h'))
lemma true_iff_false : (true ↔ false) ↔ false :=
iff_false_intro (λ h, iff.mp h trivial)
lemma false_iff_true : (false ↔ true) ↔ false :=
iff_false_intro (λ h, iff.mpr h trivial)
lemma false_of_true_iff_false : (true ↔ false) → false :=
assume h, iff.mp h trivial
lemma false_of_true_eq_false : (true = false) → false :=
assume h, h ▸ trivial
lemma true_eq_false_of_false : false → (true = false) :=
false.elim
lemma eq_comm {α : Sort u} {a b : α} : a = b ↔ b = a :=
⟨eq.symm, eq.symm⟩
/- and simp rules -/
lemma and.imp (hac : a → c) (hbd : b → d) : a ∧ b → c ∧ d :=
assume ⟨ha, hb⟩, ⟨hac ha, hbd hb⟩
lemma and_implies (hac : a → c) (hbd : b → d) : a ∧ b → c ∧ d := and.imp hac hbd
@[congr] lemma and_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ∧ b) ↔ (c ∧ d) :=
iff.intro (and.imp (iff.mp h₁) (iff.mp h₂)) (and.imp (iff.mpr h₁) (iff.mpr h₂))
lemma and_congr_right (h : a → (b ↔ c)) : (a ∧ b) ↔ (a ∧ c) :=
iff.intro
(assume ⟨ha, hb⟩, ⟨ha, iff.elim_left (h ha) hb⟩)
(assume ⟨ha, hc⟩, ⟨ha, iff.elim_right (h ha) hc⟩)
lemma and.comm : a ∧ b ↔ b ∧ a :=
iff.intro and.swap and.swap
lemma and_comm (a b : Prop) : a ∧ b ↔ b ∧ a := and.comm
lemma and.assoc : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) :=
iff.intro
(assume ⟨⟨ha, hb⟩, hc⟩, ⟨ha, ⟨hb, hc⟩⟩)
(assume ⟨ha, ⟨hb, hc⟩⟩, ⟨⟨ha, hb⟩, hc⟩)
lemma and_assoc (a b : Prop) : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) := and.assoc
lemma and.left_comm : a ∧ (b ∧ c) ↔ b ∧ (a ∧ c) :=
iff.trans (iff.symm and.assoc) (iff.trans (and_congr and.comm (iff.refl c)) and.assoc)
lemma and_iff_left {a b : Prop} (hb : b) : (a ∧ b) ↔ a :=
iff.intro and.left (λ ha, ⟨ha, hb⟩)
lemma and_iff_right {a b : Prop} (ha : a) : (a ∧ b) ↔ b :=
iff.intro and.right (and.intro ha)
@[simp] lemma and_true (a : Prop) : a ∧ true ↔ a :=
and_iff_left trivial
@[simp] lemma true_and (a : Prop) : true ∧ a ↔ a :=
and_iff_right trivial
@[simp] lemma and_false (a : Prop) : a ∧ false ↔ false :=
iff_false_intro and.right
@[simp] lemma false_and (a : Prop) : false ∧ a ↔ false :=
iff_false_intro and.left
@[simp] lemma not_and_self (a : Prop) : (¬a ∧ a) ↔ false :=
iff_false_intro (λ h, and.elim h (λ h₁ h₂, absurd h₂ h₁))
@[simp] lemma and_not_self (a : Prop) : (a ∧ ¬a) ↔ false :=
iff_false_intro (assume ⟨h₁, h₂⟩, absurd h₁ h₂)
@[simp] lemma and_self (a : Prop) : a ∧ a ↔ a :=
iff.intro and.left (assume h, ⟨h, h⟩)
/- or simp rules -/
lemma or.imp (h₂ : a → c) (h₃ : b → d) : a ∨ b → c ∨ d :=
or.rec (λ h, or.inl (h₂ h)) (λ h, or.inr (h₃ h))
lemma or.imp_left (h : a → b) : a ∨ c → b ∨ c :=
or.imp h id
lemma or.imp_right (h : a → b) : c ∨ a → c ∨ b :=
or.imp id h
@[congr] lemma or_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ∨ b) ↔ (c ∨ d) :=
iff.intro (or.imp (iff.mp h₁) (iff.mp h₂)) (or.imp (iff.mpr h₁) (iff.mpr h₂))
lemma or.comm : a ∨ b ↔ b ∨ a := iff.intro or.swap or.swap
lemma or_comm (a b : Prop) : a ∨ b ↔ b ∨ a := or.comm
lemma or.assoc : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) :=
iff.intro
(or.rec (or.imp_right or.inl) (λ h, or.inr (or.inr h)))
(or.rec (λ h, or.inl (or.inl h)) (or.imp_left or.inr))
lemma or_assoc (a b : Prop) : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) :=
or.assoc
lemma or.left_comm : a ∨ (b ∨ c) ↔ b ∨ (a ∨ c) :=
iff.trans (iff.symm or.assoc) (iff.trans (or_congr or.comm (iff.refl c)) or.assoc)
theorem or_iff_right_of_imp (ha : a → b) : (a ∨ b) ↔ b :=
iff.intro (or.rec ha id) or.inr
theorem or_iff_left_of_imp (hb : b → a) : (a ∨ b) ↔ a :=
iff.intro (or.rec id hb) or.inl
@[simp] lemma or_true (a : Prop) : a ∨ true ↔ true :=
iff_true_intro (or.inr trivial)
@[simp] lemma true_or (a : Prop) : true ∨ a ↔ true :=
iff_true_intro (or.inl trivial)
@[simp] lemma or_false (a : Prop) : a ∨ false ↔ a :=
iff.intro (or.rec id false.elim) or.inl
@[simp] lemma false_or (a : Prop) : false ∨ a ↔ a :=
iff.trans or.comm (or_false a)
@[simp] lemma or_self (a : Prop) : a ∨ a ↔ a :=
iff.intro (or.rec id id) or.inl
lemma not_or {a b : Prop} : ¬ a → ¬ b → ¬ (a ∨ b)
| hna hnb (or.inl ha) := absurd ha hna
| hna hnb (or.inr hb) := absurd hb hnb
/- or resolution rulse -/
lemma or.resolve_left {a b : Prop} (h : a ∨ b) (na : ¬ a) : b :=
or.elim h (λ ha, absurd ha na) id
lemma or.neg_resolve_left {a b : Prop} (h : ¬ a ∨ b) (ha : a) : b :=
or.elim h (λ na, absurd ha na) id
lemma or.resolve_right {a b : Prop} (h : a ∨ b) (nb : ¬ b) : a :=
or.elim h id (λ hb, absurd hb nb)
lemma or.neg_resolve_right {a b : Prop} (h : a ∨ ¬ b) (hb : b) : a :=
or.elim h id (λ nb, absurd hb nb)
/- iff simp rules -/
@[simp] lemma iff_true (a : Prop) : (a ↔ true) ↔ a :=
iff.intro (assume h, iff.mpr h trivial) iff_true_intro
@[simp] lemma true_iff (a : Prop) : (true ↔ a) ↔ a :=
iff.trans iff.comm (iff_true a)
@[simp] lemma iff_false (a : Prop) : (a ↔ false) ↔ ¬ a :=
iff.intro iff.mp iff_false_intro
@[simp] lemma false_iff (a : Prop) : (false ↔ a) ↔ ¬ a :=
iff.trans iff.comm (iff_false a)
@[simp] lemma iff_self (a : Prop) : (a ↔ a) ↔ true :=
iff_true_intro iff.rfl
@[congr] lemma iff_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ↔ b) ↔ (c ↔ d) :=
(iff_iff_implies_and_implies a b).trans
((and_congr (imp_congr h₁ h₂) (imp_congr h₂ h₁)).trans
(iff_iff_implies_and_implies c d).symm)
/- implies simp rule -/
@[simp] lemma implies_true_iff (α : Sort u) : (α → true) ↔ true :=
iff.intro (λ h, trivial) (λ ha h, trivial)
lemma false_implies_iff (a : Prop) : (false → a) ↔ true :=
iff.intro (λ h, trivial) (λ ha h, false.elim h)
theorem true_implies_iff (α : Prop) : (true → α) ↔ α :=
iff.intro (λ h, h trivial) (λ h h', h)
/--
The existential quantifier.
To prove a goal of the form `⊢ ∃ x, p x`, you can provide a witness `y` with the tactic `existsi y`.
If you are working in a project that depends on mathlib, then we recommend the `use` tactic
instead.
You'll then be left with the goal `⊢ p y`.
To extract a witness `x` and proof `hx : p x` from a hypothesis `h : ∃ x, p x`,
use the tactic `cases h with x hx`. See also the mathlib tactics `obtain` and `rcases`.
-/
inductive Exists {α : Sort u} (p : α → Prop) : Prop
| intro (w : α) (h : p w) : Exists
attribute [intro] Exists.intro
notation `exists` binders `, ` r:(scoped P, Exists P) := r
notation `∃` binders `, ` r:(scoped P, Exists P) := r
/- This is a `def`, so that it can be used as pattern in the equation compiler. -/
@[pattern] def exists.intro {α : Sort u} {p : α → Prop} (w : α) (h : p w) : ∃ x, p x := ⟨w, h⟩
lemma exists.elim {α : Sort u} {p : α → Prop} {b : Prop}
(h₁ : ∃ x, p x) (h₂ : ∀ (a : α), p a → b) : b :=
Exists.rec h₂ h₁
/- exists unique -/
def exists_unique {α : Sort u} (p : α → Prop) :=
∃ x, p x ∧ ∀ y, p y → y = x
notation `∃!` binders `, ` r:(scoped P, exists_unique P) := r
@[intro]
lemma exists_unique.intro {α : Sort u} {p : α → Prop} (w : α) (h₁ : p w) (h₂ : ∀ y, p y → y = w) :
∃! x, p x :=
exists.intro w ⟨h₁, h₂⟩
attribute [recursor 4]
lemma exists_unique.elim {α : Sort u} {p : α → Prop} {b : Prop}
(h₂ : ∃! x, p x) (h₁ : ∀ x, p x → (∀ y, p y → y = x) → b) : b :=
exists.elim h₂ (λ w hw, h₁ w (and.left hw) (and.right hw))
lemma exists_unique_of_exists_of_unique {α : Sort u} {p : α → Prop}
(hex : ∃ x, p x) (hunique : ∀ y₁ y₂, p y₁ → p y₂ → y₁ = y₂) : ∃! x, p x :=
exists.elim hex (λ x px, exists_unique.intro x px (assume y, assume : p y, hunique y x this px))
lemma exists_of_exists_unique {α : Sort u} {p : α → Prop} (h : ∃! x, p x) : ∃ x, p x :=
exists.elim h (λ x hx, ⟨x, and.left hx⟩)
lemma unique_of_exists_unique {α : Sort u} {p : α → Prop}
(h : ∃! x, p x) {y₁ y₂ : α} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ :=
exists_unique.elim h
(assume x, assume : p x,
assume unique : ∀ y, p y → y = x,
show y₁ = y₂, from eq.trans (unique _ py₁) (eq.symm (unique _ py₂)))
/- exists, forall, exists unique congruences -/
@[congr] lemma forall_congr {α : Sort u} {p q : α → Prop} (h : ∀ a, (p a ↔ q a)) : (∀ a, p a) ↔ ∀ a, q a :=
iff.intro (λ p a, iff.mp (h a) (p a)) (λ q a, iff.mpr (h a) (q a))
lemma exists_imp_exists {α : Sort u} {p q : α → Prop} (h : ∀ a, (p a → q a)) (p : ∃ a, p a) : ∃ a, q a :=
exists.elim p (λ a hp, ⟨a, h a hp⟩)
@[congr] lemma exists_congr {α : Sort u} {p q : α → Prop} (h : ∀ a, (p a ↔ q a)) : (Exists p) ↔ ∃ a, q a :=
iff.intro
(exists_imp_exists (λ a, iff.mp (h a)))
(exists_imp_exists (λ a, iff.mpr (h a)))
@[congr] lemma exists_unique_congr {α : Sort u} {p₁ p₂ : α → Prop} (h : ∀ x, p₁ x ↔ p₂ x) : (exists_unique p₁) ↔ (∃! x, p₂ x) := --
exists_congr (λ x, and_congr (h x) (forall_congr (λ y, imp_congr (h y) iff.rfl)))
lemma forall_not_of_not_exists {α : Sort u} {p : α → Prop} : ¬(∃ x, p x) → (∀ x, ¬p x) :=
λ hne x hp, hne ⟨x, hp⟩
/- decidable -/
def decidable.to_bool (p : Prop) [h : decidable p] : bool :=
decidable.cases_on h (λ h₁, bool.ff) (λ h₂, bool.tt)
export decidable (is_true is_false to_bool)
@[simp] lemma to_bool_true_eq_tt (h : decidable true) : @to_bool true h = tt :=
decidable.cases_on h (λ h, false.elim (iff.mp not_true h)) (λ _, rfl)
@[simp] lemma to_bool_false_eq_ff (h : decidable false) : @to_bool false h = ff :=
decidable.cases_on h (λ h, rfl) (λ h, false.elim h)
instance decidable.true : decidable true :=
is_true trivial
instance decidable.false : decidable false :=
is_false not_false
-- We use "dependent" if-then-else to be able to communicate the if-then-else condition
-- to the branches
@[inline] def dite {α : Sort u} (c : Prop) [h : decidable c] : (c → α) → (¬ c → α) → α :=
λ t e, decidable.rec_on h e t
/- if-then-else -/
@[inline] def ite {α : Sort u} (c : Prop) [h : decidable c] (t e : α) : α :=
decidable.rec_on h (λ hnc, e) (λ hc, t)
namespace decidable
variables {p q : Prop}
def rec_on_true [h : decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u} (h₃ : p) (h₄ : h₁ h₃)
: decidable.rec_on h h₂ h₁ :=
decidable.rec_on h (λ h, false.rec _ (h h₃)) (λ h, h₄)
def rec_on_false [h : decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u} (h₃ : ¬p) (h₄ : h₂ h₃)
: decidable.rec_on h h₂ h₁ :=
decidable.rec_on h (λ h, h₄) (λ h, false.rec _ (h₃ h))
def by_cases {q : Sort u} [φ : decidable p] : (p → q) → (¬p → q) → q := dite _
/-- Law of Excluded Middle. -/
lemma em (p : Prop) [decidable p] : p ∨ ¬p := by_cases or.inl or.inr
lemma by_contradiction [decidable p] (h : ¬p → false) : p :=
if h₁ : p then h₁ else false.rec _ (h h₁)
lemma of_not_not [decidable p] : ¬ ¬ p → p :=
λ hnn, by_contradiction (λ hn, absurd hn hnn)
lemma not_not_iff (p) [decidable p] : (¬ ¬ p) ↔ p :=
iff.intro of_not_not not_not_intro
lemma not_and_iff_or_not (p q : Prop) [d₁ : decidable p] [d₂ : decidable q] : ¬ (p ∧ q) ↔ ¬ p ∨ ¬ q :=
iff.intro
(λ h, match d₁ with
| is_true h₁ :=
match d₂ with
| is_true h₂ := absurd (and.intro h₁ h₂) h
| is_false h₂ := or.inr h₂
end
| is_false h₁ := or.inl h₁
end)
(λ h ⟨hp, hq⟩, or.elim h (λ h, h hp) (λ h, h hq))
lemma not_or_iff_and_not (p q) [d₁ : decidable p] [d₂ : decidable q] : ¬ (p ∨ q) ↔ ¬ p ∧ ¬ q :=
iff.intro
(λ h, match d₁ with
| is_true h₁ := false.elim $ h (or.inl h₁)
| is_false h₁ :=
match d₂ with
| is_true h₂ := false.elim $ h (or.inr h₂)
| is_false h₂ := ⟨h₁, h₂⟩
end
end)
(λ ⟨np, nq⟩ h, or.elim h np nq)
end decidable
section
variables {p q : Prop}
def decidable_of_decidable_of_iff (hp : decidable p) (h : p ↔ q) : decidable q :=
if hp : p then is_true (iff.mp h hp)
else is_false (iff.mp (not_iff_not_of_iff h) hp)
def decidable_of_decidable_of_eq (hp : decidable p) (h : p = q) : decidable q :=
decidable_of_decidable_of_iff hp h.to_iff
protected def or.by_cases [decidable p] [decidable q] {α : Sort u}
(h : p ∨ q) (h₁ : p → α) (h₂ : q → α) : α :=
if hp : p then h₁ hp else
if hq : q then h₂ hq else
false.rec _ (or.elim h hp hq)
end
section
variables {p q : Prop}
instance [decidable p] [decidable q] : decidable (p ∧ q) :=
if hp : p then
if hq : q then is_true ⟨hp, hq⟩
else is_false (assume h : p ∧ q, hq (and.right h))
else is_false (assume h : p ∧ q, hp (and.left h))
instance [decidable p] [decidable q] : decidable (p ∨ q) :=
if hp : p then is_true (or.inl hp) else
if hq : q then is_true (or.inr hq) else
is_false (or.rec hp hq)
instance [decidable p] : decidable (¬p) :=
if hp : p then is_false (absurd hp) else is_true hp
instance implies.decidable [decidable p] [decidable q] : decidable (p → q) :=
if hp : p then
if hq : q then is_true (assume h, hq)
else is_false (assume h : p → q, absurd (h hp) hq)
else is_true (assume h, absurd h hp)
instance [decidable p] [decidable q] : decidable (p ↔ q) :=
if hp : p then
if hq : q then is_true ⟨λ_, hq, λ_, hp⟩
else is_false $ λh, hq (h.1 hp)
else
if hq : q then is_false $ λh, hp (h.2 hq)
else is_true $ ⟨λh, absurd h hp, λh, absurd h hq⟩
instance [decidable p] [decidable q] : decidable (xor p q) :=
if hp : p then
if hq : q then is_false (or.rec (λ ⟨_, h⟩, h hq : ¬(p ∧ ¬ q)) (λ ⟨_, h⟩, h hp : ¬(q ∧ ¬ p)))
else is_true $ or.inl ⟨hp, hq⟩
else
if hq : q then is_true $ or.inr ⟨hq, hp⟩
else is_false (or.rec (λ ⟨h, _⟩, hp h : ¬(p ∧ ¬ q)) (λ ⟨h, _⟩, hq h : ¬(q ∧ ¬ p)))
instance exists_prop_decidable {p} (P : p → Prop)
[Dp : decidable p] [DP : ∀ h, decidable (P h)] : decidable (∃ h, P h) :=
if h : p then decidable_of_decidable_of_iff (DP h)
⟨λ h2, ⟨h, h2⟩, λ⟨h', h2⟩, h2⟩ else is_false (mt (λ⟨h, _⟩, h) h)
instance forall_prop_decidable {p} (P : p → Prop)
[Dp : decidable p] [DP : ∀ h, decidable (P h)] : decidable (∀ h, P h) :=
if h : p then decidable_of_decidable_of_iff (DP h)
⟨λ h2 _, h2, λal, al h⟩ else is_true (λ h2, absurd h2 h)
end
instance {α : Sort u} [decidable_eq α] (a b : α) : decidable (a ≠ b) :=
implies.decidable
lemma bool.ff_ne_tt : ff = tt → false
.
def is_dec_eq {α : Sort u} (p : α → α → bool) : Prop := ∀ ⦃x y : α⦄, p x y = tt → x = y
def is_dec_refl {α : Sort u} (p : α → α → bool) : Prop := ∀ x, p x x = tt
open decidable
instance : decidable_eq bool
| ff ff := is_true rfl
| ff tt := is_false bool.ff_ne_tt
| tt ff := is_false (ne.symm bool.ff_ne_tt)
| tt tt := is_true rfl
def decidable_eq_of_bool_pred {α : Sort u} {p : α → α → bool} (h₁ : is_dec_eq p) (h₂ : is_dec_refl p) : decidable_eq α :=
assume x y : α,
if hp : p x y = tt then is_true (h₁ hp)
else is_false (assume hxy : x = y, absurd (h₂ y) (@eq.rec_on _ _ (λ z, ¬p z y = tt) _ hxy hp))
lemma decidable_eq_inl_refl {α : Sort u} [h : decidable_eq α] (a : α) : h a a = is_true (eq.refl a) :=
match (h a a) with
| (is_true e) := rfl
| (is_false n) := absurd rfl n
end
lemma decidable_eq_inr_neg {α : Sort u} [h : decidable_eq α] {a b : α} : Π n : a ≠ b, h a b = is_false n :=
assume n,
match (h a b) with
| (is_true e) := absurd e n
| (is_false n₁) := proof_irrel n n₁ ▸ eq.refl (is_false n)
end
/- inhabited -/
class inhabited (α : Sort u) :=
(default : α)
export inhabited (default)
@[inline, irreducible] def arbitrary (α : Sort u) [inhabited α] : α :=
default
instance prop.inhabited : inhabited Prop :=
⟨true⟩
instance pi.inhabited (α : Sort u) {β : α → Sort v} [Π x, inhabited (β x)] : inhabited (Π x, β x) :=
⟨λ a, default⟩
instance : inhabited bool := ⟨ff⟩
instance : inhabited true := ⟨trivial⟩
class inductive nonempty (α : Sort u) : Prop
| intro (val : α) : nonempty
protected lemma nonempty.elim {α : Sort u} {p : Prop} (h₁ : nonempty α) (h₂ : α → p) : p :=
nonempty.rec h₂ h₁
@[priority 100]
instance nonempty_of_inhabited {α : Sort u} [inhabited α] : nonempty α :=
⟨default⟩
lemma nonempty_of_exists {α : Sort u} {p : α → Prop} : (∃ x, p x) → nonempty α
| ⟨w, h⟩ := ⟨w⟩
/- subsingleton -/
class inductive subsingleton (α : Sort u) : Prop
| intro (h : ∀ a b : α, a = b) : subsingleton
protected lemma subsingleton.elim {α : Sort u} [h : subsingleton α] : ∀ (a b : α), a = b :=
subsingleton.rec (λ p, p) h
protected lemma subsingleton.helim {α β : Sort u} [h : subsingleton α] (h : α = β) :
∀ (a : α) (b : β), a == b :=
eq.rec_on h (λ a b : α, heq_of_eq (subsingleton.elim a b))
instance subsingleton_prop (p : Prop) : subsingleton p :=
⟨λ a b, proof_irrel a b⟩
instance (p : Prop) : subsingleton (decidable p) :=
subsingleton.intro (λ d₁,
match d₁ with
| (is_true t₁) := (λ d₂,
match d₂ with
| (is_true t₂) := eq.rec_on (proof_irrel t₁ t₂) rfl
| (is_false f₂) := absurd t₁ f₂
end)
| (is_false f₁) := (λ d₂,
match d₂ with
| (is_true t₂) := absurd t₂ f₁
| (is_false f₂) := eq.rec_on (proof_irrel f₁ f₂) rfl
end)
end)
protected lemma rec_subsingleton {p : Prop} [h : decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u}
[h₃ : Π (h : p), subsingleton (h₁ h)] [h₄ : Π (h : ¬p), subsingleton (h₂ h)]
: subsingleton (decidable.rec_on h h₂ h₁) :=
match h with
| (is_true h) := h₃ h
| (is_false h) := h₄ h
end
lemma if_pos {c : Prop} [h : decidable c] (hc : c) {α : Sort u} {t e : α} : (ite c t e) = t :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := absurd hc hnc
end
lemma if_neg {c : Prop} [h : decidable c] (hnc : ¬c) {α : Sort u} {t e : α} : (ite c t e) = e :=
match h with
| (is_true hc) := absurd hc hnc
| (is_false hnc) := rfl
end
@[simp]
lemma if_t_t (c : Prop) [h : decidable c] {α : Sort u} (t : α) : (ite c t t) = t :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := rfl
end
lemma implies_of_if_pos {c t e : Prop} [decidable c] (h : ite c t e) : c → t :=
assume hc, eq.rec_on (if_pos hc : ite c t e = t) h
lemma implies_of_if_neg {c t e : Prop} [decidable c] (h : ite c t e) : ¬c → e :=
assume hnc, eq.rec_on (if_neg hnc : ite c t e = e) h
lemma if_ctx_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c]
{x y u v : α}
(h_c : b ↔ c) (h_t : c → x = u) (h_e : ¬c → y = v) :
ite b x y = ite c u v :=
match dec_b, dec_c with
| (is_false h₁), (is_false h₂) := h_e h₂
| (is_true h₁), (is_true h₂) := h_t h₂
| (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁)
| (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂)
end
lemma if_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c]
{x y u v : α}
(h_c : b ↔ c) (h_t : x = u) (h_e : y = v) :
ite b x y = ite c u v :=
@if_ctx_congr α b c dec_b dec_c x y u v h_c (λ h, h_t) (λ h, h_e)
@[simp]
lemma if_true {α : Sort u} {h : decidable true} (t e : α) : (@ite α true h t e) = t :=
if_pos trivial
@[simp]
lemma if_false {α : Sort u} {h : decidable false} (t e : α) : (@ite α false h t e) = e :=
if_neg not_false
lemma if_ctx_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c]
(h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) :
ite b x y ↔ ite c u v :=
match dec_b, dec_c with
| (is_false h₁), (is_false h₂) := h_e h₂
| (is_true h₁), (is_true h₂) := h_t h₂
| (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁)
| (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂)
end
@[congr]
lemma if_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c]
(h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) :
ite b x y ↔ ite c u v :=
if_ctx_congr_prop h_c (λ h, h_t) (λ h, h_e)
lemma if_ctx_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b]
(h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) :
ite b x y ↔ (@ite Prop c (decidable_of_decidable_of_iff dec_b h_c) u v) :=
@if_ctx_congr_prop b c x y u v dec_b (decidable_of_decidable_of_iff dec_b h_c) h_c h_t h_e
@[congr]
lemma if_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b]
(h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) :
ite b x y ↔ (@ite Prop c (decidable_of_decidable_of_iff dec_b h_c) u v) :=
@if_ctx_simp_congr_prop b c x y u v dec_b h_c (λ h, h_t) (λ h, h_e)
@[simp] lemma dif_pos {c : Prop} [h : decidable c] (hc : c) {α : Sort u} {t : c → α} {e : ¬ c → α} : dite c t e = t hc :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := absurd hc hnc
end
@[simp] lemma dif_neg {c : Prop} [h : decidable c] (hnc : ¬c) {α : Sort u} {t : c → α} {e : ¬ c → α} : dite c t e = e hnc :=
match h with
| (is_true hc) := absurd hc hnc
| (is_false hnc) := rfl
end
@[congr]
lemma dif_ctx_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c]
{x : b → α} {u : c → α} {y : ¬b → α} {v : ¬c → α}
(h_c : b ↔ c)
(h_t : ∀ (h : c), x (iff.mpr h_c h) = u h)
(h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) :
(@dite α b dec_b x y) = (@dite α c dec_c u v) :=
match dec_b, dec_c with
| (is_false h₁), (is_false h₂) := h_e h₂
| (is_true h₁), (is_true h₂) := h_t h₂
| (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁)
| (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂)
end
lemma dif_ctx_simp_congr {α : Sort u} {b c : Prop} [dec_b : decidable b]
{x : b → α} {u : c → α} {y : ¬b → α} {v : ¬c → α}
(h_c : b ↔ c)
(h_t : ∀ (h : c), x (iff.mpr h_c h) = u h)
(h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) :
(@dite α b dec_b x y) = (@dite α c (decidable_of_decidable_of_iff dec_b h_c) u v) :=
@dif_ctx_congr α b c dec_b (decidable_of_decidable_of_iff dec_b h_c) x u y v h_c h_t h_e
-- Remark: dite and ite are "defally equal" when we ignore the proofs.
lemma dif_eq_if (c : Prop) [h : decidable c] {α : Sort u} (t : α) (e : α) : dite c (λ h, t) (λ h, e) = ite c t e :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := rfl
end
instance {c t e : Prop} [d_c : decidable c] [d_t : decidable t] [d_e : decidable e] : decidable (if c then t else e) :=
match d_c with
| (is_true hc) := d_t
| (is_false hc) := d_e
end
instance {c : Prop} {t : c → Prop} {e : ¬c → Prop} [d_c : decidable c] [d_t : ∀ h, decidable (t h)] [d_e : ∀ h, decidable (e h)] : decidable (if h : c then t h else e h) :=
match d_c with
| (is_true hc) := d_t hc
| (is_false hc) := d_e hc
end
def as_true (c : Prop) [decidable c] : Prop :=
if c then true else false
def as_false (c : Prop) [decidable c] : Prop :=
if c then false else true
lemma of_as_true {c : Prop} [h₁ : decidable c] (h₂ : as_true c) : c :=
match h₁, h₂ with
| (is_true h_c), h₂ := h_c
| (is_false h_c), h₂ := false.elim h₂
end
/-- Universe lifting operation -/
structure {r s} ulift (α : Type s) : Type (max s r) :=
up :: (down : α)
namespace ulift
/- Bijection between α and ulift.{v} α -/
lemma up_down {α : Type u} : ∀ (b : ulift.{v} α), up (down b) = b
| (up a) := rfl
lemma down_up {α : Type u} (a : α) : down (up.{v} a) = a := rfl
end ulift
/-- Universe lifting operation from Sort to Type -/
structure plift (α : Sort u) : Type u :=
up :: (down : α)
namespace plift
/- Bijection between α and plift α -/
lemma up_down {α : Sort u} : ∀ (b : plift α), up (down b) = b
| (up a) := rfl
lemma down_up {α : Sort u} (a : α) : down (up a) = a := rfl
end plift
/- Equalities for rewriting let-expressions -/
lemma let_value_eq {α : Sort u} {β : Sort v} {a₁ a₂ : α} (b : α → β) :
a₁ = a₂ → (let x : α := a₁ in b x) = (let x : α := a₂ in b x) :=
λ h, eq.rec_on h rfl
lemma let_value_heq {α : Sort v} {β : α → Sort u} {a₁ a₂ : α} (b : Π x : α, β x) :
a₁ = a₂ → (let x : α := a₁ in b x) == (let x : α := a₂ in b x) :=
λ h, eq.rec_on h (heq.refl (b a₁))
lemma let_body_eq {α : Sort v} {β : α → Sort u} (a : α) {b₁ b₂ : Π x : α, β x} :
(∀ x, b₁ x = b₂ x) → (let x : α := a in b₁ x) = (let x : α := a in b₂ x) :=
λ h, h a
lemma let_eq {α : Sort v} {β : Sort u} {a₁ a₂ : α} {b₁ b₂ : α → β} :
a₁ = a₂ → (∀ x, b₁ x = b₂ x) → (let x : α := a₁ in b₁ x) = (let x : α := a₂ in b₂ x) :=
λ h₁ h₂, eq.rec_on h₁ (h₂ a₁)
section relation
variables {α : Sort u} {β : Sort v} (r : β → β → Prop)
local infix `≺`:50 := r
def reflexive := ∀ x, x ≺ x
def symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x
def transitive := ∀ ⦃x y z⦄, x ≺ y → y ≺ z → x ≺ z
def equivalence := reflexive r ∧ symmetric r ∧ transitive r
def total := ∀ x y, x ≺ y ∨ y ≺ x
lemma mk_equivalence (rfl : reflexive r) (symm : symmetric r) (trans : transitive r) :
equivalence r :=
⟨rfl, symm, trans⟩
def irreflexive := ∀ x, ¬ x ≺ x
def anti_symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x → x = y
def empty_relation := λ a₁ a₂ : α, false
def subrelation (q r : β → β → Prop) := ∀ ⦃x y⦄, q x y → r x y
def inv_image (f : α → β) : α → α → Prop :=
λ a₁ a₂, f a₁ ≺ f a₂
lemma inv_image.trans (f : α → β) (h : transitive r) : transitive (inv_image r f) :=
λ (a₁ a₂ a₃ : α) (h₁ : inv_image r f a₁ a₂) (h₂ : inv_image r f a₂ a₃), h h₁ h₂
lemma inv_image.irreflexive (f : α → β) (h : irreflexive r) : irreflexive (inv_image r f) :=
λ (a : α) (h₁ : inv_image r f a a), h (f a) h₁
end relation
section binary
variables {α : Type u} {β : Type v}
variable f : α → α → α
variable inv : α → α
variable one : α
local notation a * b := f a b
local notation a ⁻¹ := inv a
variable g : α → α → α
local notation a + b := g a b
def commutative := ∀ a b, a * b = b * a
def associative := ∀ a b c, (a * b) * c = a * (b * c)
def left_identity := ∀ a, one * a = a
def right_identity := ∀ a, a * one = a
def right_inverse := ∀ a, a * a⁻¹ = one
def left_cancelative := ∀ a b c, a * b = a * c → b = c
def right_cancelative := ∀ a b c, a * b = c * b → a = c
def left_distributive := ∀ a b c, a * (b + c) = a * b + a * c
def right_distributive := ∀ a b c, (a + b) * c = a * c + b * c
def right_commutative (h : β → α → β) := ∀ b a₁ a₂, h (h b a₁) a₂ = h (h b a₂) a₁
def left_commutative (h : α → β → β) := ∀ a₁ a₂ b, h a₁ (h a₂ b) = h a₂ (h a₁ b)
lemma left_comm : commutative f → associative f → left_commutative f :=
assume hcomm hassoc, assume a b c, calc
a*(b*c) = (a*b)*c : eq.symm (hassoc a b c)
... = (b*a)*c : hcomm a b ▸ rfl
... = b*(a*c) : hassoc b a c
lemma right_comm : commutative f → associative f → right_commutative f :=
assume hcomm hassoc, assume a b c, calc
(a*b)*c = a*(b*c) : hassoc a b c
... = a*(c*b) : hcomm b c ▸ rfl
... = (a*c)*b : eq.symm (hassoc a c b)
end binary
|
8fe371161b824c775c5667a559f5965d553fac1d | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /src/Lean/Elab/Tactic/BuiltinTactic.lean | ef2111354ba43272a5a46a8c8177d2d963ef7e94 | [
"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 | 9,912 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Elab.Tactic.Basic
namespace Lean.Elab.Tactic
open Meta
@[builtinTactic Lean.Parser.Tactic.«done»] def evalDone : Tactic := fun _ =>
done
@[builtinTactic seq1] def evalSeq1 : Tactic := fun stx => do
let args := stx[0].getArgs
for i in [:args.size] do
if i % 2 == 0 then
evalTactic args[i]
else
saveTacticInfoForToken args[i] -- add `TacticInfo` node for `;`
@[builtinTactic paren] def evalParen : Tactic := fun stx =>
evalTactic stx[1]
/- Evaluate `many (group (tactic >> optional ";")) -/
private def evalManyTacticOptSemi (stx : Syntax) : TacticM Unit := do
stx.forArgsM fun seqElem => do
evalTactic seqElem[0]
saveTacticInfoForToken seqElem[1] -- add TacticInfo node for `;`
@[builtinTactic tacticSeq1Indented] def evalTacticSeq1Indented : Tactic := fun stx =>
evalManyTacticOptSemi stx[0]
@[builtinTactic tacticSeqBracketed] def evalTacticSeqBracketed : Tactic := fun stx => do
let initInfo ← mkInitialTacticInfo stx[0]
withRef stx[2] <| closeUsingOrAdmit do
-- save state before/after entering focus on `{`
withInfoContext (pure ()) initInfo
evalManyTacticOptSemi stx[1]
@[builtinTactic Parser.Tactic.focus] def evalFocus : Tactic := fun stx => do
let mkInfo ← mkInitialTacticInfo stx[0]
focus do
-- show focused state on `focus`
withInfoContext (pure ()) mkInfo
evalTactic stx[1]
private def getOptRotation (stx : Syntax) : Nat :=
if stx.isNone then 1 else stx[0].toNat
@[builtinTactic Parser.Tactic.rotateLeft] def evalRotateLeft : Tactic := fun stx => do
let n := getOptRotation stx[1]
setGoals <| (← getGoals).rotateLeft n
@[builtinTactic Parser.Tactic.rotateRight] def evalRotateRight : Tactic := fun stx => do
let n := getOptRotation stx[1]
setGoals <| (← getGoals).rotateRight n
@[builtinTactic Parser.Tactic.open] def evalOpen : Tactic := fun stx => do
try
pushScope
let openDecls ← elabOpenDecl stx[1]
withTheReader Core.Context (fun ctx => { ctx with openDecls := openDecls }) do
evalTactic stx[3]
finally
popScope
@[builtinTactic Parser.Tactic.set_option] def elabSetOption : Tactic := fun stx => do
let options ← Elab.elabSetOption stx[1] stx[2]
withTheReader Core.Context (fun ctx => { ctx with maxRecDepth := maxRecDepth.get options, options := options }) do
evalTactic stx[4]
@[builtinTactic Parser.Tactic.allGoals] def evalAllGoals : Tactic := fun stx => do
let mvarIds ← getGoals
let mut mvarIdsNew := #[]
for mvarId in mvarIds do
unless (← isExprMVarAssigned mvarId) do
setGoals [mvarId]
try
evalTactic stx[1]
mvarIdsNew := mvarIdsNew ++ (← getUnsolvedGoals)
catch ex =>
logException ex
mvarIdsNew := mvarIdsNew.push mvarId
setGoals mvarIdsNew.toList
@[builtinTactic tacticSeq] def evalTacticSeq : Tactic := fun stx =>
evalTactic stx[0]
partial def evalChoiceAux (tactics : Array Syntax) (i : Nat) : TacticM Unit :=
if h : i < tactics.size then
let tactic := tactics.get ⟨i, h⟩
catchInternalId unsupportedSyntaxExceptionId
(evalTactic tactic)
(fun _ => evalChoiceAux tactics (i+1))
else
throwUnsupportedSyntax
@[builtinTactic choice] def evalChoice : Tactic := fun stx =>
evalChoiceAux stx.getArgs 0
@[builtinTactic skip] def evalSkip : Tactic := fun stx => pure ()
@[builtinTactic unknown] def evalUnknown : Tactic := fun stx => do
addCompletionInfo <| CompletionInfo.tactic stx (← getGoals)
@[builtinTactic failIfSuccess] def evalFailIfSuccess : Tactic := fun stx => do
let tactic := stx[1]
if (← try evalTactic tactic; pure true catch _ => pure false) then
throwError "tactic succeeded"
@[builtinTactic traceState] def evalTraceState : Tactic := fun stx => do
let gs ← getUnsolvedGoals
logInfo (goalsToMessageData gs)
@[builtinTactic Lean.Parser.Tactic.assumption] def evalAssumption : Tactic := fun stx =>
liftMetaTactic fun mvarId => do Meta.assumption mvarId; pure []
@[builtinTactic Lean.Parser.Tactic.contradiction] def evalContradiction : Tactic := fun stx =>
liftMetaTactic fun mvarId => do Meta.contradiction mvarId; pure []
@[builtinTactic Lean.Parser.Tactic.intro] def evalIntro : Tactic := fun stx => do
match stx with
| `(tactic| intro) => introStep `_
| `(tactic| intro $h:ident) => introStep h.getId
| `(tactic| intro _) => introStep `_
| `(tactic| intro $pat:term) => evalTactic (← `(tactic| intro h; match h with | $pat:term => ?_; try clear h))
| `(tactic| intro $h:term $hs:term*) => evalTactic (← `(tactic| intro $h:term; intro $hs:term*))
| _ => throwUnsupportedSyntax
where
introStep (n : Name) : TacticM Unit :=
liftMetaTactic fun mvarId => do
let (_, mvarId) ← Meta.intro mvarId n
pure [mvarId]
@[builtinTactic Lean.Parser.Tactic.introMatch] def evalIntroMatch : Tactic := fun stx => do
let matchAlts := stx[1]
let stxNew ← liftMacroM <| Term.expandMatchAltsIntoMatchTactic stx matchAlts
withMacroExpansion stx stxNew <| evalTactic stxNew
private def getIntrosSize : Expr → Nat
| Expr.forallE _ _ b _ => getIntrosSize b + 1
| Expr.letE _ _ _ b _ => getIntrosSize b + 1
| Expr.mdata _ b _ => getIntrosSize b
| _ => 0
@[builtinTactic «intros»] def evalIntros : Tactic := fun stx =>
match stx with
| `(tactic| intros) => liftMetaTactic fun mvarId => do
let type ← Meta.getMVarType mvarId
let type ← instantiateMVars type
let n := getIntrosSize type
let (_, mvarId) ← Meta.introN mvarId n
pure [mvarId]
| `(tactic| intros $ids*) => liftMetaTactic fun mvarId => do
let (_, mvarId) ← Meta.introN mvarId ids.size (ids.map getNameOfIdent').toList
pure [mvarId]
| _ => throwUnsupportedSyntax
@[builtinTactic Lean.Parser.Tactic.revert] def evalRevert : Tactic := fun stx =>
match stx with
| `(tactic| revert $hs*) => do
let (_, mvarId) ← Meta.revert (← getMainGoal) (← getFVarIds hs)
replaceMainGoal [mvarId]
| _ => throwUnsupportedSyntax
/- Sort free variables using an order `x < y` iff `x` was defined after `y` -/
private def sortFVarIds (fvarIds : Array FVarId) : TacticM (Array FVarId) :=
withMainContext do
let lctx ← getLCtx
return fvarIds.qsort fun fvarId₁ fvarId₂ =>
match lctx.find? fvarId₁, lctx.find? fvarId₂ with
| some d₁, some d₂ => d₁.index > d₂.index
| some _, none => false
| none, some _ => true
| none, none => Name.quickLt fvarId₁ fvarId₂
@[builtinTactic Lean.Parser.Tactic.clear] def evalClear : Tactic := fun stx =>
match stx with
| `(tactic| clear $hs*) => do
let fvarIds ← getFVarIds hs
let fvarIds ← sortFVarIds fvarIds
for fvarId in fvarIds do
withMainContext do
let mvarId ← clear (← getMainGoal) fvarId
replaceMainGoal [mvarId]
| _ => throwUnsupportedSyntax
def forEachVar (hs : Array Syntax) (tac : MVarId → FVarId → MetaM MVarId) : TacticM Unit := do
for h in hs do
withMainContext do
let fvarId ← getFVarId h
let mvarId ← tac (← getMainGoal) (← getFVarId h)
replaceMainGoal [mvarId]
@[builtinTactic Lean.Parser.Tactic.subst] def evalSubst : Tactic := fun stx =>
match stx with
| `(tactic| subst $hs*) => forEachVar hs Meta.subst
| _ => throwUnsupportedSyntax
/--
First method searches for a metavariable `g` s.t. `tag` is a suffix of its name.
If none is found, then it searches for a metavariable `g` s.t. `tag` is a prefix of its name. -/
private def findTag? (mvarIds : List MVarId) (tag : Name) : TacticM (Option MVarId) := do
let mvarId? ← mvarIds.findM? fun mvarId => return tag.isSuffixOf (← getMVarDecl mvarId).userName
match mvarId? with
| some mvarId => return mvarId
| none => mvarIds.findM? fun mvarId => return tag.isPrefixOf (← getMVarDecl mvarId).userName
@[builtinTactic «case»] def evalCase : Tactic
| stx@`(tactic| case $tag $hs* =>%$arr $tac:tacticSeq) => do
let tag := tag.getId
let gs ← getUnsolvedGoals
let some g ← findTag? gs tag | throwError "tag not found"
let gs := gs.erase g
let mut g := g
unless hs.isEmpty do
let mvarDecl ← getMVarDecl g
let mut lctx := mvarDecl.lctx
let mut hs := hs
let n := lctx.numIndices
for i in [:n] do
let j := n - i - 1
match lctx.getAt? j with
| none => pure ()
| some localDecl =>
if localDecl.userName.hasMacroScopes then
let h := hs.back
if h.isIdent then
let newName := h.getId
lctx := lctx.setUserName localDecl.fvarId newName
hs := hs.pop
if hs.isEmpty then
break
unless hs.isEmpty do
logError m!"too many variable names provided at 'case'"
let mvarNew ← mkFreshExprMVarAt lctx mvarDecl.localInstances mvarDecl.type MetavarKind.syntheticOpaque mvarDecl.userName
assignExprMVar g mvarNew
g := mvarNew.mvarId!
setGoals [g]
let savedTag ← getMVarTag g
setMVarTag g Name.anonymous
try
withCaseRef arr tac do
closeUsingOrAdmit (withTacticInfoContext stx (evalTactic tac))
finally
setMVarTag g savedTag
done
setGoals gs
| _ => throwUnsupportedSyntax
@[builtinTactic «first»] partial def evalFirst : Tactic := fun stx => do
let tacs := stx[1].getArgs
if tacs.isEmpty then throwUnsupportedSyntax
loop tacs 0
where
loop (tacs : Array Syntax) (i : Nat) :=
if i == tacs.size - 1 then
evalTactic tacs[i][1]
else
evalTactic tacs[i][1] <|> loop tacs (i+1)
end Lean.Elab.Tactic
|
e5bd8600f807babc267f5355aade54a284cacb33 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /library/data/fintype/basic.lean | ba0305769b5fe48e71e75118bc02f2cb4a374ad0 | [
"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 | 9,146 | 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
Finite type (type class).
-/
import data.list.perm data.list.as_type data.bool data.equiv
open list bool unit decidable option function
structure fintype [class] (A : Type) : Type :=
(elems : list A) (unique : nodup elems) (complete : ∀ a, a ∈ elems)
definition elements_of (A : Type) [h : fintype A] : list A :=
@fintype.elems A h
section
open equiv
definition fintype_of_equiv {A B : Type} [h : fintype A] : A ≃ B → fintype B
| (mk f g l r) :=
fintype.mk
(map f (elements_of A))
(nodup_map (injective_of_left_inverse l) !fintype.unique)
(λ b,
have g b ∈ elements_of A, from fintype.complete (g b),
have f (g b) ∈ map f (elements_of A), from mem_map f this,
by rewrite r at this; exact this)
end
definition fintype_unit [instance] : fintype unit :=
fintype.mk [star] dec_trivial (λ u, match u with star := dec_trivial end)
definition fintype_bool [instance] : fintype bool :=
fintype.mk [ff, tt]
dec_trivial
(λ b, match b with | tt := dec_trivial | ff := dec_trivial end)
definition fintype_product [instance] {A B : Type} : Π [h₁ : fintype A] [h₂ : fintype B], fintype (A × B)
| (fintype.mk e₁ u₁ c₁) (fintype.mk e₂ u₂ c₂) :=
fintype.mk
(product e₁ e₂)
(nodup_product u₁ u₂)
(λ p,
match p with
(a, b) := mem_product (c₁ a) (c₂ b)
end)
/- auxiliary function for finding 'a' s.t. f a ≠ g a -/
section find_discr
variables {A B : Type}
variable [h : decidable_eq B]
include h
definition find_discr (f g : A → B) : list A → option A
| [] := none
| (a::l) := if f a = g a then find_discr l else some a
theorem find_discr_nil (f g : A → B) : find_discr f g [] = none :=
rfl
theorem find_discr_cons_of_ne {f g : A → B} {a : A} (l : list A) : f a ≠ g a → find_discr f g (a::l) = some a :=
assume ne, if_neg ne
theorem find_discr_cons_of_eq {f g : A → B} {a : A} (l : list A) : f a = g a → find_discr f g (a::l) = find_discr f g l :=
assume eq, if_pos eq
theorem ne_of_find_discr_eq_some {f g : A → B} {a : A} : ∀ {l}, find_discr f g l = some a → f a ≠ g a
| [] e := by contradiction
| (x::l) e := by_cases
(suppose f x = g x,
have find_discr f g l = some a, by rewrite [find_discr_cons_of_eq l this at e]; exact e,
ne_of_find_discr_eq_some this)
(assume h : f x ≠ g x,
have some x = some a, by rewrite [find_discr_cons_of_ne l h at e]; exact e,
by clear ne_of_find_discr_eq_some; injection this; subst a; exact h)
theorem all_eq_of_find_discr_eq_none {f g : A → B} : ∀ {l}, find_discr f g l = none → ∀ a, a ∈ l → f a = g a
| [] e a i := absurd i !not_mem_nil
| (x::l) e a i := by_cases
(assume fx_eq_gx : f x = g x,
or.elim (eq_or_mem_of_mem_cons i)
(suppose a = x, by rewrite [-this at fx_eq_gx]; exact fx_eq_gx)
(suppose a ∈ l,
have aux : find_discr f g l = none, by rewrite [find_discr_cons_of_eq l fx_eq_gx at e]; exact e,
all_eq_of_find_discr_eq_none aux a this))
(suppose f x ≠ g x,
by rewrite [find_discr_cons_of_ne l this at e]; contradiction)
end find_discr
definition decidable_eq_fun [instance] {A B : Type} [h₁ : fintype A] [h₂ : decidable_eq B] : decidable_eq (A → B) :=
λ f g,
match h₁ with
| fintype.mk e u c :=
match find_discr f g e with
| some a := λ h : find_discr f g e = some a, inr (λ f_eq_g : f = g, absurd (by rewrite f_eq_g; reflexivity) (ne_of_find_discr_eq_some h))
| none := λ h : find_discr f g e = none, inl (show f = g, from funext (λ a : A, all_eq_of_find_discr_eq_none h a (c a)))
end rfl
end
section check_pred
variables {A : Type}
definition check_pred (p : A → Prop) [h : decidable_pred p] : list A → bool
| [] := tt
| (a::l) := if p a then check_pred l else ff
theorem check_pred_cons_of_pos {p : A → Prop} [h : decidable_pred p] {a : A} (l : list A) : p a → check_pred p (a::l) = check_pred p l :=
assume pa, if_pos pa
theorem check_pred_cons_of_neg {p : A → Prop} [h : decidable_pred p] {a : A} (l : list A) : ¬ p a → check_pred p (a::l) = ff :=
assume npa, if_neg npa
theorem all_of_check_pred_eq_tt {p : A → Prop} [h : decidable_pred p] : ∀ {l : list A}, check_pred p l = tt → ∀ {a}, a ∈ l → p a
| [] eqtt a ainl := absurd ainl !not_mem_nil
| (b::l) eqtt a ainbl := by_cases
(suppose p b, or.elim (eq_or_mem_of_mem_cons ainbl)
(suppose a = b, by rewrite [this]; exact `p b`)
(suppose a ∈ l,
have check_pred p l = tt, by rewrite [check_pred_cons_of_pos _ `p b` at eqtt]; exact eqtt,
all_of_check_pred_eq_tt this `a ∈ l`))
(suppose ¬ p b,
by rewrite [check_pred_cons_of_neg _ this at eqtt]; exact (bool.no_confusion eqtt))
theorem ex_of_check_pred_eq_ff {p : A → Prop} [h : decidable_pred p] : ∀ {l : list A}, check_pred p l = ff → ∃ w, ¬ p w
| [] eqtt := bool.no_confusion eqtt
| (a::l) eqtt := by_cases
(suppose p a,
have check_pred p l = ff, by rewrite [check_pred_cons_of_pos _ this at eqtt]; exact eqtt,
ex_of_check_pred_eq_ff this)
(suppose ¬ p a, exists.intro a this)
end check_pred
definition decidable_forall_finite [instance] {A : Type} {p : A → Prop} [h₁ : fintype A] [h₂ : decidable_pred p]
: decidable (∀ x : A, p x) :=
match h₁ with
| fintype.mk e u c :=
match check_pred p e with
| tt := suppose check_pred p e = tt, inl (take a : A, all_of_check_pred_eq_tt this (c a))
| ff := suppose check_pred p e = ff,
inr (suppose ∀ x, p x,
obtain (a : A) (w : ¬ p a), from ex_of_check_pred_eq_ff `check_pred p e = ff`,
absurd (this a) w)
end rfl
end
definition decidable_exists_finite [instance] {A : Type} {p : A → Prop} [h₁ : fintype A] [h₂ : decidable_pred p]
: decidable (∃ x : A, p x) :=
match h₁ with
| fintype.mk e u c :=
match check_pred (λ a, ¬ p a) e with
| tt := λ h : check_pred (λ a, ¬ p a) e = tt, inr (λ ex : (∃ x, p x),
obtain x px, from ex,
absurd px (all_of_check_pred_eq_tt h (c x)))
| ff := λ h : check_pred (λ a, ¬ p a) e = ff, inl (
have ∃ x, ¬¬p x, from ex_of_check_pred_eq_ff h,
obtain x nnpx, from this, exists.intro x (not_not_elim nnpx))
end rfl
end
open list.as_type
-- Auxiliary function for returning a list with all elements of the type: (list.as_type l)
-- Remark ⟪s⟫ is notation for (list.as_type l)
-- We use this function to define the instance for (fintype ⟪s⟫)
private definition ltype_elems {A : Type} {s : list A} : Π {l : list A}, l ⊆ s → list ⟪s⟫
| [] h := []
| (a::l) h := lval a (h a !mem_cons) :: ltype_elems (sub_of_cons_sub h)
private theorem mem_of_mem_ltype_elems {A : Type} {a : A} {s : list A}
: Π {l : list A} {h : l ⊆ s} {m : a ∈ s}, mk a m ∈ ltype_elems h → a ∈ l
| [] h m lin := absurd lin !not_mem_nil
| (b::l) h m lin := or.elim (eq_or_mem_of_mem_cons lin)
(suppose mk a m = mk b (h b (mem_cons b l)),
as_type.no_confusion this (λ aeqb em, by rewrite [aeqb]; exact !mem_cons))
(suppose mk a m ∈ ltype_elems (sub_of_cons_sub h),
have a ∈ l, from mem_of_mem_ltype_elems this,
mem_cons_of_mem _ this)
private theorem nodup_ltype_elems {A : Type} {s : list A} : Π {l : list A} (d : nodup l) (h : l ⊆ s), nodup (ltype_elems h)
| [] d h := nodup_nil
| (a::l) d h :=
have d₁ : nodup l, from nodup_of_nodup_cons d,
have nainl : a ∉ l, from not_mem_of_nodup_cons d,
let h₁ : l ⊆ s := sub_of_cons_sub h in
have d₂ : nodup (ltype_elems h₁), from nodup_ltype_elems d₁ h₁,
have nin : mk a (h a (mem_cons a l)) ∉ ltype_elems h₁, from
assume ab, absurd (mem_of_mem_ltype_elems ab) nainl,
nodup_cons nin d₂
private theorem mem_ltype_elems {A : Type} {s : list A} {a : ⟪s⟫}
: Π {l : list A} (h : l ⊆ s), value a ∈ l → a ∈ ltype_elems h
| [] h vainl := absurd vainl !not_mem_nil
| (b::l) h vainbl := or.elim (eq_or_mem_of_mem_cons vainbl)
(λ vaeqb : value a = b,
begin
revert vaeqb h,
-- TODO(Leo): check why 'cases a with va, ma' produces an incorrect proof
eapply as_type.cases_on a,
intro va ma vaeqb,
rewrite -vaeqb, intro h,
apply mem_cons
end)
(λ vainl : value a ∈ l,
have aux : a ∈ ltype_elems (sub_of_cons_sub h), from mem_ltype_elems (sub_of_cons_sub h) vainl,
mem_cons_of_mem _ aux)
definition fintype_list_as_type [instance] {A : Type} [h : decidable_eq A] {s : list A} : fintype ⟪s⟫ :=
let nds : list A := erase_dup s in
have sub₁ : nds ⊆ s, from erase_dup_sub s,
have sub₂ : s ⊆ nds, from sub_erase_dup s,
have dnds : nodup nds, from nodup_erase_dup s,
let e : list ⟪s⟫ := ltype_elems sub₁ in
fintype.mk
e
(nodup_ltype_elems dnds sub₁)
(take a : ⟪s⟫,
show a ∈ e, from
have value a ∈ s, from is_member a,
have value a ∈ nds, from sub₂ this,
mem_ltype_elems sub₁ this)
|
c36cf45be52a1bdb7eaee6042259bc3a73f8904f | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/algebra/opposites.lean | f6481c810de7e289722bf7a118ab1e6fbbfc41e3 | [
"Apache-2.0"
] | permissive | ilitzroth/mathlib | ea647e67f1fdfd19a0f7bdc5504e8acec6180011 | 5254ef14e3465f6504306132fe3ba9cec9ffff16 | refs/heads/master | 1,680,086,661,182 | 1,617,715,647,000 | 1,617,715,647,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,991 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import data.opposite
import algebra.field
import algebra.group.commute
import group_theory.group_action.defs
import data.equiv.mul_add
/-!
# Algebraic operations on `αᵒᵖ`
This file records several basic facts about the opposite of an algebraic structure, e.g. the
opposite of a ring is a ring (with multiplication `x * y = yx`). Use is made of the identity
functions `op : α → αᵒᵖ` and `unop : αᵒᵖ → α`.
-/
namespace opposite
universes u
variables (α : Type u)
instance [has_add α] : has_add (opposite α) :=
{ add := λ x y, op (unop x + unop y) }
instance [has_sub α] : has_sub (opposite α) :=
{ sub := λ x y, op (unop x - unop y) }
instance [add_semigroup α] : add_semigroup (opposite α) :=
{ add_assoc := λ x y z, unop_injective $ add_assoc (unop x) (unop y) (unop z),
.. opposite.has_add α }
instance [add_left_cancel_semigroup α] : add_left_cancel_semigroup (opposite α) :=
{ add_left_cancel := λ x y z H, unop_injective $ add_left_cancel $ op_injective H,
.. opposite.add_semigroup α }
instance [add_right_cancel_semigroup α] : add_right_cancel_semigroup (opposite α) :=
{ add_right_cancel := λ x y z H, unop_injective $ add_right_cancel $ op_injective H,
.. opposite.add_semigroup α }
instance [add_comm_semigroup α] : add_comm_semigroup (opposite α) :=
{ add_comm := λ x y, unop_injective $ add_comm (unop x) (unop y),
.. opposite.add_semigroup α }
instance [has_zero α] : has_zero (opposite α) :=
{ zero := op 0 }
instance [nontrivial α] : nontrivial (opposite α) :=
let ⟨x, y, h⟩ := exists_pair_ne α in nontrivial_of_ne (op x) (op y) (op_injective.ne h)
section
local attribute [reducible] opposite
@[simp] lemma unop_eq_zero_iff [has_zero α] (a : αᵒᵖ) : a.unop = (0 : α) ↔ a = (0 : αᵒᵖ) :=
iff.refl _
@[simp] lemma op_eq_zero_iff [has_zero α] (a : α) : op a = (0 : αᵒᵖ) ↔ a = (0 : α) :=
iff.refl _
end
instance [add_zero_class α] : add_zero_class (opposite α) :=
{ zero_add := λ x, unop_injective $ zero_add $ unop x,
add_zero := λ x, unop_injective $ add_zero $ unop x,
.. opposite.has_add α, .. opposite.has_zero α }
instance [add_monoid α] : add_monoid (opposite α) :=
{ .. opposite.add_semigroup α, .. opposite.add_zero_class α }
instance [add_comm_monoid α] : add_comm_monoid (opposite α) :=
{ .. opposite.add_monoid α, .. opposite.add_comm_semigroup α }
instance [has_neg α] : has_neg (opposite α) :=
{ neg := λ x, op $ -(unop x) }
instance [add_group α] : add_group (opposite α) :=
{ add_left_neg := λ x, unop_injective $ add_left_neg $ unop x,
sub_eq_add_neg := λ x y, unop_injective $ sub_eq_add_neg (unop x) (unop y),
.. opposite.add_monoid α, .. opposite.has_neg α, .. opposite.has_sub α }
instance [add_comm_group α] : add_comm_group (opposite α) :=
{ .. opposite.add_group α, .. opposite.add_comm_monoid α }
instance [has_mul α] : has_mul (opposite α) :=
{ mul := λ x y, op (unop y * unop x) }
instance [semigroup α] : semigroup (opposite α) :=
{ mul_assoc := λ x y z, unop_injective $ eq.symm $ mul_assoc (unop z) (unop y) (unop x),
.. opposite.has_mul α }
instance [right_cancel_semigroup α] : left_cancel_semigroup (opposite α) :=
{ mul_left_cancel := λ x y z H, unop_injective $ mul_right_cancel $ op_injective H,
.. opposite.semigroup α }
instance [left_cancel_semigroup α] : right_cancel_semigroup (opposite α) :=
{ mul_right_cancel := λ x y z H, unop_injective $ mul_left_cancel $ op_injective H,
.. opposite.semigroup α }
instance [comm_semigroup α] : comm_semigroup (opposite α) :=
{ mul_comm := λ x y, unop_injective $ mul_comm (unop y) (unop x),
.. opposite.semigroup α }
instance [has_one α] : has_one (opposite α) :=
{ one := op 1 }
section
local attribute [reducible] opposite
@[simp] lemma unop_eq_one_iff [has_one α] (a : αᵒᵖ) : a.unop = 1 ↔ a = 1 :=
iff.refl _
@[simp] lemma op_eq_one_iff [has_one α] (a : α) : op a = 1 ↔ a = 1 :=
iff.refl _
end
instance [mul_one_class α] : mul_one_class (opposite α) :=
{ one_mul := λ x, unop_injective $ mul_one $ unop x,
mul_one := λ x, unop_injective $ one_mul $ unop x,
.. opposite.has_mul α, .. opposite.has_one α }
instance [monoid α] : monoid (opposite α) :=
{ .. opposite.semigroup α, .. opposite.mul_one_class α }
instance [comm_monoid α] : comm_monoid (opposite α) :=
{ .. opposite.monoid α, .. opposite.comm_semigroup α }
instance [has_inv α] : has_inv (opposite α) :=
{ inv := λ x, op $ (unop x)⁻¹ }
instance [group α] : group (opposite α) :=
{ mul_left_inv := λ x, unop_injective $ mul_inv_self $ unop x,
.. opposite.monoid α, .. opposite.has_inv α }
instance [comm_group α] : comm_group (opposite α) :=
{ .. opposite.group α, .. opposite.comm_monoid α }
instance [distrib α] : distrib (opposite α) :=
{ left_distrib := λ x y z, unop_injective $ add_mul (unop y) (unop z) (unop x),
right_distrib := λ x y z, unop_injective $ mul_add (unop z) (unop x) (unop y),
.. opposite.has_add α, .. opposite.has_mul α }
instance [semiring α] : semiring (opposite α) :=
{ zero_mul := λ x, unop_injective $ mul_zero $ unop x,
mul_zero := λ x, unop_injective $ zero_mul $ unop x,
.. opposite.add_comm_monoid α, .. opposite.monoid α, .. opposite.distrib α }
instance [ring α] : ring (opposite α) :=
{ .. opposite.add_comm_group α, .. opposite.monoid α, .. opposite.semiring α }
instance [comm_ring α] : comm_ring (opposite α) :=
{ .. opposite.ring α, .. opposite.comm_semigroup α }
instance [has_zero α] [has_mul α] [no_zero_divisors α] : no_zero_divisors (opposite α) :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := λ x y (H : op (_ * _) = op (0:α)),
or.cases_on (eq_zero_or_eq_zero_of_mul_eq_zero $ op_injective H)
(λ hy, or.inr $ unop_injective $ hy) (λ hx, or.inl $ unop_injective $ hx), }
instance [integral_domain α] : integral_domain (opposite α) :=
{ .. opposite.no_zero_divisors α, .. opposite.comm_ring α, .. opposite.nontrivial α }
instance [field α] : field (opposite α) :=
{ mul_inv_cancel := λ x hx, unop_injective $ inv_mul_cancel $ λ hx', hx $ unop_injective hx',
inv_zero := unop_injective inv_zero,
.. opposite.comm_ring α, .. opposite.has_inv α, .. opposite.nontrivial α }
instance (R : Type*) [has_scalar R α] : has_scalar R (opposite α) :=
{ smul := λ c x, op (c • unop x) }
instance (R : Type*) [monoid R] [mul_action R α] : mul_action R (opposite α) :=
{ one_smul := λ x, unop_injective $ one_smul R (unop x),
mul_smul := λ r₁ r₂ x, unop_injective $ mul_smul r₁ r₂ (unop x),
..opposite.has_scalar α R }
instance (R : Type*) [monoid R] [add_monoid α] [distrib_mul_action R α] :
distrib_mul_action R (opposite α) :=
{ smul_add := λ r x₁ x₂, unop_injective $ smul_add r (unop x₁) (unop x₂),
smul_zero := λ r, unop_injective $ smul_zero r,
..opposite.mul_action α R }
@[simp] lemma op_zero [has_zero α] : op (0 : α) = 0 := rfl
@[simp] lemma unop_zero [has_zero α] : unop (0 : αᵒᵖ) = 0 := rfl
@[simp] lemma op_one [has_one α] : op (1 : α) = 1 := rfl
@[simp] lemma unop_one [has_one α] : unop (1 : αᵒᵖ) = 1 := rfl
variable {α}
@[simp] lemma op_add [has_add α] (x y : α) : op (x + y) = op x + op y := rfl
@[simp] lemma unop_add [has_add α] (x y : αᵒᵖ) : unop (x + y) = unop x + unop y := rfl
@[simp] lemma op_neg [has_neg α] (x : α) : op (-x) = -op x := rfl
@[simp] lemma unop_neg [has_neg α] (x : αᵒᵖ) : unop (-x) = -unop x := rfl
@[simp] lemma op_mul [has_mul α] (x y : α) : op (x * y) = op y * op x := rfl
@[simp] lemma unop_mul [has_mul α] (x y : αᵒᵖ) : unop (x * y) = unop y * unop x := rfl
@[simp] lemma op_inv [has_inv α] (x : α) : op (x⁻¹) = (op x)⁻¹ := rfl
@[simp] lemma unop_inv [has_inv α] (x : αᵒᵖ) : unop (x⁻¹) = (unop x)⁻¹ := rfl
@[simp] lemma op_sub [add_group α] (x y : α) : op (x - y) = op x - op y := rfl
@[simp] lemma unop_sub [add_group α] (x y : αᵒᵖ) : unop (x - y) = unop x - unop y := rfl
@[simp] lemma op_smul {R : Type*} [has_scalar R α] (c : R) (a : α) : op (c • a) = c • op a := rfl
@[simp] lemma unop_smul {R : Type*} [has_scalar R α] (c : R) (a : αᵒᵖ) :
unop (c • a) = c • unop a := rfl
lemma semiconj_by.op [has_mul α] {a x y : α} (h : semiconj_by a x y) :
semiconj_by (op a) (op y) (op x) :=
begin
dunfold semiconj_by,
rw [← op_mul, ← op_mul, h.eq]
end
lemma semiconj_by.unop [has_mul α] {a x y : αᵒᵖ} (h : semiconj_by a x y) :
semiconj_by (unop a) (unop y) (unop x) :=
begin
dunfold semiconj_by,
rw [← unop_mul, ← unop_mul, h.eq]
end
@[simp] lemma semiconj_by_op [has_mul α] {a x y : α} :
semiconj_by (op a) (op y) (op x) ↔ semiconj_by a x y :=
begin
split,
{ intro h,
rw [← unop_op a, ← unop_op x, ← unop_op y],
exact semiconj_by.unop h },
{ intro h,
exact semiconj_by.op h }
end
@[simp] lemma semiconj_by_unop [has_mul α] {a x y : αᵒᵖ} :
semiconj_by (unop a) (unop y) (unop x) ↔ semiconj_by a x y :=
by conv_rhs { rw [← op_unop a, ← op_unop x, ← op_unop y, semiconj_by_op] }
lemma commute.op [has_mul α] {x y : α} (h : commute x y) : commute (op x) (op y) :=
begin
dunfold commute at h ⊢,
exact semiconj_by.op h
end
lemma commute.unop [has_mul α] {x y : αᵒᵖ} (h : commute x y) : commute (unop x) (unop y) :=
begin
dunfold commute at h ⊢,
exact semiconj_by.unop h
end
@[simp] lemma commute_op [has_mul α] {x y : α} :
commute (op x) (op y) ↔ commute x y :=
begin
dunfold commute,
rw semiconj_by_op
end
@[simp] lemma commute_unop [has_mul α] {x y : αᵒᵖ} :
commute (unop x) (unop y) ↔ commute x y :=
begin
dunfold commute,
rw semiconj_by_unop
end
/-- The function `op` is an additive equivalence. -/
def op_add_equiv [has_add α] : α ≃+ αᵒᵖ :=
{ map_add' := λ a b, rfl, .. equiv_to_opposite }
@[simp] lemma coe_op_add_equiv [has_add α] : (op_add_equiv : α → αᵒᵖ) = op := rfl
@[simp] lemma coe_op_add_equiv_symm [has_add α] :
(op_add_equiv.symm : αᵒᵖ → α) = unop := rfl
@[simp] lemma op_add_equiv_to_equiv [has_add α] :
(op_add_equiv : α ≃+ αᵒᵖ).to_equiv = equiv_to_opposite :=
rfl
end opposite
open opposite
/-- A ring homomorphism `f : R →+* S` such that `f x` commutes with `f y` for all `x, y` defines
a ring homomorphism to `Sᵒᵖ`. -/
def ring_hom.to_opposite {R S : Type*} [semiring R] [semiring S] (f : R →+* S)
(hf : ∀ x y, commute (f x) (f y)) : R →+* Sᵒᵖ :=
{ map_one' := congr_arg op f.map_one,
map_mul' := λ x y, by simp [(hf x y).eq],
.. (opposite.op_add_equiv : S ≃+ Sᵒᵖ).to_add_monoid_hom.comp ↑f }
@[simp] lemma ring_hom.coe_to_opposite {R S : Type*} [semiring R] [semiring S] (f : R →+* S)
(hf : ∀ x y, commute (f x) (f y)) : ⇑(f.to_opposite hf) = op ∘ f := rfl
|
eace4030d02c3eee56dc5e0290f32faa3538ccb2 | 0cdd90d2d613d48e567989a6bb0c5808ed4ad727 | /src/padovan_sequence.lean | d1033fc161dc0291a14e4b86076036492742f773 | [
"MIT"
] | permissive | SnobbyDragon/happynumbers | e749077a6aed5b6158f168293466130c729ddbcc | e71d1c05d461a27db7dfbaa34561ad7c1e141836 | refs/heads/master | 1,669,489,237,904 | 1,596,426,948,000 | 1,596,426,948,000 | 275,297,492 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,352 | lean | import data.stream.basic
import tactic
namespace padovan
private def padovan_aux_step : (ℕ × ℕ × ℕ) → (ℕ × ℕ × ℕ) := λ t, ⟨t.2.1, t.2.2, t.1 + t.2.1⟩
private def padovan_aux_stream : stream (ℕ × ℕ × ℕ) := stream.iterate padovan_aux_step ⟨1, 1, 1⟩
def padovan (n : ℕ) : ℕ := (padovan_aux_stream n).1
@[simp] lemma padovan_zero : padovan 0 = 1 := rfl
@[simp] lemma padovan_one : padovan 1 = 1 := rfl
@[simp] lemma padovan_two : padovan 2 = 1 := rfl
private lemma padovan_aux_stream_succ {n : ℕ} :
padovan_aux_stream (n + 1) = padovan_aux_step (padovan_aux_stream n) :=
begin
change (stream.nth (n + 1) $ stream.iterate padovan_aux_step ⟨1, 1, 1⟩) =
padovan_aux_step (stream.nth n $ stream.iterate padovan_aux_step ⟨1, 1, 1⟩),
rw [stream.nth_succ_iterate, stream.map_iterate, stream.nth_map],
end
lemma padovan_succ_succ_succ {n : ℕ} : padovan (n + 3) = padovan n + padovan (n + 1) :=
begin
simp only [padovan, padovan_aux_stream_succ, padovan_aux_step],
end
lemma padovan_increasing (n m : ℕ): n ≤ m → padovan n ≤ padovan m :=
begin
intros h,
sorry
end
lemma padovan_pos (n : ℕ) : 0 < padovan n :=
begin
induction n with k nk,
{ rw padovan_zero,
linarith,
},
{ cases k,
{ rw padovan_one,
linarith,
},
{ sorry },
},
end
end padovan
|
810074d73bbf280b1224ef8b8db4a40b48460d74 | a25cc44b447f2fbde61e4b848318762eb892f2bf | /src/upper.lean | c537093514445da8f21550f711a69b0821d3e70d | [] | no_license | NeilStrickland/itloc | 20f9b9bcaa4d5840f7f32e84108174b294d84b1a | 5b13b5b418766d10926b983eb3dd2ac42abf63d8 | refs/heads/master | 1,592,747,030,159 | 1,578,138,004,000 | 1,578,138,004,000 | 197,470,135 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,403 | lean | /-
Copyright (c) 2019 Neil Strickland. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Neil Strickland
Given a finite poset `P`, we define `upper P` to be the set of
subsets `U ⊆ P` that are closed upwards. We order this by
*reverse* inclusion, to ensure that the map
`u : p ↦ {x : p ≤ x}` is a morphism of posets. We prove that
`upper P` is a bounded distributive lattice with this order.
-/
import basic
universes uP uQ uR uS
variables (P : Type uP) [decidable_eq P] [fintype P]
variables [partial_order P] [decidable_rel (has_le.le : P → P → Prop)]
namespace poset
variable {P}
def is_upper : finset P → Prop :=
λ (U : finset P), ∀ (p₀ p₁ : P), (p₀ ≤ p₁) → (p₀ ∈ U) → (p₁ ∈ U)
variable (P)
instance is_upper_decidable (U : finset P) : decidable (is_upper U) :=
by { dsimp[is_upper], apply_instance }
lemma is_upper_empty : is_upper (@finset.empty P) :=
λ p₀ p₁ hp hU, (finset.not_mem_empty p₀ hU).elim
lemma is_upper_univ : is_upper (@finset.univ P _) :=
λ p₀ p₁ hp hU, (finset.mem_univ p₁)
variable {P}
lemma is_upper_union (U V : finset P) (hU : is_upper U) (hV : is_upper V) :
is_upper (U ∪ V) :=
λ p₀ p₁ hp hpUV,
begin
rcases (finset.mem_union.mp hpUV) with hpU | hpV,
{exact finset.mem_union_left V (hU p₀ p₁ hp hpU)},
{exact finset.mem_union_right U (hV p₀ p₁ hp hpV)},
end
lemma is_upper_inter (U V : finset P) (hU : is_upper U) (hV : is_upper V) :
is_upper (U ∩ V) :=
λ p₀ p₁ hp hpUV,
begin
replace hpUV := finset.mem_inter.mp hpUV,
apply finset.mem_inter.mpr,
split,
{exact (hU p₀ p₁ hp hpUV.left)},
{exact (hV p₀ p₁ hp hpUV.right)},
end
def distrib (U V W : finset P) :
U ∩ (V ∪ W) ⊆ (U ∩ V) ∪ (U ∩ W) :=
begin
intros A h,
rw[finset.mem_inter,finset.mem_union] at h,
rw[finset.mem_union,finset.mem_inter,finset.mem_inter],
rcases h with ⟨hU,hV | hW⟩,
exact or.inl ⟨hU,hV⟩,
exact or.inr ⟨hU,hW⟩
end
variable (P)
def upper := { U : finset P // is_upper U }
namespace upper
instance : fintype (upper P) :=
by {dsimp [upper], apply_instance}
/-- To print an upper set, ignore the upperness property and
just print the underlying set.
-/
instance [has_repr P] : has_repr (upper P) := ⟨λ U, repr U.val⟩
variable {P}
def els (U : upper P) : Type* := {p // p ∈ U.val}
variable (P)
instance els_order (U : upper P) : partial_order U.els :=
by { unfold els, apply_instance }
instance : has_mem P (upper P) := ⟨λ p U, p ∈ U.val⟩
/-- Two upper sets are equal iff they have the same elements. -/
@[ext] lemma ext (U₀ U₁ : upper P) :
(∀ (p : P), p ∈ U₀ ↔ p ∈ U₁) → U₀ = U₁ :=
begin
intro h,
apply subtype.eq,
ext p,
exact h p
end
/-- upper P has a natural structure as a bounded distributive lattice.
-/
instance bdl : lattice.bounded_distrib_lattice (upper P) := {
le := λ U V, V.val ⊆ U.val,
le_refl := λ U, le_refl U.val,
le_antisymm := λ U V (h0 : V.val ⊆ U.val) (h1 : U.val ⊆ V.val),
begin apply subtype.eq, exact le_antisymm h1 h0, end,
le_trans := λ U V W (h0 : V.val ⊆ U.val) (h1 : W.val ⊆ V.val),
@le_trans (finset P) _ W.val V.val U.val h1 h0,
bot := ⟨finset.univ,is_upper_univ P⟩,
top := ⟨finset.empty,is_upper_empty P⟩,
inf := λ U V, ⟨U.val ∪ V.val,
is_upper_union U.val V.val U.property V.property⟩,
sup := λ U V, ⟨U.val ∩ V.val,
is_upper_inter U.val V.val U.property V.property⟩,
bot_le := λ U,finset.subset_univ U.val,
le_top := λ U,finset.empty_subset U.val,
le_sup_left := λ U V,finset.inter_subset_left U.val V.val,
le_sup_right := λ U V,finset.inter_subset_right U.val V.val,
sup_le := λ U V W
(U_le_W : W.val ⊆ U.val)
(V_le_W : W.val ⊆ V.val),
finset.subset_inter U_le_W V_le_W,
inf_le_left := λ U V,finset.subset_union_left U.val V.val,
inf_le_right := λ U V,finset.subset_union_right U.val V.val,
le_inf := λ U V W
(U_le_V : V.val ⊆ U.val)
(U_le_W : W.val ⊆ U.val),
finset.union_subset U_le_V U_le_W,
le_sup_inf := λ U V W A h,
distrib U.val V.val W.val h,
}
variable {P}
lemma mem_bot (p : P) : p ∈ (⊥ : upper P) := finset.mem_univ p
lemma not_mem_top (p : P) : p ∉ (⊤ : upper P) := finset.not_mem_empty p
lemma mem_inf {U V : upper P} (p : P) : p ∈ U ⊓ V ↔ (p ∈ U ∨ p ∈ V) :=
finset.mem_union
lemma mem_sup {U V : upper P} (p : P) : p ∈ U ⊔ V ↔ (p ∈ U ∧ p ∈ V) :=
finset.mem_inter
/-
We embed `P` in `upper P` using the map `u : A ↦ { B : A ⊆ B }`
-/
def u : poset.hom P (upper P) :=
⟨λ p, ⟨finset.univ.filter (λ q, p ≤ q),
begin intros q r hqr hpq,
replace hpq := (finset.mem_filter.mp hpq).right,
exact finset.mem_filter.mpr ⟨finset.mem_univ r,le_trans hpq hqr⟩,
end
⟩, λ p₀ p₁ h q q_in_up₀,
begin
have : p₀ ≤ q := le_trans h (finset.mem_filter.mp q_in_up₀).right,
exact finset.mem_filter.mpr ⟨finset.mem_univ q, this⟩
end⟩
lemma mem_u (p q : P) : p ∈ (@u P _ _ _ _ q) ↔ q ≤ p :=
begin
change p ∈ finset.filter _ _ ↔ q ≤ p,
simp [finset.mem_filter, finset.mem_univ]
end
end upper
end poset |
2c6359c0877566e3db2e58e9cc48e576835a1c6d | ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5 | /src/Lean/Compiler/IR/ElimDeadBranches.lean | 84749f19a76397e11a5a7ce0a21f1dfac29d0e1e | [
"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 | 10,549 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Compiler.IR.Format
import Lean.Compiler.IR.Basic
import Lean.Compiler.IR.CompilerM
namespace Lean.IR.UnreachableBranches
/-- Value used in the abstract interpreter -/
inductive Value where
| bot -- undefined
| top -- any value
| ctor (i : CtorInfo) (vs : Array Value)
| choice (vs : List Value)
deriving Inhabited
namespace Value
protected partial def beq : Value → Value → Bool
| bot, bot => true
| top, top => true
| ctor i₁ vs₁, ctor i₂ vs₂ => i₁ == i₂ && Array.isEqv vs₁ vs₂ Value.beq
| choice vs₁, choice vs₂ =>
vs₁.all (fun v₁ => vs₂.any fun v₂ => Value.beq v₁ v₂)
&&
vs₂.all (fun v₂ => vs₁.any fun v₁ => Value.beq v₁ v₂)
| _, _ => false
instance : BEq Value := ⟨Value.beq⟩
partial def addChoice (merge : Value → Value → Value) : List Value → Value → List Value
| [], v => [v]
| v₁@(ctor i₁ vs₁) :: cs, v₂@(ctor i₂ vs₂) =>
if i₁ == i₂ then merge v₁ v₂ :: cs
else v₁ :: addChoice merge cs v₂
| _, _ => panic! "invalid addChoice"
partial def merge : Value → Value → Value
| bot, v => v
| v, bot => v
| top, _ => top
| _, top => top
| v₁@(ctor i₁ vs₁), v₂@(ctor i₂ vs₂) =>
if i₁ == i₂ then ctor i₁ $ vs₁.size.fold (init := #[]) fun i r => r.push (merge vs₁[i] vs₂[i])
else choice [v₁, v₂]
| choice vs₁, choice vs₂ => choice $ vs₁.foldl (addChoice merge) vs₂
| choice vs, v => choice $ addChoice merge vs v
| v, choice vs => choice $ addChoice merge vs v
protected partial def format : Value → Format
| top => "top"
| bot => "bot"
| choice vs => fmt "@" ++ @List.format _ ⟨Value.format⟩ vs
| ctor i vs => fmt "#" ++ if vs.isEmpty then fmt i.name else Format.paren (fmt i.name ++ @formatArray _ ⟨Value.format⟩ vs)
instance : ToFormat Value := ⟨Value.format⟩
instance : ToString Value := ⟨Format.pretty ∘ Value.format⟩
/- Make sure constructors of recursive inductive datatypes can only occur once in each path.
We use this function this function to implement a simple widening operation for our abstract
interpreter. -/
partial def truncate (env : Environment) : Value → NameSet → Value
| ctor i vs, found =>
let I := i.name.getPrefix
if found.contains I then
top
else
let cont (found' : NameSet) : Value :=
ctor i (vs.map fun v => truncate env v found')
match env.find? I with
| some (ConstantInfo.inductInfo d) =>
if d.isRec then cont (found.insert I)
else cont found
| _ => cont found
| choice vs, found =>
let newVs := vs.map fun v => truncate env v found
if newVs.elem top then top
else choice newVs
| v, _ => v
/- Widening operator that guarantees termination in our abstract interpreter. -/
def widening (env : Environment) (v₁ v₂ : Value) : Value :=
truncate env (merge v₁ v₂) {}
end Value
abbrev FunctionSummaries := SMap FunId Value
builtin_initialize functionSummariesExt : SimplePersistentEnvExtension (FunId × Value) FunctionSummaries ←
registerSimplePersistentEnvExtension {
name := `unreachBranchesFunSummary,
addImportedFn := fun as =>
let cache : FunctionSummaries := mkStateFromImportedEntries (fun s (p : FunId × Value) => s.insert p.1 p.2) {} as
cache.switch,
addEntryFn := fun s ⟨e, n⟩ => s.insert e n
}
def addFunctionSummary (env : Environment) (fid : FunId) (v : Value) : Environment :=
functionSummariesExt.addEntry env (fid, v)
def getFunctionSummary? (env : Environment) (fid : FunId) : Option Value :=
(functionSummariesExt.getState env).find? fid
abbrev Assignment := Std.HashMap VarId Value
structure InterpContext where
currFnIdx : Nat := 0
decls : Array Decl
env : Environment
lctx : LocalContext := {}
structure InterpState where
assignments : Array Assignment
funVals : Std.PArray Value -- we take snapshots during fixpoint computations
abbrev M := ReaderT InterpContext (StateM InterpState)
open Value
def findVarValue (x : VarId) : M Value := do
let ctx ← read
let s ← get
let assignment := s.assignments[ctx.currFnIdx]
pure $ assignment.findD x bot
def findArgValue (arg : Arg) : M Value :=
match arg with
| Arg.var x => findVarValue x
| _ => pure top
def updateVarAssignment (x : VarId) (v : Value) : M Unit := do
let v' ← findVarValue x
let ctx ← read
modify fun s => { s with assignments := s.assignments.modify ctx.currFnIdx fun a => a.insert x (merge v v') }
def resetVarAssignment (x : VarId) : M Unit := do
let ctx ← read
modify fun s => { s with assignments := s.assignments.modify ctx.currFnIdx fun a => a.insert x Value.bot }
def resetParamAssignment (y : Param) : M Unit :=
resetVarAssignment y.x
partial def projValue : Value → Nat → Value
| ctor _ vs, i => vs.getD i bot
| choice vs, i => vs.foldl (fun r v => merge r (projValue v i)) bot
| v, _ => v
def interpExpr : Expr → M Value
| Expr.ctor i ys => return ctor i (← ys.mapM fun y => findArgValue y)
| Expr.proj i x => return projValue (← findVarValue x) i
| Expr.fap fid ys => do
let ctx ← read
match getFunctionSummary? ctx.env fid with
| some v => pure v
| none => do
let s ← get
match ctx.decls.findIdx? (fun decl => decl.name == fid) with
| some idx => pure s.funVals[idx]
| none => pure top
| _ => pure top
partial def containsCtor : Value → CtorInfo → Bool
| top, _ => true
| ctor i _, j => i == j
| choice vs, j => vs.any $ fun v => containsCtor v j
| _, _ => false
def updateCurrFnSummary (v : Value) : M Unit := do
let ctx ← read
let currFnIdx := ctx.currFnIdx
modify fun s => { s with funVals := s.funVals.modify currFnIdx (fun v' => widening ctx.env v v') }
/-- Return true if the assignment of at least one parameter has been updated. -/
def updateJPParamsAssignment (ys : Array Param) (xs : Array Arg) : M Bool := do
let ctx ← read
let currFnIdx := ctx.currFnIdx
ys.size.foldM (init := false) fun i r => do
let y := ys[i]
let x := xs[i]
let yVal ← findVarValue y.x
let xVal ← findArgValue x
let newVal := merge yVal xVal
if newVal == yVal then
pure r
else
modify fun s => { s with assignments := s.assignments.modify currFnIdx fun a => a.insert y.x newVal }
pure true
private partial def resetNestedJPParams : FnBody → M Unit
| FnBody.jdecl _ ys b k => do
let ctx ← read
let currFnIdx := ctx.currFnIdx
ys.forM resetParamAssignment
/- Remark we don't need to reset the parameters of joint-points
nested in `b` since they will be reset if this JP is used. -/
resetNestedJPParams k
| FnBody.case _ _ _ alts =>
alts.forM fun alt => match alt with
| Alt.ctor _ b => resetNestedJPParams b
| Alt.default b => resetNestedJPParams b
| e => do unless e.isTerminal do resetNestedJPParams e.body
partial def interpFnBody : FnBody → M Unit
| FnBody.vdecl x _ e b => do
let v ← interpExpr e
updateVarAssignment x v
interpFnBody b
| FnBody.jdecl j ys v b =>
withReader (fun ctx => { ctx with lctx := ctx.lctx.addJP j ys v }) do
interpFnBody b
| FnBody.case _ x _ alts => do
let v ← findVarValue x
alts.forM fun alt => do
match alt with
| Alt.ctor i b => if containsCtor v i then interpFnBody b
| Alt.default b => interpFnBody b
| FnBody.ret x => do
let v ← findArgValue x
-- dbgTrace ("ret " ++ toString v) $ fun _ =>
updateCurrFnSummary v
| FnBody.jmp j xs => do
let ctx ← read
let ys := (ctx.lctx.getJPParams j).get!
let b := (ctx.lctx.getJPBody j).get!
let updated ← updateJPParamsAssignment ys xs
if updated then
-- We must reset the value of nested join-point parameters since they depend on `ys` values
resetNestedJPParams b
interpFnBody b
| e => do
unless e.isTerminal do
interpFnBody e.body
def inferStep : M Bool := do
let ctx ← read
modify fun s => { s with assignments := ctx.decls.map fun _ => {} }
ctx.decls.size.foldM (init := false) fun idx modified => do
match ctx.decls[idx] with
| Decl.fdecl fid ys _ b => do
let s ← get
let currVals := s.funVals[idx]
withReader (fun ctx => { ctx with currFnIdx := idx }) do
ys.forM fun y => updateVarAssignment y.x top
interpFnBody b
let s ← get
let newVals := s.funVals[idx]
pure (modified || currVals != newVals)
| Decl.extern _ _ _ _ => pure modified
partial def inferMain : M Unit := do
let modified ← inferStep
if modified then inferMain else pure ()
partial def elimDeadAux (assignment : Assignment) : FnBody → FnBody
| FnBody.vdecl x t e b => FnBody.vdecl x t e (elimDeadAux assignment b)
| FnBody.jdecl j ys v b => FnBody.jdecl j ys (elimDeadAux assignment v) (elimDeadAux assignment b)
| FnBody.case tid x xType alts =>
let v := assignment.findD x bot
let alts := alts.map fun alt =>
match alt with
| Alt.ctor i b => Alt.ctor i $ if containsCtor v i then elimDeadAux assignment b else FnBody.unreachable
| Alt.default b => Alt.default (elimDeadAux assignment b)
FnBody.case tid x xType alts
| e =>
if e.isTerminal then e
else
let (instr, b) := e.split
let b := elimDeadAux assignment b
instr.setBody b
partial def elimDead (assignment : Assignment) : Decl → Decl
| Decl.fdecl fid ys t b => Decl.fdecl fid ys t $ elimDeadAux assignment b
| other => other
end UnreachableBranches
open UnreachableBranches
def elimDeadBranches (decls : Array Decl) : CompilerM (Array Decl) := do
let s ← get
let env := s.env
let assignments : Array Assignment := decls.map fun _ => {}
let funVals := Std.mkPArray decls.size Value.bot
let ctx : InterpContext := { decls := decls, env := env }
let s : InterpState := { assignments := assignments, funVals := funVals }
let (_, s) := (inferMain ctx).run s
let funVals := s.funVals
let assignments := s.assignments
modify fun s =>
let env := decls.size.fold (init := s.env) fun i env =>
addFunctionSummary env decls[i].name funVals[i]
{ s with env := env }
pure $ decls.mapIdx fun i decl => elimDead assignments[i] decl
end Lean.IR
|
2a5c6167ee91ec546a0d0f189afa2b20441ded33 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/algebra/direct_sum/basic.lean | 7e79c3d2a6c9fd74c6b6317a2f1ab7589a5a6d8e | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,366 | 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 data.dfinsupp
import group_theory.submonoid.operations
import group_theory.subgroup.basic
/-!
# Direct sum
This file defines the direct sum of abelian groups, indexed by a discrete type.
## Notation
`⨁ i, β i` is the n-ary direct sum `direct_sum`.
This notation is in the `direct_sum` locale, accessible after `open_locale direct_sum`.
## References
* https://en.wikipedia.org/wiki/Direct_sum
-/
open_locale big_operators
universes u v w u₁
variables (ι : Type v) [dec_ι : decidable_eq ι] (β : ι → Type w)
/-- `direct_sum β` is the direct sum of a family of additive commutative monoids `β i`.
Note: `open_locale direct_sum` will enable the notation `⨁ i, β i` for `direct_sum β`. -/
@[derive [add_comm_monoid, inhabited]]
def direct_sum [Π i, add_comm_monoid (β i)] : Type* := Π₀ i, β i
instance [Π i, add_comm_monoid (β i)] : has_coe_to_fun (direct_sum ι β) (λ _, Π i : ι, β i) :=
dfinsupp.has_coe_to_fun
localized "notation `⨁` binders `, ` r:(scoped f, direct_sum _ f) := r" in direct_sum
namespace direct_sum
variables {ι}
section add_comm_group
variables [Π i, add_comm_group (β i)]
instance : add_comm_group (direct_sum ι β) := dfinsupp.add_comm_group
variables {β}
@[simp] lemma sub_apply (g₁ g₂ : ⨁ i, β i) (i : ι) : (g₁ - g₂) i = g₁ i - g₂ i :=
dfinsupp.sub_apply _ _ _
end add_comm_group
variables [Π i, add_comm_monoid (β i)]
@[simp] lemma zero_apply (i : ι) : (0 : ⨁ i, β i) i = 0 := rfl
variables {β}
@[simp] lemma add_apply (g₁ g₂ : ⨁ i, β i) (i : ι) : (g₁ + g₂) i = g₁ i + g₂ i :=
dfinsupp.add_apply _ _ _
variables (β)
include dec_ι
/-- `mk β s x` is the element of `⨁ i, β i` that is zero outside `s`
and has coefficient `x i` for `i` in `s`. -/
def mk (s : finset ι) : (Π i : (↑s : set ι), β i.1) →+ ⨁ i, β i :=
{ to_fun := dfinsupp.mk s,
map_add' := λ _ _, dfinsupp.mk_add,
map_zero' := dfinsupp.mk_zero, }
/-- `of i` is the natural inclusion map from `β i` to `⨁ i, β i`. -/
def of (i : ι) : β i →+ ⨁ i, β i :=
dfinsupp.single_add_hom β i
variables {β}
theorem mk_injective (s : finset ι) : function.injective (mk β s) :=
dfinsupp.mk_injective s
theorem of_injective (i : ι) : function.injective (of β i) :=
dfinsupp.single_injective
@[elab_as_eliminator]
protected theorem induction_on {C : (⨁ i, β i) → Prop}
(x : ⨁ i, β i) (H_zero : C 0)
(H_basic : ∀ (i : ι) (x : β i), C (of β i x))
(H_plus : ∀ x y, C x → C y → C (x + y)) : C x :=
begin
apply dfinsupp.induction x H_zero,
intros i b f h1 h2 ih,
solve_by_elim
end
/-- If two additive homomorphisms from `⨁ i, β i` are equal on each `of β i y`,
then they are equal. -/
lemma add_hom_ext {γ : Type*} [add_monoid γ] ⦃f g : (⨁ i, β i) →+ γ⦄
(H : ∀ (i : ι) (y : β i), f (of _ i y) = g (of _ i y)) : f = g :=
dfinsupp.add_hom_ext H
/-- If two additive homomorphisms from `⨁ i, β i` are equal on each `of β i y`,
then they are equal.
See note [partially-applied ext lemmas]. -/
@[ext] lemma add_hom_ext' {γ : Type*} [add_monoid γ] ⦃f g : (⨁ i, β i) →+ γ⦄
(H : ∀ (i : ι), f.comp (of _ i) = g.comp (of _ i)) : f = g :=
add_hom_ext $ λ i, add_monoid_hom.congr_fun $ H i
variables {γ : Type u₁} [add_comm_monoid γ]
section to_add_monoid
variables (φ : Π i, β i →+ γ) (ψ : (⨁ i, β i) →+ γ)
/-- `to_add_monoid φ` is the natural homomorphism from `⨁ i, β i` to `γ`
induced by a family `φ` of homomorphisms `β i → γ`. -/
def to_add_monoid : (⨁ i, β i) →+ γ :=
(dfinsupp.lift_add_hom φ)
@[simp] lemma to_add_monoid_of (i) (x : β i) : to_add_monoid φ (of β i x) = φ i x :=
dfinsupp.lift_add_hom_apply_single φ i x
theorem to_add_monoid.unique (f : ⨁ i, β i) :
ψ f = to_add_monoid (λ i, ψ.comp (of β i)) f :=
by {congr, ext, simp [to_add_monoid, of]}
end to_add_monoid
section from_add_monoid
/-- `from_add_monoid φ` is the natural homomorphism from `γ` to `⨁ i, β i`
induced by a family `φ` of homomorphisms `γ → β i`.
Note that this is not an isomorphism. Not every homomorphism `γ →+ ⨁ i, β i` arises in this way. -/
def from_add_monoid : (⨁ i, γ →+ β i) →+ (γ →+ ⨁ i, β i) :=
to_add_monoid $ λ i, add_monoid_hom.comp_hom (of β i)
@[simp] lemma from_add_monoid_of (i : ι) (f : γ →+ β i) :
from_add_monoid (of _ i f) = (of _ i).comp f :=
by { rw [from_add_monoid, to_add_monoid_of], refl }
lemma from_add_monoid_of_apply (i : ι) (f : γ →+ β i) (x : γ) :
from_add_monoid (of _ i f) x = of _ i (f x) :=
by rw [from_add_monoid_of, add_monoid_hom.coe_comp]
end from_add_monoid
variables (β)
/-- `set_to_set β S T h` is the natural homomorphism `⨁ (i : S), β i → ⨁ (i : T), β i`,
where `h : S ⊆ T`. -/
-- TODO: generalize this to remove the assumption `S ⊆ T`.
def set_to_set (S T : set ι) (H : S ⊆ T) :
(⨁ (i : S), β i) →+ (⨁ (i : T), β i) :=
to_add_monoid $ λ i, of (λ (i : subtype T), β i) ⟨↑i, H i.prop⟩
variables {β}
omit dec_ι
/-- The natural equivalence between `⨁ _ : ι, M` and `M` when `unique ι`. -/
protected def id (M : Type v) (ι : Type* := punit) [add_comm_monoid M] [unique ι] :
(⨁ (_ : ι), M) ≃+ M :=
{ to_fun := direct_sum.to_add_monoid (λ _, add_monoid_hom.id M),
inv_fun := of (λ _, M) (default ι),
left_inv := λ x, direct_sum.induction_on x
(by rw [add_monoid_hom.map_zero, add_monoid_hom.map_zero])
(λ p x, by rw [unique.default_eq p, to_add_monoid_of]; refl)
(λ x y ihx ihy, by rw [add_monoid_hom.map_add, add_monoid_hom.map_add, ihx, ihy]),
right_inv := λ x, to_add_monoid_of _ _ _,
..direct_sum.to_add_monoid (λ _, add_monoid_hom.id M) }
/-- The `direct_sum` formed by a collection of `add_submonoid`s of `M` is said to be internal if the
canonical map `(⨁ i, A i) →+ M` is bijective.
See `direct_sum.add_subgroup_is_internal` for the same statement about `add_subgroup`s. -/
def add_submonoid_is_internal {M : Type*} [decidable_eq ι] [add_comm_monoid M]
(A : ι → add_submonoid M) : Prop :=
function.bijective (direct_sum.to_add_monoid (λ i, (A i).subtype) : (⨁ i, A i) →+ M)
lemma add_submonoid_is_internal.supr_eq_top {M : Type*} [decidable_eq ι] [add_comm_monoid M]
(A : ι → add_submonoid M)
(h : add_submonoid_is_internal A) : supr A = ⊤ :=
begin
rw [add_submonoid.supr_eq_mrange_dfinsupp_sum_add_hom, add_monoid_hom.mrange_top_iff_surjective],
exact function.bijective.surjective h,
end
/-- The `direct_sum` formed by a collection of `add_subgroup`s of `M` is said to be internal if the
canonical map `(⨁ i, A i) →+ M` is bijective.
See `direct_sum.submodule_is_internal` for the same statement about `submodules`s. -/
def add_subgroup_is_internal {M : Type*} [decidable_eq ι] [add_comm_group M]
(A : ι → add_subgroup M) : Prop :=
function.bijective (direct_sum.to_add_monoid (λ i, (A i).subtype) : (⨁ i, A i) →+ M)
lemma add_subgroup_is_internal.to_add_submonoid
{M : Type*} [decidable_eq ι] [add_comm_group M] (A : ι → add_subgroup M) :
add_subgroup_is_internal A ↔
add_submonoid_is_internal (λ i, (A i).to_add_submonoid) :=
iff.rfl
end direct_sum
|
60e8a17a9c93096f08a8f2c393bc430ec2416b81 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/tactic/lift.lean | c6d761db900212e2a916377d410a6529f1a0c4bc | [
"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 | 9,470 | lean | /-
Copyright (c) 2019 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import tactic.rcases
/-!
# lift tactic
This file defines the lift tactic, allowing the user to lift elements from one type to another
under a specified condition.
## Tags
lift, tactic
-/
universe variables u v w
/-- A class specifying that you can lift elements from `α` to `β` assuming `cond` is true.
Used by the tactic `lift`. -/
class can_lift (α : Type u) (β : Type v) : Type (max u v) :=
(coe : β → α)
(cond : α → Prop)
(prf : ∀(x : α), cond x → ∃(y : β), coe y = x)
open tactic
/--
A user attribute used internally by the `lift` tactic.
This should not be applied by hand.
-/
@[user_attribute]
meta def can_lift_attr : user_attribute (list name) :=
{ name := "_can_lift",
descr := "internal attribute used by the lift tactic",
cache_cfg := { mk_cache := λ _,
do { ls ← attribute.get_instances `instance,
ls.mfilter $ λ l,
do { (_,t) ← mk_const l >>= infer_type >>= open_pis,
return $ t.is_app_of `can_lift } },
dependencies := [`instance] } }
instance : can_lift ℤ ℕ :=
⟨coe, λ n, 0 ≤ n, λ n hn, ⟨n.nat_abs, int.nat_abs_of_nonneg hn⟩⟩
/-- Enable automatic handling of pi types in `can_lift`. -/
instance pi.can_lift (ι : Type u) (α : Π i : ι, Type v) (β : Π i : ι, Type w)
[Π i : ι, can_lift (α i) (β i)] :
can_lift (Π i : ι, α i) (Π i : ι, β i) :=
{ coe := λ f i, can_lift.coe (f i),
cond := λ f, ∀ i, can_lift.cond (β i) (f i),
prf := λ f hf, ⟨λ i, classical.some (can_lift.prf (f i) (hf i)), funext $ λ i,
classical.some_spec (can_lift.prf (f i) (hf i))⟩ }
namespace tactic
/--
Construct the proof of `cond x` in the lift tactic.
* `e` is the expression being lifted and `h` is the specified proof of `can_lift.cond e`.
* `old_tp` and `new_tp` are the arguments to `can_lift` and `inst` is the `can_lift`-instance.
* `s` and `to_unfold` contain the information of the simp set used to simplify.
If the proof was specified, we check whether it has the correct type.
If it doesn't have the correct type, we display an error message
(but first call dsimp on the expression in the message).
If the proof was not specified, we create assert it as a local constant.
(The name of this local constant doesn't matter, since `lift` will remove it from the context.)
-/
meta def get_lift_prf (h : option pexpr) (old_tp new_tp inst e : expr)
(s : simp_lemmas) (to_unfold : list name) : tactic expr :=
if h_some : h.is_some then
(do prf ← i_to_expr (option.get h_some), prf_ty ← infer_type prf,
expected_prf_ty ← mk_app `can_lift.cond [old_tp, new_tp, inst, e],
unify prf_ty expected_prf_ty <|>
(do expected_prf_ty2 ← s.dsimplify to_unfold expected_prf_ty,
pformat!"lift tactic failed. The type of\n {prf}\nis\n {prf_ty}\nbut it is expected to be\n {expected_prf_ty2}" >>= fail),
return prf)
else (do prf_nm ← get_unused_name,
prf ← mk_app `can_lift.cond [old_tp, new_tp, inst, e] >>= assert prf_nm,
dsimp_target s to_unfold {}, swap, return prf)
/-- Lift the expression `p` to the type `t`, with proof obligation given by `h`.
The list `n` is used for the two newly generated names, and to specify whether `h` should
remain in the local context. See the doc string of `tactic.interactive.lift` for more information.
-/
meta def lift (p : pexpr) (t : pexpr) (h : option pexpr) (n : list name) : tactic unit :=
do
propositional_goal <|>
fail "lift tactic failed. Tactic is only applicable when the target is a proposition.",
e ← i_to_expr p,
old_tp ← infer_type e,
new_tp ← i_to_expr t,
inst_type ← mk_app ``can_lift [old_tp, new_tp],
inst ← mk_instance inst_type <|>
pformat!"Failed to find a lift from {old_tp} to {new_tp}. Provide an instance of\n {inst_type}"
>>= fail,
/- make the simp set to get rid of `can_lift` projections -/
can_lift_instances ← can_lift_attr.get_cache >>= λ l, l.mmap resolve_name,
(s, to_unfold) ← mk_simp_set tt [] $ can_lift_instances.map simp_arg_type.expr,
prf_cond ← get_lift_prf h old_tp new_tp inst e s to_unfold,
let prf_nm := if prf_cond.is_local_constant then some prf_cond.local_pp_name else none,
/- We use mk_mapp to apply `can_lift.prf` to all but one argument, and then just use expr.app
for the last argument. For some reason we get an error when applying mk_mapp it to all
arguments. -/
prf_ex0 ← mk_mapp `can_lift.prf [old_tp, new_tp, inst, e],
let prf_ex := prf_ex0 prf_cond,
/- Find the name of the new variable -/
new_nm ← if n ≠ [] then return n.head
else if e.is_local_constant then return e.local_pp_name
else get_unused_name,
/- Find the name of the proof of the equation -/
eq_nm ← if hn : 1 < n.length then return (n.nth_le 1 hn)
else if e.is_local_constant then return `rfl
else get_unused_name `h,
/- We add the proof of the existential statement to the context and then apply
`dsimp` to it, unfolding all `can_lift` instances. -/
temp_nm ← get_unused_name,
temp_e ← note temp_nm none prf_ex,
dsimp_hyp temp_e s to_unfold {},
/- We case on the existential. We use `rcases` because `eq_nm` could be `rfl`. -/
rcases none (pexpr.of_expr temp_e) $ rcases_patt.tuple ([new_nm, eq_nm].map rcases_patt.one),
/- If the lifted variable is not a local constant, try to rewrite it away using the new equality-/
when (¬ e.is_local_constant) (get_local eq_nm >>=
λ e, interactive.rw ⟨[⟨⟨0, 0⟩, tt, (pexpr.of_expr e)⟩], none⟩ interactive.loc.wildcard),
/- If the proof `prf_cond` is a local constant, remove it from the context,
unless `n` specifies to keep it. -/
if h_prf_nm : prf_nm.is_some ∧ n.nth 2 ≠ prf_nm then
get_local (option.get h_prf_nm.1) >>= clear else skip
open lean.parser interactive interactive.types
reserve notation `to`
local postfix `?`:9001 := optional
/-- Parses an optional token "using" followed by a trailing `pexpr`. -/
meta def using_texpr := (tk "using" *> texpr)?
/-- Parses a token "to" followed by a trailing `pexpr`. -/
meta def to_texpr := (tk "to" *> texpr)
namespace interactive
/-- Lift an expression to another type.
Lift an expression to another type.
* Usage: `'lift' expr 'to' expr ('using' expr)? ('with' id (id id?)?)?`.
* If `n : ℤ` and `hn : n ≥ 0` then the tactic `lift n to ℕ using hn` creates a new
constant of type `ℕ`, also named `n` and replaces all occurrences of the old variable `(n : ℤ)`
with `↑n` (where `n` in the new variable). It will remove `n` and `hn` from the context.
+ So for example the tactic `lift n to ℕ using hn` transforms the goal
`n : ℤ, hn : n ≥ 0, h : P n ⊢ n = 3` to `n : ℕ, h : P ↑n ⊢ ↑n = 3`
(here `P` is some term of type `ℤ → Prop`).
* The argument `using hn` is optional, the tactic `lift n to ℕ` does the same, but also creates a
new subgoal that `n ≥ 0` (where `n` is the old variable).
+ So for example the tactic `lift n to ℕ` transforms the goal
`n : ℤ, h : P n ⊢ n = 3` to two goals
`n : ℕ, h : P ↑n ⊢ ↑n = 3` and `n : ℤ, h : P n ⊢ n ≥ 0`.
* You can also use `lift n to ℕ using e` where `e` is any expression of type `n ≥ 0`.
* Use `lift n to ℕ with k` to specify the name of the new variable.
* Use `lift n to ℕ with k hk` to also specify the name of the equality `↑k = n`. In this case, `n`
will remain in the context. You can use `rfl` for the name of `hk` to substitute `n` away
(i.e. the default behavior).
* You can also use `lift e to ℕ with k hk` where `e` is any expression of type `ℤ`.
In this case, the `hk` will always stay in the context, but it will be used to rewrite `e` in
all hypotheses and the target.
+ So for example the tactic `lift n + 3 to ℕ using hn with k hk` transforms the goal
`n : ℤ, hn : n + 3 ≥ 0, h : P (n + 3) ⊢ n + 3 = 2 * n` to the goal
`n : ℤ, k : ℕ, hk : ↑k = n + 3, h : P ↑k ⊢ ↑k = 2 * n`.
* The tactic `lift n to ℕ using h` will remove `h` from the context. If you want to keep it,
specify it again as the third argument to `with`, like this: `lift n to ℕ using h with n rfl h`.
* More generally, this can lift an expression from `α` to `β` assuming that there is an instance
of `can_lift α β`. In this case the proof obligation is specified by `can_lift.cond`.
* Given an instance `can_lift β γ`, it can also lift `α → β` to `α → γ`; more generally, given
`β : Π a : α, Type*`, `γ : Π a : α, Type*`, and `[Π a : α, can_lift (β a) (γ a)]`, it automatically
generates an instance `can_lift (Π a, β a) (Π a, γ a)`.
`lift` is in some sense dual to the `zify` tactic. `lift (z : ℤ) to ℕ` will change the type of an
integer `z` (in the supertype) to `ℕ` (the subtype), given a proof that `z ≥ 0`;
propositions concerning `z` will still be over `ℤ`. `zify` changes propositions about `ℕ` (the
subtype) to propositions about `ℤ` (the supertype), without changing the type of any variable.
-/
meta def lift (p : parse texpr) (t : parse to_texpr) (h : parse using_texpr)
(n : parse with_ident_list) : tactic unit :=
tactic.lift p t h n
add_tactic_doc
{ name := "lift",
category := doc_category.tactic,
decl_names := [`tactic.interactive.lift],
tags := ["coercions"] }
end interactive
end tactic
|
c70e5f61d0b6c10dcb04421a62fd0b7514c2cf39 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/tactic/ext.lean | 4bbc9171ddd3d2f61937a2a6f50e224025fffb38 | [
"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 | 11,728 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
-/
import tactic.rcases data.list.defs data.sum
universes u₁ u₂
open interactive interactive.types
open lean.parser nat tactic
/--
`derive_struct_ext_lemma n` generates two extensionality lemmas based on
the equality of all non-propositional projections.
On the following:
```lean
@[ext]
structure foo (α : Type*) :=
(x y : ℕ)
(z : {z // z < x})
(k : α)
(h : x < y)
```
`derive_struct_lemma` generates:
```lean
lemma foo.ext : ∀ {α : Type u_1} (x y : foo α),
x.x = y.x → x.y = y.y → x.z == y.z → x.k = y.k → x = y
lemma foo.ext_iff : ∀ {α : Type u_1} (x y : foo α),
x = y ↔ x.x = y.x ∧ x.y = y.y ∧ x.z == y.z ∧ x.k = y.k
```
-/
meta def derive_struct_ext_lemma (n : name) : tactic name :=
do e ← get_env,
fs ← e.structure_fields n,
d ← get_decl n,
n ← resolve_constant n,
let r := @expr.const tt n $ d.univ_params.map level.param,
(args,_) ← infer_type r >>= mk_local_pis,
let args := args.map expr.to_implicit_local_const,
let t := r.mk_app args,
x ← mk_local_def `x t,
y ← mk_local_def `y t,
let args_x := args ++ [x],
let args_y := args ++ [y],
bs ← fs.mmap $ λ f,
do { d ← get_decl (n ++ f),
let a := @expr.const tt (n ++ f) $ d.univ_params.map level.param,
t ← infer_type a,
s ← infer_type t,
if s ≠ `(Prop)
then do
let x := a.mk_app args_x,
let y := a.mk_app args_y,
t ← infer_type x,
t' ← infer_type y,
some <$> if t = t'
then mk_app `eq [x,y] >>= mk_local_def `h
else mk_mapp `heq [none,x,none,y] >>= mk_local_def `h
else pure none },
let bs := bs.filter_map id,
eq_t ← mk_app `eq [x,y],
t ← pis (args ++ [x,y] ++ bs) eq_t,
pr ← run_async $
do { (_,pr) ← solve_aux t (do
{ args ← intron args.length,
x ← intro1, y ← intro1,
cases x, cases y,
bs.mmap' (λ _,
do e ← intro1,
cases e),
reflexivity }),
instantiate_mvars pr },
let decl_n := n <.> "ext",
add_decl (declaration.thm decl_n d.univ_params t pr),
bs ← bs.mmap infer_type,
let rhs := expr.mk_and_lst bs,
iff_t ← mk_app `iff [eq_t,rhs],
t ← pis (args ++ [x,y]) iff_t,
pr ← run_async $
do { (_,pr) ← solve_aux t $ do
{ args ← intron args.length,
x ← intro1, y ← intro1,
cases x, cases y,
split,
solve1 $ do
{ h ← intro1, hs ← injection h, subst_vars,
repeat (refine ``( and.intro _ _ ) >> reflexivity ),
done <|> reflexivity },
solve1 $ do
{ repeat (do refine ``(and_imp.mpr _),
h ← intro1, cases h, skip ),
h ← intro1, cases h,
reflexivity } },
instantiate_mvars pr },
add_decl (declaration.thm (n <.> "ext_iff") d.univ_params t pr),
pure decl_n
meta def get_ext_subject : expr → tactic name
| (expr.pi n bi d b) :=
do v ← mk_local' n bi d,
b' ← whnf $ b.instantiate_var v,
get_ext_subject b'
| (expr.app _ e) :=
do t ← infer_type e >>= instantiate_mvars >>= head_beta,
if t.get_app_fn.is_constant then
pure $ t.get_app_fn.const_name
else if t.is_pi then
pure $ name.mk_numeral 0 name.anonymous
else if t.is_sort then
pure $ name.mk_numeral 1 name.anonymous
else do
t ← pp t,
fail format!"only constants and Pi types are supported: {t}"
| e := fail format!"Only expressions of the form `_ → _ → ... → R ... e are supported: {e}"
open native
@[reducible] def ext_param_type := option name ⊕ option name
meta def opt_minus : lean.parser (option name → ext_param_type) :=
sum.inl <$ tk "-" <|> pure sum.inr
meta def ext_param :=
opt_minus <*> ( name.mk_numeral 0 name.anonymous <$ brackets "(" ")" (tk "→" <|> tk "->") <|>
none <$ tk "*" <|>
some <$> ident )
meta def saturate_fun : name → tactic expr
| (name.mk_numeral 0 name.anonymous) :=
do v₀ ← mk_mvar,
v₁ ← mk_mvar,
return $ v₀.imp v₁
| (name.mk_numeral 1 name.anonymous) :=
do u ← mk_meta_univ,
pure $ expr.sort u
| n :=
do e ← resolve_constant n >>= mk_const,
a ← get_arity e,
e.mk_app <$> (list.iota a).mmap (λ _, mk_mvar)
meta def equiv_type_constr (n n' : name) : tactic unit :=
do e ← saturate_fun n,
e' ← saturate_fun n',
unify e e' <|> fail format!"{n} and {n'} are not definitionally equal types"
/--
Tag lemmas of the form:
```lean
@[ext]
lemma my_collection.ext (a b : my_collection)
(h : ∀ x, a.lookup x = b.lookup y) :
a = b := ...
```
The attribute indexes extensionality lemma using the type of the
objects (i.e. `my_collection`) which it gets from the statement of
the lemma. In some cases, the same lemma can be used to state the
extensionality of multiple types that are definitionally equivalent.
```lean
attribute [ext [(→),thunk,stream]] funext
```
Those parameters are cumulative. The following are equivalent:
```lean
attribute [ext [(→),thunk]] funext
attribute [ext [stream]] funext
```
and
```lean
attribute [ext [(→),thunk,stream]] funext
```
One removes type names from the list for one lemma with:
```lean
attribute [ext [-stream,-thunk]] funext
```
Also, the following:
```lean
@[ext]
lemma my_collection.ext (a b : my_collection)
(h : ∀ x, a.lookup x = b.lookup y) :
a = b := ...
```
is equivalent to
```lean
@[ext *]
lemma my_collection.ext (a b : my_collection)
(h : ∀ x, a.lookup x = b.lookup y) :
a = b := ...
```
This allows us specify type synonyms along with the type
that is referred to in the lemma statement.
```lean
@[ext [*,my_type_synonym]]
lemma my_collection.ext (a b : my_collection)
(h : ∀ x, a.lookup x = b.lookup y) :
a = b := ...
```
The `ext` attribute can be applied to a structure to generate its extensionality lemmas:
```lean
@[ext]
structure foo (α : Type*) :=
(x y : ℕ)
(z : {z // z < x})
(k : α)
(h : x < y)
```
will generate:
```lean
@[ext] lemma foo.ext : ∀ {α : Type u_1} (x y : foo α),
x.x = y.x → x.y = y.y → x.z == y.z → x.k = y.k → x = y
lemma foo.ext_iff : ∀ {α : Type u_1} (x y : foo α),
x = y ↔ x.x = y.x ∧ x.y = y.y ∧ x.z == y.z ∧ x.k = y.k
```
-/
@[user_attribute]
meta def extensional_attribute : user_attribute (name_map name)
(bool × list ext_param_type × list name × list (name × name)) :=
{ name := `ext,
descr := "lemmas usable by `ext` tactic",
cache_cfg := { mk_cache := λ ls,
do { attrs ← ls.mmap $ λ l,
do { ⟨_,_,ls,_⟩ ← extensional_attribute.get_param l,
pure $ prod.mk <$> ls <*> pure l },
pure $ rb_map.of_list $ attrs.join },
dependencies := [] },
parser :=
do { ls ← pure <$> ext_param <|> list_of ext_param <|> pure [],
m ← extensional_attribute.get_cache,
pure $ (ff,ls,[],m.to_list) },
after_set := some $ λ n _ b,
do (ff,ls,_,ls') ← extensional_attribute.get_param n | pure (),
e ← get_env,
n ← if (e.structure_fields n).is_some
then derive_struct_ext_lemma n
else pure n,
s ← mk_const n >>= infer_type >>= get_ext_subject,
let (rs,ls'') := if ls.empty
then ([],[s])
else ls.partition_map (sum.map (flip option.get_or_else s)
(flip option.get_or_else s)),
ls''.mmap' (equiv_type_constr s),
let l := ls'' ∪ (ls'.filter $ λ l, prod.snd l = n).map prod.fst \ rs,
extensional_attribute.set n (tt,[],l,[]) b }
add_tactic_doc
{ name := "ext",
category := doc_category.attr,
decl_names := [`extensional_attribute],
tags := ["rewrite", "logic"] }
-- We mark some existing extensionality lemmas.
attribute [ext] array.ext propext
attribute [ext [(→),thunk]] _root_.funext
-- We create some extensionality lemmas for existing structures.
attribute [ext] ulift
namespace plift
-- This is stronger than the one generated automatically.
@[ext] lemma ext {P : Prop} (a b : plift P) : a = b :=
begin
cases a, cases b, refl
end
end plift
-- Conservatively, we'll only add extensionality lemmas for `has_*` structures
-- as they become useful.
attribute [ext] has_zero
namespace tactic
meta def try_intros : ext_patt → tactic ext_patt
| [] := try intros $> []
| (x::xs) :=
do tgt ← target >>= whnf,
if tgt.is_pi
then rintro [x] >> try_intros xs
else pure (x :: xs)
meta def ext1 (xs : ext_patt) (cfg : apply_cfg := {}) : tactic ext_patt :=
do subject ← target >>= get_ext_subject,
m ← extensional_attribute.get_cache,
do { rule ← m.find subject,
applyc rule cfg } <|>
do { ls ← attribute.get_instances `ext,
ls.any_of (λ n, applyc n cfg) } <|>
fail format!"no applicable extensionality rule found for {subject}",
try_intros xs
meta def ext : ext_patt → option ℕ → tactic unit
| _ (some 0) := skip
| xs n := focus1 $ do
ys ← ext1 xs, try (ext ys (nat.pred <$> n))
local postfix `?`:9001 := optional
local postfix *:9001 := many
/--
`ext1 id` selects and apply one extensionality lemma (with attribute
`ext`), using `id`, if provided, to name a local constant
introduced by the lemma. If `id` is omitted, the local constant is
named automatically, as per `intro`.
-/
meta def interactive.ext1 (xs : parse ext_parse) : tactic unit :=
ext1 xs $> ()
/--
- `ext` applies as many extensionality lemmas as possible;
- `ext ids`, with `ids` a list of identifiers, finds extentionality and applies them
until it runs out of identifiers in `ids` to name the local constants.
When trying to prove:
```lean
α β : Type,
f g : α → set β
⊢ f = g
```
applying `ext x y` yields:
```lean
α β : Type,
f g : α → set β,
x : α,
y : β
⊢ y ∈ f x ↔ y ∈ f x
```
by applying functional extensionality and set extensionality.
A maximum depth can be provided with `ext x y z : 3`.
-/
meta def interactive.ext : parse ext_parse → parse (tk ":" *> small_nat)? → tactic unit
| [] (some n) := iterate_range 1 n (ext1 [] $> ())
| [] none := repeat1 (ext1 [] $> ())
| xs n := tactic.ext xs n
/--
* `ext1 id` selects and apply one extensionality lemma (with
attribute `ext`), using `id`, if provided, to name a
local constant introduced by the lemma. If `id` is omitted, the
local constant is named automatically, as per `intro`.
* `ext` applies as many extensionality lemmas as possible;
* `ext ids`, with `ids` a list of identifiers, finds extensionality lemmas
and applies them until it runs out of identifiers in `ids` to name
the local constants.
When trying to prove:
```lean
α β : Type,
f g : α → set β
⊢ f = g
```
applying `ext x y` yields:
```lean
α β : Type,
f g : α → set β,
x : α,
y : β
⊢ y ∈ f x ↔ y ∈ g x
```
by applying functional extensionality and set extensionality.
A maximum depth can be provided with `ext x y z : 3`.
-/
add_tactic_doc
{ name := "ext1 / ext",
category := doc_category.tactic,
decl_names := [`tactic.interactive.ext1, `tactic.interactive.ext],
tags := ["rewrite", "logic"] }
end tactic
|
452be4e40dabf6e680f9b97e9808a137e4f45b4e | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/algebra/char_p/basic.lean | 47f7376d3777663b4b67ef7133b052399b284374 | [
"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 | 15,080 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Joey van Langen, Casper Putz
-/
import data.nat.choose
import data.int.modeq
import algebra.iterate_hom
import group_theory.order_of_element
/-!
# Characteristic of semirings
-/
universes u v
variables (R : Type u)
/-- The generator of the kernel of the unique homomorphism ℕ → R for a semiring R -/
class char_p [add_monoid R] [has_one R] (p : ℕ) : Prop :=
(cast_eq_zero_iff [] : ∀ x:ℕ, (x:R) = 0 ↔ p ∣ x)
theorem char_p.cast_eq_zero [add_monoid R] [has_one R] (p : ℕ) [char_p R p] :
(p:R) = 0 :=
(char_p.cast_eq_zero_iff R p p).2 (dvd_refl p)
@[simp] lemma char_p.cast_card_eq_zero [add_group R] [has_one R] [fintype R] :
(fintype.card R : R) = 0 :=
by rw [← nsmul_one, card_nsmul_eq_zero]
lemma char_p.int_cast_eq_zero_iff [add_group R] [has_one R] (p : ℕ) [char_p R p]
(a : ℤ) :
(a : R) = 0 ↔ (p:ℤ) ∣ a :=
begin
rcases lt_trichotomy a 0 with h|rfl|h,
{ rw [← neg_eq_zero, ← int.cast_neg, ← dvd_neg],
lift -a to ℕ using neg_nonneg.mpr (le_of_lt h) with b,
rw [int.cast_coe_nat, char_p.cast_eq_zero_iff R p, int.coe_nat_dvd] },
{ simp only [int.cast_zero, eq_self_iff_true, dvd_zero] },
{ lift a to ℕ using (le_of_lt h) with b,
rw [int.cast_coe_nat, char_p.cast_eq_zero_iff R p, int.coe_nat_dvd] }
end
lemma char_p.int_coe_eq_int_coe_iff [add_group R] [has_one R] (p : ℕ) [char_p R p] (a b : ℤ) :
(a : R) = (b : R) ↔ a ≡ b [ZMOD p] :=
by rw [eq_comm, ←sub_eq_zero, ←int.cast_sub,
char_p.int_cast_eq_zero_iff R p, int.modeq.modeq_iff_dvd]
theorem char_p.eq [add_monoid R] [has_one R] {p q : ℕ} (c1 : char_p R p) (c2 : char_p R q) :
p = q :=
nat.dvd_antisymm
((char_p.cast_eq_zero_iff R p q).1 (char_p.cast_eq_zero _ _))
((char_p.cast_eq_zero_iff R q p).1 (char_p.cast_eq_zero _ _))
instance char_p.of_char_zero [add_monoid R] [has_one R] [char_zero R] : char_p R 0 :=
⟨λ x, by rw [zero_dvd_iff, ← nat.cast_zero, nat.cast_inj]⟩
theorem char_p.exists [semiring R] : ∃ p, char_p R p :=
by letI := classical.dec_eq R; exact
classical.by_cases
(assume H : ∀ p:ℕ, (p:R) = 0 → p = 0, ⟨0,
⟨λ x, by rw [zero_dvd_iff]; exact ⟨H x, by rintro rfl; refl⟩⟩⟩)
(λ H, ⟨nat.find (not_forall.1 H), ⟨λ x,
⟨λ H1, nat.dvd_of_mod_eq_zero (by_contradiction $ λ H2,
nat.find_min (not_forall.1 H)
(nat.mod_lt x $ nat.pos_of_ne_zero $ not_of_not_imp $
nat.find_spec (not_forall.1 H))
(not_imp_of_and_not ⟨by rwa [← nat.mod_add_div x (nat.find (not_forall.1 H)),
nat.cast_add, nat.cast_mul, of_not_not (not_not_of_not_imp $ nat.find_spec
(not_forall.1 H)),
zero_mul, add_zero] at H1, H2⟩)),
λ H1, by rw [← nat.mul_div_cancel' H1, nat.cast_mul,
of_not_not (not_not_of_not_imp $ nat.find_spec (not_forall.1 H)), zero_mul]⟩⟩⟩)
theorem char_p.exists_unique [semiring R] : ∃! p, char_p R p :=
let ⟨c, H⟩ := char_p.exists R in ⟨c, H, λ y H2, char_p.eq R H2 H⟩
theorem char_p.congr {R : Type u} [semiring R] {p : ℕ} (q : ℕ) [hq : char_p R q] (h : q = p) :
char_p R p :=
h ▸ hq
/-- Noncomputable function that outputs the unique characteristic of a semiring. -/
noncomputable def ring_char [semiring R] : ℕ :=
classical.some (char_p.exists_unique R)
namespace ring_char
variables [semiring R]
theorem spec : ∀ x:ℕ, (x:R) = 0 ↔ ring_char R ∣ x :=
by letI := (classical.some_spec (char_p.exists_unique R)).1;
unfold ring_char; exact char_p.cast_eq_zero_iff R (ring_char R)
theorem eq {p : ℕ} (C : char_p R p) : p = ring_char R :=
(classical.some_spec (char_p.exists_unique R)).2 p C
instance char_p : char_p R (ring_char R) :=
⟨spec R⟩
variables {R}
theorem of_eq {p : ℕ} (h : ring_char R = p) : char_p R p :=
char_p.congr (ring_char R) h
theorem eq_iff {p : ℕ} : ring_char R = p ↔ char_p R p :=
⟨of_eq, eq.symm ∘ eq R⟩
theorem dvd {x : ℕ} (hx : (x : R) = 0) : ring_char R ∣ x :=
(spec R x).1 hx
@[simp]
lemma eq_zero [char_zero R] : ring_char R = 0 := (eq R (char_p.of_char_zero R)).symm
end ring_char
theorem add_pow_char_of_commute [semiring R] {p : ℕ} [fact p.prime]
[char_p R p] (x y : R) (h : commute x y) :
(x + y)^p = x^p + y^p :=
begin
rw [commute.add_pow h, finset.sum_range_succ_comm, nat.sub_self, pow_zero, nat.choose_self],
rw [nat.cast_one, mul_one, mul_one], congr' 1,
convert finset.sum_eq_single 0 _ _,
{ simp only [mul_one, one_mul, nat.choose_zero_right, nat.sub_zero, nat.cast_one, pow_zero] },
{ intros b h1 h2,
suffices : (p.choose b : R) = 0, { rw this, simp },
rw char_p.cast_eq_zero_iff R p,
refine nat.prime.dvd_choose_self (pos_iff_ne_zero.mpr h2) _ (fact.out _),
rwa ← finset.mem_range },
{ intro h1,
contrapose! h1,
rw finset.mem_range,
exact nat.prime.pos (fact.out _) }
end
theorem add_pow_char_pow_of_commute [semiring R] {p : ℕ} [fact p.prime]
[char_p R p] {n : ℕ} (x y : R) (h : commute x y) :
(x + y) ^ (p ^ n) = x ^ (p ^ n) + y ^ (p ^ n) :=
begin
induction n, { simp, },
rw [pow_succ', pow_mul, pow_mul, pow_mul, n_ih],
apply add_pow_char_of_commute, apply commute.pow_pow h,
end
theorem sub_pow_char_of_commute [ring R] {p : ℕ} [fact p.prime]
[char_p R p] (x y : R) (h : commute x y) :
(x - y)^p = x^p - y^p :=
begin
rw [eq_sub_iff_add_eq, ← add_pow_char_of_commute _ _ _ (commute.sub_left h rfl)],
simp, repeat {apply_instance},
end
theorem sub_pow_char_pow_of_commute [ring R] {p : ℕ} [fact p.prime]
[char_p R p] {n : ℕ} (x y : R) (h : commute x y) :
(x - y) ^ (p ^ n) = x ^ (p ^ n) - y ^ (p ^ n) :=
begin
induction n, { simp, },
rw [pow_succ', pow_mul, pow_mul, pow_mul, n_ih],
apply sub_pow_char_of_commute, apply commute.pow_pow h,
end
theorem add_pow_char [comm_semiring R] {p : ℕ} [fact p.prime]
[char_p R p] (x y : R) : (x + y)^p = x^p + y^p :=
add_pow_char_of_commute _ _ _ (commute.all _ _)
theorem add_pow_char_pow [comm_semiring R] {p : ℕ} [fact p.prime]
[char_p R p] {n : ℕ} (x y : R) :
(x + y) ^ (p ^ n) = x ^ (p ^ n) + y ^ (p ^ n) :=
add_pow_char_pow_of_commute _ _ _ (commute.all _ _)
theorem sub_pow_char [comm_ring R] {p : ℕ} [fact p.prime]
[char_p R p] (x y : R) : (x - y)^p = x^p - y^p :=
sub_pow_char_of_commute _ _ _ (commute.all _ _)
theorem sub_pow_char_pow [comm_ring R] {p : ℕ} [fact p.prime]
[char_p R p] {n : ℕ} (x y : R) :
(x - y) ^ (p ^ n) = x ^ (p ^ n) - y ^ (p ^ n) :=
sub_pow_char_pow_of_commute _ _ _ (commute.all _ _)
lemma eq_iff_modeq_int [ring R] (p : ℕ) [char_p R p] (a b : ℤ) :
(a : R) = b ↔ a ≡ b [ZMOD p] :=
by rw [eq_comm, ←sub_eq_zero, ←int.cast_sub,
char_p.int_cast_eq_zero_iff R p, int.modeq.modeq_iff_dvd]
lemma char_p.neg_one_ne_one [ring R] (p : ℕ) [char_p R p] [fact (2 < p)] :
(-1 : R) ≠ (1 : R) :=
begin
suffices : (2 : R) ≠ 0,
{ symmetry, rw [ne.def, ← sub_eq_zero, sub_neg_eq_add], exact this },
assume h,
rw [show (2 : R) = (2 : ℕ), by norm_cast] at h,
have := (char_p.cast_eq_zero_iff R p 2).mp h,
have := nat.le_of_dvd dec_trivial this,
rw fact_iff at *, linarith,
end
lemma ring_hom.char_p_iff_char_p {K L : Type*} [field K] [field L] (f : K →+* L) (p : ℕ) :
char_p K p ↔ char_p L p :=
begin
split;
{ introI _c, constructor, intro n,
rw [← @char_p.cast_eq_zero_iff _ _ _ p _c n, ← f.injective.eq_iff, f.map_nat_cast, f.map_zero] }
end
section frobenius
section comm_semiring
variables [comm_semiring R] {S : Type v} [comm_semiring S] (f : R →* S) (g : R →+* S)
(p : ℕ) [fact p.prime] [char_p R p] [char_p S p] (x y : R)
/-- The frobenius map that sends x to x^p -/
def frobenius : R →+* R :=
{ to_fun := λ x, x^p,
map_one' := one_pow p,
map_mul' := λ x y, mul_pow x y p,
map_zero' := zero_pow (lt_trans zero_lt_one (fact.out (nat.prime p)).one_lt),
map_add' := add_pow_char R }
variable {R}
theorem frobenius_def : frobenius R p x = x ^ p := rfl
theorem iterate_frobenius (n : ℕ) : (frobenius R p)^[n] x = x ^ p ^ n :=
begin
induction n, {simp},
rw [function.iterate_succ', pow_succ', pow_mul, function.comp_apply, frobenius_def, n_ih]
end
theorem frobenius_mul : frobenius R p (x * y) = frobenius R p x * frobenius R p y :=
(frobenius R p).map_mul x y
theorem frobenius_one : frobenius R p 1 = 1 := one_pow _
theorem monoid_hom.map_frobenius : f (frobenius R p x) = frobenius S p (f x) :=
f.map_pow x p
theorem ring_hom.map_frobenius : g (frobenius R p x) = frobenius S p (g x) :=
g.map_pow x p
theorem monoid_hom.map_iterate_frobenius (n : ℕ) :
f (frobenius R p^[n] x) = (frobenius S p^[n] (f x)) :=
function.semiconj.iterate_right (f.map_frobenius p) n x
theorem ring_hom.map_iterate_frobenius (n : ℕ) :
g (frobenius R p^[n] x) = (frobenius S p^[n] (g x)) :=
g.to_monoid_hom.map_iterate_frobenius p x n
theorem monoid_hom.iterate_map_frobenius (f : R →* R) (p : ℕ) [fact p.prime] [char_p R p] (n : ℕ) :
f^[n] (frobenius R p x) = frobenius R p (f^[n] x) :=
f.iterate_map_pow _ _ _
theorem ring_hom.iterate_map_frobenius (f : R →+* R) (p : ℕ) [fact p.prime] [char_p R p] (n : ℕ) :
f^[n] (frobenius R p x) = frobenius R p (f^[n] x) :=
f.iterate_map_pow _ _ _
variable (R)
theorem frobenius_zero : frobenius R p 0 = 0 := (frobenius R p).map_zero
theorem frobenius_add : frobenius R p (x + y) = frobenius R p x + frobenius R p y :=
(frobenius R p).map_add x y
theorem frobenius_nat_cast (n : ℕ) : frobenius R p n = n := (frobenius R p).map_nat_cast n
end comm_semiring
section comm_ring
variables [comm_ring R] {S : Type v} [comm_ring S] (f : R →* S) (g : R →+* S)
(p : ℕ) [fact p.prime] [char_p R p] [char_p S p] (x y : R)
theorem frobenius_neg : frobenius R p (-x) = -frobenius R p x := (frobenius R p).map_neg x
theorem frobenius_sub : frobenius R p (x - y) = frobenius R p x - frobenius R p y :=
(frobenius R p).map_sub x y
end comm_ring
end frobenius
theorem frobenius_inj [comm_ring R] [no_zero_divisors R]
(p : ℕ) [fact p.prime] [char_p R p] :
function.injective (frobenius R p) :=
λ x h H, by { rw ← sub_eq_zero at H ⊢, rw ← frobenius_sub at H, exact pow_eq_zero H }
namespace char_p
section
variables [ring R]
lemma char_p_to_char_zero [char_p R 0] : char_zero R :=
char_zero_of_inj_zero $
λ n h0, eq_zero_of_zero_dvd ((cast_eq_zero_iff R 0 n).mp h0)
lemma cast_eq_mod (p : ℕ) [char_p R p] (k : ℕ) : (k : R) = (k % p : ℕ) :=
calc (k : R) = ↑(k % p + p * (k / p)) : by rw [nat.mod_add_div]
... = ↑(k % p) : by simp[cast_eq_zero]
theorem char_ne_zero_of_fintype (p : ℕ) [hc : char_p R p] [fintype R] : p ≠ 0 :=
assume h : p = 0,
have char_zero R := @char_p_to_char_zero R _ (h ▸ hc),
absurd (@nat.cast_injective R _ _ this) (not_injective_infinite_fintype coe)
end
section semiring
open nat
variables [semiring R]
theorem char_ne_one [nontrivial R] (p : ℕ) [hc : char_p R p] : p ≠ 1 :=
assume hp : p = 1,
have ( 1 : R) = 0, by simpa using (cast_eq_zero_iff R p 1).mpr (hp ▸ dvd_refl p),
absurd this one_ne_zero
section no_zero_divisors
variable [no_zero_divisors R]
theorem char_is_prime_of_two_le (p : ℕ) [hc : char_p R p] (hp : 2 ≤ p) : nat.prime p :=
suffices ∀d ∣ p, d = 1 ∨ d = p, from ⟨hp, this⟩,
assume (d : ℕ) (hdvd : ∃ e, p = d * e),
let ⟨e, hmul⟩ := hdvd in
have (p : R) = 0, from (cast_eq_zero_iff R p p).mpr (dvd_refl p),
have (d : R) * e = 0, from (@cast_mul R _ d e) ▸ (hmul ▸ this),
or.elim (eq_zero_or_eq_zero_of_mul_eq_zero this)
(assume hd : (d : R) = 0,
have p ∣ d, from (cast_eq_zero_iff R p d).mp hd,
show d = 1 ∨ d = p, from or.inr (dvd_antisymm ⟨e, hmul⟩ this))
(assume he : (e : R) = 0,
have p ∣ e, from (cast_eq_zero_iff R p e).mp he,
have e ∣ p, from dvd_of_mul_left_eq d (eq.symm hmul),
have e = p, from dvd_antisymm ‹e ∣ p› ‹p ∣ e›,
have h₀ : p > 0, from gt_of_ge_of_gt hp (nat.zero_lt_succ 1),
have d * p = 1 * p, by rw ‹e = p› at hmul; rw [one_mul]; exact eq.symm hmul,
show d = 1 ∨ d = p, from or.inl (eq_of_mul_eq_mul_right h₀ this))
section nontrivial
variables [nontrivial R]
theorem char_is_prime_or_zero (p : ℕ) [hc : char_p R p] : nat.prime p ∨ p = 0 :=
match p, hc with
| 0, _ := or.inr rfl
| 1, hc := absurd (eq.refl (1 : ℕ)) (@char_ne_one R _ _ (1 : ℕ) hc)
| (m+2), hc := or.inl (@char_is_prime_of_two_le R _ _ (m+2) hc (nat.le_add_left 2 m))
end
lemma char_is_prime_of_pos (p : ℕ) [h : fact (0 < p)] [char_p R p] : fact p.prime :=
⟨(char_p.char_is_prime_or_zero R _).resolve_right (pos_iff_ne_zero.1 h.1)⟩
end nontrivial
end no_zero_divisors
end semiring
section ring
variables (R) [ring R] [no_zero_divisors R] [nontrivial R] [fintype R]
theorem char_is_prime (p : ℕ) [char_p R p] :
p.prime :=
or.resolve_right (char_is_prime_or_zero R p) (char_ne_zero_of_fintype R p)
end ring
section char_one
variables {R} [semiring R]
@[priority 100] -- see Note [lower instance priority]
instance [char_p R 1] : subsingleton R :=
subsingleton.intro $
suffices ∀ (r : R), r = 0,
from assume a b, show a = b, by rw [this a, this b],
assume r,
calc r = 1 * r : by rw one_mul
... = (1 : ℕ) * r : by rw nat.cast_one
... = 0 * r : by rw char_p.cast_eq_zero
... = 0 : by rw zero_mul
lemma false_of_nontrivial_of_char_one [nontrivial R] [char_p R 1] : false :=
false_of_nontrivial_of_subsingleton R
lemma ring_char_ne_one [nontrivial R] : ring_char R ≠ 1 :=
by { intros h, apply @zero_ne_one R, symmetry, rw [←nat.cast_one, ring_char.spec, h], }
lemma nontrivial_of_char_ne_one {v : ℕ} (hv : v ≠ 1) [hr : char_p R v] :
nontrivial R :=
⟨⟨(1 : ℕ), 0, λ h, hv $ by rwa [char_p.cast_eq_zero_iff _ v, nat.dvd_one] at h; assumption ⟩⟩
end char_one
end char_p
section
variables (R) [comm_ring R] [fintype R] (n : ℕ)
lemma char_p_of_ne_zero (hn : fintype.card R = n) (hR : ∀ i < n, (i : R) = 0 → i = 0) :
char_p R n :=
{ cast_eq_zero_iff :=
begin
have H : (n : R) = 0, by { rw [← hn, char_p.cast_card_eq_zero] },
intro k,
split,
{ intro h,
rw [← nat.mod_add_div k n, nat.cast_add, nat.cast_mul, H, zero_mul, add_zero] at h,
rw nat.dvd_iff_mod_eq_zero,
apply hR _ (nat.mod_lt _ _) h,
rw [← hn, fintype.card_pos_iff],
exact ⟨0⟩, },
{ rintro ⟨k, rfl⟩, rw [nat.cast_mul, H, zero_mul] }
end }
lemma char_p_of_prime_pow_injective (p : ℕ) [hp : fact p.prime] (n : ℕ)
(hn : fintype.card R = p ^ n) (hR : ∀ i ≤ n, (p ^ i : R) = 0 → i = n) :
char_p R (p ^ n) :=
begin
obtain ⟨c, hc⟩ := char_p.exists R, resetI,
have hcpn : c ∣ p ^ n,
{ rw [← char_p.cast_eq_zero_iff R c, ← hn, char_p.cast_card_eq_zero], },
obtain ⟨i, hi, hc⟩ : ∃ i ≤ n, c = p ^ i, by rwa nat.dvd_prime_pow hp.1 at hcpn,
obtain rfl : i = n,
{ apply hR i hi, rw [← nat.cast_pow, ← hc, char_p.cast_eq_zero] },
rwa ← hc
end
end
|
908b43b22f1652bdbb74d22b5613a537cdb3d434 | a4673261e60b025e2c8c825dfa4ab9108246c32e | /tests/lean/run/doNotation1.lean | be0877b7e4afd3991dba704a1c0a479ff8fc06b7 | [
"Apache-2.0"
] | permissive | jcommelin/lean4 | c02dec0cc32c4bccab009285475f265f17d73228 | 2909313475588cc20ac0436e55548a4502050d0a | refs/heads/master | 1,674,129,550,893 | 1,606,415,348,000 | 1,606,415,348,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,057 | lean | open Lean
def f : IO Nat :=
pure 0
def g (x : Nat) : IO Nat :=
pure (x + 1)
def g1 {α : Type} (x : α) : IO (α × α) :=
pure (x, x)
def g2 (p : Nat × Nat) : Nat :=
p.1
-- set_option trace.Elab.definition true
def h (x : Nat) : StateT Nat IO Nat := do
let s ← get
let a ← f -- liftM inserted here
let b ← g1 1 -- liftM inserted here
let x := g2 b
IO.println b
pure (s+a)
def myPrint {α} [ToString α] (a : α) : IO Unit :=
IO.println s!">> {a}"
def h₂ (x : Nat) : StateT Nat IO Nat := do
let a ← h 1; -- liftM inserted here
IO.println x
let b ← g1 a -- liftM inserted here
if a > 100 then throw $ IO.userError "Error"
myPrint b.1 -- liftM inserted here
pure (a + 1)
def h₃ (x : Nat) : StateT Nat IO Nat := do
let m1 := do -- Type inferred from application below
g x -- liftM inserted here
IO.println 1
let m2 (y : Nat) := do -- Type inferred from application below
h (x+y) -- liftM inserted here
myPrint y -- liftM inserted here
let a ← h 1 -- liftM inserted here
IO.println x
let b ← g1 a; -- liftM inserted here
if a > 100 then throw $ IO.userError "Error"
myPrint b.1 -- liftM inserted here
m1
m2 a
pure 1
def tst0 : IO Unit := do
let a ← f
let x := a + 1
IO.println "hello"
IO.println x
def tst1 : IO Unit := do
let a ← f;
let x := a + 1;
IO.println "hello";
IO.println x;
def tst2 : IO Unit := do
let x := ← g $ (←f) + (←f);
IO.println "hello";
IO.println x
def tst3 : IO Unit := do
if (← g 1) > 0 then
IO.println "gt"
else
let x ← f;
let y ← g x;
IO.println y
def pred (x : Nat) : IO Bool := do
return (← g x) > 0
def tst4 (x : Nat) : IO Unit := do
if ← pred x then
IO.println "is true"
else
IO.println "is false"
def pred2 (x : Nat) : IO Bool := do
return x > 0
def tst5 (x : Nat) : IO (Option Nat) :=
if x > 10 then pure x else pure none
def tst6 (x : Nat) : StateT Nat IO (Option Nat) :=
if x > 10 then g x else pure none
syntax:max [doHash] "#" : term
partial def expandHash : Syntax → StateT Bool MacroM Syntax
| Syntax.node k args =>
if k == `doHash then do set true; `(←MonadState.get)
else do
let args ← args.mapM expandHash;
pure $ Syntax.node k args;
| stx => pure stx
@[macro Lean.Parser.Term.do] def expandDo : Macro :=
fun stx => do
let (stx, expanded) ← expandHash stx false;
if expanded then pure stx
else Macro.throwUnsupported
def tst7 : StateT (Nat × Nat) IO Unit := do
if #.1 == 0 then
IO.println "first field is zero"
else
IO.println "first field is not zero"
#check tst7
#eval tst7.run (0, 2)
def f1 (x : Nat) : StateT Nat IO Nat := do
IO.println "hello"
let mut z := x
let mut y := x
modify (· + 10)
if x > 0 then
y := 3*y
z := z + (← get) + (← get)
if x < (← get) then
IO.println s!">> {y}"
return y
else
IO.println s!"++ {z}"
return y+z
def f1Test : IO Unit := do
unless (← f1 30 |>.run' 0) == 140 do
throw $ IO.userError $ "error"
unless (← f1 5 |>.run' 0) == 15 do
throw $ IO.userError $ "error"
#eval f1Test
|
cd2eb7b882b829110c76ede0bd38fdc149f72c80 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/deprecated/subgroup.lean | 2cd64ba456c5d5f1dfe549da9b0f501037c99f47 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 25,257 | lean | /-
Copyright (c) 2018 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mitchell Rowett, Scott Morrison, Johan Commelin, Mario Carneiro,
Michael Howes
-/
import group_theory.subgroup.basic
import deprecated.submonoid
/-!
# Unbundled subgroups (deprecated)
This file is deprecated, and is no longer imported by anything in mathlib other than other
deprecated files, and test files. You should not need to import it.
This file defines unbundled multiplicative and additive subgroups. Instead of using this file,
please use `subgroup G` and `add_subgroup A`, defined in `group_theory.subgroup.basic`.
## Main definitions
`is_add_subgroup (S : set A)` : the predicate that `S` is the underlying subset of an additive
subgroup of `A`. The bundled variant `add_subgroup A` should be used in preference to this.
`is_subgroup (S : set G)` : the predicate that `S` is the underlying subset of a subgroup
of `G`. The bundled variant `subgroup G` should be used in preference to this.
## Tags
subgroup, subgroups, is_subgroup
-/
open set function
variables {G : Type*} {H : Type*} {A : Type*} {a a₁ a₂ b c: G}
section group
variables [group G] [add_group A]
/-- `s` is an additive subgroup: a set containing 0 and closed under addition and negation. -/
structure is_add_subgroup (s : set A) extends is_add_submonoid s : Prop :=
(neg_mem {a} : a ∈ s → -a ∈ s)
/-- `s` is a subgroup: a set containing 1 and closed under multiplication and inverse. -/
@[to_additive]
structure is_subgroup (s : set G) extends is_submonoid s : Prop :=
(inv_mem {a} : a ∈ s → a⁻¹ ∈ s)
@[to_additive]
lemma is_subgroup.div_mem {s : set G} (hs : is_subgroup s) {x y : G} (hx : x ∈ s) (hy : y ∈ s) :
x / y ∈ s :=
by simpa only [div_eq_mul_inv] using hs.mul_mem hx (hs.inv_mem hy)
lemma additive.is_add_subgroup
{s : set G} (hs : is_subgroup s) : @is_add_subgroup (additive G) _ s :=
@is_add_subgroup.mk (additive G) _ _ (additive.is_add_submonoid hs.to_is_submonoid) $
λ _, hs.inv_mem
theorem additive.is_add_subgroup_iff
{s : set G} : @is_add_subgroup (additive G) _ s ↔ is_subgroup s :=
⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_subgroup.mk G _ _ ⟨h₁, @h₂⟩ @h₃,
λ h, by exactI additive.is_add_subgroup h⟩
lemma multiplicative.is_subgroup
{s : set A} (hs : is_add_subgroup s) : @is_subgroup (multiplicative A) _ s :=
@is_subgroup.mk (multiplicative A) _ _ (multiplicative.is_submonoid hs.to_is_add_submonoid) $
λ _, hs.neg_mem
theorem multiplicative.is_subgroup_iff
{s : set A} : @is_subgroup (multiplicative A) _ s ↔ is_add_subgroup s :=
⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_add_subgroup.mk A _ _ ⟨h₁, @h₂⟩ @h₃,
λ h, by exactI multiplicative.is_subgroup h⟩
@[to_additive of_add_neg]
theorem is_subgroup.of_div (s : set G)
(one_mem : (1:G) ∈ s) (div_mem : ∀{a b:G}, a ∈ s → b ∈ s → a * b⁻¹ ∈ s) :
is_subgroup s :=
have inv_mem : ∀a, a ∈ s → a⁻¹ ∈ s, from
assume a ha,
have 1 * a⁻¹ ∈ s, from div_mem one_mem ha,
by simpa,
{ inv_mem := inv_mem,
mul_mem := assume a b ha hb,
have a * b⁻¹⁻¹ ∈ s, from div_mem ha (inv_mem b hb),
by simpa,
one_mem := one_mem }
theorem is_add_subgroup.of_sub (s : set A)
(zero_mem : (0:A) ∈ s) (sub_mem : ∀{a b:A}, a ∈ s → b ∈ s → a - b ∈ s) :
is_add_subgroup s :=
is_add_subgroup.of_add_neg s zero_mem
(λ x y hx hy, by simpa only [sub_eq_add_neg] using sub_mem hx hy)
@[to_additive]
lemma is_subgroup.inter {s₁ s₂ : set G} (hs₁ : is_subgroup s₁) (hs₂ : is_subgroup s₂) :
is_subgroup (s₁ ∩ s₂) :=
{ inv_mem := λ x hx, ⟨hs₁.inv_mem hx.1, hs₂.inv_mem hx.2⟩,
..is_submonoid.inter hs₁.to_is_submonoid hs₂.to_is_submonoid}
@[to_additive]
lemma is_subgroup.Inter {ι : Sort*} {s : ι → set G} (hs : ∀ y : ι, is_subgroup (s y)) :
is_subgroup (set.Inter s) :=
{ inv_mem := λ x h, set.mem_Inter.2 $ λ y, is_subgroup.inv_mem (hs _) (set.mem_Inter.1 h y),
..is_submonoid.Inter (λ y, (hs y).to_is_submonoid) }
@[to_additive]
lemma is_subgroup_Union_of_directed {ι : Type*} [hι : nonempty ι]
{s : ι → set G} (hs : ∀ i, is_subgroup (s i))
(directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) :
is_subgroup (⋃i, s i) :=
{ inv_mem := λ a ha,
let ⟨i, hi⟩ := set.mem_Union.1 ha in
set.mem_Union.2 ⟨i, (hs i).inv_mem hi⟩,
to_is_submonoid := is_submonoid_Union_of_directed (λ i, (hs i).to_is_submonoid) directed }
end group
namespace is_subgroup
open is_submonoid
variables [group G] {s : set G} (hs : is_subgroup s)
include hs
@[to_additive]
lemma inv_mem_iff : a⁻¹ ∈ s ↔ a ∈ s :=
⟨λ h, by simpa using hs.inv_mem h, inv_mem hs⟩
@[to_additive]
lemma mul_mem_cancel_right (h : a ∈ s) : b * a ∈ s ↔ b ∈ s :=
⟨λ hba, by simpa using hs.mul_mem hba (hs.inv_mem h), λ hb, hs.mul_mem hb h⟩
@[to_additive]
lemma mul_mem_cancel_left (h : a ∈ s) : a * b ∈ s ↔ b ∈ s :=
⟨λ hab, by simpa using hs.mul_mem (hs.inv_mem h) hab, hs.mul_mem h⟩
end is_subgroup
/-- `is_normal_add_subgroup (s : set A)` expresses the fact that `s` is a normal additive subgroup
of the additive group `A`. Important: the preferred way to say this in Lean is via bundled
subgroups `S : add_subgroup A` and `hs : S.normal`, and not via this structure. -/
structure is_normal_add_subgroup [add_group A] (s : set A) extends is_add_subgroup s : Prop :=
(normal : ∀ n ∈ s, ∀ g : A, g + n + -g ∈ s)
/-- `is_normal_subgroup (s : set G)` expresses the fact that `s` is a normal subgroup
of the group `G`. Important: the preferred way to say this in Lean is via bundled
subgroups `S : subgroup G` and not via this structure. -/
@[to_additive]
structure is_normal_subgroup [group G] (s : set G) extends is_subgroup s : Prop :=
(normal : ∀ n ∈ s, ∀ g : G, g * n * g⁻¹ ∈ s)
@[to_additive]
lemma is_normal_subgroup_of_comm_group [comm_group G] {s : set G} (hs : is_subgroup s) :
is_normal_subgroup s :=
{ normal := λ n hn g, by rwa [mul_right_comm, mul_right_inv, one_mul],
..hs }
lemma additive.is_normal_add_subgroup [group G]
{s : set G} (hs : is_normal_subgroup s) : @is_normal_add_subgroup (additive G) _ s :=
@is_normal_add_subgroup.mk (additive G) _ _
(additive.is_add_subgroup hs.to_is_subgroup)
(is_normal_subgroup.normal hs)
theorem additive.is_normal_add_subgroup_iff [group G]
{s : set G} : @is_normal_add_subgroup (additive G) _ s ↔ is_normal_subgroup s :=
⟨by rintro ⟨h₁, h₂⟩; exact
@is_normal_subgroup.mk G _ _ (additive.is_add_subgroup_iff.1 h₁) @h₂,
λ h, by exactI additive.is_normal_add_subgroup h⟩
lemma multiplicative.is_normal_subgroup [add_group A]
{s : set A} (hs : is_normal_add_subgroup s) : @is_normal_subgroup (multiplicative A) _ s :=
@is_normal_subgroup.mk (multiplicative A) _ _
(multiplicative.is_subgroup hs.to_is_add_subgroup)
(is_normal_add_subgroup.normal hs)
theorem multiplicative.is_normal_subgroup_iff [add_group A]
{s : set A} : @is_normal_subgroup (multiplicative A) _ s ↔ is_normal_add_subgroup s :=
⟨by rintro ⟨h₁, h₂⟩; exact
@is_normal_add_subgroup.mk A _ _ (multiplicative.is_subgroup_iff.1 h₁) @h₂,
λ h, by exactI multiplicative.is_normal_subgroup h⟩
namespace is_subgroup
variable [group G]
-- Normal subgroup properties
@[to_additive]
lemma mem_norm_comm {s : set G} (hs : is_normal_subgroup s) {a b : G} (hab : a * b ∈ s) :
b * a ∈ s :=
have h : a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ s, from hs.normal (a * b) hab a⁻¹,
by simp at h; exact h
@[to_additive]
lemma mem_norm_comm_iff {s : set G} (hs : is_normal_subgroup s) {a b : G} : a * b ∈ s ↔ b * a ∈ s :=
⟨mem_norm_comm hs, mem_norm_comm hs⟩
/-- The trivial subgroup -/
@[to_additive "the trivial additive subgroup"]
def trivial (G : Type*) [group G] : set G := {1}
@[simp, to_additive]
lemma mem_trivial {g : G} : g ∈ trivial G ↔ g = 1 :=
mem_singleton_iff
@[to_additive]
lemma trivial_normal : is_normal_subgroup (trivial G) :=
by refine {..}; simp [trivial] {contextual := tt}
@[to_additive]
lemma eq_trivial_iff {s : set G} (hs : is_subgroup s) :
s = trivial G ↔ (∀ x ∈ s, x = (1 : G)) :=
by simp only [set.ext_iff, is_subgroup.mem_trivial];
exact ⟨λ h x, (h x).1, λ h x, ⟨h x, λ hx, hx.symm ▸ hs.to_is_submonoid.one_mem⟩⟩
@[to_additive]
lemma univ_subgroup : is_normal_subgroup (@univ G) :=
by refine {..}; simp
/-- The underlying set of the center of a group. -/
@[to_additive add_center "The underlying set of the center of an additive group."]
def center (G : Type*) [group G] : set G := {z | ∀ g, g * z = z * g}
@[to_additive mem_add_center]
lemma mem_center {a : G} : a ∈ center G ↔ ∀g, g * a = a * g := iff.rfl
@[to_additive add_center_normal]
lemma center_normal : is_normal_subgroup (center G) :=
{ one_mem := by simp [center],
mul_mem := assume a b ha hb g,
by rw [←mul_assoc, mem_center.2 ha g, mul_assoc, mem_center.2 hb g, ←mul_assoc],
inv_mem := assume a ha g,
calc
g * a⁻¹ = a⁻¹ * (g * a) * a⁻¹ : by simp [ha g]
... = a⁻¹ * g : by rw [←mul_assoc, mul_assoc]; simp,
normal := assume n ha g h,
calc
h * (g * n * g⁻¹) = h * n : by simp [ha g, mul_assoc]
... = g * g⁻¹ * n * h : by rw ha h; simp
... = g * n * g⁻¹ * h : by rw [mul_assoc g, ha g⁻¹, ←mul_assoc] }
/-- The underlying set of the normalizer of a subset `S : set G` of a group `G`. That is,
the elements `g : G` such that `g * S * g⁻¹ = S`. -/
@[to_additive add_normalizer "The underlying set of the normalizer of a subset `S : set A` of an
additive group `A`. That is, the elements `a : A` such that `a + S - a = S`."]
def normalizer (s : set G) : set G :=
{g : G | ∀ n, n ∈ s ↔ g * n * g⁻¹ ∈ s}
@[to_additive]
lemma normalizer_is_subgroup (s : set G) : is_subgroup (normalizer s) :=
{ one_mem := by simp [normalizer],
mul_mem := λ a b (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s)
(hb : ∀ n, n ∈ s ↔ b * n * b⁻¹ ∈ s) n,
by rw [mul_inv_rev, ← mul_assoc, mul_assoc a, mul_assoc a, ← ha, ← hb],
inv_mem := λ a (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) n,
by rw [ha (a⁻¹ * n * a⁻¹⁻¹)];
simp [mul_assoc] }
@[to_additive subset_add_normalizer]
lemma subset_normalizer {s : set G} (hs : is_subgroup s) : s ⊆ normalizer s :=
λ g hg n, by rw [is_subgroup.mul_mem_cancel_right hs ((is_subgroup.inv_mem_iff hs).2 hg),
is_subgroup.mul_mem_cancel_left hs hg]
end is_subgroup
-- Homomorphism subgroups
namespace is_group_hom
open is_submonoid is_subgroup
/-- `ker f : set G` is the underlying subset of the kernel of a map `G → H`. -/
@[to_additive "`ker f : set A` is the underlying subset of the kernel of a map `A → B`"]
def ker [group H] (f : G → H) : set G := preimage f (trivial H)
@[to_additive]
lemma mem_ker [group H] (f : G → H) {x : G} : x ∈ ker f ↔ f x = 1 :=
mem_trivial
variables [group G] [group H]
@[to_additive]
lemma one_ker_inv {f : G → H} (hf : is_group_hom f) {a b : G} (h : f (a * b⁻¹) = 1) : f a = f b :=
begin
rw [hf.map_mul, hf.map_inv] at h,
rw [←inv_inv (f b), eq_inv_of_mul_eq_one_left h]
end
@[to_additive]
lemma one_ker_inv' {f : G → H} (hf : is_group_hom f) {a b : G} (h : f (a⁻¹ * b) = 1) : f a = f b :=
begin
rw [hf.map_mul, hf.map_inv] at h,
apply inv_injective,
rw eq_inv_of_mul_eq_one_left h
end
@[to_additive]
lemma inv_ker_one {f : G → H} (hf : is_group_hom f) {a b : G} (h : f a = f b) : f (a * b⁻¹) = 1 :=
have f a * (f b)⁻¹ = 1, by rw [h, mul_right_inv],
by rwa [←hf.map_inv, ←hf.map_mul] at this
@[to_additive]
lemma inv_ker_one' {f : G → H} (hf : is_group_hom f) {a b : G} (h : f a = f b) : f (a⁻¹ * b) = 1 :=
have (f a)⁻¹ * f b = 1, by rw [h, mul_left_inv],
by rwa [←hf.map_inv, ←hf.map_mul] at this
@[to_additive]
lemma one_iff_ker_inv {f : G → H} (hf : is_group_hom f) (a b : G) : f a = f b ↔ f (a * b⁻¹) = 1 :=
⟨hf.inv_ker_one, hf.one_ker_inv⟩
@[to_additive]
lemma one_iff_ker_inv' {f : G → H} (hf : is_group_hom f) (a b : G) : f a = f b ↔ f (a⁻¹ * b) = 1 :=
⟨hf.inv_ker_one', hf.one_ker_inv'⟩
@[to_additive]
lemma inv_iff_ker {f : G → H} (hf : is_group_hom f) (a b : G) : f a = f b ↔ a * b⁻¹ ∈ ker f :=
by rw [mem_ker]; exact one_iff_ker_inv hf _ _
@[to_additive]
lemma inv_iff_ker' {f : G → H} (hf : is_group_hom f) (a b : G) : f a = f b ↔ a⁻¹ * b ∈ ker f :=
by rw [mem_ker]; exact one_iff_ker_inv' hf _ _
@[to_additive]
lemma image_subgroup {f : G → H} (hf : is_group_hom f) {s : set G} (hs : is_subgroup s) :
is_subgroup (f '' s) :=
{ mul_mem := assume a₁ a₂ ⟨b₁, hb₁, eq₁⟩ ⟨b₂, hb₂, eq₂⟩,
⟨b₁ * b₂, hs.mul_mem hb₁ hb₂, by simp [eq₁, eq₂, hf.map_mul]⟩,
one_mem := ⟨1, hs.to_is_submonoid.one_mem, hf.map_one⟩,
inv_mem := assume a ⟨b, hb, eq⟩, ⟨b⁻¹, hs.inv_mem hb, by { rw hf.map_inv, simp * }⟩ }
@[to_additive]
lemma range_subgroup {f : G → H} (hf : is_group_hom f) : is_subgroup (set.range f) :=
@set.image_univ _ _ f ▸ hf.image_subgroup univ_subgroup.to_is_subgroup
local attribute [simp] one_mem inv_mem mul_mem is_normal_subgroup.normal
@[to_additive]
lemma preimage {f : G → H} (hf : is_group_hom f) {s : set H} (hs : is_subgroup s) :
is_subgroup (f ⁻¹' s) :=
by { refine {..};
simp [hs.one_mem, hs.mul_mem, hs.inv_mem, hf.map_mul, hf.map_one, hf.map_inv,
inv_mem_class.inv_mem]
{contextual := tt} }
@[to_additive]
lemma preimage_normal {f : G → H} (hf : is_group_hom f) {s : set H} (hs : is_normal_subgroup s) :
is_normal_subgroup (f ⁻¹' s) :=
{ one_mem := by simp [hf.map_one, hs.to_is_subgroup.one_mem],
mul_mem := by simp [hf.map_mul, hs.to_is_subgroup.mul_mem] {contextual := tt},
inv_mem := by simp [hf.map_inv, hs.to_is_subgroup.inv_mem] {contextual := tt},
normal := by simp [hs.normal, hf.map_mul, hf.map_inv] {contextual := tt}}
@[to_additive]
lemma is_normal_subgroup_ker {f : G → H} (hf : is_group_hom f) : is_normal_subgroup (ker f) :=
hf.preimage_normal (trivial_normal)
@[to_additive]
lemma injective_of_trivial_ker {f : G → H} (hf : is_group_hom f) (h : ker f = trivial G) :
function.injective f :=
begin
intros a₁ a₂ hfa,
simp [ext_iff, ker, is_subgroup.trivial] at h,
have ha : a₁ * a₂⁻¹ = 1, by rw ←h; exact hf.inv_ker_one hfa,
rw [eq_inv_of_mul_eq_one_left ha, inv_inv a₂]
end
@[to_additive]
lemma trivial_ker_of_injective {f : G → H} (hf : is_group_hom f) (h : function.injective f) :
ker f = trivial G :=
set.ext $ assume x, iff.intro
(assume hx,
suffices f x = f 1, by simpa using h this,
by simp [hf.map_one]; rwa [mem_ker] at hx)
(by simp [mem_ker, hf.map_one] {contextual := tt})
@[to_additive]
lemma injective_iff_trivial_ker {f : G → H} (hf : is_group_hom f) :
function.injective f ↔ ker f = trivial G :=
⟨hf.trivial_ker_of_injective, hf.injective_of_trivial_ker⟩
@[to_additive]
lemma trivial_ker_iff_eq_one {f : G → H} (hf : is_group_hom f) :
ker f = trivial G ↔ ∀ x, f x = 1 → x = 1 :=
by rw set.ext_iff; simp [ker]; exact
⟨λ h x hx, (h x).1 hx, λ h x, ⟨h x, λ hx, by rw [hx, hf.map_one]⟩⟩
end is_group_hom
namespace add_group
variables [add_group A]
/-- If `A` is an additive group and `s : set A`, then `in_closure s : set A` is the underlying
subset of the subgroup generated by `s`. -/
inductive in_closure (s : set A) : A → Prop
| basic {a : A} : a ∈ s → in_closure a
| zero : in_closure 0
| neg {a : A} : in_closure a → in_closure (-a)
| add {a b : A} : in_closure a → in_closure b → in_closure (a + b)
end add_group
namespace group
open is_submonoid is_subgroup
variables [group G] {s : set G}
/-- If `G` is a group and `s : set G`, then `in_closure s : set G` is the underlying
subset of the subgroup generated by `s`. -/
@[to_additive]
inductive in_closure (s : set G) : G → Prop
| basic {a : G} : a ∈ s → in_closure a
| one : in_closure 1
| inv {a : G} : in_closure a → in_closure a⁻¹
| mul {a b : G} : in_closure a → in_closure b → in_closure (a * b)
/-- `group.closure s` is the subgroup generated by `s`, i.e. the smallest subgroup containg `s`. -/
@[to_additive "`add_group.closure s` is the additive subgroup generated by `s`, i.e., the
smallest additive subgroup containing `s`."]
def closure (s : set G) : set G := {a | in_closure s a }
@[to_additive]
lemma mem_closure {a : G} : a ∈ s → a ∈ closure s := in_closure.basic
@[to_additive]
lemma closure.is_subgroup (s : set G) : is_subgroup (closure s) :=
{ one_mem := in_closure.one,
mul_mem := assume a b, in_closure.mul,
inv_mem := assume a, in_closure.inv }
@[to_additive]
theorem subset_closure {s : set G} : s ⊆ closure s := λ a, mem_closure
@[to_additive]
theorem closure_subset {s t : set G} (ht : is_subgroup t) (h : s ⊆ t) : closure s ⊆ t :=
assume a ha, by induction ha; simp [h _, *, ht.one_mem, ht.mul_mem, is_subgroup.inv_mem_iff]
@[to_additive]
lemma closure_subset_iff {s t : set G} (ht : is_subgroup t) : closure s ⊆ t ↔ s ⊆ t :=
⟨assume h b ha, h (mem_closure ha), assume h b ha, closure_subset ht h ha⟩
@[to_additive]
theorem closure_mono {s t : set G} (h : s ⊆ t) : closure s ⊆ closure t :=
closure_subset (closure.is_subgroup _) $ set.subset.trans h subset_closure
@[simp, to_additive]
lemma closure_subgroup {s : set G} (hs : is_subgroup s) : closure s = s :=
set.subset.antisymm (closure_subset hs $ set.subset.refl s) subset_closure
@[to_additive]
theorem exists_list_of_mem_closure {s : set G} {a : G} (h : a ∈ closure s) :
(∃l:list G, (∀x∈l, x ∈ s ∨ x⁻¹ ∈ s) ∧ l.prod = a) :=
in_closure.rec_on h
(λ x hxs, ⟨[x], list.forall_mem_singleton.2 $ or.inl hxs, one_mul _⟩)
⟨[], list.forall_mem_nil _, rfl⟩
(λ x _ ⟨L, HL1, HL2⟩, ⟨L.reverse.map has_inv.inv,
λ x hx, let ⟨y, hy1, hy2⟩ := list.exists_of_mem_map hx in
hy2 ▸ or.imp id (by rw [inv_inv]; exact id) (HL1 _ $ list.mem_reverse.1 hy1).symm,
HL2 ▸ list.rec_on L inv_one.symm (λ hd tl ih,
by rw [list.reverse_cons, list.map_append, list.prod_append, ih, list.map_singleton,
list.prod_cons, list.prod_nil, mul_one, list.prod_cons, mul_inv_rev])⟩)
(λ x y hx hy ⟨L1, HL1, HL2⟩ ⟨L2, HL3, HL4⟩, ⟨L1 ++ L2, list.forall_mem_append.2 ⟨HL1, HL3⟩,
by rw [list.prod_append, HL2, HL4]⟩)
@[to_additive]
lemma image_closure [group H] {f : G → H} (hf : is_group_hom f) (s : set G) :
f '' closure s = closure (f '' s) :=
le_antisymm
begin
rintros _ ⟨x, hx, rfl⟩,
apply in_closure.rec_on hx; intros,
{ solve_by_elim [subset_closure, set.mem_image_of_mem] },
{ rw [hf.to_is_monoid_hom.map_one],
apply is_submonoid.one_mem (closure.is_subgroup _).to_is_submonoid, },
{ rw [hf.map_inv],
apply is_subgroup.inv_mem (closure.is_subgroup _), assumption },
{ rw [hf.to_is_monoid_hom.map_mul],
solve_by_elim [is_submonoid.mul_mem (closure.is_subgroup _).to_is_submonoid] }
end
(closure_subset (hf.image_subgroup $ closure.is_subgroup _) $ set.image_subset _ subset_closure)
@[to_additive]
theorem mclosure_subset {s : set G} : monoid.closure s ⊆ closure s :=
monoid.closure_subset (closure.is_subgroup _).to_is_submonoid $ subset_closure
@[to_additive]
theorem mclosure_inv_subset {s : set G} : monoid.closure (has_inv.inv ⁻¹' s) ⊆ closure s :=
monoid.closure_subset (closure.is_subgroup _).to_is_submonoid $ λ x hx,
inv_inv x ▸ ((closure.is_subgroup _).inv_mem $ subset_closure hx)
@[to_additive]
theorem closure_eq_mclosure {s : set G} : closure s = monoid.closure (s ∪ has_inv.inv ⁻¹' s) :=
set.subset.antisymm
(@closure_subset _ _ _ (monoid.closure (s ∪ has_inv.inv ⁻¹' s))
{ one_mem := (monoid.closure.is_submonoid _).one_mem,
mul_mem := λ _ _, (monoid.closure.is_submonoid _).mul_mem,
inv_mem := λ x hx, monoid.in_closure.rec_on hx
(λ x hx, or.cases_on hx (λ hx, monoid.subset_closure $ or.inr $
show x⁻¹⁻¹ ∈ s, from (inv_inv x).symm ▸ hx)
(λ hx, monoid.subset_closure $ or.inl hx))
((@inv_one G _).symm ▸ is_submonoid.one_mem (monoid.closure.is_submonoid _))
(λ x y hx hy ihx ihy,
(mul_inv_rev x y).symm ▸ is_submonoid.mul_mem (monoid.closure.is_submonoid _) ihy ihx) }
(set.subset.trans (set.subset_union_left _ _) monoid.subset_closure))
(monoid.closure_subset (closure.is_subgroup _).to_is_submonoid $ set.union_subset subset_closure $
λ x hx, inv_inv x ▸ (is_subgroup.inv_mem (closure.is_subgroup _) $ subset_closure hx))
@[to_additive]
theorem mem_closure_union_iff {G : Type*} [comm_group G] {s t : set G} {x : G} :
x ∈ closure (s ∪ t) ↔ ∃ y ∈ closure s, ∃ z ∈ closure t, y * z = x :=
begin
simp only [closure_eq_mclosure, monoid.mem_closure_union_iff, exists_prop, preimage_union], split,
{ rintro ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, rfl⟩,
refine ⟨_, ⟨_, hys, _, hzs, rfl⟩, _, ⟨_, hyt, _, hzt, rfl⟩, _⟩,
rw [mul_assoc, mul_assoc, mul_left_comm zs] },
{ rintro ⟨_, ⟨ys, hys, zs, hzs, rfl⟩, _, ⟨yt, hyt, zt, hzt, rfl⟩, rfl⟩,
refine ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, _⟩,
rw [mul_assoc, mul_assoc, mul_left_comm yt] }
end
end group
namespace is_subgroup
variable [group G]
@[to_additive]
lemma trivial_eq_closure : trivial G = group.closure ∅ :=
subset.antisymm
(by simp [set.subset_def, (group.closure.is_subgroup _).one_mem])
(group.closure_subset (trivial_normal).to_is_subgroup $ by simp)
end is_subgroup
/-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. -/
namespace group
variables {s : set G} [group G]
lemma conjugates_of_subset {t : set G} (ht : is_normal_subgroup t) {a : G} (h : a ∈ t) :
conjugates_of a ⊆ t :=
λ x hc,
begin
obtain ⟨c, w⟩ := is_conj_iff.1 hc,
have H := is_normal_subgroup.normal ht a h c,
rwa ←w,
end
theorem conjugates_of_set_subset' {s t : set G} (ht : is_normal_subgroup t) (h : s ⊆ t) :
conjugates_of_set s ⊆ t :=
set.Union₂_subset (λ x H, conjugates_of_subset ht (h H))
/-- 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) : set 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
/-- The normal closure of a set is a subgroup. -/
lemma normal_closure.is_subgroup (s : set G) : is_subgroup (normal_closure s) :=
closure.is_subgroup (conjugates_of_set s)
/-- The normal closure of s is a normal subgroup. -/
lemma normal_closure.is_normal : is_normal_subgroup (normal_closure s) :=
{ normal := λ n h g,
begin
induction h with x hx x hx ihx x y hx hy ihx ihy,
{exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx))},
{simpa using (normal_closure.is_subgroup s).one_mem},
{rw ←conj_inv,
exact ((normal_closure.is_subgroup _).inv_mem ihx)},
{rw ←conj_mul,
exact ((normal_closure.is_subgroup _).to_is_submonoid.mul_mem ihx ihy)},
end,
..normal_closure.is_subgroup _ }
/-- The normal closure of s is the smallest normal subgroup containing s. -/
theorem normal_closure_subset {s t : set G} (ht : is_normal_subgroup t) (h : s ⊆ t) :
normal_closure s ⊆ t :=
λ a w,
begin
induction w with x hx x hx ihx x y hx hy ihx ihy,
{exact (conjugates_of_set_subset' ht h $ hx)},
{exact ht.to_is_subgroup.to_is_submonoid.one_mem},
{exact ht.to_is_subgroup.inv_mem ihx},
{exact ht.to_is_subgroup.to_is_submonoid.mul_mem ihx ihy}
end
lemma normal_closure_subset_iff {s t : set G} (ht : is_normal_subgroup t) :
s ⊆ t ↔ normal_closure s ⊆ t :=
⟨normal_closure_subset ht, set.subset.trans (subset_normal_closure)⟩
theorem normal_closure_mono {s t : set G} : s ⊆ t → normal_closure s ⊆ normal_closure t :=
λ h, normal_closure_subset normal_closure.is_normal (set.subset.trans h (subset_normal_closure))
end group
/-- Create a bundled subgroup from a set `s` and `[is_subgroup s]`. -/
@[to_additive "Create a bundled additive subgroup from a set `s` and `[is_add_subgroup s]`."]
def subgroup.of [group G] {s : set G} (h : is_subgroup s) : subgroup G :=
{ carrier := s,
one_mem' := h.1.1,
mul_mem' := λ _ _, h.1.2,
inv_mem' := λ _, h.2 }
@[to_additive]
lemma subgroup.is_subgroup [group G] (K : subgroup G) : is_subgroup (K : set G) :=
{ one_mem := K.one_mem',
mul_mem := λ _ _, K.mul_mem',
inv_mem := λ _, K.inv_mem' }
-- this will never fire if it's an instance
@[to_additive]
lemma subgroup.of_normal [group G] (s : set G) (h : is_subgroup s) (n : is_normal_subgroup s) :
subgroup.normal (subgroup.of h) :=
{ conj_mem := n.normal, }
|
3617881ba7cca0a0ccd3933647527bafcca3fbaa | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/data/real/irrational.lean | 17cdfc7d2b96f7bda37e2e1328fb3749b5acb080 | [
"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 | 4,747 | 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.
Irrationality of real numbers.
-/
import data.real.basic data.padics.padic_norm
open rat real multiplicity
def irrational (x : ℝ) := ¬ ∃ q : ℚ, x = q
theorem irr_nrt_of_notint_nrt {x : ℝ} (n : ℕ) (m : ℤ)
(hxr : x ^ n = m) (hv : ¬ ∃ y : ℤ, x = y) (hnpos : 0 < n) :
irrational x
| ⟨q, e⟩ := begin
rw [e, ← cast_pow] at hxr, cases q with N D P C,
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 _ c1, 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 hdivn' : nat.gcd N.nat_abs D = D := nat.gcd_eq_right hdivn,
refine hv ⟨N, _⟩,
rwa [num_denom', ← hdivn', C.gcd_eq_one, int.coe_nat_one, mk_eq_div,
int.cast_one, div_one, cast_coe_int] at e
end
theorem irr_nrt_of_n_not_dvd_multiplicity {x : ℝ} (n : ℕ) {m : ℤ} (hm : m ≠ 0) (p : ℕ)
[hp : nat.prime p] (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 irr_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 irr_sqrt_of_multiplicity_odd (m : ℤ) (hm : 0 < m)
(p : ℕ) [hp : nat.prime p]
(Hpv : (multiplicity (p : ℤ) m).get (finite_int_iff.2 ⟨hp.ne_one, ne.symm (ne_of_lt hm)⟩) % 2 = 1) :
irrational (sqrt m) :=
irr_nrt_of_n_not_dvd_multiplicity 2 (ne.symm (ne_of_lt hm)) p
(sqr_sqrt (int.cast_nonneg.2 $ le_of_lt hm))
(by rw Hpv; exact one_ne_zero)
theorem irr_sqrt_of_prime (p : ℕ) (hp : nat.prime p) : irrational (sqrt p) :=
irr_sqrt_of_multiplicity_odd p (int.coe_nat_pos.2 hp.pos) p $
by simp [multiplicity_self (mt is_unit_iff_dvd_one.1 (mt int.coe_nat_dvd.1 hp.not_dvd_one) : _)];
refl
theorem irr_sqrt_two : irrational (sqrt 2) :=
by simpa using irr_sqrt_of_prime 2 nat.prime_two
theorem irr_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 [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)⟩)
(λ h, H2 h.2)
instance (q : ℚ) : decidable (irrational (sqrt q)) :=
decidable_of_iff' _ (irr_sqrt_rat_iff q)
variables {q : ℚ} {x : ℝ}
theorem irr_rat_add_of_irr : irrational x → irrational (q + x) :=
mt $ λ ⟨a, h⟩, ⟨-q + a, by rw [rat.cast_add, ← h, rat.cast_neg, neg_add_cancel_left]⟩
@[simp] theorem irr_rat_add_iff_irr : irrational (q + x) ↔ irrational x :=
⟨by simpa only [cast_neg, neg_add_cancel_left] using @irr_rat_add_of_irr (-q) (q+x),
irr_rat_add_of_irr⟩
@[simp] theorem irr_add_rat_iff_irr : irrational (x + q) ↔ irrational x :=
by rw [add_comm, irr_rat_add_iff_irr]
theorem irr_mul_rat_iff_irr (Hqn0 : q ≠ 0) : irrational (x * ↑q) ↔ irrational x :=
⟨mt $ λ ⟨r, hr⟩, ⟨r * q, hr.symm ▸ (rat.cast_mul _ _).symm⟩,
mt $ λ ⟨r, hr⟩, ⟨r / q, by rw [cast_div, ← hr, mul_div_cancel]; rwa cast_ne_zero⟩⟩
theorem irr_of_irr_mul_self : irrational (x * x) → irrational x :=
mt $ λ ⟨p, e⟩, ⟨p * p, by rw [e, cast_mul]⟩
@[simp] theorem irr_neg : irrational (-x) ↔ irrational x :=
⟨λ hn ⟨q, hx⟩, hn ⟨-q, by rw [hx, cast_neg]⟩,
λ hx ⟨q, hn⟩, hx ⟨-q, by rw [←neg_neg x, hn, cast_neg]⟩⟩
|
4378c5ae283b508f818f1fd0758a93697929b614 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/analysis/inner_product_space/spectrum.lean | 525c6e21e8b0f68a7ab3a2a4cb6c5a9126bddde5 | [
"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 | 12,367 | lean | /-
Copyright (c) 2021 Heather Macbeth. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Heather Macbeth
-/
import analysis.inner_product_space.rayleigh
import analysis.inner_product_space.pi_L2
/-! # Spectral theory of self-adjoint operators
This file covers the spectral theory of self-adjoint operators on an inner product space.
The first part of the file covers general properties, true without any condition on boundedness or
compactness of the operator or finite-dimensionality of the underlying space, notably:
* `is_self_adjoint.conj_eigenvalue_eq_self`: the eigenvalues are real
* `is_self_adjoint.orthogonal_family_eigenspaces`: the eigenspaces are orthogonal
* `is_self_adjoint.orthogonal_supr_eigenspaces`: the restriction of the operator to the mutual
orthogonal complement of the eigenspaces has, itself, no eigenvectors
The second part of the file covers properties of self-adjoint operators in finite dimension.
Letting `T` be a self-adjoint operator on a finite-dimensional inner product space `T`,
* The definition `is_self_adjoint.diagonalization` provides a linear isometry equivalence `E` to
the direct sum of the eigenspaces of `T`. The theorem
`is_self_adjoint.diagonalization_apply_self_apply` states that, when `T` is transferred via this
equivalence to an operator on the direct sum, it acts diagonally.
* The definition `is_self_adjoint.eigenvector_basis` provides an orthonormal basis for `E`
consisting of eigenvectors of `T`, with `is_self_adjoint.eigenvalues` giving the corresponding
list of eigenvalues, as real numbers. The definition `is_self_adjoint.diagonalization_basis`
gives the associated linear isometry equivalence from `E` to Euclidean space, and the theorem
`is_self_adjoint.diagonalization_basis_apply_self_apply` states that, when `T` is transferred via
this equivalence to an operator on Euclidean space, it acts diagonally.
These are forms of the *diagonalization theorem* for self-adjoint operators on finite-dimensional
inner product spaces.
## TODO
Spectral theory for compact self-adjoint operators, bounded self-adjoint operators.
## Tags
self-adjoint operator, spectral theorem, diagonalization theorem
-/
variables {𝕜 : Type*} [is_R_or_C 𝕜] [dec_𝕜 : decidable_eq 𝕜]
variables {E : Type*} [normed_add_comm_group E] [inner_product_space 𝕜 E]
local notation `⟪`x`, `y`⟫` := @inner 𝕜 E _ x y
open_locale big_operators complex_conjugate
open module.End
namespace linear_map
namespace is_symmetric
variables {T : E →ₗ[𝕜] E} (hT : T.is_symmetric)
include hT
/-- A self-adjoint operator preserves orthogonal complements of its eigenspaces. -/
lemma invariant_orthogonal_eigenspace (μ : 𝕜) (v : E) (hv : v ∈ (eigenspace T μ)ᗮ) :
T v ∈ (eigenspace T μ)ᗮ :=
begin
intros w hw,
have : T w = (μ:𝕜) • w := by rwa mem_eigenspace_iff at hw,
simp [← hT w, this, inner_smul_left, hv w hw]
end
/-- The eigenvalues of a self-adjoint operator are real. -/
lemma conj_eigenvalue_eq_self {μ : 𝕜} (hμ : has_eigenvalue T μ) : conj μ = μ :=
begin
obtain ⟨v, hv₁, hv₂⟩ := hμ.exists_has_eigenvector,
rw mem_eigenspace_iff at hv₁,
simpa [hv₂, inner_smul_left, inner_smul_right, hv₁] using hT v v
end
/-- The eigenspaces of a self-adjoint operator are mutually orthogonal. -/
lemma orthogonal_family_eigenspaces :
orthogonal_family 𝕜 (λ μ, eigenspace T μ) (λ μ, (eigenspace T μ).subtypeₗᵢ) :=
begin
rintros μ ν hμν ⟨v, hv⟩ ⟨w, hw⟩,
by_cases hv' : v = 0,
{ simp [hv'] },
have H := hT.conj_eigenvalue_eq_self (has_eigenvalue_of_has_eigenvector ⟨hv, hv'⟩),
rw mem_eigenspace_iff at hv hw,
refine or.resolve_left _ hμν.symm,
simpa [inner_smul_left, inner_smul_right, hv, hw, H] using (hT v w).symm
end
lemma orthogonal_family_eigenspaces' :
orthogonal_family 𝕜 (λ μ : eigenvalues T, eigenspace T μ) (λ μ, (eigenspace T μ).subtypeₗᵢ) :=
hT.orthogonal_family_eigenspaces.comp subtype.coe_injective
/-- The mutual orthogonal complement of the eigenspaces of a self-adjoint operator on an inner
product space is an invariant subspace of the operator. -/
lemma orthogonal_supr_eigenspaces_invariant ⦃v : E⦄ (hv : v ∈ (⨆ μ, eigenspace T μ)ᗮ) :
T v ∈ (⨆ μ, eigenspace T μ)ᗮ :=
begin
rw ← submodule.infi_orthogonal at ⊢ hv,
exact T.infi_invariant hT.invariant_orthogonal_eigenspace v hv
end
/-- The mutual orthogonal complement of the eigenspaces of a self-adjoint operator on an inner
product space has no eigenvalues. -/
lemma orthogonal_supr_eigenspaces (μ : 𝕜) :
eigenspace (T.restrict hT.orthogonal_supr_eigenspaces_invariant) μ = ⊥ :=
begin
set p : submodule 𝕜 E := (⨆ μ, eigenspace T μ)ᗮ,
refine eigenspace_restrict_eq_bot hT.orthogonal_supr_eigenspaces_invariant _,
have H₂ : eigenspace T μ ⟂ p := (submodule.is_ortho_orthogonal_right _).mono_left (le_supr _ _),
exact H₂.disjoint
end
/-! ### Finite-dimensional theory -/
variables [finite_dimensional 𝕜 E]
/-- The mutual orthogonal complement of the eigenspaces of a self-adjoint operator on a
finite-dimensional inner product space is trivial. -/
lemma orthogonal_supr_eigenspaces_eq_bot : (⨆ μ, eigenspace T μ)ᗮ = ⊥ :=
begin
have hT' : is_symmetric _ := hT.restrict_invariant hT.orthogonal_supr_eigenspaces_invariant,
-- a self-adjoint operator on a nontrivial inner product space has an eigenvalue
haveI := hT'.subsingleton_of_no_eigenvalue_finite_dimensional hT.orthogonal_supr_eigenspaces,
exact submodule.eq_bot_of_subsingleton _,
end
lemma orthogonal_supr_eigenspaces_eq_bot' : (⨆ μ : eigenvalues T, eigenspace T μ)ᗮ = ⊥ :=
show (⨆ μ : {μ // (eigenspace T μ) ≠ ⊥}, eigenspace T μ)ᗮ = ⊥,
by rw [supr_ne_bot_subtype, hT.orthogonal_supr_eigenspaces_eq_bot]
include dec_𝕜
/-- The eigenspaces of a self-adjoint operator on a finite-dimensional inner product space `E` give
an internal direct sum decomposition of `E`. -/
lemma direct_sum_is_internal :
direct_sum.is_internal (λ μ : eigenvalues T, eigenspace T μ) :=
hT.orthogonal_family_eigenspaces'.is_internal_iff.mpr
hT.orthogonal_supr_eigenspaces_eq_bot'
section version1
/-- Isometry from an inner product space `E` to the direct sum of the eigenspaces of some
self-adjoint operator `T` on `E`. -/
noncomputable def diagonalization : E ≃ₗᵢ[𝕜] pi_Lp 2 (λ μ : eigenvalues T, eigenspace T μ) :=
hT.direct_sum_is_internal.isometry_L2_of_orthogonal_family
hT.orthogonal_family_eigenspaces'
@[simp] lemma diagonalization_symm_apply (w : pi_Lp 2 (λ μ : eigenvalues T, eigenspace T μ)) :
hT.diagonalization.symm w = ∑ μ, w μ :=
hT.direct_sum_is_internal.isometry_L2_of_orthogonal_family_symm_apply
hT.orthogonal_family_eigenspaces' w
/-- *Diagonalization theorem*, *spectral theorem*; version 1: A self-adjoint operator `T` on a
finite-dimensional inner product space `E` acts diagonally on the decomposition of `E` into the
direct sum of the eigenspaces of `T`. -/
lemma diagonalization_apply_self_apply (v : E) (μ : eigenvalues T) :
hT.diagonalization (T v) μ = (μ : 𝕜) • hT.diagonalization v μ :=
begin
suffices : ∀ w : pi_Lp 2 (λ μ : eigenvalues T, eigenspace T μ),
(T (hT.diagonalization.symm w)) = hT.diagonalization.symm (λ μ, (μ : 𝕜) • w μ),
{ simpa only [linear_isometry_equiv.symm_apply_apply, linear_isometry_equiv.apply_symm_apply]
using congr_arg (λ w, hT.diagonalization w μ) (this (hT.diagonalization v)) },
intros w,
have hwT : ∀ μ, T (w μ) = (μ : 𝕜) • w μ := λ μ, mem_eigenspace_iff.1 (w μ).2,
simp only [hwT, diagonalization_symm_apply, map_sum, submodule.coe_smul_of_tower],
end
end version1
section version2
variables {n : ℕ} (hn : finite_dimensional.finrank 𝕜 E = n)
/-- A choice of orthonormal basis of eigenvectors for self-adjoint operator `T` on a
finite-dimensional inner product space `E`.
TODO Postcompose with a permutation so that these eigenvectors are listed in increasing order of
eigenvalue. -/
@[irreducible] noncomputable def eigenvector_basis : orthonormal_basis (fin n) 𝕜 E :=
hT.direct_sum_is_internal.subordinate_orthonormal_basis hn
hT.orthogonal_family_eigenspaces'
/-- The sequence of real eigenvalues associated to the standard orthonormal basis of eigenvectors
for a self-adjoint operator `T` on `E`.
TODO Postcompose with a permutation so that these eigenvalues are listed in increasing order. -/
@[irreducible] noncomputable def eigenvalues (i : fin n) : ℝ :=
@is_R_or_C.re 𝕜 _ $
hT.direct_sum_is_internal.subordinate_orthonormal_basis_index hn i
hT.orthogonal_family_eigenspaces'
lemma has_eigenvector_eigenvector_basis (i : fin n) :
has_eigenvector T (hT.eigenvalues hn i) (hT.eigenvector_basis hn i) :=
begin
let v : E := hT.eigenvector_basis hn i,
let μ : 𝕜 := hT.direct_sum_is_internal.subordinate_orthonormal_basis_index
hn i hT.orthogonal_family_eigenspaces',
simp_rw [eigenvalues],
change has_eigenvector T (is_R_or_C.re μ) v,
have key : has_eigenvector T μ v,
{ have H₁ : v ∈ eigenspace T μ,
{ simp_rw [v, eigenvector_basis],
exact hT.direct_sum_is_internal.subordinate_orthonormal_basis_subordinate
hn i hT.orthogonal_family_eigenspaces' },
have H₂ : v ≠ 0 := by simpa using (hT.eigenvector_basis hn).to_basis.ne_zero i,
exact ⟨H₁, H₂⟩ },
have re_μ : ↑(is_R_or_C.re μ) = μ,
{ rw ← is_R_or_C.conj_eq_iff_re,
exact hT.conj_eigenvalue_eq_self (has_eigenvalue_of_has_eigenvector key) },
simpa [re_μ] using key,
end
lemma has_eigenvalue_eigenvalues (i : fin n) : has_eigenvalue T (hT.eigenvalues hn i) :=
module.End.has_eigenvalue_of_has_eigenvector (hT.has_eigenvector_eigenvector_basis hn i)
@[simp] lemma apply_eigenvector_basis (i : fin n) :
T (hT.eigenvector_basis hn i) = (hT.eigenvalues hn i : 𝕜) • hT.eigenvector_basis hn i :=
mem_eigenspace_iff.mp (hT.has_eigenvector_eigenvector_basis hn i).1
/-- *Diagonalization theorem*, *spectral theorem*; version 2: A self-adjoint operator `T` on a
finite-dimensional inner product space `E` acts diagonally on the identification of `E` with
Euclidean space induced by an orthonormal basis of eigenvectors of `T`. -/
lemma diagonalization_basis_apply_self_apply (v : E) (i : fin n) :
(hT.eigenvector_basis hn).repr (T v) i =
hT.eigenvalues hn i * (hT.eigenvector_basis hn).repr v i :=
begin
suffices : ∀ w : euclidean_space 𝕜 (fin n),
T ((hT.eigenvector_basis hn).repr.symm w)
= (hT.eigenvector_basis hn).repr.symm (λ i, hT.eigenvalues hn i * w i),
{ simpa [orthonormal_basis.sum_repr_symm] using
congr_arg (λ v, (hT.eigenvector_basis hn).repr v i)
(this ((hT.eigenvector_basis hn).repr v)) },
intros w,
simp_rw [← orthonormal_basis.sum_repr_symm, linear_map.map_sum,
linear_map.map_smul, apply_eigenvector_basis],
apply fintype.sum_congr,
intros a,
rw [smul_smul, mul_comm],
end
end version2
end is_symmetric
end linear_map
section nonneg
@[simp]
lemma inner_product_apply_eigenvector {μ : 𝕜} {v : E} {T : E →ₗ[𝕜] E}
(h : v ∈ module.End.eigenspace T μ) : ⟪v, T v⟫ = μ * ‖v‖ ^ 2 :=
by simp only [mem_eigenspace_iff.mp h, inner_smul_right, inner_self_eq_norm_sq_to_K]
lemma eigenvalue_nonneg_of_nonneg {μ : ℝ} {T : E →ₗ[𝕜] E} (hμ : has_eigenvalue T μ)
(hnn : ∀ (x : E), 0 ≤ is_R_or_C.re ⟪x, T x⟫) : 0 ≤ μ :=
begin
obtain ⟨v, hv⟩ := hμ.exists_has_eigenvector,
have hpos : 0 < ‖v‖ ^ 2, by simpa only [sq_pos_iff, norm_ne_zero_iff] using hv.2,
have : is_R_or_C.re ⟪v, T v⟫ = μ * ‖v‖ ^ 2,
{ exact_mod_cast congr_arg is_R_or_C.re (inner_product_apply_eigenvector hv.1) },
exact (zero_le_mul_right hpos).mp (this ▸ hnn v),
end
lemma eigenvalue_pos_of_pos {μ : ℝ} {T : E →ₗ[𝕜] E} (hμ : has_eigenvalue T μ)
(hnn : ∀ (x : E), 0 < is_R_or_C.re ⟪x, T x⟫) : 0 < μ :=
begin
obtain ⟨v, hv⟩ := hμ.exists_has_eigenvector,
have hpos : 0 < ‖v‖ ^ 2, by simpa only [sq_pos_iff, norm_ne_zero_iff] using hv.2,
have : is_R_or_C.re ⟪v, T v⟫ = μ * ‖v‖ ^ 2,
{ exact_mod_cast congr_arg is_R_or_C.re (inner_product_apply_eigenvector hv.1) },
exact (zero_lt_mul_right hpos).mp (this ▸ hnn v),
end
end nonneg
|
cd34871acec2301b69848fc921458faf5dc67c8c | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/linear_algebra/dual.lean | cd44bcc83191c14edbe354f63553f28d37b89cba | [
"Apache-2.0"
] | permissive | ChrisHughes24/mathlib | 98322577c460bc6b1fe5c21f42ce33ad1c3e5558 | a2a867e827c2a6702beb9efc2b9282bd801d5f9a | refs/heads/master | 1,583,848,251,477 | 1,565,164,247,000 | 1,565,164,247,000 | 129,409,993 | 0 | 1 | Apache-2.0 | 1,565,164,817,000 | 1,523,628,059,000 | Lean | UTF-8 | Lean | false | false | 6,599 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Fabian Glöckle
Dual vector spaces, the spaces of linear functionals to the base field, including the dual basis
isomorphism and evaluation isomorphism in the finite-dimensional case.
-/
import linear_algebra.dimension
import linear_algebra.tensor_product
noncomputable theory
namespace module
variables (R : Type*) (M : Type*)
variables [comm_ring R] [add_comm_group M] [module R M]
def dual := M →ₗ[R] R
namespace dual
instance : has_coe_to_fun (dual R M) := ⟨_, linear_map.to_fun⟩
instance : add_comm_group (dual R M) :=
by delta dual; apply_instance
instance : module R (dual R M) :=
by delta dual; apply_instance
def eval : M →ₗ[R] (dual R (dual R M)) := linear_map.id.flip
lemma eval_apply (v : M) (a : dual R M) : (eval R M v) a = a v :=
begin
dunfold eval,
rw [linear_map.flip_apply, linear_map.id_apply]
end
end dual
end module
namespace is_basis
universes u v w
variables {K : Type u} {V : Type v} {ι : Type w}
variables [discrete_field K] [add_comm_group V] [vector_space K V]
open vector_space module module.dual submodule linear_map cardinal function
instance dual.vector_space : vector_space K (dual K V) := {..dual.module K V}
variables [decidable_eq V] [decidable_eq (module.dual K V)] [decidable_eq ι]
variables {B : ι → V} (h : is_basis K B)
include h
def to_dual : V →ₗ[K] module.dual K V :=
h.constr $ λ v, h.constr $ λ w, if w = v then 1 else 0
lemma to_dual_apply (i j : ι) :
(h.to_dual (B i)) (B j) = if i = j then 1 else 0 :=
by erw [constr_basis h, constr_basis h]; ac_refl
def to_dual_flip (v : V) : (V →ₗ[K] K) := (linear_map.flip h.to_dual).to_fun v
omit h
def eval_finsupp_at (i : ι) : (ι →₀ K) →ₗ[K] K :=
{ to_fun := λ f, f i,
add := by intros; rw finsupp.add_apply,
smul := by intros; rw finsupp.smul_apply }
include h
set_option class.instance_max_depth 50
def coord_fun (i : ι) : (V →ₗ[K] K) := (eval_finsupp_at i).comp h.repr
lemma coord_fun_eq_repr (v : V) (i : ι) : h.coord_fun i v = h.repr v i := rfl
lemma to_dual_swap_eq_to_dual (v w : V) : h.to_dual_flip v w = h.to_dual w v := rfl
lemma to_dual_eq_repr (v : V) (i : ι) : (h.to_dual v) (B i) = h.repr v i :=
begin
rw [←coord_fun_eq_repr, ←to_dual_swap_eq_to_dual],
apply congr_fun,
dsimp,
congr',
apply h.ext,
{ intros,
rw [to_dual_swap_eq_to_dual, to_dual_apply],
{ split_ifs with hx,
{ rwa [hx, coord_fun_eq_repr, repr_eq_single, finsupp.single_apply, if_pos rfl] },
{ rwa [coord_fun_eq_repr, repr_eq_single, finsupp.single_apply, if_neg hx] } } },
{ exact classical.dec_eq K }
end
lemma to_dual_inj (v : V) (a : h.to_dual v = 0) : v = 0 :=
begin
rw [← mem_bot K, ← h.repr_ker, mem_ker],
apply finsupp.ext,
intro b,
rw [←to_dual_eq_repr _ _ _, a],
refl
end
theorem to_dual_ker : h.to_dual.ker = ⊥ :=
ker_eq_bot'.mpr h.to_dual_inj
theorem to_dual_range [fin : fintype ι] : h.to_dual.range = ⊤ :=
begin
rw eq_top_iff',
intro f,
rw linear_map.mem_range,
let lin_comb : ι →₀ K := finsupp.on_finset fin.elems (λ i, f.to_fun (B i)) _,
--let emb := embedding.subtype B,
{ use finsupp.total ι V K B lin_comb,
apply h.ext,
{ intros i,
rw [h.to_dual_eq_repr _ i, repr_total h],
{ simpa },
{ rw [finsupp.mem_supported],
exact λ _ _, set.mem_univ _ } },
{ exact classical.dec_eq K } },
{ intros a _,
apply fin.complete }
end
def dual_basis : ι → dual K V := λ i, h.to_dual (B i)
theorem dual_lin_independent : linear_independent K h.dual_basis :=
begin
apply linear_independent.image h.1,
rw to_dual_ker,
exact disjoint_bot_right
end
def to_dual_equiv [fintype ι] : V ≃ₗ[K] (dual K V) :=
linear_equiv.of_bijective h.to_dual h.to_dual_ker h.to_dual_range
theorem dual_basis_is_basis [fintype ι] : is_basis K h.dual_basis :=
h.to_dual_equiv.is_basis h
@[simp] lemma to_dual_to_dual [decidable_eq (dual K (dual K V))] [fintype ι] :
(h.dual_basis_is_basis.to_dual).comp h.to_dual = eval K V :=
begin
apply @is_basis.ext _ _ _ _ _ _ _ _ (classical.dec_eq (dual K (dual K V))) _ _ _ _ _ _ _ h,
intros i,
apply @is_basis.ext _ _ _ _ _ _ _ _ (classical.dec_eq _) _ _ _ _ _ _ _ h.dual_basis_is_basis,
intros j,
dunfold eval,
rw [linear_map.flip_apply, linear_map.id_apply, linear_map.comp_apply],
apply eq.trans (to_dual_apply h.dual_basis_is_basis i j),
{ dunfold dual_basis,
rw to_dual_apply,
split_ifs with h₁ h₂; try {refl},
{ exfalso, apply h₂ h₁.symm },
{ exfalso, apply ne.symm h₁ (by assumption) } }
end
theorem dual_dim_eq [fintype ι] :
cardinal.lift.{v u} (dim K V) = dim K (dual K V) :=
begin
have := linear_equiv.dim_eq_lift h.to_dual_equiv,
simp only [cardinal.lift_umax] at this,
rw [this, ← cardinal.lift_umax],
apply cardinal.lift_id,
end
end is_basis
namespace vector_space
universes u v
variables {K : Type u} {V : Type v}
variables [discrete_field K] [add_comm_group V] [vector_space K V]
open module module.dual submodule linear_map cardinal is_basis
theorem eval_ker : (eval K V).ker = ⊥ :=
begin
haveI := classical.dec_eq K,
haveI := classical.dec_eq V,
haveI := classical.dec_eq (dual K V),
rw ker_eq_bot',
intros v h,
rw linear_map.ext_iff at h,
by_contradiction H,
rcases exists_subset_is_basis (linear_independent_singleton H) with ⟨b, hv, hb⟩,
swap 4, assumption,
have hv' : v = (λ (i : b), i.val) ⟨v, hv (set.mem_singleton v)⟩ := rfl,
let hx := h (hb.to_dual v),
erw [eval_apply, hv', to_dual_apply, if_pos rfl, zero_apply _] at hx,
exact one_ne_zero hx
end
theorem dual_dim_eq (h : dim K V < omega) :
cardinal.lift.{v u} (dim K V) = dim K (dual K V) :=
begin
letI := classical.dec_eq (dual K V),
letI := classical.dec_eq V,
rcases exists_is_basis_fintype h with ⟨b, hb, ⟨hf⟩⟩,
resetI,
exact hb.dual_dim_eq
end
set_option class.instance_max_depth 70
lemma eval_range (h : dim K V < omega) : (eval K V).range = ⊤ :=
begin
haveI := classical.dec_eq (dual K V),
haveI := classical.dec_eq (dual K (dual K V)),
letI := classical.dec_eq V,
rcases exists_is_basis_fintype h with ⟨b, hb, ⟨hf⟩⟩,
resetI,
rw [← hb.to_dual_to_dual, range_comp, hb.to_dual_range, map_top, to_dual_range _],
delta dual_basis,
apply_instance
end
def eval_equiv (h : dim K V < omega) : V ≃ₗ[K] dual K (dual K V) :=
linear_equiv.of_bijective (eval K V) eval_ker (eval_range h)
end vector_space
|
c6c986bf56d07e378ba016bba6c1199ec8c5c993 | 618003631150032a5676f229d13a079ac875ff77 | /src/field_theory/finite.lean | fc4ededfe19b8199c93b8ee14c4d2032dd4d0850 | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 10,176 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Joey van Langen, Casper Putz
-/
import data.equiv.ring
import data.zmod.basic
import linear_algebra.basis
import ring_theory.integral_domain
/-!
# Finite fields
This file contains basic results about finite fields.
Throughout most of this file, `K` denotes a finite field
and `q` is notation for the cardinality of `K`.
## Main results
1. Every finite integral domain is a field (`field_of_integral_domain`).
2. The unit group of a finite field is a cyclic group of order `q - 1`.
(`finite_field.is_cyclic` and `card_units`)
3. `sum_pow_units`: The sum of `x^i`, where `x` ranges over the units of `K`, is
- `q-1` if `q-1 ∣ i`
- `0` otherwise
4. `finite_field.card`: The cardinality `q` is a power of the characteristic of `K`.
See `card'` for a variant.
## Notation
Throughout most of this file, `K` denotes a finite field
and `q` is notation for the cardinality of `K`.
-/
variables {K : Type*} [field K] [fintype K]
variables {R : Type*} [integral_domain R]
local notation `q` := fintype.card K
open_locale big_operators
namespace finite_field
open finset function
section polynomial
open polynomial
/-- The cardinality of a field is at most `n` times the cardinality of the image of a degree `n`
polynomial -/
lemma card_image_polynomial_eval [fintype R] [decidable_eq R] {p : polynomial R} (hp : 0 < p.degree) :
fintype.card R ≤ nat_degree p * (univ.image (λ x, eval x p)).card :=
finset.card_le_mul_card_image _ _
(λ a _, calc _ = (p - C a).roots.card : congr_arg card
(by simp [finset.ext, mem_roots_sub_C hp, -sub_eq_add_neg])
... ≤ _ : card_roots_sub_C' hp)
/-- If `f` and `g` are quadratic polynomials, then the `f.eval a + g.eval b = 0` has a solution. -/
lemma exists_root_sum_quadratic [fintype R] {f g : polynomial R} (hf2 : degree f = 2)
(hg2 : degree g = 2) (hR : fintype.card R % 2 = 1) : ∃ a b, f.eval a + g.eval b = 0 :=
by letI := classical.dec_eq R; exact
suffices ¬ disjoint (univ.image (λ x : R, eval x f)) (univ.image (λ x : R, eval x (-g))),
begin
simp only [disjoint_left, mem_image] at this,
push_neg at this,
rcases this with ⟨x, ⟨a, _, ha⟩, ⟨b, _, hb⟩⟩,
exact ⟨a, b, by rw [ha, ← hb, eval_neg, neg_add_self]⟩
end,
assume hd : disjoint _ _,
lt_irrefl (2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card) $
calc 2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card
≤ 2 * fintype.card R : nat.mul_le_mul_left _ (finset.card_le_of_subset (subset_univ _))
... = fintype.card R + fintype.card R : two_mul _
... < nat_degree f * (univ.image (λ x : R, eval x f)).card +
nat_degree (-g) * (univ.image (λ x : R, eval x (-g))).card :
add_lt_add_of_lt_of_le
(lt_of_le_of_ne
(card_image_polynomial_eval (by rw hf2; exact dec_trivial))
(mt (congr_arg (%2)) (by simp [nat_degree_eq_of_degree_eq_some hf2, hR])))
(card_image_polynomial_eval (by rw [degree_neg, hg2]; exact dec_trivial))
... = 2 * (univ.image (λ x : R, eval x f) ∪ univ.image (λ x : R, eval x (-g))).card :
by rw [card_disjoint_union hd]; simp [nat_degree_eq_of_degree_eq_some hf2,
nat_degree_eq_of_degree_eq_some hg2, bit0, mul_add]
end polynomial
lemma card_units : fintype.card (units K) = fintype.card K - 1 :=
begin
classical,
rw [eq_comm, nat.sub_eq_iff_eq_add (fintype.card_pos_iff.2 ⟨(0 : K)⟩)],
haveI := set_fintype {a : K | a ≠ 0},
haveI := set_fintype (@set.univ K),
rw [fintype.card_congr (equiv.units_equiv_ne_zero _),
← @set.card_insert _ _ {a : K | a ≠ 0} _ (not_not.2 (eq.refl (0 : K)))
(set.fintype_insert _ _), fintype.card_congr (equiv.set.univ K).symm],
congr; simp [set.ext_iff, classical.em]
end
lemma prod_univ_units_id_eq_neg_one :
univ.prod (λ x, x) = (-1 : units K) :=
begin
classical,
have : ((@univ (units K) _).erase (-1)).prod (λ x, x) = 1,
from prod_involution (λ x _, x⁻¹) (by simp)
(λ a, by simp [units.inv_eq_self_iff] {contextual := tt})
(λ a, by simp [@inv_eq_iff_inv_eq _ _ a, eq_comm] {contextual := tt})
(by simp),
rw [← insert_erase (mem_univ (-1 : units K)), prod_insert (not_mem_erase _ _),
this, mul_one]
end
lemma pow_card_sub_one_eq_one (a : K) (ha : a ≠ 0) :
a ^ (fintype.card K - 1) = 1 :=
calc a ^ (fintype.card K - 1) = (units.mk0 a ha ^ (fintype.card K - 1) : units K) :
by rw [units.coe_pow, units.coe_mk0]
... = 1 : by { classical, rw [← card_units, pow_card_eq_one], refl }
variable (K)
theorem card (p : ℕ) [char_p K p] : ∃ (n : ℕ+), nat.prime p ∧ q = p^(n : ℕ) :=
begin
haveI hp : fact p.prime := char_p.char_is_prime K p,
have V : vector_space (zmod p) K, from { .. (zmod.cast_hom p K).to_semimodule },
obtain ⟨n, h⟩ := @vector_space.card_fintype _ _ _ _ V _ _,
rw zmod.card at h,
refine ⟨⟨n, _⟩, hp, h⟩,
apply or.resolve_left (nat.eq_zero_or_pos n),
rintro rfl,
rw nat.pow_zero at h,
have : (0 : K) = 1, { apply fintype.card_le_one_iff.mp (le_of_eq h) },
exact absurd this zero_ne_one,
end
theorem card' : ∃ (p : ℕ) (n : ℕ+), nat.prime p ∧ q = p^(n : ℕ) :=
let ⟨p, hc⟩ := char_p.exists K in ⟨p, @finite_field.card K _ _ p hc⟩
@[simp] lemma cast_card_eq_zero : (q : K) = 0 :=
begin
rcases char_p.exists K with ⟨p, _char_p⟩, resetI,
rcases card K p with ⟨n, hp, hn⟩,
simp only [char_p.cast_eq_zero_iff K p, hn],
conv { congr, rw [← nat.pow_one p] },
exact nat.pow_dvd_pow _ n.2,
end
lemma forall_pow_eq_one_iff (i : ℕ) :
(∀ x : units K, x ^ i = 1) ↔ q - 1 ∣ i :=
begin
obtain ⟨x, hx⟩ := is_cyclic.exists_generator (units K),
classical,
rw [← card_units, ← order_of_eq_card_of_forall_mem_gpowers hx, order_of_dvd_iff_pow_eq_one],
split,
{ intro h, apply h },
{ intros h y,
rw ← powers_eq_gpowers at hx,
rcases hx y with ⟨j, rfl⟩,
rw [← pow_mul, mul_comm, pow_mul, h, one_pow], }
end
/-- The sum of `x ^ i` as `x` ranges over the units of a finite field of cardinality `q`
is equal to `0` unless `(q - 1) ∣ i`, in which case the sum is `q - 1`. -/
lemma sum_pow_units (i : ℕ) :
∑ x : units K, (x ^ i : K) = if (q - 1) ∣ i then -1 else 0 :=
begin
let φ : units K →* K :=
{ to_fun := λ x, x ^ i,
map_one' := by rw [units.coe_one, one_pow],
map_mul' := by { intros, rw [units.coe_mul, mul_pow] } },
haveI : decidable (φ = 1) := by { classical, apply_instance },
calc ∑ x : units K, φ x = if φ = 1 then fintype.card (units K) else 0 : sum_hom_units φ
... = if (q - 1) ∣ i then -1 else 0 : _,
suffices : (q - 1) ∣ i ↔ φ = 1,
{ simp only [this],
split_ifs with h h, swap, refl,
rw [card_units, nat.cast_sub, cast_card_eq_zero, nat.cast_one, zero_sub],
show 1 ≤ q, from fintype.card_pos_iff.mpr ⟨0⟩ },
rw [← forall_pow_eq_one_iff, monoid_hom.ext_iff],
apply forall_congr, intro x,
rw [units.ext_iff, units.coe_pow, units.coe_one, monoid_hom.one_apply],
refl,
end
/-- The sum of `x ^ i` as `x` ranges over a finite field of cardinality `q`
is equal to `0` if `i < q - 1`. -/
lemma sum_pow_lt_card_sub_one (i : ℕ) (h : i < q - 1) :
∑ x : K, x ^ i = 0 :=
begin
by_cases hi : i = 0,
{ simp only [hi, nsmul_one, sum_const, pow_zero, card_univ, cast_card_eq_zero], },
classical,
have hiq : ¬ (q - 1) ∣ i, { contrapose! h, exact nat.le_of_dvd (nat.pos_of_ne_zero hi) h },
let φ : units K ↪ K := ⟨coe, units.ext⟩,
have : univ.map φ = univ \ {0},
{ ext x,
simp only [true_and, embedding.coe_fn_mk, mem_sdiff, units.exists_iff_ne_zero,
mem_univ, mem_map, exists_prop_of_true, mem_singleton] },
calc ∑ x : K, x ^ i = ∑ x in univ \ {(0 : K)}, x ^ i :
by rw [← sum_sdiff ({0} : finset K).subset_univ, sum_singleton,
zero_pow (nat.pos_of_ne_zero hi), add_zero]
... = ∑ x : units K, x ^ i : by { rw [← this, univ.sum_map φ], refl }
... = 0 : by { rw [sum_pow_units K i, if_neg], exact hiq, }
end
end finite_field
namespace zmod
open finite_field polynomial
lemma sum_two_squares (p : ℕ) [hp : fact p.prime] (x : zmod p) :
∃ a b : zmod p, a^2 + b^2 = x :=
begin
cases hp.eq_two_or_odd with hp2 hp_odd,
{ unfreezeI, subst p, revert x, exact dec_trivial },
let f : polynomial (zmod p) := X^2,
let g : polynomial (zmod p) := X^2 - C x,
obtain ⟨a, b, hab⟩ : ∃ a b, f.eval a + g.eval b = 0 :=
@exists_root_sum_quadratic _ _ _ f g
(degree_X_pow 2) (degree_X_pow_sub_C dec_trivial _) (by rw [zmod.card, hp_odd]),
refine ⟨a, b, _⟩,
rw ← sub_eq_zero,
simpa only [eval_C, eval_X, eval_pow, eval_sub, ← add_sub_assoc] using hab,
end
end zmod
namespace char_p
lemma sum_two_squares (R : Type*) [integral_domain R] (p : ℕ) [fact (0 < p)] [char_p R p] (x : ℤ) :
∃ a b : ℕ, (a^2 + b^2 : R) = x :=
begin
haveI := char_is_prime_of_pos R p,
obtain ⟨a, b, hab⟩ := zmod.sum_two_squares p x,
refine ⟨a.val, b.val, _⟩,
have := congr_arg (zmod.cast_hom p R) hab,
simpa only [zmod.cast_int_cast, zmod.cast_hom_apply, zmod.cast_add,
zmod.nat_cast_val, _root_.pow_two, zmod.cast_mul]
end
end char_p
open_locale nat
open zmod
/-- The Fermat-Euler totient theorem. `nat.modeq.pow_totient` is an alternative statement
of the same theorem. -/
@[simp] lemma zmod.pow_totient {n : ℕ} [fact (0 < n)] (x : units (zmod n)) : x ^ φ n = 1 :=
by rw [← card_units_eq_totient, pow_card_eq_one]
/-- The Fermat-Euler totient theorem. `zmod.pow_totient` is an alternative statement
of the same theorem. -/
lemma nat.modeq.pow_totient {x n : ℕ} (h : nat.coprime x n) : x ^ φ n ≡ 1 [MOD n] :=
begin
cases n, {simp},
rw ← zmod.eq_iff_modeq_nat,
let x' : units (zmod (n+1)) := zmod.unit_of_coprime _ h,
have := zmod.pow_totient x',
apply_fun (coe : units (zmod (n+1)) → zmod (n+1)) at this,
simpa only [-zmod.pow_totient, nat.succ_eq_add_one, nat.cast_pow, units.coe_one,
nat.cast_one, cast_unit_of_coprime, units.coe_pow],
end
|
a97099eac3570e0e86e7e075ebfa4149691bf08c | 41ebf3cb010344adfa84907b3304db00e02db0a6 | /uexp/src/uexp/meta/canonize.lean | 6bb6ffcebb85523adbab351f4b23862a454e52a3 | [
"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 | 4,441 | lean | import .cosette_tactics .TDP
open tactic
section canonize
meta inductive canonize_term_repr : Type
| sig : expr → list name → canonize_term_repr
| prod : list expr → canonize_term_repr
meta def expr_to_canonize_term_repr (ex : expr)
: tactic canonize_term_repr :=
match sigma_expr_to_sigma_repr ex with
| ⟨[], e⟩ := canonize_term_repr.prod <$> product_to_repr e
| repr := function.uncurry canonize_term_repr.sig
<$> sigma_repr_to_closed_body_expr repr
end
private meta definition rw_kc_in_prod_repr (kc : key_constraint) (assumptions : list expr)
: expr → tactic (expr × expr)
| `(denoteProj %%k₁ %%t ≃ denoteProj %%k₂ %%t') :=
let is_denote_r (tup : expr) (e : expr) : bool :=
match e with
| `(denote_r %%R %%tup') := R = kc.table ∧ tup' = tup
| _ := ff
end in
if k₁ = k₂ ∧ k₁ = kc.column
∧ assumptions.any (is_denote_r t)
∧ assumptions.any (is_denote_r t')
then return (t, t')
else failed
| _ := failed
private meta def mk_key_lhs (kc : key_constraint) (t t' : expr)
: tactic (list expr) :=
monad.mapm to_expr
[ ``(denoteProj %%kc.column %%t ≃ denoteProj %%kc.column %%t')
, ``(denote_r %%kc.table %%t)
, ``(denote_r %%kc.table %%t')
]
private meta def mk_key_rhs (kc : key_constraint) (t t' : expr)
: tactic (list expr) :=
monad.mapm to_expr [``(%%t≃%%t'), ``(denote_r %%kc.table %%t)]
meta definition canonize : tactic unit :=
let rewrite_constraint_occurence (kc : key_constraint) (e : expr) : tactic unit := do
repr ← expr_to_canonize_term_repr e,
match repr with
| canonize_term_repr.sig body lconsts := do
body_terms ← product_to_repr body,
(t, t') ← list.foldr (<|>) failed
$ list.map (rw_kc_in_prod_repr kc body_terms) body_terms,
key_terms ← mk_key_lhs kc t t',
let non_key_terms := body_terms.remove_first_of_each key_terms,
let body' := key_terms ++ non_key_terms,
rewritten_key_terms ← mk_key_rhs kc t t',
let rewritten_body := rewritten_key_terms ++ non_key_terms,
intermediate_body_expr ← repr_to_product body',
rewritten_body_expr ← repr_to_product rewritten_body,
intermediate_sigma_repr ← closed_sigma_repr_to_sigma_repr intermediate_body_expr lconsts,
rewritten_sigma_repr ← closed_sigma_repr_to_sigma_repr rewritten_body_expr lconsts,
intermediate_expr ← sigma_repr_to_sigma_expr intermediate_sigma_repr,
rewritten_expr ← sigma_repr_to_sigma_expr rewritten_sigma_repr,
eq_lemma ← tactic.to_expr ``(%%e = %%rewritten_expr),
lemma_name ← tactic.mk_fresh_name,
tactic.assert lemma_name eq_lemma,
to_expr ``(@eq.trans _ %%e %%intermediate_expr %%rewritten_expr) >>= apply,
repeat_n lconsts.length `[apply congr_arg usr.sig, funext], ac_refl,
repeat_n lconsts.length `[apply congr_arg usr.sig, funext],
to_expr ``(isKey_times_const %%kc.column %%kc.table %%kc.name) >>= rewrite_target,
ac_refl,
eq_lemma_name ← tactic.resolve_name lemma_name >>= tactic.to_expr,
tactic.rewrite_target eq_lemma_name,
tactic.clear eq_lemma_name
| canonize_term_repr.prod terms := do
(t, t') ← list.foldr (<|>) failed
$ list.map (rw_kc_in_prod_repr kc terms) terms,
key_terms ← mk_key_lhs kc t t',
let non_key_terms := terms.remove_first_of_each key_terms,
let body' := terms ++ non_key_terms,
rewritten_key_terms ← mk_key_rhs kc t t',
let rewritten_body := rewritten_key_terms ++ non_key_terms,
intermediate_expr ← repr_to_product body',
rewritten_expr ← repr_to_product rewritten_body,
eq_lemma ← tactic.to_expr ``(%%e = %%rewritten_expr),
lemma_name ← tactic.mk_fresh_name,
tactic.assert lemma_name eq_lemma,
to_expr ``(@eq.trans _ %%e %%intermediate_expr %%rewritten_expr) >>= apply,
ac_refl,
rewrite_target kc.name,
ac_refl,
eq_lemma_name ← tactic.resolve_name lemma_name >>= tactic.to_expr,
tactic.rewrite_target eq_lemma_name,
tactic.clear eq_lemma_name
end
in do
constraints ← find_keys,
monad.mapm' (λ kc, repeat $ get_lhs >>= rewrite_constraint_occurence kc)
constraints,
monad.mapm' (λ kc, repeat $ get_rhs >>= rewrite_constraint_occurence kc)
constraints
end canonize |
f5f14a524db130aa82328c3b8dbb6e9a852567ca | 12dabd587ce2621d9a4eff9f16e354d02e206c8e | /world05/level06.lean | beba877a8ebe13670944f67321590a7949736f56 | [] | no_license | abdelq/natural-number-game | a1b5b8f1d52625a7addcefc97c966d3f06a48263 | bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2 | refs/heads/master | 1,668,606,478,691 | 1,594,175,058,000 | 1,594,175,058,000 | 278,673,209 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 139 | lean | example (P Q R : Type) : (P → (Q → R)) → ((P → Q) → (P → R)) :=
begin
intro f,
intro h,
intro p,
apply (f p),
apply (h p),
end
|
cc0d3cf52836d0405aff7167f8134e5637ba3b1a | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/field_theory/minpoly.lean | 80106643f08f258d7b2c4b6a445f438e9768778e | [
"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 | 18,023 | lean | /-
Copyright (c) 2019 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johan Commelin
-/
import data.polynomial.field_division
import ring_theory.integral_closure
import ring_theory.polynomial.gauss_lemma
/-!
# Minimal polynomials
This file defines the minimal polynomial of an element `x` of an `A`-algebra `B`,
under the assumption that x is integral over `A`.
After stating the defining property we specialize to the setting of field extensions
and derive some well-known properties, amongst which the fact that minimal polynomials
are irreducible, and uniquely determined by their defining property.
-/
open_locale classical
open polynomial set function
variables {A B : Type*}
section min_poly_def
variables (A) [comm_ring A] [ring B] [algebra A B]
/--
Suppose `x : B`, where `B` is an `A`-algebra.
The minimal polynomial `minpoly A x` of `x`
is a monic polynomial with coefficients in `A` of smallest degree that has `x` as its root,
if such exists (`is_integral A x`) or zero otherwise.
For example, if `V` is a `𝕜`-vector space for some field `𝕜` and `f : V →ₗ[𝕜] V` then
the minimal polynomial of `f` is `minpoly 𝕜 f`.
-/
noncomputable def minpoly (x : B) : polynomial A :=
if hx : is_integral A x then well_founded.min degree_lt_wf _ hx else 0
end min_poly_def
namespace minpoly
section ring
variables [comm_ring A] [ring B] [algebra A B]
variables {x : B}
/-- A minimal polynomial is monic. -/
lemma monic (hx : is_integral A x) : monic (minpoly A x) :=
by { delta minpoly, rw dif_pos hx, exact (well_founded.min_mem degree_lt_wf _ hx).1 }
/-- A minimal polynomial is nonzero. -/
lemma ne_zero [nontrivial A] (hx : is_integral A x) : minpoly A x ≠ 0 :=
ne_zero_of_monic (monic hx)
lemma eq_zero (hx : ¬ is_integral A x) : minpoly A x = 0 :=
dif_neg hx
variables (A x)
/-- An element is a root of its minimal polynomial. -/
@[simp] lemma aeval : aeval x (minpoly A x) = 0 :=
begin
delta minpoly, split_ifs with hx,
{ exact (well_founded.min_mem degree_lt_wf _ hx).2 },
{ exact aeval_zero _ }
end
lemma mem_range_of_degree_eq_one (hx : (minpoly A x).degree = 1) : x ∈ (algebra_map A B).range :=
begin
have h : is_integral A x,
{ by_contra h,
rw [eq_zero h, degree_zero, ←with_bot.coe_one] at hx,
exact (ne_of_lt (show ⊥ < ↑1, from with_bot.bot_lt_coe 1) hx) },
have key := minpoly.aeval A x,
rw [eq_X_add_C_of_degree_eq_one hx, (minpoly.monic h).leading_coeff, C_1, one_mul, aeval_add,
aeval_C, aeval_X, ←eq_neg_iff_add_eq_zero, ←ring_hom.map_neg] at key,
exact ⟨-(minpoly A x).coeff 0, key.symm⟩,
end
/-- The defining property of the minimal polynomial of an element `x`:
it is the monic polynomial with smallest degree that has `x` as its root. -/
lemma min {p : polynomial A} (pmonic : p.monic) (hp : polynomial.aeval x p = 0) :
degree (minpoly A x) ≤ degree p :=
begin
delta minpoly, split_ifs with hx,
{ exact le_of_not_lt (well_founded.not_lt_min degree_lt_wf _ hx ⟨pmonic, hp⟩) },
{ simp only [degree_zero, bot_le] }
end
end ring
section integral_domain
variables [integral_domain A]
section ring
variables [ring B] [algebra A B] [nontrivial B]
variables {x : B}
/-- The degree of a minimal polynomial, as a natural number, is positive. -/
lemma nat_degree_pos (hx : is_integral A x) : 0 < nat_degree (minpoly A x) :=
begin
rw pos_iff_ne_zero,
intro ndeg_eq_zero,
have eq_one : minpoly A x = 1,
{ rw eq_C_of_nat_degree_eq_zero ndeg_eq_zero, convert C_1,
simpa only [ndeg_eq_zero.symm] using (monic hx).leading_coeff },
simpa only [eq_one, alg_hom.map_one, one_ne_zero] using aeval A x
end
/-- The degree of a minimal polynomial is positive. -/
lemma degree_pos (hx : is_integral A x) : 0 < degree (minpoly A x) :=
nat_degree_pos_iff_degree_pos.mp (nat_degree_pos hx)
/-- If `B/A` is an injective ring extension, and `a` is an element of `A`,
then the minimal polynomial of `algebra_map A B a` is `X - C a`. -/
lemma eq_X_sub_C_of_algebra_map_inj [nontrivial A]
(a : A) (hf : function.injective (algebra_map A B)) :
minpoly A (algebra_map A B a) = X - C a :=
begin
have hdegle : (minpoly A (algebra_map A B a)).nat_degree ≤ 1,
{ apply with_bot.coe_le_coe.1,
rw [←degree_eq_nat_degree (ne_zero (@is_integral_algebra_map A B _ _ _ a)),
with_top.coe_one, ←degree_X_sub_C a],
refine min A (algebra_map A B a) (monic_X_sub_C a) _,
simp only [aeval_C, aeval_X, alg_hom.map_sub, sub_self] },
have hdeg : (minpoly A (algebra_map A B a)).degree = 1,
{ apply (degree_eq_iff_nat_degree_eq (ne_zero (@is_integral_algebra_map A B _ _ _ a))).2,
apply le_antisymm hdegle (nat_degree_pos (@is_integral_algebra_map A B _ _ _ a)) },
have hrw := eq_X_add_C_of_degree_eq_one hdeg,
simp only [monic (@is_integral_algebra_map A B _ _ _ a), one_mul,
monic.leading_coeff, ring_hom.map_one] at hrw,
have h0 : (minpoly A (algebra_map A B a)).coeff 0 = -a,
{ have hroot := aeval A (algebra_map A B a),
rw [hrw, add_comm] at hroot,
simp only [aeval_C, aeval_X, aeval_add] at hroot,
replace hroot := eq_neg_of_add_eq_zero hroot,
rw [←ring_hom.map_neg _ a] at hroot,
exact (hf hroot) },
rw hrw,
simp only [h0, ring_hom.map_neg, sub_eq_add_neg],
end
variables (A x)
/-- A minimal polynomial is not a unit. -/
lemma not_is_unit : ¬ is_unit (minpoly A x) :=
begin
by_cases hx : is_integral A x,
{ assume H, exact (ne_of_lt (degree_pos hx)).symm (degree_eq_zero_of_is_unit H) },
{ delta minpoly, rw dif_neg hx, simp only [not_is_unit_zero, not_false_iff] }
end
end ring
section domain
variables [domain B] [algebra A B]
variables {x : B}
/-- If `a` strictly divides the minimal polynomial of `x`, then `x` cannot be a root for `a`. -/
lemma aeval_ne_zero_of_dvd_not_unit_minpoly {a : polynomial A} (hx : is_integral A x)
(hamonic : a.monic) (hdvd : dvd_not_unit a (minpoly A x)) :
polynomial.aeval x a ≠ 0 :=
begin
intro ha,
refine not_lt_of_ge (minpoly.min A x hamonic ha) _,
obtain ⟨hzeroa, b, hb_nunit, prod⟩ := hdvd,
have hbmonic : b.monic,
{ rw monic.def,
have := monic hx,
rwa [monic.def, prod, leading_coeff_mul, monic.def.mp hamonic, one_mul] at this },
have hzerob : b ≠ 0 := hbmonic.ne_zero,
have degbzero : 0 < b.nat_degree,
{ apply nat.pos_of_ne_zero,
intro h,
have h₁ := eq_C_of_nat_degree_eq_zero h,
rw [←h, ←leading_coeff, monic.def.1 hbmonic, C_1] at h₁,
rw h₁ at hb_nunit,
have := is_unit_one,
contradiction },
rw [prod, degree_mul, degree_eq_nat_degree hzeroa, degree_eq_nat_degree hzerob],
exact_mod_cast lt_add_of_pos_right _ degbzero,
end
/-- A minimal polynomial is irreducible. -/
lemma irreducible (hx : is_integral A x) : irreducible (minpoly A x) :=
begin
cases irreducible_or_factor (minpoly A x) (not_is_unit A x) with hirr hred,
{ exact hirr },
exfalso,
obtain ⟨a, b, ha_nunit, hb_nunit, hab_eq⟩ := hred,
have coeff_prod : a.leading_coeff * b.leading_coeff = 1,
{ rw [←monic.def.1 (monic hx), ←hab_eq],
simp only [leading_coeff_mul] },
have hamonic : (a * C b.leading_coeff).monic,
{ rw monic.def,
simp only [coeff_prod, leading_coeff_mul, leading_coeff_C] },
have hbmonic : (b * C a.leading_coeff).monic,
{ rw [monic.def, mul_comm],
simp only [coeff_prod, leading_coeff_mul, leading_coeff_C] },
have prod : minpoly A x = (a * C b.leading_coeff) * (b * C a.leading_coeff),
{ symmetry,
calc a * C b.leading_coeff * (b * C a.leading_coeff)
= a * b * (C a.leading_coeff * C b.leading_coeff) : by ring
... = a * b * (C (a.leading_coeff * b.leading_coeff)) : by simp only [ring_hom.map_mul]
... = a * b : by rw [coeff_prod, C_1, mul_one]
... = minpoly A x : hab_eq },
have hzero := aeval A x,
rw [prod, aeval_mul, mul_eq_zero] at hzero,
cases hzero,
{ refine aeval_ne_zero_of_dvd_not_unit_minpoly hx hamonic _ hzero,
exact ⟨hamonic.ne_zero, _, mt is_unit_of_mul_is_unit_left hb_nunit, prod⟩ },
{ refine aeval_ne_zero_of_dvd_not_unit_minpoly hx hbmonic _ hzero,
rw mul_comm at prod,
exact ⟨hbmonic.ne_zero, _, mt is_unit_of_mul_is_unit_left ha_nunit, prod⟩ },
end
end domain
end integral_domain
section field
variables [field A]
section ring
variables [ring B] [algebra A B]
variables {x : B}
variables (A x)
/-- If an element `x` is a root of a nonzero polynomial `p`,
then the degree of `p` is at least the degree of the minimal polynomial of `x`. -/
lemma degree_le_of_ne_zero
{p : polynomial A} (pnz : p ≠ 0) (hp : polynomial.aeval x p = 0) :
degree (minpoly A x) ≤ degree p :=
calc degree (minpoly A x) ≤ degree (p * C (leading_coeff p)⁻¹) :
min A x (monic_mul_leading_coeff_inv pnz) (by simp [hp])
... = degree p : degree_mul_leading_coeff_inv p pnz
/-- The minimal polynomial of an element `x` is uniquely characterized by its defining property:
if there is another monic polynomial of minimal degree that has `x` as a root,
then this polynomial is equal to the minimal polynomial of `x`. -/
lemma unique {p : polynomial A}
(pmonic : p.monic) (hp : polynomial.aeval x p = 0)
(pmin : ∀ q : polynomial A, q.monic → polynomial.aeval x q = 0 → degree p ≤ degree q) :
p = minpoly A x :=
begin
have hx : is_integral A x := ⟨p, pmonic, hp⟩,
symmetry, apply eq_of_sub_eq_zero,
by_contra hnz,
have := degree_le_of_ne_zero A x hnz (by simp [hp]),
contrapose! this,
apply degree_sub_lt _ (ne_zero hx),
{ rw [(monic hx).leading_coeff, pmonic.leading_coeff] },
{ exact le_antisymm (min A x pmonic hp)
(pmin (minpoly A x) (monic hx) (aeval A x)) }
end
/-- If an element `x` is a root of a polynomial `p`,
then the minimal polynomial of `x` divides `p`. -/
lemma dvd {p : polynomial A} (hp : polynomial.aeval x p = 0) : minpoly A x ∣ p :=
begin
by_cases hp0 : p = 0,
{ simp only [hp0, dvd_zero] },
have hx : is_integral A x,
{ rw ← is_algebraic_iff_is_integral, exact ⟨p, hp0, hp⟩ },
rw ← dvd_iff_mod_by_monic_eq_zero (monic hx),
by_contra hnz,
have := degree_le_of_ne_zero A x hnz _,
{ contrapose! this,
exact degree_mod_by_monic_lt _ (monic hx) (ne_zero hx) },
{ rw ← mod_by_monic_add_div p (monic hx) at hp,
simpa using hp }
end
lemma dvd_map_of_is_scalar_tower (A K : Type*) {R : Type*} [comm_ring A] [field K] [comm_ring R]
[algebra A K] [algebra A R] [algebra K R] [is_scalar_tower A K R] (x : R) :
minpoly K x ∣ (minpoly A x).map (algebra_map A K) :=
by { refine minpoly.dvd K x _, rw [← is_scalar_tower.aeval_apply, minpoly.aeval] }
variables {A x}
theorem unique' [nontrivial B] {p : polynomial A} (hp1 : _root_.irreducible p)
(hp2 : polynomial.aeval x p = 0) (hp3 : p.monic) : p = minpoly A x :=
let ⟨q, hq⟩ := dvd A x hp2 in
eq_of_monic_of_associated hp3 (monic ⟨p, ⟨hp3, hp2⟩⟩) $
mul_one (minpoly A x) ▸ hq.symm ▸ associated_mul_mul (associated.refl _) $
associated_one_iff_is_unit.2 $ (hp1.is_unit_or_is_unit hq).resolve_left $ not_is_unit A x
lemma unique'' [nontrivial B] {p : polynomial A}
(hp1 : _root_.irreducible p) (hp2 : polynomial.aeval x p = 0) :
p * C p.leading_coeff⁻¹ = minpoly A x :=
begin
have : p.leading_coeff ≠ 0 := leading_coeff_ne_zero.mpr hp1.ne_zero,
apply unique',
{ exact irreducible_of_associated ⟨⟨C p.leading_coeff⁻¹, C p.leading_coeff,
by rwa [←C_mul, inv_mul_cancel, C_1], by rwa [←C_mul, mul_inv_cancel, C_1]⟩, rfl⟩ hp1 },
{ rw [aeval_mul, hp2, zero_mul] },
{ rwa [polynomial.monic, leading_coeff_mul, leading_coeff_C, mul_inv_cancel] },
end
/-- If `y` is the image of `x` in an extension, their minimal polynomials coincide.
We take `h : y = algebra_map L T x` as an argument because `rw h` typically fails
since `is_integral R y` depends on y.
-/
lemma eq_of_algebra_map_eq {K S T : Type*} [field K] [comm_ring S] [comm_ring T]
[algebra K S] [algebra K T] [algebra S T]
[is_scalar_tower K S T] (hST : function.injective (algebra_map S T))
{x : S} {y : T} (hx : is_integral K x) (h : y = algebra_map S T x) :
minpoly K x = minpoly K y :=
minpoly.unique _ _ (minpoly.monic hx)
(by rw [h, ← is_scalar_tower.algebra_map_aeval, minpoly.aeval, ring_hom.map_zero])
(λ q q_monic root_q, minpoly.min _ _ q_monic
(is_scalar_tower.aeval_eq_zero_of_aeval_algebra_map_eq_zero K S T hST
(h ▸ root_q : polynomial.aeval (algebra_map S T x) q = 0)))
section gcd_domain
/-- For GCD domains, the minimal polynomial over the ring is the same as the minimal polynomial
over the fraction field. -/
lemma gcd_domain_eq_field_fractions {A K R : Type*} [integral_domain A]
[gcd_monoid A] [field K] [integral_domain R] (f : fraction_map A K) [algebra f.codomain R]
[algebra A R] [is_scalar_tower A f.codomain R] {x : R} (hx : is_integral A x) :
minpoly f.codomain x = (minpoly A x).map (localization_map.to_ring_hom f) :=
begin
symmetry,
refine unique' _ _ _,
{ exact (polynomial.is_primitive.irreducible_iff_irreducible_map_fraction_map f
(polynomial.monic.is_primitive (monic hx))).1 (irreducible hx) },
{ have htower := is_scalar_tower.aeval_apply A f.codomain R x (minpoly A x),
simp only [localization_map.algebra_map_eq, aeval] at htower,
exact htower.symm },
{ exact monic_map _ (monic hx) }
end
/-- The minimal polynomial over `ℤ` is the same as the minimal polynomial over `ℚ`. -/
--TODO use `gcd_domain_eq_field_fractions` directly when localizations are defined
-- in terms of algebras instead of `ring_hom`s
lemma over_int_eq_over_rat {A : Type*} [integral_domain A] {x : A} [hℚA : algebra ℚ A]
(hx : is_integral ℤ x) :
minpoly ℚ x = map (int.cast_ring_hom ℚ) (minpoly ℤ x) :=
begin
symmetry,
refine unique' _ _ _,
{ exact (is_primitive.int.irreducible_iff_irreducible_map_cast
(polynomial.monic.is_primitive (monic hx))).1 (irreducible hx) },
{ have htower := is_scalar_tower.aeval_apply ℤ ℚ A x (minpoly ℤ x),
simp only [localization_map.algebra_map_eq, aeval] at htower,
exact htower.symm },
{ exact monic_map _ (monic hx) }
end
/-- For GCD domains, the minimal polynomial divides any primitive polynomial that has the integral
element as root. -/
lemma gcd_domain_dvd {A K R : Type*}
[integral_domain A] [gcd_monoid A] [field K] [integral_domain R]
(f : fraction_map A K) [algebra f.codomain R] [algebra A R] [is_scalar_tower A f.codomain R]
{x : R} (hx : is_integral A x)
{P : polynomial A} (hprim : is_primitive P) (hroot : polynomial.aeval x P = 0) :
minpoly A x ∣ P :=
begin
apply (is_primitive.dvd_iff_fraction_map_dvd_fraction_map f
(monic.is_primitive (monic hx)) hprim ).2,
rw [← gcd_domain_eq_field_fractions f hx],
refine dvd _ _ _,
rwa [← localization_map.algebra_map_eq, ← is_scalar_tower.aeval_apply]
end
/-- The minimal polynomial over `ℤ` divides any primitive polynomial that has the integral element
as root. -/
-- TODO use `gcd_domain_dvd` directly when localizations are defined in terms of algebras
-- instead of `ring_hom`s
lemma integer_dvd {A : Type*} [integral_domain A] [algebra ℚ A] {x : A} (hx : is_integral ℤ x)
{P : polynomial ℤ} (hprim : is_primitive P) (hroot : polynomial.aeval x P = 0) :
minpoly ℤ x ∣ P :=
begin
apply (is_primitive.int.dvd_iff_map_cast_dvd_map_cast _ _
(monic.is_primitive (monic hx)) hprim ).2,
rw [← over_int_eq_over_rat hx],
refine dvd _ _ _,
rwa [(int.cast_ring_hom ℚ).ext_int (algebra_map ℤ ℚ), ← is_scalar_tower.aeval_apply]
end
end gcd_domain
variables (B) [nontrivial B]
/-- If `B/K` is a nontrivial algebra over a field, and `x` is an element of `K`,
then the minimal polynomial of `algebra_map K B x` is `X - C x`. -/
lemma eq_X_sub_C (a : A) : minpoly A (algebra_map A B a) = X - C a :=
eq_X_sub_C_of_algebra_map_inj a (algebra_map A B).injective
lemma eq_X_sub_C' (a : A) : minpoly A a = X - C a := eq_X_sub_C A a
variables (A)
/-- The minimal polynomial of `0` is `X`. -/
@[simp] lemma zero : minpoly A (0:B) = X :=
by simpa only [add_zero, C_0, sub_eq_add_neg, neg_zero, ring_hom.map_zero]
using eq_X_sub_C B (0:A)
/-- The minimal polynomial of `1` is `X - 1`. -/
@[simp] lemma one : minpoly A (1:B) = X - 1 :=
by simpa only [ring_hom.map_one, C_1, sub_eq_add_neg] using eq_X_sub_C B (1:A)
end ring
section domain
variables [domain B] [algebra A B]
variables {x : B}
/-- A minimal polynomial is prime. -/
lemma prime (hx : is_integral A x) : prime (minpoly A x) :=
begin
refine ⟨ne_zero hx, not_is_unit A x, _⟩,
rintros p q ⟨d, h⟩,
have : polynomial.aeval x (p*q) = 0 := by simp [h, aeval A x],
replace : polynomial.aeval x p = 0 ∨ polynomial.aeval x q = 0 := by simpa,
exact or.imp (dvd A x) (dvd A x) this
end
/-- If `L/K` is a field extension and an element `y` of `K` is a root of the minimal polynomial
of an element `x ∈ L`, then `y` maps to `x` under the field embedding. -/
lemma root {x : B} (hx : is_integral A x) {y : A} (h : is_root (minpoly A x) y) :
algebra_map A B y = x :=
have key : minpoly A x = X - C y :=
eq_of_monic_of_associated (monic hx) (monic_X_sub_C y) (associated_of_dvd_dvd
(dvd_symm_of_irreducible (irreducible_X_sub_C y) (irreducible hx) (dvd_iff_is_root.2 h))
(dvd_iff_is_root.2 h)),
by { have := aeval A x, rwa [key, alg_hom.map_sub, aeval_X, aeval_C, sub_eq_zero, eq_comm] at this }
/-- The constant coefficient of the minimal polynomial of `x` is `0` if and only if `x = 0`. -/
@[simp] lemma coeff_zero_eq_zero (hx : is_integral A x) : coeff (minpoly A x) 0 = 0 ↔ x = 0 :=
begin
split,
{ intro h,
have zero_root := zero_is_root_of_coeff_zero_eq_zero h,
rw ← root hx zero_root,
exact ring_hom.map_zero _ },
{ rintro rfl, simp }
end
/-- The minimal polynomial of a nonzero element has nonzero constant coefficient. -/
lemma coeff_zero_ne_zero (hx : is_integral A x) (h : x ≠ 0) : coeff (minpoly A x) 0 ≠ 0 :=
by { contrapose! h, simpa only [hx, coeff_zero_eq_zero] using h }
end domain
end field
end minpoly
|
ce16ba53ddbdd7f036c9eab0544b9f58c82b5cac | ad0c7d243dc1bd563419e2767ed42fb323d7beea | /algebra/field_power.lean | e117ca99c94e20c4b739b1e921ab9e76047d7bcc | [
"Apache-2.0"
] | permissive | sebzim4500/mathlib | e0b5a63b1655f910dee30badf09bd7e191d3cf30 | 6997cafbd3a7325af5cb318561768c316ceb7757 | refs/heads/master | 1,585,549,958,618 | 1,538,221,723,000 | 1,538,221,723,000 | 150,869,076 | 0 | 0 | Apache-2.0 | 1,538,229,323,000 | 1,538,229,323,000 | null | UTF-8 | Lean | false | false | 3,260 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis
Integer power operation on fields.
-/
import algebra.group_power tactic.wlog
universe u
section field_power
open int nat
variables {α : Type u} [division_ring α]
@[simp] lemma zero_gpow : ∀ z : ℕ, z ≠ 0 → (0 : α)^z = 0
| 0 h := absurd rfl h
| (k+1) h := zero_mul _
def fpow (a : α) : ℤ → α
| (of_nat n) := a ^ n
| -[1+n] := 1/(a ^ (n+1))
lemma unit_pow {a : α} (ha : a ≠ 0) : ∀ n : ℕ, a ^ n = ↑((units.mk0 a ha)^n)
| 0 := by simp; refl
| (k+1) := by simp [_root_.pow_add]; congr; apply unit_pow
lemma fpow_eq_gpow {a : α} (h : a ≠ 0) : ∀ (z : ℤ), fpow a z = ↑(gpow (units.mk0 a h) z)
| (of_nat k) := by simp only [fpow, gpow]; apply unit_pow
| -[1+k] := by simp [fpow, gpow]; congr; apply unit_pow
lemma fpow_inv (a : α) : fpow a (-1) = a⁻¹ :=
begin change fpow a -[1+0] = a⁻¹, simp [fpow] end
lemma fpow_ne_zero_of_ne_zero {a : α} (ha : a ≠ 0) : ∀ (z : ℤ), fpow a z ≠ 0
| (of_nat n) := pow_ne_zero _ ha
| -[1+n] := one_div_ne_zero $ pow_ne_zero _ ha
@[simp] lemma fpow_zero {a : α} : fpow a 0 = 1 :=
pow_zero _
lemma fpow_add {a : α} (ha : a ≠ 0) (z1 z2 : ℤ) : fpow a (z1 + z2) = fpow a z1 * fpow a z2 :=
begin simp only [fpow_eq_gpow ha], rw ←units.mul_coe, congr, apply gpow_add end
end field_power
section discrete_field_power
open int nat
variables {α : Type u} [discrete_field α]
lemma zero_fpow : ∀ z : ℤ, z ≠ 0 → fpow (0 : α) z = 0
| (of_nat n) h :=
have h2 : n ≠ 0, from assume : n = 0, by simpa [this] using h,
by simp [h, h2, fpow]
| -[1+n] h :=
have h1 : (0 : α) ^ (n+1) = 0, from zero_mul _,
by simp [fpow, h1]
end discrete_field_power
section ordered_field_power
open int
variables {α : Type u} [discrete_linear_ordered_field α]
lemma fpow_nonneg_of_nonneg {a : α} (ha : a ≥ 0) : ∀ (z : ℤ), fpow a z ≥ 0
| (of_nat n) := pow_nonneg ha _
| -[1+n] := div_nonneg' zero_le_one $ pow_nonneg ha _
lemma fpow_le_of_le {x : α} (hx : 1 ≤ x) {a b : ℤ} (h : a ≤ b) : fpow x a ≤ fpow x b :=
begin
induction a with a a; induction b with b b,
{ simp only [fpow],
apply pow_le_pow hx,
apply le_of_coe_nat_le_coe_nat h },
{ apply absurd h,
apply not_le_of_gt,
exact lt_of_lt_of_le (neg_succ_lt_zero _) (of_nat_nonneg _) },
{ simp only [fpow, one_div_eq_inv],
apply le_trans (inv_le_one _); apply one_le_pow_of_one_le hx },
{ simp only [fpow],
apply (one_div_le_one_div _ _).2,
{ apply pow_le_pow hx,
have : -(↑(a+1) : ℤ) ≤ -(↑(b+1) : ℤ), from h,
have h' := le_of_neg_le_neg this,
apply le_of_coe_nat_le_coe_nat h' },
repeat { apply pow_pos (lt_of_lt_of_le zero_lt_one hx) } }
end
lemma pow_le_max_of_min_le {x : α} (hx : x ≥ 1) {a b c : ℤ} (h : min a b ≤ c) :
fpow x (-c) ≤ max (fpow x (-a)) (fpow x (-b)) :=
begin
wlog hle : a ≤ b,
have hnle : -b ≤ -a, from neg_le_neg hle,
have hfle : fpow x (-b) ≤ fpow x (-a), from fpow_le_of_le hx hnle,
have : fpow x (-c) ≤ fpow x (-a),
{ apply fpow_le_of_le hx,
simpa [hle] using h },
simpa [hfle] using this
end
end ordered_field_power |
35f43812f2bd9cccdc8bee308e20889d4eabd807 | 367134ba5a65885e863bdc4507601606690974c1 | /src/category_theory/limits/functor_category.lean | ef18c2dfe044d50b6e9d55163652e6fd719c3705 | [
"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 | 10,671 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.limits.preserves.limits
open category_theory category_theory.category
namespace category_theory.limits
universes v v₂ u -- declare the `v`'s first; see `category_theory.category` for an explanation
variables {C : Type u} [category.{v} C]
variables {J K : Type v} [small_category J] [category.{v₂} K]
@[simp, reassoc]
lemma limit.lift_π_app (H : J ⥤ K ⥤ C) [has_limit H] (c : cone H) (j : J) (k : K) :
(limit.lift H c).app k ≫ (limit.π H j).app k = (c.π.app j).app k :=
congr_app (limit.lift_π c j) k
@[simp, reassoc]
lemma colimit.ι_desc_app (H : J ⥤ K ⥤ C) [has_colimit H] (c : cocone H) (j : J) (k : K) :
(colimit.ι H j).app k ≫ (colimit.desc H c).app k = (c.ι.app j).app k :=
congr_app (colimit.ι_desc c j) k
/--
The evaluation functors jointly reflect limits: that is, to show a cone is a limit of `F`
it suffices to show that each evaluation cone is a limit. In other words, to prove a cone is
limiting you can show it's pointwise limiting.
-/
def evaluation_jointly_reflects_limits {F : J ⥤ K ⥤ C} (c : cone F)
(t : Π (k : K), is_limit (((evaluation K C).obj k).map_cone c)) : is_limit c :=
{ lift := λ s,
{ app := λ k, (t k).lift ⟨s.X.obj k, whisker_right s.π ((evaluation K C).obj k)⟩,
naturality' := λ X Y f, (t Y).hom_ext $ λ j,
begin
rw [assoc, (t Y).fac _ j],
simpa using
((t X).fac_assoc ⟨s.X.obj X, whisker_right s.π ((evaluation K C).obj X)⟩ j _).symm,
end },
fac' := λ s j, nat_trans.ext _ _ $ funext $ λ k, (t k).fac _ j,
uniq' := λ s m w, nat_trans.ext _ _ $ funext $ λ x, (t x).hom_ext $ λ j,
(congr_app (w j) x).trans
((t x).fac ⟨s.X.obj _, whisker_right s.π ((evaluation K C).obj _)⟩ j).symm }
/--
Given a functor `F` and a collection of limit cones for each diagram `X ↦ F X k`, we can stitch
them together to give a cone for the diagram `F`.
`combined_is_limit` shows that the new cone is limiting, and `eval_combined` shows it is
(essentially) made up of the original cones.
-/
@[simps] def combine_cones (F : J ⥤ K ⥤ C) (c : Π (k : K), limit_cone (F.flip.obj k)) :
cone F :=
{ X :=
{ obj := λ k, (c k).cone.X,
map := λ k₁ k₂ f, (c k₂).is_limit.lift ⟨_, (c k₁).cone.π ≫ F.flip.map f⟩,
map_id' := λ k, (c k).is_limit.hom_ext (λ j, by { dsimp, simp }),
map_comp' := λ k₁ k₂ k₃ f₁ f₂, (c k₃).is_limit.hom_ext (λ j, by simp) },
π :=
{ app := λ j, { app := λ k, (c k).cone.π.app j },
naturality' := λ j₁ j₂ g, nat_trans.ext _ _ $ funext $ λ k, (c k).cone.π.naturality g } }
/-- The stitched together cones each project down to the original given cones (up to iso). -/
def evaluate_combined_cones (F : J ⥤ K ⥤ C) (c : Π (k : K), limit_cone (F.flip.obj k)) (k : K) :
((evaluation K C).obj k).map_cone (combine_cones F c) ≅ (c k).cone :=
cones.ext (iso.refl _) (by tidy)
/-- Stitching together limiting cones gives a limiting cone. -/
def combined_is_limit (F : J ⥤ K ⥤ C) (c : Π (k : K), limit_cone (F.flip.obj k)) :
is_limit (combine_cones F c) :=
evaluation_jointly_reflects_limits _
(λ k, (c k).is_limit.of_iso_limit (evaluate_combined_cones F c k).symm)
/--
The evaluation functors jointly reflect colimits: that is, to show a cocone is a colimit of `F`
it suffices to show that each evaluation cocone is a colimit. In other words, to prove a cocone is
colimiting you can show it's pointwise colimiting.
-/
def evaluation_jointly_reflects_colimits {F : J ⥤ K ⥤ C} (c : cocone F)
(t : Π (k : K), is_colimit (((evaluation K C).obj k).map_cocone c)) : is_colimit c :=
{ desc := λ s,
{ app := λ k, (t k).desc ⟨s.X.obj k, whisker_right s.ι ((evaluation K C).obj k)⟩,
naturality' := λ X Y f, (t X).hom_ext $ λ j,
begin
rw [(t X).fac_assoc _ j],
erw ← (c.ι.app j).naturality_assoc f,
erw (t Y).fac ⟨s.X.obj _, whisker_right s.ι _⟩ j,
dsimp,
simp,
end },
fac' := λ s j, nat_trans.ext _ _ $ funext $ λ k, (t k).fac _ j,
uniq' := λ s m w, nat_trans.ext _ _ $ funext $ λ x, (t x).hom_ext $ λ j,
(congr_app (w j) x).trans
((t x).fac ⟨s.X.obj _, whisker_right s.ι ((evaluation K C).obj _)⟩ j).symm }
/--
Given a functor `F` and a collection of colimit cocones for each diagram `X ↦ F X k`, we can stitch
them together to give a cocone for the diagram `F`.
`combined_is_colimit` shows that the new cocone is colimiting, and `eval_combined` shows it is
(essentially) made up of the original cocones.
-/
@[simps] def combine_cocones (F : J ⥤ K ⥤ C) (c : Π (k : K), colimit_cocone (F.flip.obj k)) :
cocone F :=
{ X :=
{ obj := λ k, (c k).cocone.X,
map := λ k₁ k₂ f, (c k₁).is_colimit.desc ⟨_, F.flip.map f ≫ (c k₂).cocone.ι⟩,
map_id' := λ k, (c k).is_colimit.hom_ext (λ j, by { dsimp, simp }),
map_comp' := λ k₁ k₂ k₃ f₁ f₂, (c k₁).is_colimit.hom_ext (λ j, by simp) },
ι :=
{ app := λ j, { app := λ k, (c k).cocone.ι.app j },
naturality' := λ j₁ j₂ g, nat_trans.ext _ _ $ funext $ λ k, (c k).cocone.ι.naturality g } }
/-- The stitched together cocones each project down to the original given cocones (up to iso). -/
def evaluate_combined_cocones
(F : J ⥤ K ⥤ C) (c : Π (k : K), colimit_cocone (F.flip.obj k)) (k : K) :
((evaluation K C).obj k).map_cocone (combine_cocones F c) ≅ (c k).cocone :=
cocones.ext (iso.refl _) (by tidy)
/-- Stitching together colimiting cocones gives a colimiting cocone. -/
def combined_is_colimit (F : J ⥤ K ⥤ C) (c : Π (k : K), colimit_cocone (F.flip.obj k)) :
is_colimit (combine_cocones F c) :=
evaluation_jointly_reflects_colimits _
(λ k, (c k).is_colimit.of_iso_colimit (evaluate_combined_cocones F c k).symm)
noncomputable theory
instance functor_category_has_limits_of_shape
[has_limits_of_shape J C] : has_limits_of_shape J (K ⥤ C) :=
{ has_limit := λ F, has_limit.mk
{ cone := combine_cones F (λ k, get_limit_cone _),
is_limit := combined_is_limit _ _ } }
instance functor_category_has_colimits_of_shape
[has_colimits_of_shape J C] : has_colimits_of_shape J (K ⥤ C) :=
{ has_colimit := λ F, has_colimit.mk
{ cocone := combine_cocones _ (λ k, get_colimit_cocone _),
is_colimit := combined_is_colimit _ _ } }
instance functor_category_has_limits [has_limits C] : has_limits (K ⥤ C) :=
{ has_limits_of_shape := λ J 𝒥, by resetI; apply_instance }
instance functor_category_has_colimits [has_colimits C] : has_colimits (K ⥤ C) :=
{ has_colimits_of_shape := λ J 𝒥, by resetI; apply_instance }
instance evaluation_preserves_limits_of_shape [has_limits_of_shape J C] (k : K) :
preserves_limits_of_shape J ((evaluation K C).obj k) :=
{ preserves_limit :=
λ F, preserves_limit_of_preserves_limit_cone (combined_is_limit _ _) $
is_limit.of_iso_limit (limit.is_limit _)
(evaluate_combined_cones F _ k).symm }
/--
If `F : J ⥤ K ⥤ C` is a functor into a functor category which has a limit,
then the evaluation of that limit at `k` is the limit of the evaluations of `F.obj j` at `k`.
-/
def limit_obj_iso_limit_comp_evaluation [has_limits_of_shape J C] (F : J ⥤ K ⥤ C) (k : K) :
(limit F).obj k ≅ limit (F ⋙ ((evaluation K C).obj k)) :=
preserves_limit_iso ((evaluation K C).obj k) F
@[simp, reassoc]
lemma limit_obj_iso_limit_comp_evaluation_hom_π
[has_limits_of_shape J C] (F : J ⥤ (K ⥤ C)) (j : J) (k : K) :
(limit_obj_iso_limit_comp_evaluation F k).hom ≫ limit.π (F ⋙ ((evaluation K C).obj k)) j =
(limit.π F j).app k :=
begin
dsimp [limit_obj_iso_limit_comp_evaluation],
simp,
end
@[simp, reassoc]
lemma limit_obj_iso_limit_comp_evaluation_inv_π_app
[has_limits_of_shape J C] (F : J ⥤ (K ⥤ C)) (j : J) (k : K):
(limit_obj_iso_limit_comp_evaluation F k).inv ≫ (limit.π F j).app k =
limit.π (F ⋙ ((evaluation K C).obj k)) j :=
begin
dsimp [limit_obj_iso_limit_comp_evaluation],
rw iso.inv_comp_eq,
simp,
end
@[ext]
lemma limit_obj_ext {H : J ⥤ K ⥤ C} [has_limits_of_shape J C]
{k : K} {W : C} {f g : W ⟶ (limit H).obj k}
(w : ∀ j, f ≫ (limits.limit.π H j).app k = g ≫ (limits.limit.π H j).app k) : f = g :=
begin
apply (cancel_mono (limit_obj_iso_limit_comp_evaluation H k).hom).1,
ext,
simpa using w j,
end
instance evaluation_preserves_colimits_of_shape [has_colimits_of_shape J C] (k : K) :
preserves_colimits_of_shape J ((evaluation K C).obj k) :=
{ preserves_colimit :=
λ F, preserves_colimit_of_preserves_colimit_cocone (combined_is_colimit _ _) $
is_colimit.of_iso_colimit (colimit.is_colimit _)
(evaluate_combined_cocones F _ k).symm }
/--
If `F : J ⥤ K ⥤ C` is a functor into a functor category which has a colimit,
then the evaluation of that colimit at `k` is the colimit of the evaluations of `F.obj j` at `k`.
-/
def colimit_obj_iso_colimit_comp_evaluation [has_colimits_of_shape J C] (F : J ⥤ K ⥤ C) (k : K) :
(colimit F).obj k ≅ colimit (F ⋙ ((evaluation K C).obj k)) :=
preserves_colimit_iso ((evaluation K C).obj k) F
@[simp, reassoc]
lemma colimit_obj_iso_colimit_comp_evaluation_ι_inv
[has_colimits_of_shape J C] (F : J ⥤ (K ⥤ C)) (j : J) (k : K) :
colimit.ι (F ⋙ ((evaluation K C).obj k)) j ≫ (colimit_obj_iso_colimit_comp_evaluation F k).inv =
(colimit.ι F j).app k :=
begin
dsimp [colimit_obj_iso_colimit_comp_evaluation],
simp,
end
@[simp, reassoc]
lemma colimit_obj_iso_colimit_comp_evaluation_ι_app_hom
[has_colimits_of_shape J C] (F : J ⥤ (K ⥤ C)) (j : J) (k : K) :
(colimit.ι F j).app k ≫ (colimit_obj_iso_colimit_comp_evaluation F k).hom =
colimit.ι (F ⋙ ((evaluation K C).obj k)) j :=
begin
dsimp [colimit_obj_iso_colimit_comp_evaluation],
rw ←iso.eq_comp_inv,
simp,
end
@[ext]
lemma colimit_obj_ext {H : J ⥤ K ⥤ C} [has_colimits_of_shape J C]
{k : K} {W : C} {f g : (colimit H).obj k ⟶ W}
(w : ∀ j, (colimit.ι H j).app k ≫ f = (colimit.ι H j).app k ≫ g) : f = g :=
begin
apply (cancel_epi (colimit_obj_iso_colimit_comp_evaluation H k).inv).1,
ext,
simpa using w j,
end
instance evaluation_preserves_limits [has_limits C] (k : K) :
preserves_limits ((evaluation K C).obj k) :=
{ preserves_limits_of_shape := λ J 𝒥, by resetI; apply_instance }
instance evaluation_preserves_colimits [has_colimits C] (k : K) :
preserves_colimits ((evaluation K C).obj k) :=
{ preserves_colimits_of_shape := λ J 𝒥, by resetI; apply_instance }
end category_theory.limits
|
7c4dd4a8873d2a92406591a88687c402f8e8aba6 | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/field_theory/splitting_field.lean | 395ec472898d5e57b1d3a944d6603b60a85ab02f | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 31,229 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
Definition of splitting fields, and definition of homomorphism into any field that splits
-/
import ring_theory.adjoin_root
import ring_theory.algebra_tower
import ring_theory.algebraic
import ring_theory.polynomial
import field_theory.minimal_polynomial
import linear_algebra.finite_dimensional
noncomputable theory
open_locale classical big_operators
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
namespace polynomial
variables [field α] [field β] [field γ]
open polynomial
section splits
variables (i : α →+* β)
/-- a polynomial `splits` iff it is zero or all of its irreducible factors have `degree` 1 -/
def splits (f : polynomial α) : Prop :=
f = 0 ∨ ∀ {g : polynomial β}, irreducible g → g ∣ f.map i → degree g = 1
@[simp] lemma splits_zero : splits i (0 : polynomial α) := or.inl rfl
@[simp] lemma splits_C (a : α) : splits i (C a) :=
if ha : a = 0 then ha.symm ▸ (@C_0 α _).symm ▸ splits_zero i
else
have hia : i a ≠ 0, from mt ((is_add_group_hom.injective_iff i).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 α} (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 α} (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_mul {f g : polynomial α} (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 α} (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 dvd.trans hg (dvd_mul_right _ _)),
or.inr $ λ g hgi hg, or.resolve_left h hfg hgi (by rw map_mul; exact dvd.trans hg (dvd_mul_left _ _))⟩
lemma splits_of_splits_of_dvd {f g : polynomial α} (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_map_iff (j : β →+* γ) {f : polynomial α} :
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 α} (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 : α} : (X - C x).splits i :=
splits_of_degree_eq_one _ $ degree_X_sub_C x
theorem splits_id_iff_splits {f : polynomial α} :
(f.map i).splits (ring_hom.id β) ↔ f.splits i :=
by rw [splits_map_iff, ring_hom.id_comp]
theorem splits_mul_iff {f g : polynomial α} (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 w} {s : ι → polynomial α} {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
theorem splits_prod_iff {ι : Type w} {s : ι → polynomial α} {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 β}
(h_nz : p ≠ 0) (hp : irreducible p) (hp_splits : splits (ring_hom.id β) p) :
p.degree = 1 :=
begin
rcases hp_splits,
{ contradiction },
{ apply hp_splits hp, simp }
end
lemma exists_root_of_splits {f : polynomial α} (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 α} : splits i f →
∃ (s : multiset β), f.map i = C (i f.leading_coeff) *
(s.map (λ a : β, (X : polynomial β) - C a)).prod :=
suffices splits (ring_hom.id _) (f.map i) → ∃ s : multiset β, f.map i =
(C (f.map i).leading_coeff) * (s.map (λ a : β, (X : polynomial β) - 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 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 α} (hf : f.splits i) (hfd : f.degree ≠ 0) : β :=
classical.some $ exists_root_of_splits i hf hfd
theorem map_root_of_splits {f : polynomial α} (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 α} (hf : f.splits $ ring_hom.id α) :
(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 : ∀ p ∈ m.map (λ r, X - C r), (p : _) ≠ 0,
from multiset.forall_mem_map_iff.2 $ λ _ _, X_sub_C_ne_zero _,
have h2 : ∀ p ∈ m.map (λ r, X - C (i r)), (p : _) ≠ 0,
from multiset.forall_mem_map_iff.2 $ λ _ _, X_sub_C_ne_zero _,
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_cons, multiset.bind_zero, add_zero,
multiset.bind_cons, multiset.bind_zero, add_zero, multiset.map_id']
end
lemma eq_prod_roots_of_splits {p : polynomial α} {i : α →+* β}
(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 ne_zero_of_mem : ∀ (p : polynomial β), 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) = 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.cons_add, zero_add] },
rw [hs, roots_mul prod_ne_zero, roots_C, zero_add,
roots_multiset_prod _ ne_zero_of_mem,
map_bind_roots_eq]
end
lemma nat_degree_multiset_prod {R : Type*} [integral_domain R] {s : multiset (polynomial R)}
(h : ∀ p ∈ s, p ≠ (0 : polynomial R)) :
nat_degree s.prod = (s.map nat_degree).sum :=
begin
revert h,
refine s.induction_on _ _,
{ simp },
intros p s ih h,
have hs : ∀ p ∈ s, p ≠ (0 : polynomial R) := λ p hp, h p (multiset.mem_cons_of_mem hp),
have hprod : s.prod ≠ 0 := multiset.prod_ne_zero (λ p hp, hs p hp),
rw [multiset.prod_cons, nat_degree_mul (h p (multiset.mem_cons_self _ _)) hprod, ih hs,
multiset.map_cons, multiset.sum_cons],
end
lemma nat_degree_eq_card_roots {p : polynomial α} {i : α →+* β}
(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 : ∀ p' ∈ (map i p).roots.map (λ a, X - C a), p' ≠ (0 : polynomial β),
{ intros p hp,
obtain ⟨a, ha, rfl⟩ := multiset.mem_map.mp hp,
exact X_sub_C_ne_zero _ },
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 α} {i : α →+* β} (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 α} {s : multiset β}
(hs : f.map i = C (i f.leading_coeff) * (s.map (λ a : β, (X : polynomial β) - 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
(factors (f.map i)) (s.map (λ a : β, (X : polynomial β) - C a)) :=
factors_unique
(λ p hp, irreducible_of_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 : units β →* units (polynomial β)) (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⟩
... ~ᵤ _ : associated.symm (unique_factorization_monoid.factors_prod (by simpa using hf0))),
let ⟨q, hq, hpq⟩ := exists_mem_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 α} : 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 (irreducible_of_prime hp) (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 α} : splits i f ↔
∃ (s : multiset β), f.map i = C (i f.leading_coeff) *
(s.map (λ a : β, (X : polynomial β) - C a)).prod :=
⟨exists_multiset_of_splits i, λ ⟨s, hs⟩, splits_of_exists_multiset i hs⟩
lemma splits_comp_of_splits (j : β →+* γ) {f : polynomial α}
(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
end splits
end polynomial
section embeddings
variables (F : Type*) [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_minimal_polynomial
{R : Type*} [comm_ring R] [algebra F R] (x : R) (hx : is_integral F x) :
algebra.adjoin F ({x} : set R) ≃ₐ[F] adjoin_root (minimal_polynomial hx) :=
alg_equiv.symm $ alg_equiv.of_bijective
(alg_hom.cod_restrict
(adjoin_root.alg_hom _ x $ minimal_polynomial.aeval hx) _
(λ p, adjoin_root.induction_on _ p $ λ p,
(algebra.adjoin_singleton_eq_range 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 $ minimal_polynomial.dvd hx hp,
λ y, let ⟨p, _, hp⟩ := (subalgebra.ext_iff.1 (algebra.adjoin_singleton_eq_range F x) y).1 y.2 in
⟨adjoin_root.mk _ p, subtype.eq hp⟩⟩
open finset
-- Speed up the following proof.
local attribute [irreducible] minimal_polynomial
-- TODO: Why is this so slow?
/-- 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, ∃ H : is_integral F x, polynomial.splits (algebra_map F L) (minimal_polynomial H)) →
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],
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),
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 : (minimal_polynomial H5).splits (algebra_map (algebra.adjoin F (↑s : set K)) L),
{ refine polynomial.splits_of_splits_of_dvd _
(polynomial.map_ne_zero $ minimal_polynomial.ne_zero H1 :
polynomial.map (algebra_map _ _) _ ≠ 0)
((polynomial.splits_map_iff _ _).2 _)
(minimal_polynomial.dvd _ _),
{ rw ← is_scalar_tower.algebra_map_eq, exact H2 },
{ rw [← is_scalar_tower.aeval_apply, minimal_polynomial.aeval H1] } },
obtain ⟨y, hy⟩ := polynomial.exists_root_of_splits _ H6 (minimal_polynomial.degree_ne_zero H5),
exact ⟨subalgebra.of_under _ _ $ (adjoin_root.alg_hom (minimal_polynomial H5) y hy).comp $
alg_equiv.adjoin_singleton_equiv_adjoin_root_minimal_polynomial _ _ H5⟩
end
end embeddings
namespace polynomial
variables [field α] [field β] [field γ]
open polynomial
section splitting_field
/-- Non-computably choose an irreducible factor from a polynomial. -/
def factor (f : polynomial α) : polynomial α :=
if H : ∃ g, irreducible g ∧ g ∣ f then classical.some H else X
instance irreducible_factor (f : polynomial α) : 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 α} (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 α} (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 α} (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 α) : 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 α) (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 α) :
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 α} {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 : ℕ) : Π {α : Type u} [field α], by exactI Π (f : polynomial α),
f.nat_degree = n → Type u :=
nat.rec_on n (λ α _ _ _, α) $ λ n ih α _ f hf, by exactI
ih f.remove_factor (nat_degree_remove_factor' hf)
namespace splitting_field_aux
theorem succ (n : ℕ) (f : polynomial α) (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 : ℕ) : Π {α : Type u} [field α], by exactI
Π {f : polynomial α} (hfn : f.nat_degree = n), field (splitting_field_aux n f hfn) :=
nat.rec_on n (λ α _ _ _, ‹field α›) $ λ n ih α _ f hf, ih _
instance inhabited {n : ℕ} {f : polynomial α} (hfn : f.nat_degree = n) :
inhabited (splitting_field_aux n f hfn) := ⟨37⟩
instance algebra (n : ℕ) : Π {α : Type u} [field α], by exactI
Π {f : polynomial α} (hfn : f.nat_degree = n), algebra α (splitting_field_aux n f hfn) :=
nat.rec_on n (λ α _ _ _, by exactI algebra.id α) $ λ n ih α _ f hfn,
by exactI @@algebra.comap.algebra _ _ _ _ _ _ _ (ih _)
instance algebra' {n : ℕ} {f : polynomial α} (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 α} (hfn : f.nat_degree = n + 1) :
algebra α (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) :=
splitting_field_aux.algebra (n+1) hfn
instance algebra''' {n : ℕ} {f : polynomial α} (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 α} (hfn : f.nat_degree = n + 1) :
is_scalar_tower α (adjoin_root f.factor) (splitting_field_aux _ _ hfn) :=
is_scalar_tower.of_algebra_map_eq $ λ x, rfl
instance scalar_tower' {n : ℕ} {f : polynomial α} (hfn : f.nat_degree = n + 1) :
is_scalar_tower α (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 α) (hfn : f.nat_degree = n + 1) :
by exact algebra_map α (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 : ℕ) : ∀ {α : Type u} [field α], by exactI
∀ (f : polynomial α) (hfn : f.nat_degree = n),
splits (algebra_map α $ splitting_field_aux n f hfn) f :=
nat.rec_on n (λ α _ _ 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 α _ 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 : ℕ) : ∀ {α : Type u} [field α], by exactI
∀ (f : polynomial α) (hfn : f.nat_degree = n) {β : Type*} [field β], by exactI
∀ (j : α →+* β) (hf : splits j f), ∃ k : splitting_field_aux n f hfn →+* β,
k.comp (algebra_map _ _) = j :=
nat.rec_on n (λ α _ _ _ β _ j _, by exactI ⟨j, j.comp_id⟩) $ λ n ih α _ f hf β _ 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 : ℕ) : ∀ {α : Type u} [field α], by exactI
∀ (f : polynomial α) (hfn : f.nat_degree = n),
algebra.adjoin α (↑(f.map $ algebra_map α $ splitting_field_aux n f hfn).roots.to_finset :
set (splitting_field_aux n f hfn)) = ⊤ :=
nat.rec_on n (λ α _ f hf, by exactI algebra.eq_top_iff.2 (λ x, subalgebra.range_le _ ⟨x, rfl⟩)) $
λ n ih α _ 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 α (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_cons, multiset.to_finset_zero, insert_emptyc_eq, finset.coe_singleton,
algebra.adjoin_union, ← set.image_singleton, algebra.adjoin_algebra_map α (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.range_under_adjoin α (adjoin_root f.factor)
(splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)),
ih, subalgebra.res_top] }
end splitting_field_aux
/-- A splitting field of a polynomial. -/
def splitting_field (f : polynomial α) :=
splitting_field_aux _ f rfl
namespace splitting_field
variables (f : polynomial α)
instance : field (splitting_field f) :=
splitting_field_aux.field _ _
instance inhabited : inhabited (splitting_field f) := ⟨37⟩
instance : algebra α (splitting_field f) :=
splitting_field_aux.algebra _ _
protected theorem splits : splits (algebra_map α (splitting_field f)) f :=
splitting_field_aux.splits _ _ _
variables [algebra α β] (hb : splits (algebra_map α β) f)
/-- Embeds the splitting field into any other field that splits the polynomial. -/
def lift : splitting_field f →ₐ[α] β :=
{ 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 α
(↑(f.map (algebra_map α $ splitting_field f)).roots.to_finset : set (splitting_field f)) = ⊤ :=
splitting_field_aux.adjoin_roots _ _ _
end splitting_field
variables (α β) [algebra α β]
/-- Typeclass characterising splitting fields. -/
class is_splitting_field (f : polynomial α) : Prop :=
(splits [] : splits (algebra_map α β) f)
(adjoin_roots [] : algebra.adjoin α (↑(f.map (algebra_map α β)).roots.to_finset : set β) = ⊤)
namespace is_splitting_field
variables {α}
instance splitting_field (f : polynomial α) : is_splitting_field α (splitting_field f) f :=
⟨splitting_field.splits f, splitting_field.adjoin_roots f⟩
section scalar_tower
variables {α β γ} [algebra β γ] [algebra α γ] [is_scalar_tower α β γ]
variables {α}
instance map (f : polynomial α) [is_splitting_field α γ f] :
is_splitting_field β γ (f.map $ algebra_map α β) :=
⟨by { rw [splits_map_iff, ← is_scalar_tower.algebra_map_eq], exact splits γ f },
subalgebra.res_inj α $ by { rw [map_map, ← is_scalar_tower.algebra_map_eq, subalgebra.res_top,
eq_top_iff, ← adjoin_roots γ f, algebra.adjoin_le_iff],
exact λ x hx, @algebra.subset_adjoin β _ _ _ _ _ _ hx }⟩
variables {α} (β)
theorem splits_iff (f : polynomial α) [is_splitting_field α β f] :
polynomial.splits (ring_hom.id α) f ↔ (⊤ : subalgebra α β) = ⊥ :=
⟨λ h, eq_bot_iff.2 $ adjoin_roots β f ▸ (roots_map (algebra_map α β) 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 ▸ subalgebra.algebra_map_mem _ _),
λ h, @ring_equiv.to_ring_hom_refl α _ ▸
ring_equiv.trans_symm (ring_equiv.of_bijective _ $ algebra.bijective_algbera_map_iff.2 h) ▸
by { rw ring_equiv.to_ring_hom_trans, exact splits_comp_of_splits _ _ (splits β f) }⟩
theorem mul (f g : polynomial α) (hf : f ≠ 0) (hg : g ≠ 0) [is_splitting_field α β f]
[is_splitting_field β γ (g.map $ algebra_map α β)] :
is_splitting_field α γ (f * g) :=
⟨(is_scalar_tower.algebra_map_eq α β γ).symm ▸ splits_mul _
(splits_comp_of_splits _ _ (splits β f))
((splits_map_iff _ _).1 (splits γ $ g.map $ algebra_map α β)),
by rw [map_mul, roots_mul (mul_ne_zero (map_ne_zero hf : f.map (algebra_map α γ) ≠ 0)
(map_ne_zero hg)), multiset.to_finset_add, finset.coe_union, algebra.adjoin_union,
is_scalar_tower.algebra_map_eq α β γ, ← map_map,
roots_map (algebra_map β γ) ((splits_id_iff_splits $ algebra_map α β).2 $ splits β f),
multiset.to_finset_map, finset.coe_image, algebra.adjoin_algebra_map, adjoin_roots,
algebra.map_top, is_scalar_tower.range_under_adjoin, ← map_map, adjoin_roots,
subalgebra.res_top]⟩
end scalar_tower
/-- Splitting field of `f` embeds into any field that splits `f`. -/
def lift [algebra α γ] (f : polynomial α) [is_splitting_field α β f]
(hf : polynomial.splits (algebra_map α γ) f) : β →ₐ[α] γ :=
if hf0 : f = 0 then (algebra.of_id α γ).comp $
(algebra.bot_equiv α β : (⊥ : subalgebra α β) →ₐ[α] α).comp $
by { rw ← (splits_iff β f).1 (show f.splits (ring_hom.id α), from hf0.symm ▸ splits_zero _),
exact algebra.to_top } else
alg_hom.comp (by { rw ← adjoin_roots β 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 $ minimal_polynomial.dvd _ this⟩) })
algebra.to_top
theorem finite_dimensional (f : polynomial α) [is_splitting_field α β f] : finite_dimensional α β :=
finite_dimensional.iff_fg.2 $ @algebra.coe_top α β _ _ _ ▸ adjoin_roots β 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)⟩)
/-- Any splitting field is isomorphic to `splitting_field f`. -/
def alg_equiv (f : polynomial α) [is_splitting_field α β f] : β ≃ₐ[α] splitting_field f :=
begin
refine alg_equiv.of_bijective (lift β f $ splits (splitting_field f) f)
⟨ring_hom.injective (lift β f $ splits (splitting_field f) f).to_ring_hom, _⟩,
haveI := finite_dimensional (splitting_field f) f,
haveI := finite_dimensional β f,
have : finite_dimensional.findim α β = finite_dimensional.findim α (splitting_field f) :=
le_antisymm
(linear_map.findim_le_findim_of_injective
(show function.injective (lift β f $ splits (splitting_field f) f).to_linear_map, from
ring_hom.injective (lift β f $ splits (splitting_field f) f : β →+* f.splitting_field)))
(linear_map.findim_le_findim_of_injective
(show function.injective (lift (splitting_field f) f $ splits β f).to_linear_map, from
ring_hom.injective (lift (splitting_field f) f $ splits β f : f.splitting_field →+* β))),
change function.surjective (lift β f $ splits (splitting_field f) f).to_linear_map,
refine (linear_map.injective_iff_surjective_of_findim_eq_findim this).1 _,
exact ring_hom.injective (lift β f $ splits (splitting_field f) f : β →+* f.splitting_field)
end
end is_splitting_field
end splitting_field
end polynomial
|
8ae99ea68b164df089ef2304161a52a161f097f4 | 367134ba5a65885e863bdc4507601606690974c1 | /src/ring_theory/coprime.lean | b985e82d0034455b375e83a02c65b56aa3fb3f0f | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 11,824 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Ken Lee, Chris Hughes
-/
import tactic.ring
import algebra.big_operators.basic
import data.fintype.basic
import data.int.gcd
import data.set.disjointed
/-!
# Coprime elements of a ring
## Main definitions
* `is_coprime x y`: that `x` and `y` are coprime, defined to be the existence of `a` and `b` such
that `a * x + b * y = 1`. Note that elements with no common divisors are not necessarily coprime,
e.g., the multivariate polynomials `x₁` and `x₂` are not coprime.
-/
open_locale classical big_operators
universes u v
section comm_semiring
variables {R : Type u} [comm_semiring R] (x y z : R)
/-- The proposition that `x` and `y` are coprime, defined to be the existence of `a` and `b` such
that `a * x + b * y = 1`. Note that elements with no common divisors are not necessarily coprime,
e.g., the multivariate polynomials `x₁` and `x₂` are not coprime. -/
@[simp] def is_coprime : Prop :=
∃ a b, a * x + b * y = 1
theorem nat.is_coprime_iff_coprime {m n : ℕ} : is_coprime (m : ℤ) n ↔ nat.coprime m n :=
⟨λ ⟨a, b, H⟩, nat.eq_one_of_dvd_one $ int.coe_nat_dvd.1 $ by { rw [int.coe_nat_one, ← H],
exact dvd_add (dvd_mul_of_dvd_right (int.coe_nat_dvd.2 $ nat.gcd_dvd_left m n) _)
(dvd_mul_of_dvd_right (int.coe_nat_dvd.2 $ nat.gcd_dvd_right m n) _) },
λ H, ⟨nat.gcd_a m n, nat.gcd_b m n, by rw [mul_comm _ (m : ℤ), mul_comm _ (n : ℤ),
← nat.gcd_eq_gcd_ab, show _ = _, from H, int.coe_nat_one]⟩⟩
variables {x y z}
theorem is_coprime.symm (H : is_coprime x y) : is_coprime y x :=
let ⟨a, b, H⟩ := H in ⟨b, a, by rw [add_comm, H]⟩
theorem is_coprime_comm : is_coprime x y ↔ is_coprime y x :=
⟨is_coprime.symm, is_coprime.symm⟩
theorem is_coprime_self : is_coprime x x ↔ is_unit x :=
⟨λ ⟨a, b, h⟩, is_unit_of_mul_eq_one x (a + b) $ by rwa [mul_comm, add_mul],
λ h, let ⟨b, hb⟩ := is_unit_iff_exists_inv'.1 h in ⟨b, 0, by rwa [zero_mul, add_zero]⟩⟩
theorem is_coprime_zero_left : is_coprime 0 x ↔ is_unit x :=
⟨λ ⟨a, b, H⟩, is_unit_of_mul_eq_one x b $ by rwa [mul_zero, zero_add, mul_comm] at H,
λ H, let ⟨b, hb⟩ := is_unit_iff_exists_inv'.1 H in ⟨1, b, by rwa [one_mul, zero_add]⟩⟩
theorem is_coprime_zero_right : is_coprime x 0 ↔ is_unit x :=
is_coprime_comm.trans is_coprime_zero_left
theorem is_coprime_one_left : is_coprime 1 x :=
⟨1, 0, by rw [one_mul, zero_mul, add_zero]⟩
theorem is_coprime_one_right : is_coprime x 1 :=
⟨0, 1, by rw [one_mul, zero_mul, zero_add]⟩
theorem is_coprime.dvd_of_dvd_mul_right (H1 : is_coprime x z) (H2 : x ∣ y * z) : x ∣ y :=
let ⟨a, b, H⟩ := H1 in by { rw [← mul_one y, ← H, mul_add, ← mul_assoc, mul_left_comm],
exact dvd_add (dvd_mul_left _ _) (dvd_mul_of_dvd_right H2 _) }
theorem is_coprime.dvd_of_dvd_mul_left (H1 : is_coprime x y) (H2 : x ∣ y * z) : x ∣ z :=
let ⟨a, b, H⟩ := H1 in by { rw [← one_mul z, ← H, add_mul, mul_right_comm, mul_assoc b],
exact dvd_add (dvd_mul_left _ _) (dvd_mul_of_dvd_right H2 _) }
theorem is_coprime.mul_left (H1 : is_coprime x z) (H2 : is_coprime y z) : is_coprime (x * y) z :=
let ⟨a, b, h1⟩ := H1, ⟨c, d, h2⟩ := H2 in
⟨a * c, a * x * d + b * c * y + b * d * z,
calc a * c * (x * y) + (a * x * d + b * c * y + b * d * z) * z
= (a * x + b * z) * (c * y + d * z) : by ring
... = 1 : by rw [h1, h2, mul_one]⟩
theorem is_coprime.mul_right (H1 : is_coprime x y) (H2 : is_coprime x z) : is_coprime x (y * z) :=
by { rw is_coprime_comm at H1 H2 ⊢, exact H1.mul_left H2 }
variables {I : Type v} {s : I → R} {t : finset I}
theorem is_coprime.prod_left : (∀ i ∈ t, is_coprime (s i) x) → is_coprime (∏ i in t, s i) x :=
finset.induction_on t (λ _, is_coprime_one_left) $ λ b t hbt ih H,
by { rw finset.prod_insert hbt, rw finset.forall_mem_insert at H, exact H.1.mul_left (ih H.2) }
theorem is_coprime.prod_right : (∀ i ∈ t, is_coprime x (s i)) → is_coprime x (∏ i in t, s i) :=
by simpa only [is_coprime_comm] using is_coprime.prod_left
theorem is_coprime.mul_dvd (H : is_coprime x y) (H1 : x ∣ z) (H2 : y ∣ z) : x * y ∣ z :=
begin
obtain ⟨a, b, h⟩ := H,
rw [← mul_one z, ← h, mul_add],
apply dvd_add,
{ rw [mul_comm z, mul_assoc],
exact dvd_mul_of_dvd_right (mul_dvd_mul_left _ H2) _ },
{ rw [mul_comm b, ← mul_assoc],
exact dvd_mul_of_dvd_left (mul_dvd_mul_right H1 _) _ }
end
theorem finset.prod_dvd_of_coprime :
∀ (Hs : set.pairwise_on (↑t : set I) (is_coprime on s)) (Hs1 : ∀ i ∈ t, s i ∣ z),
∏ x in t, s x ∣ z :=
finset.induction_on t (λ _ _, one_dvd z)
begin
intros a r har ih Hs Hs1,
rw finset.prod_insert har,
have aux1 : a ∈ (↑(insert a r) : set I) := finset.mem_insert_self a r,
refine (is_coprime.prod_right $ λ i hir, Hs a aux1 i _ (by { rintro rfl, exact har hir })).mul_dvd
(Hs1 a aux1) (ih (Hs.mono _) $ λ i hi, Hs1 i (finset.mem_insert_of_mem hi)),
{ exact finset.mem_insert_of_mem hir },
{ simp only [finset.coe_insert, set.subset_insert] }
end
theorem fintype.prod_dvd_of_coprime [fintype I] (Hs : pairwise (is_coprime on s))
(Hs1 : ∀ i, s i ∣ z) : ∏ x, s x ∣ z :=
finset.prod_dvd_of_coprime (Hs.pairwise_on _) (λ i _, Hs1 i)
theorem is_coprime.of_mul_left_left (H : is_coprime (x * y) z) : is_coprime x z :=
let ⟨a, b, h⟩ := H in ⟨a * y, b, by rwa [mul_right_comm, mul_assoc]⟩
theorem is_coprime.of_mul_left_right (H : is_coprime (x * y) z) : is_coprime y z :=
by { rw mul_comm at H, exact H.of_mul_left_left }
theorem is_coprime.of_mul_right_left (H : is_coprime x (y * z)) : is_coprime x y :=
by { rw is_coprime_comm at H ⊢, exact H.of_mul_left_left }
theorem is_coprime.of_mul_right_right (H : is_coprime x (y * z)) : is_coprime x z :=
by { rw mul_comm at H, exact H.of_mul_right_left }
theorem is_coprime.mul_left_iff : is_coprime (x * y) z ↔ is_coprime x z ∧ is_coprime y z :=
⟨λ H, ⟨H.of_mul_left_left, H.of_mul_left_right⟩, λ ⟨H1, H2⟩, H1.mul_left H2⟩
theorem is_coprime.mul_right_iff : is_coprime x (y * z) ↔ is_coprime x y ∧ is_coprime x z :=
by rw [is_coprime_comm, is_coprime.mul_left_iff, is_coprime_comm, @is_coprime_comm _ _ z]
theorem is_coprime.prod_left_iff : is_coprime (∏ i in t, s i) x ↔ ∀ i ∈ t, is_coprime (s i) x :=
finset.induction_on t (iff_of_true is_coprime_one_left $ λ _, false.elim) $ λ b t hbt ih,
by rw [finset.prod_insert hbt, is_coprime.mul_left_iff, ih, finset.forall_mem_insert]
theorem is_coprime.prod_right_iff : is_coprime x (∏ i in t, s i) ↔ ∀ i ∈ t, is_coprime x (s i) :=
by simpa only [is_coprime_comm] using is_coprime.prod_left_iff
theorem is_coprime.of_prod_left (H1 : is_coprime (∏ i in t, s i) x) (i : I) (hit : i ∈ t) :
is_coprime (s i) x :=
is_coprime.prod_left_iff.1 H1 i hit
theorem is_coprime.of_prod_right (H1 : is_coprime x (∏ i in t, s i)) (i : I) (hit : i ∈ t) :
is_coprime x (s i) :=
is_coprime.prod_right_iff.1 H1 i hit
variables {m n : ℕ}
theorem is_coprime.pow_left (H : is_coprime x y) : is_coprime (x ^ m) y :=
by { rw [← finset.card_range m, ← finset.prod_const], exact is_coprime.prod_left (λ _ _, H) }
theorem is_coprime.pow_right (H : is_coprime x y) : is_coprime x (y ^ n) :=
by { rw [← finset.card_range n, ← finset.prod_const], exact is_coprime.prod_right (λ _ _, H) }
theorem is_coprime.pow (H : is_coprime x y) : is_coprime (x ^ m) (y ^ n) :=
H.pow_left.pow_right
theorem is_coprime.is_unit_of_dvd (H : is_coprime x y) (d : x ∣ y) : is_unit x :=
let ⟨k, hk⟩ := d in is_coprime_self.1 $ is_coprime.of_mul_right_left $
show is_coprime x (x * k), from hk ▸ H
theorem is_coprime.map (H : is_coprime x y) {S : Type v} [comm_semiring S] (f : R →+* S) :
is_coprime (f x) (f y) :=
let ⟨a, b, h⟩ := H in ⟨f a, f b, by rw [← f.map_mul, ← f.map_mul, ← f.map_add, h, f.map_one]⟩
variables {x y z}
lemma is_coprime.of_add_mul_left_left (h : is_coprime (x + y * z) y) : is_coprime x y :=
let ⟨a, b, H⟩ := h in ⟨a, a * z + b, by simpa only [add_mul, mul_add,
add_assoc, add_comm, add_left_comm, mul_assoc, mul_comm, mul_left_comm] using H⟩
lemma is_coprime.of_add_mul_right_left (h : is_coprime (x + z * y) y) : is_coprime x y :=
by { rw mul_comm at h, exact h.of_add_mul_left_left }
lemma is_coprime.of_add_mul_left_right (h : is_coprime x (y + x * z)) : is_coprime x y :=
by { rw is_coprime_comm at h ⊢, exact h.of_add_mul_left_left }
lemma is_coprime.of_add_mul_right_right (h : is_coprime x (y + z * x)) : is_coprime x y :=
by { rw mul_comm at h, exact h.of_add_mul_left_right }
lemma is_coprime.of_mul_add_left_left (h : is_coprime (y * z + x) y) : is_coprime x y :=
by { rw add_comm at h, exact h.of_add_mul_left_left }
lemma is_coprime.of_mul_add_right_left (h : is_coprime (z * y + x) y) : is_coprime x y :=
by { rw add_comm at h, exact h.of_add_mul_right_left }
lemma is_coprime.of_mul_add_left_right (h : is_coprime x (x * z + y)) : is_coprime x y :=
by { rw add_comm at h, exact h.of_add_mul_left_right }
lemma is_coprime.of_mul_add_right_right (h : is_coprime x (z * x + y)) : is_coprime x y :=
by { rw add_comm at h, exact h.of_add_mul_right_right }
end comm_semiring
namespace is_coprime
section comm_ring
variables {R : Type u} [comm_ring R]
lemma add_mul_left_left {x y : R} (h : is_coprime x y) (z : R) : is_coprime (x + y * z) y :=
@of_add_mul_left_left R _ _ _ (-z) $
by simpa only [mul_neg_eq_neg_mul_symm, add_neg_cancel_right] using h
lemma add_mul_right_left {x y : R} (h : is_coprime x y) (z : R) : is_coprime (x + z * y) y :=
by { rw mul_comm, exact h.add_mul_left_left z }
lemma add_mul_left_right {x y : R} (h : is_coprime x y) (z : R) : is_coprime x (y + x * z) :=
by { rw is_coprime_comm, exact h.symm.add_mul_left_left z }
lemma add_mul_right_right {x y : R} (h : is_coprime x y) (z : R) : is_coprime x (y + z * x) :=
by { rw is_coprime_comm, exact h.symm.add_mul_right_left z }
lemma mul_add_left_left {x y : R} (h : is_coprime x y) (z : R) : is_coprime (y * z + x) y :=
by { rw add_comm, exact h.add_mul_left_left z }
lemma mul_add_right_left {x y : R} (h : is_coprime x y) (z : R) : is_coprime (z * y + x) y :=
by { rw add_comm, exact h.add_mul_right_left z }
lemma mul_add_left_right {x y : R} (h : is_coprime x y) (z : R) : is_coprime x (x * z + y) :=
by { rw add_comm, exact h.add_mul_left_right z }
lemma mul_add_right_right {x y : R} (h : is_coprime x y) (z : R) : is_coprime x (z * x + y) :=
by { rw add_comm, exact h.add_mul_right_right z }
lemma add_mul_left_left_iff {x y z : R} : is_coprime (x + y * z) y ↔ is_coprime x y :=
⟨of_add_mul_left_left, λ h, h.add_mul_left_left z⟩
lemma add_mul_right_left_iff {x y z : R} : is_coprime (x + z * y) y ↔ is_coprime x y :=
⟨of_add_mul_right_left, λ h, h.add_mul_right_left z⟩
lemma add_mul_left_right_iff {x y z : R} : is_coprime x (y + x * z) ↔ is_coprime x y :=
⟨of_add_mul_left_right, λ h, h.add_mul_left_right z⟩
lemma add_mul_right_right_iff {x y z : R} : is_coprime x (y + z * x) ↔ is_coprime x y :=
⟨of_add_mul_right_right, λ h, h.add_mul_right_right z⟩
lemma mul_add_left_left_iff {x y z : R} : is_coprime (y * z + x) y ↔ is_coprime x y :=
⟨of_mul_add_left_left, λ h, h.mul_add_left_left z⟩
lemma mul_add_right_left_iff {x y z : R} : is_coprime (z * y + x) y ↔ is_coprime x y :=
⟨of_mul_add_right_left, λ h, h.mul_add_right_left z⟩
lemma mul_add_left_right_iff {x y z : R} : is_coprime x (x * z + y) ↔ is_coprime x y :=
⟨of_mul_add_left_right, λ h, h.mul_add_left_right z⟩
lemma mul_add_right_right_iff {x y z : R} : is_coprime x (z * x + y) ↔ is_coprime x y :=
⟨of_mul_add_right_right, λ h, h.mul_add_right_right z⟩
end comm_ring
end is_coprime
|
f231b222a24faf6807dcdf0a4e74b42f0203d087 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/hott/614.hlean | 075fb966adc87515b854254c68a04ba945fae1e7 | [
"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 | 593 | hlean | import homotopy.circle
open circle eq int pi
attribute circle.rec [recursor]
protected definition my_decode {x : circle} (c : circle.code x) : base = x :=
begin
induction x,
{ revert c, exact power loop },
{ apply arrow_pathover_left, intro b, apply concato_eq, apply pathover_eq_r,
rewrite [power_con,transport_code_loop]},
end
protected definition my_decode' {x : circle} : circle.code x → base = x :=
begin
induction x,
{ exact power loop},
{ apply arrow_pathover_left, intro b, apply concato_eq, apply pathover_eq_r,
rewrite [power_con,transport_code_loop]},
end
|
59a81f7ee95ba51bfdf15e0786c3f596feb686eb | 07c6143268cfb72beccd1cc35735d424ebcb187b | /src/data/mv_polynomial.lean | 65b46d353e9b56134abdf6bc09b8559924e4bb75 | [
"Apache-2.0"
] | permissive | khoek/mathlib | bc49a842910af13a3c372748310e86467d1dc766 | aa55f8b50354b3e11ba64792dcb06cccb2d8ee28 | refs/heads/master | 1,588,232,063,837 | 1,587,304,803,000 | 1,587,304,803,000 | 176,688,517 | 0 | 0 | Apache-2.0 | 1,553,070,585,000 | 1,553,070,585,000 | null | UTF-8 | Lean | false | false | 56,743 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro, Shing Tak Lam
-/
import algebra.ring
import data.finsupp data.polynomial data.equiv.ring data.equiv.fin
import tactic.omega
/-!
# Multivariate polynomials
This file defines polynomial rings over a base ring (or even semiring),
with variables from a general type `σ` (which could be infinite).
## Important definitions
Let `R` be a commutative ring (or a semiring) and let `σ` be an arbitrary
type. This file creates the type `mv_polynomial σ R`, which mathematicians
might denote `R[X_i : i ∈ σ]`. It is the type of multivariate
(a.k.a. multivariable) polynomials, with variables
corresponding to the terms in `σ`, and coefficients in `R`.
### Notation
In the definitions below, we use the following notation:
+ `σ : Type*` (indexing the variables)
+ `R : Type*` `[comm_semiring R]` (the coefficients)
+ `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set.
This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s`
+ `a : R`
+ `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `p : mv_polynomial σ R`
### Definitions
* `mv_polynomial σ R` : the type of polynomials with variables of type `σ` and coefficients
in the commutative semiring `R`
* `monomial s a` : the monomial which mathematically would be denoted `a * X^s`
* `C a` : the constant polynomial with value `a`
* `X i` : the degree one monomial corresponding to i; mathematically this might be denoted `Xᵢ`.
* `coeff s p` : the coefficient of `s` in `p`.
* `eval₂ (f : R → S) (g : σ → S) p` : given a semiring homomorphism from `R` to another
semiring `S`, and a map `σ → S`, evaluates `p` at this valuation, returning a term of type `S`.
Note that `eval₂` can be made using `eval` and `map` (see below), and it has been suggested
that sticking to `eval` and `map` might make the code less brittle.
* `eval (g : σ → R) p` : given a map `σ → R`, evaluates `p` at this valuation,
returning a term of type `R`
* `map (f : R → S) p` : returns the multivariate polynomial obtained from `p` by the change of
coefficient semiring corresponding to `f`
* `degrees p` : the multiset of variables representing the union of the multisets corresponding
to each non-zero monomial in `p`. For example if `7 ≠ 0` in `R` and `p = x²y+7y³` then
`degrees p = {x, x, y, y, y}`
* `vars p` : the finset of variables occurring in `p`. For example if `p = x⁴y+yz` then
`vars p = {x, y, z}`
* `degree_of n p : ℕ` -- the total degree of `p` with respect to the variable `n`. For example
if `p = x⁴y+yz` then `degree_of y p = 1`.
* `total_degree p : ℕ` -- the max of the sizes of the multisets `s` whose monomials `X^s` occur
in `p`. For example if `p = x⁴y+yz` then `total_degree p = 5`.
* `pderivative i p` : the partial derivative of `p` with respect to `i`.
## Implementation notes
Recall that if `Y` has a zero, then `X →₀ Y` is the type of functions from `X` to `Y` with finite
support, i.e. such that only finitely many elements of `X` get sent to non-zero terms in `Y`.
The definition of `mv_polynomial σ α` is `(σ →₀ ℕ) →₀ α` ; here `σ →₀ ℕ` denotes the space of all
monomials in the variables, and the function to `α` sends a monomial to its coefficient in
the polynomial being represented.
## Tags
polynomial, multivariate polynomial, multivariable polynomial
-/
noncomputable theory
local attribute [instance, priority 100] classical.prop_decidable
open set function finsupp add_monoid_algebra
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x}
/-- Multivariate polynomial, where `σ` is the index set of the variables and
`α` is the coefficient ring -/
def mv_polynomial (σ : Type*) (α : Type*) [comm_semiring α] := add_monoid_algebra α (σ →₀ ℕ)
namespace mv_polynomial
variables {σ : Type*} {a a' a₁ a₂ : α} {e : ℕ} {n m : σ} {s : σ →₀ ℕ}
section comm_semiring
variables [comm_semiring α] {p q : mv_polynomial σ α}
instance decidable_eq_mv_polynomial [decidable_eq σ] [decidable_eq α] :
decidable_eq (mv_polynomial σ α) := finsupp.decidable_eq
instance : comm_semiring (mv_polynomial σ α) := add_monoid_algebra.comm_semiring
instance : inhabited (mv_polynomial σ α) := ⟨0⟩
/-- the coercion turning an `mv_polynomial` into the function which reports the coefficient of a given monomial -/
def coeff_coe_to_fun : has_coe_to_fun (mv_polynomial σ α) :=
finsupp.has_coe_to_fun
local attribute [instance] coeff_coe_to_fun
/-- `monomial s a` is the monomial `a * X^s` -/
def monomial (s : σ →₀ ℕ) (a : α) : mv_polynomial σ α := single s a
/-- `C a` is the constant polynomial with value `a` -/
def C (a : α) : mv_polynomial σ α := monomial 0 a
/-- `X n` is the degree `1` monomial `1*n` -/
def X (n : σ) : mv_polynomial σ α := monomial (single n 1) 1
@[simp] lemma C_0 : C 0 = (0 : mv_polynomial σ α) := by simp [C, monomial]; refl
@[simp] lemma C_1 : C 1 = (1 : mv_polynomial σ α) := rfl
lemma C_mul_monomial : C a * monomial s a' = monomial s (a * a') :=
by simp [C, monomial, single_mul_single]
@[simp] lemma C_add : (C (a + a') : mv_polynomial σ α) = C a + C a' := single_add
@[simp] lemma C_mul : (C (a * a') : mv_polynomial σ α) = C a * C a' := C_mul_monomial.symm
@[simp] lemma C_pow (a : α) (n : ℕ) : (C (a^n) : mv_polynomial σ α) = (C a)^n :=
by induction n; simp [pow_succ, *]
instance : is_semiring_hom (C : α → mv_polynomial σ α) :=
{ map_zero := C_0,
map_one := C_1,
map_add := λ a a', C_add,
map_mul := λ a a', C_mul }
lemma C_eq_coe_nat (n : ℕ) : (C ↑n : mv_polynomial σ α) = n :=
by induction n; simp [nat.succ_eq_add_one, *]
lemma X_pow_eq_single : X n ^ e = monomial (single n e) (1 : α) :=
begin
induction e,
{ simp [X], refl },
{ simp [pow_succ, e_ih],
simp [X, monomial, single_mul_single, nat.succ_eq_add_one, add_comm] }
end
lemma monomial_add_single : monomial (s + single n e) a = (monomial s a * X n ^ e) :=
by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp
lemma monomial_single_add : monomial (single n e + s) a = (X n ^ e * monomial s a) :=
by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp
lemma single_eq_C_mul_X {s : σ} {a : α} {n : ℕ} :
monomial (single s n) a = C a * (X s)^n :=
by rw [← zero_add (single s n), monomial_add_single, C]
@[simp] lemma monomial_add {s : σ →₀ ℕ} {a b : α} :
monomial s a + monomial s b = monomial s (a + b) :=
by simp [monomial]
@[simp] lemma monomial_mul {s s' : σ →₀ ℕ} {a b : α} :
monomial s a * monomial s' b = monomial (s + s') (a * b) :=
by rw [monomial, monomial, monomial, add_monoid_algebra.single_mul_single]
@[simp] lemma monomial_zero {s : σ →₀ ℕ}: monomial s (0 : α) = 0 :=
by rw [monomial, single_zero]; refl
@[simp] lemma sum_monomial {A : Type*} [add_comm_monoid A]
{u : σ →₀ ℕ} {r : α} {b : (σ →₀ ℕ) → α → A} (w : b u 0 = 0) :
sum (monomial u r) b = b u r :=
sum_single_index w
lemma monomial_eq : monomial s a = C a * (s.prod $ λn e, X n ^ e : mv_polynomial σ α) :=
begin
apply @finsupp.induction σ ℕ _ _ s,
{ simp only [C, prod_zero_index]; exact (mul_one _).symm },
{ assume n e s hns he ih,
rw [monomial_single_add, ih, prod_add_index, prod_single_index, mul_left_comm],
{ simp only [pow_zero], },
{ intro a, simp only [pow_zero], },
{ intros, rw pow_add, }, }
end
@[recursor 5]
lemma induction_on {M : mv_polynomial σ α → Prop} (p : mv_polynomial σ α)
(h_C : ∀a, M (C a)) (h_add : ∀p q, M p → M q → M (p + q)) (h_X : ∀p n, M p → M (p * X n)) :
M p :=
have ∀s a, M (monomial s a),
begin
assume s a,
apply @finsupp.induction σ ℕ _ _ s,
{ show M (monomial 0 a), from h_C a, },
{ assume n e p hpn he ih,
have : ∀e:ℕ, M (monomial p a * X n ^ e),
{ intro e,
induction e,
{ simp [ih] },
{ simp [ih, pow_succ', (mul_assoc _ _ _).symm, h_X, e_ih] } },
simp [add_comm, monomial_add_single, this] }
end,
finsupp.induction p
(by have : M (C 0) := h_C 0; rwa [C_0] at this)
(assume s a p hsp ha hp, h_add _ _ (this s a) hp)
theorem induction_on' {P : mv_polynomial σ α → Prop} (p : mv_polynomial σ α)
(h1 : ∀ (u : σ →₀ ℕ) (a : α), P (monomial u a))
(h2 : ∀ (p q : mv_polynomial σ α), P p → P q → P (p + q)) : P p :=
finsupp.induction p (suffices P (monomial 0 0), by rwa monomial_zero at this,
show P (monomial 0 0), from h1 0 0)
(λ a b f ha hb hPf, h2 _ _ (h1 _ _) hPf)
lemma hom_eq_hom [semiring γ]
(f g : mv_polynomial σ α → γ) (hf : is_semiring_hom f) (hg : is_semiring_hom g)
(hC : ∀a:α, f (C a) = g (C a)) (hX : ∀n:σ, f (X n) = g (X n)) (p : mv_polynomial σ α) :
f p = g p :=
mv_polynomial.induction_on p hC
begin assume p q hp hq, rw [is_semiring_hom.map_add f, is_semiring_hom.map_add g, hp, hq] end
begin assume p n hp, rw [is_semiring_hom.map_mul f, is_semiring_hom.map_mul g, hp, hX] end
lemma is_id (f : mv_polynomial σ α → mv_polynomial σ α) (hf : is_semiring_hom f)
(hC : ∀a:α, f (C a) = (C a)) (hX : ∀n:σ, f (X n) = (X n)) (p : mv_polynomial σ α) :
f p = p :=
hom_eq_hom f id hf is_semiring_hom.id hC hX p
section coeff
section
-- While setting up `coeff`, we make `mv_polynomial` reducible so we can treat it as a function.
local attribute [reducible] mv_polynomial
/-- The coefficient of the monomial `m` in the multi-variable polynomial `p`. -/
def coeff (m : σ →₀ ℕ) (p : mv_polynomial σ α) : α := p m
end
lemma ext (p q : mv_polynomial σ α) :
(∀ m, coeff m p = coeff m q) → p = q := ext
lemma ext_iff (p q : mv_polynomial σ α) :
(∀ m, coeff m p = coeff m q) ↔ p = q :=
⟨ext p q, λ h m, by rw h⟩
@[simp] lemma coeff_add (m : σ →₀ ℕ) (p q : mv_polynomial σ α) :
coeff m (p + q) = coeff m p + coeff m q := add_apply
@[simp] lemma coeff_zero (m : σ →₀ ℕ) :
coeff m (0 : mv_polynomial σ α) = 0 := rfl
@[simp] lemma coeff_zero_X (i : σ) : coeff 0 (X i : mv_polynomial σ α) = 0 :=
single_eq_of_ne (λ h, by cases single_eq_zero.1 h)
instance coeff.is_add_monoid_hom (m : σ →₀ ℕ) :
is_add_monoid_hom (coeff m : mv_polynomial σ α → α) :=
{ map_add := coeff_add m,
map_zero := coeff_zero m }
lemma coeff_sum {X : Type*} (s : finset X) (f : X → mv_polynomial σ α) (m : σ →₀ ℕ) :
coeff m (s.sum f) = s.sum (λ x, coeff m (f x)) :=
(s.sum_hom _).symm
lemma monic_monomial_eq (m) : monomial m (1:α) = (m.prod $ λn e, X n ^ e : mv_polynomial σ α) :=
by simp [monomial_eq]
@[simp] lemma coeff_monomial (m n) (a) :
coeff m (monomial n a : mv_polynomial σ α) = if n = m then a else 0 :=
by convert single_apply
@[simp] lemma coeff_C (m) (a) :
coeff m (C a : mv_polynomial σ α) = if 0 = m then a else 0 :=
by convert single_apply
lemma coeff_X_pow (i : σ) (m) (k : ℕ) :
coeff m (X i ^ k : mv_polynomial σ α) = if single i k = m then 1 else 0 :=
begin
have := coeff_monomial m (finsupp.single i k) (1:α),
rwa [@monomial_eq _ _ (1:α) (finsupp.single i k) _,
C_1, one_mul, finsupp.prod_single_index] at this,
exact pow_zero _
end
lemma coeff_X' (i : σ) (m) :
coeff m (X i : mv_polynomial σ α) = if single i 1 = m then 1 else 0 :=
by rw [← coeff_X_pow, pow_one]
@[simp] lemma coeff_X (i : σ) :
coeff (single i 1) (X i : mv_polynomial σ α) = 1 :=
by rw [coeff_X', if_pos rfl]
@[simp] lemma coeff_C_mul (m) (a : α) (p : mv_polynomial σ α) : coeff m (C a * p) = a * coeff m p :=
begin
rw [mul_def, C, monomial],
rw sum_single_index,
{ simp only [zero_add],
convert sum_apply,
simp only [single_apply, finsupp.sum],
rw finset.sum_eq_single m,
{ rw if_pos rfl, refl },
{ intros m' hm' H, apply if_neg, exact H },
{ intros hm, rw if_pos rfl, rw not_mem_support_iff at hm, simp [hm] } },
simp only [zero_mul, single_zero, zero_add],
exact sum_zero, -- TODO doesn't work if we put this inside the simp
end
lemma coeff_mul (p q : mv_polynomial σ α) (n : σ →₀ ℕ) :
coeff n (p * q) = finset.sum (antidiagonal n).support (λ x, coeff x.1 p * coeff x.2 q) :=
begin
rw mul_def,
have := @finset.sum_sigma (σ →₀ ℕ) α _ _ p.support (λ _, q.support)
(λ x, if (x.1 + x.2 = n) then coeff x.1 p * coeff x.2 q else 0),
convert this.symm using 1; clear this,
{ rw [coeff],
repeat {rw sum_apply, apply finset.sum_congr rfl, intros, dsimp only},
convert single_apply },
{ have : (antidiagonal n).support.filter (λ x, x.1 ∈ p.support ∧ x.2 ∈ q.support) ⊆
(antidiagonal n).support := finset.filter_subset _,
rw [← finset.sum_sdiff this, finset.sum_eq_zero, zero_add], swap,
{ intros x hx,
rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter),
not_and, not_and, not_mem_support_iff] at hx,
by_cases H : x.1 ∈ p.support,
{ rw [coeff, coeff, hx.2 hx.1 H, mul_zero] },
{ rw not_mem_support_iff at H, rw [coeff, H, zero_mul] } },
symmetry,
rw [← finset.sum_sdiff (finset.filter_subset _), finset.sum_eq_zero, zero_add], swap,
{ intros x hx,
rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter), not_and] at hx,
rw if_neg,
exact hx.2 hx.1 },
{ apply finset.sum_bij, swap 5,
{ intros x hx, exact (x.1, x.2) },
{ intros x hx, rw [finset.mem_filter, finset.mem_sigma] at hx,
simpa [finset.mem_filter, mem_antidiagonal_support] using hx.symm },
{ intros x hx, rw finset.mem_filter at hx, rw if_pos hx.2 },
{ rintros ⟨i,j⟩ ⟨k,l⟩ hij hkl, simpa using and.intro },
{ rintros ⟨i,j⟩ hij, refine ⟨⟨i,j⟩, _, _⟩, { apply_instance },
{ rw [finset.mem_filter, mem_antidiagonal_support] at hij,
simpa [finset.mem_filter, finset.mem_sigma] using hij.symm },
{ refl } } },
all_goals { apply_instance } }
end
@[simp] lemma coeff_mul_X (m) (s : σ) (p : mv_polynomial σ α) :
coeff (m + single s 1) (p * X s) = coeff m p :=
begin
have : (m, single s 1) ∈ (m + single s 1).antidiagonal.support := mem_antidiagonal_support.2 rfl,
rw [coeff_mul, ← finset.insert_erase this, finset.sum_insert (finset.not_mem_erase _ _),
finset.sum_eq_zero, add_zero, coeff_X, mul_one],
rintros ⟨i,j⟩ hij,
rw [finset.mem_erase, mem_antidiagonal_support] at hij,
by_cases H : single s 1 = j,
{ subst j, simpa using hij },
{ rw [coeff_X', if_neg H, mul_zero] },
end
lemma coeff_mul_X' (m) (s : σ) (p : mv_polynomial σ α) :
coeff m (p * X s) = if s ∈ m.support then coeff (m - single s 1) p else 0 :=
begin
split_ifs with h h,
{ conv_rhs {rw ← coeff_mul_X _ s},
congr' 1, ext t,
by_cases hj : s = t,
{ subst t, simp only [nat_sub_apply, add_apply, single_eq_same],
refine (nat.sub_add_cancel $ nat.pos_of_ne_zero _).symm, rwa mem_support_iff at h },
{ simp [single_eq_of_ne hj] } },
{ delta coeff, rw ← not_mem_support_iff, intro hm, apply h,
have H := support_mul _ _ hm, simp only [finset.mem_bind] at H,
rcases H with ⟨j, hj, i', hi', H⟩,
delta X monomial at hi', rw mem_support_single at hi', cases hi', subst i',
erw finset.mem_singleton at H, subst m,
rw [mem_support_iff, add_apply, single_apply, if_pos rfl],
intro H, rw [_root_.add_eq_zero_iff] at H, exact one_ne_zero H.2 }
end
end coeff
section as_sum
@[simp]
lemma support_sum_monomial_coeff (p : mv_polynomial σ α) : p.support.sum (λ v, monomial v (coeff v p)) = p :=
finsupp.sum_single p
lemma as_sum (p : mv_polynomial σ α) : p = p.support.sum (λ v, monomial v (coeff v p)) :=
(support_sum_monomial_coeff p).symm
end as_sum
section eval₂
variables [comm_semiring β]
variables (f : α → β) (g : σ → β)
/-- Evaluate a polynomial `p` given a valuation `g` of all the variables
and a ring hom `f` from the scalar ring to the target -/
def eval₂ (p : mv_polynomial σ α) : β :=
p.sum (λs a, f a * s.prod (λn e, g n ^ e))
@[simp] lemma eval₂_zero : (0 : mv_polynomial σ α).eval₂ f g = 0 :=
finsupp.sum_zero_index
section
variables [is_semiring_hom f]
@[simp] lemma eval₂_add : (p + q).eval₂ f g = p.eval₂ f g + q.eval₂ f g :=
finsupp.sum_add_index
(by simp [is_semiring_hom.map_zero f])
(by simp [add_mul, is_semiring_hom.map_add f])
@[simp] lemma eval₂_monomial : (monomial s a).eval₂ f g = f a * s.prod (λn e, g n ^ e) :=
finsupp.sum_single_index (by simp [is_semiring_hom.map_zero f])
@[simp] lemma eval₂_C (a) : (C a).eval₂ f g = f a :=
by simp [eval₂_monomial, C, prod_zero_index]
@[simp] lemma eval₂_one : (1 : mv_polynomial σ α).eval₂ f g = 1 :=
(eval₂_C _ _ _).trans (is_semiring_hom.map_one f)
@[simp] lemma eval₂_X (n) : (X n).eval₂ f g = g n :=
by simp [eval₂_monomial,
is_semiring_hom.map_one f, X, prod_single_index, pow_one]
lemma eval₂_mul_monomial :
∀{s a}, (p * monomial s a).eval₂ f g = p.eval₂ f g * f a * s.prod (λn e, g n ^ e) :=
begin
apply mv_polynomial.induction_on p,
{ assume a' s a,
simp [C_mul_monomial, eval₂_monomial, is_semiring_hom.map_mul f] },
{ assume p q ih_p ih_q, simp [add_mul, eval₂_add, ih_p, ih_q] },
{ assume p n ih s a,
from calc (p * X n * monomial s a).eval₂ f g = (p * monomial (single n 1 + s) a).eval₂ f g :
by simp [monomial_single_add, -add_comm, pow_one, mul_assoc]
... = (p * monomial (single n 1) 1).eval₂ f g * f a * s.prod (λn e, g n ^ e) :
by simp [ih, prod_single_index, prod_add_index, pow_one, pow_add, mul_assoc, mul_left_comm,
is_semiring_hom.map_one f, -add_comm] }
end
@[simp] lemma eval₂_mul : ∀{p}, (p * q).eval₂ f g = p.eval₂ f g * q.eval₂ f g :=
begin
apply mv_polynomial.induction_on q,
{ simp [C, eval₂_monomial, eval₂_mul_monomial, prod_zero_index] },
{ simp [mul_add, eval₂_add] {contextual := tt} },
{ simp [X, eval₂_monomial, eval₂_mul_monomial, (mul_assoc _ _ _).symm] { contextual := tt} }
end
@[simp] lemma eval₂_pow {p:mv_polynomial σ α} : ∀{n:ℕ}, (p ^ n).eval₂ f g = (p.eval₂ f g)^n
| 0 := eval₂_one _ _
| (n + 1) := by rw [pow_add, pow_one, pow_add, pow_one, eval₂_mul, eval₂_pow]
instance eval₂.is_semiring_hom : is_semiring_hom (eval₂ f g) :=
{ map_zero := eval₂_zero _ _,
map_one := eval₂_one _ _,
map_add := λ p q, eval₂_add _ _,
map_mul := λ p q, eval₂_mul _ _ }
/-- `mv_polynomial.eval₂` as a `ring_hom`. -/
def eval₂_hom (f : α →+* β) (g : σ → β) : mv_polynomial σ α →+* β := ring_hom.of (eval₂ f g)
@[simp] lemma coe_eval₂_hom (f : α →+* β) (g : σ → β) : ⇑(eval₂_hom f g) = eval₂ f g := rfl
end
section
local attribute [instance, priority 10] is_semiring_hom.comp
lemma eval₂_comp_left {γ} [comm_semiring γ]
(k : β → γ) [is_semiring_hom k]
(f : α → β) [is_semiring_hom f] (g : σ → β)
(p) : k (eval₂ f g p) = eval₂ (k ∘ f) (k ∘ g) p :=
by apply mv_polynomial.induction_on p; simp [
eval₂_add, is_semiring_hom.map_add k,
eval₂_mul, is_semiring_hom.map_mul k] {contextual := tt}
end
@[simp] lemma eval₂_eta (p : mv_polynomial σ α) : eval₂ C X p = p :=
by apply mv_polynomial.induction_on p;
simp [eval₂_add, eval₂_mul] {contextual := tt}
lemma eval₂_congr (g₁ g₂ : σ → β)
(h : ∀ {i : σ} {c : σ →₀ ℕ}, i ∈ c.support → coeff c p ≠ 0 → g₁ i = g₂ i) :
p.eval₂ f g₁ = p.eval₂ f g₂ :=
begin
apply finset.sum_congr rfl,
intros c hc, dsimp, congr' 1,
apply finset.prod_congr rfl,
intros i hi, dsimp, congr' 1,
apply h hi,
rwa finsupp.mem_support_iff at hc
end
variables [is_semiring_hom f]
@[simp] lemma eval₂_prod (s : finset γ) (p : γ → mv_polynomial σ α) :
eval₂ f g (s.prod p) = s.prod (λ x, eval₂ f g $ p x) :=
(s.prod_hom _).symm
@[simp] lemma eval₂_sum (s : finset γ) (p : γ → mv_polynomial σ α) :
eval₂ f g (s.sum p) = s.sum (λ x, eval₂ f g $ p x) :=
(s.sum_hom _).symm
attribute [to_additive] eval₂_prod
lemma eval₂_assoc (q : γ → mv_polynomial σ α) (p : mv_polynomial γ α) :
eval₂ f (λ t, eval₂ f g (q t)) p = eval₂ f g (eval₂ C q p) :=
by { rw eval₂_comp_left (eval₂ f g), congr, funext, simp }
end eval₂
section eval
variables {f : σ → α}
/-- Evaluate a polynomial `p` given a valuation `f` of all the variables -/
def eval (f : σ → α) : mv_polynomial σ α → α := eval₂ id f
@[simp] lemma eval_zero : (0 : mv_polynomial σ α).eval f = 0 := eval₂_zero _ _
@[simp] lemma eval_add : (p + q).eval f = p.eval f + q.eval f := eval₂_add _ _
lemma eval_monomial : (monomial s a).eval f = a * s.prod (λn e, f n ^ e) :=
eval₂_monomial _ _
@[simp] lemma eval_C : ∀ a, (C a).eval f = a := eval₂_C _ _
@[simp] lemma eval_X : ∀ n, (X n).eval f = f n := eval₂_X _ _
@[simp] lemma eval_mul : (p * q).eval f = p.eval f * q.eval f := eval₂_mul _ _
instance eval.is_semiring_hom : is_semiring_hom (eval f) :=
eval₂.is_semiring_hom _ _
theorem eval_assoc {τ}
(f : σ → mv_polynomial τ α) (g : τ → α)
(p : mv_polynomial σ α) :
p.eval (eval g ∘ f) = (eval₂ C f p).eval g :=
begin
rw eval₂_comp_left (eval g),
unfold eval, congr; funext a; simp
end
end eval
section map
variables [comm_semiring β]
variables (f : α → β)
/-- `map f p` maps a polynomial `p` across a ring hom `f` -/
def map : mv_polynomial σ α → mv_polynomial σ β := eval₂ (C ∘ f) X
variables [is_semiring_hom f]
instance is_semiring_hom_C_f :
is_semiring_hom ((C : β → mv_polynomial σ β) ∘ f) :=
is_semiring_hom.comp _ _
@[simp] theorem map_monomial (s : σ →₀ ℕ) (a : α) : map f (monomial s a) = monomial s (f a) :=
(eval₂_monomial _ _).trans monomial_eq.symm
@[simp] theorem map_C : ∀ (a : α), map f (C a : mv_polynomial σ α) = C (f a) := map_monomial _ _
@[simp] theorem map_X : ∀ (n : σ), map f (X n : mv_polynomial σ α) = X n := eval₂_X _ _
@[simp] theorem map_one : map f (1 : mv_polynomial σ α) = 1 := eval₂_one _ _
@[simp] theorem map_add (p q : mv_polynomial σ α) :
map f (p + q) = map f p + map f q := eval₂_add _ _
@[simp] theorem map_mul (p q : mv_polynomial σ α) :
map f (p * q) = map f p * map f q := eval₂_mul _ _
@[simp] lemma map_pow (p : mv_polynomial σ α) (n : ℕ) :
map f (p^n) = (map f p)^n := eval₂_pow _ _
instance map.is_semiring_hom :
is_semiring_hom (map f : mv_polynomial σ α → mv_polynomial σ β) :=
eval₂.is_semiring_hom _ _
theorem map_id : ∀ (p : mv_polynomial σ α), map id p = p := eval₂_eta
theorem map_map [comm_semiring γ]
(g : β → γ) [is_semiring_hom g]
(p : mv_polynomial σ α) :
map g (map f p) = map (g ∘ f) p :=
(eval₂_comp_left (map g) (C ∘ f) X p).trans $
by congr; funext a; simp
theorem eval₂_eq_eval_map (g : σ → β) (p : mv_polynomial σ α) :
p.eval₂ f g = (map f p).eval g :=
begin
unfold map eval,
rw eval₂_comp_left (eval₂ id g),
congr; funext a; simp
end
lemma eval₂_comp_right {γ} [comm_semiring γ]
(k : β → γ) [is_semiring_hom k]
(f : α → β) [is_semiring_hom f] (g : σ → β)
(p) : k (eval₂ f g p) = eval₂ k (k ∘ g) (map f p) :=
begin
apply mv_polynomial.induction_on p,
{ intro r, rw [eval₂_C, map_C, eval₂_C] },
{ intros p q hp hq, rw [eval₂_add, is_semiring_hom.map_add k, map_add, eval₂_add, hp, hq] },
{ intros p s hp,
rw [eval₂_mul, is_semiring_hom.map_mul k, map_mul, eval₂_mul, map_X, hp, eval₂_X, eval₂_X] }
end
lemma map_eval₂ (f : α → β) [is_semiring_hom f] (g : γ → mv_polynomial δ α) (p : mv_polynomial γ α) :
map f (eval₂ C g p) = eval₂ C (map f ∘ g) (map f p) :=
begin
apply mv_polynomial.induction_on p,
{ intro r, rw [eval₂_C, map_C, map_C, eval₂_C] },
{ intros p q hp hq, rw [eval₂_add, map_add, hp, hq, map_add, eval₂_add] },
{ intros p s hp,
rw [eval₂_mul, map_mul, hp, map_mul, map_X, eval₂_mul, eval₂_X, eval₂_X] }
end
lemma coeff_map (p : mv_polynomial σ α) : ∀ (m : σ →₀ ℕ), coeff m (p.map f) = f (coeff m p) :=
begin
apply mv_polynomial.induction_on p; clear p,
{ intros r m, rw [map_C], simp only [coeff_C], split_ifs, {refl}, rw is_semiring_hom.map_zero f },
{ intros p q hp hq m, simp only [hp, hq, map_add, coeff_add], rw is_semiring_hom.map_add f },
{ intros p i hp m, simp only [hp, map_mul, map_X],
simp only [hp, mem_support_iff, coeff_mul_X'],
split_ifs, {refl},
rw is_semiring_hom.map_zero f }
end
lemma map_injective (hf : function.injective f) :
function.injective (map f : mv_polynomial σ α → mv_polynomial σ β) :=
λ p q h, ext _ _ $ λ m, hf $
begin
rw ← ext_iff at h,
specialize h m,
rw [coeff_map, coeff_map] at h,
exact h
end
end map
section degrees
section comm_semiring
/--
The maximal degrees of each variable in a multi-variable polynomial, expressed as a multiset.
(For example, `degrees (x^2 * y + y^3)` would be `{x, x, y, y, y}`.)
-/
def degrees (p : mv_polynomial σ α) : multiset σ :=
p.support.sup (λs:σ →₀ ℕ, s.to_multiset)
lemma degrees_monomial (s : σ →₀ ℕ) (a : α) : degrees (monomial s a) ≤ s.to_multiset :=
finset.sup_le $ assume t h,
begin
have := finsupp.support_single_subset h,
rw [finset.singleton_eq_singleton, finset.mem_singleton] at this,
rw this
end
lemma degrees_monomial_eq (s : σ →₀ ℕ) (a : α) (ha : a ≠ 0) :
degrees (monomial s a) = s.to_multiset :=
le_antisymm (degrees_monomial s a) $ finset.le_sup $
by rw [monomial, finsupp.support_single_ne_zero ha,
finset.singleton_eq_singleton, finset.mem_singleton]
lemma degrees_C (a : α) : degrees (C a : mv_polynomial σ α) = 0 :=
multiset.le_zero.1 $ degrees_monomial _ _
lemma degrees_X (n : σ) : degrees (X n : mv_polynomial σ α) ≤ {n} :=
le_trans (degrees_monomial _ _) $ le_of_eq $ to_multiset_single _ _
lemma degrees_zero : degrees (0 : mv_polynomial σ α) = 0 :=
by { rw ← C_0, exact degrees_C 0 }
lemma degrees_one : degrees (1 : mv_polynomial σ α) = 0 := degrees_C 1
lemma degrees_add (p q : mv_polynomial σ α) : (p + q).degrees ≤ p.degrees ⊔ q.degrees :=
begin
refine finset.sup_le (assume b hb, _),
have := finsupp.support_add hb, rw finset.mem_union at this,
cases this,
{ exact le_sup_left_of_le (finset.le_sup this) },
{ exact le_sup_right_of_le (finset.le_sup this) },
end
lemma degrees_sum {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) :
(s.sum f).degrees ≤ s.sup (λi, (f i).degrees) :=
begin
refine s.induction _ _,
{ simp only [finset.sum_empty, finset.sup_empty, degrees_zero], exact le_refl _ },
{ assume i s his ih,
rw [finset.sup_insert, finset.sum_insert his],
exact le_trans (degrees_add _ _) (sup_le_sup_left ih _) }
end
lemma degrees_mul (p q : mv_polynomial σ α) : (p * q).degrees ≤ p.degrees + q.degrees :=
begin
refine finset.sup_le (assume b hb, _),
have := support_mul p q hb,
simp only [finset.mem_bind, finset.singleton_eq_singleton, finset.mem_singleton] at this,
rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩,
rw [finsupp.to_multiset_add],
exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂)
end
lemma degrees_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) :
(s.prod f).degrees ≤ s.sum (λi, (f i).degrees) :=
begin
refine s.induction _ _,
{ simp only [finset.prod_empty, finset.sum_empty, degrees_one] },
{ assume i s his ih,
rw [finset.prod_insert his, finset.sum_insert his],
exact le_trans (degrees_mul _ _) (add_le_add_left ih _) }
end
lemma degrees_pow (p : mv_polynomial σ α) :
∀(n : ℕ), (p^n).degrees ≤ add_monoid.smul n p.degrees
| 0 := begin rw [pow_zero, degrees_one], exact multiset.zero_le _ end
| (n + 1) := le_trans (degrees_mul _ _) (add_le_add_left (degrees_pow n) _)
end comm_semiring
end degrees
section vars
/-- `vars p` is the set of variables appearing in the polynomial `p` -/
def vars (p : mv_polynomial σ α) : finset σ := p.degrees.to_finset
@[simp] lemma vars_0 : (0 : mv_polynomial σ α).vars = ∅ :=
by rw [vars, degrees_zero, multiset.to_finset_zero]
@[simp] lemma vars_monomial (h : a ≠ 0) : (monomial s a).vars = s.support :=
by rw [vars, degrees_monomial_eq _ _ h, finsupp.to_finset_to_multiset]
@[simp] lemma vars_C : (C a : mv_polynomial σ α).vars = ∅ :=
by rw [vars, degrees_C, multiset.to_finset_zero]
@[simp] lemma vars_X (h : 0 ≠ (1 : α)) : (X n : mv_polynomial σ α).vars = {n} :=
by rw [X, vars_monomial h.symm, finsupp.support_single_ne_zero zero_ne_one.symm]
lemma mem_support_not_mem_vars_zero {f : mv_polynomial σ α} {x : σ →₀ ℕ} (H : x ∈ f.support) {v : σ} (h : v ∉ vars f) :
x v = 0 :=
begin
rw [vars, multiset.mem_to_finset] at h,
rw ←not_mem_support_iff,
contrapose! h,
unfold degrees,
rw (show f.support = insert x f.support, from eq.symm $ finset.insert_eq_of_mem H),
rw finset.sup_insert,
simp only [multiset.mem_union, multiset.sup_eq_union],
left,
rwa [←to_finset_to_multiset, multiset.mem_to_finset] at h,
end
end vars
section degree_of
/-- `degree_of n p` gives the highest power of X_n that appears in `p` -/
def degree_of (n : σ) (p : mv_polynomial σ α) : ℕ := p.degrees.count n
end degree_of
section total_degree
/-- `total_degree p` gives the maximum |s| over the monomials X^s in `p` -/
def total_degree (p : mv_polynomial σ α) : ℕ := p.support.sup (λs, s.sum $ λn e, e)
lemma total_degree_eq (p : mv_polynomial σ α) :
p.total_degree = p.support.sup (λm, m.to_multiset.card) :=
begin
rw [total_degree],
congr, funext m,
exact (finsupp.card_to_multiset _).symm
end
lemma total_degree_le_degrees_card (p : mv_polynomial σ α) :
p.total_degree ≤ p.degrees.card :=
begin
rw [total_degree_eq],
exact finset.sup_le (assume s hs, multiset.card_le_of_le $ finset.le_sup hs)
end
lemma total_degree_C (a : α) : (C a : mv_polynomial σ α).total_degree = 0 :=
nat.eq_zero_of_le_zero $ finset.sup_le $ assume n hn,
have _ := finsupp.support_single_subset hn,
begin
rw [finset.singleton_eq_singleton, finset.mem_singleton] at this,
subst this,
exact le_refl _
end
lemma total_degree_zero : (0 : mv_polynomial σ α).total_degree = 0 :=
by rw [← C_0]; exact total_degree_C (0 : α)
lemma total_degree_one : (1 : mv_polynomial σ α).total_degree = 0 :=
total_degree_C (1 : α)
lemma total_degree_add (a b : mv_polynomial σ α) :
(a + b).total_degree ≤ max a.total_degree b.total_degree :=
finset.sup_le $ assume n hn,
have _ := finsupp.support_add hn,
begin
rw finset.mem_union at this,
cases this,
{ exact le_max_left_of_le (finset.le_sup this) },
{ exact le_max_right_of_le (finset.le_sup this) }
end
lemma total_degree_mul (a b : mv_polynomial σ α) :
(a * b).total_degree ≤ a.total_degree + b.total_degree :=
finset.sup_le $ assume n hn,
have _ := add_monoid_algebra.support_mul a b hn,
begin
simp only [finset.mem_bind, finset.mem_singleton, finset.singleton_eq_singleton] at this,
rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩,
rw [finsupp.sum_add_index],
{ exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) },
{ assume a, refl },
{ assume a b₁ b₂, refl }
end
lemma total_degree_list_prod :
∀(s : list (mv_polynomial σ α)), s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum
| [] := by rw [@list.prod_nil (mv_polynomial σ α) _, total_degree_one]; refl
| (p :: ps) :=
begin
rw [@list.prod_cons (mv_polynomial σ α) _, list.map, list.sum_cons],
exact le_trans (total_degree_mul _ _) (add_le_add_left (total_degree_list_prod ps) _)
end
lemma total_degree_multiset_prod (s : multiset (mv_polynomial σ α)) :
s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum :=
begin
refine quotient.induction_on s (assume l, _),
rw [multiset.quot_mk_to_coe, multiset.coe_prod, multiset.coe_map, multiset.coe_sum],
exact total_degree_list_prod l
end
lemma total_degree_finset_prod {ι : Type*}
(s : finset ι) (f : ι → mv_polynomial σ α) :
(s.prod f).total_degree ≤ s.sum (λi, (f i).total_degree) :=
begin
refine le_trans (total_degree_multiset_prod _) _,
rw [multiset.map_map],
refl
end
end total_degree
end comm_semiring
section comm_ring
variable [comm_ring α]
variables {p q : mv_polynomial σ α}
instance : comm_ring (mv_polynomial σ α) := add_monoid_algebra.comm_ring
instance : has_scalar α (mv_polynomial σ α) := finsupp.has_scalar
instance : module α (mv_polynomial σ α) := finsupp.module (σ →₀ ℕ) α
instance C.is_ring_hom : is_ring_hom (C : α → mv_polynomial σ α) :=
by apply is_ring_hom.of_semiring
variables (σ a a')
@[simp] lemma C_sub : (C (a - a') : mv_polynomial σ α) = C a - C a' := is_ring_hom.map_sub _
@[simp] lemma C_neg : (C (-a) : mv_polynomial σ α) = -C a := is_ring_hom.map_neg _
@[simp] lemma coeff_neg (m : σ →₀ ℕ) (p : mv_polynomial σ α) :
coeff m (-p) = -coeff m p := finsupp.neg_apply
@[simp] lemma coeff_sub (m : σ →₀ ℕ) (p q : mv_polynomial σ α) :
coeff m (p - q) = coeff m p - coeff m q := finsupp.sub_apply
instance coeff.is_add_group_hom (m : σ →₀ ℕ) :
is_add_group_hom (coeff m : mv_polynomial σ α → α) :=
{ map_add := coeff_add m }
variables {σ} (p)
theorem C_mul' : mv_polynomial.C a * p = a • p :=
begin
apply finsupp.induction p,
{ exact (mul_zero $ mv_polynomial.C a).trans (@smul_zero α (mv_polynomial σ α) _ _ _ a).symm },
intros p b f haf hb0 ih,
rw [mul_add, ih, @smul_add α (mv_polynomial σ α) _ _ _ a], congr' 1,
rw [add_monoid_algebra.mul_def, finsupp.smul_single, mv_polynomial.C, mv_polynomial.monomial],
rw [finsupp.sum_single_index, finsupp.sum_single_index, zero_add, smul_eq_mul],
{ rw [mul_zero, finsupp.single_zero] },
{ rw finsupp.sum_single_index,
all_goals { rw [zero_mul, finsupp.single_zero] }, }
end
lemma smul_eq_C_mul (p : mv_polynomial σ α) (a : α) : a • p = C a * p :=
begin
rw [← finsupp.sum_single p, @finsupp.smul_sum (σ →₀ ℕ) α α, finsupp.mul_sum],
refine finset.sum_congr rfl (assume n _, _),
simp only [finsupp.smul_single],
exact C_mul_monomial.symm
end
@[simp] lemma smul_eval (x) (p : mv_polynomial σ α) (s) : (s • p).eval x = s * p.eval x :=
by rw [smul_eq_C_mul, eval_mul, eval_C]
section degrees
lemma degrees_neg (p : mv_polynomial σ α) : (- p).degrees = p.degrees :=
by rw [degrees, finsupp.support_neg]; refl
lemma degrees_sub (p q : mv_polynomial σ α) :
(p - q).degrees ≤ p.degrees ⊔ q.degrees :=
le_trans (degrees_add p (-q)) $ by rw [degrees_neg]
end degrees
section eval₂
variables [comm_ring β]
variables (f : α → β) [is_ring_hom f] (g : σ → β)
instance eval₂.is_ring_hom : is_ring_hom (eval₂ f g) :=
by apply is_ring_hom.of_semiring
@[simp] lemma eval₂_sub : (p - q).eval₂ f g = p.eval₂ f g - q.eval₂ f g := is_ring_hom.map_sub _
@[simp] lemma eval₂_neg : (-p).eval₂ f g = -(p.eval₂ f g) := is_ring_hom.map_neg _
lemma hom_C (f : mv_polynomial σ ℤ → β) [is_ring_hom f] (n : ℤ) : f (C n) = (n : β) :=
((ring_hom.of f).comp (ring_hom.of C)).eq_int_cast n
/-- A ring homomorphism f : Z[X_1, X_2, ...] → R
is determined by the evaluations f(X_1), f(X_2), ... -/
@[simp] lemma eval₂_hom_X {α : Type u} (c : ℤ → β) [is_ring_hom c]
(f : mv_polynomial α ℤ → β) [is_ring_hom f] (x : mv_polynomial α ℤ) :
eval₂ c (f ∘ X) x = f x :=
mv_polynomial.induction_on x
(λ n, by { rw [hom_C f, eval₂_C], exact (ring_hom.of c).eq_int_cast n })
(λ p q hp hq, by { rw [eval₂_add, hp, hq], exact (is_ring_hom.map_add f).symm })
(λ p n hp, by { rw [eval₂_mul, eval₂_X, hp], exact (is_ring_hom.map_mul f).symm })
/-- Ring homomorphisms out of integer polynomials on a type `σ` are the same as
functions out of the type `σ`, -/
def hom_equiv : (mv_polynomial σ ℤ →+* β) ≃ (σ → β) :=
{ to_fun := λ f, ⇑f ∘ X,
inv_fun := λ f, eval₂_hom (int.cast_ring_hom β) f,
left_inv := λ f, ring_hom.ext $ eval₂_hom_X _ _,
right_inv := λ f, funext $ λ x, by simp only [coe_eval₂_hom, function.comp_app, eval₂_X] }
end eval₂
section eval
variables (f : σ → α)
instance eval.is_ring_hom : is_ring_hom (eval f) := eval₂.is_ring_hom _ _
@[simp] lemma eval_sub : (p - q).eval f = p.eval f - q.eval f := is_ring_hom.map_sub _
@[simp] lemma eval_neg : (-p).eval f = -(p.eval f) := is_ring_hom.map_neg _
end eval
section map
variables [comm_ring β]
variables (f : α → β) [is_ring_hom f]
instance is_ring_hom_C_f : is_ring_hom ((C : β → mv_polynomial σ β) ∘ f) :=
is_ring_hom.comp _ _
instance map.is_ring_hom : is_ring_hom (map f : mv_polynomial σ α → mv_polynomial σ β) :=
eval₂.is_ring_hom _ _
@[simp] lemma map_sub : (p - q).map f = p.map f - q.map f := is_ring_hom.map_sub _
@[simp] lemma map_neg : (-p).map f = -(p.map f) := is_ring_hom.map_neg _
end map
section aeval
/-- The algebra of multivariate polynomials. -/
-- FIXME this causes a deterministic timeout with `-T50000` (but `-T60000` seems okay)
instance mv_polynomial (R : Type u) [comm_ring R] (σ : Type v) : algebra R (mv_polynomial σ R) :=
{ commutes' := λ _ _, mul_comm _ _,
smul_def' := λ c p, (mv_polynomial.C_mul' c p).symm,
.. ring_hom.of mv_polynomial.C, .. mv_polynomial.module }
variables (R : Type u) (A : Type v) (f : σ → A)
variables [comm_ring R] [comm_ring A] [algebra R A]
/-- A map `σ → A` where `A` is an algebra over `R` generates an `R`-algebra homomorphism
from multivariate polynomials over `σ` to `A`. -/
def aeval : mv_polynomial σ R →ₐ[R] A :=
{ commutes' := λ r, eval₂_C _ _ _
.. eval₂_hom (algebra_map R A) f }
theorem aeval_def (p : mv_polynomial σ R) : aeval R A f p = eval₂ (algebra_map R A) f p := rfl
@[simp] lemma aeval_X (s : σ) : aeval R A f (X s) = f s := eval₂_X _ _ _
@[simp] lemma aeval_C (r : R) : aeval R A f (C r) = algebra_map R A r := eval₂_C _ _ _
theorem eval_unique (φ : mv_polynomial σ R →ₐ[R] A) :
φ = aeval R A (φ ∘ X) :=
begin
ext p,
apply mv_polynomial.induction_on p,
{ intro r, rw aeval_C, exact φ.commutes r },
{ intros f g ih1 ih2,
rw [φ.map_add, ih1, ih2, alg_hom.map_add] },
{ intros p j ih,
rw [φ.map_mul, alg_hom.map_mul, aeval_X, ih] }
end
end aeval
end comm_ring
section rename
variables {α} [comm_semiring α]
/-- Rename all the variables in a multivariable polynomial. -/
def rename (f : β → γ) : mv_polynomial β α → mv_polynomial γ α :=
eval₂ C (X ∘ f)
instance rename.is_semiring_hom (f : β → γ) :
is_semiring_hom (rename f : mv_polynomial β α → mv_polynomial γ α) :=
by unfold rename; apply_instance
@[simp] lemma rename_C (f : β → γ) (a : α) : rename f (C a) = C a :=
eval₂_C _ _ _
@[simp] lemma rename_X (f : β → γ) (b : β) : rename f (X b : mv_polynomial β α) = X (f b) :=
eval₂_X _ _ _
@[simp] lemma rename_zero (f : β → γ) :
rename f (0 : mv_polynomial β α) = 0 :=
eval₂_zero _ _
@[simp] lemma rename_one (f : β → γ) :
rename f (1 : mv_polynomial β α) = 1 :=
eval₂_one _ _
@[simp] lemma rename_add (f : β → γ) (p q : mv_polynomial β α) :
rename f (p + q) = rename f p + rename f q :=
eval₂_add _ _
@[simp] lemma rename_neg {α} [comm_ring α]
(f : β → γ) (p : mv_polynomial β α) :
rename f (-p) = -rename f p :=
eval₂_neg _ _ _
@[simp] lemma rename_sub {α} [comm_ring α]
(f : β → γ) (p q : mv_polynomial β α) :
rename f (p - q) = rename f p - rename f q :=
eval₂_sub _ _ _
@[simp] lemma rename_mul (f : β → γ) (p q : mv_polynomial β α) :
rename f (p * q) = rename f p * rename f q :=
eval₂_mul _ _
@[simp] lemma rename_pow (f : β → γ) (p : mv_polynomial β α) (n : ℕ) :
rename f (p^n) = (rename f p)^n :=
eval₂_pow _ _
lemma map_rename [comm_semiring β] (f : α → β) [is_semiring_hom f]
(g : γ → δ) (p : mv_polynomial γ α) :
map f (rename g p) = rename g (map f p) :=
mv_polynomial.induction_on p
(λ a, by simp)
(λ p q hp hq, by simp [hp, hq])
(λ p n hp, by simp [hp])
@[simp] lemma rename_rename (f : β → γ) (g : γ → δ) (p : mv_polynomial β α) :
rename g (rename f p) = rename (g ∘ f) p :=
show rename g (eval₂ C (X ∘ f) p) = _,
by simp only [eval₂_comp_left (rename g) C (X ∘ f) p, (∘), rename_C, rename_X]; refl
@[simp] lemma rename_id (p : mv_polynomial β α) : rename id p = p :=
eval₂_eta p
lemma rename_monomial (f : β → γ) (p : β →₀ ℕ) (a : α) :
rename f (monomial p a) = monomial (p.map_domain f) a :=
begin
rw [rename, eval₂_monomial, monomial_eq, finsupp.prod_map_domain_index],
{ exact assume n, pow_zero _ },
{ exact assume n i₁ i₂, pow_add _ _ _ }
end
lemma rename_eq (f : β → γ) (p : mv_polynomial β α) :
rename f p = finsupp.map_domain (finsupp.map_domain f) p :=
begin
simp only [rename, eval₂, finsupp.map_domain],
congr, ext s a : 2,
rw [← monomial, monomial_eq, finsupp.prod_sum_index],
congr, ext n i : 2,
rw [finsupp.prod_single_index],
exact pow_zero _,
exact assume a, pow_zero _,
exact assume a b c, pow_add _ _ _
end
lemma injective_rename (f : β → γ) (hf : function.injective f) :
function.injective (rename f : mv_polynomial β α → mv_polynomial γ α) :=
have (rename f : mv_polynomial β α → mv_polynomial γ α) =
finsupp.map_domain (finsupp.map_domain f) := funext (rename_eq f),
begin
rw this,
exact finsupp.injective_map_domain (finsupp.injective_map_domain hf)
end
lemma total_degree_rename_le (f : β → γ) (p : mv_polynomial β α) :
(p.rename f).total_degree ≤ p.total_degree :=
finset.sup_le $ assume b,
begin
assume h,
rw rename_eq at h,
have h' := finsupp.map_domain_support h,
rw finset.mem_image at h',
rcases h' with ⟨s, hs, rfl⟩,
rw finsupp.sum_map_domain_index,
exact le_trans (le_refl _) (finset.le_sup hs),
exact assume _, rfl,
exact assume _ _ _, rfl
end
section
variables [comm_semiring β] (f : α → β) [is_semiring_hom f]
variables (k : γ → δ) (g : δ → β) (p : mv_polynomial γ α)
lemma eval₂_rename : (p.rename k).eval₂ f g = p.eval₂ f (g ∘ k) :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma rename_eval₂ (g : δ → mv_polynomial γ α) :
(p.eval₂ C (g ∘ k)).rename k = (p.rename k).eval₂ C (rename k ∘ g) :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma rename_prodmk_eval₂ (d : δ) (g : γ → mv_polynomial γ α) :
(p.eval₂ C g).rename (prod.mk d) = p.eval₂ C (λ x, (g x).rename (prod.mk d)) :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma eval₂_rename_prodmk (g : δ × γ → β) (d : δ) :
(rename (prod.mk d) p).eval₂ f g = eval₂ f (λ i, g (d, i)) p :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma eval_rename_prodmk (g : δ × γ → α) (d : δ) :
(rename (prod.mk d) p).eval g = eval (λ i, g (d, i)) p :=
eval₂_rename_prodmk id _ _ _
end
/-- Every polynomial is a polynomial in finitely many variables. -/
theorem exists_finset_rename (p : mv_polynomial γ α) :
∃ (s : finset γ) (q : mv_polynomial {x // x ∈ s} α), p = q.rename coe :=
begin
apply induction_on p,
{ intro r, exact ⟨∅, C r, by rw rename_C⟩ },
{ rintro p q ⟨s, p, rfl⟩ ⟨t, q, rfl⟩,
refine ⟨s ∪ t, ⟨_, _⟩⟩,
{ exact rename (subtype.map id (finset.subset_union_left s t)) p +
rename (subtype.map id (finset.subset_union_right s t)) q },
{ rw [rename_add, rename_rename, rename_rename], refl } },
{ rintro p n ⟨s, p, rfl⟩,
refine ⟨insert n s, ⟨_, _⟩⟩,
{ exact rename (subtype.map id (finset.subset_insert n s)) p * X ⟨n, s.mem_insert_self n⟩ },
{ rw [rename_mul, rename_X, rename_rename], refl } }
end
/-- Every polynomial is a polynomial in finitely many variables. -/
theorem exists_fin_rename (p : mv_polynomial γ α) :
∃ (n : ℕ) (f : fin n → γ) (hf : injective f) (q : mv_polynomial (fin n) α), p = q.rename f :=
begin
obtain ⟨s, q, rfl⟩ := exists_finset_rename p,
obtain ⟨n, ⟨e⟩⟩ := fintype.exists_equiv_fin {x // x ∈ s},
refine ⟨n, coe ∘ e.symm, injective_comp subtype.val_injective e.symm.injective, q.rename e, _⟩,
rw [← rename_rename, rename_rename e],
simp only [function.comp, equiv.symm_apply_apply, rename_rename]
end
end rename
lemma eval₂_cast_comp {β : Type u} {γ : Type v} (f : γ → β)
{α : Type w} [comm_ring α] (c : ℤ → α) [is_ring_hom c] (g : β → α) (x : mv_polynomial γ ℤ) :
eval₂ c (g ∘ f) x = eval₂ c g (rename f x) :=
mv_polynomial.induction_on x
(λ n, by simp only [eval₂_C, rename_C])
(λ p q hp hq, by simp only [hp, hq, rename, eval₂_add])
(λ p n hp, by simp only [hp, rename, eval₂_X, eval₂_mul])
instance rename.is_ring_hom
{α} [comm_ring α] (f : β → γ) :
is_ring_hom (rename f : mv_polynomial β α → mv_polynomial γ α) :=
@is_ring_hom.of_semiring (mv_polynomial β α) (mv_polynomial γ α) _ _ (rename f)
(rename.is_semiring_hom f)
section equiv
variables (α) [comm_semiring α]
/-- The ring isomorphism between multivariable polynomials in no variables and the ground ring. -/
def pempty_ring_equiv : mv_polynomial pempty α ≃+* α :=
{ to_fun := mv_polynomial.eval₂ id $ pempty.elim,
inv_fun := C,
left_inv := is_id _ (by apply_instance) (assume a, by rw [eval₂_C]; refl) (assume a, a.elim),
right_inv := λ r, eval₂_C _ _ _,
map_mul' := λ _ _, eval₂_mul _ _,
map_add' := λ _ _, eval₂_add _ _ }
/--
The ring isomorphism between multivariable polynomials in a single variable and
polynomials over the ground ring.
-/
def punit_ring_equiv : mv_polynomial punit α ≃+* polynomial α :=
{ to_fun := eval₂ polynomial.C (λu:punit, polynomial.X),
inv_fun := polynomial.eval₂ mv_polynomial.C (X punit.star),
left_inv :=
begin
refine is_id _ _ _ _,
apply is_semiring_hom.comp (eval₂ polynomial.C (λu:punit, polynomial.X)) _; apply_instance,
{ assume a, rw [eval₂_C, polynomial.eval₂_C] },
{ rintros ⟨⟩, rw [eval₂_X, polynomial.eval₂_X] }
end,
right_inv := assume p, polynomial.induction_on p
(assume a, by rw [polynomial.eval₂_C, mv_polynomial.eval₂_C])
(assume p q hp hq, by rw [polynomial.eval₂_add, mv_polynomial.eval₂_add, hp, hq])
(assume p n hp,
by rw [polynomial.eval₂_mul, polynomial.eval₂_pow, polynomial.eval₂_X, polynomial.eval₂_C,
eval₂_mul, eval₂_C, eval₂_pow, eval₂_X]),
map_mul' := λ _ _, eval₂_mul _ _,
map_add' := λ _ _, eval₂_add _ _ }
/-- The ring isomorphism between multivariable polynomials induced by an equivalence of the variables. -/
def ring_equiv_of_equiv (e : β ≃ γ) : mv_polynomial β α ≃+* mv_polynomial γ α :=
{ to_fun := rename e,
inv_fun := rename e.symm,
left_inv := λ p, by simp only [rename_rename, (∘), e.symm_apply_apply]; exact rename_id p,
right_inv := λ p, by simp only [rename_rename, (∘), e.apply_symm_apply]; exact rename_id p,
map_mul' := rename_mul e,
map_add' := rename_add e }
/-- The ring isomorphism between multivariable polynomials induced by a ring isomorphism of the ground ring. -/
def ring_equiv_congr [comm_semiring γ] (e : α ≃+* γ) : mv_polynomial β α ≃+* mv_polynomial β γ :=
{ to_fun := map e,
inv_fun := map e.symm,
left_inv := assume p,
have (e.symm ∘ e) = id,
{ ext a, exact e.symm_apply_apply a },
by simp only [map_map, this, map_id],
right_inv := assume p,
have (e ∘ e.symm) = id,
{ ext a, exact e.apply_symm_apply a },
by simp only [map_map, this, map_id],
map_mul' := map_mul _,
map_add' := map_add _ }
section
variables (β γ δ)
/--
The function from multivariable polynomials in a sum of two types,
to multivariable polynomials in one of the types,
with coefficents in multivariable polynomials in the other type.
See `sum_ring_equiv` for the ring isomorphism.
-/
def sum_to_iter : mv_polynomial (β ⊕ γ) α → mv_polynomial β (mv_polynomial γ α) :=
eval₂ (C ∘ C) (λbc, sum.rec_on bc X (C ∘ X))
instance is_semiring_hom_C_C :
is_semiring_hom (C ∘ C : α → mv_polynomial β (mv_polynomial γ α)) :=
@is_semiring_hom.comp _ _ _ _ C mv_polynomial.is_semiring_hom _ _ C mv_polynomial.is_semiring_hom
instance is_semiring_hom_sum_to_iter : is_semiring_hom (sum_to_iter α β γ) :=
eval₂.is_semiring_hom _ _
lemma sum_to_iter_C (a : α) : sum_to_iter α β γ (C a) = C (C a) :=
eval₂_C _ _ a
lemma sum_to_iter_Xl (b : β) : sum_to_iter α β γ (X (sum.inl b)) = X b :=
eval₂_X _ _ (sum.inl b)
lemma sum_to_iter_Xr (c : γ) : sum_to_iter α β γ (X (sum.inr c)) = C (X c) :=
eval₂_X _ _ (sum.inr c)
/--
The function from multivariable polynomials in one type,
with coefficents in multivariable polynomials in another type,
to multivariable polynomials in the sum of the two types.
See `sum_ring_equiv` for the ring isomorphism.
-/
def iter_to_sum : mv_polynomial β (mv_polynomial γ α) → mv_polynomial (β ⊕ γ) α :=
eval₂ (eval₂ C (X ∘ sum.inr)) (X ∘ sum.inl)
instance is_semiring_hom_iter_to_sum : is_semiring_hom (iter_to_sum α β γ) :=
eval₂.is_semiring_hom _ _
lemma iter_to_sum_C_C (a : α) : iter_to_sum α β γ (C (C a)) = C a :=
eq.trans (eval₂_C _ _ (C a)) (eval₂_C _ _ _)
lemma iter_to_sum_X (b : β) : iter_to_sum α β γ (X b) = X (sum.inl b) :=
eval₂_X _ _ _
lemma iter_to_sum_C_X (c : γ) : iter_to_sum α β γ (C (X c)) = X (sum.inr c) :=
eq.trans (eval₂_C _ _ (X c)) (eval₂_X _ _ _)
/-- A helper function for `sum_ring_equiv`. -/
def mv_polynomial_equiv_mv_polynomial [comm_semiring δ]
(f : mv_polynomial β α → mv_polynomial γ δ) (hf : is_semiring_hom f)
(g : mv_polynomial γ δ → mv_polynomial β α) (hg : is_semiring_hom g)
(hfgC : ∀a, f (g (C a)) = C a)
(hfgX : ∀n, f (g (X n)) = X n)
(hgfC : ∀a, g (f (C a)) = C a)
(hgfX : ∀n, g (f (X n)) = X n) :
mv_polynomial β α ≃+* mv_polynomial γ δ :=
{ to_fun := f, inv_fun := g,
left_inv := is_id _ (is_semiring_hom.comp _ _) hgfC hgfX,
right_inv := is_id _ (is_semiring_hom.comp _ _) hfgC hfgX,
map_mul' := hf.map_mul,
map_add' := hf.map_add }
/--
The ring isomorphism between multivariable polynomials in a sum of two types,
and multivariable polynomials in one of the types,
with coefficents in multivariable polynomials in the other type.
-/
def sum_ring_equiv : mv_polynomial (β ⊕ γ) α ≃+* mv_polynomial β (mv_polynomial γ α) :=
begin
apply @mv_polynomial_equiv_mv_polynomial α (β ⊕ γ) _ _ _ _
(sum_to_iter α β γ) _ (iter_to_sum α β γ) _,
{ assume p,
apply hom_eq_hom _ _ _ _ _ _ p,
apply_instance,
{ apply @is_semiring_hom.comp _ _ _ _ _ _ _ _ _ _,
apply_instance,
apply @is_semiring_hom.comp _ _ _ _ _ _ _ _ _ _,
apply_instance,
{ apply @mv_polynomial.is_semiring_hom },
{ apply mv_polynomial.is_semiring_hom_iter_to_sum α β γ },
{ apply mv_polynomial.is_semiring_hom_sum_to_iter α β γ } },
{ apply mv_polynomial.is_semiring_hom },
{ assume a, rw [iter_to_sum_C_C α β γ, sum_to_iter_C α β γ] },
{ assume c, rw [iter_to_sum_C_X α β γ, sum_to_iter_Xr α β γ] } },
{ assume b, rw [iter_to_sum_X α β γ, sum_to_iter_Xl α β γ] },
{ assume a, rw [sum_to_iter_C α β γ, iter_to_sum_C_C α β γ] },
{ assume n, cases n with b c,
{ rw [sum_to_iter_Xl, iter_to_sum_X] },
{ rw [sum_to_iter_Xr, iter_to_sum_C_X] } },
{ apply mv_polynomial.is_semiring_hom_sum_to_iter α β γ },
{ apply mv_polynomial.is_semiring_hom_iter_to_sum α β γ }
end
/--
The ring isomorphism between multivariable polynomials in `option β` and
polynomials with coefficients in `mv_polynomial β α`.
-/
def option_equiv_left : mv_polynomial (option β) α ≃+* polynomial (mv_polynomial β α) :=
(ring_equiv_of_equiv α $ (equiv.option_equiv_sum_punit β).trans (equiv.sum_comm _ _)).trans $
(sum_ring_equiv α _ _).trans $
punit_ring_equiv _
/--
The ring isomorphism between multivariable polynomials in `option β` and
multivariable polynomials with coefficients in polynomials.
-/
def option_equiv_right : mv_polynomial (option β) α ≃+* mv_polynomial β (polynomial α) :=
(ring_equiv_of_equiv α $ equiv.option_equiv_sum_punit.{0} β).trans $
(sum_ring_equiv α β unit).trans $
ring_equiv_congr (mv_polynomial unit α) (punit_ring_equiv α)
/--
The ring isomorphism between multivariable polynomials in `fin (n + 1)` and
polynomials over multivariable polynomials in `fin n`.
-/
def fin_succ_equiv (n : ℕ) :
mv_polynomial (fin (n + 1)) α ≃+* polynomial (mv_polynomial (fin n) α) :=
(ring_equiv_of_equiv α (fin_succ_equiv n)).trans
(option_equiv_left α (fin n))
end
end equiv
/-!
## Partial derivatives
-/
section pderivative
variables {R : Type} [comm_ring R]
variable {S : Type}
/-- `pderivative v p` is the partial derivative of `p` with respect to `v` -/
def pderivative (v : S) (p : mv_polynomial S R) : mv_polynomial S R :=
p.sum (λ A B, monomial (A - single v 1) (B * (A v)))
@[simp]
lemma pderivative_add {v : S} {f g : mv_polynomial S R} :
pderivative v (f + g) = pderivative v f + pderivative v g :=
begin
refine sum_add_index _ _,
{ simp },
simp [add_mul],
end
@[simp]
lemma pderivative_monomial {v : S} {u : S →₀ ℕ} {a : R} :
pderivative v (monomial u a) = monomial (u - single v 1) (a * (u v)) :=
by simp [pderivative]
@[simp]
lemma pderivative_C {v : S} {a : R} : pderivative v (C a) = 0 :=
suffices pderivative v (monomial 0 a) = 0, by simpa,
by simp
@[simp]
lemma pderivative_zero {v : S} : pderivative v (0 : mv_polynomial S R) = 0 :=
suffices pderivative v (C 0 : mv_polynomial S R) = 0, by simpa,
show pderivative v (C 0 : mv_polynomial S R) = 0, from pderivative_C
section
variables (R)
/-- `pderivative : S → mv_polynomial S R → mv_polynomial S R` as an `add_monoid_hom` -/
def pderivative.add_monoid_hom (v : S) : mv_polynomial S R →+ mv_polynomial S R :=
{ to_fun := pderivative v,
map_zero' := pderivative_zero,
map_add' := λ x y, pderivative_add, }
@[simp]
lemma pderivative.add_monoid_hom_apply (v : S) (p : mv_polynomial S R) :
(pderivative.add_monoid_hom R v) p = pderivative v p :=
rfl
end
lemma pderivative_eq_zero_of_not_mem_vars {v : S} {f : mv_polynomial S R} (h : v ∉ f.vars) :
pderivative v f = 0 :=
begin
change (pderivative.add_monoid_hom R v) f = 0,
rw [f.as_sum, add_monoid_hom.map_sum],
apply finset.sum_eq_zero,
intros,
simp [mem_support_not_mem_vars_zero H h],
end
lemma pderivative_monomial_single {a : R} {v : S} {n : ℕ} :
pderivative v (monomial (single v n) a) = monomial (single v (n-1)) (a * n) :=
by simp
private lemma monomial_sub_single_one_add {v : S} {u u' : S →₀ ℕ} {r r' : R} :
monomial (u - single v 1 + u') (r * (u v) * r') =
monomial (u + u' - single v 1) (r * (u v) * r') :=
by by_cases h : u v = 0; simp [h, sub_single_one_add]
private lemma monomial_add_sub_single_one {v : S} {u u' : S →₀ ℕ} {r r' : R} :
monomial (u + (u' - single v 1)) (r * (r' * (u' v))) =
monomial (u + u' - single v 1) (r * (r' * (u' v))) :=
by by_cases h : u' v = 0; simp [h, add_sub_single_one]
lemma pderivative_monomial_mul {v : S} {u u' : S →₀ ℕ} {r r' : R} :
pderivative v (monomial u r * monomial u' r') =
pderivative v (monomial u r) * monomial u' r' + monomial u r * pderivative v (monomial u' r') :=
begin
simp [monomial_sub_single_one_add, monomial_add_sub_single_one],
congr,
ring,
end
@[simp]
lemma pderivative_mul {v : S} {f g : mv_polynomial S R} :
pderivative v (f * g) = pderivative v f * g + f * pderivative v g :=
begin
apply induction_on' f,
{ apply induction_on' g,
{ intros u r u' r', exact pderivative_monomial_mul },
{ intros p q hp hq u r,
rw [mul_add, pderivative_add, hp, hq, mul_add, pderivative_add],
ring } },
{ intros p q hp hq,
simp [add_mul, hp, hq],
ring, }
end
@[simp]
lemma pderivative_C_mul {a : R} {f : mv_polynomial S R} {v : S} :
pderivative v (C a * f) = C a * pderivative v f :=
by rw [pderivative_mul, pderivative_C, zero_mul, zero_add]
end pderivative
end mv_polynomial
|
cd8258c6ae1c92f9889848889500f5dec2c4b285 | 8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3 | /src/tactic/transform_decl.lean | 67c7e7830bbdb31f2942ba5f67f86b4c635d5213 | [
"Apache-2.0"
] | permissive | troyjlee/mathlib | e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5 | 45e7eb8447555247246e3fe91c87066506c14875 | refs/heads/master | 1,689,248,035,046 | 1,629,470,528,000 | 1,629,470,528,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,283 | lean | /-
Copyright (c) 2017 Mario Carneiro All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Floris van Doorn
-/
import tactic.core
namespace tactic
open expr
/-- Auxilliary function for `additive_test`. The bool argument *only* matters when applied
to exactly a constant. -/
meta def additive_test_aux (f : name → option name) (ignore : name_map $ list ℕ) :
bool → expr → bool
| b (var n) := tt
| b (sort l) := tt
| b (const n ls) := b || (f n).is_some
| b (mvar n m t) := tt
| b (local_const n m bi t) := tt
| b (app e f) := additive_test_aux tt e &&
-- this might be inefficient.
-- If it becomes a performance problem: we can give this info for the recursive call to `e`.
match ignore.find e.get_app_fn.const_name with
| some l := if e.get_app_num_args + 1 ∈ l then tt else additive_test_aux ff f
| none := additive_test_aux ff f
end
| b (lam n bi e t) := additive_test_aux ff t
| b (pi n bi e t) := additive_test_aux ff t
| b (elet n g e f) := additive_test_aux ff e && additive_test_aux ff f
| b (macro d args) := tt
/--
`additive_test f replace_all ignore e` tests whether the expression `e` contains no constant
`nm` that is not applied to any arguments, and such that `f nm = none`.
This is used in `@[to_additive]` for deciding which subexpressions to transform: we only transform
constants if `additive_test` applied to their first argument returns `tt`.
This means we will replace expression applied to e.g. `α` or `α × β`, but not when applied to
e.g. `ℕ` or `ℝ × α`.
`f` is the dictionary of declarations that are in the `to_additive` dictionary.
We ignore all arguments specified in the `name_map` `ignore`.
If `replace_all` is `tt` the test always return `tt`.
-/
meta def additive_test (f : name → option name) (replace_all : bool) (ignore : name_map $ list ℕ)
(e : expr) : bool :=
if replace_all then tt else additive_test_aux f ignore ff e
/-- transform the declaration `src` and all declarations `pre._proof_i` occurring in `src`
using the dictionary `f`.
`replace_all`, `trace`, `ignore` and `reorder` are configuration options.
`pre` is the declaration that got the `@[to_additive]` attribute and `tgt_pre` is the target of this
declaration. -/
meta def transform_decl_with_prefix_fun_aux (f : name → option name)
(replace_all trace : bool) (ignore reorder : name_map $ list ℕ) (pre tgt_pre : name)
(attrs : list name) : name → command :=
λ src,
do
-- if this declaration is not `pre` or an internal declaration, we do nothing.
tt ← return (src = pre ∨ src.is_internal : bool) |
if (f src).is_some then skip else fail!("@[to_additive] failed.
The declaration {pre} depends on the declaration {src} which is in the namespace {pre}, but " ++
"does not have the `@[to_additive]` attribute. This is not supported. Workaround: move {src} to " ++
"a different namespace."),
env ← get_env,
-- we find the additive name of `src`
let tgt := src.map_prefix (λ n, if n = pre then some tgt_pre else none),
-- we skip if we already transformed this declaration before
ff ← return $ env.contains tgt | skip,
decl ← get_decl src,
-- we first transform all the declarations of the form `pre._proof_i`
(decl.type.list_names_with_prefix pre).mfold () (λ n _, transform_decl_with_prefix_fun_aux n),
(decl.value.list_names_with_prefix pre).mfold () (λ n _, transform_decl_with_prefix_fun_aux n),
-- we transform `decl` using `f` and the configuration options.
let decl :=
decl.update_with_fun env (name.map_prefix f) (additive_test f replace_all ignore) reorder tgt,
pp_decl ← pp decl,
when trace $ trace!"[to_additive] > generating\n{pp_decl}",
decorate_error (format!"@[to_additive] failed. Type mismatch in additive declaration.
For help, see the docstring of `to_additive.attr`, section `Troubleshooting`.
Failed to add declaration\n{pp_decl}
Nested error message:\n").to_string $ -- the empty line is intentional
if env.is_protected src then add_protected_decl decl else add_decl decl,
attrs.mmap' $ λ n, copy_attribute n src tgt
/--
Make a new copy of a declaration,
replacing fragments of the names of identifiers in the type and the body using the function `f`.
This is used to implement `@[to_additive]`.
-/
meta def transform_decl_with_prefix_fun (f : name → option name) (replace_all trace : bool)
(ignore reorder : name_map $ list ℕ) (src tgt : name) (attrs : list name) : command :=
do transform_decl_with_prefix_fun_aux f replace_all trace ignore reorder src tgt attrs src,
ls ← get_eqn_lemmas_for tt src,
ls.mmap' $ transform_decl_with_prefix_fun_aux f replace_all trace ignore reorder src tgt attrs
/--
Make a new copy of a declaration,
replacing fragments of the names of identifiers in the type and the body using the dictionary `dict`.
This is used to implement `@[to_additive]`.
-/
meta def transform_decl_with_prefix_dict (dict : name_map name) (replace_all trace : bool)
(ignore reorder : name_map $ list ℕ) (src tgt : name) (attrs : list name) : command :=
transform_decl_with_prefix_fun dict.find replace_all trace ignore reorder src tgt attrs
end tactic
|
0f9187b5bfcc3f6653d63708d9919f14597b1021 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/analysis/calculus/inverse.lean | 782b5ef686d90d466a0b0ac05c3a517c52dbcdc5 | [] | 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 | 34,560 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Heather Macbeth.
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.analysis.calculus.times_cont_diff
import Mathlib.topology.local_homeomorph
import Mathlib.topology.metric_space.contracting
import Mathlib.PostPort
universes u_1 u_2 u_3 u_6 u_7 u_8
namespace Mathlib
/-!
# Inverse function theorem
In this file we prove the inverse function theorem. It says that if a map `f : E → F`
has an invertible strict derivative `f'` at `a`, then it is locally invertible,
and the inverse function has derivative `f' ⁻¹`.
We define `has_strict_deriv_at.to_local_homeomorph` that repacks a function `f`
with a `hf : has_strict_fderiv_at f f' a`, `f' : E ≃L[𝕜] F`, into a `local_homeomorph`.
The `to_fun` of this `local_homeomorph` is `defeq` to `f`, so one can apply theorems
about `local_homeomorph` to `hf.to_local_homeomorph f`, and get statements about `f`.
Then we define `has_strict_fderiv_at.local_inverse` to be the `inv_fun` of this `local_homeomorph`,
and prove two versions of the inverse function theorem:
* `has_strict_fderiv_at.to_local_inverse`: if `f` has an invertible derivative `f'` at `a` in the
strict sense (`hf`), then `hf.local_inverse f f' a` has derivative `f'.symm` at `f a` in the
strict sense;
* `has_strict_fderiv_at.to_local_left_inverse`: if `f` has an invertible derivative `f'` at `a` in
the strict sense and `g` is locally left inverse to `f` near `a`, then `g` has derivative
`f'.symm` at `f a` in the strict sense.
In the one-dimensional case we reformulate these theorems in terms of `has_strict_deriv_at` and
`f'⁻¹`.
We also reformulate the theorems in terms of `times_cont_diff`, to give that `C^k` (respectively,
smooth) inputs give `C^k` (smooth) inverses. These versions require that continuous
differentiability implies strict differentiability; this is false over a general field, true over
`ℝ` or `ℂ` and implemented here assuming `is_R_or_C 𝕂`.
Some related theorems, providing the derivative and higher regularity assuming that we already know
the inverse function, are formulated in `fderiv.lean`, `deriv.lean`, and `times_cont_diff.lean`.
## Notations
In the section about `approximates_linear_on` we introduce some `local notation` to make formulas
shorter:
* by `N` we denote `∥f'⁻¹∥`;
* by `g` we denote the auxiliary contracting map `x ↦ x + f'.symm (y - f x)` used to prove that
`{x | f x = y}` is nonempty.
## Tags
derivative, strictly differentiable, continuously differentiable, smooth, inverse function
-/
/-!
### Non-linear maps approximating close to affine maps
In this section we study a map `f` such that `∥f x - f y - f' (x - y)∥ ≤ c * ∥x - y∥` on an open set
`s`, where `f' : E ≃L[𝕜] F` is a continuous linear equivalence and `c < ∥f'⁻¹∥`. Maps of this type
behave like `f a + f' (x - a)` near each `a ∈ s`.
If `E` is a complete space, we prove that the image `f '' s` is open, and `f` is a homeomorphism
between `s` and `f '' s`. More precisely, we define `approximates_linear_on.to_local_homeomorph` to
be a `local_homeomorph` with `to_fun = f`, `source = s`, and `target = f '' s`.
Maps of this type naturally appear in the proof of the inverse function theorem (see next section),
and `approximates_linear_on.to_local_homeomorph` will imply that the locally inverse function
exists.
We define this auxiliary notion to split the proof of the inverse function theorem into small
lemmas. This approach makes it possible
- to prove a lower estimate on the size of the domain of the inverse function;
- to reuse parts of the proofs in the case if a function is not strictly differentiable. E.g., for a
function `f : E × F → G` with estimates on `f x y₁ - f x y₂` but not on `f x₁ y - f x₂ y`.
-/
/-- We say that `f` approximates a continuous linear map `f'` on `s` with constant `c`,
if `∥f x - f y - f' (x - y)∥ ≤ c * ∥x - y∥` whenever `x, y ∈ s`.
This predicate is defined to facilitate the splitting of the inverse function theorem into small
lemmas. Some of these lemmas can be useful, e.g., to prove that the inverse function is defined
on a specific set. -/
def approximates_linear_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : E → F) (f' : continuous_linear_map 𝕜 E F) (s : set E) (c : nnreal) :=
∀ (x : E), x ∈ s → ∀ (y : E), y ∈ s → norm (f x - f y - coe_fn f' (x - y)) ≤ ↑c * norm (x - y)
namespace approximates_linear_on
/-! First we prove some properties of a function that `approximates_linear_on` a (not necessarily
invertible) continuous linear map. -/
theorem mono_num {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {s : set E} {c : nnreal} {c' : nnreal} (hc : c ≤ c') (hf : approximates_linear_on f f' s c) : approximates_linear_on f f' s c' :=
fun (x : E) (hx : x ∈ s) (y : E) (hy : y ∈ s) =>
le_trans (hf x hx y hy) (mul_le_mul_of_nonneg_right hc (norm_nonneg (x - y)))
theorem mono_set {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {s : set E} {t : set E} {c : nnreal} (hst : s ⊆ t) (hf : approximates_linear_on f f' t c) : approximates_linear_on f f' s c :=
fun (x : E) (hx : x ∈ s) (y : E) (hy : y ∈ s) => hf x (hst hx) y (hst hy)
theorem lipschitz_sub {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f f' s c) : lipschitz_with c fun (x : ↥s) => f ↑x - coe_fn f' ↑x := sorry
protected theorem lipschitz {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f f' s c) : lipschitz_with (nnnorm f' + c) (set.restrict f s) := sorry
protected theorem continuous {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f f' s c) : continuous (set.restrict f s) :=
lipschitz_with.continuous (approximates_linear_on.lipschitz hf)
protected theorem continuous_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f f' s c) : continuous_on f s :=
iff.mpr continuous_on_iff_continuous_restrict (approximates_linear_on.continuous hf)
/-!
From now on we assume that `f` approximates an invertible continuous linear map `f : E ≃L[𝕜] F`.
We also assume that either `E = {0}`, or `c < ∥f'⁻¹∥⁻¹`. We use `N` as an abbreviation for `∥f'⁻¹∥`.
-/
protected theorem antilipschitz {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) : antilipschitz_with (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹ - c⁻¹) (set.restrict f s) := sorry
protected theorem injective {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) : function.injective (set.restrict f s) :=
antilipschitz_with.injective (approximates_linear_on.antilipschitz hf hc)
protected theorem inj_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) : set.inj_on f s :=
iff.mpr set.inj_on_iff_injective (approximates_linear_on.injective hf hc)
/-- A map approximating a linear equivalence on a set defines a local equivalence on this set.
Should not be used outside of this file, because it is superseded by `to_local_homeomorph` below.
This is a first step towards the inverse function. -/
def to_local_equiv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) : local_equiv E F :=
set.inj_on.to_local_equiv f s (approximates_linear_on.inj_on hf hc)
/-- The inverse function is continuous on `f '' s`. Use properties of `local_homeomorph` instead. -/
theorem inverse_continuous_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) : continuous_on (⇑(local_equiv.symm (to_local_equiv hf hc))) (f '' s) := sorry
/-!
Now we prove that `f '' s` is an open set. This follows from the fact that the restriction of `f`
on `s` is an open map. More precisely, we show that the image of a closed ball $$\bar B(a, ε) ⊆ s$$
under `f` includes the closed ball $$\bar B\left(f(a), \frac{ε}{∥{f'}⁻¹∥⁻¹-c}\right)$$.
In order to do this, we introduce an auxiliary map $$g_y(x) = x + {f'}⁻¹ (y - f x)$$. Provided that
$$∥y - f a∥ ≤ \frac{ε}{∥{f'}⁻¹∥⁻¹-c}$$, we prove that $$g_y$$ contracts in $$\bar B(a, ε)$$ and `f`
sends the fixed point of $$g_y$$ to `y`.
-/
/-- Iterations of this map converge to `f⁻¹ y`. The formula is very similar to the one
used in Newton's method, but we use the same `f'.symm` for all `y` instead of evaluating
the derivative at each point along the orbit. -/
def inverse_approx_map {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : E → F) (f' : continuous_linear_equiv 𝕜 E F) (y : F) (x : E) : E :=
x + coe_fn (continuous_linear_equiv.symm f') (y - f x)
theorem inverse_approx_map_sub {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} (y : F) (x : E) (x' : E) : inverse_approx_map f f' y x - inverse_approx_map f f' y x' =
x - x' - coe_fn (continuous_linear_equiv.symm f') (f x - f x') := sorry
theorem inverse_approx_map_dist_self {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} (y : F) (x : E) : dist (inverse_approx_map f f' y x) x =
dist (coe_fn (continuous_linear_equiv.symm f') (f x)) (coe_fn (continuous_linear_equiv.symm f') y) := sorry
theorem inverse_approx_map_dist_self_le {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} (y : F) (x : E) : dist (inverse_approx_map f f' y x) x ≤ ↑(nnnorm ↑(continuous_linear_equiv.symm f')) * dist (f x) y := sorry
theorem inverse_approx_map_fixed_iff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} (y : F) {x : E} : inverse_approx_map f f' y x = x ↔ f x = y := sorry
theorem inverse_approx_map_contracts_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (y : F) (hf : approximates_linear_on f (↑f') s c) {x : E} {x' : E} (hx : x ∈ s) (hx' : x' ∈ s) : dist (inverse_approx_map f f' y x) (inverse_approx_map f f' y x') ≤
↑(nnnorm ↑(continuous_linear_equiv.symm f')) * ↑c * dist x x' := sorry
theorem inverse_approx_map_maps_to {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} {y : F} {ε : ℝ} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) {b : E} (hb : b ∈ s) (hε : metric.closed_ball b ε ⊆ s) (hy : y ∈ metric.closed_ball (f b) ((↑(nnnorm ↑(continuous_linear_equiv.symm f'))⁻¹ - ↑c) * ε)) : set.maps_to (inverse_approx_map f f' y) (metric.closed_ball b ε) (metric.closed_ball b ε) := sorry
theorem surj_on_closed_ball {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} {ε : ℝ} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) {b : E} (ε0 : 0 ≤ ε) (hε : metric.closed_ball b ε ⊆ s) : set.surj_on f (metric.closed_ball b ε)
(metric.closed_ball (f b) ((↑(nnnorm ↑(continuous_linear_equiv.symm f'))⁻¹ - ↑c) * ε)) := sorry
/-- Given a function `f` that approximates a linear equivalence on an open set `s`,
returns a local homeomorph with `to_fun = f` and `source = s`. -/
def to_local_homeomorph {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] (f : E → F) {f' : continuous_linear_equiv 𝕜 E F} (s : set E) {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) (hs : is_open s) : local_homeomorph E F :=
local_homeomorph.mk (to_local_equiv hf hc) hs sorry sorry (inverse_continuous_on hf hc)
@[simp] theorem to_local_homeomorph_coe {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) (hs : is_open s) : ⇑(to_local_homeomorph f s hf hc hs) = f :=
rfl
@[simp] theorem to_local_homeomorph_source {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) (hs : is_open s) : local_equiv.source (local_homeomorph.to_local_equiv (to_local_homeomorph f s hf hc hs)) = s :=
rfl
@[simp] theorem to_local_homeomorph_target {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) (hs : is_open s) : local_equiv.target (local_homeomorph.to_local_equiv (to_local_homeomorph f s hf hc hs)) = f '' s :=
rfl
theorem closed_ball_subset_target {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} {ε : ℝ} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) (hs : is_open s) {b : E} (ε0 : 0 ≤ ε) (hε : metric.closed_ball b ε ⊆ s) : metric.closed_ball (f b) ((↑(nnnorm ↑(continuous_linear_equiv.symm f'))⁻¹ - ↑c) * ε) ⊆
local_equiv.target (local_homeomorph.to_local_equiv (to_local_homeomorph f s hf hc hs)) :=
set.surj_on.mono hε
(set.subset.refl (metric.closed_ball (f b) ((↑(nnnorm ↑(continuous_linear_equiv.symm f'))⁻¹ - ↑c) * ε)))
(surj_on_closed_ball hf hc ε0 hε)
end approximates_linear_on
/-!
### Inverse function theorem
Now we prove the inverse function theorem. Let `f : E → F` be a map defined on a complete vector
space `E`. Assume that `f` has an invertible derivative `f' : E ≃L[𝕜] F` at `a : E` in the strict
sense. Then `f` approximates `f'` in the sense of `approximates_linear_on` on an open neighborhood
of `a`, and we can apply `approximates_linear_on.to_local_homeomorph` to construct the inverse
function. -/
namespace has_strict_fderiv_at
/-- If `f` has derivative `f'` at `a` in the strict sense and `c > 0`, then `f` approximates `f'`
with constant `c` on some neighborhood of `a`. -/
theorem approximates_deriv_on_nhds {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) {c : nnreal} (hc : subsingleton E ∨ 0 < c) : ∃ (s : set E), ∃ (H : s ∈ nhds a), approximates_linear_on f f' s c := sorry
theorem approximates_deriv_on_open_nhds {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : ∃ (s : set E),
∃ (hs : a ∈ s ∧ is_open s), approximates_linear_on f (↑f') s (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹ / bit0 1) := sorry
/-- Given a function with an invertible strict derivative at `a`, returns a `local_homeomorph`
with `to_fun = f` and `a ∈ source`. This is a part of the inverse function theorem.
The other part `has_strict_fderiv_at.to_local_inverse` states that the inverse function
of this `local_homeomorph` has derivative `f'.symm`. -/
def to_local_homeomorph {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] (f : E → F) {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : local_homeomorph E F :=
approximates_linear_on.to_local_homeomorph f (classical.some (approximates_deriv_on_open_nhds hf)) sorry sorry sorry
@[simp] theorem to_local_homeomorph_coe {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : ⇑(to_local_homeomorph f hf) = f :=
rfl
theorem mem_to_local_homeomorph_source {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : a ∈ local_equiv.source (local_homeomorph.to_local_equiv (to_local_homeomorph f hf)) :=
and.left (Exists.fst (classical.some_spec (approximates_deriv_on_open_nhds hf)))
theorem image_mem_to_local_homeomorph_target {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : f a ∈ local_equiv.target (local_homeomorph.to_local_equiv (to_local_homeomorph f hf)) :=
local_homeomorph.map_source (to_local_homeomorph f hf) (mem_to_local_homeomorph_source hf)
theorem map_nhds_eq {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : filter.map f (nhds a) = nhds (f a) :=
local_homeomorph.map_nhds_eq (to_local_homeomorph f hf) (mem_to_local_homeomorph_source hf)
/-- Given a function `f` with an invertible derivative, returns a function that is locally inverse
to `f`. -/
def local_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] (f : E → F) (f' : continuous_linear_equiv 𝕜 E F) (a : E) (hf : has_strict_fderiv_at f (↑f') a) : F → E :=
⇑(local_homeomorph.symm (to_local_homeomorph f hf))
theorem local_inverse_def {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : local_inverse f f' a hf = ⇑(local_homeomorph.symm (to_local_homeomorph f hf)) :=
rfl
theorem eventually_left_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : filter.eventually (fun (x : E) => local_inverse f f' a hf (f x) = x) (nhds a) :=
local_homeomorph.eventually_left_inverse (to_local_homeomorph f hf) (mem_to_local_homeomorph_source hf)
@[simp] theorem local_inverse_apply_image {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : local_inverse f f' a hf (f a) = a :=
filter.eventually.self_of_nhds (eventually_left_inverse hf)
theorem eventually_right_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : filter.eventually (fun (y : F) => f (local_inverse f f' a hf y) = y) (nhds (f a)) :=
local_homeomorph.eventually_right_inverse' (to_local_homeomorph f hf) (mem_to_local_homeomorph_source hf)
theorem local_inverse_continuous_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : continuous_at (local_inverse f f' a hf) (f a) :=
local_homeomorph.continuous_at_symm (to_local_homeomorph f hf) (image_mem_to_local_homeomorph_target hf)
theorem local_inverse_tendsto {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : filter.tendsto (local_inverse f f' a hf) (nhds (f a)) (nhds a) :=
local_homeomorph.tendsto_symm (to_local_homeomorph f hf) (mem_to_local_homeomorph_source hf)
theorem local_inverse_unique {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) {g : F → E} (hg : filter.eventually (fun (x : E) => g (f x) = x) (nhds a)) : filter.eventually (fun (y : F) => g y = local_inverse f f' a hf y) (nhds (f a)) :=
filter.eventually_eq_of_left_inv_of_right_inv hg (eventually_right_inverse hf)
(local_homeomorph.tendsto_symm (to_local_homeomorph f hf) (mem_to_local_homeomorph_source hf))
/-- If `f` has an invertible derivative `f'` at `a` in the sense of strict differentiability `(hf)`,
then the inverse function `hf.local_inverse f` has derivative `f'.symm` at `f a`. -/
theorem to_local_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : has_strict_fderiv_at (local_inverse f f' a hf) (↑(continuous_linear_equiv.symm f')) (f a) := sorry
/-- If `f : E → F` has an invertible derivative `f'` at `a` in the sense of strict differentiability
and `g (f x) = x` in a neighborhood of `a`, then `g` has derivative `f'.symm` at `f a`.
For a version assuming `f (g y) = y` and continuity of `g` at `f a` but not `[complete_space E]`
see `of_local_left_inverse`. -/
theorem to_local_left_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) {g : F → E} (hg : filter.eventually (fun (x : E) => g (f x) = x) (nhds a)) : has_strict_fderiv_at g (↑(continuous_linear_equiv.symm f')) (f a) :=
congr_of_eventually_eq (to_local_inverse hf)
(filter.eventually.mono (local_inverse_unique hf hg) fun (_x : F) => Eq.symm)
end has_strict_fderiv_at
/-- If a function has an invertible strict derivative at all points, then it is an open map. -/
theorem open_map_of_strict_fderiv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space E] {f : E → F} {f' : E → continuous_linear_equiv 𝕜 E F} (hf : ∀ (x : E), has_strict_fderiv_at f (↑(f' x)) x) : is_open_map f :=
iff.mpr is_open_map_iff_nhds_le fun (x : E) => eq.ge (has_strict_fderiv_at.map_nhds_eq (hf x))
/-!
### Inverse function theorem, 1D case
In this case we prove a version of the inverse function theorem for maps `f : 𝕜 → 𝕜`.
We use `continuous_linear_equiv.units_equiv_aut` to translate `has_strict_deriv_at f f' a` and
`f' ≠ 0` into `has_strict_fderiv_at f (_ : 𝕜 ≃L[𝕜] 𝕜) a`.
-/
namespace has_strict_deriv_at
/-- A function that is inverse to `f` near `a`. -/
def local_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [cs : complete_space 𝕜] (f : 𝕜 → 𝕜) (f' : 𝕜) (a : 𝕜) (hf : has_strict_deriv_at f f' a) (hf' : f' ≠ 0) : 𝕜 → 𝕜 :=
has_strict_fderiv_at.local_inverse f (coe_fn (continuous_linear_equiv.units_equiv_aut 𝕜) (units.mk0 f' hf')) a
(has_strict_fderiv_at_equiv hf hf')
theorem map_nhds_eq {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [cs : complete_space 𝕜] {f : 𝕜 → 𝕜} {f' : 𝕜} {a : 𝕜} (hf : has_strict_deriv_at f f' a) (hf' : f' ≠ 0) : filter.map f (nhds a) = nhds (f a) :=
has_strict_fderiv_at.map_nhds_eq (has_strict_fderiv_at_equiv hf hf')
theorem to_local_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [cs : complete_space 𝕜] {f : 𝕜 → 𝕜} {f' : 𝕜} {a : 𝕜} (hf : has_strict_deriv_at f f' a) (hf' : f' ≠ 0) : has_strict_deriv_at (local_inverse f f' a hf hf') (f'⁻¹) (f a) :=
has_strict_fderiv_at.to_local_inverse (has_strict_fderiv_at_equiv hf hf')
theorem to_local_left_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [cs : complete_space 𝕜] {f : 𝕜 → 𝕜} {f' : 𝕜} {a : 𝕜} (hf : has_strict_deriv_at f f' a) (hf' : f' ≠ 0) {g : 𝕜 → 𝕜} (hg : filter.eventually (fun (x : 𝕜) => g (f x) = x) (nhds a)) : has_strict_deriv_at g (f'⁻¹) (f a) :=
has_strict_fderiv_at.to_local_left_inverse (has_strict_fderiv_at_equiv hf hf') hg
end has_strict_deriv_at
/-- If a function has a non-zero strict derivative at all points, then it is an open map. -/
theorem open_map_of_strict_deriv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {f : 𝕜 → 𝕜} {f' : 𝕜 → 𝕜} (hf : ∀ (x : 𝕜), has_strict_deriv_at f (f' x) x) (h0 : ∀ (x : 𝕜), f' x ≠ 0) : is_open_map f :=
iff.mpr is_open_map_iff_nhds_le fun (x : 𝕜) => eq.ge (has_strict_deriv_at.map_nhds_eq (hf x) (h0 x))
/-!
### Inverse function theorem, smooth case
-/
namespace times_cont_diff_at
/-- Given a `times_cont_diff` function over `𝕂` (which is `ℝ` or `ℂ`) with an invertible
derivative at `a`, returns a `local_homeomorph` with `to_fun = f` and `a ∈ source`. -/
def to_local_homeomorph {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] (f : E' → F') {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : local_homeomorph E' F' :=
has_strict_fderiv_at.to_local_homeomorph f sorry
@[simp] theorem to_local_homeomorph_coe {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] {f : E' → F'} {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : ⇑(to_local_homeomorph f hf hf' hn) = f :=
rfl
theorem mem_to_local_homeomorph_source {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] {f : E' → F'} {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : a ∈ local_equiv.source (local_homeomorph.to_local_equiv (to_local_homeomorph f hf hf' hn)) :=
has_strict_fderiv_at.mem_to_local_homeomorph_source (has_strict_fderiv_at' hf hf' hn)
theorem image_mem_to_local_homeomorph_target {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] {f : E' → F'} {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : f a ∈ local_equiv.target (local_homeomorph.to_local_equiv (to_local_homeomorph f hf hf' hn)) :=
has_strict_fderiv_at.image_mem_to_local_homeomorph_target (has_strict_fderiv_at' hf hf' hn)
/-- Given a `times_cont_diff` function over `𝕂` (which is `ℝ` or `ℂ`) with an invertible derivative
at `a`, returns a function that is locally inverse to `f`. -/
def local_inverse {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] {f : E' → F'} {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : F' → E' :=
has_strict_fderiv_at.local_inverse f f' a sorry
theorem local_inverse_apply_image {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] {f : E' → F'} {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : local_inverse hf hf' hn (f a) = a :=
has_strict_fderiv_at.local_inverse_apply_image (has_strict_fderiv_at' hf hf' hn)
/-- Given a `times_cont_diff` function over `𝕂` (which is `ℝ` or `ℂ`) with an invertible derivative
at `a`, the inverse function (produced by `times_cont_diff.to_local_homeomorph`) is
also `times_cont_diff`. -/
theorem to_local_inverse {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] {f : E' → F'} {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : times_cont_diff_at 𝕂 n (local_inverse hf hf' hn) (f a) := sorry
|
7837573de6b3e1e689c9e0c1303b778e462a83a7 | e61a235b8468b03aee0120bf26ec615c045005d2 | /stage0/src/Init/Lean/Scopes.lean | 1dcb04f3b691da1a531e587add54c37048526973 | [
"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 | 3,719 | 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.Environment
namespace Lean
/- Scope management
TODO: delete after we delete parser implemented in C++.
We have decided to store scope information at ElabState
-/
structure ScopeManagerState :=
(allNamespaces : NameSet := {})
/- Stack of namespaces for each each open namespace and section -/
(namespaces : List Name := [])
/- Stack of namespace/section headers -/
(headers : List Name := [])
(isNamespace : List Bool := [])
namespace ScopeManagerState
instance : Inhabited ScopeManagerState := ⟨{}⟩
def saveNamespace (s : ScopeManagerState) (n : Name) : ScopeManagerState :=
{ s with allNamespaces := s.allNamespaces.insert n }
end ScopeManagerState
def regScopeManagerExtension : IO (SimplePersistentEnvExtension Name ScopeManagerState) :=
registerSimplePersistentEnvExtension {
name := `scopes,
addImportedFn := fun as => mkStateFromImportedEntries ScopeManagerState.saveNamespace {} as,
addEntryFn := fun s n => { s with allNamespaces := s.allNamespaces.insert n },
}
@[init regScopeManagerExtension]
constant scopeManagerExt : SimplePersistentEnvExtension Name ScopeManagerState := arbitrary _
namespace Environment
@[export lean_get_namespaces]
def getNamespaces (env : Environment) : List Name :=
(scopeManagerExt.getState env).namespaces
def getNamespaceSet (env : Environment) : NameSet :=
(scopeManagerExt.getState env).allNamespaces
@[export lean_is_namespace]
def isNamespace (env : Environment) (n : Name) : Bool :=
env.getNamespaceSet.contains n
@[export lean_in_section]
def inSection (env : Environment) : Bool :=
match (scopeManagerExt.getState env).isNamespace with
| (b::_) => !b
| _ => false
@[export lean_has_open_scopes]
def hasOpenScopes (env : Environment) : Bool :=
!env.getNamespaces.isEmpty
@[export lean_get_namespace]
def getNamespace (env : Environment) : Name :=
match env.getNamespaces with
| (n::_) => n
| _ => Name.anonymous
@[export lean_get_scope_header]
def getScopeHeader (env : Environment) : Name :=
match (scopeManagerExt.getState env).headers with
| (n::_) => n
| _ => Name.anonymous
@[export lean_to_valid_namespace]
def toValidNamespace (env : Environment) (n : Name) : Option Name :=
let s := scopeManagerExt.getState env;
if s.allNamespaces.contains n then some n
else s.namespaces.foldl
(fun r ns => match r with
| some _ => r
| none =>
let c := ns ++ n;
if s.allNamespaces.contains c then some c else none)
none
def registerNamespaceAux (env : Environment) (n : Name) : Environment :=
if env.getNamespaceSet.contains n then env else scopeManagerExt.addEntry env n
@[export lean_register_namespace]
def registerNamespace : Environment → Name → Environment
| env, n@(Name.str p _ _) => registerNamespace (registerNamespaceAux env n) p
| env, _ => env
def pushScopeCore (env : Environment) (header : Name) (isNamespace : Bool) : Environment :=
let ns := env.getNamespace;
let newNs := if isNamespace then ns ++ header else ns;
let env := env.registerNamespaceAux newNs;
let env := scopeManagerExt.modifyState env $ fun s =>
{ s with
headers := header :: s.headers,
namespaces := newNs :: s.namespaces,
isNamespace := isNamespace :: s.isNamespace };
env
def popScopeCore (env : Environment) : Environment :=
if env.getNamespaces.isEmpty then env
else scopeManagerExt.modifyState env $ fun s =>
{ s with
headers := s.headers.tail!,
namespaces := s.namespaces.tail!,
isNamespace := s.isNamespace.tail! }
end Environment
end Lean
|
09043feea529e3eea6b50ff01403382ad949c278 | ad0c7d243dc1bd563419e2767ed42fb323d7beea | /category_theory/examples/topological_spaces.lean | a8c8f929aa887381af03310429eb520952e6c4a0 | [
"Apache-2.0"
] | permissive | sebzim4500/mathlib | e0b5a63b1655f910dee30badf09bd7e191d3cf30 | 6997cafbd3a7325af5cb318561768c316ceb7757 | refs/heads/master | 1,585,549,958,618 | 1,538,221,723,000 | 1,538,221,723,000 | 150,869,076 | 0 | 0 | Apache-2.0 | 1,538,229,323,000 | 1,538,229,323,000 | null | UTF-8 | Lean | false | false | 2,535 | lean | -- Copyright (c) 2017 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Patrick Massot, Scott Morrison, Mario Carneiro
import category_theory.full_subcategory
import category_theory.functor_category
import category_theory.natural_isomorphism
import analysis.topology.topological_space
import analysis.topology.continuity
open category_theory
open category_theory.nat_iso
universe u
namespace category_theory.examples
/-- The category of topological spaces and continuous maps. -/
@[reducible] def Top : Type (u+1) := bundled topological_space
instance (x : Top) : topological_space x := x.str
namespace Top
instance : concrete_category @continuous := ⟨@continuous_id, @continuous.comp⟩
-- local attribute [class] continuous
-- instance {R S : Top} (f : R ⟶ S) : continuous (f : R → S) := f.2
end Top
structure open_set (X : Top.{u}) : Type u :=
(s : set X.α)
(is_open : topological_space.is_open X.str s)
variables {X : Top.{u}}
namespace open_set
instance : has_coe (open_set X) (set X.α) := { coe := λ U, U.s }
instance : has_subset (open_set X) :=
{ subset := λ U V, U.s ⊆ V.s }
instance : preorder (open_set X) := by refine { le := (⊇), .. } ; tidy
instance open_sets : small_category (open_set X) := by apply_instance
instance : has_mem X.α (open_set X) :=
{ mem := λ a V, a ∈ V.s }
def nbhd (x : X.α) := { U : open_set X // x ∈ U }
def nbhds (x : X.α) : small_category (nbhd x) := begin unfold nbhd, apply_instance end
/-- `open_set.map f` gives the functor from open sets in Y to open set in X,
given by taking preimages under f. -/
def map
{X Y : Top.{u}} (f : X ⟶ Y) : open_set Y ⥤ open_set X :=
{ obj := λ U, ⟨ f.val ⁻¹' U.s, f.property _ U.is_open ⟩,
map' := λ U V i, ⟨ ⟨ λ a b, i.down.down b ⟩ ⟩ }.
@[simp] lemma map_id_obj (X : Top.{u}) (U : open_set X) : map (𝟙 X) U = U :=
begin
cases U, tidy
end
@[simp] def map_id (X : Top.{u}) : map (𝟙 X) ≅ functor.id (open_set X) :=
{ hom := { app := λ U, 𝟙 U },
inv := { app := λ U, 𝟙 U } }
-- We could make f g implicit here, but it's nice to be able to see when they are the identity (often!)
def map_iso {X Y : Top.{u}} (f g : X ⟶ Y) (h : f = g) : map f ≅ map g :=
nat_iso.of_components (λ U, eq_to_iso (congr_fun (congr_arg _ (congr_arg _ h)) _) ) (by obviously)
@[simp] def map_iso_id {X : Top.{u}} (h) : map_iso (𝟙 X) (𝟙 X) h = iso.refl (map _) := rfl
end open_set
end category_theory.examples
|
f0f401479124c3a3e108f0e2bd38b75233d4bcc1 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/abelian/ext.lean | e3a47ba8fcfcdfa191e1602b5b64bd28c12a5f76 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 2,679 | lean | /-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Adam Topaz
-/
import algebra.category.Group.basic
import algebra.category.Module.abelian
import category_theory.functor.left_derived
import category_theory.linear.yoneda
import category_theory.abelian.opposite
import category_theory.abelian.projective
/-!
# Ext
We define `Ext R C n : Cᵒᵖ ⥤ C ⥤ Module R` for any `R`-linear abelian category `C`
by deriving in the first argument of the bifunctor `(X, Y) ↦ Module.of R (unop X ⟶ Y)`.
## Implementation
It's not actually necessary here to assume `C` is abelian,
but the hypotheses, involving both `C` and `Cᵒᵖ`, are quite lengthy,
and in practice the abelian case is hopefully enough.
PROJECT we don't yet have injective resolutions and right derived functors
(although this is only a copy-and-paste dualisation)
so we can't even state the alternative definition
in terms of right-deriving in the first argument,
let alone start the harder project of showing they agree.
-/
noncomputable theory
open category_theory
variables (R : Type*) [ring R] (C : Type*) [category C] [abelian C] [linear R C]
[enough_projectives C]
/--
`Ext R C n` is defined by deriving in the first argument of `(X, Y) ↦ Module.of R (unop X ⟶ Y)`
(which is the second argument of `linear_yoneda`).
-/
@[simps]
def Ext (n : ℕ) : Cᵒᵖ ⥤ C ⥤ Module R :=
functor.flip
{ obj := λ Y, (((linear_yoneda R C).obj Y).right_op.left_derived n).left_op,
map := λ Y Y' f, (nat_trans.left_derived ((linear_yoneda R C).map f).right_op n).left_op,
map_id' := begin
intros X,
ext Y : 2,
dsimp only [nat_trans.id_app, nat_trans.left_op_app,
nat_trans.right_op_app, functor.left_op_obj, functor.right_op_obj],
rw [(linear_yoneda R C).map_id, ← unop_id, nat_trans.right_op_id, nat_trans.left_derived_id],
refl,
end,
map_comp' := begin
intros X Y Z f g,
rw [(linear_yoneda R C).map_comp, nat_trans.right_op_comp, nat_trans.left_derived_comp],
refl,
end }.
open_locale zero_object
/-- If `X : C` is projective and `n : ℕ`, then `Ext^(n + 1) X Y ≅ 0` for any `Y`. -/
def Ext_succ_of_projective (X Y : C) [projective X] (n : ℕ) :
((Ext R C (n+1)).obj (opposite.op X)).obj Y ≅ 0 :=
let E := (((linear_yoneda R C).obj Y).right_op.left_derived_obj_projective_succ n X).unop.symm in
E ≪≫
{ hom := 0,
inv := 0,
hom_inv_id' := begin
let Z : (Module R)ᵒᵖ := 0,
rw [← (0 : 0 ⟶ Z.unop).unop_op, ← (0 : Z.unop ⟶ 0).unop_op,
← unop_id, ← unop_comp],
congr' 1,
dsimp,
dec_trivial,
end }
|
3eadc0db907c242a32a88985546b46ec2ae945ec | 08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4 | /tests/lean/formatTerm.lean | 1500e469b4eca6a610983e0a242508434ac351f3 | [
"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 | 1,121 | lean | import Lean
open Lean PrettyPrinter
def fmt (stx : CoreM Syntax) : CoreM Format := do PrettyPrinter.ppTerm ⟨← stx⟩
#eval fmt `(if c then do t else e)
#eval fmt `(if c then do t; t else e)
#eval fmt `(if c then do t else do e)
#eval fmt `(if let x := c then do t else do e)
#eval fmt `(if c then do t else if c then do t else do e) -- FIXME: make this cascade better?
#eval fmt `(do if c then t else e)
#eval fmt `(do if c then t else if c then t else e)
#eval fmt `(def foo := by
· skip; skip
· skip; skip
skip
(skip; skip)
(skip; skip
try skip; skip
try skip
skip
skip))
#eval fmt `(by
try
skip
skip)
set_option format.indent 3 in
#eval fmt `(by
try
skip
skip)
set_option format.indent 4 in
#eval fmt `(by
try
skip
skip)
set_option format.indent 4 in
#eval fmt `(by
try
skip
skip
skip)
#eval fmt `({
foo := bar
bar := foo + bar
})
#eval fmt `(let x := { foo := bar
bar := foo + bar }
x)
#eval fmt `(command|
def foo : a b c d e f g a b c d e f g h where
h := 42
i := 42
j := 42
k := 42)
|
6f43e2899b98256bc401c4fc77b82da66cc938ab | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /tests/lean/run/synthPending1.lean | d7231a21c0d356938eb03961d9c925a548925558 | [
"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 | 50 | lean |
theorem ex : Not (1 = 2) :=
ofDecideEqFalse rfl
|
3ebe581d56541324a12dcb77a0010b4b86bae8f9 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/measure_theory/covering/vitali.lean | c3ab69ef80710e7d07abccaa8a7eb76d6f51a548 | [
"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 | 29,556 | lean | /-
Copyright (c) 2021 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import topology.metric_space.basic
import measure_theory.constructions.borel_space
import measure_theory.covering.vitali_family
/-!
# Vitali covering theorems
The topological Vitali covering theorem, in its most classical version, states the following.
Consider a family of balls `(B (x_i, r_i))_{i ∈ I}` in a metric space, with uniformly bounded
radii. Then one can extract a disjoint subfamily indexed by `J ⊆ I`, such that any `B (x_i, r_i)`
is included in a ball `B (x_j, 5 r_j)`.
We prove this theorem in `vitali.exists_disjoint_subfamily_covering_enlargment_closed_ball`.
It is deduced from a more general version, called
`vitali.exists_disjoint_subfamily_covering_enlargment`, which applies to any family of sets
together with a size function `δ` (think "radius" or "diameter").
We deduce the measurable Vitali covering theorem. Assume one is given a family `t` of closed sets
with nonempty interior, such that each `a ∈ t` is included in a ball `B (x, r)` and covers a
definite proportion of the ball `B (x, 6 r)` for a given measure `μ` (think of the situation
where `μ` is a doubling measure and `t` is a family of balls). Consider a set `s` at which the
family is fine, i.e., every point of `s` belongs to arbitrarily small elements of `t`. Then one
can extract from `t` a disjoint subfamily that covers almost all `s`. It is proved in
`vitali.exists_disjoint_covering_ae`.
A way to restate this theorem is to say that the set of closed sets `a` with nonempty interior
covering a fixed proportion `1/C` of the ball `closed_ball x (3 * diam a)` forms a Vitali family.
This version is given in `vitali.vitali_family`.
-/
variables {α : Type*}
open set metric measure_theory topological_space filter
open_locale nnreal classical ennreal topological_space
namespace vitali
/-- Vitali covering theorem: given a set `t` of subsets of a type, one may extract a disjoint
subfamily `u` such that the `τ`-enlargment of this family covers all elements of `t`, where `τ > 1`
is any fixed number.
When `t` is a family of balls, the `τ`-enlargment of `ball x r` is `ball x ((1+2τ) r)`. In general,
it is expressed in terms of a function `δ` (think "radius" or "diameter"), positive and bounded on
all elements of `t`. The condition is that every element `a` of `t` should intersect an
element `b` of `u` of size larger than that of `a` up to `τ`, i.e., `δ b ≥ δ a / τ`.
-/
theorem exists_disjoint_subfamily_covering_enlargment
(t : set (set α)) (δ : set α → ℝ) (τ : ℝ) (hτ : 1 < τ) (δnonneg : ∀ a ∈ t, 0 ≤ δ a)
(R : ℝ) (δle : ∀ a ∈ t, δ a ≤ R) (hne : ∀ a ∈ t, set.nonempty a) :
∃ u ⊆ t, u.pairwise_disjoint id ∧
∀ a ∈ t, ∃ b ∈ u, set.nonempty (a ∩ b) ∧ δ a ≤ τ * δ b :=
begin
/- The proof could be formulated as a transfinite induction. First pick an element of `t` with `δ`
as large as possible (up to a factor of `τ`). Then among the remaining elements not intersecting
the already chosen one, pick another element with large `δ`. Go on forever (transfinitely) until
there is nothing left.
Instead, we give a direct Zorn-based argument. Consider a maximal family `u` of disjoint sets
with the following property: if an element `a` of `t` intersects some element `b` of `u`, then it
intersects some `b' ∈ u` with `δ b' ≥ δ a / τ`. Such a maximal family exists by Zorn. If this
family did not intersect some element `a ∈ t`, then take an element `a' ∈ t` which does not
intersect any element of `u`, with `δ a'` almost as large as possible. One checks easily
that `u ∪ {a'}` still has this property, contradicting the maximality. Therefore, `u`
intersects all elements of `t`, and by definition it satisfies all the desired properties.
-/
let T : set (set (set α)) := {u | u ⊆ t ∧ u.pairwise_disjoint id
∧ ∀ a ∈ t, ∀ b ∈ u, set.nonempty (a ∩ b) → ∃ c ∈ u, (a ∩ c).nonempty ∧ δ a ≤ τ * δ c},
-- By Zorn, choose a maximal family in the good set `T` of disjoint families.
obtain ⟨u, uT, hu⟩ : ∃ u ∈ T, ∀ v ∈ T, u ⊆ v → v = u,
{ refine zorn_subset _ (λ U UT hU, _),
refine ⟨⋃₀ U, _, λ s hs, subset_sUnion_of_mem hs⟩,
simp only [set.sUnion_subset_iff, and_imp, exists_prop, forall_exists_index,
set.mem_set_of_eq],
refine ⟨λ u hu, (UT hu).1, (pairwise_disjoint_sUnion hU.directed_on).2 (λ u hu, (UT hu).2.1),
λ a hat b u uU hbu hab, _⟩,
obtain ⟨c, cu, ac, hc⟩ : ∃ (c : set α) (H : c ∈ u), (a ∩ c).nonempty ∧ δ a ≤ τ * δ c :=
(UT uU).2.2 a hat b hbu hab,
exact ⟨c, ⟨u, uU, cu⟩, ac, hc⟩ },
-- the only nontrivial bit is to check that every `a ∈ t` intersects an element `b ∈ u` with
-- comparatively large `δ b`. Assume this is not the case, then we will contradict the maximality.
refine ⟨u, uT.1, uT.2.1, λ a hat, _⟩,
contrapose! hu,
have a_disj : ∀ c ∈ u, disjoint a c,
{ assume c hc,
by_contra,
rw not_disjoint_iff_nonempty_inter at h,
obtain ⟨d, du, ad, hd⟩ : ∃ (d : set α) (H : d ∈ u), (a ∩ d).nonempty ∧ δ a ≤ τ * δ d :=
uT.2.2 a hat c hc h,
exact lt_irrefl _ ((hu d du ad).trans_le hd) },
-- Let `A` be all the elements of `t` which do not intersect the family `u`. It is nonempty as it
-- contains `a`. We will pick an element `a'` of `A` with `δ a'` almost as large as possible.
let A := {a' | a' ∈ t ∧ ∀ c ∈ u, disjoint a' c},
have Anonempty : A.nonempty := ⟨a, hat, a_disj⟩,
let m := Sup (δ '' A),
have bddA : bdd_above (δ '' A),
{ refine ⟨R, λ x xA, _⟩,
rcases (mem_image _ _ _).1 xA with ⟨a', ha', rfl⟩,
exact δle a' ha'.1 },
obtain ⟨a', a'A, ha'⟩ : ∃ a' ∈ A, m / τ ≤ δ a',
{ have : 0 ≤ m := (δnonneg a hat).trans (le_cSup bddA (mem_image_of_mem _ ⟨hat, a_disj⟩)),
rcases eq_or_lt_of_le this with mzero|mpos,
{ refine ⟨a, ⟨hat, a_disj⟩, _⟩,
simpa only [← mzero, zero_div] using δnonneg a hat },
{ have I : m / τ < m,
{ rw div_lt_iff (zero_lt_one.trans hτ),
conv_lhs { rw ← mul_one m },
exact (mul_lt_mul_left mpos).2 hτ },
rcases exists_lt_of_lt_cSup (nonempty_image_iff.2 Anonempty) I with ⟨x, xA, hx⟩,
rcases (mem_image _ _ _).1 xA with ⟨a', ha', rfl⟩,
exact ⟨a', ha', hx.le⟩, } },
clear hat hu a_disj a,
have a'_ne_u : a' ∉ u := λ H, (hne _ a'A.1).ne_empty (disjoint_self.1 (a'A.2 _ H)),
-- we claim that `u ∪ {a'}` still belongs to `T`, contradicting the maximality of `u`.
refine ⟨insert a' u, ⟨_, _, _⟩, subset_insert _ _, (ne_insert_of_not_mem _ a'_ne_u).symm⟩,
-- check that `u ∪ {a'}` is made of elements of `t`.
{ rw insert_subset,
exact ⟨a'A.1, uT.1⟩ },
-- check that `u ∪ {a'}` is a disjoint family. This follows from the fact that `a'` does not
-- intersect `u`.
{ exact uT.2.1.insert (λ b bu ba', a'A.2 b bu) },
-- check that every element `c` of `t` intersecting `u ∪ {a'}` intersects an element of this
-- family with large `δ`.
{ assume c ct b ba'u hcb,
-- if `c` already intersects an element of `u`, then it intersects an element of `u` with
-- large `δ` by the assumption on `u`, and there is nothing left to do.
by_cases H : ∃ d ∈ u, set.nonempty (c ∩ d),
{ rcases H with ⟨d, du, hd⟩,
rcases uT.2.2 c ct d du hd with ⟨d', d'u, hd'⟩,
exact ⟨d', mem_insert_of_mem _ d'u, hd'⟩ },
-- otherwise, `c` belongs to `A`. The element of `u ∪ {a'}` that it intersects has to be `a'`.
-- moreover, `δ c` is smaller than the maximum `m` of `δ` over `A`, which is `≤ δ a' / τ`
-- thanks to the good choice of `a'`. This is the desired inequality.
{ push_neg at H,
simp only [← not_disjoint_iff_nonempty_inter, not_not] at H,
rcases mem_insert_iff.1 ba'u with rfl|H',
{ refine ⟨b, mem_insert _ _, hcb, _⟩,
calc δ c ≤ m : le_cSup bddA (mem_image_of_mem _ ⟨ct, H⟩)
... = τ * (m / τ) : by { field_simp [(zero_lt_one.trans hτ).ne'], ring }
... ≤ τ * δ b : mul_le_mul_of_nonneg_left ha' (zero_le_one.trans hτ.le) },
{ rw ← not_disjoint_iff_nonempty_inter at hcb,
exact (hcb (H _ H')).elim } } }
end
/-- Vitali covering theorem, closed balls version: given a family `t` of closed balls, one can
extract a disjoint subfamily `u ⊆ t` so that all balls in `t` are covered by the 5-times
dilations of balls in `u`. -/
theorem exists_disjoint_subfamily_covering_enlargment_closed_ball [metric_space α]
(t : set (set α)) (R : ℝ) (ht : ∀ s ∈ t, ∃ x r, s = closed_ball x r ∧ r ≤ R) :
∃ u ⊆ t, u.pairwise_disjoint id ∧
∀ a ∈ t, ∃ x r, closed_ball x r ∈ u ∧ a ⊆ closed_ball x (5 * r) :=
begin
rcases eq_empty_or_nonempty t with rfl|tnonempty,
{ exact ⟨∅, subset.refl _, pairwise_disjoint_empty, by simp⟩ },
haveI : inhabited α,
{ choose s hst using tnonempty,
choose x r hxr using ht s hst,
exact ⟨x⟩ },
-- Exclude the trivial case where `t` is reduced to the empty set.
rcases eq_or_ne t {∅} with rfl|t_ne_empty,
{ refine ⟨{∅}, subset.refl _, _⟩,
simp only [true_and, closed_ball_eq_empty, mem_singleton_iff, and_true, empty_subset, forall_eq,
pairwise_disjoint_singleton, exists_const],
exact ⟨-1, by simp only [right.neg_neg_iff, zero_lt_one]⟩ },
-- The real proof starts now. Since the center or the radius of a ball is not uniquely defined
-- in a general metric space, we just choose one for definiteness.
choose! x r hxr using ht,
have r_nonneg : ∀ (a : set α), a ∈ t → a.nonempty → 0 ≤ r a,
{ assume a hat a_nonempty,
rw (hxr a hat).1 at a_nonempty,
simpa only [nonempty_closed_ball] using a_nonempty },
-- The difference with the generic version is that we are not excluding empty sets in our family
-- (which would correspond to `r a < 0`). To compensate for this, we apply the generic version
-- to the subfamily `t'` made of nonempty sets, and we use `δ = r` there. This gives a disjointed
-- subfamily `u'`.
let t' := {a ∈ t | 0 ≤ r a},
obtain ⟨u', u't', u'_disj, hu'⟩ : ∃ u' ⊆ t', u'.pairwise_disjoint id ∧
∀ a ∈ t', ∃ b ∈ u', set.nonempty (a ∩ b) ∧ r a ≤ 2 * r b,
{ refine exists_disjoint_subfamily_covering_enlargment t' r 2 one_lt_two
(λ a ha, ha.2) R (λ a ha, (hxr a ha.1).2) (λ a ha, _),
rw [(hxr a ha.1).1],
simp only [ha.2, nonempty_closed_ball] },
-- this subfamily is nonempty, as we have excluded the situation `t = {∅}`.
have u'_nonempty : u'.nonempty,
{ have : ∃ a ∈ t, a ≠ ∅,
{ contrapose! t_ne_empty,
apply subset.antisymm,
{ simpa only using t_ne_empty },
{ rcases tnonempty with ⟨a, hat⟩,
have := t_ne_empty a hat,
simpa only [this, singleton_subset_iff] using hat } },
rcases this with ⟨a, hat, a_nonempty⟩,
have ranonneg : 0 ≤ r a := r_nonneg a hat (ne_empty_iff_nonempty.1 a_nonempty),
rcases hu' a ⟨hat, ranonneg⟩ with ⟨b, bu', hb⟩,
exact ⟨b, bu'⟩ },
-- check that the family `u'` gives the desired disjoint covering.
refine ⟨u', λ a ha, (u't' ha).1, u'_disj, λ a hat, _⟩,
-- it remains to check that any ball in `t` is contained in the 5-dilation of a ball
-- in `u'`. This depends on whether the ball is empty of not.
rcases eq_empty_or_nonempty a with rfl|a_nonempty,
-- if the ball is empty, use any element of `u'` (since we know that `u'` is nonempty).
{ rcases u'_nonempty with ⟨b, hb⟩,
refine ⟨x b, r b, _, empty_subset _⟩,
rwa ← (hxr b (u't' hb).1).1 },
-- if the ball is not empty, it belongs to `t'`. Then it intersects a ball `a'` in `u'` with
-- controlled radius, by definition of `u'`. It is straightforward to check that this ball
-- satisfies all the desired properties.
{ have hat' : a ∈ t' := ⟨hat, r_nonneg a hat a_nonempty⟩,
obtain ⟨a', a'u', aa', raa'⟩ :
(∃ (a' : set α) (H : a' ∈ u'), (a ∩ a').nonempty ∧ r a ≤ 2 * r a') := hu' a hat',
refine ⟨x a', r a', _, _⟩,
{ convert a'u',
exact (hxr a' (u't' a'u').1).1.symm },
{ rw (hxr a hat'.1).1 at aa' ⊢,
rw (hxr a' (u't' a'u').1).1 at aa',
have : dist (x a) (x a') ≤ r a + r a' :=
dist_le_add_of_nonempty_closed_ball_inter_closed_ball aa',
apply closed_ball_subset_closed_ball',
linarith } }
end
/-- The measurable Vitali covering theorem. Assume one is given a family `t` of closed sets with
nonempty interior, such that each `a ∈ t` is included in a ball `B (x, r)` and covers a definite
proportion of the ball `B (x, 6 r)` for a given measure `μ` (think of the situation where `μ` is
a doubling measure and `t` is a family of balls). Consider a (possible non-measurable) set `s`
at which the family is fine, i.e., every point of `s` belongs to arbitrarily small elements of `t`.
Then one can extract from `t` a disjoint subfamily that covers almost all `s`. -/
theorem exists_disjoint_covering_ae [metric_space α] [measurable_space α] [opens_measurable_space α]
[second_countable_topology α]
(μ : measure α) [is_locally_finite_measure μ] (s : set α)
(t : set (set α)) (hf : ∀ x ∈ s, ∀ (ε > (0 : ℝ)), ∃ a ∈ t, x ∈ a ∧ a ⊆ closed_ball x ε)
(ht : ∀ a ∈ t, (interior a).nonempty) (h't : ∀ a ∈ t, is_closed a)
(C : ℝ≥0) (h : ∀ a ∈ t, ∃ x ∈ a, μ (closed_ball x (3 * diam a)) ≤ C * μ a) :
∃ u ⊆ t, countable u ∧ u.pairwise_disjoint id ∧ μ (s \ ⋃ (a ∈ u), a) = 0 :=
begin
/- The idea of the proof is the following. Assume for simplicity that `μ` is finite. Applying the
abstract Vitali covering theorem with `δ = diam`, one obtains a disjoint subfamily `u`, such
that any element of `t` intersects an element of `u` with comparable diameter. Fix `ε > 0`.
Since the elements of `u` have summable measure, one can remove finitely elements `w_1, ..., w_n`.
so that the measure of the remaining elements is `< ε`. Consider now a point `z` not
in the `w_i`. There is a small ball around `z` not intersecting the `w_i` (as they are closed),
an element `a ∈ t` contained in this small ball (as the family `t` is fine at `z`) and an element
`b ∈ u` intersecting `a`, with comparable diameter (by definition of `u`). Then `z` belongs to the
enlargement of `b`. This shows that `s \ (w_1 ∪ ... ∪ w_n)` is contained in
`⋃ (b ∈ u \ {w_1, ... w_n}) (enlargement of b)`. The measure of the latter set is bounded by
`∑ (b ∈ u \ {w_1, ... w_n}) C * μ b` (by the doubling property of the measure), which is at most
`C ε`. Letting `ε` tend to `0` shows that `s` is almost everywhere covered by the family `u`.
For the real argument, the measure is only locally finite. Therefore, we implement the same
strategy, but locally restricted to balls on which the measure is finite. For this, we do not
use the whole family `t`, but a subfamily `t'` supported on small balls (which is possible since
the family is assumed to be fine at every point of `s`).
-/
rcases eq_empty_or_nonempty s with rfl|nonempty,
{ refine ⟨∅, empty_subset _, countable_empty, pairwise_disjoint_empty,
by simp only [measure_empty, Union_false, Union_empty, diff_self]⟩ },
haveI : inhabited α,
{ choose x hx using nonempty,
exact ⟨x⟩ },
-- choose around each `x` a small ball on which the measure is finite
have : ∀ x, ∃ r, 0 < r ∧ r ≤ 1 ∧ μ (closed_ball x (20 * r)) < ∞,
{ assume x,
obtain ⟨R, Rpos, μR⟩ : ∃ (R : ℝ) (hR : 0 < R), μ (closed_ball x R) < ∞ :=
(μ.finite_at_nhds x).exists_mem_basis nhds_basis_closed_ball,
refine ⟨min 1 (R/20), _, min_le_left _ _, _⟩,
{ simp only [true_and, lt_min_iff, zero_lt_one],
linarith },
{ apply lt_of_le_of_lt (measure_mono _) μR,
apply closed_ball_subset_closed_ball,
calc 20 * min 1 (R / 20) ≤ 20 * (R/20) :
mul_le_mul_of_nonneg_left (min_le_right _ _) (by norm_num)
... = R : by ring } },
choose r hr0 hr1 hrμ,
-- we restrict to a subfamily `t'` of `t`, made of elements small enough to ensure that
-- they only see a finite part of the measure.
let t' := {a ∈ t | ∃ x, a ⊆ closed_ball x (r x)},
-- extract a disjoint subfamily `u` of `t'` thanks to the abstract Vitali covering theorem.
obtain ⟨u, ut', u_disj, hu⟩ : ∃ u ⊆ t', u.pairwise_disjoint id ∧
∀ a ∈ t', ∃ b ∈ u, set.nonempty (a ∩ b) ∧ diam a ≤ 2 * diam b,
{ have A : ∀ (a : set α), a ∈ t' → diam a ≤ 2,
{ rintros a ⟨hat, ⟨x, hax⟩⟩,
calc diam a ≤ 2 * 1 : diam_le_of_subset_closed_ball zero_le_one
(hax.trans $ closed_ball_subset_closed_ball $ hr1 x)
... = 2 : mul_one _ },
have B : ∀ (a : set α), a ∈ t' → a.nonempty :=
λ a hat', set.nonempty.mono interior_subset (ht a hat'.1),
exact exists_disjoint_subfamily_covering_enlargment t' diam 2 one_lt_two
(λ a ha, diam_nonneg) 2 A B },
have ut : u ⊆ t := λ a hau, (ut' hau).1,
-- As the space is second countable, the family is countable since all its sets have nonempty
-- interior.
have u_count : countable u :=
u_disj.countable_of_nonempty_interior (λ a ha, ht a (ut ha)),
-- the family `u` will be the desired family
refine ⟨u, λ a hat', (ut' hat').1, u_count, u_disj, _⟩,
-- it suffices to show that it covers almost all `s` locally around each point `x`.
refine null_of_locally_null _ (λ x hx, _),
-- let `v` be the subfamily of `u` made of those sets intersecting the small ball `ball x (r x)`
let v := {a ∈ u | (a ∩ ball x (r x)).nonempty },
have vu : v ⊆ u := λ a ha, ha.1,
-- they are all contained in a fixed ball of finite measure, thanks to our choice of `t'`
obtain ⟨R, μR, hR⟩ : ∃ R, μ (closed_ball x R) < ∞ ∧
∀ a ∈ u, (a ∩ ball x (r x)).nonempty → a ⊆ closed_ball x R,
{ have : ∀ a ∈ u, ∃ y, a ⊆ closed_ball y (r y) := λ a hau, (ut' hau).2,
choose! y hy using this,
have Idist_v : ∀ a ∈ v, dist (y a) x ≤ r (y a) + r x,
{ assume a hav,
apply dist_le_add_of_nonempty_closed_ball_inter_closed_ball,
exact hav.2.mono (inter_subset_inter (hy a hav.1) ball_subset_closed_ball) },
set R0 := Sup ((λ a, r (y a)) '' v) with hR0,
have R0_bdd : bdd_above ((λ a, r (y a)) '' v),
{ refine ⟨1, λ r' hr', _⟩,
rcases (mem_image _ _ _).1 hr' with ⟨b, hb, rfl⟩,
exact hr1 _ },
rcases le_total R0 (r x) with H|H,
{ refine ⟨20 * r x, hrμ x, λ a au hax, _⟩,
refine (hy a au).trans _,
apply closed_ball_subset_closed_ball',
have : r (y a) ≤ R0 := le_cSup R0_bdd (mem_image_of_mem _ ⟨au, hax⟩),
linarith [(hr0 (y a)).le, (hr0 x).le, Idist_v a ⟨au, hax⟩] },
{ have R0pos : 0 < R0 := (hr0 x).trans_le H,
have vnonempty : v.nonempty,
{ by_contra,
rw [← ne_empty_iff_nonempty, not_not] at h,
simp only [h, real.Sup_empty, image_empty] at hR0,
exact lt_irrefl _ (R0pos.trans_le (le_of_eq hR0)) },
obtain ⟨a, hav, R0a⟩ : ∃ a ∈ v, R0/2 < r (y a),
{ obtain ⟨r', r'mem, hr'⟩ : ∃ r' ∈ ((λ a, r (y a)) '' v), R0 / 2 < r' :=
exists_lt_of_lt_cSup (nonempty_image_iff.2 vnonempty) (half_lt_self R0pos),
rcases (mem_image _ _ _).1 r'mem with ⟨a, hav, rfl⟩,
exact ⟨a, hav, hr'⟩ },
refine ⟨8 * R0, _, _⟩,
{ apply lt_of_le_of_lt (measure_mono _) (hrμ (y a)),
apply closed_ball_subset_closed_ball',
rw dist_comm,
linarith [Idist_v a hav] },
{ assume b bu hbx,
refine (hy b bu).trans _,
apply closed_ball_subset_closed_ball',
have : r (y b) ≤ R0 := le_cSup R0_bdd (mem_image_of_mem _ ⟨bu, hbx⟩),
linarith [Idist_v b ⟨bu, hbx⟩] } } },
-- we will show that, in `ball x (r x)`, almost all `s` is covered by the family `u`.
refine ⟨_ ∩ ball x (r x), inter_mem_nhds_within _ (ball_mem_nhds _ (hr0 _)),
nonpos_iff_eq_zero.mp (le_of_forall_le_of_dense (λ ε εpos, _))⟩,
-- the elements of `v` are disjoint and all contained in a finite volume ball, hence the sum
-- of their measures is finite.
have I : ∑' (a : v), μ a < ∞,
{ calc ∑' (a : v), μ a = μ (⋃ (a ∈ v), a) : begin
rw measure_bUnion (u_count.mono vu) _ (λ a ha, (h't _ (vu.trans ut ha)).measurable_set),
exact u_disj.subset vu
end
... ≤ μ (closed_ball x R) : measure_mono (Union₂_subset (λ a ha, hR a (vu ha) ha.2))
... < ∞ : μR },
-- we can obtain a finite subfamily of `v`, such that the measures of the remaining elements
-- add up to an arbitrarily small number, say `ε / C`.
obtain ⟨w, hw⟩ : ∃ (w : finset ↥v), ∑' (a : {a // a ∉ w}), μ a < ε / C,
{ haveI : ne_bot (at_top : filter (finset v)) := at_top_ne_bot,
have : 0 < ε / C, by simp only [ennreal.div_pos_iff, εpos.ne', ennreal.coe_ne_top, ne.def,
not_false_iff, and_self],
exact ((tendsto_order.1 (ennreal.tendsto_tsum_compl_at_top_zero I.ne)).2 _ this).exists },
choose! y hy using h,
-- main property: the points `z` of `s` which are not covered by `u` are contained in the
-- enlargements of the elements not in `w`.
have M : (s \ ⋃ (a : set α) (H : a ∈ u), a) ∩ ball x (r x)
⊆ ⋃ (a : {a // a ∉ w}), closed_ball (y a) (3 * diam (a : set α)),
{ assume z hz,
set k := ⋃ (a : v) (ha : a ∈ w), (a : set α) with hk,
have k_closed : is_closed k :=
is_closed_bUnion w.finite_to_set (λ i hi, h't _ (ut (vu i.2))),
have z_notmem_k : z ∉ k,
{ simp only [not_exists, exists_prop, mem_Union, mem_sep_eq, forall_exists_index,
set_coe.exists, not_and, exists_and_distrib_right, subtype.coe_mk],
assume b hbv h'b h'z,
have : z ∈ (s \ ⋃ (a : set α) (H : a ∈ u), a) ∩ (⋃ (a : set α) (H : a ∈ u), a) :=
mem_inter (mem_of_mem_inter_left hz) (mem_bUnion (vu hbv) h'z),
simpa only [diff_inter_self] },
-- since the elements of `w` are closed and finitely many, one can find a small ball around `z`
-- not intersecting them
have : ball x (r x) \ k ∈ 𝓝 z,
{ apply is_open.mem_nhds (is_open_ball.sdiff k_closed) _,
exact (mem_diff _).2 ⟨mem_of_mem_inter_right hz, z_notmem_k⟩ },
obtain ⟨d, dpos, hd⟩ : ∃ (d : ℝ) (dpos : 0 < d), closed_ball z d ⊆ ball x (r x) \ k :=
nhds_basis_closed_ball.mem_iff.1 this,
-- choose an element `a` of the family `t` contained in this small ball
obtain ⟨a, hat, za, ad⟩ : ∃ a ∈ t, z ∈ a ∧ a ⊆ closed_ball z d :=
hf z ((mem_diff _).1 (mem_of_mem_inter_left hz)).1 d dpos,
have ax : a ⊆ ball x (r x) := ad.trans (hd.trans (diff_subset (ball x (r x)) k)),
-- it intersects an element `b` of `u` with comparable diameter, by definition of `u`
obtain ⟨b, bu, ab, bdiam⟩ : ∃ (b : set α) (H : b ∈ u), (a ∩ b).nonempty ∧ diam a ≤ 2 * diam b :=
hu a ⟨hat, ⟨x, ax.trans ball_subset_closed_ball⟩⟩,
have bv : b ∈ v,
{ refine ⟨bu, ab.mono _⟩,
rw inter_comm,
exact inter_subset_inter_right _ ax },
let b' : v := ⟨b, bv⟩,
-- `b` can not belong to `w`, as the elements of `w` do not intersect `closed_ball z d`,
-- contrary to `b`
have b'_notmem_w : b' ∉ w,
{ assume b'w,
have b'k : (b' : set α) ⊆ k := finset.subset_set_bUnion_of_mem b'w,
have : ((ball x (r x) \ k) ∩ k).nonempty := ab.mono (inter_subset_inter (ad.trans hd) b'k),
simpa only [diff_inter_self, not_nonempty_empty] },
let b'' : {a // a ∉ w} := ⟨b', b'_notmem_w⟩,
-- since `a` and `b` have comparable diameters, it follows that `z` belongs to the
-- enlargement of `b`
have zb : z ∈ closed_ball (y b) (3 * diam b),
{ rcases ab with ⟨e, ⟨ea, eb⟩⟩,
have A : dist z e ≤ diam a := dist_le_diam_of_mem (bounded_closed_ball.mono ad) za ea,
have B : dist e (y b) ≤ diam b,
{ rcases (ut' bu).2 with ⟨c, hc⟩,
apply dist_le_diam_of_mem (bounded_closed_ball.mono hc) eb (hy b (ut bu)).1 },
simp only [mem_closed_ball],
linarith [dist_triangle z e (y b)] },
suffices H : closed_ball (y (b'' : set α)) (3 * diam (b'' : set α))
⊆ ⋃ (a : {a // a ∉ w}), closed_ball (y (a : set α)) (3 * diam (a : set α)), from H zb,
exact subset_Union (λ (a : {a // a ∉ w}), closed_ball (y a) (3 * diam (a : set α))) b'' },
-- now that we have proved our main inclusion, we can use it to estimate the measure of the points
-- in `ball x (r x)` not covered by `u`.
haveI : encodable v := (u_count.mono vu).to_encodable,
calc μ ((s \ ⋃ (a : set α) (H : a ∈ u), a) ∩ ball x (r x))
≤ μ (⋃ (a : {a // a ∉ w}), closed_ball (y a) (3 * diam (a : set α))) : measure_mono M
... ≤ ∑' (a : {a // a ∉ w}), μ (closed_ball (y a) (3 * diam (a : set α))) : measure_Union_le _
... ≤ ∑' (a : {a // a ∉ w}), C * μ a : ennreal.tsum_le_tsum (λ a, (hy a (ut (vu a.1.2))).2)
... = C * ∑' (a : {a // a ∉ w}), μ a : ennreal.tsum_mul_left
... ≤ C * (ε / C) : ennreal.mul_le_mul le_rfl hw.le
... ≤ ε : ennreal.mul_div_le,
end
/-- Assume that around every point there are arbitrarily small scales at which the measure is
doubling. Then the set of closed sets `a` with nonempty interior covering a fixed proportion `1/C`
of the ball `closed_ball x (3 * diam a)` forms a Vitali family. This is essentially a restatement
of the measurable Vitali theorem. -/
protected def vitali_family [metric_space α] [measurable_space α] [opens_measurable_space α]
[second_countable_topology α] (μ : measure α) [is_locally_finite_measure μ] (C : ℝ≥0)
(h : ∀ x (ε > 0), ∃ r ∈ Ioc (0 : ℝ) ε, μ (closed_ball x (6 * r)) ≤ C * μ (closed_ball x r)) :
vitali_family μ :=
{ sets_at := λ x, {a | x ∈ a ∧ is_closed a ∧ (interior a).nonempty ∧
μ (closed_ball x (3 * diam a)) ≤ C * μ a},
measurable_set' := λ x a ha, ha.2.1.measurable_set,
nonempty_interior := λ x a ha, ha.2.2.1,
nontrivial := λ x ε εpos, begin
obtain ⟨r, ⟨rpos, rε⟩, μr⟩ : ∃ r ∈ Ioc (0 : ℝ) ε,
μ (closed_ball x (6 * r)) ≤ C * μ (closed_ball x r) := h x ε εpos,
refine ⟨closed_ball x r, ⟨_, is_closed_ball, _, _⟩, closed_ball_subset_closed_ball rε⟩,
{ simp only [rpos.le, mem_closed_ball, dist_self] },
{ exact (nonempty_ball.2 rpos).mono (ball_subset_interior_closed_ball) },
{ apply le_trans (measure_mono (closed_ball_subset_closed_ball _)) μr,
have : diam (closed_ball x r) ≤ 2 * r := diam_closed_ball rpos.le,
linarith }
end,
covering := begin
assume s f fsubset ffine,
rcases eq_empty_or_nonempty s with rfl|H,
{ exact ⟨∅, λ _, ∅, by simp, by simp⟩ },
haveI : inhabited α, { choose x hx using H, exact ⟨x⟩ },
let t := ⋃ (x ∈ s), f x,
have A₁ : ∀ x ∈ s, ∀ (ε : ℝ), 0 < ε → (∃ a ∈ t, x ∈ a ∧ a ⊆ closed_ball x ε),
{ assume x xs ε εpos,
rcases ffine x xs ε εpos with ⟨a, xa, hax⟩,
exact ⟨a, mem_bUnion xs xa, (fsubset x xs xa).1, hax⟩ },
have A₂ : ∀ a ∈ t, (interior a).nonempty,
{ rintros a ha,
rcases mem_Union₂.1 ha with ⟨x, xs, xa⟩,
exact (fsubset x xs xa).2.2.1 },
have A₃ : ∀ a ∈ t, is_closed a,
{ rintros a ha,
rcases mem_Union₂.1 ha with ⟨x, xs, xa⟩,
exact (fsubset x xs xa).2.1 },
have A₄ : ∀ a ∈ t, ∃ x ∈ a, μ (closed_ball x (3 * diam a)) ≤ C * μ a,
{ rintros a ha,
rcases mem_Union₂.1 ha with ⟨x, xs, xa⟩,
exact ⟨x, (fsubset x xs xa).1, (fsubset x xs xa).2.2.2⟩ },
obtain ⟨u, ut, u_count, u_disj, μu⟩ :
∃ u ⊆ t, u.countable ∧ u.pairwise disjoint ∧ μ (s \ ⋃ a ∈ u, a) = 0 :=
exists_disjoint_covering_ae μ s t A₁ A₂ A₃ C A₄,
have : ∀ a ∈ u, ∃ x ∈ s, a ∈ f x := λ a ha, mem_Union₂.1 (ut ha),
choose! x hx using this,
have inj_on_x : inj_on x u,
{ assume a ha b hb hab,
have A : (a ∩ b).nonempty,
{ refine ⟨x a, mem_inter ((fsubset _ (hx a ha).1 (hx a ha).2).1) _⟩,
rw hab,
exact (fsubset _ (hx b hb).1 (hx b hb).2).1 },
contrapose A,
have : disjoint a b := u_disj ha hb A,
simpa only [← not_disjoint_iff_nonempty_inter] },
refine ⟨x '' u, function.inv_fun_on x u, _, _, _, _⟩,
{ assume y hy,
rcases (mem_image _ _ _).1 hy with ⟨a, au, rfl⟩,
exact (hx a au).1 },
{ rw [inj_on_x.pairwise_disjoint_image],
assume a ha b hb hab,
simp only [function.on_fun, inj_on_x.left_inv_on_inv_fun_on ha,
inj_on_x.left_inv_on_inv_fun_on hb, (∘)],
exact u_disj ha hb hab },
{ assume y hy,
rcases (mem_image _ _ _).1 hy with ⟨a, ha, rfl⟩,
rw inj_on_x.left_inv_on_inv_fun_on ha,
exact (hx a ha).2 },
{ rw [bUnion_image],
convert μu using 3,
exact Union₂_congr (λ a ha, inj_on_x.left_inv_on_inv_fun_on ha) }
end }
end vitali
|
bdabc6b972b6ede02ae7b49865edc2cb6080b6bc | 95dcf8dea2baf2b4b0a60d438f27c35ae3dd3990 | /src/algebra/module.lean | d1a96e1fcbbd9f980f15493b9f2607dc713275ee | [
"Apache-2.0"
] | permissive | uniformity1/mathlib | 829341bad9dfa6d6be9adaacb8086a8a492e85a4 | dd0e9bd8f2e5ec267f68e72336f6973311909105 | refs/heads/master | 1,588,592,015,670 | 1,554,219,842,000 | 1,554,219,842,000 | 179,110,702 | 0 | 0 | Apache-2.0 | 1,554,220,076,000 | 1,554,220,076,000 | null | UTF-8 | Lean | false | false | 13,287 | lean | /-
Copyright (c) 2015 Nathaniel Thomas. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro
Modules over a ring.
-/
import algebra.ring algebra.big_operators group_theory.subgroup group_theory.group_action
open function
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x}
-- /-- Typeclass for types with a scalar multiplication operation, denoted `•` (`\bu`) -/
-- class has_scalar (α : Type u) (γ : Type v) := (smul : α → γ → γ)
-- infixr ` • `:73 := has_scalar.smul
/-- A semimodule is a generalization of vector spaces to a scalar semiring.
It consists of a scalar semiring `α` and an additive monoid of "vectors" `β`,
connected by a "scalar multiplication" operation `r • x : β`
(where `r : α` and `x : β`) with some natural associativity and
distributivity axioms similar to those on a ring. -/
class semimodule (α : Type u) (β : Type v) [semiring α]
[add_comm_monoid β] extends distrib_mul_action α β :=
(add_smul : ∀(r s : α) (x : β), (r + s) • x = r • x + s • x)
(zero_smul : ∀x : β, (0 : α) • x = 0)
section semimodule
variables [R:semiring α] [add_comm_monoid β] [semimodule α β] (r s : α) (x y : β)
include R
theorem add_smul : (r + s) • x = r • x + s • x := semimodule.add_smul r s x
variables (α)
@[simp] theorem zero_smul : (0 : α) • x = 0 := semimodule.zero_smul α x
lemma smul_smul : r • s • x = (r * s) • x := (mul_smul _ _ _).symm
instance smul.is_add_monoid_hom {r : α} : is_add_monoid_hom (λ x : β, r • x) :=
by refine_struct {..}; simp [smul_add]
end semimodule
/-- A module is a generalization of vector spaces to a scalar ring.
It consists of a scalar ring `α` and an additive group of "vectors" `β`,
connected by a "scalar multiplication" operation `r • x : β`
(where `r : α` and `x : β`) with some natural associativity and
distributivity axioms similar to those on a ring. -/
class module (α : Type u) (β : Type v) [ring α] [add_comm_group β] extends semimodule α β
structure module.core (α β) [ring α] [add_comm_group β] extends has_scalar α β :=
(smul_add : ∀(r : α) (x y : β), r • (x + y) = r • x + r • y)
(add_smul : ∀(r s : α) (x : β), (r + s) • x = r • x + s • x)
(mul_smul : ∀(r s : α) (x : β), (r * s) • x = r • s • x)
(one_smul : ∀x : β, (1 : α) • x = x)
def module.of_core {α β} [ring α] [add_comm_group β] (M : module.core α β) : module α β :=
by letI := M.to_has_scalar; exact
{ zero_smul := λ x,
have (0 : α) • x + (0 : α) • x = (0 : α) • x + 0, by rw ← M.add_smul; simp,
add_left_cancel this,
smul_zero := λ r,
have r • (0:β) + r • 0 = r • 0 + 0, by rw ← M.smul_add; simp,
add_left_cancel this,
..M }
section module
variables [ring α] [add_comm_group β] [module α β] (r s : α) (x y : β)
@[simp] theorem neg_smul : -r • x = - (r • x) :=
eq_neg_of_add_eq_zero (by rw [← add_smul, add_left_neg, zero_smul])
variables (α)
theorem neg_one_smul (x : β) : (-1 : α) • x = -x := by simp
variables {α}
@[simp] theorem smul_neg : r • (-x) = -(r • x) :=
by rw [← neg_one_smul α, ← mul_smul, mul_neg_one, neg_smul]
theorem smul_sub (r : α) (x y : β) : r • (x - y) = r • x - r • y :=
by simp [smul_add]; rw smul_neg
theorem sub_smul (r s : α) (y : β) : (r - s) • y = r • y - s • y :=
by simp [add_smul]
end module
instance semiring.to_semimodule [r : semiring α] : semimodule α α :=
{ smul := (*),
smul_add := mul_add,
add_smul := add_mul,
mul_smul := mul_assoc,
one_smul := one_mul,
zero_smul := zero_mul,
smul_zero := mul_zero, ..r }
@[simp] lemma smul_eq_mul [semiring α] {a a' : α} : a • a' = a * a' := rfl
instance ring.to_module [r : ring α] : module α α :=
{ ..semiring.to_semimodule }
class is_linear_map (α : Type u) {β : Type v} {γ : Type w}
[ring α] [add_comm_group β] [add_comm_group γ] [module α β] [module α γ]
(f : β → γ) : Prop :=
(add : ∀x y, f (x + y) = f x + f y)
(smul : ∀(c : α) x, f (c • x) = c • f x)
structure linear_map (α : Type u) (β : Type v) (γ : Type w)
[ring α] [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] :=
(to_fun : β → γ)
(add : ∀x y, to_fun (x + y) = to_fun x + to_fun y)
(smul : ∀(c : α) x, to_fun (c • x) = c • to_fun x)
infixr ` →ₗ `:25 := linear_map _
notation β ` →ₗ[`:25 α `] ` γ := linear_map α β γ
namespace linear_map
variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ]
variables [module α β] [module α γ] [module α δ]
variables (f g : β →ₗ[α] γ)
include α
instance : has_coe_to_fun (β →ₗ[α] γ) := ⟨_, to_fun⟩
theorem is_linear : is_linear_map α f := {..f}
@[extensionality] theorem ext {f g : β →ₗ[α] γ} (H : ∀ x, f x = g x) : f = g :=
by cases f; cases g; congr'; exact funext H
theorem ext_iff {f g : β →ₗ[α] γ} : f = g ↔ ∀ x, f x = g x :=
⟨by rintro rfl; simp, ext⟩
@[simp] lemma map_add (x y : β) : f (x + y) = f x + f y := f.add x y
@[simp] lemma map_smul (c : α) (x : β) : f (c • x) = c • f x := f.smul c x
@[simp] lemma map_zero : f 0 = 0 :=
by rw [← zero_smul α, map_smul f 0 0, zero_smul]
instance : is_add_group_hom f := ⟨map_add f⟩
@[simp] lemma map_neg (x : β) : f (- x) = - f x :=
by rw [← neg_one_smul α, map_smul, neg_one_smul]
@[simp] lemma map_sub (x y : β) : f (x - y) = f x - f y :=
by simp [map_neg, map_add]
@[simp] lemma map_sum {ι} {t : finset ι} {g : ι → β} :
f (t.sum g) = t.sum (λi, f (g i)) :=
(finset.sum_hom f).symm
def comp (f : γ →ₗ[α] δ) (g : β →ₗ[α] γ) : β →ₗ[α] δ := ⟨f ∘ g, by simp, by simp⟩
@[simp] lemma comp_apply (f : γ →ₗ[α] δ) (g : β →ₗ[α] γ) (x : β) : f.comp g x = f (g x) := rfl
def id : β →ₗ[α] β := ⟨id, by simp, by simp⟩
@[simp] lemma id_apply (x : β) : @id α β _ _ _ x = x := rfl
end linear_map
namespace is_linear_map
variables [ring α] [add_comm_group β] [add_comm_group γ]
variables [module α β] [module α γ]
include α
def mk' (f : β → γ) (H : is_linear_map α f) : β →ₗ γ := {to_fun := f, ..H}
@[simp] theorem mk'_apply {f : β → γ} (H : is_linear_map α f) (x : β) :
mk' f H x = f x := rfl
lemma is_linear_map_neg :
is_linear_map α (λ (z : β), -z) :=
is_linear_map.mk neg_add (λ x y, (smul_neg x y).symm)
lemma is_linear_map_smul {α R : Type*} [add_comm_group α] [comm_ring R] [module R α] (c : R):
is_linear_map R (λ (z : α), c • z) :=
begin
refine is_linear_map.mk (smul_add c) _,
intros _ _,
simp [smul_smul],
ac_refl
end
--TODO: move
lemma is_linear_map_smul' {α R : Type*} [add_comm_group α] [comm_ring R] [module R α] (a : α):
is_linear_map R (λ (c : R), c • a) :=
begin
refine is_linear_map.mk (λ x y, add_smul x y a) _,
intros _ _,
simp [smul_smul]
end
end is_linear_map
/-- A submodule of a module is one which is closed under vector operations.
This is a sufficient condition for the subset of vectors in the submodule
to themselves form a module. -/
structure submodule (α : Type u) (β : Type v) [ring α]
[add_comm_group β] [module α β] : Type v :=
(carrier : set β)
(zero : (0:β) ∈ carrier)
(add : ∀ {x y}, x ∈ carrier → y ∈ carrier → x + y ∈ carrier)
(smul : ∀ (c:α) {x}, x ∈ carrier → c • x ∈ carrier)
namespace submodule
variables [ring α] [add_comm_group β] [add_comm_group γ]
variables [module α β] [module α γ]
variables (p p' : submodule α β)
variables {r : α} {x y : β}
instance : has_coe (submodule α β) (set β) := ⟨submodule.carrier⟩
instance : has_mem β (submodule α β) := ⟨λ x p, x ∈ (p : set β)⟩
@[simp] theorem mem_coe : x ∈ (p : set β) ↔ x ∈ p := iff.rfl
theorem ext' {s t : submodule α β} (h : (s : set β) = t) : s = t :=
by cases s; cases t; congr'
protected theorem ext'_iff {s t : submodule α β} : (s : set β) = t ↔ s = t :=
⟨ext', λ h, h ▸ rfl⟩
@[extensionality] theorem ext {s t : submodule α β}
(h : ∀ x, x ∈ s ↔ x ∈ t) : s = t := ext' $ set.ext h
@[simp] lemma zero_mem : (0 : β) ∈ p := p.zero
lemma add_mem (h₁ : x ∈ p) (h₂ : y ∈ p) : x + y ∈ p := p.add h₁ h₂
lemma smul_mem (r : α) (h : x ∈ p) : r • x ∈ p := p.smul r h
lemma neg_mem (hx : x ∈ p) : -x ∈ p := by rw ← neg_one_smul α; exact p.smul_mem _ hx
lemma sub_mem (hx : x ∈ p) (hy : y ∈ p) : x - y ∈ p := p.add_mem hx (p.neg_mem hy)
lemma neg_mem_iff : -x ∈ p ↔ x ∈ p :=
⟨λ h, by simpa using neg_mem p h, neg_mem p⟩
lemma add_mem_iff_left (h₁ : y ∈ p) : x + y ∈ p ↔ x ∈ p :=
⟨λ h₂, by simpa using sub_mem _ h₂ h₁, λ h₂, add_mem _ h₂ h₁⟩
lemma add_mem_iff_right (h₁ : x ∈ p) : x + y ∈ p ↔ y ∈ p :=
⟨λ h₂, by simpa using sub_mem _ h₂ h₁, add_mem _ h₁⟩
lemma sum_mem {ι : Type w} [decidable_eq ι] {t : finset ι} {f : ι → β} :
(∀c∈t, f c ∈ p) → t.sum f ∈ p :=
finset.induction_on t (by simp [p.zero_mem]) (by simp [p.add_mem] {contextual := tt})
instance : has_add p := ⟨λx y, ⟨x.1 + y.1, add_mem _ x.2 y.2⟩⟩
instance : has_zero p := ⟨⟨0, zero_mem _⟩⟩
instance : has_neg p := ⟨λx, ⟨-x.1, neg_mem _ x.2⟩⟩
instance : has_scalar α p := ⟨λ c x, ⟨c • x.1, smul_mem _ c x.2⟩⟩
@[simp] lemma coe_add (x y : p) : (↑(x + y) : β) = ↑x + ↑y := rfl
@[simp] lemma coe_zero : ((0 : p) : β) = 0 := rfl
@[simp] lemma coe_neg (x : p) : ((-x : p) : β) = -x := rfl
@[simp] lemma coe_smul (r : α) (x : p) : ((r • x : p) : β) = r • ↑x := rfl
instance : add_comm_group p :=
by refine {add := (+), zero := 0, neg := has_neg.neg, ..};
{ intros, apply set_coe.ext, simp }
instance submodule_is_add_subgroup : is_add_subgroup (p : set β) :=
{ zero_mem := p.zero,
add_mem := p.add,
neg_mem := λ _, p.neg_mem }
lemma coe_sub (x y : p) : (↑(x - y) : β) = ↑x - ↑y := by simp
instance : module α p :=
by refine {smul := (•), ..};
{ intros, apply set_coe.ext, simp [smul_add, add_smul, mul_smul] }
protected def subtype : p →ₗ[α] β :=
by refine {to_fun := coe, ..}; simp [coe_smul]
@[simp] theorem subtype_apply (x : p) : p.subtype x = x := rfl
end submodule
@[reducible] def ideal (α : Type u) [comm_ring α] := submodule α α
namespace ideal
variables [comm_ring α] (I : ideal α) {a b : α}
protected lemma zero_mem : (0 : α) ∈ I := I.zero_mem
protected lemma add_mem : a ∈ I → b ∈ I → a + b ∈ I := I.add_mem
lemma neg_mem_iff : -a ∈ I ↔ a ∈ I := I.neg_mem_iff
lemma add_mem_iff_left : b ∈ I → (a + b ∈ I ↔ a ∈ I) := I.add_mem_iff_left
lemma add_mem_iff_right : a ∈ I → (a + b ∈ I ↔ b ∈ I) := I.add_mem_iff_right
protected lemma sub_mem : a ∈ I → b ∈ I → a - b ∈ I := I.sub_mem
lemma mul_mem_left : b ∈ I → a * b ∈ I := I.smul_mem _
lemma mul_mem_right (h : a ∈ I) : a * b ∈ I := mul_comm b a ▸ I.mul_mem_left h
end ideal
/-- A vector space is the same as a module, except the scalar ring is actually
a field. (This adds commutativity of the multiplication and existence of inverses.)
This is the traditional generalization of spaces like `ℝ^n`, which have a natural
addition operation and a way to multiply them by real numbers, but no multiplication
operation between vectors. -/
class vector_space (α : Type u) (β : Type v) [discrete_field α] [add_comm_group β] extends module α β
instance discrete_field.to_vector_space {α : Type*} [discrete_field α] : vector_space α α :=
{ .. ring.to_module }
/-- Subspace of a vector space. Defined to equal `submodule`. -/
@[reducible] def subspace (α : Type u) (β : Type v)
[discrete_field α] [add_comm_group β] [vector_space α β] : Type v :=
submodule α β
instance subspace.vector_space {α β}
{f : discrete_field α} [add_comm_group β] [vector_space α β]
(p : subspace α β) : vector_space α p := {..submodule.module p}
namespace submodule
variables {R:discrete_field α} [add_comm_group β] [add_comm_group γ]
variables [vector_space α β] [vector_space α γ]
variables (p p' : submodule α β)
variables {r : α} {x y : β}
include R
set_option class.instance_max_depth 36
theorem smul_mem_iff (r0 : r ≠ 0) : r • x ∈ p ↔ x ∈ p :=
⟨λ h, by simpa [smul_smul, inv_mul_cancel r0] using p.smul_mem (r⁻¹) h,
p.smul_mem r⟩
end submodule
namespace add_comm_monoid
open add_monoid
variables {M : Type*} [add_comm_monoid M]
instance : semimodule ℕ M :=
{ smul := smul,
smul_add := λ _ _ _, smul_add _ _ _,
add_smul := λ _ _ _, add_smul _ _ _,
mul_smul := λ _ _ _, mul_smul _ _ _,
one_smul := one_smul,
zero_smul := zero_smul,
smul_zero := smul_zero }
end add_comm_monoid
namespace add_comm_group
variables {M : Type*} [add_comm_group M]
instance : module ℤ M :=
{ smul := gsmul,
smul_add := λ _ _ _, gsmul_add _ _ _,
add_smul := λ _ _ _, add_gsmul _ _ _,
mul_smul := λ _ _ _, gsmul_mul _ _ _,
one_smul := one_gsmul,
zero_smul := zero_gsmul,
smul_zero := gsmul_zero }
end add_comm_group
|
1c3e069c8a1f1a43da6c2aec3fe1c57ef1e325f9 | e39f04f6ff425fe3b3f5e26a8998b817d1dba80f | /category_theory/limits/types.lean | 052922f07ce022522791d9f6bf250c596f925e3d | [
"Apache-2.0"
] | permissive | kristychoi/mathlib | c504b5e8f84e272ea1d8966693c42de7523bf0ec | 257fd84fe98927ff4a5ffe044f68c4e9d235cc75 | refs/heads/master | 1,586,520,722,896 | 1,544,030,145,000 | 1,544,031,933,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,587 | lean | -- Copyright (c) 2018 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Scott Morrison, Reid Barton
import category_theory.limits.limits
universes u v
open category_theory
open category_theory.limits
namespace category_theory.limits.types
variables {J : Type u} [small_category J]
def limit (F : J ⥤ Type u) : cone F :=
{ X := {u : Π j, F.obj j // ∀ {j j'} (f : j ⟶ j'), F.map f (u j) = u j'},
π := { app := λ j u, u.val j } }
local attribute [elab_simple] congr_fun
def limit_is_limit (F : J ⥤ Type u) : is_limit (limit F) :=
{ lift := λ s v, ⟨λ j, s.π.app j v, λ j j' f, congr_fun (cone.w s f) _⟩,
uniq' :=
begin
intros, ext x, apply subtype.eq, ext j,
exact congr_fun (w j) x
end }
instance : has_limits.{u+1 u} (Type u) :=
λ J 𝒥 F, by exactI { cone := limit F, is_limit := limit_is_limit F }
@[simp] lemma types_limit (F : J ⥤ Type u) :
limits.limit F = {u : Π j, F.obj j // ∀ {j j'} f, F.map f (u j) = u j'} := rfl
@[simp] lemma types_limit_π (F : J ⥤ Type u) (j : J) (g : (limit F).X) :
limit.π F j g = g.val j := rfl
@[simp] lemma types_limit_pre
(F : J ⥤ Type u) {K : Type u} [𝒦 : small_category K] (E : K ⥤ J) (g : (limit F).X) :
limit.pre F E g = (⟨λ k, g.val (E.obj k), by obviously⟩ : (limit (E ⋙ F)).X) := rfl
@[simp] lemma types_limit_map {F G : J ⥤ Type u} (α : F ⟹ G) (g : (limit F).X) :
(lim.map α : (limit F).X → (limit G).X) g =
(⟨λ j, (α.app j) (g.val j), λ j j' f,
by rw [←functor_to_types.naturality, ←(g.property f)]⟩ : (limit G).X) := rfl
@[simp] lemma types_limit_lift (F : J ⥤ Type u) (c : cone F) (x : c.X):
limit.lift F c x = (⟨λ j, c.π.app j x, λ j j' f, congr_fun (cone.w c f) x⟩ : (limit F).X) :=
rfl
def colimit (F : J ⥤ Type u) : cocone F :=
{ X := @quot (Σ j, F.obj j) (λ p p', ∃ f : p.1 ⟶ p'.1, p'.2 = F.map f p.2),
ι :=
{ app := λ j x, quot.mk _ ⟨j, x⟩,
naturality' := λ j j' f, funext $ λ x, eq.symm (quot.sound ⟨f, rfl⟩) } }
local attribute [elab_with_expected_type] quot.lift
def colimit_is_colimit (F : J ⥤ Type u) : is_colimit (colimit F) :=
{ desc := λ s, quot.lift (λ (p : Σ j, F.obj j), s.ι.app p.1 p.2)
(assume ⟨j, x⟩ ⟨j', x'⟩ ⟨f, hf⟩, by rw hf; exact (congr_fun (cocone.w s f) x).symm) }
instance : has_colimits.{u+1 u} (Type u) :=
λ J 𝒥 F, by exactI { cocone := colimit F, is_colimit := colimit_is_colimit F }
@[simp] lemma types_colimit (F : J ⥤ Type u) :
limits.colimit F = @quot (Σ j, F.obj j) (λ p p', ∃ f : p.1 ⟶ p'.1, p'.2 = F.map f p.2) := rfl
@[simp] lemma types_colimit_ι (F : J ⥤ Type u) (j : J) :
colimit.ι F j = λ x, quot.mk _ ⟨j, x⟩ := rfl
@[simp] lemma types_colimit_pre
(F : J ⥤ Type u) {K : Type u} [𝒦 : small_category K] (E : K ⥤ J) (g : (colimit (E ⋙ F)).X) :
colimit.pre F E =
quot.lift (λ p, quot.mk _ ⟨E.obj p.1, p.2⟩) (λ p p' ⟨f, h⟩, quot.sound ⟨E.map f, h⟩) := rfl
@[simp] lemma types_colimit_map {F G : J ⥤ Type u} (α : F ⟹ G) :
(colim.map α : (colimit F).X → (colimit G).X) =
quot.lift
(λ p, quot.mk _ ⟨p.1, (α.app p.1) p.2⟩)
(λ p p' ⟨f, h⟩, quot.sound ⟨f, by rw h; exact functor_to_types.naturality _ _ α f _⟩) := rfl
@[simp] lemma types_colimit_desc (F : J ⥤ Type u) (c : cocone F) :
colimit.desc F c =
quot.lift
(λ p, c.ι.app p.1 p.2)
(λ p p' ⟨f, h⟩, by rw h; exact (functor_to_types.naturality _ _ c.ι f _).symm) := rfl
end category_theory.limits.types
|
500e25b55ef8d78b3c2dcf753d7bbe2bb95f4cc1 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/ring_theory/localization/integer.lean | a5fdd511ce77b18170fc9f011fdedfcb0766da2c | [
"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,365 | 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, Johan Commelin, Amelia Livingston, Anne Baanen
-/
import ring_theory.localization.basic
/-!
# Integer elements of a localization
## Main definitions
* `is_localization.is_integer` is a predicate stating that `x : S` is in the image of `R`
## Implementation notes
See `src/ring_theory/localization/basic.lean` for a design overview.
## Tags
localization, ring localization, commutative ring localization, characteristic predicate,
commutative ring, field of fractions
-/
variables {R : Type*} [comm_ring R] {M : submonoid R} {S : Type*} [comm_ring S]
variables [algebra R S] {P : Type*} [comm_ring P]
open function
open_locale big_operators
namespace is_localization
section
variables (R) {M S}
-- TODO: define a subalgebra of `is_integer`s
/-- Given `a : S`, `S` a localization of `R`, `is_integer R a` iff `a` is in the image of
the localization map from `R` to `S`. -/
def is_integer (a : S) : Prop := a ∈ (algebra_map R S).range
end
lemma is_integer_zero : is_integer R (0 : S) := subring.zero_mem _
lemma is_integer_one : is_integer R (1 : S) := subring.one_mem _
lemma is_integer_add {a b : S} (ha : is_integer R a) (hb : is_integer R b) :
is_integer R (a + b) :=
subring.add_mem _ ha hb
lemma is_integer_mul {a b : S} (ha : is_integer R a) (hb : is_integer R b) :
is_integer R (a * b) :=
subring.mul_mem _ ha hb
lemma is_integer_smul {a : R} {b : S} (hb : is_integer R b) :
is_integer R (a • b) :=
begin
rcases hb with ⟨b', hb⟩,
use a * b',
rw [←hb, (algebra_map R S).map_mul, algebra.smul_def]
end
variables (M) {S} [is_localization M S]
/-- Each element `a : S` has an `M`-multiple which is an integer.
This version multiplies `a` on the right, matching the argument order in `localization_map.surj`.
-/
lemma exists_integer_multiple' (a : S) :
∃ (b : M), is_integer R (a * algebra_map R S b) :=
let ⟨⟨num, denom⟩, h⟩ := is_localization.surj _ a in ⟨denom, set.mem_range.mpr ⟨num, h.symm⟩⟩
/-- Each element `a : S` has an `M`-multiple which is an integer.
This version multiplies `a` on the left, matching the argument order in the `has_smul` instance.
-/
lemma exists_integer_multiple (a : S) :
∃ (b : M), is_integer R ((b : R) • a) :=
by { simp_rw [algebra.smul_def, mul_comm _ a], apply exists_integer_multiple' }
/-- We can clear the denominators of a `finset`-indexed family of fractions. -/
lemma exist_integer_multiples {ι : Type*} (s : finset ι) (f : ι → S) :
∃ (b : M), ∀ i ∈ s, is_localization.is_integer R ((b : R) • f i) :=
begin
haveI := classical.prop_decidable,
refine ⟨∏ i in s, (sec M (f i)).2, λ i hi, ⟨_, _⟩⟩,
{ exact (∏ j in s.erase i, (sec M (f j)).2) * (sec M (f i)).1 },
rw [ring_hom.map_mul, sec_spec', ←mul_assoc, ←(algebra_map R S).map_mul, ← algebra.smul_def],
congr' 2,
refine trans _ ((submonoid.subtype M).map_prod _ _).symm,
rw [mul_comm, ←finset.prod_insert (s.not_mem_erase i), finset.insert_erase hi],
refl
end
/-- We can clear the denominators of a `fintype`-indexed family of fractions. -/
lemma exist_integer_multiples_of_fintype {ι : Type*} [fintype ι] (f : ι → S) :
∃ (b : M), ∀ i, is_localization.is_integer R ((b : R) • f i) :=
begin
obtain ⟨b, hb⟩ := exist_integer_multiples M finset.univ f,
exact ⟨b, λ i, hb i (finset.mem_univ _)⟩
end
/-- We can clear the denominators of a finite set of fractions. -/
lemma exist_integer_multiples_of_finset (s : finset S) :
∃ (b : M), ∀ a ∈ s, is_integer R ((b : R) • a) :=
exist_integer_multiples M s id
/-- A choice of a common multiple of the denominators of a `finset`-indexed family of fractions. -/
noncomputable
def common_denom {ι : Type*} (s : finset ι) (f : ι → S) : M :=
(exist_integer_multiples M s f).some
/-- The numerator of a fraction after clearing the denominators
of a `finset`-indexed family of fractions. -/
noncomputable
def integer_multiple {ι : Type*} (s : finset ι) (f : ι → S) (i : s) : R :=
((exist_integer_multiples M s f).some_spec i i.prop).some
@[simp]
lemma map_integer_multiple {ι : Type*} (s : finset ι) (f : ι → S) (i : s) :
algebra_map R S (integer_multiple M s f i) = common_denom M s f • f i :=
((exist_integer_multiples M s f).some_spec _ i.prop).some_spec
/-- A choice of a common multiple of the denominators of a finite set of fractions. -/
noncomputable
def common_denom_of_finset (s : finset S) : M :=
common_denom M s id
/-- The finset of numerators after clearing the denominators of a finite set of fractions. -/
noncomputable
def finset_integer_multiple [decidable_eq R] (s : finset S) : finset R :=
s.attach.image (λ t, integer_multiple M s id t)
open_locale pointwise
lemma finset_integer_multiple_image [decidable_eq R] (s : finset S) :
algebra_map R S '' (finset_integer_multiple M s) =
common_denom_of_finset M s • s :=
begin
delta finset_integer_multiple common_denom,
rw finset.coe_image,
ext,
split,
{ rintro ⟨_, ⟨x, -, rfl⟩, rfl⟩,
rw map_integer_multiple,
exact set.mem_image_of_mem _ x.prop },
{ rintro ⟨x, hx, rfl⟩,
exact ⟨_, ⟨⟨x, hx⟩, s.mem_attach _, rfl⟩, map_integer_multiple M s id _⟩ }
end
end is_localization
|
0529900999c9ec7838cf1c9eaa7ea423453d94c7 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/category_theory/differential_object.lean | 96de5d3b8a33deee920fc8d917559669bd3b93ba | [
"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 | 8,871 | 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 data.int.basic
import category_theory.shift
import category_theory.concrete_category.basic
/-!
# Differential objects in a category.
A differential object in a category with zero morphisms and a shift is
an object `X` equipped with
a morphism `d : X ⟶ X⟦1⟧`, such that `d^2 = 0`.
We build the category of differential objects, and some basic constructions
such as the forgetful functor, zero morphisms and zero objects, and the shift functor
on differential objects.
-/
open category_theory.limits
universes v u
namespace category_theory
variables (C : Type u) [category.{v} C]
-- TODO: generalize to `has_shift C A` for an arbitrary `[add_monoid A]` `[has_one A]`.
variables [has_zero_morphisms C] [has_shift C ℤ]
/--
A differential object in a category with zero morphisms and a shift is
an object `X` equipped with
a morphism `d : X ⟶ X⟦1⟧`, such that `d^2 = 0`.
-/
@[nolint has_nonempty_instance]
structure differential_object :=
(X : C)
(d : X ⟶ X⟦1⟧)
(d_squared' : d ≫ d⟦(1:ℤ)⟧' = 0 . obviously)
restate_axiom differential_object.d_squared'
attribute [simp] differential_object.d_squared
variables {C}
namespace differential_object
/--
A morphism of differential objects is a morphism commuting with the differentials.
-/
@[ext, nolint has_nonempty_instance]
structure hom (X Y : differential_object C) :=
(f : X.X ⟶ Y.X)
(comm' : X.d ≫ f⟦1⟧' = f ≫ Y.d . obviously)
restate_axiom hom.comm'
attribute [simp, reassoc] hom.comm
namespace hom
/-- The identity morphism of a differential object. -/
@[simps]
def id (X : differential_object C) : hom X X :=
{ f := 𝟙 X.X }
/-- The composition of morphisms of differential objects. -/
@[simps]
def comp {X Y Z : differential_object C} (f : hom X Y) (g : hom Y Z) : hom X Z :=
{ f := f.f ≫ g.f, }
end hom
instance category_of_differential_objects : category (differential_object C) :=
{ hom := hom,
id := hom.id,
comp := λ X Y Z f g, hom.comp f g, }
@[simp]
lemma id_f (X : differential_object C) : ((𝟙 X) : X ⟶ X).f = 𝟙 (X.X) := rfl
@[simp]
lemma comp_f {X Y Z : differential_object C} (f : X ⟶ Y) (g : Y ⟶ Z) :
(f ≫ g).f = f.f ≫ g.f :=
rfl
@[simp]
lemma eq_to_hom_f {X Y : differential_object C} (h : X = Y) :
hom.f (eq_to_hom h) = eq_to_hom (congr_arg _ h) :=
by { subst h, rw [eq_to_hom_refl, eq_to_hom_refl], refl }
variables (C)
/-- The forgetful functor taking a differential object to its underlying object. -/
def forget : (differential_object C) ⥤ C :=
{ obj := λ X, X.X,
map := λ X Y f, f.f, }
instance forget_faithful : faithful (forget C) :=
{ }
instance has_zero_morphisms : has_zero_morphisms (differential_object C) :=
{ has_zero := λ X Y,
⟨{ f := 0 }⟩}
variables {C}
@[simp]
lemma zero_f (P Q : differential_object C) : (0 : P ⟶ Q).f = 0 := rfl
/--
An isomorphism of differential objects gives an isomorphism of the underlying objects.
-/
@[simps] def iso_app {X Y : differential_object C} (f : X ≅ Y) : X.X ≅ Y.X :=
⟨f.hom.f, f.inv.f, by { dsimp, rw [← comp_f, iso.hom_inv_id, id_f] },
by { dsimp, rw [← comp_f, iso.inv_hom_id, id_f] }⟩
@[simp] lemma iso_app_refl (X : differential_object C) : iso_app (iso.refl X) = iso.refl X.X := rfl
@[simp] lemma iso_app_symm {X Y : differential_object C} (f : X ≅ Y) :
iso_app f.symm = (iso_app f).symm := rfl
@[simp] lemma iso_app_trans {X Y Z : differential_object C} (f : X ≅ Y) (g : Y ≅ Z) :
iso_app (f ≪≫ g) = iso_app f ≪≫ iso_app g := rfl
/-- An isomorphism of differential objects can be constructed
from an isomorphism of the underlying objects that commutes with the differentials. -/
@[simps] def mk_iso {X Y : differential_object C}
(f : X.X ≅ Y.X) (hf : X.d ≫ f.hom⟦1⟧' = f.hom ≫ Y.d) : X ≅ Y :=
{ hom := ⟨f.hom, hf⟩,
inv := ⟨f.inv, by { dsimp, rw [← functor.map_iso_inv, iso.comp_inv_eq, category.assoc,
iso.eq_inv_comp, functor.map_iso_hom, hf] }⟩,
hom_inv_id' := by { ext1, dsimp, exact f.hom_inv_id },
inv_hom_id' := by { ext1, dsimp, exact f.inv_hom_id } }
end differential_object
namespace functor
universes v' u'
variables (D : Type u') [category.{v'} D]
variables [has_zero_morphisms D] [has_shift D ℤ]
/--
A functor `F : C ⥤ D` which commutes with shift functors on `C` and `D` and preserves zero morphisms
can be lifted to a functor `differential_object C ⥤ differential_object D`.
-/
@[simps]
def map_differential_object (F : C ⥤ D)
(η : (shift_functor C (1:ℤ)).comp F ⟶ F.comp (shift_functor D (1:ℤ)))
(hF : ∀ c c', F.map (0 : c ⟶ c') = 0) :
differential_object C ⥤ differential_object D :=
{ obj := λ X, { X := F.obj X.X,
d := F.map X.d ≫ η.app X.X,
d_squared' := begin
rw [functor.map_comp, ← functor.comp_map F (shift_functor D (1:ℤ))],
slice_lhs 2 3 { rw [← η.naturality X.d] },
rw [functor.comp_map],
slice_lhs 1 2 { rw [← F.map_comp, X.d_squared, hF] },
rw [zero_comp, zero_comp],
end },
map := λ X Y f, { f := F.map f.f,
comm' := begin
dsimp,
slice_lhs 2 3 { rw [← functor.comp_map F (shift_functor D (1:ℤ)), ← η.naturality f.f] },
slice_lhs 1 2 { rw [functor.comp_map, ← F.map_comp, f.comm, F.map_comp] },
rw [category.assoc]
end },
map_id' := by { intros, ext, simp },
map_comp' := by { intros, ext, simp }, }
end functor
end category_theory
namespace category_theory
namespace differential_object
variables (C : Type u) [category.{v} C]
variables [has_zero_object C] [has_zero_morphisms C] [has_shift C ℤ]
open_locale zero_object
instance has_zero_object : has_zero_object (differential_object C) :=
by { refine ⟨⟨⟨0, 0⟩, λ X, ⟨⟨⟨⟨0⟩⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨⟨0⟩⟩, λ f, _⟩⟩⟩⟩; ext, }
end differential_object
namespace differential_object
variables (C : Type (u+1)) [large_category C] [concrete_category C]
[has_zero_morphisms C] [has_shift C ℤ]
instance concrete_category_of_differential_objects :
concrete_category (differential_object C) :=
{ forget := forget C ⋙ category_theory.forget C }
instance : has_forget₂ (differential_object C) C :=
{ forget₂ := forget C }
end differential_object
/-! The category of differential objects itself has a shift functor. -/
namespace differential_object
variables (C : Type u) [category.{v} C]
variables [has_zero_morphisms C] [has_shift C ℤ]
noncomputable theory
/-- The shift functor on `differential_object C`. -/
@[simps]
def shift_functor (n : ℤ) : differential_object C ⥤ differential_object C :=
{ obj := λ X,
{ X := X.X⟦n⟧,
d := X.d⟦n⟧' ≫ (shift_comm _ _ _).hom,
d_squared' := by rw [functor.map_comp, category.assoc, shift_comm_hom_comp_assoc,
←functor.map_comp_assoc, X.d_squared, functor.map_zero, zero_comp] },
map := λ X Y f,
{ f := f.f⟦n⟧',
comm' := by { dsimp, rw [category.assoc, shift_comm_hom_comp, ← functor.map_comp_assoc,
f.comm, functor.map_comp_assoc], }, },
map_id' := by { intros X, ext1, dsimp, rw functor.map_id },
map_comp' := by { intros X Y Z f g, ext1, dsimp, rw functor.map_comp } }
local attribute [simp] eq_to_hom_map
local attribute [reducible] discrete.add_monoidal shift_comm
/-- The shift functor on `differential_object C` is additive. -/
@[simps] def shift_functor_add (m n : ℤ) :
shift_functor C (m + n) ≅ shift_functor C m ⋙ shift_functor C n :=
begin
refine nat_iso.of_components (λ X, mk_iso (shift_add X.X _ _) _) _,
{ dsimp,
-- This is just `simp, simp [eq_to_hom_map]`.
simp_rw [category.assoc, obj_μ_inv_app, μ_inv_hom_app_assoc, functor.map_comp, obj_μ_app,
category.assoc, μ_naturality_assoc, μ_inv_hom_app_assoc, obj_μ_inv_app, category.assoc,
μ_naturalityₗ_assoc, μ_inv_hom_app_assoc, μ_inv_naturalityᵣ_assoc],
simp only [eq_to_hom_map, eq_to_hom_app, eq_to_iso.hom, eq_to_hom_trans_assoc,
eq_to_iso.inv], },
{ intros X Y f, ext, dsimp, exact nat_trans.naturality _ _ }
end
local attribute [reducible] endofunctor_monoidal_category
section
local attribute [instance] endofunctor_monoidal_category
/-- The shift by zero is naturally isomorphic to the identity. -/
@[simps]
def shift_ε : 𝟭 (differential_object C) ≅ shift_functor C 0 :=
begin
refine nat_iso.of_components (λ X, mk_iso ((shift_monoidal_functor C ℤ).ε_iso.app X.X) _) _,
{ dsimp, simp, dsimp, simp },
{ introv, ext, dsimp, simp }
end
end
local attribute [simp] eq_to_hom_map
instance : has_shift (differential_object C) ℤ :=
has_shift_mk _ _
{ F := shift_functor C,
ε := shift_ε C,
μ := λ m n, (shift_functor_add C m n).symm }
end differential_object
end category_theory
|
69a217d84e0d7844674801ea2310c923908bb43f | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/linear_algebra/pi_tensor_product.lean | 551441a21b78ea801f86d0530eca3b57a6c1b953 | [
"Apache-2.0"
] | permissive | ilitzroth/mathlib | ea647e67f1fdfd19a0f7bdc5504e8acec6180011 | 5254ef14e3465f6504306132fe3ba9cec9ffff16 | refs/heads/master | 1,680,086,661,182 | 1,617,715,647,000 | 1,617,715,647,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 21,385 | lean | /-
Copyright (c) 2020 Frédéric Dupuis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Frédéric Dupuis, Eric Wieser
-/
import group_theory.congruence
import linear_algebra.multilinear
/-!
# Tensor product of an indexed family of semimodules over commutative semirings
We define the tensor product of an indexed family `s : ι → Type*` of semimodules over commutative
semirings. We denote this space by `⨂[R] i, s i` and define it as `free_add_monoid (R × Π i, s i)`
quotiented by the appropriate equivalence relation. The treatment follows very closely that of the
binary tensor product in `linear_algebra/tensor_product.lean`.
## Main definitions
* `pi_tensor_product R s` with `R` a commutative semiring and `s : ι → Type*` is the tensor product
of all the `s i`'s. This is denoted by `⨂[R] i, s i`.
* `tprod R f` with `f : Π i, s i` is the tensor product of the vectors `f i` over all `i : ι`.
This is bundled as a multilinear map from `Π i, s i` to `⨂[R] i, s i`.
* `lift_add_hom` constructs an `add_monoid_hom` from `(⨂[R] i, s i)` to some space `F` from a
function `φ : (R × Π i, s i) → F` with the appropriate properties.
* `lift φ` with `φ : multilinear_map R s E` is the corresponding linear map
`(⨂[R] i, s i) →ₗ[R] E`. This is bundled as a linear equivalence.
* `pi_tensor_product.reindex e` re-indexes the components of `⨂[R] i : ι, M` along `e : ι ≃ ι₂`.
* `pi_tensor_product.tmul_equiv` equivalence between a `tensor_product` of `pi_tensor_product`s and
a single `pi_tensor_product`.
## Notations
* `⨂[R] i, s i` is defined as localized notation in locale `tensor_product`
* `⨂ₜ[R] i, f i` with `f : Π i, f i` is defined globally as the tensor product of all the `f i`'s.
## Implementation notes
* We define it via `free_add_monoid (R × Π i, s i)` with the `R` representing a "hidden" tensor
factor, rather than `free_add_monoid (Π i, s i)` to ensure that, if `ι` is an empty type,
the space is isomorphic to the base ring `R`.
* We have not restricted the index type `ι` to be a `fintype`, as nothing we do here strictly
requires it. However, problems may arise in the case where `ι` is infinite; use at your own
caution.
## TODO
* Define tensor powers, symmetric subspace, etc.
* API for the various ways `ι` can be split into subsets; connect this with the binary
tensor product.
* Include connection with holors.
* Port more of the API from the binary tensor product over to this case.
## Tags
multilinear, tensor, tensor product
-/
open function
section semiring
variables {ι ι₂ ι₃ : Type*} [decidable_eq ι] [decidable_eq ι₂] [decidable_eq ι₃]
variables {R : Type*} [comm_semiring R]
variables {R' : Type*} [comm_semiring R'] [algebra R' R]
variables {s : ι → Type*} [∀ i, add_comm_monoid (s i)] [∀ i, semimodule R (s i)]
variables {M : Type*} [add_comm_monoid M] [semimodule R M]
variables {E : Type*} [add_comm_monoid E] [semimodule R E]
variables {F : Type*} [add_comm_monoid F]
namespace pi_tensor_product
include R
variables (R) (s)
/-- The relation on `free_add_monoid (R × Π i, s i)` that generates a congruence whose quotient is
the tensor product. -/
inductive eqv : free_add_monoid (R × Π i, s i) → free_add_monoid (R × Π i, s i) → Prop
| of_zero : ∀ (r : R) (f : Π i, s i) (i : ι) (hf : f i = 0), eqv (free_add_monoid.of (r, f)) 0
| of_zero_scalar : ∀ (f : Π i, s i), eqv (free_add_monoid.of (0, f)) 0
| of_add : ∀ (r : R) (f : Π i, s i) (i : ι) (m₁ m₂ : s i), eqv
(free_add_monoid.of (r, update f i m₁) + free_add_monoid.of (r, update f i m₂))
(free_add_monoid.of (r, update f i (m₁ + m₂)))
| of_add_scalar : ∀ (r r' : R) (f : Π i, s i), eqv
(free_add_monoid.of (r, f) + free_add_monoid.of (r', f))
(free_add_monoid.of (r + r', f))
| of_smul : ∀ (r : R) (f : Π i, s i) (i : ι) (r' : R), eqv
(free_add_monoid.of (r, update f i (r' • (f i))))
(free_add_monoid.of (r' * r, f))
| add_comm : ∀ x y, eqv (x + y) (y + x)
end pi_tensor_product
variables (R) (s)
/-- `pi_tensor_product R s` with `R` a commutative semiring and `s : ι → Type*` is the tensor
product of all the `s i`'s. This is denoted by `⨂[R] i, s i`. -/
def pi_tensor_product : Type* :=
(add_con_gen (pi_tensor_product.eqv R s)).quotient
variables {R}
/- This enables the notation `⨂[R] i : ι, s i` for the pi tensor product, given `s : ι → Type*`. -/
localized "notation `⨂[`:100 R `] ` binders `, ` r:(scoped:67 f, pi_tensor_product R f) := r"
in tensor_product
open_locale tensor_product
namespace pi_tensor_product
section module
instance : add_comm_monoid (⨂[R] i, s i) :=
{ 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 (pi_tensor_product.eqv R s)).add_monoid }
instance : inhabited (⨂[R] i, s i) := ⟨0⟩
variables (R) {s}
/-- `tprod_coeff R r f` with `r : R` and `f : Π i, s i` is the tensor product of the vectors `f i`
over all `i : ι`, multiplied by the coefficient `r`. Note that this is meant as an auxiliary
definition for this file alone, and that one should use `tprod` defined below for most purposes. -/
def tprod_coeff (r : R) (f : Π i, s i) : ⨂[R] i, s i := add_con.mk' _ $ free_add_monoid.of (r, f)
variables {R}
lemma zero_tprod_coeff (f : Π i, s i) : tprod_coeff R 0 f = 0 :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_scalar _
lemma zero_tprod_coeff' (z : R) (f : Π i, s i) (i : ι) (hf: f i = 0) : tprod_coeff R z f = 0 :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero _ _ i hf
lemma add_tprod_coeff (z : R) (f : Π i, s i) (i : ι) (m₁ m₂ : s i) :
tprod_coeff R z (update f i m₁) + tprod_coeff R z (update f i m₂) =
tprod_coeff R z (update f i (m₁ + m₂)) :=
quotient.sound' $ add_con_gen.rel.of _ _ (eqv.of_add z f i m₁ m₂)
lemma add_tprod_coeff' (z₁ z₂ : R) (f : Π i, s i) :
tprod_coeff R z₁ f + tprod_coeff R z₂ f = tprod_coeff R (z₁ + z₂) f :=
quotient.sound' $ add_con_gen.rel.of _ _ (eqv.of_add_scalar z₁ z₂ f)
lemma smul_tprod_coeff_aux (z : R) (f : Π i, s i) (i : ι) (r : R) :
tprod_coeff R z (update f i (r • f i)) = tprod_coeff R (r * z) f :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_smul _ _ _ _
lemma smul_tprod_coeff (z : R) (f : Π i, s i) (i : ι) (r : R')
[semimodule R' (s i)] [is_scalar_tower R' R (s i)] :
tprod_coeff R z (update f i (r • f i)) = tprod_coeff R (r • z) f :=
begin
have h₁ : r • z = (r • (1 : R)) * z := by simp,
have h₂ : r • (f i) = (r • (1 : R)) • f i := by simp,
rw [h₁, h₂],
exact smul_tprod_coeff_aux z f i _,
end
/-- Construct an `add_monoid_hom` from `(⨂[R] i, s i)` to some space `F` from a function
`φ : (R × Π i, s i) → F` with the appropriate properties. -/
def lift_add_hom (φ : (R × Π i, s i) → F)
(C0 : ∀ (r : R) (f : Π i, s i) (i : ι) (hf : f i = 0), φ (r, f) = 0)
(C0' : ∀ (f : Π i, s i), φ (0, f) = 0)
(C_add : ∀ (r : R) (f : Π i, s i) (i : ι) (m₁ m₂ : s i),
φ (r, update f i m₁) + φ (r, update f i m₂) = φ (r, update f i (m₁ + m₂)))
(C_add_scalar : ∀ (r r' : R) (f : Π i, s i),
φ (r , f) + φ (r', f) = φ (r + r', f))
(C_smul : ∀ (r : R) (f : Π i, s i) (i : ι) (r' : R),
φ (r, update f i (r' • (f i))) = φ (r' * r, f))
: (⨂[R] i, s i) →+ F :=
(add_con_gen (pi_tensor_product.eqv R s)).lift (free_add_monoid.lift φ) $ add_con.add_con_gen_le $
λ x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero r' f i hf) := (add_con.ker_rel _).2 $
by simp [free_add_monoid.lift_eval_of, C0 r' f i hf]
| _, _, (eqv.of_zero_scalar f) := (add_con.ker_rel _).2 $
by simp [free_add_monoid.lift_eval_of, C0']
| _, _, (eqv.of_add z f i m₁ m₂) := (add_con.ker_rel _).2 $
by simp [free_add_monoid.lift_eval_of, C_add]
| _, _, (eqv.of_add_scalar z₁ z₂ f) := (add_con.ker_rel _).2 $
by simp [free_add_monoid.lift_eval_of, C_add_scalar]
| _, _, (eqv.of_smul z f i r') := (add_con.ker_rel _).2 $
by simp [free_add_monoid.lift_eval_of, C_smul]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end
-- Most of the time we want the instance below this one, which is easier for typeclass resolution
-- to find.
instance has_scalar' : has_scalar R' (⨂[R] i, s i) :=
⟨λ r, lift_add_hom (λ f : R × Π i, s i, tprod_coeff R (r • f.1) f.2)
(λ r' f i hf, by simp_rw [zero_tprod_coeff' _ f i hf])
(λ f, by simp [zero_tprod_coeff])
(λ r' f i m₁ m₂, by simp [add_tprod_coeff])
(λ r' r'' f, by simp [add_tprod_coeff', mul_add])
(λ z f i r', by simp [smul_tprod_coeff])⟩
instance : has_scalar R (⨂[R] i, s i) := pi_tensor_product.has_scalar'
lemma smul_tprod_coeff' (r : R') (z : R) (f : Π i, s i) :
r • (tprod_coeff R z f) = tprod_coeff R (r • z) f := rfl
protected theorem smul_add (r : R') (x y : ⨂[R] i, s i) :
r • (x + y) = r • x + r • y :=
add_monoid_hom.map_add _ _ _
@[elab_as_eliminator]
protected theorem induction_on'
{C : (⨂[R] i, s i) → Prop}
(z : ⨂[R] i, s i)
(C1 : ∀ {r : R} {f : Π i, s i}, C (tprod_coeff R r f))
(Cp : ∀ {x y}, C x → C y → C (x + y)) : C z :=
begin
have C0 : C 0,
{ have h₁ := @C1 0 0,
rwa [zero_tprod_coeff] at h₁ },
refine add_con.induction_on z (λ x, free_add_monoid.rec_on x C0 _),
simp_rw add_con.coe_add,
refine λ f y ih, Cp _ ih,
convert @C1 f.1 f.2,
simp only [prod.mk.eta],
end
-- Most of the time we want the instance below this one, which is easier for typeclass resolution
-- to find.
instance semimodule' : semimodule R' (⨂[R] i, s i) :=
{ smul := (•),
smul_add := λ r x y, pi_tensor_product.smul_add r x y,
mul_smul := λ r r' x,
begin
refine pi_tensor_product.induction_on' x _ _,
{ intros r'' f,
simp [smul_tprod_coeff', smul_smul] },
{ intros x y ihx ihy,
simp [pi_tensor_product.smul_add, ihx, ihy] }
end,
one_smul := λ x, pi_tensor_product.induction_on' x
(λ f, by simp [smul_tprod_coeff' _ _])
(λ z y ihz ihy, by simp_rw [pi_tensor_product.smul_add, ihz, ihy]),
add_smul := λ r r' x,
begin
refine pi_tensor_product.induction_on' x _ _,
{ intros r f,
simp [smul_tprod_coeff' _ _, add_smul, add_tprod_coeff'] },
{ intros x y ihx ihy,
simp [pi_tensor_product.smul_add, ihx, ihy, add_add_add_comm] }
end,
smul_zero := λ r, add_monoid_hom.map_zero _,
zero_smul := λ x,
begin
refine pi_tensor_product.induction_on' x _ _,
{ intros r f,
simp_rw [smul_tprod_coeff' _ _, zero_smul],
exact zero_tprod_coeff _ },
{ intros x y ihx ihy,
rw [pi_tensor_product.smul_add, ihx, ihy, add_zero] },
end }
instance : semimodule R' (⨂[R] i, s i) := pi_tensor_product.semimodule'
variables {R}
variables (R)
/-- The canonical `multilinear_map R s (⨂[R] i, s i)`. -/
def tprod : multilinear_map R s (⨂[R] i, s i) :=
{ to_fun := tprod_coeff R 1,
map_add' := λ f i x y, (add_tprod_coeff (1 : R) f i x y).symm,
map_smul' := λ f i r x,
by simp_rw [smul_tprod_coeff', ←smul_tprod_coeff (1 : R) _ i, update_idem, update_same] }
variables {R}
notation `⨂ₜ[`:100 R`] ` binders `, ` r:(scoped:67 f, tprod R f) := r
@[simp]
lemma tprod_coeff_eq_smul_tprod (z : R) (f : Π i, s i) : tprod_coeff R z f = z • tprod R f :=
begin
have : z = z • (1 : R) := by simp only [mul_one, algebra.id.smul_eq_mul],
conv_lhs { rw this },
rw ←smul_tprod_coeff',
refl,
end
@[elab_as_eliminator]
protected theorem induction_on
{C : (⨂[R] i, s i) → Prop}
(z : ⨂[R] i, s i)
(C1 : ∀ {r : R} {f : Π i, s i}, C (r • (tprod R f)))
(Cp : ∀ {x y}, C x → C y → C (x + y)) : C z :=
begin
simp_rw ←tprod_coeff_eq_smul_tprod at C1,
exact pi_tensor_product.induction_on' z @C1 @Cp,
end
@[ext]
theorem ext {φ₁ φ₂ : (⨂[R] i, s i) →ₗ[R] E}
(H : φ₁.comp_multilinear_map (tprod R) = φ₂.comp_multilinear_map (tprod R)) : φ₁ = φ₂ :=
begin
refine linear_map.ext _,
refine λ z,
(pi_tensor_product.induction_on' z _ (λ x y hx hy, by rw [φ₁.map_add, φ₂.map_add, hx, hy])),
{ intros r f,
rw [tprod_coeff_eq_smul_tprod, φ₁.map_smul, φ₂.map_smul],
apply _root_.congr_arg,
exact multilinear_map.congr_fun H f }
end
end module
section multilinear
open multilinear_map
variables {s}
/-- Auxiliary function to constructing a linear map `(⨂[R] i, s i) → E` given a
`multilinear map R s E` with the property that its composition with the canonical
`multilinear_map R s (⨂[R] i, s i)` is the given multilinear map. -/
def lift_aux (φ : multilinear_map R s E) : (⨂[R] i, s i) →+ E :=
lift_add_hom (λ (p : R × Π i, s i), p.1 • (φ p.2))
(λ z f i hf, by rw [map_coord_zero φ i hf, smul_zero])
(λ f, by rw [zero_smul])
(λ z f i m₁ m₂, by rw [←smul_add, φ.map_add])
(λ z₁ z₂ f, by rw [←add_smul])
(λ z f i r, by simp [φ.map_smul, smul_smul, mul_comm])
lemma lift_aux_tprod (φ : multilinear_map R s E) (f : Π i, s i) : lift_aux φ (tprod R f) = φ f :=
by simp only [lift_aux, lift_add_hom, tprod, multilinear_map.coe_mk, tprod_coeff,
free_add_monoid.lift_eval_of, one_smul, add_con.lift_mk']
lemma lift_aux_tprod_coeff (φ : multilinear_map R s E) (z : R) (f : Π i, s i) :
lift_aux φ (tprod_coeff R z f) = z • φ f :=
by simp [lift_aux, lift_add_hom, tprod_coeff, free_add_monoid.lift_eval_of]
lemma lift_aux.smul {φ : multilinear_map R s E} (r : R) (x : ⨂[R] i, s i) :
lift_aux φ (r • x) = r • lift_aux φ x :=
begin
refine pi_tensor_product.induction_on' x _ _,
{ intros z f,
rw [smul_tprod_coeff' r z f, lift_aux_tprod_coeff, lift_aux_tprod_coeff, smul_assoc] },
{ intros z y ihz ihy,
rw [smul_add, (lift_aux φ).map_add, ihz, ihy, (lift_aux φ).map_add, smul_add] }
end
/-- Constructing a linear map `(⨂[R] i, s i) → E` given a `multilinear_map R s E` with the
property that its composition with the canonical `multilinear_map R s E` is
the given multilinear map `φ`. -/
def lift : (multilinear_map R s E) ≃ₗ[R] ((⨂[R] i, s i) →ₗ[R] E) :=
{ to_fun := λ φ, { map_smul' := lift_aux.smul, .. lift_aux φ },
inv_fun := λ φ', φ'.comp_multilinear_map (tprod R),
left_inv := λ φ, by { ext, simp [lift_aux_tprod, linear_map.comp_multilinear_map] },
right_inv := λ φ, by { ext, simp [lift_aux_tprod] },
map_add' := λ φ₁ φ₂, by { ext, simp [lift_aux_tprod] },
map_smul' := λ r φ₂, by { ext, simp [lift_aux_tprod] } }
variables {φ : multilinear_map R s E}
@[simp] lemma lift.tprod (f : Π i, s i) : lift φ (tprod R f) = φ f := lift_aux_tprod φ f
theorem lift.unique' {φ' : (⨂[R] i, s i) →ₗ[R] E} (H : φ'.comp_multilinear_map (tprod R) = φ) :
φ' = lift φ :=
ext $ H.symm ▸ (lift.symm_apply_apply φ).symm
theorem lift.unique {φ' : (⨂[R] i, s i) →ₗ[R] E} (H : ∀ f, φ' (tprod R f) = φ f) :
φ' = lift φ :=
lift.unique' (multilinear_map.ext H)
@[simp]
theorem lift_symm (φ' : (⨂[R] i, s i) →ₗ[R] E) : lift.symm φ' = φ'.comp_multilinear_map (tprod R) :=
rfl
@[simp]
theorem lift_tprod : lift (tprod R : multilinear_map R s _) = linear_map.id :=
eq.symm $ lift.unique' rfl
section
variables (R M)
/-- Re-index the components of the tensor power by `e`.
For simplicity, this is defined only for homogeneously- (rather than dependently-) typed components.
-/
def reindex (e : ι ≃ ι₂) : ⨂[R] i : ι, M ≃ₗ[R] ⨂[R] i : ι₂, M :=
linear_equiv.of_linear
((lift.symm.trans $
multilinear_map.dom_dom_congr_linear_equiv M (⨂[R] i : ι₂, M) R R e.symm).trans
lift (linear_map.id))
((lift.symm.trans $
multilinear_map.dom_dom_congr_linear_equiv M (⨂[R] i : ι, M) R R e).trans
lift (linear_map.id))
(by { ext, simp })
(by { ext, simp })
end
@[simp] lemma reindex_tprod (e : ι ≃ ι₂) (f : Π i, M) :
reindex R M e (tprod R f) = tprod R (λ i, f (e.symm i)) :=
lift.tprod f
@[simp] lemma reindex_comp_tprod (e : ι ≃ ι₂) :
(reindex R M e : ⨂[R] i : ι, M →ₗ[R] ⨂[R] i : ι₂, M).comp_multilinear_map (tprod R) =
(tprod R : multilinear_map R (λ i, M) _).dom_dom_congr e.symm :=
multilinear_map.ext $ reindex_tprod e
@[simp] lemma lift_comp_reindex (e : ι ≃ ι₂) (φ : multilinear_map R (λ _ : ι₂, M) E) :
(lift φ).comp ↑(reindex R M e) = lift (φ.dom_dom_congr e.symm) :=
by { ext, simp, }
@[simp]
lemma lift_reindex (e : ι ≃ ι₂) (φ : multilinear_map R (λ _, M) E) (x : ⨂[R] i, M) :
lift φ (reindex R M e x) = lift (φ.dom_dom_congr e.symm) x :=
linear_map.congr_fun (lift_comp_reindex e φ) x
@[simp] lemma reindex_trans (e : ι ≃ ι₂) (e' : ι₂ ≃ ι₃) :
(reindex R M e).trans (reindex R M e') = reindex R M (e.trans e') :=
begin
apply linear_equiv.to_linear_map_injective,
ext f,
simp only [linear_equiv.trans_apply, linear_equiv.coe_coe, reindex_tprod,
linear_map.coe_comp_multilinear_map, function.comp_app, multilinear_map.dom_dom_congr_apply,
reindex_comp_tprod],
congr,
end
@[simp] lemma reindex_symm (e : ι ≃ ι₂) :
(reindex R M e).symm = reindex R M e.symm := rfl
@[simp] lemma reindex_refl : reindex R M (equiv.refl ι) = linear_equiv.refl R _ :=
begin
apply linear_equiv.to_linear_map_injective,
ext1,
rw [reindex_comp_tprod, linear_equiv.refl_to_linear_map, equiv.refl_symm],
refl,
end
/-- The tensor product over an empty set of indices is isomorphic to the base ring -/
def pempty_equiv : ⨂[R] i : pempty, M ≃ₗ[R] R :=
{ to_fun := lift ⟨λ (_ : pempty → M), (1 : R), λ v, pempty.elim, λ v, pempty.elim⟩,
inv_fun := λ r, r • tprod R (λ v, pempty.elim v),
left_inv := λ x, by {
apply x.induction_on,
{ intros r f,
have : f = (λ i, pempty.elim i) := funext (λ i, pempty.elim i),
simp [this], },
{ simp only,
intros x y hx hy,
simp [add_smul, hx, hy] }},
right_inv := λ t, by simp only [mul_one, algebra.id.smul_eq_mul, multilinear_map.coe_mk,
linear_map.map_smul, pi_tensor_product.lift.tprod],
map_add' := linear_map.map_add _,
map_smul' := linear_map.map_smul _, }
section tmul
/-- Collapse a `tensor_product` of `pi_tensor_product`s. -/
private def tmul : (⨂[R] i : ι, M) ⊗[R] (⨂[R] i : ι₂, M) →ₗ[R] ⨂[R] i : ι ⊕ ι₂, M :=
tensor_product.lift
{ to_fun := λ a, pi_tensor_product.lift $ pi_tensor_product.lift
(multilinear_map.curry_sum_equiv R _ _ M _ (tprod R)) a,
map_add' := λ a b, by simp only [linear_equiv.map_add, linear_map.map_add],
map_smul' := λ r a, by simp only [linear_equiv.map_smul, linear_map.map_smul], }
private lemma tmul_apply (a : ι → M) (b : ι₂ → M) :
tmul ((⨂ₜ[R] i, a i) ⊗ₜ[R] (⨂ₜ[R] i, b i)) = ⨂ₜ[R] i, sum.elim a b i :=
begin
erw [tensor_product.lift.tmul, pi_tensor_product.lift.tprod, pi_tensor_product.lift.tprod],
refl
end
/-- Expand `pi_tensor_product` into a `tensor_product` of two factors. -/
private def tmul_symm : ⨂[R] i : ι ⊕ ι₂, M →ₗ[R] (⨂[R] i : ι, M) ⊗[R] (⨂[R] i : ι₂, M) :=
-- by using tactic mode, we avoid the need for a lot of `@`s and `_`s
pi_tensor_product.lift $ by apply multilinear_map.dom_coprod; [exact tprod R, exact tprod R]
private lemma tmul_symm_apply (a : ι ⊕ ι₂ → M) :
tmul_symm (⨂ₜ[R] i, a i) = (⨂ₜ[R] i, a (sum.inl i)) ⊗ₜ[R] (⨂ₜ[R] i, a (sum.inr i)) :=
pi_tensor_product.lift.tprod _
variables (R M)
/-- Equivalence between a `tensor_product` of `pi_tensor_product`s and a single
`pi_tensor_product` indexed by a `sum` type.
For simplicity, this is defined only for homogeneously- (rather than dependently-) typed components.
-/
def tmul_equiv : (⨂[R] i : ι, M) ⊗[R] (⨂[R] i : ι₂, M) ≃ₗ[R] ⨂[R] i : ι ⊕ ι₂, M :=
linear_equiv.of_linear tmul tmul_symm
(by { ext x,
show tmul (tmul_symm (tprod R x)) = tprod R x, -- Speed up the call to `simp`.
simp only [tmul_symm_apply, tmul_apply, sum.elim_comp_inl_inr], })
(by { ext x y,
show tmul_symm (tmul (tprod R x ⊗ₜ[R] tprod R y)) = tprod R x ⊗ₜ[R] tprod R y,
simp only [tmul_apply, tmul_symm_apply, sum.elim_inl, sum.elim_inr], })
@[simp] lemma tmul_equiv_apply (a : ι → M) (b : ι₂ → M) :
tmul_equiv R M ((⨂ₜ[R] i, a i) ⊗ₜ[R] (⨂ₜ[R] i, b i)) = ⨂ₜ[R] i, sum.elim a b i :=
tmul_apply a b
@[simp] lemma tmul_equiv_symm_apply (a : ι ⊕ ι₂ → M) :
(tmul_equiv R M).symm (⨂ₜ[R] i, a i) = (⨂ₜ[R] i, a (sum.inl i)) ⊗ₜ[R] (⨂ₜ[R] i, a (sum.inr i)) :=
tmul_symm_apply a
end tmul
end multilinear
end pi_tensor_product
end semiring
section ring
namespace pi_tensor_product
open pi_tensor_product
open_locale tensor_product
variables {ι : Type*} [decidable_eq ι] {R : Type*} [comm_ring R]
variables {s : ι → Type*} [∀ i, add_comm_group (s i)] [∀ i, module R (s i)]
/- Unlike for the binary tensor product, we require `R` to be a `comm_ring` here, otherwise
this is false in the case where `ι` is empty. -/
instance : add_comm_group (⨂[R] i, s i) := semimodule.add_comm_monoid_to_add_comm_group R
end pi_tensor_product
end ring
|
b84ed9f7f0aa6366f88926595fd6ca9195bd1fe3 | 0c6b99c0daded009943e570c13367c8cc7d3bcae | /chapter4.lean | eb2616b606365eaa494c32bb5d95b7d6ad75ffed | [] | no_license | TateKennington/logic-and-proof-exercises | e3c5c5b12b6238b47b0c5acf8717923bd471a535 | acca8882026e7b643453eb096d3021cd043005bd | refs/heads/main | 1,687,638,489,616 | 1,626,858,474,000 | 1,626,858,474,000 | 371,871,374 | 4 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,293 | lean | variables A B C D: Prop
example : A ∧ (A → B) → B :=
assume h,
have A, from and.left h,
have A → B, from and.right h,
‹A → B› ‹A›
example : A → ¬ (¬ A ∧ B) :=
assume :A,
show ¬ (¬ A ∧ B), from
assume h,
have nA: ¬A, from and.left h,
nA ‹A›
example : ¬ (A ∧ B) → (A → ¬ B) :=
assume h₁,
assume h₂,
show ¬B, from
assume :B,
have A ∧ B, from and.intro h₂ this,
h₁ this
example (h₁ : A ∨ B) (h₂ : A → C) (h₃ : B → D) : C ∨ D :=
have A → C ∨ D, from
assume :A,
have C, from h₂ ‹A›,
or.inl this,
have B → C ∨ D, from
assume :B,
have D, from h₃ ‹B›,
or.inr this,
or.elim h₁ ‹A → C ∨ D› ‹B → C ∨ D›
example (h : ¬ A ∧ ¬ B) : ¬ (A ∨ B) :=
assume nh,
have nA: ¬A, from and.left h,
have nB: ¬B, from and.right h,
have h₁: A → false, from assume :A, nA this,
have h₂: B → false, from assume :B, nB this,
or.elim nh h₁ h₂
example : ¬ (A ↔ ¬ A) :=
assume h: A ↔ ¬A,
show false, from
have hₐ: ¬A, from
assume h₁: A,
have h₂: A → ¬A, from iff.elim_left h,
show false, from h₂ h₁ h₁,
have h₁: ¬A → A, from iff.elim_right h,
hₐ (h₁ hₐ) |
fc445118c47e1b2c66af8f7060a31fce8bf81124 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/algebra/indicator_function.lean | 2762a65b1b36bdb5659d730becf25db23946c007 | [
"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 | 24,625 | lean | /-
Copyright (c) 2020 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou
-/
import algebra.support
/-!
# Indicator function
- `indicator (s : set α) (f : α → β) (a : α)` is `f a` if `a ∈ s` and is `0` otherwise.
- `mul_indicator (s : set α) (f : α → β) (a : α)` is `f a` if `a ∈ s` and is `1` otherwise.
## Implementation note
In mathematics, an indicator function or a characteristic function is a function
used to indicate membership of an element in a set `s`,
having the value `1` for all elements of `s` and the value `0` otherwise.
But since it is usually used to restrict a function to a certain set `s`,
we let the indicator function take the value `f x` for some function `f`, instead of `1`.
If the usual indicator function is needed, just set `f` to be the constant function `λx, 1`.
The indicator function is implemented non-computably, to avoid having to pass around `decidable`
arguments. This is in contrast with the design of `pi.single` or `set.piecewise`.
## Tags
indicator, characteristic
-/
open_locale big_operators
open function
variables {α β ι M N : Type*}
namespace set
section has_one
variables [has_one M] [has_one N] {s t : set α} {f g : α → M} {a : α}
/-- `indicator s f a` is `f a` if `a ∈ s`, `0` otherwise. -/
noncomputable def indicator {M} [has_zero M] (s : set α) (f : α → M) : α → M
| x := by haveI := classical.dec_pred (∈ s); exact if x ∈ s then f x else 0
/-- `mul_indicator s f a` is `f a` if `a ∈ s`, `1` otherwise. -/
@[to_additive]
noncomputable def mul_indicator (s : set α) (f : α → M) : α → M
| x := by haveI := classical.dec_pred (∈ s); exact if x ∈ s then f x else 1
@[simp, to_additive] lemma piecewise_eq_mul_indicator [decidable_pred (∈ s)] :
s.piecewise f 1 = s.mul_indicator f :=
funext $ λ x, @if_congr _ _ _ _ (id _) _ _ _ _ iff.rfl rfl rfl
@[to_additive] lemma mul_indicator_apply (s : set α) (f : α → M) (a : α) [decidable (a ∈ s)] :
mul_indicator s f a = if a ∈ s then f a else 1 := by convert rfl
@[simp, to_additive] lemma mul_indicator_of_mem (h : a ∈ s) (f : α → M) :
mul_indicator s f a = f a :=
by { letI := classical.dec (a ∈ s), exact if_pos h }
@[simp, to_additive] lemma mul_indicator_of_not_mem (h : a ∉ s) (f : α → M) :
mul_indicator s f a = 1 :=
by { letI := classical.dec (a ∈ s), exact if_neg h }
@[to_additive] lemma mul_indicator_eq_one_or_self (s : set α) (f : α → M) (a : α) :
mul_indicator s f a = 1 ∨ mul_indicator s f a = f a :=
begin
by_cases h : a ∈ s,
{ exact or.inr (mul_indicator_of_mem h f) },
{ exact or.inl (mul_indicator_of_not_mem h f) }
end
@[simp, to_additive] lemma mul_indicator_apply_eq_self :
s.mul_indicator f a = f a ↔ (a ∉ s → f a = 1) :=
by letI := classical.dec (a ∈ s); exact ite_eq_left_iff.trans (by rw [@eq_comm _ (f a)])
@[simp, to_additive] lemma mul_indicator_eq_self : s.mul_indicator f = f ↔ mul_support f ⊆ s :=
by simp only [funext_iff, subset_def, mem_mul_support, mul_indicator_apply_eq_self, not_imp_comm]
@[to_additive] lemma mul_indicator_eq_self_of_superset (h1 : s.mul_indicator f = f) (h2 : s ⊆ t) :
t.mul_indicator f = f :=
by { rw mul_indicator_eq_self at h1 ⊢, exact subset.trans h1 h2 }
@[simp, to_additive] lemma mul_indicator_apply_eq_one :
mul_indicator s f a = 1 ↔ (a ∈ s → f a = 1) :=
by letI := classical.dec (a ∈ s); exact ite_eq_right_iff
@[simp, to_additive] lemma mul_indicator_eq_one :
mul_indicator s f = (λ x, 1) ↔ disjoint (mul_support f) s :=
by simp only [funext_iff, mul_indicator_apply_eq_one, set.disjoint_left, mem_mul_support,
not_imp_not]
@[simp, to_additive] lemma mul_indicator_eq_one' :
mul_indicator s f = 1 ↔ disjoint (mul_support f) s :=
mul_indicator_eq_one
@[to_additive] lemma mul_indicator_apply_ne_one {a : α} :
s.mul_indicator f a ≠ 1 ↔ a ∈ s ∩ mul_support f :=
by simp only [ne.def, mul_indicator_apply_eq_one, not_imp, mem_inter_eq, mem_mul_support]
@[simp, to_additive] lemma mul_support_mul_indicator :
function.mul_support (s.mul_indicator f) = s ∩ function.mul_support f :=
ext $ λ x, by simp [function.mem_mul_support, mul_indicator_apply_eq_one]
/-- If a multiplicative indicator function is not equal to `1` at a point, then that point is in the
set. -/
@[to_additive "If an additive indicator function is not equal to `0` at a point, then that point is
in the set."]
lemma mem_of_mul_indicator_ne_one (h : mul_indicator s f a ≠ 1) : a ∈ s :=
not_imp_comm.1 (λ hn, mul_indicator_of_not_mem hn f) h
@[to_additive] lemma eq_on_mul_indicator : eq_on (mul_indicator s f) f s :=
λ x hx, mul_indicator_of_mem hx f
@[to_additive] lemma mul_support_mul_indicator_subset : mul_support (s.mul_indicator f) ⊆ s :=
λ x hx, hx.imp_symm (λ h, mul_indicator_of_not_mem h f)
@[simp, to_additive] lemma mul_indicator_mul_support : mul_indicator (mul_support f) f = f :=
mul_indicator_eq_self.2 subset.rfl
@[simp, to_additive] lemma mul_indicator_range_comp {ι : Sort*} (f : ι → α) (g : α → M) :
mul_indicator (range f) g ∘ f = g ∘ f :=
by letI := classical.dec_pred (∈ range f); exact piecewise_range_comp _ _ _
@[to_additive] lemma mul_indicator_congr (h : eq_on f g s) :
mul_indicator s f = mul_indicator s g :=
funext $ λx, by { simp only [mul_indicator], split_ifs, { exact h h_1 }, refl }
@[simp, to_additive] lemma mul_indicator_univ (f : α → M) : mul_indicator (univ : set α) f = f :=
mul_indicator_eq_self.2 $ subset_univ _
@[simp, to_additive] lemma mul_indicator_empty (f : α → M) : mul_indicator (∅ : set α) f = λa, 1 :=
mul_indicator_eq_one.2 $ disjoint_empty _
@[to_additive] lemma mul_indicator_empty' (f : α → M) : mul_indicator (∅ : set α) f = 1 :=
mul_indicator_empty f
variable (M)
@[simp, to_additive] lemma mul_indicator_one (s : set α) :
mul_indicator s (λx, (1:M)) = λx, (1:M) :=
mul_indicator_eq_one.2 $ by simp only [mul_support_one, empty_disjoint]
@[simp, to_additive] lemma mul_indicator_one' {s : set α} : s.mul_indicator (1 : α → M) = 1 :=
mul_indicator_one M s
variable {M}
@[to_additive] lemma mul_indicator_mul_indicator (s t : set α) (f : α → M) :
mul_indicator s (mul_indicator t f) = mul_indicator (s ∩ t) f :=
funext $ λx, by { simp only [mul_indicator], split_ifs, repeat {simp * at * {contextual := tt}} }
@[simp, to_additive] lemma mul_indicator_inter_mul_support (s : set α) (f : α → M) :
mul_indicator (s ∩ mul_support f) f = mul_indicator s f :=
by rw [← mul_indicator_mul_indicator, mul_indicator_mul_support]
@[to_additive] lemma comp_mul_indicator (h : M → β) (f : α → M) {s : set α} {x : α}
[decidable_pred (∈ s)] :
h (s.mul_indicator f x) = s.piecewise (h ∘ f) (const α (h 1)) x :=
by letI := classical.dec_pred (∈ s); convert s.apply_piecewise f (const α 1) (λ _, h)
@[to_additive] lemma mul_indicator_comp_right {s : set α} (f : β → α) {g : α → M} {x : β} :
mul_indicator (f ⁻¹' s) (g ∘ f) x = mul_indicator s g (f x) :=
by { simp only [mul_indicator], split_ifs; refl }
@[to_additive] lemma mul_indicator_image {s : set α} {f : β → M} {g : α → β} (hg : injective g)
{x : α} : mul_indicator (g '' s) f (g x) = mul_indicator s (f ∘ g) x :=
by rw [← mul_indicator_comp_right, preimage_image_eq _ hg]
@[to_additive] lemma mul_indicator_comp_of_one {g : M → N} (hg : g 1 = 1) :
mul_indicator s (g ∘ f) = g ∘ (mul_indicator s f) :=
begin
funext,
simp only [mul_indicator],
split_ifs; simp [*]
end
@[to_additive] lemma comp_mul_indicator_const (c : M) (f : M → N) (hf : f 1 = 1) :
(λ x, f (s.mul_indicator (λ x, c) x)) = s.mul_indicator (λ x, f c) :=
(mul_indicator_comp_of_one hf).symm
@[to_additive] lemma mul_indicator_preimage (s : set α) (f : α → M) (B : set M) :
(mul_indicator s f)⁻¹' B = s.ite (f ⁻¹' B) (1 ⁻¹' B) :=
by letI := classical.dec_pred (∈ s); exact piecewise_preimage s f 1 B
@[to_additive] lemma mul_indicator_preimage_of_not_mem (s : set α) (f : α → M)
{t : set M} (ht : (1:M) ∉ t) :
(mul_indicator s f)⁻¹' t = f ⁻¹' t ∩ s :=
by simp [mul_indicator_preimage, pi.one_def, set.preimage_const_of_not_mem ht]
@[to_additive] lemma mem_range_mul_indicator {r : M} {s : set α} {f : α → M} :
r ∈ range (mul_indicator s f) ↔ (r = 1 ∧ s ≠ univ) ∨ (r ∈ f '' s) :=
by simp [mul_indicator, ite_eq_iff, exists_or_distrib, eq_univ_iff_forall, and_comm, or_comm,
@eq_comm _ r 1]
@[to_additive] lemma mul_indicator_rel_mul_indicator {r : M → M → Prop} (h1 : r 1 1)
(ha : a ∈ s → r (f a) (g a)) :
r (mul_indicator s f a) (mul_indicator s g a) :=
by { simp only [mul_indicator], split_ifs with has has, exacts [ha has, h1] }
end has_one
section monoid
variables [mul_one_class M] {s t : set α} {f g : α → M} {a : α}
@[to_additive] lemma mul_indicator_union_mul_inter_apply (f : α → M) (s t : set α) (a : α) :
mul_indicator (s ∪ t) f a * mul_indicator (s ∩ t) f a =
mul_indicator s f a * mul_indicator t f a :=
by by_cases hs : a ∈ s; by_cases ht : a ∈ t; simp *
@[to_additive] lemma mul_indicator_union_mul_inter (f : α → M) (s t : set α) :
mul_indicator (s ∪ t) f * mul_indicator (s ∩ t) f = mul_indicator s f * mul_indicator t f :=
funext $ mul_indicator_union_mul_inter_apply f s t
@[to_additive] lemma mul_indicator_union_of_not_mem_inter (h : a ∉ s ∩ t) (f : α → M) :
mul_indicator (s ∪ t) f a = mul_indicator s f a * mul_indicator t f a :=
by rw [← mul_indicator_union_mul_inter_apply f s t, mul_indicator_of_not_mem h, mul_one]
@[to_additive] lemma mul_indicator_union_of_disjoint (h : disjoint s t) (f : α → M) :
mul_indicator (s ∪ t) f = λa, mul_indicator s f a * mul_indicator t f a :=
funext $ λa, mul_indicator_union_of_not_mem_inter (λ ha, h ha) _
@[to_additive] lemma mul_indicator_mul (s : set α) (f g : α → M) :
mul_indicator s (λa, f a * g a) = λa, mul_indicator s f a * mul_indicator s g a :=
by { funext, simp only [mul_indicator], split_ifs, { refl }, rw mul_one }
@[to_additive] lemma mul_indicator_mul' (s : set α) (f g : α → M) :
mul_indicator s (f * g) = mul_indicator s f * mul_indicator s g :=
mul_indicator_mul s f g
@[simp, to_additive] lemma mul_indicator_compl_mul_self_apply (s : set α) (f : α → M) (a : α) :
mul_indicator sᶜ f a * mul_indicator s f a = f a :=
classical.by_cases (λ ha : a ∈ s, by simp [ha]) (λ ha, by simp [ha])
@[simp, to_additive] lemma mul_indicator_compl_mul_self (s : set α) (f : α → M) :
mul_indicator sᶜ f * mul_indicator s f = f :=
funext $ mul_indicator_compl_mul_self_apply s f
@[simp, to_additive] lemma mul_indicator_self_mul_compl_apply (s : set α) (f : α → M) (a : α) :
mul_indicator s f a * mul_indicator sᶜ f a = f a :=
classical.by_cases (λ ha : a ∈ s, by simp [ha]) (λ ha, by simp [ha])
@[simp, to_additive] lemma mul_indicator_self_mul_compl (s : set α) (f : α → M) :
mul_indicator s f * mul_indicator sᶜ f = f :=
funext $ mul_indicator_self_mul_compl_apply s f
@[to_additive] lemma mul_indicator_mul_eq_left {f g : α → M}
(h : disjoint (mul_support f) (mul_support g)) :
(mul_support f).mul_indicator (f * g) = f :=
begin
refine (mul_indicator_congr $ λ x hx, _).trans mul_indicator_mul_support,
have : g x = 1, from nmem_mul_support.1 (disjoint_left.1 h hx),
rw [pi.mul_apply, this, mul_one]
end
@[to_additive] lemma mul_indicator_mul_eq_right {f g : α → M}
(h : disjoint (mul_support f) (mul_support g)) :
(mul_support g).mul_indicator (f * g) = g :=
begin
refine (mul_indicator_congr $ λ x hx, _).trans mul_indicator_mul_support,
have : f x = 1, from nmem_mul_support.1 (disjoint_right.1 h hx),
rw [pi.mul_apply, this, one_mul]
end
@[to_additive] lemma mul_indicator_mul_compl_eq_piecewise
[decidable_pred (∈ s)] (f g : α → M) :
s.mul_indicator f * sᶜ.mul_indicator g = s.piecewise f g :=
begin
ext x,
by_cases h : x ∈ s,
{ rw [piecewise_eq_of_mem _ _ _ h, pi.mul_apply, set.mul_indicator_of_mem h,
set.mul_indicator_of_not_mem (set.not_mem_compl_iff.2 h), mul_one] },
{ rw [piecewise_eq_of_not_mem _ _ _ h, pi.mul_apply, set.mul_indicator_of_not_mem h,
set.mul_indicator_of_mem (set.mem_compl h), one_mul] },
end
/-- `set.mul_indicator` as a `monoid_hom`. -/
@[to_additive "`set.indicator` as an `add_monoid_hom`."]
noncomputable def mul_indicator_hom {α} (M) [mul_one_class M] (s : set α) : (α → M) →* (α → M) :=
{ to_fun := mul_indicator s,
map_one' := mul_indicator_one M s,
map_mul' := mul_indicator_mul s }
end monoid
section distrib_mul_action
variables {A : Type*} [add_monoid A] [monoid M] [distrib_mul_action M A]
lemma indicator_smul_apply (s : set α) (r : α → M) (f : α → A) (x : α) :
indicator s (λ x, r x • f x) x = r x • indicator s f x :=
by { dunfold indicator, split_ifs, exacts [rfl, (smul_zero (r x)).symm] }
lemma indicator_smul (s : set α) (r : α → M) (f : α → A) :
indicator s (λ (x : α), r x • f x) = λ (x : α), r x • indicator s f x :=
funext $ indicator_smul_apply s r f
lemma indicator_const_smul_apply (s : set α) (r : M) (f : α → A) (x : α) :
indicator s (λ x, r • f x) x = r • indicator s f x :=
indicator_smul_apply s (λ x, r) f x
lemma indicator_const_smul (s : set α) (r : M) (f : α → A) :
indicator s (λ (x : α), r • f x) = λ (x : α), r • indicator s f x :=
funext $ indicator_const_smul_apply s r f
end distrib_mul_action
section group
variables {G : Type*} [group G] {s t : set α} {f g : α → G} {a : α}
@[to_additive] lemma mul_indicator_inv' (s : set α) (f : α → G) :
mul_indicator s (f⁻¹) = (mul_indicator s f)⁻¹ :=
(mul_indicator_hom G s).map_inv f
@[to_additive] lemma mul_indicator_inv (s : set α) (f : α → G) :
mul_indicator s (λa, (f a)⁻¹) = λa, (mul_indicator s f a)⁻¹ :=
mul_indicator_inv' s f
@[to_additive] lemma mul_indicator_div (s : set α) (f g : α → G) :
mul_indicator s (λ a, f a / g a) =
λ a, mul_indicator s f a / mul_indicator s g a :=
(mul_indicator_hom G s).map_div f g
@[to_additive] lemma mul_indicator_div' (s : set α) (f g : α → G) :
mul_indicator s (f / g) = mul_indicator s f / mul_indicator s g :=
mul_indicator_div s f g
@[to_additive indicator_compl'] lemma mul_indicator_compl (s : set α) (f : α → G) :
mul_indicator sᶜ f = f * (mul_indicator s f)⁻¹ :=
eq_mul_inv_of_mul_eq $ s.mul_indicator_compl_mul_self f
lemma indicator_compl {G} [add_group G] (s : set α) (f : α → G) :
indicator sᶜ f = f - indicator s f :=
by rw [sub_eq_add_neg, indicator_compl']
@[to_additive indicator_diff'] lemma mul_indicator_diff (h : s ⊆ t) (f : α → G) :
mul_indicator (t \ s) f = mul_indicator t f * (mul_indicator s f)⁻¹ :=
eq_mul_inv_of_mul_eq $ by rw [pi.mul_def, ← mul_indicator_union_of_disjoint disjoint_diff.symm f,
diff_union_self, union_eq_self_of_subset_right h]
lemma indicator_diff {G : Type*} [add_group G] {s t : set α} (h : s ⊆ t) (f : α → G) :
indicator (t \ s) f = indicator t f - indicator s f :=
by rw [indicator_diff' h, sub_eq_add_neg]
end group
section comm_monoid
variables [comm_monoid M]
/-- Consider a product of `g i (f i)` over a `finset`. Suppose `g` is a
function such as `pow`, which maps a second argument of `1` to
`1`. Then if `f` is replaced by the corresponding multiplicative indicator
function, the `finset` may be replaced by a possibly larger `finset`
without changing the value of the sum. -/
@[to_additive] lemma prod_mul_indicator_subset_of_eq_one [has_one N] (f : α → N)
(g : α → N → M) {s t : finset α} (h : s ⊆ t) (hg : ∀ a, g a 1 = 1) :
∏ i in s, g i (f i) = ∏ i in t, g i (mul_indicator ↑s f i) :=
begin
rw ← finset.prod_subset h _,
{ apply finset.prod_congr rfl,
intros i hi,
congr,
symmetry,
exact mul_indicator_of_mem hi _ },
{ refine λ i hi hn, _,
convert hg i,
exact mul_indicator_of_not_mem hn _ }
end
/-- Consider a sum of `g i (f i)` over a `finset`. Suppose `g` is a
function such as multiplication, which maps a second argument of 0 to
0. (A typical use case would be a weighted sum of `f i * h i` or `f i
• h i`, where `f` gives the weights that are multiplied by some other
function `h`.) Then if `f` is replaced by the corresponding indicator
function, the `finset` may be replaced by a possibly larger `finset`
without changing the value of the sum. -/
add_decl_doc set.sum_indicator_subset_of_eq_zero
/-- Taking the product of an indicator function over a possibly larger `finset` is the same as
taking the original function over the original `finset`. -/
@[to_additive "Summing an indicator function over a possibly larger `finset` is the same as summing
the original function over the original `finset`."]
lemma prod_mul_indicator_subset (f : α → M) {s t : finset α} (h : s ⊆ t) :
∏ i in s, f i = ∏ i in t, mul_indicator ↑s f i :=
prod_mul_indicator_subset_of_eq_one _ (λ a b, b) h (λ _, rfl)
@[to_additive] lemma _root_.finset.prod_mul_indicator_eq_prod_filter
(s : finset ι) (f : ι → α → M) (t : ι → set α) (g : ι → α) [decidable_pred (λ i, g i ∈ t i)]:
∏ i in s, mul_indicator (t i) (f i) (g i) = ∏ i in s.filter (λ i, g i ∈ t i), f i (g i) :=
begin
refine (finset.prod_filter_mul_prod_filter_not s (λ i, g i ∈ t i) _).symm.trans _,
refine eq.trans _ (mul_one _),
exact congr_arg2 (*)
(finset.prod_congr rfl $ λ x hx, mul_indicator_of_mem (finset.mem_filter.1 hx).2 _)
(finset.prod_eq_one $ λ x hx, mul_indicator_of_not_mem (finset.mem_filter.1 hx).2 _)
end
@[to_additive] lemma mul_indicator_finset_prod (I : finset ι) (s : set α) (f : ι → α → M) :
mul_indicator s (∏ i in I, f i) = ∏ i in I, mul_indicator s (f i) :=
(mul_indicator_hom M s).map_prod _ _
@[to_additive] lemma mul_indicator_finset_bUnion {ι} (I : finset ι)
(s : ι → set α) {f : α → M} : (∀ (i ∈ I) (j ∈ I), i ≠ j → disjoint (s i) (s j)) →
mul_indicator (⋃ i ∈ I, s i) f = λ a, ∏ i in I, mul_indicator (s i) f a :=
begin
classical,
refine finset.induction_on I _ _,
{ intro h, funext, simp },
assume a I haI ih hI,
funext,
rw [finset.prod_insert haI, finset.set_bUnion_insert, mul_indicator_union_of_not_mem_inter, ih _],
{ assume i hi j hj hij,
exact hI i (finset.mem_insert_of_mem hi) j (finset.mem_insert_of_mem hj) hij },
simp only [not_exists, exists_prop, mem_Union, mem_inter_eq, not_and],
assume hx a' ha',
refine disjoint_left.1 (hI a (finset.mem_insert_self _ _) a' (finset.mem_insert_of_mem ha') _) hx,
exact (ne_of_mem_of_not_mem ha' haI).symm
end
end comm_monoid
section mul_zero_class
variables [mul_zero_class M] {s t : set α} {f g : α → M} {a : α}
lemma indicator_mul (s : set α) (f g : α → M) :
indicator s (λa, f a * g a) = λa, indicator s f a * indicator s g a :=
by { funext, simp only [indicator], split_ifs, { refl }, rw mul_zero }
lemma indicator_mul_left (s : set α) (f g : α → M) :
indicator s (λa, f a * g a) a = indicator s f a * g a :=
by { simp only [indicator], split_ifs, { refl }, rw [zero_mul] }
lemma indicator_mul_right (s : set α) (f g : α → M) :
indicator s (λa, f a * g a) a = f a * indicator s g a :=
by { simp only [indicator], split_ifs, { refl }, rw [mul_zero] }
lemma inter_indicator_mul {t1 t2 : set α} (f g : α → M) (x : α) :
(t1 ∩ t2).indicator (λ x, f x * g x) x = t1.indicator f x * t2.indicator g x :=
by { rw [← set.indicator_indicator], simp [indicator] }
end mul_zero_class
section mul_zero_one_class
variables [mul_zero_one_class M]
lemma inter_indicator_one {s t : set α} :
(s ∩ t).indicator (1 : _ → M) = s.indicator 1 * t.indicator 1 :=
funext (λ _, by simpa only [← inter_indicator_mul, pi.mul_apply, pi.one_apply, one_mul])
lemma indicator_prod_one {s : set α} {t : set β} {x : α} {y : β} :
(s ×ˢ t : set _).indicator (1 : _ → M) (x, y) = s.indicator 1 x * t.indicator 1 y :=
begin
letI := classical.dec_pred (∈ s),
letI := classical.dec_pred (∈ t),
simp [indicator_apply, ← ite_and],
end
end mul_zero_one_class
section order
variables [has_one M] {s t : set α} {f g : α → M} {a : α} {y : M}
section
variables [has_le M]
@[to_additive] lemma mul_indicator_apply_le' (hfg : a ∈ s → f a ≤ y) (hg : a ∉ s → 1 ≤ y) :
mul_indicator s f a ≤ y :=
begin
by_cases ha : a ∈ s,
{ simpa [ha] using hfg ha },
{ simpa [ha] using hg ha },
end
@[to_additive] lemma mul_indicator_le' (hfg : ∀ a ∈ s, f a ≤ g a) (hg : ∀ a ∉ s, 1 ≤ g a) :
mul_indicator s f ≤ g :=
λ a, mul_indicator_apply_le' (hfg _) (hg _)
@[to_additive] lemma le_mul_indicator_apply {y} (hfg : a ∈ s → y ≤ g a) (hf : a ∉ s → y ≤ 1) :
y ≤ mul_indicator s g a :=
@mul_indicator_apply_le' α Mᵒᵈ ‹_› _ _ _ _ _ hfg hf
@[to_additive] lemma le_mul_indicator (hfg : ∀ a ∈ s, f a ≤ g a) (hf : ∀ a ∉ s, f a ≤ 1) :
f ≤ mul_indicator s g :=
λ a, le_mul_indicator_apply (hfg _) (hf _)
end
variables [preorder M]
@[to_additive indicator_apply_nonneg]
lemma one_le_mul_indicator_apply (h : a ∈ s → 1 ≤ f a) : 1 ≤ mul_indicator s f a :=
le_mul_indicator_apply h (λ _, le_rfl)
@[to_additive indicator_nonneg]
lemma one_le_mul_indicator (h : ∀ a ∈ s, 1 ≤ f a) (a : α) : 1 ≤ mul_indicator s f a :=
one_le_mul_indicator_apply (h a)
@[to_additive] lemma mul_indicator_apply_le_one (h : a ∈ s → f a ≤ 1) : mul_indicator s f a ≤ 1 :=
mul_indicator_apply_le' h (λ _, le_rfl)
@[to_additive] lemma mul_indicator_le_one (h : ∀ a ∈ s, f a ≤ 1) (a : α) :
mul_indicator s f a ≤ 1 :=
mul_indicator_apply_le_one (h a)
@[to_additive] lemma mul_indicator_le_mul_indicator (h : f a ≤ g a) :
mul_indicator s f a ≤ mul_indicator s g a :=
mul_indicator_rel_mul_indicator le_rfl (λ _, h)
attribute [mono] mul_indicator_le_mul_indicator indicator_le_indicator
@[to_additive] lemma mul_indicator_le_mul_indicator_of_subset (h : s ⊆ t) (hf : ∀ a, 1 ≤ f a)
(a : α) :
mul_indicator s f a ≤ mul_indicator t f a :=
mul_indicator_apply_le' (λ ha, le_mul_indicator_apply (λ _, le_rfl) (λ hat, (hat $ h ha).elim))
(λ ha, one_le_mul_indicator_apply (λ _, hf _))
@[to_additive] lemma mul_indicator_le_self' (hf : ∀ x ∉ s, 1 ≤ f x) : mul_indicator s f ≤ f :=
mul_indicator_le' (λ _ _, le_rfl) hf
@[to_additive] lemma mul_indicator_Union_apply {ι M} [complete_lattice M] [has_one M]
(h1 : (⊥:M) = 1) (s : ι → set α) (f : α → M) (x : α) :
mul_indicator (⋃ i, s i) f x = ⨆ i, mul_indicator (s i) f x :=
begin
by_cases hx : x ∈ ⋃ i, s i,
{ rw [mul_indicator_of_mem hx],
rw [mem_Union] at hx,
refine le_antisymm _ (supr_le $ λ i, mul_indicator_le_self' (λ x hx, h1 ▸ bot_le) x),
rcases hx with ⟨i, hi⟩,
exact le_supr_of_le i (ge_of_eq $ mul_indicator_of_mem hi _) },
{ rw [mul_indicator_of_not_mem hx],
simp only [mem_Union, not_exists] at hx,
simp [hx, ← h1] }
end
end order
section canonically_ordered_monoid
variables [canonically_ordered_monoid M]
@[to_additive] lemma mul_indicator_le_self (s : set α) (f : α → M) :
mul_indicator s f ≤ f :=
mul_indicator_le_self' $ λ _ _, one_le _
@[to_additive] lemma mul_indicator_apply_le {a : α} {s : set α} {f g : α → M}
(hfg : a ∈ s → f a ≤ g a) :
mul_indicator s f a ≤ g a :=
mul_indicator_apply_le' hfg $ λ _, one_le _
@[to_additive] lemma mul_indicator_le {s : set α} {f g : α → M} (hfg : ∀ a ∈ s, f a ≤ g a) :
mul_indicator s f ≤ g :=
mul_indicator_le' hfg $ λ _ _, one_le _
end canonically_ordered_monoid
lemma indicator_le_indicator_nonneg {β} [linear_order β] [has_zero β] (s : set α) (f : α → β) :
s.indicator f ≤ {x | 0 ≤ f x}.indicator f :=
begin
intro x,
classical,
simp_rw indicator_apply,
split_ifs,
{ exact le_rfl, },
{ exact (not_le.mp h_1).le, },
{ exact h_1, },
{ exact le_rfl, },
end
lemma indicator_nonpos_le_indicator {β} [linear_order β] [has_zero β] (s : set α) (f : α → β) :
{x | f x ≤ 0}.indicator f ≤ s.indicator f :=
@indicator_le_indicator_nonneg α βᵒᵈ _ _ s f
end set
@[to_additive] lemma monoid_hom.map_mul_indicator
{M N : Type*} [mul_one_class M] [mul_one_class N] (f : M →* N)
(s : set α) (g : α → M) (x : α) :
f (s.mul_indicator g x) = s.mul_indicator (f ∘ g) x :=
congr_fun (set.mul_indicator_comp_of_one f.map_one).symm x
|
4e32beca58b8aa6f368e0a98144aa72f513ea1e6 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/data/zmod/defs.lean | 320127f005773b35ffe743a0907f5b4d0adc8c3f | [
"Apache-2.0"
] | permissive | urkud/mathlib | eab80095e1b9f1513bfb7f25b4fa82fa4fd02989 | 6379d39e6b5b279df9715f8011369a301b634e41 | refs/heads/master | 1,658,425,342,662 | 1,658,078,703,000 | 1,658,078,703,000 | 186,910,338 | 0 | 0 | Apache-2.0 | 1,568,512,083,000 | 1,557,958,709,000 | Lean | UTF-8 | Lean | false | false | 6,732 | lean | /-
Copyright (c) 2022 Eric Rodriguez. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Rodriguez
-/
import data.int.modeq
/-!
# Definition of `zmod n` + basic results.
This file provides the basic details of `zmod n`, including its commutative ring structure.
## Implementation details
This used to be inlined into data/zmod/basic.lean. This file imports `char_p/basic`, which is an
issue; all `char_p` instances create an `algebra (zmod p) R` instance; however, this instance may
not be definitionally equal to other `algebra` instances (for example, `galois_field` also has an
`algebra` instance as it is defined as a `splitting_field`). The way to fix this is to use the
forgetful inheritance pattern, and make `char_p` carry the data of what the `smul` should be (so
for example, the `smul` on the `galois_field` `char_p` instance should be equal to the `smul` from
its `splitting_field` structure); there is only one possible `zmod p` algebra for any `p`, so this
is not an issue mathematically. For this to be possible, however, we need `char_p/basic` to be
able to import some part of `zmod`.
-/
namespace fin
/-!
## Ring structure on `fin n`
We define a commutative ring structure on `fin n`, but we do not register it as instance.
Afterwords, when we define `zmod n` in terms of `fin n`, we use these definitions
to register the ring structure on `zmod n` as type class instance.
-/
open nat.modeq int
/-- Multiplicative commutative semigroup structure on `fin (n+1)`. -/
instance (n : ℕ) : comm_semigroup (fin (n+1)) :=
{ mul_assoc := λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq
(calc ((a * b) % (n+1) * c) ≡ a * b * c [MOD (n+1)] : (nat.mod_modeq _ _).mul_right _
... ≡ a * (b * c) [MOD (n+1)] : by rw mul_assoc
... ≡ a * (b * c % (n+1)) [MOD (n+1)] : (nat.mod_modeq _ _).symm.mul_left _),
mul_comm := λ ⟨a, _⟩ ⟨b, _⟩,
fin.eq_of_veq (show (a * b) % (n+1) = (b * a) % (n+1), by rw mul_comm),
..fin.has_mul }
private lemma left_distrib_aux (n : ℕ) : ∀ a b c : fin (n+1), a * (b + c) = a * b + a * c :=
λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq
(calc a * ((b + c) % (n+1)) ≡ a * (b + c) [MOD (n+1)] : (nat.mod_modeq _ _).mul_left _
... ≡ a * b + a * c [MOD (n+1)] : by rw mul_add
... ≡ (a * b) % (n+1) + (a * c) % (n+1) [MOD (n+1)] :
(nat.mod_modeq _ _).symm.add (nat.mod_modeq _ _).symm)
/-- Commutative ring structure on `fin (n+1)`. -/
instance (n : ℕ) : comm_ring (fin (n+1)) :=
{ one_mul := fin.one_mul,
mul_one := fin.mul_one,
left_distrib := left_distrib_aux n,
right_distrib := λ a b c, by rw [mul_comm, left_distrib_aux, mul_comm _ b, mul_comm]; refl,
..fin.add_monoid_with_one,
..fin.add_comm_group n,
..fin.comm_semigroup n }
end fin
/-- The integers modulo `n : ℕ`. -/
def zmod : ℕ → Type
| 0 := ℤ
| (n+1) := fin (n+1)
instance zmod.decidable_eq : Π (n : ℕ), decidable_eq (zmod n)
| 0 := int.decidable_eq
| (n+1) := fin.decidable_eq _
instance zmod.has_repr : Π (n : ℕ), has_repr (zmod n)
| 0 := int.has_repr
| (n+1) := fin.has_repr _
namespace zmod
instance fintype : Π (n : ℕ) [fact (0 < n)], fintype (zmod n)
| 0 h := (lt_irrefl _ h.1).elim
| (n+1) _ := fin.fintype (n+1)
instance infinite : infinite (zmod 0) :=
int.infinite
@[simp] lemma card (n : ℕ) [fintype (zmod n)] : fintype.card (zmod n) = n :=
begin
casesI n,
{ exact (not_fintype (zmod 0)).elim },
{ convert fintype.card_fin (n+1) }
end
/- We define each field by cases, to ensure that the eta-expanded `zmod.comm_ring` is defeq to the
original, this helps avoid diamonds with instances coming from classes extending `comm_ring` such as
field. -/
instance comm_ring (n : ℕ) : comm_ring (zmod n) :=
{ add := nat.cases_on n ((@has_add.add) int _) (λ n, @has_add.add (fin n.succ) _),
add_assoc := nat.cases_on n (@add_assoc int _) (λ n, @add_assoc (fin n.succ) _),
zero := nat.cases_on n (0 : int) (λ n, (0 : fin n.succ)),
zero_add := nat.cases_on n (@zero_add int _) (λ n, @zero_add (fin n.succ) _),
add_zero := nat.cases_on n (@add_zero int _) (λ n, @add_zero (fin n.succ) _),
neg := nat.cases_on n ((@has_neg.neg) int _) (λ n, @has_neg.neg (fin n.succ) _),
sub := nat.cases_on n ((@has_sub.sub) int _) (λ n, @has_sub.sub (fin n.succ) _),
sub_eq_add_neg := nat.cases_on n (@sub_eq_add_neg int _) (λ n, @sub_eq_add_neg (fin n.succ) _),
zsmul := nat.cases_on n ((@comm_ring.zsmul) int _) (λ n, @comm_ring.zsmul (fin n.succ) _),
zsmul_zero' := nat.cases_on n (@comm_ring.zsmul_zero' int _)
(λ n, @comm_ring.zsmul_zero' (fin n.succ) _),
zsmul_succ' := nat.cases_on n (@comm_ring.zsmul_succ' int _)
(λ n, @comm_ring.zsmul_succ' (fin n.succ) _),
zsmul_neg' := nat.cases_on n (@comm_ring.zsmul_neg' int _)
(λ n, @comm_ring.zsmul_neg' (fin n.succ) _),
nsmul := nat.cases_on n ((@comm_ring.nsmul) int _) (λ n, @comm_ring.nsmul (fin n.succ) _),
nsmul_zero' := nat.cases_on n (@comm_ring.nsmul_zero' int _)
(λ n, @comm_ring.nsmul_zero' (fin n.succ) _),
nsmul_succ' := nat.cases_on n (@comm_ring.nsmul_succ' int _)
(λ n, @comm_ring.nsmul_succ' (fin n.succ) _),
add_left_neg := by { cases n, exacts [@add_left_neg int _, @add_left_neg (fin n.succ) _] },
add_comm := nat.cases_on n (@add_comm int _) (λ n, @add_comm (fin n.succ) _),
mul := nat.cases_on n ((@has_mul.mul) int _) (λ n, @has_mul.mul (fin n.succ) _),
mul_assoc := nat.cases_on n (@mul_assoc int _) (λ n, @mul_assoc (fin n.succ) _),
one := nat.cases_on n (1 : int) (λ n, (1 : fin n.succ)),
one_mul := nat.cases_on n (@one_mul int _) (λ n, @one_mul (fin n.succ) _),
mul_one := nat.cases_on n (@mul_one int _) (λ n, @mul_one (fin n.succ) _),
nat_cast := nat.cases_on n (coe : ℕ → ℤ) (λ n, (coe : ℕ → fin n.succ)),
nat_cast_zero := nat.cases_on n (@nat.cast_zero int _) (λ n, @nat.cast_zero (fin n.succ) _),
nat_cast_succ := nat.cases_on n (@nat.cast_succ int _) (λ n, @nat.cast_succ (fin n.succ) _),
int_cast := nat.cases_on n (coe : ℤ → ℤ) (λ n, (coe : ℤ → fin n.succ)),
int_cast_of_nat := nat.cases_on n (@int.cast_of_nat int _) (λ n, @int.cast_of_nat (fin n.succ) _),
int_cast_neg_succ_of_nat := nat.cases_on n (@int.cast_neg_succ_of_nat int _)
(λ n, @int.cast_neg_succ_of_nat (fin n.succ) _),
left_distrib := nat.cases_on n (@left_distrib int _ _ _) (λ n, @left_distrib (fin n.succ) _ _ _),
right_distrib :=
nat.cases_on n (@right_distrib int _ _ _) (λ n, @right_distrib (fin n.succ) _ _ _),
mul_comm := nat.cases_on n (@mul_comm int _) (λ n, @mul_comm (fin n.succ) _) }
instance inhabited (n : ℕ) : inhabited (zmod n) := ⟨0⟩
end zmod
|
18f41385364ef7be99b128121e9edc3cb658fdac | d1a52c3f208fa42c41df8278c3d280f075eb020c | /tests/lean/run/335.lean | e2515ac48b53686a9ec6f2d66f993d79f14255ea | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | cipher1024/lean4 | 6e1f98bb58e7a92b28f5364eb38a14c8d0aae393 | 69114d3b50806264ef35b57394391c3e738a9822 | refs/heads/master | 1,642,227,983,603 | 1,642,011,696,000 | 1,642,011,696,000 | 228,607,691 | 0 | 0 | Apache-2.0 | 1,576,584,269,000 | 1,576,584,268,000 | null | UTF-8 | Lean | false | false | 617 | lean | constant foo : {x : Nat} → Type
constant bar : {T : Type} → ({x : T} → Type) → Type
structure Baz where
baz : {x : Nat} → Type
#check bar foo
#check fun (b : Baz) => bar b.baz
structure Ty where
ctx : Type
ty : ctx → Type
structure Tm where
ty : Ty
tm : ∀ {Γ}, ty.ty Γ
#check fun (Γ : Type)
(A : Ty)
(Actx : Γ = A.ctx)
(x : Tm)
(xTy : x.ty = A)
=> Eq.rec (motive := fun ty _ => ∀ {Γ:ty.ctx}, ty.ty Γ) (fun {Γ} => x.tm (Γ:=Γ)) xTy
#check fun (Γ : Type)
(A : Ty)
(Actx : Γ = A.ctx)
(x : Tm)
(xTy : x.ty = A)
=> Eq.rec (motive := fun ty _ => ∀ {Γ:ty.ctx}, ty.ty Γ) x.tm xTy
|
487b5edd0e596765a1a1f51bdf8e21ee07edf118 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /stage0/src/Std/Data/HashMap.lean | 5b3c0bc1ed624e28fa546c08cc0d985dbcd5d7ee | [
"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 | 7,719 | lean | /-
Copyright (c) 2018 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
import Std.Data.AssocList
namespace Std
universe u v w
def HashMapBucket (α : Type u) (β : Type v) :=
{ b : Array (AssocList α β) // b.size > 0 }
def HashMapBucket.update {α : Type u} {β : Type v} (data : HashMapBucket α β) (i : USize) (d : AssocList α β) (h : i.toNat < data.val.size) : HashMapBucket α β :=
⟨ data.val.uset i d h,
by erw [Array.size_set]; exact data.property ⟩
structure HashMapImp (α : Type u) (β : Type v) where
size : Nat
buckets : HashMapBucket α β
def mkHashMapImp {α : Type u} {β : Type v} (nbuckets := 8) : HashMapImp α β :=
let n := if nbuckets = 0 then 8 else nbuckets;
{ size := 0,
buckets :=
⟨ mkArray n AssocList.nil,
by simp; cases nbuckets; decide; apply Nat.zeroLtSucc; done ⟩ }
namespace HashMapImp
variable {α : Type u} {β : Type v}
def mkIdx {n : Nat} (h : n > 0) (u : USize) : { u : USize // u.toNat < n } :=
⟨u % n, USize.modn_lt _ h⟩
@[inline] def reinsertAux (hashFn : α → UInt64) (data : HashMapBucket α β) (a : α) (b : β) : HashMapBucket α β :=
let ⟨i, h⟩ := mkIdx data.property (hashFn a |>.toUSize)
data.update i (AssocList.cons a b (data.val.uget i h)) h
@[inline] def foldBucketsM {δ : Type w} {m : Type w → Type w} [Monad m] (data : HashMapBucket α β) (d : δ) (f : δ → α → β → m δ) : m δ :=
data.val.foldlM (init := d) fun d b => b.foldlM f d
@[inline] def foldBuckets {δ : Type w} (data : HashMapBucket α β) (d : δ) (f : δ → α → β → δ) : δ :=
Id.run $ foldBucketsM data d f
@[inline] def foldM {δ : Type w} {m : Type w → Type w} [Monad m] (f : δ → α → β → m δ) (d : δ) (h : HashMapImp α β) : m δ :=
foldBucketsM h.buckets d f
@[inline] def fold {δ : Type w} (f : δ → α → β → δ) (d : δ) (m : HashMapImp α β) : δ :=
foldBuckets m.buckets d f
@[inline] def forBucketsM {m : Type w → Type w} [Monad m] (data : HashMapBucket α β) (f : α → β → m PUnit) : m PUnit :=
data.val.forM fun b => b.forM f
@[inline] def forM {m : Type w → Type w} [Monad m] (f : α → β → m PUnit) (h : HashMapImp α β) : m PUnit :=
forBucketsM h.buckets f
def findEntry? [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) : Option (α × β) :=
match m with
| ⟨_, buckets⟩ =>
let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize)
(buckets.val.uget i h).findEntry? a
def find? [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) : Option β :=
match m with
| ⟨_, buckets⟩ =>
let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize)
(buckets.val.uget i h).find? a
def contains [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) : Bool :=
match m with
| ⟨_, buckets⟩ =>
let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize)
(buckets.val.uget i h).contains a
-- TODO: remove `partial` by using well-founded recursion
partial def moveEntries [Hashable α] (i : Nat) (source : Array (AssocList α β)) (target : HashMapBucket α β) : HashMapBucket α β :=
if h : i < source.size then
let idx : Fin source.size := ⟨i, h⟩
let es : AssocList α β := source.get idx
-- We remove `es` from `source` to make sure we can reuse its memory cells when performing es.foldl
let source := source.set idx AssocList.nil
let target := es.foldl (reinsertAux hash) target
moveEntries (i+1) source target
else target
def expand [Hashable α] (size : Nat) (buckets : HashMapBucket α β) : HashMapImp α β :=
let nbuckets := buckets.val.size * 2
have : nbuckets > 0 := Nat.mulPos buckets.property (by decide)
let new_buckets : HashMapBucket α β := ⟨mkArray nbuckets AssocList.nil, by simp; assumption⟩
{ size := size,
buckets := moveEntries 0 buckets.val new_buckets }
def insert [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) (b : β) : HashMapImp α β :=
match m with
| ⟨size, buckets⟩ =>
let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize)
let bkt := buckets.val.uget i h
if bkt.contains a then
⟨size, buckets.update i (bkt.replace a b) h⟩
else
let size' := size + 1
let buckets' := buckets.update i (AssocList.cons a b bkt) h
if size' ≤ buckets.val.size then
{ size := size', buckets := buckets' }
else
expand size' buckets'
def erase [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) : HashMapImp α β :=
match m with
| ⟨ size, buckets ⟩ =>
let ⟨i, h⟩ := mkIdx buckets.property (hash a |>.toUSize)
let bkt := buckets.val.uget i h
if bkt.contains a then ⟨size - 1, buckets.update i (bkt.erase a) h⟩
else m
inductive WellFormed [BEq α] [Hashable α] : HashMapImp α β → Prop where
| mkWff : ∀ n, WellFormed (mkHashMapImp n)
| insertWff : ∀ m a b, WellFormed m → WellFormed (insert m a b)
| eraseWff : ∀ m a, WellFormed m → WellFormed (erase m a)
end HashMapImp
def HashMap (α : Type u) (β : Type v) [BEq α] [Hashable α] :=
{ m : HashMapImp α β // m.WellFormed }
open Std.HashMapImp
def mkHashMap {α : Type u} {β : Type v} [BEq α] [Hashable α] (nbuckets := 8) : HashMap α β :=
⟨ mkHashMapImp nbuckets, WellFormed.mkWff nbuckets ⟩
namespace HashMap
variable {α : Type u} {β : Type v} [BEq α] [Hashable α]
instance : Inhabited (HashMap α β) where
default := mkHashMap
instance : EmptyCollection (HashMap α β) := ⟨mkHashMap⟩
@[inline] def insert (m : HashMap α β) (a : α) (b : β) : HashMap α β :=
match m with
| ⟨ m, hw ⟩ => ⟨ m.insert a b, WellFormed.insertWff m a b hw ⟩
@[inline] def erase (m : HashMap α β) (a : α) : HashMap α β :=
match m with
| ⟨ m, hw ⟩ => ⟨ m.erase a, WellFormed.eraseWff m a hw ⟩
@[inline] def findEntry? (m : HashMap α β) (a : α) : Option (α × β) :=
match m with
| ⟨ m, _ ⟩ => m.findEntry? a
@[inline] def find? (m : HashMap α β) (a : α) : Option β :=
match m with
| ⟨ m, _ ⟩ => m.find? a
@[inline] def findD (m : HashMap α β) (a : α) (b₀ : β) : β :=
(m.find? a).getD b₀
@[inline] def find! [Inhabited β] (m : HashMap α β) (a : α) : β :=
match m.find? a with
| some b => b
| none => panic! "key is not in the map"
@[inline] def getOp (self : HashMap α β) (idx : α) : Option β :=
self.find? idx
@[inline] def contains (m : HashMap α β) (a : α) : Bool :=
match m with
| ⟨ m, _ ⟩ => m.contains a
@[inline] def foldM {δ : Type w} {m : Type w → Type w} [Monad m] (f : δ → α → β → m δ) (init : δ) (h : HashMap α β) : m δ :=
match h with
| ⟨ h, _ ⟩ => h.foldM f init
@[inline] def fold {δ : Type w} (f : δ → α → β → δ) (init : δ) (m : HashMap α β) : δ :=
match m with
| ⟨ m, _ ⟩ => m.fold f init
@[inline] def forM {m : Type w → Type w} [Monad m] (f : α → β → m PUnit) (h : HashMap α β) : m PUnit :=
match h with
| ⟨ h, _ ⟩ => h.forM f
@[inline] def size (m : HashMap α β) : Nat :=
match m with
| ⟨ {size := sz, ..}, _ ⟩ => sz
@[inline] def isEmpty (m : HashMap α β) : Bool :=
m.size = 0
@[inline] def empty : HashMap α β :=
mkHashMap
def toList (m : HashMap α β) : List (α × β) :=
m.fold (init := []) fun r k v => (k, v)::r
def toArray (m : HashMap α β) : Array (α × β) :=
m.fold (init := #[]) fun r k v => r.push (k, v)
def numBuckets (m : HashMap α β) : Nat :=
m.val.buckets.val.size
end HashMap
end Std
|
5141f604b6ea781d5c73a4ce15fbdcdcbc0a5664 | 592ee40978ac7604005a4e0d35bbc4b467389241 | /Library/generated/mathscheme-lean/CommutativeMonoid1.lean | 2ce8082bac3ed2e270f4c9796d8a75a5259198b5 | [] | no_license | ysharoda/Deriving-Definitions | 3e149e6641fae440badd35ac110a0bd705a49ad2 | dfecb27572022de3d4aa702cae8db19957523a59 | refs/heads/master | 1,679,127,857,700 | 1,615,939,007,000 | 1,615,939,007,000 | 229,785,731 | 4 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,903 | lean | import init.data.nat.basic
import init.data.fin.basic
import data.vector
import .Prelude
open Staged
open nat
open fin
open vector
section CommutativeMonoid1
structure CommutativeMonoid1 (A : Type) : Type :=
(op : (A → (A → A)))
(one : A)
(lunit_one : (∀ {x : A} , (op one x) = x))
(runit_one : (∀ {x : A} , (op x one) = x))
(associative_op : (∀ {x y z : A} , (op (op x y) z) = (op x (op y z))))
(commutative_op : (∀ {x y : A} , (op x y) = (op y x)))
open CommutativeMonoid1
structure Sig (AS : Type) : Type :=
(opS : (AS → (AS → AS)))
(oneS : AS)
structure Product (A : Type) : Type :=
(opP : ((Prod A A) → ((Prod A A) → (Prod A A))))
(oneP : (Prod A A))
(lunit_1P : (∀ {xP : (Prod A A)} , (opP oneP xP) = xP))
(runit_1P : (∀ {xP : (Prod A A)} , (opP xP oneP) = xP))
(associative_opP : (∀ {xP yP zP : (Prod A A)} , (opP (opP xP yP) zP) = (opP xP (opP yP zP))))
(commutative_opP : (∀ {xP yP : (Prod A A)} , (opP xP yP) = (opP yP xP)))
structure Hom {A1 : Type} {A2 : Type} (Co1 : (CommutativeMonoid1 A1)) (Co2 : (CommutativeMonoid1 A2)) : Type :=
(hom : (A1 → A2))
(pres_op : (∀ {x1 x2 : A1} , (hom ((op Co1) x1 x2)) = ((op Co2) (hom x1) (hom x2))))
(pres_one : (hom (one Co1)) = (one Co2))
structure RelInterp {A1 : Type} {A2 : Type} (Co1 : (CommutativeMonoid1 A1)) (Co2 : (CommutativeMonoid1 A2)) : Type 1 :=
(interp : (A1 → (A2 → Type)))
(interp_op : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((op Co1) x1 x2) ((op Co2) y1 y2))))))
(interp_one : (interp (one Co1) (one Co2)))
inductive CommutativeMonoid1LTerm : Type
| opL : (CommutativeMonoid1LTerm → (CommutativeMonoid1LTerm → CommutativeMonoid1LTerm))
| oneL : CommutativeMonoid1LTerm
open CommutativeMonoid1LTerm
inductive ClCommutativeMonoid1ClTerm (A : Type) : Type
| sing : (A → ClCommutativeMonoid1ClTerm)
| opCl : (ClCommutativeMonoid1ClTerm → (ClCommutativeMonoid1ClTerm → ClCommutativeMonoid1ClTerm))
| oneCl : ClCommutativeMonoid1ClTerm
open ClCommutativeMonoid1ClTerm
inductive OpCommutativeMonoid1OLTerm (n : ℕ) : Type
| v : ((fin n) → OpCommutativeMonoid1OLTerm)
| opOL : (OpCommutativeMonoid1OLTerm → (OpCommutativeMonoid1OLTerm → OpCommutativeMonoid1OLTerm))
| oneOL : OpCommutativeMonoid1OLTerm
open OpCommutativeMonoid1OLTerm
inductive OpCommutativeMonoid1OL2Term2 (n : ℕ) (A : Type) : Type
| v2 : ((fin n) → OpCommutativeMonoid1OL2Term2)
| sing2 : (A → OpCommutativeMonoid1OL2Term2)
| opOL2 : (OpCommutativeMonoid1OL2Term2 → (OpCommutativeMonoid1OL2Term2 → OpCommutativeMonoid1OL2Term2))
| oneOL2 : OpCommutativeMonoid1OL2Term2
open OpCommutativeMonoid1OL2Term2
def simplifyCl {A : Type} : ((ClCommutativeMonoid1ClTerm A) → (ClCommutativeMonoid1ClTerm A))
| (opCl oneCl x) := x
| (opCl x oneCl) := x
| (opCl x1 x2) := (opCl (simplifyCl x1) (simplifyCl x2))
| oneCl := oneCl
| (sing x1) := (sing x1)
def simplifyOpB {n : ℕ} : ((OpCommutativeMonoid1OLTerm n) → (OpCommutativeMonoid1OLTerm n))
| (opOL oneOL x) := x
| (opOL x oneOL) := x
| (opOL x1 x2) := (opOL (simplifyOpB x1) (simplifyOpB x2))
| oneOL := oneOL
| (v x1) := (v x1)
def simplifyOp {n : ℕ} {A : Type} : ((OpCommutativeMonoid1OL2Term2 n A) → (OpCommutativeMonoid1OL2Term2 n A))
| (opOL2 oneOL2 x) := x
| (opOL2 x oneOL2) := x
| (opOL2 x1 x2) := (opOL2 (simplifyOp x1) (simplifyOp x2))
| oneOL2 := oneOL2
| (v2 x1) := (v2 x1)
| (sing2 x1) := (sing2 x1)
def evalB {A : Type} : ((CommutativeMonoid1 A) → (CommutativeMonoid1LTerm → A))
| Co (opL x1 x2) := ((op Co) (evalB Co x1) (evalB Co x2))
| Co oneL := (one Co)
def evalCl {A : Type} : ((CommutativeMonoid1 A) → ((ClCommutativeMonoid1ClTerm A) → A))
| Co (sing x1) := x1
| Co (opCl x1 x2) := ((op Co) (evalCl Co x1) (evalCl Co x2))
| Co oneCl := (one Co)
def evalOpB {A : Type} {n : ℕ} : ((CommutativeMonoid1 A) → ((vector A n) → ((OpCommutativeMonoid1OLTerm n) → A)))
| Co vars (v x1) := (nth vars x1)
| Co vars (opOL x1 x2) := ((op Co) (evalOpB Co vars x1) (evalOpB Co vars x2))
| Co vars oneOL := (one Co)
def evalOp {A : Type} {n : ℕ} : ((CommutativeMonoid1 A) → ((vector A n) → ((OpCommutativeMonoid1OL2Term2 n A) → A)))
| Co vars (v2 x1) := (nth vars x1)
| Co vars (sing2 x1) := x1
| Co vars (opOL2 x1 x2) := ((op Co) (evalOp Co vars x1) (evalOp Co vars x2))
| Co vars oneOL2 := (one Co)
def inductionB {P : (CommutativeMonoid1LTerm → Type)} : ((∀ (x1 x2 : CommutativeMonoid1LTerm) , ((P x1) → ((P x2) → (P (opL x1 x2))))) → ((P oneL) → (∀ (x : CommutativeMonoid1LTerm) , (P x))))
| popl p1l (opL x1 x2) := (popl _ _ (inductionB popl p1l x1) (inductionB popl p1l x2))
| popl p1l oneL := p1l
def inductionCl {A : Type} {P : ((ClCommutativeMonoid1ClTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClCommutativeMonoid1ClTerm A)) , ((P x1) → ((P x2) → (P (opCl x1 x2))))) → ((P oneCl) → (∀ (x : (ClCommutativeMonoid1ClTerm A)) , (P x)))))
| psing popcl p1cl (sing x1) := (psing x1)
| psing popcl p1cl (opCl x1 x2) := (popcl _ _ (inductionCl psing popcl p1cl x1) (inductionCl psing popcl p1cl x2))
| psing popcl p1cl oneCl := p1cl
def inductionOpB {n : ℕ} {P : ((OpCommutativeMonoid1OLTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpCommutativeMonoid1OLTerm n)) , ((P x1) → ((P x2) → (P (opOL x1 x2))))) → ((P oneOL) → (∀ (x : (OpCommutativeMonoid1OLTerm n)) , (P x)))))
| pv popol p1ol (v x1) := (pv x1)
| pv popol p1ol (opOL x1 x2) := (popol _ _ (inductionOpB pv popol p1ol x1) (inductionOpB pv popol p1ol x2))
| pv popol p1ol oneOL := p1ol
def inductionOp {n : ℕ} {A : Type} {P : ((OpCommutativeMonoid1OL2Term2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpCommutativeMonoid1OL2Term2 n A)) , ((P x1) → ((P x2) → (P (opOL2 x1 x2))))) → ((P oneOL2) → (∀ (x : (OpCommutativeMonoid1OL2Term2 n A)) , (P x))))))
| pv2 psing2 popol2 p1ol2 (v2 x1) := (pv2 x1)
| pv2 psing2 popol2 p1ol2 (sing2 x1) := (psing2 x1)
| pv2 psing2 popol2 p1ol2 (opOL2 x1 x2) := (popol2 _ _ (inductionOp pv2 psing2 popol2 p1ol2 x1) (inductionOp pv2 psing2 popol2 p1ol2 x2))
| pv2 psing2 popol2 p1ol2 oneOL2 := p1ol2
def stageB : (CommutativeMonoid1LTerm → (Staged CommutativeMonoid1LTerm))
| (opL x1 x2) := (stage2 opL (codeLift2 opL) (stageB x1) (stageB x2))
| oneL := (Now oneL)
def stageCl {A : Type} : ((ClCommutativeMonoid1ClTerm A) → (Staged (ClCommutativeMonoid1ClTerm A)))
| (sing x1) := (Now (sing x1))
| (opCl x1 x2) := (stage2 opCl (codeLift2 opCl) (stageCl x1) (stageCl x2))
| oneCl := (Now oneCl)
def stageOpB {n : ℕ} : ((OpCommutativeMonoid1OLTerm n) → (Staged (OpCommutativeMonoid1OLTerm n)))
| (v x1) := (const (code (v x1)))
| (opOL x1 x2) := (stage2 opOL (codeLift2 opOL) (stageOpB x1) (stageOpB x2))
| oneOL := (Now oneOL)
def stageOp {n : ℕ} {A : Type} : ((OpCommutativeMonoid1OL2Term2 n A) → (Staged (OpCommutativeMonoid1OL2Term2 n A)))
| (sing2 x1) := (Now (sing2 x1))
| (v2 x1) := (const (code (v2 x1)))
| (opOL2 x1 x2) := (stage2 opOL2 (codeLift2 opOL2) (stageOp x1) (stageOp x2))
| oneOL2 := (Now oneOL2)
structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type :=
(opT : ((Repr A) → ((Repr A) → (Repr A))))
(oneT : (Repr A))
end CommutativeMonoid1 |
eb80275cfee98c6cd7090aaddd9708e0a1393262 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/geometry/manifold/algebra/lie_group.lean | 90009628761f93b527d6697c990f47279327f8df | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 8,538 | lean | /-
Copyright © 2020 Nicolò Cavalleri. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nicolò Cavalleri
-/
import geometry.manifold.algebra.monoid
/-!
# Lie groups
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
A Lie group is a group that is also a smooth manifold, in which the group operations of
multiplication and inversion are smooth maps. Smoothness of the group multiplication means that
multiplication is a smooth mapping of the product manifold `G` × `G` into `G`.
Note that, since a manifold here is not second-countable and Hausdorff a Lie group here is not
guaranteed to be second-countable (even though it can be proved it is Hausdorff). Note also that Lie
groups here are not necessarily finite dimensional.
## Main definitions and statements
* `lie_add_group I G` : a Lie additive group where `G` is a manifold on the model with corners `I`.
* `lie_group I G` : a Lie multiplicative group where `G` is a manifold on the model with
corners `I`.
* `normed_space_lie_add_group` : a normed vector space over a nontrivially normed field
is an additive Lie group.
## Implementation notes
A priori, a Lie group here is a manifold with corners.
The definition of Lie group cannot require `I : model_with_corners 𝕜 E E` with the same space as the
model space and as the model vector space, as one might hope, beause in the product situation,
the model space is `model_prod E E'` and the model vector space is `E × E'`, which are not the same,
so the definition does not apply. Hence the definition should be more general, allowing
`I : model_with_corners 𝕜 E H`.
-/
noncomputable theory
open_locale manifold
/-- A Lie (additive) group is a group and a smooth manifold at the same time in which
the addition and negation operations are smooth. -/
-- See note [Design choices about smooth algebraic structures]
@[ancestor has_smooth_add]
class lie_add_group {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{H : Type*} [topological_space H]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] (I : model_with_corners 𝕜 E H)
(G : Type*) [add_group G] [topological_space G] [charted_space H G]
extends has_smooth_add I G : Prop :=
(smooth_neg : smooth I I (λ a:G, -a))
/-- A Lie group is a group and a smooth manifold at the same time in which
the multiplication and inverse operations are smooth. -/
-- See note [Design choices about smooth algebraic structures]
@[ancestor has_smooth_mul, to_additive]
class lie_group {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{H : Type*} [topological_space H]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] (I : model_with_corners 𝕜 E H)
(G : Type*) [group G] [topological_space G] [charted_space H G]
extends has_smooth_mul I G : Prop :=
(smooth_inv : smooth I I (λ a:G, a⁻¹))
section lie_group
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{H : Type*} [topological_space H]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {I : model_with_corners 𝕜 E H}
{F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] {J : model_with_corners 𝕜 F F}
{G : Type*} [topological_space G] [charted_space H G] [group G] [lie_group I G]
{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'] {n : ℕ∞}
section
variable (I)
@[to_additive]
lemma smooth_inv : smooth I I (λ x : G, x⁻¹) :=
lie_group.smooth_inv
/-- A Lie group is a topological group. This is not an instance for technical reasons,
see note [Design choices about smooth algebraic structures]. -/
@[to_additive
"An additive Lie group is an additive topological group. This is not an instance for technical
reasons, see note [Design choices about smooth algebraic structures]."]
lemma topological_group_of_lie_group : topological_group G :=
{ continuous_inv := (smooth_inv I).continuous,
.. has_continuous_mul_of_smooth I }
end
@[to_additive]
lemma cont_mdiff_within_at.inv {f : M → G} {s : set M} {x₀ : M}
(hf : cont_mdiff_within_at I' I n f s x₀) : cont_mdiff_within_at I' I n (λx, (f x)⁻¹) s x₀ :=
((smooth_inv I).of_le le_top).cont_mdiff_at.cont_mdiff_within_at.comp x₀ hf $ set.maps_to_univ _ _
@[to_additive]
lemma cont_mdiff_at.inv {f : M → G} {x₀ : M}
(hf : cont_mdiff_at I' I n f x₀) : cont_mdiff_at I' I n (λx, (f x)⁻¹) x₀ :=
((smooth_inv I).of_le le_top).cont_mdiff_at.comp x₀ hf
@[to_additive]
lemma cont_mdiff_on.inv {f : M → G} {s : set M}
(hf : cont_mdiff_on I' I n f s) : cont_mdiff_on I' I n (λx, (f x)⁻¹) s :=
λ x hx, (hf x hx).inv
@[to_additive]
lemma cont_mdiff.inv {f : M → G}
(hf : cont_mdiff I' I n f) : cont_mdiff I' I n (λx, (f x)⁻¹) :=
λ x, (hf x).inv
@[to_additive]
lemma smooth_within_at.inv {f : M → G} {s : set M} {x₀ : M}
(hf : smooth_within_at I' I f s x₀) : smooth_within_at I' I (λx, (f x)⁻¹) s x₀ :=
hf.inv
@[to_additive]
lemma smooth_at.inv {f : M → G} {x₀ : M}
(hf : smooth_at I' I f x₀) : smooth_at I' I (λx, (f x)⁻¹) x₀ :=
hf.inv
@[to_additive]
lemma smooth_on.inv {f : M → G} {s : set M}
(hf : smooth_on I' I f s) : smooth_on I' I (λx, (f x)⁻¹) s :=
hf.inv
@[to_additive]
lemma smooth.inv {f : M → G}
(hf : smooth I' I f) : smooth I' I (λx, (f x)⁻¹) :=
hf.inv
@[to_additive]
lemma cont_mdiff_within_at.div {f g : M → G} {s : set M} {x₀ : M}
(hf : cont_mdiff_within_at I' I n f s x₀) (hg : cont_mdiff_within_at I' I n g s x₀) :
cont_mdiff_within_at I' I n (λ x, f x / g x) s x₀ :=
by { simp_rw div_eq_mul_inv, exact hf.mul hg.inv }
@[to_additive]
lemma cont_mdiff_at.div {f g : M → G} {x₀ : M}
(hf : cont_mdiff_at I' I n f x₀) (hg : cont_mdiff_at I' I n g x₀) :
cont_mdiff_at I' I n (λ x, f x / g x) x₀ :=
by { simp_rw div_eq_mul_inv, exact hf.mul hg.inv }
@[to_additive]
lemma cont_mdiff_on.div {f g : M → G} {s : set M}
(hf : cont_mdiff_on I' I n f s) (hg : cont_mdiff_on I' I n g s) :
cont_mdiff_on I' I n (λ x, f x / g x) s :=
by { simp_rw div_eq_mul_inv, exact hf.mul hg.inv }
@[to_additive]
lemma cont_mdiff.div {f g : M → G}
(hf : cont_mdiff I' I n f) (hg : cont_mdiff I' I n g) :
cont_mdiff I' I n (λ x, f x / g x) :=
by { simp_rw div_eq_mul_inv, exact hf.mul hg.inv }
@[to_additive]
lemma smooth_within_at.div {f g : M → G} {s : set M} {x₀ : M}
(hf : smooth_within_at I' I f s x₀) (hg : smooth_within_at I' I g s x₀) :
smooth_within_at I' I (λ x, f x / g x) s x₀ :=
hf.div hg
@[to_additive]
lemma smooth_at.div {f g : M → G} {x₀ : M}
(hf : smooth_at I' I f x₀) (hg : smooth_at I' I g x₀) :
smooth_at I' I (λ x, f x / g x) x₀ :=
hf.div hg
@[to_additive]
lemma smooth_on.div {f g : M → G} {s : set M}
(hf : smooth_on I' I f s) (hg : smooth_on I' I g s) : smooth_on I' I (f / g) s :=
hf.div hg
@[to_additive]
lemma smooth.div {f g : M → G}
(hf : smooth I' I f) (hg : smooth I' I g) : smooth I' I (f / g) :=
hf.div hg
end lie_group
section prod_lie_group
/- Instance of product group -/
@[to_additive]
instance {𝕜 : Type*} [nontrivially_normed_field 𝕜] {H : Type*} [topological_space H]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {I : model_with_corners 𝕜 E H}
{G : Type*} [topological_space G] [charted_space H G] [group G] [lie_group I G]
{E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{G' : Type*} [topological_space G'] [charted_space H' G']
[group G'] [lie_group I' G'] :
lie_group (I.prod I') (G×G') :=
{ smooth_inv := smooth_fst.inv.prod_mk smooth_snd.inv,
..has_smooth_mul.prod _ _ _ _ }
end prod_lie_group
/-! ### Normed spaces are Lie groups -/
instance normed_space_lie_add_group {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] :
lie_add_group (𝓘(𝕜, E)) E :=
{ smooth_add := smooth_iff.2 ⟨continuous_add, λ x y, cont_diff_add.cont_diff_on⟩,
smooth_neg := smooth_iff.2 ⟨continuous_neg, λ x y, cont_diff_neg.cont_diff_on⟩,
.. model_space_smooth }
|
a05812d395c71b9b6b184b8bb7845809bf4303f5 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/data/set/intervals/disjoint.lean | a361c5dede68cb0d673906c4a1f72affbb370c50 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,400 | lean | /-
Copyright (c) 2019 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Yury Kudryashov
-/
import data.set.lattice
/-!
# Extra lemmas about intervals
This file contains lemmas about intervals that cannot be included into `data.set.intervals.basic`
because this would create an `import` cycle. Namely, lemmas in this file can use definitions
from `data.set.lattice`, including `disjoint`.
-/
universe u
variables {α : Type u}
open order_dual (to_dual)
namespace set
section preorder
variables [preorder α] {a b c : α}
@[simp] lemma Iic_disjoint_Ioi (h : a ≤ b) : disjoint (Iic a) (Ioi b) :=
λ x ⟨ha, hb⟩, not_le_of_lt (h.trans_lt hb) ha
@[simp] lemma Iic_disjoint_Ioc (h : a ≤ b) : disjoint (Iic a) (Ioc b c) :=
(Iic_disjoint_Ioi h).mono (le_refl _) (λ _, and.left)
@[simp] lemma Ioc_disjoint_Ioc_same {a b c : α} : disjoint (Ioc a b) (Ioc b c) :=
(Iic_disjoint_Ioc (le_refl b)).mono (λ _, and.right) (le_refl _)
@[simp] lemma Ico_disjoint_Ico_same {a b c : α} : disjoint (Ico a b) (Ico b c) :=
λ x hx, not_le_of_lt hx.1.2 hx.2.1
@[simp] lemma Ici_disjoint_Iic : disjoint (Ici a) (Iic b) ↔ ¬(a ≤ b) :=
by rw [set.disjoint_iff_inter_eq_empty, Ici_inter_Iic, Icc_eq_empty_iff]
@[simp] lemma Iic_disjoint_Ici : disjoint (Iic a) (Ici b) ↔ ¬(b ≤ a) :=
disjoint.comm.trans Ici_disjoint_Iic
end preorder
section linear_order
variables [linear_order α] {a₁ a₂ b₁ b₂ : α}
@[simp] lemma Ico_disjoint_Ico : disjoint (Ico a₁ a₂) (Ico b₁ b₂) ↔ min a₂ b₂ ≤ max a₁ b₁ :=
by simp_rw [set.disjoint_iff_inter_eq_empty, Ico_inter_Ico, Ico_eq_empty_iff,
inf_eq_min, sup_eq_max, not_lt]
@[simp] lemma Ioc_disjoint_Ioc : disjoint (Ioc a₁ a₂) (Ioc b₁ b₂) ↔ min a₂ b₂ ≤ max a₁ b₁ :=
have h : _ ↔ min (to_dual a₁) (to_dual b₁) ≤ max (to_dual a₂) (to_dual b₂) := Ico_disjoint_Ico,
by simpa only [dual_Ico] using h
/-- If two half-open intervals are disjoint and the endpoint of one lies in the other,
then it must be equal to the endpoint of the other. -/
lemma eq_of_Ico_disjoint {x₁ x₂ y₁ y₂ : α}
(h : disjoint (Ico x₁ x₂) (Ico y₁ y₂)) (hx : x₁ < x₂) (h2 : x₂ ∈ Ico y₁ y₂) :
y₁ = x₂ :=
begin
rw [Ico_disjoint_Ico, min_eq_left (le_of_lt h2.2), le_max_iff] at h,
apply le_antisymm h2.1,
exact h.elim (λ h, absurd hx (not_lt_of_le h)) id
end
@[simp] lemma Union_Ico_eq_Iio_self_iff {ι : Sort*} {f : ι → α} {a : α} :
(⋃ i, Ico (f i) a) = Iio a ↔ ∀ x < a, ∃ i, f i ≤ x :=
by simp [← Ici_inter_Iio, ← Union_inter, subset_def]
@[simp] lemma Union_Ioc_eq_Ioi_self_iff {ι : Sort*} {f : ι → α} {a : α} :
(⋃ i, Ioc a (f i)) = Ioi a ↔ ∀ x, a < x → ∃ i, x ≤ f i :=
by simp [← Ioi_inter_Iic, ← inter_Union, subset_def]
@[simp] lemma bUnion_Ico_eq_Iio_self_iff {ι : Sort*} {p : ι → Prop} {f : Π i, p i → α} {a : α} :
(⋃ i (hi : p i), Ico (f i hi) a) = Iio a ↔ ∀ x < a, ∃ i hi, f i hi ≤ x :=
by simp [← Ici_inter_Iio, ← Union_inter, subset_def]
@[simp] lemma bUnion_Ioc_eq_Ioi_self_iff {ι : Sort*} {p : ι → Prop} {f : Π i, p i → α} {a : α} :
(⋃ i (hi : p i), Ioc a (f i hi)) = Ioi a ↔ ∀ x, a < x → ∃ i hi, x ≤ f i hi :=
by simp [← Ioi_inter_Iic, ← inter_Union, subset_def]
end linear_order
end set
|
e9e7199fe420bd242a1abf81afdc55e89c7dc2e3 | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/geometry/manifold/basic_smooth_bundle.lean | 8b4a3a302e6691ce332ae9f3b90dc9b946e8512e | [
"Apache-2.0"
] | permissive | ilitzroth/mathlib | ea647e67f1fdfd19a0f7bdc5504e8acec6180011 | 5254ef14e3465f6504306132fe3ba9cec9ffff16 | refs/heads/master | 1,680,086,661,182 | 1,617,715,647,000 | 1,617,715,647,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 31,758 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import topology.topological_fiber_bundle
import geometry.manifold.smooth_manifold_with_corners
/-!
# Basic smooth bundles
In general, a smooth bundle is a bundle over a smooth manifold, whose fiber is a manifold, and
for which the coordinate changes are smooth. In this definition, there are charts involved at
several places: in the manifold structure of the base, in the manifold structure of the fibers, and
in the local trivializations. This makes it a complicated object in general. There is however a
specific situation where things are much simpler: when the fiber is a vector space (no need for
charts for the fibers), and when the local trivializations of the bundle and the charts of the base
coincide. Then everything is expressed in terms of the charts of the base, making for a much
simpler overall structure, which is easier to manipulate formally.
Most vector bundles that naturally occur in differential geometry are of this form:
the tangent bundle, the cotangent bundle, differential forms (used to define de Rham cohomology)
and the bundle of Riemannian metrics. Therefore, it is worth defining a specific constructor for
this kind of bundle, that we call basic smooth bundles.
A basic smooth bundle is thus a smooth bundle over a smooth manifold whose fiber is a vector space,
and which is trivial in the coordinate charts of the base. (We recall that in our notion of manifold
there is a distinguished atlas, which does not need to be maximal: we require the triviality above
this specific atlas). It can be constructed from a basic smooth bundled core, defined below,
specifying the changes in the fiber when one goes from one coordinate chart to another one. We do
not require that this changes in fiber are linear, but only diffeomorphisms.
## Main definitions
* `basic_smooth_bundle_core I M F`: assuming that `M` is a smooth manifold over the model with
corners `I` on `(𝕜, E, H)`, and `F` is a normed vector space over `𝕜`, this structure registers,
for each pair of charts of `M`, a smooth change of coordinates on `F`. This is the core structure
from which one will build a smooth bundle with fiber `F` over `M`.
Let `Z` be a basic smooth bundle core over `M` with fiber `F`. We define
`Z.to_topological_fiber_bundle_core`, the (topological) fiber bundle core associated to `Z`. From
it, we get a space `Z.to_topological_fiber_bundle_core.total_space` (which as a Type is just `Σ (x :
M), F`), with the fiber bundle topology. It inherits a manifold structure (where the charts are in
bijection with the charts of the basis). We show that this manifold is smooth.
Then we use this machinery to construct the tangent bundle of a smooth manifold.
* `tangent_bundle_core I M`: the basic smooth bundle core associated to a smooth manifold `M` over a
model with corners `I`.
* `tangent_bundle I M` : the total space of `tangent_bundle_core I M`. It is itself a
smooth manifold over the model with corners `I.tangent`, the product of `I` and the trivial model
with corners on `E`.
* `tangent_space I x` : the tangent space to `M` at `x`
* `tangent_bundle.proj I M`: the projection from the tangent bundle to the base manifold
## Implementation notes
In the definition of a basic smooth bundle core, we do not require that the coordinate changes of
the fibers are linear map, only that they are diffeomorphisms. Therefore, the fibers of the
resulting fiber bundle do not inherit a vector space structure (as an algebraic object) in general.
As the fiber, as a type, is just `F`, one can still always register the vector space structure, but
it does not make sense to do so (i.e., it will not lead to any useful theorem) unless this structure
is canonical, i.e., the coordinate changes are linear maps.
For instance, we register the vector space structure on the fibers of the tangent bundle. However,
we do not register the normed space structure coming from that of `F` (as it is not canonical, and
we also want to keep the possibility to add a Riemannian structure on the manifold later on without
having two competing normed space instances on the tangent spaces).
We require `F` to be a normed space, and not just a topological vector space, as we want to talk
about smooth functions on `F`. The notion of derivative requires a norm to be defined.
## TODO
construct the cotangent bundle, and the bundles of differential forms. They should follow
functorially from the description of the tangent bundle as a basic smooth bundle.
## Tags
Smooth fiber bundle, vector bundle, tangent space, tangent bundle
-/
noncomputable theory
universe u
open topological_space set
open_locale manifold topological_space
/-- Core structure used to create a smooth bundle above `M` (a manifold over the model with
corner `I`) with fiber the normed vector space `F` over `𝕜`, which is trivial in the chart domains
of `M`. This structure registers the changes in the fibers when one changes coordinate charts in the
base. We do not require the change of coordinates of the fibers to be linear, only smooth.
Therefore, the fibers of the resulting bundle will not inherit a canonical vector space structure
in general. -/
structure basic_smooth_bundle_core {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_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]
(F : Type*) [normed_group F] [normed_space 𝕜 F] :=
(coord_change : atlas H M → atlas H M → H → F → F)
(coord_change_self :
∀ i : atlas H M, ∀ x ∈ i.1.target, ∀ v, coord_change i i x v = v)
(coord_change_comp : ∀ i j k : atlas H M,
∀ x ∈ ((i.1.symm.trans j.1).trans (j.1.symm.trans k.1)).source, ∀ v,
(coord_change j k ((i.1.symm.trans j.1) x)) (coord_change i j x v) = coord_change i k x v)
(coord_change_smooth : ∀ i j : atlas H M,
times_cont_diff_on 𝕜 ∞ (λp : E × F, coord_change i j (I.symm p.1) p.2)
((I '' (i.1.symm.trans j.1).source).prod (univ : set F)))
/-- The trivial basic smooth bundle core, in which all the changes of coordinates are the
identity. -/
def trivial_basic_smooth_bundle_core {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_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]
(F : Type*) [normed_group F] [normed_space 𝕜 F] : basic_smooth_bundle_core I M F :=
{ coord_change := λ i j x v, v,
coord_change_self := λ i x hx v, rfl,
coord_change_comp := λ i j k x hx v, rfl,
coord_change_smooth := λ i j, times_cont_diff_snd.times_cont_diff_on }
namespace basic_smooth_bundle_core
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_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]
{F : Type*} [normed_group F] [normed_space 𝕜 F]
(Z : basic_smooth_bundle_core I M F)
instance : inhabited (basic_smooth_bundle_core I M F) :=
⟨trivial_basic_smooth_bundle_core I M F⟩
/-- Fiber bundle core associated to a basic smooth bundle core -/
def to_topological_fiber_bundle_core : topological_fiber_bundle_core (atlas H M) M F :=
{ base_set := λi, i.1.source,
is_open_base_set := λi, i.1.open_source,
index_at := λx, ⟨chart_at H x, chart_mem_atlas H x⟩,
mem_base_set_at := λx, mem_chart_source H x,
coord_change := λi j x v, Z.coord_change i j (i.1 x) v,
coord_change_self := λi x hx v, Z.coord_change_self i (i.1 x) (i.1.map_source hx) v,
coord_change_comp := λi j k x ⟨⟨hx1, hx2⟩, hx3⟩ v, begin
have := Z.coord_change_comp i j k (i.1 x) _ v,
convert this using 2,
{ simp only [hx1] with mfld_simps },
{ simp only [hx1, hx2, hx3] with mfld_simps }
end,
coord_change_continuous := λi j, begin
have A : continuous_on (λp : E × F, Z.coord_change i j (I.symm p.1) p.2)
((I '' (i.1.symm.trans j.1).source).prod (univ : set F)) :=
(Z.coord_change_smooth i j).continuous_on,
have B : continuous_on (λx : M, I (i.1 x)) i.1.source :=
I.continuous.comp_continuous_on i.1.continuous_on,
have C : continuous_on (λp : M × F, (⟨I (i.1 p.1), p.2⟩ : E × F))
(i.1.source.prod univ),
{ apply continuous_on.prod _ continuous_snd.continuous_on,
exact B.comp continuous_fst.continuous_on (prod_subset_preimage_fst _ _) },
have C' : continuous_on (λp : M × F, (⟨I (i.1 p.1), p.2⟩ : E × F))
((i.1.source ∩ j.1.source).prod univ) :=
continuous_on.mono C (prod_mono (inter_subset_left _ _) (subset.refl _)),
have D : (i.1.source ∩ j.1.source).prod univ ⊆ (λ (p : M × F),
(I (i.1 p.1), p.2)) ⁻¹' ((I '' (i.1.symm.trans j.1).source).prod univ),
{ rintros ⟨x, v⟩ hx,
simp only with mfld_simps at hx,
simp only [hx] with mfld_simps },
convert continuous_on.comp A C' D,
ext p,
simp only with mfld_simps
end }
@[simp, mfld_simps] lemma base_set (i : atlas H M) :
Z.to_topological_fiber_bundle_core.base_set i = i.1.source := rfl
/-- Local chart for the total space of a basic smooth bundle -/
def chart {e : local_homeomorph M H} (he : e ∈ atlas H M) :
local_homeomorph (Z.to_topological_fiber_bundle_core.total_space) (model_prod H F) :=
(Z.to_topological_fiber_bundle_core.local_triv ⟨e, he⟩).trans
(local_homeomorph.prod e (local_homeomorph.refl F))
@[simp, mfld_simps] lemma chart_source (e : local_homeomorph M H) (he : e ∈ atlas H M) :
(Z.chart he).source = Z.to_topological_fiber_bundle_core.proj ⁻¹' e.source :=
by { simp only [chart, mem_prod], mfld_set_tac }
@[simp, mfld_simps] lemma chart_target (e : local_homeomorph M H) (he : e ∈ atlas H M) :
(Z.chart he).target = e.target.prod univ :=
by { simp only [chart], mfld_set_tac }
/-- The total space of a basic smooth bundle is endowed with a charted space structure, where the
charts are in bijection with the charts of the basis. -/
instance to_charted_space :
charted_space (model_prod H F) Z.to_topological_fiber_bundle_core.total_space :=
{ atlas := ⋃(e : local_homeomorph M H) (he : e ∈ atlas H M), {Z.chart he},
chart_at := λp, Z.chart (chart_mem_atlas H p.1),
mem_chart_source := λp, by simp [mem_chart_source],
chart_mem_atlas := λp, begin
simp only [mem_Union, mem_singleton_iff, chart_mem_atlas],
exact ⟨chart_at H p.1, chart_mem_atlas H p.1, rfl⟩
end }
lemma mem_atlas_iff
(f : local_homeomorph Z.to_topological_fiber_bundle_core.total_space (model_prod H F)) :
f ∈ atlas (model_prod H F) Z.to_topological_fiber_bundle_core.total_space ↔
∃(e : local_homeomorph M H) (he : e ∈ atlas H M), f = Z.chart he :=
by simp only [atlas, mem_Union, mem_singleton_iff]
@[simp, mfld_simps] lemma mem_chart_source_iff
(p q : Z.to_topological_fiber_bundle_core.total_space) :
p ∈ (chart_at (model_prod H F) q).source ↔ p.1 ∈ (chart_at H q.1).source :=
by simp only [chart_at] with mfld_simps
@[simp, mfld_simps] lemma mem_chart_target_iff
(p : H × F) (q : Z.to_topological_fiber_bundle_core.total_space) :
p ∈ (chart_at (model_prod H F) q).target ↔ p.1 ∈ (chart_at H q.1).target :=
by simp only [chart_at] with mfld_simps
@[simp, mfld_simps] lemma coe_chart_at_fst (p q : Z.to_topological_fiber_bundle_core.total_space) :
((chart_at (model_prod H F) q) p).1 = chart_at H q.1 p.1 := rfl
@[simp, mfld_simps] lemma coe_chart_at_symm_fst
(p : H × F) (q : Z.to_topological_fiber_bundle_core.total_space) :
((chart_at (model_prod H F) q).symm p).1 = ((chart_at H q.1).symm : H → M) p.1 := rfl
/-- Smooth manifold structure on the total space of a basic smooth bundle -/
instance to_smooth_manifold :
smooth_manifold_with_corners (I.prod (𝓘(𝕜, F))) Z.to_topological_fiber_bundle_core.total_space :=
begin
/- We have to check that the charts belong to the smooth groupoid, i.e., they are smooth on their
source, and their inverses are smooth on the target. Since both objects are of the same kind, it
suffices to prove the first statement in A below, and then glue back the pieces at the end. -/
let J := model_with_corners.to_local_equiv (I.prod (𝓘(𝕜, F))),
have A : ∀ (e e' : local_homeomorph M H) (he : e ∈ atlas H M) (he' : e' ∈ atlas H M),
times_cont_diff_on 𝕜 ∞
(J ∘ ((Z.chart he).symm.trans (Z.chart he')) ∘ J.symm)
(J.symm ⁻¹' ((Z.chart he).symm.trans (Z.chart he')).source ∩ range J),
{ assume e e' he he',
have : J.symm ⁻¹' ((chart Z he).symm.trans (chart Z he')).source ∩ range J =
(I.symm ⁻¹' (e.symm.trans e').source ∩ range I).prod univ,
by { simp only [J, chart, model_with_corners.prod], mfld_set_tac },
rw this,
-- check separately that the two components of the coordinate change are smooth
apply times_cont_diff_on.prod,
show times_cont_diff_on 𝕜 ∞ (λ (p : E × F), (I ∘ e' ∘ e.symm ∘ I.symm) p.1)
((I.symm ⁻¹' (e.symm.trans e').source ∩ range I).prod (univ : set F)),
{ -- the coordinate change on the base is just a coordinate change for `M`, smooth since
-- `M` is smooth
have A : times_cont_diff_on 𝕜 ∞ (I ∘ (e.symm.trans e') ∘ I.symm)
(I.symm ⁻¹' (e.symm.trans e').source ∩ range I) :=
(has_groupoid.compatible (times_cont_diff_groupoid ∞ I) he he').1,
have B : times_cont_diff_on 𝕜 ∞ (λp : E × F, p.1)
((I.symm ⁻¹' (e.symm.trans e').source ∩ range I).prod univ) :=
times_cont_diff_fst.times_cont_diff_on,
exact times_cont_diff_on.comp A B (prod_subset_preimage_fst _ _) },
show times_cont_diff_on 𝕜 ∞ (λ (p : E × F),
Z.coord_change ⟨chart_at H (e.symm (I.symm p.1)), _⟩ ⟨e', he'⟩
((chart_at H (e.symm (I.symm p.1)) : M → H) (e.symm (I.symm p.1)))
(Z.coord_change ⟨e, he⟩ ⟨chart_at H (e.symm (I.symm p.1)), _⟩
(e (e.symm (I.symm p.1))) p.2))
((I.symm ⁻¹' (e.symm.trans e').source ∩ range I).prod (univ : set F)),
{ /- The coordinate change in the fiber is more complicated as its definition involves the
reference chart chosen at each point. However, it appears with its inverse, so using the
cocycle property one can get rid of it, and then conclude using the smoothness of the
cocycle as given in the definition of basic smooth bundles. -/
have := Z.coord_change_smooth ⟨e, he⟩ ⟨e', he'⟩,
rw I.image_eq at this,
apply times_cont_diff_on.congr this,
rintros ⟨x, v⟩ hx,
simp only with mfld_simps at hx,
let f := chart_at H (e.symm (I.symm x)),
have A : I.symm x ∈ ((e.symm.trans f).trans (f.symm.trans e')).source,
by simp only [hx.1.1, hx.1.2] with mfld_simps,
rw e.right_inv hx.1.1,
have := Z.coord_change_comp ⟨e, he⟩ ⟨f, chart_mem_atlas _ _⟩ ⟨e', he'⟩ (I.symm x) A v,
simpa only [] using this } },
constructor,
assume e₀ e₀' he₀ he₀',
rcases (Z.mem_atlas_iff _).1 he₀ with ⟨e, he, rfl⟩,
rcases (Z.mem_atlas_iff _).1 he₀' with ⟨e', he', rfl⟩,
rw [times_cont_diff_groupoid, mem_groupoid_of_pregroupoid],
exact ⟨A e e' he he', A e' e he' he⟩
end
end basic_smooth_bundle_core
section tangent_bundle
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_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]
/-- Basic smooth bundle core version of the tangent bundle of a smooth manifold `M` modelled over a
model with corners `I` on `(E, H)`. The fibers are equal to `E`, and the coordinate change in the
fiber corresponds to the derivative of the coordinate change in `M`. -/
def tangent_bundle_core : basic_smooth_bundle_core I M E :=
{ coord_change := λi j x v, (fderiv_within 𝕜 (I ∘ j.1 ∘ i.1.symm ∘ I.symm)
(range I) (I x) : E → E) v,
coord_change_smooth := λi j, begin
/- To check that the coordinate change of the bundle is smooth, one should just use the
smoothness of the charts, and thus the smoothness of their derivatives. -/
rw I.image_eq,
have A : times_cont_diff_on 𝕜 ∞
(I ∘ (i.1.symm.trans j.1) ∘ I.symm)
(I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I) :=
(has_groupoid.compatible (times_cont_diff_groupoid ∞ I) i.2 j.2).1,
have B : unique_diff_on 𝕜 (I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I) :=
I.unique_diff_preimage_source,
have C : times_cont_diff_on 𝕜 ∞
(λ (p : E × E), (fderiv_within 𝕜 (I ∘ j.1 ∘ i.1.symm ∘ I.symm)
(I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I) p.1 : E → E) p.2)
((I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I).prod univ) :=
times_cont_diff_on_fderiv_within_apply A B le_top,
have D : ∀ x ∈ (I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I),
fderiv_within 𝕜 (I ∘ j.1 ∘ i.1.symm ∘ I.symm)
(range I) x =
fderiv_within 𝕜 (I ∘ j.1 ∘ i.1.symm ∘ I.symm)
(I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I) x,
{ assume x hx,
have N : I.symm ⁻¹' (i.1.symm.trans j.1).source ∈ nhds x :=
I.continuous_symm.continuous_at.preimage_mem_nhds
(mem_nhds_sets (local_homeomorph.open_source _) hx.1),
symmetry,
rw inter_comm,
exact fderiv_within_inter N (I.unique_diff _ hx.2) },
apply times_cont_diff_on.congr C,
rintros ⟨x, v⟩ hx,
have E : x ∈ I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I,
by simpa only [prod_mk_mem_set_prod_eq, and_true, mem_univ] using hx,
have : I (I.symm x) = x, by simp [E.2],
dsimp [-subtype.val_eq_coe],
rw [this, D x E],
refl
end,
coord_change_self := λi x hx v, begin
/- Locally, a self-change of coordinate is just the identity, thus its derivative is the
identity. One just needs to write this carefully, paying attention to the sets where the
functions are defined. -/
have A : I.symm ⁻¹' (i.1.symm.trans i.1).source ∩ range I ∈
𝓝[range I] (I x),
{ rw inter_comm,
apply inter_mem_nhds_within,
apply I.continuous_symm.continuous_at.preimage_mem_nhds
(mem_nhds_sets (local_homeomorph.open_source _) _),
simp only [hx, i.1.map_target] with mfld_simps },
have B : ∀ᶠ y in 𝓝[range I] (I x),
(I ∘ i.1 ∘ i.1.symm ∘ I.symm) y = (id : E → E) y,
{ filter_upwards [A],
assume y hy,
rw ← I.image_eq at hy,
rcases hy with ⟨z, hz⟩,
simp only with mfld_simps at hz,
simp only [hz.2.symm, hz.1] with mfld_simps },
have C : fderiv_within 𝕜 (I ∘ i.1 ∘ i.1.symm ∘ I.symm) (range I) (I x) =
fderiv_within 𝕜 (id : E → E) (range I) (I x) :=
filter.eventually_eq.fderiv_within_eq I.unique_diff_at_image B
(by simp only [hx] with mfld_simps),
rw fderiv_within_id I.unique_diff_at_image at C,
rw C,
refl
end,
coord_change_comp := λi j u x hx, begin
/- The cocycle property is just the fact that the derivative of a composition is the product of
the derivatives. One needs however to check that all the functions one considers are smooth, and
to pay attention to the domains where these functions are defined, making this proof a little
bit cumbersome although there is nothing complicated here. -/
have M : I x ∈
(I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I) :=
⟨by simpa only [mem_preimage, model_with_corners.left_inv] using hx, mem_range_self _⟩,
have U : unique_diff_within_at 𝕜
(I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I) (I x) :=
I.unique_diff_preimage_source _ M,
have A : fderiv_within 𝕜 ((I ∘ u.1 ∘ j.1.symm ∘ I.symm) ∘ (I ∘ j.1 ∘ i.1.symm ∘ I.symm))
(I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I)
(I x)
= (fderiv_within 𝕜 (I ∘ u.1 ∘ j.1.symm ∘ I.symm)
(I.symm ⁻¹' (j.1.symm.trans u.1).source ∩ range I)
((I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I x))).comp
(fderiv_within 𝕜 (I ∘ j.1 ∘ i.1.symm ∘ I.symm)
(I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I)
(I x)),
{ apply fderiv_within.comp _ _ _ _ U,
show differentiable_within_at 𝕜 (I ∘ j.1 ∘ i.1.symm ∘ I.symm)
(I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I)
(I x),
{ have A : times_cont_diff_on 𝕜 ∞
(I ∘ (i.1.symm.trans j.1) ∘ I.symm)
(I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I) :=
(has_groupoid.compatible (times_cont_diff_groupoid ∞ I) i.2 j.2).1,
have B : differentiable_on 𝕜 (I ∘ j.1 ∘ i.1.symm ∘ I.symm)
(I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I),
{ apply (A.differentiable_on le_top).mono,
have : ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ⊆
(i.1.symm.trans j.1).source := inter_subset_left _ _,
exact inter_subset_inter (preimage_mono this) (subset.refl (range I)) },
apply B,
simpa only [] with mfld_simps using hx },
show differentiable_within_at 𝕜 (I ∘ u.1 ∘ j.1.symm ∘ I.symm)
(I.symm ⁻¹' (j.1.symm.trans u.1).source ∩ range I)
((I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I x)),
{ have A : times_cont_diff_on 𝕜 ∞
(I ∘ (j.1.symm.trans u.1) ∘ I.symm)
(I.symm ⁻¹' (j.1.symm.trans u.1).source ∩ range I) :=
(has_groupoid.compatible (times_cont_diff_groupoid ∞ I) j.2 u.2).1,
apply A.differentiable_on le_top,
rw [local_homeomorph.trans_source] at hx,
simp only with mfld_simps,
exact hx.2 },
show (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I)
⊆ (I ∘ j.1 ∘ i.1.symm ∘ I.symm) ⁻¹' (I.symm ⁻¹' (j.1.symm.trans u.1).source ∩ range I),
{ assume y hy,
simp only with mfld_simps at hy,
rw [local_homeomorph.left_inv] at hy,
{ simp only [hy] with mfld_simps },
{ exact hy.1.1.2 } } },
have B : fderiv_within 𝕜 ((I ∘ u.1 ∘ j.1.symm ∘ I.symm)
∘ (I ∘ j.1 ∘ i.1.symm ∘ I.symm))
(I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I)
(I x)
= fderiv_within 𝕜 (I ∘ u.1 ∘ i.1.symm ∘ I.symm)
(I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I)
(I x),
{ have E :
∀ y ∈ (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I),
((I ∘ u.1 ∘ j.1.symm ∘ I.symm) ∘ (I ∘ j.1 ∘ i.1.symm ∘ I.symm)) y =
(I ∘ u.1 ∘ i.1.symm ∘ I.symm) y,
{ assume y hy,
simp only [function.comp_app, model_with_corners.left_inv],
rw [j.1.left_inv],
exact hy.1.1.2 },
exact fderiv_within_congr U E (E _ M) },
have C : fderiv_within 𝕜 (I ∘ u.1 ∘ i.1.symm ∘ I.symm)
(I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I)
(I x) =
fderiv_within 𝕜 (I ∘ u.1 ∘ i.1.symm ∘ I.symm)
(range I) (I x),
{ rw inter_comm,
apply fderiv_within_inter _ I.unique_diff_at_image,
apply I.continuous_symm.continuous_at.preimage_mem_nhds
(mem_nhds_sets (local_homeomorph.open_source _) _),
simpa only [model_with_corners.left_inv] using hx },
have D : fderiv_within 𝕜 (I ∘ u.1 ∘ j.1.symm ∘ I.symm)
(I.symm ⁻¹' (j.1.symm.trans u.1).source ∩ range I) ((I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I x)) =
fderiv_within 𝕜 (I ∘ u.1 ∘ j.1.symm ∘ I.symm) (range I) ((I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I x)),
{ rw inter_comm,
apply fderiv_within_inter _ I.unique_diff_at_image,
apply I.continuous_symm.continuous_at.preimage_mem_nhds
(mem_nhds_sets (local_homeomorph.open_source _) _),
rw [local_homeomorph.trans_source] at hx,
simp only with mfld_simps,
exact hx.2 },
have E : fderiv_within 𝕜 (I ∘ j.1 ∘ i.1.symm ∘ I.symm)
(I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I)
(I x) =
fderiv_within 𝕜 (I ∘ j.1 ∘ i.1.symm ∘ I.symm) (range I) (I x),
{ rw inter_comm,
apply fderiv_within_inter _ I.unique_diff_at_image,
apply I.continuous_symm.continuous_at.preimage_mem_nhds
(mem_nhds_sets (local_homeomorph.open_source _) _),
simpa only [model_with_corners.left_inv] using hx },
rw [B, C, D, E] at A,
simp only [A, continuous_linear_map.coe_comp'] with mfld_simps
end }
variable {M}
include I
/-- The tangent space at a point of the manifold `M`. It is just `E`. We could use instead
`(tangent_bundle_core I M).to_topological_fiber_bundle_core.fiber x`, but we use `E` to help the
kernel.
-/
@[nolint unused_arguments]
def tangent_space (x : M) : Type* := E
omit I
variable (M)
/-- The tangent bundle to a smooth manifold, as a plain type. We could use
`(tangent_bundle_core I M).to_topological_fiber_bundle_core.total_space`, but instead we use the
(definitionally equal) `Σ (x : M), tangent_space I x`, to make sure that rcasing an element of the
tangent bundle gives a second component in the tangent space. -/
@[nolint has_inhabited_instance, reducible] -- is empty if the base manifold is empty
def tangent_bundle := Σ (x : M), tangent_space I x
/-- The projection from the tangent bundle of a smooth manifold to the manifold. As the tangent
bundle is represented internally as a product type, the notation `p.1` also works for the projection
of the point `p`. -/
def tangent_bundle.proj : tangent_bundle I M → M :=
λ p, p.1
variable {M}
@[simp, mfld_simps] lemma tangent_bundle.proj_apply (x : M) (v : tangent_space I x) :
tangent_bundle.proj I M ⟨x, v⟩ = x :=
rfl
section tangent_bundle_instances
/- In general, the definition of tangent_bundle and tangent_space are not reducible, so that type
class inference does not pick wrong instances. In this section, we record the right instances for
them, noting in particular that the tangent bundle is a smooth manifold. -/
variable (M)
instance : topological_space (tangent_bundle I M) :=
(tangent_bundle_core I M).to_topological_fiber_bundle_core.to_topological_space
instance : charted_space (model_prod H E) (tangent_bundle I M) :=
(tangent_bundle_core I M).to_charted_space
instance : smooth_manifold_with_corners I.tangent (tangent_bundle I M) :=
(tangent_bundle_core I M).to_smooth_manifold
local attribute [reducible] tangent_space
variables {M} (x : M)
instance : has_continuous_smul 𝕜 (tangent_space I x) := by apply_instance
instance : topological_space (tangent_space I x) := by apply_instance
instance : add_comm_group (tangent_space I x) := by apply_instance
instance : topological_add_group (tangent_space I x) := by apply_instance
instance : vector_space 𝕜 (tangent_space I x) := by apply_instance
instance : inhabited (tangent_space I x) := ⟨0⟩
end tangent_bundle_instances
variable (M)
/-- The tangent bundle projection on the basis is a continuous map. -/
lemma tangent_bundle_proj_continuous : continuous (tangent_bundle.proj I M) :=
topological_fiber_bundle_core.continuous_proj _
/-- The tangent bundle projection on the basis is an open map. -/
lemma tangent_bundle_proj_open : is_open_map (tangent_bundle.proj I M) :=
topological_fiber_bundle_core.is_open_map_proj _
/-- In the tangent bundle to the model space, the charts are just the canonical identification
between a product type and a sigma type, a.k.a. `equiv.sigma_equiv_prod`. -/
@[simp, mfld_simps] lemma tangent_bundle_model_space_chart_at (p : tangent_bundle I H) :
(chart_at (model_prod H E) p).to_local_equiv = (equiv.sigma_equiv_prod H E).to_local_equiv :=
begin
have A : ∀ x_fst, fderiv_within 𝕜 (I ∘ I.symm) (range I) (I x_fst) = continuous_linear_map.id 𝕜 E,
{ assume x_fst,
have : fderiv_within 𝕜 (I ∘ I.symm) (range I) (I x_fst)
= fderiv_within 𝕜 id (range I) (I x_fst),
{ refine fderiv_within_congr I.unique_diff_at_image (λy hy, _) (by simp),
exact model_with_corners.right_inv _ hy },
rwa fderiv_within_id I.unique_diff_at_image at this },
ext x : 1,
show (chart_at (model_prod H E) p : tangent_bundle I H → model_prod H E) x =
(equiv.sigma_equiv_prod H E) x,
{ cases x,
simp only [chart_at, basic_smooth_bundle_core.chart, tangent_bundle_core,
basic_smooth_bundle_core.to_topological_fiber_bundle_core, A, prod.mk.inj_iff,
continuous_linear_map.coe_id'] with mfld_simps, },
show ∀ x, ((chart_at (model_prod H E) p).to_local_equiv).symm x =
(equiv.sigma_equiv_prod H E).symm x,
{ rintros ⟨x_fst, x_snd⟩,
simp only [chart_at, basic_smooth_bundle_core.chart, tangent_bundle_core,
continuous_linear_map.coe_id', basic_smooth_bundle_core.to_topological_fiber_bundle_core, A]
with mfld_simps},
show ((chart_at (model_prod H E) p).to_local_equiv).source = univ,
by simp only [chart_at] with mfld_simps,
end
@[simp, mfld_simps] lemma tangent_bundle_model_space_coe_chart_at (p : tangent_bundle I H) :
⇑(chart_at (model_prod H E) p) = equiv.sigma_equiv_prod H E :=
by { unfold_coes, simp only with mfld_simps }
@[simp, mfld_simps] lemma tangent_bundle_model_space_coe_chart_at_symm (p : tangent_bundle I H) :
((chart_at (model_prod H E) p).symm : model_prod H E → tangent_bundle I H) =
(equiv.sigma_equiv_prod H E).symm :=
by { unfold_coes, simp only with mfld_simps }
variable (H)
/-- The canonical identification between the tangent bundle to the model space and the product,
as a homeomorphism -/
def tangent_bundle_model_space_homeomorph : tangent_bundle I H ≃ₜ model_prod H E :=
{ continuous_to_fun :=
begin
let p : tangent_bundle I H := ⟨I.symm (0 : E), (0 : E)⟩,
have : continuous (chart_at (model_prod H E) p),
{ rw continuous_iff_continuous_on_univ,
convert local_homeomorph.continuous_on _,
simp only with mfld_simps },
simpa only with mfld_simps using this,
end,
continuous_inv_fun :=
begin
let p : tangent_bundle I H := ⟨I.symm (0 : E), (0 : E)⟩,
have : continuous (chart_at (model_prod H E) p).symm,
{ rw continuous_iff_continuous_on_univ,
convert local_homeomorph.continuous_on _,
simp only with mfld_simps },
simpa only with mfld_simps using this,
end,
.. equiv.sigma_equiv_prod H E }
@[simp, mfld_simps] lemma tangent_bundle_model_space_homeomorph_coe :
(tangent_bundle_model_space_homeomorph H I : tangent_bundle I H → model_prod H E)
= equiv.sigma_equiv_prod H E :=
rfl
@[simp, mfld_simps] lemma tangent_bundle_model_space_homeomorph_coe_symm :
((tangent_bundle_model_space_homeomorph H I).symm : model_prod H E → tangent_bundle I H)
= (equiv.sigma_equiv_prod H E).symm :=
rfl
end tangent_bundle
|
a89fe6f7a01d4d7e60b9fd88d3d886e2b60376b0 | 3f7026ea8bef0825ca0339a275c03b911baef64d | /src/category_theory/functor_category.lean | 7f531f56533e791b62dc904a8b165f5282118952 | [
"Apache-2.0"
] | permissive | rspencer01/mathlib | b1e3afa5c121362ef0881012cc116513ab09f18c | c7d36292c6b9234dc40143c16288932ae38fdc12 | refs/heads/master | 1,595,010,346,708 | 1,567,511,503,000 | 1,567,511,503,000 | 206,071,681 | 0 | 0 | Apache-2.0 | 1,567,513,643,000 | 1,567,513,643,000 | null | UTF-8 | Lean | false | false | 3,935 | 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.natural_transformation
namespace category_theory
universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation
open nat_trans category category_theory.functor
variables (C : Type u₁) [𝒞 : category.{v₁} C] (D : Type u₂) [𝒟 : category.{v₂} D]
include 𝒞 𝒟
/--
`functor.category C D` gives the category structure on functors and natural transformations
between categories `C` and `D`.
Notice that if `C` and `D` are both small categories at the same universe level,
this is another small category at that level.
However if `C` and `D` are both large categories at the same universe level,
this is a small category at the next higher level.
-/
instance functor.category : category.{(max (u₁+1) v₂)} (C ⥤ D) :=
{ hom := λ F G, nat_trans F G,
id := λ F, nat_trans.id F,
comp := λ _ _ _ α β, vcomp α β }
variables {C D} {E : Type u₃} [ℰ : category.{v₃} E]
variables {F G H I : C ⥤ D}
namespace nat_trans
@[simp] lemma vcomp_eq_comp (α : F ⟶ G) (β : G ⟶ H) : vcomp α β = α ≫ β := rfl
lemma congr_app {α β : F ⟶ G} (h : α = β) (X : C) : α.app X = β.app X := by rw h
@[simp] lemma id_app (F : C ⥤ D) (X : C) : (𝟙 F : F ⟶ F).app X = 𝟙 (F.obj X) := rfl
@[simp] lemma comp_app {F G H : C ⥤ D} (α : F ⟶ G) (β : G ⟶ H) (X : C) :
(α ≫ β).app X = α.app X ≫ β.app X := rfl
include ℰ
lemma app_naturality {F G : C ⥤ (D ⥤ E)} (T : F ⟶ G) (X : C) {Y Z : D} (f : Y ⟶ Z) :
((F.obj X).map f) ≫ ((T.app X).app Z) = ((T.app X).app Y) ≫ ((G.obj X).map f) :=
(T.app X).naturality f
lemma naturality_app {F G : C ⥤ (D ⥤ E)} (T : F ⟶ G) (Z : D) {X Y : C} (f : X ⟶ Y) :
((F.map f).app Z) ≫ ((T.app Y).app Z) = ((T.app X).app Z) ≫ ((G.map f).app Z) :=
congr_fun (congr_arg app (T.naturality f)) Z
/-- `hcomp α β` is the horizontal composition of natural transformations. -/
def hcomp {H I : D ⥤ E} (α : F ⟶ G) (β : H ⟶ I) : (F ⋙ H) ⟶ (G ⋙ I) :=
{ app := λ X : C, (β.app (F.obj X)) ≫ (I.map (α.app X)),
naturality' := begin
intros, rw [functor.comp_map, functor.comp_map, assoc_symm, naturality, assoc],
rw [← map_comp I, naturality, map_comp, assoc]
end }
infix ` ◫ `:80 := hcomp
@[simp] lemma hcomp_app {H I : D ⥤ E} (α : F ⟶ G) (β : H ⟶ I) (X : C) :
(α ◫ β).app X = (β.app (F.obj X)) ≫ (I.map (α.app X)) := rfl
-- Note that we don't yet prove a `hcomp_assoc` lemma here: even stating it is painful, because we
-- need to use associativity of functor composition
lemma exchange {I J K : D ⥤ E} (α : F ⟶ G) (β : G ⟶ H)
(γ : I ⟶ J) (δ : J ⟶ K) : (α ≫ β) ◫ (γ ≫ δ) = (α ◫ γ) ≫ (β ◫ δ) :=
by { ext, dsimp, rw [assoc, assoc, map_comp, assoc_symm (δ.app _), ← naturality, assoc] }
end nat_trans
open nat_trans
namespace functor
include ℰ
protected def flip (F : C ⥤ (D ⥤ E)) : D ⥤ (C ⥤ E) :=
{ obj := λ k,
{ obj := λ j, (F.obj j).obj k,
map := λ j j' f, (F.map f).app k,
map_id' := λ X, begin rw category_theory.functor.map_id, refl end,
map_comp' := λ X Y Z f g, by rw [map_comp, ←comp_app] },
map := λ c c' f,
{ app := λ j, (F.obj j).map f } }.
@[simp] lemma flip_obj_obj (F : C ⥤ (D ⥤ E)) (c) (d) : (F.flip.obj d).obj c = (F.obj c).obj d := rfl
@[simp] lemma flip_obj_map (F : C ⥤ (D ⥤ E)) {c c' : C} (f : c ⟶ c') (d : D) :
(F.flip.obj d).map f = (F.map f).app d := rfl
@[simp] lemma flip_map_app (F : C ⥤ (D ⥤ E)) {d d' : D} (f : d ⟶ d') (c : C) :
(F.flip.map f).app c = (F.obj c).map f := rfl
end functor
end category_theory
|
3fb11f2bc28149b2f9113834a36bdd02e0531696 | fcf3ffa92a3847189ca669cb18b34ef6b2ec2859 | /src/world2/level6.lean | e836fb047282cda527a21e768f1c9b314e2505f5 | [
"Apache-2.0"
] | permissive | nomoid/lean-proofs | 4a80a97888699dee42b092b7b959b22d9aa0c066 | b9f03a24623d1a1d111d6c2bbf53c617e2596d6a | refs/heads/master | 1,674,955,317,080 | 1,607,475,706,000 | 1,607,475,706,000 | 314,104,281 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 265 | lean | import mynat.definition
import mynat.add
import world2.level2
import world2.level4
namespace mynat
lemma add_right_comm (a b c: mynat) : a + b + c = a + c + b :=
begin [nat_num_game]
rw add_assoc,
rw add_comm b,
rw add_assoc,
refl,
end
end mynat
|
847865f7b97f145e38b508d600a349116f5c2b83 | a8ca99e8fae20e1383a6a786312b9f44f7dfdb42 | /coq/2.2 types as objects.lean | 537be997183f49792aa4744a65688c9dcb299420 | [
"MIT"
] | permissive | nolmeunion/learn | b715a4e9560101275c9c418d191cc198c905d189 | 482f4b8a5de81375e5663ea0b065d25f2f7da4b6 | refs/heads/master | 1,671,751,332,943 | 1,601,261,285,000 | 1,601,261,285,000 | 287,418,011 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 474 | lean | #check nat
#check bool
#check nat → bool
#check nat × nat → nat
#check nat → (nat → nat)
#check (nat → nat) → nat
constants α β : Type
constant F : Type → Type
#check F
#check prod α β
#check list α
#check list nat
#check Type
-- Type is a set
-- Type == Type 0
-- type of Type 0 is Type 1
-- type of Type 1 is Type 2
#check Type 1
#check Type 2
#check list
#check prod
-- u_1 u_2는 0, 1, 2, 3, ...의 값을 갖는다.
|
48099e12c7c9a68b3f4c33bb4bab7083fe4fc0b1 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/category_theory/limits/preserves/shapes/kernels.lean | 3c9751734eae023fa6a7f72198e61f83512551c7 | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 7,565 | lean | /-
Copyright (c) 2022 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.limits.shapes.kernels
import category_theory.limits.preserves.shapes.equalizers
import category_theory.limits.preserves.shapes.zero
/-!
# Preserving (co)kernels
Constructions to relate the notions of preserving (co)kernels and reflecting (co)kernels
to concrete (co)forks.
In particular, we show that `kernel_comparison f g G` is an isomorphism iff `G` preserves
the limit of the parallel pair `f,0`, as well as the dual result.
-/
noncomputable theory
universes v₁ v₂ u₁ u₂
open category_theory category_theory.category category_theory.limits
variables {C : Type u₁} [category.{v₁} C] [has_zero_morphisms C]
variables {D : Type u₂} [category.{v₂} D] [has_zero_morphisms D]
variables (G : C ⥤ D) [functor.preserves_zero_morphisms G]
namespace category_theory.limits
section kernels
variables {X Y Z : C} {f : X ⟶ Y} {h : Z ⟶ X} (w : h ≫ f = 0)
/--
The map of a kernel fork is a limit iff
the kernel fork consisting of the mapped morphisms is a limit.
This essentially lets us commute `kernel_fork.of_ι` with `functor.map_cone`.
This is a variant of `is_limit_map_cone_fork_equiv` for equalizers,
which we can't use directly between `G.map 0 = 0` does not hold definitionally.
-/
def is_limit_map_cone_fork_equiv' :
is_limit (G.map_cone (kernel_fork.of_ι h w)) ≃
is_limit (kernel_fork.of_ι (G.map h) (by simp only [←G.map_comp, w, functor.map_zero])
: fork (G.map f) 0) :=
begin
refine (is_limit.postcompose_hom_equiv _ _).symm.trans (is_limit.equiv_iso_limit _),
refine parallel_pair.ext (iso.refl _) (iso.refl _) _ _; simp,
refine fork.ext (iso.refl _) _,
simp [fork.ι]
end
/--
The property of preserving kernels expressed in terms of kernel forks.
This is a variant of `is_limit_fork_map_of_is_limit` for equalizers,
which we can't use directly between `G.map 0 = 0` does not hold definitionally.
-/
def is_limit_fork_map_of_is_limit' [preserves_limit (parallel_pair f 0) G]
(l : is_limit (kernel_fork.of_ι h w)) :
is_limit (kernel_fork.of_ι (G.map h) (by simp only [←G.map_comp, w, functor.map_zero]) :
fork (G.map f) 0) :=
is_limit_map_cone_fork_equiv' G w (preserves_limit.preserves l)
variables (f) [has_kernel f]
/--
If `G` preserves kernels and `C` has them, then the fork constructed of the mapped morphisms of
a kernel fork is a limit.
-/
def is_limit_of_has_kernel_of_preserves_limit [preserves_limit (parallel_pair f 0) G] :
is_limit (fork.of_ι (G.map (kernel.ι f))
(by simp only [←G.map_comp, equalizer.condition, comp_zero, functor.map_zero])
: fork (G.map f) 0) :=
is_limit_fork_map_of_is_limit' G (kernel.condition f) (kernel_is_kernel f)
instance [preserves_limit (parallel_pair f 0) G] : has_kernel (G.map f) :=
{ exists_limit := ⟨⟨_, is_limit_of_has_kernel_of_preserves_limit G f⟩⟩, }
variables [has_kernel (G.map f)]
/--
If the kernel comparison map for `G` at `f` is an isomorphism, then `G` preserves the
kernel of `f`.
-/
def preserves_kernel.of_iso_comparison [i : is_iso (kernel_comparison f G)] :
preserves_limit (parallel_pair f 0) G :=
begin
apply preserves_limit_of_preserves_limit_cone (kernel_is_kernel f),
apply (is_limit_map_cone_fork_equiv' G (kernel.condition f)).symm _,
apply is_limit.of_point_iso (limit.is_limit (parallel_pair (G.map f) 0)),
apply i,
end
variables [preserves_limit (parallel_pair f 0) G]
/--
If `G` preserves the kernel of `f`, then the kernel comparison map for `G` at `f` is
an isomorphism.
-/
def preserves_kernel.iso :
G.obj (kernel f) ≅ kernel (G.map f) :=
is_limit.cone_point_unique_up_to_iso
(is_limit_of_has_kernel_of_preserves_limit G f)
(limit.is_limit _)
@[simp]
lemma preserves_kernel.iso_hom :
(preserves_kernel.iso G f).hom = kernel_comparison f G :=
rfl
instance : is_iso (kernel_comparison f G) :=
begin
rw ← preserves_kernel.iso_hom,
apply_instance
end
end kernels
section cokernels
variables {X Y Z : C} {f : X ⟶ Y} {h : Y ⟶ Z} (w : f ≫ h = 0)
/--
The map of a cokernel cofork is a colimit iff
the cokernel cofork consisting of the mapped morphisms is a colimit.
This essentially lets us commute `cokernel_cofork.of_π` with `functor.map_cocone`.
This is a variant of `is_colimit_map_cocone_cofork_equiv` for equalizers,
which we can't use directly between `G.map 0 = 0` does not hold definitionally.
-/
def is_colimit_map_cocone_cofork_equiv' :
is_colimit (G.map_cocone (cokernel_cofork.of_π h w)) ≃
is_colimit (cokernel_cofork.of_π (G.map h) (by simp only [←G.map_comp, w, functor.map_zero])
: cofork (G.map f) 0) :=
begin
refine (is_colimit.precompose_hom_equiv _ _).symm.trans (is_colimit.equiv_iso_colimit _),
refine parallel_pair.ext (iso.refl _) (iso.refl _) _ _; simp,
refine cofork.ext (iso.refl _) _,
simp only [cofork.π, iso.refl_hom, id_comp, cocones.precompose_obj_ι,
nat_trans.comp_app, parallel_pair.ext_hom_app, functor.map_cocone_ι_app,
cofork.of_π_ι_app],
apply category.comp_id
end
/--
The property of preserving cokernels expressed in terms of cokernel coforks.
This is a variant of `is_colimit_cofork_map_of_is_colimit` for equalizers,
which we can't use directly between `G.map 0 = 0` does not hold definitionally.
-/
def is_colimit_cofork_map_of_is_colimit' [preserves_colimit (parallel_pair f 0) G]
(l : is_colimit (cokernel_cofork.of_π h w)) :
is_colimit (cokernel_cofork.of_π (G.map h) (by simp only [←G.map_comp, w, functor.map_zero]) :
cofork (G.map f) 0) :=
is_colimit_map_cocone_cofork_equiv' G w (preserves_colimit.preserves l)
variables (f) [has_cokernel f]
/--
If `G` preserves cokernels and `C` has them, then the cofork constructed of the mapped morphisms of
a cokernel cofork is a colimit.
-/
def is_colimit_of_has_cokernel_of_preserves_colimit [preserves_colimit (parallel_pair f 0) G] :
is_colimit (cofork.of_π (G.map (cokernel.π f))
(by simp only [←G.map_comp, coequalizer.condition, zero_comp, functor.map_zero])
: cofork (G.map f) 0) :=
is_colimit_cofork_map_of_is_colimit' G (cokernel.condition f) (cokernel_is_cokernel f)
instance [preserves_colimit (parallel_pair f 0) G] : has_cokernel (G.map f) :=
{ exists_colimit := ⟨⟨_, is_colimit_of_has_cokernel_of_preserves_colimit G f⟩⟩, }
variables [has_cokernel (G.map f)]
/--
If the cokernel comparison map for `G` at `f` is an isomorphism, then `G` preserves the
cokernel of `f`.
-/
def preserves_cokernel.of_iso_comparison [i : is_iso (cokernel_comparison f G)] :
preserves_colimit (parallel_pair f 0) G :=
begin
apply preserves_colimit_of_preserves_colimit_cocone (cokernel_is_cokernel f),
apply (is_colimit_map_cocone_cofork_equiv' G (cokernel.condition f)).symm _,
apply is_colimit.of_point_iso (colimit.is_colimit (parallel_pair (G.map f) 0)),
apply i,
end
variables [preserves_colimit (parallel_pair f 0) G]
/--
If `G` preserves the cokernel of `f`, then the cokernel comparison map for `G` at `f` is
an isomorphism.
-/
def preserves_cokernel.iso :
G.obj (cokernel f) ≅ cokernel (G.map f) :=
is_colimit.cocone_point_unique_up_to_iso
(is_colimit_of_has_cokernel_of_preserves_colimit G f)
(colimit.is_colimit _)
@[simp]
lemma preserves_cokernel.iso_inv :
(preserves_cokernel.iso G f).inv = cokernel_comparison f G :=
rfl
instance : is_iso (cokernel_comparison f G) :=
begin
rw ← preserves_cokernel.iso_inv,
apply_instance
end
end cokernels
end category_theory.limits
|
4f25eadce85c33f8910e3207bd82087ca8963764 | 0c9c1ff8e5013c525bf1d72338b62db639374733 | /library/init/core.lean | c510ee535b339d33019fedf006fe5a7418dacfb4 | [
"Apache-2.0"
] | permissive | semorrison/lean | 1f2bb450c3400098666ff6e43aa29b8e1e3cdc3a | 85dcb385d5219f2fca8c73b2ebca270fe81337e0 | refs/heads/master | 1,638,526,143,586 | 1,634,825,588,000 | 1,634,825,588,000 | 258,650,844 | 0 | 0 | Apache-2.0 | 1,587,772,955,000 | 1,587,772,954,000 | null | UTF-8 | Lean | false | false | 18,592 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
notation, basic datatypes and type classes
-/
prelude
notation `Prop` := Sort 0
notation f ` $ `:1 a:0 := f a
universes u v w
/--
The kernel definitional equality test (t =?= s) has special support for id_delta applications.
It implements the following rules
1) (id_delta t) =?= t
2) t =?= (id_delta t)
3) (id_delta t) =?= s IF (unfold_of t) =?= s
4) t =?= id_delta s IF t =?= (unfold_of s)
This is mechanism for controlling the delta reduction (aka unfolding) used in the kernel.
We use id_delta applications to address performance problems when type checking
lemmas generated by the equation compiler.
-/
@[inline] def id_delta {α : Sort u} (a : α) : α :=
a
/-- Gadget for optional parameter support. -/
@[reducible] def opt_param (α : Sort u) (default : α) : Sort u :=
α
/-- Gadget for marking output parameters in type classes. -/
@[reducible] def out_param (α : Sort u) : Sort u := α
/-
id_rhs is an auxiliary declaration used in the equation compiler to address performance
issues when proving equational lemmas. The equation compiler uses it as a marker.
-/
abbreviation id_rhs (α : Sort u) (a : α) : α := a
inductive punit : Sort u
| star : punit
/-- An abbreviation for `punit.{0}`, its most common instantiation.
This type should be preferred over `punit` where possible to avoid
unnecessary universe parameters. -/
abbreviation unit : Type := punit
@[pattern] abbreviation unit.star : unit := punit.star
/--
Gadget for defining thunks, thunk parameters have special treatment.
Example: given
def f (s : string) (t : thunk nat) : nat
an application
f "hello" 10
is converted into
f "hello" (λ _, 10)
-/
@[reducible] def thunk (α : Type u) : Type u :=
unit → α
inductive true : Prop
| intro : true
inductive false : Prop
inductive empty : Type
/--
Logical not.
`not P`, with notation `¬ P`, is the `Prop` which is true if and only if `P` is false. It is
internally represented as `P → false`, so one way to prove a goal `⊢ ¬ P` is to use `intro h`,
which gives you a new hypothesis `h : P` and the goal `⊢ false`.
A hypothesis `h : ¬ P` can be used in term mode as a function, so if `w : P` then `h w : false`.
Related mathlib tactic: `contrapose`.
-/
def not (a : Prop) := a → false
prefix `¬`:40 := not
inductive eq {α : Sort u} (a : α) : α → Prop
| refl [] : eq a
/-
Initialize the quotient module, which effectively adds the following definitions:
constant quot {α : Sort u} (r : α → α → Prop) : Sort u
constant quot.mk {α : Sort u} (r : α → α → Prop) (a : α) : quot r
constant quot.lift {α : Sort u} {r : α → α → Prop} {β : Sort v} (f : α → β) :
(∀ a b : α, r a b → eq (f a) (f b)) → quot r → β
constant quot.ind {α : Sort u} {r : α → α → Prop} {β : quot r → Prop} :
(∀ a : α, β (quot.mk r a)) → ∀ q : quot r, β q
Also the reduction rule:
quot.lift f _ (quot.mk a) ~~> f a
-/
init_quotient
/--
Heterogeneous equality.
Its purpose is to write down equalities between terms whose types are not definitionally equal.
For example, given `x : vector α n` and `y : vector α (0+n)`, `x = y` doesn't typecheck but `x == y` does.
If you have a goal `⊢ x == y`,
your first instinct should be to ask (either yourself, or on [zulip](https://leanprover.zulipchat.com/))
if something has gone wrong already.
If you really do need to follow this route,
you may find the lemmas `eq_rec_heq` and `eq_mpr_heq` useful.
-/
inductive heq {α : Sort u} (a : α) : Π {β : Sort u}, β → Prop
| refl [] : heq a
structure prod (α : Type u) (β : Type v) :=
(fst : α) (snd : β)
/-- Similar to `prod`, but α and β can be propositions.
We use this type internally to automatically generate the brec_on recursor. -/
structure pprod (α : Sort u) (β : Sort v) :=
(fst : α) (snd : β)
/--
Logical and.
`and P Q`, with notation `P ∧ Q`, is the `Prop` which is true precisely when `P` and `Q` are
both true.
To prove a goal `⊢ P ∧ Q`, you can use the tactic `split`,
which gives two separate goals `⊢ P` and `⊢ Q`.
Given a hypothesis `h : P ∧ Q`, you can use the tactic `cases h with hP hQ`
to obtain two new hypotheses `hP : P` and `hQ : Q`. See also the `obtain` or `rcases` tactics in
mathlib.
-/
structure and (a b : Prop) : Prop :=
intro :: (left : a) (right : b)
lemma and.elim_left {a b : Prop} (h : and a b) : a := h.1
lemma and.elim_right {a b : Prop} (h : and a b) : b := h.2
/- eq basic support -/
infix ` = `:50 := eq
attribute [refl] eq.refl
/- This is a `def`, so that it can be used as pattern in the equation compiler. -/
@[pattern] def rfl {α : Sort u} {a : α} : a = a := eq.refl a
@[elab_as_eliminator, subst]
lemma eq.subst {α : Sort u} {P : α → Prop} {a b : α} (h₁ : a = b) (h₂ : P a) : P b :=
eq.rec h₂ h₁
infixr ` ▸ `:75 := eq.subst
@[trans] lemma eq.trans {α : Sort u} {a b c : α} (h₁ : a = b) (h₂ : b = c) : a = c :=
h₂ ▸ h₁
@[symm] lemma eq.symm {α : Sort u} {a b : α} (h : a = b) : b = a :=
h ▸ rfl
infix ` == `:50 := heq
/- This is a `def`, so that it can be used as pattern in the equation compiler. -/
@[pattern] def heq.rfl {α : Sort u} {a : α} : a == a := heq.refl a
lemma eq_of_heq {α : Sort u} {a a' : α} (h : a == a') : a = a' :=
have ∀ (α' : Sort u) (a' : α') (h₁ : @heq α a α' a') (h₂ : α = α'), (eq.rec_on h₂ a : α') = a', from
λ (α' : Sort u) (a' : α') (h₁ : @heq α a α' a'), heq.rec_on h₁ (λ h₂ : α = α, rfl),
show (eq.rec_on (eq.refl α) a : α) = a', from
this α a' h (eq.refl α)
/- The following four lemmas could not be automatically generated when the
structures were declared, so we prove them manually here. -/
lemma prod.mk.inj {α : Type u} {β : Type v} {x₁ : α} {y₁ : β} {x₂ : α} {y₂ : β}
: (x₁, y₁) = (x₂, y₂) → and (x₁ = x₂) (y₁ = y₂) :=
λ h, prod.no_confusion h (λ h₁ h₂, ⟨h₁, h₂⟩)
lemma prod.mk.inj_arrow {α : Type u} {β : Type v} {x₁ : α} {y₁ : β} {x₂ : α} {y₂ : β}
: (x₁, y₁) = (x₂, y₂) → Π ⦃P : Sort w⦄, (x₁ = x₂ → y₁ = y₂ → P) → P :=
λ h₁ _ h₂, prod.no_confusion h₁ h₂
lemma pprod.mk.inj {α : Sort u} {β : Sort v} {x₁ : α} {y₁ : β} {x₂ : α} {y₂ : β}
: pprod.mk x₁ y₁ = pprod.mk x₂ y₂ → and (x₁ = x₂) (y₁ = y₂) :=
λ h, pprod.no_confusion h (λ h₁ h₂, ⟨h₁, h₂⟩)
lemma pprod.mk.inj_arrow {α : Type u} {β : Type v} {x₁ : α} {y₁ : β} {x₂ : α} {y₂ : β}
: (x₁, y₁) = (x₂, y₂) → Π ⦃P : Sort w⦄, (x₁ = x₂ → y₁ = y₂ → P) → P :=
λ h₁ _ h₂, prod.no_confusion h₁ h₂
inductive sum (α : Type u) (β : Type v)
| inl (val : α) : sum
| inr (val : β) : sum
inductive psum (α : Sort u) (β : Sort v)
| inl (val : α) : psum
| inr (val : β) : psum
/--
Logical or.
`or P Q`, with notation `P ∨ Q`, is the proposition which is true if and only if `P` or `Q` is
true.
To prove a goal `⊢ P ∨ Q`, if you know which alternative you want to prove,
you can use the tactics `left` (which gives the goal `⊢ P`)
or `right` (which gives the goal `⊢ Q`).
Given a hypothesis `h : P ∨ Q` and goal `⊢ R`,
the tactic `cases h` will give you two copies of the goal `⊢ R`,
with the hypothesis `h : P` in the first, and the hypothesis `h : Q` in the second.
-/
inductive or (a b : Prop) : Prop
| inl (h : a) : or
| inr (h : b) : or
lemma or.intro_left {a : Prop} (b : Prop) (ha : a) : or a b :=
or.inl ha
lemma or.intro_right (a : Prop) {b : Prop} (hb : b) : or a b :=
or.inr hb
structure sigma {α : Type u} (β : α → Type v) :=
mk :: (fst : α) (snd : β fst)
structure psigma {α : Sort u} (β : α → Sort v) :=
mk :: (fst : α) (snd : β fst)
inductive bool : Type
| ff : bool
| tt : bool
/- Remark: subtype must take a Sort instead of Type because of the axiom strong_indefinite_description. -/
structure subtype {α : Sort u} (p : α → Prop) :=
(val : α) (property : p val)
attribute [pp_using_anonymous_constructor] sigma psigma subtype pprod and
class inductive decidable (p : Prop)
| is_false (h : ¬p) : decidable
| is_true (h : p) : decidable
@[reducible]
def decidable_pred {α : Sort u} (r : α → Prop) :=
Π (a : α), decidable (r a)
@[reducible]
def decidable_rel {α : Sort u} (r : α → α → Prop) :=
Π (a b : α), decidable (r a b)
@[reducible]
def decidable_eq (α : Sort u) :=
decidable_rel (@eq α)
inductive option (α : Type u)
| none : option
| some (val : α) : option
export option (none some)
export bool (ff tt)
inductive list (T : Type u)
| nil : list
| cons (hd : T) (tl : list) : list
infixr ` :: `:67 := list.cons
notation `[` l:(foldr `, ` (h t, list.cons h t) list.nil `]`) := l
inductive nat
| zero : nat
| succ (n : nat) : nat
structure unification_constraint :=
{α : Type u} (lhs : α) (rhs : α)
infix ` ≟ `:50 := unification_constraint.mk
infix ` =?= `:50 := unification_constraint.mk
structure unification_hint :=
(pattern : unification_constraint)
(constraints : list unification_constraint)
/- Declare builtin and reserved notation -/
class has_zero (α : Type u) := (zero : α)
class has_one (α : Type u) := (one : α)
class has_add (α : Type u) := (add : α → α → α)
class has_mul (α : Type u) := (mul : α → α → α)
class has_inv (α : Type u) := (inv : α → α)
class has_neg (α : Type u) := (neg : α → α)
class has_sub (α : Type u) := (sub : α → α → α)
class has_div (α : Type u) := (div : α → α → α)
class has_dvd (α : Type u) := (dvd : α → α → Prop)
class has_mod (α : Type u) := (mod : α → α → α)
class has_le (α : Type u) := (le : α → α → Prop)
class has_lt (α : Type u) := (lt : α → α → Prop)
class has_append (α : Type u) := (append : α → α → α)
class has_andthen (α : Type u) (β : Type v) (σ : out_param $ Type w) := (andthen : α → β → σ)
class has_union (α : Type u) := (union : α → α → α)
class has_inter (α : Type u) := (inter : α → α → α)
class has_sdiff (α : Type u) := (sdiff : α → α → α)
class has_equiv (α : Sort u) := (equiv : α → α → Prop)
class has_subset (α : Type u) := (subset : α → α → Prop)
class has_ssubset (α : Type u) := (ssubset : α → α → Prop)
/- Type classes has_emptyc and has_insert are
used to implement polymorphic notation for collections.
Example: {a, b, c}. -/
class has_emptyc (α : Type u) := (emptyc : α)
class has_insert (α : out_param $ Type u) (γ : Type v) := (insert : α → γ → γ)
class has_singleton (α : out_param $ Type u) (β : Type v) := (singleton : α → β)
/- Type class used to implement the notation { a ∈ c | p a } -/
class has_sep (α : out_param $ Type u) (γ : Type v) :=
(sep : (α → Prop) → γ → γ)
/- Type class for set-like membership -/
class has_mem (α : out_param $ Type u) (γ : Type v) := (mem : α → γ → Prop)
class has_pow (α : Type u) (β : Type v) :=
(pow : α → β → α)
export has_andthen (andthen)
export has_pow (pow)
infix ` ∈ `:50 := has_mem.mem
notation a ` ∉ `:50 s:50 := ¬ has_mem.mem a s
infixl ` + `:65 := has_add.add
infixl ` * `:70 := has_mul.mul
infixl ` - `:65 := has_sub.sub
infixl ` / `:70 := has_div.div
infix ` ∣ `:50 := has_dvd.dvd -- Note this is different to `|`.
infixl ` % `:70 := has_mod.mod
prefix `-`:75 := has_neg.neg
infix ` <= `:50 := has_le.le
infix ` ≤ `:50 := has_le.le
infix ` < `:50 := has_lt.lt
infixl ` ++ `:65 := has_append.append
infixl `; `:1 := andthen
notation `∅` := has_emptyc.emptyc
infixl ` ∪ `:65 := has_union.union
infixl ` ∩ `:70 := has_inter.inter
infix ` ⊆ `:50 := has_subset.subset
infix ` ⊂ `:50 := has_ssubset.ssubset
infix ` \ `:70 := has_sdiff.sdiff
infix ` ≈ `:50 := has_equiv.equiv
infixr ` ^ `:80 := has_pow.pow
export has_append (append)
@[reducible] def ge {α : Type u} [has_le α] (a b : α) : Prop := has_le.le b a
@[reducible] def gt {α : Type u} [has_lt α] (a b : α) : Prop := has_lt.lt b a
infix ` >= `:50 := ge
infix ` ≥ `:50 := ge
infix ` > `:50 := gt
@[reducible] def superset {α : Type u} [has_subset α] (a b : α) : Prop := has_subset.subset b a
@[reducible] def ssuperset {α : Type u} [has_ssubset α] (a b : α) : Prop := has_ssubset.ssubset b a
infix ` ⊇ `:50 := superset
infix ` ⊃ `:50 := ssuperset
def bit0 {α : Type u} [s : has_add α] (a : α) : α := a + a
def bit1 {α : Type u} [s₁ : has_one α] [s₂ : has_add α] (a : α) : α := (bit0 a) + 1
attribute [pattern] has_zero.zero has_one.one bit0 bit1 has_add.add has_neg.neg has_mul.mul
export has_insert (insert)
class is_lawful_singleton (α : Type u) (β : Type v) [has_emptyc β] [has_insert α β]
[has_singleton α β] : Prop :=
(insert_emptyc_eq : ∀ (x : α), (insert x ∅ : β) = {x})
export has_singleton (singleton)
export is_lawful_singleton (insert_emptyc_eq)
attribute [simp] insert_emptyc_eq
/- nat basic instances -/
namespace nat
protected def add : nat → nat → nat
| a zero := a
| a (succ b) := succ (add a b)
/- We mark the following definitions as pattern to make sure they can be used in recursive equations,
and reduced by the equation compiler. -/
attribute [pattern] nat.add nat.add._main
end nat
instance : has_zero nat := ⟨nat.zero⟩
instance : has_one nat := ⟨nat.succ (nat.zero)⟩
instance : has_add nat := ⟨nat.add⟩
def std.priority.default : nat := 1000
def std.priority.max : nat := 0xFFFFFFFF
namespace nat
protected def prio := std.priority.default + 100
end nat
/-
Global declarations of right binding strength
If a module reassigns these, it will be incompatible with other modules that adhere to these
conventions.
When hovering over a symbol, use "C-c C-k" to see how to input it.
-/
def std.prec.max : nat := 1024 -- the strength of application, identifiers, (, [, etc.
def std.prec.arrow : nat := 25
/-
The next def is "max + 10". It can be used e.g. for postfix operations that should
be stronger than application.
-/
def std.prec.max_plus : nat := std.prec.max + 10
postfix `⁻¹`:std.prec.max_plus := has_inv.inv -- input with \sy or \-1 or \inv
infixr ` × `:35 := prod
-- notation for n-ary tuples
/- sizeof -/
class has_sizeof (α : Sort u) :=
(sizeof : α → nat)
def sizeof {α : Sort u} [s : has_sizeof α] : α → nat :=
has_sizeof.sizeof
/-
Declare sizeof instances and lemmas for types declared before has_sizeof.
From now on, the inductive compiler will automatically generate sizeof instances and lemmas.
-/
/- Every type `α` has a default has_sizeof instance that just returns 0 for every element of `α` -/
protected def default.sizeof (α : Sort u) : α → nat
| a := 0
instance default_has_sizeof (α : Sort u) : has_sizeof α :=
⟨default.sizeof α⟩
protected def nat.sizeof : nat → nat
| n := n
instance : has_sizeof nat :=
⟨nat.sizeof⟩
protected def prod.sizeof {α : Type u} {β : Type v} [has_sizeof α] [has_sizeof β] : (prod α β) → nat
| ⟨a, b⟩ := 1 + sizeof a + sizeof b
instance (α : Type u) (β : Type v) [has_sizeof α] [has_sizeof β] : has_sizeof (prod α β) :=
⟨prod.sizeof⟩
protected def sum.sizeof {α : Type u} {β : Type v} [has_sizeof α] [has_sizeof β] : (sum α β) → nat
| (sum.inl a) := 1 + sizeof a
| (sum.inr b) := 1 + sizeof b
instance (α : Type u) (β : Type v) [has_sizeof α] [has_sizeof β] : has_sizeof (sum α β) :=
⟨sum.sizeof⟩
protected def psum.sizeof {α : Type u} {β : Type v} [has_sizeof α] [has_sizeof β] : (psum α β) → nat
| (psum.inl a) := 1 + sizeof a
| (psum.inr b) := 1 + sizeof b
instance (α : Type u) (β : Type v) [has_sizeof α] [has_sizeof β] : has_sizeof (psum α β) :=
⟨psum.sizeof⟩
protected def sigma.sizeof {α : Type u} {β : α → Type v} [has_sizeof α] [∀ a, has_sizeof (β a)] : sigma β → nat
| ⟨a, b⟩ := 1 + sizeof a + sizeof b
instance (α : Type u) (β : α → Type v) [has_sizeof α] [∀ a, has_sizeof (β a)] : has_sizeof (sigma β) :=
⟨sigma.sizeof⟩
protected def psigma.sizeof {α : Type u} {β : α → Type v} [has_sizeof α] [∀ a, has_sizeof (β a)] : psigma β → nat
| ⟨a, b⟩ := 1 + sizeof a + sizeof b
instance (α : Type u) (β : α → Type v) [has_sizeof α] [∀ a, has_sizeof (β a)] : has_sizeof (psigma β) :=
⟨psigma.sizeof⟩
protected def punit.sizeof : punit → nat
| u := 1
instance : has_sizeof punit := ⟨punit.sizeof⟩
protected def bool.sizeof : bool → nat
| b := 1
instance : has_sizeof bool := ⟨bool.sizeof⟩
protected def option.sizeof {α : Type u} [has_sizeof α] : option α → nat
| none := 1
| (some a) := 1 + sizeof a
instance (α : Type u) [has_sizeof α] : has_sizeof (option α) :=
⟨option.sizeof⟩
protected def list.sizeof {α : Type u} [has_sizeof α] : list α → nat
| list.nil := 1
| (list.cons a l) := 1 + sizeof a + list.sizeof l
instance (α : Type u) [has_sizeof α] : has_sizeof (list α) :=
⟨list.sizeof⟩
protected def subtype.sizeof {α : Type u} [has_sizeof α] {p : α → Prop} : subtype p → nat
| ⟨a, _⟩ := sizeof a
instance {α : Type u} [has_sizeof α] (p : α → Prop) : has_sizeof (subtype p) :=
⟨subtype.sizeof⟩
lemma nat_add_zero (n : nat) : n + 0 = n := rfl
/- Combinator calculus -/
namespace combinator
universes u₁ u₂ u₃
def I {α : Type u₁} (a : α) := a
def K {α : Type u₁} {β : Type u₂} (a : α) (b : β) := a
def S {α : Type u₁} {β : Type u₂} {γ : Type u₃} (x : α → β → γ) (y : α → β) (z : α) := x z (y z)
end combinator
/-- Auxiliary datatype for #[ ... ] notation.
#[1, 2, 3, 4] is notation for
bin_tree.node
(bin_tree.node (bin_tree.leaf 1) (bin_tree.leaf 2))
(bin_tree.node (bin_tree.leaf 3) (bin_tree.leaf 4))
We use this notation to input long sequences without exhausting the system stack space.
Later, we define a coercion from `bin_tree` into `list`.
-/
inductive bin_tree (α : Type u)
| empty : bin_tree
| leaf (val : α) : bin_tree
| node (left right : bin_tree) : bin_tree
attribute [elab_simple] bin_tree.node bin_tree.leaf
/-- Like `by apply_instance`, but not dependent on the tactic framework. -/
@[reducible] def infer_instance {α : Sort u} [i : α] : α := i
|
315dda9e5ecf777d8f367524276c4254731dd9ef | a44280b79dc85615010e3fbda46abf82c6730fa3 | /library/init/lean/compiler/ir.lean | f02ec0090876cda3445d6e5915482a93ac045546 | [
"Apache-2.0"
] | permissive | kodyvajjha/lean4 | 8e1c613248b531d47367ca6e8d97ee1046645aa1 | c8a045d69fac152fd5e3a577f718615cecb9c53d | refs/heads/master | 1,589,684,450,102 | 1,555,200,447,000 | 1,556,139,945,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,258 | 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 init.lean.name init.lean.kvmap
prelude
/-
Implements (extended) λPure and λRc proposed in the article
"Counting Immutable Beans", Sebastian Ullrich and Leonardo de Moura.
The Lean to IR transformation produces λPure code. That is,
then transformed using the procedures described in the paper above.
-/
namespace Lean
namespace IR
/- Variable identifier -/
abbrev VarId := Name
/- Function identifier -/
abbrev FunId := Name
/- Join point identifier -/
abbrev JoinPointId := Name
abbrev MData := KVMap
namespace MData
abbrev empty : MData := {KVMap .}
instance : HasEmptyc MData := ⟨empty⟩
end MData
/- Low Level IR types. Most are self explanatory.
- `Usize` represents the C++ `sizeT` Type. We have it here
because it is 32-bit in 32-bit machines, and 64-bit in 64-bit machines,
and we want the C++ backend for our Compiler to generate platform independent code.
- `irrelevant` for Lean types, propositions and proofs.
- `object` a pointer to a value in the heap.
- `tobject` a pointer to a value in the heap or tagged pointer
(i.e., the least significant bit is 1) storing a scalar value.
Remark: the RC operations for `tobject` are slightly more expensive because we
first need to test whether the `tobject` is really a pointer or not.
Remark: the Lean runtime assumes that sizeof(void*) == sizeof(sizeT).
Lean cannot be compiled on old platforms where this is not True. -/
inductive IRType
| float | uint8 | uint16 | uint32 | uint64 | Usize
| irrelevant | object | tobject
def IRType.beq : IRType → IRType → Bool
| IRType.float IRType.float := true
| IRType.uint8 IRType.uint8 := true
| IRType.uint16 IRType.uint16 := true
| IRType.uint32 IRType.uint32 := true
| IRType.uint64 IRType.uint64 := true
| IRType.Usize IRType.Usize := true
| IRType.irrelevant IRType.irrelevant := true
| IRType.object IRType.object := true
| IRType.tobject IRType.tobject := true
| _ _ := false
instance IRType.HasBeq : HasBeq IRType := ⟨IRType.beq⟩
/- Arguments to applications, constructors, etc.
We use `irrelevant` for Lean types, propositions and proofs that have been erased.
Recall that for a Function `f`, we also generate `f._rarg` which does not take
`irrelevant` arguments. However, `f._rarg` is only safe to be used in full applications. -/
inductive Arg
| var (id : VarId)
| irrelevant
inductive Litval
| num (v : Nat)
| str (v : String)
def Litval.beq : Litval → Litval → Bool
| (Litval.num v₁) (Litval.num v₂) := v₁ == v₂
| (Litval.str v₁) (Litval.str v₂) := v₁ == v₂
| _ _ := false
instance Litval.HasBeq : HasBeq Litval := ⟨Litval.beq⟩
/- Constructor information.
- `id` is the Name of the Constructor in Lean.
- `cidx` is the Constructor index (aka tag).
- `Usize` is the number of arguments of type `Usize`.
- `ssize` is the number of bytes used to store scalar values.
Recall that a Constructor object contains a header, then a sequence of
pointers to other Lean objects, a sequence of `Usize` (i.e., `sizeT`)
scalar values, and a sequence of other scalar values. -/
structure CtorInfo :=
(id : Name) (cidx : Nat) (Usize : Nat) (ssize : Nat)
def CtorInfo.beq : CtorInfo → CtorInfo → Bool
| ⟨id₁, cidx₁, usize₁, ssize₁⟩ ⟨id₂, cidx₂, usize₂, ssize₂⟩ :=
id₁ == id₂ && cidx₁ == cidx₂ && usize₁ == usize₂ && ssize₁ == ssize₂
instance CtorInfo.HasBeq : HasBeq CtorInfo := ⟨CtorInfo.beq⟩
inductive Expr
| ctor (i : CtorInfo) (ys : List Arg)
| reset (x : VarId)
/- `reuse x in ctorI ys` instruction in the paper. -/
| reuse (x : VarId) (i : CtorInfo) (ys : List Arg)
/- Extract the `tobject` value at Position `sizeof(void)*i` from `x`. -/
| proj (i : Nat) (x : VarId)
/- Extract the `Usize` value at Position `sizeof(void)*i` from `x`. -/
| uproj (i : Nat) (x : VarId)
/- Extract the scalar value at Position `n` (in bytes) from `x`. -/
| sproj (n : Nat) (x : VarId)
/- Full application. -/
| fap (c : FunId) (ys : List Arg)
/- Partial application that creates a `pap` value (aka closure in our nonstandard terminology). -/
| pap (c : FunId) (ys : List Arg)
/- Application. `x` must be a `pap` value. -/
| ap (x : VarId) (ys : List Arg)
/- Given `x : ty` where `ty` is a scalar type, this operation returns a value of Type `tobject`.
For small scalar values, the Result is a tagged pointer, and no memory allocation is performed. -/
| box (ty : IRType) (x : VarId)
/- Given `x : [t]object`, obtain the scalar value. -/
| unbox (x : VarId)
| lit (v : Litval)
/- Return `1 : uint8` Iff `RC(x) > 1` -/
| isShared (x : VarId)
/- Return `1 : uint8` Iff `x : tobject` is a tagged pointer (storing a scalar value). -/
| isTaggedPtr (x : VarId)
structure Param :=
(x : Name) (borrowed : Bool) (ty : IRType)
inductive AltCore (Fnbody : Type) : Type
| ctor (info : CtorInfo) (b : Fnbody) : AltCore
| default (b : Fnbody) : AltCore
inductive Fnbody
/- `let x : ty := e; b` -/
| vdecl (x : VarId) (ty : IRType) (e : Expr) (b : Fnbody)
/- Join point Declaration `let j (xs) : ty := e; b` -/
| jdecl (j : JoinPointId) (xs : List Param) (ty : IRType) (v : Fnbody) (b : Fnbody)
/- Store `y` at Position `sizeof(void*)*i` in `x`. `x` must be a Constructor object and `RC(x)` must be 1.
This operation is not part of λPure is only used during optimization. -/
| set (x : VarId) (i : Nat) (y : VarId) (b : Fnbody)
/- Store `y : Usize` at Position `sizeof(void*)*i` in `x`. `x` must be a Constructor object and `RC(x)` must be 1. -/
| uset (x : VarId) (i : Nat) (y : VarId) (b : Fnbody)
/- Store `y : ty` at Position `sizeof(void*)*i + offset` in `x`. `x` must be a Constructor object and `RC(x)` must be 1.
`ty` must not be `object`, `tobject`, `irrelevant` nor `Usize`. -/
| sset (x : VarId) (i : Nat) (offset : Nat) (y : VarId) (ty : IRType) (b : Fnbody)
| release (x : VarId) (i : Nat) (b : Fnbody)
/- RC increment for `object`. If c == `true`, then `inc` must check whether `x` is a tagged pointer or not. -/
| inc (x : VarId) (n : Nat) (c : Bool) (b : Fnbody)
/- RC decrement for `object`. If c == `true`, then `inc` must check whether `x` is a tagged pointer or not. -/
| dec (x : VarId) (n : Nat) (c : Bool) (b : Fnbody)
| mdata (d : MData) (b : Fnbody)
| case (tid : Name) (x : VarId) (cs : List (AltCore Fnbody))
| ret (x : VarId)
/- Jump to join point `j` -/
| jmp (j : JoinPointId) (ys : List Arg)
| unreachable
abbrev Alt := AltCore Fnbody
@[pattern] abbrev Alt.ctor := @AltCore.ctor Fnbody
@[pattern] abbrev Alt.default := @AltCore.default Fnbody
inductive Decl
| fdecl (f : FunId) (xs : List Param) (ty : IRType) (b : Fnbody)
| extern (f : FunId) (xs : List Param) (ty : IRType)
/-- `Expr.isPure e` return `true` Iff `e` is in the `λPure` fragment. -/
def Expr.isPure : Expr → Bool
| (Expr.ctor _ _) := true
| (Expr.proj _ _) := true
| (Expr.uproj _ _) := true
| (Expr.sproj _ _) := true
| (Expr.fap _ _) := true
| (Expr.pap _ _) := true
| (Expr.ap _ _) := true
| (Expr.lit _) := true
| _ := false
/-- `Fnbody.isPure b` return `true` Iff `b` is in the `λPure` fragment. -/
partial def Fnbody.isPure : Fnbody → Bool
| (Fnbody.vdecl _ _ e b) := e.isPure && b.isPure
| (Fnbody.jdecl _ _ _ e b) := e.isPure && b.isPure
| (Fnbody.uset _ _ _ b) := b.isPure
| (Fnbody.sset _ _ _ _ _ b) := b.isPure
| (Fnbody.mdata _ b) := b.isPure
| (Fnbody.case _ _ alts) := alts.any $ λ alt,
(match alt with
| (Alt.ctor _ b) := b.isPure
| (Alt.default b) := false)
| (Fnbody.ret _) := true
| (Fnbody.jmp _ _) := true
| Fnbody.unreachable := true
| _ := false
abbrev VarRenaming := NameMap Name
class HasAlphaEqv (α : Type) :=
(aeqv : VarRenaming → α → α → Bool)
local notation a `=[`:50 ρ `]=`:0 b:50 := HasAlphaEqv.aeqv ρ a b
def VarId.alphaEqv (ρ : VarRenaming) (v₁ v₂ : VarId) : Bool :=
match ρ.find v₁ with
| some v := v == v₂
| none := v₁ == v₂
instance VarId.hasAeqv : HasAlphaEqv VarId := ⟨VarId.alphaEqv⟩
def Arg.alphaEqv (ρ : VarRenaming) : Arg → Arg → Bool
| (Arg.var v₁) (Arg.var v₂) := v₁ =[ρ]= v₂
| Arg.irrelevant Arg.irrelevant := true
| _ _ := false
instance Arg.hasAeqv : HasAlphaEqv Arg := ⟨Arg.alphaEqv⟩
def args.alphaEqv (ρ : VarRenaming) : List Arg → List Arg → Bool
| [] [] := true
| (a::as) (b::bs) := a =[ρ]= b && args.alphaEqv as bs
| _ _ := false
instance args.hasAeqv : HasAlphaEqv (List Arg) := ⟨args.alphaEqv⟩
def Expr.alphaEqv (ρ : VarRenaming) : Expr → Expr → Bool
| (Expr.ctor i₁ ys₁) (Expr.ctor i₂ ys₂) := i₁ == i₂ && ys₁ =[ρ]= ys₂
| (Expr.reset x₁) (Expr.reset x₂) := x₁ =[ρ]= x₂
| (Expr.reuse x₁ i₁ ys₁) (Expr.reuse x₂ i₂ ys₂) := x₁ =[ρ]= x₂ && i₁ == i₂ && ys₁ =[ρ]= ys₂
| (Expr.proj i₁ x₁) (Expr.proj i₂ x₂) := i₁ == i₂ && x₁ =[ρ]= x₂
| (Expr.uproj i₁ x₁) (Expr.uproj i₂ x₂) := i₁ == i₂ && x₁ =[ρ]= x₂
| (Expr.sproj n₁ x₁) (Expr.sproj n₂ x₂) := n₁ == n₂ && x₁ =[ρ]= x₂
| (Expr.fap c₁ ys₁) (Expr.fap c₂ ys₂) := c₁ == c₂ && ys₁ =[ρ]= ys₂
| (Expr.pap c₁ ys₁) (Expr.pap c₂ ys₂) := c₁ == c₂ && ys₂ =[ρ]= ys₂
| (Expr.ap x₁ ys₁) (Expr.ap x₂ ys₂) := x₁ =[ρ]= x₂ && ys₁ =[ρ]= ys₂
| (Expr.box ty₁ x₁) (Expr.box ty₂ x₂) := ty₁ == ty₂ && x₁ =[ρ]= x₂
| (Expr.unbox x₁) (Expr.unbox x₂) := x₁ =[ρ]= x₂
| (Expr.lit v₁) (Expr.lit v₂) := v₁ == v₂
| (Expr.isShared x₁) (Expr.isShared x₂) := x₁ =[ρ]= x₂
| (Expr.isTaggedPtr x₁) (Expr.isTaggedPtr x₂) := x₁ =[ρ]= x₂
| _ _ := false
instance Expr.hasAeqv : HasAlphaEqv Expr:= ⟨Expr.alphaEqv⟩
def addVarRename (ρ : VarRenaming) (x₁ x₂ : Name) :=
if x₁ = x₂ then ρ else ρ.insert x₁ x₂
def addParamRename (ρ : VarRenaming) (p₁ p₂ : Param) : Option VarRenaming :=
if p₁.ty == p₂.ty && p₁.borrowed = p₂.borrowed then some (addVarRename ρ p₁.x p₂.x)
else none
def addParamsRename : VarRenaming → List Param → List Param → Option VarRenaming
| ρ (p₁::ps₁) (p₂::ps₂) := do ρ ← addParamRename ρ p₁ p₂, addParamsRename ρ ps₁ ps₂
| ρ [] [] := some ρ
| _ _ _ := none
partial def Fnbody.alphaEqv : VarRenaming → Fnbody → Fnbody → Bool
| ρ (Fnbody.vdecl x₁ t₁ v₁ b₁) (Fnbody.vdecl x₂ t₂ v₂ b₂) := t₁ == t₂ && v₁ =[ρ]= v₂ && Fnbody.alphaEqv (addVarRename ρ x₁ x₂) b₁ b₂
| ρ (Fnbody.jdecl j₁ ys₁ t₁ v₁ b₁) (Fnbody.jdecl j₂ ys₂ t₂ v₂ b₂) :=
(match addParamsRename ρ ys₁ ys₂ with
| some ρ' := t₁ == t₂ && Fnbody.alphaEqv ρ' v₁ v₂ && Fnbody.alphaEqv (addVarRename ρ j₁ j₂) b₁ b₂
| none := false)
| ρ (Fnbody.set x₁ i₁ y₁ b₁) (Fnbody.set x₂ i₂ y₂ b₂) := x₁ =[ρ]= x₂ && i₁ == i₂ && y₁ =[ρ]= y₂ && Fnbody.alphaEqv ρ b₁ b₂
| ρ (Fnbody.uset x₁ i₁ y₁ b₁) (Fnbody.uset x₂ i₂ y₂ b₂) := x₁ =[ρ]= x₂ && i₁ == i₂ && y₁ =[ρ]= y₂ && Fnbody.alphaEqv ρ b₁ b₂
| ρ (Fnbody.sset x₁ i₁ o₁ y₁ t₁ b₁) (Fnbody.sset x₂ i₂ o₂ y₂ t₂ b₂) :=
x₁ =[ρ]= x₂ && i₁ = i₂ && o₁ = o₂ && y₁ =[ρ]= y₂ && t₁ == t₂ && Fnbody.alphaEqv ρ b₁ b₂
| ρ (Fnbody.release x₁ i₁ b₁) (Fnbody.release x₂ i₂ b₂) := x₁ =[ρ]= x₂ && i₁ == i₂ && Fnbody.alphaEqv ρ b₁ b₂
| ρ (Fnbody.inc x₁ n₁ c₁ b₁) (Fnbody.inc x₂ n₂ c₂ b₂) := x₁ =[ρ]= x₂ && n₁ == n₂ && c₁ == c₂ && Fnbody.alphaEqv ρ b₁ b₂
| ρ (Fnbody.dec x₁ n₁ c₁ b₁) (Fnbody.dec x₂ n₂ c₂ b₂) := x₁ =[ρ]= x₂ && n₁ == n₂ && c₁ == c₂ && Fnbody.alphaEqv ρ b₁ b₂
| ρ (Fnbody.mdata m₁ b₁) (Fnbody.mdata m₂ b₂) := m₁ == m₂ && Fnbody.alphaEqv ρ b₁ b₂
| ρ (Fnbody.case n₁ x₁ alts₁) (Fnbody.case n₂ x₂ alts₂) := n₁ == n₂ && x₁ =[ρ]= x₂ && List.isEqv alts₁ alts₂ (λ alt₁ alt₂,
match alt₁, alt₂ with
| Alt.ctor i₁ b₁, Alt.ctor i₂ b₂ := i₁ == i₂ && Fnbody.alphaEqv ρ b₁ b₂
| Alt.default b₁, Alt.default b₂ := Fnbody.alphaEqv ρ b₁ b₂
| _, _ := false)
| ρ (Fnbody.jmp j₁ ys₁) (Fnbody.jmp j₂ ys₂) := j₁ == j₂ && ys₁ =[ρ]= ys₂
| ρ (Fnbody.ret x₁) (Fnbody.ret x₂) := x₁ =[ρ]= x₂
| _ Fnbody.unreachable Fnbody.unreachable := true
| _ _ _ := false
def Fnbody.beq (b₁ b₂ : Fnbody) : Bool :=
Fnbody.alphaEqv ∅ b₁ b₂
instance Fnbody.HasBeq : HasBeq Fnbody := ⟨Fnbody.beq⟩
abbrev VarSet := NameSet
section freeVariables
abbrev Collector := NameSet → NameSet → NameSet
@[inline] private def skip : Collector :=
λ bv fv, fv
@[inline] private def collectVar (x : VarId) : Collector :=
λ bv fv, if bv.contains x then fv else fv.insert x
@[inline] private def withBv (x : VarId) : Collector → Collector :=
λ k bv fv, k (bv.insert x) fv
def insertParams (s : VarSet) (ys : List Param) : VarSet :=
ys.foldl (λ s p, s.insert p.x) s
@[inline] private def withParams (ys : List Param) : Collector → Collector :=
λ k bv fv, k (insertParams bv ys) fv
@[inline] private def seq : Collector → Collector → Collector :=
λ k₁ k₂ bv fv, k₂ bv (k₁ bv fv)
instance : HasAndthen Collector :=
⟨seq⟩
private def collectArg : Arg → Collector
| (Arg.var x) := collectVar x
| irrelevant := skip
private def collectArgs : List Arg → Collector
| [] := skip
| (a::as) := collectArg a; collectArgs as
private def collectExpr : Expr → Collector
| (Expr.ctor i ys) := collectArgs ys
| (Expr.reset x) := collectVar x
| (Expr.reuse x i ys) := collectVar x; collectArgs ys
| (Expr.proj i x) := collectVar x
| (Expr.uproj i x) := collectVar x
| (Expr.sproj n x) := collectVar x
| (Expr.fap c ys) := collectArgs ys
| (Expr.pap c ys) := collectArgs ys
| (Expr.ap x ys) := collectVar x; collectArgs ys
| (Expr.box ty x) := collectVar x
| (Expr.unbox x) := collectVar x
| (Expr.lit v) := skip
| (Expr.isShared x) := collectVar x
| (Expr.isTaggedPtr x) := collectVar x
private def collectAlts (f : Fnbody → Collector) : List Alt → Collector
| [] := skip
| (Alt.ctor _ b :: alts) := f b; collectAlts alts
| (Alt.default b :: alts) := f b; collectAlts alts
private partial def collectFnBody : Fnbody → Collector
| (Fnbody.vdecl x _ v b) := collectExpr v; withBv x (collectFnBody b)
| (Fnbody.jdecl j ys _ v b) := withParams ys (collectFnBody v); withBv j (collectFnBody b)
| (Fnbody.set x _ y b) := collectVar x; collectVar y; collectFnBody b
| (Fnbody.uset x _ y b) := collectVar x; collectVar y; collectFnBody b
| (Fnbody.sset x _ _ y _ b) := collectVar x; collectVar y; collectFnBody b
| (Fnbody.release x _ b) := collectVar x; collectFnBody b
| (Fnbody.inc x _ _ b) := collectVar x; collectFnBody b
| (Fnbody.dec x _ _ b) := collectVar x; collectFnBody b
| (Fnbody.mdata _ b) := collectFnBody b
| (Fnbody.case _ x alts) := collectVar x; collectAlts collectFnBody alts
| (Fnbody.jmp j ys) := collectVar j; collectArgs ys
| (Fnbody.ret x) := collectVar x
| Fnbody.unreachable := skip
def freeVars (b : Fnbody) : VarSet :=
collectFnBody b {} {}
end freeVariables
end IR
end Lean
|
531615c7b86fcd2a57685d161fe50b077a760c78 | a721fe7446524f18ba361625fc01033d9c8b7a78 | /elaborate/mul_cancel_induction.stripped.lean | 654426fcc3e3d6a7235b9e1b98e0c366d66e5fcf | [] | no_license | Sterrs/leaning | 8fd80d1f0a6117a220bb2e57ece639b9a63deadc | 3901cc953694b33adda86cb88ca30ba99594db31 | refs/heads/master | 1,627,023,822,744 | 1,616,515,221,000 | 1,616,515,221,000 | 245,512,190 | 2 | 0 | null | 1,616,429,050,000 | 1,583,527,118,000 | Lean | UTF-8 | Lean | false | false | 18,680 | lean | mynat.rec (λ {m k : mynat} (hmn0 : m = zero → false) (hmk0 : zero = mul m k), eq.rec (eq.refl k) (mynat.rec (eq.rec true.intro (eq.rec (eq.refl ((m = zero → false) → zero = zero → zero = zero)) (eq.rec (propext {mp := λ (hab : (m = zero → false) → zero = zero → zero = zero) (hc : m = zero → false), (eq.rec {mp := λ (h : zero = zero → zero = zero), h, mpr := λ (h : zero = zero → zero = zero), h} (eq.rec (propext {mp := λ (hab : zero = zero → zero = zero) (hc : true), (eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mp (hab ((eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mpr hc)), mpr := λ (hcd : true → true) (ha : zero = zero), (eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mpr (hcd ((eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mp ha))}) (propext {mp := λ (h : true → true), h true.intro, mpr := λ (h h' : true), h}))).mp (hab hc), mpr := λ (hcd : (m = zero → false) → true) (ha : m = zero → false), (eq.rec {mp := λ (h : zero = zero → zero = zero), h, mpr := λ (h : zero = zero → zero = zero), h} (eq.rec (propext {mp := λ (hab : zero = zero → zero = zero) (hc : true), (eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mp (hab ((eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mpr hc)), mpr := λ (hcd : true → true) (ha : zero = zero), (eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mpr (hcd ((eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mp ha))}) (propext {mp := λ (h : true → true), h true.intro, mpr := λ (h h' : true), h}))).mpr (hcd ha)}) (propext {mp := λ (h : (m = zero → false) → true), true.intro, mpr := λ (ha : true) (h : m = zero → false), true.intro})))) (λ (n : mynat) (ih : (m = zero → false) → mul m n = zero → n = zero) (hmne0 : m = zero → false) (hmn0 : add m (mul m n) = zero), false.rec (succ n = zero) (hmne0 (mynat.rec (eq.rec true.intro (eq.rec (eq.refl (add zero (mul m n) = zero → zero = zero)) (eq.rec (propext {mp := λ (hab : add zero (mul m n) = zero → zero = zero) (hc : mul m n = zero), (eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mp (hab ((eq.rec {mp := λ (h : add zero (mul m n) = zero), h, mpr := λ (h : add zero (mul m n) = zero), h} (eq.rec (eq.refl (add zero (mul m n) = zero)) (eq.rec (eq.refl (eq (add zero (mul m n)))) (eq.rec (eq.refl (add zero (mul m n))) ((mynat.rec ⟨λ (m : mynat), eq.rec (eq.refl m) (eq.rec (eq.refl (m = add zero m)) (eq.rec (eq.refl (m = add zero m)) ((mynat.rec ⟨eq.refl zero, punit.star⟩ (λ (n : mynat) (ih : pprod (add zero n = n) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (add zero n = n) ih) punit) n)), ⟨eq.rec (eq.refl (succ n)) (eq.rec (eq.refl (succ (add zero n) = succ n)) (eq.rec (eq.refl (succ (add zero n) = succ n)) (ih.fst))), ⟨ih, punit.star⟩⟩) m).fst))), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add m n = add n m) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add m n = add n m) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.rec (eq.refl (succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (ih.fst m)))) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) ((mynat.rec ⟨λ (m : mynat), eq.refl (succ m), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.refl (succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (ih.fst m))), ⟨ih, punit.star⟩⟩) m).fst n))), ⟨ih, punit.star⟩⟩) (mul m n)).fst zero))))).mpr hc)), mpr := λ (hcd : mul m n = zero → true) (ha : add zero (mul m n) = zero), (eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mpr (hcd ((eq.rec {mp := λ (h : add zero (mul m n) = zero), h, mpr := λ (h : add zero (mul m n) = zero), h} (eq.rec (eq.refl (add zero (mul m n) = zero)) (eq.rec (eq.refl (eq (add zero (mul m n)))) (eq.rec (eq.refl (add zero (mul m n))) ((mynat.rec ⟨λ (m : mynat), eq.rec (eq.refl m) (eq.rec (eq.refl (m = add zero m)) (eq.rec (eq.refl (m = add zero m)) ((mynat.rec ⟨eq.refl zero, punit.star⟩ (λ (n : mynat) (ih : pprod (add zero n = n) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (add zero n = n) ih) punit) n)), ⟨eq.rec (eq.refl (succ n)) (eq.rec (eq.refl (succ (add zero n) = succ n)) (eq.rec (eq.refl (succ (add zero n) = succ n)) (ih.fst))), ⟨ih, punit.star⟩⟩) m).fst))), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add m n = add n m) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add m n = add n m) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.rec (eq.refl (succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (ih.fst m)))) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) ((mynat.rec ⟨λ (m : mynat), eq.refl (succ m), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.refl (succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (ih.fst m))), ⟨ih, punit.star⟩⟩) m).fst n))), ⟨ih, punit.star⟩⟩) (mul m n)).fst zero))))).mp ha))}) (propext {mp := λ (h : mul m n = zero → true), true.intro, mpr := λ (ha : true) (h : mul m n = zero), true.intro})))) (λ (n_1 : mynat) (ih : add n_1 (mul m n) = zero → n_1 = zero), eq.rec (λ (h : succ (add n_1 (mul m n)) = zero), false.rec (succ n_1 = zero) (eq.rec (λ («_» : succ (add n_1 (mul m n)) = succ (add n_1 (mul m n))) (a : zero = succ (add n_1 (mul m n))), eq.rec (λ (h11 : zero = zero) (a : h == eq.refl (succ (add n_1 (mul m n))) → false), a) a a) h h (eq.refl zero) (heq.refl h))) (eq.rec (eq.refl (add (succ n_1) (mul m n) = zero → succ n_1 = zero)) (eq.rec (eq.refl (add (succ n_1) (mul m n) = zero → succ n_1 = zero)) ((mynat.rec ⟨λ (m : mynat), eq.refl (succ m), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.refl (succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (ih.fst m))), ⟨ih, punit.star⟩⟩) (mul m n)).fst n_1)))) m hmn0))) k hmn0 (eq.rec (eq.refl zero) hmk0))) (λ (n : mynat) (hn : ∀ {m k : mynat}, (m = zero → false) → mul m n = mul m k → n = k) {m k : mynat} (hmn0 : m = zero → false) (heq_1 : add m (mul m n) = mul m k), mynat.rec (λ (heq_1 : add m (mul m n) = zero), false.rec (succ n = zero) (eq.rec (λ («_» : succ n = succ n) (a : zero = succ n), eq.rec (λ (h11 : zero = zero) (a : false.rec (succ n = zero) (hmn0 (mynat.rec (eq.rec true.intro (eq.rec (eq.refl (add zero (mul m n) = zero → zero = zero)) (eq.rec (propext {mp := λ (hab : add zero (mul m n) = zero → zero = zero) (hc : mul m n = zero), (eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mp (hab ((eq.rec {mp := λ (h : add zero (mul m n) = zero), h, mpr := λ (h : add zero (mul m n) = zero), h} (eq.rec (eq.refl (add zero (mul m n) = zero)) (eq.rec (eq.refl (eq (add zero (mul m n)))) (eq.rec (eq.refl (add zero (mul m n))) ((mynat.rec ⟨λ (m : mynat), eq.rec (eq.refl m) (eq.rec (eq.refl (m = add zero m)) (eq.rec (eq.refl (m = add zero m)) ((mynat.rec ⟨eq.refl zero, punit.star⟩ (λ (n : mynat) (ih : pprod (add zero n = n) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod … ih) punit) n)), ⟨eq.rec (eq.refl (succ n)) (eq.rec (eq.refl (succ (… n) = succ n)) (eq.rec (eq.refl (succ … = succ n)) (ih.fst))), ⟨ih, punit.star⟩⟩) m).fst))), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add m n = add n m) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add m n = add n m) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.rec (eq.refl (succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (ih.fst m)))) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) ((mynat.rec ⟨λ (m : mynat), eq.refl (succ m), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (… ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.refl (succ (succ …))) (eq.rec (eq.refl (… = …)) (eq.rec (eq.refl …) (ih.fst m))), ⟨ih, punit.star⟩⟩) m).fst n))), ⟨ih, punit.star⟩⟩) (mul m n)).fst zero))))).mpr hc)), mpr := λ (hcd : mul m n = zero → true) (ha : add zero (mul m n) = zero), (eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mpr (hcd ((eq.rec {mp := λ (h : add zero (mul m n) = zero), h, mpr := λ (h : add zero (mul m n) = zero), h} (eq.rec (eq.refl (add zero (mul m n) = zero)) (eq.rec (eq.refl (eq (add zero (mul m n)))) (eq.rec (eq.refl (add zero (mul m n))) ((mynat.rec ⟨λ (m : mynat), eq.rec (eq.refl m) (eq.rec (eq.refl (m = add zero m)) (eq.rec (eq.refl (m = add zero m)) ((mynat.rec ⟨eq.refl zero, punit.star⟩ (λ (n : mynat) (ih : pprod (add zero n = n) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod … ih) punit) n)), ⟨eq.rec (eq.refl (succ n)) (eq.rec (eq.refl (succ (… n) = succ n)) (eq.rec (eq.refl (succ … = succ n)) (ih.fst))), ⟨ih, punit.star⟩⟩) m).fst))), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add m n = add n m) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add m n = add n m) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.rec (eq.refl (succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (ih.fst m)))) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) ((mynat.rec ⟨λ (m : mynat), eq.refl (succ m), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (… ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.refl (succ (succ …))) (eq.rec (eq.refl (… = …)) (eq.rec (eq.refl …) (ih.fst m))), ⟨ih, punit.star⟩⟩) m).fst n))), ⟨ih, punit.star⟩⟩) (mul m n)).fst zero))))).mp ha))}) (propext {mp := λ (h : mul m n = zero → true), true.intro, mpr := λ (ha : true) (h : mul m n = zero), true.intro})))) (λ (n_1 : mynat) (ih : add n_1 (mul m n) = zero → n_1 = zero), eq.rec (λ (h : succ (add n_1 (mul m n)) = zero), false.rec (succ n_1 = zero) (eq.rec (λ («_» : succ (add n_1 (mul m n)) = succ (add n_1 (mul m n))) (a : zero = succ (add n_1 (mul m n))), eq.rec (λ (h11 : zero = zero) (a : h == eq.refl (succ (add n_1 (mul m n))) → false), a) a a) h h (eq.refl zero) (heq.refl h))) (eq.rec (eq.refl (add (succ n_1) (mul m n) = zero → succ n_1 = zero)) (eq.rec (eq.refl (add (succ n_1) (mul m n) = zero → succ n_1 = zero)) ((mynat.rec ⟨λ (m : mynat), eq.refl (succ m), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.refl (succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (ih.fst m))), ⟨ih, punit.star⟩⟩) (mul m n)).fst n_1)))) m heq_1)) == eq.refl (succ n) → false), a) a a) (false.rec (succ n = zero) (hmn0 (mynat.rec (eq.rec true.intro (eq.rec (eq.refl (add zero (mul m n) = zero → zero = zero)) (eq.rec (propext {mp := λ (hab : add zero (mul m n) = zero → zero = zero) (hc : mul m n = zero), (eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mp (hab ((eq.rec {mp := λ (h : add zero (mul m n) = zero), h, mpr := λ (h : add zero (mul m n) = zero), h} (eq.rec (eq.refl (add zero (mul m n) = zero)) (eq.rec (eq.refl (eq (add zero (mul m n)))) (eq.rec (eq.refl (add zero (mul m n))) ((mynat.rec ⟨λ (m : mynat), eq.rec (eq.refl m) (eq.rec (eq.refl (m = add zero m)) (eq.rec (eq.refl (m = add zero m)) ((mynat.rec ⟨eq.refl zero, punit.star⟩ (λ (n : mynat) (ih : pprod (add zero n = n) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (add zero n = n) ih) punit) n)), ⟨eq.rec (eq.refl (succ n)) (eq.rec (eq.refl (succ (add zero n) = succ n)) (eq.rec (eq.refl (succ (add zero n) = succ n)) (ih.fst))), ⟨ih, punit.star⟩⟩) m).fst))), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add m n = add n m) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add m n = add n m) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.rec (eq.refl (succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (ih.fst m)))) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) ((mynat.rec ⟨λ (m : mynat), eq.refl (succ m), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.refl (succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (ih.fst m))), ⟨ih, punit.star⟩⟩) m).fst n))), ⟨ih, punit.star⟩⟩) (mul m n)).fst zero))))).mpr hc)), mpr := λ (hcd : mul m n = zero → true) (ha : add zero (mul m n) = zero), (eq.rec {mp := λ (h : zero = zero), h, mpr := λ (h : zero = zero), h} (eq.rec (eq.refl (zero = zero)) (propext {mp := λ (hl : zero = zero), true.intro, mpr := λ (hr : true), eq.refl zero}))).mpr (hcd ((eq.rec {mp := λ (h : add zero (mul m n) = zero), h, mpr := λ (h : add zero (mul m n) = zero), h} (eq.rec (eq.refl (add zero (mul m n) = zero)) (eq.rec (eq.refl (eq (add zero (mul m n)))) (eq.rec (eq.refl (add zero (mul m n))) ((mynat.rec ⟨λ (m : mynat), eq.rec (eq.refl m) (eq.rec (eq.refl (m = add zero m)) (eq.rec (eq.refl (m = add zero m)) ((mynat.rec ⟨eq.refl zero, punit.star⟩ (λ (n : mynat) (ih : pprod (add zero n = n) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (add zero n = n) ih) punit) n)), ⟨eq.rec (eq.refl (succ n)) (eq.rec (eq.refl (succ (add zero n) = succ n)) (eq.rec (eq.refl (succ (add zero n) = succ n)) (ih.fst))), ⟨ih, punit.star⟩⟩) m).fst))), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add m n = add n m) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add m n = add n m) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.rec (eq.refl (succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (ih.fst m)))) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) ((mynat.rec ⟨λ (m : mynat), eq.refl (succ m), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.refl (succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (ih.fst m))), ⟨ih, punit.star⟩⟩) m).fst n))), ⟨ih, punit.star⟩⟩) (mul m n)).fst zero))))).mp ha))}) (propext {mp := λ (h : mul m n = zero → true), true.intro, mpr := λ (ha : true) (h : mul m n = zero), true.intro})))) (λ (n_1 : mynat) (ih : add n_1 (mul m n) = zero → n_1 = zero), eq.rec (λ (h : succ (add n_1 (mul m n)) = zero), false.rec (succ n_1 = zero) (eq.rec (λ («_» : succ (add n_1 (mul m n)) = succ (add n_1 (mul m n))) (a : zero = succ (add n_1 (mul m n))), eq.rec (λ (h11 : zero = zero) (a : h == eq.refl (succ (add n_1 (mul m n))) → false), a) a a) h h (eq.refl zero) (heq.refl h))) (eq.rec (eq.refl (add (succ n_1) (mul m n) = zero → succ n_1 = zero)) (eq.rec (eq.refl …) …))) m heq_1))) … … …)) … k heq_1) ?M_1
|
896140e43941115d8226064d6665e830abbcca4b | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/measure_theory/probability_mass_function.lean | 0109981417717d88f8448c25c486986bcd313394 | [
"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 | 13,254 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import topology.instances.ennreal
/-!
# Probability mass functions
This file is about probability mass functions or discrete probability measures:
a function `α → ℝ≥0` such that the values have (infinite) sum `1`.
This file features the monadic structure of `pmf` and the Bernoulli distribution
## Implementation Notes
This file is not yet connected to the `measure_theory` library in any way.
At some point we need to define a `measure` from a `pmf` and prove the appropriate lemmas about
that.
## Tags
probability mass function, discrete probability measure, bernoulli distribution
-/
noncomputable theory
variables {α : Type*} {β : Type*} {γ : Type*}
open_locale classical big_operators nnreal ennreal
/-- A probability mass function, or discrete probability measures is a function `α → ℝ≥0` such that
the values have (infinite) sum `1`. -/
def {u} pmf (α : Type u) : Type u := { f : α → ℝ≥0 // has_sum f 1 }
namespace pmf
instance : has_coe_to_fun (pmf α) := ⟨λ p, α → ℝ≥0, λ p a, p.1 a⟩
@[ext] protected lemma ext : ∀ {p q : pmf α}, (∀ a, p a = q a) → p = q
| ⟨f, hf⟩ ⟨g, hg⟩ eq := subtype.eq $ funext eq
lemma has_sum_coe_one (p : pmf α) : has_sum p 1 := p.2
lemma summable_coe (p : pmf α) : summable p := (p.has_sum_coe_one).summable
@[simp] lemma tsum_coe (p : pmf α) : ∑' a, p a = 1 := p.has_sum_coe_one.tsum_eq
/-- The support of a `pmf` is the set where it is nonzero. -/
def support (p : pmf α) : set α := {a | p.1 a ≠ 0}
@[simp] lemma mem_support_iff (p : pmf α) (a : α) :
a ∈ p.support ↔ p a ≠ 0 := iff.rfl
/-- The pure `pmf` is the `pmf` where all the mass lies in one point.
The value of `pure a` is `1` at `a` and `0` elsewhere. -/
def pure (a : α) : pmf α := ⟨λ a', if a' = a then 1 else 0, has_sum_ite_eq _ _⟩
@[simp] lemma pure_apply (a a' : α) : pure a a' = (if a' = a then 1 else 0) := rfl
lemma mem_support_pure_iff (a a' : α) : a' ∈ (pure a).support ↔ a' = a :=
by simp
instance [inhabited α] : inhabited (pmf α) := ⟨pure (default α)⟩
lemma coe_le_one (p : pmf α) (a : α) : p a ≤ 1 :=
has_sum_le (by intro b; split_ifs; simp [h]; exact le_refl _) (has_sum_ite_eq a (p a)) p.2
protected lemma bind.summable (p : pmf α) (f : α → pmf β) (b : β) :
summable (λ a : α, p a * f a b) :=
begin
refine nnreal.summable_of_le (assume a, _) p.summable_coe,
suffices : p a * f a b ≤ p a * 1, { simpa },
exact mul_le_mul_of_nonneg_left ((f a).coe_le_one _) (p a).2
end
/-- The monadic bind operation for `pmf`. -/
def bind (p : pmf α) (f : α → pmf β) : pmf β :=
⟨λ b, ∑'a, p a * f a b,
begin
apply ennreal.has_sum_coe.1,
simp only [ennreal.coe_tsum (bind.summable p f _)],
rw [ennreal.summable.has_sum_iff, ennreal.tsum_comm],
simp [ennreal.tsum_mul_left, (ennreal.coe_tsum (f _).summable_coe).symm,
(ennreal.coe_tsum p.summable_coe).symm]
end⟩
@[simp] lemma bind_apply (p : pmf α) (f : α → pmf β) (b : β) : p.bind f b = ∑'a, p a * f a b :=
rfl
lemma coe_bind_apply (p : pmf α) (f : α → pmf β) (b : β) :
(p.bind f b : ℝ≥0∞) = ∑'a, p a * f a b :=
eq.trans (ennreal.coe_tsum $ bind.summable p f b) $ by simp
@[simp] lemma pure_bind (a : α) (f : α → pmf β) : (pure a).bind f = f a :=
have ∀ b a', ite (a' = a) 1 0 * f a' b = ite (a' = a) (f a b) 0, from
assume b a', by split_ifs; simp; subst h; simp,
by ext b; simp [this]
@[simp] lemma bind_pure (p : pmf α) : p.bind pure = p :=
have ∀ a a', (p a * ite (a' = a) 1 0) = ite (a = a') (p a') 0, from
assume a a', begin split_ifs; try { subst a }; try { subst a' }; simp * at * end,
by ext b; simp [this]
@[simp] lemma bind_bind (p : pmf α) (f : α → pmf β) (g : β → pmf γ) :
(p.bind f).bind g = p.bind (λ a, (f a).bind g) :=
begin
ext1 b,
simp only [ennreal.coe_eq_coe.symm, coe_bind_apply, ennreal.tsum_mul_left.symm,
ennreal.tsum_mul_right.symm],
rw [ennreal.tsum_comm],
simp [mul_assoc, mul_left_comm, mul_comm]
end
lemma bind_comm (p : pmf α) (q : pmf β) (f : α → β → pmf γ) :
p.bind (λ a, q.bind (f a)) = q.bind (λ b, p.bind (λ a, f a b)) :=
begin
ext1 b,
simp only [ennreal.coe_eq_coe.symm, coe_bind_apply, ennreal.tsum_mul_left.symm,
ennreal.tsum_mul_right.symm],
rw [ennreal.tsum_comm],
simp [mul_assoc, mul_left_comm, mul_comm]
end
protected lemma bind_on_support.summable (p : pmf α) (f : ∀ a ∈ p.support, pmf β) (b : β) :
summable (λ a : α, p a * if h : p a = 0 then 0 else f a h b) :=
begin
refine nnreal.summable_of_le (assume a, _) p.summable_coe,
split_ifs,
{ refine (mul_zero (p a)).symm ▸ le_of_eq h.symm },
{ suffices : p a * f a h b ≤ p a * 1, { simpa },
exact mul_le_mul_of_nonneg_left ((f a h).coe_le_one _) (p a).2 }
end
/-- Generalized version of `bind` allowing `f` to only be defined on the support of `p`.
`p.bind f` is equivalent to `p.bind_on_support (λ a _, f a)`, see `bind_on_support_eq_bind` -/
def bind_on_support (p : pmf α) (f : ∀ a ∈ p.support, pmf β) : pmf β :=
⟨λ b, ∑' a, p a * if h : p a = 0 then 0 else f a h b,
ennreal.has_sum_coe.1 begin
simp only [ennreal.coe_tsum (bind_on_support.summable p f _)],
rw [ennreal.summable.has_sum_iff, ennreal.tsum_comm],
simp only [ennreal.coe_mul, ennreal.coe_one, ennreal.tsum_mul_left],
have : ∑' (a : α), (p a : ennreal) = 1 :=
by simp only [←ennreal.coe_tsum p.summable_coe, ennreal.coe_one, tsum_coe],
refine trans (tsum_congr (λ a, _)) this,
split_ifs with h,
{ simp [h] },
{ simp [← ennreal.coe_tsum (f a h).summable_coe, (f a h).tsum_coe] }
end⟩
@[simp] lemma bind_on_support_apply (p : pmf α) (f : ∀ a ∈ p.support, pmf β) (b : β) :
p.bind_on_support f b = ∑' a, p a * if h : p a = 0 then 0 else f a h b := rfl
/-- `bind_on_support` reduces to `bind` if `f` doesn't depend on the additional hypothesis -/
@[simp] lemma bind_on_support_eq_bind (p : pmf α) (f : α → pmf β) :
p.bind_on_support (λ a _, f a) = p.bind f :=
begin
ext b,
simp only [p.bind_on_support_apply (λ a _, f a), p.bind_apply f,
dite_eq_ite, nnreal.coe_eq, mul_ite, mul_zero],
refine congr_arg _ (funext (λ a, _)),
split_ifs with h; simp [h],
end
lemma coe_bind_on_support_apply (p : pmf α) (f : ∀ a ∈ p.support, pmf β) (b : β) :
(p.bind_on_support f b : ℝ≥0∞) = ∑' a, p a * if h : p a = 0 then 0 else f a h b :=
by simp only [bind_on_support_apply, ennreal.coe_tsum (bind_on_support.summable p f b),
dite_cast, ennreal.coe_mul, ennreal.coe_zero]
@[simp] lemma mem_support_bind_on_support_iff (p : pmf α) (f : ∀ a ∈ p.support, pmf β) (b : β) :
b ∈ (p.bind_on_support f).support ↔ ∃ a (ha : p a ≠ 0), b ∈ (f a ha).support :=
begin
simp only [mem_support_iff, bind_on_support_apply,
tsum_ne_zero_iff (bind_on_support.summable p f b), mul_ne_zero_iff],
split; { rintro ⟨a, ha, haf⟩, refine ⟨a, ha, ne_of_eq_of_ne _ haf⟩, simp [ha], },
end
lemma bind_on_support_eq_zero_iff (p : pmf α) (f : ∀ a ∈ p.support, pmf β) (b : β) :
p.bind_on_support f b = 0 ↔ ∀ a (ha : p a ≠ 0), f a ha b = 0 :=
begin
simp only [bind_on_support_apply, tsum_eq_zero_iff (bind_on_support.summable p f b),
mul_eq_zero, or_iff_not_imp_left],
exact ⟨λ h a ha, trans (dif_neg ha).symm (h a ha), λ h a ha, trans (dif_neg ha) (h a ha)⟩,
end
@[simp] lemma pure_bind_on_support (a : α) (f : ∀ (a' : α) (ha : a' ∈ (pure a).support), pmf β) :
(pure a).bind_on_support f = f a ((mem_support_pure_iff a a).mpr rfl) :=
begin
refine pmf.ext (λ b, _),
simp only [nnreal.coe_eq, bind_on_support_apply, pure_apply],
refine trans (tsum_congr (λ a', _)) (tsum_ite_eq a _),
by_cases h : (a' = a); simp [h],
end
lemma bind_on_support_pure (p : pmf α) :
p.bind_on_support (λ a _, pure a) = p :=
by simp only [pmf.bind_pure, pmf.bind_on_support_eq_bind]
@[simp] lemma bind_on_support_bind_on_support (p : pmf α)
(f : ∀ a ∈ p.support, pmf β)
(g : ∀ (b ∈ (p.bind_on_support f).support), pmf γ) :
(p.bind_on_support f).bind_on_support g =
p.bind_on_support (λ a ha, (f a ha).bind_on_support
(λ b hb, g b ((p.mem_support_bind_on_support_iff f b).mpr ⟨a, ha, hb⟩))) :=
begin
refine pmf.ext (λ a, _),
simp only [ennreal.coe_eq_coe.symm, coe_bind_on_support_apply, ← tsum_dite_right,
ennreal.tsum_mul_left.symm, ennreal.tsum_mul_right.symm],
refine trans (ennreal.tsum_comm) (tsum_congr (λ a', _)),
split_ifs with h,
{ simp only [h, ennreal.coe_zero, zero_mul, tsum_zero] },
{ simp only [← ennreal.tsum_mul_left, ← mul_assoc],
refine tsum_congr (λ b, _),
split_ifs with h1 h2 h2,
any_goals { ring1 },
{ rw bind_on_support_eq_zero_iff at h1,
simp only [h1 a' h, ennreal.coe_zero, zero_mul, mul_zero] },
{ simp only [h2, ennreal.coe_zero, mul_zero, zero_mul] } }
end
lemma bind_on_support_comm (p : pmf α) (q : pmf β)
(f : ∀ (a ∈ p.support) (b ∈ q.support), pmf γ) :
p.bind_on_support (λ a ha, q.bind_on_support (f a ha)) =
q.bind_on_support (λ b hb, p.bind_on_support (λ a ha, f a ha b hb)) :=
begin
apply pmf.ext, rintro c,
simp only [ennreal.coe_eq_coe.symm, coe_bind_on_support_apply, ← tsum_dite_right,
ennreal.tsum_mul_left.symm, ennreal.tsum_mul_right.symm],
refine trans (ennreal.tsum_comm) (tsum_congr (λ b, tsum_congr (λ a, _))),
split_ifs with h1 h2 h2; ring,
end
/-- The functorial action of a function on a `pmf`. -/
def map (f : α → β) (p : pmf α) : pmf β := bind p (pure ∘ f)
lemma bind_pure_comp (f : α → β) (p : pmf α) : bind p (pure ∘ f) = map f p := rfl
lemma map_id (p : pmf α) : map id p = p := by simp [map]
lemma map_comp (p : pmf α) (f : α → β) (g : β → γ) : (p.map f).map g = p.map (g ∘ f) :=
by simp [map]
lemma pure_map (a : α) (f : α → β) : (pure a).map f = pure (f a) :=
by simp [map]
/-- The monadic sequencing operation for `pmf`. -/
def seq (f : pmf (α → β)) (p : pmf α) : pmf β := f.bind (λ m, p.bind $ λ a, pure (m a))
/-- Given a non-empty multiset `s` we construct the `pmf` which sends `a` to the fraction of
elements in `s` that are `a`. -/
def of_multiset (s : multiset α) (hs : s ≠ 0) : pmf α :=
⟨λ a, s.count a / s.card,
have ∑ a in s.to_finset, (s.count a : ℝ) / s.card = 1,
by simp [div_eq_inv_mul, finset.mul_sum.symm, (finset.sum_nat_cast _ _).symm, hs],
have ∑ a in s.to_finset, (s.count a : ℝ≥0) / s.card = 1,
by rw [← nnreal.eq_iff, nnreal.coe_one, ← this, nnreal.coe_sum]; simp,
begin
rw ← this,
apply has_sum_sum_of_ne_finset_zero,
simp {contextual := tt},
end⟩
/-- Given a finite type `α` and a function `f : α → ℝ≥0` with sum 1, we get a `pmf`. -/
def of_fintype [fintype α] (f : α → ℝ≥0) (h : ∑ x, f x = 1) : pmf α :=
⟨f, h ▸ has_sum_sum_of_ne_finset_zero (by simp)⟩
/-- Given a `f` with non-zero sum, we get a `pmf` by normalizing `f` by its `tsum` -/
def normalize (f : α → ℝ≥0) (hf0 : tsum f ≠ 0) : pmf α :=
⟨λ a, f a * (∑' x, f x)⁻¹,
(mul_inv_cancel hf0) ▸ has_sum.mul_right (∑' x, f x)⁻¹
(not_not.mp (mt tsum_eq_zero_of_not_summable hf0 : ¬¬summable f)).has_sum⟩
lemma normalize_apply {f : α → ℝ≥0} (hf0 : tsum f ≠ 0) (a : α) :
(normalize f hf0) a = f a * (∑' x, f x)⁻¹ := rfl
/-- Create new `pmf` by filtering on a set with non-zero measure and normalizing -/
def filter (p : pmf α) (s : set α) (h : ∃ a ∈ s, p a ≠ 0) : pmf α :=
pmf.normalize (s.indicator p) $ nnreal.tsum_indicator_ne_zero p.2.summable h
lemma filter_apply (p : pmf α) {s : set α} (h : ∃ a ∈ s, p a ≠ 0) {a : α} :
(p.filter s h) a = (s.indicator p a) * (∑' x, (s.indicator p) x)⁻¹ :=
by rw [filter, normalize_apply]
lemma filter_apply_eq_zero_of_not_mem (p : pmf α) {s : set α} (h : ∃ a ∈ s, p a ≠ 0)
{a : α} (ha : a ∉ s) : (p.filter s h) a = 0 :=
by rw [filter_apply, set.indicator_apply_eq_zero.mpr (λ ha', absurd ha' ha), zero_mul]
lemma filter_apply_eq_zero_iff (p : pmf α) {s : set α} (h : ∃ a ∈ s, p a ≠ 0) (a : α) :
(p.filter s h) a = 0 ↔ a ∉ (p.support ∩ s) :=
begin
rw [set.mem_inter_iff, p.mem_support_iff, not_and_distrib, not_not],
split; intro ha,
{ rw [filter_apply, mul_eq_zero] at ha,
refine ha.by_cases
(λ ha, (em (a ∈ s)).by_cases (λ h, or.inl ((set.indicator_apply_eq_zero.mp ha) h)) or.inr)
(λ ha, absurd (inv_eq_zero.1 ha) (nnreal.tsum_indicator_ne_zero p.2.summable h)) },
{ rw [filter_apply, set.indicator_apply_eq_zero.2 (λ h, ha.by_cases id (absurd h)), zero_mul] }
end
lemma filter_apply_ne_zero_iff (p : pmf α) {s : set α} (h : ∃ a ∈ s, p a ≠ 0) (a : α) :
(p.filter s h) a ≠ 0 ↔ a ∈ (p.support ∩ s) :=
by rw [← not_iff, filter_apply_eq_zero_iff, not_iff, not_not]
/-- A `pmf` which assigns probability `p` to `tt` and `1 - p` to `ff`. -/
def bernoulli (p : ℝ≥0) (h : p ≤ 1) : pmf bool :=
of_fintype (λ b, cond b p (1 - p)) (nnreal.eq $ by simp [h])
end pmf
|
1e4d8a593a46fb54b944fe309ae6ad7f4f1ea69b | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /src/Lean/Elab/MacroArgUtil.lean | 16e84d9dbc8180e3b30365ce096a33a3951e80d2 | [
"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 | 4,251 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Elab.Syntax
namespace Lean.Elab.Command
open Lean.Syntax
open Lean.Parser.Term hiding macroArg
open Lean.Parser.Command
/-- Convert `macro` arg into a `syntax` command item and a pattern element -/
partial def expandMacroArg (stx : TSyntax ``macroArg) : CommandElabM (TSyntax `stx × Term) := do
let (id?, id, stx) ← match (← liftMacroM <| expandMacros stx) with
| `(macroArg| $id:ident:$stx) => pure (some id, (id : Term), stx)
| `(macroArg| $stx:stx) => pure (none, (← `(x)), stx)
| _ => throwUnsupportedSyntax
mkSyntaxAndPat id? id stx
where
mkSyntaxAndPat (id? : Option Ident) (id : Term) (stx : TSyntax `stx) := do
let pat ← match stx with
| `(stx| $s:str) => pure ⟨mkNode `token_antiquot #[← liftMacroM <| strLitToPattern s, mkAtom "%", mkAtom "$", id]⟩
| `(stx| &$s:str) => pure ⟨mkNode `token_antiquot #[← liftMacroM <| strLitToPattern s, mkAtom "%", mkAtom "$", id]⟩
| `(stx| optional($stx)) => mkSplicePat `optional stx id "?"
| `(stx| many($stx)) => mkSplicePat `many stx id "*"
| `(stx| many1($stx)) => mkSplicePat `many stx id "*"
| `(stx| sepBy($stx, $sep:str $[, $stxsep]? $[, allowTrailingSep]?)) =>
mkSplicePat `sepBy stx id ((isStrLit? sep).get! ++ "*")
| `(stx| sepBy1($stx, $sep:str $[, $stxsep]? $[, allowTrailingSep]?)) =>
mkSplicePat `sepBy stx id ((isStrLit? sep).get! ++ "*")
-- NOTE: all `interpolatedStr(·)` reuse the same node kind
| `(stx| interpolatedStr(term)) => pure ⟨Syntax.mkAntiquotNode interpolatedStrKind id⟩
-- bind through withPosition
| `(stx| withPosition($stx)) =>
let (stx, pat) ← mkSyntaxAndPat id? id stx
let stx ← `(stx| withPosition($stx))
return (stx, pat)
| _ => match id? with
-- if there is a binding, we assume the user knows what they are doing
| some id => mkAntiquotNode stx id
-- otherwise `group` the syntax to enforce arity 1, e.g. for `noWs`
| none => return (← `(stx| group($stx)), (← mkAntiquotNode stx id))
pure (stx, pat)
mkSplicePat (kind : SyntaxNodeKind) (stx : TSyntax `stx) (id : Term) (suffix : String) : CommandElabM Term :=
return ⟨mkNullNode #[mkAntiquotSuffixSpliceNode kind (← mkAntiquotNode stx id) suffix]⟩
mkAntiquotNode : TSyntax `stx → Term → CommandElabM Term
| `(stx| $id:ident$[:$_]?), term => do
let kind ← match (← Elab.Term.resolveParserName id) with
| [(`Lean.Parser.ident, _)] => pure identKind
| [(`Lean.Parser.Term.ident, _)] => pure identKind
| [(`Lean.Parser.strLit, _)] => pure strLitKind
-- a syntax abbrev, assume kind == decl name
| [(c, _)] => pure c
| cs@(_ :: _ :: _) => throwError "ambiguous parser declaration {cs.map (·.1)}"
| [] =>
let id := id.getId.eraseMacroScopes
if Parser.isParserCategory (← getEnv) id then
return ⟨Syntax.mkAntiquotNode id term (isPseudoKind := true)⟩
else if (← Parser.isParserAlias id) then
pure <| (← Parser.getSyntaxKindOfParserAlias? id).getD Name.anonymous
else
throwError "unknown parser declaration/category/alias '{id}'"
pure ⟨Syntax.mkAntiquotNode kind term⟩
| stx, term => do
-- can't match against `` `(stx| ($stxs*)) `` as `*` is interpreted as the `stx` operator
if stx.raw.isOfKind ``Parser.Syntax.paren then
-- translate argument `v:(p1 ... pn)` where all but one `pi` produce zero nodes to
-- `v:pi` using that single `pi`
let nonNullaryNodes ← stx.raw[1].getArgs.filterM fun
| `(stx| $id:ident$[:$_]?) | `(stx| $id:ident($_)) => do
let info ← Parser.getParserAliasInfo id.getId
return info.stackSz? != some 0
| _ => return true
if let #[stx] := nonNullaryNodes then
return (← mkAntiquotNode ⟨stx⟩ term)
pure ⟨Syntax.mkAntiquotNode Name.anonymous term (isPseudoKind := true)⟩
end Lean.Elab.Command
|
5792419f1b7f77164760c453d124b469312d22d5 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/booltst.lean | b0359ead0cb2940e42f96c649e35c89cd72066e0 | [
"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 | 37 | lean | import data.bool
open bool
check ff
|
e7ac7ffb513327f261cfd9ff1377c3375fe94de7 | d1a52c3f208fa42c41df8278c3d280f075eb020c | /stage0/src/Lean/Parser/Tactic.lean | 33a3086d2e085ccb415c9ac0acc8ce5f9cda4cc3 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | cipher1024/lean4 | 6e1f98bb58e7a92b28f5364eb38a14c8d0aae393 | 69114d3b50806264ef35b57394391c3e738a9822 | refs/heads/master | 1,642,227,983,603 | 1,642,011,696,000 | 1,642,011,696,000 | 228,607,691 | 0 | 0 | Apache-2.0 | 1,576,584,269,000 | 1,576,584,268,000 | null | UTF-8 | Lean | false | false | 1,244 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Sebastian Ullrich
-/
import Lean.Parser.Term
namespace Lean
namespace Parser
namespace Tactic
builtin_initialize
register_parser_alias tacticSeq
@[builtinTacticParser] def «unknown» := leading_parser withPosition (ident >> errorAtSavedPos "unknown tactic" true)
@[builtinTacticParser] def nestedTactic := tacticSeqBracketed
/- Auxiliary parser for expanding `match` tactic -/
@[builtinTacticParser] def eraseAuxDiscrs := leading_parser:maxPrec "eraseAuxDiscrs!"
def matchRhs := Term.hole <|> Term.syntheticHole <|> tacticSeq
def matchAlts := Term.matchAlts (rhsParser := matchRhs)
@[builtinTacticParser] def «match» := leading_parser:leadPrec "match " >> optional Term.generalizingParam >> sepBy1 Term.matchDiscr ", " >> Term.optType >> " with " >> ppDedent matchAlts
@[builtinTacticParser] def introMatch := leading_parser nonReservedSymbol "intro " >> matchAlts
@[builtinTacticParser] def decide := leading_parser nonReservedSymbol "decide"
@[builtinTacticParser] def nativeDecide := leading_parser nonReservedSymbol "nativeDecide"
end Tactic
end Parser
end Lean
|
8e7a7baaaf3b117d9f88102cf25d5e5e446f9c58 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/algebra/ring/idempotents.lean | d6d32cfbe379fbc934e81075ac0acce141b65f83 | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 3,873 | lean | /-
Copyright (c) 2022 Christopher Hoskin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Christopher Hoskin
-/
import algebra.ring.basic
import algebra.group_power.basic
import tactic.nth_rewrite.default
/-!
# Idempotents
This file defines idempotents for an arbitary multiplication and proves some basic results,
including:
* `is_idempotent_elem.mul_of_commute`: In a semigroup, the product of two commuting idempotents is
an idempotent;
* `is_idempotent_elem.one_sub_iff`: In a (non-associative) ring, `p` is an idempotent if and only if
`1-p` is an idempotent.
* `is_idempotent_elem.pow_succ_eq`: In a monoid `p ^ (n+1) = p` for `p` an idempotent and `n` a
natural number.
## Tags
projection, idempotent
-/
variables {M N S M₀ M₁ R G G₀ : Type*}
variables [has_mul M] [monoid N] [semigroup S] [mul_zero_class M₀] [mul_one_class M₁]
[non_assoc_ring R] [group G] [cancel_monoid_with_zero G₀]
/--
An element `p` is said to be idempotent if `p * p = p`
-/
def is_idempotent_elem (p : M) : Prop := p * p = p
namespace is_idempotent_elem
lemma of_is_idempotent [is_idempotent M (*)] (a : M) : is_idempotent_elem a :=
is_idempotent.idempotent a
lemma eq {p : M} (h : is_idempotent_elem p) : p * p = p := h
lemma mul_of_commute {p q : S} (h : commute p q) (h₁ : is_idempotent_elem p)
(h₂ : is_idempotent_elem q) : is_idempotent_elem (p * q) :=
by rw [is_idempotent_elem, mul_assoc, ← mul_assoc q, ← h.eq, mul_assoc p, h₂.eq, ← mul_assoc, h₁.eq]
lemma zero : is_idempotent_elem (0 : M₀) := mul_zero _
lemma one : is_idempotent_elem (1 : M₁) := mul_one _
lemma one_sub {p : R} (h : is_idempotent_elem p) : is_idempotent_elem (1 - p) :=
by rw [is_idempotent_elem, mul_sub, mul_one, sub_mul, one_mul, h.eq, sub_self, sub_zero]
@[simp] lemma one_sub_iff {p : R} : is_idempotent_elem (1 - p) ↔ is_idempotent_elem p :=
⟨ λ h, sub_sub_cancel 1 p ▸ h.one_sub, is_idempotent_elem.one_sub ⟩
lemma pow {p : N} (n : ℕ) (h : is_idempotent_elem p) : is_idempotent_elem (p ^ n) :=
nat.rec_on n ((pow_zero p).symm ▸ one) (λ n ih, show p ^ n.succ * p ^ n.succ = p ^ n.succ,
by { nth_rewrite 2 ←h.eq, rw [←sq, ←sq, ←pow_mul, ←pow_mul'] })
lemma pow_succ_eq {p : N} (n : ℕ) (h : is_idempotent_elem p) : p ^ (n + 1) = p :=
nat.rec_on n ((nat.zero_add 1).symm ▸ pow_one p) (λ n ih, by rw [pow_succ, ih, h.eq])
@[simp] lemma iff_eq_one {p : G} : is_idempotent_elem p ↔ p = 1 :=
iff.intro (λ h, mul_left_cancel ((mul_one p).symm ▸ h.eq : p * p = p * 1)) (λ h, h.symm ▸ one)
@[simp] lemma iff_eq_zero_or_one {p : G₀} : is_idempotent_elem p ↔ p = 0 ∨ p = 1 :=
begin
refine iff.intro
(λ h, or_iff_not_imp_left.mpr (λ hp, _))
(λ h, h.elim (λ hp, hp.symm ▸ zero) (λ hp, hp.symm ▸ one)),
exact mul_left_cancel₀ hp (h.trans (mul_one p).symm)
end
/-! ### Instances on `subtype is_idempotent_elem` -/
section instances
instance : has_zero { p : M₀ // is_idempotent_elem p } := { zero := ⟨ 0, zero ⟩ }
@[simp] lemma coe_zero : ↑(0 : {p : M₀ // is_idempotent_elem p}) = (0 : M₀) := rfl
instance : has_one { p : M₁ // is_idempotent_elem p } := { one := ⟨ 1, one ⟩ }
@[simp] lemma coe_one : ↑(1 : { p : M₁ // is_idempotent_elem p }) = (1 : M₁) := rfl
instance : has_compl { p : R // is_idempotent_elem p } := ⟨λ p, ⟨1 - p, p.prop.one_sub⟩⟩
@[simp] lemma coe_compl (p : { p : R // is_idempotent_elem p }) : ↑(pᶜ) = (1 : R) - ↑p := rfl
@[simp] lemma compl_compl (p : {p : R // is_idempotent_elem p}) : pᶜᶜ = p :=
subtype.ext $ sub_sub_cancel _ _
@[simp] lemma zero_compl : (0 : {p : R // is_idempotent_elem p})ᶜ = 1 := subtype.ext $ sub_zero _
@[simp] lemma one_compl : (1 : {p : R // is_idempotent_elem p})ᶜ = 0 := subtype.ext $ sub_self _
end instances
end is_idempotent_elem
|
0fc5ee7d0b3b8b6fe3acb16304a5a639ad38c055 | 6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf | /src/game/world10/level16.lean | 15ac4dab1631019cb22c066477a83b6478093630 | [
"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 | 436 | lean | import game.world10.level15 -- hide
namespace mynat -- hide
/-
# Inequality world.
## Level 16: equivalence of two definitions of `<`
Now let's go the other way.
-/
/- Lemma :
For all naturals $a$ and $b$,
$$
\operatorname{succ}(a)\le b
\implies
a\le b\land\lnot(b\le a).$$
-/
lemma lt_aux_two (a b : mynat) : succ a ≤ b → a ≤ b ∧ ¬ (b ≤ a) :=
begin [nat_num_game]
end
/-
Now for the payoff.
-/
end mynat -- hide
|
bd7b151122e9ea7c396d3bcad12a1c5909c2cab9 | 682dc1c167e5900ba3168b89700ae1cf501cfa29 | /src/del/semantics/consistencyDEL.lean | 40e39c12731a745317a9d7bcbc3c20ed4a7ed69b | [] | no_license | paulaneeley/modal | 834558c87f55cdd6d8a29bb46c12f4d1de3239bc | ee5d149d4ecb337005b850bddf4453e56a5daf04 | refs/heads/master | 1,675,911,819,093 | 1,609,785,144,000 | 1,609,785,144,000 | 270,388,715 | 13 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 18,055 | lean | /-
Copyright (c) 2021 Paula Neeley. All rights reserved.
Author: Paula Neeley
Following the textbook "Dynamic Epistemic Logic" by
Hans van Ditmarsch, Wiebe van der Hoek, and Barteld Kooi
-/
import del.languageDEL del.syntax.syntaxDEL
import del.semantics.semanticsDEL del.syntax.syntaxlemmasDEL
import del.syntax.soundnessDEL order.zorn data.set.basic data.list.basic
local attribute [instance] classical.prop_decidable
variables {agents : Type}
open prfS5
open S5lemma
---------------------- Consistency ----------------------
def sem_cons (AX : ctx agents) (F : set (frame agents)) := ¬ global_sem_csq AX F form.bot
attribute [class] sem_cons
lemma sem_consS5 : sem_cons (∅ : ctx agents) equiv_class :=
begin
rw sem_cons,
rw global_sem_csq,
push_neg,
let f : frame agents :=
{ states := ℕ,
h := ⟨0⟩,
rel := λ a x y, x = y},
use f, let v := λ n x, true,
split,
intro a,
split,
intro x,
exact rfl,
split,
intros x y h1,
exact eq.symm h1,
intros x y z h1 h2,
exact (rfl.congr h2).mp h1,
use v,
split,
intros φ x h1,
exact false.elim h1,
let x := 42,
use x,
rw forces,
trivial
end
-- The S5 axiom system does not prove false
lemma nprfalse (hax : sem_cons (∅ : ctx agents) equiv_class) : ¬ prfS5 (∅ : ctx agents) form.bot :=
begin
have h1 : ¬ global_sem_csq (∅ : ctx agents) equiv_class form.bot → ¬ prfS5 ∅ form.bot,
{have h2 := soundnessS5,
rw ←not_imp_not at h2, exact h2},
apply h1, exact hax
end
lemma prnot_to_notpr (φ : form agents) (hax : sem_cons (∅ : ctx agents) equiv_class) :
prfS5 ∅ (¬φ) → ¬ prfS5 ∅ φ :=
begin
intro h1, by_contradiction h2,
exact absurd (mp (mp pl5 contra_equiv_false) (mp (mp pl4 h2) h1)) (nprfalse hax)
end
lemma pr_to_notprnot (φ : form agents) (hax : sem_cons (∅ : ctx agents) equiv_class) :
prfS5 ∅ φ → ¬ prfS5 ∅ (¬φ) :=
begin
have h1 := prnot_to_notpr φ hax,
rw ←not_imp_not at h1, intro h2, apply h1, rw not_not, exact h2,
end
-- finite conjunction of formulas
def fin_conj : list (form agents) → form agents
| [] := ¬form.bot
| (φ::φs) := φ & (fin_conj φs)
-- a few helper lemmas about finite conjunctions
lemma fin_conj_simp {Γ : ctx agents} : ∀ ψ : form agents, prfS5 Γ (¬fin_conj [ψ, ¬ψ]) :=
begin
intro ψ, exact (not_and_subst phi_and_true).mpr not_contra
end
lemma imp_conj_imp_imp {Γ : ctx agents} {φ ψ χ : form agents} {L : list (form agents)} :
prfS5 Γ ((fin_conj (φ::L)) ⊃ ψ) ↔ prfS5 Γ (fin_conj L ⊃ (φ ⊃ ψ)) :=
begin
split,
intro h, dsimp [fin_conj] at h, rw and_right_imp at h, exact h,
intro h, dsimp [fin_conj], rw and_right_imp, exact h
end
lemma fin_conj_cons_imp {Γ : ctx agents} {φ b : form agents} {L : list (form agents)} :
prfS5 Γ (fin_conj (φ::L) ⊃ (φ ⊃ b)) → prfS5 Γ (fin_conj L ⊃ (φ ⊃ b)) :=
begin
intro h, rw imp_conj_imp_imp at h, rw imp_imp_iff_imp at h, exact h, exact φ,
end
lemma fin_conj_append {Γ : ctx agents} {L L' : list (form agents)} :
prfS5 Γ ((fin_conj L' & fin_conj L) ↔ (fin_conj (L' ++ L))) :=
begin
induction L', rw fin_conj,
exact (mp (mp pl4 (cut (mp pl6 and_switch) (mp pl5 phi_and_true)))
(cut (mp pl6 phi_and_true) (mp pl5 and_switch))),
exact mp (mp pl4 (cut (mp pl5 and_commute) (imp_and_imp (mp pl5 L'_ih))))
(cut iden (cut (imp_and_imp (mp pl6 L'_ih)) (mp pl6 and_commute)))
end
lemma fin_conj_empty {L : list (form agents)}
(hax : sem_cons (∅ : ctx agents) equiv_class) :
L = [] → ¬ prfS5 ∅ (fin_conj L ⊃ ⊥) :=
begin
intro h1, subst h1,
by_contradiction h2,
exact absurd (mp h2 iden) (nprfalse hax)
end
lemma fin_conj_repeat_helper {θ : form agents} {L : list (form agents)}
(hax : sem_cons (∅ : ctx agents) equiv_class) :
(∀ ψ ∈ L, ψ = θ) → prfS5 ∅ (θ ⊃ fin_conj L) :=
begin
intros h1, induction L,
exact mp pl1 iden,
rw fin_conj, simp at *,
cases h1 with h1 h2,
subst h1,
exact cut (mp double_imp pl4) (imp_and_and_imp (mp (mp pl4 iden) (L_ih h2))),
end
lemma fin_conj_repeat {φ : form agents} {L : list (form agents)}
(hax : sem_cons (∅ : ctx agents) equiv_class) :
(∀ ψ ∈ L, ψ = ¬φ) → prfS5 ∅ (¬fin_conj L) → prfS5 ∅ φ :=
begin
intros h1 h2, induction L,
exact absurd (mp dne h2) (nprfalse hax),
repeat {rw fin_conj at *}, simp at *,
cases h1 with h1 h3,
have h5 := contrapos.mpr (fin_conj_repeat_helper hax h3),
subst h1,
exact (mp (mp pl3 (contrapos.mpr (cut h5 dne)))
(contrapos.mpr (cut ((demorgans.mp) (mp (mp pl6 (iff_not and_switch)) h2)) dne)))
end
lemma fin_conj_box2 {φ ψ : form agents} {a : agents} :
prfS5 ∅ (((K a φ) & K a ψ) ⊃ K a (φ & ψ)) :=
begin
exact (mp double_imp (cut2 pl6 (cut pl5 (cut (mp kdist (nec pl4)) kdist))))
end
lemma fin_conj_boxn {L : list (form agents)} {a : agents} :
prfS5 ∅ ((fin_conj (list.map (K a) L)) ⊃ (K a (fin_conj L))) :=
begin
induction L,
exact (mp pl1 (nec prtrue)),
exact (cut (imp_and_imp L_ih) fin_conj_box2)
end
lemma listempty {Γ : ctx agents} {L : list (form agents)} :
(∀ φ ∈ L, φ ∈ Γ) → Γ = ∅ → L = [] :=
begin
intros h1 h2,
by_contradiction h3,
have h4 := list.length_pos_of_ne_nil,
have h5 := list.exists_mem_of_length_pos,
cases h5 (h4 h3) with φ h5,
exact absurd (h1 φ h5) (set.eq_empty_iff_forall_not_mem.mp h2 φ)
end
-- Consistency for a finite set of formulas L
def fin_ax_consist (L : list (form agents)) := ¬ prfS5 ∅ (fin_conj L ⊃ ⊥)
-- Consistency for an arbitrary set of formulas Γ
def ax_consist (Γ : ctx agents) :=
∀ L : list (form agents), (∀ ψ ∈ L, ψ ∈ Γ) → fin_ax_consist L
-- Γ is maximally ax-consistent
def max_ax_consist (Γ : ctx agents) :=
ax_consist Γ ∧ (∀ Γ', Γ ⊂ Γ' → ¬ ax_consist Γ')
lemma max_imp_ax {Γ : ctx agents} : max_ax_consist Γ → ax_consist Γ :=
begin
intro h1, exact h1.left
end
-- Lemma 5 from class notes
lemma five_helper :
∀ Γ : ctx agents, ∀ φ : form agents, ∀ L : list (form agents), ∀ b : form agents,
(∀ ψ ∈ L, ψ ∈ Γ ∨ ψ = φ) → prfS5 ∅ (fin_conj L ⊃ b) → ∃ L',
(∀ ψ ∈ L', ψ ∈ Γ) ∧ prfS5 ∅ (fin_conj L' ⊃ (φ ⊃ b)) :=
begin
intros Γ φ L b h1 h2,
revert b,
induction L,
{intros b h2, existsi ([] : list (form agents)), split,
intros ψ h3, exfalso, apply list.not_mem_nil ψ h3,
exact imp_if_imp_imp h2},
{intros b h2,
have h1a : ∀ (ψ : form agents), ψ ∈ L_tl → ψ ∈ Γ ∨ ψ = φ,
{intros ψ h2, apply h1 ψ (list.mem_cons_of_mem L_hd h2)},
have h1b: L_hd ∈ Γ ∨ L_hd = φ,
{apply h1 L_hd, left, refl},
cases h1b,
have h3 := and_right_imp.mp h2,
cases L_ih h1a (L_hd ⊃ b) h3 with L' ih, existsi (L_hd::L' : list (form agents)),
cases ih, split, intros ψ h4, cases h4,
subst h4, exact h1b,
apply ih_left ψ h4, rw imp_shift at ih_right,
rw ←imp_conj_imp_imp at ih_right, exact ih_right,
have h3 : prfS5 ∅ (fin_conj (L_hd::L_tl) ⊃ b),
exact h2, exact b,
have h4 : prfS5 ∅ (fin_conj L_tl ⊃ (φ ⊃ b)),
from eq.subst h1b (and_right_imp.mp) h2,
cases L_ih h1a (φ ⊃ b) h4 with L' ih,
cases ih, existsi (L' : list (form agents)), split,
exact ih_left, exact imp_imp_iff_imp.mp ih_right}
end
lemma five :
∀ Γ : ctx agents, ∀ φ : form agents, ¬ ax_consist (Γ ∪ φ) → ∃ L',
(∀ ψ ∈ L', ψ ∈ Γ) ∧ prfS5 ∅ (fin_conj L' ⊃ ¬φ) :=
begin
intro Γ, intro φ, intro h1, rw ax_consist at h1,
push_neg at h1,
cases h1 with L h1,
have h2 : (∀ ψ ∈ L, ψ ∈ Γ ∨ ψ = φ) → prfS5 ∅ (fin_conj L ⊃ ⊥) → ∃ L',
(∀ ψ ∈ L', ψ ∈ Γ) ∧ prfS5 ∅ (fin_conj L' ⊃ (φ ⊃ ⊥)), from five_helper Γ φ L ⊥,
cases h1,
have h3 : (∀ (ψ : form agents), ψ ∈ L → ψ ∈ Γ ∨ ψ = φ),
{intros ψ this, exact or.swap (h1_left ψ this)},
apply h2 h3, rw fin_ax_consist at h1_right, rw not_not at h1_right,
exact h1_right,
end
-- Lemma 6 from class notes
lemma six_helper (Γ : ctx agents) (h : ax_consist Γ) :
max_ax_consist Γ → ∀ φ : form agents, φ ∈ Γ ∨ (¬φ) ∈ Γ :=
begin
intros h1 φ, rw or_iff_not_and_not, by_contradiction h2,
cases h2 with h2l h2r,
cases h1 with h1l h1r, clear h,
have h2 := h1r (Γ ∪ φ),
have h3 : ¬ax_consist (Γ ∪ ¬φ),
{apply h1r (Γ ∪ ¬φ), from set.ssubset_insert h2r},
have h5 : ¬ax_consist (Γ ∪ φ),
{apply h2, from set.ssubset_insert h2l},
have h6 := five Γ φ _, have h7 := five Γ (¬φ) _,
cases h6 with L' h6, cases h7 with L h7, cases h6 with h6l h6r,
cases h7 with h7l h7r,
have h8 := imp_and_and_imp (mp (mp pl4 h6r) h7r),
have h12 := cut (mp pl6 fin_conj_append) (cut h8 (mp pl5 contra_equiv_false)),
have h13 : (∀ (ψ : form agents), ψ ∈ L' ++ L → ψ ∈ Γ),
intro ψ, intro h13,
rw list.mem_append at h13, cases h13, exact h6l ψ h13, exact h7l ψ h13,
exact absurd h12 (h1l (L' ++ L) h13),
exact h3,
exact h5,
end
lemma six (Γ : ctx agents) (h : ax_consist Γ) :
max_ax_consist Γ ↔ ∀ φ, (φ ∈ Γ ∨ (¬φ) ∈ Γ) ∧ ¬(φ ∈ Γ ∧ (¬φ) ∈ Γ) :=
begin
simp, split,
intro h1, intro φ,
split, exact six_helper Γ h h1 φ,
{rw ←not_and, by_contradiction h2,
cases h2 with h2 h3,
specialize h ([φ, ¬φ]), simp at *,
have h4 : (∀ (ψ : form agents), ψ = φ ∨ ψ = ¬φ → ψ ∈ Γ),
{intros ψ h4, cases h4, subst h4, exact h2, subst h4, exact h3},
have h5 : prfS5 ∅ (¬fin_conj [φ, ¬φ]), from fin_conj_simp φ,
exact absurd h5 (h h4)},
intro h1, split, exact h,
intros Γ' h2,
have h3 : Γ ⊆ Γ' ∧ ¬ Γ' ⊆ Γ, from h2,
cases h3,
rw set.not_subset at h3_right,
apply (exists.elim h3_right), simp, intros ψ h4 h5,
specialize h1 ψ, cases h1,
cases h1_left,
apply absurd h1_left h5,
have h6 : (¬ψ) ∈ Γ', from set.mem_of_subset_of_mem h3_left h1_left,
rw ax_consist,
push_neg,
existsi ([ψ,¬ψ] : list (form agents)),
simp, split, intros φ h7, cases h7, subst h7, exact h4,
subst h7, exact h6, rw fin_ax_consist, rw not_not,
exact fin_conj_simp ψ
end
-- Exercise 1 from class notes
lemma ex1help {Γ : ctx agents} {φ : form agents} {L L' : list (form agents)} :
(∀ ψ ∈ L, ψ ∈ Γ) → prfS5 ∅ (fin_conj L ⊃ φ) → (∀ ψ ∈ L', ψ ∈ (insert φ Γ))
→ ∃ L'' : list (form agents), (∀ ψ ∈ L'', ψ ∈ Γ) ∧ prfS5 ∅ (fin_conj L'' ⊃ fin_conj L') :=
begin
intros h1 h2 h3, induction L',
existsi ([] : list (form agents)),
split,
intros ψ h4, exact false.elim h4,
exact iden,
simp at *, cases h3 with h3 h4,
cases L'_ih h4 with L'' L'_ih,
cases L'_ih with ih1 ih2,
cases h3,
existsi (L''++L : list (form agents)),
split,
simp at *, intros ψ h2,
cases h2 with h2 h5,
exact ih1 ψ h2,
exact h1 ψ h5,
subst h3,
exact (cut (mp pl6 fin_conj_append) (cut (mp pl5 and_switch) (imp_and_and_imp (mp (mp pl4 h2) ih2)))),
existsi (L'_hd::L'' : list (form agents)),
split, simp at *, split, exact h3, exact ih1,
exact imp_and_imp ih2
end
lemma exercise1 {Γ : ctx agents} {φ : form agents} {L : list (form agents)} :
max_ax_consist Γ → (∀ ψ ∈ L, ψ ∈ Γ) → prfS5 ∅ (fin_conj L ⊃ φ) → φ ∈ Γ :=
begin
intros h1 h2 h3,
by_contradiction h4,
cases h1 with h1 h5,
specialize h5 (Γ ∪ {φ}),
simp at h5,
specialize h5 (set.ssubset_insert h4),
rw ax_consist at h5,
push_neg at h5,
cases h5 with L' h5,
cases h5 with h5 h6,
rw fin_ax_consist at h6,
rw not_not at h6,
have h7 := ex1help h2 h3 h5,
cases h7 with L'' h7,
cases h7 with h7 h8,
apply h1 L'' h7,
exact cut h8 h6
end
lemma max_dn (Γ : ctx agents) (h : max_ax_consist Γ) (φ : form agents) :
φ ∈ Γ ↔ (¬¬φ) ∈ Γ :=
begin
split, intro h1,
have h2 : (∀ ψ ∈ [φ], ψ ∈ Γ) → prfS5 ∅ (fin_conj [φ] ⊃ (¬¬φ)) → (¬¬φ) ∈ Γ, from exercise1 h,
simp at *, apply h2, exact h1,
exact (cut (mp pl5 phi_and_true) dni),
intro h1,
have h2 : (∀ ψ ∈ [¬¬φ], ψ ∈ Γ) → prfS5 ∅ (fin_conj [¬¬φ] ⊃ φ) → φ ∈ Γ, from exercise1 h,
simp at *, apply h2, exact h1,
exact (cut (mp pl5 phi_and_true) dne),
end
lemma max_boxdn (Γ : ctx agents) (h : max_ax_consist Γ) (φ : form agents) (a : agents) :
(¬K a φ) ∈ Γ → (¬ (K a (¬¬φ))) ∈ Γ :=
begin
intro h1,
have h2 : ∀ a, (∀ ψ ∈ [¬ K a φ], ψ ∈ Γ) → prfS5 ∅ (fin_conj [¬ K a φ] ⊃ (¬ (K a (¬¬φ)))) → (¬ (K a (¬¬φ))) ∈ Γ,
from λ a, exercise1 h,
simp at *,
apply h2 a, exact h1, clear h2,
exact (cut (mp pl5 phi_and_true) (mp pl5 box_dn)),
end
lemma max_notiff (Γ : ctx agents) (h : max_ax_consist Γ) (φ : form agents) :
φ ∉ Γ ↔ (¬φ) ∈ Γ :=
begin
split, intro h1,
have h2 := max_imp_ax h,
have h3 := six_helper Γ h2 h,
specialize h3 φ, cases h3, exact absurd h3 h1, exact h3,
intro h1,
have h2 := max_imp_ax h,
have h3 := six Γ h2,
cases h3, specialize h3_mp h (¬φ), simp at *,
cases h3_mp with mp1 mp2,
have h4 := max_dn Γ h φ,
rw ←not_iff_not at h4, apply h4.mpr, exact mp2 h1
end
lemma max_imp_1 {Γ : ctx agents} {φ ψ : form agents} :
max_ax_consist Γ → (φ ∈ Γ → ψ ∈ Γ) → (φ ⊃ ψ) ∈ Γ :=
begin
intros h1 h2, rw imp_iff_not_or at h2,
cases h2,
have h3 : (∀ χ ∈ [¬φ], χ ∈ Γ) → prfS5 ∅ (fin_conj [¬φ] ⊃ (φ ⊃ ψ)) → (φ ⊃ ψ) ∈ Γ, from exercise1 h1,
simp at *, apply h3,
exact (max_notiff Γ h1 φ).mp h2,
exact cut (mp pl5 phi_and_true) (and_right_imp.mp exfalso),
have h3 : (∀ χ ∈ [ψ], χ ∈ Γ) → prfS5 ∅ (fin_conj [ψ] ⊃ (φ ⊃ ψ)) → (φ ⊃ ψ) ∈ Γ, from exercise1 h1,
simp at *,
apply h3, exact h2, exact (cut (mp pl5 phi_and_true) pl1),
end
lemma max_imp_2 {Γ : ctx agents} {φ ψ : form agents} :
max_ax_consist Γ → (φ ⊃ ψ) ∈ Γ → φ ∈ Γ → ψ ∈ Γ :=
begin
intros h1 h2 h3,
have h4 : (∀ χ ∈ [φ, (φ ⊃ ψ)], χ ∈ Γ) → prfS5 ∅ (fin_conj [φ, (φ ⊃ ψ)] ⊃ ψ) → ψ ∈ Γ, from exercise1 h1,
simp at *, apply h4, intros χ h5, cases h5, subst h5, exact h3, subst h5, exact h2,
exact and_right_imp.mpr (mp pl5 phi_and_true)
end
lemma max_conj_1 {Γ : ctx agents} {φ ψ : form agents} :
max_ax_consist Γ → (φ ∈ Γ ∧ ψ ∈ Γ) → (φ & ψ) ∈ Γ :=
begin
intros h1 h2,
have h3 : (∀ χ ∈ [φ], χ ∈ Γ) → prfS5 ∅ (fin_conj [φ] ⊃ (ψ ⊃ (φ & ψ))) → (ψ ⊃ (φ & ψ)) ∈ Γ,
from exercise1 h1,
simp at *,
apply max_imp_2 h1,
exact (h3 h2.left) (cut (mp pl5 phi_and_true) pl4), exact h2.right
end
lemma max_conj_2 {Γ : ctx agents} {φ ψ : form agents} :
max_ax_consist Γ → (φ & ψ) ∈ Γ → φ ∈ Γ :=
begin
intros h1 h2,
have h3 : (∀ χ ∈ [(φ & ψ)], χ ∈ Γ) → prfS5 ∅ (fin_conj [(φ & ψ)] ⊃ φ) → φ ∈ Γ,
from exercise1 h1,
simp at *, apply h3, exact h2,
exact (cut (mp pl5 phi_and_true) pl5)
end
lemma max_conj_3 {Γ : ctx agents} {φ ψ : form agents} :
max_ax_consist Γ → (φ & ψ) ∈ Γ → ψ ∈ Γ :=
begin
intros h1 h2,
have h3 : (∀ χ ∈ [(φ & ψ)], χ ∈ Γ) → prfS5 ∅ (fin_conj [(φ & ψ)] ⊃ ψ) → ψ ∈ Γ,
from exercise1 h1,
simp at *, apply h3, exact h2,
exact (cut (mp pl5 phi_and_true) pl6)
end
-- Γ is maximally AX-consistent iff it is AX-consistent and for
-- every AX-consistent set Γ', if Γ ⊆ Γ', then Γ = Γ'
lemma max_equiv (Γ : ctx agents) : max_ax_consist Γ ↔ ax_consist Γ ∧
∀ Γ', ax_consist Γ' → Γ ⊆ Γ' → Γ = Γ' :=
begin
split,
{intro h1, split, exact h1.left,
intros Γ' h2 h3, rw set.subset.antisymm_iff, split, exact h3,
by_contradiction h4, exact h1.right Γ' (and.intro h3 h4) h2},
intro h1, split, exact h1.left,
intros Γ' h2, by_contradiction h3,
rw set.ssubset_def at h2, apply h2.right,
rw (h1.right Γ' h3) h2.left
end
open zorn
-- Lemma: if c is a chain of sets, L is a list of elements such that
-- every element in L is in Union(c), then there is an element m in c such that every
-- element of L is in m.
lemma lindenhelper (c : set (ctx agents)) (h : c.nonempty) (h1 : chain (⊆) c) (L : list (form agents)) :
(∀ φ ∈ L, φ ∈ ⋃₀(c)) → ∃ m ∈ c, (∀ ψ ∈ L, ψ ∈ m) :=
begin
intro h2,
induction L, simp,
rw ←set.nonempty_def, exact h,
have h2b : ∀ φ, φ ∈ L_tl → φ ∈ ⋃₀(c),
{intros φ h3, apply h2, exact set.mem_union_right (eq φ) h3},
cases L_ih h2b with m ih,
cases ih with h3 ih,
specialize h2 L_hd,
simp at h2,
cases h2 with m' h2,
cases h2 with h2 h4,
existsi (m' ∪ m : ctx agents),
have h5 : m' ∪ m ∈ c,
{have h6 := chain.total_of_refl h1 h3 h2,
cases h6,
exact (eq.substr (set.union_eq_self_of_subset_right h6) h2),
exact (eq.substr (set.union_eq_self_of_subset_left h6) h3)},
existsi (h5 : m' ∪ m ∈ c),
intros ψ h6, cases h6,
subst h6, exact set.mem_union_left m h4,
exact set.mem_union_right m' (ih ψ h6)
end
lemma lindenbaum (Γ : ctx agents) (hax : ax_consist Γ) :
∃ Γ', max_ax_consist Γ' ∧ Γ ⊆ Γ' :=
begin
let P := { Γ'' | Γ'' ⊇ Γ ∧ ax_consist Γ''},
have h : ∀ c ⊆ P, chain (⊆) c → c.nonempty → ∃ub ∈ P, ∀ s ∈ c, s ⊆ ub,
{intros c h2 h3 h4, use ⋃₀(c),
have h5 := lindenhelper c h4 h3,
repeat {split},
cases h4 with Γ'' h4,
have h6 := set.mem_of_mem_of_subset h4 h2,
cases h6 with h6 h7,
apply set.subset_sUnion_of_subset c Γ'' h6 h4,
intros L h6,
cases h5 L h6 with m h5,
cases h5 with h7 h5,
cases (set.mem_of_mem_of_subset h7 h2) with h8 h9,
apply h9, exact h5,
intros s h7, exact set.subset_sUnion_of_mem h7},
have h1 : Γ ∈ P,
split,
exact set.subset.rfl,
exact hax,
cases zorn_subset₀ P h Γ h1 with Γ' h2,
cases h2 with h2 h3,
cases h3 with h3 h4,
use Γ', split, rw max_equiv, split, apply h2.2,
intros m h5 h6, symmetry, apply h4 m, split,
apply set.subset.trans h2.1 h6,
exact h5, exact h6, apply h2.1
end
-- Corollary 8 from class notes
lemma max_ax_exists (hax : sem_cons (∅ : ctx agents) equiv_class) : ∃ Γ : ctx agents, max_ax_consist Γ :=
begin
have h1 : ax_consist ∅,
{intro L, intro h2, rw fin_ax_consist,
have h3 := listempty h2, have this : ∅ = ∅, refl,
specialize h3 this, subst h3, by_contradiction h4,
apply nprfalse hax, exact mp dne h4},
have h2 := lindenbaum ∅ h1,
cases h2 with Γ h2, cases h2 with h2 h3, existsi (Γ : ctx agents), apply h2
end |
a6193b8aec5fae7be5ea6b1b5f70542d1bb5dbe5 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/1606.lean | f01e8acbce4c055992b8ca7f96166a24bbe122ef | [
"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 | 551 | lean | example : True := by
skip
skip --< should complain about misleading indentation
trivial
macro "frobnicate" : tactic => `(tactic| skip)
example : True := by
conv =>
skip
frobnicate --< should not parse frobnicate as a tactic
trivial
-- check error message without default handler for conv tactics
declare_syntax_cat item
syntax "valid_item" : item
macro "block" "=>" sepByIndentSemicolon(item) : tactic => `(tactic| skip)
example : True := by
block =>
valid_item
frobnicate --< should not parse frobnicate as a tactic
|
27b5bdc52343f36c214c4e3ade0b9f70c9a41705 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/calcBug.lean | a6e4ccbdbd0793c7d8b07207d6ab111fb50afd1e | [
"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 | 304 | lean | namespace Hidden
open Nat
theorem zero_add (n : Nat) : 0 + n = n :=
Nat.recOn (motive := fun x => 0 + x = x)
n
rfl
(fun (n : Nat) (ih : 0 + n = n) =>
show 0 + succ n = succ n from
calc
0 + succ n = succ (0 + n) := rfl
_ = succ n := by rw [ih])
end Hidden
|
ca9ac67a3a4247c839cf24b4268de2e65bf83bca | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/data/pfun.lean | 071550fef62c904bf1fdf83c0f4b49561dd4f288 | [
"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,682 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Jeremy Avigad, Simon Hudon
-/
import data.part
import data.rel
/-!
# Partial functions
This file defines partial functions. Partial functions are like functions, except they can also be
"undefined" on some inputs. We define them as functions `α → part β`.
## Definitions
* `pfun α β`: Type of partial functions from `α` to `β`. Defined as `α → part β` and denoted
`α →. β`.
* `pfun.dom`: Domain of a partial function. Set of values on which it is defined. Not to be confused
with the domain of a function `α → β`, which is a type (`α` presently).
* `pfun.fn`: Evaluation of a partial function. Takes in an element and a proof it belongs to the
partial function's `dom`.
* `pfun.as_subtype`: Returns a partial function as a function from its `dom`.
* `pfun.eval_opt`: Returns a partial function with a decidable `dom` as a function `a → option β`.
* `pfun.lift`: Turns a function into a partial function.
* `pfun.restrict`: Restriction of a partial function to a smaller `dom`.
* `pfun.res`: Turns a function into a partial function with a prescribed domain.
* `pfun.fix` : First return map of a partial function `f : α →. β ⊕ α`.
* `pfun.fix_induction`: A recursion principle for `pfun.fix`.
### Partial functions as relations
Partial functions can be considered as relations, so we specialize some `rel` definitions to `pfun`:
* `pfun.image`: Image of a set under a partial function.
* `pfun.ran`: Range of a partial function.
* `pfun.preimage`: Preimage of a set under a partial function.
* `pfun.core`: Core of a set under a partial function.
* `pfun.graph`: Graph of a partial function `a →. β`as a `set (α × β)`.
* `pfun.graph'`: Graph of a partial function `a →. β`as a `rel α β`.
### `pfun α` as a monad
Monad operations:
* `pfun.pure`: The monad `pure` function, the constant `x` function.
* `pfun.bind`: The monad `bind` function, pointwise `part.bind`
* `pfun.map`: The monad `map` function, pointwise `part.map`.
-/
/-- `pfun α β`, or `α →. β`, is the type of partial functions from
`α` to `β`. It is defined as `α → part β`. -/
def pfun (α β : Type*) := α → part β
infixr ` →. `:25 := pfun
namespace pfun
variables {α β γ : Type*}
instance : inhabited (α →. β) := ⟨λ a, part.none⟩
/-- The domain of a partial function -/
def dom (f : α →. β) : set α := {a | (f a).dom}
theorem mem_dom (f : α →. β) (x : α) : x ∈ dom f ↔ ∃ y, y ∈ f x :=
by simp [dom, part.dom_iff_mem]
theorem dom_eq (f : α →. β) : dom f = {x | ∃ y, y ∈ f x} :=
set.ext (mem_dom f)
/-- Evaluate a partial function -/
def fn (f : α →. β) (x) (h : dom f x) : β := (f x).get h
/-- Evaluate a partial function to return an `option` -/
def eval_opt (f : α →. β) [D : decidable_pred (∈ dom f)] (x : α) : option β :=
@part.to_option _ _ (D x)
/-- Partial function extensionality -/
theorem ext' {f g : α →. β}
(H1 : ∀ a, a ∈ dom f ↔ a ∈ dom g)
(H2 : ∀ a p q, f.fn a p = g.fn a q) : f = g :=
funext $ λ a, part.ext' (H1 a) (H2 a)
theorem ext {f g : α →. β} (H : ∀ a b, b ∈ f a ↔ b ∈ g a) : f = g :=
funext $ λ a, part.ext (H a)
/-- Turns a partial function into a function out of its domain. -/
def as_subtype (f : α →. β) (s : f.dom) : β := f.fn s s.2
/-- The type of partial functions `α →. β` is equivalent to
the type of pairs `(p : α → Prop, f : subtype p → β)`. -/
def equiv_subtype : (α →. β) ≃ (Σ p : α → Prop, subtype p → β) :=
⟨λ f, ⟨λ a, (f a).dom, as_subtype f⟩,
λ f x, ⟨f.1 x, λ h, f.2 ⟨x, h⟩⟩,
λ f, funext $ λ a, part.eta _,
λ ⟨p, f⟩, by dsimp; congr; funext a; cases a; refl⟩
theorem as_subtype_eq_of_mem {f : α →. β} {x : α} {y : β} (fxy : y ∈ f x) (domx : x ∈ f.dom) :
f.as_subtype ⟨x, domx⟩ = y :=
part.mem_unique (part.get_mem _) fxy
/-- Turn a total function into a partial function. -/
protected def lift (f : α → β) : α →. β := λ a, part.some (f a)
instance : has_coe (α → β) (α →. β) := ⟨pfun.lift⟩
@[simp] theorem lift_eq_coe (f : α → β) : pfun.lift f = f := rfl
@[simp] theorem coe_val (f : α → β) (a : α) :
(f : α →. β) a = part.some (f a) := rfl
/-- Graph of a partial function `f` as the set of pairs `(x, f x)` where `x` is in the domain of
`f`. -/
def graph (f : α →. β) : set (α × β) := {p | p.2 ∈ f p.1}
/-- Graph of a partial function as a relation. `x` and `y` are related iff `f x` is defined and
"equals" `y`. -/
def graph' (f : α →. β) : rel α β := λ x y, y ∈ f x
/-- The range of a partial function is the set of values
`f x` where `x` is in the domain of `f`. -/
def ran (f : α →. β) : set β := {b | ∃ a, b ∈ f a}
/-- Restrict a partial function to a smaller domain. -/
def restrict (f : α →. β) {p : set α} (H : p ⊆ f.dom) : α →. β :=
λ x, (f x).restrict (x ∈ p) (@H x)
@[simp]
theorem mem_restrict {f : α →. β} {s : set α} (h : s ⊆ f.dom) (a : α) (b : β) :
b ∈ f.restrict h a ↔ a ∈ s ∧ b ∈ f a :=
by simp [restrict]
/-- Turns a function into a partial function with a prescribed domain. -/
def res (f : α → β) (s : set α) : α →. β :=
(pfun.lift f).restrict s.subset_univ
theorem mem_res (f : α → β) (s : set α) (a : α) (b : β) :
b ∈ res f s a ↔ (a ∈ s ∧ f a = b) :=
by simp [res, @eq_comm _ b]
theorem res_univ (f : α → β) : pfun.res f set.univ = f :=
rfl
theorem dom_iff_graph (f : α →. β) (x : α) : x ∈ f.dom ↔ ∃ y, (x, y) ∈ f.graph :=
part.dom_iff_mem
theorem lift_graph {f : α → β} {a b} : (a, b) ∈ (f : α →. β).graph ↔ f a = b :=
show (∃ (h : true), f a = b) ↔ f a = b, by simp
/-- The monad `pure` function, the total constant `x` function -/
protected def pure (x : β) : α →. β := λ _, part.some x
/-- The monad `bind` function, pointwise `part.bind` -/
def bind (f : α →. β) (g : β → α →. γ) : α →. γ :=
λ a, (f a).bind (λ b, g b a)
/-- The monad `map` function, pointwise `part.map` -/
def map (f : β → γ) (g : α →. β) : α →. γ :=
λ a, (g a).map f
instance : monad (pfun α) :=
{ pure := @pfun.pure _,
bind := @pfun.bind _,
map := @pfun.map _ }
instance : is_lawful_monad (pfun α) :=
{ bind_pure_comp_eq_map := λ β γ f x, funext $ λ a, part.bind_some_eq_map _ _,
id_map := λ β f, by funext a; dsimp [functor.map, pfun.map]; cases f a; refl,
pure_bind := λ β γ x f, funext $ λ a, part.bind_some.{u_1 u_2} _ (f x),
bind_assoc := λ β γ δ f g k,
funext $ λ a, (f a).bind_assoc (λ b, g b a) (λ b, k b a) }
theorem pure_defined (p : set α) (x : β) : p ⊆ (@pfun.pure α _ x).dom := p.subset_univ
theorem bind_defined {α β γ} (p : set α) {f : α →. β} {g : β → α →. γ}
(H1 : p ⊆ f.dom) (H2 : ∀ x, p ⊆ (g x).dom) : p ⊆ (f >>= g).dom :=
λ a ha, (⟨H1 ha, H2 _ ha⟩ : (f >>= g).dom a)
/-- First return map. Transforms a partial function `f : α →. β ⊕ α` into the partial function
`α →. β` which sends `a : α` to the first value in `β` it hits by iterating `f`, if such a value
exists. By abusing notation to illustrate, either `f a` is in the `β` part of `β ⊕ α` (in which
case `f.fix a` returns `f a`), or it is undefined (in which case `f.fix a` is undefined as well), or
it is in the `α` part of `β ⊕ α` (in which case we repeat the procedure, so `f.fix a` will return
`f.fix (f a)`). -/
def fix (f : α →. β ⊕ α) : α →. β := λ a,
part.assert (acc (λ x y, sum.inr x ∈ f y) a) $ λ h,
@well_founded.fix_F _ (λ x y, sum.inr x ∈ f y) _
(λ a IH, part.assert (f a).dom $ λ hf,
by cases e : (f a).get hf with b a';
[exact part.some b, exact IH _ ⟨hf, e⟩])
a h
theorem dom_of_mem_fix {f : α →. β ⊕ α} {a : α} {b : β}
(h : b ∈ f.fix a) : (f a).dom :=
let ⟨h₁, h₂⟩ := part.mem_assert_iff.1 h in
by rw well_founded.fix_F_eq at h₂; exact h₂.fst.fst
theorem mem_fix_iff {f : α →. β ⊕ α} {a : α} {b : β} :
b ∈ f.fix a ↔ sum.inl b ∈ f a ∨ ∃ a', sum.inr a' ∈ f a ∧ b ∈ f.fix a' :=
⟨λ h, let ⟨h₁, h₂⟩ := part.mem_assert_iff.1 h in
begin
rw well_founded.fix_F_eq at h₂,
simp at h₂,
cases h₂ with h₂ h₃,
cases e : (f a).get h₂ with b' a'; simp [e] at h₃,
{ subst b', refine or.inl ⟨h₂, e⟩ },
{ exact or.inr ⟨a', ⟨_, e⟩, part.mem_assert _ h₃⟩ }
end,
λ h, begin
simp [fix],
rcases h with ⟨h₁, h₂⟩ | ⟨a', h, h₃⟩,
{ refine ⟨⟨_, λ y h', _⟩, _⟩,
{ injection part.mem_unique ⟨h₁, h₂⟩ h' },
{ rw well_founded.fix_F_eq, simp [h₁, h₂] } },
{ simp [fix] at h₃, cases h₃ with h₃ h₄,
refine ⟨⟨_, λ y h', _⟩, _⟩,
{ injection part.mem_unique h h' with e,
exact e ▸ h₃ },
{ cases h with h₁ h₂,
rw well_founded.fix_F_eq, simp [h₁, h₂, h₄] } }
end⟩
/-- A recursion principle for `pfun.fix`. -/
@[elab_as_eliminator] def fix_induction
{f : α →. β ⊕ α} {b : β} {C : α → Sort*} {a : α} (h : b ∈ f.fix a)
(H : ∀ a, b ∈ f.fix a →
(∀ a', b ∈ f.fix a' → sum.inr a' ∈ f a → C a') → C a) : C a :=
begin
replace h := part.mem_assert_iff.1 h,
have := h.snd, revert this,
induction h.fst with a ha IH, intro h₂,
refine H a (part.mem_assert_iff.2 ⟨⟨_, ha⟩, h₂⟩)
(λ a' ha' fa', _),
have := (part.mem_assert_iff.1 ha').snd,
exact IH _ fa' ⟨ha _ fa', this⟩ this
end
end pfun
namespace pfun
variables {α β : Type*} (f : α →. β)
/-- Image of a set under a partial function. -/
def image (s : set α) : set β := f.graph'.image s
lemma image_def (s : set α) : f.image s = {y | ∃ x ∈ s, y ∈ f x} := rfl
lemma mem_image (y : β) (s : set α) : y ∈ f.image s ↔ ∃ x ∈ s, y ∈ f x :=
iff.rfl
lemma image_mono {s t : set α} (h : s ⊆ t) : f.image s ⊆ f.image t :=
rel.image_mono _ h
lemma image_inter (s t : set α) : f.image (s ∩ t) ⊆ f.image s ∩ f.image t :=
rel.image_inter _ s t
lemma image_union (s t : set α) : f.image (s ∪ t) = f.image s ∪ f.image t :=
rel.image_union _ s t
/-- Preimage of a set under a partial function. -/
def preimage (s : set β) : set α := rel.image (λ x y, x ∈ f y) s
lemma preimage_def (s : set β) : f.preimage s = {x | ∃ y ∈ s, y ∈ f x} := rfl
lemma mem_preimage (s : set β) (x : α) : x ∈ f.preimage s ↔ ∃ y ∈ s, y ∈ f x :=
iff.rfl
lemma preimage_subset_dom (s : set β) : f.preimage s ⊆ f.dom :=
λ x ⟨y, ys, fxy⟩, part.dom_iff_mem.mpr ⟨y, fxy⟩
lemma preimage_mono {s t : set β} (h : s ⊆ t) : f.preimage s ⊆ f.preimage t :=
rel.preimage_mono _ h
lemma preimage_inter (s t : set β) : f.preimage (s ∩ t) ⊆ f.preimage s ∩ f.preimage t :=
rel.preimage_inter _ s t
lemma preimage_union (s t : set β) : f.preimage (s ∪ t) = f.preimage s ∪ f.preimage t :=
rel.preimage_union _ s t
lemma preimage_univ : f.preimage set.univ = f.dom :=
by ext; simp [mem_preimage, mem_dom]
/-- Core of a set `s : set β` with respect to a partial function `f : α →. β`. Set of all `a : α`
such that `f a ∈ s`, if `f a` is defined. -/
def core (s : set β) : set α := f.graph'.core s
lemma core_def (s : set β) : f.core s = {x | ∀ y, y ∈ f x → y ∈ s} := rfl
lemma mem_core (x : α) (s : set β) : x ∈ f.core s ↔ (∀ y, y ∈ f x → y ∈ s) :=
iff.rfl
lemma compl_dom_subset_core (s : set β) : f.domᶜ ⊆ f.core s :=
λ x hx y fxy,
absurd ((mem_dom f x).mpr ⟨y, fxy⟩) hx
lemma core_mono {s t : set β} (h : s ⊆ t) : f.core s ⊆ f.core t :=
rel.core_mono _ h
lemma core_inter (s t : set β) : f.core (s ∩ t) = f.core s ∩ f.core t :=
rel.core_inter _ s t
lemma mem_core_res (f : α → β) (s : set α) (t : set β) (x : α) :
x ∈ (res f s).core t ↔ x ∈ s → f x ∈ t :=
by simp [mem_core, mem_res]
section
open_locale classical
lemma core_res (f : α → β) (s : set α) (t : set β) : (res f s).core t = sᶜ ∪ f ⁻¹' t :=
by { ext, rw mem_core_res, by_cases h : x ∈ s; simp [h] }
end
lemma core_restrict (f : α → β) (s : set β) : (f : α →. β).core s = s.preimage f :=
by ext x; simp [core_def]
lemma preimage_subset_core (f : α →. β) (s : set β) : f.preimage s ⊆ f.core s :=
λ x ⟨y, ys, fxy⟩ y' fxy',
have y = y', from part.mem_unique fxy fxy',
this ▸ ys
lemma preimage_eq (f : α →. β) (s : set β) : f.preimage s = f.core s ∩ f.dom :=
set.eq_of_subset_of_subset
(set.subset_inter (f.preimage_subset_core s) (f.preimage_subset_dom s))
(λ x ⟨xcore, xdom⟩,
let y := (f x).get xdom in
have ys : y ∈ s, from xcore _ (part.get_mem _),
show x ∈ f.preimage s, from ⟨(f x).get xdom, ys, part.get_mem _⟩)
lemma core_eq (f : α →. β) (s : set β) : f.core s = f.preimage s ∪ f.domᶜ :=
by rw [preimage_eq, set.union_distrib_right, set.union_comm (dom f), set.compl_union_self,
set.inter_univ, set.union_eq_self_of_subset_right (f.compl_dom_subset_core s)]
lemma preimage_as_subtype (f : α →. β) (s : set β) :
f.as_subtype ⁻¹' s = subtype.val ⁻¹' f.preimage s :=
begin
ext x,
simp only [set.mem_preimage, set.mem_set_of_eq, pfun.as_subtype, pfun.mem_preimage],
show f.fn (x.val) _ ∈ s ↔ ∃ y ∈ s, y ∈ f (x.val),
exact iff.intro
(λ h, ⟨_, h, part.get_mem _⟩)
(λ ⟨y, ys, fxy⟩,
have f.fn x.val x.property ∈ f x.val := part.get_mem _,
part.mem_unique fxy this ▸ ys)
end
end pfun
|
f47bd516dfaa8daca344cd0ffb89a2d4a79dd7fa | b19a1b7dc79c802247fdce4c04708e070863b4d2 | /namespaces.lean | e5b795398852aa07b0a0039824746c71655f4b88 | [] | no_license | utanapishtim/promethazine | 99a1e80311fb20251a54ba78a534b23852b88c40 | 08a6f9bd6dd08feb3df8d4697e19ffc8d333b249 | refs/heads/master | 1,653,595,504,487 | 1,480,129,933,000 | 1,480,129,933,000 | 74,801,596 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 293 | lean | namespace foo
constant A : Type
constant a : A
constant f : A → A
definition fa : A := f a
namespace bar
definition ffa : A := f (f a)
check fa
check ffa
end bar
check fa
check bar.ffa
end foo
check foo.fa
check foo.bar.ffa
open foo
check fa
check bar.ffa
|
dec5b4ef968e1aab2adc77dcc87c2910f5a037ed | 76df16d6c3760cb415f1294caee997cc4736e09b | /rosette-benchmarks-4/jitterbug/jitterbug/lean/src/bv/helper.lean | db7a4eeac9a15934aac467bde2be09dd7c2a69da | [
"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 | 711 | lean | import data.int.modeq
import tactic.basic
import tactic.linarith.frontend
import tactic.ring_exp
namespace bv.helper
open nat
lemma pow2_succ (n : ℕ) : 2^(succ n) = 2 * 2^n :=
by simp [pow_succ, mul_comm]
lemma two_pos : 2 > 0 :=
dec_trivial
lemma pow2_pos (n : ℕ) : 2^n > 0 :=
pow_pos two_pos n
@[simp]
lemma bit_div_two (b : bool) (n : ℕ) : bit b n / 2 = n :=
by rw [← nat.div2_val, nat.div2_bit]
@[simp]
lemma bit_mod_two (b : bool) (n : ℕ) : bit b n % 2 = cond b 1 0 :=
by rw [nat.mod_two_of_bodd, nat.bodd_bit]
lemma int.sub_mod_self (a b : ℤ) :
(a - b) % b = a % b :=
calc a - b
≡ a - 0 [ZMOD b] : by apply int.modeq.modeq_sub; simp [int.modeq]
... = a : by simp
end bv.helper
|
0834c38aec3e352e60ce792115456b3219a015bf | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/order/filter/ultrafilter.lean | b2f2b93c935aac3da1a133dd25b358190e9279a9 | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 16,696 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Jeremy Avigad, Yury Kudryashov
-/
import order.filter.cofinite
import order.zorn
/-!
# Ultrafilters
An ultrafilter is a minimal (maximal in the set order) proper filter.
In this file we define
* `ultrafilter.of`: an ultrafilter that is less than or equal to a given filter;
* `ultrafilter`: subtype of ultrafilters;
* `ultrafilter.pure`: `pure x` as an `ultrafiler`;
* `ultrafilter.map`, `ultrafilter.bind`, `ultrafilter.comap` : operations on ultrafilters;
* `hyperfilter`: the ultrafilter extending the cofinite filter.
-/
universes u v
variables {α : Type u} {β : Type v} {γ : Type*}
open set filter function
open_locale classical filter
/-- An ultrafilter is a minimal (maximal in the set order) proper filter. -/
@[protect_proj]
structure ultrafilter (α : Type*) extends filter α :=
(ne_bot' : ne_bot to_filter)
(le_of_le : ∀ g, filter.ne_bot g → g ≤ to_filter → to_filter ≤ g)
namespace ultrafilter
variables {f g : ultrafilter α} {s t : set α} {p q : α → Prop}
instance : has_coe_t (ultrafilter α) (filter α) := ⟨ultrafilter.to_filter⟩
instance : has_mem (set α) (ultrafilter α) := ⟨λ s f, s ∈ (f : filter α)⟩
lemma unique (f : ultrafilter α) {g : filter α} (h : g ≤ f)
(hne : ne_bot g . tactic.apply_instance) : g = f :=
le_antisymm h $ f.le_of_le g hne h
instance ne_bot (f : ultrafilter α) : ne_bot (f : filter α) := f.ne_bot'
@[simp, norm_cast] lemma mem_coe : s ∈ (f : filter α) ↔ s ∈ f := iff.rfl
lemma coe_injective : injective (coe : ultrafilter α → filter α)
| ⟨f, h₁, h₂⟩ ⟨g, h₃, h₄⟩ rfl := by congr
lemma eq_of_le {f g : ultrafilter α} (h : (f : filter α) ≤ g) : f = g :=
coe_injective (g.unique h)
@[simp, norm_cast] lemma coe_le_coe {f g : ultrafilter α} : (f : filter α) ≤ g ↔ f = g :=
⟨λ h, eq_of_le h, λ h, h ▸ le_rfl⟩
@[simp, norm_cast] lemma coe_inj : (f : filter α) = g ↔ f = g := coe_injective.eq_iff
@[ext] lemma ext ⦃f g : ultrafilter α⦄ (h : ∀ s, s ∈ f ↔ s ∈ g) : f = g :=
coe_injective $ filter.ext h
lemma le_of_inf_ne_bot (f : ultrafilter α) {g : filter α} (hg : ne_bot (↑f ⊓ g)) : ↑f ≤ g :=
le_of_inf_eq (f.unique inf_le_left hg)
lemma le_of_inf_ne_bot' (f : ultrafilter α) {g : filter α} (hg : ne_bot (g ⊓ f)) : ↑f ≤ g :=
f.le_of_inf_ne_bot $ by rwa inf_comm
@[simp] lemma compl_not_mem_iff : sᶜ ∉ f ↔ s ∈ f :=
⟨λ hsc, le_principal_iff.1 $ f.le_of_inf_ne_bot
⟨λ h, hsc $ mem_of_eq_bot$ by rwa compl_compl⟩, compl_not_mem⟩
@[simp] lemma frequently_iff_eventually : (∃ᶠ x in f, p x) ↔ ∀ᶠ x in f, p x :=
compl_not_mem_iff
alias frequently_iff_eventually ↔ _root_.filter.frequently.eventually _
lemma compl_mem_iff_not_mem : sᶜ ∈ f ↔ s ∉ f := by rw [← compl_not_mem_iff, compl_compl]
lemma diff_mem_iff (f : ultrafilter α) : s \ t ∈ f ↔ s ∈ f ∧ t ∉ f :=
inter_mem_iff.trans $ and_congr iff.rfl compl_mem_iff_not_mem
/-- If `sᶜ ∉ f ↔ s ∈ f`, then `f` is an ultrafilter. The other implication is given by
`ultrafilter.compl_not_mem_iff`. -/
def of_compl_not_mem_iff (f : filter α) (h : ∀ s, sᶜ ∉ f ↔ s ∈ f) : ultrafilter α :=
{ to_filter := f,
ne_bot' := ⟨λ hf, by simpa [hf] using h⟩,
le_of_le := λ g hg hgf s hs, (h s).1 $ λ hsc, by exactI compl_not_mem hs (hgf hsc) }
lemma nonempty_of_mem (hs : s ∈ f) : s.nonempty := nonempty_of_mem hs
lemma ne_empty_of_mem (hs : s ∈ f) : s ≠ ∅ := (nonempty_of_mem hs).ne_empty
@[simp] lemma empty_not_mem : ∅ ∉ f := empty_not_mem f
lemma mem_or_compl_mem (f : ultrafilter α) (s : set α) : s ∈ f ∨ sᶜ ∈ f :=
or_iff_not_imp_left.2 compl_mem_iff_not_mem.2
protected lemma em (f : ultrafilter α) (p : α → Prop) :
(∀ᶠ x in f, p x) ∨ ∀ᶠ x in f, ¬p x :=
f.mem_or_compl_mem {x | p x}
lemma eventually_or : (∀ᶠ x in f, p x ∨ q x) ↔ (∀ᶠ x in f, p x) ∨ ∀ᶠ x in f, q x :=
⟨λ H, (f.em p).imp_right $ λ hp, (H.and hp).mono $ λ x ⟨hx, hnx⟩, hx.resolve_left hnx,
λ H, H.elim (λ hp, hp.mono $ λ x, or.inl) (λ hp, hp.mono $ λ x, or.inr)⟩
lemma union_mem_iff : s ∪ t ∈ f ↔ s ∈ f ∨ t ∈ f := eventually_or
lemma eventually_not : (∀ᶠ x in f, ¬p x) ↔ ¬∀ᶠ x in f, p x := compl_mem_iff_not_mem
lemma eventually_imp : (∀ᶠ x in f, p x → q x) ↔ (∀ᶠ x in f, p x) → ∀ᶠ x in f, q x :=
by simp only [imp_iff_not_or, eventually_or, eventually_not]
lemma finite_sUnion_mem_iff {s : set (set α)} (hs : s.finite) : ⋃₀ s ∈ f ↔ ∃t∈s, t ∈ f :=
finite.induction_on hs (by simp) $ λ a s ha hs his,
by simp [union_mem_iff, his, or_and_distrib_right, exists_or_distrib]
lemma finite_bUnion_mem_iff {is : set β} {s : β → set α} (his : is.finite) :
(⋃i∈is, s i) ∈ f ↔ ∃i∈is, s i ∈ f :=
by simp only [← sUnion_image, finite_sUnion_mem_iff (his.image s), bex_image_iff]
/-- Pushforward for ultrafilters. -/
def map (m : α → β) (f : ultrafilter α) : ultrafilter β :=
of_compl_not_mem_iff (map m f) $ λ s, @compl_not_mem_iff _ f (m ⁻¹' s)
@[simp, norm_cast] lemma coe_map (m : α → β) (f : ultrafilter α) :
(map m f : filter β) = filter.map m ↑f := rfl
@[simp] lemma mem_map {m : α → β} {f : ultrafilter α} {s : set β} :
s ∈ map m f ↔ m ⁻¹' s ∈ f := iff.rfl
@[simp] lemma map_id (f : ultrafilter α) : f.map id = f := coe_injective map_id
@[simp] lemma map_id' (f : ultrafilter α) : f.map (λ x, x) = f := map_id _
@[simp] lemma map_map (f : ultrafilter α) (m : α → β) (n : β → γ) :
(f.map m).map n = f.map (n ∘ m) :=
coe_injective map_map
/-- The pullback of an ultrafilter along an injection whose range is large with respect to the given
ultrafilter. -/
def comap {m : α → β} (u : ultrafilter β) (inj : injective m)
(large : set.range m ∈ u) : ultrafilter α :=
{ to_filter := comap m u,
ne_bot' := u.ne_bot'.comap_of_range_mem large,
le_of_le := λ g hg hgu, by { resetI,
simp only [← u.unique (map_le_iff_le_comap.2 hgu), comap_map inj, le_rfl] } }
@[simp] lemma mem_comap {m : α → β} (u : ultrafilter β) (inj : injective m)
(large : set.range m ∈ u) {s : set α} :
s ∈ u.comap inj large ↔ m '' s ∈ u :=
mem_comap_iff inj large
@[simp, norm_cast] lemma coe_comap {m : α → β} (u : ultrafilter β) (inj : injective m)
(large : set.range m ∈ u) : (u.comap inj large : filter α) = filter.comap m u := rfl
@[simp] lemma comap_id (f : ultrafilter α) (h₀ : injective (id : α → α) := injective_id)
(h₁ : range id ∈ f := by { rw range_id, exact univ_mem}) :
f.comap h₀ h₁ = f :=
coe_injective comap_id
@[simp] lemma comap_comap (f : ultrafilter γ) {m : α → β} {n : β → γ} (inj₀ : injective n)
(large₀ : range n ∈ f) (inj₁ : injective m) (large₁ : range m ∈ f.comap inj₀ large₀)
(inj₂ : injective (n ∘ m) := inj₀.comp inj₁)
(large₂ : range (n ∘ m) ∈ f := by { rw range_comp, exact image_mem_of_mem_comap large₀ large₁ }) :
(f.comap inj₀ large₀).comap inj₁ large₁ = f.comap inj₂ large₂ :=
coe_injective comap_comap
/-- The principal ultrafilter associated to a point `x`. -/
instance : has_pure ultrafilter :=
⟨λ α a, of_compl_not_mem_iff (pure a) $ λ s, by simp⟩
@[simp] lemma mem_pure {a : α} {s : set α} : s ∈ (pure a : ultrafilter α) ↔ a ∈ s := iff.rfl
@[simp] lemma coe_pure (a : α) : ↑(pure a : ultrafilter α) = (pure a : filter α) := rfl
@[simp] lemma map_pure (m : α → β) (a : α) : map m (pure a) = pure (m a) := rfl
@[simp] lemma comap_pure {m : α → β} (a : α) (inj : injective m) (large) :
comap (pure $ m a) inj large = pure a :=
coe_injective $ comap_pure.trans $
by rw [coe_pure, ←principal_singleton, ←image_singleton, preimage_image_eq _ inj]
lemma pure_injective : injective (pure : α → ultrafilter α) :=
λ a b h, filter.pure_injective (congr_arg ultrafilter.to_filter h : _)
instance [inhabited α] : inhabited (ultrafilter α) := ⟨pure default⟩
instance [nonempty α] : nonempty (ultrafilter α) := nonempty.map pure infer_instance
lemma eq_pure_of_finite_mem (h : s.finite) (h' : s ∈ f) : ∃ x ∈ s, (f : filter α) = pure x :=
begin
rw ← bUnion_of_singleton s at h',
rcases (ultrafilter.finite_bUnion_mem_iff h).mp h' with ⟨a, has, haf⟩,
use [a, has],
change (f : filter α) = (pure a : ultrafilter α),
rw [ultrafilter.coe_inj, ← ultrafilter.coe_le_coe],
change (f : filter α) ≤ pure a,
rwa [← principal_singleton, le_principal_iff]
end
lemma eq_pure_of_finite [finite α] (f : ultrafilter α) : ∃ a, (f : filter α) = pure a :=
(eq_pure_of_finite_mem finite_univ univ_mem).imp $ λ a ⟨_, ha⟩, ha
/-- Monadic bind for ultrafilters, coming from the one on filters
defined in terms of map and join.-/
def bind (f : ultrafilter α) (m : α → ultrafilter β) : ultrafilter β :=
of_compl_not_mem_iff (bind ↑f (λ x, ↑(m x))) $ λ s,
by simp only [mem_bind', mem_coe, ← compl_mem_iff_not_mem, compl_set_of, compl_compl]
instance has_bind : has_bind ultrafilter := ⟨@ultrafilter.bind⟩
instance functor : functor ultrafilter := { map := @ultrafilter.map }
instance monad : monad ultrafilter := { map := @ultrafilter.map }
section
local attribute [instance] filter.monad filter.is_lawful_monad
instance is_lawful_monad : is_lawful_monad ultrafilter :=
{ id_map := assume α f, coe_injective (id_map f.1),
pure_bind := assume α β a f, coe_injective (pure_bind a (coe ∘ f)),
bind_assoc := assume α β γ f m₁ m₂, coe_injective (filter_eq rfl),
bind_pure_comp_eq_map := assume α β f x, coe_injective (bind_pure_comp_eq_map f x.1) }
end
/-- The ultrafilter lemma: Any proper filter is contained in an ultrafilter. -/
lemma exists_le (f : filter α) [h : ne_bot f] : ∃u : ultrafilter α, ↑u ≤ f :=
begin
let τ := {f' // ne_bot f' ∧ f' ≤ f},
let r : τ → τ → Prop := λt₁ t₂, t₂.val ≤ t₁.val,
haveI := nonempty_of_ne_bot f,
let top : τ := ⟨f, h, le_refl f⟩,
let sup : Π(c:set τ), is_chain r c → τ :=
λc hc, ⟨⨅a:{a:τ // a ∈ insert top c}, a.1,
infi_ne_bot_of_directed
(is_chain.directed $ hc.insert $ λ ⟨b, _, hb⟩ _ _, or.inl hb)
(assume ⟨⟨a, ha, _⟩, _⟩, ha),
infi_le_of_le ⟨top, mem_insert _ _⟩ le_rfl⟩,
have : ∀ c (hc : is_chain r c) a (ha : a ∈ c), r a (sup c hc),
from assume c hc a ha, infi_le_of_le ⟨a, mem_insert_of_mem _ ha⟩ le_rfl,
have : (∃ (u : τ), ∀ (a : τ), r u a → r a u),
from exists_maximal_of_chains_bounded (assume c hc, ⟨sup c hc, this c hc⟩)
(assume f₁ f₂ f₃ h₁ h₂, le_trans h₂ h₁),
cases this with uτ hmin,
exact ⟨⟨uτ.val, uτ.property.left, assume g hg₁ hg₂,
hmin ⟨g, hg₁, le_trans hg₂ uτ.property.right⟩ hg₂⟩, uτ.property.right⟩
end
alias exists_le ← _root_.filter.exists_ultrafilter_le
/-- Construct an ultrafilter extending a given filter.
The ultrafilter lemma is the assertion that such a filter exists;
we use the axiom of choice to pick one. -/
noncomputable def of (f : filter α) [ne_bot f] : ultrafilter α :=
classical.some (exists_le f)
lemma of_le (f : filter α) [ne_bot f] : ↑(of f) ≤ f := classical.some_spec (exists_le f)
lemma of_coe (f : ultrafilter α) : of ↑f = f :=
coe_inj.1 $ f.unique (of_le f)
lemma exists_ultrafilter_of_finite_inter_nonempty (S : set (set α))
(cond : ∀ T : finset (set α), (↑T : set (set α)) ⊆ S → (⋂₀ (↑T : set (set α))).nonempty) :
∃ F : ultrafilter α, S ⊆ F.sets :=
begin
rsufficesI ⟨F, cond, hF⟩ : ∃ F : filter α, ne_bot F ∧ S ⊆ F.sets,
{ obtain ⟨G : ultrafilter α, h1 : ↑G ≤ F⟩ := exists_le F,
exact ⟨G, λ T hT, h1 (hF hT)⟩ },
use filter.generate S,
refine ⟨_, λ T hT, filter.generate_sets.basic hT⟩,
rw ← forall_mem_nonempty_iff_ne_bot,
intros T hT,
rcases mem_generate_iff.mp hT with ⟨A, h1, h2, h3⟩,
let B := set.finite.to_finset h2,
rw (show A = ↑B, by simp) at *,
rcases cond B h1 with ⟨x, hx⟩,
exact ⟨x, h3 hx⟩,
end
end ultrafilter
namespace filter
variables {f : filter α} {s : set α} {a : α}
open ultrafilter
protected lemma ne_bot.le_pure_iff (hf : f.ne_bot) : f ≤ pure a ↔ f = pure a :=
⟨ultrafilter.unique (pure a), le_of_eq⟩
lemma mem_iff_ultrafilter : s ∈ f ↔ ∀ g : ultrafilter α, ↑g ≤ f → s ∈ g :=
begin
refine ⟨λ hf g hg, hg hf, λ H, by_contra $ λ hf, _⟩,
set g : filter ↥sᶜ := comap coe f,
haveI : ne_bot g := comap_ne_bot_iff_compl_range.2 (by simpa [compl_set_of]),
simpa using H ((of g).map coe) (map_le_iff_le_comap.mpr (of_le g))
end
lemma le_iff_ultrafilter {f₁ f₂ : filter α} : f₁ ≤ f₂ ↔ ∀ g : ultrafilter α, ↑g ≤ f₁ → ↑g ≤ f₂ :=
⟨λ h g h₁, h₁.trans h, λ h s hs, mem_iff_ultrafilter.2 $ λ g hg, h g hg hs⟩
/-- A filter equals the intersection of all the ultrafilters which contain it. -/
lemma supr_ultrafilter_le_eq (f : filter α) :
(⨆ (g : ultrafilter α) (hg : ↑g ≤ f), (g : filter α)) = f :=
eq_of_forall_ge_iff $ λ f', by simp only [supr_le_iff, ← le_iff_ultrafilter]
/-- The `tendsto` relation can be checked on ultrafilters. -/
lemma tendsto_iff_ultrafilter (f : α → β) (l₁ : filter α) (l₂ : filter β) :
tendsto f l₁ l₂ ↔ ∀ g : ultrafilter α, ↑g ≤ l₁ → tendsto f g l₂ :=
by simpa only [tendsto_iff_comap] using le_iff_ultrafilter
lemma exists_ultrafilter_iff {f : filter α} : (∃ (u : ultrafilter α), ↑u ≤ f) ↔ ne_bot f :=
⟨λ ⟨u, uf⟩, ne_bot_of_le uf, λ h, @exists_ultrafilter_le _ _ h⟩
lemma forall_ne_bot_le_iff {g : filter α} {p : filter α → Prop} (hp : monotone p) :
(∀ f : filter α, ne_bot f → f ≤ g → p f) ↔ ∀ f : ultrafilter α, ↑f ≤ g → p f :=
begin
refine ⟨λ H f hf, H f f.ne_bot hf, _⟩,
introsI H f hf hfg,
exact hp (of_le f) (H _ ((of_le f).trans hfg))
end
section hyperfilter
variables (α) [infinite α]
/-- The ultrafilter extending the cofinite filter. -/
noncomputable def hyperfilter : ultrafilter α := ultrafilter.of cofinite
variable {α}
lemma hyperfilter_le_cofinite : ↑(hyperfilter α) ≤ @cofinite α :=
ultrafilter.of_le cofinite
@[simp] lemma bot_ne_hyperfilter : (⊥ : filter α) ≠ hyperfilter α :=
(by apply_instance : ne_bot ↑(hyperfilter α)).1.symm
theorem nmem_hyperfilter_of_finite {s : set α} (hf : s.finite) : s ∉ hyperfilter α :=
λ hy, compl_not_mem hy $ hyperfilter_le_cofinite hf.compl_mem_cofinite
alias nmem_hyperfilter_of_finite ← _root_.set.finite.nmem_hyperfilter
theorem compl_mem_hyperfilter_of_finite {s : set α} (hf : set.finite s) :
sᶜ ∈ hyperfilter α :=
compl_mem_iff_not_mem.2 hf.nmem_hyperfilter
alias compl_mem_hyperfilter_of_finite ← _root_.set.finite.compl_mem_hyperfilter
theorem mem_hyperfilter_of_finite_compl {s : set α} (hf : set.finite sᶜ) :
s ∈ hyperfilter α :=
compl_compl s ▸ hf.compl_mem_hyperfilter
end hyperfilter
end filter
namespace ultrafilter
open filter
variables {m : α → β} {s : set α} {g : ultrafilter β}
lemma comap_inf_principal_ne_bot_of_image_mem (h : m '' s ∈ g) :
(filter.comap m g ⊓ 𝓟 s).ne_bot :=
filter.comap_inf_principal_ne_bot_of_image_mem g.ne_bot h
/-- Ultrafilter extending the inf of a comapped ultrafilter and a principal ultrafilter. -/
noncomputable def of_comap_inf_principal (h : m '' s ∈ g) : ultrafilter α :=
@of _ (filter.comap m g ⊓ 𝓟 s) (comap_inf_principal_ne_bot_of_image_mem h)
lemma of_comap_inf_principal_mem (h : m '' s ∈ g) : s ∈ of_comap_inf_principal h :=
begin
let f := filter.comap m g ⊓ 𝓟 s,
haveI : f.ne_bot := comap_inf_principal_ne_bot_of_image_mem h,
have : s ∈ f := mem_inf_of_right (mem_principal_self s),
exact le_def.mp (of_le _) s this
end
lemma of_comap_inf_principal_eq_of_map (h : m '' s ∈ g) :
(of_comap_inf_principal h).map m = g :=
begin
let f := filter.comap m g ⊓ 𝓟 s,
haveI : f.ne_bot := comap_inf_principal_ne_bot_of_image_mem h,
apply eq_of_le,
calc filter.map m (of f) ≤ filter.map m f : map_mono (of_le _)
... ≤ (filter.map m $ filter.comap m g) ⊓ filter.map m (𝓟 s) : map_inf_le
... = (filter.map m $ filter.comap m g) ⊓ (𝓟 $ m '' s) : by rw map_principal
... ≤ g ⊓ (𝓟 $ m '' s) : inf_le_inf_right _ map_comap_le
... = g : inf_of_le_left (le_principal_iff.mpr h)
end
end ultrafilter
|
401cca9e58e0282b4a5f1f7d6dcd15733df23649 | 4950bf76e5ae40ba9f8491647d0b6f228ddce173 | /src/ring_theory/derivation.lean | f22eec16816560172029492887ca27c1ae56bf67 | [
"Apache-2.0"
] | permissive | ntzwq/mathlib | ca50b21079b0a7c6781c34b62199a396dd00cee2 | 36eec1a98f22df82eaccd354a758ef8576af2a7f | refs/heads/master | 1,675,193,391,478 | 1,607,822,996,000 | 1,607,822,996,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,122 | lean | /-
Copyright © 2020 Nicolò Cavalleri. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Nicolò Cavalleri.
-/
import algebra.lie.basic
import ring_theory.algebra_tower
/-!
# Derivations
This file defines derivation. A derivation `D` from the `R`-algebra `A` to the `A`-module `M` is an
`R`-linear map that satisfy the Leibniz rule `D (a * b) = a * D b + D a * b`.
## Notation
The notation `⁅D1, D2⁆` is used for the commutator of two derivations.
TODO: this file is just a stub to go on with some PRs in the geometry section. It only
implements the definition of derivations in commutative algebra. This will soon change: as soon
as bimodules will be there in mathlib I will change this file to take into account the
non-commutative case. Any development on the theory of derivations is discouraged until the
definitive definition of derivation will be implemented.
-/
open algebra ring_hom
/-- `D : derivation R A M` is an `R`-linear map from `A` to `M` that satisfies the `leibniz`
equality.
TODO: update this when bimodules are defined. -/
@[protect_proj]
structure derivation (R : Type*) (A : Type*) [comm_semiring R] [comm_semiring A]
[algebra R A] (M : Type*) [add_cancel_comm_monoid M] [semimodule A M] [semimodule R M]
[is_scalar_tower R A M]
extends A →ₗ[R] M :=
(leibniz' (a b : A) : to_fun (a * b) = a • to_fun b + b • to_fun a)
namespace derivation
section
variables {R : Type*} [comm_semiring R]
variables {A : Type*} [comm_semiring A] [algebra R A]
variables {M : Type*} [add_cancel_comm_monoid M] [semimodule A M] [semimodule R M]
variables [is_scalar_tower R A M]
variables (D : derivation R A M) {D1 D2 : derivation R A M} (r : R) (a b : A)
instance : has_coe_to_fun (derivation R A M) := ⟨_, λ D, D.to_linear_map.to_fun⟩
instance has_coe_to_linear_map : has_coe (derivation R A M) (A →ₗ[R] M) :=
⟨λ D, D.to_linear_map⟩
@[simp] lemma to_fun_eq_coe : D.to_fun = ⇑D := rfl
@[simp, norm_cast]
lemma coe_fn_coe (f : derivation R A M) :
⇑(f : A →ₗ[R] M) = f := rfl
lemma coe_injective (H : ⇑D1 = D2) : D1 = D2 :=
by { cases D1, cases D2, congr', exact linear_map.coe_injective H }
@[ext] theorem ext (H : ∀ a, D1 a = D2 a) : D1 = D2 :=
coe_injective $ funext H
@[simp] lemma map_add : D (a + b) = D a + D b := is_add_hom.map_add D a b
@[simp] lemma map_zero : D 0 = 0 := is_add_monoid_hom.map_zero D
@[simp] lemma map_smul : D (r • a) = r • D a := linear_map.map_smul D r a
@[simp] lemma leibniz : D (a * b) = a • D b + b • D a := D.leibniz' _ _
@[simp] lemma map_one_eq_zero : D 1 = 0 :=
begin
have h : D 1 = D (1 * 1) := by rw mul_one,
rwa [leibniz D 1 1, one_smul, left_eq_add_iff] at h,
end
@[simp] lemma map_algebra_map : D (algebra_map R A r) = 0 :=
by rw [←mul_one r, ring_hom.map_mul, map_one, ←smul_def, map_smul, map_one_eq_zero, smul_zero]
instance : has_zero (derivation R A M) :=
⟨⟨(0 : A →ₗ[R] M), λ a b, by simp only [add_zero, linear_map.zero_apply,
linear_map.to_fun_eq_coe, smul_zero]⟩⟩
instance : inhabited (derivation R A M) := ⟨0⟩
instance : add_comm_monoid (derivation R A M) :=
{ add := λ D1 D2, ⟨D1 + D2, λ a b, by { simp only [leibniz, linear_map.add_apply,
linear_map.to_fun_eq_coe, coe_fn_coe, smul_add], cc }⟩,
add_assoc := λ D E F, ext $ λ a, add_assoc _ _ _,
zero_add := λ D, ext $ λ a, zero_add _,
add_zero := λ D, ext $ λ a, add_zero _,
add_comm := λ D E, ext $ λ a, add_comm _ _,
..derivation.has_zero }
@[simp] lemma add_apply : (D1 + D2) a = D1 a + D2 a := rfl
@[priority 100]
instance derivation.Rsemimodule : semimodule R (derivation R A M) :=
{ smul := λ r D, ⟨r • D, λ a b, by simp only [linear_map.smul_apply, leibniz,
linear_map.to_fun_eq_coe, smul_algebra_smul_comm, coe_fn_coe, smul_add, add_comm],⟩,
mul_smul := λ a1 a2 D, ext $ λ b, mul_smul _ _ _,
one_smul := λ D, ext $ λ b, one_smul _ _,
smul_add := λ a D1 D2, ext $ λ b, smul_add _ _ _,
smul_zero := λ a, ext $ λ b, smul_zero _,
add_smul := λ a1 a2 D, ext $ λ b, add_smul _ _ _,
zero_smul := λ D, ext $ λ b, zero_smul _ _ }
@[simp] lemma smul_to_linear_map_coe : ↑(r • D) = (r • D : A →ₗ[R] M) := rfl
@[simp] lemma Rsmul_apply : (r • D) a = r • D a := rfl
instance : semimodule A (derivation R A M) :=
{ smul := λ a D, ⟨a • D, λ b c, by { dsimp, simp only [smul_add, leibniz, smul_comm a, add_comm] }⟩,
mul_smul := λ a1 a2 D, ext $ λ b, mul_smul _ _ _,
one_smul := λ D, ext $ λ b, one_smul A _,
smul_add := λ a D1 D2, ext $ λ b, smul_add _ _ _,
smul_zero := λ a, ext $ λ b, smul_zero _,
add_smul := λ a1 a2 D, ext $ λ b, add_smul _ _ _,
zero_smul := λ D, ext $ λ b, zero_smul A _ }
@[simp] lemma smul_apply : (a • D) b = a • D b := rfl
instance : is_scalar_tower R A (derivation R A M) :=
⟨λ x y z, ext (λ a, smul_assoc _ _ _)⟩
end
section
variables {R : Type*} [comm_ring R]
variables {A : Type*} [comm_ring A] [algebra R A]
section
variables {M : Type*} [add_comm_group M] [module A M] [module R M] [is_scalar_tower R A M]
variables (D : derivation R A M) {D1 D2 : derivation R A M} (r : R) (a b : A)
@[simp] lemma map_neg : D (-a) = -D a := linear_map.map_neg D a
@[simp] lemma map_sub : D (a - b) = D a - D b := linear_map.map_sub D a b
instance : add_comm_group (derivation R A M) :=
{ neg := λ D, ⟨-D, λ a b, by simp only [linear_map.neg_apply, smul_neg, neg_add_rev, leibniz,
linear_map.to_fun_eq_coe, coe_fn_coe, add_comm]⟩,
add_left_neg := λ D, ext $ λ a, add_left_neg _,
..derivation.add_comm_monoid }
@[simp] lemma sub_apply : (D1 - D2) a = D1 a - D2 a := rfl
end
section lie_structures
/-! # Lie structures -/
variables (D : derivation R A A) {D1 D2 : derivation R A A} (r : R) (a b : A)
open ring_commutator
/-- The commutator of derivations is again a derivation. -/
def commutator (D1 D2 : derivation R A A) : derivation R A A :=
{ leibniz' := λ a b, by
{ simp only [commutator, map_add, id.smul_eq_mul, linear_map.mul_app, leibniz,
linear_map.to_fun_eq_coe, coe_fn_coe, linear_map.sub_apply], ring, },
..⁅(D1 : module.End R A), (D2 : module.End R A)⁆, }
instance : has_bracket (derivation R A A) (derivation R A A) := ⟨derivation.commutator⟩
@[simp] lemma commutator_coe_linear_map :
↑⁅D1, D2⁆ = ⁅(D1 : module.End R A), (D2 : module.End R A)⁆ := rfl
lemma commutator_apply : ⁅D1, D2⁆ a = D1 (D2 a) - D2 (D1 a) := rfl
instance : lie_ring (derivation R A A) :=
{ add_lie := λ d e f, by { ext a, simp only [commutator_apply, add_apply, map_add], ring, },
lie_add := λ d e f, by { ext a, simp only [commutator_apply, add_apply, map_add], ring, },
lie_self := λ d, by { ext a, simp only [commutator_apply, add_apply, map_add], ring, },
leibniz_lie := λ d e f,
by { ext a, simp only [commutator_apply, add_apply, sub_apply, map_sub], ring, } }
instance : lie_algebra R (derivation R A A) :=
{ lie_smul := λ r d e, by { ext a, simp only [commutator_apply, map_smul, smul_sub, Rsmul_apply]},
..derivation.Rsemimodule }
end lie_structures
end
end derivation
section comp_der
namespace linear_map
variables {R : Type*} [comm_semiring R]
variables {A : Type*} [comm_semiring A] [algebra R A]
variables {M : Type*} [add_cancel_comm_monoid M] [semimodule A M] [semimodule R M]
variables {N : Type*} [add_cancel_comm_monoid N] [semimodule A N] [semimodule R N]
variables [is_scalar_tower R A M] [is_scalar_tower R A N]
/-- The composition of a linear map and a derivation is a derivation. -/
def comp_der (f : M →ₗ[A] N) (D : derivation R A M) : derivation R A N :=
{ to_fun := λ a, f (D a),
map_add' := λ a1 a2, by rw [D.map_add, f.map_add],
map_smul' := λ r a, by rw [derivation.map_smul, map_smul_of_tower],
leibniz' := λ a b, by simp only [derivation.leibniz, linear_map.map_smul, linear_map.map_add, add_comm] }
@[simp] lemma comp_der_apply (f : M →ₗ[A] N) (D : derivation R A M) (a : A) :
f.comp_der D a = f (D a) := rfl
end linear_map
end comp_der
|
07244313961397e9af5831134d291ceee6458ac0 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /test/examples.lean | 54ab934a9e02ffcb0c2b77a125fbb4f7482e1c9a | [
"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,160 | lean | import data.finset.basic
/-!
## Miscellaneous examples
Please don't add further content to this file;
it too easily becomes a grab bag of forgotten arcana.
Tactics should have their own file in the `test/` directory.
Examples verifying correct behaviour of simp sets or instances
belong in `src/` near the definitions.
TODO: remove or move the remaining content of this file.
-/
open tactic
universe u
variable {α : Type u}
example (s t u : set ℕ) (h : s ⊆ t ∩ u) (h' : u ⊆ s) : u ⊆ s → true :=
begin
dunfold has_subset.subset has_inter.inter at *,
-- trace_state,
intro1, triv
end
example (s t u : set ℕ) (h : s ⊆ t ∩ u) (h' : u ⊆ s) : u ⊆ s → true :=
begin
delta has_subset.subset has_inter.inter at *,
-- trace_state,
intro1, triv
end
example (x y z : ℕ) (h'' : true) (h : 0 + y = x) (h' : 0 + y = z) : x = z + 0 :=
begin
simp at *,
-- trace_state,
rw [←h, ←h']
end
example (x y z : ℕ) (h'' : true) (h : 0 + y = x) (h' : 0 + y = z) : x = z + 0 :=
begin
simp at *,
simp [h] at h',
simp [*]
end
def my_id (x : α) := x
def my_id_def (x : α) : my_id x = x := rfl
example (x y z : ℕ) (h'' : true) (h : 0 + my_id y = x) (h' : 0 + y = z) : x = z + 0 :=
begin
simp [my_id_def] at *, simp [h] at h', simp [*]
end
@[simp] theorem mem_set_of {a : α} {p : α → Prop} : a ∈ {a | p a} = p a := rfl
meta example : true :=
begin
success_if_fail { let := elim_gen_sum_aux },
trivial
end
import_private elim_gen_sum_aux
meta example : true :=
begin
let := elim_gen_sum_aux,
trivial
end
/- tests of has_sep on finset -/
example {α} (s : finset α) (p : α → Prop) [decidable_pred p] : {x ∈ s | p x} = s.filter p :=
by simp
example {α} (s : finset α) (p : α → Prop) [decidable_pred p] :
{x ∈ s | p x} = @finset.filter α p (λ _, classical.prop_decidable _) s :=
by simp
section
open_locale classical
example {α} (s : finset α) (p : α → Prop) : {x ∈ s | p x} = s.filter p :=
by simp
example (n m k : ℕ) : {x ∈ finset.range n | x < m ∨ x < k } =
{x ∈ finset.range n | x < m } ∪ {x ∈ finset.range n | x < k } :=
by simp [finset.filter_or]
end
|
df7413ff8da4876acc0547beae0e21ff0ce39db5 | 02fbe05a45fda5abde7583464416db4366eedfbf | /library/init/algebra/order.lean | 76371309670ccb01da161467118afb16521cf4f9 | [
"Apache-2.0"
] | permissive | jasonrute/lean | cc12807e11f9ac6b01b8951a8bfb9c2eb35a0154 | 4be962c167ca442a0ec5e84472d7ff9f5302788f | refs/heads/master | 1,672,036,664,637 | 1,601,642,826,000 | 1,601,642,826,000 | 260,777,966 | 0 | 0 | Apache-2.0 | 1,588,454,819,000 | 1,588,454,818,000 | null | UTF-8 | Lean | false | false | 11,505 | 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.logic init.classical init.meta.name init.algebra.classes
/- Make sure instances defined in this file have lower priority than the ones
defined for concrete structures -/
set_option default_priority 100
set_option old_structure_cmd true
universe u
variables {α : Type u}
set_option auto_param.check_exists false
/-- A preorder is a reflexive, transitive relation `≤` with `a < b` defined in the obvious way. -/
class preorder (α : Type u) extends has_le α, has_lt α :=
(le_refl : ∀ a : α, a ≤ a)
(le_trans : ∀ a b c : α, a ≤ b → b ≤ c → a ≤ c)
(lt := λ a b, a ≤ b ∧ ¬ b ≤ a)
(lt_iff_le_not_le : ∀ a b : α, a < b ↔ (a ≤ b ∧ ¬ b ≤ a) . order_laws_tac)
/-- A partial order is a reflexive, transitive, antisymmetric relation `≤`. -/
class partial_order (α : Type u) extends preorder α :=
(le_antisymm : ∀ a b : α, a ≤ b → b ≤ a → a = b)
/-- A linear order is reflexive, transitive, antisymmetric and total relation `≤`.-/
class linear_order (α : Type u) extends partial_order α :=
(le_total : ∀ a b : α, a ≤ b ∨ b ≤ a)
/-- The relation `≤` on a preorder is reflexive. -/
@[refl] lemma le_refl [preorder α] : ∀ a : α, a ≤ a :=
preorder.le_refl
/-- The relation `≤` on a preorder is transitive. -/
@[trans] lemma le_trans [preorder α] : ∀ {a b c : α}, a ≤ b → b ≤ c → a ≤ c :=
preorder.le_trans
lemma lt_iff_le_not_le [preorder α] : ∀ {a b : α}, a < b ↔ (a ≤ b ∧ ¬ b ≤ a) :=
preorder.lt_iff_le_not_le
lemma lt_of_le_not_le [preorder α] : ∀ {a b : α}, a ≤ b → ¬ b ≤ a → a < b
| a b hab hba := lt_iff_le_not_le.mpr ⟨hab, hba⟩
lemma le_not_le_of_lt [preorder α] : ∀ {a b : α}, a < b → a ≤ b ∧ ¬ b ≤ a
| a b hab := lt_iff_le_not_le.mp hab
lemma le_antisymm [partial_order α] : ∀ {a b : α}, a ≤ b → b ≤ a → a = b :=
partial_order.le_antisymm
lemma le_of_eq [preorder α] {a b : α} : a = b → a ≤ b :=
λ h, h ▸ le_refl a
lemma le_antisymm_iff [partial_order α] {a b : α} : a = b ↔ a ≤ b ∧ b ≤ a :=
⟨λe, ⟨le_of_eq e, le_of_eq e.symm⟩, λ⟨h1, h2⟩, le_antisymm h1 h2⟩
@[trans] lemma ge_trans [preorder α] : ∀ {a b c : α}, a ≥ b → b ≥ c → a ≥ c :=
λ a b c h₁ h₂, le_trans h₂ h₁
lemma le_total [linear_order α] : ∀ a b : α, a ≤ b ∨ b ≤ a :=
linear_order.le_total
lemma le_of_not_ge [linear_order α] {a b : α} : ¬ a ≥ b → a ≤ b :=
or.resolve_left (le_total b a)
lemma le_of_not_le [linear_order α] {a b : α} : ¬ a ≤ b → b ≤ a :=
or.resolve_left (le_total a b)
lemma lt_irrefl [preorder α] : ∀ a : α, ¬ a < a
| a haa := match le_not_le_of_lt haa with
| ⟨h1, h2⟩ := false.rec _ (h2 h1)
end
lemma gt_irrefl [preorder α] : ∀ a : α, ¬ a > a :=
lt_irrefl
@[trans] lemma lt_trans [preorder α] : ∀ {a b c : α}, a < b → b < c → a < c
| a b c hab hbc :=
match le_not_le_of_lt hab, le_not_le_of_lt hbc with
| ⟨hab, hba⟩, ⟨hbc, hcb⟩ := lt_of_le_not_le (le_trans hab hbc) (λ hca, hcb (le_trans hca hab))
end
def lt.trans := @lt_trans
@[trans] lemma gt_trans [preorder α] : ∀ {a b c : α}, a > b → b > c → a > c :=
λ a b c h₁ h₂, lt_trans h₂ h₁
def gt.trans := @gt_trans
lemma ne_of_lt [preorder α] {a b : α} (h : a < b) : a ≠ b :=
λ he, absurd h (he ▸ lt_irrefl a)
lemma ne_of_gt [preorder α] {a b : α} (h : a > b) : a ≠ b :=
λ he, absurd h (he ▸ lt_irrefl a)
lemma lt_asymm [preorder α] {a b : α} (h : a < b) : ¬ b < a :=
λ h1 : b < a, lt_irrefl a (lt_trans h h1)
lemma not_lt_of_gt [linear_order α] {a b : α} (h : a > b) : ¬ a < b :=
lt_asymm h
lemma le_of_lt [preorder α] : ∀ {a b : α}, a < b → a ≤ b
| a b hab := (le_not_le_of_lt hab).left
@[trans] lemma lt_of_lt_of_le [preorder α] : ∀ {a b c : α}, a < b → b ≤ c → a < c
| a b c hab hbc :=
let ⟨hab, hba⟩ := le_not_le_of_lt hab in
lt_of_le_not_le (le_trans hab hbc) $ λ hca, hba (le_trans hbc hca)
@[trans] lemma lt_of_le_of_lt [preorder α] : ∀ {a b c : α}, a ≤ b → b < c → a < c
| a b c hab hbc :=
let ⟨hbc, hcb⟩ := le_not_le_of_lt hbc in
lt_of_le_not_le (le_trans hab hbc) $ λ hca, hcb (le_trans hca hab)
@[trans] lemma gt_of_gt_of_ge [preorder α] {a b c : α} (h₁ : a > b) (h₂ : b ≥ c) : a > c :=
lt_of_le_of_lt h₂ h₁
@[trans] lemma gt_of_ge_of_gt [preorder α] {a b c : α} (h₁ : a ≥ b) (h₂ : b > c) : a > c :=
lt_of_lt_of_le h₂ h₁
lemma not_le_of_gt [preorder α] {a b : α} (h : a > b) : ¬ a ≤ b :=
(le_not_le_of_lt h).right
lemma not_lt_of_ge [preorder α] {a b : α} (h : a ≥ b) : ¬ a < b :=
λ hab, not_le_of_gt hab h
lemma lt_or_eq_of_le [partial_order α] : ∀ {a b : α}, a ≤ b → a < b ∨ a = b
| a b hab := classical.by_cases
(λ hba : b ≤ a, or.inr (le_antisymm hab hba))
(λ hba, or.inl (lt_of_le_not_le hab hba))
lemma le_of_lt_or_eq [preorder α] : ∀ {a b : α}, (a < b ∨ a = b) → a ≤ b
| a b (or.inl hab) := le_of_lt hab
| a b (or.inr hab) := hab ▸ le_refl _
lemma le_iff_lt_or_eq [partial_order α] : ∀ {a b : α}, a ≤ b ↔ a < b ∨ a = b
| a b := ⟨lt_or_eq_of_le, le_of_lt_or_eq⟩
lemma lt_of_le_of_ne [partial_order α] {a b : α} : a ≤ b → a ≠ b → a < b :=
λ h₁ h₂, lt_of_le_not_le h₁ $ mt (le_antisymm h₁) h₂
lemma lt_trichotomy [linear_order α] (a b : α) : a < b ∨ a = b ∨ b < a :=
or.elim (le_total a b)
(λ h : a ≤ b, or.elim (lt_or_eq_of_le h)
(λ h : a < b, or.inl h)
(λ h : a = b, or.inr (or.inl h)))
(λ h : b ≤ a, or.elim (lt_or_eq_of_le h)
(λ h : b < a, or.inr (or.inr h))
(λ h : b = a, or.inr (or.inl h.symm)))
lemma le_of_not_gt [linear_order α] {a b : α} (h : ¬ a > b) : a ≤ b :=
match lt_trichotomy a b with
| or.inl hlt := le_of_lt hlt
| or.inr (or.inl heq) := heq ▸ le_refl a
| or.inr (or.inr hgt) := absurd hgt h
end
lemma lt_of_not_ge [linear_order α] {a b : α} (h : ¬ a ≥ b) : a < b :=
lt_of_le_not_le ((le_total _ _).resolve_right h) h
lemma lt_or_ge [linear_order α] (a b : α) : a < b ∨ a ≥ b :=
match lt_trichotomy a b with
| or.inl hlt := or.inl hlt
| or.inr (or.inl heq) := or.inr (heq ▸ le_refl a)
| or.inr (or.inr hgt) := or.inr (le_of_lt hgt)
end
lemma le_or_gt [linear_order α] (a b : α) : a ≤ b ∨ a > b :=
or.swap (lt_or_ge b a)
lemma lt_or_gt_of_ne [linear_order α] {a b : α} (h : a ≠ b) : a < b ∨ a > b :=
match lt_trichotomy a b with
| or.inl hlt := or.inl hlt
| or.inr (or.inl heq) := absurd heq h
| or.inr (or.inr hgt) := or.inr hgt
end
lemma le_of_eq_or_lt [preorder α] {a b : α} (h : a = b ∨ a < b) : a ≤ b :=
or.elim h le_of_eq le_of_lt
lemma ne_iff_lt_or_gt [linear_order α] {a b : α} : a ≠ b ↔ a < b ∨ a > b :=
⟨lt_or_gt_of_ne, λo, or.elim o ne_of_lt ne_of_gt⟩
lemma lt_iff_not_ge [linear_order α] (x y : α) : x < y ↔ ¬ x ≥ y :=
⟨not_le_of_gt, lt_of_not_ge⟩
@[simp] lemma not_lt [linear_order α] {a b : α} : ¬ a < b ↔ b ≤ a := ⟨le_of_not_gt, not_lt_of_ge⟩
@[simp] lemma not_le [linear_order α] {a b : α} : ¬ a ≤ b ↔ b < a := (lt_iff_not_ge _ _).symm
instance decidable_lt_of_decidable_le [preorder α]
[decidable_rel ((≤) : α → α → Prop)] :
decidable_rel ((<) : α → α → Prop)
| a b :=
if hab : a ≤ b then
if hba : b ≤ a then
is_false $ λ hab', not_le_of_gt hab' hba
else
is_true $ lt_of_le_not_le hab hba
else
is_false $ λ hab', hab (le_of_lt hab')
instance decidable_eq_of_decidable_le [partial_order α]
[decidable_rel ((≤) : α → α → Prop)] :
decidable_eq α
| a b :=
if hab : a ≤ b then
if hba : b ≤ a then
is_true (le_antisymm hab hba)
else
is_false (λ heq, hba (heq ▸ le_refl _))
else
is_false (λ heq, hab (heq ▸ le_refl _))
class decidable_linear_order (α : Type u) extends linear_order α :=
(decidable_le : decidable_rel (≤))
(decidable_eq : decidable_eq α := @decidable_eq_of_decidable_le _ _ decidable_le)
(decidable_lt : decidable_rel ((<) : α → α → Prop) :=
@decidable_lt_of_decidable_le _ _ decidable_le)
instance [decidable_linear_order α] (a b : α) : decidable (a < b) :=
decidable_linear_order.decidable_lt a b
instance [decidable_linear_order α] (a b : α) : decidable (a ≤ b) :=
decidable_linear_order.decidable_le a b
instance [decidable_linear_order α] (a b : α) : decidable (a = b) :=
decidable_linear_order.decidable_eq a b
lemma eq_or_lt_of_not_lt [decidable_linear_order α] {a b : α} (h : ¬ a < b) : a = b ∨ b < a :=
if h₁ : a = b then or.inl h₁
else or.inr (lt_of_not_ge (λ hge, h (lt_of_le_of_ne hge h₁)))
instance [decidable_linear_order α] : is_total_preorder α (≤) :=
{trans := @le_trans _ _, total := le_total}
/- TODO(Leo): decide whether we should keep this instance or not -/
instance is_strict_weak_order_of_decidable_linear_order [decidable_linear_order α] : is_strict_weak_order α (<) :=
is_strict_weak_order_of_is_total_preorder lt_iff_not_ge
/- TODO(Leo): decide whether we should keep this instance or not -/
instance is_strict_total_order_of_decidable_linear_order [decidable_linear_order α] : is_strict_total_order α (<) :=
{ trichotomous := lt_trichotomy }
namespace decidable
lemma lt_or_eq_of_le [partial_order α] [@decidable_rel α (≤)] {a b : α} (hab : a ≤ b) : a < b ∨ a = b :=
if hba : b ≤ a then or.inr (le_antisymm hab hba)
else or.inl (lt_of_le_not_le hab hba)
lemma eq_or_lt_of_le [partial_order α] [@decidable_rel α (≤)] {a b : α} (hab : a ≤ b) : a = b ∨ a < b :=
(lt_or_eq_of_le hab).swap
lemma le_iff_lt_or_eq [partial_order α] [@decidable_rel α (≤)] {a b : α} : a ≤ b ↔ a < b ∨ a = b :=
⟨lt_or_eq_of_le, le_of_lt_or_eq⟩
lemma le_of_not_lt [decidable_linear_order α] {a b : α} (h : ¬ b < a) : a ≤ b :=
decidable.by_contradiction $ λ h', h $ lt_of_le_not_le ((le_total _ _).resolve_right h') h'
lemma not_lt [decidable_linear_order α] {a b : α} : ¬ a < b ↔ b ≤ a :=
⟨le_of_not_lt, not_lt_of_ge⟩
lemma lt_or_le [decidable_linear_order α] (a b : α) : a < b ∨ b ≤ a :=
if hba : b ≤ a then or.inr hba else or.inl $ lt_of_not_ge hba
lemma le_or_lt [decidable_linear_order α] (a b : α) : a ≤ b ∨ b < a :=
(lt_or_le b a).swap
lemma lt_trichotomy [decidable_linear_order α] (a b : α) : a < b ∨ a = b ∨ b < a :=
(lt_or_le _ _).imp_right $ λ h, (eq_or_lt_of_le h).imp_left eq.symm
lemma lt_or_gt_of_ne [decidable_linear_order α] {a b : α} (h : a ≠ b) : a < b ∨ b < a :=
(lt_trichotomy a b).imp_right $ λ h', h'.resolve_left h
/-- Perform a case-split on the ordering of `x` and `y` in a decidable linear order. -/
def lt_by_cases [decidable_linear_order α] (x y : α) {P : Sort*}
(h₁ : x < y → P) (h₂ : x = y → P) (h₃ : y < x → P) : P :=
if h : x < y then h₁ h else
if h' : y < x then h₃ h' else
h₂ (le_antisymm (le_of_not_gt h') (le_of_not_gt h))
lemma ne_iff_lt_or_gt [decidable_linear_order α] {a b : α} : a ≠ b ↔ a < b ∨ b < a :=
⟨lt_or_gt_of_ne, λo, o.elim ne_of_lt ne_of_gt⟩
lemma le_imp_le_of_lt_imp_lt {β} [preorder α] [decidable_linear_order β]
{a b : α} {c d : β} (H : d < c → b < a) (h : a ≤ b) : c ≤ d :=
le_of_not_lt $ λ h', not_le_of_gt (H h') h
end decidable
|
875d74309a41c50ed470a1b4874beb20cbc374de | c09f5945267fd905e23a77be83d9a78580e04a4a | /src/topology/separation.lean | dc1789f164e03e76b5327b3f2058643cf2c9da40 | [
"Apache-2.0"
] | permissive | OHIHIYA20/mathlib | 023a6df35355b5b6eb931c404f7dd7535dccfa89 | 1ec0a1f49db97d45e8666a3bf33217ff79ca1d87 | refs/heads/master | 1,587,964,529,965 | 1,551,819,319,000 | 1,551,819,319,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,435 | 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
Separation properties of topological spaces.
-/
import topology.order
open set filter lattice
local attribute [instance] classical.prop_decidable
universes u v
variables {α : Type u} {β : Type v} [topological_space α]
section separation
/-- A T₀ space, also known as a Kolmogorov space, is a topological space
where for every pair `x ≠ y`, there is an open set containing one but not the other. -/
class t0_space (α : Type u) [topological_space α] : Prop :=
(t0 : ∀ x y, x ≠ y → ∃ U:set α, is_open U ∧ (xor (x ∈ U) (y ∈ U)))
theorem exists_open_singleton_of_fintype [t0_space α]
[f : fintype α] [decidable_eq α] [ha : nonempty α] :
∃ x:α, is_open ({x}:set α) :=
have H : ∀ (T : finset α), T ≠ ∅ → ∃ x ∈ T, ∃ u, is_open u ∧ {x} = {y | y ∈ T} ∩ u :=
begin
intro T,
apply finset.case_strong_induction_on T,
{ intro h, exact (h rfl).elim },
{ intros x S hxS ih h,
by_cases hs : S = ∅,
{ existsi [x, finset.mem_insert_self x S, univ, is_open_univ],
rw [hs, inter_univ], refl },
{ rcases ih S (finset.subset.refl S) hs with ⟨y, hy, V, hv1, hv2⟩,
by_cases hxV : x ∈ V,
{ cases t0_space.t0 x y (λ hxy, hxS $ by rwa hxy) with U hu,
rcases hu with ⟨hu1, ⟨hu2, hu3⟩ | ⟨hu2, hu3⟩⟩,
{ existsi [x, finset.mem_insert_self x S, U ∩ V, is_open_inter hu1 hv1],
apply set.ext,
intro z,
split,
{ intro hzx,
rw set.mem_singleton_iff at hzx,
rw hzx,
exact ⟨finset.mem_insert_self x S, ⟨hu2, hxV⟩⟩ },
{ intro hz,
rw set.mem_singleton_iff,
rcases hz with ⟨hz1, hz2, hz3⟩,
cases finset.mem_insert.1 hz1 with hz4 hz4,
{ exact hz4 },
{ have h1 : z ∈ {y : α | y ∈ S} ∩ V,
{ exact ⟨hz4, hz3⟩ },
rw ← hv2 at h1,
rw set.mem_singleton_iff at h1,
rw h1 at hz2,
exact (hu3 hz2).elim } } },
{ existsi [y, finset.mem_insert_of_mem hy, U ∩ V, is_open_inter hu1 hv1],
apply set.ext,
intro z,
split,
{ intro hz,
rw set.mem_singleton_iff at hz,
rw hz,
refine ⟨finset.mem_insert_of_mem hy, hu2, _⟩,
have h1 : y ∈ {y} := set.mem_singleton y,
rw hv2 at h1,
exact h1.2 },
{ intro hz,
rw set.mem_singleton_iff,
cases hz with hz1 hz2,
cases finset.mem_insert.1 hz1 with hz3 hz3,
{ rw hz3 at hz2,
exact (hu3 hz2.1).elim },
{ have h1 : z ∈ {y : α | y ∈ S} ∩ V := ⟨hz3, hz2.2⟩,
rw ← hv2 at h1,
rw set.mem_singleton_iff at h1,
exact h1 } } } },
{ existsi [y, finset.mem_insert_of_mem hy, V, hv1],
apply set.ext,
intro z,
split,
{ intro hz,
rw set.mem_singleton_iff at hz,
rw hz,
split,
{ exact finset.mem_insert_of_mem hy },
{ have h1 : y ∈ {y} := set.mem_singleton y,
rw hv2 at h1,
exact h1.2 } },
{ intro hz,
rw hv2,
cases hz with hz1 hz2,
cases finset.mem_insert.1 hz1 with hz3 hz3,
{ rw hz3 at hz2,
exact (hxV hz2).elim },
{ exact ⟨hz3, hz2⟩ } } } } }
end,
begin
apply nonempty.elim ha, intro x,
specialize H finset.univ (finset.ne_empty_of_mem $ finset.mem_univ x),
rcases H with ⟨y, hyf, U, hu1, hu2⟩,
existsi y,
have h1 : {y : α | y ∈ finset.univ} = (univ : set α),
{ exact set.eq_univ_of_forall (λ x : α,
by rw mem_set_of_eq; exact finset.mem_univ x) },
rw h1 at hu2,
rw set.univ_inter at hu2,
rw hu2,
exact hu1
end
/-- A T₁ space, also known as a Fréchet space, is a topological space
where every singleton set is closed. Equivalently, for every pair
`x ≠ y`, there is an open set containing `x` and not `y`. -/
class t1_space (α : Type u) [topological_space α] : Prop :=
(t1 : ∀x, is_closed ({x} : set α))
lemma is_closed_singleton [t1_space α] {x : α} : is_closed ({x} : set α) :=
t1_space.t1 x
instance t1_space.t0_space [t1_space α] : t0_space α :=
⟨λ x y h, ⟨-{x}, is_open_compl_iff.2 is_closed_singleton,
or.inr ⟨λ hyx, or.cases_on hyx h.symm id, λ hx, hx $ or.inl rfl⟩⟩⟩
lemma compl_singleton_mem_nhds [t1_space α] {x y : α} (h : y ≠ x) : - {x} ∈ (nhds y).sets :=
mem_nhds_sets is_closed_singleton $ by rwa [mem_compl_eq, mem_singleton_iff]
@[simp] lemma closure_singleton [t1_space α] {a : α} :
closure ({a} : set α) = {a} :=
closure_eq_of_is_closed is_closed_singleton
/-- A T₂ space, also known as a Hausdorff space, is one in which for every
`x ≠ y` there exists disjoint open sets around `x` and `y`. This is
the most widely used of the separation axioms. -/
class t2_space (α : Type u) [topological_space α] : Prop :=
(t2 : ∀x y, x ≠ y → ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅)
lemma t2_separation [t2_space α] {x y : α} (h : x ≠ y) :
∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ :=
t2_space.t2 x y h
instance t2_space.t1_space [t2_space α] : t1_space α :=
⟨λ x, is_open_iff_forall_mem_open.2 $ λ y hxy,
let ⟨u, v, hu, hv, hyu, hxv, huv⟩ := t2_separation (mt mem_singleton_of_eq hxy) in
⟨u, λ z hz1 hz2, ((ext_iff _ _).1 huv x).1 ⟨mem_singleton_iff.1 hz2 ▸ hz1, hxv⟩, hu, hyu⟩⟩
lemma eq_of_nhds_neq_bot [ht : t2_space α] {x y : α} (h : nhds x ⊓ nhds y ≠ ⊥) : x = y :=
classical.by_contradiction $ assume : x ≠ y,
let ⟨u, v, hu, hv, hx, hy, huv⟩ := t2_space.t2 x y this in
have u ∩ v ∈ (nhds x ⊓ nhds y).sets,
from inter_mem_inf_sets (mem_nhds_sets hu hx) (mem_nhds_sets hv hy),
h $ empty_in_sets_eq_bot.mp $ huv ▸ this
lemma t2_iff_nhds : t2_space α ↔ ∀ {x y : α}, nhds x ⊓ nhds y ≠ ⊥ → x = y :=
⟨assume h, by exactI λ x y, eq_of_nhds_neq_bot,
assume h, ⟨assume x y xy,
have nhds x ⊓ nhds y = ⊥ := classical.by_contradiction (mt h xy),
let ⟨u', hu', v', hv', u'v'⟩ := empty_in_sets_eq_bot.mpr this,
⟨u, uu', uo, hu⟩ := mem_nhds_sets_iff.mp hu',
⟨v, vv', vo, hv⟩ := mem_nhds_sets_iff.mp hv' in
⟨u, v, uo, vo, hu, hv, disjoint.eq_bot $ disjoint_mono uu' vv' u'v'⟩⟩⟩
lemma t2_iff_ultrafilter :
t2_space α ↔ ∀ f {x y : α}, is_ultrafilter f → f ≤ nhds x → f ≤ nhds y → x = y :=
t2_iff_nhds.trans
⟨assume h f x y u fx fy, h $ neq_bot_of_le_neq_bot u.1 (le_inf fx fy),
assume h x y xy,
let ⟨f, hf, uf⟩ := exists_ultrafilter xy in
h f uf (le_trans hf lattice.inf_le_left) (le_trans hf lattice.inf_le_right)⟩
@[simp] lemma nhds_eq_nhds_iff {a b : α} [t2_space α] : nhds a = nhds b ↔ a = b :=
⟨assume h, eq_of_nhds_neq_bot $ by rw [h, inf_idem]; exact nhds_neq_bot, assume h, h ▸ rfl⟩
@[simp] lemma nhds_le_nhds_iff {a b : α} [t2_space α] : nhds a ≤ nhds b ↔ a = b :=
⟨assume h, eq_of_nhds_neq_bot $ by rw [inf_of_le_left h]; exact nhds_neq_bot, assume h, h ▸ le_refl _⟩
lemma tendsto_nhds_unique [t2_space α] {f : β → α} {l : filter β} {a b : α}
(hl : l ≠ ⊥) (ha : tendsto f l (nhds a)) (hb : tendsto f l (nhds b)) : a = b :=
eq_of_nhds_neq_bot $ neq_bot_of_le_neq_bot (map_ne_bot hl) $ le_inf ha hb
section lim
variables [inhabited α] [t2_space α] {f : filter α}
lemma lim_eq {a : α} (hf : f ≠ ⊥) (h : f ≤ nhds a) : lim f = a :=
eq_of_nhds_neq_bot $ neq_bot_of_le_neq_bot hf $ le_inf (lim_spec ⟨_, h⟩) h
@[simp] lemma lim_nhds_eq {a : α} : lim (nhds a) = a :=
lim_eq nhds_neq_bot (le_refl _)
@[simp] lemma lim_nhds_eq_of_closure {a : α} {s : set α} (h : a ∈ closure s) :
lim (nhds a ⊓ principal s) = a :=
lim_eq begin rw [closure_eq_nhds] at h, exact h end inf_le_left
end lim
instance t2_space_discrete [topological_space α] [discrete_topology α] : t2_space α :=
{ t2 := assume x y hxy, ⟨{x}, {y}, is_open_discrete _, is_open_discrete _, mem_insert _ _, mem_insert _ _,
eq_empty_iff_forall_not_mem.2 $ by intros z hz;
cases eq_of_mem_singleton hz.1; cases eq_of_mem_singleton hz.2; cc⟩ }
private lemma separated_by_f
[tα : topological_space α] [tβ : topological_space β] [t2_space β]
(f : α → β) (hf : tβ.induced f ≤ tα) {x y : α} (h : f x ≠ f y) :
∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ :=
let ⟨u, v, uo, vo, xu, yv, uv⟩ := t2_separation h in
⟨f ⁻¹' u, f ⁻¹' v, hf _ ⟨u, uo, rfl⟩, hf _ ⟨v, vo, rfl⟩, xu, yv,
by rw [←preimage_inter, uv, preimage_empty]⟩
instance {p : α → Prop} [t : topological_space α] [t2_space α] : t2_space (subtype p) :=
⟨assume x y h,
separated_by_f subtype.val (le_refl _) (mt subtype.eq h)⟩
instance [t₁ : topological_space α] [t2_space α] [t₂ : topological_space β] [t2_space β] :
t2_space (α × β) :=
⟨assume ⟨x₁,x₂⟩ ⟨y₁,y₂⟩ h,
or.elim (not_and_distrib.mp (mt prod.ext_iff.mpr h))
(λ h₁, separated_by_f prod.fst le_sup_left h₁)
(λ h₂, separated_by_f prod.snd le_sup_right h₂)⟩
instance Pi.t2_space {β : α → Type v} [t₂ : Πa, topological_space (β a)] [Πa, t2_space (β a)] :
t2_space (Πa, β a) :=
⟨assume x y h,
let ⟨i, hi⟩ := not_forall.mp (mt funext h) in
separated_by_f (λz, z i) (le_supr _ i) hi⟩
end separation
section regularity
/-- A T₃ space, also known as a regular space (although this condition sometimes
omits T₂), is one in which for every closed `C` and `x ∉ C`, there exist
disjoint open sets containing `x` and `C` respectively. -/
class regular_space (α : Type u) [topological_space α] extends t1_space α : Prop :=
(regular : ∀{s:set α} {a}, is_closed s → a ∉ s → ∃t, is_open t ∧ s ⊆ t ∧ nhds a ⊓ principal t = ⊥)
lemma nhds_is_closed [regular_space α] {a : α} {s : set α} (h : s ∈ (nhds a).sets) :
∃t∈(nhds a).sets, t ⊆ s ∧ is_closed t :=
let ⟨s', h₁, h₂, h₃⟩ := mem_nhds_sets_iff.mp h in
have ∃t, is_open t ∧ -s' ⊆ t ∧ nhds a ⊓ principal t = ⊥,
from regular_space.regular (is_closed_compl_iff.mpr h₂) (not_not_intro h₃),
let ⟨t, ht₁, ht₂, ht₃⟩ := this in
⟨-t,
mem_sets_of_neq_bot $ by rwa [lattice.neg_neg],
subset.trans (compl_subset_comm.1 ht₂) h₁,
is_closed_compl_iff.mpr ht₁⟩
variable (α)
instance regular_space.t2_space [regular_space α] : t2_space α :=
⟨λ x y hxy,
let ⟨s, hs, hys, hxs⟩ := regular_space.regular is_closed_singleton
(mt mem_singleton_iff.1 hxy),
⟨t, hxt, u, hsu, htu⟩ := empty_in_sets_eq_bot.2 hxs,
⟨v, hvt, hv, hxv⟩ := mem_nhds_sets_iff.1 hxt in
⟨v, s, hv, hs, hxv, singleton_subset_iff.1 hys,
eq_empty_of_subset_empty $ λ z ⟨hzv, hzs⟩, htu ⟨hvt hzv, hsu hzs⟩⟩⟩
end regularity
section normality
/-- A T₄ space, also known as a normal space (although this condition sometimes
omits T₂), is one in which for every pair of disjoint closed sets `C` and `D`,
there exist disjoint open sets containing `C` and `D` respectively. -/
class normal_space (α : Type u) [topological_space α] extends t1_space α : Prop :=
(normal : ∀ s t : set α, is_closed s → is_closed t → disjoint s t →
∃ u v, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ disjoint u v)
theorem normal_separation [normal_space α] (s t : set α)
(H1 : is_closed s) (H2 : is_closed t) (H3 : disjoint s t) :
∃ u v, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ disjoint u v :=
normal_space.normal s t H1 H2 H3
variable (α)
instance normal_space.regular_space [normal_space α] : regular_space α :=
{ regular := λ s x hs hxs, let ⟨u, v, hu, hv, hsu, hxv, huv⟩ := normal_separation s {x} hs is_closed_singleton
(λ _ ⟨hx, hy⟩, hxs $ set.mem_of_eq_of_mem (set.eq_of_mem_singleton hy).symm hx) in
⟨u, hu, hsu, filter.empty_in_sets_eq_bot.1 $ filter.mem_inf_sets.2
⟨v, mem_nhds_sets hv (set.singleton_subset_iff.1 hxv), u, filter.mem_principal_self u, set.inter_comm u v ▸ huv⟩⟩ }
end normality |
80fb3a35adc3bfc6eed6886a80dfe23bfbd36169 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/logic/relator.lean | e2d4251b193b51ca4d93a4c3fc7154bb7179fcd7 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 3,583 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import logic.basic
/-!
# Relator for functions, pairs, sums, and lists.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
namespace relator
universes u₁ u₂ v₁ v₂
/- TODO(johoelzl):
* should we introduce relators of datatypes as recursive function or as inductive
predicate? For now we stick to the recursor approach.
* relation lift for datatypes, Π, Σ, set, and subtype types
* proof composition and identity laws
* implement method to derive relators from datatype
-/
section
variables {α : Sort u₁} {β : Sort u₂} {γ : Sort v₁} {δ : Sort v₂}
variables (R : α → β → Prop) (S : γ → δ → Prop)
def lift_fun (f : α → γ) (g : β → δ) : Prop :=
∀⦃a b⦄, R a b → S (f a) (g b)
infixr ` ⇒ ` := lift_fun
end
section
variables {α : Type u₁} {β : Type u₂} (R : α → β → Prop)
def right_total : Prop := ∀ b, ∃ a, R a b
def left_total : Prop := ∀ a, ∃ b, R a b
def bi_total : Prop := left_total R ∧ right_total R
def left_unique : Prop := ∀ ⦃a b c⦄, R a c → R b c → a = b
def right_unique : Prop := ∀ ⦃a b c⦄, R a b → R a c → b = c
def bi_unique : Prop := left_unique R ∧ right_unique R
variable {R}
lemma right_total.rel_forall (h : right_total R) :
((R ⇒ implies) ⇒ implies) (λp, ∀i, p i) (λq, ∀i, q i) :=
assume p q Hrel H b, exists.elim (h b) (assume a Rab, Hrel Rab (H _))
lemma left_total.rel_exists (h : left_total R) :
((R ⇒ implies) ⇒ implies) (λp, ∃i, p i) (λq, ∃i, q i) :=
assume p q Hrel ⟨a, pa⟩, (h a).imp $ λ b Rab, Hrel Rab pa
lemma bi_total.rel_forall (h : bi_total R) :
((R ⇒ iff) ⇒ iff) (λp, ∀i, p i) (λq, ∀i, q i) :=
assume p q Hrel,
⟨assume H b, exists.elim (h.right b) (assume a Rab, (Hrel Rab).mp (H _)),
assume H a, exists.elim (h.left a) (assume b Rab, (Hrel Rab).mpr (H _))⟩
lemma bi_total.rel_exists (h : bi_total R) : ((R ⇒ iff) ⇒ iff) (λp, ∃i, p i) (λq, ∃i, q i) :=
assume p q Hrel,
⟨assume ⟨a, pa⟩, (h.left a).imp $ λ b Rab, (Hrel Rab).1 pa,
assume ⟨b, qb⟩, (h.right b).imp $ λ a Rab, (Hrel Rab).2 qb⟩
lemma left_unique_of_rel_eq {eq' : β → β → Prop} (he : (R ⇒ (R ⇒ iff)) eq eq') : left_unique R :=
λ a b c (ac : R a c) (bc : R b c), (he ac bc).mpr ((he bc bc).mp rfl)
end
lemma rel_imp : (iff ⇒ (iff ⇒ iff)) implies implies :=
assume p q h r s l, imp_congr h l
lemma rel_not : (iff ⇒ iff) not not :=
assume p q h, not_congr h
lemma bi_total_eq {α : Type u₁} : relator.bi_total (@eq α) :=
{ left := λ a, ⟨a, rfl⟩, right := λ a, ⟨a, rfl⟩ }
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
variables {r : α → β → Prop} {p : β → γ → Prop} {q : γ → δ → Prop}
lemma left_unique.flip (h : left_unique r) : right_unique (flip r) :=
λ a b c h₁ h₂, h h₁ h₂
lemma rel_and : ((↔) ⇒ (↔) ⇒ (↔)) (∧) (∧) :=
assume a b h₁ c d h₂, and_congr h₁ h₂
lemma rel_or : ((↔) ⇒ (↔) ⇒ (↔)) (∨) (∨) :=
assume a b h₁ c d h₂, or_congr h₁ h₂
lemma rel_iff : ((↔) ⇒ (↔) ⇒ (↔)) (↔) (↔) :=
assume a b h₁ c d h₂, iff_congr h₁ h₂
lemma rel_eq {r : α → β → Prop} (hr : bi_unique r) : (r ⇒ r ⇒ (↔)) (=) (=) :=
λ a b h₁ c d h₂, ⟨λ h, hr.right h₁ $ h.symm ▸ h₂, λ h, hr.left h₁ $ h.symm ▸ h₂⟩
end relator
|
771db84c55d0731c9d32bd06b7c5b036e9f8dd5f | 3dc4623269159d02a444fe898d33e8c7e7e9461b | /.github/workflows/project_1_a_decrire/lean-scheme-submission/src/sheaves/presheaf_of_rings.lean | 05ece16e587cb6cf539d3afbbc14265c14c775db | [] | 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,684 | lean | /-
Presheaf of rings.
https://stacks.math.columbia.edu/tag/006N
-/
import data.equiv.algebra
import topology.opens
import sheaves.presheaf
open topological_space
universes u v
-- Definition of a presheaf of rings.
structure presheaf_of_rings (α : Type u) [topological_space α]
extends presheaf α :=
(Fring : ∀ (U), comm_ring (F U))
(res_is_ring_hom : ∀ (U V) (HVU : V ⊆ U), is_ring_hom (res U V HVU))
instance (α : Type u) [topological_space α] : has_coe_to_fun (presheaf_of_rings α) :=
{ F := λ _, opens α → Type v,
coe := λ F, (presheaf_of_rings.to_presheaf F).F }
attribute [instance] presheaf_of_rings.Fring
attribute [instance] presheaf_of_rings.res_is_ring_hom
namespace presheaf_of_rings
variables {α : Type u} {β : Type v} [topological_space α] [topological_space β]
-- Morphism of presheaf of rings.
structure morphism (F G : presheaf_of_rings α)
extends presheaf.morphism F.to_presheaf G.to_presheaf :=
(ring_homs : ∀ (U), is_ring_hom (map U))
infix `⟶`:80 := morphism
def identity (F : presheaf_of_rings α) : F ⟶ F :=
{ ring_homs := λ U, is_ring_hom.id,
..presheaf.id F.to_presheaf }
-- Isomorphic presheaves of rings.
structure iso (F G : presheaf_of_rings α) :=
(mor : F ⟶ G)
(inv : G ⟶ F)
(mor_inv_id : mor.to_morphism ⊚ inv.to_morphism = presheaf.id F.to_presheaf)
(inv_mor_id : inv.to_morphism ⊚ mor.to_morphism = presheaf.id G.to_presheaf)
infix `≅`:80 := λ A B, nonempty (iso A B)
-- Equality lemma
lemma presheaf_of_rings_eq_of_subset_eq (F : presheaf_of_rings α) (U V : opens α)
: U = V → ring_equiv (F U) (F V) :=
λ h, by rw h; by apply ring_equiv.refl
end presheaf_of_rings
|
93a5260199f2bd6e3b63d3f7ab281c639fe33401 | aa5a655c05e5359a70646b7154e7cac59f0b4132 | /tests/lean/run/subst.lean | 9ef6b778a554372991fbad54723e539fa6d70ab3 | [
"Apache-2.0"
] | permissive | lambdaxymox/lean4 | ae943c960a42247e06eff25c35338268d07454cb | 278d47c77270664ef29715faab467feac8a0f446 | refs/heads/master | 1,677,891,867,340 | 1,612,500,005,000 | 1,612,500,005,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,262 | lean |
universes u
def f1 (n m : Nat) (x : Fin n) (h : n = m) : Fin m :=
h ▸ x
def f2 (n m : Nat) (x : Fin n) (h : m = n) : Fin m :=
h ▸ x
theorem ex1 {α : Sort u} {a b c : α} (h₁ : a = b) (h₂ : b = c) : a = c :=
h₂ ▸ h₁
theorem ex2 {α : Sort u} {a b : α} (h : a = b) : b = a :=
h ▸ rfl
theorem ex3 {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : r a b) (h₂ : b = c) : r a c :=
h₂ ▸ h₁
theorem ex3b {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : r a b) (h₂ : b = c) : r a c :=
h₂.symm ▸ h₁
theorem ex3c {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : r a b) (h₂ : b = c) : r a c :=
h₂.symm.symm ▸ h₁
theorem ex4 {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : a = b) (h₂ : r b c) : r a c :=
h₁ ▸ h₂
theorem ex5 {p : Prop} (h : p = True) : p :=
h ▸ trivial
theorem ex6 {p : Prop} (h : p = False) : ¬p :=
fun hp => h ▸ hp
theorem ex7 {α} {a b c d : α} (h₁ : a = c) (h₂ : b = d) (h₃ : c ≠ d) : a ≠ b :=
h₁ ▸ h₂ ▸ h₃
theorem ex8 (n m k : Nat) (h : Nat.succ n + m = Nat.succ n + k) : Nat.succ (n + m) = Nat.succ (n + k) :=
Nat.succAdd .. ▸ Nat.succAdd .. ▸ h
theorem ex9 (a b : Nat) (h₁ : a = a + b) (h₂ : a = b) : a = b + a :=
h₂ ▸ h₁
theorem ex10 (a b : Nat) (h : a = b) : b = a :=
h ▸ rfl
def ex11 {α : Type u} {n : Nat} (a : Array α) (i : Nat) (h₁ : a.size = n) (h₂ : i < n) : α :=
a.get ⟨i, h₁ ▸ h₂⟩
theorem ex12 {α : Type u} {n : Nat}
(a b : Array α)
(hsz₁ : a.size = n) (hsz₂ : b.size = n)
(h : ∀ (i : Nat) (hi : i < n), a.getLit i hsz₁ hi = b.getLit i hsz₂ hi) : a = b :=
Array.ext a b (hsz₁.trans hsz₂.symm) fun i hi₁ hi₂ => h i (hsz₁ ▸ hi₁)
def toArrayLit {α : Type u} (a : Array α) (n : Nat) (hsz : a.size = n) : Array α :=
List.toArray $ Array.toListLitAux a n hsz n (hsz ▸ Nat.leRefl _) []
partial def isEqvAux {α} (a b : Array α) (hsz : a.size = b.size) (p : α → α → Bool) (i : Nat) : Bool :=
if h : i < a.size then
let aidx : Fin a.size := ⟨i, h⟩
let bidx : Fin b.size := ⟨i, hsz ▸ h⟩
match p (a.get aidx) (b.get bidx) with
| true => isEqvAux a b hsz p (i+1)
| false => false
else
true
|
9291ad3672a47c441a177c83f264c0acd9c1fb0f | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/group_theory/free_group.lean | 4bfc175984254f4433d0e4f48864e79aa3bae602 | [
"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 | 32,603 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import data.fintype.basic
import group_theory.subgroup
/-!
# Free groups
This file defines free groups over a type. Furthermore, it is shown that the free group construction
is an instance of a monad. For the result that `free_group` is the left adjoint to the forgetful
functor from groups to types, see `algebra/category/Group/adjunctions`.
## Main definitions
* `free_group`: the free group associated to a type `α` defined as the words over `a : α × bool `
modulo the relation `a * x * x⁻¹ * b = a * b`.
* `mk`: the canonical quotient map `list (α × bool) → free_group α`.
* `of`: the canoical injection `α → free_group α`.
* `lift f`: the canonical group homomorphism `free_group α →* G` given a group `G` and a
function `f : α → G`.
## Main statements
* `church_rosser`: The Church-Rosser theorem for word reduction (also known as Newman's diamond
lemma).
* `free_group_unit_equiv_int`: The free group over the one-point type is isomorphic to the integers.
* The free group construction is an instance of a monad.
## Implementation details
First we introduce the one step reduction relation `free_group.red.step`:
`w * x * x⁻¹ * v ~> w * v`, its reflexive transitive closure `free_group.red.trans`
and prove that its join is an equivalence relation. Then we introduce `free_group α` as a quotient
over `free_group.red.step`.
## Tags
free group, Newman's diamond lemma, Church-Rosser theorem
-/
open relation
universes u v w
variables {α : Type u}
local attribute [simp] list.append_eq_has_append
namespace free_group
variables {L L₁ L₂ L₃ L₄ : list (α × bool)}
/-- Reduction step: `w * x * x⁻¹ * v ~> w * v` -/
inductive red.step : list (α × bool) → list (α × bool) → Prop
| bnot {L₁ L₂ x b} : red.step (L₁ ++ (x, b) :: (x, bnot b) :: L₂) (L₁ ++ L₂)
attribute [simp] red.step.bnot
/-- Reflexive-transitive closure of red.step -/
def red : list (α × bool) → list (α × bool) → Prop := refl_trans_gen red.step
@[refl] lemma red.refl : red L L := refl_trans_gen.refl
@[trans] lemma red.trans : red L₁ L₂ → red L₂ L₃ → red L₁ L₃ := refl_trans_gen.trans
namespace red
/-- Predicate asserting that word `w₁` can be reduced to `w₂` in one step, i.e. there are words
`w₃ w₄` and letter `x` such that `w₁ = w₃xx⁻¹w₄` and `w₂ = w₃w₄` -/
theorem step.length : ∀ {L₁ L₂ : list (α × bool)}, step L₁ L₂ → L₂.length + 2 = L₁.length
| _ _ (@red.step.bnot _ L1 L2 x b) := by rw [list.length_append, list.length_append]; refl
@[simp] lemma step.bnot_rev {x b} : step (L₁ ++ (x, bnot b) :: (x, b) :: L₂) (L₁ ++ L₂) :=
by cases b; from step.bnot
@[simp] lemma step.cons_bnot {x b} : red.step ((x, b) :: (x, bnot b) :: L) L :=
@step.bnot _ [] _ _ _
@[simp] lemma step.cons_bnot_rev {x b} : red.step ((x, bnot b) :: (x, b) :: L) L :=
@red.step.bnot_rev _ [] _ _ _
theorem step.append_left : ∀ {L₁ L₂ L₃ : list (α × bool)}, step L₂ L₃ → step (L₁ ++ L₂) (L₁ ++ L₃)
| _ _ _ red.step.bnot := by rw [← list.append_assoc, ← list.append_assoc]; constructor
theorem step.cons {x} (H : red.step L₁ L₂) : red.step (x :: L₁) (x :: L₂) :=
@step.append_left _ [x] _ _ H
theorem step.append_right : ∀ {L₁ L₂ L₃ : list (α × bool)}, step L₁ L₂ → step (L₁ ++ L₃) (L₂ ++ L₃)
| _ _ _ red.step.bnot := by simp
lemma not_step_nil : ¬ step [] L :=
begin
generalize h' : [] = L',
assume h,
cases h with L₁ L₂,
simp [list.nil_eq_append_iff] at h',
contradiction
end
lemma step.cons_left_iff {a : α} {b : bool} :
step ((a, b) :: L₁) L₂ ↔ (∃L, step L₁ L ∧ L₂ = (a, b) :: L) ∨ (L₁ = (a, bnot b)::L₂) :=
begin
split,
{ generalize hL : ((a, b) :: L₁ : list _) = L,
assume h,
rcases h with ⟨_ | ⟨p, s'⟩, e, a', b'⟩,
{ simp at hL, simp [*] },
{ simp at hL,
rcases hL with ⟨rfl, rfl⟩,
refine or.inl ⟨s' ++ e, step.bnot, _⟩,
simp } },
{ assume h,
rcases h with ⟨L, h, rfl⟩ | rfl,
{ exact step.cons h },
{ exact step.cons_bnot } }
end
lemma not_step_singleton : ∀ {p : α × bool}, ¬ step [p] L
| (a, b) := by simp [step.cons_left_iff, not_step_nil]
lemma step.cons_cons_iff : ∀{p : α × bool}, step (p :: L₁) (p :: L₂) ↔ step L₁ L₂ :=
by simp [step.cons_left_iff, iff_def, or_imp_distrib] {contextual := tt}
lemma step.append_left_iff : ∀L, step (L ++ L₁) (L ++ L₂) ↔ step L₁ L₂
| [] := by simp
| (p :: l) := by simp [step.append_left_iff l, step.cons_cons_iff]
private theorem step.diamond_aux : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)} {x1 b1 x2 b2},
L₁ ++ (x1, b1) :: (x1, bnot b1) :: L₂ = L₃ ++ (x2, b2) :: (x2, bnot b2) :: L₄ →
L₁ ++ L₂ = L₃ ++ L₄ ∨ ∃ L₅, red.step (L₁ ++ L₂) L₅ ∧ red.step (L₃ ++ L₄) L₅
| [] _ [] _ _ _ _ _ H := by injections; subst_vars; simp
| [] _ [(x3,b3)] _ _ _ _ _ H := by injections; subst_vars; simp
| [(x3,b3)] _ [] _ _ _ _ _ H := by injections; subst_vars; simp
| [] _ ((x3,b3)::(x4,b4)::tl) _ _ _ _ _ H :=
by injections; subst_vars; simp; right; exact ⟨_, red.step.bnot, red.step.cons_bnot⟩
| ((x3,b3)::(x4,b4)::tl) _ [] _ _ _ _ _ H :=
by injections; subst_vars; simp; right; exact ⟨_, red.step.cons_bnot, red.step.bnot⟩
| ((x3,b3)::tl) _ ((x4,b4)::tl2) _ _ _ _ _ H :=
let ⟨H1, H2⟩ := list.cons.inj H in
match step.diamond_aux H2 with
| or.inl H3 := or.inl $ by simp [H1, H3]
| or.inr ⟨L₅, H3, H4⟩ := or.inr
⟨_, step.cons H3, by simpa [H1] using step.cons H4⟩
end
theorem step.diamond : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)},
red.step L₁ L₃ → red.step L₂ L₄ → L₁ = L₂ →
L₃ = L₄ ∨ ∃ L₅, red.step L₃ L₅ ∧ red.step L₄ L₅
| _ _ _ _ red.step.bnot red.step.bnot H := step.diamond_aux H
lemma step.to_red : step L₁ L₂ → red L₁ L₂ :=
refl_trans_gen.single
/-- Church-Rosser theorem for word reduction: If `w1 w2 w3` are words such that `w1` reduces to `w2`
and `w3` respectively, then there is a word `w4` such that `w2` and `w3` reduce to `w4`
respectively. This is also known as Newman's diamond lemma. -/
theorem church_rosser : red L₁ L₂ → red L₁ L₃ → join red L₂ L₃ :=
relation.church_rosser (assume a b c hab hac,
match b, c, red.step.diamond hab hac rfl with
| b, _, or.inl rfl := ⟨b, by refl, by refl⟩
| b, c, or.inr ⟨d, hbd, hcd⟩ := ⟨d, refl_gen.single hbd, hcd.to_red⟩
end)
lemma cons_cons {p} : red L₁ L₂ → red (p :: L₁) (p :: L₂) :=
refl_trans_gen_lift (list.cons p) (assume a b, step.cons)
lemma cons_cons_iff (p) : red (p :: L₁) (p :: L₂) ↔ red L₁ L₂ :=
iff.intro
begin
generalize eq₁ : (p :: L₁ : list _) = LL₁,
generalize eq₂ : (p :: L₂ : list _) = LL₂,
assume h,
induction h using relation.refl_trans_gen.head_induction_on
with L₁ L₂ h₁₂ h ih
generalizing L₁ L₂,
{ subst_vars, cases eq₂, constructor },
{ subst_vars,
cases p with a b,
rw [step.cons_left_iff] at h₁₂,
rcases h₁₂ with ⟨L, h₁₂, rfl⟩ | rfl,
{ exact (ih rfl rfl).head h₁₂ },
{ exact (cons_cons h).tail step.cons_bnot_rev } }
end
cons_cons
lemma append_append_left_iff : ∀L, red (L ++ L₁) (L ++ L₂) ↔ red L₁ L₂
| [] := iff.rfl
| (p :: L) := by simp [append_append_left_iff L, cons_cons_iff]
lemma append_append (h₁ : red L₁ L₃) (h₂ : red L₂ L₄) : red (L₁ ++ L₂) (L₃ ++ L₄) :=
(refl_trans_gen_lift (λL, L ++ L₂) (assume a b, step.append_right) h₁).trans
((append_append_left_iff _).2 h₂)
lemma to_append_iff : red L (L₁ ++ L₂) ↔ (∃L₃ L₄, L = L₃ ++ L₄ ∧ red L₃ L₁ ∧ red L₄ L₂) :=
iff.intro
begin
generalize eq : L₁ ++ L₂ = L₁₂,
assume h,
induction h with L' L₁₂ hLL' h ih generalizing L₁ L₂,
{ exact ⟨_, _, eq.symm, by refl, by refl⟩ },
{ cases h with s e a b,
rcases list.append_eq_append_iff.1 eq with ⟨s', rfl, rfl⟩ | ⟨e', rfl, rfl⟩,
{ have : L₁ ++ (s' ++ ((a, b) :: (a, bnot b) :: e)) =
(L₁ ++ s') ++ ((a, b) :: (a, bnot b) :: e),
{ simp },
rcases ih this with ⟨w₁, w₂, rfl, h₁, h₂⟩,
exact ⟨w₁, w₂, rfl, h₁, h₂.tail step.bnot⟩ },
{ have : (s ++ ((a, b) :: (a, bnot b) :: e')) ++ L₂ =
s ++ ((a, b) :: (a, bnot b) :: (e' ++ L₂)),
{ simp },
rcases ih this with ⟨w₁, w₂, rfl, h₁, h₂⟩,
exact ⟨w₁, w₂, rfl, h₁.tail step.bnot, h₂⟩ }, }
end
(assume ⟨L₃, L₄, eq, h₃, h₄⟩, eq.symm ▸ append_append h₃ h₄)
/-- The empty word `[]` only reduces to itself. -/
theorem nil_iff : red [] L ↔ L = [] :=
refl_trans_gen_iff_eq (assume l, red.not_step_nil)
/-- A letter only reduces to itself. -/
theorem singleton_iff {x} : red [x] L₁ ↔ L₁ = [x] :=
refl_trans_gen_iff_eq (assume l, not_step_singleton)
/-- If `x` is a letter and `w` is a word such that `xw` reduces to the empty word, then `w` reduces
to `x⁻¹` -/
theorem cons_nil_iff_singleton {x b} : red ((x, b) :: L) [] ↔ red L [(x, bnot b)] :=
iff.intro
(assume h,
have h₁ : red ((x, bnot b) :: (x, b) :: L) [(x, bnot b)], from cons_cons h,
have h₂ : red ((x, bnot b) :: (x, b) :: L) L, from refl_trans_gen.single step.cons_bnot_rev,
let ⟨L', h₁, h₂⟩ := church_rosser h₁ h₂ in
by rw [singleton_iff] at h₁; subst L'; assumption)
(assume h, (cons_cons h).tail step.cons_bnot)
theorem red_iff_irreducible {x1 b1 x2 b2} (h : (x1, b1) ≠ (x2, b2)) :
red [(x1, bnot b1), (x2, b2)] L ↔ L = [(x1, bnot b1), (x2, b2)] :=
begin
apply refl_trans_gen_iff_eq,
generalize eq : [(x1, bnot b1), (x2, b2)] = L',
assume L h',
cases h',
simp [list.cons_eq_append_iff, list.nil_eq_append_iff] at eq,
rcases eq with ⟨rfl, ⟨rfl, rfl⟩, ⟨rfl, rfl⟩, rfl⟩, subst_vars,
simp at h,
contradiction
end
/-- If `x` and `y` are distinct letters and `w₁ w₂` are words such that `xw₁` reduces to `yw₂`, then
`w₁` reduces to `x⁻¹yw₂`. -/
theorem inv_of_red_of_ne {x1 b1 x2 b2}
(H1 : (x1, b1) ≠ (x2, b2))
(H2 : red ((x1, b1) :: L₁) ((x2, b2) :: L₂)) :
red L₁ ((x1, bnot b1) :: (x2, b2) :: L₂) :=
begin
have : red ((x1, b1) :: L₁) ([(x2, b2)] ++ L₂), from H2,
rcases to_append_iff.1 this with ⟨_ | ⟨p, L₃⟩, L₄, eq, h₁, h₂⟩,
{ simp [nil_iff] at h₁, contradiction },
{ cases eq,
show red (L₃ ++ L₄) ([(x1, bnot b1), (x2, b2)] ++ L₂),
apply append_append _ h₂,
have h₁ : red ((x1, bnot b1) :: (x1, b1) :: L₃) [(x1, bnot b1), (x2, b2)],
{ exact cons_cons h₁ },
have h₂ : red ((x1, bnot b1) :: (x1, b1) :: L₃) L₃,
{ exact step.cons_bnot_rev.to_red },
rcases church_rosser h₁ h₂ with ⟨L', h₁, h₂⟩,
rw [red_iff_irreducible H1] at h₁,
rwa [h₁] at h₂ }
end
theorem step.sublist (H : red.step L₁ L₂) : L₂ <+ L₁ :=
by cases H; simp; constructor; constructor; refl
/-- If `w₁ w₂` are words such that `w₁` reduces to `w₂`, then `w₂` is a sublist of `w₁`. -/
theorem sublist : red L₁ L₂ → L₂ <+ L₁ :=
refl_trans_gen_of_transitive_reflexive
(λl, list.sublist.refl l) (λa b c hab hbc, list.sublist.trans hbc hab) (λa b, red.step.sublist)
theorem sizeof_of_step : ∀ {L₁ L₂ : list (α × bool)}, step L₁ L₂ → L₂.sizeof < L₁.sizeof
| _ _ (@step.bnot _ L1 L2 x b) :=
begin
induction L1 with hd tl ih,
case list.nil
{ dsimp [list.sizeof],
have H : 1 + sizeof (x, b) + (1 + sizeof (x, bnot b) + list.sizeof L2)
= (list.sizeof L2 + 1) + (sizeof (x, b) + sizeof (x, bnot b) + 1),
{ ac_refl },
rw H,
exact nat.le_add_right _ _ },
case list.cons
{ dsimp [list.sizeof],
exact nat.add_lt_add_left ih _ }
end
theorem length (h : red L₁ L₂) : ∃ n, L₁.length = L₂.length + 2 * n :=
begin
induction h with L₂ L₃ h₁₂ h₂₃ ih,
{ exact ⟨0, rfl⟩ },
{ rcases ih with ⟨n, eq⟩,
existsi (1 + n),
simp [mul_add, eq, (step.length h₂₃).symm, add_assoc] }
end
theorem antisymm (h₁₂ : red L₁ L₂) : red L₂ L₁ → L₁ = L₂ :=
match L₁, h₁₂.cases_head with
| _, or.inl rfl := assume h, rfl
| L₁, or.inr ⟨L₃, h₁₃, h₃₂⟩ := assume h₂₁,
let ⟨n, eq⟩ := length (h₃₂.trans h₂₁) in
have list.length L₃ + 0 = list.length L₃ + (2 * n + 2),
by simpa [(step.length h₁₃).symm, add_comm, add_assoc] using eq,
(nat.no_confusion $ nat.add_left_cancel this)
end
end red
theorem equivalence_join_red : equivalence (join (@red α)) :=
equivalence_join_refl_trans_gen $ assume a b c hab hac,
(match b, c, red.step.diamond hab hac rfl with
| b, _, or.inl rfl := ⟨b, by refl, by refl⟩
| b, c, or.inr ⟨d, hbd, hcd⟩ := ⟨d, refl_gen.single hbd, refl_trans_gen.single hcd⟩
end)
theorem join_red_of_step (h : red.step L₁ L₂) : join red L₁ L₂ :=
join_of_single reflexive_refl_trans_gen h.to_red
theorem eqv_gen_step_iff_join_red : eqv_gen red.step L₁ L₂ ↔ join red L₁ L₂ :=
iff.intro
(assume h,
have eqv_gen (join red) L₁ L₂ := eqv_gen_mono (assume a b, join_red_of_step) h,
(eqv_gen_iff_of_equivalence $ equivalence_join_red).1 this)
(join_of_equivalence (eqv_gen.is_equivalence _) $ assume a b,
refl_trans_gen_of_equivalence (eqv_gen.is_equivalence _) eqv_gen.rel)
end free_group
/-- The free group over a type, i.e. the words formed by the elements of the type and their formal
inverses, quotient by one step reduction. -/
def free_group (α : Type u) : Type u :=
quot $ @free_group.red.step α
namespace free_group
variables {α} {L L₁ L₂ L₃ L₄ : list (α × bool)}
/-- The canonical map from `list (α × bool)` to the free group on `α`. -/
def mk (L) : free_group α := quot.mk red.step L
@[simp] lemma quot_mk_eq_mk : quot.mk red.step L = mk L := rfl
@[simp] lemma quot_lift_mk (β : Type v) (f : list (α × bool) → β)
(H : ∀ L₁ L₂, red.step L₁ L₂ → f L₁ = f L₂) :
quot.lift f H (mk L) = f L := rfl
@[simp] lemma quot_lift_on_mk (β : Type v) (f : list (α × bool) → β)
(H : ∀ L₁ L₂, red.step L₁ L₂ → f L₁ = f L₂) :
quot.lift_on (mk L) f H = f L := rfl
instance : has_one (free_group α) := ⟨mk []⟩
lemma one_eq_mk : (1 : free_group α) = mk [] := rfl
instance : inhabited (free_group α) := ⟨1⟩
instance : has_mul (free_group α) :=
⟨λ x y, quot.lift_on x
(λ L₁, quot.lift_on y (λ L₂, mk $ L₁ ++ L₂) (λ L₂ L₃ H, quot.sound $ red.step.append_left H))
(λ L₁ L₂ H, quot.induction_on y $ λ L₃, quot.sound $ red.step.append_right H)⟩
@[simp] lemma mul_mk : mk L₁ * mk L₂ = mk (L₁ ++ L₂) := rfl
instance : has_inv (free_group α) :=
⟨λx, quot.lift_on x (λ L, mk (L.map $ λ x : α × bool, (x.1, bnot x.2)).reverse)
(assume a b h, quot.sound $ by cases h; simp)⟩
@[simp] lemma inv_mk : (mk L)⁻¹ = mk (L.map $ λ x : α × bool, (x.1, bnot x.2)).reverse := rfl
instance : group (free_group α) :=
{ mul := (*),
one := 1,
inv := has_inv.inv,
mul_assoc := by rintros ⟨L₁⟩ ⟨L₂⟩ ⟨L₃⟩; simp,
one_mul := by rintros ⟨L⟩; refl,
mul_one := by rintros ⟨L⟩; simp [one_eq_mk],
mul_left_inv := by rintros ⟨L⟩; exact (list.rec_on L rfl $
λ ⟨x, b⟩ tl ih, eq.trans (quot.sound $ by simp [one_eq_mk]) ih) }
/-- `of` is the canonical injection from the type to the free group over that type by sending each
element to the equivalence class of the letter that is the element. -/
def of (x : α) : free_group α :=
mk [(x, tt)]
theorem red.exact : mk L₁ = mk L₂ ↔ join red L₁ L₂ :=
calc (mk L₁ = mk L₂) ↔ eqv_gen red.step L₁ L₂ : iff.intro (quot.exact _) quot.eqv_gen_sound
... ↔ join red L₁ L₂ : eqv_gen_step_iff_join_red
/-- The canonical injection from the type to the free group is an injection. -/
theorem of_injective : function.injective (@of α) :=
λ _ _ H, let ⟨L₁, hx, hy⟩ := red.exact.1 H in
by simp [red.singleton_iff] at hx hy; cc
section lift
variables {β : Type v} [group β] (f : α → β) {x y : free_group α}
/-- Given `f : α → β` with `β` a group, the canonical map `list (α × bool) → β` -/
def lift.aux : list (α × bool) → β :=
λ L, list.prod $ L.map $ λ x, cond x.2 (f x.1) (f x.1)⁻¹
theorem red.step.lift {f : α → β} (H : red.step L₁ L₂) :
lift.aux f L₁ = lift.aux f L₂ :=
by cases H with _ _ _ b; cases b; simp [lift.aux]
/-- If `β` is a group, then any function from `α` to `β`
extends uniquely to a group homomorphism from
the free group over `α` to `β` -/
@[simps symm_apply]
def lift : (α → β) ≃ (free_group α →* β) :=
{ to_fun := λ f,
monoid_hom.mk' (quot.lift (lift.aux f) $ λ L₁ L₂, red.step.lift) $ begin
rintros ⟨L₁⟩ ⟨L₂⟩, simp [lift.aux],
end,
inv_fun := λ g, g ∘ of,
left_inv := λ f, one_mul _,
right_inv := λ g, monoid_hom.ext $ begin
rintros ⟨L⟩,
apply list.rec_on L,
{ exact g.map_one.symm, },
{ rintros ⟨x, _ | _⟩ t (ih : _ = g (mk t)),
{ show _ = g ((of x)⁻¹ * mk t),
simpa [lift.aux] using ih },
{ show _ = g (of x * mk t),
simpa [lift.aux] using ih }, },
end }
variable {f}
@[simp] lemma lift.mk : lift f (mk L) =
list.prod (L.map $ λ x, cond x.2 (f x.1) (f x.1)⁻¹) :=
rfl
@[simp] lemma lift.of {x} : lift f (of x) = f x :=
one_mul _
theorem lift.unique (g : free_group α →* β)
(hg : ∀ x, g (of x) = f x) : ∀{x}, g x = lift f x :=
monoid_hom.congr_fun $ (lift.symm_apply_eq).mp (funext hg : g ∘ of = f)
/-- Two homomorphisms out of a free group are equal if they are equal on generators.
See note [partially-applied ext lemmas]. -/
@[ext]
lemma ext_hom {G : Type*} [group G] (f g : free_group α →* G) (h : ∀ a, f (of a) = g (of a)) :
f = g :=
lift.symm.injective $ funext h
theorem lift.of_eq (x : free_group α) : lift of x = x :=
monoid_hom.congr_fun (lift.apply_symm_apply (monoid_hom.id _)) x
theorem lift.range_subset {s : subgroup β} (H : set.range f ⊆ s) :
set.range (lift f) ⊆ s :=
by rintros _ ⟨⟨L⟩, rfl⟩; exact list.rec_on L s.one_mem
(λ ⟨x, b⟩ tl ih, bool.rec_on b
(by simp at ih ⊢; from s.mul_mem
(s.inv_mem $ H ⟨x, rfl⟩) ih)
(by simp at ih ⊢; from s.mul_mem (H ⟨x, rfl⟩) ih))
theorem closure_subset {G : Type*} [group G] {s : set G} {t : subgroup G}
(h : s ⊆ t) : subgroup.closure s ≤ t :=
begin
simp only [h, subgroup.closure_le],
end
theorem lift.range_eq_closure :
set.range (lift f) = subgroup.closure (set.range f) :=
set.subset.antisymm
(lift.range_subset subgroup.subset_closure)
begin
suffices : (subgroup.closure (set.range f)) ≤ monoid_hom.range (lift f),
simpa,
rw subgroup.closure_le,
rintros y ⟨x, hx⟩,
exact ⟨of x, by simpa⟩
end
end lift
section map
variables {β : Type v} (f : α → β) {x y : free_group α}
/-- Given `f : α → β`, the canonical map `list (α × bool) → list (β × bool)`. -/
def map.aux (L : list (α × bool)) : list (β × bool) :=
L.map $ λ x, (f x.1, x.2)
/-- Any function from `α` to `β` extends uniquely
to a group homomorphism from the free group
over `α` to the free group over `β`. Note that this is the bare function;
for the group homomorphism use `map`. -/
def map.to_fun (x : free_group α) : free_group β :=
x.lift_on (λ L, mk $ map.aux f L) $
λ L₁ L₂ H, quot.sound $ by cases H; simp [map.aux]
/-- Any function from `α` to `β` extends uniquely
to a group homomorphism from the free group
ver `α` to the free group over `β`. -/
def map : free_group α →* free_group β := monoid_hom.mk' (map.to_fun f)
begin
rintros ⟨L₁⟩ ⟨L₂⟩,
simp [map.to_fun, map.aux]
end
--by rintros ⟨L₁⟩ ⟨L₂⟩; simp [map, map.aux]
variable {f}
@[simp] lemma map.mk : map f (mk L) = mk (L.map (λ x, (f x.1, x.2))) :=
rfl
@[simp] lemma map.id : map id x = x :=
have H1 : (λ (x : α × bool), x) = id := rfl,
by rcases x with ⟨L⟩; simp [H1]
@[simp] lemma map.id' : map (λ z, z) x = x := map.id
theorem map.comp {γ : Type w} {f : α → β} {g : β → γ} {x} :
map g (map f x) = map (g ∘ f) x :=
by rcases x with ⟨L⟩; simp
@[simp] lemma map.of {x} : map f (of x) = of (f x) := rfl
theorem map.unique (g : free_group α →* free_group β)
(hg : ∀ x, g (of x) = of (f x)) : ∀{x}, g x = map f x :=
by rintros ⟨L⟩; exact list.rec_on L g.map_one
(λ ⟨x, b⟩ t (ih : g (mk t) = map f (mk t)), bool.rec_on b
(show g ((of x)⁻¹ * mk t) = map f ((of x)⁻¹ * mk t),
by simp [g.map_mul, g.map_inv, hg, ih])
(show g (of x * mk t) = map f (of x * mk t),
by simp [g.map_mul, hg, ih]))
/-- Equivalent types give rise to equivalent free groups. -/
def free_group_congr {α β} (e : α ≃ β) : free_group α ≃ free_group β :=
⟨map e, map e.symm,
λ x, by simp [function.comp, map.comp],
λ x, by simp [function.comp, map.comp]⟩
theorem map_eq_lift : map f x = lift (of ∘ f) x :=
eq.symm $ map.unique _ $ λ x, by simp
end map
section prod
variables [group α] (x y : free_group α)
/-- If `α` is a group, then any function from `α` to `α`
extends uniquely to a homomorphism from the
free group over `α` to `α`. This is the multiplicative
version of `sum`. -/
def prod : free_group α →* α := lift id
variables {x y}
@[simp] lemma prod_mk :
prod (mk L) = list.prod (L.map $ λ x, cond x.2 x.1 x.1⁻¹) :=
rfl
@[simp] lemma prod.of {x : α} : prod (of x) = x :=
lift.of
lemma prod.unique (g : free_group α →* α)
(hg : ∀ x, g (of x) = x) {x} :
g x = prod x :=
lift.unique g hg
end prod
theorem lift_eq_prod_map {β : Type v} [group β] {f : α → β} {x} :
lift f x = prod (map f x) :=
begin
rw ←lift.unique (prod.comp (map f)),
{ refl },
{ simp }
end
section sum
variables [add_group α] (x y : free_group α)
/-- If `α` is a group, then any function from `α` to `α`
extends uniquely to a homomorphism from the
free group over `α` to `α`. This is the additive
version of `prod`. -/
def sum : α :=
@prod (multiplicative _) _ x
variables {x y}
@[simp] lemma sum_mk :
sum (mk L) = list.sum (L.map $ λ x, cond x.2 x.1 (-x.1)) :=
rfl
@[simp] lemma sum.of {x : α} : sum (of x) = x :=
prod.of
-- note: there are no bundled homs with different notation in the domain and codomain, so we copy
-- these manually
@[simp] lemma sum.map_mul : sum (x * y) = sum x + sum y :=
(@prod (multiplicative _) _).map_mul _ _
@[simp] lemma sum.map_one : sum (1:free_group α) = 0 :=
(@prod (multiplicative _) _).map_one
@[simp] lemma sum.map_inv : sum x⁻¹ = -sum x :=
(@prod (multiplicative _) _).map_inv _
end sum
/-- The bijection between the free group on the empty type, and a type with one element. -/
def free_group_empty_equiv_unit : free_group empty ≃ unit :=
{ to_fun := λ _, (),
inv_fun := λ _, 1,
left_inv := by rintros ⟨_ | ⟨⟨⟨⟩, _⟩, _⟩⟩; refl,
right_inv := λ ⟨⟩, rfl }
/-- The bijection between the free group on a singleton, and the integers. -/
def free_group_unit_equiv_int : free_group unit ≃ ℤ :=
{ to_fun := λ x,
sum begin revert x, apply monoid_hom.to_fun,
apply map (λ _, (1 : ℤ)),
end,
inv_fun := λ x, of () ^ x,
left_inv :=
begin
rintros ⟨L⟩,
refine list.rec_on L rfl _,
exact (λ ⟨⟨⟩, b⟩ tl ih, by cases b; simp [gpow_add] at ih ⊢; rw ih; refl),
end,
right_inv :=
λ x, int.induction_on x (by simp)
(λ i ih, by simp at ih; simp [gpow_add, ih])
(λ i ih, by simp at ih; simp [gpow_add, ih, sub_eq_add_neg, -int.add_neg_one])
}
section category
variables {β : Type u}
instance : monad free_group.{u} :=
{ pure := λ α, of,
map := λ α β f, (map f),
bind := λ α β x f, lift f x }
@[elab_as_eliminator]
protected theorem induction_on
{C : free_group α → Prop}
(z : free_group α)
(C1 : C 1)
(Cp : ∀ x, C $ pure x)
(Ci : ∀ x, C (pure x) → C (pure x)⁻¹)
(Cm : ∀ x y, C x → C y → C (x * y)) : C z :=
quot.induction_on z $ λ L, list.rec_on L C1 $ λ ⟨x, b⟩ tl ih,
bool.rec_on b (Cm _ _ (Ci _ $ Cp x) ih) (Cm _ _ (Cp x) ih)
@[simp] lemma map_pure (f : α → β) (x : α) : f <$> (pure x : free_group α) = pure (f x) :=
map.of
@[simp] lemma map_one (f : α → β) : f <$> (1 : free_group α) = 1 :=
(map f).map_one
@[simp] lemma map_mul (f : α → β) (x y : free_group α) : f <$> (x * y) = f <$> x * f <$> y :=
(map f).map_mul x y
@[simp] lemma map_inv (f : α → β) (x : free_group α) : f <$> (x⁻¹) = (f <$> x)⁻¹ :=
(map f).map_inv x
@[simp] lemma pure_bind (f : α → free_group β) (x) : pure x >>= f = f x :=
lift.of
@[simp] lemma one_bind (f : α → free_group β) : 1 >>= f = 1 :=
(lift f).map_one
@[simp] lemma mul_bind (f : α → free_group β) (x y : free_group α) :
x * y >>= f = (x >>= f) * (y >>= f) :=
(lift f).map_mul _ _
@[simp] lemma inv_bind (f : α → free_group β) (x : free_group α) : x⁻¹ >>= f = (x >>= f)⁻¹ :=
(lift f).map_inv _
instance : is_lawful_monad free_group.{u} :=
{ id_map := λ α x, free_group.induction_on x (map_one id) (λ x, map_pure id x)
(λ x ih, by rw [map_inv, ih]) (λ x y ihx ihy, by rw [map_mul, ihx, ihy]),
pure_bind := λ α β x f, pure_bind f x,
bind_assoc := λ α β γ x f g, free_group.induction_on x
(by iterate 3 { rw one_bind }) (λ x, by iterate 2 { rw pure_bind })
(λ x ih, by iterate 3 { rw inv_bind }; rw ih)
(λ x y ihx ihy, by iterate 3 { rw mul_bind }; rw [ihx, ihy]),
bind_pure_comp_eq_map := λ α β f x, free_group.induction_on x
(by rw [one_bind, map_one]) (λ x, by rw [pure_bind, map_pure])
(λ x ih, by rw [inv_bind, map_inv, ih]) (λ x y ihx ihy, by rw [mul_bind, map_mul, ihx, ihy]) }
end category
section reduce
variable [decidable_eq α]
/-- The maximal reduction of a word. It is computable
iff `α` has decidable equality. -/
def reduce (L : list (α × bool)) : list (α × bool) :=
list.rec_on L [] $ λ hd1 tl1 ih,
list.cases_on ih [hd1] $ λ hd2 tl2,
if hd1.1 = hd2.1 ∧ hd1.2 = bnot hd2.2 then tl2
else hd1 :: hd2 :: tl2
@[simp] lemma reduce.cons (x) : reduce (x :: L) =
list.cases_on (reduce L) [x] (λ hd tl,
if x.1 = hd.1 ∧ x.2 = bnot hd.2 then tl
else x :: hd :: tl) := rfl
/-- The first theorem that characterises the function
`reduce`: a word reduces to its maximal reduction. -/
theorem reduce.red : red L (reduce L) :=
begin
induction L with hd1 tl1 ih,
case list.nil
{ constructor },
case list.cons
{ dsimp,
revert ih,
generalize htl : reduce tl1 = TL,
intro ih,
cases TL with hd2 tl2,
case list.nil
{ exact red.cons_cons ih },
case list.cons
{ dsimp,
by_cases h : hd1.fst = hd2.fst ∧ hd1.snd = bnot (hd2.snd),
{ rw [if_pos h],
transitivity,
{ exact red.cons_cons ih },
{ cases hd1, cases hd2, cases h,
dsimp at *, subst_vars,
exact red.step.cons_bnot_rev.to_red } },
{ rw [if_neg h],
exact red.cons_cons ih } } }
end
theorem reduce.not {p : Prop} :
∀ {L₁ L₂ L₃ : list (α × bool)} {x b}, reduce L₁ = L₂ ++ (x, b) :: (x, bnot b) :: L₃ → p
| [] L2 L3 _ _ := λ h, by cases L2; injections
| ((x,b)::L1) L2 L3 x' b' := begin
dsimp,
cases r : reduce L1,
{ dsimp, intro h,
have := congr_arg list.length h,
simp [-add_comm] at this,
exact absurd this dec_trivial },
cases hd with y c,
by_cases x = y ∧ b = bnot c; simp [h]; intro H,
{ rw H at r,
exact @reduce.not L1 ((y,c)::L2) L3 x' b' r },
rcases L2 with _|⟨a, L2⟩,
{ injections, subst_vars,
simp at h, cc },
{ refine @reduce.not L1 L2 L3 x' b' _,
injection H with _ H,
rw [r, H], refl }
end
/-- The second theorem that characterises the
function `reduce`: the maximal reduction of a word
only reduces to itself. -/
theorem reduce.min (H : red (reduce L₁) L₂) : reduce L₁ = L₂ :=
begin
induction H with L1 L' L2 H1 H2 ih,
{ refl },
{ cases H1 with L4 L5 x b,
exact reduce.not H2 }
end
/-- `reduce` is idempotent, i.e. the maximal reduction
of the maximal reduction of a word is the maximal
reduction of the word. -/
theorem reduce.idem : reduce (reduce L) = reduce L :=
eq.symm $ reduce.min reduce.red
theorem reduce.step.eq (H : red.step L₁ L₂) : reduce L₁ = reduce L₂ :=
let ⟨L₃, HR13, HR23⟩ := red.church_rosser reduce.red (reduce.red.head H) in
(reduce.min HR13).trans (reduce.min HR23).symm
/-- If a word reduces to another word, then they have
a common maximal reduction. -/
theorem reduce.eq_of_red (H : red L₁ L₂) : reduce L₁ = reduce L₂ :=
let ⟨L₃, HR13, HR23⟩ := red.church_rosser reduce.red (red.trans H reduce.red) in
(reduce.min HR13).trans (reduce.min HR23).symm
/-- If two words correspond to the same element in
the free group, then they have a common maximal
reduction. This is the proof that the function that
sends an element of the free group to its maximal
reduction is well-defined. -/
theorem reduce.sound (H : mk L₁ = mk L₂) : reduce L₁ = reduce L₂ :=
let ⟨L₃, H13, H23⟩ := red.exact.1 H in
(reduce.eq_of_red H13).trans (reduce.eq_of_red H23).symm
/-- If two words have a common maximal reduction,
then they correspond to the same element in the free group. -/
theorem reduce.exact (H : reduce L₁ = reduce L₂) : mk L₁ = mk L₂ :=
red.exact.2 ⟨reduce L₂, H ▸ reduce.red, reduce.red⟩
/-- A word and its maximal reduction correspond to
the same element of the free group. -/
theorem reduce.self : mk (reduce L) = mk L :=
reduce.exact reduce.idem
/-- If words `w₁ w₂` are such that `w₁` reduces to `w₂`,
then `w₂` reduces to the maximal reduction of `w₁`. -/
theorem reduce.rev (H : red L₁ L₂) : red L₂ (reduce L₁) :=
(reduce.eq_of_red H).symm ▸ reduce.red
/-- The function that sends an element of the free
group to its maximal reduction. -/
def to_word : free_group α → list (α × bool) :=
quot.lift reduce $ λ L₁ L₂ H, reduce.step.eq H
lemma to_word.mk : ∀{x : free_group α}, mk (to_word x) = x :=
by rintros ⟨L⟩; exact reduce.self
lemma to_word.inj : ∀(x y : free_group α), to_word x = to_word y → x = y :=
by rintros ⟨L₁⟩ ⟨L₂⟩; exact reduce.exact
/-- Constructive Church-Rosser theorem (compare `church_rosser`). -/
def reduce.church_rosser (H12 : red L₁ L₂) (H13 : red L₁ L₃) :
{ L₄ // red L₂ L₄ ∧ red L₃ L₄ } :=
⟨reduce L₁, reduce.rev H12, reduce.rev H13⟩
instance : decidable_eq (free_group α) :=
function.injective.decidable_eq to_word.inj
instance red.decidable_rel : decidable_rel (@red α)
| [] [] := is_true red.refl
| [] (hd2::tl2) := is_false $ λ H, list.no_confusion (red.nil_iff.1 H)
| ((x,b)::tl) [] := match red.decidable_rel tl [(x, bnot b)] with
| is_true H := is_true $ red.trans (red.cons_cons H) $
(@red.step.bnot _ [] [] _ _).to_red
| is_false H := is_false $ λ H2, H $ red.cons_nil_iff_singleton.1 H2
end
| ((x1,b1)::tl1) ((x2,b2)::tl2) := if h : (x1, b1) = (x2, b2)
then match red.decidable_rel tl1 tl2 with
| is_true H := is_true $ h ▸ red.cons_cons H
| is_false H := is_false $ λ H2, H $ h ▸ (red.cons_cons_iff _).1 $ H2
end
else match red.decidable_rel tl1 ((x1,bnot b1)::(x2,b2)::tl2) with
| is_true H := is_true $ (red.cons_cons H).tail red.step.cons_bnot
| is_false H := is_false $ λ H2, H $ red.inv_of_red_of_ne h H2
end
/-- A list containing every word that `w₁` reduces to. -/
def red.enum (L₁ : list (α × bool)) : list (list (α × bool)) :=
list.filter (λ L₂, red L₁ L₂) (list.sublists L₁)
theorem red.enum.sound (H : L₂ ∈ red.enum L₁) : red L₁ L₂ :=
list.of_mem_filter H
theorem red.enum.complete (H : red L₁ L₂) : L₂ ∈ red.enum L₁ :=
list.mem_filter_of_mem (list.mem_sublists.2 $ red.sublist H) H
instance : fintype { L₂ // red L₁ L₂ } :=
fintype.subtype (list.to_finset $ red.enum L₁) $
λ L₂, ⟨λ H, red.enum.sound $ list.mem_to_finset.1 H,
λ H, list.mem_to_finset.2 $ red.enum.complete H⟩
end reduce
end free_group
|
59c72a05514c12bb1a44d7e1e8b7c7ba6eeef488 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /tests/lean/run/CommandExtOverlap.lean | 4c3f604ace3fe8fbcf15e1f3160656b1a0a69856 | [
"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 | 324 | lean | syntax (name := mycheck) "#check" sepBy(term, ",") : command
open Lean
macro_rules (kind := mycheck)
| `(#check $es,*) =>
let cmds := es.getElems.map $ fun e => Syntax.node `Lean.Parser.Command.check #[mkAtom "#check", e]
pure $ mkNullNode cmds
#check true
#check true, true
#check true, 1, 3, fun (x : Nat) => x + 1
|
fecf82dfcdb860dd6abc01b8bf456cfce61de926 | 9b9a16fa2cb737daee6b2785474678b6fa91d6d4 | /src/category_theory/discrete_category.lean | 5c6840370bade4f8231840a6b5ce17a6d4db9753 | [
"Apache-2.0"
] | permissive | johoelzl/mathlib | 253f46daa30b644d011e8e119025b01ad69735c4 | 592e3c7a2dfbd5826919b4605559d35d4d75938f | refs/heads/master | 1,625,657,216,488 | 1,551,374,946,000 | 1,551,374,946,000 | 98,915,829 | 0 | 0 | Apache-2.0 | 1,522,917,267,000 | 1,501,524,499,000 | Lean | UTF-8 | Lean | false | false | 1,637 | 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 data.ulift
import category_theory.natural_transformation
import category_theory.isomorphism
import category_theory.functor_category
namespace category_theory
universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation
def discrete (α : Type u₁) := α
instance discrete_category (α : Type u₁) : small_category (discrete α) :=
{ hom := λ X Y, ulift (plift (X = Y)),
id := by tidy,
comp := by tidy }
variables {C : Type u₂} [𝒞 : category.{v₂} C]
include 𝒞
namespace functor
@[simp] def of_function {I : Type u₁} (F : I → C) : (discrete I) ⥤ C :=
{ obj := F,
map := λ X Y f, begin cases f, cases f, cases f, exact 𝟙 (F X) end }
end functor
namespace nat_trans
@[simp] def of_function {I : Type u₁} {F G : I → C} (f : Π i : I, F i ⟶ G i) :
(functor.of_function F) ⟹ (functor.of_function G) :=
{ app := λ i, f i,
naturality' := λ X Y g,
begin
cases g, cases g, cases g,
dsimp [functor.of_function],
simp,
end }
end nat_trans
namespace discrete
omit 𝒞
def lift {α : Type u₁} {β : Type u₂} (f : α → β) : (discrete α) ⥤ (discrete β) :=
functor.of_function f
include 𝒞
variables (J : Type v₂)
@[simp] lemma functor_map_id
(F : discrete J ⥤ C) (j : discrete J) (f : j ⟶ j) : F.map f = 𝟙 (F.obj j) :=
begin
have h : f = 𝟙 j, cases f, cases f, ext,
rw h,
simp,
end
end discrete
end category_theory
|
77b9c93b6ef7d800b4bf8a709275e0211e80e8e1 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/single_obj.lean | 43eaf45676791d3001609cd310de4c2953d56b61 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 6,570 | lean | /-
Copyright (c) 2019 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import category_theory.endomorphism
import category_theory.category.Cat
import algebra.category.Mon.basic
import combinatorics.quiver.single_obj
/-!
# Single-object category
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
Single object category with a given monoid of endomorphisms.
It is defined to facilitate transfering some definitions and lemmas (e.g., conjugacy etc.)
from category theory to monoids and groups.
## Main definitions
Given a type `α` with a monoid structure, `single_obj α` is `unit` type with `category` structure
such that `End (single_obj α).star` is the monoid `α`. This can be extended to a functor `Mon ⥤
Cat`.
If `α` is a group, then `single_obj α` is a groupoid.
An element `x : α` can be reinterpreted as an element of `End (single_obj.star α)` using
`single_obj.to_End`.
## Implementation notes
- `category_struct.comp` on `End (single_obj.star α)` is `flip (*)`, not `(*)`. This way
multiplication on `End` agrees with the multiplication on `α`.
- By default, Lean puts instances into `category_theory` namespace instead of
`category_theory.single_obj`, so we give all names explicitly.
-/
universes u v w
namespace category_theory
/--
Abbreviation that allows writing `category_theory.single_obj` rather than `quiver.single_obj`.
-/
abbreviation single_obj := quiver.single_obj
namespace single_obj
variables (α : Type u)
/-- One and `flip (*)` become `id` and `comp` for morphisms of the single object category. -/
instance category_struct [has_one α] [has_mul α] : category_struct (single_obj α) :=
{ hom := λ _ _, α,
comp := λ _ _ _ x y, y * x,
id := λ _, 1 }
/-- Monoid laws become category laws for the single object category. -/
instance category [monoid α] : category (single_obj α) :=
{ comp_id' := λ _ _, one_mul,
id_comp' := λ _ _, mul_one,
assoc' := λ _ _ _ _ x y z, (mul_assoc z y x).symm }
lemma id_as_one [monoid α] (x : single_obj α) : 𝟙 x = 1 := rfl
lemma comp_as_mul [monoid α] {x y z : single_obj α} (f : x ⟶ y) (g : y ⟶ z) :
f ≫ g = g * f := rfl
/--
Groupoid structure on `single_obj α`.
See <https://stacks.math.columbia.edu/tag/0019>.
-/
instance groupoid [group α] : groupoid (single_obj α) :=
{ inv := λ _ _ x, x⁻¹,
inv_comp' := λ _ _, mul_right_inv,
comp_inv' := λ _ _, mul_left_inv }
lemma inv_as_inv [group α] {x y : single_obj α} (f : x ⟶ y) : inv f = f⁻¹ :=
by { ext, rw [comp_as_mul, inv_mul_self, id_as_one] }
/--
Abbreviation that allows writing `category_theory.single_obj.star` rather than
`quiver.single_obj.star`.
-/
abbreviation star : single_obj α := quiver.single_obj.star α
/-- The endomorphisms monoid of the only object in `single_obj α` is equivalent to the original
monoid α. -/
def to_End [monoid α] : α ≃* End (single_obj.star α) :=
{ map_mul' := λ x y, rfl,
.. equiv.refl α }
lemma to_End_def [monoid α] (x : α) : to_End α x = x := rfl
/-- There is a 1-1 correspondence between monoid homomorphisms `α → β` and functors between the
corresponding single-object categories. It means that `single_obj` is a fully faithful
functor.
See <https://stacks.math.columbia.edu/tag/001F> --
although we do not characterize when the functor is full or faithful.
-/
def map_hom (α : Type u) (β : Type v) [monoid α] [monoid β] :
(α →* β) ≃ (single_obj α) ⥤ (single_obj β) :=
{ to_fun := λ f,
{ obj := id,
map := λ _ _, ⇑f,
map_id' := λ _, f.map_one,
map_comp' := λ _ _ _ x y, f.map_mul y x },
inv_fun := λ f,
{ to_fun := @functor.map _ _ _ _ f (single_obj.star α) (single_obj.star α),
map_one' := f.map_id _,
map_mul' := λ x y, f.map_comp y x },
left_inv := λ ⟨f, h₁, h₂⟩, rfl,
right_inv := λ f, by cases f; obviously }
lemma map_hom_id (α : Type u) [monoid α] : map_hom α α (monoid_hom.id α) = 𝟭 _ := rfl
lemma map_hom_comp {α : Type u} {β : Type v} [monoid α] [monoid β] (f : α →* β)
{γ : Type w} [monoid γ] (g : β →* γ) :
map_hom α γ (g.comp f) = map_hom α β f ⋙ map_hom β γ g :=
rfl
/-- Given a function `f : C → G` from a category to a group, we get a functor
`C ⥤ G` sending any morphism `x ⟶ y` to `f y * (f x)⁻¹`. -/
@[simps] def difference_functor {C G} [category C] [group G] (f : C → G) : C ⥤ single_obj G :=
{ obj := λ _, (),
map := λ x y _, f y * (f x)⁻¹,
map_id' := by { intro, rw [single_obj.id_as_one, mul_right_inv] },
map_comp' := by { intros, rw [single_obj.comp_as_mul, ←mul_assoc,
mul_left_inj, mul_assoc, inv_mul_self, mul_one] } }
end single_obj
end category_theory
open category_theory
namespace monoid_hom
/-- Reinterpret a monoid homomorphism `f : α → β` as a functor `(single_obj α) ⥤ (single_obj β)`.
See also `category_theory.single_obj.map_hom` for an equivalence between these types. -/
@[reducible] def to_functor {α : Type u} {β : Type v} [monoid α] [monoid β] (f : α →* β) :
(single_obj α) ⥤ (single_obj β) :=
single_obj.map_hom α β f
@[simp] lemma id_to_functor (α : Type u) [monoid α] : (id α).to_functor = 𝟭 _ := rfl
@[simp] lemma comp_to_functor {α : Type u} {β : Type v} [monoid α] [monoid β] (f : α →* β)
{γ : Type w} [monoid γ] (g : β →* γ) :
(g.comp f).to_functor = f.to_functor ⋙ g.to_functor :=
rfl
end monoid_hom
namespace units
variables (α : Type u) [monoid α]
/--
The units in a monoid are (multiplicatively) equivalent to
the automorphisms of `star` when we think of the monoid as a single-object category. -/
def to_Aut : αˣ ≃* Aut (single_obj.star α) :=
(units.map_equiv (single_obj.to_End α)).trans $
Aut.units_End_equiv_Aut _
@[simp] lemma to_Aut_hom (x : αˣ) : (to_Aut α x).hom = single_obj.to_End α x := rfl
@[simp] lemma to_Aut_inv (x : αˣ) :
(to_Aut α x).inv = single_obj.to_End α (x⁻¹ : αˣ) :=
rfl
end units
namespace Mon
open category_theory
/-- The fully faithful functor from `Mon` to `Cat`. -/
def to_Cat : Mon ⥤ Cat :=
{ obj := λ x, Cat.of (single_obj x),
map := λ x y f, single_obj.map_hom x y f }
instance to_Cat_full : full to_Cat :=
{ preimage := λ x y, (single_obj.map_hom x y).inv_fun,
witness' := λ x y, by apply equiv.right_inv }
instance to_Cat_faithful : faithful to_Cat :=
{ map_injective' := λ x y, by apply equiv.injective }
end Mon
|
e33fe238382062d45d9e4eb002c30fd74c90d18e | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/field_theory/tower.lean | 487fdc65e7a4121ae21d8d40ac3a60480badb366 | [
"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 | 5,120 | 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 ring_theory.algebra_tower
import linear_algebra.matrix.finite_dimensional
import linear_algebra.matrix.to_lin
/-!
# Tower of field extensions
In this file we prove the tower law for arbitrary extensions and finite extensions.
Suppose `L` is a field extension of `K` and `K` is a field extension of `F`.
Then `[L:F] = [L:K] [K:F]` where `[E₁:E₂]` means the `E₂`-dimension of `E₁`.
In fact we generalize it to vector spaces, where `L` is not necessarily a field,
but just a vector space over `K`.
## Implementation notes
We prove two versions, since there are two notions of dimensions: `module.rank` which gives
the dimension of an arbitrary vector space as a cardinal, and `finite_dimensional.finrank` which
gives the dimension of a finitely-dimensional vector space as a natural number.
## Tags
tower law
-/
universes u v w u₁ v₁ w₁
open_locale classical big_operators
section field
open cardinal
variables (F : Type u) (K : Type v) (A : Type w)
variables [field F] [field K] [add_comm_group A]
variables [algebra F K] [module K A] [module F A] [is_scalar_tower F K A]
/-- Tower law: if `A` is a `K`-vector space and `K` is a field extension of `F` then
`dim_F(A) = dim_F(K) * dim_K(A)`. -/
theorem dim_mul_dim' :
(cardinal.lift.{v w} (module.rank F K) *
cardinal.lift.{w v} (module.rank K A) : cardinal.{max w v}) =
cardinal.lift.{w v} (module.rank F A) :=
let b := basis.of_vector_space F K, c := basis.of_vector_space K A in
by rw [← (module.rank F K).lift_id, ← b.mk_eq_dim,
← (module.rank K A).lift_id, ← c.mk_eq_dim,
← lift_umax.{w v}, ← (b.smul c).mk_eq_dim, mk_prod, lift_mul,
lift_lift, lift_lift, lift_lift, lift_lift, lift_umax]
/-- Tower law: if `A` is a `K`-vector space and `K` is a field extension of `F` then
`dim_F(A) = dim_F(K) * dim_K(A)`. -/
theorem dim_mul_dim (F : Type u) (K A : Type v) [field F] [field K] [add_comm_group A]
[algebra F K] [module K A] [module F A] [is_scalar_tower F K A] :
module.rank F K * module.rank K A = module.rank F A :=
by convert dim_mul_dim' F K A; rw lift_id
namespace finite_dimensional
open is_noetherian
theorem trans [finite_dimensional F K] [finite_dimensional K A] : finite_dimensional F A :=
let b := basis.of_vector_space F K, c := basis.of_vector_space K A in
of_fintype_basis $ b.smul c
lemma right [hf : finite_dimensional F A] : finite_dimensional K A :=
let ⟨⟨b, hb⟩⟩ := iff_fg.1 hf in
iff_fg.2 ⟨⟨b, submodule.restrict_scalars_injective F _ _ $
by { rw [submodule.restrict_scalars_top, eq_top_iff, ← hb, submodule.span_le],
exact submodule.subset_span }⟩⟩
/-- Tower law: if `A` is a `K`-algebra and `K` is a field extension of `F` then
`dim_F(A) = dim_F(K) * dim_K(A)`. -/
theorem finrank_mul_finrank [finite_dimensional F K] :
finrank F K * finrank K A = finrank F A :=
begin
by_cases hA : finite_dimensional K A,
{ resetI,
let b := basis.of_vector_space F K,
let c := basis.of_vector_space K A,
rw [finrank_eq_card_basis b, finrank_eq_card_basis c,
finrank_eq_card_basis (b.smul c), fintype.card_prod] },
{ rw [finrank_of_infinite_dimensional hA, mul_zero, finrank_of_infinite_dimensional],
exact mt (@right F K A _ _ _ _ _ _ _) hA }
end
instance linear_map (F : Type u) (V : Type v) (W : Type w)
[field F] [add_comm_group V] [module F V] [add_comm_group W] [module F W]
[finite_dimensional F V] [finite_dimensional F W] :
finite_dimensional F (V →ₗ[F] W) :=
let b := basis.of_vector_space F V, c := basis.of_vector_space F W in
(matrix.to_lin b c).finite_dimensional
lemma finrank_linear_map (F : Type u) (V : Type v) (W : Type w)
[field F] [add_comm_group V] [module F V] [add_comm_group W] [module F W]
[finite_dimensional F V] [finite_dimensional F W] :
finrank F (V →ₗ[F] W) = finrank F V * finrank F W :=
let b := basis.of_vector_space F V, c := basis.of_vector_space F W in
by rw [linear_equiv.finrank_eq (linear_map.to_matrix b c), matrix.finrank_matrix,
finrank_eq_card_basis b, finrank_eq_card_basis c, mul_comm]
-- TODO: generalize by removing [finite_dimensional F K]
-- V = ⊕F,
-- (V →ₗ[F] K) = ((⊕F) →ₗ[F] K) = (⊕ (F →ₗ[F] K)) = ⊕K
instance linear_map' (F : Type u) (K : Type v) (V : Type w)
[field F] [field K] [algebra F K] [finite_dimensional F K]
[add_comm_group V] [module F V] [finite_dimensional F V] :
finite_dimensional K (V →ₗ[F] K) :=
right F _ _
lemma finrank_linear_map' (F : Type u) (K : Type v) (V : Type w)
[field F] [field K] [algebra F K] [finite_dimensional F K]
[add_comm_group V] [module F V] [finite_dimensional F V] :
finrank K (V →ₗ[F] K) = finrank F V :=
(nat.mul_right_inj $ show 0 < finrank F K, from finrank_pos).1 $
calc finrank F K * finrank K (V →ₗ[F] K)
= finrank F (V →ₗ[F] K) : finrank_mul_finrank _ _ _
... = finrank F V * finrank F K : finrank_linear_map F V K
... = finrank F K * finrank F V : mul_comm _ _
end finite_dimensional
end field
|
b8262d9a3983047fe3bba5239202a4490855412e | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/data/polynomial/basic.lean | 3517ef332e4859b6bdf5da802b01eedcc9236301 | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 32,752 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import algebra.monoid_algebra.basic
/-!
# Theory of univariate polynomials
This file defines `polynomial R`, the type of univariate polynomials over the semiring `R`, builds
a semiring structure on it, and gives basic definitions that are expanded in other files in this
directory.
## Main definitions
* `monomial n a` is the polynomial `a X^n`. Note that `monomial n` is defined as an `R`-linear map.
* `C a` is the constant polynomial `a`. Note that `C` is defined as a ring homomorphism.
* `X` is the polynomial `X`, i.e., `monomial 1 1`.
* `p.sum f` is `∑ n in p.support, f n (p.coeff n)`, i.e., one sums the values of functions applied
to coefficients of the polynomial `p`.
* `p.erase n` is the polynomial `p` in which one removes the `c X^n` term.
There are often two natural variants of lemmas involving sums, depending on whether one acts on the
polynomials, or on the function. The naming convention is that one adds `index` when acting on
the polynomials. For instance,
* `sum_add_index` states that `(p + q).sum f = p.sum f + q.sum f`;
* `sum_add` states that `p.sum (λ n x, f n x + g n x) = p.sum f + p.sum g`.
* Notation to refer to `polynomial R`, as `R[X]` or `R[t]`.
## Implementation
Polynomials are defined using `add_monoid_algebra R ℕ`, where `R` is a semiring.
The variable `X` commutes with every polynomial `p`: lemma `X_mul` proves the identity
`X * p = p * X`. The relationship to `add_monoid_algebra R ℕ` is through a structure
to make polynomials irreducible from the point of view of the kernel. Most operations
are irreducible since Lean can not compute anyway with `add_monoid_algebra`. There are two
exceptions that we make semireducible:
* The zero polynomial, so that its coefficients are definitionally equal to `0`.
* The scalar action, to permit typeclass search to unfold it to resolve potential instance
diamonds.
The raw implementation of the equivalence between `polynomial R` and `add_monoid_algebra R ℕ` is
done through `of_finsupp` and `to_finsupp` (or, equivalently, `rcases p` when `p` is a polynomial
gives an element `q` of `add_monoid_algebra R ℕ`, and conversely `⟨q⟩` gives back `p`). The
equivalence is also registered as a ring equiv in `polynomial.to_finsupp_iso`. These should
in general not be used once the basic API for polynomials is constructed.
-/
noncomputable theory
/-- `polynomial R` is the type of univariate polynomials over `R`.
Polynomials should be seen as (semi-)rings with the additional constructor `X`.
The embedding from `R` is called `C`. -/
structure polynomial (R : Type*) [semiring R] := of_finsupp ::
(to_finsupp : add_monoid_algebra R ℕ)
localized "notation (name := polynomial) R`[X]`:9000 := polynomial R" in polynomial
open add_monoid_algebra finsupp function
open_locale big_operators polynomial
namespace polynomial
universes u
variables {R : Type u} {a b : R} {m n : ℕ}
section semiring
variables [semiring R] {p q : R[X]}
lemma forall_iff_forall_finsupp (P : R[X] → Prop) :
(∀ p, P p) ↔ ∀ (q : add_monoid_algebra R ℕ), P ⟨q⟩ :=
⟨λ h q, h ⟨q⟩, λ h ⟨p⟩, h p⟩
lemma exists_iff_exists_finsupp (P : R[X] → Prop) :
(∃ p, P p) ↔ ∃ (q : add_monoid_algebra R ℕ), P ⟨q⟩ :=
⟨λ ⟨⟨p⟩, hp⟩, ⟨p, hp⟩, λ ⟨q, hq⟩, ⟨⟨q⟩, hq⟩ ⟩
/-! ### Conversions to and from `add_monoid_algebra`
Since `polynomial R` is not defeq to `add_monoid_algebra R ℕ`, but instead is a structure wrapping
it, we have to copy across all the arithmetic operators manually, along with the lemmas about how
they unfold around `polynomial.of_finsupp` and `polynomial.to_finsupp`.
-/
section add_monoid_algebra
@[irreducible] private def add : R[X] → R[X] → R[X]
| ⟨a⟩ ⟨b⟩ := ⟨a + b⟩
@[irreducible] private def neg {R : Type u} [ring R] : R[X] → R[X]
| ⟨a⟩ := ⟨-a⟩
@[irreducible] private def mul : R[X] → R[X] → R[X]
| ⟨a⟩ ⟨b⟩ := ⟨a * b⟩
instance : has_zero R[X] := ⟨⟨0⟩⟩
instance : has_one R[X] := ⟨⟨1⟩⟩
instance : has_add R[X] := ⟨add⟩
instance {R : Type u} [ring R] : has_neg R[X] := ⟨neg⟩
instance {R : Type u} [ring R] : has_sub R[X] := ⟨λ a b, a + -b⟩
instance : has_mul R[X] := ⟨mul⟩
instance {S : Type*} [monoid S] [distrib_mul_action S R] : has_smul S R[X] :=
⟨λ r p, ⟨r • p.to_finsupp⟩⟩
@[priority 1] -- to avoid a bug in the `ring` tactic
instance has_pow : has_pow R[X] ℕ := { pow := λ p n, npow_rec n p }
@[simp] lemma of_finsupp_zero : (⟨0⟩ : R[X]) = 0 := rfl
@[simp] lemma of_finsupp_one : (⟨1⟩ : R[X]) = 1 := rfl
@[simp] lemma of_finsupp_add {a b} : (⟨a + b⟩ : R[X]) = ⟨a⟩ + ⟨b⟩ := show _ = add _ _, by rw add
@[simp] lemma of_finsupp_neg {R : Type u} [ring R] {a} : (⟨-a⟩ : R[X]) = -⟨a⟩ :=
show _ = neg _, by rw neg
@[simp] lemma of_finsupp_sub {R : Type u} [ring R] {a b} : (⟨a - b⟩ : R[X]) = ⟨a⟩ - ⟨b⟩ :=
by { rw [sub_eq_add_neg, of_finsupp_add, of_finsupp_neg], refl }
@[simp] lemma of_finsupp_mul (a b) : (⟨a * b⟩ : R[X]) = ⟨a⟩ * ⟨b⟩ := show _ = mul _ _, by rw mul
@[simp] lemma of_finsupp_smul {S : Type*} [monoid S] [distrib_mul_action S R] (a : S) (b) :
(⟨a • b⟩ : R[X]) = (a • ⟨b⟩ : R[X]) := rfl
@[simp] lemma of_finsupp_pow (a) (n : ℕ) : (⟨a ^ n⟩ : R[X]) = ⟨a⟩ ^ n :=
begin
change _ = npow_rec n _,
induction n,
{ simp [npow_rec], } ,
{ simp [npow_rec, n_ih, pow_succ] }
end
@[simp] lemma to_finsupp_zero : (0 : R[X]).to_finsupp = 0 :=
rfl
@[simp] lemma to_finsupp_one : (1 : R[X]).to_finsupp = 1 := rfl
@[simp] lemma to_finsupp_add (a b : R[X]) : (a + b).to_finsupp = a.to_finsupp + b.to_finsupp :=
by { cases a, cases b, rw ←of_finsupp_add }
@[simp] lemma to_finsupp_neg {R : Type u} [ring R] (a : R[X]) : (-a).to_finsupp = -a.to_finsupp :=
by { cases a, rw ←of_finsupp_neg }
@[simp] lemma to_finsupp_sub {R : Type u} [ring R] (a b : R[X]) :
(a - b).to_finsupp = a.to_finsupp - b.to_finsupp :=
by { rw [sub_eq_add_neg, ←to_finsupp_neg, ←to_finsupp_add], refl }
@[simp] lemma to_finsupp_mul (a b : R[X]) : (a * b).to_finsupp = a.to_finsupp * b.to_finsupp :=
by { cases a, cases b, rw ←of_finsupp_mul }
@[simp] lemma to_finsupp_smul {S : Type*} [monoid S] [distrib_mul_action S R] (a : S) (b : R[X]) :
(a • b).to_finsupp = a • b.to_finsupp := rfl
@[simp] lemma to_finsupp_pow (a : R[X]) (n : ℕ) : (a ^ n).to_finsupp = a.to_finsupp ^ n :=
by { cases a, rw ←of_finsupp_pow }
lemma _root_.is_smul_regular.polynomial {S : Type*} [monoid S] [distrib_mul_action S R] {a : S}
(ha : is_smul_regular R a) : is_smul_regular R[X] a
| ⟨x⟩ ⟨y⟩ h := congr_arg _ $ ha.finsupp (polynomial.of_finsupp.inj h)
lemma to_finsupp_injective : function.injective (to_finsupp : R[X] → add_monoid_algebra _ _) :=
λ ⟨x⟩ ⟨y⟩, congr_arg _
@[simp] lemma to_finsupp_inj {a b : R[X]} : a.to_finsupp = b.to_finsupp ↔ a = b :=
to_finsupp_injective.eq_iff
@[simp] lemma to_finsupp_eq_zero {a : R[X]} : a.to_finsupp = 0 ↔ a = 0 :=
by rw [←to_finsupp_zero, to_finsupp_inj]
@[simp] lemma to_finsupp_eq_one {a : R[X]} : a.to_finsupp = 1 ↔ a = 1 :=
by rw [←to_finsupp_one, to_finsupp_inj]
/-- A more convenient spelling of `polynomial.of_finsupp.inj_eq` in terms of `iff`. -/
lemma of_finsupp_inj {a b} : (⟨a⟩ : R[X]) = ⟨b⟩ ↔ a = b :=
iff_of_eq of_finsupp.inj_eq
@[simp] lemma of_finsupp_eq_zero {a} : (⟨a⟩ : R[X]) = 0 ↔ a = 0 :=
by rw [←of_finsupp_zero, of_finsupp_inj]
@[simp] lemma of_finsupp_eq_one {a} : (⟨a⟩ : R[X]) = 1 ↔ a = 1 :=
by rw [←of_finsupp_one, of_finsupp_inj]
instance : inhabited R[X] := ⟨0⟩
instance : has_nat_cast R[X] := ⟨λ n, polynomial.of_finsupp n⟩
instance : semiring R[X] :=
function.injective.semiring to_finsupp to_finsupp_injective
to_finsupp_zero to_finsupp_one to_finsupp_add to_finsupp_mul
(λ _ _, to_finsupp_smul _ _) to_finsupp_pow (λ _, rfl)
instance {S} [monoid S] [distrib_mul_action S R] : distrib_mul_action S R[X] :=
function.injective.distrib_mul_action
⟨to_finsupp, to_finsupp_zero, to_finsupp_add⟩ to_finsupp_injective to_finsupp_smul
instance {S} [monoid S] [distrib_mul_action S R] [has_faithful_smul S R] :
has_faithful_smul S R[X] :=
{ eq_of_smul_eq_smul := λ s₁ s₂ h, eq_of_smul_eq_smul $ λ a : ℕ →₀ R, congr_arg to_finsupp (h ⟨a⟩) }
instance {S} [semiring S] [module S R] : module S R[X] :=
function.injective.module _
⟨to_finsupp, to_finsupp_zero, to_finsupp_add⟩ to_finsupp_injective to_finsupp_smul
instance {S₁ S₂} [monoid S₁] [monoid S₂] [distrib_mul_action S₁ R] [distrib_mul_action S₂ R]
[smul_comm_class S₁ S₂ R] : smul_comm_class S₁ S₂ R[X] :=
⟨by { rintros _ _ ⟨⟩, simp_rw [←of_finsupp_smul, smul_comm] }⟩
instance {S₁ S₂} [has_smul S₁ S₂] [monoid S₁] [monoid S₂] [distrib_mul_action S₁ R]
[distrib_mul_action S₂ R] [is_scalar_tower S₁ S₂ R] : is_scalar_tower S₁ S₂ R[X] :=
⟨by { rintros _ _ ⟨⟩, simp_rw [←of_finsupp_smul, smul_assoc] }⟩
instance {S} [monoid S] [distrib_mul_action S R] [distrib_mul_action Sᵐᵒᵖ R]
[is_central_scalar S R] : is_central_scalar S R[X] :=
⟨by { rintros _ ⟨⟩, simp_rw [←of_finsupp_smul, op_smul_eq_smul] }⟩
instance [subsingleton R] : unique R[X] :=
{ uniq := by { rintros ⟨x⟩, refine congr_arg of_finsupp _, simp },
.. polynomial.inhabited }
variable (R)
/-- Ring isomorphism between `R[X]` and `add_monoid_algebra R ℕ`. This is just an
implementation detail, but it can be useful to transfer results from `finsupp` to polynomials. -/
@[simps apply symm_apply]
def to_finsupp_iso : R[X] ≃+* add_monoid_algebra R ℕ :=
{ to_fun := to_finsupp,
inv_fun := of_finsupp,
left_inv := λ ⟨p⟩, rfl,
right_inv := λ p, rfl,
map_mul' := to_finsupp_mul,
map_add' := to_finsupp_add }
end add_monoid_algebra
variable {R}
lemma of_finsupp_sum {ι : Type*} (s : finset ι) (f : ι → add_monoid_algebra R ℕ) :
(⟨∑ i in s, f i⟩ : R[X]) = ∑ i in s, ⟨f i⟩ :=
map_sum (to_finsupp_iso R).symm f s
lemma to_finsupp_sum {ι : Type*} (s : finset ι) (f : ι → R[X]) :
(∑ i in s, f i : R[X]).to_finsupp = ∑ i in s, (f i).to_finsupp :=
map_sum (to_finsupp_iso R) f s
/--
The set of all `n` such that `X^n` has a non-zero coefficient.
-/
@[simp]
def support : R[X] → finset ℕ
| ⟨p⟩ := p.support
@[simp] lemma support_of_finsupp (p) : support (⟨p⟩ : R[X]) = p.support :=
by rw support
@[simp] lemma support_zero : (0 : R[X]).support = ∅ :=
rfl
@[simp] lemma support_eq_empty : p.support = ∅ ↔ p = 0 :=
by { rcases p, simp [support] }
lemma card_support_eq_zero : p.support.card = 0 ↔ p = 0 :=
by simp
/-- `monomial s a` is the monomial `a * X^s` -/
def monomial (n : ℕ) : R →ₗ[R] R[X] :=
{ to_fun := λ t, ⟨finsupp.single n t⟩,
map_add' := by simp,
map_smul' := by simp [←of_finsupp_smul] }
@[simp] lemma to_finsupp_monomial (n : ℕ) (r : R) :
(monomial n r).to_finsupp = finsupp.single n r :=
by simp [monomial]
@[simp] lemma of_finsupp_single (n : ℕ) (r : R) :
(⟨finsupp.single n r⟩ : R[X]) = monomial n r :=
by simp [monomial]
@[simp]
lemma monomial_zero_right (n : ℕ) :
monomial n (0 : R) = 0 :=
(monomial n).map_zero
-- This is not a `simp` lemma as `monomial_zero_left` is more general.
lemma monomial_zero_one : monomial 0 (1 : R) = 1 := rfl
-- TODO: can't we just delete this one?
lemma monomial_add (n : ℕ) (r s : R) :
monomial n (r + s) = monomial n r + monomial n s :=
(monomial n).map_add _ _
lemma monomial_mul_monomial (n m : ℕ) (r s : R) :
monomial n r * monomial m s = monomial (n + m) (r * s) :=
to_finsupp_injective $
by simp only [to_finsupp_monomial, to_finsupp_mul, add_monoid_algebra.single_mul_single]
@[simp]
lemma monomial_pow (n : ℕ) (r : R) (k : ℕ) :
(monomial n r)^k = monomial (n*k) (r^k) :=
begin
induction k with k ih,
{ simp [pow_zero, monomial_zero_one], },
{ simp [pow_succ, ih, monomial_mul_monomial, nat.succ_eq_add_one, mul_add, add_comm] },
end
lemma smul_monomial {S} [monoid S] [distrib_mul_action S R] (a : S) (n : ℕ) (b : R) :
a • monomial n b = monomial n (a • b) :=
to_finsupp_injective $ by simp
lemma monomial_injective (n : ℕ) :
function.injective (monomial n : R → R[X]) :=
(to_finsupp_iso R).symm.injective.comp (single_injective n)
@[simp] lemma monomial_eq_zero_iff (t : R) (n : ℕ) :
monomial n t = 0 ↔ t = 0 :=
linear_map.map_eq_zero_iff _ (polynomial.monomial_injective n)
lemma support_add : (p + q).support ⊆ p.support ∪ q.support :=
begin
rcases p, rcases q,
simp only [←of_finsupp_add, support],
exact support_add
end
/--
`C a` is the constant polynomial `a`.
`C` is provided as a ring homomorphism.
-/
def C : R →+* R[X] :=
{ map_one' := by simp [monomial_zero_one],
map_mul' := by simp [monomial_mul_monomial],
map_zero' := by simp,
.. monomial 0 }
@[simp] lemma monomial_zero_left (a : R) : monomial 0 a = C a := rfl
@[simp] lemma to_finsupp_C (a : R) : (C a).to_finsupp = single 0 a :=
by rw [←monomial_zero_left, to_finsupp_monomial]
lemma C_0 : C (0 : R) = 0 := by simp
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 smul_C {S} [monoid S] [distrib_mul_action S R] (s : S) (r : R) :
s • C r = C (s • r) :=
smul_monomial _ _ r
@[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_eq_nat_cast (n : ℕ) : C (n : R) = (n : R[X]) :=
map_nat_cast C 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]
/-- `X` is the polynomial variable (aka indeterminate). -/
def X : R[X] := monomial 1 1
lemma monomial_one_one_eq_X : monomial 1 (1 : R) = X := rfl
lemma monomial_one_right_eq_X_pow (n : ℕ) : monomial n (1 : R) = X^n :=
begin
induction n with n ih,
{ simp [monomial_zero_one], },
{ rw [pow_succ, ←ih, ←monomial_one_one_eq_X, monomial_mul_monomial, add_comm, one_mul], }
end
/-- `X` commutes with everything, even when the coefficients are noncommutative. -/
lemma X_mul : X * p = p * X :=
begin
rcases p,
simp only [X, ←of_finsupp_single, ←of_finsupp_mul, linear_map.coe_mk],
ext,
simp [add_monoid_algebra.mul_apply, sum_single_index, add_comm],
end
lemma X_pow_mul {n : ℕ} : X^n * p = p * X^n :=
begin
induction n with n ih,
{ simp, },
{ conv_lhs { rw pow_succ', },
rw [mul_assoc, X_mul, ←mul_assoc, ih, mul_assoc, ←pow_succ'], }
end
/-- Prefer putting constants to the left of `X`.
This lemma is the loop-avoiding `simp` version of `polynomial.X_mul`. -/
@[simp] lemma X_mul_C (r : R) : X * C r = C r * X :=
X_mul
/-- Prefer putting constants to the left of `X ^ n`.
This lemma is the loop-avoiding `simp` version of `X_pow_mul`. -/
@[simp] lemma X_pow_mul_C (r : R) (n : ℕ) : X^n * C r = C r * X^n :=
X_pow_mul
lemma X_pow_mul_assoc {n : ℕ} : (p * X^n) * q = (p * q) * X^n :=
by rw [mul_assoc, X_pow_mul, ←mul_assoc]
/-- Prefer putting constants to the left of `X ^ n`.
This lemma is the loop-avoiding `simp` version of `X_pow_mul_assoc`. -/
@[simp] lemma X_pow_mul_assoc_C {n : ℕ} (r : R) : (p * X^n) * C r = p * C r * X^n :=
X_pow_mul_assoc
lemma commute_X (p : R[X]) : commute X p := X_mul
lemma commute_X_pow (p : R[X]) (n : ℕ) : commute (X ^ n) p := X_pow_mul
@[simp]
lemma monomial_mul_X (n : ℕ) (r : R) : monomial n r * X = monomial (n+1) r :=
by erw [monomial_mul_monomial, mul_one]
@[simp]
lemma monomial_mul_X_pow (n : ℕ) (r : R) (k : ℕ) : monomial n r * X^k = monomial (n+k) r :=
begin
induction k with k ih,
{ simp, },
{ simp [ih, pow_succ', ←mul_assoc, add_assoc], },
end
@[simp]
lemma X_mul_monomial (n : ℕ) (r : R) : X * monomial n r = monomial (n+1) r :=
by rw [X_mul, monomial_mul_X]
@[simp]
lemma X_pow_mul_monomial (k n : ℕ) (r : R) : X^k * monomial n r = monomial (n+k) r :=
by rw [X_pow_mul, monomial_mul_X_pow]
/-- `coeff p n` (often denoted `p.coeff n`) is the coefficient of `X^n` in `p`. -/
@[simp] def coeff : R[X] → ℕ → R
| ⟨p⟩ := p
lemma coeff_injective : injective (coeff : R[X] → ℕ → R) :=
by { rintro ⟨p⟩ ⟨q⟩, simp only [coeff, fun_like.coe_fn_eq, imp_self] }
@[simp] lemma coeff_inj : p.coeff = q.coeff ↔ p = q := coeff_injective.eq_iff
lemma coeff_monomial : coeff (monomial n a) m = if n = m then a else 0 :=
by { simp only [←of_finsupp_single, coeff, linear_map.coe_mk], rw finsupp.single_apply }
@[simp] lemma coeff_zero (n : ℕ) : coeff (0 : R[X]) n = 0 := rfl
@[simp] lemma coeff_one_zero : coeff (1 : R[X]) 0 = 1 :=
by { rw [← monomial_zero_one, coeff_monomial], simp }
@[simp] lemma coeff_X_one : coeff (X : R[X]) 1 = 1 := coeff_monomial
@[simp] lemma coeff_X_zero : coeff (X : R[X]) 0 = 0 := coeff_monomial
@[simp] lemma coeff_monomial_succ : coeff (monomial (n+1) a) 0 = 0 :=
by simp [coeff_monomial]
lemma coeff_X : coeff (X : R[X]) n = if 1 = n then 1 else 0 := coeff_monomial
lemma coeff_X_of_ne_one {n : ℕ} (hn : n ≠ 1) : coeff (X : R[X]) n = 0 :=
by rw [coeff_X, if_neg hn.symm]
@[simp] lemma mem_support_iff : n ∈ p.support ↔ p.coeff n ≠ 0 :=
by { rcases p, simp }
lemma not_mem_support_iff : n ∉ p.support ↔ p.coeff n = 0 :=
by simp
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 :=
nontrivial_of_ne 0 1 $ λ h01, h $
by rw [← mul_one p, ← mul_one q, ← C_1, ← h01, C_0, mul_zero, mul_zero]
lemma monomial_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 [monomial_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
lemma subsingleton_iff_subsingleton :
subsingleton R[X] ↔ subsingleton R :=
⟨λ h, subsingleton_iff.mpr (λ a b, C_inj.mp (subsingleton_iff.mp h _ _)),
by { introI, apply_instance } ⟩
lemma forall_eq_iff_forall_eq :
(∀ f g : R[X], f = g) ↔ (∀ a b : R, a = b) :=
by simpa only [← subsingleton_iff] using subsingleton_iff_subsingleton
theorem ext_iff {p q : R[X]} : p = q ↔ ∀ n, coeff p n = coeff q n :=
by { rcases p, rcases q, simp [coeff, finsupp.ext_iff] }
@[ext] lemma ext {p q : R[X]} : (∀ n, coeff p n = coeff q n) → p = q :=
ext_iff.2
/-- Monomials generate the additive monoid of polynomials. -/
lemma add_submonoid_closure_set_of_eq_monomial :
add_submonoid.closure {p : R[X] | ∃ n a, p = monomial n a} = ⊤ :=
begin
apply top_unique,
rw [← add_submonoid.map_equiv_top (to_finsupp_iso R).symm.to_add_equiv,
← finsupp.add_closure_set_of_eq_single, add_monoid_hom.map_mclosure],
refine add_submonoid.closure_mono (set.image_subset_iff.2 _),
rintro _ ⟨n, a, rfl⟩,
exact ⟨n, a, polynomial.of_finsupp_single _ _⟩,
end
lemma add_hom_ext {M : Type*} [add_monoid M] {f g : R[X] →+ M}
(h : ∀ n a, f (monomial n a) = g (monomial n a)) :
f = g :=
add_monoid_hom.eq_of_eq_on_mdense add_submonoid_closure_set_of_eq_monomial $
by { rintro p ⟨n, a, rfl⟩, exact h n a }
@[ext] lemma add_hom_ext' {M : Type*} [add_monoid M] {f g : R[X] →+ M}
(h : ∀ n, f.comp (monomial n).to_add_monoid_hom = g.comp (monomial n).to_add_monoid_hom) :
f = g :=
add_hom_ext (λ n, add_monoid_hom.congr_fun (h n))
@[ext] lemma lhom_ext' {M : Type*} [add_comm_monoid M] [module R M] {f g : R[X] →ₗ[R] M}
(h : ∀ n, f.comp (monomial n) = g.comp (monomial n)) :
f = g :=
linear_map.to_add_monoid_hom_injective $ add_hom_ext $ λ n, linear_map.congr_fun (h n)
-- this has the same content as the subsingleton
lemma eq_zero_of_eq_zero (h : (0 : R) = (1 : R)) (p : R[X]) : p = 0 :=
by rw [←one_smul R p, ←h, zero_smul]
section fewnomials
lemma support_monomial (n) {a : R} (H : a ≠ 0) : (monomial n a).support = singleton n :=
by rw [←of_finsupp_single, support, finsupp.support_single_ne_zero _ H]
lemma support_monomial' (n) (a : R) : (monomial n a).support ⊆ singleton n :=
by { rw [←of_finsupp_single, support], exact finsupp.support_single_subset }
lemma support_C_mul_X_pow (n : ℕ) {c : R} (h : c ≠ 0) : (C c * X ^ n).support = singleton n :=
by rw [←monomial_eq_C_mul_X, support_monomial n h]
lemma support_C_mul_X_pow' (n : ℕ) (c : R) : (C c * X ^ n).support ⊆ singleton n :=
begin
rw ← monomial_eq_C_mul_X,
exact support_monomial' n c,
end
open finset
lemma support_binomial' (k m : ℕ) (x y : R) : (C x * X ^ k + C y * X ^ m).support ⊆ {k, m} :=
support_add.trans (union_subset ((support_C_mul_X_pow' k x).trans
(singleton_subset_iff.mpr (mem_insert_self k {m}))) ((support_C_mul_X_pow' m y).trans
(singleton_subset_iff.mpr (mem_insert_of_mem (mem_singleton_self m)))))
lemma support_trinomial' (k m n : ℕ) (x y z : R) :
(C x * X ^ k + C y * X ^ m + C z * X ^ n).support ⊆ {k, m, n} :=
support_add.trans (union_subset (support_add.trans (union_subset ((support_C_mul_X_pow' k x).trans
(singleton_subset_iff.mpr (mem_insert_self k {m, n}))) ((support_C_mul_X_pow' m y).trans
(singleton_subset_iff.mpr (mem_insert_of_mem (mem_insert_self m {n}))))))
((support_C_mul_X_pow' n z).trans (singleton_subset_iff.mpr
(mem_insert_of_mem (mem_insert_of_mem (mem_singleton_self n))))))
end fewnomials
lemma X_pow_eq_monomial (n) : X ^ n = monomial n (1:R) :=
begin
induction n with n hn,
{ rw [pow_zero, monomial_zero_one] },
{ rw [pow_succ', hn, X, monomial_mul_monomial, one_mul] },
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 : by rw [smul_monomial, smul_eq_mul]
... = a • X^n : by rw X_pow_eq_monomial
lemma support_X_pow (H : ¬ (1:R) = 0) (n : ℕ) : (X^n : R[X]).support = singleton n :=
begin
convert support_monomial n H,
exact X_pow_eq_monomial n,
end
lemma support_X_empty (H : (1:R)=0) : (X : R[X]).support = ∅ :=
begin
rw [X, H, monomial_zero_right, support_zero],
end
lemma support_X (H : ¬ (1 : R) = 0) : (X : R[X]).support = singleton 1 :=
begin
rw [← pow_one X, support_X_pow H 1],
end
lemma monomial_left_inj {a : R} (ha : a ≠ 0) {i j : ℕ} : (monomial i a) = (monomial j a) ↔ i = j :=
by simp_rw [←of_finsupp_single, finsupp.single_left_inj ha]
lemma binomial_eq_binomial {k l m n : ℕ} {u v : R} (hu : u ≠ 0) (hv : v ≠ 0) :
C u * X ^ k + C v * X ^ l = C u * X ^ m + C v * X ^ n ↔
(k = m ∧ l = n) ∨ (u = v ∧ k = n ∧ l = m) ∨ (u + v = 0 ∧ k = l ∧ m = n) :=
begin
simp_rw [←monomial_eq_C_mul_X, ←to_finsupp_inj, to_finsupp_add, to_finsupp_monomial],
exact finsupp.single_add_single_eq_single_add_single hu hv,
end
lemma nat_cast_mul (n : ℕ) (p : R[X]) : (n : R[X]) * p = n • p :=
(nsmul_eq_mul _ _).symm
/-- Summing the values of a function applied to the coefficients of a polynomial -/
def sum {S : Type*} [add_comm_monoid S] (p : R[X]) (f : ℕ → R → S) : S :=
∑ n in p.support, f n (p.coeff n)
lemma sum_def {S : Type*} [add_comm_monoid S] (p : R[X]) (f : ℕ → R → S) :
p.sum f = ∑ n in p.support, f n (p.coeff n) := rfl
lemma sum_eq_of_subset {S : Type*} [add_comm_monoid S] (p : R[X])
(f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) (s : finset ℕ) (hs : p.support ⊆ s) :
p.sum f = ∑ n in s, f n (p.coeff n) :=
begin
apply finset.sum_subset hs (λ n hn h'n, _),
rw not_mem_support_iff at h'n,
simp [h'n, hf]
end
/-- Expressing the product of two polynomials as a double sum. -/
lemma mul_eq_sum_sum :
p * q = ∑ i in p.support, q.sum (λ j a, (monomial (i + j)) (p.coeff i * a)) :=
begin
apply to_finsupp_injective,
rcases p, rcases q,
simp [support, sum, coeff, to_finsupp_sum],
refl
end
@[simp] lemma sum_zero_index {S : Type*} [add_comm_monoid S] (f : ℕ → R → S) :
(0 : R[X]).sum f = 0 :=
by simp [sum]
@[simp] lemma sum_monomial_index {S : Type*} [add_comm_monoid S]
(n : ℕ) (a : R) (f : ℕ → R → S) (hf : f n 0 = 0) :
(monomial n a : R[X]).sum f = f n a :=
begin
by_cases h : a = 0,
{ simp [h, hf] },
{ simp [sum, support_monomial, h, coeff_monomial] }
end
@[simp] lemma sum_C_index {a} {β} [add_comm_monoid β] {f : ℕ → R → β} (h : f 0 0 = 0) :
(C a).sum f = f 0 a :=
sum_monomial_index 0 a f h
-- the assumption `hf` is only necessary when the ring is trivial
@[simp] lemma sum_X_index {S : Type*} [add_comm_monoid S] {f : ℕ → R → S} (hf : f 1 0 = 0) :
(X : R[X]).sum f = f 1 1 :=
sum_monomial_index 1 1 f hf
lemma sum_add_index {S : Type*} [add_comm_monoid S] (p q : R[X])
(f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) (h_add : ∀a b₁ b₂, f a (b₁ + b₂) = f a b₁ + f a b₂) :
(p + q).sum f = p.sum f + q.sum f :=
begin
rcases p, rcases q,
simp only [←of_finsupp_add, sum, support, coeff, pi.add_apply, coe_add],
exact finsupp.sum_add_index' hf h_add,
end
lemma sum_add' {S : Type*} [add_comm_monoid S] (p : R[X]) (f g : ℕ → R → S) :
p.sum (f + g) = p.sum f + p.sum g :=
by simp [sum_def, finset.sum_add_distrib]
lemma sum_add {S : Type*} [add_comm_monoid S] (p : R[X]) (f g : ℕ → R → S) :
p.sum (λ n x, f n x + g n x) = p.sum f + p.sum g :=
sum_add' _ _ _
lemma sum_smul_index {S : Type*} [add_comm_monoid S] (p : R[X]) (b : R)
(f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) : (b • p).sum f = p.sum (λ n a, f n (b * a)) :=
begin
rcases p,
simpa [sum, support, coeff] using finsupp.sum_smul_index hf,
end
lemma sum_monomial_eq : ∀ p : R[X], p.sum (λ n a, monomial n a) = p
| ⟨p⟩ := (of_finsupp_sum _ _).symm.trans (congr_arg _ $ finsupp.sum_single _)
lemma sum_C_mul_X_eq (p : R[X]) : p.sum (λn a, C a * X^n) = p :=
by simp_rw [←monomial_eq_C_mul_X, sum_monomial_eq]
/-- `erase p n` is the polynomial `p` in which the `X^n` term has been erased. -/
@[irreducible] definition erase (n : ℕ) : R[X] → R[X]
| ⟨p⟩ := ⟨p.erase n⟩
@[simp] lemma to_finsupp_erase (p : R[X]) (n : ℕ) :
to_finsupp (p.erase n) = (p.to_finsupp).erase n :=
by { rcases p, simp only [erase] }
@[simp] lemma of_finsupp_erase (p : add_monoid_algebra R ℕ) (n : ℕ) :
(⟨p.erase n⟩ : R[X]) = (⟨p⟩ : R[X]).erase n :=
by { rcases p, simp only [erase] }
@[simp] lemma support_erase (p : R[X]) (n : ℕ) :
support (p.erase n) = (support p).erase n :=
by { rcases p, simp only [support, erase, support_erase] }
lemma monomial_add_erase (p : R[X]) (n : ℕ) : monomial n (coeff p n) + p.erase n = p :=
to_finsupp_injective $ begin
rcases p,
rw [to_finsupp_add, to_finsupp_monomial, to_finsupp_erase, coeff],
exact finsupp.single_add_erase _ _,
end
lemma coeff_erase (p : R[X]) (n i : ℕ) :
(p.erase n).coeff i = if i = n then 0 else p.coeff i :=
begin
rcases p,
simp only [erase, coeff],
convert rfl
end
@[simp] lemma erase_zero (n : ℕ) : (0 : R[X]).erase n = 0 :=
to_finsupp_injective $ by simp
@[simp] lemma erase_monomial {n : ℕ} {a : R} : erase n (monomial n a) = 0 :=
to_finsupp_injective $ by simp
@[simp] lemma erase_same (p : R[X]) (n : ℕ) : coeff (p.erase n) n = 0 :=
by simp [coeff_erase]
@[simp] lemma erase_ne (p : R[X]) (n i : ℕ) (h : i ≠ n) :
coeff (p.erase n) i = coeff p i :=
by simp [coeff_erase, h]
section update
/-- Replace the coefficient of a `p : polynomial p` at a given degree `n : ℕ`
by a given value `a : R`. If `a = 0`, this is equal to `p.erase n`
If `p.nat_degree < n` and `a ≠ 0`, this increases the degree to `n`. -/
def update (p : R[X]) (n : ℕ) (a : R) :
R[X] :=
polynomial.of_finsupp (p.to_finsupp.update n a)
lemma coeff_update (p : R[X]) (n : ℕ) (a : R) :
(p.update n a).coeff = function.update p.coeff n a :=
begin
ext,
cases p,
simp only [coeff, update, function.update_apply, coe_update],
end
lemma coeff_update_apply (p : R[X]) (n : ℕ) (a : R) (i : ℕ) :
(p.update n a).coeff i = if (i = n) then a else p.coeff i :=
by rw [coeff_update, function.update_apply]
@[simp] lemma coeff_update_same (p : R[X]) (n : ℕ) (a : R) :
(p.update n a).coeff n = a :=
by rw [p.coeff_update_apply, if_pos rfl]
lemma coeff_update_ne (p : R[X]) {n : ℕ} (a : R) {i : ℕ} (h : i ≠ n) :
(p.update n a).coeff i = p.coeff i :=
by rw [p.coeff_update_apply, if_neg h]
@[simp] lemma update_zero_eq_erase (p : R[X]) (n : ℕ) :
p.update n 0 = p.erase n :=
by { ext, rw [coeff_update_apply, coeff_erase] }
lemma support_update (p : R[X]) (n : ℕ) (a : R) [decidable (a = 0)] :
support (p.update n a) = if a = 0 then p.support.erase n else insert n p.support :=
by { cases p, simp only [support, update, support_update], congr }
lemma support_update_zero (p : R[X]) (n : ℕ) :
support (p.update n 0) = p.support.erase n :=
by rw [update_zero_eq_erase, support_erase]
lemma support_update_ne_zero (p : R[X]) (n : ℕ) {a : R} (ha : a ≠ 0) :
support (p.update n a) = insert n p.support :=
by classical; rw [support_update, if_neg ha]
end update
end semiring
section comm_semiring
variables [comm_semiring R]
instance : comm_semiring R[X] :=
function.injective.comm_semiring to_finsupp to_finsupp_injective
to_finsupp_zero to_finsupp_one to_finsupp_add to_finsupp_mul
(λ _ _, to_finsupp_smul _ _) to_finsupp_pow (λ _, rfl)
end comm_semiring
section ring
variables [ring R]
instance : has_int_cast R[X] := ⟨λ n, of_finsupp n⟩
instance : ring R[X] :=
function.injective.ring to_finsupp to_finsupp_injective
to_finsupp_zero to_finsupp_one to_finsupp_add to_finsupp_mul to_finsupp_neg to_finsupp_sub
(λ _ _, to_finsupp_smul _ _) (λ _ _, to_finsupp_smul _ _) to_finsupp_pow (λ _, rfl) (λ _, rfl)
@[simp] lemma coeff_neg (p : R[X]) (n : ℕ) : coeff (-p) n = -coeff p n :=
by { rcases p, rw [←of_finsupp_neg, coeff, coeff, finsupp.neg_apply] }
@[simp]
lemma coeff_sub (p q : R[X]) (n : ℕ) : coeff (p - q) n = coeff p n - coeff q n :=
by { rcases p, rcases q, rw [←of_finsupp_sub, coeff, coeff, coeff, finsupp.sub_apply] }
@[simp] lemma monomial_neg (n : ℕ) (a : R) : monomial n (-a) = -(monomial n a) :=
by rw [eq_neg_iff_add_eq_zero, ←monomial_add, neg_add_self, monomial_zero_right]
@[simp] lemma support_neg {p : R[X]} : (-p).support = p.support :=
by { rcases p, rw [←of_finsupp_neg, support, support, finsupp.support_neg] }
@[simp] lemma C_eq_int_cast (n : ℤ) : C (n : R) = n := map_int_cast C n
end ring
instance [comm_ring R] : comm_ring R[X] :=
function.injective.comm_ring to_finsupp to_finsupp_injective
to_finsupp_zero to_finsupp_one to_finsupp_add to_finsupp_mul to_finsupp_neg to_finsupp_sub
(λ _ _, to_finsupp_smul _ _) (λ _ _, to_finsupp_smul _ _) to_finsupp_pow (λ _, rfl) (λ _, rfl)
section nonzero_semiring
variables [semiring R] [nontrivial R]
instance : nontrivial R[X] :=
begin
have h : nontrivial (add_monoid_algebra R ℕ) := by apply_instance,
rcases h.exists_pair_ne with ⟨x, y, hxy⟩,
refine ⟨⟨⟨x⟩, ⟨y⟩, _⟩⟩,
simp [hxy],
end
lemma X_ne_zero : (X : R[X]) ≠ 0 :=
mt (congr_arg (λ p, coeff p 1)) (by simp)
end nonzero_semiring
@[simp] lemma nontrivial_iff [semiring R] : nontrivial R[X] ↔ nontrivial R :=
⟨λ h, let ⟨r, s, hrs⟩ := @exists_pair_ne _ h in nontrivial.of_polynomial_ne hrs,
λ h, @polynomial.nontrivial _ _ h⟩
section repr
variables [semiring R]
open_locale classical
instance [has_repr R] : has_repr R[X] :=
⟨λ p, if p = 0 then "0"
else (p.support.sort (≤)).foldr
(λ n a, a ++ (if a = "" then "" else " + ") ++
if n = 0
then "C (" ++ repr (coeff p n) ++ ")"
else if n = 1
then if (coeff p n) = 1 then "X" else "C (" ++ repr (coeff p n) ++ ") * X"
else if (coeff p n) = 1 then "X ^ " ++ repr n
else "C (" ++ repr (coeff p n) ++ ") * X ^ " ++ repr n) ""⟩
end repr
end polynomial
|
220f369a62f751728101304e158e862600342093 | 5412d79aa1dc0b521605c38bef9f0d4557b5a29d | /src/Lean/Meta/FunInfo.lean | 16004ad2b10727be9f416a13cb4ffccfe71d32a4 | [
"Apache-2.0"
] | permissive | smunix/lean4 | a450ec0927dc1c74816a1bf2818bf8600c9fc9bf | 3407202436c141e3243eafbecb4b8720599b970a | refs/heads/master | 1,676,334,875,188 | 1,610,128,510,000 | 1,610,128,521,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,092 | 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.Meta.Basic
import Lean.Meta.InferType
namespace Lean.Meta
@[inline] private def checkFunInfoCache (fn : Expr) (maxArgs? : Option Nat) (k : MetaM FunInfo) : MetaM FunInfo := do
let s ← get
let t ← getTransparency
match s.cache.funInfo.find? ⟨t, fn, maxArgs?⟩ with
| some finfo => pure finfo
| none => do
let finfo ← k
modify fun s => { s with cache := { s.cache with funInfo := s.cache.funInfo.insert ⟨t, fn, maxArgs?⟩ finfo } }
pure finfo
@[inline] private def whenHasVar {α} (e : Expr) (deps : α) (k : α → α) : α :=
if e.hasFVar then k deps else deps
private def collectDeps (fvars : Array Expr) (e : Expr) : Array Nat :=
let rec visit : Expr → Array Nat → Array Nat
| e@(Expr.app f a _), deps => whenHasVar e deps (visit a ∘ visit f)
| e@(Expr.forallE _ d b _), deps => whenHasVar e deps (visit b ∘ visit d)
| e@(Expr.lam _ d b _), deps => whenHasVar e deps (visit b ∘ visit d)
| e@(Expr.letE _ t v b _), deps => whenHasVar e deps (visit b ∘ visit v ∘ visit t)
| Expr.proj _ _ e _, deps => visit e deps
| Expr.mdata _ e _, deps => visit e deps
| e@(Expr.fvar _ _), deps =>
match fvars.indexOf? e with
| none => deps
| some i => if deps.contains i.val then deps else deps.push i.val
| _, deps => deps
let deps := visit e #[]
deps.qsort (fun i j => i < j)
/-- Update `hasFwdDeps` fields using new `backDeps` -/
private def updateHasFwdDeps (pinfo : Array ParamInfo) (backDeps : Array Nat) : Array ParamInfo :=
if backDeps.size == 0 then
pinfo
else
-- update hasFwdDeps fields
pinfo.mapIdx fun i info =>
if info.hasFwdDeps then info
else if backDeps.contains i then
{ info with hasFwdDeps := true }
else
info
private def getFunInfoAux (fn : Expr) (maxArgs? : Option Nat) : MetaM FunInfo :=
checkFunInfoCache fn maxArgs? do
let fnType ← inferType fn
withTransparency TransparencyMode.default do
forallBoundedTelescope fnType maxArgs? fun fvars type => do
let mut pinfo := #[]
for i in [:fvars.size] do
let fvar := fvars[i]
let decl ← getFVarLocalDecl fvar
let backDeps := collectDeps fvars decl.type
pinfo := updateHasFwdDeps pinfo backDeps
pinfo := pinfo.push {
backDeps := backDeps,
implicit := decl.binderInfo == BinderInfo.implicit,
instImplicit := decl.binderInfo == BinderInfo.instImplicit }
let resultDeps := collectDeps fvars type
let pinfo := updateHasFwdDeps pinfo resultDeps
pure { resultDeps := resultDeps, paramInfo := pinfo }
def getFunInfo (fn : Expr) : MetaM FunInfo :=
getFunInfoAux fn none
def getFunInfoNArgs (fn : Expr) (nargs : Nat) : MetaM FunInfo :=
getFunInfoAux fn (some nargs)
end Lean.Meta
|
38af759bf9898bc78a2c17e62dc34b7484e7643b | 3f48345ac9bbaa421714efc9872a0409379bb4ae | /src/set_category/colimits/Coequalizer.lean | 8a77f93125bbf9f2801abd080b88cbcdd8e53035 | [] | no_license | QaisHamarneh/Coalgebra-in-Lean | b4318ee6d83780e5c734eb78fed98b1fe8016f7e | bd0452df98bc64b608e5dfd7babc42c301bb6a46 | refs/heads/master | 1,663,371,200,241 | 1,661,004,695,000 | 1,661,004,695,000 | 209,798,828 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,246 | lean | import tactic.tidy
import set_category.diagram_lemmas
import help_functions
import category_theory.category
namespace Coequalizer
open set
diagram_lemmas
classical
function
help_functions
category_theory
universe u
local notation f ` ⊚ `:80 g:80 := category_struct.comp g f
def is_coequalizer {X : Type u} [category X]
{A B C : X}
(f g : A ⟶ B)
(c : B ⟶ C) : Prop :=
c ⊚ f = c ⊚ g ∧
Π (Q : X) (q : B ⟶ Q),
q ⊚ f = q ⊚ g →
∃! h : C ⟶ Q , h ⊚ c = q
variables {A B C : Type u}
variables (f g : A ⟶ B)
def R : B → B → Prop :=
λ b₁ b₂ , ∃ a , f a = b₁ ∧ g a = b₂
def Θ : B → B → Prop := eqv_gen (R f g)
def Θ_setoid : setoid B := eqv_gen.setoid (R f g)
definition theta := quotient (Θ_setoid f g)
local notation `⟦`:max a `⟧`:0 := @quotient.mk B (Θ_setoid f g) a
definition coequalizer : B → theta f g := λ b, ⟦b⟧
open eqv_gen
lemma quot_is_surjective : surjective (coequalizer f g) :=
by tidy
lemma coequalizer_kern :
Π (Q : Type u) (q : B → Q),
q ∘ f = q ∘ g → sub_kern (coequalizer f g) q :=
begin
intros Q q qfg,
let co := (coequalizer f g),
let ker := kern co,
let ker_q := kern q,
intros b₁ b₂ kb1b2,
have quotb1b2 : ⟦b₁⟧ = ⟦b₂⟧ := kb1b2,
let Θb1b2 : eqv_gen (R f g) b₁ b₂ :=
@quotient.exact B (Θ_setoid f g)
b₁ b₂
quotb1b2,
apply eqv_gen.rec_on Θb1b2,
exact (λ b₁ b₂ (h: ∃ a : A , f a = b₁ ∧ g a = b₂),
let a : A := some h in
have fa_ga : f a = b₁ ∧ g a = b₂ := some_spec h,
calc q b₁ = (q ∘ f) a : by simp [some_spec h]
... = (q ∘ g) a : by simp [qfg]
... = q b₂ : by simp [some_spec h]
),
-- refl : ∀ (x : B), ker_q x x
exact (λ x, rfl),
-- symm : ∀ (x y : B), eqv_gen (R f g) x y → ker_q x y → ker_q y x
exact (λ x y _ (h : q x = q y), eq.symm h),
-- trans :∀ (x y z : B),
-- eqv_gen (R f g) x y → eqv_gen (R f g) y z →
-- ker_q x y → ker_q y z → ker_q x z
exact (λ x y z _ _ (h₁ : q x = q y) (h₂ : q y = q z), eq.trans h₁ h₂),
end
lemma quot_is_coequalizer
: is_coequalizer f g (coequalizer f g) :=
⟨
have element : ∀ a : A,
((coequalizer f g) ∘ f) a = ((coequalizer f g) ∘ g) a
:= assume a,
have Rfg : R f g (f a) (g a) := exists.intro a ⟨rfl , rfl⟩,
have Θfg : Θ f g (f a) (g a) := eqv_gen.rel (f a) (g a) Rfg,
quot.sound Θfg,
funext element
,
begin
intros Q q qfg,
let co := (coequalizer f g),
have sub_ker : sub_kern co q := coequalizer_kern f g Q q qfg,
exact
(diagram_surjective
co
q
(quot_is_surjective f g)).elim_right
sub_ker
end
⟩
end Coequalizer |
3a1d8ce0f99bc3fa71d63a178124051498b7b678 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/algebraic_geometry/presheafed_space.lean | f84b929f8c8fe292d1ecd65e3dfe8fa82e2b1d85 | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,942 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import topology.sheaves.presheaf
/-!
# Presheafed spaces
Introduces the category of topological spaces equipped with a presheaf (taking values in an
arbitrary target category `C`.)
We further describe how to apply functors and natural transformations to the values of the
presheaves.
-/
universes v u
open category_theory
open Top
open topological_space
open opposite
open category_theory.category category_theory.functor
variables (C : Type u) [category.{v} C]
local attribute [tidy] tactic.op_induction'
namespace algebraic_geometry
/-- A `PresheafedSpace C` is a topological space equipped with a presheaf of `C`s. -/
structure PresheafedSpace :=
(carrier : Top)
(presheaf : carrier.presheaf C)
variables {C}
namespace PresheafedSpace
attribute [protected] presheaf
instance coe_carrier : has_coe (PresheafedSpace C) Top :=
{ coe := λ X, X.carrier }
@[simp] lemma as_coe (X : PresheafedSpace C) : X.carrier = (X : Top.{v}) := rfl
@[simp] lemma mk_coe (carrier) (presheaf) : (({ carrier := carrier, presheaf := presheaf } :
PresheafedSpace.{v} C) : Top.{v}) = carrier := rfl
instance (X : PresheafedSpace.{v} C) : topological_space X := X.carrier.str
/-- The constant presheaf on `X` with value `Z`. -/
def const (X : Top) (Z : C) : PresheafedSpace C :=
{ carrier := X,
presheaf :=
{ obj := λ U, Z,
map := λ U V f, 𝟙 Z, } }
instance [inhabited C] : inhabited (PresheafedSpace C) := ⟨const (Top.of pempty) (default C)⟩
/-- A morphism between presheafed spaces `X` and `Y` consists of a continuous map
`f` between the underlying topological spaces, and a (notice contravariant!) map
from the presheaf on `Y` to the pushforward of the presheaf on `X` via `f`. -/
structure hom (X Y : PresheafedSpace C) :=
(base : (X : Top.{v}) ⟶ (Y : Top.{v}))
(c : Y.presheaf ⟶ base _* X.presheaf)
@[ext] lemma ext {X Y : PresheafedSpace C} (α β : hom X Y)
(w : α.base = β.base)
(h : α.c ≫ (whisker_right (nat_trans.op (opens.map_iso _ _ w).inv) X.presheaf) = β.c) :
α = β :=
begin
cases α, cases β,
dsimp [presheaf.pushforward_obj] at *,
tidy, -- TODO including `injections` would make tidy work earlier.
end
.
/-- The identity morphism of a `PresheafedSpace`. -/
def id (X : PresheafedSpace C) : hom X X :=
{ base := 𝟙 (X : Top.{v}),
c := (functor.left_unitor _).inv ≫ whisker_right (nat_trans.op (opens.map_id X.carrier).hom) _ }
instance hom_inhabited (X : PresheafedSpace C) : inhabited (hom X X) := ⟨id X⟩
/-- Composition of morphisms of `PresheafedSpace`s. -/
def comp {X Y Z : PresheafedSpace C} (α : hom X Y) (β : hom Y Z) : hom X Z :=
{ base := α.base ≫ β.base,
c := β.c ≫ (whisker_left (opens.map β.base).op α.c) ≫ (Top.presheaf.pushforward.comp _ _ _).inv }
variables (C)
section
local attribute [simp] id comp
/- The proofs below can be done by `tidy`, but it is too slow,
and we don't have a tactic caching mechanism. -/
/-- The category of PresheafedSpaces. Morphisms are pairs, a continuous map and a presheaf map
from the presheaf on the target to the pushforward of the presheaf on the source. -/
instance category_of_PresheafedSpaces : category (PresheafedSpace C) :=
{ hom := hom,
id := id,
comp := λ X Y Z f g, comp f g,
id_comp' := λ X Y f,
begin
ext1, swap,
{ dsimp, simp only [id_comp] }, -- See note [dsimp, simp].
{ ext U, induction U using opposite.rec, cases U,
dsimp,
simp only [presheaf.pushforward.comp_inv_app, opens.map_iso_inv_app],
dsimp,
simp only [comp_id, comp_id, map_id], },
end,
comp_id' := λ X Y f,
begin
ext1, swap,
{ dsimp, simp only [comp_id] },
{ ext U, induction U using opposite.rec, cases U,
dsimp,
simp only [presheaf.pushforward.comp_inv_app, opens.map_iso_inv_app],
dsimp,
simp only [id_comp, comp_id, map_id], }
end,
assoc' := λ W X Y Z f g h,
begin
ext1, swap,
refl,
{ ext U, induction U using opposite.rec, cases U,
dsimp,
simp only [assoc, presheaf.pushforward.comp_inv_app, opens.map_iso_inv_app],
dsimp,
simp only [comp_id, id_comp, map_id], }
end }
end
variables {C}
@[simp] lemma id_base (X : PresheafedSpace C) :
((𝟙 X) : X ⟶ X).base = (𝟙 (X : Top.{v})) := rfl
lemma id_c (X : PresheafedSpace C) :
((𝟙 X) : X ⟶ X).c =
(functor.left_unitor _).inv ≫ whisker_right (nat_trans.op (opens.map_id X.carrier).hom) _ := rfl
@[simp] lemma id_c_app (X : PresheafedSpace C) (U) :
((𝟙 X) : X ⟶ X).c.app U = eq_to_hom (by { induction U using opposite.rec, cases U, refl }) :=
by { induction U using opposite.rec, cases U, simp only [id_c], dsimp, simp, }
@[simp] lemma comp_base {X Y Z : PresheafedSpace C} (f : X ⟶ Y) (g : Y ⟶ Z) :
(f ≫ g).base = f.base ≫ g.base := rfl
@[simp] lemma comp_c_app {X Y Z : PresheafedSpace C} (α : X ⟶ Y) (β : Y ⟶ Z) (U) :
(α ≫ β).c.app U = (β.c).app U ≫ (α.c).app (op ((opens.map (β.base)).obj (unop U))) ≫
(Top.presheaf.pushforward.comp _ _ _).inv.app U := rfl
lemma congr_app {X Y : PresheafedSpace C} {α β : X ⟶ Y} (h : α = β) (U) :
α.c.app U = β.c.app U ≫ X.presheaf.map (eq_to_hom (by subst h)) :=
by { subst h, dsimp, simp, }
section
variables (C)
/-- The forgetful functor from `PresheafedSpace` to `Top`. -/
@[simps]
def forget : PresheafedSpace C ⥤ Top :=
{ obj := λ X, (X : Top.{v}),
map := λ X Y f, f.base }
end
/--
The restriction of a presheafed space along an open embedding into the space.
-/
@[simps]
def restrict {U : Top} (X : PresheafedSpace C)
(f : U ⟶ (X : Top.{v})) (h : open_embedding f) : PresheafedSpace C :=
{ carrier := U,
presheaf := h.is_open_map.functor.op ⋙ X.presheaf }
/--
The map from the restriction of a presheafed space.
-/
def of_restrict {U : Top} (X : PresheafedSpace C)
(f : U ⟶ (X : Top.{v})) (h : open_embedding f) :
X.restrict f h ⟶ X :=
{ base := f,
c := { app := λ V, X.presheaf.map $
((h.is_open_map.adjunction.hom_equiv _ _).symm (𝟙 $ (opens.map f).obj $ unop V)).op,
naturality':= λ U V f, show _ = _ ≫ X.presheaf.map _,
by { rw [← map_comp, ← map_comp], refl } } }
/--
The map to the restriction of a presheafed space along the canonical inclusion from the top
subspace.
-/
@[simps]
def to_restrict_top (X : PresheafedSpace C) :
X ⟶ X.restrict (opens.inclusion ⊤) (opens.open_embedding ⊤) :=
{ base := ⟨λ x, ⟨x, trivial⟩, continuous_def.2 $ λ U ⟨S, hS, hSU⟩, hSU ▸ hS⟩,
c := { app := λ U, X.presheaf.map $ (hom_of_le $ λ x hxU, ⟨⟨x, trivial⟩, hxU, rfl⟩ :
(opens.map (⟨λ x, ⟨x, trivial⟩, continuous_def.2 $ λ U ⟨S, hS, hSU⟩, hSU ▸ hS⟩ :
X.1 ⟶ (opens.to_Top X.1).obj ⊤)).obj (unop U) ⟶
(opens.open_embedding ⊤).is_open_map.functor.obj (unop U)).op,
naturality':= λ U V f, show X.presheaf.map _ ≫ _ = _ ≫ X.presheaf.map _,
by { rw [← map_comp, ← map_comp], refl } } }
/--
The isomorphism from the restriction to the top subspace.
-/
@[simps]
def restrict_top_iso (X : PresheafedSpace C) :
X.restrict (opens.inclusion ⊤) (opens.open_embedding ⊤) ≅ X :=
{ hom := X.of_restrict _ _,
inv := X.to_restrict_top,
hom_inv_id' := ext _ _ (concrete_category.hom_ext _ _ $ λ ⟨x, _⟩, rfl) $
nat_trans.ext _ _ $ funext $ λ U, by { induction U using opposite.rec,
dsimp only [nat_trans.comp_app, comp_c_app, to_restrict_top, of_restrict,
whisker_right_app, comp_base, nat_trans.op_app, opens.map_iso_inv_app],
erw [presheaf.pushforward.comp_inv_app, comp_id, ← X.presheaf.map_comp,
← X.presheaf.map_comp, id_c_app],
exact X.presheaf.map_id _ },
inv_hom_id' := ext _ _ rfl $ nat_trans.ext _ _ $ funext $ λ U, by {
induction U using opposite.rec,
dsimp only [nat_trans.comp_app, comp_c_app, of_restrict, to_restrict_top,
whisker_right_app, comp_base, nat_trans.op_app, opens.map_iso_inv_app],
erw [← X.presheaf.map_comp, ← X.presheaf.map_comp, ← X.presheaf.map_comp, id_c_app],
convert eq_to_hom_map X.presheaf _,
erw [op_obj, id_base, opens.map_id_obj], refl } }
/--
The global sections, notated Gamma.
-/
@[simps]
def Γ : (PresheafedSpace C)ᵒᵖ ⥤ C :=
{ obj := λ X, (unop X).presheaf.obj (op ⊤),
map := λ X Y f, f.unop.c.app (op ⊤) ≫ (unop Y).presheaf.map (opens.le_map_top _ _).op,
map_id' := λ X, begin
induction X using opposite.rec,
erw [unop_id_op, id_c_app, eq_to_hom_refl, id_comp],
exact X.presheaf.map_id _
end,
map_comp' := λ X Y Z f g, begin
rw [unop_comp, comp_c_app],
simp_rw category.assoc,
erw [nat_trans.naturality_assoc, presheaf.pushforward.comp_inv_app, id_comp,
category_theory.functor.comp_map, ← map_comp],
refl
end }
lemma Γ_obj_op (X : PresheafedSpace C) : Γ.obj (op X) = X.presheaf.obj (op ⊤) := rfl
lemma Γ_map_op {X Y : PresheafedSpace C} (f : X ⟶ Y) :
Γ.map f.op = f.c.app (op ⊤) ≫ X.presheaf.map (opens.le_map_top _ _).op := rfl
end PresheafedSpace
end algebraic_geometry
open algebraic_geometry algebraic_geometry.PresheafedSpace
variables {C}
namespace category_theory
variables {D : Type u} [category.{v} D]
local attribute [simp] presheaf.pushforward_obj
namespace functor
/-- We can apply a functor `F : C ⥤ D` to the values of the presheaf in any `PresheafedSpace C`,
giving a functor `PresheafedSpace C ⥤ PresheafedSpace D` -/
def map_presheaf (F : C ⥤ D) : PresheafedSpace C ⥤ PresheafedSpace D :=
{ obj := λ X, { carrier := X.carrier, presheaf := X.presheaf ⋙ F },
map := λ X Y f, { base := f.base, c := whisker_right f.c F }, }
@[simp] lemma map_presheaf_obj_X (F : C ⥤ D) (X : PresheafedSpace C) :
((F.map_presheaf.obj X) : Top.{v}) = (X : Top.{v}) := rfl
@[simp] lemma map_presheaf_obj_presheaf (F : C ⥤ D) (X : PresheafedSpace C) :
(F.map_presheaf.obj X).presheaf = X.presheaf ⋙ F := rfl
@[simp] lemma map_presheaf_map_f (F : C ⥤ D) {X Y : PresheafedSpace C} (f : X ⟶ Y) :
(F.map_presheaf.map f).base = f.base := rfl
@[simp] lemma map_presheaf_map_c (F : C ⥤ D) {X Y : PresheafedSpace C} (f : X ⟶ Y) :
(F.map_presheaf.map f).c = whisker_right f.c F := rfl
end functor
namespace nat_trans
/--
A natural transformation induces a natural transformation between the `map_presheaf` functors.
-/
def on_presheaf {F G : C ⥤ D} (α : F ⟶ G) : G.map_presheaf ⟶ F.map_presheaf :=
{ app := λ X,
{ base := 𝟙 _,
c := whisker_left X.presheaf α ≫ (functor.left_unitor _).inv ≫
whisker_right (nat_trans.op (opens.map_id X.carrier).hom) _ }, }
-- TODO Assemble the last two constructions into a functor
-- `(C ⥤ D) ⥤ (PresheafedSpace C ⥤ PresheafedSpace D)`
end nat_trans
end category_theory
|
93ff32c84818db9a9eb2ee29201487b58e6d93f2 | 82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7 | /stage0/src/Lean/Parser/Level.lean | e584d5f6a884d967d71c8ebb9d17e8135849ce8a | [
"Apache-2.0"
] | permissive | banksonian/lean4 | 3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc | 78da6b3aa2840693eea354a41e89fc5b212a5011 | refs/heads/master | 1,673,703,624,165 | 1,605,123,551,000 | 1,605,123,551,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,093 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Sebastian Ullrich
-/
import Lean.Parser.Extra
-- necessary for auto-generation
import Lean.PrettyPrinter.Parenthesizer
import Lean.PrettyPrinter.Formatter
namespace Lean
namespace Parser
builtin_initialize
registerBuiltinParserAttribute `builtinLevelParser `level
@[inline] def levelParser (rbp : Nat := 0) : Parser :=
categoryParser `level rbp
namespace Level
@[builtinLevelParser] def paren := parser! "(" >> levelParser >> ")"
@[builtinLevelParser] def max := parser! nonReservedSymbol "max " true >> many1 (levelParser maxPrec)
@[builtinLevelParser] def imax := parser! nonReservedSymbol "imax " true >> many1 (levelParser maxPrec)
@[builtinLevelParser] def hole := parser! "_"
@[builtinLevelParser] def num := checkPrec maxPrec >> numLit
@[builtinLevelParser] def ident := checkPrec maxPrec >> Parser.ident
@[builtinLevelParser] def addLit := tparser!:65 " + " >> numLit
end Level
end Parser
end Lean
|
2971b1c107bf9b037391577c6514b7bd160701b8 | 22e97a5d648fc451e25a06c668dc03ac7ed7bc25 | /src/number_theory/dioph.lean | 71c5261b96fa53f04d645c4f6fe0172e104739be | [
"Apache-2.0"
] | permissive | keeferrowan/mathlib | f2818da875dbc7780830d09bd4c526b0764a4e50 | aad2dfc40e8e6a7e258287a7c1580318e865817e | refs/heads/master | 1,661,736,426,952 | 1,590,438,032,000 | 1,590,438,032,000 | 266,892,663 | 0 | 0 | Apache-2.0 | 1,590,445,835,000 | 1,590,445,835,000 | null | UTF-8 | Lean | false | false | 36,863 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import number_theory.pell
import data.pfun
universe u
open nat function
namespace int
lemma eq_nat_abs_iff_mul (x n) : nat_abs x = n ↔ (x - n) * (x + n) = 0 :=
begin
refine iff.trans _ mul_eq_zero.symm,
refine iff.trans _ (or_congr sub_eq_zero add_eq_zero_iff_eq_neg).symm,
exact ⟨λe, by rw ← e; apply nat_abs_eq,
λo, by cases o; subst x; simp [nat_abs_of_nat]⟩
end
end int
/-- An alternate definition of `fin n` defined as an inductive type
instead of a subtype of `nat`. This is useful for its induction
principle and different definitional equalities. -/
inductive fin2 : ℕ → Type
| fz {n} : fin2 (succ n)
| fs {n} : fin2 n → fin2 (succ n)
namespace fin2
@[elab_as_eliminator]
protected def cases' {n} {C : fin2 (succ n) → Sort u} (H1 : C fz) (H2 : Π n, C (fs n)) :
Π (i : fin2 (succ n)), C i
| fz := H1
| (fs n) := H2 n
def elim0 {C : fin2 0 → Sort u} : Π (i : fin2 0), C i.
/-- convert a `fin2` into a `nat` -/
def to_nat : Π {n}, fin2 n → ℕ
| ._ (@fz n) := 0
| ._ (@fs n i) := succ (to_nat i)
/-- convert a `nat` into a `fin2` if it is in range -/
def opt_of_nat : Π {n} (k : ℕ), option (fin2 n)
| 0 _ := none
| (succ n) 0 := some fz
| (succ n) (succ k) := fs <$> @opt_of_nat n k
/-- `i + k : fin2 (n + k)` when `i : fin2 n` and `k : ℕ` -/
def add {n} (i : fin2 n) : Π k, fin2 (n + k)
| 0 := i
| (succ k) := fs (add k)
/-- `left k` is the embedding `fin2 n → fin2 (k + n)` -/
def left (k) : Π {n}, fin2 n → fin2 (k + n)
| ._ (@fz n) := fz
| ._ (@fs n i) := fs (left i)
/-- `insert_perm a` is a permutation of `fin2 n` with the following properties:
* `insert_perm a i = i+1` if `i < a`
* `insert_perm a a = 0`
* `insert_perm a i = i` if `i > a` -/
def insert_perm : Π {n}, fin2 n → fin2 n → fin2 n
| ._ (@fz n) (@fz ._) := fz
| ._ (@fz n) (@fs ._ j) := fs j
| ._ (@fs (succ n) i) (@fz ._) := fs fz
| ._ (@fs (succ n) i) (@fs ._ j) := match insert_perm i j with fz := fz | fs k := fs (fs k) end
/-- `remap_left f k : fin2 (m + k) → fin2 (n + k)` applies the function
`f : fin2 m → fin2 n` to inputs less than `m`, and leaves the right part
on the right (that is, `remap_left f k (m + i) = n + i`). -/
def remap_left {m n} (f : fin2 m → fin2 n) : Π k, fin2 (m + k) → fin2 (n + k)
| 0 i := f i
| (succ k) (@fz ._) := fz
| (succ k) (@fs ._ i) := fs (remap_left _ i)
/-- This is a simple type class inference prover for proof obligations
of the form `m < n` where `m n : ℕ`. -/
class is_lt (m n : ℕ) := (h : m < n)
instance is_lt.zero (n) : is_lt 0 (succ n) := ⟨succ_pos _⟩
instance is_lt.succ (m n) [l : is_lt m n] : is_lt (succ m) (succ n) := ⟨succ_lt_succ l.h⟩
/-- Use type class inference to infer the boundedness proof, so that we
can directly convert a `nat` into a `fin2 n`. This supports
notation like `&1 : fin 3`. -/
def of_nat' : Π {n} m [is_lt m n], fin2 n
| 0 m ⟨h⟩ := absurd h (not_lt_zero _)
| (succ n) 0 ⟨h⟩ := fz
| (succ n) (succ m) ⟨h⟩ := fs (@of_nat' n m ⟨lt_of_succ_lt_succ h⟩)
local prefix `&`:max := of_nat'
end fin2
open fin2
/-- Alternate definition of `vector` based on `fin2`. -/
def vector3 (α : Type u) (n : ℕ) : Type u := fin2 n → α
namespace vector3
/-- The empty vector -/
@[pattern] def nil {α} : vector3 α 0.
/-- The vector cons operation -/
@[pattern] def cons {α} {n} (a : α) (v : vector3 α n) : vector3 α (succ n) :=
λi, by {refine i.cases' _ _, exact a, exact v}
/- We do not want to make the following notation global, because then these expressions will be
overloaded, and only the expected type will be able to disambiguate the meaning. Worse: Lean will
try to insert a coercion from `vector3 α _` to `list α`, if a list is expected. -/
localized "notation `[` l:(foldr `, ` (h t, vector3.cons h t) nil `]`) := l" in vector3
notation a :: b := cons a b
@[simp] theorem cons_fz {α} {n} (a : α) (v : vector3 α n) : (a :: v) fz = a := rfl
@[simp] theorem cons_fs {α} {n} (a : α) (v : vector3 α n) (i) : (a :: v) (fs i) = v i := rfl
/-- Get the `i`th element of a vector -/
@[reducible] def nth {α} {n} (i : fin2 n) (v : vector3 α n) : α := v i
/-- Construct a vector from a function on `fin2`. -/
@[reducible] def of_fn {α} {n} (f : fin2 n → α) : vector3 α n := f
/-- Get the head of a nonempty vector. -/
def head {α} {n} (v : vector3 α (succ n)) : α := v fz
/-- Get the tail of a nonempty vector. -/
def tail {α} {n} (v : vector3 α (succ n)) : vector3 α n := λi, v (fs i)
theorem eq_nil {α} (v : vector3 α 0) : v = [] :=
funext $ λi, match i with end
theorem cons_head_tail {α} {n} (v : vector3 α (succ n)) : head v :: tail v = v :=
funext $ λi, fin2.cases' rfl (λ_, rfl) i
def nil_elim {α} {C : vector3 α 0 → Sort u} (H : C []) (v : vector3 α 0) : C v :=
by rw eq_nil v; apply H
def cons_elim {α n} {C : vector3 α (succ n) → Sort u} (H : Π (a : α) (t : vector3 α n), C (a :: t))
(v : vector3 α (succ n)) : C v :=
by rw ← (cons_head_tail v); apply H
@[simp] theorem cons_elim_cons {α n C H a t} : @cons_elim α n C H (a :: t) = H a t := rfl
@[elab_as_eliminator]
protected def rec_on {α} {C : Π {n}, vector3 α n → Sort u} {n} (v : vector3 α n)
(H0 : C [])
(Hs : Π {n} (a) (w : vector3 α n), C w → C (a :: w)) : C v :=
nat.rec_on n
(λv, v.nil_elim H0)
(λn IH v, v.cons_elim (λa t, Hs _ _ (IH _))) v
@[simp] theorem rec_on_nil {α C H0 Hs} : @vector3.rec_on α @C 0 [] H0 @Hs = H0 :=
rfl
@[simp] theorem rec_on_cons {α C H0 Hs n a v} :
@vector3.rec_on α @C (succ n) (a :: v) H0 @Hs = Hs a v (@vector3.rec_on α @C n v H0 @Hs) :=
rfl
/-- Append two vectors -/
def append {α} {m} (v : vector3 α m) {n} (w : vector3 α n) : vector3 α (n+m) :=
nat.rec_on m (λ_, w) (λm IH v, v.cons_elim $ λa t, @fin2.cases' (n+m) (λ_, α) a (IH t)) v
local infix ` +-+ `:65 := vector3.append
@[simp] theorem append_nil {α} {n} (w : vector3 α n) : [] +-+ w = w := rfl
@[simp] theorem append_cons {α} (a : α) {m} (v : vector3 α m) {n} (w : vector3 α n) :
(a::v) +-+ w = a :: (v +-+ w) := rfl
@[simp] theorem append_left {α} : ∀ {m} (i : fin2 m) (v : vector3 α m) {n} (w : vector3 α n),
(v +-+ w) (left n i) = v i
| ._ (@fz m) v n w := v.cons_elim (λa t, by simp [*, left])
| ._ (@fs m i) v n w := v.cons_elim (λa t, by simp [*, left])
@[simp] theorem append_add {α} : ∀ {m} (v : vector3 α m) {n} (w : vector3 α n) (i : fin2 n),
(v +-+ w) (add i m) = w i
| 0 v n w i := rfl
| (succ m) v n w i := v.cons_elim (λa t, by simp [*, add])
/-- Insert `a` into `v` at index `i`. -/
def insert {α} (a : α) {n} (v : vector3 α n) (i : fin2 (succ n)) : vector3 α (succ n) :=
λj, (a :: v) (insert_perm i j)
@[simp] theorem insert_fz {α} (a : α) {n} (v : vector3 α n) : insert a v fz = a :: v :=
by refine funext (λj, j.cases' _ _); intros; refl
@[simp] theorem insert_fs {α} (a : α) {n} (b : α) (v : vector3 α n) (i : fin2 (succ n)) :
insert a (b :: v) (fs i) = b :: insert a v i :=
funext $ λj, by {
refine j.cases' _ (λj, _); simp [insert, insert_perm],
refine fin2.cases' _ _ (insert_perm i j); simp [insert_perm] }
theorem append_insert {α} (a : α) {k} (t : vector3 α k) {n} (v : vector3 α n) (i : fin2 (succ n)) (e : succ n + k = succ (n + k)) :
insert a (t +-+ v) (eq.rec_on e (i.add k)) = eq.rec_on e (t +-+ insert a v i) :=
begin
refine vector3.rec_on t (λe, _) (λk b t IH e, _) e, refl,
have e' := succ_add n k,
change insert a (b :: (t +-+ v)) (eq.rec_on (congr_arg succ e') (fs (add i k)))
= eq.rec_on (congr_arg succ e') (b :: (t +-+ insert a v i)),
rw ← (eq.drec_on e' rfl : fs (eq.rec_on e' (i.add k) : fin2 (succ (n + k))) = eq.rec_on (congr_arg succ e') (fs (i.add k))),
simp, rw IH, exact eq.drec_on e' rfl
end
end vector3
section vector3
open vector3
open_locale vector3
/-- "Curried" exists, i.e. ∃ x1 ... xn, f [x1, ..., xn] -/
def vector_ex {α} : Π k, (vector3 α k → Prop) → Prop
| 0 f := f []
| (succ k) f := ∃x : α, vector_ex k (λv, f (x :: v))
/-- "Curried" forall, i.e. ∀ x1 ... xn, f [x1, ..., xn] -/
def vector_all {α} : Π k, (vector3 α k → Prop) → Prop
| 0 f := f []
| (succ k) f := ∀x : α, vector_all k (λv, f (x :: v))
theorem exists_vector_zero {α} (f : vector3 α 0 → Prop) : Exists f ↔ f [] :=
⟨λ⟨v, fv⟩, by rw ← (eq_nil v); exact fv, λf0, ⟨[], f0⟩⟩
theorem exists_vector_succ {α n} (f : vector3 α (succ n) → Prop) : Exists f ↔ ∃x v, f (x :: v) :=
⟨λ⟨v, fv⟩, ⟨_, _, by rw cons_head_tail v; exact fv⟩, λ⟨x, v, fxv⟩, ⟨_, fxv⟩⟩
theorem vector_ex_iff_exists {α} : ∀ {n} (f : vector3 α n → Prop), vector_ex n f ↔ Exists f
| 0 f := (exists_vector_zero f).symm
| (succ n) f := iff.trans (exists_congr (λx, vector_ex_iff_exists _)) (exists_vector_succ f).symm
theorem vector_all_iff_forall {α} : ∀ {n} (f : vector3 α n → Prop), vector_all n f ↔ ∀ v, f v
| 0 f := ⟨λf0 v, v.nil_elim f0, λal, al []⟩
| (succ n) f := (forall_congr (λx, vector_all_iff_forall (λv, f (x :: v)))).trans
⟨λal v, v.cons_elim al, λal x v, al (x::v)⟩
/-- `vector_allp p v` is equivalent to `∀ i, p (v i)`, but unfolds directly to a conjunction,
i.e. `vector_allp p [0, 1, 2] = p 0 ∧ p 1 ∧ p 2`. -/
def vector_allp {α} (p : α → Prop) {n} (v : vector3 α n) : Prop :=
vector3.rec_on v true (λn a v IH, @vector3.rec_on _ (λn v, Prop) _ v (p a) (λn b v' _, p a ∧ IH))
@[simp] theorem vector_allp_nil {α} (p : α → Prop) : vector_allp p [] = true := rfl
@[simp] theorem vector_allp_singleton {α} (p : α → Prop) (x : α) : vector_allp p [x] = p x := rfl
@[simp] theorem vector_allp_cons {α} (p : α → Prop) {n} (x : α) (v : vector3 α n) :
vector_allp p (x :: v) ↔ p x ∧ vector_allp p v :=
vector3.rec_on v (and_true _).symm (λn a v IH, iff.rfl)
theorem vector_allp_iff_forall {α} (p : α → Prop) {n} (v : vector3 α n) : vector_allp p v ↔ ∀ i, p (v i) :=
begin refine v.rec_on _ _,
{ exact ⟨λ_, fin2.elim0, λ_, trivial⟩ },
{ simp, refine λn a v IH, (and_congr_right (λ_, IH)).trans
⟨λ⟨pa, h⟩ i, by {refine i.cases' _ _, exacts [pa, h]}, λh, ⟨_, λi, _⟩⟩,
{ have h0 := h fz, simp at h0, exact h0 },
{ have hs := h (fs i), simp at hs, exact hs } }
end
theorem vector_allp.imp {α} {p q : α → Prop} (h : ∀ x, p x → q x)
{n} {v : vector3 α n} (al : vector_allp p v) : vector_allp q v :=
(vector_allp_iff_forall _ _).2 (λi, h _ $ (vector_allp_iff_forall _ _).1 al _)
end vector3
/-- `list_all p l` is equivalent to `∀ a ∈ l, p a`, but unfolds directly to a conjunction,
i.e. `list_all p [0, 1, 2] = p 0 ∧ p 1 ∧ p 2`. -/
@[simp] def list_all {α} (p : α → Prop) : list α → Prop
| [] := true
| (x :: []) := p x
| (x :: l) := p x ∧ list_all l
@[simp] theorem list_all_cons {α} (p : α → Prop) (x : α) : ∀ (l : list α), list_all p (x :: l) ↔ p x ∧ list_all p l
| [] := (and_true _).symm
| (x :: l) := iff.rfl
theorem list_all_iff_forall {α} (p : α → Prop) : ∀ (l : list α), list_all p l ↔ ∀ x ∈ l, p x
| [] := (iff_true_intro $ list.ball_nil _).symm
| (x :: l) := by rw [list.ball_cons, ← list_all_iff_forall l]; simp
theorem list_all.imp {α} {p q : α → Prop} (h : ∀ x, p x → q x) : ∀ {l : list α}, list_all p l → list_all q l
| [] := id
| (x :: l) := by simpa using and.imp (h x) list_all.imp
@[simp] theorem list_all_map {α β} {p : β → Prop} (f : α → β) {l : list α} : list_all p (l.map f) ↔ list_all (p ∘ f) l :=
by induction l; simp *
theorem list_all_congr {α} {p q : α → Prop} (h : ∀ x, p x ↔ q x) {l : list α} : list_all p l ↔ list_all q l :=
⟨list_all.imp (λx, (h x).1), list_all.imp (λx, (h x).2)⟩
instance decidable_list_all {α} (p : α → Prop) [decidable_pred p] (l : list α) : decidable (list_all p l) :=
decidable_of_decidable_of_iff (by apply_instance) (list_all_iff_forall _ _).symm
/- poly -/
/-- A predicate asserting that a function is a multivariate integer polynomial.
(We are being a bit lazy here by allowing many representations for multiplication,
rather than only allowing monomials and addition, but the definition is equivalent
and this is easier to use.) -/
inductive is_poly {α} : ((α → ℕ) → ℤ) → Prop
| proj : ∀ i, is_poly (λx : α → ℕ, x i)
| const : Π (n : ℤ), is_poly (λx : α → ℕ, n)
| sub : Π {f g : (α → ℕ) → ℤ}, is_poly f → is_poly g → is_poly (λx, f x - g x)
| mul : Π {f g : (α → ℕ) → ℤ}, is_poly f → is_poly g → is_poly (λx, f x * g x)
/-- The type of multivariate integer polynomials -/
def poly (α : Type u) := {f : (α → ℕ) → ℤ // is_poly f}
namespace poly
section
parameter {α : Type u}
instance : has_coe_to_fun (poly α) := ⟨_, λ f, f.1⟩
/-- The underlying function of a `poly` is a polynomial -/
lemma isp (f : poly α) : is_poly f := f.2
/-- Extensionality for `poly α` -/
lemma ext {f g : poly α} (e : ∀x, f x = g x) : f = g :=
subtype.eq (funext e)
/-- Construct a `poly` given an extensionally equivalent `poly`. -/
def subst (f : poly α) (g : (α → ℕ) → ℤ) (e : ∀x, f x = g x) : poly α :=
⟨g, by rw ← (funext e : coe_fn f = g); exact f.isp⟩
@[simp] theorem subst_eval (f g e x) : subst f g e x = g x := rfl
/-- The `i`th projection function, `x_i`. -/
def proj (i) : poly α := ⟨_, is_poly.proj i⟩
@[simp] theorem proj_eval (i x) : proj i x = x i := rfl
/-- The constant function with value `n : ℤ`. -/
def const (n) : poly α := ⟨_, is_poly.const n⟩
@[simp] theorem const_eval (n x) : const n x = n := rfl
/-- The zero polynomial -/
def zero : poly α := const 0
instance : has_zero (poly α) := ⟨poly.zero⟩
@[simp] theorem zero_eval (x) : (0 : poly α) x = 0 := rfl
/-- The zero polynomial -/
def one : poly α := const 1
instance : has_one (poly α) := ⟨poly.one⟩
@[simp] theorem one_eval (x) : (1 : poly α) x = 1 := rfl
/-- Subtraction of polynomials -/
def sub : poly α → poly α → poly α | ⟨f, pf⟩ ⟨g, pg⟩ :=
⟨_, is_poly.sub pf pg⟩
instance : has_sub (poly α) := ⟨poly.sub⟩
@[simp] theorem sub_eval : Π (f g x), (f - g : poly α) x = f x - g x
| ⟨f, pf⟩ ⟨g, pg⟩ x := rfl
/-- Negation of a polynomial -/
def neg (f : poly α) : poly α := 0 - f
instance : has_neg (poly α) := ⟨poly.neg⟩
@[simp] theorem neg_eval (f x) : (-f : poly α) x = -f x :=
show (0-f) x = _, by simp
/-- Addition of polynomials -/
def add : poly α → poly α → poly α | ⟨f, pf⟩ ⟨g, pg⟩ :=
subst (⟨f, pf⟩ - -⟨g, pg⟩) _
(λx, show f x - (0 - g x) = f x + g x, by simp)
instance : has_add (poly α) := ⟨poly.add⟩
@[simp] theorem add_eval : Π (f g x), (f + g : poly α) x = f x + g x
| ⟨f, pf⟩ ⟨g, pg⟩ x := rfl
/-- Multiplication of polynomials -/
def mul : poly α → poly α → poly α | ⟨f, pf⟩ ⟨g, pg⟩ :=
⟨_, is_poly.mul pf pg⟩
instance : has_mul (poly α) := ⟨poly.mul⟩
@[simp] theorem mul_eval : Π (f g x), (f * g : poly α) x = f x * g x
| ⟨f, pf⟩ ⟨g, pg⟩ x := rfl
instance : comm_ring (poly α) := by refine
{ add := (+),
zero := 0,
neg := has_neg.neg,
mul := (*),
one := 1, .. }; {intros, exact ext (λx, by simp [mul_add, mul_left_comm, mul_comm, add_comm, add_assoc])}
lemma induction {C : poly α → Prop}
(H1 : ∀i, C (proj i)) (H2 : ∀n, C (const n))
(H3 : ∀f g, C f → C g → C (f - g))
(H4 : ∀f g, C f → C g → C (f * g)) (f : poly α) : C f :=
begin
cases f with f pf,
induction pf with i n f g pf pg ihf ihg f g pf pg ihf ihg,
apply H1, apply H2, apply H3 _ _ ihf ihg, apply H4 _ _ ihf ihg
end
/-- The sum of squares of a list of polynomials. This is relevant for
Diophantine equations, because it means that a list of equations
can be encoded as a single equation: `x = 0 ∧ y = 0 ∧ z = 0` is
equivalent to `x^2 + y^2 + z^2 = 0`. -/
def sumsq : list (poly α) → poly α
| [] := 0
| (p::ps) := p*p + sumsq ps
theorem sumsq_nonneg (x) : ∀ l, 0 ≤ sumsq l x
| [] := le_refl 0
| (p::ps) := by rw sumsq; simp [-add_comm];
exact add_nonneg (mul_self_nonneg _) (sumsq_nonneg ps)
theorem sumsq_eq_zero (x) : ∀ l, sumsq l x = 0 ↔ list_all (λa : poly α, a x = 0) l
| [] := eq_self_iff_true _
| (p::ps) := by rw [list_all_cons, ← sumsq_eq_zero ps]; rw sumsq; simp [-add_comm]; exact
⟨λ(h : p x * p x + sumsq ps x = 0),
have p x = 0, from eq_zero_of_mul_self_eq_zero $ le_antisymm
(by rw ← h; have t := add_le_add_left (sumsq_nonneg x ps) (p x * p x); rwa [add_zero] at t)
(mul_self_nonneg _),
⟨this, by simp [this] at h; exact h⟩,
λ⟨h1, h2⟩, by rw [h1, h2]; refl⟩
end
/-- Map the index set of variables, replacing `x_i` with `x_(f i)`. -/
def remap {α β} (f : α → β) (g : poly α) : poly β :=
⟨λv, g $ v ∘ f, g.induction
(λi, by simp; apply is_poly.proj)
(λn, by simp; apply is_poly.const)
(λf g pf pg, by simp; apply is_poly.sub pf pg)
(λf g pf pg, by simp; apply is_poly.mul pf pg)⟩
@[simp] theorem remap_eval {α β} (f : α → β) (g : poly α) (v) : remap f g v = g (v ∘ f) := rfl
end poly
namespace sum
/-- combine two functions into a function on the disjoint union -/
def join {α β γ} (f : α → γ) (g : β → γ) : α ⊕ β → γ :=
by {refine sum.rec _ _, exacts [f, g]}
end sum
local infixr ` ⊗ `:65 := sum.join
open sum
namespace option
/-- Functions from `option` can be combined similarly to `vector.cons` -/
def cons {α β} (a : β) (v : α → β) : option α → β :=
by {refine option.rec _ _, exacts [a, v]}
notation a :: b := cons a b
@[simp] theorem cons_head_tail {α β} (v : option α → β) : v none :: v ∘ some = v :=
funext $ λo, by cases o; refl
end option
/- dioph -/
/-- A set `S ⊆ ℕ^α` is diophantine if there exists a polynomial on
`α ⊕ β` such that `v ∈ S` iff there exists `t : ℕ^β` with `p (v, t) = 0`. -/
def dioph {α : Type u} (S : set (α → ℕ)) : Prop :=
∃ {β : Type u} (p : poly (α ⊕ β)), ∀ (v : α → ℕ), S v ↔ ∃t, p (v ⊗ t) = 0
namespace dioph
section
variables {α β γ : Type u}
theorem ext {S S' : set (α → ℕ)} (d : dioph S) (H : ∀v, S v ↔ S' v) : dioph S' :=
eq.rec d $ show S = S', from set.ext H
theorem of_no_dummies (S : set (α → ℕ)) (p : poly α) (h : ∀ (v : α → ℕ), S v ↔ p v = 0) : dioph S :=
⟨ulift empty, p.remap inl, λv, (h v).trans
⟨λh, ⟨λt, empty.rec _ t.down, by simp; rw [
show (v ⊗ λt:ulift empty, empty.rec _ t.down) ∘ inl = v, from rfl, h]⟩,
λ⟨t, ht⟩, by simp at ht; rwa [show (v ⊗ t) ∘ inl = v, from rfl] at ht⟩⟩
lemma inject_dummies_lem (f : β → γ) (g : γ → option β) (inv : ∀ x, g (f x) = some x)
(p : poly (α ⊕ β)) (v : α → ℕ) : (∃t, p (v ⊗ t) = 0) ↔
(∃t, p.remap (inl ⊗ (inr ∘ f)) (v ⊗ t) = 0) :=
begin
simp, refine ⟨λt, _, λt, _⟩; cases t with t ht,
{ have : (v ⊗ (0 :: t) ∘ g) ∘ (inl ⊗ inr ∘ f) = v ⊗ t :=
funext (λs, by cases s with a b; dsimp [join, (∘)]; try {rw inv}; refl),
exact ⟨(0 :: t) ∘ g, by rwa this⟩ },
{ have : v ⊗ t ∘ f = (v ⊗ t) ∘ (inl ⊗ inr ∘ f) :=
funext (λs, by cases s with a b; refl),
exact ⟨t ∘ f, by rwa this⟩ }
end
theorem inject_dummies {S : set (α → ℕ)} (f : β → γ) (g : γ → option β) (inv : ∀ x, g (f x) = some x)
(p : poly (α ⊕ β)) (h : ∀ (v : α → ℕ), S v ↔ ∃t, p (v ⊗ t) = 0) :
∃ q : poly (α ⊕ γ), ∀ (v : α → ℕ), S v ↔ ∃t, q (v ⊗ t) = 0 :=
⟨p.remap (inl ⊗ (inr ∘ f)), λv, (h v).trans $ inject_dummies_lem f g inv _ _⟩
theorem reindex_dioph {S : set (α → ℕ)} : Π (d : dioph S) (f : α → β), dioph (λv, S (v ∘ f))
| ⟨γ, p, pe⟩ f := ⟨γ, p.remap ((inl ∘ f) ⊗ inr), λv, (pe _).trans $ exists_congr $ λt,
suffices v ∘ f ⊗ t = (v ⊗ t) ∘ (inl ∘ f ⊗ inr), by simp [this],
funext $ λs, by cases s with a b; refl⟩
theorem dioph_list_all (l) (d : list_all dioph l) : dioph (λv, list_all (λS : set (α → ℕ), S v) l) :=
suffices ∃ β (pl : list (poly (α ⊕ β))), ∀ v, list_all (λS : set _, S v) l ↔ ∃t, list_all (λp : poly (α ⊕ β), p (v ⊗ t) = 0) pl,
from let ⟨β, pl, h⟩ := this in ⟨β, poly.sumsq pl, λv, (h v).trans $ exists_congr $ λt, (poly.sumsq_eq_zero _ _).symm⟩,
begin
induction l with S l IH,
exact ⟨ulift empty, [], λv, by simp; exact ⟨λ⟨t⟩, empty.rec _ t, trivial⟩⟩,
simp at d,
exact let ⟨⟨β, p, pe⟩, dl⟩ := d, ⟨γ, pl, ple⟩ := IH dl in
⟨β ⊕ γ, p.remap (inl ⊗ inr ∘ inl) :: pl.map (λq, q.remap (inl ⊗ (inr ∘ inr))), λv,
by simp; exact iff.trans (and_congr (pe v) (ple v))
⟨λ⟨⟨m, hm⟩, ⟨n, hn⟩⟩,
⟨m ⊗ n, by rw [
show (v ⊗ m ⊗ n) ∘ (inl ⊗ inr ∘ inl) = v ⊗ m,
from funext $ λs, by cases s with a b; refl]; exact hm,
by { refine list_all.imp (λq hq, _) hn, dsimp [(∘)],
rw [show (λ (x : α ⊕ γ), (v ⊗ m ⊗ n) ((inl ⊗ λ (x : γ), inr (inr x)) x)) = v ⊗ n,
from funext $ λs, by cases s with a b; refl]; exact hq }⟩,
λ⟨t, hl, hr⟩,
⟨⟨t ∘ inl, by rwa [
show (v ⊗ t) ∘ (inl ⊗ inr ∘ inl) = v ⊗ t ∘ inl,
from funext $ λs, by cases s with a b; refl] at hl⟩,
⟨t ∘ inr, by {
refine list_all.imp (λq hq, _) hr, dsimp [(∘)] at hq,
rwa [show (λ (x : α ⊕ γ), (v ⊗ t) ((inl ⊗ λ (x : γ), inr (inr x)) x)) = v ⊗ t ∘ inr,
from funext $ λs, by cases s with a b; refl] at hq }⟩⟩⟩⟩
end
theorem and_dioph {S S' : set (α → ℕ)} (d : dioph S) (d' : dioph S') : dioph (λv, S v ∧ S' v) :=
dioph_list_all [S, S'] ⟨d, d'⟩
theorem or_dioph {S S' : set (α → ℕ)} : ∀ (d : dioph S) (d' : dioph S'), dioph (λv, S v ∨ S' v)
| ⟨β, p, pe⟩ ⟨γ, q, qe⟩ := ⟨β ⊕ γ, p.remap (inl ⊗ inr ∘ inl) * q.remap (inl ⊗ inr ∘ inr), λv,
begin
refine iff.trans (or_congr ((pe v).trans _) ((qe v).trans _)) (exists_or_distrib.symm.trans (exists_congr $ λt,
(@mul_eq_zero_iff_eq_zero_or_eq_zero _ _ (p ((v ⊗ t) ∘ (inl ⊗ inr ∘ inl))) (q ((v ⊗ t) ∘ (inl ⊗ inr ∘ inr)))).symm)),
exact inject_dummies_lem _ (some ⊗ (λ_, none)) (λx, rfl) _ _,
exact inject_dummies_lem _ ((λ_, none) ⊗ some) (λx, rfl) _ _,
end⟩
/-- A partial function is Diophantine if its graph is Diophantine. -/
def dioph_pfun (f : (α → ℕ) →. ℕ) := dioph (λv : option α → ℕ, f.graph (v ∘ some, v none))
/-- A function is Diophantine if its graph is Diophantine. -/
def dioph_fn (f : (α → ℕ) → ℕ) := dioph (λv : option α → ℕ, f (v ∘ some) = v none)
theorem reindex_dioph_fn {f : (α → ℕ) → ℕ} (d : dioph_fn f) (g : α → β) : dioph_fn (λv, f (v ∘ g)) :=
reindex_dioph d (functor.map g)
theorem ex_dioph {S : set (α ⊕ β → ℕ)} : dioph S → dioph (λv, ∃x, S (v ⊗ x))
| ⟨γ, p, pe⟩ := ⟨β ⊕ γ, p.remap ((inl ⊗ inr ∘ inl) ⊗ inr ∘ inr), λv,
⟨λ⟨x, hx⟩, let ⟨t, ht⟩ := (pe _).1 hx in ⟨x ⊗ t, by simp; rw [
show (v ⊗ x ⊗ t) ∘ ((inl ⊗ inr ∘ inl) ⊗ inr ∘ inr) = (v ⊗ x) ⊗ t,
from funext $ λs, by cases s with a b; try {cases a}; refl]; exact ht⟩,
λ⟨t, ht⟩, ⟨t ∘ inl, (pe _).2 ⟨t ∘ inr, by simp at ht; rwa [
show (v ⊗ t) ∘ ((inl ⊗ inr ∘ inl) ⊗ inr ∘ inr) = (v ⊗ t ∘ inl) ⊗ t ∘ inr,
from funext $ λs, by cases s with a b; try {cases a}; refl] at ht⟩⟩⟩⟩
theorem ex1_dioph {S : set (option α → ℕ)} : dioph S → dioph (λv, ∃x, S (x :: v))
| ⟨β, p, pe⟩ := ⟨option β, p.remap (inr none :: inl ⊗ inr ∘ some), λv,
⟨λ⟨x, hx⟩, let ⟨t, ht⟩ := (pe _).1 hx in ⟨x :: t, by simp; rw [
show (v ⊗ x :: t) ∘ (inr none :: inl ⊗ inr ∘ some) = x :: v ⊗ t,
from funext $ λs, by cases s with a b; try {cases a}; refl]; exact ht⟩,
λ⟨t, ht⟩, ⟨t none, (pe _).2 ⟨t ∘ some, by simp at ht; rwa [
show (v ⊗ t) ∘ (inr none :: inl ⊗ inr ∘ some) = t none :: v ⊗ t ∘ some,
from funext $ λs, by cases s with a b; try {cases a}; refl] at ht⟩⟩⟩⟩
theorem dom_dioph {f : (α → ℕ) →. ℕ} (d : dioph_pfun f) : dioph f.dom :=
cast (congr_arg dioph $ set.ext $ λv, (pfun.dom_iff_graph _ _).symm) (ex1_dioph d)
theorem dioph_fn_iff_pfun (f : (α → ℕ) → ℕ) : dioph_fn f = @dioph_pfun α f :=
by refine congr_arg dioph (set.ext $ λv, _); exact pfun.lift_graph.symm
theorem abs_poly_dioph (p : poly α) : dioph_fn (λv, (p v).nat_abs) :=
by refine of_no_dummies _ ((p.remap some - poly.proj none) * (p.remap some + poly.proj none)) (λv, _);
apply int.eq_nat_abs_iff_mul
theorem proj_dioph (i : α) : dioph_fn (λv, v i) :=
abs_poly_dioph (poly.proj i)
theorem dioph_pfun_comp1 {S : set (option α → ℕ)} (d : dioph S) {f} (df : dioph_pfun f) :
dioph (λv : α → ℕ, ∃ h : f.dom v, S (f.fn v h :: v)) :=
ext (ex1_dioph (and_dioph d df)) $ λv,
⟨λ⟨x, hS, (h: Exists _)⟩, by
rw [show (x :: v) ∘ some = v, from funext $ λs, rfl] at h;
cases h with hf h; refine ⟨hf, _⟩; rw [pfun.fn, h]; exact hS,
λ⟨x, hS⟩, ⟨f.fn v x, hS, show Exists _,
by rw [show (f.fn v x :: v) ∘ some = v, from funext $ λs, rfl]; exact ⟨x, rfl⟩⟩⟩
theorem dioph_fn_comp1 {S : set (option α → ℕ)} (d : dioph S) {f : (α → ℕ) → ℕ} (df : dioph_fn f) :
dioph (λv : α → ℕ, S (f v :: v)) :=
ext (dioph_pfun_comp1 d (cast (dioph_fn_iff_pfun f) df)) $ λv,
⟨λ⟨_, h⟩, h, λh, ⟨trivial, h⟩⟩
end
section
variables {α β γ : Type}
open vector3
open_locale vector3
theorem dioph_fn_vec_comp1 {n} {S : set (vector3 ℕ (succ n))} (d : dioph S) {f : (vector3 ℕ n) → ℕ} (df : dioph_fn f) :
dioph (λv : vector3 ℕ n, S (cons (f v) v)) :=
ext (dioph_fn_comp1 (reindex_dioph d (none :: some)) df) $ λv, by rw [
show option.cons (f v) v ∘ (cons none some) = f v :: v,
from funext $ λs, by cases s with a b; refl]
theorem vec_ex1_dioph (n) {S : set (vector3 ℕ (succ n))} (d : dioph S) : dioph (λv : vector3 ℕ n, ∃x, S (x :: v)) :=
ext (ex1_dioph $ reindex_dioph d (none :: some)) $ λv, exists_congr $ λx, by rw [
show (option.cons x v) ∘ (cons none some) = x :: v,
from funext $ λs, by cases s with a b; refl]
theorem dioph_fn_vec {n} (f : vector3 ℕ n → ℕ) : dioph_fn f ↔ dioph (λv : vector3 ℕ (succ n), f (v ∘ fs) = v fz) :=
⟨λh, reindex_dioph h (fz :: fs), λh, reindex_dioph h (none :: some)⟩
theorem dioph_pfun_vec {n} (f : vector3 ℕ n →. ℕ) : dioph_pfun f ↔ dioph (λv : vector3 ℕ (succ n), f.graph (v ∘ fs, v fz)) :=
⟨λh, reindex_dioph h (fz :: fs), λh, reindex_dioph h (none :: some)⟩
theorem dioph_fn_compn {α : Type} : ∀ {n} {S : set (α ⊕ fin2 n → ℕ)} (d : dioph S) {f : vector3 ((α → ℕ) → ℕ) n} (df : vector_allp dioph_fn f),
dioph (λv : α → ℕ, S (v ⊗ λi, f i v))
| 0 S d f := λdf, ext (reindex_dioph d (id ⊗ fin2.elim0)) $ λv,
by refine eq.to_iff (congr_arg S $ funext $ λs, _); {cases s with a b, refl, cases b}
| (succ n) S d f := f.cons_elim $ λf fl, by simp; exact λ df dfl,
have dioph (λv, S (v ∘ inl ⊗ f (v ∘ inl) :: v ∘ inr)),
from ext (dioph_fn_comp1 (reindex_dioph d (some ∘ inl ⊗ none :: some ∘ inr)) (reindex_dioph_fn df inl)) $
λv, by {refine eq.to_iff (congr_arg S $ funext $ λs, _); cases s with a b, refl, cases b; refl},
have dioph (λv, S (v ⊗ f v :: λ (i : fin2 n), fl i v)),
from @dioph_fn_compn n (λv, S (v ∘ inl ⊗ f (v ∘ inl) :: v ∘ inr)) this _ dfl,
ext this $ λv, by rw [
show cons (f v) (λ (i : fin2 n), fl i v) = λ (i : fin2 (succ n)), (f :: fl) i v,
from funext $ λs, by cases s with a b; refl]
theorem dioph_comp {n} {S : set (vector3 ℕ n)} (d : dioph S)
(f : vector3 ((α → ℕ) → ℕ) n) (df : vector_allp dioph_fn f) : dioph (λv, S (λi, f i v)) :=
dioph_fn_compn (reindex_dioph d inr) df
theorem dioph_fn_comp {n} {f : vector3 ℕ n → ℕ} (df : dioph_fn f)
(g : vector3 ((α → ℕ) → ℕ) n) (dg : vector_allp dioph_fn g) : dioph_fn (λv, f (λi, g i v)) :=
dioph_comp ((dioph_fn_vec _).1 df) ((λv, v none) :: λi v, g i (v ∘ some)) $
by simp; exact ⟨proj_dioph none, (vector_allp_iff_forall _ _).2 $ λi,
reindex_dioph_fn ((vector_allp_iff_forall _ _).1 dg _) _⟩
localized "notation x ` D∧ `:35 y := dioph.and_dioph x y" in dioph
localized "notation x ` D∨ `:35 y := dioph.or_dioph x y" in dioph
localized "notation `D∃`:30 := dioph.vec_ex1_dioph" in dioph
localized "prefix `&`:max := of_nat'" in dioph
theorem proj_dioph_of_nat {n : ℕ} (m : ℕ) [is_lt m n] : dioph_fn (λv : vector3 ℕ n, v &m) :=
proj_dioph &m
localized "prefix `D&`:100 := dioph.proj_dioph_of_nat" in dioph
theorem const_dioph (n : ℕ) : dioph_fn (const (α → ℕ) n) :=
abs_poly_dioph (poly.const n)
localized "prefix `D.`:100 := dioph.const_dioph" in dioph
variables {f g : (α → ℕ) → ℕ} (df : dioph_fn f) (dg : dioph_fn g)
include df dg
theorem dioph_comp2 {S : ℕ → ℕ → Prop} (d : dioph (λv:vector3 ℕ 2, S (v &0) (v &1))) :
dioph (λv, S (f v) (g v)) :=
dioph_comp d [f, g] (by exact ⟨df, dg⟩)
theorem dioph_fn_comp2 {h : ℕ → ℕ → ℕ} (d : dioph_fn (λv:vector3 ℕ 2, h (v &0) (v &1))) :
dioph_fn (λv, h (f v) (g v)) :=
dioph_fn_comp d [f, g] (by exact ⟨df, dg⟩)
theorem eq_dioph : dioph (λv, f v = g v) :=
dioph_comp2 df dg $ of_no_dummies _ (poly.proj &0 - poly.proj &1)
(λv, (int.coe_nat_eq_coe_nat_iff _ _).symm.trans
⟨@sub_eq_zero_of_eq ℤ _ (v &0) (v &1), eq_of_sub_eq_zero⟩)
localized "infix ` D= `:50 := dioph.eq_dioph" in dioph
theorem add_dioph : dioph_fn (λv, f v + g v) :=
dioph_fn_comp2 df dg $ abs_poly_dioph (poly.proj &0 + poly.proj &1)
localized "infix ` D+ `:80 := dioph.add_dioph" in dioph
theorem mul_dioph : dioph_fn (λv, f v * g v) :=
dioph_fn_comp2 df dg $ abs_poly_dioph (poly.proj &0 * poly.proj &1)
localized "infix ` D* `:90 := dioph.mul_dioph" in dioph
theorem le_dioph : dioph (λv, f v ≤ g v) :=
dioph_comp2 df dg $ ext (D∃2 $ D&1 D+ D&0 D= D&2) (λv, ⟨λ⟨x, hx⟩, le.intro hx, le.dest⟩)
localized "infix ` D≤ `:50 := dioph.le_dioph" in dioph
theorem lt_dioph : dioph (λv, f v < g v) := df D+ (D. 1) D≤ dg
localized "infix ` D< `:50 := dioph.lt_dioph" in dioph
theorem ne_dioph : dioph (λv, f v ≠ g v) :=
ext (df D< dg D∨ dg D< df) $ λv, ne_iff_lt_or_gt.symm
localized "infix ` D≠ `:50 := dioph.ne_dioph" in dioph
theorem sub_dioph : dioph_fn (λv, f v - g v) :=
dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $
ext (D&1 D= D&0 D+ D&2 D∨ D&1 D≤ D&2 D∧ D&0 D= D.0) $ (vector_all_iff_forall _).1 $ λx y z,
show (y = x + z ∨ y ≤ z ∧ x = 0) ↔ y - z = x, from
⟨λo, begin
rcases o with ae | ⟨yz, x0⟩,
{ rw [ae, nat.add_sub_cancel] },
{ rw [x0, nat.sub_eq_zero_of_le yz] }
end, λh, begin
subst x,
cases le_total y z with yz zy,
{ exact or.inr ⟨yz, nat.sub_eq_zero_of_le yz⟩ },
{ exact or.inl (nat.sub_add_cancel zy).symm },
end⟩
localized "infix ` D- `:80 := dioph.sub_dioph" in dioph
theorem dvd_dioph : dioph (λv, f v ∣ g v) :=
dioph_comp (D∃2 $ D&2 D= D&1 D* D&0) [f, g] (by exact ⟨df, dg⟩)
localized "infix ` D∣ `:50 := dioph.dvd_dioph" in dioph
theorem mod_dioph : dioph_fn (λv, f v % g v) :=
have dioph (λv : vector3 ℕ 3, (v &2 = 0 ∨ v &0 < v &2) ∧ ∃ (x : ℕ), v &0 + v &2 * x = v &1),
from (D&2 D= D.0 D∨ D&0 D< D&2) D∧ (D∃3 $ D&1 D+ D&3 D* D&0 D= D&2),
dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ ext this $ (vector_all_iff_forall _).1 $ λz x y,
show ((y = 0 ∨ z < y) ∧ ∃ c, z + y * c = x) ↔ x % y = z, from
⟨λ⟨h, c, hc⟩, begin rw ← hc; simp; cases h with x0 hl, rw [x0, mod_zero], exact mod_eq_of_lt hl end,
λe, by rw ← e; exact ⟨or_iff_not_imp_left.2 $ λh, mod_lt _ (nat.pos_of_ne_zero h), x / y, mod_add_div _ _⟩⟩
localized "infix ` D% `:80 := dioph.mod_dioph" in dioph
theorem modeq_dioph {h : (α → ℕ) → ℕ} (dh : dioph_fn h) : dioph (λv, f v ≡ g v [MOD h v]) :=
df D% dh D= dg D% dh
localized "notation `D≡` := dioph.modeq_dioph" in dioph
theorem div_dioph : dioph_fn (λv, f v / g v) :=
have dioph (λv : vector3 ℕ 3, v &2 = 0 ∧ v &0 = 0 ∨ v &0 * v &2 ≤ v &1 ∧ v &1 < (v &0 + 1) * v &2),
from (D&2 D= D.0 D∧ D&0 D= D.0) D∨ D&0 D* D&2 D≤ D&1 D∧ D&1 D< (D&0 D+ D.1) D* D&2,
dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ ext this $ (vector_all_iff_forall _).1 $ λz x y,
show y = 0 ∧ z = 0 ∨ z * y ≤ x ∧ x < (z + 1) * y ↔ x / y = z,
by refine iff.trans _ eq_comm; exact y.eq_zero_or_pos.elim
(λy0, by rw [y0, nat.div_zero]; exact
⟨λo, (o.resolve_right $ λ⟨_, h2⟩, not_lt_zero _ h2).right, λz0, or.inl ⟨rfl, z0⟩⟩)
(λypos, iff.trans ⟨λo, o.resolve_left $ λ⟨h1, _⟩, ne_of_gt ypos h1, or.inr⟩
(le_antisymm_iff.trans $ and_congr (nat.le_div_iff_mul_le _ _ ypos) $
iff.trans ⟨lt_succ_of_le, le_of_lt_succ⟩ (div_lt_iff_lt_mul _ _ ypos)).symm)
localized "infix ` D/ `:80 := dioph.div_dioph" in dioph
omit df dg
open pell
theorem pell_dioph : dioph (λv:vector3 ℕ 4, ∃ h : v &0 > 1,
xn h (v &1) = v &2 ∧ yn h (v &1) = v &3) :=
have dioph {v : vector3 ℕ 4 |
v &0 > 1 ∧ v &1 ≤ v &3 ∧
(v &2 = 1 ∧ v &3 = 0 ∨
∃ (u w s t b : ℕ),
v &2 * v &2 - (v &0 * v &0 - 1) * v &3 * v &3 = 1 ∧
u * u - (v &0 * v &0 - 1) * w * w = 1 ∧
s * s - (b * b - 1) * t * t = 1 ∧
b > 1 ∧ (b ≡ 1 [MOD 4 * v &3]) ∧ (b ≡ v &0 [MOD u]) ∧
w > 0 ∧ v &3 * v &3 ∣ w ∧
(s ≡ v &2 [MOD u]) ∧
(t ≡ v &1 [MOD 4 * v &3]))}, from
D.1 D< D&0 D∧ D&1 D≤ D&3 D∧
((D&2 D= D.1 D∧ D&3 D= D.0) D∨
(D∃4 $ D∃5 $ D∃6 $ D∃7 $ D∃8 $
D&7 D* D&7 D- (D&5 D* D&5 D- D.1) D* D&8 D* D&8 D= D.1 D∧
D&4 D* D&4 D- (D&5 D* D&5 D- D.1) D* D&3 D* D&3 D= D.1 D∧
D&2 D* D&2 D- (D&0 D* D&0 D- D.1) D* D&1 D* D&1 D= D.1 D∧
D.1 D< D&0 D∧ (D≡ (D&0) (D.1) (D.4 D* D&8)) D∧ (D≡ (D&0) (D&5) D&4) D∧
D.0 D< D&3 D∧ D&8 D* D&8 D∣ D&3 D∧
(D≡ (D&2) (D&7) D&4) D∧
(D≡ (D&1) (D&6) (D.4 D* D&8)))),
dioph.ext this $ λv, matiyasevic.symm
theorem xn_dioph : dioph_pfun (λv:vector3 ℕ 2, ⟨v &0 > 1, λh, xn h (v &1)⟩) :=
have dioph (λv:vector3 ℕ 3, ∃ y, ∃ h : v &1 > 1, xn h (v &2) = v &0 ∧ yn h (v &2) = y), from
let D_pell := @reindex_dioph _ (fin2 4) _ pell_dioph [&2, &3, &1, &0] in D∃3 D_pell,
(dioph_pfun_vec _).2 $ dioph.ext this $ λv, ⟨λ⟨y, h, xe, ye⟩, ⟨h, xe⟩, λ⟨h, xe⟩, ⟨_, h, xe, rfl⟩⟩
include df dg
theorem pow_dioph : dioph_fn (λv, f v ^ g v) :=
have dioph {v : vector3 ℕ 3 |
v &2 = 0 ∧ v &0 = 1 ∨ v &2 > 0 ∧
(v &1 = 0 ∧ v &0 = 0 ∨ v &1 > 0 ∧
∃ (w a t z x y : ℕ),
(∃ (a1 : a > 1), xn a1 (v &2) = x ∧ yn a1 (v &2) = y) ∧
(x ≡ y * (a - v &1) + v &0 [MOD t]) ∧
2 * a * v &1 = t + (v &1 * v &1 + 1) ∧
v &0 < t ∧ v &1 ≤ w ∧ v &2 ≤ w ∧
a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)}, from
let D_pell := @reindex_dioph _ (fin2 9) _ pell_dioph [&4, &8, &1, &0] in
(D&2 D= D.0 D∧ D&0 D= D.1) D∨ (D.0 D< D&2 D∧
((D&1 D= D.0 D∧ D&0 D= D.0) D∨ (D.0 D< D&1 D∧
(D∃3 $ D∃4 $ D∃5 $ D∃6 $ D∃7 $ D∃8 $ D_pell D∧
(D≡ (D&1) (D&0 D* (D&4 D- D&7) D+ D&6) (D&3)) D∧
D.2 D* D&4 D* D&7 D= D&3 D+ (D&7 D* D&7 D+ D.1) D∧
D&6 D< D&3 D∧ D&7 D≤ D&5 D∧ D&8 D≤ D&5 D∧
D&4 D* D&4 D- ((D&5 D+ D.1) D* (D&5 D+ D.1) D- D.1) D* (D&5 D* D&2) D* (D&5 D* D&2) D= D.1)))),
dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ dioph.ext this $ λv, iff.symm $
eq_pow_of_pell.trans $ or_congr iff.rfl $ and_congr iff.rfl $ or_congr iff.rfl $ and_congr iff.rfl $
⟨λ⟨w, a, t, z, a1, h⟩, ⟨w, a, t, z, _, _, ⟨a1, rfl, rfl⟩, h⟩,
λ⟨w, a, t, z, ._, ._, ⟨a1, rfl, rfl⟩, h⟩, ⟨w, a, t, z, a1, h⟩⟩
end
end dioph
|
b59fc8c5e1406156b951168739dfc7a6905af797 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/category_theory/limits/shapes/products.lean | f39b4dc560a8655f1fa3df31c93a74068726115b | [
"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,949 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.limits.limits
import category_theory.discrete_category
noncomputable theory
universes v u
open category_theory
namespace category_theory.limits
variables {β : Type v}
variables {C : Type u} [category.{v} C]
-- We don't need an analogue of `pair` (for binary products), `parallel_pair` (for equalizers),
-- or `(co)span`, since we already have `discrete.functor`.
/-- A fan over `f : β → C` consists of a collection of maps from an object `P` to every `f b`. -/
abbreviation fan (f : β → C) := cone (discrete.functor f)
/-- A cofan over `f : β → C` consists of a collection of maps from every `f b` to an object `P`. -/
abbreviation cofan (f : β → C) := cocone (discrete.functor f)
/-- A fan over `f : β → C` consists of a collection of maps from an object `P` to every `f b`. -/
@[simps]
def fan.mk {f : β → C} (P : C) (p : Π b, P ⟶ f b) : fan f :=
{ X := P,
π := { app := p } }
/-- A cofan over `f : β → C` consists of a collection of maps from every `f b` to an object `P`. -/
@[simps]
def cofan.mk {f : β → C} (P : C) (p : Π b, f b ⟶ P) : cofan f :=
{ X := P,
ι := { app := p } }
/-- An abbreviation for `has_limit (discrete.functor f)`. -/
abbreviation has_product (f : β → C) := has_limit (discrete.functor f)
/-- An abbreviation for `has_colimit (discrete.functor f)`. -/
abbreviation has_coproduct (f : β → C) := has_colimit (discrete.functor f)
section
variables (C)
/-- An abbreviation for `has_limits_of_shape (discrete f)`. -/
abbreviation has_products_of_shape (β : Type v) := has_limits_of_shape.{v} (discrete β)
/-- An abbreviation for `has_colimits_of_shape (discrete f)`. -/
abbreviation has_coproducts_of_shape (β : Type v) := has_colimits_of_shape.{v} (discrete β)
end
/-- `pi_obj f` computes the product of a family of elements `f`. (It is defined as an abbreviation
for `limit (discrete.functor f)`, so for most facts about `pi_obj f`, you will just use general facts
about limits.) -/
abbreviation pi_obj (f : β → C) [has_product f] := limit (discrete.functor f)
/-- `sigma_obj f` computes the coproduct of a family of elements `f`. (It is defined as an abbreviation
for `colimit (discrete.functor f)`, so for most facts about `sigma_obj f`, you will just use general facts
about colimits.) -/
abbreviation sigma_obj (f : β → C) [has_coproduct f] := colimit (discrete.functor f)
notation `∏ ` f:20 := pi_obj f
notation `∐ ` f:20 := sigma_obj f
/-- The `b`-th projection from the pi object over `f` has the form `∏ f ⟶ f b`. -/
abbreviation pi.π (f : β → C) [has_product f] (b : β) : ∏ f ⟶ f b :=
limit.π (discrete.functor f) b
/-- The `b`-th inclusion into the sigma object over `f` has the form `f b ⟶ ∐ f`. -/
abbreviation sigma.ι (f : β → C) [has_coproduct f] (b : β) : f b ⟶ ∐ f :=
colimit.ι (discrete.functor f) b
/-- A collection of morphisms `P ⟶ f b` induces a morphism `P ⟶ ∏ f`. -/
abbreviation pi.lift {f : β → C} [has_product f] {P : C} (p : Π b, P ⟶ f b) : P ⟶ ∏ f :=
limit.lift _ (fan.mk P p)
/-- A collection of morphisms `f b ⟶ P` induces a morphism `∐ f ⟶ P`. -/
abbreviation sigma.desc {f : β → C} [has_coproduct f] {P : C} (p : Π b, f b ⟶ P) : ∐ f ⟶ P :=
colimit.desc _ (cofan.mk P p)
/--
Construct a morphism between categorical products (indexed by the same type)
from a family of morphisms between the factors.
-/
abbreviation pi.map {f g : β → C} [has_products_of_shape β C]
(p : Π b, f b ⟶ g b) : ∏ f ⟶ ∏ g :=
lim.map (discrete.nat_trans p)
/--
Construct an isomorphism between categorical products (indexed by the same type)
from a family of isomorphisms between the factors.
-/
abbreviation pi.map_iso {f g : β → C} [has_products_of_shape β C]
(p : Π b, f b ≅ g b) : ∏ f ≅ ∏ g :=
lim.map_iso (discrete.nat_iso p)
/--
Construct a morphism between categorical coproducts (indexed by the same type)
from a family of morphisms between the factors.
-/
abbreviation sigma.map {f g : β → C} [has_coproducts_of_shape β C]
(p : Π b, f b ⟶ g b) : ∐ f ⟶ ∐ g :=
colim.map (discrete.nat_trans p)
/--
Construct an isomorphism between categorical coproducts (indexed by the same type)
from a family of isomorphisms between the factors.
-/
abbreviation sigma.map_iso {f g : β → C} [has_coproducts_of_shape β C]
(p : Π b, f b ≅ g b) : ∐ f ≅ ∐ g :=
colim.map_iso (discrete.nat_iso p)
variables (C)
/-- An abbreviation for `Π J, has_limits_of_shape (discrete J) C` -/
abbreviation has_products := Π (J : Type v), has_limits_of_shape (discrete J) C
/-- An abbreviation for `Π J, has_colimits_of_shape (discrete J) C` -/
abbreviation has_coproducts := Π (J : Type v), has_colimits_of_shape (discrete J) C
end category_theory.limits
|
0b954092806fdeb476765040d5659dc89fb36bc7 | 968e2f50b755d3048175f176376eff7139e9df70 | /examples/prop_logic_theory/unnamed_786.lean | fb8dfe44361d81bbdf3b3e623cf2c63ebe5b167c | [] | no_license | gihanmarasingha/mth1001_sphinx | 190a003269ba5e54717b448302a27ca26e31d491 | 05126586cbf5786e521be1ea2ef5b4ba3c44e74a | refs/heads/master | 1,672,913,933,677 | 1,604,516,583,000 | 1,604,516,583,000 | 309,245,750 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 425 | lean | variables p q : Prop
-- BEGIN
theorem and_of_and : p ∧ q → q ∧ p :=
begin
intro h, -- Assume `h : p ∧ q`. It suffices to prove `q ∧ p`.
split, -- By `∧` intro., it suffices to prove both `q` and `p`.
{ show q, from h.right, }, -- We show `q` from right `∧` elimination on `h`.
{ show p, from h.left, }, -- We show `p` from left `∧` elimination on `h`.
end
-- END |
34d3e9d08eb6f1d61505453998c78040d43e582c | f02b05b665ad2d2cd970e93758a98d57f310a822 | /src/tactic/tcache/io.lean | 62bb91231e2ddb926235c77ebc65b40b1ccd92c9 | [] | no_license | khoek/leancache | b0ed5d2a9a2e87f58d4ad27343d957a9cd49ce5f | 5c8329f7b647b8d82966ab180c4473b20d1f249c | refs/heads/master | 1,585,344,140,717 | 1,549,094,327,000 | 1,549,094,327,000 | 147,138,187 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,138 | lean | import system.io
open io io.proc
namespace tcache
def CACHE_DIR := ".cache"
def exec_cmd (args : io.process.spawn_args) : io unit :=
spawn args >> return ()
def mk_cache_dir : io unit :=
exec_cmd {cmd := "mkdir", args := ["-p", CACHE_DIR], stdout := io.process.stdio.null, stderr := io.process.stdio.null}
def rm_cache_dir : io unit :=
exec_cmd {cmd := "rm", args := ["-rf", CACHE_DIR], stdout := io.process.stdio.null, stderr := io.process.stdio.null}
meta def read_to_end' (h : handle) : io string := do
cb ← (iterate none $ λ r,
do done ← io.fs.is_eof h,
if done
then return none
else do
c ← io.fs.read h $ 20000 * 1024,
return $ some $ match r with
| none := some c
| some r := some (r ++ c)
end
),
return $ match cb with
| none := ""
| some cb := cb.to_string
end
meta def file_read (fn : string) : io string := do
h ← io.mk_file_handle fn io.mode.read ff,
s ← read_to_end' h,
return s
def file_write (fn : string) (data : string) : io unit := do
h ← io.mk_file_handle fn io.mode.write ff,
io.fs.write h data.to_char_buffer,
io.fs.close h
end tcache
|
2468a2da474a98c41f8c0d90d9139d50f5becae6 | ebbdcbd7ddc89a9ef7c3b397b301d5f5272a918f | /qp/p1_categories/c1_basic/s3_nattrans.lean | 327b0d3d70c2dc84dedfceb63929353cd17bd76a | [] | no_license | intoverflow/qvr | 34b9ef23604738381ca20b7d622fd0399d88f2dd | 0cfcd33fe4bf8d93851a00cec5bfd21e77105d74 | refs/heads/master | 1,616,591,570,371 | 1,492,575,772,000 | 1,492,575,772,000 | 80,061,627 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 28,042 | lean | /- -----------------------------------------------------------------------
Natural transformations.
----------------------------------------------------------------------- -/
import .s2_functors
namespace qp
open stdaux
universe variables ℓobj₁ ℓhom₁ ℓobj₂ ℓhom₂ ℓobj₃ ℓhom₃ ℓobj₄ ℓhom₄
/-! #brief A natural transformation between functors.
-/
structure NatTrans
{C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
(F G : Fun C D)
: Type (max ℓobj₁ ℓhom₁ ℓobj₂ ℓhom₂)
:= (com : ∀ (x : C^.obj), D^.hom (F^.obj x) (G^.obj x))
(natural : ∀ {x y : C^.obj} (f : C^.hom x y)
, D^.circ (com y) (F^.hom f)
= D^.circ (G^.hom f) (com x))
-- A natural transformation.
-- \rightarrowtail
notation F `↣` G := NatTrans F G
/-! #brief Equality of natural transformations.
-/
theorem NatTrans.eq
{C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ : Fun C D}
: ∀ {η₁ η₂ : NatTrans F₁ F₂}
(ω : η₁^.com = η₂^.com)
, η₁ = η₂
| (NatTrans.mk com ω₁) (NatTrans.mk .(com) ω₂) (eq.refl .(com)) := rfl
/-! #brief Heterogeneous equality of natural transformations.
-/
theorem NatTrans.heq
{C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
: ∀ {F₁₁ F₁₂ F₂₁ F₂₂ : Fun C D}
{η₁ : NatTrans F₁₁ F₁₂}
{η₂ : NatTrans F₂₁ F₂₂}
(ω₁ : F₁₁ = F₂₁)
(ω₂ : F₁₂ = F₂₂)
(ωcom : F₁₁ = F₂₁ → F₁₂ = F₂₂ → η₁^.com == η₂^.com)
, η₁ == η₂
| F₁ F₂ .(F₁) .(F₂) η₁ η₂ (eq.refl .(F₁)) (eq.refl .(F₂)) ωcom
:= heq_of_eq (NatTrans.eq (eq_of_heq (ωcom rfl rfl)))
/-! #brief Congruence for NatTrans.com
-/
theorem NatTrans.congr_com
{C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ : Fun C D}
: ∀ {η₁ η₂ : NatTrans F₁ F₂}
(ωη : η₁ = η₂)
{c : C^.obj}
, η₁^.com c = η₂^.com c
| η .(η) (eq.refl .(η)) c := rfl
/- -----------------------------------------------------------------------
Natural transformations are morphisms of functors.
----------------------------------------------------------------------- -/
/-! #brief The identity natural transformation.
-/
definition NatTrans.id {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
(F : Fun C D)
: NatTrans F F
:= { com := λ x, D^.id (F^.obj x)
, natural := λ x y f, by rw [D^.circ_id_left, D^.circ_id_right]
}
/-! #brief Composition of natural transformations.
-/
definition NatTrans.comp {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ F₃ : Fun C D}
(η₂₃ : NatTrans F₂ F₃)
(η₁₂ : NatTrans F₁ F₂)
: NatTrans F₁ F₃
:= { com := λ x, D^.circ (η₂₃^.com x) (η₁₂^.com x)
, natural := λ x y f
, by calc η₂₃^.com y ∘∘ η₁₂^.com y ∘∘ F₁^.hom f
= η₂₃^.com y ∘∘ (η₁₂^.com y ∘∘ F₁^.hom f) : by rw D^.circ_assoc
... = η₂₃^.com y ∘∘ (F₂^.hom f ∘∘ η₁₂^.com x) : by rw η₁₂^.natural
... = (η₂₃^.com y ∘∘ F₂^.hom f) ∘∘ η₁₂^.com x : by rw D^.circ_assoc
... = (F₃^.hom f ∘∘ η₂₃^.com x) ∘∘ η₁₂^.com x : by rw η₂₃^.natural
... = F₃^.hom f ∘∘ (η₂₃^.com x ∘∘ η₁₂^.com x) : by rw D^.circ_assoc
}
-- Composition of natural transformations.
-- \Diamond\Diamond
infixl ` ◇◇ ` : 150 := NatTrans.comp
/-! #brief Composition of natural transformations is associative.
-/
theorem NatTrans.comp_assoc {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
(F₁ F₂ F₃ F₄ : Fun C D)
(η₃₄ : NatTrans F₃ F₄)
(η₂₃ : NatTrans F₂ F₃)
(η₁₂ : NatTrans F₁ F₂)
: NatTrans.comp η₃₄ (NatTrans.comp η₂₃ η₁₂)
= NatTrans.comp (NatTrans.comp η₃₄ η₂₃) η₁₂
:= NatTrans.eq (funext (λ c, D^.circ_assoc))
/-! #brief Left-identity for composition of natural transformation.
-/
theorem NatTrans.comp_id_left {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ : Fun C D}
(η : NatTrans F₁ F₂)
: NatTrans.comp (NatTrans.id F₂) η = η
:= NatTrans.eq (funext (λ c, D^.circ_id_left))
/-! #brief Right-identity for composition of natural transformation.
-/
theorem NatTrans.comp_id_right {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ : Fun C D}
(η : NatTrans F₁ F₂)
: NatTrans.comp η (NatTrans.id F₁) = η
:= NatTrans.eq (funext (λ c, D^.circ_id_right))
/-! #brief The casting natural transformation.
-/
definition NatTrans.cast {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
: ∀ {F₁ F₂ : Fun C D}
(ωF : F₁ = F₂)
, NatTrans F₁ F₂
| F .(F) (eq.refl .(F)) := NatTrans.id F
/-! #brief The casting transformation is the identity.
-/
theorem NatTrans.simp_cast {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
: ∀ {F₁ F₂ : Fun C D}
{ωF : F₁ = F₂}
, NatTrans.cast ωF == NatTrans.id F₁
| F .(F) (eq.refl .(F)) := heq.refl _
/-! #brief The casting transformation has identity components.
-/
theorem NatTrans.cast.com {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
: ∀ {F₁ F₂ : Fun C D}
{ωF : F₁ = F₂}
{c : C^.obj}
, (NatTrans.cast ωF)^.com c = cast_hom (by rw ωF)
| F .(F) (eq.refl .(F)) c := rfl
/-! #brief Composition of casting natural transformations.
-/
theorem NatTrans.comp_cast {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
: ∀ {F₁ F₂ F₃ : Fun C D}
{ωF₁₂ : F₁ = F₂}
{ωF₂₃ : F₂ = F₃}
, NatTrans.comp (NatTrans.cast ωF₂₃) (NatTrans.cast ωF₁₂)
= NatTrans.cast (eq.trans ωF₁₂ ωF₂₃)
| F .(F) .(F) (eq.refl .(F)) (eq.refl .(F))
:= NatTrans.eq (funext (λ c, D^.circ_id_left))
/-! #brief Casting natural transformations are just identities.
-/
theorem NatTrans.cast_rfl {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F : Fun C D}
: NatTrans.cast (eq.refl F) = NatTrans.id F
:= rfl
/- -----------------------------------------------------------------------
Constant transformations.
----------------------------------------------------------------------- -/
/-! #brief A constant transformation.
-/
definition ConstTrans (C : Cat.{ℓobj₁ ℓhom₁}) {D : Cat.{ℓobj₂ ℓhom₂}}
{d₁ d₂ : D^.obj} (h : D^.hom d₁ d₂)
: NatTrans (ConstFun C d₁) (ConstFun C d₂)
:= { com := λ c, h
, natural := λ c₁ c₂ f, eq.trans D^.circ_id_right (eq.symm D^.circ_id_left)
}
/- -----------------------------------------------------------------------
Functor categories.
----------------------------------------------------------------------- -/
/-! #brief A functor category.
-/
definition FunCat (C : Cat.{ℓobj₁ ℓhom₁}) (D : Cat.{ℓobj₂ ℓhom₂})
: Cat
:= { obj := Fun C D
, hom := NatTrans
, id := NatTrans.id
, circ := @NatTrans.comp C D
, circ_assoc := @NatTrans.comp_assoc C D
, circ_id_left := @NatTrans.comp_id_left C D
, circ_id_right := @NatTrans.comp_id_right C D
}
/-! #brief A presheaf category.
-/
definition PreShCat (C : Cat.{ℓobj₁ ℓhom₁})
: Cat.{(max ℓobj₁ (ℓhom₁ + 1)) ((max ℓobj₁ (ℓhom₁ + 1)) + 1)}
:= FunCat (OpCat C) LeanCat.{ℓhom₁}
/-! #brief Functor categories have pointwise final objects.
-/
definition FunCat.HasFinal {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
[D_HasFinal : HasFinal D]
: HasFinal (FunCat C D)
:= HasFinal.show (ConstFun C (final D))
(λ F, { com := λ c, final_hom (F^.obj c)
, natural
:= λ c₁ c₂ f
, eq.trans (final_hom.uniq D) (eq.symm D^.circ_id_left)
})
(λ F η, NatTrans.eq (funext (λ c, final_hom.uniq D)))
/-! #brief Functor categories have pointwise initial objects.
-/
definition FunCat.HasInit {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
[D_HasInit : HasInit D]
: HasInit (FunCat C D)
:= HasFinal.show (ConstFun C (init D))
(λ F, { com := λ c, init_hom (F^.obj c)
, natural
:= λ c₁ c₂ f
, eq.trans D^.circ_id_right (eq.symm (init_hom.uniq D))
})
(λ F η, NatTrans.eq (funext (λ c, init_hom.uniq D)))
/- -----------------------------------------------------------------------
Swapping the arguments of functors into functor categories.
----------------------------------------------------------------------- -/
/-! #brief Swap the arguments of a functor.
-/
definition Fun.swap.obj {A : Cat.{ℓobj₁ ℓhom₁}} {B : Cat.{ℓobj₂ ℓhom₂}} {C : Cat.{ℓobj₃ ℓhom₃}}
(F : Fun A (FunCat B C))
(b : B^.obj)
: Fun A C
:= { obj := λ a, (F^.obj a)^.obj b
, hom := λ a₁ a₂ f, (F^.hom f)^.com b
, hom_id := λ x, begin
rw F^.hom_id,
trivial
end
, hom_circ := λ x y z g f, begin rw F^.hom_circ, trivial end
}
/-! #brief Swap the arguments of a natural transformation.
-/
definition Fun.swap.hom {A : Cat.{ℓobj₁ ℓhom₁}} {B : Cat.{ℓobj₂ ℓhom₂}} {C : Cat.{ℓobj₃ ℓhom₃}}
(F : Fun A (FunCat B C))
(b₁ b₂ : B^.obj) (f : B^.hom b₁ b₂)
: NatTrans (Fun.swap.obj F b₁) (Fun.swap.obj F b₂)
:= { com := λ a, (F^.obj a)^.hom f
, natural := λ x y g, eq.symm ((F^.hom g)^.natural f)
}
/-! #brief Swap the arguments.
-/
definition Fun.swap {A : Cat.{ℓobj₁ ℓhom₁}} {B : Cat.{ℓobj₂ ℓhom₂}} {C : Cat.{ℓobj₃ ℓhom₃}}
(F : Fun A (FunCat B C))
: Fun B (FunCat A C)
:= { obj := Fun.swap.obj F
, hom := Fun.swap.hom F
, hom_id := λ b, NatTrans.eq (funext (λ a, (F^.obj a)^.hom_id))
, hom_circ := λ b₁ b₂ b₃ g f, NatTrans.eq (funext (λ a, (F^.obj a)^.hom_circ))
}
/- -----------------------------------------------------------------------
Natural isomorphisms.
----------------------------------------------------------------------- -/
/-! #brief A natural isomorphism between two functors.
-/
definition NatIso {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ : Fun C D}
(η₁₂ : NatTrans F₁ F₂)
(η₂₁ : NatTrans F₂ F₁)
: Prop
:= @Iso (FunCat C D) F₁ F₂ η₁₂ η₂₁
/-! #brief The identity natural transformation is a natural isomorphism.
-/
theorem NatIso.id {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F : Fun C D}
: NatIso (NatTrans.id F) (NatTrans.id F)
:= Cat.id.Iso F
/-! #brief The composition of two natural isomorphisms is a natural iso.
-/
theorem NatIso.comp {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ F₃ : Fun C D}
{η₂₃ : NatTrans F₂ F₃} {η₂₃' : NatTrans F₃ F₂} (iso₂₃ : NatIso η₂₃ η₂₃')
{η₁₂ : NatTrans F₁ F₂} {η₁₂' : NatTrans F₂ F₁} (iso₁₂ : NatIso η₁₂ η₁₂')
: NatIso (NatTrans.comp η₂₃ η₁₂) (NatTrans.comp η₁₂' η₂₃')
:= Cat.circ.Iso iso₂₃ iso₁₂
/-! #brief The components of a natural isomorphism are isomorphisms.
-/
theorem NatIso.com {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ : Fun C D}
{η₁₂ : NatTrans F₁ F₂}
{η₂₁ : NatTrans F₂ F₁}
(iso : NatIso η₁₂ η₂₁)
(c : C^.obj)
: Iso (η₁₂^.com c) (η₂₁^.com c)
:= { id₁ := congr_arg (λ η, NatTrans.com η c) iso^.id₁
, id₂ := congr_arg (λ η, NatTrans.com η c) iso^.id₂
}
/-! #brief A natural transformation whose components are isos is a natural iso.
-/
theorem NatTrans.Iso_on_com {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ : Fun C D}
{η₁₂ : NatTrans F₁ F₂}
{η₂₁ : NatTrans F₂ F₁}
(ω : ∀ (c : C^.obj), Iso (η₁₂^.com c) (η₂₁^.com c))
: NatIso η₁₂ η₂₁
:= { id₁ := NatTrans.eq (funext (λ c, (ω c)^.id₁))
, id₂ := NatTrans.eq (funext (λ c, (ω c)^.id₂))
}
/- -----------------------------------------------------------------------
Opposite transformations.
----------------------------------------------------------------------- -/
/-! #brief An opposite transformation.
-/
definition OpTrans {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ : Fun C D}
(η : NatTrans F₁ F₂)
: NatTrans (OpFun F₂) (OpFun F₁)
:= { com := λ x, η^.com x
, natural := λ x y f, eq.symm (η^.natural f)
}
/-! #brief Opposites of natural isos are again natural isos.
-/
definition OpNatIso {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ : Fun C D}
{η₁₂ : NatTrans F₁ F₂}
{η₂₁ : NatTrans F₂ F₁}
(η_iso : NatIso η₁₂ η₂₁)
: NatIso (OpTrans η₁₂) (OpTrans η₂₁)
:= { id₁ := begin
apply NatTrans.eq, apply funext, intro c,
apply NatTrans.congr_com η_iso^.id₂
end
, id₂ := begin
apply NatTrans.eq, apply funext, intro c,
apply NatTrans.congr_com η_iso^.id₁
end
}
/- -----------------------------------------------------------------------
Monics.
----------------------------------------------------------------------- -/
/-! #brief Monic natural transformations have monic components.
-/
theorem NatTrans.com.Monic {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ : Fun C D}
{η : NatTrans F₁ F₂}
(η_Monic : @Monic (FunCat C D) F₁ F₂ η)
(c : C^.obj)
: Monic (η^.com c)
:= Monic.show (λ d f₁ f₂ ω
, begin exact sorry end)
/- -----------------------------------------------------------------------
Whisker composition of natural transformations.
----------------------------------------------------------------------- -/
/-! #brief Left whisker composition.
-/
definition NatTrans.whisk_left {B : Cat.{ℓobj₁ ℓhom₁}} {C : Cat.{ℓobj₂ ℓhom₂}} {D : Cat.{ℓobj₃ ℓhom₃}}
(G : Fun C D)
{F₁ F₂ : Fun B C}
(η : NatTrans F₁ F₂)
: NatTrans (Fun.comp G F₁) (Fun.comp G F₂)
:= { com := λ b, G^.hom (η^.com b)
, natural := λ b₁ b₂ f
, by calc G^.hom (η^.com b₂) ∘∘ (G □□ F₁)^.hom f
= G^.hom (η^.com b₂ ∘∘ F₁^.hom f) : eq.symm G^.hom_circ
... = G^.hom (F₂^.hom f ∘∘ η^.com b₁) : by rw η^.natural
... = (G □□ F₂)^.hom f ∘∘ G^.hom (η^.com b₁) : G^.hom_circ
}
-- Left whisker composition.
-- \Box\Diamond
infixr ` □◇ ` : 150 := NatTrans.whisk_left
/-! #brief Left whisker composition and identity functors.
-/
theorem NatTrans.whisk_left.id_left {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ : Fun C D}
{η : NatTrans F₁ F₂}
: NatTrans.whisk_left (Fun.id D) η == η
:= NatTrans.heq Fun.comp_id_left Fun.comp_id_left
(λ ω₁ ω₂, heq.refl _)
/-! #brief Left whisker composition and composition of functors.
-/
definition NatTrans.whisk_left.comp_left
{A : Cat.{ℓobj₁ ℓhom₁}} {B : Cat.{ℓobj₂ ℓhom₂}} {C : Cat.{ℓobj₃ ℓhom₃}} {D : Cat.{ℓobj₄ ℓhom₄}}
{H : Fun C D}
{G : Fun B C}
{F₁ F₂ : Fun A B}
{η : NatTrans F₁ F₂}
: NatTrans.whisk_left (Fun.comp H G) η == NatTrans.whisk_left H (NatTrans.whisk_left G η)
:= NatTrans.heq Fun.comp_assoc Fun.comp_assoc
(λ ω₁ ω₂, heq.refl _)
/-! #brief Left whisker composition and identity natural transformations.
-/
theorem NatTrans.whisk_left.id_right {B : Cat.{ℓobj₁ ℓhom₁}} {C : Cat.{ℓobj₂ ℓhom₂}} {D : Cat.{ℓobj₃ ℓhom₃}}
{G : Fun C D}
{F : Fun B C}
: NatTrans.whisk_left G (NatTrans.id F) = NatTrans.id (Fun.comp G F)
:= NatTrans.eq (funext (λ b, G^.hom_id))
/-! #brief Left whisker composition and composition of natural transformations.
-/
theorem NatTrans.whisk_left.comp_right {B : Cat.{ℓobj₁ ℓhom₁}} {C : Cat.{ℓobj₂ ℓhom₂}} {D : Cat.{ℓobj₃ ℓhom₃}}
{G : Fun C D}
{F₁ F₂ F₃ : Fun B C}
{η₂₃ : NatTrans F₂ F₃}
{η₁₂ : NatTrans F₁ F₂}
: NatTrans.whisk_left G (NatTrans.comp η₂₃ η₁₂)
= NatTrans.comp (NatTrans.whisk_left G η₂₃) (NatTrans.whisk_left G η₁₂)
:= NatTrans.eq (funext (λ b, G^.hom_circ))
/-! #brief Left whisker composition preserves natural isomorphisms.
-/
theorem NatTrans.whisk_left.NatIso {B : Cat.{ℓobj₁ ℓhom₁}} {C : Cat.{ℓobj₂ ℓhom₂}} {D : Cat.{ℓobj₃ ℓhom₃}}
(G : Fun C D)
{F₁ F₂ : Fun B C}
{η₁₂ : NatTrans F₁ F₂}
{η₂₁ : NatTrans F₂ F₁}
(iso : NatIso η₁₂ η₂₁)
: NatIso (NatTrans.whisk_left G η₁₂) (NatTrans.whisk_left G η₂₁)
:= NatTrans.Iso_on_com (λ b, Fun.preserves_Iso G (NatIso.com iso b))
/-! #brief Right whisker composition.
-/
definition NatTrans.whisk_right {B : Cat.{ℓobj₁ ℓhom₁}} {C : Cat.{ℓobj₂ ℓhom₂}} {D : Cat.{ℓobj₃ ℓhom₃}}
{G₁ G₂ : Fun C D}
(η : NatTrans G₁ G₂)
(F : Fun B C)
: NatTrans (Fun.comp G₁ F) (Fun.comp G₂ F)
:= { com := λ b, η^.com (F^.obj b)
, natural := λ b₁ b₂ f, η^.natural _
}
-- Right whisker composition.
-- \Diamond\Box
infixl ` ◇□ ` : 150 := NatTrans.whisk_right
/-! #brief Right whisker composition and identity functors.
-/
theorem NatTrans.whisk_right_id_right {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ F₂ : Fun C D}
{η : NatTrans F₁ F₂}
: NatTrans.whisk_right η (Fun.id C) == η
:= NatTrans.heq Fun.comp_id_right Fun.comp_id_right
(λ ω₁ ω₂, heq.refl _)
/-! #brief Right whisker composition and composition of functors.
-/
definition NatTrans.whisk_right.comp_right
{A : Cat.{ℓobj₁ ℓhom₁}} {B : Cat.{ℓobj₂ ℓhom₂}} {C : Cat.{ℓobj₃ ℓhom₃}} {D : Cat.{ℓobj₄ ℓhom₄}}
{F₁ F₂ : Fun C D}
{η : NatTrans F₁ F₂}
{H : Fun B C}
{G : Fun A B}
: NatTrans.whisk_right η (Fun.comp H G) == NatTrans.whisk_right (NatTrans.whisk_right η H) G
:= NatTrans.heq Fun.comp_assoc Fun.comp_assoc
(λ ω₁ ω₂, heq.refl _)
/-! #brief Right whisker composition and identity natural transformations.
-/
theorem NatTrans.whisk_right.id_left {B : Cat.{ℓobj₁ ℓhom₁}} {C : Cat.{ℓobj₂ ℓhom₂}} {D : Cat.{ℓobj₃ ℓhom₃}}
{G : Fun C D}
{F : Fun B C}
: NatTrans.whisk_right (NatTrans.id G) F = NatTrans.id (Fun.comp G F)
:= NatTrans.eq (funext (λ b, rfl))
/-! #brief Right whisker composition and composition of natural transformations.
-/
theorem NatTrans.whisk_right.comp_left {B : Cat.{ℓobj₁ ℓhom₁}} {C : Cat.{ℓobj₂ ℓhom₂}} {D : Cat.{ℓobj₃ ℓhom₃}}
{G : Fun B C}
{F₁ F₂ F₃ : Fun C D}
{η₂₃ : NatTrans F₂ F₃}
{η₁₂ : NatTrans F₁ F₂}
: NatTrans.whisk_right (NatTrans.comp η₂₃ η₁₂) G
= NatTrans.comp (NatTrans.whisk_right η₂₃ G) (NatTrans.whisk_right η₁₂ G)
:= NatTrans.eq (funext (λ b, rfl))
/-! #brief Right whisker composition preserves natural isomorphisms.
-/
theorem NatTrans.whisk_right.NatIso {B : Cat.{ℓobj₁ ℓhom₁}} {C : Cat.{ℓobj₂ ℓhom₂}} {D : Cat.{ℓobj₃ ℓhom₃}}
{G₁ G₂ : Fun C D}
{η₁₂ : NatTrans G₁ G₂}
{η₂₁ : NatTrans G₂ G₁}
(iso : NatIso η₁₂ η₂₁)
(F : Fun B C)
: NatIso (NatTrans.whisk_right η₁₂ F) (NatTrans.whisk_right η₂₁ F)
:= NatTrans.Iso_on_com (λ b, NatIso.com iso (F^.obj b))
/- -----------------------------------------------------------------------
Adjoint functors.
----------------------------------------------------------------------- -/
/-! #brief An adjunction between two functors.
-/
structure Adj
{C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
(L : Fun C D)
(R : Fun D C)
: Type (max ℓobj₁ ℓhom₁ ℓobj₂ ℓhom₂)
:= mk :: (counit : NatTrans (Fun.comp L R) (Fun.id D))
(unit : NatTrans (Fun.id C) (Fun.comp R L))
(id_left : ∀ {c : C^.obj}, counit^.com (L^.obj c) ∘∘ L^.hom (unit^.com c) = D^.id (L^.obj c))
(id_right : ∀ {d : D^.obj}, R^.hom (counit^.com d) ∘∘ unit^.com (R^.obj d) = C^.id (R^.obj d))
-- An adjunction of functors.
-- \dashv
notation L ` ⊣ ` R := Adj L R
/-! #brief The right adjoint of a hom.
-/
definition Adj.right_adj {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{L : Fun C D} {R : Fun D C} (adj : Adj L R)
{c : C^.obj} {d : D^.obj} (f : D^.hom (L^.obj c) d)
: C^.hom c (R^.obj d)
:= R^.hom f ∘∘ adj^.unit^.com c
/-! #brief The left adjoint of a hom.
-/
definition Adj.left_adj {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{L : Fun C D} {R : Fun D C} (adj : Adj L R)
{c : C^.obj} {d : D^.obj} (f : C^.hom c (R^.obj d))
: D^.hom (L^.obj c) d
:= adj^.counit^.com d ∘∘ L^.hom f
/-! #brief right_adj and left_adj are inverses of one another.
-/
theorem Adj.right_adj_left_adj {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{L : Fun C D} {R : Fun D C} (adj : Adj L R)
{c : C^.obj} {d : D^.obj} {f : C^.hom c (R^.obj d)}
: adj^.right_adj (adj^.left_adj f) = f
:= by calc adj^.right_adj (adj^.left_adj f)
= R^.hom (adj^.counit^.com d ∘∘ L^.hom f) ∘∘ adj^.unit^.com c : rfl
... = R^.hom (adj^.counit^.com d) ∘∘ R^.hom (L^.hom f) ∘∘ adj^.unit^.com c : by rw R^.hom_circ
... = R^.hom (adj^.counit^.com d) ∘∘ (R^.hom (L^.hom f) ∘∘ adj^.unit^.com c) : by rw C^.circ_assoc
... = R^.hom (adj^.counit^.com d) ∘∘ ((R □□ L)^.hom f ∘∘ adj^.unit^.com c) : rfl
... = R^.hom (adj^.counit^.com d) ∘∘ (adj^.unit^.com (R^.obj d) ∘∘ f) : congr_arg (λ q, R^.hom (adj^.counit^.com d) ∘∘ q) (eq.symm (adj^.unit^.natural _))
... = R^.hom (adj^.counit^.com d) ∘∘ adj^.unit^.com (R^.obj d) ∘∘ f : C^.circ_assoc
... = ⟨⟨R^.obj d⟩⟩ ∘∘ f : congr_arg (λ q, q ∘∘ f) adj^.id_right
... = f : C^.circ_id_left
/-! #brief left_adj and right_adj are inverses of one another.
-/
theorem Adj.left_adj_right_adj {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{L : Fun C D} {R : Fun D C} (adj : Adj L R)
{c : C^.obj} {d : D^.obj} {f : D^.hom (L^.obj c) d}
: adj^.left_adj (adj^.right_adj f) = f
:= by calc adj^.left_adj (adj^.right_adj f)
= adj^.counit^.com d ∘∘ L^.hom (R^.hom f ∘∘ adj^.unit^.com c) : rfl
... = adj^.counit^.com d ∘∘ (L^.hom (R^.hom f) ∘∘ L^.hom (adj^.unit^.com c)) : congr_arg (λ q, adj^.counit^.com d ∘∘ q) L^.hom_circ
... = adj^.counit^.com d ∘∘ ((L □□ R)^.hom f ∘∘ L^.hom (adj^.unit^.com c)) : rfl
... = adj^.counit^.com d ∘∘ (L □□ R)^.hom f ∘∘ L^.hom (adj^.unit^.com c) : by rw D^.circ_assoc
... = f ∘∘ adj^.counit^.com (L^.obj c) ∘∘ L^.hom (adj^.unit^.com c) : congr_arg (λ q, q ∘∘ L^.hom (adj^.unit^.com c)) (adj^.counit^.natural _)
... = f ∘∘ (adj^.counit^.com (L^.obj c) ∘∘ L^.hom (adj^.unit^.com c)) : by rw D^.circ_assoc
... = f ∘∘ ⟨⟨L^.obj c⟩⟩ : congr_arg (λ q, f ∘∘ q) adj^.id_left
... = f : D^.circ_id_right
/-! #brief left_adj is injective.
-/
theorem Adj.left_adj.inj {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{L : Fun C D} {R : Fun D C} (adj : Adj L R)
{c : C^.obj} {d : D^.obj} {f₁ f₂ : C^.hom c (R^.obj d)}
(ω : Adj.left_adj adj f₁ = Adj.left_adj adj f₂)
: f₁ = f₂
:= begin
apply eq.trans (eq.symm adj^.right_adj_left_adj),
rw ω,
apply adj^.right_adj_left_adj
end
/-! #brief right_adj is injective.
-/
theorem Adj.right_adj.inj {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{L : Fun C D} {R : Fun D C} (adj : Adj L R)
{c : C^.obj} {d : D^.obj} {f₁ f₂ : D^.hom (L^.obj c) d}
(ω : Adj.right_adj adj f₁ = Adj.right_adj adj f₂)
: f₁ = f₂
:= begin
apply eq.trans (eq.symm adj^.left_adj_right_adj),
rw ω,
apply adj^.left_adj_right_adj
end
/- -----------------------------------------------------------------------
Bijections of categories give adjunctions.
----------------------------------------------------------------------- -/
/-! #brief The adjunction induced by a bijection.
-/
definition Cat.Bij.Adj {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F : Fun C D} {G : Fun D C}
(FG_Bij : Cat.Bij F G)
: Adj F G
:= { counit := NatTrans.cast FG_Bij^.id₂
, unit := NatTrans.cast (eq.symm FG_Bij^.id₁)
, id_left
:= λ c, begin
repeat { rw NatTrans.cast.com },
refine eq.trans (Cat.circ.congr_right Fun.cast_hom) _,
refine eq.trans cast_hom.circ _,
apply cast_hom.simp
end
, id_right
:= λ c, begin
repeat { rw NatTrans.cast.com },
refine eq.trans (Cat.circ.congr_left Fun.cast_hom) _,
refine eq.trans cast_hom.circ _,
apply cast_hom.simp
end
}
/-! #brief Bijections preserve final objects.
-/
definition Cat.Bij.PresFinal₂ {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ : Fun C D} {F₂ : Fun D C}
(bij : Cat.Bij F₁ F₂)
: PresFinal F₂
:= { hom := λ D_HasFinal c, bij^.Adj^.right_adj (@final_hom D D_HasFinal (F₁^.obj c))
, pres := λ D_HasFinal
, { hom_uniq := λ c h
, begin
apply bij^.Adj^.left_adj.inj,
rw bij^.Adj^.left_adj_right_adj,
apply final_hom.uniq
end
}
}
/-! #brief Bijections preserve final objects.
-/
definition Cat.Bij.PresFinal₁ {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ : Fun C D} {F₂ : Fun D C}
(bij : Cat.Bij F₁ F₂)
: PresFinal F₁
:= bij^.flip^.PresFinal₂
/-! #brief Bijections preserve initial objects.
-/
definition Cat.Bij.PresInit₁ {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ : Fun C D} {F₂ : Fun D C}
(bij : Cat.Bij F₁ F₂)
: PresInit F₁
:= { hom := λ C_HasInit c, bij^.Adj^.left_adj (@init_hom _ C_HasInit (F₂^.obj c))
, pres := λ C_HasInit
, { hom_uniq := λ c h
, begin
apply bij^.Adj^.right_adj.inj,
rw bij^.Adj^.right_adj_left_adj,
apply init_hom.uniq
end
}
}
/-! #brief Bijections preserve initial objects.
-/
definition Cat.Bij.PresInit₂ {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
{F₁ : Fun C D} {F₂ : Fun D C}
(bij : Cat.Bij F₁ F₂)
: PresInit F₂
:= bij^.flip^.PresInit₁
end qp
|
92c99e34c05a3a2ed77f4b0f6e54ebe2661a1a1c | 1f6fe2f89976b14a4567ab298c35792b21f2e50b | /homotopy/smash.hlean | 137b92a09463b574ba39b59f3b05bf67f479f4cf | [
"Apache-2.0"
] | permissive | jonas-frey/Spectral | e5c1c2f7bcac26aa55f7b1e041a81272a146198d | 72d521091525a4bc9a31cac859840efe9461cf66 | refs/heads/master | 1,610,235,743,345 | 1,505,417,795,000 | 1,505,417,795,000 | 102,653,342 | 0 | 0 | null | 1,504,728,483,000 | 1,504,728,483,000 | null | UTF-8 | Lean | false | false | 43,963 | hlean | -- Authors: Floris van Doorn
import homotopy.smash types.pointed2 .pushout homotopy.red_susp
open bool pointed eq equiv is_equiv sum bool prod unit circle cofiber prod.ops wedge is_trunc
function red_susp unit
/- To prove: Σ(X × Y) ≃ ΣX ∨ ΣY ∨ Σ(X ∧ Y) (notation means suspension, wedge, smash) -/
/- To prove: Σ(X ∧ Y) ≃ X ★ Y (?) (notation means suspension, smash, join) -/
/- To prove: A ∧ S¹ ≃ ΣA -/
/- associativity is proven in smash_adjoint -/
variables {A A' B B' C C' D E F : Type*}
namespace smash
definition elim_gluel' {P : Type} {Pmk : Πa b, P} {Pl Pr : P}
(Pgl : Πa : A, Pmk a pt = Pl) (Pgr : Πb : B, Pmk pt b = Pr) (a a' : A) :
ap (smash.elim Pmk Pl Pr Pgl Pgr) (gluel' a a') = Pgl a ⬝ (Pgl a')⁻¹ :=
!ap_con ⬝ whisker_left _ !ap_inv ⬝ !elim_gluel ◾ !elim_gluel⁻²
definition elim_gluer' {P : Type} {Pmk : Πa b, P} {Pl Pr : P}
(Pgl : Πa : A, Pmk a pt = Pl) (Pgr : Πb : B, Pmk pt b = Pr) (b b' : B) :
ap (smash.elim Pmk Pl Pr Pgl Pgr) (gluer' b b') = Pgr b ⬝ (Pgr b')⁻¹ :=
!ap_con ⬝ whisker_left _ !ap_inv ⬝ !elim_gluer ◾ !elim_gluer⁻²
definition elim_gluel'_same {P : Type} {Pmk : Πa b, P} {Pl Pr : P}
(Pgl : Πa : A, Pmk a pt = Pl) (Pgr : Πb : B, Pmk pt b = Pr) (a : A) :
elim_gluel' Pgl Pgr a a =
ap02 (smash.elim Pmk Pl Pr Pgl Pgr) (con.right_inv (gluel a)) ⬝ (con.right_inv (Pgl a))⁻¹ :=
begin
refine _ ⬝ whisker_right _ (eq_top_of_square (!ap_con_right_inv_sq))⁻¹,
refine _ ⬝ whisker_right _ !con_idp⁻¹,
refine _ ⬝ !con.assoc⁻¹,
apply whisker_left,
apply eq_con_inv_of_con_eq, symmetry,
apply con_right_inv_natural
end
definition elim_gluer'_same {P : Type} {Pmk : Πa b, P} {Pl Pr : P}
(Pgl : Πa : A, Pmk a pt = Pl) (Pgr : Πb : B, Pmk pt b = Pr) (b : B) :
elim_gluer' Pgl Pgr b b =
ap02 (smash.elim Pmk Pl Pr Pgl Pgr) (con.right_inv (gluer b)) ⬝ (con.right_inv (Pgr b))⁻¹ :=
begin
refine _ ⬝ whisker_right _ (eq_top_of_square (!ap_con_right_inv_sq))⁻¹,
refine _ ⬝ whisker_right _ !con_idp⁻¹,
refine _ ⬝ !con.assoc⁻¹,
apply whisker_left,
apply eq_con_inv_of_con_eq, symmetry,
apply con_right_inv_natural
end
definition elim'_gluel'_pt {P : Type} {Pmk : Πa b, P}
(Pgl : Πa : A, Pmk a pt = Pmk pt pt) (Pgr : Πb : B, Pmk pt b = Pmk pt pt)
(a : A) (ql : Pgl pt = idp) (qr : Pgr pt = idp) :
ap (smash.elim' Pmk Pgl Pgr ql qr) (gluel' a pt) = Pgl a :=
!elim_gluel' ⬝ whisker_left _ ql⁻²
definition elim'_gluer'_pt {P : Type} {Pmk : Πa b, P}
(Pgl : Πa : A, Pmk a pt = Pmk pt pt) (Pgr : Πb : B, Pmk pt b = Pmk pt pt)
(b : B) (ql : Pgl pt = idp) (qr : Pgr pt = idp) :
ap (smash.elim' Pmk Pgl Pgr ql qr) (gluer' b pt) = Pgr b :=
!elim_gluer' ⬝ whisker_left _ qr⁻²
protected definition rec_eq {A B : Type*} {C : Type} {f g : smash A B → C}
(Pmk : Πa b, f (smash.mk a b) = g (smash.mk a b))
(Pl : f auxl = g auxl) (Pr : f auxr = g auxr)
(Pgl : Πa, square (Pmk a pt) Pl (ap f (gluel a)) (ap g (gluel a)))
(Pgr : Πb, square (Pmk pt b) Pr (ap f (gluer b)) (ap g (gluer b))) (x : smash' A B) : f x = g x :=
begin
induction x with a b a b,
{ exact Pmk a b },
{ exact Pl },
{ exact Pr },
{ apply eq_pathover, apply Pgl },
{ apply eq_pathover, apply Pgr }
end
definition rec_eq_gluel {A B : Type*} {C : Type} {f g : smash A B → C}
{Pmk : Πa b, f (smash.mk a b) = g (smash.mk a b)}
{Pl : f auxl = g auxl} {Pr : f auxr = g auxr}
(Pgl : Πa, square (Pmk a pt) Pl (ap f (gluel a)) (ap g (gluel a)))
(Pgr : Πb, square (Pmk pt b) Pr (ap f (gluer b)) (ap g (gluer b))) (a : A) :
natural_square (smash.rec_eq Pmk Pl Pr Pgl Pgr) (gluel a) = Pgl a :=
begin
refine ap square_of_pathover !rec_gluel ⬝ _,
apply to_right_inv !eq_pathover_equiv_square
end
definition rec_eq_gluer {A B : Type*} {C : Type} {f g : smash A B → C}
{Pmk : Πa b, f (smash.mk a b) = g (smash.mk a b)}
{Pl : f auxl = g auxl} {Pr : f auxr = g auxr}
(Pgl : Πa, square (Pmk a pt) Pl (ap f (gluel a)) (ap g (gluel a)))
(Pgr : Πb, square (Pmk pt b) Pr (ap f (gluer b)) (ap g (gluer b))) (b : B) :
natural_square (smash.rec_eq Pmk Pl Pr Pgl Pgr) (gluer b) = Pgr b :=
begin
refine ap square_of_pathover !rec_gluer ⬝ _,
apply to_right_inv !eq_pathover_equiv_square
end
/- the functorial action of the smash product -/
definition smash_functor' [unfold 7] (f : A →* C) (g : B →* D) : A ∧ B → C ∧ D :=
begin
intro x, induction x,
{ exact smash.mk (f a) (g b) },
{ exact auxl },
{ exact auxr },
{ exact ap (smash.mk (f a)) (respect_pt g) ⬝ gluel (f a) },
{ exact ap (λa, smash.mk a (g b)) (respect_pt f) ⬝ gluer (g b) }
end
definition smash_functor [constructor] (f : A →* C) (g : B →* D) : A ∧ B →* C ∧ D :=
begin
fapply pmap.mk,
{ exact smash_functor' f g },
{ exact ap011 smash.mk (respect_pt f) (respect_pt g) },
end
infixr ` ∧→ `:65 := smash_functor
definition functor_gluel (f : A →* C) (g : B →* D) (a : A) :
ap (f ∧→ g) (gluel a) = ap (smash.mk (f a)) (respect_pt g) ⬝ gluel (f a) :=
!elim_gluel
definition functor_gluer (f : A →* C) (g : B →* D) (b : B) :
ap (f ∧→ g) (gluer b) = ap (λc, smash.mk c (g b)) (respect_pt f) ⬝ gluer (g b) :=
!elim_gluer
definition functor_gluel2 {C D : Type} (f : A → C) (g : B → D) (a : A) :
ap (pmap_of_map f pt ∧→ pmap_of_map g pt) (gluel a) = gluel (f a) :=
begin
refine !elim_gluel ⬝ !idp_con
end
definition functor_gluer2 {C D : Type} (f : A → C) (g : B → D) (b : B) :
ap (pmap_of_map f pt ∧→ pmap_of_map g pt) (gluer b) = gluer (g b) :=
begin
refine !elim_gluer ⬝ !idp_con
end
definition functor_gluel' (f : A →* C) (g : B →* D) (a a' : A) :
ap (f ∧→ g) (gluel' a a') = ap (smash.mk (f a)) (respect_pt g) ⬝
gluel' (f a) (f a') ⬝ (ap (smash.mk (f a')) (respect_pt g))⁻¹ :=
begin
refine !elim_gluel' ⬝ _,
refine whisker_left _ !con_inv ⬝ _,
refine !con.assoc⁻¹ ⬝ _, apply whisker_right,
apply con.assoc
end
definition functor_gluer' (f : A →* C) (g : B →* D) (b b' : B) :
ap (f ∧→ g) (gluer' b b') = ap (λc, smash.mk c (g b)) (respect_pt f) ⬝
gluer' (g b) (g b') ⬝ (ap (λc, smash.mk c (g b')) (respect_pt f))⁻¹ :=
begin
refine !elim_gluer' ⬝ _,
refine whisker_left _ !con_inv ⬝ _,
refine !con.assoc⁻¹ ⬝ _, apply whisker_right,
apply con.assoc
end
/- the statements of the above rules becomes easier if one of the functions respects the basepoint
by reflexivity -/
-- definition functor_gluel'2 {D : Type} (f : A →* C) (g : B → D) (a a' : A) :
-- ap (f ∧→ (pmap_of_map g pt)) (gluel' a a') = gluel' (f a) (f a') :=
-- begin
-- refine !ap_con ⬝ whisker_left _ !ap_inv ⬝ _,
-- refine (!functor_gluel ⬝ !idp_con) ◾ (!functor_gluel ⬝ !idp_con)⁻²
-- end
-- definition functor_gluer'2 {C : Type} (f : A → C) (g : B →* D) (b b' : B) :
-- ap (pmap_of_map f pt ∧→ g) (gluer' b b') = gluer' (g b) (g b') :=
-- begin
-- refine !ap_con ⬝ whisker_left _ !ap_inv ⬝ _,
-- refine (!functor_gluer ⬝ !idp_con) ◾ (!functor_gluer ⬝ !idp_con)⁻²
-- end
definition functor_gluel'2 {C D : Type} (f : A → C) (g : B → D) (a a' : A) :
ap (pmap_of_map f pt ∧→ pmap_of_map g pt) (gluel' a a') = gluel' (f a) (f a') :=
!ap_con ⬝ whisker_left _ !ap_inv ⬝ !functor_gluel2 ◾ !functor_gluel2⁻²
definition functor_gluer'2 {C D : Type} (f : A → C) (g : B → D) (b b' : B) :
ap (pmap_of_map f pt ∧→ pmap_of_map g pt) (gluer' b b') = gluer' (g b) (g b') :=
!ap_con ⬝ whisker_left _ !ap_inv ⬝ !functor_gluer2 ◾ !functor_gluer2⁻²
lemma functor_gluel'2_same {C D : Type} (f : A → C) (g : B → D) (a : A) :
functor_gluel'2 f g a a =
ap02 (pmap_of_map f pt ∧→ pmap_of_map g pt) (con.right_inv (gluel a)) ⬝
(con.right_inv (gluel (f a)))⁻¹ :=
begin
refine _ ⬝ whisker_right _ (eq_top_of_square (!ap_con_right_inv_sq))⁻¹,
refine _ ⬝ whisker_right _ !con_idp⁻¹,
refine _ ⬝ !con.assoc⁻¹,
apply whisker_left,
apply eq_con_inv_of_con_eq, symmetry,
apply con_right_inv_natural
end
lemma functor_gluer'2_same {C D : Type} (f : A → C) (g : B → D) (b : B) :
functor_gluer'2 (pmap_of_map f pt) g b b =
ap02 (pmap_of_map f pt ∧→ pmap_of_map g pt) (con.right_inv (gluer b)) ⬝
(con.right_inv (gluer (g b)))⁻¹ :=
begin
refine _ ⬝ whisker_right _ (eq_top_of_square (!ap_con_right_inv_sq))⁻¹,
refine _ ⬝ whisker_right _ !con_idp⁻¹,
refine _ ⬝ !con.assoc⁻¹,
apply whisker_left,
apply eq_con_inv_of_con_eq, symmetry,
apply con_right_inv_natural
end
definition smash_functor_pid [constructor] (A B : Type*) :
pid A ∧→ pid B ~* pid (A ∧ B) :=
begin
fapply phomotopy.mk,
{ intro x, induction x with a b a b,
{ reflexivity },
{ reflexivity },
{ reflexivity },
{ apply eq_pathover_id_right, apply hdeg_square, exact !functor_gluel ⬝ !idp_con },
{ apply eq_pathover_id_right, apply hdeg_square, exact !functor_gluer ⬝ !idp_con }},
{ reflexivity }
end
/- the functorial action of the smash product respects pointed homotopies, and some computation
rules for this pointed homotopy -/
definition smash_functor_phomotopy {f f' : A →* C} {g g' : B →* D}
(h₁ : f ~* f') (h₂ : g ~* g') : f ∧→ g ~* f' ∧→ g' :=
begin
induction h₁ using phomotopy_rec_idp,
induction h₂ using phomotopy_rec_idp,
reflexivity
end
/- a more explicit proof, if we ever need it -/
-- definition smash_functor_homotopy [unfold 11] {f f' : A →* C} {g g' : B →* D}
-- (h₁ : f ~* f') (h₂ : g ~* g') : f ∧→ g ~ f' ∧→ g' :=
-- begin
-- intro x, induction x with a b a b,
-- { exact ap011 smash.mk (h₁ a) (h₂ b) },
-- { reflexivity },
-- { reflexivity },
-- { apply eq_pathover,
-- refine !functor_gluel ⬝ph _ ⬝hp !functor_gluel⁻¹,
-- refine _ ⬝v square_of_eq_top (ap_mk_left (h₁ a)),
-- exact ap011_ap_square_right smash.mk (h₁ a) (to_homotopy_pt h₂) },
-- { apply eq_pathover,
-- refine !functor_gluer ⬝ph _ ⬝hp !functor_gluer⁻¹,
-- refine _ ⬝v square_of_eq_top (ap_mk_right (h₂ b)),
-- exact ap011_ap_square_left smash.mk (h₂ b) (to_homotopy_pt h₁) },
-- end
-- definition smash_functor_phomotopy [constructor] {f f' : A →* C} {g g' : B →* D}
-- (h₁ : f ~* f') (h₂ : g ~* g') : f ∧→ g ~* f' ∧→ g' :=
-- begin
-- apply phomotopy.mk (smash_functor_homotopy h₁ h₂),
-- induction h₁ with h₁ h₁₀, induction h₂ with h₂ h₂₀,
-- induction f with f f₀, induction g with g g₀,
-- induction f' with f' f'₀, induction g' with g' g'₀,
-- induction C with C c₀, induction D with D d₀, esimp at *,
-- induction h₁₀, induction h₂₀, induction f'₀, induction g'₀,
-- exact !ap_ap011⁻¹
-- end
definition smash_functor_phomotopy_refl (f : A →* C) (g : B →* D) :
smash_functor_phomotopy (phomotopy.refl f) (phomotopy.refl g) = phomotopy.rfl :=
!phomotopy_rec_idp_refl ⬝ !phomotopy_rec_idp_refl
definition smash_functor_phomotopy_symm {f₁ f₂ : A →* C} {g₁ g₂ : B →* D}
(h : f₁ ~* f₂) (k : g₁ ~* g₂) :
smash_functor_phomotopy h⁻¹* k⁻¹* = (smash_functor_phomotopy h k)⁻¹* :=
begin
induction h using phomotopy_rec_idp, induction k using phomotopy_rec_idp,
exact ap011 smash_functor_phomotopy !refl_symm !refl_symm ⬝ !smash_functor_phomotopy_refl ⬝
!refl_symm⁻¹ ⬝ !smash_functor_phomotopy_refl⁻¹⁻²**
end
definition smash_functor_phomotopy_trans {f₁ f₂ f₃ : A →* C} {g₁ g₂ g₃ : B →* D}
(h₁ : f₁ ~* f₂) (h₂ : f₂ ~* f₃) (k₁ : g₁ ~* g₂) (k₂ : g₂ ~* g₃) :
smash_functor_phomotopy (h₁ ⬝* h₂) (k₁ ⬝* k₂) =
smash_functor_phomotopy h₁ k₁ ⬝* smash_functor_phomotopy h₂ k₂ :=
begin
induction h₁ using phomotopy_rec_idp, induction h₂ using phomotopy_rec_idp,
induction k₁ using phomotopy_rec_idp, induction k₂ using phomotopy_rec_idp,
refine ap011 smash_functor_phomotopy !trans_refl !trans_refl ⬝ !trans_refl⁻¹ ⬝ idp ◾** _,
exact !smash_functor_phomotopy_refl⁻¹
end
definition smash_functor_phomotopy_trans_right {f₁ f₂ : A →* C} {g₁ g₂ g₃ : B →* D}
(h₁ : f₁ ~* f₂) (k₁ : g₁ ~* g₂) (k₂ : g₂ ~* g₃) :
smash_functor_phomotopy h₁ (k₁ ⬝* k₂) =
smash_functor_phomotopy h₁ k₁ ⬝* smash_functor_phomotopy phomotopy.rfl k₂ :=
begin
refine ap (λx, smash_functor_phomotopy x _) !trans_refl⁻¹ ⬝ !smash_functor_phomotopy_trans,
end
definition smash_functor_phomotopy_phsquare {f₁ f₂ f₃ f₄ : A →* C} {g₁ g₂ g₃ g₄ : B →* D}
{h₁ : f₁ ~* f₂} {h₂ : f₃ ~* f₄} {h₃ : f₁ ~* f₃} {h₄ : f₂ ~* f₄}
{k₁ : g₁ ~* g₂} {k₂ : g₃ ~* g₄} {k₃ : g₁ ~* g₃} {k₄ : g₂ ~* g₄}
(p : phsquare h₁ h₂ h₃ h₄) (q : phsquare k₁ k₂ k₃ k₄) :
phsquare (smash_functor_phomotopy h₁ k₁)
(smash_functor_phomotopy h₂ k₂)
(smash_functor_phomotopy h₃ k₃)
(smash_functor_phomotopy h₄ k₄) :=
!smash_functor_phomotopy_trans⁻¹ ⬝ ap011 smash_functor_phomotopy p q ⬝
!smash_functor_phomotopy_trans
definition smash_functor_eq_of_phomotopy (f : A →* C) {g g' : B →* D}
(p : g ~* g') : ap (smash_functor f) (eq_of_phomotopy p) =
eq_of_phomotopy (smash_functor_phomotopy phomotopy.rfl p) :=
begin
induction p using phomotopy_rec_idp,
refine ap02 _ !eq_of_phomotopy_refl ⬝ _,
refine !eq_of_phomotopy_refl⁻¹ ⬝ _,
apply ap eq_of_phomotopy,
exact !smash_functor_phomotopy_refl⁻¹
end
/- the functorial action preserves compositions, the interchange law -/
definition smash_functor_pcompose_homotopy [unfold 11] {C D E F : Type}
(f' : C → E) (f : A → C) (g' : D → F) (g : B → D) :
(pmap_of_map f' (f pt) ∘* pmap_of_map f pt) ∧→ (pmap_of_map g' (g pt) ∘* pmap_of_map g pt) ~
(pmap_of_map f' (f pt) ∧→ pmap_of_map g' (g pt)) ∘* (pmap_of_map f pt ∧→ pmap_of_map g pt) :=
begin
intro x, induction x with a b a b,
{ reflexivity },
{ reflexivity },
{ reflexivity },
{ apply eq_pathover, refine !functor_gluel2 ⬝ph _, esimp,
refine _ ⬝hp (ap_compose (_ ∧→ _) _ _)⁻¹,
refine _ ⬝hp ap02 _ !functor_gluel2⁻¹, refine _ ⬝hp !functor_gluel2⁻¹, exact hrfl },
{ apply eq_pathover, refine !functor_gluer2 ⬝ph _, esimp,
refine _ ⬝hp (ap_compose (_ ∧→ _) _ _)⁻¹,
refine _ ⬝hp ap02 _ !functor_gluer2⁻¹, refine _ ⬝hp !functor_gluer2⁻¹, exact hrfl }
end
definition smash_functor_pcompose (f' : C →* E) (f : A →* C) (g' : D →* F) (g : B →* D) :
(f' ∘* f) ∧→ (g' ∘* g) ~* f' ∧→ g' ∘* f ∧→ g :=
begin
induction C with C, induction D with D, induction E with E, induction F with F,
induction f with f f₀, induction f' with f' f'₀, induction g with g g₀,
induction g' with g' g'₀, esimp at *,
induction f₀, induction f'₀, induction g₀, induction g'₀,
fapply phomotopy.mk,
{ rexact smash_functor_pcompose_homotopy f' f g' g },
{ reflexivity }
end
definition smash_functor_split (f : A →* C) (g : B →* D) :
f ∧→ g ~* (pid C) ∧→ g ∘* f ∧→ (pid B) :=
smash_functor_phomotopy !pid_pcompose⁻¹* !pcompose_pid⁻¹* ⬝* !smash_functor_pcompose
/- An alternative proof which doesn't start by applying inductions, so which is more explicit -/
-- definition smash_functor_pcompose_homotopy [unfold 11] (f' : C →* E) (f : A →* C) (g' : D →* F)
-- (g : B →* D) : (f' ∘* f) ∧→ (g' ∘* g) ~ (f' ∧→ g') ∘* (f ∧→ g) :=
-- begin
-- intro x, induction x with a b a b,
-- { reflexivity },
-- { reflexivity },
-- { reflexivity },
-- { apply eq_pathover, exact abstract begin apply hdeg_square,
-- refine !functor_gluel ⬝ _ ⬝ (ap_compose (f' ∧→ g') _ _)⁻¹,
-- refine whisker_right _ !ap_con ⬝ !con.assoc ⬝ _ ⬝ ap02 _ !functor_gluel⁻¹,
-- refine (!ap_compose'⁻¹ ⬝ !ap_compose') ◾ proof !functor_gluel⁻¹ qed ⬝ !ap_con⁻¹ end end },
-- { apply eq_pathover, exact abstract begin apply hdeg_square,
-- refine !functor_gluer ⬝ _ ⬝ (ap_compose (f' ∧→ g') _ _)⁻¹,
-- refine whisker_right _ !ap_con ⬝ !con.assoc ⬝ _ ⬝ ap02 _ !functor_gluer⁻¹,
-- refine (!ap_compose'⁻¹ ⬝ !ap_compose') ◾ proof !functor_gluer⁻¹ qed ⬝ !ap_con⁻¹ end end }
-- end
-- definition smash_functor_pcompose [constructor] (f' : C →* E) (f : A →* C) (g' : D →* F) (g : B →* D) :
-- (f' ∘* f) ∧→ (g' ∘* g) ~* f' ∧→ g' ∘* f ∧→ g :=
-- begin
-- fapply phomotopy.mk,
-- { exact smash_functor_pcompose_homotopy f' f g' g },
-- { exact abstract begin induction C, induction D, induction E, induction F,
-- induction f with f f₀, induction f' with f' f'₀, induction g with g g₀,
-- induction g' with g' g'₀, esimp at *,
-- induction f₀, induction f'₀, induction g₀, induction g'₀, reflexivity end end }
-- end
definition smash_functor_pid_pcompose [constructor] (A : Type*) (g' : C →* D) (g : B →* C)
: pid A ∧→ (g' ∘* g) ~* pid A ∧→ g' ∘* pid A ∧→ g :=
smash_functor_phomotopy !pid_pcompose⁻¹* phomotopy.rfl ⬝* !smash_functor_pcompose
definition smash_functor_pcompose_pid [constructor] (B : Type*) (f' : C →* D) (f : A →* C)
: (f' ∘* f) ∧→ pid B ~* f' ∧→ (pid B) ∘* f ∧→ (pid B) :=
smash_functor_phomotopy phomotopy.rfl !pid_pcompose⁻¹* ⬝* !smash_functor_pcompose
/- composing commutes with applying homotopies -/
definition smash_functor_pcompose_phomotopy {f₂ f₂' : C →* E} {f f' : A →* C} {g₂ g₂' : D →* F}
{g g' : B →* D} (h₂ : f₂ ~* f₂') (h₁ : f ~* f') (k₂ : g₂ ~* g₂') (k₁ : g ~* g') :
phsquare (smash_functor_pcompose f₂ f g₂ g)
(smash_functor_pcompose f₂' f' g₂' g')
(smash_functor_phomotopy (h₂ ◾* h₁) (k₂ ◾* k₁))
(smash_functor_phomotopy h₂ k₂ ◾* smash_functor_phomotopy h₁ k₁) :=
begin
induction h₁ using phomotopy_rec_idp, induction h₂ using phomotopy_rec_idp,
induction k₁ using phomotopy_rec_idp, induction k₂ using phomotopy_rec_idp,
refine (ap011 smash_functor_phomotopy !pcompose2_refl !pcompose2_refl ⬝
!smash_functor_phomotopy_refl) ⬝ph** phvrfl ⬝hp**
(ap011 pcompose2 !smash_functor_phomotopy_refl !smash_functor_phomotopy_refl ⬝
!pcompose2_refl)⁻¹,
end
definition smash_functor_pid_pcompose_phomotopy_right (g₂ : D →* E) {g g' : B →* D}
(k : g ~* g') :
phsquare (smash_functor_pid_pcompose A g₂ g)
(smash_functor_pid_pcompose A g₂ g')
(smash_functor_phomotopy phomotopy.rfl (pwhisker_left g₂ k))
(pwhisker_left (pid A ∧→ g₂) (smash_functor_phomotopy phomotopy.rfl k)) :=
begin
refine smash_functor_phomotopy_phsquare _ _ ⬝h** !smash_functor_pcompose_phomotopy ⬝hp**
((ap (pwhisker_right _) !smash_functor_phomotopy_refl) ◾** idp ⬝ !pcompose2_refl_left),
exact (!pcompose2_refl ⬝ph** phvrfl)⁻¹ʰ**,
exact (phhrfl ⬝hp** !pcompose2_refl_left⁻¹)
end
section
variables {A₀₀ A₂₀ A₀₂ A₂₂ : Type*} {B₀₀ B₂₀ B₀₂ B₂₂ : Type*}
{f₁₀ : A₀₀ →* A₂₀} {f₀₁ : A₀₀ →* A₀₂} {f₂₁ : A₂₀ →* A₂₂} {f₁₂ : A₀₂ →* A₂₂}
{g₁₀ : B₀₀ →* B₂₀} {g₀₁ : B₀₀ →* B₀₂} {g₂₁ : B₂₀ →* B₂₂} {g₁₂ : B₀₂ →* B₂₂}
/- applying the functorial action of smash to squares of pointed maps -/
definition smash_functor_psquare (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) (q : psquare g₁₀ g₁₂ g₀₁ g₂₁) :
psquare (f₁₀ ∧→ g₁₀) (f₁₂ ∧→ g₁₂) (f₀₁ ∧→ g₀₁) (f₂₁ ∧→ g₂₁) :=
!smash_functor_pcompose⁻¹* ⬝* smash_functor_phomotopy p q ⬝* !smash_functor_pcompose
end
/- f ∧ g is constant if g is constant -/
definition smash_functor_pconst_right_homotopy [unfold 6] {C : Type} (f : A → C) (x : A ∧ B) :
(pmap_of_map f pt ∧→ pconst B D) x = pt :=
begin
induction x with a b a b,
{ exact gluel' (f a) pt },
{ exact (gluel pt)⁻¹ },
{ exact (gluer pt)⁻¹ },
{ apply eq_pathover, note x := functor_gluel2 f (λx : B, Point D) a, esimp [pconst] at *,
refine x ⬝ph _, refine _ ⬝hp !ap_constant⁻¹, apply square_of_eq, reflexivity },
{ apply eq_pathover, note x := functor_gluer2 f (λx : B, Point D) b, esimp [pconst] at *,
refine x ⬝ph _, refine _ ⬝hp !ap_constant⁻¹, apply square_of_eq,
rexact con.right_inv (gluel (f pt)) ⬝ (con.right_inv (gluer pt))⁻¹ }
end
definition smash_functor_pconst_right (f : A →* C) :
f ∧→ (pconst B D) ~* pconst (A ∧ B) (C ∧ D) :=
begin
induction C with C, induction f with f f₀, esimp at *, induction f₀,
fapply phomotopy.mk,
{ exact smash_functor_pconst_right_homotopy f },
{ rexact con.right_inv (gluel (f pt)) }
end
definition smash_functor_pconst_right_phomotopy {f f' : A →* C} (p : f ~* f') :
smash_functor_phomotopy p (phomotopy.refl (pconst B D)) ⬝* smash_functor_pconst_right f' =
smash_functor_pconst_right f :=
begin
induction p using phomotopy_rec_idp,
exact !smash_functor_phomotopy_refl ◾** idp ⬝ !refl_trans
end
/- This makes smash_functor into a pointed map (B →* B') →* (A ∧ B →* A ∧ B') -/
definition smash_functor_right [constructor] (A B C : Type*) :
ppmap B C →* ppmap (A ∧ B) (A ∧ C) :=
pmap.mk (smash_functor (pid A)) (eq_of_phomotopy (smash_functor_pconst_right (pid A)))
/- We want to show that smash_functor_right is natural in A, B and C.
For this we need two coherence rules. Given the function h := (f' ∘ f) ∧→ (g' ∘ g) and suppose
that either g' or g is constant. There are two ways to show that h is constant: either by using
exchange, or directly. We need to show that these two proofs result in the same pointed
homotopy. First we do the case where g is constant -/
private definition my_squarel {A : Type} {a₁ a₂ a₃ : A} (p₁ : a₁ = a₃) (p₂ : a₂ = a₃) :
square (p₁ ⬝ p₂⁻¹) p₂⁻¹ p₁ idp :=
proof square_of_eq idp qed
private definition my_squarer {A : Type} {a₁ a₂ a₃ : A} (p₁ : a₁ = a₃) (p₂ : a₁ = a₂) :
square (p₁ ⬝ p₁⁻¹) p₂⁻¹ p₂ idp :=
proof square_of_eq (con.right_inv p₁ ⬝ (con.right_inv p₂)⁻¹) qed
private definition my_cube_fillerl {A B C : Type} {g : B → C} {f : A → B} {a₁ a₂ : A} {b₀ : B}
{p : f ~ λa, b₀} {q : Πa, g (f a) = g b₀} (r : (λa, ap g (p a)) ~ q) :
cube (hrfl ⬝hp (r a₁)⁻¹) hrfl
(my_squarel (q a₁) (q a₂)) (aps g (my_squarel (p a₁) (p a₂)))
(hrfl ⬝hp (!ap_con ⬝ whisker_left _ !ap_inv ⬝ (r a₁) ◾ (r a₂)⁻²)⁻¹)
(hrfl ⬝hp (r a₂)⁻²⁻¹ ⬝hp !ap_inv⁻¹) :=
begin
induction r using homotopy.rec_on_idp, induction p using homotopy.rec_on_idp_left, exact idc
end
private definition my_cube_fillerr {B C : Type} {g : B → C} {b₀ bl br : B}
{pl : b₀ = bl} {pr : b₀ = br} {ql : g b₀ = g bl} {qr : g b₀ = g br}
(sl : ap g pl = ql) (sr : ap g pr = qr) :
cube (hrfl ⬝hp sr⁻¹) hrfl
(my_squarer ql qr) (aps g (my_squarer pl pr))
(hrfl ⬝hp (!ap_con ⬝ whisker_left _ !ap_inv ⬝ sl ◾ sl⁻²)⁻¹)
(hrfl ⬝hp sr⁻²⁻¹ ⬝hp !ap_inv⁻¹) :=
begin
induction sr, induction sl, induction pr, induction pl, exact idc
end
definition smash_functor_pcompose_pconst_homotopy {A B C D E F : Type}
(a₀ : A) (b₀ : B) (d₀ : D) (f' : C → E) (f : A → C) (g : D → F)
(x : pointed.MK A a₀ ∧ pointed.MK B b₀) :
square (smash_functor_pcompose_homotopy f' f g (λ a, d₀) x)
idp
(smash_functor_pconst_right_homotopy (λ a, f' (f a)) x)
(ap (smash_functor' (pmap.mk f' (refl (f' (f a₀)))) (pmap.mk g (refl (g d₀))))
(smash_functor_pconst_right_homotopy f x)) :=
begin
induction x with a b a b,
{ refine _ ⬝hp (functor_gluel'2 f' g (f a) (f a₀))⁻¹, exact hrfl },
{ refine _ ⬝hp !ap_inv⁻¹, refine _ ⬝hp !functor_gluel2⁻²⁻¹, exact hrfl },
{ refine _ ⬝hp !ap_inv⁻¹, refine _ ⬝hp !functor_gluer2⁻²⁻¹, exact hrfl },
{ exact abstract begin apply square_pathover,
refine !rec_eq_gluel ⬝p1 _ ⬝1p !natural_square_refl⁻¹,
refine !rec_eq_gluel ⬝p2 _ ⬝2p !natural_square_ap_fn⁻¹,
apply whisker001, apply whisker021,
apply move201, refine _ ⬝1p !eq_hconcat_hdeg_square⁻¹,
apply move221, refine _ ⬝1p !hdeg_square_hconcat_eq⁻¹,
refine ap (hconcat_eq _) !ap_inv ⬝p1 _ ⬝2p (ap (aps _) !rec_eq_gluel ⬝ !aps_eq_hconcat)⁻¹,
apply whisker021, refine _ ⬝2p !aps_hconcat_eq⁻¹, apply move221,
refine _ ⬝1p !hdeg_square_hconcat_eq⁻¹,
refine _ ⬝1p ap hdeg_square (eq_bot_of_square (transpose !ap02_ap_constant)),
apply my_cube_fillerl end end },
{ exact abstract begin apply square_pathover,
refine !rec_eq_gluer ⬝p1 _ ⬝1p !natural_square_refl⁻¹,
refine !rec_eq_gluer ⬝p2 _ ⬝2p !natural_square_ap_fn⁻¹,
apply whisker001, apply whisker021,
apply move201, refine _ ⬝1p !eq_hconcat_hdeg_square⁻¹,
apply move221, refine _ ⬝1p !hdeg_square_hconcat_eq⁻¹,
refine ap (hconcat_eq _) !ap_inv ⬝p1 _ ⬝2p (ap (aps _) !rec_eq_gluer ⬝ !aps_eq_hconcat)⁻¹,
apply whisker021, refine _ ⬝2p !aps_hconcat_eq⁻¹, apply move221,
refine _ ⬝1p !hdeg_square_hconcat_eq⁻¹,
refine _ ⬝1p ap hdeg_square (eq_bot_of_square (transpose !ap02_ap_constant)),
apply my_cube_fillerr end end }
end
definition smash_functor_pcompose_pconst (f' : C →* E) (f : A →* C) (g : D →* F) :
phsquare (smash_functor_pcompose f' f g (pconst B D))
(smash_functor_pconst_right (f' ∘* f))
(smash_functor_phomotopy phomotopy.rfl (pcompose_pconst g))
(pwhisker_left (f' ∧→ g) (smash_functor_pconst_right f) ⬝*
pcompose_pconst (f' ∧→ g)) :=
begin
induction A with A a₀, induction B with B b₀,
induction E with E e₀, induction C with C c₀, induction F with F x₀, induction D with D d₀,
induction f' with f' f'₀, induction f with f f₀, induction g with g g₀,
esimp at *, induction f'₀, induction f₀, induction g₀,
refine !smash_functor_phomotopy_refl ⬝ph** _, refine _ ⬝ !refl_trans⁻¹,
fapply phomotopy_eq,
{ intro x, refine eq_of_square _ ⬝ !con_idp,
exact smash_functor_pcompose_pconst_homotopy a₀ b₀ d₀ f' f g x, },
{ refine _ ⬝ !idp_con⁻¹,
refine whisker_right _ (!whisker_right_idp ⬝ !eq_of_square_hrfl_hconcat_eq) ⬝ _,
refine !con.assoc ⬝ _, apply con_eq_of_eq_inv_con,
refine whisker_right _ _ ⬝ _, rotate 1, rexact functor_gluel'2_same f' g (f a₀),
refine !inv_con_cancel_right ⬝ _,
exact sorry, -- TODO: FIX, the proof below should work
-- refine _ ⬝ whisker_left _ _,
-- rotate 2, refine ap (whisker_left _) _, symmetry, exact !idp_con ⬝ !idp_con ⬝ !whisker_right_idp ⬝ !idp_con,
-- symmetry, apply whisker_left_idp
}
end
/- a version where the left maps are identities -/
definition smash_functor_pid_pcompose_pconst (g : D →* F) :
phsquare (smash_functor_pid_pcompose A g (pconst B D))
(smash_functor_pconst_right (pid A))
(smash_functor_phomotopy phomotopy.rfl (pcompose_pconst g))
(pwhisker_left (pid A ∧→ g) (smash_functor_pconst_right (pid A)) ⬝*
pcompose_pconst (pid A ∧→ g)) :=
(!smash_functor_phomotopy_refl ◾** idp ⬝ !refl_trans) ⬝pv**
smash_functor_pcompose_pconst (pid A) (pid A) g
/- a small rewrite of the previous -/
definition smash_functor_pid_pcompose_pconst' (g : D →* F) :
pwhisker_left (pid A ∧→ g) (smash_functor_pconst_right (pid A)) ⬝*
pcompose_pconst (pid A ∧→ g) =
(smash_functor_pid_pcompose A g (pconst B D))⁻¹* ⬝*
(smash_functor_phomotopy phomotopy.rfl (pcompose_pconst g) ⬝*
smash_functor_pconst_right (pid A)) :=
begin
apply eq_symm_trans_of_trans_eq,
exact smash_functor_pid_pcompose_pconst g
end
/- if g' is constant -/
definition smash_functor_pconst_pcompose_homotopy [unfold 13] {A B C D E F : Type}
(a₀ : A) (b₀ : B) (x₀ : F) (f' : C → E) (f : A → C) (g : B → D)
(x : pointed.MK A a₀ ∧ pointed.MK B b₀) :
square (smash_functor_pcompose_homotopy f' f (λ a, x₀) g x)
idp
(smash_functor_pconst_right_homotopy (λ a, f' (f a)) x)
(smash_functor_pconst_right_homotopy f'
(smash_functor (pmap_of_map f a₀) (pmap_of_map g b₀) x)) :=
begin
induction x with a b a b,
{ exact hrfl },
{ exact hrfl },
{ exact hrfl },
{ exact abstract begin apply square_pathover,
refine !rec_eq_gluel ⬝p1 _ ⬝1p !natural_square_refl⁻¹,
refine !rec_eq_gluel ⬝p2 _ ⬝2p
(natural_square_compose (smash_functor_pconst_right_homotopy f') _ _)⁻¹ᵖ,
apply whisker001, apply whisker021,
apply move201, refine _ ⬝1p !eq_hconcat_hdeg_square⁻¹,
apply move221, refine _ ⬝1p !hdeg_square_hconcat_eq⁻¹,
refine ap (hconcat_eq _) !ap_inv ⬝p1 _ ⬝2p (natural_square_eq2 _ !functor_gluel2)⁻¹ᵖ,
apply whisker021,
refine _ ⬝1p ap hdeg_square (eq_of_square (!ap_constant_compose⁻¹ʰ) ⬝ !idp_con)⁻¹,
apply move221, refine _ ⬝1p !hdeg_square_hconcat_eq⁻¹,
refine _ ⬝2p !rec_eq_gluel⁻¹, apply whisker021,
apply move221, refine _ ⬝1p !hdeg_square_hconcat_eq⁻¹,
refine _ ⬝1p ap hdeg_square (eq_bot_of_square (transpose !ap02_constant)),
exact rfl2 end end },
{ exact abstract begin apply square_pathover,
refine !rec_eq_gluer ⬝p1 _ ⬝1p !natural_square_refl⁻¹,
refine !rec_eq_gluer ⬝p2 _ ⬝2p
(natural_square_compose (smash_functor_pconst_right_homotopy f') _ _)⁻¹ᵖ,
apply whisker001, apply whisker021,
apply move201, refine _ ⬝1p !eq_hconcat_hdeg_square⁻¹,
apply move221, refine _ ⬝1p !hdeg_square_hconcat_eq⁻¹,
refine ap (hconcat_eq _) !ap_inv ⬝p1 _ ⬝2p (natural_square_eq2 _ !functor_gluer2)⁻¹ᵖ,
apply whisker021,
refine _ ⬝1p ap hdeg_square (eq_of_square (!ap_constant_compose⁻¹ʰ) ⬝ !idp_con)⁻¹,
apply move221, refine _ ⬝1p !hdeg_square_hconcat_eq⁻¹,
refine _ ⬝2p !rec_eq_gluer⁻¹, apply whisker021,
apply move221, refine _ ⬝1p !hdeg_square_hconcat_eq⁻¹,
refine _ ⬝1p ap hdeg_square (eq_bot_of_square (transpose !ap02_constant)),
exact rfl2 end end },
end
definition smash_functor_pconst_pcompose (f' : C →* E) (f : A →* C) (g : B →* D) :
phsquare (smash_functor_pcompose f' f (pconst D F) g)
(smash_functor_pconst_right (f' ∘* f))
(smash_functor_phomotopy phomotopy.rfl (pconst_pcompose g))
(pwhisker_right (f ∧→ g) (smash_functor_pconst_right f') ⬝*
pconst_pcompose (f ∧→ g)) :=
begin
induction A with A a₀, induction B with B b₀,
induction E with E e₀, induction C with C c₀, induction F with F x₀, induction D with D d₀,
induction f' with f' f'₀, induction f with f f₀, induction g with g g₀,
esimp at *, induction f'₀, induction f₀, induction g₀,
refine !smash_functor_phomotopy_refl ⬝ph** _, refine _ ⬝ !refl_trans⁻¹,
fapply phomotopy_eq,
{ intro x, refine eq_of_square (smash_functor_pconst_pcompose_homotopy a₀ b₀ x₀ f' f g x) },
{ refine whisker_right _ (!whisker_right_idp ⬝ !eq_of_square_hrfl) ⬝ _,
have H : Π{A : Type} {a a' : A} (p : a = a'),
idp_con (p ⬝ p⁻¹) ⬝ con.right_inv p = idp ⬝
whisker_left idp (idp ⬝ (idp ⬝ proof whisker_right idp (idp_con (p ⬝ p⁻¹ᵖ))⁻¹ᵖ qed ⬝
whisker_left idp (con.right_inv p))), by intros; induction p; reflexivity,
rexact H (gluel (f' (f a₀))) }
end
/- a version where the left maps are identities -/
definition smash_functor_pid_pconst_pcompose (g : B →* D) :
phsquare (smash_functor_pid_pcompose A (pconst D F) g)
(smash_functor_pconst_right (pid A))
(smash_functor_phomotopy phomotopy.rfl (pconst_pcompose g))
(pwhisker_right (pid A ∧→ g) (smash_functor_pconst_right (pid A)) ⬝*
pconst_pcompose (pid A ∧→ g)) :=
(!smash_functor_phomotopy_refl ◾** idp ⬝ !refl_trans) ⬝pv**
smash_functor_pconst_pcompose (pid A) (pid A) g
/- Using these lemmas we show that smash_functor_right is natural in all arguments -/
definition smash_functor_right_natural_right (f : C →* C') :
psquare (smash_functor_right A B C) (smash_functor_right A B C')
(ppcompose_left f) (ppcompose_left (pid A ∧→ f)) :=
begin
refine _⁻¹*,
fapply phomotopy_mk_ppmap,
{ exact smash_functor_pid_pcompose A f },
{ refine idp ◾** (!phomotopy_of_eq_con ⬝ (ap phomotopy_of_eq !pcompose_left_eq_of_phomotopy ⬝
!phomotopy_of_eq_of_phomotopy) ◾** !phomotopy_of_eq_of_phomotopy) ⬝ _ ,
refine _ ⬝ (!phomotopy_of_eq_con ⬝ (ap phomotopy_of_eq !smash_functor_eq_of_phomotopy ⬝
!phomotopy_of_eq_of_phomotopy) ◾** !phomotopy_of_eq_of_phomotopy)⁻¹,
apply smash_functor_pid_pcompose_pconst }
end
definition smash_functor_right_natural_middle (f : B' →* B) :
psquare (smash_functor_right A B C) (smash_functor_right A B' C)
(ppcompose_right f) (ppcompose_right (pid A ∧→ f)) :=
begin
refine _⁻¹*,
fapply phomotopy_mk_ppmap,
{ intro g, exact smash_functor_pid_pcompose A g f },
{ refine idp ◾** (!phomotopy_of_eq_con ⬝ (ap phomotopy_of_eq !pcompose_right_eq_of_phomotopy ⬝
!phomotopy_of_eq_of_phomotopy) ◾** !phomotopy_of_eq_of_phomotopy) ⬝ _ ,
refine _ ⬝ (!phomotopy_of_eq_con ⬝ (ap phomotopy_of_eq !smash_functor_eq_of_phomotopy ⬝
!phomotopy_of_eq_of_phomotopy) ◾** !phomotopy_of_eq_of_phomotopy)⁻¹,
apply smash_functor_pid_pconst_pcompose }
end
definition smash_functor_right_natural_left (f : A →* A') :
psquare (smash_functor_right A B C) (ppcompose_right (f ∧→ (pid B)))
(smash_functor_right A' B C) (ppcompose_left (f ∧→ (pid C))) :=
begin
refine _⁻¹*,
fapply phomotopy_mk_ppmap,
{ intro g, exact smash_functor_psquare proof phomotopy.rfl qed proof phomotopy.rfl qed },
{ esimp,
refine idp ◾** (!phomotopy_of_eq_con ⬝ (ap phomotopy_of_eq !pcompose_left_eq_of_phomotopy ⬝
!phomotopy_of_eq_of_phomotopy) ◾** !phomotopy_of_eq_of_phomotopy) ⬝ _ ,
refine _ ⬝ (!phomotopy_of_eq_con ⬝ (ap phomotopy_of_eq !pcompose_right_eq_of_phomotopy ⬝
!phomotopy_of_eq_of_phomotopy) ◾** !phomotopy_of_eq_of_phomotopy)⁻¹,
apply eq_of_phsquare,
refine (phmove_bot_of_left _ !smash_functor_pconst_pcompose⁻¹ʰ**) ⬝h**
(!smash_functor_phomotopy_refl ⬝pv** !phhrfl) ⬝h** !smash_functor_pcompose_pconst ⬝vp** _,
refine !trans_assoc ⬝ !trans_assoc ⬝ idp ◾** _ ⬝ !trans_refl,
refine idp ◾** !refl_trans ⬝ !trans_left_inv }
end
/- f ∧ g is a pointed equivalence if f and g are -/
definition smash_functor_using_pushout [unfold 7] (f : A →* C) (g : B →* D) : A ∧ B → C ∧ D :=
begin
fapply pushout.functor (sum_functor f g) (prod_functor f g) id,
{ intro v, induction v with a b,
exact prod_eq idp (respect_pt g),
exact prod_eq (respect_pt f) idp },
{ intro v, induction v with a b: reflexivity }
end
definition smash_functor_homotopy_pushout_functor (f : A →* C) (g : B →* D) :
f ∧→ g ~ smash_functor_using_pushout f g :=
begin
intro x, induction x,
{ reflexivity },
{ reflexivity },
{ reflexivity },
{ apply eq_pathover, refine !elim_gluel ⬝ph _ ⬝hp !pushout.elim_glue⁻¹,
apply hdeg_square, esimp, apply whisker_right, exact !ap_ap011⁻¹ },
{ apply eq_pathover, refine !elim_gluer ⬝ph _ ⬝hp !pushout.elim_glue⁻¹,
apply hdeg_square, esimp, apply whisker_right, exact !ap_ap011⁻¹ }
end
local attribute is_equiv_sum_functor [instance]
definition smash_pequiv [constructor] (f : A ≃* C) (g : B ≃* D) : A ∧ B ≃* C ∧ D :=
begin
fapply pequiv_of_pmap (f ∧→ g),
refine @homotopy_closed _ _ _ _ _ (smash_functor_homotopy_pushout_functor f g)⁻¹ʰᵗʸ,
apply pushout.is_equiv_functor
end
definition smash_pequiv_left [constructor] (B : Type*) (f : A ≃* C) : A ∧ B ≃* C ∧ B :=
smash_pequiv f pequiv.rfl
definition smash_pequiv_right [constructor] (A : Type*) (g : B ≃* D) : A ∧ B ≃* A ∧ D :=
smash_pequiv pequiv.rfl g
/- A ∧ B ≃* pcofiber (pprod_of_wedge A B) -/
definition prod_of_wedge [unfold 3] (v : wedge A B) : A × B :=
begin
induction v with a b ,
{ exact (a, pt) },
{ exact (pt, b) },
{ reflexivity }
end
definition wedge_of_sum [unfold 3] (v : A + B) : wedge A B :=
begin
induction v with a b,
{ exact pushout.inl a },
{ exact pushout.inr b }
end
definition prod_of_wedge_of_sum [unfold 3] (v : A + B) : prod_of_wedge (wedge_of_sum v) = prod_of_sum v :=
begin
induction v with a b,
{ reflexivity },
{ reflexivity }
end
end smash open smash
namespace pushout
definition eq_inl_pushout_wedge_of_sum [unfold 3] (v : wedge A B) :
inl pt = inl v :> pushout wedge_of_sum bool_of_sum :=
begin
induction v with a b,
{ exact glue (sum.inl pt) ⬝ (glue (sum.inl a))⁻¹, },
{ exact ap inl (glue ⋆) ⬝ glue (sum.inr pt) ⬝ (glue (sum.inr b))⁻¹, },
{ apply eq_pathover_constant_left,
refine !con.right_inv ⬝pv _ ⬝vp !con_inv_cancel_right⁻¹, exact square_of_eq idp }
end
variables (A B)
definition eq_inr_pushout_wedge_of_sum [unfold 3] (b : bool) :
inl pt = inr b :> pushout (@wedge_of_sum A B) bool_of_sum :=
begin
induction b,
{ exact glue (sum.inl pt) },
{ exact ap inl (glue ⋆) ⬝ glue (sum.inr pt) }
end
definition is_contr_pushout_wedge_of_sum : is_contr (pushout (@wedge_of_sum A B) bool_of_sum) :=
begin
apply is_contr.mk (pushout.inl pt),
intro x, induction x with v b w,
{ apply eq_inl_pushout_wedge_of_sum },
{ apply eq_inr_pushout_wedge_of_sum },
{ apply eq_pathover_constant_left_id_right,
induction w with a b,
{ apply whisker_rt, exact vrfl },
{ apply whisker_rt, exact vrfl }}
end
definition bool_of_sum_of_bool {A B : Type*} (b : bool) : bool_of_sum (sum_of_bool A B b) = b :=
by induction b: reflexivity
/- a different proof, using pushout lemmas, and the fact that the wedge is the pushout of
A + B <-- 2 --> 1 -/
definition pushout_wedge_of_sum_equiv_unit : pushout (@wedge_of_sum A B) bool_of_sum ≃ unit :=
begin
refine pushout_hcompose_equiv (sum_of_bool A B) (wedge_equiv_pushout_sum A B ⬝e !pushout.symm)
_ _ ⬝e _,
exact erfl,
intro x, induction x,
reflexivity, reflexivity,
exact bool_of_sum_of_bool,
apply pushout_of_equiv_right
end
end pushout open pushout
namespace smash
variables (A B)
definition smash_equiv_cofiber : smash A B ≃ cofiber (@prod_of_wedge A B) :=
begin
unfold [smash, cofiber, smash'], symmetry,
fapply pushout_vcompose_equiv wedge_of_sum,
{ symmetry, apply equiv_unit_of_is_contr, apply is_contr_pushout_wedge_of_sum },
{ intro x, reflexivity },
{ apply prod_of_wedge_of_sum }
end
definition smash_punit_pequiv [constructor] : smash A punit ≃* punit :=
begin
apply pequiv_punit_of_is_contr,
apply is_contr.mk (smash.mk pt ⋆), intro x,
induction x,
{ induction b, exact gluel' pt a },
{ exact gluel pt },
{ exact gluer pt },
{ apply eq_pathover_constant_left_id_right, apply square_of_eq_top,
exact whisker_right _ !idp_con⁻¹ },
{ apply eq_pathover_constant_left_id_right, induction b,
refine !con.right_inv ⬝pv _, exact square_of_eq idp },
end
definition pprod_of_wedge [constructor] : wedge A B →* A ×* B :=
begin
fconstructor,
{ exact prod_of_wedge },
{ reflexivity }
end
definition smash_pequiv_pcofiber [constructor] : smash A B ≃* pcofiber (pprod_of_wedge A B) :=
begin
apply pequiv_of_equiv (smash_equiv_cofiber A B),
exact cofiber.glue pt
end
variables {A B}
/- commutativity -/
definition smash_flip' [unfold 3] (x : smash A B) : smash B A :=
begin
induction x,
{ exact smash.mk b a },
{ exact auxr },
{ exact auxl },
{ exact gluer a },
{ exact gluel b }
end
definition smash_flip_smash_flip' [unfold 3] (x : smash A B) : smash_flip' (smash_flip' x) = x :=
begin
induction x,
{ reflexivity },
{ reflexivity },
{ reflexivity },
{ apply eq_pathover_id_right,
refine ap_compose' smash_flip' _ _ ⬝ ap02 _ !elim_gluel ⬝ !elim_gluer ⬝ph _,
apply hrfl },
{ apply eq_pathover_id_right,
refine ap_compose' smash_flip' _ _ ⬝ ap02 _ !elim_gluer ⬝ !elim_gluel ⬝ph _,
apply hrfl }
end
variables (A B)
definition smash_flip [constructor] : smash A B →* smash B A :=
pmap.mk smash_flip' idp
definition smash_flip_smash_flip [constructor] :
smash_flip B A ∘* smash_flip A B ~* pid (A ∧ B) :=
phomotopy.mk smash_flip_smash_flip' idp
definition smash_comm [constructor] : smash A B ≃* smash B A :=
begin
apply pequiv.MK, do 2 apply smash_flip_smash_flip
end
variables {A B}
definition smash_flip_smash_functor' [unfold 7] (f : A →* C) (g : B →* D) : hsquare
smash_flip' smash_flip' (smash_functor' f g) (smash_functor' g f) :=
begin
intro x, induction x,
{ reflexivity },
{ reflexivity },
{ reflexivity },
{ apply eq_pathover,
refine ap_compose' (smash_functor' _ _) _ _ ⬝ ap02 _ !elim_gluel ⬝ !functor_gluer ⬝ph _
⬝hp (ap_compose' smash_flip' _ _ ⬝ ap02 _ !functor_gluel)⁻¹ᵖ,
refine _ ⬝hp (!ap_con ⬝ !ap_compose'⁻¹ ◾ !elim_gluel)⁻¹, exact hrfl },
{ apply eq_pathover,
refine ap_compose' (smash_functor' _ _) _ _ ⬝ ap02 _ !elim_gluer ⬝ !functor_gluel ⬝ph _
⬝hp (ap_compose' smash_flip' _ _ ⬝ ap02 _ !functor_gluer)⁻¹ᵖ,
refine _ ⬝hp (!ap_con ⬝ !ap_compose'⁻¹ ◾ !elim_gluer)⁻¹, exact hrfl },
end
definition smash_flip_smash_functor (f : A →* C) (g : B →* D) :
psquare (smash_flip A B) (smash_flip C D) (f ∧→ g) (g ∧→ f) :=
begin
apply phomotopy.mk (smash_flip_smash_functor' f g), refine !idp_con ⬝ _ ⬝ !idp_con⁻¹,
refine !ap_ap011 ⬝ _, apply ap011_flip,
end
end smash
|
9d7102b1e9069bcdb17d7d6bad26681edc935496 | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /library/algebra/category/yoneda.lean | 72176d657419bfd7fd0ba45c724bf23034b3ae5e | [
"Apache-2.0"
] | permissive | codyroux/lean | 7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3 | 0cca265db19f7296531e339192e9b9bae4a31f8b | refs/heads/master | 1,610,909,964,159 | 1,407,084,399,000 | 1,416,857,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 306 | lean | -- Copyright (c) 2014 Floris van Doorn. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Author: Floris van Doorn
import .constructions
open eq eq.ops category functor category.ops prod
namespace yoneda
--representable functor
section
end
end yoneda
|
e07850e40c0e6eb5d8dbd1997cc9100ba522521f | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/tactic/simps.lean | f4c64207c27f4de4966c86c8d4f5f959cdbaded3 | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 25,532 | lean | /-
Copyright (c) 2019 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import tactic.core
/-!
# simps attribute
This file defines the `@[simps]` attribute, to automatically generate simp-lemmas
reducing a definition when projections are applied to it.
## Implementation Notes
There are three attributes being defined here
* `@[simps]` is the attribute for objects of a structure or instances of a class. It will
automatically generate simplication lemmas for each projection of the object/instance that contains
data. See the doc strings for `simps_attr` and `simps_cfg` for more details and configuration
options
* `@[_simps_str]` is automatically added to structures that have been used in `@[simps]` at least
once. They contain the data of the projections used for this structure by all following
invocations of `@[simps]`.
* `@[notation_class]` should be added to all classes that define notation, like `has_mul` and
`has_zero`. This specifies that the projections that `@[simps]` used are the projections from
these notation classes instead of the projections of the superclasses.
Example: if `has_mul` is tagged with `@[notation_class]` then the projection used for `semigroup`
will be `λ α hα, @has_mul.mul α (@semigroup.to_has_mul α hα)` instead of `@semigroup.mul`.
## Tags
structures, projections, simp, simplifier, generates declarations
-/
open tactic expr
setup_tactic_parser
reserve notation `initialize_simps_projections`
declare_trace simps.verbose
/--
The `@[_simps_str]` attribute specifies the preferred projections of the given structure,
used by the `@[simps]` attribute.
- This will usually be tagged by the `@[simps]` tactic.
- You can also generate this with the command `initialize_simps_projections`.
- To change the default value, see Note [custom simps projection].
- You are strongly discouraged to add this attribute manually.
- The first argument is the list of names of the universe variables used in the structure
- The second argument is the expressions that correspond to the projections of the structure
(these can contain the universe parameters specified in the first argument).
-/
@[user_attribute] meta def simps_str_attr : user_attribute unit (list name × list expr) :=
{ name := `_simps_str,
descr := "An attribute specifying the projection of the given structure.",
parser := do e ← texpr, eval_pexpr _ e }
/--
The `@[notation_class]` attribute specifies that this is a notation class,
and this notation should be used instead of projections by @[simps].
* The first argument `tt` for notation classes and `ff` for classes applied to the structure,
like `has_coe_to_sort` and `has_coe_to_fun`
* The second argument is the name of the projection (by default it is the first projection
of the structure)
-/
@[user_attribute] meta def notation_class_attr : user_attribute unit (bool × option name) :=
{ name := `notation_class,
descr := "An attribute specifying that this is a notation class. Used by @[simps].",
parser := prod.mk <$> (option.is_none <$> (tk "*")?) <*> ident? }
attribute [notation_class] has_zero has_one has_add has_mul has_inv has_neg has_sub has_div has_dvd
has_mod has_le has_lt has_append has_andthen has_union has_inter has_sdiff has_equiv has_subset
has_ssubset has_emptyc has_insert has_singleton has_sep has_mem has_pow
attribute [notation_class* coe_sort] has_coe_to_sort
attribute [notation_class* coe_fn] has_coe_to_fun
/--
Get the projections used by `simps` associated to a given structure `str`. The second component is
the list of projections, and the first component the (shared) list of universe levels used by the
projections.
The returned universe levels are the universe levels of the structure. For the projections there
are three cases
* If the declaration `{structure_name}.simps.{projection_name}` has been declared, then the value
of this declaration is used (after checking that it is definitionally equal to the actual
projection
* Otherwise, for every class with the `notation_class` attribute, and the structure has an
instance of that notation class, then the projection of that notation class is used for the
projection that is definitionally equal to it (if there is such a projection).
This means in practice that coercions to function types and sorts will be used instead of
a projection, if this coercion is definitionally equal to a projection. Furthermore, for
notation classes like `has_mul` and `has_zero` those projections are used instead of the
corresponding projection
* Otherwise, the projection of the structure is chosen.
For example: ``simps_get_raw_projections env `prod`` gives the default projections
```
([u, v], [prod.fst.{u v}, prod.snd.{u v}])
```
while ``simps_get_raw_projections env `equiv`` gives
```
([u_1, u_2], [λ α β, coe_fn, λ {α β} (e : α ≃ β), ⇑(e.symm), left_inv, right_inv])
```
after declaring the coercion from `equiv` to function and adding the declaration
```
def equiv.simps.inv_fun {α β} (e : α ≃ β) : β → α := e.symm
```
-/
-- if performance becomes a problem, possible heuristic: use the names of the projections to
-- skip all classes that don't have the corresponding field.
meta def simps_get_raw_projections (e : environment) (str : name) :
tactic (list name × list expr) := do
has_attr ← has_attribute' `_simps_str str,
if has_attr then do
when_tracing `simps.verbose trace!"[simps] > found projection information for structure {str}",
simps_str_attr.get_param str
else do
when_tracing `simps.verbose trace!"[simps] > generating projection information for structure {str}:",
d_str ← e.get str,
projs ← e.structure_fields_full str,
let raw_univs := d_str.univ_params,
let raw_levels := level.param <$> raw_univs,
/- Define the raw expressions for the projections, by default as the projections
(as an expression), but this can be overriden by the user. -/
raw_exprs ← projs.mmap (λ proj, let raw_expr : expr := expr.const proj raw_levels in do
custom_proj ← (do
decl ← e.get (str ++ `simps ++ proj.last),
let custom_proj := decl.value.instantiate_univ_params $ decl.univ_params.zip raw_levels,
when_tracing `simps.verbose trace!"[simps] > found custom projection for {proj}:\n > {custom_proj}",
return custom_proj) <|> return raw_expr,
is_def_eq custom_proj raw_expr <|>
fail!"Invalid custom projection:\n {custom_proj}\nExpression is not definitionally equal to {raw_expr}.",
return custom_proj),
/- Check for other coercions and type-class arguments to use as projections instead. -/
(args, _) ← open_pis d_str.type,
let e_str := (expr.const str raw_levels).mk_app args,
automatic_projs ← attribute.get_instances `notation_class,
raw_exprs ← automatic_projs.mfoldl (λ (raw_exprs : list expr) class_nm, (do
(is_class, proj_nm) ← notation_class_attr.get_param class_nm,
proj_nm ← proj_nm <|> (e.structure_fields_full class_nm).map list.head,
/- For this class, find the projection. `raw_expr` is the projection found applied to `args`,
and `lambda_raw_expr` has the arguments `args` abstracted. -/
(raw_expr, lambda_raw_expr) ← if is_class then (do
guard $ args.length = 1,
let e_inst_type := (expr.const class_nm raw_levels).mk_app args,
(hyp, e_inst) ← try_for 1000 (mk_conditional_instance e_str e_inst_type),
raw_expr ← mk_mapp proj_nm [args.head, e_inst],
clear hyp,
raw_expr_lambda ← lambdas [hyp] raw_expr, -- `expr.bind_lambda` doesn't give the correct type
return (raw_expr, raw_expr_lambda.lambdas args))
else (do
e_inst_type ← to_expr ((expr.const class_nm []).app (pexpr.of_expr e_str)),
e_inst ← try_for 1000 (mk_instance e_inst_type),
raw_expr ← mk_mapp proj_nm [e_str, e_inst],
return (raw_expr, raw_expr.lambdas args)),
raw_expr_whnf ← whnf raw_expr,
let relevant_proj := raw_expr_whnf.binding_body.get_app_fn.const_name,
/- Use this as projection, if the function reduces to a projection, and this projection has
not been overrriden by the user. -/
guard (projs.any (= relevant_proj) ∧ ¬ e.contains (str ++ `simps ++ relevant_proj.last)),
let pos := projs.find_index (= relevant_proj),
when_tracing `simps.verbose trace!" > using function {proj_nm} instead of the default projection {relevant_proj.last}.",
return $ raw_exprs.update_nth pos lambda_raw_expr) <|> return raw_exprs) raw_exprs,
when_tracing `simps.verbose trace!"[simps] > generated projections for {str}:\n > {raw_exprs}",
simps_str_attr.set str (raw_univs, raw_exprs) tt,
return (raw_univs, raw_exprs)
/--
You can specify custom projections for the `@[simps]` attribute.
To do this for the projection `my_structure.awesome_projection` by adding a declaration
`my_structure.simps.awesome_projection` that is definitionally equal to
`my_structure.awesome_projection` but has the projection in the desired (simp-normal) form.
You can initialize the projections `@[simps]` uses with `initialize_simps_projections`
(after declaring any custom projections). This is not necessary, it has the same effect
if you just add `@[simps]` to a declaration.
If you do anything to change the default projections, make sure to call either `@[simps]` or
`initialize_simps_projections` in the same file as the structure declaration. Otherwise, you might
have a file that imports the structure, but not your custom projections.
-/
library_note "custom simps projection"
/-- Specify simps projections, see Note [custom simps projection].
Set `trace.simps.verbose` to true to see the generated projections. -/
@[user_command] meta def initialize_simps_projections_cmd
(_ : parse $ tk "initialize_simps_projections") : parser unit := do
env ← get_env,
ns ← many ident,
ns.mmap' $ λ nm, do nm ← resolve_constant nm, simps_get_raw_projections env nm
/--
Get the projections of a structure used by `@[simps]` applied to the appropriate arguments.
Returns a list of triples (projection expression, projection name, corresponding right-hand-side),
one for each projection.
Example: ``simps_get_projection_exprs env `(α × β) `(⟨x, y⟩)`` will give the output
```
[(`(@prod.fst.{u v} α β), `prod.fst, `(x)), (`(@prod.snd.{u v} α β), `prod.snd, `(y))]
```
-/
-- This function does not use `tactic.mk_app` or `tactic.mk_mapp`, because the the given arguments
-- might not uniquely specify the universe levels yet.
meta def simps_get_projection_exprs (e : environment) (tgt : expr)
(rhs : expr) : tactic $ list $ expr × name × expr := do
let params := get_app_args tgt, -- the parameters of the structure
(params.zip $ (get_app_args rhs).take params.length).mmap' (λ ⟨a, b⟩, is_def_eq a b)
<|> fail "unreachable code (1)",
let str := tgt.get_app_fn.const_name,
projs ← e.structure_fields_full str,
let rhs_args := (get_app_args rhs).drop params.length, -- the fields of the object
guard (rhs_args.length = projs.length) <|> fail "unreachable code (2)",
(raw_univs, raw_exprs) ← simps_get_raw_projections e str,
let univs := raw_univs.zip tgt.get_app_fn.univ_levels,
let proj_exprs := raw_exprs.map $
λ raw_expr, (raw_expr.instantiate_univ_params univs).instantiate_lambdas_or_apps params,
return $ proj_exprs.zip $ projs.zip rhs_args
/--
Configuration options for the `@[simps]` attribute.
* `attrs` specifies the list of attributes given to the generated lemmas. Default: ``[`simp]``.
If ``[`simp]`` is in the list, then ``[`_refl_lemma]`` is added automatically if appropriate.
The attributes can be either basic attributes, or user attributes without parameters.
* `short_name` gives the generated lemmas a shorter name. This only has an effect when multiple
projections are applied in a lemma. When this is `ff` (default) all projection names will be
appended to the definition name to form the lemma name, and when this is `tt`, only the
last projection name will be appended.
* if `simp_rhs` is `tt` then the right-hand-side of the generated lemmas will be put simp-normal form
* `type_md` specifies how aggressively definitions are unfolded in the type of expressions
for the purposes of finding out whether the type is a function type.
Default: `instances`. This will unfold coercion instances (so that a coercion to a function type
is recognized as a function type), but not declarations like `set`.
* `rhs_md` specifies how aggressively definition in the declaration are unfolded for the purposes
of finding out whether it is a constructor.
Default: `none`
* If `fully_applied` is `ff` then the generated simp-lemmas will be between non-fully applied
terms, i.e. equalities between functions. This does not restrict the recursive behavior of
`@[simps]`, so only the "final" projection will be non-fully applied.
However, it can be used in combination with explicit field names, to get a partially applied
intermediate projection.
-/
@[derive [has_reflect, inhabited]] structure simps_cfg :=
(attrs := [`simp])
(short_name := ff)
(simp_rhs := ff)
(type_md := transparency.instances)
(rhs_md := transparency.none)
(fully_applied := tt)
/-- Add a lemma with `nm` stating that `lhs = rhs`. `type` is the type of both `lhs` and `rhs`,
`args` is the list of local constants occurring, and `univs` is the list of universe variables.
If `add_simp` then we make the resulting lemma a simp-lemma. -/
meta def simps_add_projection (nm : name) (type lhs rhs : expr) (args : list expr)
(univs : list name) (cfg : simps_cfg) : tactic unit := do
-- simplify `rhs` if `simp_rhs` and `simp` makes progress
(rhs, prf) ← (guard cfg.simp_rhs >> rhs.simp) <|> prod.mk rhs <$> mk_app `eq.refl [type, lhs],
eq_ap ← mk_mapp `eq $ [type, lhs, rhs].map some,
decl_name ← get_unused_decl_name nm,
let decl_type := eq_ap.pis args,
let decl_value := prf.lambdas args,
let decl := declaration.thm decl_name univs decl_type (pure decl_value),
when_tracing `simps.verbose trace!"[simps] > adding projection\n > {decl_name} : {decl_type}",
decorate_error ("failed to add projection lemma " ++ decl_name.to_string ++ ". Nested error:") $
add_decl decl,
b ← succeeds $ is_def_eq lhs rhs,
when (b ∧ `simp ∈ cfg.attrs) (set_basic_attribute `_refl_lemma decl_name tt),
cfg.attrs.mmap' $ λ nm, set_attribute nm decl_name tt
/-- Derive lemmas specifying the projections of the declaration.
If `todo` is non-empty, it will generate exactly the names in `todo`. -/
meta def simps_add_projections : ∀(e : environment) (nm : name) (suffix : string)
(type lhs rhs : expr) (args : list expr) (univs : list name) (must_be_str : bool)
(cfg : simps_cfg) (todo : list string), tactic unit
| e nm suffix type lhs rhs args univs must_be_str cfg todo := do
-- we don't want to unfold non-reducible definitions (like `set`) to apply more arguments
(type_args, tgt) ← whnf type cfg.type_md >>= open_pis,
tgt ← whnf tgt,
let new_args := args ++ type_args,
let lhs_ap := lhs.mk_app type_args,
let rhs_ap := rhs.instantiate_lambdas_or_apps type_args,
let str := tgt.get_app_fn.const_name,
let new_nm := nm.append_suffix suffix,
/- We want to generate the current projection if it is in `todo` -/
let todo_next := todo.filter (≠ ""),
/- Don't recursively continue if `str` is not a structure. As a special case, also don't
recursively continue if the nested structure is `prod` or `pprod`, unless projections are
specified manually. -/
if e.is_structure str ∧ ¬(todo = [] ∧ str ∈ [`prod, `pprod] ∧ ¬must_be_str) then do
[intro] ← return $ e.constructors_of str | fail "unreachable code (3)",
rhs_whnf ← whnf rhs_ap cfg.rhs_md,
(rhs_ap, todo_now) ← if h : ¬is_constant_of rhs_ap.get_app_fn intro ∧
is_constant_of rhs_whnf.get_app_fn intro then
/- If this was a desired projection, we want to apply it before taking the whnf.
However, if the current field is an eta-expansion (see below), we first want
to eta-reduce it and only then construct the projection.
This makes the flow of this function hard to follow. -/
when ("" ∈ todo) (if cfg.fully_applied then
simps_add_projection new_nm tgt lhs_ap rhs_ap new_args univs cfg else
simps_add_projection new_nm type lhs rhs args univs cfg) >>
return (rhs_whnf, ff) else
return (rhs_ap, "" ∈ todo),
if is_constant_of (get_app_fn rhs_ap) intro then do -- if the value is a constructor application
tuples ← simps_get_projection_exprs e tgt rhs_ap,
let projs := tuples.map $ λ x, x.2.1,
let pairs := tuples.map $ λ x, x.2,
eta ← expr.is_eta_expansion_aux rhs_ap pairs, -- check whether `rhs_ap` is an eta-expansion
let rhs_ap := eta.lhoare rhs_ap, -- eta-reduce `rhs_ap`
/- As a special case, we want to automatically generate the current projection if `rhs_ap`
was an eta-expansion. Also, when this was a desired projection, we need to generate the
current projection if we haven't done it above. -/
when (todo_now ∨ (todo = [] ∧ eta.is_some)) $
if cfg.fully_applied then
simps_add_projection new_nm tgt lhs_ap rhs_ap new_args univs cfg else
simps_add_projection new_nm type lhs rhs args univs cfg,
/- We stop if no further projection is specified or if we just reduced an eta-expansion and we
automatically choose projections -/
when ¬(todo = [""] ∨ (eta.is_some ∧ todo = [])) $ do
let todo := todo_next,
-- check whether all elements in `todo` have a projection as prefix
guard (todo.all $ λ x, projs.any $ λ proj, ("_" ++ proj.last).is_prefix_of x) <|>
let x := (todo.find $ λ x, projs.all $ λ proj, ¬ ("_" ++ proj.last).is_prefix_of x).iget,
simp_lemma := nm.append_suffix $ suffix ++ x,
needed_proj := (x.split_on '_').tail.head in
fail!"Invalid simp-lemma {simp_lemma}. Projection {needed_proj} doesn't exist.",
tuples.mmap' $ λ ⟨proj_expr, proj, new_rhs⟩, do
new_type ← infer_type new_rhs,
let new_todo := todo.filter_map $ λ x, string.get_rest x $ "_" ++ proj.last,
b ← is_prop new_type,
-- we only continue with this field if it is non-propositional or mentioned in todo
when ((¬ b ∧ todo = []) ∨ new_todo ≠ []) $ do
let new_lhs := proj_expr.instantiate_lambdas_or_apps [lhs_ap],
let new_suffix := if cfg.short_name then "_" ++ proj.last else
suffix ++ "_" ++ proj.last,
simps_add_projections e nm new_suffix new_type new_lhs new_rhs new_args univs
ff cfg new_todo
else do
when must_be_str $
fail!"Invalid `simps` attribute. The body is not a constructor application:\n{rhs_ap}\nPossible solution: add option {{rhs_md := semireducible}.",
when (todo_next ≠ []) $
fail!"Invalid simp-lemma {nm.append_suffix $ suffix ++ todo_next.head}. The given definition is not a constructor application:\n{rhs_ap}\nPossible solution: add option {{rhs_md := semireducible}.",
if cfg.fully_applied then
simps_add_projection new_nm tgt lhs_ap rhs_ap new_args univs cfg else
simps_add_projection new_nm type lhs rhs args univs cfg
else do
when must_be_str $
fail "Invalid `simps` attribute. Target is not a structure",
when (todo_next ≠ [] ∧ str ∉ [`prod, `pprod]) $
fail!"Invalid simp-lemma {nm.append_suffix $ suffix ++ todo_next.head}. Projection {(todo_next.head.split_on '_').tail.head} doesn't exist, because target is not a structure.",
if cfg.fully_applied then
simps_add_projection new_nm tgt lhs_ap rhs_ap new_args univs cfg else
simps_add_projection new_nm type lhs rhs args univs cfg
/-- `simps_tac` derives simp-lemmas for all (nested) non-Prop projections of the declaration.
If `todo` is non-empty, it will generate exactly the names in `todo`.
If `short_nm` is true, the generated names will only use the last projection name. -/
meta def simps_tac (nm : name) (cfg : simps_cfg := {}) (todo : list string := []) : tactic unit := do
e ← get_env,
d ← e.get nm,
let lhs : expr := const d.to_name (d.univ_params.map level.param),
let todo := todo.erase_dup.map $ λ proj, "_" ++ proj,
simps_add_projections e nm "" d.type lhs d.value [] d.univ_params tt cfg todo
/-- The parser for the `@[simps]` attribute. -/
meta def simps_parser : parser (list string × simps_cfg) := do
/- note: we currently don't check whether the user has written a nonsense namespace as arguments. -/
prod.mk <$> many (name.last <$> ident) <*>
(do some e ← parser.pexpr? | return {}, eval_pexpr simps_cfg e)
/--
The `@[simps]` attribute automatically derives lemmas specifying the projections of this
declaration.
Example:
```lean
@[simps] def foo : ℕ × ℤ := (1, 2)
```
derives two simp-lemmas:
```lean
@[simp] lemma foo_fst : foo.fst = 1
@[simp] lemma foo_snd : foo.snd = 2
```
* It does not derive simp-lemmas for the prop-valued projections.
* It will automatically reduce newly created beta-redexes, but will not unfold any definitions.
* If the structure has a coercion to either sorts or functions, and this is defined to be one
of the projections, then this coercion will be used instead of the projection.
* If the structure is a class that has an instance to a notation class, like `has_mul`, then this
notation is used instead of the corresponding projection.
* You can specify custom projections, by giving a declaration with name
`{structure_name}.simps.{projection_name}`. See Note [custom simps projection].
Example:
```lean
def equiv.simps.inv_fun (e : α ≃ β) : β → α := e.symm
@[simps] def equiv.trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ :=
⟨e₂ ∘ e₁, e₁.symm ∘ e₂.symm⟩
```
generates
```
@[simp] lemma equiv.trans_to_fun : ∀ {α β γ} (e₁ e₂) (a : α), ⇑(e₁.trans e₂) a = (⇑e₂ ∘ ⇑e₁) a
@[simp] lemma equiv.trans_inv_fun : ∀ {α β γ} (e₁ e₂) (a : γ),
⇑((e₁.trans e₂).symm) a = (⇑(e₁.symm) ∘ ⇑(e₂.symm)) a
```
* If one of the fields itself is a structure, this command will recursively create
simp-lemmas for all fields in that structure.
* Exception: by default it will not recursively create simp-lemmas for fields in the structures
`prod` and `pprod`. Give explicit projection names to override this.
Example:
```lean
structure my_prod (α β : Type*) := (fst : α) (snd : β)
@[simps] def foo : prod ℕ ℕ × my_prod ℕ ℕ := ⟨⟨1, 2⟩, 3, 4⟩
```
generates
```lean
@[simp] lemma foo_fst : foo.fst = (1, 2)
@[simp] lemma foo_snd_fst : foo.snd.fst = 3
@[simp] lemma foo_snd_snd : foo.snd.snd = 4
```
* You can use `@[simps proj1 proj2 ...]` to only generate the projection lemmas for the specified
projections.
* Recursive projection names can be specified using `proj1_proj2_proj3`.
This will create a lemma of the form `foo.proj1.proj2.proj3 = ...`.
Example:
```lean
structure my_prod (α β : Type*) := (fst : α) (snd : β)
@[simps fst fst_fst snd] def foo : prod ℕ ℕ × my_prod ℕ ℕ := ⟨⟨1, 2⟩, 3, 4⟩
```
generates
```lean
@[simp] lemma foo_fst : foo.fst = (1, 2)
@[simp] lemma foo_fst_fst : foo.fst.fst = 1
@[simp] lemma foo_snd : foo.snd = {fst := 3, snd := 4}
```
* If one of the values is an eta-expanded structure, we will eta-reduce this structure.
Example:
```lean
structure equiv_plus_data (α β) extends α ≃ β := (data : bool)
@[simps] def bar {α} : equiv_plus_data α α := { data := tt, ..equiv.refl α }
```
generates the following, even though Lean inserts an eta-expanded version of `equiv.refl α` in the
definition of `bar`:
```lean
@[simp] lemma bar_to_equiv : ∀ {α : Sort u_1}, bar.to_equiv = equiv.refl α
@[simp] lemma bar_data : ∀ {α : Sort u_1}, bar.data = tt
```
* For configuration options, see the doc string of `simps_cfg`.
* The precise syntax is `('simps' ident* e)`, where `e` is an expression of type `simps_cfg`.
* If one of the projections is marked as a coercion, the generated lemmas do *not* use this
coercion.
* `@[simps]` reduces let-expressions where necessary.
* If one of the fields is a partially applied constructor, we will eta-expand it
(this likely never happens).
* When option `trace.simps.verbose` is true, `simps` will print the projections it finds and the
lemmas it generates.
-/
@[user_attribute] meta def simps_attr : user_attribute unit (list string × simps_cfg) :=
{ name := `simps,
descr := "Automatically derive lemmas specifying the projections of this declaration.",
parser := simps_parser,
after_set := some $
λ n _ _, do (todo, cfg) ← simps_attr.get_param n, simps_tac n cfg todo }
add_tactic_doc
{ name := "simps",
category := doc_category.attr,
decl_names := [`simps_attr],
tags := ["simplification"] }
|
fbfedbe708ccbfc4b8849ce3236625c1155d3cb6 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/topology/instances/rat_lemmas.lean | b20d5f7e8d5a2fed42b0ef1a2e92286fa83a2a5a | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 3,388 | lean | /-
Copyright (c) 2022 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import topology.instances.irrational
import topology.instances.rat
import topology.alexandroff
/-!
# Additional lemmas about the topology on rational numbers
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
The structure of a metric space on `ℚ` (`rat.metric_space`) is introduced elsewhere, induced from
`ℝ`. In this file we prove some properties of this topological space and its one-point
compactification.
## Main statements
- `rat.totally_disconnected_space`: `ℚ` is a totally disconnected space;
- `rat.not_countably_generated_nhds_infty_alexandroff`: the filter of neighbourhoods of infinity in
`alexandroff ℚ` is not countably generated.
## Notation
- `ℚ∞` is used as a local notation for `alexandroff ℚ`
-/
open set metric filter topological_space
open_locale topology alexandroff
local notation `ℚ∞` := alexandroff ℚ
namespace rat
variables {p q : ℚ} {s t : set ℚ}
lemma interior_compact_eq_empty (hs : is_compact s) :
interior s = ∅ :=
dense_embedding_coe_real.to_dense_inducing.interior_compact_eq_empty dense_irrational hs
lemma dense_compl_compact (hs : is_compact s) : dense sᶜ :=
interior_eq_empty_iff_dense_compl.1 (interior_compact_eq_empty hs)
instance cocompact_inf_nhds_ne_bot : ne_bot (cocompact ℚ ⊓ 𝓝 p) :=
begin
refine (has_basis_cocompact.inf (nhds_basis_opens _)).ne_bot_iff.2 _,
rintro ⟨s, o⟩ ⟨hs, hpo, ho⟩, rw inter_comm,
exact (dense_compl_compact hs).inter_open_nonempty _ ho ⟨p, hpo⟩
end
lemma not_countably_generated_cocompact : ¬is_countably_generated (cocompact ℚ) :=
begin
introI H,
rcases exists_seq_tendsto (cocompact ℚ ⊓ 𝓝 0) with ⟨x, hx⟩,
rw tendsto_inf at hx, rcases hx with ⟨hxc, hx0⟩,
obtain ⟨n, hn⟩ : ∃ n : ℕ, x n ∉ insert (0 : ℚ) (range x),
from (hxc.eventually hx0.is_compact_insert_range.compl_mem_cocompact).exists,
exact hn (or.inr ⟨n, rfl⟩)
end
lemma not_countably_generated_nhds_infty_alexandroff :
¬is_countably_generated (𝓝 (∞ : ℚ∞)) :=
begin
introI,
have : is_countably_generated (comap (coe : ℚ → ℚ∞) (𝓝 ∞)), by apply_instance,
rw [alexandroff.comap_coe_nhds_infty, coclosed_compact_eq_cocompact] at this,
exact not_countably_generated_cocompact this
end
lemma not_first_countable_topology_alexandroff :
¬first_countable_topology ℚ∞ :=
by { introI, exact not_countably_generated_nhds_infty_alexandroff infer_instance }
lemma not_second_countable_topology_alexandroff :
¬second_countable_topology ℚ∞ :=
by { introI, exact not_first_countable_topology_alexandroff infer_instance }
instance : totally_disconnected_space ℚ :=
begin
refine ⟨λ s hsu hs x hx y hy, _⟩, clear hsu,
by_contra' H : x ≠ y,
wlog hlt : x < y,
{ exact this s hs y hy x hx H.symm (H.lt_or_lt.resolve_left hlt) },
rcases exists_irrational_btwn (rat.cast_lt.2 hlt) with ⟨z, hz, hxz, hzy⟩,
have := hs.image coe continuous_coe_real.continuous_on,
rw [is_preconnected_iff_ord_connected] at this,
have : z ∈ coe '' s := this.out (mem_image_of_mem _ hx) (mem_image_of_mem _ hy) ⟨hxz.le, hzy.le⟩,
exact hz (image_subset_range _ _ this)
end
end rat
|
c4db1fae70720e3c1595a865d2ec12f9b7b6e51d | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/data/option/defs.lean | 7cc5f348c3736577e8c0c8467841396f79fddcb7 | [
"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 | 4,899 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
Extra definitions on option.
-/
namespace option
variables {α : Type*} {β : Type*}
attribute [inline] option.is_some option.is_none
/-- An elimination principle for `option`. It is a nondependent version of `option.rec_on`. -/
@[simp] protected def elim : option α → β → (α → β) → β
| (some x) y f := f x
| none y f := y
instance has_mem : has_mem α (option α) := ⟨λ a b, b = some a⟩
@[simp] theorem mem_def {a : α} {b : option α} : a ∈ b ↔ b = some a :=
iff.rfl
theorem is_none_iff_eq_none {o : option α} : o.is_none = tt ↔ o = none :=
⟨option.eq_none_of_is_none, λ e, e.symm ▸ rfl⟩
theorem some_inj {a b : α} : some a = some b ↔ a = b := by simp
/--
`o = none` is decidable even if the wrapped type does not have decidable equality.
This is not an instance because it is not definitionally equal to `option.decidable_eq`.
Try to use `o.is_none` or `o.is_some` instead.
-/
@[inline]
def decidable_eq_none {o : option α} : decidable (o = none) :=
decidable_of_decidable_of_iff (bool.decidable_eq _ _) is_none_iff_eq_none
instance decidable_forall_mem {p : α → Prop} [decidable_pred p] :
∀ o : option α, decidable (∀ a ∈ o, p a)
| none := is_true (by simp)
| (some a) := if h : p a
then is_true $ λ o e, some_inj.1 e ▸ h
else is_false $ mt (λ H, H _ rfl) h
instance decidable_exists_mem {p : α → Prop} [decidable_pred p] :
∀ o : option α, decidable (∃ a ∈ o, p a)
| none := is_false (λ ⟨a, ⟨h, _⟩⟩, by cases h)
| (some a) := if h : p a
then is_true $ ⟨_, rfl, h⟩
else is_false $ λ ⟨_, ⟨rfl, hn⟩⟩, h hn
/-- inhabited `get` function. Returns `a` if the input is `some a`,
otherwise returns `default`. -/
@[reducible] def iget [inhabited α] : option α → α
| (some x) := x
| none := default α
@[simp] theorem iget_some [inhabited α] {a : α} : (some a).iget = a := rfl
/-- `guard p a` returns `some a` if `p a` holds, otherwise `none`. -/
def guard (p : α → Prop) [decidable_pred p] (a : α) : option α :=
if p a then some a else none
/-- `filter p o` returns `some a` if `o` is `some a`
and `p a` holds, otherwise `none`. -/
def filter (p : α → Prop) [decidable_pred p] (o : option α) : option α :=
o.bind (guard p)
def to_list : option α → list α
| none := []
| (some a) := [a]
@[simp] theorem mem_to_list {a : α} {o : option α} : a ∈ to_list o ↔ a ∈ o :=
by cases o; simp [to_list, eq_comm]
def lift_or_get (f : α → α → α) : option α → option α → option α
| none none := none
| (some a) none := some a -- get a
| none (some b) := some b -- get b
| (some a) (some b) := some (f a b) -- lift f
instance lift_or_get_comm (f : α → α → α) [h : is_commutative α f] :
is_commutative (option α) (lift_or_get f) :=
⟨λ a b, by cases a; cases b; simp [lift_or_get, h.comm]⟩
instance lift_or_get_assoc (f : α → α → α) [h : is_associative α f] :
is_associative (option α) (lift_or_get f) :=
⟨λ a b c, by cases a; cases b; cases c; simp [lift_or_get, h.assoc]⟩
instance lift_or_get_idem (f : α → α → α) [h : is_idempotent α f] :
is_idempotent (option α) (lift_or_get f) :=
⟨λ a, by cases a; simp [lift_or_get, h.idempotent]⟩
instance lift_or_get_is_left_id (f : α → α → α) :
is_left_id (option α) (lift_or_get f) none :=
⟨λ a, by cases a; simp [lift_or_get]⟩
instance lift_or_get_is_right_id (f : α → α → α) :
is_right_id (option α) (lift_or_get f) none :=
⟨λ a, by cases a; simp [lift_or_get]⟩
inductive rel (r : α → β → Prop) : option α → option β → Prop
| some {a b} : r a b → rel (some a) (some b)
| none : rel none none
/--
Flatten an `option` of `option`, a specialization of `mjoin`.
-/
@[simp] def join : option (option α) → option α :=
λ x, bind x id
protected def {u v} traverse {F : Type u → Type v} [applicative F] {α β : Type*} (f : α → F β) :
option α → F (option β)
| none := pure none
| (some x) := some <$> f x
/- By analogy with `monad.sequence` in `init/category/combinators.lean`. -/
/-- If you maybe have a monadic computation in a `[monad m]` which produces a term of type `α`, then
there is a naturally associated way to always perform a computation in `m` which maybe produces a
result. -/
def {u v} maybe {m : Type u → Type v} [monad m] {α : Type u} : option (m α) → m (option α)
| none := return none
| (some fn) := some <$> fn
/-- Map a monadic function `f : α → m β` over an `o : option α`, maybe producing a result. -/
def {u v w} mmap {m : Type u → Type v} [monad m] {α : Type w} {β : Type u} (f : α → m β)
(o : option α) : m (option β) := (o.map f).maybe
end option
|
1aa09743d94b10a0a405fbc3b936957f231c6ed9 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/algebraic_geometry/presheafed_space/has_colimits.lean | 0f8b2c04e610f8ed8c58de08912f9872f3c37944 | [] | 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 | 10,134 | 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.algebraic_geometry.presheafed_space
import Mathlib.topology.category.Top.limits
import Mathlib.topology.sheaves.limits
import Mathlib.category_theory.limits.concrete_category
import Mathlib.PostPort
universes v u
namespace Mathlib
/-!
# `PresheafedSpace C` has colimits.
If `C` has limits, then the category `PresheafedSpace C` has colimits,
and the forgetful functor to `Top` preserves these colimits.
When restricted to a diagram where the underlying continuous maps are open embeddings,
this says that we can glue presheaved spaces.
Given a diagram `F : J ⥤ PresheafedSpace C`,
we first build the colimit of the underlying topological spaces,
as `colimit (F ⋙ PresheafedSpace.forget C)`. Call that colimit space `X`.
Our strategy is to push each of the presheaves `F.obj j`
forward along the continuous map `colimit.ι (F ⋙ PresheafedSpace.forget C) j` to `X`.
Since pushforward is functorial, we obtain a diagram `J ⥤ (presheaf C X)ᵒᵖ`
of presheaves on a single space `X`.
(Note that the arrows now point the other direction,
because this is the way `PresheafedSpace C` is set up.)
The limit of this diagram then constitutes the colimit presheaf.
-/
namespace algebraic_geometry
namespace PresheafedSpace
@[simp] theorem map_id_c_app {J : Type v} [category_theory.small_category J] {C : Type u} [category_theory.category C] (F : J ⥤ PresheafedSpace C) (j : J) (U : topological_space.opens ↥(carrier (category_theory.functor.obj F j))) : category_theory.nat_trans.app (hom.c (category_theory.functor.map F 𝟙)) (opposite.op U) =
category_theory.nat_trans.app
(category_theory.iso.inv
(Top.presheaf.pushforward.id (PresheafedSpace.presheaf (category_theory.functor.obj F j))))
(opposite.op U) ≫
category_theory.nat_trans.app
(category_theory.iso.hom
(Top.presheaf.pushforward_eq
(eq.mpr
(id
((fun (a a_1 : carrier (category_theory.functor.obj F j) ⟶ carrier (category_theory.functor.obj F j))
(e_1 : a = a_1)
(ᾰ ᾰ_1 : carrier (category_theory.functor.obj F j) ⟶ carrier (category_theory.functor.obj F j))
(e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2)
𝟙 𝟙 (Eq.refl 𝟙) (hom.base (category_theory.functor.map F 𝟙)) 𝟙
(Eq.trans
((fun (c c_1 : hom (category_theory.functor.obj F j) (category_theory.functor.obj F j))
(e_1 : c = c_1) => congr_arg hom.base e_1)
(category_theory.functor.map F 𝟙) 𝟙 (category_theory.functor.map_id F j))
(id_base (category_theory.functor.obj F j)))))
(Eq.refl 𝟙))
(PresheafedSpace.presheaf (category_theory.functor.obj F j))))
(opposite.op U) := sorry
@[simp] theorem map_comp_c_app {J : Type v} [category_theory.small_category J] {C : Type u} [category_theory.category C] (F : J ⥤ PresheafedSpace C) {j₁ : J} {j₂ : J} {j₃ : J} (f : j₁ ⟶ j₂) (g : j₂ ⟶ j₃) (U : topological_space.opens ↥(carrier (category_theory.functor.obj F j₃))) : category_theory.nat_trans.app (hom.c (category_theory.functor.map F (f ≫ g))) (opposite.op U) =
category_theory.nat_trans.app (hom.c (category_theory.functor.map F g)) (opposite.op U) ≫
category_theory.nat_trans.app
(Top.presheaf.pushforward_map (hom.base (category_theory.functor.map F g))
(hom.c (category_theory.functor.map F f)))
(opposite.op U) ≫
category_theory.nat_trans.app
(category_theory.iso.inv
(Top.presheaf.pushforward.comp (PresheafedSpace.presheaf (category_theory.functor.obj F j₁))
(hom.base (category_theory.functor.map F f)) (hom.base (category_theory.functor.map F g))))
(opposite.op U) ≫
category_theory.nat_trans.app
(category_theory.iso.hom
(Top.presheaf.pushforward_eq
(eq.mpr
(id
(Eq._oldrec
(Eq.refl
(hom.base (category_theory.functor.map F f) ≫ hom.base (category_theory.functor.map F g) =
hom.base (category_theory.functor.map F (f ≫ g))))
(category_theory.functor.map_comp F f g)))
(Eq.refl (hom.base (category_theory.functor.map F f) ≫ hom.base (category_theory.functor.map F g))))
(PresheafedSpace.presheaf (category_theory.functor.obj F j₁))))
(opposite.op U) := sorry
/--
Given a diagram of presheafed spaces,
we can push all the presheaves forward to the colimit `X` of the underlying topological spaces,
obtaining a diagram in `(presheaf C X)ᵒᵖ`.
-/
@[simp] theorem pushforward_diagram_to_colimit_obj {J : Type v} [category_theory.small_category J] {C : Type u} [category_theory.category C] (F : J ⥤ PresheafedSpace C) (j : J) : category_theory.functor.obj (pushforward_diagram_to_colimit F) j =
opposite.op
(category_theory.limits.colimit.ι (F ⋙ forget C) j _* PresheafedSpace.presheaf (category_theory.functor.obj F j)) :=
Eq.refl (category_theory.functor.obj (pushforward_diagram_to_colimit F) j)
/--
Auxilliary definition for `PresheafedSpace.has_colimits`.
-/
def colimit {J : Type v} [category_theory.small_category J] {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] (F : J ⥤ PresheafedSpace C) : PresheafedSpace C :=
mk (category_theory.limits.colimit (F ⋙ forget C))
(category_theory.limits.limit (category_theory.functor.left_op (pushforward_diagram_to_colimit F)))
/--
Auxilliary definition for `PresheafedSpace.has_colimits`.
-/
@[simp] theorem colimit_cocone_X {J : Type v} [category_theory.small_category J] {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] (F : J ⥤ PresheafedSpace C) : category_theory.limits.cocone.X (colimit_cocone F) = colimit F :=
Eq.refl (category_theory.limits.cocone.X (colimit_cocone F))
namespace colimit_cocone_is_colimit
/--
Auxilliary definition for `PresheafedSpace.colimit_cocone_is_colimit`.
-/
def desc_c_app {J : Type v} [category_theory.small_category J] {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] (F : J ⥤ PresheafedSpace C) (s : category_theory.limits.cocone F) (U : topological_space.opens ↥(carrier (category_theory.limits.cocone.X s))ᵒᵖ) : category_theory.functor.obj (PresheafedSpace.presheaf (category_theory.limits.cocone.X s)) U ⟶
category_theory.functor.obj
(category_theory.limits.colimit.desc (F ⋙ forget C) (category_theory.functor.map_cocone (forget C) s) _*
category_theory.limits.limit (category_theory.functor.left_op (pushforward_diagram_to_colimit F)))
U := sorry
theorem desc_c_naturality {J : Type v} [category_theory.small_category J] {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] (F : J ⥤ PresheafedSpace C) (s : category_theory.limits.cocone F) {U : topological_space.opens ↥(carrier (category_theory.limits.cocone.X s))ᵒᵖ} {V : topological_space.opens ↥(carrier (category_theory.limits.cocone.X s))ᵒᵖ} (i : U ⟶ V) : category_theory.functor.map (PresheafedSpace.presheaf (category_theory.limits.cocone.X s)) i ≫ desc_c_app F s V =
desc_c_app F s U ≫
category_theory.functor.map
(category_theory.limits.colimit.desc (F ⋙ forget C) (category_theory.functor.map_cocone (forget C) s) _*
PresheafedSpace.presheaf (category_theory.limits.cocone.X (colimit_cocone F)))
i := sorry
end colimit_cocone_is_colimit
/--
Auxilliary definition for `PresheafedSpace.has_colimits`.
-/
def colimit_cocone_is_colimit {J : Type v} [category_theory.small_category J] {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] (F : J ⥤ PresheafedSpace C) : category_theory.limits.is_colimit (colimit_cocone F) :=
category_theory.limits.is_colimit.mk
fun (s : category_theory.limits.cocone F) =>
hom.mk (category_theory.limits.colimit.desc (F ⋙ forget C) (category_theory.functor.map_cocone (forget C) s))
(category_theory.nat_trans.mk
fun (U : topological_space.opens ↥(carrier (category_theory.limits.cocone.X s))ᵒᵖ) => sorry)
/--
When `C` has limits, the category of presheaved spaces with values in `C` itself has colimits.
-/
protected instance category_theory.limits.has_colimits {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] : category_theory.limits.has_colimits (PresheafedSpace C) :=
category_theory.limits.has_colimits.mk
fun (J : Type v) (𝒥 : category_theory.small_category J) =>
category_theory.limits.has_colimits_of_shape.mk
fun (F : J ⥤ PresheafedSpace C) =>
category_theory.limits.has_colimit.mk
(category_theory.limits.colimit_cocone.mk (colimit_cocone F) (colimit_cocone_is_colimit F))
/--
The underlying topological space of a colimit of presheaved spaces is
the colimit of the underlying topological spaces.
-/
protected instance forget_preserves_colimits {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] : category_theory.limits.preserves_colimits (forget C) :=
category_theory.limits.preserves_colimits.mk
fun (J : Type v) (𝒥 : category_theory.small_category J) =>
category_theory.limits.preserves_colimits_of_shape.mk
fun (F : J ⥤ PresheafedSpace C) =>
category_theory.limits.preserves_colimit_of_preserves_colimit_cocone (colimit_cocone_is_colimit F)
(category_theory.limits.is_colimit.of_iso_colimit (category_theory.limits.colimit.is_colimit (F ⋙ forget C))
(category_theory.limits.cocones.ext
(category_theory.iso.refl
(category_theory.limits.cocone.X (category_theory.limits.colimit.cocone (F ⋙ forget C))))
sorry))
|
a7bc6853bdf8f90512dcd95fb836b31c882a4db5 | fd3506535396cef3d1bdcf4ae5b87c8ed9ff2c2e | /group_theory/action.lean | 0dd68d60ab51c948610d4ad533521d81111f7c3b | [
"Apache-2.0"
] | permissive | williamdemeo/leanproved | 77933dbcb8bfbae61a753ae31fa669b3ed8cda9d | d8c2e2ca0002b252fce049c4ff9be0e9e83a6374 | refs/heads/master | 1,598,674,802,432 | 1,437,528,488,000 | 1,437,528,488,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 22,964 | lean | /-
Copyright (c) 2015 Haitao Zhang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author : Haitao Zhang
-/
import algebra.group data .hom .perm .finsubg
namespace group
open finset algebra function
local attribute perm.f [coercion]
private lemma and_left_true {a b : Prop} (Pa : a) : a ∧ b ↔ b :=
by rewrite [iff_true_intro Pa, true_and]
section def
variables {G S : Type} [ambientG : group G] [finS : fintype S] [deceqS : decidable_eq S]
include ambientG finS
definition is_fixed_point (hom : G → perm S) (H : finset G) (a : S) : Prop :=
∀ h, h ∈ H → hom h a = a
include deceqS
definition orbit (hom : G → perm S) (H : finset G) (a : S) : finset S :=
image (move_by a) (image hom H)
definition fixed_points [reducible] (hom : G → perm S) (H : finset G) : finset S :=
{a ∈ univ | orbit hom H a = singleton a}
variable [deceqG : decidable_eq G]
include deceqG -- required by {x ∈ H |p x} filtering
definition moverset (hom : G → perm S) (H : finset G) (a b : S) : finset G :=
{f ∈ H | hom f a = b}
definition stab (hom : G → perm S) (H : finset G) (a : S) : finset G :=
{f ∈ H | hom f a = a}
end def
section orbit_stabilizer
variables {G S : Type}
variable [ambientG : group G]
include ambientG
variable [finS : fintype S]
include finS
variable [deceqS : decidable_eq S]
include deceqS
section
variables {hom : G → perm S} {H : finset G} {a : S} [Hom : is_hom_class hom]
include Hom
lemma exists_of_orbit {b : S} : b ∈ orbit hom H a → ∃ h, h ∈ H ∧ hom h a = b :=
assume Pb,
obtain p (Pp₁ : p ∈ image hom H) (Pp₂ : move_by a p = b), from exists_of_mem_image Pb,
obtain h (Ph₁ : h ∈ H) (Ph₂ : hom h = p), from exists_of_mem_image Pp₁,
assert Phab : hom h a = b, from calc
hom h a = p a : Ph₂
... = b : Pp₂,
exists.intro h (and.intro Ph₁ Phab)
lemma orbit_of_exists {b : S} : (∃ h, h ∈ H ∧ hom h a = b) → b ∈ orbit hom H a :=
assume Pex, obtain h PinH Phab, from Pex,
mem_image_of_mem_of_eq (mem_image_of_mem hom PinH) Phab
lemma is_fixed_point_of_mem_fixed_points :
a ∈ fixed_points hom H → is_fixed_point hom H a :=
assume Pain, take h, assume Phin,
eq_of_mem_singleton
(of_mem_filter Pain ▸ orbit_of_exists (exists.intro h (and.intro Phin rfl)))
lemma mem_fixed_points_of_exists_of_is_fixed_point :
(∃ h, h ∈ H) → is_fixed_point hom H a → a ∈ fixed_points hom H :=
assume Pex Pfp, mem_filter_of_mem !mem_univ
(ext take x, iff.intro
(assume Porb, obtain h Phin Pha, from exists_of_orbit Porb,
by rewrite [mem_singleton_eq, -Pha, Pfp h Phin])
(obtain h Phin, from Pex,
by rewrite mem_singleton_eq;
intro Peq; rewrite Peq;
apply orbit_of_exists;
existsi h; apply and.intro Phin (Pfp h Phin)))
lemma is_fixed_point_iff_mem_fixed_points_of_exists :
(∃ h, h ∈ H) → (a ∈ fixed_points hom H ↔ is_fixed_point hom H a) :=
assume Pex, iff.intro is_fixed_point_of_mem_fixed_points (mem_fixed_points_of_exists_of_is_fixed_point Pex)
lemma is_fixed_point_iff_mem_fixed_points [finsubgH : is_finsubg H] :
a ∈ fixed_points hom H ↔ is_fixed_point hom H a :=
is_fixed_point_iff_mem_fixed_points_of_exists (exists.intro 1 !finsubg_has_one)
lemma is_fixed_point_of_one : is_fixed_point hom (singleton 1) a :=
take h, assume Ph, by rewrite [eq_of_mem_singleton Ph, hom_map_one]
lemma fixed_points_of_one : fixed_points hom (singleton 1) = univ :=
ext take s, iff.intro (assume Pl, mem_univ s)
(assume Pr, mem_fixed_points_of_exists_of_is_fixed_point
(exists.intro 1 !mem_singleton) is_fixed_point_of_one)
open fintype
lemma card_fixed_points_of_one : card (fixed_points hom (singleton 1)) = card S :=
by rewrite [fixed_points_of_one]
end
variable [deceqG : decidable_eq G]
include deceqG
-- these are already specified by stab hom H a
variables {hom : G → perm S} {H : finset G} {a : S}
variable [Hom : is_hom_class hom]
include Hom
lemma stab_lmul {f g : G} : g ∈ stab hom H a → hom (f*g) a = hom f a :=
assume Pgstab,
assert Pg : hom g a = a, from of_mem_filter Pgstab, calc
hom (f*g) a = perm.f ((hom f) * (hom g)) a : is_hom hom
... = ((hom f) ∘ (hom g)) a : rfl
... = (hom f) a : Pg
lemma stab_subset : stab hom H a ⊆ H :=
begin
apply subset_of_forall, intro f Pfstab, apply mem_of_mem_filter Pfstab
end
lemma reverse_move {h g : G} : g ∈ moverset hom H a (hom h a) → hom (h⁻¹*g) a = a :=
assume Pg,
assert Pga : hom g a = hom h a, from of_mem_filter Pg, calc
hom (h⁻¹*g) a = perm.f ((hom h⁻¹) * (hom g)) a : is_hom hom
... = ((hom h⁻¹) ∘ hom g) a : rfl
... = ((hom h⁻¹) ∘ hom h) a : {Pga}
... = perm.f ((hom h⁻¹) * hom h) a : rfl
... = perm.f ((hom h)⁻¹ * hom h) a : hom_map_inv hom h
... = (1 : perm S) a : mul.left_inv (hom h)
... = a : rfl
lemma moverset_inj_on_orbit : set.inj_on (moverset hom H a) (ts (orbit hom H a)) :=
take b1 b2,
assume Pb1, obtain h1 Ph1₁ Ph1₂, from exists_of_orbit Pb1,
assert Ph1b1 : h1 ∈ moverset hom H a b1,
from mem_filter_of_mem Ph1₁ Ph1₂,
assume Psetb2 Pmeq, begin
subst b1,
rewrite Pmeq at Ph1b1,
apply of_mem_filter Ph1b1
end
variable [subgH : is_finsubg H]
include subgH
lemma subg_stab_of_move {h g : G} :
h ∈ H → g ∈ moverset hom H a (hom h a) → h⁻¹*g ∈ stab hom H a :=
assume Ph Pg,
assert Phinvg : h⁻¹*g ∈ H, from begin
apply finsubg_mul_closed H,
apply finsubg_has_inv H, assumption,
apply mem_of_mem_filter Pg
end,
mem_filter_of_mem Phinvg (reverse_move Pg)
lemma subg_stab_closed : finset_mul_closed_on (stab hom H a) :=
take f g, assume Pfstab, assert Pf : hom f a = a, from of_mem_filter Pfstab,
assume Pgstab,
assert Pfg : hom (f*g) a = a, from calc
hom (f*g) a = (hom f) a : stab_lmul Pgstab
... = a : Pf,
assert PfginH : (f*g) ∈ H,
from finsubg_mul_closed H (mem_of_mem_filter Pfstab) (mem_of_mem_filter Pgstab),
mem_filter_of_mem PfginH Pfg
lemma subg_stab_has_one : 1 ∈ stab hom H a :=
assert P : hom 1 a = a, from calc
hom 1 a = (1 : perm S) a : {hom_map_one hom}
... = a : rfl,
assert PoneinH : 1 ∈ H, from finsubg_has_one H,
mem_filter_of_mem PoneinH P
lemma subg_stab_has_inv : finset_has_inv (stab hom H a) :=
take f, assume Pfstab, assert Pf : hom f a = a, from of_mem_filter Pfstab,
assert Pfinv : hom f⁻¹ a = a, from calc
hom f⁻¹ a = hom f⁻¹ ((hom f) a) : Pf
... = perm.f ((hom f⁻¹) * (hom f)) a : rfl
... = hom (f⁻¹ * f) a : is_hom hom
... = hom 1 a : mul.left_inv
... = (1 : perm S) a : hom_map_one hom,
assert PfinvinH : f⁻¹ ∈ H, from finsubg_has_inv H (mem_of_mem_filter Pfstab),
mem_filter_of_mem PfinvinH Pfinv
definition subg_stab_is_finsubg [instance] :
is_finsubg (stab hom H a) :=
is_finsubg.mk subg_stab_has_one subg_stab_closed subg_stab_has_inv
lemma subg_lcoset_eq_moverset {h : G} :
h ∈ H → fin_lcoset (stab hom H a) h = moverset hom H a (hom h a) :=
assume Ph, ext (take g, iff.intro
(assume Pl, obtain f (Pf₁ : f ∈ stab hom H a) (Pf₂ : h*f = g), from exists_of_mem_image Pl,
assert Pfstab : hom f a = a, from of_mem_filter Pf₁,
assert PginH : g ∈ H, begin
subst Pf₂,
apply finsubg_mul_closed H,
assumption,
apply mem_of_mem_filter Pf₁
end,
assert Pga : hom g a = hom h a, from calc
hom g a = hom (h*f) a : by subst g
... = hom h a : stab_lmul Pf₁,
mem_filter_of_mem PginH Pga)
(assume Pr, begin
rewrite [↑fin_lcoset, mem_image_iff],
existsi h⁻¹*g,
split,
exact subg_stab_of_move Ph Pr,
apply mul_inv_cancel_left
end))
lemma subg_moverset_of_orbit_is_lcoset_of_stab (b : S) :
b ∈ orbit hom H a → ∃ h, h ∈ H ∧ fin_lcoset (stab hom H a) h = moverset hom H a b :=
assume Porb,
obtain p (Pp₁ : p ∈ image hom H) (Pp₂ : move_by a p = b), from exists_of_mem_image Porb,
obtain h (Ph₁ : h ∈ H) (Ph₂ : hom h = p), from exists_of_mem_image Pp₁,
assert Phab : hom h a = b, from by subst p; assumption,
exists.intro h (and.intro Ph₁ (Phab ▸ subg_lcoset_eq_moverset Ph₁))
lemma subg_lcoset_of_stab_is_moverset_of_orbit (h : G) :
h ∈ H → ∃ b, b ∈ orbit hom H a ∧ moverset hom H a b = fin_lcoset (stab hom H a) h :=
assume Ph,
have Pha : (hom h a) ∈ orbit hom H a, by
apply mem_image_of_mem; apply mem_image_of_mem; exact Ph,
exists.intro (hom h a) (and.intro Pha (eq.symm (subg_lcoset_eq_moverset Ph)))
lemma subg_moversets_of_orbit_eq_stab_lcosets :
image (moverset hom H a) (orbit hom H a) = fin_lcosets (stab hom H a) H :=
ext (take s, iff.intro
(assume Pl, obtain b Pb₁ Pb₂, from exists_of_mem_image Pl,
obtain h Ph, from subg_moverset_of_orbit_is_lcoset_of_stab b Pb₁, begin
rewrite [↑fin_lcosets, mem_image_eq],
existsi h, subst Pb₂, assumption
end)
(assume Pr, obtain h Ph₁ Ph₂, from exists_of_mem_image Pr,
obtain b Pb, from @subg_lcoset_of_stab_is_moverset_of_orbit G S ambientG finS deceqS deceqG hom H a Hom subgH h Ph₁, begin
rewrite [mem_image_eq],
existsi b, subst Ph₂, assumption
end))
open nat
theorem orbit_stabilizer_theorem : card H = card (orbit hom H a) * card (stab hom H a) :=
calc card H = card (fin_lcosets (stab hom H a) H) * card (stab hom H a) : lagrange_theorem stab_subset
... = card (image (moverset hom H a) (orbit hom H a)) * card (stab hom H a) : subg_moversets_of_orbit_eq_stab_lcosets
... = card (orbit hom H a) * card (stab hom H a) : card_image_eq_of_inj_on moverset_inj_on_orbit
end orbit_stabilizer
section orbit_partition
variables {G S : Type} [ambientG : group G] [finS : fintype S]
variables [deceqS : decidable_eq S]
include ambientG finS deceqS
variables {hom : G → perm S} [Hom : is_hom_class hom] {H : finset G} [subgH : is_finsubg H]
include Hom subgH
lemma in_orbit_refl {a : S} : a ∈ orbit hom H a :=
mem_image_of_mem_of_eq (mem_image_of_mem_of_eq (finsubg_has_one H) (hom_map_one hom)) rfl
lemma in_orbit_trans {a b c : S} :
a ∈ orbit hom H b → b ∈ orbit hom H c → a ∈ orbit hom H c :=
assume Painb Pbinc,
obtain h PhinH Phba, from exists_of_orbit Painb,
obtain g PginH Pgcb, from exists_of_orbit Pbinc,
orbit_of_exists (exists.intro (h*g) (and.intro
(finsubg_mul_closed H PhinH PginH)
(calc hom (h*g) c = perm.f ((hom h) * (hom g)) c : is_hom hom
... = ((hom h) ∘ (hom g)) c : rfl
... = (hom h) b : Pgcb
... = a : Phba)))
lemma in_orbit_symm {a b : S} : a ∈ orbit hom H b → b ∈ orbit hom H a :=
assume Painb, obtain h PhinH Phba, from exists_of_orbit Painb,
assert Phinvab : perm.f (hom h)⁻¹ a = b, from
calc perm.f (hom h)⁻¹ a = perm.f (hom h)⁻¹ ((hom h) b) : Phba
... = perm.f ((hom h)⁻¹ * (hom h)) b : rfl
... = perm.f (1 : perm S) b : mul.left_inv,
orbit_of_exists (exists.intro h⁻¹ (and.intro
(finsubg_has_inv H PhinH)
(calc (hom h⁻¹) a = perm.f (hom h)⁻¹ a : hom_map_inv hom h
... = b : Phinvab)))
lemma orbit_is_partition : is_partition (orbit hom H) :=
take a b, propext (iff.intro
(assume Painb, obtain h PhinH Phba, from exists_of_orbit Painb,
ext take c, iff.intro
(assume Pcina, in_orbit_trans Pcina Painb)
(assume Pcinb, obtain g PginH Pgbc, from exists_of_orbit Pcinb,
in_orbit_trans Pcinb (in_orbit_symm Painb)))
(assume Peq, Peq ▸ in_orbit_refl))
variables (hom) (H)
open nat finset.partition fintype
definition orbit_partition : @partition S _ :=
mk univ (orbit hom H) orbit_is_partition
(restriction_imp_union (orbit hom H) orbit_is_partition (λ a Pa, !subset_univ))
definition orbits : finset (finset S) := equiv_classes (orbit_partition hom H)
definition fixed_point_orbits : finset (finset S) :=
{cls ∈ orbits hom H | card cls = 1}
variables {hom} {H}
lemma exists_iff_mem_orbits (orb : finset S) :
orb ∈ orbits hom H ↔ ∃ a : S, orbit hom H a = orb :=
begin
esimp [orbits, equiv_classes, orbit_partition],
rewrite [mem_image_iff],
apply iff.intro,
intro Pl,
cases Pl with a Pa,
rewrite (and_left_true !mem_univ) at Pa,
existsi a, exact Pa,
intro Pr,
cases Pr with a Pa,
rewrite -true_and at Pa, rewrite -(iff_true_intro (mem_univ a)) at Pa,
existsi a, exact Pa
end
lemma exists_of_mem_orbits {orb : finset S} :
orb ∈ orbits hom H → ∃ a : S, orbit hom H a = orb :=
iff.elim_left (exists_iff_mem_orbits orb)
lemma fixed_point_orbits_eq : fixed_point_orbits hom H = image (orbit hom H) (fixed_points hom H) :=
ext take s, iff.intro
(assume Pin,
obtain Psin Ps, from iff.elim_left !mem_filter_iff Pin,
obtain a Pa, from exists_of_mem_orbits Psin,
mem_image_of_mem_of_eq
(mem_filter_of_mem !mem_univ (eq.symm
(eq_of_card_eq_of_subset (by rewrite [card_singleton, Pa, Ps])
(subset_of_forall
take x, assume Pxin, eq_of_mem_singleton Pxin ▸ in_orbit_refl))))
Pa)
(assume Pin,
obtain a Pain Porba, from exists_of_mem_image Pin,
mem_filter_of_mem
(begin esimp [orbits, equiv_classes, orbit_partition], rewrite [mem_image_iff],
existsi a, exact and.intro !mem_univ Porba end)
(begin substvars, rewrite [of_mem_filter Pain] end))
lemma orbit_inj_on_fixed_points : set.inj_on (orbit hom H) (ts (fixed_points hom H)) :=
take a₁ a₂, begin
rewrite [-*mem_eq_mem_to_set, ↑fixed_points, *mem_filter_iff],
intro Pa₁ Pa₂,
rewrite [and.right Pa₁, and.right Pa₂],
exact eq_of_singleton_eq
end
lemma card_fixed_point_orbits_eq : card (fixed_point_orbits hom H) = card (fixed_points hom H) :=
by rewrite fixed_point_orbits_eq; apply card_image_eq_of_inj_on orbit_inj_on_fixed_points
lemma orbit_class_equation : card S = Sum (orbits hom H) card :=
class_equation (orbit_partition hom H)
lemma card_fixed_point_orbits : Sum (fixed_point_orbits hom H) card = card (fixed_point_orbits hom H) :=
calc Sum _ _ = Sum (fixed_point_orbits hom H) (λ x, 1) : Sum_ext (take c Pin, of_mem_filter Pin)
... = card (fixed_point_orbits hom H) * 1 : Sum_const_eq_card_mul
... = card (fixed_point_orbits hom H) : mul_one (card (fixed_point_orbits hom H))
local attribute nat.comm_semiring [instance]
lemma orbit_class_equation' : card S = card (fixed_points hom H) + Sum {cls ∈ orbits hom H | card cls ≠ 1} card :=
calc card S = Sum (orbits hom H) finset.card : orbit_class_equation
... = Sum (fixed_point_orbits hom H) finset.card + Sum {cls ∈ orbits hom H | card cls ≠ 1} card : Sum_binary_union
... = card (fixed_point_orbits hom H) + Sum {cls ∈ orbits hom H | card cls ≠ 1} card : {card_fixed_point_orbits}
... = card (fixed_points hom H) + Sum {cls ∈ orbits hom H | card cls ≠ 1} card : card_fixed_point_orbits_eq
end orbit_partition
section cayley
variables {G : Type}
variable [ambientG : group G]
include ambientG
variable [finG : fintype G]
include finG
definition action_by_lmul : G → perm G :=
take g, perm.mk (lmul_by g) (lmul_inj g)
variable [deceqG : decidable_eq G]
include deceqG
lemma action_by_lmul_hom : homomorphic (@action_by_lmul G _ _) :=
take g₁ (g₂ : G), eq.symm (calc
action_by_lmul g₁ * action_by_lmul g₂
= perm.mk ((lmul_by g₁)∘(lmul_by g₂)) _ : rfl
... = perm.mk (lmul_by (g₁*g₂)) _ : by congruence; apply coset.lmul_compose)
lemma action_by_lmul_inj : injective (@action_by_lmul G _ _) :=
take g₁ g₂, assume Peq, perm.no_confusion Peq
(λ Pfeq Pqeq,
have Pappeq : g₁*1 = g₂*1, from congr_fun Pfeq _,
calc g₁ = g₁ * 1 : mul_one
... = g₂ * 1 : Pappeq
... = g₂ : mul_one)
definition action_by_lmul_is_iso [instance] : is_iso_class (@action_by_lmul G _ _) :=
is_iso_class.mk action_by_lmul_hom action_by_lmul_inj
end cayley
section lcosets
open fintype subtype
variables {G : Type} [ambientG : group G] [finG : fintype G] [deceqG : decidable_eq G]
include ambientG deceqG finG
variables H : finset G
definition action_on_lcoset : G → perm (lcoset_type univ H) :=
take g, perm.mk (lcoset_lmul (mem_univ g)) lcoset_lmul_inj
private definition lcoset_of (g : G) : lcoset_type univ H :=
tag (fin_lcoset H g) (exists.intro g (and.intro !mem_univ rfl))
variable {H}
lemma action_on_lcoset_eq (g : G) (J : lcoset_type univ H)
: elt_of (action_on_lcoset H g J) = fin_lcoset (elt_of J) g := rfl
lemma action_on_lcoset_hom : homomorphic (action_on_lcoset H) :=
take g₁ g₂, eq_of_feq (funext take S, subtype.eq
(by rewrite [↑action_on_lcoset, ↑lcoset_lmul, -fin_lcoset_compose]))
definition action_on_lcoset_is_hom [instance] : is_hom_class (action_on_lcoset H) :=
is_hom_class.mk action_on_lcoset_hom
variable [finsubgH : is_finsubg H]
include finsubgH
lemma aol_fixed_point_subset_normalizer (J : lcoset_type univ H) :
is_fixed_point (action_on_lcoset H) H J → elt_of J ⊆ normalizer H :=
obtain j Pjin Pj, from exists_of_lcoset_type J,
assume Pfp,
assert PH : ∀ {h}, h ∈ H → fin_lcoset (fin_lcoset H j) h = fin_lcoset H j,
from take h, assume Ph, by rewrite [Pj, -action_on_lcoset_eq, Pfp h Ph],
subset_of_forall take g, begin
rewrite [-Pj, fin_lcoset_same, -inv_inv at {2}],
intro Pg,
rewrite -Pg at PH,
apply finsubg_has_inv,
apply mem_filter_of_mem !mem_univ,
intro h Ph,
assert Phg : fin_lcoset (fin_lcoset H g) h = fin_lcoset H g, exact PH Ph,
revert Phg,
rewrite [↑conj_by, inv_inv, mul.assoc, fin_lcoset_compose, -fin_lcoset_same, ↑fin_lcoset, mem_image_iff, ↑lmul_by],
intro Pex, cases Pex with k Pand, cases Pand with Pkin Pk,
rewrite [-Pk, inv_mul_cancel_left], exact Pkin
end
lemma aol_fixed_point_of_mem_normalizer {g : G} :
g ∈ normalizer H → is_fixed_point (action_on_lcoset H) H (lcoset_of H g) :=
assume Pgin, take h, assume Phin, subtype.eq
(by rewrite [action_on_lcoset_eq, ↑lcoset_of, lrcoset_same_of_mem_normalizer Pgin, fin_lrcoset_comm, finsubg_lcoset_id Phin])
lemma aol_fixed_points_eq_normalizer :
Union (fixed_points (action_on_lcoset H) H) elt_of = normalizer H :=
ext take g, begin
rewrite [mem_Union_iff],
apply iff.intro,
intro Pl,
cases Pl with L PL, revert PL,
rewrite [is_fixed_point_iff_mem_fixed_points],
intro Pg,
apply mem_of_subset_of_mem,
apply aol_fixed_point_subset_normalizer L, exact and.left Pg,
exact and.right Pg,
intro Pr,
existsi (lcoset_of H g), apply and.intro,
rewrite [is_fixed_point_iff_mem_fixed_points],
exact aol_fixed_point_of_mem_normalizer Pr,
exact fin_mem_lcoset g
end
open nat
lemma card_aol_fixed_points_eq_card_cosets :
card (fixed_points (action_on_lcoset H) H) = card (lcoset_type (normalizer H) H) :=
have Peq : card (fixed_points (action_on_lcoset H) H) * card H = card (lcoset_type (normalizer H) H) * card H, from calc
card _ * card H = card (Union (fixed_points (action_on_lcoset H) H) elt_of) : card_Union_lcosets
... = card (normalizer H) : aol_fixed_points_eq_normalizer
... = card (lcoset_type (normalizer H) H) * card H : lagrange_theorem' subset_normalizer,
eq_of_mul_eq_mul_right (card_pos_of_mem !finsubg_has_one) Peq
end lcosets
section perm_fin
open fin nat eq.ops
variable {n : nat}
definition lift_perm (p : perm (fin n)) : perm (fin (succ n)) :=
perm.mk (lift_fun p) (lift_fun_of_inj (perm.inj p))
definition lower_perm (p : perm (fin (succ n))) (P : p maxi = maxi) : perm (fin n) :=
perm.mk (lower_inj p (perm.inj p) P)
(take i j, begin
rewrite [-eq_iff_veq, *lower_inj_apply, eq_iff_veq],
apply injective_compose (perm.inj p) lift_succ_inj
end)
lemma lift_lower_eq : ∀ {p : perm (fin (succ n))} (P : p maxi = maxi),
lift_perm (lower_perm p P) = p
| (perm.mk pf Pinj) := assume Pmax, begin
rewrite [↑lift_perm], congruence,
apply funext, intro i,
assert Pfmax : pf maxi = maxi, apply Pmax,
assert Pd : decidable (i = maxi),
exact _,
cases Pd with Pe Pne,
rewrite [Pe, Pfmax], apply lift_fun_max,
rewrite [lift_fun_of_ne_max Pne, ↑lower_perm, ↑lift_succ],
rewrite [-eq_iff_veq, -val_lift, lower_inj_apply, eq_iff_veq],
congruence, rewrite [-eq_iff_veq]
end
lemma lift_perm_inj : injective (@lift_perm n) :=
take p1 p2, assume Peq, eq_of_feq (lift_fun_inj (feq_of_eq Peq))
lemma lift_perm_inj_on_univ : set.inj_on (@lift_perm n) (ts univ) :=
eq.symm to_set_univ ▸ iff.elim_left set.injective_iff_inj_on_univ lift_perm_inj
lemma lift_to_stab : image (@lift_perm n) univ = stab id univ maxi :=
ext (take (pp : perm (fin (succ n))), iff.intro
(assume Pimg, obtain p P_ Pp, from exists_of_mem_image Pimg,
assert Ppp : pp maxi = maxi, from calc
pp maxi = lift_perm p maxi : {eq.symm Pp}
... = lift_fun p maxi : rfl
... = maxi : lift_fun_max,
mem_filter_of_mem !mem_univ Ppp)
(assume Pstab,
assert Ppp : pp maxi = maxi, from of_mem_filter Pstab,
mem_image_of_mem_of_eq !mem_univ (lift_lower_eq Ppp)))
definition move_from_max_to (i : fin (succ n)) : perm (fin (succ n)) :=
perm.mk (madd (i - maxi)) madd_inj
lemma orbit_max : orbit (@id (perm (fin (succ n)))) univ maxi = univ :=
ext (take i, iff.intro
(assume P, !mem_univ)
(assume P, begin
apply mem_image_of_mem_of_eq,
apply mem_image_of_mem_of_eq,
apply mem_univ (move_from_max_to i), apply rfl,
apply sub_add_cancel
end))
lemma card_orbit_max : card (orbit (@id (perm (fin (succ n)))) univ maxi) = succ n :=
calc card (orbit (@id (perm (fin (succ n)))) univ maxi) = card univ : {orbit_max}
... = succ n : card_fin (succ n)
open fintype
lemma card_lift_to_stab : card (stab (@id (perm (fin (succ n)))) univ maxi) = card (perm (fin n)) :=
calc finset.card (stab (@id (perm (fin (succ n)))) univ maxi)
= finset.card (image (@lift_perm n) univ) : {eq.symm lift_to_stab}
... = card univ : card_image_eq_of_inj_on lift_perm_inj_on_univ
lemma card_perm_step : card (perm (fin (succ n))) = (succ n) * card (perm (fin n)) :=
calc card (perm (fin (succ n)))
= card (orbit id univ maxi) * card (stab id univ maxi) : orbit_stabilizer_theorem
... = (succ n) * card (stab id univ maxi) : {card_orbit_max}
... = (succ n) * card (perm (fin n)) : {card_lift_to_stab}
end perm_fin
end group
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.