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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
6c865b3dc00426d8a6ed150389e6bf6cbc170770 | 947b78d97130d56365ae2ec264df196ce769371a | /tests/lean/run/getline_crash.lean | 454899b9955869078289e4ba9724fba1b64821fc | [
"Apache-2.0"
] | permissive | shyamalschandra/lean4 | 27044812be8698f0c79147615b1d5090b9f4b037 | 6e7a883b21eaf62831e8111b251dc9b18f40e604 | refs/heads/master | 1,671,417,126,371 | 1,601,859,995,000 | 1,601,860,020,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,277 | lean | new_frontend
def tstGetLine (str : String) : IO Unit := do
let path := "tmp_file";
IO.FS.withFile path IO.FS.Mode.write $ λ (h : IO.FS.Handle) =>
h.putStrLn str;
IO.FS.withFile path IO.FS.Mode.read $ λ (h : IO.FS.Handle) => do
let str' ← h.getLine;
IO.println str.length;
IO.println str'.length;
IO.print str';
«unless» (str'.length == str.length + 1) $
throw (IO.userError ("unexpected length: " ++ toString str'.trim.length));
«unless» (str'.trim == str) $
throw (IO.userError ("unexpected result: " ++ str'))
def tstGetLine2 (str1 str2 : String) : IO Unit := do
let path := "tmp_file";
IO.FS.withFile path IO.FS.Mode.write $ λ (h : IO.FS.Handle) => do {
h.putStrLn str1; h.putStr str2
};
IO.FS.withFile path IO.FS.Mode.read $ λ (h : IO.FS.Handle) => do
let str1' ← h.getLine;
let str2' ← h.getLine;
«unless» (str1'.length == str1.length + 1) $
throw (IO.userError ("unexpected length: " ++ toString str1'.trim.length));
«unless» (str1'.trim == str1) $
throw (IO.userError ("unexpected result: " ++ str1'));
«unless» (str2'.length == str2.length) $
throw (IO.userError ("unexpected length: " ++ toString str2'.trim.length));
«unless» (str2'.trim == str2) $
throw (IO.userError ("unexpected result: " ++ str2'))
def tstGetLine3 (str : String) : IO Unit := do
let path := "tmp_file";
IO.FS.withFile path IO.FS.Mode.write $ λ (h : IO.FS.Handle) => do {
h.putStrLn str
};
IO.FS.withFile path IO.FS.Mode.read $ λ (h : IO.FS.Handle) => do
(h.getLine >>= IO.println);
(h.getLine >>= IO.println);
(h.getLine >>= IO.println);
IO.print "done";
pure ()
#eval tstGetLine3 "abc"
#eval tstGetLine ("".pushn 'α' 40)
#eval tstGetLine "a"
#eval tstGetLine ""
#eval tstGetLine ("".pushn 'α' 20)
#eval tstGetLine ("".pushn 'a' 61)
#eval tstGetLine ("".pushn 'α' 61)
#eval tstGetLine ("".pushn 'a' 62)
#eval tstGetLine ("".pushn 'a' 63)
#eval tstGetLine ("".pushn 'a' 64)
#eval tstGetLine ("".pushn 'a' 65)
#eval tstGetLine ("".pushn 'a' 66)
#eval tstGetLine ("".pushn 'a' 128)
#eval tstGetLine2 ("".pushn 'α' 20) ("".pushn 'β' 20)
#eval tstGetLine2 ("".pushn 'α' 40) ("".pushn 'β' 40)
#eval tstGetLine2 ("".pushn 'a' 61) ("".pushn 'b' 61)
#eval tstGetLine2 ("".pushn 'a' 61) ("".pushn 'b' 62)
|
44c5075001bc3dca73b98846a835bf0ba0521733 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/data/list/range.lean | 643e56218e07c426202f96737646c2f54042c243 | [
"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 | 10,536 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Kenny Lau, Scott Morrison
-/
import data.list.chain
import data.list.nodup
import data.list.of_fn
import data.list.zip
/-!
# Ranges of naturals as lists
This file shows basic results about `list.iota`, `list.range`, `list.range'` (all defined in
`data.list.defs`) and defines `list.fin_range`.
`fin_range n` is the list of elements of `fin n`.
`iota n = [1, ..., n]` and `range n = [0, ..., n - 1]` are basic list constructions used for
tactics. `range' a b = [a, ..., a + b - 1]` is there to help prove properties about them.
Actual maths should use `list.Ico` instead.
-/
universe u
open nat
namespace list
variables {α : Type u}
@[simp] theorem length_range' : ∀ (s n : ℕ), length (range' s n) = n
| s 0 := rfl
| s (n+1) := congr_arg succ (length_range' _ _)
@[simp] theorem range'_eq_nil {s n : ℕ} : range' s n = [] ↔ n = 0 :=
by rw [← length_eq_zero, length_range']
@[simp] theorem mem_range' {m : ℕ} : ∀ {s n : ℕ}, m ∈ range' s n ↔ s ≤ m ∧ m < s + n
| s 0 := (false_iff _).2 $ λ ⟨H1, H2⟩, not_le_of_lt H2 H1
| s (succ n) :=
have m = s → m < s + n + 1,
from λ e, e ▸ lt_succ_of_le (le_add_right _ _),
have l : m = s ∨ s + 1 ≤ m ↔ s ≤ m,
by simpa only [eq_comm] using (@decidable.le_iff_eq_or_lt _ _ _ s m).symm,
(mem_cons_iff _ _ _).trans $ by simp only [mem_range',
or_and_distrib_left, or_iff_right_of_imp this, l, add_right_comm]; refl
theorem map_add_range' (a) : ∀ s n : ℕ, map ((+) a) (range' s n) = range' (a + s) n
| s 0 := rfl
| s (n+1) := congr_arg (cons _) (map_add_range' (s+1) n)
theorem map_sub_range' (a) :
∀ (s n : ℕ) (h : a ≤ s), map (λ x, x - a) (range' s n) = range' (s - a) n
| s 0 _ := rfl
| s (n+1) h :=
begin
convert congr_arg (cons (s-a)) (map_sub_range' (s+1) n (nat.le_succ_of_le h)),
rw nat.succ_sub h,
refl,
end
theorem chain_succ_range' : ∀ s n : ℕ, chain (λ a b, b = succ a) s (range' (s+1) n)
| s 0 := chain.nil
| s (n+1) := (chain_succ_range' (s+1) n).cons rfl
theorem chain_lt_range' (s n : ℕ) : chain (<) s (range' (s+1) n) :=
(chain_succ_range' s n).imp (λ a b e, e.symm ▸ lt_succ_self _)
theorem pairwise_lt_range' : ∀ s n : ℕ, pairwise (<) (range' s n)
| s 0 := pairwise.nil
| s (n+1) := (chain_iff_pairwise (by exact λ a b c, lt_trans)).1 (chain_lt_range' s n)
theorem nodup_range' (s n : ℕ) : nodup (range' s n) :=
(pairwise_lt_range' s n).imp (λ a b, ne_of_lt)
@[simp] theorem range'_append : ∀ s m n : ℕ, range' s m ++ range' (s+m) n = range' s (n+m)
| s 0 n := rfl
| s (m+1) n := show s :: (range' (s+1) m ++ range' (s+m+1) n) = s :: range' (s+1) (n+m),
by rw [add_right_comm, range'_append]
theorem range'_sublist_right {s m n : ℕ} : range' s m <+ range' s n ↔ m ≤ n :=
⟨λ h, by simpa only [length_range'] using length_le_of_sublist h,
λ h, by rw [← nat.sub_add_cancel h, ← range'_append]; apply sublist_append_left⟩
theorem range'_subset_right {s m n : ℕ} : range' s m ⊆ range' s n ↔ m ≤ n :=
⟨λ h, le_of_not_lt $ λ hn, lt_irrefl (s+n) $
(mem_range'.1 $ h $ mem_range'.2 ⟨le_add_right _ _, nat.add_lt_add_left hn s⟩).2,
λ h, (range'_sublist_right.2 h).subset⟩
theorem nth_range' : ∀ s {m n : ℕ}, m < n → nth (range' s n) m = some (s + m)
| s 0 (n+1) _ := rfl
| s (m+1) (n+1) h := (nth_range' (s+1) (lt_of_add_lt_add_right h)).trans $
by rw add_right_comm; refl
@[simp] lemma nth_le_range' {n m} (i) (H : i < (range' n m).length) :
nth_le (range' n m) i H = n + i :=
option.some.inj $ by rw [←nth_le_nth _, nth_range' _ (by simpa using H)]
theorem range'_concat (s n : ℕ) : range' s (n + 1) = range' s n ++ [s+n] :=
by rw add_comm n 1; exact (range'_append s n 1).symm
theorem range_core_range' : ∀ s n : ℕ, range_core s (range' s n) = range' 0 (n + s)
| 0 n := rfl
| (s+1) n := by rw [show n+(s+1) = n+1+s, from add_right_comm n s 1];
exact range_core_range' s (n+1)
theorem range_eq_range' (n : ℕ) : range n = range' 0 n :=
(range_core_range' n 0).trans $ by rw zero_add
theorem range_succ_eq_map (n : ℕ) : range (n + 1) = 0 :: map succ (range n) :=
by rw [range_eq_range', range_eq_range', range',
add_comm, ← map_add_range'];
congr; exact funext one_add
theorem range'_eq_map_range (s n : ℕ) : range' s n = map ((+) s) (range n) :=
by rw [range_eq_range', map_add_range']; refl
@[simp] theorem length_range (n : ℕ) : length (range n) = n :=
by simp only [range_eq_range', length_range']
@[simp] theorem range_eq_nil {n : ℕ} : range n = [] ↔ n = 0 :=
by rw [← length_eq_zero, length_range]
theorem pairwise_lt_range (n : ℕ) : pairwise (<) (range n) :=
by simp only [range_eq_range', pairwise_lt_range']
theorem nodup_range (n : ℕ) : nodup (range n) :=
by simp only [range_eq_range', nodup_range']
theorem range_sublist {m n : ℕ} : range m <+ range n ↔ m ≤ n :=
by simp only [range_eq_range', range'_sublist_right]
theorem range_subset {m n : ℕ} : range m ⊆ range n ↔ m ≤ n :=
by simp only [range_eq_range', range'_subset_right]
@[simp] theorem mem_range {m n : ℕ} : m ∈ range n ↔ m < n :=
by simp only [range_eq_range', mem_range', nat.zero_le, true_and, zero_add]
@[simp] theorem not_mem_range_self {n : ℕ} : n ∉ range n :=
mt mem_range.1 $ lt_irrefl _
@[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) :=
by simp only [succ_pos', lt_add_iff_pos_right, mem_range]
theorem nth_range {m n : ℕ} (h : m < n) : nth (range n) m = some m :=
by simp only [range_eq_range', nth_range' _ h, zero_add]
theorem range_succ (n : ℕ) : range (succ n) = range n ++ [n] :=
by simp only [range_eq_range', range'_concat, zero_add]
@[simp] lemma range_zero : range 0 = [] := rfl
theorem iota_eq_reverse_range' : ∀ n : ℕ, iota n = reverse (range' 1 n)
| 0 := rfl
| (n+1) := by simp only [iota, range'_concat, iota_eq_reverse_range' n,
reverse_append, add_comm]; refl
@[simp] theorem length_iota (n : ℕ) : length (iota n) = n :=
by simp only [iota_eq_reverse_range', length_reverse, length_range']
theorem pairwise_gt_iota (n : ℕ) : pairwise (>) (iota n) :=
by simp only [iota_eq_reverse_range', pairwise_reverse, pairwise_lt_range']
theorem nodup_iota (n : ℕ) : nodup (iota n) :=
by simp only [iota_eq_reverse_range', nodup_reverse, nodup_range']
theorem mem_iota {m n : ℕ} : m ∈ iota n ↔ 1 ≤ m ∧ m ≤ n :=
by simp only [iota_eq_reverse_range', mem_reverse, mem_range', add_comm, lt_succ_iff]
theorem reverse_range' : ∀ s n : ℕ,
reverse (range' s n) = map (λ i, s + n - 1 - i) (range n)
| s 0 := rfl
| s (n+1) := by rw [range'_concat, reverse_append, range_succ_eq_map];
simpa only [show s + (n + 1) - 1 = s + n, from rfl, (∘),
λ a i, show a - 1 - i = a - succ i, from pred_sub _ _,
reverse_singleton, map_cons, nat.sub_zero, cons_append,
nil_append, eq_self_iff_true, true_and, map_map]
using reverse_range' s n
/-- All elements of `fin n`, from `0` to `n-1`. -/
def fin_range (n : ℕ) : list (fin n) :=
(range n).pmap fin.mk (λ _, list.mem_range.1)
@[simp] lemma fin_range_zero : fin_range 0 = [] := rfl
@[simp] lemma mem_fin_range {n : ℕ} (a : fin n) : a ∈ fin_range n :=
mem_pmap.2 ⟨a.1, mem_range.2 a.2, fin.eta _ _⟩
lemma nodup_fin_range (n : ℕ) : (fin_range n).nodup :=
nodup_pmap (λ _ _ _ _, fin.veq_of_eq) (nodup_range _)
@[simp] lemma length_fin_range (n : ℕ) : (fin_range n).length = n :=
by rw [fin_range, length_pmap, length_range]
@[simp] lemma fin_range_eq_nil {n : ℕ} : fin_range n = [] ↔ n = 0 :=
by rw [← length_eq_zero, length_fin_range]
@[to_additive]
theorem prod_range_succ {α : Type u} [monoid α] (f : ℕ → α) (n : ℕ) :
((range n.succ).map f).prod = ((range n).map f).prod * f n :=
by rw [range_succ, map_append, map_singleton,
prod_append, prod_cons, prod_nil, mul_one]
/-- A variant of `prod_range_succ` which pulls off the first
term in the product rather than the last.-/
@[to_additive "A variant of `sum_range_succ` which pulls off the first term in the sum
rather than the last."]
theorem prod_range_succ' {α : Type u} [monoid α] (f : ℕ → α) (n : ℕ) :
((range n.succ).map f).prod = f 0 * ((range n).map (λ i, f (succ i))).prod :=
nat.rec_on n
(show 1 * f 0 = f 0 * 1, by rw [one_mul, mul_one])
(λ _ hd, by rw [list.prod_range_succ, hd, mul_assoc, ←list.prod_range_succ])
@[simp] theorem enum_from_map_fst : ∀ n (l : list α),
map prod.fst (enum_from n l) = range' n l.length
| n [] := rfl
| n (a :: l) := congr_arg (cons _) (enum_from_map_fst _ _)
@[simp] theorem enum_map_fst (l : list α) :
map prod.fst (enum l) = range l.length :=
by simp only [enum, enum_from_map_fst, range_eq_range']
lemma enum_eq_zip_range (l : list α) :
l.enum = (range l.length).zip l :=
zip_of_prod (enum_map_fst _) (enum_map_snd _)
@[simp] lemma unzip_enum_eq_prod (l : list α) :
l.enum.unzip = (range l.length, l) :=
by simp only [enum_eq_zip_range, unzip_zip, length_range]
lemma enum_from_eq_zip_range' (l : list α) {n : ℕ} :
l.enum_from n = (range' n l.length).zip l :=
zip_of_prod (enum_from_map_fst _ _) (enum_from_map_snd _ _)
@[simp] lemma unzip_enum_from_eq_prod (l : list α) {n : ℕ} :
(l.enum_from n).unzip = (range' n l.length, l) :=
by simp only [enum_from_eq_zip_range', unzip_zip, length_range']
@[simp] lemma nth_le_range {n} (i) (H : i < (range n).length) :
nth_le (range n) i H = i :=
option.some.inj $ by rw [← nth_le_nth _, nth_range (by simpa using H)]
@[simp] lemma nth_le_fin_range {n : ℕ} {i : ℕ} (h) :
(fin_range n).nth_le i h = ⟨i, length_fin_range n ▸ h⟩ :=
by simp only [fin_range, nth_le_range, nth_le_pmap, fin.mk_eq_subtype_mk]
theorem of_fn_eq_pmap {α n} {f : fin n → α} :
of_fn f = pmap (λ i hi, f ⟨i, hi⟩) (range n) (λ _, mem_range.1) :=
by rw [pmap_eq_map_attach]; from ext_le (by simp)
(λ i hi1 hi2, by { simp at hi1, simp [nth_le_of_fn f ⟨i, hi1⟩, -subtype.val_eq_coe] })
theorem of_fn_id (n) : of_fn id = fin_range n := of_fn_eq_pmap
theorem of_fn_eq_map {α n} {f : fin n → α} :
of_fn f = (fin_range n).map f :=
by rw [← of_fn_id, map_of_fn, function.right_id]
theorem nodup_of_fn {α n} {f : fin n → α} (hf : function.injective f) :
nodup (of_fn f) :=
by rw of_fn_eq_pmap; from nodup_pmap
(λ _ _ _ _ H, fin.veq_of_eq $ hf H) (nodup_range n)
end list
|
fb3b3abea9ec24628a73e3551705cc9cbdbe71c2 | 4fa161becb8ce7378a709f5992a594764699e268 | /src/algebra/pointwise.lean | 103fb681d190e37d5f6d5d1edca2ec94256fc0e7 | [
"Apache-2.0"
] | permissive | laughinggas/mathlib | e4aa4565ae34e46e834434284cb26bd9d67bc373 | 86dcd5cda7a5017c8b3c8876c89a510a19d49aad | refs/heads/master | 1,669,496,232,688 | 1,592,831,995,000 | 1,592,831,995,000 | 274,155,979 | 0 | 0 | Apache-2.0 | 1,592,835,190,000 | 1,592,835,189,000 | null | UTF-8 | Lean | false | false | 12,340 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import algebra.module
/-!
# Pointwise addition, multiplication, and scalar multiplication of sets.
This file defines `pointwise_mul`: for a type `α` with multiplication,
multiplication is defined on `set α` by taking `s * t` to be the set
of all `x * y` where `x ∈ s` and `y ∈ t`.
Pointwise multiplication on `set α` where `α` is a semigroup makes
`set α` into a semigroup. If `α` is additionally a (commutative)
monoid, `set α` becomes a (commutative) semiring with union as
addition. These are given by `pointwise_mul_semigroup` and
`pointwise_mul_semiring`.
Definitions and results are also transported to the additive theory
via `to_additive`.
For a type `β` with scalar multiplication by another type `α`, this
file defines `pointwise_smul`. Separately it defines `smul_set`, for
scalar multiplication of `set β` by a single term of type `α`.
## Implementation notes
Elsewhere, one should register local instances to use the definitions
in this file.
-/
namespace set
open function
variables {α : Type*} {β : Type*} (f : α → β)
@[to_additive]
def pointwise_one [has_one α] : has_one (set α) := ⟨{1}⟩
local attribute [instance] pointwise_one
@[simp, to_additive]
lemma mem_pointwise_one [has_one α] (a : α) :
a ∈ (1 : set α) ↔ a = 1 :=
mem_singleton_iff
@[to_additive]
def pointwise_mul [has_mul α] : has_mul (set α) :=
⟨λ s t, {a | ∃ x ∈ s, ∃ y ∈ t, a = x * y}⟩
local attribute [instance] pointwise_one pointwise_mul pointwise_add
@[to_additive]
lemma mem_pointwise_mul [has_mul α] {s t : set α} {a : α} :
a ∈ s * t ↔ ∃ x ∈ s, ∃ y ∈ t, a = x * y := iff.rfl
@[to_additive]
lemma mul_mem_pointwise_mul [has_mul α] {s t : set α} {a b : α} (ha : a ∈ s) (hb : b ∈ t) :
a * b ∈ s * t := ⟨_, ha, _, hb, rfl⟩
@[to_additive]
lemma pointwise_mul_eq_image [has_mul α] {s t : set α} :
s * t = (λ x : α × α, x.fst * x.snd) '' s.prod t :=
set.ext $ λ a,
⟨ by { rintros ⟨_, _, _, _, rfl⟩, exact ⟨(_, _), mem_prod.mpr ⟨‹_›, ‹_›⟩, rfl⟩ },
by { rintros ⟨_, _, rfl⟩, exact ⟨_, (mem_prod.mp ‹_›).1, _, (mem_prod.mp ‹_›).2, rfl⟩ }⟩
@[to_additive]
lemma pointwise_mul_finite [has_mul α] {s t : set α} (hs : finite s) (ht : finite t) :
finite (s * t) :=
by { rw pointwise_mul_eq_image, apply set.finite_image, exact set.finite_prod hs ht }
@[to_additive pointwise_add_add_semigroup]
def pointwise_mul_semigroup [semigroup α] : semigroup (set α) :=
{ mul_assoc := λ _ _ _, set.ext $ λ _,
begin
split,
{ rintros ⟨_, ⟨_, _, _, _, rfl⟩, _, _, rfl⟩,
exact ⟨_, ‹_›, _, ⟨_, ‹_›, _, ‹_›, rfl⟩, mul_assoc _ _ _⟩ },
{ rintros ⟨_, _, _, ⟨_, _, _, _, rfl⟩, rfl⟩,
exact ⟨_, ⟨_, ‹_›, _, ‹_›, rfl⟩, _, ‹_›, (mul_assoc _ _ _).symm⟩ }
end,
..pointwise_mul }
@[to_additive pointwise_add_add_monoid]
def pointwise_mul_monoid [monoid α] : monoid (set α) :=
{ one_mul := λ s, set.ext $ λ a,
⟨by {rintros ⟨_, _, _, _, rfl⟩, simp * at *},
λ h, ⟨1, mem_singleton 1, a, h, (one_mul a).symm⟩⟩,
mul_one := λ s, set.ext $ λ a,
⟨by {rintros ⟨_, _, _, _, rfl⟩, simp * at *},
λ h, ⟨a, h, 1, mem_singleton 1, (mul_one a).symm⟩⟩,
..pointwise_mul_semigroup,
..pointwise_one }
local attribute [instance] pointwise_mul_monoid
@[to_additive]
lemma singleton.is_mul_hom [has_mul α] : is_mul_hom (singleton : α → set α) :=
{ map_mul := λ x y, set.ext $ λ a, by simp [mem_singleton_iff, mem_pointwise_mul] }
@[to_additive is_add_monoid_hom]
lemma singleton.is_monoid_hom [monoid α] : is_monoid_hom (singleton : α → set α) :=
{ map_one := rfl, ..singleton.is_mul_hom }
@[to_additive]
def pointwise_inv [has_inv α] : has_inv (set α) :=
⟨λ s, {a | a⁻¹ ∈ s}⟩
@[simp, to_additive]
lemma pointwise_mul_empty [has_mul α] (s : set α) :
s * ∅ = ∅ :=
set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, tauto}, false.elim⟩
@[simp, to_additive]
lemma empty_pointwise_mul [has_mul α] (s : set α) :
∅ * s = ∅ :=
set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, tauto}, false.elim⟩
@[to_additive]
lemma pointwise_mul_subset_mul [has_mul α] {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) :
s₁ * s₂ ⊆ t₁ * t₂ :=
by {rintros _ ⟨_, _, _, _, rfl⟩, exact ⟨_, h₁ ‹_›, _, h₂ ‹_›, rfl⟩ }
@[to_additive]
lemma pointwise_mul_union [has_mul α] (s t u : set α) :
s * (t ∪ u) = (s * t) ∪ (s * u) :=
begin
ext a, split,
{ rintros ⟨_, _, _, H, rfl⟩,
cases H; [left, right]; exact ⟨_, ‹_›, _, ‹_›, rfl⟩ },
{ intro H,
cases H with H H;
{ rcases H with ⟨_, _, _, _, rfl⟩,
refine ⟨_, ‹_›, _, _, rfl⟩,
erw mem_union,
simp * } }
end
@[to_additive]
lemma union_pointwise_mul [has_mul α] (s t u : set α) :
(s ∪ t) * u = (s * u) ∪ (t * u) :=
begin
ext a, split,
{ rintros ⟨_, H, _, _, rfl⟩,
cases H; [left, right]; exact ⟨_, ‹_›, _, ‹_›, rfl⟩ },
{ intro H,
cases H with H H;
{ rcases H with ⟨_, _, _, _, rfl⟩;
refine ⟨_, _, _, ‹_›, rfl⟩;
erw mem_union;
simp * } }
end
@[to_additive]
lemma pointwise_mul_eq_Union_mul_left [has_mul α] {s t : set α} : s * t = ⋃a∈s, (λx, a * x) '' t :=
by { ext y; split; simp only [mem_Union]; rintros ⟨a, ha, x, hx, ax⟩; exact ⟨a, ha, x, hx, ax.symm⟩ }
@[to_additive]
lemma pointwise_mul_eq_Union_mul_right [has_mul α] {s t : set α} : s * t = ⋃a∈t, (λx, x * a) '' s :=
by { ext y; split; simp only [mem_Union]; rintros ⟨a, ha, x, hx, ax⟩; exact ⟨x, hx, a, ha, ax.symm⟩ }
@[to_additive]
lemma nonempty.pointwise_mul [has_mul α] {s t : set α} : s.nonempty → t.nonempty → (s * t).nonempty
| ⟨x, hx⟩ ⟨y, hy⟩ := ⟨x * y, ⟨x, hx, y, hy, rfl⟩⟩
@[simp, to_additive]
lemma univ_pointwise_mul_univ [monoid α] : (univ : set α) * univ = univ :=
begin
have : ∀x, ∃a b : α, x = a * b := λx, ⟨x, ⟨1, (mul_one x).symm⟩⟩,
show {a | ∃ x ∈ univ, ∃ y ∈ univ, a = x * y} = univ,
simpa [eq_univ_iff_forall]
end
def pointwise_mul_fintype [has_mul α] [decidable_eq α] (s t : set α) [hs : fintype s] [ht : fintype t] :
fintype (s * t : set α) := by { rw pointwise_mul_eq_image, apply set.fintype_image }
def pointwise_add_fintype [has_add α] [decidable_eq α] (s t : set α) [hs : fintype s] [ht : fintype t] :
fintype (s + t : set α) := by { rw pointwise_add_eq_image, apply set.fintype_image }
attribute [to_additive] set.pointwise_mul_fintype
/-- Pointwise scalar multiplication by a set of scalars. -/
def pointwise_smul [has_scalar α β] : has_scalar (set α) (set β) :=
⟨λ s t, { x | ∃ a ∈ s, ∃ y ∈ t, x = a • y }⟩
/-- Scaling a set: multiplying every element by a scalar. -/
def smul_set [has_scalar α β] : has_scalar α (set β) :=
⟨λ a s, { x | ∃ y ∈ s, x = a • y }⟩
local attribute [instance] pointwise_smul smul_set
lemma mem_smul_set [has_scalar α β] (a : α) (s : set β) (x : β) :
x ∈ a • s ↔ ∃ y ∈ s, x = a • y := iff.rfl
lemma smul_set_eq_image [has_scalar α β] (a : α) (s : set β) :
a • s = (λ x, a • x) '' s :=
set.ext $ λ x, iff.intro
(λ ⟨_, hy₁, hy₂⟩, ⟨_, hy₁, hy₂.symm⟩)
(λ ⟨_, hy₁, hy₂⟩, ⟨_, hy₁, hy₂.symm⟩)
lemma smul_set_eq_pointwise_smul_singleton [has_scalar α β]
(a : α) (s : set β) : a • s = ({a} : set α) • s :=
set.ext $ λ x, iff.intro
(λ ⟨_, h⟩, ⟨a, mem_singleton _, _, h⟩)
(λ ⟨_, h, y, hy, hx⟩, ⟨_, hy, by {
rw mem_singleton_iff at h; rwa h at hx }⟩)
lemma smul_mem_smul_set [has_scalar α β]
(a : α) {s : set β} {y : β} (hy : y ∈ s) : a • y ∈ a • s :=
by rw mem_smul_set; use [y, hy]
lemma smul_set_union [has_scalar α β] (a : α) (s t : set β) :
a • (s ∪ t) = a • s ∪ a • t :=
by simp only [smul_set_eq_image, image_union]
@[simp] lemma smul_set_empty [has_scalar α β] (a : α) :
a • (∅ : set β) = ∅ :=
by rw [smul_set_eq_image, image_empty]
lemma smul_set_mono [has_scalar α β]
(a : α) {s t : set β} (h : s ⊆ t) : a • s ⊆ a • t :=
by { rw [smul_set_eq_image, smul_set_eq_image], exact image_subset _ h }
section monoid
def pointwise_mul_semiring [monoid α] : semiring (set α) :=
{ add := (⊔),
zero := ∅,
add_assoc := set.union_assoc,
zero_add := set.empty_union,
add_zero := set.union_empty,
add_comm := set.union_comm,
zero_mul := empty_pointwise_mul,
mul_zero := pointwise_mul_empty,
left_distrib := pointwise_mul_union,
right_distrib := union_pointwise_mul,
..pointwise_mul_monoid }
def pointwise_mul_comm_semiring [comm_monoid α] : comm_semiring (set α) :=
{ mul_comm := λ s t, set.ext $ λ a,
by split; { rintros ⟨_, _, _, _, rfl⟩, exact ⟨_, ‹_›, _, ‹_›, mul_comm _ _⟩ },
..pointwise_mul_semiring }
local attribute [instance] pointwise_mul_semiring
def comm_monoid [comm_monoid α] : comm_monoid (set α) :=
@comm_semiring.to_comm_monoid (set α) pointwise_mul_comm_semiring
def add_comm_monoid [add_comm_monoid α] : add_comm_monoid (set α) :=
show @add_comm_monoid (additive (set (multiplicative α))),
from @additive.add_comm_monoid _ set.comm_monoid
attribute [to_additive set.add_comm_monoid] set.comm_monoid
/-- A multiplicative action of a monoid on a type β gives also a
multiplicative action on the subsets of β. -/
def smul_set_action [monoid α] [mul_action α β] :
mul_action α (set β) :=
{ smul := λ a s, a • s,
mul_smul := λ a b s, set.ext $ λ x, iff.intro
(λ ⟨_, hy, _⟩, ⟨b • _, smul_mem_smul_set _ hy, by rwa ←mul_smul⟩)
(λ ⟨_, hy, _⟩, let ⟨_, hz, h⟩ := (mem_smul_set _ _ _).2 hy in
⟨_, hz, by rwa [mul_smul, ←h]⟩),
one_smul := λ b, set.ext $ λ x, iff.intro
(λ ⟨_, _, h⟩, by { rw [one_smul] at h; rwa h })
(λ h, ⟨_, h, by rw one_smul⟩) }
section is_mul_hom
open is_mul_hom
variables [has_mul α] [has_mul β] (m : α → β) [is_mul_hom m]
@[to_additive]
lemma image_pointwise_mul (s t : set α) : m '' (s * t) = m '' s * m '' t :=
set.ext $ assume y,
begin
split,
{ rintros ⟨_, ⟨_, _, _, _, rfl⟩, rfl⟩,
refine ⟨_, mem_image_of_mem _ ‹_›, _, mem_image_of_mem _ ‹_›, map_mul _ _ _⟩ },
{ rintros ⟨_, ⟨_, _, rfl⟩, _, ⟨_, _, rfl⟩, rfl⟩,
refine ⟨_, ⟨_, ‹_›, _, ‹_›, rfl⟩, map_mul _ _ _⟩ }
end
@[to_additive]
lemma preimage_pointwise_mul_preimage_subset (s t : set β) : m ⁻¹' s * m ⁻¹' t ⊆ m ⁻¹' (s * t) :=
begin
rintros _ ⟨_, _, _, _, rfl⟩,
exact ⟨_, ‹_›, _, ‹_›, map_mul _ _ _⟩,
end
end is_mul_hom
variables [monoid α] [monoid β] [is_monoid_hom f]
lemma pointwise_mul_image_is_semiring_hom : is_semiring_hom (image f) :=
{ map_zero := image_empty _,
map_one := by erw [image_singleton, is_monoid_hom.map_one f]; refl,
map_add := image_union _,
map_mul := image_pointwise_mul _ }
end monoid
end set
section
open set
variables {α : Type*} {β : Type*}
local attribute [instance] set.smul_set
/-- A nonempty set in a semimodule is scaled by zero to the singleton
containing 0 in the semimodule. -/
lemma zero_smul_set [semiring α] [add_comm_monoid β] [semimodule α β]
{s : set β} (h : s.nonempty) : (0 : α) • s = {(0 : β)} :=
set.ext $ λ x, iff.intro
(λ ⟨_, _, hx⟩, mem_singleton_iff.mpr (by { rwa [hx, zero_smul] }))
(λ hx, let ⟨_, hs⟩ := h in
⟨_, hs, by { rw mem_singleton_iff at hx; rw [hx, zero_smul] }⟩)
lemma mem_inv_smul_set_iff [field α] [mul_action α β]
{a : α} (ha : a ≠ 0) (A : set β) (x : β) : x ∈ a⁻¹ • A ↔ a • x ∈ A :=
iff.intro
(λ ⟨y, hy, h⟩, by rwa [h, ←mul_smul, mul_inv_cancel ha, one_smul])
(λ h, ⟨_, h, by rw [←mul_smul, inv_mul_cancel ha, one_smul]⟩)
lemma mem_smul_set_iff_inv_smul_mem [field α] [mul_action α β]
{a : α} (ha : a ≠ 0) (A : set β) (x : β) : x ∈ a • A ↔ a⁻¹ • x ∈ A :=
by conv_lhs { rw ← inv_inv' a };
exact (mem_inv_smul_set_iff (inv_ne_zero ha) _ _)
end
|
c32de6229686ea4e6377f0e3d435e7f044b1f09d | 94e33a31faa76775069b071adea97e86e218a8ee | /src/data/finset/lattice.lean | 24f8a6368f2905e81aeb7035510b23a9901660dd | [
"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 | 57,606 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.finset.fold
import data.finset.option
import data.finset.prod
import data.multiset.lattice
import order.complete_lattice
/-!
# Lattice operations on finsets
-/
variables {α β γ ι : Type*}
namespace finset
open multiset order_dual
/-! ### sup -/
section sup
-- TODO: define with just `[has_bot α]` where some lemmas hold without requiring `[order_bot α]`
variables [semilattice_sup α] [order_bot α]
/-- Supremum of a finite set: `sup {a, b, c} f = f a ⊔ f b ⊔ f c` -/
def sup (s : finset β) (f : β → α) : α := s.fold (⊔) ⊥ f
variables {s s₁ s₂ : finset β} {f g : β → α}
lemma sup_def : s.sup f = (s.1.map f).sup := rfl
@[simp] lemma sup_empty : (∅ : finset β).sup f = ⊥ :=
fold_empty
@[simp] lemma sup_cons {b : β} (h : b ∉ s) : (cons b s h).sup f = f b ⊔ s.sup f :=
fold_cons h
@[simp] lemma sup_insert [decidable_eq β] {b : β} : (insert b s : finset β).sup f = f b ⊔ s.sup f :=
fold_insert_idem
lemma sup_image [decidable_eq β] (s : finset γ) (f : γ → β) (g : β → α) :
(s.image f).sup g = s.sup (g ∘ f) :=
fold_image_idem
@[simp] lemma sup_map (s : finset γ) (f : γ ↪ β) (g : β → α) :
(s.map f).sup g = s.sup (g ∘ f) :=
fold_map
@[simp] lemma sup_singleton {b : β} : ({b} : finset β).sup f = f b :=
sup_singleton
lemma sup_union [decidable_eq β] : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f :=
finset.induction_on s₁ (by rw [empty_union, sup_empty, bot_sup_eq]) $ λ a s has ih,
by rw [insert_union, sup_insert, sup_insert, ih, sup_assoc]
lemma sup_sup : s.sup (f ⊔ g) = s.sup f ⊔ s.sup g :=
begin
refine finset.cons_induction_on s _ (λ b t _ h, _),
{ rw [sup_empty, sup_empty, sup_empty, bot_sup_eq] },
{ rw [sup_cons, sup_cons, sup_cons, h],
exact sup_sup_sup_comm _ _ _ _ }
end
theorem sup_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.sup f = s₂.sup g :=
by subst hs; exact finset.fold_congr hfg
@[simp] protected lemma sup_le_iff {a : α} : s.sup f ≤ a ↔ (∀b ∈ s, f b ≤ a) :=
begin
apply iff.trans multiset.sup_le,
simp only [multiset.mem_map, and_imp, exists_imp_distrib],
exact ⟨λ k b hb, k _ _ hb rfl, λ k a' b hb h, h ▸ k _ hb⟩,
end
@[simp] lemma sup_bUnion [decidable_eq β] (s : finset γ) (t : γ → finset β) :
(s.bUnion t).sup f = s.sup (λ x, (t x).sup f) :=
eq_of_forall_ge_iff $ λ c, by simp [@forall_swap _ β]
lemma sup_const {s : finset β} (h : s.nonempty) (c : α) : s.sup (λ _, c) = c :=
eq_of_forall_ge_iff $ λ b, finset.sup_le_iff.trans h.forall_const
@[simp] lemma sup_bot (s : finset β) : s.sup (λ _, ⊥) = (⊥ : α) :=
begin
obtain rfl | hs := s.eq_empty_or_nonempty,
{ exact sup_empty },
{ exact sup_const hs _ }
end
lemma sup_ite (p : β → Prop) [decidable_pred p] :
s.sup (λ i, ite (p i) (f i) (g i)) =
(s.filter p).sup f ⊔ (s.filter (λ i, ¬ p i)).sup g :=
fold_ite _
lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a :=
finset.sup_le_iff.2
lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f :=
finset.sup_le_iff.1 le_rfl _ hb
lemma sup_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.sup f ≤ s.sup g :=
sup_le (λ b hb, le_trans (h b hb) (le_sup hb))
lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f :=
sup_le $ assume b hb, le_sup (h hb)
lemma sup_comm (s : finset β) (t : finset γ) (f : β → γ → α) :
s.sup (λ b, t.sup (f b)) = t.sup (λ c, s.sup (λ b, f b c)) :=
begin
refine eq_of_forall_ge_iff (λ a, _),
simp_rw finset.sup_le_iff,
exact ⟨λ h c hc b hb, h b hb c hc, λ h b hb c hc, h c hc b hb⟩,
end
@[simp] lemma sup_attach (s : finset β) (f : β → α) : s.attach.sup (λ x, f x) = s.sup f :=
(s.attach.sup_map (function.embedding.subtype _) f).symm.trans $ congr_arg _ attach_map_val
/-- See also `finset.product_bUnion`. -/
lemma sup_product_left (s : finset β) (t : finset γ) (f : β × γ → α) :
(s.product t).sup f = s.sup (λ i, t.sup $ λ i', f ⟨i, i'⟩) :=
begin
refine le_antisymm _ (sup_le (λ i hi, sup_le $ λ i' hi', le_sup $ mem_product.2 ⟨hi, hi'⟩)),
refine sup_le _,
rintro ⟨i, i'⟩ hi,
rw mem_product at hi,
refine le_trans _ (le_sup hi.1),
convert le_sup hi.2,
end
lemma sup_product_right (s : finset β) (t : finset γ) (f : β × γ → α) :
(s.product t).sup f = t.sup (λ i', s.sup $ λ i, f ⟨i, i'⟩) :=
by rw [sup_product_left, sup_comm]
@[simp] lemma sup_erase_bot [decidable_eq α] (s : finset α) : (s.erase ⊥).sup id = s.sup id :=
begin
refine (sup_mono (s.erase_subset _)).antisymm (finset.sup_le_iff.2 $ λ a ha, _),
obtain rfl | ha' := eq_or_ne a ⊥,
{ exact bot_le },
{ exact le_sup (mem_erase.2 ⟨ha', ha⟩) }
end
lemma sup_sdiff_right {α β : Type*} [generalized_boolean_algebra α] (s : finset β) (f : β → α)
(a : α) :
s.sup (λ b, f b \ a) = s.sup f \ a :=
begin
refine finset.cons_induction_on s _ (λ b t _ h, _),
{ rw [sup_empty, sup_empty, bot_sdiff] },
{ rw [sup_cons, sup_cons, h, sup_sdiff] }
end
lemma comp_sup_eq_sup_comp [semilattice_sup γ] [order_bot γ] {s : finset β}
{f : β → α} (g : α → γ) (g_sup : ∀ x y, g (x ⊔ y) = g x ⊔ g y) (bot : g ⊥ = ⊥) :
g (s.sup f) = s.sup (g ∘ f) :=
finset.cons_induction_on s bot (λ c t hc ih, by rw [sup_cons, sup_cons, g_sup, ih])
/-- Computing `sup` in a subtype (closed under `sup`) is the same as computing it in `α`. -/
lemma sup_coe {P : α → Prop}
{Pbot : P ⊥} {Psup : ∀{{x y}}, P x → P y → P (x ⊔ y)}
(t : finset β) (f : β → {x : α // P x}) :
(@sup _ _ (subtype.semilattice_sup Psup) (subtype.order_bot Pbot) t f : α) = t.sup (λ x, f x) :=
by { rw [comp_sup_eq_sup_comp coe]; intros; refl }
@[simp] lemma sup_to_finset {α β} [decidable_eq β]
(s : finset α) (f : α → multiset β) :
(s.sup f).to_finset = s.sup (λ x, (f x).to_finset) :=
comp_sup_eq_sup_comp multiset.to_finset to_finset_union rfl
theorem subset_range_sup_succ (s : finset ℕ) : s ⊆ range (s.sup id).succ :=
λ n hn, mem_range.2 $ nat.lt_succ_of_le $ le_sup hn
theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n :=
⟨_, s.subset_range_sup_succ⟩
lemma sup_induction {p : α → Prop} (hb : p ⊥) (hp : ∀ a₁, p a₁ → ∀ a₂, p a₂ → p (a₁ ⊔ a₂))
(hs : ∀ b ∈ s, p (f b)) : p (s.sup f) :=
begin
induction s using finset.cons_induction with c s hc ih,
{ exact hb, },
{ rw sup_cons,
apply hp,
{ exact hs c (mem_cons.2 (or.inl rfl)), },
{ exact ih (λ b h, hs b (mem_cons.2 (or.inr h))), }, },
end
lemma sup_le_of_le_directed {α : Type*} [semilattice_sup α] [order_bot α] (s : set α)
(hs : s.nonempty) (hdir : directed_on (≤) s) (t : finset α) :
(∀ x ∈ t, ∃ y ∈ s, x ≤ y) → ∃ x, x ∈ s ∧ t.sup id ≤ x :=
begin
classical,
apply finset.induction_on t,
{ simpa only [forall_prop_of_true, and_true, forall_prop_of_false, bot_le, not_false_iff,
sup_empty, forall_true_iff, not_mem_empty], },
{ intros a r har ih h,
have incs : ↑r ⊆ ↑(insert a r), by { rw finset.coe_subset, apply finset.subset_insert, },
-- x ∈ s is above the sup of r
obtain ⟨x, ⟨hxs, hsx_sup⟩⟩ := ih (λ x hx, h x $ incs hx),
-- y ∈ s is above a
obtain ⟨y, hys, hay⟩ := h a (finset.mem_insert_self a r),
-- z ∈ s is above x and y
obtain ⟨z, hzs, ⟨hxz, hyz⟩⟩ := hdir x hxs y hys,
use [z, hzs],
rw [sup_insert, id.def, sup_le_iff],
exact ⟨le_trans hay hyz, le_trans hsx_sup hxz⟩, },
end
-- If we acquire sublattices
-- the hypotheses should be reformulated as `s : subsemilattice_sup_bot`
lemma sup_mem
(s : set α) (w₁ : ⊥ ∈ s) (w₂ : ∀ x y ∈ s, x ⊔ y ∈ s)
{ι : Type*} (t : finset ι) (p : ι → α) (h : ∀ i ∈ t, p i ∈ s) :
t.sup p ∈ s :=
@sup_induction _ _ _ _ _ _ (∈ s) w₁ w₂ h
@[simp]
lemma sup_eq_bot_iff (f : β → α)
(S : finset β) : S.sup f = ⊥ ↔ ∀ s ∈ S, f s = ⊥ :=
begin
classical,
induction S using finset.induction with a S haS hi;
simp [*],
end
end sup
lemma sup_eq_supr [complete_lattice β] (s : finset α) (f : α → β) : s.sup f = (⨆a∈s, f a) :=
le_antisymm
(finset.sup_le $ assume a ha, le_supr_of_le a $ le_supr _ ha)
(supr_le $ assume a, supr_le $ assume ha, le_sup ha)
lemma sup_id_eq_Sup [complete_lattice α] (s : finset α) : s.sup id = Sup s :=
by simp [Sup_eq_supr, sup_eq_supr]
lemma sup_id_set_eq_sUnion (s : finset (set α)) : s.sup id = ⋃₀(↑s) :=
sup_id_eq_Sup _
@[simp] lemma sup_set_eq_bUnion (s : finset α) (f : α → set β) : s.sup f = ⋃ x ∈ s, f x :=
sup_eq_supr _ _
lemma sup_eq_Sup_image [complete_lattice β] (s : finset α) (f : α → β) : s.sup f = Sup (f '' s) :=
begin
classical,
rw [←finset.coe_image, ←sup_id_eq_Sup, sup_image, function.comp.left_id],
end
/-! ### inf -/
section inf
-- TODO: define with just `[has_top α]` where some lemmas hold without requiring `[order_top α]`
variables [semilattice_inf α] [order_top α]
/-- Infimum of a finite set: `inf {a, b, c} f = f a ⊓ f b ⊓ f c` -/
def inf (s : finset β) (f : β → α) : α := s.fold (⊓) ⊤ f
variables {s s₁ s₂ : finset β} {f g : β → α}
lemma inf_def : s.inf f = (s.1.map f).inf := rfl
@[simp] lemma inf_empty : (∅ : finset β).inf f = ⊤ :=
fold_empty
@[simp] lemma inf_cons {b : β} (h : b ∉ s) : (cons b s h).inf f = f b ⊓ s.inf f :=
@sup_cons αᵒᵈ _ _ _ _ _ _ h
@[simp] lemma inf_insert [decidable_eq β] {b : β} : (insert b s : finset β).inf f = f b ⊓ s.inf f :=
fold_insert_idem
lemma inf_image [decidable_eq β] (s : finset γ) (f : γ → β) (g : β → α) :
(s.image f).inf g = s.inf (g ∘ f) :=
fold_image_idem
@[simp] lemma inf_map (s : finset γ) (f : γ ↪ β) (g : β → α) :
(s.map f).inf g = s.inf (g ∘ f) :=
fold_map
@[simp] lemma inf_singleton {b : β} : ({b} : finset β).inf f = f b :=
inf_singleton
lemma inf_union [decidable_eq β] : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f :=
@sup_union αᵒᵈ _ _ _ _ _ _ _
lemma inf_inf : s.inf (f ⊓ g) = s.inf f ⊓ s.inf g :=
@sup_sup αᵒᵈ _ _ _ _ _ _
theorem inf_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.inf f = s₂.inf g :=
by subst hs; exact finset.fold_congr hfg
@[simp] lemma inf_bUnion [decidable_eq β] (s : finset γ) (t : γ → finset β) :
(s.bUnion t).inf f = s.inf (λ x, (t x).inf f) :=
@sup_bUnion αᵒᵈ _ _ _ _ _ _ _ _
lemma inf_const {s : finset β} (h : s.nonempty) (c : α) : s.inf (λ _, c) = c :=
@sup_const αᵒᵈ _ _ _ _ h _
@[simp] lemma inf_top (s : finset β) : s.inf (λ _, ⊤) = (⊤ : α) := @sup_bot αᵒᵈ _ _ _ _
lemma le_inf_iff {a : α} : a ≤ s.inf f ↔ ∀ b ∈ s, a ≤ f b :=
@finset.sup_le_iff αᵒᵈ _ _ _ _ _ _
lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b :=
le_inf_iff.1 le_rfl _ hb
lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f :=
le_inf_iff.2
lemma inf_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.inf f ≤ s.inf g :=
le_inf (λ b hb, le_trans (inf_le hb) (h b hb))
lemma inf_mono (h : s₁ ⊆ s₂) : s₂.inf f ≤ s₁.inf f :=
le_inf $ assume b hb, inf_le (h hb)
lemma inf_attach (s : finset β) (f : β → α) : s.attach.inf (λ x, f x) = s.inf f :=
@sup_attach αᵒᵈ _ _ _ _ _
lemma inf_comm (s : finset β) (t : finset γ) (f : β → γ → α) :
s.inf (λ b, t.inf (f b)) = t.inf (λ c, s.inf (λ b, f b c)) :=
@sup_comm αᵒᵈ _ _ _ _ _ _ _
lemma inf_product_left (s : finset β) (t : finset γ) (f : β × γ → α) :
(s.product t).inf f = s.inf (λ i, t.inf $ λ i', f ⟨i, i'⟩) :=
@sup_product_left αᵒᵈ _ _ _ _ _ _ _
lemma inf_product_right (s : finset β) (t : finset γ) (f : β × γ → α) :
(s.product t).inf f = t.inf (λ i', s.inf $ λ i, f ⟨i, i'⟩) :=
@sup_product_right αᵒᵈ _ _ _ _ _ _ _
@[simp] lemma inf_erase_top [decidable_eq α] (s : finset α) : (s.erase ⊤).inf id = s.inf id :=
@sup_erase_bot αᵒᵈ _ _ _ _
lemma sup_sdiff_left {α β : Type*} [boolean_algebra α] (s : finset β) (f : β → α) (a : α) :
s.sup (λ b, a \ f b) = a \ s.inf f :=
begin
refine finset.cons_induction_on s _ (λ b t _ h, _),
{ rw [sup_empty, inf_empty, sdiff_top] },
{ rw [sup_cons, inf_cons, h, sdiff_inf] }
end
lemma inf_sdiff_left {α β : Type*} [boolean_algebra α] {s : finset β} (hs : s.nonempty) (f : β → α)
(a : α) :
s.inf (λ b, a \ f b) = a \ s.sup f :=
begin
induction hs using finset.nonempty.cons_induction with b b t _ _ h,
{ rw [sup_singleton, inf_singleton] },
{ rw [sup_cons, inf_cons, h, sdiff_sup] }
end
lemma inf_sdiff_right {α β : Type*} [boolean_algebra α] {s : finset β} (hs : s.nonempty) (f : β → α)
(a : α) :
s.inf (λ b, f b \ a) = s.inf f \ a :=
begin
induction hs using finset.nonempty.cons_induction with b b t _ _ h,
{ rw [inf_singleton, inf_singleton] },
{ rw [inf_cons, inf_cons, h, inf_sdiff] }
end
lemma comp_inf_eq_inf_comp [semilattice_inf γ] [order_top γ] {s : finset β}
{f : β → α} (g : α → γ) (g_inf : ∀ x y, g (x ⊓ y) = g x ⊓ g y) (top : g ⊤ = ⊤) :
g (s.inf f) = s.inf (g ∘ f) :=
@comp_sup_eq_sup_comp αᵒᵈ _ γᵒᵈ _ _ _ _ _ _ _ g_inf top
/-- Computing `inf` in a subtype (closed under `inf`) is the same as computing it in `α`. -/
lemma inf_coe {P : α → Prop}
{Ptop : P ⊤} {Pinf : ∀{{x y}}, P x → P y → P (x ⊓ y)}
(t : finset β) (f : β → {x : α // P x}) :
(@inf _ _ (subtype.semilattice_inf Pinf) (subtype.order_top Ptop) t f : α) = t.inf (λ x, f x) :=
@sup_coe αᵒᵈ _ _ _ _ Ptop Pinf t f
lemma inf_induction {p : α → Prop} (ht : p ⊤) (hp : ∀ a₁, p a₁ → ∀ a₂, p a₂ → p (a₁ ⊓ a₂))
(hs : ∀ b ∈ s, p (f b)) : p (s.inf f) :=
@sup_induction αᵒᵈ _ _ _ _ _ _ ht hp hs
lemma inf_mem
(s : set α) (w₁ : ⊤ ∈ s) (w₂ : ∀ x y ∈ s, x ⊓ y ∈ s)
{ι : Type*} (t : finset ι) (p : ι → α) (h : ∀ i ∈ t, p i ∈ s) :
t.inf p ∈ s :=
@inf_induction _ _ _ _ _ _ (∈ s) w₁ w₂ h
@[simp]
lemma inf_eq_top_iff (f : β → α)
(S : finset β) : S.inf f = ⊤ ↔ ∀ s ∈ S, f s = ⊤ :=
@finset.sup_eq_bot_iff αᵒᵈ _ _ _ _ _
end inf
@[simp] lemma to_dual_sup [semilattice_sup α] [order_bot α] (s : finset β) (f : β → α) :
to_dual (s.sup f) = s.inf (to_dual ∘ f) := rfl
@[simp] lemma to_dual_inf [semilattice_inf α] [order_top α] (s : finset β) (f : β → α) :
to_dual (s.inf f) = s.sup (to_dual ∘ f) := rfl
@[simp] lemma of_dual_sup [semilattice_inf α] [order_top α] (s : finset β) (f : β → αᵒᵈ) :
of_dual (s.sup f) = s.inf (of_dual ∘ f) := rfl
@[simp] lemma of_dual_inf [semilattice_sup α] [order_bot α] (s : finset β) (f : β → αᵒᵈ) :
of_dual (s.inf f) = s.sup (of_dual ∘ f) := rfl
section distrib_lattice
variables [distrib_lattice α]
section order_bot
variables [order_bot α] {s : finset β} {f : β → α} {a : α}
lemma sup_inf_distrib_left (s : finset ι) (f : ι → α) (a : α) :
a ⊓ s.sup f = s.sup (λ i, a ⊓ f i) :=
begin
induction s using finset.cons_induction with i s hi h,
{ simp_rw [finset.sup_empty, inf_bot_eq] },
{ rw [sup_cons, sup_cons, inf_sup_left, h] }
end
lemma sup_inf_distrib_right (s : finset ι) (f : ι → α) (a : α) :
s.sup f ⊓ a = s.sup (λ i, f i ⊓ a) :=
by { rw [_root_.inf_comm, s.sup_inf_distrib_left], simp_rw _root_.inf_comm }
lemma disjoint_sup_right : disjoint a (s.sup f) ↔ ∀ i ∈ s, disjoint a (f i) :=
by simp only [disjoint_iff, sup_inf_distrib_left, sup_eq_bot_iff]
lemma disjoint_sup_left : disjoint (s.sup f) a ↔ ∀ i ∈ s, disjoint (f i) a :=
by simp only [disjoint_iff, sup_inf_distrib_right, sup_eq_bot_iff]
end order_bot
section order_top
variables [order_top α]
lemma inf_sup_distrib_left (s : finset ι) (f : ι → α) (a : α) :
a ⊔ s.inf f = s.inf (λ i, a ⊔ f i) :=
@sup_inf_distrib_left αᵒᵈ _ _ _ _ _ _
lemma inf_sup_distrib_right (s : finset ι) (f : ι → α) (a : α) :
s.inf f ⊔ a = s.inf (λ i, f i ⊔ a) :=
@sup_inf_distrib_right αᵒᵈ _ _ _ _ _ _
end order_top
end distrib_lattice
section linear_order
variables [linear_order α]
section order_bot
variables [order_bot α] {s : finset ι} {f : ι → α} {a : α}
lemma comp_sup_eq_sup_comp_of_is_total [semilattice_sup β] [order_bot β] (g : α → β)
(mono_g : monotone g) (bot : g ⊥ = ⊥) : g (s.sup f) = s.sup (g ∘ f) :=
comp_sup_eq_sup_comp g mono_g.map_sup bot
@[simp] protected lemma le_sup_iff (ha : ⊥ < a) : a ≤ s.sup f ↔ ∃ b ∈ s, a ≤ f b :=
⟨finset.cons_induction_on s (λ h, absurd h (not_le_of_lt ha))
(λ c t hc ih, by simpa using @or.rec _ _ (∃ b, (b = c ∨ b ∈ t) ∧ a ≤ f b)
(λ h, ⟨c, or.inl rfl, h⟩) (λ h, let ⟨b, hb, hle⟩ := ih h in ⟨b, or.inr hb, hle⟩)),
(λ ⟨b, hb, hle⟩, trans hle (le_sup hb))⟩
@[simp] protected lemma lt_sup_iff : a < s.sup f ↔ ∃ b ∈ s, a < f b :=
⟨finset.cons_induction_on s (λ h, absurd h not_lt_bot)
(λ c t hc ih, by simpa using @or.rec _ _ (∃ b, (b = c ∨ b ∈ t) ∧ a < f b)
(λ h, ⟨c, or.inl rfl, h⟩) (λ h, let ⟨b, hb, hlt⟩ := ih h in ⟨b, or.inr hb, hlt⟩)),
(λ ⟨b, hb, hlt⟩, lt_of_lt_of_le hlt (le_sup hb))⟩
@[simp] protected lemma sup_lt_iff (ha : ⊥ < a) : s.sup f < a ↔ ∀ b ∈ s, f b < a :=
⟨(λ hs b hb, lt_of_le_of_lt (le_sup hb) hs), finset.cons_induction_on s (λ _, ha)
(λ c t hc, by simpa only [sup_cons, sup_lt_iff, mem_cons, forall_eq_or_imp] using and.imp_right)⟩
end order_bot
section order_top
variables [order_top α] {s : finset ι} {f : ι → α} {a : α}
lemma comp_inf_eq_inf_comp_of_is_total [semilattice_inf β] [order_top β] (g : α → β)
(mono_g : monotone g) (top : g ⊤ = ⊤) : g (s.inf f) = s.inf (g ∘ f) :=
comp_inf_eq_inf_comp g mono_g.map_inf top
@[simp] protected lemma inf_le_iff (ha : a < ⊤) : s.inf f ≤ a ↔ ∃ b ∈ s, f b ≤ a :=
@finset.le_sup_iff αᵒᵈ _ _ _ _ _ _ ha
@[simp] protected lemma inf_lt_iff : s.inf f < a ↔ ∃ b ∈ s, f b < a :=
@finset.lt_sup_iff αᵒᵈ _ _ _ _ _ _
@[simp] protected lemma lt_inf_iff (ha : a < ⊤) : a < s.inf f ↔ ∀ b ∈ s, a < f b :=
@finset.sup_lt_iff αᵒᵈ _ _ _ _ _ _ ha
end order_top
end linear_order
lemma inf_eq_infi [complete_lattice β] (s : finset α) (f : α → β) : s.inf f = ⨅ a ∈ s, f a :=
@sup_eq_supr _ βᵒᵈ _ _ _
lemma inf_id_eq_Inf [complete_lattice α] (s : finset α) : s.inf id = Inf s := @sup_id_eq_Sup αᵒᵈ _ _
lemma inf_id_set_eq_sInter (s : finset (set α)) : s.inf id = ⋂₀(↑s) :=
inf_id_eq_Inf _
@[simp] lemma inf_set_eq_bInter (s : finset α) (f : α → set β) : s.inf f = ⋂ x ∈ s, f x :=
inf_eq_infi _ _
lemma inf_eq_Inf_image [complete_lattice β] (s : finset α) (f : α → β) : s.inf f = Inf (f '' s) :=
@sup_eq_Sup_image _ βᵒᵈ _ _ _
section sup'
variables [semilattice_sup α]
lemma sup_of_mem {s : finset β} (f : β → α) {b : β} (h : b ∈ s) :
∃ (a : α), s.sup (coe ∘ f : β → with_bot α) = ↑a :=
Exists.imp (λ a, Exists.fst) (@le_sup (with_bot α) _ _ _ _ _ _ h (f b) rfl)
/-- Given nonempty finset `s` then `s.sup' H f` is the supremum of its image under `f` in (possibly
unbounded) join-semilattice `α`, where `H` is a proof of nonemptiness. If `α` has a bottom element
you may instead use `finset.sup` which does not require `s` nonempty. -/
def sup' (s : finset β) (H : s.nonempty) (f : β → α) : α :=
option.get $ let ⟨b, hb⟩ := H in option.is_some_iff_exists.2 (sup_of_mem f hb)
variables {s : finset β} (H : s.nonempty) (f : β → α)
@[simp] lemma coe_sup' : ((s.sup' H f : α) : with_bot α) = s.sup (coe ∘ f) :=
by rw [sup', ←with_bot.some_eq_coe, option.some_get]
@[simp] lemma sup'_cons {b : β} {hb : b ∉ s} {h : (cons b s hb).nonempty} :
(cons b s hb).sup' h f = f b ⊔ s.sup' H f :=
by { rw ←with_bot.coe_eq_coe, simp only [coe_sup', sup_cons, with_bot.coe_sup], }
@[simp] lemma sup'_insert [decidable_eq β] {b : β} {h : (insert b s).nonempty} :
(insert b s).sup' h f = f b ⊔ s.sup' H f :=
by { rw ←with_bot.coe_eq_coe, simp only [coe_sup', sup_insert, with_bot.coe_sup], }
@[simp] lemma sup'_singleton {b : β} {h : ({b} : finset β).nonempty} :
({b} : finset β).sup' h f = f b := rfl
lemma sup'_le {a : α} (hs : ∀ b ∈ s, f b ≤ a) : s.sup' H f ≤ a :=
by { rw [←with_bot.coe_le_coe, coe_sup'], exact sup_le (λ b h, with_bot.coe_le_coe.2 $ hs b h), }
lemma le_sup' {b : β} (h : b ∈ s) : f b ≤ s.sup' ⟨b, h⟩ f :=
by { rw [←with_bot.coe_le_coe, coe_sup'], exact le_sup h, }
@[simp] lemma sup'_const (a : α) : s.sup' H (λ b, a) = a :=
begin
apply le_antisymm,
{ apply sup'_le, intros, exact le_rfl, },
{ apply le_sup' (λ b, a) H.some_spec, }
end
@[simp] lemma sup'_le_iff {a : α} : s.sup' H f ≤ a ↔ ∀ b ∈ s, f b ≤ a :=
iff.intro (λ h b hb, trans (le_sup' f hb) h) (sup'_le H f)
lemma sup'_bUnion [decidable_eq β] {s : finset γ} (Hs : s.nonempty) {t : γ → finset β}
(Ht : ∀ b, (t b).nonempty) :
(s.bUnion t).sup' (Hs.bUnion (λ b _, Ht b)) f = s.sup' Hs (λ b, (t b).sup' (Ht b) f) :=
eq_of_forall_ge_iff $ λ c, by simp [@forall_swap _ β]
lemma comp_sup'_eq_sup'_comp [semilattice_sup γ] {s : finset β} (H : s.nonempty)
{f : β → α} (g : α → γ) (g_sup : ∀ x y, g (x ⊔ y) = g x ⊔ g y) :
g (s.sup' H f) = s.sup' H (g ∘ f) :=
begin
rw [←with_bot.coe_eq_coe, coe_sup'],
let g' : with_bot α → with_bot γ := with_bot.rec_bot_coe ⊥ (λ x, ↑(g x)),
show g' ↑(s.sup' H f) = s.sup (λ a, g' ↑(f a)),
rw coe_sup',
refine comp_sup_eq_sup_comp g' _ rfl,
intros f₁ f₂,
cases f₁,
{ rw [with_bot.none_eq_bot, bot_sup_eq], exact bot_sup_eq.symm, },
{ cases f₂, refl,
exact congr_arg coe (g_sup f₁ f₂), },
end
lemma sup'_induction {p : α → Prop} (hp : ∀ a₁, p a₁ → ∀ a₂, p a₂ → p (a₁ ⊔ a₂))
(hs : ∀ b ∈ s, p (f b)) : p (s.sup' H f) :=
begin
show @with_bot.rec_bot_coe α (λ _, Prop) true p ↑(s.sup' H f),
rw coe_sup',
refine sup_induction trivial _ hs,
rintro (_|a₁) h₁ a₂ h₂,
{ rw [with_bot.none_eq_bot, bot_sup_eq], exact h₂ },
cases a₂,
exacts [h₁, hp a₁ h₁ a₂ h₂]
end
lemma sup'_mem
(s : set α) (w : ∀ x y ∈ s, x ⊔ y ∈ s)
{ι : Type*} (t : finset ι) (H : t.nonempty) (p : ι → α) (h : ∀ i ∈ t, p i ∈ s) :
t.sup' H p ∈ s := sup'_induction H p w h
@[congr] lemma sup'_congr {t : finset β} {f g : β → α} (h₁ : s = t) (h₂ : ∀ x ∈ s, f x = g x) :
s.sup' H f = t.sup' (h₁ ▸ H) g :=
begin
subst s,
refine eq_of_forall_ge_iff (λ c, _),
simp only [sup'_le_iff, h₂] { contextual := tt }
end
end sup'
section inf'
variables [semilattice_inf α]
lemma inf_of_mem {s : finset β} (f : β → α) {b : β} (h : b ∈ s) :
∃ (a : α), s.inf (coe ∘ f : β → with_top α) = ↑a :=
@sup_of_mem αᵒᵈ _ _ _ f _ h
/-- Given nonempty finset `s` then `s.inf' H f` is the infimum of its image under `f` in (possibly
unbounded) meet-semilattice `α`, where `H` is a proof of nonemptiness. If `α` has a top element you
may instead use `finset.inf` which does not require `s` nonempty. -/
def inf' (s : finset β) (H : s.nonempty) (f : β → α) : α :=
@sup' αᵒᵈ _ _ s H f
variables {s : finset β} (H : s.nonempty) (f : β → α) {a : α} {b : β}
@[simp] lemma coe_inf' : ((s.inf' H f : α) : with_top α) = s.inf (coe ∘ f) :=
@coe_sup' αᵒᵈ _ _ _ H f
@[simp] lemma inf'_cons {b : β} {hb : b ∉ s} {h : (cons b s hb).nonempty} :
(cons b s hb).inf' h f = f b ⊓ s.inf' H f :=
@sup'_cons αᵒᵈ _ _ _ H f _ _ _
@[simp] lemma inf'_insert [decidable_eq β] {b : β} {h : (insert b s).nonempty} :
(insert b s).inf' h f = f b ⊓ s.inf' H f :=
@sup'_insert αᵒᵈ _ _ _ H f _ _ _
@[simp] lemma inf'_singleton {b : β} {h : ({b} : finset β).nonempty} :
({b} : finset β).inf' h f = f b := rfl
lemma le_inf' (hs : ∀ b ∈ s, a ≤ f b) : a ≤ s.inf' H f := @sup'_le αᵒᵈ _ _ _ H f _ hs
lemma inf'_le (h : b ∈ s) : s.inf' ⟨b, h⟩ f ≤ f b := @le_sup' αᵒᵈ _ _ _ f _ h
@[simp] lemma inf'_const (a : α) : s.inf' H (λ b, a) = a := @sup'_const αᵒᵈ _ _ _ _ _
@[simp] lemma le_inf'_iff : a ≤ s.inf' H f ↔ ∀ b ∈ s, a ≤ f b := @sup'_le_iff αᵒᵈ _ _ _ H f _
lemma inf'_bUnion [decidable_eq β] {s : finset γ} (Hs : s.nonempty) {t : γ → finset β}
(Ht : ∀ b, (t b).nonempty) :
(s.bUnion t).inf' (Hs.bUnion (λ b _, Ht b)) f = s.inf' Hs (λ b, (t b).inf' (Ht b) f) :=
@sup'_bUnion αᵒᵈ _ _ _ _ _ _ Hs _ Ht
lemma comp_inf'_eq_inf'_comp [semilattice_inf γ] {s : finset β} (H : s.nonempty)
{f : β → α} (g : α → γ) (g_inf : ∀ x y, g (x ⊓ y) = g x ⊓ g y) :
g (s.inf' H f) = s.inf' H (g ∘ f) :=
@comp_sup'_eq_sup'_comp αᵒᵈ _ γᵒᵈ _ _ _ H f g g_inf
lemma inf'_induction {p : α → Prop} (hp : ∀ a₁, p a₁ → ∀ a₂, p a₂ → p (a₁ ⊓ a₂))
(hs : ∀ b ∈ s, p (f b)) : p (s.inf' H f) :=
@sup'_induction αᵒᵈ _ _ _ H f _ hp hs
lemma inf'_mem (s : set α) (w : ∀ x y ∈ s, x ⊓ y ∈ s)
{ι : Type*} (t : finset ι) (H : t.nonempty) (p : ι → α) (h : ∀ i ∈ t, p i ∈ s) :
t.inf' H p ∈ s := inf'_induction H p w h
@[congr] lemma inf'_congr {t : finset β} {f g : β → α} (h₁ : s = t) (h₂ : ∀ x ∈ s, f x = g x) :
s.inf' H f = t.inf' (h₁ ▸ H) g :=
@sup'_congr αᵒᵈ _ _ _ H _ _ _ h₁ h₂
end inf'
section sup
variables [semilattice_sup α] [order_bot α]
lemma sup'_eq_sup {s : finset β} (H : s.nonempty) (f : β → α) : s.sup' H f = s.sup f :=
le_antisymm (sup'_le H f (λ b, le_sup)) (sup_le (λ b, le_sup' f))
lemma sup_closed_of_sup_closed {s : set α} (t : finset α) (htne : t.nonempty) (h_subset : ↑t ⊆ s)
(h : ∀ a b ∈ s, a ⊔ b ∈ s) : t.sup id ∈ s :=
sup'_eq_sup htne id ▸ sup'_induction _ _ h h_subset
lemma coe_sup_of_nonempty {s : finset β} (h : s.nonempty) (f : β → α) :
(↑(s.sup f) : with_bot α) = s.sup (coe ∘ f) :=
by simp only [←sup'_eq_sup h, coe_sup' h]
end sup
section inf
variables [semilattice_inf α] [order_top α]
lemma inf'_eq_inf {s : finset β} (H : s.nonempty) (f : β → α) : s.inf' H f = s.inf f :=
@sup'_eq_sup αᵒᵈ _ _ _ _ H f
lemma inf_closed_of_inf_closed {s : set α} (t : finset α) (htne : t.nonempty) (h_subset : ↑t ⊆ s)
(h : ∀ a b ∈ s, a ⊓ b ∈ s) : t.inf id ∈ s :=
@sup_closed_of_sup_closed αᵒᵈ _ _ _ t htne h_subset h
lemma coe_inf_of_nonempty {s : finset β} (h : s.nonempty) (f : β → α):
(↑(s.inf f) : with_top α) = s.inf (λ i, f i) :=
@coe_sup_of_nonempty αᵒᵈ _ _ _ _ h f
end inf
section sup
variables {C : β → Type*} [Π (b : β), semilattice_sup (C b)] [Π (b : β), order_bot (C b)]
@[simp]
protected lemma sup_apply (s : finset α) (f : α → Π (b : β), C b) (b : β) :
s.sup f b = s.sup (λ a, f a b) :=
comp_sup_eq_sup_comp (λ x : Π b : β, C b, x b) (λ i j, rfl) rfl
end sup
section inf
variables {C : β → Type*} [Π (b : β), semilattice_inf (C b)] [Π (b : β), order_top (C b)]
@[simp]
protected lemma inf_apply (s : finset α) (f : α → Π (b : β), C b) (b : β) :
s.inf f b = s.inf (λ a, f a b) :=
@finset.sup_apply _ _ (λ b, (C b)ᵒᵈ) _ _ s f b
end inf
section sup'
variables {C : β → Type*} [Π (b : β), semilattice_sup (C b)]
@[simp]
protected lemma sup'_apply {s : finset α} (H : s.nonempty) (f : α → Π (b : β), C b) (b : β) :
s.sup' H f b = s.sup' H (λ a, f a b) :=
comp_sup'_eq_sup'_comp H (λ x : Π b : β, C b, x b) (λ i j, rfl)
end sup'
section inf'
variables {C : β → Type*} [Π (b : β), semilattice_inf (C b)]
@[simp]
protected lemma inf'_apply {s : finset α} (H : s.nonempty) (f : α → Π (b : β), C b) (b : β) :
s.inf' H f b = s.inf' H (λ a, f a b) :=
@finset.sup'_apply _ _ (λ b, (C b)ᵒᵈ) _ _ H f b
end inf'
@[simp] lemma to_dual_sup' [semilattice_sup α] {s : finset ι} (hs : s.nonempty) (f : ι → α) :
to_dual (s.sup' hs f) = s.inf' hs (to_dual ∘ f) := rfl
@[simp] lemma to_dual_inf' [semilattice_inf α] {s : finset ι} (hs : s.nonempty) (f : ι → α) :
to_dual (s.inf' hs f) = s.sup' hs (to_dual ∘ f) := rfl
@[simp] lemma of_dual_sup' [semilattice_inf α] {s : finset ι} (hs : s.nonempty) (f : ι → αᵒᵈ) :
of_dual (s.sup' hs f) = s.inf' hs (of_dual ∘ f) := rfl
@[simp] lemma of_dual_inf' [semilattice_sup α] {s : finset ι} (hs : s.nonempty) (f : ι → αᵒᵈ) :
of_dual (s.inf' hs f) = s.sup' hs (of_dual ∘ f) := rfl
section linear_order
variables [linear_order α] {s : finset ι} (H : s.nonempty) {f : ι → α} {a : α}
@[simp] lemma le_sup'_iff : a ≤ s.sup' H f ↔ ∃ b ∈ s, a ≤ f b :=
begin
rw [←with_bot.coe_le_coe, coe_sup', finset.le_sup_iff (with_bot.bot_lt_coe a)],
exact bex_congr (λ b hb, with_bot.coe_le_coe),
end
@[simp] lemma lt_sup'_iff : a < s.sup' H f ↔ ∃ b ∈ s, a < f b :=
begin
rw [←with_bot.coe_lt_coe, coe_sup', finset.lt_sup_iff],
exact bex_congr (λ b hb, with_bot.coe_lt_coe),
end
@[simp] lemma sup'_lt_iff : s.sup' H f < a ↔ ∀ i ∈ s, f i < a :=
begin
rw [←with_bot.coe_lt_coe, coe_sup', finset.sup_lt_iff (with_bot.bot_lt_coe a)],
exact ball_congr (λ b hb, with_bot.coe_lt_coe),
end
@[simp] lemma inf'_le_iff : s.inf' H f ≤ a ↔ ∃ i ∈ s, f i ≤ a := @le_sup'_iff αᵒᵈ _ _ _ H f _
@[simp] lemma inf'_lt_iff : s.inf' H f < a ↔ ∃ i ∈ s, f i < a := @lt_sup'_iff αᵒᵈ _ _ _ H f _
@[simp] lemma lt_inf'_iff : a < s.inf' H f ↔ ∀ i ∈ s, a < f i := @sup'_lt_iff αᵒᵈ _ _ _ H f _
lemma exists_mem_eq_sup' (f : ι → α) : ∃ i, i ∈ s ∧ s.sup' H f = f i :=
begin
refine H.cons_induction (λ c, _) (λ c s hc hs ih, _),
{ exact ⟨c, mem_singleton_self c, rfl⟩, },
{ rcases ih with ⟨b, hb, h'⟩,
rw [sup'_cons hs, h'],
cases total_of (≤) (f b) (f c) with h h,
{ exact ⟨c, mem_cons.2 (or.inl rfl), sup_eq_left.2 h⟩, },
{ exact ⟨b, mem_cons.2 (or.inr hb), sup_eq_right.2 h⟩, }, },
end
lemma exists_mem_eq_inf' (f : ι → α) : ∃ i, i ∈ s ∧ s.inf' H f = f i :=
@exists_mem_eq_sup' αᵒᵈ _ _ _ H f
lemma exists_mem_eq_sup [order_bot α] (s : finset ι) (h : s.nonempty) (f : ι → α) :
∃ i, i ∈ s ∧ s.sup f = f i :=
sup'_eq_sup h f ▸ exists_mem_eq_sup' h f
lemma exists_mem_eq_inf [order_top α] (s : finset ι) (h : s.nonempty) (f : ι → α) :
∃ i, i ∈ s ∧ s.inf f = f i :=
@exists_mem_eq_sup αᵒᵈ _ _ _ _ h f
end linear_order
/-! ### max and min of finite sets -/
section max_min
variables [linear_order α]
/-- Let `s` be a finset in a linear order. Then `s.max` is the maximum of `s` if `s` is not empty,
and `⊥` otherwise. It belongs to `with_bot α`. If you want to get an element of `α`, see
`s.max'`. -/
protected def max (s : finset α) : with_bot α :=
sup s coe
theorem max_eq_sup_with_bot (s : finset α) :
s.max = sup s coe := rfl
@[simp] theorem max_empty : (∅ : finset α).max = ⊥ := rfl
@[simp] theorem max_insert {a : α} {s : finset α} :
(insert a s).max = max a s.max := fold_insert_idem
@[simp] theorem max_singleton {a : α} : finset.max {a} = (a : with_bot α) :=
by { rw [← insert_emptyc_eq], exact max_insert }
theorem max_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ (b : α), s.max = b :=
(@le_sup (with_bot α) _ _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst
theorem max_of_nonempty {s : finset α} (h : s.nonempty) : ∃ (a : α), s.max = a :=
let ⟨a, ha⟩ := h in max_of_mem ha
theorem max_eq_bot {s : finset α} : s.max = ⊥ ↔ s = ∅ :=
⟨λ h, s.eq_empty_or_nonempty.elim id
(λ H, let ⟨a, ha⟩ := max_of_nonempty H in by rw h at ha; cases ha),
λ h, h.symm ▸ max_empty⟩
theorem mem_of_max {s : finset α} : ∀ {a : α}, s.max = a → a ∈ s :=
finset.induction_on s (λ _ H, by cases H)
(λ b s _ (ih : ∀ {a : α}, s.max = a → a ∈ s) a (h : (insert b s).max = a),
begin
by_cases p : b = a,
{ induction p, exact mem_insert_self b s },
{ cases max_choice ↑b s.max with q q;
rw [max_insert, q] at h,
{ cases h, cases p rfl },
{ exact mem_insert_of_mem (ih h) } }
end)
lemma coe_le_max_of_mem {a : α} {s : finset α} (as : a ∈ s) : ↑a ≤ s.max :=
le_sup as
theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : s.max = b) : a ≤ b :=
with_bot.coe_le_coe.mp $ (coe_le_max_of_mem h₁).trans h₂.le
lemma max_mono {s t : finset α} (st : s ⊆ t) : s.max ≤ t.max :=
sup_mono st
lemma max_le {M : with_bot α} {s : finset α} (st : ∀ a : α, a ∈ s → (a : with_bot α) ≤ M) :
s.max ≤ M :=
sup_le st
/-- Let `s` be a finset in a linear order. Then `s.min` is the minimum of `s` if `s` is not empty,
and `⊤` otherwise. It belongs to `with_top α`. If you want to get an element of `α`, see
`s.min'`. -/
protected def min (s : finset α) : with_top α :=
inf s coe
theorem min_eq_inf_with_top (s : finset α) :
s.min = inf s coe := rfl
@[simp] theorem min_empty : (∅ : finset α).min = ⊤ := rfl
@[simp] theorem min_insert {a : α} {s : finset α} :
(insert a s).min = min ↑a s.min :=
fold_insert_idem
@[simp] theorem min_singleton {a : α} : finset.min {a} = (a : with_top α) :=
by { rw ← insert_emptyc_eq, exact min_insert }
theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b : α, s.min = b :=
(@inf_le (with_top α) _ _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst
theorem min_of_nonempty {s : finset α} (h : s.nonempty) : ∃ a : α, s.min = a :=
let ⟨a, ha⟩ := h in min_of_mem ha
theorem min_eq_top {s : finset α} : s.min = ⊤ ↔ s = ∅ :=
⟨λ h, s.eq_empty_or_nonempty.elim id
(λ H, let ⟨a, ha⟩ := min_of_nonempty H in by rw h at ha; cases ha),
λ h, h.symm ▸ min_empty⟩
theorem mem_of_min {s : finset α} : ∀ {a : α}, s.min = a → a ∈ s := @mem_of_max αᵒᵈ _ s
lemma min_le_coe_of_mem {a : α} {s : finset α} (as : a ∈ s) : s.min ≤ a :=
inf_le as
theorem min_le_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : s.min = a) : a ≤ b :=
with_top.coe_le_coe.mp $ h₂.ge.trans (min_le_coe_of_mem h₁)
lemma min_mono {s t : finset α} (st : s ⊆ t) : t.min ≤ s.min :=
inf_mono st
lemma le_min {m : with_top α} {s : finset α} (st : ∀ a : α, a ∈ s → m ≤ a) :
m ≤ s.min :=
le_inf st
/-- Given a nonempty finset `s` in a linear order `α `, then `s.min' h` is its minimum, as an
element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.min`,
taking values in `with_top α`. -/
def min' (s : finset α) (H : s.nonempty) : α :=
with_top.untop s.min $ mt min_eq_top.1 H.ne_empty
/-- Given a nonempty finset `s` in a linear order `α `, then `s.max' h` is its maximum, as an
element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.max`,
taking values in `with_bot α`. -/
def max' (s : finset α) (H : s.nonempty) : α :=
with_bot.unbot s.max $
let ⟨k, hk⟩ := H in
let ⟨b, hb⟩ := max_of_mem hk in by simp [hb]
variables (s : finset α) (H : s.nonempty) {x : α}
theorem min'_mem : s.min' H ∈ s := mem_of_min $ by simp [min']
theorem min'_le (x) (H2 : x ∈ s) : s.min' ⟨x, H2⟩ ≤ x :=
min_le_of_mem H2 (with_top.coe_untop _ _).symm
theorem le_min' (x) (H2 : ∀ y ∈ s, x ≤ y) : x ≤ s.min' H := H2 _ $ min'_mem _ _
theorem is_least_min' : is_least ↑s (s.min' H) := ⟨min'_mem _ _, min'_le _⟩
@[simp] lemma le_min'_iff {x} : x ≤ s.min' H ↔ ∀ y ∈ s, x ≤ y :=
le_is_glb_iff (is_least_min' s H).is_glb
/-- `{a}.min' _` is `a`. -/
@[simp] lemma min'_singleton (a : α) :
({a} : finset α).min' (singleton_nonempty _) = a :=
by simp [min']
theorem max'_mem : s.max' H ∈ s := mem_of_max $ by simp [max']
theorem le_max' (x) (H2 : x ∈ s) : x ≤ s.max' ⟨x, H2⟩ :=
le_max_of_mem H2 (with_bot.coe_unbot _ _).symm
theorem max'_le (x) (H2 : ∀ y ∈ s, y ≤ x) : s.max' H ≤ x := H2 _ $ max'_mem _ _
theorem is_greatest_max' : is_greatest ↑s (s.max' H) := ⟨max'_mem _ _, le_max' _⟩
@[simp] lemma max'_le_iff {x} : s.max' H ≤ x ↔ ∀ y ∈ s, y ≤ x :=
is_lub_le_iff (is_greatest_max' s H).is_lub
@[simp] lemma max'_lt_iff {x} : s.max' H < x ↔ ∀ y ∈ s, y < x :=
⟨λ Hlt y hy, (s.le_max' y hy).trans_lt Hlt, λ H, H _ $ s.max'_mem _⟩
@[simp] lemma lt_min'_iff : x < s.min' H ↔ ∀ y ∈ s, x < y := @max'_lt_iff αᵒᵈ _ _ H _
lemma max'_eq_sup' : s.max' H = s.sup' H id :=
eq_of_forall_ge_iff $ λ a, (max'_le_iff _ _).trans (sup'_le_iff _ _).symm
lemma min'_eq_inf' : s.min' H = s.inf' H id := @max'_eq_sup' αᵒᵈ _ s H
/-- `{a}.max' _` is `a`. -/
@[simp] lemma max'_singleton (a : α) :
({a} : finset α).max' (singleton_nonempty _) = a :=
by simp [max']
theorem min'_lt_max' {i j} (H1 : i ∈ s) (H2 : j ∈ s) (H3 : i ≠ j) :
s.min' ⟨i, H1⟩ < s.max' ⟨i, H1⟩ :=
is_glb_lt_is_lub_of_ne (s.is_least_min' _).is_glb (s.is_greatest_max' _).is_lub H1 H2 H3
/--
If there's more than 1 element, the min' is less than the max'. An alternate version of
`min'_lt_max'` which is sometimes more convenient.
-/
lemma min'_lt_max'_of_card (h₂ : 1 < card s) :
s.min' (finset.card_pos.mp $ lt_trans zero_lt_one h₂) <
s.max' (finset.card_pos.mp $ lt_trans zero_lt_one h₂) :=
begin
rcases one_lt_card.1 h₂ with ⟨a, ha, b, hb, hab⟩,
exact s.min'_lt_max' ha hb hab
end
lemma map_of_dual_min (s : finset αᵒᵈ) : s.min.map of_dual = (s.image of_dual).max :=
by { rw [max_eq_sup_with_bot, sup_image], exact congr_fun option.map_id _ }
lemma map_of_dual_max (s : finset αᵒᵈ) : s.max.map of_dual = (s.image of_dual).min :=
by { rw [min_eq_inf_with_top, inf_image], exact congr_fun option.map_id _ }
lemma map_to_dual_min (s : finset α) : s.min.map to_dual = (s.image to_dual).max :=
by { rw [max_eq_sup_with_bot, sup_image], exact congr_fun option.map_id _ }
lemma map_to_dual_max (s : finset α) : s.max.map to_dual = (s.image to_dual).min :=
by { rw [min_eq_inf_with_top, inf_image], exact congr_fun option.map_id _ }
lemma of_dual_min' {s : finset αᵒᵈ} (hs : s.nonempty) :
of_dual (min' s hs) = max' (s.image of_dual) (hs.image _) :=
by { convert rfl, exact image_id }
lemma of_dual_max' {s : finset αᵒᵈ} (hs : s.nonempty) :
of_dual (max' s hs) = min' (s.image of_dual) (hs.image _) :=
by { convert rfl, exact image_id }
lemma to_dual_min' {s : finset α} (hs : s.nonempty) :
to_dual (min' s hs) = max' (s.image to_dual) (hs.image _) :=
by { convert rfl, exact image_id }
lemma to_dual_max' {s : finset α} (hs : s.nonempty) :
to_dual (max' s hs) = min' (s.image to_dual) (hs.image _) :=
by { convert rfl, exact image_id }
lemma max'_subset {s t : finset α} (H : s.nonempty) (hst : s ⊆ t) :
s.max' H ≤ t.max' (H.mono hst) :=
le_max' _ _ (hst (s.max'_mem H))
lemma min'_subset {s t : finset α} (H : s.nonempty) (hst : s ⊆ t) :
t.min' (H.mono hst) ≤ s.min' H :=
min'_le _ _ (hst (s.min'_mem H))
lemma max'_insert (a : α) (s : finset α) (H : s.nonempty) :
(insert a s).max' (s.insert_nonempty a) = max (s.max' H) a :=
(is_greatest_max' _ _).unique $
by { rw [coe_insert, max_comm], exact (is_greatest_max' _ _).insert _ }
lemma min'_insert (a : α) (s : finset α) (H : s.nonempty) :
(insert a s).min' (s.insert_nonempty a) = min (s.min' H) a :=
(is_least_min' _ _).unique $
by { rw [coe_insert, min_comm], exact (is_least_min' _ _).insert _ }
lemma lt_max'_of_mem_erase_max' [decidable_eq α] {a : α} (ha : a ∈ s.erase (s.max' H)) :
a < s.max' H :=
lt_of_le_of_ne (le_max' _ _ (mem_of_mem_erase ha)) $ ne_of_mem_of_not_mem ha $ not_mem_erase _ _
lemma min'_lt_of_mem_erase_min' [decidable_eq α] {a : α} (ha : a ∈ s.erase (s.min' H)) :
s.min' H < a :=
@lt_max'_of_mem_erase_max' αᵒᵈ _ s H _ a ha
@[simp] lemma max'_image [linear_order β]
{f : α → β} (hf : monotone f) (s : finset α) (h : (s.image f).nonempty) :
(s.image f).max' h = f (s.max' ((nonempty.image_iff f).mp h)) :=
begin
refine le_antisymm (max'_le _ _ _ (λ y hy, _))
(le_max' _ _ (mem_image.mpr ⟨_, max'_mem _ _, rfl⟩)),
obtain ⟨x, hx, rfl⟩ := mem_image.mp hy,
exact hf (le_max' _ _ hx)
end
@[simp] lemma min'_image [linear_order β]
{f : α → β} (hf : monotone f) (s : finset α) (h : (s.image f).nonempty) :
(s.image f).min' h = f (s.min' ((nonempty.image_iff f).mp h)) :=
begin
refine le_antisymm (min'_le _ _ (mem_image.mpr ⟨_, min'_mem _ _, rfl⟩))
(le_min' _ _ _ (λ y hy, _)),
obtain ⟨x, hx, rfl⟩ := mem_image.mp hy,
exact hf (min'_le _ _ hx)
end
/-- Induction principle for `finset`s in a linearly ordered type: a predicate is true on all
`s : finset α` provided that:
* it is true on the empty `finset`,
* for every `s : finset α` and an element `a` strictly greater than all elements of `s`, `p s`
implies `p (insert a s)`. -/
@[elab_as_eliminator]
lemma induction_on_max [decidable_eq α] {p : finset α → Prop} (s : finset α) (h0 : p ∅)
(step : ∀ a s, (∀ x ∈ s, x < a) → p s → p (insert a s)) : p s :=
begin
induction s using finset.strong_induction_on with s ihs,
rcases s.eq_empty_or_nonempty with rfl|hne,
{ exact h0 },
{ have H : s.max' hne ∈ s, from max'_mem s hne,
rw ← insert_erase H,
exact step _ _ (λ x, s.lt_max'_of_mem_erase_max' hne) (ihs _ $ erase_ssubset H) }
end
/-- Induction principle for `finset`s in a linearly ordered type: a predicate is true on all
`s : finset α` provided that:
* it is true on the empty `finset`,
* for every `s : finset α` and an element `a` strictly less than all elements of `s`, `p s`
implies `p (insert a s)`. -/
@[elab_as_eliminator]
lemma induction_on_min [decidable_eq α] {p : finset α → Prop} (s : finset α) (h0 : p ∅)
(step : ∀ a s, (∀ x ∈ s, a < x) → p s → p (insert a s)) : p s :=
@induction_on_max αᵒᵈ _ _ _ s h0 step
end max_min
section max_min_induction_value
variables [linear_order α] [linear_order β]
/-- Induction principle for `finset`s in any type from which a given function `f` maps to a linearly
ordered type : a predicate is true on all `s : finset α` provided that:
* it is true on the empty `finset`,
* for every `s : finset α` and an element `a` such that for elements of `s` denoted by `x` we have
`f x ≤ f a`, `p s` implies `p (insert a s)`. -/
@[elab_as_eliminator]
lemma induction_on_max_value [decidable_eq ι] (f : ι → α)
{p : finset ι → Prop} (s : finset ι) (h0 : p ∅)
(step : ∀ a s, a ∉ s → (∀ x ∈ s, f x ≤ f a) → p s → p (insert a s)) : p s :=
begin
induction s using finset.strong_induction_on with s ihs,
rcases (s.image f).eq_empty_or_nonempty with hne|hne,
{ simp only [image_eq_empty] at hne,
simp only [hne, h0] },
{ have H : (s.image f).max' hne ∈ (s.image f), from max'_mem (s.image f) hne,
simp only [mem_image, exists_prop] at H,
rcases H with ⟨a, has, hfa⟩,
rw ← insert_erase has,
refine step _ _ (not_mem_erase a s) (λ x hx, _) (ihs _ $ erase_ssubset has),
rw hfa,
exact le_max' _ _ (mem_image_of_mem _ $ mem_of_mem_erase hx) }
end
/-- Induction principle for `finset`s in any type from which a given function `f` maps to a linearly
ordered type : a predicate is true on all `s : finset α` provided that:
* it is true on the empty `finset`,
* for every `s : finset α` and an element `a` such that for elements of `s` denoted by `x` we have
`f a ≤ f x`, `p s` implies `p (insert a s)`. -/
@[elab_as_eliminator]
lemma induction_on_min_value [decidable_eq ι] (f : ι → α)
{p : finset ι → Prop} (s : finset ι) (h0 : p ∅)
(step : ∀ a s, a ∉ s → (∀ x ∈ s, f a ≤ f x) → p s → p (insert a s)) : p s :=
@induction_on_max_value αᵒᵈ ι _ _ _ _ s h0 step
end max_min_induction_value
section exists_max_min
variables [linear_order α]
lemma exists_max_image (s : finset β) (f : β → α) (h : s.nonempty) :
∃ x ∈ s, ∀ x' ∈ s, f x' ≤ f x :=
begin
cases max_of_nonempty (h.image f) with y hy,
rcases mem_image.mp (mem_of_max hy) with ⟨x, hx, rfl⟩,
exact ⟨x, hx, λ x' hx', le_max_of_mem (mem_image_of_mem f hx') hy⟩,
end
lemma exists_min_image (s : finset β) (f : β → α) (h : s.nonempty) :
∃ x ∈ s, ∀ x' ∈ s, f x ≤ f x' :=
@exists_max_image αᵒᵈ β _ s f h
end exists_max_min
end finset
namespace multiset
lemma map_finset_sup [decidable_eq α] [decidable_eq β]
(s : finset γ) (f : γ → multiset β) (g : β → α) (hg : function.injective g) :
map g (s.sup f) = s.sup (map g ∘ f) :=
finset.comp_sup_eq_sup_comp _ (λ _ _, map_union hg) (map_zero _)
lemma count_finset_sup [decidable_eq β] (s : finset α) (f : α → multiset β) (b : β) :
count b (s.sup f) = s.sup (λa, count b (f a)) :=
begin
letI := classical.dec_eq α,
refine s.induction _ _,
{ exact count_zero _ },
{ assume i s his ih,
rw [finset.sup_insert, sup_eq_union, count_union, finset.sup_insert, ih],
refl }
end
lemma mem_sup {α β} [decidable_eq β] {s : finset α} {f : α → multiset β}
{x : β} : x ∈ s.sup f ↔ ∃ v ∈ s, x ∈ f v :=
begin
classical,
apply s.induction_on,
{ simp },
{ intros a s has hxs,
rw [finset.sup_insert, multiset.sup_eq_union, multiset.mem_union],
split,
{ intro hxi,
cases hxi with hf hf,
{ refine ⟨a, _, hf⟩,
simp only [true_or, eq_self_iff_true, finset.mem_insert] },
{ rcases hxs.mp hf with ⟨v, hv, hfv⟩,
refine ⟨v, _, hfv⟩,
simp only [hv, or_true, finset.mem_insert] } },
{ rintros ⟨v, hv, hfv⟩,
rw [finset.mem_insert] at hv,
rcases hv with rfl | hv,
{ exact or.inl hfv },
{ refine or.inr (hxs.mpr ⟨v, hv, hfv⟩) } } },
end
end multiset
namespace finset
lemma mem_sup {α β} [decidable_eq β] {s : finset α} {f : α → finset β}
{x : β} : x ∈ s.sup f ↔ ∃ v ∈ s, x ∈ f v :=
begin
change _ ↔ ∃ v ∈ s, x ∈ (f v).val,
rw [←multiset.mem_sup, ←multiset.mem_to_finset, sup_to_finset],
simp_rw [val_to_finset],
end
lemma sup_eq_bUnion {α β} [decidable_eq β] (s : finset α) (t : α → finset β) :
s.sup t = s.bUnion t :=
by { ext, rw [mem_sup, mem_bUnion], }
@[simp] lemma sup_singleton'' [decidable_eq α] (s : finset β) (f : β → α) :
s.sup (λ b, {f b}) = s.image f :=
by { ext a, rw [mem_sup, mem_image], simp only [mem_singleton, eq_comm] }
@[simp] lemma sup_singleton' [decidable_eq α] (s : finset α) : s.sup singleton = s :=
(s.sup_singleton'' _).trans image_id
end finset
section lattice
variables {ι' : Sort*} [complete_lattice α]
/-- Supremum of `s i`, `i : ι`, is equal to the supremum over `t : finset ι` of suprema
`⨆ i ∈ t, s i`. This version assumes `ι` is a `Type*`. See `supr_eq_supr_finset'` for a version
that works for `ι : Sort*`. -/
lemma supr_eq_supr_finset (s : ι → α) :
(⨆i, s i) = (⨆t:finset ι, ⨆i∈t, s i) :=
begin
classical,
exact le_antisymm
(supr_le $ assume b, le_supr_of_le {b} $ le_supr_of_le b $ le_supr_of_le
(by simp) $ le_rfl)
(supr_le $ assume t, supr_le $ assume b, supr_le $ assume hb, le_supr _ _)
end
/-- Supremum of `s i`, `i : ι`, is equal to the supremum over `t : finset ι` of suprema
`⨆ i ∈ t, s i`. This version works for `ι : Sort*`. See `supr_eq_supr_finset` for a version
that assumes `ι : Type*` but has no `plift`s. -/
lemma supr_eq_supr_finset' (s : ι' → α) :
(⨆i, s i) = (⨆t:finset (plift ι'), ⨆i∈t, s (plift.down i)) :=
by rw [← supr_eq_supr_finset, ← equiv.plift.surjective.supr_comp]; refl
/-- Infimum of `s i`, `i : ι`, is equal to the infimum over `t : finset ι` of infima
`⨅ i ∈ t, s i`. This version assumes `ι` is a `Type*`. See `infi_eq_infi_finset'` for a version
that works for `ι : Sort*`. -/
lemma infi_eq_infi_finset (s : ι → α) : (⨅ i, s i) = ⨅ (t : finset ι) (i ∈ t), s i :=
@supr_eq_supr_finset αᵒᵈ _ _ _
/-- Infimum of `s i`, `i : ι`, is equal to the infimum over `t : finset ι` of infima
`⨅ i ∈ t, s i`. This version works for `ι : Sort*`. See `infi_eq_infi_finset` for a version
that assumes `ι : Type*` but has no `plift`s. -/
lemma infi_eq_infi_finset' (s : ι' → α) :
(⨅i, s i) = (⨅t:finset (plift ι'), ⨅i∈t, s (plift.down i)) :=
@supr_eq_supr_finset' αᵒᵈ _ _ _
end lattice
namespace set
variables {ι' : Sort*}
/-- Union of an indexed family of sets `s : ι → set α` is equal to the union of the unions
of finite subfamilies. This version assumes `ι : Type*`. See also `Union_eq_Union_finset'` for
a version that works for `ι : Sort*`. -/
lemma Union_eq_Union_finset (s : ι → set α) :
(⋃i, s i) = (⋃t:finset ι, ⋃i∈t, s i) :=
supr_eq_supr_finset s
/-- Union of an indexed family of sets `s : ι → set α` is equal to the union of the unions
of finite subfamilies. This version works for `ι : Sort*`. See also `Union_eq_Union_finset` for
a version that assumes `ι : Type*` but avoids `plift`s in the right hand side. -/
lemma Union_eq_Union_finset' (s : ι' → set α) :
(⋃i, s i) = (⋃t:finset (plift ι'), ⋃i∈t, s (plift.down i)) :=
supr_eq_supr_finset' s
/-- Intersection of an indexed family of sets `s : ι → set α` is equal to the intersection of the
intersections of finite subfamilies. This version assumes `ι : Type*`. See also
`Inter_eq_Inter_finset'` for a version that works for `ι : Sort*`. -/
lemma Inter_eq_Inter_finset (s : ι → set α) :
(⋂i, s i) = (⋂t:finset ι, ⋂i∈t, s i) :=
infi_eq_infi_finset s
/-- Intersection of an indexed family of sets `s : ι → set α` is equal to the intersection of the
intersections of finite subfamilies. This version works for `ι : Sort*`. See also
`Inter_eq_Inter_finset` for a version that assumes `ι : Type*` but avoids `plift`s in the right
hand side. -/
lemma Inter_eq_Inter_finset' (s : ι' → set α) :
(⋂i, s i) = (⋂t:finset (plift ι'), ⋂i∈t, s (plift.down i)) :=
infi_eq_infi_finset' s
end set
namespace finset
/-! ### Interaction with ordered algebra structures -/
lemma sup_mul_le_mul_sup_of_nonneg [linear_ordered_semiring α] [order_bot α]
{a b : ι → α} (s : finset ι) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) :
s.sup (a * b) ≤ s.sup a * s.sup b :=
finset.sup_le $ λ i hi, mul_le_mul (le_sup hi) (le_sup hi) (hb _ hi) ((ha _ hi).trans $ le_sup hi)
lemma mul_inf_le_inf_mul_of_nonneg [linear_ordered_semiring α] [order_top α]
{a b : ι → α} (s : finset ι) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) :
s.inf a * s.inf b ≤ s.inf (a * b) :=
finset.le_inf $ λ i hi, mul_le_mul (inf_le hi) (inf_le hi) (finset.le_inf hb) (ha i hi)
lemma sup'_mul_le_mul_sup'_of_nonneg [linear_ordered_semiring α]
{a b : ι → α} (s : finset ι) (H : s.nonempty) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) :
s.sup' H (a * b) ≤ s.sup' H a * s.sup' H b :=
sup'_le _ _ $ λ i hi,
mul_le_mul (le_sup' _ hi) (le_sup' _ hi) (hb _ hi) ((ha _ hi).trans $ le_sup' _ hi)
lemma inf'_mul_le_mul_inf'_of_nonneg [linear_ordered_semiring α]
{a b : ι → α} (s : finset ι) (H : s.nonempty) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) :
s.inf' H a * s.inf' H b ≤ s.inf' H (a * b) :=
le_inf' _ _ $ λ i hi, mul_le_mul (inf'_le _ hi) (inf'_le _ hi) (le_inf' _ _ hb) (ha _ hi)
open function
/-! ### Interaction with big lattice/set operations -/
section lattice
lemma supr_coe [has_Sup β] (f : α → β) (s : finset α) :
(⨆ x ∈ (↑s : set α), f x) = ⨆ x ∈ s, f x :=
rfl
lemma infi_coe [has_Inf β] (f : α → β) (s : finset α) :
(⨅ x ∈ (↑s : set α), f x) = ⨅ x ∈ s, f x :=
rfl
variables [complete_lattice β]
theorem supr_singleton (a : α) (s : α → β) : (⨆ x ∈ ({a} : finset α), s x) = s a :=
by simp
theorem infi_singleton (a : α) (s : α → β) : (⨅ x ∈ ({a} : finset α), s x) = s a :=
by simp
lemma supr_option_to_finset (o : option α) (f : α → β) :
(⨆ x ∈ o.to_finset, f x) = ⨆ x ∈ o, f x :=
by simp
lemma infi_option_to_finset (o : option α) (f : α → β) : (⨅ x ∈ o.to_finset, f x) = ⨅ x ∈ o, f x :=
@supr_option_to_finset _ βᵒᵈ _ _ _
variables [decidable_eq α]
theorem supr_union {f : α → β} {s t : finset α} :
(⨆ x ∈ s ∪ t, f x) = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) :=
by simp [supr_or, supr_sup_eq]
theorem infi_union {f : α → β} {s t : finset α} :
(⨅ x ∈ s ∪ t, f x) = (⨅ x ∈ s, f x) ⊓ (⨅ x ∈ t, f x) :=
@supr_union α βᵒᵈ _ _ _ _ _
lemma supr_insert (a : α) (s : finset α) (t : α → β) :
(⨆ x ∈ insert a s, t x) = t a ⊔ (⨆ x ∈ s, t x) :=
by { rw insert_eq, simp only [supr_union, finset.supr_singleton] }
lemma infi_insert (a : α) (s : finset α) (t : α → β) :
(⨅ x ∈ insert a s, t x) = t a ⊓ (⨅ x ∈ s, t x) :=
@supr_insert α βᵒᵈ _ _ _ _ _
lemma supr_finset_image {f : γ → α} {g : α → β} {s : finset γ} :
(⨆ x ∈ s.image f, g x) = (⨆ y ∈ s, g (f y)) :=
by rw [← supr_coe, coe_image, supr_image, supr_coe]
lemma sup_finset_image {β γ : Type*} [semilattice_sup β] [order_bot β]
(f : γ → α) (g : α → β) (s : finset γ) :
(s.image f).sup g = s.sup (g ∘ f) :=
begin
classical,
induction s using finset.induction_on with a s' ha ih; simp *
end
lemma infi_finset_image {f : γ → α} {g : α → β} {s : finset γ} :
(⨅ x ∈ s.image f, g x) = (⨅ y ∈ s, g (f y)) :=
by rw [← infi_coe, coe_image, infi_image, infi_coe]
lemma supr_insert_update {x : α} {t : finset α} (f : α → β) {s : β} (hx : x ∉ t) :
(⨆ (i ∈ insert x t), function.update f x s i) = (s ⊔ ⨆ (i ∈ t), f i) :=
begin
simp only [finset.supr_insert, update_same],
rcongr i hi, apply update_noteq, rintro rfl, exact hx hi
end
lemma infi_insert_update {x : α} {t : finset α} (f : α → β) {s : β} (hx : x ∉ t) :
(⨅ (i ∈ insert x t), update f x s i) = (s ⊓ ⨅ (i ∈ t), f i) :=
@supr_insert_update α βᵒᵈ _ _ _ _ f _ hx
lemma supr_bUnion (s : finset γ) (t : γ → finset α) (f : α → β) :
(⨆ y ∈ s.bUnion t, f y) = ⨆ (x ∈ s) (y ∈ t x), f y :=
by simp [@supr_comm _ α, supr_and]
lemma infi_bUnion (s : finset γ) (t : γ → finset α) (f : α → β) :
(⨅ y ∈ s.bUnion t, f y) = ⨅ (x ∈ s) (y ∈ t x), f y :=
@supr_bUnion _ βᵒᵈ _ _ _ _ _ _
end lattice
theorem set_bUnion_coe (s : finset α) (t : α → set β) :
(⋃ x ∈ (↑s : set α), t x) = ⋃ x ∈ s, t x :=
rfl
theorem set_bInter_coe (s : finset α) (t : α → set β) :
(⋂ x ∈ (↑s : set α), t x) = ⋂ x ∈ s, t x :=
rfl
theorem set_bUnion_singleton (a : α) (s : α → set β) :
(⋃ x ∈ ({a} : finset α), s x) = s a :=
supr_singleton a s
theorem set_bInter_singleton (a : α) (s : α → set β) :
(⋂ x ∈ ({a} : finset α), s x) = s a :=
infi_singleton a s
@[simp] lemma set_bUnion_preimage_singleton (f : α → β) (s : finset β) :
(⋃ y ∈ s, f ⁻¹' {y}) = f ⁻¹' s :=
set.bUnion_preimage_singleton f s
lemma set_bUnion_option_to_finset (o : option α) (f : α → set β) :
(⋃ x ∈ o.to_finset, f x) = ⋃ x ∈ o, f x :=
supr_option_to_finset o f
lemma set_bInter_option_to_finset (o : option α) (f : α → set β) :
(⋂ x ∈ o.to_finset, f x) = ⋂ x ∈ o, f x :=
infi_option_to_finset o f
lemma subset_set_bUnion_of_mem {s : finset α} {f : α → set β} {x : α} (h : x ∈ s) :
f x ⊆ ⋃ (y ∈ s), f y :=
show f x ≤ (⨆ y ∈ s, f y), from le_supr_of_le x $ le_supr _ h
variables [decidable_eq α]
lemma set_bUnion_union (s t : finset α) (u : α → set β) :
(⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) :=
supr_union
lemma set_bInter_inter (s t : finset α) (u : α → set β) :
(⋂ x ∈ s ∪ t, u x) = (⋂ x ∈ s, u x) ∩ (⋂ x ∈ t, u x) :=
infi_union
lemma set_bUnion_insert (a : α) (s : finset α) (t : α → set β) :
(⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) :=
supr_insert a s t
lemma set_bInter_insert (a : α) (s : finset α) (t : α → set β) :
(⋂ x ∈ insert a s, t x) = t a ∩ (⋂ x ∈ s, t x) :=
infi_insert a s t
lemma set_bUnion_finset_image {f : γ → α} {g : α → set β} {s : finset γ} :
(⋃x ∈ s.image f, g x) = (⋃y ∈ s, g (f y)) :=
supr_finset_image
lemma set_bInter_finset_image {f : γ → α} {g : α → set β} {s : finset γ} :
(⋂ x ∈ s.image f, g x) = (⋂ y ∈ s, g (f y)) :=
infi_finset_image
lemma set_bUnion_insert_update {x : α} {t : finset α} (f : α → set β) {s : set β} (hx : x ∉ t) :
(⋃ (i ∈ insert x t), @update _ _ _ f x s i) = (s ∪ ⋃ (i ∈ t), f i) :=
supr_insert_update f hx
lemma set_bInter_insert_update {x : α} {t : finset α} (f : α → set β) {s : set β} (hx : x ∉ t) :
(⋂ (i ∈ insert x t), @update _ _ _ f x s i) = (s ∩ ⋂ (i ∈ t), f i) :=
infi_insert_update f hx
lemma set_bUnion_bUnion (s : finset γ) (t : γ → finset α) (f : α → set β) :
(⋃ y ∈ s.bUnion t, f y) = ⋃ (x ∈ s) (y ∈ t x), f y :=
supr_bUnion s t f
lemma set_bInter_bUnion (s : finset γ) (t : γ → finset α) (f : α → set β) :
(⋂ y ∈ s.bUnion t, f y) = ⋂ (x ∈ s) (y ∈ t x), f y :=
infi_bUnion s t f
end finset
|
cf13f371cd901bdbc79bc5c39bf77c7dc925fc0e | 37683ecbb27d7c2037bfd9ad7e06d662f460a005 | /higher_groups.hlean | 1c959b6fd6f2faa2c9e8835c32e8a456a072e5d8 | [
"Apache-2.0"
] | permissive | GRSEB9S/Spectral-1 | b2443b09cae7aac1247b1f88c846c532ac802b8e | dd14277e0bfc6270a488eb3b9ec231484065b9d8 | refs/heads/master | 1,631,315,269,407 | 1,522,048,315,000 | 1,522,799,803,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 18,272 | hlean | /-
Copyright (c) 2015 Ulrik Buchholtz, Egbert Rijke and Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Ulrik Buchholtz, Egbert Rijke, Floris van Doorn
Formalization of the higher groups paper
-/
import .homotopy.EM
open eq is_conn pointed is_trunc trunc equiv is_equiv trunc_index susp nat algebra
prod.ops sigma sigma.ops category EM
namespace higher_group
set_option pp.binder_types true
universe variable u
/- Results not necessarily about higher groups which we repeat here, because they are mentioned in
the higher group paper -/
namespace hide
open pushout
definition connect_intro_pequiv {k : ℕ} {B : Type*} (A : Type*) (H : is_conn k B) :
ppmap B (connect k A) ≃* ppmap B A :=
is_conn.connect_intro_pequiv A H
definition is_conn_fun_prod_of_wedge (n m : ℕ) (A B : Type*)
[cA : is_conn n A] [cB : is_conn m B] : is_conn_fun (m + n) (@prod_of_wedge A B) :=
is_conn_fun_prod_of_wedge n m A B
definition is_trunc_ppi_of_is_conn (k n : ℕ) (X : Type*) (H : is_conn (k.-1) X)
(Y : X → Type*) (H3 : Πx, is_trunc (k + n) (Y x)) :
is_trunc n (Π*(x : X), Y x) :=
is_conn.is_trunc_ppi_of_is_conn _ (k.-2) _ _ (le_of_eq (sub_one_add_plus_two_sub_one k n)⁻¹) _ H3
end hide
/- The k-groupal n-types.
We require that the carrier has a point (preserved by the equivalence) -/
structure GType (n k : ℕ) : Type := /- (n,k)GType, denoted here as [n;k]GType -/
(car : ptrunctype.{u} n)
(B : pconntype.{u} (k.-1)) /- this is Bᵏ -/
(e : car ≃* Ω[k] B)
structure InfGType (k : ℕ) : Type := /- (∞,k)GType, denoted here as [∞;k]GType -/
(car : pType.{u})
(B : pconntype.{u} (k.-1)) /- this is Bᵏ -/
(e : car ≃* Ω[k] B)
structure ωGType (n : ℕ) := /- (n,ω)GType, denoted here as [n;ω]GType -/
(B : Π(k : ℕ), (n+k)-Type*[k.-1])
(e : Π(k : ℕ), B k ≃* Ω (B (k+1)))
attribute InfGType.car GType.car [coercion]
variables {n k k' l : ℕ}
notation `[`:95 n:0 `; ` k `]GType`:0 := GType n k
notation `[∞; `:95 k:0 `]GType`:0 := InfGType k
notation `[`:95 n:0 `;ω]GType`:0 := ωGType n
open GType
open InfGType (renaming B→iB e→ie)
open ωGType (renaming B→oB e→oe)
/- some basic properties -/
lemma is_trunc_B' (G : [n;k]GType) : is_trunc (k+n) (B G) :=
begin
apply is_trunc_of_is_trunc_loopn,
exact is_trunc_equiv_closed _ (e G),
exact _
end
lemma is_trunc_B (G : [n;k]GType) : is_trunc (n+k) (B G) :=
transport (λm, is_trunc m (B G)) (add.comm k n) (is_trunc_B' G)
local attribute [instance] is_trunc_B
definition GType.sigma_char (n k : ℕ) :
GType.{u} n k ≃ Σ(B : pconntype.{u} (k.-1)), Σ(X : ptrunctype.{u} n), X ≃* Ω[k] B :=
begin
fapply equiv.MK,
{ intro G, exact ⟨B G, G, e G⟩ },
{ intro v, exact GType.mk v.2.1 v.1 v.2.2 },
{ intro v, induction v with v₁ v₂, induction v₂, reflexivity },
{ intro G, induction G, reflexivity },
end
definition GType_equiv (n k : ℕ) : [n;k]GType ≃ (n+k)-Type*[k.-1] :=
GType.sigma_char n k ⬝e
sigma_equiv_of_is_embedding_left_contr
ptruncconntype.to_pconntype
(is_embedding_ptruncconntype_to_pconntype (n+k) (k.-1))
begin
intro X,
apply is_trunc_equiv_closed_rev -2,
{ apply sigma_equiv_sigma_right, intro B',
refine _ ⬝e (ptrunctype_eq_equiv B' (ptrunctype.mk (Ω[k] X) !is_trunc_loopn_nat pt))⁻¹ᵉ,
assert lem : Π(A : n-Type*) (B : Type*) (H : is_trunc n B),
(A ≃* B) ≃ (A ≃* (ptrunctype.mk B H pt)),
{ intro A B'' H, induction B'', reflexivity },
apply lem }
end
begin
intro B' H, apply fiber.mk (ptruncconntype.mk B' (is_trunc_B (GType.mk H.1 B' H.2)) pt _),
induction B' with G' B' e', reflexivity
end
definition GType_equiv_pequiv {n k : ℕ} (G : [n;k]GType) : GType_equiv n k G ≃* B G :=
by reflexivity
definition GType_eq_equiv {n k : ℕ} (G H : [n;k]GType) : (G = H :> [n;k]GType) ≃ (B G ≃* B H) :=
eq_equiv_fn_eq_of_equiv (GType_equiv n k) _ _ ⬝e !ptruncconntype_eq_equiv
definition GType_eq {n k : ℕ} {G H : [n;k]GType} (e : B G ≃* B H) : G = H :=
(GType_eq_equiv G H)⁻¹ᵉ e
/- similar properties for [∞;k]GType -/
definition InfGType.sigma_char (k : ℕ) :
InfGType.{u} k ≃ Σ(B : pconntype.{u} (k.-1)), Σ(X : pType.{u}), X ≃* Ω[k] B :=
begin
fapply equiv.MK,
{ intro G, exact ⟨iB G, G, ie G⟩ },
{ intro v, exact InfGType.mk v.2.1 v.1 v.2.2 },
{ intro v, induction v with v₁ v₂, induction v₂, reflexivity },
{ intro G, induction G, reflexivity },
end
definition InfGType_equiv (k : ℕ) : [∞;k]GType ≃ Type*[k.-1] :=
InfGType.sigma_char k ⬝e
@sigma_equiv_of_is_contr_right _ _
(λX, is_trunc_equiv_closed_rev -2 (sigma_equiv_sigma_right (λB', !pType_eq_equiv⁻¹ᵉ)))
definition InfGType_equiv_pequiv {k : ℕ} (G : [∞;k]GType) : InfGType_equiv k G ≃* iB G :=
by reflexivity
definition InfGType_eq_equiv {k : ℕ} (G H : [∞;k]GType) : (G = H :> [∞;k]GType) ≃ (iB G ≃* iB H) :=
eq_equiv_fn_eq_of_equiv (InfGType_equiv k) _ _ ⬝e !pconntype_eq_equiv
definition InfGType_eq {k : ℕ} {G H : [∞;k]GType} (e : iB G ≃* iB H) : G = H :=
(InfGType_eq_equiv G H)⁻¹ᵉ e
/- alternative constructor for ωGType -/
definition ωGType.mk_le {n : ℕ} (k₀ : ℕ)
(C : Π⦃k : ℕ⦄, k₀ ≤ k → ((n+k)-Type*[k.-1] : Type.{u+1}))
(e : Π⦃k : ℕ⦄ (H : k₀ ≤ k), C H ≃* Ω (C (le.step H))) : ([n;ω]GType : Type.{u+1}) :=
begin
fconstructor,
{ apply rec_down_le _ k₀ C, intro n' D,
refine (ptruncconntype.mk (Ω D) _ pt _),
apply is_trunc_loop, apply is_trunc_ptruncconntype, apply is_conn_loop,
apply is_conn_ptruncconntype },
{ intro n', apply rec_down_le_univ, exact e, intro n D, reflexivity }
end
definition ωGType.mk_le_beta {n : ℕ} {k₀ : ℕ}
(C : Π⦃k : ℕ⦄, k₀ ≤ k → ((n+k)-Type*[k.-1] : Type.{u+1}))
(e : Π⦃k : ℕ⦄ (H : k₀ ≤ k), C H ≃* Ω (C (le.step H)))
(k : ℕ) (H : k₀ ≤ k) : oB (ωGType.mk_le k₀ C e) k ≃* C H :=
ptruncconntype_eq_equiv _ _ !rec_down_le_beta_ge
definition GType_hom (G H : [n;k]GType) : Type :=
B G →* B H
definition ωGType_hom (G H : [n;ω]GType) : Type* :=
pointed.MK
(Σ(f : Πn, oB G n →* oB H n), Πn, psquare (f n) (Ω→ (f (n+1))) (oe G n) (oe H n))
⟨λn, pconst (oB G n) (oB H n), λn, !phconst_square ⬝vp* !ap1_pconst⟩
/- Constructions on higher groups -/
definition Decat (G : [n+1;k]GType) : [n;k]GType :=
GType.mk (ptrunctype.mk (ptrunc n G) _ pt) (pconntype.mk (ptrunc (n + k) (B G)) _ pt)
abstract begin
refine ptrunc_pequiv_ptrunc n (e G) ⬝e* _,
symmetry, exact !loopn_ptrunc_pequiv_nat
end end
definition Disc (G : [n;k]GType) : [n+1;k]GType :=
GType.mk (ptrunctype.mk G (show is_trunc (n.+1) G, from _) pt) (B G) (e G)
definition Decat_adjoint_Disc (G : [n+1;k]GType) (H : [n;k]GType) :
ppmap (B (Decat G)) (B H) ≃* ppmap (B G) (B (Disc H)) :=
pmap_ptrunc_pequiv (n + k) (B G) (B H)
definition Decat_adjoint_Disc_natural {G G' : [n+1;k]GType} {H H' : [n;k]GType}
(g : B G' →* B G) (h : B H →* B H') :
psquare (Decat_adjoint_Disc G H)
(Decat_adjoint_Disc G' H')
(ppcompose_left h ∘* ppcompose_right (ptrunc_functor _ g))
(ppcompose_left h ∘* ppcompose_right g) :=
pmap_ptrunc_pequiv_natural (n + k) g h
definition Decat_Disc (G : [n;k]GType) : Decat (Disc G) = G :=
GType_eq !ptrunc_pequiv
definition InfDecat (n : ℕ) (G : [∞;k]GType) : [n;k]GType :=
GType.mk (ptrunctype.mk (ptrunc n G) _ pt) (pconntype.mk (ptrunc (n + k) (iB G)) _ pt)
abstract begin
refine ptrunc_pequiv_ptrunc n (ie G) ⬝e* _,
symmetry, exact !loopn_ptrunc_pequiv_nat
end end
definition InfDisc (n : ℕ) (G : [n;k]GType) : [∞;k]GType :=
InfGType.mk G (B G) (e G)
definition InfDecat_adjoint_InfDisc (G : [∞;k]GType) (H : [n;k]GType) :
ppmap (B (InfDecat n G)) (B H) ≃* ppmap (iB G) (iB (InfDisc n H)) :=
pmap_ptrunc_pequiv (n + k) (iB G) (B H)
definition InfDecat_adjoint_InfDisc_natural {G G' : [∞;k]GType} {H H' : [n;k]GType}
(g : iB G' →* iB G) (h : B H →* B H') :
psquare (InfDecat_adjoint_InfDisc G H)
(InfDecat_adjoint_InfDisc G' H')
(ppcompose_left h ∘* ppcompose_right (ptrunc_functor _ g))
(ppcompose_left h ∘* ppcompose_right g) :=
pmap_ptrunc_pequiv_natural (n + k) g h
definition InfDecat_InfDisc (G : [n;k]GType) : InfDecat n (InfDisc n G) = G :=
GType_eq !ptrunc_pequiv
definition Deloop (G : [n;k+1]GType) : [n+1;k]GType :=
have is_conn k (B G), from is_conn_pconntype (B G),
have is_trunc (n + (k + 1)) (B G), from is_trunc_B G,
have is_trunc ((n + 1) + k) (B G), from transport (λ(n : ℕ), is_trunc n _) (succ_add n k)⁻¹ this,
GType.mk (ptrunctype.mk (Ω[k] (B G)) !is_trunc_loopn_nat pt)
(pconntype.mk (B G) !is_conn_of_is_conn_succ pt)
(pequiv_of_equiv erfl idp)
definition Loop (G : [n+1;k]GType) : [n;k+1]GType :=
GType.mk (ptrunctype.mk (Ω G) !is_trunc_loop_nat pt)
(connconnect k (B G))
(loop_pequiv_loop (e G) ⬝e* (loopn_connect k (B G))⁻¹ᵉ*)
definition Deloop_adjoint_Loop (G : [n;k+1]GType) (H : [n+1;k]GType) :
ppmap (B (Deloop G)) (B H) ≃* ppmap (B G) (B (Loop H)) :=
(connect_intro_pequiv _ !is_conn_pconntype)⁻¹ᵉ*
definition Deloop_adjoint_Loop_natural {G G' : [n;k+1]GType} {H H' : [n+1;k]GType}
(g : B G' →* B G) (h : B H →* B H') :
psquare (Deloop_adjoint_Loop G H)
(Deloop_adjoint_Loop G' H')
(ppcompose_left h ∘* ppcompose_right g)
(ppcompose_left (connect_functor k h) ∘* ppcompose_right g) :=
(connect_intro_pequiv_natural g h _ _)⁻¹ʰ*
definition Loop_Deloop (G : [n;k+1]GType) : Loop (Deloop G) = G :=
GType_eq (connect_pequiv (is_conn_pconntype (B G)))
definition Forget (G : [n;k+1]GType) : [n;k]GType :=
have is_conn k (B G), from !is_conn_pconntype,
GType.mk G (pconntype.mk (Ω (B G)) !is_conn_loop pt)
abstract begin
refine e G ⬝e* !loopn_succ_in
end end
definition Stabilize (G : [n;k]GType) : [n;k+1]GType :=
have is_conn k (susp (B G)), from !is_conn_susp,
have Hconn : is_conn k (ptrunc (n + k + 1) (susp (B G))), from !is_conn_ptrunc,
GType.mk (ptrunctype.mk (ptrunc n (Ω[k+1] (susp (B G)))) _ pt)
(pconntype.mk (ptrunc (n+k+1) (susp (B G))) Hconn pt)
abstract begin
refine !loopn_ptrunc_pequiv⁻¹ᵉ* ⬝e* _,
apply loopn_pequiv_loopn,
exact ptrunc_change_index !of_nat_add_of_nat _
end end
definition Stabilize_pequiv {G H : [n;k]GType} (e : B G ≃* B H) :
B (Stabilize G) ≃* B (Stabilize H) :=
ptrunc_pequiv_ptrunc (n+k+1) (susp_pequiv e)
definition Stabilize_adjoint_Forget (G : [n;k]GType) (H : [n;k+1]GType) :
ppmap (B (Stabilize G)) (B H) ≃* ppmap (B G) (B (Forget H)) :=
have is_trunc (n + k + 1) (B H), from !is_trunc_B,
pmap_ptrunc_pequiv (n + k + 1) (⅀ (B G)) (B H) ⬝e* susp_adjoint_loop (B G) (B H)
definition Stabilize_adjoint_Forget_natural {G G' : [n;k]GType} {H H' : [n;k+1]GType}
(g : B G' →* B G) (h : B H →* B H') :
psquare (Stabilize_adjoint_Forget G H)
(Stabilize_adjoint_Forget G' H')
(ppcompose_left h ∘* ppcompose_right (ptrunc_functor (n+k+1) (⅀→ g)))
(ppcompose_left (Ω→ h) ∘* ppcompose_right g) :=
begin
have is_trunc (n + k + 1) (B H), from !is_trunc_B,
have is_trunc (n + k + 1) (B H'), from !is_trunc_B,
refine pmap_ptrunc_pequiv_natural (n+k+1) (⅀→ g) h ⬝h* _,
exact susp_adjoint_loop_natural_left g ⬝v* susp_adjoint_loop_natural_right h
end
definition Forget_Stabilize (H : k ≥ n + 2) (G : [n;k]GType) : B (Forget (Stabilize G)) ≃* B G :=
loop_ptrunc_pequiv _ _ ⬝e*
begin
cases k with k,
{ cases H },
{ have k ≥ succ n, from le_of_succ_le_succ H,
assert this : n + succ k ≤ 2 * k,
{ rewrite [two_mul, add_succ, -succ_add], exact nat.add_le_add_right this k },
exact freudenthal_pequiv (B G) this }
end⁻¹ᵉ* ⬝e*
ptrunc_pequiv (n + k) _
definition Stabilize_Forget (H : k ≥ n + 1) (G : [n;k+1]GType) : B (Stabilize (Forget G)) ≃* B G :=
begin
assert lem1 : n + succ k ≤ 2 * k,
{ rewrite [two_mul, add_succ, -succ_add], exact nat.add_le_add_right H k },
have is_conn k (B G), from !is_conn_pconntype,
have Π(G' : [n;k+1]GType), is_trunc (n + k + 1) (B G'), from is_trunc_B,
note z := is_conn_fun_loop_susp_counit (B G) (nat.le_refl (2 * k)),
refine ptrunc_pequiv_ptrunc_of_le (of_nat_le_of_nat lem1) (@(ptrunc_pequiv_ptrunc_of_is_conn_fun _ _) z) ⬝e*
!ptrunc_pequiv,
end
definition stabilization (H : k ≥ n + 2) : is_equiv (@Stabilize n k) :=
begin
fapply adjointify,
{ exact Forget },
{ intro G, apply GType_eq, exact Stabilize_Forget (le.trans !self_le_succ H) _ },
{ intro G, apply GType_eq, exact Forget_Stabilize H G }
end
/- an incomplete formalization of ω-Stabilization -/
definition ωForget (k : ℕ) (G : [n;ω]GType) : [n;k]GType :=
have is_trunc (n + k) (oB G k), from _,
have is_trunc n (Ω[k] (oB G k)), from !is_trunc_loopn_nat,
GType.mk (ptrunctype.mk (Ω[k] (oB G k)) _ pt) (oB G k) (pequiv_of_equiv erfl idp)
definition nStabilize (H : k ≤ l) (G : GType.{u} n k) : GType.{u} n l :=
begin
induction H with l H IH, exact G, exact Stabilize IH
end
definition nStabilize_pequiv (H H' : k ≤ l) {G G' : [n;k]GType}
(e : B G ≃* B G') : B (nStabilize H G) ≃* B (nStabilize H' G') :=
begin
induction H with l H IH,
{ exact e ⬝e* pequiv_ap (λH, B (nStabilize H G')) (is_prop.elim (le.refl k) H') },
cases H' with l H'',
{ exfalso, exact not_succ_le_self H },
exact Stabilize_pequiv (IH H'')
end
definition nStabilize_pequiv_of_eq (H : k ≤ l) (p : k = l) (G : [n;k]GType) :
B (nStabilize H G) ≃* B G :=
begin induction p, exact pequiv_ap (λH, B (nStabilize H G)) (is_prop.elim H (le.refl k)) end
definition ωStabilize_of_le (H : k ≥ n + 2) (G : [n;k]GType) : [n;ω]GType :=
ωGType.mk_le k (λl H', GType_equiv n l (nStabilize H' G))
(λl H', (Forget_Stabilize (le.trans H H') (nStabilize H' G))⁻¹ᵉ*)
definition ωStabilize_of_le_beta (H : k ≥ n + 2) (G : [n;k]GType) (H' : l ≥ k) :
oB (ωStabilize_of_le H G) l ≃* GType_equiv n l (nStabilize H' G) :=
ptruncconntype_eq_equiv _ _ !rec_down_le_beta_ge
definition ωStabilize_of_le_pequiv (H : k ≥ n + 2) (H' : k' ≥ n + 2) {G : [n;k]GType}
{G' : [n;k']GType} (e : B G ≃* B G') (l : ℕ) (Hl : l ≥ k) (Hl' : l ≥ k') (p : k = k') :
oB (ωStabilize_of_le H G) l ≃* oB (ωStabilize_of_le H' G') l :=
begin
refine ωStabilize_of_le_beta H G Hl ⬝e* _ ⬝e* (ωStabilize_of_le_beta H' G' Hl')⁻¹ᵉ*,
induction p,
exact nStabilize_pequiv _ _ e
end
definition ωForget_ωStabilize_of_le (H : k ≥ n + 2) (G : [n;k]GType) :
B (ωForget k (ωStabilize_of_le H G)) ≃* B G :=
ωStabilize_of_le_beta H _ (le.refl k)
definition ωStabilize (G : [n;k]GType) : [n;ω]GType :=
ωStabilize_of_le !le_max_left (nStabilize !le_max_right G)
definition ωForget_ωStabilize (H : k ≥ n + 2) (G : [n;k]GType) :
B (ωForget k (ωStabilize G)) ≃* B G :=
begin
refine _ ⬝e* ωForget_ωStabilize_of_le H G,
esimp [ωForget, ωStabilize],
have H' : max (n + 2) k = k, from max_eq_right H,
exact ωStabilize_of_le_pequiv !le_max_left H (nStabilize_pequiv_of_eq _ H'⁻¹ _)
k (le_of_eq H') (le.refl k) H'
end
/-
definition ωStabilize_adjoint_ωForget (G : [n;k]GType) (H : [n;ω]GType) :
ωGType_hom (ωStabilize G) H ≃* ppmap (B G) (B (ωForget k H)) :=
sorry
definition ωStabilize_ωForget (G : [n;ω]GType) (l : ℕ) :
oB (ωStabilize (ωForget k G)) l ≃* oB G l :=
begin
exact sorry
end
definition ωstabilization (H : k ≥ n + 2) : is_equiv (@ωStabilize n k) :=
begin
apply adjointify _ (ωForget k),
{ intro G', exact sorry },
{ intro G, apply GType_eq, exact ωForget_ωStabilize H G }
end
-/
definition is_trunc_GType_hom (G H : [n;k]GType) : is_trunc n (GType_hom G H) :=
is_trunc_pmap_of_is_conn _ (k.-2) _ (k + n) _ (le_of_eq (sub_one_add_plus_two_sub_one k n)⁻¹)
(is_trunc_B' H)
definition is_set_GType_hom (G H : [0;k]GType) : is_set (GType_hom G H) :=
is_trunc_GType_hom G H
definition is_trunc_GType (n k : ℕ) : is_trunc (n + 1) [n;k]GType :=
begin
apply @is_trunc_equiv_closed_rev _ _ (n + 1) (GType_equiv n k),
apply is_trunc_succ_intro, intros X Y,
apply @is_trunc_equiv_closed_rev _ _ _ (ptruncconntype_eq_equiv X Y),
apply @is_trunc_equiv_closed_rev _ _ _ (pequiv.sigma_char_pmap X Y),
apply @is_trunc_subtype (X →* Y) (λ f, trunctype.mk' -1 (is_equiv f)),
exact is_trunc_GType_hom ((GType_equiv n k)⁻¹ᵉ X) ((GType_equiv n k)⁻¹ᵉ Y)
end
local attribute [instance] is_set_GType_hom
definition cGType [constructor] (k : ℕ) : Precategory :=
pb_Precategory (cptruncconntype' (k.-1))
(GType_equiv 0 k ⬝e ptruncconntype_equiv (ap of_nat (zero_add k)) idp ⬝e
(ptruncconntype'_equiv_ptruncconntype (k.-1))⁻¹ᵉ)
example (k : ℕ) : Precategory.carrier (cGType k) = [0;k]GType := by reflexivity
example (k : ℕ) (G H : cGType k) : (G ⟶ H) = (B G →* B H) := by reflexivity
definition cGType_equivalence_cType [constructor] (k : ℕ) : cGType k ≃c cType*[k.-1] :=
!pb_Precategory_equivalence_of_equiv
definition cGType_equivalence_Grp [constructor] : cGType.{u} 1 ≃c Grp.{u} :=
equivalence.trans !pb_Precategory_equivalence_of_equiv
(equivalence.trans (equivalence.symm Grp_equivalence_cptruncconntype')
proof equivalence.refl _ qed)
definition cGType_equivalence_AbGrp [constructor] (k : ℕ) : cGType.{u} (k+2) ≃c AbGrp.{u} :=
equivalence.trans !pb_Precategory_equivalence_of_equiv
(equivalence.trans (equivalence.symm (AbGrp_equivalence_cptruncconntype' k))
proof equivalence.refl _ qed)
print axioms GType_equiv
print axioms InfGType_equiv
print axioms Decat_adjoint_Disc
print axioms Decat_adjoint_Disc_natural
print axioms InfDecat_adjoint_InfDisc
print axioms InfDecat_adjoint_InfDisc_natural
print axioms Deloop_adjoint_Loop
print axioms Deloop_adjoint_Loop_natural
print axioms Stabilize_adjoint_Forget
print axioms Stabilize_adjoint_Forget_natural
print axioms stabilization
print axioms is_trunc_GType
print axioms cGType_equivalence_Grp
print axioms cGType_equivalence_AbGrp
end higher_group
|
6d4b44347bb8e076f42236ad0874f61d0d28dbd6 | 968e2f50b755d3048175f176376eff7139e9df70 | /examples/pred_logic/unnamed_215.lean | 8982a91cc75685109ee8ab5472d51b878dd59c02 | [] | 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 | 276 | lean | variable U : Type* -- Declare a type, `U`.
variable x : U -- Declare a term `x`, of type `U`.
variable P : U → Prop -- Declare a predicate `P` on `U`.
#check P x -- `P x` has type `Prop`
#check P -- `P` has type `U → Prop` |
882fc39031c96e8976d80707c484f7931264478a | 4950bf76e5ae40ba9f8491647d0b6f228ddce173 | /src/topology/separation.lean | 618219f0d5346e3f386140d83a91e4d6b4d003f6 | [
"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 | 26,251 | 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.subset_properties
open set filter
open_locale topological_space filter
local attribute [instance] classical.prop_decidable -- TODO: use "open_locale classical"
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_open_finset [t0_space α] (s : finset α) (sne : s.nonempty)
(hso : is_open (↑s : set α)) :
∃ x ∈ s, is_open ({x} : set α):=
begin
induction s using finset.strong_induction_on with s ihs,
by_cases hs : set.subsingleton (↑s : set α),
{ rcases sne with ⟨x, hx⟩,
refine ⟨x, hx, _⟩,
have : (↑s : set α) = {x}, from hs.eq_singleton_of_mem hx,
rwa this at hso },
{ dunfold set.subsingleton at hs,
push_neg at hs,
rcases hs with ⟨x, hx, y, hy, hxy⟩,
rcases t0_space.t0 x y hxy with ⟨U, hU, hxyU⟩,
wlog H : x ∈ U ∧ y ∉ U := hxyU using [x y, y x],
obtain ⟨z, hzs, hz⟩ : ∃ z ∈ s.filter (λ z, z ∈ U), is_open ({z} : set α),
{ refine ihs _ (finset.filter_ssubset.2 ⟨y, hy, H.2⟩) ⟨x, finset.mem_filter.2 ⟨hx, H.1⟩⟩ _,
rw [finset.coe_filter],
exact is_open_inter hso hU },
exact ⟨z, (finset.mem_filter.1 hzs).1, hz⟩ }
end
theorem exists_open_singleton_of_fintype [t0_space α] [f : fintype α] [ha : nonempty α] :
∃ x:α, is_open ({x}:set α) :=
begin
refine ha.elim (λ x, _),
have : is_open (↑(finset.univ : finset α) : set α), { simp },
rcases exists_open_singleton_of_open_finset _ ⟨x, finset.mem_univ x⟩ this with ⟨x, _, hx⟩,
exact ⟨x, hx⟩
end
instance subtype.t0_space [t0_space α] {p : α → Prop} : t0_space (subtype p) :=
⟨λ x y hxy, let ⟨U, hU, hxyU⟩ := t0_space.t0 (x:α) y ((not_congr subtype.ext_iff_val).1 hxy) in
⟨(coe : subtype p → α) ⁻¹' U, is_open_induced hU, hxyU⟩⟩
/-- 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
lemma is_open_compl_singleton [t1_space α] {x : α} : is_open ({x}ᶜ : set α) :=
is_closed_singleton
lemma is_open_ne [t1_space α] {x : α} : is_open {y | y ≠ x} :=
is_open_compl_singleton
instance subtype.t1_space {α : Type u} [topological_space α] [t1_space α] {p : α → Prop} :
t1_space (subtype p) :=
⟨λ ⟨x, hx⟩, is_closed_induced_iff.2 $ ⟨{x}, is_closed_singleton, set.ext $ λ y,
by simp [subtype.ext_iff_val]⟩⟩
@[priority 100] -- see Note [lower instance priority]
instance t1_space.t0_space [t1_space α] : t0_space α :=
⟨λ x y h, ⟨{z | z ≠ y}, is_open_ne, or.inl ⟨h, not_not_intro rfl⟩⟩⟩
lemma compl_singleton_mem_nhds [t1_space α] {x y : α} (h : y ≠ x) : {x}ᶜ ∈ 𝓝 y :=
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} :=
is_closed_singleton.closure_eq
lemma is_closed_map_const {α β} [topological_space α] [topological_space β] [t1_space β] {y : β} :
is_closed_map (function.const α y) :=
begin
apply is_closed_map.of_nonempty, intros s hs h2s, simp_rw [h2s.image_const, is_closed_singleton]
end
lemma discrete_of_t1_of_finite {X : Type*} [topological_space X] [t1_space X] [fintype X] :
discrete_topology X :=
begin
apply singletons_open_iff_discrete.mp,
intros x,
rw [← is_closed_compl_iff, ← bUnion_of_singleton ({x} : set X)ᶜ],
exact is_closed_bUnion (finite.of_fintype _) (λ y _, is_closed_singleton)
end
/-- 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
@[priority 100] -- see Note [lower instance priority]
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_ne_bot [ht : t2_space α] {x y : α} (h : ne_bot (𝓝 x ⊓ 𝓝 y)) : x = y :=
classical.by_contradiction $ assume : x ≠ y,
let ⟨u, v, hu, hv, hx, hy, huv⟩ := t2_space.t2 x y this in
absurd huv $ (inf_ne_bot_iff.1 h (mem_nhds_sets hu hx) (mem_nhds_sets hv hy)).ne_empty
lemma t2_iff_nhds : t2_space α ↔ ∀ {x y : α}, ne_bot (𝓝 x ⊓ 𝓝 y) → x = y :=
⟨assume h, by exactI λ x y, eq_of_nhds_ne_bot,
assume h, ⟨assume x y xy,
have 𝓝 x ⊓ 𝓝 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 α ↔ ∀ {x y : α} (f : ultrafilter α), ↑f ≤ 𝓝 x → ↑f ≤ 𝓝 y → x = y :=
t2_iff_nhds.trans $ by simp only [←exists_ultrafilter_iff, and_imp, le_inf_iff, exists_imp_distrib]
lemma is_closed_diagonal [t2_space α] : is_closed (diagonal α) :=
is_closed_iff_cluster_pt.mpr $ assume ⟨a₁, a₂⟩ h, eq_of_nhds_ne_bot $ assume : 𝓝 a₁ ⊓ 𝓝 a₂ = ⊥, h $
let ⟨t₁, ht₁, t₂, ht₂, (h' : t₁ ∩ t₂ ⊆ ∅)⟩ :=
by rw [←empty_in_sets_eq_bot, mem_inf_sets] at this; exact this in
begin
change t₁ ∈ 𝓝 a₁ at ht₁,
change t₂ ∈ 𝓝 a₂ at ht₂,
rw [nhds_prod_eq, ←empty_in_sets_eq_bot],
apply filter.sets_of_superset,
apply inter_mem_inf_sets (prod_mem_prod ht₁ ht₂) (mem_principal_sets.mpr (subset.refl _)),
exact assume ⟨x₁, x₂⟩ ⟨⟨hx₁, hx₂⟩, (heq : x₁ = x₂)⟩,
show false, from @h' x₁ ⟨hx₁, heq.symm ▸ hx₂⟩
end
lemma t2_iff_is_closed_diagonal : t2_space α ↔ is_closed (diagonal α) :=
begin
split,
{ introI h,
exact is_closed_diagonal },
{ intro h,
constructor,
intros x y hxy,
have : (x, y) ∈ (diagonal α)ᶜ, by rwa [mem_compl_iff],
obtain ⟨t, t_sub, t_op, xyt⟩ : ∃ t ⊆ (diagonal α)ᶜ, is_open t ∧ (x, y) ∈ t :=
is_open_iff_forall_mem_open.mp h _ this,
rcases is_open_prod_iff.mp t_op x y xyt with ⟨U, V, U_op, V_op, xU, yV, H⟩,
use [U, V, U_op, V_op, xU, yV],
have := subset.trans H t_sub,
rw eq_empty_iff_forall_not_mem,
rintros z ⟨zU, zV⟩,
have : ¬ (z, z) ∈ diagonal α := this (mk_mem_prod zU zV),
exact this rfl },
end
@[simp] lemma nhds_eq_nhds_iff {a b : α} [t2_space α] : 𝓝 a = 𝓝 b ↔ a = b :=
⟨assume h, eq_of_nhds_ne_bot $ by rw [h, inf_idem]; exact nhds_ne_bot, assume h, h ▸ rfl⟩
@[simp] lemma nhds_le_nhds_iff {a b : α} [t2_space α] : 𝓝 a ≤ 𝓝 b ↔ a = b :=
⟨assume h, eq_of_nhds_ne_bot $ by rw [inf_of_le_left h]; exact nhds_ne_bot, assume h, h ▸ le_refl _⟩
lemma tendsto_nhds_unique [t2_space α] {f : β → α} {l : filter β} {a b : α}
[ne_bot l] (ha : tendsto f l (𝓝 a)) (hb : tendsto f l (𝓝 b)) : a = b :=
eq_of_nhds_ne_bot $ ne_bot_of_le $ le_inf ha hb
lemma tendsto_nhds_unique' [t2_space α] {f : β → α} {l : filter β} {a b : α}
(hl : ne_bot l) (ha : tendsto f l (𝓝 a)) (hb : tendsto f l (𝓝 b)) : a = b :=
eq_of_nhds_ne_bot $ ne_bot_of_le $ le_inf ha hb
section lim
variables [t2_space α] {f : filter α}
/-!
### Properties of `Lim` and `lim`
In this section we use explicit `nonempty α` instances for `Lim` and `lim`. This way the lemmas
are useful without a `nonempty α` instance.
-/
lemma Lim_eq {a : α} [ne_bot f] (h : f ≤ 𝓝 a) :
@Lim _ _ ⟨a⟩ f = a :=
tendsto_nhds_unique (le_nhds_Lim ⟨a, h⟩) h
lemma Lim_eq_iff [ne_bot f] (h : ∃ (a : α), f ≤ nhds a) {a} : @Lim _ _ ⟨a⟩ f = a ↔ f ≤ 𝓝 a :=
⟨λ c, c ▸ le_nhds_Lim h, Lim_eq⟩
lemma ultrafilter.Lim_eq_iff_le_nhds [compact_space α] {x : α} {F : ultrafilter α} :
F.Lim = x ↔ ↑F ≤ 𝓝 x :=
⟨λ h, h ▸ F.le_nhds_Lim, Lim_eq⟩
lemma is_open_iff_ultrafilter' [compact_space α] (U : set α) :
is_open U ↔ (∀ F : ultrafilter α, F.Lim ∈ U → U ∈ F.1) :=
begin
rw is_open_iff_ultrafilter,
refine ⟨λ h F hF, h F.Lim hF F F.le_nhds_Lim, _⟩,
intros cond x hx f h,
rw [← (ultrafilter.Lim_eq_iff_le_nhds.2 h)] at hx,
exact cond _ hx
end
lemma filter.tendsto.lim_eq {a : α} {f : filter β} [ne_bot f] {g : β → α} (h : tendsto g f (𝓝 a)) :
@lim _ _ _ ⟨a⟩ f g = a :=
Lim_eq h
lemma filter.lim_eq_iff {f : filter β} [ne_bot f] {g : β → α} (h : ∃ a, tendsto g f (𝓝 a)) {a} :
@lim _ _ _ ⟨a⟩ f g = a ↔ tendsto g f (𝓝 a) :=
⟨λ c, c ▸ tendsto_nhds_lim h, filter.tendsto.lim_eq⟩
lemma continuous.lim_eq [topological_space β] {f : β → α} (h : continuous f) (a : β) :
@lim _ _ _ ⟨f a⟩ (𝓝 a) f = f a :=
(h.tendsto a).lim_eq
@[simp] lemma Lim_nhds (a : α) : @Lim _ _ ⟨a⟩ (𝓝 a) = a :=
Lim_eq (le_refl _)
@[simp] lemma lim_nhds_id (a : α) : @lim _ _ _ ⟨a⟩ (𝓝 a) id = a :=
Lim_nhds a
@[simp] lemma Lim_nhds_within {a : α} {s : set α} (h : a ∈ closure s) :
@Lim _ _ ⟨a⟩ (𝓝[s] a) = a :=
by haveI : ne_bot (𝓝[s] a) := mem_closure_iff_cluster_pt.1 h;
exact Lim_eq inf_le_left
@[simp] lemma lim_nhds_within_id {a : α} {s : set α} (h : a ∈ closure s) :
@lim _ _ _ ⟨a⟩ (𝓝[s] a) id = a :=
Lim_nhds_within h
end lim
/-!
### Instances of `t2_space` typeclass
We use two lemmas to prove that various standard constructions generate Hausdorff spaces from
Hausdorff spaces:
* `separated_by_continuous` says that two points `x y : α` can be separated by open neighborhoods
provided that there exists a continuous map `f`: α → β` with a Hausdorff codomain such that
`f x ≠ f y`. We use this lemma to prove that topological spaces defined using `induced` are
Hausdorff spaces.
* `separated_by_open_embedding` says that for an open embedding `f : α → β` of a Hausdorff space
`α`, the images of two distinct points `x y : α`, `x ≠ y` can be separated by open neighborhoods.
We use this lemma to prove that topological spaces defined using `coinduced` are Hausdorff spaces.
-/
@[priority 100] -- see Note [lower instance priority]
instance t2_space_discrete {α : Type*} [topological_space α] [discrete_topology α] : t2_space α :=
{ t2 := assume x y hxy, ⟨{x}, {y}, is_open_discrete _, is_open_discrete _, rfl, rfl,
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⟩ }
lemma separated_by_continuous {α : Type*} {β : Type*}
[topological_space α] [topological_space β] [t2_space β]
{f : α → β} (hf : continuous f) {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, uo.preimage hf, vo.preimage hf, xu, yv,
by rw [←preimage_inter, uv, preimage_empty]⟩
lemma separated_by_open_embedding {α β : Type*} [topological_space α] [topological_space β]
[t2_space α] {f : α → β} (hf : open_embedding f) {x y : α} (h : x ≠ y) :
∃ u v : set β, is_open u ∧ is_open v ∧ f x ∈ u ∧ f y ∈ v ∧ u ∩ v = ∅ :=
let ⟨u, v, uo, vo, xu, yv, uv⟩ := t2_separation h in
⟨f '' u, f '' v, hf.is_open_map _ uo, hf.is_open_map _ vo,
mem_image_of_mem _ xu, mem_image_of_mem _ yv, by rw [image_inter hf.inj, uv, image_empty]⟩
instance {α : Type*} {p : α → Prop} [t : topological_space α] [t2_space α] : t2_space (subtype p) :=
⟨assume x y h, separated_by_continuous continuous_subtype_val (mt subtype.eq h)⟩
instance {α : Type*} {β : Type*} [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_continuous continuous_fst h₁)
(λ h₂, separated_by_continuous continuous_snd h₂)⟩
instance {α : Type*} {β : Type*} [t₁ : topological_space α] [t2_space α]
[t₂ : topological_space β] [t2_space β] : t2_space (α ⊕ β) :=
begin
constructor,
rintros (x|x) (y|y) h,
{ replace h : x ≠ y := λ c, (c.subst h) rfl,
exact separated_by_open_embedding open_embedding_inl h },
{ exact ⟨_, _, is_open_range_inl, is_open_range_inr, ⟨x, rfl⟩, ⟨y, rfl⟩,
range_inl_inter_range_inr⟩ },
{ exact ⟨_, _, is_open_range_inr, is_open_range_inl, ⟨x, rfl⟩, ⟨y, rfl⟩,
range_inr_inter_range_inl⟩ },
{ replace h : x ≠ y := λ c, (c.subst h) rfl,
exact separated_by_open_embedding open_embedding_inr h }
end
instance Pi.t2_space {α : Type*} {β : α → 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_continuous (continuous_apply i) hi⟩
instance sigma.t2_space {ι : Type*} {α : ι → Type*} [Πi, topological_space (α i)]
[∀a, t2_space (α a)] :
t2_space (Σi, α i) :=
begin
constructor,
rintros ⟨i, x⟩ ⟨j, y⟩ neq,
rcases em (i = j) with (rfl|h),
{ replace neq : x ≠ y := λ c, (c.subst neq) rfl,
exact separated_by_open_embedding open_embedding_sigma_mk neq },
{ exact ⟨_, _, is_open_range_sigma_mk, is_open_range_sigma_mk, ⟨x, rfl⟩, ⟨y, rfl⟩, by tidy⟩ }
end
variables [topological_space β]
lemma is_closed_eq [t2_space α] {f g : β → α}
(hf : continuous f) (hg : continuous g) : is_closed {x:β | f x = g x} :=
continuous_iff_is_closed.mp (hf.prod_mk hg) _ is_closed_diagonal
/-- If two continuous maps are equal on `s`, then they are equal on the closure of `s`. -/
lemma set.eq_on.closure [t2_space α] {s : set β} {f g : β → α} (h : eq_on f g s)
(hf : continuous f) (hg : continuous g) :
eq_on f g (closure s) :=
closure_minimal h (is_closed_eq hf hg)
/-- If two continuous functions are equal on a dense set, then they are equal. -/
lemma continuous.ext_on [t2_space α] {s : set β} (hs : dense s) {f g : β → α}
(hf : continuous f) (hg : continuous g) (h : eq_on f g s) :
f = g :=
funext $ λ x, h.closure hf hg (hs x)
lemma diagonal_eq_range_diagonal_map {α : Type*} : {p:α×α | p.1 = p.2} = range (λx, (x,x)) :=
ext $ assume p, iff.intro
(assume h, ⟨p.1, prod.ext_iff.2 ⟨rfl, h⟩⟩)
(assume ⟨x, hx⟩, show p.1 = p.2, by rw ←hx)
lemma prod_subset_compl_diagonal_iff_disjoint {α : Type*} {s t : set α} :
set.prod s t ⊆ {p:α×α | p.1 = p.2}ᶜ ↔ s ∩ t = ∅ :=
by rw [eq_empty_iff_forall_not_mem, subset_compl_comm,
diagonal_eq_range_diagonal_map, range_subset_iff]; simp
lemma compact_compact_separated [t2_space α] {s t : set α}
(hs : is_compact s) (ht : is_compact t) (hst : s ∩ t = ∅) :
∃u v : set α, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ u ∩ v = ∅ :=
by simp only [prod_subset_compl_diagonal_iff_disjoint.symm] at ⊢ hst;
exact generalized_tube_lemma hs ht is_closed_diagonal hst
/-- In a `t2_space`, every compact set is closed. -/
lemma is_compact.is_closed [t2_space α] {s : set α} (hs : is_compact s) : is_closed s :=
is_open_compl_iff.mpr $ is_open_iff_forall_mem_open.mpr $ assume x hx,
let ⟨u, v, uo, vo, su, xv, uv⟩ :=
compact_compact_separated hs (compact_singleton : is_compact {x})
(by rwa [inter_comm, ←subset_compl_iff_disjoint, singleton_subset_iff]) in
have v ⊆ sᶜ, from
subset_compl_comm.mp (subset.trans su (subset_compl_iff_disjoint.mpr uv)),
⟨v, this, vo, by simpa using xv⟩
lemma is_compact.inter [t2_space α] {s t : set α} (hs : is_compact s) (ht : is_compact t) :
is_compact (s ∩ t) :=
hs.inter_right $ ht.is_closed
/-- If a compact set is covered by two open sets, then we can cover it by two compact subsets. -/
lemma is_compact.binary_compact_cover [t2_space α] {K U V : set α} (hK : is_compact K)
(hU : is_open U) (hV : is_open V) (h2K : K ⊆ U ∪ V) :
∃ K₁ K₂ : set α, is_compact K₁ ∧ is_compact K₂ ∧ K₁ ⊆ U ∧ K₂ ⊆ V ∧ K = K₁ ∪ K₂ :=
begin
rcases compact_compact_separated (compact_diff hK hU) (compact_diff hK hV)
(by rwa [diff_inter_diff, diff_eq_empty]) with ⟨O₁, O₂, h1O₁, h1O₂, h2O₁, h2O₂, hO⟩,
refine ⟨_, _, compact_diff hK h1O₁, compact_diff hK h1O₂,
by rwa [diff_subset_comm], by rwa [diff_subset_comm], by rw [← diff_inter, hO, diff_empty]⟩
end
section
open finset function
/-- For every finite open cover `Uᵢ` of a compact set, there exists a compact cover `Kᵢ ⊆ Uᵢ`. -/
lemma is_compact.finite_compact_cover [t2_space α] {s : set α} (hs : is_compact s) {ι} (t : finset ι)
(U : ι → set α) (hU : ∀ i ∈ t, is_open (U i)) (hsC : s ⊆ ⋃ i ∈ t, U i) :
∃ K : ι → set α, (∀ i, is_compact (K i)) ∧ (∀i, K i ⊆ U i) ∧ s = ⋃ i ∈ t, K i :=
begin
classical,
induction t using finset.induction with x t hx ih generalizing U hU s hs hsC,
{ refine ⟨λ _, ∅, λ i, compact_empty, λ i, empty_subset _, _⟩, simpa only [subset_empty_iff,
finset.not_mem_empty, Union_neg, Union_empty, not_false_iff] using hsC },
simp only [finset.bUnion_insert] at hsC,
simp only [finset.mem_insert] at hU,
have hU' : ∀ i ∈ t, is_open (U i) := λ i hi, hU i (or.inr hi),
rcases hs.binary_compact_cover (hU x (or.inl rfl)) (is_open_bUnion hU') hsC
with ⟨K₁, K₂, h1K₁, h1K₂, h2K₁, h2K₂, hK⟩,
rcases ih U hU' h1K₂ h2K₂ with ⟨K, h1K, h2K, h3K⟩,
refine ⟨update K x K₁, _, _, _⟩,
{ intros i, by_cases hi : i = x,
{ simp only [update_same, hi, h1K₁] },
{ rw [← ne.def] at hi, simp only [update_noteq hi, h1K] }},
{ intros i, by_cases hi : i = x,
{ simp only [update_same, hi, h2K₁] },
{ rw [← ne.def] at hi, simp only [update_noteq hi, h2K] }},
{ simp only [bUnion_insert_update _ hx, hK, h3K] }
end
end
lemma locally_compact_of_compact_nhds [t2_space α] (h : ∀ x : α, ∃ s, s ∈ 𝓝 x ∧ is_compact s) :
locally_compact_space α :=
⟨assume x n hn,
let ⟨u, un, uo, xu⟩ := mem_nhds_sets_iff.mp hn in
let ⟨k, kx, kc⟩ := h x in
-- K is compact but not necessarily contained in N.
-- K \ U is again compact and doesn't contain x, so
-- we may find open sets V, W separating x from K \ U.
-- Then K \ W is a compact neighborhood of x contained in U.
let ⟨v, w, vo, wo, xv, kuw, vw⟩ :=
compact_compact_separated compact_singleton (compact_diff kc uo)
(by rw [singleton_inter_eq_empty]; exact λ h, h.2 xu) in
have wn : wᶜ ∈ 𝓝 x, from
mem_nhds_sets_iff.mpr
⟨v, subset_compl_iff_disjoint.mpr vw, vo, singleton_subset_iff.mp xv⟩,
⟨k \ w,
filter.inter_mem_sets kx wn,
subset.trans (diff_subset_comm.mp kuw) un,
compact_diff kc wo⟩⟩
@[priority 100] -- see Note [lower instance priority]
instance locally_compact_of_compact [t2_space α] [compact_space α] : locally_compact_space α :=
locally_compact_of_compact_nhds (assume x, ⟨univ, mem_nhds_sets is_open_univ trivial, compact_univ⟩)
/-- In a locally compact T₂ space, every point has an open neighborhood with compact closure -/
lemma exists_open_with_compact_closure [locally_compact_space α] [t2_space α] (x : α) :
∃ (U : set α), is_open U ∧ x ∈ U ∧ is_compact (closure U) :=
begin
rcases locally_compact_space.local_compact_nhds x set.univ filter.univ_mem_sets with
⟨K, h1K, _, h2K⟩,
rw [mem_nhds_sets_iff] at h1K, rcases h1K with ⟨t, h1t, h2t, h3t⟩,
exact ⟨t, h2t, h3t, compact_of_is_closed_subset h2K is_closed_closure $
closure_minimal h1t $ h2K.is_closed⟩
end
/-- In a locally compact T₂ space, every compact set is contained in the interior of a compact
set. -/
lemma exists_compact_superset [locally_compact_space α] [t2_space α] {K : set α}
(hK : is_compact K) : ∃ (K' : set α), is_compact K' ∧ K ⊆ interior K' :=
begin
choose U hU using λ x : K, exists_open_with_compact_closure (x : α),
rcases hK.elim_finite_subcover U (λ x, (hU x).1) (λ x hx, ⟨_, ⟨⟨x, hx⟩, rfl⟩, (hU ⟨x, hx⟩).2.1⟩) with
⟨s, hs⟩,
refine ⟨⋃ (i : K) (H : i ∈ s), closure (U i), _, _⟩,
exact (finite_mem_finset s).compact_bUnion (λ x hx, (hU x).2.2),
refine subset.trans hs _, rw subset_interior_iff_subset_of_open,
exact bUnion_subset_bUnion_right (λ x hx, subset_closure),
exact is_open_bUnion (λ x hx, (hU x).1)
end
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 ∧ 𝓝[t] a = ⊥)
lemma nhds_is_closed [regular_space α] {a : α} {s : set α} (h : s ∈ 𝓝 a) :
∃t∈(𝓝 a), t ⊆ s ∧ is_closed t :=
let ⟨s', h₁, h₂, h₃⟩ := mem_nhds_sets_iff.mp h in
have ∃t, is_open t ∧ s'ᶜ ⊆ t ∧ 𝓝[t] a = ⊥,
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_eq_bot $ by rwa [compl_compl],
subset.trans (compl_subset_comm.1 ht₂) h₁,
is_closed_compl_iff.mpr ht₁⟩
lemma closed_nhds_basis [regular_space α] (a : α) :
(𝓝 a).has_basis (λ s : set α, s ∈ 𝓝 a ∧ is_closed s) id :=
⟨λ t, ⟨λ t_in, let ⟨s, s_in, h_st, h⟩ := nhds_is_closed t_in in ⟨s, ⟨s_in, h⟩, h_st⟩,
λ ⟨s, ⟨s_in, hs⟩, hst⟩, mem_sets_of_superset s_in hst⟩⟩
instance subtype.regular_space [regular_space α] {p : α → Prop} : regular_space (subtype p) :=
⟨begin
intros s a hs ha,
rcases is_closed_induced_iff.1 hs with ⟨s, hs', rfl⟩,
rcases regular_space.regular hs' ha with ⟨t, ht, hst, hat⟩,
refine ⟨coe ⁻¹' t, is_open_induced ht, preimage_mono hst, _⟩,
rw [nhds_within, nhds_induced, ← comap_principal, ← comap_inf, ← nhds_within, hat, comap_bot]
end⟩
variable (α)
@[priority 100] -- see Note [lower instance priority]
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⟩⟩⟩
variable {α}
lemma disjoint_nested_nhds [regular_space α] {x y : α} (h : x ≠ y) :
∃ (U₁ V₁ ∈ 𝓝 x) (U₂ V₂ ∈ 𝓝 y), is_closed V₁ ∧ is_closed V₂ ∧ is_open U₁ ∧ is_open U₂ ∧
V₁ ⊆ U₁ ∧ V₂ ⊆ U₂ ∧ U₁ ∩ U₂ = ∅ :=
begin
rcases t2_separation h with ⟨U₁, U₂, U₁_op, U₂_op, x_in, y_in, H⟩,
rcases nhds_is_closed (mem_nhds_sets U₁_op x_in) with ⟨V₁, V₁_in, h₁, V₁_closed⟩,
rcases nhds_is_closed (mem_nhds_sets U₂_op y_in) with ⟨V₂, V₂_in, h₂, V₂_closed⟩,
use [U₁, V₁, mem_sets_of_superset V₁_in h₁, V₁_in,
U₂, V₂, mem_sets_of_superset V₂_in h₂, V₂_in],
tauto
end
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
@[priority 100] -- see Note [lower instance priority]
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⟩⟩ }
-- We can't make this an instance because it could cause an instance loop.
lemma normal_of_compact_t2 [compact_space α] [t2_space α] : normal_space α :=
begin
refine ⟨assume s t hs ht st, _⟩,
simp only [disjoint_iff],
exact compact_compact_separated hs.compact ht.compact st.eq_bot
end
end normality
|
22141be3f5dbb44fca80679e82b82da94c597929 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/algebra/group/type_tags.lean | 17589bd42bc3ba786fda72a914d6220355defe8c | [
"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 | 8,180 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import algebra.group.hom
import data.equiv.basic
/-!
# Type tags that turn additive structures into multiplicative, and vice versa
We define two type tags:
* `additive α`: turns any multiplicative structure on `α` into the corresponding
additive structure on `additive α`;
* `multiplicative α`: turns any additive structure on `α` into the corresponding
multiplicative structure on `multiplicative α`.
We also define instances `additive.*` and `multiplicative.*` that actually transfer the structures.
-/
universes u v
variables {α : Type u} {β : Type v}
/-- If `α` carries some multiplicative structure, then `additive α` carries the corresponding
additive structure. -/
def additive (α : Type*) := α
/-- If `α` carries some additive structure, then `multiplicative α` carries the corresponding
multiplicative structure. -/
def multiplicative (α : Type*) := α
namespace additive
/-- Reinterpret `x : α` as an element of `additive α`. -/
def of_mul : α ≃ additive α := ⟨λ x, x, λ x, x, λ x, rfl, λ x, rfl⟩
/-- Reinterpret `x : additive α` as an element of `α`. -/
def to_mul : additive α ≃ α := of_mul.symm
@[simp] lemma of_mul_symm_eq : (@of_mul α).symm = to_mul := rfl
@[simp] lemma to_mul_symm_eq : (@to_mul α).symm = of_mul := rfl
end additive
namespace multiplicative
/-- Reinterpret `x : α` as an element of `multiplicative α`. -/
def of_add : α ≃ multiplicative α := ⟨λ x, x, λ x, x, λ x, rfl, λ x, rfl⟩
/-- Reinterpret `x : multiplicative α` as an element of `α`. -/
def to_add : multiplicative α ≃ α := of_add.symm
@[simp] lemma of_add_symm_eq : (@of_add α).symm = to_add := rfl
@[simp] lemma to_add_symm_eq : (@to_add α).symm = of_add := rfl
end multiplicative
@[simp] lemma to_add_of_add (x : α) : (multiplicative.of_add x).to_add = x := rfl
@[simp] lemma of_add_to_add (x : multiplicative α) : multiplicative.of_add x.to_add = x := rfl
@[simp] lemma to_mul_of_mul (x : α) : (additive.of_mul x).to_mul = x := rfl
@[simp] lemma of_mul_to_mul (x : additive α) : additive.of_mul x.to_mul = x := rfl
instance [inhabited α] : inhabited (additive α) := ⟨additive.of_mul (default α)⟩
instance [inhabited α] : inhabited (multiplicative α) := ⟨multiplicative.of_add (default α)⟩
instance additive.has_add [has_mul α] : has_add (additive α) :=
{ add := λ x y, additive.of_mul (x.to_mul * y.to_mul) }
instance [has_add α] : has_mul (multiplicative α) :=
{ mul := λ x y, multiplicative.of_add (x.to_add + y.to_add) }
@[simp] lemma of_add_add [has_add α] (x y : α) :
multiplicative.of_add (x + y) = multiplicative.of_add x * multiplicative.of_add y :=
rfl
@[simp] lemma to_add_mul [has_add α] (x y : multiplicative α) :
(x * y).to_add = x.to_add + y.to_add :=
rfl
@[simp] lemma of_mul_mul [has_mul α] (x y : α) :
additive.of_mul (x * y) = additive.of_mul x + additive.of_mul y :=
rfl
@[simp] lemma to_mul_add [has_mul α] (x y : additive α) :
(x + y).to_mul = x.to_mul * y.to_mul :=
rfl
instance [semigroup α] : add_semigroup (additive α) :=
{ add_assoc := @mul_assoc α _,
..additive.has_add }
instance [add_semigroup α] : semigroup (multiplicative α) :=
{ mul_assoc := @add_assoc α _,
..multiplicative.has_mul }
instance [comm_semigroup α] : add_comm_semigroup (additive α) :=
{ add_comm := @mul_comm _ _,
..additive.add_semigroup }
instance [add_comm_semigroup α] : comm_semigroup (multiplicative α) :=
{ mul_comm := @add_comm _ _,
..multiplicative.semigroup }
instance [left_cancel_semigroup α] : add_left_cancel_semigroup (additive α) :=
{ add_left_cancel := @mul_left_cancel _ _,
..additive.add_semigroup }
instance [add_left_cancel_semigroup α] : left_cancel_semigroup (multiplicative α) :=
{ mul_left_cancel := @add_left_cancel _ _,
..multiplicative.semigroup }
instance [right_cancel_semigroup α] : add_right_cancel_semigroup (additive α) :=
{ add_right_cancel := @mul_right_cancel _ _,
..additive.add_semigroup }
instance [add_right_cancel_semigroup α] : right_cancel_semigroup (multiplicative α) :=
{ mul_right_cancel := @add_right_cancel _ _,
..multiplicative.semigroup }
instance [has_one α] : has_zero (additive α) := ⟨additive.of_mul 1⟩
@[simp] lemma of_mul_one [has_one α] : @additive.of_mul α 1 = 0 := rfl
@[simp] lemma to_mul_zero [has_one α] : (0 : additive α).to_mul = 1 := rfl
instance [has_zero α] : has_one (multiplicative α) := ⟨multiplicative.of_add 0⟩
@[simp] lemma of_add_zero [has_zero α] : @multiplicative.of_add α 0 = 1 := rfl
@[simp] lemma to_add_one [has_zero α] : (1 : multiplicative α).to_add = 0 := rfl
instance [monoid α] : add_monoid (additive α) :=
{ zero := 0,
zero_add := @one_mul _ _,
add_zero := @mul_one _ _,
..additive.add_semigroup }
instance [add_monoid α] : monoid (multiplicative α) :=
{ one := 1,
one_mul := @zero_add _ _,
mul_one := @add_zero _ _,
..multiplicative.semigroup }
instance [comm_monoid α] : add_comm_monoid (additive α) :=
{ .. additive.add_monoid, .. additive.add_comm_semigroup }
instance [add_comm_monoid α] : comm_monoid (multiplicative α) :=
{ ..multiplicative.monoid, .. multiplicative.comm_semigroup }
instance [has_inv α] : has_neg (additive α) := ⟨λ x, multiplicative.of_add x.to_mul⁻¹⟩
@[simp] lemma of_mul_inv [has_inv α] (x : α) : additive.of_mul x⁻¹ = -(additive.of_mul x) := rfl
@[simp] lemma to_mul_neg [has_inv α] (x : additive α) : (-x).to_mul = x.to_mul⁻¹ := rfl
instance [has_neg α] : has_inv (multiplicative α) := ⟨λ x, additive.of_mul (-x.to_add)⟩
@[simp] lemma of_add_neg [has_neg α] (x : α) :
multiplicative.of_add (-x) = (multiplicative.of_add x)⁻¹ := rfl
@[simp] lemma to_add_inv [has_neg α] (x : multiplicative α) :
(x⁻¹).to_add = -x.to_add := rfl
instance [group α] : add_group (additive α) :=
{ add_left_neg := @mul_left_inv α _,
.. additive.has_neg, .. additive.add_monoid }
instance [add_group α] : group (multiplicative α) :=
{ mul_left_inv := @add_left_neg α _,
.. multiplicative.has_inv, ..multiplicative.monoid }
instance [comm_group α] : add_comm_group (additive α) :=
{ .. additive.add_group, .. additive.add_comm_monoid }
instance [add_comm_group α] : comm_group (multiplicative α) :=
{ .. multiplicative.group, .. multiplicative.comm_monoid }
/-- Reinterpret `α →+ β` as `multiplicative α →* multiplicative β`. -/
def add_monoid_hom.to_multiplicative [add_monoid α] [add_monoid β] :
(α →+ β) ≃ (multiplicative α →* multiplicative β) :=
⟨λ f, ⟨f.1, f.2, f.3⟩, λ f, ⟨f.1, f.2, f.3⟩, λ x, by { ext, refl, }, λ x, by { ext, refl, }⟩
/-- Reinterpret `α →* β` as `additive α →+ additive β`. -/
def monoid_hom.to_additive [monoid α] [monoid β] :
(α →* β) ≃ (additive α →+ additive β) :=
⟨λ f, ⟨f.1, f.2, f.3⟩, λ f, ⟨f.1, f.2, f.3⟩, λ x, by { ext, refl, }, λ x, by { ext, refl, }⟩
/-- Reinterpret `additive α →+ β` as `α →* multiplicative β`. -/
def add_monoid_hom.to_multiplicative' [monoid α] [add_monoid β] :
(additive α →+ β) ≃ (α →* multiplicative β) :=
⟨λ f, ⟨f.1, f.2, f.3⟩, λ f, ⟨f.1, f.2, f.3⟩, λ x, by { ext, refl, }, λ x, by { ext, refl, }⟩
/-- Reinterpret `α →* multiplicative β` as `additive α →+ β`. -/
def monoid_hom.to_additive' [monoid α] [add_monoid β] :
(α →* multiplicative β) ≃ (additive α →+ β) :=
add_monoid_hom.to_multiplicative'.symm
/-- Reinterpret `α →+ additive β` as `multiplicative α →* β`. -/
def add_monoid_hom.to_multiplicative'' [add_monoid α] [monoid β] :
(α →+ additive β) ≃ (multiplicative α →* β) :=
⟨λ f, ⟨f.1, f.2, f.3⟩, λ f, ⟨f.1, f.2, f.3⟩, λ x, by { ext, refl, }, λ x, by { ext, refl, }⟩
/-- Reinterpret `multiplicative α →* β` as `α →+ additive β`. -/
def monoid_hom.to_additive'' [add_monoid α] [monoid β] :
(multiplicative α →* β) ≃ (α →+ additive β) :=
add_monoid_hom.to_multiplicative''.symm
|
da067b668c019c42980ed22da259cadb0daededd | f5f7e6fae601a5fe3cac7cc3ed353ed781d62419 | /src/tactic/wlog.lean | cefb82122b4ab5268dbc5af6dedd13d4a22d2bf4 | [
"Apache-2.0"
] | permissive | EdAyers/mathlib | 9ecfb2f14bd6caad748b64c9c131befbff0fb4e0 | ca5d4c1f16f9c451cf7170b10105d0051db79e1b | refs/heads/master | 1,626,189,395,845 | 1,555,284,396,000 | 1,555,284,396,000 | 144,004,030 | 0 | 0 | Apache-2.0 | 1,533,727,664,000 | 1,533,727,663,000 | null | UTF-8 | Lean | false | false | 9,321 | 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
Without loss of generality tactic.
-/
import tactic.basic tactic.interactive data.list.perm
open expr tactic lean lean.parser
local postfix `?`:9001 := optional
local postfix *:9001 := many
namespace tactic
private meta def update_pp_name : expr → name → expr
| (local_const n _ bi d) pp := local_const n pp bi d
| e n := e
private meta def elim_or : ℕ → expr → tactic (list expr)
| 0 h := fail "zero cases"
| 1 h := return [h]
| (n + 1) h := do
[(_, [hl], []), (_, [hr], [])] ← induction h, -- there should be no dependent terms
[gl, gr] ← get_goals,
set_goals [gr],
hsr ← elim_or n hr,
gsr ← get_goals,
set_goals (gl :: gsr),
return (hl :: hsr)
private meta def dest_or : expr → tactic (list expr) | e := do
`(%%a ∨ %%b) ← whnf e | return [e],
lb ← dest_or b,
return (a :: lb)
private meta def match_perms (pat : pattern) : expr → tactic (list $ list expr) | t :=
(do
m ← match_pattern pat t,
guard (m.2.all expr.is_local_constant),
return [m.2]) <|>
(do
`(%%l ∨ %%r) ← whnf t,
m ← match_pattern pat l,
rs ← match_perms r,
return (m.2 :: rs))
private meta def update_type : expr → expr → expr
| (local_const n pp bi d) t := local_const n pp bi t
| e t := e
private meta def intron' : ℕ → tactic (list expr)
| 0 := return []
| (i + 1) := do
n ← intro1,
ls ← intron' i,
return (n :: ls)
meta def wlog (vars' : list expr) (h_cases fst_case : expr) (perms : list (list expr)) :
tactic unit := do
guard h_cases.is_local_constant,
-- reorder s.t. context is Γ ⬝ vars ⬝ cases ⊢ ∀deps, …
nr ← revert_lst (vars' ++ [h_cases]),
vars ← intron' vars'.length,
h_cases ← intro h_cases.local_pp_name,
cases ← infer_type h_cases,
h_fst_case ←
mk_local_def h_cases.local_pp_name
(fst_case.instantiate_locals $ (vars'.zip vars).map $ λ⟨o, n⟩, (o.local_uniq_name, n)),
((), pr) ← solve_aux cases (repeat $ exact h_fst_case <|> left >> skip),
t ← target,
fixed_vars ← vars.mmap (λv, do t ← infer_type v, return (update_type v t) ),
let t' := (instantiate_local h_cases.local_uniq_name pr t).pis (fixed_vars ++ [h_fst_case]),
(h, [g]) ← local_proof `this t' (do
clear h_cases,
vars.mmap clear,
intron nr),
h₀ :: hs ← elim_or perms.length h_cases,
solve1 (do
exact (h.mk_app $ vars ++ [h₀])),
focus ((hs.zip perms.tail).map $ λ⟨h_case, perm⟩, do
let p_v := (vars'.zip vars).map (λ⟨p, v⟩, (p.local_uniq_name, v)),
let p := perm.map (λp, p.instantiate_locals p_v),
note `this none (h.mk_app $ p ++ [h_case]),
clear h,
return ()),
gs ← get_goals,
set_goals (g :: gs)
namespace interactive
open interactive interactive.types expr
private meta def parse_permutations : option (list (list name)) → tactic (list (list expr))
| none := return []
| (some []) := return []
| (some perms@(p₀ :: ps)) := do
(guard p₀.nodup <|>
fail "No permutation `xs_i` in `using [xs_1, …, xs_n]` should contain the same variable twice."),
(guard (perms.all $ λp, p.perm p₀) <|>
fail "The permutations `xs_i` in `using [xs_1, …, xs_n]` must be permutations of the same variables."),
perms.mmap (λp, p.mmap get_local)
/-- Without loss of generality: reduces to one goal under variables permutations.
Given a goal of the form `g xs`, a predicate `p` over a set of variables, as well as variable
permutations `xs_i`. Then `wlog` produces goals of the form
The case goal, i.e. the permutation `xs_i` covers all possible cases:
`⊢ p xs_0 ∨ ⋯ ∨ p xs_n`
The main goal, i.e. the goal reduced to `xs_0`:
`(h : p xs_0) ⊢ g xs_0`
The invariant goals, i.e. `g` is invariant under `xs_i`:
`(h : p xs_i) (this : g xs_0) ⊢ gs xs_i`
Either the permutation is provided, or a proof of the disjunction is provided to compute the
permutation. The disjunction need to be in assoc normal form, e.g. `p₀ ∨ (p₁ ∨ p₂)`. In many cases
the invariant goals can be solved by AC rewriting using `cc` etc.
Example:
On a state `(n m : ℕ) ⊢ p n m` the tactic `wlog h : n ≤ m using [n m, m n]` produces the following
states:
`(n m : ℕ) ⊢ n ≤ m ∨ m ≤ n`
`(n m : ℕ) (h : n ≤ m) ⊢ p n m`
`(n m : ℕ) (h : m ≤ n) (this : p n m) ⊢ p m n`
`wlog` supports different calling conventions. The name `h` is used to give a name to the introduced
case hypothesis. If the name is avoided, the default will be `case`.
(1) `wlog : p xs0 using [xs0, …, xsn]`
Results in the case goal `p xs0 ∨ ⋯ ∨ ps xsn`, the main goal `(case : p xs0) ⊢ g xs0` and the
invariance goals `(case : p xsi) (this : g xs0) ⊢ g xsi`.
(2) `wlog : p xs0 := r using xs0`
The expression `r` is a proof of the shape `p xs0 ∨ ⋯ ∨ p xsi`, it is also used to compute the
variable permutations.
(3) `wlog := r using xs0`
The expression `r` is a proof of the shape `p xs0 ∨ ⋯ ∨ p xsi`, it is also used to compute the
variable permutations. This is not as stable as (2), for example `p` cannot be a disjunction.
(4) `wlog : R x y using x y` and `wlog : R x y`
Produces the case `R x y ∨ R y x`. If `R` is ≤, then the disjunction discharged using linearity.
If `using x y` is avoided then `x` and `y` are the last two variables appearing in the
expression `R x y`. -/
meta def wlog
(h : parse ident?)
(pat : parse (tk ":" *> texpr)?)
(cases : parse (tk ":=" *> texpr)?)
(perms : parse (tk "using" *> (list_of (ident*) <|> (λx, [x]) <$> ident*))?)
(discharger : tactic unit :=
(tactic.solve_by_elim <|> tactic.tautology tt <|> using_smt (smt_tactic.intros >> smt_tactic.solve_goals))) :
tactic unit := do
perms ← parse_permutations perms,
(pat, cases_pr, cases_goal, vars, perms) ← (match cases with
| some r := do
vars::_ ← return perms |
fail "At least one set of variables expected, i.e. `using x y` or `using [x y, y x]`.",
cases_pr ← to_expr r,
cases_pr ← (if cases_pr.is_local_constant
then return $ match h with some n := update_pp_name cases_pr n | none := cases_pr end
else do
note (h.get_or_else `case) none cases_pr),
cases ← infer_type cases_pr,
(pat, perms') ← match pat with
| some pat := do
pat ← to_expr pat,
let vars' := vars.filter $ λv, v.occurs pat,
case_pat ← mk_pattern [] vars' pat [] vars',
perms' ← match_perms case_pat cases,
return (pat, perms')
| none := do
(p :: ps) ← dest_or cases,
let vars' := vars.filter $ λv, v.occurs p,
case_pat ← mk_pattern [] vars' p [] vars',
perms' ← (p :: ps).mmap (λp, do m ← match_pattern case_pat p, return m.2),
return (p, perms')
end,
let vars_name := vars.map local_uniq_name,
guard (perms'.all $ λp, p.all $ λv, v.is_local_constant ∧ v.local_uniq_name ∈ vars_name) <|>
fail "Cases contains variables not declared in `using x y z`",
perms ← (if perms.length = 1
then do
return (perms'.map $ λp, p ++ vars.filter (λv, p.all (λv', v'.local_uniq_name ≠ v.local_uniq_name)))
else do
guard (perms.length = perms'.length) <|>
fail "The provided permutation list has a different length then the provided cases.",
return perms),
return (pat, cases_pr, @none expr, vars, perms)
| none := do
let name_h := h.get_or_else `case,
some pat ← return pat | fail "Either specify cases or a pattern with permutations",
pat ← to_expr pat,
(do
[x, y] ← match perms with
| [] := return pat.list_local_consts
| [l] := return l
| _ := failed
end,
let cases := mk_or_lst [pat, pat.instantiate_locals [(x.local_uniq_name, y), (y.local_uniq_name, x)]],
(do
`(%%x' ≤ %%y') ← return pat,
(cases_pr, []) ← local_proof name_h cases (exact ``(le_total %%x' %%y')),
return (pat, cases_pr, none, [x, y], [[x, y], [y, x]]))
<|>
(do
(cases_pr, [g]) ← local_proof name_h cases skip,
return (pat, cases_pr, some g, [x, y], [[x, y], [y, x]]))) <|>
(do
guard (perms.length ≥ 2) <|>
fail ("To generate cases at least two permutations are required, i.e. `using [x y, y x]`" ++
" or exactly 0 or 2 variables"),
(vars :: perms') ← return perms,
let names := vars.map local_uniq_name,
let cases := mk_or_lst (pat :: perms'.map (λp, pat.instantiate_locals (names.zip p))),
(cases_pr, [g]) ← local_proof name_h cases skip,
return (pat, cases_pr, some g, vars, perms))
end),
let name_fn :=
(if perms.length = 2 then λi, `invariant else λi, mk_simple_name ("invariant_" ++ to_string (i + 1))),
with_enable_tags $ tactic.focus1 $ do
t ← get_main_tag,
tactic.wlog vars cases_pr pat perms,
tactic.focus (set_main_tag (mk_num_name `_case 0 :: `main :: t) ::
(list.range (perms.length - 1)).map (λi, do
set_main_tag (mk_num_name `_case 0 :: name_fn i :: t),
try discharger)),
match cases_goal with
| some g := do
set_tag g (mk_num_name `_case 0 :: `cases :: t),
gs ← get_goals,
set_goals (g :: gs)
| none := skip
end
end interactive
end tactic
|
828c6efa0333c91b0019512132afb2dad0634ef0 | c3f2fcd060adfa2ca29f924839d2d925e8f2c685 | /tests/lean/run/forest_height.lean | 2ce6df0428346cf5367106c31fc8000b60e66547 | [
"Apache-2.0"
] | permissive | respu/lean | 6582d19a2f2838a28ecd2b3c6f81c32d07b5341d | 8c76419c60b63d0d9f7bc04ebb0b99812d0ec654 | refs/heads/master | 1,610,882,451,231 | 1,427,747,084,000 | 1,427,747,429,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,286 | lean | import data.nat.basic data.sum data.sigma data.bool
open nat sigma
inductive tree (A : Type) : Type :=
| node : A → forest A → tree A
with forest : Type :=
| nil : forest A
| cons : tree A → forest A → forest A
namespace manual
check tree.rec_on
definition tree.height {A : Type} (t : tree A) : nat :=
tree.rec_on t
(λ (a : A) (f : forest A) (ih : nat), succ ih)
zero
(λ (t : tree A) (f : forest A) (ih₁ : nat) (ih₂ : nat), succ (max ih₁ ih₂))
definition forest.height {A : Type} (f : forest A) : nat :=
forest.rec_on f
(λ (a : A) (f : forest A) (ih : nat), succ ih)
zero
(λ (t : tree A) (f : forest A) (ih₁ : nat) (ih₂ : nat), succ (max ih₁ ih₂))
definition tree_forest (A : Type) := sum (tree A) (forest A)
definition tree_forest_height {A : Type} (t : tree_forest A) : nat :=
sum.rec_on t (λ t, tree.height t) (λ f, forest.height f)
definition tree_forest.subterm {A : Type} : tree_forest A → tree_forest A → Prop :=
inv_image lt tree_forest_height
definition tree_forest.subterm.wf [instance] (A : Type) : well_founded (@tree_forest.subterm A) :=
inv_image.wf tree_forest_height lt.wf
local infix `≺`:50 := tree_forest.subterm
definition tree_forest.height_lt.node {A : Type} (a : A) (f : forest A) : sum.inr f ≺ sum.inl (tree.node a f) :=
have aux : forest.height f < tree.height (tree.node a f), from
lt.base (forest.height f),
aux
definition tree_forest.height_lt.cons₁ {A : Type} (t : tree A) (f : forest A) : sum.inl t ≺ sum.inr (forest.cons t f) :=
have aux : tree.height t < forest.height (forest.cons t f), from
lt_succ_of_le (max.left _ _),
aux
definition tree_forest.height_lt.cons₂ {A : Type} (t : tree A) (f : forest A) : sum.inr f ≺ sum.inr (forest.cons t f) :=
have aux : forest.height f < forest.height (forest.cons t f), from
lt_succ_of_le (max.right _ _),
aux
definition kind {A : Type} (t : tree_forest A) : bool :=
sum.cases_on t (λ t, bool.tt) (λ f, bool.ff)
definition map.res {A : Type} (B : Type) (t : tree_forest A) :=
Σ r : tree_forest B, kind r = kind t
set_option find_decl.expensive true
find_decl bool.ff ≠ bool.tt
-- map using well-founded recursion. We could have used the default recursor.
-- this is just a test for the definitional package
definition map.F {A B : Type} (f : A → B) (tf₁ : tree_forest A) : (Π tf₂ : tree_forest A, tf₂ ≺ tf₁ → map.res B tf₂) → map.res B tf₁ :=
sum.cases_on tf₁
(λ t : tree A, tree.cases_on t
(λ a₁ f₁ (r : Π (tf₂ : tree_forest A), tf₂ ≺ sum.inl (tree.node a₁ f₁) → map.res B tf₂),
show map.res B (sum.inl (tree.node a₁ f₁)), from
have rf₁ : map.res B (sum.inr f₁), from r (sum.inr f₁) (tree_forest.height_lt.node a₁ f₁),
have nf₁ : forest B, from sum.cases_on (pr₁ rf₁)
(λf (h : kind (sum.inl f) = kind (sum.inr f₁)), absurd (eq.symm h) bool.ff_ne_tt)
(λf h, f)
(pr₂ rf₁),
sigma.mk (sum.inl (tree.node (f a₁) nf₁)) rfl))
(λ f : forest A, forest.cases_on f
(λ r : Π (tf₂ : tree_forest A), tf₂ ≺ sum.inr (forest.nil A) → map.res B tf₂,
show map.res B (sum.inr (forest.nil A)), from
sigma.mk (sum.inr (forest.nil B)) rfl)
(λ t₁ f₁ (r : Π (tf₂ : tree_forest A), tf₂ ≺ sum.inr (forest.cons t₁ f₁) → map.res B tf₂),
show map.res B (sum.inr (forest.cons t₁ f₁)), from
have rt₁ : map.res B (sum.inl t₁), from r (sum.inl t₁) (tree_forest.height_lt.cons₁ t₁ f₁),
have rf₁ : map.res B (sum.inr f₁), from r (sum.inr f₁) (tree_forest.height_lt.cons₂ t₁ f₁),
have nt₁ : tree B, from sum.cases_on (pr₁ rt₁)
(λ t h, t)
(λ f h, absurd h bool.ff_ne_tt)
(pr₂ rt₁),
have nf₁ : forest B, from sum.cases_on (pr₁ rf₁)
(λf (h : kind (sum.inl f) = kind (sum.inr f₁)), absurd (eq.symm h) bool.ff_ne_tt)
(λf h, f)
(pr₂ rf₁),
sigma.mk (sum.inr (forest.cons nt₁ nf₁)) rfl))
definition map {A B : Type} (f : A → B) (tf : tree_forest A) : map.res B tf :=
well_founded.fix (@map.F A B f) tf
eval map succ (sum.inl (tree.node 2 (forest.cons (tree.node 1 (forest.nil nat)) (forest.nil nat))))
end manual
|
abd6ebfe5e7cb8deb12d78182b264b53af9ef818 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/category_theory/limits/shapes/multiequalizer.lean | b094fe7be5d54bb9cc51895b36ee778a2f401455 | [
"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 | 26,276 | lean | /-
Copyright (c) 2021 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz
-/
import category_theory.limits.shapes.products
import category_theory.limits.shapes.equalizers
import category_theory.limits.cone_category
import category_theory.adjunction
/-!
# Multi-(co)equalizers
A *multiequalizer* is an equalizer of two morphisms between two products.
Since both products and equalizers are limits, such an object is again a limit.
This file provides the diagram whose limit is indeed such an object.
In fact, it is well-known that any limit can be obtained as a multiequalizer.
The dual construction (multicoequalizers) is also provided.
## Projects
Prove that a multiequalizer can be identified with
an equalizer between products (and analogously for multicoequalizers).
Prove that the limit of any diagram is a multiequalizer (and similarly for colimits).
-/
namespace category_theory.limits
open category_theory
universes v u
/-- The type underlying the multiequalizer diagram. -/
@[nolint unused_arguments]
inductive walking_multicospan {L R : Type v} (fst snd : R → L) : Type v
| left : L → walking_multicospan
| right : R → walking_multicospan
/-- The type underlying the multiecoqualizer diagram. -/
@[nolint unused_arguments]
inductive walking_multispan {L R : Type v} (fst snd : L → R) : Type v
| left : L → walking_multispan
| right : R → walking_multispan
namespace walking_multicospan
variables {L R : Type v} {fst snd : R → L}
instance [inhabited L] : inhabited (walking_multicospan fst snd) :=
⟨left default⟩
/-- Morphisms for `walking_multicospan`. -/
inductive hom : Π (a b : walking_multicospan fst snd), Type v
| id (A) : hom A A
| fst (b) : hom (left (fst b)) (right b)
| snd (b) : hom (left (snd b)) (right b)
instance {a : walking_multicospan fst snd} : inhabited (hom a a) :=
⟨hom.id _⟩
/-- Composition of morphisms for `walking_multicospan`. -/
def hom.comp : Π {A B C : walking_multicospan fst snd} (f : hom A B) (g : hom B C),
hom A C
| _ _ _ (hom.id X) f := f
| _ _ _ (hom.fst b) (hom.id X) := hom.fst b
| _ _ _ (hom.snd b) (hom.id X) := hom.snd b
instance : small_category (walking_multicospan fst snd) :=
{ hom := hom,
id := hom.id,
comp := λ X Y Z, hom.comp,
id_comp' := by { rintro (_|_) (_|_) (_|_|_), tidy },
comp_id' := by { rintro (_|_) (_|_) (_|_|_), tidy },
assoc' := by { rintro (_|_) (_|_) (_|_) (_|_) (_|_|_) (_|_|_) (_|_|_), tidy } }
end walking_multicospan
namespace walking_multispan
variables {L R : Type v} {fst snd : L → R}
instance [inhabited L] : inhabited (walking_multispan fst snd) :=
⟨left default⟩
/-- Morphisms for `walking_multispan`. -/
inductive hom : Π (a b : walking_multispan fst snd), Type v
| id (A) : hom A A
| fst (a) : hom (left a) (right (fst a))
| snd (a) : hom (left a) (right (snd a))
instance {a : walking_multispan fst snd} : inhabited (hom a a) :=
⟨hom.id _⟩
/-- Composition of morphisms for `walking_multispan`. -/
def hom.comp : Π {A B C : walking_multispan fst snd} (f : hom A B) (g : hom B C),
hom A C
| _ _ _ (hom.id X) f := f
| _ _ _ (hom.fst a) (hom.id X) := hom.fst a
| _ _ _ (hom.snd a) (hom.id X) := hom.snd a
instance : small_category (walking_multispan fst snd) :=
{ hom := hom,
id := hom.id,
comp := λ X Y Z, hom.comp,
id_comp' := by { rintro (_|_) (_|_) (_|_|_), tidy },
comp_id' := by { rintro (_|_) (_|_) (_|_|_), tidy },
assoc' := by { rintro (_|_) (_|_) (_|_) (_|_) (_|_|_) (_|_|_) (_|_|_), tidy } }
end walking_multispan
/-- This is a structure encapsulating the data necessary to define a `multicospan`. -/
@[nolint has_inhabited_instance]
structure multicospan_index (C : Type u) [category.{v} C] :=
(L R : Type v)
(fst_to snd_to : R → L)
(left : L → C)
(right : R → C)
(fst : Π b, left (fst_to b) ⟶ right b)
(snd : Π b, left (snd_to b) ⟶ right b)
/-- This is a structure encapsulating the data necessary to define a `multispan`. -/
@[nolint has_inhabited_instance]
structure multispan_index (C : Type u) [category.{v} C] :=
(L R : Type v)
(fst_from snd_from : L → R)
(left : L → C)
(right : R → C)
(fst : Π a, left a ⟶ right (fst_from a))
(snd : Π a, left a ⟶ right (snd_from a))
namespace multicospan_index
variables {C : Type u} [category.{v} C] (I : multicospan_index C)
/-- The multicospan associated to `I : multicospan_index`. -/
def multicospan : walking_multicospan I.fst_to I.snd_to ⥤ C :=
{ obj := λ x,
match x with
| walking_multicospan.left a := I.left a
| walking_multicospan.right b := I.right b
end,
map := λ x y f,
match x, y, f with
| _, _, walking_multicospan.hom.id x := 𝟙 _
| _, _, walking_multicospan.hom.fst b := I.fst _
| _, _, walking_multicospan.hom.snd b := I.snd _
end,
map_id' := by { rintros (_|_), tidy },
map_comp' := by { rintros (_|_) (_|_) (_|_) (_|_|_) (_|_|_), tidy } }
@[simp] lemma multicospan_obj_left (a) :
I.multicospan.obj (walking_multicospan.left a) = I.left a := rfl
@[simp] lemma multicospan_obj_right (b) :
I.multicospan.obj (walking_multicospan.right b) = I.right b := rfl
@[simp] lemma multicospan_map_fst (b) :
I.multicospan.map (walking_multicospan.hom.fst b) = I.fst b := rfl
@[simp] lemma multicospan_map_snd (b) :
I.multicospan.map (walking_multicospan.hom.snd b) = I.snd b := rfl
variables [has_product I.left] [has_product I.right]
/-- The induced map `∏ I.left ⟶ ∏ I.right` via `I.fst`. -/
noncomputable
def fst_pi_map : ∏ I.left ⟶ ∏ I.right := pi.lift (λ b, pi.π I.left (I.fst_to b) ≫ I.fst b)
/-- The induced map `∏ I.left ⟶ ∏ I.right` via `I.snd`. -/
noncomputable
def snd_pi_map : ∏ I.left ⟶ ∏ I.right := pi.lift (λ b, pi.π I.left (I.snd_to b) ≫ I.snd b)
@[simp, reassoc]
lemma fst_pi_map_π (b) : I.fst_pi_map ≫ pi.π I.right b = pi.π I.left _ ≫ I.fst b :=
by simp [fst_pi_map]
@[simp, reassoc]
lemma snd_pi_map_π (b) : I.snd_pi_map ≫ pi.π I.right b = pi.π I.left _ ≫ I.snd b :=
by simp [snd_pi_map]
/--
Taking the multiequalizer over the multicospan index is equivalent to taking the equalizer over
the two morphsims `∏ I.left ⇉ ∏ I.right`. This is the diagram of the latter.
-/
@[simps] protected noncomputable
def parallel_pair_diagram := parallel_pair I.fst_pi_map I.snd_pi_map
end multicospan_index
namespace multispan_index
variables {C : Type u} [category.{v} C] (I : multispan_index C)
/-- The multispan associated to `I : multispan_index`. -/
def multispan : walking_multispan I.fst_from I.snd_from ⥤ C :=
{ obj := λ x,
match x with
| walking_multispan.left a := I.left a
| walking_multispan.right b := I.right b
end,
map := λ x y f,
match x, y, f with
| _, _, walking_multispan.hom.id x := 𝟙 _
| _, _, walking_multispan.hom.fst b := I.fst _
| _, _, walking_multispan.hom.snd b := I.snd _
end,
map_id' := by { rintros (_|_), tidy },
map_comp' := by { rintros (_|_) (_|_) (_|_) (_|_|_) (_|_|_), tidy } }
@[simp] lemma multispan_obj_left (a) :
I.multispan.obj (walking_multispan.left a) = I.left a := rfl
@[simp] lemma multispan_obj_right (b) :
I.multispan.obj (walking_multispan.right b) = I.right b := rfl
@[simp] lemma multispan_map_fst (a) :
I.multispan.map (walking_multispan.hom.fst a) = I.fst a := rfl
@[simp] lemma multispan_map_snd (a) :
I.multispan.map (walking_multispan.hom.snd a) = I.snd a := rfl
variables [has_coproduct I.left] [has_coproduct I.right]
/-- The induced map `∐ I.left ⟶ ∐ I.right` via `I.fst`. -/
noncomputable
def fst_sigma_map : ∐ I.left ⟶ ∐ I.right := sigma.desc (λ b, I.fst b ≫ sigma.ι _ (I.fst_from b))
/-- The induced map `∐ I.left ⟶ ∐ I.right` via `I.snd`. -/
noncomputable
def snd_sigma_map : ∐ I.left ⟶ ∐ I.right := sigma.desc (λ b, I.snd b ≫ sigma.ι _ (I.snd_from b))
@[simp, reassoc]
lemma ι_fst_sigma_map (b) : sigma.ι I.left b ≫ I.fst_sigma_map = I.fst b ≫ sigma.ι I.right _ :=
by simp [fst_sigma_map]
@[simp, reassoc]
lemma ι_snd_sigma_map (b) : sigma.ι I.left b ≫ I.snd_sigma_map = I.snd b ≫ sigma.ι I.right _ :=
by simp [snd_sigma_map]
/--
Taking the multicoequalizer over the multispan index is equivalent to taking the coequalizer over
the two morphsims `∐ I.left ⇉ ∐ I.right`. This is the diagram of the latter.
-/
protected noncomputable
abbreviation parallel_pair_diagram := parallel_pair I.fst_sigma_map I.snd_sigma_map
end multispan_index
variables {C : Type u} [category.{v} C]
/-- A multifork is a cone over a multicospan. -/
@[nolint has_inhabited_instance]
abbreviation multifork (I : multicospan_index C) := cone I.multicospan
/-- A multicofork is a cocone over a multispan. -/
@[nolint has_inhabited_instance]
abbreviation multicofork (I : multispan_index C) := cocone I.multispan
namespace multifork
variables {I : multicospan_index C} (K : multifork I)
/-- The maps from the cone point of a multifork to the objects on the left. -/
def ι (a : I.L) : K.X ⟶ I.left a :=
K.π.app (walking_multicospan.left _)
@[simp] lemma ι_eq_app_left (a) : K.ι a = K.π.app (walking_multicospan.left _) := rfl
@[simp] lemma app_left_fst (b) :
K.π.app (walking_multicospan.left (I.fst_to b)) ≫ I.fst b =
K.π.app (walking_multicospan.right b) :=
by { rw ← K.w (walking_multicospan.hom.fst b), refl }
@[simp] lemma app_left_snd (b) :
K.π.app (walking_multicospan.left (I.snd_to b)) ≫ I.snd b =
K.π.app (walking_multicospan.right b) :=
by { rw ← K.w (walking_multicospan.hom.snd b), refl }
/-- Construct a multifork using a collection `ι` of morphisms. -/
@[simps]
def of_ι (I : multicospan_index C) (P : C) (ι : Π a, P ⟶ I.left a)
(w : ∀ b, ι (I.fst_to b) ≫ I.fst b = ι (I.snd_to b) ≫ I.snd b) :
multifork I :=
{ X := P,
π :=
{ app := λ x,
match x with
| walking_multicospan.left a := ι _
| walking_multicospan.right b := ι (I.fst_to b) ≫ I.fst b
end,
naturality' := begin
rintros (_|_) (_|_) (_|_|_),
any_goals { symmetry, dsimp, rw category.id_comp, apply category.comp_id },
{ dsimp, rw category.id_comp, refl },
{ dsimp, rw category.id_comp, apply w }
end } }
@[reassoc]
lemma condition (b) :
K.ι (I.fst_to b) ≫ I.fst b = K.ι (I.snd_to b) ≫ I.snd b := by simp
/-- This definition provides a convenient way to show that a multifork is a limit. -/
@[simps]
def is_limit.mk
(lift : Π (E : multifork I), E.X ⟶ K.X)
(fac : ∀ (E : multifork I) (i : I.L), lift E ≫ K.ι i = E.ι i)
(uniq : ∀ (E : multifork I) (m : E.X ⟶ K.X),
(∀ i : I.L, m ≫ K.ι i = E.ι i) → m = lift E) : is_limit K :=
{ lift := lift,
fac' := begin
rintros E (a|b),
{ apply fac },
{ rw [← E.w (walking_multicospan.hom.fst b), ← K.w (walking_multicospan.hom.fst b),
← category.assoc],
congr' 1,
apply fac }
end,
uniq' := begin
rintros E m hm,
apply uniq,
intros i,
apply hm,
end }
variables [has_product I.left] [has_product I.right]
@[simp, reassoc]
lemma pi_condition :
pi.lift K.ι ≫ I.fst_pi_map = pi.lift K.ι ≫ I.snd_pi_map := by { ext, simp }
/-- Given a multifork, we may obtain a fork over `∏ I.left ⇉ ∏ I.right`. -/
@[simps X] noncomputable
def to_pi_fork (K : multifork I) : fork I.fst_pi_map I.snd_pi_map :=
{ X := K.X,
π :=
{ app := λ x,
match x with
| walking_parallel_pair.zero := pi.lift K.ι
| walking_parallel_pair.one := pi.lift K.ι ≫ I.fst_pi_map
end,
naturality' :=
begin
rintros (_|_) (_|_) (_|_|_),
any_goals { symmetry, dsimp, rw category.id_comp, apply category.comp_id },
all_goals { change 𝟙 _ ≫ _ ≫ _ = pi.lift _ ≫ _, simp }
end } }
@[simp] lemma to_pi_fork_π_app_zero :
K.to_pi_fork.π.app walking_parallel_pair.zero = pi.lift K.ι := rfl
@[simp] lemma to_pi_fork_π_app_one :
K.to_pi_fork.π.app walking_parallel_pair.one = pi.lift K.ι ≫ I.fst_pi_map := rfl
variable (I)
/-- Given a fork over `∏ I.left ⇉ ∏ I.right`, we may obtain a multifork. -/
@[simps X] noncomputable
def of_pi_fork (c : fork I.fst_pi_map I.snd_pi_map) : multifork I :=
{ X := c.X,
π :=
{ app := λ x,
match x with
| walking_multicospan.left a := c.ι ≫ pi.π _ _
| walking_multicospan.right b := c.ι ≫ I.fst_pi_map ≫ pi.π _ _
end,
naturality' :=
begin
rintros (_|_) (_|_) (_|_|_),
any_goals { symmetry, dsimp, rw category.id_comp, apply category.comp_id },
{ change 𝟙 _ ≫ _ ≫ _ = (_ ≫ _) ≫ _, simp },
{ change 𝟙 _ ≫ _ ≫ _ = (_ ≫ _) ≫ _, rw c.condition_assoc, simp }
end } }
@[simp] lemma of_pi_fork_π_app_left (c : fork I.fst_pi_map I.snd_pi_map) (a) :
(of_pi_fork I c).π.app (walking_multicospan.left a) = c.ι ≫ pi.π _ _ := rfl
@[simp] lemma of_pi_fork_π_app_right (c : fork I.fst_pi_map I.snd_pi_map) (a) :
(of_pi_fork I c).π.app (walking_multicospan.right a) = c.ι ≫ I.fst_pi_map ≫ pi.π _ _ := rfl
end multifork
namespace multicospan_index
variables (I : multicospan_index C) [has_product I.left] [has_product I.right]
local attribute [tidy] tactic.case_bash
/-- `multifork.to_pi_fork` is functorial. -/
@[simps] noncomputable
def to_pi_fork_functor : multifork I ⥤ fork I.fst_pi_map I.snd_pi_map :=
{ obj := multifork.to_pi_fork, map := λ K₁ K₂ f, { hom := f.hom } }
/-- `multifork.of_pi_fork` is functorial. -/
@[simps] noncomputable
def of_pi_fork_functor : fork I.fst_pi_map I.snd_pi_map ⥤ multifork I :=
{ obj := multifork.of_pi_fork I, map := λ K₁ K₂ f, { hom := f.hom, w' := by rintros (_|_); simp } }
/--
The category of multiforks is equivalent to the category of forks over `∏ I.left ⇉ ∏ I.right`.
It then follows from `category_theory.is_limit_of_preserves_cone_terminal` (or `reflects`) that it
preserves and reflects limit cones.
-/
@[simps] noncomputable
def multifork_equiv_pi_fork : multifork I ≌ fork I.fst_pi_map I.snd_pi_map :=
{ functor := to_pi_fork_functor I,
inverse := of_pi_fork_functor I,
unit_iso := nat_iso.of_components (λ K, cones.ext (iso.refl _) (by rintros (_|_); dsimp; simp))
(λ K₁ K₂ f, by { ext, simp }),
counit_iso := nat_iso.of_components (λ K, fork.ext (iso.refl _) (by { ext, dsimp, simp }))
(λ K₁ K₂ f, by { ext, simp }) }
end multicospan_index
namespace multicofork
variables {I : multispan_index C} (K : multicofork I)
/-- The maps to the cocone point of a multicofork from the objects on the right. -/
def π (b : I.R) : I.right b ⟶ K.X :=
K.ι.app (walking_multispan.right _)
@[simp] lemma π_eq_app_right (b) : K.π b = K.ι.app (walking_multispan.right _) := rfl
@[simp] lemma fst_app_right (a) :
I.fst a ≫ K.ι.app (walking_multispan.right (I.fst_from a)) =
K.ι.app (walking_multispan.left a) :=
by { rw ← K.w (walking_multispan.hom.fst a), refl }
@[simp] lemma snd_app_right (a) :
I.snd a ≫ K.ι.app (walking_multispan.right (I.snd_from a)) =
K.ι.app (walking_multispan.left a) :=
by { rw ← K.w (walking_multispan.hom.snd a), refl }
/-- Construct a multicofork using a collection `π` of morphisms. -/
@[simps]
def of_π (I : multispan_index C) (P : C) (π : Π b, I.right b ⟶ P)
(w : ∀ a, I.fst a ≫ π (I.fst_from a) = I.snd a ≫ π (I.snd_from a)) :
multicofork I :=
{ X := P,
ι :=
{ app := λ x,
match x with
| walking_multispan.left a := I.fst a ≫ π _
| walking_multispan.right b := π _
end,
naturality' := begin
rintros (_|_) (_|_) (_|_|_),
any_goals { dsimp, rw category.comp_id, apply category.id_comp },
{ dsimp, rw category.comp_id, refl },
{ dsimp, rw category.comp_id, apply (w _).symm }
end } }
@[reassoc]
lemma condition (a) :
I.fst a ≫ K.π (I.fst_from a) = I.snd a ≫ K.π (I.snd_from a) := by simp
/-- This definition provides a convenient way to show that a multicofork is a colimit. -/
@[simps]
def is_colimit.mk
(desc : Π (E : multicofork I), K.X ⟶ E.X)
(fac : ∀ (E : multicofork I) (i : I.R), K.π i ≫ desc E = E.π i)
(uniq : ∀ (E : multicofork I) (m : K.X ⟶ E.X),
(∀ i : I.R, K.π i ≫ m = E.π i) → m = desc E) : is_colimit K :=
{ desc := desc,
fac' := begin
rintros S (a|b),
{ rw [← K.w (walking_multispan.hom.fst a), ← S.w (walking_multispan.hom.fst a),
category.assoc],
congr' 1,
apply fac },
{ apply fac },
end,
uniq' := begin
intros S m hm,
apply uniq,
intros i,
apply hm
end }
variables [has_coproduct I.left] [has_coproduct I.right]
@[simp, reassoc]
lemma sigma_condition :
I.fst_sigma_map ≫ sigma.desc K.π = I.snd_sigma_map ≫ sigma.desc K.π := by { ext, simp }
/-- Given a multicofork, we may obtain a cofork over `∐ I.left ⇉ ∐ I.right`. -/
@[simps X] noncomputable
def to_sigma_cofork (K : multicofork I) : cofork I.fst_sigma_map I.snd_sigma_map :=
{ X := K.X,
ι :=
{ app := λ x,
match x with
| walking_parallel_pair.zero := I.fst_sigma_map ≫ sigma.desc K.π
| walking_parallel_pair.one := sigma.desc K.π
end,
naturality' :=
begin
rintros (_|_) (_|_) (_|_|_),
any_goals { dsimp, rw category.comp_id, apply category.id_comp },
all_goals { change _ ≫ sigma.desc _ = (_ ≫ _) ≫ 𝟙 _, simp }
end } }
@[simp] lemma to_sigma_cofork_ι_app_zero :
K.to_sigma_cofork.ι.app walking_parallel_pair.zero = I.fst_sigma_map ≫ sigma.desc K.π := rfl
@[simp] lemma to_sigma_cofork_ι_app_one :
K.to_sigma_cofork.ι.app walking_parallel_pair.one = sigma.desc K.π := rfl
variable (I)
/-- Given a cofork over `∐ I.left ⇉ ∐ I.right`, we may obtain a multicofork. -/
@[simps X] noncomputable
def of_sigma_cofork (c : cofork I.fst_sigma_map I.snd_sigma_map) : multicofork I :=
{ X := c.X,
ι :=
{ app := λ x,
match x with
| walking_multispan.left a := (sigma.ι I.left a : _) ≫ I.fst_sigma_map ≫ c.π
| walking_multispan.right b := (sigma.ι I.right b : _) ≫ c.π
end,
naturality' :=
begin
rintros (_|_) (_|_) (_|_|_),
any_goals { dsimp, rw category.comp_id, apply category.id_comp },
{ change _ ≫ _ ≫ _ = (_ ≫ _) ≫ _,
dsimp, simp [←cofork.left_app_one, -cofork.left_app_one] },
{ change _ ≫ _ ≫ _ = (_ ≫ _) ≫ 𝟙 _,
rw c.condition,
dsimp, simp [←cofork.right_app_one, -cofork.right_app_one] }
end } }
@[simp] lemma of_sigma_cofork_ι_app_left (c : cofork I.fst_sigma_map I.snd_sigma_map) (a) :
(of_sigma_cofork I c).ι.app (walking_multispan.left a) =
(sigma.ι I.left a : _) ≫ I.fst_sigma_map ≫ c.π := rfl
@[simp] lemma of_sigma_cofork_ι_app_right (c : cofork I.fst_sigma_map I.snd_sigma_map) (b) :
(of_sigma_cofork I c).ι.app (walking_multispan.right b) = (sigma.ι I.right b : _) ≫ c.π := rfl
end multicofork
namespace multispan_index
variables (I : multispan_index C) [has_coproduct I.left] [has_coproduct I.right]
local attribute [tidy] tactic.case_bash
/-- `multicofork.to_sigma_cofork` is functorial. -/
@[simps] noncomputable
def to_sigma_cofork_functor : multicofork I ⥤ cofork I.fst_sigma_map I.snd_sigma_map :=
{ obj := multicofork.to_sigma_cofork, map := λ K₁ K₂ f, { hom := f.hom } }
/-- `multicofork.of_sigma_cofork` is functorial. -/
@[simps] noncomputable
def of_sigma_cofork_functor : cofork I.fst_sigma_map I.snd_sigma_map ⥤ multicofork I :=
{ obj := multicofork.of_sigma_cofork I,
map := λ K₁ K₂ f, { hom := f.hom, w' := by rintros (_|_); simp } }
/--
The category of multicoforks is equivalent to the category of coforks over `∐ I.left ⇉ ∐ I.right`.
It then follows from `category_theory.is_colimit_of_preserves_cocone_initial` (or `reflects`) that
it preserves and reflects colimit cocones.
-/
@[simps] noncomputable
def multicofork_equiv_sigma_cofork : multicofork I ≌ cofork I.fst_sigma_map I.snd_sigma_map :=
{ functor := to_sigma_cofork_functor I,
inverse := of_sigma_cofork_functor I,
unit_iso := nat_iso.of_components (λ K, cocones.ext (iso.refl _) (by rintros (_|_); dsimp; simp))
(λ K₁ K₂ f, by { ext, simp }),
counit_iso := nat_iso.of_components (λ K, cofork.ext (iso.refl _) (by { ext, dsimp, simp }))
(λ K₁ K₂ f, by { ext, dsimp, simp, }) }
end multispan_index
/-- For `I : multicospan_index C`, we say that it has a multiequalizer if the associated
multicospan has a limit. -/
abbreviation has_multiequalizer (I : multicospan_index C) :=
has_limit I.multicospan
noncomputable theory
/-- The multiequalizer of `I : multicospan_index C`. -/
abbreviation multiequalizer (I : multicospan_index C) [has_multiequalizer I] : C :=
limit I.multicospan
/-- For `I : multispan_index C`, we say that it has a multicoequalizer if
the associated multicospan has a limit. -/
abbreviation has_multicoequalizer (I : multispan_index C) :=
has_colimit I.multispan
/-- The multiecoqualizer of `I : multispan_index C`. -/
abbreviation multicoequalizer (I : multispan_index C) [has_multicoequalizer I] : C :=
colimit I.multispan
namespace multiequalizer
variables (I : multicospan_index C) [has_multiequalizer I]
/-- The canonical map from the multiequalizer to the objects on the left. -/
abbreviation ι (a : I.L) : multiequalizer I ⟶ I.left a :=
limit.π _ (walking_multicospan.left a)
/-- The multifork associated to the multiequalizer. -/
abbreviation multifork : multifork I :=
limit.cone _
@[simp]
lemma multifork_ι (a) :
(multiequalizer.multifork I).ι a = multiequalizer.ι I a := rfl
@[simp]
lemma multifork_π_app_left (a) :
(multiequalizer.multifork I).π.app (walking_multicospan.left a) =
multiequalizer.ι I a := rfl
@[reassoc]
lemma condition (b) :
multiequalizer.ι I (I.fst_to b) ≫ I.fst b =
multiequalizer.ι I (I.snd_to b) ≫ I.snd b :=
multifork.condition _ _
/-- Construct a morphism to the multiequalizer from its universal property. -/
abbreviation lift (W : C) (k : Π a, W ⟶ I.left a)
(h : ∀ b, k (I.fst_to b) ≫ I.fst b = k (I.snd_to b) ≫ I.snd b) :
W ⟶ multiequalizer I :=
limit.lift _ (multifork.of_ι I _ k h)
@[simp, reassoc]
lemma lift_ι (W : C) (k : Π a, W ⟶ I.left a)
(h : ∀ b, k (I.fst_to b) ≫ I.fst b = k (I.snd_to b) ≫ I.snd b) (a) :
multiequalizer.lift I _ k h ≫ multiequalizer.ι I a = k _ :=
limit.lift_π _ _
@[ext]
lemma hom_ext {W : C} (i j : W ⟶ multiequalizer I)
(h : ∀ a, i ≫ multiequalizer.ι I a =
j ≫ multiequalizer.ι I a) :
i = j :=
limit.hom_ext
begin
rintro (a|b),
{ apply h },
simp_rw [← limit.w I.multicospan (walking_multicospan.hom.fst b),
← category.assoc, h],
end
variables [has_product I.left] [has_product I.right]
instance : has_equalizer I.fst_pi_map I.snd_pi_map :=
⟨⟨⟨_,is_limit.of_preserves_cone_terminal
I.multifork_equiv_pi_fork.functor (limit.is_limit _)⟩⟩⟩
/-- The multiequalizer is isomorphic to the equalizer of `∏ I.left ⇉ ∏ I.right`. -/
def iso_equalizer : multiequalizer I ≅ equalizer I.fst_pi_map I.snd_pi_map :=
limit.iso_limit_cone ⟨_, is_limit.of_preserves_cone_terminal
I.multifork_equiv_pi_fork.inverse (limit.is_limit _)⟩
/-- The canonical injection `multiequalizer I ⟶ ∏ I.left`. -/
def ι_pi : multiequalizer I ⟶ ∏ I.left :=
(iso_equalizer I).hom ≫ equalizer.ι I.fst_pi_map I.snd_pi_map
@[simp, reassoc]
lemma ι_pi_π (a) : ι_pi I ≫ pi.π I.left a = ι I a :=
by { rw [ι_pi, category.assoc, ← iso.eq_inv_comp, iso_equalizer], simpa }
instance : mono (ι_pi I) := @@mono_comp _ _ _ _ equalizer.ι_mono
end multiequalizer
namespace multicoequalizer
variables (I : multispan_index C) [has_multicoequalizer I]
/-- The canonical map from the multiequalizer to the objects on the left. -/
abbreviation π (b : I.R) : I.right b ⟶ multicoequalizer I :=
colimit.ι I.multispan (walking_multispan.right _)
/-- The multicofork associated to the multicoequalizer. -/
abbreviation multicofork : multicofork I :=
colimit.cocone _
@[simp]
lemma multicofork_π (b) :
(multicoequalizer.multicofork I).π b = multicoequalizer.π I b := rfl
@[simp]
lemma multicofork_ι_app_right (b) :
(multicoequalizer.multicofork I).ι.app (walking_multispan.right b) =
multicoequalizer.π I b := rfl
@[reassoc]
lemma condition (a) :
I.fst a ≫ multicoequalizer.π I (I.fst_from a) =
I.snd a ≫ multicoequalizer.π I (I.snd_from a) :=
multicofork.condition _ _
/-- Construct a morphism from the multicoequalizer from its universal property. -/
abbreviation desc (W : C) (k : Π b, I.right b ⟶ W)
(h : ∀ a, I.fst a ≫ k (I.fst_from a) = I.snd a ≫ k (I.snd_from a)) :
multicoequalizer I ⟶ W :=
colimit.desc _ (multicofork.of_π I _ k h)
@[simp, reassoc]
lemma π_desc (W : C) (k : Π b, I.right b ⟶ W)
(h : ∀ a, I.fst a ≫ k (I.fst_from a) = I.snd a ≫ k (I.snd_from a)) (b) :
multicoequalizer.π I b ≫ multicoequalizer.desc I _ k h = k _ :=
colimit.ι_desc _ _
@[ext]
lemma hom_ext {W : C} (i j : multicoequalizer I ⟶ W)
(h : ∀ b, multicoequalizer.π I b ≫ i = multicoequalizer.π I b ≫ j) :
i = j :=
colimit.hom_ext
begin
rintro (a|b),
{ simp_rw [← colimit.w I.multispan (walking_multispan.hom.fst a),
category.assoc, h] },
{ apply h },
end
variables [has_coproduct I.left] [has_coproduct I.right]
instance : has_coequalizer I.fst_sigma_map I.snd_sigma_map :=
⟨⟨⟨_,is_colimit.of_preserves_cocone_initial
I.multicofork_equiv_sigma_cofork.functor (colimit.is_colimit _)⟩⟩⟩
/-- The multicoequalizer is isomorphic to the coequalizer of `∐ I.left ⇉ ∐ I.right`. -/
def iso_coequalizer : multicoequalizer I ≅ coequalizer I.fst_sigma_map I.snd_sigma_map :=
colimit.iso_colimit_cocone ⟨_, is_colimit.of_preserves_cocone_initial
I.multicofork_equiv_sigma_cofork.inverse (colimit.is_colimit _)⟩
/-- The canonical projection `∐ I.right ⟶ multicoequalizer I`. -/
def sigma_π : ∐ I.right ⟶ multicoequalizer I :=
coequalizer.π I.fst_sigma_map I.snd_sigma_map ≫ (iso_coequalizer I).inv
@[simp, reassoc]
lemma ι_sigma_π (b) : sigma.ι I.right b ≫ sigma_π I = π I b :=
by { rw [sigma_π, ← category.assoc, iso.comp_inv_eq, iso_coequalizer], simpa }
instance : epi (sigma_π I) := @@epi_comp _ _ coequalizer.π_epi _ _
end multicoequalizer
end category_theory.limits
|
012d22a972c40dca771fdf51a5800e0fdef390cd | bc3bab676131ea3e06edb46172d92cf30b89311d | /tutorial 3.lean | 3550cb97194a0a1ad4e5fe64a312368266a033e5 | [] | no_license | enharsha/hello-world | fc13031788876fdab33d28c0834370896e85f356 | ef81d688af1aed47ffadf6662b80db5d9eba587d | refs/heads/master | 1,597,391,078,827 | 1,575,926,406,000 | 1,575,926,406,000 | 215,125,509 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,987 | lean | variables (α:Type) (p q :α → Prop)
/-Question-/
example: (∀x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) :=
iff.intro
(assume h: (∀ x,p x ∧ q x), and.intro (assume h1 : α, (h h1).left) (assume h2 : α, (h h2).right))
(assume h: (∀ x, p x) ∧ (∀ x, q x), assume h1 : α, and.intro (h.left h1) (h.right h1))
example: (∀ x, p x → q x) → (∀ x, p x) → (∀ x, q x) :=
assume h1: (∀ x, p x → q x), assume h2: (∀ x, p x),assume h3: α, (h1 h3) (h2 h3)
example: (∀ x, p x) ∨ (∀ x, q x) → ∀ x, p x ∨ q x :=
assume h: (∀ x, p x) ∨ (∀ x, q x),assume x : α,
or.elim h (assume h1 : ∀ x, p x, or.inl (h1 x)) (assume h2 : ∀ x, q x, or.inr (h2 x))
/-Question-/
variables (men : Type) (barber : men)
variable (shaves : men → men → Prop)
example (h : ∀ x : men, shaves barber x ↔ ¬ shaves x x) : false :=
have h1 : shaves barber barber ↔ ¬shaves barber barber, from (h barber),
have h2 : ¬shaves barber barber, from (assume h3 : shaves barber barber, absurd h3 (h1.mp h3)),
false.elim (h2 (h1.mpr h2))
/-Question-/
namespace hidden
def divides (m n : ℕ) : Prop := ∃ k, m * k = n
instance : has_dvd nat := ⟨divides⟩
def even (n : ℕ) : Prop := 2 ∣ n
def prime (n : ℕ) : Prop := ∀ m, (m ∣ n) → (m=1) ∨ (m=n)
def infinitely_many_primes : Prop := ∀ n, ∃ m, (m > n) ∧ prime m
def Fermat_number (n : ℕ) : Prop := ∃ m:nat, n=2^(2^m) + 1
def Fermat_prime (n : ℕ) : Prop := prime n ∧ Fermat_number n
def infinitely_many_Fermat_primes : Prop := ∀ n, ∃ m,(m > n) ∧ Fermat_prime m
def goldbach_conjecture : Prop := ∀ n, (n > 2) → ∃ p q, (p + q = n) ∧ prime p ∧ prime q
def Goldbach's_weak_conjecture : Prop := ∀ n, ¬even n ∧ (n > 5) → ∃ p p1 p2, (p + p1 + p2 = n) ∧ prime p ∧ prime p1 ∧ prime p2
def Fermat's_last_theorem : Prop := ∀ n, (n > 2) → ¬∃ a b c, a^n + b^n = c^n ∧ (a > 0) ∧ (b > 0) ∧ (c > 0)
end hidden
|
b8cff5200f00c7f71e194ca3a23d4ea798995f24 | 22e97a5d648fc451e25a06c668dc03ac7ed7bc25 | /src/algebra/category/CommRing/adjunctions.lean | 94b73b4de8994734ea5865f559982b36c929e791 | [
"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 | 1,580 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Johannes Hölzl
-/
import algebra.category.CommRing.basic
import data.mv_polynomial
/-!
Multivariable polynomials on a type is the left adjoint of the
forgetful functor from commutative rings to types.
-/
noncomputable theory
universe u
open mv_polynomial
open category_theory
namespace CommRing
open_locale classical
/--
The free functor `Type u ⥤ CommRing.{u}` sending a type `X` to the multivariable (commutative)
polynomials with variables `x : X`.
-/
def free : Type u ⥤ CommRing.{u} :=
{ obj := λ α, of (mv_polynomial α ℤ),
-- TODO this should just be `ring_hom.of (rename f)`, but this causes a mysterious deterministic timeout!
map := λ X Y f, @ring_hom.of _ _ _ _ (rename f) (by apply_instance),
-- TODO these next two fields can be done by `tidy`, but the calls in `dsimp` and `simp` it generates are too slow.
map_id' := λ X, ring_hom.ext $ rename_id,
map_comp' := λ X Y Z f g, ring_hom.ext $ λ p, (rename_rename f g p).symm }
@[simp] lemma free_obj_coe {α : Type u} :
(free.obj α : Type u) = mv_polynomial α ℤ := rfl
@[simp] lemma free_map_coe {α β : Type u} {f : α → β} :
⇑(free.map f) = rename f := rfl
/--
The free-forgetful adjunction for commutative rings.
-/
def adj : free ⊣ forget CommRing :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ X R, hom_equiv,
hom_equiv_naturality_left_symm' := by {intros, ext, dsimp, apply eval₂_cast_comp} }
end CommRing
|
42e3b53fcc06a7b9883d0ce9bef52d08b18a804b | c3f2fcd060adfa2ca29f924839d2d925e8f2c685 | /tests/lean/open_tst.lean | bcf4f8ab65940483e4b656005a16be67b8b5ab91 | [
"Apache-2.0"
] | permissive | respu/lean | 6582d19a2f2838a28ecd2b3c6f81c32d07b5341d | 8c76419c60b63d0d9f7bc04ebb0b99812d0ec654 | refs/heads/master | 1,610,882,451,231 | 1,427,747,084,000 | 1,427,747,429,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 836 | lean | context
open [notations] [coercions] nat
check 1 + 2
check add -- Error aliases were not created
end
context
open [declarations] [notations] nat
variable a : nat
check a + a
check add a a
check a + 1 -- Error coercion from num to nat was not loaded
end
context
open - [classes] nat
variable a : nat
check a + a
check add a a
check a + 1
definition foo1 : inhabited nat :=
_ -- Error inhabited instances was not loaded
end
context
open - [classes] [decls] nat
variable a : nat
check a + a
check add a a -- Error aliases were not created
check a + 1
definition foo2 : inhabited nat :=
_ -- Error inhabited instances was not loaded
end
context
open [classes] nat
definition foo3 : inhabited nat :=
_
variable a : nat
check a + a -- Error notation declarations were not loaded
end
|
88d092d5c87f4dede4ecccdc9fa01e726d7236ba | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/representation_theory/maschke.lean | 3392e60864210be339a2eea7a5b469c4f55c86db | [
"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 | 5,837 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Scott Morrison
-/
import algebra.monoid_algebra
import algebra.invertible
import algebra.char_p.basic
import linear_algebra.basis
/-!
# Maschke's theorem
We prove Maschke's theorem for finite groups,
in the formulation that every submodule of a `k[G]` module has a complement,
when `k` is a field with `¬(ring_char k ∣ fintype.card G)`.
We do the core computation in greater generality.
For any `[comm_ring k]` in which `[invertible (fintype.card G : k)]`,
and a `k[G]`-linear map `i : V → W` which admits a `k`-linear retraction `π`,
we produce a `k[G]`-linear retraction by
taking the average over `G` of the conjugates of `π`.
## Future work
It's not so far to give the usual statement, that every finite dimensional representation
of a finite group is semisimple (i.e. a direct sum of irreducibles).
-/
universes u
noncomputable theory
open semimodule
open monoid_algebra
open_locale big_operators
section
-- At first we work with any `[comm_ring k]`, and add the assumption that
-- `[invertible (fintype.card G : k)]` when it is required.
variables {k : Type u} [comm_ring k] {G : Type u} [group G]
variables {V : Type u} [add_comm_group V] [module k V] [module (monoid_algebra k G) V]
variables [is_scalar_tower k (monoid_algebra k G) V]
variables {W : Type u} [add_comm_group W] [module k W] [module (monoid_algebra k G) W]
variables [is_scalar_tower k (monoid_algebra k G) W]
/-!
We now do the key calculation in Maschke's theorem.
Given `V → W`, an inclusion of `k[G]` modules,,
assume we have some retraction `π` (i.e. `∀ v, π (i v) = v`),
just as a `k`-linear map.
(When `k` is a field, this will be available cheaply, by choosing a basis.)
We now construct a retraction of the inclusion as a `k[G]`-linear map,
by the formula
$$ \frac{1}{|G|} \sum_{g \in G} g⁻¹ • π(g • -). $$
-/
namespace linear_map
variables (π : W →ₗ[k] V)
include π
/--
We define the conjugate of `π` by `g`, as a `k`-linear map.
-/
def conjugate (g : G) : W →ₗ[k] V :=
((group_smul.linear_map k V g⁻¹).comp π).comp (group_smul.linear_map k W g)
variables (i : V →ₗ[monoid_algebra k G] W) (h : ∀ v : V, π (i v) = v)
section
include h
lemma conjugate_i (g : G) (v : V) : (conjugate π g) (i v) = v :=
begin
dsimp [conjugate],
simp only [←i.map_smul, h, ←mul_smul, single_mul_single, mul_one, mul_left_inv],
change (1 : monoid_algebra k G) • v = v,
simp,
end
end
variables (G) [fintype G]
/--
The sum of the conjugates of `π` by each element `g : G`, as a `k`-linear map.
(We postpone dividing by the size of the group as long as possible.)
-/
def sum_of_conjugates : W →ₗ[k] V :=
∑ g : G, π.conjugate g
/--
In fact, the sum over `g : G` of the conjugate of `π` by `g` is a `k[G]`-linear map.
-/
def sum_of_conjugates_equivariant : W →ₗ[monoid_algebra k G] V :=
monoid_algebra.equivariant_of_linear_of_comm (π.sum_of_conjugates G) (λ g v,
begin
dsimp [sum_of_conjugates],
simp only [linear_map.sum_apply, finset.smul_sum],
dsimp [conjugate],
conv_lhs {
rw [←finset.univ_map_embedding (mul_right_embedding g⁻¹)],
simp only [mul_right_embedding],
},
simp only [←mul_smul, single_mul_single, mul_inv_rev, mul_one, function.embedding.coe_fn_mk,
finset.sum_map, inv_inv, inv_mul_cancel_right],
recover,
end)
section
variables [inv : invertible (fintype.card G : k)]
include inv
/--
We construct our `k[G]`-linear retraction of `i` as
$$ \frac{1}{|G|} \sum_{g \in G} g⁻¹ • π(g • -). $$
-/
def equivariant_projection : W →ₗ[monoid_algebra k G] V :=
⅟(fintype.card G : k) • (π.sum_of_conjugates_equivariant G)
include h
lemma equivariant_projection_condition (v : V) : (π.equivariant_projection G) (i v) = v :=
begin
rw [equivariant_projection, smul_apply, sum_of_conjugates_equivariant,
equivariant_of_linear_of_comm_apply, sum_of_conjugates],
rw [linear_map.sum_apply],
simp only [conjugate_i π i h],
rw [finset.sum_const, finset.card_univ, nsmul_eq_smul_cast k,
←mul_smul, invertible.inv_of_mul_self, one_smul],
end
end
end linear_map
end
-- Now we work over a `[field k]`, and replace the assumption `[invertible (fintype.card G : k)]`
-- with `¬(ring_char k ∣ fintype.card G)`.
variables {k : Type u} [field k] {G : Type u} [fintype G] [group G]
variables {V : Type u} [add_comm_group V] [module k V] [module (monoid_algebra k G) V]
variables [is_scalar_tower k (monoid_algebra k G) V]
variables {W : Type u} [add_comm_group W] [module k W] [module (monoid_algebra k G) W]
variables [is_scalar_tower k (monoid_algebra k G) W]
lemma monoid_algebra.exists_left_inverse_of_injective
(not_dvd : ¬(ring_char k ∣ fintype.card G)) (f : V →ₗ[monoid_algebra k G] W) (hf : f.ker = ⊥) :
∃ (g : W →ₗ[monoid_algebra k G] V), g.comp f = linear_map.id :=
begin
haveI : invertible (fintype.card G : k) :=
invertible_of_ring_char_not_dvd not_dvd,
obtain ⟨φ, hφ⟩ := (f.restrict_scalars k).exists_left_inverse_of_injective
(by simp only [hf, submodule.restrict_scalars_bot, linear_map.ker_restrict_scalars]),
refine ⟨φ.equivariant_projection G, _⟩,
ext v,
simp only [linear_map.id_coe, id.def, linear_map.comp_apply],
apply linear_map.equivariant_projection_condition,
intro v,
have := congr_arg linear_map.to_fun hφ,
exact congr_fun this v
end
lemma monoid_algebra.submodule.exists_is_compl
(not_dvd : ¬(ring_char k ∣ fintype.card G)) (p : submodule (monoid_algebra k G) V) :
∃ q : submodule (monoid_algebra k G) V, is_compl p q :=
let ⟨f, hf⟩ := monoid_algebra.exists_left_inverse_of_injective not_dvd p.subtype p.ker_subtype in
⟨f.ker, linear_map.is_compl_of_proj $ linear_map.ext_iff.1 hf⟩
|
990b8d162d4aa0006a0b6793049e153d983a52a3 | ce6917c5bacabee346655160b74a307b4a5ab620 | /src/ch3/ex0320.lean | ff5e921ebe9f400cba5796ccc4634324dcfbdb23 | [] | no_license | Ailrun/Theorem_Proving_in_Lean | ae6a23f3c54d62d401314d6a771e8ff8b4132db2 | 2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68 | refs/heads/master | 1,609,838,270,467 | 1,586,846,743,000 | 1,586,846,743,000 | 240,967,761 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 185 | lean | variables p q : Prop
theorem and_swap : p ∧ q ↔ q ∧ p :=
⟨λ h, ⟨h.right, h.left⟩, λ h, ⟨h.right, h.left⟩⟩
example (h : p ∧ q) : q ∧ p := (and_swap p q).mp h
|
0b89dfb515b4a28dfecc061dde619a7f9d504cc5 | 86d27503307c38df1c2f623003db02428c647dc6 | /tests/lean/run/check_constants.lean | 3bc4b8824e697a6955a9552a25a90fc5821b02ca | [
"Apache-2.0"
] | permissive | david-christiansen/lean | ec01bce37268b3e442272832ea955c48a8b92a92 | 89136339ff8c551a2313b22e92d38c1d76d33606 | refs/heads/master | 1,594,457,625,583 | 1,497,312,913,000 | 1,497,325,368,000 | 94,268,567 | 0 | 0 | null | 1,497,397,719,000 | 1,497,397,719,000 | null | UTF-8 | Lean | false | false | 14,863 | lean | -- DO NOT EDIT, automatically generated file, generator scripts/gen_constants_cpp.py
import smt system.io
open tactic
meta def script_check_id (n : name) : tactic unit :=
do env ← get_env, (env^.get n >> return ()) <|> (guard $ env^.is_namespace n) <|> (attribute.get_instances n >> return ()) <|> fail ("identifier '" ++ to_string n ++ "' is not a constant, namespace nor attribute")
run_cmd script_check_id `abs
run_cmd script_check_id `absurd
run_cmd script_check_id `acc.cases_on
run_cmd script_check_id `add_comm_group
run_cmd script_check_id `add_comm_semigroup
run_cmd script_check_id `add_group
run_cmd script_check_id `add_monoid
run_cmd script_check_id `and
run_cmd script_check_id `and.elim_left
run_cmd script_check_id `and.elim_right
run_cmd script_check_id `and.intro
run_cmd script_check_id `and.rec
run_cmd script_check_id `and.cases_on
run_cmd script_check_id `auto_param
run_cmd script_check_id `bit0
run_cmd script_check_id `bit1
run_cmd script_check_id `bool
run_cmd script_check_id `bool.ff
run_cmd script_check_id `bool.tt
run_cmd script_check_id `combinator.K
run_cmd script_check_id `caching_user_attribute
run_cmd script_check_id `cast
run_cmd script_check_id `cast_heq
run_cmd script_check_id `char
run_cmd script_check_id `char.of_nat
run_cmd script_check_id `char.of_nat_ne_of_ne
run_cmd script_check_id `classical.prop_decidable
run_cmd script_check_id `classical.type_decidable_eq
run_cmd script_check_id `coe
run_cmd script_check_id `coe_fn
run_cmd script_check_id `coe_sort
run_cmd script_check_id `coe_to_lift
run_cmd script_check_id `congr
run_cmd script_check_id `congr_arg
run_cmd script_check_id `congr_fun
run_cmd script_check_id `decidable
run_cmd script_check_id `decidable.to_bool
run_cmd script_check_id `distrib
run_cmd script_check_id `dite
run_cmd script_check_id `id
run_cmd script_check_id `empty
run_cmd script_check_id `Exists
run_cmd script_check_id `eq
run_cmd script_check_id `eq.cases_on
run_cmd script_check_id `eq.drec
run_cmd script_check_id `eq.mp
run_cmd script_check_id `eq.mpr
run_cmd script_check_id `eq.rec
run_cmd script_check_id `eq.refl
run_cmd script_check_id `eq.subst
run_cmd script_check_id `eq.symm
run_cmd script_check_id `eq.trans
run_cmd script_check_id `eq_of_heq
run_cmd script_check_id `eq_rec_heq
run_cmd script_check_id `eq_true_intro
run_cmd script_check_id `eq_false_intro
run_cmd script_check_id `eq_self_iff_true
run_cmd script_check_id `expr
run_cmd script_check_id `expr.subst
run_cmd script_check_id `format
run_cmd script_check_id `false
run_cmd script_check_id `false_of_true_iff_false
run_cmd script_check_id `false_of_true_eq_false
run_cmd script_check_id `true_eq_false_of_false
run_cmd script_check_id `false.rec
run_cmd script_check_id `field
run_cmd script_check_id `fin.mk
run_cmd script_check_id `fin.ne_of_vne
run_cmd script_check_id `forall_congr
run_cmd script_check_id `forall_congr_eq
run_cmd script_check_id `forall_not_of_not_exists
run_cmd script_check_id `funext
run_cmd script_check_id `ge
run_cmd script_check_id `gt
run_cmd script_check_id `has_add
run_cmd script_check_id `has_add.add
run_cmd script_check_id `andthen
run_cmd script_check_id `has_bind.and_then
run_cmd script_check_id `has_bind.bind
run_cmd script_check_id `has_bind.seq
run_cmd script_check_id `has_div
run_cmd script_check_id `has_div.div
run_cmd script_check_id `has_emptyc.emptyc
run_cmd script_check_id `has_mod.mod
run_cmd script_check_id `has_mul
run_cmd script_check_id `has_mul.mul
run_cmd script_check_id `has_insert.insert
run_cmd script_check_id `has_inv
run_cmd script_check_id `has_inv.inv
run_cmd script_check_id `has_le
run_cmd script_check_id `has_le.le
run_cmd script_check_id `has_lt
run_cmd script_check_id `has_lt.lt
run_cmd script_check_id `has_neg
run_cmd script_check_id `has_neg.neg
run_cmd script_check_id `has_one
run_cmd script_check_id `has_one.one
run_cmd script_check_id `has_orelse.orelse
run_cmd script_check_id `has_sep.sep
run_cmd script_check_id `has_sizeof
run_cmd script_check_id `has_sizeof.mk
run_cmd script_check_id `has_sub
run_cmd script_check_id `has_sub.sub
run_cmd script_check_id `has_to_format
run_cmd script_check_id `has_to_string
run_cmd script_check_id `has_well_founded
run_cmd script_check_id `has_well_founded.r
run_cmd script_check_id `has_well_founded.wf
run_cmd script_check_id `has_zero
run_cmd script_check_id `has_zero.zero
run_cmd script_check_id `has_coe_t
run_cmd script_check_id `heq
run_cmd script_check_id `heq.refl
run_cmd script_check_id `heq.symm
run_cmd script_check_id `heq.trans
run_cmd script_check_id `heq_of_eq
run_cmd script_check_id `id_locked
run_cmd script_check_id `if_neg
run_cmd script_check_id `if_pos
run_cmd script_check_id `iff
run_cmd script_check_id `iff_false_intro
run_cmd script_check_id `iff.intro
run_cmd script_check_id `iff.mp
run_cmd script_check_id `iff.mpr
run_cmd script_check_id `iff.refl
run_cmd script_check_id `iff.symm
run_cmd script_check_id `iff.trans
run_cmd script_check_id `iff_true_intro
run_cmd script_check_id `imp_congr
run_cmd script_check_id `imp_congr_eq
run_cmd script_check_id `imp_congr_ctx
run_cmd script_check_id `imp_congr_ctx_eq
run_cmd script_check_id `implies
run_cmd script_check_id `implies_of_if_neg
run_cmd script_check_id `implies_of_if_pos
run_cmd script_check_id `int
run_cmd script_check_id `int.has_add
run_cmd script_check_id `int.has_mul
run_cmd script_check_id `int.has_sub
run_cmd script_check_id `int.has_div
run_cmd script_check_id `int.has_le
run_cmd script_check_id `int.has_lt
run_cmd script_check_id `int.has_neg
run_cmd script_check_id `int.has_mod
run_cmd script_check_id `int.bit0_nonneg
run_cmd script_check_id `int.bit1_nonneg
run_cmd script_check_id `int.one_nonneg
run_cmd script_check_id `int.zero_nonneg
run_cmd script_check_id `int.bit0_pos
run_cmd script_check_id `int.bit1_pos
run_cmd script_check_id `int.one_pos
run_cmd script_check_id `int.nat_abs_zero
run_cmd script_check_id `int.nat_abs_one
run_cmd script_check_id `int.nat_abs_bit0_step
run_cmd script_check_id `int.nat_abs_bit1_nonneg_step
run_cmd script_check_id `int.ne_of_nat_ne_nonneg_case
run_cmd script_check_id `int.ne_neg_of_ne
run_cmd script_check_id `int.neg_ne_of_pos
run_cmd script_check_id `int.ne_neg_of_pos
run_cmd script_check_id `int.neg_ne_zero_of_ne
run_cmd script_check_id `int.zero_ne_neg_of_ne
run_cmd script_check_id `int.decidable_linear_ordered_comm_group
run_cmd script_check_id `interactive.param_desc
run_cmd script_check_id `interactive.parse
run_cmd script_check_id `io
run_cmd script_check_id `io.interface
run_cmd script_check_id `is_associative
run_cmd script_check_id `is_associative.assoc
run_cmd script_check_id `is_commutative
run_cmd script_check_id `is_commutative.comm
run_cmd script_check_id `ite
run_cmd script_check_id `lean.parser
run_cmd script_check_id `lean.parser.pexpr
run_cmd script_check_id `lean.parser.tk
run_cmd script_check_id `left_distrib
run_cmd script_check_id `left_comm
run_cmd script_check_id `le_refl
run_cmd script_check_id `linear_ordered_ring
run_cmd script_check_id `linear_ordered_semiring
run_cmd script_check_id `list
run_cmd script_check_id `list.nil
run_cmd script_check_id `list.cons
run_cmd script_check_id `match_failed
run_cmd script_check_id `monad
run_cmd script_check_id `monad_fail
run_cmd script_check_id `monoid
run_cmd script_check_id `mul_one
run_cmd script_check_id `mul_zero
run_cmd script_check_id `mul_zero_class
run_cmd script_check_id `name.anonymous
run_cmd script_check_id `name.mk_numeral
run_cmd script_check_id `name.mk_string
run_cmd script_check_id `nat
run_cmd script_check_id `nat.succ
run_cmd script_check_id `nat.zero
run_cmd script_check_id `nat.has_zero
run_cmd script_check_id `nat.has_one
run_cmd script_check_id `nat.has_add
run_cmd script_check_id `nat.add
run_cmd script_check_id `nat.cases_on
run_cmd script_check_id `nat.bit0_ne
run_cmd script_check_id `nat.bit0_ne_bit1
run_cmd script_check_id `nat.bit0_ne_zero
run_cmd script_check_id `nat.bit0_ne_one
run_cmd script_check_id `nat.bit1_ne
run_cmd script_check_id `nat.bit1_ne_bit0
run_cmd script_check_id `nat.bit1_ne_zero
run_cmd script_check_id `nat.bit1_ne_one
run_cmd script_check_id `nat.zero_ne_one
run_cmd script_check_id `nat.zero_ne_bit0
run_cmd script_check_id `nat.zero_ne_bit1
run_cmd script_check_id `nat.one_ne_zero
run_cmd script_check_id `nat.one_ne_bit0
run_cmd script_check_id `nat.one_ne_bit1
run_cmd script_check_id `nat.bit0_lt
run_cmd script_check_id `nat.bit1_lt
run_cmd script_check_id `nat.bit0_lt_bit1
run_cmd script_check_id `nat.bit1_lt_bit0
run_cmd script_check_id `nat.zero_lt_one
run_cmd script_check_id `nat.zero_lt_bit1
run_cmd script_check_id `nat.zero_lt_bit0
run_cmd script_check_id `nat.one_lt_bit0
run_cmd script_check_id `nat.one_lt_bit1
run_cmd script_check_id `nat.le_of_lt
run_cmd script_check_id `nat.le_refl
run_cmd script_check_id `ne
run_cmd script_check_id `neq_of_not_iff
run_cmd script_check_id `norm_num.add1
run_cmd script_check_id `norm_num.add1_bit0
run_cmd script_check_id `norm_num.add1_bit1_helper
run_cmd script_check_id `norm_num.add1_one
run_cmd script_check_id `norm_num.add1_zero
run_cmd script_check_id `norm_num.add_div_helper
run_cmd script_check_id `norm_num.bin_add_zero
run_cmd script_check_id `norm_num.bin_zero_add
run_cmd script_check_id `norm_num.bit0_add_bit0_helper
run_cmd script_check_id `norm_num.bit0_add_bit1_helper
run_cmd script_check_id `norm_num.bit0_add_one
run_cmd script_check_id `norm_num.bit1_add_bit0_helper
run_cmd script_check_id `norm_num.bit1_add_bit1_helper
run_cmd script_check_id `norm_num.bit1_add_one_helper
run_cmd script_check_id `norm_num.div_add_helper
run_cmd script_check_id `norm_num.div_eq_div_helper
run_cmd script_check_id `norm_num.div_helper
run_cmd script_check_id `norm_num.div_mul_helper
run_cmd script_check_id `norm_num.mk_cong
run_cmd script_check_id `norm_num.mul_bit0_helper
run_cmd script_check_id `norm_num.mul_bit1_helper
run_cmd script_check_id `norm_num.mul_div_helper
run_cmd script_check_id `norm_num.neg_add_neg_helper
run_cmd script_check_id `norm_num.neg_add_pos_helper1
run_cmd script_check_id `norm_num.neg_add_pos_helper2
run_cmd script_check_id `norm_num.neg_mul_neg_helper
run_cmd script_check_id `norm_num.neg_mul_pos_helper
run_cmd script_check_id `norm_num.neg_neg_helper
run_cmd script_check_id `norm_num.neg_zero_helper
run_cmd script_check_id `norm_num.nonneg_bit0_helper
run_cmd script_check_id `norm_num.nonneg_bit1_helper
run_cmd script_check_id `norm_num.nonzero_of_div_helper
run_cmd script_check_id `norm_num.nonzero_of_neg_helper
run_cmd script_check_id `norm_num.nonzero_of_pos_helper
run_cmd script_check_id `norm_num.one_add_bit0
run_cmd script_check_id `norm_num.one_add_bit1_helper
run_cmd script_check_id `norm_num.one_add_one
run_cmd script_check_id `norm_num.pos_add_neg_helper
run_cmd script_check_id `norm_num.pos_bit0_helper
run_cmd script_check_id `norm_num.pos_bit1_helper
run_cmd script_check_id `norm_num.pos_mul_neg_helper
run_cmd script_check_id `norm_num.sub_nat_zero_helper
run_cmd script_check_id `norm_num.sub_nat_pos_helper
run_cmd script_check_id `norm_num.subst_into_div
run_cmd script_check_id `norm_num.subst_into_prod
run_cmd script_check_id `norm_num.subst_into_subtr
run_cmd script_check_id `norm_num.subst_into_sum
run_cmd script_check_id `not
run_cmd script_check_id `not_of_iff_false
run_cmd script_check_id `not_of_eq_false
run_cmd script_check_id `of_eq_true
run_cmd script_check_id `of_iff_true
run_cmd script_check_id `opt_param
run_cmd script_check_id `or
run_cmd script_check_id `inout_param
run_cmd script_check_id `punit
run_cmd script_check_id `punit.star
run_cmd script_check_id `prod.mk
run_cmd script_check_id `pprod
run_cmd script_check_id `pprod.mk
run_cmd script_check_id `pprod.fst
run_cmd script_check_id `pprod.snd
run_cmd script_check_id `propext
run_cmd script_check_id `to_pexpr
run_cmd script_check_id `quot.mk
run_cmd script_check_id `quot.lift
run_cmd script_check_id `real
run_cmd script_check_id `real.of_int
run_cmd script_check_id `real.to_int
run_cmd script_check_id `real.is_int
run_cmd script_check_id `real.has_neg
run_cmd script_check_id `real.has_div
run_cmd script_check_id `real.has_add
run_cmd script_check_id `real.has_mul
run_cmd script_check_id `real.has_sub
run_cmd script_check_id `real.has_lt
run_cmd script_check_id `real.has_le
run_cmd script_check_id `reflected
run_cmd script_check_id `reflected.subst
run_cmd script_check_id `rfl
run_cmd script_check_id `right_distrib
run_cmd script_check_id `ring
run_cmd script_check_id `scope_trace
run_cmd script_check_id `set_of
run_cmd script_check_id `semiring
run_cmd script_check_id `psigma
run_cmd script_check_id `psigma.cases_on
run_cmd script_check_id `psigma.mk
run_cmd script_check_id `psigma.fst
run_cmd script_check_id `psigma.snd
run_cmd script_check_id `singleton
run_cmd script_check_id `sizeof
run_cmd script_check_id `smt.array
run_cmd script_check_id `smt.select
run_cmd script_check_id `smt.store
run_cmd script_check_id `smt.prove
run_cmd script_check_id `string
run_cmd script_check_id `string.empty
run_cmd script_check_id `string.str
run_cmd script_check_id `string.empty_ne_str
run_cmd script_check_id `string.str_ne_empty
run_cmd script_check_id `string.str_ne_str_left
run_cmd script_check_id `string.str_ne_str_right
run_cmd script_check_id `subsingleton
run_cmd script_check_id `subsingleton.elim
run_cmd script_check_id `subsingleton.helim
run_cmd script_check_id `subtype
run_cmd script_check_id `subtype.mk
run_cmd script_check_id `subtype.val
run_cmd script_check_id `subtype.rec
run_cmd script_check_id `psum
run_cmd script_check_id `psum.cases_on
run_cmd script_check_id `psum.inl
run_cmd script_check_id `psum.inr
run_cmd script_check_id `tactic
run_cmd script_check_id `tactic.try
run_cmd script_check_id `tactic.triv
run_cmd script_check_id `tactic.add_coinductive_predicate
run_cmd script_check_id `thunk
run_cmd script_check_id `to_fmt
run_cmd script_check_id `to_string
run_cmd script_check_id `trans_rel_left
run_cmd script_check_id `trans_rel_right
run_cmd script_check_id `true
run_cmd script_check_id `true.intro
run_cmd script_check_id `unification_hint
run_cmd script_check_id `unification_hint.mk
run_cmd script_check_id `unit
run_cmd script_check_id `unit.cases_on
run_cmd script_check_id `unit.star
run_cmd script_check_id `unsafe_monad_from_pure_bind
run_cmd script_check_id `user_attribute
run_cmd script_check_id `vm_monitor
run_cmd script_check_id `weak_order
run_cmd script_check_id `well_founded.fix
run_cmd script_check_id `well_founded.fix_eq
run_cmd script_check_id `well_founded_tactics
run_cmd script_check_id `well_founded_tactics.default
run_cmd script_check_id `well_founded_tactics.rel_tac
run_cmd script_check_id `well_founded_tactics.dec_tac
run_cmd script_check_id `xor
run_cmd script_check_id `zero_le_one
run_cmd script_check_id `zero_lt_one
run_cmd script_check_id `zero_mul
|
4c2d7a4489284eaf8dc5574a27d175520b6bbd70 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/category_theory/monoidal/natural_transformation.lean | c889dc10ddf1b78aa8c1ce2462cb28bd29328f33 | [
"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 | 5,016 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.monoidal.functor
import category_theory.full_subcategory
/-!
# Monoidal natural transformations
Natural transformations between (lax) monoidal functors must satisfy
an additional compatibility relation with the tensorators:
`F.μ X Y ≫ app (X ⊗ Y) = (app X ⊗ app Y) ≫ G.μ X Y`.
(Lax) monoidal functors between a fixed pair of monoidal categories
themselves form a category.
-/
open category_theory
universes v₁ v₂ v₃ u₁ u₂ u₃
open category_theory.category
open category_theory.functor
namespace category_theory
open monoidal_category
variables {C : Type u₁} [category.{v₁} C] [monoidal_category.{v₁} C]
{D : Type u₂} [category.{v₂} D] [monoidal_category.{v₂} D]
/--
A monoidal natural transformation is a natural transformation between (lax) monoidal functors
additionally satisfying:
`F.μ X Y ≫ app (X ⊗ Y) = (app X ⊗ app Y) ≫ G.μ X Y`
-/
@[ext]
structure monoidal_nat_trans (F G : lax_monoidal_functor C D)
extends nat_trans F.to_functor G.to_functor :=
(unit' : F.ε ≫ app (𝟙_ C) = G.ε . obviously)
(tensor' : ∀ X Y, F.μ _ _ ≫ app (X ⊗ Y) = (app X ⊗ app Y) ≫ G.μ _ _ . obviously)
restate_axiom monoidal_nat_trans.tensor'
attribute [simp, reassoc] monoidal_nat_trans.tensor
restate_axiom monoidal_nat_trans.unit'
attribute [simp, reassoc] monoidal_nat_trans.unit
namespace monoidal_nat_trans
/--
The identity monoidal natural transformation.
-/
@[simps]
def id (F : lax_monoidal_functor C D) : monoidal_nat_trans F F :=
{ ..(𝟙 F.to_functor) }
instance (F : lax_monoidal_functor C D) : inhabited (monoidal_nat_trans F F) := ⟨id F⟩
/--
Vertical composition of monoidal natural transformations.
-/
@[simps]
def vcomp {F G H : lax_monoidal_functor C D}
(α : monoidal_nat_trans F G) (β : monoidal_nat_trans G H) : monoidal_nat_trans F H :=
{ ..(nat_trans.vcomp α.to_nat_trans β.to_nat_trans) }
instance category_lax_monoidal_functor : category (lax_monoidal_functor C D) :=
{ hom := monoidal_nat_trans,
id := id,
comp := λ F G H α β, vcomp α β, }
@[simp] lemma comp_to_nat_trans' {F G H : lax_monoidal_functor C D} {α : F ⟶ G} {β : G ⟶ H} :
(α ≫ β).to_nat_trans =
@category_struct.comp (C ⥤ D) _ _ _ _ (α.to_nat_trans) (β.to_nat_trans) := rfl
instance category_monoidal_functor : category (monoidal_functor C D) :=
induced_category.category monoidal_functor.to_lax_monoidal_functor
@[simp] lemma comp_to_nat_trans'' {F G H : monoidal_functor C D} {α : F ⟶ G} {β : G ⟶ H} :
(α ≫ β).to_nat_trans =
@category_struct.comp (C ⥤ D) _ _ _ _ (α.to_nat_trans) (β.to_nat_trans) := rfl
variables {E : Type u₃} [category.{v₃} E] [monoidal_category.{v₃} E]
/--
Horizontal composition of monoidal natural transformations.
-/
@[simps]
def hcomp {F G : lax_monoidal_functor C D} {H K : lax_monoidal_functor D E}
(α : monoidal_nat_trans F G) (β : monoidal_nat_trans H K) :
monoidal_nat_trans (F ⊗⋙ H) (G ⊗⋙ K) :=
{ unit' :=
begin
dsimp, simp,
conv_lhs { rw [←K.to_functor.map_comp, α.unit], },
end,
tensor' := λ X Y,
begin
dsimp, simp,
conv_lhs { rw [←K.to_functor.map_comp, α.tensor, K.to_functor.map_comp], },
end,
..(nat_trans.hcomp α.to_nat_trans β.to_nat_trans) }
end monoidal_nat_trans
namespace monoidal_nat_iso
variables {F G : lax_monoidal_functor C D}
instance is_iso_of_is_iso_app (α : F ⟶ G) [∀ X : C, is_iso (α.app X)] : is_iso α :=
{ inv :=
{ app := λ X, inv (α.app X),
naturality' := λ X Y f,
begin
have h := congr_arg (λ f, inv (α.app X) ≫ (f ≫ inv (α.app Y))) (α.to_nat_trans.naturality f).symm,
simp only [is_iso.inv_hom_id_assoc, is_iso.hom_inv_id, assoc, comp_id, cancel_mono] at h,
exact h
end,
tensor' := λ X Y,
begin
dsimp,
rw [is_iso.comp_inv_eq, assoc, monoidal_nat_trans.tensor, ←inv_tensor,
is_iso.inv_hom_id_assoc],
end }, }
/--
Construct a monoidal natural isomorphism from object level isomorphisms,
and the monoidal naturality in the forward direction.
-/
def of_components
(app : ∀ X : C, F.obj X ≅ G.obj X)
(naturality : ∀ {X Y : C} (f : X ⟶ Y), F.map f ≫ (app Y).hom = (app X).hom ≫ G.map f)
(unit : F.ε ≫ (app (𝟙_ C)).hom = G.ε)
(tensor : ∀ X Y, F.μ X Y ≫ (app (X ⊗ Y)).hom = ((app X).hom ⊗ (app Y).hom) ≫ G.μ X Y) :
F ≅ G :=
as_iso { app := λ X, (app X).hom }
@[simp] lemma of_components.hom_app (app : ∀ X : C, F.obj X ≅ G.obj X) (naturality) (unit) (tensor) (X) :
(of_components app naturality unit tensor).hom.app X = (app X).hom := rfl
@[simp] lemma of_components.inv_app (app : ∀ X : C, F.obj X ≅ G.obj X) (naturality) (unit) (tensor) (X) :
(of_components app naturality unit tensor).inv.app X = (app X).inv := rfl
end monoidal_nat_iso
end category_theory
|
fe62907bf0920adc4a567d10430570f5966d3650 | 3f7026ea8bef0825ca0339a275c03b911baef64d | /src/tactic/linarith.lean | 2654191f0f7e363a1e594143f0ad6c26974a4bde | [
"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 | 33,040 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
-/
import tactic.ring data.nat.gcd data.list.basic meta.rb_map data.tree
/-!
A tactic for discharging linear arithmetic goals using Fourier-Motzkin elimination.
`linarith` is (in principle) complete for ℚ and ℝ. It is not complete for non-dense orders, i.e. ℤ.
@TODO: investigate storing comparisons in a list instead of a set, for possible efficiency gains
@TODO: delay proofs of denominator normalization and nat casting until after contradiction is found
-/
meta def nat.to_pexpr : ℕ → pexpr
| 0 := ``(0)
| 1 := ``(1)
| n := if n % 2 = 0 then ``(bit0 %%(nat.to_pexpr (n/2))) else ``(bit1 %%(nat.to_pexpr (n/2)))
open native
namespace linarith
section lemmas
lemma int.coe_nat_bit0 (n : ℕ) : (↑(bit0 n : ℕ) : ℤ) = bit0 (↑n : ℤ) := by simp [bit0]
lemma int.coe_nat_bit1 (n : ℕ) : (↑(bit1 n : ℕ) : ℤ) = bit1 (↑n : ℤ) := by simp [bit1, bit0]
lemma int.coe_nat_bit0_mul (n : ℕ) (x : ℕ) : (↑(bit0 n * x) : ℤ) = (↑(bit0 n) : ℤ) * (↑x : ℤ) := by simp
lemma int.coe_nat_bit1_mul (n : ℕ) (x : ℕ) : (↑(bit1 n * x) : ℤ) = (↑(bit1 n) : ℤ) * (↑x : ℤ) := by simp
lemma int.coe_nat_one_mul (x : ℕ) : (↑(1 * x) : ℤ) = 1 * (↑x : ℤ) := by simp
lemma int.coe_nat_zero_mul (x : ℕ) : (↑(0 * x) : ℤ) = 0 * (↑x : ℤ) := by simp
lemma int.coe_nat_mul_bit0 (n : ℕ) (x : ℕ) : (↑(x * bit0 n) : ℤ) = (↑x : ℤ) * (↑(bit0 n) : ℤ) := by simp
lemma int.coe_nat_mul_bit1 (n : ℕ) (x : ℕ) : (↑(x * bit1 n) : ℤ) = (↑x : ℤ) * (↑(bit1 n) : ℤ) := by simp
lemma int.coe_nat_mul_one (x : ℕ) : (↑(x * 1) : ℤ) = (↑x : ℤ) * 1 := by simp
lemma int.coe_nat_mul_zero (x : ℕ) : (↑(x * 0) : ℤ) = (↑x : ℤ) * 0 := by simp
lemma nat_eq_subst {n1 n2 : ℕ} {z1 z2 : ℤ} (hn : n1 = n2) (h1 : ↑n1 = z1) (h2 : ↑n2 = z2) : z1 = z2 :=
by simpa [eq.symm h1, eq.symm h2, int.coe_nat_eq_coe_nat_iff]
lemma nat_le_subst {n1 n2 : ℕ} {z1 z2 : ℤ} (hn : n1 ≤ n2) (h1 : ↑n1 = z1) (h2 : ↑n2 = z2) : z1 ≤ z2 :=
by simpa [eq.symm h1, eq.symm h2, int.coe_nat_le]
lemma nat_lt_subst {n1 n2 : ℕ} {z1 z2 : ℤ} (hn : n1 < n2) (h1 : ↑n1 = z1) (h2 : ↑n2 = z2) : z1 < z2 :=
by simpa [eq.symm h1, eq.symm h2, int.coe_nat_lt]
lemma eq_of_eq_of_eq {α} [ordered_semiring α] {a b : α} (ha : a = 0) (hb : b = 0) : a + b = 0 :=
by simp *
lemma le_of_eq_of_le {α} [ordered_semiring α] {a b : α} (ha : a = 0) (hb : b ≤ 0) : a + b ≤ 0 :=
by simp *
lemma lt_of_eq_of_lt {α} [ordered_semiring α] {a b : α} (ha : a = 0) (hb : b < 0) : a + b < 0 :=
by simp *
lemma le_of_le_of_eq {α} [ordered_semiring α] {a b : α} (ha : a ≤ 0) (hb : b = 0) : a + b ≤ 0 :=
by simp *
lemma lt_of_lt_of_eq {α} [ordered_semiring α] {a b : α} (ha : a < 0) (hb : b = 0) : a + b < 0 :=
by simp *
lemma mul_neg {α} [ordered_ring α] {a b : α} (ha : a < 0) (hb : b > 0) : b * a < 0 :=
have (-b)*a > 0, from mul_pos_of_neg_of_neg (neg_neg_of_pos hb) ha,
neg_of_neg_pos (by simpa)
lemma mul_nonpos {α} [ordered_ring α] {a b : α} (ha : a ≤ 0) (hb : b > 0) : b * a ≤ 0 :=
have (-b)*a ≥ 0, from mul_nonneg_of_nonpos_of_nonpos (le_of_lt (neg_neg_of_pos hb)) ha,
nonpos_of_neg_nonneg (by simp at this; exact this)
lemma mul_eq {α} [ordered_semiring α] {a b : α} (ha : a = 0) (hb : b > 0) : b * a = 0 :=
by simp *
lemma eq_of_not_lt_of_not_gt {α} [linear_order α] (a b : α) (h1 : ¬ a < b) (h2 : ¬ b < a) : a = b :=
le_antisymm (le_of_not_gt h2) (le_of_not_gt h1)
lemma add_subst {α} [ring α] {n e1 e2 t1 t2 : α} (h1 : n * e1 = t1) (h2 : n * e2 = t2) :
n * (e1 + e2) = t1 + t2 := by simp [left_distrib, *]
lemma sub_subst {α} [ring α] {n e1 e2 t1 t2 : α} (h1 : n * e1 = t1) (h2 : n * e2 = t2) :
n * (e1 - e2) = t1 - t2 := by simp [left_distrib, *]
lemma neg_subst {α} [ring α] {n e t : α} (h1 : n * e = t) : n * (-e) = -t := by simp *
private meta def apnn : tactic unit := `[norm_num]
lemma mul_subst {α} [comm_ring α] {n1 n2 k e1 e2 t1 t2 : α} (h1 : n1 * e1 = t1) (h2 : n2 * e2 = t2)
(h3 : n1*n2 = k . apnn) : k * (e1 * e2) = t1 * t2 :=
have h3 : n1 * n2 = k, from h3,
by rw [←h3, mul_comm n1, mul_assoc n2, ←mul_assoc n1, h1, ←mul_assoc n2, mul_comm n2, mul_assoc, h2] -- OUCH
lemma div_subst {α} [field α] {n1 n2 k e1 e2 t1 : α} (h1 : n1 * e1 = t1) (h2 : n2 / e2 = 1) (h3 : n1*n2 = k) :
k * (e1 / e2) = t1 :=
by rw [←h3, mul_assoc, mul_div_comm, h2, ←mul_assoc, h1, mul_comm, one_mul]
end lemmas
section datatypes
@[derive decidable_eq]
inductive ineq
| eq | le | lt
open ineq
def ineq.max : ineq → ineq → ineq
| eq a := a
| le a := a
| lt a := lt
def ineq.is_lt : ineq → ineq → bool
| eq le := tt
| eq lt := tt
| le lt := tt
| _ _ := ff
def ineq.to_string : ineq → string
| eq := "="
| le := "≤"
| lt := "<"
instance : has_to_string ineq := ⟨ineq.to_string⟩
/--
The main datatype for FM elimination.
Variables are represented by natural numbers, each of which has an integer coefficient.
Index 0 is reserved for constants, i.e. `coeffs.find 0` is the coefficient of 1.
The represented term is coeffs.keys.sum (λ i, coeffs.find i * Var[i]).
str determines the direction of the comparison -- is it < 0, ≤ 0, or = 0?
-/
meta structure comp :=
(str : ineq)
(coeffs : rb_map ℕ int)
meta instance : inhabited comp := ⟨⟨ineq.eq, mk_rb_map⟩⟩
meta inductive comp_source
| assump : ℕ → comp_source
| add : comp_source → comp_source → comp_source
| scale : ℕ → comp_source → comp_source
meta def comp_source.flatten : comp_source → rb_map ℕ ℕ
| (comp_source.assump n) := mk_rb_map.insert n 1
| (comp_source.add c1 c2) := (comp_source.flatten c1).add (comp_source.flatten c2)
| (comp_source.scale n c) := (comp_source.flatten c).map (λ v, v * n)
meta def comp_source.to_string : comp_source → string
| (comp_source.assump e) := to_string e
| (comp_source.add c1 c2) := comp_source.to_string c1 ++ " + " ++ comp_source.to_string c2
| (comp_source.scale n c) := to_string n ++ " * " ++ comp_source.to_string c
meta instance comp_source.has_to_format : has_to_format comp_source :=
⟨λ a, comp_source.to_string a⟩
meta structure pcomp :=
(c : comp)
(src : comp_source)
meta def map_lt (m1 m2 : rb_map ℕ int) : bool :=
list.lex (prod.lex (<) (<)) m1.to_list m2.to_list
-- make more efficient
meta def comp.lt (c1 c2 : comp) : bool :=
(c1.str.is_lt c2.str) || (c1.str = c2.str) && map_lt c1.coeffs c2.coeffs
meta instance comp.has_lt : has_lt comp := ⟨λ a b, comp.lt a b⟩
meta instance pcomp.has_lt : has_lt pcomp := ⟨λ p1 p2, p1.c < p2.c⟩
meta instance pcomp.has_lt_dec : decidable_rel ((<) : pcomp → pcomp → Prop) := by apply_instance
meta def comp.coeff_of (c : comp) (a : ℕ) : ℤ :=
c.coeffs.zfind a
meta def comp.scale (c : comp) (n : ℕ) : comp :=
{ c with coeffs := c.coeffs.map ((*) (n : ℤ)) }
meta def comp.add (c1 c2 : comp) : comp :=
⟨c1.str.max c2.str, c1.coeffs.add c2.coeffs⟩
meta def pcomp.scale (c : pcomp) (n : ℕ) : pcomp :=
⟨c.c.scale n, comp_source.scale n c.src⟩
meta def pcomp.add (c1 c2 : pcomp) : pcomp :=
⟨c1.c.add c2.c, comp_source.add c1.src c2.src⟩
meta instance pcomp.to_format : has_to_format pcomp :=
⟨λ p, to_fmt p.c.coeffs ++ to_string p.c.str ++ "0"⟩
meta instance comp.to_format : has_to_format comp :=
⟨λ p, to_fmt p.coeffs⟩
end datatypes
section fm_elim
/-- If c1 and c2 both contain variable a with opposite coefficients,
produces v1, v2, and c such that a has been cancelled in c := v1*c1 + v2*c2 -/
meta def elim_var (c1 c2 : comp) (a : ℕ) : option (ℕ × ℕ × comp) :=
let v1 := c1.coeff_of a,
v2 := c2.coeff_of a in
if v1 * v2 < 0 then
let vlcm := nat.lcm v1.nat_abs v2.nat_abs,
v1' := vlcm / v1.nat_abs,
v2' := vlcm / v2.nat_abs in
some ⟨v1', v2', comp.add (c1.scale v1') (c2.scale v2')⟩
else none
meta def pelim_var (p1 p2 : pcomp) (a : ℕ) : option pcomp :=
do (n1, n2, c) ← elim_var p1.c p2.c a,
return ⟨c, comp_source.add (p1.src.scale n1) (p2.src.scale n2)⟩
meta def comp.is_contr (c : comp) : bool := c.coeffs.empty ∧ c.str = ineq.lt
meta def pcomp.is_contr (p : pcomp) : bool := p.c.is_contr
meta def elim_with_set (a : ℕ) (p : pcomp) (comps : rb_set pcomp) : rb_set pcomp :=
if ¬ p.c.coeffs.contains a then mk_rb_set.insert p else
comps.fold mk_rb_set $ λ pc s,
match pelim_var p pc a with
| some pc := s.insert pc
| none := s
end
/--
The state for the elimination monad.
vars: the set of variables present in comps
comps: a set of comparisons
inputs: a set of pairs of exprs (t, pf), where t is a term and pf is a proof that t {<, ≤, =} 0,
indexed by ℕ.
has_false: stores a pcomp of 0 < 0 if one has been found
TODO: is it more efficient to store comps as a list, to avoid comparisons?
-/
meta structure linarith_structure :=
(vars : rb_set ℕ)
(comps : rb_set pcomp)
@[reducible] meta def linarith_monad :=
state_t linarith_structure (except_t pcomp id)
meta instance : monad linarith_monad := state_t.monad
meta instance : monad_except pcomp linarith_monad :=
state_t.monad_except pcomp
meta def get_vars : linarith_monad (rb_set ℕ) :=
linarith_structure.vars <$> get
meta def get_var_list : linarith_monad (list ℕ) :=
rb_set.to_list <$> get_vars
meta def get_comps : linarith_monad (rb_set pcomp) :=
linarith_structure.comps <$> get
meta def validate : linarith_monad unit :=
do ⟨_, comps⟩ ← get,
match comps.to_list.find (λ p : pcomp, p.is_contr) with
| none := return ()
| some c := throw c
end
meta def update (vars : rb_set ℕ) (comps : rb_set pcomp) : linarith_monad unit :=
state_t.put ⟨vars, comps⟩ >> validate
meta def monad.elim_var (a : ℕ) : linarith_monad unit :=
do vs ← get_vars,
when (vs.contains a) $
do comps ← get_comps,
let cs' := comps.fold mk_rb_set (λ p s, s.union (elim_with_set a p comps)),
update (vs.erase a) cs'
meta def elim_all_vars : linarith_monad unit :=
get_var_list >>= list.mmap' monad.elim_var
end fm_elim
section parse
open ineq tactic
meta def map_of_expr_mul_aux (c1 c2 : rb_map ℕ ℤ) : option (rb_map ℕ ℤ) :=
match c1.keys, c2.keys with
| [0], _ := some $ c2.scale (c1.zfind 0)
| _, [0] := some $ c1.scale (c2.zfind 0)
| [], _ := some mk_rb_map
| _, [] := some mk_rb_map
| _, _ := none
end
meta def list.mfind {α} (tac : α → tactic unit) : list α → tactic α
| [] := failed
| (h::t) := tac h >> return h <|> list.mfind t
meta def rb_map.find_defeq (red : transparency) {v} (m : expr_map v) (e : expr) : tactic v :=
prod.snd <$> list.mfind (λ p, is_def_eq e p.1 red) m.to_list
/--
Turns an expression into a map from ℕ to ℤ, for use in a comp object.
The expr_map ℕ argument identifies which expressions have already been assigned numbers.
Returns a new map.
-/
meta def map_of_expr (red : transparency) : expr_map ℕ → expr → tactic (expr_map ℕ × rb_map ℕ ℤ)
| m e@`(%%e1 * %%e2) :=
(do (m', comp1) ← map_of_expr m e1,
(m', comp2) ← map_of_expr m' e2,
mp ← map_of_expr_mul_aux comp1 comp2,
return (m', mp)) <|>
(do k ← rb_map.find_defeq red m e, return (m, mk_rb_map.insert k 1)) <|>
(let n := m.size + 1 in return (m.insert e n, mk_rb_map.insert n 1))
| m `(%%e1 + %%e2) :=
do (m', comp1) ← map_of_expr m e1,
(m', comp2) ← map_of_expr m' e2,
return (m', comp1.add comp2)
| m `(%%e1 - %%e2) :=
do (m', comp1) ← map_of_expr m e1,
(m', comp2) ← map_of_expr m' e2,
return (m', comp1.add (comp2.scale (-1)))
| m `(-%%e) := do (m', comp) ← map_of_expr m e, return (m', comp.scale (-1))
| m e :=
match e.to_int with
| some 0 := return ⟨m, mk_rb_map⟩
| some z := return ⟨m, mk_rb_map.insert 0 z⟩
| none :=
(do k ← rb_map.find_defeq red m e, return (m, mk_rb_map.insert k 1)) <|>
(let n := m.size + 1 in return (m.insert e n, mk_rb_map.insert n 1))
end
meta def parse_into_comp_and_expr : expr → option (ineq × expr)
| `(%%e < 0) := (ineq.lt, e)
| `(%%e ≤ 0) := (ineq.le, e)
| `(%%e = 0) := (ineq.eq, e)
| _ := none
meta def to_comp (red : transparency) (e : expr) (m : expr_map ℕ) : tactic (comp × expr_map ℕ) :=
do (iq, e) ← parse_into_comp_and_expr e,
(m', comp') ← map_of_expr red m e,
return ⟨⟨iq, comp'⟩, m'⟩
meta def to_comp_fold (red : transparency) : expr_map ℕ → list expr →
tactic (list (option comp) × expr_map ℕ)
| m [] := return ([], m)
| m (h::t) :=
(do (c, m') ← to_comp red h m,
(l, mp) ← to_comp_fold m' t,
return (c::l, mp)) <|>
(do (l, mp) ← to_comp_fold m t,
return (none::l, mp))
/--
Takes a list of proofs of props of the form t {<, ≤, =} 0, and creates a linarith_structure.
-/
meta def mk_linarith_structure (red : transparency) (l : list expr) : tactic (linarith_structure × rb_map ℕ (expr × expr)) :=
do pftps ← l.mmap infer_type,
(l', map) ← to_comp_fold red mk_rb_map pftps,
let lz := list.enum $ ((l.zip pftps).zip l').filter_map (λ ⟨a, b⟩, prod.mk a <$> b),
let prmap := rb_map.of_list $ lz.map (λ ⟨n, x⟩, (n, x.1)),
let vars : rb_set ℕ := rb_map.set_of_list $ list.range map.size.succ,
let pc : rb_set pcomp := rb_map.set_of_list $
lz.map (λ ⟨n, x⟩, ⟨x.2, comp_source.assump n⟩),
return (⟨vars, pc⟩, prmap)
meta def linarith_monad.run (red : transparency) {α} (tac : linarith_monad α) (l : list expr) : tactic ((pcomp ⊕ α) × rb_map ℕ (expr × expr)) :=
do (struct, inputs) ← mk_linarith_structure red l,
match (state_t.run (validate >> tac) struct).run with
| (except.ok (a, _)) := return (sum.inr a, inputs)
| (except.error contr) := return (sum.inl contr, inputs)
end
end parse
section prove
open ineq tactic
meta def get_rel_sides : expr → tactic (expr × expr)
| `(%%a < %%b) := return (a, b)
| `(%%a ≤ %%b) := return (a, b)
| `(%%a = %%b) := return (a, b)
| `(%%a ≥ %%b) := return (a, b)
| `(%%a > %%b) := return (a, b)
| _ := failed
meta def mul_expr (n : ℕ) (e : expr) : pexpr :=
if n = 1 then ``(%%e) else
``(%%(nat.to_pexpr n) * %%e)
meta def add_exprs_aux : pexpr → list pexpr → pexpr
| p [] := p
| p [a] := ``(%%p + %%a)
| p (h::t) := add_exprs_aux ``(%%p + %%h) t
meta def add_exprs : list pexpr → pexpr
| [] := ``(0)
| (h::t) := add_exprs_aux h t
meta def find_contr (m : rb_set pcomp) : option pcomp :=
m.keys.find (λ p, p.c.is_contr)
meta def ineq_const_mul_nm : ineq → name
| lt := ``mul_neg
| le := ``mul_nonpos
| eq := ``mul_eq
meta def ineq_const_nm : ineq → ineq → (name × ineq)
| eq eq := (``eq_of_eq_of_eq, eq)
| eq le := (``le_of_eq_of_le, le)
| eq lt := (``lt_of_eq_of_lt, lt)
| le eq := (``le_of_le_of_eq, le)
| le le := (`add_nonpos, le)
| le lt := (`add_neg_of_nonpos_of_neg, lt)
| lt eq := (``lt_of_lt_of_eq, lt)
| lt le := (`add_neg_of_neg_of_nonpos, lt)
| lt lt := (`add_neg, lt)
meta def mk_single_comp_zero_pf (c : ℕ) (h : expr) : tactic (ineq × expr) :=
do tp ← infer_type h,
some (iq, e) ← return $ parse_into_comp_and_expr tp,
if c = 0 then
do e' ← mk_app ``zero_mul [e], return (eq, e')
else if c = 1 then return (iq, h)
else
do nm ← resolve_name (ineq_const_mul_nm iq),
tp ← (prod.snd <$> (infer_type h >>= get_rel_sides)) >>= infer_type,
cpos ← to_expr ``((%%c.to_pexpr : %%tp) > 0),
(_, ex) ← solve_aux cpos `[norm_num, done],
-- e' ← mk_app (ineq_const_mul_nm iq) [h, ex], -- this takes many seconds longer in some examples! why?
e' ← to_expr ``(%%nm %%h %%ex) ff,
return (iq, e')
meta def mk_lt_zero_pf_aux (c : ineq) (pf npf : expr) (coeff : ℕ) : tactic (ineq × expr) :=
do (iq, h') ← mk_single_comp_zero_pf coeff npf,
let (nm, niq) := ineq_const_nm c iq,
n ← resolve_name nm,
e' ← to_expr ``(%%n %%pf %%h'),
return (niq, e')
/--
Takes a list of coefficients [c] and list of expressions, of equal length.
Each expression is a proof of a prop of the form t {<, ≤, =} 0.
Produces a proof that the sum of (c*t) {<, ≤, =} 0, where the comp is as strong as possible.
-/
meta def mk_lt_zero_pf : list ℕ → list expr → tactic expr
| _ [] := fail "no linear hypotheses found"
| [c] [h] := prod.snd <$> mk_single_comp_zero_pf c h
| (c::ct) (h::t) :=
do (iq, h') ← mk_single_comp_zero_pf c h,
prod.snd <$> (ct.zip t).mfoldl (λ pr ce, mk_lt_zero_pf_aux pr.1 pr.2 ce.2 ce.1) (iq, h')
| _ _ := fail "not enough args to mk_lt_zero_pf"
meta def term_of_ineq_prf (prf : expr) : tactic expr :=
do (lhs, _) ← infer_type prf >>= get_rel_sides,
return lhs
meta structure linarith_config :=
(discharger : tactic unit := `[ring])
(restrict_type : option Type := none)
(restrict_type_reflect : reflected restrict_type . apply_instance)
(exfalso : bool := tt)
(transparency : transparency := reducible)
meta def ineq_pf_tp (pf : expr) : tactic expr :=
do (_, z) ← infer_type pf >>= get_rel_sides,
infer_type z
meta def mk_neg_one_lt_zero_pf (tp : expr) : tactic expr :=
to_expr ``((neg_neg_of_pos zero_lt_one : -1 < (0 : %%tp)))
/--
Assumes e is a proof that t = 0. Creates a proof that -t = 0.
-/
meta def mk_neg_eq_zero_pf (e : expr) : tactic expr :=
to_expr ``(neg_eq_zero.mpr %%e)
meta def add_neg_eq_pfs : list expr → tactic (list expr)
| [] := return []
| (h::t) :=
do some (iq, tp) ← parse_into_comp_and_expr <$> infer_type h,
match iq with
| ineq.eq := do nep ← mk_neg_eq_zero_pf h, tl ← add_neg_eq_pfs t, return $ h::nep::tl
| _ := list.cons h <$> add_neg_eq_pfs t
end
/--
Takes a list of proofs of propositions of the form t {<, ≤, =} 0,
and tries to prove the goal `false`.
-/
meta def prove_false_by_linarith1 (cfg : linarith_config) : list expr → tactic unit
| [] := fail "no args to linarith"
| l@(h::t) :=
do l' ← add_neg_eq_pfs l,
hz ← ineq_pf_tp h >>= mk_neg_one_lt_zero_pf,
(sum.inl contr, inputs) ← elim_all_vars.run cfg.transparency (hz::l')
| fail "linarith failed to find a contradiction",
let coeffs := inputs.keys.map (λ k, (contr.src.flatten.ifind k)),
let pfs : list expr := inputs.keys.map (λ k, (inputs.ifind k).1),
let zip := (coeffs.zip pfs).filter (λ pr, pr.1 ≠ 0),
let (coeffs, pfs) := zip.unzip,
mls ← zip.mmap (λ pr, do e ← term_of_ineq_prf pr.2, return (mul_expr pr.1 e)),
sm ← to_expr $ add_exprs mls,
tgt ← to_expr ``(%%sm = 0),
(a, b) ← solve_aux tgt (cfg.discharger >> done),
pf ← mk_lt_zero_pf coeffs pfs,
pftp ← infer_type pf,
(_, nep, _) ← rewrite_core b pftp,
pf' ← mk_eq_mp nep pf,
mk_app `lt_irrefl [pf'] >>= exact
end prove
section normalize
open tactic
set_option eqn_compiler.max_steps 50000
meta def rem_neg (prf : expr) : expr → tactic expr
| `(_ ≤ _) := to_expr ``(lt_of_not_ge %%prf)
| `(_ < _) := to_expr ``(le_of_not_gt %%prf)
| `(_ > _) := to_expr ``(le_of_not_gt %%prf)
| `(_ ≥ _) := to_expr ``(lt_of_not_ge %%prf)
| e := failed
meta def rearr_comp : expr → expr → tactic expr
| prf `(%%a ≤ 0) := return prf
| prf `(%%a < 0) := return prf
| prf `(%%a = 0) := return prf
| prf `(%%a ≥ 0) := to_expr ``(neg_nonpos.mpr %%prf)
| prf `(%%a > 0) := to_expr ``(neg_neg_of_pos %%prf)
| prf `(0 ≥ %%a) := to_expr ``(show %%a ≤ 0, from %%prf)
| prf `(0 > %%a) := to_expr ``(show %%a < 0, from %%prf)
| prf `(0 = %%a) := to_expr ``(eq.symm %%prf)
| prf `(0 ≤ %%a) := to_expr ``(neg_nonpos.mpr %%prf)
| prf `(0 < %%a) := to_expr ``(neg_neg_of_pos %%prf)
| prf `(%%a ≤ %%b) := to_expr ``(sub_nonpos.mpr %%prf)
| prf `(%%a < %%b) := to_expr ``(sub_neg_of_lt %%prf)
| prf `(%%a = %%b) := to_expr ``(sub_eq_zero.mpr %%prf)
| prf `(%%a > %%b) := to_expr ``(sub_neg_of_lt %%prf)
| prf `(%%a ≥ %%b) := to_expr ``(sub_nonpos.mpr %%prf)
| prf `(¬ %%t) := do nprf ← rem_neg prf t, tp ← infer_type nprf, rearr_comp nprf tp
| prf _ := fail "couldn't rearrange comp"
meta def is_numeric : expr → option ℚ
| `(%%e1 + %%e2) := (+) <$> is_numeric e1 <*> is_numeric e2
| `(%%e1 - %%e2) := has_sub.sub <$> is_numeric e1 <*> is_numeric e2
| `(%%e1 * %%e2) := (*) <$> is_numeric e1 <*> is_numeric e2
| `(%%e1 / %%e2) := (/) <$> is_numeric e1 <*> is_numeric e2
| `(-%%e) := rat.neg <$> is_numeric e
| e := e.to_rat
meta def find_cancel_factor : expr → ℕ × tree ℕ
| `(%%e1 + %%e2) :=
let (v1, t1) := find_cancel_factor e1, (v2, t2) := find_cancel_factor e2, lcm := v1.lcm v2 in
(lcm, tree.node lcm t1 t2)
| `(%%e1 - %%e2) :=
let (v1, t1) := find_cancel_factor e1, (v2, t2) := find_cancel_factor e2, lcm := v1.lcm v2 in
(lcm, tree.node lcm t1 t2)
| `(%%e1 * %%e2) :=
match is_numeric e1, is_numeric e2 with
| none, none := (1, tree.node 1 tree.nil tree.nil)
| _, _ :=
let (v1, t1) := find_cancel_factor e1, (v2, t2) := find_cancel_factor e2, pd := v1*v2 in
(pd, tree.node pd t1 t2)
end
| `(%%e1 / %%e2) :=
match is_numeric e2 with
| some q := let (v1, t1) := find_cancel_factor e1, n := v1.lcm q.num.nat_abs in
(n, tree.node n t1 (tree.node q.num.nat_abs tree.nil tree.nil))
| none := (1, tree.node 1 tree.nil tree.nil)
end
| `(-%%e) := find_cancel_factor e
| _ := (1, tree.node 1 tree.nil tree.nil)
open tree
meta def mk_prod_prf : ℕ → tree ℕ → expr → tactic expr
| v (node _ lhs rhs) `(%%e1 + %%e2) :=
do v1 ← mk_prod_prf v lhs e1, v2 ← mk_prod_prf v rhs e2, mk_app ``add_subst [v1, v2]
| v (node _ lhs rhs) `(%%e1 - %%e2) :=
do v1 ← mk_prod_prf v lhs e1, v2 ← mk_prod_prf v rhs e2, mk_app ``sub_subst [v1, v2]
| v (node n lhs@(node ln _ _) rhs) `(%%e1 * %%e2) :=
do tp ← infer_type e1, v1 ← mk_prod_prf ln lhs e1, v2 ← mk_prod_prf (v/ln) rhs e2,
ln' ← tp.of_nat ln, vln' ← tp.of_nat (v/ln), v' ← tp.of_nat v,
ntp ← to_expr ``(%%ln' * %%vln' = %%v'),
(_, npf) ← solve_aux ntp `[norm_num, done],
mk_app ``mul_subst [v1, v2, npf]
| v (node n lhs rhs@(node rn _ _)) `(%%e1 / %%e2) :=
do tp ← infer_type e1, v1 ← mk_prod_prf (v/rn) lhs e1,
rn' ← tp.of_nat rn, vrn' ← tp.of_nat (v/rn), n' ← tp.of_nat n, v' ← tp.of_nat v,
ntp ← to_expr ``(%%rn' / %%e2 = 1),
(_, npf) ← solve_aux ntp `[norm_num, done],
ntp2 ← to_expr ``(%%vrn' * %%n' = %%v'),
(_, npf2) ← solve_aux ntp2 `[norm_num, done],
mk_app ``div_subst [v1, npf, npf2]
| v t `(-%%e) := do v' ← mk_prod_prf v t e, mk_app ``neg_subst [v']
| v _ e :=
do tp ← infer_type e,
v' ← tp.of_nat v,
e' ← to_expr ``(%%v' * %%e),
mk_app `eq.refl [e']
/--
e is a term with rational division. produces a natural number n and a proof that n*e = e',
where e' has no division.
-/
meta def kill_factors (e : expr) : tactic (ℕ × expr) :=
let (n, t) := find_cancel_factor e in
do e' ← mk_prod_prf n t e, return (n, e')
open expr
meta def expr_contains (n : name) : expr → bool
| (const nm _) := nm = n
| (lam _ _ _ bd) := expr_contains bd
| (pi _ _ _ bd) := expr_contains bd
| (app e1 e2) := expr_contains e1 || expr_contains e2
| _ := ff
lemma sub_into_lt {α} [ordered_semiring α] {a b : α} (he : a = b) (hl : a ≤ 0) : b ≤ 0 :=
by rwa he at hl
meta def norm_hyp_aux (h' lhs : expr) : tactic expr :=
do (v, lhs') ← kill_factors lhs,
if v = 1 then return h' else do
(ih, h'') ← mk_single_comp_zero_pf v h',
(_, nep, _) ← infer_type h'' >>= rewrite_core lhs',
mk_eq_mp nep h''
meta def norm_hyp (h : expr) : tactic expr :=
do htp ← infer_type h,
h' ← rearr_comp h htp,
some (c, lhs) ← parse_into_comp_and_expr <$> infer_type h',
if expr_contains `has_div.div lhs then
norm_hyp_aux h' lhs
else return h'
meta def get_contr_lemma_name : expr → option name
| `(%%a < %%b) := return `lt_of_not_ge
| `(%%a ≤ %%b) := return `le_of_not_gt
| `(%%a = %%b) := return ``eq_of_not_lt_of_not_gt
| `(%%a ≠ %%b) := return `not.intro
| `(%%a ≥ %%b) := return `le_of_not_gt
| `(%%a > %%b) := return `lt_of_not_ge
| `(¬ %%a < %%b) := return `not.intro
| `(¬ %%a ≤ %%b) := return `not.intro
| `(¬ %%a = %%b) := return `not.intro
| `(¬ %%a ≥ %%b) := return `not.intro
| `(¬ %%a > %%b) := return `not.intro
| _ := none
-- assumes the input t is of type ℕ. Produces t' of type ℤ such that ↑t = t' and a proof of equality
meta def cast_expr (e : expr) : tactic (expr × expr) :=
do s ← [`int.coe_nat_add, `int.coe_nat_zero, `int.coe_nat_one,
``int.coe_nat_bit0_mul, ``int.coe_nat_bit1_mul, ``int.coe_nat_zero_mul, ``int.coe_nat_one_mul,
``int.coe_nat_mul_bit0, ``int.coe_nat_mul_bit1, ``int.coe_nat_mul_zero, ``int.coe_nat_mul_one,
``int.coe_nat_bit0, ``int.coe_nat_bit1].mfoldl simp_lemmas.add_simp simp_lemmas.mk,
ce ← to_expr ``(↑%%e : ℤ),
simplify s [] ce {fail_if_unchanged := ff}
meta def is_nat_int_coe : expr → option expr
| `((↑(%%n : ℕ) : ℤ)) := some n
| _ := none
meta def mk_coe_nat_nonneg_prf (e : expr) : tactic expr :=
mk_app `int.coe_nat_nonneg [e]
meta def get_nat_comps : expr → list expr
| `(%%a + %%b) := (get_nat_comps a).append (get_nat_comps b)
| `(%%a * %%b) := (get_nat_comps a).append (get_nat_comps b)
| e := match is_nat_int_coe e with
| some e' := [e']
| none := []
end
meta def mk_coe_nat_nonneg_prfs (e : expr) : tactic (list expr) :=
(get_nat_comps e).mmap mk_coe_nat_nonneg_prf
meta def mk_cast_eq_and_nonneg_prfs (pf a b : expr) (ln : name) : tactic (list expr) :=
do (a', prfa) ← cast_expr a,
(b', prfb) ← cast_expr b,
la ← mk_coe_nat_nonneg_prfs a',
lb ← mk_coe_nat_nonneg_prfs b',
pf' ← mk_app ln [pf, prfa, prfb],
return $ pf'::(la.append lb)
meta def mk_int_pfs_of_nat_pf (pf : expr) : tactic (list expr) :=
do tp ← infer_type pf,
match tp with
| `(%%a = %%b) := mk_cast_eq_and_nonneg_prfs pf a b ``nat_eq_subst
| `(%%a ≤ %%b) := mk_cast_eq_and_nonneg_prfs pf a b ``nat_le_subst
| `(%%a < %%b) := mk_cast_eq_and_nonneg_prfs pf a b ``nat_lt_subst
| `(%%a ≥ %%b) := mk_cast_eq_and_nonneg_prfs pf b a ``nat_le_subst
| `(%%a > %%b) := mk_cast_eq_and_nonneg_prfs pf b a ``nat_lt_subst
| `(¬ %%a ≤ %%b) := do pf' ← mk_app ``lt_of_not_ge [pf], mk_cast_eq_and_nonneg_prfs pf' b a ``nat_lt_subst
| `(¬ %%a < %%b) := do pf' ← mk_app ``le_of_not_gt [pf], mk_cast_eq_and_nonneg_prfs pf' b a ``nat_le_subst
| `(¬ %%a ≥ %%b) := do pf' ← mk_app ``lt_of_not_ge [pf], mk_cast_eq_and_nonneg_prfs pf' a b ``nat_lt_subst
| `(¬ %%a > %%b) := do pf' ← mk_app ``le_of_not_gt [pf], mk_cast_eq_and_nonneg_prfs pf' a b ``nat_le_subst
| _ := fail "mk_int_pfs_of_nat_pf failed: proof is not an inequality"
end
meta def mk_non_strict_int_pf_of_strict_int_pf (pf : expr) : tactic expr :=
do tp ← infer_type pf,
match tp with
| `(%%a < %%b) := to_expr ``(@cast (%%a < %%b) (%%a + 1 ≤ %%b) (by refl) %%pf)
| `(%%a > %%b) := to_expr ``(@cast (%%a > %%b) (%%a ≥ %%b + 1) (by refl) %%pf)
| `(¬ %%a ≤ %%b) := to_expr ``(@cast (%%a > %%b) (%%a ≥ %%b + 1) (by refl) (lt_of_not_ge %%pf))
| `(¬ %%a ≥ %%b) := to_expr ``(@cast (%%a < %%b) (%%a + 1 ≤ %%b) (by refl) (lt_of_not_ge %%pf))
| _ := fail "mk_non_strict_int_pf_of_strict_int_pf failed: proof is not an inequality"
end
meta def guard_is_nat_prop : expr → tactic unit
| `(%%a = _) := infer_type a >>= unify `(ℕ)
| `(%%a ≤ _) := infer_type a >>= unify `(ℕ)
| `(%%a < _) := infer_type a >>= unify `(ℕ)
| `(%%a ≥ _) := infer_type a >>= unify `(ℕ)
| `(%%a > _) := infer_type a >>= unify `(ℕ)
| `(¬ %%p) := guard_is_nat_prop p
| _ := failed
meta def guard_is_strict_int_prop : expr → tactic unit
| `(%%a < _) := infer_type a >>= unify `(ℤ)
| `(%%a > _) := infer_type a >>= unify `(ℤ)
| `(¬ %%a ≤ _) := infer_type a >>= unify `(ℤ)
| `(¬ %%a ≥ _) := infer_type a >>= unify `(ℤ)
| _ := failed
meta def replace_nat_pfs : list expr → tactic (list expr)
| [] := return []
| (h::t) :=
(do infer_type h >>= guard_is_nat_prop,
ls ← mk_int_pfs_of_nat_pf h,
list.append ls <$> replace_nat_pfs t) <|> list.cons h <$> replace_nat_pfs t
meta def replace_strict_int_pfs : list expr → tactic (list expr)
| [] := return []
| (h::t) :=
(do infer_type h >>= guard_is_strict_int_prop,
l ← mk_non_strict_int_pf_of_strict_int_pf h,
list.cons l <$> replace_strict_int_pfs t) <|> list.cons h <$> replace_strict_int_pfs t
meta def partition_by_type_aux : rb_lmap expr expr → list expr → tactic (rb_lmap expr expr)
| m [] := return m
| m (h::t) := do tp ← ineq_pf_tp h, partition_by_type_aux (m.insert tp h) t
meta def partition_by_type (l : list expr) : tactic (rb_lmap expr expr) :=
partition_by_type_aux mk_rb_map l
private meta def try_linarith_on_lists (cfg : linarith_config) (ls : list (list expr)) : tactic unit :=
(first $ ls.map $ prove_false_by_linarith1 cfg) <|> fail "linarith failed"
/--
Takes a list of proofs of propositions.
Filters out the proofs of linear (in)equalities,
and tries to use them to prove `false`.
If pref_type is given, starts by working over this type
-/
meta def prove_false_by_linarith (cfg : linarith_config) (pref_type : option expr) (l : list expr) : tactic unit :=
do l' ← replace_nat_pfs l,
l'' ← replace_strict_int_pfs l',
ls ← list.reduce_option <$> l''.mmap (λ h, (do s ← norm_hyp h, return (some s)) <|> return none)
>>= partition_by_type,
pref_type ← (unify pref_type.iget `(ℕ) >> return (some `(ℤ) : option expr)) <|> return pref_type,
match cfg.restrict_type, ls.values, pref_type with
| some rtp, _, _ :=
do m ← mk_mvar, unify `(some %%m : option Type) cfg.restrict_type_reflect, m ← instantiate_mvars m,
prove_false_by_linarith1 cfg (ls.ifind m)
| none, [ls'], _ := prove_false_by_linarith1 cfg ls'
| none, ls', none := try_linarith_on_lists cfg ls'
| none, _, (some t) := prove_false_by_linarith1 cfg (ls.ifind t) <|> try_linarith_on_lists cfg (ls.erase t).values
end
end normalize
end linarith
section
open tactic linarith
open lean lean.parser interactive tactic interactive.types
local postfix `?`:9001 := optional
local postfix *:9001 := many
meta def linarith.elab_arg_list : option (list pexpr) → tactic (list expr)
| none := return []
| (some l) := l.mmap i_to_expr
meta def linarith.preferred_type_of_goal : option expr → tactic (option expr)
| none := return none
| (some e) := some <$> ineq_pf_tp e
/--
linarith.interactive_aux cfg o_goal restrict_hyps args:
* cfg is a linarith_config object
* o_goal : option expr is the local constant corresponding to the former goal, if there was one
* restrict_hyps : bool is tt if `linarith only [...]` was used
* args : option (list pexpr) is the optional list of arguments in `linarith [...]`
-/
meta def linarith.interactive_aux (cfg : linarith_config) :
option expr → bool → option (list pexpr) → tactic unit
| none tt none := fail "linarith only called with no arguments"
| none tt (some l) := l.mmap i_to_expr >>= prove_false_by_linarith cfg none
| (some e) tt l :=
do tp ← ineq_pf_tp e,
list.cons e <$> linarith.elab_arg_list l >>= prove_false_by_linarith cfg (some tp)
| oe ff l :=
do otp ← linarith.preferred_type_of_goal oe,
list.append <$> local_context <*>
(list.filter (λ a, bnot $ expr.is_local_constant a) <$> linarith.elab_arg_list l) >>=
prove_false_by_linarith cfg otp
/--
Tries to prove a goal of `false` by linear arithmetic on hypotheses.
If the goal is a linear (in)equality, tries to prove it by contradiction.
If the goal is not `false` or an inequality, applies `exfalso` and tries linarith on the
hypotheses.
`linarith` will use all relevant hypotheses in the local context.
`linarith [t1, t2, t3]` will add proof terms t1, t2, t3 to the local context.
`linarith only [h1, h2, h3, t1, t2, t3]` will use only the goal (if relevant), local hypotheses
h1, h2, h3, and proofs t1, t2, t3. It will ignore the rest of the local context.
`linarith!` will use a stronger reducibility setting to identify atoms.
Config options:
`linarith {exfalso := ff}` will fail on a goal that is neither an inequality nor `false`
`linarith {restrict_type := T}` will run only on hypotheses that are inequalities over `T`
`linarith {discharger := tac}` will use `tac` instead of `ring` for normalization.
Options: `ring2`, `ring SOP`, `simp`
-/
meta def tactic.interactive.linarith (red : parse ((tk "!")?))
(restr : parse ((tk "only")?)) (hyps : parse pexpr_list?)
(cfg : linarith_config := {}) : tactic unit :=
let cfg :=
if red.is_some then {cfg with transparency := semireducible, discharger := `[ring!]}
else cfg in
do t ← target,
match get_contr_lemma_name t with
| some nm := seq (applyc nm) $
do t ← intro1, linarith.interactive_aux cfg (some t) restr.is_some hyps
| none := if cfg.exfalso then exfalso >> linarith.interactive_aux cfg none restr.is_some hyps
else fail "linarith failed: target type is not an inequality."
end
end
|
3fba86443443854dc0ba3a67fc85a428f48541f4 | 8e31b9e0d8cec76b5aa1e60a240bbd557d01047c | /scratch/tobig_polyhedral.lean | 0896fea8031be7f59c3e3e0ce292b6516a1be00c | [] | no_license | ChrisHughes24/LP | 7bdd62cb648461c67246457f3ddcb9518226dd49 | e3ed64c2d1f642696104584e74ae7226d8e916de | refs/heads/master | 1,685,642,642,858 | 1,578,070,602,000 | 1,578,070,602,000 | 195,268,102 | 4 | 3 | null | 1,569,229,518,000 | 1,562,255,287,000 | Lean | UTF-8 | Lean | false | false | 2,455 | lean | import data.matrix
import data.rat
import data.set data.set.enumerate data.set.finite data.finset
variables {α : Type} {n m l s t : Type} [fintype n] [fintype m] [fintype s] [fintype t]
variables {n1 n2 : nat}
section matrix
def le [partial_order α] (M N : matrix n m α) :=
∀i:n, ∀j:m, M i j ≤ N i j
instance [partial_order α] : has_le (matrix n m α) :=
{ le := le }
protected lemma matrix.le_refl [partial_order α] (A: matrix n m α) :
A ≤ A :=
by intros i j; refl
protected lemma matrix.le_trans [partial_order α] (a b c: matrix n m α)
(h1 : a ≤ b) (h2 : b ≤ c) : a ≤ c :=
by intros i j; transitivity; solve_by_elim
protected lemma matrix.le_antisymm [partial_order α] (a b: matrix n m α)
(h1 : a ≤ b) (h2 : b ≤ a) : a = b :=
by ext i j; exact le_antisymm (h1 i j) (h2 i j)
instance [partial_order α] : partial_order (matrix n m α) :=
{
le := le,
le_refl := matrix.le_refl,
le_trans := matrix.le_trans,
le_antisymm := matrix.le_antisymm
}
end matrix
local infixl ` *ₘ ` : 70 := matrix.mul
def polyhedron [ordered_ring α] (A : matrix m n α) (b : matrix m unit α) :
set (matrix n unit α) :=
{ x : matrix n unit α | A *ₘ x ≥ b }
def vec (n: Type) (α : Type) [fintype n] :=
n → α
instance [ordered_ring α] [decidable_rel ((≤) : α → α → Prop)]
(A : matrix m n α) (b : matrix m unit α) (x : matrix n unit α) :
decidable (x ∈ polyhedron A b) :=
show decidable (∀i:m, ∀j:unit, b i j ≤ (A *ₘ x) i j),
by apply_instance
variables [ordered_ring α] (A B : matrix m n α) (b : matrix m unit α)
protected def matrix.row (A : matrix m n α) (row : m) : matrix unit n α :=
λ x: unit, λ y: n, (A row) y
lemma polyhedron_rowinE [ordered_ring α]
(x: matrix n unit α) (A: matrix m n α) (b: matrix m unit α):
x ∈ (polyhedron A b) = ∀ i:m, (matrix.row
A i *ₘ x) () () ≥ b i () :=
propext $ iff.intro
(assume h: x ∈ (polyhedron A b),
assume d: m,
show (matrix.row A d *ₘ x) () () ≥ b d (),
begin
rw polyhedron at h,
rw set.mem_set_of_eq at h,
exact h d ()
end
)
(assume h: ∀ i:m, (matrix.row A i *ₘ x) () () ≥ b i (),
show x ∈ (polyhedron A b),
begin
assume d : m,
assume j : unit,
rw <-(≥),
cases j,
apply h,
end
)
def active_ineq [ordered_ring α] (x: matrix n unit α)
(A: matrix m n α) (b: matrix m unit α) : set m :=
{ i | ((A *ₘ x) i () == b i ())} |
b4a74dce13c7768a5765e86b21437f3527bb9133 | 12b755d17b2fc1d2a08d39a11658674c4dc6c2c3 | /src/week03.lean | f76c3bb6f7a3721b1e09a06b4b0d74b488dfb4f2 | [] | no_license | UVM-M52/lean-hdthomas | a0066caf3bddd8fc8075016257a63a4d80a8fb33 | 1d399a5bb22544831ff466b2fe4cec38832b8ba6 | refs/heads/master | 1,609,092,927,189 | 1,580,748,817,000 | 1,580,748,817,000 | 238,001,939 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,357 | lean | -- Math 52: Week 3
definition is_even (n : ℤ) := ∃ (k : ℤ), n = 2 * k
definition is_odd (n : ℤ) := ∃ (k : ℤ), n = 2 * k + 1
-- Lakins 1.2.4: For every integer n, 4*n+7 is odd.
theorem L124 : ∀ (n : ℤ), is_odd (4*n+7) :=
begin
intro n,
unfold is_odd,
existsi (2*n+3:ℤ),
symmetry,
calc 2*(2*n+3) + 1
= (2*(2*n) + 2*3) + 1 : by rw mul_add ...
= 2*(2*n) + (2*3 + 1) : by rw add_assoc ...
= (2*2)*n + (2*3 + 1) : by rw mul_assoc ...
= 4*n + 7 : by refl,
end
-- Proof: Let n be a given integer. By definition of odd,
-- we want to show that there is an integer k such that
-- 4*n+7 = 2*k+1. Take k = 2*n+3, then:
-- 2*k+1 = 2*(2*n+3)+1 = 4*n+7,
-- as required. □
-- Lakins 1.2.1(a) : For every integer n, if n is even then n² is even.
theorem L121a : ∀ (n : ℤ), is_even n → is_even (n * n) :=
begin
sorry
end
-- Lakins 1.2.3(a): For all integers m and n, if m is even then m*n is even.
theorem L123a : ∀ (m n : ℤ), is_even m → is_even (m * n) :=
begin
sorry
end
-- Lakins 1.2.2(a) : For all integers m and n, if m and n are even then m+n is even.
theorem L122a : ∀ (m n : ℤ), is_even m ∧ is_even n → is_even (m + n) :=
begin
sorry
end
-- Lakins 1.2.2(b) : For all integers m and n, if m and n are odd then m+n is even.
theorem L122b : ∀ (m n : ℤ), is_odd m ∧ is_odd n → is_even (m + n) :=
begin
sorry
end
|
3d91a0f24b9c2e5753067631cd42d72035dc29d7 | 57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d | /stage0/src/Lean/Elab/PreDefinition/Main.lean | ae3b3b57744708e251ea3709b404f30557ca3931 | [
"Apache-2.0"
] | permissive | collares/lean4 | 861a9269c4592bce49b71059e232ff0bfe4594cc | 52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee | refs/heads/master | 1,691,419,031,324 | 1,618,678,138,000 | 1,618,678,138,000 | 358,989,750 | 0 | 0 | Apache-2.0 | 1,618,696,333,000 | 1,618,696,333,000 | null | UTF-8 | Lean | false | false | 3,248 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Elab.PreDefinition.Basic
import Lean.Elab.PreDefinition.Structural
import Lean.Elab.PreDefinition.WF
namespace Lean.Elab
open Meta
open Term
private def addAndCompilePartial (preDefs : Array PreDefinition) : TermElabM Unit := do
for preDef in preDefs do
trace[Elab.definition] "processing {preDef.declName}"
forallTelescope preDef.type fun xs type => do
let inh ← liftM $ mkInhabitantFor preDef.declName xs type
trace[Elab.definition] "inhabitant for {preDef.declName}"
addNonRec { preDef with
kind := DefKind.«opaque»,
value := inh
}
addAndCompilePartialRec preDefs
private def isNonRecursive (preDef : PreDefinition) : Bool :=
Option.isNone $ preDef.value.find? fun
| Expr.const declName _ _ => preDef.declName == declName
| _ => false
private def partitionPreDefs (preDefs : Array PreDefinition) : Array (Array PreDefinition) :=
let getPreDef := fun declName => (preDefs.find? fun preDef => preDef.declName == declName).get!
let vertices := preDefs.toList.map (·.declName)
let successorsOf := fun declName => (getPreDef declName).value.foldConsts [] fun declName successors =>
if preDefs.any fun preDef => preDef.declName == declName then
declName :: successors
else
successors
let sccs := SCC.scc vertices successorsOf
sccs.toArray.map fun scc => scc.toArray.map getPreDef
private def collectMVarsAtPreDef (preDef : PreDefinition) : StateRefT CollectMVars.State MetaM Unit := do
collectMVars preDef.value
collectMVars preDef.type
private def getMVarsAtPreDef (preDef : PreDefinition) : MetaM (Array MVarId) := do
let (_, s) ← (collectMVarsAtPreDef preDef).run {}
pure s.result
private def ensureNoUnassignedMVarsAtPreDef (preDef : PreDefinition) : TermElabM Unit := do
let pendingMVarIds ← getMVarsAtPreDef preDef
if (← logUnassignedUsingErrorInfos pendingMVarIds) then
throwAbortCommand
def addPreDefinitions (preDefs : Array PreDefinition) : TermElabM Unit := do
for preDef in preDefs do
trace[Elab.definition.body] "{preDef.declName} : {preDef.type} :=\n{preDef.value}"
for preDef in preDefs do
ensureNoUnassignedMVarsAtPreDef preDef
for preDefs in partitionPreDefs preDefs do
trace[Elab.definition.scc] "{preDefs.map (·.declName)}"
if preDefs.size == 1 && isNonRecursive preDefs[0] then
let preDef := preDefs[0]
if preDef.modifiers.isNoncomputable then
addNonRec preDef
else
addAndCompileNonRec preDef
else if preDefs.any (·.modifiers.isUnsafe) then
addAndCompileUnsafe preDefs
else if preDefs.any (·.modifiers.isPartial) then
addAndCompilePartial preDefs
else withRef (preDefs[0].ref) do
mapError
(orelseMergeErrors
(structuralRecursion preDefs)
(WFRecursion preDefs))
(fun msg =>
let preDefMsgs := preDefs.toList.map (MessageData.ofExpr $ mkConst ·.declName)
m!"fail to show termination for{indentD (MessageData.joinSep preDefMsgs Format.line)}\nwith errors\n{msg}")
end Lean.Elab
|
21c0eaec27785bdb6776b739b16e0e7f412ed947 | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/order/complete_boolean_algebra.lean | 4d93048d410a72747b22899358de6a24e5e12295 | [
"Apache-2.0"
] | permissive | agjftucker/mathlib | d634cd0d5256b6325e3c55bb7fb2403548371707 | 87fe50de17b00af533f72a102d0adefe4a2285e8 | refs/heads/master | 1,625,378,131,941 | 1,599,166,526,000 | 1,599,166,526,000 | 160,748,509 | 0 | 0 | Apache-2.0 | 1,544,141,789,000 | 1,544,141,789,000 | null | UTF-8 | Lean | false | false | 4,669 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
Theory of complete Boolean algebras.
-/
import order.complete_lattice
set_option old_structure_cmd true
universes u v w
variables {α : Type u} {β : Type v} {ι : Sort w}
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A complete distributive lattice is a bit stronger than the name might
suggest; perhaps completely distributive lattice is more descriptive,
as this class includes a requirement that the lattice join
distribute over *arbitrary* infima, and similarly for the dual. -/
class complete_distrib_lattice α extends complete_lattice α :=
(infi_sup_le_sup_Inf : ∀a s, (⨅ b ∈ s, a ⊔ b) ≤ a ⊔ Inf s)
(inf_Sup_le_supr_inf : ∀a s, a ⊓ Sup s ≤ (⨆ b ∈ s, a ⊓ b))
end prio
section complete_distrib_lattice
variables [complete_distrib_lattice α] {a b : α} {s t : set α}
theorem sup_Inf_eq : a ⊔ Inf s = (⨅ b ∈ s, a ⊔ b) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume h, sup_le_sup_left (Inf_le h) _)
(complete_distrib_lattice.infi_sup_le_sup_Inf _ _)
theorem Inf_sup_eq : Inf s ⊔ b = (⨅ a ∈ s, a ⊔ b) :=
by simpa [sup_comm] using @sup_Inf_eq α _ b s
theorem inf_Sup_eq : a ⊓ Sup s = (⨆ b ∈ s, a ⊓ b) :=
le_antisymm
(complete_distrib_lattice.inf_Sup_le_supr_inf _ _)
(supr_le $ assume i, supr_le $ assume h, inf_le_inf_left _ (le_Sup h))
theorem Sup_inf_eq : Sup s ⊓ b = (⨆ a ∈ s, a ⊓ b) :=
by simpa [inf_comm] using @inf_Sup_eq α _ b s
theorem Inf_sup_Inf : Inf s ⊔ Inf t = (⨅p ∈ set.prod s t, (p : α × α).1 ⊔ p.2) :=
begin
apply le_antisymm,
{ finish },
{ have : ∀ a ∈ s, (⨅p ∈ set.prod s t, (p : α × α).1 ⊔ p.2) ≤ a ⊔ Inf t,
{ assume a ha,
have : (⨅p ∈ set.prod s t, ((p : α × α).1 : α) ⊔ p.2) ≤
(⨅p ∈ prod.mk a '' t, (p : α × α).1 ⊔ p.2),
{ apply infi_le_infi_of_subset,
rintros ⟨x, y⟩,
simp only [and_imp, set.mem_image, prod.mk.inj_iff, set.prod_mk_mem_set_prod_eq,
exists_imp_distrib],
assume x' x't ax x'y,
rw [← x'y, ← ax],
simp [ha, x't] },
rw [infi_image] at this,
simp only at this,
rwa ← sup_Inf_eq at this },
calc (⨅p ∈ set.prod s t, (p : α × α).1 ⊔ p.2) ≤ (⨅a∈s, a ⊔ Inf t) : by simp; exact this
... = Inf s ⊔ Inf t : Inf_sup_eq.symm }
end
theorem Sup_inf_Sup : Sup s ⊓ Sup t = (⨆p ∈ set.prod s t, (p : α × α).1 ⊓ p.2) :=
begin
apply le_antisymm,
{ have : ∀ a ∈ s, a ⊓ Sup t ≤ (⨆p ∈ set.prod s t, (p : α × α).1 ⊓ p.2),
{ assume a ha,
have : (⨆p ∈ prod.mk a '' t, (p : α × α).1 ⊓ p.2)
≤ (⨆p ∈ set.prod s t, ((p : α × α).1 : α) ⊓ p.2),
{ apply supr_le_supr_of_subset,
rintros ⟨x, y⟩,
simp only [and_imp, set.mem_image, prod.mk.inj_iff, set.prod_mk_mem_set_prod_eq,
exists_imp_distrib],
assume x' x't ax x'y,
rw [← x'y, ← ax],
simp [ha, x't] },
rw [supr_image] at this,
simp only at this,
rwa ← inf_Sup_eq at this },
calc Sup s ⊓ Sup t = (⨆a∈s, a ⊓ Sup t) : Sup_inf_eq
... ≤ (⨆p ∈ set.prod s t, (p : α × α).1 ⊓ p.2) : by simp; exact this },
{ finish }
end
end complete_distrib_lattice
@[priority 100] -- see Note [lower instance priority]
instance complete_distrib_lattice.bounded_distrib_lattice [d : complete_distrib_lattice α] :
bounded_distrib_lattice α :=
{ le_sup_inf := λ x y z, by rw [← Inf_pair, ← Inf_pair, sup_Inf_eq, ← Inf_image, set.image_pair],
..d }
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A complete boolean algebra is a completely distributive boolean algebra. -/
class complete_boolean_algebra α extends boolean_algebra α, complete_distrib_lattice α
end prio
section complete_boolean_algebra
variables [complete_boolean_algebra α] {a b : α} {s : set α} {f : ι → α}
theorem compl_infi : (infi f)ᶜ = (⨆i, (f i)ᶜ) :=
le_antisymm
(compl_le_of_compl_le $ le_infi $ assume i, compl_le_of_compl_le $ le_supr (compl ∘ f) i)
(supr_le $ assume i, compl_le_compl $ infi_le _ _)
theorem compl_supr : (supr f)ᶜ = (⨅i, (f i)ᶜ) :=
compl_injective (by simp [compl_infi])
theorem compl_Inf : (Inf s)ᶜ = (⨆i∈s, iᶜ) :=
by simp only [Inf_eq_infi, compl_infi]
theorem compl_Sup : (Sup s)ᶜ = (⨅i∈s, iᶜ) :=
by simp only [Sup_eq_supr, compl_supr]
end complete_boolean_algebra
|
36de08a393196c33f5ee40032c26e4148b0fab90 | 271e26e338b0c14544a889c31c30b39c989f2e0f | /src/Init/Lean/Elab/BuiltinNotation.lean | 0bf81a2c30a4a646f34cd43d4f55a0ce98f4f205 | [
"Apache-2.0"
] | permissive | dgorokho/lean4 | 805f99b0b60c545b64ac34ab8237a8504f89d7d4 | e949a052bad59b1c7b54a82d24d516a656487d8a | refs/heads/master | 1,607,061,363,851 | 1,578,006,086,000 | 1,578,006,086,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,978 | 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.Elab.Term
namespace Lean
namespace Elab
namespace Term
@[builtinTermElab dollar] def elabDollar : TermElab :=
adaptExpander $ fun stx => match_syntax stx with
| `($f $ $a) => `($f $a)
| _ => throwUnexpectedSyntax stx "application"
@[builtinTermElab dollarProj] def elabDollarProj : TermElab :=
adaptExpander $ fun stx => match_syntax stx with
| `($term $.$field) => `($(term).$field)
| _ => throwUnexpectedSyntax stx "$."
@[builtinTermElab «if»] def elabIf : TermElab :=
adaptExpander $ fun stx => match_syntax stx with
| `(if $h : $cond then $t else $e) => let h := mkTermIdFromIdent h; `(dite $cond (fun $h => $t) (fun $h => $e))
| `(if $cond then $t else $e) => `(ite $cond $t $e)
| _ => throwUnexpectedSyntax stx "if-then-else"
@[builtinTermElab subtype] def elabSubtype : TermElab :=
adaptExpander $ fun stx => match_syntax stx with
| `({ $x : $type // $p }) => let x := mkTermIdFromIdent x; `(Subtype (fun ($x : $type) => $p))
| `({ $x // $p }) => let x := mkTermIdFromIdent x; `(Subtype (fun ($x : _) => $p))
| _ => throwUnexpectedSyntax stx "subtype"
@[builtinTermElab anonymousCtor] def elabAnoymousCtor : TermElab :=
fun stx expectedType? => do
let ref := stx.val;
tryPostponeIfNoneOrMVar expectedType?;
match expectedType? with
| none => throwError ref "invalid constructor ⟨...⟩, expected type must be known"
| some expectedType => do
expectedType ← instantiateMVars ref expectedType;
let expectedType := expectedType.consumeMData;
match expectedType.getAppFn with
| Expr.const constName _ _ => do
env ← getEnv;
match env.find? constName with
| some (ConstantInfo.inductInfo val) =>
if val.ctors.length != 1 then
throwError ref ("invalid constructor ⟨...⟩, '" ++ constName ++ "' must have only one constructor")
else
let ctor := mkTermId ref val.ctors.head!;
let args := (stx.getArg 1).getArgs.getEvenElems; do
withMacroExpansion ref $ elabTerm (mkAppStx ctor args) expectedType?
| _ => throwError ref ("invalid constructor ⟨...⟩, '" ++ constName ++ "' is not an inductive type")
| _ => throwError ref ("invalid constructor ⟨...⟩, expected type is not an inductive type " ++ indentExpr expectedType)
@[builtinTermElab «show»] def elabShow : TermElab :=
adaptExpander $ fun stx => match_syntax stx with
| `(show $type from $val) => let thisId := mkTermId stx `this; `((fun ($thisId : $type) => $thisId) $val)
| _ => throwUnexpectedSyntax stx "show-from"
@[builtinTermElab «have»] def elabHave : TermElab :=
adaptExpander $ fun stx => match_syntax stx with
| `(have $type from $val; $body) => let thisId := mkTermId stx `this; `((fun ($thisId : $type) => $body) $val)
| `(have $type := $val; $body) => let thisId := mkTermId stx `this; `((fun ($thisId : $type) => $body) $val)
| `(have $x : $type from $val; $body) => let x := mkTermIdFromIdent x; `((fun ($x : $type) => $body) $val)
| `(have $x : $type := $val; $body) => let x := mkTermIdFromIdent x; `((fun ($x : $type) => $body) $val)
| _ => throwUnexpectedSyntax stx "have"
@[termElab «where»] def elabWhere : TermElab :=
adaptExpander $ fun stx => match_syntax stx with
| `($body where $decls*) => do
let decls := decls.getEvenElems;
decls.foldrM
(fun decl body => `(let $decl; $body))
body
| _ => throwUnexpectedSyntax stx "where"
def elabInfix (f : Syntax) : TermElab :=
fun stx expectedType? => do
-- term `op` term
let a := stx.getArg 0;
let b := stx.getArg 2;
elabTerm (mkAppStx f #[a, b]) expectedType?
def elabInfixOp (op : Name) : TermElab :=
fun stx expectedType? => elabInfix (mkTermId (stx.getArg 1) op) stx expectedType?
@[builtinTermElab prod] def elabProd : TermElab := elabInfixOp `Prod
@[builtinTermElab fcomp] def ElabFComp : TermElab := elabInfixOp `Function.comp
@[builtinTermElab add] def elabAdd : TermElab := elabInfixOp `HasAdd.add
@[builtinTermElab sub] def elabSub : TermElab := elabInfixOp `HasSub.sub
@[builtinTermElab mul] def elabMul : TermElab := elabInfixOp `HasMul.mul
@[builtinTermElab div] def elabDiv : TermElab := elabInfixOp `HasDiv.div
@[builtinTermElab mod] def elabMod : TermElab := elabInfixOp `HasMod.mod
@[builtinTermElab modN] def elabModN : TermElab := elabInfixOp `HasModN.modn
@[builtinTermElab pow] def elabPow : TermElab := elabInfixOp `HasPow.pow
@[builtinTermElab le] def elabLE : TermElab := elabInfixOp `HasLessEq.LessEq
@[builtinTermElab ge] def elabGE : TermElab := elabInfixOp `GreaterEq
@[builtinTermElab lt] def elabLT : TermElab := elabInfixOp `HasLess.Less
@[builtinTermElab gt] def elabGT : TermElab := elabInfixOp `Greater
@[builtinTermElab eq] def elabEq : TermElab := elabInfixOp `Eq
@[builtinTermElab ne] def elabNe : TermElab := elabInfixOp `Ne
@[builtinTermElab beq] def elabBEq : TermElab := elabInfixOp `HasBeq.beq
@[builtinTermElab bne] def elabBNe : TermElab := elabInfixOp `bne
@[builtinTermElab heq] def elabHEq : TermElab := elabInfixOp `HEq
@[builtinTermElab equiv] def elabEquiv : TermElab := elabInfixOp `HasEquiv.Equiv
@[builtinTermElab and] def elabAnd : TermElab := elabInfixOp `And
@[builtinTermElab or] def elabOr : TermElab := elabInfixOp `Or
@[builtinTermElab iff] def elabIff : TermElab := elabInfixOp `Iff
@[builtinTermElab band] def elabBAnd : TermElab := elabInfixOp `and
@[builtinTermElab bor] def elabBOr : TermElab := elabInfixOp `or
@[builtinTermElab append] def elabAppend : TermElab := elabInfixOp `HasAppend.append
@[builtinTermElab cons] def elabCons : TermElab := elabInfixOp `List.cons
@[builtinTermElab andthen] def elabAndThen : TermElab := elabInfixOp `HasAndthen.andthen
@[builtinTermElab bindOp] def elabBind : TermElab := elabInfixOp `HasBind.bind
@[builtinTermElab seq] def elabseq : TermElab := elabInfixOp `HasSeq.seq
@[builtinTermElab seqLeft] def elabseqLeft : TermElab := elabInfixOp `HasSeqLeft.seqLeft
@[builtinTermElab seqRight] def elabseqRight : TermElab := elabInfixOp `HasSeqRight.seqRight
@[builtinTermElab map] def elabMap : TermElab := elabInfixOp `Functor.map
@[builtinTermElab mapRev] def elabMapRev : TermElab := elabInfixOp `Functor.mapRev
@[builtinTermElab mapConst] def elabMapConst : TermElab := elabInfixOp `Functor.mapConst
@[builtinTermElab mapConstRev] def elabMapConstRev : TermElab := elabInfixOp `Functor.mapConstRev
@[builtinTermElab orelse] def elabOrElse : TermElab := elabInfixOp `HasOrelse.orelse
@[builtinTermElab orM] def elabOrM : TermElab := elabInfixOp `orM
@[builtinTermElab andM] def elabAndM : TermElab := elabInfixOp `andM
/-
TODO
@[builtinTermElab] def elabsubst : TermElab := elabInfixOp infixR " ▸ " 75
-/
end Term
end Elab
end Lean
|
6b361e7c69976e55a245018843dbe9e223f5b79b | 0d19caa7d4fa8370c14061de04a835cb13b84e87 | /06-formalizacija-dokazov/vaje.lean | f212a61c8a4569b059d57b10d9264d09c3644378 | [] | no_license | natasataskov/teorija-programskih-jezikov | dfc285ce4980f9e24e745677a69f39c5d20bd512 | 82022713f0f5d946b1e59e51d69ab15d518eb07a | refs/heads/master | 1,599,537,599,218 | 1,575,240,132,000 | 1,575,240,132,000 | 221,009,337 | 0 | 0 | null | 1,573,485,567,000 | 1,573,485,566,000 | null | UTF-8 | Lean | false | false | 1,406 | lean | namespace hidden
universe u
-------------------------------------------------------------------------------
inductive list (A:Type) : Type
| Nil {} : list -- Brez {} moramo konstruktorju Nil vedno podati tip A
| Cons : A -> list -> list -- Cons tip A ugotovi iz tipa prvega elementa
namespace list
-- Dopolnite definicije in dokažitve trditve za sezname iz vaj 3. Uporabljate -- lahko notacijo x :: xs, [] in ++ (namesto @), vendar bodite pozorni na
-- oklepaje.
notation x `::` xs := Cons x xs
notation `[]` := Nil
def join {A} : list A -> list A -> list A := sorry
notation xs `++` ys := join xs ys
theorem join_nil {A} (xs: list A) :
xs ++ [] = xs
:=
begin
sorry
end
end list
-------------------------------------------------------------------------------
-- Podobno kot za sezname, napišite tip za drevesa in dokažite trditve iz
-- vaj 3. Če po definiciji tipa `tree` odprete `namespace tree` lahko
-- uporabljate konstruktorje brez predpone, torej `Empty` namesto
-- `tree.Empty`.
-------------------------------------------------------------------------------
-- Definirajte nekaj konstruktov jezika IMP.
inductive loc : Type
| Loc : int -> loc
-- Ker v IMPu ločimo med različnimi vrstami termov, definiramo tip vrst
inductive kind : Type
| AExp | BExp | Comm
-- Tip 'term' sprejme še vrsto terma. Ukazi so tako tipa `term Comm`.
inductive term : kind -> Type
end hidden |
948f58baacefd56db7a1bb6c83a20ab8ff3ee546 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/linear_algebra/matrix/block.lean | a471f4d867a113248a7fc4e567020bae9e73c574 | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 9,847 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Patrick Massot, Casper Putz, Anne Baanen
-/
import linear_algebra.matrix.determinant
import tactic.fin_cases
/-!
# Block matrices and their determinant
This file defines a predicate `matrix.block_triangular_matrix` saying a matrix
is block triangular, and proves the value of the determinant for various
matrices built out of blocks.
## Main definitions
* `matrix.block_triangular_matrix` expresses that a `o` by `o` matrix is block triangular,
if the rows and columns are ordered according to some order `b : o → ℕ`
## Main results
* `det_of_block_triangular_matrix`: the determinant of a block triangular matrix
is equal to the product of the determinants of all the blocks
* `det_of_upper_triangular` and `det_of_lower_triangular`: the determinant of
a triangular matrix is the product of the entries along the diagonal
## Tags
matrix, diagonal, det, block triangular
-/
open_locale big_operators
universes v
variables {m n : Type*} [decidable_eq n] [fintype n] [decidable_eq m] [fintype m]
variables {R : Type v} [comm_ring R]
namespace matrix
lemma det_to_block (M : matrix m m R) (p : m → Prop) [decidable_pred p] :
M.det = (matrix.from_blocks (to_block M p p) (to_block M p (λ j, ¬p j))
(to_block M (λ j, ¬p j) p) (to_block M (λ j, ¬p j) (λ j, ¬p j))).det :=
begin
rw ← matrix.det_reindex_self (equiv.sum_compl p).symm M,
rw [det_apply', det_apply'],
congr, ext σ, congr, ext,
generalize hy : σ x = y,
cases x; cases y;
simp only [matrix.reindex_apply, to_block_apply, equiv.symm_symm,
equiv.sum_compl_apply_inr, equiv.sum_compl_apply_inl,
from_blocks_apply₁₁, from_blocks_apply₁₂, from_blocks_apply₂₁, from_blocks_apply₂₂,
matrix.minor_apply],
end
lemma det_to_square_block (M : matrix m m R) {n : nat} (b : m → fin n) (k : fin n) :
(to_square_block M b k).det = (to_square_block_prop M (λ i, b i = k)).det :=
by simp
lemma det_to_square_block' (M : matrix m m R) (b : m → ℕ) (k : ℕ) :
(to_square_block' M b k).det = (to_square_block_prop M (λ i, b i = k)).det :=
by simp
lemma two_block_triangular_det (M : matrix m m R) (p : m → Prop) [decidable_pred p]
(h : ∀ i (h1 : ¬p i) j (h2 : p j), M i j = 0) :
M.det = (to_square_block_prop M p).det * (to_square_block_prop M (λ i, ¬p i)).det :=
begin
rw det_to_block M p,
convert upper_two_block_triangular_det (to_block M p p) (to_block M p (λ j, ¬p j))
(to_block M (λ j, ¬p j) (λ j, ¬p j)),
ext,
exact h ↑i i.2 ↑j j.2
end
lemma equiv_block_det (M : matrix m m R) {p q : m → Prop} [decidable_pred p] [decidable_pred q]
(e : ∀x, q x ↔ p x) : (to_square_block_prop M p).det = (to_square_block_prop M q).det :=
by convert matrix.det_reindex_self (equiv.subtype_equiv_right e) (to_square_block_prop M q)
lemma to_square_block_det'' (M : matrix m m R) {n : nat} (b : m → fin n) (k : fin n) :
(to_square_block M b k).det = (to_square_block' M (λ i, ↑(b i)) ↑k).det :=
begin
rw [to_square_block_def', to_square_block_def],
apply equiv_block_det,
intro x,
apply (fin.ext_iff _ _).symm
end
/-- Let `b` map rows and columns of a square matrix `M` to `n` blocks. Then
`block_triangular_matrix' M n b` says the matrix is block triangular. -/
def block_triangular_matrix' {o : Type*} (M : matrix o o R) {n : ℕ}
(b : o → fin n) : Prop :=
∀ i j, b j < b i → M i j = 0
lemma upper_two_block_triangular' {m n : Type*}
(A : matrix m m R) (B : matrix m n R) (D : matrix n n R) :
block_triangular_matrix' (from_blocks A B 0 D) (sum.elim (λ i, (0 : fin 2)) (λ j, 1)) :=
begin
intros k1 k2 hk12,
have h0 : ∀ (k : m ⊕ n), sum.elim (λ i, (0 : fin 2)) (λ j, 1) k = 0 → ∃ i, k = sum.inl i,
{ simp },
have h1 : ∀ (k : m ⊕ n), sum.elim (λ i, (0 : fin 2)) (λ j, 1) k = 1 → ∃ j, k = sum.inr j,
{ simp },
set mk1 := (sum.elim (λ i, (0 : fin 2)) (λ j, 1)) k1 with hmk1,
set mk2 := (sum.elim (λ i, (0 : fin 2)) (λ j, 1)) k2 with hmk2,
fin_cases mk1 using h; fin_cases mk2 using h_1; rw [h, h_1] at hk12,
{ exact absurd hk12 (nat.not_lt_zero 0) },
{ exact absurd hk12 (by norm_num) },
{ rw hmk1 at h,
obtain ⟨i, hi⟩ := h1 k1 h,
rw hmk2 at h_1,
obtain ⟨j, hj⟩ := h0 k2 h_1,
rw [hi, hj], simp },
{ exact absurd hk12 (irrefl 1) }
end
/-- Let `b` map rows and columns of a square matrix `M` to blocks indexed by `ℕ`s. Then
`block_triangular_matrix M n b` says the matrix is block triangular. -/
def block_triangular_matrix {o : Type*} (M : matrix o o R) (b : o → ℕ) : Prop :=
∀ i j, b j < b i → M i j = 0
lemma upper_two_block_triangular {m n : Type*}
(A : matrix m m R) (B : matrix m n R) (D : matrix n n R) :
block_triangular_matrix (from_blocks A B 0 D) (sum.elim (λ i, 0) (λ j, 1)) :=
begin
intros k1 k2 hk12,
have h01 : ∀ (k : m ⊕ n), sum.elim (λ i, 0) (λ j, 1) k = 0 ∨ sum.elim (λ i, 0) (λ j, 1) k = 1,
{ simp },
have h0 : ∀ (k : m ⊕ n), sum.elim (λ i, 0) (λ j, 1) k = 0 → ∃ i, k = sum.inl i, { simp },
have h1 : ∀ (k : m ⊕ n), sum.elim (λ i, 0) (λ j, 1) k = 1 → ∃ j, k = sum.inr j, { simp },
cases (h01 k1) with hk1 hk1; cases (h01 k2) with hk2 hk2; rw [hk1, hk2] at hk12,
{ exact absurd hk12 (nat.not_lt_zero 0) },
{ exact absurd hk12 (nat.not_lt_zero 1) },
{ obtain ⟨i, hi⟩ := h1 k1 hk1,
obtain ⟨j, hj⟩ := h0 k2 hk2,
rw [hi, hj], simp },
{ exact absurd hk12 (irrefl 1) }
end
lemma det_of_block_triangular_matrix (M : matrix m m R) (b : m → ℕ)
(h : block_triangular_matrix M b) :
∀ (n : ℕ) (hn : ∀ i, b i < n), M.det = ∏ k in finset.range n, (to_square_block' M b k).det :=
begin
intros n hn,
unfreezingI { induction n with n hi generalizing m M b },
{ rw finset.prod_range_zero,
apply det_eq_one_of_card_eq_zero,
apply fintype.card_eq_zero_iff.mpr,
exact ⟨λ i, nat.not_lt_zero (b i) (hn i)⟩ },
{ rw [finset.prod_range_succ_comm],
have h2 : (M.to_square_block_prop (λ (i : m), b i = n.succ)).det =
(M.to_square_block' b n.succ).det,
{ dunfold to_square_block', dunfold to_square_block_prop, refl },
rw two_block_triangular_det M (λ i, ¬(b i = n)),
{ rw mul_comm,
apply congr (congr_arg has_mul.mul _),
{ let m' := {a // ¬b a = n },
let b' := (λ (i : m'), b ↑i),
have h' :
block_triangular_matrix (M.to_square_block_prop (λ (i : m), ¬b i = n)) b',
{ intros i j, apply h ↑i ↑j },
have hni : ∀ (i : {a // ¬b a = n}), b' i < n,
{ exact λ i, (ne.le_iff_lt i.property).mp (nat.lt_succ_iff.mp (hn ↑i)) },
have h1 := hi (M.to_square_block_prop (λ (i : m), ¬b i = n)) b' h' hni,
rw ←fin.prod_univ_eq_prod_range at h1 ⊢,
convert h1,
ext k,
simp only [to_square_block_def', to_square_block_def],
let he : {a // b' a = ↑k} ≃ {a // b a = ↑k},
{ have hc : ∀ (i : m), (λ a, b a = ↑k) i → (λ a, ¬b a = n) i,
{ intros i hbi, rw hbi, exact ne_of_lt (fin.is_lt k) },
exact equiv.subtype_subtype_equiv_subtype hc },
exact matrix.det_reindex_self he (λ (i j : {a // b' a = ↑k}), M ↑i ↑j) },
{ rw det_to_square_block' M b n,
have hh : ∀ a, b a = n ↔ ¬(λ (i : m), ¬b i = n) a,
{ intro i, simp only [not_not] },
exact equiv_block_det M hh }},
{ intros i hi j hj,
apply (h i), simp only [not_not] at hi,
rw hi,
exact (ne.le_iff_lt hj).mp (nat.lt_succ_iff.mp (hn j)) }}
end
lemma det_of_block_triangular_matrix'' (M : matrix m m R) (b : m → ℕ)
(h : block_triangular_matrix M b) :
M.det = ∏ k in finset.image b finset.univ, (to_square_block' M b k).det :=
begin
let n : ℕ := (Sup (finset.image b finset.univ : set ℕ)).succ,
have hn : ∀ i, b i < n,
{ have hbi : ∀ i, b i ∈ finset.image b finset.univ, { simp },
intro i,
dsimp only [n],
apply nat.lt_succ_iff.mpr,
exact le_cSup (finset.bdd_above _) (hbi i) },
rw det_of_block_triangular_matrix M b h n hn,
refine (finset.prod_subset _ _).symm,
{ intros a ha, apply finset.mem_range.mpr,
obtain ⟨i, ⟨hi, hbi⟩⟩ := finset.mem_image.mp ha,
rw ←hbi,
exact hn i },
{ intros k hk hbk,
apply det_eq_one_of_card_eq_zero,
apply fintype.card_eq_zero_iff.mpr,
constructor,
simp only [subtype.forall],
intros a hba, apply hbk,
apply finset.mem_image.mpr,
use a,
exact ⟨finset.mem_univ a, hba⟩ }
end
lemma det_of_block_triangular_matrix' (M : matrix m m R) {n : ℕ} (b : m → fin n)
(h : block_triangular_matrix' M b) :
M.det = ∏ (k : fin n), (to_square_block M b k).det :=
begin
let b2 : m → ℕ := λ i, ↑(b i),
simp_rw to_square_block_det'',
rw fin.prod_univ_eq_prod_range (λ (k : ℕ), (M.to_square_block' b2 k).det) n,
apply det_of_block_triangular_matrix,
{ intros i j hij, exact h i j (fin.coe_fin_lt.mp hij) },
{ intro i, exact fin.is_lt (b i) }
end
lemma det_of_upper_triangular {n : ℕ} (M : matrix (fin n) (fin n) R)
(h : ∀ (i j : fin n), j < i → M i j = 0) :
M.det = ∏ i : (fin n), M i i :=
begin
convert det_of_block_triangular_matrix' M id h,
ext i,
have h2 : ∀ (j : {a // id a = i}), j = ⟨i, rfl⟩ :=
λ (j : {a // id a = i}), subtype.ext j.property,
haveI : unique {a // id a = i} := ⟨⟨⟨i, rfl⟩⟩, h2⟩,
simp [h2 default]
end
lemma det_of_lower_triangular {n : ℕ} (M : matrix (fin n) (fin n) R)
(h : ∀ (i j : fin n), i < j → M i j = 0) :
M.det = ∏ i : (fin n), M i i :=
begin
rw ← det_transpose,
exact det_of_upper_triangular _ (λ (i j : fin n) (hji : j < i), h j i hji)
end
end matrix
|
02170ed6d81d6716c54076678be016a618c0734e | 63abd62053d479eae5abf4951554e1064a4c45b4 | /test/congr.lean | d107bad15782fd6e8b1889cb8c6ce47de14e1063 | [
"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 | 1,993 | lean | import algebra.group.defs
import data.subtype
import tactic.congr
section congr
open tactic.interactive
example (c : Prop → Prop → Prop → Prop) (x x' y z z' : Prop)
(h₀ : x ↔ x')
(h₁ : z ↔ z') :
c x y z ↔ c x' y z' :=
begin
congr',
{ guard_target x = x', ext, assumption },
{ guard_target z = z', ext, assumption },
end
example {α β γ δ} {F : ∀{α β}, (α → β) → γ → δ} {f g : α → β} {s : γ} (h : ∀ (x : α), f x = g x) :
F f s = F g s :=
by { congr' with x, apply_assumption }
example {α β} {f : _ → β} {x y : {x : {x : α // x = x} // x = x} } (h : x.1 = y.1) :
f x = f y :=
by { congr' with x : 1, exact h }
example {α β} {F : _ → β} {f g : {f : α → β // f = f} }
(h : ∀ x : α, (f : α → β) x = (g : α → β) x) :
F f = F g :=
begin
rcongr x,
revert x,
do { t ← tactic.target, s ← tactic.get_local_type `h, guard (t =ₐ s) },
exact h
end
example {ls : list ℕ} :
ls.map (λ x, (ls.map (λ y, 1 + y)).sum + 1) = ls.map (λ x, (ls.map (λ y, nat.succ y)).sum + 1) :=
begin
rcongr x y, guard_target (1 + y = y.succ), rw [nat.add_comm],
end
example {ls : list ℕ} {f g : ℕ → ℕ} {h : ∀ x, f x = g x} :
ls.map (λ x, f x + 3) = ls.map (λ x, g x + 3) :=
begin
rcongr x, exact h x
end
-- succeed when either `ext` or `congr` can close the goal
example : () = () := by rcongr
example : 0 = 0 := by rcongr
end congr
section convert_to
example {α} [add_comm_monoid α] {a b c d : α} (H : a = c) (H' : b = d) : a + b = d + c :=
by {convert_to c + d = _ using 2, from H, from H', rw[add_comm]}
example {α} [add_comm_monoid α] {a b c d : α} (H : a = c) (H' : b = d) : a + b = d + c :=
by {convert_to c + d = _ using 0, congr' 2, from H, from H', rw[add_comm]}
example {α} [add_comm_monoid α] [partial_order α] {a b c d e f g : α} :
(a + b) + (c + d) + (e + f) + g ≤ a + d + e + f + c + g + b :=
by {ac_change a + d + e + f + c + g + b ≤ _, refl}
end convert_to
|
e94b4216f869acad41cbfb7f4c2a86671c1e822d | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/epi_mono.lean | f59c4b5666a9c23997286f87bc91b9817c829d37 | [] | 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 | 7,591 | lean | /-
Copyright (c) 2019 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton, Scott Morrison
Facts about epimorphisms and monomorphisms.
The definitions of `epi` and `mono` are in `category_theory.category`,
since they are used by some lemmas for `iso`, which is used everywhere.
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.adjunction.basic
import Mathlib.category_theory.opposites
import Mathlib.PostPort
universes v₁ v₂ u₁ u₂ l
namespace Mathlib
namespace category_theory
theorem left_adjoint_preserves_epi {C : Type u₁} [category C] {D : Type u₂} [category D] {F : C ⥤ D} {G : D ⥤ C} (adj : F ⊣ G) {X : C} {Y : C} {f : X ⟶ Y} (hf : epi f) : epi (functor.map F f) := sorry
theorem right_adjoint_preserves_mono {C : Type u₁} [category C] {D : Type u₂} [category D] {F : C ⥤ D} {G : D ⥤ C} (adj : F ⊣ G) {X : D} {Y : D} {f : X ⟶ Y} (hf : mono f) : mono (functor.map G f) := sorry
theorem faithful_reflects_epi {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) [faithful F] {X : C} {Y : C} {f : X ⟶ Y} (hf : epi (functor.map F f)) : epi f := sorry
theorem faithful_reflects_mono {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) [faithful F] {X : C} {Y : C} {f : X ⟶ Y} (hf : mono (functor.map F f)) : mono f := sorry
/--
A split monomorphism is a morphism `f : X ⟶ Y` admitting a retraction `retraction f : Y ⟶ X`
such that `f ≫ retraction f = 𝟙 X`.
Every split monomorphism is a monomorphism.
-/
class split_mono {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y)
where
retraction : Y ⟶ X
id' : autoParam (f ≫ retraction = 𝟙)
(Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously")
(Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") [])
/--
A split epimorphism is a morphism `f : X ⟶ Y` admitting a section `section_ f : Y ⟶ X`
such that `section_ f ≫ f = 𝟙 Y`.
(Note that `section` is a reserved keyword, so we append an underscore.)
Every split epimorphism is an epimorphism.
-/
class split_epi {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y)
where
section_ : Y ⟶ X
id' : autoParam (section_ ≫ f = 𝟙)
(Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously")
(Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") [])
/-- The chosen retraction of a split monomorphism. -/
def retraction {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y) [split_mono f] : Y ⟶ X :=
split_mono.retraction f
@[simp] theorem split_mono.id_assoc {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y) [split_mono f] {X' : C} (f' : X ⟶ X') : f ≫ retraction f ≫ f' = f' := sorry
/-- The retraction of a split monomorphism is itself a split epimorphism. -/
protected instance retraction_split_epi {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y) [split_mono f] : split_epi (retraction f) :=
split_epi.mk f
/-- A split mono which is epi is an iso. -/
def is_iso_of_epi_of_split_mono {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y) [split_mono f] [epi f] : is_iso f :=
is_iso.mk (retraction f)
/--
The chosen section of a split epimorphism.
(Note that `section` is a reserved keyword, so we append an underscore.)
-/
def section_ {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y) [split_epi f] : Y ⟶ X :=
split_epi.section_ f
@[simp] theorem split_epi.id_assoc {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y) [split_epi f] {X' : C} (f' : Y ⟶ X') : section_ f ≫ f ≫ f' = f' := sorry
/-- The section of a split epimorphism is itself a split monomorphism. -/
protected instance section_split_mono {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y) [split_epi f] : split_mono (section_ f) :=
split_mono.mk f
/-- A split epi which is mono is an iso. -/
def is_iso_of_mono_of_split_epi {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y) [mono f] [split_epi f] : is_iso f :=
is_iso.mk (section_ f)
/-- Every iso is a split mono. -/
protected instance split_mono.of_iso {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y) [is_iso f] : split_mono f :=
split_mono.mk (inv f)
/-- Every iso is a split epi. -/
protected instance split_epi.of_iso {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y) [is_iso f] : split_epi f :=
split_epi.mk (inv f)
/-- Every split mono is a mono. -/
protected instance split_mono.mono {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y) [split_mono f] : mono f := sorry
/-- Every split epi is an epi. -/
protected instance split_epi.epi {C : Type u₁} [category C] {X : C} {Y : C} (f : X ⟶ Y) [split_epi f] : epi f :=
epi.mk
fun (Z : C) (g h : Y ⟶ Z) (w : f ≫ g = f ≫ h) =>
eq.mpr (id (Eq.refl (g = h)))
(eq.mp
((fun (a a_1 : Y ⟶ Z) (e_1 : a = a_1) (ᾰ ᾰ_1 : Y ⟶ Z) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2)
(section_ f ≫ f ≫ g) g (split_epi.id_assoc f g) (section_ f ≫ f ≫ h) h (split_epi.id_assoc f h))
(section_ f ≫= w))
/-- Every split mono whose retraction is mono is an iso. -/
def is_iso.of_mono_retraction {C : Type u₁} [category C] {X : C} {Y : C} {f : X ⟶ Y} [split_mono f] [mono (retraction f)] : is_iso f :=
is_iso.mk (retraction f)
/-- Every split epi whose section is epi is an iso. -/
def is_iso.of_epi_section {C : Type u₁} [category C] {X : C} {Y : C} {f : X ⟶ Y} [split_epi f] [epi (section_ f)] : is_iso f :=
is_iso.mk (section_ f)
protected instance unop_mono_of_epi {C : Type u₁} [category C] {A : Cᵒᵖ} {B : Cᵒᵖ} (f : A ⟶ B) [epi f] : mono (has_hom.hom.unop f) :=
mono.mk
fun (Z : C) (g h : Z ⟶ opposite.unop B) (eq : g ≫ has_hom.hom.unop f = h ≫ has_hom.hom.unop f) =>
has_hom.hom.op_inj (iff.mp (cancel_epi f) (has_hom.hom.unop_inj eq))
protected instance unop_epi_of_mono {C : Type u₁} [category C] {A : Cᵒᵖ} {B : Cᵒᵖ} (f : A ⟶ B) [mono f] : epi (has_hom.hom.unop f) :=
epi.mk
fun (Z : C) (g h : opposite.unop A ⟶ Z) (eq : has_hom.hom.unop f ≫ g = has_hom.hom.unop f ≫ h) =>
has_hom.hom.op_inj (iff.mp (cancel_mono f) (has_hom.hom.unop_inj eq))
protected instance op_mono_of_epi {C : Type u₁} [category C] {A : C} {B : C} (f : A ⟶ B) [epi f] : mono (has_hom.hom.op f) :=
mono.mk
fun (Z : Cᵒᵖ) (g h : Z ⟶ opposite.op B) (eq : g ≫ has_hom.hom.op f = h ≫ has_hom.hom.op f) =>
has_hom.hom.unop_inj (iff.mp (cancel_epi f) (has_hom.hom.op_inj eq))
protected instance op_epi_of_mono {C : Type u₁} [category C] {A : C} {B : C} (f : A ⟶ B) [mono f] : epi (has_hom.hom.op f) :=
epi.mk
fun (Z : Cᵒᵖ) (g h : opposite.op A ⟶ Z) (eq : has_hom.hom.op f ≫ g = has_hom.hom.op f ≫ h) =>
has_hom.hom.unop_inj (iff.mp (cancel_mono f) (has_hom.hom.op_inj eq))
/-- Split monomorphisms are also absolute monomorphisms. -/
protected instance functor.map.split_mono {C : Type u₁} [category C] {D : Type u₂} [category D] {X : C} {Y : C} (f : X ⟶ Y) [split_mono f] (F : C ⥤ D) : split_mono (functor.map F f) :=
split_mono.mk (functor.map F (retraction f))
/-- Split epimorphisms are also absolute epimorphisms. -/
protected instance functor.map.split_epi {C : Type u₁} [category C] {D : Type u₂} [category D] {X : C} {Y : C} (f : X ⟶ Y) [split_epi f] (F : C ⥤ D) : split_epi (functor.map F f) :=
split_epi.mk (functor.map F (section_ f))
|
1ab4674e8d6a266c142b603d5fe2a3c990281d4c | a8c03ed21a1bd6fc45901943b79dd6574ea3f0c2 | /clause.lean | 26fcff3c885911308f7dd049ea31933585cfa073 | [] | no_license | gebner/resolution.lean | 716c355fbb5204e5c4d0c5a7f3f3cc825892a2bf | c6fafe06fba1cfad73db68f2aa474b29fe892a2b | refs/heads/master | 1,601,111,444,528 | 1,475,256,701,000 | 1,475,256,701,000 | 67,711,151 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,626 | lean | import init.meta.tactic utils
open expr list tactic monad decidable
meta structure clause :=
(num_quants : ℕ)
(num_lits : ℕ)
(proof : expr)
(type : expr)
namespace clause
private meta def tactic_format (c : clause) : tactic format := do
prf_fmt : format ← pp (proof c),
type_fmt ← pp (type c),
return $ prf_fmt ++ to_fmt " : " ++ type_fmt ++ to_fmt " (" ++
to_fmt (num_quants c) ++ to_fmt " quants, " ++ to_fmt (num_lits c) ++ to_fmt " lits" ++ to_fmt ")"
meta instance : has_to_tactic_format clause := ⟨tactic_format⟩
meta def num_binders (c : clause) : ℕ := num_quants c + num_lits c
/- foo foo -- bar baz -/
private meta def parse_clause_type : expr → ℕ × ℕ × bool
| (pi n bi d b) :=
match parse_clause_type b with
| (0, ls, fin) := if has_var b then (1, ls, fin) else (0, ls+1, fin)
| (qs, ls, fin) := (qs+1, ls, fin)
end
| e := if expr.is_false e then (0, 0, ff) else (0, 1, tt)
meta def validate (c : clause) : tactic unit := do
type' ← infer_type c↣proof,
unify c↣type type' <|> (do pp_ty ← pp c↣type, pp_ty' ← pp type',
fail (to_fmt "wrong type: " ++ pp_ty ++ " =!= " ++ pp_ty'))
meta def inst (c : clause) (e : expr) : clause :=
(if num_quants c > 0
then mk (num_quants c - 1) (num_lits c)
else mk 0 (num_lits c - 1))
(app (proof c) e) (instantiate_var (binding_body (type c)) e)
meta def instn (c : clause) (es : list expr) : clause :=
foldr (λe c', inst c' e) c es
meta def open_const (c : clause) : tactic (clause × expr) := do
n ← mk_fresh_name,
b ← return $ local_const n (binding_name (type c)) (binding_info (type c)) (binding_domain (type c)),
return (inst c b, b)
meta def open_meta (c : clause) : tactic (clause × expr) := do
b ← mk_meta_var (binding_domain (type c)),
return (inst c b, b)
meta def close_const (c : clause) (e : expr) : clause :=
match e with
| local_const uniq pp info t :=
let abst_type' := abstract_local (type c) (local_uniq_name e) in
let type' := pi pp binder_info.default t (abstract_local (type c) uniq) in
let abs_prf := abstract_local (proof c) uniq in
let proof' := lambdas [e] c↣proof in
if num_quants c > 0 ∨ has_var abst_type' then
{ c with num_quants := c↣num_quants + 1, proof := proof', type := type' }
else
{ c with num_lits := c↣num_lits + 1, proof := proof', type := type' }
| _ := ⟨0, 0, default expr, default expr⟩
end
meta def open_constn : clause → ℕ → tactic (clause × list expr)
| c 0 := return (c, nil)
| c (n+1) := do
(c', b) ← open_const c,
(c'', bs) ← open_constn c' n,
return (c'', b::bs)
meta def open_metan : clause → ℕ → tactic (clause × list expr)
| c 0 := return (c, nil)
| c (n+1) := do
(c', b) ← open_meta c,
(c'', bs) ← open_metan c' n,
return (c'', b::bs)
meta def close_constn : clause → list expr → clause
| c [] := c
| c (b::bs') := close_const (close_constn c bs') b
set_option eqn_compiler.max_steps 500
private meta def parse_clause : expr → expr → tactic clause
| proof (pi n bi d b) := do
lc_n ← mk_fresh_name,
lc ← return $ local_const lc_n n bi d,
c ← parse_clause (app proof lc) (instantiate_var b lc),
return $ c↣close_const (local_const lc_n n binder_info.default d)
| proof (const ``false []) := return { num_quants := 0, num_lits := 0, proof := proof, type := false_ }
| proof (app (const ``not []) formula) := parse_clause proof (formula↣imp false_)
| proof type := do
univ ← infer_univ type,
not_type ← return $ if univ = level.zero then not_ type else type↣imp false_,
parse_clause (lam `H binder_info.default not_type $ app (mk_var 0) proof) (imp not_type false_)
meta def of_proof_and_type (proof type : expr) : tactic clause :=
parse_clause proof type
meta def of_proof (proof : expr) : tactic clause := do
type ← infer_type proof,
of_proof_and_type proof type
meta def inst_mvars (c : clause) : tactic clause := do
proof' ← instantiate_mvars (proof c),
type' ← instantiate_mvars (type c),
return { c with proof := proof', type := type' }
meta inductive literal
| left : expr → literal
| right : expr → literal
namespace literal
meta instance : decidable_eq literal := by mk_dec_eq_instance
meta def formula : literal → expr
| (left f) := f
| (right f) := f
meta def is_neg : literal → bool
| (left _) := tt
| (right _) := ff
meta def is_pos (l : literal) : bool := bnot l↣is_neg
meta def to_formula (l : literal) : tactic expr :=
if is_neg l then mk_mapp ``not [some (formula l)]
else return (formula l)
meta def type_str : literal → string
| (literal.left _) := "left"
| (literal.right _) := "right"
meta instance : has_to_tactic_format literal :=
⟨λl, do
pp_f ← pp l↣formula,
return $ to_fmt l↣type_str ++ " (" ++ pp_f ++ ")"⟩
end literal
private meta def get_binding_body : expr → ℕ → expr
| e 0 := e
| e (i+1) := get_binding_body e↣binding_body i
meta def get_binder (e : expr) (i : nat) :=
binding_domain (get_binding_body e i)
meta def get_lit (c : clause) (i : nat) : literal :=
let bind := get_binder (type c) (num_quants c + i) in
match is_not bind with
| some formula := literal.right formula
| none := literal.left bind
end
meta def lits_where (c : clause) (p : literal → bool) : list nat :=
list.filter (λl, p (get_lit c l)) (range (num_lits c))
meta def get_lits (c : clause) : list literal :=
list.map (get_lit c) (range c↣num_lits)
meta def is_maximal (gt : expr → expr → bool) (c : clause) (i : nat) : bool :=
list.empty (filter (λj, gt (get_lit c j)↣formula (get_lit c i)↣formula) (range c↣num_lits))
meta def normalize (c : clause) : tactic clause := do
opened ← open_constn c (num_binders c),
lconsts_in_types ← return $ contained_lconsts_list (list.map local_type opened.2),
quants' ← return $ filter (λlc, rb_map.contains lconsts_in_types (local_uniq_name lc)) opened.2,
lits' ← return $ filter (λlc, ¬rb_map.contains lconsts_in_types (local_uniq_name lc)) opened.2,
return $ close_constn opened.1 (quants' ++ lits')
meta def whnf_head_lit (c : clause) : tactic clause := do
atom' ← whnf (literal.formula $ get_lit c 0),
return $
if literal.is_neg (get_lit c 0) then
{ c with type := imp atom' (binding_body c↣type) }
else
{ c with type := imp (app (const ``not []) atom') c↣type↣binding_body }
end clause
meta def unify_lit (l1 l2 : clause.literal) : tactic unit :=
if clause.literal.is_pos l1 = clause.literal.is_pos l2 then
unify_core transparency.none (clause.literal.formula l1) (clause.literal.formula l2)
else
fail "cannot unify literals"
-- FIXME: this is most definitely broken with meta-variables that were already in the goal
meta def sort_and_constify_metas : list expr → tactic (list expr)
| exprs_with_metas := do
inst_exprs ← mapM instantiate_mvars exprs_with_metas,
metas ← return $ inst_exprs >>= get_metas,
match list.filter (λm, ¬has_meta_var (get_meta_type m)) metas with
| [] :=
if list.empty metas then
return []
else do
forM' metas (λm, do trace (expr.to_string m), t ← infer_type m, trace (expr.to_string t)),
fail "could not sort metas"
| ((mvar n t) :: _) := do
t' ← infer_type (mvar n t),
uniq ← mk_fresh_name,
c ← return (local_const uniq uniq binder_info.default t'),
unify c (mvar n t),
rest ← sort_and_constify_metas metas,
return (rest ++ [c])
| _ := failed
end
namespace clause
meta def meta_closure (metas : list expr) (qf : clause) : tactic clause := do
bs ← sort_and_constify_metas metas,
qf' ← clause.inst_mvars qf,
clause.inst_mvars $ clause.close_constn qf' bs
end clause
|
2f45b5debac1be352924ebaec83f758ece812dd2 | b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77 | /src/algebra/group_with_zero/power.lean | 4c398f6d19668b758976ea66dc0291216dedf286 | [
"Apache-2.0"
] | permissive | molodiuc/mathlib | cae2ba3ef1601c1f42ca0b625c79b061b63fef5b | 98ebe5a6739fbe254f9ee9d401882d4388f91035 | refs/heads/master | 1,674,237,127,059 | 1,606,353,533,000 | 1,606,353,533,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,292 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import algebra.group_power
/-!
# Powers of elements of groups with an adjoined zero element
In this file we define integer power functions for groups with an adjoined zero element.
This generalises the integer power function on a division ring.
-/
section zero
variables {M : Type*} [monoid_with_zero M]
@[simp] lemma zero_pow' : ∀ n : ℕ, n ≠ 0 → (0 : M) ^ n = 0
| 0 h := absurd rfl h
| (k+1) h := zero_mul _
lemma ne_zero_pow {a : M} {n : ℕ} (hn : n ≠ 0) : a ^ n ≠ 0 → a ≠ 0 :=
by { contrapose!, rintro rfl, exact zero_pow' n hn }
@[simp] lemma zero_pow_eq_zero [nontrivial M] {n : ℕ} : (0 : M) ^ n = 0 ↔ 0 < n :=
begin
split; intro h,
{ rw [nat.pos_iff_ne_zero], rintro rfl, simpa using h },
{ exact zero_pow' n h.ne.symm }
end
end zero
section group_with_zero
variables {G₀ : Type*} [group_with_zero G₀]
section nat_pow
@[simp, field_simps] theorem inv_pow' (a : G₀) (n : ℕ) : (a⁻¹) ^ n = (a ^ n)⁻¹ :=
by induction n with n ih; [exact inv_one.symm,
rw [pow_succ', pow_succ, ih, mul_inv_rev']]
theorem pow_sub' (a : G₀) {m n : ℕ} (ha : a ≠ 0) (h : n ≤ m) : a ^ (m - n) = a ^ m * (a ^ n)⁻¹ :=
have h1 : m - n + n = m, from nat.sub_add_cancel h,
have h2 : a ^ (m - n) * a ^ n = a ^ m, by rw [←pow_add, h1],
eq_div_of_mul_eq (pow_ne_zero _ ha) h2
theorem pow_inv_comm' (a : G₀) (m n : ℕ) : (a⁻¹) ^ m * a ^ n = a ^ n * (a⁻¹) ^ m :=
(commute.refl a).inv_left'.pow_pow m n
end nat_pow
end group_with_zero
section int_pow
open int
variables {G₀ : Type*} [group_with_zero G₀]
/--
The power operation in a group with zero.
This extends `monoid.pow` to negative integers
with the definition `a ^ (-n) = (a ^ n)⁻¹`.
-/
def fpow (a : G₀) : ℤ → G₀
| (of_nat n) := a ^ n
| -[1+n] := (a ^ (nat.succ n))⁻¹
@[priority 10] instance : has_pow G₀ ℤ := ⟨fpow⟩
@[simp] theorem fpow_coe_nat (a : G₀) (n : ℕ) : a ^ (n:ℤ) = a ^ n := rfl
theorem fpow_of_nat (a : G₀) (n : ℕ) : a ^ of_nat n = a ^ n := rfl
@[simp] theorem fpow_neg_succ_of_nat (a : G₀) (n : ℕ) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := rfl
local attribute [ematch] le_of_lt
@[simp] theorem fpow_zero (a : G₀) : a ^ (0:ℤ) = 1 := rfl
@[simp] theorem fpow_one (a : G₀) : a ^ (1:ℤ) = a := mul_one _
@[simp] theorem one_fpow : ∀ (n : ℤ), (1 : G₀) ^ n = 1
| (n : ℕ) := one_pow _
| -[1+ n] := show _⁻¹=(1:G₀), by rw [one_pow, inv_one]
lemma zero_fpow : ∀ z : ℤ, z ≠ 0 → (0 : G₀) ^ z = 0
| (of_nat n) h := zero_pow' _ $ by rintro rfl; exact h rfl
| -[1+n] h := show (0*0 ^ n)⁻¹ = (0 : G₀), by simp
@[simp] theorem fpow_neg (a : G₀) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹
| (n+1:ℕ) := rfl
| 0 := inv_one.symm
| -[1+ n] := (inv_inv' _).symm
theorem fpow_neg_one (x : G₀) : x ^ (-1:ℤ) = x⁻¹ := congr_arg has_inv.inv $ pow_one x
theorem inv_fpow (a : G₀) : ∀n:ℤ, a⁻¹ ^ n = (a ^ n)⁻¹
| (n : ℕ) := inv_pow' a n
| -[1+ n] := congr_arg has_inv.inv $ inv_pow' a (n+1)
lemma fpow_add_one {a : G₀} (ha : a ≠ 0) : ∀ n : ℤ, a ^ (n + 1) = a ^ n * a
| (of_nat n) := by simp [← int.coe_nat_succ, pow_succ']
| -[1+0] := by simp [int.neg_succ_of_nat_eq, ha]
| -[1+(n+1)] := by rw [int.neg_succ_of_nat_eq, fpow_neg, neg_add, neg_add_cancel_right, fpow_neg,
← int.coe_nat_succ, fpow_coe_nat, fpow_coe_nat, pow_succ _ (n + 1), mul_inv_rev', mul_assoc,
inv_mul_cancel ha, mul_one]
lemma fpow_sub_one {a : G₀} (ha : a ≠ 0) (n : ℤ) : a ^ (n - 1) = a ^ n * a⁻¹ :=
calc a ^ (n - 1) = a ^ (n - 1) * a * a⁻¹ : by rw [mul_assoc, mul_inv_cancel ha, mul_one]
... = a^n * a⁻¹ : by rw [← fpow_add_one ha, sub_add_cancel]
lemma fpow_add {a : G₀} (ha : a ≠ 0) (m n : ℤ) : a ^ (m + n) = a ^ m * a ^ n :=
begin
induction n using int.induction_on with n ihn n ihn,
case hz : { simp },
{ simp only [← add_assoc, fpow_add_one ha, ihn, mul_assoc] },
{ rw [fpow_sub_one ha, ← mul_assoc, ← ihn, ← fpow_sub_one ha, add_sub_assoc] }
end
lemma fpow_add' {a : G₀} {m n : ℤ} (h : a ≠ 0 ∨ m + n ≠ 0 ∨ m = 0 ∧ n = 0) :
a ^ (m + n) = a ^ m * a ^ n :=
begin
by_cases hm : m = 0, { simp [hm] },
by_cases hn : n = 0, { simp [hn] },
by_cases ha : a = 0,
{ subst a,
simp only [false_or, eq_self_iff_true, not_true, ne.def, hm, hn, false_and, or_false] at h,
rw [zero_fpow _ h, zero_fpow _ hm, zero_mul] },
{ exact fpow_add ha m n }
end
theorem fpow_one_add {a : G₀} (h : a ≠ 0) (i : ℤ) : a ^ (1 + i) = a * a ^ i :=
by rw [fpow_add h, fpow_one]
theorem semiconj_by.fpow_right {a x y : G₀} (h : semiconj_by a x y) :
∀ m : ℤ, semiconj_by a (x^m) (y^m)
| (n : ℕ) := h.pow_right n
| -[1+n] := (h.pow_right (n + 1)).inv_right'
theorem commute.fpow_right {a b : G₀} (h : commute a b) : ∀ m : ℤ, commute a (b^m) :=
h.fpow_right
theorem commute.fpow_left {a b : G₀} (h : commute a b) (m : ℤ) : commute (a^m) b :=
(h.symm.fpow_right m).symm
theorem commute.fpow_fpow {a b : G₀} (h : commute a b) (m n : ℤ) : commute (a^m) (b^n) :=
(h.fpow_left m).fpow_right n
theorem commute.fpow_self (a : G₀) (n : ℤ) : commute (a^n) a := (commute.refl a).fpow_left n
theorem commute.self_fpow (a : G₀) (n : ℤ) : commute a (a^n) := (commute.refl a).fpow_right n
theorem commute.fpow_fpow_self (a : G₀) (m n : ℤ) : commute (a^m) (a^n) := (commute.refl a).fpow_fpow m n
theorem fpow_bit0 (a : G₀) (n : ℤ) : a ^ bit0 n = a ^ n * a ^ n :=
begin
apply fpow_add', right,
by_cases hn : n = 0,
{ simp [hn] },
{ simp [← two_mul, hn, two_ne_zero] }
end
theorem fpow_bit1 (a : G₀) (n : ℤ) : a ^ bit1 n = a ^ n * a ^ n * a :=
begin
rw [← fpow_bit0, bit1, fpow_add', fpow_one],
right, left,
apply bit1_ne_zero
end
theorem fpow_mul (a : G₀) : ∀ m n : ℤ, a ^ (m * n) = (a ^ m) ^ n
| (m : ℕ) (n : ℕ) := pow_mul _ _ _
| (m : ℕ) -[1+ n] := (fpow_neg _ (m * n.succ)).trans $
show (a ^ (m * n.succ))⁻¹ = _, by rw pow_mul; refl
| -[1+ m] (n : ℕ) := (fpow_neg _ (m.succ * n)).trans $
show (a ^ (m.succ * n))⁻¹ = _, by rw [pow_mul, ← inv_pow']; refl
| -[1+ m] -[1+ n] := (pow_mul a m.succ n.succ).trans $
show _ = (_⁻¹ ^ _)⁻¹, by rw [inv_pow', inv_inv']
theorem fpow_mul' (a : G₀) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m :=
by rw [mul_comm, fpow_mul]
@[simp, norm_cast] lemma units.coe_gpow' (u : units G₀) :
∀ (n : ℤ), ((u ^ n : units G₀) : G₀) = u ^ n
| (n : ℕ) := u.coe_pow n
| -[1+k] := by rw [gpow_neg_succ_of_nat, fpow_neg_succ_of_nat, units.coe_inv', u.coe_pow]
lemma fpow_ne_zero_of_ne_zero {a : G₀} (ha : a ≠ 0) : ∀ (z : ℤ), a ^ z ≠ 0
| (of_nat n) := pow_ne_zero _ ha
| -[1+n] := inv_ne_zero $ pow_ne_zero _ ha
lemma fpow_sub {a : G₀} (ha : a ≠ 0) (z1 z2 : ℤ) : a ^ (z1 - z2) = a ^ z1 / a ^ z2 :=
by rw [sub_eq_add_neg, fpow_add ha, fpow_neg]; refl
lemma commute.mul_fpow {a b : G₀} (h : commute a b) :
∀ (i : ℤ), (a * b) ^ i = (a ^ i) * (b ^ i)
| (n : ℕ) := h.mul_pow n
| -[1+n] := by simp [h.mul_pow, (h.pow_pow _ _).eq, mul_inv_rev']
lemma mul_fpow {G₀ : Type*} [comm_group_with_zero G₀] (a b : G₀) (m : ℤ):
(a * b) ^ m = (a ^ m) * (b ^ m) :=
(commute.all a b).mul_fpow m
theorem fpow_bit0' (a : G₀) (n : ℤ) : a ^ bit0 n = (a * a) ^ n :=
(fpow_bit0 a n).trans ((commute.refl a).mul_fpow n).symm
theorem fpow_bit1' (a : G₀) (n : ℤ) : a ^ bit1 n = (a * a) ^ n * a :=
by rw [fpow_bit1, (commute.refl a).mul_fpow]
lemma fpow_eq_zero {x : G₀} {n : ℤ} (h : x ^ n = 0) : x = 0 :=
classical.by_contradiction $ λ hx, fpow_ne_zero_of_ne_zero hx n h
lemma fpow_ne_zero {x : G₀} (n : ℤ) : x ≠ 0 → x ^ n ≠ 0 :=
mt fpow_eq_zero
theorem fpow_neg_mul_fpow_self (n : ℤ) {x : G₀} (h : x ≠ 0) :
x ^ (-n) * x ^ n = 1 :=
begin
rw [fpow_neg],
exact inv_mul_cancel (fpow_ne_zero n h)
end
theorem one_div_pow {a : G₀} (n : ℕ) :
(1 / a) ^ n = 1 / a ^ n :=
by simp only [one_div, inv_pow']
theorem one_div_fpow {a : G₀} (n : ℤ) :
(1 / a) ^ n = 1 / a ^ n :=
by simp only [one_div, inv_fpow]
@[simp] lemma inv_fpow' {a : G₀} (n : ℤ) :
(a ⁻¹) ^ n = a ^ (-n) :=
by { rw [inv_fpow, ← fpow_neg_one, ← fpow_mul], simp }
end int_pow
section
variables {G₀ : Type*} [comm_group_with_zero G₀]
@[simp] theorem div_pow (a b : G₀) (n : ℕ) :
(a / b) ^ n = a ^ n / b ^ n :=
by simp only [div_eq_mul_inv, mul_pow, inv_pow']
@[simp] theorem div_fpow (a : G₀) {b : G₀} (n : ℤ) :
(a / b) ^ n = a ^ n / b ^ n :=
by simp only [div_eq_mul_inv, mul_fpow, inv_fpow]
lemma div_sq_cancel {a : G₀} (ha : a ≠ 0) (b : G₀) : a ^ 2 * b / a = a * b :=
by rw [pow_two, mul_assoc, mul_div_cancel_left _ ha]
end
/-- If a monoid homomorphism `f` between two `group_with_zero`s maps `0` to `0`, then it maps `x^n`,
`n : ℤ`, to `(f x)^n`. -/
lemma monoid_with_zero_hom.map_fpow {G₀ G₀' : Type*} [group_with_zero G₀] [group_with_zero G₀']
(f : monoid_with_zero_hom G₀ G₀') (x : G₀) :
∀ n : ℤ, f (x ^ n) = f x ^ n
| (n : ℕ) := f.to_monoid_hom.map_pow x n
| -[1+n] := (f.map_inv' _).trans $ congr_arg _ $ f.to_monoid_hom.map_pow x _
|
ee460d8cea647d2703b48ac544eb73e87492cf42 | 54d7e71c3616d331b2ec3845d31deb08f3ff1dea | /tests/lean/pp_zero_bug.lean | 66ecd1b97e5319d65abab79dd82e653bf1740d0f | [
"Apache-2.0"
] | permissive | pachugupta/lean | 6f3305c4292288311cc4ab4550060b17d49ffb1d | 0d02136a09ac4cf27b5c88361750e38e1f485a1a | refs/heads/master | 1,611,110,653,606 | 1,493,130,117,000 | 1,493,167,649,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 30 | lean | #check @zero
#check @zero nat
|
173b29c785809195bd8dc2ddd108bd2f485e29c1 | d29d82a0af640c937e499f6be79fc552eae0aa13 | /src/algebra/lie/submodule.lean | 57d897cdc5bd16704c1c56c067c8ff4be8dba0e1 | [
"Apache-2.0"
] | permissive | AbdulMajeedkhurasani/mathlib | 835f8a5c5cf3075b250b3737172043ab4fa1edf6 | 79bc7323b164aebd000524ebafd198eb0e17f956 | refs/heads/master | 1,688,003,895,660 | 1,627,788,521,000 | 1,627,788,521,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 33,228 | lean | /-
Copyright (c) 2021 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import algebra.lie.subalgebra
import ring_theory.noetherian
/-!
# Lie submodules of a Lie algebra
In this file we define Lie submodules and Lie ideals, we construct the lattice structure on Lie
submodules and we use it to define various important operations, notably the Lie span of a subset
of a Lie module.
## Main definitions
* `lie_submodule`
* `lie_submodule.well_founded_of_noetherian`
* `lie_submodule.lie_span`
* `lie_submodule.map`
* `lie_submodule.comap`
* `lie_ideal`
* `lie_ideal.map`
* `lie_ideal.comap`
## Tags
lie algebra, lie submodule, lie ideal, lattice structure
-/
universes u v w w₁ w₂
section lie_submodule
variables (R : Type u) (L : Type v) (M : Type w)
variables [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M]
variables [lie_ring_module L M] [lie_module R L M]
set_option old_structure_cmd true
/-- A Lie submodule of a Lie module is a submodule that is closed under the Lie bracket.
This is a sufficient condition for the subset itself to form a Lie module. -/
structure lie_submodule extends submodule R M :=
(lie_mem : ∀ {x : L} {m : M}, m ∈ carrier → ⁅x, m⁆ ∈ carrier)
attribute [nolint doc_blame] lie_submodule.to_submodule
namespace lie_submodule
variables {R L M} (N N' : lie_submodule R L M)
/-- The zero module is a Lie submodule of any Lie module. -/
instance : has_zero (lie_submodule R L M) :=
⟨{ lie_mem := λ x m h, by { rw ((submodule.mem_bot R).1 h), apply lie_zero, },
..(0 : submodule R M)}⟩
instance : inhabited (lie_submodule R L M) := ⟨0⟩
instance coe_submodule : has_coe (lie_submodule R L M) (submodule R M) := ⟨to_submodule⟩
@[norm_cast]
lemma coe_to_submodule : ((N : submodule R M) : set M) = N := rfl
instance has_mem : has_mem M (lie_submodule R L M) := ⟨λ x N, x ∈ (N : set M)⟩
@[simp] lemma mem_carrier {x : M} : x ∈ N.carrier ↔ x ∈ (N : set M) :=
iff.rfl
@[simp] lemma mem_mk_iff (S : set M) (h₁ h₂ h₃ h₄) {x : M} :
x ∈ (⟨S, h₁, h₂, h₃, h₄⟩ : lie_submodule R L M) ↔ x ∈ S :=
iff.rfl
@[simp] lemma mem_coe_submodule {x : M} : x ∈ (N : submodule R M) ↔ x ∈ N := iff.rfl
lemma mem_coe {x : M} : x ∈ (N : set M) ↔ x ∈ N := iff.rfl
@[simp] lemma zero_mem : (0 : M) ∈ N := (N : submodule R M).zero_mem
@[simp] lemma coe_to_set_mk (S : set M) (h₁ h₂ h₃ h₄) :
((⟨S, h₁, h₂, h₃, h₄⟩ : lie_submodule R L M) : set M) = S := rfl
@[simp] lemma coe_to_submodule_mk (p : submodule R M) (h) :
(({lie_mem := h, ..p} : lie_submodule R L M) : submodule R M) = p :=
by { cases p, refl, }
@[ext] lemma ext (h : ∀ m, m ∈ N ↔ m ∈ N') : N = N' :=
by { cases N, cases N', simp only [], ext m, exact h m, }
@[simp] lemma coe_to_submodule_eq_iff : (N : submodule R M) = (N' : submodule R M) ↔ N = N' :=
begin
split; intros h,
{ ext, rw [← mem_coe_submodule, h], simp, },
{ rw h, },
end
/-- Copy of a lie_submodule with a new `carrier` equal to the old one. Useful to fix definitional
equalities. -/
protected def copy (s : set M) (hs : s = ↑N) : lie_submodule R L M :=
{ carrier := s,
zero_mem' := hs.symm ▸ N.zero_mem',
add_mem' := hs.symm ▸ N.add_mem',
smul_mem' := hs.symm ▸ N.smul_mem',
lie_mem := hs.symm ▸ N.lie_mem, }
instance : lie_ring_module L N :=
{ bracket := λ (x : L) (m : N), ⟨⁅x, m.val⁆, N.lie_mem m.property⟩,
add_lie := by { intros x y m, apply set_coe.ext, apply add_lie, },
lie_add := by { intros x m n, apply set_coe.ext, apply lie_add, },
leibniz_lie := by { intros x y m, apply set_coe.ext, apply leibniz_lie, }, }
instance : lie_module R L N :=
{ lie_smul := by { intros t x y, apply set_coe.ext, apply lie_smul, },
smul_lie := by { intros t x y, apply set_coe.ext, apply smul_lie, }, }
@[simp, norm_cast] lemma coe_zero : ((0 : N) : M) = (0 : M) := rfl
@[simp, norm_cast] lemma coe_add (m m' : N) : (↑(m + m') : M) = (m : M) + (m' : M) := rfl
@[simp, norm_cast] lemma coe_neg (m : N) : (↑(-m) : M) = -(m : M) := rfl
@[simp, norm_cast] lemma coe_sub (m m' : N) : (↑(m - m') : M) = (m : M) - (m' : M) := rfl
@[simp, norm_cast] lemma coe_smul (t : R) (m : N) : (↑(t • m) : M) = t • (m : M) := rfl
@[simp, norm_cast] lemma coe_bracket (x : L) (m : N) : (↑⁅x, m⁆ : M) = ⁅x, ↑m⁆ := rfl
end lie_submodule
section lie_ideal
variables (L)
/-- An ideal of a Lie algebra is a Lie submodule of the Lie algebra as a Lie module over itself. -/
abbreviation lie_ideal := lie_submodule R L L
lemma lie_mem_right (I : lie_ideal R L) (x y : L) (h : y ∈ I) : ⁅x, y⁆ ∈ I := I.lie_mem h
lemma lie_mem_left (I : lie_ideal R L) (x y : L) (h : x ∈ I) : ⁅x, y⁆ ∈ I :=
by { rw [←lie_skew, ←neg_lie], apply lie_mem_right, assumption, }
/-- An ideal of a Lie algebra is a Lie subalgebra. -/
def lie_ideal_subalgebra (I : lie_ideal R L) : lie_subalgebra R L :=
{ lie_mem' := by { intros x y hx hy, apply lie_mem_right, exact hy, },
..I.to_submodule, }
instance : has_coe (lie_ideal R L) (lie_subalgebra R L) := ⟨λ I, lie_ideal_subalgebra R L I⟩
@[norm_cast] lemma lie_ideal.coe_to_subalgebra (I : lie_ideal R L) :
((I : lie_subalgebra R L) : set L) = I := rfl
@[norm_cast] lemma lie_ideal.coe_to_lie_subalgebra_to_submodule (I : lie_ideal R L) :
((I : lie_subalgebra R L) : submodule R L) = I := rfl
end lie_ideal
variables {R M}
lemma submodule.exists_lie_submodule_coe_eq_iff (p : submodule R M) :
(∃ (N : lie_submodule R L M), ↑N = p) ↔ ∀ (x : L) (m : M), m ∈ p → ⁅x, m⁆ ∈ p :=
begin
split,
{ rintros ⟨N, rfl⟩, exact N.lie_mem, },
{ intros h, use { lie_mem := h, ..p }, exact lie_submodule.coe_to_submodule_mk p _, },
end
namespace lie_subalgebra
variables {L}
/-- Given a Lie subalgebra `K ⊆ L`, if we view `L` as a `K`-module by restriction, it contains
a distinguished Lie submodule for the action of `K`, namely `K` itself. -/
def to_lie_submodule (K : lie_subalgebra R L) : lie_submodule R K L :=
{ lie_mem := λ x y hy, K.lie_mem x.property hy,
.. (K : submodule R L) }
@[simp] lemma coe_to_lie_submodule (K : lie_subalgebra R L) :
(K.to_lie_submodule : submodule R L) = K :=
rfl
@[simp] lemma mem_to_lie_submodule {K : lie_subalgebra R L} (x : L) :
x ∈ K.to_lie_submodule ↔ x ∈ K :=
iff.rfl
lemma exists_lie_ideal_coe_eq_iff (K : lie_subalgebra R L) :
(∃ (I : lie_ideal R L), ↑I = K) ↔ ∀ (x y : L), y ∈ K → ⁅x, y⁆ ∈ K :=
begin
simp only [← coe_to_submodule_eq_iff, lie_ideal.coe_to_lie_subalgebra_to_submodule,
submodule.exists_lie_submodule_coe_eq_iff L],
exact iff.rfl,
end
lemma exists_nested_lie_ideal_coe_eq_iff {K K' : lie_subalgebra R L} (h : K ≤ K') :
(∃ (I : lie_ideal R K'), ↑I = of_le h) ↔ ∀ (x y : L), x ∈ K' → y ∈ K → ⁅x, y⁆ ∈ K :=
begin
simp only [exists_lie_ideal_coe_eq_iff, coe_bracket, mem_of_le],
split,
{ intros h' x y hx hy, exact h' ⟨x, hx⟩ ⟨y, h hy⟩ hy, },
{ rintros h' ⟨x, hx⟩ ⟨y, hy⟩ hy', exact h' x y hx hy', },
end
end lie_subalgebra
end lie_submodule
namespace lie_submodule
variables {R : Type u} {L : Type v} {M : Type w}
variables [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M]
variables [lie_ring_module L M] [lie_module R L M]
variables (N N' : lie_submodule R L M) (I J : lie_ideal R L)
section lattice_structure
open set
lemma coe_injective : function.injective (coe : lie_submodule R L M → set M) :=
λ N N' h, by { cases N, cases N', simp only, exact h, }
lemma coe_submodule_injective : function.injective (coe : lie_submodule R L M → submodule R M) :=
λ N N' h, by { ext, rw [← mem_coe_submodule, h], refl, }
instance : partial_order (lie_submodule R L M) :=
{ le := λ N N', ∀ ⦃x⦄, x ∈ N → x ∈ N', -- Overriding `le` like this gives a better defeq.
..partial_order.lift (coe : lie_submodule R L M → set M) coe_injective }
lemma le_def : N ≤ N' ↔ (N : set M) ⊆ N' := iff.rfl
@[simp, norm_cast] lemma coe_submodule_le_coe_submodule : (N : submodule R M) ≤ N' ↔ N ≤ N' :=
iff.rfl
instance : has_bot (lie_submodule R L M) := ⟨0⟩
@[simp] lemma bot_coe : ((⊥ : lie_submodule R L M) : set M) = {0} := rfl
@[simp] lemma bot_coe_submodule : ((⊥ : lie_submodule R L M) : submodule R M) = ⊥ := rfl
@[simp] lemma mem_bot (x : M) : x ∈ (⊥ : lie_submodule R L M) ↔ x = 0 := mem_singleton_iff
instance : has_top (lie_submodule R L M) :=
⟨{ lie_mem := λ x m h, mem_univ ⁅x, m⁆,
..(⊤ : submodule R M) }⟩
@[simp] lemma top_coe : ((⊤ : lie_submodule R L M) : set M) = univ := rfl
@[simp] lemma top_coe_submodule : ((⊤ : lie_submodule R L M) : submodule R M) = ⊤ := rfl
@[simp] lemma mem_top (x : M) : x ∈ (⊤ : lie_submodule R L M) := mem_univ x
instance : has_inf (lie_submodule R L M) :=
⟨λ N N', { lie_mem := λ x m h, mem_inter (N.lie_mem h.1) (N'.lie_mem h.2),
..(N ⊓ N' : submodule R M) }⟩
instance : has_Inf (lie_submodule R L M) :=
⟨λ S, { lie_mem := λ x m h, by
{ simp only [submodule.mem_carrier, mem_Inter, submodule.Inf_coe, mem_set_of_eq,
forall_apply_eq_imp_iff₂, exists_imp_distrib] at *,
intros N hN, apply N.lie_mem (h N hN), },
..Inf {(s : submodule R M) | s ∈ S} }⟩
@[simp] theorem inf_coe : (↑(N ⊓ N') : set M) = N ∩ N' := rfl
@[simp] lemma Inf_coe_to_submodule (S : set (lie_submodule R L M)) :
(↑(Inf S) : submodule R M) = Inf {(s : submodule R M) | s ∈ S} := rfl
@[simp] lemma Inf_coe (S : set (lie_submodule R L M)) : (↑(Inf S) : set M) = ⋂ s ∈ S, (s : set M) :=
begin
rw [← lie_submodule.coe_to_submodule, Inf_coe_to_submodule, submodule.Inf_coe],
ext m,
simpa only [mem_Inter, mem_set_of_eq, forall_apply_eq_imp_iff₂, exists_imp_distrib],
end
lemma Inf_glb (S : set (lie_submodule R L M)) : is_glb S (Inf S) :=
begin
have h : ∀ (N N' : lie_submodule R L M), (N : set M) ≤ N' ↔ N ≤ N', { intros, apply iff.rfl, },
simp only [is_glb.of_image h, Inf_coe, is_glb_binfi],
end
/-- The set of Lie submodules of a Lie module form a complete lattice.
We provide explicit values for the fields `bot`, `top`, `inf` to get more convenient definitions
than we would otherwise obtain from `complete_lattice_of_Inf`. -/
instance : complete_lattice (lie_submodule R L M) :=
{ bot := ⊥,
bot_le := λ N _ h, by { rw mem_bot at h, rw h, exact N.zero_mem', },
top := ⊤,
le_top := λ _ _ _, trivial,
inf := (⊓),
le_inf := λ N₁ N₂ N₃ h₁₂ h₁₃ m hm, ⟨h₁₂ hm, h₁₃ hm⟩,
inf_le_left := λ _ _ _, and.left,
inf_le_right := λ _ _ _, and.right,
..complete_lattice_of_Inf _ Inf_glb }
instance : add_comm_monoid (lie_submodule R L M) :=
{ add := (⊔),
add_assoc := λ _ _ _, sup_assoc,
zero := ⊥,
zero_add := λ _, bot_sup_eq,
add_zero := λ _, sup_bot_eq,
add_comm := λ _ _, sup_comm, }
@[simp] lemma add_eq_sup : N + N' = N ⊔ N' := rfl
@[norm_cast, simp] lemma sup_coe_to_submodule :
(↑(N ⊔ N') : submodule R M) = (N : submodule R M) ⊔ (N' : submodule R M) :=
begin
have aux : ∀ (x : L) m, m ∈ (N ⊔ N' : submodule R M) → ⁅x,m⁆ ∈ (N ⊔ N' : submodule R M),
{ simp only [submodule.mem_sup],
rintro x m ⟨y, hy, z, hz, rfl⟩,
refine ⟨⁅x, y⁆, N.lie_mem hy, ⁅x, z⁆, N'.lie_mem hz, (lie_add _ _ _).symm⟩ },
refine le_antisymm (Inf_le ⟨{ lie_mem := aux, ..(N ⊔ N' : submodule R M) }, _⟩) _,
{ simp only [exists_prop, and_true, mem_set_of_eq, eq_self_iff_true, coe_to_submodule_mk,
← coe_submodule_le_coe_submodule, and_self, le_sup_left, le_sup_right] },
{ simp, },
end
@[norm_cast, simp] lemma inf_coe_to_submodule :
(↑(N ⊓ N') : submodule R M) = (N : submodule R M) ⊓ (N' : submodule R M) := rfl
@[simp] lemma mem_inf (x : M) : x ∈ N ⊓ N' ↔ x ∈ N ∧ x ∈ N' :=
by rw [← mem_coe_submodule, ← mem_coe_submodule, ← mem_coe_submodule, inf_coe_to_submodule,
submodule.mem_inf]
lemma mem_sup (x : M) : x ∈ N ⊔ N' ↔ ∃ (y ∈ N) (z ∈ N'), y + z = x :=
by { rw [← mem_coe_submodule, sup_coe_to_submodule, submodule.mem_sup], exact iff.rfl, }
lemma eq_bot_iff : N = ⊥ ↔ ∀ (m : M), m ∈ N → m = 0 :=
by { rw eq_bot_iff, exact iff.rfl, }
-- TODO[gh-6025]: make this an instance once safe to do so
lemma subsingleton_of_bot : subsingleton (lie_submodule R L ↥(⊥ : lie_submodule R L M)) :=
begin
apply subsingleton_of_bot_eq_top,
ext ⟨x, hx⟩, change x ∈ ⊥ at hx, rw submodule.mem_bot at hx, subst hx,
simp only [true_iff, eq_self_iff_true, submodule.mk_eq_zero, lie_submodule.mem_bot],
end
instance : is_modular_lattice (lie_submodule R L M) :=
{ sup_inf_le_assoc_of_le := λ N₁ N₂ N₃,
by { simp only [← coe_submodule_le_coe_submodule, sup_coe_to_submodule, inf_coe_to_submodule],
exact is_modular_lattice.sup_inf_le_assoc_of_le ↑N₂, }, }
variables (R L M)
lemma well_founded_of_noetherian [is_noetherian R M] :
well_founded ((>) : lie_submodule R L M → lie_submodule R L M → Prop) :=
begin
let f : ((>) : lie_submodule R L M → lie_submodule R L M → Prop) →r
((>) : submodule R M → submodule R M → Prop) :=
{ to_fun := coe,
map_rel' := λ N N' h, h, },
apply f.well_founded, rw ← is_noetherian_iff_well_founded, apply_instance,
end
lemma subsingleton_iff : subsingleton M ↔ subsingleton (lie_submodule R L M) :=
(submodule.subsingleton_iff R).trans $ by
rw [← subsingleton_iff_bot_eq_top, ← subsingleton_iff_bot_eq_top, ← coe_to_submodule_eq_iff,
top_coe_submodule, bot_coe_submodule]
lemma nontrivial_iff : nontrivial M ↔ nontrivial (lie_submodule R L M) :=
not_iff_not.mp (
(not_nontrivial_iff_subsingleton.trans $ subsingleton_iff R L M).trans
not_nontrivial_iff_subsingleton.symm)
instance [nontrivial M] : nontrivial (lie_submodule R L M) := (nontrivial_iff R L M).mp ‹_›
variables {R L M}
section inclusion_maps
/-- The inclusion of a Lie submodule into its ambient space is a morphism of Lie modules. -/
def incl : N →ₗ⁅R,L⁆ M :=
{ map_lie' := λ x m, rfl,
..submodule.subtype (N : submodule R M) }
@[simp] lemma incl_apply (m : N) : N.incl m = m := rfl
lemma incl_eq_val : (N.incl : N → M) = subtype.val := rfl
variables {N N'} (h : N ≤ N')
/-- Given two nested Lie submodules `N ⊆ N'`, the inclusion `N ↪ N'` is a morphism of Lie modules.-/
def hom_of_le : N →ₗ⁅R,L⁆ N' :=
{ map_lie' := λ x m, rfl,
..submodule.of_le h }
@[simp] lemma coe_hom_of_le (m : N) : (hom_of_le h m : M) = m := rfl
lemma hom_of_le_apply (m : N) : hom_of_le h m = ⟨m.1, h m.2⟩ := rfl
lemma hom_of_le_injective : function.injective (hom_of_le h) :=
λ x y, by simp only [hom_of_le_apply, imp_self, subtype.mk_eq_mk, set_like.coe_eq_coe,
subtype.val_eq_coe]
end inclusion_maps
section lie_span
variables (R L) (s : set M)
/-- The `lie_span` of a set `s ⊆ M` is the smallest Lie submodule of `M` that contains `s`. -/
def lie_span : lie_submodule R L M := Inf {N | s ⊆ N}
variables {R L s}
lemma mem_lie_span {x : M} : x ∈ lie_span R L s ↔ ∀ N : lie_submodule R L M, s ⊆ N → x ∈ N :=
by { change x ∈ (lie_span R L s : set M) ↔ _, erw Inf_coe, exact mem_bInter_iff, }
lemma subset_lie_span : s ⊆ lie_span R L s :=
by { intros m hm, erw mem_lie_span, intros N hN, exact hN hm, }
lemma submodule_span_le_lie_span : submodule.span R s ≤ lie_span R L s :=
by { rw submodule.span_le, apply subset_lie_span, }
lemma lie_span_le {N} : lie_span R L s ≤ N ↔ s ⊆ N :=
begin
split,
{ exact subset.trans subset_lie_span, },
{ intros hs m hm, rw mem_lie_span at hm, exact hm _ hs, },
end
lemma lie_span_mono {t : set M} (h : s ⊆ t) : lie_span R L s ≤ lie_span R L t :=
by { rw lie_span_le, exact subset.trans h subset_lie_span, }
lemma lie_span_eq : lie_span R L (N : set M) = N :=
le_antisymm (lie_span_le.mpr rfl.subset) subset_lie_span
lemma coe_lie_span_submodule_eq_iff {p : submodule R M} :
(lie_span R L (p : set M) : submodule R M) = p ↔ ∃ (N : lie_submodule R L M), ↑N = p :=
begin
rw p.exists_lie_submodule_coe_eq_iff L, split; intros h,
{ intros x m hm, rw [← h, mem_coe_submodule], exact lie_mem _ (subset_lie_span hm), },
{ rw [← coe_to_submodule_mk p h, coe_to_submodule, coe_to_submodule_eq_iff, lie_span_eq], },
end
variables (R L M)
/-- `lie_span` forms a Galois insertion with the coercion from `lie_submodule` to `set`. -/
protected def gi : galois_insertion (lie_span R L : set M → lie_submodule R L M) coe :=
{ choice := λ s _, lie_span R L s,
gc := λ s t, lie_span_le,
le_l_u := λ s, subset_lie_span,
choice_eq := λ s h, rfl }
@[simp] lemma span_empty : lie_span R L (∅ : set M) = ⊥ :=
(lie_submodule.gi R L M).gc.l_bot
@[simp] lemma span_univ : lie_span R L (set.univ : set M) = ⊤ :=
eq_top_iff.2 $ set_like.le_def.2 $ subset_lie_span
variables {M}
lemma span_union (s t : set M) : lie_span R L (s ∪ t) = lie_span R L s ⊔ lie_span R L t :=
(lie_submodule.gi R L M).gc.l_sup
lemma span_Union {ι} (s : ι → set M) : lie_span R L (⋃ i, s i) = ⨆ i, lie_span R L (s i) :=
(lie_submodule.gi R L M).gc.l_supr
end lie_span
end lattice_structure
end lie_submodule
section lie_submodule_map_and_comap
variables {R : Type u} {L : Type v} {L' : Type w₂} {M : Type w} {M' : Type w₁}
variables [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L']
variables [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M]
variables [add_comm_group M'] [module R M'] [lie_ring_module L M'] [lie_module R L M']
namespace lie_submodule
variables (f : M →ₗ⁅R,L⁆ M') (N N₂ : lie_submodule R L M) (N' : lie_submodule R L M')
/-- A morphism of Lie modules `f : M → M'` pushes forward Lie submodules of `M` to Lie submodules
of `M'`. -/
def map : lie_submodule R L M' :=
{ lie_mem := λ x m' h, by
{ rcases h with ⟨m, hm, hfm⟩, use ⁅x, m⁆, split,
{ apply N.lie_mem hm, },
{ norm_cast at hfm, simp [hfm], }, },
..(N : submodule R M).map (f : M →ₗ[R] M') }
/-- A morphism of Lie modules `f : M → M'` pulls back Lie submodules of `M'` to Lie submodules of
`M`. -/
def comap : lie_submodule R L M :=
{ lie_mem := λ x m h, by { suffices : ⁅x, f m⁆ ∈ N', { simp [this], }, apply N'.lie_mem h, },
..(N' : submodule R M').comap (f : M →ₗ[R] M') }
variables {f N N₂ N'}
lemma map_le_iff_le_comap : map f N ≤ N' ↔ N ≤ comap f N' :=
set.image_subset_iff
variables (f)
lemma gc_map_comap : galois_connection (map f) (comap f) :=
λ N N', map_le_iff_le_comap
variables {f}
@[simp] lemma map_sup : (N ⊔ N₂).map f = N.map f ⊔ N₂.map f :=
(gc_map_comap f).l_sup
lemma mem_map (m' : M') : m' ∈ N.map f ↔ ∃ m, m ∈ N ∧ f m = m' :=
submodule.mem_map
@[simp] lemma mem_comap {m : M} : m ∈ comap f N' ↔ f m ∈ N' := iff.rfl
end lie_submodule
namespace lie_ideal
variables (f : L →ₗ⁅R⁆ L') (I I₂ : lie_ideal R L) (J : lie_ideal R L')
@[simp] lemma top_coe_lie_subalgebra : ((⊤ : lie_ideal R L) : lie_subalgebra R L) = ⊤ := rfl
/-- A morphism of Lie algebras `f : L → L'` pushes forward Lie ideals of `L` to Lie ideals of `L'`.
Note that unlike `lie_submodule.map`, we must take the `lie_span` of the image. Mathematically
this is because although `f` makes `L'` into a Lie module over `L`, in general the `L` submodules of
`L'` are not the same as the ideals of `L'`. -/
def map : lie_ideal R L' := lie_submodule.lie_span R L' $ (I : submodule R L).map (f : L →ₗ[R] L')
/-- A morphism of Lie algebras `f : L → L'` pulls back Lie ideals of `L'` to Lie ideals of `L`.
Note that `f` makes `L'` into a Lie module over `L` (turning `f` into a morphism of Lie modules)
and so this is a special case of `lie_submodule.comap` but we do not exploit this fact. -/
def comap : lie_ideal R L :=
{ lie_mem := λ x y h, by { suffices : ⁅f x, f y⁆ ∈ J, { simp [this], }, apply J.lie_mem h, },
..(J : submodule R L').comap (f : L →ₗ[R] L') }
@[simp] lemma map_coe_submodule (h : ↑(map f I) = f '' I) :
(map f I : submodule R L') = (I : submodule R L).map f :=
by { rw [set_like.ext'_iff, lie_submodule.coe_to_submodule, h, submodule.map_coe], refl, }
@[simp] lemma comap_coe_submodule : (comap f J : submodule R L) = (J : submodule R L').comap f :=
rfl
lemma map_le : map f I ≤ J ↔ f '' I ⊆ J := lie_submodule.lie_span_le
variables {f I I₂ J}
lemma mem_map {x : L} (hx : x ∈ I) : f x ∈ map f I :=
by { apply lie_submodule.subset_lie_span, use x, exact ⟨hx, rfl⟩, }
@[simp] lemma mem_comap {x : L} : x ∈ comap f J ↔ f x ∈ J := iff.rfl
lemma map_le_iff_le_comap : map f I ≤ J ↔ I ≤ comap f J :=
by { rw map_le, exact set.image_subset_iff, }
variables (f)
lemma gc_map_comap : galois_connection (map f) (comap f) :=
λ I I', map_le_iff_le_comap
variables {f}
@[simp] lemma map_sup : (I ⊔ I₂).map f = I.map f ⊔ I₂.map f :=
(gc_map_comap f).l_sup
lemma map_comap_le : map f (comap f J) ≤ J :=
by { rw map_le_iff_le_comap, apply le_refl _, }
/-- See also `lie_ideal.map_comap_eq`. -/
lemma comap_map_le : I ≤ comap f (map f I) :=
by { rw ← map_le_iff_le_comap, apply le_refl _, }
@[mono] lemma map_mono : monotone (map f) :=
λ I₁ I₂ h,
by { rw lie_submodule.le_def at h, apply lie_submodule.lie_span_mono (set.image_subset ⇑f h), }
@[mono] lemma comap_mono : monotone (comap f) :=
λ J₁ J₂ h, by { rw lie_submodule.le_def at h ⊢, exact set.preimage_mono h, }
lemma map_of_image (h : f '' I = J) : I.map f = J :=
begin
apply le_antisymm,
{ erw [lie_submodule.lie_span_le, submodule.map_coe, h], },
{ rw [lie_submodule.le_def, ← h], exact lie_submodule.subset_lie_span, },
end
/-- Note that this is not a special case of `lie_submodule.subsingleton_of_bot`. Indeed, given
`I : lie_ideal R L`, in general the two lattices `lie_ideal R I` and `lie_submodule R L I` are
different (though the latter does naturally inject into the former).
In other words, in general, ideals of `I`, regarded as a Lie algebra in its own right, are not the
same as ideals of `L` contained in `I`. -/
-- TODO[gh-6025]: make this an instance once safe to do so
lemma subsingleton_of_bot : subsingleton (lie_ideal R ↥(⊥ : lie_ideal R L)) :=
begin
apply subsingleton_of_bot_eq_top,
ext ⟨x, hx⟩, change x ∈ ⊥ at hx, rw submodule.mem_bot at hx, subst hx,
simp only [true_iff, eq_self_iff_true, submodule.mk_eq_zero, lie_submodule.mem_bot],
end
end lie_ideal
namespace lie_hom
variables (f : L →ₗ⁅R⁆ L') (I : lie_ideal R L) (J : lie_ideal R L')
/-- The kernel of a morphism of Lie algebras, as an ideal in the domain. -/
def ker : lie_ideal R L := lie_ideal.comap f ⊥
/-- The range of a morphism of Lie algebras as an ideal in the codomain. -/
def ideal_range : lie_ideal R L' := lie_submodule.lie_span R L' f.range
lemma ideal_range_eq_lie_span_range :
f.ideal_range = lie_submodule.lie_span R L' f.range := rfl
lemma ideal_range_eq_map :
f.ideal_range = lie_ideal.map f ⊤ :=
by { ext, simp only [ideal_range, range_eq_map], refl }
/-- The condition that the image of a morphism of Lie algebras is an ideal. -/
def is_ideal_morphism : Prop := (f.ideal_range : lie_subalgebra R L') = f.range
@[simp] lemma is_ideal_morphism_def :
f.is_ideal_morphism ↔ (f.ideal_range : lie_subalgebra R L') = f.range := iff.rfl
lemma is_ideal_morphism_iff :
f.is_ideal_morphism ↔ ∀ (x : L') (y : L), ∃ (z : L), ⁅x, f y⁆ = f z :=
begin
simp only [is_ideal_morphism_def, ideal_range_eq_lie_span_range,
← lie_subalgebra.coe_to_submodule_eq_iff, ← f.range.coe_to_submodule,
lie_ideal.coe_to_lie_subalgebra_to_submodule, lie_submodule.coe_lie_span_submodule_eq_iff,
lie_subalgebra.mem_coe_submodule, mem_range, exists_imp_distrib,
submodule.exists_lie_submodule_coe_eq_iff],
split,
{ intros h x y, obtain ⟨z, hz⟩ := h x (f y) y rfl, use z, exact hz.symm, },
{ intros h x y z hz, obtain ⟨w, hw⟩ := h x z, use w, rw [← hw, hz], },
end
lemma range_subset_ideal_range : (f.range : set L') ⊆ f.ideal_range := lie_submodule.subset_lie_span
lemma map_le_ideal_range : I.map f ≤ f.ideal_range :=
begin
rw f.ideal_range_eq_map,
exact lie_ideal.map_mono le_top,
end
lemma ker_le_comap : f.ker ≤ J.comap f := lie_ideal.comap_mono bot_le
@[simp] lemma ker_coe_submodule : (ker f : submodule R L) = (f : L →ₗ[R] L').ker := rfl
@[simp] lemma mem_ker {x : L} : x ∈ ker f ↔ f x = 0 :=
show x ∈ (f.ker : submodule R L) ↔ _,
by simp only [ker_coe_submodule, linear_map.mem_ker, coe_to_linear_map]
lemma mem_ideal_range {x : L} : f x ∈ ideal_range f :=
begin
rw ideal_range_eq_map,
exact lie_ideal.mem_map (lie_submodule.mem_top x)
end
@[simp] lemma mem_ideal_range_iff (h : is_ideal_morphism f) {y : L'} :
y ∈ ideal_range f ↔ ∃ (x : L), f x = y :=
begin
rw f.is_ideal_morphism_def at h,
rw [← lie_submodule.mem_coe, ← lie_ideal.coe_to_subalgebra, h, f.range_coe, set.mem_range],
end
lemma le_ker_iff : I ≤ f.ker ↔ ∀ x, x ∈ I → f x = 0 :=
begin
split; intros h x hx,
{ specialize h hx, rw mem_ker at h, exact h, },
{ rw mem_ker, apply h x hx, },
end
lemma ker_eq_bot : f.ker = ⊥ ↔ function.injective f :=
by rw [← lie_submodule.coe_to_submodule_eq_iff, ker_coe_submodule, lie_submodule.bot_coe_submodule,
linear_map.ker_eq_bot, coe_to_linear_map]
@[simp] lemma range_coe_submodule : (f.range : submodule R L') = (f : L →ₗ[R] L').range := rfl
lemma range_eq_top : f.range = ⊤ ↔ function.surjective f :=
begin
rw [← lie_subalgebra.coe_to_submodule_eq_iff, range_coe_submodule,
lie_subalgebra.top_coe_submodule],
exact linear_map.range_eq_top,
end
@[simp] lemma ideal_range_eq_top_of_surjective (h : function.surjective f) : f.ideal_range = ⊤ :=
begin
rw ← f.range_eq_top at h,
rw [ideal_range_eq_lie_span_range, h, ← lie_subalgebra.coe_to_submodule,
← lie_submodule.coe_to_submodule_eq_iff, lie_submodule.top_coe_submodule,
lie_subalgebra.top_coe_submodule, lie_submodule.coe_lie_span_submodule_eq_iff],
use ⊤,
exact lie_submodule.top_coe_submodule,
end
lemma is_ideal_morphism_of_surjective (h : function.surjective f) : f.is_ideal_morphism :=
by rw [is_ideal_morphism_def, f.ideal_range_eq_top_of_surjective h, f.range_eq_top.mpr h,
lie_ideal.top_coe_lie_subalgebra]
end lie_hom
namespace lie_ideal
variables {f : L →ₗ⁅R⁆ L'} {I : lie_ideal R L} {J : lie_ideal R L'}
@[simp] lemma map_eq_bot_iff : I.map f = ⊥ ↔ I ≤ f.ker :=
by { rw ← le_bot_iff, exact lie_ideal.map_le_iff_le_comap }
lemma coe_map_of_surjective (h : function.surjective f) :
(I.map f : submodule R L') = (I : submodule R L).map f :=
begin
let J : lie_ideal R L' :=
{ lie_mem := λ x y hy,
begin
have hy' : ∃ (x : L), x ∈ I ∧ f x = y, { simpa [hy], },
obtain ⟨z₂, hz₂, rfl⟩ := hy',
obtain ⟨z₁, rfl⟩ := h x,
simp only [lie_hom.coe_to_linear_map, set_like.mem_coe, set.mem_image,
lie_submodule.mem_coe_submodule, submodule.mem_carrier, submodule.map_coe],
use ⁅z₁, z₂⁆,
exact ⟨I.lie_mem hz₂, f.map_lie z₁ z₂⟩,
end,
..(I : submodule R L).map (f : L →ₗ[R] L'), },
erw lie_submodule.coe_lie_span_submodule_eq_iff,
use J,
apply lie_submodule.coe_to_submodule_mk,
end
lemma mem_map_of_surjective {y : L'} (h₁ : function.surjective f) (h₂ : y ∈ I.map f) :
∃ (x : I), f x = y :=
begin
rw [← lie_submodule.mem_coe_submodule, coe_map_of_surjective h₁, submodule.mem_map] at h₂,
obtain ⟨x, hx, rfl⟩ := h₂,
use ⟨x, hx⟩,
refl,
end
lemma bot_of_map_eq_bot {I : lie_ideal R L} (h₁ : function.injective f) (h₂ : I.map f = ⊥) :
I = ⊥ :=
begin
rw ← f.ker_eq_bot at h₁, change comap f ⊥ = ⊥ at h₁,
rw [eq_bot_iff, map_le_iff_le_comap, h₁] at h₂,
rw eq_bot_iff, exact h₂,
end
/-- Given two nested Lie ideals `I₁ ⊆ I₂`, the inclusion `I₁ ↪ I₂` is a morphism of Lie algebras. -/
def hom_of_le {I₁ I₂ : lie_ideal R L} (h : I₁ ≤ I₂) : I₁ →ₗ⁅R⁆ I₂ :=
{ map_lie' := λ x y, rfl,
..submodule.of_le h, }
@[simp] lemma coe_hom_of_le {I₁ I₂ : lie_ideal R L} (h : I₁ ≤ I₂) (x : I₁) :
(hom_of_le h x : L) = x := rfl
lemma hom_of_le_apply {I₁ I₂ : lie_ideal R L} (h : I₁ ≤ I₂) (x : I₁) :
hom_of_le h x = ⟨x.1, h x.2⟩ := rfl
lemma hom_of_le_injective {I₁ I₂ : lie_ideal R L} (h : I₁ ≤ I₂) :
function.injective (hom_of_le h) :=
λ x y, by simp only [hom_of_le_apply, imp_self, subtype.mk_eq_mk, set_like.coe_eq_coe,
subtype.val_eq_coe]
@[simp] lemma map_sup_ker_eq_map : lie_ideal.map f (I ⊔ f.ker) = lie_ideal.map f I :=
begin
suffices : lie_ideal.map f (I ⊔ f.ker) ≤ lie_ideal.map f I,
{ exact le_antisymm this (lie_ideal.map_mono le_sup_left), },
apply lie_submodule.lie_span_mono,
rintros x ⟨y, hy₁, hy₂⟩, rw ← hy₂,
erw lie_submodule.mem_sup at hy₁, obtain ⟨z₁, hz₁, z₂, hz₂, hy⟩ := hy₁, rw ← hy,
rw [f.coe_to_linear_map, f.map_add, f.mem_ker.mp hz₂, add_zero], exact ⟨z₁, hz₁, rfl⟩,
end
@[simp] lemma map_comap_eq (h : f.is_ideal_morphism) : map f (comap f J) = f.ideal_range ⊓ J :=
begin
apply le_antisymm,
{ rw le_inf_iff, exact ⟨f.map_le_ideal_range _, map_comap_le⟩, },
{ rw f.is_ideal_morphism_def at h,
rw [lie_submodule.le_def, lie_submodule.inf_coe, ← coe_to_subalgebra, h],
rintros y ⟨⟨x, h₁⟩, h₂⟩, rw ← h₁ at h₂ ⊢, exact mem_map h₂, },
end
@[simp] lemma comap_map_eq (h : ↑(map f I) = f '' I) : comap f (map f I) = I ⊔ f.ker :=
by rw [← lie_submodule.coe_to_submodule_eq_iff, comap_coe_submodule, I.map_coe_submodule f h,
lie_submodule.sup_coe_to_submodule, f.ker_coe_submodule, linear_map.comap_map_eq]
variables (f I J)
/-- Regarding an ideal `I` as a subalgebra, the inclusion map into its ambient space is a morphism
of Lie algebras. -/
def incl : I →ₗ⁅R⁆ L := (I : lie_subalgebra R L).incl
@[simp] lemma incl_range : I.incl.range = I := (I : lie_subalgebra R L).incl_range
@[simp] lemma incl_apply (x : I) : I.incl x = x := rfl
@[simp] lemma incl_coe : (I.incl : I →ₗ[R] L) = (I : submodule R L).subtype := rfl
@[simp] lemma comap_incl_self : comap I.incl I = ⊤ :=
by { rw ← lie_submodule.coe_to_submodule_eq_iff, exact submodule.comap_subtype_self _, }
@[simp] lemma ker_incl : I.incl.ker = ⊥ :=
by rw [← lie_submodule.coe_to_submodule_eq_iff, I.incl.ker_coe_submodule,
lie_submodule.bot_coe_submodule, incl_coe, submodule.ker_subtype]
@[simp] lemma incl_ideal_range : I.incl.ideal_range = I :=
begin
rw [lie_hom.ideal_range_eq_lie_span_range, ← lie_subalgebra.coe_to_submodule,
← lie_submodule.coe_to_submodule_eq_iff, incl_range, coe_to_lie_subalgebra_to_submodule,
lie_submodule.coe_lie_span_submodule_eq_iff],
use I,
end
lemma incl_is_ideal_morphism : I.incl.is_ideal_morphism :=
begin
rw [I.incl.is_ideal_morphism_def, incl_ideal_range],
exact (I : lie_subalgebra R L).incl_range.symm,
end
end lie_ideal
end lie_submodule_map_and_comap
namespace lie_module_hom
variables {R : Type u} {L : Type v} {M : Type w} {N : Type w₁}
variables [comm_ring R] [lie_ring L] [lie_algebra R L]
variables [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M]
variables [add_comm_group N] [module R N] [lie_ring_module L N] [lie_module R L N]
variables (f : M →ₗ⁅R,L⁆ N)
/-- The range of a morphism of Lie modules `f : M → N` is a Lie submodule of `N`.
See Note [range copy pattern]. -/
def range : lie_submodule R L N :=
(lie_submodule.map f ⊤).copy (set.range f) set.image_univ.symm
@[simp] lemma coe_range : (f.range : set N) = set.range f := rfl
@[simp] lemma coe_submodule_range : (f.range : submodule R N) = (f : M →ₗ[R] N).range := rfl
@[simp] lemma mem_range (n : N) : n ∈ f.range ↔ ∃ m, f m = n :=
iff.rfl
lemma map_top : lie_submodule.map f ⊤ = f.range :=
by { ext, simp [lie_submodule.mem_map], }
end lie_module_hom
section top_equiv_self
variables {R : Type u} {L : Type v}
variables [comm_ring R] [lie_ring L] [lie_algebra R L]
/-- The natural equivalence between the 'top' Lie subalgebra and the enclosing Lie algebra. -/
def lie_subalgebra.top_equiv_self : (⊤ : lie_subalgebra R L) ≃ₗ⁅R⁆ L :=
{ inv_fun := λ x, ⟨x, set.mem_univ x⟩,
left_inv := λ x, by { ext, refl, },
right_inv := λ x, rfl,
..(⊤ : lie_subalgebra R L).incl, }
@[simp] lemma lie_subalgebra.top_equiv_self_apply (x : (⊤ : lie_subalgebra R L)) :
lie_subalgebra.top_equiv_self x = x := rfl
/-- The natural equivalence between the 'top' Lie ideal and the enclosing Lie algebra. -/
def lie_ideal.top_equiv_self : (⊤ : lie_ideal R L) ≃ₗ⁅R⁆ L :=
lie_subalgebra.top_equiv_self
@[simp] lemma lie_ideal.top_equiv_self_apply (x : (⊤ : lie_ideal R L)) :
lie_ideal.top_equiv_self x = x := rfl
end top_equiv_self
|
ee2fcb24502b45300ff7dc598493346dd253ae80 | 037dba89703a79cd4a4aec5e959818147f97635d | /src/2021/sets/sheet2.lean | 713be5519dcaa03b97e30536fd4436c49c70d557 | [] | no_license | ImperialCollegeLondon/M40001_lean | 3a6a09298da395ab51bc220a535035d45bbe919b | 62a76fa92654c855af2b2fc2bef8e60acd16ccec | refs/heads/master | 1,666,750,403,259 | 1,665,771,117,000 | 1,665,771,117,000 | 209,141,835 | 115 | 12 | null | 1,640,270,596,000 | 1,568,749,174,000 | Lean | UTF-8 | Lean | false | false | 1,985 | lean | /-
Copyright (c) 2021 Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author : Kevin Buzzard
-/
import tactic -- imports all the Lean tactics
/-!
# Sets in Lean, example sheet 2 : the empty set and the "universal set".
We know what the empty subset of `X` is, and the Lean notation for
it is `∅`, or, if you want to say which type we're the empty subset
of, it's `∅ : set X`.
At the other extreme, the subset of `X` containing all the terms of type `X`
is...well...mathematicians would just call it `X`, but `X` is a type, and
so if we want a set it's called `set.univ : set X`, or just `univ : set X` if
we have opened the `set` namespace. Let's do that now.
-/
open set
/-
## Important
`x ∈ ∅` is *by definition* equal to `false` and `x ∈ univ` is *by definition*
equal to `true`. You can use the `change` tactic to make these changes
if you like. But you don't have to.
## Tactics you will need
You've seen them already. `trivial` proves `⊢ true` and `exfalso`
changes `⊢ P` to `⊢ false`.
-/
-- set up variables
variables
(X : Type) -- Everything will be a subset of `X`
(A B C D E : set X) -- A,B,C,D,E are subsets of `X`
(x y z : X) -- x,y,z are elements of `X` or, more precisely, terms of type `X`
/-
If `x : X` then `x ∈ ∅` is *by definition* `false`, and `x ∈ univ` is
*by definition* `true`. So you can use the `change` tactic to change
between these things, for example if your goal is
```
⊢ x ∈ univ
```
then `change true` will change the goal to
```
⊢ true
```
and you can now prove this goal with `trivial`. However you can prove
it with `trivial` even without `change`ing it.
-/
open set
example : x ∈ (univ : set X) :=
begin
sorry
end
example : x ∈ (∅ : set X) → false :=
begin
sorry
end
example : ∀ x : X, x ∈ A → x ∈ (univ : set X) :=
begin
sorry
end
example : ∀ x : X, x ∈ (∅ : set X) → x ∈ A :=
begin
sorry
end
|
9a759bbb960ec19d945bebe43b412d98d42cc981 | 3dd1b66af77106badae6edb1c4dea91a146ead30 | /tests/lean/run/trick.lean | 20bac83ef91436ea99b3d6ed252bcfd6222bd1a4 | [
"Apache-2.0"
] | permissive | silky/lean | 79c20c15c93feef47bb659a2cc139b26f3614642 | df8b88dca2f8da1a422cb618cd476ef5be730546 | refs/heads/master | 1,610,737,587,697 | 1,406,574,534,000 | 1,406,574,534,000 | 22,362,176 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 804 | lean | import standard
using num
definition proj1 (x : num) (y : num) := x
definition One := 1
(*
local num = Const({"num", "num"})
local m = mk_metavar("m", num)
local proj1 = Const("proj1")
local zero = Const({"num", "zero"})
local one = Const("One")
local t1 = proj1(m, one)
local t2 = proj1(m, zero)
function test_unify(env, m, cs, num_s)
local ss = unify(env, cs, name_generator())
local n = 0
for s in ss do
print("solution: " .. tostring(s:instantiate(m)))
n = n + 1
end
if num_s ~= n then print("n: " .. n) end
assert(num_s == n)
end
local env = get_env()
print("===============")
test_unify(env, m, { mk_eq_cnstr(proj1(m, one), proj1(zero, zero)) }, 1)
print("===============")
test_unify(env, m, { mk_eq_cnstr(proj1(m, one), proj1(zero, m)) }, 1)
*)
|
d2113d868d74d7dffde29c15b7dd72e58dedb8d4 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/tactic/linarith/verification.lean | 8f6e0b1e5010c632a25de2c0adeb2713b8c03423 | [
"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 | 7,220 | lean | /-
Copyright (c) 2020 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis
-/
import tactic.linarith.elimination
import tactic.linarith.parsing
/-!
# Deriving a proof of false
`linarith` uses an untrusted oracle to produce a certificate of unsatisfiability.
It needs to do some proof reconstruction work to turn this into a proof term.
This file implements the reconstruction.
## Main declarations
The public facing declaration in this file is `prove_false_by_linarith`.
-/
namespace linarith
open ineq tactic native
/-! ### Auxiliary functions for assembling proofs -/
/--
`mul_expr n e` creates a `pexpr` representing `n*e`.
When elaborated, the coefficient will be a native numeral of the same type as `e`.
-/
meta def mul_expr (n : ℕ) (e : expr) : pexpr :=
if n = 1 then ``(%%e) else
``(%%(nat.to_pexpr n) * %%e)
private meta def add_exprs_aux : pexpr → list pexpr → pexpr
| p [] := p
| p [a] := ``(%%p + %%a)
| p (h::t) := add_exprs_aux ``(%%p + %%h) t
/--
`add_exprs l` creates a `pexpr` representing the sum of the elements of `l`, associated left.
If `l` is empty, it will be the `pexpr` 0. Otherwise, it does not include 0 in the sum.
-/
meta def add_exprs : list pexpr → pexpr
| [] := ``(0)
| (h::t) := add_exprs_aux h t
/--
If our goal is to add together two inequalities `t1 R1 0` and `t2 R2 0`,
`ineq_const_nm R1 R2` produces the strength of the inequality in the sum `R`,
along with the name of a lemma to apply in order to conclude `t1 + t2 R 0`.
-/
meta def ineq_const_nm : ineq → ineq → (name × ineq)
| eq eq := (``eq_of_eq_of_eq, eq)
| eq le := (``le_of_eq_of_le, le)
| eq lt := (``lt_of_eq_of_lt, lt)
| le eq := (``le_of_le_of_eq, le)
| le le := (`add_nonpos, le)
| le lt := (`add_lt_of_le_of_neg, lt)
| lt eq := (``lt_of_lt_of_eq, lt)
| lt le := (`add_lt_of_neg_of_le, lt)
| lt lt := (`left.add_neg, lt)
/--
`mk_lt_zero_pf_aux c pf npf coeff` assumes that `pf` is a proof of `t1 R1 0` and `npf` is a proof
of `t2 R2 0`. It uses `mk_single_comp_zero_pf` to prove `t1 + coeff*t2 R 0`, and returns `R`
along with this proof.
-/
meta def mk_lt_zero_pf_aux (c : ineq) (pf npf : expr) (coeff : ℕ) : tactic (ineq × expr) :=
do (iq, h') ← mk_single_comp_zero_pf coeff npf,
let (nm, niq) := ineq_const_nm c iq,
prod.mk niq <$> mk_app nm [pf, h']
/--
`mk_lt_zero_pf coeffs pfs` takes a list of proofs of the form `tᵢ Rᵢ 0`,
paired with coefficients `cᵢ`.
It produces a proof that `∑cᵢ * tᵢ R 0`, where `R` is as strong as possible.
-/
meta def mk_lt_zero_pf : list (expr × ℕ) → tactic expr
| [] := fail "no linear hypotheses found"
| [(h, c)] := prod.snd <$> mk_single_comp_zero_pf c h
| ((h, c)::t) :=
do (iq, h') ← mk_single_comp_zero_pf c h,
prod.snd <$> t.mfoldl (λ pr ce, mk_lt_zero_pf_aux pr.1 pr.2 ce.1 ce.2) (iq, h')
/-- If `prf` is a proof of `t R s`, `term_of_ineq_prf prf` returns `t`. -/
meta def term_of_ineq_prf (prf : expr) : tactic expr :=
prod.fst <$> (infer_type prf >>= get_rel_sides)
/-- If `prf` is a proof of `t R s`, `ineq_prf_tp prf` returns the type of `t`. -/
meta def ineq_prf_tp (prf : expr) : tactic expr :=
term_of_ineq_prf prf >>= infer_type
/--
`mk_neg_one_lt_zero_pf tp` returns a proof of `-1 < 0`,
where the numerals are natively of type `tp`.
-/
meta def mk_neg_one_lt_zero_pf (tp : expr) : tactic expr :=
do h ← mk_mapp `linarith.zero_lt_one [tp, none, none],
mk_app `neg_neg_of_pos [h]
/--
If `e` is a proof that `t = 0`, `mk_neg_eq_zero_pf e` returns a proof that `-t = 0`.
-/
meta def mk_neg_eq_zero_pf (e : expr) : tactic expr :=
to_expr ``(neg_eq_zero.mpr %%e)
/--
`prove_eq_zero_using tac e` tries to use `tac` to construct a proof of `e = 0`.
-/
meta def prove_eq_zero_using (tac : tactic unit) (e : expr) : tactic expr :=
do tgt ← to_expr ``(%%e = 0),
prod.snd <$> solve_aux tgt (tac >> done)
/--
`add_neg_eq_pfs l` inspects the list of proofs `l` for proofs of the form `t = 0`. For each such
proof, it adds a proof of `-t = 0` to the list.
-/
meta def add_neg_eq_pfs : list expr → tactic (list expr)
| [] := return []
| (h::t) :=
do some (iq, tp) ← parse_into_comp_and_expr <$> infer_type h,
match iq with
| ineq.eq := do nep ← mk_neg_eq_zero_pf h, tl ← add_neg_eq_pfs t, return $ h::nep::tl
| _ := list.cons h <$> add_neg_eq_pfs t
end
/-! #### The main method -/
/--
`prove_false_by_linarith` is the main workhorse of `linarith`.
Given a list `l` of proofs of `tᵢ Rᵢ 0`,
it tries to derive a contradiction from `l` and use this to produce a proof of `false`.
An oracle is used to search for a certificate of unsatisfiability.
In the current implementation, this is the Fourier Motzkin elimination routine in
`elimination.lean`, but other oracles could easily be swapped in.
The returned certificate is a map `m` from hypothesis indices to natural number coefficients.
If our set of hypotheses has the form `{tᵢ Rᵢ 0}`,
then the elimination process should have guaranteed that
1.\ `∑ (m i)*tᵢ = 0`,
with at least one `i` such that `m i > 0` and `Rᵢ` is `<`.
We have also that
2.\ `∑ (m i)*tᵢ < 0`,
since for each `i`, `(m i)*tᵢ ≤ 0` and at least one is strictly negative.
So we conclude a contradiction `0 < 0`.
It remains to produce proofs of (1) and (2). (1) is verified by calling the `discharger` tactic
of the `linarith_config` object, which is typically `ring`. We prove (2) by folding over the
set of hypotheses.
-/
meta def prove_false_by_linarith (cfg : linarith_config) : list expr → tactic expr
| [] := fail "no args to linarith"
| l@(h::t) := do
-- for the elimination to work properly, we must add a proof of `-1 < 0` to the list,
-- along with negated equality proofs.
l' ← add_neg_eq_pfs l,
hz ← ineq_prf_tp h >>= mk_neg_one_lt_zero_pf,
let inputs := hz::l',
-- perform the elimination and fail if no contradiction is found.
(comps, max_var) ← linear_forms_and_max_var cfg.transparency inputs,
certificate ← cfg.oracle.get_or_else fourier_motzkin.produce_certificate comps max_var
<|> fail "linarith failed to find a contradiction",
linarith_trace "linarith has found a contradiction",
let enum_inputs := inputs.enum,
-- construct a list pairing nonzero coeffs with the proof of their corresponding comparison
let zip := enum_inputs.filter_map $ λ ⟨n, e⟩, prod.mk e <$> certificate.find n,
mls ← zip.mmap (λ ⟨e, n⟩, do e ← term_of_ineq_prf e, return (mul_expr n e)),
-- `sm` is the sum of input terms, scaled to cancel out all variables.
sm ← to_expr $ add_exprs mls,
pformat! "The expression\n {sm}\nshould be both 0 and negative" >>= linarith_trace,
-- we prove that `sm = 0`, typically with `ring`.
sm_eq_zero ← prove_eq_zero_using cfg.discharger sm,
linarith_trace "We have proved that it is zero",
-- we also prove that `sm < 0`.
sm_lt_zero ← mk_lt_zero_pf zip,
linarith_trace "We have proved that it is negative",
-- this is a contradiction.
pftp ← infer_type sm_lt_zero,
(_, nep, _) ← rewrite_core sm_eq_zero pftp,
pf' ← mk_eq_mp nep sm_lt_zero,
mk_app `lt_irrefl [pf']
end linarith
|
3e4bdfcce98180436a16fd1dfd95a9389025bfe1 | ebf7140a9ea507409ff4c994124fa36e79b4ae35 | /src/solutions/friday/manifolds.lean | d07e3239b0e30f89236743e13b6cac9cebe42f9d | [] | no_license | fundou/lftcm2020 | 3e88d58a92755ea5dd49f19c36239c35286ecf5e | 99d11bf3bcd71ffeaef0250caa08ecc46e69b55b | refs/heads/master | 1,685,610,799,304 | 1,624,070,416,000 | 1,624,070,416,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 42,341 | lean | import ...for_mathlib.manifolds
noncomputable theory
open_locale manifold classical big_operators
open set
universe u
/-!
## Reminder on updating the exercises
These instructions are now available at:
https://leanprover-community.github.io/lftcm2020/exercises.html
To get a new copy of the exercises,
run the following commands in your terminal:
```
leanproject get lftcm2020
cp -r lftcm2020/src/exercises_sources/ lftcm2020/src/my_exercises
code lftcm2020
```
To update your exercise files, run the following commands:
```
cd /path/to/lftcm2020
git pull
leanproject get-mathlib-cache
```
Don’t forget to copy the updated files to `src/my_exercises`.
-/
/-!
## An overview of manifolds in Lean, discussing design decisions
Warning: there are sorries in this section, they are not supposed to be filled! The exercises sections
start later, and there you will have plenty of sorries to fill.
What is a manifold?
1) allow field other than `ℝ` or `ℂ`?
2) allow infinite dimension?
3) allow boundary?
4) allow model space depending on the point of the manifold?
Bourbaki: 2, 4 (and just definitions and statements, no proofs!)
Lean: 1, 2, 3
Perelman geometrization theorem : any compact connected irreducible 3-manifold can
be cut along tori into finitely many pieces, each of which has a _geometric structure_ of
finite volume, i.e., it is locally like a model space, with changes of coordinates given
locally by the action of a Lie group
Typical dynamics theorem : let `M` be a compact manifold, and `f : M → M` a map with property
such and such. Then ...
Or : Consider a hyperbolic surface of genus `g`, and a random geodesic of length `T`. How many
times does it typically self-intersect?
Manifold in Lean:
* charted space structure, i.e., set of local homeos to a model space. This is data, fixed
once and for all (and a typeclass)
* compatibility condition, i.e., the change of coordinates should belong to some subgroup
of the group of local homeos of the model space. This is Prop (and a typeclass). The same
manifold can be at the same time an analytic manifold, a smooth manifold and a topological
manifold (with the same fixed atlas).
* A charted space is a smooth manifold (with corners) if it is compatible with the smooth
groupoid on the model space. To cover uniformly both situations with and without boundary,
the smooth groupoid is with respect to a map `I : H → E` (think of `H` as the half-space and
`E` the full space), which is the identity in the boundaryless situation, the inclusion in
the half-space situation. This map `I` is called a _model with corners_. The most standard ones
(identity in `ℝ^n` and inclusion of half-space in `ℝ^n`) have dedicated notations:
`𝓡 n` and `𝓡∂ n`.
-/
#check charted_space (euclidean_half_space 1) (Icc (0 : ℝ) 1)
#check has_groupoid (Icc (0 : ℝ) 1) (times_cont_diff_groupoid ∞ (𝓡∂ 1))
#check smooth_manifold_with_corners (𝓡∂ 1) (Icc (0 : ℝ) 1)
-- atlases are not maximal in general
#check (times_cont_diff_groupoid ∞ (𝓡∂ 1)).maximal_atlas (Icc (0 : ℝ) 1)
-- let's try to put a smooth manifold structure on the sphere
-- (we don't have submanifolds yet, but it's coming in the near future)
@[derive topological_space]
definition sphere (n : ℕ) : Type := metric.sphere (0 : euclidean_space ℝ (fin (n+1))) 1
instance (n : ℕ) : has_coe (sphere n) (euclidean_space ℝ (fin (n+1))) := ⟨subtype.val⟩
instance (n : ℕ) : charted_space (euclidean_space ℝ (fin n)) (sphere n) :=
{ atlas := begin sorry end,
chart_at := begin sorry end,
mem_chart_source := begin sorry end,
chart_mem_atlas := begin sorry end }
instance (n : ℕ) : smooth_manifold_with_corners (𝓡 n) (sphere n) :=
{ compatible := begin
assume e e' he he',
sorry
end }
-- smooth functions
def inc (n : ℕ) : sphere n → euclidean_space ℝ (fin (n+1)) :=
λ p : sphere n, (p : euclidean_space ℝ (fin (n+1)))
lemma inc_smooth (n : ℕ) : times_cont_mdiff (𝓡 n) (𝓡 (n+1)) ∞ (inc n) :=
begin
rw times_cont_mdiff_iff,
split,
{ exact continuous_subtype_coe, },
{ assume x y,
sorry }
end
lemma inc_continuous (n : ℕ) : continuous (inc n) :=
(inc_smooth n).continuous
lemma inc_mdifferentiable (n : ℕ) : mdifferentiable (𝓡 n) (𝓡 (n+1)) (inc n) :=
(inc_smooth n).mdifferentiable le_top
-- tangent space and tangent bundles
example (n : ℕ) (p : sphere n) (v : tangent_space (𝓡 n) p) :
tangent_bundle (𝓡 n) (sphere n) :=
⟨p, v⟩
-- tangent map, derivatives
example (n : ℕ) : times_cont_mdiff ((𝓡 n).prod (𝓡 n)) ((𝓡 (n+1)).prod (𝓡 (n+1))) ∞
(tangent_map (𝓡 n) (𝓡 (n+1)) (inc n)) :=
(inc_smooth n).times_cont_mdiff_tangent_map le_top
example (n : ℕ) (f : sphere n → sphere (n^2)) (p : sphere n) (v : tangent_space (𝓡 n) p) :
mfderiv (𝓡 n) (𝓡 (n^2)) f p v = (tangent_map (𝓡 n) (𝓡 (n^2)) f ⟨p, v⟩).2 :=
rfl
/- Can you express the sphere eversion theorem, i.e., the fact that there is a smooth isotopy
of immersions between the canonical embedding of the sphere `S^2` and `ℝ^3`, and the antipodal
embedding?
Note that we haven't defined immersions in mathlib, but you can jut require that the fiber
derivative is injective everywhere, which is easy to express if you know that the derivative
of a function `f` from a manifold of dimension `2` to a manifold of dimension `3` at a point `x` is
`mfderiv (𝓡 2) (𝓡 3) f x`.
Don't forget to require the global smoothness of the map! You may need to know that the interval
`[0,1]`, called `Icc (0 : ℝ) 1` in Lean, already has a manifold (with boundary!) structure,
where the corresponding model with corners is called `𝓡∂ 1`.
-/
theorem sphere_eversion :
∃ f : (Icc (0 : ℝ) 1) × sphere 2 → euclidean_space ℝ (fin 3),
times_cont_mdiff ((𝓡∂ 1).prod (𝓡 2)) (𝓡 3) ∞ f
∧ ∀ (t : (Icc (0 : ℝ) 1)), ∀ (p : sphere 2),
function.injective (mfderiv (𝓡 2) (𝓡 3) (f ∘ λ y, (t, y)) p)
∧ ∀ (p : sphere 2), f (0, p) = p
∧ ∀ (p : sphere 2), f (1, p) = - p :=
sorry
/- Dicussing three (controversial?) design decisions
#### Local homeos
What is a local homeo `f` between an open subset of `E` and an open subset of `F`?
1) a map defined on a subtype: `f x` only makes sense for `x : f.source`
2) a map defined on the whole space `E`, but taking values in `option F = F ∪ {junk}`, with
`f x = junk` when `x ∉ f.source`
3) a map defined on the whole space `E`, taking values in `F`, and we don't care about its values
outside of `f.source`.
Just like division by zero! But worse:
* issue with 1): you keep intersecting chart domains. But the subtype `u ∩ v` is not the same as
the subtype `v ∩ u`, so you keep adding casts everywhere
* issue with 2): if you want to say that a chart is smooth, then you define to define smooth functions
between `option E` and `option F` when `E` and `F` are vector spaces. All notions need to be
redefined with `option`.
* issue with 3): it works perfectly well, but it makes mathematicians unhappy/uneasy (and it is *not*
equivalent to 1) or 2) when one of the spaces is empty)
I picked 3)
#### Tangent vectors
What is a tangent vector (for a manifold `M` modelled on a vector space `E`)?
1) An equivalence class of germs of curves
2) A derivation
3) Physicist point of view: I don't know what a tangent vector is, but I know in charts.
Mathematician's interpretation: equivalence class of `(e, v)` where `e` is a chart at `x`, `v` a vector
in the vector space, and `(e, v) ∼ (e', v')` if `D(e' ∘ e ⁻¹) v = v'`
4) ...
Issues:
1) Pictures are pretty, but this doesn't bring anything compared to 3) when you go down to details.
And what about boundaries, where you can only have a half-curve
2) Need partitions of unity to show that this is local and coincides with the usual point of view.
Doesn't work well in finite smoothness, nor in complex manifolds
3) Fine, works in all situations, but requires a lot of work to define the equivalence classes,
the topology, check that the topology is compatible with the vector space structure, and so on.
In a vector space, the tangent space is not defeq to the vector space itself
4) Pick one favorite chart at `x`, say `e_x`, and *define* the tangent space at `x` to be `E`,
but "seen" in the chart `e_x` (this will show up in the definition of the derivative : the
derivative of `f : M → M'` at `x` is defined to be the derivative of the map
`e_{f x} ∘ f ∘ e_x⁻¹`). Works perfectly fine, but makes mathematicians unhappy/uneasy.
(Axiom of choice? In fact we put the choice of `e_x` in the *definition* of charted spaces,
so not further choice)
I picked 4)
#### Smooth functions in manifolds with boundary
Usual definition of smooth functions in a half space: extend to a smooth function a little bit
beyond the boundary, so one only really needs to speak of smooth functions in open subsets of
vector spaces.
When you define the derivative, you will need to check that it does not depend on the choice
of the extension. Even worse when you want to define the tangent bundle: choose an open extension
of your manifold with boundary, and then check that the restriction of the tangent bundle does
not depend on the choice of the extension. Very easy when handwaving, nightmare to formalize.
(What is the extension of the manifold with boundary? Another type?)
Instead, if you define derivatives in (non-open) domains, you can talk of smooth functions in
domains, and do everything without extending. Need to know this early enough: when starting to
define derivatives, you should already think of manifolds with boundaries! That's what we did
in mathlib.
Difficulty: if a domain `s` is too small (think `s = ℝ ⊆ ℝ^2`), the values of `f` on `s` do not
prescribe uniquely a derivative, so `fderiv_within_at ℝ f s x` may behave badly: the derivative of
a sum might be different from sum of derivatives, as there is an arbitrary choice to be made.
This does not happen with the half-space, as it is large enough: derivatives within domains only
work well if the tangent directions span the whole space. Predicate `unique_diff_on` for sets
in vector spaces. You won't find this in books!
-/
/-! ## Exercises -/
/-! ### Local homeomorphisms
Local homeomorphisms are globally defined maps with a globally defined "inverse", but the only
relevant set is the *source*, which should be mapped homeomorphically to the *target*.
-/
-- set up a simple helper simp lemma to simplify our life later.
@[simp] lemma neg_mem_Ioo_minus_one_one (x : ℝ) : -x ∈ Ioo (-1 : ℝ) 1 ↔ x ∈ Ioo (-1 : ℝ) 1 :=
begin
-- sorry
simp [neg_lt, and_comm],
-- sorry
end
/- Define a local homeomorphism from `ℝ` to `ℝ` which is just `x ↦ -x`, but on `(-1, 1)`. In
Lean, the interval `(-1, 1)` is denoted by `Ioo (-1 : ℝ) 1` (where `o` stands for _open_). -/
def my_first_local_homeo : local_homeomorph ℝ ℝ :=
{ to_fun := λ x, -x,
inv_fun := λ x, -x,
source := Ioo (-1) 1,
target := /- inline sorry -/Ioo (-1) 1/- inline sorry -/,
map_source' :=
begin
-- sorry
assume x hx,
simp [hx],
-- sorry
end,
map_target' :=
begin
-- sorry
assume x hx,
simp [hx],
-- sorry
end,
left_inv' :=
begin
-- sorry
simp,
-- sorry
end,
right_inv' :=
begin
-- sorry
simp,
-- sorry
end,
open_source := /- inline sorry -/is_open_Ioo/- inline sorry -/,
open_target := /- inline sorry -/is_open_Ioo/- inline sorry -/,
continuous_to_fun := /- inline sorry -/continuous_neg.continuous_on/- inline sorry -/,
continuous_inv_fun := /- inline sorry -/continuous_neg.continuous_on/- inline sorry -/ }
/- Two simple lemmas that will prove useful below. You can leave them sorried if you like. -/
lemma ne_3_of_mem_Ioo {x : ℝ} (h : x ∈ Ioo (-1 : ℝ) 1) : x ≠ 3 :=
begin
-- sorry
exact ne_of_lt (lt_trans h.2 (by norm_num))
-- sorry
end
lemma neg_ne_3_of_mem_Ioo {x : ℝ} (h : x ∈ Ioo (-1 : ℝ) 1) : -x ≠ 3 :=
begin
-- sorry
assume h',
simp at h,
linarith,
-- sorry
end
/- Now, define a second local homeomorphism which is almost like the previous one. You may find the
following lemma useful for `continuous_to_fun`: -/
#check continuous_on.congr
def my_second_local_homeo : local_homeomorph ℝ ℝ :=
{ to_fun := λ x, if x = 3 then 0 else - x,
inv_fun := λ x, -x,
source := Ioo (-1) 1,
target := /- inline sorry -/Ioo (-1) 1/- inline sorry -/,
map_source' := /- inline sorry -/λ x hx, by simp [hx, ne_3_of_mem_Ioo hx]/- inline sorry -/,
map_target' := /- inline sorry -/λ x hx, by simp [hx]/- inline sorry -/,
left_inv' := /- inline sorry -/λ x hx, by simp [hx, ne_3_of_mem_Ioo hx]/- inline sorry -/,
right_inv' := /- inline sorry -/λ x hx, by simp [hx, neg_ne_3_of_mem_Ioo hx]/- inline sorry -/,
open_source := /- inline sorry -/is_open_Ioo/- inline sorry -/,
open_target := /- inline sorry -/is_open_Ioo/- inline sorry -/,
continuous_to_fun :=
begin
-- sorry
refine continuous_neg.continuous_on.congr (λ x hx, _),
simp [hx, ne_3_of_mem_Ioo hx],
-- sorry
end,
continuous_inv_fun := /- inline sorry -/continuous_neg.continuous_on/- inline sorry -/ }
/- Although the two above local homeos are the same for all practical purposes as they coincide
where relevant, they are not *equal*: -/
lemma my_first_local_homeo_ne_my_second_local_homeo :
my_first_local_homeo ≠ my_second_local_homeo :=
begin
-- sorry
assume h,
have : my_first_local_homeo 3 = my_second_local_homeo 3, by rw h,
simp [my_first_local_homeo, my_second_local_homeo] at this,
linarith,
-- sorry
end
/- The right equivalence relation for local homeos is not equality, but `eq_on_source`.
Indeed, the two local homeos we have defined above coincide from this point of view. -/
#check local_homeomorph.eq_on_source
lemma eq_on_source_my_first_local_homeo_my_second_local_homeo :
local_homeomorph.eq_on_source my_first_local_homeo my_second_local_homeo :=
begin
-- sorry
refine ⟨rfl, λ x hx, _⟩,
simp [my_first_local_homeo, my_second_local_homeo, ne_3_of_mem_Ioo hx],
-- sorry
end
/-! ### An example of a charted space structure on `ℝ`
A charted space is a topological space together with a set of local homeomorphisms to a model space,
whose sources cover the whole space. For instance, `ℝ` is already endowed with a charted space
structure with model space `ℝ`, where the unique chart is the identity:
-/
#check charted_space_self ℝ
/- For educational purposes only, we will put another charted space structure on `ℝ` using the
local homeomorphisms we have constructed above. To avoid using too much structure of `ℝ` (and to
avoid confusing Lean), we will work with a copy of `ℝ`, on which we will only register the
topology. -/
@[derive topological_space]
def myℝ : Type := ℝ
instance : charted_space ℝ myℝ :=
{ atlas := { local_homeomorph.refl ℝ, my_first_local_homeo },
chart_at := λ x, if x ∈ Ioo (-1 : ℝ) 1 then my_first_local_homeo else local_homeomorph.refl ℝ,
mem_chart_source :=
begin
-- sorry
assume x,
split_ifs,
{ exact h },
{ exact mem_univ _ }
-- sorry
end,
chart_mem_atlas :=
begin
-- sorry
assume x,
split_ifs;
simp,
-- sorry
end }
/- Now come more interesting bits. We have endowed `myℝ` with a charted space structure, with charts
taking values in `ℝ`. We want to say that this is a smooth structure, i.e., the changes of
coordinates are smooth. In Lean, this is written with `has_groupoid`. A groupoid is a set
of local homeomorphisms of the model space (for example, local homeos that are smooth on their
domain). A charted space admits the groupoid as a structure groupoid if all the changes of
coordinates belong to the groupoid.
There is a difficulty that the definitions are set up to be able to also speak of smooth manifolds
with boundary or with corners, so the name of the smooth groupoid on `ℝ` has the slightly strange
name `times_cont_diff_groupoid ∞ (model_with_corners_self ℝ ℝ)`. To avoid typing again and again
`model_with_corners_self ℝ ℝ`, let us introduce a shortcut
-/
abbreviation 𝓡1 := model_with_corners_self ℝ ℝ
/- In the library, there are such shortcuts for manifolds modelled on `ℝ^n`, denoted with `𝓡 n`,
but for `n = 1` this does not coincide with the above one, as `ℝ^1` (a.k.a. `fin 1 → ℝ`) is not
the same as `ℝ`! Still, since they are of the same nature, the notation we have just introduced
is very close, compare `𝓡1` with `𝓡 1` (and try not to get confused): -/
instance smooth_myℝ : has_groupoid myℝ (times_cont_diff_groupoid ∞ 𝓡1) :=
begin
-- in theory, we should prove that all compositions of charts are diffeos, i.e., they are smooth
-- and their inverse are smooth. For symmetry reasons, it suffices to check one direction
apply has_groupoid_of_pregroupoid,
-- take two charts `e` and `e'`
assume e e' he he',
-- if next line is a little bit slow for your taste, you can replace `simp` with `squeeze_simp`
-- and then follow the advice
simp [atlas] at he he',
dsimp,
-- to continue, some hints:
-- (1) don't hesitate to use the fact that the restriction of a smooth function to a
-- subset is still smooth there (`times_cont_diff.times_cont_diff_on`)
-- (2) hopefully, there is a theorem saying that the negation function is smooth.
-- you can either try to guess its name, or hope that `suggest` will help you there.
-- sorry
rcases he with rfl|rfl; rcases he' with rfl|rfl,
{ exact times_cont_diff_id.times_cont_diff_on },
{ exact times_cont_diff_id.neg.times_cont_diff_on },
{ exact times_cont_diff_id.neg.times_cont_diff_on },
{ convert times_cont_diff_id.times_cont_diff_on,
ext x,
simp [my_first_local_homeo], },
-- sorry
end
/- The statement of the previous instance is not very readable. There is a shortcut notation: -/
instance : smooth_manifold_with_corners 𝓡1 myℝ := { .. smooth_myℝ }
/- We will now study a very simple map from `myℝ` to `ℝ`, the identity. -/
def my_map : myℝ → ℝ := λ x, x
/- The map `my_map` is a map going from the type `myℝ` to the type `ℝ`. From the point of view of
the kernel of Lean, it is just the identity, but from the point of view of structures on `myℝ`
and `ℝ` it might not be trivial, as we have registered different instances on these two types. -/
/- The continuity should be trivial, as the topologies on `myℝ` and `ℝ` are definitionally the
same. So `continuous_id` might help. -/
lemma continuous_my_map : continuous my_map :=
-- sorry
continuous_id
-- sorry
/- Smoothness should not be obvious, though, as the manifold structures are not the same: the atlas
on `myℝ` has two elements, while the atlas on `ℝ` has one single element.
Note that `myℝ` is not a vector space, nor a normed space, so one can not ask whether `my_map`
is smooth in the usual sense (as a map between vector spaces): -/
-- lemma times_cont_diff_my_map : times_cont_diff ℝ ∞ my_map := sorry
/- does not make sense (try uncommenting it!) However, we can ask whether `my_map` is a smooth
map between manifolds, i.e., whether it is smooth when read in the charts. When we mention the
smoothness of a map, we should always specify explicitly the model with corners we are using,
because there might be several around (think of a complex manifold that you may want to consider
as a real manifold, to talk about functions which are real-smooth but not holomorphic) -/
lemma times_cont_mdiff_my_map : times_cont_mdiff 𝓡1 𝓡1 ∞ my_map :=
begin
-- put things in a nicer form. The simpset `mfld_simps` registers many simplification rules for
-- manifolds. `simp` is used heavily in manifold files to bring everything into manageable form.
rw times_cont_mdiff_iff,
simp only [continuous_my_map] with mfld_simps,
-- simp has erased the chart in the target, as it knows that the only chart in the manifold `ℝ`
-- is the identity.
assume x y,
-- sorry
simp [my_map, (∘), chart_at],
split_ifs,
{ exact times_cont_diff_id.neg.times_cont_diff_on },
{ exact times_cont_diff_id.times_cont_diff_on },
-- sorry
end
/- Now, let's go to tangent bundles. We have a smooth manifold, so its tangent bundle should also
be a smooth manifold. -/
-- the type `tangent_bundle 𝓡1 myℝ` makes sense
#check tangent_bundle 𝓡1 myℝ
/- The tangent space above a point of `myℝ` is just a one-dimensional vector space (identified with `ℝ`).
So, one can prescribe an element of the tangent bundle as a pair (more on this below) -/
example : tangent_bundle 𝓡1 myℝ := ⟨(4 : ℝ), 0⟩
/- Construct the smooth manifold structure on the tangent bundle. Hint: the answer is a one-liner,
and this instance is not really needed. -/
instance tangent_bundle_myℝ : smooth_manifold_with_corners (𝓡1.prod 𝓡1) (tangent_bundle 𝓡1 myℝ) :=
-- sorry
by apply_instance
-- sorry
/-
NB: the model space for the tangent bundle to a product manifold or a tangent space is not
`ℝ × ℝ`, but a copy called `model_prod ℝ ℝ`. Otherwise, `ℝ × ℝ` would have two charted space
structures with model `ℝ × ℝ`, the identity one and the product one, which are not definitionally
equal. And this would be bad.
-/
#check tangent_bundle.charted_space 𝓡1 myℝ
/- A smooth map between manifolds induces a map between their tangent bundles. In `mathlib` this is
called the `tangent_map` (you might instead know it as the "differential" or "pushforward" of the
map). Let us check that the `tangent_map` of `my_map` is smooth. -/
lemma times_cont_mdiff_tangent_map_my_map :
times_cont_mdiff (𝓡1.prod 𝓡1) (𝓡1.prod 𝓡1) ∞ (tangent_map 𝓡1 𝓡1 my_map) :=
begin
-- hopefully, there is a theorem providing the general result, i.e. the tangent map to a smooth
-- map is smooth.
-- you can either try to guess its name, or hope that `suggest` will help you there.
-- sorry
exact times_cont_mdiff_my_map.times_cont_mdiff_tangent_map le_top,
-- sorry
end
/- (Harder question) Can you show that this tangent bundle is homeomorphic to `ℝ × ℝ`? You could
try to build the homeomorphism by hand, using `tangent_map 𝓡1 𝓡1 my_map` in one direction and a
similar map in the other direction, but it is probably more efficient to use one of the charts of
the tangent bundle.
Remember, the model space for `tangent_bundle 𝓡1 myℝ` is `model_prod ℝ ℝ`, not `ℝ × ℝ`. But the
topologies on `model_prod ℝ ℝ` and `ℝ × ℝ` are the same, so it is by definition good enough to
construct a homeomorphism with `model_prod ℝ ℝ`.
-/
def my_homeo : tangent_bundle 𝓡1 myℝ ≃ₜ (ℝ × ℝ) :=
begin
-- sorry
let p : tangent_bundle 𝓡1 myℝ := ⟨(4 : ℝ), 0⟩,
let F := chart_at (model_prod ℝ ℝ) p,
have A : ¬ ((4 : ℝ) < 1), by norm_num,
have S : F.source = univ, by simp [F, chart_at, A, @local_homeomorph.refl_source ℝ _],
have T : F.target = univ, by simp [F, chart_at, A, @local_homeomorph.refl_target ℝ _],
exact F.to_homeomorph_of_source_eq_univ_target_eq_univ S T,
-- sorry
end
/- Up to now, we have never used the definition of the tangent bundle, and this corresponds to
the usual mathematical practice: one doesn't care if the tangent space is defined using germs of
curves, or spaces of derivations, or whatever equivalent definition. Instead, one relies all the
time on functoriality (i.e., a smooth map has a well defined derivative, and they compose well,
together with the fact that the tangent bundle to a vector space is the product).
If you want to know more about the internals of the tangent bundle in mathlib, you can browse
through the next section, but it is maybe wiser to skip it on first reading, as it is not needed
to use the library
-/
section you_should_probably_skip_this
/- If `M` is a manifold modelled on a vector space `E`, then the underlying type for the tangent
bundle is just `Σ (x : M), tangent_space x M` (i.e., the disjoint union of the tangent spaces,
indexed by `x` -- this is a basic object in dependent type theory). And `tangent_space x M`
is just (a copy of) `E` by definition. -/
lemma tangent_bundle_myℝ_is_prod : tangent_bundle 𝓡1 myℝ = Σ (x : myℝ), ℝ :=
/- inline sorry -/rfl/- inline sorry -/
/- This means that you can specify a point in the tangent bundle as a pair `⟨x, v⟩`.
However, in general, a tangent bundle is not trivial: the topology on `tangent_bundle 𝓡1 myℝ` is *not*
the product topology. Instead, the tangent space at a point `x` is identified with `ℝ` through some
preferred chart at `x`, called `chart_at ℝ x`, but the way they are glued together depends on the
manifold and the charts.
In vector spaces, the tangent space is canonically the product space, with the same topology, as
there is only one chart so there is no strange gluing at play. The fact that the canonical map
from the sigma type to the product type (called `equiv.sigma_equiv_prod`) is a homeomorphism is
given in the library by `tangent_bundle_model_space_homeomorph` (note that this is a definition,
constructing the homeomorphism, instead of a proposition asserting that `equiv.sigma_equiv_prod`
is a homeomorphism, because we use bundled homeomorphisms in mathlib).
Let us register the identification explicitly, as a homeomorphism. You can use the relevant fields
of `tangent_bundle_model_space_homeomorph` to fill the nontrivial fields here.
-/
def tangent_bundle_vector_space_triv (E : Type u) [normed_group E] [normed_space ℝ E] :
tangent_bundle (model_with_corners_self ℝ E) E ≃ₜ E × E :=
{ to_fun := λ p, (p.1, p.2),
inv_fun := λ p, ⟨p.1, p.2⟩,
left_inv := /- inline sorry -/begin rintro ⟨x, v⟩, refl end/- inline sorry -/,
right_inv := /- inline sorry -/begin rintro ⟨x, v⟩, refl end/- inline sorry -/,
continuous_to_fun := begin
-- sorry
exact (tangent_bundle_model_space_homeomorph E (model_with_corners_self ℝ E)).continuous,
-- sorry
end,
continuous_inv_fun :=
begin
-- sorry
exact (tangent_bundle_model_space_homeomorph E (model_with_corners_self ℝ E)).continuous_inv_fun,
-- sorry
end }
/- Even though the tangent bundle to `myℝ` is trivial abstractly, with this construction the
tangent bundle is *not* the product space with the product topology, as we have used various charts
so the gluing is not trivial. The following exercise unfolds the definition to see what is going on.
It is not a reasonable exercise, in the sense that one should never ever do this when working
with a manifold! -/
lemma crazy_formula_after_identifications (x : ℝ) (v : ℝ) :
let p : tangent_bundle 𝓡1 myℝ := ⟨(3 : ℝ), 0⟩ in
chart_at (model_prod ℝ ℝ) p ⟨x, v⟩ = if x ∈ Ioo (-1 : ℝ) 1 then (x, -v) else (x, v) :=
begin
-- this exercise is not easy (and shouldn't be: you are not supposed to use the library like this!)
-- if you really want to do this, you should unfold as much as you can using simp and dsimp, until you
-- are left with a statement speaking of derivatives of real functions, without any manifold code left.
-- sorry
have : ¬ ((3 : ℝ) < 1), by norm_num,
simp only [chart_at, this, mem_Ioo, if_false, and_false],
dsimp [tangent_bundle_core, basic_smooth_bundle_core.chart,
topological_fiber_bundle_core.local_triv, topological_fiber_bundle_core.local_triv',
topological_fiber_bundle_core.index_at,
basic_smooth_bundle_core.to_topological_fiber_bundle_core],
split_ifs,
{ simp only [chart_at, h, my_first_local_homeo, if_true, fderiv_within_univ, mem_Ioo, fderiv_id',
continuous_linear_map.coe_id', continuous_linear_map.neg_apply, fderiv_neg] with mfld_simps },
{ simp only [chart_at, h, fderiv_within_univ, mem_Ioo, if_false, @local_homeomorph.refl_symm ℝ,
fderiv_id, continuous_linear_map.coe_id'] with mfld_simps }
-- sorry
end
end you_should_probably_skip_this
/-!
### The language of manifolds
In this paragraph, we will try to write down interesting statements of theorems, without proving them. The
goal here is that Lean should not complain on the statement, but the proof should be sorried.
-/
/- Here is a first example, already filled up, to show you how diffeomorphisms are currently named
(we will probably introduce an abbreviation, but this hasn't been done yet).
Don't try to fill the sorried proof! -/
/-- Two zero-dimensional connected manifolds are diffeomorphic. -/
theorem diffeomorph_of_zero_dim_connected
(M M' : Type*) [topological_space M] [topological_space M']
[charted_space (euclidean_space ℝ (fin 0)) M] [charted_space (euclidean_space ℝ (fin 0)) M']
[connected_space M] [connected_space M'] :
nonempty (structomorph (times_cont_diff_groupoid ∞ (𝓡 0)) M M') :=
sorry
/- Do you think that this statement is correct? (note that we have not assumed that our manifolds
are smooth, nor that they are separated, but this is maybe automatic in zero dimension).
Now, write down a version of this theorem in dimension 1, replacing the first sorry with meaningful content
(and adding what is needed before the colon).
Don't try to fill the sorried proof! -/
/-- Two one-dimensional smooth compact connected manifolds are diffeomorphic. -/
theorem diffeomorph_of_one_dim_compact_connected
-- omit
(M M' : Type*) [topological_space M] [topological_space M']
[charted_space (euclidean_space ℝ (fin 1)) M] [charted_space (euclidean_space ℝ (fin 1)) M']
[connected_space M] [connected_space M'] [compact_space M] [compact_space M']
[t2_space M] [t2_space M']
[smooth_manifold_with_corners (𝓡 1) M] [smooth_manifold_with_corners (𝓡 1) M']
-- omit
:
-- sorry
nonempty (structomorph (times_cont_diff_groupoid ∞ (𝓡 1)) M M')
-- sorry
:= sorry
/- You will definitely need to require smoothness and separation in this case, as it is wrong otherwise.
Note that Lean won't complain if you don't put these assumptions, as the theorem would still make
sense, but it would just turn out to be wrong.
The previous statement is not really satisfactory: we would instead like to express that any such
manifold is diffeomorphic to the circle. The trouble is that we don't have the circle as a smooth
manifold yet. Since we have cheated and introduced it (with sorries) at the beginning of the tutorial,
let's cheat again and use it to reformulate the previous statement.
-/
-- the next result is not trivial, leave it sorried (but you can work on it if you don't like
-- manifolds and prefer topology -- then please PR it to mathlib!).
instance connected_sphere (n : ℕ) : connected_space (sphere (n+1)) := sorry
/- The next two instances are easier to prove, you can prove them or leave them sorried
as you like. For the second one, you may need to use facts of the library such as -/
#check is_compact_iff_compact_space
#check metric.compact_iff_closed_bounded
instance (n : ℕ) : t2_space (sphere n) :=
begin
-- sorry
dunfold sphere,
apply_instance
-- sorry
end
instance (n : ℕ) : compact_space (sphere n) :=
begin
-- sorry
dunfold sphere,
apply is_compact_iff_compact_space.1,
rw metric.compact_iff_closed_bounded,
split,
{ exact metric.is_closed_sphere },
{ rw metric.bounded_iff_subset_ball (0 : euclidean_space ℝ (fin (n+1))),
exact ⟨1, metric.sphere_subset_closed_ball⟩ }
-- sorry
end
/- Now, you can prove that any one-dimensional compact connected manifold is diffeomorphic to
the circle. Here, you should fill the `sorry` (but luckily you may use
`diffeomorph_of_one_dim_compact_connected`). -/
theorem diffeomorph_circle_of_one_dim_compact_connected
(M : Type*) [topological_space M] [charted_space (euclidean_space ℝ (fin 1)) M]
[connected_space M] [compact_space M] [t2_space M] [smooth_manifold_with_corners (𝓡 1) M] :
nonempty (structomorph (times_cont_diff_groupoid ∞ (𝓡 1)) M (sphere 1)) :=
-- sorry
diffeomorph_of_one_dim_compact_connected M (sphere 1)
-- sorry
/- What about trying to say that there are uncountably many different smooth structures on `ℝ⁴`?
(see https://en.wikipedia.org/wiki/Exotic_R4). The library is not really designed with this in mind,
as in general we only work with one differentiable structure on a space, but it is perfectly
capable of expressing this fact if one uses the `@` version of some definitions.
Don't try to fill the sorried proof!
-/
theorem exotic_ℝ4 :
-- sorry
let E := (euclidean_space ℝ (fin 4)) in
∃ f : ℝ → charted_space E E,
∀ i, @has_groupoid E _ E _ (f i) (times_cont_diff_groupoid ∞ (𝓡 4))
∧ ∀ i j, nonempty (@structomorph _ _ (times_cont_diff_groupoid ∞ (𝓡 4)) E E _ _ (f i) (f j)) →
i = j
-- sorry
:=
sorry
/-!
### Smooth functions on `[0, 1]`
In this paragraph, you will prove several (math-trivial but Lean-nontrivial) statements on the smooth
structure of `[0,1]`. These facts should be Lean-trivial, but they are not (yet) since there is essentially
nothing in this direction for now in the library.
The goal is as much to be able to write the statements as to prove them. Most of the necessary vocabulary
has been introduced above, so don't hesitate to browse the file if you are stuck. Additionally, you will
need the notion of a smooth function on a subset: it is `times_cont_diff_on` for functions between vector
spaces and `times_cont_mdiff_on` for functions between manifolds.
Try to formulate the next math statements in Lean, and prove them (but see below for hints):
Lemma times_cont_mdiff_g : the inclusion `g` of `[0, 1]` in `ℝ` is smooth.
Lemma msmooth_of_smooth : Consider a function `f : ℝ → [0, 1]`, which is smooth in the usual sense as a function
from `ℝ` to `ℝ` on a set `s`. Then it is manifold-smooth on `s`.
Definition : construct a function `f` from `ℝ` to `[0,1]` which is the identity on `[0, 1]`.
Theorem : the tangent bundle to `[0, 1]` is homeomorphic to `[0, 1] × ℝ`
Hint for the last theorem: don't try to unfold the definition of the tangent bundle, it will only get you
into trouble. Instead, use the derivatives of the maps `f` and `g`, and rely on functoriality
to check that they are inverse to each other. (This advice is slightly misleading as these derivatives
do not go between the right spaces, so you will need to massage them a little bit).
A global advice: don't hesitate to use and abuse `simp`, it is the main workhorse in this
area of mathlib.
-/
/- After doing the exercise myself, I realized it was (way!) too hard. So I will give at least the statements
of the lemmas, to guide you a little bit more. To let you try the original version if you want,
I have left a big blank space to avoid spoilers. -/
def g : Icc (0 : ℝ) 1 → ℝ := subtype.val
-- smoothness results for `euclidean_space` are expressed for general `L^p` spaces
-- (as `euclidean_space` has the `L^2` norm), in:
#check pi_Lp.times_cont_diff_coord
#check pi_Lp.times_cont_diff_on_iff_coord
lemma times_cont_mdiff_g : times_cont_mdiff (𝓡∂ 1) 𝓡1 ∞ g :=
begin
-- sorry
rw times_cont_mdiff_iff,
refine ⟨continuous_subtype_val, λ x y, _⟩,
by_cases h : (x : ℝ) < 1,
{ simp only [g, chart_at, h, Icc_left_chart, function.comp, model_with_corners_euclidean_half_space,
add_zero, dif_pos, if_true, max_lt_iff, preimage_set_of_eq, sub_zero, subtype.range_coe_subtype,
subtype.coe_mk, subtype.val_eq_coe] with mfld_simps,
refine (pi_Lp.times_cont_diff_coord 0).times_cont_diff_on.congr (λ f hf, _),
obtain ⟨hf₀, hf₁⟩ : 0 ≤ f 0 ∧ f 0 < 1, by simpa using hf,
simp [min_eq_left hf₁.le, max_eq_left hf₀] },
{ simp only [chart_at, h, Icc_right_chart, function.comp, model_with_corners_euclidean_half_space, dif_pos,
max_lt_iff, preimage_set_of_eq, sub_zero, subtype.range_coe_subtype, if_false, subtype.coe_mk,
subtype.val_eq_coe, g] with mfld_simps,
have : times_cont_diff ℝ ⊤ (λ (x : euclidean_space ℝ (fin 1)), 1 - x 0) :=
times_cont_diff_const.sub (pi_Lp.times_cont_diff_coord 0),
apply this.times_cont_diff_on.congr (λ f hf, _),
obtain ⟨hf₀, hf₁⟩ : 0 ≤ f 0 ∧ f 0 < 1, by simpa using hf,
have : 0 ≤ 1 - f 0, by linarith,
simp [hf₀, this, max_eq_left], }
-- sorry
end
lemma msmooth_of_smooth {f : ℝ → Icc (0 : ℝ) 1} {s : set ℝ} (h : times_cont_diff_on ℝ ∞ (λ x, (f x : ℝ)) s) :
times_cont_mdiff_on 𝓡1 (𝓡∂ 1) ∞ f s :=
begin
-- sorry
rw times_cont_mdiff_on_iff,
split,
{ have : embedding (subtype.val : Icc (0 : ℝ) 1 → ℝ) := embedding_subtype_coe,
exact (embedding.continuous_on_iff this).2 h.continuous_on },
simp only with mfld_simps,
assume y,
by_cases hy : (y : ℝ) < 1,
{ simp [chart_at, model_with_corners_euclidean_half_space, (∘), hy, Icc_left_chart,
pi_Lp.times_cont_diff_on_iff_coord],
apply h.mono (inter_subset_left _ _) },
{ simp [chart_at, model_with_corners_euclidean_half_space, (∘), hy, Icc_right_chart,
pi_Lp.times_cont_diff_on_iff_coord],
assume i,
apply (times_cont_diff_on_const.sub h).mono (inter_subset_left _ _) }
-- sorry
end
/- A function from `ℝ` to `[0,1]` which is the identity on `[0,1]`. -/
def f : ℝ → Icc (0 : ℝ) 1 :=
λ x, ⟨max (min x 1) 0, by simp [le_refl, zero_le_one]⟩
lemma times_cont_mdiff_on_f : times_cont_mdiff_on 𝓡1 (𝓡∂ 1) ∞ f (Icc 0 1) :=
begin
-- sorry
apply msmooth_of_smooth,
apply times_cont_diff_id.times_cont_diff_on.congr,
assume x hx,
simp at hx,
simp [f, hx],
-- sorry
end
lemma fog : f ∘ g = id :=
begin
-- sorry
ext x,
rcases x with ⟨x', h'⟩,
simp at h',
simp [f, g, h'],
-- sorry
end
lemma gof : ∀ x ∈ Icc (0 : ℝ) 1, g (f x) = x :=
begin
-- sorry
assume x hx,
simp at hx,
simp [g, f],
simp [hx],
-- sorry
end
def G : tangent_bundle (𝓡∂ 1) (Icc (0 : ℝ) 1) → (Icc (0 : ℝ) 1) × ℝ :=
λ p, (p.1, ((tangent_bundle_vector_space_triv ℝ) (tangent_map (𝓡∂ 1) 𝓡1 g p)).2)
lemma continuous_G : continuous G :=
begin
-- sorry
apply continuous.prod_mk (tangent_bundle_proj_continuous _ _),
refine continuous_snd.comp _,
apply continuous.comp (homeomorph.continuous _),
apply times_cont_mdiff.continuous_tangent_map times_cont_mdiff_g le_top,
-- sorry
end
def F : (Icc (0 : ℝ) 1) × ℝ → tangent_bundle (𝓡∂ 1) (Icc (0 : ℝ) 1) :=
λ p, tangent_map_within 𝓡1 (𝓡∂ 1) f (Icc 0 1)
((tangent_bundle_vector_space_triv ℝ).symm (p.1, p.2))
lemma continuous_F : continuous F :=
begin
-- sorry
rw continuous_iff_continuous_on_univ,
apply (times_cont_mdiff_on_f.continuous_on_tangent_map_within le_top _).comp,
{ apply ((tangent_bundle_vector_space_triv ℝ).symm.continuous.comp _).continuous_on,
apply (continuous_subtype_coe.comp continuous_fst).prod_mk continuous_snd },
{ rintros ⟨⟨x, hx⟩, v⟩ _,
simp [tangent_bundle_vector_space_triv],
exact hx },
{ rw unique_mdiff_on_iff_unique_diff_on,
exact unique_diff_on_Icc_zero_one }
-- sorry
end
lemma FoG : F ∘ G = id :=
begin
-- sorry
ext1 ⟨x, v⟩,
simp [F, G, tangent_map_within, tangent_bundle_vector_space_triv, f],
dsimp,
split,
{ rcases x with ⟨x', h'⟩,
simp at h',
simp [h'] },
{ change (tangent_map_within 𝓡1 (𝓡∂ 1) f (Icc 0 1) (tangent_map (𝓡∂ 1) 𝓡1 g ⟨x, v⟩)).snd = v,
rw [← tangent_map_within_univ, ← tangent_map_within_comp_at, fog, tangent_map_within_univ, tangent_map_id],
{ refl },
{ apply times_cont_mdiff_on_f.mdifferentiable_on le_top,
simpa [g] using x.2 },
{ apply (times_cont_mdiff_g.times_cont_mdiff_at.mdifferentiable_at le_top).mdifferentiable_within_at },
{ assume z hz,
simpa [g] using z.2 },
{ apply unique_mdiff_on_univ _ (mem_univ _) } }
-- sorry
end
lemma GoF : G ∘ F = id :=
begin
-- sorry
ext1 ⟨x, v⟩,
simp [F, G, tangent_map_within, tangent_bundle_vector_space_triv, f],
dsimp,
split,
{ rcases x with ⟨x', h'⟩,
simp at h',
simp [h'] },
{ have A : unique_mdiff_within_at 𝓡1 (Icc 0 1) (⟨(x : ℝ), v⟩ : tangent_bundle 𝓡1 ℝ).fst,
{ rw unique_mdiff_within_at_iff_unique_diff_within_at,
apply unique_diff_on_Icc_zero_one _ x.2 },
change (tangent_map (𝓡∂ 1) 𝓡1 g (tangent_map_within 𝓡1 (𝓡∂ 1) f (Icc 0 1) ⟨x, v⟩)).snd = v,
rw [← tangent_map_within_univ, ← tangent_map_within_comp_at _ _ _ subset_preimage_univ A],
{ have : tangent_map_within 𝓡1 𝓡1 (g ∘ f) (Icc 0 1) ⟨x, v⟩
= tangent_map_within 𝓡1 𝓡1 id (Icc 0 1) ⟨x, v⟩ :=
tangent_map_within_congr gof _ x.2 A,
rw [this, tangent_map_within_id _ A] },
{ apply times_cont_mdiff_g.times_cont_mdiff_on.mdifferentiable_on le_top _ (mem_univ _) },
{ apply times_cont_mdiff_on_f.mdifferentiable_on le_top _ x.2 } }
-- sorry
end
def my_tangent_homeo : tangent_bundle (𝓡∂ 1) (Icc (0 : ℝ) 1) ≃ₜ (Icc (0 : ℝ) 1) × ℝ :=
-- sorry
{ to_fun := G,
inv_fun := F,
continuous_to_fun := continuous_G,
continuous_inv_fun := continuous_F,
left_inv := λ p, show (F ∘ G) p = id p, by rw FoG,
right_inv := λ p, show (G ∘ F) p = id p, by rw GoF }
-- sorry
/-!
### Further things to do
1) can you prove `diffeomorph_of_zero_dim_connected` or `connected_sphere`?
2) Try to express and then prove the local inverse theorem in real manifolds: if a map between
real manifolds (without boundary, modelled on a complete vector space) is smooth, then it is
a local homeomorphism around each point. We already have versions of this statement in mathlib
for functions between vector spaces, but this is very much a work in progress.
3) What about trying to prove `diffeomorph_of_one_dim_compact_connected`? (I am not sure mathlib
is ready for this, as the proofs I am thinking of are currently a little bit too high-powered.
If you manage to do it, you should absolutely PR it!)
4) Why not contribute to the proof of `sphere_eversion`? You can have a look at
https://leanprover-community.github.io/sphere-eversion/ to learn more about this project
by Patrick Massot.
-/
|
1a3abd681ec4260b535446ce2f00f6127b4be6dd | 432d948a4d3d242fdfb44b81c9e1b1baacd58617 | /src/category_theory/products/basic.lean | fc089dc6a3c3868fbc304f952af4c976ab5e83f7 | [
"Apache-2.0"
] | permissive | JLimperg/aesop3 | 306cc6570c556568897ed2e508c8869667252e8a | a4a116f650cc7403428e72bd2e2c4cda300fe03f | refs/heads/master | 1,682,884,916,368 | 1,620,320,033,000 | 1,620,320,033,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,444 | 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 category_theory.eq_to_hom
/-!
# Cartesian products of categories
We define the category instance on `C × D` when `C` and `D` are categories.
We define:
* `sectl C Z` : the functor `C ⥤ C × D` given by `X ↦ ⟨X, Z⟩`
* `sectr Z D` : the functor `D ⥤ C × D` given by `Y ↦ ⟨Z, Y⟩`
* `fst` : the functor `⟨X, Y⟩ ↦ X`
* `snd` : the functor `⟨X, Y⟩ ↦ Y`
* `swap` : the functor `C × D ⥤ D × C` given by `⟨X, Y⟩ ↦ ⟨Y, X⟩`
(and the fact this is an equivalence)
We further define `evaluation : C ⥤ (C ⥤ D) ⥤ D` and `evaluation_uncurried : C × (C ⥤ D) ⥤ D`,
and products of functors and natural transformations, written `F.prod G` and `α.prod β`.
-/
namespace category_theory
-- declare the `v`'s first; see `category_theory.category` for an explanation
universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄
section
variables (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D]
/--
`prod C D` gives the cartesian product of two categories.
See https://stacks.math.columbia.edu/tag/001K.
-/
instance prod : category.{max v₁ v₂} (C × D) :=
{ hom := λ X Y, ((X.1) ⟶ (Y.1)) × ((X.2) ⟶ (Y.2)),
id := λ X, ⟨ 𝟙 (X.1), 𝟙 (X.2) ⟩,
comp := λ _ _ _ f g, (f.1 ≫ g.1, f.2 ≫ g.2) }
-- rfl lemmas for category.prod
@[simp] lemma prod_id (X : C) (Y : D) : 𝟙 (X, Y) = (𝟙 X, 𝟙 Y) := rfl
@[simp] lemma prod_comp {P Q R : C} {S T U : D} (f : (P, S) ⟶ (Q, T)) (g : (Q, T) ⟶ (R, U)) :
f ≫ g = (f.1 ≫ g.1, f.2 ≫ g.2) := rfl
@[simp] lemma prod_id_fst (X : prod C D) : prod.fst (𝟙 X) = 𝟙 X.fst := rfl
@[simp] lemma prod_id_snd (X : prod C D) : prod.snd (𝟙 X) = 𝟙 X.snd := rfl
@[simp] lemma prod_comp_fst {X Y Z : prod C D} (f : X ⟶ Y) (g : Y ⟶ Z) :
(f ≫ g).1 = f.1 ≫ g.1 := rfl
@[simp] lemma prod_comp_snd {X Y Z : prod C D} (f : X ⟶ Y) (g : Y ⟶ Z) :
(f ≫ g).2 = f.2 ≫ g.2 := rfl
end
section
variables (C : Type u₁) [category.{v₁} C] (D : Type u₁) [category.{v₁} D]
/--
`prod.category.uniform C D` is an additional instance specialised so both factors have the same
universe levels. This helps typeclass resolution.
-/
instance uniform_prod : category (C × D) := category_theory.prod C D
end
-- Next we define the natural functors into and out of product categories. For now this doesn't
-- address the universal properties.
namespace prod
/-- `sectl C Z` is the functor `C ⥤ C × D` given by `X ↦ (X, Z)`. -/
@[simps] def sectl
(C : Type u₁) [category.{v₁} C] {D : Type u₂} [category.{v₂} D] (Z : D) : C ⥤ C × D :=
{ obj := λ X, (X, Z),
map := λ X Y f, (f, 𝟙 Z) }
/-- `sectr Z D` is the functor `D ⥤ C × D` given by `Y ↦ (Z, Y)` . -/
@[simps] def sectr
{C : Type u₁} [category.{v₁} C] (Z : C) (D : Type u₂) [category.{v₂} D] : D ⥤ C × D :=
{ obj := λ X, (Z, X),
map := λ X Y f, (𝟙 Z, f) }
variables (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D]
/-- `fst` is the functor `(X, Y) ↦ X`. -/
@[simps] def fst : C × D ⥤ C :=
{ obj := λ X, X.1,
map := λ X Y f, f.1 }
/-- `snd` is the functor `(X, Y) ↦ Y`. -/
@[simps] def snd : C × D ⥤ D :=
{ obj := λ X, X.2,
map := λ X Y f, f.2 }
/-- The functor swapping the factors of a cartesian product of categories, `C × D ⥤ D × C`. -/
@[simps] def swap : C × D ⥤ D × C :=
{ obj := λ X, (X.2, X.1),
map := λ _ _ f, (f.2, f.1) }
/--
Swapping the factors of a cartesion product of categories twice is naturally isomorphic
to the identity functor.
-/
@[simps] def symmetry : swap C D ⋙ swap D C ≅ 𝟭 (C × D) :=
{ hom := { app := λ X, 𝟙 X },
inv := { app := λ X, 𝟙 X } }
/--
The equivalence, given by swapping factors, between `C × D` and `D × C`.
-/
@[simps]
def braiding : C × D ≌ D × C :=
equivalence.mk (swap C D) (swap D C)
(nat_iso.of_components (λ X, eq_to_iso (by simp)) (by tidy))
(nat_iso.of_components (λ X, eq_to_iso (by simp)) (by tidy))
instance swap_is_equivalence : is_equivalence (swap C D) :=
(by apply_instance : is_equivalence (braiding C D).functor)
end prod
section
variables (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D]
/--
The "evaluation at `X`" functor, such that
`(evaluation.obj X).obj F = F.obj X`,
which is functorial in both `X` and `F`.
-/
@[simps] def evaluation : C ⥤ (C ⥤ D) ⥤ D :=
{ obj := λ X,
{ obj := λ F, F.obj X,
map := λ F G α, α.app X, },
map := λ X Y f,
{ app := λ F, F.map f,
naturality' := λ F G α, eq.symm (α.naturality f) } }
/--
The "evaluation of `F` at `X`" functor,
as a functor `C × (C ⥤ D) ⥤ D`.
-/
@[simps] def evaluation_uncurried : C × (C ⥤ D) ⥤ D :=
{ obj := λ p, p.2.obj p.1,
map := λ x y f, (x.2.map f.1) ≫ (f.2.app y.1),
map_comp' := λ X Y Z f g,
begin
cases g, cases f, cases Z, cases Y, cases X,
simp only [prod_comp, nat_trans.comp_app, functor.map_comp, category.assoc],
rw [←nat_trans.comp_app, nat_trans.naturality, nat_trans.comp_app,
category.assoc, nat_trans.naturality],
end }
end
variables {A : Type u₁} [category.{v₁} A]
{B : Type u₂} [category.{v₂} B]
{C : Type u₃} [category.{v₃} C]
{D : Type u₄} [category.{v₄} D]
namespace functor
/-- The cartesian product of two functors. -/
@[simps] def prod (F : A ⥤ B) (G : C ⥤ D) : A × C ⥤ B × D :=
{ obj := λ X, (F.obj X.1, G.obj X.2),
map := λ _ _ f, (F.map f.1, G.map f.2) }
/- Because of limitations in Lean 3's handling of notations, we do not setup a notation `F × G`.
You can use `F.prod G` as a "poor man's infix", or just write `functor.prod F G`. -/
end functor
namespace nat_trans
/-- The cartesian product of two natural transformations. -/
@[simps] def prod {F G : A ⥤ B} {H I : C ⥤ D} (α : F ⟶ G) (β : H ⟶ I) :
F.prod H ⟶ G.prod I :=
{ app := λ X, (α.app X.1, β.app X.2),
naturality' := λ X Y f,
begin
cases X, cases Y,
simp only [functor.prod_map, prod.mk.inj_iff, prod_comp],
split; rw naturality
end }
/- Again, it is inadvisable in Lean 3 to setup a notation `α × β`;
use instead `α.prod β` or `nat_trans.prod α β`. -/
end nat_trans
end category_theory
|
8e9dd38c4198f80b8f170328064bf14618922de7 | a9fe717b93ccfa4b2e64faeb24f96dfefb390240 | /nat.lean | 5c22ecf3713ef9002a3c34866dc7762f7cdf1d19 | [] | no_license | skbaek/omega | ab1f4a6daadfc8c855f14c39d9459ab841527141 | 715e384ed14e8eb177a326700066e7c98269e078 | refs/heads/master | 1,588,000,876,352 | 1,552,645,917,000 | 1,552,645,917,000 | 174,442,914 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 651 | lean | import data.nat.basic
namespace nat
lemma lt_iff_add_one_le {m n : ℕ} :
m < n ↔ m + 1 ≤ n := by rw succ_le_iff
lemma add_lt_add_of_le_of_le_of_lt_or_lt {a b c d : nat} :
a ≤ b → c ≤ d → (a < b ∨ c < d) → a + c < b + d :=
begin
intros h1 h2 h3, cases h3,
apply add_lt_add_of_lt_of_le; assumption,
apply add_lt_add_of_le_of_lt; assumption
end
lemma max_succ_succ {m n} :
max (succ m) (succ n) = succ (max m n) :=
begin
by_cases h1 : m ≤ n,
rw [max_eq_right h1, max_eq_right (succ_le_succ h1)],
{ rw not_le at h1, have h2 := le_of_lt h1,
rw [max_eq_left h2, max_eq_left (succ_le_succ h2)] }
end
end nat |
8a40388ba9db0e4c0a6c104f5a4f0c0230419ac3 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/string/defs.lean | ea5351bf282e7c06c733d95e7aed43da6cfc606d | [
"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,141 | lean | /-
Copyright (c) 2019 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Keeley Hoek, Floris van Doorn
-/
import data.list.defs
/-!
# Definitions for `string`
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines a bunch of functions for the `string` datatype.
-/
namespace string
/-- `s.split_on c` tokenizes `s : string` on `c : char`. -/
def split_on (s : string) (c : char) : list string :=
split (= c) s
/-- `string.map_tokens c f s` tokenizes `s : string` on `c : char`, maps `f` over each token, and
then reassembles the string by intercalating the separator token `c` over the mapped tokens. -/
def map_tokens (c : char) (f : string → string) : string → string :=
intercalate (singleton c) ∘ list.map f ∘ split (= c)
/-- Tests whether the first string is a prefix of the second string. -/
def is_prefix_of (x y : string) : bool :=
x.to_list.is_prefix_of y.to_list
/-- Tests whether the first string is a suffix of the second string. -/
def is_suffix_of (x y : string) : bool :=
x.to_list.is_suffix_of y.to_list
/-- `x.starts_with y` is true if `y` is a prefix of `x`, and is false otherwise. -/
abbreviation starts_with (x y : string) : bool :=
y.is_prefix_of x
/-- `x.ends_with y` is true if `y` is a suffix of `x`, and is false otherwise. -/
abbreviation ends_with (x y : string) : bool :=
y.is_suffix_of x
/-- `get_rest s t` returns `some r` if `s = t ++ r`.
If `t` is not a prefix of `s`, returns `none` -/
def get_rest (s t : string) : option string :=
list.as_string <$> s.to_list.get_rest t.to_list
/-- Removes the first `n` elements from the string `s` -/
def popn (s : string) (n : nat) : string :=
(s.mk_iterator.nextn n).next_to_string
/-- `is_nat s` is true iff `s` is a nonempty sequence of digits. -/
def is_nat (s : string) : bool :=
¬ s.is_empty ∧ s.to_list.all (λ c, to_bool c.is_digit)
/-- Produce the head character from the string `s`, if `s` is not empty, otherwise 'A'. -/
def head (s : string) : char :=
s.mk_iterator.curr
end string
|
d30f2f7523e5ea6de81ca9833d12c87f19c90c4d | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/data/zmod/basic.lean | 9fc67e9146b9bfce273dc21b708ab3fcbe780b75 | [
"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 | 28,289 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import data.int.modeq
import algebra.char_p.basic
import data.nat.totient
import ring_theory.ideal.operations
/-!
# Integers mod `n`
Definition of the integers mod n, and the field structure on the integers mod p.
## Definitions
* `zmod n`, which is for integers modulo a nat `n : ℕ`
* `val a` is defined as a natural number:
- for `a : zmod 0` it is the absolute value of `a`
- for `a : zmod n` with `0 < n` it is the least natural number in the equivalence class
* `val_min_abs` returns the integer closest to zero in the equivalence class.
* A coercion `cast` is defined from `zmod n` into any ring.
This is a ring hom if the ring has characteristic dividing `n`
-/
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 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)] : modeq_mul (nat.mod_mod _ _) rfl
... ≡ a * (b * c) [MOD (n+1)] : by rw mul_assoc
... ≡ a * (b * c % (n+1)) [MOD (n+1)] : modeq_mul rfl (nat.mod_mod _ _).symm),
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)] : modeq_mul rfl (nat.mod_mod _ _)
... ≡ a * b + a * c [MOD (n+1)] : by rw mul_add
... ≡ (a * b) % (n+1) + (a * c) % (n+1) [MOD (n+1)] :
modeq_add (nat.mod_mod _ _).symm (nat.mod_mod _ _).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.has_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)
namespace zmod
instance fintype : Π (n : ℕ) [fact (0 < n)], fintype (zmod n)
| 0 h := false.elim $ nat.not_lt_zero 0 h.1
| (n+1) _ := fin.fintype (n+1)
lemma card (n : ℕ) [fact (0 < n)] : fintype.card (zmod n) = n :=
begin
casesI n,
{ exfalso, exact nat.not_lt_zero 0 (fact.out _) },
{ exact fintype.card_fin (n+1) }
end
instance decidable_eq : Π (n : ℕ), decidable_eq (zmod n)
| 0 := int.decidable_eq
| (n+1) := fin.decidable_eq _
instance has_repr : Π (n : ℕ), has_repr (zmod n)
| 0 := int.has_repr
| (n+1) := fin.has_repr _
instance comm_ring : Π (n : ℕ), comm_ring (zmod n)
| 0 := int.comm_ring
| (n+1) := fin.comm_ring n
instance inhabited (n : ℕ) : inhabited (zmod n) := ⟨0⟩
/-- `val a` is a natural number defined as:
- for `a : zmod 0` it is the absolute value of `a`
- for `a : zmod n` with `0 < n` it is the least natural number in the equivalence class
See `zmod.val_min_abs` for a variant that takes values in the integers.
-/
def val : Π {n : ℕ}, zmod n → ℕ
| 0 := int.nat_abs
| (n+1) := (coe : fin (n + 1) → ℕ)
lemma val_lt {n : ℕ} [fact (0 < n)] (a : zmod n) : a.val < n :=
begin
casesI n,
{ exfalso, exact nat.not_lt_zero 0 (fact.out _) },
exact fin.is_lt a
end
@[simp] lemma val_zero : ∀ {n}, (0 : zmod n).val = 0
| 0 := rfl
| (n+1) := rfl
lemma val_nat_cast {n : ℕ} (a : ℕ) : (a : zmod n).val = a % n :=
begin
casesI n,
{ rw [nat.mod_zero, int.nat_cast_eq_coe_nat],
exact int.nat_abs_of_nat a, },
rw ← fin.of_nat_eq_coe,
refl
end
instance (n : ℕ) : char_p (zmod n) n :=
{ cast_eq_zero_iff :=
begin
intro k,
cases n,
{ simp only [int.nat_cast_eq_coe_nat, zero_dvd_iff, int.coe_nat_eq_zero], },
rw [fin.eq_iff_veq],
show (k : zmod (n+1)).val = (0 : zmod (n+1)).val ↔ _,
rw [val_nat_cast, val_zero, nat.dvd_iff_mod_eq_zero],
end }
@[simp] lemma nat_cast_self (n : ℕ) : (n : zmod n) = 0 :=
char_p.cast_eq_zero (zmod n) n
@[simp] lemma nat_cast_self' (n : ℕ) : (n + 1 : zmod (n + 1)) = 0 :=
by rw [← nat.cast_add_one, nat_cast_self (n + 1)]
section universal_property
variables {n : ℕ} {R : Type*}
section
variables [has_zero R] [has_one R] [has_add R] [has_neg R]
/-- Cast an integer modulo `n` to another semiring.
This function is a morphism if the characteristic of `R` divides `n`.
See `zmod.cast_hom` for a bundled version. -/
def cast : Π {n : ℕ}, zmod n → R
| 0 := int.cast
| (n+1) := λ i, i.val
-- see Note [coercion into rings]
@[priority 900] instance (n : ℕ) : has_coe_t (zmod n) R := ⟨cast⟩
@[simp] lemma cast_zero : ((0 : zmod n) : R) = 0 :=
by { cases n; refl }
end
/-- So-named because the coercion is `nat.cast` into `zmod`. For `nat.cast` into an arbitrary ring,
see `zmod.nat_cast_val`. -/
lemma nat_cast_zmod_val {n : ℕ} [fact (0 < n)] (a : zmod n) : (a.val : zmod n) = a :=
begin
casesI n,
{ exfalso, exact nat.not_lt_zero 0 (fact.out _) },
{ apply fin.coe_coe_eq_self }
end
lemma nat_cast_right_inverse [fact (0 < n)] : function.right_inverse val (coe : ℕ → zmod n) :=
nat_cast_zmod_val
lemma nat_cast_zmod_surjective [fact (0 < n)] : function.surjective (coe : ℕ → zmod n) :=
nat_cast_right_inverse.surjective
/-- So-named because the outer coercion is `int.cast` into `zmod`. For `int.cast` into an arbitrary
ring, see `zmod.int_cast_cast`. -/
lemma int_cast_zmod_cast (a : zmod n) : ((a : ℤ) : zmod n) = a :=
begin
cases n,
{ rw [int.cast_id a, int.cast_id a], },
{ rw [coe_coe, int.nat_cast_eq_coe_nat, int.cast_coe_nat, fin.coe_coe_eq_self] }
end
lemma int_cast_right_inverse : function.right_inverse (coe : zmod n → ℤ) (coe : ℤ → zmod n) :=
int_cast_zmod_cast
lemma int_cast_surjective : function.surjective (coe : ℤ → zmod n) :=
int_cast_right_inverse.surjective
@[norm_cast]
lemma cast_id : ∀ n (i : zmod n), ↑i = i
| 0 i := int.cast_id i
| (n+1) i := nat_cast_zmod_val i
@[simp]
lemma cast_id' : (coe : zmod n → zmod n) = id := funext (cast_id n)
variables (R) [ring R]
/-- The coercions are respectively `nat.cast` and `zmod.cast`. -/
@[simp] lemma nat_cast_comp_val [fact (0 < n)] :
(coe : ℕ → R) ∘ (val : zmod n → ℕ) = coe :=
begin
casesI n,
{ exfalso, exact nat.not_lt_zero 0 (fact.out _) },
refl
end
/-- The coercions are respectively `int.cast`, `zmod.cast`, and `zmod.cast`. -/
@[simp] lemma int_cast_comp_cast : (coe : ℤ → R) ∘ (coe : zmod n → ℤ) = coe :=
begin
cases n,
{ exact congr_arg ((∘) int.cast) zmod.cast_id', },
{ ext, simp }
end
variables {R}
@[simp] lemma nat_cast_val [fact (0 < n)] (i : zmod n) : (i.val : R) = i :=
congr_fun (nat_cast_comp_val R) i
@[simp] lemma int_cast_cast (i : zmod n) : ((i : ℤ) : R) = i :=
congr_fun (int_cast_comp_cast R) i
section char_dvd
/-! If the characteristic of `R` divides `n`, then `cast` is a homomorphism. -/
variables {n} {m : ℕ} [char_p R m]
@[simp] lemma cast_one (h : m ∣ n) : ((1 : zmod n) : R) = 1 :=
begin
casesI n,
{ exact int.cast_one },
show ((1 % (n+1) : ℕ) : R) = 1,
cases n, { rw [nat.dvd_one] at h, substI m, apply subsingleton.elim },
rw nat.mod_eq_of_lt,
{ exact nat.cast_one },
exact nat.lt_of_sub_eq_succ rfl
end
lemma cast_add (h : m ∣ n) (a b : zmod n) : ((a + b : zmod n) : R) = a + b :=
begin
casesI n,
{ apply int.cast_add },
simp only [coe_coe],
symmetry,
erw [fin.coe_add, ← nat.cast_add, ← sub_eq_zero, ← nat.cast_sub (nat.mod_le _ _),
@char_p.cast_eq_zero_iff R _ _ m],
exact dvd_trans h (nat.dvd_sub_mod _),
end
lemma cast_mul (h : m ∣ n) (a b : zmod n) : ((a * b : zmod n) : R) = a * b :=
begin
casesI n,
{ apply int.cast_mul },
simp only [coe_coe],
symmetry,
erw [fin.coe_mul, ← nat.cast_mul, ← sub_eq_zero, ← nat.cast_sub (nat.mod_le _ _),
@char_p.cast_eq_zero_iff R _ _ m],
exact dvd_trans h (nat.dvd_sub_mod _),
end
/-- The canonical ring homomorphism from `zmod n` to a ring of characteristic `n`. -/
def cast_hom (h : m ∣ n) (R : Type*) [ring R] [char_p R m] : zmod n →+* R :=
{ to_fun := coe,
map_zero' := cast_zero,
map_one' := cast_one h,
map_add' := cast_add h,
map_mul' := cast_mul h }
@[simp] lemma cast_hom_apply {h : m ∣ n} (i : zmod n) : cast_hom h R i = i := rfl
@[simp, norm_cast]
lemma cast_sub (h : m ∣ n) (a b : zmod n) : ((a - b : zmod n) : R) = a - b :=
(cast_hom h R).map_sub a b
@[simp, norm_cast]
lemma cast_neg (h : m ∣ n) (a : zmod n) : ((-a : zmod n) : R) = -a :=
(cast_hom h R).map_neg a
@[simp, norm_cast]
lemma cast_pow (h : m ∣ n) (a : zmod n) (k : ℕ) : ((a ^ k : zmod n) : R) = a ^ k :=
(cast_hom h R).map_pow a k
@[simp, norm_cast]
lemma cast_nat_cast (h : m ∣ n) (k : ℕ) : ((k : zmod n) : R) = k :=
(cast_hom h R).map_nat_cast k
@[simp, norm_cast]
lemma cast_int_cast (h : m ∣ n) (k : ℤ) : ((k : zmod n) : R) = k :=
(cast_hom h R).map_int_cast k
end char_dvd
section char_eq
/-! Some specialised simp lemmas which apply when `R` has characteristic `n`. -/
variable [char_p R n]
@[simp] lemma cast_one' : ((1 : zmod n) : R) = 1 :=
cast_one (dvd_refl _)
@[simp] lemma cast_add' (a b : zmod n) : ((a + b : zmod n) : R) = a + b :=
cast_add (dvd_refl _) a b
@[simp] lemma cast_mul' (a b : zmod n) : ((a * b : zmod n) : R) = a * b :=
cast_mul (dvd_refl _) a b
@[simp] lemma cast_sub' (a b : zmod n) : ((a - b : zmod n) : R) = a - b :=
cast_sub (dvd_refl _) a b
@[simp] lemma cast_pow' (a : zmod n) (k : ℕ) : ((a ^ k : zmod n) : R) = a ^ k :=
cast_pow (dvd_refl _) a k
@[simp, norm_cast]
lemma cast_nat_cast' (k : ℕ) : ((k : zmod n) : R) = k :=
cast_nat_cast (dvd_refl _) k
@[simp, norm_cast]
lemma cast_int_cast' (k : ℤ) : ((k : zmod n) : R) = k :=
cast_int_cast (dvd_refl _) k
instance (R : Type*) [comm_ring R] [char_p R n] : algebra (zmod n) R :=
(zmod.cast_hom (dvd_refl n) R).to_algebra
variables (R)
lemma cast_hom_injective : function.injective (zmod.cast_hom (dvd_refl n) R) :=
begin
rw ring_hom.injective_iff,
intro x,
obtain ⟨k, rfl⟩ := zmod.int_cast_surjective x,
rw [ring_hom.map_int_cast, char_p.int_cast_eq_zero_iff R n,
char_p.int_cast_eq_zero_iff (zmod n) n],
exact id
end
lemma cast_hom_bijective [fintype R] (h : fintype.card R = n) :
function.bijective (zmod.cast_hom (dvd_refl n) R) :=
begin
haveI : fact (0 < n) :=
⟨begin
rw [pos_iff_ne_zero],
unfreezingI { rintro rfl },
exact fintype.card_eq_zero_iff.mp h 0
end⟩,
rw [fintype.bijective_iff_injective_and_card, zmod.card, h, eq_self_iff_true, and_true],
apply zmod.cast_hom_injective
end
/-- The unique ring isomorphism between `zmod n` and a ring `R`
of characteristic `n` and cardinality `n`. -/
noncomputable def ring_equiv [fintype R] (h : fintype.card R = n) : zmod n ≃+* R :=
ring_equiv.of_bijective _ (zmod.cast_hom_bijective R h)
end char_eq
end universal_property
lemma int_coe_eq_int_coe_iff (a b : ℤ) (c : ℕ) :
(a : zmod c) = (b : zmod c) ↔ a ≡ b [ZMOD c] :=
char_p.int_coe_eq_int_coe_iff (zmod c) c a b
lemma nat_coe_eq_nat_coe_iff (a b c : ℕ) :
(a : zmod c) = (b : zmod c) ↔ a ≡ b [MOD c] :=
begin
convert zmod.int_coe_eq_int_coe_iff a b c,
simp [nat.modeq.modeq_iff_dvd, int.modeq.modeq_iff_dvd],
end
lemma int_coe_zmod_eq_zero_iff_dvd (a : ℤ) (b : ℕ) : (a : zmod b) = 0 ↔ (b : ℤ) ∣ a :=
begin
change (a : zmod b) = ((0 : ℤ) : zmod b) ↔ (b : ℤ) ∣ a,
rw [zmod.int_coe_eq_int_coe_iff, int.modeq.modeq_zero_iff],
end
lemma nat_coe_zmod_eq_zero_iff_dvd (a b : ℕ) : (a : zmod b) = 0 ↔ b ∣ a :=
begin
change (a : zmod b) = ((0 : ℕ) : zmod b) ↔ b ∣ a,
rw [zmod.nat_coe_eq_nat_coe_iff, nat.modeq.modeq_zero_iff],
end
@[push_cast, simp]
lemma int_cast_mod (a : ℤ) (b : ℕ) : ((a % b : ℤ) : zmod b) = (a : zmod b) :=
begin
rw zmod.int_coe_eq_int_coe_iff,
apply int.modeq.mod_modeq,
end
local attribute [semireducible] int.nonneg
@[simp] lemma nat_cast_to_nat (p : ℕ) :
∀ {z : ℤ} (h : 0 ≤ z), (z.to_nat : zmod p) = z
| (n : ℕ) h := by simp only [int.cast_coe_nat, int.to_nat_coe_nat]
| -[1+n] h := false.elim h
lemma val_injective (n : ℕ) [fact (0 < n)] :
function.injective (zmod.val : zmod n → ℕ) :=
begin
casesI n,
{ exfalso, exact nat.not_lt_zero 0 (fact.out _) },
assume a b h,
ext,
exact h
end
lemma val_one_eq_one_mod (n : ℕ) : (1 : zmod n).val = 1 % n :=
by rw [← nat.cast_one, val_nat_cast]
lemma val_one (n : ℕ) [fact (1 < n)] : (1 : zmod n).val = 1 :=
by { rw val_one_eq_one_mod, exact nat.mod_eq_of_lt (fact.out _) }
lemma val_add {n : ℕ} [fact (0 < n)] (a b : zmod n) : (a + b).val = (a.val + b.val) % n :=
begin
casesI n,
{ exfalso, exact nat.not_lt_zero 0 (fact.out _) },
{ apply fin.val_add }
end
lemma val_mul {n : ℕ} (a b : zmod n) : (a * b).val = (a.val * b.val) % n :=
begin
cases n,
{ rw nat.mod_zero, apply int.nat_abs_mul },
{ apply fin.val_mul }
end
instance nontrivial (n : ℕ) [fact (1 < n)] : nontrivial (zmod n) :=
⟨⟨0, 1, assume h, zero_ne_one $
calc 0 = (0 : zmod n).val : by rw val_zero
... = (1 : zmod n).val : congr_arg zmod.val h
... = 1 : val_one n ⟩⟩
/-- The inversion on `zmod n`.
It is setup in such a way that `a * a⁻¹` is equal to `gcd a.val n`.
In particular, if `a` is coprime to `n`, and hence a unit, `a * a⁻¹ = 1`. -/
def inv : Π (n : ℕ), zmod n → zmod n
| 0 i := int.sign i
| (n+1) i := nat.gcd_a i.val (n+1)
instance (n : ℕ) : has_inv (zmod n) := ⟨inv n⟩
lemma inv_zero : ∀ (n : ℕ), (0 : zmod n)⁻¹ = 0
| 0 := int.sign_zero
| (n+1) := show (nat.gcd_a _ (n+1) : zmod (n+1)) = 0,
by { rw val_zero, unfold nat.gcd_a nat.xgcd nat.xgcd_aux, refl }
lemma mul_inv_eq_gcd {n : ℕ} (a : zmod n) :
a * a⁻¹ = nat.gcd a.val n :=
begin
cases n,
{ calc a * a⁻¹ = a * int.sign a : rfl
... = a.nat_abs : by rw [int.mul_sign, int.nat_cast_eq_coe_nat]
... = a.val.gcd 0 : by rw nat.gcd_zero_right; refl },
{ set k := n.succ,
calc a * a⁻¹ = a * a⁻¹ + k * nat.gcd_b (val a) k : by rw [nat_cast_self, zero_mul, add_zero]
... = ↑(↑a.val * nat.gcd_a (val a) k + k * nat.gcd_b (val a) k) :
by { push_cast, rw nat_cast_zmod_val, refl }
... = nat.gcd a.val k : (congr_arg coe (nat.gcd_eq_gcd_ab a.val k)).symm, }
end
@[simp] lemma nat_cast_mod (n : ℕ) (a : ℕ) : ((a % n : ℕ) : zmod n) = a :=
by conv {to_rhs, rw ← nat.mod_add_div a n}; simp
lemma eq_iff_modeq_nat (n : ℕ) {a b : ℕ} : (a : zmod n) = b ↔ a ≡ b [MOD n] :=
begin
cases n,
{ simp only [nat.modeq, int.coe_nat_inj', nat.mod_zero, int.nat_cast_eq_coe_nat], },
{ rw [fin.ext_iff, nat.modeq, ← val_nat_cast, ← val_nat_cast], exact iff.rfl, }
end
lemma coe_mul_inv_eq_one {n : ℕ} (x : ℕ) (h : nat.coprime x n) :
(x * x⁻¹ : zmod n) = 1 :=
begin
rw [nat.coprime, nat.gcd_comm, nat.gcd_rec] at h,
rw [mul_inv_eq_gcd, val_nat_cast, h, nat.cast_one],
end
/-- `unit_of_coprime` makes an element of `units (zmod n)` given
a natural number `x` and a proof that `x` is coprime to `n` -/
def unit_of_coprime {n : ℕ} (x : ℕ) (h : nat.coprime x n) : units (zmod n) :=
⟨x, x⁻¹, coe_mul_inv_eq_one x h, by rw [mul_comm, coe_mul_inv_eq_one x h]⟩
@[simp] lemma coe_unit_of_coprime {n : ℕ} (x : ℕ) (h : nat.coprime x n) :
(unit_of_coprime x h : zmod n) = x := rfl
lemma val_coe_unit_coprime {n : ℕ} (u : units (zmod n)) :
nat.coprime (u : zmod n).val n :=
begin
cases n,
{ rcases int.units_eq_one_or u with rfl|rfl; exact dec_trivial },
apply nat.modeq.coprime_of_mul_modeq_one ((u⁻¹ : units (zmod (n+1))) : zmod (n+1)).val,
have := units.ext_iff.1 (mul_right_inv u),
rw [units.coe_one] at this,
rw [← eq_iff_modeq_nat, nat.cast_one, ← this], clear this,
rw [← nat_cast_zmod_val ((u * u⁻¹ : units (zmod (n+1))) : zmod (n+1))],
rw [units.coe_mul, val_mul, nat_cast_mod],
end
@[simp] lemma inv_coe_unit {n : ℕ} (u : units (zmod n)) :
(u : zmod n)⁻¹ = (u⁻¹ : units (zmod n)) :=
begin
have := congr_arg (coe : ℕ → zmod n) (val_coe_unit_coprime u),
rw [← mul_inv_eq_gcd, nat.cast_one] at this,
let u' : units (zmod n) := ⟨u, (u : zmod n)⁻¹, this, by rwa mul_comm⟩,
have h : u = u', { apply units.ext, refl },
rw h,
refl
end
lemma mul_inv_of_unit {n : ℕ} (a : zmod n) (h : is_unit a) :
a * a⁻¹ = 1 :=
begin
rcases h with ⟨u, rfl⟩,
rw [inv_coe_unit, u.mul_inv],
end
lemma inv_mul_of_unit {n : ℕ} (a : zmod n) (h : is_unit a) :
a⁻¹ * a = 1 :=
by rw [mul_comm, mul_inv_of_unit a h]
/-- Equivalence between the units of `zmod n` and
the subtype of terms `x : zmod n` for which `x.val` is comprime to `n` -/
def units_equiv_coprime {n : ℕ} [fact (0 < n)] :
units (zmod n) ≃ {x : zmod n // nat.coprime x.val n} :=
{ to_fun := λ x, ⟨x, val_coe_unit_coprime x⟩,
inv_fun := λ x, unit_of_coprime x.1.val x.2,
left_inv := λ ⟨_, _, _, _⟩, units.ext (nat_cast_zmod_val _),
right_inv := λ ⟨_, _⟩, by simp }
section totient
open_locale nat
@[simp] lemma card_units_eq_totient (n : ℕ) [fact (0 < n)] :
fintype.card (units (zmod n)) = φ n :=
calc fintype.card (units (zmod n)) = fintype.card {x : zmod n // x.val.coprime n} :
fintype.card_congr zmod.units_equiv_coprime
... = φ n :
begin
apply finset.card_congr (λ (a : {x : zmod n // x.val.coprime n}) _, a.1.val),
{ intro a, simp [(a : zmod n).val_lt, a.prop.symm] {contextual := tt} },
{ intros _ _ _ _ h, rw subtype.ext_iff_val, apply val_injective, exact h, },
{ intros b hb,
rw [finset.mem_filter, finset.mem_range] at hb,
refine ⟨⟨b, _⟩, finset.mem_univ _, _⟩,
{ let u := unit_of_coprime b hb.2.symm,
exact val_coe_unit_coprime u },
{ show zmod.val (b : zmod n) = b,
rw [val_nat_cast, nat.mod_eq_of_lt hb.1], } }
end
end totient
instance subsingleton_units : subsingleton (units (zmod 2)) :=
⟨λ x y, begin
cases x with x xi,
cases y with y yi,
revert x y xi yi,
exact dec_trivial
end⟩
lemma le_div_two_iff_lt_neg (n : ℕ) [hn : fact ((n : ℕ) % 2 = 1)]
{x : zmod n} (hx0 : x ≠ 0) : x.val ≤ (n / 2 : ℕ) ↔ (n / 2 : ℕ) < (-x).val :=
begin
haveI npos : fact (0 < n) := ⟨by
{ apply (nat.eq_zero_or_pos n).resolve_left,
unfreezingI { rintro rfl },
simpa [fact_iff] using hn, }⟩,
have hn2 : (n : ℕ) / 2 < n := nat.div_lt_of_lt_mul
((lt_mul_iff_one_lt_left npos.1).2 dec_trivial),
have hn2' : (n : ℕ) - n / 2 = n / 2 + 1,
{ conv {to_lhs, congr, rw [← nat.succ_sub_one n, nat.succ_sub npos.1]},
rw [← nat.two_mul_odd_div_two hn.1, two_mul, ← nat.succ_add, nat.add_sub_cancel], },
have hxn : (n : ℕ) - x.val < n,
{ rw [nat.sub_lt_iff (le_of_lt x.val_lt) (le_refl _), nat.sub_self],
rw ← zmod.nat_cast_zmod_val x at hx0,
exact nat.pos_of_ne_zero (λ h, by simpa [h] using hx0) },
by conv {to_rhs, rw [← nat.succ_le_iff, nat.succ_eq_add_one, ← hn2', ← zero_add (- x),
← zmod.nat_cast_self, ← sub_eq_add_neg, ← zmod.nat_cast_zmod_val x,
← nat.cast_sub (le_of_lt x.val_lt),
zmod.val_nat_cast, nat.mod_eq_of_lt hxn, nat.sub_le_sub_left_iff (le_of_lt x.val_lt)] }
end
lemma ne_neg_self (n : ℕ) [hn : fact ((n : ℕ) % 2 = 1)] {a : zmod n} (ha : a ≠ 0) : a ≠ -a :=
λ h, have a.val ≤ n / 2 ↔ (n : ℕ) / 2 < (-a).val := le_div_two_iff_lt_neg n ha,
by rwa [← h, ← not_lt, not_iff_self] at this
lemma neg_one_ne_one {n : ℕ} [fact (2 < n)] :
(-1 : zmod n) ≠ 1 :=
char_p.neg_one_ne_one (zmod n) n
@[simp] lemma neg_eq_self_mod_two : ∀ (a : zmod 2), -a = a := dec_trivial
@[simp] lemma nat_abs_mod_two (a : ℤ) : (a.nat_abs : zmod 2) = a :=
begin
cases a,
{ simp only [int.nat_abs_of_nat, int.cast_coe_nat, int.of_nat_eq_coe] },
{ simp only [neg_eq_self_mod_two, nat.cast_succ, int.nat_abs, int.cast_neg_succ_of_nat] }
end
@[simp] lemma val_eq_zero : ∀ {n : ℕ} (a : zmod n), a.val = 0 ↔ a = 0
| 0 a := int.nat_abs_eq_zero
| (n+1) a := by { rw fin.ext_iff, exact iff.rfl }
lemma val_cast_of_lt {n : ℕ} {a : ℕ} (h : a < n) : (a : zmod n).val = a :=
by rw [val_nat_cast, nat.mod_eq_of_lt h]
lemma neg_val' {n : ℕ} [fact (0 < n)] (a : zmod n) : (-a).val = (n - a.val) % n :=
begin
have : ((-a).val + a.val) % n = (n - a.val + a.val) % n,
{ rw [←val_add, add_left_neg, nat.sub_add_cancel (le_of_lt a.val_lt), nat.mod_self, val_zero], },
calc (-a).val = val (-a) % n : by rw nat.mod_eq_of_lt ((-a).val_lt)
... = (n - val a) % n : nat.modeq.modeq_add_cancel_right rfl this
end
lemma neg_val {n : ℕ} [fact (0 < n)] (a : zmod n) : (-a).val = if a = 0 then 0 else n - a.val :=
begin
rw neg_val',
by_cases h : a = 0, { rw [if_pos h, h, val_zero, nat.sub_zero, nat.mod_self] },
rw if_neg h,
apply nat.mod_eq_of_lt,
apply nat.sub_lt (fact.out (0 < n)),
contrapose! h,
rwa [nat.le_zero_iff, val_eq_zero] at h,
end
/-- `val_min_abs x` returns the integer in the same equivalence class as `x` that is closest to `0`,
The result will be in the interval `(-n/2, n/2]`. -/
def val_min_abs : Π {n : ℕ}, zmod n → ℤ
| 0 x := x
| n@(_+1) x := if x.val ≤ n / 2 then x.val else (x.val : ℤ) - n
@[simp] lemma val_min_abs_def_zero (x : zmod 0) : val_min_abs x = x := rfl
lemma val_min_abs_def_pos {n : ℕ} [fact (0 < n)] (x : zmod n) :
val_min_abs x = if x.val ≤ n / 2 then x.val else x.val - n :=
begin
casesI n,
{ exfalso, exact nat.not_lt_zero 0 (fact.out (0 < 0)) },
{ refl }
end
@[simp] lemma coe_val_min_abs : ∀ {n : ℕ} (x : zmod n), (x.val_min_abs : zmod n) = x
| 0 x := int.cast_id x
| k@(n+1) x :=
begin
rw val_min_abs_def_pos,
split_ifs,
{ rw [int.cast_coe_nat, nat_cast_zmod_val] },
{ rw [int.cast_sub, int.cast_coe_nat, nat_cast_zmod_val, int.cast_coe_nat, nat_cast_self,
sub_zero] }
end
lemma nat_abs_val_min_abs_le {n : ℕ} [fact (0 < n)] (x : zmod n) : x.val_min_abs.nat_abs ≤ n / 2 :=
begin
rw zmod.val_min_abs_def_pos,
split_ifs with h, { exact h },
have : (x.val - n : ℤ) ≤ 0,
{ rw [sub_nonpos, int.coe_nat_le], exact le_of_lt x.val_lt, },
rw [← int.coe_nat_le, int.of_nat_nat_abs_of_nonpos this, neg_sub],
conv_lhs { congr, rw [← nat.mod_add_div n 2, int.coe_nat_add, int.coe_nat_mul,
int.coe_nat_bit0, int.coe_nat_one] },
suffices : ((n % 2 : ℕ) + (n / 2) : ℤ) ≤ (val x),
{ rw ← sub_nonneg at this ⊢, apply le_trans this (le_of_eq _), ring_nf, ring },
norm_cast,
calc (n : ℕ) % 2 + n / 2 ≤ 1 + n / 2 :
nat.add_le_add_right (nat.le_of_lt_succ (nat.mod_lt _ dec_trivial)) _
... ≤ x.val :
by { rw add_comm, exact nat.succ_le_of_lt (lt_of_not_ge h) }
end
@[simp] lemma val_min_abs_zero : ∀ n, (0 : zmod n).val_min_abs = 0
| 0 := by simp only [val_min_abs_def_zero]
| (n+1) := by simp only [val_min_abs_def_pos, if_true, int.coe_nat_zero, zero_le, val_zero]
@[simp] lemma val_min_abs_eq_zero {n : ℕ} (x : zmod n) :
x.val_min_abs = 0 ↔ x = 0 :=
begin
cases n, { simp },
split,
{ simp only [val_min_abs_def_pos, int.coe_nat_succ],
split_ifs with h h; assume h0,
{ apply val_injective, rwa [int.coe_nat_eq_zero] at h0, },
{ apply absurd h0, rw sub_eq_zero, apply ne_of_lt, exact_mod_cast x.val_lt } },
{ rintro rfl, rw val_min_abs_zero }
end
lemma nat_cast_nat_abs_val_min_abs {n : ℕ} [fact (0 < n)] (a : zmod n) :
(a.val_min_abs.nat_abs : zmod n) = if a.val ≤ (n : ℕ) / 2 then a else -a :=
begin
have : (a.val : ℤ) - n ≤ 0,
by { erw [sub_nonpos, int.coe_nat_le], exact le_of_lt a.val_lt, },
rw [zmod.val_min_abs_def_pos],
split_ifs,
{ rw [int.nat_abs_of_nat, nat_cast_zmod_val] },
{ rw [← int.cast_coe_nat, int.of_nat_nat_abs_of_nonpos this, int.cast_neg, int.cast_sub],
rw [int.cast_coe_nat, int.cast_coe_nat, nat_cast_self, sub_zero, nat_cast_zmod_val], }
end
@[simp] lemma nat_abs_val_min_abs_neg {n : ℕ} (a : zmod n) :
(-a).val_min_abs.nat_abs = a.val_min_abs.nat_abs :=
begin
cases n, { simp only [int.nat_abs_neg, val_min_abs_def_zero], },
by_cases ha0 : a = 0, { rw [ha0, neg_zero] },
by_cases haa : -a = a, { rw [haa] },
suffices hpa : (n+1 : ℕ) - a.val ≤ (n+1) / 2 ↔ (n+1 : ℕ) / 2 < a.val,
{ rw [val_min_abs_def_pos, val_min_abs_def_pos],
rw ← not_le at hpa,
simp only [if_neg ha0, neg_val, hpa, int.coe_nat_sub (le_of_lt a.val_lt)],
split_ifs,
all_goals { rw [← int.nat_abs_neg], congr' 1, ring } },
suffices : (((n+1 : ℕ) % 2) + 2 * ((n + 1) / 2)) - a.val ≤ (n+1) / 2 ↔ (n+1 : ℕ) / 2 < a.val,
by rwa [nat.mod_add_div] at this,
suffices : (n + 1) % 2 + (n + 1) / 2 ≤ val a ↔ (n + 1) / 2 < val a,
by rw [nat.sub_le_iff, two_mul, ← add_assoc, nat.add_sub_cancel, this],
cases (n + 1 : ℕ).mod_two_eq_zero_or_one with hn0 hn1,
{ split,
{ assume h,
apply lt_of_le_of_ne (le_trans (nat.le_add_left _ _) h),
contrapose! haa,
rw [← zmod.nat_cast_zmod_val a, ← haa, neg_eq_iff_add_eq_zero, ← nat.cast_add],
rw [char_p.cast_eq_zero_iff (zmod (n+1)) (n+1)],
rw [← two_mul, ← zero_add (2 * _), ← hn0, nat.mod_add_div] },
{ rw [hn0, zero_add], exact le_of_lt } },
{ rw [hn1, add_comm, nat.succ_le_iff] }
end
lemma val_eq_ite_val_min_abs {n : ℕ} [fact (0 < n)] (a : zmod n) :
(a.val : ℤ) = a.val_min_abs + if a.val ≤ n / 2 then 0 else n :=
by { rw [zmod.val_min_abs_def_pos], split_ifs; simp only [add_zero, sub_add_cancel] }
lemma prime_ne_zero (p q : ℕ) [hp : fact p.prime] [hq : fact q.prime] (hpq : p ≠ q) :
(q : zmod p) ≠ 0 :=
by rwa [← nat.cast_zero, ne.def, eq_iff_modeq_nat, nat.modeq.modeq_zero_iff,
← hp.1.coprime_iff_not_dvd, nat.coprime_primes hp.1 hq.1]
end zmod
namespace zmod
variables (p : ℕ) [fact p.prime]
private lemma mul_inv_cancel_aux (a : zmod p) (h : a ≠ 0) : a * a⁻¹ = 1 :=
begin
obtain ⟨k, rfl⟩ := nat_cast_zmod_surjective a,
apply coe_mul_inv_eq_one,
apply nat.coprime.symm,
rwa [nat.prime.coprime_iff_not_dvd (fact.out p.prime), ← char_p.cast_eq_zero_iff (zmod p)]
end
/-- Field structure on `zmod p` if `p` is prime. -/
instance : field (zmod p) :=
{ mul_inv_cancel := mul_inv_cancel_aux p,
inv_zero := inv_zero p,
.. zmod.comm_ring p,
.. zmod.has_inv p,
.. zmod.nontrivial p }
end zmod
lemma ring_hom.ext_zmod {n : ℕ} {R : Type*} [semiring R] (f g : (zmod n) →+* R) : f = g :=
begin
ext a,
obtain ⟨k, rfl⟩ := zmod.int_cast_surjective a,
let φ : ℤ →+* R := f.comp (int.cast_ring_hom (zmod n)),
let ψ : ℤ →+* R := g.comp (int.cast_ring_hom (zmod n)),
show φ k = ψ k,
rw φ.ext_int ψ,
end
namespace zmod
variables {n : ℕ} {R : Type*}
instance subsingleton_ring_hom [semiring R] : subsingleton ((zmod n) →+* R) :=
⟨ring_hom.ext_zmod⟩
instance subsingleton_ring_equiv [semiring R] : subsingleton (zmod n ≃+* R) :=
⟨λ f g, by { rw ring_equiv.coe_ring_hom_inj_iff, apply ring_hom.ext_zmod _ _ }⟩
@[simp] lemma ring_hom_map_cast [ring R] (f : R →+* (zmod n)) (k : zmod n) :
f k = k :=
by { cases n; simp }
lemma ring_hom_right_inverse [ring R] (f : R →+* (zmod n)) :
function.right_inverse (coe : zmod n → R) f :=
ring_hom_map_cast f
lemma ring_hom_surjective [ring R] (f : R →+* (zmod n)) : function.surjective f :=
(ring_hom_right_inverse f).surjective
lemma ring_hom_eq_of_ker_eq [comm_ring R] (f g : R →+* (zmod n))
(h : f.ker = g.ker) : f = g :=
begin
have := f.lift_of_right_inverse_comp _ (zmod.ring_hom_right_inverse f) ⟨g, le_of_eq h⟩,
rw subtype.coe_mk at this,
rw [←this, ring_hom.ext_zmod (f.lift_of_right_inverse _ _ _) (ring_hom.id _), ring_hom.id_comp],
end
end zmod
|
55c6cfe57d44613cc4dbe4d0ae2de601765b613a | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/group_theory/nilpotent.lean | 82ccce8fd1bb60e79d3b596007b418dac80c2628 | [
"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 | 30,124 | lean | /-
Copyright (c) 2021 Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Ines Wright, Joachim Breitner
-/
import group_theory.general_commutator
import group_theory.quotient_group
import group_theory.solvable
import group_theory.p_group
/-!
# Nilpotent groups
An API for nilpotent groups, that is, groups for which the upper central series
reaches `⊤`.
## Main definitions
Recall that if `H K : subgroup G` then `⁅H, K⁆ : subgroup G` is the subgroup of `G` generated
by the commutators `hkh⁻¹k⁻¹`. Recall also Lean's conventions that `⊤` denotes the
subgroup `G` of `G`, and `⊥` denotes the trivial subgroup `{1}`.
* `upper_central_series G : ℕ → subgroup G` : the upper central series of a group `G`.
This is an increasing sequence of normal subgroups `H n` of `G` with `H 0 = ⊥` and
`H (n + 1) / H n` is the centre of `G / H n`.
* `lower_central_series G : ℕ → subgroup G` : the lower central series of a group `G`.
This is a decreasing sequence of normal subgroups `H n` of `G` with `H 0 = ⊤` and
`H (n + 1) = ⁅H n, G⁆`.
* `is_nilpotent` : A group G is nilpotent if its upper central series reaches `⊤`, or
equivalently if its lower central series reaches `⊥`.
* `nilpotency_class` : the length of the upper central series of a nilpotent group.
* `is_ascending_central_series (H : ℕ → subgroup G) : Prop` and
* `is_descending_central_series (H : ℕ → subgroup G) : Prop` : Note that in the literature
a "central series" for a group is usually defined to be a *finite* sequence of normal subgroups
`H 0`, `H 1`, ..., starting at `⊤`, finishing at `⊥`, and with each `H n / H (n + 1)`
central in `G / H (n + 1)`. In this formalisation it is convenient to have two weaker predicates
on an infinite sequence of subgroups `H n` of `G`: we say a sequence is a *descending central
series* if it starts at `G` and `⁅H n, ⊤⁆ ⊆ H (n + 1)` for all `n`. Note that this series
may not terminate at `⊥`, and the `H i` need not be normal. Similarly a sequence is an
*ascending central series* if `H 0 = ⊥` and `⁅H (n + 1), ⊤⁆ ⊆ H n` for all `n`, again with no
requirement that the series reaches `⊤` or that the `H i` are normal.
## Main theorems
`G` is *defined* to be nilpotent if the upper central series reaches `⊤`.
* `nilpotent_iff_finite_ascending_central_series` : `G` is nilpotent iff some ascending central
series reaches `⊤`.
* `nilpotent_iff_finite_descending_central_series` : `G` is nilpotent iff some descending central
series reaches `⊥`.
* `nilpotent_iff_lower` : `G` is nilpotent iff the lower central series reaches `⊥`.
* The `nilpotency_class` can likeways be obtained from these equivalent
definitions, see `least_ascending_central_series_length_eq_nilpotency_class`,
`least_descending_central_series_length_eq_nilpotency_class` and
`lower_central_series_length_eq_nilpotency_class`.
* If `G` is nilpotent, then so are its subgroups, images, quotients and preimages.
Corresponding lemmas about the `nilpotency_class` are provided.
* The `nilpotency_class` of `G ⧸ center G` is given explicitly, and an induction principle
is derived from that.
* `is_nilpotent.to_is_solvable`: If `G` is nilpotent, it is solvable.
## Warning
A "central series" is usually defined to be a finite sequence of normal subgroups going
from `⊥` to `⊤` with the property that each subquotient is contained within the centre of
the associated quotient of `G`. This means that if `G` is not nilpotent, then
none of what we have called `upper_central_series G`, `lower_central_series G` or
the sequences satisfying `is_ascending_central_series` or `is_descending_central_series`
are actually central series. Note that the fact that the upper and lower central series
are not central series if `G` is not nilpotent is a standard abuse of notation.
-/
open subgroup
variables {G : Type*} [group G] (H : subgroup G) [normal H]
/-- If `H` is a normal subgroup of `G`, then the set `{x : G | ∀ y : G, x*y*x⁻¹*y⁻¹ ∈ H}`
is a subgroup of `G` (because it is the preimage in `G` of the centre of the
quotient group `G/H`.)
-/
def upper_central_series_step : subgroup G :=
{ carrier := {x : G | ∀ y : G, x * y * x⁻¹ * y⁻¹ ∈ H},
one_mem' := λ y, by simp [subgroup.one_mem],
mul_mem' := λ a b ha hb y, begin
convert subgroup.mul_mem _ (ha (b * y * b⁻¹)) (hb y) using 1,
group,
end,
inv_mem' := λ x hx y, begin
specialize hx y⁻¹,
rw [mul_assoc, inv_inv] at ⊢ hx,
exact subgroup.normal.mem_comm infer_instance hx,
end }
lemma mem_upper_central_series_step (x : G) :
x ∈ upper_central_series_step H ↔ ∀ y, x * y * x⁻¹ * y⁻¹ ∈ H := iff.rfl
open quotient_group
/-- The proof that `upper_central_series_step H` is the preimage of the centre of `G/H` under
the canonical surjection. -/
lemma upper_central_series_step_eq_comap_center :
upper_central_series_step H = subgroup.comap (mk' H) (center (G ⧸ H)) :=
begin
ext,
rw [mem_comap, mem_center_iff, forall_coe],
apply forall_congr,
intro y,
rw [coe_mk', ←quotient_group.coe_mul, ←quotient_group.coe_mul, eq_comm, eq_iff_div_mem,
div_eq_mul_inv, mul_inv_rev, mul_assoc],
end
instance : normal (upper_central_series_step H) :=
begin
rw upper_central_series_step_eq_comap_center,
apply_instance,
end
variable (G)
/-- An auxiliary type-theoretic definition defining both the upper central series of
a group, and a proof that it is normal, all in one go. -/
def upper_central_series_aux : ℕ → Σ' (H : subgroup G), normal H
| 0 := ⟨⊥, infer_instance⟩
| (n + 1) := let un := upper_central_series_aux n, un_normal := un.2 in
by exactI ⟨upper_central_series_step un.1, infer_instance⟩
/-- `upper_central_series G n` is the `n`th term in the upper central series of `G`. -/
def upper_central_series (n : ℕ) : subgroup G := (upper_central_series_aux G n).1
instance (n : ℕ) : normal (upper_central_series G n) := (upper_central_series_aux G n).2
@[simp] lemma upper_central_series_zero : upper_central_series G 0 = ⊥ := rfl
@[simp] lemma upper_central_series_one : upper_central_series G 1 = center G :=
begin
ext,
simp only [upper_central_series, upper_central_series_aux, upper_central_series_step, center,
set.center, mem_mk, mem_bot, set.mem_set_of_eq],
exact forall_congr (λ y, by rw [mul_inv_eq_one, mul_inv_eq_iff_eq_mul, eq_comm]),
end
/-- The `n+1`st term of the upper central series `H i` has underlying set equal to the `x` such
that `⁅x,G⁆ ⊆ H n`-/
lemma mem_upper_central_series_succ_iff (n : ℕ) (x : G) :
x ∈ upper_central_series G (n + 1) ↔
∀ y : G, x * y * x⁻¹ * y⁻¹ ∈ upper_central_series G n := iff.rfl
-- is_nilpotent is already defined in the root namespace (for elements of rings).
/-- A group `G` is nilpotent if its upper central series is eventually `G`. -/
class group.is_nilpotent (G : Type*) [group G] : Prop :=
(nilpotent [] : ∃ n : ℕ, upper_central_series G n = ⊤)
open group
variable {G}
/-- A sequence of subgroups of `G` is an ascending central series if `H 0` is trivial and
`⁅H (n + 1), G⁆ ⊆ H n` for all `n`. Note that we do not require that `H n = G` for some `n`. -/
def is_ascending_central_series (H : ℕ → subgroup G) : Prop :=
H 0 = ⊥ ∧ ∀ (x : G) (n : ℕ), x ∈ H (n + 1) → ∀ g, x * g * x⁻¹ * g⁻¹ ∈ H n
/-- A sequence of subgroups of `G` is a descending central series if `H 0` is `G` and
`⁅H n, G⁆ ⊆ H (n + 1)` for all `n`. Note that we do not requre that `H n = {1}` for some `n`. -/
def is_descending_central_series (H : ℕ → subgroup G) := H 0 = ⊤ ∧
∀ (x : G) (n : ℕ), x ∈ H n → ∀ g, x * g * x⁻¹ * g⁻¹ ∈ H (n + 1)
/-- Any ascending central series for a group is bounded above by the upper central series. -/
lemma ascending_central_series_le_upper (H : ℕ → subgroup G) (hH : is_ascending_central_series H) :
∀ n : ℕ, H n ≤ upper_central_series G n
| 0 := hH.1.symm ▸ le_refl ⊥
| (n + 1) := begin
intros x hx,
rw mem_upper_central_series_succ_iff,
exact λ y, ascending_central_series_le_upper n (hH.2 x n hx y),
end
variable (G)
/-- The upper central series of a group is an ascending central series. -/
lemma upper_central_series_is_ascending_central_series :
is_ascending_central_series (upper_central_series G) :=
⟨rfl, λ x n h, h⟩
lemma upper_central_series_mono : monotone (upper_central_series G) :=
begin
refine monotone_nat_of_le_succ _,
intros n x hx y,
rw [mul_assoc, mul_assoc, ← mul_assoc y x⁻¹ y⁻¹],
exact mul_mem (upper_central_series G n) hx
(normal.conj_mem (upper_central_series.subgroup.normal G n) x⁻¹ (inv_mem _ hx) y),
end
/-- A group `G` is nilpotent iff there exists an ascending central series which reaches `G` in
finitely many steps. -/
theorem nilpotent_iff_finite_ascending_central_series :
is_nilpotent G ↔ ∃ n : ℕ, ∃ H : ℕ → subgroup G, is_ascending_central_series H ∧ H n = ⊤ :=
begin
split,
{ rintro ⟨n, nH⟩,
refine ⟨_, _, upper_central_series_is_ascending_central_series G, nH⟩ },
{ rintro ⟨n, H, hH, hn⟩,
use n,
rw [eq_top_iff, ←hn],
exact ascending_central_series_le_upper H hH n }
end
lemma is_decending_rev_series_of_is_ascending
{H: ℕ → subgroup G} {n : ℕ} (hn : H n = ⊤) (hasc : is_ascending_central_series H) :
is_descending_central_series (λ (m : ℕ), H (n - m)) :=
begin
cases hasc with h0 hH,
refine ⟨hn, λ x m hx g, _⟩,
dsimp at hx,
by_cases hm : n ≤ m,
{ rw [tsub_eq_zero_of_le hm, h0, subgroup.mem_bot] at hx,
subst hx,
convert subgroup.one_mem _,
group },
{ push_neg at hm,
apply hH,
convert hx,
rw [tsub_add_eq_add_tsub (nat.succ_le_of_lt hm), nat.succ_sub_succ] },
end
lemma is_ascending_rev_series_of_is_descending
{H: ℕ → subgroup G} {n : ℕ} (hn : H n = ⊥) (hdesc : is_descending_central_series H) :
is_ascending_central_series (λ (m : ℕ), H (n - m)) :=
begin
cases hdesc with h0 hH,
refine ⟨hn, λ x m hx g, _⟩,
dsimp only at hx ⊢,
by_cases hm : n ≤ m,
{ have hnm : n - m = 0 := tsub_eq_zero_iff_le.mpr hm,
rw [hnm, h0],
exact mem_top _ },
{ push_neg at hm,
convert hH x _ hx g,
rw [tsub_add_eq_add_tsub (nat.succ_le_of_lt hm), nat.succ_sub_succ] },
end
/-- A group `G` is nilpotent iff there exists a descending central series which reaches the
trivial group in a finite time. -/
theorem nilpotent_iff_finite_descending_central_series :
is_nilpotent G ↔ ∃ n : ℕ, ∃ H : ℕ → subgroup G, is_descending_central_series H ∧ H n = ⊥ :=
begin
rw nilpotent_iff_finite_ascending_central_series,
split,
{ rintro ⟨n, H, hH, hn⟩,
refine ⟨n, λ m, H (n - m), is_decending_rev_series_of_is_ascending G hn hH, _⟩,
rw tsub_self,
exact hH.1 },
{ rintro ⟨n, H, hH, hn⟩,
refine ⟨n, λ m, H (n - m), is_ascending_rev_series_of_is_descending G hn hH, _⟩,
rw tsub_self,
exact hH.1 },
end
/-- The lower central series of a group `G` is a sequence `H n` of subgroups of `G`, defined
by `H 0` is all of `G` and for `n≥1`, `H (n + 1) = ⁅H n, G⁆` -/
def lower_central_series (G : Type*) [group G] : ℕ → subgroup G
| 0 := ⊤
| (n+1) := ⁅lower_central_series n, ⊤⁆
variable {G}
@[simp] lemma lower_central_series_zero : lower_central_series G 0 = ⊤ := rfl
@[simp] lemma lower_central_series_one : lower_central_series G 1 = commutator G :=
by simp [lower_central_series]
lemma mem_lower_central_series_succ_iff (n : ℕ) (q : G) :
q ∈ lower_central_series G (n + 1) ↔
q ∈ closure {x | ∃ (p ∈ lower_central_series G n) (q ∈ (⊤ : subgroup G)), p * q * p⁻¹ * q⁻¹ = x}
:= iff.rfl
lemma lower_central_series_succ (n : ℕ) :
lower_central_series G (n + 1) =
closure {x | ∃ (p ∈ lower_central_series G n) (q ∈ (⊤ : subgroup G)), p * q * p⁻¹ * q⁻¹ = x} :=
rfl
instance (n : ℕ) : normal (lower_central_series G n) :=
begin
induction n with d hd,
{ exact (⊤ : subgroup G).normal_of_characteristic },
{ exactI general_commutator_normal (lower_central_series G d) ⊤ },
end
lemma lower_central_series_antitone :
antitone (lower_central_series G) :=
begin
refine antitone_nat_of_succ_le (λ n x hx, _),
simp only [mem_lower_central_series_succ_iff, exists_prop, mem_top, exists_true_left, true_and]
at hx,
refine closure_induction hx _ (subgroup.one_mem _) (@subgroup.mul_mem _ _ _)
(@subgroup.inv_mem _ _ _),
rintros y ⟨z, hz, a, ha⟩,
rw [← ha, mul_assoc, mul_assoc, ← mul_assoc a z⁻¹ a⁻¹],
exact mul_mem (lower_central_series G n) hz
(normal.conj_mem (lower_central_series.subgroup.normal n) z⁻¹ (inv_mem _ hz) a),
end
/-- The lower central series of a group is a descending central series. -/
theorem lower_central_series_is_descending_central_series :
is_descending_central_series (lower_central_series G) :=
begin
split, refl,
intros x n hxn g,
exact general_commutator_containment _ _ hxn (subgroup.mem_top g),
end
/-- Any descending central series for a group is bounded below by the lower central series. -/
lemma descending_central_series_ge_lower (H : ℕ → subgroup G)
(hH : is_descending_central_series H) : ∀ n : ℕ, lower_central_series G n ≤ H n
| 0 := hH.1.symm ▸ le_refl ⊤
| (n + 1) := begin
specialize descending_central_series_ge_lower n,
apply (general_commutator_le _ _ _).2,
intros x hx q _,
exact hH.2 x n (descending_central_series_ge_lower hx) q,
end
/-- A group is nilpotent if and only if its lower central series eventually reaches
the trivial subgroup. -/
theorem nilpotent_iff_lower_central_series : is_nilpotent G ↔ ∃ n, lower_central_series G n = ⊥ :=
begin
rw nilpotent_iff_finite_descending_central_series,
split,
{ rintro ⟨n, H, ⟨h0, hs⟩, hn⟩,
use n,
rw [eq_bot_iff, ←hn],
exact descending_central_series_ge_lower H ⟨h0, hs⟩ n },
{ rintro ⟨n, hn⟩,
exact ⟨n, lower_central_series G, lower_central_series_is_descending_central_series, hn⟩ },
end
section classical
open_locale classical
variables [hG : is_nilpotent G]
include hG
variable (G)
/-- The nilpotency class of a nilpotent group is the smallest natural `n` such that
the `n`'th term of the upper central series is `G`. -/
noncomputable def group.nilpotency_class : ℕ :=
nat.find (is_nilpotent.nilpotent G)
variable {G}
@[simp]
lemma upper_central_series_nilpotency_class :
upper_central_series G (group.nilpotency_class G) = ⊤ :=
nat.find_spec (is_nilpotent.nilpotent G)
lemma upper_central_series_eq_top_iff_nilpotency_class_le {n : ℕ} :
(upper_central_series G n = ⊤) ↔ (group.nilpotency_class G ≤ n) :=
begin
split,
{ intro h,
exact (nat.find_le h), },
{ intro h,
apply eq_top_iff.mpr,
rw ← upper_central_series_nilpotency_class,
exact (upper_central_series_mono _ h), }
end
/-- The nilpotency class of a nilpotent `G` is equal to the smallest `n` for which an ascending
central series reaches `G` in its `n`'th term. -/
lemma least_ascending_central_series_length_eq_nilpotency_class :
nat.find ((nilpotent_iff_finite_ascending_central_series G).mp hG) = group.nilpotency_class G :=
begin
refine le_antisymm (nat.find_mono _) (nat.find_mono _),
{ intros n hn,
exact ⟨upper_central_series G, upper_central_series_is_ascending_central_series G, hn ⟩, },
{ rintros n ⟨H, ⟨hH, hn⟩⟩,
rw [←top_le_iff, ←hn],
exact ascending_central_series_le_upper H hH n, }
end
/-- The nilpotency class of a nilpotent `G` is equal to the smallest `n` for which the descending
central series reaches `⊥` in its `n`'th term. -/
lemma least_descending_central_series_length_eq_nilpotency_class :
nat.find ((nilpotent_iff_finite_descending_central_series G).mp hG) = group.nilpotency_class G :=
begin
rw ← least_ascending_central_series_length_eq_nilpotency_class,
refine le_antisymm (nat.find_mono _) (nat.find_mono _),
{ rintros n ⟨H, ⟨hH, hn⟩⟩,
refine ⟨(λ m, H (n - m)), is_decending_rev_series_of_is_ascending G hn hH, _⟩,
rw tsub_self,
exact hH.1 },
{ rintros n ⟨H, ⟨hH, hn⟩⟩,
refine ⟨(λ m, H (n - m)), is_ascending_rev_series_of_is_descending G hn hH, _⟩,
rw tsub_self,
exact hH.1 },
end
/-- The nilpotency class of a nilpotent `G` is equal to the length of the lower central series. -/
lemma lower_central_series_length_eq_nilpotency_class :
nat.find (nilpotent_iff_lower_central_series.mp hG) = @group.nilpotency_class G _ _ :=
begin
rw ← least_descending_central_series_length_eq_nilpotency_class,
refine le_antisymm (nat.find_mono _) (nat.find_mono _),
{ rintros n ⟨H, ⟨hH, hn⟩⟩,
rw [←le_bot_iff, ←hn],
exact (descending_central_series_ge_lower H hH n), },
{ rintros n h,
exact ⟨lower_central_series G, ⟨lower_central_series_is_descending_central_series, h⟩⟩ },
end
@[simp]
lemma lower_central_series_nilpotency_class :
lower_central_series G (group.nilpotency_class G) = ⊥ :=
begin
rw ← lower_central_series_length_eq_nilpotency_class,
exact (nat.find_spec (nilpotent_iff_lower_central_series.mp _))
end
lemma lower_central_series_eq_bot_iff_nilpotency_class_le {n : ℕ} :
(lower_central_series G n = ⊥) ↔ (group.nilpotency_class G ≤ n) :=
begin
split,
{ intro h,
rw ← lower_central_series_length_eq_nilpotency_class,
exact (nat.find_le h), },
{ intro h,
apply eq_bot_iff.mpr,
rw ← lower_central_series_nilpotency_class,
exact (lower_central_series_antitone h), }
end
end classical
lemma lower_central_series_map_subtype_le (H : subgroup G) (n : ℕ) :
(lower_central_series H n).map H.subtype ≤ lower_central_series G n :=
begin
induction n with d hd,
{ simp },
{ rw [lower_central_series_succ, lower_central_series_succ, monoid_hom.map_closure],
apply subgroup.closure_mono,
rintros x1 ⟨x2, ⟨x3, hx3, x4, hx4, rfl⟩, rfl⟩,
exact ⟨x3, (hd (mem_map.mpr ⟨x3, hx3, rfl⟩)), x4, by simp⟩ }
end
/-- A subgroup of a nilpotent group is nilpotent -/
instance subgroup.is_nilpotent (H : subgroup G) [hG : is_nilpotent G] :
is_nilpotent H :=
begin
rw nilpotent_iff_lower_central_series at *,
rcases hG with ⟨n, hG⟩,
use n,
have := lower_central_series_map_subtype_le H n,
simp only [hG, set_like.le_def, mem_map, forall_apply_eq_imp_iff₂, exists_imp_distrib] at this,
exact eq_bot_iff.mpr (λ x hx, subtype.ext (this x hx)),
end
/-- A the nilpotency class of a subgroup is less or equal the the nilpotency class of the group -/
lemma subgroup.nilpotency_class_le (H : subgroup G) [hG : is_nilpotent G] :
group.nilpotency_class H ≤ group.nilpotency_class G :=
begin
repeat { rw ← lower_central_series_length_eq_nilpotency_class },
apply nat.find_mono,
intros n hG,
have := lower_central_series_map_subtype_le H n,
simp only [hG, set_like.le_def, mem_map, forall_apply_eq_imp_iff₂, exists_imp_distrib] at this,
exact eq_bot_iff.mpr (λ x hx, subtype.ext (this x hx)),
end
@[priority 100]
instance is_nilpotent_of_subsingleton [subsingleton G] : is_nilpotent G :=
nilpotent_iff_lower_central_series.2 ⟨0, subsingleton.elim ⊤ ⊥⟩
lemma upper_central_series.map {H : Type*} [group H] {f : G →* H} (h : function.surjective f)
(n : ℕ) : subgroup.map f (upper_central_series G n) ≤ upper_central_series H n :=
begin
induction n with d hd,
{ simp },
{ rintros _ ⟨x, hx : x ∈ upper_central_series G d.succ, rfl⟩ y',
rcases h y' with ⟨y, rfl⟩,
simpa using hd (mem_map_of_mem f (hx y)) }
end
lemma lower_central_series.map {H : Type*} [group H] (f : G →* H) (n : ℕ) :
subgroup.map f (lower_central_series G n) ≤ lower_central_series H n :=
begin
induction n with d hd,
{ simp [nat.nat_zero_eq_zero] },
{ rintros a ⟨x, hx : x ∈ lower_central_series G d.succ, rfl⟩,
refine closure_induction hx _ (by simp [f.map_one, subgroup.one_mem _])
(λ y z hy hz, by simp [monoid_hom.map_mul, subgroup.mul_mem _ hy hz])
(λ y hy, by simp [f.map_inv, subgroup.inv_mem _ hy]),
rintros a ⟨y, hy, z, ⟨-, rfl⟩⟩,
apply mem_closure.mpr,
exact λ K hK, hK ⟨f y, hd (mem_map_of_mem f hy), by simp⟩ }
end
lemma lower_central_series_succ_eq_bot {n : ℕ} (h : lower_central_series G n ≤ center G) :
lower_central_series G (n + 1) = ⊥ :=
begin
rw [lower_central_series_succ, closure_eq_bot_iff, set.subset_singleton_iff],
rintro x ⟨y, hy1, z, ⟨⟩, rfl⟩,
rw [mul_assoc, ←mul_inv_rev, mul_inv_eq_one, eq_comm],
exact mem_center_iff.mp (h hy1) z,
end
/-- The preimage of a nilpotent group is nilpotent if the kernel of the homomorphism is contained
in the center -/
lemma is_nilpotent_of_ker_le_center {H : Type*} [group H] (f : G →* H)
(hf1 : f.ker ≤ center G) (hH : is_nilpotent H) : is_nilpotent G :=
begin
rw nilpotent_iff_lower_central_series at *,
rcases hH with ⟨n, hn⟩,
use (n + 1),
refine lower_central_series_succ_eq_bot (le_trans ((map_eq_bot_iff _).mp _) hf1),
exact eq_bot_iff.mpr (hn ▸ (lower_central_series.map f n)),
end
section classical
open_locale classical
lemma nilpotency_class_le_of_ker_le_center {H : Type*} [group H] (f : G →* H)
(hf1 : f.ker ≤ center G) (hH : is_nilpotent H) :
@group.nilpotency_class G _ (is_nilpotent_of_ker_le_center f hf1 hH) ≤
group.nilpotency_class H + 1 :=
begin
rw ← lower_central_series_length_eq_nilpotency_class,
apply nat.find_min',
refine lower_central_series_succ_eq_bot (le_trans ((map_eq_bot_iff _).mp _) hf1),
apply eq_bot_iff.mpr,
apply (le_trans (lower_central_series.map f _)),
simp only [lower_central_series_nilpotency_class, le_bot_iff],
end
end classical
/-- The range of a surejctive homomorphism from a nilpotent group is nilpotent -/
lemma nilpotent_of_surjective {G' : Type*} [group G'] [h : is_nilpotent G]
(f : G →* G') (hf : function.surjective f) :
is_nilpotent G' :=
begin
unfreezingI { rcases h with ⟨n, hn⟩ },
use n,
apply eq_top_iff.mpr,
calc ⊤ = f.range : symm (f.range_top_of_surjective hf)
... = subgroup.map f ⊤ : monoid_hom.range_eq_map _
... = subgroup.map f (upper_central_series G n) : by rw hn
... ≤ upper_central_series G' n : upper_central_series.map hf n,
end
/-- The nilpotency class of the range of a surejctive homomorphism from a
nilpotent group is less or equal the nilpotency class of the domain -/
lemma nilpotency_class_le_of_surjective
{G' : Type*} [group G'] (f : G →* G') (hf : function.surjective f) [h : is_nilpotent G] :
@group.nilpotency_class G' _ (nilpotent_of_surjective _ hf) ≤
group.nilpotency_class G :=
begin
apply nat.find_mono,
intros n hn,
apply eq_top_iff.mpr,
calc ⊤ = f.range : symm (f.range_top_of_surjective hf)
... = subgroup.map f ⊤ : monoid_hom.range_eq_map _
... = subgroup.map f (upper_central_series G n) : by rw hn
... ≤ upper_central_series G' n : upper_central_series.map hf n,
end
/-- A quotient of a nilpotent group is nilpotent -/
instance nilpotent_quotient_of_nilpotent (H : subgroup G) [H.normal] [h : is_nilpotent G] :
is_nilpotent (G ⧸ H) :=
nilpotent_of_surjective _ (show function.surjective (quotient_group.mk' H), by tidy)
/-- The nilpotency class of a quotient of `G` is less or equal the nilpotency class of `G` -/
lemma nilpotency_class_quotient_le (H : subgroup G) [H.normal] [h : is_nilpotent G] :
group.nilpotency_class (G ⧸ H) ≤ group.nilpotency_class G := nilpotency_class_le_of_surjective _ _
-- This technical lemma helps with rewriting the subgroup, which occurs in indices
private lemma comap_center_subst {H₁ H₂ : subgroup G} [normal H₁] [normal H₂] (h : H₁ = H₂) :
comap (mk' H₁) (center (G ⧸ H₁)) = comap (mk' H₂) (center (G ⧸ H₂)) :=
by unfreezingI { subst h }
lemma comap_upper_central_series_quotient_center (n : ℕ) :
comap (mk' (center G)) (upper_central_series (G ⧸ center G) n) = upper_central_series G n.succ :=
begin
induction n with n ih,
{ simp, },
{ let Hn := upper_central_series (G ⧸ center G) n,
calc comap (mk' (center G)) (upper_central_series_step Hn)
= comap (mk' (center G)) (comap (mk' Hn) (center ((G ⧸ center G) ⧸ Hn))) :
by rw upper_central_series_step_eq_comap_center
... = comap (mk' (comap (mk' (center G)) Hn)) (center (G ⧸ (comap (mk' (center G)) Hn))) :
quotient_group.comap_comap_center
... = comap (mk' (upper_central_series G n.succ)) (center (G ⧸ upper_central_series G n.succ)) :
comap_center_subst ih
... = upper_central_series_step (upper_central_series G n.succ) :
symm (upper_central_series_step_eq_comap_center _), }
end
lemma nilpotency_class_zero_iff_subsingleton [is_nilpotent G] :
group.nilpotency_class G = 0 ↔ subsingleton G :=
by simp [group.nilpotency_class, nat.find_eq_zero, subsingleton_iff_bot_eq_top]
section classical
open_locale classical
/-- Quotienting the `center G` reduces the nilpotency class by 1 -/
lemma nilpotency_class_quotient_center [hH : is_nilpotent G] :
group.nilpotency_class (G ⧸ center G) = group.nilpotency_class G - 1 :=
begin
generalize hn : group.nilpotency_class G = n,
rcases n with rfl | n,
{ simp [nilpotency_class_zero_iff_subsingleton] at *,
haveI := hn,
apply_instance, },
{ suffices : group.nilpotency_class (G ⧸ center G) = n, by simpa,
apply le_antisymm,
{ apply upper_central_series_eq_top_iff_nilpotency_class_le.mp,
apply (@comap_injective G _ _ _ (mk' (center G)) (surjective_quot_mk _)),
rw [ comap_upper_central_series_quotient_center, comap_top, ← hn],
exact upper_central_series_nilpotency_class, },
{ apply le_of_add_le_add_right,
calc n + 1 = n.succ : rfl
... = group.nilpotency_class G : symm hn
... ≤ group.nilpotency_class (G ⧸ center G) + 1
: nilpotency_class_le_of_ker_le_center _ (le_of_eq (ker_mk _)) _, } }
end
/-- The nilpotency class of a non-trivial group is one more than its quotient by the center -/
lemma nilpotency_class_eq_quotient_center_plus_one [hH : is_nilpotent G] [nontrivial G] :
group.nilpotency_class G = group.nilpotency_class (G ⧸ center G) + 1 :=
begin
rw nilpotency_class_quotient_center,
rcases h : group.nilpotency_class G,
{ exfalso,
rw nilpotency_class_zero_iff_subsingleton at h, resetI,
apply (false_of_nontrivial_of_subsingleton G), },
{ simp }
end
/-- If the quotient by `center G` is nilpotent, then so is G. -/
lemma of_quotient_center_nilpotent (h : is_nilpotent (G ⧸ center G)) : is_nilpotent G :=
begin
obtain ⟨n, hn⟩ := h.nilpotent,
use n.succ,
simp [← comap_upper_central_series_quotient_center, hn],
end
end classical
/-- A custom induction principle for nilpotent groups. The base case is a trivial group
(`subsingleton G`), and in the induction step, one can assume the hypothesis for
the group quotiented by its center. -/
@[elab_as_eliminator]
lemma nilpotent_center_quotient_ind
{P : Π G [group G], by exactI ∀ [is_nilpotent G], Prop}
(G : Type*) [group G] [is_nilpotent G]
(hbase : ∀ G [group G] [subsingleton G], by exactI P G)
(hstep : ∀ G [group G], by exactI ∀ [is_nilpotent G], by exactI ∀ (ih : P (G ⧸ center G)), P G) :
P G :=
begin
obtain ⟨n, h⟩ : ∃ n, group.nilpotency_class G = n := ⟨ _, rfl⟩,
unfreezingI { induction n with n ih generalizing G },
{ haveI := nilpotency_class_zero_iff_subsingleton.mp h,
exact hbase _, },
{ have hn : group.nilpotency_class (G ⧸ center G) = n :=
by simp [nilpotency_class_quotient_center, h],
exact hstep _ (ih _ hn), },
end
lemma derived_le_lower_central (n : ℕ) : derived_series G n ≤ lower_central_series G n :=
by { induction n with i ih, { simp }, { apply general_commutator_mono ih, simp } }
/-- Abelian groups are nilpotent -/
@[priority 100]
instance comm_group.is_nilpotent {G : Type*} [comm_group G] : is_nilpotent G :=
begin
use 1,
rw upper_central_series_one,
apply comm_group.center_eq_top,
end
/-- Abelian groups have nilpotency class at most one -/
lemma comm_group.nilpotency_class_le_one {G : Type*} [comm_group G] :
group.nilpotency_class G ≤ 1 :=
begin
apply upper_central_series_eq_top_iff_nilpotency_class_le.mp,
rw upper_central_series_one,
apply comm_group.center_eq_top,
end
/-- Groups with nilpotency class at most one are abelian -/
def comm_group_of_nilpotency_class [is_nilpotent G] (h : group.nilpotency_class G ≤ 1) :
comm_group G :=
group.comm_group_of_center_eq_top $
begin
rw ← upper_central_series_one,
exact upper_central_series_eq_top_iff_nilpotency_class_le.mpr h,
end
/-- A nilpotent subgroup is solvable -/
@[priority 100]
instance is_nilpotent.to_is_solvable [h : is_nilpotent G]: is_solvable G :=
begin
obtain ⟨n, hn⟩ := nilpotent_iff_lower_central_series.1 h,
use n,
rw [eq_bot_iff, ←hn],
exact derived_le_lower_central n,
end
section classical
open_locale classical -- to get the fintype instance for quotient groups
/-- A p-group is nilpotent -/
lemma is_p_group.is_nilpotent {G : Type*} [hG : group G] [hf : fintype G]
{p : ℕ} (hp : fact (nat.prime p)) (h : is_p_group p G) :
is_nilpotent G :=
begin
unfreezingI
{ revert hG,
induction hf using fintype.induction_subsingleton_or_nontrivial with G hG hS G hG hN ih },
{ apply_instance, },
{ introI _, intro h,
have hc : center G > ⊥ := gt_iff_lt.mp h.bot_lt_center,
have hcq : fintype.card (G ⧸ center G) < fintype.card G,
{ rw card_eq_card_quotient_mul_card_subgroup (center G),
apply lt_mul_of_one_lt_right,
exact (fintype.card_pos_iff.mpr has_one.nonempty),
exact ((subgroup.one_lt_card_iff_ne_bot _).mpr (ne_of_gt hc)), },
have hnq : is_nilpotent (G ⧸ center G) := ih _ hcq (h.to_quotient (center G)),
exact (of_quotient_center_nilpotent hnq), }
end
end classical
|
cd34199e70fa219fe9243db3fe511d1a493e2c3d | cf39355caa609c0f33405126beee2739aa3cb77e | /library/init/control/lawful.lean | 2fb3c372b3f24b90214d2f554d53085f2744e9aa | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 11,185 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
prelude
import init.control.monad init.meta.interactive
import init.control.state init.control.except init.control.reader init.control.option
universes u v
open function
open tactic
meta def control_laws_tac := whnf_target >> intros >> to_expr ``(rfl) >>= exact
class is_lawful_functor (f : Type u → Type v) [functor f] : Prop :=
(map_const_eq : ∀ {α β : Type u}, ((<$) : α → f β → f α) = (<$>) ∘ const β . control_laws_tac)
-- `functor` is indeed a categorical functor
(id_map : Π {α : Type u} (x : f α), id <$> x = x)
(comp_map : Π {α β γ : Type u} (g : α → β) (h : β → γ) (x : f α), (h ∘ g) <$> x = h <$> g <$> x)
export is_lawful_functor (map_const_eq id_map comp_map)
attribute [simp] id_map
-- `comp_map` does not make a good simp lemma
class is_lawful_applicative (f : Type u → Type v) [applicative f] extends is_lawful_functor f : Prop :=
(seq_left_eq : ∀ {α β : Type u} (a : f α) (b : f β), a <* b = const β <$> a <*> b . control_laws_tac)
(seq_right_eq : ∀ {α β : Type u} (a : f α) (b : f β), a *> b = const α id <$> a <*> b . control_laws_tac)
-- applicative laws
(pure_seq_eq_map : ∀ {α β : Type u} (g : α → β) (x : f α), pure g <*> x = g <$> x)
(map_pure : ∀ {α β : Type u} (g : α → β) (x : α), g <$> (pure x : f α) = pure (g x))
(seq_pure : ∀ {α β : Type u} (g : f (α → β)) (x : α), g <*> pure x = (λ g : α → β, g x) <$> g)
(seq_assoc : ∀ {α β γ : Type u} (x : f α) (g : f (α → β)) (h : f (β → γ)), h <*> (g <*> x) = (@comp α β γ <$> h) <*> g <*> x)
-- default functor law
(comp_map := begin intros; simp [(pure_seq_eq_map _ _).symm, seq_assoc, map_pure, seq_pure] end)
export is_lawful_applicative (seq_left_eq seq_right_eq pure_seq_eq_map map_pure seq_pure seq_assoc)
attribute [simp] map_pure seq_pure
-- applicative "law" derivable from other laws
@[simp] theorem pure_id_seq {α : Type u} {f : Type u → Type v} [applicative f] [is_lawful_applicative f] (x : f α) : pure id <*> x = x :=
by simp [pure_seq_eq_map]
class is_lawful_monad (m : Type u → Type v) [monad m] extends is_lawful_applicative m : Prop :=
(bind_pure_comp_eq_map : ∀ {α β : Type u} (f : α → β) (x : m α), x >>= pure ∘ f = f <$> x . control_laws_tac)
(bind_map_eq_seq : ∀ {α β : Type u} (f : m (α → β)) (x : m α), f >>= (<$> x) = f <*> x . control_laws_tac)
-- monad laws
(pure_bind : ∀ {α β : Type u} (x : α) (f : α → m β), pure x >>= f = f x)
(bind_assoc : ∀ {α β γ : Type u} (x : m α) (f : α → m β) (g : β → m γ),
x >>= f >>= g = x >>= λ x, f x >>= g)
(pure_seq_eq_map := by intros; rw ←bind_map_eq_seq; simp [pure_bind])
(map_pure := by intros; rw ←bind_pure_comp_eq_map; simp [pure_bind])
(seq_pure := by intros; rw ←bind_map_eq_seq; simp [map_pure, bind_pure_comp_eq_map])
(seq_assoc := by intros; simp [(bind_pure_comp_eq_map _ _).symm,
(bind_map_eq_seq _ _).symm,
bind_assoc, pure_bind])
export is_lawful_monad (bind_pure_comp_eq_map bind_map_eq_seq pure_bind bind_assoc)
attribute [simp] pure_bind
-- monad "law" derivable from other laws
@[simp] theorem bind_pure {α : Type u} {m : Type u → Type v} [monad m] [is_lawful_monad m] (x : m α) : x >>= pure = x :=
show x >>= pure ∘ id = x, by rw bind_pure_comp_eq_map; simp [id_map]
lemma bind_ext_congr {α β} {m : Type u → Type v} [has_bind m] {x : m α} {f g : α → m β} :
(∀ a, f a = g a) →
x >>= f = x >>= g :=
λ h, by simp [show f = g, from funext h]
lemma map_ext_congr {α β} {m : Type u → Type v} [functor m] {x : m α} {f g : α → β} :
(∀ a, f a = g a) →
(f <$> x : m β) = g <$> x :=
λ h, by simp [show f = g, from funext h]
-- instances of previously defined monads
namespace id
variables {α β : Type}
@[simp] lemma map_eq (x : id α) (f : α → β) : f <$> x = f x := rfl
@[simp] lemma bind_eq (x : id α) (f : α → id β) : x >>= f = f x := rfl
@[simp] lemma pure_eq (a : α) : (pure a : id α) = a := rfl
end id
instance : is_lawful_monad id :=
by refine { .. }; intros; refl
namespace state_t
section
variable {σ : Type u}
variable {m : Type u → Type v}
variables {α β : Type u}
variables (x : state_t σ m α) (st : σ)
lemma ext {x x' : state_t σ m α} (h : ∀ st, x.run st = x'.run st) : x = x' :=
by cases x; cases x'; simp [show x = x', from funext h]
variable [monad m]
@[simp] lemma run_pure (a) : (pure a : state_t σ m α).run st = pure (a, st) := rfl
@[simp] lemma run_bind (f : α → state_t σ m β) :
(x >>= f).run st = x.run st >>= λ p, (f p.1).run p.2 :=
by apply bind_ext_congr; intro a; cases a; simp [state_t.bind, state_t.run]
@[simp] lemma run_map (f : α → β) [is_lawful_monad m] :
(f <$> x).run st = (λ p : α × σ, (f (prod.fst p), prod.snd p)) <$> x.run st :=
begin
rw ← bind_pure_comp_eq_map _ (x.run st),
change (x >>= pure ∘ f).run st = _,
simp
end
@[simp] lemma run_monad_lift {n} [has_monad_lift_t n m] (x : n α) :
(monad_lift x : state_t σ m α).run st = do a ← (monad_lift x : m α), pure (a, st) := rfl
@[simp] lemma run_monad_map {m' n n'} [monad m'] [monad_functor_t n n' m m'] (f : ∀ {α}, n α → n' α) :
(monad_map @f x : state_t σ m' α).run st = monad_map @f (x.run st) := rfl
@[simp] lemma run_adapt {σ' σ''} (st : σ) (split : σ → σ' × σ'') (join : σ' → σ'' → σ)
(x : state_t σ' m α) :
(state_t.adapt split join x : state_t σ m α).run st =
do let (st, ctx) := split st,
(a, st') ← x.run st,
pure (a, join st' ctx) :=
by delta state_t.adapt; refl
@[simp] lemma run_get : (state_t.get : state_t σ m σ).run st = pure (st, st) := rfl
@[simp] lemma run_put (st') : (state_t.put st' : state_t σ m _).run st = pure (punit.star, st') := rfl
end
end state_t
instance (m : Type u → Type v) [monad m] [is_lawful_monad m] (σ : Type u) : is_lawful_monad (state_t σ m) :=
{ id_map := by intros; apply state_t.ext; intro; simp; erw id_map,
pure_bind := by { intros, apply state_t.ext, simp },
bind_assoc := by { intros, apply state_t.ext, simp [bind_assoc] } }
namespace except_t
variables {α β ε : Type u} {m : Type u → Type v} (x : except_t ε m α)
lemma ext {x x' : except_t ε m α} (h : x.run = x'.run) : x = x' :=
by cases x; cases x'; simp * at *
variable [monad m]
@[simp] lemma run_pure (a) : (pure a : except_t ε m α).run = pure (@except.ok ε α a) := rfl
@[simp] lemma run_bind (f : α → except_t ε m β) : (x >>= f).run = x.run >>= except_t.bind_cont f :=
rfl
@[simp] lemma run_map (f : α → β) [is_lawful_monad m] : (f <$> x).run = except.map f <$> x.run :=
begin
rw ← bind_pure_comp_eq_map _ x.run,
change x.run >>= except_t.bind_cont (pure ∘ f) = _,
apply bind_ext_congr,
intro a; cases a; simp [except_t.bind_cont, except.map]
end
@[simp] lemma run_monad_lift {n} [has_monad_lift_t n m] (x : n α) :
(monad_lift x : except_t ε m α).run = except.ok <$> (monad_lift x : m α) := rfl
@[simp] lemma run_monad_map {m' n n'} [monad m'] [monad_functor_t n n' m m'] (f : ∀ {α}, n α → n' α) :
(monad_map @f x : except_t ε m' α).run = monad_map @f x.run := rfl
end except_t
instance (m : Type u → Type v) [monad m] [is_lawful_monad m] (ε : Type u) : is_lawful_monad (except_t ε m) :=
{ id_map := begin
intros, apply except_t.ext, simp only [except_t.run_map],
rw [map_ext_congr, id_map],
intro a, cases a; refl
end,
bind_pure_comp_eq_map := begin
intros, apply except_t.ext, simp only [except_t.run_map, except_t.run_bind],
rw [bind_ext_congr, bind_pure_comp_eq_map],
intro a, cases a; refl
end,
bind_assoc := begin
intros, apply except_t.ext, simp only [except_t.run_bind, bind_assoc],
rw [bind_ext_congr],
intro a, cases a; simp [except_t.bind_cont]
end,
pure_bind := by intros; apply except_t.ext; simp [except_t.bind_cont] }
namespace reader_t
section
variable {ρ : Type u}
variable {m : Type u → Type v}
variables {α β : Type u}
variables (x : reader_t ρ m α) (r : ρ)
lemma ext {x x' : reader_t ρ m α} (h : ∀ r, x.run r = x'.run r) : x = x' :=
by cases x; cases x'; simp [show x = x', from funext h]
variable [monad m]
@[simp] lemma run_pure (a) : (pure a : reader_t ρ m α).run r = pure a := rfl
@[simp] lemma run_bind (f : α → reader_t ρ m β) :
(x >>= f).run r = x.run r >>= λ a, (f a).run r := rfl
@[simp] lemma run_map (f : α → β) [is_lawful_monad m] : (f <$> x).run r = f <$> x.run r :=
by rw ← bind_pure_comp_eq_map _ (x.run r); refl
@[simp] lemma run_monad_lift {n} [has_monad_lift_t n m] (x : n α) :
(monad_lift x : reader_t ρ m α).run r = (monad_lift x : m α) := rfl
@[simp] lemma run_monad_map {m' n n'} [monad m'] [monad_functor_t n n' m m'] (f : ∀ {α}, n α → n' α) :
(monad_map @f x : reader_t ρ m' α).run r = monad_map @f (x.run r) := rfl
@[simp] lemma run_read : (reader_t.read : reader_t ρ m ρ).run r = pure r := rfl
end
end reader_t
instance (ρ : Type u) (m : Type u → Type v) [monad m] [is_lawful_monad m] : is_lawful_monad (reader_t ρ m) :=
{ id_map := by intros; apply reader_t.ext; intro; simp,
pure_bind := by intros; apply reader_t.ext; intro; simp,
bind_assoc := by intros; apply reader_t.ext; intro; simp [bind_assoc] }
namespace option_t
variables {α β : Type u} {m : Type u → Type v} (x : option_t m α)
lemma ext {x x' : option_t m α} (h : x.run = x'.run) : x = x' :=
by cases x; cases x'; simp * at *
variable [monad m]
@[simp] lemma run_pure (a) : (pure a : option_t m α).run = pure (some a) := rfl
@[simp] lemma run_bind (f : α → option_t m β) : (x >>= f).run = x.run >>= option_t.bind_cont f :=
rfl
@[simp] lemma run_map (f : α → β) [is_lawful_monad m] : (f <$> x).run = option.map f <$> x.run :=
begin
rw ← bind_pure_comp_eq_map _ x.run,
change x.run >>= option_t.bind_cont (pure ∘ f) = _,
apply bind_ext_congr,
intro a; cases a; simp [option_t.bind_cont, option.map, option.bind]
end
@[simp] lemma run_monad_lift {n} [has_monad_lift_t n m] (x : n α) :
(monad_lift x : option_t m α).run = some <$> (monad_lift x : m α) := rfl
@[simp] lemma run_monad_map {m' n n'} [monad m'] [monad_functor_t n n' m m'] (f : ∀ {α}, n α → n' α) :
(monad_map @f x : option_t m' α).run = monad_map @f x.run := rfl
end option_t
instance (m : Type u → Type v) [monad m] [is_lawful_monad m] : is_lawful_monad (option_t m) :=
{ id_map := begin
intros, apply option_t.ext, simp only [option_t.run_map],
rw [map_ext_congr, id_map],
intro a, cases a; refl
end,
bind_assoc := begin
intros, apply option_t.ext, simp only [option_t.run_bind, bind_assoc],
rw [bind_ext_congr],
intro a, cases a; simp [option_t.bind_cont]
end,
pure_bind := by intros; apply option_t.ext; simp [option_t.bind_cont] }
|
404786814e3f650c43cbb94cf81baecab4ddf4e9 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/list/pairwise.lean | 860d991cc13661bed634227bbf9ce823a730abdf | [] | 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,400 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.list.basic
import Mathlib.PostPort
universes u v
namespace Mathlib
namespace list
/- pairwise relation (generalized no duplicate) -/
theorem pairwise_iff {α : Type u} (R : α → α → Prop) : ∀ (ᾰ : List α),
pairwise R ᾰ ↔
ᾰ = [] ∨ Exists fun {a : α} => Exists fun {l : List α} => (∀ (a' : α), a' ∈ l → R a a') ∧ pairwise R l ∧ ᾰ = a :: l := sorry
theorem rel_of_pairwise_cons {α : Type u} {R : α → α → Prop} {a : α} {l : List α} (p : pairwise R (a :: l)) {a' : α} : a' ∈ l → R a a' :=
and.left (iff.mp pairwise_cons p)
theorem pairwise_of_pairwise_cons {α : Type u} {R : α → α → Prop} {a : α} {l : List α} (p : pairwise R (a :: l)) : pairwise R l :=
and.right (iff.mp pairwise_cons p)
theorem pairwise.tail {α : Type u} {R : α → α → Prop} {l : List α} (p : pairwise R l) : pairwise R (tail l) := sorry
theorem pairwise.imp_of_mem {α : Type u} {R : α → α → Prop} {S : α → α → Prop} {l : List α} (H : ∀ {a b : α}, a ∈ l → b ∈ l → R a b → S a b) (p : pairwise R l) : pairwise S l := sorry
theorem pairwise.imp {α : Type u} {R : α → α → Prop} {S : α → α → Prop} (H : ∀ (a b : α), R a b → S a b) {l : List α} : pairwise R l → pairwise S l :=
pairwise.imp_of_mem fun (a b : α) (_x : a ∈ l) (_x : b ∈ l) => H a b
theorem pairwise.and {α : Type u} {R : α → α → Prop} {S : α → α → Prop} {l : List α} : pairwise (fun (a b : α) => R a b ∧ S a b) l ↔ pairwise R l ∧ pairwise S l := sorry
theorem pairwise.imp₂ {α : Type u} {R : α → α → Prop} {S : α → α → Prop} {T : α → α → Prop} (H : ∀ (a b : α), R a b → S a b → T a b) {l : List α} (hR : pairwise R l) (hS : pairwise S l) : pairwise T l :=
pairwise.imp (fun (a b : α) => And._oldrec (H a b)) (iff.mpr pairwise.and { left := hR, right := hS })
theorem pairwise.iff_of_mem {α : Type u} {R : α → α → Prop} {S : α → α → Prop} {l : List α} (H : ∀ {a b : α}, a ∈ l → b ∈ l → (R a b ↔ S a b)) : pairwise R l ↔ pairwise S l :=
{ mp := pairwise.imp_of_mem fun (a b : α) (m : a ∈ l) (m' : b ∈ l) => iff.mp (H m m'),
mpr := pairwise.imp_of_mem fun (a b : α) (m : a ∈ l) (m' : b ∈ l) => iff.mpr (H m m') }
theorem pairwise.iff {α : Type u} {R : α → α → Prop} {S : α → α → Prop} (H : ∀ (a b : α), R a b ↔ S a b) {l : List α} : pairwise R l ↔ pairwise S l :=
pairwise.iff_of_mem fun (a b : α) (_x : a ∈ l) (_x : b ∈ l) => H a b
theorem pairwise_of_forall {α : Type u} {R : α → α → Prop} {l : List α} (H : ∀ (x y : α), R x y) : pairwise R l := sorry
theorem pairwise.and_mem {α : Type u} {R : α → α → Prop} {l : List α} : pairwise R l ↔ pairwise (fun (x y : α) => x ∈ l ∧ y ∈ l ∧ R x y) l := sorry
theorem pairwise.imp_mem {α : Type u} {R : α → α → Prop} {l : List α} : pairwise R l ↔ pairwise (fun (x y : α) => x ∈ l → y ∈ l → R x y) l := sorry
theorem pairwise_of_sublist {α : Type u} {R : α → α → Prop} {l₁ : List α} {l₂ : List α} : l₁ <+ l₂ → pairwise R l₂ → pairwise R l₁ := sorry
theorem forall_of_forall_of_pairwise {α : Type u} {R : α → α → Prop} (H : symmetric R) {l : List α} (H₁ : ∀ (x : α), x ∈ l → R x x) (H₂ : pairwise R l) (x : α) : x ∈ l → ∀ (y : α), y ∈ l → R x y := sorry
theorem forall_of_pairwise {α : Type u} {R : α → α → Prop} (H : symmetric R) {l : List α} (hl : pairwise R l) (a : α) : a ∈ l → ∀ (b : α), b ∈ l → a ≠ b → R a b :=
forall_of_forall_of_pairwise (fun (a b : α) (h : a ≠ b → R a b) (hne : b ≠ a) => H (h (ne.symm hne)))
(fun (_x : α) (_x_1 : _x ∈ l) (h : _x ≠ _x) => false.elim (h rfl))
(pairwise.imp (fun (_x _x_1 : α) (h : R _x _x_1) (_x : _x ≠ _x_1) => h) hl)
theorem pairwise_singleton {α : Type u} (R : α → α → Prop) (a : α) : pairwise R [a] := sorry
theorem pairwise_pair {α : Type u} {R : α → α → Prop} {a : α} {b : α} : pairwise R [a, b] ↔ R a b := sorry
theorem pairwise_append {α : Type u} {R : α → α → Prop} {l₁ : List α} {l₂ : List α} : pairwise R (l₁ ++ l₂) ↔ pairwise R l₁ ∧ pairwise R l₂ ∧ ∀ (x : α), x ∈ l₁ → ∀ (y : α), y ∈ l₂ → R x y := sorry
theorem pairwise_append_comm {α : Type u} {R : α → α → Prop} (s : symmetric R) {l₁ : List α} {l₂ : List α} : pairwise R (l₁ ++ l₂) ↔ pairwise R (l₂ ++ l₁) := sorry
theorem pairwise_middle {α : Type u} {R : α → α → Prop} (s : symmetric R) {a : α} {l₁ : List α} {l₂ : List α} : pairwise R (l₁ ++ a :: l₂) ↔ pairwise R (a :: (l₁ ++ l₂)) := sorry
theorem pairwise_map {α : Type u} {β : Type v} {R : α → α → Prop} (f : β → α) {l : List β} : pairwise R (map f l) ↔ pairwise (fun (a b : β) => R (f a) (f b)) l := sorry
theorem pairwise_of_pairwise_map {α : Type u} {β : Type v} {R : α → α → Prop} {S : β → β → Prop} (f : α → β) (H : ∀ (a b : α), S (f a) (f b) → R a b) {l : List α} (p : pairwise S (map f l)) : pairwise R l :=
pairwise.imp H (iff.mp (pairwise_map f) p)
theorem pairwise_map_of_pairwise {α : Type u} {β : Type v} {R : α → α → Prop} {S : β → β → Prop} (f : α → β) (H : ∀ (a b : α), R a b → S (f a) (f b)) {l : List α} (p : pairwise R l) : pairwise S (map f l) :=
iff.mpr (pairwise_map f) (pairwise.imp H p)
theorem pairwise_filter_map {α : Type u} {β : Type v} {R : α → α → Prop} (f : β → Option α) {l : List β} : pairwise R (filter_map f l) ↔ pairwise (fun (a a' : β) => ∀ (b : α), b ∈ f a → ∀ (b' : α), b' ∈ f a' → R b b') l := sorry
theorem pairwise_filter_map_of_pairwise {α : Type u} {β : Type v} {R : α → α → Prop} {S : β → β → Prop} (f : α → Option β) (H : ∀ (a a' : α), R a a' → ∀ (b : β), b ∈ f a → ∀ (b' : β), b' ∈ f a' → S b b') {l : List α} (p : pairwise R l) : pairwise S (filter_map f l) :=
iff.mpr (pairwise_filter_map f) (pairwise.imp H p)
theorem pairwise_filter {α : Type u} {R : α → α → Prop} (p : α → Prop) [decidable_pred p] {l : List α} : pairwise R (filter p l) ↔ pairwise (fun (x y : α) => p x → p y → R x y) l := sorry
theorem pairwise_filter_of_pairwise {α : Type u} {R : α → α → Prop} (p : α → Prop) [decidable_pred p] {l : List α} : pairwise R l → pairwise R (filter p l) :=
pairwise_of_sublist (filter_sublist l)
theorem pairwise_pmap {α : Type u} {β : Type v} {R : α → α → Prop} {p : β → Prop} {f : (b : β) → p b → α} {l : List β} (h : ∀ (x : β), x ∈ l → p x) : pairwise R (pmap f l h) ↔ pairwise (fun (b₁ b₂ : β) => ∀ (h₁ : p b₁) (h₂ : p b₂), R (f b₁ h₁) (f b₂ h₂)) l := sorry
theorem pairwise.pmap {α : Type u} {β : Type v} {R : α → α → Prop} {l : List α} (hl : pairwise R l) {p : α → Prop} {f : (a : α) → p a → β} (h : ∀ (x : α), x ∈ l → p x) {S : β → β → Prop} (hS : ∀ {x : α} (hx : p x) {y : α} (hy : p y), R x y → S (f x hx) (f y hy)) : pairwise S (pmap f l h) :=
iff.mpr (pairwise_pmap h)
(pairwise.imp_of_mem (fun (a b : α) (ᾰ : a ∈ l) (ᾰ_1 : b ∈ l) (ᾰ_2 : R a b) (h₁ : p a) (h₂ : p b) => hS h₁ h₂ ᾰ_2) hl)
theorem pairwise_join {α : Type u} {R : α → α → Prop} {L : List (List α)} : pairwise R (join L) ↔
(∀ (l : List α), l ∈ L → pairwise R l) ∧
pairwise (fun (l₁ l₂ : List α) => ∀ (x : α), x ∈ l₁ → ∀ (y : α), y ∈ l₂ → R x y) L := sorry
@[simp] theorem pairwise_reverse {α : Type u} {R : α → α → Prop} {l : List α} : pairwise R (reverse l) ↔ pairwise (fun (x y : α) => R y x) l := sorry
theorem pairwise_iff_nth_le {α : Type u} {R : α → α → Prop} {l : List α} : pairwise R l ↔ ∀ (i j : ℕ) (h₁ : j < length l) (h₂ : i < j), R (nth_le l i (lt_trans h₂ h₁)) (nth_le l j h₁) := sorry
theorem pairwise_sublists' {α : Type u} {R : α → α → Prop} {l : List α} : pairwise R l → pairwise (lex (function.swap R)) (sublists' l) := sorry
theorem pairwise_sublists {α : Type u} {R : α → α → Prop} {l : List α} (H : pairwise R l) : pairwise (fun (l₁ l₂ : List α) => lex R (reverse l₁) (reverse l₂)) (sublists l) := sorry
/- pairwise reduct -/
@[simp] theorem pw_filter_nil {α : Type u} {R : α → α → Prop} [DecidableRel R] : pw_filter R [] = [] :=
rfl
@[simp] theorem pw_filter_cons_of_pos {α : Type u} {R : α → α → Prop} [DecidableRel R] {a : α} {l : List α} (h : ∀ (b : α), b ∈ pw_filter R l → R a b) : pw_filter R (a :: l) = a :: pw_filter R l :=
if_pos h
@[simp] theorem pw_filter_cons_of_neg {α : Type u} {R : α → α → Prop} [DecidableRel R] {a : α} {l : List α} (h : ¬∀ (b : α), b ∈ pw_filter R l → R a b) : pw_filter R (a :: l) = pw_filter R l :=
if_neg h
theorem pw_filter_map {α : Type u} {β : Type v} {R : α → α → Prop} [DecidableRel R] (f : β → α) (l : List β) : pw_filter R (map f l) = map f (pw_filter (fun (x y : β) => R (f x) (f y)) l) := sorry
theorem pw_filter_sublist {α : Type u} {R : α → α → Prop} [DecidableRel R] (l : List α) : pw_filter R l <+ l := sorry
theorem pw_filter_subset {α : Type u} {R : α → α → Prop} [DecidableRel R] (l : List α) : pw_filter R l ⊆ l :=
sublist.subset (pw_filter_sublist l)
theorem pairwise_pw_filter {α : Type u} {R : α → α → Prop} [DecidableRel R] (l : List α) : pairwise R (pw_filter R l) := sorry
theorem pw_filter_eq_self {α : Type u} {R : α → α → Prop} [DecidableRel R] {l : List α} : pw_filter R l = l ↔ pairwise R l := sorry
@[simp] theorem pw_filter_idempotent {α : Type u} {R : α → α → Prop} [DecidableRel R] {l : List α} : pw_filter R (pw_filter R l) = pw_filter R l :=
iff.mpr pw_filter_eq_self (pairwise_pw_filter l)
theorem forall_mem_pw_filter {α : Type u} {R : α → α → Prop} [DecidableRel R] (neg_trans : ∀ {x y z : α}, R x z → R x y ∨ R y z) (a : α) (l : List α) : (∀ (b : α), b ∈ pw_filter R l → R a b) ↔ ∀ (b : α), b ∈ l → R a b := sorry
|
7523a8b8e4132e09a7497853c43be7838ae14e7c | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /10_Structures_and_Records.org.12.lean | 7c412b2c4f4a10920cfd29cff1eefb6fcace69d1 | [] | no_license | cjmazey/lean-tutorial | ba559a49f82aa6c5848b9bf17b7389bf7f4ba645 | 381f61c9fcac56d01d959ae0fa6e376f2c4e3b34 | refs/heads/master | 1,610,286,098,832 | 1,447,124,923,000 | 1,447,124,923,000 | 43,082,433 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 328 | lean | import standard
open nat
structure point (A : Type) :=
mk :: (x : A) (y : A)
definition p1 : point nat := {| point, x := 10, y := 20 |}
definition p2 : point nat := {| point, x := 1, p1 |}
definition p3 : point nat := {| point, y := 1, p1 |}
example : point.y p1 = point.y p2 :=
rfl
example : point.x p1 = point.x p3 :=
rfl
|
0a6ab7e3178f860fec62e6de00a3069964cfea79 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/algebra/lie/free.lean | 0bb0e5c04bd27683aaaed77dbc2a18e284499012 | [
"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 | 10,920 | lean | /-
Copyright (c) 2021 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import algebra.lie.of_associative
import algebra.lie.non_unital_non_assoc_algebra
import algebra.lie.universal_enveloping
import algebra.free_non_unital_non_assoc_algebra
/-!
# Free Lie algebras
Given a commutative ring `R` and a type `X` we construct the free Lie algebra on `X` with
coefficients in `R` together with its universal property.
## Main definitions
* `free_lie_algebra`
* `free_lie_algebra.lift`
* `free_lie_algebra.of`
* `free_lie_algebra.universal_enveloping_equiv_free_algebra`
## Implementation details
### Quotient of free non-unital, non-associative algebra
We follow [N. Bourbaki, *Lie Groups and Lie Algebras, Chapters 1--3*](bourbaki1975) and construct
the free Lie algebra as a quotient of the free non-unital, non-associative algebra. Since we do not
currently have definitions of ideals, lattices of ideals, and quotients for
`non_unital_non_assoc_semiring`, we construct our quotient using the low-level `quot` function on
an inductively-defined relation.
### Alternative construction (needs PBW)
An alternative construction of the free Lie algebra on `X` is to start with the free unital
associative algebra on `X`, regard it as a Lie algebra via the ring commutator, and take its
smallest Lie subalgebra containing `X`. I.e.:
`lie_subalgebra.lie_span R (free_algebra R X) (set.range (free_algebra.ι R))`.
However with this definition there does not seem to be an easy proof that the required universal
property holds, and I don't know of a proof that avoids invoking the Poincaré–Birkhoff–Witt theorem.
A related MathOverflow question is [this one](https://mathoverflow.net/questions/396680/).
## Tags
lie algebra, free algebra, non-unital, non-associative, universal property, forgetful functor,
adjoint functor
-/
universes u v w
noncomputable theory
variables (R : Type u) (X : Type v) [comm_ring R]
/-- We save characters by using Bourbaki's name `lib` (as in «libre») for
`free_non_unital_non_assoc_algebra` in this file. -/
local notation `lib` := free_non_unital_non_assoc_algebra
local notation `lib.lift` := free_non_unital_non_assoc_algebra.lift
local notation `lib.of` := free_non_unital_non_assoc_algebra.of
local notation `lib.lift_of_apply` := free_non_unital_non_assoc_algebra.lift_of_apply
local notation `lib.lift_comp_of` := free_non_unital_non_assoc_algebra.lift_comp_of
namespace free_lie_algebra
/-- The quotient of `lib R X` by the equivalence relation generated by this relation will give us
the free Lie algebra. -/
inductive rel : lib R X → lib R X → Prop
| lie_self (a : lib R X) : rel (a * a) 0
| leibniz_lie (a b c : lib R X) : rel (a * (b * c)) (((a * b) * c) + (b * (a * c)))
| smul (t : R) {a b : lib R X} : rel a b → rel (t • a) (t • b)
| add_right {a b : lib R X} (c : lib R X) : rel a b → rel (a + c) (b + c)
| mul_left (a : lib R X) {b c : lib R X} : rel b c → rel (a * b) (a * c)
| mul_right {a b : lib R X} (c : lib R X) : rel a b → rel (a * c) (b * c)
variables {R X}
lemma rel.add_left (a : lib R X) {b c : lib R X} (h : rel R X b c) : rel R X (a + b) (a + c) :=
by { rw [add_comm _ b, add_comm _ c], exact h.add_right _, }
lemma rel.neg {a b : lib R X} (h : rel R X a b) : rel R X (-a) (-b) :=
by simpa only [neg_one_smul] using h.smul (-1)
lemma rel.sub_left (a : lib R X) {b c : lib R X} (h : rel R X b c) : rel R X (a - b) (a - c) :=
by simpa only [sub_eq_add_neg] using h.neg.add_left a
lemma rel.sub_right {a b : lib R X} (c : lib R X) (h : rel R X a b) : rel R X (a - c) (b - c) :=
by simpa only [sub_eq_add_neg] using h.add_right (-c)
lemma rel.smul_of_tower {S : Type*} [monoid S] [distrib_mul_action S R] [is_scalar_tower S R R]
(t : S) (a b : lib R X)
(h : rel R X a b) : rel R X (t • a) (t • b) :=
begin
rw [←smul_one_smul R t a, ←smul_one_smul R t b],
exact h.smul _,
end
end free_lie_algebra
/-- The free Lie algebra on the type `X` with coefficients in the commutative ring `R`. -/
@[derive inhabited]
def free_lie_algebra := quot (free_lie_algebra.rel R X)
namespace free_lie_algebra
instance {S : Type*} [monoid S] [distrib_mul_action S R] [is_scalar_tower S R R] :
has_scalar S (free_lie_algebra R X) :=
{ smul := λ t, quot.map ((•) t) (rel.smul_of_tower t) }
instance {S : Type*} [monoid S] [distrib_mul_action S R] [distrib_mul_action Sᵐᵒᵖ R]
[is_scalar_tower S R R] [is_central_scalar S R] :
is_central_scalar S (free_lie_algebra R X) :=
{ op_smul_eq_smul := λ t, quot.ind $ by exact λ a, congr_arg (quot.mk _) (op_smul_eq_smul t a) }
instance : has_zero (free_lie_algebra R X) :=
{ zero := quot.mk _ 0 }
instance : has_add (free_lie_algebra R X) :=
{ add := quot.map₂ (+) (λ _ _ _, rel.add_left _) (λ _ _ _, rel.add_right _) }
instance : has_neg (free_lie_algebra R X) :=
{ neg := quot.map has_neg.neg (λ _ _, rel.neg) }
instance : has_sub (free_lie_algebra R X) :=
{ sub := quot.map₂ has_sub.sub (λ _ _ _, rel.sub_left _) (λ _ _ _, rel.sub_right _) }
instance : add_group (free_lie_algebra R X) :=
function.surjective.add_group (quot.mk _) (surjective_quot_mk _)
rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
instance : add_comm_semigroup (free_lie_algebra R X) :=
function.surjective.add_comm_semigroup (quot.mk _) (surjective_quot_mk _) (λ _ _, rfl)
instance : add_comm_group (free_lie_algebra R X) :=
{ ..free_lie_algebra.add_group R X,
..free_lie_algebra.add_comm_semigroup R X }
instance {S : Type*} [semiring S] [module S R] [is_scalar_tower S R R] :
module S (free_lie_algebra R X) :=
function.surjective.module S ⟨quot.mk _, rfl, λ _ _, rfl⟩ (surjective_quot_mk _) (λ _ _, rfl)
/-- Note that here we turn the `has_mul` coming from the `non_unital_non_assoc_semiring` structure
on `lib R X` into a `has_bracket` on `free_lie_algebra`. -/
instance : lie_ring (free_lie_algebra R X) :=
{ bracket := quot.map₂ (*) (λ _ _ _, rel.mul_left _) (λ _ _ _, rel.mul_right _),
add_lie := by { rintros ⟨a⟩ ⟨b⟩ ⟨c⟩, change quot.mk _ _ = quot.mk _ _, rw add_mul, },
lie_add := by { rintros ⟨a⟩ ⟨b⟩ ⟨c⟩, change quot.mk _ _ = quot.mk _ _, rw mul_add, },
lie_self := by { rintros ⟨a⟩, exact quot.sound (rel.lie_self a), },
leibniz_lie := by { rintros ⟨a⟩ ⟨b⟩ ⟨c⟩, exact quot.sound (rel.leibniz_lie a b c), }, }
instance : lie_algebra R (free_lie_algebra R X) :=
{ lie_smul :=
begin
rintros t ⟨a⟩ ⟨c⟩,
change quot.mk _ (a • (t • c)) = quot.mk _ (t • (a • c)),
rw ← smul_comm,
end, }
variables {X}
/-- The embedding of `X` into the free Lie algebra of `X` with coefficients in the commutative ring
`R`. -/
def of : X → free_lie_algebra R X := λ x, quot.mk _ (lib.of R x)
variables {L : Type w} [lie_ring L] [lie_algebra R L]
local attribute [instance] lie_ring.to_non_unital_non_assoc_semiring
/-- An auxiliary definition used to construct the equivalence `lift` below. -/
def lift_aux (f : X → L) := lib.lift R f
lemma lift_aux_map_smul (f : X → L) (t : R) (a : lib R X) :
lift_aux R f (t • a) = t • lift_aux R f a :=
non_unital_alg_hom.map_smul _ t a
lemma lift_aux_map_add (f : X → L) (a b : lib R X) :
lift_aux R f (a + b) = (lift_aux R f a) + (lift_aux R f b) :=
non_unital_alg_hom.map_add _ a b
lemma lift_aux_map_mul (f : X → L) (a b : lib R X) :
lift_aux R f (a * b) = ⁅lift_aux R f a, lift_aux R f b⁆ :=
non_unital_alg_hom.map_mul _ a b
lemma lift_aux_spec (f : X → L) (a b : lib R X) (h : free_lie_algebra.rel R X a b) :
lift_aux R f a = lift_aux R f b :=
begin
induction h,
case rel.lie_self : a'
{ simp only [lift_aux_map_mul, non_unital_alg_hom.map_zero, lie_self], },
case rel.leibniz_lie : a' b' c'
{ simp only [lift_aux_map_mul, lift_aux_map_add, sub_add_cancel, lie_lie], },
case rel.smul : t a' b' h₁ h₂
{ simp only [lift_aux_map_smul, h₂], },
case rel.add_right : a' b' c' h₁ h₂
{ simp only [lift_aux_map_add, h₂], },
case rel.mul_left : a' b' c' h₁ h₂
{ simp only [lift_aux_map_mul, h₂], },
case rel.mul_right : a' b' c' h₁ h₂
{ simp only [lift_aux_map_mul, h₂], },
end
/-- The quotient map as a `non_unital_alg_hom`. -/
def mk : lib R X →ₙₐ[R] free_lie_algebra R X :=
{ to_fun := quot.mk (rel R X),
map_smul' := λ t a, rfl,
map_zero' := rfl,
map_add' := λ a b, rfl,
map_mul' := λ a b, rfl, }
/-- The functor `X ↦ free_lie_algebra R X` from the category of types to the category of Lie
algebras over `R` is adjoint to the forgetful functor in the other direction. -/
def lift : (X → L) ≃ (free_lie_algebra R X →ₗ⁅R⁆ L) :=
{ to_fun := λ f,
{ to_fun := λ c, quot.lift_on c (lift_aux R f) (lift_aux_spec R f),
map_add' := by { rintros ⟨a⟩ ⟨b⟩, rw ← lift_aux_map_add, refl, },
map_smul' := by { rintros t ⟨a⟩, rw ← lift_aux_map_smul, refl, },
map_lie' := by { rintros ⟨a⟩ ⟨b⟩, rw ← lift_aux_map_mul, refl, }, },
inv_fun := λ F, F ∘ (of R),
left_inv := λ f, by { ext x, simp only [lift_aux, of, quot.lift_on_mk, lie_hom.coe_mk,
function.comp_app, lib.lift_of_apply], },
right_inv := λ F,
begin
ext ⟨a⟩,
let F' := F.to_non_unital_alg_hom.comp (mk R),
exact non_unital_alg_hom.congr_fun (lib.lift_comp_of R F') a,
end, }
@[simp] lemma lift_symm_apply (F : free_lie_algebra R X →ₗ⁅R⁆ L) : (lift R).symm F = F ∘ (of R) :=
rfl
variables {R}
@[simp] lemma of_comp_lift (f : X → L) : (lift R f) ∘ (of R) = f :=
(lift R).left_inv f
@[simp] lemma lift_unique (f : X → L) (g : free_lie_algebra R X →ₗ⁅R⁆ L) :
g ∘ (of R) = f ↔ g = lift R f :=
(lift R).symm_apply_eq
attribute [irreducible] of lift
@[simp] lemma lift_of_apply (f : X → L) (x) : lift R f (of R x) = f x :=
by rw [← function.comp_app (lift R f) (of R) x, of_comp_lift]
@[simp] lemma lift_comp_of (F : free_lie_algebra R X →ₗ⁅R⁆ L) : lift R (F ∘ (of R)) = F :=
by { rw ← lift_symm_apply, exact (lift R).apply_symm_apply F, }
@[ext] lemma hom_ext {F₁ F₂ : free_lie_algebra R X →ₗ⁅R⁆ L} (h : ∀ x, F₁ (of R x) = F₂ (of R x)) :
F₁ = F₂ :=
have h' : (lift R).symm F₁ = (lift R).symm F₂, { ext, simp [h], },
(lift R).symm.injective h'
variables (R X)
/-- The universal enveloping algebra of the free Lie algebra is just the free unital associative
algebra. -/
@[simps] def universal_enveloping_equiv_free_algebra :
universal_enveloping_algebra R (free_lie_algebra R X) ≃ₐ[R] free_algebra R X :=
alg_equiv.of_alg_hom
(universal_enveloping_algebra.lift R $ free_lie_algebra.lift R $ free_algebra.ι R)
(free_algebra.lift R $ (universal_enveloping_algebra.ι R) ∘ (free_lie_algebra.of R))
(by { ext, simp, })
(by { ext, simp, })
end free_lie_algebra
|
a974fe8b02f2a41b08c05da72e7e44a71798e751 | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /tests/lean/run/uni.lean | 1011cf4622487bbed68e6c03bcf6dd9d267357b8 | [
"Apache-2.0"
] | permissive | tjiaqi/lean | 4634d729795c164664d10d093f3545287c76628f | d0ce4cf62f4246b0600c07e074d86e51f2195e30 | refs/heads/master | 1,622,323,796,480 | 1,422,643,069,000 | 1,422,643,069,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,343 | lean | import logic
namespace experiment
inductive nat : Type :=
zero : nat,
succ : nat → nat
check @nat.rec
(*
local env = get_env()
local nat_rec = Const({"nat", "rec"}, {1})
local nat = Const("nat")
local n = Local("n", nat)
local C = Fun(n, Prop)
local p = Local("p", Prop)
local ff = Const("false")
local tt = Const("true")
local t = nat_rec(C, ff, Fun(n, p, tt))
local zero = Const("zero")
local succ = Const("succ")
local one = succ(zero)
local tc = type_checker(env)
print(env:whnf(t(one)))
print(env:whnf(t(zero)))
local m = mk_metavar("m", nat)
print(env:whnf(t(m)))
function test_unify(env, lhs, rhs, num_s)
print(tostring(lhs) .. " =?= " .. tostring(rhs) .. ", expected: " .. tostring(num_s))
local ss = unify(env, lhs, rhs, name_generator(), true, substitution(), options())
local n = 0
for s in ss do
print("solution: ")
s:for_each_expr(function(n, v, j)
print(" " .. tostring(n) .. " := " .. tostring(v))
end)
s:for_each_level(function(n, v, j)
print(" " .. tostring(n) .. " := " .. tostring(v))
end)
n = n + 1
end
if num_s ~= n then print("n: " .. n) end
assert(num_s == n)
end
test_unify(env, t(m), tt, 1)
test_unify(env, t(m), ff, 1)
*)
end experiment
|
5640b59f711c576c0a684d76fabc4b41a5148bf9 | 432d948a4d3d242fdfb44b81c9e1b1baacd58617 | /src/linear_algebra/matrix.lean | 515f936df921cac186496e7356036d63ba2104be | [
"Apache-2.0"
] | permissive | JLimperg/aesop3 | 306cc6570c556568897ed2e508c8869667252e8a | a4a116f650cc7403428e72bd2e2c4cda300fe03f | refs/heads/master | 1,682,884,916,368 | 1,620,320,033,000 | 1,620,320,033,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 60,715 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Patrick Massot, Casper Putz
-/
import linear_algebra.finite_dimensional
import linear_algebra.nonsingular_inverse
import linear_algebra.multilinear
import linear_algebra.dual
import ring_theory.algebra_tower
import ring_theory.matrix_algebra
/-!
# Linear maps and matrices
This file defines the maps to send matrices to a linear map,
and to send linear maps between modules with a finite bases
to matrices. This defines a linear equivalence between linear maps
between finite-dimensional vector spaces and matrices indexed by
the respective bases.
It also defines the trace of an endomorphism, and the determinant of a family of vectors with
respect to some basis.
Some results are proved about the linear map corresponding to a
diagonal matrix (`range`, `ker` and `rank`).
Some results are proved for determinants of block triangular matrices.
## Main definitions
In the list below, and in all this file, `R` is a commutative ring (semiring
is sometimes enough), `M` and its variations are `R`-modules, `ι`, `κ`, `n` and `m` are finite
types used for indexing.
* `linear_map.to_matrix`: given bases `v₁ : ι → M₁` and `v₂ : κ → M₂`,
the `R`-linear equivalence from `M₁ →ₗ[R] M₂` to `matrix κ ι R`
* `matrix.to_lin`: the inverse of `linear_map.to_matrix`
* `linear_map.to_matrix'`: the `R`-linear equivalence from `(n → R) →ₗ[R] (m → R)`
to `matrix n m R` (with the standard basis on `n → R` and `m → R`)
* `matrix.to_lin'`: the inverse of `linear_map.to_matrix'`
* `alg_equiv_matrix`: given a basis indexed by `n`, the `R`-algebra equivalence between
`R`-endomorphisms of `M` and `matrix n n R`
* `matrix.trace`: the trace of a square matrix
* `linear_map.trace`: the trace of an endomorphism
* `is_basis.to_matrix`: the matrix whose columns are a given family of vectors in a given basis
* `is_basis.to_matrix_equiv`: given a basis, the linear equivalence between families of vectors
and matrices arising from `is_basis.to_matrix`
* `is_basis.det`: the determinant of a family of vectors with respect to a basis, as a multilinear
map
## Tags
linear_map, matrix, linear_equiv, diagonal, det, trace
-/
noncomputable theory
open linear_map matrix set submodule
open_locale big_operators
open_locale matrix
universes u v w
section to_matrix'
variables {R : Type*} [comm_ring R]
variables {l m n : Type*} [fintype l] [fintype m] [fintype n]
instance [decidable_eq m] [decidable_eq n] (R) [fintype R] : fintype (matrix m n R) :=
by unfold matrix; apply_instance
/-- `matrix.mul_vec M` is a linear map. -/
def matrix.mul_vec_lin (M : matrix m n R) : (n → R) →ₗ[R] (m → R) :=
{ to_fun := M.mul_vec,
map_add' := λ v w, funext (λ i, dot_product_add _ _ _),
map_smul' := λ c v, funext (λ i, dot_product_smul _ _ _) }
@[simp] lemma matrix.mul_vec_lin_apply (M : matrix m n R) (v : n → R) :
matrix.mul_vec_lin M v = M.mul_vec v := rfl
variables [decidable_eq n]
@[simp] lemma matrix.mul_vec_std_basis (M : matrix m n R) (i j) :
M.mul_vec (std_basis R (λ _, R) j 1) i = M i j :=
begin
have : (∑ j', M i j' * if j = j' then 1 else 0) = M i j,
{ simp_rw [mul_boole, finset.sum_ite_eq, finset.mem_univ, if_true] },
convert this,
ext,
split_ifs with h; simp only [std_basis_apply],
{ rw [h, function.update_same] },
{ rw [function.update_noteq (ne.symm h), pi.zero_apply] }
end
/-- Linear maps `(n → R) →ₗ[R] (m → R)` are linearly equivalent to `matrix m n R`. -/
def linear_map.to_matrix' : ((n → R) →ₗ[R] (m → R)) ≃ₗ[R] matrix m n R :=
{ to_fun := λ f i j, f (std_basis R (λ _, R) j 1) i,
inv_fun := matrix.mul_vec_lin,
right_inv := λ M, by { ext i j, simp only [matrix.mul_vec_std_basis, matrix.mul_vec_lin_apply] },
left_inv := λ f, begin
apply (pi.is_basis_fun R n).ext,
intro j, ext i,
simp only [matrix.mul_vec_std_basis, matrix.mul_vec_lin_apply]
end,
map_add' := λ f g, by { ext i j, simp only [pi.add_apply, linear_map.add_apply] },
map_smul' := λ c f, by { ext i j, simp only [pi.smul_apply, linear_map.smul_apply] } }
/-- A `matrix m n R` is linearly equivalent to a linear map `(n → R) →ₗ[R] (m → R)`. -/
def matrix.to_lin' : matrix m n R ≃ₗ[R] ((n → R) →ₗ[R] (m → R)) :=
linear_map.to_matrix'.symm
@[simp] lemma linear_map.to_matrix'_symm :
(linear_map.to_matrix'.symm : matrix m n R ≃ₗ[R] _) = matrix.to_lin' :=
rfl
@[simp] lemma matrix.to_lin'_symm :
(matrix.to_lin'.symm : ((n → R) →ₗ[R] (m → R)) ≃ₗ[R] _) = linear_map.to_matrix' :=
rfl
@[simp] lemma linear_map.to_matrix'_to_lin' (M : matrix m n R) :
linear_map.to_matrix' (matrix.to_lin' M) = M :=
linear_map.to_matrix'.apply_symm_apply M
@[simp] lemma matrix.to_lin'_to_matrix' (f : (n → R) →ₗ[R] (m → R)) :
matrix.to_lin' (linear_map.to_matrix' f) = f :=
matrix.to_lin'.apply_symm_apply f
@[simp] lemma linear_map.to_matrix'_apply (f : (n → R) →ₗ[R] (m → R)) (i j) :
linear_map.to_matrix' f i j = f (λ j', if j' = j then 1 else 0) i :=
begin
simp only [linear_map.to_matrix', linear_equiv.coe_mk],
congr,
ext j',
split_ifs with h,
{ rw [h, std_basis_same] },
apply std_basis_ne _ _ _ _ h
end
@[simp] lemma matrix.to_lin'_apply (M : matrix m n R) (v : n → R) :
matrix.to_lin' M v = M.mul_vec v := rfl
@[simp] lemma matrix.to_lin'_one :
matrix.to_lin' (1 : matrix n n R) = id :=
by { ext, simp [linear_map.one_apply, std_basis_apply] }
@[simp] lemma linear_map.to_matrix'_id :
(linear_map.to_matrix' (linear_map.id : (n → R) →ₗ[R] (n → R))) = 1 :=
by { ext, rw [matrix.one_apply, linear_map.to_matrix'_apply, id_apply] }
@[simp] lemma matrix.to_lin'_mul [decidable_eq m] (M : matrix l m R) (N : matrix m n R) :
matrix.to_lin' (M ⬝ N) = (matrix.to_lin' M).comp (matrix.to_lin' N) :=
by { ext, simp }
lemma linear_map.to_matrix'_comp [decidable_eq l]
(f : (n → R) →ₗ[R] (m → R)) (g : (l → R) →ₗ[R] (n → R)) :
(f.comp g).to_matrix' = f.to_matrix' ⬝ g.to_matrix' :=
suffices (f.comp g) = (f.to_matrix' ⬝ g.to_matrix').to_lin',
by rw [this, linear_map.to_matrix'_to_lin'],
by rw [matrix.to_lin'_mul, matrix.to_lin'_to_matrix', matrix.to_lin'_to_matrix']
lemma linear_map.to_matrix'_mul [decidable_eq m]
(f g : (m → R) →ₗ[R] (m → R)) :
(f * g).to_matrix' = f.to_matrix' ⬝ g.to_matrix' :=
linear_map.to_matrix'_comp f g
/-- Linear maps `(n → R) →ₗ[R] (n → R)` are algebra equivalent to `matrix n n R`. -/
def linear_map.to_matrix_alg_equiv' : ((n → R) →ₗ[R] (n → R)) ≃ₐ[R] matrix n n R :=
alg_equiv.of_linear_equiv linear_map.to_matrix' linear_map.to_matrix'_mul
(by simp [module.algebra_map_End_eq_smul_id])
/-- A `matrix n n R` is algebra equivalent to a linear map `(n → R) →ₗ[R] (n → R)`. -/
def matrix.to_lin_alg_equiv' : matrix n n R ≃ₐ[R] ((n → R) →ₗ[R] (n → R)) :=
linear_map.to_matrix_alg_equiv'.symm
@[simp] lemma linear_map.to_matrix_alg_equiv'_symm :
(linear_map.to_matrix_alg_equiv'.symm : matrix n n R ≃ₐ[R] _) = matrix.to_lin_alg_equiv' :=
rfl
@[simp] lemma matrix.to_lin_alg_equiv'_symm :
(matrix.to_lin_alg_equiv'.symm : ((n → R) →ₗ[R] (n → R)) ≃ₐ[R] _) =
linear_map.to_matrix_alg_equiv' :=
rfl
@[simp] lemma linear_map.to_matrix_alg_equiv'_to_lin_alg_equiv' (M : matrix n n R) :
linear_map.to_matrix_alg_equiv' (matrix.to_lin_alg_equiv' M) = M :=
linear_map.to_matrix_alg_equiv'.apply_symm_apply M
@[simp] lemma matrix.to_lin_alg_equiv'_to_matrix_alg_equiv' (f : (n → R) →ₗ[R] (n → R)) :
matrix.to_lin_alg_equiv' (linear_map.to_matrix_alg_equiv' f) = f :=
matrix.to_lin_alg_equiv'.apply_symm_apply f
@[simp] lemma linear_map.to_matrix_alg_equiv'_apply (f : (n → R) →ₗ[R] (n → R)) (i j) :
linear_map.to_matrix_alg_equiv' f i j = f (λ j', if j' = j then 1 else 0) i :=
by simp [linear_map.to_matrix_alg_equiv']
@[simp] lemma matrix.to_lin_alg_equiv'_apply (M : matrix n n R) (v : n → R) :
matrix.to_lin_alg_equiv' M v = M.mul_vec v := rfl
@[simp] lemma matrix.to_lin_alg_equiv'_one :
matrix.to_lin_alg_equiv' (1 : matrix n n R) = id :=
by { ext, simp [matrix.one_apply, std_basis_apply] }
@[simp] lemma linear_map.to_matrix_alg_equiv'_id :
(linear_map.to_matrix_alg_equiv' (linear_map.id : (n → R) →ₗ[R] (n → R))) = 1 :=
by { ext, rw [matrix.one_apply, linear_map.to_matrix_alg_equiv'_apply, id_apply] }
@[simp] lemma matrix.to_lin_alg_equiv'_mul (M N : matrix n n R) :
matrix.to_lin_alg_equiv' (M ⬝ N) =
(matrix.to_lin_alg_equiv' M).comp (matrix.to_lin_alg_equiv' N) :=
by { ext, simp }
lemma linear_map.to_matrix_alg_equiv'_comp (f g : (n → R) →ₗ[R] (n → R)) :
(f.comp g).to_matrix_alg_equiv' = f.to_matrix_alg_equiv' ⬝ g.to_matrix_alg_equiv' :=
suffices (f.comp g) = (f.to_matrix_alg_equiv' ⬝ g.to_matrix_alg_equiv').to_lin_alg_equiv',
by rw [this, linear_map.to_matrix_alg_equiv'_to_lin_alg_equiv'],
by rw [matrix.to_lin_alg_equiv'_mul, matrix.to_lin_alg_equiv'_to_matrix_alg_equiv',
matrix.to_lin_alg_equiv'_to_matrix_alg_equiv']
lemma linear_map.to_matrix_alg_equiv'_mul
(f g : (n → R) →ₗ[R] (n → R)) :
(f * g).to_matrix_alg_equiv' = f.to_matrix_alg_equiv' ⬝ g.to_matrix_alg_equiv' :=
linear_map.to_matrix_alg_equiv'_comp f g
end to_matrix'
section to_matrix
variables {R : Type*} [comm_ring R]
variables {l m n : Type*} [fintype l] [fintype m] [fintype n] [decidable_eq n]
variables {M₁ M₂ : Type*} [add_comm_group M₁] [add_comm_group M₂] [module R M₁] [module R M₂]
variables {v₁ : n → M₁} (hv₁ : is_basis R v₁) {v₂ : m → M₂} (hv₂ : is_basis R v₂)
/-- Given bases of two modules `M₁` and `M₂` over a commutative ring `R`, we get a linear
equivalence between linear maps `M₁ →ₗ M₂` and matrices over `R` indexed by the bases. -/
def linear_map.to_matrix : (M₁ →ₗ[R] M₂) ≃ₗ[R] matrix m n R :=
linear_equiv.trans (linear_equiv.arrow_congr hv₁.equiv_fun hv₂.equiv_fun) linear_map.to_matrix'
/-- Given bases of two modules `M₁` and `M₂` over a commutative ring `R`, we get a linear
equivalence between matrices over `R` indexed by the bases and linear maps `M₁ →ₗ M₂`. -/
def matrix.to_lin : matrix m n R ≃ₗ[R] (M₁ →ₗ[R] M₂) :=
(linear_map.to_matrix hv₁ hv₂).symm
@[simp] lemma linear_map.to_matrix_symm :
(linear_map.to_matrix hv₁ hv₂).symm = matrix.to_lin hv₁ hv₂ :=
rfl
@[simp] lemma matrix.to_lin_symm :
(matrix.to_lin hv₁ hv₂).symm = linear_map.to_matrix hv₁ hv₂ :=
rfl
@[simp] lemma matrix.to_lin_to_matrix (f : M₁ →ₗ[R] M₂) :
matrix.to_lin hv₁ hv₂ (linear_map.to_matrix hv₁ hv₂ f) = f :=
by rw [← matrix.to_lin_symm, linear_equiv.apply_symm_apply]
@[simp] lemma linear_map.to_matrix_to_lin (M : matrix m n R) :
linear_map.to_matrix hv₁ hv₂ (matrix.to_lin hv₁ hv₂ M) = M :=
by rw [← matrix.to_lin_symm, linear_equiv.symm_apply_apply]
lemma linear_map.to_matrix_apply (f : M₁ →ₗ[R] M₂) (i : m) (j : n) :
linear_map.to_matrix hv₁ hv₂ f i j = hv₂.equiv_fun (f (v₁ j)) i :=
begin
rw [linear_map.to_matrix, linear_equiv.trans_apply, linear_map.to_matrix'_apply,
linear_equiv.arrow_congr_apply, is_basis.equiv_fun_symm_apply, finset.sum_eq_single j,
if_pos rfl, one_smul],
{ intros j' _ hj',
rw [if_neg hj', zero_smul] },
{ intro hj,
have := finset.mem_univ j,
contradiction }
end
lemma linear_map.to_matrix_transpose_apply (f : M₁ →ₗ[R] M₂) (j : n) :
(linear_map.to_matrix hv₁ hv₂ f)ᵀ j = hv₂.equiv_fun (f (v₁ j)) :=
funext $ λ i, f.to_matrix_apply _ _ i j
lemma linear_map.to_matrix_apply' (f : M₁ →ₗ[R] M₂) (i : m) (j : n) :
linear_map.to_matrix hv₁ hv₂ f i j = hv₂.repr (f (v₁ j)) i :=
linear_map.to_matrix_apply hv₁ hv₂ f i j
lemma linear_map.to_matrix_transpose_apply' (f : M₁ →ₗ[R] M₂) (j : n) :
(linear_map.to_matrix hv₁ hv₂ f)ᵀ j = hv₂.repr (f (v₁ j)) :=
linear_map.to_matrix_transpose_apply hv₁ hv₂ f j
lemma matrix.to_lin_apply (M : matrix m n R) (v : M₁) :
matrix.to_lin hv₁ hv₂ M v = ∑ j, M.mul_vec (hv₁.equiv_fun v) j • v₂ j :=
show hv₂.equiv_fun.symm (matrix.to_lin' M (hv₁.equiv_fun v)) = _,
by rw [matrix.to_lin'_apply, hv₂.equiv_fun_symm_apply]
@[simp] lemma matrix.to_lin_self (M : matrix m n R) (i : n) :
matrix.to_lin hv₁ hv₂ M (v₁ i) = ∑ j, M j i • v₂ j :=
by simp only [matrix.to_lin_apply, matrix.mul_vec, dot_product, hv₁.equiv_fun_self, mul_boole,
finset.sum_ite_eq, finset.mem_univ, if_true]
/-- This will be a special case of `linear_map.to_matrix_id_eq_basis_to_matrix`. -/
lemma linear_map.to_matrix_id : linear_map.to_matrix hv₁ hv₁ id = 1 :=
begin
ext i j,
simp [linear_map.to_matrix_apply, is_basis.equiv_fun, matrix.one_apply, finsupp.single, eq_comm]
end
lemma linear_map.to_matrix_one : linear_map.to_matrix hv₁ hv₁ 1 = 1 :=
linear_map.to_matrix_id hv₁
@[simp]
lemma matrix.to_lin_one : matrix.to_lin hv₁ hv₁ 1 = id :=
by rw [← linear_map.to_matrix_id hv₁, matrix.to_lin_to_matrix]
theorem linear_map.to_matrix_range [decidable_eq M₁] [decidable_eq M₂]
(f : M₁ →ₗ[R] M₂) (k : m) (i : n) :
linear_map.to_matrix hv₁.range hv₂.range f ⟨v₂ k, mem_range_self k⟩ ⟨v₁ i, mem_range_self i⟩ =
linear_map.to_matrix hv₁ hv₂ f k i :=
by simp_rw [linear_map.to_matrix_apply, subtype.coe_mk, is_basis.equiv_fun_apply, hv₂.range_repr]
variables {M₃ : Type*} [add_comm_group M₃] [module R M₃] {v₃ : l → M₃} (hv₃ : is_basis R v₃)
lemma linear_map.to_matrix_comp [decidable_eq m] (f : M₂ →ₗ[R] M₃) (g : M₁ →ₗ[R] M₂) :
linear_map.to_matrix hv₁ hv₃ (f.comp g) =
linear_map.to_matrix hv₂ hv₃ f ⬝ linear_map.to_matrix hv₁ hv₂ g :=
by simp_rw [linear_map.to_matrix, linear_equiv.trans_apply,
linear_equiv.arrow_congr_comp _ hv₂.equiv_fun, linear_map.to_matrix'_comp]
lemma linear_map.to_matrix_mul (f g : M₁ →ₗ[R] M₁) :
linear_map.to_matrix hv₁ hv₁ (f * g) =
linear_map.to_matrix hv₁ hv₁ f ⬝ linear_map.to_matrix hv₁ hv₁ g :=
by { rw [show (@has_mul.mul (M₁ →ₗ[R] M₁) _) = linear_map.comp, from rfl,
linear_map.to_matrix_comp hv₁ hv₁ hv₁ f g] }
lemma linear_map.to_matrix_mul_vec_repr (f : M₁ →ₗ[R] M₂) (x : M₁) :
(linear_map.to_matrix hv₁ hv₂ f).mul_vec (hv₁.repr x) = hv₂.repr (f x) :=
by { ext i,
rw [← matrix.to_lin'_apply, linear_map.to_matrix, linear_equiv.trans_apply,
matrix.to_lin'_to_matrix', linear_equiv.arrow_congr_apply, hv₂.equiv_fun_apply],
congr,
exact hv₁.equiv_fun.symm_apply_apply x }
lemma matrix.to_lin_mul [decidable_eq m] (A : matrix l m R) (B : matrix m n R) :
matrix.to_lin hv₁ hv₃ (A ⬝ B) =
(matrix.to_lin hv₂ hv₃ A).comp (matrix.to_lin hv₁ hv₂ B) :=
begin
apply (linear_map.to_matrix hv₁ hv₃).injective,
haveI : decidable_eq l := λ _ _, classical.prop_decidable _,
rw linear_map.to_matrix_comp hv₁ hv₂ hv₃,
repeat { rw linear_map.to_matrix_to_lin },
end
/-- Given a basis of a module `M₁` over a commutative ring `R`, we get an algebra
equivalence between linear maps `M₁ →ₗ M₁` and square matrices over `R` indexed by the basis. -/
def linear_map.to_matrix_alg_equiv :
(M₁ →ₗ[R] M₁) ≃ₐ[R] matrix n n R :=
alg_equiv.of_linear_equiv (linear_map.to_matrix hv₁ hv₁) (linear_map.to_matrix_mul hv₁)
(by simp [module.algebra_map_End_eq_smul_id, linear_map.to_matrix_id])
/-- Given a basis of a module `M₁` over a commutative ring `R`, we get an algebra
equivalence between square matrices over `R` indexed by the basis and linear maps `M₁ →ₗ M₁`. -/
def matrix.to_lin_alg_equiv : matrix n n R ≃ₐ[R] (M₁ →ₗ[R] M₁) :=
(linear_map.to_matrix_alg_equiv hv₁).symm
@[simp] lemma linear_map.to_matrix_alg_equiv_symm :
(linear_map.to_matrix_alg_equiv hv₁).symm = matrix.to_lin_alg_equiv hv₁ :=
rfl
@[simp] lemma matrix.to_lin_alg_equiv_symm :
(matrix.to_lin_alg_equiv hv₁).symm = linear_map.to_matrix_alg_equiv hv₁ :=
rfl
@[simp] lemma matrix.to_lin_alg_equiv_to_matrix_alg_equiv (f : M₁ →ₗ[R] M₁) :
matrix.to_lin_alg_equiv hv₁ (linear_map.to_matrix_alg_equiv hv₁ f) = f :=
by rw [← matrix.to_lin_alg_equiv_symm, alg_equiv.apply_symm_apply]
@[simp] lemma linear_map.to_matrix_alg_equiv_to_lin_alg_equiv (M : matrix n n R) :
linear_map.to_matrix_alg_equiv hv₁ (matrix.to_lin_alg_equiv hv₁ M) = M :=
by rw [← matrix.to_lin_alg_equiv_symm, alg_equiv.symm_apply_apply]
lemma linear_map.to_matrix_alg_equiv_apply (f : M₁ →ₗ[R] M₁) (i j : n) :
linear_map.to_matrix_alg_equiv hv₁ f i j = hv₁.equiv_fun (f (v₁ j)) i :=
by simp [linear_map.to_matrix_alg_equiv, linear_map.to_matrix_apply]
lemma linear_map.to_matrix_alg_equiv_transpose_apply (f : M₁ →ₗ[R] M₁) (j : n) :
(linear_map.to_matrix_alg_equiv hv₁ f)ᵀ j = hv₁.equiv_fun (f (v₁ j)) :=
funext $ λ i, f.to_matrix_apply _ _ i j
lemma linear_map.to_matrix_alg_equiv_apply' (f : M₁ →ₗ[R] M₁) (i j : n) :
linear_map.to_matrix_alg_equiv hv₁ f i j = hv₁.repr (f (v₁ j)) i :=
linear_map.to_matrix_alg_equiv_apply hv₁ f i j
lemma linear_map.to_matrix_alg_equiv_transpose_apply' (f : M₁ →ₗ[R] M₁) (j : n) :
(linear_map.to_matrix_alg_equiv hv₁ f)ᵀ j = hv₁.repr (f (v₁ j)) :=
linear_map.to_matrix_alg_equiv_transpose_apply hv₁ f j
lemma matrix.to_lin_alg_equiv_apply (M : matrix n n R) (v : M₁) :
matrix.to_lin_alg_equiv hv₁ M v = ∑ j, M.mul_vec (hv₁.equiv_fun v) j • v₁ j :=
show hv₁.equiv_fun.symm (matrix.to_lin_alg_equiv' M (hv₁.equiv_fun v)) = _,
by rw [matrix.to_lin_alg_equiv'_apply, hv₁.equiv_fun_symm_apply]
@[simp] lemma matrix.to_lin_alg_equiv_self (M : matrix n n R) (i : n) :
matrix.to_lin_alg_equiv hv₁ M (v₁ i) = ∑ j, M j i • v₁ j :=
by simp only [matrix.to_lin_alg_equiv_apply, matrix.mul_vec, dot_product, hv₁.equiv_fun_self,
mul_boole, finset.sum_ite_eq, finset.mem_univ, if_true]
lemma linear_map.to_matrix_alg_equiv_id : linear_map.to_matrix_alg_equiv hv₁ id = 1 :=
by simp_rw [linear_map.to_matrix_alg_equiv, alg_equiv.of_linear_equiv_apply,
linear_map.to_matrix_id]
@[simp]
lemma matrix.to_lin_alg_equiv_one : matrix.to_lin_alg_equiv hv₁ 1 = id :=
by rw [← linear_map.to_matrix_alg_equiv_id hv₁, matrix.to_lin_alg_equiv_to_matrix_alg_equiv]
theorem linear_map.to_matrix_alg_equiv_range [decidable_eq M₁]
(f : M₁ →ₗ[R] M₁) (k i : n) :
linear_map.to_matrix_alg_equiv hv₁.range f ⟨v₁ k, mem_range_self k⟩ ⟨v₁ i, mem_range_self i⟩ =
linear_map.to_matrix_alg_equiv hv₁ f k i :=
by simp_rw [linear_map.to_matrix_alg_equiv_apply, subtype.coe_mk, is_basis.equiv_fun_apply,
hv₁.range_repr]
lemma linear_map.to_matrix_alg_equiv_comp (f g : M₁ →ₗ[R] M₁) :
linear_map.to_matrix_alg_equiv hv₁ (f.comp g) =
linear_map.to_matrix_alg_equiv hv₁ f ⬝ linear_map.to_matrix_alg_equiv hv₁ g :=
by simp [linear_map.to_matrix_alg_equiv, linear_map.to_matrix_comp hv₁ hv₁ hv₁ f g]
lemma linear_map.to_matrix_alg_equiv_mul (f g : M₁ →ₗ[R] M₁) :
linear_map.to_matrix_alg_equiv hv₁ (f * g) =
linear_map.to_matrix_alg_equiv hv₁ f ⬝ linear_map.to_matrix_alg_equiv hv₁ g :=
by { rw [show (@has_mul.mul (M₁ →ₗ[R] M₁) _) = linear_map.comp, from rfl,
linear_map.to_matrix_alg_equiv_comp hv₁ f g] }
lemma matrix.to_lin_alg_equiv_mul (A B : matrix n n R) :
matrix.to_lin_alg_equiv hv₁ (A ⬝ B) =
(matrix.to_lin_alg_equiv hv₁ A).comp (matrix.to_lin_alg_equiv hv₁ B) :=
by convert matrix.to_lin_mul hv₁ hv₁ hv₁ A B
end to_matrix
section is_basis_to_matrix
variables {ι ι' κ κ' : Type*} [fintype ι] [fintype ι'] [fintype κ] [fintype κ']
variables {R M : Type*} [comm_ring R] [add_comm_group M] [module R M]
open function matrix
/-- From a basis `e : ι → M` and a family of vectors `v : ι' → M`, make the matrix whose columns
are the vectors `v i` written in the basis `e`. -/
def is_basis.to_matrix {e : ι → M} (he : is_basis R e) (v : ι' → M) : matrix ι ι' R :=
λ i j, he.equiv_fun (v j) i
variables {e : ι → M} (he : is_basis R e) (v : ι' → M) (i : ι) (j : ι')
namespace is_basis
lemma to_matrix_apply : he.to_matrix v i j = he.equiv_fun (v j) i :=
rfl
lemma to_matrix_transpose_apply : (he.to_matrix v)ᵀ j = he.repr (v j) :=
funext $ (λ _, rfl)
lemma to_matrix_eq_to_matrix_constr [decidable_eq ι] (v : ι → M) :
he.to_matrix v = linear_map.to_matrix he he (he.constr v) :=
by { ext, simp [is_basis.to_matrix_apply, linear_map.to_matrix_apply] }
@[simp] lemma to_matrix_self [decidable_eq ι] : he.to_matrix e = 1 :=
begin
rw is_basis.to_matrix,
ext i j,
simp [is_basis.equiv_fun, matrix.one_apply, finsupp.single, eq_comm]
end
lemma to_matrix_update [decidable_eq ι'] (x : M) :
he.to_matrix (function.update v j x) = matrix.update_column (he.to_matrix v) j (he.repr x) :=
begin
ext i' k,
rw [is_basis.to_matrix, matrix.update_column_apply, he.to_matrix_apply],
split_ifs,
{ rw [h, update_same j x v, he.equiv_fun_apply] },
{ rw update_noteq h },
end
@[simp] lemma sum_to_matrix_smul_self : ∑ (i : ι), he.to_matrix v i j • e i = v j :=
begin
conv_rhs { rw ← he.total_repr (v j) },
rw [finsupp.total_apply, finsupp.sum_fintype],
{ refl },
simp
end
@[simp] lemma to_lin_to_matrix [decidable_eq ι'] (hv : is_basis R v) :
matrix.to_lin hv he (he.to_matrix v) = id :=
hv.ext (λ i, by rw [to_lin_self, id_apply, he.sum_to_matrix_smul_self])
/-- From a basis `e : ι → M`, build a linear equivalence between families of vectors `v : ι → M`,
and matrices, making the matrix whose columns are the vectors `v i` written in the basis `e`. -/
def to_matrix_equiv {e : ι → M} (he : is_basis R e) : (ι → M) ≃ₗ[R] matrix ι ι R :=
{ to_fun := he.to_matrix,
map_add' := λ v w, begin
ext i j,
change _ = _ + _,
simp [he.to_matrix_apply]
end,
map_smul' := begin
intros c v,
ext i j,
simp [he.to_matrix_apply]
end,
inv_fun := λ m j, ∑ i, (m i j) • e i,
left_inv := begin
intro v,
ext j,
simp [he.to_matrix_apply, he.equiv_fun_total (v j)]
end,
right_inv := begin
intros x,
ext k l,
simp [he.to_matrix_apply, he.equiv_fun.map_sum, he.equiv_fun.map_smul,
fintype.sum_apply k (λ i, x i l • he.equiv_fun (e i)),
he.equiv_fun_self]
end }
end is_basis
section mul_linear_map_to_matrix
variables {N : Type*} [add_comm_group N] [module R N]
variables {b : ι → M} {b' : ι' → M} {c : κ → N} {c' : κ' → N}
variables (hb : is_basis R b) (hb' : is_basis R b') (hc : is_basis R c) (hc' : is_basis R c')
variables (f : M →ₗ[R] N)
open linear_map
@[simp] lemma is_basis_to_matrix_mul_linear_map_to_matrix [decidable_eq ι'] :
hc.to_matrix c' ⬝ linear_map.to_matrix hb' hc' f = linear_map.to_matrix hb' hc f :=
(matrix.to_lin hb' hc).injective
(by haveI := classical.dec_eq κ';
rw [to_lin_to_matrix, to_lin_mul hb' hc' hc, to_lin_to_matrix, hc.to_lin_to_matrix, id_comp])
@[simp] lemma linear_map_to_matrix_mul_is_basis_to_matrix [decidable_eq ι] [decidable_eq ι'] :
linear_map.to_matrix hb' hc' f ⬝ hb'.to_matrix b = linear_map.to_matrix hb hc' f :=
(matrix.to_lin hb hc').injective
(by rw [to_lin_to_matrix, to_lin_mul hb hb' hc', to_lin_to_matrix, hb'.to_lin_to_matrix, comp_id])
/-- A generalization of `linear_map.to_matrix_id`. -/
@[simp] lemma linear_map.to_matrix_id_eq_basis_to_matrix [decidable_eq ι] :
linear_map.to_matrix hb hb' id = hb'.to_matrix b :=
by { haveI := classical.dec_eq ι',
rw [← is_basis_to_matrix_mul_linear_map_to_matrix hb hb', to_matrix_id, matrix.mul_one] }
/-- A generalization of `is_basis.to_matrix_self`, in the opposite direction. -/
@[simp] lemma is_basis.to_matrix_mul_to_matrix
{ι'' : Type*} [fintype ι''] {b'' : ι'' → M} (hb'' : is_basis R b'') :
hb.to_matrix b' ⬝ hb'.to_matrix b'' = hb.to_matrix b'' :=
begin
haveI := classical.dec_eq ι,
haveI := classical.dec_eq ι',
haveI := classical.dec_eq ι'',
rw [← linear_map.to_matrix_id_eq_basis_to_matrix hb' hb,
← linear_map.to_matrix_id_eq_basis_to_matrix hb'' hb',
← to_matrix_comp, id_comp, linear_map.to_matrix_id_eq_basis_to_matrix],
end
end mul_linear_map_to_matrix
end is_basis_to_matrix
open_locale matrix
section det
open linear_map matrix
variables {R : Type*} [comm_ring R]
variables {M : Type*} [add_comm_group M] [module R M]
variables {M' : Type*} [add_comm_group M'] [module R M']
variables {ι : Type*} [decidable_eq ι] [fintype ι] {v : ι → M} {v' : ι → M'}
lemma linear_equiv.is_unit_det (f : M ≃ₗ[R] M') (hv : is_basis R v) (hv' : is_basis R v') :
is_unit (linear_map.to_matrix hv hv' f).det :=
begin
apply is_unit_det_of_left_inverse,
simpa using (linear_map.to_matrix_comp hv hv' hv f.symm f).symm
end
/-- Builds a linear equivalence from a linear map whose determinant in some bases is a unit. -/
def linear_equiv.of_is_unit_det {f : M →ₗ[R] M'} {hv : is_basis R v} {hv' : is_basis R v'}
(h : is_unit (linear_map.to_matrix hv hv' f).det) : M ≃ₗ[R] M' :=
{ to_fun := f,
map_add' := f.map_add,
map_smul' := f.map_smul,
inv_fun := to_lin hv' hv (to_matrix hv hv' f)⁻¹,
left_inv := λ x,
calc to_lin hv' hv (to_matrix hv hv' f)⁻¹ (f x)
= to_lin hv hv ((to_matrix hv hv' f)⁻¹ ⬝ to_matrix hv hv' f) x :
by { rw [to_lin_mul hv hv' hv, to_lin_to_matrix, linear_map.comp_apply] }
... = x : by simp [h],
right_inv := λ x,
calc f (to_lin hv' hv (to_matrix hv hv' f)⁻¹ x)
= to_lin hv' hv' (to_matrix hv hv' f ⬝ (to_matrix hv hv' f)⁻¹) x :
by { rw [to_lin_mul hv' hv hv', linear_map.comp_apply, to_lin_to_matrix hv hv'] }
... = x : by simp [h],
}
variables {e : ι → M} (he : is_basis R e)
/-- The determinant of a family of vectors with respect to some basis, as an alternating
multilinear map. -/
def is_basis.det : alternating_map R M R ι :=
{ to_fun := λ v, det (he.to_matrix v),
map_add' := begin
intros v i x y,
simp only [he.to_matrix_update, linear_map.map_add],
apply det_update_column_add
end,
map_smul' := begin
intros u i c x,
simp only [he.to_matrix_update, algebra.id.smul_eq_mul, map_smul_of_tower],
apply det_update_column_smul
end,
map_eq_zero_of_eq' := begin
intros v i j h hij,
rw [←function.update_eq_self i v, h, ←det_transpose, he.to_matrix_update,
←update_row_transpose, ←he.to_matrix_transpose_apply],
apply det_zero_of_row_eq hij,
rw [update_row_ne hij.symm, update_row_self],
end }
lemma is_basis.det_apply (v : ι → M) : he.det v = det (he.to_matrix v) := rfl
lemma is_basis.det_self : he.det e = 1 :=
by simp [he.det_apply]
lemma is_basis.iff_det {v : ι → M} : is_basis R v ↔ is_unit (he.det v) :=
begin
split,
{ intro hv,
suffices :
is_unit (linear_map.to_matrix he he (linear_equiv_of_is_basis he hv $ equiv.refl ι)).det,
{ rw [is_basis.det_apply, is_basis.to_matrix_eq_to_matrix_constr],
exact this },
apply linear_equiv.is_unit_det },
{ intro h,
rw [is_basis.det_apply, is_basis.to_matrix_eq_to_matrix_constr] at h,
convert linear_equiv.is_basis he (linear_equiv.of_is_unit_det h),
ext i,
exact (constr_basis he).symm },
end
end det
section transpose
variables {K V₁ V₂ ι₁ ι₂ : Type*} [field K]
[add_comm_group V₁] [module K V₁]
[add_comm_group V₂] [module K V₂]
[fintype ι₁] [fintype ι₂] [decidable_eq ι₁] [decidable_eq ι₂]
{B₁ : ι₁ → V₁} (h₁ : is_basis K B₁)
{B₂ : ι₂ → V₂} (h₂ : is_basis K B₂)
@[simp] lemma linear_map.to_matrix_transpose (u : V₁ →ₗ[K] V₂) :
linear_map.to_matrix h₂.dual_basis_is_basis h₁.dual_basis_is_basis (module.dual.transpose u) =
(linear_map.to_matrix h₁ h₂ u)ᵀ :=
begin
ext i j,
simp only [linear_map.to_matrix_apply, module.dual.transpose_apply, h₁.dual_basis_equiv_fun,
h₂.dual_basis_apply, matrix.transpose_apply, linear_map.comp_apply]
end
lemma linear_map.to_matrix_symm_transpose (M : matrix ι₁ ι₂ K) :
(linear_map.to_matrix h₁.dual_basis_is_basis h₂.dual_basis_is_basis).symm Mᵀ =
module.dual.transpose (matrix.to_lin h₂ h₁ M) :=
begin
apply (linear_map.to_matrix h₁.dual_basis_is_basis h₂.dual_basis_is_basis).injective,
rw [linear_equiv.apply_symm_apply],
ext i j,
simp only [linear_map.to_matrix_apply, module.dual.transpose_apply, h₂.dual_basis_equiv_fun,
h₁.dual_basis_apply, matrix.transpose_apply, linear_map.comp_apply, if_true,
matrix.to_lin_apply, linear_equiv.map_smul, mul_boole, algebra.id.smul_eq_mul,
linear_equiv.map_sum, is_basis.equiv_fun_self, fintype.sum_apply, finset.sum_ite_eq',
finset.sum_ite_eq, is_basis.equiv_fun_symm_apply, pi.smul_apply, matrix.to_lin_apply,
matrix.mul_vec, matrix.dot_product, is_basis.equiv_fun_self, finset.mem_univ]
end
end transpose
namespace matrix
section trace
variables {m : Type*} [fintype m] (n : Type*) [fintype n]
variables (R : Type v) (M : Type w) [semiring R] [add_comm_monoid M] [module R M]
/--
The diagonal of a square matrix.
-/
def diag : (matrix n n M) →ₗ[R] n → M :=
{ to_fun := λ A i, A i i,
map_add' := by { intros, ext, refl, },
map_smul' := by { intros, ext, refl, } }
variables {n} {R} {M}
@[simp] lemma diag_apply (A : matrix n n M) (i : n) : diag n R M A i = A i i := rfl
@[simp] lemma diag_one [decidable_eq n] :
diag n R R 1 = λ i, 1 := by { dunfold diag, ext, simp [one_apply_eq] }
@[simp] lemma diag_transpose (A : matrix n n M) : diag n R M Aᵀ = diag n R M A := rfl
variables (n) (R) (M)
/--
The trace of a square matrix.
-/
def trace : (matrix n n M) →ₗ[R] M :=
{ to_fun := λ A, ∑ i, diag n R M A i,
map_add' := by { intros, apply finset.sum_add_distrib, },
map_smul' := by { intros, simp [finset.smul_sum], } }
variables {n} {R} {M}
@[simp] lemma trace_diag (A : matrix n n M) : trace n R M A = ∑ i, diag n R M A i := rfl
lemma trace_apply (A : matrix n n M) : trace n R M A = ∑ i, A i i := rfl
@[simp] lemma trace_one [decidable_eq n] :
trace n R R 1 = fintype.card n :=
have h : trace n R R 1 = ∑ i, diag n R R 1 i := rfl,
by simp_rw [h, diag_one, finset.sum_const, nsmul_one]; refl
@[simp] lemma trace_transpose (A : matrix n n M) : trace n R M Aᵀ = trace n R M A := rfl
@[simp] lemma trace_transpose_mul (A : matrix m n R) (B : matrix n m R) :
trace n R R (Aᵀ ⬝ Bᵀ) = trace m R R (A ⬝ B) := finset.sum_comm
lemma trace_mul_comm {S : Type v} [comm_ring S] (A : matrix m n S) (B : matrix n m S) :
trace n S S (B ⬝ A) = trace m S S (A ⬝ B) :=
by rw [←trace_transpose, ←trace_transpose_mul, transpose_mul]
end trace
section ring
variables {n : Type*} [fintype n] [decidable_eq n] {R : Type v} [comm_ring R]
open linear_map matrix
lemma proj_diagonal (i : n) (w : n → R) :
(proj i).comp (to_lin' (diagonal w)) = (w i) • proj i :=
by ext j; simp [mul_vec_diagonal]
lemma diagonal_comp_std_basis (w : n → R) (i : n) :
(diagonal w).to_lin'.comp (std_basis R (λ_:n, R) i) = (w i) • std_basis R (λ_:n, R) i :=
begin
ext j,
simp_rw [linear_map.comp_apply, to_lin'_apply, mul_vec_diagonal, linear_map.smul_apply,
pi.smul_apply, algebra.id.smul_eq_mul],
by_cases i = j,
{ subst h },
{ rw [std_basis_ne R (λ_:n, R) _ _ (ne.symm h), _root_.mul_zero, _root_.mul_zero] }
end
lemma diagonal_to_lin' (w : n → R) :
(diagonal w).to_lin' = linear_map.pi (λi, w i • linear_map.proj i) :=
by ext v j; simp [mul_vec_diagonal]
/-- An invertible matrix yields a linear equivalence from the free module to itself.
See `matrix.to_linear_equiv` for the same map on arbitrary modules.
-/
def to_linear_equiv' (P : matrix n n R) (h : is_unit P) : (n → R) ≃ₗ[R] (n → R) :=
have h' : is_unit P.det := P.is_unit_iff_is_unit_det.mp h,
{ inv_fun := P⁻¹.to_lin',
left_inv := λ v,
show (P⁻¹.to_lin'.comp P.to_lin') v = v,
by rw [← matrix.to_lin'_mul, P.nonsing_inv_mul h', matrix.to_lin'_one, linear_map.id_apply],
right_inv := λ v,
show (P.to_lin'.comp P⁻¹.to_lin') v = v,
by rw [← matrix.to_lin'_mul, P.mul_nonsing_inv h', matrix.to_lin'_one, linear_map.id_apply],
..P.to_lin' }
@[simp] lemma to_linear_equiv'_apply (P : matrix n n R) (h : is_unit P) :
(↑(P.to_linear_equiv' h) : module.End R (n → R)) = P.to_lin' := rfl
@[simp] lemma to_linear_equiv'_symm_apply (P : matrix n n R) (h : is_unit P) :
(↑(P.to_linear_equiv' h).symm : module.End R (n → R)) = P⁻¹.to_lin' := rfl
end ring
section module
variables {m n : Type*} [fintype m] [fintype n]
variables {K : Type u} [field K] -- maybe try to relax the universe constraint
open linear_map matrix
lemma rank_vec_mul_vec {m n : Type u} [fintype m] [fintype n] [decidable_eq n]
(w : m → K) (v : n → K) :
rank (vec_mul_vec w v).to_lin' ≤ 1 :=
begin
rw [vec_mul_vec_eq, to_lin'_mul],
refine le_trans (rank_comp_le1 _ _) _,
refine le_trans (rank_le_domain _) _,
rw [dim_fun', ← cardinal.lift_eq_nat_iff.mpr (cardinal.fintype_card unit), cardinal.mk_unit],
exact le_of_eq (cardinal.lift_one)
end
lemma ker_diagonal_to_lin' [decidable_eq m] (w : m → K) :
ker (diagonal w).to_lin' = (⨆i∈{i | w i = 0 }, range (std_basis K (λi, K) i)) :=
begin
rw [← comap_bot, ← infi_ker_proj],
simp only [comap_infi, (ker_comp _ _).symm, proj_diagonal, ker_smul'],
have : univ ⊆ {i : m | w i = 0} ∪ {i : m | w i = 0}ᶜ, { rw set.union_compl_self },
exact (supr_range_std_basis_eq_infi_ker_proj K (λi:m, K)
disjoint_compl_right this (finite.of_fintype _)).symm
end
lemma range_diagonal [decidable_eq m] (w : m → K) :
(diagonal w).to_lin'.range = (⨆ i ∈ {i | w i ≠ 0}, (std_basis K (λi, K) i).range) :=
begin
dsimp only [mem_set_of_eq],
rw [← map_top, ← supr_range_std_basis, map_supr],
congr, funext i,
rw [← linear_map.range_comp, diagonal_comp_std_basis, ← range_smul']
end
lemma rank_diagonal [decidable_eq m] [decidable_eq K] (w : m → K) :
rank (diagonal w).to_lin' = fintype.card { i // w i ≠ 0 } :=
begin
have hu : univ ⊆ {i : m | w i = 0}ᶜ ∪ {i : m | w i = 0}, { rw set.compl_union_self },
have hd : disjoint {i : m | w i ≠ 0} {i : m | w i = 0} := disjoint_compl_left,
have h₁ := supr_range_std_basis_eq_infi_ker_proj K (λi:m, K) hd hu (finite.of_fintype _),
have h₂ := @infi_ker_proj_equiv K _ _ (λi:m, K) _ _ _ _ (by simp; apply_instance) hd hu,
rw [rank, range_diagonal, h₁, ←@dim_fun' K],
apply linear_equiv.dim_eq,
apply h₂,
end
variables {R M : Type*} [comm_ring R] [add_comm_group M] [module R M]
variables {b : n → M} (hb : is_basis R b)
include hb
/-- Given `hA : is_unit A.det` and `hb : is_basis R b`, `A.to_linear_equiv hb hA` is
the `linear_equiv` arising from `to_lin hb hb A`.
See `matrix.to_linear_equiv'` for this result on `n → R`.
-/
@[simps apply]
def to_linear_equiv [decidable_eq n] (A : matrix n n R) (hA : is_unit A.det) :
M ≃ₗ[R] M :=
begin
refine ⟨to_lin hb hb A, linear_map.map_add _, linear_map.map_smul _, to_lin hb hb A⁻¹,
λ x, _, λ x, _⟩;
simp only [← linear_map.comp_apply, ← to_lin_mul,
matrix.nonsing_inv_mul _ hA, matrix.mul_nonsing_inv _ hA,
to_lin_one, linear_map.id_apply]
end
lemma ker_to_lin_eq_bot [decidable_eq n] (A : matrix n n R) (hA : is_unit A.det) :
(to_lin hb hb A).ker = ⊥ :=
ker_eq_bot.mpr (to_linear_equiv hb A hA).injective
lemma range_to_lin_eq_top [decidable_eq n] (A : matrix n n R) (hA : is_unit A.det) :
(to_lin hb hb A).range = ⊤ :=
range_eq_top.mpr (to_linear_equiv hb A hA).surjective
end module
section finite_dimensional
variables {m n : Type*} [fintype m] [fintype n]
variables {R : Type v} [field R]
instance : finite_dimensional R (matrix m n R) :=
linear_equiv.finite_dimensional (linear_equiv.uncurry R m n).symm
/--
The dimension of the space of finite dimensional matrices
is the product of the number of rows and columns.
-/
@[simp] lemma finrank_matrix :
finite_dimensional.finrank R (matrix m n R) = fintype.card m * fintype.card n :=
by rw [@linear_equiv.finrank_eq R (matrix m n R) _ _ _ _ _ _ (linear_equiv.uncurry R m n),
finite_dimensional.finrank_fintype_fun_eq_card, fintype.card_prod]
end finite_dimensional
section reindexing
variables {l m n : Type*} [fintype l] [fintype m] [fintype n]
variables {l' m' n' : Type*} [fintype l'] [fintype m'] [fintype n']
variables {R : Type v}
/-- The natural map that reindexes a matrix's rows and columns with equivalent types,
`matrix.reindex`, is a linear equivalence. -/
def reindex_linear_equiv [semiring R] (eₘ : m ≃ m') (eₙ : n ≃ n') :
matrix m n R ≃ₗ[R] matrix m' n' R :=
{ map_add' := λ M N, rfl,
map_smul' := λ M N, rfl,
..(reindex eₘ eₙ)}
@[simp] lemma reindex_linear_equiv_apply [semiring R]
(eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m n R) :
reindex_linear_equiv eₘ eₙ M = reindex eₘ eₙ M :=
rfl
@[simp] lemma reindex_linear_equiv_symm [semiring R] (eₘ : m ≃ m') (eₙ : n ≃ n') :
(reindex_linear_equiv eₘ eₙ : _ ≃ₗ[R] _).symm = reindex_linear_equiv eₘ.symm eₙ.symm :=
rfl
@[simp] lemma reindex_linear_equiv_refl_refl [semiring R] :
reindex_linear_equiv (equiv.refl m) (equiv.refl n) = linear_equiv.refl R _ :=
linear_equiv.ext $ λ _, rfl
/-- For square matrices, the natural map that reindexes a matrix's rows and columns with equivalent
types, `matrix.reindex`, is an equivalence of algebras. -/
def reindex_alg_equiv [comm_semiring R] [decidable_eq m] [decidable_eq n]
(e : m ≃ n) : matrix m m R ≃ₐ[R] matrix n n R :=
{ to_fun := reindex e e,
map_mul' := λ M N, minor_mul_equiv M N e.symm e.symm e.symm,
commutes' := λ r, by simp [algebra_map, algebra.to_ring_hom, minor_smul],
..(reindex_linear_equiv e e) }
@[simp] lemma reindex_alg_equiv_apply [comm_semiring R] [decidable_eq m] [decidable_eq n]
(e : m ≃ n) (M : matrix m m R) :
reindex_alg_equiv e M = reindex e e M :=
rfl
@[simp] lemma reindex_alg_equiv_symm [comm_semiring R] [decidable_eq m] [decidable_eq n]
(e : m ≃ n) :
(reindex_alg_equiv e : _ ≃ₐ[R] _).symm = reindex_alg_equiv e.symm :=
rfl
@[simp] lemma reindex_alg_equiv_refl [comm_semiring R] [decidable_eq m] :
reindex_alg_equiv (equiv.refl m) = (alg_equiv.refl : _ ≃ₐ[R] _) :=
alg_equiv.ext $ λ _, rfl
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_minor_equiv_self`.
-/
lemma det_reindex_linear_equiv_self [decidable_eq m] [decidable_eq n] [comm_ring R]
(e : m ≃ n) (A : matrix m m R) :
det (reindex_linear_equiv e e A) = det A :=
det_reindex_self e A
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_minor_equiv_self`.
-/
lemma det_reindex_alg_equiv [decidable_eq m] [decidable_eq n] [comm_ring R]
(e : m ≃ n) (A : matrix m m R) :
det (reindex_alg_equiv e A) = det A :=
det_reindex_self e A
end reindexing
end matrix
namespace algebra
section lmul
variables {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T]
variables [algebra R S] [algebra S T] [algebra R T] [is_scalar_tower R S T]
variables {m n : Type*} [fintype m] [decidable_eq m] [fintype n] [decidable_eq n]
variables {b : m → S} (hb : is_basis R b) {c : n → T} (hc : is_basis S c)
open algebra
lemma to_matrix_lmul' (x : S) (i j) :
linear_map.to_matrix hb hb (lmul R S x) i j = hb.repr (x * b j) i :=
by rw [linear_map.to_matrix_apply', lmul_apply]
@[simp] lemma to_matrix_lsmul (x : R) (i j) :
linear_map.to_matrix hb hb (algebra.lsmul R S x) i j = if i = j then x else 0 :=
by { rw [linear_map.to_matrix_apply', algebra.lsmul_coe, linear_map.map_smul, finsupp.smul_apply,
hb.repr_self_apply, smul_eq_mul, mul_boole],
congr' 1; simp only [eq_comm] }
/-- `left_mul_matrix hb x` is the matrix corresponding to the linear map `λ y, x * y`.
`left_mul_matrix_eq_repr_mul` gives a formula for the entries of `left_mul_matrix`.
This definition is useful for doing (more) explicit computations with `algebra.lmul`,
such as the trace form or norm map for algebras.
-/
noncomputable def left_mul_matrix : S →ₐ[R] matrix m m R :=
{ to_fun := λ x, linear_map.to_matrix hb hb (algebra.lmul R S x),
map_zero' := by rw [alg_hom.map_zero, linear_equiv.map_zero],
map_one' := by rw [alg_hom.map_one, linear_map.to_matrix_one],
map_add' := λ x y, by rw [alg_hom.map_add, linear_equiv.map_add],
map_mul' := λ x y, by rw [alg_hom.map_mul, linear_map.to_matrix_mul, matrix.mul_eq_mul],
commutes' := λ r, by { ext, rw [lmul_algebra_map, to_matrix_lsmul,
algebra_map_matrix_apply, id.map_eq_self] } }
lemma left_mul_matrix_apply (x : S) :
left_mul_matrix hb x = linear_map.to_matrix hb hb (lmul R S x) := rfl
lemma left_mul_matrix_eq_repr_mul (x : S) (i j) :
left_mul_matrix hb x i j = hb.repr (x * b j) i :=
-- This is defeq to just `to_matrix_lmul' hb x i j`,
-- but the unfolding goes a lot faster with this explicit `rw`.
by rw [left_mul_matrix_apply, to_matrix_lmul' hb x i j]
lemma left_mul_matrix_mul_vec_repr (x y : S) :
(left_mul_matrix hb x).mul_vec (hb.repr y) = hb.repr (x * y) :=
linear_map.to_matrix_mul_vec_repr hb hb (algebra.lmul R S x) y
@[simp] lemma to_matrix_lmul_eq (x : S) :
linear_map.to_matrix hb hb (lmul R S x) = left_mul_matrix hb x :=
rfl
lemma left_mul_matrix_injective : function.injective (left_mul_matrix hb) :=
λ x x' h, calc x = algebra.lmul R S x 1 : (mul_one x).symm
... = algebra.lmul R S x' 1 : by rw (linear_map.to_matrix hb hb).injective h
... = x' : mul_one x'
lemma smul_left_mul_matrix (x) (i j) (k k') :
left_mul_matrix (hb.smul hc) x (i, k) (j, k') =
left_mul_matrix hb (left_mul_matrix hc x k k') i j :=
by simp only [left_mul_matrix_apply, linear_map.to_matrix_apply, is_basis.equiv_fun_apply, mul_comm,
is_basis.smul_repr, finsupp.smul_apply, algebra.lmul_apply, id.smul_eq_mul,
linear_map.map_smul, mul_smul_comm]
lemma smul_left_mul_matrix_algebra_map (x : S) :
left_mul_matrix (hb.smul hc) (algebra_map _ _ x) = block_diagonal (λ k, left_mul_matrix hb x) :=
begin
ext ⟨i, k⟩ ⟨j, k'⟩,
rw [smul_left_mul_matrix, alg_hom.commutes, block_diagonal_apply, algebra_map_matrix_apply],
split_ifs with h; simp [h],
end
lemma smul_left_mul_matrix_algebra_map_eq (x : S) (i j k) :
left_mul_matrix (hb.smul hc) (algebra_map _ _ x) (i, k) (j, k) = left_mul_matrix hb x i j :=
by rw [smul_left_mul_matrix_algebra_map, block_diagonal_apply_eq]
lemma smul_left_mul_matrix_algebra_map_ne (x : S) (i j) {k k'}
(h : k ≠ k') : left_mul_matrix (hb.smul hc) (algebra_map _ _ x) (i, k) (j, k') = 0 :=
by rw [smul_left_mul_matrix_algebra_map, block_diagonal_apply_ne _ _ _ h]
end lmul
end algebra
namespace linear_map
open_locale matrix
/-- The trace of an endomorphism given a basis. -/
def trace_aux (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b) :
(M →ₗ[R] M) →ₗ[R] R :=
(matrix.trace ι R R).comp $ linear_map.to_matrix hb hb
-- Can't be `simp` because it would cause a loop.
lemma trace_aux_def (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b) (f : M →ₗ[R] M) :
trace_aux R hb f = matrix.trace ι R R (linear_map.to_matrix hb hb f) :=
rfl
theorem trace_aux_eq' (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b)
{κ : Type w} [decidable_eq κ] [fintype κ] {c : κ → M} (hc : is_basis R c) :
trace_aux R hb = trace_aux R hc :=
linear_map.ext $ λ f,
calc matrix.trace ι R R (linear_map.to_matrix hb hb f)
= matrix.trace ι R R (linear_map.to_matrix hb hb ((linear_map.id.comp f).comp linear_map.id)) :
by rw [linear_map.id_comp, linear_map.comp_id]
... = matrix.trace ι R R (linear_map.to_matrix hc hb linear_map.id ⬝
linear_map.to_matrix hc hc f ⬝
linear_map.to_matrix hb hc linear_map.id) :
by rw [linear_map.to_matrix_comp _ hc, linear_map.to_matrix_comp _ hc]
... = matrix.trace κ R R (linear_map.to_matrix hc hc f ⬝
linear_map.to_matrix hb hc linear_map.id ⬝
linear_map.to_matrix hc hb linear_map.id) :
by rw [matrix.mul_assoc, matrix.trace_mul_comm]
... = matrix.trace κ R R (linear_map.to_matrix hc hc ((f.comp linear_map.id).comp linear_map.id)) :
by rw [linear_map.to_matrix_comp _ hb, linear_map.to_matrix_comp _ hc]
... = matrix.trace κ R R (linear_map.to_matrix hc hc f) :
by rw [linear_map.comp_id, linear_map.comp_id]
open_locale classical
theorem trace_aux_range (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type w} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b) :
trace_aux R hb.range = trace_aux R hb :=
linear_map.ext $ λ f, if H : 0 = 1 then eq_of_zero_eq_one H _ _ else
begin
haveI : nontrivial R := ⟨⟨0, 1, H⟩⟩,
change ∑ i : set.range b, _ = ∑ i : ι, _, simp_rw [matrix.diag_apply], symmetry,
convert (equiv.of_injective _ hb.injective).sum_comp _, ext i,
exact (linear_map.to_matrix_range hb hb f i i).symm
end
/-- where `ι` and `κ` can reside in different universes -/
theorem trace_aux_eq (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
{ι : Type*} [decidable_eq ι] [fintype ι] {b : ι → M} (hb : is_basis R b)
{κ : Type*} [decidable_eq κ] [fintype κ] {c : κ → M} (hc : is_basis R c) :
trace_aux R hb = trace_aux R hc :=
calc trace_aux R hb
= trace_aux R hb.range : by rw trace_aux_range R hb
... = trace_aux R hc.range : trace_aux_eq' _ _ _
... = trace_aux R hc : by rw trace_aux_range R hc
/-- Trace of an endomorphism independent of basis. -/
def trace (R : Type u) [comm_ring R] (M : Type v) [add_comm_group M] [module R M] :
(M →ₗ[R] M) →ₗ[R] R :=
if H : ∃ s : finset M, is_basis R (λ x, x : (↑s : set M) → M)
then trace_aux R (classical.some_spec H)
else 0
theorem trace_eq_matrix_trace (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M]
[module R M] {ι : Type w} [fintype ι] [decidable_eq ι] {b : ι → M} (hb : is_basis R b)
(f : M →ₗ[R] M) :
trace R M f = matrix.trace ι R R (linear_map.to_matrix hb hb f) :=
have ∃ s : finset M, is_basis R (λ x, x : (↑s : set M) → M),
from ⟨finset.univ.image b,
by { rw [finset.coe_image, finset.coe_univ, set.image_univ], exact hb.range }⟩,
by { rw [trace, dif_pos this, ← trace_aux_def], congr' 1, apply trace_aux_eq }
theorem trace_mul_comm (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M]
(f g : M →ₗ[R] M) : trace R M (f * g) = trace R M (g * f) :=
if H : ∃ s : finset M, is_basis R (λ x, x : (↑s : set M) → M) then let ⟨s, hb⟩ := H in
by { simp_rw [trace_eq_matrix_trace R hb, linear_map.to_matrix_mul], apply matrix.trace_mul_comm }
else by rw [trace, dif_neg H, linear_map.zero_apply, linear_map.zero_apply]
section finite_dimensional
variables {K : Type*} [field K]
variables {V : Type*} [add_comm_group V] [module K V] [finite_dimensional K V]
variables {W : Type*} [add_comm_group W] [module K W] [finite_dimensional K W]
instance : finite_dimensional K (V →ₗ[K] W) :=
begin
classical,
cases finite_dimensional.exists_is_basis_finset K V with bV hbV,
cases finite_dimensional.exists_is_basis_finset K W with bW hbW,
apply linear_equiv.finite_dimensional (linear_map.to_matrix hbV hbW).symm,
end
/--
The dimension of the space of linear transformations is the product of the dimensions of the
domain and codomain.
-/
@[simp] lemma finrank_linear_map :
finite_dimensional.finrank K (V →ₗ[K] W) =
(finite_dimensional.finrank K V) * (finite_dimensional.finrank K W) :=
begin
classical,
cases finite_dimensional.exists_is_basis_finset K V with bV hbV,
cases finite_dimensional.exists_is_basis_finset K W with bW hbW,
rw [linear_equiv.finrank_eq (linear_map.to_matrix hbV hbW), matrix.finrank_matrix,
finite_dimensional.finrank_eq_card_basis hbV, finite_dimensional.finrank_eq_card_basis hbW,
mul_comm],
end
end finite_dimensional
end linear_map
/-- The natural equivalence between linear endomorphisms of finite free modules and square matrices
is compatible with the algebra structures. -/
def alg_equiv_matrix' {R : Type v} [comm_ring R] {n : Type*} [fintype n] [decidable_eq n] :
module.End R (n → R) ≃ₐ[R] matrix n n R :=
{ map_mul' := linear_map.to_matrix'_comp,
map_add' := linear_map.to_matrix'.map_add,
commutes' := λ r, by { change (r • (linear_map.id : module.End R _)).to_matrix' = r • 1,
rw ←linear_map.to_matrix'_id, refl, },
..linear_map.to_matrix' }
/-- A linear equivalence of two modules induces an equivalence of algebras of their
endomorphisms. -/
def linear_equiv.alg_conj {R : Type v} [comm_ring R] {M₁ M₂ : Type*}
[add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] (e : M₁ ≃ₗ[R] M₂) :
module.End R M₁ ≃ₐ[R] module.End R M₂ :=
{ map_mul' := λ f g, by apply e.arrow_congr_comp,
map_add' := e.conj.map_add,
commutes' := λ r, by { change e.conj (r • linear_map.id) = r • linear_map.id,
rw [linear_equiv.map_smul, linear_equiv.conj_id], },
..e.conj }
/-- A basis of a module induces an equivalence of algebras from the endomorphisms of the module to
square matrices. -/
def alg_equiv_matrix {R : Type v} {M : Type w} {n : Type*} [fintype n]
[comm_ring R] [add_comm_group M] [module R M] [decidable_eq n] {b : n → M} (h : is_basis R b) :
module.End R M ≃ₐ[R] matrix n n R :=
h.equiv_fun.alg_conj.trans alg_equiv_matrix'
section
variables {R : Type v} [semiring R] {n : Type w} [fintype n]
@[simp] lemma matrix.dot_product_std_basis_eq_mul [decidable_eq n] (v : n → R) (c : R) (i : n) :
matrix.dot_product v (linear_map.std_basis R (λ _, R) i c) = v i * c :=
begin
rw [matrix.dot_product, finset.sum_eq_single i, linear_map.std_basis_same],
exact λ _ _ hb, by rw [linear_map.std_basis_ne _ _ _ _ hb, mul_zero],
exact λ hi, false.elim (hi $ finset.mem_univ _)
end
@[simp] lemma matrix.dot_product_std_basis_one [decidable_eq n] (v : n → R) (i : n) :
matrix.dot_product v (linear_map.std_basis R (λ _, R) i 1) = v i :=
by rw [matrix.dot_product_std_basis_eq_mul, mul_one]
lemma matrix.dot_product_eq
(v w : n → R) (h : ∀ u, matrix.dot_product v u = matrix.dot_product w u) : v = w :=
begin
funext x,
classical,
rw [← matrix.dot_product_std_basis_one v x, ← matrix.dot_product_std_basis_one w x, h],
end
lemma matrix.dot_product_eq_iff {v w : n → R} :
(∀ u, matrix.dot_product v u = matrix.dot_product w u) ↔ v = w :=
⟨λ h, matrix.dot_product_eq v w h, λ h _, h ▸ rfl⟩
lemma matrix.dot_product_eq_zero (v : n → R) (h : ∀ w, matrix.dot_product v w = 0) : v = 0 :=
matrix.dot_product_eq _ _ $ λ u, (h u).symm ▸ (zero_dot_product u).symm
lemma matrix.dot_product_eq_zero_iff {v : n → R} : (∀ w, matrix.dot_product v w = 0) ↔ v = 0 :=
⟨λ h, matrix.dot_product_eq_zero v h, λ h w, h.symm ▸ zero_dot_product w⟩
end
namespace matrix
variables {m n : Type*} [decidable_eq n] [fintype n] [decidable_eq m] [fintype m]
variables {R : Type v} [comm_ring R]
lemma det_to_block (M : matrix m m R) (p : m → Prop) [decidable_pred p] :
M.det = (matrix.from_blocks (to_block M p p) (to_block M p (λ j, ¬p j))
(to_block M (λ j, ¬p j) p) (to_block M (λ j, ¬p j) (λ j, ¬p j))).det :=
begin
rw ← matrix.det_reindex_self (equiv.sum_compl p).symm M,
rw [det_apply', det_apply'],
congr, ext σ, congr, ext,
generalize hy : σ x = y,
cases x; cases y;
simp only [matrix.reindex_apply, to_block_apply, equiv.symm_symm,
equiv.sum_compl_apply_inr, equiv.sum_compl_apply_inl,
from_blocks_apply₁₁, from_blocks_apply₁₂, from_blocks_apply₂₁, from_blocks_apply₂₂,
matrix.minor_apply],
end
lemma det_to_square_block (M : matrix m m R) {n : nat} (b : m → fin n) (k : fin n) :
(to_square_block M b k).det = (to_square_block_prop M (λ i, b i = k)).det :=
by simp
lemma det_to_square_block' (M : matrix m m R) (b : m → ℕ) (k : ℕ) :
(to_square_block' M b k).det = (to_square_block_prop M (λ i, b i = k)).det :=
by simp
lemma two_block_triangular_det (M : matrix m m R) (p : m → Prop) [decidable_pred p]
(h : ∀ i (h1 : ¬p i) j (h2 : p j), M i j = 0) :
M.det = (to_square_block_prop M p).det * (to_square_block_prop M (λ i, ¬p i)).det :=
begin
rw det_to_block M p,
convert upper_two_block_triangular_det (to_block M p p) (to_block M p (λ j, ¬p j))
(to_block M (λ j, ¬p j) (λ j, ¬p j)),
ext,
exact h ↑i i.2 ↑j j.2
end
lemma equiv_block_det (M : matrix m m R) {p q : m → Prop} [decidable_pred p] [decidable_pred q]
(e : ∀x, q x ↔ p x) : (to_square_block_prop M p).det = (to_square_block_prop M q).det :=
by convert matrix.det_reindex_self (equiv.subtype_equiv_right e) (to_square_block_prop M q)
lemma to_square_block_det'' (M : matrix m m R) {n : nat} (b : m → fin n) (k : fin n) :
(to_square_block M b k).det = (to_square_block' M (λ i, ↑(b i)) ↑k).det :=
begin
rw [to_square_block_def', to_square_block_def],
apply equiv_block_det,
intro x,
apply (fin.ext_iff _ _).symm
end
/-- Let `b` map rows and columns of a square matrix `M` to `n` blocks. Then
`block_triangular_matrix' M n b` says the matrix is block triangular. -/
def block_triangular_matrix' {o : Type*} [fintype o] (M : matrix o o R) {n : ℕ}
(b : o → fin n) : Prop :=
∀ i j, b j < b i → M i j = 0
lemma upper_two_block_triangular' {m n : Type*} [fintype m] [fintype n]
(A : matrix m m R) (B : matrix m n R) (D : matrix n n R) :
block_triangular_matrix' (from_blocks A B 0 D) (sum.elim (λ i, (0 : fin 2)) (λ j, 1)) :=
begin
intros k1 k2 hk12,
have h0 : ∀ (k : m ⊕ n), sum.elim (λ i, (0 : fin 2)) (λ j, 1) k = 0 → ∃ i, k = sum.inl i,
{ simp },
have h1 : ∀ (k : m ⊕ n), sum.elim (λ i, (0 : fin 2)) (λ j, 1) k = 1 → ∃ j, k = sum.inr j,
{ simp },
set mk1 := (sum.elim (λ i, (0 : fin 2)) (λ j, 1)) k1 with hmk1,
set mk2 := (sum.elim (λ i, (0 : fin 2)) (λ j, 1)) k2 with hmk2,
fin_cases mk1; fin_cases mk2; rw [h, h_1] at hk12,
{ exact absurd hk12 (nat.not_lt_zero 0) },
{ exact absurd hk12 (by norm_num) },
{ rw hmk1 at h,
obtain ⟨i, hi⟩ := h1 k1 h,
rw hmk2 at h_1,
obtain ⟨j, hj⟩ := h0 k2 h_1,
rw [hi, hj], simp },
{ exact absurd hk12 (irrefl 1) }
end
/-- Let `b` map rows and columns of a square matrix `M` to blocks indexed by `ℕ`s. Then
`block_triangular_matrix M n b` says the matrix is block triangular. -/
def block_triangular_matrix {o : Type*} [fintype o] (M : matrix o o R) (b : o → ℕ) : Prop :=
∀ i j, b j < b i → M i j = 0
lemma upper_two_block_triangular {m n : Type*} [fintype m] [fintype n]
(A : matrix m m R) (B : matrix m n R) (D : matrix n n R) :
block_triangular_matrix (from_blocks A B 0 D) (sum.elim (λ i, 0) (λ j, 1)) :=
begin
intros k1 k2 hk12,
have h01 : ∀ (k : m ⊕ n), sum.elim (λ i, 0) (λ j, 1) k = 0 ∨ sum.elim (λ i, 0) (λ j, 1) k = 1,
{ simp },
have h0 : ∀ (k : m ⊕ n), sum.elim (λ i, 0) (λ j, 1) k = 0 → ∃ i, k = sum.inl i, { simp },
have h1 : ∀ (k : m ⊕ n), sum.elim (λ i, 0) (λ j, 1) k = 1 → ∃ j, k = sum.inr j, { simp },
cases (h01 k1) with hk1 hk1; cases (h01 k2) with hk2 hk2; rw [hk1, hk2] at hk12,
{ exact absurd hk12 (nat.not_lt_zero 0) },
{ exact absurd hk12 (nat.not_lt_zero 1) },
{ obtain ⟨i, hi⟩ := h1 k1 hk1,
obtain ⟨j, hj⟩ := h0 k2 hk2,
rw [hi, hj], simp },
{ exact absurd hk12 (irrefl 1) }
end
lemma det_of_block_triangular_matrix (M : matrix m m R) (b : m → ℕ)
(h : block_triangular_matrix M b) :
∀ (n : ℕ) (hn : ∀ i, b i < n), M.det = ∏ k in finset.range n, (to_square_block' M b k).det :=
begin
intros n hn,
tactic.unfreeze_local_instances,
induction n with n hi generalizing m M b,
{ rw finset.prod_range_zero,
apply det_eq_one_of_card_eq_zero,
apply fintype.card_eq_zero_iff.mpr,
intro i,
exact nat.not_lt_zero (b i) (hn i) },
{ rw [finset.prod_range_succ_comm],
have h2 : (M.to_square_block_prop (λ (i : m), b i = n.succ)).det =
(M.to_square_block' b n.succ).det,
{ dunfold to_square_block', dunfold to_square_block_prop, refl },
rw two_block_triangular_det M (λ i, ¬(b i = n)),
{ rw mul_comm,
apply congr (congr_arg has_mul.mul _),
{ let m' := {a // ¬b a = n },
let b' := (λ (i : m'), b ↑i),
have h' :
block_triangular_matrix (M.to_square_block_prop (λ (i : m), ¬b i = n)) b',
{ intros i j, apply h ↑i ↑j },
have hni : ∀ (i : {a // ¬b a = n}), b' i < n,
{ exact λ i, (ne.le_iff_lt i.property).mp (nat.lt_succ_iff.mp (hn ↑i)) },
have h1 := hi (M.to_square_block_prop (λ (i : m), ¬b i = n)) b' h' hni,
rw ←fin.prod_univ_eq_prod_range at h1 ⊢,
convert h1,
ext k,
simp only [to_square_block_def', to_square_block_def],
let he : {a // b' a = ↑k} ≃ {a // b a = ↑k},
{ have hc : ∀ (i : m), (λ a, b a = ↑k) i → (λ a, ¬b a = n) i,
{ intros i hbi, rw hbi, exact ne_of_lt (fin.is_lt k) },
exact equiv.subtype_subtype_equiv_subtype hc },
exact matrix.det_reindex_self he (λ (i j : {a // b' a = ↑k}), M ↑i ↑j) },
{ rw det_to_square_block' M b n,
have hh : ∀ a, b a = n ↔ ¬(λ (i : m), ¬b i = n) a,
{ intro i, simp only [not_not] },
exact equiv_block_det M hh }},
{ intros i hi j hj,
apply (h i), simp only [not_not] at hi,
rw hi,
exact (ne.le_iff_lt hj).mp (nat.lt_succ_iff.mp (hn j)) }}
end
lemma det_of_block_triangular_matrix'' (M : matrix m m R) (b : m → ℕ)
(h : block_triangular_matrix M b) :
M.det = ∏ k in finset.image b finset.univ, (to_square_block' M b k).det :=
begin
let n : ℕ := (Sup (finset.image b finset.univ : set ℕ)).succ,
have hn : ∀ i, b i < n,
{ have hbi : ∀ i, b i ∈ finset.image b finset.univ, { simp },
intro i,
dsimp only [n],
apply nat.lt_succ_iff.mpr,
exact le_cSup (finset.bdd_above _) (hbi i) },
rw det_of_block_triangular_matrix M b h n hn,
refine (finset.prod_subset _ _).symm,
{ intros a ha, apply finset.mem_range.mpr,
obtain ⟨i, ⟨hi, hbi⟩⟩ := finset.mem_image.mp ha,
rw ←hbi,
exact hn i },
{ intros k hk hbk,
apply det_eq_one_of_card_eq_zero,
apply fintype.card_eq_zero_iff.mpr,
simp only [subtype.forall],
intros a hba, apply hbk,
apply finset.mem_image.mpr,
use a,
exact ⟨finset.mem_univ a, hba⟩ }
end
lemma det_of_block_triangular_matrix' (M : matrix m m R) {n : ℕ} (b : m → fin n)
(h : block_triangular_matrix' M b) :
M.det = ∏ (k : fin n), (to_square_block M b k).det :=
begin
let b2 : m → ℕ := λ i, ↑(b i),
simp_rw to_square_block_det'',
rw fin.prod_univ_eq_prod_range (λ (k : ℕ), (M.to_square_block' b2 k).det) n,
apply det_of_block_triangular_matrix,
{ intros i j hij, exact h i j (fin.coe_fin_lt.mp hij) },
{ intro i, exact fin.is_lt (b i) }
end
lemma det_of_upper_triangular {n : ℕ} (M : matrix (fin n) (fin n) R)
(h : ∀ (i j : fin n), j < i → M i j = 0) :
M.det = ∏ i : (fin n), M i i :=
begin
convert det_of_block_triangular_matrix' M id h,
ext i,
have h2 : ∀ (j : {a // id a = i}), j = ⟨i, rfl⟩ :=
λ (j : {a // id a = i}), subtype.ext j.property,
haveI : unique {a // id a = i} := ⟨⟨⟨i, rfl⟩⟩, h2⟩,
simp [h2 (default {a // id a = i})]
end
lemma det_of_lower_triangular {n : ℕ} (M : matrix (fin n) (fin n) R)
(h : ∀ (i j : fin n), i < j → M i j = 0) :
M.det = ∏ i : (fin n), M i i :=
begin
rw ← det_transpose,
exact det_of_upper_triangular _ (λ (i j : fin n) (hji : j < i), h j i hji)
end
end matrix
|
6fdaa8a412d22807f8660c698b8796cc7983522e | 737dc4b96c97368cb66b925eeea3ab633ec3d702 | /stage0/src/Lean/Elab/MutualDef.lean | d4f857d3847ccf04083c187e126d3f8ffd5c9140 | [
"Apache-2.0"
] | permissive | Bioye97/lean4 | 1ace34638efd9913dc5991443777b01a08983289 | bc3900cbb9adda83eed7e6affeaade7cfd07716d | refs/heads/master | 1,690,589,820,211 | 1,631,051,000,000 | 1,631,067,598,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 32,856 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Parser.Term
import Lean.Meta.Closure
import Lean.Meta.Check
import Lean.Elab.Command
import Lean.Elab.DefView
import Lean.Elab.PreDefinition
import Lean.Elab.DeclarationRange
namespace Lean.Elab
open Lean.Parser.Term
/- DefView after elaborating the header. -/
structure DefViewElabHeader where
ref : Syntax
modifiers : Modifiers
kind : DefKind
shortDeclName : Name
declName : Name
levelNames : List Name
numParams : Nat
type : Expr -- including the parameters
valueStx : Syntax
deriving Inhabited
namespace Term
open Meta
private def checkModifiers (m₁ m₂ : Modifiers) : TermElabM Unit := do
unless m₁.isUnsafe == m₂.isUnsafe do
throwError "cannot mix unsafe and safe definitions"
unless m₁.isNoncomputable == m₂.isNoncomputable do
throwError "cannot mix computable and non-computable definitions"
unless m₁.isPartial == m₂.isPartial do
throwError "cannot mix partial and non-partial definitions"
private def checkKinds (k₁ k₂ : DefKind) : TermElabM Unit := do
unless k₁.isExample == k₂.isExample do
throwError "cannot mix examples and definitions" -- Reason: we should discard examples
unless k₁.isTheorem == k₂.isTheorem do
throwError "cannot mix theorems and definitions" -- Reason: we will eventually elaborate theorems in `Task`s.
private def check (prevHeaders : Array DefViewElabHeader) (newHeader : DefViewElabHeader) : TermElabM Unit := do
if newHeader.kind.isTheorem && newHeader.modifiers.isUnsafe then
throwError "'unsafe' theorems are not allowed"
if newHeader.kind.isTheorem && newHeader.modifiers.isPartial then
throwError "'partial' theorems are not allowed, 'partial' is a code generation directive"
if newHeader.kind.isTheorem && newHeader.modifiers.isNoncomputable then
throwError "'theorem' subsumes 'noncomputable', code is not generated for theorems"
if newHeader.modifiers.isNoncomputable && newHeader.modifiers.isUnsafe then
throwError "'noncomputable unsafe' is not allowed"
if newHeader.modifiers.isNoncomputable && newHeader.modifiers.isPartial then
throwError "'noncomputable partial' is not allowed"
if newHeader.modifiers.isPartial && newHeader.modifiers.isUnsafe then
throwError "'unsafe' subsumes 'partial'"
if h : 0 < prevHeaders.size then
let firstHeader := prevHeaders.get ⟨0, h⟩
try
unless newHeader.levelNames == firstHeader.levelNames do
throwError "universe parameters mismatch"
checkModifiers newHeader.modifiers firstHeader.modifiers
checkKinds newHeader.kind firstHeader.kind
catch
| Exception.error ref msg => throw (Exception.error ref m!"invalid mutually recursive definitions, {msg}")
| ex => throw ex
else
pure ()
private def registerFailedToInferDefTypeInfo (type : Expr) (ref : Syntax) : TermElabM Unit :=
registerCustomErrorIfMVar type ref "failed to infer definition type"
/--
Return `some [b, c]` if the given `views` are representing a declaration of the form
```
constant a b c : Nat
``` -/
private def isMultiConstant? (views : Array DefView) : Option (List Name) :=
if views.size == 1 &&
views[0].kind == DefKind.opaque &&
views[0].binders.getArgs.size > 0 &&
views[0].binders.getArgs.all (·.getKind == ``Parser.Term.simpleBinder) then
some <| (views[0].binders.getArgs.toList.map (fun stx => stx[0].getArgs.toList.map (·.getId))).join
else
none
private def getPendindMVarErrorMessage (views : Array DefView) : String :=
match isMultiConstant? views with
| some ids =>
let idsStr := ", ".intercalate <| ids.map fun id => s!"`{id}`"
let paramsStr := ", ".intercalate <| ids.map fun id => s!"`({id} : _)`"
s!"\nrecall that you cannot declare multiple constants in a single declaration. The identifier(s) {idsStr} are being interpreted as parameters {paramsStr}"
| none =>
"\nwhen the resulting type of a declaration is explicitly provided, all holes (e.g., `_`) in the header are resolved before the declaration body is processed"
private def elabHeaders (views : Array DefView) : TermElabM (Array DefViewElabHeader) := do
let mut headers := #[]
for view in views do
let newHeader ← withRef view.ref do
let ⟨shortDeclName, declName, levelNames⟩ ← expandDeclId (← getCurrNamespace) (← getLevelNames) view.declId view.modifiers
addDeclarationRanges declName view.ref
applyAttributesAt declName view.modifiers.attrs AttributeApplicationTime.beforeElaboration
withDeclName declName <| withAutoBoundImplicit <| withLevelNames levelNames <|
elabBinders view.binders.getArgs fun xs => do
let refForElabFunType := view.value
let type ← match view.type? with
| some typeStx =>
let type ← elabType typeStx
registerFailedToInferDefTypeInfo type typeStx
pure type
| none =>
let hole := mkHole refForElabFunType
let type ← elabType hole
registerFailedToInferDefTypeInfo type refForElabFunType
pure type
Term.synthesizeSyntheticMVarsNoPostponing
let type ← mkForallFVars xs type
let type ← mkForallFVars (← read).autoBoundImplicits.toArray type
let type ← instantiateMVars type
let xs ← addAutoBoundImplicits xs
let levelNames ← getLevelNames
if view.type?.isSome then
let pendingMVarIds ← getMVars type
discard <| logUnassignedUsingErrorInfos pendingMVarIds <|
getPendindMVarErrorMessage views
let newHeader := {
ref := view.ref,
modifiers := view.modifiers,
kind := view.kind,
shortDeclName := shortDeclName,
declName := declName,
levelNames := levelNames,
numParams := xs.size,
type := type,
valueStx := view.value : DefViewElabHeader }
check headers newHeader
pure newHeader
headers := headers.push newHeader
pure headers
private partial def withFunLocalDecls {α} (headers : Array DefViewElabHeader) (k : Array Expr → TermElabM α) : TermElabM α :=
let rec loop (i : Nat) (fvars : Array Expr) := do
if h : i < headers.size then
let header := headers.get ⟨i, h⟩
if header.modifiers.isNonrec then
loop (i+1) fvars
else
withLocalDecl header.shortDeclName BinderInfo.auxDecl header.type fun fvar => loop (i+1) (fvars.push fvar)
else
k fvars
loop 0 #[]
private def expandWhereDeclsAsStructInst : Macro
| `(whereDecls|where $[$decls:letRecDecl$[;]?]*) => do
let letIdDecls ← decls.mapM fun stx => match stx with
| `(letRecDecl|$attrs:attributes $decl:letDecl) => Macro.throwErrorAt stx "attributes are 'where' elements are currently not supported here"
| `(letRecDecl|$decl:letPatDecl) => Macro.throwErrorAt stx "patterns are not allowed here"
| `(letRecDecl|$decl:letEqnsDecl) => expandLetEqnsDecl decl
| `(letRecDecl|$decl:letIdDecl) => pure decl
| _ => Macro.throwUnsupported
let structInstFields ← letIdDecls.mapM fun
| stx@`(letIdDecl|$id:ident $[$binders]* $[: $ty?]? := $val) => withRef stx do
let mut val := val
if let some ty := ty? then
val ← `(($val : $ty))
val ← if binders.size > 0 then `(fun $[$binders]* => $val:term) else val
`(structInstField|$id:ident := $val)
| _ => Macro.throwUnsupported
`({ $[$structInstFields,]* })
| _ => Macro.throwUnsupported
/-
Recall that
```
def declValSimple := leading_parser " :=\n" >> termParser >> optional Term.whereDecls
def declValEqns := leading_parser Term.matchAltsWhereDecls
def declVal := declValSimple <|> declValEqns <|> Term.whereDecls
```
-/
private def declValToTerm (declVal : Syntax) : MacroM Syntax := withRef declVal do
if declVal.isOfKind `Lean.Parser.Command.declValSimple then
expandWhereDeclsOpt declVal[2] declVal[1]
else if declVal.isOfKind `Lean.Parser.Command.declValEqns then
expandMatchAltsWhereDecls declVal[0]
else if declVal.isOfKind `Lean.Parser.Term.whereDecls then
expandWhereDeclsAsStructInst declVal
else if declVal.isMissing then
Macro.throwErrorAt declVal "declaration body is missing"
else
Macro.throwErrorAt declVal "unexpected declaration body"
private def elabFunValues (headers : Array DefViewElabHeader) : TermElabM (Array Expr) :=
headers.mapM fun header => withDeclName header.declName $ withLevelNames header.levelNames do
let valStx ← liftMacroM $ declValToTerm header.valueStx
forallBoundedTelescope header.type header.numParams fun xs type => do
let val ← elabTermEnsuringType valStx type
mkLambdaFVars xs val
private def collectUsed (headers : Array DefViewElabHeader) (values : Array Expr) (toLift : List LetRecToLift)
: StateRefT CollectFVars.State MetaM Unit := do
headers.forM fun header => collectUsedFVars header.type
values.forM collectUsedFVars
toLift.forM fun letRecToLift => do
collectUsedFVars letRecToLift.type
collectUsedFVars letRecToLift.val
private def removeUnusedVars (vars : Array Expr) (headers : Array DefViewElabHeader) (values : Array Expr) (toLift : List LetRecToLift)
: TermElabM (LocalContext × LocalInstances × Array Expr) := do
let (_, used) ← (collectUsed headers values toLift).run {}
removeUnused vars used
private def withUsed {α} (vars : Array Expr) (headers : Array DefViewElabHeader) (values : Array Expr) (toLift : List LetRecToLift)
(k : Array Expr → TermElabM α) : TermElabM α := do
let (lctx, localInsts, vars) ← removeUnusedVars vars headers values toLift
withLCtx lctx localInsts $ k vars
private def isExample (views : Array DefView) : Bool :=
views.any (·.kind.isExample)
private def isTheorem (views : Array DefView) : Bool :=
views.any (·.kind.isTheorem)
private def instantiateMVarsAtHeader (header : DefViewElabHeader) : TermElabM DefViewElabHeader := do
let type ← instantiateMVars header.type
pure { header with type := type }
private def instantiateMVarsAtLetRecToLift (toLift : LetRecToLift) : TermElabM LetRecToLift := do
let type ← instantiateMVars toLift.type
let val ← instantiateMVars toLift.val
pure { toLift with type := type, val := val }
private def typeHasRecFun (type : Expr) (funFVars : Array Expr) (letRecsToLift : List LetRecToLift) : Option FVarId :=
let occ? := type.find? fun e => match e with
| Expr.fvar fvarId _ => funFVars.contains e || letRecsToLift.any fun toLift => toLift.fvarId == fvarId
| _ => false
match occ? with
| some (Expr.fvar fvarId _) => some fvarId
| _ => none
private def getFunName (fvarId : FVarId) (letRecsToLift : List LetRecToLift) : TermElabM Name := do
match (← findLocalDecl? fvarId) with
| some decl => pure decl.userName
| none =>
/- Recall that the FVarId of nested let-recs are not in the current local context. -/
match letRecsToLift.findSome? fun toLift => if toLift.fvarId == fvarId then some toLift.shortDeclName else none with
| none => throwError "unknown function"
| some n => pure n
/-
Ensures that the of let-rec definition types do not contain functions being defined.
In principle, this test can be improved. We could perform it after we separate the set of functions is strongly connected components.
However, this extra complication doesn't seem worth it.
-/
private def checkLetRecsToLiftTypes (funVars : Array Expr) (letRecsToLift : List LetRecToLift) : TermElabM Unit :=
letRecsToLift.forM fun toLift =>
match typeHasRecFun toLift.type funVars letRecsToLift with
| none => pure ()
| some fvarId => do
let fnName ← getFunName fvarId letRecsToLift
throwErrorAt toLift.ref "invalid type in 'let rec', it uses '{fnName}' which is being defined simultaneously"
namespace MutualClosure
/- A mapping from FVarId to Set of FVarIds. -/
abbrev UsedFVarsMap := NameMap NameSet
/-
Create the `UsedFVarsMap` mapping that takes the variable id for the mutually recursive functions being defined to the set of
free variables in its definition.
For `mainFVars`, this is just the set of section variables `sectionVars` used.
For nested let-rec functions, we collect their free variables.
Recall that a `let rec` expressions are encoded as follows in the elaborator.
```lean
let rec
f : A := t,
g : B := s;
body
```
is encoded as
```lean
let f : A := ?m₁;
let g : B := ?m₂;
body
```
where `?m₁` and `?m₂` are synthetic opaque metavariables. That are assigned by this module.
We may have nested `let rec`s.
```lean
let rec f : A :=
let rec g : B := t;
s;
body
```
is encoded as
```lean
let f : A := ?m₁;
body
```
and the body of `f` is stored the field `val` of a `LetRecToLift`. For the example above,
we would have a `LetRecToLift` containing:
```
{
mvarId := m₁,
val := `(let g : B := ?m₂; body)
...
}
```
Note that `g` is not a free variable at `(let g : B := ?m₂; body)`. We recover the fact that
`f` depends on `g` because it contains `m₂`
-/
private def mkInitialUsedFVarsMap (mctx : MetavarContext) (sectionVars : Array Expr) (mainFVarIds : Array FVarId) (letRecsToLift : List LetRecToLift)
: UsedFVarsMap := do
let mut sectionVarSet := {}
for var in sectionVars do
sectionVarSet := sectionVarSet.insert var.fvarId!
let mut usedFVarMap := {}
for mainFVarId in mainFVarIds do
usedFVarMap := usedFVarMap.insert mainFVarId sectionVarSet
for toLift in letRecsToLift do
let state := Lean.collectFVars {} toLift.val
let state := Lean.collectFVars state toLift.type
let mut set := state.fvarSet
/- toLift.val may contain metavariables that are placeholders for nested let-recs. We should collect the fvarId
for the associated let-rec because we need this information to compute the fixpoint later. -/
let mvarIds := (toLift.val.collectMVars {}).result
for mvarId in mvarIds do
match letRecsToLift.findSome? fun (toLift : LetRecToLift) => if toLift.mvarId == mctx.getDelayedRoot mvarId then some toLift.fvarId else none with
| some fvarId => set := set.insert fvarId
| none => pure ()
usedFVarMap := usedFVarMap.insert toLift.fvarId set
pure usedFVarMap
/-
The let-recs may invoke each other. Example:
```
let rec
f (x : Nat) := g x + y
g : Nat → Nat
| 0 => 1
| x+1 => f x + z
```
`y` is free variable in `f`, and `z` is a free variable in `g`.
To close `f` and `g`, `y` and `z` must be in the closure of both.
That is, we need to generate the top-level definitions.
```
def f (y z x : Nat) := g y z x + y
def g (y z : Nat) : Nat → Nat
| 0 => 1
| x+1 => f y z x + z
```
-/
namespace FixPoint
structure State where
usedFVarsMap : UsedFVarsMap := {}
modified : Bool := false
abbrev M := ReaderT (List FVarId) $ StateM State
private def isModified : M Bool := do pure (← get).modified
private def resetModified : M Unit := modify fun s => { s with modified := false }
private def markModified : M Unit := modify fun s => { s with modified := true }
private def getUsedFVarsMap : M UsedFVarsMap := do pure (← get).usedFVarsMap
private def modifyUsedFVars (f : UsedFVarsMap → UsedFVarsMap) : M Unit := modify fun s => { s with usedFVarsMap := f s.usedFVarsMap }
-- merge s₂ into s₁
private def merge (s₁ s₂ : NameSet) : M NameSet :=
s₂.foldM (init := s₁) fun s₁ k => do
if s₁.contains k then
pure s₁
else
markModified
pure $ s₁.insert k
private def updateUsedVarsOf (fvarId : FVarId) : M Unit := do
let usedFVarsMap ← getUsedFVarsMap
match usedFVarsMap.find? fvarId with
| none => pure ()
| some fvarIds =>
let fvarIdsNew ← fvarIds.foldM (init := fvarIds) fun fvarIdsNew fvarId' =>
if fvarId == fvarId' then
pure fvarIdsNew
else
match usedFVarsMap.find? fvarId' with
| none => pure fvarIdsNew
/- We are being sloppy here `otherFVarIds` may contain free variables that are
not in the context of the let-rec associated with fvarId.
We filter these out-of-context free variables later. -/
| some otherFVarIds => merge fvarIdsNew otherFVarIds
modifyUsedFVars fun usedFVars => usedFVars.insert fvarId fvarIdsNew
private partial def fixpoint : Unit → M Unit
| _ => do
resetModified
let letRecFVarIds ← read
letRecFVarIds.forM updateUsedVarsOf
if (← isModified) then
fixpoint ()
def run (letRecFVarIds : List FVarId) (usedFVarsMap : UsedFVarsMap) : UsedFVarsMap :=
let (_, s) := ((fixpoint ()).run letRecFVarIds).run { usedFVarsMap := usedFVarsMap }
s.usedFVarsMap
end FixPoint
abbrev FreeVarMap := NameMap (Array FVarId)
private def mkFreeVarMap
(mctx : MetavarContext) (sectionVars : Array Expr) (mainFVarIds : Array FVarId)
(recFVarIds : Array FVarId) (letRecsToLift : List LetRecToLift) : FreeVarMap := do
let usedFVarsMap := mkInitialUsedFVarsMap mctx sectionVars mainFVarIds letRecsToLift
let letRecFVarIds := letRecsToLift.map fun toLift => toLift.fvarId
let usedFVarsMap := FixPoint.run letRecFVarIds usedFVarsMap
let mut freeVarMap := {}
for toLift in letRecsToLift do
let lctx := toLift.lctx
let fvarIdsSet := (usedFVarsMap.find? toLift.fvarId).get!
let fvarIds := fvarIdsSet.fold (init := #[]) fun fvarIds fvarId =>
if lctx.contains fvarId && !recFVarIds.contains fvarId then
fvarIds.push fvarId
else
fvarIds
freeVarMap := freeVarMap.insert toLift.fvarId fvarIds
pure freeVarMap
structure ClosureState where
newLocalDecls : Array LocalDecl := #[]
localDecls : Array LocalDecl := #[]
newLetDecls : Array LocalDecl := #[]
exprArgs : Array Expr := #[]
private def pickMaxFVar? (lctx : LocalContext) (fvarIds : Array FVarId) : Option FVarId :=
fvarIds.getMax? fun fvarId₁ fvarId₂ => (lctx.get! fvarId₁).index < (lctx.get! fvarId₂).index
private def preprocess (e : Expr) : TermElabM Expr := do
let e ← instantiateMVars e
-- which let-decls are dependent. We say a let-decl is dependent if its lambda abstraction is type incorrect.
Meta.check e
pure e
/- Push free variables in `s` to `toProcess` if they are not already there. -/
private def pushNewVars (toProcess : Array FVarId) (s : CollectFVars.State) : Array FVarId :=
s.fvarSet.fold (init := toProcess) fun toProcess fvarId =>
if toProcess.contains fvarId then toProcess else toProcess.push fvarId
private def pushLocalDecl (toProcess : Array FVarId) (fvarId : FVarId) (userName : Name) (type : Expr) (bi := BinderInfo.default)
: StateRefT ClosureState TermElabM (Array FVarId) := do
let type ← preprocess type
modify fun s => { s with
newLocalDecls := s.newLocalDecls.push $ LocalDecl.cdecl arbitrary fvarId userName type bi,
exprArgs := s.exprArgs.push (mkFVar fvarId)
}
pure $ pushNewVars toProcess (collectFVars {} type)
private partial def mkClosureForAux (toProcess : Array FVarId) : StateRefT ClosureState TermElabM Unit := do
let lctx ← getLCtx
match pickMaxFVar? lctx toProcess with
| none => pure ()
| some fvarId =>
trace[Elab.definition.mkClosure] "toProcess: {toProcess.map mkFVar}, maxVar: {mkFVar fvarId}"
let toProcess := toProcess.erase fvarId
let localDecl ← getLocalDecl fvarId
match localDecl with
| LocalDecl.cdecl _ _ userName type bi =>
let toProcess ← pushLocalDecl toProcess fvarId userName type bi
mkClosureForAux toProcess
| LocalDecl.ldecl _ _ userName type val _ =>
let zetaFVarIds ← getZetaFVarIds
if !zetaFVarIds.contains fvarId then
/- Non-dependent let-decl. See comment at src/Lean/Meta/Closure.lean -/
let toProcess ← pushLocalDecl toProcess fvarId userName type
mkClosureForAux toProcess
else
/- Dependent let-decl. -/
let type ← preprocess type
let val ← preprocess val
modify fun s => { s with
newLetDecls := s.newLetDecls.push $ LocalDecl.ldecl arbitrary fvarId userName type val false,
/- We don't want to interleave let and lambda declarations in our closure. So, we expand any occurrences of fvarId
at `newLocalDecls` and `localDecls` -/
newLocalDecls := s.newLocalDecls.map (replaceFVarIdAtLocalDecl fvarId val),
localDecls := s.localDecls.map (replaceFVarIdAtLocalDecl fvarId val)
}
mkClosureForAux (pushNewVars toProcess (collectFVars (collectFVars {} type) val))
private partial def mkClosureFor (freeVars : Array FVarId) (localDecls : Array LocalDecl) : TermElabM ClosureState := do
let (_, s) ← (mkClosureForAux freeVars).run { localDecls := localDecls }
pure { s with
newLocalDecls := s.newLocalDecls.reverse,
newLetDecls := s.newLetDecls.reverse,
exprArgs := s.exprArgs.reverse
}
structure LetRecClosure where
ref : Syntax
localDecls : Array LocalDecl
closed : Expr -- expression used to replace occurrences of the let-rec FVarId
toLift : LetRecToLift
private def mkLetRecClosureFor (toLift : LetRecToLift) (freeVars : Array FVarId) : TermElabM LetRecClosure := do
let lctx := toLift.lctx
withLCtx lctx toLift.localInstances do
lambdaTelescope toLift.val fun xs val => do
let type ← instantiateForall toLift.type xs
let lctx ← getLCtx
let s ← mkClosureFor freeVars $ xs.map fun x => lctx.get! x.fvarId!
let type := Closure.mkForall s.localDecls $ Closure.mkForall s.newLetDecls type
let val := Closure.mkLambda s.localDecls $ Closure.mkLambda s.newLetDecls val
let c := mkAppN (Lean.mkConst toLift.declName) s.exprArgs
assignExprMVar toLift.mvarId c
return {
ref := toLift.ref
localDecls := s.newLocalDecls
closed := c
toLift := { toLift with val := val, type := type }
}
private def mkLetRecClosures (letRecsToLift : List LetRecToLift) (freeVarMap : FreeVarMap) : TermElabM (List LetRecClosure) :=
letRecsToLift.mapM fun toLift => mkLetRecClosureFor toLift (freeVarMap.find? toLift.fvarId).get!
/- Mapping from FVarId of mutually recursive functions being defined to "closure" expression. -/
abbrev Replacement := NameMap Expr
def insertReplacementForMainFns (r : Replacement) (sectionVars : Array Expr) (mainHeaders : Array DefViewElabHeader) (mainFVars : Array Expr) : Replacement :=
mainFVars.size.fold (init := r) fun i r =>
r.insert mainFVars[i].fvarId! (mkAppN (Lean.mkConst mainHeaders[i].declName) sectionVars)
def insertReplacementForLetRecs (r : Replacement) (letRecClosures : List LetRecClosure) : Replacement :=
letRecClosures.foldl (init := r) fun r c =>
r.insert c.toLift.fvarId c.closed
def Replacement.apply (r : Replacement) (e : Expr) : Expr :=
e.replace fun e => match e with
| Expr.fvar fvarId _ => match r.find? fvarId with
| some c => some c
| _ => none
| _ => none
def pushMain (preDefs : Array PreDefinition) (sectionVars : Array Expr) (mainHeaders : Array DefViewElabHeader) (mainVals : Array Expr)
: TermElabM (Array PreDefinition) :=
mainHeaders.size.foldM (init := preDefs) fun i preDefs => do
let header := mainHeaders[i]
let val ← mkLambdaFVars sectionVars mainVals[i]
let type ← mkForallFVars sectionVars header.type
return preDefs.push {
ref := getDeclarationSelectionRef header.ref
kind := header.kind
declName := header.declName
levelParams := [], -- we set it later
modifiers := header.modifiers
type := type
value := val
}
def pushLetRecs (preDefs : Array PreDefinition) (letRecClosures : List LetRecClosure) (kind : DefKind) (modifiers : Modifiers) : Array PreDefinition :=
letRecClosures.foldl (init := preDefs) fun preDefs c =>
let type := Closure.mkForall c.localDecls c.toLift.type
let val := Closure.mkLambda c.localDecls c.toLift.val
preDefs.push {
ref := c.ref
kind := kind
declName := c.toLift.declName
levelParams := [] -- we set it later
modifiers := { modifiers with attrs := c.toLift.attrs }
type := type
value := val
}
def getKindForLetRecs (mainHeaders : Array DefViewElabHeader) : DefKind :=
if mainHeaders.any fun h => h.kind.isTheorem then DefKind.«theorem»
else DefKind.«def»
def getModifiersForLetRecs (mainHeaders : Array DefViewElabHeader) : Modifiers := {
isNoncomputable := mainHeaders.any fun h => h.modifiers.isNoncomputable
recKind := if mainHeaders.any fun h => h.modifiers.isPartial then RecKind.partial else RecKind.default
isUnsafe := mainHeaders.any fun h => h.modifiers.isUnsafe
}
/-
- `sectionVars`: The section variables used in the `mutual` block.
- `mainHeaders`: The elaborated header of the top-level definitions being defined by the mutual block.
- `mainFVars`: The auxiliary variables used to represent the top-level definitions being defined by the mutual block.
- `mainVals`: The elaborated value for the top-level definitions
- `letRecsToLift`: The let-rec's definitions that need to be lifted
-/
def main (sectionVars : Array Expr) (mainHeaders : Array DefViewElabHeader) (mainFVars : Array Expr) (mainVals : Array Expr) (letRecsToLift : List LetRecToLift)
: TermElabM (Array PreDefinition) := do
-- Store in recFVarIds the fvarId of every function being defined by the mutual block.
let mainFVarIds := mainFVars.map Expr.fvarId!
let recFVarIds := (letRecsToLift.toArray.map fun toLift => toLift.fvarId) ++ mainFVarIds
-- Compute the set of free variables (excluding `recFVarIds`) for each let-rec.
let mctx ← getMCtx
let freeVarMap := mkFreeVarMap mctx sectionVars mainFVarIds recFVarIds letRecsToLift
resetZetaFVarIds
withTrackingZeta do
-- By checking `toLift.type` and `toLift.val` we populate `zetaFVarIds`. See comments at `src/Lean/Meta/Closure.lean`.
letRecsToLift.forM fun toLift => withLCtx toLift.lctx toLift.localInstances do Meta.check toLift.type; Meta.check toLift.val
let letRecClosures ← mkLetRecClosures letRecsToLift freeVarMap
-- mkLetRecClosures assign metavariables that were placeholders for the lifted declarations.
let mainVals ← mainVals.mapM (instantiateMVars ·)
let mainHeaders ← mainHeaders.mapM instantiateMVarsAtHeader
let letRecClosures ← letRecClosures.mapM fun closure => do pure { closure with toLift := (← instantiateMVarsAtLetRecToLift closure.toLift) }
-- Replace fvarIds for functions being defined with closed terms
let r := insertReplacementForMainFns {} sectionVars mainHeaders mainFVars
let r := insertReplacementForLetRecs r letRecClosures
let mainVals := mainVals.map r.apply
let mainHeaders := mainHeaders.map fun h => { h with type := r.apply h.type }
let letRecClosures := letRecClosures.map fun c => { c with toLift := { c.toLift with type := r.apply c.toLift.type, val := r.apply c.toLift.val } }
let letRecKind := getKindForLetRecs mainHeaders
let letRecMods := getModifiersForLetRecs mainHeaders
pushMain (pushLetRecs #[] letRecClosures letRecKind letRecMods) sectionVars mainHeaders mainVals
end MutualClosure
private def getAllUserLevelNames (headers : Array DefViewElabHeader) : List Name :=
if h : 0 < headers.size then
-- Recall that all top-level functions must have the same levels. See `check` method above
(headers.get ⟨0, h⟩).levelNames
else
[]
/-- Eagerly convert universe metavariables occurring in theorem headers to universe parameters. -/
private def levelMVarToParamHeaders (views : Array DefView) (headers : Array DefViewElabHeader) : TermElabM (Array DefViewElabHeader) := do
let rec process : StateRefT Nat TermElabM (Array DefViewElabHeader) := do
let mut newHeaders := #[]
for view in views, header in headers do
if view.kind.isTheorem then
newHeaders := newHeaders.push { header with type := (← levelMVarToParam' header.type) }
else
newHeaders := newHeaders.push header
return newHeaders
let newHeaders ← process.run' 1
newHeaders.mapM fun header => return { header with type := (← instantiateMVars header.type) }
/-- Result for `mkInst?` -/
structure MkInstResult where
instVal : Expr
instType : Expr
outParams : Array Expr := #[]
/--
Construct an instance for `className out₁ ... outₙ type`.
The method support classes with a prefix of `outParam`s (e.g. `MonadReader`). -/
private partial def mkInst? (className : Name) (type : Expr) : MetaM (Option MkInstResult) := do
let rec go? (instType instTypeType : Expr) (outParams : Array Expr) : MetaM (Option MkInstResult) := do
let instTypeType ← whnfD instTypeType
unless instTypeType.isForall do
return none
let d := instTypeType.bindingDomain!
if isOutParam d then
let mvar ← mkFreshExprMVar d
go? (mkApp instType mvar) (instTypeType.bindingBody!.instantiate1 mvar) (outParams.push mvar)
else
unless (← isDefEqGuarded (← inferType type) d) do
return none
let instType ← instantiateMVars (mkApp instType type)
let instVal ← synthInstance instType
return some { instVal, instType, outParams }
let instType ← mkConstWithFreshMVarLevels className
go? instType (← inferType instType) #[]
def processDefDeriving (className : Name) (declName : Name) : TermElabM Bool := do
try
let ConstantInfo.defnInfo info ← getConstInfo declName | return false
let some result ← mkInst? className info.value | return false
let instTypeNew := mkApp result.instType.appFn! (Lean.mkConst declName (info.levelParams.map mkLevelParam))
Meta.check instTypeNew
let instName ← liftMacroM <| mkUnusedBaseName (declName.appendBefore "inst" |>.appendAfter className.getString!)
addAndCompile <| Declaration.defnDecl {
name := instName
levelParams := info.levelParams
type := (← instantiateMVars instTypeNew)
value := (← instantiateMVars result.instVal)
hints := info.hints
safety := info.safety
}
addInstance instName AttributeKind.global (eval_prio default)
return true
catch ex =>
return false
def elabMutualDef (vars : Array Expr) (views : Array DefView) : TermElabM Unit :=
if isExample views then
withoutModifyingEnv go
else
go
where
go := do
let scopeLevelNames ← getLevelNames
let headers ← elabHeaders views
let headers ← levelMVarToParamHeaders views headers
let allUserLevelNames := getAllUserLevelNames headers
withFunLocalDecls headers fun funFVars => do
let values ← elabFunValues headers
Term.synthesizeSyntheticMVarsNoPostponing
let values ← values.mapM (instantiateMVars ·)
let headers ← headers.mapM instantiateMVarsAtHeader
let letRecsToLift ← getLetRecsToLift
let letRecsToLift ← letRecsToLift.mapM instantiateMVarsAtLetRecToLift
checkLetRecsToLiftTypes funFVars letRecsToLift
withUsed vars headers values letRecsToLift fun vars => do
let preDefs ← MutualClosure.main vars headers funFVars values letRecsToLift
let preDefs ← levelMVarToParamPreDecls preDefs
let preDefs ← instantiateMVarsAtPreDecls preDefs
let preDefs ← fixLevelParams preDefs scopeLevelNames allUserLevelNames
addPreDefinitions preDefs
processDeriving headers
processDeriving (headers : Array DefViewElabHeader) := do
for header in headers, view in views do
if let some classNamesStx := view.deriving? then
for classNameStx in classNamesStx do
let className ← resolveGlobalConstNoOverload classNameStx
withRef classNameStx do
unless (← processDefDeriving className header.declName) do
throwError "failed to synthesize instance '{className}' for '{header.declName}'"
end Term
namespace Command
def elabMutualDef (ds : Array Syntax) : CommandElabM Unit := do
let views ← ds.mapM fun d => do
let modifiers ← elabModifiers d[0]
if ds.size > 1 && modifiers.isNonrec then
throwErrorAt d "invalid use of 'nonrec' modifier in 'mutual' block"
mkDefView modifiers d[1]
runTermElabM none fun vars => Term.elabMutualDef vars views
end Command
end Lean.Elab
|
f89430e635d7a8f996a4616d64b6c7e66dd1452a | 5e3548e65f2c037cb94cd5524c90c623fbd6d46a | /src_icannos_totilas/reduction/cpge_reduction_007_c.lean | d27e176313e8bc099899d1a77ce2a8999fccbba5 | [] | no_license | ahayat16/lean_exos | d4f08c30adb601a06511a71b5ffb4d22d12ef77f | 682f2552d5b04a8c8eb9e4ab15f875a91b03845c | refs/heads/main | 1,693,101,073,585 | 1,636,479,336,000 | 1,636,479,336,000 | 415,000,441 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 454 | lean | import algebra.module.basic
import algebra.module.linear_map
import linear_algebra.basic
import linear_algebra.prod
import linear_algebra.projection
import order.bounded_lattice
theorem cpge_reduction_7_c (R : Type*) (M : Type*)
[semiring R] [add_comm_group M] [module R M]
(u : linear_map R M M):
u^3 + u = 0 ->
∃ (v : linear_equiv R ((⊤ : ).map u) ((⊤ : submodule R M).map u)), ∀ x ∈ ((⊤ : submodule R M).map u), v x = -x := sorry |
36d35d5130662b3840a9fd29209d290b2f2c05b8 | c1a29ca460720df88ab68dc42d9a1a02e029d505 | /examples/logic/unnamed_444.lean | ee3c870a983c2f4ff125731e118e665144272ed0 | [] | no_license | agusakov/mathematics_in_lean | acb5b3d659e4522ae4b4836ea550527f03f6546c | 2539562e4d91c858c73dbecb5b282ce1a7d38b6d | refs/heads/master | 1,665,963,365,241 | 1,592,080,022,000 | 1,592,080,022,000 | 272,078,062 | 0 | 0 | null | 1,592,078,772,000 | 1,592,078,772,000 | null | UTF-8 | Lean | false | false | 240 | lean | variables {α : Type*} (r s t : set α)
example : s ⊆ s :=
by { intros x xs, exact xs }
example : s ⊆ s := λ x xs, xs
example : r ⊆ s → s ⊆ t → r ⊆ t :=
begin
sorry
end
example : r ⊆ s → s ⊆ t → r ⊆ t :=
sorry |
b62c6f8b5e3983d1506bbc4430c6f454eecbf424 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/topology/metric_space/gluing.lean | 8d6ae28faf350dc0adbd617f9328a2d7aae7696b | [
"Apache-2.0"
] | permissive | Lix0120/mathlib | 0020745240315ed0e517cbf32e738d8f9811dd80 | e14c37827456fc6707f31b4d1d16f1f3a3205e91 | refs/heads/master | 1,673,102,855,024 | 1,604,151,044,000 | 1,604,151,044,000 | 308,930,245 | 0 | 0 | Apache-2.0 | 1,604,164,710,000 | 1,604,163,547,000 | null | UTF-8 | Lean | false | false | 23,855 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Gluing metric spaces
Authors: Sébastien Gouëzel
-/
import topology.metric_space.isometry
import topology.metric_space.premetric_space
/-!
# Metric space gluing
Gluing two metric spaces along a common subset. Formally, we are given
```
Φ
γ ---> α
|
|Ψ
v
β
```
where `hΦ : isometry Φ` and `hΨ : isometry Ψ`.
We want to complete the square by a space `glue_space hΦ hΨ` and two isometries
`to_glue_l hΦ hΨ` and `to_glue_r hΦ hΨ` that make the square commute.
We start by defining a predistance on the disjoint union `α ⊕ β`, for which
points `Φ p` and `Ψ p` are at distance 0. The (quotient) metric space associated
to this predistance is the desired space.
This is an instance of a more general construction, where `Φ` and `Ψ` do not have to be isometries,
but the distances in the image almost coincide, up to `2ε` say. Then one can almost glue the two
spaces so that the images of a point under `Φ` and `Ψ` are ε-close. If `ε > 0`, this yields a
metric space structure on `α ⊕ β`, without the need to take a quotient. In particular, when
`α` and `β` are inhabited, this gives a natural metric space structure on `α ⊕ β`, where the basepoints
are at distance 1, say, and the distances between other points are obtained by going through the
two basepoints.
We also define the inductive limit of metric spaces. Given
```
f 0 f 1 f 2 f 3
X 0 -----> X 1 -----> X 2 -----> X 3 -----> ...
```
where the `X n` are metric spaces and `f n` isometric embeddings, we define the inductive
limit of the `X n`, also known as the increasing union of the `X n` in this context, if we
identify `X n` and `X (n+1)` through `f n`. This is a metric space in which all `X n` embed
isometrically and in a way compatible with `f n`.
-/
noncomputable theory
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
open function set premetric
open_locale uniformity
namespace metric
section approx_gluing
variables [metric_space α] [metric_space β]
{Φ : γ → α} {Ψ : γ → β} {ε : ℝ}
open sum (inl inr)
/-- Define a predistance on α ⊕ β, for which Φ p and Ψ p are at distance ε -/
def glue_dist (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) : α ⊕ β → α ⊕ β → ℝ
| (inl x) (inl y) := dist x y
| (inr x) (inr y) := dist x y
| (inl x) (inr y) := infi (λp, dist x (Φ p) + dist y (Ψ p)) + ε
| (inr x) (inl y) := infi (λp, dist y (Φ p) + dist x (Ψ p)) + ε
private lemma glue_dist_self (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) : ∀x, glue_dist Φ Ψ ε x x = 0
| (inl x) := dist_self _
| (inr x) := dist_self _
lemma glue_dist_glued_points [nonempty γ] (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) (p : γ) :
glue_dist Φ Ψ ε (inl (Φ p)) (inr (Ψ p)) = ε :=
begin
have : infi (λq, dist (Φ p) (Φ q) + dist (Ψ p) (Ψ q)) = 0,
{ have A : ∀q, 0 ≤ dist (Φ p) (Φ q) + dist (Ψ p) (Ψ q) :=
λq, by rw ← add_zero (0 : ℝ); exact add_le_add dist_nonneg dist_nonneg,
refine le_antisymm _ (le_cinfi A),
have : 0 = dist (Φ p) (Φ p) + dist (Ψ p) (Ψ p), by simp,
rw this,
exact cinfi_le ⟨0, forall_range_iff.2 A⟩ p },
rw [glue_dist, this, zero_add]
end
private lemma glue_dist_comm (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) :
∀x y, glue_dist Φ Ψ ε x y = glue_dist Φ Ψ ε y x
| (inl x) (inl y) := dist_comm _ _
| (inr x) (inr y) := dist_comm _ _
| (inl x) (inr y) := rfl
| (inr x) (inl y) := rfl
variable [nonempty γ]
private lemma glue_dist_triangle (Φ : γ → α) (Ψ : γ → β) (ε : ℝ)
(H : ∀p q, abs (dist (Φ p) (Φ q) - dist (Ψ p) (Ψ q)) ≤ 2 * ε) :
∀x y z, glue_dist Φ Ψ ε x z ≤ glue_dist Φ Ψ ε x y + glue_dist Φ Ψ ε y z
| (inl x) (inl y) (inl z) := dist_triangle _ _ _
| (inr x) (inr y) (inr z) := dist_triangle _ _ _
| (inr x) (inl y) (inl z) := begin
have B : ∀a b, bdd_below (range (λ (p : γ), dist a (Φ p) + dist b (Ψ p))) :=
λa b, ⟨0, forall_range_iff.2 (λp, add_nonneg dist_nonneg dist_nonneg)⟩,
unfold glue_dist,
have : infi (λp, dist z (Φ p) + dist x (Ψ p)) ≤ infi (λp, dist y (Φ p) + dist x (Ψ p)) + dist y z,
{ have : infi (λp, dist y (Φ p) + dist x (Ψ p)) + dist y z =
infi ((λt, t + dist y z) ∘ (λp, dist y (Φ p) + dist x (Ψ p))),
{ refine map_cinfi_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) _
(B _ _),
intros x y hx, simpa },
rw [this, comp],
refine cinfi_le_cinfi (B _ _) (λp, _),
calc
dist z (Φ p) + dist x (Ψ p) ≤ (dist y z + dist y (Φ p)) + dist x (Ψ p) :
add_le_add (dist_triangle_left _ _ _) (le_refl _)
... = dist y (Φ p) + dist x (Ψ p) + dist y z : by ring },
linarith
end
| (inr x) (inr y) (inl z) := begin
have B : ∀a b, bdd_below (range (λ (p : γ), dist a (Φ p) + dist b (Ψ p))) :=
λa b, ⟨0, forall_range_iff.2 (λp, add_nonneg dist_nonneg dist_nonneg)⟩,
unfold glue_dist,
have : infi (λp, dist z (Φ p) + dist x (Ψ p)) ≤ dist x y + infi (λp, dist z (Φ p) + dist y (Ψ p)),
{ have : dist x y + infi (λp, dist z (Φ p) + dist y (Ψ p)) =
infi ((λt, dist x y + t) ∘ (λp, dist z (Φ p) + dist y (Ψ p))),
{ refine map_cinfi_of_continuous_at_of_monotone (continuous_at_const.add continuous_at_id) _
(B _ _),
intros x y hx, simpa },
rw [this, comp],
refine cinfi_le_cinfi (B _ _) (λp, _),
calc
dist z (Φ p) + dist x (Ψ p) ≤ dist z (Φ p) + (dist x y + dist y (Ψ p)) :
add_le_add (le_refl _) (dist_triangle _ _ _)
... = dist x y + (dist z (Φ p) + dist y (Ψ p)) : by ring },
linarith
end
| (inl x) (inl y) (inr z) := begin
have B : ∀a b, bdd_below (range (λ (p : γ), dist a (Φ p) + dist b (Ψ p))) :=
λa b, ⟨0, forall_range_iff.2 (λp, add_nonneg dist_nonneg dist_nonneg)⟩,
unfold glue_dist,
have : infi (λp, dist x (Φ p) + dist z (Ψ p)) ≤ dist x y + infi (λp, dist y (Φ p) + dist z (Ψ p)),
{ have : dist x y + infi (λp, dist y (Φ p) + dist z (Ψ p)) =
infi ((λt, dist x y + t) ∘ (λp, dist y (Φ p) + dist z (Ψ p))),
{ refine map_cinfi_of_continuous_at_of_monotone (continuous_at_const.add continuous_at_id) _
(B _ _),
intros x y hx, simpa },
rw [this, comp],
refine cinfi_le_cinfi (B _ _) (λp, _),
calc
dist x (Φ p) + dist z (Ψ p) ≤ (dist x y + dist y (Φ p)) + dist z (Ψ p) :
add_le_add (dist_triangle _ _ _) (le_refl _)
... = dist x y + (dist y (Φ p) + dist z (Ψ p)) : by ring },
linarith
end
| (inl x) (inr y) (inr z) := begin
have B : ∀a b, bdd_below (range (λ (p : γ), dist a (Φ p) + dist b (Ψ p))) :=
λa b, ⟨0, forall_range_iff.2 (λp, add_nonneg dist_nonneg dist_nonneg)⟩,
unfold glue_dist,
have : infi (λp, dist x (Φ p) + dist z (Ψ p)) ≤ infi (λp, dist x (Φ p) + dist y (Ψ p)) + dist y z,
{ have : infi (λp, dist x (Φ p) + dist y (Ψ p)) + dist y z =
infi ((λt, t + dist y z) ∘ (λp, dist x (Φ p) + dist y (Ψ p))),
{ refine map_cinfi_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) _
(B _ _),
intros x y hx, simpa },
rw [this, comp],
refine cinfi_le_cinfi (B _ _) (λp, _),
calc
dist x (Φ p) + dist z (Ψ p) ≤ dist x (Φ p) + (dist y z + dist y (Ψ p)) :
add_le_add (le_refl _) (dist_triangle_left _ _ _)
... = dist x (Φ p) + dist y (Ψ p) + dist y z : by ring },
linarith
end
| (inl x) (inr y) (inl z) := real.le_of_forall_epsilon_le $ λδ δpos, begin
have : ∃a ∈ range (λp, dist x (Φ p) + dist y (Ψ p)), a < infi (λp, dist x (Φ p) + dist y (Ψ p)) + δ/2 :=
exists_lt_of_cInf_lt (range_nonempty _) (by rw [infi]; linarith),
rcases this with ⟨a, arange, ha⟩,
rcases mem_range.1 arange with ⟨p, pa⟩,
rw ← pa at ha,
have : ∃b ∈ range (λp, dist z (Φ p) + dist y (Ψ p)), b < infi (λp, dist z (Φ p) + dist y (Ψ p)) + δ/2 :=
exists_lt_of_cInf_lt (range_nonempty _) (by rw [infi]; linarith),
rcases this with ⟨b, brange, hb⟩,
rcases mem_range.1 brange with ⟨q, qb⟩,
rw ← qb at hb,
have : dist (Φ p) (Φ q) ≤ dist (Ψ p) (Ψ q) + 2 * ε,
{ have := le_trans (le_abs_self _) (H p q), by linarith },
calc dist x z ≤ dist x (Φ p) + dist (Φ p) (Φ q) + dist (Φ q) z : dist_triangle4 _ _ _ _
... ≤ dist x (Φ p) + dist (Ψ p) (Ψ q) + dist z (Φ q) + 2 * ε : by rw [dist_comm z]; linarith
... ≤ dist x (Φ p) + (dist y (Ψ p) + dist y (Ψ q)) + dist z (Φ q) + 2 * ε :
add_le_add (add_le_add (add_le_add (le_refl _) (dist_triangle_left _ _ _)) (le_refl _)) (le_refl _)
... ≤ (infi (λp, dist x (Φ p) + dist y (Ψ p)) + ε) + (infi (λp, dist z (Φ p) + dist y (Ψ p)) + ε) + δ :
by linarith
end
| (inr x) (inl y) (inr z) := real.le_of_forall_epsilon_le $ λδ δpos, begin
have : ∃a ∈ range (λp, dist y (Φ p) + dist x (Ψ p)), a < infi (λp, dist y (Φ p) + dist x (Ψ p)) + δ/2 :=
exists_lt_of_cInf_lt (range_nonempty _) (by rw [infi]; linarith),
rcases this with ⟨a, arange, ha⟩,
rcases mem_range.1 arange with ⟨p, pa⟩,
rw ← pa at ha,
have : ∃b ∈ range (λp, dist y (Φ p) + dist z (Ψ p)), b < infi (λp, dist y (Φ p) + dist z (Ψ p)) + δ/2 :=
exists_lt_of_cInf_lt (range_nonempty _) (by rw [infi]; linarith),
rcases this with ⟨b, brange, hb⟩,
rcases mem_range.1 brange with ⟨q, qb⟩,
rw ← qb at hb,
have : dist (Ψ p) (Ψ q) ≤ dist (Φ p) (Φ q) + 2 * ε,
{ have := le_trans (neg_le_abs_self _) (H p q), by linarith },
calc dist x z ≤ dist x (Ψ p) + dist (Ψ p) (Ψ q) + dist (Ψ q) z : dist_triangle4 _ _ _ _
... ≤ dist x (Ψ p) + dist (Φ p) (Φ q) + dist z (Ψ q) + 2 * ε : by rw [dist_comm z]; linarith
... ≤ dist x (Ψ p) + (dist y (Φ p) + dist y (Φ q)) + dist z (Ψ q) + 2 * ε :
add_le_add (add_le_add (add_le_add (le_refl _) (dist_triangle_left _ _ _)) (le_refl _)) (le_refl _)
... ≤ (infi (λp, dist y (Φ p) + dist x (Ψ p)) + ε) + (infi (λp, dist y (Φ p) + dist z (Ψ p)) + ε) + δ :
by linarith
end
private lemma glue_eq_of_dist_eq_zero (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) (ε0 : 0 < ε) :
∀p q : α ⊕ β, glue_dist Φ Ψ ε p q = 0 → p = q
| (inl x) (inl y) h := by rw eq_of_dist_eq_zero h
| (inl x) (inr y) h := begin
have : 0 ≤ infi (λp, dist x (Φ p) + dist y (Ψ p)) :=
le_cinfi (λp, by simpa using add_le_add (@dist_nonneg _ _ x _) (@dist_nonneg _ _ y _)),
have : 0 + ε ≤ glue_dist Φ Ψ ε (inl x) (inr y) := add_le_add this (le_refl ε),
exfalso,
linarith
end
| (inr x) (inl y) h := begin
have : 0 ≤ infi (λp, dist y (Φ p) + dist x (Ψ p)) :=
le_cinfi (λp, by simpa [add_comm]
using add_le_add (@dist_nonneg _ _ x _) (@dist_nonneg _ _ y _)),
have : 0 + ε ≤ glue_dist Φ Ψ ε (inr x) (inl y) := add_le_add this (le_refl ε),
exfalso,
linarith
end
| (inr x) (inr y) h := by rw eq_of_dist_eq_zero h
/-- Given two maps Φ and Ψ intro metric spaces α and β such that the distances between Φ p and Φ q,
and between Ψ p and Ψ q, coincide up to 2 ε where ε > 0, one can almost glue the two spaces α
and β along the images of Φ and Ψ, so that Φ p and Ψ p are at distance ε. -/
def glue_metric_approx (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) (ε0 : 0 < ε)
(H : ∀p q, abs (dist (Φ p) (Φ q) - dist (Ψ p) (Ψ q)) ≤ 2 * ε) : metric_space (α ⊕ β) :=
{ dist := glue_dist Φ Ψ ε,
dist_self := glue_dist_self Φ Ψ ε,
dist_comm := glue_dist_comm Φ Ψ ε,
dist_triangle := glue_dist_triangle Φ Ψ ε H,
eq_of_dist_eq_zero := glue_eq_of_dist_eq_zero Φ Ψ ε ε0 }
end approx_gluing
section sum
/- A particular case of the previous construction is when one uses basepoints in α and β and one
glues only along the basepoints, putting them at distance 1. We give a direct definition of
the distance, without infi, as it is easier to use in applications, and show that it is equal to
the gluing distance defined above to take advantage of the lemmas we have already proved. -/
variables [metric_space α] [metric_space β] [inhabited α] [inhabited β]
open sum (inl inr)
/- Distance on a disjoint union. There are many (noncanonical) ways to put a distance compatible
with each factor.
If the two spaces are bounded, one can say for instance that each point in the first is at distance
`diam α + diam β + 1` of each point in the second.
Instead, we choose a construction that works for unbounded spaces, but requires basepoints.
We embed isometrically each factor, set the basepoints at distance 1,
arbitrarily, and say that the distance from `a` to `b` is the sum of the distances of `a` and `b` to
their respective basepoints, plus the distance 1 between the basepoints.
Since there is an arbitrary choice in this construction, it is not an instance by default. -/
def sum.dist : α ⊕ β → α ⊕ β → ℝ
| (inl a) (inl a') := dist a a'
| (inr b) (inr b') := dist b b'
| (inl a) (inr b) := dist a (default α) + 1 + dist (default β) b
| (inr b) (inl a) := dist b (default β) + 1 + dist (default α) a
lemma sum.dist_eq_glue_dist {p q : α ⊕ β} :
sum.dist p q = glue_dist (λ_ : unit, default α) (λ_ : unit, default β) 1 p q :=
by cases p; cases q; refl <|> simp [sum.dist, glue_dist, dist_comm, add_comm, add_left_comm]
private lemma sum.dist_comm (x y : α ⊕ β) : sum.dist x y = sum.dist y x :=
by cases x; cases y; simp only [sum.dist, dist_comm, add_comm, add_left_comm]
lemma sum.one_dist_le {x : α} {y : β} : 1 ≤ sum.dist (inl x) (inr y) :=
le_trans (le_add_of_nonneg_right dist_nonneg) $
add_le_add_right (le_add_of_nonneg_left dist_nonneg) _
lemma sum.one_dist_le' {x : α} {y : β} : 1 ≤ sum.dist (inr y) (inl x) :=
by rw sum.dist_comm; exact sum.one_dist_le
private lemma sum.mem_uniformity (s : set ((α ⊕ β) × (α ⊕ β))) :
s ∈ 𝓤 (α ⊕ β) ↔ ∃ ε > 0, ∀ a b, sum.dist a b < ε → (a, b) ∈ s :=
begin
split,
{ rintro ⟨hsα, hsβ⟩,
rcases mem_uniformity_dist.1 hsα with ⟨εα, εα0, hα⟩,
rcases mem_uniformity_dist.1 hsβ with ⟨εβ, εβ0, hβ⟩,
refine ⟨min (min εα εβ) 1, lt_min (lt_min εα0 εβ0) zero_lt_one, _⟩,
rintro (a|a) (b|b) h,
{ exact hα (lt_of_lt_of_le h (le_trans (min_le_left _ _) (min_le_left _ _))) },
{ cases not_le_of_lt (lt_of_lt_of_le h (min_le_right _ _)) sum.one_dist_le },
{ cases not_le_of_lt (lt_of_lt_of_le h (min_le_right _ _)) sum.one_dist_le' },
{ exact hβ (lt_of_lt_of_le h (le_trans (min_le_left _ _) (min_le_right _ _))) } },
{ rintro ⟨ε, ε0, H⟩,
split; rw [filter.mem_sets, filter.mem_map, mem_uniformity_dist];
exact ⟨ε, ε0, λ x y h, H _ _ (by exact h)⟩ }
end
/-- The distance on the disjoint union indeed defines a metric space. All the distance properties
follow from our choice of the distance. The harder work is to show that the uniform structure
defined by the distance coincides with the disjoint union uniform structure. -/
def metric_space_sum : metric_space (α ⊕ β) :=
{ dist := sum.dist,
dist_self := λx, by cases x; simp only [sum.dist, dist_self],
dist_comm := sum.dist_comm,
dist_triangle := λp q r,
by simp only [dist, sum.dist_eq_glue_dist]; exact glue_dist_triangle _ _ _ (by simp; norm_num) _ _ _,
eq_of_dist_eq_zero := λp q,
by simp only [dist, sum.dist_eq_glue_dist]; exact glue_eq_of_dist_eq_zero _ _ _ zero_lt_one _ _,
to_uniform_space := sum.uniform_space,
uniformity_dist := uniformity_dist_of_mem_uniformity _ _ sum.mem_uniformity }
local attribute [instance] metric_space_sum
lemma sum.dist_eq {x y : α ⊕ β} : dist x y = sum.dist x y := rfl
/-- The left injection of a space in a disjoint union in an isometry -/
lemma isometry_on_inl : isometry (sum.inl : α → (α ⊕ β)) :=
isometry_emetric_iff_metric.2 $ λx y, rfl
/-- The right injection of a space in a disjoint union in an isometry -/
lemma isometry_on_inr : isometry (sum.inr : β → (α ⊕ β)) :=
isometry_emetric_iff_metric.2 $ λx y, rfl
end sum
section gluing
/- Exact gluing of two metric spaces along isometric subsets. -/
variables [nonempty γ] [metric_space γ] [metric_space α] [metric_space β]
{Φ : γ → α} {Ψ : γ → β} {ε : ℝ}
open sum (inl inr)
local attribute [instance] premetric.dist_setoid
def glue_premetric (hΦ : isometry Φ) (hΨ : isometry Ψ) : premetric_space (α ⊕ β) :=
{ dist := glue_dist Φ Ψ 0,
dist_self := glue_dist_self Φ Ψ 0,
dist_comm := glue_dist_comm Φ Ψ 0,
dist_triangle := glue_dist_triangle Φ Ψ 0 $ λp q, by rw [hΦ.dist_eq, hΨ.dist_eq]; simp }
def glue_space (hΦ : isometry Φ) (hΨ : isometry Ψ) : Type* :=
@metric_quot _ (glue_premetric hΦ hΨ)
instance metric_space_glue_space (hΦ : isometry Φ) (hΨ : isometry Ψ) :
metric_space (glue_space hΦ hΨ) :=
@premetric.metric_space_quot _ (glue_premetric hΦ hΨ)
def to_glue_l (hΦ : isometry Φ) (hΨ : isometry Ψ) (x : α) : glue_space hΦ hΨ :=
by letI : premetric_space (α ⊕ β) := glue_premetric hΦ hΨ; exact ⟦inl x⟧
def to_glue_r (hΦ : isometry Φ) (hΨ : isometry Ψ) (y : β) : glue_space hΦ hΨ :=
by letI : premetric_space (α ⊕ β) := glue_premetric hΦ hΨ; exact ⟦inr y⟧
instance inhabited_left (hΦ : isometry Φ) (hΨ : isometry Ψ) [inhabited α] :
inhabited (glue_space hΦ hΨ) :=
⟨to_glue_l _ _ (default _)⟩
instance inhabited_right (hΦ : isometry Φ) (hΨ : isometry Ψ) [inhabited β] :
inhabited (glue_space hΦ hΨ) :=
⟨to_glue_r _ _ (default _)⟩
lemma to_glue_commute (hΦ : isometry Φ) (hΨ : isometry Ψ) :
(to_glue_l hΦ hΨ) ∘ Φ = (to_glue_r hΦ hΨ) ∘ Ψ :=
begin
letI : premetric_space (α ⊕ β) := glue_premetric hΦ hΨ,
funext,
simp only [comp, to_glue_l, to_glue_r, quotient.eq],
exact glue_dist_glued_points Φ Ψ 0 x
end
lemma to_glue_l_isometry (hΦ : isometry Φ) (hΨ : isometry Ψ) : isometry (to_glue_l hΦ hΨ) :=
isometry_emetric_iff_metric.2 $ λ_ _, rfl
lemma to_glue_r_isometry (hΦ : isometry Φ) (hΨ : isometry Ψ) : isometry (to_glue_r hΦ hΨ) :=
isometry_emetric_iff_metric.2 $ λ_ _, rfl
end gluing --section
section inductive_limit
/- In this section, we define the inductive limit of
f 0 f 1 f 2 f 3
X 0 -----> X 1 -----> X 2 -----> X 3 -----> ...
where the X n are metric spaces and f n isometric embeddings. We do it by defining a premetric
space structure on Σn, X n, where the predistance dist x y is obtained by pushing x and y in a
common X k using composition by the f n, and taking the distance there. This does not depend on
the choice of k as the f n are isometries. The metric space associated to this premetric space
is the desired inductive limit.-/
open nat
variables {X : ℕ → Type u} [∀n, metric_space (X n)] {f : Πn, X n → X (n+1)}
/-- Predistance on the disjoint union Σn, X n. -/
def inductive_limit_dist (f : Πn, X n → X (n+1)) (x y : Σn, X n) : ℝ :=
dist (le_rec_on (le_max_left x.1 y.1) f x.2 : X (max x.1 y.1))
(le_rec_on (le_max_right x.1 y.1) f y.2 : X (max x.1 y.1))
/-- The predistance on the disjoint union Σn, X n can be computed in any X k for large enough k.-/
lemma inductive_limit_dist_eq_dist (I : ∀n, isometry (f n))
(x y : Σn, X n) (m : ℕ) : ∀hx : x.1 ≤ m, ∀hy : y.1 ≤ m,
inductive_limit_dist f x y = dist (le_rec_on hx f x.2 : X m) (le_rec_on hy f y.2 : X m) :=
begin
induction m with m hm,
{ assume hx hy,
have A : max x.1 y.1 = 0, { rw [le_zero_iff_eq.1 hx, le_zero_iff_eq.1 hy], simp },
unfold inductive_limit_dist,
congr; simp only [A] },
{ assume hx hy,
by_cases h : max x.1 y.1 = m.succ,
{ unfold inductive_limit_dist,
congr; simp only [h] },
{ have : max x.1 y.1 ≤ succ m := by simp [hx, hy],
have : max x.1 y.1 ≤ m := by simpa [h] using of_le_succ this,
have xm : x.1 ≤ m := le_trans (le_max_left _ _) this,
have ym : y.1 ≤ m := le_trans (le_max_right _ _) this,
rw [le_rec_on_succ xm, le_rec_on_succ ym, (I m).dist_eq],
exact hm xm ym }}
end
/-- Premetric space structure on Σn, X n.-/
def inductive_premetric (I : ∀n, isometry (f n)) :
premetric_space (Σn, X n) :=
{ dist := inductive_limit_dist f,
dist_self := λx, by simp [dist, inductive_limit_dist],
dist_comm := λx y, begin
let m := max x.1 y.1,
have hx : x.1 ≤ m := le_max_left _ _,
have hy : y.1 ≤ m := le_max_right _ _,
unfold dist,
rw [inductive_limit_dist_eq_dist I x y m hx hy, inductive_limit_dist_eq_dist I y x m hy hx,
dist_comm]
end,
dist_triangle := λx y z, begin
let m := max (max x.1 y.1) z.1,
have hx : x.1 ≤ m := le_trans (le_max_left _ _) (le_max_left _ _),
have hy : y.1 ≤ m := le_trans (le_max_right _ _) (le_max_left _ _),
have hz : z.1 ≤ m := le_max_right _ _,
calc inductive_limit_dist f x z
= dist (le_rec_on hx f x.2 : X m) (le_rec_on hz f z.2 : X m) :
inductive_limit_dist_eq_dist I x z m hx hz
... ≤ dist (le_rec_on hx f x.2 : X m) (le_rec_on hy f y.2 : X m)
+ dist (le_rec_on hy f y.2 : X m) (le_rec_on hz f z.2 : X m) :
dist_triangle _ _ _
... = inductive_limit_dist f x y + inductive_limit_dist f y z :
by rw [inductive_limit_dist_eq_dist I x y m hx hy,
inductive_limit_dist_eq_dist I y z m hy hz]
end }
local attribute [instance] inductive_premetric premetric.dist_setoid
/-- The type giving the inductive limit in a metric space context. -/
def inductive_limit (I : ∀n, isometry (f n)) : Type* :=
@metric_quot _ (inductive_premetric I)
/-- Metric space structure on the inductive limit. -/
instance metric_space_inductive_limit (I : ∀n, isometry (f n)) :
metric_space (inductive_limit I) :=
@premetric.metric_space_quot _ (inductive_premetric I)
/-- Mapping each `X n` to the inductive limit. -/
def to_inductive_limit (I : ∀n, isometry (f n)) (n : ℕ) (x : X n) : metric.inductive_limit I :=
by letI : premetric_space (Σn, X n) := inductive_premetric I; exact ⟦sigma.mk n x⟧
instance (I : ∀ n, isometry (f n)) [inhabited (X 0)] : inhabited (inductive_limit I) :=
⟨to_inductive_limit _ 0 (default _)⟩
/-- The map `to_inductive_limit n` mapping `X n` to the inductive limit is an isometry. -/
lemma to_inductive_limit_isometry (I : ∀n, isometry (f n)) (n : ℕ) :
isometry (to_inductive_limit I n) := isometry_emetric_iff_metric.2 $ λx y,
begin
change inductive_limit_dist f ⟨n, x⟩ ⟨n, y⟩ = dist x y,
rw [inductive_limit_dist_eq_dist I ⟨n, x⟩ ⟨n, y⟩ n (le_refl n) (le_refl n),
le_rec_on_self, le_rec_on_self]
end
/-- The maps `to_inductive_limit n` are compatible with the maps `f n`. -/
lemma to_inductive_limit_commute (I : ∀n, isometry (f n)) (n : ℕ) :
(to_inductive_limit I n.succ) ∘ (f n) = to_inductive_limit I n :=
begin
funext,
simp only [comp, to_inductive_limit, quotient.eq],
show inductive_limit_dist f ⟨n.succ, f n x⟩ ⟨n, x⟩ = 0,
{ rw [inductive_limit_dist_eq_dist I ⟨n.succ, f n x⟩ ⟨n, x⟩ n.succ,
le_rec_on_self, le_rec_on_succ, le_rec_on_self, dist_self],
exact le_refl _,
exact le_refl _,
exact le_succ _ }
end
end inductive_limit --section
end metric --namespace
|
feb7151379baba86de55cd4d4044f51ca7e58298 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/measure_theory/integral/layercake.lean | beeef7164966addf02dbbb322a3f2ac85009ad0c | [
"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 | 11,808 | lean | /-
Copyright (c) 2022 Kalle Kytölä. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kalle Kytölä
-/
import measure_theory.integral.interval_integral
import analysis.special_functions.integrals
/-!
# The layer cake formula / Cavalieri's principle / tail probability formula
In this file we prove the following layer cake formula.
Consider a non-negative measurable function `f` on a sigma-finite measure space. Apply pointwise
to it an increasing absolutely continuous function `G : ℝ≥0 → ℝ≥0` vanishing at the origin, with
derivative `G' = g` on the positive real line (in other words, `G` a primitive of a non-negative
locally integrable function `g` on the positive real line). Then the integral of the result,
`∫ G ∘ f`, can be written as the integral over the positive real line of the "tail measures" of `f`
(i.e., a function giving the measures of the sets on which `f` exceeds different positive real
values) weighted by `g`. In probability theory contexts, the "tail measures" could be referred to
as "tail probabilities" of the random variable `f`, or as values of the "complementary cumulative
distribution function" of the random variable `f`. The terminology "tail probability formula" is
therefore occasionally used for the layer cake formula (or a standard application of it).
The essence of the (mathematical) proof is Fubini's theorem.
We also give the two most common applications of the layer cake formula
* a representation of the integral of a nonnegative function f:
∫ f(ω) ∂μ(ω) = ∫ μ {ω | f(ω) ≥ t} dt
* a representation of the integral of the p:th power of a nonnegative function f:
∫ f(ω)^p ∂μ(ω) = p * ∫ t^(p-1) * μ {ω | f(ω) ≥ t} dt .
## Main results
* `lintegral_comp_eq_lintegral_meas_le_mul`: The general layer cake formula with Lebesgue
integrals.
* `lintegral_eq_lintegral_meas_le`: The most common special case of the layer cake formula, which
states that for a nonnegative function f we have ∫ f(ω) ∂μ(ω) = ∫ μ {ω | f(ω) ≥ t} dt .
## Tags
layer cake representation, Cavalieri's principle, tail probability formula
-/
noncomputable theory
open_locale ennreal measure_theory
open set measure_theory filter
/-! ### Layercake theorem -/
section layercake
namespace measure_theory
variables {α : Type*} [measurable_space α] {f : α → ℝ} {g : ℝ → ℝ} {s : set α}
/-- An auxiliary version of the layer cake theorem (Cavalieri's principle, tail probability
formula), with a measurability assumption that would also essentially follow from the
integrability assumptions.
See `measure_theory.layercake` for the main formulation of the layer cake theorem. -/
lemma lintegral_comp_eq_lintegral_meas_le_mul_of_measurable (μ : measure α) [sigma_finite μ]
(f_nn : 0 ≤ f) (f_mble : measurable f)
(g_intble : ∀ t > 0, interval_integrable g volume 0 t)
(g_mble : measurable g) (g_nn : ∀ t > 0, 0 ≤ g t) :
∫⁻ ω, ennreal.of_real (∫ t in 0 .. (f ω), g t) ∂μ
= ∫⁻ t in Ioi 0, (μ {a : α | t ≤ f a}) * ennreal.of_real (g t) :=
begin
have g_intble' : ∀ (t : ℝ), 0 ≤ t → interval_integrable g volume 0 t,
{ intros t ht,
cases eq_or_lt_of_le ht,
{ simp [← h], },
{ exact g_intble t h, }, },
have integrand_eq : ∀ ω, ennreal.of_real (∫ t in 0 .. (f ω), g t)
= ∫⁻ t in Ioc 0 (f ω), ennreal.of_real (g t),
{ intro ω,
have g_ae_nn : 0 ≤ᵐ[volume.restrict (Ioc 0 (f ω))] g,
{ filter_upwards [self_mem_ae_restrict (measurable_set_Ioc : measurable_set (Ioc 0 (f ω)))]
with x hx using g_nn x hx.1, },
rw ← of_real_integral_eq_lintegral_of_real (g_intble' (f ω) (f_nn ω)).1 g_ae_nn,
congr,
exact interval_integral.integral_of_le (f_nn ω), },
simp_rw [integrand_eq, ← lintegral_indicator (λ t, ennreal.of_real (g t)) measurable_set_Ioc,
← lintegral_indicator _ measurable_set_Ioi],
rw lintegral_lintegral_swap,
{ apply congr_arg,
funext s,
have aux₁ : (λ x, (Ioc 0 (f x)).indicator (λ (t : ℝ), ennreal.of_real (g t)) s)
= (λ x, (ennreal.of_real (g s) * (Ioi (0 : ℝ)).indicator (λ _, 1) s)
* (Ici s).indicator (λ (t : ℝ), (1 : ℝ≥0∞)) (f x)),
{ funext a,
by_cases s ∈ Ioc (0 : ℝ) (f a),
{ simp only [h, (show s ∈ Ioi (0 : ℝ), from h.1),
(show f a ∈ Ici s, from h.2), indicator_of_mem, mul_one], },
{ have h_copy := h,
simp only [mem_Ioc, not_and, not_le] at h,
by_cases h' : 0 < s,
{ simp only [h_copy, h h', indicator_of_not_mem, not_false_iff, mem_Ici, not_le,
mul_zero], },
{ have : s ∉ Ioi (0 : ℝ) := h',
simp only [this, h', indicator_of_not_mem, not_false_iff, mul_zero, zero_mul, mem_Ioc,
false_and], }, }, },
simp_rw aux₁,
rw lintegral_const_mul',
swap, { apply ennreal.mul_ne_top ennreal.of_real_ne_top,
by_cases s ∈ Ioi (0 : ℝ); { simp [h], }, },
simp_rw [(show (λ a, (Ici s).indicator (λ (t : ℝ), (1 : ℝ≥0∞)) (f a))
= (λ a, {a : α | s ≤ f a}.indicator (λ _, 1) a),
by { funext a, by_cases s ≤ f a; simp [h], })],
rw lintegral_indicator,
swap, { exact f_mble measurable_set_Ici, },
rw [lintegral_one, measure.restrict_apply measurable_set.univ, univ_inter, indicator_mul_left,
mul_assoc,
(show (Ioi 0).indicator (λ (_x : ℝ), (1 : ℝ≥0∞)) s * μ {a : α | s ≤ f a}
= (Ioi 0).indicator (λ (_x : ℝ), 1 * μ {a : α | s ≤ f a}) s,
by { by_cases 0 < s; simp [h], })],
simp_rw [mul_comm _ (ennreal.of_real _), one_mul],
refl, },
have aux₂ : function.uncurry
(λ (x : α) (y : ℝ), (Ioc 0 (f x)).indicator (λ (t : ℝ), ennreal.of_real (g t)) y)
= {p : α × ℝ | p.2 ∈ Ioc 0 (f p.1)}.indicator (λ p, ennreal.of_real (g p.2)),
{ funext p,
cases p,
rw function.uncurry_apply_pair,
by_cases p_snd ∈ Ioc 0 (f p_fst),
{ have h' : (p_fst, p_snd) ∈ {p : α × ℝ | p.snd ∈ Ioc 0 (f p.fst)} := h,
rw [set.indicator_of_mem h', set.indicator_of_mem h], },
{ have h' : (p_fst, p_snd) ∉ {p : α × ℝ | p.snd ∈ Ioc 0 (f p.fst)} := h,
rw [set.indicator_of_not_mem h', set.indicator_of_not_mem h], }, },
rw aux₂,
have mble := measurable_set_region_between_oc measurable_zero f_mble measurable_set.univ,
simp_rw [mem_univ, pi.zero_apply, true_and] at mble,
exact (ennreal.measurable_of_real.comp (g_mble.comp measurable_snd)).ae_measurable.indicator mble,
end
/-- The layer cake theorem / Cavalieri's principle / tail probability formula:
Let `f` be a non-negative measurable function on a sigma-finite measure space. Let `G` be an
increasing absolutely continuous function on the positive real line, vanishing at the origin,
with derivative `G' = g`. Then the integral of the composition `G ∘ f` can be written as
the integral over the positive real line of the "tail measures" `μ {ω | f(ω) ≥ t}` of `f`
weighted by `g`.
Roughly speaking, the statement is: `∫⁻ (G ∘ f) ∂μ = ∫⁻ t in 0 .. ∞, g(t) * μ {ω | f(ω) ≥ t}`. -/
theorem lintegral_comp_eq_lintegral_meas_le_mul (μ : measure α) [sigma_finite μ]
(f_nn : 0 ≤ f) (f_mble : measurable f)
(g_intble : ∀ t > 0, interval_integrable g volume 0 t)
(g_nn : ∀ᵐ t ∂(volume.restrict (Ioi 0)), 0 ≤ g t) :
∫⁻ ω, ennreal.of_real (∫ t in 0 .. f ω, g t) ∂μ
= ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ennreal.of_real (g t) :=
begin
have ex_G : ∃ (G : ℝ → ℝ), measurable G ∧ 0 ≤ G ∧ g =ᵐ[volume.restrict (Ioi 0)] G,
{ refine ae_measurable.exists_measurable_nonneg _ g_nn,
exact ae_measurable_Ioi_of_forall_Ioc (λ t ht, (g_intble t ht).1.1.ae_measurable), },
rcases ex_G with ⟨G, G_mble, G_nn, g_eq_G⟩,
have g_eq_G_on : ∀ t, g =ᵐ[volume.restrict (Ioc 0 t)] G,
from λ t, ae_mono (measure.restrict_mono Ioc_subset_Ioi_self le_rfl) g_eq_G,
have G_intble : ∀ t > 0, interval_integrable G volume 0 t,
{ refine λ t t_pos, ⟨integrable_on.congr_fun' (g_intble t t_pos).1 (g_eq_G_on t), _⟩,
rw Ioc_eq_empty_of_le t_pos.lt.le,
exact integrable_on_empty, },
have eq₁ : ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ennreal.of_real (g t)
= ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ennreal.of_real (G t),
{ apply lintegral_congr_ae,
filter_upwards [g_eq_G] with a ha,
rw [ha], },
have eq₂ : ∀ ω, ∫ t in 0..f ω, g t = ∫ t in 0..f ω, G t,
{ refine λ ω, interval_integral.integral_congr_ae _,
have fω_nn : 0 ≤ f ω := f_nn ω,
rw [interval_oc_of_le fω_nn,
← ae_restrict_iff' (measurable_set_Ioc : measurable_set (Ioc (0 : ℝ) (f ω)))],
exact g_eq_G_on (f ω), },
simp_rw [eq₁, eq₂],
exact lintegral_comp_eq_lintegral_meas_le_mul_of_measurable μ f_nn f_mble
G_intble G_mble (λ t t_pos, G_nn t),
end
/-- The standard case of the layer cake formula / Cavalieri's principle / tail probability formula:
For a nonnegative function `f` on a sigma-finite measure space, the Lebesgue integral of `f` can
be written (roughly speaking) as: `∫⁻ f ∂μ = ∫⁻ t in 0 .. ∞, μ {ω | f(ω) ≥ t}`. -/
theorem lintegral_eq_lintegral_meas_le (μ : measure α) [sigma_finite μ]
(f_nn : 0 ≤ f) (f_mble : measurable f) :
∫⁻ ω, ennreal.of_real (f ω) ∂μ = ∫⁻ t in Ioi 0, (μ {a : α | t ≤ f a}) :=
begin
set cst := λ (t : ℝ), (1 : ℝ) with def_cst,
have cst_intble : ∀ t > 0, interval_integrable cst volume 0 t,
from λ _ _, interval_integrable_const,
have key := lintegral_comp_eq_lintegral_meas_le_mul μ f_nn f_mble cst_intble
(eventually_of_forall (λ t, zero_le_one)),
simp_rw [def_cst, ennreal.of_real_one, mul_one] at key,
rw ← key,
congr' with ω,
simp only [interval_integral.integral_const, sub_zero, algebra.id.smul_eq_mul, mul_one],
end
/-- An application of the layer cake formula / Cavalieri's principle / tail probability formula:
For a nonnegative function `f` on a sigma-finite measure space, the Lebesgue integral of `f` can
be written (roughly speaking) as: `∫⁻ f^p ∂μ = p * ∫⁻ t in 0 .. ∞, t^(p-1) * μ {ω | f(ω) ≥ t}`. -/
theorem lintegral_rpow_eq_lintegral_meas_le_mul (μ : measure α) [sigma_finite μ]
(f_nn : 0 ≤ f) (f_mble : measurable f) {p : ℝ} (p_pos: 0 < p) :
∫⁻ ω, ennreal.of_real ((f ω)^p) ∂μ
= (ennreal.of_real p) * ∫⁻ t in Ioi 0, (μ {a : α | t ≤ f a}) * ennreal.of_real (t^(p-1)) :=
begin
have one_lt_p : -1 < p - 1 := by linarith,
have obs : ∀ (x : ℝ), (∫ (t : ℝ) in 0..x, t^(p-1)) = x^p / p,
{ intros x,
rw integral_rpow (or.inl one_lt_p),
simp [real.zero_rpow p_pos.ne.symm], },
set g := λ (t : ℝ), t^(p-1) with g_def,
have g_nn : ∀ᵐ t ∂(volume.restrict (Ioi (0 : ℝ))), 0 ≤ g t,
{ filter_upwards [self_mem_ae_restrict (measurable_set_Ioi : measurable_set (Ioi (0 : ℝ)))],
intros t t_pos,
rw g_def,
exact real.rpow_nonneg_of_nonneg (mem_Ioi.mp t_pos).le (p - 1), },
have g_intble : ∀ t > 0, interval_integrable g volume 0 t,
from λ _ _, interval_integral.interval_integrable_rpow' one_lt_p,
have key := lintegral_comp_eq_lintegral_meas_le_mul μ f_nn f_mble g_intble g_nn,
simp_rw [g_def] at key,
rw [← key, ← lintegral_const_mul (ennreal.of_real p)]; simp_rw obs,
{ congr' with ω,
rw [← ennreal.of_real_mul p_pos.le, mul_div_cancel' ((f ω)^p) p_pos.ne.symm], },
{ exact ((f_mble.pow measurable_const).div_const p).ennreal_of_real, },
end
end measure_theory
end layercake
|
1cd949b71e7fffe979500df06c9f0fdd5a957f08 | 853df553b1d6ca524e3f0a79aedd32dde5d27ec3 | /src/ring_theory/polynomial/basic.lean | 218f31584df1a5958aaae674c6233380fea7f734 | [
"Apache-2.0"
] | permissive | DanielFabian/mathlib | efc3a50b5dde303c59eeb6353ef4c35a345d7112 | f520d07eba0c852e96fe26da71d85bf6d40fcc2a | refs/heads/master | 1,668,739,922,971 | 1,595,201,756,000 | 1,595,201,756,000 | 279,469,476 | 0 | 0 | null | 1,594,696,604,000 | 1,594,696,604,000 | null | UTF-8 | Lean | false | false | 19,776 | lean | /-
Copyright (c) 2019 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
Ring-theoretic supplement of data.polynomial.
Main result: Hilbert basis theorem, that if a ring is noetherian then so is its polynomial ring.
-/
import algebra.char_p
import data.mv_polynomial
import ring_theory.noetherian
noncomputable theory
local attribute [instance, priority 100] classical.prop_decidable
universes u v w
namespace polynomial
instance {R : Type u} [comm_semiring R] (p : ℕ) [h : char_p R p] : char_p (polynomial R) p :=
let ⟨h⟩ := h in ⟨λ n, by rw [← C.map_nat_cast, ← C_0, C_inj, h]⟩
variables (R : Type u) [comm_ring R]
/-- The `R`-submodule of `R[X]` consisting of polynomials of degree ≤ `n`. -/
def degree_le (n : with_bot ℕ) : submodule R (polynomial R) :=
⨅ k : ℕ, ⨅ h : ↑k > n, (lcoeff R k).ker
/-- The `R`-submodule of `R[X]` consisting of polynomials of degree < `n`. -/
def degree_lt (n : ℕ) : submodule R (polynomial R) :=
⨅ k : ℕ, ⨅ h : k ≥ n, (lcoeff R k).ker
variable {R}
theorem mem_degree_le {n : with_bot ℕ} {f : polynomial R} :
f ∈ degree_le R n ↔ degree f ≤ n :=
by simp only [degree_le, submodule.mem_infi, degree_le_iff_coeff_zero, linear_map.mem_ker]; refl
@[mono] theorem degree_le_mono {m n : with_bot ℕ} (H : m ≤ n) :
degree_le R m ≤ degree_le R n :=
λ f hf, mem_degree_le.2 (le_trans (mem_degree_le.1 hf) H)
theorem degree_le_eq_span_X_pow {n : ℕ} :
degree_le R n = submodule.span R ↑((finset.range (n+1)).image (λ n, X^n) : finset (polynomial R)) :=
begin
apply le_antisymm,
{ intros p hp, replace hp := mem_degree_le.1 hp,
rw [← finsupp.sum_single p, finsupp.sum],
refine submodule.sum_mem _ (λ k hk, _),
show monomial _ _ ∈ _,
have := with_bot.coe_le_coe.1 (finset.sup_le_iff.1 hp k hk),
rw [single_eq_C_mul_X, C_mul'],
refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $
finset.mem_image.2 ⟨_, finset.mem_range.2 (nat.lt_succ_of_le this), rfl⟩) },
rw [submodule.span_le, finset.coe_image, set.image_subset_iff],
intros k hk, apply mem_degree_le.2,
apply le_trans (degree_X_pow_le _) (with_bot.coe_le_coe.2 $ nat.le_of_lt_succ $ finset.mem_range.1 hk)
end
theorem mem_degree_lt {n : ℕ} {f : polynomial R} :
f ∈ degree_lt R n ↔ degree f < n :=
by { simp_rw [degree_lt, submodule.mem_infi, linear_map.mem_ker, degree,
finset.sup_lt_iff (with_bot.bot_lt_coe n), finsupp.mem_support_iff, with_bot.some_eq_coe,
with_bot.coe_lt_coe, lt_iff_not_ge', ne, not_imp_not], refl }
@[mono] theorem degree_lt_mono {m n : ℕ} (H : m ≤ n) :
degree_lt R m ≤ degree_lt R n :=
λ f hf, mem_degree_lt.2 (lt_of_lt_of_le (mem_degree_lt.1 hf) $ with_bot.coe_le_coe.2 H)
theorem degree_lt_eq_span_X_pow {n : ℕ} :
degree_lt R n = submodule.span R ↑((finset.range n).image (λ n, X^n) : finset (polynomial R)) :=
begin
apply le_antisymm,
{ intros p hp, replace hp := mem_degree_lt.1 hp,
rw [← finsupp.sum_single p, finsupp.sum],
refine submodule.sum_mem _ (λ k hk, _),
show monomial _ _ ∈ _,
have := with_bot.coe_lt_coe.1 ((finset.sup_lt_iff $ with_bot.bot_lt_coe n).1 hp k hk),
rw [single_eq_C_mul_X, C_mul'],
refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $
finset.mem_image.2 ⟨_, finset.mem_range.2 this, rfl⟩) },
rw [submodule.span_le, finset.coe_image, set.image_subset_iff],
intros k hk, apply mem_degree_lt.2,
exact lt_of_le_of_lt (degree_X_pow_le _) (with_bot.coe_lt_coe.2 $ finset.mem_range.1 hk)
end
/-- Given a polynomial, return the polynomial whose coefficients are in
the ring closure of the original coefficients. -/
def restriction (p : polynomial R) : polynomial (ring.closure (↑p.frange : set R)) :=
⟨p.support, λ i, ⟨p.to_fun i,
if H : p.to_fun i = 0 then H.symm ▸ is_add_submonoid.zero_mem
else ring.subset_closure $ finsupp.mem_frange.2 ⟨H, i, rfl⟩⟩,
λ i, finsupp.mem_support_iff.trans (not_iff_not_of_iff ⟨λ H, subtype.eq H, subtype.mk.inj⟩)⟩
@[simp] theorem coeff_restriction {p : polynomial R} {n : ℕ} : ↑(coeff (restriction p) n) = coeff p n := rfl
@[simp] theorem coeff_restriction' {p : polynomial R} {n : ℕ} : (coeff (restriction p) n).1 = coeff p n := rfl
@[simp] theorem degree_restriction {p : polynomial R} : (restriction p).degree = p.degree := rfl
@[simp] theorem nat_degree_restriction {p : polynomial R} : (restriction p).nat_degree = p.nat_degree := rfl
@[simp] theorem monic_restriction {p : polynomial R} : monic (restriction p) ↔ monic p :=
⟨λ H, congr_arg subtype.val H, λ H, subtype.eq H⟩
@[simp] theorem restriction_zero : restriction (0 : polynomial R) = 0 := rfl
@[simp] theorem restriction_one : restriction (1 : polynomial R) = 1 :=
ext $ λ i, subtype.eq $ by rw [coeff_restriction', coeff_one, coeff_one]; split_ifs; refl
variables {S : Type v} [comm_ring S] {f : R →+* S} {x : S}
theorem eval₂_restriction {p : polynomial R} :
eval₂ f x p = eval₂ (f.comp (is_subring.subtype _)) x p.restriction :=
rfl
section to_subring
variables (p : polynomial R) (T : set R) [is_subring T]
/-- Given a polynomial `p` and a subring `T` that contains the coefficients of `p`,
return the corresponding polynomial whose coefficients are in `T. -/
def to_subring (hp : ↑p.frange ⊆ T) : polynomial T :=
⟨p.support, λ i, ⟨p.to_fun i,
if H : p.to_fun i = 0 then H.symm ▸ is_add_submonoid.zero_mem
else hp $ finsupp.mem_frange.2 ⟨H, i, rfl⟩⟩,
λ i, finsupp.mem_support_iff.trans (not_iff_not_of_iff ⟨λ H, subtype.eq H, subtype.mk.inj⟩)⟩
variables (hp : ↑p.frange ⊆ T)
include hp
@[simp] theorem coeff_to_subring {n : ℕ} : ↑(coeff (to_subring p T hp) n) = coeff p n := rfl
@[simp] theorem coeff_to_subring' {n : ℕ} : (coeff (to_subring p T hp) n).1 = coeff p n := rfl
@[simp] theorem degree_to_subring : (to_subring p T hp).degree = p.degree := rfl
@[simp] theorem nat_degree_to_subring : (to_subring p T hp).nat_degree = p.nat_degree := rfl
@[simp] theorem monic_to_subring : monic (to_subring p T hp) ↔ monic p :=
⟨λ H, congr_arg subtype.val H, λ H, subtype.eq H⟩
omit hp
@[simp] theorem to_subring_zero : to_subring (0 : polynomial R) T (set.empty_subset _) = 0 := rfl
@[simp] theorem to_subring_one : to_subring (1 : polynomial R) T
(set.subset.trans (finset.coe_subset.2 finsupp.frange_single)
(finset.singleton_subset_set_iff.2 is_submonoid.one_mem)) = 1 :=
ext $ λ i, subtype.eq $ by rw [coeff_to_subring', coeff_one, coeff_one]; split_ifs; refl
end to_subring
variables (T : set R) [is_subring T]
/-- Given a polynomial whose coefficients are in some subring, return
the corresponding polynomial whose coefificents are in the ambient ring. -/
def of_subring (p : polynomial T) : polynomial R :=
⟨p.support, subtype.val ∘ p.to_fun,
λ n, finsupp.mem_support_iff.trans (not_iff_not_of_iff
⟨λ h, congr_arg subtype.val h, λ h, subtype.eq h⟩)⟩
@[simp] theorem frange_of_subring {p : polynomial T} :
↑(p.of_subring T).frange ⊆ T :=
λ y H, let ⟨hy, x, hx⟩ := finsupp.mem_frange.1 H in hx ▸ (p.to_fun x).2
end polynomial
variables {R : Type u} {σ : Type v} [comm_ring R]
namespace ideal
open polynomial
/-- Transport an ideal of `R[X]` to an `R`-submodule of `R[X]`. -/
def of_polynomial (I : ideal (polynomial R)) : submodule R (polynomial R) :=
{ carrier := I.carrier,
zero_mem' := I.zero_mem,
add_mem' := λ _ _, I.add_mem,
smul_mem' := λ c x H, by rw [← C_mul']; exact submodule.smul_mem _ _ H }
variables {I : ideal (polynomial R)}
theorem mem_of_polynomial (x) : x ∈ I.of_polynomial ↔ x ∈ I := iff.rfl
variables (I)
/-- Given an ideal `I` of `R[X]`, make the `R`-submodule of `I`
consisting of polynomials of degree ≤ `n`. -/
def degree_le (n : with_bot ℕ) : submodule R (polynomial R) :=
degree_le R n ⊓ I.of_polynomial
/-- Given an ideal `I` of `R[X]`, make the ideal in `R` of
leading coefficients of polynomials in `I` with degree ≤ `n`. -/
def leading_coeff_nth (n : ℕ) : ideal R :=
(I.degree_le n).map $ lcoeff R n
theorem mem_leading_coeff_nth (n : ℕ) (x) :
x ∈ I.leading_coeff_nth n ↔ ∃ p ∈ I, degree p ≤ n ∧ leading_coeff p = x :=
begin
simp only [leading_coeff_nth, degree_le, submodule.mem_map, lcoeff_apply, submodule.mem_inf, mem_degree_le],
split,
{ rintro ⟨p, ⟨hpdeg, hpI⟩, rfl⟩,
cases lt_or_eq_of_le hpdeg with hpdeg hpdeg,
{ refine ⟨0, I.zero_mem, bot_le, _⟩,
rw [leading_coeff_zero, eq_comm],
exact coeff_eq_zero_of_degree_lt hpdeg },
{ refine ⟨p, hpI, le_of_eq hpdeg, _⟩,
rw [leading_coeff, nat_degree, hpdeg], refl } },
{ rintro ⟨p, hpI, hpdeg, rfl⟩,
have : nat_degree p + (n - nat_degree p) = n,
{ exact nat.add_sub_cancel' (nat_degree_le_of_degree_le hpdeg) },
refine ⟨p * X ^ (n - nat_degree p), ⟨_, I.mul_mem_right hpI⟩, _⟩,
{ apply le_trans (degree_mul_le _ _) _,
apply le_trans (add_le_add (degree_le_nat_degree) (degree_X_pow_le _)) _,
rw [← with_bot.coe_add, this],
exact le_refl _ },
{ rw [leading_coeff, ← coeff_mul_X_pow p (n - nat_degree p), this] } }
end
theorem mem_leading_coeff_nth_zero (x) :
x ∈ I.leading_coeff_nth 0 ↔ C x ∈ I :=
(mem_leading_coeff_nth _ _ _).trans
⟨λ ⟨p, hpI, hpdeg, hpx⟩, by rwa [← hpx, leading_coeff,
nat.eq_zero_of_le_zero (nat_degree_le_of_degree_le hpdeg),
← eq_C_of_degree_le_zero hpdeg],
λ hx, ⟨C x, hx, degree_C_le, leading_coeff_C x⟩⟩
theorem leading_coeff_nth_mono {m n : ℕ} (H : m ≤ n) :
I.leading_coeff_nth m ≤ I.leading_coeff_nth n :=
begin
intros r hr,
simp only [submodule.mem_coe, mem_leading_coeff_nth] at hr ⊢,
rcases hr with ⟨p, hpI, hpdeg, rfl⟩,
refine ⟨p * X ^ (n - m), I.mul_mem_right hpI, _, leading_coeff_mul_X_pow⟩,
refine le_trans (degree_mul_le _ _) _,
refine le_trans (add_le_add hpdeg (degree_X_pow_le _)) _,
rw [← with_bot.coe_add, nat.add_sub_cancel' H],
exact le_refl _
end
/-- Given an ideal `I` in `R[X]`, make the ideal in `R` of the
leading coefficients in `I`. -/
def leading_coeff : ideal R :=
⨆ n : ℕ, I.leading_coeff_nth n
theorem mem_leading_coeff (x) :
x ∈ I.leading_coeff ↔ ∃ p ∈ I, polynomial.leading_coeff p = x :=
begin
rw [leading_coeff, submodule.mem_supr_of_directed],
simp only [mem_leading_coeff_nth],
{ split, { rintro ⟨i, p, hpI, hpdeg, rfl⟩, exact ⟨p, hpI, rfl⟩ },
rintro ⟨p, hpI, rfl⟩, exact ⟨nat_degree p, p, hpI, degree_le_nat_degree, rfl⟩ },
intros i j, exact ⟨i + j, I.leading_coeff_nth_mono (nat.le_add_right _ _),
I.leading_coeff_nth_mono (nat.le_add_left _ _)⟩
end
theorem is_fg_degree_le [is_noetherian_ring R] (n : ℕ) :
submodule.fg (I.degree_le n) :=
is_noetherian_submodule_left.1 (is_noetherian_of_fg_of_noetherian _
⟨_, degree_le_eq_span_X_pow.symm⟩) _
end ideal
/-- Hilbert basis theorem: a polynomial ring over a noetherian ring is a noetherian ring. -/
protected theorem polynomial.is_noetherian_ring [is_noetherian_ring R] :
is_noetherian_ring (polynomial R) :=
⟨assume I : ideal (polynomial R),
let L := I.leading_coeff in
let M := well_founded.min (is_noetherian_iff_well_founded.1 (by apply_instance))
(set.range I.leading_coeff_nth) ⟨_, ⟨0, rfl⟩⟩ in
have hm : M ∈ set.range I.leading_coeff_nth := well_founded.min_mem _ _ _,
let ⟨N, HN⟩ := hm, ⟨s, hs⟩ := I.is_fg_degree_le N in
have hm2 : ∀ k, I.leading_coeff_nth k ≤ M := λ k, or.cases_on (le_or_lt k N)
(λ h, HN ▸ I.leading_coeff_nth_mono h)
(λ h x hx, classical.by_contradiction $ λ hxm,
have ¬M < I.leading_coeff_nth k, by refine well_founded.not_lt_min
(well_founded_submodule_gt _ _) _ _ _; exact ⟨k, rfl⟩,
this ⟨HN ▸ I.leading_coeff_nth_mono (le_of_lt h), λ H, hxm (H hx)⟩),
have hs2 : ∀ {x}, x ∈ I.degree_le N → x ∈ ideal.span (↑s : set (polynomial R)),
from hs ▸ λ x hx, submodule.span_induction hx (λ _ hx, ideal.subset_span hx) (ideal.zero_mem _)
(λ _ _, ideal.add_mem _) (λ c f hf, f.C_mul' c ▸ ideal.mul_mem_left _ hf),
⟨s, le_antisymm (ideal.span_le.2 $ λ x hx, have x ∈ I.degree_le N, from hs ▸ submodule.subset_span hx, this.2) $ begin
change I ≤ ideal.span ↑s,
intros p hp, generalize hn : p.nat_degree = k,
induction k using nat.strong_induction_on with k ih generalizing p,
cases le_or_lt k N,
{ subst k, refine hs2 ⟨polynomial.mem_degree_le.2
(le_trans polynomial.degree_le_nat_degree $ with_bot.coe_le_coe.2 h), hp⟩ },
{ have hp0 : p ≠ 0,
{ rintro rfl, cases hn, exact nat.not_lt_zero _ h },
have : (0 : R) ≠ 1,
{ intro h, apply hp0, ext i, refine (mul_one _).symm.trans _,
rw [← h, mul_zero], refl },
haveI : nontrivial R := ⟨⟨0, 1, this⟩⟩,
have : p.leading_coeff ∈ I.leading_coeff_nth N,
{ rw HN, exact hm2 k ((I.mem_leading_coeff_nth _ _).2
⟨_, hp, hn ▸ polynomial.degree_le_nat_degree, rfl⟩) },
rw I.mem_leading_coeff_nth at this,
rcases this with ⟨q, hq, hdq, hlqp⟩,
have hq0 : q ≠ 0,
{ intro H, rw [← polynomial.leading_coeff_eq_zero] at H,
rw [hlqp, polynomial.leading_coeff_eq_zero] at H, exact hp0 H },
have h1 : p.degree = (q * polynomial.X ^ (k - q.nat_degree)).degree,
{ rw [polynomial.degree_mul_eq', polynomial.degree_X_pow],
rw [polynomial.degree_eq_nat_degree hp0, polynomial.degree_eq_nat_degree hq0],
rw [← with_bot.coe_add, nat.add_sub_cancel', hn],
{ refine le_trans (polynomial.nat_degree_le_of_degree_le hdq) (le_of_lt h) },
rw [polynomial.leading_coeff_X_pow, mul_one],
exact mt polynomial.leading_coeff_eq_zero.1 hq0 },
have h2 : p.leading_coeff = (q * polynomial.X ^ (k - q.nat_degree)).leading_coeff,
{ rw [← hlqp, polynomial.leading_coeff_mul_X_pow] },
have := polynomial.degree_sub_lt h1 hp0 h2,
rw [polynomial.degree_eq_nat_degree hp0] at this,
rw ← sub_add_cancel p (q * polynomial.X ^ (k - q.nat_degree)),
refine (ideal.span ↑s).add_mem _ ((ideal.span ↑s).mul_mem_right _),
{ by_cases hpq : p - q * polynomial.X ^ (k - q.nat_degree) = 0,
{ rw hpq, exact ideal.zero_mem _ },
refine ih _ _ (I.sub_mem hp (I.mul_mem_right hq)) rfl,
rwa [polynomial.degree_eq_nat_degree hpq, with_bot.coe_lt_coe, hn] at this },
exact hs2 ⟨polynomial.mem_degree_le.2 hdq, hq⟩ }
end⟩⟩
attribute [instance] polynomial.is_noetherian_ring
namespace polynomial
theorem exists_irreducible_of_degree_pos {R : Type u} [integral_domain R] [is_noetherian_ring R]
{f : polynomial R} (hf : 0 < f.degree) : ∃ g, irreducible g ∧ g ∣ f :=
is_noetherian_ring.exists_irreducible_factor
(λ huf, ne_of_gt hf $ degree_eq_zero_of_is_unit huf)
(λ hf0, not_lt_of_lt hf $ hf0.symm ▸ (@degree_zero R _).symm ▸ with_bot.bot_lt_coe _)
theorem exists_irreducible_of_nat_degree_pos {R : Type u} [integral_domain R] [is_noetherian_ring R]
{f : polynomial R} (hf : 0 < f.nat_degree) : ∃ g, irreducible g ∧ g ∣ f :=
exists_irreducible_of_degree_pos $ by { contrapose! hf, exact nat_degree_le_of_degree_le hf }
theorem exists_irreducible_of_nat_degree_ne_zero {R : Type u} [integral_domain R] [is_noetherian_ring R]
{f : polynomial R} (hf : f.nat_degree ≠ 0) : ∃ g, irreducible g ∧ g ∣ f :=
exists_irreducible_of_nat_degree_pos $ nat.pos_of_ne_zero hf
end polynomial
namespace mv_polynomial
lemma is_noetherian_ring_fin_0 [is_noetherian_ring R] :
is_noetherian_ring (mv_polynomial (fin 0) R) :=
is_noetherian_ring_of_ring_equiv R
((mv_polynomial.pempty_ring_equiv R).symm.trans
(mv_polynomial.ring_equiv_of_equiv _ fin_zero_equiv'.symm))
theorem is_noetherian_ring_fin [is_noetherian_ring R] :
∀ {n : ℕ}, is_noetherian_ring (mv_polynomial (fin n) R)
| 0 := is_noetherian_ring_fin_0
| (n+1) :=
@is_noetherian_ring_of_ring_equiv (polynomial (mv_polynomial (fin n) R)) _ _ _
(mv_polynomial.fin_succ_equiv _ n).symm
(@polynomial.is_noetherian_ring (mv_polynomial (fin n) R) _ (is_noetherian_ring_fin))
/-- The multivariate polynomial ring in finitely many variables over a noetherian ring
is itself a noetherian ring. -/
instance is_noetherian_ring [fintype σ] [is_noetherian_ring R] :
is_noetherian_ring (mv_polynomial σ R) :=
trunc.induction_on (fintype.equiv_fin σ) $ λ e,
@is_noetherian_ring_of_ring_equiv (mv_polynomial (fin (fintype.card σ)) R) _ _ _
(mv_polynomial.ring_equiv_of_equiv _ e.symm) is_noetherian_ring_fin
lemma is_integral_domain_fin_zero (R : Type u) [comm_ring R] (hR : is_integral_domain R) :
is_integral_domain (mv_polynomial (fin 0) R) :=
ring_equiv.is_integral_domain R hR
((ring_equiv_of_equiv R fin_zero_equiv').trans (mv_polynomial.pempty_ring_equiv R))
/-- Auxilliary lemma:
Multivariate polynomials over an integral domain
with variables indexed by `fin n` form an integral domain.
This fact is proven inductively,
and then used to prove the general case without any finiteness hypotheses.
See `mv_polynomial.integral_domain` for the general case. -/
lemma is_integral_domain_fin (R : Type u) [comm_ring R] (hR : is_integral_domain R) :
∀ (n : ℕ), is_integral_domain (mv_polynomial (fin n) R)
| 0 := is_integral_domain_fin_zero R hR
| (n+1) :=
ring_equiv.is_integral_domain
(polynomial (mv_polynomial (fin n) R))
(is_integral_domain_fin n).polynomial
(mv_polynomial.fin_succ_equiv _ n)
lemma is_integral_domain_fintype (R : Type u) (σ : Type v) [comm_ring R] [fintype σ]
(hR : is_integral_domain R) : is_integral_domain (mv_polynomial σ R) :=
trunc.induction_on (fintype.equiv_fin σ) $ λ e,
@ring_equiv.is_integral_domain _ (mv_polynomial (fin $ fintype.card σ) R) _ _
(mv_polynomial.is_integral_domain_fin _ hR _)
(ring_equiv_of_equiv R e)
/-- Auxilliary definition:
Multivariate polynomials in finitely many variables over an integral domain form an integral domain.
This fact is proven by transport of structure from the `mv_polynomial.integral_domain_fin`,
and then used to prove the general case without finiteness hypotheses.
See `mv_polynomial.integral_domain` for the general case. -/
def integral_domain_fintype (R : Type u) (σ : Type v) [integral_domain R] [fintype σ] :
integral_domain (mv_polynomial σ R) :=
@is_integral_domain.to_integral_domain _ _ $ mv_polynomial.is_integral_domain_fintype R σ $
integral_domain.to_is_integral_domain R
protected theorem eq_zero_or_eq_zero_of_mul_eq_zero {R : Type u} [integral_domain R] {σ : Type v}
(p q : mv_polynomial σ R) (h : p * q = 0) : p = 0 ∨ q = 0 :=
begin
obtain ⟨s, p, rfl⟩ := exists_finset_rename p,
obtain ⟨t, q, rfl⟩ := exists_finset_rename q,
have : p.rename (subtype.map id (finset.subset_union_left s t) : {x // x ∈ s} → {x // x ∈ s ∪ t}) *
q.rename (subtype.map id (finset.subset_union_right s t)) = 0,
{ apply rename_injective _ subtype.val_injective, simpa using h },
letI := mv_polynomial.integral_domain_fintype R {x // x ∈ (s ∪ t)},
rw mul_eq_zero at this,
cases this; [left, right],
all_goals { simpa using congr_arg (rename subtype.val) this }
end
/-- The multivariate polynomial ring over an integral domain is an integral domain. -/
instance {R : Type u} {σ : Type v} [integral_domain R] :
integral_domain (mv_polynomial σ R) :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := mv_polynomial.eq_zero_or_eq_zero_of_mul_eq_zero,
exists_pair_ne := ⟨0, 1, λ H,
begin
have : eval₂ id (λ s, (0:R)) (0 : mv_polynomial σ R) =
eval₂ id (λ s, (0:R)) (1 : mv_polynomial σ R),
{ congr, exact H },
simpa,
end⟩,
.. (by apply_instance : comm_ring (mv_polynomial σ R)) }
end mv_polynomial
|
8b00cf1a4e96cd9fcaff710e0ec39a90eb3254cf | 94e33a31faa76775069b071adea97e86e218a8ee | /src/analysis/normed_space/multilinear.lean | 6c75986eeba0e3e2e399802506a1a906a5e43a3b | [
"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 | 72,231 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import analysis.normed_space.operator_norm
import topology.algebra.module.multilinear
/-!
# Operator norm on the space of continuous multilinear maps
When `f` is a continuous multilinear map in finitely many variables, we define its norm `∥f∥` as the
smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`.
We show that it is indeed a norm, and prove its basic properties.
## Main results
Let `f` be a multilinear map in finitely many variables.
* `exists_bound_of_continuous` asserts that, if `f` is continuous, then there exists `C > 0`
with `∥f m∥ ≤ C * ∏ i, ∥m i∥` for all `m`.
* `continuous_of_bound`, conversely, asserts that this bound implies continuity.
* `mk_continuous` constructs the associated continuous multilinear map.
Let `f` be a continuous multilinear map in finitely many variables.
* `∥f∥` is its norm, i.e., the smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for
all `m`.
* `le_op_norm f m` asserts the fundamental inequality `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥`.
* `norm_image_sub_le f m₁ m₂` gives a control of the difference `f m₁ - f m₂` in terms of
`∥f∥` and `∥m₁ - m₂∥`.
We also register isomorphisms corresponding to currying or uncurrying variables, transforming a
continuous multilinear function `f` in `n+1` variables into a continuous linear function taking
values in continuous multilinear functions in `n` variables, and also into a continuous multilinear
function in `n` variables taking values in continuous linear functions. These operations are called
`f.curry_left` and `f.curry_right` respectively (with inverses `f.uncurry_left` and
`f.uncurry_right`). They induce continuous linear equivalences between spaces of
continuous multilinear functions in `n+1` variables and spaces of continuous linear functions into
continuous multilinear functions in `n` variables (resp. continuous multilinear functions in `n`
variables taking values in continuous linear functions), called respectively
`continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`.
## Implementation notes
We mostly follow the API (and the proofs) of `operator_norm.lean`, with the additional complexity
that we should deal with multilinear maps in several variables. The currying/uncurrying
constructions are based on those in `multilinear.lean`.
From the mathematical point of view, all the results follow from the results on operator norm in
one variable, by applying them to one variable after the other through currying. However, this
is only well defined when there is an order on the variables (for instance on `fin n`) although
the final result is independent of the order. While everything could be done following this
approach, it turns out that direct proofs are easier and more efficient.
-/
noncomputable theory
open_locale classical big_operators nnreal
open finset metric
local attribute [instance, priority 1001]
add_comm_group.to_add_comm_monoid normed_group.to_add_comm_group normed_space.to_module'
-- hack to speed up simp when dealing with complicated types
local attribute [-instance] unique.subsingleton pi.subsingleton
/-!
### Type variables
We use the following type variables in this file:
* `𝕜` : a `nondiscrete_normed_field`;
* `ι`, `ι'` : finite index types with decidable equality;
* `E`, `E₁` : families of normed vector spaces over `𝕜` indexed by `i : ι`;
* `E'` : a family of normed vector spaces over `𝕜` indexed by `i' : ι'`;
* `Ei` : a family of normed vector spaces over `𝕜` indexed by `i : fin (nat.succ n)`;
* `G`, `G'` : normed vector spaces over `𝕜`.
-/
universes u v v' wE wE₁ wE' wEi wG wG'
variables {𝕜 : Type u} {ι : Type v} {ι' : Type v'} {n : ℕ}
{E : ι → Type wE} {E₁ : ι → Type wE₁} {E' : ι' → Type wE'} {Ei : fin n.succ → Type wEi}
{G : Type wG} {G' : Type wG'}
[decidable_eq ι] [fintype ι] [decidable_eq ι'] [fintype ι'] [nondiscrete_normed_field 𝕜]
[Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)]
[Π i, normed_group (E₁ i)] [Π i, normed_space 𝕜 (E₁ i)]
[Π i, normed_group (E' i)] [Π i, normed_space 𝕜 (E' i)]
[Π i, normed_group (Ei i)] [Π i, normed_space 𝕜 (Ei i)]
[normed_group G] [normed_space 𝕜 G] [normed_group G'] [normed_space 𝕜 G']
/-!
### Continuity properties of multilinear maps
We relate continuity of multilinear maps to the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, in
both directions. Along the way, we prove useful bounds on the difference `∥f m₁ - f m₂∥`.
-/
namespace multilinear_map
variable (f : multilinear_map 𝕜 E G)
/-- If a multilinear map in finitely many variables on normed spaces satisfies the inequality
`∥f m∥ ≤ C * ∏ i, ∥m i∥` on a shell `ε i / ∥c i∥ < ∥m i∥ < ε i` for some positive numbers `ε i`
and elements `c i : 𝕜`, `1 < ∥c i∥`, then it satisfies this inequality for all `m`. -/
lemma bound_of_shell {ε : ι → ℝ} {C : ℝ} (hε : ∀ i, 0 < ε i) {c : ι → 𝕜} (hc : ∀ i, 1 < ∥c i∥)
(hf : ∀ m : Π i, E i, (∀ i, ε i / ∥c i∥ ≤ ∥m i∥) → (∀ i, ∥m i∥ < ε i) → ∥f m∥ ≤ C * ∏ i, ∥m i∥)
(m : Π i, E i) : ∥f m∥ ≤ C * ∏ i, ∥m i∥ :=
begin
rcases em (∃ i, m i = 0) with ⟨i, hi⟩|hm; [skip, push_neg at hm],
{ simp [f.map_coord_zero i hi, prod_eq_zero (mem_univ i), hi] },
choose δ hδ0 hδm_lt hle_δm hδinv using λ i, rescale_to_shell (hc i) (hε i) (hm i),
have hδ0 : 0 < ∏ i, ∥δ i∥, from prod_pos (λ i _, norm_pos_iff.2 (hδ0 i)),
simpa [map_smul_univ, norm_smul, prod_mul_distrib, mul_left_comm C, mul_le_mul_left hδ0]
using hf (λ i, δ i • m i) hle_δm hδm_lt,
end
/-- If a multilinear map in finitely many variables on normed spaces is continuous, then it
satisfies the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, for some `C` which can be chosen to be
positive. -/
theorem exists_bound_of_continuous (hf : continuous f) :
∃ (C : ℝ), 0 < C ∧ (∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) :=
begin
casesI is_empty_or_nonempty ι,
{ refine ⟨∥f 0∥ + 1, add_pos_of_nonneg_of_pos (norm_nonneg _) zero_lt_one, λ m, _⟩,
obtain rfl : m = 0, from funext (is_empty.elim ‹_›),
simp [univ_eq_empty, zero_le_one] },
obtain ⟨ε : ℝ, ε0 : 0 < ε, hε : ∀ m : Π i, E i, ∥m - 0∥ < ε → ∥f m - f 0∥ < 1⟩ :=
normed_group.tendsto_nhds_nhds.1 (hf.tendsto 0) 1 zero_lt_one,
simp only [sub_zero, f.map_zero] at hε,
rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩,
have : 0 < (∥c∥ / ε) ^ fintype.card ι, from pow_pos (div_pos (zero_lt_one.trans hc) ε0) _,
refine ⟨_, this, _⟩,
refine f.bound_of_shell (λ _, ε0) (λ _, hc) (λ m hcm hm, _),
refine (hε m ((pi_norm_lt_iff ε0).2 hm)).le.trans _,
rw [← div_le_iff' this, one_div, ← inv_pow, inv_div, fintype.card, ← prod_const],
exact prod_le_prod (λ _ _, div_nonneg ε0.le (norm_nonneg _)) (λ i _, hcm i)
end
/-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂`
using the multilinearity. Here, we give a precise but hard to use version. See
`norm_image_sub_le_of_bound` for a less precise but more usable version. The bound reads
`∥f m - f m'∥ ≤
C * ∥m 1 - m' 1∥ * max ∥m 2∥ ∥m' 2∥ * max ∥m 3∥ ∥m' 3∥ * ... * max ∥m n∥ ∥m' n∥ + ...`,
where the other terms in the sum are the same products where `1` is replaced by any `i`. -/
lemma norm_image_sub_le_of_bound' {C : ℝ} (hC : 0 ≤ C)
(H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m₁ m₂ : Πi, E i) :
∥f m₁ - f m₂∥ ≤
C * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ :=
begin
have A : ∀(s : finset ι), ∥f m₁ - f (s.piecewise m₂ m₁)∥
≤ C * ∑ i in s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥,
{ refine finset.induction (by simp) _,
assume i s his Hrec,
have I : ∥f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)∥
≤ C * ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥,
{ have A : ((insert i s).piecewise m₂ m₁)
= function.update (s.piecewise m₂ m₁) i (m₂ i) := s.piecewise_insert _ _ _,
have B : s.piecewise m₂ m₁ = function.update (s.piecewise m₂ m₁) i (m₁ i),
{ ext j,
by_cases h : j = i,
{ rw h, simp [his] },
{ simp [h] } },
rw [B, A, ← f.map_sub],
apply le_trans (H _) (mul_le_mul_of_nonneg_left _ hC),
refine prod_le_prod (λj hj, norm_nonneg _) (λj hj, _),
by_cases h : j = i,
{ rw h, simp },
{ by_cases h' : j ∈ s;
simp [h', h, le_refl] } },
calc ∥f m₁ - f ((insert i s).piecewise m₂ m₁)∥ ≤
∥f m₁ - f (s.piecewise m₂ m₁)∥ + ∥f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)∥ :
by { rw [← dist_eq_norm, ← dist_eq_norm, ← dist_eq_norm], exact dist_triangle _ _ _ }
... ≤ (C * ∑ i in s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥)
+ C * ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ :
add_le_add Hrec I
... = C * ∑ i in insert i s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ :
by simp [his, add_comm, left_distrib] },
convert A univ,
simp
end
/-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂`
using the multilinearity. Here, we give a usable but not very precise version. See
`norm_image_sub_le_of_bound'` for a more precise but less usable version. The bound is
`∥f m - f m'∥ ≤ C * card ι * ∥m - m'∥ * (max ∥m∥ ∥m'∥) ^ (card ι - 1)`. -/
lemma norm_image_sub_le_of_bound {C : ℝ} (hC : 0 ≤ C)
(H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m₁ m₂ : Πi, E i) :
∥f m₁ - f m₂∥ ≤ C * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ :=
begin
have A : ∀ (i : ι), ∏ j, (if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥)
≤ ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1),
{ assume i,
calc ∏ j, (if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥)
≤ ∏ j : ι, function.update (λ j, max ∥m₁∥ ∥m₂∥) i (∥m₁ - m₂∥) j :
begin
apply prod_le_prod,
{ assume j hj, by_cases h : j = i; simp [h, norm_nonneg] },
{ assume j hj,
by_cases h : j = i,
{ rw h, simp, exact norm_le_pi_norm (m₁ - m₂) i },
{ simp [h, max_le_max, norm_le_pi_norm (_ : Π i, E i)] } }
end
... = ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) :
by { rw prod_update_of_mem (finset.mem_univ _), simp [card_univ_diff] } },
calc
∥f m₁ - f m₂∥
≤ C * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ :
f.norm_image_sub_le_of_bound' hC H m₁ m₂
... ≤ C * ∑ i, ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) :
mul_le_mul_of_nonneg_left (sum_le_sum (λi hi, A i)) hC
... = C * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ :
by { rw [sum_const, card_univ, nsmul_eq_mul], ring }
end
/-- If a multilinear map satisfies an inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, then it is
continuous. -/
theorem continuous_of_bound (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) :
continuous f :=
begin
let D := max C 1,
have D_pos : 0 ≤ D := le_trans zero_le_one (le_max_right _ _),
replace H : ∀ m, ∥f m∥ ≤ D * ∏ i, ∥m i∥,
{ assume m,
apply le_trans (H m) (mul_le_mul_of_nonneg_right (le_max_left _ _) _),
exact prod_nonneg (λ(i : ι) hi, norm_nonneg (m i)) },
refine continuous_iff_continuous_at.2 (λm, _),
refine continuous_at_of_locally_lipschitz zero_lt_one
(D * (fintype.card ι) * (∥m∥ + 1) ^ (fintype.card ι - 1)) (λm' h', _),
rw [dist_eq_norm, dist_eq_norm],
have : 0 ≤ (max ∥m'∥ ∥m∥), by simp,
have : (max ∥m'∥ ∥m∥) ≤ ∥m∥ + 1,
by simp [zero_le_one, norm_le_of_mem_closed_ball (le_of_lt h'), -add_comm],
calc
∥f m' - f m∥
≤ D * (fintype.card ι) * (max ∥m'∥ ∥m∥) ^ (fintype.card ι - 1) * ∥m' - m∥ :
f.norm_image_sub_le_of_bound D_pos H m' m
... ≤ D * (fintype.card ι) * (∥m∥ + 1) ^ (fintype.card ι - 1) * ∥m' - m∥ :
by apply_rules [mul_le_mul_of_nonneg_right, mul_le_mul_of_nonneg_left, mul_nonneg,
norm_nonneg, nat.cast_nonneg, pow_le_pow_of_le_left]
end
/-- Constructing a continuous multilinear map from a multilinear map satisfying a boundedness
condition. -/
def mk_continuous (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) :
continuous_multilinear_map 𝕜 E G :=
{ cont := f.continuous_of_bound C H, ..f }
@[simp] lemma coe_mk_continuous (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) :
⇑(f.mk_continuous C H) = f :=
rfl
/-- Given a multilinear map in `n` variables, if one restricts it to `k` variables putting `z` on
the other coordinates, then the resulting restricted function satisfies an inequality
`∥f.restr v∥ ≤ C * ∥z∥^(n-k) * Π ∥v i∥` if the original function satisfies `∥f v∥ ≤ C * Π ∥v i∥`. -/
lemma restr_norm_le {k n : ℕ} (f : (multilinear_map 𝕜 (λ i : fin n, G) G' : _))
(s : finset (fin n)) (hk : s.card = k) (z : G) {C : ℝ}
(H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (v : fin k → G) :
∥f.restr s hk z v∥ ≤ C * ∥z∥ ^ (n - k) * ∏ i, ∥v i∥ :=
begin
rw [mul_right_comm, mul_assoc],
convert H _ using 2,
simp only [apply_dite norm, fintype.prod_dite, prod_const (∥z∥), finset.card_univ,
fintype.card_of_subtype sᶜ (λ x, mem_compl), card_compl, fintype.card_fin, hk, mk_coe,
← (s.order_iso_of_fin hk).symm.bijective.prod_comp (λ x, ∥v x∥)],
refl
end
end multilinear_map
/-!
### Continuous multilinear maps
We define the norm `∥f∥` of a continuous multilinear map `f` in finitely many variables as the
smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`. We show that this
defines a normed space structure on `continuous_multilinear_map 𝕜 E G`.
-/
namespace continuous_multilinear_map
variables (c : 𝕜) (f g : continuous_multilinear_map 𝕜 E G) (m : Πi, E i)
theorem bound : ∃ (C : ℝ), 0 < C ∧ (∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) :=
f.to_multilinear_map.exists_bound_of_continuous f.2
open real
/-- The operator norm of a continuous multilinear map is the inf of all its bounds. -/
def op_norm := Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥}
instance has_op_norm : has_norm (continuous_multilinear_map 𝕜 E G) := ⟨op_norm⟩
/-- An alias of `continuous_multilinear_map.has_op_norm` with non-dependent types to help typeclass
search. -/
instance has_op_norm' : has_norm (continuous_multilinear_map 𝕜 (λ (i : ι), G) G') :=
continuous_multilinear_map.has_op_norm
lemma norm_def : ∥f∥ = Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} := rfl
-- So that invocations of `le_cInf` make sense: we show that the set of
-- bounds is nonempty and bounded below.
lemma bounds_nonempty {f : continuous_multilinear_map 𝕜 E G} :
∃ c, c ∈ {c | 0 ≤ c ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} :=
let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩
lemma bounds_bdd_below {f : continuous_multilinear_map 𝕜 E G} :
bdd_below {c | 0 ≤ c ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} :=
⟨0, λ _ ⟨hn, _⟩, hn⟩
lemma op_norm_nonneg : 0 ≤ ∥f∥ :=
le_cInf bounds_nonempty (λ _ ⟨hx, _⟩, hx)
/-- The fundamental property of the operator norm of a continuous multilinear map:
`∥f m∥` is bounded by `∥f∥` times the product of the `∥m i∥`. -/
theorem le_op_norm : ∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥ :=
begin
have A : 0 ≤ ∏ i, ∥m i∥ := prod_nonneg (λj hj, norm_nonneg _),
cases A.eq_or_lt with h hlt,
{ rcases prod_eq_zero_iff.1 h.symm with ⟨i, _, hi⟩,
rw norm_eq_zero at hi,
have : f m = 0 := f.map_coord_zero i hi,
rw [this, norm_zero],
exact mul_nonneg (op_norm_nonneg f) A },
{ rw [← div_le_iff hlt],
apply le_cInf bounds_nonempty,
rintro c ⟨_, hc⟩, rw [div_le_iff hlt], apply hc }
end
theorem le_of_op_norm_le {C : ℝ} (h : ∥f∥ ≤ C) : ∥f m∥ ≤ C * ∏ i, ∥m i∥ :=
(f.le_op_norm m).trans $ mul_le_mul_of_nonneg_right h (prod_nonneg $ λ i _, norm_nonneg (m i))
lemma ratio_le_op_norm : ∥f m∥ / ∏ i, ∥m i∥ ≤ ∥f∥ :=
div_le_of_nonneg_of_le_mul (prod_nonneg $ λ i _, norm_nonneg _) (op_norm_nonneg _) (f.le_op_norm m)
/-- The image of the unit ball under a continuous multilinear map is bounded. -/
lemma unit_le_op_norm (h : ∥m∥ ≤ 1) : ∥f m∥ ≤ ∥f∥ :=
calc
∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥ : f.le_op_norm m
... ≤ ∥f∥ * ∏ i : ι, 1 :
mul_le_mul_of_nonneg_left (prod_le_prod (λi hi, norm_nonneg _)
(λi hi, le_trans (norm_le_pi_norm (_ : Π i, E i) _) h)) (op_norm_nonneg f)
... = ∥f∥ : by simp
/-- If one controls the norm of every `f x`, then one controls the norm of `f`. -/
lemma op_norm_le_bound {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ m, ∥f m∥ ≤ M * ∏ i, ∥m i∥) :
∥f∥ ≤ M :=
cInf_le bounds_bdd_below ⟨hMp, hM⟩
/-- The operator norm satisfies the triangle inequality. -/
theorem op_norm_add_le : ∥f + g∥ ≤ ∥f∥ + ∥g∥ :=
cInf_le bounds_bdd_below
⟨add_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x, by { rw add_mul,
exact norm_add_le_of_le (le_op_norm _ _) (le_op_norm _ _) }⟩
/-- A continuous linear map is zero iff its norm vanishes. -/
theorem op_norm_zero_iff : ∥f∥ = 0 ↔ f = 0 :=
begin
split,
{ assume h,
ext m,
simpa [h] using f.le_op_norm m },
{ rintro rfl,
apply le_antisymm (op_norm_le_bound 0 le_rfl (λm, _)) (op_norm_nonneg _),
simp }
end
section
variables {𝕜' : Type*} [normed_field 𝕜'] [normed_space 𝕜' G] [smul_comm_class 𝕜 𝕜' G]
lemma op_norm_smul_le (c : 𝕜') : ∥c • f∥ ≤ ∥c∥ * ∥f∥ :=
(c • f).op_norm_le_bound
(mul_nonneg (norm_nonneg _) (op_norm_nonneg _))
begin
intro m,
erw [norm_smul, mul_assoc],
exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _)
end
lemma op_norm_neg : ∥-f∥ = ∥f∥ := by { rw norm_def, apply congr_arg, ext, simp }
/-- Continuous multilinear maps themselves form a normed space with respect to
the operator norm. -/
instance normed_group : normed_group (continuous_multilinear_map 𝕜 E G) :=
normed_group.of_core _ ⟨op_norm_zero_iff, op_norm_add_le, op_norm_neg⟩
/-- An alias of `continuous_multilinear_map.normed_group` with non-dependent types to help typeclass
search. -/
instance normed_group' : normed_group (continuous_multilinear_map 𝕜 (λ i : ι, G) G') :=
continuous_multilinear_map.normed_group
instance normed_space : normed_space 𝕜' (continuous_multilinear_map 𝕜 E G) :=
⟨λ c f, f.op_norm_smul_le c⟩
/-- An alias of `continuous_multilinear_map.normed_space` with non-dependent types to help typeclass
search. -/
instance normed_space' : normed_space 𝕜' (continuous_multilinear_map 𝕜 (λ i : ι, G') G) :=
continuous_multilinear_map.normed_space
theorem le_op_norm_mul_prod_of_le {b : ι → ℝ} (hm : ∀ i, ∥m i∥ ≤ b i) : ∥f m∥ ≤ ∥f∥ * ∏ i, b i :=
(f.le_op_norm m).trans $ mul_le_mul_of_nonneg_left
(prod_le_prod (λ _ _, norm_nonneg _) (λ i _, hm i)) (norm_nonneg f)
theorem le_op_norm_mul_pow_card_of_le {b : ℝ} (hm : ∀ i, ∥m i∥ ≤ b) :
∥f m∥ ≤ ∥f∥ * b ^ fintype.card ι :=
by simpa only [prod_const] using f.le_op_norm_mul_prod_of_le m hm
theorem le_op_norm_mul_pow_of_le {Ei : fin n → Type*} [Π i, normed_group (Ei i)]
[Π i, normed_space 𝕜 (Ei i)] (f : continuous_multilinear_map 𝕜 Ei G) (m : Π i, Ei i)
{b : ℝ} (hm : ∥m∥ ≤ b) :
∥f m∥ ≤ ∥f∥ * b ^ n :=
by simpa only [fintype.card_fin]
using f.le_op_norm_mul_pow_card_of_le m (λ i, (norm_le_pi_norm m i).trans hm)
/-- The fundamental property of the operator norm of a continuous multilinear map:
`∥f m∥` is bounded by `∥f∥` times the product of the `∥m i∥`, `nnnorm` version. -/
theorem le_op_nnnorm : ∥f m∥₊ ≤ ∥f∥₊ * ∏ i, ∥m i∥₊ :=
nnreal.coe_le_coe.1 $ by { push_cast, exact f.le_op_norm m }
theorem le_of_op_nnnorm_le {C : ℝ≥0} (h : ∥f∥₊ ≤ C) : ∥f m∥₊ ≤ C * ∏ i, ∥m i∥₊ :=
(f.le_op_nnnorm m).trans $ mul_le_mul' h le_rfl
lemma op_norm_prod (f : continuous_multilinear_map 𝕜 E G) (g : continuous_multilinear_map 𝕜 E G') :
∥f.prod g∥ = max (∥f∥) (∥g∥) :=
le_antisymm
(op_norm_le_bound _ (norm_nonneg (f, g)) (λ m,
have H : 0 ≤ ∏ i, ∥m i∥, from prod_nonneg $ λ _ _, norm_nonneg _,
by simpa only [prod_apply, prod.norm_def, max_mul_of_nonneg, H]
using max_le_max (f.le_op_norm m) (g.le_op_norm m))) $
max_le
(f.op_norm_le_bound (norm_nonneg _) $ λ m, (le_max_left _ _).trans ((f.prod g).le_op_norm _))
(g.op_norm_le_bound (norm_nonneg _) $ λ m, (le_max_right _ _).trans ((f.prod g).le_op_norm _))
lemma norm_pi {ι' : Type v'} [fintype ι'] {E' : ι' → Type wE'} [Π i', normed_group (E' i')]
[Π i', normed_space 𝕜 (E' i')] (f : Π i', continuous_multilinear_map 𝕜 E (E' i')) :
∥pi f∥ = ∥f∥ :=
begin
apply le_antisymm,
{ refine (op_norm_le_bound _ (norm_nonneg f) (λ m, _)),
dsimp,
rw pi_norm_le_iff,
exacts [λ i, (f i).le_of_op_norm_le m (norm_le_pi_norm f i),
mul_nonneg (norm_nonneg f) (prod_nonneg $ λ _ _, norm_nonneg _)] },
{ refine (pi_norm_le_iff (norm_nonneg _)).2 (λ i, _),
refine (op_norm_le_bound _ (norm_nonneg _) (λ m, _)),
refine le_trans _ ((pi f).le_op_norm m),
convert norm_le_pi_norm (λ j, f j m) i }
end
section
variables (𝕜 E E' G G')
/-- `continuous_multilinear_map.prod` as a `linear_isometry_equiv`. -/
def prodL :
(continuous_multilinear_map 𝕜 E G) × (continuous_multilinear_map 𝕜 E G') ≃ₗᵢ[𝕜]
continuous_multilinear_map 𝕜 E (G × G') :=
{ to_fun := λ f, f.1.prod f.2,
inv_fun := λ f, ((continuous_linear_map.fst 𝕜 G G').comp_continuous_multilinear_map f,
(continuous_linear_map.snd 𝕜 G G').comp_continuous_multilinear_map f),
map_add' := λ f g, rfl,
map_smul' := λ c f, rfl,
left_inv := λ f, by ext; refl,
right_inv := λ f, by ext; refl,
norm_map' := λ f, op_norm_prod f.1 f.2 }
/-- `continuous_multilinear_map.pi` as a `linear_isometry_equiv`. -/
def piₗᵢ {ι' : Type v'} [fintype ι'] {E' : ι' → Type wE'} [Π i', normed_group (E' i')]
[Π i', normed_space 𝕜 (E' i')] :
@linear_isometry_equiv 𝕜 𝕜 _ _ (ring_hom.id 𝕜) _ _ _
(Π i', continuous_multilinear_map 𝕜 E (E' i')) (continuous_multilinear_map 𝕜 E (Π i, E' i)) _ _
(@pi.module ι' _ 𝕜 _ _ (λ i', infer_instance)) _ :=
{ to_linear_equiv :=
-- note: `pi_linear_equiv` does not unify correctly here, presumably due to issues with dependent
-- typeclass arguments.
{ map_add' := λ f g, rfl,
map_smul' := λ c f, rfl,
.. pi_equiv, },
norm_map' := norm_pi }
end
end
section restrict_scalars
variables {𝕜' : Type*} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜' 𝕜]
variables [normed_space 𝕜' G] [is_scalar_tower 𝕜' 𝕜 G]
variables [Π i, normed_space 𝕜' (E i)] [∀ i, is_scalar_tower 𝕜' 𝕜 (E i)]
@[simp] lemma norm_restrict_scalars : ∥f.restrict_scalars 𝕜'∥ = ∥f∥ :=
by simp only [norm_def, coe_restrict_scalars]
variable (𝕜')
/-- `continuous_multilinear_map.restrict_scalars` as a `continuous_multilinear_map`. -/
def restrict_scalars_linear :
continuous_multilinear_map 𝕜 E G →L[𝕜'] continuous_multilinear_map 𝕜' E G :=
linear_map.mk_continuous
{ to_fun := restrict_scalars 𝕜',
map_add' := λ m₁ m₂, rfl,
map_smul' := λ c m, rfl } 1 $ λ f, by simp
variable {𝕜'}
lemma continuous_restrict_scalars :
continuous (restrict_scalars 𝕜' : continuous_multilinear_map 𝕜 E G →
continuous_multilinear_map 𝕜' E G) :=
(restrict_scalars_linear 𝕜').continuous
end restrict_scalars
/-- The difference `f m₁ - f m₂` is controlled in terms of `∥f∥` and `∥m₁ - m₂∥`, precise version.
For a less precise but more usable version, see `norm_image_sub_le`. The bound reads
`∥f m - f m'∥ ≤
∥f∥ * ∥m 1 - m' 1∥ * max ∥m 2∥ ∥m' 2∥ * max ∥m 3∥ ∥m' 3∥ * ... * max ∥m n∥ ∥m' n∥ + ...`,
where the other terms in the sum are the same products where `1` is replaced by any `i`.-/
lemma norm_image_sub_le' (m₁ m₂ : Πi, E i) :
∥f m₁ - f m₂∥ ≤
∥f∥ * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ :=
f.to_multilinear_map.norm_image_sub_le_of_bound' (norm_nonneg _) f.le_op_norm _ _
/-- The difference `f m₁ - f m₂` is controlled in terms of `∥f∥` and `∥m₁ - m₂∥`, less precise
version. For a more precise but less usable version, see `norm_image_sub_le'`.
The bound is `∥f m - f m'∥ ≤ ∥f∥ * card ι * ∥m - m'∥ * (max ∥m∥ ∥m'∥) ^ (card ι - 1)`.-/
lemma norm_image_sub_le (m₁ m₂ : Πi, E i) :
∥f m₁ - f m₂∥ ≤ ∥f∥ * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ :=
f.to_multilinear_map.norm_image_sub_le_of_bound (norm_nonneg _) f.le_op_norm _ _
/-- Applying a multilinear map to a vector is continuous in both coordinates. -/
lemma continuous_eval :
continuous (λ p : continuous_multilinear_map 𝕜 E G × Π i, E i, p.1 p.2) :=
begin
apply continuous_iff_continuous_at.2 (λp, _),
apply continuous_at_of_locally_lipschitz zero_lt_one
((∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) + ∏ i, ∥p.2 i∥)
(λq hq, _),
have : 0 ≤ (max ∥q.2∥ ∥p.2∥), by simp,
have : 0 ≤ ∥p∥ + 1 := zero_le_one.trans ((le_add_iff_nonneg_left 1).2 $ norm_nonneg p),
have A : ∥q∥ ≤ ∥p∥ + 1 := norm_le_of_mem_closed_ball hq.le,
have : (max ∥q.2∥ ∥p.2∥) ≤ ∥p∥ + 1 :=
(max_le_max (norm_snd_le q) (norm_snd_le p)).trans (by simp [A, -add_comm, zero_le_one]),
have : ∀ (i : ι), i ∈ univ → 0 ≤ ∥p.2 i∥ := λ i hi, norm_nonneg _,
calc dist (q.1 q.2) (p.1 p.2)
≤ dist (q.1 q.2) (q.1 p.2) + dist (q.1 p.2) (p.1 p.2) : dist_triangle _ _ _
... = ∥q.1 q.2 - q.1 p.2∥ + ∥q.1 p.2 - p.1 p.2∥ : by rw [dist_eq_norm, dist_eq_norm]
... ≤ ∥q.1∥ * (fintype.card ι) * (max ∥q.2∥ ∥p.2∥) ^ (fintype.card ι - 1) * ∥q.2 - p.2∥
+ ∥q.1 - p.1∥ * ∏ i, ∥p.2 i∥ :
add_le_add (norm_image_sub_le _ _ _) ((q.1 - p.1).le_op_norm p.2)
... ≤ (∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) * ∥q - p∥
+ ∥q - p∥ * ∏ i, ∥p.2 i∥ :
by apply_rules [add_le_add, mul_le_mul, le_refl, le_trans (norm_fst_le q) A, nat.cast_nonneg,
mul_nonneg, pow_le_pow_of_le_left, pow_nonneg, norm_snd_le (q - p), norm_nonneg,
norm_fst_le (q - p), prod_nonneg]
... = ((∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1)
+ (∏ i, ∥p.2 i∥)) * dist q p : by { rw dist_eq_norm, ring }
end
lemma continuous_eval_left (m : Π i, E i) :
continuous (λ p : continuous_multilinear_map 𝕜 E G, p m) :=
continuous_eval.comp (continuous_id.prod_mk continuous_const)
lemma has_sum_eval
{α : Type*} {p : α → continuous_multilinear_map 𝕜 E G} {q : continuous_multilinear_map 𝕜 E G}
(h : has_sum p q) (m : Π i, E i) : has_sum (λ a, p a m) (q m) :=
begin
dsimp [has_sum] at h ⊢,
convert ((continuous_eval_left m).tendsto _).comp h,
ext s,
simp
end
lemma tsum_eval {α : Type*} {p : α → continuous_multilinear_map 𝕜 E G} (hp : summable p)
(m : Π i, E i) : (∑' a, p a) m = ∑' a, p a m :=
(has_sum_eval hp.has_sum m).tsum_eq.symm
open_locale topological_space
open filter
/-- If the target space is complete, the space of continuous multilinear maps with its norm is also
complete. The proof is essentially the same as for the space of continuous linear maps (modulo the
addition of `finset.prod` where needed. The duplication could be avoided by deducing the linear
case from the multilinear case via a currying isomorphism. However, this would mess up imports,
and it is more satisfactory to have the simplest case as a standalone proof. -/
instance [complete_space G] : complete_space (continuous_multilinear_map 𝕜 E G) :=
begin
have nonneg : ∀ (v : Π i, E i), 0 ≤ ∏ i, ∥v i∥ :=
λ v, finset.prod_nonneg (λ i hi, norm_nonneg _),
-- We show that every Cauchy sequence converges.
refine metric.complete_of_cauchy_seq_tendsto (λ f hf, _),
-- We now expand out the definition of a Cauchy sequence,
rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩,
-- and establish that the evaluation at any point `v : Π i, E i` is Cauchy.
have cau : ∀ v, cauchy_seq (λ n, f n v),
{ assume v,
apply cauchy_seq_iff_le_tendsto_0.2 ⟨λ n, b n * ∏ i, ∥v i∥, λ n, _, _, _⟩,
{ exact mul_nonneg (b0 n) (nonneg v) },
{ assume n m N hn hm,
rw dist_eq_norm,
apply le_trans ((f n - f m).le_op_norm v) _,
exact mul_le_mul_of_nonneg_right (b_bound n m N hn hm) (nonneg v) },
{ simpa using b_lim.mul tendsto_const_nhds } },
-- We assemble the limits points of those Cauchy sequences
-- (which exist as `G` is complete)
-- into a function which we call `F`.
choose F hF using λv, cauchy_seq_tendsto_of_complete (cau v),
-- Next, we show that this `F` is multilinear,
let Fmult : multilinear_map 𝕜 E G :=
{ to_fun := F,
map_add' := λ v i x y, begin
have A := hF (function.update v i (x + y)),
have B := (hF (function.update v i x)).add (hF (function.update v i y)),
simp at A B,
exact tendsto_nhds_unique A B
end,
map_smul' := λ v i c x, begin
have A := hF (function.update v i (c • x)),
have B := filter.tendsto.smul (@tendsto_const_nhds _ ℕ _ c _) (hF (function.update v i x)),
simp at A B,
exact tendsto_nhds_unique A B
end },
-- and that `F` has norm at most `(b 0 + ∥f 0∥)`.
have Fnorm : ∀ v, ∥F v∥ ≤ (b 0 + ∥f 0∥) * ∏ i, ∥v i∥,
{ assume v,
have A : ∀ n, ∥f n v∥ ≤ (b 0 + ∥f 0∥) * ∏ i, ∥v i∥,
{ assume n,
apply le_trans ((f n).le_op_norm _) _,
apply mul_le_mul_of_nonneg_right _ (nonneg v),
calc ∥f n∥ = ∥(f n - f 0) + f 0∥ : by { congr' 1, abel }
... ≤ ∥f n - f 0∥ + ∥f 0∥ : norm_add_le _ _
... ≤ b 0 + ∥f 0∥ : begin
apply add_le_add_right,
simpa [dist_eq_norm] using b_bound n 0 0 (zero_le _) (zero_le _)
end },
exact le_of_tendsto (hF v).norm (eventually_of_forall A) },
-- Thus `F` is continuous, and we propose that as the limit point of our original Cauchy sequence.
let Fcont := Fmult.mk_continuous _ Fnorm,
use Fcont,
-- Our last task is to establish convergence to `F` in norm.
have : ∀ n, ∥f n - Fcont∥ ≤ b n,
{ assume n,
apply op_norm_le_bound _ (b0 n) (λ v, _),
have A : ∀ᶠ m in at_top, ∥(f n - f m) v∥ ≤ b n * ∏ i, ∥v i∥,
{ refine eventually_at_top.2 ⟨n, λ m hm, _⟩,
apply le_trans ((f n - f m).le_op_norm _) _,
exact mul_le_mul_of_nonneg_right (b_bound n m n le_rfl hm) (nonneg v) },
have B : tendsto (λ m, ∥(f n - f m) v∥) at_top (𝓝 (∥(f n - Fcont) v∥)) :=
tendsto.norm (tendsto_const_nhds.sub (hF v)),
exact le_of_tendsto B A },
erw tendsto_iff_norm_tendsto_zero,
exact squeeze_zero (λ n, norm_nonneg _) this b_lim,
end
end continuous_multilinear_map
/-- If a continuous multilinear map is constructed from a multilinear map via the constructor
`mk_continuous`, then its norm is bounded by the bound given to the constructor if it is
nonnegative. -/
lemma multilinear_map.mk_continuous_norm_le (f : multilinear_map 𝕜 E G) {C : ℝ} (hC : 0 ≤ C)
(H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : ∥f.mk_continuous C H∥ ≤ C :=
continuous_multilinear_map.op_norm_le_bound _ hC (λm, H m)
/-- If a continuous multilinear map is constructed from a multilinear map via the constructor
`mk_continuous`, then its norm is bounded by the bound given to the constructor if it is
nonnegative. -/
lemma multilinear_map.mk_continuous_norm_le' (f : multilinear_map 𝕜 E G) {C : ℝ}
(H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : ∥f.mk_continuous C H∥ ≤ max C 0 :=
continuous_multilinear_map.op_norm_le_bound _ (le_max_right _ _) $
λ m, (H m).trans $ mul_le_mul_of_nonneg_right (le_max_left _ _)
(prod_nonneg $ λ _ _, norm_nonneg _)
namespace continuous_multilinear_map
/-- Given a continuous multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset
`s` of `k` of these variables, one gets a new continuous multilinear map on `fin k` by varying
these variables, and fixing the other ones equal to a given value `z`. It is denoted by
`f.restr s hk z`, where `hk` is a proof that the cardinality of `s` is `k`. The implicit
identification between `fin k` and `s` that we use is the canonical (increasing) bijection. -/
def restr {k n : ℕ} (f : (G [×n]→L[𝕜] G' : _)) (s : finset (fin n)) (hk : s.card = k) (z : G) :
G [×k]→L[𝕜] G' :=
(f.to_multilinear_map.restr s hk z).mk_continuous
(∥f∥ * ∥z∥^(n-k)) $ λ v, multilinear_map.restr_norm_le _ _ _ _ f.le_op_norm _
lemma norm_restr {k n : ℕ} (f : G [×n]→L[𝕜] G') (s : finset (fin n)) (hk : s.card = k) (z : G) :
∥f.restr s hk z∥ ≤ ∥f∥ * ∥z∥ ^ (n - k) :=
begin
apply multilinear_map.mk_continuous_norm_le,
exact mul_nonneg (norm_nonneg _) (pow_nonneg (norm_nonneg _) _)
end
section
variables {𝕜 ι} {A : Type*} [normed_comm_ring A] [normed_algebra 𝕜 A]
@[simp]
lemma norm_mk_pi_algebra_le [nonempty ι] :
∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ 1 :=
begin
have := λ f, @op_norm_le_bound 𝕜 ι (λ i, A) A _ _ _ _ _ _ _ f _ zero_le_one,
refine this _ _,
intros m,
simp only [continuous_multilinear_map.mk_pi_algebra_apply, one_mul],
exact norm_prod_le' _ univ_nonempty _,
end
lemma norm_mk_pi_algebra_of_empty [is_empty ι] :
∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ = ∥(1 : A)∥ :=
begin
apply le_antisymm,
{ have := λ f, @op_norm_le_bound 𝕜 ι (λ i, A) A _ _ _ _ _ _ _ f _ (norm_nonneg (1 : A)),
refine this _ _,
simp, },
{ convert ratio_le_op_norm _ (λ _, (1 : A)),
simp [eq_empty_of_is_empty (univ : finset ι)], },
end
@[simp] lemma norm_mk_pi_algebra [norm_one_class A] :
∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ = 1 :=
begin
casesI is_empty_or_nonempty ι,
{ simp [norm_mk_pi_algebra_of_empty] },
{ refine le_antisymm norm_mk_pi_algebra_le _,
convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance],
simp },
end
end
section
variables {𝕜 n} {A : Type*} [normed_ring A] [normed_algebra 𝕜 A]
lemma norm_mk_pi_algebra_fin_succ_le :
∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n.succ A∥ ≤ 1 :=
begin
have := λ f, @op_norm_le_bound 𝕜 (fin n.succ) (λ i, A) A _ _ _ _ _ _ _ f _ zero_le_one,
refine this _ _,
intros m,
simp only [continuous_multilinear_map.mk_pi_algebra_fin_apply, one_mul, list.of_fn_eq_map,
fin.univ_def, finset.fin_range, finset.prod, multiset.coe_map, multiset.coe_prod],
refine (list.norm_prod_le' _).trans_eq _,
{ rw [ne.def, list.map_eq_nil, list.fin_range_eq_nil],
exact nat.succ_ne_zero _, },
rw list.map_map,
end
lemma norm_mk_pi_algebra_fin_le_of_pos (hn : 0 < n) :
∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A∥ ≤ 1 :=
begin
obtain ⟨n, rfl⟩ := nat.exists_eq_succ_of_ne_zero hn.ne',
exact norm_mk_pi_algebra_fin_succ_le
end
lemma norm_mk_pi_algebra_fin_zero :
∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 0 A∥ = ∥(1 : A)∥ :=
begin
refine le_antisymm _ _,
{ have := λ f, @op_norm_le_bound 𝕜 (fin 0) (λ i, A) A _ _ _ _ _ _ _ f _ (norm_nonneg (1 : A)),
refine this _ _,
simp, },
{ convert ratio_le_op_norm _ (λ _, (1 : A)),
simp }
end
@[simp] lemma norm_mk_pi_algebra_fin [norm_one_class A] :
∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A∥ = 1 :=
begin
cases n,
{ simp [norm_mk_pi_algebra_fin_zero] },
{ refine le_antisymm norm_mk_pi_algebra_fin_succ_le _,
convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance],
simp }
end
end
variables (𝕜 ι)
/-- The canonical continuous multilinear map on `𝕜^ι`, associating to `m` the product of all the
`m i` (multiplied by a fixed reference element `z` in the target module) -/
protected def mk_pi_field (z : G) : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G :=
multilinear_map.mk_continuous
(multilinear_map.mk_pi_ring 𝕜 ι z) (∥z∥)
(λ m, by simp only [multilinear_map.mk_pi_ring_apply, norm_smul, norm_prod,
mul_comm])
variables {𝕜 ι}
@[simp] lemma mk_pi_field_apply (z : G) (m : ι → 𝕜) :
(continuous_multilinear_map.mk_pi_field 𝕜 ι z : (ι → 𝕜) → G) m = (∏ i, m i) • z := rfl
lemma mk_pi_field_apply_one_eq_self (f : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G) :
continuous_multilinear_map.mk_pi_field 𝕜 ι (f (λi, 1)) = f :=
to_multilinear_map_inj f.to_multilinear_map.mk_pi_ring_apply_one_eq_self
@[simp] lemma norm_mk_pi_field (z : G) : ∥continuous_multilinear_map.mk_pi_field 𝕜 ι z∥ = ∥z∥ :=
(multilinear_map.mk_continuous_norm_le _ (norm_nonneg z) _).antisymm $
by simpa using (continuous_multilinear_map.mk_pi_field 𝕜 ι z).le_op_norm (λ _, 1)
variables (𝕜 ι G)
/-- Continuous multilinear maps on `𝕜^n` with values in `G` are in bijection with `G`, as such a
continuous multilinear map is completely determined by its value on the constant vector made of
ones. We register this bijection as a linear isometry in
`continuous_multilinear_map.pi_field_equiv`. -/
protected def pi_field_equiv : G ≃ₗᵢ[𝕜] (continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G) :=
{ to_fun := λ z, continuous_multilinear_map.mk_pi_field 𝕜 ι z,
inv_fun := λ f, f (λi, 1),
map_add' := λ z z', by { ext m, simp [smul_add] },
map_smul' := λ c z, by { ext m, simp [smul_smul, mul_comm] },
left_inv := λ z, by simp,
right_inv := λ f, f.mk_pi_field_apply_one_eq_self,
norm_map' := norm_mk_pi_field }
end continuous_multilinear_map
namespace continuous_linear_map
lemma norm_comp_continuous_multilinear_map_le (g : G →L[𝕜] G')
(f : continuous_multilinear_map 𝕜 E G) :
∥g.comp_continuous_multilinear_map f∥ ≤ ∥g∥ * ∥f∥ :=
continuous_multilinear_map.op_norm_le_bound _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) $ λ m,
calc ∥g (f m)∥ ≤ ∥g∥ * (∥f∥ * ∏ i, ∥m i∥) : g.le_op_norm_of_le $ f.le_op_norm _
... = _ : (mul_assoc _ _ _).symm
variables (𝕜 E G G')
/-- `continuous_linear_map.comp_continuous_multilinear_map` as a bundled continuous bilinear map. -/
def comp_continuous_multilinear_mapL :
(G →L[𝕜] G') →L[𝕜] continuous_multilinear_map 𝕜 E G →L[𝕜] continuous_multilinear_map 𝕜 E G' :=
linear_map.mk_continuous₂
(linear_map.mk₂ 𝕜 comp_continuous_multilinear_map (λ f₁ f₂ g, rfl) (λ c f g, rfl)
(λ f g₁ g₂, by { ext1, apply f.map_add }) (λ c f g, by { ext1, simp }))
1 $ λ f g, by { rw one_mul, exact f.norm_comp_continuous_multilinear_map_le g }
variables {𝕜 E G G'}
/-- Flip arguments in `f : G →L[𝕜] continuous_multilinear_map 𝕜 E G'` to get
`continuous_multilinear_map 𝕜 E (G →L[𝕜] G')` -/
def flip_multilinear (f : G →L[𝕜] continuous_multilinear_map 𝕜 E G') :
continuous_multilinear_map 𝕜 E (G →L[𝕜] G') :=
multilinear_map.mk_continuous
{ to_fun := λ m, linear_map.mk_continuous
{ to_fun := λ x, f x m,
map_add' := λ x y, by simp only [map_add, continuous_multilinear_map.add_apply],
map_smul' := λ c x, by simp only [continuous_multilinear_map.smul_apply, map_smul,
ring_hom.id_apply] }
(∥f∥ * ∏ i, ∥m i∥) $ λ x,
by { rw mul_right_comm, exact (f x).le_of_op_norm_le _ (f.le_op_norm x) },
map_add' := λ m i x y,
by { ext1, simp only [add_apply, continuous_multilinear_map.map_add, linear_map.coe_mk,
linear_map.mk_continuous_apply]},
map_smul' := λ m i c x,
by { ext1, simp only [coe_smul', continuous_multilinear_map.map_smul, linear_map.coe_mk,
linear_map.mk_continuous_apply, pi.smul_apply]} }
∥f∥ $ λ m,
linear_map.mk_continuous_norm_le _
(mul_nonneg (norm_nonneg f) (prod_nonneg $ λ i hi, norm_nonneg (m i))) _
end continuous_linear_map
open continuous_multilinear_map
namespace multilinear_map
/-- Given a map `f : G →ₗ[𝕜] multilinear_map 𝕜 E G'` and an estimate
`H : ∀ x m, ∥f x m∥ ≤ C * ∥x∥ * ∏ i, ∥m i∥`, construct a continuous linear
map from `G` to `continuous_multilinear_map 𝕜 E G'`.
In order to lift, e.g., a map `f : (multilinear_map 𝕜 E G) →ₗ[𝕜] multilinear_map 𝕜 E' G'`
to a map `(continuous_multilinear_map 𝕜 E G) →L[𝕜] continuous_multilinear_map 𝕜 E' G'`,
one can apply this construction to `f.comp continuous_multilinear_map.to_multilinear_map_linear`
which is a linear map from `continuous_multilinear_map 𝕜 E G` to `multilinear_map 𝕜 E' G'`. -/
def mk_continuous_linear (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') (C : ℝ)
(H : ∀ x m, ∥f x m∥ ≤ C * ∥x∥ * ∏ i, ∥m i∥) :
G →L[𝕜] continuous_multilinear_map 𝕜 E G' :=
linear_map.mk_continuous
{ to_fun := λ x, (f x).mk_continuous (C * ∥x∥) $ H x,
map_add' := λ x y, by { ext1, simp },
map_smul' := λ c x, by { ext1, simp } }
(max C 0) $ λ x, ((f x).mk_continuous_norm_le' _).trans_eq $
by rw [max_mul_of_nonneg _ _ (norm_nonneg x), zero_mul]
lemma mk_continuous_linear_norm_le' (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') (C : ℝ)
(H : ∀ x m, ∥f x m∥ ≤ C * ∥x∥ * ∏ i, ∥m i∥) :
∥mk_continuous_linear f C H∥ ≤ max C 0 :=
begin
dunfold mk_continuous_linear,
exact linear_map.mk_continuous_norm_le _ (le_max_right _ _) _
end
lemma mk_continuous_linear_norm_le (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') {C : ℝ} (hC : 0 ≤ C)
(H : ∀ x m, ∥f x m∥ ≤ C * ∥x∥ * ∏ i, ∥m i∥) :
∥mk_continuous_linear f C H∥ ≤ C :=
(mk_continuous_linear_norm_le' f C H).trans_eq (max_eq_left hC)
/-- Given a map `f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)` and an estimate
`H : ∀ m m', ∥f m m'∥ ≤ C * ∏ i, ∥m i∥ * ∏ i, ∥m' i∥`, upgrade all `multilinear_map`s in the type to
`continuous_multilinear_map`s. -/
def mk_continuous_multilinear (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) (C : ℝ)
(H : ∀ m₁ m₂, ∥f m₁ m₂∥ ≤ C * (∏ i, ∥m₁ i∥) * ∏ i, ∥m₂ i∥) :
continuous_multilinear_map 𝕜 E (continuous_multilinear_map 𝕜 E' G) :=
mk_continuous
{ to_fun := λ m, mk_continuous (f m) (C * ∏ i, ∥m i∥) $ H m,
map_add' := λ m i x y, by { ext1, simp },
map_smul' := λ m i c x, by { ext1, simp } }
(max C 0) $ λ m, ((f m).mk_continuous_norm_le' _).trans_eq $
by { rw [max_mul_of_nonneg, zero_mul], exact prod_nonneg (λ _ _, norm_nonneg _) }
@[simp] lemma mk_continuous_multilinear_apply (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G))
{C : ℝ} (H : ∀ m₁ m₂, ∥f m₁ m₂∥ ≤ C * (∏ i, ∥m₁ i∥) * ∏ i, ∥m₂ i∥) (m : Π i, E i) :
⇑(mk_continuous_multilinear f C H m) = f m :=
rfl
lemma mk_continuous_multilinear_norm_le' (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) (C : ℝ)
(H : ∀ m₁ m₂, ∥f m₁ m₂∥ ≤ C * (∏ i, ∥m₁ i∥) * ∏ i, ∥m₂ i∥) :
∥mk_continuous_multilinear f C H∥ ≤ max C 0 :=
begin
dunfold mk_continuous_multilinear,
exact mk_continuous_norm_le _ (le_max_right _ _) _
end
lemma mk_continuous_multilinear_norm_le (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) {C : ℝ}
(hC : 0 ≤ C) (H : ∀ m₁ m₂, ∥f m₁ m₂∥ ≤ C * (∏ i, ∥m₁ i∥) * ∏ i, ∥m₂ i∥) :
∥mk_continuous_multilinear f C H∥ ≤ C :=
(mk_continuous_multilinear_norm_le' f C H).trans_eq (max_eq_left hC)
end multilinear_map
namespace continuous_multilinear_map
lemma norm_comp_continuous_linear_le (g : continuous_multilinear_map 𝕜 E₁ G)
(f : Π i, E i →L[𝕜] E₁ i) :
∥g.comp_continuous_linear_map f∥ ≤ ∥g∥ * ∏ i, ∥f i∥ :=
op_norm_le_bound _ (mul_nonneg (norm_nonneg _) $ prod_nonneg $ λ i hi, norm_nonneg _) $ λ m,
calc ∥g (λ i, f i (m i))∥ ≤ ∥g∥ * ∏ i, ∥f i (m i)∥ : g.le_op_norm _
... ≤ ∥g∥ * ∏ i, (∥f i∥ * ∥m i∥) :
mul_le_mul_of_nonneg_left
(prod_le_prod (λ _ _, norm_nonneg _) (λ i hi, (f i).le_op_norm (m i))) (norm_nonneg g)
... = (∥g∥ * ∏ i, ∥f i∥) * ∏ i, ∥m i∥ : by rw [prod_mul_distrib, mul_assoc]
/-- `continuous_multilinear_map.comp_continuous_linear_map` as a bundled continuous linear map.
This implementation fixes `f : Π i, E i →L[𝕜] E₁ i`.
TODO: Actually, the map is multilinear in `f` but an attempt to formalize this failed because of
issues with class instances. -/
def comp_continuous_linear_mapL (f : Π i, E i →L[𝕜] E₁ i) :
continuous_multilinear_map 𝕜 E₁ G →L[𝕜] continuous_multilinear_map 𝕜 E G :=
linear_map.mk_continuous
{ to_fun := λ g, g.comp_continuous_linear_map f,
map_add' := λ g₁ g₂, rfl,
map_smul' := λ c g, rfl }
(∏ i, ∥f i∥) $ λ g, (norm_comp_continuous_linear_le _ _).trans_eq (mul_comm _ _)
@[simp] lemma comp_continuous_linear_mapL_apply (g : continuous_multilinear_map 𝕜 E₁ G)
(f : Π i, E i →L[𝕜] E₁ i) :
comp_continuous_linear_mapL f g = g.comp_continuous_linear_map f :=
rfl
lemma norm_comp_continuous_linear_mapL_le (f : Π i, E i →L[𝕜] E₁ i) :
∥@comp_continuous_linear_mapL 𝕜 ι E E₁ G _ _ _ _ _ _ _ _ _ f∥ ≤ (∏ i, ∥f i∥) :=
linear_map.mk_continuous_norm_le _ (prod_nonneg $ λ i _, norm_nonneg _) _
end continuous_multilinear_map
section currying
/-!
### Currying
We associate to a continuous multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two
curried functions, named `f.curry_left` (which is a continuous linear map on `E 0` taking values
in continuous multilinear maps in `n` variables) and `f.curry_right` (which is a continuous
multilinear map in `n` variables taking values in continuous linear maps on `E (last n)`).
The inverse operations are called `uncurry_left` and `uncurry_right`.
We also register continuous linear equiv versions of these correspondences, in
`continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`.
-/
open fin function
lemma continuous_linear_map.norm_map_tail_le
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) (m : Πi, Ei i) :
∥f (m 0) (tail m)∥ ≤ ∥f∥ * ∏ i, ∥m i∥ :=
calc
∥f (m 0) (tail m)∥ ≤ ∥f (m 0)∥ * ∏ i, ∥(tail m) i∥ : (f (m 0)).le_op_norm _
... ≤ (∥f∥ * ∥m 0∥) * ∏ i, ∥(tail m) i∥ :
mul_le_mul_of_nonneg_right (f.le_op_norm _) (prod_nonneg (λi hi, norm_nonneg _))
... = ∥f∥ * (∥m 0∥ * ∏ i, ∥(tail m) i∥) : by ring
... = ∥f∥ * ∏ i, ∥m i∥ : by { rw prod_univ_succ, refl }
lemma continuous_multilinear_map.norm_map_init_le
(f : continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G))
(m : Πi, Ei i) :
∥f (init m) (m (last n))∥ ≤ ∥f∥ * ∏ i, ∥m i∥ :=
calc
∥f (init m) (m (last n))∥ ≤ ∥f (init m)∥ * ∥m (last n)∥ : (f (init m)).le_op_norm _
... ≤ (∥f∥ * (∏ i, ∥(init m) i∥)) * ∥m (last n)∥ :
mul_le_mul_of_nonneg_right (f.le_op_norm _) (norm_nonneg _)
... = ∥f∥ * ((∏ i, ∥(init m) i∥) * ∥m (last n)∥) : mul_assoc _ _ _
... = ∥f∥ * ∏ i, ∥m i∥ : by { rw prod_univ_cast_succ, refl }
lemma continuous_multilinear_map.norm_map_cons_le
(f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (m : Π(i : fin n), Ei i.succ) :
∥f (cons x m)∥ ≤ ∥f∥ * ∥x∥ * ∏ i, ∥m i∥ :=
calc
∥f (cons x m)∥ ≤ ∥f∥ * ∏ i, ∥cons x m i∥ : f.le_op_norm _
... = (∥f∥ * ∥x∥) * ∏ i, ∥m i∥ : by { rw prod_univ_succ, simp [mul_assoc] }
lemma continuous_multilinear_map.norm_map_snoc_le
(f : continuous_multilinear_map 𝕜 Ei G) (m : Π(i : fin n), Ei i.cast_succ) (x : Ei (last n)) :
∥f (snoc m x)∥ ≤ ∥f∥ * (∏ i, ∥m i∥) * ∥x∥ :=
calc
∥f (snoc m x)∥ ≤ ∥f∥ * ∏ i, ∥snoc m x i∥ : f.le_op_norm _
... = ∥f∥ * (∏ i, ∥m i∥) * ∥x∥ : by { rw prod_univ_cast_succ, simp [mul_assoc] }
/-! #### Left currying -/
/-- Given a continuous linear map `f` from `E 0` to continuous multilinear maps on `n` variables,
construct the corresponding continuous multilinear map on `n+1` variables obtained by concatenating
the variables, given by `m ↦ f (m 0) (tail m)`-/
def continuous_linear_map.uncurry_left
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) :
continuous_multilinear_map 𝕜 Ei G :=
(@linear_map.uncurry_left 𝕜 n Ei G _ _ _ _ _
(continuous_multilinear_map.to_multilinear_map_linear.comp f.to_linear_map)).mk_continuous
(∥f∥) (λm, continuous_linear_map.norm_map_tail_le f m)
@[simp] lemma continuous_linear_map.uncurry_left_apply
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) (m : Πi, Ei i) :
f.uncurry_left m = f (m 0) (tail m) := rfl
/-- Given a continuous multilinear map `f` in `n+1` variables, split the first variable to obtain
a continuous linear map into continuous multilinear maps in `n` variables, given by
`x ↦ (m ↦ f (cons x m))`. -/
def continuous_multilinear_map.curry_left
(f : continuous_multilinear_map 𝕜 Ei G) :
Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G) :=
linear_map.mk_continuous
{ -- define a linear map into `n` continuous multilinear maps from an `n+1` continuous multilinear
-- map
to_fun := λx, (f.to_multilinear_map.curry_left x).mk_continuous
(∥f∥ * ∥x∥) (f.norm_map_cons_le x),
map_add' := λx y, by { ext m, exact f.cons_add m x y },
map_smul' := λc x, by { ext m, exact f.cons_smul m c x } }
-- then register its continuity thanks to its boundedness properties.
(∥f∥) (λx, multilinear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _)
@[simp] lemma continuous_multilinear_map.curry_left_apply
(f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (m : Π(i : fin n), Ei i.succ) :
f.curry_left x m = f (cons x m) := rfl
@[simp] lemma continuous_linear_map.curry_uncurry_left
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) :
f.uncurry_left.curry_left = f :=
begin
ext m x,
simp only [tail_cons, continuous_linear_map.uncurry_left_apply,
continuous_multilinear_map.curry_left_apply],
rw cons_zero
end
@[simp] lemma continuous_multilinear_map.uncurry_curry_left
(f : continuous_multilinear_map 𝕜 Ei G) : f.curry_left.uncurry_left = f :=
continuous_multilinear_map.to_multilinear_map_inj $ f.to_multilinear_map.uncurry_curry_left
variables (𝕜 Ei G)
/-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to
the space of continuous linear maps from `E 0` to the space of continuous multilinear maps on
`Π(i : fin n), E i.succ `, by separating the first variable. We register this isomorphism in
`continuous_multilinear_curry_left_equiv 𝕜 E E₂`. The algebraic version (without topology) is given
in `multilinear_curry_left_equiv 𝕜 E E₂`.
The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these
unless you need the full framework of linear isometric equivs. -/
def continuous_multilinear_curry_left_equiv :
(Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) ≃ₗᵢ[𝕜]
(continuous_multilinear_map 𝕜 Ei G) :=
linear_isometry_equiv.of_bounds
{ to_fun := continuous_linear_map.uncurry_left,
map_add' := λf₁ f₂, by { ext m, refl },
map_smul' := λc f, by { ext m, refl },
inv_fun := continuous_multilinear_map.curry_left,
left_inv := continuous_linear_map.curry_uncurry_left,
right_inv := continuous_multilinear_map.uncurry_curry_left }
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
(λ f, linear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
variables {𝕜 Ei G}
@[simp] lemma continuous_multilinear_curry_left_equiv_apply
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.succ) G)) (v : Π i, Ei i) :
continuous_multilinear_curry_left_equiv 𝕜 Ei G f v = f (v 0) (tail v) := rfl
@[simp] lemma continuous_multilinear_curry_left_equiv_symm_apply
(f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (v : Π i : fin n, Ei i.succ) :
(continuous_multilinear_curry_left_equiv 𝕜 Ei G).symm f x v = f (cons x v) := rfl
@[simp] lemma continuous_multilinear_map.curry_left_norm
(f : continuous_multilinear_map 𝕜 Ei G) : ∥f.curry_left∥ = ∥f∥ :=
(continuous_multilinear_curry_left_equiv 𝕜 Ei G).symm.norm_map f
@[simp] lemma continuous_linear_map.uncurry_left_norm
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) :
∥f.uncurry_left∥ = ∥f∥ :=
(continuous_multilinear_curry_left_equiv 𝕜 Ei G).norm_map f
/-! #### Right currying -/
/-- Given a continuous linear map `f` from continuous multilinear maps on `n` variables to
continuous linear maps on `E 0`, construct the corresponding continuous multilinear map on `n+1`
variables obtained by concatenating the variables, given by `m ↦ f (init m) (m (last n))`. -/
def continuous_multilinear_map.uncurry_right
(f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) :
continuous_multilinear_map 𝕜 Ei G :=
let f' : multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →ₗ[𝕜] G) :=
{ to_fun := λ m, (f m).to_linear_map,
map_add' := λ m i x y, by simp,
map_smul' := λ m i c x, by simp } in
(@multilinear_map.uncurry_right 𝕜 n Ei G _ _ _ _ _ f').mk_continuous
(∥f∥) (λm, f.norm_map_init_le m)
@[simp] lemma continuous_multilinear_map.uncurry_right_apply
(f : continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G))
(m : Πi, Ei i) :
f.uncurry_right m = f (init m) (m (last n)) := rfl
/-- Given a continuous multilinear map `f` in `n+1` variables, split the last variable to obtain
a continuous multilinear map in `n` variables into continuous linear maps, given by
`m ↦ (x ↦ f (snoc m x))`. -/
def continuous_multilinear_map.curry_right
(f : continuous_multilinear_map 𝕜 Ei G) :
continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G) :=
let f' : multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G) :=
{ to_fun := λm, (f.to_multilinear_map.curry_right m).mk_continuous
(∥f∥ * ∏ i, ∥m i∥) $ λx, f.norm_map_snoc_le m x,
map_add' := λ m i x y, by { simp, refl },
map_smul' := λ m i c x, by { simp, refl } } in
f'.mk_continuous (∥f∥) (λm, linear_map.mk_continuous_norm_le _
(mul_nonneg (norm_nonneg _) (prod_nonneg (λj hj, norm_nonneg _))) _)
@[simp] lemma continuous_multilinear_map.curry_right_apply
(f : continuous_multilinear_map 𝕜 Ei G) (m : Π i : fin n, Ei i.cast_succ) (x : Ei (last n)) :
f.curry_right m x = f (snoc m x) := rfl
@[simp] lemma continuous_multilinear_map.curry_uncurry_right
(f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) :
f.uncurry_right.curry_right = f :=
begin
ext m x,
simp only [snoc_last, continuous_multilinear_map.curry_right_apply,
continuous_multilinear_map.uncurry_right_apply],
rw init_snoc
end
@[simp] lemma continuous_multilinear_map.uncurry_curry_right
(f : continuous_multilinear_map 𝕜 Ei G) : f.curry_right.uncurry_right = f :=
by { ext m, simp }
variables (𝕜 Ei G)
/--
The space of continuous multilinear maps on `Π(i : fin (n+1)), Ei i` is canonically isomorphic to
the space of continuous multilinear maps on `Π(i : fin n), Ei i.cast_succ` with values in the space
of continuous linear maps on `Ei (last n)`, by separating the last variable. We register this
isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv 𝕜 Ei G`.
The algebraic version (without topology) is given in `multilinear_curry_right_equiv 𝕜 Ei G`.
The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these
unless you need the full framework of linear isometric equivs.
-/
def continuous_multilinear_curry_right_equiv :
(continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) ≃ₗᵢ[𝕜]
(continuous_multilinear_map 𝕜 Ei G) :=
linear_isometry_equiv.of_bounds
{ to_fun := continuous_multilinear_map.uncurry_right,
map_add' := λf₁ f₂, by { ext m, refl },
map_smul' := λc f, by { ext m, refl },
inv_fun := continuous_multilinear_map.curry_right,
left_inv := continuous_multilinear_map.curry_uncurry_right,
right_inv := continuous_multilinear_map.uncurry_curry_right }
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
variables (n G')
/-- The space of continuous multilinear maps on `Π(i : fin (n+1)), G` is canonically isomorphic to
the space of continuous multilinear maps on `Π(i : fin n), G` with values in the space
of continuous linear maps on `G`, by separating the last variable. We register this
isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv' 𝕜 n G G'`.
For a version allowing dependent types, see `continuous_multilinear_curry_right_equiv`. When there
are no dependent types, use the primed version as it helps Lean a lot for unification.
The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these
unless you need the full framework of linear isometric equivs. -/
def continuous_multilinear_curry_right_equiv' :
(G [×n]→L[𝕜] (G →L[𝕜] G')) ≃ₗᵢ[𝕜] (G [×n.succ]→L[𝕜] G') :=
continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin n.succ), G) G'
variables {n 𝕜 G Ei G'}
@[simp] lemma continuous_multilinear_curry_right_equiv_apply
(f : (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G)))
(v : Π i, Ei i) :
(continuous_multilinear_curry_right_equiv 𝕜 Ei G) f v = f (init v) (v (last n)) := rfl
@[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply
(f : continuous_multilinear_map 𝕜 Ei G)
(v : Π (i : fin n), Ei i.cast_succ) (x : Ei (last n)) :
(continuous_multilinear_curry_right_equiv 𝕜 Ei G).symm f v x = f (snoc v x) := rfl
@[simp] lemma continuous_multilinear_curry_right_equiv_apply'
(f : G [×n]→L[𝕜] (G →L[𝕜] G')) (v : fin (n + 1) → G) :
continuous_multilinear_curry_right_equiv' 𝕜 n G G' f v = f (init v) (v (last n)) := rfl
@[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply'
(f : G [×n.succ]→L[𝕜] G') (v : fin n → G) (x : G) :
(continuous_multilinear_curry_right_equiv' 𝕜 n G G').symm f v x = f (snoc v x) := rfl
@[simp] lemma continuous_multilinear_map.curry_right_norm
(f : continuous_multilinear_map 𝕜 Ei G) : ∥f.curry_right∥ = ∥f∥ :=
(continuous_multilinear_curry_right_equiv 𝕜 Ei G).symm.norm_map f
@[simp] lemma continuous_multilinear_map.uncurry_right_norm
(f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) :
∥f.uncurry_right∥ = ∥f∥ :=
(continuous_multilinear_curry_right_equiv 𝕜 Ei G).norm_map f
/-!
#### Currying with `0` variables
The space of multilinear maps with `0` variables is trivial: such a multilinear map is just an
arbitrary constant (note that multilinear maps in `0` variables need not map `0` to `0`!).
Therefore, the space of continuous multilinear maps on `(fin 0) → G` with values in `E₂` is
isomorphic (and even isometric) to `E₂`. As this is the zeroth step in the construction of iterated
derivatives, we register this isomorphism. -/
section
local attribute [instance] unique.subsingleton
variables {𝕜 G G'}
/-- Associating to a continuous multilinear map in `0` variables the unique value it takes. -/
def continuous_multilinear_map.uncurry0
(f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) G') : G' := f 0
variables (𝕜 G)
/-- Associating to an element `x` of a vector space `E₂` the continuous multilinear map in `0`
variables taking the (unique) value `x` -/
def continuous_multilinear_map.curry0 (x : G') : G [×0]→L[𝕜] G' :=
{ to_fun := λm, x,
map_add' := λ m i, fin.elim0 i,
map_smul' := λ m i, fin.elim0 i,
cont := continuous_const }
variable {G}
@[simp] lemma continuous_multilinear_map.curry0_apply (x : G') (m : (fin 0) → G) :
continuous_multilinear_map.curry0 𝕜 G x m = x := rfl
variable {𝕜}
@[simp] lemma continuous_multilinear_map.uncurry0_apply (f : G [×0]→L[𝕜] G') :
f.uncurry0 = f 0 := rfl
@[simp] lemma continuous_multilinear_map.apply_zero_curry0 (f : G [×0]→L[𝕜] G') {x : fin 0 → G} :
continuous_multilinear_map.curry0 𝕜 G (f x) = f :=
by { ext m, simp [(subsingleton.elim _ _ : x = m)] }
lemma continuous_multilinear_map.uncurry0_curry0 (f : G [×0]→L[𝕜] G') :
continuous_multilinear_map.curry0 𝕜 G (f.uncurry0) = f :=
by simp
variables (𝕜 G)
@[simp] lemma continuous_multilinear_map.curry0_uncurry0 (x : G') :
(continuous_multilinear_map.curry0 𝕜 G x).uncurry0 = x := rfl
@[simp] lemma continuous_multilinear_map.curry0_norm (x : G') :
∥continuous_multilinear_map.curry0 𝕜 G x∥ = ∥x∥ :=
begin
apply le_antisymm,
{ exact continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm, by simp) },
{ simpa using (continuous_multilinear_map.curry0 𝕜 G x).le_op_norm 0 }
end
variables {𝕜 G}
@[simp] lemma continuous_multilinear_map.fin0_apply_norm (f : G [×0]→L[𝕜] G') {x : fin 0 → G} :
∥f x∥ = ∥f∥ :=
begin
obtain rfl : x = 0 := subsingleton.elim _ _,
refine le_antisymm (by simpa using f.le_op_norm 0) _,
have : ∥continuous_multilinear_map.curry0 𝕜 G (f.uncurry0)∥ ≤ ∥f.uncurry0∥ :=
continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm,
by simp [-continuous_multilinear_map.apply_zero_curry0]),
simpa
end
lemma continuous_multilinear_map.uncurry0_norm (f : G [×0]→L[𝕜] G') : ∥f.uncurry0∥ = ∥f∥ :=
by simp
variables (𝕜 G G')
/-- The continuous linear isomorphism between elements of a normed space, and continuous multilinear
maps in `0` variables with values in this normed space.
The direct and inverse maps are `uncurry0` and `curry0`. Use these unless you need the full
framework of linear isometric equivs. -/
def continuous_multilinear_curry_fin0 : (G [×0]→L[𝕜] G') ≃ₗᵢ[𝕜] G' :=
{ to_fun := λf, continuous_multilinear_map.uncurry0 f,
inv_fun := λf, continuous_multilinear_map.curry0 𝕜 G f,
map_add' := λf g, rfl,
map_smul' := λc f, rfl,
left_inv := continuous_multilinear_map.uncurry0_curry0,
right_inv := continuous_multilinear_map.curry0_uncurry0 𝕜 G,
norm_map' := continuous_multilinear_map.uncurry0_norm }
variables {𝕜 G G'}
@[simp] lemma continuous_multilinear_curry_fin0_apply (f : G [×0]→L[𝕜] G') :
continuous_multilinear_curry_fin0 𝕜 G G' f = f 0 := rfl
@[simp] lemma continuous_multilinear_curry_fin0_symm_apply (x : G') (v : (fin 0) → G) :
(continuous_multilinear_curry_fin0 𝕜 G G').symm x v = x := rfl
end
/-! #### With 1 variable -/
variables (𝕜 G G')
/-- Continuous multilinear maps from `G^1` to `G'` are isomorphic with continuous linear maps from
`G` to `G'`. -/
def continuous_multilinear_curry_fin1 : (G [×1]→L[𝕜] G') ≃ₗᵢ[𝕜] (G →L[𝕜] G') :=
(continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin 1), G) G').symm.trans
(continuous_multilinear_curry_fin0 𝕜 G (G →L[𝕜] G'))
variables {𝕜 G G'}
@[simp] lemma continuous_multilinear_curry_fin1_apply (f : G [×1]→L[𝕜] G') (x : G) :
continuous_multilinear_curry_fin1 𝕜 G G' f x = f (fin.snoc 0 x) := rfl
@[simp] lemma continuous_multilinear_curry_fin1_symm_apply
(f : G →L[𝕜] G') (v : (fin 1) → G) :
(continuous_multilinear_curry_fin1 𝕜 G G').symm f v = f (v 0) := rfl
namespace continuous_multilinear_map
variables (𝕜 G G')
/-- An equivalence of the index set defines a linear isometric equivalence between the spaces
of multilinear maps. -/
def dom_dom_congr (σ : ι ≃ ι') :
continuous_multilinear_map 𝕜 (λ _ : ι, G) G' ≃ₗᵢ[𝕜]
continuous_multilinear_map 𝕜 (λ _ : ι', G) G' :=
linear_isometry_equiv.of_bounds
{ to_fun := λ f, (multilinear_map.dom_dom_congr σ f.to_multilinear_map).mk_continuous ∥f∥ $
λ m, (f.le_op_norm (λ i, m (σ i))).trans_eq $ by rw [← σ.prod_comp],
inv_fun := λ f, (multilinear_map.dom_dom_congr σ.symm f.to_multilinear_map).mk_continuous ∥f∥ $
λ m, (f.le_op_norm (λ i, m (σ.symm i))).trans_eq $ by rw [← σ.symm.prod_comp],
left_inv := λ f, ext $ λ m, congr_arg f $ by simp only [σ.symm_apply_apply],
right_inv := λ f, ext $ λ m, congr_arg f $ by simp only [σ.apply_symm_apply],
map_add' := λ f g, rfl,
map_smul' := λ c f, rfl }
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
variables {𝕜 G G'}
section
variable [decidable_eq (ι ⊕ ι')]
/-- A continuous multilinear map with variables indexed by `ι ⊕ ι'` defines a continuous multilinear
map with variables indexed by `ι` taking values in the space of continuous multilinear maps with
variables indexed by `ι'`. -/
def curry_sum (f : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G') :
continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G') :=
multilinear_map.mk_continuous_multilinear (multilinear_map.curry_sum f.to_multilinear_map) (∥f∥) $
λ m m', by simpa [fintype.prod_sum_type, mul_assoc] using f.le_op_norm (sum.elim m m')
@[simp] lemma curry_sum_apply (f : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G')
(m : ι → G) (m' : ι' → G) :
f.curry_sum m m' = f (sum.elim m m') :=
rfl
/-- A continuous multilinear map with variables indexed by `ι` taking values in the space of
continuous multilinear maps with variables indexed by `ι'` defines a continuous multilinear map with
variables indexed by `ι ⊕ ι'`. -/
def uncurry_sum
(f : continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G')) :
continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G' :=
multilinear_map.mk_continuous
(to_multilinear_map_linear.comp_multilinear_map f.to_multilinear_map).uncurry_sum (∥f∥) $ λ m,
by simpa [fintype.prod_sum_type, mul_assoc]
using (f (m ∘ sum.inl)).le_of_op_norm_le (m ∘ sum.inr) (f.le_op_norm _)
@[simp] lemma uncurry_sum_apply
(f : continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G'))
(m : ι ⊕ ι' → G) :
f.uncurry_sum m = f (m ∘ sum.inl) (m ∘ sum.inr) :=
rfl
variables (𝕜 ι ι' G G')
/-- Linear isometric equivalence between the space of continuous multilinear maps with variables
indexed by `ι ⊕ ι'` and the space of continuous multilinear maps with variables indexed by `ι`
taking values in the space of continuous multilinear maps with variables indexed by `ι'`.
The forward and inverse functions are `continuous_multilinear_map.curry_sum`
and `continuous_multilinear_map.uncurry_sum`. Use this definition only if you need
some properties of `linear_isometry_equiv`. -/
def curry_sum_equiv : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G' ≃ₗᵢ[𝕜]
continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G') :=
linear_isometry_equiv.of_bounds
{ to_fun := curry_sum,
inv_fun := uncurry_sum,
map_add' := λ f g, by { ext, refl },
map_smul' := λ c f, by { ext, refl },
left_inv := λ f, by { ext m, exact congr_arg f (sum.elim_comp_inl_inr m) },
right_inv := λ f, by { ext m₁ m₂, change f _ _ = f _ _,
rw [sum.elim_comp_inl, sum.elim_comp_inr] } }
(λ f, multilinear_map.mk_continuous_multilinear_norm_le _ (norm_nonneg f) _)
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
end
section
variables (𝕜 G G') {k l : ℕ} {s : finset (fin n)}
/-- If `s : finset (fin n)` is a finite set of cardinality `k` and its complement has cardinality
`l`, then the space of continuous multilinear maps `G [×n]→L[𝕜] G'` of `n` variables is isomorphic
to the space of continuous multilinear maps `G [×k]→L[𝕜] G [×l]→L[𝕜] G'` of `k` variables taking
values in the space of continuous multilinear maps of `l` variables. -/
def curry_fin_finset {k l n : ℕ} {s : finset (fin n)}
(hk : s.card = k) (hl : sᶜ.card = l) :
(G [×n]→L[𝕜] G') ≃ₗᵢ[𝕜] (G [×k]→L[𝕜] G [×l]→L[𝕜] G') :=
(dom_dom_congr 𝕜 G G' (fin_sum_equiv_of_finset hk hl).symm).trans
(curry_sum_equiv 𝕜 (fin k) (fin l) G G')
variables {𝕜 G G'}
@[simp] lemma curry_fin_finset_apply (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×n]→L[𝕜] G') (mk : fin k → G) (ml : fin l → G) :
curry_fin_finset 𝕜 G G' hk hl f mk ml =
f (λ i, sum.elim mk ml ((fin_sum_equiv_of_finset hk hl).symm i)) :=
rfl
@[simp] lemma curry_fin_finset_symm_apply (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (m : fin n → G) :
(curry_fin_finset 𝕜 G G' hk hl).symm f m =
f (λ i, m $ fin_sum_equiv_of_finset hk hl (sum.inl i))
(λ i, m $ fin_sum_equiv_of_finset hk hl (sum.inr i)) :=
rfl
@[simp] lemma curry_fin_finset_symm_apply_piecewise_const (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (x y : G) :
(curry_fin_finset 𝕜 G G' hk hl).symm f (s.piecewise (λ _, x) (λ _, y)) = f (λ _, x) (λ _, y) :=
multilinear_map.curry_fin_finset_symm_apply_piecewise_const hk hl _ x y
@[simp] lemma curry_fin_finset_symm_apply_const (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (x : G) :
(curry_fin_finset 𝕜 G G' hk hl).symm f (λ _, x) = f (λ _, x) (λ _, x) :=
rfl
@[simp] lemma curry_fin_finset_apply_const (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×n]→L[𝕜] G') (x y : G) :
curry_fin_finset 𝕜 G G' hk hl f (λ _, x) (λ _, y) = f (s.piecewise (λ _, x) (λ _, y)) :=
begin
refine (curry_fin_finset_symm_apply_piecewise_const hk hl _ _ _).symm.trans _, -- `rw` fails
rw linear_isometry_equiv.symm_apply_apply
end
end
end continuous_multilinear_map
end currying
|
86f04f7ac31e7d3932e2694484656e652345b09a | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/term_app.lean | 7306622db6d4f296e78b80c693fdcd86391e1e93 | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 314 | lean | inductive term
| const : string → term
| app : string → list term → term
mutual def num_consts, num_consts_lst
with num_consts : term → nat
| (term.const n) := 1
| (term.app n ts) := num_consts_lst ts
with num_consts_lst : list term → nat
| [] := 0
| (t::ts) := num_consts t + num_consts_lst ts
|
055699d74564ca808b46bfde30d3b7b8e5c810bf | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/compatibleTypesEtaIssue.lean | bc19cb9d347c7e36ab217f4070ce0e27e6fbad13 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | leanprover/lean4 | 4bdf9790294964627eb9be79f5e8f6157780b4cc | f1f9dc0f2f531af3312398999d8b8303fa5f096b | refs/heads/master | 1,693,360,665,786 | 1,693,350,868,000 | 1,693,350,868,000 | 129,571,436 | 2,827 | 311 | Apache-2.0 | 1,694,716,156,000 | 1,523,760,560,000 | Lean | UTF-8 | Lean | false | false | 1,673 | lean | namespace MWE
universe u v w
inductive Id {A : Type u} : A → A → Type u
| refl {a : A} : Id a a
attribute [eliminator] Id.casesOn
infix:50 (priority := high) " = " => Id
def contr (A : Type u) := Σ (a : A), ∀ b, a = b
def singl {A : Type u} (a : A) :=
Σ b, a = b
def Corr (A : Type u) (B : Type v) :=
Σ (R : A → B → Type w), (∀ a, contr (Σ b, R a b)) × (∀ b, contr (Σ a, R a b))
def Homotopy {A : Type u} {B : A → Type v} (f g : ∀ x, B x) :=
∀ (x : A), f x = g x
infix:80 " ~ " => Homotopy
def isQinv {A : Type u} {B : Type v} (f : A → B) (g : B → A) :=
(f ∘ g ~ id) × (g ∘ f ~ id)
def Qinv {A : Type u} {B : Type v} (f : A → B) :=
Σ (g : B → A), isQinv f g
def Qinv.eqv (A : Type u) (B : Type v) :=
Σ (f : A → B), Qinv f
def linv {A : Type u} {B : Type v} (f : A → B) :=
Σ (g : B → A), g ∘ f ~ id
def rinv {A : Type u} {B : Type v} (f : A → B) :=
Σ (g : B → A), f ∘ g ~ id
def biinv {A : Type u} {B : Type v} (f : A → B) :=
linv f × rinv f
def Equiv (A : Type u) (B : Type v) : Type (max u v) :=
Σ (f : A → B), biinv f
infix:25 " ≃ " => Equiv
def ax₁ {A : Type u} {B : Type v} : A ≃ B → contr A → contr B :=
sorry
def ax₂ {A : Type v} {B : Type u} (f g : A → B) : (Σ x, f x = g x) ≃ (Σ x, g x = f x) :=
sorry
def ax₄ {A : Type u} {B : Type v} (w : Qinv.eqv A B) (b : B) : contr (Σ a, b = w.1 a) :=
sorry
def ax₃ {A : Type u} (a : A) : contr (singl a) :=
sorry
def corrOfQinv {A : Type u} {B : Type v} : Qinv.eqv A B → Corr A B :=
by { intro w; exists (λ a b => b = w.1 a); apply Prod.mk <;> intro x;
apply ax₁; apply ax₂; apply ax₃; apply ax₄ }
|
19ad68455b23826272e51abf72be2e50987439c1 | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /hott/init/num.hlean | 57c6e740193e722b12fbcb8f31f3eba1ebf8eab8 | [
"Apache-2.0"
] | permissive | jroesch/lean | 30ef0860fa905d35b9ad6f76de1a4f65c9af6871 | 3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2 | refs/heads/master | 1,586,090,835,348 | 1,455,142,203,000 | 1,455,142,277,000 | 51,536,958 | 1 | 0 | null | 1,455,215,811,000 | 1,455,215,811,000 | null | UTF-8 | Lean | false | false | 2,049 | hlean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.bool
open bool algebra
namespace pos_num
protected definition mul (a b : pos_num) : pos_num :=
pos_num.rec_on a
b
(λn r, bit0 r + b)
(λn r, bit0 r)
definition lt (a b : pos_num) : bool :=
pos_num.rec_on a
(λ b, pos_num.cases_on b
ff
(λm, tt)
(λm, tt))
(λn f b, pos_num.cases_on b
ff
(λm, f m)
(λm, f m))
(λn f b, pos_num.cases_on b
ff
(λm, f (succ m))
(λm, f m))
b
definition le (a b : pos_num) : bool :=
pos_num.lt a (succ b)
end pos_num
definition pos_num_has_mul [instance] [reducible] : has_mul pos_num :=
has_mul.mk pos_num.mul
namespace num
open pos_num
definition pred (a : num) : num :=
num.rec_on a zero (λp, cond (is_one p) zero (pos (pred p)))
definition size (a : num) : num :=
num.rec_on a (pos one) (λp, pos (size p))
protected definition mul (a b : num) : num :=
num.rec_on a zero (λpa, num.rec_on b zero (λpb, pos (pos_num.mul pa pb)))
end num
definition num_has_mul [instance] [reducible] : has_mul num :=
has_mul.mk num.mul
namespace num
protected definition le (a b : num) : bool :=
num.rec_on a tt (λpa, num.rec_on b ff (λpb, pos_num.le pa pb))
private definition psub (a b : pos_num) : num :=
pos_num.rec_on a
(λb, zero)
(λn f b,
cond (pos_num.le (bit1 n) b)
zero
(pos_num.cases_on b
(pos (bit0 n))
(λm, 2 * f m)
(λm, 2 * f m + 1)))
(λn f b,
cond (pos_num.le (bit0 n) b)
zero
(pos_num.cases_on b
(pos (pos_num.pred (bit0 n)))
(λm, pred (2 * f m))
(λm, 2 * f m)))
b
protected definition sub (a b : num) : num :=
num.rec_on a zero (λpa, num.rec_on b a (λpb, psub pa pb))
end num
definition num_has_sub [instance] [reducible] : has_sub num :=
has_sub.mk num.sub
|
5106f7b4969fbdaf8d596a12393841f7046eb91b | f5f7e6fae601a5fe3cac7cc3ed353ed781d62419 | /src/field_theory/finite.lean | 4770195b137b4297b815df73b537a5d6eafa1c26 | [
"Apache-2.0"
] | permissive | EdAyers/mathlib | 9ecfb2f14bd6caad748b64c9c131befbff0fb4e0 | ca5d4c1f16f9c451cf7170b10105d0051db79e1b | refs/heads/master | 1,626,189,395,845 | 1,555,284,396,000 | 1,555,284,396,000 | 144,004,030 | 0 | 0 | Apache-2.0 | 1,533,727,664,000 | 1,533,727,663,000 | null | UTF-8 | Lean | false | false | 3,584 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import group_theory.order_of_element data.nat.totient data.polynomial data.equiv.algebra
universes u v
variables {α : Type u} {β : Type v}
open function finset polynomial nat
section
variables [integral_domain α] [decidable_eq α] (S : set (units α)) [is_subgroup S] [fintype S]
lemma card_nth_roots_subgroup_units {n : ℕ} (hn : 0 < n) (a : S) :
(univ.filter (λ b : S, b ^ n = a)).card ≤ (nth_roots n ((a : units α) : α)).card :=
card_le_card_of_inj_on (λ a, ((a : units α) : α))
(by simp [mem_nth_roots hn, (units.coe_pow _ _).symm, -units.coe_pow, units.ext_iff.symm, subtype.coe_ext])
(by simp [units.ext_iff.symm, subtype.coe_ext.symm])
instance subgroup_units_cyclic : is_cyclic S :=
by haveI := classical.dec_eq α; exact
is_cyclic_of_card_pow_eq_one_le
(λ n hn, le_trans (card_nth_roots_subgroup_units S hn 1) (card_nth_roots _ _))
end
namespace finite_field
def field_of_integral_domain [fintype α] [decidable_eq α] [integral_domain α] :
discrete_field α :=
{ has_decidable_eq := by apply_instance,
inv := λ a, if h : a = 0 then 0
else fintype.bij_inv (show function.bijective (* a),
from fintype.injective_iff_bijective.1 $ λ _ _, (domain.mul_right_inj h).1) 1,
inv_mul_cancel := λ a ha, show dite _ _ _ * a = _, by rw dif_neg ha;
exact fintype.right_inverse_bij_inv (show function.bijective (* a), from _) 1,
mul_inv_cancel := λ a ha, show a * dite _ _ _ = _, by rw [dif_neg ha, mul_comm];
exact fintype.right_inverse_bij_inv (show function.bijective (* a), from _) 1,
inv_zero := dif_pos rfl,
..show integral_domain α, by apply_instance }
section
variables [field α] [fintype α]
instance [decidable_eq α] : fintype (units α) :=
by haveI := set_fintype {a : α | a ≠ 0}; exact
fintype.of_equiv _ (equiv.units_equiv_ne_zero α).symm
lemma card_units [decidable_eq α] : fintype.card (units α) = fintype.card α - 1 :=
begin
rw [eq_comm, nat.sub_eq_iff_eq_add (fintype.card_pos_iff.2 ⟨(0 : α)⟩)],
haveI := set_fintype {a : α | a ≠ 0},
haveI := set_fintype (@set.univ α),
rw [fintype.card_congr (equiv.units_equiv_ne_zero _),
← @set.card_insert _ _ {a : α | a ≠ 0} _ (not_not.2 (eq.refl (0 : α)))
(set.fintype_insert _ _), fintype.card_congr (equiv.set.univ α).symm],
congr; simp [set.ext_iff, classical.em]
end
instance : is_cyclic (units α) :=
by haveI := classical.dec_eq α;
haveI := set_fintype (@set.univ (units α)); exact
let ⟨g, hg⟩ := is_cyclic.exists_generator (@set.univ (units α)) in
⟨⟨g, λ x, let ⟨n, hn⟩ := hg ⟨x, trivial⟩ in ⟨n, by rw [← is_subgroup.coe_gpow, hn]; refl⟩⟩⟩
lemma prod_univ_units_id_eq_neg_one [decidable_eq α] :
univ.prod (λ x, x) = (-1 : units α) :=
have ((@univ (units α) _).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),
by rw [← insert_erase (mem_univ (-1 : units α)), prod_insert (not_mem_erase _ _),
this, mul_one]
end
lemma pow_card_sub_one_eq_one [discrete_field α] [fintype α] (a : α) (ha : a ≠ 0) :
a ^ (fintype.card α - 1) = 1 :=
calc a ^ (fintype.card α - 1) = (units.mk0 a ha ^ (fintype.card α - 1) : units α) :
by rw [units.coe_pow, units.mk0_val]
... = 1 : by rw [← card_units, pow_card_eq_one]; refl
end finite_field
|
99aeaae531ccd210c353c127de2744f710e93137 | 1a61aba1b67cddccce19532a9596efe44be4285f | /library/data/int/gcd.lean | 502c00bb09eb056926b9d5b9af74838907395f1a | [
"Apache-2.0"
] | permissive | eigengrau/lean | 07986a0f2548688c13ba36231f6cdbee82abf4c6 | f8a773be1112015e2d232661ce616d23f12874d0 | refs/heads/master | 1,610,939,198,566 | 1,441,352,386,000 | 1,441,352,494,000 | 41,903,576 | 0 | 0 | null | 1,441,352,210,000 | 1,441,352,210,000 | null | UTF-8 | Lean | false | false | 14,581 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura
Definitions and properties of gcd, lcm, and coprime.
-/
import .div data.nat.gcd
open eq.ops
namespace int
/- gcd -/
definition gcd (a b : ℤ) : ℤ := of_nat (nat.gcd (nat_abs a) (nat_abs b))
theorem gcd_nonneg (a b : ℤ) : gcd a b ≥ 0 :=
of_nat_nonneg (nat.gcd (nat_abs a) (nat_abs b))
theorem gcd.comm (a b : ℤ) : gcd a b = gcd b a :=
by rewrite [↑gcd, nat.gcd.comm]
theorem gcd_zero_right (a : ℤ) : gcd a 0 = abs a :=
by krewrite [↑gcd, nat.gcd_zero_right, of_nat_nat_abs]
theorem gcd_zero_left (a : ℤ) : gcd 0 a = abs a :=
by rewrite [gcd.comm, gcd_zero_right]
theorem gcd_one_right (a : ℤ) : gcd a 1 = 1 :=
by krewrite [↑gcd, nat.gcd_one_right]
theorem gcd_one_left (a : ℤ) : gcd 1 a = 1 :=
by rewrite [gcd.comm, gcd_one_right]
theorem gcd_abs_left (a b : ℤ) : gcd (abs a) b = gcd a b :=
by rewrite [↑gcd, *nat_abs_abs]
theorem gcd_abs_right (a b : ℤ) : gcd (abs a) b = gcd a b :=
by rewrite [↑gcd, *nat_abs_abs]
theorem gcd_abs_abs (a b : ℤ) : gcd (abs a) (abs b) = gcd a b :=
by rewrite [↑gcd, *nat_abs_abs]
theorem gcd_of_ne_zero (a : ℤ) {b : ℤ} (H : b ≠ 0) : gcd a b = gcd b (abs a mod abs b) :=
have nat_abs b ≠ nat.zero, from assume H', H (eq_zero_of_nat_abs_eq_zero H'),
have (#nat nat_abs b > nat.zero), from nat.pos_of_ne_zero this,
assert nat.gcd (nat_abs a) (nat_abs b) = (#nat nat.gcd (nat_abs b) (nat_abs a mod nat_abs b)),
from @nat.gcd_of_pos (nat_abs a) (nat_abs b) this,
calc
gcd a b = nat.gcd (nat_abs b) (#nat nat_abs a mod nat_abs b) : by rewrite [↑gcd, this]
... = gcd (abs b) (abs a mod abs b) :
by rewrite [↑gcd, -*of_nat_nat_abs, of_nat_mod]
... = gcd b (abs a mod abs b) : by rewrite [↑gcd, *nat_abs_abs]
theorem gcd_of_pos (a : ℤ) {b : ℤ} (H : b > 0) : gcd a b = gcd b (abs a mod b) :=
by rewrite [!gcd_of_ne_zero (ne_of_gt H), abs_of_pos H]
theorem gcd_of_nonneg_of_pos {a b : ℤ} (H1 : a ≥ 0) (H2 : b > 0) : gcd a b = gcd b (a mod b) :=
by rewrite [!gcd_of_pos H2, abs_of_nonneg H1]
theorem gcd_self (a : ℤ) : gcd a a = abs a :=
by rewrite [↑gcd, nat.gcd_self, of_nat_nat_abs]
theorem gcd_dvd_left (a b : ℤ) : gcd a b ∣ a :=
have gcd a b ∣ abs a,
by rewrite [↑gcd, -of_nat_nat_abs, of_nat_dvd_of_nat]; apply nat.gcd_dvd_left,
iff.mp !dvd_abs_iff this
theorem gcd_dvd_right (a b : ℤ) : gcd a b ∣ b :=
by rewrite gcd.comm; apply gcd_dvd_left
theorem dvd_gcd {a b c : ℤ} : a ∣ b → a ∣ c → a ∣ gcd b c :=
begin
rewrite [↑gcd, -*(abs_dvd_iff a), -(dvd_abs_iff _ b), -(dvd_abs_iff _ c), -*of_nat_nat_abs],
rewrite [*of_nat_dvd_of_nat] ,
apply nat.dvd_gcd
end
theorem gcd.assoc (a b c : ℤ) : gcd (gcd a b) c = gcd a (gcd b c) :=
dvd.antisymm !gcd_nonneg !gcd_nonneg
(dvd_gcd
(dvd.trans !gcd_dvd_left !gcd_dvd_left)
(dvd_gcd (dvd.trans !gcd_dvd_left !gcd_dvd_right) !gcd_dvd_right))
(dvd_gcd
(dvd_gcd !gcd_dvd_left (dvd.trans !gcd_dvd_right !gcd_dvd_left))
(dvd.trans !gcd_dvd_right !gcd_dvd_right))
theorem gcd_mul_left (a b c : ℤ) : gcd (a * b) (a * c) = abs a * gcd b c :=
by rewrite [↑gcd, *nat_abs_mul, nat.gcd_mul_left, of_nat_mul, of_nat_nat_abs]
theorem gcd_mul_right (a b c : ℤ) : gcd (a * b) (c * b) = gcd a c * abs b :=
by rewrite [mul.comm a, mul.comm c, mul.comm (gcd a c), gcd_mul_left]
theorem gcd_pos_of_ne_zero_left {a : ℤ} (b : ℤ) (H : a ≠ 0) : gcd a b > 0 :=
have gcd a b ≠ 0, from
suppose gcd a b = 0,
have 0 ∣ a, from this ▸ gcd_dvd_left a b,
show false, from H (eq_zero_of_zero_dvd this),
lt_of_le_of_ne (gcd_nonneg a b) (ne.symm this)
theorem gcd_pos_of_ne_zero_right (a : ℤ) {b : ℤ} (H : b ≠ 0) : gcd a b > 0 :=
by rewrite gcd.comm; apply !gcd_pos_of_ne_zero_left H
theorem eq_zero_of_gcd_eq_zero_left {a b : ℤ} (H : gcd a b = 0) : a = 0 :=
decidable.by_contradiction
(suppose a ≠ 0,
have gcd a b > 0, from !gcd_pos_of_ne_zero_left this,
ne_of_lt this H⁻¹)
theorem eq_zero_of_gcd_eq_zero_right {a b : ℤ} (H : gcd a b = 0) : b = 0 :=
by rewrite gcd.comm at H; apply !eq_zero_of_gcd_eq_zero_left H
theorem gcd_div {a b c : ℤ} (H1 : c ∣ a) (H2 : c ∣ b) :
gcd (a div c) (b div c) = gcd a b div (abs c) :=
decidable.by_cases
(suppose c = 0,
calc
gcd (a div c) (b div c) = gcd 0 0 : by subst c; rewrite *div_zero
... = 0 : gcd_zero_left
... = gcd a b div 0 : div_zero
... = gcd a b div (abs c) : by subst c)
(suppose c ≠ 0,
have abs c ≠ 0, from assume H', this (eq_zero_of_abs_eq_zero H'),
eq.symm (div_eq_of_eq_mul_left this
(eq.symm (calc
gcd (a div c) (b div c) * abs c = gcd (a div c * c) (b div c * c) : gcd_mul_right
... = gcd a (b div c * c) : div_mul_cancel H1
... = gcd a b : div_mul_cancel H2))))
theorem gcd_dvd_gcd_mul_left (a b c : ℤ) : gcd a b ∣ gcd (c * a) b :=
dvd_gcd (dvd.trans !gcd_dvd_left !dvd_mul_left) !gcd_dvd_right
theorem gcd_dvd_gcd_mul_right (a b c : ℤ) : gcd a b ∣ gcd (a * c) b :=
!mul.comm ▸ !gcd_dvd_gcd_mul_left
theorem div_gcd_eq_div_gcd_of_nonneg {a₁ b₁ a₂ b₂ : ℤ} (H : a₁ * b₂ = a₂ * b₁)
(H1 : b₁ ≠ 0) (H2 : b₂ ≠ 0) (H3 : a₁ ≥ 0) (H4 : a₂ ≥ 0) :
a₁ div (gcd a₁ b₁) = a₂ div (gcd a₂ b₂) :=
begin
apply div_eq_div_of_dvd_of_dvd,
repeat (apply gcd_dvd_left),
intro H', apply H1, apply eq_zero_of_gcd_eq_zero_right H',
intro H', apply H2, apply eq_zero_of_gcd_eq_zero_right H',
rewrite [-abs_of_nonneg H3 at {1}, -abs_of_nonneg H4 at {2}],
rewrite [-gcd_mul_left, -gcd_mul_right, H, mul.comm b₁]
end
theorem div_gcd_eq_div_gcd {a₁ b₁ a₂ b₂ : ℤ} (H : a₁ * b₂ = a₂ * b₁) (H1 : b₁ > 0) (H2 : b₂ > 0) :
a₁ div (gcd a₁ b₁) = a₂ div (gcd a₂ b₂) :=
or.elim (le_or_gt 0 a₁)
(assume H3 : a₁ ≥ 0,
have H4 : a₂ * b₁ ≥ 0, by rewrite -H; apply mul_nonneg H3 (le_of_lt H2),
have H5 : a₂ ≥ 0, from nonneg_of_mul_nonneg_right H4 H1,
div_gcd_eq_div_gcd_of_nonneg H (ne_of_gt H1) (ne_of_gt H2) H3 H5)
(assume H3 : a₁ < 0,
have H4 : a₂ * b₁ < 0, by rewrite -H; apply mul_neg_of_neg_of_pos H3 H2,
assert H5 : a₂ < 0, from neg_of_mul_neg_right H4 (le_of_lt H1),
assert H6 : abs a₁ div (gcd (abs a₁) (abs b₁)) = abs a₂ div (gcd (abs a₂) (abs b₂)),
begin
apply div_gcd_eq_div_gcd_of_nonneg,
rewrite [abs_of_pos H1, abs_of_pos H2, abs_of_neg H3, abs_of_neg H5],
rewrite [-*neg_mul_eq_neg_mul, H],
apply ne_of_gt (abs_pos_of_pos H1),
apply ne_of_gt (abs_pos_of_pos H2),
repeat (apply abs_nonneg)
end,
have H7 : -a₁ div (gcd a₁ b₁) = -a₂ div (gcd a₂ b₂),
begin
rewrite [-abs_of_neg H3, -abs_of_neg H5, -gcd_abs_abs a₁],
rewrite [-gcd_abs_abs a₂ b₂],
exact H6
end,
calc
a₁ div (gcd a₁ b₁) = -(-a₁ div (gcd a₁ b₁)) :
by rewrite [neg_div_of_dvd !gcd_dvd_left, neg_neg]
... = -(-a₂ div (gcd a₂ b₂)) : H7
... = a₂ div (gcd a₂ b₂) :
by rewrite [neg_div_of_dvd !gcd_dvd_left, neg_neg])
/- lcm -/
definition lcm (a b : ℤ) : ℤ := of_nat (nat.lcm (nat_abs a) (nat_abs b))
theorem lcm_nonneg (a b : ℤ) : lcm a b ≥ 0 :=
of_nat_nonneg (nat.lcm (nat_abs a) (nat_abs b))
theorem lcm.comm (a b : ℤ) : lcm a b = lcm b a :=
by rewrite [↑lcm, nat.lcm.comm]
theorem lcm_zero_left (a : ℤ) : lcm 0 a = 0 :=
by krewrite [↑lcm, nat.lcm_zero_left]
theorem lcm_zero_right (a : ℤ) : lcm a 0 = 0 :=
!lcm.comm ▸ !lcm_zero_left
theorem lcm_one_left (a : ℤ) : lcm 1 a = abs a :=
by krewrite [↑lcm, nat.lcm_one_left, of_nat_nat_abs]
theorem lcm_one_right (a : ℤ) : lcm a 1 = abs a :=
!lcm.comm ▸ !lcm_one_left
theorem lcm_abs_left (a b : ℤ) : lcm (abs a) b = lcm a b :=
by rewrite [↑lcm, *nat_abs_abs]
theorem lcm_abs_right (a b : ℤ) : lcm (abs a) b = lcm a b :=
by rewrite [↑lcm, *nat_abs_abs]
theorem lcm_abs_abs (a b : ℤ) : lcm (abs a) (abs b) = lcm a b :=
by rewrite [↑lcm, *nat_abs_abs]
theorem lcm_self (a : ℤ) : lcm a a = abs a :=
by krewrite [↑lcm, nat.lcm_self, of_nat_nat_abs]
theorem dvd_lcm_left (a b : ℤ) : a ∣ lcm a b :=
by rewrite [↑lcm, -abs_dvd_iff, -of_nat_nat_abs, of_nat_dvd_of_nat]; apply nat.dvd_lcm_left
theorem dvd_lcm_right (a b : ℤ) : b ∣ lcm a b :=
!lcm.comm ▸ !dvd_lcm_left
theorem gcd_mul_lcm (a b : ℤ) : gcd a b * lcm a b = abs (a * b) :=
begin
rewrite [↑gcd, ↑lcm, -of_nat_nat_abs, -of_nat_mul, of_nat_eq_of_nat, nat_abs_mul],
apply nat.gcd_mul_lcm
end
theorem lcm_dvd {a b c : ℤ} : a ∣ c → b ∣ c → lcm a b ∣ c :=
begin
rewrite [↑lcm, -(abs_dvd_iff a), -(abs_dvd_iff b), -*(dvd_abs_iff _ c), -*of_nat_nat_abs],
rewrite [*of_nat_dvd_of_nat] ,
apply nat.lcm_dvd
end
theorem lcm_assoc (a b c : ℤ) : lcm (lcm a b) c = lcm a (lcm b c) :=
dvd.antisymm !lcm_nonneg !lcm_nonneg
(lcm_dvd
(lcm_dvd !dvd_lcm_left (dvd.trans !dvd_lcm_left !dvd_lcm_right))
(dvd.trans !dvd_lcm_right !dvd_lcm_right))
(lcm_dvd
(dvd.trans !dvd_lcm_left !dvd_lcm_left)
(lcm_dvd (dvd.trans !dvd_lcm_right !dvd_lcm_left) !dvd_lcm_right))
/- coprime -/
abbreviation coprime (a b : ℤ) : Prop := gcd a b = 1
theorem coprime_swap {a b : ℤ} (H : coprime b a) : coprime a b :=
!gcd.comm ▸ H
theorem dvd_of_coprime_of_dvd_mul_right {a b c : ℤ} (H1 : coprime c b) (H2 : c ∣ a * b) : c ∣ a :=
assert H3 : gcd (a * c) (a * b) = abs a, from
calc
gcd (a * c) (a * b) = abs a * gcd c b : gcd_mul_left
... = abs a * 1 : H1
... = abs a : mul_one,
assert H4 : (c ∣ gcd (a * c) (a * b)), from dvd_gcd !dvd_mul_left H2,
by rewrite [-dvd_abs_iff, -H3]; apply H4
theorem dvd_of_coprime_of_dvd_mul_left {a b c : ℤ} (H1 : coprime c a) (H2 : c ∣ a * b) : c ∣ b :=
dvd_of_coprime_of_dvd_mul_right H1 (!mul.comm ▸ H2)
theorem gcd_mul_left_cancel_of_coprime {c : ℤ} (a : ℤ) {b : ℤ} (H : coprime c b) :
gcd (c * a) b = gcd a b :=
begin
revert H, rewrite [↑coprime, ↑gcd, *of_nat_eq_of_nat, nat_abs_mul],
apply nat.gcd_mul_left_cancel_of_coprime
end
theorem gcd_mul_right_cancel_of_coprime (a : ℤ) {c b : ℤ} (H : coprime c b) :
gcd (a * c) b = gcd a b :=
!mul.comm ▸ !gcd_mul_left_cancel_of_coprime H
theorem gcd_mul_left_cancel_of_coprime_right {c a : ℤ} (b : ℤ) (H : coprime c a) :
gcd a (c * b) = gcd a b :=
!gcd.comm ▸ !gcd.comm ▸ !gcd_mul_left_cancel_of_coprime H
theorem gcd_mul_right_cancel_of_coprime_right {c a : ℤ} (b : ℤ) (H : coprime c a) :
gcd a (b * c) = gcd a b :=
!gcd.comm ▸ !gcd.comm ▸ !gcd_mul_right_cancel_of_coprime H
theorem coprime_div_gcd_div_gcd {a b : ℤ} (H : gcd a b ≠ 0) :
coprime (a div gcd a b) (b div gcd a b) :=
calc
gcd (a div gcd a b) (b div gcd a b)
= gcd a b div abs (gcd a b) : gcd_div !gcd_dvd_left !gcd_dvd_right
... = 1 : by rewrite [abs_of_nonneg !gcd_nonneg, div_self H]
theorem not_coprime_of_dvd_of_dvd {m n d : ℤ} (dgt1 : d > 1) (Hm : d ∣ m) (Hn : d ∣ n) :
¬ coprime m n :=
assume co : coprime m n,
assert d ∣ gcd m n, from dvd_gcd Hm Hn,
have d ∣ 1, by rewrite [↑coprime at co, co at this]; apply this,
have d ≤ 1, from le_of_dvd dec_trivial this,
show false, from not_lt_of_ge `d ≤ 1` `d > 1`
theorem exists_coprime {a b : ℤ} (H : gcd a b ≠ 0) :
exists a' b', coprime a' b' ∧ a = a' * gcd a b ∧ b = b' * gcd a b :=
have H1 : a = (a div gcd a b) * gcd a b, from (div_mul_cancel !gcd_dvd_left)⁻¹,
have H2 : b = (b div gcd a b) * gcd a b, from (div_mul_cancel !gcd_dvd_right)⁻¹,
exists.intro _ (exists.intro _ (and.intro (coprime_div_gcd_div_gcd H) (and.intro H1 H2)))
theorem coprime_mul {a b c : ℤ} (H1 : coprime a c) (H2 : coprime b c) : coprime (a * b) c :=
calc
gcd (a * b) c = gcd b c : !gcd_mul_left_cancel_of_coprime H1
... = 1 : H2
theorem coprime_mul_right {c a b : ℤ} (H1 : coprime c a) (H2 : coprime c b) : coprime c (a * b) :=
coprime_swap (coprime_mul (coprime_swap H1) (coprime_swap H2))
theorem coprime_of_coprime_mul_left {c a b : ℤ} (H : coprime (c * a) b) : coprime a b :=
have H1 : (gcd a b ∣ gcd (c * a) b), from !gcd_dvd_gcd_mul_left,
eq_one_of_dvd_one !gcd_nonneg (H ▸ H1)
theorem coprime_of_coprime_mul_right {c a b : ℤ} (H : coprime (a * c) b) : coprime a b :=
coprime_of_coprime_mul_left (!mul.comm ▸ H)
theorem coprime_of_coprime_mul_left_right {c a b : ℤ} (H : coprime a (c * b)) : coprime a b :=
coprime_swap (coprime_of_coprime_mul_left (coprime_swap H))
theorem coprime_of_coprime_mul_right_right {c a b : ℤ} (H : coprime a (b * c)) : coprime a b :=
coprime_of_coprime_mul_left_right (!mul.comm ▸ H)
theorem exists_eq_prod_and_dvd_and_dvd {a b c} (H : c ∣ a * b) :
∃ a' b', c = a' * b' ∧ a' ∣ a ∧ b' ∣ b :=
decidable.by_cases
(suppose gcd c a = 0,
have c = 0, from eq_zero_of_gcd_eq_zero_left `gcd c a = 0`,
have a = 0, from eq_zero_of_gcd_eq_zero_right `gcd c a = 0`,
have c = 0 * b, from `c = 0` ⬝ !zero_mul⁻¹,
have 0 ∣ a, from `a = 0`⁻¹ ▸ !dvd.refl,
have b ∣ b, from !dvd.refl,
exists.intro _ (exists.intro _ (and.intro `c = 0 * b` (and.intro `0 ∣ a` `b ∣ b`))))
(suppose gcd c a ≠ 0,
have gcd c a ∣ c, from !gcd_dvd_left,
have H3 : c div gcd c a ∣ (a * b) div gcd c a, from div_dvd_div this H,
have H4 : (a * b) div gcd c a = (a div gcd c a) * b, from
calc
a * b div gcd c a = b * a div gcd c a : mul.comm
... = b * (a div gcd c a) : !mul_div_assoc !gcd_dvd_right
... = a div gcd c a * b : mul.comm,
have H5 : c div gcd c a ∣ (a div gcd c a) * b, from H4 ▸ H3,
have H6 : coprime (c div gcd c a) (a div gcd c a), from coprime_div_gcd_div_gcd `gcd c a ≠ 0`,
have H7 : c div gcd c a ∣ b, from dvd_of_coprime_of_dvd_mul_left H6 H5,
have H8 : c = gcd c a * (c div gcd c a), from (mul_div_cancel' `gcd c a ∣ c`)⁻¹,
exists.intro _ (exists.intro _ (and.intro H8 (and.intro !gcd_dvd_right H7))))
end int
|
29b0e7c4687bb9bd6baa2edce94a9abe1f284de6 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/algebra/category/Group/images_auto.lean | 106c31d2a58c71f38292be9f5e73a83849bd2eb3 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,510 | 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.algebra.category.Group.abelian
import Mathlib.category_theory.limits.shapes.images
import Mathlib.category_theory.limits.types
import Mathlib.PostPort
namespace Mathlib
/-!
# The category of commutative additive groups has images.
Note that we don't need to register any of the constructions here as instances, because we get them
from the fact that `AddCommGroup` is an abelian category.
-/
namespace AddCommGroup
-- Note that because `injective_of_mono` is currently only proved in `Type 0`,
-- we restrict to the lowest universe here for now.
/-- the image of a morphism in AddCommGroup is just the bundling of `add_monoid_hom.range f` -/
def image {G : AddCommGroup} {H : AddCommGroup} (f : G ⟶ H) : AddCommGroup :=
of ↥(add_monoid_hom.range f)
/-- the inclusion of `image f` into the target -/
def image.ι {G : AddCommGroup} {H : AddCommGroup} (f : G ⟶ H) : image f ⟶ H :=
add_subgroup.subtype (add_monoid_hom.range f)
protected instance image.ι.category_theory.mono {G : AddCommGroup} {H : AddCommGroup} (f : G ⟶ H) :
category_theory.mono (image.ι f) :=
category_theory.concrete_category.mono_of_injective (image.ι f) subtype.val_injective
/-- the corestriction map to the image -/
def factor_thru_image {G : AddCommGroup} {H : AddCommGroup} (f : G ⟶ H) : G ⟶ image f :=
add_monoid_hom.to_range f
theorem image.fac {G : AddCommGroup} {H : AddCommGroup} (f : G ⟶ H) :
factor_thru_image f ≫ image.ι f = f :=
ext G H (factor_thru_image f ≫ image.ι f) f
fun (x : ↥G) => Eq.refl (coe_fn (factor_thru_image f ≫ image.ι f) x)
/-- the universal property for the image factorisation -/
def image.lift {G : AddCommGroup} {H : AddCommGroup} {f : G ⟶ H}
(F' : category_theory.limits.mono_factorisation f) :
image f ⟶ category_theory.limits.mono_factorisation.I F' :=
add_monoid_hom.mk
(fun (x : ↥(image f)) =>
coe_fn (category_theory.limits.mono_factorisation.e F')
(subtype.val
(classical.indefinite_description (fun (x_1 : ↥G) => coe_fn f x_1 = subtype.val x)
sorry)))
sorry sorry
theorem image.lift_fac {G : AddCommGroup} {H : AddCommGroup} {f : G ⟶ H}
(F' : category_theory.limits.mono_factorisation f) :
image.lift F' ≫ category_theory.limits.mono_factorisation.m F' = image.ι f :=
sorry
/-- the factorisation of any morphism in AddCommGroup through a mono. -/
def mono_factorisation {G : AddCommGroup} {H : AddCommGroup} (f : G ⟶ H) :
category_theory.limits.mono_factorisation f :=
category_theory.limits.mono_factorisation.mk (image f) (image.ι f) (factor_thru_image f)
/-- the factorisation of any morphism in AddCommGroup through a mono has the universal property of
the image. -/
def is_image {G : AddCommGroup} {H : AddCommGroup} (f : G ⟶ H) :
category_theory.limits.is_image (mono_factorisation f) :=
category_theory.limits.is_image.mk image.lift
/--
The categorical image of a morphism in `AddCommGroup`
agrees with the usual group-theoretical range.
-/
def image_iso_range {G : AddCommGroup} {H : AddCommGroup} (f : G ⟶ H) :
category_theory.limits.image f ≅ of ↥(add_monoid_hom.range f) :=
category_theory.limits.is_image.iso_ext (category_theory.limits.image.is_image f) (is_image f)
end Mathlib |
f6b97d1a79b131cf5b7a7dec964449b899ddd4bf | 969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb | /src/algebra/ring/basic.lean | 125223110df3190d00cad2bb73502f7e90f9c07e | [
"Apache-2.0"
] | permissive | SAAluthwela/mathlib | 62044349d72dd63983a8500214736aa7779634d3 | 83a4b8b990907291421de54a78988c024dc8a552 | refs/heads/master | 1,679,433,873,417 | 1,615,998,031,000 | 1,615,998,031,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 42,972 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Amelia Livingston, Yury Kudryashov,
Neil Strickland
-/
import algebra.divisibility
import data.set.basic
/-!
# Properties and homomorphisms of semirings and rings
This file proves simple properties of semirings, rings and domains and their unit groups. It also
defines bundled homomorphisms of semirings and rings. As with monoid and groups, we use the same
structure `ring_hom a β`, a.k.a. `α →+* β`, for both homomorphism types.
The unbundled homomorphisms are defined in `deprecated/ring`. They are deprecated and the plan is to
slowly remove them from mathlib.
## Main definitions
ring_hom, nonzero, domain, integral_domain
## Notations
→+* for bundled ring homs (also use for semiring homs)
## Implementation notes
There's a coercion from bundled homs to fun, and the canonical
notation is to use the bundled hom as a function via this coercion.
There is no `semiring_hom` -- the idea is that `ring_hom` is used.
The constructor for a `ring_hom` between semirings needs a proof of `map_zero`, `map_one` and
`map_add` as well as `map_mul`; a separate constructor `ring_hom.mk'` will construct ring homs
between rings from monoid homs given only a proof that addition is preserved.
## Tags
`ring_hom`, `semiring_hom`, `semiring`, `comm_semiring`, `ring`, `comm_ring`, `domain`,
`integral_domain`, `nonzero`, `units`
-/
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {R : Type x}
set_option old_structure_cmd true
open function
/-!
### `distrib` class
-/
/-- A typeclass stating that multiplication is left and right distributive
over addition. -/
@[protect_proj, ancestor has_mul has_add]
class distrib (R : Type*) extends has_mul R, has_add R :=
(left_distrib : ∀ a b c : R, a * (b + c) = (a * b) + (a * c))
(right_distrib : ∀ a b c : R, (a + b) * c = (a * c) + (b * c))
lemma left_distrib [distrib R] (a b c : R) : a * (b + c) = a * b + a * c :=
distrib.left_distrib a b c
alias left_distrib ← mul_add
lemma right_distrib [distrib R] (a b c : R) : (a + b) * c = a * c + b * c :=
distrib.right_distrib a b c
alias right_distrib ← add_mul
/-- Pullback a `distrib` instance along an injective function. -/
protected def function.injective.distrib {S} [has_mul R] [has_add R] [distrib S]
(f : R → S) (hf : injective f) (add : ∀ x y, f (x + y) = f x + f y)
(mul : ∀ x y, f (x * y) = f x * f y) :
distrib R :=
{ mul := (*),
add := (+),
left_distrib := λ x y z, hf $ by simp only [*, left_distrib],
right_distrib := λ x y z, hf $ by simp only [*, right_distrib] }
/-- Pushforward a `distrib` instance along a surjective function. -/
protected def function.surjective.distrib {S} [distrib R] [has_add S] [has_mul S]
(f : R → S) (hf : surjective f) (add : ∀ x y, f (x + y) = f x + f y)
(mul : ∀ x y, f (x * y) = f x * f y) :
distrib S :=
{ mul := (*),
add := (+),
left_distrib := hf.forall₃.2 $ λ x y z, by simp only [← add, ← mul, left_distrib],
right_distrib := hf.forall₃.2 $ λ x y z, by simp only [← add, ← mul, right_distrib] }
/-!
### Semirings
-/
/-- A semiring is a type with the following structures: additive commutative monoid
(`add_comm_monoid`), multiplicative monoid (`monoid`), distributive laws (`distrib`), and
multiplication by zero law (`mul_zero_class`). The actual definition extends `monoid_with_zero`
instead of `monoid` and `mul_zero_class`. -/
@[protect_proj, ancestor add_comm_monoid monoid_with_zero distrib]
class semiring (α : Type u) extends add_comm_monoid α, monoid_with_zero α, distrib α
section semiring
variables [semiring α]
/-- Pullback a `semiring` instance along an injective function. -/
protected def function.injective.semiring [has_zero β] [has_one β] [has_add β] [has_mul β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
semiring β :=
{ .. hf.monoid_with_zero f zero one mul, .. hf.add_comm_monoid f zero add,
.. hf.distrib f add mul }
/-- Pushforward a `semiring` instance along a surjective function. -/
protected def function.surjective.semiring [has_zero β] [has_one β] [has_add β] [has_mul β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
semiring β :=
{ .. hf.monoid_with_zero f zero one mul, .. hf.add_comm_monoid f zero add,
.. hf.distrib f add mul }
lemma one_add_one_eq_two : 1 + 1 = (2 : α) :=
by unfold bit0
theorem two_mul (n : α) : 2 * n = n + n :=
eq.trans (right_distrib 1 1 n) (by simp)
lemma distrib_three_right (a b c d : α) : (a + b + c) * d = a * d + b * d + c * d :=
by simp [right_distrib]
theorem mul_two (n : α) : n * 2 = n + n :=
(left_distrib n 1 1).trans (by simp)
theorem bit0_eq_two_mul (n : α) : bit0 n = 2 * n :=
(two_mul _).symm
@[to_additive] lemma mul_ite {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) :
a * (if P then b else c) = if P then a * b else a * c :=
by split_ifs; refl
@[to_additive] lemma ite_mul {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) :
(if P then a else b) * c = if P then a * c else b * c :=
by split_ifs; refl
-- We make `mul_ite` and `ite_mul` simp lemmas,
-- but not `add_ite` or `ite_add`.
-- The problem we're trying to avoid is dealing with
-- summations of the form `∑ x in s, (f x + ite P 1 0)`,
-- in which `add_ite` followed by `sum_ite` would needlessly slice up
-- the `f x` terms according to whether `P` holds at `x`.
-- There doesn't appear to be a corresponding difficulty so far with
-- `mul_ite` and `ite_mul`.
attribute [simp] mul_ite ite_mul
@[simp] lemma mul_boole {α} [semiring α] (P : Prop) [decidable P] (a : α) :
a * (if P then 1 else 0) = if P then a else 0 :=
by simp
@[simp] lemma boole_mul {α} [semiring α] (P : Prop) [decidable P] (a : α) :
(if P then 1 else 0) * a = if P then a else 0 :=
by simp
lemma ite_mul_zero_left {α : Type*} [mul_zero_class α] (P : Prop) [decidable P] (a b : α) :
ite P (a * b) 0 = ite P a 0 * b :=
by { by_cases h : P; simp [h], }
lemma ite_mul_zero_right {α : Type*} [mul_zero_class α] (P : Prop) [decidable P] (a b : α) :
ite P (a * b) 0 = a * ite P b 0 :=
by { by_cases h : P; simp [h], }
/-- An element `a` of a semiring is even if there exists `k` such `a = 2*k`. -/
def even (a : α) : Prop := ∃ k, a = 2*k
lemma even_iff_two_dvd {a : α} : even a ↔ 2 ∣ a := iff.rfl
@[simp] lemma range_two_mul (α : Type*) [semiring α] :
set.range (λ x : α, 2 * x) = {a | even a} :=
by { ext x, simp [even, eq_comm] }
/-- An element `a` of a semiring is odd if there exists `k` such `a = 2*k + 1`. -/
def odd (a : α) : Prop := ∃ k, a = 2*k + 1
@[simp] lemma range_two_mul_add_one (α : Type*) [semiring α] :
set.range (λ x : α, 2 * x + 1) = {a | odd a} :=
by { ext x, simp [odd, eq_comm] }
theorem dvd_add {a b c : α} (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b + c :=
dvd.elim h₁ (λ d hd, dvd.elim h₂ (λ e he, dvd.intro (d + e) (by simp [left_distrib, hd, he])))
end semiring
namespace add_monoid_hom
/-- Left multiplication by an element of a (semi)ring is an `add_monoid_hom` -/
def mul_left {R : Type*} [semiring R] (r : R) : R →+ R :=
{ to_fun := (*) r,
map_zero' := mul_zero r,
map_add' := mul_add r }
@[simp] lemma coe_mul_left {R : Type*} [semiring R] (r : R) : ⇑(mul_left r) = (*) r := rfl
/-- Right multiplication by an element of a (semi)ring is an `add_monoid_hom` -/
def mul_right {R : Type*} [semiring R] (r : R) : R →+ R :=
{ to_fun := λ a, a * r,
map_zero' := zero_mul r,
map_add' := λ _ _, add_mul _ _ r }
@[simp] lemma coe_mul_right {R : Type*} [semiring R] (r : R) :
⇑(mul_right r) = (* r) := rfl
lemma mul_right_apply {R : Type*} [semiring R] (a r : R) :
mul_right r a = a * r := rfl
end add_monoid_hom
/-- Bundled semiring homomorphisms; use this for bundled ring homomorphisms too.
This extends from both `monoid_hom` and `monoid_with_zero_hom` in order to put the fields in a
sensible order, even though `monoid_with_zero_hom` already extends `monoid_hom`. -/
structure ring_hom (α : Type*) (β : Type*) [semiring α] [semiring β]
extends monoid_hom α β, add_monoid_hom α β, monoid_with_zero_hom α β
infixr ` →+* `:25 := ring_hom
/-- Reinterpret a ring homomorphism `f : R →+* S` as a `monoid_with_zero_hom R S`.
The `simp`-normal form is `(f : monoid_with_zero_hom R S)`. -/
add_decl_doc ring_hom.to_monoid_with_zero_hom
/-- Reinterpret a ring homomorphism `f : R →+* S` as a monoid homomorphism `R →* S`.
The `simp`-normal form is `(f : R →* S)`. -/
add_decl_doc ring_hom.to_monoid_hom
/-- Reinterpret a ring homomorphism `f : R →+* S` as an additive monoid homomorphism `R →+ S`.
The `simp`-normal form is `(f : R →+ S)`. -/
add_decl_doc ring_hom.to_add_monoid_hom
namespace ring_hom
section coe
/-!
Throughout this section, some `semiring` arguments are specified with `{}` instead of `[]`.
See note [implicit instance arguments].
-/
variables {rα : semiring α} {rβ : semiring β}
include rα rβ
instance : has_coe_to_fun (α →+* β) := ⟨_, ring_hom.to_fun⟩
initialize_simps_projections ring_hom (to_fun → apply)
@[simp] lemma to_fun_eq_coe (f : α →+* β) : f.to_fun = f := rfl
@[simp] lemma coe_mk (f : α → β) (h₁ h₂ h₃ h₄) : ⇑(⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) = f := rfl
instance has_coe_monoid_hom : has_coe (α →+* β) (α →* β) := ⟨ring_hom.to_monoid_hom⟩
@[simp, norm_cast] lemma coe_monoid_hom (f : α →+* β) : ⇑(f : α →* β) = f := rfl
@[simp] lemma to_monoid_hom_eq_coe (f : α →+* β) : f.to_monoid_hom = f := rfl
@[simp] lemma coe_monoid_hom_mk (f : α → β) (h₁ h₂ h₃ h₄) :
((⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) : α →* β) = ⟨f, h₁, h₂⟩ :=
rfl
instance has_coe_add_monoid_hom : has_coe (α →+* β) (α →+ β) := ⟨ring_hom.to_add_monoid_hom⟩
@[simp, norm_cast] lemma coe_add_monoid_hom (f : α →+* β) : ⇑(f : α →+ β) = f := rfl
@[simp] lemma to_add_monoid_hom_eq_coe (f : α →+* β) : f.to_add_monoid_hom = f := rfl
@[simp] lemma coe_add_monoid_hom_mk (f : α → β) (h₁ h₂ h₃ h₄) :
((⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) : α →+ β) = ⟨f, h₃, h₄⟩ :=
rfl
end coe
variables [rα : semiring α] [rβ : semiring β]
section
include rα rβ
variables (f : α →+* β) {x y : α} {rα rβ}
theorem congr_fun {f g : α →+* β} (h : f = g) (x : α) : f x = g x :=
congr_arg (λ h : α →+* β, h x) h
theorem congr_arg (f : α →+* β) {x y : α} (h : x = y) : f x = f y :=
congr_arg (λ x : α, f x) h
theorem coe_inj ⦃f g : α →+* β⦄ (h : (f : α → β) = g) : f = g :=
by cases f; cases g; cases h; refl
@[ext] theorem ext ⦃f g : α →+* β⦄ (h : ∀ x, f x = g x) : f = g :=
coe_inj (funext h)
theorem ext_iff {f g : α →+* β} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, ext h⟩
@[simp] lemma mk_coe (f : α →+* β) (h₁ h₂ h₃ h₄) : ring_hom.mk f h₁ h₂ h₃ h₄ = f :=
ext $ λ _, rfl
theorem coe_add_monoid_hom_injective : function.injective (coe : (α →+* β) → (α →+ β)) :=
λ f g h, ext (λ x, add_monoid_hom.congr_fun h x)
theorem coe_monoid_hom_injective : function.injective (coe : (α →+* β) → (α →* β)) :=
λ f g h, ext (λ x, monoid_hom.congr_fun h x)
/-- Ring homomorphisms map zero to zero. -/
@[simp] lemma map_zero (f : α →+* β) : f 0 = 0 := f.map_zero'
/-- Ring homomorphisms map one to one. -/
@[simp] lemma map_one (f : α →+* β) : f 1 = 1 := f.map_one'
/-- Ring homomorphisms preserve addition. -/
@[simp] lemma map_add (f : α →+* β) (a b : α) : f (a + b) = f a + f b := f.map_add' a b
/-- Ring homomorphisms preserve multiplication. -/
@[simp] lemma map_mul (f : α →+* β) (a b : α) : f (a * b) = f a * f b := f.map_mul' a b
/-- Ring homomorphisms preserve `bit0`. -/
@[simp] lemma map_bit0 (f : α →+* β) (a : α) : f (bit0 a) = bit0 (f a) := map_add _ _ _
/-- Ring homomorphisms preserve `bit1`. -/
@[simp] lemma map_bit1 (f : α →+* β) (a : α) : f (bit1 a) = bit1 (f a) :=
by simp [bit1]
/-- `f : R →+* S` has a trivial codomain iff `f 1 = 0`. -/
lemma codomain_trivial_iff_map_one_eq_zero : (0 : β) = 1 ↔ f 1 = 0 :=
by rw [map_one, eq_comm]
/-- `f : R →+* S` has a trivial codomain iff it has a trivial range. -/
lemma codomain_trivial_iff_range_trivial : (0 : β) = 1 ↔ (∀ x, f x = 0) :=
f.codomain_trivial_iff_map_one_eq_zero.trans
⟨λ h x, by rw [←mul_one x, map_mul, h, mul_zero], λ h, h 1⟩
/-- `f : R →+* S` has a trivial codomain iff its range is `{0}`. -/
lemma codomain_trivial_iff_range_eq_singleton_zero : (0 : β) = 1 ↔ set.range f = {0} :=
f.codomain_trivial_iff_range_trivial.trans
⟨ λ h, set.ext (λ y, ⟨λ ⟨x, hx⟩, by simp [←hx, h x], λ hy, ⟨0, by simpa using hy.symm⟩⟩),
λ h x, set.mem_singleton_iff.mp (h ▸ set.mem_range_self x)⟩
/-- `f : R →+* S` doesn't map `1` to `0` if `S` is nontrivial -/
lemma map_one_ne_zero [nontrivial β] : f 1 ≠ 0 :=
mt f.codomain_trivial_iff_map_one_eq_zero.mpr zero_ne_one
/-- If there is a homomorphism `f : R →+* S` and `S` is nontrivial, then `R` is nontrivial. -/
lemma domain_nontrivial [nontrivial β] : nontrivial α :=
⟨⟨1, 0, mt (λ h, show f 1 = 0, by rw [h, map_zero]) f.map_one_ne_zero⟩⟩
lemma is_unit_map (f : α →+* β) {a : α} (h : is_unit a) : is_unit (f a) :=
h.map (f.to_monoid_hom)
end
/-- The identity ring homomorphism from a semiring to itself. -/
def id (α : Type*) [semiring α] : α →+* α :=
by refine {to_fun := id, ..}; intros; refl
include rα
instance : inhabited (α →+* α) := ⟨id α⟩
@[simp] lemma id_apply (x : α) : ring_hom.id α x = x := rfl
variable {rγ : semiring γ}
include rβ rγ
/-- Composition of ring homomorphisms is a ring homomorphism. -/
def comp (hnp : β →+* γ) (hmn : α →+* β) : α →+* γ :=
{ to_fun := hnp ∘ hmn,
map_zero' := by simp,
map_one' := by simp,
map_add' := λ x y, by simp,
map_mul' := λ x y, by simp}
/-- Composition of semiring homomorphisms is associative. -/
lemma comp_assoc {δ} {rδ: semiring δ} (f : α →+* β) (g : β →+* γ) (h : γ →+* δ) :
(h.comp g).comp f = h.comp (g.comp f) := rfl
@[simp] lemma coe_comp (hnp : β →+* γ) (hmn : α →+* β) : (hnp.comp hmn : α → γ) = hnp ∘ hmn := rfl
lemma comp_apply (hnp : β →+* γ) (hmn : α →+* β) (x : α) : (hnp.comp hmn : α → γ) x =
(hnp (hmn x)) := rfl
omit rγ
@[simp] lemma comp_id (f : α →+* β) : f.comp (id α) = f := ext $ λ x, rfl
@[simp] lemma id_comp (f : α →+* β) : (id β).comp f = f := ext $ λ x, rfl
omit rβ
instance : monoid (α →+* α) :=
{ one := id α,
mul := comp,
mul_one := comp_id,
one_mul := id_comp,
mul_assoc := λ f g h, comp_assoc _ _ _ }
lemma one_def : (1 : α →+* α) = id α := rfl
@[simp] lemma coe_one : ⇑(1 : α →+* α) = _root_.id := rfl
lemma mul_def (f g : α →+* α) : f * g = f.comp g := rfl
@[simp] lemma coe_mul (f g : α →+* α) : ⇑(f * g) = f ∘ g := rfl
include rβ rγ
lemma cancel_right {g₁ g₂ : β →+* γ} {f : α →+* β} (hf : surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
⟨λ h, ring_hom.ext $ (forall_iff_forall_surj hf).1 (ext_iff.1 h), λ h, h ▸ rfl⟩
lemma cancel_left {g : β →+* γ} {f₁ f₂ : α →+* β} (hg : injective g) :
g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ :=
⟨λ h, ring_hom.ext $ λ x, hg $ by rw [← comp_apply, h, comp_apply], λ h, h ▸ rfl⟩
omit rα rβ rγ
end ring_hom
/-- A commutative semiring is a `semiring` with commutative multiplication. In other words, it is a
type with the following structures: additive commutative monoid (`add_comm_monoid`), multiplicative
commutative monoid (`comm_monoid`), distributive laws (`distrib`), and multiplication by zero law
(`mul_zero_class`). -/
@[protect_proj, ancestor semiring comm_monoid]
class comm_semiring (α : Type u) extends semiring α, comm_monoid α
@[priority 100] -- see Note [lower instance priority]
instance comm_semiring.to_comm_monoid_with_zero [comm_semiring α] : comm_monoid_with_zero α :=
{ .. comm_semiring.to_comm_monoid α, .. comm_semiring.to_semiring α }
section comm_semiring
variables [comm_semiring α] [comm_semiring β] {a b c : α}
/-- Pullback a `semiring` instance along an injective function. -/
protected def function.injective.comm_semiring [has_zero γ] [has_one γ] [has_add γ] [has_mul γ]
(f : γ → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
comm_semiring γ :=
{ .. hf.semiring f zero one add mul, .. hf.comm_semigroup f mul }
/-- Pullback a `semiring` instance along an injective function. -/
protected def function.surjective.comm_semiring [has_zero γ] [has_one γ] [has_add γ] [has_mul γ]
(f : α → γ) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
comm_semiring γ :=
{ .. hf.semiring f zero one add mul, .. hf.comm_semigroup f mul }
lemma add_mul_self_eq (a b : α) : (a + b) * (a + b) = a*a + 2*a*b + b*b :=
by simp only [two_mul, add_mul, mul_add, add_assoc, mul_comm b]
@[simp] theorem two_dvd_bit0 : 2 ∣ bit0 a := ⟨a, bit0_eq_two_mul _⟩
lemma ring_hom.map_dvd (f : α →+* β) {a b : α} : a ∣ b → f a ∣ f b :=
λ ⟨z, hz⟩, ⟨f z, by rw [hz, f.map_mul]⟩
end comm_semiring
/-!
### Rings
-/
/-- A ring is a type with the following structures: additive commutative group (`add_comm_group`),
multiplicative monoid (`monoid`), and distributive laws (`distrib`). Equivalently, a ring is a
`semiring` with a negation operation making it an additive group. -/
@[protect_proj, ancestor add_comm_group monoid distrib]
class ring (α : Type u) extends add_comm_group α, monoid α, distrib α
section ring
variables [ring α] {a b c d e : α}
/- The instance from `ring` to `semiring` happens often in linear algebra, for which all the basic
definitions are given in terms of semirings, but many applications use rings or fields. We increase
a little bit its priority above 100 to try it quickly, but remaining below the default 1000 so that
more specific instances are tried first. -/
@[priority 200]
instance ring.to_semiring : semiring α :=
{ zero_mul := λ a, add_left_cancel $ show 0 * a + 0 * a = 0 * a + 0,
by rw [← add_mul, zero_add, add_zero],
mul_zero := λ a, add_left_cancel $ show a * 0 + a * 0 = a * 0 + 0,
by rw [← mul_add, add_zero, add_zero],
..‹ring α› }
/-- Pullback a `ring` instance along an injective function. -/
protected def function.injective.ring [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) :
ring β :=
{ .. hf.add_comm_group f zero add neg, .. hf.monoid f one mul, .. hf.distrib f add mul }
/-- Pullback a `ring` instance along an injective function,
with a subtraction (`-`) that is not necessarily defeq to `a + -b`. -/
protected def function.injective.ring_sub
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) :
ring β :=
{ .. hf.add_comm_group_sub f zero add neg sub, .. hf.monoid f one mul, .. hf.distrib f add mul }
/-- Pullback a `ring` instance along an injective function. -/
protected def function.surjective.ring [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) :
ring β :=
{ .. hf.add_comm_group f zero add neg, .. hf.monoid f one mul, .. hf.distrib f add mul }
/-- Pullback a `ring` instance along an injective function,
with a subtraction (`-`) that is not necessarily defeq to `a + -b`. -/
protected def function.surjective.ring_sub
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) :
ring β :=
{ .. hf.add_comm_group_sub f zero add neg sub, .. hf.monoid f one mul, .. hf.distrib f add mul }
lemma neg_mul_eq_neg_mul (a b : α) : -(a * b) = -a * b :=
neg_eq_of_add_eq_zero
begin rw [← right_distrib, add_right_neg, zero_mul] end
lemma neg_mul_eq_mul_neg (a b : α) : -(a * b) = a * -b :=
neg_eq_of_add_eq_zero
begin rw [← left_distrib, add_right_neg, mul_zero] end
@[simp] lemma neg_mul_eq_neg_mul_symm (a b : α) : - a * b = - (a * b) :=
eq.symm (neg_mul_eq_neg_mul a b)
@[simp] lemma mul_neg_eq_neg_mul_symm (a b : α) : a * - b = - (a * b) :=
eq.symm (neg_mul_eq_mul_neg a b)
lemma neg_mul_neg (a b : α) : -a * -b = a * b :=
by simp
lemma neg_mul_comm (a b : α) : -a * b = a * -b :=
by simp
theorem neg_eq_neg_one_mul (a : α) : -a = -1 * a :=
by simp
lemma mul_sub_left_distrib (a b c : α) : a * (b - c) = a * b - a * c :=
by simpa only [sub_eq_add_neg, neg_mul_eq_mul_neg] using mul_add a b (-c)
alias mul_sub_left_distrib ← mul_sub
lemma mul_sub_right_distrib (a b c : α) : (a - b) * c = a * c - b * c :=
by simpa only [sub_eq_add_neg, neg_mul_eq_neg_mul] using add_mul a (-b) c
alias mul_sub_right_distrib ← sub_mul
/-- An element of a ring multiplied by the additive inverse of one is the element's additive
inverse. -/
lemma mul_neg_one (a : α) : a * -1 = -a := by simp
/-- The additive inverse of one multiplied by an element of a ring is the element's additive
inverse. -/
lemma neg_one_mul (a : α) : -1 * a = -a := by simp
/-- An iff statement following from right distributivity in rings and the definition
of subtraction. -/
theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d :=
calc
a * e + c = b * e + d ↔ a * e + c = d + b * e : by simp [add_comm]
... ↔ a * e + c - b * e = d : iff.intro (λ h, begin rw h, simp end) (λ h,
begin rw ← h, simp end)
... ↔ (a - b) * e + c = d : begin simp [sub_mul, sub_add_eq_add_sub] end
/-- A simplification of one side of an equation exploiting right distributivity in rings
and the definition of subtraction. -/
theorem sub_mul_add_eq_of_mul_add_eq_mul_add : a * e + c = b * e + d → (a - b) * e + c = d :=
assume h,
calc
(a - b) * e + c = (a * e + c) - b * e : begin simp [sub_mul, sub_add_eq_add_sub] end
... = d : begin rw h, simp [@add_sub_cancel α] end
end ring
namespace units
variables [ring α] {a b : α}
/-- Each element of the group of units of a ring has an additive inverse. -/
instance : has_neg (units α) := ⟨λu, ⟨-↑u, -↑u⁻¹, by simp, by simp⟩ ⟩
/-- Representing an element of a ring's unit group as an element of the ring commutes with
mapping this element to its additive inverse. -/
@[simp, norm_cast] protected theorem coe_neg (u : units α) : (↑-u : α) = -u := rfl
@[simp, norm_cast] protected theorem coe_neg_one : ((-1 : units α) : α) = -1 := rfl
/-- Mapping an element of a ring's unit group to its inverse commutes with mapping this element
to its additive inverse. -/
@[simp] protected theorem neg_inv (u : units α) : (-u)⁻¹ = -u⁻¹ := rfl
/-- An element of a ring's unit group equals the additive inverse of its additive inverse. -/
@[simp] protected theorem neg_neg (u : units α) : - -u = u :=
units.ext $ neg_neg _
/-- Multiplication of elements of a ring's unit group commutes with mapping the first
argument to its additive inverse. -/
@[simp] protected theorem neg_mul (u₁ u₂ : units α) : -u₁ * u₂ = -(u₁ * u₂) :=
units.ext $ neg_mul_eq_neg_mul_symm _ _
/-- Multiplication of elements of a ring's unit group commutes with mapping the second argument
to its additive inverse. -/
@[simp] protected theorem mul_neg (u₁ u₂ : units α) : u₁ * -u₂ = -(u₁ * u₂) :=
units.ext $ (neg_mul_eq_mul_neg _ _).symm
/-- Multiplication of the additive inverses of two elements of a ring's unit group equals
multiplication of the two original elements. -/
@[simp] protected theorem neg_mul_neg (u₁ u₂ : units α) : -u₁ * -u₂ = u₁ * u₂ := by simp
/-- The additive inverse of an element of a ring's unit group equals the additive inverse of
one times the original element. -/
protected theorem neg_eq_neg_one_mul (u : units α) : -u = -1 * u := by simp
end units
namespace ring_hom
/-- Ring homomorphisms preserve additive inverse. -/
@[simp] theorem map_neg {α β} [ring α] [ring β] (f : α →+* β) (x : α) : f (-x) = -(f x) :=
(f : α →+ β).map_neg x
/-- Ring homomorphisms preserve subtraction. -/
@[simp] theorem map_sub {α β} [ring α] [ring β] (f : α →+* β) (x y : α) :
f (x - y) = (f x) - (f y) := (f : α →+ β).map_sub x y
/-- A ring homomorphism is injective iff its kernel is trivial. -/
theorem injective_iff {α β} [ring α] [semiring β] (f : α →+* β) :
function.injective f ↔ (∀ a, f a = 0 → a = 0) :=
(f : α →+ β).injective_iff
/-- Makes a ring homomorphism from a monoid homomorphism of rings which preserves addition. -/
def mk' {γ} [semiring α] [ring γ] (f : α →* γ) (map_add : ∀ a b : α, f (a + b) = f a + f b) :
α →+* γ :=
{ to_fun := f,
.. add_monoid_hom.mk' f map_add, .. f }
end ring_hom
/-- A commutative ring is a `ring` with commutative multiplication. -/
@[protect_proj, ancestor ring comm_semigroup]
class comm_ring (α : Type u) extends ring α, comm_semigroup α
@[priority 100] -- see Note [lower instance priority]
instance comm_ring.to_comm_semiring [s : comm_ring α] : comm_semiring α :=
{ mul_zero := mul_zero, zero_mul := zero_mul, ..s }
section comm_ring
variables [comm_ring α] {a b c : α}
/-- Pullback a `comm_ring` instance along an injective function. -/
protected def function.injective.comm_ring [has_zero β] [has_one β] [has_add β] [has_mul β]
[has_neg β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) :
comm_ring β :=
{ .. hf.ring f zero one add mul neg, .. hf.comm_semigroup f mul }
/-- Pullback a `comm_ring` instance along an injective function,
with a subtraction (`-`) that is not necessarily defeq to `a + -b`. -/
protected def function.injective.comm_ring_sub
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) :
comm_ring β :=
{ .. hf.ring_sub f zero one add mul neg sub, .. hf.comm_semigroup f mul }
/-- Pullback a `comm_ring` instance along an injective function. -/
protected def function.surjective.comm_ring [has_zero β] [has_one β] [has_add β] [has_mul β]
[has_neg β] (f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) :
comm_ring β :=
{ .. hf.ring f zero one add mul neg, .. hf.comm_semigroup f mul }
/-- Pullback a `comm_ring` instance along an injective function,
with a subtraction (`-`) that is not necessarily defeq to `a + -b`. -/
protected def function.surjective.comm_ring_sub
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) :
comm_ring β :=
{ .. hf.ring_sub f zero one add mul neg sub, .. hf.comm_semigroup f mul }
local attribute [simp] add_assoc add_comm add_left_comm mul_comm
theorem dvd_neg_of_dvd (h : a ∣ b) : (a ∣ -b) :=
dvd.elim h
(assume c, assume : b = a * c,
dvd.intro (-c) (by simp [this]))
theorem dvd_of_dvd_neg (h : a ∣ -b) : (a ∣ b) :=
let t := dvd_neg_of_dvd h in by rwa neg_neg at t
theorem dvd_neg_iff_dvd (a b : α) : (a ∣ -b) ↔ (a ∣ b) :=
⟨dvd_of_dvd_neg, dvd_neg_of_dvd⟩
theorem neg_dvd_of_dvd (h : a ∣ b) : -a ∣ b :=
dvd.elim h
(assume c, assume : b = a * c,
dvd.intro (-c) (by simp [this]))
theorem dvd_of_neg_dvd (h : -a ∣ b) : a ∣ b :=
let t := neg_dvd_of_dvd h in by rwa neg_neg at t
theorem neg_dvd_iff_dvd (a b : α) : (-a ∣ b) ↔ (a ∣ b) :=
⟨dvd_of_neg_dvd, neg_dvd_of_dvd⟩
theorem dvd_sub (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b - c :=
by { rw sub_eq_add_neg, exact dvd_add h₁ (dvd_neg_of_dvd h₂) }
theorem dvd_add_iff_left (h : a ∣ c) : a ∣ b ↔ a ∣ b + c :=
⟨λh₂, dvd_add h₂ h, λH, by have t := dvd_sub H h; rwa add_sub_cancel at t⟩
theorem dvd_add_iff_right (h : a ∣ b) : a ∣ c ↔ a ∣ b + c :=
by rw add_comm; exact dvd_add_iff_left h
theorem two_dvd_bit1 : 2 ∣ bit1 a ↔ (2 : α) ∣ 1 := (dvd_add_iff_right (@two_dvd_bit0 _ _ a)).symm
/-- Representation of a difference of two squares in a commutative ring as a product. -/
theorem mul_self_sub_mul_self (a b : α) : a * a - b * b = (a + b) * (a - b) :=
by rw [add_mul, mul_sub, mul_sub, mul_comm a b, sub_add_sub_cancel]
lemma mul_self_sub_one (a : α) : a * a - 1 = (a + 1) * (a - 1) :=
by rw [← mul_self_sub_mul_self, mul_one]
/-- An element a of a commutative ring divides the additive inverse of an element b iff a
divides b. -/
@[simp] lemma dvd_neg (a b : α) : (a ∣ -b) ↔ (a ∣ b) :=
⟨dvd_of_dvd_neg, dvd_neg_of_dvd⟩
/-- The additive inverse of an element a of a commutative ring divides another element b iff a
divides b. -/
@[simp] lemma neg_dvd (a b : α) : (-a ∣ b) ↔ (a ∣ b) :=
⟨dvd_of_neg_dvd, neg_dvd_of_dvd⟩
/-- If an element a divides another element c in a commutative ring, a divides the sum of another
element b with c iff a divides b. -/
theorem dvd_add_left (h : a ∣ c) : a ∣ b + c ↔ a ∣ b :=
(dvd_add_iff_left h).symm
/-- If an element a divides another element b in a commutative ring, a divides the sum of b and
another element c iff a divides c. -/
theorem dvd_add_right (h : a ∣ b) : a ∣ b + c ↔ a ∣ c :=
(dvd_add_iff_right h).symm
/-- An element a divides the sum a + b if and only if a divides b.-/
@[simp] lemma dvd_add_self_left {a b : α} : a ∣ a + b ↔ a ∣ b :=
dvd_add_right (dvd_refl a)
/-- An element a divides the sum b + a if and only if a divides b.-/
@[simp] lemma dvd_add_self_right {a b : α} : a ∣ b + a ↔ a ∣ b :=
dvd_add_left (dvd_refl a)
/-- Vieta's formula for a quadratic equation, relating the coefficients of the polynomial with
its roots. This particular version states that if we have a root `x` of a monic quadratic
polynomial, then there is another root `y` such that `x + y` is negative the `a_1` coefficient
and `x * y` is the `a_0` coefficient. -/
lemma Vieta_formula_quadratic {b c x : α} (h : x * x - b * x + c = 0) :
∃ y : α, y * y - b * y + c = 0 ∧ x + y = b ∧ x * y = c :=
begin
have : c = -(x * x - b * x) := (neg_eq_of_add_eq_zero h).symm,
have : c = x * (b - x), by subst this; simp [mul_sub, mul_comm],
refine ⟨b - x, _, by simp, by rw this⟩,
rw [this, sub_add, ← sub_mul, sub_self]
end
lemma dvd_mul_sub_mul {k a b x y : α} (hab : k ∣ a - b) (hxy : k ∣ x - y) :
k ∣ a * x - b * y :=
begin
convert dvd_add (dvd_mul_of_dvd_right hxy a) (dvd_mul_of_dvd_left hab y),
rw [mul_sub_left_distrib, mul_sub_right_distrib],
simp only [sub_eq_add_neg, add_assoc, neg_add_cancel_left],
end
lemma dvd_iff_dvd_of_dvd_sub {a b c : α} (h : a ∣ (b - c)) : (a ∣ b ↔ a ∣ c) :=
begin
split,
{ intro h',
convert dvd_sub h' h,
exact eq.symm (sub_sub_self b c) },
{ intro h',
convert dvd_add h h',
exact eq_add_of_sub_eq rfl }
end
end comm_ring
lemma succ_ne_self [ring α] [nontrivial α] (a : α) : a + 1 ≠ a :=
λ h, one_ne_zero ((add_right_inj a).mp (by simp [h]))
lemma pred_ne_self [ring α] [nontrivial α] (a : α) : a - 1 ≠ a :=
λ h, one_ne_zero (neg_injective ((add_right_inj a).mp (by simpa [sub_eq_add_neg] using h)))
/-- A domain is a ring with no zero divisors, i.e. satisfying
the condition `a * b = 0 ↔ a = 0 ∨ b = 0`. Alternatively, a domain
is an integral domain without assuming commutativity of multiplication. -/
@[protect_proj] class domain (α : Type u) extends ring α, nontrivial α :=
(eq_zero_or_eq_zero_of_mul_eq_zero : ∀ a b : α, a * b = 0 → a = 0 ∨ b = 0)
section domain
variable [domain α]
@[priority 100] -- see Note [lower instance priority]
instance domain.to_no_zero_divisors : no_zero_divisors α :=
⟨domain.eq_zero_or_eq_zero_of_mul_eq_zero⟩
@[priority 100] -- see Note [lower instance priority]
instance domain.to_cancel_monoid_with_zero : cancel_monoid_with_zero α :=
{ mul_left_cancel_of_ne_zero := λ a b c ha,
by { rw [← sub_eq_zero, ← mul_sub], simp [ha, sub_eq_zero] },
mul_right_cancel_of_ne_zero := λ a b c hb,
by { rw [← sub_eq_zero, ← sub_mul], simp [hb, sub_eq_zero] },
.. (infer_instance : semiring α) }
/-- Pullback a `domain` instance along an injective function. -/
protected def function.injective.domain [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) :
domain β :=
{ .. hf.ring f zero one add mul neg, .. pullback_nonzero f zero one,
.. hf.no_zero_divisors f zero mul }
end domain
/-!
### Integral domains
-/
/-- An integral domain is a commutative ring with no zero divisors, i.e. satisfying the condition
`a * b = 0 ↔ a = 0 ∨ b = 0`. Alternatively, an integral domain is a domain with commutative
multiplication. -/
@[protect_proj, ancestor comm_ring domain]
class integral_domain (α : Type u) extends comm_ring α, domain α
section integral_domain
variables [integral_domain α] {a b c d e : α}
@[priority 100] -- see Note [lower instance priority]
instance integral_domain.to_comm_cancel_monoid_with_zero : comm_cancel_monoid_with_zero α :=
{ ..comm_semiring.to_comm_monoid_with_zero, ..domain.to_cancel_monoid_with_zero }
/-- Pullback an `integral_domain` instance along an injective function. -/
protected def function.injective.integral_domain [has_zero β] [has_one β] [has_add β]
[has_mul β] [has_neg β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) :
integral_domain β :=
{ .. hf.comm_ring f zero one add mul neg, .. hf.domain f zero one add mul neg }
lemma mul_self_eq_mul_self_iff {a b : α} : a * a = b * b ↔ a = b ∨ a = -b :=
by rw [← sub_eq_zero, mul_self_sub_mul_self, mul_eq_zero, or_comm, sub_eq_zero,
add_eq_zero_iff_eq_neg]
lemma mul_self_eq_one_iff {a : α} : a * a = 1 ↔ a = 1 ∨ a = -1 :=
by rw [← mul_self_eq_mul_self_iff, one_mul]
/-- In the unit group of an integral domain, a unit is its own inverse iff the unit is one or
one's additive inverse. -/
lemma units.inv_eq_self_iff (u : units α) : u⁻¹ = u ↔ u = 1 ∨ u = -1 :=
by { rw inv_eq_iff_mul_eq_one, simp only [units.ext_iff], push_cast, exact mul_self_eq_one_iff }
/--
Makes a ring homomorphism from an additive group homomorphism from a commutative ring to an integral
domain that commutes with self multiplication, assumes that two is nonzero and one is sent to one.
-/
def add_monoid_hom.mk_ring_hom_of_mul_self_of_two_ne_zero [comm_ring β] (f : β →+ α)
(h : ∀ x, f (x * x) = f x * f x) (h_two : (2 : α) ≠ 0) (h_one : f 1 = 1) : β →+* α :=
{ map_one' := h_one,
map_mul' := begin
intros x y,
have hxy := h (x + y),
rw [mul_add, add_mul, add_mul, f.map_add, f.map_add, f.map_add, f.map_add, h x, h y, add_mul,
mul_add, mul_add, ← sub_eq_zero_iff_eq, add_comm, ← sub_sub, ← sub_sub, ← sub_sub,
mul_comm y x, mul_comm (f y) (f x)] at hxy,
simp only [add_assoc, add_sub_assoc, add_sub_cancel'_right] at hxy,
rw [sub_sub, ← two_mul, ← add_sub_assoc, ← two_mul, ← mul_sub, mul_eq_zero, sub_eq_zero_iff_eq,
or_iff_not_imp_left] at hxy,
exact hxy h_two,
end,
..f }
@[simp]
lemma add_monoid_hom.coe_fn_mk_ring_hom_of_mul_self_of_two_ne_zero [comm_ring β] (f : β →+ α)
(h h_two h_one) :
(f.mk_ring_hom_of_mul_self_of_two_ne_zero h h_two h_one : β → α) = f := rfl
@[simp]
lemma add_monoid_hom.coe_add_monoid_hom_mk_ring_hom_of_mul_self_of_two_ne_zero [comm_ring β]
(f : β →+ α) (h h_two h_one) :
(f.mk_ring_hom_of_mul_self_of_two_ne_zero h h_two h_one : β →+ α) = f := by {ext, simp}
end integral_domain
namespace ring
variables {M₀ : Type*} [monoid_with_zero M₀]
open_locale classical
/-- Introduce a function `inverse` on a monoid with zero `M₀`, which sends `x` to `x⁻¹` if `x` is
invertible and to `0` otherwise. This definition is somewhat ad hoc, but one needs a fully (rather
than partially) defined inverse function for some purposes, including for calculus.
Note that while this is in the `ring` namespace for brevity, it requires the weaker assumption
`monoid_with_zero M₀` instead of `ring M₀`. -/
noncomputable def inverse : M₀ → M₀ :=
λ x, if h : is_unit x then (((classical.some h)⁻¹ : units M₀) : M₀) else 0
/-- By definition, if `x` is invertible then `inverse x = x⁻¹`. -/
@[simp] lemma inverse_unit (u : units M₀) : inverse (u : M₀) = (u⁻¹ : units M₀) :=
begin
simp only [is_unit_unit, inverse, dif_pos],
exact units.inv_unique (classical.some_spec (is_unit_unit u))
end
/-- By definition, if `x` is not invertible then `inverse x = 0`. -/
@[simp] lemma inverse_non_unit (x : M₀) (h : ¬(is_unit x)) : inverse x = 0 := dif_neg h
end ring
/-- A predicate to express that a ring is an integral domain.
This is mainly useful because such a predicate does not contain data,
and can therefore be easily transported along ring isomorphisms. -/
structure is_integral_domain (R : Type u) [ring R] extends nontrivial R : Prop :=
(mul_comm : ∀ (x y : R), x * y = y * x)
(eq_zero_or_eq_zero_of_mul_eq_zero : ∀ x y : R, x * y = 0 → x = 0 ∨ y = 0)
-- The linter does not recognize that is_integral_domain.to_nontrivial is a structure
-- projection, disable it
attribute [nolint def_lemma doc_blame] is_integral_domain.to_nontrivial
/-- Every integral domain satisfies the predicate for integral domains. -/
lemma integral_domain.to_is_integral_domain (R : Type u) [integral_domain R] :
is_integral_domain R :=
{ .. (‹_› : integral_domain R) }
/-- If a ring satisfies the predicate for integral domains,
then it can be endowed with an `integral_domain` instance
whose data is definitionally equal to the existing data. -/
def is_integral_domain.to_integral_domain (R : Type u) [ring R] (h : is_integral_domain R) :
integral_domain R :=
{ .. (‹_› : ring R), .. (‹_› : is_integral_domain R) }
namespace semiconj_by
@[simp] lemma add_right [distrib R] {a x y x' y' : R}
(h : semiconj_by a x y) (h' : semiconj_by a x' y') :
semiconj_by a (x + x') (y + y') :=
by simp only [semiconj_by, left_distrib, right_distrib, h.eq, h'.eq]
@[simp] lemma add_left [distrib R] {a b x y : R}
(ha : semiconj_by a x y) (hb : semiconj_by b x y) :
semiconj_by (a + b) x y :=
by simp only [semiconj_by, left_distrib, right_distrib, ha.eq, hb.eq]
variables [ring R] {a b x y x' y' : R}
lemma neg_right (h : semiconj_by a x y) : semiconj_by a (-x) (-y) :=
by simp only [semiconj_by, h.eq, neg_mul_eq_neg_mul_symm, mul_neg_eq_neg_mul_symm]
@[simp] lemma neg_right_iff : semiconj_by a (-x) (-y) ↔ semiconj_by a x y :=
⟨λ h, neg_neg x ▸ neg_neg y ▸ h.neg_right, semiconj_by.neg_right⟩
lemma neg_left (h : semiconj_by a x y) : semiconj_by (-a) x y :=
by simp only [semiconj_by, h.eq, neg_mul_eq_neg_mul_symm, mul_neg_eq_neg_mul_symm]
@[simp] lemma neg_left_iff : semiconj_by (-a) x y ↔ semiconj_by a x y :=
⟨λ h, neg_neg a ▸ h.neg_left, semiconj_by.neg_left⟩
@[simp] lemma neg_one_right (a : R) : semiconj_by a (-1) (-1) :=
(one_right a).neg_right
@[simp] lemma neg_one_left (x : R) : semiconj_by (-1) x x :=
(semiconj_by.one_left x).neg_left
@[simp] lemma sub_right (h : semiconj_by a x y) (h' : semiconj_by a x' y') :
semiconj_by a (x - x') (y - y') :=
by simpa only [sub_eq_add_neg] using h.add_right h'.neg_right
@[simp] lemma sub_left (ha : semiconj_by a x y) (hb : semiconj_by b x y) :
semiconj_by (a - b) x y :=
by simpa only [sub_eq_add_neg] using ha.add_left hb.neg_left
end semiconj_by
namespace commute
@[simp] theorem add_right [distrib R] {a b c : R} :
commute a b → commute a c → commute a (b + c) :=
semiconj_by.add_right
@[simp] theorem add_left [distrib R] {a b c : R} :
commute a c → commute b c → commute (a + b) c :=
semiconj_by.add_left
lemma bit0_right [distrib R] {x y : R} (h : commute x y) : commute x (bit0 y) :=
h.add_right h
lemma bit0_left [distrib R] {x y : R} (h : commute x y) : commute (bit0 x) y :=
h.add_left h
lemma bit1_right [semiring R] {x y : R} (h : commute x y) : commute x (bit1 y) :=
h.bit0_right.add_right (commute.one_right x)
lemma bit1_left [semiring R] {x y : R} (h : commute x y) : commute (bit1 x) y :=
h.bit0_left.add_left (commute.one_left y)
variables [ring R] {a b c : R}
theorem neg_right : commute a b → commute a (- b) := semiconj_by.neg_right
@[simp] theorem neg_right_iff : commute a (-b) ↔ commute a b := semiconj_by.neg_right_iff
theorem neg_left : commute a b → commute (- a) b := semiconj_by.neg_left
@[simp] theorem neg_left_iff : commute (-a) b ↔ commute a b := semiconj_by.neg_left_iff
@[simp] theorem neg_one_right (a : R) : commute a (-1) := semiconj_by.neg_one_right a
@[simp] theorem neg_one_left (a : R): commute (-1) a := semiconj_by.neg_one_left a
@[simp] theorem sub_right : commute a b → commute a c → commute a (b - c) := semiconj_by.sub_right
@[simp] theorem sub_left : commute a c → commute b c → commute (a - b) c := semiconj_by.sub_left
end commute
|
0fc6b9a74729b4f91f61a304e4ffaf75db4383ad | 36938939954e91f23dec66a02728db08a7acfcf9 | /lean/deps/galois_stdlib/src/galois/category/reader.lean | 11501c2761beb0efd846aa264aa65d4d984881d2 | [
"Apache-2.0"
] | permissive | pnwamk/reopt-vcg | f8b56dd0279392a5e1c6aee721be8138e6b558d3 | c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d | refs/heads/master | 1,631,145,017,772 | 1,593,549,019,000 | 1,593,549,143,000 | 254,191,418 | 0 | 0 | null | 1,586,377,077,000 | 1,586,377,077,000 | null | UTF-8 | Lean | false | false | 210 | lean | namespace reader_t
instance is_monad_state {s : Type _} {m : Type _ → Type _} {ρ: Type _} [monad_state s m]
: monad_state s (reader_t ρ m) :=
{ lift := λ_ f, ⟨λr, monad_state.lift f⟩
}
end reader_t
|
5681bb2b8738b4b4b5cde88da90286507371e5d8 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/data/rat/cast.lean | 05870c23d9625c95555d655ae6d8796703f3613d | [
"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 | 13,316 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import data.rat.order
import data.int.char_zero
/-!
# Casts for Rational Numbers
## Summary
We define the canonical injection from ℚ into an arbitrary division ring and prove various
casting lemmas showing the well-behavedness of this injection.
## Notations
- `/.` is infix notation for `rat.mk`.
## Tags
rat, rationals, field, ℚ, numerator, denominator, num, denom, cast, coercion, casting
-/
namespace rat
variable {α : Type*}
open_locale rat
section with_div_ring
variable [division_ring α]
/-- Construct the canonical injection from `ℚ` into an arbitrary
division ring. If the field has positive characteristic `p`,
we define `1 / p = 1 / 0 = 0` for consistency with our
division by zero convention. -/
-- see Note [coercion into rings]
@[priority 900] instance cast_coe : has_coe_t ℚ α := ⟨λ r, r.1 / r.2⟩
theorem cast_def (r : ℚ) : (r : α) = r.num / r.denom := rfl
@[simp] theorem cast_of_int (n : ℤ) : (of_int n : α) = n :=
show (n / (1:ℕ) : α) = n, by rw [nat.cast_one, div_one]
@[simp, norm_cast] theorem cast_coe_int (n : ℤ) : ((n : ℚ) : α) = n :=
by rw [coe_int_eq_of_int, cast_of_int]
@[simp, norm_cast] theorem cast_coe_nat (n : ℕ) : ((n : ℚ) : α) = n := cast_coe_int n
@[simp, norm_cast] theorem cast_zero : ((0 : ℚ) : α) = 0 :=
(cast_of_int _).trans int.cast_zero
@[simp, norm_cast] theorem cast_one : ((1 : ℚ) : α) = 1 :=
(cast_of_int _).trans int.cast_one
theorem cast_commute (r : ℚ) (a : α) : commute ↑r a :=
(r.1.cast_commute a).div_left (r.2.cast_commute a)
theorem cast_comm (r : ℚ) (a : α) : (r : α) * a = a * r :=
(cast_commute r a).eq
theorem commute_cast (a : α) (r : ℚ) : commute a r :=
(r.cast_commute a).symm
@[norm_cast] theorem cast_mk_of_ne_zero (a b : ℤ)
(b0 : (b:α) ≠ 0) : (a /. b : α) = a / b :=
begin
have b0' : b ≠ 0, { refine mt _ b0, simp {contextual := tt} },
cases e : a /. b with n d h c,
have d0 : (d:α) ≠ 0,
{ intro d0,
have dd := denom_dvd a b,
cases (show (d:ℤ) ∣ b, by rwa e at dd) with k ke,
have : (b:α) = (d:α) * (k:α), {rw [ke, int.cast_mul], refl},
rw [d0, zero_mul] at this, contradiction },
rw [num_denom'] at e,
have := congr_arg (coe : ℤ → α) ((mk_eq b0' $ ne_of_gt $ int.coe_nat_pos.2 h).1 e),
rw [int.cast_mul, int.cast_mul, int.cast_coe_nat] at this,
symmetry, change (a / b : α) = n / d,
rw [div_eq_mul_inv, eq_div_iff_mul_eq d0, mul_assoc, (d.commute_cast _).eq,
← mul_assoc, this, mul_assoc, mul_inv_cancel b0, mul_one]
end
@[norm_cast] theorem cast_add_of_ne_zero : ∀ {m n : ℚ},
(m.denom : α) ≠ 0 → (n.denom : α) ≠ 0 → ((m + n : ℚ) : α) = m + n
| ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := λ (d₁0 : (d₁:α) ≠ 0) (d₂0 : (d₂:α) ≠ 0), begin
have d₁0' : (d₁:ℤ) ≠ 0 := int.coe_nat_ne_zero.2 (λ e, by rw e at d₁0; exact d₁0 rfl),
have d₂0' : (d₂:ℤ) ≠ 0 := int.coe_nat_ne_zero.2 (λ e, by rw e at d₂0; exact d₂0 rfl),
rw [num_denom', num_denom', add_def d₁0' d₂0'],
suffices : (n₁ * (d₂ * (d₂⁻¹ * d₁⁻¹)) +
n₂ * (d₁ * d₂⁻¹) * d₁⁻¹ : α) = n₁ * d₁⁻¹ + n₂ * d₂⁻¹,
{ rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero, cast_mk_of_ne_zero],
{ simpa [division_def, left_distrib, right_distrib, mul_inv_rev₀, d₁0, d₂0, mul_assoc] },
all_goals {simp [d₁0, d₂0]} },
rw [← mul_assoc (d₂:α), mul_inv_cancel d₂0, one_mul,
(nat.cast_commute _ _).eq], simp [d₁0, mul_assoc]
end
@[simp, norm_cast] theorem cast_neg : ∀ n, ((-n : ℚ) : α) = -n
| ⟨n, d, h, c⟩ := show (↑-n / d : α) = -(n / d),
by rw [div_eq_mul_inv, div_eq_mul_inv, int.cast_neg, neg_mul_eq_neg_mul]
@[norm_cast] theorem cast_sub_of_ne_zero {m n : ℚ}
(m0 : (m.denom : α) ≠ 0) (n0 : (n.denom : α) ≠ 0) : ((m - n : ℚ) : α) = m - n :=
have ((-n).denom : α) ≠ 0, by cases n; exact n0,
by simp [sub_eq_add_neg, (cast_add_of_ne_zero m0 this)]
@[norm_cast] theorem cast_mul_of_ne_zero : ∀ {m n : ℚ},
(m.denom : α) ≠ 0 → (n.denom : α) ≠ 0 → ((m * n : ℚ) : α) = m * n
| ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := λ (d₁0 : (d₁:α) ≠ 0) (d₂0 : (d₂:α) ≠ 0), begin
have d₁0' : (d₁:ℤ) ≠ 0 := int.coe_nat_ne_zero.2 (λ e, by rw e at d₁0; exact d₁0 rfl),
have d₂0' : (d₂:ℤ) ≠ 0 := int.coe_nat_ne_zero.2 (λ e, by rw e at d₂0; exact d₂0 rfl),
rw [num_denom', num_denom', mul_def d₁0' d₂0'],
suffices : (n₁ * ((n₂ * d₂⁻¹) * d₁⁻¹) : α) = n₁ * (d₁⁻¹ * (n₂ * d₂⁻¹)),
{ rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero, cast_mk_of_ne_zero],
{ simpa [division_def, mul_inv_rev₀, d₁0, d₂0, mul_assoc] },
all_goals {simp [d₁0, d₂0]} },
rw [(d₁.commute_cast (_:α)).inv_right₀.eq]
end
@[simp] theorem cast_inv_nat (n : ℕ) : ((n⁻¹ : ℚ) : α) = n⁻¹ :=
begin
cases n, { simp },
simp_rw [coe_nat_eq_mk, inv_def, mk, mk_nat, dif_neg n.succ_ne_zero, mk_pnat],
simp [cast_def]
end
@[simp] theorem cast_inv_int (n : ℤ) : ((n⁻¹ : ℚ) : α) = n⁻¹ :=
begin
cases n,
{ exact cast_inv_nat _ },
{ simp only [int.cast_neg_succ_of_nat, ← nat.cast_succ, cast_neg, inv_neg, cast_inv_nat] }
end
@[norm_cast] theorem cast_inv_of_ne_zero : ∀ {n : ℚ},
(n.num : α) ≠ 0 → (n.denom : α) ≠ 0 → ((n⁻¹ : ℚ) : α) = n⁻¹
| ⟨n, d, h, c⟩ := λ (n0 : (n:α) ≠ 0) (d0 : (d:α) ≠ 0), begin
have n0' : (n:ℤ) ≠ 0 := λ e, by rw e at n0; exact n0 rfl,
have d0' : (d:ℤ) ≠ 0 := int.coe_nat_ne_zero.2 (λ e, by rw e at d0; exact d0 rfl),
rw [num_denom', inv_def],
rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero, inv_div];
simp [n0, d0]
end
@[norm_cast] theorem cast_div_of_ne_zero {m n : ℚ} (md : (m.denom : α) ≠ 0)
(nn : (n.num : α) ≠ 0) (nd : (n.denom : α) ≠ 0) : ((m / n : ℚ) : α) = m / n :=
have (n⁻¹.denom : ℤ) ∣ n.num,
by conv in n⁻¹.denom { rw [←(@num_denom n), inv_def] };
apply denom_dvd,
have (n⁻¹.denom : α) = 0 → (n.num : α) = 0, from
λ h, let ⟨k, e⟩ := this in
by have := congr_arg (coe : ℤ → α) e;
rwa [int.cast_mul, int.cast_coe_nat, h, zero_mul] at this,
by rw [division_def, cast_mul_of_ne_zero md (mt this nn), cast_inv_of_ne_zero nn nd, division_def]
@[simp, norm_cast] theorem cast_inj [char_zero α] : ∀ {m n : ℚ}, (m : α) = n ↔ m = n
| ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := begin
refine ⟨λ h, _, congr_arg _⟩,
have d₁0 : d₁ ≠ 0 := ne_of_gt h₁,
have d₂0 : d₂ ≠ 0 := ne_of_gt h₂,
have d₁a : (d₁:α) ≠ 0 := nat.cast_ne_zero.2 d₁0,
have d₂a : (d₂:α) ≠ 0 := nat.cast_ne_zero.2 d₂0,
rw [num_denom', num_denom'] at h ⊢,
rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero] at h; simp [d₁0, d₂0] at h ⊢,
rwa [eq_div_iff_mul_eq d₂a, division_def, mul_assoc, (d₁.cast_commute (d₂:α)).inv_left₀.eq,
← mul_assoc, ← division_def, eq_comm, eq_div_iff_mul_eq d₁a, eq_comm,
← int.cast_coe_nat, ← int.cast_mul, ← int.cast_coe_nat, ← int.cast_mul,
int.cast_inj, ← mk_eq (int.coe_nat_ne_zero.2 d₁0) (int.coe_nat_ne_zero.2 d₂0)] at h
end
theorem cast_injective [char_zero α] : function.injective (coe : ℚ → α)
| m n := cast_inj.1
@[simp] theorem cast_eq_zero [char_zero α] {n : ℚ} : (n : α) = 0 ↔ n = 0 :=
by rw [← cast_zero, cast_inj]
theorem cast_ne_zero [char_zero α] {n : ℚ} : (n : α) ≠ 0 ↔ n ≠ 0 :=
not_congr cast_eq_zero
@[simp, norm_cast] theorem cast_add [char_zero α] (m n) :
((m + n : ℚ) : α) = m + n :=
cast_add_of_ne_zero (nat.cast_ne_zero.2 $ ne_of_gt m.pos) (nat.cast_ne_zero.2 $ ne_of_gt n.pos)
@[simp, norm_cast] theorem cast_sub [char_zero α] (m n) :
((m - n : ℚ) : α) = m - n :=
cast_sub_of_ne_zero (nat.cast_ne_zero.2 $ ne_of_gt m.pos) (nat.cast_ne_zero.2 $ ne_of_gt n.pos)
@[simp, norm_cast] theorem cast_mul [char_zero α] (m n) :
((m * n : ℚ) : α) = m * n :=
cast_mul_of_ne_zero (nat.cast_ne_zero.2 $ ne_of_gt m.pos) (nat.cast_ne_zero.2 $ ne_of_gt n.pos)
@[simp, norm_cast] theorem cast_bit0 [char_zero α] (n : ℚ) :
((bit0 n : ℚ) : α) = bit0 n :=
cast_add _ _
@[simp, norm_cast] theorem cast_bit1 [char_zero α] (n : ℚ) :
((bit1 n : ℚ) : α) = bit1 n :=
by rw [bit1, cast_add, cast_one, cast_bit0]; refl
variable (α)
/-- Coercion `ℚ → α` as a `ring_hom`. -/
def cast_hom [char_zero α] : ℚ →+* α := ⟨coe, cast_one, cast_mul, cast_zero, cast_add⟩
variable {α}
@[simp] lemma coe_cast_hom [char_zero α] : ⇑(cast_hom α) = coe := rfl
@[simp, norm_cast] theorem cast_inv [char_zero α] (n) : ((n⁻¹ : ℚ) : α) = n⁻¹ :=
(cast_hom α).map_inv _
@[simp, norm_cast] theorem cast_div [char_zero α] (m n) :
((m / n : ℚ) : α) = m / n :=
(cast_hom α).map_div _ _
@[norm_cast] theorem cast_mk [char_zero α] (a b : ℤ) : ((a /. b) : α) = a / b :=
by simp only [mk_eq_div, cast_div, cast_coe_int]
@[simp, norm_cast] theorem cast_pow [char_zero α] (q) (k : ℕ) :
((q ^ k : ℚ) : α) = q ^ k :=
(cast_hom α).map_pow q k
end with_div_ring
@[simp, norm_cast] theorem cast_nonneg [linear_ordered_field α] : ∀ {n : ℚ}, 0 ≤ (n : α) ↔ 0 ≤ n
| ⟨n, d, h, c⟩ :=
by { rw [num_denom', cast_mk, mk_eq_div, div_nonneg_iff, div_nonneg_iff], norm_cast }
@[simp, norm_cast] theorem cast_le [linear_ordered_field α] {m n : ℚ} : (m : α) ≤ n ↔ m ≤ n :=
by rw [← sub_nonneg, ← cast_sub, cast_nonneg, sub_nonneg]
@[simp, norm_cast] theorem cast_lt [linear_ordered_field α] {m n : ℚ} : (m : α) < n ↔ m < n :=
by simpa [-cast_le] using not_congr (@cast_le α _ n m)
@[simp] theorem cast_nonpos [linear_ordered_field α] {n : ℚ} : (n : α) ≤ 0 ↔ n ≤ 0 :=
by rw [← cast_zero, cast_le]
@[simp] theorem cast_pos [linear_ordered_field α] {n : ℚ} : (0 : α) < n ↔ 0 < n :=
by rw [← cast_zero, cast_lt]
@[simp] theorem cast_lt_zero [linear_ordered_field α] {n : ℚ} : (n : α) < 0 ↔ n < 0 :=
by rw [← cast_zero, cast_lt]
@[simp, norm_cast] theorem cast_id : ∀ n : ℚ, ↑n = n
| ⟨n, d, h, c⟩ := by rw [num_denom', cast_mk, mk_eq_div]
@[simp, norm_cast] theorem cast_min [linear_ordered_field α] {a b : ℚ} :
(↑(min a b) : α) = min a b :=
by by_cases a ≤ b; simp [h, min_def]
@[simp, norm_cast] theorem cast_max [linear_ordered_field α] {a b : ℚ} :
(↑(max a b) : α) = max a b :=
by by_cases b ≤ a; simp [h, max_def]
@[simp, norm_cast] theorem cast_abs [linear_ordered_field α] {q : ℚ} :
((|q| : ℚ) : α) = |q| :=
by simp [abs_eq_max_neg]
end rat
open rat ring_hom
lemma ring_hom.eq_rat_cast {k} [division_ring k] (f : ℚ →+* k) (r : ℚ) : f r = r :=
calc f r = f (r.1 / r.2) : by rw [← int.cast_coe_nat, ← mk_eq_div, num_denom]
... = f r.1 / f r.2 : f.map_div _ _
... = r.1 / r.2 : by rw [map_nat_cast, map_int_cast]
-- This seems to be true for a `[char_p k]` too because `k'` must have the same characteristic
-- but the proof would be much longer
lemma ring_hom.map_rat_cast {k k'} [division_ring k] [char_zero k] [division_ring k']
(f : k →+* k') (r : ℚ) :
f r = r :=
(f.comp (cast_hom k)).eq_rat_cast r
lemma ring_hom.ext_rat {R : Type*} [semiring R] (f g : ℚ →+* R) : f = g :=
begin
ext r,
refine rat.num_denom_cases_on' r _,
intros a b b0,
let φ : ℤ →+* R := f.comp (int.cast_ring_hom ℚ),
let ψ : ℤ →+* R := g.comp (int.cast_ring_hom ℚ),
rw [rat.mk_eq_div, int.cast_coe_nat],
have b0' : (b:ℚ) ≠ 0 := nat.cast_ne_zero.2 b0,
have : ∀ n : ℤ, f n = g n := λ n, show φ n = ψ n, by rw [φ.ext_int ψ],
calc f (a * b⁻¹)
= f a * f b⁻¹ * (g (b:ℤ) * g b⁻¹) :
by rw [int.cast_coe_nat, ← g.map_mul, mul_inv_cancel b0', g.map_one, mul_one, f.map_mul]
... = g a * f b⁻¹ * (f (b:ℤ) * g b⁻¹) : by rw [this a, ← this b]
... = g (a * b⁻¹) :
by rw [int.cast_coe_nat, mul_assoc, ← mul_assoc (f b⁻¹),
← f.map_mul, inv_mul_cancel b0', f.map_one, one_mul, g.map_mul]
end
instance rat.subsingleton_ring_hom {R : Type*} [semiring R] : subsingleton (ℚ →+* R) :=
⟨ring_hom.ext_rat⟩
namespace monoid_with_zero_hom
variables {M : Type*} [group_with_zero M]
/-- If `f` and `g` agree on the integers then they are equal `φ`.
See note [partially-applied ext lemmas] for why `comp` is used here. -/
@[ext]
theorem ext_rat {f g : monoid_with_zero_hom ℚ M}
(same_on_int : f.comp (int.cast_ring_hom ℚ).to_monoid_with_zero_hom =
g.comp (int.cast_ring_hom ℚ).to_monoid_with_zero_hom) : f = g :=
begin
have same_on_int' : ∀ k : ℤ, f k = g k := congr_fun same_on_int,
ext x,
rw [← @rat.num_denom x, rat.mk_eq_div, f.map_div, g.map_div,
same_on_int' x.num, same_on_int' x.denom],
end
/-- Positive integer values of a morphism `φ` and its value on `-1` completely determine `φ`. -/
theorem ext_rat_on_pnat {f g : monoid_with_zero_hom ℚ M}
(same_on_neg_one : f (-1) = g (-1)) (same_on_pnat : ∀ n : ℕ, 0 < n → f n = g n) : f = g :=
ext_rat $ ext_int' (by simpa) ‹_›
end monoid_with_zero_hom
|
5639fa72aa032cd02ce8c898474e41d1c9de4cac | c1cc0e0109964c513d82364865617b077fbbca41 | /src/cosets.lean | 00214e8707e1aa9280fe56fefa299980c7d3e695 | [] | no_license | loganrjmurphy/lean-algebra | 314f12a28459d8734200207b4304ce036b0c886d | 474ac0c2714b7c0ad544a90c0d0807b92a0da227 | refs/heads/master | 1,675,055,520,716 | 1,607,564,600,000 | 1,607,564,600,000 | 316,637,587 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,746 | lean | import definitions subgroups basic
namespace Algebra
namespace coset
variables {G : Type} [group G] {K : subgroup G}
{a b g h: G}
def left_coset (g : G) (H : subgroup G) : set G :=
{g' | ∃ h ∈ H, g + h = g'}
def right_coset (g : G) (H : subgroup G) : set G :=
{g' | ∃ h ∈ H, h + g = g'}
def in_left_coset {g₁ g₂ g₃: G} {H : subgroup G} (h₁ : g₃ ∈ H) (h₂ : g₁ + g₃ = g₂) : g₂ ∈ left_coset g₁ H :=
by {rw left_coset, simp, exact ⟨g₃, h₁,h₂⟩}
def left_cosets_exists_iff_exists
(H' : left_coset a K = left_coset b K) :
(∃ (h : G), h ∈ K ∧ a + h = g) → (∃ (h : G), h ∈ K ∧ b + h = g) := begin rintros ⟨w, H1, H2⟩,
have H3: g ∈ left_coset a K,
from in_left_coset H1 H2,
have H4: g ∈ left_coset b K,
by {rw ← H', assumption},
cases H4 with w' H4, cases H4 with H5 H4,
use w', split, assumption, assumption
end
def left_cosets_eq_iff {a b : G} {K : subgroup G}:
left_coset a K = left_coset b K ↔
∀ g : G,
(∃ h : G, h ∈ K ∧ a + h = g) ↔
(∃ h : G, h ∈ K ∧ b + h = g) :=
begin
split,
{ intro H', intro g, split,
{apply left_cosets_exists_iff_exists H'},
{apply left_cosets_exists_iff_exists H'.symm}},
{ intro H', repeat {rw left_coset},
ext, simp, split,
{ intro H1,
replace H' := H' x,
apply H'.mp, exact H1,},
{ intro H1, replace H' := H' x, apply H'.mpr, exact H1,}}
end
variables {g₁ g₂ : G}
lemma right_inv_cosets_eq_of_left_cosets_eq (H : left_coset a K = left_coset b K) :
right_coset (-a) K = right_coset (-b) K :=
begin
replace H := left_cosets_eq_iff.mp H,
ext,
split,
{
intro H,
}
end
end coset
end Algebra |
38914e9c23685b924e4779d2ff7092e54e123916 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/bench/rbmap_library.lean | daea811fb3e93a1779e528aef1177b961b9327fe | [
"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 | 384 | lean | import Lean.Data.RBMap
open Lean
abbrev Tree : Type := RBMap Nat Bool compare
def mkMapAux : Nat → Tree → Tree
| 0, m => m
| n+1, m => mkMapAux n (m.insert n (n % 10 = 0))
def mkMap (n : Nat) :=
mkMapAux n {}
def main (xs : List String) : IO Unit :=
let m := mkMap xs.head!.toNat!
let v := m.fold (fun _ r v => if v then r + 1 else r) 0
IO.println (toString v)
|
2728a9a956270e0cf9aba43af014fcae79ac7bdd | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/measure_theory/constructions/polish.lean | 9558e512508585bc2921f7ac9b0777f1fe3fa444 | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 34,620 | lean | /-
Copyright (c) 2022 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.polish
import measure_theory.constructions.borel_space
/-!
# The Borel sigma-algebra on Polish spaces
We discuss several results pertaining to the relationship between the topology and the Borel
structure on Polish spaces.
## Main definitions and results
First, we define the class of analytic sets and establish its basic properties.
* `measure_theory.analytic_set s`: a set in a topological space is analytic if it is the continuous
image of a Polish space. Equivalently, it is empty, or the image of `ℕ → ℕ`.
* `measure_theory.analytic_set.image_of_continuous`: a continuous image of an analytic set is
analytic.
* `measurable_set.analytic_set`: in a Polish space, any Borel-measurable set is analytic.
Then, we show Lusin's theorem that two disjoint analytic sets can be separated by Borel sets.
* `measurably_separable s t` states that there exists a measurable set containing `s` and disjoint
from `t`.
* `analytic_set.measurably_separable` shows that two disjoint analytic sets are separated by a
Borel set.
Finally, we prove the Lusin-Souslin theorem that a continuous injective image of a Borel subset of
a Polish space is Borel. The proof of this nontrivial result relies on the above results on
analytic sets.
* `measurable_set.image_of_continuous_on_inj_on` asserts that, if `s` is a Borel measurable set in
a Polish space, then the image of `s` under a continuous injective map is still Borel measurable.
* `continuous.measurable_embedding` states that a continuous injective map on a Polish space
is a measurable embedding for the Borel sigma-algebra.
* `continuous_on.measurable_embedding` is the same result for a map restricted to a measurable set
on which it is continuous.
* `measurable.measurable_embedding` states that a measurable injective map from a Polish space
to a second-countable topological space is a measurable embedding.
* `is_clopenable_iff_measurable_set`: in a Polish space, a set is clopenable (i.e., it can be made
open and closed by using a finer Polish topology) if and only if it is Borel-measurable.
-/
open set function polish_space pi_nat topological_space metric filter
open_locale topological_space measure_theory filter
variables {α : Type*} [topological_space α] {ι : Type*}
namespace measure_theory
/-! ### Analytic sets -/
/-- An analytic set is a set which is the continuous image of some Polish space. There are several
equivalent characterizations of this definition. For the definition, we pick one that avoids
universe issues: a set is analytic if and only if it is a continuous image of `ℕ → ℕ` (or if it
is empty). The above more usual characterization is given
in `analytic_set_iff_exists_polish_space_range`.
Warning: these are analytic sets in the context of descriptive set theory (which is why they are
registered in the namespace `measure_theory`). They have nothing to do with analytic sets in the
context of complex analysis. -/
@[irreducible] def analytic_set (s : set α) : Prop :=
s = ∅ ∨ ∃ (f : (ℕ → ℕ) → α), continuous f ∧ range f = s
lemma analytic_set_empty : analytic_set (∅ : set α) :=
begin
rw analytic_set,
exact or.inl rfl
end
lemma analytic_set_range_of_polish_space
{β : Type*} [topological_space β] [polish_space β] {f : β → α} (f_cont : continuous f) :
analytic_set (range f) :=
begin
casesI is_empty_or_nonempty β,
{ rw range_eq_empty,
exact analytic_set_empty },
{ rw analytic_set,
obtain ⟨g, g_cont, hg⟩ : ∃ (g : (ℕ → ℕ) → β), continuous g ∧ surjective g :=
exists_nat_nat_continuous_surjective β,
refine or.inr ⟨f ∘ g, f_cont.comp g_cont, _⟩,
rwa hg.range_comp }
end
/-- The image of an open set under a continuous map is analytic. -/
lemma _root_.is_open.analytic_set_image {β : Type*} [topological_space β] [polish_space β]
{s : set β} (hs : is_open s) {f : β → α} (f_cont : continuous f) :
analytic_set (f '' s) :=
begin
rw image_eq_range,
haveI : polish_space s := hs.polish_space,
exact analytic_set_range_of_polish_space (f_cont.comp continuous_subtype_coe),
end
/-- A set is analytic if and only if it is the continuous image of some Polish space. -/
theorem analytic_set_iff_exists_polish_space_range {s : set α} :
analytic_set s ↔ ∃ (β : Type) (h : topological_space β) (h' : @polish_space β h) (f : β → α),
@continuous _ _ h _ f ∧ range f = s :=
begin
split,
{ assume h,
rw analytic_set at h,
cases h,
{ refine ⟨empty, by apply_instance, by apply_instance, empty.elim, continuous_bot, _⟩,
rw h,
exact range_eq_empty _ },
{ exact ⟨ℕ → ℕ, by apply_instance, by apply_instance, h⟩ } },
{ rintros ⟨β, h, h', f, f_cont, f_range⟩,
resetI,
rw ← f_range,
exact analytic_set_range_of_polish_space f_cont }
end
/-- The continuous image of an analytic set is analytic -/
lemma analytic_set.image_of_continuous_on {β : Type*} [topological_space β]
{s : set α} (hs : analytic_set s) {f : α → β} (hf : continuous_on f s) :
analytic_set (f '' s) :=
begin
rcases analytic_set_iff_exists_polish_space_range.1 hs with ⟨γ, γtop, γpolish, g, g_cont, gs⟩,
resetI,
have : f '' s = range (f ∘ g), by rw [range_comp, gs],
rw this,
apply analytic_set_range_of_polish_space,
apply hf.comp_continuous g_cont (λ x, _),
rw ← gs,
exact mem_range_self _
end
lemma analytic_set.image_of_continuous {β : Type*} [topological_space β]
{s : set α} (hs : analytic_set s) {f : α → β} (hf : continuous f) :
analytic_set (f '' s) :=
hs.image_of_continuous_on hf.continuous_on
/-- A countable intersection of analytic sets is analytic. -/
theorem analytic_set.Inter [hι : nonempty ι] [encodable ι] [t2_space α]
{s : ι → set α} (hs : ∀ n, analytic_set (s n)) :
analytic_set (⋂ n, s n) :=
begin
unfreezingI { rcases hι with ⟨i₀⟩ },
/- For the proof, write each `s n` as the continuous image under a map `f n` of a
Polish space `β n`. The product space `γ = Π n, β n` is also Polish, and so is the subset
`t` of sequences `x n` for which `f n (x n)` is independent of `n`. The set `t` is Polish, and the
range of `x ↦ f 0 (x 0)` on `t` is exactly `⋂ n, s n`, so this set is analytic. -/
choose β hβ h'β f f_cont f_range using λ n, analytic_set_iff_exists_polish_space_range.1 (hs n),
resetI,
let γ := Π n, β n,
let t : set γ := ⋂ n, {x | f n (x n) = f i₀ (x i₀)},
have t_closed : is_closed t,
{ apply is_closed_Inter,
assume n,
exact is_closed_eq ((f_cont n).comp (continuous_apply n))
((f_cont i₀).comp (continuous_apply i₀)) },
haveI : polish_space t := t_closed.polish_space,
let F : t → α := λ x, f i₀ ((x : γ) i₀),
have F_cont : continuous F :=
(f_cont i₀).comp ((continuous_apply i₀).comp continuous_subtype_coe),
have F_range : range F = ⋂ (n : ι), s n,
{ apply subset.antisymm,
{ rintros y ⟨x, rfl⟩,
apply mem_Inter.2 (λ n, _),
have : f n ((x : γ) n) = F x := (mem_Inter.1 x.2 n : _),
rw [← this, ← f_range n],
exact mem_range_self _ },
{ assume y hy,
have A : ∀ n, ∃ (x : β n), f n x = y,
{ assume n,
rw [← mem_range, f_range n],
exact mem_Inter.1 hy n },
choose x hx using A,
have xt : x ∈ t,
{ apply mem_Inter.2 (λ n, _),
simp [hx] },
refine ⟨⟨x, xt⟩, _⟩,
exact hx i₀ } },
rw ← F_range,
exact analytic_set_range_of_polish_space F_cont,
end
/-- A countable union of analytic sets is analytic. -/
theorem analytic_set.Union [encodable ι] {s : ι → set α} (hs : ∀ n, analytic_set (s n)) :
analytic_set (⋃ n, s n) :=
begin
/- For the proof, write each `s n` as the continuous image under a map `f n` of a
Polish space `β n`. The union space `γ = Σ n, β n` is also Polish, and the map `F : γ → α` which
coincides with `f n` on `β n` sends it to `⋃ n, s n`. -/
choose β hβ h'β f f_cont f_range using λ n, analytic_set_iff_exists_polish_space_range.1 (hs n),
resetI,
let γ := Σ n, β n,
let F : γ → α := by { rintros ⟨n, x⟩, exact f n x },
have F_cont : continuous F := continuous_sigma f_cont,
have F_range : range F = ⋃ n, s n,
{ rw [range_sigma_eq_Union_range],
congr,
ext1 n,
rw ← f_range n },
rw ← F_range,
exact analytic_set_range_of_polish_space F_cont,
end
theorem _root_.is_closed.analytic_set [polish_space α] {s : set α} (hs : is_closed s) :
analytic_set s :=
begin
haveI : polish_space s := hs.polish_space,
rw ← @subtype.range_val α s,
exact analytic_set_range_of_polish_space continuous_subtype_coe,
end
/-- Given a Borel-measurable set in a Polish space, there exists a finer Polish topology making
it clopen. This is in fact an equivalence, see `is_clopenable_iff_measurable_set`. -/
lemma _root_.measurable_set.is_clopenable [polish_space α] [measurable_space α] [borel_space α]
{s : set α} (hs : measurable_set s) :
is_clopenable s :=
begin
revert s,
apply measurable_set.induction_on_open,
{ exact λ u hu, hu.is_clopenable },
{ exact λ u hu h'u, h'u.compl },
{ exact λ f f_disj f_meas hf, is_clopenable.Union hf }
end
theorem _root_.measurable_set.analytic_set
{α : Type*} [t : topological_space α] [polish_space α] [measurable_space α] [borel_space α]
{s : set α} (hs : measurable_set s) :
analytic_set s :=
begin
/- For a short proof (avoiding measurable induction), one sees `s` as a closed set for a finer
topology `t'`. It is analytic for this topology. As the identity from `t'` to `t` is continuous
and the image of an analytic set is analytic, it follows that `s` is also analytic for `t`. -/
obtain ⟨t', t't, t'_polish, s_closed, s_open⟩ :
∃ (t' : topological_space α), t' ≤ t ∧ @polish_space α t' ∧ @is_closed α t' s ∧
@is_open α t' s := hs.is_clopenable,
have A := @is_closed.analytic_set α t' t'_polish s s_closed,
convert @analytic_set.image_of_continuous α t' α t s A id (continuous_id_of_le t't),
simp only [id.def, image_id'],
end
/-- Given a Borel-measurable function from a Polish space to a second-countable space, there exists
a finer Polish topology on the source space for which the function is continuous. -/
lemma _root_.measurable.exists_continuous {α β : Type*}
[t : topological_space α] [polish_space α] [measurable_space α] [borel_space α]
[tβ : topological_space β] [second_countable_topology β] [measurable_space β] [borel_space β]
{f : α → β} (hf : measurable f) :
∃ (t' : topological_space α), t' ≤ t ∧ @continuous α β t' tβ f ∧ @polish_space α t' :=
begin
obtain ⟨b, b_count, -, hb⟩ : ∃b : set (set β), b.countable ∧ ∅ ∉ b ∧ is_topological_basis b :=
exists_countable_basis β,
haveI : encodable b := b_count.to_encodable,
have : ∀ (s : b), is_clopenable (f ⁻¹' s),
{ assume s,
apply measurable_set.is_clopenable,
exact hf (hb.is_open s.2).measurable_set },
choose T Tt Tpolish Tclosed Topen using this,
obtain ⟨t', t'T, t't, t'_polish⟩ :
∃ (t' : topological_space α), (∀ i, t' ≤ T i) ∧ (t' ≤ t) ∧ @polish_space α t' :=
exists_polish_space_forall_le T Tt Tpolish,
refine ⟨t', t't, _, t'_polish⟩,
apply hb.continuous _ (λ s hs, _),
exact t'T ⟨s, hs⟩ _ (Topen ⟨s, hs⟩),
end
/-! ### Separating sets with measurable sets -/
/-- Two sets `u` and `v` in a measurable space are measurably separable if there
exists a measurable set containing `u` and disjoint from `v`.
This is mostly interesting for Borel-separable sets. -/
def measurably_separable {α : Type*} [measurable_space α] (s t : set α) : Prop :=
∃ u, s ⊆ u ∧ disjoint t u ∧ measurable_set u
lemma measurably_separable.Union [encodable ι]
{α : Type*} [measurable_space α] {s t : ι → set α}
(h : ∀ m n, measurably_separable (s m) (t n)) :
measurably_separable (⋃ n, s n) (⋃ m, t m) :=
begin
choose u hsu htu hu using h,
refine ⟨⋃ m, (⋂ n, u m n), _, _, _⟩,
{ refine Union_subset (λ m, subset_Union_of_subset m _),
exact subset_Inter (λ n, hsu m n) },
{ simp_rw [disjoint_Union_left, disjoint_Union_right],
assume n m,
apply disjoint.mono_right _ (htu m n),
apply Inter_subset },
{ refine measurable_set.Union (λ m, _),
exact measurable_set.Inter (λ n, hu m n) }
end
/-- The hard part of the Lusin separation theorem saying that two disjoint analytic sets are
contained in disjoint Borel sets (see the full statement in `analytic_set.measurably_separable`).
Here, we prove this when our analytic sets are the ranges of functions from `ℕ → ℕ`.
-/
lemma measurably_separable_range_of_disjoint [t2_space α] [measurable_space α] [borel_space α]
{f g : (ℕ → ℕ) → α} (hf : continuous f) (hg : continuous g) (h : disjoint (range f) (range g)) :
measurably_separable (range f) (range g) :=
begin
/- We follow [Kechris, *Classical Descriptive Set Theory* (Theorem 14.7)][kechris1995].
If the ranges are not Borel-separated, then one can find two cylinders of length one whose images
are not Borel-separated, and then two smaller cylinders of length two whose images are not
Borel-separated, and so on. One thus gets two sequences of cylinders, that decrease to two
points `x` and `y`. Their images are different by the disjointness assumption, hence contained
in two disjoint open sets by the T2 property. By continuity, long enough cylinders around `x`
and `y` have images which are separated by these two disjoint open sets, a contradiction.
-/
by_contra hfg,
have I : ∀ n x y, (¬(measurably_separable (f '' (cylinder x n)) (g '' (cylinder y n))))
→ ∃ x' y', x' ∈ cylinder x n ∧ y' ∈ cylinder y n ∧
¬(measurably_separable (f '' (cylinder x' (n+1))) (g '' (cylinder y' (n+1)))),
{ assume n x y,
contrapose!,
assume H,
rw [← Union_cylinder_update x n, ← Union_cylinder_update y n, image_Union, image_Union],
refine measurably_separable.Union (λ i j, _),
exact H _ _ (update_mem_cylinder _ _ _) (update_mem_cylinder _ _ _) },
-- consider the set of pairs of cylinders of some length whose images are not Borel-separated
let A := {p : ℕ × (ℕ → ℕ) × (ℕ → ℕ) //
¬(measurably_separable (f '' (cylinder p.2.1 p.1)) (g '' (cylinder p.2.2 p.1)))},
-- for each such pair, one can find longer cylinders whose images are not Borel-separated either
have : ∀ (p : A), ∃ (q : A), q.1.1 = p.1.1 + 1 ∧ q.1.2.1 ∈ cylinder p.1.2.1 p.1.1
∧ q.1.2.2 ∈ cylinder p.1.2.2 p.1.1,
{ rintros ⟨⟨n, x, y⟩, hp⟩,
rcases I n x y hp with ⟨x', y', hx', hy', h'⟩,
exact ⟨⟨⟨n+1, x', y'⟩, h'⟩, rfl, hx', hy'⟩ },
choose F hFn hFx hFy using this,
let p0 : A := ⟨⟨0, λ n, 0, λ n, 0⟩, by simp [hfg]⟩,
-- construct inductively decreasing sequences of cylinders whose images are not separated
let p : ℕ → A := λ n, F^[n] p0,
have prec : ∀ n, p (n+1) = F (p n) := λ n, by simp only [p, iterate_succ'],
-- check that at the `n`-th step we deal with cylinders of length `n`
have pn_fst : ∀ n, (p n).1.1 = n,
{ assume n,
induction n with n IH,
{ refl },
{ simp only [prec, hFn, IH] } },
-- check that the cylinders we construct are indeed decreasing, by checking that the coordinates
-- are stationary.
have Ix : ∀ m n, m + 1 ≤ n → (p n).1.2.1 m = (p (m+1)).1.2.1 m,
{ assume m,
apply nat.le_induction,
{ refl },
assume n hmn IH,
have I : (F (p n)).val.snd.fst m = (p n).val.snd.fst m,
{ apply hFx (p n) m,
rw pn_fst,
exact hmn },
rw [prec, I, IH] },
have Iy : ∀ m n, m + 1 ≤ n → (p n).1.2.2 m = (p (m+1)).1.2.2 m,
{ assume m,
apply nat.le_induction,
{ refl },
assume n hmn IH,
have I : (F (p n)).val.snd.snd m = (p n).val.snd.snd m,
{ apply hFy (p n) m,
rw pn_fst,
exact hmn },
rw [prec, I, IH] },
-- denote by `x` and `y` the limit points of these two sequences of cylinders.
set x : ℕ → ℕ := λ n, (p (n+1)).1.2.1 n with hx,
set y : ℕ → ℕ := λ n, (p (n+1)).1.2.2 n with hy,
-- by design, the cylinders around these points have images which are not Borel-separable.
have M : ∀ n, ¬(measurably_separable (f '' (cylinder x n)) (g '' (cylinder y n))),
{ assume n,
convert (p n).2 using 3,
{ rw [pn_fst, ← mem_cylinder_iff_eq, mem_cylinder_iff],
assume i hi,
rw hx,
exact (Ix i n hi).symm },
{ rw [pn_fst, ← mem_cylinder_iff_eq, mem_cylinder_iff],
assume i hi,
rw hy,
exact (Iy i n hi).symm } },
-- consider two open sets separating `f x` and `g y`.
obtain ⟨u, v, u_open, v_open, xu, yv, huv⟩ :
∃ u v : set α, is_open u ∧ is_open v ∧ f x ∈ u ∧ g y ∈ v ∧ disjoint u v,
{ apply t2_separation,
exact disjoint_iff_forall_ne.1 h _ (mem_range_self _) _ (mem_range_self _) },
letI : metric_space (ℕ → ℕ) := metric_space_nat_nat,
obtain ⟨εx, εxpos, hεx⟩ : ∃ (εx : ℝ) (H : εx > 0), metric.ball x εx ⊆ f ⁻¹' u,
{ apply metric.mem_nhds_iff.1,
exact hf.continuous_at.preimage_mem_nhds (u_open.mem_nhds xu) },
obtain ⟨εy, εypos, hεy⟩ : ∃ (εy : ℝ) (H : εy > 0), metric.ball y εy ⊆ g ⁻¹' v,
{ apply metric.mem_nhds_iff.1,
exact hg.continuous_at.preimage_mem_nhds (v_open.mem_nhds yv) },
obtain ⟨n, hn⟩ : ∃ (n : ℕ), (1/2 : ℝ)^n < min εx εy :=
exists_pow_lt_of_lt_one (lt_min εxpos εypos) (by norm_num),
-- for large enough `n`, these open sets separate the images of long cylinders around `x` and `y`
have B : measurably_separable (f '' (cylinder x n)) (g '' (cylinder y n)),
{ refine ⟨u, _, _, u_open.measurable_set⟩,
{ rw image_subset_iff,
apply subset.trans _ hεx,
assume z hz,
rw mem_cylinder_iff_dist_le at hz,
exact hz.trans_lt (hn.trans_le (min_le_left _ _)) },
{ refine disjoint.mono_left _ huv.symm,
change g '' cylinder y n ⊆ v,
rw image_subset_iff,
apply subset.trans _ hεy,
assume z hz,
rw mem_cylinder_iff_dist_le at hz,
exact hz.trans_lt (hn.trans_le (min_le_right _ _)) } },
-- this is a contradiction.
exact M n B
end
/-- The Lusin separation theorem: if two analytic sets are disjoint, then they are contained in
disjoint Borel sets. -/
theorem analytic_set.measurably_separable [t2_space α] [measurable_space α] [borel_space α]
{s t : set α} (hs : analytic_set s) (ht : analytic_set t) (h : disjoint s t) :
measurably_separable s t :=
begin
rw analytic_set at hs ht,
rcases hs with rfl|⟨f, f_cont, rfl⟩,
{ refine ⟨∅, subset.refl _, by simp, measurable_set.empty⟩ },
rcases ht with rfl|⟨g, g_cont, rfl⟩,
{ exact ⟨univ, subset_univ _, by simp, measurable_set.univ⟩ },
exact measurably_separable_range_of_disjoint f_cont g_cont h,
end
/-! ### Injective images of Borel sets -/
variables {γ : Type*} [tγ : topological_space γ] [polish_space γ]
include tγ
/-- The Lusin-Souslin theorem: the range of a continuous injective function defined on a Polish
space is Borel-measurable. -/
theorem measurable_set_range_of_continuous_injective {β : Type*}
[topological_space β] [t2_space β] [measurable_space β] [borel_space β]
{f : γ → β} (f_cont : continuous f) (f_inj : injective f) :
measurable_set (range f) :=
begin
/- We follow [Fremlin, *Measure Theory* (volume 4, 423I)][fremlin_vol4].
Let `b = {s i}` be a countable basis for `α`. When `s i` and `s j` are disjoint, their images are
disjoint analytic sets, hence by the separation theorem one can find a Borel-measurable set
`q i j` separating them.
Let `E i = closure (f '' s i) ∩ ⋂ j, q i j \ q j i`. It contains `f '' (s i)` and it is
measurable. Let `F n = ⋃ E i`, where the union is taken over those `i` for which `diam (s i)`
is bounded by some number `u n` tending to `0` with `n`.
We claim that `range f = ⋂ F n`, from which the measurability is obvious. The inclusion `⊆` is
straightforward. To show `⊇`, consider a point `x` in the intersection. For each `n`, it belongs
to some `E i` with `diam (s i) ≤ u n`. Pick a point `y i ∈ s i`. We claim that for such `i`
and `j`, the intersection `s i ∩ s j` is nonempty: if it were empty, then thanks to the
separating set `q i j` in the definition of `E i` one could not have `x ∈ E i ∩ E j`.
Since these two sets have small diameter, it follows that `y i` and `y j` are close.
Thus, `y` is a Cauchy sequence, converging to a limit `z`. We claim that `f z = x`, completing
the proof.
Otherwise, one could find open sets `v` and `w` separating `f z` from `x`. Then, for large `n`,
the image `f '' (s i)` would be included in `v` by continuity of `f`, so its closure would be
contained in the closure of `v`, and therefore it would be disjoint from `w`. This is a
contradiction since `x` belongs both to this closure and to `w`. -/
letI := upgrade_polish_space γ,
obtain ⟨b, b_count, b_nonempty, hb⟩ :
∃ b : set (set γ), b.countable ∧ ∅ ∉ b ∧ is_topological_basis b := exists_countable_basis γ,
haveI : encodable b := b_count.to_encodable,
let A := {p : b × b // disjoint (p.1 : set γ) p.2},
-- for each pair of disjoint sets in the topological basis `b`, consider Borel sets separating
-- their images, by injectivity of `f` and the Lusin separation theorem.
have : ∀ (p : A), ∃ (q : set β), f '' (p.1.1 : set γ) ⊆ q ∧ disjoint (f '' (p.1.2 : set γ)) q
∧ measurable_set q,
{ assume p,
apply analytic_set.measurably_separable ((hb.is_open p.1.1.2).analytic_set_image f_cont)
((hb.is_open p.1.2.2).analytic_set_image f_cont),
exact disjoint.image p.2 (f_inj.inj_on univ) (subset_univ _) (subset_univ _) },
choose q hq1 hq2 q_meas using this,
-- define sets `E i` and `F n` as in the proof sketch above
let E : b → set β := λ s, closure (f '' s) ∩
(⋂ (t : b) (ht : disjoint s.1 t.1), q ⟨(s, t), ht⟩ \ q ⟨(t, s), ht.symm⟩),
obtain ⟨u, u_anti, u_pos, u_lim⟩ :
∃ (u : ℕ → ℝ), strict_anti u ∧ (∀ (n : ℕ), 0 < u n) ∧ tendsto u at_top (𝓝 0) :=
exists_seq_strict_anti_tendsto (0 : ℝ),
let F : ℕ → set β := λ n, ⋃ (s : b) (hs : bounded s.1 ∧ diam s.1 ≤ u n), E s,
-- it is enough to show that `range f = ⋂ F n`, as the latter set is obviously measurable.
suffices : range f = ⋂ n, F n,
{ have E_meas : ∀ (s : b), measurable_set (E s),
{ assume b,
refine is_closed_closure.measurable_set.inter _,
refine measurable_set.Inter (λ s, _),
exact measurable_set.Inter_Prop (λ hs, (q_meas _).diff (q_meas _)) },
have F_meas : ∀ n, measurable_set (F n),
{ assume n,
refine measurable_set.Union (λ s, _),
exact measurable_set.Union_Prop (λ hs, E_meas _) },
rw this,
exact measurable_set.Inter (λ n, F_meas n) },
-- we check both inclusions.
apply subset.antisymm,
-- we start with the easy inclusion `range f ⊆ ⋂ F n`. One just needs to unfold the definitions.
{ rintros x ⟨y, rfl⟩,
apply mem_Inter.2 (λ n, _),
obtain ⟨s, sb, ys, hs⟩ : ∃ (s : set γ) (H : s ∈ b), y ∈ s ∧ s ⊆ ball y (u n / 2),
{ apply hb.mem_nhds_iff.1,
exact ball_mem_nhds _ (half_pos (u_pos n)) },
have diam_s : diam s ≤ u n,
{ apply (diam_mono hs bounded_ball).trans,
convert diam_ball (half_pos (u_pos n)).le,
ring },
refine mem_Union.2 ⟨⟨s, sb⟩, _⟩,
refine mem_Union.2 ⟨⟨metric.bounded.mono hs bounded_ball, diam_s⟩, _⟩,
apply mem_inter (subset_closure (mem_image_of_mem _ ys)),
refine mem_Inter.2 (λ t, mem_Inter.2 (λ ht, ⟨_, _⟩)),
{ apply hq1,
exact mem_image_of_mem _ ys },
{ apply disjoint_left.1 (hq2 ⟨(t, ⟨s, sb⟩), ht.symm⟩),
exact mem_image_of_mem _ ys } },
-- Now, let us prove the harder inclusion `⋂ F n ⊆ range f`.
{ assume x hx,
-- pick for each `n` a good set `s n` of small diameter for which `x ∈ E (s n)`.
have C1 : ∀ n, ∃ (s : b) (hs : bounded s.1 ∧ diam s.1 ≤ u n), x ∈ E s :=
λ n, by simpa only [mem_Union] using mem_Inter.1 hx n,
choose s hs hxs using C1,
have C2 : ∀ n, (s n).1.nonempty,
{ assume n,
rw ← ne_empty_iff_nonempty,
assume hn,
have := (s n).2,
rw hn at this,
exact b_nonempty this },
-- choose a point `y n ∈ s n`.
choose y hy using C2,
have I : ∀ m n, ((s m).1 ∩ (s n).1).nonempty,
{ assume m n,
rw ← not_disjoint_iff_nonempty_inter,
by_contra' h,
have A : x ∈ q ⟨(s m, s n), h⟩ \ q ⟨(s n, s m), h.symm⟩,
{ have := mem_Inter.1 (hxs m).2 (s n), exact (mem_Inter.1 this h : _) },
have B : x ∈ q ⟨(s n, s m), h.symm⟩ \ q ⟨(s m, s n), h⟩,
{ have := mem_Inter.1 (hxs n).2 (s m), exact (mem_Inter.1 this h.symm : _) },
exact A.2 B.1 },
-- the points `y n` are nearby, and therefore they form a Cauchy sequence.
have cauchy_y : cauchy_seq y,
{ have : tendsto (λ n, 2 * u n) at_top (𝓝 0), by simpa only [mul_zero] using u_lim.const_mul 2,
apply cauchy_seq_of_le_tendsto_0' (λ n, 2 * u n) (λ m n hmn, _) this,
rcases I m n with ⟨z, zsm, zsn⟩,
calc dist (y m) (y n) ≤ dist (y m) z + dist z (y n) : dist_triangle _ _ _
... ≤ u m + u n :
add_le_add ((dist_le_diam_of_mem (hs m).1 (hy m) zsm).trans (hs m).2)
((dist_le_diam_of_mem (hs n).1 zsn (hy n)).trans (hs n).2)
... ≤ 2 * u m : by linarith [u_anti.antitone hmn] },
haveI : nonempty γ := ⟨y 0⟩,
-- let `z` be its limit.
let z := lim at_top y,
have y_lim : tendsto y at_top (𝓝 z) := cauchy_y.tendsto_lim,
suffices : f z = x, by { rw ← this, exact mem_range_self _ },
-- assume for a contradiction that `f z ≠ x`.
by_contra' hne,
-- introduce disjoint open sets `v` and `w` separating `f z` from `x`.
obtain ⟨v, w, v_open, w_open, fzv, xw, hvw⟩ := t2_separation hne,
obtain ⟨δ, δpos, hδ⟩ : ∃ δ > (0 : ℝ), ball z δ ⊆ f ⁻¹' v,
{ apply metric.mem_nhds_iff.1,
exact f_cont.continuous_at.preimage_mem_nhds (v_open.mem_nhds fzv) },
obtain ⟨n, hn⟩ : ∃ n, u n + dist (y n) z < δ,
{ have : tendsto (λ n, u n + dist (y n) z) at_top (𝓝 0),
by simpa only [add_zero] using u_lim.add (tendsto_iff_dist_tendsto_zero.1 y_lim),
exact ((tendsto_order.1 this).2 _ δpos).exists },
-- for large enough `n`, the image of `s n` is contained in `v`, by continuity of `f`.
have fsnv : f '' (s n) ⊆ v,
{ rw image_subset_iff,
apply subset.trans _ hδ,
assume a ha,
calc dist a z ≤ dist a (y n) + dist (y n) z : dist_triangle _ _ _
... ≤ u n + dist (y n) z :
add_le_add_right ((dist_le_diam_of_mem (hs n).1 ha (hy n)).trans (hs n).2) _
... < δ : hn },
-- as `x` belongs to the closure of `f '' (s n)`, it belongs to the closure of `v`.
have : x ∈ closure v := closure_mono fsnv (hxs n).1,
-- this is a contradiction, as `x` is supposed to belong to `w`, which is disjoint from
-- the closure of `v`.
exact disjoint_left.1 (hvw.closure_left w_open) this xw }
end
theorem _root_.is_closed.measurable_set_image_of_continuous_on_inj_on
{β : Type*} [topological_space β] [t2_space β] [measurable_space β] [borel_space β]
{s : set γ} (hs : is_closed s) {f : γ → β} (f_cont : continuous_on f s) (f_inj : inj_on f s) :
measurable_set (f '' s) :=
begin
rw image_eq_range,
haveI : polish_space s := is_closed.polish_space hs,
apply measurable_set_range_of_continuous_injective,
{ rwa continuous_on_iff_continuous_restrict at f_cont },
{ rwa inj_on_iff_injective at f_inj }
end
variables [measurable_space γ] [hγb : borel_space γ]
{β : Type*} [tβ : topological_space β] [t2_space β] [measurable_space β] [borel_space β]
{s : set γ} {f : γ → β}
include tβ hγb
/-- The Lusin-Souslin theorem: if `s` is Borel-measurable in a Polish space, then its image under
a continuous injective map is also Borel-measurable. -/
theorem _root_.measurable_set.image_of_continuous_on_inj_on
(hs : measurable_set s) (f_cont : continuous_on f s) (f_inj : inj_on f s) :
measurable_set (f '' s) :=
begin
obtain ⟨t', t't, t'_polish, s_closed, s_open⟩ :
∃ (t' : topological_space γ), t' ≤ tγ ∧ @polish_space γ t' ∧ @is_closed γ t' s ∧
@is_open γ t' s := hs.is_clopenable,
exact @is_closed.measurable_set_image_of_continuous_on_inj_on γ t' t'_polish β _ _ _ _ s
s_closed f (f_cont.mono_dom t't) f_inj,
end
/-- The Lusin-Souslin theorem: if `s` is Borel-measurable in a Polish space, then its image under
a measurable injective map taking values in a second-countable topological space
is also Borel-measurable. -/
theorem _root_.measurable_set.image_of_measurable_inj_on [second_countable_topology β]
(hs : measurable_set s) (f_meas : measurable f) (f_inj : inj_on f s) :
measurable_set (f '' s) :=
begin
-- for a finer Polish topology, `f` is continuous. Therefore, one may apply the corresponding
-- result for continuous maps.
obtain ⟨t', t't, f_cont, t'_polish⟩ :
∃ (t' : topological_space γ), t' ≤ tγ ∧ @continuous γ β t' tβ f ∧ @polish_space γ t' :=
f_meas.exists_continuous,
have M : measurable_set[@borel γ t'] s :=
@continuous.measurable γ γ t' (@borel γ t')
(@borel_space.opens_measurable γ t' (@borel γ t') (by { constructor, refl }))
tγ _ _ _ (continuous_id_of_le t't) s hs,
exact @measurable_set.image_of_continuous_on_inj_on γ t' t'_polish
(@borel γ t') (by { constructor, refl }) β _ _ _ _ s f M
(@continuous.continuous_on γ β t' tβ f s f_cont) f_inj,
end
/-- An injective continuous function on a Polish space is a measurable embedding. -/
theorem _root_.continuous.measurable_embedding (f_cont : continuous f) (f_inj : injective f) :
measurable_embedding f :=
{ injective := f_inj,
measurable := f_cont.measurable,
measurable_set_image' := λ u hu,
hu.image_of_continuous_on_inj_on f_cont.continuous_on (f_inj.inj_on _) }
/-- If `s` is Borel-measurable in a Polish space and `f` is continuous injective on `s`, then
the restriction of `f` to `s` is a measurable embedding. -/
theorem _root_.continuous_on.measurable_embedding (hs : measurable_set s)
(f_cont : continuous_on f s) (f_inj : inj_on f s) :
measurable_embedding (s.restrict f) :=
{ injective := inj_on_iff_injective.1 f_inj,
measurable := (continuous_on_iff_continuous_restrict.1 f_cont).measurable,
measurable_set_image' :=
begin
assume u hu,
have A : measurable_set ((coe : s → γ) '' u) :=
(measurable_embedding.subtype_coe hs).measurable_set_image.2 hu,
have B : measurable_set (f '' ((coe : s → γ) '' u)) :=
A.image_of_continuous_on_inj_on (f_cont.mono (subtype.coe_image_subset s u))
(f_inj.mono ((subtype.coe_image_subset s u))),
rwa ← image_comp at B,
end }
/-- An injective measurable function from a Polish space to a second-countable topological space
is a measurable embedding. -/
theorem _root_.measurable.measurable_embedding [second_countable_topology β]
(f_meas : measurable f) (f_inj : injective f) :
measurable_embedding f :=
{ injective := f_inj,
measurable := f_meas,
measurable_set_image' := λ u hu, hu.image_of_measurable_inj_on f_meas (f_inj.inj_on _) }
omit tβ
/-- In a Polish space, a set is clopenable if and only if it is Borel-measurable. -/
lemma is_clopenable_iff_measurable_set :
is_clopenable s ↔ measurable_set s :=
begin
-- we already know that a measurable set is clopenable. Conversely, assume that `s` is clopenable.
refine ⟨λ hs, _, λ hs, hs.is_clopenable⟩,
-- consider a finer topology `t'` in which `s` is open and closed.
obtain ⟨t', t't, t'_polish, s_closed, s_open⟩ :
∃ (t' : topological_space γ), t' ≤ tγ ∧ @polish_space γ t' ∧ @is_closed γ t' s ∧
@is_open γ t' s := hs,
-- the identity is continuous from `t'` to `tγ`.
have C : @continuous γ γ t' tγ id := continuous_id_of_le t't,
-- therefore, it is also a measurable embedding, by the Lusin-Souslin theorem
have E := @continuous.measurable_embedding γ t' t'_polish (@borel γ t') (by { constructor, refl })
γ tγ (polish_space.t2_space γ) _ _ id C injective_id,
-- the set `s` is measurable for `t'` as it is closed.
have M : @measurable_set γ (@borel γ t') s :=
@is_closed.measurable_set γ s t' (@borel γ t')
(@borel_space.opens_measurable γ t' (@borel γ t') (by { constructor, refl })) s_closed,
-- therefore, its image under the measurable embedding `id` is also measurable for `tγ`.
convert E.measurable_set_image.2 M,
simp only [id.def, image_id'],
end
omit hγb
/-- The set of points for which a measurable sequence of functions converges is measurable. -/
@[measurability] lemma measurable_set_exists_tendsto
[hγ : opens_measurable_space γ] [countable ι] {l : filter ι}
[l.is_countably_generated] {f : ι → β → γ} (hf : ∀ i, measurable (f i)) :
measurable_set {x | ∃ c, tendsto (λ n, f n x) l (𝓝 c)} :=
begin
by_cases hl : l.ne_bot,
swap, { rw not_ne_bot at hl, simp [hl] },
letI := upgrade_polish_space γ,
rcases l.exists_antitone_basis with ⟨u, hu⟩,
simp_rw ← cauchy_map_iff_exists_tendsto,
change measurable_set {x | _ ∧ _},
have : ∀ x, ((map (λ i, f i x) l) ×ᶠ (map (λ i, f i x) l)).has_antitone_basis
(λ n, ((λ i, f i x) '' u n) ×ˢ ((λ i, f i x) '' u n)) := λ x, hu.map.prod hu.map,
simp_rw [and_iff_right (hl.map _), filter.has_basis.le_basis_iff (this _).to_has_basis
metric.uniformity_basis_dist_inv_nat_succ, set.set_of_forall],
refine measurable_set.bInter set.countable_univ (λ K _, _),
simp_rw set.set_of_exists,
refine measurable_set.bUnion set.countable_univ (λ N hN, _),
simp_rw [prod_image_image_eq, image_subset_iff, prod_subset_iff, set.set_of_forall],
exact measurable_set.bInter (to_countable (u N)) (λ i _,
measurable_set.bInter (to_countable (u N)) (λ j _,
measurable_set_lt (measurable.dist (hf i) (hf j)) measurable_const)),
end
end measure_theory
|
0c9c258778583a6332cbba2b22e816ed9172194f | 798dd332c1ad790518589a09bc82459fb12e5156 | /logic/basic.lean | dde642f1337ad75b654a86037454b2fca9aaae6a | [
"Apache-2.0"
] | permissive | tobiasgrosser/mathlib | b040b7eb42d5942206149371cf92c61404de3c31 | 120635628368ec261e031cefc6d30e0304088b03 | refs/heads/master | 1,644,803,442,937 | 1,536,663,752,000 | 1,536,663,907,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 24,024 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura
Theorems that require decidability hypotheses are in the namespace "decidable".
Classical versions are in the namespace "classical".
Note: in the presence of automation, this whole file may be unnecessary. On the other hand,
maybe it is useful for writing automation.
-/
import data.prod tactic.cache
/-
miscellany
TODO: move elsewhere
-/
section miscellany
variables {α : Type*} {β : Type*}
def empty.elim {C : Sort*} : empty → C.
instance : subsingleton empty := ⟨λa, a.elim⟩
instance : decidable_eq empty := λa, a.elim
@[priority 0] instance decidable_eq_of_subsingleton
{α} [subsingleton α] : decidable_eq α
| a b := is_true (subsingleton.elim a b)
/- Add an instance to "undo" coercion transitivity into a chain of coercions, because
most simp lemmas are stated with respect to simple coercions and will not match when
part of a chain. -/
@[simp] theorem coe_coe {α β γ} [has_coe α β] [has_coe_t β γ]
(a : α) : (a : γ) = (a : β) := rfl
/-- `pempty` is the universe-polymorphic analogue of `empty`. -/
@[derive decidable_eq]
inductive {u} pempty : Sort u
def pempty.elim {C : Sort*} : pempty → C.
instance subsingleton_pempty : subsingleton pempty := ⟨λa, a.elim⟩
end miscellany
/-
propositional connectives
-/
@[simp] theorem false_ne_true : false ≠ true
| h := h.symm ▸ trivial
section propositional
variables {a b c d : Prop}
/- implies -/
theorem iff_of_eq (e : a = b) : a ↔ b := e ▸ iff.rfl
theorem iff_iff_eq : (a ↔ b) ↔ a = b := ⟨propext, iff_of_eq⟩
@[simp] theorem imp_self : (a → a) ↔ true := iff_true_intro id
theorem imp_intro {α β} (h : α) (h₂ : β) : α := h
theorem imp_false : (a → false) ↔ ¬ a := iff.rfl
theorem imp_and_distrib {α} : (α → b ∧ c) ↔ (α → b) ∧ (α → c) :=
⟨λ h, ⟨λ ha, (h ha).left, λ ha, (h ha).right⟩,
λ h ha, ⟨h.left ha, h.right ha⟩⟩
@[simp] theorem and_imp : (a ∧ b → c) ↔ (a → b → c) :=
iff.intro (λ h ha hb, h ⟨ha, hb⟩) (λ h ⟨ha, hb⟩, h ha hb)
theorem iff_def : (a ↔ b) ↔ (a → b) ∧ (b → a) :=
iff_iff_implies_and_implies _ _
theorem iff_def' : (a ↔ b) ↔ (b → a) ∧ (a → b) :=
iff_def.trans and.comm
@[simp] theorem imp_true_iff {α : Sort*} : (α → true) ↔ true :=
iff_true_intro $ λ_, trivial
@[simp] theorem imp_iff_right (ha : a) : (a → b) ↔ b :=
⟨λf, f ha, imp_intro⟩
/- not -/
theorem not.elim {α : Sort*} (H1 : ¬a) (H2 : a) : α := absurd H2 H1
@[reducible] theorem not.imp {a b : Prop} (H2 : ¬b) (H1 : a → b) : ¬a := mt H1 H2
theorem not_not_of_not_imp : ¬(a → b) → ¬¬a :=
mt not.elim
theorem not_of_not_imp {α} : ¬(α → b) → ¬b :=
mt imp_intro
theorem dec_em (p : Prop) [decidable p] : p ∨ ¬p := decidable.em p
theorem by_contradiction {p} [decidable p] : (¬p → false) → p :=
decidable.by_contradiction
@[simp] theorem not_not [decidable a] : ¬¬a ↔ a :=
iff.intro by_contradiction not_not_intro
theorem of_not_not [decidable a] : ¬¬a → a :=
by_contradiction
theorem of_not_imp [decidable a] (h : ¬ (a → b)) : a :=
by_contradiction (not_not_of_not_imp h)
theorem not.imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a :=
by_contradiction $ hb ∘ h
theorem not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) :=
⟨not.imp_symm, not.imp_symm⟩
theorem imp.swap : (a → b → c) ↔ (b → a → c) :=
⟨function.swap, function.swap⟩
theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) :=
imp.swap
/- and -/
theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) :=
mt and.left
theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) :=
mt and.right
theorem and.imp_left (h : a → b) : a ∧ c → b ∧ c :=
and.imp h id
theorem and.imp_right (h : a → b) : c ∧ a → c ∧ b :=
and.imp id h
lemma and.right_comm : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b :=
by simp [and.left_comm, and.comm]
lemma and.rotate : a ∧ b ∧ c ↔ b ∧ c ∧ a :=
by simp [and.left_comm, and.comm]
theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false :=
iff.intro (assume h, (h.right) (h.left)) (assume h, h.elim)
theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false :=
iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim
theorem and_iff_left_of_imp {a b : Prop} (h : a → b) : (a ∧ b) ↔ a :=
iff.intro and.left (λ ha, ⟨ha, h ha⟩)
theorem and_iff_right_of_imp {a b : Prop} (h : b → a) : (a ∧ b) ↔ b :=
iff.intro and.right (λ hb, ⟨h hb, hb⟩)
lemma and.congr_right_iff : (a ∧ b ↔ a ∧ c) ↔ (a → (b ↔ c)) :=
⟨λ h ha, by simp [ha] at h; exact h, and_congr_right⟩
/- or -/
theorem or_of_or_of_imp_of_imp (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d :=
or.imp h₂ h₃ h₁
theorem or_of_or_of_imp_left (h₁ : a ∨ c) (h : a → b) : b ∨ c :=
or.imp_left h h₁
theorem or_of_or_of_imp_right (h₁ : c ∨ a) (h : a → b) : c ∨ b :=
or.imp_right h h₁
theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d :=
or.elim h ha (assume h₂, or.elim h₂ hb hc)
theorem or_imp_distrib : (a ∨ b → c) ↔ (a → c) ∧ (b → c) :=
⟨assume h, ⟨assume ha, h (or.inl ha), assume hb, h (or.inr hb)⟩,
assume ⟨ha, hb⟩, or.rec ha hb⟩
theorem or_iff_not_imp_left [decidable a] : a ∨ b ↔ (¬ a → b) :=
⟨or.resolve_left, λ h, dite _ or.inl (or.inr ∘ h)⟩
theorem or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) :=
or.comm.trans or_iff_not_imp_left
theorem not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) :=
⟨assume h hb, by_contradiction $ assume na, h na hb, mt⟩
/- distributivity -/
theorem and_or_distrib_left : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) :=
⟨λ ⟨ha, hbc⟩, hbc.imp (and.intro ha) (and.intro ha),
or.rec (and.imp_right or.inl) (and.imp_right or.inr)⟩
theorem or_and_distrib_right : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) :=
(and.comm.trans and_or_distrib_left).trans (or_congr and.comm and.comm)
theorem or_and_distrib_left : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) :=
⟨or.rec (λha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr),
and.rec $ or.rec (imp_intro ∘ or.inl) (or.imp_right ∘ and.intro)⟩
theorem and_or_distrib_right : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) :=
(or.comm.trans or_and_distrib_left).trans (and_congr or.comm or.comm)
/- iff -/
theorem iff_of_true (ha : a) (hb : b) : a ↔ b :=
⟨λ_, hb, λ _, ha⟩
theorem iff_of_false (ha : ¬a) (hb : ¬b) : a ↔ b :=
⟨ha.elim, hb.elim⟩
theorem iff_true_left (ha : a) : (a ↔ b) ↔ b :=
⟨λ h, h.1 ha, iff_of_true ha⟩
theorem iff_true_right (ha : a) : (b ↔ a) ↔ b :=
iff.comm.trans (iff_true_left ha)
theorem iff_false_left (ha : ¬a) : (a ↔ b) ↔ ¬b :=
⟨λ h, mt h.2 ha, iff_of_false ha⟩
theorem iff_false_right (ha : ¬a) : (b ↔ a) ↔ ¬b :=
iff.comm.trans (iff_false_left ha)
theorem not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b :=
if ha : a then or.inr (h ha) else or.inl ha
theorem imp_iff_not_or [decidable a] : (a → b) ↔ (¬ a ∨ b) :=
⟨not_or_of_imp, or.neg_resolve_left⟩
theorem imp_or_distrib [decidable a] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) :=
by simp [imp_iff_not_or, or.comm, or.left_comm]
theorem imp_or_distrib' [decidable b] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) :=
by by_cases b; simp [h, or_iff_right_of_imp ((∘) false.elim)]
theorem not_imp_of_and_not : a ∧ ¬ b → ¬ (a → b)
| ⟨ha, hb⟩ h := hb $ h ha
@[simp] theorem not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b :=
⟨λ h, ⟨of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩
theorem peirce (a b : Prop) [decidable a] : ((a → b) → a) → a :=
if ha : a then λ h, ha else λ h, h ha.elim
theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id
theorem not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) :=
by rw [@iff_def (¬ a), @iff_def' a]; exact and_congr not_imp_not not_imp_not
theorem not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) :=
by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact and_congr not_imp_comm imp_not_comm
theorem not_iff [decidable a] [decidable b] : ¬ (a ↔ b) ↔ (¬ a ↔ b) :=
by split; intro h; [split, skip]; intro h'; [by_contradiction,intro,skip];
try { refine h _; simp [*] }; rw [h',not_iff_self] at h; exact h
theorem iff_not_comm [decidable a] [decidable b] : (a ↔ ¬ b) ↔ (b ↔ ¬ a) :=
by rw [@iff_def a, @iff_def b]; exact and_congr imp_not_comm not_imp_comm
theorem iff_iff_and_or_not_and_not [decidable b] : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) :=
by { split; intro h,
{ rw h; by_cases b; [left,right]; split; assumption },
{ cases h with h h; cases h; split; intro; { contradiction <|> assumption } } }
@[simp] theorem not_and_not_right [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) :=
⟨λ h ha, h.imp_symm $ and.intro ha, λ h ⟨ha, hb⟩, hb $ h ha⟩
@[inline] def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b :=
decidable_of_decidable_of_iff D h
@[inline] def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a :=
decidable_of_decidable_of_iff D h.symm
def decidable_of_bool : ∀ (b : bool) (h : b ↔ a), decidable a
| tt h := is_true (h.1 rfl)
| ff h := is_false (mt h.2 bool.ff_ne_tt)
/- de morgan's laws -/
theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b)
| ⟨ha, hb⟩ := or.elim h (absurd ha) (absurd hb)
theorem not_and_distrib [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b :=
⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩
theorem not_and_distrib' [decidable b] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b :=
⟨λ h, if hb : b then or.inl (λ ha, h ⟨ha, hb⟩) else or.inr hb, not_and_of_not_or_not⟩
@[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp
theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a :=
not_and.trans imp_not_comm
theorem not_or_distrib : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b :=
⟨λ h, ⟨λ ha, h (or.inl ha), λ hb, h (or.inr hb)⟩,
λ ⟨h₁, h₂⟩ h, or.elim h h₁ h₂⟩
theorem or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) :=
by rw [← not_or_distrib, not_not]
theorem and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) :=
by rw [← not_and_distrib, not_not]
end propositional
/- equality -/
section equality
variables {α : Sort*} {a b : α}
@[simp] theorem heq_iff_eq : a == b ↔ a = b :=
⟨eq_of_heq, heq_of_eq⟩
theorem proof_irrel_heq {p q : Prop} (hp : p) (hq : q) : hp == hq :=
have p = q, from propext ⟨λ _, hq, λ _, hp⟩,
by subst q; refl
theorem ne_of_mem_of_not_mem {α β} [has_mem α β] {s : β} {a b : α}
(h : a ∈ s) : b ∉ s → a ≠ b :=
mt $ λ e, e ▸ h
theorem eq_equivalence : equivalence (@eq α) :=
⟨eq.refl, @eq.symm _, @eq.trans _⟩
lemma heq_of_eq_mp :
∀ {α β : Sort*} {a : α} {a' : β} (e : α = β) (h₂ : (eq.mp e a) = a'), a == a'
| α ._ a a' rfl h := eq.rec_on h (heq.refl _)
end equality
/-
quantifiers
-/
section quantifiers
variables {α : Sort*} {p q : α → Prop} {b : Prop}
def Exists.imp := @exists_imp_exists
theorem forall_swap {α β} {p : α → β → Prop} : (∀ x y, p x y) ↔ ∀ y x, p x y :=
⟨function.swap, function.swap⟩
theorem exists_swap {α β} {p : α → β → Prop} : (∃ x y, p x y) ↔ ∃ y x, p x y :=
⟨λ ⟨x, y, h⟩, ⟨y, x, h⟩, λ ⟨y, x, h⟩, ⟨x, y, h⟩⟩
@[simp] theorem exists_imp_distrib : ((∃ x, p x) → b) ↔ ∀ x, p x → b :=
⟨λ h x hpx, h ⟨x, hpx⟩, λ h ⟨x, hpx⟩, h x hpx⟩
--theorem forall_not_of_not_exists (h : ¬ ∃ x, p x) : ∀ x, ¬ p x :=
--forall_imp_of_exists_imp h
theorem not_exists_of_forall_not (h : ∀ x, ¬ p x) : ¬ ∃ x, p x :=
exists_imp_distrib.2 h
@[simp] theorem not_exists : (¬ ∃ x, p x) ↔ ∀ x, ¬ p x :=
exists_imp_distrib
theorem not_forall_of_exists_not : (∃ x, ¬ p x) → ¬ ∀ x, p x
| ⟨x, hn⟩ h := hn (h x)
theorem not_forall {p : α → Prop}
[decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] :
(¬ ∀ x, p x) ↔ ∃ x, ¬ p x :=
⟨not.imp_symm $ λ nx x, nx.imp_symm $ λ h, ⟨x, h⟩,
not_forall_of_exists_not⟩
@[simp] theorem not_forall_not [decidable (∃ x, p x)] :
(¬ ∀ x, ¬ p x) ↔ ∃ x, p x :=
by haveI := decidable_of_iff (¬ ∃ x, p x) not_exists;
exact not_iff_comm.1 not_exists
@[simp] theorem not_exists_not [∀ x, decidable (p x)] :
(¬ ∃ x, ¬ p x) ↔ ∀ x, p x :=
by simp
@[simp] theorem forall_true_iff : (α → true) ↔ true :=
iff_true_intro (λ _, trivial)
-- Unfortunately this causes simp to loop sometimes, so we
-- add the 2 and 3 cases as simp lemmas instead
theorem forall_true_iff' (h : ∀ a, p a ↔ true) : (∀ a, p a) ↔ true :=
iff_true_intro (λ _, of_iff_true (h _))
@[simp] theorem forall_2_true_iff {β : α → Sort*} : (∀ a, β a → true) ↔ true :=
forall_true_iff' $ λ _, forall_true_iff
@[simp] theorem forall_3_true_iff {β : α → Sort*} {γ : Π a, β a → Sort*} :
(∀ a (b : β a), γ a b → true) ↔ true :=
forall_true_iff' $ λ _, forall_2_true_iff
@[simp] theorem forall_const (α : Sort*) [inhabited α] : (α → b) ↔ b :=
⟨λ h, h (arbitrary α), λ hb x, hb⟩
@[simp] theorem exists_const (α : Sort*) [inhabited α] : (∃ x : α, b) ↔ b :=
⟨λ ⟨x, h⟩, h, λ h, ⟨arbitrary α, h⟩⟩
theorem forall_and_distrib : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) :=
⟨λ h, ⟨λ x, (h x).left, λ x, (h x).right⟩, λ ⟨h₁, h₂⟩ x, ⟨h₁ x, h₂ x⟩⟩
theorem exists_or_distrib : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) :=
⟨λ ⟨x, hpq⟩, hpq.elim (λ hpx, or.inl ⟨x, hpx⟩) (λ hqx, or.inr ⟨x, hqx⟩),
λ hepq, hepq.elim (λ ⟨x, hpx⟩, ⟨x, or.inl hpx⟩) (λ ⟨x, hqx⟩, ⟨x, or.inr hqx⟩)⟩
@[simp] theorem exists_and_distrib_left {q : Prop} {p : α → Prop} :
(∃x, q ∧ p x) ↔ q ∧ (∃x, p x) :=
⟨λ ⟨x, hq, hp⟩, ⟨hq, x, hp⟩, λ ⟨hq, x, hp⟩, ⟨x, hq, hp⟩⟩
@[simp] theorem exists_and_distrib_right {q : Prop} {p : α → Prop} :
(∃x, p x ∧ q) ↔ (∃x, p x) ∧ q :=
by simp [and_comm]
@[simp] theorem forall_eq {a' : α} : (∀a, a = a' → p a) ↔ p a' :=
⟨λ h, h a' rfl, λ h a e, e.symm ▸ h⟩
@[simp] theorem exists_eq {a' : α} : ∃ a, a = a' := ⟨_, rfl⟩
@[simp] theorem exists_eq_left {a' : α} : (∃ a, a = a' ∧ p a) ↔ p a' :=
⟨λ ⟨a, e, h⟩, e ▸ h, λ h, ⟨_, rfl, h⟩⟩
@[simp] theorem exists_eq_right {a' : α} : (∃ a, p a ∧ a = a') ↔ p a' :=
(exists_congr $ by exact λ a, and.comm).trans exists_eq_left
@[simp] theorem forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' :=
by simp [@eq_comm _ a']
@[simp] theorem exists_eq_left' {a' : α} : (∃ a, a' = a ∧ p a) ↔ p a' :=
by simp [@eq_comm _ a']
@[simp] theorem exists_eq_right' {a' : α} : (∃ a, p a ∧ a' = a) ↔ p a' :=
by simp [@eq_comm _ a']
theorem forall_or_of_or_forall (h : b ∨ ∀x, p x) (x) : b ∨ p x :=
h.imp_right $ λ h₂, h₂ x
theorem forall_or_distrib_left {q : Prop} {p : α → Prop} [decidable q] :
(∀x, q ∨ p x) ↔ q ∨ (∀x, p x) :=
⟨λ h, if hq : q then or.inl hq else or.inr $ λ x, (h x).resolve_left hq,
forall_or_of_or_forall⟩
@[simp] theorem exists_prop {p q : Prop} : (∃ h : p, q) ↔ p ∧ q :=
⟨λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩⟩
@[simp] theorem exists_false : ¬ (∃a:α, false) := assume ⟨a, h⟩, h
theorem Exists.fst {p : b → Prop} : Exists p → b
| ⟨h, _⟩ := h
theorem Exists.snd {p : b → Prop} : ∀ h : Exists p, p h.fst
| ⟨_, h⟩ := h
@[simp] theorem forall_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∀ h' : p, q h') ↔ q h :=
@forall_const (q h) p ⟨h⟩
@[simp] theorem exists_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃ h' : p, q h') ↔ q h :=
@exists_const (q h) p ⟨h⟩
@[simp] theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ true :=
iff_true_intro $ λ h, hn.elim h
@[simp] theorem exists_prop_of_false {p : Prop} {q : p → Prop} : ¬ p → ¬ (∃ h' : p, q h') :=
mt Exists.fst
end quantifiers
/- classical versions -/
namespace classical
variables {α : Sort*} {p : α → Prop}
local attribute [instance] prop_decidable
protected theorem not_forall : (¬ ∀ x, p x) ↔ (∃ x, ¬ p x) := not_forall
protected theorem forall_or_distrib_left {q : Prop} {p : α → Prop} :
(∀x, q ∨ p x) ↔ q ∨ (∀x, p x) :=
forall_or_distrib_left
theorem cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a :=
assume a, cases_on a h1 h2
theorem or_not {p : Prop} : p ∨ ¬ p :=
by_cases or.inl or.inr
protected theorem or_iff_not_imp_left {p q : Prop} : p ∨ q ↔ (¬ p → q) :=
or_iff_not_imp_left
protected theorem or_iff_not_imp_right {p q : Prop} : q ∨ p ↔ (¬ p → q) :=
or_iff_not_imp_right
/- use shortened names to avoid conflict when classical namespace is open -/
noncomputable theorem dec (p : Prop) : decidable p := by apply_instance
noncomputable theorem dec_pred (p : α → Prop) : decidable_pred p := by apply_instance
noncomputable theorem dec_rel (p : α → α → Prop) : decidable_rel p := by apply_instance
noncomputable theorem dec_eq (α : Sort*) : decidable_eq α := by apply_instance
@[elab_as_eliminator]
noncomputable def {u} rec_on {C : Sort u} (h : ∃ a, p a) (H : ∀ a, p a → C) : C :=
H (classical.some h) (classical.some_spec h)
lemma some_spec2 {α : Type*} {p : α → Prop} {h : ∃a, p a}
(q : α → Prop) (hpq : ∀a, p a → q a) : q (some h) :=
hpq _ $ some_spec _
end classical
/-
bounded quantifiers
-/
section bounded_quantifiers
variables {α : Sort*} {r p q : α → Prop} {P Q : ∀ x, p x → Prop} {b : Prop}
theorem bex_def : (∃ x (h : p x), q x) ↔ ∃ x, p x ∧ q x :=
⟨λ ⟨x, px, qx⟩, ⟨x, px, qx⟩, λ ⟨x, px, qx⟩, ⟨x, px, qx⟩⟩
theorem bex.elim {b : Prop} : (∃ x h, P x h) → (∀ a h, P a h → b) → b
| ⟨a, h₁, h₂⟩ h' := h' a h₁ h₂
theorem bex.intro (a : α) (h₁ : p a) (h₂ : P a h₁) : ∃ x (h : p x), P x h :=
⟨a, h₁, h₂⟩
theorem ball_congr (H : ∀ x h, P x h ↔ Q x h) :
(∀ x h, P x h) ↔ (∀ x h, Q x h) :=
forall_congr $ λ x, forall_congr (H x)
theorem bex_congr (H : ∀ x h, P x h ↔ Q x h) :
(∃ x h, P x h) ↔ (∃ x h, Q x h) :=
exists_congr $ λ x, exists_congr (H x)
theorem ball.imp_right (H : ∀ x h, (P x h → Q x h))
(h₁ : ∀ x h, P x h) (x h) : Q x h :=
H _ _ $ h₁ _ _
theorem bex.imp_right (H : ∀ x h, (P x h → Q x h)) :
(∃ x h, P x h) → ∃ x h, Q x h
| ⟨x, h, h'⟩ := ⟨_, _, H _ _ h'⟩
theorem ball.imp_left (H : ∀ x, p x → q x)
(h₁ : ∀ x, q x → r x) (x) (h : p x) : r x :=
h₁ _ $ H _ h
theorem bex.imp_left (H : ∀ x, p x → q x) :
(∃ x (_ : p x), r x) → ∃ x (_ : q x), r x
| ⟨x, hp, hr⟩ := ⟨x, H _ hp, hr⟩
theorem ball_of_forall (h : ∀ x, p x) (x) (_ : q x) : p x :=
h x
theorem forall_of_ball (H : ∀ x, p x) (h : ∀ x, p x → q x) (x) : q x :=
h x $ H x
theorem bex_of_exists (H : ∀ x, p x) : (∃ x, q x) → ∃ x (_ : p x), q x
| ⟨x, hq⟩ := ⟨x, H x, hq⟩
theorem exists_of_bex : (∃ x (_ : p x), q x) → ∃ x, q x
| ⟨x, _, hq⟩ := ⟨x, hq⟩
@[simp] theorem bex_imp_distrib : ((∃ x h, P x h) → b) ↔ (∀ x h, P x h → b) :=
by simp
theorem not_bex : (¬ ∃ x h, P x h) ↔ ∀ x h, ¬ P x h :=
bex_imp_distrib
theorem not_ball_of_bex_not : (∃ x h, ¬ P x h) → ¬ ∀ x h, P x h
| ⟨x, h, hp⟩ al := hp $ al x h
theorem not_ball [decidable (∃ x h, ¬ P x h)] [∀ x h, decidable (P x h)] :
(¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) :=
⟨not.imp_symm $ λ nx x h, nx.imp_symm $ λ h', ⟨x, h, h'⟩,
not_ball_of_bex_not⟩
theorem ball_true_iff (p : α → Prop) : (∀ x, p x → true) ↔ true :=
iff_true_intro (λ h hrx, trivial)
theorem ball_and_distrib : (∀ x h, P x h ∧ Q x h) ↔ (∀ x h, P x h) ∧ (∀ x h, Q x h) :=
iff.trans (forall_congr $ λ x, forall_and_distrib) forall_and_distrib
theorem bex_or_distrib : (∃ x h, P x h ∨ Q x h) ↔ (∃ x h, P x h) ∨ (∃ x h, Q x h) :=
iff.trans (exists_congr $ λ x, exists_or_distrib) exists_or_distrib
end bounded_quantifiers
namespace classical
local attribute [instance] prop_decidable
theorem not_ball {α : Sort*} {p : α → Prop} {P : Π (x : α), p x → Prop} :
(¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := _root_.not_ball
end classical
section nonempty
universes u v w
variables {α : Type u} {β : Type v} {γ : α → Type w}
attribute [simp] nonempty_of_inhabited
lemma exists_true_iff_nonempty {α : Sort*} : (∃a:α, true) ↔ nonempty α :=
iff.intro (λ⟨a, _⟩, ⟨a⟩) (λ⟨a⟩, ⟨a, trivial⟩)
@[simp] lemma nonempty_Prop {p : Prop} : nonempty p ↔ p :=
iff.intro (assume ⟨h⟩, h) (assume h, ⟨h⟩)
lemma not_nonempty_iff_imp_false {p : Prop} : ¬ nonempty α ↔ α → false :=
⟨λ h a, h ⟨a⟩, λ h ⟨a⟩, h a⟩
@[simp] lemma nonempty_sigma : nonempty (Σa:α, γ a) ↔ (∃a:α, nonempty (γ a)) :=
iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩)
@[simp] lemma nonempty_subtype {α : Sort u} {p : α → Prop} : nonempty (subtype p) ↔ (∃a:α, p a) :=
iff.intro (assume ⟨⟨a, h⟩⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a, h⟩⟩)
@[simp] lemma nonempty_prod : nonempty (α × β) ↔ (nonempty α ∧ nonempty β) :=
iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩)
@[simp] lemma nonempty_pprod {α : Sort u} {β : Sort v} :
nonempty (pprod α β) ↔ (nonempty α ∧ nonempty β) :=
iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩)
@[simp] lemma nonempty_sum : nonempty (α ⊕ β) ↔ (nonempty α ∨ nonempty β) :=
iff.intro
(assume ⟨h⟩, match h with sum.inl a := or.inl ⟨a⟩ | sum.inr b := or.inr ⟨b⟩ end)
(assume h, match h with or.inl ⟨a⟩ := ⟨sum.inl a⟩ | or.inr ⟨b⟩ := ⟨sum.inr b⟩ end)
@[simp] lemma nonempty_psum {α : Sort u} {β : Sort v} :
nonempty (psum α β) ↔ (nonempty α ∨ nonempty β) :=
iff.intro
(assume ⟨h⟩, match h with psum.inl a := or.inl ⟨a⟩ | psum.inr b := or.inr ⟨b⟩ end)
(assume h, match h with or.inl ⟨a⟩ := ⟨psum.inl a⟩ | or.inr ⟨b⟩ := ⟨psum.inr b⟩ end)
@[simp] lemma nonempty_psigma {α : Sort u} {β : α → Sort v} :
nonempty (psigma β) ↔ (∃a:α, nonempty (β a)) :=
iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩)
@[simp] lemma nonempty_empty : ¬ nonempty empty :=
assume ⟨h⟩, h.elim
@[simp] lemma nonempty_ulift : nonempty (ulift α) ↔ nonempty α :=
iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩)
@[simp] lemma nonempty_plift {α : Sort u} : nonempty (plift α) ↔ nonempty α :=
iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩)
@[simp] lemma nonempty.forall {α : Sort u} {p : nonempty α → Prop} :
(∀h:nonempty α, p h) ↔ (∀a, p ⟨a⟩) :=
iff.intro (assume h a, h _) (assume h ⟨a⟩, h _)
@[simp] lemma nonempty.exists {α : Sort u} {p : nonempty α → Prop} :
(∃h:nonempty α, p h) ↔ (∃a, p ⟨a⟩) :=
iff.intro (assume ⟨⟨a⟩, h⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a⟩, h⟩)
lemma classical.nonempty_pi {α : Sort u} {β : α → Sort v} :
nonempty (Πa:α, β a) ↔ (∀a:α, nonempty (β a)) :=
iff.intro (assume ⟨f⟩ a, ⟨f a⟩) (assume f, ⟨assume a, classical.choice $ f a⟩)
end nonempty
|
0f2355cd65f653f9fcadae5cbe92e4990c776e2f | ea5678cc400c34ff95b661fa26d15024e27ea8cd | /uncountable_real.lean | 53016db4b78aae2af8a325d3235fab0cefc37a75 | [] | no_license | ChrisHughes24/leanstuff | dca0b5349c3ed893e8792ffbd98cbcadaff20411 | 9efa85f72efaccd1d540385952a6acc18fce8687 | refs/heads/master | 1,654,883,241,759 | 1,652,873,885,000 | 1,652,873,885,000 | 134,599,537 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,020 | lean | import topology.separation topology.subset_properties tactic.push_neg data.real.basic
import analysis.complex.exponential
open function set finset
local attribute [instance] classical.dec
noncomputable theory
def set_nat_to_seq (s : set ℕ) (n : ℕ) : ℝ := if n ∈ s then (3⁻¹)^n else 0
--#print cauchy_seq
lemma is_cau_seq_set_nat_to_seq (s : set ℕ) : is_cau_seq abs
(λ n, (range n).sum (set_nat_to_seq s)) :=
is_cau_series_of_abv_le_cau 0 (λ m hm, show abs (set_nat_to_seq s m) ≤ ((3 : ℝ)⁻¹) ^ m,
from if h : m ∈ s then by simp [set_nat_to_seq, h,
abs_of_nonneg (pow_nonneg (inv_nonneg.2 (show (3 : ℝ) ≥ 0, by norm_num)) _)]
else by rw [set_nat_to_seq, if_neg h, abs_zero];
exact pow_nonneg (inv_nonneg.2 (by norm_num)) _)
(is_cau_geo_series (3⁻¹ : ℝ) (by rw [abs_of_nonneg]; norm_num))
def set_nat_to_cau_seq (s : set ℕ) : cau_seq ℝ abs := ⟨_, is_cau_seq_set_nat_to_seq s⟩
lemma lim_set_nat_to_seq_injective_mul_three_pow_eq (s : set ℕ) : ∀ (n : ℕ),
⌊cau_seq.lim (set_nat_to_cau_seq s) * 3^n⌋ = (range n).sum (λ m, if m ∈ s then 3^(n-m) else 0)
| 0 := by simp
| (n+1) := _
#print set.Inter
#print compact_elim_finite_subcover_image
variables {α : Type*} [topological_space α]
lemma cantor_intersection (c : ℕ → set α) (hc : ∀ n, compact (c n))
(hn : ∀ n, c n ≠ ∅) (hmono : ∀ m n, m ≤ n → c n ⊆ c m) : (⋂ n, c n) ≠ ∅ :=
λ h,
let U : Π n:ℕ, set α := λ n, c 0 \ c n in
-- have hU : ∀ n : ℕ, (⋃ (m : ℕ) (hm : n ≤ m), U m) = c 0 \ ⋂ (m : ℕ) (hm : n ≤ m), c m,
-- from λ n, by simp only [diff_Inter_left, U],
have ∀ x : α, ∃ n, x ∉ c n, by simpa [set.ext_iff, classical.not_forall] using h,
have (⋃ (n : ℕ) (h : n ∈ @univ ℕ), U n) = c 0, by simp [set.ext_iff, this],
have hc : compact (⋂ n, c n), from sorry,
have hU : ∀ n, n ∈ @univ ℕ → is_open (U n), from λ _ _, is_open_diff _ _,
let ⟨s, hs⟩ := compact_elim_finite_subcover_image hc
_ _ in
begin
end
local attribute [instance] classical.dec
--#print not_mem_closure
lemma exists_subset_not_mem_closure [t2_space α] (h : ∀ x : α, ¬ is_open ({x} : set α)) :
∀ x : α, ∀ U : set α, is_open U → U ≠ ∅ → ∃ V ⊆ U, x ∉ closure V ∧ V ≠ ∅ :=
λ x U hU hUn,
have hUxn : U \ {x} ≠ ∅, from mt diff_eq_empty.1 $
λ hUx, if hxU : x ∈ U
then h x $ by convert hU; exact set.subset.antisymm (by simpa) hUx
else hUn $ eq_empty_iff_forall_not_mem.2
(λ y hy, hxU $ by convert hy; simpa [eq_comm] using hUx hy),
let ⟨y, hy⟩ := set.exists_mem_of_ne_empty hUxn in
let ⟨W₁, W₂, hW₁o, hW₂o, hxW, hyW, hW⟩ := t2_space.t2 x y (by finish) in
⟨W₂ ∩ U, inter_subset_right _ _, begin
rw mem_closure_iff,
push_neg,
exact ⟨W₁, hW₁o, hxW, by rw [← inter_assoc, hW, empty_inter]⟩,
end, set.ne_empty_of_mem ⟨hyW, hy.1⟩⟩
def uncountable_space_aux [t2_space α] [nonempty α] (h : ∀ x : α, ¬ is_open ({x} : set α)) (f : ℕ → α) :
Π n : ℕ, {s : set α // is_closed s ∧ f n ∉ s ∧ s ≠ ∅}
| 0 := have h : _ := exists_subset_not_mem_closure h (f 0)
univ is_open_univ (by simp),
⟨closure (classical.some h), is_closed_closure, (classical.some_spec h).snd.1,
λ hV, (classical.some_spec h).snd.2 (eq_empty_of_subset_empty $
by convert subset_closure; exact hV.symm)⟩
| (n+1) := have h : _ := exists_subset_not_mem_closure h (f (n+1))
(-uncountable_space_aux n) (is_open_compl_iff.2
(uncountable_space_aux n).2.1)
(set.ne_empty_of_mem (uncountable_space_aux n).2.2.1),
⟨closure (classical.some h), is_closed_closure, (classical.some_spec h).snd.1,
λ hV, (classical.some_spec h).snd.2 (eq_empty_of_subset_empty $
by convert subset_closure; exact hV.symm)⟩
lemma uncountable [compact_space α] [t2_space α]
[n : nonempty α] (h : ∀ x : α, ¬ is_open ({x} : set α)) (f : ℕ → α) : ¬surjective f :=
let ⟨x⟩ := n in
λ h, let ⟨y, hy⟩ := _ in
|
037cb2a5ab1e65ea85800714c48bdb516c1fa19b | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/algebra/big_operators/intervals.lean | 8aede9d22b545ffef0ca090021167fe40beea4c3 | [
"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 | 5,792 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import algebra.big_operators.basic
import data.finset.intervals
/-!
# Results about big operators over intervals
We prove results about big operators over intervals (mostly the `ℕ`-valued `Ico m n`).
-/
universes u v w
open_locale big_operators nat
namespace finset
variables {α : Type u} {β : Type v} {γ : Type w} {s₂ s₁ s : finset α} {a : α} {g f : α → β}
[comm_monoid β]
lemma sum_Ico_add {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) (m n k : ℕ) :
(∑ l in Ico m n, f (k + l)) = (∑ l in Ico (m + k) (n + k), f l) :=
Ico.image_add m n k ▸ eq.symm $ sum_image $ λ x hx y hy h, nat.add_left_cancel h
@[to_additive]
lemma prod_Ico_add (f : ℕ → β) (m n k : ℕ) :
(∏ l in Ico m n, f (k + l)) = (∏ l in Ico (m + k) (n + k), f l) :=
@sum_Ico_add (additive β) _ f m n k
lemma sum_Ico_succ_top {δ : Type*} [add_comm_monoid δ] {a b : ℕ}
(hab : a ≤ b) (f : ℕ → δ) : (∑ k in Ico a (b + 1), f k) = (∑ k in Ico a b, f k) + f b :=
by rw [Ico.succ_top hab, sum_insert Ico.not_mem_top, add_comm]
@[to_additive]
lemma prod_Ico_succ_top {a b : ℕ} (hab : a ≤ b) (f : ℕ → β) :
(∏ k in Ico a (b + 1), f k) = (∏ k in Ico a b, f k) * f b :=
@sum_Ico_succ_top (additive β) _ _ _ hab _
lemma sum_eq_sum_Ico_succ_bot {δ : Type*} [add_comm_monoid δ] {a b : ℕ}
(hab : a < b) (f : ℕ → δ) : (∑ k in Ico a b, f k) = f a + (∑ k in Ico (a + 1) b, f k) :=
have ha : a ∉ Ico (a + 1) b, by simp,
by rw [← sum_insert ha, Ico.insert_succ_bot hab]
@[to_additive]
lemma prod_eq_prod_Ico_succ_bot {a b : ℕ} (hab : a < b) (f : ℕ → β) :
(∏ k in Ico a b, f k) = f a * (∏ k in Ico (a + 1) b, f k) :=
@sum_eq_sum_Ico_succ_bot (additive β) _ _ _ hab _
@[to_additive]
lemma prod_Ico_consecutive (f : ℕ → β) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) :
(∏ i in Ico m n, f i) * (∏ i in Ico n k, f i) = (∏ i in Ico m k, f i) :=
Ico.union_consecutive hmn hnk ▸ eq.symm $ prod_union $ Ico.disjoint_consecutive m n k
@[to_additive]
lemma prod_range_mul_prod_Ico (f : ℕ → β) {m n : ℕ} (h : m ≤ n) :
(∏ k in range m, f k) * (∏ k in Ico m n, f k) = (∏ k in range n, f k) :=
Ico.zero_bot m ▸ Ico.zero_bot n ▸ prod_Ico_consecutive f (nat.zero_le m) h
@[to_additive]
lemma prod_Ico_eq_mul_inv {δ : Type*} [comm_group δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) :
(∏ k in Ico m n, f k) = (∏ k in range n, f k) * (∏ k in range m, f k)⁻¹ :=
eq_mul_inv_iff_mul_eq.2 $ by rw [mul_comm]; exact prod_range_mul_prod_Ico f h
lemma sum_Ico_eq_sub {δ : Type*} [add_comm_group δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) :
(∑ k in Ico m n, f k) = (∑ k in range n, f k) - (∑ k in range m, f k) :=
sum_Ico_eq_add_neg f h
@[to_additive]
lemma prod_Ico_eq_prod_range (f : ℕ → β) (m n : ℕ) :
(∏ k in Ico m n, f k) = (∏ k in range (n - m), f (m + k)) :=
begin
by_cases h : m ≤ n,
{ rw [← Ico.zero_bot, prod_Ico_add, zero_add, nat.sub_add_cancel h] },
{ replace h : n ≤ m := le_of_not_ge h,
rw [Ico.eq_empty_of_le h, nat.sub_eq_zero_of_le h, range_zero, prod_empty, prod_empty] }
end
lemma prod_Ico_reflect (f : ℕ → β) (k : ℕ) {m n : ℕ} (h : m ≤ n + 1) :
∏ j in Ico k m, f (n - j) = ∏ j in Ico (n + 1 - m) (n + 1 - k), f j :=
begin
have : ∀ i < m, i ≤ n,
{ intros i hi,
exact (add_le_add_iff_right 1).1 (le_trans (nat.lt_iff_add_one_le.1 hi) h) },
cases lt_or_le k m with hkm hkm,
{ rw [← finset.Ico.image_const_sub (this _ hkm)],
refine (prod_image _).symm,
simp only [Ico.mem],
rintros i ⟨ki, im⟩ j ⟨kj, jm⟩ Hij,
rw [← nat.sub_sub_self (this _ im), Hij, nat.sub_sub_self (this _ jm)] },
{ simp [Ico.eq_empty_of_le, nat.sub_le_sub_left, hkm] }
end
lemma sum_Ico_reflect {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) (k : ℕ) {m n : ℕ}
(h : m ≤ n + 1) :
∑ j in Ico k m, f (n - j) = ∑ j in Ico (n + 1 - m) (n + 1 - k), f j :=
@prod_Ico_reflect (multiplicative δ) _ f k m n h
lemma prod_range_reflect (f : ℕ → β) (n : ℕ) :
∏ j in range n, f (n - 1 - j) = ∏ j in range n, f j :=
begin
cases n,
{ simp },
{ simp only [range_eq_Ico, nat.succ_sub_succ_eq_sub, nat.sub_zero],
rw [prod_Ico_reflect _ _ (le_refl _)],
simp }
end
lemma sum_range_reflect {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) (n : ℕ) :
∑ j in range n, f (n - 1 - j) = ∑ j in range n, f j :=
@prod_range_reflect (multiplicative δ) _ f n
@[simp] lemma prod_Ico_id_eq_factorial : ∀ n : ℕ, ∏ x in Ico 1 (n + 1), x = n!
| 0 := rfl
| (n+1) := by rw [prod_Ico_succ_top $ nat.succ_le_succ $ zero_le n,
nat.factorial_succ, prod_Ico_id_eq_factorial n, nat.succ_eq_add_one, mul_comm]
@[simp] lemma prod_range_add_one_eq_factorial : ∀ n : ℕ, ∏ x in range n, (x+1) = n!
| 0 := rfl
| (n+1) := by simp [finset.range_succ, prod_range_add_one_eq_factorial n]
section gauss_sum
/-- Gauss' summation formula -/
lemma sum_range_id_mul_two (n : ℕ) :
(∑ i in range n, i) * 2 = n * (n - 1) :=
calc (∑ i in range n, i) * 2 = (∑ i in range n, i) + (∑ i in range n, (n - 1 - i)) :
by rw [sum_range_reflect (λ i, i) n, mul_two]
... = ∑ i in range n, (i + (n - 1 - i)) : sum_add_distrib.symm
... = ∑ i in range n, (n - 1) : sum_congr rfl $ λ i hi, nat.add_sub_cancel' $
nat.le_pred_of_lt $ mem_range.1 hi
... = n * (n - 1) : by rw [sum_const, card_range, nat.nsmul_eq_mul]
/-- Gauss' summation formula -/
lemma sum_range_id (n : ℕ) : (∑ i in range n, i) = (n * (n - 1)) / 2 :=
by rw [← sum_range_id_mul_two n, nat.mul_div_cancel]; exact dec_trivial
end gauss_sum
end finset
|
cd2cc208ab7becdd7ac51c322ca3a7b932c54f41 | 11e28114d9553ecd984ac4819661ffce3068bafe | /src/examples/archemedian.lean | 8bddedb8396713d39253849936d884c7b910c21e | [
"MIT"
] | permissive | EdAyers/lean-subtask | 9a26eb81f0c8576effed4ca94342ae1281445c59 | 04ac5a6c3bc3bfd190af4d6dcce444ddc8914e4b | refs/heads/master | 1,586,516,665,621 | 1,558,701,948,000 | 1,558,701,948,000 | 160,983,035 | 4 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 624 | lean | /- Author: E.W.Ayers © 2019 -/
import ..equate
import algebra.archimedean
open robot
universe u
variables {R : Type u} [linear_ordered_ring R] [floor_ring R] {x : R}
attribute [equate] int.cast_neg
attribute [equate] floor_coe
attribute [equate] floor_add_int
attribute [equate] floor_sub_int
@[equate] lemma ciel_def : ⌈x⌉ = -⌊-x⌋ := rfl
-- attribute [equate] neg_neg
@[equate] lemma my_ciel_coe (z : ℤ) : z = ⌈(z:R)⌉ :=
by equate
@[equate] theorem my_ceil_add_int (x : R) (z : ℤ) : ⌈x + z⌉ = ⌈x⌉ + z :=
by equate
-- [FIXME] bug where it thinks `0 = x * 0` is a good idea. Should be forbidden. |
780207153d85861256bb1e143cfd769ce73360de | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/lake/Lake/Load/Main.lean | 4b84884f58263ddd58df32e609332be773ce2bac | [
"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 | 8,795 | lean | /-
Copyright (c) 2022 Mac Malone. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mac Malone, Gabriel Ebner
-/
import Lake.Util.EStateT
import Lake.Util.StoreInsts
import Lake.Config.Workspace
import Lake.Build.Topological
import Lake.Build.Module
import Lake.Build.Package
import Lake.Build.Library
import Lake.Load.Materialize
import Lake.Load.Package
import Lake.Load.Elab
open System Lean
/-! # Loading a Workspace
The main definitions for loading a workspace and resolving dependencies.
-/
namespace Lake
/-- Load the tagged `Dependency` definitions from a package configuration environment. -/
def loadDepsFromEnv (env : Environment) (opts : Options) : Except String (Array Dependency) := do
(packageDepAttr.ext.getState env).mapM (evalConstCheck env opts Dependency ``Dependency)
/--
Elaborate a dependency's configuration file into a `Package`.
The resulting package does not yet include any dependencies.
-/
def loadDepPackage (wsDir : FilePath) (dep : MaterializedDep)
(leanOpts : Options) (lakeOpts : NameMap String) : LogIO Package := do
let dir := wsDir / dep.relPkgDir
let configEnv ← elabConfigFile dir lakeOpts leanOpts (dir / defaultConfigFile)
let config ← IO.ofExcept <| PackageConfig.loadFromEnv configEnv leanOpts
return {
dir, config, configEnv, leanOpts
remoteUrl? := dep.remoteUrl?
gitTag? := dep.gitTag?
}
/--
Rebuild the workspace's Lake manifest and materialize missing dependencies.
Packages are updated to latest specific revision matching that in `require`
(e.g., if the `require` is `@master`, update to latest commit on master) or
removed if the `require` is removed. If `tuUpdate` is empty, update/remove all
root dependencies. Otherwise, only update the root dependencies specified.
-/
def buildUpdatedManifest (ws : Workspace) (toUpdate : NameSet := {}) : LogIO Manifest := do
let res ← StateT.run (s := mkNameMap MaterializedDep) <| EStateT.run' (mkNameMap Package) do
-- Use manifest versions of root packages that should not be updated
unless toUpdate.isEmpty do
for entry in (← Manifest.loadOrEmpty ws.manifestFile) do
unless entry.inherited || toUpdate.contains entry.name do
let dep ← entry.materialize ws.dir ws.relPkgsDir
modifyThe (NameMap MaterializedDep) (·.insert entry.name dep)
buildAcyclic (·.1.name) (ws.root, FilePath.mk ".") fun (pkg, relPkgDir) resolve => do
let inherited := pkg.name != ws.root.name
let deps ← IO.ofExcept <| loadDepsFromEnv pkg.configEnv pkg.leanOpts
-- Materialize this package's dependencies first
let deps ← deps.mapM fun dep => fetchOrCreate dep.name do
dep.materialize inherited ws.dir ws.relPkgsDir relPkgDir
-- Load dependency packages and materialize their locked dependencies
let deps ← deps.mapM fun dep => do
if let .some pkg := (← getThe (NameMap Package)).find? dep.name then
return (pkg, dep.relPkgDir)
else
-- Load the package
let depPkg ← loadDepPackage ws.dir dep pkg.leanOpts dep.opts
if depPkg.name ≠ dep.name then
logWarning s!"{pkg.name}: package '{depPkg.name}' was required as '{dep.name}'"
-- Materialize locked dependencies
for entry in (← Manifest.loadOrEmpty depPkg.manifestFile) do
unless (← getThe (NameMap MaterializedDep)).contains entry.name do
let entry := entry.setInherited.inDirectory dep.relPkgDir
let dep ← entry.materialize ws.dir ws.relPkgsDir
modifyThe (NameMap MaterializedDep) (·.insert entry.name dep)
modifyThe (NameMap Package) (·.insert dep.name depPkg)
return (depPkg, dep.relPkgDir)
-- Resolve dependencies's dependencies recursively
return {pkg with opaqueDeps := ← deps.mapM (.mk <$> resolve ·)}
match res with
| (.ok _, deps) =>
let manifest : Manifest := {packagesDir? := ws.relPkgsDir}
return deps.fold (fun m _ d => m.insert d.manifestEntry) manifest
| (.error cycle, _) =>
let cycle := cycle.map (s!" {·}")
error s!"dependency cycle detected:\n{"\n".intercalate cycle}"
/--
Load a `Workspace` for a Lake package by elaborating its configuration file.
Does not resolve dependencies.
-/
def loadWorkspaceRoot (config : LoadConfig) : LogIO Workspace := do
Lean.searchPathRef.set config.env.leanSearchPath
let configEnv ← elabConfigFile config.rootDir config.configOpts config.leanOpts config.configFile
let pkgConfig ← IO.ofExcept <| PackageConfig.loadFromEnv configEnv config.leanOpts
let repo := GitRepo.mk config.rootDir
let root := {
configEnv, leanOpts := config.leanOpts
dir := config.rootDir, config := pkgConfig
remoteUrl? := ← repo.getFilteredRemoteUrl?
gitTag? := ← repo.findTag?
}
return {
root, lakeEnv := config.env
moduleFacetConfigs := initModuleFacetConfigs
packageFacetConfigs := initPackageFacetConfigs
libraryFacetConfigs := initLibraryFacetConfigs
}
/--
Finalize the workspace's root and its transitive dependencies
and add them to the workspace.
-/
def Workspace.finalize (ws : Workspace) : LogIO Workspace := do
have : MonadStore Name Package (StateT Workspace LogIO) := {
fetch? := fun name => return (← get).findPackage? name
store := fun _ pkg => modify (·.addPackage pkg)
}
let (res, ws) ← EStateT.run ws do
buildTop (·.name) ws.root fun pkg load => do
let depPkgs ← pkg.deps.mapM load
set <| ← IO.ofExcept <| (← get).addFacetsFromEnv pkg.configEnv pkg.leanOpts
let pkg ← pkg.finalize depPkgs
return pkg
match res with
| Except.ok root =>
return {ws with root}
| Except.error cycle => do
let cycle := cycle.map (s!" {·}")
error <|
s!"oops! dependency load cycle detected (this likely indicates a bug in Lake):\n" ++
"\n".intercalate cycle
/--
Resolving a workspace's dependencies using a manifest,
downloading and/or updating them as necessary.
-/
def Workspace.materializeDeps (ws : Workspace) (manifest : Manifest) : LogIO Workspace := do
if !manifest.isEmpty && manifest.packagesDir? != some ws.relPkgsDir then
logWarning <|
"manifest out of date: packages directory changed, " ++
"use `lake update` to update"
let relPkgsDir := manifest.packagesDir?.getD ws.relPkgsDir
let res ← EStateT.run' (mkNameMap Package) do
buildAcyclic (·.name) ws.root fun pkg resolve => do
let topLevel := pkg.name = ws.root.name
let deps ← IO.ofExcept <| loadDepsFromEnv pkg.configEnv pkg.leanOpts
if topLevel then
for dep in deps do
let warnOutOfDate (what : String) :=
logWarning <|
s!"manifest out of date: {what} of dependency '{dep.name}' changed, " ++
"use `lake update` to update"
if let .some entry := manifest.find? dep.name then
match dep.src, entry with
| .git (url := url) (rev := rev) .., .git (url := url') (inputRev? := rev') .. =>
if url ≠ url' then warnOutOfDate "git url"
if rev ≠ rev' then warnOutOfDate "git revision"
| .path .., .path .. => pure ()
| _, _ => warnOutOfDate "source kind (git/path)"
let depPkgs ← deps.mapM fun dep => fetchOrCreate dep.name do
let .some entry := manifest.find? dep.name
| error <| s!"dependency '{dep.name}' of '{pkg.name}' not in manifest, " ++
"use `lake update` to update"
let result ← entry.materialize ws.dir relPkgsDir
loadDepPackage ws.dir result pkg.leanOpts dep.opts
return { pkg with opaqueDeps := ← depPkgs.mapM (.mk <$> resolve ·) }
match res with
| Except.ok root =>
({ws with root}).finalize
| Except.error cycle =>
let cycle := cycle.map (s!" {·}")
error s!"dependency cycle detected:\n{"\n".intercalate cycle}"
/--
Load a `Workspace` for a Lake package by
elaborating its configuration file and resolving its dependencies.
If `updateDeps` is true, updates the manifest before resolving dependencies.
-/
def loadWorkspace (config : LoadConfig) (updateDeps := false) : LogIO Workspace := do
let ws ← loadWorkspaceRoot config
let manifest ← do
if updateDeps then
let manifest ← buildUpdatedManifest ws
manifest.saveToFile ws.manifestFile
pure manifest
else
Manifest.loadOrEmpty ws.manifestFile
ws.materializeDeps manifest
/-- Updates the manifest for the loaded Lake workspace (see `buildUpdatedManifest`). -/
def updateManifest (config : LoadConfig) (toUpdate : NameSet := {}) : LogIO Unit := do
let ws ← loadWorkspaceRoot config
let manifest ← buildUpdatedManifest ws toUpdate
manifest.saveToFile ws.manifestFile
|
660db0aa73bc1c94ba27f1e75f9b437547a317e4 | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /tests/compiler/strictAndOr.lean | 441523d94915719c19add700472e1e3a7642529f | [
"Apache-2.0"
] | permissive | mhuisi/lean4 | 28d35a4febc2e251c7f05492e13f3b05d6f9b7af | dda44bc47f3e5d024508060dac2bcb59fd12e4c0 | refs/heads/master | 1,621,225,489,283 | 1,585,142,689,000 | 1,585,142,689,000 | 250,590,438 | 0 | 2 | Apache-2.0 | 1,602,443,220,000 | 1,585,327,814,000 | C | UTF-8 | Lean | false | false | 311 | lean | def main : IO Unit :=
IO.println (strictOr false false) *>
IO.println (strictOr false true) *>
IO.println (strictOr true false) *>
IO.println (strictOr true true) *>
IO.println (strictAnd false false) *>
IO.println (strictAnd false true) *>
IO.println (strictAnd true false) *>
IO.println (strictAnd true true)
|
21c87755d11623c6d2cd696db7a29a4b2d67452e | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/ring_theory/dedekind_domain.lean | d5eefb454de05d2f88ddbde3e5827f9f60023dd8 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,717 | lean | /-
Copyright (c) 2020 Kenji Nakagawa. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenji Nakagawa, Anne Baanen, Filippo A. E. Nuccio
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.ring_theory.discrete_valuation_ring
import Mathlib.ring_theory.fractional_ideal
import Mathlib.ring_theory.ideal.over
import Mathlib.PostPort
universes u_1 u_2 l u_3 u_4 u_5
namespace Mathlib
/-!
# Dedekind domains
This file defines the notion of a Dedekind domain (or Dedekind ring),
giving three equivalent definitions (TODO: and shows that they are equivalent).
## Main definitions
- `is_dedekind_domain` defines a Dedekind domain as a commutative ring that is not a field,
Noetherian, integrally closed in its field of fractions and has Krull dimension exactly one.
`is_dedekind_domain_iff` shows that this does not depend on the choice of field of fractions.
- `is_dedekind_domain_dvr` alternatively defines a Dedekind domain as an integral domain that
is not a field, Noetherian, and the localization at every nonzero prime ideal is a DVR.
- `is_dedekind_domain_inv` alternatively defines a Dedekind domain as an integral domain that
is not a field, and every nonzero fractional ideal is invertible.
- `is_dedekind_domain_inv_iff` shows that this does note depend on the choice of field of fractions.
## Implementation notes
The definitions that involve a field of fractions choose a canonical field of fractions,
but are independent of that choice. The `..._iff` lemmas express this independence.
## References
* [D. Marcus, *Number Fields*][marcus1977number]
* [J.W.S. Cassels, A. Frölich, *Algebraic Number Theory*][cassels1967algebraic]
## Tags
dedekind domain, dedekind ring
-/
/-- A ring `R` has Krull dimension at most one if all nonzero prime ideals are maximal. -/
def ring.dimension_le_one (R : Type u_1) [comm_ring R] :=
∀ (p : ideal R), p ≠ ⊥ → ideal.is_prime p → ideal.is_maximal p
namespace ring
theorem dimension_le_one.principal_ideal_ring (A : Type u_2) [integral_domain A] [is_principal_ideal_ring A] : dimension_le_one A :=
fun (p : ideal A) (nonzero : p ≠ ⊥) (prime : ideal.is_prime p) => is_prime.to_maximal_ideal nonzero
theorem dimension_le_one.integral_closure (R : Type u_1) (A : Type u_2) [comm_ring R] [integral_domain A] [nontrivial R] [algebra R A] (h : dimension_le_one R) : dimension_le_one ↥(integral_closure R A) := sorry
end ring
/--
A Dedekind domain is an integral domain that is Noetherian, integrally closed, and
has Krull dimension exactly one (`not_is_field` and `dimension_le_one`).
The integral closure condition is independent of the choice of field of fractions:
use `is_dedekind_domain_iff` to prove `is_dedekind_domain` for a given `fraction_map`.
This is the default implementation, but there are equivalent definitions,
`is_dedekind_domain_dvr` and `is_dedekind_domain_inv`.
TODO: Prove that these are actually equivalent definitions.
-/
class is_dedekind_domain (A : Type u_2) [integral_domain A]
where
not_is_field : ¬is_field A
is_noetherian_ring : is_noetherian_ring A
dimension_le_one : ring.dimension_le_one A
is_integrally_closed : integral_closure A (fraction_ring A) = ⊥
/-- An integral domain is a Dedekind domain iff and only if it is not a field, is
Noetherian, has dimension ≤ 1, and is integrally closed in a given fraction field.
In particular, this definition does not depend on the choice of this fraction field. -/
theorem is_dedekind_domain_iff (A : Type u_2) (K : Type u_3) [integral_domain A] [field K] (f : fraction_map A K) : is_dedekind_domain A ↔
¬is_field A ∧ is_noetherian_ring A ∧ ring.dimension_le_one A ∧ integral_closure A (localization_map.codomain f) = ⊥ := sorry
/--
A Dedekind domain is an integral domain that is not a field, is Noetherian, and the
localization at every nonzero prime is a discrete valuation ring.
This is equivalent to `is_dedekind_domain`.
TODO: prove the equivalence.
-/
structure is_dedekind_domain_dvr (A : Type u_2) [integral_domain A]
where
not_is_field : ¬is_field A
is_noetherian_ring : is_noetherian_ring A
is_dvr_at_nonzero_prime : ∀ (P : ideal A), P ≠ ⊥ → ideal.is_prime P → discrete_valuation_ring (localization.at_prime P)
protected instance ring.fractional_ideal.has_inv (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} : has_inv (ring.fractional_ideal g) :=
has_inv.mk fun (I : ring.fractional_ideal g) => 1 / I
theorem inv_eq (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} {I : ring.fractional_ideal g} : I⁻¹ = 1 / I :=
rfl
theorem inv_zero' (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} : 0⁻¹ = 0 :=
ring.fractional_ideal.div_zero
theorem inv_nonzero (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} {J : ring.fractional_ideal g} (h : J ≠ 0) : J⁻¹ = { val := ↑1 / ↑J, property := ring.fractional_ideal.fractional_div_of_nonzero h } :=
ring.fractional_ideal.div_nonzero h
theorem coe_inv_of_nonzero (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} {J : ring.fractional_ideal g} (h : J ≠ 0) : ↑(J⁻¹) = localization_map.coe_submodule g 1 / ↑J :=
eq.mpr (id (Eq._oldrec (Eq.refl (↑(J⁻¹) = localization_map.coe_submodule g 1 / ↑J)) (inv_nonzero K h)))
(Eq.refl ↑{ val := ↑1 / ↑J, property := ring.fractional_ideal.fractional_div_of_nonzero h })
/-- `I⁻¹` is the inverse of `I` if `I` has an inverse. -/
theorem right_inverse_eq (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} (I : ring.fractional_ideal g) (J : ring.fractional_ideal g) (h : I * J = 1) : J = (I⁻¹) :=
congr_arg units.inv (units.ext rfl)
theorem mul_inv_cancel_iff (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} {I : ring.fractional_ideal g} : I * (I⁻¹) = 1 ↔ ∃ (J : ring.fractional_ideal g), I * J = 1 := sorry
@[simp] theorem map_inv (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} {K' : Type u_5} [field K'] {g' : fraction_map R₁ K'} (I : ring.fractional_ideal g) (h : alg_equiv R₁ (localization_map.codomain g) (localization_map.codomain g')) : ring.fractional_ideal.map (↑h) (I⁻¹) = (ring.fractional_ideal.map (↑h) I⁻¹) := sorry
@[simp] theorem span_singleton_inv (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} (x : localization_map.codomain g) : ring.fractional_ideal.span_singleton x⁻¹ = ring.fractional_ideal.span_singleton (x⁻¹) :=
ring.fractional_ideal.one_div_span_singleton x
theorem mul_generator_self_inv (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} (I : ring.fractional_ideal g) [submodule.is_principal ↑I] (h : I ≠ 0) : I * ring.fractional_ideal.span_singleton (submodule.is_principal.generator ↑I⁻¹) = 1 := sorry
theorem invertible_of_principal (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} (I : ring.fractional_ideal g) [submodule.is_principal ↑I] (h : I ≠ 0) : I * (I⁻¹) = 1 :=
iff.mpr ring.fractional_ideal.mul_div_self_cancel_iff
(Exists.intro (ring.fractional_ideal.span_singleton (submodule.is_principal.generator ↑I⁻¹))
(mul_generator_self_inv K I h))
theorem invertible_iff_generator_nonzero (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} (I : ring.fractional_ideal g) [submodule.is_principal ↑I] : I * (I⁻¹) = 1 ↔ submodule.is_principal.generator ↑I ≠ 0 := sorry
theorem is_principal_inv (K : Type u_3) [field K] {R₁ : Type u_4} [integral_domain R₁] {g : fraction_map R₁ K} (I : ring.fractional_ideal g) [submodule.is_principal ↑I] (h : I ≠ 0) : submodule.is_principal (subtype.val (I⁻¹)) := sorry
/--
A Dedekind domain is an integral domain that is not a field such that every fractional ideal
has an inverse.
This is equivalent to `is_dedekind_domain`.
TODO: prove the equivalence.
-/
structure is_dedekind_domain_inv (A : Type u_2) [integral_domain A]
where
not_is_field : ¬is_field A
mul_inv_cancel : ∀ (I : ring.fractional_ideal (fraction_ring.of A)), I ≠ ⊥ → I * (1 / I) = 1
theorem is_dedekind_domain_inv_iff (A : Type u_2) (K : Type u_3) [integral_domain A] [field K] (f : fraction_map A K) : is_dedekind_domain_inv A ↔ ¬is_field A ∧ ∀ (I : ring.fractional_ideal f), I ≠ ⊥ → I * (I⁻¹) = 1 := sorry
|
efbe9b330424f71582fc267cc5113d55375eb756 | 78630e908e9624a892e24ebdd21260720d29cf55 | /src/logic_propositional/prop_10.lean | 53306a7ce6eac9599400ac13c93de28fa8eb603c | [
"CC0-1.0"
] | permissive | tomasz-lisowski/lean-logic-examples | 84e612466776be0a16c23a0439ff8ef6114ddbe1 | 2b2ccd467b49c3989bf6c92ec0358a8d6ee68c5d | refs/heads/master | 1,683,334,199,431 | 1,621,938,305,000 | 1,621,938,305,000 | 365,041,573 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 357 | lean | namespace prop_10
variables A B : Prop
theorem prop_10 : ¬ (A ∨ ¬ B) ∨ (¬ A ∧ ¬ B) → ¬ A :=
assume h1: ¬ (A ∨ ¬ B) ∨ (¬ A ∧ ¬ B),
assume h2: A,
show false, from or.elim h1
(assume h3: ¬(A ∨ ¬B),
h3 (or.inl h2))
(assume h4: ¬A ∧ ¬B,
have h5: ¬ A, from and.left h4,
h5 h2)
-- end namespace
end prop_10 |
a208f9fca0bf3c280fbfca8c801b03a8e8d4c07e | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /tests/lean/run/eq7.lean | a98f7999172f29a179ff556c61e1ffe2aee26321 | [
"Apache-2.0"
] | permissive | tjiaqi/lean | 4634d729795c164664d10d093f3545287c76628f | d0ce4cf62f4246b0600c07e074d86e51f2195e30 | refs/heads/master | 1,622,323,796,480 | 1,422,643,069,000 | 1,422,643,069,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 425 | lean | import data.vector
open nat vector
definition diag {A : Type} : Π {n}, vector (vector A n) n → vector A n,
diag nil := nil,
diag ((a :: va) :: vs) := a :: diag (map tail vs)
theorem diag_nil (A : Type) : diag (@nil (vector A 0)) = nil :=
rfl
theorem diag_succ {A : Type} {n : nat} (a : A) (va : vector A n) (vs : vector (vector A (succ n)) n) :
diag ((a :: va) :: vs) = a :: diag (map tail vs) :=
rfl
|
4e87aeab8a58f03db393eac542ef445fe31118a1 | 1789ef53372ad44b5ce5db2341556f91c8c31395 | /Mermin_Peres.lean | ddf6cab89fddde1ed622fc89216bc221518ed45a | [] | no_license | iceplant/Mermin_Peres | d0a0f4c9f27111b3fee4e0ab5119ef1eafc59dc2 | d7ea59b5767157420b0fae9dfc09f22ad2826564 | refs/heads/master | 1,609,583,206,767 | 1,582,220,678,000 | 1,582,220,678,000 | 239,577,114 | 1 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 9,523 | lean | -- import init.data.nat.basic
-- import init.algebra.ring
-- import init.algebra.norm_num
import tactic
import data.nat.parity
import data.finset
import data.fintype
import algebra.big_operators
import data.real.basic
set_option class.instance_max_depth 15000000
#eval 2
open_locale classical
--open_local matrix
--Alice only sees r and Bob only sees c. The strategy isn't (r,c) → (...) but two maps, r→(r1 r2 r3) and c → (c1 c2 c3)
--I'm using 0 and 1 instead of Green and Red as the two options to fill squares. This makes checking parity of strategies easier
--fin 3 (type = {1,2,3})
--eventually we will be interested in relative probabilities
------------METHOD 1: enumerate all the constraints as concisely as possible and reduce to show we get a contraditcion----------------------------------
--try namespace outside of def but enclosing it
def checkStrategyrc (r c : ℕ) (strategy : ((ℕ → ℕ × ℕ × ℕ) × (ℕ → ℕ × ℕ × ℕ))) : Prop :=
--functionalize this with lists.
let r1 := (strategy.1 r).1,
r2 := (strategy.1 r).2.1,
r3 := (strategy.1 r).2.2,
c1 := (strategy.2 c).1,
c2 := (strategy.2 c).2.1,
c3 := (strategy.2 c).2.2
in nat.even(r1 + r2 + r3) ∧ (¬ nat.even(c1 + c2 + c3)) ∧
((r = 1 ∧ c = 1 ∧ r1 = c1) ∨ (r = 1 ∧ c = 2 ∧ r2 = c1) ∨ (r = 1 ∧ c = 3 ∧ r3 = c1)
∨(r = 2 ∧ c = 1 ∧ r1 = c2) ∨ (r = 2 ∧ c = 2 ∧ r2 = c2) ∨ (r = 2 ∧ c = 3 ∧ r3 = c2)
∨(r = 3 ∧ c = 1 ∧ r1 = c3) ∨ (r = 3 ∧ c = 2 ∧ r2 = c3) ∨ (r = 3 ∧ c = 3 ∧ r3 = c3))
--checks all three conditions are met for the strategy
def checkStrategy (strategy : ((ℕ → ℕ × ℕ × ℕ) × (ℕ → ℕ × ℕ × ℕ))) : Prop :=
(checkStrategyrc 1 1 strategy) ∧ (checkStrategyrc 1 2 strategy) ∧ (checkStrategyrc 1 3 strategy) ∧
(checkStrategyrc 2 1 strategy) ∧ (checkStrategyrc 2 2 strategy) ∧ (checkStrategyrc 2 3 strategy) ∧
(checkStrategyrc 3 1 strategy) ∧ (checkStrategyrc 3 2 strategy) ∧ (checkStrategyrc 3 3 strategy)
--someone on Zulip said to try putting this not directly after the import statements. This seems to have helped
--given a strategy, we can't have it satisfy all the conditions
theorem noStrategy2 (strategy : ((ℕ → ℕ × ℕ × ℕ) × (ℕ → ℕ × ℕ × ℕ))) : ¬ (checkStrategy (strategy)) :=
begin
intro s,
rw checkStrategy at s,
repeat {rw checkStrategyrc at s},
revert s,
rw (show 1 = 2 ↔ false, by norm_num),
rw (show 2 = 1 ↔ false, by norm_num),
rw (show 3 = 1 ↔ false, by norm_num),
rw (show 1 = 3 ↔ false, by norm_num),
rw (show 3 = 2 ↔ false, by norm_num),
rw (show 2 = 3 ↔ false, by norm_num),
repeat {simp only [false_and, false_or, and_false, or_false]},
rw (show 1=1 ↔ true, by norm_num),
rw (show 2=2 ↔ true, by norm_num),
rw (show 3=3 ↔ true, by norm_num),
simp only [true_and, and_true, true_or, or_true],
intro s,
rcases s with ⟨⟨s1, t1, u1⟩, ⟨s2, t2, u2⟩, ⟨s3, t3, u3⟩,
⟨s4, t4, u4⟩, ⟨s5, t5, u5⟩, ⟨s6, t6, u6⟩,
⟨s7, t7, u7⟩, ⟨s8, t8, u8⟩, ⟨s9, t9, u9⟩⟩,
-- fails
rw u1 at *,
rw u2 at *,
rw u3 at *,
rw u4 at *,
rw u5 at *,
rw u6 at *,
rw u7 at *,
rw u8 at *,
rw u9 at *,
apply odd_add_even t1 (odd_add_odd t2 t3),
convert even_add_even s1 (even_add_even s4 s7) using 1,
ring,
--seems like norm_num, finish, or safe could be useful here
--every case here has a false equality and-ed with something else. How do I replace them with false and reduce?
--how do I tell it that we only care about the cases where r,c ∈ {1,2,3} and then do cases on those?
end
--------------General case: mxn where m and n are odd
def checkStrategyMN {m n : nat} (strategyA : fin m → fin n → nat) (strategyB: fin n → fin m → nat) : Prop :=
(∀ r : fin m, (∀ c : fin n, ((finset.univ.sum (strategyA r)).even) ∧ (¬ (finset.univ.sum (strategyB c)).even) ∧ ((strategyA r c) = (strategyB c r))))
-- (∀ r : fin m, (finset.univ.sum (strategyA r)).even) ∧ (∀ c : fin n, ¬ (finset.univ.sum (strategyB c)).even) ∧
-- (∀ r : m, ∀ c : n, ((strategyA r c) = (strategyB c r)))
theorem noStrategyMN {m n : nat} (strategyA : fin m → fin n → nat) (strategyB: fin n → fin m → nat) : ((¬ m.even) ∧ (¬ n.even)) → ¬ (checkStrategyMN strategyA strategyB) :=
begin
rw checkStrategyMN,
intro h,
push_neg,
--??????????????????????
end
theorem q : 1 = 1 ↔ true :=
begin
squeeze_simp,
end
----Method 2: show sampling---
#check [[1,2],[1,2,3,4],[1,2,3,4,0],[]]
def board {m n : nat} (strategyA : fin m → fin n → nat) (strategyB: fin n → fin m → nat) : fin m → fin n → nat := strategyA --woah man...these representations are THE SAME!!!! (function that returns a vector and a matrix)
def consistent {m n : nat} (strategyA : fin m → fin n → nat) (strategyB: fin n → fin m → nat) : Prop
:= ∀ (r : fin m) (c : fin n), (strategyA r c) = (strategyB c r)
def sampleA {m n : nat} (board : fin m → fin n → nat) : fin m → fin n → nat := board
def sampleB {m n : nat} (board : fin m → fin n → nat) : fin n → fin m → nat := λ (n : fin n) (m : fin m), board m n
#check sampleA
#check sampleB
lemma board_equiv_strategy {m n : nat} (strategyA : fin m → fin n → nat) (strategyB: fin n → fin m → nat) :
(consistent strategyA strategyB) → (strategyA = ((sampleA (board strategyA strategyB))) ∧ (strategyB = (sampleB (board strategyA strategyB))))
:=
begin
intro h,
split,
rw sampleA,
rw board,
rw board,
rw sampleB,
rw consistent at h,
ext i,
rw h, --Logically this should work and seems pretty simple. How do I tell lean to do this???????????
end
def each_row_sum_even {m n : nat} (board : fin m → fin n → nat) : Prop := ∀ (r : fin m), (finset.univ.sum (sampleA board r)).even
def each_col_sum_odd {m n : nat} (board : fin m → fin n → nat) : Prop := ∀ (c : fin n), ¬(finset.univ.sum (sampleB board c)).even
def even_strategy {m n : nat} (strategyA : fin m → fin n → nat) : Prop := (∀ r : fin m, ((finset.univ.sum (strategyA r)).even))
def odd_strategy {m n : nat} (strategyB : fin n → fin m → nat) : Prop := (∀ (c : fin n), ¬ (finset.univ.sum (strategyB c)).even)
lemma even_strategy_implies_even_rows {m n : nat} (strategyA : fin m → fin n → nat) (strategyB : fin n → fin m → nat)
: (consistent strategyA strategyB) → (even_strategy strategyA) → each_row_sum_even (board strategyA strategyB)
:=
begin
rw even_strategy,
rw each_row_sum_even,
rw sampleA,
rw board,
intros h1 h2,
exact h2,
end
--In order to talk about a board we need to assume the two strategies are consistent. Or else we need to define the board differently to allowfor this
lemma odd_strategy_implies_odd_cols {m n : nat} (strategyA : fin m → fin n → nat) (strategyB : fin n → fin m → nat)
: ((consistent strategyA strategyB) ∧ odd_strategy strategyB) → each_col_sum_odd (board strategyA strategyB)
:=
begin
intro h,
cases h with h1 h2,
have h3 := board_equiv_strategy strategyA strategyB h1,
cases h3 with h4 h5,
rw each_col_sum_odd,
rw ← h5,
rw odd_strategy at h2,
exact h2,
end
--switch to mathlib matrix implementation
--Posted on Zulip about this-------------------
--Does this actually sum rows???????? We don't need a column one because we have our sampling function sampleB which is like a transpose
def matrix_sum {m n : ℕ} (M : fin m → fin n → ℕ ) : ℕ :=
finset.univ.sum $ λ i, finset.univ.sum $ M i
def row_sum {m n : nat} (board : fin m → fin n → nat) : nat := matrix_sum (sampleA board)
def col_sum {m n : nat} (board : fin m → fin n → nat) : nat := matrix_sum (sampleB board)
--how does this use sorry?????
theorem sum_univ_fin_eq_sum_range (n : ℕ) (f : fin n → ℝ) :
finset.sum finset.univ f = (finset.range n).sum (λ i, if hi : i < n then f ⟨i, hi⟩ else 0) :=
begin
set F : ℕ → ℝ := λ i, if hi : i < n then f ⟨i, hi⟩ else 0 with hF,
have H : f = λ (i : fin n), F (i.val),
{ ext i,
rw hF,
show f i = dite (i.val < n) (λ (hi : i.val < n), f ⟨i.val, hi⟩) (λ (hi : ¬i.val < n), 0),
rw dif_pos i.is_lt,
cases i, refl,
},
rw H,
rw ←finset.sum_image,
{ congr',
-- ⊢ finset.image (λ (x : fin n), x.val) finset.univ = finset.range n
ext j, -- sigh
rw [finset.mem_range, finset.mem_image],
split,
rintro ⟨⟨j,hj⟩, _, rfl⟩,
exact hj,
intro hj,
use ⟨j, hj⟩,
split, apply finset.mem_univ, refl
},
intros _ _ _ _,
exact fin.eq_of_veq,
end
lemma row_sum_eq_col_sum {m n : nat} (board : fin m → fin n → nat) : row_sum board = col_sum board
:=
begin
rw row_sum,
rw col_sum,
rw matrix_sum,
rw matrix_sum,
rw sampleB,
rw sampleA,
have h := sum_univ_fin_eq_sum_range,
sorry,
-- finset.sum_comm,
end
theorem noStrategyMN2 {m n : nat} (strategyA : fin m → fin n → nat) (strategyB: fin n → fin m → nat) :
¬ ((consistent strategyA strategyB) ∧ (even_strategy strategyA) ∧ (odd_strategy strategyB))
:=
begin
intro h,
cases h with c y,
cases y with even odd,
have cEven := even,
have x := even_strategy_implies_even_rows strategyA strategyB c even, --why is this not ok???????
have y := odd_strategy_implies_odd_cols strategyA strategyB (c ∧ even),
end
------------------------tests-----------------
theorem myTheorem : ¬ (∀ x : nat, x = 2) :=
begin
intro h,
specialize h 0,
end
------Ideas----
--use mathlib matrix representation
|
67f1d74795daa647e129ae12bb04dc52b04e4ef0 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/category_theory/groupoid/basic.lean | 1cf1a3bb07e881bf5f1b7cc14077b96c2ed0b12b | [
"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 | 999 | lean | /-
Copyright (c) 2022 Rémi Bottinelli. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Rémi Bottinelli
-/
import category_theory.groupoid
import combinatorics.quiver.basic
/-!
This file defines a few basic properties of groupoids.
-/
namespace category_theory
namespace groupoid
variables (C : Type*) [groupoid C]
section thin
lemma is_thin_iff : quiver.is_thin C ↔ ∀ c : C, subsingleton (c ⟶ c) :=
begin
refine ⟨λ h c, h c c, λ h c d, subsingleton.intro $ λ f g, _⟩,
haveI := h d,
calc f = f ≫ (inv g ≫ g) : by simp only [inv_eq_inv, is_iso.inv_hom_id, category.comp_id]
... = f ≫ (inv f ≫ g) : by congr
... = g : by simp only [inv_eq_inv, is_iso.hom_inv_id_assoc],
end
end thin
section disconnected
/-- A subgroupoid is totally disconnected if it only has loops. -/
def is_totally_disconnected := ∀ (c d : C), (c ⟶ d) → c = d
end disconnected
end groupoid
end category_theory
|
06b5a2625d22cd9caab39cd7ea81eb34862946d6 | 8711b4976859218c98ea2129103249d693a4178b | /src/modal_logic/basic.lean | d42d5fded96d5104b201f18cbcf6875e90472abd | [] | no_license | kendfrey/modal_logic | e2433c122ff7012aa743739283ceaa3451e41e79 | c07b5524de478cb57d796d0617b28990c7c77770 | refs/heads/master | 1,679,147,393,521 | 1,615,255,686,000 | 1,615,255,686,000 | 345,164,286 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 81 | lean | import modal_logic.classes
import modal_logic.axioms
import modal_logic.universal |
b40652a3437f6e7e19a518c294dd41c388d2b17b | d8f6c47921aa2f4f02b5272d2ed9c99f396d3858 | /lean/src/bv/helper.lean | 34f0f9928a59338d90a8b1e65e3f7791c8384a5a | [] | no_license | hongyunnchen/jitterbug | cc94e01483cdb36f9007ab978f174e5df9a65fd2 | eb5d50ef17f78c430f9033ff18472972b3588aee | refs/heads/master | 1,670,909,198,044 | 1,599,154,934,000 | 1,599,154,934,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 684 | 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 [nat.pow_succ, mul_comm]
lemma pow2_pos (n : ℕ) : 2^n > 0 :=
pos_pow_of_pos _ two_pos
@[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
|
786eb9895f358f70e9b2cc893065469511effc6b | 57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d | /stage0/src/Lean/Elab/Exception.lean | 6ff84fc4da29c0464fa47b9f9dfd5743c2b3ef94 | [
"Apache-2.0"
] | permissive | collares/lean4 | 861a9269c4592bce49b71059e232ff0bfe4594cc | 52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee | refs/heads/master | 1,691,419,031,324 | 1,618,678,138,000 | 1,618,678,138,000 | 358,989,750 | 0 | 0 | Apache-2.0 | 1,618,696,333,000 | 1,618,696,333,000 | null | UTF-8 | Lean | false | false | 2,525 | 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.InternalExceptionId
import Lean.Meta.Basic
namespace Lean.Elab
builtin_initialize postponeExceptionId : InternalExceptionId ← registerInternalExceptionId `postpone
builtin_initialize unsupportedSyntaxExceptionId : InternalExceptionId ← registerInternalExceptionId `unsupportedSyntax
builtin_initialize abortCommandExceptionId : InternalExceptionId ← registerInternalExceptionId `abortCommandElab
builtin_initialize abortTermExceptionId : InternalExceptionId ← registerInternalExceptionId `abortTermElab
builtin_initialize autoBoundImplicitExceptionId : InternalExceptionId ← registerInternalExceptionId `autoBoundImplicit
def throwPostpone [MonadExceptOf Exception m] : m α :=
throw $ Exception.internal postponeExceptionId
def throwUnsupportedSyntax [MonadExceptOf Exception m] : m α :=
throw $ Exception.internal unsupportedSyntaxExceptionId
def throwIllFormedSyntax [Monad m] [MonadError m] : m α :=
throwError "ill-formed syntax"
def throwAutoBoundImplicitLocal [MonadExceptOf Exception m] (n : Name) : m α :=
throw $ Exception.internal autoBoundImplicitExceptionId <| KVMap.empty.insert `localId n
def isAutoBoundImplicitLocalException? (ex : Exception) : Option Name :=
match ex with
| Exception.internal id k =>
if id == autoBoundImplicitExceptionId then
some <| k.getName `localId `x
else
none
| _ => none
def throwAlreadyDeclaredUniverseLevel [Monad m] [MonadError m] (u : Name) : m α :=
throwError "a universe level named '{u}' has already been declared"
-- Throw exception to abort elaboration of the current command without producing any error message
def throwAbortCommand {α m} [MonadExcept Exception m] : m α :=
throw <| Exception.internal abortCommandExceptionId
-- Throw exception to abort elaboration of the current term without producing any error message
def throwAbortTerm {α m} [MonadExcept Exception m] : m α :=
throw <| Exception.internal abortTermExceptionId
def isAbortExceptionId (id : InternalExceptionId) : Bool :=
id == abortCommandExceptionId || id == abortTermExceptionId
def mkMessageCore (fileName : String) (fileMap : FileMap) (msgData : MessageData) (severity : MessageSeverity) (pos : String.Pos) : Message :=
let pos := fileMap.toPosition pos
{ fileName := fileName, pos := pos, data := msgData, severity := severity }
end Lean.Elab
|
03bd8c1fe6462f400e76ca9f3b461cb51ba0bc9a | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/data/real/ennreal.lean | 54c7a694c0c88f5202357c5e9e1f761901f8fa3d | [
"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 | 53,220 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl, Yury Kudryashov
-/
import data.real.nnreal
import data.set.intervals
/-!
# Extended non-negative reals
We define `ennreal := with_no ℝ≥0` to be the type of extended nonnegative real numbers, i.e., the
interval `[0, +∞]`. This type is used as the codomain of a `measure_theory.measure`, and of the
extended distance `edist` in a `emetric_space`. In this file we define some algebraic operations and
a linear order on `ennreal` and prove basic properties of these operations, order, and conversions
to/from `ℝ`, `ℝ≥0`, and `ℕ`.
## Main definitions
* `ennreal`: the extended nonnegative real numbers `[0, ∞]`; defined as `with_top ℝ≥0`; it is
equipped with the following structures:
- coercion from `ℝ≥0` defined in the natural way;
- the natural structure of a complete dense linear order: `↑p ≤ ↑q ↔ p ≤ q` and `∀ a, a ≤ ∞`;
- `a + b` is defined so that `↑p + ↑q = ↑(p + q)` for `(p q : ℝ≥0)` and `a + ∞ = ∞ + a = ∞`;
- `a * b` is defined so that `↑p * ↑q = ↑(p * q)` for `(p q : ℝ≥0)`, `0 * ∞ = ∞ * 0 = 0`, and `a *
∞ = ∞ * a = ∞` for `a ≠ 0`;
- `a - b` is defined as the minimal `d` such that `a ≤ d + b`; this way we have
`↑p - ↑q = ↑(p - q)`, `∞ - ↑p = ∞`, `↑p - ∞ = ∞ - ∞ = 0`; note that there is no negation, only
subtraction;
- `a⁻¹` is defined as `Inf {b | 1 ≤ a * b}`. This way we have `(↑p)⁻¹ = ↑(p⁻¹)` for
`p : ℝ≥0`, `p ≠ 0`, `0⁻¹ = ∞`, and `∞⁻¹ = 0`.
- `a / b` is defined as `a * b⁻¹`.
The addition and multiplication defined this way together with `0 = ↑0` and `1 = ↑1` turn
`ennreal` into a canonically ordered commutative semiring of characteristic zero.
* Coercions to/from other types:
- coercion `ℝ≥0 → ennreal` is defined as `has_coe`, so one can use `(p : ℝ≥0)` in a context that
expects `a : ennreal`, and Lean will apply `coe` automatically;
- `ennreal.to_nnreal` sends `↑p` to `p` and `∞` to `0`;
- `ennreal.to_real := coe ∘ ennreal.to_nnreal` sends `↑p`, `p : ℝ≥0` to `(↑p : ℝ)` and `∞` to `0`;
- `ennreal.of_real := coe ∘ nnreal.of_real` sends `x : ℝ` to `↑⟨max x 0, _⟩`
- `ennreal.ne_top_equiv_nnreal` is an equivalence between `{a : ennreal // a ≠ 0}` and `ℝ≥0`.
## Implementation notes
We define a `can_lift ennreal ℝ≥0` instance, so one of the ways to prove theorems about an `ennreal`
number `a` is to consider the cases `a = ∞` and `a ≠ ∞`, and use the tactic `lift a to ℝ≥0 using ha`
in the second case. This instance is even more useful if one already has `ha : a ≠ ∞` in the
context, or if we have `(f : α → ennreal) (hf : ∀ x, f x ≠ ⊤)`.
## Notations
* `ℝ≥0`: type of nonnegative real numbers `[0, ∞)`; defined in `data.real.nnreal`;
* `∞`: a localized notation in `ennreal` for `⊤ : ennreal`.
-/
noncomputable theory
open classical set
open_locale classical big_operators nnreal
variables {α : Type*} {β : Type*}
/-- The extended nonnegative real numbers. This is usually denoted [0, ∞],
and is relevant as the codomain of a measure. -/
@[derive canonically_ordered_comm_semiring, derive complete_linear_order, derive densely_ordered,
derive nontrivial]
def ennreal := with_top ℝ≥0
localized "notation `∞` := (⊤ : ennreal)" in ennreal
namespace ennreal
variables {a b c d : ennreal} {r p q : ℝ≥0}
instance : inhabited ennreal := ⟨0⟩
instance : has_coe ℝ≥0 ennreal := ⟨ option.some ⟩
instance : can_lift ennreal ℝ≥0 :=
{ coe := coe,
cond := λ r, r ≠ ∞,
prf := λ x hx, ⟨option.get $ option.ne_none_iff_is_some.1 hx, option.some_get _⟩ }
@[simp] lemma none_eq_top : (none : ennreal) = (⊤ : ennreal) := rfl
@[simp] lemma some_eq_coe (a : ℝ≥0) : (some a : ennreal) = (↑a : ennreal) := rfl
/-- `to_nnreal x` returns `x` if it is real, otherwise 0. -/
protected def to_nnreal : ennreal → ℝ≥0
| (some r) := r
| none := 0
/-- `to_real x` returns `x` if it is real, `0` otherwise. -/
protected def to_real (a : ennreal) : real := coe (a.to_nnreal)
/-- `of_real x` returns `x` if it is nonnegative, `0` otherwise. -/
protected def of_real (r : real) : ennreal := coe (nnreal.of_real r)
@[simp, norm_cast] lemma to_nnreal_coe : (r : ennreal).to_nnreal = r := rfl
@[simp] lemma coe_to_nnreal : ∀{a:ennreal}, a ≠ ∞ → ↑(a.to_nnreal) = a
| (some r) h := rfl
| none h := (h rfl).elim
@[simp] lemma of_real_to_real {a : ennreal} (h : a ≠ ∞) : ennreal.of_real (a.to_real) = a :=
by simp [ennreal.to_real, ennreal.of_real, h]
@[simp] lemma to_real_of_real {r : ℝ} (h : 0 ≤ r) : ennreal.to_real (ennreal.of_real r) = r :=
by simp [ennreal.to_real, ennreal.of_real, nnreal.coe_of_real _ h]
lemma to_real_of_real' {r : ℝ} : ennreal.to_real (ennreal.of_real r) = max r 0 := rfl
lemma coe_to_nnreal_le_self : ∀{a:ennreal}, ↑(a.to_nnreal) ≤ a
| (some r) := by rw [some_eq_coe, to_nnreal_coe]; exact le_refl _
| none := le_top
lemma coe_nnreal_eq (r : ℝ≥0) : (r : ennreal) = ennreal.of_real r :=
by { rw [ennreal.of_real, nnreal.of_real], cases r with r h, congr, dsimp, rw max_eq_left h }
lemma of_real_eq_coe_nnreal {x : ℝ} (h : 0 ≤ x) :
ennreal.of_real x = @coe ℝ≥0 ennreal _ (⟨x, h⟩ : ℝ≥0) :=
by { rw [coe_nnreal_eq], refl }
@[simp] lemma of_real_coe_nnreal : ennreal.of_real p = p := (coe_nnreal_eq p).symm
@[simp, norm_cast] lemma coe_zero : ↑(0 : ℝ≥0) = (0 : ennreal) := rfl
@[simp, norm_cast] lemma coe_one : ↑(1 : ℝ≥0) = (1 : ennreal) := rfl
@[simp] lemma to_real_nonneg {a : ennreal} : 0 ≤ a.to_real := by simp [ennreal.to_real]
@[simp] lemma top_to_nnreal : ∞.to_nnreal = 0 := rfl
@[simp] lemma top_to_real : ∞.to_real = 0 := rfl
@[simp] lemma one_to_real : (1 : ennreal).to_real = 1 := rfl
@[simp] lemma one_to_nnreal : (1 : ennreal).to_nnreal = 1 := rfl
@[simp] lemma coe_to_real (r : ℝ≥0) : (r : ennreal).to_real = r := rfl
@[simp] lemma zero_to_nnreal : (0 : ennreal).to_nnreal = 0 := rfl
@[simp] lemma zero_to_real : (0 : ennreal).to_real = 0 := rfl
@[simp] lemma of_real_zero : ennreal.of_real (0 : ℝ) = 0 :=
by simp [ennreal.of_real]; refl
@[simp] lemma of_real_one : ennreal.of_real (1 : ℝ) = (1 : ennreal) :=
by simp [ennreal.of_real]
lemma of_real_to_real_le {a : ennreal} : ennreal.of_real (a.to_real) ≤ a :=
if ha : a = ∞ then ha.symm ▸ le_top else le_of_eq (of_real_to_real ha)
lemma forall_ennreal {p : ennreal → Prop} : (∀a, p a) ↔ (∀r:ℝ≥0, p r) ∧ p ∞ :=
⟨assume h, ⟨assume r, h _, h _⟩,
assume ⟨h₁, h₂⟩ a, match a with some r := h₁ _ | none := h₂ end⟩
lemma forall_ne_top {p : ennreal → Prop} : (∀ a ≠ ∞, p a) ↔ ∀ r : ℝ≥0, p r :=
option.ball_ne_none
lemma exists_ne_top {p : ennreal → Prop} : (∃ a ≠ ∞, p a) ↔ ∃ r : ℝ≥0, p r :=
option.bex_ne_none
lemma to_nnreal_eq_zero_iff (x : ennreal) : x.to_nnreal = 0 ↔ x = 0 ∨ x = ⊤ :=
⟨begin
cases x,
{ simp [none_eq_top] },
{ have A : some (0:ℝ≥0) = (0:ennreal) := rfl,
simp [ennreal.to_nnreal, A] {contextual := tt} }
end,
by intro h; cases h; simp [h]⟩
lemma to_real_eq_zero_iff (x : ennreal) : x.to_real = 0 ↔ x = 0 ∨ x = ⊤ :=
by simp [ennreal.to_real, to_nnreal_eq_zero_iff]
@[simp] lemma coe_ne_top : (r : ennreal) ≠ ∞ := with_top.coe_ne_top
@[simp] lemma top_ne_coe : ∞ ≠ (r : ennreal) := with_top.top_ne_coe
@[simp] lemma of_real_ne_top {r : ℝ} : ennreal.of_real r ≠ ∞ := by simp [ennreal.of_real]
@[simp] lemma of_real_lt_top {r : ℝ} : ennreal.of_real r < ∞ := lt_top_iff_ne_top.2 of_real_ne_top
@[simp] lemma top_ne_of_real {r : ℝ} : ∞ ≠ ennreal.of_real r := by simp [ennreal.of_real]
@[simp] lemma zero_ne_top : 0 ≠ ∞ := coe_ne_top
@[simp] lemma top_ne_zero : ∞ ≠ 0 := top_ne_coe
@[simp] lemma one_ne_top : 1 ≠ ∞ := coe_ne_top
@[simp] lemma top_ne_one : ∞ ≠ 1 := top_ne_coe
@[simp, norm_cast] lemma coe_eq_coe : (↑r : ennreal) = ↑q ↔ r = q := with_top.coe_eq_coe
@[simp, norm_cast] lemma coe_le_coe : (↑r : ennreal) ≤ ↑q ↔ r ≤ q := with_top.coe_le_coe
@[simp, norm_cast] lemma coe_lt_coe : (↑r : ennreal) < ↑q ↔ r < q := with_top.coe_lt_coe
lemma coe_mono : monotone (coe : ℝ≥0 → ennreal) := λ _ _, coe_le_coe.2
@[simp, norm_cast] lemma coe_eq_zero : (↑r : ennreal) = 0 ↔ r = 0 := coe_eq_coe
@[simp, norm_cast] lemma zero_eq_coe : 0 = (↑r : ennreal) ↔ 0 = r := coe_eq_coe
@[simp, norm_cast] lemma coe_eq_one : (↑r : ennreal) = 1 ↔ r = 1 := coe_eq_coe
@[simp, norm_cast] lemma one_eq_coe : 1 = (↑r : ennreal) ↔ 1 = r := coe_eq_coe
@[simp, norm_cast] lemma coe_nonneg : 0 ≤ (↑r : ennreal) ↔ 0 ≤ r := coe_le_coe
@[simp, norm_cast] lemma coe_pos : 0 < (↑r : ennreal) ↔ 0 < r := coe_lt_coe
@[simp, norm_cast] lemma coe_add : ↑(r + p) = (r + p : ennreal) := with_top.coe_add
@[simp, norm_cast] lemma coe_mul : ↑(r * p) = (r * p : ennreal) := with_top.coe_mul
@[simp, norm_cast] lemma coe_bit0 : (↑(bit0 r) : ennreal) = bit0 r := coe_add
@[simp, norm_cast] lemma coe_bit1 : (↑(bit1 r) : ennreal) = bit1 r := by simp [bit1]
lemma coe_two : ((2:ℝ≥0) : ennreal) = 2 := by norm_cast
protected lemma zero_lt_one : 0 < (1 : ennreal) :=
canonically_ordered_semiring.zero_lt_one
@[simp] lemma one_lt_two : (1 : ennreal) < 2 :=
coe_one ▸ coe_two ▸ by exact_mod_cast (@one_lt_two ℕ _ _)
@[simp] lemma zero_lt_two : (0:ennreal) < 2 := lt_trans ennreal.zero_lt_one one_lt_two
lemma two_ne_zero : (2:ennreal) ≠ 0 := (ne_of_lt zero_lt_two).symm
lemma two_ne_top : (2:ennreal) ≠ ∞ := coe_two ▸ coe_ne_top
/-- The set of `ennreal` numbers that are not equal to `∞` is equivalent to `ℝ≥0`. -/
def ne_top_equiv_nnreal : {a | a ≠ ∞} ≃ ℝ≥0 :=
{ to_fun := λ x, ennreal.to_nnreal x,
inv_fun := λ x, ⟨x, coe_ne_top⟩,
left_inv := λ ⟨x, hx⟩, subtype.eq $ coe_to_nnreal hx,
right_inv := λ x, to_nnreal_coe }
lemma cinfi_ne_top [has_Inf α] (f : ennreal → α) : (⨅ x : {x // x ≠ ∞}, f x) = ⨅ x : ℝ≥0, f x :=
eq.symm $ infi_congr _ ne_top_equiv_nnreal.symm.surjective $ λ x, rfl
lemma infi_ne_top [complete_lattice α] (f : ennreal → α) : (⨅ x ≠ ∞, f x) = ⨅ x : ℝ≥0, f x :=
by rw [infi_subtype', cinfi_ne_top]
lemma csupr_ne_top [has_Sup α] (f : ennreal → α) : (⨆ x : {x // x ≠ ∞}, f x) = ⨆ x : ℝ≥0, f x :=
@cinfi_ne_top (order_dual α) _ _
lemma supr_ne_top [complete_lattice α] (f : ennreal → α) : (⨆ x ≠ ∞, f x) = ⨆ x : ℝ≥0, f x :=
@infi_ne_top (order_dual α) _ _
lemma infi_ennreal {α : Type*} [complete_lattice α] {f : ennreal → α} :
(⨅ n, f n) = (⨅ n : ℝ≥0, f n) ⊓ f ⊤ :=
le_antisymm
(le_inf (le_infi $ assume i, infi_le _ _) (infi_le _ _))
(le_infi $ forall_ennreal.2 ⟨assume r, inf_le_left_of_le $ infi_le _ _, inf_le_right⟩)
lemma supr_ennreal {α : Type*} [complete_lattice α] {f : ennreal → α} :
(⨆ n, f n) = (⨆ n : ℝ≥0, f n) ⊔ f ⊤ :=
@infi_ennreal (order_dual α) _ _
@[simp] lemma add_top : a + ∞ = ∞ := with_top.add_top
@[simp] lemma top_add : ∞ + a = ∞ := with_top.top_add
/-- Coercion `ℝ≥0 → ennreal` as a `ring_hom`. -/
def of_nnreal_hom : ℝ≥0 →+* ennreal :=
⟨coe, coe_one, λ _ _, coe_mul, coe_zero, λ _ _, coe_add⟩
@[simp] lemma coe_of_nnreal_hom : ⇑of_nnreal_hom = coe := rfl
@[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) :
((s.indicator f a : ℝ≥0) : ennreal) = s.indicator (λ x, f x) a :=
(of_nnreal_hom : ℝ≥0 →+ ennreal).map_indicator _ _ _
@[simp, norm_cast] lemma coe_pow (n : ℕ) : (↑(r^n) : ennreal) = r^n :=
of_nnreal_hom.map_pow r n
lemma add_eq_top : a + b = ∞ ↔ a = ∞ ∨ b = ∞ := with_top.add_eq_top _ _
lemma add_lt_top : a + b < ∞ ↔ a < ∞ ∧ b < ∞ := with_top.add_lt_top _ _
lemma to_nnreal_add {r₁ r₂ : ennreal} (h₁ : r₁ < ⊤) (h₂ : r₂ < ⊤) :
(r₁ + r₂).to_nnreal = r₁.to_nnreal + r₂.to_nnreal :=
begin
rw [← coe_eq_coe, coe_add, coe_to_nnreal, coe_to_nnreal, coe_to_nnreal];
apply @ne_top_of_lt ennreal _ _ ⊤,
exact h₂,
exact h₁,
exact add_lt_top.2 ⟨h₁, h₂⟩
end
/- rw has trouble with the generic lt_top_iff_ne_top and bot_lt_iff_ne_bot
(contrary to erw). This is solved with the next lemmas -/
protected lemma lt_top_iff_ne_top : a < ∞ ↔ a ≠ ∞ := lt_top_iff_ne_top
protected lemma bot_lt_iff_ne_bot : 0 < a ↔ a ≠ 0 := bot_lt_iff_ne_bot
lemma not_lt_top {x : ennreal} : ¬ x < ∞ ↔ x = ∞ := by rw [lt_top_iff_ne_top, not_not]
lemma add_ne_top : a + b ≠ ∞ ↔ a ≠ ∞ ∧ b ≠ ∞ :=
by simpa only [lt_top_iff_ne_top] using add_lt_top
lemma mul_top : a * ∞ = (if a = 0 then 0 else ∞) :=
begin split_ifs, { simp [h] }, { exact with_top.mul_top h } end
lemma top_mul : ∞ * a = (if a = 0 then 0 else ∞) :=
begin split_ifs, { simp [h] }, { exact with_top.top_mul h } end
@[simp] lemma top_mul_top : ∞ * ∞ = ∞ := with_top.top_mul_top
lemma top_pow {n:ℕ} (h : 0 < n) : ∞^n = ∞ :=
nat.le_induction (pow_one _) (λ m hm hm', by rw [pow_succ, hm', top_mul_top])
_ (nat.succ_le_of_lt h)
lemma mul_eq_top : a * b = ⊤ ↔ (a ≠ 0 ∧ b = ⊤) ∨ (a = ⊤ ∧ b ≠ 0) :=
with_top.mul_eq_top_iff
lemma mul_ne_top : a ≠ ∞ → b ≠ ∞ → a * b ≠ ∞ :=
by simp [(≠), mul_eq_top] {contextual := tt}
lemma mul_lt_top : a < ⊤ → b < ⊤ → a * b < ⊤ :=
by simpa only [ennreal.lt_top_iff_ne_top] using mul_ne_top
lemma ne_top_of_mul_ne_top_left (h : a * b ≠ ⊤) (hb : b ≠ 0) : a ≠ ⊤ :=
by { simp [mul_eq_top, hb, not_or_distrib] at h ⊢, exact h.2 }
lemma ne_top_of_mul_ne_top_right (h : a * b ≠ ⊤) (ha : a ≠ 0) : b ≠ ⊤ :=
ne_top_of_mul_ne_top_left (by rwa [mul_comm]) ha
lemma lt_top_of_mul_lt_top_left (h : a * b < ⊤) (hb : b ≠ 0) : a < ⊤ :=
by { rw [ennreal.lt_top_iff_ne_top] at h ⊢, exact ne_top_of_mul_ne_top_left h hb }
lemma lt_top_of_mul_lt_top_right (h : a * b < ⊤) (ha : a ≠ 0) : b < ⊤ :=
lt_top_of_mul_lt_top_left (by rwa [mul_comm]) ha
lemma mul_lt_top_iff {a b : ennreal} : a * b < ⊤ ↔ (a < ⊤ ∧ b < ⊤) ∨ a = 0 ∨ b = 0 :=
begin
split,
{ intro h, rw [← or_assoc, or_iff_not_imp_right, or_iff_not_imp_right], intros hb ha,
exact ⟨lt_top_of_mul_lt_top_left h hb, lt_top_of_mul_lt_top_right h ha⟩ },
{ rintro (⟨ha, hb⟩|rfl|rfl); [exact mul_lt_top ha hb, simp, simp] }
end
@[simp] lemma mul_pos : 0 < a * b ↔ 0 < a ∧ 0 < b :=
by simp only [zero_lt_iff_ne_zero, ne.def, mul_eq_zero, not_or_distrib]
lemma pow_eq_top : ∀ n:ℕ, a^n=∞ → a=∞
| 0 := by simp
| (n+1) := λ o, (mul_eq_top.1 o).elim (λ h, pow_eq_top n h.2) and.left
lemma pow_ne_top (h : a ≠ ∞) {n:ℕ} : a^n ≠ ∞ :=
mt (pow_eq_top n) h
lemma pow_lt_top : a < ∞ → ∀ n:ℕ, a^n < ∞ :=
by simpa only [lt_top_iff_ne_top] using pow_ne_top
@[simp, norm_cast] lemma coe_finset_sum {s : finset α} {f : α → ℝ≥0} :
↑(∑ a in s, f a) = (∑ a in s, f a : ennreal) :=
of_nnreal_hom.map_sum f s
@[simp, norm_cast] lemma coe_finset_prod {s : finset α} {f : α → ℝ≥0} :
↑(∏ a in s, f a) = ((∏ a in s, f a) : ennreal) :=
of_nnreal_hom.map_prod f s
section order
@[simp] lemma bot_eq_zero : (⊥ : ennreal) = 0 := rfl
@[simp] lemma coe_lt_top : coe r < ∞ := with_top.coe_lt_top r
@[simp] lemma not_top_le_coe : ¬ (⊤:ennreal) ≤ ↑r := with_top.not_top_le_coe r
lemma zero_lt_coe_iff : 0 < (↑p : ennreal) ↔ 0 < p := coe_lt_coe
@[simp, norm_cast] lemma one_le_coe_iff : (1:ennreal) ≤ ↑r ↔ 1 ≤ r := coe_le_coe
@[simp, norm_cast] lemma coe_le_one_iff : ↑r ≤ (1:ennreal) ↔ r ≤ 1 := coe_le_coe
@[simp, norm_cast] lemma coe_lt_one_iff : (↑p : ennreal) < 1 ↔ p < 1 := coe_lt_coe
@[simp, norm_cast] lemma one_lt_coe_iff : 1 < (↑p : ennreal) ↔ 1 < p := coe_lt_coe
@[simp, norm_cast] lemma coe_nat (n : nat) : ((n : ℝ≥0) : ennreal) = n := with_top.coe_nat n
@[simp] lemma nat_ne_top (n : nat) : (n : ennreal) ≠ ⊤ := with_top.nat_ne_top n
@[simp] lemma top_ne_nat (n : nat) : (⊤ : ennreal) ≠ n := with_top.top_ne_nat n
@[simp] lemma one_lt_top : 1 < ∞ := coe_lt_top
lemma le_coe_iff : a ≤ ↑r ↔ (∃p:ℝ≥0, a = p ∧ p ≤ r) := with_top.le_coe_iff r a
lemma coe_le_iff : ↑r ≤ a ↔ (∀p:ℝ≥0, a = p → r ≤ p) := with_top.coe_le_iff r a
lemma lt_iff_exists_coe : a < b ↔ (∃p:ℝ≥0, a = p ∧ ↑p < b) := with_top.lt_iff_exists_coe a b
lemma pow_le_pow {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m :=
begin
cases a,
{ cases m,
{ rw eq_bot_iff.mpr h,
exact le_refl _ },
{ rw [none_eq_top, top_pow (nat.succ_pos m)],
exact le_top } },
{ rw [some_eq_coe, ← coe_pow, ← coe_pow, coe_le_coe],
exact pow_le_pow (by simpa using ha) h }
end
@[simp] lemma max_eq_zero_iff : max a b = 0 ↔ a = 0 ∧ b = 0 :=
by simp only [le_zero_iff_eq.symm, max_le_iff]
@[simp] lemma max_zero_left : max 0 a = a := max_eq_right (zero_le a)
@[simp] lemma max_zero_right : max a 0 = a := max_eq_left (zero_le a)
-- TODO: why this is not a `rfl`? There is some hidden diamond here.
@[simp] lemma sup_eq_max : a ⊔ b = max a b :=
eq_of_forall_ge_iff $ λ c, sup_le_iff.trans max_le_iff.symm
protected lemma pow_pos : 0 < a → ∀ n : ℕ, 0 < a^n :=
canonically_ordered_semiring.pow_pos
protected lemma pow_ne_zero : a ≠ 0 → ∀ n : ℕ, a^n ≠ 0 :=
by simpa only [zero_lt_iff_ne_zero] using ennreal.pow_pos
@[simp] lemma not_lt_zero : ¬ a < 0 := by simp
lemma add_lt_add_iff_left : a < ⊤ → (a + c < a + b ↔ c < b) :=
with_top.add_lt_add_iff_left
lemma add_lt_add_iff_right : a < ⊤ → (c + a < b + a ↔ c < b) :=
with_top.add_lt_add_iff_right
lemma lt_add_right (ha : a < ⊤) (hb : 0 < b) : a < a + b :=
by rwa [← add_lt_add_iff_left ha, add_zero] at hb
lemma le_of_forall_epsilon_le : ∀{a b : ennreal}, (∀ε:ℝ≥0, 0 < ε → b < ∞ → a ≤ b + ε) → a ≤ b
| a none h := le_top
| none (some a) h :=
have (⊤:ennreal) ≤ ↑a + ↑(1:ℝ≥0), from h 1 zero_lt_one coe_lt_top,
by rw [← coe_add] at this; exact (not_top_le_coe this).elim
| (some a) (some b) h :=
by simp only [none_eq_top, some_eq_coe, coe_add.symm, coe_le_coe, coe_lt_top, true_implies_iff]
at *; exact nnreal.le_of_forall_epsilon_le h
lemma lt_iff_exists_rat_btwn :
a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ (nnreal.of_real q:ennreal) < b) :=
⟨λ h,
begin
rcases lt_iff_exists_coe.1 h with ⟨p, rfl, _⟩,
rcases exists_between h with ⟨c, pc, cb⟩,
rcases lt_iff_exists_coe.1 cb with ⟨r, rfl, _⟩,
rcases (nnreal.lt_iff_exists_rat_btwn _ _).1 (coe_lt_coe.1 pc) with ⟨q, hq0, pq, qr⟩,
exact ⟨q, hq0, coe_lt_coe.2 pq, lt_trans (coe_lt_coe.2 qr) cb⟩
end,
λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩
lemma lt_iff_exists_real_btwn :
a < b ↔ (∃r:ℝ, 0 ≤ r ∧ a < ennreal.of_real r ∧ (ennreal.of_real r:ennreal) < b) :=
⟨λ h, let ⟨q, q0, aq, qb⟩ := ennreal.lt_iff_exists_rat_btwn.1 h in
⟨q, rat.cast_nonneg.2 q0, aq, qb⟩,
λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩
lemma lt_iff_exists_nnreal_btwn :
a < b ↔ (∃r:ℝ≥0, a < r ∧ (r : ennreal) < b) :=
with_top.lt_iff_exists_coe_btwn
lemma lt_iff_exists_add_pos_lt : a < b ↔ (∃ r : ℝ≥0, 0 < r ∧ a + r < b) :=
begin
refine ⟨λ hab, _, λ ⟨r, rpos, hr⟩, lt_of_le_of_lt (le_add_right (le_refl _)) hr⟩,
cases a, { simpa using hab },
rcases lt_iff_exists_real_btwn.1 hab with ⟨c, c_nonneg, ac, cb⟩,
let d : ℝ≥0 := ⟨c, c_nonneg⟩,
have ad : a < d,
{ rw of_real_eq_coe_nnreal c_nonneg at ac,
exact coe_lt_coe.1 ac },
refine ⟨d-a, nnreal.sub_pos.2 ad, _⟩,
rw [some_eq_coe, ← coe_add],
convert cb,
have : nnreal.of_real c = d,
by { rw [← nnreal.coe_eq, nnreal.coe_of_real _ c_nonneg], refl },
rw [add_comm, this],
exact nnreal.sub_add_cancel_of_le (le_of_lt ad)
end
lemma coe_nat_lt_coe {n : ℕ} : (n : ennreal) < r ↔ ↑n < r := ennreal.coe_nat n ▸ coe_lt_coe
lemma coe_lt_coe_nat {n : ℕ} : (r : ennreal) < n ↔ r < n := ennreal.coe_nat n ▸ coe_lt_coe
@[norm_cast] lemma coe_nat_lt_coe_nat {m n : ℕ} : (m : ennreal) < n ↔ m < n :=
ennreal.coe_nat n ▸ coe_nat_lt_coe.trans nat.cast_lt
lemma coe_nat_ne_top {n : ℕ} : (n : ennreal) ≠ ∞ := ennreal.coe_nat n ▸ coe_ne_top
lemma coe_nat_mono : strict_mono (coe : ℕ → ennreal) := λ _ _, coe_nat_lt_coe_nat.2
@[norm_cast] lemma coe_nat_le_coe_nat {m n : ℕ} : (m : ennreal) ≤ n ↔ m ≤ n :=
coe_nat_mono.le_iff_le
instance : char_zero ennreal := ⟨coe_nat_mono.injective⟩
protected lemma exists_nat_gt {r : ennreal} (h : r ≠ ⊤) : ∃n:ℕ, r < n :=
begin
lift r to ℝ≥0 using h,
rcases exists_nat_gt r with ⟨n, hn⟩,
exact ⟨n, coe_lt_coe_nat.2 hn⟩,
end
lemma add_lt_add (ac : a < c) (bd : b < d) : a + b < c + d :=
begin
rcases exists_between ac with ⟨a', aa', a'c⟩,
rcases lt_iff_exists_coe.1 aa' with ⟨aR, rfl, _⟩,
rcases lt_iff_exists_coe.1 a'c with ⟨a'R, rfl, _⟩,
rcases exists_between bd with ⟨b', bb', b'd⟩,
rcases lt_iff_exists_coe.1 bb' with ⟨bR, rfl, _⟩,
rcases lt_iff_exists_coe.1 b'd with ⟨b'R, rfl, _⟩,
have I : ↑aR + ↑bR < ↑a'R + ↑b'R :=
begin
rw [← coe_add, ← coe_add, coe_lt_coe],
apply add_lt_add (coe_lt_coe.1 aa') (coe_lt_coe.1 bb')
end,
have J : ↑a'R + ↑b'R ≤ c + d := add_le_add (le_of_lt a'c) (le_of_lt b'd),
apply lt_of_lt_of_le I J
end
@[norm_cast] lemma coe_min : ((min r p:ℝ≥0):ennreal) = min r p :=
coe_mono.map_min
@[norm_cast] lemma coe_max : ((max r p:ℝ≥0):ennreal) = max r p :=
coe_mono.map_max
end order
section complete_lattice
lemma coe_Sup {s : set ℝ≥0} : bdd_above s → (↑(Sup s) : ennreal) = (⨆a∈s, ↑a) := with_top.coe_Sup
lemma coe_Inf {s : set ℝ≥0} : s.nonempty → (↑(Inf s) : ennreal) = (⨅a∈s, ↑a) := with_top.coe_Inf
@[simp] lemma top_mem_upper_bounds {s : set ennreal} : ∞ ∈ upper_bounds s :=
assume x hx, le_top
lemma coe_mem_upper_bounds {s : set ℝ≥0} :
↑r ∈ upper_bounds ((coe : ℝ≥0 → ennreal) '' s) ↔ r ∈ upper_bounds s :=
by simp [upper_bounds, ball_image_iff, -mem_image, *] {contextual := tt}
end complete_lattice
section mul
lemma mul_le_mul : a ≤ b → c ≤ d → a * c ≤ b * d :=
canonically_ordered_semiring.mul_le_mul
lemma mul_left_mono : monotone ((*) a) := λ b c, mul_le_mul (le_refl a)
lemma mul_right_mono : monotone (λ x, x * a) := λ b c h, mul_le_mul h (le_refl a)
lemma max_mul : max a b * c = max (a * c) (b * c) :=
mul_right_mono.map_max
lemma mul_max : a * max b c = max (a * b) (a * c) :=
mul_left_mono.map_max
lemma mul_eq_mul_left : a ≠ 0 → a ≠ ⊤ → (a * b = a * c ↔ b = c) :=
begin
cases a; cases b; cases c;
simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm,
nnreal.mul_eq_mul_left] {contextual := tt},
end
lemma mul_eq_mul_right : c ≠ 0 → c ≠ ∞ → (a * c = b * c ↔ a = b) :=
mul_comm c a ▸ mul_comm c b ▸ mul_eq_mul_left
lemma mul_le_mul_left : a ≠ 0 → a ≠ ⊤ → (a * b ≤ a * c ↔ b ≤ c) :=
begin
cases a; cases b; cases c;
simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm] {contextual := tt},
assume h, exact mul_le_mul_left (zero_lt_iff_ne_zero.2 h)
end
lemma mul_le_mul_right : c ≠ 0 → c ≠ ∞ → (a * c ≤ b * c ↔ a ≤ b) :=
mul_comm c a ▸ mul_comm c b ▸ mul_le_mul_left
lemma mul_lt_mul_left : a ≠ 0 → a ≠ ⊤ → (a * b < a * c ↔ b < c) :=
λ h0 ht, by simp only [mul_le_mul_left h0 ht, lt_iff_le_not_le]
lemma mul_lt_mul_right : c ≠ 0 → c ≠ ∞ → (a * c < b * c ↔ a < b) :=
mul_comm c a ▸ mul_comm c b ▸ mul_lt_mul_left
end mul
section sub
instance : has_sub ennreal := ⟨λa b, Inf {d | a ≤ d + b}⟩
@[norm_cast] lemma coe_sub : ↑(p - r) = (↑p:ennreal) - r :=
le_antisymm
(le_Inf $ assume b (hb : ↑p ≤ b + r), coe_le_iff.2 $
by rintros d rfl; rwa [← coe_add, coe_le_coe, ← nnreal.sub_le_iff_le_add] at hb)
(Inf_le $ show (↑p : ennreal) ≤ ↑(p - r) + ↑r,
by rw [← coe_add, coe_le_coe, ← nnreal.sub_le_iff_le_add])
@[simp] lemma top_sub_coe : ∞ - ↑r = ∞ :=
top_unique $ le_Inf $ by simp [add_eq_top]
@[simp] lemma sub_eq_zero_of_le (h : a ≤ b) : a - b = 0 :=
le_antisymm (Inf_le $ le_add_left h) (zero_le _)
@[simp] lemma sub_self : a - a = 0 := sub_eq_zero_of_le $ le_refl _
@[simp] lemma zero_sub : 0 - a = 0 :=
le_antisymm (Inf_le $ zero_le $ 0 + a) (zero_le _)
@[simp] lemma sub_infty : a - ∞ = 0 :=
le_antisymm (Inf_le $ by simp) (zero_le _)
lemma sub_le_sub (h₁ : a ≤ b) (h₂ : d ≤ c) : a - c ≤ b - d :=
Inf_le_Inf $ assume e (h : b ≤ e + d),
calc a ≤ b : h₁
... ≤ e + d : h
... ≤ e + c : add_le_add (le_refl _) h₂
@[simp] lemma add_sub_self : ∀{a b : ennreal}, b < ∞ → (a + b) - b = a
| a none := by simp [none_eq_top]
| none (some b) := by simp [none_eq_top, some_eq_coe]
| (some a) (some b) :=
by simp [some_eq_coe]; rw [← coe_add, ← coe_sub, coe_eq_coe, nnreal.add_sub_cancel]
@[simp] lemma add_sub_self' (h : a < ∞) : (a + b) - a = b :=
by rw [add_comm, add_sub_self h]
lemma add_right_inj (h : a < ∞) : a + b = a + c ↔ b = c :=
⟨λ e, by simpa [h] using congr_arg (λ x, x - a) e, congr_arg _⟩
lemma add_left_inj (h : a < ∞) : b + a = c + a ↔ b = c :=
by rw [add_comm, add_comm c, add_right_inj h]
@[simp] lemma sub_add_cancel_of_le : ∀{a b : ennreal}, b ≤ a → (a - b) + b = a :=
begin
simp [forall_ennreal, le_coe_iff, -add_comm] {contextual := tt},
rintros r p x rfl h,
rw [← coe_sub, ← coe_add, nnreal.sub_add_cancel_of_le h]
end
@[simp] lemma add_sub_cancel_of_le (h : b ≤ a) : b + (a - b) = a :=
by rwa [add_comm, sub_add_cancel_of_le]
lemma sub_add_self_eq_max : (a - b) + b = max a b :=
match le_total a b with
| or.inl h := by simp [h, max_eq_right]
| or.inr h := by simp [h, max_eq_left]
end
lemma le_sub_add_self : a ≤ (a - b) + b :=
by { rw sub_add_self_eq_max, exact le_max_left a b }
@[simp] protected lemma sub_le_iff_le_add : a - b ≤ c ↔ a ≤ c + b :=
iff.intro
(assume h : a - b ≤ c,
calc a ≤ (a - b) + b : le_sub_add_self
... ≤ c + b : add_le_add_right h _)
(assume h : a ≤ c + b,
calc a - b ≤ (c + b) - b : sub_le_sub h (le_refl _)
... ≤ c : Inf_le (le_refl (c + b)))
protected lemma sub_le_iff_le_add' : a - b ≤ c ↔ a ≤ b + c :=
add_comm c b ▸ ennreal.sub_le_iff_le_add
lemma sub_eq_of_add_eq : b ≠ ∞ → a + b = c → c - b = a :=
λ hb hc, hc ▸ add_sub_self (lt_top_iff_ne_top.2 hb)
protected lemma sub_le_of_sub_le (h : a - b ≤ c) : a - c ≤ b :=
ennreal.sub_le_iff_le_add.2 $ by { rw add_comm, exact ennreal.sub_le_iff_le_add.1 h }
protected lemma sub_lt_sub_self : a ≠ ⊤ → a ≠ 0 → 0 < b → a - b < a :=
match a, b with
| none, _ := by { have := none_eq_top, assume h, contradiction }
| (some a), none := by {intros, simp only [none_eq_top, sub_infty, zero_lt_iff_ne_zero], assumption}
| (some a), (some b) :=
begin
simp only [some_eq_coe, coe_sub.symm, coe_pos, coe_eq_zero, coe_lt_coe, ne.def],
assume h₁ h₂, apply nnreal.sub_lt_self, exact zero_lt_iff_ne_zero.2 h₂
end
end
@[simp] lemma sub_eq_zero_iff_le : a - b = 0 ↔ a ≤ b :=
by simpa [-ennreal.sub_le_iff_le_add] using @ennreal.sub_le_iff_le_add a b 0
@[simp] lemma zero_lt_sub_iff_lt : 0 < a - b ↔ b < a :=
by simpa [ennreal.bot_lt_iff_ne_bot, -sub_eq_zero_iff_le]
using not_iff_not.2 (@sub_eq_zero_iff_le a b)
lemma lt_sub_iff_add_lt : a < b - c ↔ a + c < b :=
begin
cases a, { simp },
cases c, { simp },
cases b, { simp only [true_iff, coe_lt_top, some_eq_coe, top_sub_coe, none_eq_top, ← coe_add] },
simp only [some_eq_coe],
rw [← coe_add, ← coe_sub, coe_lt_coe, coe_lt_coe, nnreal.lt_sub_iff_add_lt],
end
lemma sub_le_self (a b : ennreal) : a - b ≤ a :=
ennreal.sub_le_iff_le_add.2 $ le_add_right (le_refl a)
@[simp] lemma sub_zero : a - 0 = a :=
eq.trans (add_zero (a - 0)).symm $ by simp
/-- A version of triangle inequality for difference as a "distance". -/
lemma sub_le_sub_add_sub : a - c ≤ a - b + (b - c) :=
ennreal.sub_le_iff_le_add.2 $
calc a ≤ a - b + b : le_sub_add_self
... ≤ a - b + ((b - c) + c) : add_le_add_left le_sub_add_self _
... = a - b + (b - c) + c : (add_assoc _ _ _).symm
lemma sub_sub_cancel (h : a < ∞) (h2 : b ≤ a) : a - (a - b) = b :=
by rw [← add_left_inj (lt_of_le_of_lt (sub_le_self _ _) h),
sub_add_cancel_of_le (sub_le_self _ _), add_sub_cancel_of_le h2]
lemma sub_right_inj {a b c : ennreal} (ha : a < ⊤) (hb : b ≤ a) (hc : c ≤ a) :
a - b = a - c ↔ b = c :=
iff.intro
begin
assume h, have : a - (a - b) = a - (a - c), rw h,
rw [sub_sub_cancel ha hb, sub_sub_cancel ha hc] at this, exact this
end
(λ h, by rw h)
lemma sub_mul (h : 0 < b → b < a → c ≠ ∞) : (a - b) * c = a * c - b * c :=
begin
cases le_or_lt a b with hab hab,
{ simp [hab, mul_right_mono hab] },
symmetry,
cases eq_or_lt_of_le (zero_le b) with hb hb,
{ subst b, simp },
apply sub_eq_of_add_eq,
{ exact mul_ne_top (ne_top_of_lt hab) (h hb hab) },
rw [← add_mul, sub_add_cancel_of_le (le_of_lt hab)]
end
lemma mul_sub (h : 0 < c → c < b → a ≠ ∞) :
a * (b - c) = a * b - a * c :=
by { simp only [mul_comm a], exact sub_mul h }
lemma sub_mul_ge : a * c - b * c ≤ (a - b) * c :=
begin
-- with `0 < b → b < a → c ≠ ∞` Lean names the first variable `a`
by_cases h : ∀ (hb : 0 < b), b < a → c ≠ ∞,
{ rw [sub_mul h],
exact le_refl _ },
{ push_neg at h,
rcases h with ⟨hb, hba, hc⟩,
subst c,
simp only [mul_top, if_neg (ne_of_gt hb), if_neg (ne_of_gt $ lt_trans hb hba), sub_self,
zero_le] }
end
end sub
section sum
open finset
/-- A sum of finite numbers is still finite -/
lemma sum_lt_top {s : finset α} {f : α → ennreal} :
(∀a∈s, f a < ⊤) → ∑ a in s, f a < ⊤ :=
with_top.sum_lt_top
/-- A sum of finite numbers is still finite -/
lemma sum_lt_top_iff {s : finset α} {f : α → ennreal} :
∑ a in s, f a < ⊤ ↔ (∀a∈s, f a < ⊤) :=
with_top.sum_lt_top_iff
/-- A sum of numbers is infinite iff one of them is infinite -/
lemma sum_eq_top_iff {s : finset α} {f : α → ennreal} :
(∑ x in s, f x) = ⊤ ↔ (∃a∈s, f a = ⊤) :=
with_top.sum_eq_top_iff
/-- seeing `ennreal` as `nnreal` does not change their sum, unless one of the `ennreal` is
infinity -/
lemma to_nnreal_sum {s : finset α} {f : α → ennreal} (hf : ∀a∈s, f a < ⊤) :
ennreal.to_nnreal (∑ a in s, f a) = ∑ a in s, ennreal.to_nnreal (f a) :=
begin
rw [← coe_eq_coe, coe_to_nnreal, coe_finset_sum, sum_congr],
{ refl },
{ intros x hx, rw coe_to_nnreal, rw ← ennreal.lt_top_iff_ne_top, exact hf x hx },
{ rw ← ennreal.lt_top_iff_ne_top, exact sum_lt_top hf }
end
/-- seeing `ennreal` as `real` does not change their sum, unless one of the `ennreal` is infinity -/
lemma to_real_sum {s : finset α} {f : α → ennreal} (hf : ∀a∈s, f a < ⊤) :
ennreal.to_real (∑ a in s, f a) = ∑ a in s, ennreal.to_real (f a) :=
by { rw [ennreal.to_real, to_nnreal_sum hf, nnreal.coe_sum], refl }
end sum
section interval
variables {x y z : ennreal} {ε ε₁ ε₂ : ennreal} {s : set ennreal}
protected lemma Ico_eq_Iio : (Ico 0 y) = (Iio y) :=
ext $ assume a, iff.intro
(assume ⟨_, hx⟩, hx)
(assume hx, ⟨zero_le _, hx⟩)
lemma mem_Iio_self_add : x ≠ ⊤ → 0 < ε → x ∈ Iio (x + ε) :=
assume xt ε0, lt_add_right (by rwa lt_top_iff_ne_top) ε0
lemma not_mem_Ioo_self_sub : x = 0 → x ∉ Ioo (x - ε) y :=
assume x0, by simp [x0]
lemma mem_Ioo_self_sub_add : x ≠ ⊤ → x ≠ 0 → 0 < ε₁ → 0 < ε₂ → x ∈ Ioo (x - ε₁) (x + ε₂) :=
assume xt x0 ε0 ε0',
⟨ennreal.sub_lt_sub_self xt x0 ε0, lt_add_right (by rwa [lt_top_iff_ne_top]) ε0'⟩
end interval
section bit
@[simp] lemma bit0_inj : bit0 a = bit0 b ↔ a = b :=
⟨λh, begin
rcases (lt_trichotomy a b) with h₁| h₂| h₃,
{ exact (absurd h (ne_of_lt (add_lt_add h₁ h₁))) },
{ exact h₂ },
{ exact (absurd h.symm (ne_of_lt (add_lt_add h₃ h₃))) }
end,
λh, congr_arg _ h⟩
@[simp] lemma bit0_eq_zero_iff : bit0 a = 0 ↔ a = 0 :=
by simpa only [bit0_zero] using @bit0_inj a 0
@[simp] lemma bit0_eq_top_iff : bit0 a = ∞ ↔ a = ∞ :=
by rw [bit0, add_eq_top, or_self]
@[simp] lemma bit1_inj : bit1 a = bit1 b ↔ a = b :=
⟨λh, begin
unfold bit1 at h,
rwa [add_left_inj, bit0_inj] at h,
simp [lt_top_iff_ne_top]
end,
λh, congr_arg _ h⟩
@[simp] lemma bit1_ne_zero : bit1 a ≠ 0 :=
by unfold bit1; simp
@[simp] lemma bit1_eq_one_iff : bit1 a = 1 ↔ a = 0 :=
by simpa only [bit1_zero] using @bit1_inj a 0
@[simp] lemma bit1_eq_top_iff : bit1 a = ∞ ↔ a = ∞ :=
by unfold bit1; rw add_eq_top; simp
end bit
section inv
instance : has_inv ennreal := ⟨λa, Inf {b | 1 ≤ a * b}⟩
instance : has_div ennreal := ⟨λa b, a * b⁻¹⟩
lemma div_def : a / b = a * b⁻¹ := rfl
lemma mul_div_assoc : (a * b) / c = a * (b / c) :=
mul_assoc _ _ _
@[simp] lemma inv_zero : (0 : ennreal)⁻¹ = ∞ :=
show Inf {b : ennreal | 1 ≤ 0 * b} = ∞, by simp; refl
@[simp] lemma inv_top : (∞ : ennreal)⁻¹ = 0 :=
bot_unique $ le_of_forall_le_of_dense $ λ a (h : a > 0), Inf_le $ by simp [*, ne_of_gt h, top_mul]
@[simp, norm_cast] lemma coe_inv (hr : r ≠ 0) : (↑r⁻¹ : ennreal) = (↑r)⁻¹ :=
le_antisymm
(le_Inf $ assume b (hb : 1 ≤ ↑r * b), coe_le_iff.2 $
by rintros b rfl; rwa [← coe_mul, ← coe_one, coe_le_coe, ← nnreal.inv_le hr] at hb)
(Inf_le $ by simp; rw [← coe_mul, nnreal.mul_inv_cancel hr]; exact le_refl 1)
lemma coe_inv_le : (↑r⁻¹ : ennreal) ≤ (↑r)⁻¹ :=
if hr : r = 0 then by simp only [hr, nnreal.inv_zero, inv_zero, coe_zero, zero_le]
else by simp only [coe_inv hr, le_refl]
@[norm_cast] lemma coe_inv_two : ((2⁻¹:ℝ≥0):ennreal) = 2⁻¹ :=
by rw [coe_inv (ne_of_gt _root_.zero_lt_two), coe_two]
@[simp, norm_cast] lemma coe_div (hr : r ≠ 0) : (↑(p / r) : ennreal) = p / r :=
show ↑(p * r⁻¹) = ↑p * (↑r)⁻¹, by rw [coe_mul, coe_inv hr]
@[simp] lemma inv_one : (1:ennreal)⁻¹ = 1 :=
by simpa only [coe_inv one_ne_zero, coe_one] using coe_eq_coe.2 nnreal.inv_one
@[simp] lemma div_one {a : ennreal} : a / 1 = a :=
by simp [ennreal.div_def]
protected lemma inv_pow {n : ℕ} : (a^n)⁻¹ = (a⁻¹)^n :=
begin
by_cases a = 0; cases a; cases n; simp [*, none_eq_top, some_eq_coe,
zero_pow, top_pow, nat.zero_lt_succ] at *,
rw [← coe_inv h, ← coe_pow, ← coe_inv, nnreal.inv_pow, coe_pow],
rw [← ne.def] at h,
rw [← zero_lt_iff_ne_zero] at *,
apply pow_pos h
end
@[simp] lemma inv_inv : (a⁻¹)⁻¹ = a :=
by by_cases a = 0; cases a; simp [*, none_eq_top, some_eq_coe,
-coe_inv, (coe_inv _).symm] at *
lemma inv_involutive : function.involutive (λ a:ennreal, a⁻¹) :=
λ a, ennreal.inv_inv
lemma inv_bijective : function.bijective (λ a:ennreal, a⁻¹) :=
ennreal.inv_involutive.bijective
@[simp] lemma inv_eq_inv : a⁻¹ = b⁻¹ ↔ a = b := inv_bijective.1.eq_iff
@[simp] lemma inv_eq_top : a⁻¹ = ∞ ↔ a = 0 :=
inv_zero ▸ inv_eq_inv
lemma inv_ne_top : a⁻¹ ≠ ∞ ↔ a ≠ 0 := by simp
@[simp] lemma inv_lt_top {x : ennreal} : x⁻¹ < ⊤ ↔ 0 < x :=
by { simp only [lt_top_iff_ne_top, inv_ne_top, zero_lt_iff_ne_zero] }
lemma div_lt_top {x y : ennreal} (h1 : x < ⊤) (h2 : 0 < y) : x / y < ⊤ :=
mul_lt_top h1 (inv_lt_top.mpr h2)
@[simp] lemma inv_eq_zero : a⁻¹ = 0 ↔ a = ∞ :=
inv_top ▸ inv_eq_inv
lemma inv_ne_zero : a⁻¹ ≠ 0 ↔ a ≠ ∞ := by simp
@[simp] lemma inv_pos : 0 < a⁻¹ ↔ a ≠ ∞ :=
zero_lt_iff_ne_zero.trans inv_ne_zero
@[simp] lemma inv_lt_inv : a⁻¹ < b⁻¹ ↔ b < a :=
begin
cases a; cases b; simp only [some_eq_coe, none_eq_top, inv_top],
{ simp only [lt_irrefl] },
{ exact inv_pos.trans lt_top_iff_ne_top.symm },
{ simp only [not_lt_zero, not_top_lt] },
{ cases eq_or_lt_of_le (zero_le a) with ha ha;
cases eq_or_lt_of_le (zero_le b) with hb hb,
{ subst a, subst b, simp },
{ subst a, simp },
{ subst b, simp [zero_lt_iff_ne_zero, lt_top_iff_ne_top, inv_ne_top] },
{ rw [← coe_inv (ne_of_gt ha), ← coe_inv (ne_of_gt hb), coe_lt_coe, coe_lt_coe],
simp only [nnreal.coe_lt_coe.symm] at *,
exact inv_lt_inv ha hb } }
end
lemma inv_lt_iff_inv_lt : a⁻¹ < b ↔ b⁻¹ < a :=
by simpa only [inv_inv] using @inv_lt_inv a b⁻¹
lemma lt_inv_iff_lt_inv : a < b⁻¹ ↔ b < a⁻¹ :=
by simpa only [inv_inv] using @inv_lt_inv a⁻¹ b
@[simp, priority 1100] -- higher than le_inv_iff_mul_le
lemma inv_le_inv : a⁻¹ ≤ b⁻¹ ↔ b ≤ a :=
by simp only [le_iff_lt_or_eq, inv_lt_inv, inv_eq_inv, eq_comm]
lemma inv_le_iff_inv_le : a⁻¹ ≤ b ↔ b⁻¹ ≤ a :=
by simpa only [inv_inv] using @inv_le_inv a b⁻¹
lemma le_inv_iff_le_inv : a ≤ b⁻¹ ↔ b ≤ a⁻¹ :=
by simpa only [inv_inv] using @inv_le_inv a⁻¹ b
@[simp] lemma inv_lt_one : a⁻¹ < 1 ↔ 1 < a :=
inv_lt_iff_inv_lt.trans $ by rw [inv_one]
@[simp] lemma div_top : a / ∞ = 0 := by simp only [div_def, inv_top, mul_zero]
@[simp] lemma top_div_coe : ∞ / p = ∞ := by simp [div_def, top_mul]
lemma top_div_of_ne_top (h : a ≠ ⊤) : ∞ / a = ∞ :=
by { lift a to ℝ≥0 using h, exact top_div_coe }
lemma top_div_of_lt_top (h : a < ⊤) : ∞ / a = ∞ :=
top_div_of_ne_top h.ne
lemma top_div : ∞ / a = if a = ∞ then 0 else ∞ :=
by by_cases a = ∞; simp [top_div_of_ne_top, *]
@[simp] lemma zero_div : 0 / a = 0 := zero_mul a⁻¹
lemma div_eq_top : a / b = ⊤ ↔ (a ≠ 0 ∧ b = 0) ∨ (a = ⊤ ∧ b ≠ ⊤) :=
by simp [ennreal.div_def, ennreal.mul_eq_top]
lemma le_div_iff_mul_le (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ⊤ ∨ c ≠ ⊤) :
a ≤ c / b ↔ a * b ≤ c :=
begin
cases b,
{ simp at ht,
split,
{ assume ha, simp at ha, simp [ha] },
{ contrapose,
assume ha,
simp at ha,
have : a * ⊤ = ⊤, by simp [ennreal.mul_eq_top, ha],
simp [this, ht] } },
by_cases hb : b ≠ 0,
{ have : (b : ennreal) ≠ 0, by simp [hb],
rw [← ennreal.mul_le_mul_left this coe_ne_top],
suffices : ↑b * a ≤ (↑b * ↑b⁻¹) * c ↔ a * ↑b ≤ c,
{ simpa [some_eq_coe, div_def, hb, mul_left_comm, mul_comm, mul_assoc] },
rw [← coe_mul, nnreal.mul_inv_cancel hb, coe_one, one_mul, mul_comm] },
{ simp at hb,
simp [hb] at h0,
have : c / 0 = ⊤, by simp [div_eq_top, h0],
simp [hb, this] }
end
lemma div_le_iff_le_mul (hb0 : b ≠ 0 ∨ c ≠ ⊤) (hbt : b ≠ ⊤ ∨ c ≠ 0) : a / b ≤ c ↔ a ≤ c * b :=
begin
suffices : a * b⁻¹ ≤ c ↔ a ≤ c / b⁻¹, by simpa [div_def],
apply (le_div_iff_mul_le _ _).symm,
simpa [inv_ne_zero] using hbt,
simpa [inv_ne_zero] using hb0
end
lemma div_le_of_le_mul (h : a ≤ b * c) : a / c ≤ b :=
begin
by_cases h0 : c = 0,
{ have : a = 0, by simpa [h0] using h, simp [*] },
by_cases hinf : c = ⊤, by simp [hinf],
exact (div_le_iff_le_mul (or.inl h0) (or.inl hinf)).2 h
end
protected lemma div_lt_iff (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ⊤ ∨ c ≠ ⊤) :
c / b < a ↔ c < a * b :=
lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le h0 ht
lemma mul_lt_of_lt_div (h : a < b / c) : a * c < b :=
by { contrapose! h, exact ennreal.div_le_of_le_mul h }
lemma inv_le_iff_le_mul : (b = ⊤ → a ≠ 0) → (a = ⊤ → b ≠ 0) → (a⁻¹ ≤ b ↔ 1 ≤ a * b) :=
begin
cases a; cases b; simp [none_eq_top, some_eq_coe, mul_top, top_mul] {contextual := tt},
by_cases a = 0; simp [*, -coe_mul, coe_mul.symm, -coe_inv, (coe_inv _).symm, nnreal.inv_le]
end
@[simp] lemma le_inv_iff_mul_le : a ≤ b⁻¹ ↔ a * b ≤ 1 :=
begin
cases b, { by_cases a = 0; simp [*, none_eq_top, mul_top] },
by_cases b = 0; simp [*, some_eq_coe, le_div_iff_mul_le],
suffices : a ≤ 1 / b ↔ a * b ≤ 1, { simpa [div_def, h] },
exact le_div_iff_mul_le (or.inl (mt coe_eq_coe.1 h)) (or.inl coe_ne_top)
end
lemma mul_inv_cancel (h0 : a ≠ 0) (ht : a ≠ ⊤) : a * a⁻¹ = 1 :=
begin
lift a to ℝ≥0 using ht,
norm_cast at h0,
norm_cast,
exact nnreal.mul_inv_cancel h0
end
lemma inv_mul_cancel (h0 : a ≠ 0) (ht : a ≠ ∞) : a⁻¹ * a = 1 :=
mul_comm a a⁻¹ ▸ mul_inv_cancel h0 ht
lemma mul_le_iff_le_inv {a b r : ennreal} (hr₀ : r ≠ 0) (hr₁ : r ≠ ⊤) : (r * a ≤ b ↔ a ≤ r⁻¹ * b) :=
by rw [← @ennreal.mul_le_mul_left _ a _ hr₀ hr₁, ← mul_assoc, mul_inv_cancel hr₀ hr₁, one_mul]
lemma le_of_forall_lt_one_mul_lt : ∀{x y : ennreal}, (∀a<1, a * x ≤ y) → x ≤ y :=
forall_ennreal.2 $ and.intro
(assume r, forall_ennreal.2 $ and.intro
(assume q h, coe_le_coe.2 $ nnreal.le_of_forall_lt_one_mul_lt $ assume a ha,
begin rw [← coe_le_coe, coe_mul], exact h _ (coe_lt_coe.2 ha) end)
(assume h, le_top))
(assume r hr,
have ((1 / 2 : ℝ≥0) : ennreal) * ⊤ ≤ r :=
hr _ (coe_lt_coe.2 ((@nnreal.coe_lt_coe (1/2) 1).1 one_half_lt_one)),
have ne : ((1 / 2 : ℝ≥0) : ennreal) ≠ 0,
begin
rw [(≠), coe_eq_zero],
refine zero_lt_iff_ne_zero.1 _,
show 0 < (1 / 2 : ℝ),
linarith,
end,
by rwa [mul_top, if_neg ne] at this)
lemma div_add_div_same {a b c : ennreal} : a / c + b / c = (a + b) / c :=
eq.symm $ right_distrib a b (c⁻¹)
lemma div_self (h0 : a ≠ 0) (hI : a ≠ ∞) : a / a = 1 :=
mul_inv_cancel h0 hI
lemma mul_div_cancel (h0 : a ≠ 0) (hI : a ≠ ∞) : (b / a) * a = b :=
by rw [div_def, mul_assoc, inv_mul_cancel h0 hI, mul_one]
lemma mul_div_cancel' (h0 : a ≠ 0) (hI : a ≠ ∞) : a * (b / a) = b :=
by rw [mul_comm, mul_div_cancel h0 hI]
lemma inv_two_add_inv_two : (2:ennreal)⁻¹ + 2⁻¹ = 1 :=
by rw [← two_mul, ← div_def, div_self two_ne_zero two_ne_top]
lemma add_halves (a : ennreal) : a / 2 + a / 2 = a :=
by rw [div_def, ← mul_add, inv_two_add_inv_two, mul_one]
@[simp] lemma div_zero_iff : a / b = 0 ↔ a = 0 ∨ b = ⊤ :=
by simp [div_def, mul_eq_zero]
@[simp] lemma div_pos_iff : 0 < a / b ↔ a ≠ 0 ∧ b ≠ ⊤ :=
by simp [zero_lt_iff_ne_zero, not_or_distrib]
lemma half_pos {a : ennreal} (h : 0 < a) : 0 < a / 2 :=
by simp [ne_of_gt h]
lemma one_half_lt_one : (2⁻¹:ennreal) < 1 := inv_lt_one.2 $ one_lt_two
lemma half_lt_self {a : ennreal} (hz : a ≠ 0) (ht : a ≠ ⊤) : a / 2 < a :=
begin
lift a to ℝ≥0 using ht,
have h : (2 : ennreal) = ((2 : ℝ≥0) : ennreal), from rfl,
have h' : (2 : ℝ≥0) ≠ 0, from _root_.two_ne_zero',
rw [h, ← coe_div h', coe_lt_coe], -- `norm_cast` fails to apply `coe_div`
norm_cast at hz,
exact nnreal.half_lt_self hz
end
lemma sub_half (h : a ≠ ∞) : a - a / 2 = a / 2 :=
begin
lift a to ℝ≥0 using h,
exact sub_eq_of_add_eq (mul_ne_top coe_ne_top $ by simp) (add_halves a)
end
lemma one_sub_inv_two : (1:ennreal) - 2⁻¹ = 2⁻¹ :=
by simpa only [div_def, one_mul] using sub_half one_ne_top
lemma exists_inv_nat_lt {a : ennreal} (h : a ≠ 0) :
∃n:ℕ, (n:ennreal)⁻¹ < a :=
@inv_inv a ▸ by simp only [inv_lt_inv, ennreal.exists_nat_gt (inv_ne_top.2 h)]
lemma exists_nat_mul_gt (ha : a ≠ 0) (hb : b ≠ ⊤) :
∃ n : ℕ, b < n * a :=
begin
have : b / a ≠ ⊤, from mul_ne_top hb (inv_ne_top.2 ha),
refine (ennreal.exists_nat_gt this).imp (λ n hn, _),
rwa [← ennreal.div_lt_iff (or.inl ha) (or.inr hb)]
end
end inv
section real
lemma to_real_add (ha : a ≠ ⊤) (hb : b ≠ ⊤) : (a+b).to_real = a.to_real + b.to_real :=
begin
lift a to ℝ≥0 using ha,
lift b to ℝ≥0 using hb,
refl
end
lemma to_real_add_le : (a+b).to_real ≤ a.to_real + b.to_real :=
if ha : a = ⊤ then by simp only [ha, top_add, top_to_real, zero_add, to_real_nonneg]
else if hb : b = ⊤ then by simp only [hb, add_top, top_to_real, add_zero, to_real_nonneg]
else le_of_eq (to_real_add ha hb)
lemma of_real_add {p q : ℝ} (hp : 0 ≤ p) (hq : 0 ≤ q) :
ennreal.of_real (p + q) = ennreal.of_real p + ennreal.of_real q :=
by rw [ennreal.of_real, ennreal.of_real, ennreal.of_real, ← coe_add,
coe_eq_coe, nnreal.of_real_add hp hq]
lemma of_real_add_le {p q : ℝ} : ennreal.of_real (p + q) ≤ ennreal.of_real p + ennreal.of_real q :=
coe_le_coe.2 nnreal.of_real_add_le
@[simp] lemma to_real_le_to_real (ha : a ≠ ⊤) (hb : b ≠ ⊤) : a.to_real ≤ b.to_real ↔ a ≤ b :=
begin
lift a to ℝ≥0 using ha,
lift b to ℝ≥0 using hb,
norm_cast
end
@[simp] lemma to_real_lt_to_real (ha : a ≠ ⊤) (hb : b ≠ ⊤) : a.to_real < b.to_real ↔ a < b :=
begin
lift a to ℝ≥0 using ha,
lift b to ℝ≥0 using hb,
norm_cast
end
lemma to_real_max (hr : a ≠ ⊤) (hp : b ≠ ⊤) :
ennreal.to_real (max a b) = max (ennreal.to_real a) (ennreal.to_real b) :=
(le_total a b).elim
(λ h, by simp only [h, (ennreal.to_real_le_to_real hr hp).2 h, max_eq_right])
(λ h, by simp only [h, (ennreal.to_real_le_to_real hp hr).2 h, max_eq_left])
lemma to_nnreal_pos_iff : 0 < a.to_nnreal ↔ (0 < a ∧ a ≠ ∞) :=
begin
cases a,
{ simp [none_eq_top] },
{ simp [some_eq_coe] }
end
lemma to_real_pos_iff : 0 < a.to_real ↔ (0 < a ∧ a ≠ ∞):=
(nnreal.coe_pos).trans to_nnreal_pos_iff
lemma of_real_le_of_real {p q : ℝ} (h : p ≤ q) : ennreal.of_real p ≤ ennreal.of_real q :=
by simp [ennreal.of_real, nnreal.of_real_le_of_real h]
lemma of_real_le_of_le_to_real {a : ℝ} {b : ennreal} (h : a ≤ ennreal.to_real b) :
ennreal.of_real a ≤ b :=
(of_real_le_of_real h).trans of_real_to_real_le
@[simp] lemma of_real_le_of_real_iff {p q : ℝ} (h : 0 ≤ q) :
ennreal.of_real p ≤ ennreal.of_real q ↔ p ≤ q :=
by rw [ennreal.of_real, ennreal.of_real, coe_le_coe, nnreal.of_real_le_of_real_iff h]
@[simp] lemma of_real_lt_of_real_iff {p q : ℝ} (h : 0 < q) :
ennreal.of_real p < ennreal.of_real q ↔ p < q :=
by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, nnreal.of_real_lt_of_real_iff h]
lemma of_real_lt_of_real_iff_of_nonneg {p q : ℝ} (hp : 0 ≤ p) :
ennreal.of_real p < ennreal.of_real q ↔ p < q :=
by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, nnreal.of_real_lt_of_real_iff_of_nonneg hp]
@[simp] lemma of_real_pos {p : ℝ} : 0 < ennreal.of_real p ↔ 0 < p :=
by simp [ennreal.of_real]
@[simp] lemma of_real_eq_zero {p : ℝ} : ennreal.of_real p = 0 ↔ p ≤ 0 :=
by simp [ennreal.of_real]
lemma of_real_le_iff_le_to_real {a : ℝ} {b : ennreal} (hb : b ≠ ⊤) :
ennreal.of_real a ≤ b ↔ a ≤ ennreal.to_real b :=
begin
lift b to ℝ≥0 using hb,
simpa [ennreal.of_real, ennreal.to_real] using nnreal.of_real_le_iff_le_coe
end
lemma of_real_lt_iff_lt_to_real {a : ℝ} {b : ennreal} (ha : 0 ≤ a) (hb : b ≠ ⊤) :
ennreal.of_real a < b ↔ a < ennreal.to_real b :=
begin
lift b to ℝ≥0 using hb,
simpa [ennreal.of_real, ennreal.to_real] using nnreal.of_real_lt_iff_lt_coe ha
end
lemma le_of_real_iff_to_real_le {a : ennreal} {b : ℝ} (ha : a ≠ ⊤) (hb : 0 ≤ b) :
a ≤ ennreal.of_real b ↔ ennreal.to_real a ≤ b :=
begin
lift a to ℝ≥0 using ha,
simpa [ennreal.of_real, ennreal.to_real] using nnreal.le_of_real_iff_coe_le hb
end
lemma to_real_le_of_le_of_real {a : ennreal} {b : ℝ} (hb : 0 ≤ b) (h : a ≤ ennreal.of_real b) :
ennreal.to_real a ≤ b :=
have ha : a ≠ ⊤, from ne_top_of_le_ne_top of_real_ne_top h,
(le_of_real_iff_to_real_le ha hb).1 h
lemma lt_of_real_iff_to_real_lt {a : ennreal} {b : ℝ} (ha : a ≠ ⊤) :
a < ennreal.of_real b ↔ ennreal.to_real a < b :=
begin
lift a to ℝ≥0 using ha,
simpa [ennreal.of_real, ennreal.to_real] using nnreal.lt_of_real_iff_coe_lt
end
lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) :
ennreal.of_real (p * q) = (ennreal.of_real p) * (ennreal.of_real q) :=
by { simp only [ennreal.of_real, coe_mul.symm, coe_eq_coe], exact nnreal.of_real_mul hp }
lemma to_real_of_real_mul (c : ℝ) (a : ennreal) (h : 0 ≤ c) :
ennreal.to_real ((ennreal.of_real c) * a) = c * ennreal.to_real a :=
begin
cases a,
{ simp only [none_eq_top, ennreal.to_real, top_to_nnreal, nnreal.coe_zero, mul_zero, mul_top],
by_cases h' : c ≤ 0,
{ rw [if_pos], { simp }, { convert of_real_zero, exact le_antisymm h' h } },
{ rw [if_neg], refl, rw [of_real_eq_zero], assumption } },
{ simp only [ennreal.to_real, ennreal.to_nnreal],
simp only [some_eq_coe, ennreal.of_real, coe_mul.symm, to_nnreal_coe, nnreal.coe_mul],
congr, apply nnreal.coe_of_real, exact h }
end
@[simp] lemma to_real_mul_top (a : ennreal) : ennreal.to_real (a * ⊤) = 0 :=
begin
by_cases h : a = 0,
{ rw [h, zero_mul, zero_to_real] },
{ rw [mul_top, if_neg h, top_to_real] }
end
@[simp] lemma to_real_top_mul (a : ennreal) : ennreal.to_real (⊤ * a) = 0 :=
by { rw mul_comm, exact to_real_mul_top _ }
lemma to_real_eq_to_real (ha : a < ⊤) (hb : b < ⊤) :
ennreal.to_real a = ennreal.to_real b ↔ a = b :=
begin
rw ennreal.lt_top_iff_ne_top at *,
split,
{ assume h, apply le_antisymm,
rw ← to_real_le_to_real ha hb, exact le_of_eq h,
rw ← to_real_le_to_real hb ha, exact le_of_eq h.symm },
{ assume h, rw h }
end
lemma to_real_mul_to_real :
(ennreal.to_real a) * (ennreal.to_real b) = ennreal.to_real (a * b) :=
begin
by_cases ha : a = ⊤,
{ rw ha, simp },
by_cases hb : b = ⊤,
{ rw hb, simp },
have ha : ennreal.of_real (ennreal.to_real a) = a := of_real_to_real ha,
have hb : ennreal.of_real (ennreal.to_real b) = b := of_real_to_real hb,
conv_rhs { rw [← ha, ← hb, ← of_real_mul to_real_nonneg] },
rw [to_real_of_real (mul_nonneg to_real_nonneg to_real_nonneg)]
end
end real
section infi
variables {ι : Sort*} {f g : ι → ennreal}
lemma infi_add : infi f + a = ⨅i, f i + a :=
le_antisymm
(le_infi $ assume i, add_le_add (infi_le _ _) $ le_refl _)
(ennreal.sub_le_iff_le_add.1 $ le_infi $ assume i, ennreal.sub_le_iff_le_add.2 $ infi_le _ _)
lemma supr_sub : (⨆i, f i) - a = (⨆i, f i - a) :=
le_antisymm
(ennreal.sub_le_iff_le_add.2 $ supr_le $ assume i, ennreal.sub_le_iff_le_add.1 $ le_supr _ i)
(supr_le $ assume i, ennreal.sub_le_sub (le_supr _ _) (le_refl a))
lemma sub_infi : a - (⨅i, f i) = (⨆i, a - f i) :=
begin
refine (eq_of_forall_ge_iff $ λ c, _),
rw [ennreal.sub_le_iff_le_add, add_comm, infi_add],
simp [ennreal.sub_le_iff_le_add, sub_eq_add_neg, add_comm],
end
lemma Inf_add {s : set ennreal} : Inf s + a = ⨅b∈s, b + a :=
by simp [Inf_eq_infi, infi_add]
lemma add_infi {a : ennreal} : a + infi f = ⨅b, a + f b :=
by rw [add_comm, infi_add]; simp [add_comm]
lemma infi_add_infi (h : ∀i j, ∃k, f k + g k ≤ f i + g j) : infi f + infi g = (⨅a, f a + g a) :=
suffices (⨅a, f a + g a) ≤ infi f + infi g,
from le_antisymm (le_infi $ assume a, add_le_add (infi_le _ _) (infi_le _ _)) this,
calc (⨅a, f a + g a) ≤ (⨅ a a', f a + g a') :
le_infi $ assume a, le_infi $ assume a',
let ⟨k, h⟩ := h a a' in infi_le_of_le k h
... ≤ infi f + infi g :
by simp [add_infi, infi_add, -add_comm, -le_infi_iff]; exact le_refl _
lemma infi_sum {f : ι → α → ennreal} {s : finset α} [nonempty ι]
(h : ∀(t : finset α) (i j : ι), ∃k, ∀a∈t, f k a ≤ f i a ∧ f k a ≤ f j a) :
(⨅i, ∑ a in s, f i a) = ∑ a in s, ⨅i, f i a :=
finset.induction_on s (by simp) $ assume a s ha ih,
have ∀ (i j : ι), ∃ (k : ι), f k a + ∑ b in s, f k b ≤ f i a + ∑ b in s, f j b,
from assume i j,
let ⟨k, hk⟩ := h (insert a s) i j in
⟨k, add_le_add (hk a (finset.mem_insert_self _ _)).left $ finset.sum_le_sum $
assume a ha, (hk _ $ finset.mem_insert_of_mem ha).right⟩,
by simp [ha, ih.symm, infi_add_infi this]
lemma infi_mul {ι} [nonempty ι] {f : ι → ennreal} {x : ennreal} (h : x ≠ ⊤) :
infi f * x = ⨅i, f i * x :=
begin
by_cases h2 : x = 0, simp only [h2, mul_zero, infi_const],
refine le_antisymm
(le_infi $ λ i, mul_right_mono $ infi_le _ _)
((div_le_iff_le_mul (or.inl h2) $ or.inl h).mp $ le_infi $
λ i, (div_le_iff_le_mul (or.inl h2) $ or.inl h).mpr $ infi_le _ _)
end
lemma mul_infi {ι} [nonempty ι] {f : ι → ennreal} {x : ennreal} (h : x ≠ ⊤) :
x * infi f = ⨅i, x * f i :=
by { rw [mul_comm, infi_mul h], simp only [mul_comm], assumption }
/-! `supr_mul`, `mul_supr` and variants are in `topology.instances.ennreal`. -/
end infi
section supr
lemma supr_coe_nat : (⨆n:ℕ, (n : ennreal)) = ⊤ :=
(supr_eq_top _).2 $ assume b hb, ennreal.exists_nat_gt (lt_top_iff_ne_top.1 hb)
end supr
/-- `le_of_add_le_add_left` is normally applicable to `ordered_cancel_add_comm_monoid`,
but it holds in `ennreal` with the additional assumption that `a < ∞`. -/
lemma le_of_add_le_add_left {a b c : ennreal} : a < ∞ →
a + b ≤ a + c → b ≤ c :=
by cases a; cases b; cases c; simp [← ennreal.coe_add, ennreal.coe_le_coe]
end ennreal
|
0ebcad25b30d87f61ccdc7e180553ffa6064eb5f | a4673261e60b025e2c8c825dfa4ab9108246c32e | /stage0/src/Lean/Compiler/IR/EmitC.lean | 57e6f80dd91fd45d0e2dd37ffe0f964f10e22bae | [
"Apache-2.0"
] | permissive | jcommelin/lean4 | c02dec0cc32c4bccab009285475f265f17d73228 | 2909313475588cc20ac0436e55548a4502050d0a | refs/heads/master | 1,674,129,550,893 | 1,606,415,348,000 | 1,606,415,348,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 25,084 | 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.Runtime
import Lean.Compiler.NameMangling
import Lean.Compiler.ExportAttr
import Lean.Compiler.InitAttr
import Lean.Compiler.IR.CompilerM
import Lean.Compiler.IR.EmitUtil
import Lean.Compiler.IR.NormIds
import Lean.Compiler.IR.SimpCase
import Lean.Compiler.IR.Boxing
namespace Lean.IR.EmitC
open ExplicitBoxing (requiresBoxedVersion mkBoxedName isBoxedName)
def leanMainFn := "_lean_main"
structure Context :=
(env : Environment)
(modName : Name)
(jpMap : JPParamsMap := {})
(mainFn : FunId := arbitrary)
(mainParams : Array Param := #[])
abbrev M := ReaderT Context (EStateM String String)
def getEnv : M Environment := Context.env <$> read
def getModName : M Name := Context.modName <$> read
def getDecl (n : Name) : M Decl := do
let env ← getEnv
match findEnvDecl env n with
| some d => pure d
| none => throw s!"unknown declaration '{n}'"
@[inline] def emit {α : Type} [ToString α] (a : α) : M Unit :=
modify fun out => out ++ toString a
@[inline] def emitLn {α : Type} [ToString α] (a : α) : M Unit := do
emit a; emit "\n"
def emitLns {α : Type} [ToString α] (as : List α) : M Unit :=
as.forM fun a => emitLn a
def argToCString (x : Arg) : String :=
match x with
| Arg.var x => toString x
| _ => "lean_box(0)"
def emitArg (x : Arg) : M Unit :=
emit (argToCString x)
def toCType : IRType → String
| IRType.float => "double"
| IRType.uint8 => "uint8_t"
| IRType.uint16 => "uint16_t"
| IRType.uint32 => "uint32_t"
| IRType.uint64 => "uint64_t"
| IRType.usize => "size_t"
| IRType.object => "lean_object*"
| IRType.tobject => "lean_object*"
| IRType.irrelevant => "lean_object*"
| IRType.struct _ _ => panic! "not implemented yet"
| IRType.union _ _ => panic! "not implemented yet"
def throwInvalidExportName {α : Type} (n : Name) : M α :=
throw s!"invalid export name '{n}'"
def toCName (n : Name) : M String := do
let env ← getEnv;
-- TODO: we should support simple export names only
match getExportNameFor env n with
| some (Name.str Name.anonymous s _) => pure s
| some _ => throwInvalidExportName n
| none => if n == `main then pure leanMainFn else pure n.mangle
def emitCName (n : Name) : M Unit :=
toCName n >>= emit
def toCInitName (n : Name) : M String := do
let env ← getEnv;
-- TODO: we should support simple export names only
match getExportNameFor env n with
| some (Name.str Name.anonymous s _) => pure $ "_init_" ++ s
| some _ => throwInvalidExportName n
| none => pure ("_init_" ++ n.mangle)
def emitCInitName (n : Name) : M Unit :=
toCInitName n >>= emit
def emitFnDeclAux (decl : Decl) (cppBaseName : String) (addExternForConsts : Bool) : M Unit := do
let ps := decl.params
let env ← getEnv
if ps.isEmpty && addExternForConsts then emit "extern "
emit (toCType decl.resultType ++ " " ++ cppBaseName)
unless ps.isEmpty do
emit "("
-- We omit irrelevant parameters for extern constants
let ps := if isExternC env decl.name then ps.filter (fun p => !p.ty.isIrrelevant) else ps
if ps.size > closureMaxArgs && isBoxedName decl.name then
emit "lean_object**"
else
ps.size.forM fun i => do
if i > 0 then emit ", "
emit (toCType ps[i].ty)
emit ")"
emitLn ";"
def emitFnDecl (decl : Decl) (addExternForConsts : Bool) : M Unit := do
let cppBaseName ← toCName decl.name
emitFnDeclAux decl cppBaseName addExternForConsts
def emitExternDeclAux (decl : Decl) (cNameStr : String) : M Unit := do
let cName := Name.mkSimple cNameStr
let env ← getEnv
let extC := isExternC env decl.name
emitFnDeclAux decl cNameStr (!extC)
def emitFnDecls : M Unit := do
let env ← getEnv
let decls := getDecls env
let modDecls : NameSet := decls.foldl (fun s d => s.insert d.name) {}
let usedDecls : NameSet := decls.foldl (fun s d => collectUsedDecls env d (s.insert d.name)) {}
let usedDecls := usedDecls.toList
usedDecls.forM fun n => do
let decl ← getDecl n;
match getExternNameFor env `c decl.name with
| some cName => emitExternDeclAux decl cName
| none => emitFnDecl decl (!modDecls.contains n)
def emitMainFn : M Unit := do
let d ← getDecl `main
match d with
| Decl.fdecl f xs t b => do
unless xs.size == 2 || xs.size == 1 do throw "invalid main function, incorrect arity when generating code"
let env ← getEnv
let usesLeanAPI := usesModuleFrom env `Lean
if usesLeanAPI then
emitLn "void lean_initialize();"
else
emitLn "void lean_initialize_runtime_module();";
emitLn "
#if defined(WIN32) || defined(_WIN32)
#include <windows.h>
#endif
int main(int argc, char ** argv) {
#if defined(WIN32) || defined(_WIN32)
SetErrorMode(SEM_FAILCRITICALERRORS);
#endif
lean_object* in; lean_object* res;";
if usesLeanAPI then
emitLn "lean_initialize();"
else
emitLn "lean_initialize_runtime_module();"
let modName ← getModName
emitLn ("res = " ++ mkModuleInitializationFunctionName modName ++ "(lean_io_mk_world());")
emitLns ["lean_io_mark_end_initialization();",
"if (lean_io_result_is_ok(res)) {",
"lean_dec_ref(res);",
"lean_init_task_manager();"];
if xs.size == 2 then
emitLns ["in = lean_box(0);",
"int i = argc;",
"while (i > 1) {",
" lean_object* n;",
" i--;",
" n = lean_alloc_ctor(1,2,0); lean_ctor_set(n, 0, lean_mk_string(argv[i])); lean_ctor_set(n, 1, in);",
" in = n;",
"}"]
emitLn ("res = " ++ leanMainFn ++ "(in, lean_io_mk_world());")
else
emitLn ("res = " ++ leanMainFn ++ "(lean_io_mk_world());")
emitLn "}"
emitLns ["if (lean_io_result_is_ok(res)) {",
" int ret = lean_unbox(lean_io_result_get_value(res));",
" lean_dec_ref(res);",
" return ret;",
"} else {",
" lean_io_result_show_error(res);",
" lean_dec_ref(res);",
" return 1;",
"}"]
emitLn "}"
| other => throw "function declaration expected"
def hasMainFn : M Bool := do
let env ← getEnv
let decls := getDecls env
pure $ decls.any (fun d => d.name == `main)
def emitMainFnIfNeeded : M Unit := do
if (← hasMainFn) then emitMainFn
def emitFileHeader : M Unit := do
let env ← getEnv
let modName ← getModName
emitLn "// Lean compiler output"
emitLn ("// Module: " ++ toString modName)
emit "// Imports:"
env.imports.forM fun m => emit (" " ++ toString m)
emitLn ""
emitLn "#include <lean/lean.h>"
emitLns [
"#if defined(__clang__)",
"#pragma clang diagnostic ignored \"-Wunused-parameter\"",
"#pragma clang diagnostic ignored \"-Wunused-label\"",
"#elif defined(__GNUC__) && !defined(__CLANG__)",
"#pragma GCC diagnostic ignored \"-Wunused-parameter\"",
"#pragma GCC diagnostic ignored \"-Wunused-label\"",
"#pragma GCC diagnostic ignored \"-Wunused-but-set-variable\"",
"#endif",
"#ifdef __cplusplus",
"extern \"C\" {",
"#endif"
]
def emitFileFooter : M Unit :=
emitLns [
"#ifdef __cplusplus",
"}",
"#endif"
]
def throwUnknownVar {α : Type} (x : VarId) : M α :=
throw s!"unknown variable '{x}'"
def getJPParams (j : JoinPointId) : M (Array Param) := do
let ctx ← read;
match ctx.jpMap.find? j with
| some ps => pure ps
| none => throw "unknown join point"
def declareVar (x : VarId) (t : IRType) : M Unit := do
emit (toCType t); emit " "; emit x; emit "; "
def declareParams (ps : Array Param) : M Unit :=
ps.forM fun p => declareVar p.x p.ty
partial def declareVars : FnBody → Bool → M Bool
| e@(FnBody.vdecl x t _ b), d => do
let ctx ← read
if isTailCallTo ctx.mainFn e then
pure d
else
declareVar x t; declareVars b true
| FnBody.jdecl j xs _ b, d => do declareParams xs; declareVars b (d || xs.size > 0)
| e, d => if e.isTerminal then pure d else declareVars e.body d
def emitTag (x : VarId) (xType : IRType) : M Unit := do
if xType.isObj then do
emit "lean_obj_tag("; emit x; emit ")"
else
emit x
def isIf (alts : Array Alt) : Option (Nat × FnBody × FnBody) :=
if alts.size != 2 then none
else match alts[0] with
| Alt.ctor c b => some (c.cidx, b, alts[1].body)
| _ => none
def emitInc (x : VarId) (n : Nat) (checkRef : Bool) : M Unit := do
emit $
if checkRef then (if n == 1 then "lean_inc" else "lean_inc_n")
else (if n == 1 then "lean_inc_ref" else "lean_inc_ref_n")
emit "("; emit x
if n != 1 then emit ", "; emit n
emitLn ");"
def emitDec (x : VarId) (n : Nat) (checkRef : Bool) : M Unit := do
emit (if checkRef then "lean_dec" else "lean_dec_ref");
emit "("; emit x;
if n != 1 then emit ", "; emit n
emitLn ");"
def emitDel (x : VarId) : M Unit := do
emit "lean_free_object("; emit x; emitLn ");"
def emitSetTag (x : VarId) (i : Nat) : M Unit := do
emit "lean_ctor_set_tag("; emit x; emit ", "; emit i; emitLn ");"
def emitSet (x : VarId) (i : Nat) (y : Arg) : M Unit := do
emit "lean_ctor_set("; emit x; emit ", "; emit i; emit ", "; emitArg y; emitLn ");"
def emitOffset (n : Nat) (offset : Nat) : M Unit := do
if n > 0 then
emit "sizeof(void*)*"; emit n;
if offset > 0 then emit " + "; emit offset
else
emit offset
def emitUSet (x : VarId) (n : Nat) (y : VarId) : M Unit := do
emit "lean_ctor_set_usize("; emit x; emit ", "; emit n; emit ", "; emit y; emitLn ");"
def emitSSet (x : VarId) (n : Nat) (offset : Nat) (y : VarId) (t : IRType) : M Unit := do
match t with
| IRType.float => emit "lean_ctor_set_float"
| IRType.uint8 => emit "lean_ctor_set_uint8"
| IRType.uint16 => emit "lean_ctor_set_uint16"
| IRType.uint32 => emit "lean_ctor_set_uint32"
| IRType.uint64 => emit "lean_ctor_set_uint64"
| _ => throw "invalid instruction";
emit "("; emit x; emit ", "; emitOffset n offset; emit ", "; emit y; emitLn ");"
def emitJmp (j : JoinPointId) (xs : Array Arg) : M Unit := do
let ps ← getJPParams j
unless xs.size == ps.size do throw "invalid goto"
xs.size.forM fun i => do
let p := ps[i]
let x := xs[i]
emit p.x; emit " = "; emitArg x; emitLn ";"
emit "goto "; emit j; emitLn ";"
def emitLhs (z : VarId) : M Unit := do
emit z; emit " = "
def emitArgs (ys : Array Arg) : M Unit :=
ys.size.forM fun i => do
if i > 0 then emit ", "
emitArg ys[i]
def emitCtorScalarSize (usize : Nat) (ssize : Nat) : M Unit := do
if usize == 0 then emit ssize
else if ssize == 0 then emit "sizeof(size_t)*"; emit usize
else emit "sizeof(size_t)*"; emit usize; emit " + "; emit ssize
def emitAllocCtor (c : CtorInfo) : M Unit := do
emit "lean_alloc_ctor("; emit c.cidx; emit ", "; emit c.size; emit ", ";
emitCtorScalarSize c.usize c.ssize; emitLn ");"
def emitCtorSetArgs (z : VarId) (ys : Array Arg) : M Unit :=
ys.size.forM fun i => do
emit "lean_ctor_set("; emit z; emit ", "; emit i; emit ", "; emitArg ys[i]; emitLn ");"
def emitCtor (z : VarId) (c : CtorInfo) (ys : Array Arg) : M Unit := do
emitLhs z;
if c.size == 0 && c.usize == 0 && c.ssize == 0 then do
emit "lean_box("; emit c.cidx; emitLn ");"
else do
emitAllocCtor c; emitCtorSetArgs z ys
def emitReset (z : VarId) (n : Nat) (x : VarId) : M Unit := do
emit "if (lean_is_exclusive("; emit x; emitLn ")) {";
n.forM fun i => do
emit " lean_ctor_release("; emit x; emit ", "; emit i; emitLn ");"
emit " "; emitLhs z; emit x; emitLn ";";
emitLn "} else {";
emit " lean_dec_ref("; emit x; emitLn ");";
emit " "; emitLhs z; emitLn "lean_box(0);";
emitLn "}"
def emitReuse (z : VarId) (x : VarId) (c : CtorInfo) (updtHeader : Bool) (ys : Array Arg) : M Unit := do
emit "if (lean_is_scalar("; emit x; emitLn ")) {";
emit " "; emitLhs z; emitAllocCtor c;
emitLn "} else {";
emit " "; emitLhs z; emit x; emitLn ";";
if updtHeader then emit " lean_ctor_set_tag("; emit z; emit ", "; emit c.cidx; emitLn ");"
emitLn "}";
emitCtorSetArgs z ys
def emitProj (z : VarId) (i : Nat) (x : VarId) : M Unit := do
emitLhs z; emit "lean_ctor_get("; emit x; emit ", "; emit i; emitLn ");"
def emitUProj (z : VarId) (i : Nat) (x : VarId) : M Unit := do
emitLhs z; emit "lean_ctor_get_usize("; emit x; emit ", "; emit i; emitLn ");"
def emitSProj (z : VarId) (t : IRType) (n offset : Nat) (x : VarId) : M Unit := do
emitLhs z;
match t with
| IRType.float => emit "lean_ctor_get_float"
| IRType.uint8 => emit "lean_ctor_get_uint8"
| IRType.uint16 => emit "lean_ctor_get_uint16"
| IRType.uint32 => emit "lean_ctor_get_uint32"
| IRType.uint64 => emit "lean_ctor_get_uint64"
| _ => throw "invalid instruction"
emit "("; emit x; emit ", "; emitOffset n offset; emitLn ");"
def toStringArgs (ys : Array Arg) : List String :=
ys.toList.map argToCString
def emitSimpleExternalCall (f : String) (ps : Array Param) (ys : Array Arg) : M Unit := do
emit f; emit "("
-- We must remove irrelevant arguments to extern calls.
ys.size.foldM
(fun i (first : Bool) =>
if ps[i].ty.isIrrelevant then
pure first
else do
unless first do emit ", "
emitArg ys[i]
pure false)
true
emitLn ");"
pure ()
def emitExternCall (f : FunId) (ps : Array Param) (extData : ExternAttrData) (ys : Array Arg) : M Unit :=
match getExternEntryFor extData `c with
| some (ExternEntry.standard _ extFn) => emitSimpleExternalCall extFn ps ys
| some (ExternEntry.inline _ pat) => do emit (expandExternPattern pat (toStringArgs ys)); emitLn ";"
| some (ExternEntry.foreign _ extFn) => emitSimpleExternalCall extFn ps ys
| _ => throw s!"failed to emit extern application '{f}'"
def emitFullApp (z : VarId) (f : FunId) (ys : Array Arg) : M Unit := do
emitLhs z
let decl ← getDecl f
match decl with
| Decl.extern _ ps _ extData => emitExternCall f ps extData ys
| _ =>
emitCName f
if ys.size > 0 then emit "("; emitArgs ys; emit ")"
emitLn ";"
def emitPartialApp (z : VarId) (f : FunId) (ys : Array Arg) : M Unit := do
let decl ← getDecl f
let arity := decl.params.size;
emitLhs z; emit "lean_alloc_closure((void*)("; emitCName f; emit "), "; emit arity; emit ", "; emit ys.size; emitLn ");";
ys.size.forM fun i => do
let y := ys[i]
emit "lean_closure_set("; emit z; emit ", "; emit i; emit ", "; emitArg y; emitLn ");"
def emitApp (z : VarId) (f : VarId) (ys : Array Arg) : M Unit :=
if ys.size > closureMaxArgs then do
emit "{ lean_object* _aargs[] = {"; emitArgs ys; emitLn "};";
emitLhs z; emit "lean_apply_m("; emit f; emit ", "; emit ys.size; emitLn ", _aargs); }"
else do
emitLhs z; emit "lean_apply_"; emit ys.size; emit "("; emit f; emit ", "; emitArgs ys; emitLn ");"
def emitBoxFn (xType : IRType) : M Unit :=
match xType with
| IRType.usize => emit "lean_box_usize"
| IRType.uint32 => emit "lean_box_uint32"
| IRType.uint64 => emit "lean_box_uint64"
| IRType.float => emit "lean_box_float"
| other => emit "lean_box"
def emitBox (z : VarId) (x : VarId) (xType : IRType) : M Unit := do
emitLhs z; emitBoxFn xType; emit "("; emit x; emitLn ");"
def emitUnbox (z : VarId) (t : IRType) (x : VarId) : M Unit := do
emitLhs z;
match t with
| IRType.usize => emit "lean_unbox_usize"
| IRType.uint32 => emit "lean_unbox_uint32"
| IRType.uint64 => emit "lean_unbox_uint64"
| IRType.float => emit "lean_unbox_float"
| other => emit "lean_unbox";
emit "("; emit x; emitLn ");"
def emitIsShared (z : VarId) (x : VarId) : M Unit := do
emitLhs z; emit "!lean_is_exclusive("; emit x; emitLn ");"
def emitIsTaggedPtr (z : VarId) (x : VarId) : M Unit := do
emitLhs z; emit "!lean_is_scalar("; emit x; emitLn ");"
def toHexDigit (c : Nat) : String :=
String.singleton c.digitChar
def quoteString (s : String) : String :=
let q := "\"";
let q := s.foldl
(fun q c => q ++
if c == '\n' then "\\n"
else if c == '\n' then "\\t"
else if c == '\\' then "\\\\"
else if c == '\"' then "\\\""
else if c.toNat <= 31 then
"\\x" ++ toHexDigit (c.toNat / 16) ++ toHexDigit (c.toNat % 16)
-- TODO(Leo): we should use `\unnnn` for escaping unicode characters.
else String.singleton c)
q;
q ++ "\""
def emitNumLit (t : IRType) (v : Nat) : M Unit := do
if t.isObj then
if v < UInt32.size then
emit "lean_unsigned_to_nat("; emit v; emit "u)"
else
emit "lean_cstr_to_nat(\""; emit v; emit "\")"
else
emit v
def emitLit (z : VarId) (t : IRType) (v : LitVal) : M Unit := do
emitLhs z;
match v with
| LitVal.num v => emitNumLit t v; emitLn ";"
| LitVal.str v => emit "lean_mk_string("; emit (quoteString v); emitLn ");"
def emitVDecl (z : VarId) (t : IRType) (v : Expr) : M Unit :=
match v with
| Expr.ctor c ys => emitCtor z c ys
| Expr.reset n x => emitReset z n x
| Expr.reuse x c u ys => emitReuse z x c u ys
| Expr.proj i x => emitProj z i x
| Expr.uproj i x => emitUProj z i x
| Expr.sproj n o x => emitSProj z t n o x
| Expr.fap c ys => emitFullApp z c ys
| Expr.pap c ys => emitPartialApp z c ys
| Expr.ap x ys => emitApp z x ys
| Expr.box t x => emitBox z x t
| Expr.unbox x => emitUnbox z t x
| Expr.isShared x => emitIsShared z x
| Expr.isTaggedPtr x => emitIsTaggedPtr z x
| Expr.lit v => emitLit z t v
def isTailCall (x : VarId) (v : Expr) (b : FnBody) : M Bool := do
let ctx ← read;
match v, b with
| Expr.fap f _, FnBody.ret (Arg.var y) => pure $ f == ctx.mainFn && x == y
| _, _ => pure false
def paramEqArg (p : Param) (x : Arg) : Bool :=
match x with
| Arg.var x => p.x == x
| _ => false
/-
Given `[p_0, ..., p_{n-1}]`, `[y_0, ..., y_{n-1}]`, representing the assignments
```
p_0 := y_0,
...
p_{n-1} := y_{n-1}
```
Return true iff we have `(i, j)` where `j > i`, and `y_j == p_i`.
That is, we have
```
p_i := y_i,
...
p_j := p_i, -- p_i was overwritten above
```
-/
def overwriteParam (ps : Array Param) (ys : Array Arg) : Bool :=
let n := ps.size;
n.any $ fun i =>
let p := ps[i]
(i+1, n).anyI fun j => paramEqArg p ys[j]
def emitTailCall (v : Expr) : M Unit :=
match v with
| Expr.fap _ ys => do
let ctx ← read
let ps := ctx.mainParams
unless ps.size == ys.size do throw "invalid tail call"
if overwriteParam ps ys then
emitLn "{"
ps.size.forM fun i => do
let p := ps[i]
let y := ys[i]
unless paramEqArg p y do
emit (toCType p.ty); emit " _tmp_"; emit i; emit " = "; emitArg y; emitLn ";"
ps.size.forM fun i => do
let p := ps[i]
let y := ys[i]
unless paramEqArg p y do emit p.x; emit " = _tmp_"; emit i; emitLn ";"
emitLn "}"
else
ys.size.forM fun i => do
let p := ps[i]
let y := ys[i]
unless paramEqArg p y do emit p.x; emit " = "; emitArg y; emitLn ";"
emitLn "goto _start;"
| _ => throw "bug at emitTailCall"
mutual
partial def emitIf (x : VarId) (xType : IRType) (tag : Nat) (t : FnBody) (e : FnBody) : M Unit := do
emit "if ("; emitTag x xType; emit " == "; emit tag; emitLn ")";
emitFnBody t;
emitLn "else";
emitFnBody e
partial def emitCase (x : VarId) (xType : IRType) (alts : Array Alt) : M Unit :=
match isIf alts with
| some (tag, t, e) => emitIf x xType tag t e
| _ => do
emit "switch ("; emitTag x xType; emitLn ") {";
let alts := ensureHasDefault alts;
alts.forM fun alt => do
match alt with
| Alt.ctor c b => emit "case "; emit c.cidx; emitLn ":"; emitFnBody b
| Alt.default b => emitLn "default: "; emitFnBody b
emitLn "}"
partial def emitBlock (b : FnBody) : M Unit := do
match b with
| FnBody.jdecl j xs v b => emitBlock b
| d@(FnBody.vdecl x t v b) =>
let ctx ← read
if isTailCallTo ctx.mainFn d then
emitTailCall v
else
emitVDecl x t v
emitBlock b
| FnBody.inc x n c p b =>
unless p do emitInc x n c
emitBlock b
| FnBody.dec x n c p b =>
unless p do emitDec x n c
emitBlock b
| FnBody.del x b => emitDel x; emitBlock b
| FnBody.setTag x i b => emitSetTag x i; emitBlock b
| FnBody.set x i y b => emitSet x i y; emitBlock b
| FnBody.uset x i y b => emitUSet x i y; emitBlock b
| FnBody.sset x i o y t b => emitSSet x i o y t; emitBlock b
| FnBody.mdata _ b => emitBlock b
| FnBody.ret x => emit "return "; emitArg x; emitLn ";"
| FnBody.case _ x xType alts => emitCase x xType alts
| FnBody.jmp j xs => emitJmp j xs
| FnBody.unreachable => emitLn "lean_panic_unreachable();"
partial def emitJPs : FnBody → M Unit
| FnBody.jdecl j xs v b => do emit j; emitLn ":"; emitFnBody v; emitJPs b
| e => do unless e.isTerminal do emitJPs e.body
partial def emitFnBody (b : FnBody) : M Unit := do
emitLn "{"
let declared ← declareVars b false
if declared then emitLn ""
emitBlock b
emitJPs b
emitLn "}"
end
def emitDeclAux (d : Decl) : M Unit := do
let env ← getEnv
let (vMap, jpMap) := mkVarJPMaps d
withReader (fun ctx => { ctx with jpMap := jpMap }) do
unless hasInitAttr env d.name do
match d with
| Decl.fdecl f xs t b =>
let baseName ← toCName f;
if xs.size == 0 then
emit "static "
emit (toCType t); emit " ";
if xs.size > 0 then
emit baseName;
emit "(";
if xs.size > closureMaxArgs && isBoxedName d.name then
emit "lean_object** _args"
else
xs.size.forM fun i => do
if i > 0 then emit ", "
let x := xs[i]
emit (toCType x.ty); emit " "; emit x.x
emit ")"
else
emit ("_init_" ++ baseName ++ "()")
emitLn " {";
if xs.size > closureMaxArgs && isBoxedName d.name then
xs.size.forM fun i => do
let x := xs[i]
emit "lean_object* "; emit x.x; emit " = _args["; emit i; emitLn "];"
emitLn "_start:";
withReader (fun ctx => { ctx with mainFn := f, mainParams := xs }) (emitFnBody b);
emitLn "}"
| _ => pure ()
def emitDecl (d : Decl) : M Unit := do
let d := d.normalizeIds; -- ensure we don't have gaps in the variable indices
try
emitDeclAux d
catch err =>
throw s!"{err}\ncompiling:\n{d}"
def emitFns : M Unit := do
let env ← getEnv;
let decls := getDecls env;
decls.reverse.forM emitDecl
def emitMarkPersistent (d : Decl) (n : Name) : M Unit := do
if d.resultType.isObj then
emit "lean_mark_persistent("
emitCName n
emitLn ");"
def emitDeclInit (d : Decl) : M Unit := do
let env ← getEnv
let n := d.name
if isIOUnitInitFn env n then
emit "res = "; emitCName n; emitLn "(lean_io_mk_world());"
emitLn "if (lean_io_result_is_error(res)) return res;"
emitLn "lean_dec_ref(res);"
else if d.params.size == 0 then
match getInitFnNameFor? env d.name with
| some initFn =>
emit "res = "; emitCName initFn; emitLn "(lean_io_mk_world());"
emitLn "if (lean_io_result_is_error(res)) return res;"
emitCName n; emitLn " = lean_io_result_get_value(res);"
emitMarkPersistent d n
emitLn "lean_dec_ref(res);"
| _ =>
emitCName n; emit " = "; emitCInitName n; emitLn "();"; emitMarkPersistent d n
def emitInitFn : M Unit := do
let env ← getEnv
let modName ← getModName
env.imports.forM fun imp => emitLn ("lean_object* " ++ mkModuleInitializationFunctionName imp.module ++ "(lean_object*);")
emitLns [
"static bool _G_initialized = false;",
"lean_object* " ++ mkModuleInitializationFunctionName modName ++ "(lean_object* w) {",
"lean_object * res;",
"if (_G_initialized) return lean_io_result_mk_ok(lean_box(0));",
"_G_initialized = true;"
]
env.imports.forM fun imp => emitLns [
"res = " ++ mkModuleInitializationFunctionName imp.module ++ "(lean_io_mk_world());",
"if (lean_io_result_is_error(res)) return res;",
"lean_dec_ref(res);"]
let decls := getDecls env
decls.reverse.forM emitDeclInit
emitLns ["return lean_io_result_mk_ok(lean_box(0));", "}"]
def main : M Unit := do
emitFileHeader
emitFnDecls
emitFns
emitInitFn
emitMainFnIfNeeded
emitFileFooter
end EmitC
@[export lean_ir_emit_c]
def emitC (env : Environment) (modName : Name) : Except String String :=
match (EmitC.main { env := env, modName := modName }).run "" with
| EStateM.Result.ok _ s => Except.ok s
| EStateM.Result.error err _ => Except.error err
end Lean.IR
|
4da76f61a513358a5960a2bf279efa89367b15c5 | ee8cdbabf07f77e7be63a449b8483ce308d37218 | /lean/src/valid/mathd-numbertheory-198.lean | a23583b5940ebaa49351951a41b68f10d78c63ec | [
"MIT",
"Apache-2.0"
] | permissive | zeta1999/miniF2F | 6d66c75d1c18152e224d07d5eed57624f731d4b7 | c1ba9629559c5273c92ec226894baa0c1ce27861 | refs/heads/main | 1,681,897,460,642 | 1,620,646,361,000 | 1,620,646,361,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 245 | lean | /-
Copyright (c) 2021 OpenAI. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kunhao Zheng
-/
import data.nat.basic
import data.real.basic
example : ( 5 ^ 2005 ) % 100 = 25 :=
begin
sorry
end
|
b4b27d98ac694af4d6ff7a8750eb7f102ff799dc | 137c667471a40116a7afd7261f030b30180468c2 | /src/topology/metric_space/gromov_hausdorff_realized.lean | 20417005f6676f08b8cd288abae7685f6373a7df | [
"Apache-2.0"
] | permissive | bragadeesh153/mathlib | 46bf814cfb1eecb34b5d1549b9117dc60f657792 | b577bb2cd1f96eb47031878256856020b76f73cd | refs/heads/master | 1,687,435,188,334 | 1,626,384,207,000 | 1,626,384,207,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 27,479 | 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.metric_space.gluing
import topology.metric_space.hausdorff_distance
import topology.continuous_function.bounded
/-!
# The Gromov-Hausdorff distance is realized
In this file, we construct of a good coupling between nonempty compact metric spaces, minimizing
their Hausdorff distance. This construction is instrumental to study the Gromov-Hausdorff
distance between nonempty compact metric spaces.
Given two nonempty compact metric spaces `X` and `Y`, we define `optimal_GH_coupling X Y` as a
compact metric space, together with two isometric embeddings `optimal_GH_injl` and `optimal_GH_injr`
respectively of `X` and `Y` into `optimal_GH_coupling X Y`. The main property of the optimal
coupling is that the Hausdorff distance between `X` and `Y` in `optimal_GH_coupling X Y` is smaller
than the corresponding distance in any other coupling. We do not prove completely this fact in this
file, but we show a good enough approximation of this fact in `Hausdorff_dist_optimal_le_HD`, that
will suffice to obtain the full statement once the Gromov-Hausdorff distance is properly defined,
in `Hausdorff_dist_optimal`.
The key point in the construction is that the set of possible distances coming from isometric
embeddings of `X` and `Y` in metric spaces is a set of equicontinuous functions. By Arzela-Ascoli,
it is compact, and one can find such a distance which is minimal. This distance defines a premetric
space structure on `X ⊕ Y`. The corresponding metric quotient is `optimal_GH_coupling X Y`.
-/
noncomputable theory
open_locale classical topological_space nnreal
universes u v w
open classical set function topological_space filter metric quotient
open bounded_continuous_function
open sum (inl inr)
local attribute [instance] metric_space_sum
namespace Gromov_Hausdorff
section Gromov_Hausdorff_realized
/- This section shows that the Gromov-Hausdorff distance
is realized. For this, we consider candidate distances on the disjoint union
`X ⊕ Y` of two compact nonempty metric spaces, almost realizing the Gromov-Hausdorff
distance, and show that they form a compact family by applying Arzela-Ascoli
theorem. The existence of a minimizer follows. -/
section definitions
variables (X : Type u) (Y : Type v)
[metric_space X] [compact_space X] [nonempty X]
[metric_space Y] [compact_space Y] [nonempty Y]
@[reducible] private def prod_space_fun : Type* := ((X ⊕ Y) × (X ⊕ Y)) → ℝ
@[reducible] private def Cb : Type* := bounded_continuous_function ((X ⊕ Y) × (X ⊕ Y)) ℝ
private def max_var : ℝ≥0 :=
2 * ⟨diam (univ : set X), diam_nonneg⟩ + 1 + 2 * ⟨diam (univ : set Y), diam_nonneg⟩
private lemma one_le_max_var : 1 ≤ max_var X Y := calc
(1 : real) = 2 * 0 + 1 + 2 * 0 : by simp
... ≤ 2 * diam (univ : set X) + 1 + 2 * diam (univ : set Y) :
by apply_rules [add_le_add, mul_le_mul_of_nonneg_left, diam_nonneg]; norm_num
/-- The set of functions on `X ⊕ Y` that are candidates distances to realize the
minimum of the Hausdorff distances between `X` and `Y` in a coupling -/
def candidates : set (prod_space_fun X Y) :=
{f | (((((∀ x y : X, f (sum.inl x, sum.inl y) = dist x y)
∧ (∀ x y : Y, f (sum.inr x, sum.inr y) = dist x y))
∧ (∀ x y, f (x, y) = f (y, x)))
∧ (∀ x y z, f (x, z) ≤ f (x, y) + f (y, z)))
∧ (∀ x, f (x, x) = 0))
∧ (∀ x y, f (x, y) ≤ max_var X Y) }
/-- Version of the set of candidates in bounded_continuous_functions, to apply
Arzela-Ascoli -/
private def candidates_b : set (Cb X Y) := {f : Cb X Y | (f : _ → ℝ) ∈ candidates X Y}
end definitions --section
section constructions
variables {X : Type u} {Y : Type v}
[metric_space X] [compact_space X] [nonempty X] [metric_space Y] [compact_space Y] [nonempty Y]
{f : prod_space_fun X Y} {x y z t : X ⊕ Y}
local attribute [instance, priority 10] inhabited_of_nonempty'
private lemma max_var_bound : dist x y ≤ max_var X Y := calc
dist x y ≤ diam (univ : set (X ⊕ Y)) :
dist_le_diam_of_mem (bounded_of_compact compact_univ) (mem_univ _) (mem_univ _)
... = diam (inl '' (univ : set X) ∪ inr '' (univ : set Y)) :
by apply congr_arg; ext x y z; cases x; simp [mem_univ, mem_range_self]
... ≤ diam (inl '' (univ : set X)) + dist (inl (default X)) (inr (default Y)) +
diam (inr '' (univ : set Y)) :
diam_union (mem_image_of_mem _ (mem_univ _)) (mem_image_of_mem _ (mem_univ _))
... = diam (univ : set X) + (dist (default X) (default X) + 1 + dist (default Y) (default Y)) +
diam (univ : set Y) :
by { rw [isometry_on_inl.diam_image, isometry_on_inr.diam_image], refl }
... = 1 * diam (univ : set X) + 1 + 1 * diam (univ : set Y) : by simp
... ≤ 2 * diam (univ : set X) + 1 + 2 * diam (univ : set Y) :
begin
apply_rules [add_le_add, mul_le_mul_of_nonneg_right, diam_nonneg, le_refl],
norm_num, norm_num
end
private lemma candidates_symm (fA : f ∈ candidates X Y) : f (x, y) = f (y, x) := fA.1.1.1.2 x y
private lemma candidates_triangle (fA : f ∈ candidates X Y) : f (x, z) ≤ f (x, y) + f (y, z) :=
fA.1.1.2 x y z
private lemma candidates_refl (fA : f ∈ candidates X Y) : f (x, x) = 0 := fA.1.2 x
private lemma candidates_nonneg (fA : f ∈ candidates X Y) : 0 ≤ f (x, y) :=
begin
have : 0 ≤ 2 * f (x, y) := calc
0 = f (x, x) : (candidates_refl fA).symm
... ≤ f (x, y) + f (y, x) : candidates_triangle fA
... = f (x, y) + f (x, y) : by rw [candidates_symm fA]
... = 2 * f (x, y) : by ring,
by linarith
end
private lemma candidates_dist_inl (fA : f ∈ candidates X Y) (x y: X) :
f (inl x, inl y) = dist x y :=
fA.1.1.1.1.1 x y
private lemma candidates_dist_inr (fA : f ∈ candidates X Y) (x y : Y) :
f (inr x, inr y) = dist x y :=
fA.1.1.1.1.2 x y
private lemma candidates_le_max_var (fA : f ∈ candidates X Y) : f (x, y) ≤ max_var X Y :=
fA.2 x y
/-- candidates are bounded by `max_var X Y` -/
private lemma candidates_dist_bound (fA : f ∈ candidates X Y) :
∀ {x y : X ⊕ Y}, f (x, y) ≤ max_var X Y * dist x y
| (inl x) (inl y) := calc
f (inl x, inl y) = dist x y : candidates_dist_inl fA x y
... = dist (inl x) (inl y) : by { rw @sum.dist_eq X Y, refl }
... = 1 * dist (inl x) (inl y) : by simp
... ≤ max_var X Y * dist (inl x) (inl y) :
mul_le_mul_of_nonneg_right (one_le_max_var X Y) dist_nonneg
| (inl x) (inr y) := calc
f (inl x, inr y) ≤ max_var X Y : candidates_le_max_var fA
... = max_var X Y * 1 : by simp
... ≤ max_var X Y * dist (inl x) (inr y) :
mul_le_mul_of_nonneg_left sum.one_dist_le (le_trans (zero_le_one) (one_le_max_var X Y))
| (inr x) (inl y) := calc
f (inr x, inl y) ≤ max_var X Y : candidates_le_max_var fA
... = max_var X Y * 1 : by simp
... ≤ max_var X Y * dist (inl x) (inr y) :
mul_le_mul_of_nonneg_left sum.one_dist_le (le_trans (zero_le_one) (one_le_max_var X Y))
| (inr x) (inr y) := calc
f (inr x, inr y) = dist x y : candidates_dist_inr fA x y
... = dist (inr x) (inr y) : by { rw @sum.dist_eq X Y, refl }
... = 1 * dist (inr x) (inr y) : by simp
... ≤ max_var X Y * dist (inr x) (inr y) :
mul_le_mul_of_nonneg_right (one_le_max_var X Y) dist_nonneg
/-- Technical lemma to prove that candidates are Lipschitz -/
private lemma candidates_lipschitz_aux (fA : f ∈ candidates X Y) :
f (x, y) - f (z, t) ≤ 2 * max_var X Y * dist (x, y) (z, t) :=
calc
f (x, y) - f(z, t) ≤ f (x, t) + f (t, y) - f (z, t) : sub_le_sub_right (candidates_triangle fA) _
... ≤ (f (x, z) + f (z, t) + f(t, y)) - f (z, t) :
sub_le_sub_right (add_le_add_right (candidates_triangle fA) _ ) _
... = f (x, z) + f (t, y) : by simp [sub_eq_add_neg, add_assoc]
... ≤ max_var X Y * dist x z + max_var X Y * dist t y :
add_le_add (candidates_dist_bound fA) (candidates_dist_bound fA)
... ≤ max_var X Y * max (dist x z) (dist t y) + max_var X Y * max (dist x z) (dist t y) :
begin
apply add_le_add,
apply mul_le_mul_of_nonneg_left (le_max_left (dist x z) (dist t y))
(zero_le_one.trans (one_le_max_var X Y)),
apply mul_le_mul_of_nonneg_left (le_max_right (dist x z) (dist t y))
(zero_le_one.trans (one_le_max_var X Y)),
end
... = 2 * max_var X Y * max (dist x z) (dist y t) :
by { simp [dist_comm], ring }
... = 2 * max_var X Y * dist (x, y) (z, t) : by refl
/-- Candidates are Lipschitz -/
private lemma candidates_lipschitz (fA : f ∈ candidates X Y) :
lipschitz_with (2 * max_var X Y) f :=
begin
apply lipschitz_with.of_dist_le_mul,
rintros ⟨x, y⟩ ⟨z, t⟩,
rw [real.dist_eq, abs_sub_le_iff],
use candidates_lipschitz_aux fA,
rw [dist_comm],
exact candidates_lipschitz_aux fA
end
/-- candidates give rise to elements of bounded_continuous_functions -/
def candidates_b_of_candidates (f : prod_space_fun X Y) (fA : f ∈ candidates X Y) : Cb X Y :=
bounded_continuous_function.mk_of_compact ⟨f, (candidates_lipschitz fA).continuous⟩
lemma candidates_b_of_candidates_mem (f : prod_space_fun X Y) (fA : f ∈ candidates X Y) :
candidates_b_of_candidates f fA ∈ candidates_b X Y := fA
/-- The distance on `X ⊕ Y` is a candidate -/
private lemma dist_mem_candidates : (λp : (X ⊕ Y) × (X ⊕ Y), dist p.1 p.2) ∈ candidates X Y :=
begin
simp only [candidates, dist_comm, forall_const, and_true, add_comm, eq_self_iff_true,
and_self, sum.forall, set.mem_set_of_eq, dist_self],
repeat { split
<|> exact (λa y z, dist_triangle_left _ _ _)
<|> exact (λx y, by refl)
<|> exact (λx y, max_var_bound) }
end
/-- The distance on `X ⊕ Y` as a candidate -/
def candidates_b_dist (X : Type u) (Y : Type v) [metric_space X] [compact_space X] [inhabited X]
[metric_space Y] [compact_space Y] [inhabited Y] : Cb X Y :=
candidates_b_of_candidates _ dist_mem_candidates
lemma candidates_b_dist_mem_candidates_b : candidates_b_dist X Y ∈ candidates_b X Y :=
candidates_b_of_candidates_mem _ _
private lemma candidates_b_nonempty : (candidates_b X Y).nonempty :=
⟨_, candidates_b_dist_mem_candidates_b⟩
/-- To apply Arzela-Ascoli, we need to check that the set of candidates is closed and
equicontinuous. Equicontinuity follows from the Lipschitz control, we check closedness. -/
private lemma closed_candidates_b : is_closed (candidates_b X Y) :=
begin
have I1 : ∀ x y, is_closed {f : Cb X Y | f (inl x, inl y) = dist x y} :=
λx y, is_closed_eq continuous_evalx continuous_const,
have I2 : ∀ x y, is_closed {f : Cb X Y | f (inr x, inr y) = dist x y } :=
λx y, is_closed_eq continuous_evalx continuous_const,
have I3 : ∀ x y, is_closed {f : Cb X Y | f (x, y) = f (y, x)} :=
λx y, is_closed_eq continuous_evalx continuous_evalx,
have I4 : ∀ x y z, is_closed {f : Cb X Y | f (x, z) ≤ f (x, y) + f (y, z)} :=
λx y z, is_closed_le continuous_evalx (continuous_evalx.add continuous_evalx),
have I5 : ∀ x, is_closed {f : Cb X Y | f (x, x) = 0} :=
λx, is_closed_eq continuous_evalx continuous_const,
have I6 : ∀ x y, is_closed {f : Cb X Y | f (x, y) ≤ max_var X Y} :=
λx y, is_closed_le continuous_evalx continuous_const,
have : candidates_b X Y = (⋂x y, {f : Cb X Y | f ((@inl X Y x), (@inl X Y y)) = dist x y})
∩ (⋂x y, {f : Cb X Y | f ((@inr X Y x), (@inr X Y y)) = dist x y})
∩ (⋂x y, {f : Cb X Y | f (x, y) = f (y, x)})
∩ (⋂x y z, {f : Cb X Y | f (x, z) ≤ f (x, y) + f (y, z)})
∩ (⋂x, {f : Cb X Y | f (x, x) = 0})
∩ (⋂x y, {f : Cb X Y | f (x, y) ≤ max_var X Y}),
{ ext, simp only [candidates_b, candidates, mem_inter_eq, mem_Inter, mem_set_of_eq], refl },
rw this,
repeat { apply is_closed.inter _ _
<|> apply is_closed_Inter _
<|> apply I1 _ _
<|> apply I2 _ _
<|> apply I3 _ _
<|> apply I4 _ _ _
<|> apply I5 _
<|> apply I6 _ _
<|> assume x },
end
/-- Compactness of candidates (in bounded_continuous_functions) follows. -/
private lemma compact_candidates_b : is_compact (candidates_b X Y) :=
begin
refine arzela_ascoli₂ (Icc 0 (max_var X Y)) is_compact_Icc (candidates_b X Y)
closed_candidates_b _ _,
{ rintros f ⟨x1, x2⟩ hf,
simp only [set.mem_Icc],
exact ⟨candidates_nonneg hf, candidates_le_max_var hf⟩ },
{ refine equicontinuous_of_continuity_modulus (λt, 2 * max_var X Y * t) _ _ _,
{ have : tendsto (λ (t : ℝ), 2 * (max_var X Y : ℝ) * t) (𝓝 0) (𝓝 (2 * max_var X Y * 0)) :=
tendsto_const_nhds.mul tendsto_id,
simpa using this },
{ assume x y f hf,
exact (candidates_lipschitz hf).dist_le_mul _ _ } }
end
/-- We will then choose the candidate minimizing the Hausdorff distance. Except that we are not
in a metric space setting, so we need to define our custom version of Hausdorff distance,
called HD, and prove its basic properties. -/
def HD (f : Cb X Y) := max (⨆ x, ⨅ y, f (inl x, inr y)) (⨆ y, ⨅ x, f (inl x, inr y))
/- We will show that HD is continuous on bounded_continuous_functions, to deduce that its
minimum on the compact set candidates_b is attained. Since it is defined in terms of
infimum and supremum on `ℝ`, which is only conditionnally complete, we will need all the time
to check that the defining sets are bounded below or above. This is done in the next few
technical lemmas -/
lemma HD_below_aux1 {f : Cb X Y} (C : ℝ) {x : X} :
bdd_below (range (λ (y : Y), f (inl x, inr y) + C)) :=
let ⟨cf, hcf⟩ := (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 in
⟨cf + C, forall_range_iff.2 (λi, add_le_add_right ((λx, hcf (mem_range_self x)) _) _)⟩
private lemma HD_bound_aux1 (f : Cb X Y) (C : ℝ) :
bdd_above (range (λ (x : X), ⨅ y, f (inl x, inr y) + C)) :=
begin
rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).2 with ⟨Cf, hCf⟩,
refine ⟨Cf + C, forall_range_iff.2 (λx, _)⟩,
calc (⨅ y, f (inl x, inr y) + C) ≤ f (inl x, inr (default Y)) + C :
cinfi_le (HD_below_aux1 C) (default Y)
... ≤ Cf + C : add_le_add ((λx, hCf (mem_range_self x)) _) (le_refl _)
end
lemma HD_below_aux2 {f : Cb X Y} (C : ℝ) {y : Y} :
bdd_below (range (λ (x : X), f (inl x, inr y) + C)) :=
let ⟨cf, hcf⟩ := (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 in
⟨cf + C, forall_range_iff.2 (λi, add_le_add_right ((λx, hcf (mem_range_self x)) _) _)⟩
private lemma HD_bound_aux2 (f : Cb X Y) (C : ℝ) :
bdd_above (range (λ (y : Y), ⨅ x, f (inl x, inr y) + C)) :=
begin
rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).2 with ⟨Cf, hCf⟩,
refine ⟨Cf + C, forall_range_iff.2 (λy, _)⟩,
calc (⨅ x, f (inl x, inr y) + C) ≤ f (inl (default X), inr y) + C :
cinfi_le (HD_below_aux2 C) (default X)
... ≤ Cf + C : add_le_add ((λx, hCf (mem_range_self x)) _) (le_refl _)
end
/-- Explicit bound on `HD (dist)`. This means that when looking for minimizers it will
be sufficient to look for functions with `HD(f)` bounded by this bound. -/
lemma HD_candidates_b_dist_le :
HD (candidates_b_dist X Y) ≤ diam (univ : set X) + 1 + diam (univ : set Y) :=
begin
refine max_le (csupr_le (λx, _)) (csupr_le (λy, _)),
{ have A : (⨅ y, candidates_b_dist X Y (inl x, inr y)) ≤
candidates_b_dist X Y (inl x, inr (default Y)) :=
cinfi_le (by simpa using HD_below_aux1 0) (default Y),
have B : dist (inl x) (inr (default Y)) ≤ diam (univ : set X) + 1 + diam (univ : set Y) := calc
dist (inl x) (inr (default Y)) = dist x (default X) + 1 + dist (default Y) (default Y) : rfl
... ≤ diam (univ : set X) + 1 + diam (univ : set Y) :
begin
apply add_le_add (add_le_add _ (le_refl _)),
exact dist_le_diam_of_mem (bounded_of_compact (compact_univ)) (mem_univ _) (mem_univ _),
any_goals { exact ordered_add_comm_monoid.to_covariant_class_left ℝ },
any_goals { exact ordered_add_comm_monoid.to_covariant_class_right ℝ },
exact dist_le_diam_of_mem (bounded_of_compact (compact_univ)) (mem_univ _) (mem_univ _),
end,
exact le_trans A B },
{ have A : (⨅ x, candidates_b_dist X Y (inl x, inr y)) ≤
candidates_b_dist X Y (inl (default X), inr y) :=
cinfi_le (by simpa using HD_below_aux2 0) (default X),
have B : dist (inl (default X)) (inr y) ≤ diam (univ : set X) + 1 + diam (univ : set Y) := calc
dist (inl (default X)) (inr y) = dist (default X) (default X) + 1 + dist (default Y) y : rfl
... ≤ diam (univ : set X) + 1 + diam (univ : set Y) :
begin
apply add_le_add (add_le_add _ (le_refl _)),
exact dist_le_diam_of_mem (bounded_of_compact (compact_univ)) (mem_univ _) (mem_univ _),
any_goals { exact ordered_add_comm_monoid.to_covariant_class_left ℝ },
any_goals { exact ordered_add_comm_monoid.to_covariant_class_right ℝ },
exact dist_le_diam_of_mem (bounded_of_compact (compact_univ)) (mem_univ _) (mem_univ _)
end,
exact le_trans A B },
end
/- To check that HD is continuous, we check that it is Lipschitz. As HD is a max, we
prove separately inequalities controlling the two terms (relying too heavily on copy-paste...) -/
private lemma HD_lipschitz_aux1 (f g : Cb X Y) :
(⨆ x, ⨅ y, f (inl x, inr y)) ≤ (⨆ x, ⨅ y, g (inl x, inr y)) + dist f g :=
begin
rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 with ⟨cg, hcg⟩,
have Hcg : ∀ x, cg ≤ g x := λx, hcg (mem_range_self x),
rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 with ⟨cf, hcf⟩,
have Hcf : ∀ x, cf ≤ f x := λx, hcf (mem_range_self x),
-- prove the inequality but with `dist f g` inside, by using inequalities comparing
-- supr to supr and infi to infi
have Z : (⨆ x, ⨅ y, f (inl x, inr y)) ≤ ⨆ x, ⨅ y, g (inl x, inr y) + dist f g :=
csupr_le_csupr (HD_bound_aux1 _ (dist f g))
(λx, cinfi_le_cinfi ⟨cf, forall_range_iff.2(λi, Hcf _)⟩ (λy, coe_le_coe_add_dist)),
-- move the `dist f g` out of the infimum and the supremum, arguing that continuous monotone maps
-- (here the addition of `dist f g`) preserve infimum and supremum
have E1 : ∀ x, (⨅ y, g (inl x, inr y)) + dist f g = ⨅ y, g (inl x, inr y) + dist f g,
{ assume x,
refine map_cinfi_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) _ _,
{ assume x y hx, simpa },
{ show bdd_below (range (λ (y : Y), g (inl x, inr y))),
from ⟨cg, forall_range_iff.2(λi, Hcg _)⟩ } },
have E2 : (⨆ x, ⨅ y, g (inl x, inr y)) + dist f g = ⨆ x, (⨅ y, g (inl x, inr y)) + dist f g,
{ refine map_csupr_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) _ _,
{ assume x y hx, simpa },
{ by simpa using HD_bound_aux1 _ 0 } },
-- deduce the result from the above two steps
simpa [E2, E1, function.comp]
end
private lemma HD_lipschitz_aux2 (f g : Cb X Y) :
(⨆ y, ⨅ x, f (inl x, inr y)) ≤ (⨆ y, ⨅ x, g (inl x, inr y)) + dist f g :=
begin
rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 with ⟨cg, hcg⟩,
have Hcg : ∀ x, cg ≤ g x := λx, hcg (mem_range_self x),
rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 with ⟨cf, hcf⟩,
have Hcf : ∀ x, cf ≤ f x := λx, hcf (mem_range_self x),
-- prove the inequality but with `dist f g` inside, by using inequalities comparing
-- supr to supr and infi to infi
have Z : (⨆ y, ⨅ x, f (inl x, inr y)) ≤ ⨆ y, ⨅ x, g (inl x, inr y) + dist f g :=
csupr_le_csupr (HD_bound_aux2 _ (dist f g))
(λy, cinfi_le_cinfi ⟨cf, forall_range_iff.2(λi, Hcf _)⟩ (λy, coe_le_coe_add_dist)),
-- move the `dist f g` out of the infimum and the supremum, arguing that continuous monotone maps
-- (here the addition of `dist f g`) preserve infimum and supremum
have E1 : ∀ y, (⨅ x, g (inl x, inr y)) + dist f g = ⨅ x, g (inl x, inr y) + dist f g,
{ assume y,
refine map_cinfi_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) _ _,
{ assume x y hx, simpa },
{ show bdd_below (range (λx:X, g (inl x, inr y))),
from ⟨cg, forall_range_iff.2 (λi, Hcg _)⟩ } },
have E2 : (⨆ y, ⨅ x, g (inl x, inr y)) + dist f g = ⨆ y, (⨅ x, g (inl x, inr y)) + dist f g,
{ refine map_csupr_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) _ _,
{ assume x y hx, simpa },
{ by simpa using HD_bound_aux2 _ 0 } },
-- deduce the result from the above two steps
simpa [E2, E1]
end
private lemma HD_lipschitz_aux3 (f g : Cb X Y) : HD f ≤ HD g + dist f g :=
max_le (le_trans (HD_lipschitz_aux1 f g) (add_le_add_right (le_max_left _ _) _))
(le_trans (HD_lipschitz_aux2 f g) (add_le_add_right (le_max_right _ _) _))
/-- Conclude that HD, being Lipschitz, is continuous -/
private lemma HD_continuous : continuous (HD : Cb X Y → ℝ) :=
lipschitz_with.continuous (lipschitz_with.of_le_add HD_lipschitz_aux3)
end constructions --section
section consequences
variables (X : Type u) (Y : Type v) [metric_space X] [compact_space X] [nonempty X] [metric_space Y]
[compact_space Y] [nonempty Y]
/- Now that we have proved that the set of candidates is compact, and that HD is continuous,
we can finally select a candidate minimizing HD. This will be the candidate realizing the
optimal coupling. -/
private lemma exists_minimizer : ∃ f ∈ candidates_b X Y, ∀ g ∈ candidates_b X Y, HD f ≤ HD g :=
compact_candidates_b.exists_forall_le candidates_b_nonempty HD_continuous.continuous_on
private definition optimal_GH_dist : Cb X Y := classical.some (exists_minimizer X Y)
private lemma optimal_GH_dist_mem_candidates_b : optimal_GH_dist X Y ∈ candidates_b X Y :=
by cases (classical.some_spec (exists_minimizer X Y)); assumption
private lemma HD_optimal_GH_dist_le (g : Cb X Y) (hg : g ∈ candidates_b X Y) :
HD (optimal_GH_dist X Y) ≤ HD g :=
let ⟨Z1, Z2⟩ := classical.some_spec (exists_minimizer X Y) in Z2 g hg
/-- With the optimal candidate, construct a premetric space structure on `X ⊕ Y`, on which the
predistance is given by the candidate. Then, we will identify points at `0` predistance
to obtain a genuine metric space -/
def premetric_optimal_GH_dist : pseudo_metric_space (X ⊕ Y) :=
{ dist := λp q, optimal_GH_dist X Y (p, q),
dist_self := λx, candidates_refl (optimal_GH_dist_mem_candidates_b X Y),
dist_comm := λx y, candidates_symm (optimal_GH_dist_mem_candidates_b X Y),
dist_triangle := λx y z, candidates_triangle (optimal_GH_dist_mem_candidates_b X Y) }
local attribute [instance] premetric_optimal_GH_dist pseudo_metric.dist_setoid
/-- A metric space which realizes the optimal coupling between `X` and `Y` -/
@[derive metric_space, nolint has_inhabited_instance]
definition optimal_GH_coupling : Type* :=
pseudo_metric_quot (X ⊕ Y)
/-- Injection of `X` in the optimal coupling between `X` and `Y` -/
def optimal_GH_injl (x : X) : optimal_GH_coupling X Y := ⟦inl x⟧
/-- The injection of `X` in the optimal coupling between `X` and `Y` is an isometry. -/
lemma isometry_optimal_GH_injl : isometry (optimal_GH_injl X Y) :=
begin
refine isometry_emetric_iff_metric.2 (λx y, _),
change dist ⟦inl x⟧ ⟦inl y⟧ = dist x y,
exact candidates_dist_inl (optimal_GH_dist_mem_candidates_b X Y) _ _,
end
/-- Injection of `Y` in the optimal coupling between `X` and `Y` -/
def optimal_GH_injr (y : Y) : optimal_GH_coupling X Y := ⟦inr y⟧
/-- The injection of `Y` in the optimal coupling between `X` and `Y` is an isometry. -/
lemma isometry_optimal_GH_injr : isometry (optimal_GH_injr X Y) :=
begin
refine isometry_emetric_iff_metric.2 (λx y, _),
change dist ⟦inr x⟧ ⟦inr y⟧ = dist x y,
exact candidates_dist_inr (optimal_GH_dist_mem_candidates_b X Y) _ _,
end
/-- The optimal coupling between two compact spaces `X` and `Y` is still a compact space -/
instance compact_space_optimal_GH_coupling : compact_space (optimal_GH_coupling X Y) :=
⟨begin
have : (univ : set (optimal_GH_coupling X Y)) =
(optimal_GH_injl X Y '' univ) ∪ (optimal_GH_injr X Y '' univ),
{ refine subset.antisymm (λxc hxc, _) (subset_univ _),
rcases quotient.exists_rep xc with ⟨x, hx⟩,
cases x; rw ← hx,
{ have : ⟦inl x⟧ = optimal_GH_injl X Y x := rfl,
rw this,
exact mem_union_left _ (mem_image_of_mem _ (mem_univ _)) },
{ have : ⟦inr x⟧ = optimal_GH_injr X Y x := rfl,
rw this,
exact mem_union_right _ (mem_image_of_mem _ (mem_univ _)) } },
rw this,
exact (compact_univ.image (isometry_optimal_GH_injl X Y).continuous).union
(compact_univ.image (isometry_optimal_GH_injr X Y).continuous)
end⟩
/-- For any candidate `f`, `HD(f)` is larger than or equal to the Hausdorff distance in the
optimal coupling. This follows from the fact that HD of the optimal candidate is exactly
the Hausdorff distance in the optimal coupling, although we only prove here the inequality
we need. -/
lemma Hausdorff_dist_optimal_le_HD {f} (h : f ∈ candidates_b X Y) :
Hausdorff_dist (range (optimal_GH_injl X Y)) (range (optimal_GH_injr X Y)) ≤ HD f :=
begin
refine le_trans (le_of_forall_le_of_dense (λr hr, _)) (HD_optimal_GH_dist_le X Y f h),
have A : ∀ x ∈ range (optimal_GH_injl X Y), ∃ y ∈ range (optimal_GH_injr X Y), dist x y ≤ r,
{ assume x hx,
rcases mem_range.1 hx with ⟨z, hz⟩,
rw ← hz,
have I1 : (⨆ x, ⨅ y, optimal_GH_dist X Y (inl x, inr y)) < r :=
lt_of_le_of_lt (le_max_left _ _) hr,
have I2 : (⨅ y, optimal_GH_dist X Y (inl z, inr y)) ≤
⨆ x, ⨅ y, optimal_GH_dist X Y (inl x, inr y) :=
le_cSup (by simpa using HD_bound_aux1 _ 0) (mem_range_self _),
have I : (⨅ y, optimal_GH_dist X Y (inl z, inr y)) < r := lt_of_le_of_lt I2 I1,
rcases exists_lt_of_cInf_lt (range_nonempty _) I with ⟨r', r'range, hr'⟩,
rcases mem_range.1 r'range with ⟨z', hz'⟩,
existsi [optimal_GH_injr X Y z', mem_range_self _],
have : (optimal_GH_dist X Y) (inl z, inr z') ≤ r, by { rw hz', exact le_of_lt hr' },
exact this },
refine Hausdorff_dist_le_of_mem_dist _ A _,
{ rcases exists_mem_of_nonempty X with ⟨xX, _⟩,
have : optimal_GH_injl X Y xX ∈ range (optimal_GH_injl X Y) := mem_range_self _,
rcases A _ this with ⟨y, yrange, hy⟩,
exact le_trans dist_nonneg hy },
{ assume y hy,
rcases mem_range.1 hy with ⟨z, hz⟩,
rw ← hz,
have I1 : (⨆ y, ⨅ x, optimal_GH_dist X Y (inl x, inr y)) < r :=
lt_of_le_of_lt (le_max_right _ _) hr,
have I2 : (⨅ x, optimal_GH_dist X Y (inl x, inr z)) ≤
⨆ y, ⨅ x, optimal_GH_dist X Y (inl x, inr y) :=
le_cSup (by simpa using HD_bound_aux2 _ 0) (mem_range_self _),
have I : (⨅ x, optimal_GH_dist X Y (inl x, inr z)) < r := lt_of_le_of_lt I2 I1,
rcases exists_lt_of_cInf_lt (range_nonempty _) I with ⟨r', r'range, hr'⟩,
rcases mem_range.1 r'range with ⟨z', hz'⟩,
existsi [optimal_GH_injl X Y z', mem_range_self _],
have : (optimal_GH_dist X Y) (inl z', inr z) ≤ r, by { rw hz', exact le_of_lt hr' },
rw dist_comm,
exact this }
end
end consequences
/- We are done with the construction of the optimal coupling -/
end Gromov_Hausdorff_realized
end Gromov_Hausdorff
|
480d3811ba87fa6e5a9d0d59a05af4a6d2f3896e | 6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf | /src/game/world6/level4.lean | dd33a86bc0f7fcff0af39de3d435a318ae569565 | [
"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 | 1,237 | lean | /-
# Proposition world.
## Level 4: `apply`.
Let's do the same level again:

We are given a proof $p$ of $P$ and our goal is to find a proof of $U$, or
in other words to find a path through the maze that links $P$ to $U$.
In level 3 we solved this by using `have`s to move forward, from $P$
to $Q$ to $T$ to $U$. Using the `apply` tactic we can instead construct
the path backwards, moving from $U$ to $T$ to $Q$ to $P$.
Our goal is to prove $U$. But $l:T\implies U$ is
an implication which we are assuming, so it would suffice to prove $T$.
Tell Lean this by starting the proof below with
`apply l,`
and notice that our assumptions don't change but *the goal changes*
from `⊢ U` to `⊢ T`.
Keep `apply`ing implications until your goal is `P`, and try not
to get lost! Now solve this goal
with `exact p`. Note: you will need to learn the difference between
`exact p` (which works) and `exact P` (which doesn't, because $P$ is
not a proof of $P$).
-/
/- Lemma : no-side-bar
We can solve a maze.
-/
lemma maze (P Q R S T U: Prop)
(p : P)
(h : P → Q)
(i : Q → R)
(j : Q → T)
(k : S → T)
(l : T → U)
: U :=
begin
end |
80d8ae45e55b3cf353792673d37dad7a3c280d7e | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/category_theory/sites/limits.lean | 50ff8db9774df22a45d9290efb9e59d02d6341a7 | [
"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,045 | lean | /-
Copyright (c) 2021 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz
-/
import category_theory.limits.creates
import category_theory.sites.sheafification
/-!
# Limits and colimits of sheaves
## Limits
We prove that the forgetful functor from `Sheaf J D` to presheaves creates limits.
If the target category `D` has limits (of a certain shape),
this then implies that `Sheaf J D` has limits of the same shape and that the forgetful
functor preserves these limits.
## Colimits
Given a diagram `F : K ⥤ Sheaf J D` of sheaves, and a colimit cocone on the level of presheaves,
we show that the cocone obtained by sheafifying the cocone point is a colimit cocone of sheaves.
This allows us to show that `Sheaf J D` has colimits (of a certain shape) as soon as `D` does.
-/
namespace category_theory
namespace Sheaf
open category_theory.limits
open opposite
section limits
universes w v u z
variables {C : Type (max v u)} [category.{v} C] {J : grothendieck_topology C}
variables {D : Type w} [category.{max v u} D]
variables {K : Type z} [small_category K]
noncomputable theory
section
/-- An auxiliary definition to be used below.
Whenever `E` is a cone of shape `K` of sheaves, and `S` is the multifork associated to a
covering `W` of an object `X`, with respect to the cone point `E.X`, this provides a cone of
shape `K` of objects in `D`, with cone point `S.X`.
See `is_limit_multifork_of_is_limit` for more on how this definition is used.
-/
def multifork_evaluation_cone (F : K ⥤ Sheaf J D)
(E : cone (F ⋙ Sheaf_to_presheaf J D)) (X : C) (W : J.cover X) (S : multifork (W.index E.X)) :
cone (F ⋙ Sheaf_to_presheaf J D ⋙ (evaluation Cᵒᵖ D).obj (op X)) :=
{ X := S.X,
π :=
{ app := λ k, (presheaf.is_limit_of_is_sheaf J (F.obj k).1 W (F.obj k).2).lift $
multifork.of_ι _ S.X (λ i, S.ι i ≫ (E.π.app k).app (op i.Y)) begin
intros i,
simp only [category.assoc],
erw [← (E.π.app k).naturality, ← (E.π.app k).naturality],
dsimp,
simp only [← category.assoc],
congr' 1,
apply S.condition,
end,
naturality' := begin
intros i j f,
dsimp [presheaf.is_limit_of_is_sheaf],
rw [category.id_comp],
apply presheaf.is_sheaf.hom_ext (F.obj j).2 W,
intros ii,
rw [presheaf.is_sheaf.amalgamate_map, category.assoc, ← (F.map f).val.naturality,
← category.assoc, presheaf.is_sheaf.amalgamate_map],
dsimp [multifork.of_ι],
erw [category.assoc, ← E.w f],
tidy,
end } }
variables [has_limits_of_shape K D]
/-- If `E` is a cone of shape `K` of sheaves, which is a limit on the level of presheves,
this definition shows that the limit presheaf satisfies the multifork variant of the sheaf
condition, at a given covering `W`.
This is used below in `is_sheaf_of_is_limit` to show that the limit presheaf is indeed a sheaf.
-/
def is_limit_multifork_of_is_limit (F : K ⥤ Sheaf J D)
(E : cone (F ⋙ Sheaf_to_presheaf J D))
(hE : is_limit E) (X : C) (W : J.cover X) : is_limit (W.multifork E.X) :=
multifork.is_limit.mk _
(λ S, (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op X)) hE).lift $
multifork_evaluation_cone F E X W S)
begin
intros S i,
apply (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op i.Y)) hE).hom_ext,
intros k,
dsimp [multifork.of_ι],
erw [category.assoc, (E.π.app k).naturality],
dsimp,
rw ← category.assoc,
erw (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op X)) hE).fac
(multifork_evaluation_cone F E X W S),
dsimp [multifork_evaluation_cone, presheaf.is_limit_of_is_sheaf],
erw presheaf.is_sheaf.amalgamate_map,
refl,
end
begin
intros S m hm,
apply (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op X)) hE).hom_ext,
intros k,
dsimp,
erw (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op X)) hE).fac,
apply presheaf.is_sheaf.hom_ext (F.obj k).2 W,
intros i,
erw presheaf.is_sheaf.amalgamate_map,
dsimp [multifork.of_ι],
change _ = S.ι i ≫ _,
erw [← hm, category.assoc, ← (E.π.app k).naturality, category.assoc],
refl,
end
/-- If `E` is a cone which is a limit on the level of presheaves,
then the limit presheaf is again a sheaf.
This is used to show that the forgetful functor from sheaves to presheaves creates limits.
-/
lemma is_sheaf_of_is_limit (F : K ⥤ Sheaf J D) (E : cone (F ⋙ Sheaf_to_presheaf J D))
(hE : is_limit E) : presheaf.is_sheaf J E.X :=
begin
rw presheaf.is_sheaf_iff_multifork,
intros X S,
exact ⟨is_limit_multifork_of_is_limit _ _ hE _ _⟩,
end
instance (F : K ⥤ Sheaf J D) : creates_limit F (Sheaf_to_presheaf J D) :=
creates_limit_of_reflects_iso $ λ E hE,
{ lifted_cone := ⟨⟨E.X, is_sheaf_of_is_limit _ _ hE⟩,
⟨λ t, ⟨E.π.app _⟩, λ u v e, Sheaf.hom.ext _ _ $ E.π.naturality _⟩⟩,
valid_lift := cones.ext (eq_to_iso rfl) $ λ j, by { dsimp, simp },
makes_limit :=
{ lift := λ S, ⟨hE.lift ((Sheaf_to_presheaf J D).map_cone S)⟩,
fac' := λ S j, by { ext1, apply hE.fac ((Sheaf_to_presheaf J D).map_cone S) j },
uniq' := λ S m hm, begin
ext1,
exact hE.uniq ((Sheaf_to_presheaf J D).map_cone S) m.val (λ j, congr_arg hom.val (hm j)),
end } }
instance : creates_limits_of_shape K (Sheaf_to_presheaf J D) := {}
instance : has_limits_of_shape K (Sheaf J D) :=
has_limits_of_shape_of_has_limits_of_shape_creates_limits_of_shape (Sheaf_to_presheaf J D)
end
instance [has_limits D] : creates_limits (Sheaf_to_presheaf J D) := ⟨⟩
instance [has_limits D] : has_limits (Sheaf J D) :=
has_limits_of_has_limits_creates_limits (Sheaf_to_presheaf J D)
end limits
section colimits
universes w v u
variables {C : Type (max v u)} [category.{v} C] {J : grothendieck_topology C}
variables {D : Type w} [category.{max v u} D]
variables {K : Type (max v u)} [small_category K]
-- Now we need a handful of instances to obtain sheafification...
variables [concrete_category.{max v u} D]
variables [∀ (P : Cᵒᵖ ⥤ D) (X : C) (S : J.cover X), has_multiequalizer (S.index P)]
variables [preserves_limits (forget D)]
variables [∀ (X : C), has_colimits_of_shape (J.cover X)ᵒᵖ D]
variables [∀ (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ (forget D)]
variables [reflects_isomorphisms (forget D)]
/-- Construct a cocone by sheafifying a cocone point of a cocone `E` of presheaves
over a functor which factors through sheaves.
In `is_colimit_sheafify_cocone`, we show that this is a colimit cocone when `E` is a colimit. -/
@[simps]
def sheafify_cocone {F : K ⥤ Sheaf J D} (E : cocone (F ⋙ Sheaf_to_presheaf J D)) : cocone F :=
{ X := ⟨J.sheafify E.X, grothendieck_topology.plus.is_sheaf_plus_plus _ _⟩,
ι :=
{ app := λ k, ⟨E.ι.app k ≫ J.to_sheafify E.X⟩,
naturality' := λ i j f, by { ext1, dsimp, erw [category.comp_id, ← category.assoc, E.w f] } } }
/-- If `E` is a colimit cocone of presheaves, over a diagram factoring through sheaves,
then `sheafify_cocone E` is a colimit cocone. -/
@[simps]
def is_colimit_sheafify_cocone {F : K ⥤ Sheaf J D} (E : cocone (F ⋙ Sheaf_to_presheaf J D))
(hE : is_colimit E) :
is_colimit (sheafify_cocone E) :=
{ desc := λ S, ⟨J.sheafify_lift (hE.desc ((Sheaf_to_presheaf J D).map_cocone S)) S.X.2⟩,
fac' := begin
intros S j,
ext1,
dsimp [sheafify_cocone],
erw [category.assoc, J.to_sheafify_sheafify_lift, hE.fac],
refl,
end,
uniq' := begin
intros S m hm,
ext1,
apply J.sheafify_lift_unique,
apply hE.uniq ((Sheaf_to_presheaf J D).map_cocone S),
intros j,
dsimp,
simpa only [← category.assoc, ← hm],
end }
instance [has_colimits_of_shape K D] : has_colimits_of_shape K (Sheaf J D) :=
⟨λ F, has_colimit.mk ⟨sheafify_cocone (colimit.cocone _),
is_colimit_sheafify_cocone _ (colimit.is_colimit _)⟩⟩
instance [has_colimits D] : has_colimits (Sheaf J D) := ⟨infer_instance⟩
end colimits
end Sheaf
end category_theory
|
8859024c3daf7b25ebeb2f2e465adc6033f7ca8c | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/Lean3Lib/init/meta/decl_cmds_auto.lean | 70f4a11ebc96dc114a133cfbcfd7599086134418 | [] | 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 | 300 | 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
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.meta.tactic
import Mathlib.Lean3Lib.init.meta.rb_map
namespace Mathlib
end Mathlib |
41abc8936b04fd322b438a3ee239f00ae1b016d4 | 8b9f17008684d796c8022dab552e42f0cb6fb347 | /tests/lean/run/group4.lean | 3a0856e03b2f945256995552e55fae2d7802bf73 | [
"Apache-2.0"
] | permissive | chubbymaggie/lean | 0d06ae25f9dd396306fb02190e89422ea94afd7b | d2c7b5c31928c98f545b16420d37842c43b4ae9a | refs/heads/master | 1,611,313,622,901 | 1,430,266,839,000 | 1,430,267,083,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,066 | lean | -- Copyright (c) 2014 Jeremy Avigad. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Jeremy Avigad, Leonardo de Moura
-- algebra.group
-- =============
-- Various structures with 1, *, inv, including groups.
import logic.eq
import data.unit data.sigma data.prod
import algebra.function algebra.binary
open eq
namespace algebra
structure has_mul [class] (A : Type) :=
mk :: (mul : A → A → A)
structure has_one [class] (A : Type) :=
mk :: (one : A)
structure has_inv [class] (A : Type) :=
mk :: (inv : A → A)
infixl `*` := has_mul.mul
postfix `⁻¹` := has_inv.inv
notation 1 := !has_one.one
structure semigroup [class] (A : Type) extends has_mul A :=
mk :: (assoc : ∀ a b c, mul (mul a b) c = mul a (mul b c))
set_option pp.notation false
-- set_option pp.implicit true
-- set_option pp.coercions true
print instances has_mul
section
variables {A : Type} [s : semigroup A]
include s
variables a b : A
example : a * b = semigroup.mul a b :=
rfl
theorem mul_assoc (a b c : A) : a * b * c = a * (b * c) :=
semigroup.assoc a b c
end
structure comm_semigroup [class] (A : Type) extends semigroup A :=
mk :: (comm : ∀a b, mul a b = mul b a)
namespace comm_semigroup
variables {A : Type} [s : comm_semigroup A]
include s
variables a b c : A
theorem mul_comm : a * b = b * a := !comm_semigroup.comm
theorem mul_left_comm : a * (b * c) = b * (a * c) :=
binary.left_comm mul_comm mul_assoc a b c
end comm_semigroup
structure monoid [class] (A : Type) extends semigroup A, has_one A :=
mk :: (right_id : ∀a, mul a one = a) (left_id : ∀a, mul one a = a)
section
variables {A : Type} [s : monoid A]
variable a : A
include s
theorem mul_right_id : a * 1 = a := !monoid.right_id
theorem mul_left_id : 1 * a = a := !monoid.left_id
end
structure comm_monoid [class] (A : Type) extends monoid A, comm_semigroup A
print prefix algebra.comm_monoid
structure Semigroup :=
mk :: (carrier : Type) (struct : semigroup carrier)
attribute Semigroup.carrier [coercion]
attribute Semigroup.struct [instance]
structure CommSemigroup :=
mk :: (carrier : Type) (struct : comm_semigroup carrier)
attribute CommSemigroup.carrier [coercion]
attribute CommSemigroup.struct [instance]
structure Monoid :=
mk :: (carrier : Type) (struct : monoid carrier)
attribute Monoid.carrier [coercion]
attribute Monoid.struct [instance]
structure CommMonoid :=
mk :: (carrier : Type) (struct : comm_monoid carrier)
attribute CommMonoid.carrier [coercion]
attribute CommMonoid.struct [instance]
end algebra
open algebra
section examples
theorem test1 {S : Semigroup} (a b c d : S) : a * (b * c) * d = a * b * (c * d) :=
calc
a * (b * c) * d = a * b * c * d : mul_assoc
... = a * b * (c * d) : mul_assoc
theorem test2 {M : CommSemigroup} (a b : M) : a * b = a * b := rfl
theorem test3 {M : Monoid} (a b c d : M) : a * (b * c) * d = a * b * (c * d) :=
calc
a * (b * c) * d = a * b * c * d : mul_assoc
... = a * b * (c * d) : mul_assoc
-- for test4b to work, we need instances at the level of the bundled structures as well
definition Monoid_Semigroup [coercion] [reducible] (M : Monoid) : Semigroup :=
Semigroup.mk (Monoid.carrier M) _
theorem test4 {M : Monoid} (a b c d : M) : a * (b * c) * d = a * b * (c * d) :=
test1 a b c d
theorem test5 {M : Monoid} (a b c : M) : a * 1 * b * c = a * (b * c) :=
calc
a * 1 * b * c = a * b * c : {!mul_right_id}
... = a * (b * c) : mul_assoc
theorem test5a {M : Monoid} (a b c : M) : a * 1 * b * c = a * (b * c) :=
calc
a * 1 * b * c = a * b * c : {!mul_right_id}
... = a * (b * c) : mul_assoc
theorem test5b {A : Type} {M : monoid A} (a b c : A) : a * 1 * b * c = a * (b * c) :=
calc
a * 1 * b * c = a * b * c : mul_right_id
... = a * (b * c) : mul_assoc
theorem test6 {M : CommMonoid} (a b c : M) : a * 1 * b * c = a * (b * c) :=
calc
a * 1 * b * c = a * b * c : mul_right_id
... = a * (b * c) : mul_assoc
end examples
|
5a042d74d6a282cffe02b3c4e6255d8e56883c53 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/shadow.lean | 06ad2b7328af015ffc77b0f7be47205305148c4f | [
"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 | 639 | lean | import Lean
theorem ex1 {α} (x : α) (h : x = x) (x : α) : x = x :=
h
set_option pp.sanitizeNames false in
theorem ex2 {α} (x : α) (h : x = x) (x : α) : x = x :=
h -- this error is confusing because we have disabled `pp.sanitizeNames`
def foo1 {α} [Inhabited α] (inst inst : α) : {β δ : Type} → α → β → δ → α × β × δ :=
_
set_option pp.sanitizeNames false in
def foo2 {α} [Inhabited α] (inst inst : α) : {β δ : Type} → α → β → δ → α × β × δ :=
_
def foo3 {α β} (inst : α) (inst : β) (b : β) (inst : α) [Inhabited α] : {β δ : Type} → α → β → δ → α × β × δ :=
_
|
0600e25b3ed5e6ed6035b5f600d2fe79ce8bd418 | 1b8f093752ba748c5ca0083afef2959aaa7dace5 | /src/category_theory/examples/groups/universal.lean | 710fd7efe14359afe3eac8944c97e6ca33a29585 | [] | no_license | khoek/lean-category-theory | 7ec4cda9cc64a5a4ffeb84712ac7d020dbbba386 | 63dcb598e9270a3e8b56d1769eb4f825a177cd95 | refs/heads/master | 1,585,251,725,759 | 1,539,344,445,000 | 1,539,344,445,000 | 145,281,070 | 0 | 0 | null | 1,534,662,376,000 | 1,534,662,376,000 | null | UTF-8 | Lean | false | false | 4,953 | lean | -- Copyright (c) 2017 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Scott Morrison
import category_theory.examples.groups
import category_theory.universal.strongly_concrete
import category_theory.limits
namespace category_theory.examples.groups
open category_theory
open category_theory.limits
universe u
instance : group punit :=
begin
refine {
one := punit.star,
mul := λ x y, punit.star,
..
},
tidy,
end
-- private meta def unfold_is_group_hom := `[unfold is_group_hom]
-- local attribute [tidy] unfold_is_group_hom
instance maps_to_punit_hom {Z : Type u} [group Z] (f : Z → punit) : is_group_hom f := by obviously
instance : has_terminal_object.{u+1 u} Group :=
{ terminal := ⟨ punit, by obviously ⟩ }
variables {α : Type u} [group α] {β : Type u} [group β] {γ : Type u} [group γ] {δ : Type u} [group δ]
-- instance group_binary_product : group (α × β) :=
-- begin
-- refine {
-- one := (1, 1),
-- mul := λ p q, (p.1 * q.1, p.2 * q.2),
-- inv := λ p, (p.1⁻¹, p.2⁻¹),
-- ..
-- },
-- tidy, -- PROJECT tidy is not (currently) the correct tool for this.
-- end
-- section
-- variables (f : α → β) [is_group_hom f] (g : γ → δ) [is_group_hom g]
-- def fun_prod : (α × γ) → (β × δ) := λ p, (f p.1, g p.2)
-- instance group_morphism_binary_product
-- : is_group_hom (fun_prod f g) := sorry
-- instance Groups_has_BinaryProducts : has_BinaryProducts Group := {
-- binary_product := λ s t, {
-- product := ⟨ s.1 × t.1, by apply_instance ⟩ ,
-- left_projection := ⟨ prod.fst ⟩,
-- right_projection := ⟨ prod.snd ⟩,
-- map := λ _ f g, {
-- map := λ x, (f.map x, g.map x)
-- },
-- uniqueness := λ _ f g w₁ w₂, begin
-- tidy,
-- have p := congr_arg semigroup_morphism.map w₁,
-- have p' := congr_fun p x,
-- exact p',
-- have q := congr_arg semigroup_morphism.map w₂,
-- have q' := congr_fun q x,
-- exact q',
-- end
-- }
-- }
-- end
-- def group_product {I : Type u} {f : I → Type v} (s : Π i : I, group (f i)) : group (Π i, f i) :=
-- begin
-- refine {
-- one := λ i, 1,
-- mul := λ p q i, (p i) * (q i),
-- inv := λ p i, (p i)⁻¹,
-- ..
-- },
-- sorry
-- end
-- def group_morphism_product
-- {I : Type u}
-- {f g : I → Type v}
-- {s : Π i : I, group (f i)} {t : Π i : I, group (g i)}
-- (f : Π i : I, semigroup_morphism (s i) (t i))
-- : semigroup_morphism (semigroup_product s) (semigroup_product t) := {
-- map := λ p i, (f i) (p i)
-- }
-- instance Groups_has_Products : has_Products Group := {
-- product := λ I F, {
-- product := ⟨ _, semigroup_product (λ i, (F i).2) ⟩,
-- projection := λ i, {map := λ f, f i},
-- map := λ _ f, {map := λ y j, (f j).map y},
-- uniqueness := λ _ f g w, begin
-- tidy,
-- have p := congr_arg semigroup_morphism.map (w x_1),
-- have p' := congr_fun p x,
-- exact p',
-- end
-- }
-- }
-- section
-- variables (f : α → β) [is_group_hom f] (g : α → β) [is_group_hom g]
-- def group_equalizer := {x : α // f x = g x}
-- instance : group (group_equalizer f g) :=
-- begin
-- refine {
-- one := ⟨ 1, sorry ⟩,
-- mul := λ p q, ⟨ (p.val) * (q.val), by obviously ⟩,
-- inv := λ p, ⟨ (p.val)⁻¹, sorry ⟩,
-- ..
-- },
-- sorry
-- end
-- instance Groups_has_Equalizers : has_Equalizers Group := {
-- equalizer := λ X Y f g, {
-- equalizer := ⟨ group_equalizer f.1 g.1, by apply_instance ⟩,
-- inclusion := {map := λ x, x.val},
-- map := λ _ h w, {map := λ x, ⟨ h.map x, begin
-- tidy,
-- have p := congr_arg semigroup_morphism.map w,
-- have p' := congr_fun p x,
-- exact p',
-- end ⟩},
-- uniqueness := λ r h k w, begin
-- tidy,
-- have p := congr_arg semigroup_morphism.map w,
-- have p' := congr_fun p x,
-- exact p'
-- end,
-- }
-- }
-- end
end category_theory.examples.groups
|
1bb3ed3af176d38cbdb5a86007ba9ab24a927524 | 6df8d5ae3acf20ad0d7f0247d2cee1957ef96df1 | /HW/hw8_intro_proofs.lean | a95f5a32c892d069880e42f579a794c3e08b96e7 | [] | no_license | derekjohnsonva/CS2102 | 8ed45daa6658e6121bac0f6691eac6147d08246d | b3f507d4be824a2511838a1054d04fc9aef3304c | refs/heads/master | 1,648,529,162,527 | 1,578,851,859,000 | 1,578,851,859,000 | 233,433,207 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,495 | lean | /-
CS 2102 F19, Homework #8, Predicate Logic & Proofs.
-/
namespace hw8
/-
#1. Equality and proofs of equality.
-/
/-
A. [10 points] Fill in the blank.
When we say that the binary equality relation,
=, on objects of any type, α, is reflexive, we
mean that for any value, a, of type, α, _______.
ANSWER:
a = a
-/
/-
B. [10 points]
Complete the following definition in Lean
to formalize the proposition that 1 equals
1.
-/
def one_eq_one : Prop :=
-- ANSWER
1 = 1
/-
C. [10 points]
Give an English language proof of the
proposition that 1 = 1 by completing the
following incomplete proof.
To obtain a proof of 1 = 1 we apply the ________
property of the ________ relation to the specific
value, _________.
ANSWER:
reflexive, equals, 1
-/
/-
D. [10 points]
Give a formal proof of this proposition
by completing the following definition.
-/
def proof_that_one_eq_one : one_eq_one :=
-- ANSWER
eq.refl 1
/- [10 points]
E. Complete the following test case to produce
an example that suggests (correctly) that Lean
will accept a proof that two *different terms* are
equal as long as they reduce to the same value.
-/
-- ANSWER: Give a proposition and proof in Lean
def two_plus_two_eq_four : Prop :=
(2+2)=4
def proof_that_two_plus_two_eq_four : two_plus_two_eq_four :=
eq.refl (2+2)
-- ANSWER Does eq.refl work as suggested? _____
--Yes, the refletive prop works the way I would expect it to
/-
#2. Predicates and Properties
A predicate is a parameterized proposition. By
applying a predicate to different arguments, we get
different propositions. Such a propopsition can
be said to be "about" the argument to which it was
applied.
We interpret such a proposition as asserting that
its argument has a *property* of interest. If the
proposition has a proof (is true), the object does
have the asserted property, and if the proposition
is not true, the object doesn't have that property.
In the following set of problems, we will take the
property of a natural number "being even" as a case
in point.
In natural language, we will say that a natural
number, n, is even if and only if (1) it is zero,
or, (2) it is two more than another even number.
Think about this inductive definition. Why does it
cover all possible cases -- an infinitude of cases?
(No need for an answer here.)
-/
/-
A. [10 points]
We have accepted as *axioms* in our definition
of evennness that zero is even and that any number
that is two more than another even number is even.
These are the only axioms you may use in giving a
proof that four is even. Give a natural language
proof now. Hint, start by saying the following:
Proof: To prove that four is even, *it will suffice*
to show that 2 is even, because if two is even then,
given that 4 is two more than two, if two is even
then so is four, by rule (2). Now all that remains
to be proved is that _______________.
Give the rest of your natural language proof here,
and be sure to indicate which of the two rules you
are applying at each step in your reasoning.
ANSWER:
All that remains to be proved is that 2 is even. This
can be done by showing that 0 is even and that 2 is
two more than zero. We know that 0 is even because it is
even rule 1. We know that two is even because it is two more
than zero (2)
-/
/-
B. [10 points]
We formalize a predicate, such as is_even, as a
family of "inductive propositions" given by a
function from argument values to propositions.
Such an inductive definition thus has the type,
α → Prop, where α is the type of argument to
which the predicate is applied.
Please see the first line of the definition of
is_even that follows for an example.
Having specified the *type* of a predicate, in
this case from ℕ → Prop, we then define the set
of constructors to define the logical rules that
can be used to construct proofs of any such
proposition.
These rules are the (formal) axioms that can be
used to construct proofs. The first one (below)
states that the term, pf_zero_is_even, is to be
accepted as a proof of is_even 0 (which is how
we write the application of a predicate to a value
to obtain a proposition, here that "0 is even").
The second constructor/axiom/rule provides a way
to build a proof of (is_even 2+n) by applying the
constructor to any n along with a proof that that
particular n is even. (Yes: the ∀ specifies the
first argument to pf_even_plus_two_is_even. This
is necessary to give the argument a name, here n,
so that that name can be used in defining the rest
of the constructor's type).
-/
inductive is_even : ℕ → Prop
| pf_zero_is_even : (is_even 0)
| pf_even_plus_two_is_even :
∀ (n : ℕ), is_even n → is_even (nat.succ (nat.succ n))
/-
Give formal proofs for each of the following
propositions. (Notice how we obtain different
propositions by applying the predicate, is_even,
to different argument values. This is what we
mean when we say that a predicate defines a
family of propositions.)
-/
open is_even
theorem zero_even : is_even 0 :=
-- ANSWER
pf_zero_is_even
/-
In this case, give a proof term without using
a begin/end proof script.
-/
theorem two_even : is_even 2 :=
-- ANSWER
pf_even_plus_two_is_even 0 zero_even
/-
In this case, give a proof using a begin/end
proof script.
-/
theorem eight_even : is_even 8 :=
begin
apply pf_even_plus_two_is_even,
apply pf_even_plus_two_is_even, -- replace this with answer
apply pf_even_plus_two_is_even,
exact two_even -- exact is the answer
end
/-
C. [10 points]
Formally specify a predicate, is_odd, on the
natural numbers. You can reason about any
natural number being odd using two rules, just
as for being even. Once you've defined your
predicate (an inductive family of proposition,
just like is_even), formally state and prove
the following propositions (however you wish).
- 1 is odd
- 7 is odd
-/
-- ANSWER
inductive is_odd : ℕ → Prop
| pf_one_is_odd : (is_odd (nat.succ 0))
| pf_odd_plus_two_is_odd :
∀ (n : ℕ), is_odd n → is_odd (nat.succ (nat.succ n))
open is_odd
-- ANSWER
theorem one_odd : is_odd 1 :=
pf_one_is_odd
-- ANSWER
theorem seven_odd : is_odd 7 :=
begin
apply pf_odd_plus_two_is_odd,
apply pf_odd_plus_two_is_odd,
apply pf_odd_plus_two_is_odd,
exact one_odd
end
/-
Introducing an important concept. In the preceding
problems, we've seen that we can think of a predicate
with one argument as defining a property objects, such
as the property of being even. Now we shift perspective
from the concept of a property, per se, to the concept
of "the set of objects that have a given property." The
set of objects that have the is_even property, for
example, could be written as
evens = {0, 2, 4, 6, 8, 10, ...}
or more formally as
evens = { n : ℕ | is_even n}
The elements of these sets are all, and only,
the values that "satisfy" the is_even predicate.
A value satisfies its predicate if, when plugged
in, the resulting proposition has a proof (and so
is true). The key conclusion is that a predicate
with a single argument defines a *set*, namely
the set of all and only those objects that have
that property.
-/
/-
#4. Predicates and binary relations
Mathematicians define *binary relations* as sets
of ordered pairs. For example, the equality relation
on natural numbers comprises the set of of all pairs
of natural numbers such that the first and second
elements are the same. We could write this set like
this:
equals = { (0,0), (1,1), (2,2), ...},
or like this:
equals = { (m : ℕ, n : ℕ) | m = n }
We formalize binary relations as predicates with
*two* arguments. The type of such a predicate in
Lean is thus α → β → Prop, where α and β are the
types of the arguments.
In our example, we have a two-place predicate that
defines the set of ordered pairs of natural numbers
where the two elements of each pair are co-equal.
Study and understand the following specification
of this binary relation. Look at the construtor,
mk, in particular: it says that you can construct
a proof that a pair of values, n and m, is in our
id_nat_relation if you have a proof of n = m. (In
other words, it suffices to show that n = m using
Lean's built in equality relation to construct a
proof that (n, m) is in our id_nat_relation.)
-/
inductive id_nat_relation : ℕ → ℕ → Prop
| mk : ∀ (n m : ℕ), n = m → id_nat_relation n m
| sy : ∀ (n m : ℕ), n = m ∧ m = n → id_nat_relation n m
| tr : ∀ (n m c : ℕ), (n = m ∧ m = c ) → n = c → id_nat_relation n m
/-
A. [10 points]
Give a formal proof that id_nat_relation contains
the pair, (3, 3). Do it by completing the following
proof. Think carefully about the third argument: you
need a *value* of what type here? What do we call a
value of a logical type?
-/
theorem three_three_in_id : id_nat_relation 3 3 :=
-- ANSWER (apply a constructor, of course)
id_nat_relation.mk 3 3 (eq.refl 3)
/-
B. [10 points]
Explain in just a few words why it is not
possible to prove that (3,5) is in this relation.
-/
-- ANSWER
-- 3 and 5 are not reflexive
/-
EXTRA CREDIT.
-/
/-
Here's a definition of what it means for a
relation to be reflexive.
-/
def reflexive {α : Type} (r : α → α → Prop) :=
∀ (a : α), r a a
/-
A. Formally state and prove that id_nat_relation
is reflexive. Hint: use a script and start it
with "assume (a : ℕ)". Remember that to prove a ∀
proposition, we *assume* that we're given some
arbitrary but specific value of the given type,
then we prove the rest of the proposition about
it. But because we didn't say anything about
the element we picked, we can conclude that the
statement must be true of any element of the type.
-/
-- ANSWER
theorem id_nat_refl : reflexive id_nat_relation :=
begin
assume (a : ℕ),
apply id_nat_relation.mk a a,
exact eq.refl a
end
/-
B. [Double extra credit.]
Formally define what we mean by a relation being
symmetric and transitive, in the style of the above
definition of reflexive, and formally state and show
that our id_nat_reflexive relation is also symmetric
and transitive.
-/
def symmetric {α : Type} (r : α → α → Prop) :=
∀ (a b : α), r a b ↔ r b a
def transitive {α : Type} (r : α → α → Prop) :=
∀ (a b c: α), (r a b ∧ r b c) → r a c
theorem id_nat_symm : symmetric id_nat_relation :=
begin
assume (a : ℕ),
apply id_nat_relation.sy
end
end hw8 |
ddce061dbce6103ada3324e5a751f07b33407c91 | 3aad12fe82645d2d3173fbedc2e5c2ba945a4d75 | /src/category/liftable.lean | 1d6c47384aa825d60cecc49edbe81ef5ce976268 | [] | no_license | seanpm2001/LeanProver-Community_MathLIB-Nursery | 4f88d539cb18d73a94af983092896b851e6640b5 | 0479b31fa5b4d39f41e89b8584c9f5bf5271e8ec | refs/heads/master | 1,688,730,786,645 | 1,572,070,026,000 | 1,572,070,026,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,526 | lean |
import data.equiv.basic
import tactic
universes u₀ v₀ u₁ v₁ v₂ w w₀ w₁
class liftable (α : Type u₀) (β : (Type v₀)) :=
(lift {} : α ≃ β)
-- instance {α} : liftable α (ulift α) :=
-- ⟨ equiv.ulift.symm ⟩
instance ulift.liftable.ulift {α : Type w} : liftable (ulift.{u₀} α) (ulift.{u₁} α) :=
⟨ equiv.trans equiv.ulift equiv.ulift.symm ⟩
class liftable1 (f : (Type u₀ → Type u₁)) (g : Type v₀ → Type v₁) :=
(up : Π {α β}, α ≃ β → f α → g β)
(down : Π {α β}, α ≃ β → g β → f α)
(up_down : ∀ {α β} (F : α ≃ β) (x : g β), up F (down F x) = x)
(down_up : ∀ {α β} (F : α ≃ β) (x : f α), down F (up F x) = x)
attribute [simp] liftable1.up_down liftable1.down_up
namespace liftable
@[reducible]
def up' {f : Type v₀ → Type v₁} (g : Type (max u₀ v₀) → Type u₁) [liftable1 f g]
{α} : f α → g (ulift.{u₀} α) :=
liftable1.up g equiv.ulift.symm
@[reducible]
def down' {f : Type u₀ → Type u₁} {g : Type (max u₀ v₀) → Type v₁} [liftable1 f g]
{α} : g (ulift α) → f α :=
liftable1.down _ equiv.ulift.symm
@[reducible]
def shift' {f : Type (max w u₀) → Type u₁} {g : Type (max w v₀) → Type v₁} [liftable1 f g]
{α : Type w} : g (ulift α) → f (ulift α) :=
liftable1.down _ $ equiv.trans equiv.ulift equiv.ulift.symm
end liftable
open ulift
protected lemma prod.map_map {α α' α'' β β' β''}
(x : α × β) (f : α → α') (f' : α' → α'')
(g : β → β') (g' : β' → β'') :
prod.map f' g' (prod.map f g x) = prod.map (f' ∘ f) (g' ∘ g) x :=
by cases x; refl
protected lemma prod.id_map {α β}
(x : α × β) :
prod.map (λ x, x) (λ x, x) x = x :=
by cases x; refl
instance : liftable1 id id :=
{ up := λ _ _ F, F
, down := λ _ _ F, F.symm
, up_down := by intros; simp
, down_up := by intros; simp }
def state_t.liftable' {s : Type u₀} {s' : Type u₁} {m m'} [functor m'] [functor m] [is_lawful_functor m'] [is_lawful_functor m]
[liftable1 m m']
(F : s ≃ s') :
liftable1 (state_t s m) (state_t s' m') :=
{ up := λ _ _ G ⟨ f ⟩, ⟨ λ s, liftable1.up _ (equiv.prod_congr G F) (f $ F.symm s) ⟩
, down := λ _ _ G ⟨ g ⟩, ⟨ λ s, liftable1.down _ (equiv.prod_congr G F) $ g (F s) ⟩
, up_down := by { rintros α β G ⟨ f ⟩, simp! }
, down_up := by { rintros α β G ⟨ g ⟩, simp! [map_map,function.comp] } }
instance {s : Type u₀} {s' : Type u₁} {m m'} [functor m'] [functor m] [is_lawful_functor m'] [is_lawful_functor m]
[liftable1 m m'] [liftable s s'] :
liftable1 (state_t s m) (state_t s' m') :=
state_t.liftable' liftable.lift
def reader_t.liftable' {s : Type u₀} {s' : Type u₁} {m : Type u₀ → Type v₀} {m' : Type u₁ → Type v₁}
[liftable1 m m']
(F : s ≃ s') :
liftable1 (reader_t s m) (reader_t s' m') :=
{ up := λ _ _ G ⟨ f ⟩, ⟨ λ s, liftable1.up _ G (f $ F.symm s) ⟩
, down := λ _ _ G ⟨ g ⟩, ⟨ λ s, liftable1.down _ G $ g $ F s ⟩
, up_down := by { rintros α β G ⟨ f ⟩, simp! }
, down_up := by { rintros α β G ⟨ g ⟩, simp! [map_map,function.comp] } }
instance {s : Type u₀} {s' : Type u₁} {m : Type u₀ → Type v₀} {m' : Type u₁ → Type v₁}
[liftable1 m m'] [liftable s s'] :
liftable1 (reader_t s m) (reader_t s' m') :=
reader_t.liftable' liftable.lift
-- namespace liftable
-- variables {f : Type (max u₀ w) → Type u₁} {g : Type (max (max u₀ w) v₀) → Type v₁}
-- variables [liftable f g] [functor g]
-- variable {α : Type w}
-- open functor
-- def up' (x : f (ulift.{u₀} α)) : g (ulift.{max u₀ v₀} α) :=
-- map (ulift.up ∘ ulift.down ∘ ulift.down) $ up x
-- def down' (x : g (ulift.{max u₀ v₀} α)) : f (ulift.{u₀} α) :=
-- down $ map (ulift.up ∘ ulift.up ∘ ulift.down) x
-- variables [is_lawful_functor g]
-- lemma up_down' : ∀ (x : g (ulift.{max u₀ v₀} α)), up' (down' x : f (ulift.{u₀} α)) = x :=
-- by intros; simp [up',down',map_map,function.comp,ulift.up_down]
-- lemma down_up' : ∀ (x : f (ulift.{u₀} α)), down' (up' x : g (ulift.{max u₀ v₀} α)) = x :=
-- by intros; simp [up',down',map_map,function.comp,ulift.up_down]
-- end liftable
section trans
open liftable
-- def liftable.trans
-- {f : Type u₀ → Type u₁}
-- {g : Type v₀ → Type v₁}
-- {h : Type w₀ → Type w₁}
-- [functor h] [is_lawful_functor h]
-- (_ : liftable f g)
-- (_ : liftable g h) :
-- liftable f h :=
-- by refine
-- { up := λ α β G, (up' : g (ulift α) → h (ulift.{max v₀ w} α)) ∘ (up : f α → g (ulift α)),
-- down := λ α β G, (down : g (ulift α) → f α) ∘ (down' : h (ulift.{max v₀ w} α) → g (ulift α)) ,
-- .. };
-- intros; simp [function.comp,up_down',down_up']
end trans
instance : liftable1 option.{u₀} option.{v₁} :=
{ up := λ α β eq, @option.rec _ (λ _, option β) (@none β) (λ x, some $ eq x)
, down := λ α β eq, @option.rec _ (λ _, option α) (@none α) (λ x, some $ eq.symm x)
, up_down := by intros; cases x; simp
, down_up := by intros; cases x; simp }
namespace pliftable
@[reducible]
def up' {f : Type v₀ → Type v₁} (g : Type u₀ → Type u₁) [liftable1 f g] :
f punit → g punit :=
liftable1.up g equiv.punit_equiv_punit
@[reducible]
def down' {f : Type u₀ → Type u₁} {g : Type v₀ → Type v₁} [liftable1 f g] :
g punit → f punit :=
liftable1.down _ equiv.punit_equiv_punit
end pliftable
|
203aa8a46237f34f1e46b0ec954c8324dc0cba64 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/class5.lean | 187fe022ec2b749f01a24294c23eb5716176fdbe | [
"Apache-2.0"
] | permissive | soonhokong/lean | cb8aa01055ffe2af0fb99a16b4cda8463b882cd1 | 38607e3eb57f57f77c0ac114ad169e9e4262e24f | refs/heads/master | 1,611,187,284,081 | 1,450,766,737,000 | 1,476,122,547,000 | 11,513,992 | 2 | 0 | null | 1,401,763,102,000 | 1,374,182,235,000 | C++ | UTF-8 | Lean | false | false | 1,357 | lean | import logic
namespace experiment
namespace algebra
inductive mul_struct [class] (A : Type) : Type :=
mk : (A → A → A) → mul_struct A
definition mul {A : Type} [s : mul_struct A] (a b : A)
:= mul_struct.rec (λ f, f) s a b
infixl `*` := mul
end algebra
open algebra
namespace nat
inductive nat : Type :=
| zero : nat
| succ : nat → nat
constant mul : nat → nat → nat
constant add : nat → nat → nat
definition mul_struct [instance] : algebra.mul_struct nat
:= algebra.mul_struct.mk mul
end nat
section
open algebra nat
variables a b c : nat
check a * b * c
definition tst1 : nat := a * b * c
end
section
open [notation] algebra
open nat
-- check mul_struct nat << This is an error, we opened only the notation from algebra
variables a b c : nat
check a * b * c
definition tst2 : nat := a * b * c
end
section
open nat
-- check mul_struct nat << This is an error, we opened only the notation from algebra
variables a b c : nat
check a*b*c
definition tst3 : nat := a*b*c
end
section
open nat
set_option pp.implicit true
definition add_struct [instance] : algebra.mul_struct nat
:= algebra.mul_struct.mk add
variables a b c : nat
check #experiment.algebra a*b*c -- << is open add instead of mul
definition tst4 : nat := #experiment.algebra a*b*c
end
end experiment
|
a3956651e1d0dfd38047b605ce1b1d9ae783f592 | 38ee9024fb5974f555fb578fcf5a5a7b71e669b5 | /test/SolveByElim.lean | ed8f30beb5692f9538fc26c4e934ed49a9446890 | [
"Apache-2.0"
] | permissive | denayd/mathlib4 | 750e0dcd106554640a1ac701e51517501a574715 | 7f40a5c514066801ab3c6d431e9f405baa9b9c58 | refs/heads/master | 1,693,743,991,894 | 1,636,618,048,000 | 1,636,618,048,000 | 373,926,241 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 673 | lean | /-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Scott Morrison
-/
import Mathlib.Tactic.SolveByElim
def test1 (h : Nat) : Nat := by solveByElim
def test2 {α β : Type} (f : α → β) (a : α) : β := by solveByElim
def test3 {α β : Type} (f : α → α → β) (a : α) : β := by solveByElim
def test4 {α β γ : Type} (f : α → β) (g : β → γ) (a : α) : γ := by solveByElim
def test5 {α β γ : Type} (f : α → β) (g : β → γ) (b : β) : γ := by solveByElim
def test6 {α : Nat → Type} (f : (n : Nat) → α n → α (n+1)) (a : α 0) : α 5 := by solveByElim
|
aba6911be95aeaac639dc9e16802d777fd8f7e11 | 07c6143268cfb72beccd1cc35735d424ebcb187b | /src/analysis/complex/exponential.lean | 0d3f55a17ba9e64cd085e7c509899054708c5451 | [
"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 | 103,853 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne
-/
import tactic.linarith data.complex.exponential analysis.specific_limits
group_theory.quotient_group analysis.complex.basic
/-!
# Exponential
## Main definitions
This file contains the following definitions:
* π, arcsin, arccos, arctan
* argument of a complex number
* logarithm on real and complex numbers
* complex and real power function
## Main statements
The following functions are shown to be continuous:
* complex and real exponential function
* sin, cos, tan, sinh, cosh
* logarithm on real numbers
* real power function
* square root function
The following functions are shown to be differentiable, and their derivatives are computed:
* complex and real exponential function
* sin, cos, sinh, cosh
## Tags
exp, log, sin, cos, tan, arcsin, arccos, arctan, angle, argument, power, square root,
-/
noncomputable theory
open finset filter metric asymptotics
open_locale classical
open_locale topological_space
namespace complex
/-- The complex exponential is everywhere differentiable, with the derivative `exp x`. -/
lemma has_deriv_at_exp (x : ℂ) : has_deriv_at exp (exp x) x :=
begin
rw has_deriv_at_iff_is_o_nhds_zero,
have : (1 : ℕ) < 2 := by norm_num,
refine (is_O.of_bound (∥exp x∥) _).trans_is_o (is_o_pow_id this),
have : metric.ball (0 : ℂ) 1 ∈ nhds (0 : ℂ) := metric.ball_mem_nhds 0 zero_lt_one,
apply filter.mem_sets_of_superset this (λz hz, _),
simp only [metric.mem_ball, dist_zero_right] at hz,
simp only [exp_zero, mul_one, one_mul, add_comm, normed_field.norm_pow,
zero_add, set.mem_set_of_eq],
calc ∥exp (x + z) - exp x - z * exp x∥
= ∥exp x * (exp z - 1 - z)∥ : by { congr, rw [exp_add], ring }
... = ∥exp x∥ * ∥exp z - 1 - z∥ : normed_field.norm_mul _ _
... ≤ ∥exp x∥ * ∥z∥^2 :
mul_le_mul_of_nonneg_left (abs_exp_sub_one_sub_id_le (le_of_lt hz)) (norm_nonneg _)
end
lemma differentiable_exp : differentiable ℂ exp :=
λx, (has_deriv_at_exp x).differentiable_at
lemma differentiable_at_exp {x : ℂ} : differentiable_at ℂ exp x :=
differentiable_exp x
@[simp] lemma deriv_exp : deriv exp = exp :=
funext $ λ x, (has_deriv_at_exp x).deriv
@[simp] lemma iter_deriv_exp : ∀ n : ℕ, (deriv^[n] exp) = exp
| 0 := rfl
| (n+1) := by rw [nat.iterate_succ, deriv_exp, iter_deriv_exp n]
lemma continuous_exp : continuous exp :=
differentiable_exp.continuous
end complex
section
variables {f : ℂ → ℂ} {f' x : ℂ} {s : set ℂ}
lemma has_deriv_at.cexp (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') x :=
(complex.has_deriv_at_exp (f x)).comp x hf
lemma has_deriv_within_at.cexp (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') s x :=
(complex.has_deriv_at_exp (f x)).comp_has_deriv_within_at x hf
lemma differentiable_within_at.cexp (hf : differentiable_within_at ℂ f s x) :
differentiable_within_at ℂ (λ x, complex.exp (f x)) s x :=
hf.has_deriv_within_at.cexp.differentiable_within_at
@[simp] lemma differentiable_at.cexp (hc : differentiable_at ℂ f x) :
differentiable_at ℂ (λx, complex.exp (f x)) x :=
hc.has_deriv_at.cexp.differentiable_at
lemma differentiable_on.cexp (hc : differentiable_on ℂ f s) :
differentiable_on ℂ (λx, complex.exp (f x)) s :=
λx h, (hc x h).cexp
@[simp] lemma differentiable.cexp (hc : differentiable ℂ f) :
differentiable ℂ (λx, complex.exp (f x)) :=
λx, (hc x).cexp
lemma deriv_within_cexp (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
deriv_within (λx, complex.exp (f x)) s x = complex.exp (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.cexp.deriv_within hxs
@[simp] lemma deriv_cexp (hc : differentiable_at ℂ f x) :
deriv (λx, complex.exp (f x)) x = complex.exp (f x) * (deriv f x) :=
hc.has_deriv_at.cexp.deriv
end
namespace complex
/-- The complex sine function is everywhere differentiable, with the derivative `cos x`. -/
lemma has_deriv_at_sin (x : ℂ) : has_deriv_at sin (cos x) x :=
begin
simp only [cos, div_eq_mul_inv],
convert ((((has_deriv_at_id x).neg.mul_const I).cexp.sub
((has_deriv_at_id x).mul_const I).cexp).mul_const I).mul_const (2:ℂ)⁻¹,
simp only [function.comp, id],
rw [sub_mul, mul_assoc, mul_assoc, I_mul_I, neg_one_mul, neg_neg, mul_one, one_mul, mul_assoc,
I_mul_I, mul_neg_one, sub_neg_eq_add, add_comm]
end
lemma differentiable_sin : differentiable ℂ sin :=
λx, (has_deriv_at_sin x).differentiable_at
lemma differentiable_at_sin {x : ℂ} : differentiable_at ℂ sin x :=
differentiable_sin x
@[simp] lemma deriv_sin : deriv sin = cos :=
funext $ λ x, (has_deriv_at_sin x).deriv
lemma continuous_sin : continuous sin :=
differentiable_sin.continuous
/-- The complex cosine function is everywhere differentiable, with the derivative `-sin x`. -/
lemma has_deriv_at_cos (x : ℂ) : has_deriv_at cos (-sin x) x :=
begin
simp only [sin, div_eq_mul_inv, neg_mul_eq_neg_mul],
convert (((has_deriv_at_id x).mul_const I).cexp.add
((has_deriv_at_id x).neg.mul_const I).cexp).mul_const (2:ℂ)⁻¹,
simp only [function.comp, id],
ring
end
lemma differentiable_cos : differentiable ℂ cos :=
λx, (has_deriv_at_cos x).differentiable_at
lemma differentiable_at_cos {x : ℂ} : differentiable_at ℂ cos x :=
differentiable_cos x
lemma deriv_cos {x : ℂ} : deriv cos x = -sin x :=
(has_deriv_at_cos x).deriv
@[simp] lemma deriv_cos' : deriv cos = (λ x, -sin x) :=
funext $ λ x, deriv_cos
lemma continuous_cos : continuous cos :=
differentiable_cos.continuous
lemma continuous_tan : continuous (λ x : {x // cos x ≠ 0}, tan x) :=
(continuous_sin.comp continuous_subtype_val).mul
(continuous.inv subtype.property (continuous_cos.comp continuous_subtype_val))
/-- The complex hyperbolic sine function is everywhere differentiable, with the derivative `sinh x`. -/
lemma has_deriv_at_sinh (x : ℂ) : has_deriv_at sinh (cosh x) x :=
begin
simp only [cosh, div_eq_mul_inv],
convert ((has_deriv_at_exp x).sub (has_deriv_at_id x).neg.cexp).mul_const (2:ℂ)⁻¹,
rw [id, mul_neg_one, neg_neg]
end
lemma differentiable_sinh : differentiable ℂ sinh :=
λx, (has_deriv_at_sinh x).differentiable_at
lemma differentiable_at_sinh {x : ℂ} : differentiable_at ℂ sinh x :=
differentiable_sinh x
@[simp] lemma deriv_sinh : deriv sinh = cosh :=
funext $ λ x, (has_deriv_at_sinh x).deriv
lemma continuous_sinh : continuous sinh :=
differentiable_sinh.continuous
/-- The complex hyperbolic cosine function is everywhere differentiable, with the derivative `cosh x`. -/
lemma has_deriv_at_cosh (x : ℂ) : has_deriv_at cosh (sinh x) x :=
begin
simp only [sinh, div_eq_mul_inv],
convert ((has_deriv_at_exp x).add (has_deriv_at_id x).neg.cexp).mul_const (2:ℂ)⁻¹,
rw [id, mul_neg_one, sub_eq_add_neg]
end
lemma differentiable_cosh : differentiable ℂ cosh :=
λx, (has_deriv_at_cosh x).differentiable_at
lemma differentiable_at_cosh {x : ℂ} : differentiable_at ℂ cos x :=
differentiable_cos x
@[simp] lemma deriv_cosh : deriv cosh = sinh :=
funext $ λ x, (has_deriv_at_cosh x).deriv
lemma continuous_cosh : continuous cosh :=
differentiable_cosh.continuous
end complex
section
/-! Register lemmas for the derivatives of the composition of `complex.cos`, `complex.sin`,
`complex.cosh` and `complex.sinh` with a differentiable function, for standalone use and use with
`simp`. -/
variables {f : ℂ → ℂ} {f' x : ℂ} {s : set ℂ}
/-! `complex.cos`-/
lemma has_deriv_at.ccos (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, complex.cos (f x)) (- complex.sin (f x) * f') x :=
(complex.has_deriv_at_cos (f x)).comp x hf
lemma has_deriv_within_at.ccos (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, complex.cos (f x)) (- complex.sin (f x) * f') s x :=
(complex.has_deriv_at_cos (f x)).comp_has_deriv_within_at x hf
lemma differentiable_within_at.ccos (hf : differentiable_within_at ℂ f s x) :
differentiable_within_at ℂ (λ x, complex.cos (f x)) s x :=
hf.has_deriv_within_at.ccos.differentiable_within_at
@[simp] lemma differentiable_at.ccos (hc : differentiable_at ℂ f x) :
differentiable_at ℂ (λx, complex.cos (f x)) x :=
hc.has_deriv_at.ccos.differentiable_at
lemma differentiable_on.ccos (hc : differentiable_on ℂ f s) :
differentiable_on ℂ (λx, complex.cos (f x)) s :=
λx h, (hc x h).ccos
@[simp] lemma differentiable.ccos (hc : differentiable ℂ f) :
differentiable ℂ (λx, complex.cos (f x)) :=
λx, (hc x).ccos
lemma deriv_within_ccos (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
deriv_within (λx, complex.cos (f x)) s x = - complex.sin (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.ccos.deriv_within hxs
@[simp] lemma deriv_ccos (hc : differentiable_at ℂ f x) :
deriv (λx, complex.cos (f x)) x = - complex.sin (f x) * (deriv f x) :=
hc.has_deriv_at.ccos.deriv
/-! `complex.sin`-/
lemma has_deriv_at.csin (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, complex.sin (f x)) (complex.cos (f x) * f') x :=
(complex.has_deriv_at_sin (f x)).comp x hf
lemma has_deriv_within_at.csin (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, complex.sin (f x)) (complex.cos (f x) * f') s x :=
(complex.has_deriv_at_sin (f x)).comp_has_deriv_within_at x hf
lemma differentiable_within_at.csin (hf : differentiable_within_at ℂ f s x) :
differentiable_within_at ℂ (λ x, complex.sin (f x)) s x :=
hf.has_deriv_within_at.csin.differentiable_within_at
@[simp] lemma differentiable_at.csin (hc : differentiable_at ℂ f x) :
differentiable_at ℂ (λx, complex.sin (f x)) x :=
hc.has_deriv_at.csin.differentiable_at
lemma differentiable_on.csin (hc : differentiable_on ℂ f s) :
differentiable_on ℂ (λx, complex.sin (f x)) s :=
λx h, (hc x h).csin
@[simp] lemma differentiable.csin (hc : differentiable ℂ f) :
differentiable ℂ (λx, complex.sin (f x)) :=
λx, (hc x).csin
lemma deriv_within_csin (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
deriv_within (λx, complex.sin (f x)) s x = complex.cos (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.csin.deriv_within hxs
@[simp] lemma deriv_csin (hc : differentiable_at ℂ f x) :
deriv (λx, complex.sin (f x)) x = complex.cos (f x) * (deriv f x) :=
hc.has_deriv_at.csin.deriv
/-! `complex.cosh`-/
lemma has_deriv_at.ccosh (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, complex.cosh (f x)) (complex.sinh (f x) * f') x :=
(complex.has_deriv_at_cosh (f x)).comp x hf
lemma has_deriv_within_at.ccosh (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, complex.cosh (f x)) (complex.sinh (f x) * f') s x :=
(complex.has_deriv_at_cosh (f x)).comp_has_deriv_within_at x hf
lemma differentiable_within_at.ccosh (hf : differentiable_within_at ℂ f s x) :
differentiable_within_at ℂ (λ x, complex.cosh (f x)) s x :=
hf.has_deriv_within_at.ccosh.differentiable_within_at
@[simp] lemma differentiable_at.ccosh (hc : differentiable_at ℂ f x) :
differentiable_at ℂ (λx, complex.cosh (f x)) x :=
hc.has_deriv_at.ccosh.differentiable_at
lemma differentiable_on.ccosh (hc : differentiable_on ℂ f s) :
differentiable_on ℂ (λx, complex.cosh (f x)) s :=
λx h, (hc x h).ccosh
@[simp] lemma differentiable.ccosh (hc : differentiable ℂ f) :
differentiable ℂ (λx, complex.cosh (f x)) :=
λx, (hc x).ccosh
lemma deriv_within_ccosh (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
deriv_within (λx, complex.cosh (f x)) s x = complex.sinh (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.ccosh.deriv_within hxs
@[simp] lemma deriv_ccosh (hc : differentiable_at ℂ f x) :
deriv (λx, complex.cosh (f x)) x = complex.sinh (f x) * (deriv f x) :=
hc.has_deriv_at.ccosh.deriv
/-! `complex.sinh`-/
lemma has_deriv_at.csinh (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, complex.sinh (f x)) (complex.cosh (f x) * f') x :=
(complex.has_deriv_at_sinh (f x)).comp x hf
lemma has_deriv_within_at.csinh (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, complex.sinh (f x)) (complex.cosh (f x) * f') s x :=
(complex.has_deriv_at_sinh (f x)).comp_has_deriv_within_at x hf
lemma differentiable_within_at.csinh (hf : differentiable_within_at ℂ f s x) :
differentiable_within_at ℂ (λ x, complex.sinh (f x)) s x :=
hf.has_deriv_within_at.csinh.differentiable_within_at
@[simp] lemma differentiable_at.csinh (hc : differentiable_at ℂ f x) :
differentiable_at ℂ (λx, complex.sinh (f x)) x :=
hc.has_deriv_at.csinh.differentiable_at
lemma differentiable_on.csinh (hc : differentiable_on ℂ f s) :
differentiable_on ℂ (λx, complex.sinh (f x)) s :=
λx h, (hc x h).csinh
@[simp] lemma differentiable.csinh (hc : differentiable ℂ f) :
differentiable ℂ (λx, complex.sinh (f x)) :=
λx, (hc x).csinh
lemma deriv_within_csinh (hf : differentiable_within_at ℂ f s x)
(hxs : unique_diff_within_at ℂ s x) :
deriv_within (λx, complex.sinh (f x)) s x = complex.cosh (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.csinh.deriv_within hxs
@[simp] lemma deriv_csinh (hc : differentiable_at ℂ f x) :
deriv (λx, complex.sinh (f x)) x = complex.cosh (f x) * (deriv f x) :=
hc.has_deriv_at.csinh.deriv
end
namespace real
variables {x y z : ℝ}
lemma has_deriv_at_exp (x : ℝ) : has_deriv_at exp (exp x) x :=
has_deriv_at_real_of_complex (complex.has_deriv_at_exp x)
lemma differentiable_exp : differentiable ℝ exp :=
λx, (has_deriv_at_exp x).differentiable_at
lemma differentiable_at_exp : differentiable_at ℝ exp x :=
differentiable_exp x
@[simp] lemma deriv_exp : deriv exp = exp :=
funext $ λ x, (has_deriv_at_exp x).deriv
@[simp] lemma iter_deriv_exp : ∀ n : ℕ, (deriv^[n] exp) = exp
| 0 := rfl
| (n+1) := by rw [nat.iterate_succ, deriv_exp, iter_deriv_exp n]
lemma continuous_exp : continuous exp :=
differentiable_exp.continuous
lemma has_deriv_at_sin (x : ℝ) : has_deriv_at sin (cos x) x :=
has_deriv_at_real_of_complex (complex.has_deriv_at_sin x)
lemma differentiable_sin : differentiable ℝ sin :=
λx, (has_deriv_at_sin x).differentiable_at
lemma differentiable_at_sin : differentiable_at ℝ sin x :=
differentiable_sin x
@[simp] lemma deriv_sin : deriv sin = cos :=
funext $ λ x, (has_deriv_at_sin x).deriv
lemma continuous_sin : continuous sin :=
differentiable_sin.continuous
lemma has_deriv_at_cos (x : ℝ) : has_deriv_at cos (-sin x) x :=
(has_deriv_at_real_of_complex (complex.has_deriv_at_cos x) : _)
lemma differentiable_cos : differentiable ℝ cos :=
λx, (has_deriv_at_cos x).differentiable_at
lemma differentiable_at_cos : differentiable_at ℝ cos x :=
differentiable_cos x
lemma deriv_cos : deriv cos x = - sin x :=
(has_deriv_at_cos x).deriv
@[simp] lemma deriv_cos' : deriv cos = (λ x, - sin x) :=
funext $ λ _, deriv_cos
lemma continuous_cos : continuous cos :=
differentiable_cos.continuous
lemma continuous_tan : continuous (λ x : {x // cos x ≠ 0}, tan x) :=
by simp only [tan_eq_sin_div_cos]; exact
(continuous_sin.comp continuous_subtype_val).mul
(continuous.inv subtype.property
(continuous_cos.comp continuous_subtype_val))
lemma has_deriv_at_sinh (x : ℝ) : has_deriv_at sinh (cosh x) x :=
has_deriv_at_real_of_complex (complex.has_deriv_at_sinh x)
lemma differentiable_sinh : differentiable ℝ sinh :=
λx, (has_deriv_at_sinh x).differentiable_at
lemma differentiable_at_sinh : differentiable_at ℝ sinh x :=
differentiable_sinh x
@[simp] lemma deriv_sinh : deriv sinh = cosh :=
funext $ λ x, (has_deriv_at_sinh x).deriv
lemma continuous_sinh : continuous sinh :=
differentiable_sinh.continuous
lemma has_deriv_at_cosh (x : ℝ) : has_deriv_at cosh (sinh x) x :=
has_deriv_at_real_of_complex (complex.has_deriv_at_cosh x)
lemma differentiable_cosh : differentiable ℝ cosh :=
λx, (has_deriv_at_cosh x).differentiable_at
lemma differentiable_at_cosh : differentiable_at ℝ cosh x :=
differentiable_cosh x
@[simp] lemma deriv_cosh : deriv cosh = sinh :=
funext $ λ x, (has_deriv_at_cosh x).deriv
lemma continuous_cosh : continuous cosh :=
differentiable_cosh.continuous
end real
section
/-! Register lemmas for the derivatives of the composition of `real.exp`, `real.cos`, `real.sin`,
`real.cosh` and `real.sinh` with a differentiable function, for standalone use and use with
`simp`. -/
variables {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ}
/-! `real.exp`-/
lemma has_deriv_at.exp (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, real.exp (f x)) (real.exp (f x) * f') x :=
(real.has_deriv_at_exp (f x)).comp x hf
lemma has_deriv_within_at.exp (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, real.exp (f x)) (real.exp (f x) * f') s x :=
(real.has_deriv_at_exp (f x)).comp_has_deriv_within_at x hf
lemma differentiable_within_at.exp (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ x, real.exp (f x)) s x :=
hf.has_deriv_within_at.exp.differentiable_within_at
@[simp] lemma differentiable_at.exp (hc : differentiable_at ℝ f x) :
differentiable_at ℝ (λx, real.exp (f x)) x :=
hc.has_deriv_at.exp.differentiable_at
lemma differentiable_on.exp (hc : differentiable_on ℝ f s) :
differentiable_on ℝ (λx, real.exp (f x)) s :=
λx h, (hc x h).exp
@[simp] lemma differentiable.exp (hc : differentiable ℝ f) :
differentiable ℝ (λx, real.exp (f x)) :=
λx, (hc x).exp
lemma deriv_within_exp (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λx, real.exp (f x)) s x = real.exp (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.exp.deriv_within hxs
@[simp] lemma deriv_exp (hc : differentiable_at ℝ f x) :
deriv (λx, real.exp (f x)) x = real.exp (f x) * (deriv f x) :=
hc.has_deriv_at.exp.deriv
/-! `real.cos`-/
lemma has_deriv_at.cos (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, real.cos (f x)) (- real.sin (f x) * f') x :=
(real.has_deriv_at_cos (f x)).comp x hf
lemma has_deriv_within_at.cos (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, real.cos (f x)) (- real.sin (f x) * f') s x :=
(real.has_deriv_at_cos (f x)).comp_has_deriv_within_at x hf
lemma differentiable_within_at.cos (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ x, real.cos (f x)) s x :=
hf.has_deriv_within_at.cos.differentiable_within_at
@[simp] lemma differentiable_at.cos (hc : differentiable_at ℝ f x) :
differentiable_at ℝ (λx, real.cos (f x)) x :=
hc.has_deriv_at.cos.differentiable_at
lemma differentiable_on.cos (hc : differentiable_on ℝ f s) :
differentiable_on ℝ (λx, real.cos (f x)) s :=
λx h, (hc x h).cos
@[simp] lemma differentiable.cos (hc : differentiable ℝ f) :
differentiable ℝ (λx, real.cos (f x)) :=
λx, (hc x).cos
lemma deriv_within_cos (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λx, real.cos (f x)) s x = - real.sin (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.cos.deriv_within hxs
@[simp] lemma deriv_cos (hc : differentiable_at ℝ f x) :
deriv (λx, real.cos (f x)) x = - real.sin (f x) * (deriv f x) :=
hc.has_deriv_at.cos.deriv
/-! `real.sin`-/
lemma has_deriv_at.sin (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, real.sin (f x)) (real.cos (f x) * f') x :=
(real.has_deriv_at_sin (f x)).comp x hf
lemma has_deriv_within_at.sin (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, real.sin (f x)) (real.cos (f x) * f') s x :=
(real.has_deriv_at_sin (f x)).comp_has_deriv_within_at x hf
lemma differentiable_within_at.sin (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ x, real.sin (f x)) s x :=
hf.has_deriv_within_at.sin.differentiable_within_at
@[simp] lemma differentiable_at.sin (hc : differentiable_at ℝ f x) :
differentiable_at ℝ (λx, real.sin (f x)) x :=
hc.has_deriv_at.sin.differentiable_at
lemma differentiable_on.sin (hc : differentiable_on ℝ f s) :
differentiable_on ℝ (λx, real.sin (f x)) s :=
λx h, (hc x h).sin
@[simp] lemma differentiable.sin (hc : differentiable ℝ f) :
differentiable ℝ (λx, real.sin (f x)) :=
λx, (hc x).sin
lemma deriv_within_sin (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λx, real.sin (f x)) s x = real.cos (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.sin.deriv_within hxs
@[simp] lemma deriv_sin (hc : differentiable_at ℝ f x) :
deriv (λx, real.sin (f x)) x = real.cos (f x) * (deriv f x) :=
hc.has_deriv_at.sin.deriv
/-! `real.cosh`-/
lemma has_deriv_at.cosh (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, real.cosh (f x)) (real.sinh (f x) * f') x :=
(real.has_deriv_at_cosh (f x)).comp x hf
lemma has_deriv_within_at.cosh (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, real.cosh (f x)) (real.sinh (f x) * f') s x :=
(real.has_deriv_at_cosh (f x)).comp_has_deriv_within_at x hf
lemma differentiable_within_at.cosh (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ x, real.cosh (f x)) s x :=
hf.has_deriv_within_at.cosh.differentiable_within_at
@[simp] lemma differentiable_at.cosh (hc : differentiable_at ℝ f x) :
differentiable_at ℝ (λx, real.cosh (f x)) x :=
hc.has_deriv_at.cosh.differentiable_at
lemma differentiable_on.cosh (hc : differentiable_on ℝ f s) :
differentiable_on ℝ (λx, real.cosh (f x)) s :=
λx h, (hc x h).cosh
@[simp] lemma differentiable.cosh (hc : differentiable ℝ f) :
differentiable ℝ (λx, real.cosh (f x)) :=
λx, (hc x).cosh
lemma deriv_within_cosh (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λx, real.cosh (f x)) s x = real.sinh (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.cosh.deriv_within hxs
@[simp] lemma deriv_cosh (hc : differentiable_at ℝ f x) :
deriv (λx, real.cosh (f x)) x = real.sinh (f x) * (deriv f x) :=
hc.has_deriv_at.cosh.deriv
/-! `real.sinh`-/
lemma has_deriv_at.sinh (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, real.sinh (f x)) (real.cosh (f x) * f') x :=
(real.has_deriv_at_sinh (f x)).comp x hf
lemma has_deriv_within_at.sinh (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, real.sinh (f x)) (real.cosh (f x) * f') s x :=
(real.has_deriv_at_sinh (f x)).comp_has_deriv_within_at x hf
lemma differentiable_within_at.sinh (hf : differentiable_within_at ℝ f s x) :
differentiable_within_at ℝ (λ x, real.sinh (f x)) s x :=
hf.has_deriv_within_at.sinh.differentiable_within_at
@[simp] lemma differentiable_at.sinh (hc : differentiable_at ℝ f x) :
differentiable_at ℝ (λx, real.sinh (f x)) x :=
hc.has_deriv_at.sinh.differentiable_at
lemma differentiable_on.sinh (hc : differentiable_on ℝ f s) :
differentiable_on ℝ (λx, real.sinh (f x)) s :=
λx h, (hc x h).sinh
@[simp] lemma differentiable.sinh (hc : differentiable ℝ f) :
differentiable ℝ (λx, real.sinh (f x)) :=
λx, (hc x).sinh
lemma deriv_within_sinh (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λx, real.sinh (f x)) s x = real.cosh (f x) * (deriv_within f s x) :=
hf.has_deriv_within_at.sinh.deriv_within hxs
@[simp] lemma deriv_sinh (hc : differentiable_at ℝ f x) :
deriv (λx, real.sinh (f x)) x = real.cosh (f x) * (deriv f x) :=
hc.has_deriv_at.sinh.deriv
end
namespace real
variables {x y z : ℝ}
lemma exists_exp_eq_of_pos {x : ℝ} (hx : 0 < x) : ∃ y, exp y = x :=
have ∀ {z:ℝ}, 1 ≤ z → z ∈ set.range exp,
from λ z hz, intermediate_value_univ 0 (z - 1) continuous_exp
⟨by simpa, by simpa using add_one_le_exp_of_nonneg (sub_nonneg.2 hz)⟩,
match le_total x 1 with
| (or.inl hx1) := let ⟨y, hy⟩ := this (one_le_inv hx hx1) in
⟨-y, by rw [exp_neg, hy, inv_inv']⟩
| (or.inr hx1) := this hx1
end
/-- The real logarithm function, equal to `0` for `x ≤ 0` and to the inverse of the exponential
for `x > 0`. -/
noncomputable def log (x : ℝ) : ℝ :=
if hx : 0 < x then classical.some (exists_exp_eq_of_pos hx) else 0
lemma exp_log {x : ℝ} (hx : 0 < x) : exp (log x) = x :=
by rw [log, dif_pos hx]; exact classical.some_spec (exists_exp_eq_of_pos hx)
@[simp] lemma log_exp (x : ℝ) : log (exp x) = x :=
exp_injective $ exp_log (exp_pos x)
@[simp] lemma log_zero : log 0 = 0 :=
by simp [log, lt_irrefl]
@[simp] lemma log_one : log 1 = 0 :=
exp_injective $ by rw [exp_log zero_lt_one, exp_zero]
lemma log_mul {x y : ℝ} (hx : 0 < x) (hy : 0 < y) : log (x * y) = log x + log y :=
exp_injective $ by rw [exp_log (mul_pos hx hy), exp_add, exp_log hx, exp_log hy]
lemma log_le_log {x y : ℝ} (h : 0 < x) (h₁ : 0 < y) : real.log x ≤ real.log y ↔ x ≤ y :=
⟨λ h₂, by rwa [←real.exp_le_exp, real.exp_log h, real.exp_log h₁] at h₂, λ h₂,
(real.exp_le_exp).1 $ by rwa [real.exp_log h₁, real.exp_log h]⟩
lemma log_lt_log (hx : 0 < x) : x < y → log x < log y :=
by { intro h, rwa [← exp_lt_exp, exp_log hx, exp_log (lt_trans hx h)] }
lemma log_lt_log_iff (hx : 0 < x) (hy : 0 < y) : log x < log y ↔ x < y :=
by { rw [← exp_lt_exp, exp_log hx, exp_log hy] }
lemma log_pos_iff (x : ℝ) : 0 < log x ↔ 1 < x :=
begin
by_cases h : 0 < x,
{ rw ← log_one, exact log_lt_log_iff (by norm_num) h },
{ rw [log, dif_neg], split, repeat {intro, linarith} }
end
lemma log_pos : 1 < x → 0 < log x := (log_pos_iff x).2
lemma log_neg_iff (h : 0 < x) : log x < 0 ↔ x < 1 :=
by { rw ← log_one, exact log_lt_log_iff h (by norm_num) }
lemma log_neg (h0 : 0 < x) (h1 : x < 1) : log x < 0 := (log_neg_iff h0).2 h1
lemma log_nonneg : 1 ≤ x → 0 ≤ log x :=
by { intro, rwa [← log_one, log_le_log], norm_num, linarith }
lemma log_nonpos : x ≤ 1 → log x ≤ 0 :=
begin
intro, by_cases hx : 0 < x,
{ rwa [← log_one, log_le_log], exact hx, norm_num },
{ simp [log, dif_neg hx] }
end
section prove_log_is_continuous
lemma tendsto_log_one_zero : tendsto log (𝓝 1) (𝓝 0) :=
begin
rw tendsto_nhds_nhds, assume ε ε0,
let δ := min (exp ε - 1) (1 - exp (-ε)),
have : 0 < δ,
refine lt_min (sub_pos_of_lt (by rwa one_lt_exp_iff)) (sub_pos_of_lt _),
by { rw exp_lt_one_iff, linarith },
use [δ, this], assume x h,
cases le_total 1 x with hx hx,
{ have h : x < exp ε,
rw [dist_eq, abs_of_nonneg (sub_nonneg_of_le hx)] at h,
linarith [(min_le_left _ _ : δ ≤ exp ε - 1)],
calc abs (log x - 0) = abs (log x) : by simp
... = log x : abs_of_nonneg $ log_nonneg hx
... < ε : by { rwa [← exp_lt_exp, exp_log], linarith }},
{ have h : exp (-ε) < x,
rw [dist_eq, abs_of_nonpos (sub_nonpos_of_le hx)] at h,
linarith [(min_le_right _ _ : δ ≤ 1 - exp (-ε))],
have : 0 < x := lt_trans (exp_pos _) h,
calc abs (log x - 0) = abs (log x) : by simp
... = -log x : abs_of_nonpos $ log_nonpos hx
... < ε : by { rw [neg_lt, ← exp_lt_exp, exp_log], assumption' } }
end
lemma continuous_log' : continuous (λx : {x:ℝ // 0 < x}, log x.val) :=
continuous_iff_continuous_at.2 $ λ x,
begin
rw continuous_at,
let f₁ := λ h:{h:ℝ // 0 < h}, log (x.1 * h.1),
let f₂ := λ y:{y:ℝ // 0 < y}, subtype.mk (x.1 ⁻¹ * y.1) (mul_pos (inv_pos.2 x.2) y.2),
have H1 : tendsto f₁ (𝓝 ⟨1, zero_lt_one⟩) (𝓝 (log (x.1*1))),
have : f₁ = λ h:{h:ℝ // 0 < h}, log x.1 + log h.1,
ext h, rw ← log_mul x.2 h.2,
simp only [this, log_mul x.2 zero_lt_one, log_one],
exact tendsto_const_nhds.add (tendsto.comp tendsto_log_one_zero continuous_at_subtype_val),
have H2 : tendsto f₂ (𝓝 x) (𝓝 ⟨x.1⁻¹ * x.1, mul_pos (inv_pos.2 x.2) x.2⟩),
rw tendsto_subtype_rng, exact tendsto_const_nhds.mul continuous_at_subtype_val,
suffices h : tendsto (f₁ ∘ f₂) (𝓝 x) (𝓝 (log x.1)),
begin
convert h, ext y,
have : x.val * (x.val⁻¹ * y.val) = y.val,
rw [← mul_assoc, mul_inv_cancel (ne_of_gt x.2), one_mul],
show log (y.val) = log (x.val * (x.val⁻¹ * y.val)), rw this
end,
exact tendsto.comp (by rwa mul_one at H1)
(by { simp only [inv_mul_cancel (ne_of_gt x.2)] at H2, assumption })
end
lemma continuous_at_log (hx : 0 < x) : continuous_at log x :=
continuous_within_at.continuous_at (continuous_on_iff_continuous_restrict.2 continuous_log' _ hx)
(mem_nhds_sets (is_open_lt' _) hx)
/--
Three forms of the continuity of `real.log` is provided.
For the other two forms, see `real.continuous_log'` and `real.continuous_at_log`
-/
lemma continuous_log {α : Type*} [topological_space α] {f : α → ℝ} (h : ∀a, 0 < f a)
(hf : continuous f) : continuous (λa, log (f a)) :=
show continuous ((log ∘ @subtype.val ℝ (λr, 0 < r)) ∘ λa, ⟨f a, h a⟩),
from continuous_log'.comp (continuous_subtype_mk _ hf)
end prove_log_is_continuous
lemma exists_cos_eq_zero : 0 ∈ cos '' set.Icc (1:ℝ) 2 :=
intermediate_value_Icc' (by norm_num) continuous_cos.continuous_on
⟨le_of_lt cos_two_neg, le_of_lt cos_one_pos⟩
/-- The number π = 3.14159265... Defined here using choice as twice a zero of cos in [1,2], from
which one can derive all its properties. For explicit bounds on π, see `data.real.pi`. -/
noncomputable def pi : ℝ := 2 * classical.some exists_cos_eq_zero
localized "notation `π` := real.pi" in real
@[simp] lemma cos_pi_div_two : cos (π / 2) = 0 :=
by rw [pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)];
exact (classical.some_spec exists_cos_eq_zero).2
lemma one_le_pi_div_two : (1 : ℝ) ≤ π / 2 :=
by rw [pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)];
exact (classical.some_spec exists_cos_eq_zero).1.1
lemma pi_div_two_le_two : π / 2 ≤ 2 :=
by rw [pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)];
exact (classical.some_spec exists_cos_eq_zero).1.2
lemma two_le_pi : (2 : ℝ) ≤ π :=
(div_le_div_right (show (0 : ℝ) < 2, by norm_num)).1
(by rw div_self (@two_ne_zero' ℝ _ _ _); exact one_le_pi_div_two)
lemma pi_le_four : π ≤ 4 :=
(div_le_div_right (show (0 : ℝ) < 2, by norm_num)).1
(calc π / 2 ≤ 2 : pi_div_two_le_two
... = 4 / 2 : by norm_num)
lemma pi_pos : 0 < π :=
lt_of_lt_of_le (by norm_num) two_le_pi
lemma pi_div_two_pos : 0 < π / 2 :=
half_pos pi_pos
lemma two_pi_pos : 0 < 2 * π :=
by linarith [pi_pos]
@[simp] lemma sin_pi : sin π = 0 :=
by rw [← mul_div_cancel_left pi (@two_ne_zero ℝ _), two_mul, add_div,
sin_add, cos_pi_div_two]; simp
@[simp] lemma cos_pi : cos π = -1 :=
by rw [← mul_div_cancel_left pi (@two_ne_zero ℝ _), mul_div_assoc,
cos_two_mul, cos_pi_div_two];
simp [bit0, pow_add]
@[simp] lemma sin_two_pi : sin (2 * π) = 0 :=
by simp [two_mul, sin_add]
@[simp] lemma cos_two_pi : cos (2 * π) = 1 :=
by simp [two_mul, cos_add]
lemma sin_add_pi (x : ℝ) : sin (x + π) = -sin x :=
by simp [sin_add]
lemma sin_add_two_pi (x : ℝ) : sin (x + 2 * π) = sin x :=
by simp [sin_add_pi, sin_add, sin_two_pi, cos_two_pi]
lemma cos_add_two_pi (x : ℝ) : cos (x + 2 * π) = cos x :=
by simp [cos_add, cos_two_pi, sin_two_pi]
lemma sin_pi_sub (x : ℝ) : sin (π - x) = sin x :=
by simp [sub_eq_add_neg, sin_add]
lemma cos_add_pi (x : ℝ) : cos (x + π) = -cos x :=
by simp [cos_add]
lemma cos_pi_sub (x : ℝ) : cos (π - x) = -cos x :=
by simp [sub_eq_add_neg, cos_add]
lemma sin_pos_of_pos_of_lt_pi {x : ℝ} (h0x : 0 < x) (hxp : x < π) : 0 < sin x :=
if hx2 : x ≤ 2 then sin_pos_of_pos_of_le_two h0x hx2
else
have (2 : ℝ) + 2 = 4, from rfl,
have π - x ≤ 2, from sub_le_iff_le_add.2
(le_trans pi_le_four (this ▸ add_le_add_left (le_of_not_ge hx2) _)),
sin_pi_sub x ▸ sin_pos_of_pos_of_le_two (sub_pos.2 hxp) this
lemma sin_nonneg_of_nonneg_of_le_pi {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π) : 0 ≤ sin x :=
match lt_or_eq_of_le h0x with
| or.inl h0x := (lt_or_eq_of_le hxp).elim
(le_of_lt ∘ sin_pos_of_pos_of_lt_pi h0x)
(λ hpx, by simp [hpx])
| or.inr h0x := by simp [h0x.symm]
end
lemma sin_neg_of_neg_of_neg_pi_lt {x : ℝ} (hx0 : x < 0) (hpx : -π < x) : sin x < 0 :=
neg_pos.1 $ sin_neg x ▸ sin_pos_of_pos_of_lt_pi (neg_pos.2 hx0) (neg_lt.1 hpx)
lemma sin_nonpos_of_nonnpos_of_neg_pi_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -π ≤ x) : sin x ≤ 0 :=
neg_nonneg.1 $ sin_neg x ▸ sin_nonneg_of_nonneg_of_le_pi (neg_nonneg.2 hx0) (neg_le.1 hpx)
@[simp] lemma sin_pi_div_two : sin (π / 2) = 1 :=
have sin (π / 2) = 1 ∨ sin (π / 2) = -1 :=
by simpa [pow_two, mul_self_eq_one_iff] using sin_sq_add_cos_sq (π / 2),
this.resolve_right
(λ h, (show ¬(0 : ℝ) < -1, by norm_num) $
h ▸ sin_pos_of_pos_of_lt_pi pi_div_two_pos (half_lt_self pi_pos))
lemma sin_add_pi_div_two (x : ℝ) : sin (x + π / 2) = cos x :=
by simp [sin_add]
lemma sin_sub_pi_div_two (x : ℝ) : sin (x - π / 2) = -cos x :=
by simp [sub_eq_add_neg, sin_add]
lemma sin_pi_div_two_sub (x : ℝ) : sin (π / 2 - x) = cos x :=
by simp [sub_eq_add_neg, sin_add]
lemma cos_add_pi_div_two (x : ℝ) : cos (x + π / 2) = -sin x :=
by simp [cos_add]
lemma cos_sub_pi_div_two (x : ℝ) : cos (x - π / 2) = sin x :=
by simp [sub_eq_add_neg, cos_add]
lemma cos_pi_div_two_sub (x : ℝ) : cos (π / 2 - x) = sin x :=
by rw [← cos_neg, neg_sub, cos_sub_pi_div_two]
lemma cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two
{x : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2) : 0 < cos x :=
sin_add_pi_div_two x ▸ sin_pos_of_pos_of_lt_pi (by linarith) (by linarith)
lemma cos_nonneg_of_neg_pi_div_two_le_of_le_pi_div_two
{x : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) : 0 ≤ cos x :=
match lt_or_eq_of_le hx₁, lt_or_eq_of_le hx₂ with
| or.inl hx₁, or.inl hx₂ := le_of_lt (cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two hx₁ hx₂)
| or.inl hx₁, or.inr hx₂ := by simp [hx₂]
| or.inr hx₁, _ := by simp [hx₁.symm]
end
lemma cos_neg_of_pi_div_two_lt_of_lt {x : ℝ} (hx₁ : π / 2 < x) (hx₂ : x < π + π / 2) : cos x < 0 :=
neg_pos.1 $ cos_pi_sub x ▸
cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two (by linarith) (by linarith)
lemma cos_nonpos_of_pi_div_two_le_of_le {x : ℝ} (hx₁ : π / 2 ≤ x) (hx₂ : x ≤ π + π / 2) : cos x ≤ 0 :=
neg_nonneg.1 $ cos_pi_sub x ▸
cos_nonneg_of_neg_pi_div_two_le_of_le_pi_div_two (by linarith) (by linarith)
lemma sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 :=
by induction n; simp [add_mul, sin_add, *]
lemma sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 :=
by cases n; simp [add_mul, sin_add, *, sin_nat_mul_pi]
lemma cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 :=
by induction n; simp [*, mul_add, cos_add, add_mul, cos_two_pi, sin_two_pi]
lemma cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 :=
by cases n; simp only [cos_nat_mul_two_pi, int.of_nat_eq_coe,
int.neg_succ_of_nat_coe, int.cast_coe_nat, int.cast_neg,
(neg_mul_eq_neg_mul _ _).symm, cos_neg]
lemma cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 :=
by simp [cos_add, sin_add, cos_int_mul_two_pi]
lemma sin_eq_zero_iff_of_lt_of_lt {x : ℝ} (hx₁ : -π < x) (hx₂ : x < π) :
sin x = 0 ↔ x = 0 :=
⟨λ h, le_antisymm
(le_of_not_gt (λ h0, lt_irrefl (0 : ℝ) $
calc 0 < sin x : sin_pos_of_pos_of_lt_pi h0 hx₂
... = 0 : h))
(le_of_not_gt (λ h0, lt_irrefl (0 : ℝ) $
calc 0 = sin x : h.symm
... < 0 : sin_neg_of_neg_of_neg_pi_lt h0 hx₁)),
λ h, by simp [h]⟩
lemma sin_eq_zero_iff {x : ℝ} : sin x = 0 ↔ ∃ n : ℤ, (n : ℝ) * π = x :=
⟨λ h, ⟨⌊x / π⌋, le_antisymm (sub_nonneg.1 (sub_floor_div_mul_nonneg _ pi_pos))
(sub_nonpos.1 $ le_of_not_gt $ λ h₃, ne_of_lt (sin_pos_of_pos_of_lt_pi h₃ (sub_floor_div_mul_lt _ pi_pos))
(by simp [sub_eq_add_neg, sin_add, h, sin_int_mul_pi]))⟩,
λ ⟨n, hn⟩, hn ▸ sin_int_mul_pi _⟩
lemma sin_eq_zero_iff_cos_eq {x : ℝ} : sin x = 0 ↔ cos x = 1 ∨ cos x = -1 :=
by rw [← mul_self_eq_one_iff (cos x), ← sin_sq_add_cos_sq x,
pow_two, pow_two, ← sub_eq_iff_eq_add, sub_self];
exact ⟨λ h, by rw [h, mul_zero], eq_zero_of_mul_self_eq_zero ∘ eq.symm⟩
theorem sin_sub_sin (θ ψ : ℝ) : sin θ - sin ψ = 2 * sin((θ - ψ)/2) * cos((θ + ψ)/2) :=
begin
have s1 := sin_add ((θ + ψ) / 2) ((θ - ψ) / 2),
have s2 := sin_sub ((θ + ψ) / 2) ((θ - ψ) / 2),
rw [div_add_div_same, add_sub, add_right_comm, add_sub_cancel, add_self_div_two] at s1,
rw [div_sub_div_same, ←sub_add, add_sub_cancel', add_self_div_two] at s2,
rw [s1, s2, ←sub_add, add_sub_cancel', ← two_mul, ← mul_assoc, mul_right_comm]
end
lemma cos_eq_one_iff (x : ℝ) : cos x = 1 ↔ ∃ n : ℤ, (n : ℝ) * (2 * π) = x :=
⟨λ h, let ⟨n, hn⟩ := sin_eq_zero_iff.1 (sin_eq_zero_iff_cos_eq.2 (or.inl h)) in
⟨n / 2, (int.mod_two_eq_zero_or_one n).elim
(λ hn0, by rwa [← mul_assoc, ← @int.cast_two ℝ, ← int.cast_mul, int.div_mul_cancel
((int.dvd_iff_mod_eq_zero _ _).2 hn0)])
(λ hn1, by rw [← int.mod_add_div n 2, hn1, int.cast_add, int.cast_one, add_mul,
one_mul, add_comm, mul_comm (2 : ℤ), int.cast_mul, mul_assoc, int.cast_two] at hn;
rw [← hn, cos_int_mul_two_pi_add_pi] at h;
exact absurd h (by norm_num))⟩,
λ ⟨n, hn⟩, hn ▸ cos_int_mul_two_pi _⟩
theorem cos_eq_zero_iff {θ : ℝ} : cos θ = 0 ↔ ∃ k : ℤ, θ = (2 * k + 1) * pi / 2 :=
begin
rw [←real.sin_pi_div_two_sub, sin_eq_zero_iff],
split,
{ rintro ⟨n, hn⟩, existsi -n,
rw [int.cast_neg, add_mul, add_div, mul_assoc, mul_div_cancel_left _ (@two_ne_zero ℝ _),
one_mul, ←neg_mul_eq_neg_mul, hn, neg_sub, sub_add_cancel] },
{ rintro ⟨n, hn⟩, existsi -n,
rw [hn, add_mul, one_mul, add_div, mul_assoc, mul_div_cancel_left _ (@two_ne_zero ℝ _),
sub_add_eq_sub_sub_swap, sub_self, zero_sub, neg_mul_eq_neg_mul, int.cast_neg] }
end
lemma cos_eq_one_iff_of_lt_of_lt {x : ℝ} (hx₁ : -(2 * π) < x) (hx₂ : x < 2 * π) : cos x = 1 ↔ x = 0 :=
⟨λ h, let ⟨n, hn⟩ := (cos_eq_one_iff x).1 h in
begin
clear _let_match,
subst hn,
rw [mul_lt_iff_lt_one_left two_pi_pos, ← int.cast_one, int.cast_lt, ← int.le_sub_one_iff, sub_self] at hx₂,
rw [neg_lt, neg_mul_eq_neg_mul, mul_lt_iff_lt_one_left two_pi_pos, neg_lt,
← int.cast_one, ← int.cast_neg, int.cast_lt, ← int.add_one_le_iff, neg_add_self] at hx₁,
exact mul_eq_zero.2 (or.inl (int.cast_eq_zero.2 (le_antisymm hx₂ hx₁))),
end,
λ h, by simp [h]⟩
theorem cos_sub_cos (θ ψ : ℝ) : cos θ - cos ψ = -2 * sin((θ + ψ)/2) * sin((θ - ψ)/2) :=
by rw [← sin_pi_div_two_sub, ← sin_pi_div_two_sub, sin_sub_sin, sub_sub_sub_cancel_left,
add_sub, sub_add_eq_add_sub, add_halves, sub_sub, sub_div π, cos_pi_div_two_sub,
← neg_sub, neg_div, sin_neg, ← neg_mul_eq_mul_neg, neg_mul_eq_neg_mul, mul_right_comm]
lemma cos_lt_cos_of_nonneg_of_le_pi_div_two {x y : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π / 2)
(hy₁ : 0 ≤ y) (hy₂ : y ≤ π / 2) (hxy : x < y) : cos y < cos x :=
calc cos y = cos x * cos (y - x) - sin x * sin (y - x) :
by rw [← cos_add, add_sub_cancel'_right]
... < (cos x * 1) - sin x * sin (y - x) :
sub_lt_sub_right ((mul_lt_mul_left
(cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two (lt_of_lt_of_le (neg_neg_of_pos pi_div_two_pos) hx₁)
(lt_of_lt_of_le hxy hy₂))).2
(lt_of_le_of_ne (cos_le_one _) (mt (cos_eq_one_iff_of_lt_of_lt
(show -(2 * π) < y - x, by linarith) (show y - x < 2 * π, by linarith)).1
(sub_ne_zero.2 (ne_of_lt hxy).symm)))) _
... ≤ _ : by rw mul_one;
exact sub_le_self _ (mul_nonneg (sin_nonneg_of_nonneg_of_le_pi hx₁ (by linarith))
(sin_nonneg_of_nonneg_of_le_pi (by linarith) (by linarith)))
lemma cos_lt_cos_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π)
(hy₁ : 0 ≤ y) (hy₂ : y ≤ π) (hxy : x < y) : cos y < cos x :=
match (le_total x (π / 2) : x ≤ π / 2 ∨ π / 2 ≤ x), le_total y (π / 2) with
| or.inl hx, or.inl hy := cos_lt_cos_of_nonneg_of_le_pi_div_two hx₁ hx hy₁ hy hxy
| or.inl hx, or.inr hy := (lt_or_eq_of_le hx).elim
(λ hx, calc cos y ≤ 0 : cos_nonpos_of_pi_div_two_le_of_le hy (by linarith [pi_pos])
... < cos x : cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two (by linarith) hx)
(λ hx, calc cos y < 0 : cos_neg_of_pi_div_two_lt_of_lt (by linarith) (by linarith [pi_pos])
... = cos x : by rw [hx, cos_pi_div_two])
| or.inr hx, or.inl hy := by linarith
| or.inr hx, or.inr hy := neg_lt_neg_iff.1 (by rw [← cos_pi_sub, ← cos_pi_sub];
apply cos_lt_cos_of_nonneg_of_le_pi_div_two; linarith)
end
lemma cos_le_cos_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π)
(hy₁ : 0 ≤ y) (hy₂ : y ≤ π) (hxy : x ≤ y) : cos y ≤ cos x :=
(lt_or_eq_of_le hxy).elim
(le_of_lt ∘ cos_lt_cos_of_nonneg_of_le_pi hx₁ hx₂ hy₁ hy₂)
(λ h, h ▸ le_refl _)
lemma sin_lt_sin_of_le_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) (hy₁ : -(π / 2) ≤ y)
(hy₂ : y ≤ π / 2) (hxy : x < y) : sin x < sin y :=
by rw [← cos_sub_pi_div_two, ← cos_sub_pi_div_two, ← cos_neg (x - _), ← cos_neg (y - _)];
apply cos_lt_cos_of_nonneg_of_le_pi; linarith
lemma sin_le_sin_of_le_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) (hy₁ : -(π / 2) ≤ y)
(hy₂ : y ≤ π / 2) (hxy : x ≤ y) : sin x ≤ sin y :=
(lt_or_eq_of_le hxy).elim
(le_of_lt ∘ sin_lt_sin_of_le_of_le_pi_div_two hx₁ hx₂ hy₁ hy₂)
(λ h, h ▸ le_refl _)
lemma sin_inj_of_le_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) (hy₁ : -(π / 2) ≤ y)
(hy₂ : y ≤ π / 2) (hxy : sin x = sin y) : x = y :=
match lt_trichotomy x y with
| or.inl h := absurd (sin_lt_sin_of_le_of_le_pi_div_two hx₁ hx₂ hy₁ hy₂ h) (by rw hxy; exact lt_irrefl _)
| or.inr (or.inl h) := h
| or.inr (or.inr h) := absurd (sin_lt_sin_of_le_of_le_pi_div_two hy₁ hy₂ hx₁ hx₂ h) (by rw hxy; exact lt_irrefl _)
end
lemma cos_inj_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π) (hy₁ : 0 ≤ y) (hy₂ : y ≤ π)
(hxy : cos x = cos y) : x = y :=
begin
rw [← sin_pi_div_two_sub, ← sin_pi_div_two_sub] at hxy,
refine (sub_left_inj).1 (sin_inj_of_le_of_le_pi_div_two _ _ _ _ hxy);
linarith
end
lemma exists_sin_eq : set.Icc (-1:ℝ) 1 ⊆ sin '' set.Icc (-(π / 2)) (π / 2) :=
by convert intermediate_value_Icc
(le_trans (neg_nonpos.2 (le_of_lt pi_div_two_pos)) (le_of_lt pi_div_two_pos))
continuous_sin.continuous_on; simp only [sin_neg, sin_pi_div_two]
lemma sin_lt {x : ℝ} (h : 0 < x) : sin x < x :=
begin
cases le_or_gt x 1 with h' h',
{ have hx : abs x = x := abs_of_nonneg (le_of_lt h),
have : abs x ≤ 1, rwa [hx],
have := sin_bound this, rw [abs_le] at this,
have := this.2, rw [sub_le_iff_le_add', hx] at this,
apply lt_of_le_of_lt this, rw [sub_add], apply lt_of_lt_of_le _ (le_of_eq (sub_zero x)),
apply sub_lt_sub_left, rw sub_pos, apply mul_lt_mul',
{ rw [pow_succ x 3], refine le_trans _ (le_of_eq (one_mul _)),
rw mul_le_mul_right, exact h', apply pow_pos h },
norm_num, norm_num, apply pow_pos h },
exact lt_of_le_of_lt (sin_le_one x) h'
end
/- note 1: this inequality is not tight, the tighter inequality is sin x > x - x ^ 3 / 6.
note 2: this is also true for x > 1, but it's nontrivial for x just above 1. -/
lemma sin_gt_sub_cube {x : ℝ} (h : 0 < x) (h' : x ≤ 1) : x - x ^ 3 / 4 < sin x :=
begin
have hx : abs x = x := abs_of_nonneg (le_of_lt h),
have : abs x ≤ 1, rwa [hx],
have := sin_bound this, rw [abs_le] at this,
have := this.1, rw [le_sub_iff_add_le, hx] at this,
refine lt_of_lt_of_le _ this,
rw [add_comm, sub_add, sub_neg_eq_add], apply sub_lt_sub_left,
apply add_lt_of_lt_sub_left,
rw (show x ^ 3 / 4 - x ^ 3 / 6 = x ^ 3 / 12,
by simp [div_eq_mul_inv, (mul_sub _ _ _).symm, -sub_eq_add_neg]; congr; norm_num),
apply mul_lt_mul',
{ rw [pow_succ x 3], refine le_trans _ (le_of_eq (one_mul _)),
rw mul_le_mul_right, exact h', apply pow_pos h },
norm_num, norm_num, apply pow_pos h
end
/-- The type of angles -/
def angle : Type :=
quotient_add_group.quotient (gmultiples (2 * π))
namespace angle
instance angle.add_comm_group : add_comm_group angle :=
quotient_add_group.add_comm_group _
instance : inhabited angle := ⟨0⟩
instance angle.has_coe : has_coe ℝ angle :=
⟨quotient.mk'⟩
instance angle.is_add_group_hom : @is_add_group_hom ℝ angle _ _ (coe : ℝ → angle) :=
@quotient_add_group.is_add_group_hom _ _ _ (normal_add_subgroup_of_add_comm_group _)
@[simp] lemma coe_zero : ↑(0 : ℝ) = (0 : angle) := rfl
@[simp] lemma coe_add (x y : ℝ) : ↑(x + y : ℝ) = (↑x + ↑y : angle) := rfl
@[simp] lemma coe_neg (x : ℝ) : ↑(-x : ℝ) = -(↑x : angle) := rfl
@[simp] lemma coe_sub (x y : ℝ) : ↑(x - y : ℝ) = (↑x - ↑y : angle) := rfl
@[simp] lemma coe_smul (x : ℝ) (n : ℕ) :
↑(add_monoid.smul n x : ℝ) = add_monoid.smul n (↑x : angle) :=
add_monoid_hom.map_smul ⟨coe, coe_zero, coe_add⟩ _ _
@[simp] lemma coe_gsmul (x : ℝ) (n : ℤ) : ↑(gsmul n x : ℝ) = gsmul n (↑x : angle) :=
add_monoid_hom.map_gsmul ⟨coe, coe_zero, coe_add⟩ _ _
@[simp] lemma coe_two_pi : ↑(2 * π : ℝ) = (0 : angle) :=
quotient.sound' ⟨-1, by dsimp only; rw [neg_one_gsmul, add_zero]⟩
lemma angle_eq_iff_two_pi_dvd_sub {ψ θ : ℝ} : (θ : angle) = ψ ↔ ∃ k : ℤ, θ - ψ = 2 * π * k :=
by simp only [quotient_add_group.eq, gmultiples, set.mem_range, gsmul_eq_mul', (sub_eq_neg_add _ _).symm, eq_comm]
theorem cos_eq_iff_eq_or_eq_neg {θ ψ : ℝ} : cos θ = cos ψ ↔ (θ : angle) = ψ ∨ (θ : angle) = -ψ :=
begin
split,
{ intro Hcos,
rw [←sub_eq_zero, cos_sub_cos, mul_eq_zero, mul_eq_zero, neg_eq_zero, eq_false_intro two_ne_zero,
false_or, sin_eq_zero_iff, sin_eq_zero_iff] at Hcos,
rcases Hcos with ⟨n, hn⟩ | ⟨n, hn⟩,
{ right,
rw [eq_div_iff_mul_eq _ _ (@two_ne_zero ℝ _), ← sub_eq_iff_eq_add] at hn,
rw [← hn, coe_sub, eq_neg_iff_add_eq_zero, sub_add_cancel, mul_assoc,
← gsmul_eq_mul, coe_gsmul, mul_comm, coe_two_pi, gsmul_zero] },
{ left,
rw [eq_div_iff_mul_eq _ _ (@two_ne_zero ℝ _), eq_sub_iff_add_eq] at hn,
rw [← hn, coe_add, mul_assoc,
← gsmul_eq_mul, coe_gsmul, mul_comm, coe_two_pi, gsmul_zero, zero_add] } },
{ rw [angle_eq_iff_two_pi_dvd_sub, ← coe_neg, angle_eq_iff_two_pi_dvd_sub],
rintro (⟨k, H⟩ | ⟨k, H⟩),
rw [← sub_eq_zero_iff_eq, cos_sub_cos, H, mul_assoc 2 π k, mul_div_cancel_left _ (@two_ne_zero ℝ _),
mul_comm π _, sin_int_mul_pi, mul_zero],
rw [←sub_eq_zero_iff_eq, cos_sub_cos, ← sub_neg_eq_add, H, mul_assoc 2 π k,
mul_div_cancel_left _ (@two_ne_zero ℝ _), mul_comm π _, sin_int_mul_pi, mul_zero, zero_mul] }
end
theorem sin_eq_iff_eq_or_add_eq_pi {θ ψ : ℝ} : sin θ = sin ψ ↔ (θ : angle) = ψ ∨ (θ : angle) + ψ = π :=
begin
split,
{ intro Hsin, rw [← cos_pi_div_two_sub, ← cos_pi_div_two_sub] at Hsin,
cases cos_eq_iff_eq_or_eq_neg.mp Hsin with h h,
{ left, rw coe_sub at h, exact sub_left_inj.1 h },
right, rw [coe_sub, coe_sub, eq_neg_iff_add_eq_zero, add_sub,
sub_add_eq_add_sub, ← coe_add, add_halves, sub_sub, sub_eq_zero] at h,
exact h.symm },
{ rw [angle_eq_iff_two_pi_dvd_sub, ←eq_sub_iff_add_eq, ←coe_sub, angle_eq_iff_two_pi_dvd_sub],
rintro (⟨k, H⟩ | ⟨k, H⟩),
rw [← sub_eq_zero_iff_eq, sin_sub_sin, H, mul_assoc 2 π k, mul_div_cancel_left _ (@two_ne_zero ℝ _),
mul_comm π _, sin_int_mul_pi, mul_zero, zero_mul],
have H' : θ + ψ = (2 * k) * π + π := by rwa [←sub_add, sub_add_eq_add_sub, sub_eq_iff_eq_add,
mul_assoc, mul_comm π _, ←mul_assoc] at H,
rw [← sub_eq_zero_iff_eq, sin_sub_sin, H', add_div, mul_assoc 2 _ π, mul_div_cancel_left _ (@two_ne_zero ℝ _),
cos_add_pi_div_two, sin_int_mul_pi, neg_zero, mul_zero] }
end
theorem cos_sin_inj {θ ψ : ℝ} (Hcos : cos θ = cos ψ) (Hsin : sin θ = sin ψ) : (θ : angle) = ψ :=
begin
cases cos_eq_iff_eq_or_eq_neg.mp Hcos with hc hc, { exact hc },
cases sin_eq_iff_eq_or_add_eq_pi.mp Hsin with hs hs, { exact hs },
rw [eq_neg_iff_add_eq_zero, hs] at hc,
cases quotient.exact' hc with n hn, dsimp only at hn,
rw [← neg_one_mul, add_zero, ← sub_eq_zero_iff_eq, gsmul_eq_mul, ← mul_assoc, ← sub_mul,
mul_eq_zero, eq_false_intro (ne_of_gt pi_pos), or_false, sub_neg_eq_add,
← int.cast_zero, ← int.cast_one, ← int.cast_bit0, ← int.cast_mul, ← int.cast_add, int.cast_inj] at hn,
have : (n * 2 + 1) % (2:ℤ) = 0 % (2:ℤ) := congr_arg (%(2:ℤ)) hn,
rw [add_comm, int.add_mul_mod_self] at this,
exact absurd this one_ne_zero
end
end angle
/-- Inverse of the `sin` function, returns values in the range `-π / 2 ≤ arcsin x` and `arcsin x ≤ π / 2`.
If the argument is not between `-1` and `1` it defaults to `0` -/
noncomputable def arcsin (x : ℝ) : ℝ :=
if hx : -1 ≤ x ∧ x ≤ 1 then classical.some (exists_sin_eq hx) else 0
lemma arcsin_le_pi_div_two (x : ℝ) : arcsin x ≤ π / 2 :=
if hx : -1 ≤ x ∧ x ≤ 1
then by rw [arcsin, dif_pos hx]; exact (classical.some_spec (exists_sin_eq hx)).1.2
else by rw [arcsin, dif_neg hx]; exact le_of_lt pi_div_two_pos
lemma neg_pi_div_two_le_arcsin (x : ℝ) : -(π / 2) ≤ arcsin x :=
if hx : -1 ≤ x ∧ x ≤ 1
then by rw [arcsin, dif_pos hx]; exact (classical.some_spec (exists_sin_eq hx)).1.1
else by rw [arcsin, dif_neg hx]; exact neg_nonpos.2 (le_of_lt pi_div_two_pos)
lemma sin_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arcsin x) = x :=
by rw [arcsin, dif_pos (and.intro hx₁ hx₂)];
exact (classical.some_spec (exists_sin_eq ⟨hx₁, hx₂⟩)).2
lemma arcsin_sin {x : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) : arcsin (sin x) = x :=
sin_inj_of_le_of_le_pi_div_two (neg_pi_div_two_le_arcsin _) (arcsin_le_pi_div_two _) hx₁ hx₂
(by rw sin_arcsin (neg_one_le_sin _) (sin_le_one _))
lemma arcsin_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1)
(hxy : arcsin x = arcsin y) : x = y :=
by rw [← sin_arcsin hx₁ hx₂, ← sin_arcsin hy₁ hy₂, hxy]
@[simp] lemma arcsin_zero : arcsin 0 = 0 :=
sin_inj_of_le_of_le_pi_div_two
(neg_pi_div_two_le_arcsin _)
(arcsin_le_pi_div_two _)
(neg_nonpos.2 (le_of_lt pi_div_two_pos))
(le_of_lt pi_div_two_pos)
(by rw [sin_arcsin, sin_zero]; norm_num)
@[simp] lemma arcsin_one : arcsin 1 = π / 2 :=
sin_inj_of_le_of_le_pi_div_two
(neg_pi_div_two_le_arcsin _)
(arcsin_le_pi_div_two _)
(by linarith [pi_pos])
(le_refl _)
(by rw [sin_arcsin, sin_pi_div_two]; norm_num)
@[simp] lemma arcsin_neg (x : ℝ) : arcsin (-x) = -arcsin x :=
if h : -1 ≤ x ∧ x ≤ 1 then
have -1 ≤ -x ∧ -x ≤ 1, by rwa [neg_le_neg_iff, neg_le, and.comm],
sin_inj_of_le_of_le_pi_div_two
(neg_pi_div_two_le_arcsin _)
(arcsin_le_pi_div_two _)
(neg_le_neg (arcsin_le_pi_div_two _))
(neg_le.1 (neg_pi_div_two_le_arcsin _))
(by rw [sin_arcsin this.1 this.2, sin_neg, sin_arcsin h.1 h.2])
else
have ¬(-1 ≤ -x ∧ -x ≤ 1) := by rwa [neg_le_neg_iff, neg_le, and.comm],
by rw [arcsin, arcsin, dif_neg h, dif_neg this, neg_zero]
@[simp] lemma arcsin_neg_one : arcsin (-1) = -(π / 2) := by simp
lemma arcsin_nonneg {x : ℝ} (hx : 0 ≤ x) : 0 ≤ arcsin x :=
if hx₁ : x ≤ 1 then
not_lt.1 (λ h, not_lt.2 hx begin
have := sin_lt_sin_of_le_of_le_pi_div_two
(neg_pi_div_two_le_arcsin _) (arcsin_le_pi_div_two _)
(neg_nonpos.2 (le_of_lt pi_div_two_pos)) (le_of_lt pi_div_two_pos) h,
rw [real.sin_arcsin, sin_zero] at this; linarith
end)
else by rw [arcsin, dif_neg]; simp [hx₁]
lemma arcsin_eq_zero_iff {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : arcsin x = 0 ↔ x = 0 :=
⟨λ h, have sin (arcsin x) = 0, by simp [h],
by rwa [sin_arcsin hx₁ hx₂] at this,
λ h, by simp [h]⟩
lemma arcsin_pos {x : ℝ} (hx₁ : 0 < x) (hx₂ : x ≤ 1) : 0 < arcsin x :=
lt_of_le_of_ne (arcsin_nonneg (le_of_lt hx₁))
(ne.symm (mt (arcsin_eq_zero_iff (by linarith) hx₂).1 (ne_of_lt hx₁).symm))
lemma arcsin_nonpos {x : ℝ} (hx : x ≤ 0) : arcsin x ≤ 0 :=
neg_nonneg.1 (arcsin_neg x ▸ arcsin_nonneg (neg_nonneg.2 hx))
/-- Inverse of the `cos` function, returns values in the range `0 ≤ arccos x` and `arccos x ≤ π`.
If the argument is not between `-1` and `1` it defaults to `π / 2` -/
noncomputable def arccos (x : ℝ) : ℝ :=
π / 2 - arcsin x
lemma arccos_eq_pi_div_two_sub_arcsin (x : ℝ) : arccos x = π / 2 - arcsin x := rfl
lemma arcsin_eq_pi_div_two_sub_arccos (x : ℝ) : arcsin x = π / 2 - arccos x :=
by simp [sub_eq_add_neg, arccos]
lemma arccos_le_pi (x : ℝ) : arccos x ≤ π :=
by unfold arccos; linarith [neg_pi_div_two_le_arcsin x]
lemma arccos_nonneg (x : ℝ) : 0 ≤ arccos x :=
by unfold arccos; linarith [arcsin_le_pi_div_two x]
lemma cos_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arccos x) = x :=
by rw [arccos, cos_pi_div_two_sub, sin_arcsin hx₁ hx₂]
lemma arccos_cos {x : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π) : arccos (cos x) = x :=
by rw [arccos, ← sin_pi_div_two_sub, arcsin_sin]; simp [sub_eq_add_neg]; linarith
lemma arccos_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1)
(hxy : arccos x = arccos y) : x = y :=
arcsin_inj hx₁ hx₂ hy₁ hy₂ $ by simp [arccos, *] at *
@[simp] lemma arccos_zero : arccos 0 = π / 2 := by simp [arccos]
@[simp] lemma arccos_one : arccos 1 = 0 := by simp [arccos]
@[simp] lemma arccos_neg_one : arccos (-1) = π := by simp [arccos, add_halves]
lemma arccos_neg (x : ℝ) : arccos (-x) = π - arccos x :=
by rw [← add_halves π, arccos, arcsin_neg, arccos, add_sub_assoc, sub_sub_self]; simp
lemma cos_arcsin_nonneg (x : ℝ) : 0 ≤ cos (arcsin x) :=
cos_nonneg_of_neg_pi_div_two_le_of_le_pi_div_two
(neg_pi_div_two_le_arcsin _) (arcsin_le_pi_div_two _)
lemma cos_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arcsin x) = sqrt (1 - x ^ 2) :=
have sin (arcsin x) ^ 2 + cos (arcsin x) ^ 2 = 1 := sin_sq_add_cos_sq (arcsin x),
begin
rw [← eq_sub_iff_add_eq', ← sqrt_inj (pow_two_nonneg _) (sub_nonneg.2 (sin_sq_le_one (arcsin x))),
pow_two, sqrt_mul_self (cos_arcsin_nonneg _)] at this,
rw [this, sin_arcsin hx₁ hx₂],
end
lemma sin_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arccos x) = sqrt (1 - x ^ 2) :=
by rw [arccos_eq_pi_div_two_sub_arcsin, sin_pi_div_two_sub, cos_arcsin hx₁ hx₂]
lemma abs_div_sqrt_one_add_lt (x : ℝ) : abs (x / sqrt (1 + x ^ 2)) < 1 :=
have h₁ : 0 < 1 + x ^ 2, from add_pos_of_pos_of_nonneg zero_lt_one (pow_two_nonneg _),
have h₂ : 0 < sqrt (1 + x ^ 2), from sqrt_pos.2 h₁,
by rw [abs_div, div_lt_iff (abs_pos_of_pos h₂), one_mul,
mul_self_lt_mul_self_iff (abs_nonneg x) (abs_nonneg _),
← abs_mul, ← abs_mul, mul_self_sqrt (add_nonneg zero_le_one (pow_two_nonneg _)),
abs_of_nonneg (mul_self_nonneg x), abs_of_nonneg (le_of_lt h₁), pow_two, add_comm];
exact lt_add_one _
lemma div_sqrt_one_add_lt_one (x : ℝ) : x / sqrt (1 + x ^ 2) < 1 :=
(abs_lt.1 (abs_div_sqrt_one_add_lt _)).2
lemma neg_one_lt_div_sqrt_one_add (x : ℝ) : -1 < x / sqrt (1 + x ^ 2) :=
(abs_lt.1 (abs_div_sqrt_one_add_lt _)).1
lemma tan_pos_of_pos_of_lt_pi_div_two {x : ℝ} (h0x : 0 < x) (hxp : x < π / 2) : 0 < tan x :=
by rw tan_eq_sin_div_cos; exact div_pos (sin_pos_of_pos_of_lt_pi h0x (by linarith))
(cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two (by linarith) hxp)
lemma tan_nonneg_of_nonneg_of_le_pi_div_two {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π / 2) : 0 ≤ tan x :=
match lt_or_eq_of_le h0x, lt_or_eq_of_le hxp with
| or.inl hx0, or.inl hxp := le_of_lt (tan_pos_of_pos_of_lt_pi_div_two hx0 hxp)
| or.inl hx0, or.inr hxp := by simp [hxp, tan_eq_sin_div_cos]
| or.inr hx0, _ := by simp [hx0.symm]
end
lemma tan_neg_of_neg_of_pi_div_two_lt {x : ℝ} (hx0 : x < 0) (hpx : -(π / 2) < x) : tan x < 0 :=
neg_pos.1 (tan_neg x ▸ tan_pos_of_pos_of_lt_pi_div_two (by linarith) (by linarith [pi_pos]))
lemma tan_nonpos_of_nonpos_of_neg_pi_div_two_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -(π / 2) ≤ x) : tan x ≤ 0 :=
neg_nonneg.1 (tan_neg x ▸ tan_nonneg_of_nonneg_of_le_pi_div_two (by linarith) (by linarith [pi_pos]))
lemma tan_lt_tan_of_nonneg_of_lt_pi_div_two {x y : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x < π / 2) (hy₁ : 0 ≤ y)
(hy₂ : y < π / 2) (hxy : x < y) : tan x < tan y :=
begin
rw [tan_eq_sin_div_cos, tan_eq_sin_div_cos],
exact div_lt_div
(sin_lt_sin_of_le_of_le_pi_div_two (by linarith) (le_of_lt hx₂)
(by linarith) (le_of_lt hy₂) hxy)
(cos_le_cos_of_nonneg_of_le_pi hx₁ (by linarith) hy₁ (by linarith) (le_of_lt hxy))
(sin_nonneg_of_nonneg_of_le_pi hy₁ (by linarith))
(cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two (by linarith) hy₂)
end
lemma tan_lt_tan_of_lt_of_lt_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2)
(hy₁ : -(π / 2) < y) (hy₂ : y < π / 2) (hxy : x < y) : tan x < tan y :=
match le_total x 0, le_total y 0 with
| or.inl hx0, or.inl hy0 := neg_lt_neg_iff.1 $ by rw [← tan_neg, ← tan_neg]; exact
tan_lt_tan_of_nonneg_of_lt_pi_div_two (neg_nonneg.2 hy0) (neg_lt.2 hy₁)
(neg_nonneg.2 hx0) (neg_lt.2 hx₁) (neg_lt_neg hxy)
| or.inl hx0, or.inr hy0 := (lt_or_eq_of_le hy0).elim
(λ hy0, calc tan x ≤ 0 : tan_nonpos_of_nonpos_of_neg_pi_div_two_le hx0 (le_of_lt hx₁)
... < tan y : tan_pos_of_pos_of_lt_pi_div_two hy0 hy₂)
(λ hy0, by rw [← hy0, tan_zero]; exact
tan_neg_of_neg_of_pi_div_two_lt (hy0.symm ▸ hxy) hx₁)
| or.inr hx0, or.inl hy0 := by linarith
| or.inr hx0, or.inr hy0 := tan_lt_tan_of_nonneg_of_lt_pi_div_two hx0 hx₂ hy0 hy₂ hxy
end
lemma tan_inj_of_lt_of_lt_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2)
(hy₁ : -(π / 2) < y) (hy₂ : y < π / 2) (hxy : tan x = tan y) : x = y :=
match lt_trichotomy x y with
| or.inl h := absurd (tan_lt_tan_of_lt_of_lt_pi_div_two hx₁ hx₂ hy₁ hy₂ h) (by rw hxy; exact lt_irrefl _)
| or.inr (or.inl h) := h
| or.inr (or.inr h) := absurd (tan_lt_tan_of_lt_of_lt_pi_div_two hy₁ hy₂ hx₁ hx₂ h) (by rw hxy; exact lt_irrefl _)
end
/-- Inverse of the `tan` function, returns values in the range `-π / 2 < arctan x` and `arctan x < π / 2` -/
noncomputable def arctan (x : ℝ) : ℝ :=
arcsin (x / sqrt (1 + x ^ 2))
lemma sin_arctan (x : ℝ) : sin (arctan x) = x / sqrt (1 + x ^ 2) :=
sin_arcsin (le_of_lt (neg_one_lt_div_sqrt_one_add _)) (le_of_lt (div_sqrt_one_add_lt_one _))
lemma cos_arctan (x : ℝ) : cos (arctan x) = 1 / sqrt (1 + x ^ 2) :=
have h₁ : (0 : ℝ) < 1 + x ^ 2,
from add_pos_of_pos_of_nonneg zero_lt_one (pow_two_nonneg _),
have h₂ : (x / sqrt (1 + x ^ 2)) ^ 2 < 1,
by rw [pow_two, ← abs_mul_self, _root_.abs_mul];
exact mul_lt_one_of_nonneg_of_lt_one_left (abs_nonneg _)
(abs_div_sqrt_one_add_lt _) (le_of_lt (abs_div_sqrt_one_add_lt _)),
by rw [arctan, cos_arcsin (le_of_lt (neg_one_lt_div_sqrt_one_add _)) (le_of_lt (div_sqrt_one_add_lt_one _)),
one_div_eq_inv, ← sqrt_inv, sqrt_inj (sub_nonneg.2 (le_of_lt h₂)) (inv_nonneg.2 (le_of_lt h₁)),
div_pow, pow_two (sqrt _), mul_self_sqrt (le_of_lt h₁),
← domain.mul_left_inj (ne.symm (ne_of_lt h₁)), mul_sub,
mul_div_cancel' _ (ne.symm (ne_of_lt h₁)), mul_inv_cancel (ne.symm (ne_of_lt h₁))];
simp
lemma tan_arctan (x : ℝ) : tan (arctan x) = x :=
by rw [tan_eq_sin_div_cos, sin_arctan, cos_arctan, div_div_div_div_eq, mul_one,
mul_div_assoc,
div_self (mt sqrt_eq_zero'.1 (not_le_of_gt (add_pos_of_pos_of_nonneg zero_lt_one (pow_two_nonneg x)))),
mul_one]
lemma arctan_lt_pi_div_two (x : ℝ) : arctan x < π / 2 :=
lt_of_le_of_ne (arcsin_le_pi_div_two _)
(λ h, ne_of_lt (div_sqrt_one_add_lt_one x) $
by rw [← sin_arcsin (le_of_lt (neg_one_lt_div_sqrt_one_add _))
(le_of_lt (div_sqrt_one_add_lt_one _)), ← arctan, h, sin_pi_div_two])
lemma neg_pi_div_two_lt_arctan (x : ℝ) : -(π / 2) < arctan x :=
lt_of_le_of_ne (neg_pi_div_two_le_arcsin _)
(λ h, ne_of_lt (neg_one_lt_div_sqrt_one_add x) $
by rw [← sin_arcsin (le_of_lt (neg_one_lt_div_sqrt_one_add _))
(le_of_lt (div_sqrt_one_add_lt_one _)), ← arctan, ← h, sin_neg, sin_pi_div_two])
lemma tan_surjective : function.surjective tan :=
function.surjective_of_has_right_inverse ⟨_, tan_arctan⟩
lemma arctan_tan {x : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2) : arctan (tan x) = x :=
tan_inj_of_lt_of_lt_pi_div_two (neg_pi_div_two_lt_arctan _)
(arctan_lt_pi_div_two _) hx₁ hx₂ (by rw tan_arctan)
@[simp] lemma arctan_zero : arctan 0 = 0 :=
by simp [arctan]
@[simp] lemma arctan_neg (x : ℝ) : arctan (-x) = - arctan x :=
by simp [arctan, neg_div]
end real
namespace complex
open_locale real
/-- `arg` returns values in the range (-π, π], such that for `x ≠ 0`,
`sin (arg x) = x.im / x.abs` and `cos (arg x) = x.re / x.abs`,
`arg 0` defaults to `0` -/
noncomputable def arg (x : ℂ) : ℝ :=
if 0 ≤ x.re
then real.arcsin (x.im / x.abs)
else if 0 ≤ x.im
then real.arcsin ((-x).im / x.abs) + π
else real.arcsin ((-x).im / x.abs) - π
lemma arg_le_pi (x : ℂ) : arg x ≤ π :=
if hx₁ : 0 ≤ x.re
then by rw [arg, if_pos hx₁];
exact le_trans (real.arcsin_le_pi_div_two _) (le_of_lt (half_lt_self real.pi_pos))
else
have hx : x ≠ 0, from λ h, by simpa [h, lt_irrefl] using hx₁,
if hx₂ : 0 ≤ x.im
then by rw [arg, if_neg hx₁, if_pos hx₂];
exact le_sub_iff_add_le.1 (by rw sub_self;
exact real.arcsin_nonpos (by rw [neg_im, neg_div, neg_nonpos]; exact div_nonneg hx₂ (abs_pos.2 hx)))
else by rw [arg, if_neg hx₁, if_neg hx₂];
exact sub_le_iff_le_add.2 (le_trans (real.arcsin_le_pi_div_two _)
(by linarith [real.pi_pos]))
lemma neg_pi_lt_arg (x : ℂ) : -π < arg x :=
if hx₁ : 0 ≤ x.re
then by rw [arg, if_pos hx₁];
exact lt_of_lt_of_le (neg_lt_neg (half_lt_self real.pi_pos)) (real.neg_pi_div_two_le_arcsin _)
else
have hx : x ≠ 0, from λ h, by simpa [h, lt_irrefl] using hx₁,
if hx₂ : 0 ≤ x.im
then by rw [arg, if_neg hx₁, if_pos hx₂];
exact sub_lt_iff_lt_add.1
(lt_of_lt_of_le (by linarith [real.pi_pos]) (real.neg_pi_div_two_le_arcsin _))
else by rw [arg, if_neg hx₁, if_neg hx₂];
exact lt_sub_iff_add_lt.2 (by rw neg_add_self;
exact real.arcsin_pos (by rw [neg_im]; exact div_pos (neg_pos.2 (lt_of_not_ge hx₂))
(abs_pos.2 hx)) (by rw [← abs_neg x]; exact (abs_le.1 (abs_im_div_abs_le_one _)).2))
lemma arg_eq_arg_neg_add_pi_of_im_nonneg_of_re_neg {x : ℂ} (hxr : x.re < 0) (hxi : 0 ≤ x.im) :
arg x = arg (-x) + π :=
have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos],
by rw [arg, arg, if_neg (not_le.2 hxr), if_pos this, if_pos hxi, abs_neg]
lemma arg_eq_arg_neg_sub_pi_of_im_neg_of_re_neg {x : ℂ} (hxr : x.re < 0) (hxi : x.im < 0) :
arg x = arg (-x) - π :=
have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos],
by rw [arg, arg, if_neg (not_le.2 hxr), if_neg (not_le.2 hxi), if_pos this, abs_neg]
@[simp] lemma arg_zero : arg 0 = 0 :=
by simp [arg, le_refl]
@[simp] lemma arg_one : arg 1 = 0 :=
by simp [arg, zero_le_one]
@[simp] lemma arg_neg_one : arg (-1) = π :=
by simp [arg, le_refl, not_le.2 (@zero_lt_one ℝ _)]
@[simp] lemma arg_I : arg I = π / 2 :=
by simp [arg, le_refl]
@[simp] lemma arg_neg_I : arg (-I) = -(π / 2) :=
by simp [arg, le_refl]
lemma sin_arg (x : ℂ) : real.sin (arg x) = x.im / x.abs :=
by unfold arg; split_ifs;
simp [sub_eq_add_neg, arg, real.sin_arcsin (abs_le.1 (abs_im_div_abs_le_one x)).1
(abs_le.1 (abs_im_div_abs_le_one x)).2, real.sin_add, neg_div, real.arcsin_neg,
real.sin_neg]
private lemma cos_arg_of_re_nonneg {x : ℂ} (hx : x ≠ 0) (hxr : 0 ≤ x.re) : real.cos (arg x) = x.re / x.abs :=
have 0 ≤ 1 - (x.im / abs x) ^ 2,
from sub_nonneg.2 $ by rw [pow_two, ← _root_.abs_mul_self, _root_.abs_mul, ← pow_two];
exact pow_le_one _ (_root_.abs_nonneg _) (abs_im_div_abs_le_one _),
by rw [eq_div_iff_mul_eq _ _ (mt abs_eq_zero.1 hx), ← real.mul_self_sqrt (abs_nonneg x),
arg, if_pos hxr, real.cos_arcsin (abs_le.1 (abs_im_div_abs_le_one x)).1
(abs_le.1 (abs_im_div_abs_le_one x)).2, ← real.sqrt_mul (abs_nonneg _), ← real.sqrt_mul this,
sub_mul, div_pow, ← pow_two, div_mul_cancel _ (pow_ne_zero 2 (mt abs_eq_zero.1 hx)),
one_mul, pow_two, mul_self_abs, norm_sq, pow_two, add_sub_cancel, real.sqrt_mul_self hxr]
lemma cos_arg {x : ℂ} (hx : x ≠ 0) : real.cos (arg x) = x.re / x.abs :=
if hxr : 0 ≤ x.re then cos_arg_of_re_nonneg hx hxr
else
have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos] using hxr,
if hxi : 0 ≤ x.im
then have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos] using hxr,
by rw [arg_eq_arg_neg_add_pi_of_im_nonneg_of_re_neg (not_le.1 hxr) hxi, real.cos_add_pi,
cos_arg_of_re_nonneg (neg_ne_zero.2 hx) this];
simp [neg_div]
else by rw [arg_eq_arg_neg_sub_pi_of_im_neg_of_re_neg (not_le.1 hxr) (not_le.1 hxi)];
simp [sub_eq_add_neg, real.cos_add, neg_div, cos_arg_of_re_nonneg (neg_ne_zero.2 hx) this]
lemma tan_arg {x : ℂ} : real.tan (arg x) = x.im / x.re :=
begin
by_cases h : x = 0,
{ simp only [h, euclidean_domain.zero_div,
complex.zero_im, complex.arg_zero, real.tan_zero, complex.zero_re]},
rw [real.tan_eq_sin_div_cos, sin_arg, cos_arg h,
div_div_div_cancel_right' _ (mt abs_eq_zero.1 h)]
end
lemma arg_cos_add_sin_mul_I {x : ℝ} (hx₁ : -π < x) (hx₂ : x ≤ π) :
arg (cos x + sin x * I) = x :=
if hx₃ : -(π / 2) ≤ x ∧ x ≤ π / 2
then
have hx₄ : 0 ≤ (cos x + sin x * I).re,
by simp; exact real.cos_nonneg_of_neg_pi_div_two_le_of_le_pi_div_two hx₃.1 hx₃.2,
by rw [arg, if_pos hx₄];
simp [abs_cos_add_sin_mul_I, sin_of_real_re, real.arcsin_sin hx₃.1 hx₃.2]
else if hx₄ : x < -(π / 2)
then
have hx₅ : ¬0 ≤ (cos x + sin x * I).re :=
suffices ¬ 0 ≤ real.cos x, by simpa,
not_le.2 $ by rw ← real.cos_neg;
apply real.cos_neg_of_pi_div_two_lt_of_lt; linarith,
have hx₆ : ¬0 ≤ (cos ↑x + sin ↑x * I).im :=
suffices real.sin x < 0, by simpa,
by apply real.sin_neg_of_neg_of_neg_pi_lt; linarith,
suffices -π + -real.arcsin (real.sin x) = x,
by rw [arg, if_neg hx₅, if_neg hx₆];
simpa [sub_eq_add_neg, add_comm, abs_cos_add_sin_mul_I, sin_of_real_re],
by rw [← real.arcsin_neg, ← real.sin_add_pi, real.arcsin_sin]; try {simp [add_left_comm]}; linarith
else
have hx₅ : π / 2 < x, by cases not_and_distrib.1 hx₃; linarith,
have hx₆ : ¬0 ≤ (cos x + sin x * I).re :=
suffices ¬0 ≤ real.cos x, by simpa,
not_le.2 $ by apply real.cos_neg_of_pi_div_two_lt_of_lt; linarith,
have hx₇ : 0 ≤ (cos x + sin x * I).im :=
suffices 0 ≤ real.sin x, by simpa,
by apply real.sin_nonneg_of_nonneg_of_le_pi; linarith,
suffices π - real.arcsin (real.sin x) = x,
by rw [arg, if_neg hx₆, if_pos hx₇];
simpa [sub_eq_add_neg, add_comm, abs_cos_add_sin_mul_I, sin_of_real_re],
by rw [← real.sin_pi_sub, real.arcsin_sin]; simp [sub_eq_add_neg]; linarith
lemma arg_eq_arg_iff {x y : ℂ} (hx : x ≠ 0) (hy : y ≠ 0) :
arg x = arg y ↔ (abs y / abs x : ℂ) * x = y :=
have hax : abs x ≠ 0, from (mt abs_eq_zero.1 hx),
have hay : abs y ≠ 0, from (mt abs_eq_zero.1 hy),
⟨λ h,
begin
have hcos := congr_arg real.cos h,
rw [cos_arg hx, cos_arg hy, div_eq_div_iff hax hay] at hcos,
have hsin := congr_arg real.sin h,
rw [sin_arg, sin_arg, div_eq_div_iff hax hay] at hsin,
apply complex.ext,
{ rw [mul_re, ← of_real_div, of_real_re, of_real_im, zero_mul, sub_zero, mul_comm,
← mul_div_assoc, hcos, mul_div_cancel _ hax] },
{ rw [mul_im, ← of_real_div, of_real_re, of_real_im, zero_mul, add_zero,
mul_comm, ← mul_div_assoc, hsin, mul_div_cancel _ hax] }
end,
λ h,
have hre : abs (y / x) * x.re = y.re,
by rw ← of_real_div at h;
simpa [-of_real_div] using congr_arg re h,
have hre' : abs (x / y) * y.re = x.re,
by rw [← hre, abs_div, abs_div, ← mul_assoc, div_mul_div,
mul_comm (abs _), div_self (mul_ne_zero hay hax), one_mul],
have him : abs (y / x) * x.im = y.im,
by rw ← of_real_div at h;
simpa [-of_real_div] using congr_arg im h,
have him' : abs (x / y) * y.im = x.im,
by rw [← him, abs_div, abs_div, ← mul_assoc, div_mul_div,
mul_comm (abs _), div_self (mul_ne_zero hay hax), one_mul],
have hxya : x.im / abs x = y.im / abs y,
by rw [← him, abs_div, mul_comm, ← mul_div_comm, mul_div_cancel_left _ hay],
have hnxya : (-x).im / abs x = (-y).im / abs y,
by rw [neg_im, neg_im, neg_div, neg_div, hxya],
if hxr : 0 ≤ x.re
then
have hyr : 0 ≤ y.re, from hre ▸ mul_nonneg (abs_nonneg _) hxr,
by simp [arg, *] at *
else
have hyr : ¬ 0 ≤ y.re, from λ hyr, hxr $ hre' ▸ mul_nonneg (abs_nonneg _) hyr,
if hxi : 0 ≤ x.im
then
have hyi : 0 ≤ y.im, from him ▸ mul_nonneg (abs_nonneg _) hxi,
by simp [arg, *] at *
else
have hyi : ¬ 0 ≤ y.im, from λ hyi, hxi $ him' ▸ mul_nonneg (abs_nonneg _) hyi,
by simp [arg, *] at *⟩
lemma arg_real_mul (x : ℂ) {r : ℝ} (hr : 0 < r) : arg (r * x) = arg x :=
if hx : x = 0 then by simp [hx]
else (arg_eq_arg_iff (mul_ne_zero (of_real_ne_zero.2 (ne_of_lt hr).symm) hx) hx).2 $
by rw [abs_mul, abs_of_nonneg (le_of_lt hr), ← mul_assoc,
of_real_mul, mul_comm (r : ℂ), ← div_div_eq_div_mul,
div_mul_cancel _ (of_real_ne_zero.2 (ne_of_lt hr).symm),
div_self (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), one_mul]
lemma ext_abs_arg {x y : ℂ} (h₁ : x.abs = y.abs) (h₂ : x.arg = y.arg) : x = y :=
if hy : y = 0 then by simp * at *
else have hx : x ≠ 0, from λ hx, by simp [*, eq_comm] at *,
by rwa [arg_eq_arg_iff hx hy, h₁, div_self (of_real_ne_zero.2 (mt abs_eq_zero.1 hy)), one_mul] at h₂
lemma arg_of_real_of_nonneg {x : ℝ} (hx : 0 ≤ x) : arg x = 0 :=
by simp [arg, hx]
lemma arg_of_real_of_neg {x : ℝ} (hx : x < 0) : arg x = π :=
by rw [arg_eq_arg_neg_add_pi_of_im_nonneg_of_re_neg, ← of_real_neg, arg_of_real_of_nonneg];
simp [*, le_iff_eq_or_lt, lt_neg]
/-- Inverse of the `exp` function. Returns values such that `(log x).im > - π` and `(log x).im ≤ π`.
`log 0 = 0`-/
noncomputable def log (x : ℂ) : ℂ := x.abs.log + arg x * I
lemma log_re (x : ℂ) : x.log.re = x.abs.log := by simp [log]
lemma log_im (x : ℂ) : x.log.im = x.arg := by simp [log]
lemma exp_log {x : ℂ} (hx : x ≠ 0) : exp (log x) = x :=
by rw [log, exp_add_mul_I, ← of_real_sin, sin_arg, ← of_real_cos, cos_arg hx,
← of_real_exp, real.exp_log (abs_pos.2 hx), mul_add, of_real_div, of_real_div,
mul_div_cancel' _ (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), ← mul_assoc,
mul_div_cancel' _ (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), re_add_im]
lemma exp_inj_of_neg_pi_lt_of_le_pi {x y : ℂ} (hx₁ : -π < x.im) (hx₂ : x.im ≤ π)
(hy₁ : - π < y.im) (hy₂ : y.im ≤ π) (hxy : exp x = exp y) : x = y :=
by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y] at hxy;
exact complex.ext
(real.exp_injective $
by simpa [abs_mul, abs_cos_add_sin_mul_I] using congr_arg complex.abs hxy)
(by simpa [(of_real_exp _).symm, - of_real_exp, arg_real_mul _ (real.exp_pos _),
arg_cos_add_sin_mul_I hx₁ hx₂, arg_cos_add_sin_mul_I hy₁ hy₂] using congr_arg arg hxy)
lemma log_exp {x : ℂ} (hx₁ : -π < x.im) (hx₂: x.im ≤ π) : log (exp x) = x :=
exp_inj_of_neg_pi_lt_of_le_pi
(by rw log_im; exact neg_pi_lt_arg _)
(by rw log_im; exact arg_le_pi _)
hx₁ hx₂ (by rw [exp_log (exp_ne_zero _)])
lemma of_real_log {x : ℝ} (hx : 0 ≤ x) : (x.log : ℂ) = log x :=
complex.ext
(by rw [log_re, of_real_re, abs_of_nonneg hx])
(by rw [of_real_im, log_im, arg_of_real_of_nonneg hx])
@[simp] lemma log_zero : log 0 = 0 := by simp [log]
@[simp] lemma log_one : log 1 = 0 := by simp [log]
lemma log_neg_one : log (-1) = π * I := by simp [log]
lemma log_I : log I = π / 2 * I := by simp [log]
lemma log_neg_I : log (-I) = -(π / 2) * I := by simp [log]
lemma exp_eq_one_iff {x : ℂ} : exp x = 1 ↔ ∃ n : ℤ, x = n * ((2 * π) * I) :=
have real.exp (x.re) * real.cos (x.im) = 1 → real.cos x.im ≠ -1,
from λ h₁ h₂, begin
rw [h₂, mul_neg_eq_neg_mul_symm, mul_one, neg_eq_iff_neg_eq] at h₁,
have := real.exp_pos x.re,
rw ← h₁ at this,
exact absurd this (by norm_num)
end,
calc exp x = 1 ↔ (exp x).re = 1 ∧ (exp x).im = 0 : by simp [complex.ext_iff]
... ↔ real.cos x.im = 1 ∧ real.sin x.im = 0 ∧ x.re = 0 :
begin
rw exp_eq_exp_re_mul_sin_add_cos,
simp [complex.ext_iff, cos_of_real_re, sin_of_real_re, exp_of_real_re,
real.exp_ne_zero],
split; finish [real.sin_eq_zero_iff_cos_eq]
end
... ↔ (∃ n : ℤ, ↑n * (2 * π) = x.im) ∧ (∃ n : ℤ, ↑n * π = x.im) ∧ x.re = 0 :
by rw [real.sin_eq_zero_iff, real.cos_eq_one_iff]
... ↔ ∃ n : ℤ, x = n * ((2 * π) * I) :
⟨λ ⟨⟨n, hn⟩, ⟨m, hm⟩, h⟩, ⟨n, by simp [complex.ext_iff, hn.symm, h]⟩,
λ ⟨n, hn⟩, ⟨⟨n, by simp [hn]⟩, ⟨2 * n, by simp [hn, mul_comm, mul_assoc, mul_left_comm]⟩,
by simp [hn]⟩⟩
lemma exp_eq_exp_iff_exp_sub_eq_one {x y : ℂ} : exp x = exp y ↔ exp (x - y) = 1 :=
by rw [exp_sub, div_eq_one_iff_eq _ (exp_ne_zero _)]
lemma exp_eq_exp_iff_exists_int {x y : ℂ} : exp x = exp y ↔ ∃ n : ℤ, x = y + n * ((2 * π) * I) :=
by simp only [exp_eq_exp_iff_exp_sub_eq_one, exp_eq_one_iff, sub_eq_iff_eq_add']
@[simp] lemma cos_pi_div_two : cos (π / 2) = 0 :=
calc cos (π / 2) = real.cos (π / 2) : by rw [of_real_cos]; simp
... = 0 : by simp
@[simp] lemma sin_pi_div_two : sin (π / 2) = 1 :=
calc sin (π / 2) = real.sin (π / 2) : by rw [of_real_sin]; simp
... = 1 : by simp
@[simp] lemma sin_pi : sin π = 0 :=
by rw [← of_real_sin, real.sin_pi]; simp
@[simp] lemma cos_pi : cos π = -1 :=
by rw [← of_real_cos, real.cos_pi]; simp
@[simp] lemma sin_two_pi : sin (2 * π) = 0 :=
by simp [two_mul, sin_add]
@[simp] lemma cos_two_pi : cos (2 * π) = 1 :=
by simp [two_mul, cos_add]
lemma sin_add_pi (x : ℝ) : sin (x + π) = -sin x :=
by simp [sin_add]
lemma sin_add_two_pi (x : ℝ) : sin (x + 2 * π) = sin x :=
by simp [sin_add_pi, sin_add, sin_two_pi, cos_two_pi]
lemma cos_add_two_pi (x : ℝ) : cos (x + 2 * π) = cos x :=
by simp [cos_add, cos_two_pi, sin_two_pi]
lemma sin_pi_sub (x : ℝ) : sin (π - x) = sin x :=
by simp [sub_eq_add_neg, sin_add]
lemma cos_add_pi (x : ℝ) : cos (x + π) = -cos x :=
by simp [cos_add]
lemma cos_pi_sub (x : ℝ) : cos (π - x) = -cos x :=
by simp [sub_eq_add_neg, cos_add]
lemma sin_add_pi_div_two (x : ℝ) : sin (x + π / 2) = cos x :=
by simp [sin_add]
lemma sin_sub_pi_div_two (x : ℝ) : sin (x - π / 2) = -cos x :=
by simp [sub_eq_add_neg, sin_add]
lemma sin_pi_div_two_sub (x : ℝ) : sin (π / 2 - x) = cos x :=
by simp [sub_eq_add_neg, sin_add]
lemma cos_add_pi_div_two (x : ℝ) : cos (x + π / 2) = -sin x :=
by simp [cos_add]
lemma cos_sub_pi_div_two (x : ℝ) : cos (x - π / 2) = sin x :=
by simp [sub_eq_add_neg, cos_add]
lemma cos_pi_div_two_sub (x : ℝ) : cos (π / 2 - x) = sin x :=
by rw [← cos_neg, neg_sub, cos_sub_pi_div_two]
lemma sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 :=
by induction n; simp [add_mul, sin_add, *]
lemma sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 :=
by cases n; simp [add_mul, sin_add, *, sin_nat_mul_pi]
lemma cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 :=
by induction n; simp [*, mul_add, cos_add, add_mul, cos_two_pi, sin_two_pi]
lemma cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 :=
by cases n; simp only [cos_nat_mul_two_pi, int.of_nat_eq_coe,
int.neg_succ_of_nat_coe, int.cast_coe_nat, int.cast_neg,
(neg_mul_eq_neg_mul _ _).symm, cos_neg]
lemma cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 :=
by simp [cos_add, sin_add, cos_int_mul_two_pi]
section pow
/-- The complex power function `x^y`, given by `x^y = exp(y log x)` (where `log` is the principal
determination of the logarithm), unless `x = 0` where one sets `0^0 = 1` and `0^y = 0` for
`y ≠ 0`. -/
noncomputable def cpow (x y : ℂ) : ℂ :=
if x = 0
then if y = 0
then 1
else 0
else exp (log x * y)
noncomputable instance : has_pow ℂ ℂ := ⟨cpow⟩
@[simp] lemma cpow_eq_pow (x y : ℂ) : cpow x y = x ^ y := rfl
lemma cpow_def (x y : ℂ) : x ^ y =
if x = 0
then if y = 0
then 1
else 0
else exp (log x * y) := rfl
@[simp] lemma cpow_zero (x : ℂ) : x ^ (0 : ℂ) = 1 := by simp [cpow_def]
@[simp] lemma cpow_eq_zero_iff (x y : ℂ) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 :=
by { simp only [cpow_def], split_ifs; simp [*, exp_ne_zero] }
@[simp] lemma zero_cpow {x : ℂ} (h : x ≠ 0) : (0 : ℂ) ^ x = 0 :=
by simp [cpow_def, *]
@[simp] lemma cpow_one (x : ℂ) : x ^ (1 : ℂ) = x :=
if hx : x = 0 then by simp [hx, cpow_def]
else by rw [cpow_def, if_neg (@one_ne_zero ℂ _), if_neg hx, mul_one, exp_log hx]
@[simp] lemma one_cpow (x : ℂ) : (1 : ℂ) ^ x = 1 :=
by rw cpow_def; split_ifs; simp [one_ne_zero, *] at *
lemma cpow_add {x : ℂ} (y z : ℂ) (hx : x ≠ 0) : x ^ (y + z) = x ^ y * x ^ z :=
by simp [cpow_def]; split_ifs; simp [*, exp_add, mul_add] at *
lemma cpow_mul {x y : ℂ} (z : ℂ) (h₁ : -π < (log x * y).im) (h₂ : (log x * y).im ≤ π) :
x ^ (y * z) = (x ^ y) ^ z :=
begin
simp [cpow_def],
split_ifs;
simp [*, exp_ne_zero, log_exp h₁ h₂, mul_assoc] at *
end
lemma cpow_neg (x y : ℂ) : x ^ -y = (x ^ y)⁻¹ :=
by simp [cpow_def]; split_ifs; simp [exp_neg]
@[simp] lemma cpow_nat_cast (x : ℂ) : ∀ (n : ℕ), x ^ (n : ℂ) = x ^ n
| 0 := by simp
| (n + 1) := if hx : x = 0 then by simp only [hx, pow_succ,
complex.zero_cpow (nat.cast_ne_zero.2 (nat.succ_ne_zero _)), zero_mul]
else by simp [cpow_def, hx, mul_comm, mul_add, exp_add, pow_succ, (cpow_nat_cast n).symm,
exp_log hx]
@[simp] lemma cpow_int_cast (x : ℂ) : ∀ (n : ℤ), x ^ (n : ℂ) = x ^ n
| (n : ℕ) := by simp; refl
| -[1+ n] := by rw fpow_neg_succ_of_nat;
simp only [int.neg_succ_of_nat_coe, int.cast_neg, complex.cpow_neg, inv_eq_one_div,
int.cast_coe_nat, cpow_nat_cast]
lemma cpow_nat_inv_pow (x : ℂ) {n : ℕ} (hn : 0 < n) : (x ^ (n⁻¹ : ℂ)) ^ n = x :=
have (log x * (↑n)⁻¹).im = (log x).im / n,
by rw [div_eq_mul_inv, ← of_real_nat_cast, ← of_real_inv, mul_im,
of_real_re, of_real_im]; simp,
have h : -π < (log x * (↑n)⁻¹).im ∧ (log x * (↑n)⁻¹).im ≤ π,
from (le_total (log x).im 0).elim
(λ h, ⟨calc -π < (log x).im : by simp [log, neg_pi_lt_arg]
... ≤ ((log x).im * 1) / n : le_div_of_mul_le (nat.cast_pos.2 hn)
(mul_le_mul_of_nonpos_left (by rw ← nat.cast_one; exact nat.cast_le.2 hn) h)
... = (log x * (↑n)⁻¹).im : by simp [this],
this.symm ▸ le_trans (div_nonpos_of_nonpos_of_pos h (nat.cast_pos.2 hn))
(le_of_lt real.pi_pos)⟩)
(λ h, ⟨this.symm ▸ lt_of_lt_of_le (neg_neg_of_pos real.pi_pos)
(div_nonneg h (nat.cast_pos.2 hn)),
calc (log x * (↑n)⁻¹).im = (1 * (log x).im) / n : by simp [this]
... ≤ (log x).im : (div_le_of_le_mul (nat.cast_pos.2 hn)
(mul_le_mul_of_nonneg_right (by rw ← nat.cast_one; exact nat.cast_le.2 hn) h))
... ≤ _ : by simp [log, arg_le_pi]⟩),
by rw [← cpow_nat_cast, ← cpow_mul _ h.1 h.2,
inv_mul_cancel (show (n : ℂ) ≠ 0, from nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 hn)),
cpow_one]
end pow
end complex
namespace real
/-- The real power function `x^y`, defined as the real part of the complex power function.
For `x > 0`, it is equal to `exp(y log x)`. For `x = 0`, one sets `0^0=1` and `0^y=0` for `y ≠ 0`.
For `x < 0`, the definition is somewhat arbitary as it depends on the choice of a complex
determination of the logarithm. With our conventions, it is equal to `exp (y log (-x)) cos (πy)`. -/
noncomputable def rpow (x y : ℝ) := ((x : ℂ) ^ (y : ℂ)).re
noncomputable instance : has_pow ℝ ℝ := ⟨rpow⟩
@[simp] lemma rpow_eq_pow (x y : ℝ) : rpow x y = x ^ y := rfl
lemma rpow_def (x y : ℝ) : x ^ y = ((x : ℂ) ^ (y : ℂ)).re := rfl
lemma rpow_def_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ y =
if x = 0
then if y = 0
then 1
else 0
else exp (log x * y) :=
by simp only [rpow_def, complex.cpow_def];
split_ifs;
simp [*, (complex.of_real_log hx).symm, -complex.of_real_mul,
(complex.of_real_mul _ _).symm, complex.exp_of_real_re] at *
lemma rpow_def_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : x ^ y = exp (log x * y) :=
by rw [rpow_def_of_nonneg (le_of_lt hx), if_neg (ne_of_gt hx)]
lemma rpow_eq_zero_iff_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 :=
by { simp only [rpow_def_of_nonneg hx], split_ifs; simp [*, exp_ne_zero] }
open_locale real
lemma rpow_def_of_neg {x : ℝ} (hx : x < 0) (y : ℝ) : x ^ y = exp (log (-x) * y) * cos (y * π) :=
begin
rw [rpow_def, complex.cpow_def, if_neg],
have : complex.log x * y = ↑(log(-x) * y) + ↑(y * π) * complex.I,
simp only [complex.log, abs_of_neg hx, complex.arg_of_real_of_neg hx,
complex.abs_of_real, complex.of_real_mul], ring,
{ rw [this, complex.exp_add_mul_I, ← complex.of_real_exp, ← complex.of_real_cos,
← complex.of_real_sin, mul_add, ← complex.of_real_mul, ← mul_assoc, ← complex.of_real_mul,
complex.add_re, complex.of_real_re, complex.mul_re, complex.I_re, complex.of_real_im], ring },
{ rw complex.of_real_eq_zero, exact ne_of_lt hx }
end
lemma rpow_def_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℝ) : x ^ y =
if x = 0
then if y = 0
then 1
else 0
else exp (log (-x) * y) * cos (y * π) :=
by split_ifs; simp [rpow_def, *]; exact rpow_def_of_neg (lt_of_le_of_ne hx h) _
lemma rpow_pos_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : 0 < x ^ y :=
by rw rpow_def_of_pos hx; apply exp_pos
lemma abs_rpow_le_abs_rpow (x y : ℝ) : abs (x ^ y) ≤ abs (x) ^ y :=
abs_le_of_le_of_neg_le
begin
cases lt_trichotomy 0 x, { rw abs_of_pos h },
cases h, { simp [h.symm] },
rw [rpow_def_of_neg h, rpow_def_of_pos (abs_pos_of_neg h), abs_of_neg h],
calc exp (log (-x) * y) * cos (y * π) ≤ exp (log (-x) * y) * 1 :
mul_le_mul_of_nonneg_left (cos_le_one _) (le_of_lt $ exp_pos _)
... = _ : mul_one _
end
begin
cases lt_trichotomy 0 x, { rw abs_of_pos h, have : 0 < x^y := rpow_pos_of_pos h _, linarith },
cases h, { simp only [h.symm, abs_zero, rpow_def_of_nonneg], split_ifs, repeat {norm_num}},
rw [rpow_def_of_neg h, rpow_def_of_pos (abs_pos_of_neg h), abs_of_neg h],
calc -(exp (log (-x) * y) * cos (y * π)) = exp (log (-x) * y) * (-cos (y * π)) : by ring
... ≤ exp (log (-x) * y) * 1 :
mul_le_mul_of_nonneg_left (neg_le.2 $ neg_one_le_cos _) (le_of_lt $ exp_pos _)
... = exp (log (-x) * y) : mul_one _
end
end real
namespace complex
lemma of_real_cpow {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : ((x ^ y : ℝ) : ℂ) = (x : ℂ) ^ (y : ℂ) :=
by simp [real.rpow_def_of_nonneg hx, complex.cpow_def]; split_ifs; simp [complex.of_real_log hx]
@[simp] lemma abs_cpow_real (x : ℂ) (y : ℝ) : abs (x ^ (y : ℂ)) = x.abs ^ y :=
begin
rw [real.rpow_def_of_nonneg (abs_nonneg _), complex.cpow_def],
split_ifs;
simp [*, abs_of_nonneg (le_of_lt (real.exp_pos _)), complex.log, complex.exp_add,
add_mul, mul_right_comm _ I, exp_mul_I, abs_cos_add_sin_mul_I,
(complex.of_real_mul _ _).symm, -complex.of_real_mul] at *
end
@[simp] lemma abs_cpow_inv_nat (x : ℂ) (n : ℕ) : abs (x ^ (n⁻¹ : ℂ)) = x.abs ^ (n⁻¹ : ℝ) :=
by rw ← abs_cpow_real; simp [-abs_cpow_real]
end complex
namespace real
open_locale real
variables {x y z : ℝ}
@[simp] lemma rpow_zero (x : ℝ) : x ^ (0 : ℝ) = 1 := by simp [rpow_def]
@[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ) ^ x = 0 :=
by simp [rpow_def, *]
@[simp] lemma rpow_one (x : ℝ) : x ^ (1 : ℝ) = x := by simp [rpow_def]
@[simp] lemma one_rpow (x : ℝ) : (1 : ℝ) ^ x = 1 := by simp [rpow_def]
lemma rpow_nonneg_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : 0 ≤ x ^ y :=
by rw [rpow_def_of_nonneg hx];
split_ifs; simp only [zero_le_one, le_refl, le_of_lt (exp_pos _)]
lemma rpow_add {x : ℝ} (y z : ℝ) (hx : 0 < x) : x ^ (y + z) = x ^ y * x ^ z :=
by simp only [rpow_def_of_pos hx, mul_add, exp_add]
lemma rpow_mul {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z :=
by rw [← complex.of_real_inj, complex.of_real_cpow (rpow_nonneg_of_nonneg hx _),
complex.of_real_cpow hx, complex.of_real_mul, complex.cpow_mul, complex.of_real_cpow hx];
simp only [(complex.of_real_mul _ _).symm, (complex.of_real_log hx).symm,
complex.of_real_im, neg_lt_zero, pi_pos, le_of_lt pi_pos]
lemma rpow_neg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ :=
by simp only [rpow_def_of_nonneg hx]; split_ifs; simp [*, exp_neg] at *
@[simp] lemma rpow_nat_cast (x : ℝ) (n : ℕ) : x ^ (n : ℝ) = x ^ n :=
by simp only [rpow_def, (complex.of_real_pow _ _).symm, complex.cpow_nat_cast,
complex.of_real_nat_cast, complex.of_real_re]
@[simp] lemma rpow_int_cast (x : ℝ) (n : ℤ) : x ^ (n : ℝ) = x ^ n :=
by simp only [rpow_def, (complex.of_real_fpow _ _).symm, complex.cpow_int_cast,
complex.of_real_int_cast, complex.of_real_re]
lemma mul_rpow {x y z : ℝ} (h : 0 ≤ x) (h₁ : 0 ≤ y) : (x*y)^z = x^z * y^z :=
begin
iterate 3 { rw real.rpow_def_of_nonneg }, split_ifs; simp * at *,
{ have hx : 0 < x, cases lt_or_eq_of_le h with h₂ h₂, exact h₂, exfalso, apply h_2, exact eq.symm h₂,
have hy : 0 < y, cases lt_or_eq_of_le h₁ with h₂ h₂, exact h₂, exfalso, apply h_3, exact eq.symm h₂,
rw [log_mul hx hy, add_mul, exp_add]},
{ exact h₁},
{ exact h},
{ exact mul_nonneg h h₁},
end
lemma one_le_rpow {x z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x^z :=
begin
rw real.rpow_def_of_nonneg, split_ifs with h₂ h₃,
{ refl},
{ simp [*, not_le_of_gt zero_lt_one] at *},
{ have hx : 0 < x, exact lt_of_lt_of_le zero_lt_one h,
rw [←log_le_log zero_lt_one hx, log_one] at h,
have pos : 0 ≤ log x * z, exact mul_nonneg h h₁,
rwa [←exp_le_exp, exp_zero] at pos},
{ exact le_trans zero_le_one h},
end
lemma rpow_le_rpow {x y z: ℝ} (h : 0 ≤ x) (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z :=
begin
rw le_iff_eq_or_lt at h h₂, cases h₂,
{ rw [←h₂, rpow_zero, rpow_zero]},
{ cases h,
{ rw [←h, zero_rpow], rw real.rpow_def_of_nonneg, split_ifs,
{ exact zero_le_one},
{ refl},
{ exact le_of_lt (exp_pos (log y * z))},
{ rwa ←h at h₁},
{ exact ne.symm (ne_of_lt h₂)}},
{ have one_le : 1 ≤ y / x, rw one_le_div_iff_le h, exact h₁,
have one_le_pow : 1 ≤ (y / x)^z, exact one_le_rpow one_le (le_of_lt h₂),
rw [←mul_div_cancel y (ne.symm (ne_of_lt h)), mul_comm, mul_div_assoc],
rw [mul_rpow (le_of_lt h) (le_trans zero_le_one one_le), mul_comm],
exact (le_mul_of_ge_one_left (rpow_nonneg_of_nonneg (le_of_lt h) z) one_le_pow) } }
end
lemma rpow_lt_rpow (hx : 0 ≤ x) (hxy : x < y) (hz : 0 < z) : x^z < y^z :=
begin
rw le_iff_eq_or_lt at hx, cases hx,
{ rw [← hx, zero_rpow (ne_of_gt hz)], exact rpow_pos_of_pos (by rwa ← hx at hxy) _ },
rw [rpow_def_of_pos hx, rpow_def_of_pos (lt_trans hx hxy), exp_lt_exp],
exact mul_lt_mul_of_pos_right (log_lt_log hx hxy) hz
end
lemma rpow_lt_rpow_of_exponent_lt (hx : 1 < x) (hyz : y < z) : x^y < x^z :=
begin
repeat {rw [rpow_def_of_pos (lt_trans zero_lt_one hx)]},
rw exp_lt_exp, exact mul_lt_mul_of_pos_left hyz (log_pos hx),
end
lemma rpow_le_rpow_of_exponent_le (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z :=
begin
repeat {rw [rpow_def_of_pos (lt_of_lt_of_le zero_lt_one hx)]},
rw exp_le_exp, exact mul_le_mul_of_nonneg_left hyz (log_nonneg hx),
end
lemma rpow_lt_rpow_of_exponent_gt (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) :
x^y < x^z :=
begin
repeat {rw [rpow_def_of_pos hx0]},
rw exp_lt_exp, exact mul_lt_mul_of_neg_left hyz (log_neg hx0 hx1),
end
lemma rpow_le_rpow_of_exponent_ge (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) :
x^y ≤ x^z :=
begin
repeat {rw [rpow_def_of_pos hx0]},
rw exp_le_exp, exact mul_le_mul_of_nonpos_left hyz (log_nonpos hx1),
end
lemma rpow_le_one {x e : ℝ} (he : 0 ≤ e) (hx : 0 ≤ x) (hx2 : x ≤ 1) : x^e ≤ 1 :=
by rw ←one_rpow e; apply rpow_le_rpow; assumption
lemma one_lt_rpow (hx : 1 < x) (hz : 0 < z) : 1 < x^z :=
by { rw ← one_rpow z, exact rpow_lt_rpow zero_le_one hx hz }
lemma rpow_lt_one (hx : 0 < x) (hx1 : x < 1) (hz : 0 < z) : x^z < 1 :=
by { rw ← one_rpow z, exact rpow_lt_rpow (le_of_lt hx) hx1 hz }
lemma pow_nat_rpow_nat_inv {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : 0 < n) :
(x ^ n) ^ (n⁻¹ : ℝ) = x :=
have hn0 : (n : ℝ) ≠ 0, by simpa [nat.pos_iff_ne_zero] using hn,
by rw [← rpow_nat_cast, ← rpow_mul hx, mul_inv_cancel hn0, rpow_one]
lemma rpow_nat_inv_pow_nat {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : 0 < n) :
(x ^ (n⁻¹ : ℝ)) ^ n = x :=
have hn0 : (n : ℝ) ≠ 0, by simpa [nat.pos_iff_ne_zero] using hn,
by rw [← rpow_nat_cast, ← rpow_mul hx, inv_mul_cancel hn0, rpow_one]
section prove_rpow_is_continuous
lemma continuous_rpow_aux1 : continuous (λp : {p:ℝ×ℝ // 0 < p.1}, p.val.1 ^ p.val.2) :=
suffices h : continuous (λ p : {p:ℝ×ℝ // 0 < p.1 }, exp (log p.val.1 * p.val.2)),
by { convert h, ext p, rw rpow_def_of_pos p.2 },
continuous_exp.comp $
(show continuous ((λp:{p:ℝ//0 < p}, log (p.val)) ∘ (λp:{p:ℝ×ℝ//0<p.fst}, ⟨p.val.1, p.2⟩)), from
continuous_log'.comp $ continuous_subtype_mk _ $ continuous_fst.comp continuous_subtype_val).mul
(continuous_snd.comp $ continuous_subtype_val.comp continuous_id)
lemma continuous_rpow_aux2 : continuous (λ p : {p:ℝ×ℝ // p.1 < 0}, p.val.1 ^ p.val.2) :=
suffices h : continuous (λp:{p:ℝ×ℝ // p.1 < 0}, exp (log (-p.val.1) * p.val.2) * cos (p.val.2 * π)),
by { convert h, ext p, rw [rpow_def_of_neg p.2] },
(continuous_exp.comp $
(show continuous $ (λp:{p:ℝ//0<p},
log (p.val))∘(λp:{p:ℝ×ℝ//p.1<0}, ⟨-p.val.1, neg_pos_of_neg p.2⟩),
from continuous_log'.comp $ continuous_subtype_mk _ $ continuous_neg.comp $
continuous_fst.comp continuous_subtype_val).mul
(continuous_snd.comp $ continuous_subtype_val.comp continuous_id)).mul
(continuous_cos.comp $
(continuous_snd.comp $ continuous_subtype_val.comp continuous_id).mul continuous_const)
lemma continuous_at_rpow_of_ne_zero (hx : x ≠ 0) (y : ℝ) :
continuous_at (λp:ℝ×ℝ, p.1^p.2) (x, y) :=
begin
cases lt_trichotomy 0 x,
exact continuous_within_at.continuous_at
(continuous_on_iff_continuous_restrict.2 continuous_rpow_aux1 _ h)
(mem_nhds_sets (by { convert is_open_prod (is_open_lt' (0:ℝ)) is_open_univ, ext, finish }) h),
cases h,
{ exact absurd h.symm hx },
exact continuous_within_at.continuous_at
(continuous_on_iff_continuous_restrict.2 continuous_rpow_aux2 _ h)
(mem_nhds_sets (by { convert is_open_prod (is_open_gt' (0:ℝ)) is_open_univ, ext, finish }) h)
end
lemma continuous_rpow_aux3 : continuous (λ p : {p:ℝ×ℝ // 0 < p.2}, p.val.1 ^ p.val.2) :=
continuous_iff_continuous_at.2 $ λ ⟨(x₀, y₀), hy₀⟩,
begin
by_cases hx₀ : x₀ = 0,
{ simp only [continuous_at, hx₀, zero_rpow (ne_of_gt hy₀), tendsto_nhds_nhds], assume ε ε0,
rcases exists_pos_rat_lt (half_pos hy₀) with ⟨q, q_pos, q_lt⟩,
let q := (q:ℝ), replace q_pos : 0 < q := rat.cast_pos.2 q_pos,
let δ := min (min q (ε ^ (1 / q))) (1/2),
have δ0 : 0 < δ := lt_min (lt_min q_pos (rpow_pos_of_pos ε0 _)) (by norm_num),
have : δ ≤ q := le_trans (min_le_left _ _) (min_le_left _ _),
have : δ ≤ ε ^ (1 / q) := le_trans (min_le_left _ _) (min_le_right _ _),
have : δ < 1 := lt_of_le_of_lt (min_le_right _ _) (by norm_num),
use δ, use δ0, rintros ⟨⟨x, y⟩, hy⟩,
simp only [subtype.dist_eq, real.dist_eq, prod.dist_eq, sub_zero, subtype.coe_mk],
assume h, rw max_lt_iff at h, cases h with xδ yy₀,
have qy : q < y, calc q < y₀ / 2 : q_lt
... = y₀ - y₀ / 2 : (sub_half _).symm
... ≤ y₀ - δ : by linarith
... < y : sub_lt_of_abs_sub_lt_left yy₀,
calc abs(x^y) ≤ abs(x)^y : abs_rpow_le_abs_rpow _ _
... < δ ^ y : rpow_lt_rpow (abs_nonneg _) xδ hy
... < δ ^ q : by { refine rpow_lt_rpow_of_exponent_gt _ _ _, repeat {linarith} }
... ≤ (ε ^ (1 / q)) ^ q : by { refine rpow_le_rpow _ _ _, repeat {linarith} }
... = ε : by { rw [← rpow_mul, div_mul_cancel, rpow_one], exact ne_of_gt q_pos, linarith }},
{ exact (continuous_within_at_iff_continuous_at_restrict (λp:ℝ×ℝ, p.1^p.2) _).1
(continuous_at_rpow_of_ne_zero hx₀ _).continuous_within_at }
end
lemma continuous_at_rpow_of_pos (hy : 0 < y) (x : ℝ) :
continuous_at (λp:ℝ×ℝ, p.1^p.2) (x, y) :=
continuous_within_at.continuous_at
(continuous_on_iff_continuous_restrict.2 continuous_rpow_aux3 _ hy)
(mem_nhds_sets (by { convert is_open_prod is_open_univ (is_open_lt' (0:ℝ)), ext, finish }) hy)
lemma continuous_at_rpow {x y : ℝ} (h : x ≠ 0 ∨ 0 < y) :
continuous_at (λp:ℝ×ℝ, p.1^p.2) (x, y) :=
by { cases h, exact continuous_at_rpow_of_ne_zero h _, exact continuous_at_rpow_of_pos h x }
variables {α : Type*} [topological_space α] {f g : α → ℝ}
/--
`real.rpow` is continuous at all points except for the lower half of the y-axis.
In other words, the function `λp:ℝ×ℝ, p.1^p.2` is continuous at `(x, y)` if `x ≠ 0` or `y > 0`.
Multiple forms of the claim is provided in the current section.
-/
lemma continuous_rpow (h : ∀a, f a ≠ 0 ∨ 0 < g a) (hf : continuous f) (hg : continuous g):
continuous (λa:α, (f a) ^ (g a)) :=
continuous_iff_continuous_at.2 $ λ a,
begin
show continuous_at ((λp:ℝ×ℝ, p.1^p.2) ∘ (λa, (f a, g a))) a,
refine continuous_at.comp _ (continuous_iff_continuous_at.1 (hf.prod_mk hg) _),
{ replace h := h a, cases h,
{ exact continuous_at_rpow_of_ne_zero h _ },
{ exact continuous_at_rpow_of_pos h _ }},
end
lemma continuous_rpow_of_ne_zero (h : ∀a, f a ≠ 0) (hf : continuous f) (hg : continuous g):
continuous (λa:α, (f a) ^ (g a)) := continuous_rpow (λa, or.inl $ h a) hf hg
lemma continuous_rpow_of_pos (h : ∀a, 0 < g a) (hf : continuous f) (hg : continuous g):
continuous (λa:α, (f a) ^ (g a)) := continuous_rpow (λa, or.inr $ h a) hf hg
end prove_rpow_is_continuous
section sqrt
lemma sqrt_eq_rpow : sqrt = λx:ℝ, x ^ (1/(2:ℝ)) :=
begin
funext, by_cases h : 0 ≤ x,
{ rw [← mul_self_inj_of_nonneg, mul_self_sqrt h, ← pow_two, ← rpow_nat_cast, ← rpow_mul h],
norm_num, exact sqrt_nonneg _, exact rpow_nonneg_of_nonneg h _ },
{ replace h : x < 0 := lt_of_not_ge h,
have : 1 / (2:ℝ) * π = π / (2:ℝ), ring,
rw [sqrt_eq_zero_of_nonpos (le_of_lt h), rpow_def_of_neg h, this, cos_pi_div_two, mul_zero] }
end
lemma continuous_sqrt : continuous sqrt :=
by rw sqrt_eq_rpow; exact continuous_rpow_of_pos (λa, by norm_num) continuous_id continuous_const
end sqrt
section exp
/-- The real exponential function tends to +infinity at +infinity -/
lemma tendsto_exp_at_top : tendsto exp at_top at_top :=
begin
have A : tendsto (λx:ℝ, x + 1) at_top at_top :=
tendsto_at_top_add_const_right at_top 1 tendsto_id,
have B : ∀ᶠ x in at_top, x + 1 ≤ exp x,
{ have : ∀ᶠ (x : ℝ) in at_top, 0 ≤ x := mem_at_top 0,
filter_upwards [this],
exact λx hx, add_one_le_exp_of_nonneg hx },
exact tendsto_at_top_mono' at_top B A
end
/-- The real exponential function tends to 0 at -infinity or, equivalently, `exp(-x)` tends to `0`
at +infinity -/
lemma tendsto_exp_neg_at_top_nhds_0 : tendsto (λx, exp (-x)) at_top (𝓝 0) :=
(tendsto_inv_at_top_zero.comp (tendsto_exp_at_top)).congr (λx, (exp_neg x).symm)
/-- The function `exp(x)/x^n` tends to +infinity at +infinity, for any natural number `n` -/
lemma tendsto_exp_div_pow_at_top (n : ℕ) : tendsto (λx, exp x / x^n) at_top at_top :=
begin
have n_pos : (0 : ℝ) < n + 1 := nat.cast_add_one_pos n,
have n_ne_zero : (n : ℝ) + 1 ≠ 0 := ne_of_gt n_pos,
have A : ∀x:ℝ, 0 < x → exp (x / (n+1)) / (n+1)^n ≤ exp x / x^n,
{ assume x hx,
let y := x / (n+1),
have y_pos : 0 < y := div_pos hx n_pos,
have : exp (x / (n+1)) ≤ (n+1)^n * (exp x / x^n), from calc
exp y = exp y * 1 : by simp
... ≤ exp y * (exp y / y)^n : begin
apply mul_le_mul_of_nonneg_left (one_le_pow_of_one_le _ n) (le_of_lt (exp_pos _)),
apply one_le_div_of_le _ y_pos,
apply le_trans _ (add_one_le_exp_of_nonneg (le_of_lt y_pos)),
exact le_add_of_le_of_nonneg (le_refl _) (zero_le_one)
end
... = exp y * exp (n * y) / y^n :
by rw [div_pow, exp_nat_mul, mul_div_assoc]
... = exp ((n + 1) * y) / y^n :
by rw [← exp_add, add_mul, one_mul, add_comm]
... = exp x / (x / (n+1))^n :
by { dsimp [y], rw mul_div_cancel' _ n_ne_zero }
... = (n+1)^n * (exp x / x^n) :
by rw [← mul_div_assoc, div_pow, div_div_eq_mul_div, mul_comm],
rwa div_le_iff' (pow_pos n_pos n) },
have B : ∀ᶠ x in at_top, exp (x / (n+1)) / (n+1)^n ≤ exp x / x^n :=
mem_at_top_sets.2 ⟨1, λx hx, A _ (lt_of_lt_of_le zero_lt_one hx)⟩,
have C : tendsto (λx, exp (x / (n+1)) / (n+1)^n) at_top at_top :=
tendsto_at_top_div (pow_pos n_pos n)
(tendsto_exp_at_top.comp (tendsto_at_top_div (nat.cast_add_one_pos n) tendsto_id)),
exact tendsto_at_top_mono' at_top B C
end
/-- The function `x^n * exp(-x)` tends to `0` at +infinity, for any natural number `n`. -/
lemma tendsto_pow_mul_exp_neg_at_top_nhds_0 (n : ℕ) : tendsto (λx, x^n * exp (-x)) at_top (𝓝 0) :=
(tendsto_inv_at_top_zero.comp (tendsto_exp_div_pow_at_top n)).congr $ λx,
by rw [function.comp_app, inv_eq_one_div, div_div_eq_mul_div, one_mul, div_eq_mul_inv, exp_neg]
end exp
end real
lemma has_deriv_at.rexp {f : ℝ → ℝ} {f' x : ℝ} (hf : has_deriv_at f f' x) :
has_deriv_at (real.exp ∘ f) (real.exp (f x) * f') x :=
(real.has_deriv_at_exp (f x)).comp x hf
lemma has_deriv_within_at.rexp {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ}
(hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (real.exp ∘ f) (real.exp (f x) * f') s x :=
(real.has_deriv_at_exp (f x)).comp_has_deriv_within_at x hf
namespace nnreal
/-- The nonnegative real power function `x^y`, defined for `x : nnreal` and `y : ℝ ` as the
restriction of the real power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`,
one sets `0 ^ 0 = 1` and `0 ^ y = 0` for `y ≠ 0`. -/
noncomputable def rpow (x : nnreal) (y : ℝ) : nnreal :=
⟨(x : ℝ) ^ y, real.rpow_nonneg_of_nonneg x.2 y⟩
noncomputable instance : has_pow nnreal ℝ := ⟨rpow⟩
@[simp] lemma rpow_eq_pow (x : nnreal) (y : ℝ) : rpow x y = x ^ y := rfl
@[simp, norm_cast] lemma coe_rpow (x : nnreal) (y : ℝ) : ((x ^ y : nnreal) : ℝ) = (x : ℝ) ^ y := rfl
@[simp] lemma rpow_zero (x : nnreal) : x ^ (0 : ℝ) = 1 :=
by { rw ← nnreal.coe_eq, exact real.rpow_zero _ }
@[simp] lemma rpow_eq_zero_iff {x : nnreal} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 :=
begin
rw [← nnreal.coe_eq, coe_rpow, ← nnreal.coe_eq_zero],
exact real.rpow_eq_zero_iff_of_nonneg x.2
end
@[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : nnreal) ^ x = 0 :=
by { rw ← nnreal.coe_eq, exact real.zero_rpow h }
@[simp] lemma rpow_one (x : nnreal) : x ^ (1 : ℝ) = x :=
by { rw ← nnreal.coe_eq, exact real.rpow_one _ }
@[simp] lemma one_rpow (x : ℝ) : (1 : nnreal) ^ x = 1 :=
by { rw ← nnreal.coe_eq, exact real.one_rpow _ }
lemma rpow_add {x : nnreal} (y z : ℝ) (hx : 0 < x) : x ^ (y + z) = x ^ y * x ^ z :=
by { rw ← nnreal.coe_eq, exact real.rpow_add _ _ hx }
lemma rpow_mul (x : nnreal) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z :=
by { rw ← nnreal.coe_eq, exact real.rpow_mul x.2 y z }
lemma rpow_neg (x : nnreal) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ :=
by { rw ← nnreal.coe_eq, exact real.rpow_neg x.2 _ }
@[simp] lemma rpow_nat_cast (x : nnreal) (n : ℕ) : x ^ (n : ℝ) = x ^ n :=
by { rw [← nnreal.coe_eq, coe_pow], exact real.rpow_nat_cast (x : ℝ) n }
lemma mul_rpow {x y : nnreal} {z : ℝ} : (x*y)^z = x^z * y^z :=
by { rw ← nnreal.coe_eq, exact real.mul_rpow x.2 y.2 }
lemma one_le_rpow {x : nnreal} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x^z :=
real.one_le_rpow h h₁
lemma rpow_le_rpow {x y : nnreal} {z: ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z :=
real.rpow_le_rpow x.2 h₁ h₂
lemma rpow_lt_rpow {x y : nnreal} {z: ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z :=
real.rpow_lt_rpow x.2 h₁ h₂
lemma rpow_lt_rpow_of_exponent_lt {x : nnreal} {y z : ℝ} (hx : 1 < x) (hyz : y < z) : x^y < x^z :=
real.rpow_lt_rpow_of_exponent_lt hx hyz
lemma rpow_le_rpow_of_exponent_le {x : nnreal} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z :=
real.rpow_le_rpow_of_exponent_le hx hyz
lemma rpow_lt_rpow_of_exponent_gt {x : nnreal} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) :
x^y < x^z :=
real.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz
lemma rpow_le_rpow_of_exponent_ge {x : nnreal} {y z : ℝ} (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) :
x^y ≤ x^z :=
real.rpow_le_rpow_of_exponent_ge hx0 hx1 hyz
lemma rpow_le_one {x : nnreal} {e : ℝ} (he : 0 ≤ e) (hx2 : x ≤ 1) : x^e ≤ 1 :=
real.rpow_le_one he x.2 hx2
lemma one_lt_rpow {x : nnreal} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z :=
real.one_lt_rpow hx hz
lemma rpow_lt_one {x : nnreal} {z : ℝ} (hx : 0 < x) (hx1 : x < 1) (hz : 0 < z) : x^z < 1 :=
real.rpow_lt_one hx hx1 hz
lemma pow_nat_rpow_nat_inv (x : nnreal) {n : ℕ} (hn : 0 < n) :
(x ^ n) ^ (n⁻¹ : ℝ) = x :=
by { rw [← nnreal.coe_eq, coe_rpow, coe_pow], exact real.pow_nat_rpow_nat_inv x.2 hn }
lemma rpow_nat_inv_pow_nat (x : nnreal) {n : ℕ} (hn : 0 < n) :
(x ^ (n⁻¹ : ℝ)) ^ n = x :=
by { rw [← nnreal.coe_eq, coe_pow, coe_rpow], exact real.rpow_nat_inv_pow_nat x.2 hn }
lemma continuous_at_rpow {x : nnreal} {y : ℝ} (h : x ≠ 0 ∨ 0 < y) :
continuous_at (λp:nnreal×ℝ, p.1^p.2) (x, y) :=
begin
have : (λp:nnreal×ℝ, p.1^p.2) = nnreal.of_real ∘ (λp:ℝ×ℝ, p.1^p.2) ∘ (λp:nnreal × ℝ, (p.1.1, p.2)),
{ ext p,
rw [← nnreal.coe_eq, coe_rpow, coe_of_real _ (real.rpow_nonneg_of_nonneg p.1.2 _)],
refl },
rw this,
refine continuous_of_real.continuous_at.comp (continuous_at.comp _ _),
{ apply real.continuous_at_rpow,
simp at h,
rw ← (nnreal.coe_eq_zero x) at h,
exact h },
{ exact ((continuous_subtype_val.comp continuous_fst).prod_mk continuous_snd).continuous_at }
end
end nnreal
lemma filter.tendsto.nnrpow {α : Type*} {f : filter α} {u : α → nnreal} {v : α → ℝ} {x : nnreal} {y : ℝ}
(hx : tendsto u f (𝓝 x)) (hy : tendsto v f (𝓝 y)) (h : x ≠ 0 ∨ 0 < y) :
tendsto (λ a, (u a) ^ (v a)) f (𝓝 (x ^ y)) :=
tendsto.comp (nnreal.continuous_at_rpow h) (tendsto.prod_mk_nhds hx hy)
|
662af1a18106179618f7588c559f1c7f9ab21ea1 | 32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7 | /tests/lean/run/foldConsts.lean | a7bf01e7d445b624f05c9ac16e0ce019c24740df | [
"Apache-2.0"
] | permissive | walterhu1015/lean4 | b2c71b688975177402758924eaa513475ed6ce72 | 2214d81e84646a905d0b20b032c89caf89c737ad | refs/heads/master | 1,671,342,096,906 | 1,599,695,985,000 | 1,599,695,985,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 274 | lean | import Lean
open Lean
def mkTerm : Nat → Expr
| 0 => mkApp (mkConst `a) (mkConst `b)
| n+1 => mkApp (mkTerm n) (mkTerm n)
def collectConsts (e : Expr) : List Name :=
e.foldConsts [] List.cons
def tst1 : IO Unit :=
IO.println $ collectConsts (mkTerm 1000)
#eval tst1
|
789e8cd1fddf1e3986d35edd5b4b98befee9ff25 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/group_theory/free_abelian_group_finsupp.lean | 0bbc42f979be511d558d73d611bc6c62b31a53d0 | [
"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 | 6,994 | lean | /-
Copyright (c) 2021 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import group_theory.free_abelian_group
import group_theory.is_free_group
import data.finsupp.basic
import data.equiv.module
import linear_algebra.dimension
/-!
# Isomorphism between `free_abelian_group X` and `X →₀ ℤ`
In this file we construct the canonical isomorphism between `free_abelian_group X` and `X →₀ ℤ`.
We use this to transport the notion of `support` from `finsupp` to `free_abelian_group`.
## Main declarations
- `free_abelian_group.equiv_finsupp`: group isomorphism between `free_abelian_group X` and `X →₀ ℤ`
- `free_abelian_group.coeff`: the multiplicity of `x : X` in `a : free_abelian_group X`
- `free_abelian_group.support`: the finset of `x : X` that occur in `a : free_abelian_group X`
-/
noncomputable theory
open_locale big_operators
variables {X : Type*}
/-- The group homomorphism `free_abelian_group X →+ (X →₀ ℤ)`. -/
def free_abelian_group.to_finsupp : free_abelian_group X →+ (X →₀ ℤ) :=
free_abelian_group.lift $ λ x, finsupp.single x (1 : ℤ)
/-- The group homomorphism `(X →₀ ℤ) →+ free_abelian_group X`. -/
def finsupp.to_free_abelian_group : (X →₀ ℤ) →+ free_abelian_group X :=
finsupp.lift_add_hom $ λ x, (smul_add_hom ℤ (free_abelian_group X)).flip (free_abelian_group.of x)
open finsupp free_abelian_group
@[simp] lemma finsupp.to_free_abelian_group_comp_single_add_hom (x : X) :
finsupp.to_free_abelian_group.comp (finsupp.single_add_hom x) =
(smul_add_hom ℤ (free_abelian_group X)).flip (of x) :=
begin
ext,
simp only [add_monoid_hom.coe_comp, finsupp.single_add_hom_apply, function.comp_app,
one_smul, to_free_abelian_group, finsupp.lift_add_hom_apply_single]
end
@[simp] lemma free_abelian_group.to_finsupp_comp_to_free_abelian_group :
to_finsupp.comp to_free_abelian_group = add_monoid_hom.id (X →₀ ℤ) :=
begin
ext x y, simp only [add_monoid_hom.id_comp],
rw [add_monoid_hom.comp_assoc, finsupp.to_free_abelian_group_comp_single_add_hom],
simp only [to_finsupp, add_monoid_hom.coe_comp, finsupp.single_add_hom_apply,
function.comp_app, one_smul, lift.of, add_monoid_hom.flip_apply,
smul_add_hom_apply, add_monoid_hom.id_apply],
end
@[simp] lemma finsupp.to_free_abelian_group_comp_to_finsupp :
to_free_abelian_group.comp to_finsupp = add_monoid_hom.id (free_abelian_group X) :=
begin
ext,
rw [to_free_abelian_group, to_finsupp, add_monoid_hom.comp_apply, lift.of,
lift_add_hom_apply_single, add_monoid_hom.flip_apply, smul_add_hom_apply, one_smul,
add_monoid_hom.id_apply],
end
@[simp] lemma finsupp.to_free_abelian_group_to_finsupp {X} (x : free_abelian_group X) :
x.to_finsupp.to_free_abelian_group = x :=
by rw [← add_monoid_hom.comp_apply, finsupp.to_free_abelian_group_comp_to_finsupp,
add_monoid_hom.id_apply]
namespace free_abelian_group
open finsupp
variable {X}
@[simp] lemma to_finsupp_of (x : X) :
to_finsupp (of x) = finsupp.single x 1 :=
by simp only [to_finsupp, lift.of]
@[simp] lemma to_finsupp_to_free_abelian_group (f : X →₀ ℤ) :
f.to_free_abelian_group.to_finsupp = f :=
by rw [← add_monoid_hom.comp_apply, to_finsupp_comp_to_free_abelian_group, add_monoid_hom.id_apply]
variable (X)
/-- The additive equivalence between `free_abelian_group X` and `(X →₀ ℤ)`. -/
@[simps]
def equiv_finsupp : free_abelian_group X ≃+ (X →₀ ℤ) :=
{ to_fun := to_finsupp,
inv_fun := to_free_abelian_group,
left_inv := to_free_abelian_group_to_finsupp,
right_inv := to_finsupp_to_free_abelian_group,
map_add' := to_finsupp.map_add }
/-- `A` is a basis of the ℤ-module `free_abelian_group A`. -/
noncomputable def basis (α : Type*) :
basis α ℤ (free_abelian_group α) :=
⟨(free_abelian_group.equiv_finsupp α).to_int_linear_equiv ⟩
/-- Isomorphic free ablian groups (as modules) have equivalent bases. -/
def equiv.of_free_abelian_group_linear_equiv {α β : Type*}
(e : free_abelian_group α ≃ₗ[ℤ] free_abelian_group β) :
α ≃ β :=
let t : _root_.basis α ℤ (free_abelian_group β) := (free_abelian_group.basis α).map e
in t.index_equiv $ free_abelian_group.basis _
/-- Isomorphic free abelian groups (as additive groups) have equivalent bases. -/
def equiv.of_free_abelian_group_equiv {α β : Type*}
(e : free_abelian_group α ≃+ free_abelian_group β) :
α ≃ β :=
equiv.of_free_abelian_group_linear_equiv e.to_int_linear_equiv
/-- Isomorphic free groups have equivalent bases. -/
def equiv.of_free_group_equiv {α β : Type*}
(e : free_group α ≃* free_group β) :
α ≃ β :=
equiv.of_free_abelian_group_equiv e.abelianization_congr.to_additive
open is_free_group
/-- Isomorphic free groups have equivalent bases (`is_free_group` variant`). -/
def equiv.of_is_free_group_equiv {G H : Type*}
[group G] [group H] [is_free_group G] [is_free_group H]
(e : G ≃* H) :
generators G ≃ generators H :=
equiv.of_free_group_equiv $
mul_equiv.trans ((to_free_group G).symm) $
mul_equiv.trans e $
to_free_group H
variable {X}
/-- `coeff x` is the additive group homomorphism `free_abelian_group X →+ ℤ`
that sends `a` to the multiplicity of `x : X` in `a`. -/
def coeff (x : X) : free_abelian_group X →+ ℤ :=
(finsupp.apply_add_hom x).comp to_finsupp
/-- `support a` for `a : free_abelian_group X` is the finite set of `x : X`
that occur in the formal sum `a`. -/
def support (a : free_abelian_group X) : finset X :=
a.to_finsupp.support
lemma mem_support_iff (x : X) (a : free_abelian_group X) :
x ∈ a.support ↔ coeff x a ≠ 0 :=
by { rw [support, finsupp.mem_support_iff], exact iff.rfl }
lemma not_mem_support_iff (x : X) (a : free_abelian_group X) :
x ∉ a.support ↔ coeff x a = 0 :=
by { rw [support, finsupp.not_mem_support_iff], exact iff.rfl }
@[simp] lemma support_zero : support (0 : free_abelian_group X) = ∅ :=
by simp only [support, finsupp.support_zero, add_monoid_hom.map_zero]
@[simp] lemma support_of (x : X) : support (of x) = {x} :=
by simp only [support, to_finsupp_of, finsupp.support_single_ne_zero (one_ne_zero)]
@[simp] lemma support_neg (a : free_abelian_group X) : support (-a) = support a :=
by simp only [support, add_monoid_hom.map_neg, finsupp.support_neg]
@[simp] lemma support_zsmul (k : ℤ) (h : k ≠ 0) (a : free_abelian_group X) :
support (k • a) = support a :=
begin
ext x,
simp only [mem_support_iff, add_monoid_hom.map_zsmul],
simp only [h, zsmul_int_int, false_or, ne.def, mul_eq_zero]
end
@[simp] lemma support_nsmul (k : ℕ) (h : k ≠ 0) (a : free_abelian_group X) :
support (k • a) = support a :=
by { apply support_zsmul k _ a, exact_mod_cast h }
open_locale classical
lemma support_add (a b : free_abelian_group X) : (support (a + b)) ⊆ a.support ∪ b.support :=
begin
simp only [support, add_monoid_hom.map_add],
apply finsupp.support_add
end
end free_abelian_group
|
c007a8c67a432a02cfda5bba609ee4a89b7a1b90 | 8e6cad62ec62c6c348e5faaa3c3f2079012bdd69 | /src/topology/separation.lean | 9906a1bdfeff8fbdec2a59d4e11b373bb916ce3a | [
"Apache-2.0"
] | permissive | benjamindavidson/mathlib | 8cc81c865aa8e7cf4462245f58d35ae9a56b150d | fad44b9f670670d87c8e25ff9cdf63af87ad731e | refs/heads/master | 1,679,545,578,362 | 1,615,343,014,000 | 1,615,343,014,000 | 312,926,983 | 0 | 0 | Apache-2.0 | 1,615,360,301,000 | 1,605,399,418,000 | Lean | UTF-8 | Lean | false | false | 42,633 | 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.subset_properties
import topology.connected
open set filter
open_locale topological_space filter
local attribute [instance] classical.prop_decidable -- TODO: use "open_locale classical"
universes u v
variables {α : Type u} {β : Type v} [topological_space α]
section separation
/--
`separated` is a predicate on pairs of sub`set`s of a topological space. It holds if the two
sub`set`s are contained in disjoint open sets.
-/
def separated : set α → set α → Prop :=
λ (s t : set α), ∃ U V : (set α), (is_open U) ∧ is_open V ∧
(s ⊆ U) ∧ (t ⊆ V) ∧ disjoint U V
namespace separated
open separated
@[symm] lemma symm {s t : set α} : separated s t → separated t s :=
λ ⟨U, V, oU, oV, aU, bV, UV⟩, ⟨V, U, oV, oU, bV, aU, disjoint.symm UV⟩
lemma comm (s t : set α) : separated s t ↔ separated t s :=
⟨symm, symm⟩
lemma empty_right (a : set α) : separated a ∅ :=
⟨_, _, is_open_univ, is_open_empty, λ a h, mem_univ a, λ a h, by cases h, disjoint_empty _⟩
lemma empty_left (a : set α) : separated ∅ a :=
(empty_right _).symm
lemma union_left {a b c : set α} : separated a c → separated b c → separated (a ∪ b) c :=
λ ⟨U, V, oU, oV, aU, bV, UV⟩ ⟨W, X, oW, oX, aW, bX, WX⟩,
⟨U ∪ W, V ∩ X, is_open_union oU oW, is_open_inter oV oX,
union_subset_union aU aW, subset_inter bV bX, set.disjoint_union_left.mpr
⟨disjoint_of_subset_right (inter_subset_left _ _) UV,
disjoint_of_subset_right (inter_subset_right _ _) WX⟩⟩
lemma union_right {a b c : set α} (ab : separated a b) (ac : separated a c) :
separated a (b ∪ c) :=
(ab.symm.union_left ac.symm).symm
end separated
/-- 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 is_closed.exists_closed_singleton {α : Type*} [topological_space α]
[t0_space α] [compact_space α] {S : set α} (hS : is_closed S) (hne : S.nonempty) :
∃ (x : α), x ∈ S ∧ is_closed ({x} : set α) :=
begin
obtain ⟨V, Vsub, Vne, Vcls, hV⟩ := hS.exists_minimal_nonempty_closed_subset hne,
by_cases hnt : ∃ (x y : α) (hx : x ∈ V) (hy : y ∈ V), x ≠ y,
{ exfalso,
obtain ⟨x, y, hx, hy, hne⟩ := hnt,
obtain ⟨U, hU, hsep⟩ := t0_space.t0 _ _ hne,
have : ∀ (z w : α) (hz : z ∈ V) (hw : w ∈ V) (hz' : z ∈ U) (hw' : ¬ w ∈ U), false,
{ intros z w hz hw hz' hw',
have uvne : (V ∩ Uᶜ).nonempty,
{ use w, simp only [hw, hw', set.mem_inter_eq, not_false_iff, and_self, set.mem_compl_eq], },
specialize hV (V ∩ Uᶜ) (set.inter_subset_left _ _) uvne
(is_closed_inter Vcls (is_closed_compl_iff.mpr hU)),
have : V ⊆ Uᶜ,
{ rw ←hV, exact set.inter_subset_right _ _ },
exact this hz hz', },
cases hsep,
{ exact this x y hx hy hsep.1 hsep.2 },
{ exact this y x hy hx hsep.1 hsep.2 } },
{ push_neg at hnt,
obtain ⟨z, hz⟩ := Vne,
refine ⟨z, Vsub hz, _⟩,
convert Vcls,
ext,
simp only [set.mem_singleton_iff, set.mem_compl_eq],
split,
{ rintro rfl, exact hz, },
{ exact λ hx, hnt x z hx hz, }, },
end
theorem exists_open_singleton_of_open_finset [t0_space α] (s : finset α) (sne : s.nonempty)
(hso : is_open (↑s : set α)) :
∃ x ∈ s, is_open ({x} : set α):=
begin
induction s using finset.strong_induction_on with s ihs,
by_cases hs : set.subsingleton (↑s : set α),
{ rcases sne with ⟨x, hx⟩,
refine ⟨x, hx, _⟩,
have : (↑s : set α) = {x}, from hs.eq_singleton_of_mem hx,
rwa this at hso },
{ dunfold set.subsingleton at hs,
push_neg at hs,
rcases hs with ⟨x, hx, y, hy, hxy⟩,
rcases t0_space.t0 x y hxy with ⟨U, hU, hxyU⟩,
wlog H : x ∈ U ∧ y ∉ U := hxyU using [x y, y x],
obtain ⟨z, hzs, hz⟩ : ∃ z ∈ s.filter (λ z, z ∈ U), is_open ({z} : set α),
{ refine ihs _ (finset.filter_ssubset.2 ⟨y, hy, H.2⟩) ⟨x, finset.mem_filter.2 ⟨hx, H.1⟩⟩ _,
rw [finset.coe_filter],
exact is_open_inter hso hU },
exact ⟨z, (finset.mem_filter.1 hzs).1, hz⟩ }
end
theorem exists_open_singleton_of_fintype [t0_space α] [f : fintype α] [ha : nonempty α] :
∃ x:α, is_open ({x}:set α) :=
begin
refine ha.elim (λ x, _),
have : is_open (↑(finset.univ : finset α) : set α), { simp },
rcases exists_open_singleton_of_open_finset _ ⟨x, finset.mem_univ x⟩ this with ⟨x, _, hx⟩,
exact ⟨x, hx⟩
end
instance subtype.t0_space [t0_space α] {p : α → Prop} : t0_space (subtype p) :=
⟨λ x y hxy, let ⟨U, hU, hxyU⟩ := t0_space.t0 (x:α) y ((not_congr subtype.ext_iff_val).1 hxy) in
⟨(coe : subtype p → α) ⁻¹' U, is_open_induced hU, hxyU⟩⟩
/-- 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
lemma is_open_compl_singleton [t1_space α] {x : α} : is_open ({x}ᶜ : set α) :=
is_closed_singleton
lemma is_open_ne [t1_space α] {x : α} : is_open {y | y ≠ x} :=
is_open_compl_singleton
instance subtype.t1_space {α : Type u} [topological_space α] [t1_space α] {p : α → Prop} :
t1_space (subtype p) :=
⟨λ ⟨x, hx⟩, is_closed_induced_iff.2 $ ⟨{x}, is_closed_singleton, set.ext $ λ y,
by simp [subtype.ext_iff_val]⟩⟩
@[priority 100] -- see Note [lower instance priority]
instance t1_space.t0_space [t1_space α] : t0_space α :=
⟨λ x y h, ⟨{z | z ≠ y}, is_open_ne, or.inl ⟨h, not_not_intro rfl⟩⟩⟩
lemma compl_singleton_mem_nhds [t1_space α] {x y : α} (h : y ≠ x) : {x}ᶜ ∈ 𝓝 y :=
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} :=
is_closed_singleton.closure_eq
lemma is_closed_map_const {α β} [topological_space α] [topological_space β] [t1_space β] {y : β} :
is_closed_map (function.const α y) :=
begin
apply is_closed_map.of_nonempty, intros s hs h2s, simp_rw [h2s.image_const, is_closed_singleton]
end
lemma discrete_of_t1_of_finite {X : Type*} [topological_space X] [t1_space X] [fintype X] :
discrete_topology X :=
begin
apply singletons_open_iff_discrete.mp,
intros x,
rw [← is_closed_compl_iff, ← bUnion_of_singleton ({x} : set X)ᶜ],
exact is_closed_bUnion (finite.of_fintype _) (λ y _, is_closed_singleton)
end
lemma singleton_mem_nhds_within_of_mem_discrete {s : set α} [discrete_topology s]
{x : α} (hx : x ∈ s) :
{x} ∈ 𝓝[s] x :=
begin
have : ({⟨x, hx⟩} : set s) ∈ 𝓝 (⟨x, hx⟩ : s), by simp [nhds_discrete],
simpa only [nhds_within_eq_map_subtype_coe hx, image_singleton]
using @image_mem_map _ _ _ (coe : s → α) _ this
end
lemma nhds_within_of_mem_discrete {s : set α} [discrete_topology s] {x : α} (hx : x ∈ s) :
𝓝[s] x = pure x :=
le_antisymm (le_pure_iff.2 $ singleton_mem_nhds_within_of_mem_discrete hx) (pure_le_nhds_within hx)
lemma filter.has_basis.exists_inter_eq_singleton_of_mem_discrete
{ι : Type*} {p : ι → Prop} {t : ι → set α} {s : set α} [discrete_topology s] {x : α}
(hb : (𝓝 x).has_basis p t) (hx : x ∈ s) :
∃ i (hi : p i), t i ∩ s = {x} :=
begin
rcases (nhds_within_has_basis hb s).mem_iff.1 (singleton_mem_nhds_within_of_mem_discrete hx)
with ⟨i, hi, hix⟩,
exact ⟨i, hi, subset.antisymm hix $ singleton_subset_iff.2 ⟨mem_of_nhds $ hb.mem_of_mem hi, hx⟩⟩
end
/-- A point `x` in a discrete subset `s` of a topological space admits a neighbourhood
that only meets `s` at `x`. -/
lemma nhds_inter_eq_singleton_of_mem_discrete {s : set α} [discrete_topology s]
{x : α} (hx : x ∈ s) :
∃ U ∈ 𝓝 x, U ∩ s = {x} :=
by simpa using (𝓝 x).basis_sets.exists_inter_eq_singleton_of_mem_discrete hx
/-- For point `x` in a discrete subset `s` of a topological space, there is a set `U`
such that
1. `U` is a punctured neighborhood of `x` (ie. `U ∪ {x}` is a neighbourhood of `x`),
2. `U` is disjoint from `s`.
-/
lemma disjoint_nhds_within_of_mem_discrete {s : set α} [discrete_topology s] {x : α} (hx : x ∈ s) :
∃ U ∈ 𝓝[{x}ᶜ] x, disjoint U s :=
let ⟨V, h, h'⟩ := nhds_inter_eq_singleton_of_mem_discrete hx in
⟨{x}ᶜ ∩ V, inter_mem_nhds_within _ h,
(disjoint_iff_inter_eq_empty.mpr (by { rw [inter_assoc, h', compl_inter_self] }))⟩
/-- Let `X` be a topological space and let `s, t ⊆ X` be two subsets. If there is an inclusion
`t ⊆ s`, then the topological space structure on `t` induced by `X` is the same as the one
obtained by the induced topological space structure on `s`. -/
lemma topological_space.subset_trans {X : Type*} [tX : topological_space X]
{s t : set X} (ts : t ⊆ s) :
(subtype.topological_space : topological_space t) =
(subtype.topological_space : topological_space s).induced (set.inclusion ts) :=
begin
change tX.induced ((coe : s → X) ∘ (set.inclusion ts)) =
topological_space.induced (set.inclusion ts) (tX.induced _),
rw ← induced_compose,
end
/-- This lemma characterizes discrete topological spaces as those whose singletons are
neighbourhoods. -/
lemma discrete_topology_iff_nhds {X : Type*} [topological_space X] :
discrete_topology X ↔ (nhds : X → filter X) = pure :=
begin
split,
{ introI hX,
exact nhds_discrete X },
{ intro h,
constructor,
apply eq_of_nhds_eq_nhds,
simp [h, nhds_bot] }
end
/-- The topology pulled-back under an inclusion `f : X → Y` from the discrete topology (`⊥`) is the
discrete topology.
This version does not assume the choice of a topology on either the source `X`
nor the target `Y` of the inclusion `f`. -/
lemma induced_bot {X Y : Type*} {f : X → Y} (hf : function.injective f) :
topological_space.induced f ⊥ = ⊥ :=
eq_of_nhds_eq_nhds (by simp [nhds_induced, ← set.image_singleton, hf.preimage_image, nhds_bot])
/-- The topology induced under an inclusion `f : X → Y` from the discrete topological space `Y`
is the discrete topology on `X`. -/
lemma discrete_topology_induced {X Y : Type*} [tY : topological_space Y] [discrete_topology Y]
{f : X → Y} (hf : function.injective f) : @discrete_topology X (topological_space.induced f tY) :=
begin
constructor,
rw discrete_topology.eq_bot Y,
exact induced_bot hf
end
/-- Let `s, t ⊆ X` be two subsets of a topological space `X`. If `t ⊆ s` and the topology induced
by `X`on `s` is discrete, then also the topology induces on `t` is discrete. -/
lemma discrete_topology.of_subset {X : Type*} [topological_space X] {s t : set X}
(ds : discrete_topology s) (ts : t ⊆ s) :
discrete_topology t :=
begin
rw [topological_space.subset_trans ts, ds.eq_bot],
exact {eq_bot := induced_bot (set.inclusion_injective ts)}
end
/-- 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
@[priority 100] -- see Note [lower instance priority]
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_ne_bot [ht : t2_space α] {x y : α} (h : ne_bot (𝓝 x ⊓ 𝓝 y)) : x = y :=
classical.by_contradiction $ assume : x ≠ y,
let ⟨u, v, hu, hv, hx, hy, huv⟩ := t2_space.t2 x y this in
absurd huv $ (inf_ne_bot_iff.1 h (mem_nhds_sets hu hx) (mem_nhds_sets hv hy)).ne_empty
lemma t2_iff_nhds : t2_space α ↔ ∀ {x y : α}, ne_bot (𝓝 x ⊓ 𝓝 y) → x = y :=
⟨assume h, by exactI λ x y, eq_of_nhds_ne_bot,
assume h, ⟨assume x y xy,
have 𝓝 x ⊓ 𝓝 y = ⊥ := not_ne_bot.1 $ 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 α ↔ ∀ {x y : α} (f : ultrafilter α), ↑f ≤ 𝓝 x → ↑f ≤ 𝓝 y → x = y :=
t2_iff_nhds.trans $ by simp only [←exists_ultrafilter_iff, and_imp, le_inf_iff, exists_imp_distrib]
lemma is_closed_diagonal [t2_space α] : is_closed (diagonal α) :=
begin
refine is_closed_iff_cluster_pt.mpr _,
rintro ⟨a₁, a₂⟩ h,
refine eq_of_nhds_ne_bot ⟨λ this : 𝓝 a₁ ⊓ 𝓝 a₂ = ⊥, h.ne _⟩,
obtain ⟨t₁, (ht₁ : t₁ ∈ 𝓝 a₁), t₂, (ht₂ : t₂ ∈ 𝓝 a₂), (h' : t₁ ∩ t₂ ⊆ ∅)⟩ :=
by rw [←empty_in_sets_eq_bot, mem_inf_sets] at this; exact this,
rw [nhds_prod_eq, ←empty_in_sets_eq_bot],
apply filter.sets_of_superset,
apply inter_mem_inf_sets (prod_mem_prod ht₁ ht₂) (mem_principal_sets.mpr (subset.refl _)),
exact assume ⟨x₁, x₂⟩ ⟨⟨hx₁, hx₂⟩, (heq : x₁ = x₂)⟩,
show false, from @h' x₁ ⟨hx₁, heq.symm ▸ hx₂⟩
end
lemma t2_iff_is_closed_diagonal : t2_space α ↔ is_closed (diagonal α) :=
begin
split,
{ introI h,
exact is_closed_diagonal },
{ intro h,
constructor,
intros x y hxy,
have : (x, y) ∈ (diagonal α)ᶜ, by rwa [mem_compl_iff],
obtain ⟨t, t_sub, t_op, xyt⟩ : ∃ t ⊆ (diagonal α)ᶜ, is_open t ∧ (x, y) ∈ t :=
is_open_iff_forall_mem_open.mp h _ this,
rcases is_open_prod_iff.mp t_op x y xyt with ⟨U, V, U_op, V_op, xU, yV, H⟩,
use [U, V, U_op, V_op, xU, yV],
have := subset.trans H t_sub,
rw eq_empty_iff_forall_not_mem,
rintros z ⟨zU, zV⟩,
have : ¬ (z, z) ∈ diagonal α := this (mk_mem_prod zU zV),
exact this rfl },
end
section separated
open separated finset
lemma finset_disjoint_finset_opens_of_t2 [t2_space α] :
∀ (s t : finset α), disjoint s t → separated (s : set α) t :=
begin
refine induction_on_union _ (λ a b hi d, (hi d.symm).symm) (λ a d, empty_right a) (λ a b ab, _) _,
{ obtain ⟨U, V, oU, oV, aU, bV, UV⟩ := t2_separation
(by { rw [ne.def, ← finset.mem_singleton], exact (disjoint_singleton.mp ab.symm) }),
refine ⟨U, V, oU, oV, _, _, set.disjoint_iff_inter_eq_empty.mpr UV⟩;
exact singleton_subset_set_iff.mpr ‹_› },
{ intros a b c ac bc d,
apply_mod_cast union_left (ac (disjoint_of_subset_left (a.subset_union_left b) d)) (bc _),
exact disjoint_of_subset_left (a.subset_union_right b) d },
end
lemma point_disjoint_finset_opens_of_t2 [t2_space α] {x : α} {s : finset α} (h : x ∉ s) :
separated ({x} : set α) ↑s :=
by exact_mod_cast finset_disjoint_finset_opens_of_t2 {x} s (singleton_disjoint.mpr h)
end separated
@[simp] lemma nhds_eq_nhds_iff {a b : α} [t2_space α] : 𝓝 a = 𝓝 b ↔ a = b :=
⟨assume h, eq_of_nhds_ne_bot $ by rw [h, inf_idem]; exact nhds_ne_bot, assume h, h ▸ rfl⟩
@[simp] lemma nhds_le_nhds_iff {a b : α} [t2_space α] : 𝓝 a ≤ 𝓝 b ↔ a = b :=
⟨assume h, eq_of_nhds_ne_bot $ by rw [inf_of_le_left h]; exact nhds_ne_bot, assume h, h ▸ le_refl _⟩
lemma tendsto_nhds_unique [t2_space α] {f : β → α} {l : filter β} {a b : α}
[ne_bot l] (ha : tendsto f l (𝓝 a)) (hb : tendsto f l (𝓝 b)) : a = b :=
eq_of_nhds_ne_bot $ ne_bot_of_le $ le_inf ha hb
lemma tendsto_nhds_unique' [t2_space α] {f : β → α} {l : filter β} {a b : α}
(hl : ne_bot l) (ha : tendsto f l (𝓝 a)) (hb : tendsto f l (𝓝 b)) : a = b :=
eq_of_nhds_ne_bot $ ne_bot_of_le $ le_inf ha hb
section lim
variables [t2_space α] {f : filter α}
/-!
### Properties of `Lim` and `lim`
In this section we use explicit `nonempty α` instances for `Lim` and `lim`. This way the lemmas
are useful without a `nonempty α` instance.
-/
lemma Lim_eq {a : α} [ne_bot f] (h : f ≤ 𝓝 a) :
@Lim _ _ ⟨a⟩ f = a :=
tendsto_nhds_unique (le_nhds_Lim ⟨a, h⟩) h
lemma Lim_eq_iff [ne_bot f] (h : ∃ (a : α), f ≤ nhds a) {a} : @Lim _ _ ⟨a⟩ f = a ↔ f ≤ 𝓝 a :=
⟨λ c, c ▸ le_nhds_Lim h, Lim_eq⟩
lemma ultrafilter.Lim_eq_iff_le_nhds [compact_space α] {x : α} {F : ultrafilter α} :
F.Lim = x ↔ ↑F ≤ 𝓝 x :=
⟨λ h, h ▸ F.le_nhds_Lim, Lim_eq⟩
lemma is_open_iff_ultrafilter' [compact_space α] (U : set α) :
is_open U ↔ (∀ F : ultrafilter α, F.Lim ∈ U → U ∈ F.1) :=
begin
rw is_open_iff_ultrafilter,
refine ⟨λ h F hF, h F.Lim hF F F.le_nhds_Lim, _⟩,
intros cond x hx f h,
rw [← (ultrafilter.Lim_eq_iff_le_nhds.2 h)] at hx,
exact cond _ hx
end
lemma filter.tendsto.lim_eq {a : α} {f : filter β} [ne_bot f] {g : β → α} (h : tendsto g f (𝓝 a)) :
@lim _ _ _ ⟨a⟩ f g = a :=
Lim_eq h
lemma filter.lim_eq_iff {f : filter β} [ne_bot f] {g : β → α} (h : ∃ a, tendsto g f (𝓝 a)) {a} :
@lim _ _ _ ⟨a⟩ f g = a ↔ tendsto g f (𝓝 a) :=
⟨λ c, c ▸ tendsto_nhds_lim h, filter.tendsto.lim_eq⟩
lemma continuous.lim_eq [topological_space β] {f : β → α} (h : continuous f) (a : β) :
@lim _ _ _ ⟨f a⟩ (𝓝 a) f = f a :=
(h.tendsto a).lim_eq
@[simp] lemma Lim_nhds (a : α) : @Lim _ _ ⟨a⟩ (𝓝 a) = a :=
Lim_eq (le_refl _)
@[simp] lemma lim_nhds_id (a : α) : @lim _ _ _ ⟨a⟩ (𝓝 a) id = a :=
Lim_nhds a
@[simp] lemma Lim_nhds_within {a : α} {s : set α} (h : a ∈ closure s) :
@Lim _ _ ⟨a⟩ (𝓝[s] a) = a :=
by haveI : ne_bot (𝓝[s] a) := mem_closure_iff_cluster_pt.1 h;
exact Lim_eq inf_le_left
@[simp] lemma lim_nhds_within_id {a : α} {s : set α} (h : a ∈ closure s) :
@lim _ _ _ ⟨a⟩ (𝓝[s] a) id = a :=
Lim_nhds_within h
end lim
/-!
### Instances of `t2_space` typeclass
We use two lemmas to prove that various standard constructions generate Hausdorff spaces from
Hausdorff spaces:
* `separated_by_continuous` says that two points `x y : α` can be separated by open neighborhoods
provided that there exists a continuous map `f`: α → β` with a Hausdorff codomain such that
`f x ≠ f y`. We use this lemma to prove that topological spaces defined using `induced` are
Hausdorff spaces.
* `separated_by_open_embedding` says that for an open embedding `f : α → β` of a Hausdorff space
`α`, the images of two distinct points `x y : α`, `x ≠ y` can be separated by open neighborhoods.
We use this lemma to prove that topological spaces defined using `coinduced` are Hausdorff spaces.
-/
@[priority 100] -- see Note [lower instance priority]
instance t2_space_discrete {α : Type*} [topological_space α] [discrete_topology α] : t2_space α :=
{ t2 := assume x y hxy, ⟨{x}, {y}, is_open_discrete _, is_open_discrete _, rfl, rfl,
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⟩ }
lemma separated_by_continuous {α : Type*} {β : Type*}
[topological_space α] [topological_space β] [t2_space β]
{f : α → β} (hf : continuous f) {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, uo.preimage hf, vo.preimage hf, xu, yv,
by rw [←preimage_inter, uv, preimage_empty]⟩
lemma separated_by_open_embedding {α β : Type*} [topological_space α] [topological_space β]
[t2_space α] {f : α → β} (hf : open_embedding f) {x y : α} (h : x ≠ y) :
∃ u v : set β, is_open u ∧ is_open v ∧ f x ∈ u ∧ f y ∈ v ∧ u ∩ v = ∅ :=
let ⟨u, v, uo, vo, xu, yv, uv⟩ := t2_separation h in
⟨f '' u, f '' v, hf.is_open_map _ uo, hf.is_open_map _ vo,
mem_image_of_mem _ xu, mem_image_of_mem _ yv, by rw [image_inter hf.inj, uv, image_empty]⟩
instance {α : Type*} {p : α → Prop} [t : topological_space α] [t2_space α] : t2_space (subtype p) :=
⟨assume x y h, separated_by_continuous continuous_subtype_val (mt subtype.eq h)⟩
instance {α : Type*} {β : Type*} [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_continuous continuous_fst h₁)
(λ h₂, separated_by_continuous continuous_snd h₂)⟩
instance {α : Type*} {β : Type*} [t₁ : topological_space α] [t2_space α]
[t₂ : topological_space β] [t2_space β] : t2_space (α ⊕ β) :=
begin
constructor,
rintros (x|x) (y|y) h,
{ replace h : x ≠ y := λ c, (c.subst h) rfl,
exact separated_by_open_embedding open_embedding_inl h },
{ exact ⟨_, _, is_open_range_inl, is_open_range_inr, ⟨x, rfl⟩, ⟨y, rfl⟩,
range_inl_inter_range_inr⟩ },
{ exact ⟨_, _, is_open_range_inr, is_open_range_inl, ⟨x, rfl⟩, ⟨y, rfl⟩,
range_inr_inter_range_inl⟩ },
{ replace h : x ≠ y := λ c, (c.subst h) rfl,
exact separated_by_open_embedding open_embedding_inr h }
end
instance Pi.t2_space {α : Type*} {β : α → 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_continuous (continuous_apply i) hi⟩
instance sigma.t2_space {ι : Type*} {α : ι → Type*} [Πi, topological_space (α i)]
[∀a, t2_space (α a)] :
t2_space (Σi, α i) :=
begin
constructor,
rintros ⟨i, x⟩ ⟨j, y⟩ neq,
rcases em (i = j) with (rfl|h),
{ replace neq : x ≠ y := λ c, (c.subst neq) rfl,
exact separated_by_open_embedding open_embedding_sigma_mk neq },
{ exact ⟨_, _, is_open_range_sigma_mk, is_open_range_sigma_mk, ⟨x, rfl⟩, ⟨y, rfl⟩, by tidy⟩ }
end
variables [topological_space β]
lemma is_closed_eq [t2_space α] {f g : β → α}
(hf : continuous f) (hg : continuous g) : is_closed {x:β | f x = g x} :=
continuous_iff_is_closed.mp (hf.prod_mk hg) _ is_closed_diagonal
/-- If two continuous maps are equal on `s`, then they are equal on the closure of `s`. -/
lemma set.eq_on.closure [t2_space α] {s : set β} {f g : β → α} (h : eq_on f g s)
(hf : continuous f) (hg : continuous g) :
eq_on f g (closure s) :=
closure_minimal h (is_closed_eq hf hg)
/-- If two continuous functions are equal on a dense set, then they are equal. -/
lemma continuous.ext_on [t2_space α] {s : set β} (hs : dense s) {f g : β → α}
(hf : continuous f) (hg : continuous g) (h : eq_on f g s) :
f = g :=
funext $ λ x, h.closure hf hg (hs x)
lemma function.left_inverse.closed_range [t2_space α] {f : α → β} {g : β → α}
(h : function.left_inverse f g) (hf : continuous f) (hg : continuous g) :
is_closed (range g) :=
have eq_on (g ∘ f) id (closure $ range g),
from h.right_inv_on_range.eq_on.closure (hg.comp hf) continuous_id,
is_closed_of_closure_subset $ λ x hx,
calc x = g (f x) : (this hx).symm
... ∈ _ : mem_range_self _
lemma function.left_inverse.closed_embedding [t2_space α] {f : α → β} {g : β → α}
(h : function.left_inverse f g) (hf : continuous f) (hg : continuous g) :
closed_embedding g :=
⟨h.embedding hf hg, h.closed_range hf hg⟩
lemma diagonal_eq_range_diagonal_map {α : Type*} : {p:α×α | p.1 = p.2} = range (λx, (x,x)) :=
ext $ assume p, iff.intro
(assume h, ⟨p.1, prod.ext_iff.2 ⟨rfl, h⟩⟩)
(assume ⟨x, hx⟩, show p.1 = p.2, by rw ←hx)
lemma prod_subset_compl_diagonal_iff_disjoint {α : Type*} {s t : set α} :
set.prod s t ⊆ {p:α×α | p.1 = p.2}ᶜ ↔ s ∩ t = ∅ :=
by rw [eq_empty_iff_forall_not_mem, subset_compl_comm,
diagonal_eq_range_diagonal_map, range_subset_iff]; simp
lemma compact_compact_separated [t2_space α] {s t : set α}
(hs : is_compact s) (ht : is_compact t) (hst : s ∩ t = ∅) :
∃u v : set α, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ u ∩ v = ∅ :=
by simp only [prod_subset_compl_diagonal_iff_disjoint.symm] at ⊢ hst;
exact generalized_tube_lemma hs ht is_closed_diagonal hst
/-- In a `t2_space`, every compact set is closed. -/
lemma is_compact.is_closed [t2_space α] {s : set α} (hs : is_compact s) : is_closed s :=
is_open_compl_iff.mpr $ is_open_iff_forall_mem_open.mpr $ assume x hx,
let ⟨u, v, uo, vo, su, xv, uv⟩ :=
compact_compact_separated hs (compact_singleton : is_compact {x})
(by rwa [inter_comm, ←subset_compl_iff_disjoint, singleton_subset_iff]) in
have v ⊆ sᶜ, from
subset_compl_comm.mp (subset.trans su (subset_compl_iff_disjoint.mpr uv)),
⟨v, this, vo, by simpa using xv⟩
lemma compact_exhaustion.is_closed [t2_space α] (K : compact_exhaustion α) (n : ℕ) :
is_closed (K n) :=
(K.is_compact n).is_closed
lemma is_compact.inter [t2_space α] {s t : set α} (hs : is_compact s) (ht : is_compact t) :
is_compact (s ∩ t) :=
hs.inter_right $ ht.is_closed
lemma compact_closure_of_subset_compact [t2_space α] {s t : set α} (ht : is_compact t) (h : s ⊆ t) :
is_compact (closure s) :=
compact_of_is_closed_subset ht is_closed_closure (closure_minimal h ht.is_closed)
lemma image_closure_of_compact [t2_space β]
{s : set α} (hs : is_compact (closure s)) {f : α → β} (hf : continuous_on f (closure s)) :
f '' closure s = closure (f '' s) :=
subset.antisymm hf.image_closure $ closure_minimal (image_subset f subset_closure)
(hs.image_of_continuous_on hf).is_closed
/-- If a compact set is covered by two open sets, then we can cover it by two compact subsets. -/
lemma is_compact.binary_compact_cover [t2_space α] {K U V : set α} (hK : is_compact K)
(hU : is_open U) (hV : is_open V) (h2K : K ⊆ U ∪ V) :
∃ K₁ K₂ : set α, is_compact K₁ ∧ is_compact K₂ ∧ K₁ ⊆ U ∧ K₂ ⊆ V ∧ K = K₁ ∪ K₂ :=
begin
rcases compact_compact_separated (compact_diff hK hU) (compact_diff hK hV)
(by rwa [diff_inter_diff, diff_eq_empty]) with ⟨O₁, O₂, h1O₁, h1O₂, h2O₁, h2O₂, hO⟩,
refine ⟨_, _, compact_diff hK h1O₁, compact_diff hK h1O₂,
by rwa [diff_subset_comm], by rwa [diff_subset_comm], by rw [← diff_inter, hO, diff_empty]⟩
end
lemma continuous.is_closed_map [compact_space α] [t2_space β] {f : α → β} (h : continuous f) :
is_closed_map f :=
λ s hs, (hs.compact.image h).is_closed
lemma continuous.closed_embedding [compact_space α] [t2_space β] {f : α → β} (h : continuous f)
(hf : function.injective f) : closed_embedding f :=
closed_embedding_of_continuous_injective_closed h hf h.is_closed_map
section
open finset function
/-- For every finite open cover `Uᵢ` of a compact set, there exists a compact cover `Kᵢ ⊆ Uᵢ`. -/
lemma is_compact.finite_compact_cover [t2_space α] {s : set α} (hs : is_compact s)
{ι} (t : finset ι) (U : ι → set α) (hU : ∀ i ∈ t, is_open (U i)) (hsC : s ⊆ ⋃ i ∈ t, U i) :
∃ K : ι → set α, (∀ i, is_compact (K i)) ∧ (∀i, K i ⊆ U i) ∧ s = ⋃ i ∈ t, K i :=
begin
classical,
induction t using finset.induction with x t hx ih generalizing U hU s hs hsC,
{ refine ⟨λ _, ∅, λ i, compact_empty, λ i, empty_subset _, _⟩, simpa only [subset_empty_iff,
finset.not_mem_empty, Union_neg, Union_empty, not_false_iff] using hsC },
simp only [finset.set_bUnion_insert] at hsC,
simp only [finset.mem_insert] at hU,
have hU' : ∀ i ∈ t, is_open (U i) := λ i hi, hU i (or.inr hi),
rcases hs.binary_compact_cover (hU x (or.inl rfl)) (is_open_bUnion hU') hsC
with ⟨K₁, K₂, h1K₁, h1K₂, h2K₁, h2K₂, hK⟩,
rcases ih U hU' h1K₂ h2K₂ with ⟨K, h1K, h2K, h3K⟩,
refine ⟨update K x K₁, _, _, _⟩,
{ intros i, by_cases hi : i = x,
{ simp only [update_same, hi, h1K₁] },
{ rw [← ne.def] at hi, simp only [update_noteq hi, h1K] }},
{ intros i, by_cases hi : i = x,
{ simp only [update_same, hi, h2K₁] },
{ rw [← ne.def] at hi, simp only [update_noteq hi, h2K] }},
{ simp only [set_bUnion_insert_update _ hx, hK, h3K] }
end
end
lemma locally_compact_of_compact_nhds [t2_space α] (h : ∀ x : α, ∃ s, s ∈ 𝓝 x ∧ is_compact s) :
locally_compact_space α :=
⟨assume x n hn,
let ⟨u, un, uo, xu⟩ := mem_nhds_sets_iff.mp hn in
let ⟨k, kx, kc⟩ := h x in
-- K is compact but not necessarily contained in N.
-- K \ U is again compact and doesn't contain x, so
-- we may find open sets V, W separating x from K \ U.
-- Then K \ W is a compact neighborhood of x contained in U.
let ⟨v, w, vo, wo, xv, kuw, vw⟩ :=
compact_compact_separated compact_singleton (compact_diff kc uo)
(by rw [singleton_inter_eq_empty]; exact λ h, h.2 xu) in
have wn : wᶜ ∈ 𝓝 x, from
mem_nhds_sets_iff.mpr
⟨v, subset_compl_iff_disjoint.mpr vw, vo, singleton_subset_iff.mp xv⟩,
⟨k \ w,
filter.inter_mem_sets kx wn,
subset.trans (diff_subset_comm.mp kuw) un,
compact_diff kc wo⟩⟩
@[priority 100] -- see Note [lower instance priority]
instance locally_compact_of_compact [t2_space α] [compact_space α] : locally_compact_space α :=
locally_compact_of_compact_nhds (assume x, ⟨univ, mem_nhds_sets is_open_univ trivial, compact_univ⟩)
/-- In a locally compact T₂ space, every point has an open neighborhood with compact closure -/
lemma exists_open_with_compact_closure [locally_compact_space α] [t2_space α] (x : α) :
∃ (U : set α), is_open U ∧ x ∈ U ∧ is_compact (closure U) :=
begin
rcases exists_compact_mem_nhds x with ⟨K, hKc, hxK⟩,
rcases mem_nhds_sets_iff.1 hxK with ⟨t, h1t, h2t, h3t⟩,
exact ⟨t, h2t, h3t, compact_closure_of_subset_compact hKc h1t⟩
end
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 ∧ 𝓝[t] a = ⊥)
lemma nhds_is_closed [regular_space α] {a : α} {s : set α} (h : s ∈ 𝓝 a) :
∃ t ∈ 𝓝 a, t ⊆ s ∧ is_closed t :=
let ⟨s', h₁, h₂, h₃⟩ := mem_nhds_sets_iff.mp h in
have ∃t, is_open t ∧ s'ᶜ ⊆ t ∧ 𝓝[t] a = ⊥,
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_eq_bot $ by rwa [compl_compl],
subset.trans (compl_subset_comm.1 ht₂) h₁,
is_closed_compl_iff.mpr ht₁⟩
lemma closed_nhds_basis [regular_space α] (a : α) :
(𝓝 a).has_basis (λ s : set α, s ∈ 𝓝 a ∧ is_closed s) id :=
⟨λ t, ⟨λ t_in, let ⟨s, s_in, h_st, h⟩ := nhds_is_closed t_in in ⟨s, ⟨s_in, h⟩, h_st⟩,
λ ⟨s, ⟨s_in, hs⟩, hst⟩, mem_sets_of_superset s_in hst⟩⟩
instance subtype.regular_space [regular_space α] {p : α → Prop} : regular_space (subtype p) :=
⟨begin
intros s a hs ha,
rcases is_closed_induced_iff.1 hs with ⟨s, hs', rfl⟩,
rcases regular_space.regular hs' ha with ⟨t, ht, hst, hat⟩,
refine ⟨coe ⁻¹' t, is_open_induced ht, preimage_mono hst, _⟩,
rw [nhds_within, nhds_induced, ← comap_principal, ← comap_inf, ← nhds_within, hat, comap_bot]
end⟩
variable (α)
@[priority 100] -- see Note [lower instance priority]
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⟩⟩⟩
variable {α}
lemma disjoint_nested_nhds [regular_space α] {x y : α} (h : x ≠ y) :
∃ (U₁ V₁ ∈ 𝓝 x) (U₂ V₂ ∈ 𝓝 y), is_closed V₁ ∧ is_closed V₂ ∧ is_open U₁ ∧ is_open U₂ ∧
V₁ ⊆ U₁ ∧ V₂ ⊆ U₂ ∧ U₁ ∩ U₂ = ∅ :=
begin
rcases t2_separation h with ⟨U₁, U₂, U₁_op, U₂_op, x_in, y_in, H⟩,
rcases nhds_is_closed (mem_nhds_sets U₁_op x_in) with ⟨V₁, V₁_in, h₁, V₁_closed⟩,
rcases nhds_is_closed (mem_nhds_sets U₂_op y_in) with ⟨V₂, V₂_in, h₂, V₂_closed⟩,
use [U₁, V₁, mem_sets_of_superset V₁_in h₁, V₁_in,
U₂, V₂, mem_sets_of_superset V₂_in h₂, V₂_in],
tauto
end
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
theorem normal_exists_closure_subset [normal_space α] {s t : set α} (hs : is_closed s)
(ht : is_open t) (hst : s ⊆ t) :
∃ u, is_open u ∧ s ⊆ u ∧ closure u ⊆ t :=
begin
have : disjoint s tᶜ, from λ x ⟨hxs, hxt⟩, hxt (hst hxs),
rcases normal_separation hs (is_closed_compl_iff.2 ht) this
with ⟨s', t', hs', ht', hss', htt', hs't'⟩,
refine ⟨s', hs', hss',
subset.trans (closure_minimal _ (is_closed_compl_iff.2 ht')) (compl_subset_comm.1 htt')⟩,
exact λ x hxs hxt, hs't' ⟨hxs, hxt⟩
end
@[priority 100] -- see Note [lower instance priority]
instance normal_space.regular_space [normal_space α] : regular_space α :=
{ regular := λ s x hs hxs, let ⟨u, v, hu, hv, hsu, hxv, huv⟩ :=
normal_separation hs is_closed_singleton
(λ _ ⟨hx, hy⟩, hxs $ mem_of_eq_of_mem (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 (singleton_subset_iff.1 hxv), u, filter.mem_principal_self u,
inter_comm u v ▸ huv⟩⟩ }
-- We can't make this an instance because it could cause an instance loop.
lemma normal_of_compact_t2 [compact_space α] [t2_space α] : normal_space α :=
begin
refine ⟨assume s t hs ht st, _⟩,
simp only [disjoint_iff],
exact compact_compact_separated hs.compact ht.compact st.eq_bot
end
end normality
/-- In a compact t2 space, the connected component of a point equals the intersection of all
its clopen neighbourhoods. -/
lemma connected_component_eq_Inter_clopen [t2_space α] [compact_space α] {x : α} :
connected_component x = ⋂ Z : {Z : set α // is_clopen Z ∧ x ∈ Z}, Z :=
begin
apply eq_of_subset_of_subset connected_component_subset_Inter_clopen,
-- Reduce to showing that the clopen intersection is connected.
refine is_preconnected.subset_connected_component _ (mem_Inter.2 (λ Z, Z.2.2)),
-- We do this by showing that any disjoint cover by two closed sets implies
-- that one of these closed sets must contain our whole thing.
-- To reduce to the case where the cover is disjoint on all of `α` we need that `s` is closed
have hs : @is_closed _ _inst_1 (⋂ (Z : {Z : set α // is_clopen Z ∧ x ∈ Z}), ↑Z) :=
is_closed_Inter (λ Z, Z.2.1.2),
rw (is_preconnected_iff_subset_of_fully_disjoint_closed hs),
intros a b ha hb hab ab_empty,
haveI := @normal_of_compact_t2 α _ _ _,
-- Since our space is normal, we get two larger disjoint open sets containing the disjoint
-- closed sets. If we can show that our intersection is a subset of any of these we can then
-- "descend" this to show that it is a subset of either a or b.
rcases normal_separation ha hb (disjoint_iff.2 ab_empty) with ⟨u, v, hu, hv, hau, hbv, huv⟩,
-- If we can find a clopen set around x, contained in u ∪ v, we get a disjoint decomposition
-- Z = Z ∩ u ∪ Z ∩ v of clopen sets. The intersection of all clopen neighbourhoods will then lie
-- in whichever of u or v x lies in and hence will be a subset of either a or b.
suffices : ∃ (Z : set α), is_clopen Z ∧ x ∈ Z ∧ Z ⊆ u ∪ v,
{ cases this with Z H,
rw [disjoint_iff_inter_eq_empty] at huv,
have H1 := is_clopen_inter_of_disjoint_cover_clopen H.1 H.2.2 hu hv huv,
rw [union_comm] at H,
have H2 := is_clopen_inter_of_disjoint_cover_clopen H.1 H.2.2 hv hu (inter_comm u v ▸ huv),
by_cases (x ∈ u),
-- The x ∈ u case.
{ left,
suffices : (⋂ (Z : {Z : set α // is_clopen Z ∧ x ∈ Z}), ↑Z) ⊆ u,
{ rw ←set.disjoint_iff_inter_eq_empty at huv,
replace hab : (⋂ (Z : {Z // is_clopen Z ∧ x ∈ Z}), ↑Z) ≤ a ∪ b := hab,
replace this : (⋂ (Z : {Z // is_clopen Z ∧ x ∈ Z}), ↑Z) ≤ u := this,
exact disjoint.left_le_of_le_sup_right hab (huv.mono this hbv) },
{ apply subset.trans _ (inter_subset_right Z u),
apply Inter_subset (λ Z : {Z : set α // is_clopen Z ∧ x ∈ Z}, ↑Z)
⟨Z ∩ u, H1, mem_inter H.2.1 h⟩ } },
-- If x ∉ u, we get x ∈ v since x ∈ u ∪ v. The rest is then like the x ∈ u case.
have h1 : x ∈ v,
{ cases (mem_union x u v).1 (mem_of_subset_of_mem (subset.trans hab
(union_subset_union hau hbv)) (mem_Inter.2 (λ i, i.2.2))) with h1 h1,
{ exfalso, exact h h1},
{ exact h1} },
right,
suffices : (⋂ (Z : {Z : set α // is_clopen Z ∧ x ∈ Z}), ↑Z) ⊆ v,
{ rw [inter_comm, ←set.disjoint_iff_inter_eq_empty] at huv,
replace hab : (⋂ (Z : {Z // is_clopen Z ∧ x ∈ Z}), ↑Z) ≤ a ∪ b := hab,
replace this : (⋂ (Z : {Z // is_clopen Z ∧ x ∈ Z}), ↑Z) ≤ v := this,
exact disjoint.left_le_of_le_sup_left hab (huv.mono this hau) },
{ apply subset.trans _ (inter_subset_right Z v),
apply Inter_subset (λ Z : {Z : set α // is_clopen Z ∧ x ∈ Z}, ↑Z)
⟨Z ∩ v, H2, mem_inter H.2.1 h1⟩ } },
-- Now we find the required Z. We utilize the fact that X \ u ∪ v will be compact,
-- so there must be some finite intersection of clopen neighbourhoods of X disjoint to it,
-- but a finite intersection of clopen sets is clopen so we let this be our Z.
have H1 := ((is_closed_compl_iff.2 (is_open_union hu hv)).compact.inter_Inter_nonempty
(λ Z : {Z : set α // is_clopen Z ∧ x ∈ Z}, Z) (λ Z, Z.2.1.2)),
rw [←not_imp_not, not_forall, not_nonempty_iff_eq_empty, inter_comm] at H1,
have huv_union := subset.trans hab (union_subset_union hau hbv),
rw [← compl_compl (u ∪ v), subset_compl_iff_disjoint] at huv_union,
cases H1 huv_union with Zi H2,
refine ⟨(⋂ (U ∈ Zi), subtype.val U), _, _, _⟩,
{ exact is_clopen_bInter (λ Z hZ, Z.2.1) },
{ exact mem_bInter_iff.2 (λ Z hZ, Z.2.2) },
{ rwa [not_nonempty_iff_eq_empty, inter_comm, ←subset_compl_iff_disjoint, compl_compl] at H2 }
end
section connected_component_setoid
local attribute [instance] connected_component_setoid
/-- `connected_components α` is Hausdorff when `α` is Hausdorff and compact -/
instance connected_components.t2 [t2_space α] [compact_space α] :
t2_space (connected_components α) :=
begin
-- Proof follows that of: https://stacks.math.columbia.edu/tag/0900
-- Fix 2 distinct connected components, with points a and b
refine ⟨λ x y, quotient.induction_on x (quotient.induction_on y (λ a b ne, _))⟩,
rw connected_component_nrel_iff at ne,
have h := connected_component_disjoint ne,
-- write ⟦b⟧ as the intersection of all clopen subsets containing it
rw [connected_component_eq_Inter_clopen, disjoint_iff_inter_eq_empty, inter_comm] at h,
-- Now we show that this can be reduced to some clopen containing ⟦b⟧ being disjoint to ⟦a⟧
cases is_closed_connected_component.compact.elim_finite_subfamily_closed _ _ h
with fin_a ha,
swap, { exact λ Z, Z.2.1.2 },
set U : set α := (⋂ (i : {Z // is_clopen Z ∧ b ∈ Z}) (H : i ∈ fin_a), ↑i) with hU,
rw ←hU at ha,
have hu_clopen : is_clopen U := is_clopen_bInter (λ i j, i.2.1),
-- This clopen and its complement will separate the points corresponding to ⟦a⟧ and ⟦b⟧
use [quotient.mk '' U, quotient.mk '' Uᶜ],
-- Using the fact that clopens are unions of connected components, we show that
-- U and Uᶜ is the preimage of a clopen set in the quotient
have hu : quotient.mk ⁻¹' (quotient.mk '' U) = U :=
(connected_components_preimage_image U ▸ eq.symm) hu_clopen.eq_union_connected_components,
have huc : quotient.mk ⁻¹' (quotient.mk '' Uᶜ) = Uᶜ :=
(connected_components_preimage_image Uᶜ ▸ eq.symm)
(is_clopen_compl hu_clopen).eq_union_connected_components,
-- showing that U and Uᶜ are open and separates ⟦a⟧ and ⟦b⟧
refine ⟨_,_,_,_,_⟩,
{ rw [(quotient_map_iff.1 quotient_map_quotient_mk).2 _, hu],
exact hu_clopen.1 },
{ rw [(quotient_map_iff.1 quotient_map_quotient_mk).2 _, huc],
exact is_open_compl_iff.2 hu_clopen.2 },
{ exact mem_image_of_mem _ (mem_Inter.2 (λ Z, mem_Inter.2 (λ Zmem, Z.2.2))) },
{ apply mem_image_of_mem,
exact mem_of_subset_of_mem (subset_compl_iff_disjoint.2 ha) (@mem_connected_component _ _ a) },
apply preimage_injective.2 (@surjective_quotient_mk _ _),
rw [preimage_inter, preimage_empty, hu, huc, inter_compl_self _],
end
end connected_component_setoid
|
c68a864b7d3898136e069d332c62a327e20f7f1a | 3dc4623269159d02a444fe898d33e8c7e7e9461b | /.github/workflows/project_1_a_decrire/lean-scheme-submission/src/spectrum_of_a_ring/structure_presheaf_stalks.lean | a12eb932f407db8d672d4386ca1566d3e736e2a0 | [] | 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 | 4,703 | lean | /-
The stalks of the structure presheaf on the basis are local rings.
-/
import ring_theory.ideals
import ring_theory.localization
import to_mathlib.localization.local_rings
import to_mathlib.localization.localization_alt
import sheaves.stalk_of_rings_on_standard_basis
import spectrum_of_a_ring.structure_presheaf
import spectrum_of_a_ring.structure_presheaf_localization
import spectrum_of_a_ring.structure_presheaf_res
universe u
noncomputable theory
variables {R : Type u} [comm_ring R]
variables (P : Spec R)
open localization localization_alt stalk_of_rings_on_standard_basis
open classical
def Bstd := D_fs_standard_basis R
namespace strucutre_presheaf_stalks
def F := structure_presheaf_on_basis R
def FP := stalk_of_rings_on_standard_basis Bstd F P
-- This is essentially the map from x to x/1.
def φ : R → FP P := λ x,
⟦{ U := opens.univ,
BU := (D_fs_standard_basis R).1,
Hx := set.mem_univ P,
s := (of : R → localization R (S (opens.univ))) x, }⟧
instance FP.is_comm_ring : comm_ring (FP P) :=
by simp [FP]; by apply_instance
instance prime.is_submonoid : is_submonoid (-P.1 : set R) :=
{ one_mem := P.1.ne_top_iff_one.1 P.2.1,
mul_mem := λ x y hnx hny hxy, or.cases_on (P.2.2 hxy) hnx hny }
instance φP.is_ring_hom : is_ring_hom (φ P) :=
{ map_one := rfl,
map_mul := λ x y,
begin
apply quotient.sound,
use [opens.univ, (D_fs_standard_basis R).1, set.mem_univ P],
use [set.subset.refl _, (λ x Hx, ⟨Hx, Hx⟩)],
rw (F.res_is_ring_hom _ _ _).map_mul,
rw ←presheaf_on_basis.Hcomp',
rw ←presheaf_on_basis.Hcomp',
rw of.is_ring_hom.map_mul,
rw (F.res_is_ring_hom _ _ _).map_mul,
end,
map_add := λ x y,
begin
apply quotient.sound,
use [opens.univ, (D_fs_standard_basis R).1, set.mem_univ P],
use [set.subset.refl _, (λ x Hx, ⟨Hx, Hx⟩)],
rw (F.res_is_ring_hom _ _ _).map_add,
rw ←presheaf_on_basis.Hcomp',
rw ←presheaf_on_basis.Hcomp',
rw of.is_ring_hom.map_add,
rw (F.res_is_ring_hom _ _ _).map_add,
end, }
lemma stalk_local.inverts_data : inverts_data (-P.1 : set R) (φ P) :=
begin
rintros ⟨s, Hs⟩,
change s ∉ P.val at Hs,
let BDs := D_fs.mem R s,
have HsS : s ∈ S (Spec.DO R s) := S.f_mem s,
let sinv : FP P :=
⟦{ U := Spec.DO R s,
BU := BDs,
Hx := Hs,
s := ⟦⟨1, ⟨s, HsS⟩⟩⟧, }⟧,
use sinv,
apply quotient.sound,
use [Spec.DO R s, BDs, Hs, (λ x Hx, ⟨trivial, Hx⟩), set.subset_univ _],
simp,
erw (F.res_is_ring_hom _ _ _).map_mul,
erw (F.res_is_ring_hom _ _ _).map_one,
iterate 2 { rw ←presheaf_on_basis.Hcomp', },
erw presheaf_on_basis.Hid',
erw structure_presheaf_on_basis.res_eq,
erw ←structure_presheaf_on_basis.res_comp_of',
apply quotient.sound,
use [1, is_submonoid.one_mem _],
simp,
end
lemma stalk_local.has_denom : has_denom (-P.1 : set R) (φ P) :=
begin
intros x,
apply quotient.induction_on x,
rintros ⟨U, BU, HPU, s⟩,
-- s ∈ R[1/S(U)].
apply quotient.induction_on s,
rintros ⟨p, ⟨q, Hq⟩⟩,
change U ⊆ Spec.DO R q at Hq,
-- Since P ∈ D(q).
have HqnP : q ∉ P.val := Hq HPU,
use [⟨⟨q, HqnP⟩, p⟩],
apply quotient.sound,
use [U, BU, HPU, (λ x Hx, ⟨trivial, Hx⟩), set.subset_univ _],
simp,
erw (F.res_is_ring_hom _ _ _).map_mul,
iterate 2 { rw ←presheaf_on_basis.Hcomp', },
erw presheaf_on_basis.Hid',
erw structure_presheaf_on_basis.res_eq,
iterate 2 { erw ←structure_presheaf_on_basis.res_comp_of', },
apply quotient.sound,
use [1, is_submonoid.one_mem _],
simp,
end
lemma stalk_local.has_denom_data : has_denom_data (-P.1 : set R) (φ P) :=
has_denom_some (-P.1 : set R) (φ P) (stalk_local.has_denom P)
lemma stalk_local.ker_le : ker (φ P) ≤ submonoid_ann (-P.1 : set R) :=
begin
intros x Hx,
change φ P x = 0 at Hx,
erw quotient.eq at Hx,
rcases Hx with ⟨W, BW, HPW, HWuniv1, HWuniv2, Hres⟩,
dsimp at Hres,
erw is_ring_hom.map_zero (F.res (D_fs_standard_basis R).1 BW HWuniv2) at Hres,
erw structure_presheaf_on_basis.res_eq at Hres,
erw ←structure_presheaf_on_basis.res_comp_of' at Hres,
erw quotient.eq at Hres,
rcases Hres with ⟨t, HtSW, Ht⟩,
simp at Ht,
have HtnP : t ∉ P.val := HtSW HPW,
use ⟨⟨x, ⟨t, HtnP⟩⟩, Ht⟩,
end
lemma stalk_local.localization : is_localization_data (-P.1 : set R) (φ P) :=
{ inverts := stalk_local.inverts_data P,
has_denom := stalk_local.has_denom_data P,
ker_le := stalk_local.ker_le P, }
lemma stalk_local : local_ring (FP P) :=
local_ring.of_is_localization_data_at_prime P.2 (stalk_local.localization P)
end strucutre_presheaf_stalks
|
418db3c569d0c2b055248ad8ea03e5d31a5ca5d8 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/monad/products.lean | fea2f76ee96801adbaba4e7a5dfd724c6e1916ef | [
"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 | 3,984 | lean | /-
Copyright (c) 2021 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import category_theory.over
import category_theory.monad.algebra
import category_theory.limits.shapes.binary_products
/-!
# Algebras for the coproduct monad
The functor `Y ↦ X ⨿ Y` forms a monad, whose category of monads is equivalent to the under category
of `X`. Similarly, `Y ↦ X ⨯ Y` forms a comonad, whose category of comonads is equivalent to the
over category of `X`.
## TODO
Show that `over.forget X : over X ⥤ C` is a comonadic left adjoint and `under.forget : under X ⥤ C`
is a monadic right adjoint.
-/
noncomputable theory
universes v u -- morphism levels before object levels. See note [category_theory universes].
namespace category_theory
open category limits
variables {C : Type u} [category.{v} C] (X : C)
section
open comonad
variable [has_binary_products C]
/-- `X ⨯ -` has a comonad structure. This is sometimes called the writer comonad. -/
@[simps]
def prod_comonad : comonad C :=
{ to_functor := prod.functor.obj X,
ε' := { app := λ Y, limits.prod.snd },
δ' := { app := λ Y, prod.lift limits.prod.fst (𝟙 _) } }
/--
The forward direction of the equivalence from coalgebras for the product comonad to the over
category.
-/
@[simps]
def coalgebra_to_over :
coalgebra (prod_comonad X) ⥤ over X :=
{ obj := λ A, over.mk (A.a ≫ limits.prod.fst),
map := λ A₁ A₂ f, over.hom_mk f.f (by { rw [over.mk_hom, ← f.h_assoc], dsimp, simp }) }
/--
The backward direction of the equivalence from coalgebras for the product comonad to the over
category.
-/
@[simps]
def over_to_coalgebra :
over X ⥤ coalgebra (prod_comonad X) :=
{ obj := λ f, { A := f.left, a := prod.lift f.hom (𝟙 _) },
map := λ f₁ f₂ g, { f := g.left } }
/-- The equivalence from coalgebras for the product comonad to the over category. -/
@[simps]
def coalgebra_equiv_over :
coalgebra (prod_comonad X) ≌ over X :=
{ functor := coalgebra_to_over X,
inverse := over_to_coalgebra X,
unit_iso := nat_iso.of_components
(λ A, coalgebra.iso_mk (iso.refl _)
(prod.hom_ext (by { dsimp, simp }) (by { dsimp, simpa using A.counit })))
(λ A₁ A₂ f, by { ext, simp }),
counit_iso := nat_iso.of_components (λ f, over.iso_mk (iso.refl _)) (λ f g k, by tidy) }.
end
section
open _root_.monad
variable [has_binary_coproducts C]
/-- `X ⨿ -` has a monad structure. This is sometimes called the either monad. -/
@[simps]
def coprod_monad : monad C :=
{ to_functor := coprod.functor.obj X,
η' := { app := λ Y, coprod.inr },
μ' := { app := λ Y, coprod.desc coprod.inl (𝟙 _) } }
/--
The forward direction of the equivalence from algebras for the coproduct monad to the under
category.
-/
@[simps]
def algebra_to_under :
monad.algebra (coprod_monad X) ⥤ under X :=
{ obj := λ A, under.mk (coprod.inl ≫ A.a),
map := λ A₁ A₂ f, under.hom_mk f.f (by { rw [under.mk_hom, assoc, ←f.h], dsimp, simp }) }
/--
The backward direction of the equivalence from algebras for the coproduct monad to the under
category.
-/
@[simps]
def under_to_algebra :
under X ⥤ monad.algebra (coprod_monad X) :=
{ obj := λ f, { A := f.right, a := coprod.desc f.hom (𝟙 _) },
map := λ f₁ f₂ g, { f := g.right } }
/--
The equivalence from algebras for the coproduct monad to the under category.
-/
@[simps]
def algebra_equiv_under :
monad.algebra (coprod_monad X) ≌ under X :=
{ functor := algebra_to_under X,
inverse := under_to_algebra X,
unit_iso := nat_iso.of_components
(λ A, monad.algebra.iso_mk (iso.refl _)
(coprod.hom_ext (by tidy) (by { dsimp, simpa using A.unit.symm })))
(λ A₁ A₂ f, by { ext, simp }),
counit_iso :=
nat_iso.of_components (λ f, under.iso_mk (iso.refl _) (by tidy)) (λ f g k, by tidy) }.
end
end category_theory
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.