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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ec8ac09cf2dd929f43c5f8a08717bd7ad3c566e1 | 367134ba5a65885e863bdc4507601606690974c1 | /src/combinatorics/simple_graph/degree_sum.lean | e915d98c4ab92765543d861c9d49e647ed15bfed | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 8,342 | lean | /-
Copyright (c) 2020 Kyle Miller. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Kyle Miller
-/
import combinatorics.simple_graph.basic
import algebra.big_operators.basic
import data.nat.parity
import data.zmod.parity
/-!
# Degree-sum formula and handshaking lemma
The degree-sum formula is that the sum of the degrees of the vertices in
a finite graph is equal to twice the number of edges. The handshaking lemma,
a corollary, is that the number of odd-degree vertices is even.
## Main definitions
- A `dart` is a directed edge, consisting of an ordered pair of adjacent vertices,
thought of as being a directed edge.
- `simple_graph.sum_degrees_eq_twice_card_edges` is the degree-sum formula.
- `simple_graph.even_card_odd_degree_vertices` is the handshaking lemma.
- `simple_graph.odd_card_odd_degree_vertices_ne` is that the number of odd-degree
vertices different from a given odd-degree vertex is odd.
- `simple_graph.exists_ne_odd_degree_of_exists_odd_degree` is that the existence of an
odd-degree vertex implies the existence of another one.
## Implementation notes
We give a combinatorial proof by using the facts that (1) the map from
darts to vertices is such that each fiber has cardinality the degree
of the corresponding vertex and that (2) the map from darts to edges is 2-to-1.
## Tags
simple graphs, sums, degree-sum formula, handshaking lemma
-/
open finset
open_locale big_operators
namespace simple_graph
universes u
variables {V : Type u} (G : simple_graph V)
/-- A dart is a directed edge, consisting of an ordered pair of adjacent vertices. -/
@[ext, derive decidable_eq]
structure dart :=
(fst snd : V)
(is_adj : G.adj fst snd)
instance dart.fintype [fintype V] [decidable_rel G.adj] : fintype G.dart :=
fintype.of_equiv (Σ v, G.neighbor_set v)
{ to_fun := λ s, ⟨s.fst, s.snd, s.snd.property⟩,
inv_fun := λ d, ⟨d.fst, d.snd, d.is_adj⟩,
left_inv := λ s, by ext; simp,
right_inv := λ d, by ext; simp }
variables {G}
/-- The edge associated to the dart. -/
def dart.edge (d : G.dart) : sym2 V := ⟦(d.fst, d.snd)⟧
@[simp] lemma dart.edge_mem (d : G.dart) : d.edge ∈ G.edge_set :=
d.is_adj
/-- The dart with reversed orientation from a given dart. -/
def dart.rev (d : G.dart) : G.dart :=
⟨d.snd, d.fst, G.sym d.is_adj⟩
@[simp] lemma dart.rev_edge (d : G.dart) : d.rev.edge = d.edge :=
sym2.eq_swap
@[simp] lemma dart.rev_rev (d : G.dart) : d.rev.rev = d :=
dart.ext _ _ rfl rfl
@[simp] lemma dart.rev_involutive : function.involutive (dart.rev : G.dart → G.dart) :=
dart.rev_rev
lemma dart.rev_ne (d : G.dart) : d.rev ≠ d :=
begin
cases d with f s h,
simp only [dart.rev, not_and, ne.def],
rintro rfl,
exact false.elim (G.loopless _ h),
end
lemma dart_edge_eq_iff (d₁ d₂ : G.dart) :
d₁.edge = d₂.edge ↔ d₁ = d₂ ∨ d₁ = d₂.rev :=
begin
cases d₁ with s₁ t₁ h₁,
cases d₂ with s₂ t₂ h₂,
simp only [dart.edge, dart.rev_edge, dart.rev],
rw sym2.eq_iff,
end
variables (G)
/-- For a given vertex `v`, this is the bijective map from the neighbor set at `v`
to the darts `d` with `d.fst = v`. --/
def dart_of_neighbor_set (v : V) (w : G.neighbor_set v) : G.dart :=
⟨v, w, w.property⟩
lemma dart_of_neighbor_set_injective (v : V) : function.injective (G.dart_of_neighbor_set v) :=
λ e₁ e₂ h, by { injection h with h₁ h₂, exact subtype.ext h₂ }
instance dart.inhabited [inhabited V] [inhabited (G.neighbor_set (default _))] :
inhabited G.dart := ⟨G.dart_of_neighbor_set (default _) (default _)⟩
section degree_sum
variables [fintype V] [decidable_rel G.adj]
lemma dart_fst_fiber [decidable_eq V] (v : V) :
univ.filter (λ d : G.dart, d.fst = v) = univ.image (G.dart_of_neighbor_set v) :=
begin
ext d,
simp only [mem_image, true_and, mem_filter, set_coe.exists, mem_univ, exists_prop_of_true],
split,
{ rintro rfl,
exact ⟨_, d.is_adj, dart.ext _ _ rfl rfl⟩, },
{ rintro ⟨e, he, rfl⟩,
refl, },
end
lemma dart_fst_fiber_card_eq_degree [decidable_eq V] (v : V) :
(univ.filter (λ d : G.dart, d.fst = v)).card = G.degree v :=
begin
have hh := card_image_of_injective univ (G.dart_of_neighbor_set_injective v),
rw [finset.card_univ, card_neighbor_set_eq_degree] at hh,
rwa dart_fst_fiber,
end
lemma dart_card_eq_sum_degrees : fintype.card G.dart = ∑ v, G.degree v :=
begin
haveI h : decidable_eq V := by { classical, apply_instance },
simp only [←card_univ, ←dart_fst_fiber_card_eq_degree],
exact card_eq_sum_card_fiberwise (by simp),
end
variables {G} [decidable_eq V]
lemma dart.edge_fiber (d : G.dart) :
(univ.filter (λ (d' : G.dart), d'.edge = d.edge)) = {d, d.rev} :=
finset.ext (λ d', by simpa using dart_edge_eq_iff d' d)
variables (G)
lemma dart_edge_fiber_card (e : sym2 V) (h : e ∈ G.edge_set) :
(univ.filter (λ (d : G.dart), d.edge = e)).card = 2 :=
begin
refine quotient.ind (λ p h, _) e h,
cases p with v w,
let d : G.dart := ⟨v, w, h⟩,
convert congr_arg card d.edge_fiber,
rw [card_insert_of_not_mem, card_singleton],
rw [mem_singleton],
exact d.rev_ne.symm,
end
lemma dart_card_eq_twice_card_edges : fintype.card G.dart = 2 * G.edge_finset.card :=
begin
rw ←card_univ,
rw @card_eq_sum_card_fiberwise _ _ _ dart.edge _ G.edge_finset
(λ d h, by { rw mem_edge_finset, apply dart.edge_mem }),
rw [←mul_comm, sum_const_nat],
intros e h,
apply G.dart_edge_fiber_card e,
rwa ←mem_edge_finset,
end
/-- The degree-sum formula. This is also known as the handshaking lemma, which might
more specifically refer to `simple_graph.even_card_odd_degree_vertices`. -/
theorem sum_degrees_eq_twice_card_edges : ∑ v, G.degree v = 2 * G.edge_finset.card :=
G.dart_card_eq_sum_degrees.symm.trans G.dart_card_eq_twice_card_edges
end degree_sum
/-- The handshaking lemma. See also `simple_graph.sum_degrees_eq_twice_card_edges`. -/
theorem even_card_odd_degree_vertices [fintype V] [decidable_rel G.adj] :
even (univ.filter (λ v, odd (G.degree v))).card :=
begin
classical,
have h := congr_arg ((λ n, ↑n) : ℕ → zmod 2) G.sum_degrees_eq_twice_card_edges,
simp only [zmod.nat_cast_self, zero_mul, nat.cast_mul] at h,
rw [sum_nat_cast, ←sum_filter_ne_zero] at h,
rw @sum_congr _ (zmod 2) _ _ (λ v, (G.degree v : zmod 2)) (λ v, (1 : zmod 2)) _ rfl at h,
{ simp only [filter_congr_decidable, mul_one, nsmul_eq_mul, sum_const, ne.def] at h,
rw ←zmod.eq_zero_iff_even,
convert h,
ext v,
rw ←zmod.ne_zero_iff_odd,
congr' },
{ intros v,
simp only [true_and, mem_filter, mem_univ, ne.def],
rw [zmod.eq_zero_iff_even, zmod.eq_one_iff_odd, nat.odd_iff_not_even, imp_self],
trivial }
end
lemma odd_card_odd_degree_vertices_ne [fintype V] [decidable_eq V] [decidable_rel G.adj]
(v : V) (h : odd (G.degree v)) :
odd (univ.filter (λ w, w ≠ v ∧ odd (G.degree w))).card :=
begin
rcases G.even_card_odd_degree_vertices with ⟨k, hg⟩,
have hk : 0 < k,
{ have hh : (filter (λ (v : V), odd (G.degree v)) univ).nonempty,
{ use v,
simp only [true_and, mem_filter, mem_univ],
use h, },
rwa [←card_pos, hg, zero_lt_mul_left] at hh,
exact zero_lt_two, },
have hc : (λ (w : V), w ≠ v ∧ odd (G.degree w)) = (λ (w : V), odd (G.degree w) ∧ w ≠ v),
{ ext w,
rw and_comm, },
simp only [hc, filter_congr_decidable],
rw [←filter_filter, filter_ne', card_erase_of_mem],
{ use k - 1,
rw [nat.pred_eq_succ_iff, hg, nat.mul_sub_left_distrib, ← nat.sub_add_comm, eq_comm,
← (nat.sub_eq_iff_eq_add _).symm],
{ ring },
{ exact add_le_add_right (zero_le (2 * k)) 2 },
{ exact nat.mul_le_mul_left _ hk } },
{ simpa only [true_and, mem_filter, mem_univ] },
end
lemma exists_ne_odd_degree_of_exists_odd_degree [fintype V] [decidable_rel G.adj]
(v : V) (h : odd (G.degree v)) :
∃ (w : V), w ≠ v ∧ odd (G.degree w) :=
begin
haveI : decidable_eq V := by { classical, apply_instance },
rcases G.odd_card_odd_degree_vertices_ne v h with ⟨k, hg⟩,
have hg' : (filter (λ (w : V), w ≠ v ∧ odd (G.degree w)) univ).card > 0,
{ rw hg,
apply nat.succ_pos, },
rcases card_pos.mp hg' with ⟨w, hw⟩,
simp only [true_and, mem_filter, mem_univ, ne.def] at hw,
exact ⟨w, hw⟩,
end
end simple_graph
|
0b8a96c8faf00e5ddcf40578b68b11fe03b11844 | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/data/complex/exponential.lean | 0f20ee63741b6daf4f1746d6e662e5c338fb07e5 | [
"Apache-2.0"
] | permissive | stjordanis/mathlib | 51e286d19140e3788ef2c470bc7b953e4991f0c9 | 2568d41bca08f5d6bf39d915434c8447e21f42ee | refs/heads/master | 1,631,748,053,501 | 1,627,938,886,000 | 1,627,938,886,000 | 228,728,358 | 0 | 0 | Apache-2.0 | 1,576,630,588,000 | 1,576,630,587,000 | null | UTF-8 | Lean | false | false | 61,909 | 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
-/
import algebra.geom_sum
import data.complex.basic
import data.nat.choose.sum
/-!
# Exponential, trigonometric and hyperbolic trigonometric functions
This file contains the definitions of the real and complex exponential, sine, cosine, tangent,
hyperbolic sine, hyperbolic cosine, and hyperbolic tangent functions.
-/
local notation `abs'` := _root_.abs
open is_absolute_value
open_locale classical big_operators nat
section
open real is_absolute_value finset
lemma forall_ge_le_of_forall_le_succ {α : Type*} [preorder α] (f : ℕ → α) {m : ℕ}
(h : ∀ n ≥ m, f n.succ ≤ f n) : ∀ {l}, ∀ k ≥ m, k ≤ l → f l ≤ f k :=
begin
assume l k hkm hkl,
generalize hp : l - k = p,
have : l = k + p := add_comm p k ▸ (nat.sub_eq_iff_eq_add hkl).1 hp,
subst this,
clear hkl hp,
induction p with p ih,
{ simp },
{ exact le_trans (h _ (le_trans hkm (nat.le_add_right _ _))) ih }
end
section
variables {α : Type*} {β : Type*} [ring β]
[linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv]
lemma is_cau_of_decreasing_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a)
(hnm : ∀ n ≥ m, f n.succ ≤ f n) : is_cau_seq abs f :=
λ ε ε0,
let ⟨k, hk⟩ := archimedean.arch a ε0 in
have h : ∃ l, ∀ n ≥ m, a - l • ε < f n :=
⟨k + k + 1, λ n hnm, lt_of_lt_of_le
(show a - (k + (k + 1)) • ε < -abs (f n),
from lt_neg.1 $ lt_of_le_of_lt (ham n hnm) (begin
rw [neg_sub, lt_sub_iff_add_lt, add_nsmul, add_nsmul, one_nsmul],
exact add_lt_add_of_le_of_lt hk (lt_of_le_of_lt hk
(lt_add_of_pos_right _ ε0)),
end))
(neg_le.2 $ (abs_neg (f n)) ▸ le_abs_self _)⟩,
let l := nat.find h in
have hl : ∀ (n : ℕ), n ≥ m → f n > a - l • ε := nat.find_spec h,
have hl0 : l ≠ 0 := λ hl0, not_lt_of_ge (ham m (le_refl _))
(lt_of_lt_of_le (by have := hl m (le_refl m); simpa [hl0] using this) (le_abs_self (f m))),
begin
cases not_forall.1
(nat.find_min h (nat.pred_lt hl0)) with i hi,
rw [not_imp, not_lt] at hi,
existsi i,
assume j hj,
have hfij : f j ≤ f i := forall_ge_le_of_forall_le_succ f hnm _ hi.1 hj,
rw [abs_of_nonpos (sub_nonpos.2 hfij), neg_sub, sub_lt_iff_lt_add'],
exact calc f i ≤ a - (nat.pred l) • ε : hi.2
... = a - l • ε + ε :
by conv {to_rhs, rw [← nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hl0), succ_nsmul',
sub_add, add_sub_cancel] }
... < f j + ε : add_lt_add_right (hl j (le_trans hi.1 hj)) _
end
lemma is_cau_of_mono_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a)
(hnm : ∀ n ≥ m, f n ≤ f n.succ) : is_cau_seq abs f :=
begin
refine @eq.rec_on (ℕ → α) _ (is_cau_seq abs) _ _
(-⟨_, @is_cau_of_decreasing_bounded _ _ _ (λ n, -f n) a m (by simpa) (by simpa)⟩ :
cau_seq α abs).2,
ext,
exact neg_neg _
end
end
section no_archimedean
variables {α : Type*} {β : Type*} [ring β]
[linear_ordered_field α] {abv : β → α} [is_absolute_value abv]
lemma is_cau_series_of_abv_le_cau {f : ℕ → β} {g : ℕ → α} (n : ℕ) :
(∀ m, n ≤ m → abv (f m) ≤ g m) →
is_cau_seq abs (λ n, ∑ i in range n, g i) →
is_cau_seq abv (λ n, ∑ i in range n, f i) :=
begin
assume hm hg ε ε0,
cases hg (ε / 2) (div_pos ε0 (by norm_num)) with i hi,
existsi max n i,
assume j ji,
have hi₁ := hi j (le_trans (le_max_right n i) ji),
have hi₂ := hi (max n i) (le_max_right n i),
have sub_le := abs_sub_le (∑ k in range j, g k) (∑ k in range i, g k)
(∑ k in range (max n i), g k),
have := add_lt_add hi₁ hi₂,
rw [abs_sub_comm (∑ k in range (max n i), g k), add_halves ε] at this,
refine lt_of_le_of_lt (le_trans (le_trans _ (le_abs_self _)) sub_le) this,
generalize hk : j - max n i = k,
clear this hi₂ hi₁ hi ε0 ε hg sub_le,
rw nat.sub_eq_iff_eq_add ji at hk,
rw hk,
clear hk ji j,
induction k with k' hi,
{ simp [abv_zero abv] },
{ simp only [nat.succ_add, sum_range_succ_comm, sub_eq_add_neg, add_assoc],
refine le_trans (abv_add _ _ _) _,
simp only [sub_eq_add_neg] at hi,
exact add_le_add (hm _ (le_add_of_nonneg_of_le (nat.zero_le _) (le_max_left _ _))) hi },
end
lemma is_cau_series_of_abv_cau {f : ℕ → β} : is_cau_seq abs (λ m, ∑ n in range m, abv (f n)) →
is_cau_seq abv (λ m, ∑ n in range m, f n) :=
is_cau_series_of_abv_le_cau 0 (λ n h, le_refl _)
end no_archimedean
section
variables {α : Type*} {β : Type*} [ring β]
[linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv]
lemma is_cau_geo_series {β : Type*} [field β] {abv : β → α} [is_absolute_value abv]
(x : β) (hx1 : abv x < 1) : is_cau_seq abv (λ n, ∑ m in range n, x ^ m) :=
have hx1' : abv x ≠ 1 := λ h, by simpa [h, lt_irrefl] using hx1,
is_cau_series_of_abv_cau
begin
simp only [abv_pow abv] {eta := ff},
have : (λ (m : ℕ), ∑ n in range m, (abv x) ^ n) =
λ m, geom_sum (abv x) m := rfl,
simp only [this, geom_sum_eq hx1'] {eta := ff},
conv in (_ / _) { rw [← neg_div_neg_eq, neg_sub, neg_sub] },
refine @is_cau_of_mono_bounded _ _ _ _ ((1 : α) / (1 - abv x)) 0 _ _,
{ assume n hn,
rw abs_of_nonneg,
refine div_le_div_of_le (le_of_lt $ sub_pos.2 hx1)
(sub_le_self _ (abv_pow abv x n ▸ abv_nonneg _ _)),
refine div_nonneg (sub_nonneg.2 _) (sub_nonneg.2 $ le_of_lt hx1),
clear hn,
induction n with n ih,
{ simp },
{ rw [pow_succ, ← one_mul (1 : α)],
refine mul_le_mul (le_of_lt hx1) ih (abv_pow abv x n ▸ abv_nonneg _ _) (by norm_num) } },
{ assume n hn,
refine div_le_div_of_le (le_of_lt $ sub_pos.2 hx1) (sub_le_sub_left _ _),
rw [← one_mul (_ ^ n), pow_succ],
exact mul_le_mul_of_nonneg_right (le_of_lt hx1) (pow_nonneg (abv_nonneg _ _) _) }
end
lemma is_cau_geo_series_const (a : α) {x : α} (hx1 : abs x < 1) :
is_cau_seq abs (λ m, ∑ n in range m, a * x ^ n) :=
have is_cau_seq abs (λ m, a * ∑ n in range m, x ^ n) :=
(cau_seq.const abs a * ⟨_, is_cau_geo_series x hx1⟩).2,
by simpa only [mul_sum]
lemma series_ratio_test {f : ℕ → β} (n : ℕ) (r : α)
(hr0 : 0 ≤ r) (hr1 : r < 1) (h : ∀ m, n ≤ m → abv (f m.succ) ≤ r * abv (f m)) :
is_cau_seq abv (λ m, ∑ n in range m, f n) :=
have har1 : abs r < 1, by rwa abs_of_nonneg hr0,
begin
refine is_cau_series_of_abv_le_cau n.succ _
(is_cau_geo_series_const (abv (f n.succ) * r⁻¹ ^ n.succ) har1),
assume m hmn,
cases classical.em (r = 0) with r_zero r_ne_zero,
{ have m_pos := lt_of_lt_of_le (nat.succ_pos n) hmn,
have := h m.pred (nat.le_of_succ_le_succ (by rwa [nat.succ_pred_eq_of_pos m_pos])),
simpa [r_zero, nat.succ_pred_eq_of_pos m_pos, pow_succ] },
generalize hk : m - n.succ = k,
have r_pos : 0 < r := lt_of_le_of_ne hr0 (ne.symm r_ne_zero),
replace hk : m = k + n.succ := (nat.sub_eq_iff_eq_add hmn).1 hk,
induction k with k ih generalizing m n,
{ rw [hk, zero_add, mul_right_comm, inv_pow' _ _, ← div_eq_mul_inv, mul_div_cancel],
exact (ne_of_lt (pow_pos r_pos _)).symm },
{ have kn : k + n.succ ≥ n.succ, by rw ← zero_add n.succ; exact add_le_add (zero_le _) (by simp),
rw [hk, nat.succ_add, pow_succ' r, ← mul_assoc],
exact le_trans (by rw mul_comm; exact h _ (nat.le_of_succ_le kn))
(mul_le_mul_of_nonneg_right (ih (k + n.succ) n h kn rfl) hr0) }
end
lemma sum_range_diag_flip {α : Type*} [add_comm_monoid α] (n : ℕ) (f : ℕ → ℕ → α) :
∑ m in range n, ∑ k in range (m + 1), f k (m - k) =
∑ m in range n, ∑ k in range (n - m), f m k :=
by rw [sum_sigma', sum_sigma']; exact sum_bij
(λ a _, ⟨a.2, a.1 - a.2⟩)
(λ a ha, have h₁ : a.1 < n := mem_range.1 (mem_sigma.1 ha).1,
have h₂ : a.2 < nat.succ a.1 := mem_range.1 (mem_sigma.1 ha).2,
mem_sigma.2 ⟨mem_range.2 (lt_of_lt_of_le h₂ h₁),
mem_range.2 ((nat.sub_lt_sub_right_iff (nat.le_of_lt_succ h₂)).2 h₁)⟩)
(λ _ _, rfl)
(λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h,
have ha : a₁ < n ∧ a₂ ≤ a₁ :=
⟨mem_range.1 (mem_sigma.1 ha).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 ha).2)⟩,
have hb : b₁ < n ∧ b₂ ≤ b₁ :=
⟨mem_range.1 (mem_sigma.1 hb).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 hb).2)⟩,
have h : a₂ = b₂ ∧ _ := sigma.mk.inj h,
have h' : a₁ = b₁ - b₂ + a₂ := (nat.sub_eq_iff_eq_add ha.2).1 (eq_of_heq h.2),
sigma.mk.inj_iff.2
⟨nat.sub_add_cancel hb.2 ▸ h'.symm ▸ h.1 ▸ rfl,
(heq_of_eq h.1)⟩)
(λ ⟨a₁, a₂⟩ ha,
have ha : a₁ < n ∧ a₂ < n - a₁ :=
⟨mem_range.1 (mem_sigma.1 ha).1, (mem_range.1 (mem_sigma.1 ha).2)⟩,
⟨⟨a₂ + a₁, a₁⟩, ⟨mem_sigma.2 ⟨mem_range.2 (nat.lt_sub_right_iff_add_lt.1 ha.2),
mem_range.2 (nat.lt_succ_of_le (nat.le_add_left _ _))⟩,
sigma.mk.inj_iff.2 ⟨rfl, heq_of_eq (nat.add_sub_cancel _ _).symm⟩⟩⟩)
lemma sum_range_sub_sum_range {α : Type*} [add_comm_group α] {f : ℕ → α}
{n m : ℕ} (hnm : n ≤ m) : ∑ k in range m, f k - ∑ k in range n, f k =
∑ k in (range m).filter (λ k, n ≤ k), f k :=
begin
rw [← sum_sdiff (@filter_subset _ (λ k, n ≤ k) _ (range m)),
sub_eq_iff_eq_add, ← eq_sub_iff_add_eq, add_sub_cancel'],
refine finset.sum_congr
(finset.ext $ λ a, ⟨λ h, by simp at *; finish,
λ h, have ham : a < m := lt_of_lt_of_le (mem_range.1 h) hnm,
by simp * at *⟩)
(λ _ _, rfl),
end
end
section no_archimedean
variables {α : Type*} {β : Type*} [ring β]
[linear_ordered_field α] {abv : β → α} [is_absolute_value abv]
lemma abv_sum_le_sum_abv {γ : Type*} (f : γ → β) (s : finset γ) :
abv (∑ k in s, f k) ≤ ∑ k in s, abv (f k) :=
by haveI := classical.dec_eq γ; exact
finset.induction_on s (by simp [abv_zero abv])
(λ a s has ih, by rw [sum_insert has, sum_insert has];
exact le_trans (abv_add abv _ _) (add_le_add_left ih _))
lemma cauchy_product {a b : ℕ → β}
(ha : is_cau_seq abs (λ m, ∑ n in range m, abv (a n)))
(hb : is_cau_seq abv (λ m, ∑ n in range m, b n)) (ε : α) (ε0 : 0 < ε) :
∃ i : ℕ, ∀ j ≥ i, abv ((∑ k in range j, a k) * (∑ k in range j, b k) -
∑ n in range j, ∑ m in range (n + 1), a m * b (n - m)) < ε :=
let ⟨Q, hQ⟩ := cau_seq.bounded ⟨_, hb⟩ in
let ⟨P, hP⟩ := cau_seq.bounded ⟨_, ha⟩ in
have hP0 : 0 < P, from lt_of_le_of_lt (abs_nonneg _) (hP 0),
have hPε0 : 0 < ε / (2 * P),
from div_pos ε0 (mul_pos (show (2 : α) > 0, from by norm_num) hP0),
let ⟨N, hN⟩ := cau_seq.cauchy₂ ⟨_, hb⟩ hPε0 in
have hQε0 : 0 < ε / (4 * Q),
from div_pos ε0 (mul_pos (show (0 : α) < 4, by norm_num)
(lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))),
let ⟨M, hM⟩ := cau_seq.cauchy₂ ⟨_, ha⟩ hQε0 in
⟨2 * (max N M + 1), λ K hK,
have h₁ : ∑ m in range K, ∑ k in range (m + 1), a k * b (m - k) =
∑ m in range K, ∑ n in range (K - m), a m * b n,
by simpa using sum_range_diag_flip K (λ m n, a m * b n),
have h₂ : (λ i, ∑ k in range (K - i), a i * b k) = (λ i, a i * ∑ k in range (K - i), b k),
by simp [finset.mul_sum],
have h₃ : ∑ i in range K, a i * ∑ k in range (K - i), b k =
∑ i in range K, a i * (∑ k in range (K - i), b k - ∑ k in range K, b k)
+ ∑ i in range K, a i * ∑ k in range K, b k,
by rw ← sum_add_distrib; simp [(mul_add _ _ _).symm],
have two_mul_two : (4 : α) = 2 * 2, by norm_num,
have hQ0 : Q ≠ 0, from λ h, by simpa [h, lt_irrefl] using hQε0,
have h2Q0 : 2 * Q ≠ 0, from mul_ne_zero two_ne_zero hQ0,
have hε : ε / (2 * P) * P + ε / (4 * Q) * (2 * Q) = ε,
by rw [← div_div_eq_div_mul, div_mul_cancel _ (ne.symm (ne_of_lt hP0)),
two_mul_two, mul_assoc, ← div_div_eq_div_mul, div_mul_cancel _ h2Q0, add_halves],
have hNMK : max N M + 1 < K,
from lt_of_lt_of_le (by rw two_mul; exact lt_add_of_pos_left _ (nat.succ_pos _)) hK,
have hKN : N < K,
from calc N ≤ max N M : le_max_left _ _
... < max N M + 1 : nat.lt_succ_self _
... < K : hNMK,
have hsumlesum : ∑ i in range (max N M + 1), abv (a i) *
abv (∑ k in range (K - i), b k - ∑ k in range K, b k) ≤
∑ i in range (max N M + 1), abv (a i) * (ε / (2 * P)),
from sum_le_sum (λ m hmJ, mul_le_mul_of_nonneg_left
(le_of_lt (hN (K - m) K
(nat.le_sub_left_of_add_le (le_trans
(by rw two_mul; exact add_le_add (le_of_lt (mem_range.1 hmJ))
(le_trans (le_max_left _ _) (le_of_lt (lt_add_one _)))) hK))
(le_of_lt hKN))) (abv_nonneg abv _)),
have hsumltP : ∑ n in range (max N M + 1), abv (a n) < P :=
calc ∑ n in range (max N M + 1), abv (a n)
= abs (∑ n in range (max N M + 1), abv (a n)) :
eq.symm (abs_of_nonneg (sum_nonneg (λ x h, abv_nonneg abv (a x))))
... < P : hP (max N M + 1),
begin
rw [h₁, h₂, h₃, sum_mul, ← sub_sub, sub_right_comm, sub_self, zero_sub, abv_neg abv],
refine lt_of_le_of_lt (abv_sum_le_sum_abv _ _) _,
suffices : ∑ i in range (max N M + 1),
abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) +
(∑ i in range K, abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) -
∑ i in range (max N M + 1), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k)) <
ε / (2 * P) * P + ε / (4 * Q) * (2 * Q),
{ rw hε at this, simpa [abv_mul abv] },
refine add_lt_add (lt_of_le_of_lt hsumlesum
(by rw [← sum_mul, mul_comm]; exact (mul_lt_mul_left hPε0).mpr hsumltP)) _,
rw sum_range_sub_sum_range (le_of_lt hNMK),
exact calc ∑ i in (range K).filter (λ k, max N M + 1 ≤ k),
abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k)
≤ ∑ i in (range K).filter (λ k, max N M + 1 ≤ k), abv (a i) * (2 * Q) :
sum_le_sum (λ n hn, begin
refine mul_le_mul_of_nonneg_left _ (abv_nonneg _ _),
rw sub_eq_add_neg,
refine le_trans (abv_add _ _ _) _,
rw [two_mul, abv_neg abv],
exact add_le_add (le_of_lt (hQ _)) (le_of_lt (hQ _)),
end)
... < ε / (4 * Q) * (2 * Q) :
by rw [← sum_mul, ← sum_range_sub_sum_range (le_of_lt hNMK)];
refine (mul_lt_mul_right $ by rw two_mul;
exact add_pos (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))
(lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))).2
(lt_of_le_of_lt (le_abs_self _)
(hM _ _ (le_trans (nat.le_succ_of_le (le_max_right _ _)) (le_of_lt hNMK))
(nat.le_succ_of_le (le_max_right _ _))))
end⟩
end no_archimedean
end
open finset
open cau_seq
namespace complex
lemma is_cau_abs_exp (z : ℂ) : is_cau_seq _root_.abs
(λ n, ∑ m in range n, abs (z ^ m / m!)) :=
let ⟨n, hn⟩ := exists_nat_gt (abs z) in
have hn0 : (0 : ℝ) < n, from lt_of_le_of_lt (abs_nonneg _) hn,
series_ratio_test n (complex.abs z / n) (div_nonneg (complex.abs_nonneg _) (le_of_lt hn0))
(by rwa [div_lt_iff hn0, one_mul])
(λ m hm,
by rw [abs_abs, abs_abs, nat.factorial_succ, pow_succ,
mul_comm m.succ, nat.cast_mul, ← div_div_eq_div_mul, mul_div_assoc,
mul_div_right_comm, abs_mul, abs_div, abs_cast_nat];
exact mul_le_mul_of_nonneg_right
(div_le_div_of_le_left (abs_nonneg _) hn0
(nat.cast_le.2 (le_trans hm (nat.le_succ _)))) (abs_nonneg _))
noncomputable theory
lemma is_cau_exp (z : ℂ) :
is_cau_seq abs (λ n, ∑ m in range n, z ^ m / m!) :=
is_cau_series_of_abv_cau (is_cau_abs_exp z)
/-- The Cauchy sequence consisting of partial sums of the Taylor series of
the complex exponential function -/
@[pp_nodot] def exp' (z : ℂ) :
cau_seq ℂ complex.abs :=
⟨λ n, ∑ m in range n, z ^ m / m!, is_cau_exp z⟩
/-- The complex exponential function, defined via its Taylor series -/
@[pp_nodot] def exp (z : ℂ) : ℂ := lim (exp' z)
/-- The complex sine function, defined via `exp` -/
@[pp_nodot] def sin (z : ℂ) : ℂ := ((exp (-z * I) - exp (z * I)) * I) / 2
/-- The complex cosine function, defined via `exp` -/
@[pp_nodot] def cos (z : ℂ) : ℂ := (exp (z * I) + exp (-z * I)) / 2
/-- The complex tangent function, defined as `sin z / cos z` -/
@[pp_nodot] def tan (z : ℂ) : ℂ := sin z / cos z
/-- The complex hyperbolic sine function, defined via `exp` -/
@[pp_nodot] def sinh (z : ℂ) : ℂ := (exp z - exp (-z)) / 2
/-- The complex hyperbolic cosine function, defined via `exp` -/
@[pp_nodot] def cosh (z : ℂ) : ℂ := (exp z + exp (-z)) / 2
/-- The complex hyperbolic tangent function, defined as `sinh z / cosh z` -/
@[pp_nodot] def tanh (z : ℂ) : ℂ := sinh z / cosh z
end complex
namespace real
open complex
/-- The real exponential function, defined as the real part of the complex exponential -/
@[pp_nodot] def exp (x : ℝ) : ℝ := (exp x).re
/-- The real sine function, defined as the real part of the complex sine -/
@[pp_nodot] def sin (x : ℝ) : ℝ := (sin x).re
/-- The real cosine function, defined as the real part of the complex cosine -/
@[pp_nodot] def cos (x : ℝ) : ℝ := (cos x).re
/-- The real tangent function, defined as the real part of the complex tangent -/
@[pp_nodot] def tan (x : ℝ) : ℝ := (tan x).re
/-- The real hypebolic sine function, defined as the real part of the complex hyperbolic sine -/
@[pp_nodot] def sinh (x : ℝ) : ℝ := (sinh x).re
/-- The real hypebolic cosine function, defined as the real part of the complex hyperbolic cosine -/
@[pp_nodot] def cosh (x : ℝ) : ℝ := (cosh x).re
/-- The real hypebolic tangent function, defined as the real part of
the complex hyperbolic tangent -/
@[pp_nodot] def tanh (x : ℝ) : ℝ := (tanh x).re
end real
namespace complex
variables (x y : ℂ)
@[simp] lemma exp_zero : exp 0 = 1 :=
lim_eq_of_equiv_const $
λ ε ε0, ⟨1, λ j hj, begin
convert ε0,
cases j,
{ exact absurd hj (not_le_of_gt zero_lt_one) },
{ dsimp [exp'],
induction j with j ih,
{ dsimp [exp']; simp },
{ rw ← ih dec_trivial,
simp only [sum_range_succ, pow_succ],
simp } }
end⟩
lemma exp_add : exp (x + y) = exp x * exp y :=
show lim (⟨_, is_cau_exp (x + y)⟩ : cau_seq ℂ abs) =
lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp x⟩)
* lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp y⟩),
from
have hj : ∀ j : ℕ, ∑ m in range j, (x + y) ^ m / m! =
∑ i in range j, ∑ k in range (i + 1), x ^ k / k! * (y ^ (i - k) / (i - k)!),
from assume j,
finset.sum_congr rfl (λ m hm, begin
rw [add_pow, div_eq_mul_inv, sum_mul],
refine finset.sum_congr rfl (λ i hi, _),
have h₁ : (m.choose i : ℂ) ≠ 0 := nat.cast_ne_zero.2
(pos_iff_ne_zero.1 (nat.choose_pos (nat.le_of_lt_succ (mem_range.1 hi)))),
have h₂ := nat.choose_mul_factorial_mul_factorial (nat.le_of_lt_succ $ finset.mem_range.1 hi),
rw [← h₂, nat.cast_mul, nat.cast_mul, mul_inv', mul_inv'],
simp only [mul_left_comm (m.choose i : ℂ), mul_assoc, mul_left_comm (m.choose i : ℂ)⁻¹,
mul_comm (m.choose i : ℂ)],
rw inv_mul_cancel h₁,
simp [div_eq_mul_inv, mul_comm, mul_assoc, mul_left_comm]
end),
by rw lim_mul_lim;
exact eq.symm (lim_eq_lim_of_equiv (by dsimp; simp only [hj];
exact cauchy_product (is_cau_abs_exp x) (is_cau_exp y)))
attribute [irreducible] complex.exp
lemma exp_list_sum (l : list ℂ) : exp l.sum = (l.map exp).prod :=
@monoid_hom.map_list_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ l
lemma exp_multiset_sum (s : multiset ℂ) : exp s.sum = (s.map exp).prod :=
@monoid_hom.map_multiset_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ s
lemma exp_sum {α : Type*} (s : finset α) (f : α → ℂ) : exp (∑ x in s, f x) = ∏ x in s, exp (f x) :=
@monoid_hom.map_prod (multiplicative ℂ) α ℂ _ _ ⟨exp, exp_zero, exp_add⟩ f s
lemma exp_nat_mul (x : ℂ) : ∀ n : ℕ, exp(n*x) = (exp x)^n
| 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero]
| (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul]
lemma exp_ne_zero : exp x ≠ 0 :=
λ h, zero_ne_one $ by rw [← exp_zero, ← add_neg_self x, exp_add, h]; simp
lemma exp_neg : exp (-x) = (exp x)⁻¹ :=
by rw [← mul_right_inj' (exp_ne_zero x), ← exp_add];
simp [mul_inv_cancel (exp_ne_zero x)]
lemma exp_sub : exp (x - y) = exp x / exp y :=
by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv]
lemma exp_int_mul (z : ℂ) (n : ℤ) : complex.exp (n * z) = (complex.exp z) ^ n :=
begin
cases n,
{ apply complex.exp_nat_mul },
{ simpa [complex.exp_neg, add_comm, ← neg_mul_eq_neg_mul_symm]
using complex.exp_nat_mul (-z) (1 + n) },
end
@[simp] lemma exp_conj : exp (conj x) = conj (exp x) :=
begin
dsimp [exp],
rw [← lim_conj],
refine congr_arg lim (cau_seq.ext (λ _, _)),
dsimp [exp', function.comp, cau_seq_conj],
rw conj.map_sum,
refine sum_congr rfl (λ n hn, _),
rw [conj.map_div, conj.map_pow, ← of_real_nat_cast, conj_of_real]
end
@[simp] lemma of_real_exp_of_real_re (x : ℝ) : ((exp x).re : ℂ) = exp x :=
eq_conj_iff_re.1 $ by rw [← exp_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_exp (x : ℝ) : (real.exp x : ℂ) = exp x :=
of_real_exp_of_real_re _
@[simp] lemma exp_of_real_im (x : ℝ) : (exp x).im = 0 :=
by rw [← of_real_exp_of_real_re, of_real_im]
lemma exp_of_real_re (x : ℝ) : (exp x).re = real.exp x := rfl
lemma two_sinh : 2 * sinh x = exp x - exp (-x) :=
mul_div_cancel' _ two_ne_zero'
lemma two_cosh : 2 * cosh x = exp x + exp (-x) :=
mul_div_cancel' _ two_ne_zero'
@[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh]
@[simp] lemma sinh_neg : sinh (-x) = -sinh x :=
by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul]
private lemma sinh_add_aux {a b c d : ℂ} :
(a - b) * (c + d) + (a + b) * (c - d) = 2 * (a * c - b * d) := by ring
lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y :=
begin
rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_sinh,
exp_add, neg_add, exp_add, eq_comm,
mul_add, ← mul_assoc, two_sinh, mul_left_comm, two_sinh,
← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_add,
mul_left_comm, two_cosh, ← mul_assoc, two_cosh],
exact sinh_add_aux
end
@[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh]
@[simp] lemma cosh_neg : cosh (-x) = cosh x :=
by simp [add_comm, cosh, exp_neg]
private lemma cosh_add_aux {a b c d : ℂ} :
(a + b) * (c + d) + (a - b) * (c - d) = 2 * (a * c + b * d) := by ring
lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y :=
begin
rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_cosh,
exp_add, neg_add, exp_add, eq_comm,
mul_add, ← mul_assoc, two_cosh, ← mul_assoc, two_sinh,
← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_add,
mul_left_comm, two_cosh, mul_left_comm, two_sinh],
exact cosh_add_aux
end
lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y :=
by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg]
lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y :=
by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg]
lemma sinh_conj : sinh (conj x) = conj (sinh x) :=
by rw [sinh, ← conj.map_neg, exp_conj, exp_conj, ← conj.map_sub, sinh, conj.map_div, conj_bit0,
conj.map_one]
@[simp] lemma of_real_sinh_of_real_re (x : ℝ) : ((sinh x).re : ℂ) = sinh x :=
eq_conj_iff_re.1 $ by rw [← sinh_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_sinh (x : ℝ) : (real.sinh x : ℂ) = sinh x :=
of_real_sinh_of_real_re _
@[simp] lemma sinh_of_real_im (x : ℝ) : (sinh x).im = 0 :=
by rw [← of_real_sinh_of_real_re, of_real_im]
lemma sinh_of_real_re (x : ℝ) : (sinh x).re = real.sinh x := rfl
lemma cosh_conj : cosh (conj x) = conj (cosh x) :=
begin
rw [cosh, ← conj.map_neg, exp_conj, exp_conj, ← conj.map_add, cosh, conj.map_div,
conj_bit0, conj.map_one]
end
@[simp] lemma of_real_cosh_of_real_re (x : ℝ) : ((cosh x).re : ℂ) = cosh x :=
eq_conj_iff_re.1 $ by rw [← cosh_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_cosh (x : ℝ) : (real.cosh x : ℂ) = cosh x :=
of_real_cosh_of_real_re _
@[simp] lemma cosh_of_real_im (x : ℝ) : (cosh x).im = 0 :=
by rw [← of_real_cosh_of_real_re, of_real_im]
lemma cosh_of_real_re (x : ℝ) : (cosh x).re = real.cosh x := rfl
lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := rfl
@[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh]
@[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div]
lemma tanh_conj : tanh (conj x) = conj (tanh x) :=
by rw [tanh, sinh_conj, cosh_conj, ← conj.map_div, tanh]
@[simp] lemma of_real_tanh_of_real_re (x : ℝ) : ((tanh x).re : ℂ) = tanh x :=
eq_conj_iff_re.1 $ by rw [← tanh_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_tanh (x : ℝ) : (real.tanh x : ℂ) = tanh x :=
of_real_tanh_of_real_re _
@[simp] lemma tanh_of_real_im (x : ℝ) : (tanh x).im = 0 :=
by rw [← of_real_tanh_of_real_re, of_real_im]
lemma tanh_of_real_re (x : ℝ) : (tanh x).re = real.tanh x := rfl
lemma cosh_add_sinh : cosh x + sinh x = exp x :=
by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_add,
two_cosh, two_sinh, add_add_sub_cancel, two_mul]
lemma sinh_add_cosh : sinh x + cosh x = exp x :=
by rw [add_comm, cosh_add_sinh]
lemma cosh_sub_sinh : cosh x - sinh x = exp (-x) :=
by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_sub,
two_cosh, two_sinh, add_sub_sub_cancel, two_mul]
lemma cosh_sq_sub_sinh_sq : cosh x ^ 2 - sinh x ^ 2 = 1 :=
by rw [sq_sub_sq, cosh_add_sinh, cosh_sub_sinh, ← exp_add, add_neg_self, exp_zero]
lemma cosh_sq : cosh x ^ 2 = sinh x ^ 2 + 1 :=
begin
rw ← cosh_sq_sub_sinh_sq x,
ring
end
lemma sinh_sq : sinh x ^ 2 = cosh x ^ 2 - 1 :=
begin
rw ← cosh_sq_sub_sinh_sq x,
ring
end
lemma cosh_two_mul : cosh (2 * x) = cosh x ^ 2 + sinh x ^ 2 :=
by rw [two_mul, cosh_add, sq, sq]
lemma sinh_two_mul : sinh (2 * x) = 2 * sinh x * cosh x :=
begin
rw [two_mul, sinh_add],
ring
end
lemma cosh_three_mul : cosh (3 * x) = 4 * cosh x ^ 3 - 3 * cosh x :=
begin
have h1 : x + 2 * x = 3 * x, by ring,
rw [← h1, cosh_add x (2 * x)],
simp only [cosh_two_mul, sinh_two_mul],
have h2 : sinh x * (2 * sinh x * cosh x) = 2 * cosh x * sinh x ^ 2, by ring,
rw [h2, sinh_sq],
ring
end
lemma sinh_three_mul : sinh (3 * x) = 4 * sinh x ^ 3 + 3 * sinh x :=
begin
have h1 : x + 2 * x = 3 * x, by ring,
rw [← h1, sinh_add x (2 * x)],
simp only [cosh_two_mul, sinh_two_mul],
have h2 : cosh x * (2 * sinh x * cosh x) = 2 * sinh x * cosh x ^ 2, by ring,
rw [h2, cosh_sq],
ring,
end
@[simp] lemma sin_zero : sin 0 = 0 := by simp [sin]
@[simp] lemma sin_neg : sin (-x) = -sin x :=
by simp [sin, sub_eq_add_neg, exp_neg, (neg_div _ _).symm, add_mul]
lemma two_sin : 2 * sin x = (exp (-x * I) - exp (x * I)) * I :=
mul_div_cancel' _ two_ne_zero'
lemma two_cos : 2 * cos x = exp (x * I) + exp (-x * I) :=
mul_div_cancel' _ two_ne_zero'
lemma sinh_mul_I : sinh (x * I) = sin x * I :=
by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_sinh,
← mul_assoc, two_sin, mul_assoc, I_mul_I, mul_neg_one,
neg_sub, neg_mul_eq_neg_mul]
lemma cosh_mul_I : cosh (x * I) = cos x :=
by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_cosh,
two_cos, neg_mul_eq_neg_mul]
lemma tanh_mul_I : tanh (x * I) = tan x * I :=
by rw [tanh_eq_sinh_div_cosh, cosh_mul_I, sinh_mul_I, mul_div_right_comm, tan]
lemma cos_mul_I : cos (x * I) = cosh x :=
by rw ← cosh_mul_I; ring_nf; simp
lemma sin_mul_I : sin (x * I) = sinh x * I :=
have h : I * sin (x * I) = -sinh x := by { rw [mul_comm, ← sinh_mul_I], ring_nf, simp },
by simpa only [neg_mul_eq_neg_mul_symm, div_I, neg_neg]
using cancel_factors.cancel_factors_eq_div h I_ne_zero
lemma tan_mul_I : tan (x * I) = tanh x * I :=
by rw [tan, sin_mul_I, cos_mul_I, mul_div_right_comm, tanh_eq_sinh_div_cosh]
lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y :=
by rw [← mul_left_inj' I_ne_zero, ← sinh_mul_I,
add_mul, add_mul, mul_right_comm, ← sinh_mul_I,
mul_assoc, ← sinh_mul_I, ← cosh_mul_I, ← cosh_mul_I, sinh_add]
@[simp] lemma cos_zero : cos 0 = 1 := by simp [cos]
@[simp] lemma cos_neg : cos (-x) = cos x :=
by simp [cos, sub_eq_add_neg, exp_neg, add_comm]
private lemma cos_add_aux {a b c d : ℂ} :
(a + b) * (c + d) - (b - a) * (d - c) * (-1) =
2 * (a * c + b * d) := by ring
lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y :=
by rw [← cosh_mul_I, add_mul, cosh_add, cosh_mul_I, cosh_mul_I,
sinh_mul_I, sinh_mul_I, mul_mul_mul_comm, I_mul_I,
mul_neg_one, sub_eq_add_neg]
lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y :=
by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg]
lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y :=
by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg]
lemma sin_add_mul_I (x y : ℂ) : sin (x + y*I) = sin x * cosh y + cos x * sinh y * I :=
by rw [sin_add, cos_mul_I, sin_mul_I, mul_assoc]
lemma sin_eq (z : ℂ) : sin z = sin z.re * cosh z.im + cos z.re * sinh z.im * I :=
by convert sin_add_mul_I z.re z.im; exact (re_add_im z).symm
lemma cos_add_mul_I (x y : ℂ) : cos (x + y*I) = cos x * cosh y - sin x * sinh y * I :=
by rw [cos_add, cos_mul_I, sin_mul_I, mul_assoc]
lemma cos_eq (z : ℂ) : cos z = cos z.re * cosh z.im - sin z.re * sinh z.im * I :=
by convert cos_add_mul_I z.re z.im; exact (re_add_im z).symm
theorem sin_sub_sin : sin x - sin y = 2 * sin((x - y)/2) * cos((x + y)/2) :=
begin
have s1 := sin_add ((x + y) / 2) ((x - y) / 2),
have s2 := sin_sub ((x + y) / 2) ((x - y) / 2),
rw [div_add_div_same, add_sub, add_right_comm, add_sub_cancel, half_add_self] at s1,
rw [div_sub_div_same, ←sub_add, add_sub_cancel', half_add_self] at s2,
rw [s1, s2],
ring
end
theorem cos_sub_cos : cos x - cos y = -2 * sin((x + y)/2) * sin((x - y)/2) :=
begin
have s1 := cos_add ((x + y) / 2) ((x - y) / 2),
have s2 := cos_sub ((x + y) / 2) ((x - y) / 2),
rw [div_add_div_same, add_sub, add_right_comm, add_sub_cancel, half_add_self] at s1,
rw [div_sub_div_same, ←sub_add, add_sub_cancel', half_add_self] at s2,
rw [s1, s2],
ring,
end
lemma cos_add_cos : cos x + cos y = 2 * cos ((x + y) / 2) * cos ((x - y) / 2) :=
begin
have h2 : (2:ℂ) ≠ 0 := by norm_num,
calc cos x + cos y = cos ((x + y) / 2 + (x - y) / 2) + cos ((x + y) / 2 - (x - y) / 2) : _
... = (cos ((x + y) / 2) * cos ((x - y) / 2) - sin ((x + y) / 2) * sin ((x - y) / 2))
+ (cos ((x + y) / 2) * cos ((x - y) / 2) + sin ((x + y) / 2) * sin ((x - y) / 2)) : _
... = 2 * cos ((x + y) / 2) * cos ((x - y) / 2) : _,
{ congr; field_simp [h2]; ring },
{ rw [cos_add, cos_sub] },
ring,
end
lemma sin_conj : sin (conj x) = conj (sin x) :=
by rw [← mul_left_inj' I_ne_zero, ← sinh_mul_I,
← conj_neg_I, ← conj.map_mul, ← conj.map_mul, sinh_conj,
mul_neg_eq_neg_mul_symm, sinh_neg, sinh_mul_I, mul_neg_eq_neg_mul_symm]
@[simp] lemma of_real_sin_of_real_re (x : ℝ) : ((sin x).re : ℂ) = sin x :=
eq_conj_iff_re.1 $ by rw [← sin_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_sin (x : ℝ) : (real.sin x : ℂ) = sin x :=
of_real_sin_of_real_re _
@[simp] lemma sin_of_real_im (x : ℝ) : (sin x).im = 0 :=
by rw [← of_real_sin_of_real_re, of_real_im]
lemma sin_of_real_re (x : ℝ) : (sin x).re = real.sin x := rfl
lemma cos_conj : cos (conj x) = conj (cos x) :=
by rw [← cosh_mul_I, ← conj_neg_I, ← conj.map_mul, ← cosh_mul_I,
cosh_conj, mul_neg_eq_neg_mul_symm, cosh_neg]
@[simp] lemma of_real_cos_of_real_re (x : ℝ) : ((cos x).re : ℂ) = cos x :=
eq_conj_iff_re.1 $ by rw [← cos_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_cos (x : ℝ) : (real.cos x : ℂ) = cos x :=
of_real_cos_of_real_re _
@[simp] lemma cos_of_real_im (x : ℝ) : (cos x).im = 0 :=
by rw [← of_real_cos_of_real_re, of_real_im]
lemma cos_of_real_re (x : ℝ) : (cos x).re = real.cos x := rfl
@[simp] lemma tan_zero : tan 0 = 0 := by simp [tan]
lemma tan_eq_sin_div_cos : tan x = sin x / cos x := rfl
lemma tan_mul_cos {x : ℂ} (hx : cos x ≠ 0) : tan x * cos x = sin x :=
by rw [tan_eq_sin_div_cos, div_mul_cancel _ hx]
@[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div]
lemma tan_conj : tan (conj x) = conj (tan x) :=
by rw [tan, sin_conj, cos_conj, ← conj.map_div, tan]
@[simp] lemma of_real_tan_of_real_re (x : ℝ) : ((tan x).re : ℂ) = tan x :=
eq_conj_iff_re.1 $ by rw [← tan_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_tan (x : ℝ) : (real.tan x : ℂ) = tan x :=
of_real_tan_of_real_re _
@[simp] lemma tan_of_real_im (x : ℝ) : (tan x).im = 0 :=
by rw [← of_real_tan_of_real_re, of_real_im]
lemma tan_of_real_re (x : ℝ) : (tan x).re = real.tan x := rfl
lemma cos_add_sin_I : cos x + sin x * I = exp (x * I) :=
by rw [← cosh_add_sinh, sinh_mul_I, cosh_mul_I]
lemma cos_sub_sin_I : cos x - sin x * I = exp (-x * I) :=
by rw [← neg_mul_eq_neg_mul, ← cosh_sub_sinh, sinh_mul_I, cosh_mul_I]
@[simp] lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 :=
eq.trans
(by rw [cosh_mul_I, sinh_mul_I, mul_pow, I_sq, mul_neg_one, sub_neg_eq_add, add_comm])
(cosh_sq_sub_sinh_sq (x * I))
@[simp] lemma cos_sq_add_sin_sq : cos x ^ 2 + sin x ^ 2 = 1 :=
by rw [add_comm, sin_sq_add_cos_sq]
lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 :=
by rw [two_mul, cos_add, ← sq, ← sq]
lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 :=
by rw [cos_two_mul', eq_sub_iff_add_eq.2 (sin_sq_add_cos_sq x),
← sub_add, sub_add_eq_add_sub, two_mul]
lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x :=
by rw [two_mul, sin_add, two_mul, add_mul, mul_comm]
lemma cos_sq : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 :=
by simp [cos_two_mul, div_add_div_same, mul_div_cancel_left, two_ne_zero', -one_div]
lemma cos_sq' : cos x ^ 2 = 1 - sin x ^ 2 :=
by rw [←sin_sq_add_cos_sq x, add_sub_cancel']
lemma sin_sq : sin x ^ 2 = 1 - cos x ^ 2 :=
by rw [←sin_sq_add_cos_sq x, add_sub_cancel]
lemma inv_one_add_tan_sq {x : ℂ} (hx : cos x ≠ 0) : (1 + tan x ^ 2)⁻¹ = cos x ^ 2 :=
have cos x ^ 2 ≠ 0, from pow_ne_zero 2 hx,
by { rw [tan_eq_sin_div_cos, div_pow], field_simp [this] }
lemma tan_sq_div_one_add_tan_sq {x : ℂ} (hx : cos x ≠ 0) :
tan x ^ 2 / (1 + tan x ^ 2) = sin x ^ 2 :=
by simp only [← tan_mul_cos hx, mul_pow, ← inv_one_add_tan_sq hx, div_eq_mul_inv, one_mul]
lemma cos_three_mul : cos (3 * x) = 4 * cos x ^ 3 - 3 * cos x :=
begin
have h1 : x + 2 * x = 3 * x, by ring,
rw [← h1, cos_add x (2 * x)],
simp only [cos_two_mul, sin_two_mul, mul_add, mul_sub, mul_one, sq],
have h2 : 4 * cos x ^ 3 = 2 * cos x * cos x * cos x + 2 * cos x * cos x ^ 2, by ring,
rw [h2, cos_sq'],
ring
end
lemma sin_three_mul : sin (3 * x) = 3 * sin x - 4 * sin x ^ 3 :=
begin
have h1 : x + 2 * x = 3 * x, by ring,
rw [← h1, sin_add x (2 * x)],
simp only [cos_two_mul, sin_two_mul, cos_sq'],
have h2 : cos x * (2 * sin x * cos x) = 2 * sin x * cos x ^ 2, by ring,
rw [h2, cos_sq'],
ring
end
lemma exp_mul_I : exp (x * I) = cos x + sin x * I :=
(cos_add_sin_I _).symm
lemma exp_add_mul_I : exp (x + y * I) = exp x * (cos y + sin y * I) :=
by rw [exp_add, exp_mul_I]
lemma exp_eq_exp_re_mul_sin_add_cos : exp x = exp x.re * (cos x.im + sin x.im * I) :=
by rw [← exp_add_mul_I, re_add_im]
lemma exp_re : (exp x).re = real.exp x.re * real.cos x.im :=
by { rw [exp_eq_exp_re_mul_sin_add_cos], simp [exp_of_real_re, cos_of_real_re] }
lemma exp_im : (exp x).im = real.exp x.re * real.sin x.im :=
by { rw [exp_eq_exp_re_mul_sin_add_cos], simp [exp_of_real_re, sin_of_real_re] }
/-- **De Moivre's formula** -/
theorem cos_add_sin_mul_I_pow (n : ℕ) (z : ℂ) :
(cos z + sin z * I) ^ n = cos (↑n * z) + sin (↑n * z) * I :=
begin
rw [← exp_mul_I, ← exp_mul_I],
induction n with n ih,
{ rw [pow_zero, nat.cast_zero, zero_mul, zero_mul, exp_zero] },
{ rw [pow_succ', ih, nat.cast_succ, add_mul, add_mul, one_mul, exp_add] }
end
end complex
namespace real
open complex
variables (x y : ℝ)
@[simp] lemma exp_zero : exp 0 = 1 :=
by simp [real.exp]
lemma exp_add : exp (x + y) = exp x * exp y :=
by simp [exp_add, exp]
lemma exp_list_sum (l : list ℝ) : exp l.sum = (l.map exp).prod :=
@monoid_hom.map_list_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ l
lemma exp_multiset_sum (s : multiset ℝ) : exp s.sum = (s.map exp).prod :=
@monoid_hom.map_multiset_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ s
lemma exp_sum {α : Type*} (s : finset α) (f : α → ℝ) : exp (∑ x in s, f x) = ∏ x in s, exp (f x) :=
@monoid_hom.map_prod (multiplicative ℝ) α ℝ _ _ ⟨exp, exp_zero, exp_add⟩ f s
lemma exp_nat_mul (x : ℝ) : ∀ n : ℕ, exp(n*x) = (exp x)^n
| 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero]
| (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul]
lemma exp_ne_zero : exp x ≠ 0 :=
λ h, exp_ne_zero x $ by rw [exp, ← of_real_inj] at h; simp * at *
lemma exp_neg : exp (-x) = (exp x)⁻¹ :=
by rw [← of_real_inj, exp, of_real_exp_of_real_re, of_real_neg, exp_neg,
of_real_inv, of_real_exp]
lemma exp_sub : exp (x - y) = exp x / exp y :=
by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv]
@[simp] lemma sin_zero : sin 0 = 0 := by simp [sin]
@[simp] lemma sin_neg : sin (-x) = -sin x :=
by simp [sin, exp_neg, (neg_div _ _).symm, add_mul]
lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y :=
by rw [← of_real_inj]; simp [sin, sin_add]
@[simp] lemma cos_zero : cos 0 = 1 := by simp [cos]
@[simp] lemma cos_neg : cos (-x) = cos x :=
by simp [cos, exp_neg]
lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y :=
by rw ← of_real_inj; simp [cos, cos_add]
lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y :=
by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg]
lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y :=
by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg]
lemma sin_sub_sin : sin x - sin y = 2 * sin((x - y)/2) * cos((x + y)/2) :=
begin
rw ← of_real_inj,
simp only [sin, cos, of_real_sin_of_real_re, of_real_sub, of_real_add, of_real_div, of_real_mul,
of_real_one, of_real_bit0],
convert sin_sub_sin _ _;
norm_cast
end
theorem cos_sub_cos : cos x - cos y = -2 * sin((x + y)/2) * sin((x - y)/2) :=
begin
rw ← of_real_inj,
simp only [cos, neg_mul_eq_neg_mul_symm, of_real_sin, of_real_sub, of_real_add,
of_real_cos_of_real_re, of_real_div, of_real_mul, of_real_one, of_real_neg, of_real_bit0],
convert cos_sub_cos _ _,
ring,
end
lemma cos_add_cos : cos x + cos y = 2 * cos ((x + y) / 2) * cos ((x - y) / 2) :=
begin
rw ← of_real_inj,
simp only [cos, of_real_sub, of_real_add, of_real_cos_of_real_re, of_real_div, of_real_mul,
of_real_one, of_real_bit0],
convert cos_add_cos _ _;
norm_cast,
end
lemma tan_eq_sin_div_cos : tan x = sin x / cos x :=
by rw [← of_real_inj, of_real_tan, tan_eq_sin_div_cos, of_real_div, of_real_sin, of_real_cos]
lemma tan_mul_cos {x : ℝ} (hx : cos x ≠ 0) : tan x * cos x = sin x :=
by rw [tan_eq_sin_div_cos, div_mul_cancel _ hx]
@[simp] lemma tan_zero : tan 0 = 0 := by simp [tan]
@[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div]
@[simp] lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 :=
of_real_inj.1 $ by simp
@[simp] lemma cos_sq_add_sin_sq : cos x ^ 2 + sin x ^ 2 = 1 :=
by rw [add_comm, sin_sq_add_cos_sq]
lemma sin_sq_le_one : sin x ^ 2 ≤ 1 :=
by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_right (sq_nonneg _)
lemma cos_sq_le_one : cos x ^ 2 ≤ 1 :=
by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_left (sq_nonneg _)
lemma abs_sin_le_one : abs' (sin x) ≤ 1 :=
abs_le_one_iff_mul_self_le_one.2 $ by simp only [← sq, sin_sq_le_one]
lemma abs_cos_le_one : abs' (cos x) ≤ 1 :=
abs_le_one_iff_mul_self_le_one.2 $ by simp only [← sq, cos_sq_le_one]
lemma sin_le_one : sin x ≤ 1 :=
(abs_le.1 (abs_sin_le_one _)).2
lemma cos_le_one : cos x ≤ 1 :=
(abs_le.1 (abs_cos_le_one _)).2
lemma neg_one_le_sin : -1 ≤ sin x :=
(abs_le.1 (abs_sin_le_one _)).1
lemma neg_one_le_cos : -1 ≤ cos x :=
(abs_le.1 (abs_cos_le_one _)).1
lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 :=
by rw ← of_real_inj; simp [cos_two_mul]
lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 :=
by rw ← of_real_inj; simp [cos_two_mul']
lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x :=
by rw ← of_real_inj; simp [sin_two_mul]
lemma cos_sq : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 :=
of_real_inj.1 $ by simpa using cos_sq x
lemma cos_sq' : cos x ^ 2 = 1 - sin x ^ 2 :=
by rw [←sin_sq_add_cos_sq x, add_sub_cancel']
lemma sin_sq : sin x ^ 2 = 1 - cos x ^ 2 :=
eq_sub_iff_add_eq.2 $ sin_sq_add_cos_sq _
lemma abs_sin_eq_sqrt_one_sub_cos_sq (x : ℝ) :
abs' (sin x) = sqrt (1 - cos x ^ 2) :=
by rw [← sin_sq, sqrt_sq_eq_abs]
lemma abs_cos_eq_sqrt_one_sub_sin_sq (x : ℝ) :
abs' (cos x) = sqrt (1 - sin x ^ 2) :=
by rw [← cos_sq', sqrt_sq_eq_abs]
lemma inv_one_add_tan_sq {x : ℝ} (hx : cos x ≠ 0) : (1 + tan x ^ 2)⁻¹ = cos x ^ 2 :=
have complex.cos x ≠ 0, from mt (congr_arg re) hx,
of_real_inj.1 $ by simpa using complex.inv_one_add_tan_sq this
lemma tan_sq_div_one_add_tan_sq {x : ℝ} (hx : cos x ≠ 0) :
tan x ^ 2 / (1 + tan x ^ 2) = sin x ^ 2 :=
by simp only [← tan_mul_cos hx, mul_pow, ← inv_one_add_tan_sq hx, div_eq_mul_inv, one_mul]
lemma inv_sqrt_one_add_tan_sq {x : ℝ} (hx : 0 < cos x) :
(sqrt (1 + tan x ^ 2))⁻¹ = cos x :=
by rw [← sqrt_sq hx.le, ← sqrt_inv, inv_one_add_tan_sq hx.ne']
lemma tan_div_sqrt_one_add_tan_sq {x : ℝ} (hx : 0 < cos x) :
tan x / sqrt (1 + tan x ^ 2) = sin x :=
by rw [← tan_mul_cos hx.ne', ← inv_sqrt_one_add_tan_sq hx, div_eq_mul_inv]
lemma cos_three_mul : cos (3 * x) = 4 * cos x ^ 3 - 3 * cos x :=
by rw ← of_real_inj; simp [cos_three_mul]
lemma sin_three_mul : sin (3 * x) = 3 * sin x - 4 * sin x ^ 3 :=
by rw ← of_real_inj; simp [sin_three_mul]
/-- The definition of `sinh` in terms of `exp`. -/
lemma sinh_eq (x : ℝ) : sinh x = (exp x - exp (-x)) / 2 :=
eq_div_of_mul_eq two_ne_zero $ by rw [sinh, exp, exp, complex.of_real_neg, complex.sinh, mul_two,
← complex.add_re, ← mul_two, div_mul_cancel _ (two_ne_zero' : (2 : ℂ) ≠ 0), complex.sub_re]
@[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh]
@[simp] lemma sinh_neg : sinh (-x) = -sinh x :=
by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul]
lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y :=
by rw ← of_real_inj; simp [sinh_add]
/-- The definition of `cosh` in terms of `exp`. -/
lemma cosh_eq (x : ℝ) : cosh x = (exp x + exp (-x)) / 2 :=
eq_div_of_mul_eq two_ne_zero $ by rw [cosh, exp, exp, complex.of_real_neg, complex.cosh, mul_two,
← complex.add_re, ← mul_two, div_mul_cancel _ (two_ne_zero' : (2 : ℂ) ≠ 0), complex.add_re]
@[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh]
@[simp] lemma cosh_neg : cosh (-x) = cosh x :=
by simp [cosh, exp_neg]
lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y :=
by rw ← of_real_inj; simp [cosh, cosh_add]
lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y :=
by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg]
lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y :=
by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg]
lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x :=
of_real_inj.1 $ by simp [tanh_eq_sinh_div_cosh]
@[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh]
@[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div]
lemma cosh_add_sinh : cosh x + sinh x = exp x :=
by rw ← of_real_inj; simp [cosh_add_sinh]
lemma sinh_add_cosh : sinh x + cosh x = exp x :=
by rw ← of_real_inj; simp [sinh_add_cosh]
lemma cosh_sq_sub_sinh_sq (x : ℝ) : cosh x ^ 2 - sinh x ^ 2 = 1 :=
by rw ← of_real_inj; simp [cosh_sq_sub_sinh_sq]
lemma cosh_sq : cosh x ^ 2 = sinh x ^ 2 + 1 :=
by rw ← of_real_inj; simp [cosh_sq]
lemma sinh_sq : sinh x ^ 2 = cosh x ^ 2 - 1 :=
by rw ← of_real_inj; simp [sinh_sq]
lemma cosh_two_mul : cosh (2 * x) = cosh x ^ 2 + sinh x ^ 2 :=
by rw ← of_real_inj; simp [cosh_two_mul]
lemma sinh_two_mul : sinh (2 * x) = 2 * sinh x * cosh x :=
by rw ← of_real_inj; simp [sinh_two_mul]
lemma cosh_three_mul : cosh (3 * x) = 4 * cosh x ^ 3 - 3 * cosh x :=
by rw ← of_real_inj; simp [cosh_three_mul]
lemma sinh_three_mul : sinh (3 * x) = 4 * sinh x ^ 3 + 3 * sinh x :=
by rw ← of_real_inj; simp [sinh_three_mul]
open is_absolute_value
/- TODO make this private and prove ∀ x -/
lemma add_one_le_exp_of_nonneg {x : ℝ} (hx : 0 ≤ x) : x + 1 ≤ exp x :=
calc x + 1 ≤ lim (⟨(λ n : ℕ, ((exp' x) n).re), is_cau_seq_re (exp' x)⟩ : cau_seq ℝ abs') :
le_lim (cau_seq.le_of_exists ⟨2,
λ j hj, show x + (1 : ℝ) ≤ (∑ m in range j, (x ^ m / m! : ℂ)).re,
from have h₁ : (((λ m : ℕ, (x ^ m / m! : ℂ)) ∘ nat.succ) 0).re = x, by simp,
have h₂ : ((x : ℂ) ^ 0 / 0!).re = 1, by simp,
begin
rw [← nat.sub_add_cancel hj, sum_range_succ', sum_range_succ',
add_re, add_re, h₁, h₂, add_assoc,
← coe_re_add_group_hom, (re_add_group_hom).map_sum, coe_re_add_group_hom ],
refine le_add_of_nonneg_of_le (sum_nonneg (λ m hm, _)) (le_refl _),
rw [← of_real_pow, ← of_real_nat_cast, ← of_real_div, of_real_re],
exact div_nonneg (pow_nonneg hx _) (nat.cast_nonneg _),
end⟩)
... = exp x : by rw [exp, complex.exp, ← cau_seq_re, lim_re]
lemma one_le_exp {x : ℝ} (hx : 0 ≤ x) : 1 ≤ exp x :=
by linarith [add_one_le_exp_of_nonneg hx]
lemma exp_pos (x : ℝ) : 0 < exp x :=
(le_total 0 x).elim (lt_of_lt_of_le zero_lt_one ∘ one_le_exp)
(λ h, by rw [← neg_neg x, real.exp_neg];
exact inv_pos.2 (lt_of_lt_of_le zero_lt_one (one_le_exp (neg_nonneg.2 h))))
@[simp] lemma abs_exp (x : ℝ) : abs' (exp x) = exp x :=
abs_of_pos (exp_pos _)
lemma exp_strict_mono : strict_mono exp :=
λ x y h, by rw [← sub_add_cancel y x, real.exp_add];
exact (lt_mul_iff_one_lt_left (exp_pos _)).2
(lt_of_lt_of_le (by linarith) (add_one_le_exp_of_nonneg (by linarith)))
@[mono] lemma exp_monotone : ∀ {x y : ℝ}, x ≤ y → exp x ≤ exp y := exp_strict_mono.monotone
@[simp] lemma exp_lt_exp {x y : ℝ} : exp x < exp y ↔ x < y := exp_strict_mono.lt_iff_lt
@[simp] lemma exp_le_exp {x y : ℝ} : exp x ≤ exp y ↔ x ≤ y := exp_strict_mono.le_iff_le
lemma exp_injective : function.injective exp := exp_strict_mono.injective
@[simp] lemma exp_eq_exp {x y : ℝ} : exp x = exp y ↔ x = y := exp_injective.eq_iff
@[simp] lemma exp_eq_one_iff : exp x = 1 ↔ x = 0 :=
by rw [← exp_zero, exp_injective.eq_iff]
@[simp] lemma one_lt_exp_iff {x : ℝ} : 1 < exp x ↔ 0 < x :=
by rw [← exp_zero, exp_lt_exp]
@[simp] lemma exp_lt_one_iff {x : ℝ} : exp x < 1 ↔ x < 0 :=
by rw [← exp_zero, exp_lt_exp]
@[simp] lemma exp_le_one_iff {x : ℝ} : exp x ≤ 1 ↔ x ≤ 0 :=
exp_zero ▸ exp_le_exp
@[simp] lemma one_le_exp_iff {x : ℝ} : 1 ≤ exp x ↔ 0 ≤ x :=
exp_zero ▸ exp_le_exp
/-- `real.cosh` is always positive -/
lemma cosh_pos (x : ℝ) : 0 < real.cosh x :=
(cosh_eq x).symm ▸ half_pos (add_pos (exp_pos x) (exp_pos (-x)))
end real
namespace complex
lemma sum_div_factorial_le {α : Type*} [linear_ordered_field α] (n j : ℕ) (hn : 0 < n) :
∑ m in filter (λ k, n ≤ k) (range j), (1 / m! : α) ≤ n.succ / (n! * n) :=
calc ∑ m in filter (λ k, n ≤ k) (range j), (1 / m! : α)
= ∑ m in range (j - n), 1 / (m + n)! :
sum_bij (λ m _, m - n)
(λ m hm, mem_range.2 $ (nat.sub_lt_sub_right_iff (by simp at hm; tauto)).2
(by simp at hm; tauto))
(λ m hm, by rw nat.sub_add_cancel; simp at *; tauto)
(λ a₁ a₂ ha₁ ha₂ h,
by rwa [nat.sub_eq_iff_eq_add, ← nat.sub_add_comm, eq_comm, nat.sub_eq_iff_eq_add,
add_left_inj, eq_comm] at h;
simp at *; tauto)
(λ b hb, ⟨b + n,
mem_filter.2 ⟨mem_range.2 $ nat.add_lt_of_lt_sub_right (mem_range.1 hb), nat.le_add_left _ _⟩,
by rw nat.add_sub_cancel⟩)
... ≤ ∑ m in range (j - n), (n! * n.succ ^ m)⁻¹ :
begin
refine sum_le_sum (assume m n, _),
rw [one_div, inv_le_inv],
{ rw [← nat.cast_pow, ← nat.cast_mul, nat.cast_le, add_comm],
exact nat.factorial_mul_pow_le_factorial },
{ exact nat.cast_pos.2 (nat.factorial_pos _) },
{ exact mul_pos (nat.cast_pos.2 (nat.factorial_pos _))
(pow_pos (nat.cast_pos.2 (nat.succ_pos _)) _) },
end
... = n!⁻¹ * ∑ m in range (j - n), n.succ⁻¹ ^ m :
by simp [mul_inv', mul_sum.symm, sum_mul.symm, -nat.factorial_succ, mul_comm, inv_pow']
... = (n.succ - n.succ * n.succ⁻¹ ^ (j - n)) / (n! * n) :
have h₁ : (n.succ : α) ≠ 1, from @nat.cast_one α _ _ ▸ mt nat.cast_inj.1
(mt nat.succ.inj (pos_iff_ne_zero.1 hn)),
have h₂ : (n.succ : α) ≠ 0, from nat.cast_ne_zero.2 (nat.succ_ne_zero _),
have h₃ : (n! * n : α) ≠ 0,
from mul_ne_zero (nat.cast_ne_zero.2 (pos_iff_ne_zero.1 (nat.factorial_pos _)))
(nat.cast_ne_zero.2 (pos_iff_ne_zero.1 hn)),
have h₄ : (n.succ - 1 : α) = n, by simp,
by rw [← geom_sum_def, geom_sum_inv h₁ h₂, eq_div_iff_mul_eq h₃,
mul_comm _ (n! * n : α), ← mul_assoc (n!⁻¹ : α), ← mul_inv_rev', h₄,
← mul_assoc (n! * n : α), mul_comm (n : α) n!, mul_inv_cancel h₃];
simp [mul_add, add_mul, mul_assoc, mul_comm]
... ≤ n.succ / (n! * n) :
begin
refine iff.mpr (div_le_div_right (mul_pos _ _)) _,
exact nat.cast_pos.2 (nat.factorial_pos _),
exact nat.cast_pos.2 hn,
exact sub_le_self _
(mul_nonneg (nat.cast_nonneg _) (pow_nonneg (inv_nonneg.2 (nat.cast_nonneg _)) _))
end
lemma exp_bound {x : ℂ} (hx : abs x ≤ 1) {n : ℕ} (hn : 0 < n) :
abs (exp x - ∑ m in range n, x ^ m / m!) ≤ abs x ^ n * (n.succ * (n! * n)⁻¹) :=
begin
rw [← lim_const (∑ m in range n, _), exp, sub_eq_add_neg, ← lim_neg, lim_add, ← lim_abs],
refine lim_le (cau_seq.le_of_exists ⟨n, λ j hj, _⟩),
simp_rw ← sub_eq_add_neg,
show abs (∑ m in range j, x ^ m / m! - ∑ m in range n, x ^ m / m!)
≤ abs x ^ n * (n.succ * (n! * n)⁻¹),
rw sum_range_sub_sum_range hj,
exact calc abs (∑ m in (range j).filter (λ k, n ≤ k), (x ^ m / m! : ℂ))
= abs (∑ m in (range j).filter (λ k, n ≤ k), (x ^ n * (x ^ (m - n) / m!) : ℂ)) :
begin
refine congr_arg abs (sum_congr rfl (λ m hm, _)),
rw [mem_filter, mem_range] at hm,
rw [← mul_div_assoc, ← pow_add, nat.add_sub_cancel' hm.2]
end
... ≤ ∑ m in filter (λ k, n ≤ k) (range j), abs (x ^ n * (_ / m!)) : abv_sum_le_sum_abv _ _
... ≤ ∑ m in filter (λ k, n ≤ k) (range j), abs x ^ n * (1 / m!) :
begin
refine sum_le_sum (λ m hm, _),
rw [abs_mul, abv_pow abs, abs_div, abs_cast_nat],
refine mul_le_mul_of_nonneg_left ((div_le_div_right _).2 _) _,
exact nat.cast_pos.2 (nat.factorial_pos _),
rw abv_pow abs,
exact (pow_le_one _ (abs_nonneg _) hx),
exact pow_nonneg (abs_nonneg _) _
end
... = abs x ^ n * (∑ m in (range j).filter (λ k, n ≤ k), (1 / m! : ℝ)) :
by simp [abs_mul, abv_pow abs, abs_div, mul_sum.symm]
... ≤ abs x ^ n * (n.succ * (n! * n)⁻¹) :
mul_le_mul_of_nonneg_left (sum_div_factorial_le _ _ hn) (pow_nonneg (abs_nonneg _) _)
end
lemma abs_exp_sub_one_le {x : ℂ} (hx : abs x ≤ 1) :
abs (exp x - 1) ≤ 2 * abs x :=
calc abs (exp x - 1) = abs (exp x - ∑ m in range 1, x ^ m / m!) :
by simp [sum_range_succ]
... ≤ abs x ^ 1 * ((nat.succ 1) * (1! * (1 : ℕ))⁻¹) :
exp_bound hx dec_trivial
... = 2 * abs x : by simp [two_mul, mul_two, mul_add, mul_comm]
lemma abs_exp_sub_one_sub_id_le {x : ℂ} (hx : abs x ≤ 1) :
abs (exp x - 1 - x) ≤ (abs x)^2 :=
calc abs (exp x - 1 - x) = abs (exp x - ∑ m in range 2, x ^ m / m!) :
by simp [sub_eq_add_neg, sum_range_succ_comm, add_assoc]
... ≤ (abs x)^2 * (nat.succ 2 * (2! * (2 : ℕ))⁻¹) :
exp_bound hx dec_trivial
... ≤ (abs x)^2 * 1 :
mul_le_mul_of_nonneg_left (by norm_num) (sq_nonneg (abs x))
... = (abs x)^2 :
by rw [mul_one]
end complex
namespace real
open complex finset
lemma exp_bound {x : ℝ} (hx : abs' x ≤ 1) {n : ℕ} (hn : 0 < n) :
abs' (exp x - ∑ m in range n, x ^ m / m!) ≤ abs' x ^ n * (n.succ / (n! * n)) :=
begin
have hxc : complex.abs x ≤ 1, by exact_mod_cast hx,
convert exp_bound hxc hn; norm_cast
end
/-- A finite initial segment of the exponential series, followed by an arbitrary tail.
For fixed `n` this is just a linear map wrt `r`, and each map is a simple linear function
of the previous (see `exp_near_succ`), with `exp_near n x r ⟶ exp x` as `n ⟶ ∞`,
for any `r`. -/
def exp_near (n : ℕ) (x r : ℝ) : ℝ := ∑ m in range n, x ^ m / m! + x ^ n / n! * r
@[simp] theorem exp_near_zero (x r) : exp_near 0 x r = r := by simp [exp_near]
@[simp] theorem exp_near_succ (n x r) : exp_near (n + 1) x r = exp_near n x (1 + x / (n+1) * r) :=
by simp [exp_near, range_succ, mul_add, add_left_comm, add_assoc, pow_succ, div_eq_mul_inv,
mul_inv']; ac_refl
theorem exp_near_sub (n x r₁ r₂) : exp_near n x r₁ - exp_near n x r₂ = x ^ n / n! * (r₁ - r₂) :=
by simp [exp_near, mul_sub]
lemma exp_approx_end (n m : ℕ) (x : ℝ)
(e₁ : n + 1 = m) (h : abs' x ≤ 1) :
abs' (exp x - exp_near m x 0) ≤ abs' x ^ m / m! * ((m+1)/m) :=
by { simp [exp_near], convert exp_bound h _ using 1, field_simp [mul_comm], linarith }
lemma exp_approx_succ {n} {x a₁ b₁ : ℝ} (m : ℕ)
(e₁ : n + 1 = m) (a₂ b₂ : ℝ)
(e : abs' (1 + x / m * a₂ - a₁) ≤ b₁ - abs' x / m * b₂)
(h : abs' (exp x - exp_near m x a₂) ≤ abs' x ^ m / m! * b₂) :
abs' (exp x - exp_near n x a₁) ≤ abs' x ^ n / n! * b₁ :=
begin
refine (_root_.abs_sub_le _ _ _).trans ((add_le_add_right h _).trans _),
subst e₁, rw [exp_near_succ, exp_near_sub, _root_.abs_mul],
convert mul_le_mul_of_nonneg_left (le_sub_iff_add_le'.1 e) _,
{ simp [mul_add, pow_succ', div_eq_mul_inv, _root_.abs_mul, _root_.abs_inv, ← pow_abs, mul_inv'],
ac_refl },
{ simp [_root_.div_nonneg, _root_.abs_nonneg] }
end
lemma exp_approx_end' {n} {x a b : ℝ} (m : ℕ)
(e₁ : n + 1 = m) (rm : ℝ) (er : ↑m = rm) (h : abs' x ≤ 1)
(e : abs' (1 - a) ≤ b - abs' x / rm * ((rm+1)/rm)) :
abs' (exp x - exp_near n x a) ≤ abs' x ^ n / n! * b :=
by subst er; exact
exp_approx_succ _ e₁ _ _ (by simpa using e) (exp_approx_end _ _ _ e₁ h)
lemma exp_1_approx_succ_eq {n} {a₁ b₁ : ℝ} {m : ℕ}
(en : n + 1 = m) {rm : ℝ} (er : ↑m = rm)
(h : abs' (exp 1 - exp_near m 1 ((a₁ - 1) * rm)) ≤ abs' 1 ^ m / m! * (b₁ * rm)) :
abs' (exp 1 - exp_near n 1 a₁) ≤ abs' 1 ^ n / n! * b₁ :=
begin
subst er,
refine exp_approx_succ _ en _ _ _ h,
field_simp [show (m : ℝ) ≠ 0, by norm_cast; linarith],
end
lemma exp_approx_start (x a b : ℝ)
(h : abs' (exp x - exp_near 0 x a) ≤ abs' x ^ 0 / 0! * b) :
abs' (exp x - a) ≤ b :=
by simpa using h
lemma cos_bound {x : ℝ} (hx : abs' x ≤ 1) :
abs' (cos x - (1 - x ^ 2 / 2)) ≤ abs' x ^ 4 * (5 / 96) :=
calc abs' (cos x - (1 - x ^ 2 / 2)) = abs (complex.cos x - (1 - x ^ 2 / 2)) :
by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv]
... = abs ((complex.exp (x * I) + complex.exp (-x * I) - (2 - x ^ 2)) / 2) :
by simp [complex.cos, sub_div, add_div, neg_div, div_self (@two_ne_zero' ℂ _ _ _)]
... = abs (((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) +
((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!))) / 2) :
congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin
simp only [sum_range_succ],
simp [pow_succ],
apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring
end)
... ≤ abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) / 2) +
abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) / 2) :
by rw add_div; exact abs_add _ _
... = (abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) / 2 +
abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!)) / 2) :
by simp [complex.abs_div]
... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2 +
(complex.abs (-x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2) :
add_le_add ((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial))
((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial))
... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc]
lemma sin_bound {x : ℝ} (hx : abs' x ≤ 1) :
abs' (sin x - (x - x ^ 3 / 6)) ≤ abs' x ^ 4 * (5 / 96) :=
calc abs' (sin x - (x - x ^ 3 / 6)) = abs (complex.sin x - (x - x ^ 3 / 6)) :
by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv]
... = abs (((complex.exp (-x * I) - complex.exp (x * I)) * I - (2 * x - x ^ 3 / 3)) / 2) :
by simp [complex.sin, sub_div, add_div, neg_div, mul_div_cancel_left _ (@two_ne_zero' ℂ _ _ _),
div_div_eq_div_mul, show (3 : ℂ) * 2 = 6, by norm_num]
... = abs ((((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) -
(complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) * I) / 2) :
congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin
simp only [sum_range_succ],
simp [pow_succ],
apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring
end)
... ≤ abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) * I / 2) +
abs (-((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) * I) / 2) :
by rw [sub_mul, sub_eq_add_neg, add_div]; exact abs_add _ _
... = (abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) / 2 +
abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!)) / 2) :
by simp [add_comm, complex.abs_div, complex.abs_mul]
... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2 +
(complex.abs (-x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2) :
add_le_add ((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial))
((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial))
... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc]
lemma cos_pos_of_le_one {x : ℝ} (hx : abs' x ≤ 1) : 0 < cos x :=
calc 0 < (1 - x ^ 2 / 2) - abs' x ^ 4 * (5 / 96) :
sub_pos.2 $ lt_sub_iff_add_lt.2
(calc abs' x ^ 4 * (5 / 96) + x ^ 2 / 2
≤ 1 * (5 / 96) + 1 / 2 :
add_le_add
(mul_le_mul_of_nonneg_right (pow_le_one _ (abs_nonneg _) hx) (by norm_num))
((div_le_div_right (by norm_num)).2 (by rw [sq, ← abs_mul_self, _root_.abs_mul];
exact mul_le_one hx (abs_nonneg _) hx))
... < 1 : by norm_num)
... ≤ cos x : sub_le.1 (abs_sub_le_iff.1 (cos_bound hx)).2
lemma sin_pos_of_pos_of_le_one {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 1) : 0 < sin x :=
calc 0 < x - x ^ 3 / 6 - abs' x ^ 4 * (5 / 96) :
sub_pos.2 $ lt_sub_iff_add_lt.2
(calc abs' x ^ 4 * (5 / 96) + x ^ 3 / 6
≤ x * (5 / 96) + x / 6 :
add_le_add
(mul_le_mul_of_nonneg_right
(calc abs' x ^ 4 ≤ abs' x ^ 1 : pow_le_pow_of_le_one (abs_nonneg _)
(by rwa _root_.abs_of_nonneg (le_of_lt hx0))
dec_trivial
... = x : by simp [_root_.abs_of_nonneg (le_of_lt (hx0))]) (by norm_num))
((div_le_div_right (by norm_num)).2
(calc x ^ 3 ≤ x ^ 1 : pow_le_pow_of_le_one (le_of_lt hx0) hx dec_trivial
... = x : pow_one _))
... < x : by linarith)
... ≤ sin x : sub_le.1 (abs_sub_le_iff.1 (sin_bound
(by rwa [_root_.abs_of_nonneg (le_of_lt hx0)]))).2
lemma sin_pos_of_pos_of_le_two {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 2) : 0 < sin x :=
have x / 2 ≤ 1, from (div_le_iff (by norm_num)).mpr (by simpa),
calc 0 < 2 * sin (x / 2) * cos (x / 2) :
mul_pos (mul_pos (by norm_num) (sin_pos_of_pos_of_le_one (half_pos hx0) this))
(cos_pos_of_le_one (by rwa [_root_.abs_of_nonneg (le_of_lt (half_pos hx0))]))
... = sin x : by rw [← sin_two_mul, two_mul, add_halves]
lemma cos_one_le : cos 1 ≤ 2 / 3 :=
calc cos 1 ≤ abs' (1 : ℝ) ^ 4 * (5 / 96) + (1 - 1 ^ 2 / 2) :
sub_le_iff_le_add.1 (abs_sub_le_iff.1 (cos_bound (by simp))).1
... ≤ 2 / 3 : by norm_num
lemma cos_one_pos : 0 < cos 1 := cos_pos_of_le_one (le_of_eq abs_one)
lemma cos_two_neg : cos 2 < 0 :=
calc cos 2 = cos (2 * 1) : congr_arg cos (mul_one _).symm
... = _ : real.cos_two_mul 1
... ≤ 2 * (2 / 3) ^ 2 - 1 : sub_le_sub_right (mul_le_mul_of_nonneg_left
(by { rw [sq, sq], exact mul_self_le_mul_self (le_of_lt cos_one_pos) cos_one_le })
zero_le_two) _
... < 0 : by norm_num
end real
namespace complex
lemma abs_cos_add_sin_mul_I (x : ℝ) : abs (cos x + sin x * I) = 1 :=
have _ := real.sin_sq_add_cos_sq x,
by simp [add_comm, abs, norm_sq, sq, *, sin_of_real_re, cos_of_real_re, mul_re] at *
lemma abs_exp_eq_iff_re_eq {x y : ℂ} : abs (exp x) = abs (exp y) ↔ x.re = y.re :=
by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y,
abs_mul, abs_mul, abs_cos_add_sin_mul_I, abs_cos_add_sin_mul_I,
← of_real_exp, ← of_real_exp, abs_of_nonneg (le_of_lt (real.exp_pos _)),
abs_of_nonneg (le_of_lt (real.exp_pos _)), mul_one, mul_one];
exact ⟨λ h, real.exp_injective h, congr_arg _⟩
@[simp] lemma abs_exp_of_real (x : ℝ) : abs (exp x) = real.exp x :=
by rw [← of_real_exp]; exact abs_of_nonneg (le_of_lt (real.exp_pos _))
end complex
|
634cf2992b04c508362698955551886a04b3f989 | a721fe7446524f18ba361625fc01033d9c8b7a78 | /src/principia/mynat/fib.lean | 22fbf2a166838edf303d03dd4f1920bf4f16bdde | [] | no_license | Sterrs/leaning | 8fd80d1f0a6117a220bb2e57ece639b9a63deadc | 3901cc953694b33adda86cb88ca30ba99594db31 | refs/heads/master | 1,627,023,822,744 | 1,616,515,221,000 | 1,616,515,221,000 | 245,512,190 | 2 | 0 | null | 1,616,429,050,000 | 1,583,527,118,000 | Lean | UTF-8 | Lean | false | false | 5,682 | lean | -- vim: ts=2 sw=0 sts=-1 et ai tw=70
import .basic
import .dvd
import .induction
namespace hidden
open mynat
def fib: mynat → mynat
| 0 := 0
| 1 := 1
| (succ (succ n)) := fib n + fib (succ n)
variables {m n k p: mynat}
@[simp] theorem fib_zero: fib 0 = 0 := rfl
@[simp] theorem fib_one: fib 1 = 1 := rfl
@[simp]
theorem fib_succsucc:
fib (succ (succ n)) = fib n + fib (succ n) := rfl
theorem fib_k_formula (k: mynat):
fib (m + (k + 1)) = fib k * fib m + fib (k + 1) * fib (m + 1) :=
begin
-- this is here because I retroactively changed the theorem
-- statement and I'm lazy
rw ←add_assoc,
revert k,
apply duo_induction, {
simp,
}, {
simp,
rw ←one_eq_succ_zero,
rw fib_succsucc,
simp,
}, {
intro k,
assume h_ih1 h_ih2,
-- yucky algebra
have: (2: mynat) = 1 + 1 := rfl,
rw this,
repeat {rw ←add_assoc},
rw [add_one_succ, add_one_succ],
rw fib_succsucc,
rw ←add_one_succ,
rw ←add_assoc at h_ih2,
rw [h_ih1, h_ih2],
-- now we have to collect the terms in F_m and F_{m + 1}
conv {
to_lhs,
rw add_assoc,
rw add_comm,
congr, congr, skip,
rw add_comm,
},
rw ←add_assoc,
rw ←add_mul,
conv {
to_lhs,
congr, congr, congr, congr, skip,
rw add_one_succ,
},
rw ←fib_succsucc,
rw add_assoc,
rw ←add_mul,
conv {
to_lhs,
congr, skip, congr,
rw add_comm,
rw add_one_succ,
},
rw ←fib_succsucc,
rw add_comm,
refl,
},
end
-- this is a consequence of the big one we want to prove, which is
-- F_gcd(m, n) = gcd(F_m, F_n), which actually needs only fairly basic
-- properties of gcd - but it does require that you've defined gcd,
-- sadly.
theorem f_preserves_multiples
(k: mynat):
n ∣ m → fib n ∣ fib m :=
begin
assume hnm,
cases hnm with k hk,
cases n, {
rw hk,
simp,
}, {
rw [hk, mul_comm],
clear hk,
induction k with k_n k_ih, {
from dvd_zero,
}, {
rw [mul_succ, add_comm],
conv {
congr, skip, congr, congr, skip,
rw ←add_one_succ,
},
rw [fib_k_formula n, add_one_succ],
apply dvd_sum, {
rw mul_comm,
from dvd_mul _ k_ih,
}, {
rw mul_comm,
from dvd_multiple,
},
},
},
end
-- TODO: adjacent are coprime,
-- https://en.wikipedia.org/wiki/Fibonacci_number#Other_identities
-- the squares one from
-- https://en.wikipedia.org/wiki/Fibonacci_number#Combinatorial_identities
-- induction, just to see if I could, really.
-- the nice high-level way to prove this is using determinants
theorem cassini_odd:
fib (2 * n) * fib (2 * n + 2) + 1
= fib (2 * n + 1) * fib (2 * n + 1) :=
begin
have cancel2: ∀ a b c: mynat, a = b → c + a = c + b, {
intros, rw a_1,
},
induction n with n hn, {
refl,
}, {
repeat {rw mul_succ},
have h2k: ∀ k: mynat, 2 + k = succ (succ k), {
intro k,
rw add_comm,
refl,
},
repeat {rw h2k},
have hk2: ∀ k: mynat, k + 2 = succ (succ k), {
intro k, refl,
},
repeat {rw hk2},
repeat {rw add_one_succ},
repeat {rw fib_succsucc},
repeat {rw ←add_one_succ}, -- legibility
repeat {rw mul_add},
repeat {rw add_mul},
-- laboriously cancel terms. This is likely very inefficient
-- algebra, but it's hard for me to keep track of things
-- otherwise. Lots of conv, for similar reasons
conv {
to_lhs,
congr,
rw [←add_assoc, ←add_assoc, ←add_assoc, ←add_assoc, ←add_assoc,
←add_assoc],
rw add_comm,
},
repeat {rw add_assoc},
apply cancel2,
repeat {rw ←add_assoc},
rw add_comm (fib (2 * n) * fib (2 * n)),
repeat {rw add_assoc},
rw mul_comm,
apply cancel2,
repeat {rw ←add_assoc},
rw add_comm (fib (2 * n) * fib (2 * n)),
repeat {rw add_assoc},
conv {
to_rhs,
rw add_comm,
},
repeat {rw add_assoc},
apply cancel2,
apply cancel2,
repeat {rw ←add_assoc},
rw add_comm (fib (2 * n + 1) * fib (2 * n + 1)),
repeat {rw add_assoc},
apply cancel2,
apply cancel2,
conv {
to_rhs,
rw add_comm,
rw add_assoc,
},
apply cancel2,
conv {
congr,
rw add_comm,
rw add_assoc,
skip,
rw add_comm,
},
apply cancel2,
conv {
to_lhs,
rw add_comm,
rw ←add_assoc,
rw ←mul_add,
congr,
rw add_one_succ,
rw ←fib_succsucc,
},
from hn,
},
end
theorem cassini_even:
fib (2 * n + 1) * fib (2 * n + 3)
= fib (2 * n + 2) * fib (2 * n + 2) + 1 :=
begin
have cancel2: ∀ a b c: mynat, a = b → c + a = c + b, {
intros, rw a_1,
},
repeat {rw mul_succ},
have h2k: ∀ k: mynat, 2 + k = succ (succ k), {
intro k,
rw add_comm,
refl,
},
repeat {rw h2k},
have hk2: ∀ k: mynat, k + 2 = succ (succ k), {
intro k, refl,
},
repeat {rw hk2},
have hk3: ∀ k: mynat, k + 3 = succ (succ (succ k)), {
intro k, refl,
},
repeat {rw hk3},
repeat {rw add_one_succ},
repeat {rw fib_succsucc},
repeat {rw ←add_one_succ}, -- legibility
repeat {rw mul_add},
repeat {rw add_mul},
repeat {rw add_assoc},
conv {
congr,
rw add_comm,
skip,
rw add_comm,
},
repeat {rw add_assoc},
apply cancel2,
conv {
to_rhs,
rw add_comm,
},
repeat {rw add_assoc},
apply cancel2,
conv {
to_rhs,
rw add_comm,
congr,
rw ←mul_add,
rw add_one_succ,
rw ←fib_succsucc,
},
from cassini_odd.symm,
end
end hidden
|
0658044b4ec23ec1a1cb4a4cd927f9e7adf00044 | 6065973b1fa7bbacba932011c9e2f32bf7bdd6c1 | /src/computability/DFA.lean | 664c35fb40caedb0dbb889143bad68c46b97eb7d | [
"Apache-2.0"
] | permissive | khmacdonald/mathlib | 90a0fa2222369fa69ed2fbfb841b74d2bdfd66cb | 3669cb35c578441812ad30fd967d21a94b6f387e | refs/heads/master | 1,675,863,801,090 | 1,609,761,876,000 | 1,609,761,876,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,500 | lean | /-
Copyright (c) 2020 Fox Thomson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Fox Thomson
-/
import data.fintype.basic
import computability.language
/-!
# Deterministic Finite Automata
This file contains the definition of a Deterministic Finite Automaton (DFA), a state machine which
determines whether a string (implemented as a list over an arbitrary alphabet) is in a regular set
in linear time.
Note that this definition allows for Automaton with infinite states, a `fintype` instance must be
supplied for true DFA's.
-/
universes u v
/-- A DFA is a set of states (`σ`), a transition function from state to state labelled by the
alphabet (`step`), a starting state (`start`) and a set of acceptance states (`accept`). -/
structure DFA (α : Type u) (σ : Type v) :=
(step : σ → α → σ)
(start : σ)
(accept : set σ)
namespace DFA
variables {α : Type u} {σ σ₁ σ₂ σ₃ : Type v} (M : DFA α σ)
instance [inhabited σ] : inhabited (DFA α σ) :=
⟨DFA.mk (λ _ _, default σ) (default σ) ∅⟩
/-- `M.eval_from s x` evaluates `M` with input `x` starting from the state `s`. -/
def eval_from (start : σ) : list α → σ :=
list.foldl M.step start
/-- `M.eval x` evaluates `M` with input `x` starting from the state `M.start`. -/
def eval := M.eval_from M.start
/-- `M.accepts` is the language of `x` such that `M.eval x` is an accept state. -/
def accepts : language α :=
λ x, M.eval x ∈ M.accept
end DFA
|
4cfe49e3f86b1ac9d77568cc28b1af2a618902d4 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/def2.lean | 0ec294d7b8e0c25a8cadddb8cfb6de2d06357fb6 | [
"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 | 93 | lean |
definition plus (a b : nat) : nat :=
nat.rec_on a b (λ a' ih, nat.succ ih)
#eval plus 3 5
|
22b8651ed2405843d42aab94894125d025433fb4 | d436468d80b739ba7e06843c4d0d2070e43448e5 | /src/tactic/scc.lean | 93b0d400557679611c2a9821d8f5b3b9385818bb | [
"Apache-2.0"
] | permissive | roro47/mathlib | 761fdc002aef92f77818f3fef06bf6ec6fc1a28e | 80aa7d52537571a2ca62a3fdf71c9533a09422cf | refs/heads/master | 1,599,656,410,625 | 1,573,649,488,000 | 1,573,649,488,000 | 221,452,951 | 0 | 0 | Apache-2.0 | 1,573,647,693,000 | 1,573,647,692,000 | null | UTF-8 | Lean | false | false | 12,833 | lean | /-
Copyright (c) 2018 Simon Hudon All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
Tactics based on the strongly connected components (SCC) of a graph where
the vertices are propositions and the edges are implications found
in the context.
They are used for finding the sets of equivalent propositions in a set
of implications.
-/
import tactic.tauto
import category.basic data.sum
/-!
# Strongly Connected Components
This file defines tactics to construct proofs of equivalences between a set of mutually equivalent
propositions. The tactics use implications transitively to find sets of equivalent propositions.
## Implementation notes
The tactics use a strongly connected components algorithm on a graph where propositions are
vertices and edges are proofs that the source implies the target. The strongly connected components
are therefore sets of propositions that are pairwise equivalent to each other.
The resulting strongly connected components are encoded in a disjoint set data structure to
facilitate the construction of equivalence proofs between two arbitrary members of an equivalence
class.
## Possible generalizations
Instead of reasoning about implications and equivalence, we could generalize the machinery to
reason about arbitrary partial orders.
## References
* Tarjan, R. E. (1972), "Depth-first search and linear graph algorithms",
SIAM Journal on Computing, 1 (2): 146–160, doi:10.1137/0201010
* Dijkstra, Edsger (1976), A Discipline of Programming, NJ: Prentice Hall, Ch. 25.
* https://en.wikipedia.org/wiki/Disjoint-set_data_structure
## Tags
graphs, tactic, strongly connected components, disjoint sets
-/
namespace tactic
/--
`closure` implements a disjoint set data structure using path compression
optimization. For the sake of the scc algorithm, it also stores the preorder
numbering of the equivalence graph of the local assumptions.
The `expr_map` encodes a directed forest by storing for every non-root
node, a reference to its parent and a proof of equivalence between
that node's expression and its parent's expression. Given that data
structure, checking that two nodes belong to the same tree is easy and
fast by repeatedly following the parent references until a root is reached.
If both nodes have the same root, they belong to the same tree, i.e. their
expressions are equivalent. The proof of equivalence can be formed by
composing the proofs along the edges of the paths to the root.
More concretely, if we ignore preorder numbering, the set
`{ {e₀,e₁,e₂,e₃}, {e₄,e₅} }` is represented as:
```
e₀ → ⊥ -- no parent, i.e. e₀ is a root
e₁ → e₀, p₁ -- with p₁ : e₁ ↔ e₀
e₂ → e₁, p₂ -- with p₂ : e₂ ↔ e₁
e₃ → e₀, p₃ -- with p₃ : e₃ ↔ e₀
e₄ → ⊥ -- no parent, i.e. e₄ is a root
e₅ → e₄, p₅ -- with p₅ : e₅ ↔ e₄
```
We can check that `e₂` and `e₃` are equivalent by seeking the root of
the tree of each. The parent of `e₂` is `e₁`, the parent of `e₁` is
`e₀` and `e₀` does not have a parent, and thus, this is the root of its tree.
The parent of `e₃` is `e₀` and it's also the root, the same as for `e₂` and
they are therefore equivalent. We can build a proof of that equivalence by using
transitivity on `p₂`, `p₁` and `p₃.symm` in that order.
Similarly, we can discover that `e₂` and `e₅` aren't equivalent.
A description of the path compression optimization can be found at:
https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Path_compression
-/
meta def closure := ref (expr_map (ℕ ⊕ (expr × expr)))
namespace closure
/-- `with_new_closure f` creates an empty `closure` `c`, executes `f` on `c`, and then deletes `c`,
returning the output of `f`. -/
meta def with_new_closure {α} : (closure → tactic α) → tactic α :=
using_new_ref (expr_map.mk _)
/-- `to_tactic_format cl` pretty-prints the `closure` `cl` as a list. Assuming `cl` was built by
`dfs_at`, each element corresponds to a node `pᵢ : expr` and is one of the folllowing:
- if `pᵢ` is a root: `"pᵢ ⇐ i"`, where `i` is the preorder number of `pᵢ`,
- otherwise: `"(pᵢ, pⱼ) : P"`, where `P` is `pᵢ ↔ pⱼ`.
Useful for debugging. -/
meta def to_tactic_format (cl : closure) : tactic format :=
do m ← read_ref cl,
let l := m.to_list,
fmt ← l.mmap $ λ ⟨x,y⟩, match y with
| sum.inl y := pformat!"{x} ⇐ {y}"
| sum.inr ⟨y,p⟩ := pformat!"({x}, {y}) : {infer_type p}"
end,
pure $ to_fmt fmt
meta instance : has_to_tactic_format closure := ⟨ to_tactic_format ⟩
/-- `(n,r,p) ← root cl e` returns `r` the root of the tree that `e` is a part of (which might be
itself) along with `p` a proof of `e ↔ r` and `n`, the preorder numbering of the root. -/
meta def root (cl : closure) : expr → tactic (ℕ × expr × expr) | e :=
do m ← read_ref cl,
match m.find e with
| none :=
do p ← mk_app ``iff.refl [e],
pure (0,e,p)
| (some (sum.inl n)) :=
do p ← mk_app ``iff.refl [e],
pure (n,e,p)
| (some (sum.inr (e₀,p₀))) :=
do (n,e₁,p₁) ← root e₀,
p ← mk_app ``iff.trans [p₀,p₁],
modify_ref cl $ λ m, m.insert e (sum.inr (e₁,p)),
pure (n,e₁,p)
end
/-- (Implementation of `merge`.) -/
meta def merge_intl (cl : closure) (p e₀ p₀ e₁ p₁ : expr) : tactic unit :=
do p₂ ← mk_app ``iff.symm [p₀],
p ← mk_app ``iff.trans [p₂,p],
p ← mk_app ``iff.trans [p,p₁],
modify_ref cl $ λ m, m.insert e₀ $ sum.inr (e₁,p)
/-- `merge cl p`, with `p` a proof of `e₀ ↔ e₁` for some `e₀` and `e₁`,
merges the trees of `e₀` and `e₁` and keeps the root with the smallest preorder
number as the root. This ensures that, in the depth-first traversal of the graph,
when encountering an edge going into a vertex whose equivalence class includes
a vertex that originated the current search, that vertex will be the root of
the corresponding tree. -/
meta def merge (cl : closure) (p : expr) : tactic unit :=
do `(%%e₀ ↔ %%e₁) ← infer_type p >>= instantiate_mvars,
(n₂,e₂,p₂) ← root cl e₀,
(n₃,e₃,p₃) ← root cl e₁,
if e₂ ≠ e₃ then do
if n₂ < n₃ then do p ← mk_app ``iff.symm [p],
cl.merge_intl p e₃ p₃ e₂ p₂
else cl.merge_intl p e₂ p₂ e₃ p₃
else pure ()
/-- Sequentially assign numbers to the nodes of the graph as they are being visited. -/
meta def assign_preorder (cl : closure) (e : expr) : tactic unit :=
modify_ref cl $ λ m, m.insert e (sum.inl m.size)
/-- `prove_eqv cl e₀ e₁` constructs a proof of equivalence of `e₀` and `e₁` if
they are equivalent. -/
meta def prove_eqv (cl : closure) (e₀ e₁ : expr) : tactic expr :=
do (_,r,p₀) ← root cl e₀,
(_,r',p₁) ← root cl e₁,
guard (r = r') <|> fail!"{e₀} and {e₁} are not equivalent",
p₁ ← mk_app ``iff.symm [p₁],
mk_app ``iff.trans [p₀,p₁]
/-- `prove_impl cl e₀ e₁` constructs a proof of `e₀ -> e₁` if they are equivalent. -/
meta def prove_impl (cl : closure) (e₀ e₁ : expr) : tactic expr :=
cl.prove_eqv e₀ e₁ >>= iff_mp
/-- `is_eqv cl e₀ e₁` checks whether `e₀` and `e₁` are equivalent without building a proof. -/
meta def is_eqv (cl : closure) (e₀ e₁ : expr) : tactic bool :=
do (_,r,p₀) ← root cl e₀,
(_,r',p₁) ← root cl e₁,
return $ r = r'
end closure
/-- mutable graphs between local propositions that imply each other with the proof of implication -/
@[reducible]
meta def impl_graph := ref (expr_map (list $ expr × expr))
/-- `with_impl_graph f` creates an empty `impl_graph` `g`, executes `f` on `g`, and then deletes
`g`, returning the output of `f`. -/
meta def with_impl_graph {α} : (impl_graph → tactic α) → tactic α :=
using_new_ref (expr_map.mk (list $ expr × expr))
namespace impl_graph
/-- `add_edge g p`, with `p` a proof of `v₀ → v₁` or `v₀ ↔ v₁`, adds an edge to the implication
graph `g`. -/
meta def add_edge (g : impl_graph) : expr → tactic unit | p :=
do t ← infer_type p,
match t with
| `(%%v₀ → %%v₁) :=
do m ← read_ref g,
let xs := (m.find v₀).get_or_else [],
let xs' := (m.find v₁).get_or_else [],
modify_ref g $ λ m, (m.insert v₀ ((v₁,p) :: xs)).insert v₁ xs'
| `(%%v₀ ↔ %%v₁) :=
do p₀ ← mk_mapp ``iff.mp [none,none,p],
p₁ ← mk_mapp ``iff.mpr [none,none,p],
add_edge p₀, add_edge p₁
| _ := failed
end
section scc
open list
parameter g : expr_map (list $ expr × expr)
parameter visit : ref $ expr_map bool
parameter cl : closure
/-- `merge_path path e`, where `path` and `e` forms a cycle with proofs of implication between
consecutive vertices. The proofs are compiled into proofs of equivalences and added to the closure
structure. `e` and the first vertex of `path` do not have to be the same but they have to be
in the same equivalence class. -/
meta def merge_path (path : list (expr × expr)) (e : expr) : tactic unit :=
do p₁ ← cl.prove_impl e path.head.fst,
p₂ ← mk_mapp ``id [e],
let path := (e,p₁) :: path,
(_,ls) ← path.mmap_accuml (λ p p',
prod.mk <$> mk_mapp ``implies.trans [none,p'.1,none,p,p'.2] <*> pure p) p₂,
(_,rs) ← path.mmap_accumr (λ p p',
prod.mk <$> mk_mapp ``implies.trans [none,none,none,p.2,p'] <*> pure p') p₂,
ps ← mzip_with (λ p₀ p₁, mk_app ``iff.intro [p₀,p₁]) ls.tail rs.init,
ps.mmap' cl.merge
/-- (implementation of `collapse`) -/
meta def collapse' : list (expr × expr) → list (expr × expr) → expr → tactic unit
| acc [] v := merge_path acc v
| acc ((x,pr) :: xs) v :=
do b ← cl.is_eqv x v,
let acc' := (x,pr)::acc,
if b
then merge_path acc' v
else collapse' acc' xs v
/-- `collapse path v`, where `v` is a vertex that originated the current search
(or a vertex in the same equivalence class as the one that originated the current search).
It or its equivalent should be found in `path`. Since the vertices following `v` in the path
form a cycle with `v`, they can all be added to an equivalence class. -/
meta def collapse : list (expr × expr) → expr → tactic unit :=
collapse' []
/--
Strongly connected component algorithm inspired by Tarjan's and
Dijkstra's scc algorithm. Whereas they return strongly connected
components by enumerating them, this algorithm returns a disjoint set
data structure using path compression. This is a compact
representation that allows us, after the fact, to construct a proof of
equivalence between any two members of an equivalence class.
* Tarjan, R. E. (1972), "Depth-first search and linear graph algorithms",
SIAM Journal on Computing, 1 (2): 146–160, doi:10.1137/0201010
* Dijkstra, Edsger (1976), A Discipline of Programming, NJ: Prentice Hall, Ch. 25.
-/
meta def dfs_at :
list (expr × expr) → expr → tactic unit
| vs v :=
do m ← read_ref visit,
(_,v',_) ← cl.root v,
match m.find v' with
| (some tt) :=
pure ()
| (some ff) :=
collapse vs v
| none :=
do cl.assign_preorder v,
modify_ref visit $ λ m, m.insert v ff,
ns ← g.find v,
ns.mmap' $ λ ⟨w,e⟩, dfs_at ((v,e) :: vs) w,
modify_ref visit $ λ m, m.insert v tt,
pure ()
end
end scc
/-- Use the local assumptions to create a set of equivalence classes. -/
meta def mk_scc (cl : closure) : tactic (expr_map (list (expr × expr))) :=
with_impl_graph $ λ g,
using_new_ref (expr_map.mk bool) $ λ visit,
do ls ← local_context,
ls.mmap' $ λ l, try (g.add_edge l),
m ← read_ref g,
m.to_list.mmap $ λ ⟨v,_⟩, impl_graph.dfs_at m visit cl [] v,
pure m
end impl_graph
meta def prove_eqv_target (cl : closure) : tactic unit :=
do `(%%p ↔ %%q) ← target >>= whnf,
cl.prove_eqv p q >>= exact
/-- Use the available equivalences and implications to prove
a goal of the form `p ↔ q`. -/
meta def interactive.scc : tactic unit :=
closure.with_new_closure $ λ cl,
do impl_graph.mk_scc cl,
`(%%p ↔ %%q) ← target,
cl.prove_eqv p q >>= exact
/-- Collect all the available equivalences and implications and
add assumptions for every equivalence that can be proven using the
strongly connected components technique. Mostly useful for testing. -/
meta def interactive.scc' : tactic unit :=
closure.with_new_closure $ λ cl,
do m ← impl_graph.mk_scc cl,
let ls := m.to_list.map prod.fst,
let ls' := prod.mk <$> ls <*> ls,
ls'.mmap' $ λ x,
do { h ← get_unused_name `h,
try $ closure.prove_eqv cl x.1 x.2 >>= note h none }
end tactic
|
05f91dae99f3ae7910a3c4980c9d08b0a95fc8f9 | f3849be5d845a1cb97680f0bbbe03b85518312f0 | /library/init/meta/environment.lean | be88ffedbb22ceb5a40b3ff676d9ec14367709a6 | [
"Apache-2.0"
] | permissive | bjoeris/lean | 0ed95125d762b17bfcb54dad1f9721f953f92eeb | 4e496b78d5e73545fa4f9a807155113d8e6b0561 | refs/heads/master | 1,611,251,218,281 | 1,495,337,658,000 | 1,495,337,658,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,975 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.meta.declaration init.meta.exceptional init.data.option.basic
import init.meta.rb_map
meta constant environment : Type
namespace environment
/--
Information for a projection declaration
- `cname` is the name of the constructor associated with the projection.
- `nparams` is the number of constructor parameters
- `idx` is the parameter being projected by this projection
- `is_class` is tt iff this is a class projection.
-/
structure projection_info :=
(cname : name)
(nparams : nat)
(idx : nat)
(is_class : bool)
/- Create a standard environment using the given trust level -/
meta constant mk_std : nat → environment
/- Return the trust level of the given environment -/
meta constant trust_lvl : environment → nat
/- Add a new declaration to the environment -/
meta constant add : environment → declaration → exceptional environment
/- Retrieve a declaration from the environment -/
meta constant get : environment → name → exceptional declaration
meta def contains (env : environment) (d : name) : bool :=
match env.get d with
| exceptional.success _ := tt
| exceptional.exception ._ _ := ff
end
/- Return tt iff the given name is a namespace -/
meta constant is_namespace : environment → name → bool
/- Add a new inductive datatype to the environment
name, universe parameters, number of parameters, type, constructors (name and type), is_meta -/
meta constant add_inductive : environment → name → list name → nat → expr → list (name × expr) → bool →
exceptional environment
/- Return tt iff the given name is an inductive datatype -/
meta constant is_inductive : environment → name → bool
/- Return tt iff the given name is a constructor -/
meta constant is_constructor : environment → name → bool
/- Return tt iff the given name is a recursor -/
meta constant is_recursor : environment → name → bool
/- Return tt iff the given name is a recursive inductive datatype -/
meta constant is_recursive : environment → name → bool
/- Return the name of the inductive datatype of the given constructor. -/
meta constant inductive_type_of : environment → name → option name
/- Return the constructors of the inductive datatype with the given name -/
meta constant constructors_of : environment → name → list name
/- Return the recursor of the given inductive datatype -/
meta constant recursor_of : environment → name → option name
/- Return the number of parameters of the inductive datatype -/
meta constant inductive_num_params : environment → name → nat
/- Return the number of indices of the inductive datatype -/
meta constant inductive_num_indices : environment → name → nat
/- Return tt iff the inductive datatype recursor supports dependent elimination -/
meta constant inductive_dep_elim : environment → name → bool
/- Return tt iff the given name is a generalized inductive datatype -/
meta constant is_ginductive : environment → name → bool
meta constant is_projection : environment → name → option projection_info
/- Fold over declarations in the environment -/
meta constant fold {α :Type} : environment → α → (declaration → α → α) → α
/- (relation_info env n) returns some value if n is marked as a relation in the given environment.
the tuple contains: total number of arguments of the relation, lhs position and rhs position. -/
meta constant relation_info : environment → name → option (nat × nat × nat)
/- (refl_for env R) returns the name of the reflexivity theorem for the relation R -/
meta constant refl_for : environment → name → option name
/- (symm_for env R) returns the name of the symmetry theorem for the relation R -/
meta constant symm_for : environment → name → option name
/- (trans_for env R) returns the name of the transitivity theorem for the relation R -/
meta constant trans_for : environment → name → option name
/- (decl_olean env d) returns the name of the .olean file where d was defined.
The result is none if d was not defined in an imported file. -/
meta constant decl_olean : environment → name → option string
/- (decl_pos env d) returns the source location of d if available. -/
meta constant decl_pos : environment → name → option pos
/- Return the fields of the structure with the given name, or `none` if it is not a structure -/
meta constant structure_fields : environment → name → option (list name)
/- (get_class_attribute_symbols env attr_name) return symbols
occurring in instances of type classes tagged with the attribute `attr_name`.
Example: [algebra] -/
meta constant get_class_attribute_symbols : environment → name → name_set
open expr
meta constant unfold_untrusted_macros : environment → expr → expr
meta def is_constructor_app (env : environment) (e : expr) : bool :=
is_constant (get_app_fn e) && is_constructor env (const_name (get_app_fn e))
meta def is_refl_app (env : environment) (e : expr) : option (name × expr × expr) :=
match (refl_for env (const_name (get_app_fn e))) with
| (some n) :=
if get_app_num_args e ≥ 2
then some (n, app_arg (app_fn e), app_arg e)
else none
| none := none
end
/-- Return true if 'n' has been declared in the current file -/
meta def in_current_file (env : environment) (n : name) : bool :=
(env.decl_olean n).is_none && env.contains n
meta def is_definition (env : environment) (n : name) : bool :=
match env.get n with
| exceptional.success (declaration.defn _ _ _ _ _ _) := tt
| _ := ff
end
end environment
meta instance : has_to_string environment :=
⟨λ e, "[environment]"⟩
meta instance : inhabited environment :=
⟨environment.mk_std 0⟩
|
69ce38a16446419738513462b72893c2a4bf1306 | 9dd3f3912f7321eb58ee9aa8f21778ad6221f87c | /tests/lean/io_bug1.lean | 27c90dc7ffca549989f5a1cf7eab61d6f80ed3f9 | [
"Apache-2.0"
] | permissive | bre7k30/lean | de893411bcfa7b3c5572e61b9e1c52951b310aa4 | 5a924699d076dab1bd5af23a8f910b433e598d7a | refs/heads/master | 1,610,900,145,817 | 1,488,006,845,000 | 1,488,006,845,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 412 | lean | import system.io
def bar : io unit :=
do put_str "one", put_str "two", put_str "three"
vm_eval bar
print "---------"
def foo : ℕ → io unit
| 0 := put_str "at zero\n"
| (n+1) := do put_str "in\n", foo n, put_str "out\n"
vm_eval foo 3
print "---------"
def foo2 : ℕ → io unit
| 0 := put_str "at zero\n"
| (n+1) := do put_str "in\n", foo2 n, put_str "out\n", put_str "out2\n"
vm_eval foo2 3
|
61d5d32cd017a37039e0f4f6f5fdd0cdd33673b0 | 48eee836fdb5c613d9a20741c17db44c8e12e61c | /src/universal/basic.lean | 70a245cecbe899ba6951ed6ae57eb4861d099834 | [
"Apache-2.0"
] | permissive | fgdorais/lean-universal | 06430443a4abe51e303e602684c2977d1f5c0834 | 9259b0f7fb3aa83a9e0a7a3eaa44c262e42cc9b1 | refs/heads/master | 1,592,479,744,136 | 1,589,473,399,000 | 1,589,473,399,000 | 196,287,552 | 1 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 2,824 | lean | -- Copyright © 2019 François G. Dorais. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
import util
namespace universal
@[derive decidable_eq]
structure arity (τ : Type) := (cod : τ) (dom : list τ)
namespace arity
variables {τ : Type} (a b : arity τ)
protected theorem eq : ∀ {a b : arity τ}, a.cod = b.cod → a.dom = b.dom → a = b
| ⟨_,_⟩ ⟨_,_⟩ rfl rfl := rfl
theorem eq_iff : a.cod = b.cod ∧ a.dom = b.dom ↔ a = b :=
⟨λ ⟨hc, hd⟩, arity.eq hc hd, λ h, eq.subst h ⟨rfl, rfl⟩⟩
abbreviation index := index a.dom
abbreviation func (sort : τ → Type*) := (Π (i : a.index), sort i.val) → sort a.cod
end arity
definition signature (τ : Type) (σ : Type*) := σ → arity τ
universe u
variables {τ : Type} {σ : Type u} (sig : signature τ σ)
abbreviation signature.cod (f : σ) : τ := arity.cod (sig f)
abbreviation signature.dom (f : σ) : list τ := arity.dom (sig f)
abbreviation signature.index (f : σ) := arity.index (sig f)
inductive term (dom : list τ) : τ → Type u
| proj {} (i : index dom) : term i.val
| func (f : σ) : (Π (i : sig.index f), term i.val) → term (sig.cod f)
namespace term
variables {sig} {dom : list τ} {cod : τ} (t : term sig dom cod)
include t
abbreviation arity : arity τ := ⟨cod, dom⟩
abbreviation dom : list τ := dom
abbreviation cod : τ := cod
abbreviation index : Type := index dom
end term
structure algebra :=
(sort : τ → Type*)
(func (f : σ) : arity.func (sig f) sort)
definition term_algebra (dom : list τ) : algebra sig :=
{ sort := term sig dom
, func := term.func
}
definition const_algebra : algebra sig := term_algebra sig []
namespace algebra
variables {sig} (alg : algebra sig)
abbreviation valuation (dom : list τ) := Π (i : index dom), alg.sort i.val
definition eval {dom : list τ} : Π {cod : τ} (t : term sig dom cod), (Π (i : index dom), alg.sort i.val) → alg.sort cod
| _ (term.proj i) val := val i
| _ (term.func f ts) val := alg.func f (λ i, eval (ts i) val)
theorem eval_proj {dom : list τ} (i) (val : Π i : index dom, alg.sort i.val) : eval alg (term.proj i) val = val i := rfl
theorem eval_func {dom : list τ} (f) (ts) (val : Π i : index dom, alg.sort i.val) : eval alg (term.func f ts) val = alg.func f (λ i, eval alg (ts i) val) := rfl
end algebra
structure equation (dom : list τ) (cod : τ) := (lhs rhs : term sig dom cod)
namespace equation
variables {sig} {dom : list τ} {cod : τ}
abbreviation func (f) (es : Π (i : sig.index f), equation sig dom i.val) : equation sig dom (sig.cod f) :=
⟨term.func f (λ i, (es i).lhs), term.func f (λ i, (es i).rhs)⟩
end equation
definition equation_algebra (dom : list τ) : algebra sig :=
{ sort := equation sig dom
, func := equation.func
}
end universal
|
1a1274113c2b42c6e4e371057610140ce8b6cad0 | bb31430994044506fa42fd667e2d556327e18dfe | /src/combinatorics/quiver/basic.lean | 1dc41a020da102afbd6f3ef030ec89d93e549939 | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 4,390 | lean | /-
Copyright (c) 2021 David Wärn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: David Wärn, Scott Morrison
-/
import data.opposite
/-!
# Quivers
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This module defines quivers. A quiver on a type `V` of vertices assigns to every
pair `a b : V` of vertices a type `a ⟶ b` of arrows from `a` to `b`. This
is a very permissive notion of directed graph.
## Implementation notes
Currently `quiver` is defined with `arrow : V → V → Sort v`.
This is different from the category theory setup,
where we insist that morphisms live in some `Type`.
There's some balance here: it's nice to allow `Prop` to ensure there are no multiple arrows,
but it is also results in error-prone universe signatures when constraints require a `Type`.
-/
open opposite
-- We use the same universe order as in category theory.
-- See note [category_theory universes]
universes v v₁ v₂ u u₁ u₂
/--
A quiver `G` on a type `V` of vertices assigns to every pair `a b : V` of vertices
a type `a ⟶ b` of arrows from `a` to `b`.
For graphs with no repeated edges, one can use `quiver.{0} V`, which ensures
`a ⟶ b : Prop`. For multigraphs, one can use `quiver.{v+1} V`, which ensures
`a ⟶ b : Type v`.
Because `category` will later extend this class, we call the field `hom`.
Except when constructing instances, you should rarely see this, and use the `⟶` notation instead.
-/
class quiver (V : Type u) :=
(hom : V → V → Sort v)
infixr ` ⟶ `:10 := quiver.hom -- type as \h
/--
A morphism of quivers. As we will later have categorical functors extend this structure,
we call it a `prefunctor`.
-/
structure prefunctor (V : Type u₁) [quiver.{v₁} V] (W : Type u₂) [quiver.{v₂} W] :=
(obj [] : V → W)
(map : Π {X Y : V}, (X ⟶ Y) → (obj X ⟶ obj Y))
namespace prefunctor
@[ext]
lemma ext {V : Type u} [quiver.{v₁} V] {W : Type u₂} [quiver.{v₂} W]
{F G : prefunctor V W}
(h_obj : ∀ X, F.obj X = G.obj X)
(h_map : ∀ (X Y : V) (f : X ⟶ Y),
F.map f = eq.rec_on (h_obj Y).symm (eq.rec_on (h_obj X).symm (G.map f))) : F = G :=
begin
cases F with F_obj _, cases G with G_obj _,
obtain rfl : F_obj = G_obj, by { ext X, apply h_obj },
congr,
funext X Y f,
simpa using h_map X Y f,
end
/--
The identity morphism between quivers.
-/
@[simps]
def id (V : Type*) [quiver V] : prefunctor V V :=
{ obj := id,
map := λ X Y f, f, }
instance (V : Type*) [quiver V] : inhabited (prefunctor V V) := ⟨id V⟩
/--
Composition of morphisms between quivers.
-/
@[simps]
def comp {U : Type*} [quiver U] {V : Type*} [quiver V] {W : Type*} [quiver W]
(F : prefunctor U V) (G : prefunctor V W) : prefunctor U W :=
{ obj := λ X, G.obj (F.obj X),
map := λ X Y f, G.map (F.map f), }
@[simp] lemma comp_id {U : Type*} [quiver U] {V : Type*} [quiver V] (F : prefunctor U V) :
F.comp (id _) = F := by { cases F, refl, }
@[simp] lemma id_comp {U : Type*} [quiver U] {V : Type*} [quiver V] (F : prefunctor U V) :
(id _).comp F = F := by { cases F, refl, }
@[simp]
lemma comp_assoc
{U V W Z : Type*} [quiver U] [quiver V] [quiver W] [quiver Z]
(F : prefunctor U V) (G : prefunctor V W) (H : prefunctor W Z) :
(F.comp G).comp H = F.comp (G.comp H) := rfl
infix ` ⥤q `:50 := prefunctor
infix ` ⋙q `:50 := prefunctor.comp
notation `𝟭q` := id
end prefunctor
namespace quiver
/-- `Vᵒᵖ` reverses the direction of all arrows of `V`. -/
instance opposite {V} [quiver V] : quiver Vᵒᵖ :=
⟨λ a b, (unop b) ⟶ (unop a)⟩
/--
The opposite of an arrow in `V`.
-/
def hom.op {V} [quiver V] {X Y : V} (f : X ⟶ Y) : op Y ⟶ op X := f
/--
Given an arrow in `Vᵒᵖ`, we can take the "unopposite" back in `V`.
-/
def hom.unop {V} [quiver V] {X Y : Vᵒᵖ} (f : X ⟶ Y) : unop Y ⟶ unop X := f
attribute [irreducible] quiver.opposite
/-- A type synonym for a quiver with no arrows. -/
@[nolint has_nonempty_instance]
def empty (V) : Type u := V
instance empty_quiver (V : Type u) : quiver.{u} (empty V) := ⟨λ a b, pempty⟩
@[simp] lemma empty_arrow {V : Type u} (a b : empty V) : (a ⟶ b) = pempty := rfl
/-- A quiver is thin if it has no parallel arrows. -/
@[reducible] def is_thin (V : Type u) [quiver V] := ∀ (a b : V), subsingleton (a ⟶ b)
end quiver
|
ba18220bda9e62035a135a2f8213d05b7e05f733 | d65db56b17d72b62013ed1f438f74457ad25b140 | /src/algebra.lean | 8584c2030eb3574cf59fa34ad5a6116545f2ed0d | [] | no_license | swgillespie/proofs | cfa65722fdb4bb7d7910a0856d0cbea8e8b9a3f9 | 168ef7ef33d372ae0ed6ee4ca336137a52b89f8f | refs/heads/master | 1,585,700,692,028 | 1,540,162,775,000 | 1,540,162,775,000 | 152,695,805 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,175 | lean | universe u
variables (α β : Type u)
-- Equality is useful in proofs. If two things are equal, they can be substituted
-- for one another in proofs using eq.subst. Such as this proof:
example (a b : α) (p : α → Prop) (h₁ : a = b) (h₂ : p a) : p b :=
eq.subst h₁ h₂
-- ▸ is shorthand for eq.subst:
example (a b : α) (p : α → Prop) (h₁ : a = b) (h₂ : p a) : p b := h₁ ▸ h₂
-- We can start using algebraic theorems to prove useful theorems:
theorem foil (x y : ℕ) : (x + y) * (x + y) = x * x + y * x + x * y + y * y :=
have h1 : (x + y) * (x + y) = (x + y) * x + (x + y) * y,
from mul_add (x + y) x y,
have h2 : (x + y) * (x + y) = x * x + y * x + (x * y + y * y),
from (add_mul x y x) ▸ (add_mul x y y) ▸ h1,
h2.trans (add_assoc (x * x + y * x) (x * y) (y * y)).symm
-- We can do "calculative proofs" by chaining together basic principles:
section
variables (a b c d e : ℕ)
variable h1 : a = b
variable h2 : b = c + 1
variable h3 : c = d
variable h4 : e = 1 + d
theorem T : a = e :=
calc
a = b : h1 -- a == b by hypothesis 1
... = c + 1 : h2 -- b == c + 1 by hypothesis 2
... = d + 1 : congr_arg _ h3 -- c + 1 == d + 1 by the congruence of c and d, hypothesis 3
... = 1 + d : add_comm d (1 : ℕ) -- d + 1 == 1 + d by the communitivity of addition
... = e : eq.symm h4 -- 1 + d == e by hypothesis 4, qed
-- We can do the same proof above using rewrite tactics:
include h1 h2 h3 h4
theorem T': a = e :=
calc
a = b : by rw h1
... = c + 1 : by rw h2
... = d + 1 : by rw h3
... = 1 + d : by rw add_comm
... = e : by rw h4
-- Not explained, but here's a super simple proof of foil using tactics
theorem foil_fast (x y : ℕ) : (x + y) * (x + y) = x * x + y * x + x * y + y * y :=
by simp [mul_add, add_mul]
end
-- Existential quantifiers
-- Like universal quantifiers, existential quantifiers have an introduction rule
-- and an elimination rule. Like in regular math, it sufficies to prove an existential
-- to prove that a prop p holds for one element of some type:
section
open nat
example : ∃ x : ℕ, x > 0 :=
-- Obvious proof that there exists a natural number greater than zero:
-- one exists, is a natural number, and is greater than zero
have h : 1 > 0, from zero_lt_succ 0,
exists.intro 1 h
-- Equivalent syntax
example : ∃ x : ℕ, x > 0 :=
have h : 1 > 0, from zero_lt_succ 0,
⟨1, h⟩
variables (p q : α → Prop)
-- The existential elimination rule allows us to prove some prop q through an existential
-- by showing that q holds for all x
example (h : ∃ x, p x ∧ q x) : ∃ x, q x ∧ p x :=
exists.elim h
(
assume w, -- There exists some x such that p x ∧ q x, call it w
assume hw : p w ∧ q w,
show ∃ x, q x ∧ p x, from ⟨w, hw.right, hw.left⟩
)
-- Equivalent proof, using 'match' to deconstruct the existential into cases
example (h : ∃ x, p x ∧ q x) : ∃ x, q x ∧ p x :=
-- Not sure what the semantics of this are yet... I guess you can always
-- destructure existentials into a witness and a hypothesis that the property
-- holds for the witness
match h with ⟨(w : α), (hw : p w ∧ q w)⟩ :=
⟨w, hw.right, hw.left⟩
end
def is_even (p : ℕ) := ∃ x, p = 2 * x
theorem even_plus_even_is_even { a b : ℕ } (h₁ : is_even a) (h₂ : is_even b) : is_even (a + b) :=
match h₁, h₂ with
-- This is extremely concise, but the core reasoning in the rewrite tactic is:
-- 0) start with is_even w₁ + w₂
-- 1) w₁ = 2 * q for some q (2 * q + w₂), since w₁ is even (h1)
-- 2) w₂ = 2 * p for some p (2 * q + 2 * p), since w₂ is even (h2)
-- 3) (2 * q) + 2 * (p + q) (mul_add)
-- QED
⟨w₁, hw₁⟩, ⟨w₂, hw₂⟩ := ⟨w₁ + w₂, by rw [hw₁, hw₂, mul_add]⟩
end
-- A similar proof, done on my own...
def is_odd (p : ℕ) := ∃ x, p = 2 * x + 1
theorem even_plus_odd_is_odd { a b : ℕ } (h1 : is_even a) (h2 : is_odd b) : is_odd (a + b) :=
match h1, h2 with
⟨w1, hw1⟩, ⟨w2, hw2⟩ := ⟨w1 + w2, begin rw hw1, rw hw2, simp, rw mul_add end⟩
end
end |
abe6bb6e2f57cd53b80026c0803ae11eae555b14 | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /tests/lean/run/elab_failure.lean | 957dc8fcfde97e887da7c1a01ad296cb04f2a8dd | [
"Apache-2.0"
] | permissive | codyroux/lean | 7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3 | 0cca265db19f7296531e339192e9b9bae4a31f8b | refs/heads/master | 1,610,909,964,159 | 1,407,084,399,000 | 1,416,857,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 424 | lean | import data.nat.basic data.bool
open bool nat
reducible nat.rec_on
definition is_eq (a b : nat) : bool :=
nat.rec_on a
(λ b, nat.cases_on b tt (λb₁, ff))
(λ a₁ r₁ b, nat.cases_on b ff (λb₁, r₁ b₁))
b
example (a₁ : nat) (b : nat) : true :=
@nat.cases_on (λ (n : nat), true) b
true.intro
(λ (b₁ : _),
have aux : is_eq a₁ b₁ = is_eq (succ a₁) (succ b₁), from rfl,
true.intro)
|
0a5d3be71e3303b35549fdf2b78aa248316db59a | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/field_theory/subfield.lean | d704fc08fbd467e59b1a94e22a31a83200685350 | [
"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 | 27,719 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import algebra.algebra.basic
import algebra.order.field.inj_surj
/-!
# Subfields
Let `K` be a field. This file defines the "bundled" subfield type `subfield K`, a type
whose terms correspond to subfields of `K`. This is the preferred way to talk
about subfields in mathlib. Unbundled subfields (`s : set K` and `is_subfield s`)
are not in this file, and they will ultimately be deprecated.
We prove that subfields are a complete lattice, and that you can `map` (pushforward) and
`comap` (pull back) them along ring homomorphisms.
We define the `closure` construction from `set R` to `subfield R`, sending a subset of `R`
to the subfield it generates, and prove that it is a Galois insertion.
## Main definitions
Notation used here:
`(K : Type u) [field K] (L : Type u) [field L] (f g : K →+* L)`
`(A : subfield K) (B : subfield L) (s : set K)`
* `subfield R` : the type of subfields of a ring `R`.
* `instance : complete_lattice (subfield R)` : the complete lattice structure on the subfields.
* `subfield.closure` : subfield closure of a set, i.e., the smallest subfield that includes the set.
* `subfield.gi` : `closure : set M → subfield M` and coercion `coe : subfield M → set M`
form a `galois_insertion`.
* `comap f B : subfield K` : the preimage of a subfield `B` along the ring homomorphism `f`
* `map f A : subfield L` : the image of a subfield `A` along the ring homomorphism `f`.
* `prod A B : subfield (K × L)` : the product of subfields
* `f.field_range : subfield B` : the range of the ring homomorphism `f`.
* `eq_locus_field f g : subfield K` : given ring homomorphisms `f g : K →+* R`,
the subfield of `K` where `f x = g x`
## Implementation notes
A subfield is implemented as a subring which is is closed under `⁻¹`.
Lattice inclusion (e.g. `≤` and `⊓`) is used rather than set notation (`⊆` and `∩`), although
`∈` is defined as membership of a subfield's underlying set.
## Tags
subfield, subfields
-/
open_locale big_operators
universes u v w
variables {K : Type u} {L : Type v} {M : Type w} [field K] [field L] [field M]
/-- `subfield_class S K` states `S` is a type of subsets `s ⊆ K` closed under field operations. -/
class subfield_class (S : Type*) (K : out_param $ Type*) [field K] [set_like S K]
extends subring_class S K, inv_mem_class S K.
namespace subfield_class
variables (S : Type*) [set_like S K] [h : subfield_class S K]
include h
/-- A subfield contains `1`, products and inverses.
Be assured that we're not actually proving that subfields are subgroups:
`subgroup_class` is really an abbreviation of `subgroup_with_or_without_zero_class`.
-/
@[priority 100] -- See note [lower instance priority]
instance subfield_class.to_subgroup_class : subgroup_class S K := { .. h }
variables {S}
lemma coe_rat_mem (s : S) (x : ℚ) : (x : K) ∈ s :=
by simpa only [rat.cast_def] using div_mem (coe_int_mem s x.num) (coe_nat_mem s x.denom)
instance (s : S) : has_rat_cast s :=
⟨λ x, ⟨↑x, coe_rat_mem s x⟩⟩
@[simp] lemma coe_rat_cast (s : S) (x : ℚ) : ((x : s) : K) = x := rfl
lemma rat_smul_mem (s : S) (a : ℚ) (x : s) : (a • x : K) ∈ s :=
by simpa only [rat.smul_def] using mul_mem (coe_rat_mem s a) x.prop
instance (s : S) : has_smul ℚ s :=
⟨λ a x, ⟨a • x, rat_smul_mem s a x⟩⟩
@[simp] lemma coe_rat_smul (s : S) (a : ℚ) (x : s) : (↑(a • x) : K) = a • x := rfl
variables (S)
/-- A subfield inherits a field structure -/
@[priority 75] -- Prefer subclasses of `field` over subclasses of `subfield_class`.
instance to_field (s : S) : field s :=
subtype.coe_injective.field (coe : s → K)
rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
(λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) (λ _, rfl)
omit h
/-- A subfield of a `linear_ordered_field` is a `linear_ordered_field`. -/
@[priority 75] -- Prefer subclasses of `field` over subclasses of `subfield_class`.
instance to_linear_ordered_field {K} [linear_ordered_field K] [set_like S K]
[subfield_class S K] (s : S) :
linear_ordered_field s :=
subtype.coe_injective.linear_ordered_field coe
rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
(λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) (λ _, rfl) (λ _ _, rfl)
(λ _ _, rfl)
end subfield_class
set_option old_structure_cmd true
/-- `subfield R` is the type of subfields of `R`. A subfield of `R` is a subset `s` that is a
multiplicative submonoid and an additive subgroup. Note in particular that it shares the
same 0 and 1 as R. -/
structure subfield (K : Type u) [field K] extends subring K :=
(inv_mem' : ∀ x ∈ carrier, x⁻¹ ∈ carrier)
/-- Reinterpret a `subfield` as a `subring`. -/
add_decl_doc subfield.to_subring
namespace subfield
/-- The underlying `add_subgroup` of a subfield. -/
def to_add_subgroup (s : subfield K) : add_subgroup K :=
{ ..s.to_subring.to_add_subgroup }
/-- The underlying submonoid of a subfield. -/
def to_submonoid (s : subfield K) : submonoid K :=
{ ..s.to_subring.to_submonoid }
instance : set_like (subfield K) K :=
⟨subfield.carrier, λ p q h, by cases p; cases q; congr'⟩
instance : subfield_class (subfield K) K :=
{ add_mem := add_mem',
zero_mem := zero_mem',
neg_mem := neg_mem',
mul_mem := mul_mem',
one_mem := one_mem',
inv_mem := inv_mem' }
@[simp]
lemma mem_carrier {s : subfield K} {x : K} : x ∈ s.carrier ↔ x ∈ s := iff.rfl
@[simp]
lemma mem_mk {S : set K} {x : K} (h₁ h₂ h₃ h₄ h₅ h₆) :
x ∈ (⟨S, h₁, h₂, h₃, h₄, h₅, h₆⟩ : subfield K) ↔ x ∈ S := iff.rfl
@[simp] lemma coe_set_mk (S : set K) (h₁ h₂ h₃ h₄ h₅ h₆) :
((⟨S, h₁, h₂, h₃, h₄, h₅, h₆⟩ : subfield K) : set K) = S := rfl
@[simp]
lemma mk_le_mk {S S' : set K} (h₁ h₂ h₃ h₄ h₅ h₆ h₁' h₂' h₃' h₄' h₅' h₆') :
(⟨S, h₁, h₂, h₃, h₄, h₅, h₆⟩ : subfield K) ≤ (⟨S', h₁', h₂', h₃', h₄', h₅', h₆'⟩ : subfield K) ↔
S ⊆ S' :=
iff.rfl
/-- Two subfields are equal if they have the same elements. -/
@[ext] theorem ext {S T : subfield K} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h
/-- Copy of a subfield with a new `carrier` equal to the old one. Useful to fix definitional
equalities. -/
protected def copy (S : subfield K) (s : set K) (hs : s = ↑S) : subfield K :=
{ carrier := s,
inv_mem' := hs.symm ▸ S.inv_mem',
..S.to_subring.copy s hs }
@[simp] lemma coe_copy (S : subfield K) (s : set K) (hs : s = ↑S) :
(S.copy s hs : set K) = s := rfl
lemma copy_eq (S : subfield K) (s : set K) (hs : s = ↑S) : S.copy s hs = S :=
set_like.coe_injective hs
@[simp] lemma coe_to_subring (s : subfield K) : (s.to_subring : set K) = s :=
rfl
@[simp] lemma mem_to_subring (s : subfield K) (x : K) :
x ∈ s.to_subring ↔ x ∈ s := iff.rfl
end subfield
/-- A `subring` containing inverses is a `subfield`. -/
def subring.to_subfield (s : subring K) (hinv : ∀ x ∈ s, x⁻¹ ∈ s) : subfield K :=
{ inv_mem' := hinv
..s }
namespace subfield
variables (s t : subfield K)
section derived_from_subfield_class
/-- A subfield contains the field's 1. -/
protected theorem one_mem : (1 : K) ∈ s := one_mem s
/-- A subfield contains the field's 0. -/
protected theorem zero_mem : (0 : K) ∈ s := zero_mem s
/-- A subfield is closed under multiplication. -/
protected theorem mul_mem {x y : K} : x ∈ s → y ∈ s → x * y ∈ s := mul_mem
/-- A subfield is closed under addition. -/
protected theorem add_mem {x y : K} : x ∈ s → y ∈ s → x + y ∈ s := add_mem
/-- A subfield is closed under negation. -/
protected theorem neg_mem {x : K} : x ∈ s → -x ∈ s := neg_mem
/-- A subfield is closed under subtraction. -/
protected theorem sub_mem {x y : K} : x ∈ s → y ∈ s → x - y ∈ s := sub_mem
/-- A subfield is closed under inverses. -/
protected theorem inv_mem {x : K} : x ∈ s → x⁻¹ ∈ s := inv_mem
/-- A subfield is closed under division. -/
protected theorem div_mem {x y : K} : x ∈ s → y ∈ s → x / y ∈ s := div_mem
/-- Product of a list of elements in a subfield is in the subfield. -/
protected lemma list_prod_mem {l : list K} : (∀ x ∈ l, x ∈ s) → l.prod ∈ s := list_prod_mem
/-- Sum of a list of elements in a subfield is in the subfield. -/
protected lemma list_sum_mem {l : list K} : (∀ x ∈ l, x ∈ s) → l.sum ∈ s := list_sum_mem
/-- Product of a multiset of elements in a subfield is in the subfield. -/
protected lemma multiset_prod_mem (m : multiset K) : (∀ a ∈ m, a ∈ s) → m.prod ∈ s :=
multiset_prod_mem m
/-- Sum of a multiset of elements in a `subfield` is in the `subfield`. -/
protected lemma multiset_sum_mem (m : multiset K) : (∀ a ∈ m, a ∈ s) → m.sum ∈ s :=
multiset_sum_mem m
/-- Product of elements of a subfield indexed by a `finset` is in the subfield. -/
protected lemma prod_mem {ι : Type*} {t : finset ι} {f : ι → K} (h : ∀ c ∈ t, f c ∈ s) :
∏ i in t, f i ∈ s :=
prod_mem h
/-- Sum of elements in a `subfield` indexed by a `finset` is in the `subfield`. -/
protected lemma sum_mem {ι : Type*} {t : finset ι} {f : ι → K} (h : ∀ c ∈ t, f c ∈ s) :
∑ i in t, f i ∈ s :=
sum_mem h
protected lemma pow_mem {x : K} (hx : x ∈ s) (n : ℕ) : x^n ∈ s := pow_mem hx n
protected lemma zsmul_mem {x : K} (hx : x ∈ s) (n : ℤ) : n • x ∈ s := zsmul_mem hx n
protected lemma coe_int_mem (n : ℤ) : (n : K) ∈ s := coe_int_mem s n
lemma zpow_mem {x : K} (hx : x ∈ s) (n : ℤ) : x^n ∈ s :=
begin
cases n,
{ simpa using s.pow_mem hx n },
{ simpa [pow_succ] using s.inv_mem (s.mul_mem hx (s.pow_mem hx n)) },
end
instance : ring s := s.to_subring.to_ring
instance : has_div s := ⟨λ x y, ⟨x / y, s.div_mem x.2 y.2⟩⟩
instance : has_inv s := ⟨λ x, ⟨x⁻¹, s.inv_mem x.2⟩⟩
instance : has_pow s ℤ := ⟨λ x z, ⟨x ^ z, s.zpow_mem x.2 z⟩⟩
/-- A subfield inherits a field structure -/
instance to_field : field s :=
subtype.coe_injective.field (coe : s → K)
rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
(λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) (λ _, rfl)
/-- A subfield of a `linear_ordered_field` is a `linear_ordered_field`. -/
instance to_linear_ordered_field {K} [linear_ordered_field K] (s : subfield K) :
linear_ordered_field s :=
subtype.coe_injective.linear_ordered_field coe
rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
(λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) (λ _, rfl) (λ _ _, rfl)
(λ _ _, rfl)
@[simp, norm_cast] lemma coe_add (x y : s) : (↑(x + y) : K) = ↑x + ↑y := rfl
@[simp, norm_cast] lemma coe_sub (x y : s) : (↑(x - y) : K) = ↑x - ↑y := rfl
@[simp, norm_cast] lemma coe_neg (x : s) : (↑(-x) : K) = -↑x := rfl
@[simp, norm_cast] lemma coe_mul (x y : s) : (↑(x * y) : K) = ↑x * ↑y := rfl
@[simp, norm_cast] lemma coe_div (x y : s) : (↑(x / y) : K) = ↑x / ↑y := rfl
@[simp, norm_cast] lemma coe_inv (x : s) : (↑(x⁻¹) : K) = (↑x)⁻¹ := rfl
@[simp, norm_cast] lemma coe_zero : ((0 : s) : K) = 0 := rfl
@[simp, norm_cast] lemma coe_one : ((1 : s) : K) = 1 := rfl
end derived_from_subfield_class
/-- The embedding from a subfield of the field `K` to `K`. -/
def subtype (s : subfield K) : s →+* K :=
{ to_fun := coe,
.. s.to_submonoid.subtype, .. s.to_add_subgroup.subtype }
instance to_algebra : algebra s K := ring_hom.to_algebra s.subtype
@[simp] theorem coe_subtype : ⇑s.subtype = coe := rfl
lemma to_subring.subtype_eq_subtype (F : Type*) [field F] (S : subfield F) :
S.to_subring.subtype = S.subtype := rfl
/-! # Partial order -/
variables (s t)
@[simp] lemma mem_to_submonoid {s : subfield K} {x : K} : x ∈ s.to_submonoid ↔ x ∈ s := iff.rfl
@[simp] lemma coe_to_submonoid : (s.to_submonoid : set K) = s := rfl
@[simp] lemma mem_to_add_subgroup {s : subfield K} {x : K} :
x ∈ s.to_add_subgroup ↔ x ∈ s := iff.rfl
@[simp] lemma coe_to_add_subgroup : (s.to_add_subgroup : set K) = s := rfl
/-! # top -/
/-- The subfield of `K` containing all elements of `K`. -/
instance : has_top (subfield K) :=
⟨{ inv_mem' := λ x _, subring.mem_top x, .. (⊤ : subring K)}⟩
instance : inhabited (subfield K) := ⟨⊤⟩
@[simp] lemma mem_top (x : K) : x ∈ (⊤ : subfield K) := set.mem_univ x
@[simp] lemma coe_top : ((⊤ : subfield K) : set K) = set.univ := rfl
/-- The ring equiv between the top element of `subfield K` and `K`. -/
@[simps]
def top_equiv : (⊤ : subfield K) ≃+* K := subsemiring.top_equiv
/-! # comap -/
variables (f : K →+* L)
/-- The preimage of a subfield along a ring homomorphism is a subfield. -/
def comap (s : subfield L) : subfield K :=
{ inv_mem' := λ x hx, show f (x⁻¹) ∈ s, by { rw map_inv₀ f, exact s.inv_mem hx },
.. s.to_subring.comap f }
@[simp] lemma coe_comap (s : subfield L) : (s.comap f : set K) = f ⁻¹' s := rfl
@[simp]
lemma mem_comap {s : subfield L} {f : K →+* L} {x : K} : x ∈ s.comap f ↔ f x ∈ s := iff.rfl
lemma comap_comap (s : subfield M) (g : L →+* M) (f : K →+* L) :
(s.comap g).comap f = s.comap (g.comp f) :=
rfl
/-! # map -/
/-- The image of a subfield along a ring homomorphism is a subfield. -/
def map (s : subfield K) : subfield L :=
{ inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, s.inv_mem hx, map_inv₀ f x⟩ },
.. s.to_subring.map f }
@[simp] lemma coe_map : (s.map f : set L) = f '' s := rfl
@[simp] lemma mem_map {f : K →+* L} {s : subfield K} {y : L} :
y ∈ s.map f ↔ ∃ x ∈ s, f x = y :=
set.mem_image_iff_bex
lemma map_map (g : L →+* M) (f : K →+* L) : (s.map f).map g = s.map (g.comp f) :=
set_like.ext' $ set.image_image _ _ _
lemma map_le_iff_le_comap {f : K →+* L} {s : subfield K} {t : subfield L} :
s.map f ≤ t ↔ s ≤ t.comap f :=
set.image_subset_iff
lemma gc_map_comap (f : K →+* L) : galois_connection (map f) (comap f) :=
λ S T, map_le_iff_le_comap
end subfield
namespace ring_hom
variables (g : L →+* M) (f : K →+* L)
/-! # range -/
/-- The range of a ring homomorphism, as a subfield of the target. See Note [range copy pattern]. -/
def field_range : subfield L :=
((⊤ : subfield K).map f).copy (set.range f) set.image_univ.symm
@[simp] lemma coe_field_range : (f.field_range : set L) = set.range f := rfl
@[simp] lemma mem_field_range {f : K →+* L} {y : L} : y ∈ f.field_range ↔ ∃ x, f x = y := iff.rfl
lemma field_range_eq_map : f.field_range = subfield.map f ⊤ :=
by { ext, simp }
lemma map_field_range : f.field_range.map g = (g.comp f).field_range :=
by simpa only [field_range_eq_map] using (⊤ : subfield K).map_map g f
/-- The range of a morphism of fields is a fintype, if the domain is a fintype.
Note that this instance can cause a diamond with `subtype.fintype` if `L` is also a fintype.-/
instance fintype_field_range [fintype K] [decidable_eq L] (f : K →+* L) : fintype f.field_range :=
set.fintype_range f
end ring_hom
namespace subfield
/-! # inf -/
/-- The inf of two subfields is their intersection. -/
instance : has_inf (subfield K) :=
⟨λ s t,
{ inv_mem' := λ x hx, subring.mem_inf.mpr
⟨s.inv_mem (subring.mem_inf.mp hx).1,
t.inv_mem (subring.mem_inf.mp hx).2⟩,
.. s.to_subring ⊓ t.to_subring }⟩
@[simp] lemma coe_inf (p p' : subfield K) : ((p ⊓ p' : subfield K) : set K) = p ∩ p' := rfl
@[simp] lemma mem_inf {p p' : subfield K} {x : K} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl
instance : has_Inf (subfield K) :=
⟨λ S,
{ inv_mem' := begin
rintros x hx,
apply subring.mem_Inf.mpr,
rintro _ ⟨p, p_mem, rfl⟩,
exact p.inv_mem (subring.mem_Inf.mp hx p.to_subring ⟨p, p_mem, rfl⟩),
end,
.. Inf (subfield.to_subring '' S) }⟩
@[simp, norm_cast] lemma coe_Inf (S : set (subfield K)) :
((Inf S : subfield K) : set K) = ⋂ s ∈ S, ↑s :=
show ((Inf (subfield.to_subring '' S) : subring K) : set K) = ⋂ s ∈ S, ↑s,
begin
ext x,
rw [subring.coe_Inf, set.mem_Inter, set.mem_Inter],
exact ⟨λ h s s' ⟨s_mem, s'_eq⟩, h s.to_subring _ ⟨⟨s, s_mem, rfl⟩, s'_eq⟩,
λ h s s' ⟨⟨s'', s''_mem, s_eq⟩, (s'_eq : ↑s = s')⟩,
h s'' _ ⟨s''_mem, by simp [←s_eq, ← s'_eq]⟩⟩
end
lemma mem_Inf {S : set (subfield K)} {x : K} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p :=
subring.mem_Inf.trans
⟨λ h p hp, h p.to_subring ⟨p, hp, rfl⟩,
λ h p ⟨p', hp', p_eq⟩, p_eq ▸ h p' hp'⟩
@[simp] lemma Inf_to_subring (s : set (subfield K)) :
(Inf s).to_subring = ⨅ t ∈ s, subfield.to_subring t :=
begin
ext x,
rw [mem_to_subring, mem_Inf],
erw subring.mem_Inf,
exact ⟨λ h p ⟨p', hp⟩, hp ▸ subring.mem_Inf.mpr (λ p ⟨hp', hp⟩, hp ▸ h _ hp'),
λ h p hp, h p.to_subring ⟨p, subring.ext (λ x,
⟨λ hx, subring.mem_Inf.mp hx _ ⟨hp, rfl⟩,
λ hx, subring.mem_Inf.mpr (λ p' ⟨hp, p'_eq⟩, p'_eq ▸ hx)⟩)⟩⟩
end
lemma is_glb_Inf (S : set (subfield K)) : is_glb S (Inf S) :=
begin
refine is_glb.of_image (λ s t, show (s : set K) ≤ t ↔ s ≤ t, from set_like.coe_subset_coe) _,
convert is_glb_binfi,
exact coe_Inf _
end
/-- Subfields of a ring form a complete lattice. -/
instance : complete_lattice (subfield K) :=
{ top := ⊤,
le_top := λ s x hx, trivial,
inf := (⊓),
inf_le_left := λ s t x, and.left,
inf_le_right := λ s t x, and.right,
le_inf := λ s t₁ t₂ h₁ h₂ x hx, ⟨h₁ hx, h₂ hx⟩,
.. complete_lattice_of_Inf (subfield K) is_glb_Inf }
/-! # subfield closure of a subset -/
/-- The `subfield` generated by a set. -/
def closure (s : set K) : subfield K :=
{ carrier := { (x / y) | (x ∈ subring.closure s) (y ∈ subring.closure s) },
zero_mem' := ⟨0, subring.zero_mem _, 1, subring.one_mem _, div_one _⟩,
one_mem' := ⟨1, subring.one_mem _, 1, subring.one_mem _, div_one _⟩,
neg_mem' := λ x ⟨y, hy, z, hz, x_eq⟩, ⟨-y, subring.neg_mem _ hy, z, hz, x_eq ▸ neg_div _ _⟩,
inv_mem' := λ x ⟨y, hy, z, hz, x_eq⟩, ⟨z, hz, y, hy, x_eq ▸ (inv_div _ _).symm⟩,
add_mem' := λ x y x_mem y_mem, begin
obtain ⟨nx, hnx, dx, hdx, rfl⟩ := id x_mem,
obtain ⟨ny, hny, dy, hdy, rfl⟩ := id y_mem,
by_cases hx0 : dx = 0, { rwa [hx0, div_zero, zero_add] },
by_cases hy0 : dy = 0, { rwa [hy0, div_zero, add_zero] },
exact ⟨nx * dy + dx * ny,
subring.add_mem _ (subring.mul_mem _ hnx hdy) (subring.mul_mem _ hdx hny),
dx * dy, subring.mul_mem _ hdx hdy,
(div_add_div nx ny hx0 hy0).symm⟩
end,
mul_mem' := λ x y x_mem y_mem, begin
obtain ⟨nx, hnx, dx, hdx, rfl⟩ := id x_mem,
obtain ⟨ny, hny, dy, hdy, rfl⟩ := id y_mem,
exact ⟨nx * ny, subring.mul_mem _ hnx hny,
dx * dy, subring.mul_mem _ hdx hdy,
(div_mul_div_comm _ _ _ _).symm⟩
end }
lemma mem_closure_iff {s : set K} {x} :
x ∈ closure s ↔ ∃ (y ∈ subring.closure s) (z ∈ subring.closure s), y / z = x := iff.rfl
lemma subring_closure_le (s : set K) : subring.closure s ≤ (closure s).to_subring :=
λ x hx, ⟨x, hx, 1, subring.one_mem _, div_one x⟩
/-- The subfield generated by a set includes the set. -/
@[simp] lemma subset_closure {s : set K} : s ⊆ closure s :=
set.subset.trans subring.subset_closure (subring_closure_le s)
lemma not_mem_of_not_mem_closure {s : set K} {P : K} (hP : P ∉ closure s) : P ∉ s :=
λ h, hP (subset_closure h)
lemma mem_closure {x : K} {s : set K} : x ∈ closure s ↔ ∀ S : subfield K, s ⊆ S → x ∈ S :=
⟨λ ⟨y, hy, z, hz, x_eq⟩ t le, x_eq ▸
t.div_mem
(subring.mem_closure.mp hy t.to_subring le)
(subring.mem_closure.mp hz t.to_subring le),
λ h, h (closure s) subset_closure⟩
/-- A subfield `t` includes `closure s` if and only if it includes `s`. -/
@[simp]
lemma closure_le {s : set K} {t : subfield K} : closure s ≤ t ↔ s ⊆ t :=
⟨set.subset.trans subset_closure, λ h x hx, mem_closure.mp hx t h⟩
/-- Subfield closure of a set is monotone in its argument: if `s ⊆ t`,
then `closure s ≤ closure t`. -/
lemma closure_mono ⦃s t : set K⦄ (h : s ⊆ t) : closure s ≤ closure t :=
closure_le.2 $ set.subset.trans h subset_closure
lemma closure_eq_of_le {s : set K} {t : subfield K} (h₁ : s ⊆ t) (h₂ : t ≤ closure s) :
closure s = t :=
le_antisymm (closure_le.2 h₁) h₂
/-- An induction principle for closure membership. If `p` holds for `1`, and all elements
of `s`, and is preserved under addition, negation, and multiplication, then `p` holds for all
elements of the closure of `s`. -/
@[elab_as_eliminator]
lemma closure_induction {s : set K} {p : K → Prop} {x} (h : x ∈ closure s)
(Hs : ∀ x ∈ s, p x) (H1 : p 1)
(Hadd : ∀ x y, p x → p y → p (x + y))
(Hneg : ∀ x, p x → p (-x))
(Hinv : ∀ x, p x → p (x⁻¹))
(Hmul : ∀ x y, p x → p y → p (x * y)) : p x :=
(@closure_le _ _ _ ⟨p, Hmul, H1,
Hadd, @add_neg_self K _ 1 ▸ Hadd _ _ H1 (Hneg _ H1), Hneg, Hinv⟩).2 Hs h
variable (K)
/-- `closure` forms a Galois insertion with the coercion to set. -/
protected def gi : galois_insertion (@closure K _) coe :=
{ choice := λ s _, closure s,
gc := λ s t, closure_le,
le_l_u := λ s, subset_closure,
choice_eq := λ s h, rfl }
variable {K}
/-- Closure of a subfield `S` equals `S`. -/
lemma closure_eq (s : subfield K) : closure (s : set K) = s := (subfield.gi K).l_u_eq s
@[simp] lemma closure_empty : closure (∅ : set K) = ⊥ := (subfield.gi K).gc.l_bot
@[simp] lemma closure_univ : closure (set.univ : set K) = ⊤ := @coe_top K _ ▸ closure_eq ⊤
lemma closure_union (s t : set K) : closure (s ∪ t) = closure s ⊔ closure t :=
(subfield.gi K).gc.l_sup
lemma closure_Union {ι} (s : ι → set K) : closure (⋃ i, s i) = ⨆ i, closure (s i) :=
(subfield.gi K).gc.l_supr
lemma closure_sUnion (s : set (set K)) : closure (⋃₀ s) = ⨆ t ∈ s, closure t :=
(subfield.gi K).gc.l_Sup
lemma map_sup (s t : subfield K) (f : K →+* L) : (s ⊔ t).map f = s.map f ⊔ t.map f :=
(gc_map_comap f).l_sup
lemma map_supr {ι : Sort*} (f : K →+* L) (s : ι → subfield K) :
(supr s).map f = ⨆ i, (s i).map f :=
(gc_map_comap f).l_supr
lemma comap_inf (s t : subfield L) (f : K →+* L) : (s ⊓ t).comap f = s.comap f ⊓ t.comap f :=
(gc_map_comap f).u_inf
lemma comap_infi {ι : Sort*} (f : K →+* L) (s : ι → subfield L) :
(infi s).comap f = ⨅ i, (s i).comap f :=
(gc_map_comap f).u_infi
@[simp] lemma map_bot (f : K →+* L) : (⊥ : subfield K).map f = ⊥ :=
(gc_map_comap f).l_bot
@[simp] lemma comap_top (f : K →+* L) : (⊤ : subfield L).comap f = ⊤ :=
(gc_map_comap f).u_top
/-- The underlying set of a non-empty directed Sup of subfields is just a union of the subfields.
Note that this fails without the directedness assumption (the union of two subfields is
typically not a subfield) -/
lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subfield K} (hS : directed (≤) S)
{x : K} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i :=
begin
refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr S i) hi⟩,
suffices : x ∈ closure (⋃ i, (S i : set K)) → ∃ i, x ∈ S i,
by simpa only [closure_Union, closure_eq],
refine λ hx, closure_induction hx (λ x, set.mem_Union.mp) _ _ _ _ _,
{ exact hι.elim (λ i, ⟨i, (S i).one_mem⟩) },
{ rintros x y ⟨i, hi⟩ ⟨j, hj⟩,
obtain ⟨k, hki, hkj⟩ := hS i j,
exact ⟨k, (S k).add_mem (hki hi) (hkj hj)⟩ },
{ rintros x ⟨i, hi⟩,
exact ⟨i, (S i).neg_mem hi⟩ },
{ rintros x ⟨i, hi⟩,
exact ⟨i, (S i).inv_mem hi⟩ },
{ rintros x y ⟨i, hi⟩ ⟨j, hj⟩,
obtain ⟨k, hki, hkj⟩ := hS i j,
exact ⟨k, (S k).mul_mem (hki hi) (hkj hj)⟩ }
end
lemma coe_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subfield K} (hS : directed (≤) S) :
((⨆ i, S i : subfield K) : set K) = ⋃ i, ↑(S i) :=
set.ext $ λ x, by simp [mem_supr_of_directed hS]
lemma mem_Sup_of_directed_on {S : set (subfield K)} (Sne : S.nonempty)
(hS : directed_on (≤) S) {x : K} :
x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s :=
begin
haveI : nonempty S := Sne.to_subtype,
simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk]
end
lemma coe_Sup_of_directed_on {S : set (subfield K)} (Sne : S.nonempty) (hS : directed_on (≤) S) :
(↑(Sup S) : set K) = ⋃ s ∈ S, ↑s :=
set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS]
end subfield
namespace ring_hom
variables {s : subfield K}
open subfield
/-- Restriction of a ring homomorphism to its range interpreted as a subfield. -/
def range_restrict_field (f : K →+* L) : K →+* f.field_range :=
f.srange_restrict
@[simp] lemma coe_range_restrict_field (f : K →+* L) (x : K) :
(f.range_restrict_field x : L) = f x := rfl
/-- The subfield of elements `x : R` such that `f x = g x`, i.e.,
the equalizer of f and g as a subfield of R -/
def eq_locus_field (f g : K →+* L) : subfield K :=
{ inv_mem' := λ x (hx : f x = g x), show f x⁻¹ = g x⁻¹, by rw [map_inv₀ f, map_inv₀ g, hx],
carrier := {x | f x = g x}, .. (f : K →+* L).eq_locus g }
/-- If two ring homomorphisms are equal on a set, then they are equal on its subfield closure. -/
lemma eq_on_field_closure {f g : K →+* L} {s : set K} (h : set.eq_on f g s) :
set.eq_on f g (closure s) :=
show closure s ≤ f.eq_locus_field g, from closure_le.2 h
lemma eq_of_eq_on_subfield_top {f g : K →+* L} (h : set.eq_on f g (⊤ : subfield K)) :
f = g :=
ext $ λ x, h trivial
lemma eq_of_eq_on_of_field_closure_eq_top {s : set K} (hs : closure s = ⊤) {f g : K →+* L}
(h : s.eq_on f g) : f = g :=
eq_of_eq_on_subfield_top $ hs ▸ eq_on_field_closure h
lemma field_closure_preimage_le (f : K →+* L) (s : set L) :
closure (f ⁻¹' s) ≤ (closure s).comap f :=
closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx
/-- The image under a ring homomorphism of the subfield generated by a set equals
the subfield generated by the image of the set. -/
lemma map_field_closure (f : K →+* L) (s : set K) :
(closure s).map f = closure (f '' s) :=
le_antisymm
(map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _)
(field_closure_preimage_le _ _))
(closure_le.2 $ set.image_subset _ subset_closure)
end ring_hom
namespace subfield
open ring_hom
/-- The ring homomorphism associated to an inclusion of subfields. -/
def inclusion {S T : subfield K} (h : S ≤ T) : S →+* T :=
S.subtype.cod_restrict _ (λ x, h x.2)
@[simp] lemma field_range_subtype (s : subfield K) : s.subtype.field_range = s :=
set_like.ext' $ (coe_srange _).trans subtype.range_coe
end subfield
namespace ring_equiv
variables {s t : subfield K}
/-- Makes the identity isomorphism from a proof two subfields of a multiplicative
monoid are equal. -/
def subfield_congr (h : s = t) : s ≃+* t :=
{ map_mul' := λ _ _, rfl, map_add' := λ _ _, rfl, ..equiv.set_congr $ set_like.ext'_iff.1 h }
end ring_equiv
namespace subfield
variables {s : set K}
lemma closure_preimage_le (f : K →+* L) (s : set L) :
closure (f ⁻¹' s) ≤ (closure s).comap f :=
closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx
end subfield
|
ef903113445c58d5c4c851c0101f7cf460f8f4b4 | f5f7e6fae601a5fe3cac7cc3ed353ed781d62419 | /src/order/order_iso.lean | a4d3e4442edb2eff44f87c723bbd6b1ee09e9ae1 | [
"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 | 12,996 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import order.basic logic.embedding data.nat.basic
open function
universes u v w
variables {α : Type*} {β : Type*} {γ : Type*}
{r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop}
structure order_embedding {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ↪ β :=
(ord : ∀ {a b}, r a b ↔ s (to_embedding a) (to_embedding b))
infix ` ≼o `:50 := order_embedding
/-- the induced order on a subtype is an embedding under the natural inclusion. -/
definition subtype.order_embedding {X : Type*} (r : X → X → Prop) (p : X → Prop) :
((subtype.val : subtype p → X) ⁻¹'o r) ≼o r :=
⟨⟨subtype.val,subtype.val_injective⟩,by intros;refl⟩
theorem preimage_equivalence {α β} (f : α → β) {s : β → β → Prop}
(hs : equivalence s) : equivalence (f ⁻¹'o s) :=
⟨λ a, hs.1 _, λ a b h, hs.2.1 h, λ a b c h₁ h₂, hs.2.2 h₁ h₂⟩
namespace order_embedding
instance : has_coe_to_fun (r ≼o s) := ⟨λ _, α → β, λ o, o.to_embedding⟩
theorem ord' : ∀ (f : r ≼o s) {a b}, r a b ↔ s (f a) (f b)
| ⟨f, o⟩ := @o
@[simp] theorem coe_fn_mk (f : α ↪ β) (o) :
(@order_embedding.mk _ _ r s f o : α → β) = f := rfl
@[simp] theorem coe_fn_to_embedding (f : r ≼o s) : (f.to_embedding : α → β) = f := rfl
theorem eq_of_to_fun_eq : ∀ {e₁ e₂ : r ≼o s}, (e₁ : α → β) = e₂ → e₁ = e₂
| ⟨⟨f₁, h₁⟩, o₁⟩ ⟨⟨f₂, h₂⟩, o₂⟩ h := by congr; exact h
@[refl] protected def refl (r : α → α → Prop) : r ≼o r :=
⟨embedding.refl _, λ a b, iff.rfl⟩
@[trans] protected def trans (f : r ≼o s) (g : s ≼o t) : r ≼o t :=
⟨f.1.trans g.1, λ a b, by rw [f.2, g.2]; simp⟩
@[simp] theorem refl_apply (x : α) : order_embedding.refl r x = x := rfl
@[simp] theorem trans_apply (f : r ≼o s) (g : s ≼o t) (a : α) : (f.trans g) a = g (f a) := rfl
/-- An order embedding is also an order embedding between dual orders. -/
def rsymm (f : r ≼o s) : swap r ≼o swap s :=
⟨f.to_embedding, λ a b, f.ord'⟩
/-- If `f` is injective, then it is an order embedding from the
preimage order of `s` to `s`. -/
def preimage (f : α ↪ β) (s : β → β → Prop) : f ⁻¹'o s ≼o s := ⟨f, λ a b, iff.rfl⟩
theorem eq_preimage (f : r ≼o s) : r = f ⁻¹'o s :=
by funext a b; exact propext f.ord'
protected theorem is_irrefl : ∀ (f : r ≼o s) [is_irrefl β s], is_irrefl α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a h, H _ (o.1 h)⟩
protected theorem is_refl : ∀ (f : r ≼o s) [is_refl β s], is_refl α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a, o.2 (H _)⟩
protected theorem is_symm : ∀ (f : r ≼o s) [is_symm β s], is_symm α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h, o.2 (H _ _ (o.1 h))⟩
protected theorem is_asymm : ∀ (f : r ≼o s) [is_asymm β s], is_asymm α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, H _ _ (o.1 h₁) (o.1 h₂)⟩
protected theorem is_antisymm : ∀ (f : r ≼o s) [is_antisymm β s], is_antisymm α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, f.inj' (H _ _ (o.1 h₁) (o.1 h₂))⟩
protected theorem is_trans : ∀ (f : r ≼o s) [is_trans β s], is_trans α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a b c h₁ h₂, o.2 (H _ _ _ (o.1 h₁) (o.1 h₂))⟩
protected theorem is_total : ∀ (f : r ≼o s) [is_total β s], is_total α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o o).2 (H _ _)⟩
protected theorem is_preorder : ∀ (f : r ≼o s) [is_preorder β s], is_preorder α r
| f H := by exactI {..f.is_refl, ..f.is_trans}
protected theorem is_partial_order : ∀ (f : r ≼o s) [is_partial_order β s], is_partial_order α r
| f H := by exactI {..f.is_preorder, ..f.is_antisymm}
protected theorem is_linear_order : ∀ (f : r ≼o s) [is_linear_order β s], is_linear_order α r
| f H := by exactI {..f.is_partial_order, ..f.is_total}
protected theorem is_strict_order : ∀ (f : r ≼o s) [is_strict_order β s], is_strict_order α r
| f H := by exactI {..f.is_irrefl, ..f.is_trans}
protected theorem is_trichotomous : ∀ (f : r ≼o s) [is_trichotomous β s], is_trichotomous α r
| ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o (or_congr f.inj'.eq_iff.symm o)).2 (H _ _)⟩
protected theorem is_strict_total_order' : ∀ (f : r ≼o s) [is_strict_total_order' β s], is_strict_total_order' α r
| f H := by exactI {..f.is_trichotomous, ..f.is_strict_order}
protected theorem acc (f : r ≼o s) (a : α) : acc s (f a) → acc r a :=
begin
generalize h : f a = b, intro ac,
induction ac with _ H IH generalizing a, subst h,
exact ⟨_, λ a' h, IH (f a') (f.ord'.1 h) _ rfl⟩
end
protected theorem well_founded : ∀ (f : r ≼o s) (h : well_founded s), well_founded r
| f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩
protected theorem is_well_order : ∀ (f : r ≼o s) [is_well_order β s], is_well_order α r
| f H := by exactI {wf := f.well_founded H.wf, ..f.is_strict_total_order'}
/-- It suffices to prove `f` is monotone between strict orders
to show it is an order embedding. -/
def of_monotone [is_trichotomous α r] [is_asymm β s] (f : α → β) (H : ∀ a b, r a b → s (f a) (f b)) : r ≼o s :=
begin
haveI := @is_irrefl_of_is_asymm β s _,
refine ⟨⟨f, λ a b e, _⟩, λ a b, ⟨H _ _, λ h, _⟩⟩,
{ refine ((@trichotomous _ r _ a b).resolve_left _).resolve_right _;
exact λ h, @irrefl _ s _ _ (by simpa [e] using H _ _ h) },
{ refine (@trichotomous _ r _ a b).resolve_right (or.rec (λ e, _) (λ h', _)),
{ subst e, exact irrefl _ h },
{ exact asymm (H _ _ h') h } }
end
@[simp] theorem of_monotone_coe [is_trichotomous α r] [is_asymm β s] (f : α → β) (H) :
(@of_monotone _ _ r s _ _ f H : α → β) = f := rfl
-- If le is preserved by an order embedding of preorders, then lt is too
def lt_embedding_of_le_embedding [preorder α] [preorder β]
(f : (has_le.le : α → α → Prop) ≼o (has_le.le : β → β → Prop)) :
(has_lt.lt : α → α → Prop) ≼o (has_lt.lt : β → β → Prop) :=
{ to_fun := f,
inj := f.inj,
ord := by intros; simp [lt_iff_le_not_le,f.ord] }
theorem nat_lt [is_strict_order α r] (f : ℕ → α) (H : ∀ n:ℕ, r (f n) (f (n+1))) :
((<) : ℕ → ℕ → Prop) ≼o r :=
of_monotone f $ λ a b h, begin
induction b with b IH, {exact (nat.not_lt_zero _ h).elim},
cases nat.lt_succ_iff_lt_or_eq.1 h with h e,
{ exact trans (IH h) (H _) },
{ subst b, apply H }
end
theorem nat_gt [is_strict_order α r] (f : ℕ → α) (H : ∀ n:ℕ, r (f (n+1)) (f n)) :
((>) : ℕ → ℕ → Prop) ≼o r :=
by haveI := is_strict_order.swap r; exact rsymm (nat_lt f H)
theorem well_founded_iff_no_descending_seq [is_strict_order α r] : well_founded r ↔ ¬ nonempty (((>) : ℕ → ℕ → Prop) ≼o r) :=
⟨λ ⟨h⟩ ⟨⟨f, o⟩⟩,
suffices ∀ a, acc r a → ∀ n, a ≠ f n, from this (f 0) (h _) 0 rfl,
λ a ac, begin
induction ac with a _ IH, intros n h, subst a,
exact IH (f (n+1)) (o.1 (nat.lt_succ_self _)) _ rfl
end,
λ N, ⟨λ a, classical.by_contradiction $ λ na,
let ⟨f, h⟩ := classical.axiom_of_choice $
show ∀ x : {a // ¬ acc r a}, ∃ y : {a // ¬ acc r a}, r y.1 x.1,
from λ ⟨x, h⟩, classical.by_contradiction $ λ hn, h $
⟨_, λ y h, classical.by_contradiction $ λ na, hn ⟨⟨y, na⟩, h⟩⟩ in
N ⟨nat_gt (λ n, (f^[n] ⟨a, na⟩).1) $ λ n,
by rw nat.iterate_succ'; apply h⟩⟩⟩
end order_embedding
/-- The inclusion map `fin n → ℕ` is an order embedding. -/
def fin.val.order_embedding (n) : @order_embedding (fin n) ℕ (<) (<) :=
⟨⟨fin.val, @fin.eq_of_veq _⟩, λ a b, iff.rfl⟩
/-- The inclusion map `fin m → fin n` is an order embedding. -/
def fin_fin.order_embedding {m n} (h : m ≤ n) : @order_embedding (fin m) (fin n) (<) (<) :=
⟨⟨λ ⟨x, h'⟩, ⟨x, lt_of_lt_of_le h' h⟩,
λ ⟨a, _⟩ ⟨b, _⟩ h, by congr; injection h⟩,
by intros; cases a; cases b; refl⟩
instance fin.lt.is_well_order (n) : is_well_order (fin n) (<) :=
(fin.val.order_embedding _).is_well_order
/-- An order isomorphism is an equivalence that is also an order embedding. -/
structure order_iso {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ≃ β :=
(ord : ∀ {a b}, r a b ↔ s (to_equiv a) (to_equiv b))
infix ` ≃o `:50 := order_iso
namespace order_iso
def to_order_embedding (f : r ≃o s) : r ≼o s :=
⟨f.to_equiv.to_embedding, f.ord⟩
instance : has_coe (r ≃o s) (r ≼o s) := ⟨to_order_embedding⟩
theorem coe_coe_fn (f : r ≃o s) : ((f : r ≼o s) : α → β) = f := rfl
theorem ord' : ∀ (f : r ≃o s) {a b}, r a b ↔ s (f a) (f b)
| ⟨f, o⟩ := @o
@[simp] theorem coe_fn_mk (f : α ≃ β) (o) :
(@order_iso.mk _ _ r s f o : α → β) = f := rfl
@[simp] theorem coe_fn_to_equiv (f : r ≃o s) : (f.to_equiv : α → β) = f := rfl
theorem eq_of_to_fun_eq : ∀ {e₁ e₂ : r ≃o s}, (e₁ : α → β) = e₂ → e₁ = e₂
| ⟨e₁, o₁⟩ ⟨e₂, o₂⟩ h := by congr; exact equiv.eq_of_to_fun_eq h
@[refl] protected def refl (r : α → α → Prop) : r ≃o r :=
⟨equiv.refl _, λ a b, iff.rfl⟩
@[symm] protected def symm (f : r ≃o s) : s ≃o r :=
⟨f.to_equiv.symm, λ a b, by cases f with f o; rw o; simp⟩
@[trans] protected def trans (f₁ : r ≃o s) (f₂ : s ≃o t) : r ≃o t :=
⟨f₁.to_equiv.trans f₂.to_equiv, λ a b,
by cases f₁ with f₁ o₁; cases f₂ with f₂ o₂; rw [o₁, o₂]; simp⟩
@[simp] theorem coe_fn_symm_mk (f o) : ((@order_iso.mk _ _ r s f o).symm : β → α) = f.symm :=
rfl
@[simp] theorem refl_apply (x : α) : order_iso.refl r x = x := rfl
@[simp] theorem trans_apply : ∀ (f : r ≃o s) (g : s ≃o t) (a : α), (f.trans g) a = g (f a)
| ⟨f₁, o₁⟩ ⟨f₂, o₂⟩ a := equiv.trans_apply _ _ _
@[simp] theorem apply_symm_apply : ∀ (e : r ≃o s) (x : β), e (e.symm x) = x
| ⟨f₁, o₁⟩ x := by simp
@[simp] theorem symm_apply_apply : ∀ (e : r ≃o s) (x : α), e.symm (e x) = x
| ⟨f₁, o₁⟩ x := by simp
/-- Any equivalence lifts to an order isomorphism between `s` and its preimage. -/
def preimage (f : α ≃ β) (s : β → β → Prop) : f ⁻¹'o s ≃o s := ⟨f, λ a b, iff.rfl⟩
noncomputable def of_surjective (f : r ≼o s) (H : surjective f) : r ≃o s :=
⟨equiv.of_bijective ⟨f.inj, H⟩, by simp [f.ord']⟩
@[simp] theorem of_surjective_coe (f : r ≼o s) (H) : (of_surjective f H : α → β) = f :=
by delta of_surjective; simp
theorem sum_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂}
(e₁ : @order_iso α₁ α₂ r₁ r₂) (e₂ : @order_iso β₁ β₂ s₁ s₂) :
sum.lex r₁ s₁ ≃o sum.lex r₂ s₂ :=
⟨equiv.sum_congr e₁.to_equiv e₂.to_equiv, λ a b,
by cases e₁ with f hf; cases e₂ with g hg;
cases a; cases b; simp [hf, hg]⟩
theorem prod_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂}
(e₁ : @order_iso α₁ α₂ r₁ r₂) (e₂ : @order_iso β₁ β₂ s₁ s₂) :
prod.lex r₁ s₁ ≃o prod.lex r₂ s₂ :=
⟨equiv.prod_congr e₁.to_equiv e₂.to_equiv, λ a b, begin
cases e₁ with f hf; cases e₂ with g hg,
cases a with a₁ a₂; cases b with b₁ b₂,
suffices : prod.lex r₁ s₁ (a₁, a₂) (b₁, b₂) ↔
prod.lex r₂ s₂ (f a₁, g a₂) (f b₁, g b₂), {simpa [hf, hg]},
split,
{ intro h, cases h with _ _ _ _ h _ _ _ h,
{ left, exact hf.1 h },
{ right, exact hg.1 h } },
{ generalize e : f b₁ = fb₁,
intro h, cases h with _ _ _ _ h _ _ _ h,
{ subst e, left, exact hf.2 h },
{ have := f.injective e, subst b₁,
right, exact hg.2 h } }
end⟩
end order_iso
/-- A subset `p : set α` embeds into `α` -/
def set_coe_embedding {α : Type*} (p : set α) : p ↪ α := ⟨subtype.val, @subtype.eq _ _⟩
/-- `subrel r p` is the inherited relation on a subset. -/
def subrel (r : α → α → Prop) (p : set α) : p → p → Prop :=
@subtype.val _ p ⁻¹'o r
@[simp] theorem subrel_val (r : α → α → Prop) (p : set α)
{a b} : subrel r p a b ↔ r a.1 b.1 := iff.rfl
namespace subrel
protected def order_embedding (r : α → α → Prop) (p : set α) :
subrel r p ≼o r := ⟨set_coe_embedding _, λ a b, iff.rfl⟩
@[simp] theorem order_embedding_apply (r : α → α → Prop) (p a) :
subrel.order_embedding r p a = a.1 := rfl
instance (r : α → α → Prop) [is_well_order α r]
(p : set α) : is_well_order p (subrel r p) :=
order_embedding.is_well_order (subrel.order_embedding r p)
end subrel
/-- Restrict the codomain of an order embedding -/
def order_embedding.cod_restrict (p : set β) (f : r ≼o s) (H : ∀ a, f a ∈ p) : r ≼o subrel s p :=
⟨f.to_embedding.cod_restrict p H, f.ord⟩
@[simp] theorem order_embedding.cod_restrict_apply (p) (f : r ≼o s) (H a) :
order_embedding.cod_restrict p f H a = ⟨f a, H a⟩ := rfl
|
a1880eec4420529ed650bd1871b4c090f99ac92f | 4fa161becb8ce7378a709f5992a594764699e268 | /src/category_theory/monoidal/category.lean | 83454e7fe408f05d2db9b8f0cb11d6fd749b42c4 | [
"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 | 17,802 | lean | /-
Copyright (c) 2018 Michael Jendrusch. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Michael Jendrusch, Scott Morrison
-/
import category_theory.products.basic
open category_theory
universes v u
open category_theory
open category_theory.category
open category_theory.iso
namespace category_theory
/--
In a monoidal category, we can take the tensor product of objects, `X ⊗ Y` and of morphisms `f ⊗ g`.
Tensor product does not need to be strictly associative on objects, but there is a
specified associator, `α_ X Y Z : (X ⊗ Y) ⊗ Z ≅ X ⊗ (Y ⊗ Z)`. There is a tensor unit `𝟙_ C`,
with specified left and right unitor isomorphisms `λ_ X : 𝟙_ C ⊗ X ≅ X` and `ρ_ X : X ⊗ 𝟙_ C ≅ X`.
These associators and unitors satisfy the pentagon and triangle equations.
-/
class monoidal_category (C : Type u) [𝒞 : category.{v} C] :=
-- curried tensor product of objects:
(tensor_obj : C → C → C)
(infixr ` ⊗ `:70 := tensor_obj) -- This notation is only temporary
-- curried tensor product of morphisms:
(tensor_hom :
Π {X₁ Y₁ X₂ Y₂ : C}, (X₁ ⟶ Y₁) → (X₂ ⟶ Y₂) → ((X₁ ⊗ X₂) ⟶ (Y₁ ⊗ Y₂)))
(infixr ` ⊗' `:69 := tensor_hom) -- This notation is only temporary
-- tensor product laws:
(tensor_id' :
∀ (X₁ X₂ : C), (𝟙 X₁) ⊗' (𝟙 X₂) = 𝟙 (X₁ ⊗ X₂) . obviously)
(tensor_comp' :
∀ {X₁ Y₁ Z₁ X₂ Y₂ Z₂ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (g₁ : Y₁ ⟶ Z₁) (g₂ : Y₂ ⟶ Z₂),
(f₁ ≫ g₁) ⊗' (f₂ ≫ g₂) = (f₁ ⊗' f₂) ≫ (g₁ ⊗' g₂) . obviously)
-- tensor unit:
(tensor_unit [] : C)
(notation `𝟙_` := tensor_unit)
-- associator:
(associator :
Π X Y Z : C, (X ⊗ Y) ⊗ Z ≅ X ⊗ (Y ⊗ Z))
(notation `α_` := associator)
(associator_naturality' :
∀ {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃),
((f₁ ⊗' f₂) ⊗' f₃) ≫ (α_ Y₁ Y₂ Y₃).hom = (α_ X₁ X₂ X₃).hom ≫ (f₁ ⊗' (f₂ ⊗' f₃)) . obviously)
-- left unitor:
(left_unitor : Π X : C, 𝟙_ ⊗ X ≅ X)
(notation `λ_` := left_unitor)
(left_unitor_naturality' :
∀ {X Y : C} (f : X ⟶ Y), ((𝟙 𝟙_) ⊗' f) ≫ (λ_ Y).hom = (λ_ X).hom ≫ f . obviously)
-- right unitor:
(right_unitor : Π X : C, X ⊗ 𝟙_ ≅ X)
(notation `ρ_` := right_unitor)
(right_unitor_naturality' :
∀ {X Y : C} (f : X ⟶ Y), (f ⊗' (𝟙 𝟙_)) ≫ (ρ_ Y).hom = (ρ_ X).hom ≫ f . obviously)
-- pentagon identity:
(pentagon' : ∀ W X Y Z : C,
((α_ W X Y).hom ⊗' (𝟙 Z)) ≫ (α_ W (X ⊗ Y) Z).hom ≫ ((𝟙 W) ⊗' (α_ X Y Z).hom)
= (α_ (W ⊗ X) Y Z).hom ≫ (α_ W X (Y ⊗ Z)).hom . obviously)
-- triangle identity:
(triangle' :
∀ X Y : C, (α_ X 𝟙_ Y).hom ≫ ((𝟙 X) ⊗' (λ_ Y).hom) = (ρ_ X).hom ⊗' (𝟙 Y) . obviously)
restate_axiom monoidal_category.tensor_id'
attribute [simp] monoidal_category.tensor_id
restate_axiom monoidal_category.tensor_comp'
attribute [simp] monoidal_category.tensor_comp
restate_axiom monoidal_category.associator_naturality'
attribute [reassoc] monoidal_category.associator_naturality
restate_axiom monoidal_category.left_unitor_naturality'
attribute [reassoc] monoidal_category.left_unitor_naturality
restate_axiom monoidal_category.right_unitor_naturality'
attribute [reassoc] monoidal_category.right_unitor_naturality
restate_axiom monoidal_category.pentagon'
restate_axiom monoidal_category.triangle'
attribute [simp] monoidal_category.triangle
open monoidal_category
infixr ` ⊗ `:70 := tensor_obj
infixr ` ⊗ `:70 := tensor_hom
notation `𝟙_` := tensor_unit
notation `α_` := associator
notation `λ_` := left_unitor
notation `ρ_` := right_unitor
/-- The tensor product of two isomorphisms is an isomorphism. -/
def tensor_iso {C : Type u} {X Y X' Y' : C} [category.{v} C] [monoidal_category.{v} C] (f : X ≅ Y) (g : X' ≅ Y') :
X ⊗ X' ≅ Y ⊗ Y' :=
{ hom := f.hom ⊗ g.hom,
inv := f.inv ⊗ g.inv,
hom_inv_id' := by rw [←tensor_comp, iso.hom_inv_id, iso.hom_inv_id, ←tensor_id],
inv_hom_id' := by rw [←tensor_comp, iso.inv_hom_id, iso.inv_hom_id, ←tensor_id] }
infixr ` ⊗ `:70 := tensor_iso
namespace monoidal_category
section
variables {C : Type u} [category.{v} C] [monoidal_category.{v} C]
instance tensor_is_iso {W X Y Z : C} (f : W ⟶ X) [is_iso f] (g : Y ⟶ Z) [is_iso g] : is_iso (f ⊗ g) :=
{ ..(as_iso f ⊗ as_iso g) }
@[simp] lemma inv_tensor {W X Y Z : C} (f : W ⟶ X) [is_iso f] (g : Y ⟶ Z) [is_iso g] :
inv (f ⊗ g) = inv f ⊗ inv g := rfl
variables {U V W X Y Z : C}
-- When `rewrite_search` lands, add @[search] attributes to
-- monoidal_category.tensor_id monoidal_category.tensor_comp monoidal_category.associator_naturality
-- monoidal_category.left_unitor_naturality monoidal_category.right_unitor_naturality
-- monoidal_category.pentagon monoidal_category.triangle
-- tensor_comp_id tensor_id_comp comp_id_tensor_tensor_id
-- triangle_assoc_comp_left triangle_assoc_comp_right triangle_assoc_comp_left_inv triangle_assoc_comp_right_inv
-- left_unitor_tensor left_unitor_tensor_inv
-- right_unitor_tensor right_unitor_tensor_inv
-- pentagon_inv
-- associator_inv_naturality
-- left_unitor_inv_naturality
-- right_unitor_inv_naturality
@[simp] lemma comp_tensor_id (f : W ⟶ X) (g : X ⟶ Y) :
(f ≫ g) ⊗ (𝟙 Z) = (f ⊗ (𝟙 Z)) ≫ (g ⊗ (𝟙 Z)) :=
by { rw ←tensor_comp, simp }
@[simp] lemma id_tensor_comp (f : W ⟶ X) (g : X ⟶ Y) :
(𝟙 Z) ⊗ (f ≫ g) = (𝟙 Z ⊗ f) ≫ (𝟙 Z ⊗ g) :=
by { rw ←tensor_comp, simp }
@[simp] lemma id_tensor_comp_tensor_id (f : W ⟶ X) (g : Y ⟶ Z) :
((𝟙 Y) ⊗ f) ≫ (g ⊗ (𝟙 X)) = g ⊗ f :=
by { rw [←tensor_comp], simp }
@[simp] lemma tensor_id_comp_id_tensor (f : W ⟶ X) (g : Y ⟶ Z) :
(g ⊗ (𝟙 W)) ≫ ((𝟙 Z) ⊗ f) = g ⊗ f :=
by { rw [←tensor_comp], simp }
lemma left_unitor_inv_naturality {X X' : C} (f : X ⟶ X') :
f ≫ (λ_ X').inv = (λ_ X).inv ≫ (𝟙 _ ⊗ f) :=
begin
apply (cancel_mono (λ_ X').hom).1,
simp only [assoc, comp_id, iso.inv_hom_id],
rw [left_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp]
end
lemma right_unitor_inv_naturality {X X' : C} (f : X ⟶ X') :
f ≫ (ρ_ X').inv = (ρ_ X).inv ≫ (f ⊗ 𝟙 _) :=
begin
apply (cancel_mono (ρ_ X').hom).1,
simp only [assoc, comp_id, iso.inv_hom_id],
rw [right_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp]
end
@[simp] lemma tensor_left_iff
{X Y : C} (f g : X ⟶ Y) :
((𝟙 (𝟙_ C)) ⊗ f = (𝟙 (𝟙_ C)) ⊗ g) ↔ (f = g) :=
begin
split,
{ intro h,
have h' := congr_arg (λ k, (λ_ _).inv ≫ k) h,
dsimp at h',
rw [←left_unitor_inv_naturality, ←left_unitor_inv_naturality] at h',
exact (cancel_mono _).1 h', },
{ intro h, subst h, }
end
@[simp] lemma tensor_right_iff
{X Y : C} (f g : X ⟶ Y) :
(f ⊗ (𝟙 (𝟙_ C)) = g ⊗ (𝟙 (𝟙_ C))) ↔ (f = g) :=
begin
split,
{ intro h,
have h' := congr_arg (λ k, (ρ_ _).inv ≫ k) h,
dsimp at h',
rw [←right_unitor_inv_naturality, ←right_unitor_inv_naturality] at h',
exact (cancel_mono _).1 h' },
{ intro h, subst h, }
end
-- We now prove:
-- ((α_ (𝟙_ C) X Y).hom) ≫
-- ((λ_ (X ⊗ Y)).hom)
-- = ((λ_ X).hom ⊗ (𝟙 Y))
-- (and the corresponding fact for right unitors)
-- following the proof on nLab:
-- Lemma 2.2 at <https://ncatlab.org/nlab/revision/monoidal+category/115>
lemma left_unitor_product_aux_perimeter (X Y : C) :
((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y)) ≫
(α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom ≫
((𝟙 (𝟙_ C)) ⊗ (α_ (𝟙_ C) X Y).hom) ≫
((𝟙 (𝟙_ C)) ⊗ (λ_ (X ⊗ Y)).hom)
= (((ρ_ (𝟙_ C)).hom ⊗ (𝟙 X)) ⊗ (𝟙 Y)) ≫
(α_ (𝟙_ C) X Y).hom :=
begin
conv_lhs { congr, skip, rw [←category.assoc] },
rw [←category.assoc, monoidal_category.pentagon, associator_naturality, tensor_id,
←monoidal_category.triangle, ←category.assoc]
end
lemma left_unitor_product_aux_triangle (X Y : C) :
((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y)) ≫
(((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom) ⊗ (𝟙 Y))
= ((ρ_ (𝟙_ C)).hom ⊗ (𝟙 X)) ⊗ (𝟙 Y) :=
by rw [←comp_tensor_id, ←monoidal_category.triangle]
lemma left_unitor_product_aux_square (X Y : C) :
(α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom ≫
((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom ⊗ (𝟙 Y))
= (((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom) ⊗ (𝟙 Y)) ≫
(α_ (𝟙_ C) X Y).hom :=
by rw associator_naturality
lemma left_unitor_product_aux (X Y : C) :
((𝟙 (𝟙_ C)) ⊗ (α_ (𝟙_ C) X Y).hom) ≫
((𝟙 (𝟙_ C)) ⊗ (λ_ (X ⊗ Y)).hom)
= (𝟙 (𝟙_ C)) ⊗ ((λ_ X).hom ⊗ (𝟙 Y)) :=
begin
rw ←(cancel_epi (α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom),
rw left_unitor_product_aux_square,
rw ←(cancel_epi ((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y))),
slice_rhs 1 2 { rw left_unitor_product_aux_triangle },
conv_lhs { rw [left_unitor_product_aux_perimeter] }
end
lemma right_unitor_product_aux_perimeter (X Y : C) :
((α_ X Y (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫
(α_ X (Y ⊗ (𝟙_ C)) (𝟙_ C)).hom ≫
((𝟙 X) ⊗ (α_ Y (𝟙_ C) (𝟙_ C)).hom) ≫
((𝟙 X) ⊗ (𝟙 Y) ⊗ (λ_ (𝟙_ C)).hom)
= ((ρ_ (X ⊗ Y)).hom ⊗ (𝟙 (𝟙_ C))) ≫
(α_ X Y (𝟙_ C)).hom :=
begin
transitivity (((α_ X Y _).hom ⊗ 𝟙 _) ≫ (α_ X _ _).hom ≫
(𝟙 X ⊗ (α_ Y _ _).hom)) ≫
(𝟙 X ⊗ 𝟙 Y ⊗ (λ_ _).hom),
{ conv_lhs { congr, skip, rw [←category.assoc] },
conv_rhs { rw [category.assoc] } },
{ conv_lhs { congr, rw [monoidal_category.pentagon] },
conv_rhs { congr, rw [←monoidal_category.triangle] },
conv_rhs { rw [category.assoc] },
conv_rhs { congr, skip, congr, congr, rw [←tensor_id] },
conv_rhs { congr, skip, rw [associator_naturality] },
conv_rhs { rw [←category.assoc] } }
end
lemma right_unitor_product_aux_triangle (X Y : C) :
((𝟙 X) ⊗ (α_ Y (𝟙_ C) (𝟙_ C)).hom) ≫
((𝟙 X) ⊗ (𝟙 Y) ⊗ (λ_ (𝟙_ C)).hom)
= (𝟙 X) ⊗ (ρ_ Y).hom ⊗ (𝟙 (𝟙_ C)) :=
by rw [←id_tensor_comp, ←monoidal_category.triangle]
lemma right_unitor_product_aux_square (X Y : C) :
(α_ X (Y ⊗ (𝟙_ C)) (𝟙_ C)).hom ≫
((𝟙 X) ⊗ (ρ_ Y).hom ⊗ (𝟙 (𝟙_ C)))
= (((𝟙 X) ⊗ (ρ_ Y).hom) ⊗ (𝟙 (𝟙_ C))) ≫
(α_ X Y (𝟙_ C)).hom :=
by rw [associator_naturality]
lemma right_unitor_product_aux (X Y : C) :
((α_ X Y (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫
(((𝟙 X) ⊗ (ρ_ Y).hom) ⊗ (𝟙 (𝟙_ C)))
= ((ρ_ (X ⊗ Y)).hom ⊗ (𝟙 (𝟙_ C))) :=
begin
rw ←(cancel_mono (α_ X Y (𝟙_ C)).hom),
slice_lhs 2 3 { rw ←right_unitor_product_aux_square },
rw [←right_unitor_product_aux_triangle, ←right_unitor_product_aux_perimeter],
end
-- See Proposition 2.2.4 of <http://www-math.mit.edu/~etingof/egnobookfinal.pdf>
@[simp] lemma left_unitor_tensor (X Y : C) :
((α_ (𝟙_ C) X Y).hom) ≫ ((λ_ (X ⊗ Y)).hom) =
((λ_ X).hom ⊗ (𝟙 Y)) :=
by rw [←tensor_left_iff, id_tensor_comp, left_unitor_product_aux]
@[simp] lemma left_unitor_tensor_inv (X Y : C) :
((λ_ (X ⊗ Y)).inv) ≫ ((α_ (𝟙_ C) X Y).inv) =
((λ_ X).inv ⊗ (𝟙 Y)) :=
eq_of_inv_eq_inv (by simp)
@[simp] lemma right_unitor_tensor (X Y : C) :
((α_ X Y (𝟙_ C)).hom) ≫ ((𝟙 X) ⊗ (ρ_ Y).hom) =
((ρ_ (X ⊗ Y)).hom) :=
by rw [←tensor_right_iff, comp_tensor_id, right_unitor_product_aux]
@[simp] lemma right_unitor_tensor_inv (X Y : C) :
((𝟙 X) ⊗ (ρ_ Y).inv) ≫ ((α_ X Y (𝟙_ C)).inv) =
((ρ_ (X ⊗ Y)).inv) :=
eq_of_inv_eq_inv (by simp)
lemma associator_inv_naturality {X Y Z X' Y' Z' : C} (f : X ⟶ X') (g : Y ⟶ Y') (h : Z ⟶ Z') :
(f ⊗ (g ⊗ h)) ≫ (α_ X' Y' Z').inv = (α_ X Y Z).inv ≫ ((f ⊗ g) ⊗ h) :=
begin
apply (cancel_mono (α_ X' Y' Z').hom).1,
simp only [assoc, comp_id, iso.inv_hom_id],
rw [associator_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp]
end
lemma pentagon_inv (W X Y Z : C) :
((𝟙 W) ⊗ (α_ X Y Z).inv) ≫ (α_ W (X ⊗ Y) Z).inv ≫ ((α_ W X Y).inv ⊗ (𝟙 Z))
= (α_ W X (Y ⊗ Z)).inv ≫ (α_ (W ⊗ X) Y Z).inv :=
begin
apply category_theory.eq_of_inv_eq_inv,
dsimp,
rw [category.assoc, monoidal_category.pentagon]
end
lemma triangle_assoc_comp_left (X Y : C) :
(α_ X (𝟙_ C) Y).hom ≫ ((𝟙 X) ⊗ (λ_ Y).hom) = (ρ_ X).hom ⊗ 𝟙 Y :=
monoidal_category.triangle X Y
@[simp] lemma triangle_assoc_comp_right (X Y : C) :
(α_ X (𝟙_ C) Y).inv ≫ ((ρ_ X).hom ⊗ 𝟙 Y) = ((𝟙 X) ⊗ (λ_ Y).hom) :=
by rw [←triangle_assoc_comp_left, ←category.assoc, iso.inv_hom_id, category.id_comp]
@[simp] lemma triangle_assoc_comp_right_inv (X Y : C) :
((ρ_ X).inv ⊗ 𝟙 Y) ≫ (α_ X (𝟙_ C) Y).hom = ((𝟙 X) ⊗ (λ_ Y).inv) :=
begin
apply (cancel_mono (𝟙 X ⊗ (λ_ Y).hom)).1,
simp only [assoc, triangle_assoc_comp_left],
rw [←comp_tensor_id, iso.inv_hom_id, ←id_tensor_comp, iso.inv_hom_id]
end
@[simp] lemma triangle_assoc_comp_left_inv (X Y : C) :
((𝟙 X) ⊗ (λ_ Y).inv) ≫ (α_ X (𝟙_ C) Y).inv = ((ρ_ X).inv ⊗ 𝟙 Y) :=
begin
apply (cancel_mono ((ρ_ X).hom ⊗ 𝟙 Y)).1,
simp only [triangle_assoc_comp_right, assoc],
rw [←id_tensor_comp, iso.inv_hom_id, ←comp_tensor_id, iso.inv_hom_id]
end
end
section
variables (C : Type u) [category.{v} C] [monoidal_category.{v} C]
/-- The tensor product expressed as a functor. -/
def tensor : (C × C) ⥤ C :=
{ obj := λ X, X.1 ⊗ X.2,
map := λ {X Y : C × C} (f : X ⟶ Y), f.1 ⊗ f.2 }
/-- The left-associated triple tensor product as a functor. -/
def left_assoc_tensor : (C × C × C) ⥤ C :=
{ obj := λ X, (X.1 ⊗ X.2.1) ⊗ X.2.2,
map := λ {X Y : C × C × C} (f : X ⟶ Y), (f.1 ⊗ f.2.1) ⊗ f.2.2 }
@[simp] lemma left_assoc_tensor_obj (X) :
(left_assoc_tensor C).obj X = (X.1 ⊗ X.2.1) ⊗ X.2.2 := rfl
@[simp] lemma left_assoc_tensor_map {X Y} (f : X ⟶ Y) :
(left_assoc_tensor C).map f = (f.1 ⊗ f.2.1) ⊗ f.2.2 := rfl
/-- The right-associated triple tensor product as a functor. -/
def right_assoc_tensor : (C × C × C) ⥤ C :=
{ obj := λ X, X.1 ⊗ (X.2.1 ⊗ X.2.2),
map := λ {X Y : C × C × C} (f : X ⟶ Y), f.1 ⊗ (f.2.1 ⊗ f.2.2) }
@[simp] lemma right_assoc_tensor_obj (X) :
(right_assoc_tensor C).obj X = X.1 ⊗ (X.2.1 ⊗ X.2.2) := rfl
@[simp] lemma right_assoc_tensor_map {X Y} (f : X ⟶ Y) :
(right_assoc_tensor C).map f = f.1 ⊗ (f.2.1 ⊗ f.2.2) := rfl
/-- The functor `λ X, 𝟙_ C ⊗ X`. -/
def tensor_unit_left : C ⥤ C :=
{ obj := λ X, 𝟙_ C ⊗ X,
map := λ {X Y : C} (f : X ⟶ Y), (𝟙 (𝟙_ C)) ⊗ f }
/-- The functor `λ X, X ⊗ 𝟙_ C`. -/
def tensor_unit_right : C ⥤ C :=
{ obj := λ X, X ⊗ 𝟙_ C,
map := λ {X Y : C} (f : X ⟶ Y), f ⊗ (𝟙 (𝟙_ C)) }
-- We can express the associator and the unitors, given componentwise above,
-- as natural isomorphisms.
/-- The associator as a natural isomorphism. -/
def associator_nat_iso :
left_assoc_tensor C ≅ right_assoc_tensor C :=
nat_iso.of_components
(by { intros, apply monoidal_category.associator })
(by { intros, apply monoidal_category.associator_naturality })
/-- The left unitor as a natural isomorphism. -/
def left_unitor_nat_iso :
tensor_unit_left C ≅ 𝟭 C :=
nat_iso.of_components
(by { intros, apply monoidal_category.left_unitor })
(by { intros, apply monoidal_category.left_unitor_naturality })
/-- The right unitor as a natural isomorphism. -/
def right_unitor_nat_iso :
tensor_unit_right C ≅ 𝟭 C :=
nat_iso.of_components
(by { intros, apply monoidal_category.right_unitor })
(by { intros, apply monoidal_category.right_unitor_naturality })
section
variables {C}
/-- Tensoring on the left with as fixed object, as a functor. -/
@[simps]
def tensor_left (X : C) : C ⥤ C :=
{ obj := λ Y, X ⊗ Y,
map := λ Y Y' f, (𝟙 X) ⊗ f, }
/--
Tensoring on the left with `X ⊗ Y` is naturally isomorphic to
tensoring on the left with `Y`, and then again with `X`.
-/
def tensor_left_tensor (X Y : C) : tensor_left (X ⊗ Y) ≅ tensor_left Y ⋙ tensor_left X :=
nat_iso.of_components
(associator _ _)
(λ Z Z' f, by { dsimp, rw[←tensor_id], apply associator_naturality })
@[simp] lemma tensor_left_tensor_hom_app (X Y Z : C) :
(tensor_left_tensor.{v} X Y).hom.app Z = (associator X Y Z).hom :=
rfl
@[simp] lemma tensor_left_tensor_inv_app (X Y Z : C) :
(tensor_left_tensor.{v} X Y).inv.app Z = (associator X Y Z).inv :=
rfl
/-- Tensoring on the right with as fixed object, as a functor. -/
@[simps]
def tensor_right (X : C) : C ⥤ C :=
{ obj := λ Y, Y ⊗ X,
map := λ Y Y' f, f ⊗ (𝟙 X), }
/--
Tensoring on the right with `X ⊗ Y` is naturally isomorphic to
tensoring on the right with `X`, and then again with `Y`.
-/
def tensor_right_tensor (X Y : C) : tensor_right (X ⊗ Y) ≅ tensor_right X ⋙ tensor_right Y :=
nat_iso.of_components
(λ Z, (associator.{v} Z X Y).symm)
(λ Z Z' f, by { dsimp, rw[←tensor_id], apply associator_inv_naturality })
@[simp] lemma tensor_right_tensor_hom_app (X Y Z : C) :
(tensor_right_tensor.{v} X Y).hom.app Z = (associator Z X Y).inv :=
rfl
@[simp] lemma tensor_right_tensor_inv_app (X Y Z : C) :
(tensor_right_tensor.{v} X Y).inv.app Z = (associator Z X Y).hom :=
rfl
end
end
end monoidal_category
end category_theory
|
6ee997259a127ea8c0552a8005f70678e2b43ee5 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/topology/instances/matrix.lean | e6ecbaf2d458a0981171d92c398280597e9780ad | [
"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 | 18,206 | lean | /-
Copyright (c) 2021 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash, Eric Wieser
-/
import linear_algebra.determinant
import topology.algebra.infinite_sum
import topology.algebra.ring
import topology.algebra.star
/-!
# Topological properties of matrices
This file is a place to collect topological results about matrices.
## Main definitions:
* `matrix.topological_ring`: square matrices form a topological ring
## Main results
* Continuity:
* `continuous.matrix_det`: the determinant is continuous over a topological ring.
* `continuous.matrix_adjugate`: the adjugate is continuous over a topological ring.
* Infinite sums
* `matrix.transpose_tsum`: transpose commutes with infinite sums
* `matrix.diagonal_tsum`: diagonal commutes with infinite sums
* `matrix.block_diagonal_tsum`: block diagonal commutes with infinite sums
* `matrix.block_diagonal'_tsum`: non-uniform block diagonal commutes with infinite sums
-/
open matrix
open_locale matrix
variables {X α l m n p S R : Type*} {m' n' : l → Type*}
instance [topological_space R] : topological_space (matrix m n R) := Pi.topological_space
instance [topological_space R] [t2_space R] : t2_space (matrix m n R) := Pi.t2_space
/-! ### Lemmas about continuity of operations -/
section continuity
variables [topological_space X] [topological_space R]
instance [has_scalar α R] [has_continuous_const_smul α R] :
has_continuous_const_smul α (matrix n n R) :=
pi.has_continuous_const_smul
instance [topological_space α] [has_scalar α R] [has_continuous_smul α R] :
has_continuous_smul α (matrix n n R) :=
pi.has_continuous_smul
/-- To show a function into matrices is continuous it suffices to show the coefficients of the
resulting matrix are continuous -/
@[continuity]
lemma continuous_matrix [topological_space α] {f : α → matrix m n R}
(h : ∀ i j, continuous (λ a, f a i j)) : continuous f :=
continuous_pi $ λ _, continuous_pi $ λ j, h _ _
lemma continuous.matrix_elem {A : X → matrix m n R} (hA : continuous A) (i : m) (j : n) :
continuous (λ x, A x i j) :=
(continuous_apply_apply i j).comp hA
@[continuity]
lemma continuous.matrix_map [topological_space S] {A : X → matrix m n S} {f : S → R}
(hA : continuous A) (hf : continuous f) :
continuous (λ x, (A x).map f) :=
continuous_matrix $ λ i j, hf.comp $ hA.matrix_elem _ _
@[continuity]
lemma continuous.matrix_transpose {A : X → matrix m n R} (hA : continuous A) :
continuous (λ x, (A x)ᵀ) :=
continuous_matrix $ λ i j, hA.matrix_elem j i
lemma continuous.matrix_conj_transpose [has_star R] [has_continuous_star R] {A : X → matrix m n R}
(hA : continuous A) : continuous (λ x, (A x)ᴴ) :=
hA.matrix_transpose.matrix_map continuous_star
instance [has_star R] [has_continuous_star R] : has_continuous_star (matrix m m R) :=
⟨continuous_id.matrix_conj_transpose⟩
@[continuity]
lemma continuous.matrix_col {A : X → n → R} (hA : continuous A) : continuous (λ x, col (A x)) :=
continuous_matrix $ λ i j, (continuous_apply _).comp hA
@[continuity]
lemma continuous.matrix_row {A : X → n → R} (hA : continuous A) : continuous (λ x, row (A x)) :=
continuous_matrix $ λ i j, (continuous_apply _).comp hA
@[continuity]
lemma continuous.matrix_diagonal [has_zero R] [decidable_eq n] {A : X → n → R} (hA : continuous A) :
continuous (λ x, diagonal (A x)) :=
continuous_matrix $ λ i j, ((continuous_apply i).comp hA).if_const _ continuous_zero
@[continuity]
lemma continuous.matrix_dot_product [fintype n] [has_mul R] [add_comm_monoid R]
[has_continuous_add R] [has_continuous_mul R]
{A : X → n → R} {B : X → n → R} (hA : continuous A) (hB : continuous B) :
continuous (λ x, dot_product (A x) (B x)) :=
continuous_finset_sum _ $ λ i _, ((continuous_apply i).comp hA).mul ((continuous_apply i).comp hB)
/-- For square matrices the usual `continuous_mul` can be used. -/
@[continuity]
lemma continuous.matrix_mul [fintype n] [has_mul R] [add_comm_monoid R] [has_continuous_add R]
[has_continuous_mul R]
{A : X → matrix m n R} {B : X → matrix n p R} (hA : continuous A) (hB : continuous B) :
continuous (λ x, (A x).mul (B x)) :=
continuous_matrix $ λ i j, continuous_finset_sum _ $ λ k _,
(hA.matrix_elem _ _).mul (hB.matrix_elem _ _)
instance [fintype n] [has_mul R] [add_comm_monoid R] [has_continuous_add R]
[has_continuous_mul R] : has_continuous_mul (matrix n n R) :=
⟨continuous_fst.matrix_mul continuous_snd⟩
instance [fintype n] [non_unital_non_assoc_semiring R] [topological_semiring R] :
topological_semiring (matrix n n R) :=
{ ..pi.has_continuous_add }
instance [fintype n] [non_unital_non_assoc_ring R] [topological_ring R] :
topological_ring (matrix n n R) :=
{ ..pi.has_continuous_neg, ..pi.has_continuous_add }
@[continuity]
lemma continuous.matrix_vec_mul_vec [has_mul R] [has_continuous_mul R]
{A : X → m → R} {B : X → n → R} (hA : continuous A) (hB : continuous B) :
continuous (λ x, vec_mul_vec (A x) (B x)) :=
continuous_matrix $ λ i j, ((continuous_apply _).comp hA).mul ((continuous_apply _).comp hB)
@[continuity]
lemma continuous.matrix_mul_vec [non_unital_non_assoc_semiring R] [has_continuous_add R]
[has_continuous_mul R] [fintype n]
{A : X → matrix m n R} {B : X → n → R} (hA : continuous A) (hB : continuous B) :
continuous (λ x, (A x).mul_vec (B x)) :=
continuous_pi $ λ i, ((continuous_apply i).comp hA).matrix_dot_product hB
@[continuity]
lemma continuous.matrix_vec_mul [non_unital_non_assoc_semiring R] [has_continuous_add R]
[has_continuous_mul R] [fintype m]
{A : X → m → R} {B : X → matrix m n R} (hA : continuous A) (hB : continuous B) :
continuous (λ x, vec_mul (A x) (B x)) :=
continuous_pi $ λ i, hA.matrix_dot_product $ continuous_pi $ λ j, hB.matrix_elem _ _
@[continuity]
lemma continuous.matrix_minor {A : X → matrix l n R} (hA : continuous A) (e₁ : m → l) (e₂ : p → n) :
continuous (λ x, (A x).minor e₁ e₂) :=
continuous_matrix $ λ i j, hA.matrix_elem _ _
@[continuity]
lemma continuous.matrix_reindex {A : X → matrix l n R}
(hA : continuous A) (e₁ : l ≃ m) (e₂ : n ≃ p) :
continuous (λ x, reindex e₁ e₂ (A x)) :=
hA.matrix_minor _ _
@[continuity]
lemma continuous.matrix_diag {A : X → matrix n n R} (hA : continuous A) :
continuous (λ x, matrix.diag (A x)) :=
continuous_pi $ λ _, hA.matrix_elem _ _
-- note this doesn't elaborate well from the above
lemma continuous_matrix_diag : continuous (matrix.diag : matrix n n R → n → R) :=
show continuous (λ x : matrix n n R, matrix.diag x), from continuous_id.matrix_diag
@[continuity]
lemma continuous.matrix_trace [fintype n] [add_comm_monoid R] [has_continuous_add R]
{A : X → matrix n n R} (hA : continuous A) :
continuous (λ x, trace (A x)) :=
continuous_finset_sum _ $ λ i hi, hA.matrix_elem _ _
@[continuity]
lemma continuous.matrix_det [fintype n] [decidable_eq n] [comm_ring R] [topological_ring R]
{A : X → matrix n n R} (hA : continuous A) :
continuous (λ x, (A x).det) :=
begin
simp_rw matrix.det_apply,
refine continuous_finset_sum _ (λ l _, continuous.const_smul _ _),
refine continuous_finset_prod _ (λ l _, hA.matrix_elem _ _),
end
@[continuity]
lemma continuous.matrix_update_column [decidable_eq n] (i : n)
{A : X → matrix m n R} {B : X → m → R} (hA : continuous A) (hB : continuous B) :
continuous (λ x, (A x).update_column i (B x)) :=
continuous_matrix $ λ j k, (continuous_apply k).comp $
((continuous_apply _).comp hA).update i ((continuous_apply _).comp hB)
@[continuity]
lemma continuous.matrix_update_row [decidable_eq m] (i : m)
{A : X → matrix m n R} {B : X → n → R} (hA : continuous A) (hB : continuous B) :
continuous (λ x, (A x).update_row i (B x)) :=
hA.update i hB
@[continuity]
lemma continuous.matrix_cramer [fintype n] [decidable_eq n] [comm_ring R] [topological_ring R]
{A : X → matrix n n R} {B : X → n → R} (hA : continuous A) (hB : continuous B) :
continuous (λ x, (A x).cramer (B x)) :=
continuous_pi $ λ i, (hA.matrix_update_column _ hB).matrix_det
@[continuity]
lemma continuous.matrix_adjugate [fintype n] [decidable_eq n] [comm_ring R] [topological_ring R]
{A : X → matrix n n R} (hA : continuous A) :
continuous (λ x, (A x).adjugate) :=
continuous_matrix $ λ j k, (hA.matrix_transpose.matrix_update_column k continuous_const).matrix_det
/-- When `ring.inverse` is continuous at the determinant (such as in a `normed_ring`, or a
`topological_field`), so is `matrix.has_inv`. -/
lemma continuous_at_matrix_inv [fintype n] [decidable_eq n] [comm_ring R] [topological_ring R]
(A : matrix n n R) (h : continuous_at ring.inverse A.det) :
continuous_at has_inv.inv A :=
(h.comp continuous_id.matrix_det.continuous_at).smul continuous_id.matrix_adjugate.continuous_at
-- lemmas about functions in `data/matrix/block.lean`
section block_matrices
@[continuity]
lemma continuous.matrix_from_blocks
{A : X → matrix n l R} {B : X → matrix n m R} {C : X → matrix p l R} {D : X → matrix p m R}
(hA : continuous A) (hB : continuous B) (hC : continuous C) (hD : continuous D) :
continuous (λ x, matrix.from_blocks (A x) (B x) (C x) (D x)) :=
continuous_matrix $ λ i j,
by cases i; cases j; refine continuous.matrix_elem _ i j; assumption
@[continuity]
lemma continuous.matrix_block_diagonal [has_zero R] [decidable_eq p] {A : X → p → matrix m n R}
(hA : continuous A) :
continuous (λ x, block_diagonal (A x)) :=
continuous_matrix $ λ ⟨i₁, i₂⟩ ⟨j₁, j₂⟩,
(((continuous_apply i₂).comp hA).matrix_elem i₁ j₁).if_const _ continuous_zero
@[continuity]
lemma continuous.matrix_block_diag {A : X → matrix (m × p) (n × p) R} (hA : continuous A) :
continuous (λ x, block_diag (A x)) :=
continuous_pi $ λ i, continuous_matrix $ λ j k, hA.matrix_elem _ _
@[continuity]
lemma continuous.matrix_block_diagonal' [has_zero R] [decidable_eq l]
{A : X → Π i, matrix (m' i) (n' i) R} (hA : continuous A) :
continuous (λ x, block_diagonal' (A x)) :=
continuous_matrix $ λ ⟨i₁, i₂⟩ ⟨j₁, j₂⟩, begin
dsimp only [block_diagonal'],
split_ifs,
{ subst h,
exact ((continuous_apply i₁).comp hA).matrix_elem i₂ j₂ },
{ exact continuous_const },
end
@[continuity]
lemma continuous.matrix_block_diag' {A : X → matrix (Σ i, m' i) (Σ i, n' i) R} (hA : continuous A) :
continuous (λ x, block_diag' (A x)) :=
continuous_pi $ λ i, continuous_matrix $ λ j k, hA.matrix_elem _ _
end block_matrices
end continuity
/-! ### Lemmas about infinite sums -/
section tsum
variables [semiring α] [add_comm_monoid R] [topological_space R] [module α R]
lemma has_sum.matrix_transpose {f : X → matrix m n R} {a : matrix m n R} (hf : has_sum f a) :
has_sum (λ x, (f x)ᵀ) aᵀ :=
(hf.map (@matrix.transpose_add_equiv m n R _) continuous_id.matrix_transpose : _)
lemma summable.matrix_transpose {f : X → matrix m n R} (hf : summable f) :
summable (λ x, (f x)ᵀ) :=
hf.has_sum.matrix_transpose.summable
@[simp] lemma summable_matrix_transpose {f : X → matrix m n R} :
summable (λ x, (f x)ᵀ) ↔ summable f :=
(summable.map_iff_of_equiv (@matrix.transpose_add_equiv m n R _)
(@continuous_id (matrix m n R) _).matrix_transpose (continuous_id.matrix_transpose) : _)
lemma matrix.transpose_tsum [t2_space R] {f : X → matrix m n R} : (∑' x, f x)ᵀ = ∑' x, (f x)ᵀ :=
begin
by_cases hf : summable f,
{ exact hf.has_sum.matrix_transpose.tsum_eq.symm },
{ have hft := summable_matrix_transpose.not.mpr hf,
rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable hft, transpose_zero] },
end
lemma has_sum.matrix_conj_transpose [star_add_monoid R] [has_continuous_star R]
{f : X → matrix m n R} {a : matrix m n R} (hf : has_sum f a) :
has_sum (λ x, (f x)ᴴ) aᴴ :=
(hf.map (@matrix.conj_transpose_add_equiv m n R _ _) continuous_id.matrix_conj_transpose : _)
lemma summable.matrix_conj_transpose [star_add_monoid R] [has_continuous_star R]
{f : X → matrix m n R} (hf : summable f) :
summable (λ x, (f x)ᴴ) :=
hf.has_sum.matrix_conj_transpose.summable
@[simp] lemma summable_matrix_conj_transpose [star_add_monoid R] [has_continuous_star R]
{f : X → matrix m n R} :
summable (λ x, (f x)ᴴ) ↔ summable f :=
(summable.map_iff_of_equiv (@matrix.conj_transpose_add_equiv m n R _ _)
(@continuous_id (matrix m n R) _).matrix_conj_transpose (continuous_id.matrix_conj_transpose) : _)
lemma matrix.conj_transpose_tsum [star_add_monoid R] [has_continuous_star R] [t2_space R]
{f : X → matrix m n R} : (∑' x, f x)ᴴ = ∑' x, (f x)ᴴ :=
begin
by_cases hf : summable f,
{ exact hf.has_sum.matrix_conj_transpose.tsum_eq.symm },
{ have hft := summable_matrix_conj_transpose.not.mpr hf,
rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable hft, conj_transpose_zero] },
end
lemma has_sum.matrix_diagonal [decidable_eq n] {f : X → n → R} {a : n → R} (hf : has_sum f a) :
has_sum (λ x, diagonal (f x)) (diagonal a) :=
(hf.map (diagonal_add_monoid_hom n R) $ continuous.matrix_diagonal $ by exact continuous_id : _)
lemma summable.matrix_diagonal [decidable_eq n] {f : X → n → R} (hf : summable f) :
summable (λ x, diagonal (f x)) :=
hf.has_sum.matrix_diagonal.summable
@[simp] lemma summable_matrix_diagonal [decidable_eq n] {f : X → n → R} :
summable (λ x, diagonal (f x)) ↔ summable f :=
(summable.map_iff_of_left_inverse
(@matrix.diagonal_add_monoid_hom n R _ _) (matrix.diag_add_monoid_hom n R)
(by exact continuous.matrix_diagonal continuous_id)
continuous_matrix_diag
(λ A, diag_diagonal A) : _)
lemma matrix.diagonal_tsum [decidable_eq n] [t2_space R] {f : X → n → R} :
diagonal (∑' x, f x) = ∑' x, diagonal (f x) :=
begin
by_cases hf : summable f,
{ exact hf.has_sum.matrix_diagonal.tsum_eq.symm },
{ have hft := summable_matrix_diagonal.not.mpr hf,
rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable hft],
exact diagonal_zero },
end
lemma has_sum.matrix_diag {f : X → matrix n n R} {a : matrix n n R} (hf : has_sum f a) :
has_sum (λ x, diag (f x)) (diag a) :=
(hf.map (diag_add_monoid_hom n R) continuous_matrix_diag : _)
lemma summable.matrix_diag {f : X → matrix n n R} (hf : summable f) : summable (λ x, diag (f x)) :=
hf.has_sum.matrix_diag.summable
section block_matrices
lemma has_sum.matrix_block_diagonal [decidable_eq p]
{f : X → p → matrix m n R} {a : p → matrix m n R} (hf : has_sum f a) :
has_sum (λ x, block_diagonal (f x)) (block_diagonal a) :=
(hf.map (block_diagonal_add_monoid_hom m n p R) $
continuous.matrix_block_diagonal $ by exact continuous_id : _)
lemma summable.matrix_block_diagonal [decidable_eq p] {f : X → p → matrix m n R} (hf : summable f) :
summable (λ x, block_diagonal (f x)) :=
hf.has_sum.matrix_block_diagonal.summable
lemma summable_matrix_block_diagonal [decidable_eq p] {f : X → p → matrix m n R} :
summable (λ x, block_diagonal (f x)) ↔ summable f :=
(summable.map_iff_of_left_inverse
(matrix.block_diagonal_add_monoid_hom m n p R) (matrix.block_diag_add_monoid_hom m n p R)
(by exact continuous.matrix_block_diagonal continuous_id)
(by exact continuous.matrix_block_diag continuous_id)
(λ A, block_diag_block_diagonal A) : _)
lemma matrix.block_diagonal_tsum [decidable_eq p] [t2_space R] {f : X → p → matrix m n R} :
block_diagonal (∑' x, f x) = ∑' x, block_diagonal (f x) :=
begin
by_cases hf : summable f,
{ exact hf.has_sum.matrix_block_diagonal.tsum_eq.symm },
{ have hft := summable_matrix_block_diagonal.not.mpr hf,
rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable hft],
exact block_diagonal_zero },
end
lemma has_sum.matrix_block_diag {f : X → matrix (m × p) (n × p) R}
{a : matrix (m × p) (n × p) R} (hf : has_sum f a) :
has_sum (λ x, block_diag (f x)) (block_diag a) :=
(hf.map (block_diag_add_monoid_hom m n p R) $ continuous.matrix_block_diag continuous_id : _)
lemma summable.matrix_block_diag {f : X → matrix (m × p) (n × p) R} (hf : summable f) :
summable (λ x, block_diag (f x)) :=
hf.has_sum.matrix_block_diag.summable
lemma has_sum.matrix_block_diagonal' [decidable_eq l]
{f : X → Π i, matrix (m' i) (n' i) R} {a : Π i, matrix (m' i) (n' i) R} (hf : has_sum f a) :
has_sum (λ x, block_diagonal' (f x)) (block_diagonal' a) :=
(hf.map (block_diagonal'_add_monoid_hom m' n' R) $
continuous.matrix_block_diagonal' $ by exact continuous_id : _)
lemma summable.matrix_block_diagonal' [decidable_eq l]
{f : X → Π i, matrix (m' i) (n' i) R} (hf : summable f) :
summable (λ x, block_diagonal' (f x)) :=
hf.has_sum.matrix_block_diagonal'.summable
lemma summable_matrix_block_diagonal' [decidable_eq l] {f : X → Π i, matrix (m' i) (n' i) R} :
summable (λ x, block_diagonal' (f x)) ↔ summable f :=
(summable.map_iff_of_left_inverse
(matrix.block_diagonal'_add_monoid_hom m' n' R) (matrix.block_diag'_add_monoid_hom m' n' R)
(by exact continuous.matrix_block_diagonal' continuous_id)
(by exact continuous.matrix_block_diag' continuous_id)
(λ A, block_diag'_block_diagonal' A) : _)
lemma matrix.block_diagonal'_tsum [decidable_eq l] [t2_space R]
{f : X → Π i, matrix (m' i) (n' i) R} :
block_diagonal' (∑' x, f x) = ∑' x, block_diagonal' (f x) :=
begin
by_cases hf : summable f,
{ exact hf.has_sum.matrix_block_diagonal'.tsum_eq.symm },
{ have hft := summable_matrix_block_diagonal'.not.mpr hf,
rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable hft],
exact block_diagonal'_zero },
end
lemma has_sum.matrix_block_diag' {f : X → matrix (Σ i, m' i) (Σ i, n' i) R}
{a : matrix (Σ i, m' i) (Σ i, n' i) R} (hf : has_sum f a) :
has_sum (λ x, block_diag' (f x)) (block_diag' a) :=
(hf.map (block_diag'_add_monoid_hom m' n' R) $ continuous.matrix_block_diag' continuous_id : _)
lemma summable.matrix_block_diag' {f : X → matrix (Σ i, m' i) (Σ i, n' i) R} (hf : summable f) :
summable (λ x, block_diag' (f x)) :=
hf.has_sum.matrix_block_diag'.summable
end block_matrices
end tsum
|
bee49ba523a9b2b4e06a904cbc33844725bac6ba | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/closed/monoidal.lean | d60c3a7b61830d6691c379d49ce7ef26f78ae67a | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,633 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.monoidal.category
import Mathlib.category_theory.adjunction.basic
import Mathlib.PostPort
universes v u l
namespace Mathlib
/-!
# Closed monoidal categories
Define (right) closed objects and (right) closed monoidal categories.
## TODO
Some of the theorems proved about cartesian closed categories
should be generalised and moved to this file.
-/
namespace category_theory
/-- An object `X` is (right) closed if `(X ⊗ -)` is a left adjoint. -/
class closed {C : Type u} [category C] [monoidal_category C] (X : C)
where
is_adj : is_left_adjoint (monoidal_category.tensor_left X)
/-- A monoidal category `C` is (right) monoidal closed if every object is (right) closed. -/
class monoidal_closed (C : Type u) [category C] [monoidal_category C]
where
closed : (X : C) → closed X
/--
The unit object is always closed.
This isn't an instance because most of the time we'll prove closedness for all objects at once,
rather than just for this one.
-/
def unit_closed {C : Type u} [category C] [monoidal_category C] : closed 𝟙_ :=
closed.mk
(is_left_adjoint.mk 𝟭
(adjunction.mk_of_hom_equiv
(adjunction.core_hom_equiv.mk
fun (X _x : C) =>
equiv.mk (fun (a : functor.obj (monoidal_category.tensor_left 𝟙_) X ⟶ _x) => iso.inv λ_ ≫ a)
(fun (a : X ⟶ functor.obj 𝟭 _x) => iso.hom λ_ ≫ a) sorry sorry)))
|
36849ecea6924299070f0aa60403f4993d47ef4e | 43390109ab88557e6090f3245c47479c123ee500 | /src/inner_product_spaces/sesquilinear_forms.lean | 5cc18ecc052f699ebc3f6b722d789bf7a60b3166 | [
"Apache-2.0"
] | permissive | Ja1941/xena-UROP-2018 | 41f0956519f94d56b8bf6834a8d39473f4923200 | b111fb87f343cf79eca3b886f99ee15c1dd9884b | refs/heads/master | 1,662,355,955,139 | 1,590,577,325,000 | 1,590,577,325,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,018 | lean | import linear_algebra.basic inner_product_spaces.ring_involution
open module ring ring_invo
class sesquilinear_form_space (R : Type*) (V : Type*) [ring R] (Hi : ring_invo R) extends module R V :=
(sesq_form : V → V → R)
(fst_add_lin : ∀ (x y z : V), sesq_form (x + y) z = sesq_form x z + sesq_form y z)
(fst_mul_lin : ∀ (x y : V) (a : R), sesq_form (a • x) y = a * (sesq_form x y))
(snd_add_lin : ∀ (x y z : V), sesq_form x (y + z) = sesq_form x y + sesq_form x z)
(snd_mul_antilin : ∀ (a : R) (x y : V), sesq_form x (a • y) = (invo Hi a) * (sesq_form x y))
open sesquilinear_form_space
section sesquilinear_form_space
variables {R : Type*} [ring R] {V : Type*} (Hi : ring_invo R) [sesquilinear_form_space R V Hi]
lemma zero_sesq (x : V) :
sesq_form Hi 0 x = 0 := by rw [←zero_smul, fst_mul_lin, ring.zero_mul]; exact 0
lemma sesq_zero (x : V) :
sesq_form Hi x 0 = 0 := by rw [←zero_smul, snd_mul_antilin, invo_zero, ring.zero_mul]; exact 0
lemma sesq_neg_left (x y : V) :
sesq_form Hi (-x) y = -(sesq_form Hi x y : R) := by rw [←neg_one_smul, fst_mul_lin, neg_one_mul]
lemma sesq_neg_right (x y : V) :
sesq_form Hi x (-y) = -(sesq_form Hi x y) := by rw [←neg_one_smul, snd_mul_antilin, invo_neg, ring_invo.map_one, neg_one_mul]
lemma sesq_sub_left (x y z : V) :
sesq_form Hi (x - y) z = sesq_form Hi x z - sesq_form Hi y z := by rw [sub_eq_add_neg, fst_add_lin, sesq_neg_left]; refl
lemma sesq_sub_right (x y z : V) :
sesq_form Hi x (y - z) = sesq_form Hi x y - sesq_form Hi x z := by rw [sub_eq_add_neg, snd_add_lin, sesq_neg_right]; refl
def is_ortho (x y : V) : Prop :=
sesq_form Hi x y = 0
lemma ortho_zero (x : V) :
is_ortho Hi 0 x := zero_sesq Hi x
theorem ortho_mul_left {D : Type*} [domain D] (Hi : ring_invo D) [sesquilinear_form_space D V Hi] (x y : V) (a : D) (ha : a ≠ 0) :
(is_ortho Hi x y) ↔ (is_ortho Hi (a • x) y) :=
begin
dunfold is_ortho,
split,
intros H,
rw [fst_mul_lin, H, ring.mul_zero],
intros H,
rw [fst_mul_lin, mul_eq_zero] at H,
cases H,
trivial,
exact H,
end
theorem ortho_mul_right {D : Type*} [domain D] (Hi : ring_invo D) [sesquilinear_form_space D V Hi] (x y : V) (a : D) (ha : a ≠ 0) :
(is_ortho Hi x y) ↔ (is_ortho Hi x (a • y)) :=
begin
dunfold is_ortho,
split,
intros H,
rw [snd_mul_antilin, H, ring.mul_zero],
intros H,
rw [snd_mul_antilin, mul_eq_zero] at H,
cases H,
rw invo_zero_iff at H,
trivial,
exact H,
end
end sesquilinear_form_space
class sym_sesquilinear_form_space (R : Type*) (V : Type*) [ring R] (Hi : ring_invo R) extends sesquilinear_form_space R V Hi :=
(is_invo_sym : ∀ (x y : V), sesq_form x y = invo Hi (sesq_form y x))
open sym_sesquilinear_form_space
lemma ortho_sym {R : Type*} {V : Type*} [ring R] (Hi : ring_invo R) [sym_sesquilinear_form_space R V Hi] (x y : V) :
is_ortho Hi x y ↔ is_ortho Hi y x :=
begin
dunfold is_ortho,
split;
intros H;
rw is_invo_sym;
rw invo_zero_iff;
exact H,
end
|
3a5f22e9236d5be468c4dc43ca06b37ba8bb11eb | d7189ea2ef694124821b033e533f18905b5e87ef | /galois/category/monad/state/class.lean | e28879e530bc1529dcfbf469b12efd58dc1d731d | [
"Apache-2.0"
] | permissive | digama0/lean-protocol-support | eaa7e6f8b8e0d5bbfff1f7f52bfb79a3b11b0f59 | cabfa3abedbdd6fdca6e2da6fbbf91a13ed48dda | refs/heads/master | 1,625,421,450,627 | 1,506,035,462,000 | 1,506,035,462,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 851 | lean | /- This file defines a class monad_state for monads that have a state variable.
-/
universe variables u v w
namespace monad
-- A unit type with a polymorphic universe parameter.
inductive punit : Type u
| punit : punit
-- A typeclass for monad states.
--
-- The definition allows one to get a state value, and apply a function that returns
-- a value and new state.
class monad_state (m : Type u → Type v) extends monad m :=
(state : Type u)
(with_state : Π{α : Type u}, (state → α × state) → m α)
-- Get the current state value
def get {m : Type u → Type v} [inst : monad_state m] : m inst.state :=
@monad_state.with_state _ _ _ (λs, (s, s))
-- Set the current state value
def put {m : Type u → Type v} [inst : monad_state m] (v : inst.state) : m punit :=
@monad_state.with_state _ inst _ (λt, (punit.punit, v))
end monad
|
49a66bdb509528f5716cacc541f5d7c8a81d2e48 | a7602958ab456501ff85db8cf5553f7bcab201d7 | /Assignment5/9.5-homework.lean | 01338493d9af2a23aed5559894ede187da83072c | [] | no_license | enlauren/math-logic | 081e2e737c8afb28dbb337968df95ead47321ba0 | 086b6935543d1841f1db92d0e49add1124054c37 | refs/heads/master | 1,594,506,621,950 | 1,558,634,976,000 | 1,558,634,976,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,945 | lean | -- Assignment 5
-- Dom Farolino, farolidm@mail.uc.edu
-- Math Logic
-- Exercise 5
section
variable U: Type
variables A B: U → Prop
example : (∃ x, A x) → ∃ x, A x ∨ B x := -- Added parens for clarity.
assume h1: (∃ x, A x),
-- Need to do exists elimination, to get A y alone.
show ∃ x, (A x ∨ B x), from
exists.elim h1 (
assume y: U,
assume h2: A y,
have h3: A y ∨ B y, from or.inl h2,
show ∃ x, (A x ∨ B x), from exists.intro y h3
)
end
-- Exercise 6
section
variable U: Type
variables A B: U → Prop
variable h1: ∀ x, A x → B x
variable h2: ∃ x, A x
example: ∃ x, B x :=
show ∃ x, B x, from
exists.elim h2 ( -- We know we need a variable to apply |h1| to, and we can
assume y: U, -- get that variable via assumption by doing exists.elim.
assume h3: A y, -- Just usual exists.elim assumptions going on here...
have h4: A y -> B y, from h1 y,
have h5: B y, from h4 h3, -- Implication elimination.
show ∃ x, B x, from exists.intro y h5
)
end
-- Exercise 7
section
variable U: Type
variables A B C: U → Prop
variable h1: ∃ x, A x ∧ B x
variable h2: ∀ x, B x → C x
example: ∃ x, (A x ∧ C x) := -- Added parens for clarity.
show ∃ x, (A x ∧ C x), from
exists.elim h1 (
assume y: U,
assume h3: A(y) ∧ B(y),
have h4: C(y), from (h2 y) (and.right h3), -- Both ∀, and implication elimination.
show ∃ x, (A x ∧ C x), from
exists.intro y (and.intro (and.left h3) h4) -- Sorry not sorry.
-- OK, so that last line can also be written as:
-- have h5: A y ∧ C y, from and.intro (and.left h3) h4,
-- exists.intro y h5
)
end
-- Exercise 8
-- These proofs are the same as Notes/Chapter8/8.5.1.JPG.
section
variable U: Type
variables A B C: U → Prop
example : (¬ ∃ x, A x) → ∀ x, ¬ A x :=
assume h1: ¬ (∃ x, A x),
assume y: U, -- ∀-introduction (see 9.2.1); business as usual.
show ¬ (A y), from
assume h2: A y, -- Can assume this from negation.
have h3: (∃ x, A x), from exists.intro y h2,
show false, from h1 h3
example : (∀ x, ¬ A x) → ¬ ∃ x, A x :=
assume h1: (∀ x, ¬ A x),
assume h2: ∃ x, A x,
show false, from
exists.elim h2 (
assume y: U,
assume h3: A(y),
show false, from (h1 y) h3 -- Beautiful.
)
end
-- Exercise 9
section
variable U: Type
variables R: U → U → Prop
example: (∃ x, ∀ y, R x y) → ∀ y, ∃ x, R x y :=
assume h1: ∃ x, (∀ y, R x y), -- Re-arrange parens for clarity.
show ∀ y, ∃ x, (R x y), from
assume b: U,
show ∃ x, (R x b), from
exists.elim h1 (
assume a: U, -- Get to assume this and next line, bc ∃-elim.
assume h2: ∀ y, R a y,
have h3: R a b, from h2 b,
show ∃ a, (R a b), from exists.intro a h3
)
end |
61b7a7bd5741e578a5903d16a98c2ee450e94dfb | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/tactic/norm_cast.lean | 3f6bd45f219f464c465eccdc8bfd43c5b1ce0a6d | [
"Apache-2.0"
] | permissive | gebner/mathlib | eab0150cc4f79ec45d2016a8c21750244a2e7ff0 | cc6a6edc397c55118df62831e23bfbd6e6c6b4ab | refs/heads/master | 1,625,574,853,976 | 1,586,712,827,000 | 1,586,712,827,000 | 99,101,412 | 1 | 0 | Apache-2.0 | 1,586,716,389,000 | 1,501,667,958,000 | Lean | UTF-8 | Lean | false | false | 14,548 | lean | /-
Copyright (c) 2019 Paul-Nicolas Madelaine. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Paul-Nicolas Madelaine
Normalizing casts inside expressions.
-/
import tactic.basic tactic.interactive tactic.converter.interactive
import data.buffer.parser
namespace tactic
/--
This is a work around to the fact that in some cases
mk_instance times out instead of failing
example: has_lift_t ℤ ℕ
mk_instance' is used when we assume the type class search
should end instantly
-/
meta def mk_instance' (e : expr) : tactic expr :=
try_for 1000 (mk_instance e)
end tactic
namespace expr
open tactic expr
meta def flip_eq (ty : expr) : tactic (expr × (expr → expr)) :=
do
(a, b) ← is_eq ty,
α ← infer_type a,
new_ty ← to_expr ``(%%b = %%a),
f ← to_expr ``(@eq.symm %%α %%a %%b),
return (new_ty, ⇑f)
meta def flip_iff (ty : expr) : tactic (expr × (expr → expr)) :=
do
(a, b) ← is_iff ty,
new_ty ← to_expr ``(%%b ↔ %%a),
f ← to_expr ``(@iff.symm %%a %%b),
return (new_ty, ⇑f)
end expr
namespace norm_cast
open tactic expr
private meta def new_name (n : name) : name := name.mk_string "reversed" n
private meta def aux_after_set (tac : expr → tactic (expr × (expr → expr))) :
expr → tactic (expr × (expr → expr))
| (pi n bi d b) := do
uniq_n ← mk_fresh_name,
let b' := b.instantiate_var (local_const uniq_n n bi d),
(b', f) ← aux_after_set b',
return $ (
pi n bi d $ b'.abstract_local uniq_n,
λ e, lam n bi d $ ( f $ e (local_const uniq_n n bi d) ).abstract_local uniq_n
)
| ty := tac ty
mk_simp_attribute push_cast "The `push_cast` simp attribute uses `move_cast` lemmas in the \"forward\" direction,
to move casts toward the leaf nodes of the expression."
/-- Called after the `move_cast` attribute is applied to a declaration. -/
private meta def after_set (decl : name) (prio : ℕ) (pers : bool) : tactic unit :=
do
(declaration.thm n l ty e) ← get_decl decl | failed,
let tac := λ ty, (flip_eq ty <|> flip_iff ty),
(ty', f) ← aux_after_set tac ty,
let e' := task.map f e,
let n' := new_name n,
add_decl (declaration.thm n' l ty' e'),
simp_attr.push_cast.set decl () tt
private meta def mk_cache : list name → tactic simp_lemmas :=
monad.foldl simp_lemmas.add_simp simp_lemmas.mk
/--
This is an attribute for simplification rules that are
used to normalize casts.
Let r be = or ↔, then elimination lemmas of the shape
Π ..., P ↑a1 ... ↑an r P a1 ... an should be given the
attribute elim_cast.
-/
@[user_attribute]
meta def elim_cast_attr : user_attribute simp_lemmas :=
{
name := `elim_cast,
descr := "attribute for lemmas of the shape Π ..., P ↑a1 ... ↑an = P a1 ... an",
cache_cfg :=
{ mk_cache := mk_cache,
dependencies := [], },
}
/--
This is an attribute for simplification rules that are
used to normalize casts.
Let r be = or ↔, then compositional lemmas of the shape
Π ..., ↑(P a1 ... an) r P ↑a1 ... ↑an should be given the
attribute move_cast.
-/
@[user_attribute]
meta def move_cast_attr : user_attribute simp_lemmas :=
{
name := `move_cast,
descr := "attribute for lemmas of the shape Π ..., ↑(P a1 ... an) = P ↑a1 ... ↑an",
after_set := some after_set,
cache_cfg :=
{ mk_cache := mk_cache ∘ (list.map new_name),
dependencies := [], },
}
private meta def get_cache : tactic simp_lemmas :=
do
a ← elim_cast_attr.get_cache,
b ← move_cast_attr.get_cache,
return $ simp_lemmas.join a b
/--
This is an attribute for simplification rules of the shape
Π ..., ↑↑a = ↑a or Π ..., ↑a = a.
They are used in a heuristic to infer intermediate casts.
-/
@[user_attribute]
meta def squash_cast_attr : user_attribute simp_lemmas :=
{
name := `squash_cast,
descr := "attribute for lemmas of the shape Π ..., ↑↑a = ↑a",
after_set := none,
cache_cfg := {
mk_cache := monad.foldl simp_lemmas.add_simp simp_lemmas.mk,
dependencies := [],
}
}
/-
This is an auxiliary function that proves e = new_e
using only squash_cast lemmas
-/
private meta def aux_simp (e new_e : expr) : tactic expr :=
do
s ← squash_cast_attr.get_cache,
(e', pr) ← s.rewrite new_e,
is_def_eq e e',
mk_eq_symm pr
/-
This is a supecial function for numerals:
- (1 : α) is rewritten as ((1 : ℕ) : α)
- (0 : α) is rewritten as ((0 : ℕ) : α)
-/
private meta def aux_num (_ : unit) (e : expr) : tactic (unit × expr × expr) :=
match e with
| `(0 : ℕ) := failed
| `(1 : ℕ) := failed
| `(@has_zero.zero %%α %%h) := do
coe_nat ← to_expr ``(has_lift_t ℕ %%α) >>= mk_instance',
new_e ← to_expr ``(@coe ℕ %%α %%coe_nat 0),
pr ← aux_simp e new_e,
return ((), new_e, pr)
| `(@has_one.one %%α %%h) := do
coe_nat ← to_expr ``(has_lift_t ℕ %%α) >>= mk_instance',
new_e ← to_expr ``(@coe ℕ %%α %%coe_nat 1),
pr ← aux_simp e new_e,
return ((), new_e, pr)
| _ := failed
end
/-
This is the main heuristic used alongside the elim_cast and move_cast lemmas.
An expression of the shape: op (↑(x : α) : γ) (↑(y : β) : γ)
is rewritten as: op (↑(↑(x : α) : β) : γ) (↑(y : β) : γ)
when the squash_cast lemmas can prove that (↑(x : α) : γ) = (↑(↑(x : α) : β) : γ)
-/
private meta def heur (_ : unit) (e : expr) : tactic (unit × expr × expr) :=
match e with
| (app (expr.app op x) y) :=
do
`(@coe %%α %%δ %%coe1 %%xx) ← return x,
`(@coe %%β %%γ %%coe2 %%yy) ← return y,
success_if_fail $ is_def_eq α β,
is_def_eq δ γ,
(do
coe3 ← mk_app `has_lift_t [α, β] >>= mk_instance',
new_x ← to_expr ``(@coe %%β %%δ %%coe2 (@coe %%α %%β %%coe3 %%xx)),
let new_e := app (app op new_x) y,
eq_x ← aux_simp x new_x,
pr ← mk_congr_arg op eq_x,
pr ← mk_congr_fun pr y,
return ((), new_e, pr)
) <|> (do
coe3 ← mk_app `has_lift_t [β, α] >>= mk_instance',
new_y ← to_expr ``(@coe %%α %%δ %%coe1 (@coe %%β %%α %%coe3 %%yy)),
let new_e := app (app op x) new_y,
eq_y ← aux_simp y new_y,
pr ← mk_congr_arg (app op x) eq_y,
return ((), new_e, pr)
)
| _ := failed
end
/-
simpa is used to discharge proofs
-/
private meta def prove : tactic unit :=
tactic.interactive.simpa none ff [] [] none
private meta def post (s : simp_lemmas) (_ : unit) (e : expr) : tactic (unit × expr × expr) :=
do
r ← mcond (is_prop e) (return `iff) (return `eq),
(new_e, pr) ← s.rewrite e prove r,
pr ← match r with
|`iff := mk_app `propext [pr]
| _ := return pr
end,
return ((), new_e, pr)
/-
Core function
-/
meta def derive (e : expr) : tactic (expr × expr) :=
do
s ← get_cache,
e ← instantiate_mvars e,
let cfg : simp_config := {fail_if_unchanged := ff},
-- step 1: casts are moved upwards and eliminated
((), new_e, pr1) ← simplify_bottom_up ()
(λ a e, post s a e <|> heur a e <|> aux_num a e)
e cfg,
-- step 2: casts are squashed
s ← squash_cast_attr.get_cache,
(new_e, pr2) ← simplify s [] new_e cfg,
guard (¬ new_e =ₐ e),
pr ← mk_eq_trans pr1 pr2,
return (new_e, pr)
end norm_cast
namespace tactic
open tactic expr
open norm_cast
private meta def aux_mod_cast (e : expr) (include_goal : bool := tt) : tactic expr :=
match e with
| local_const _ lc _ _ := do
e ← get_local lc,
replace_at derive [e] include_goal,
get_local lc
| e := do
t ← infer_type e,
e ← assertv `this t e,
replace_at derive [e] include_goal,
get_local `this
end
meta def exact_mod_cast (e : expr) : tactic unit :=
( do
new_e ← aux_mod_cast e,
exact new_e
) <|> fail "exact_mod_cast failed"
meta def apply_mod_cast (e : expr) : tactic (list (name × expr)) :=
( do
new_e ← aux_mod_cast e,
apply new_e
) <|> fail "apply_mod_cast failed"
meta def assumption_mod_cast : tactic unit :=
do {
let cfg : simp_config := {
fail_if_unchanged := ff,
canonize_instances := ff,
canonize_proofs := ff,
proj := ff
},
replace_at derive [] tt,
ctx ← local_context,
try_lst $ ctx.map (λ h, aux_mod_cast h ff >>= tactic.exact)
} <|> fail "assumption_mod_cast failed"
end tactic
namespace tactic.interactive
open tactic interactive tactic.interactive interactive.types expr lean.parser
open norm_cast
local postfix `?`:9001 := optional
/--
Normalize casts at the given locations by moving them "upwards".
As opposed to simp, norm_cast can be used without necessarily
closing the goal.
-/
meta def norm_cast (loc : parse location) : tactic unit :=
do
ns ← loc.get_locals,
tt ← replace_at derive ns loc.include_goal
| fail "norm_cast failed to simplify",
when loc.include_goal $ try tactic.reflexivity,
when loc.include_goal $ try tactic.triv,
when (¬ ns.empty) $ try tactic.contradiction
/--
Rewrite with the given rule and normalize casts between steps.
-/
meta def rw_mod_cast (rs : parse rw_rules) (loc : parse location) : tactic unit :=
( do
let cfg_norm : simp_config := {},
let cfg_rw : rewrite_cfg := {},
ns ← loc.get_locals,
monad.mapm' (λ r : rw_rule, do
save_info r.pos,
replace_at derive ns loc.include_goal,
rw ⟨[r], none⟩ loc {}
) rs.rules,
replace_at derive ns loc.include_goal,
skip
) <|> fail "rw_mod_cast failed"
/--
Normalize the goal and the given expression,
then close the goal with exact.
-/
meta def exact_mod_cast (e : parse texpr) : tactic unit :=
do
e ← i_to_expr e <|> do {
ty ← target,
e ← i_to_expr_strict ``(%%e : %%ty),
pty ← pp ty, ptgt ← pp e,
fail ("exact_mod_cast failed, expression type not directly " ++
"inferrable. Try:\n\nexact_mod_cast ...\nshow " ++
to_fmt pty ++ ",\nfrom " ++ ptgt : format)
},
tactic.exact_mod_cast e
/--
Normalize the goal and the given expression,
then apply the expression to the goal.
-/
meta def apply_mod_cast (e : parse texpr) : tactic unit :=
do
e ← i_to_expr_for_apply e,
concat_tags $ tactic.apply_mod_cast e
/--
Normalize the goal and every expression in the local context,
then close the goal with assumption.
-/
meta def assumption_mod_cast : tactic unit :=
tactic.assumption_mod_cast
/-- `push_cast` rewrites the expression to move casts toward the leaf nodes.
For example, `↑(a + b)` will be written to `↑a + ↑b`.
Equivalent to `simp only with push_cast`.
Can also be used at hypotheses.
-/
meta def push_cast (l : parse location): tactic unit :=
tactic.interactive.simp none tt [] [`push_cast] l
end tactic.interactive
namespace conv.interactive
open conv tactic tactic.interactive interactive interactive.types
open norm_cast (derive)
meta def norm_cast : conv unit := replace_lhs derive
end conv.interactive
@[elim_cast] lemma ge_from_le {α} [has_le α] : ∀ (x y : α), x ≥ y ↔ y ≤ x := λ _ _, iff.rfl
@[elim_cast] lemma gt_from_lt {α} [has_lt α] : ∀ (x y : α), x > y ↔ y < x := λ _ _, iff.rfl
@[elim_cast] lemma ne_from_not_eq {α} : ∀ (x y : α), x ≠ y ↔ ¬(x = y) := λ _ _, iff.rfl
attribute [squash_cast] int.coe_nat_zero
attribute [squash_cast] int.coe_nat_one
attribute [elim_cast] int.nat_abs_of_nat
attribute [move_cast] int.coe_nat_succ
attribute [move_cast] int.coe_nat_add
attribute [move_cast] int.coe_nat_sub
attribute [move_cast] int.coe_nat_mul
@[move_cast] lemma ite_cast {α β : Type} [has_coe α β]
{c : Prop} [decidable c] {a b : α} :
↑(ite c a b) = ite c (↑a : β) (↑b : β) :=
by by_cases h : c; simp [h]
add_hint_tactic "norm_cast at *"
/--
The `norm_cast` family of tactics is used to normalize casts inside expressions.
It is basically a simp tactic with a specific set of lemmas to move casts
upwards in the expression.
Therefore it can be used more safely as a non-terminating tactic.
It also has special handling of numerals.
For instance, given an assumption
```lean
a b : ℤ
h : ↑a + ↑b < (10 : ℚ)
```
writing `norm_cast at h` will turn `h` into
```lean
h : a + b < 10
```
You can also use `exact_mod_cast`, `apply_mod_cast`, `rw_mod_cast`
or `assumption_mod_cast`.
Writing `exact_mod_cast h` and `apply_mod_cast h` will normalize the goal and h before using `exact h` or `apply h`.
Writing `assumption_mod_cast` will normalize the goal and for every
expression `h` in the context it will try to normalize `h` and use
`exact h`.
`rw_mod_cast` acts like the `rw` tactic but it applies `norm_cast` between steps.
`push_cast` rewrites the expression to move casts toward the leaf nodes.
This uses `move_cast` lemmas in the forward direction.
For example, `↑(a + b)` will be written to `↑a + ↑b`.
It is equivalent to `simp only with push_cast`, and can also be used at hypotheses
with `push_cast at h`.
-/
add_tactic_doc
{ name := "norm_cast",
category := doc_category.tactic,
decl_names := [`tactic.interactive.norm_cast, `tactic.interactive.rw_mod_cast,
`tactic.interactive.apply_mod_cast, `tactic.interactive.assumption_mod_cast,
`tactic.interactive.exact_mod_cast, `tactic.interactive.push_cast],
tags := ["coercions", "simplification"] }
/--
The `norm_cast` family of tactics works with three attributes,
`elim_cast`, `move_cast` and `squash_cast`.
`elim_cast` is for elimination lemmas of the shape
`Π ..., P ↑a1 ... ↑an = P a1 ... an`, for instance:
```lean
int.coe_nat_inj' : ∀ {m n : ℕ}, ↑m = ↑n ↔ m = n
rat.coe_int_denom : ∀ (n : ℤ), ↑n.denom = 1
```
`move_cast` is for compositional lemmas of the shape
`Π ..., ↑(P a1 ... an) = P ↑a1 ... ↑an`, for instance:
```lean
int.coe_nat_add : ∀ (m n : ℕ), ↑(m + n) = ↑m + ↑n`
nat.cast_sub : ∀ {α : Type*} [add_group α] [has_one α] {m n : ℕ}, m ≤ n → ↑(n - m) = ↑n - ↑m
```
`squash_cast` is for lemmas of the shape
`Π ..., ↑↑a = ↑a`, for instance:
```lean
int.cast_coe_nat : ∀ (n : ℕ), ↑↑n = ↑n
int.cats_id : int.cast_id : ∀ (n : ℤ), ↑n = n
```
-/
add_tactic_doc
{ name := "norm_cast attributes",
category := doc_category.attr,
decl_names := [`norm_cast.move_cast_attr, `norm_cast.elim_cast_attr, `norm_cast.squash_cast_attr],
tags := ["coercions", "simplification"] }
|
4074c6086b7ee2acb68f2a978e06c213cae0623e | b82c5bb4c3b618c23ba67764bc3e93f4999a1a39 | /src/formal_ml/classical_deriv.lean | 8079414b1b7103b898f7755832865724445aaaad | [
"Apache-2.0"
] | permissive | nouretienne/formal-ml | 83c4261016955bf9bcb55bd32b4f2621b44163e0 | 40b6da3b6e875f47412d50c7cd97936cb5091a2b | refs/heads/master | 1,671,216,448,724 | 1,600,472,285,000 | 1,600,472,285,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 17,241 | lean | /-
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-/
import order.filter.basic
import data.real.basic
import topology.basic
import topology.instances.real
import data.complex.exponential
import topology.algebra.infinite_sum
import data.nat.basic
import analysis.specific_limits
import analysis.calculus.deriv
import analysis.asymptotics
import formal_ml.sum
import formal_ml.real
--classical limit and derivative of a real function-------------------------------------------------
/-
The classical limit is dramatically different than the topological
limit. Specifically, the topological concept of limit assumes
f x = y. However, the concept of limit from analysis explicitly
excludes the point itself. This allows us to define derivatives in
terms of limits.
The nuance of the classical definition is that it requires some
creative interpretation of division by zero. However, we already
have to deal with this when it comes to fields, so avoiding it here
is kind of pointless.
Thus, note that the derivatives in mathlib are defined in terms
of Landau notation: i.e., that the difference between the function
and an affine function goes to zero in a (little o) way.
-/
def has_classical_rlimit (f:ℝ → ℝ) (x:ℝ) (y:ℝ):Prop :=
∀ ε:ℝ, (0 < ε)→
∃ δ:ℝ, (0 < δ)∧
∀ x':ℝ, (x'≠ x) → (abs (x-x') < δ) → abs (f x' - y)<ε
lemma has_classical_rlimit_def {f:ℝ → ℝ} {x:ℝ} {y:ℝ}:
(has_classical_rlimit f x y) ↔
(∀ ε:ℝ, (0 < ε)→
∃ δ:ℝ, (0 < δ)∧
∀ x':ℝ, (x'≠ x) → (abs (x-x') < δ) → abs (f x' - y)<ε) :=
begin
refl,
end
def has_classical_rderiv_at (f:ℝ → ℝ) (f':ℝ) (x:ℝ):Prop :=
has_classical_rlimit (λ x':ℝ, (f x' - f x)/(x' - x)) x f'
lemma has_classical_rderiv_at_defh (f:ℝ → ℝ) (f' x x':ℝ):
(x' ≠ x)→
abs (f x' + (-f x + -((x' + -x) * f'))) * (abs (x' - x))⁻¹=
abs ((f x' - f x) / (x' - x) - f') :=
begin
intro A1,
rw ← abs_inv,
rw ← abs_mul,
have B1:(f x' + (-f x + -((x' + -x) * f'))) * (x' - x)⁻¹ = (f x' - f x) / (x' - x) - f',
{
have A2:(f x' + (-f x + -((x' + -x) * f')))=(f x' -f x) + -((x' + -x) * f'),
{
rw ← add_assoc,
refl,
},
rw A2,
rw right_distrib,
have A3:-((x' + -x) * f') * (x' - x)⁻¹ = -f',
{
rw mul_comm (x' + -x) f',
simp,
rw mul_assoc,
rw sub_eq_add_neg,
rw mul_inv_cancel,
simp,
apply diff_ne_zero_of_ne,
apply A1,
},
rw A3,
simp,
rw div_def,
rw sub_eq_add_neg _ f',
},
rw B1,
end
lemma has_classical_rderiv_at_def (f:ℝ → ℝ) (f':ℝ) (x:ℝ):
has_classical_rderiv_at f f' x ↔
has_deriv_at f f' x :=
begin
unfold has_classical_rderiv_at,
unfold has_deriv_at,
unfold has_deriv_at_filter,
unfold has_fderiv_at_filter,
unfold asymptotics.is_o,
unfold has_classical_rlimit,
unfold asymptotics.is_O_with,
have B1:∀ x' x, (x' * f' - x * f') = ((x' + -x) * f'),
{
intros x' x,
rw ← mul_sub_right_distrib,
have B1A:(x' - x) = (x' + -x) := rfl,
rw B1A,
},
split,
{
intros A1 c A2,
have A3:=A1 c A2,
cases A3 with δ A4,
cases A4 with A5 A6,
simp,
apply @mem_nhds_intro_real _ x (x-δ) (x + δ),
{
apply x_in_Ioo A5,
},
{
rw set.subset_def,
intro x',
intro A7,
rw ← abs_lt_iff_in_Ioo2 at A7,
simp,
have A8:x' = x ∨ (x' ≠ x) := eq_or_ne,
cases A8,
{
rw A8,
simp,
},
{
have A9 := A6 x' A8 A7,
rw ← has_classical_rderiv_at_defh at A9,
rw ← abs_eq_norm,
rw ← abs_eq_norm,
apply move_le2,
apply abs_diff_pos,
apply A8,
apply le_of_lt,
rw sub_eq_add_neg,
rw sub_eq_add_neg,
rw ← add_assoc (f x') (-f x) at A9,
rw B1,
apply A9,
apply A8,
}
}
},
{
intros A1 ε A2,
have A3:0 < ε/2 := zero_lt_epsilon_half_of_zero_lt_epsilon A2,
have A4:=A1 A3,
rw filter.eventually_iff at A4,
-- This simp has caused issues. However, I don't know what
-- to do with a continuous_linear_map.
simp at A4,
have A5:∃ r:ℝ,∃ (H:r > 0), (set.Ioo (x-r) (x+r)) ⊆ {x_1 : ℝ | ∥f x_1 -f x -((x_1 + -x) * f')∥
≤ ε / 2 * ∥x_1 + -x∥},
{
/-
mem_nhds_elim_real_bound :
∀ (b : set ℝ) (x : ℝ), b ∈ nhds x → (∃ (r : ℝ) (H : r > 0), set.Ioo (x - r) (x + r) ⊆ b)
-/
--sorry,
apply @mem_nhds_elim_real_bound {x_1 : ℝ | ∥f x_1 - f x -((x_1 + -x) * f')∥
≤ ε / 2 * ∥x_1 + -x∥}x,
have B2:{x_1 : ℝ | ∥f x_1 - f x - (x_1 * f' - x * f')∥ ≤ ε / 2 * ∥x_1 - x∥} =
{x_1 : ℝ | ∥f x_1 - f x - (x_1 + -x) * f'∥ ≤ ε / 2 * ∥x_1 + -x∥},
{
ext x'',
simp,split;intro B2A,
{
rw ← B1 x'' x,
rw ← sub_eq_add_neg x'' x,
apply B2A,
},
{
rw ← B1 x'' x at B2A,
rw ← sub_eq_add_neg x'' x at B2A,
apply B2A,
},
},
rw ← B2,
apply A4,
},
cases A5 with r A6,
cases A6 with A7 A8,
apply exists.intro r,
split,
{
apply A7,
},
{
intros x' A9 A10,
rw abs_lt_iff_in_Ioo2 at A10,
rw set.subset_def at A8,
have A11 := A8 x' A10,
simp at A11,
rw ← abs_eq_norm at A11,
rw ← abs_eq_norm at A11,
have A12:abs (f x' - f x - ((x' + -x) * f')) * (abs (x' - x))⁻¹ ≤ ε /2,
{
apply move_le,
apply abs_diff_pos,
apply A9,
apply A11,
},
have A13:abs (f x' -f x -((x' + -x) * f')) * (abs (x' - x))⁻¹ < ε,
{
apply lt_of_le_of_lt,
apply A12,
apply epsilon_half_lt_epsilon,
apply A2,
},
rw ← has_classical_rderiv_at_defh,
rw sub_eq_add_neg at A13,
rw sub_eq_add_neg at A13,
rw add_assoc at A13,
apply A13,
apply A9,
}
}
end
lemma has_classical_rderiv_at_def2 (f:ℝ → ℝ) (f':ℝ) (x:ℝ):
has_classical_rderiv_at f f' x ↔ has_classical_rlimit (λ x':ℝ, (f x' - f x)/(x' - x)) x f' :=
begin
refl
end
lemma has_classical_rlimit_intro2 (f:ℝ → ℝ) (x:ℝ) (y:ℝ):(
∀ ε:ℝ, (0 < ε)→
∃ δ:ℝ, (0 < δ) ∧
∀ h:ℝ, (h≠ 0) → (abs(h) < δ) → abs (f (x + h) - y)<ε) → has_classical_rlimit f x y :=
begin
intro A1,
unfold has_classical_rlimit,
intros ε A2,
have A3:=A1 ε A2,
cases A3 with δ A4,
cases A4 with A5 A6,
apply exists.intro δ,
split,
{
apply A5,
},
{
intros x' A7 A8,
let h:= x' - x,
begin
have A9:h = x' - x := rfl,
have A10:h ≠ 0,
{
rw A9,
intro A10,
apply A7,
apply eq_of_sub_eq_zero A10,
},
have A11:abs h < δ,
{
rw A9,
rw abs_antisymm,
apply A8
},
have A12:= A6 h A10 A11,
have A13:x+h = x',
{
rw A9,
simp,
},
rw A13 at A12,
apply A12,
end
}
end
lemma add_cancel_left {x y z:ℝ}:x + y = x + z ↔ y = z :=
begin
split,
intros A1,
{
simp at A1,
apply A1,
},
{
simp,
}
end
lemma add_cancel_right {x y z:ℝ}:y + x = z + x ↔ y = z :=
begin
split,
intros A1,
{
simp at A1,
apply A1,
},
{
simp,
}
end
lemma has_classical_rlimit_offset {f:ℝ → ℝ} {x y k:ℝ}:
has_classical_rlimit f x y → has_classical_rlimit (f∘ (λ h,k + h)) (x - k) y :=
begin
intro A1,
apply has_classical_rlimit_intro2,
intros ε A2,
unfold has_classical_rlimit at A1,
have A3:= A1 ε A2,
cases A3 with δ A4,
cases A4 with A5 A6,
apply exists.intro δ,
split,
{
apply A5,
},
{
intros h A7 A8,
simp,
rw sub_eq_add_neg _ k,
rw add_assoc x (-k) h,
rw add_comm (-k) h,
rw ← add_assoc x h (-k),
rw add_comm (x + h) (-k),
rw ← add_assoc (k) (-k) (x + h),
rw add_right_neg,
rw zero_add,
let x' := x + h,
begin
have A9:x' = x + h := rfl,
rw ← A9,
have A13:h = x' - x,
{
rw ← @add_cancel_right (x),
simp,
},
apply A6,
{
intro A11,
apply A7,
rw A11 at A13,
simp at A13,
apply A13,
},
{
rw abs_antisymm,
rw ← A13,
apply A8,
}
end
}
end
lemma has_classical_rlimit_offset2 {f:ℝ → ℝ} {x y k:ℝ}:
has_classical_rlimit f x y ↔ has_classical_rlimit (f∘ (λ h,k + h)) (x - k) y :=
begin
split;intros A1,
{
apply has_classical_rlimit_offset A1,
},
{
have A2:f = (f ∘ λ (h : ℝ), k + h) ∘ (λ (h : ℝ), (-k) + h),
{
ext n,
simp,
},
have A3:(x - k) - (-k) = x,
{
simp,
},
rw ← A3,
rw A2,
apply @has_classical_rlimit_offset (f ∘ λ (h : ℝ), k + h) (x - k) y (-k),
apply A1,
}
end
lemma has_classical_rlimit_def2 {f:ℝ → ℝ} {x:ℝ} {y:ℝ}:
has_classical_rlimit f x y ↔ has_classical_rlimit (f∘ (λ h,x + h)) 0 y :=
begin
rw (@has_classical_rlimit_offset2 f x y x),
simp,
end
lemma has_classical_rlimit_subst {f g:ℝ → ℝ} {x:ℝ} {y:ℝ}:
(∀ x'≠ x, f x' = g x') →
has_classical_rlimit f x y → has_classical_rlimit g x y :=
begin
intros A1 A2,
rw has_classical_rlimit_def,
intros ε A3,
rw has_classical_rlimit_def at A2,
have A4 := A2 ε A3,
cases A4 with δ A5,
cases A5 with A6 A7,
apply exists.intro δ,
split,
{
apply A6,
},
{
intros x' A8 A9,
have A10 := A1 x' A8,
rw ← A10,
apply A7,
apply A8,
apply A9,
}
end
lemma has_classical_rderiv_at_def3 (f:ℝ → ℝ) (f':ℝ) (x:ℝ):
has_classical_rderiv_at f f' x ↔ has_classical_rlimit (λ h:ℝ, (f (x + h) - f x)/h) 0 f' :=
begin
rw has_classical_rderiv_at_def2,
rw has_classical_rlimit_def2,
have A1:((λ (x' : ℝ), (f x' - f x) / (x' - x)) ∘ λ (h : ℝ), x + h)=
(λ (h : ℝ), (f (x + h) - f x) / h),
{
ext x',
simp,
},
rw A1,
end
/- TODO: rewrite this using has_classical_rderiv_at_def3 -/
lemma has_classical_rderiv_at_intro2 (f:ℝ → ℝ) (f':ℝ) (x:ℝ):(
∀ ε:ℝ, (0 < ε)→
∃ δ:ℝ, (0 < δ) ∧
∀ h:ℝ, (h≠ 0) → (abs(h) < δ) → abs ((f (x + h) - f x) / h - f')<ε) → has_classical_rderiv_at f f' x :=
begin
intro A1,
unfold has_classical_rderiv_at,
apply has_classical_rlimit_intro2,
intros ε A3,
have A4:=A1 ε A3,
cases A4 with δ A5,
cases A5 with A6 A7,
apply exists.intro δ,
split,
{
apply A6,
},
{
intros h A8 A9,
have A10:x + h -x = h,
{
simp,
},
rw A10,
have A11 := A7 h A8 A9,
apply A11,
},
end
/-
real.is_topological_basis_Ioo_rat :
topological_space.is_topological_basis (⋃ (a b : ℚ) (h : a < b), {set.Ioo ↑a ↑b})
-/
lemma center_in_ball {x:ℝ} {r:ℝ}:(0 < r) → (x∈ set.Ioo (x-r) (x + r)) :=
begin
intro A1,
rw ← abs_lt_iff_in_Ioo,
simp,
apply A1,
end
lemma ball_subset_Ioo {a b:ℝ} {x:ℝ}:
x ∈ set.Ioo a b →
∃ r>0, set.Ioo (x - r) (x + r) ⊆ set.Ioo a b :=
begin
intro A1,
let r := min (x-a) (b-x),
begin
apply exists.intro r,
simp at A1,
cases A1 with A2 A3,
have A4:0 < x - a := sub_pos_of_lt A2,
have A5:0 < b - x := sub_pos_of_lt A3,
have A6:0 < r := lt_min A4 A5,
split,
{
apply A6,
},
{
rw set.Ioo_subset_Ioo_iff,
{
split,
{
apply le_sub_left_of_add_le,
apply add_le_of_le_sub_right,
apply min_le_left,
},
{
rw add_comm,
apply add_le_of_le_sub_right,
apply min_le_right,
}
},
{
rw ← set.nonempty_Ioo,
have B1:x ∈ set.Ioo (x - r) (x + r) := center_in_ball A6,
apply set.nonempty_of_mem,
apply B1,
}
}
end
end
lemma is_open_iff_ball_subset' {V:set ℝ}:
is_open V ↔ (∀ x∈ V, ∃ ε>0, set.Ioo (x - ε) (x+ ε )⊆ V) :=
begin
have A0 := real.is_topological_basis_Ioo_rat,
split;intros A1,
{
intros x A2,
have A3 := topological_space.mem_basis_subset_of_mem_open A0 A2 A1,
cases A3 with V2 A4,
cases A4 with A5 A6,
cases A6 with A7 A8,
simp at A5,
cases A5 with a A9,
cases A9 with b A10,
cases A10 with A11 A12,
subst V2,
have A13 := ball_subset_Ioo A7,
cases A13 with r A14,
cases A14 with A15 A16,
apply exists.intro r,
split,
{
apply A15,
},
{
apply set.subset.trans A16 A8,
}
},
{
rw is_open_iff_forall_mem_open,
intros x A2,
have A3 := A1 x A2,
cases A3 with ε A4,
cases A4 with A5 A6,
apply exists.intro (set.Ioo (x - ε) (x + ε)),
split,
{
apply A6,
},
split,
{
apply @is_open_Ioo ℝ _ _ _ (x -ε) (x + ε),
},
{
apply center_in_ball A5,
},
}
end
lemma continuous_iff_has_classical_rlimit {f:ℝ → ℝ}:
(∀ x:ℝ, has_classical_rlimit f x (f x)) ↔ continuous f :=
begin
split;intros A1,
{
intros V A2,
rw is_open_iff_ball_subset',
intros x A3,
have A4 := A1 x,
unfold has_classical_rlimit at A4,
rw is_open_iff_ball_subset' at A2,
have A5 := A2 (f x) A3,
cases A5 with ε A6,
cases A6 with A7 A8,
have A9 := A4 ε A7,
cases A9 with δ A10,
cases A10 with A11 A12,
apply exists.intro δ,
split,
{
apply A11,
},
{
rw set.subset_def,
intros x' A13,
rw ← abs_lt_iff_in_Ioo at A13,
rw abs_antisymm at A13,
have A14:(x'=x)∨ (x'≠ x) := eq_or_ne,
cases A14,
{
subst x',
apply A3,
},
{
have A15 := A12 x' A14 A13,
simp,
apply A8,
rw ← abs_lt_iff_in_Ioo,
apply A15,
}
}
},
{
intro x,
unfold continuous at A1,
unfold has_classical_rlimit,
intros ε A2,
have A3:is_open (set.Ioo (f x - ε) (f x + ε)),
{
apply @is_open_Ioo ℝ _ _ _ (f x -ε) (f x + ε),
},
have A4 := A1 (set.Ioo (f x - ε) (f x + ε)) A3,
have A5 :x ∈ f⁻¹' (set.Ioo (f x - ε) (f x + ε)),
{
simp,
split,
{
apply sub_lt_of_sub_lt,
rw sub_self,
apply A2,
},
{
apply A2,
}
},
rw is_open_iff_ball_subset' at A4,
have A6 := A4 x A5,
cases A6 with δ A7,
cases A7 with A8 A9,
apply exists.intro δ,
split,
apply A8,
intros x' A10 A11,
rw abs_lt_iff_in_Ioo,
apply A9,
rw abs_antisymm at A11,
rw abs_lt_iff_in_Ioo at A11,
apply A11,
}
end
lemma classical_rlimit_continuous_def {f:ℝ → ℝ} {x:ℝ}:
(has_classical_rlimit f x (f x)) ↔ (∀ ε > 0, ∃ δ > 0, ∀ x', (abs (x' - x) < δ → abs (f x' - f x) < ε)) :=
begin
split;intros A1,
{
intros ε A2,
unfold has_classical_rlimit at A1,
have A3 := A1 ε A2,
cases A3 with δ A4,
cases A4 with A5 A6,
apply exists.intro δ,
split,
{
apply A5,
},
{
intros x' A7,
have A8:x' = x ∨ (x' ≠ x) := eq_or_ne,
cases A8,
{
subst x',
simp,
apply A2,
},
{
have A9 := A6 x' A8,
apply A9,
rw abs_antisymm,
apply A7,
}
}
},
{
unfold has_classical_rlimit,
intros ε A2,
have A3 := A1 ε A2,
cases A3 with δ A4,
cases A4 with A5 A6,
apply exists.intro δ,
split,
{
apply A5,
},
{
intros x' A7 A8,
rw abs_antisymm at A8,
apply A6 x' A8,
}
}
end
lemma classical_rlimit_continuous_all_def {f:ℝ → ℝ}:
(∀ x, (has_classical_rlimit f x (f x))) ↔
(∀ x, (∀ ε > 0, ∃ δ > 0, ∀ x', (abs (x' - x) < δ → abs (f x' - f x) < ε))) :=
begin
split;intros A1,
{
intro x,
have A2 := A1 x,
rw classical_rlimit_continuous_def at A2,
apply A2,
},
{
intro x,
have A2 := A1 x,
rw ← classical_rlimit_continuous_def at A2,
apply A2,
},
end
lemma continuous_def {f:ℝ → ℝ}:
continuous f ↔ (∀ x, ∀ ε > 0, ∃ δ > 0, ∀ x', (abs (x' - x) < δ → abs (f x' - f x) < ε)) :=
begin
rw ← continuous_iff_has_classical_rlimit,
rw classical_rlimit_continuous_all_def,
end
lemma continuous_elim {f:ℝ → ℝ} (x:ℝ):
continuous f → (∀ ε > 0, ∃ δ > 0, ∀ x', (abs (x' - x) < δ → abs (f x' - f x) < ε)) :=
begin
intro A1,
rw continuous_def at A1,
apply (A1 x),
end
|
6fe498fda688e50eb0a360ee869c9a8b8d34ee8d | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Compiler/LCNF/ForEachExpr.lean | 79da9c1247d717bfd3fb54dac39829077e284d34 | [
"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 | 295 | lean | /-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Util.ForEachExpr
import Lean.Compiler.LCNF.Basic
namespace Lean.Compiler.LCNF
-- TODO: delete
end Lean.Compiler.LCNF |
dc480b11d5a133ac74c79cab9f6f35ae5c6d5359 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/algebra/algebra/spectrum.lean | 2a005b72ac92f3ba6270675d0897d680b53cd7ee | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 19,383 | lean | /-
Copyright (c) 2021 Jireh Loreaux. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jireh Loreaux
-/
import tactic.noncomm_ring
import field_theory.is_alg_closed.basic
import algebra.star.pointwise
/-!
# Spectrum of an element in an algebra
This file develops the basic theory of the spectrum of an element of an algebra.
This theory will serve as the foundation for spectral theory in Banach algebras.
## Main definitions
* `resolvent_set a : set R`: the resolvent set of an element `a : A` where
`A` is an `R`-algebra.
* `spectrum a : set R`: the spectrum of an element `a : A` where
`A` is an `R`-algebra.
* `resolvent : R → A`: the resolvent function is `λ r, ring.inverse (↑ₐr - a)`, and hence
when `r ∈ resolvent R A`, it is actually the inverse of the unit `(↑ₐr - a)`.
## Main statements
* `spectrum.unit_smul_eq_smul` and `spectrum.smul_eq_smul`: units in the scalar ring commute
(multiplication) with the spectrum, and over a field even `0` commutes with the spectrum.
* `spectrum.left_add_coset_eq`: elements of the scalar ring commute (addition) with the spectrum.
* `spectrum.unit_mem_mul_iff_mem_swap_mul` and `spectrum.preimage_units_mul_eq_swap_mul`: the
units (of `R`) in `σ (a*b)` coincide with those in `σ (b*a)`.
* `spectrum.scalar_eq`: in a nontrivial algebra over a field, the spectrum of a scalar is
a singleton.
* `spectrum.subset_polynomial_aeval`, `spectrum.map_polynomial_aeval_of_degree_pos`,
`spectrum.map_polynomial_aeval_of_nonempty`: variations on the spectral mapping theorem.
## Notations
* `σ a` : `spectrum R a` of `a : A`
-/
open set
universes u v
section defs
variables (R : Type u) {A : Type v}
variables [comm_semiring R] [ring A] [algebra R A]
local notation `↑ₐ` := algebra_map R A
-- definition and basic properties
/-- Given a commutative ring `R` and an `R`-algebra `A`, the *resolvent set* of `a : A`
is the `set R` consisting of those `r : R` for which `r•1 - a` is a unit of the
algebra `A`. -/
def resolvent_set (a : A) : set R :=
{ r : R | is_unit (↑ₐr - a) }
/-- Given a commutative ring `R` and an `R`-algebra `A`, the *spectrum* of `a : A`
is the `set R` consisting of those `r : R` for which `r•1 - a` is not a unit of the
algebra `A`.
The spectrum is simply the complement of the resolvent set. -/
def spectrum (a : A) : set R :=
(resolvent_set R a)ᶜ
variable {R}
/-- Given an `a : A` where `A` is an `R`-algebra, the *resolvent* is
a map `R → A` which sends `r : R` to `(algebra_map R A r - a)⁻¹` when
`r ∈ resolvent R A` and `0` when `r ∈ spectrum R A`. -/
noncomputable def resolvent (a : A) (r : R) : A :=
ring.inverse (↑ₐr - a)
/-- The unit `1 - r⁻¹ • a` constructed from `r • 1 - a` when the latter is a unit. -/
@[simps]
noncomputable def is_unit.sub_inv_smul {r : Rˣ} {s : R} {a : A}
(h : is_unit $ r • ↑ₐs - a) : Aˣ :=
{ val := ↑ₐs - r⁻¹ • a,
inv := r • ↑h.unit⁻¹,
val_inv := by rw [mul_smul_comm, ←smul_mul_assoc, smul_sub, smul_inv_smul, h.mul_coe_inv],
inv_val := by rw [smul_mul_assoc, ←mul_smul_comm, smul_sub, smul_inv_smul, h.coe_inv_mul], }
end defs
namespace spectrum
open_locale polynomial
section scalar_semiring
variables {R : Type u} {A : Type v}
variables [comm_semiring R] [ring A] [algebra R A]
local notation `σ` := spectrum R
local notation `↑ₐ` := algebra_map R A
lemma mem_iff {r : R} {a : A} :
r ∈ σ a ↔ ¬ is_unit (↑ₐr - a) :=
iff.rfl
lemma not_mem_iff {r : R} {a : A} :
r ∉ σ a ↔ is_unit (↑ₐr - a) :=
by { apply not_iff_not.mp, simp [set.not_not_mem, mem_iff] }
lemma mem_resolvent_set_of_left_right_inverse {r : R} {a b c : A}
(h₁ : (↑ₐr - a) * b = 1) (h₂ : c * (↑ₐr - a) = 1) :
r ∈ resolvent_set R a :=
units.is_unit ⟨↑ₐr - a, b, h₁, by rwa ←left_inv_eq_right_inv h₂ h₁⟩
lemma mem_resolvent_set_iff {r : R} {a : A} :
r ∈ resolvent_set R a ↔ is_unit (↑ₐr - a) :=
iff.rfl
@[simp] lemma resolvent_set_of_subsingleton [subsingleton A] (a : A) :
resolvent_set R a = set.univ :=
by simp_rw [resolvent_set, subsingleton.elim (algebra_map R A _ - a) 1, is_unit_one,
set.set_of_true]
@[simp] lemma of_subsingleton [subsingleton A] (a : A) :
spectrum R a = ∅ :=
by rw [spectrum, resolvent_set_of_subsingleton, set.compl_univ]
lemma resolvent_eq {a : A} {r : R} (h : r ∈ resolvent_set R a) :
resolvent a r = ↑h.unit⁻¹ :=
ring.inverse_unit h.unit
lemma units_smul_resolvent {r : Rˣ} {s : R} {a : A} :
r • resolvent a (s : R) = resolvent (r⁻¹ • a) (r⁻¹ • s : R) :=
begin
by_cases h : s ∈ spectrum R a,
{ rw [mem_iff] at h,
simp only [resolvent, algebra.algebra_map_eq_smul_one] at *,
rw [smul_assoc, ←smul_sub],
have h' : ¬ is_unit (r⁻¹ • (s • 1 - a)),
from λ hu, h (by simpa only [smul_inv_smul] using is_unit.smul r hu),
simp only [ring.inverse_non_unit _ h, ring.inverse_non_unit _ h', smul_zero] },
{ simp only [resolvent],
have h' : is_unit (r • (algebra_map R A (r⁻¹ • s)) - a),
{ simpa [algebra.algebra_map_eq_smul_one, smul_assoc] using not_mem_iff.mp h },
rw [←h'.coe_sub_inv_smul, ←(not_mem_iff.mp h).unit_spec, ring.inverse_unit, ring.inverse_unit,
h'.coe_inv_sub_inv_smul],
simp only [algebra.algebra_map_eq_smul_one, smul_assoc, smul_inv_smul], },
end
lemma units_smul_resolvent_self {r : Rˣ} {a : A} :
r • resolvent a (r : R) = resolvent (r⁻¹ • a) (1 : R) :=
by simpa only [units.smul_def, algebra.id.smul_eq_mul, units.inv_mul]
using @units_smul_resolvent _ _ _ _ _ r r a
/-- The resolvent is a unit when the argument is in the resolvent set. -/
lemma is_unit_resolvent {r : R} {a : A} :
r ∈ resolvent_set R a ↔ is_unit (resolvent a r) :=
is_unit_ring_inverse.symm
lemma inv_mem_resolvent_set {r : Rˣ} {a : Aˣ} (h : (r : R) ∈ resolvent_set R (a : A)) :
(↑r⁻¹ : R) ∈ resolvent_set R (↑a⁻¹ : A) :=
begin
rw [mem_resolvent_set_iff, algebra.algebra_map_eq_smul_one, ←units.smul_def] at h ⊢,
rw [is_unit.smul_sub_iff_sub_inv_smul, inv_inv, is_unit.sub_iff],
have h₁ : (a : A) * (r • (↑a⁻¹ : A) - 1) = r • 1 - a,
{ rw [mul_sub, mul_smul_comm, a.mul_inv, mul_one], },
have h₂ : (r • (↑a⁻¹ : A) - 1) * a = r • 1 - a,
{ rw [sub_mul, smul_mul_assoc, a.inv_mul, one_mul], },
have hcomm : commute (a : A) (r • (↑a⁻¹ : A) - 1), { rwa ←h₂ at h₁ },
exact (hcomm.is_unit_mul_iff.mp (h₁.symm ▸ h)).2,
end
lemma inv_mem_iff {r : Rˣ} {a : Aˣ} :
(r : R) ∈ σ (a : A) ↔ (↑r⁻¹ : R) ∈ σ (↑a⁻¹ : A) :=
begin
simp only [mem_iff, not_iff_not, ←mem_resolvent_set_iff],
exact ⟨λ h, inv_mem_resolvent_set h, λ h, by simpa using inv_mem_resolvent_set h⟩,
end
lemma zero_mem_resolvent_set_of_unit (a : Aˣ) : 0 ∈ resolvent_set R (a : A) :=
by { rw [mem_resolvent_set_iff, is_unit.sub_iff], simp }
lemma ne_zero_of_mem_of_unit {a : Aˣ} {r : R} (hr : r ∈ σ (a : A)) : r ≠ 0 :=
λ hn, (hn ▸ hr) (zero_mem_resolvent_set_of_unit a)
lemma add_mem_iff {a : A} {r s : R} :
r ∈ σ a ↔ r + s ∈ σ (↑ₐs + a) :=
begin
apply not_iff_not.mpr,
simp only [mem_resolvent_set_iff],
have h_eq : ↑ₐ(r + s) - (↑ₐs + a) = ↑ₐr - a,
{ simp, noncomm_ring },
rw h_eq,
end
lemma smul_mem_smul_iff {a : A} {s : R} {r : Rˣ} :
r • s ∈ σ (r • a) ↔ s ∈ σ a :=
begin
apply not_iff_not.mpr,
simp only [mem_resolvent_set_iff, algebra.algebra_map_eq_smul_one],
have h_eq : (r • s) • (1 : A) = r • s • 1, by simp,
rw [h_eq, ←smul_sub, is_unit_smul_iff],
end
open_locale pointwise polynomial
theorem unit_smul_eq_smul (a : A) (r : Rˣ) :
σ (r • a) = r • σ a :=
begin
ext,
have x_eq : x = r • r⁻¹ • x, by simp,
nth_rewrite 0 x_eq,
rw smul_mem_smul_iff,
split,
{ exact λ h, ⟨r⁻¹ • x, ⟨h, by simp⟩⟩},
{ rintros ⟨_, _, x'_eq⟩, simpa [←x'_eq],}
end
-- `r ∈ σ(a*b) ↔ r ∈ σ(b*a)` for any `r : Rˣ`
theorem unit_mem_mul_iff_mem_swap_mul {a b : A} {r : Rˣ} :
↑r ∈ σ (a * b) ↔ ↑r ∈ σ (b * a) :=
begin
apply not_iff_not.mpr,
simp only [mem_resolvent_set_iff, algebra.algebra_map_eq_smul_one],
have coe_smul_eq : ↑r • 1 = r • (1 : A), from rfl,
rw coe_smul_eq,
simp only [is_unit.smul_sub_iff_sub_inv_smul],
have right_inv_of_swap : ∀ {x y z : A} (h : (1 - x * y) * z = 1),
(1 - y * x) * (1 + y * z * x) = 1, from λ x y z h,
calc (1 - y * x) * (1 + y * z * x) = 1 - y * x + y * ((1 - x * y) * z) * x : by noncomm_ring
... = 1 : by simp [h],
have left_inv_of_swap : ∀ {x y z : A} (h : z * (1 - x * y) = 1),
(1 + y * z * x) * (1 - y * x) = 1, from λ x y z h,
calc (1 + y * z * x) * (1 - y * x) = 1 - y * x + y * (z * (1 - x * y)) * x : by noncomm_ring
... = 1 : by simp [h],
have is_unit_one_sub_mul_of_swap : ∀ {x y : A} (h : is_unit (1 - x * y)),
is_unit (1 - y * x), from λ x y h, by
{ let h₁ := right_inv_of_swap h.unit.val_inv,
let h₂ := left_inv_of_swap h.unit.inv_val,
exact ⟨⟨1 - y * x, 1 + y * h.unit.inv * x, h₁, h₂⟩, rfl⟩, },
have is_unit_one_sub_mul_iff_swap : ∀ {x y : A},
is_unit (1 - x * y) ↔ is_unit (1 - y * x), by
{ intros, split, repeat {apply is_unit_one_sub_mul_of_swap}, },
rw [←smul_mul_assoc, ←mul_smul_comm r⁻¹ b a, is_unit_one_sub_mul_iff_swap],
end
theorem preimage_units_mul_eq_swap_mul {a b : A} :
(coe : Rˣ → R) ⁻¹' σ (a * b) = coe ⁻¹' σ (b * a) :=
by { ext, exact unit_mem_mul_iff_mem_swap_mul, }
section star
variables [has_involutive_star R] [star_ring A] [star_module R A]
lemma star_mem_resolvent_set_iff {r : R} {a : A} :
star r ∈ resolvent_set R a ↔ r ∈ resolvent_set R (star a) :=
by refine ⟨λ h, _, λ h, _⟩;
simpa only [mem_resolvent_set_iff, algebra.algebra_map_eq_smul_one, star_sub, star_smul,
star_star, star_one] using is_unit.star h
protected lemma map_star (a : A) : σ (star a) = star (σ a) :=
by { ext, simpa only [set.mem_star, mem_iff, not_iff_not] using star_mem_resolvent_set_iff.symm }
end star
end scalar_semiring
section scalar_ring
variables {R : Type u} {A : Type v}
variables [comm_ring R] [ring A] [algebra R A]
local notation `σ` := spectrum R
local notation `↑ₐ` := algebra_map R A
theorem left_add_coset_eq (a : A) (r : R) :
left_add_coset r (σ a) = σ (↑ₐr + a) :=
by { ext, rw [mem_left_add_coset_iff, neg_add_eq_sub, add_mem_iff],
nth_rewrite 1 ←sub_add_cancel x r, }
open polynomial
lemma exists_mem_of_not_is_unit_aeval_prod [is_domain R] {p : R[X]} {a : A} (hp : p ≠ 0)
(h : ¬is_unit (aeval a (multiset.map (λ (x : R), X - C x) p.roots).prod)) :
∃ k : R, k ∈ σ a ∧ eval k p = 0 :=
begin
rw [←multiset.prod_to_list, alg_hom.map_list_prod] at h,
replace h := mt list.prod_is_unit h,
simp only [not_forall, exists_prop, aeval_C, multiset.mem_to_list,
list.mem_map, aeval_X, exists_exists_and_eq_and, multiset.mem_map, alg_hom.map_sub] at h,
rcases h with ⟨r, r_mem, r_nu⟩,
exact ⟨r, by rwa [mem_iff, ←is_unit.sub_iff], by rwa [←is_root.def, ←mem_roots hp]⟩
end
end scalar_ring
section scalar_field
variables {𝕜 : Type u} {A : Type v}
variables [field 𝕜] [ring A] [algebra 𝕜 A]
local notation `σ` := spectrum 𝕜
local notation `↑ₐ` := algebra_map 𝕜 A
/-- Without the assumption `nontrivial A`, then `0 : A` would be invertible. -/
@[simp] lemma zero_eq [nontrivial A] : σ (0 : A) = {0} :=
begin
refine set.subset.antisymm _ (by simp [algebra.algebra_map_eq_smul_one, mem_iff]),
rw [spectrum, set.compl_subset_comm],
intros k hk,
rw set.mem_compl_singleton_iff at hk,
have : is_unit (units.mk0 k hk • (1 : A)) := is_unit.smul (units.mk0 k hk) is_unit_one,
simpa [mem_resolvent_set_iff, algebra.algebra_map_eq_smul_one]
end
@[simp] theorem scalar_eq [nontrivial A] (k : 𝕜) : σ (↑ₐk) = {k} :=
begin
have coset_eq : left_add_coset k {0} = {k}, by
{ ext, split,
{ intro hx, simp [left_add_coset] at hx, exact hx, },
{ intro hx, simp at hx, exact ⟨0, ⟨set.mem_singleton 0, by simp [hx]⟩⟩, }, },
calc σ (↑ₐk) = σ (↑ₐk + 0) : by simp
... = left_add_coset k (σ (0 : A)) : by rw ←left_add_coset_eq
... = left_add_coset k {0} : by rw zero_eq
... = {k} : coset_eq,
end
@[simp] lemma one_eq [nontrivial A] : σ (1 : A) = {1} :=
calc σ (1 : A) = σ (↑ₐ1) : by simp [algebra.algebra_map_eq_smul_one]
... = {1} : scalar_eq 1
open_locale pointwise
/-- the assumption `(σ a).nonempty` is necessary and cannot be removed without
further conditions on the algebra `A` and scalar field `𝕜`. -/
theorem smul_eq_smul [nontrivial A] (k : 𝕜) (a : A) (ha : (σ a).nonempty) :
σ (k • a) = k • (σ a) :=
begin
rcases eq_or_ne k 0 with rfl | h,
{ simpa [ha, zero_smul_set] },
{ exact unit_smul_eq_smul a (units.mk0 k h) },
end
theorem nonzero_mul_eq_swap_mul (a b : A) : σ (a * b) \ {0} = σ (b * a) \ {0} :=
begin
suffices h : ∀ (x y : A), σ (x * y) \ {0} ⊆ σ (y * x) \ {0},
{ exact set.eq_of_subset_of_subset (h a b) (h b a) },
{ rintros _ _ k ⟨k_mem, k_neq⟩,
change k with ↑(units.mk0 k k_neq) at k_mem,
exact ⟨unit_mem_mul_iff_mem_swap_mul.mp k_mem, k_neq⟩ },
end
protected lemma map_inv (a : Aˣ) : (σ (a : A))⁻¹ = σ (↑a⁻¹ : A) :=
begin
refine set.eq_of_subset_of_subset (λ k hk, _) (λ k hk, _),
{ rw set.mem_inv at hk,
have : k ≠ 0,
{ simpa only [inv_inv] using inv_ne_zero (ne_zero_of_mem_of_unit hk), },
lift k to 𝕜ˣ using is_unit_iff_ne_zero.mpr this,
rw ←units.coe_inv k at hk,
exact inv_mem_iff.mp hk },
{ lift k to 𝕜ˣ using is_unit_iff_ne_zero.mpr (ne_zero_of_mem_of_unit hk),
simpa only [units.coe_inv] using inv_mem_iff.mp hk, }
end
open polynomial
/-- Half of the spectral mapping theorem for polynomials. We prove it separately
because it holds over any field, whereas `spectrum.map_polynomial_aeval_of_degree_pos` and
`spectrum.map_polynomial_aeval_of_nonempty` need the field to be algebraically closed. -/
theorem subset_polynomial_aeval (a : A) (p : 𝕜[X]) :
(λ k, eval k p) '' (σ a) ⊆ σ (aeval a p) :=
begin
rintros _ ⟨k, hk, rfl⟩,
let q := C (eval k p) - p,
have hroot : is_root q k, by simp only [eval_C, eval_sub, sub_self, is_root.def],
rw [←mul_div_eq_iff_is_root, ←neg_mul_neg, neg_sub] at hroot,
have aeval_q_eq : ↑ₐ(eval k p) - aeval a p = aeval a q,
by simp only [aeval_C, alg_hom.map_sub, sub_left_inj],
rw [mem_iff, aeval_q_eq, ←hroot, aeval_mul],
have hcomm := (commute.all (C k - X) (- (q / (X - C k)))).map (aeval a),
apply mt (λ h, (hcomm.is_unit_mul_iff.mp h).1),
simpa only [aeval_X, aeval_C, alg_hom.map_sub] using hk,
end
/-- The *spectral mapping theorem* for polynomials. Note: the assumption `degree p > 0`
is necessary in case `σ a = ∅`, for then the left-hand side is `∅` and the right-hand side,
assuming `[nontrivial A]`, is `{k}` where `p = polynomial.C k`. -/
theorem map_polynomial_aeval_of_degree_pos [is_alg_closed 𝕜] (a : A) (p : 𝕜[X])
(hdeg : 0 < degree p) : σ (aeval a p) = (λ k, eval k p) '' (σ a) :=
begin
/- handle the easy direction via `spectrum.subset_polynomial_aeval` -/
refine set.eq_of_subset_of_subset (λ k hk, _) (subset_polynomial_aeval a p),
/- write `C k - p` product of linear factors and a constant; show `C k - p ≠ 0`. -/
have hprod := eq_prod_roots_of_splits_id (is_alg_closed.splits (C k - p)),
have h_ne : C k - p ≠ 0, from ne_zero_of_degree_gt
(by rwa [degree_sub_eq_right_of_degree_lt (lt_of_le_of_lt degree_C_le hdeg)]),
have lead_ne := leading_coeff_ne_zero.mpr h_ne,
have lead_unit := (units.map (↑ₐ).to_monoid_hom (units.mk0 _ lead_ne)).is_unit,
/- leading coefficient is a unit so product of linear factors is not a unit;
apply `exists_mem_of_not_is_unit_aeval_prod`. -/
have p_a_eq : aeval a (C k - p) = ↑ₐk - aeval a p,
by simp only [aeval_C, alg_hom.map_sub, sub_left_inj],
rw [mem_iff, ←p_a_eq, hprod, aeval_mul,
((commute.all _ _).map (aeval a)).is_unit_mul_iff, aeval_C] at hk,
replace hk := exists_mem_of_not_is_unit_aeval_prod h_ne (not_and.mp hk lead_unit),
rcases hk with ⟨r, r_mem, r_ev⟩,
exact ⟨r, r_mem, symm (by simpa [eval_sub, eval_C, sub_eq_zero] using r_ev)⟩,
end
/-- In this version of the spectral mapping theorem, we assume the spectrum
is nonempty instead of assuming the degree of the polynomial is positive. Note: the
assumption `[nontrivial A]` is necessary for the same reason as in `spectrum.zero_eq`. -/
theorem map_polynomial_aeval_of_nonempty [is_alg_closed 𝕜] [nontrivial A] (a : A) (p : 𝕜[X])
(hnon : (σ a).nonempty) : σ (aeval a p) = (λ k, eval k p) '' (σ a) :=
begin
refine or.elim (le_or_gt (degree p) 0) (λ h, _) (map_polynomial_aeval_of_degree_pos a p),
{ rw eq_C_of_degree_le_zero h,
simp only [set.image_congr, eval_C, aeval_C, scalar_eq, set.nonempty.image_const hnon] },
end
variable (𝕜)
/--
Every element `a` in a nontrivial finite-dimensional algebra `A`
over an algebraically closed field `𝕜` has non-empty spectrum. -/
-- We will use this both to show eigenvalues exist, and to prove Schur's lemma.
lemma nonempty_of_is_alg_closed_of_finite_dimensional [is_alg_closed 𝕜]
[nontrivial A] [I : finite_dimensional 𝕜 A] (a : A) :
∃ k : 𝕜, k ∈ σ a :=
begin
obtain ⟨p, ⟨h_mon, h_eval_p⟩⟩ := is_integral_of_noetherian (is_noetherian.iff_fg.2 I) a,
have nu : ¬ is_unit (aeval a p), { rw [←aeval_def] at h_eval_p, rw h_eval_p, simp, },
rw [eq_prod_roots_of_monic_of_splits_id h_mon (is_alg_closed.splits p)] at nu,
obtain ⟨k, hk, _⟩ := exists_mem_of_not_is_unit_aeval_prod (monic.ne_zero h_mon) nu,
exact ⟨k, hk⟩
end
end scalar_field
end spectrum
namespace alg_hom
section comm_semiring
variables {F R A B : Type*} [comm_ring R] [ring A] [algebra R A] [ring B] [algebra R B]
variables [alg_hom_class F R A B]
local notation `σ` := spectrum R
local notation `↑ₐ` := algebra_map R A
lemma mem_resolvent_set_apply (φ : F) {a : A} {r : R} (h : r ∈ resolvent_set R a) :
r ∈ resolvent_set R ((φ : A → B) a) :=
by simpa only [map_sub, alg_hom_class.commutes] using h.map φ
lemma spectrum_apply_subset (φ : F) (a : A) : σ ((φ : A → B) a) ⊆ σ a :=
λ _, mt (mem_resolvent_set_apply φ)
end comm_semiring
section comm_ring
variables {F R A B : Type*} [comm_ring R] [ring A] [algebra R A] [ring B] [algebra R B]
variables [alg_hom_class F R A R]
local notation `σ` := spectrum R
local notation `↑ₐ` := algebra_map R A
lemma apply_mem_spectrum [nontrivial R] (φ : F) (a : A) : φ a ∈ σ a :=
begin
have h : ↑ₐ(φ a) - a ∈ (φ : A →+* R).ker,
{ simp only [ring_hom.mem_ker, map_sub, ring_hom.coe_coe, alg_hom_class.commutes,
algebra.id.map_eq_id, ring_hom.id_apply, sub_self], },
simp only [spectrum.mem_iff, ←mem_nonunits_iff, coe_subset_nonunits ((φ : A →+* R).ker_ne_top) h],
end
end comm_ring
end alg_hom
|
fc6fa8a1898538204d9ebd8673b452328230972a | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/ambiguousOpenExport.lean | 905b2484e8f1e36c143b70ba0420c7d7b7a97013 | [
"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 | 119 | lean | inductive Foo.C where
| mk
inductive Bla.C where
| mk
open Foo Bla
open C (mk) -- Error
export C (mk) -- Error
|
fff1987e826838ff1949f2b8ba73f2ccba8fcd42 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/algebra/direct_sum/basic.lean | 26d4a432d26be42530c7cc6db6d2768da043f22d | [
"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 | 10,966 | lean | /-
Copyright (c) 2019 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import data.dfinsupp.basic
import group_theory.submonoid.operations
import group_theory.subgroup.basic
/-!
# Direct sum
This file defines the direct sum of abelian groups, indexed by a discrete type.
## Notation
`⨁ i, β i` is the n-ary direct sum `direct_sum`.
This notation is in the `direct_sum` locale, accessible after `open_locale direct_sum`.
## References
* https://en.wikipedia.org/wiki/Direct_sum
-/
open_locale big_operators
universes u v w u₁
variables (ι : Type v) [dec_ι : decidable_eq ι] (β : ι → Type w)
/-- `direct_sum β` is the direct sum of a family of additive commutative monoids `β i`.
Note: `open_locale direct_sum` will enable the notation `⨁ i, β i` for `direct_sum β`. -/
@[derive [add_comm_monoid, inhabited]]
def direct_sum [Π i, add_comm_monoid (β i)] : Type* := Π₀ i, β i
instance [Π i, add_comm_monoid (β i)] : has_coe_to_fun (direct_sum ι β) (λ _, Π i : ι, β i) :=
dfinsupp.has_coe_to_fun
localized "notation `⨁` binders `, ` r:(scoped f, direct_sum _ f) := r" in direct_sum
namespace direct_sum
variables {ι}
section add_comm_group
variables [Π i, add_comm_group (β i)]
instance : add_comm_group (direct_sum ι β) := dfinsupp.add_comm_group
variables {β}
@[simp] lemma sub_apply (g₁ g₂ : ⨁ i, β i) (i : ι) : (g₁ - g₂) i = g₁ i - g₂ i :=
dfinsupp.sub_apply _ _ _
end add_comm_group
variables [Π i, add_comm_monoid (β i)]
@[simp] lemma zero_apply (i : ι) : (0 : ⨁ i, β i) i = 0 := rfl
variables {β}
@[simp] lemma add_apply (g₁ g₂ : ⨁ i, β i) (i : ι) : (g₁ + g₂) i = g₁ i + g₂ i :=
dfinsupp.add_apply _ _ _
variables (β)
include dec_ι
/-- `mk β s x` is the element of `⨁ i, β i` that is zero outside `s`
and has coefficient `x i` for `i` in `s`. -/
def mk (s : finset ι) : (Π i : (↑s : set ι), β i.1) →+ ⨁ i, β i :=
{ to_fun := dfinsupp.mk s,
map_add' := λ _ _, dfinsupp.mk_add,
map_zero' := dfinsupp.mk_zero, }
/-- `of i` is the natural inclusion map from `β i` to `⨁ i, β i`. -/
def of (i : ι) : β i →+ ⨁ i, β i :=
dfinsupp.single_add_hom β i
@[simp] lemma of_eq_same (i : ι) (x : β i) : (of _ i x) i = x :=
dfinsupp.single_eq_same
lemma of_eq_of_ne (i j : ι) (x : β i) (h : i ≠ j) : (of _ i x) j = 0 :=
dfinsupp.single_eq_of_ne h
@[simp] lemma support_zero [Π (i : ι) (x : β i), decidable (x ≠ 0)] :
(0 : ⨁ i, β i).support = ∅ := dfinsupp.support_zero
@[simp] lemma support_of [Π (i : ι) (x : β i), decidable (x ≠ 0)]
(i : ι) (x : β i) (h : x ≠ 0) :
(of _ i x).support = {i} := dfinsupp.support_single_ne_zero h
lemma support_of_subset [Π (i : ι) (x : β i), decidable (x ≠ 0)] {i : ι} {b : β i} :
(of _ i b).support ⊆ {i} := dfinsupp.support_single_subset
lemma sum_support_of [Π (i : ι) (x : β i), decidable (x ≠ 0)] (x : ⨁ i, β i) :
∑ i in x.support, of β i (x i) = x := dfinsupp.sum_single
variables {β}
theorem mk_injective (s : finset ι) : function.injective (mk β s) :=
dfinsupp.mk_injective s
theorem of_injective (i : ι) : function.injective (of β i) :=
dfinsupp.single_injective
@[elab_as_eliminator]
protected theorem induction_on {C : (⨁ i, β i) → Prop}
(x : ⨁ i, β i) (H_zero : C 0)
(H_basic : ∀ (i : ι) (x : β i), C (of β i x))
(H_plus : ∀ x y, C x → C y → C (x + y)) : C x :=
begin
apply dfinsupp.induction x H_zero,
intros i b f h1 h2 ih,
solve_by_elim
end
/-- If two additive homomorphisms from `⨁ i, β i` are equal on each `of β i y`,
then they are equal. -/
lemma add_hom_ext {γ : Type*} [add_monoid γ] ⦃f g : (⨁ i, β i) →+ γ⦄
(H : ∀ (i : ι) (y : β i), f (of _ i y) = g (of _ i y)) : f = g :=
dfinsupp.add_hom_ext H
/-- If two additive homomorphisms from `⨁ i, β i` are equal on each `of β i y`,
then they are equal.
See note [partially-applied ext lemmas]. -/
@[ext] lemma add_hom_ext' {γ : Type*} [add_monoid γ] ⦃f g : (⨁ i, β i) →+ γ⦄
(H : ∀ (i : ι), f.comp (of _ i) = g.comp (of _ i)) : f = g :=
add_hom_ext $ λ i, add_monoid_hom.congr_fun $ H i
variables {γ : Type u₁} [add_comm_monoid γ]
section to_add_monoid
variables (φ : Π i, β i →+ γ) (ψ : (⨁ i, β i) →+ γ)
/-- `to_add_monoid φ` is the natural homomorphism from `⨁ i, β i` to `γ`
induced by a family `φ` of homomorphisms `β i → γ`. -/
def to_add_monoid : (⨁ i, β i) →+ γ :=
(dfinsupp.lift_add_hom φ)
@[simp] lemma to_add_monoid_of (i) (x : β i) : to_add_monoid φ (of β i x) = φ i x :=
dfinsupp.lift_add_hom_apply_single φ i x
theorem to_add_monoid.unique (f : ⨁ i, β i) :
ψ f = to_add_monoid (λ i, ψ.comp (of β i)) f :=
by {congr, ext, simp [to_add_monoid, of]}
end to_add_monoid
section from_add_monoid
/-- `from_add_monoid φ` is the natural homomorphism from `γ` to `⨁ i, β i`
induced by a family `φ` of homomorphisms `γ → β i`.
Note that this is not an isomorphism. Not every homomorphism `γ →+ ⨁ i, β i` arises in this way. -/
def from_add_monoid : (⨁ i, γ →+ β i) →+ (γ →+ ⨁ i, β i) :=
to_add_monoid $ λ i, add_monoid_hom.comp_hom (of β i)
@[simp] lemma from_add_monoid_of (i : ι) (f : γ →+ β i) :
from_add_monoid (of _ i f) = (of _ i).comp f :=
by { rw [from_add_monoid, to_add_monoid_of], refl }
lemma from_add_monoid_of_apply (i : ι) (f : γ →+ β i) (x : γ) :
from_add_monoid (of _ i f) x = of _ i (f x) :=
by rw [from_add_monoid_of, add_monoid_hom.coe_comp]
end from_add_monoid
variables (β)
/-- `set_to_set β S T h` is the natural homomorphism `⨁ (i : S), β i → ⨁ (i : T), β i`,
where `h : S ⊆ T`. -/
-- TODO: generalize this to remove the assumption `S ⊆ T`.
def set_to_set (S T : set ι) (H : S ⊆ T) :
(⨁ (i : S), β i) →+ (⨁ (i : T), β i) :=
to_add_monoid $ λ i, of (λ (i : subtype T), β i) ⟨↑i, H i.prop⟩
variables {β}
omit dec_ι
/-- A direct sum over an empty type is trivial. -/
instance [is_empty ι] : unique (⨁ i, β i) := dfinsupp.unique
/-- The natural equivalence between `⨁ _ : ι, M` and `M` when `unique ι`. -/
protected def id (M : Type v) (ι : Type* := punit) [add_comm_monoid M] [unique ι] :
(⨁ (_ : ι), M) ≃+ M :=
{ to_fun := direct_sum.to_add_monoid (λ _, add_monoid_hom.id M),
inv_fun := of (λ _, M) default,
left_inv := λ x, direct_sum.induction_on x
(by rw [add_monoid_hom.map_zero, add_monoid_hom.map_zero])
(λ p x, by rw [unique.default_eq p, to_add_monoid_of]; refl)
(λ x y ihx ihy, by rw [add_monoid_hom.map_add, add_monoid_hom.map_add, ihx, ihy]),
right_inv := λ x, to_add_monoid_of _ _ _,
..direct_sum.to_add_monoid (λ _, add_monoid_hom.id M) }
section congr_left
variables {κ : Type*}
/--Reindexing terms of a direct sum.-/
def equiv_congr_left (h : ι ≃ κ) : (⨁ i, β i) ≃+ ⨁ k, β (h.symm k) :=
{ map_add' := dfinsupp.comap_domain'_add _ _,
..dfinsupp.equiv_congr_left h }
@[simp] lemma equiv_congr_left_apply (h : ι ≃ κ) (f : ⨁ i, β i) (k : κ) :
equiv_congr_left h f k = f (h.symm k) := dfinsupp.comap_domain'_apply _ _ _ _
end congr_left
section option
variables {α : option ι → Type w} [Π i, add_comm_monoid (α i)]
include dec_ι
/--Isomorphism obtained by separating the term of index `none` of a direct sum over `option ι`.-/
@[simps] noncomputable def add_equiv_prod_direct_sum : (⨁ i, α i) ≃+ α none × ⨁ i, α (some i) :=
{ map_add' := dfinsupp.equiv_prod_dfinsupp_add, ..dfinsupp.equiv_prod_dfinsupp }
end option
section sigma
variables {α : ι → Type u} {δ : Π i, α i → Type w} [Π i j, add_comm_monoid (δ i j)]
/--The natural map between `⨁ (i : Σ i, α i), δ i.1 i.2` and `⨁ i (j : α i), δ i j`.-/
noncomputable def sigma_curry : (⨁ (i : Σ i, _), δ i.1 i.2) →+ ⨁ i j, δ i j :=
{ to_fun := @dfinsupp.sigma_curry _ _ δ _,
map_zero' := dfinsupp.sigma_curry_zero,
map_add' := λ f g, dfinsupp.sigma_curry_add f g }
@[simp] lemma sigma_curry_apply (f : ⨁ (i : Σ i, _), δ i.1 i.2) (i : ι) (j : α i) :
sigma_curry f i j = f ⟨i, j⟩ := dfinsupp.sigma_curry_apply f i j
/--The natural map between `⨁ i (j : α i), δ i j` and `Π₀ (i : Σ i, α i), δ i.1 i.2`, inverse of
`curry`.-/
noncomputable def sigma_uncurry : (⨁ i j, δ i j) →+ ⨁ (i : Σ i, _), δ i.1 i.2 :=
{ to_fun := dfinsupp.sigma_uncurry,
map_zero' := dfinsupp.sigma_uncurry_zero,
map_add' := dfinsupp.sigma_uncurry_add }
@[simp] lemma sigma_uncurry_apply (f : ⨁ i j, δ i j) (i : ι) (j : α i) :
sigma_uncurry f ⟨i, j⟩ = f i j := dfinsupp.sigma_uncurry_apply f i j
/--The natural map between `⨁ (i : Σ i, α i), δ i.1 i.2` and `⨁ i (j : α i), δ i j`.-/
noncomputable def sigma_curry_equiv : (⨁ (i : Σ i, _), δ i.1 i.2) ≃+ ⨁ i j, δ i j :=
{ ..sigma_curry, ..dfinsupp.sigma_curry_equiv }
end sigma
/-- The canonical embedding from `⨁ i, A i` to `M` where `A` is a collection of `add_submonoid M`
indexed by `ι`.
When `S = submodule _ M`, this is available as a `linear_map`, `direct_sum.coe_linear_map`. -/
protected def coe_add_monoid_hom {M S : Type*} [decidable_eq ι] [add_comm_monoid M]
[set_like S M] [add_submonoid_class S M] (A : ι → S) : (⨁ i, A i) →+ M :=
to_add_monoid (λ i, add_submonoid_class.subtype (A i))
@[simp] lemma coe_add_monoid_hom_of {M S : Type*} [decidable_eq ι] [add_comm_monoid M]
[set_like S M] [add_submonoid_class S M] (A : ι → S) (i : ι) (x : A i) :
direct_sum.coe_add_monoid_hom A (of (λ i, A i) i x) = x :=
to_add_monoid_of _ _ _
lemma coe_of_apply {M S : Type*} [decidable_eq ι] [add_comm_monoid M]
[set_like S M] [add_submonoid_class S M] {A : ι → S} (i j : ι) (x : A i) :
(of _ i x j : M) = if i = j then x else 0 :=
begin
obtain rfl | h := decidable.eq_or_ne i j,
{ rw [direct_sum.of_eq_same, if_pos rfl], },
{ rw [direct_sum.of_eq_of_ne _ _ _ _ h, if_neg h, add_submonoid_class.coe_zero], },
end
/-- The `direct_sum` formed by a collection of additive submonoids (or subgroups, or submodules) of
`M` is said to be internal if the canonical map `(⨁ i, A i) →+ M` is bijective.
For the alternate statement in terms of independence and spanning, see
`direct_sum.subgroup_is_internal_iff_independent_and_supr_eq_top` and
`direct_sum.is_internal_submodule_iff_independent_and_supr_eq_top`. -/
def is_internal {M S : Type*} [decidable_eq ι] [add_comm_monoid M]
[set_like S M] [add_submonoid_class S M] (A : ι → S) : Prop :=
function.bijective (direct_sum.coe_add_monoid_hom A)
lemma is_internal.add_submonoid_supr_eq_top {M : Type*} [decidable_eq ι] [add_comm_monoid M]
(A : ι → add_submonoid M)
(h : is_internal A) : supr A = ⊤ :=
begin
rw [add_submonoid.supr_eq_mrange_dfinsupp_sum_add_hom, add_monoid_hom.mrange_top_iff_surjective],
exact function.bijective.surjective h,
end
end direct_sum
|
b3263475ca58ddbaa67a10fc45a733c906d18946 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /hott/algebra/binary.hlean | d11673f164d27b8f58bfd320d4d3963348f30fe1 | [
"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 | 4,568 | 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, Jeremy Avigad
General properties of binary operations.
-/
open eq.ops function
namespace binary
section
variable {A : Type}
variables (op₁ : A → A → A) (inv : A → A) (one : A)
local notation a * b := op₁ a b
local notation a ⁻¹ := inv a
definition commutative := Πa b, a * b = b * a
definition associative := Πa b c, (a * b) * c = a * (b * c)
definition left_identity := Πa, one * a = a
definition right_identity := Πa, a * one = a
definition left_inverse := Πa, a⁻¹ * a = one
definition right_inverse := Πa, a * a⁻¹ = one
definition left_cancelative := Πa b c, a * b = a * c → b = c
definition right_cancelative := Πa b c, a * b = c * b → a = c
definition inv_op_cancel_left := Πa b, a⁻¹ * (a * b) = b
definition op_inv_cancel_left := Πa b, a * (a⁻¹ * b) = b
definition inv_op_cancel_right := Πa b, a * b⁻¹ * b = a
definition op_inv_cancel_right := Πa b, a * b * b⁻¹ = a
variable (op₂ : A → A → A)
local notation a + b := op₂ a b
definition left_distributive := Πa b c, a * (b + c) = a * b + a * c
definition right_distributive := Πa b c, (a + b) * c = a * c + b * c
definition right_commutative {B : Type} (f : B → A → B) := Π b a₁ a₂, f (f b a₁) a₂ = f (f b a₂) a₁
definition left_commutative {B : Type} (f : A → B → B) := Π a₁ a₂ b, f a₁ (f a₂ b) = f a₂ (f a₁ b)
end
section
variable {A : Type}
variable {f : A → A → A}
variable H_comm : commutative f
variable H_assoc : associative f
local infixl `*` := f
theorem left_comm : left_commutative f :=
take a b c, calc
a*(b*c) = (a*b)*c : H_assoc
... = (b*a)*c : H_comm
... = b*(a*c) : H_assoc
theorem right_comm : right_commutative f :=
take a b c, calc
(a*b)*c = a*(b*c) : H_assoc
... = a*(c*b) : H_comm
... = (a*c)*b : H_assoc
theorem comm4 (a b c d : A) : a*b*(c*d) = a*c*(b*d) :=
calc
a*b*(c*d) = a*b*c*d : H_assoc
... = a*c*b*d : right_comm H_comm H_assoc
... = a*c*(b*d) : H_assoc
end
section
variable {A : Type}
variable {f : A → A → A}
variable H_assoc : associative f
local infixl `*` := f
theorem assoc4helper (a b c d) : (a*b)*(c*d) = a*((b*c)*d) :=
calc
(a*b)*(c*d) = a*(b*(c*d)) : H_assoc
... = a*((b*c)*d) : H_assoc
end
definition right_commutative_compose_right
{A B : Type} (f : A → A → A) (g : B → A) (rcomm : right_commutative f) : right_commutative (compose_right f g) :=
λ a b₁ b₂, !rcomm
definition left_commutative_compose_left
{A B : Type} (f : A → A → A) (g : B → A) (lcomm : left_commutative f) : left_commutative (compose_left f g) :=
λ a b₁ b₂, !lcomm
end binary
open eq
namespace is_equiv
definition inv_preserve_binary {A B : Type} (f : A → B) [H : is_equiv f]
(mA : A → A → A) (mB : B → B → B) (H : Π(a a' : A), f (mA a a') = mB (f a) (f a'))
(b b' : B) : f⁻¹ (mB b b') = mA (f⁻¹ b) (f⁻¹ b') :=
begin
have H2 : f⁻¹ (mB (f (f⁻¹ b)) (f (f⁻¹ b'))) = f⁻¹ (f (mA (f⁻¹ b) (f⁻¹ b'))), from ap f⁻¹ !H⁻¹,
rewrite [+right_inv f at H2,left_inv f at H2,▸* at H2,H2]
end
definition preserve_binary_of_inv_preserve {A B : Type} (f : A → B) [H : is_equiv f]
(mA : A → A → A) (mB : B → B → B) (H : Π(b b' : B), f⁻¹ (mB b b') = mA (f⁻¹ b) (f⁻¹ b'))
(a a' : A) : f (mA a a') = mB (f a) (f a') :=
begin
have H2 : f (mA (f⁻¹ (f a)) (f⁻¹ (f a'))) = f (f⁻¹ (mB (f a) (f a'))), from ap f !H⁻¹,
rewrite [right_inv f at H2,+left_inv f at H2,▸* at H2,H2]
end
end is_equiv
namespace equiv
open is_equiv
definition inv_preserve_binary {A B : Type} (f : A ≃ B)
(mA : A → A → A) (mB : B → B → B) (H : Π(a a' : A), f (mA a a') = mB (f a) (f a'))
(b b' : B) : f⁻¹ (mB b b') = mA (f⁻¹ b) (f⁻¹ b') :=
inv_preserve_binary f mA mB H b b'
definition preserve_binary_of_inv_preserve {A B : Type} (f : A ≃ B)
(mA : A → A → A) (mB : B → B → B) (H : Π(b b' : B), f⁻¹ (mB b b') = mA (f⁻¹ b) (f⁻¹ b'))
(a a' : A) : f (mA a a') = mB (f a) (f a') :=
preserve_binary_of_inv_preserve f mA mB H a a'
end equiv
|
f0c88e99dbab4175174fbcb950ce89eee77a979b | 97f752b44fd85ec3f635078a2dd125ddae7a82b6 | /library/algebra/ordered_ring.lean | 489a4f6a35b820860859688d5df22abc53fbbfaa | [
"Apache-2.0"
] | permissive | tectronics/lean | ab977ba6be0fcd46047ddbb3c8e16e7c26710701 | f38af35e0616f89c6e9d7e3eb1d48e47ee666efe | refs/heads/master | 1,532,358,526,384 | 1,456,276,623,000 | 1,456,276,623,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 28,563 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
Here an "ordered_ring" is partially ordered ring, which is ordered with respect to both a weak
order and an associated strict order. Our numeric structures (int, rat, and real) will be instances
of "linear_ordered_comm_ring". This development is modeled after Isabelle's library.
-/
import algebra.ordered_group algebra.ring
open eq eq.ops
variable {A : Type}
private definition absurd_a_lt_a {B : Type} {a : A} [s : strict_order A] (H : a < a) : B :=
absurd H (lt.irrefl a)
/- semiring structures -/
structure ordered_semiring [class] (A : Type)
extends semiring A, ordered_cancel_comm_monoid A :=
(mul_le_mul_of_nonneg_left: ∀a b c, le a b → le zero c → le (mul c a) (mul c b))
(mul_le_mul_of_nonneg_right: ∀a b c, le a b → le zero c → le (mul a c) (mul b c))
(mul_lt_mul_of_pos_left: ∀a b c, lt a b → lt zero c → lt (mul c a) (mul c b))
(mul_lt_mul_of_pos_right: ∀a b c, lt a b → lt zero c → lt (mul a c) (mul b c))
section
variable [s : ordered_semiring A]
variables (a b c d e : A)
include s
theorem mul_le_mul_of_nonneg_left {a b c : A} (Hab : a ≤ b) (Hc : 0 ≤ c) :
c * a ≤ c * b := !ordered_semiring.mul_le_mul_of_nonneg_left Hab Hc
theorem mul_le_mul_of_nonneg_right {a b c : A} (Hab : a ≤ b) (Hc : 0 ≤ c) :
a * c ≤ b * c := !ordered_semiring.mul_le_mul_of_nonneg_right Hab Hc
-- TODO: there are four variations, depending on which variables we assume to be nonneg
theorem mul_le_mul {a b c d : A} (Hac : a ≤ c) (Hbd : b ≤ d) (nn_b : 0 ≤ b) (nn_c : 0 ≤ c) :
a * b ≤ c * d :=
calc
a * b ≤ c * b : mul_le_mul_of_nonneg_right Hac nn_b
... ≤ c * d : mul_le_mul_of_nonneg_left Hbd nn_c
theorem mul_nonneg {a b : A} (Ha : a ≥ 0) (Hb : b ≥ 0) : a * b ≥ 0 :=
begin
have H : 0 * b ≤ a * b, from mul_le_mul_of_nonneg_right Ha Hb,
rewrite zero_mul at H,
exact H
end
theorem mul_nonpos_of_nonneg_of_nonpos {a b : A} (Ha : a ≥ 0) (Hb : b ≤ 0) : a * b ≤ 0 :=
begin
have H : a * b ≤ a * 0, from mul_le_mul_of_nonneg_left Hb Ha,
rewrite mul_zero at H,
exact H
end
theorem mul_nonpos_of_nonpos_of_nonneg {a b : A} (Ha : a ≤ 0) (Hb : b ≥ 0) : a * b ≤ 0 :=
begin
have H : a * b ≤ 0 * b, from mul_le_mul_of_nonneg_right Ha Hb,
rewrite zero_mul at H,
exact H
end
theorem mul_lt_mul_of_pos_left {a b c : A} (Hab : a < b) (Hc : 0 < c) :
c * a < c * b := !ordered_semiring.mul_lt_mul_of_pos_left Hab Hc
theorem mul_lt_mul_of_pos_right {a b c : A} (Hab : a < b) (Hc : 0 < c) :
a * c < b * c := !ordered_semiring.mul_lt_mul_of_pos_right Hab Hc
-- TODO: once again, there are variations
theorem mul_lt_mul {a b c d : A} (Hac : a < c) (Hbd : b ≤ d) (pos_b : 0 < b) (nn_c : 0 ≤ c) :
a * b < c * d :=
calc
a * b < c * b : mul_lt_mul_of_pos_right Hac pos_b
... ≤ c * d : mul_le_mul_of_nonneg_left Hbd nn_c
theorem mul_lt_mul' {a b c d : A} (H1 : a < c) (H2 : b < d) (H3 : b ≥ 0) (H4 : c > 0) :
a * b < c * d :=
calc
a * b ≤ c * b : mul_le_mul_of_nonneg_right (le_of_lt H1) H3
... < c * d : mul_lt_mul_of_pos_left H2 H4
theorem mul_pos {a b : A} (Ha : a > 0) (Hb : b > 0) : a * b > 0 :=
begin
have H : 0 * b < a * b, from mul_lt_mul_of_pos_right Ha Hb,
rewrite zero_mul at H,
exact H
end
theorem mul_neg_of_pos_of_neg {a b : A} (Ha : a > 0) (Hb : b < 0) : a * b < 0 :=
begin
have H : a * b < a * 0, from mul_lt_mul_of_pos_left Hb Ha,
rewrite mul_zero at H,
exact H
end
theorem mul_neg_of_neg_of_pos {a b : A} (Ha : a < 0) (Hb : b > 0) : a * b < 0 :=
begin
have H : a * b < 0 * b, from mul_lt_mul_of_pos_right Ha Hb,
rewrite zero_mul at H,
exact H
end
theorem mul_self_lt_mul_self {a b : A} (H1 : 0 ≤ a) (H2 : a < b) : a * a < b * b :=
mul_lt_mul' H2 H2 H1 (lt_of_le_of_lt H1 H2)
end
structure linear_ordered_semiring [class] (A : Type)
extends ordered_semiring A, linear_strong_order_pair A :=
(zero_lt_one : lt zero one)
section
variable [s : linear_ordered_semiring A]
variables {a b c : A}
include s
theorem zero_lt_one : 0 < (1:A) := linear_ordered_semiring.zero_lt_one A
theorem lt_of_mul_lt_mul_left (H : c * a < c * b) (Hc : c ≥ 0) : a < b :=
lt_of_not_ge
(assume H1 : b ≤ a,
have H2 : c * b ≤ c * a, from mul_le_mul_of_nonneg_left H1 Hc,
not_lt_of_ge H2 H)
theorem lt_of_mul_lt_mul_right (H : a * c < b * c) (Hc : c ≥ 0) : a < b :=
lt_of_not_ge
(assume H1 : b ≤ a,
have H2 : b * c ≤ a * c, from mul_le_mul_of_nonneg_right H1 Hc,
not_lt_of_ge H2 H)
theorem le_of_mul_le_mul_left (H : c * a ≤ c * b) (Hc : c > 0) : a ≤ b :=
le_of_not_gt
(assume H1 : b < a,
have H2 : c * b < c * a, from mul_lt_mul_of_pos_left H1 Hc,
not_le_of_gt H2 H)
theorem le_of_mul_le_mul_right (H : a * c ≤ b * c) (Hc : c > 0) : a ≤ b :=
le_of_not_gt
(assume H1 : b < a,
have H2 : b * c < a * c, from mul_lt_mul_of_pos_right H1 Hc,
not_le_of_gt H2 H)
theorem le_iff_mul_le_mul_left (a b : A) {c : A} (H : c > 0) : a ≤ b ↔ c * a ≤ c * b :=
iff.intro
(assume H', mul_le_mul_of_nonneg_left H' (le_of_lt H))
(assume H', le_of_mul_le_mul_left H' H)
theorem le_iff_mul_le_mul_right (a b : A) {c : A} (H : c > 0) : a ≤ b ↔ a * c ≤ b * c :=
iff.intro
(assume H', mul_le_mul_of_nonneg_right H' (le_of_lt H))
(assume H', le_of_mul_le_mul_right H' H)
theorem pos_of_mul_pos_left (H : 0 < a * b) (H1 : 0 ≤ a) : 0 < b :=
lt_of_not_ge
(assume H2 : b ≤ 0,
have H3 : a * b ≤ 0, from mul_nonpos_of_nonneg_of_nonpos H1 H2,
not_lt_of_ge H3 H)
theorem pos_of_mul_pos_right (H : 0 < a * b) (H1 : 0 ≤ b) : 0 < a :=
lt_of_not_ge
(assume H2 : a ≤ 0,
have H3 : a * b ≤ 0, from mul_nonpos_of_nonpos_of_nonneg H2 H1,
not_lt_of_ge H3 H)
theorem nonneg_of_mul_nonneg_left (H : 0 ≤ a * b) (H1 : 0 < a) : 0 ≤ b :=
le_of_not_gt
(assume H2 : b < 0,
not_le_of_gt (mul_neg_of_pos_of_neg H1 H2) H)
theorem nonneg_of_mul_nonneg_right (H : 0 ≤ a * b) (H1 : 0 < b) : 0 ≤ a :=
le_of_not_gt
(assume H2 : a < 0,
not_le_of_gt (mul_neg_of_neg_of_pos H2 H1) H)
theorem neg_of_mul_neg_left (H : a * b < 0) (H1 : 0 ≤ a) : b < 0 :=
lt_of_not_ge
(assume H2 : b ≥ 0,
not_lt_of_ge (mul_nonneg H1 H2) H)
theorem neg_of_mul_neg_right (H : a * b < 0) (H1 : 0 ≤ b) : a < 0 :=
lt_of_not_ge
(assume H2 : a ≥ 0,
not_lt_of_ge (mul_nonneg H2 H1) H)
theorem nonpos_of_mul_nonpos_left (H : a * b ≤ 0) (H1 : 0 < a) : b ≤ 0 :=
le_of_not_gt
(assume H2 : b > 0,
not_le_of_gt (mul_pos H1 H2) H)
theorem nonpos_of_mul_nonpos_right (H : a * b ≤ 0) (H1 : 0 < b) : a ≤ 0 :=
le_of_not_gt
(assume H2 : a > 0,
not_le_of_gt (mul_pos H2 H1) H)
end
structure decidable_linear_ordered_semiring [class] (A : Type)
extends linear_ordered_semiring A, decidable_linear_ordered_cancel_comm_monoid A
/- ring structures -/
structure ordered_ring [class] (A : Type)
extends ring A, ordered_comm_group A, zero_ne_one_class A :=
(mul_nonneg : ∀a b, le zero a → le zero b → le zero (mul a b))
(mul_pos : ∀a b, lt zero a → lt zero b → lt zero (mul a b))
theorem ordered_ring.mul_le_mul_of_nonneg_left [s : ordered_ring A] {a b c : A}
(Hab : a ≤ b) (Hc : 0 ≤ c) : c * a ≤ c * b :=
have H1 : 0 ≤ b - a, from iff.elim_right !sub_nonneg_iff_le Hab,
assert H2 : 0 ≤ c * (b - a), from ordered_ring.mul_nonneg _ _ Hc H1,
begin
rewrite mul_sub_left_distrib at H2,
exact (iff.mp !sub_nonneg_iff_le H2)
end
theorem ordered_ring.mul_le_mul_of_nonneg_right [s : ordered_ring A] {a b c : A}
(Hab : a ≤ b) (Hc : 0 ≤ c) : a * c ≤ b * c :=
have H1 : 0 ≤ b - a, from iff.elim_right !sub_nonneg_iff_le Hab,
assert H2 : 0 ≤ (b - a) * c, from ordered_ring.mul_nonneg _ _ H1 Hc,
begin
rewrite mul_sub_right_distrib at H2,
exact (iff.mp !sub_nonneg_iff_le H2)
end
theorem ordered_ring.mul_lt_mul_of_pos_left [s : ordered_ring A] {a b c : A}
(Hab : a < b) (Hc : 0 < c) : c * a < c * b :=
have H1 : 0 < b - a, from iff.elim_right !sub_pos_iff_lt Hab,
assert H2 : 0 < c * (b - a), from ordered_ring.mul_pos _ _ Hc H1,
begin
rewrite mul_sub_left_distrib at H2,
exact (iff.mp !sub_pos_iff_lt H2)
end
theorem ordered_ring.mul_lt_mul_of_pos_right [s : ordered_ring A] {a b c : A}
(Hab : a < b) (Hc : 0 < c) : a * c < b * c :=
have H1 : 0 < b - a, from iff.elim_right !sub_pos_iff_lt Hab,
assert H2 : 0 < (b - a) * c, from ordered_ring.mul_pos _ _ H1 Hc,
begin
rewrite mul_sub_right_distrib at H2,
exact (iff.mp !sub_pos_iff_lt H2)
end
definition ordered_ring.to_ordered_semiring [trans_instance] [reducible]
[s : ordered_ring A] :
ordered_semiring A :=
⦃ ordered_semiring, s,
mul_zero := mul_zero,
zero_mul := zero_mul,
add_left_cancel := @add.left_cancel A _,
add_right_cancel := @add.right_cancel A _,
le_of_add_le_add_left := @le_of_add_le_add_left A _,
mul_le_mul_of_nonneg_left := @ordered_ring.mul_le_mul_of_nonneg_left A _,
mul_le_mul_of_nonneg_right := @ordered_ring.mul_le_mul_of_nonneg_right A _,
mul_lt_mul_of_pos_left := @ordered_ring.mul_lt_mul_of_pos_left A _,
mul_lt_mul_of_pos_right := @ordered_ring.mul_lt_mul_of_pos_right A _,
lt_of_add_lt_add_left := @lt_of_add_lt_add_left A _⦄
section
variable [s : ordered_ring A]
variables {a b c : A}
include s
theorem mul_le_mul_of_nonpos_left (H : b ≤ a) (Hc : c ≤ 0) : c * a ≤ c * b :=
have Hc' : -c ≥ 0, from iff.mpr !neg_nonneg_iff_nonpos Hc,
assert H1 : -c * b ≤ -c * a, from mul_le_mul_of_nonneg_left H Hc',
have H2 : -(c * b) ≤ -(c * a),
begin
rewrite [-*neg_mul_eq_neg_mul at H1],
exact H1
end,
iff.mp !neg_le_neg_iff_le H2
theorem mul_le_mul_of_nonpos_right (H : b ≤ a) (Hc : c ≤ 0) : a * c ≤ b * c :=
have Hc' : -c ≥ 0, from iff.mpr !neg_nonneg_iff_nonpos Hc,
assert H1 : b * -c ≤ a * -c, from mul_le_mul_of_nonneg_right H Hc',
have H2 : -(b * c) ≤ -(a * c),
begin
rewrite [-*neg_mul_eq_mul_neg at H1],
exact H1
end,
iff.mp !neg_le_neg_iff_le H2
theorem mul_nonneg_of_nonpos_of_nonpos (Ha : a ≤ 0) (Hb : b ≤ 0) : 0 ≤ a * b :=
begin
have H : 0 * b ≤ a * b, from mul_le_mul_of_nonpos_right Ha Hb,
rewrite zero_mul at H,
exact H
end
theorem mul_lt_mul_of_neg_left (H : b < a) (Hc : c < 0) : c * a < c * b :=
have Hc' : -c > 0, from iff.mpr !neg_pos_iff_neg Hc,
assert H1 : -c * b < -c * a, from mul_lt_mul_of_pos_left H Hc',
have H2 : -(c * b) < -(c * a),
begin
rewrite [-*neg_mul_eq_neg_mul at H1],
exact H1
end,
iff.mp !neg_lt_neg_iff_lt H2
theorem mul_lt_mul_of_neg_right (H : b < a) (Hc : c < 0) : a * c < b * c :=
have Hc' : -c > 0, from iff.mpr !neg_pos_iff_neg Hc,
assert H1 : b * -c < a * -c, from mul_lt_mul_of_pos_right H Hc',
have H2 : -(b * c) < -(a * c),
begin
rewrite [-*neg_mul_eq_mul_neg at H1],
exact H1
end,
iff.mp !neg_lt_neg_iff_lt H2
theorem mul_pos_of_neg_of_neg (Ha : a < 0) (Hb : b < 0) : 0 < a * b :=
begin
have H : 0 * b < a * b, from mul_lt_mul_of_neg_right Ha Hb,
rewrite zero_mul at H,
exact H
end
end
-- TODO: we can eliminate mul_pos_of_pos, but now it is not worth the effort to redeclare the
-- class instance
structure linear_ordered_ring [class] (A : Type)
extends ordered_ring A, linear_strong_order_pair A :=
(zero_lt_one : lt zero one)
definition linear_ordered_ring.to_linear_ordered_semiring [trans_instance] [reducible]
[s : linear_ordered_ring A] :
linear_ordered_semiring A :=
⦃ linear_ordered_semiring, s,
mul_zero := mul_zero,
zero_mul := zero_mul,
add_left_cancel := @add.left_cancel A _,
add_right_cancel := @add.right_cancel A _,
le_of_add_le_add_left := @le_of_add_le_add_left A _,
mul_le_mul_of_nonneg_left := @mul_le_mul_of_nonneg_left A _,
mul_le_mul_of_nonneg_right := @mul_le_mul_of_nonneg_right A _,
mul_lt_mul_of_pos_left := @mul_lt_mul_of_pos_left A _,
mul_lt_mul_of_pos_right := @mul_lt_mul_of_pos_right A _,
le_total := linear_ordered_ring.le_total,
lt_of_add_lt_add_left := @lt_of_add_lt_add_left A _ ⦄
structure linear_ordered_comm_ring [class] (A : Type) extends linear_ordered_ring A, comm_monoid A
theorem linear_ordered_comm_ring.eq_zero_or_eq_zero_of_mul_eq_zero [s : linear_ordered_comm_ring A]
{a b : A} (H : a * b = 0) : a = 0 ∨ b = 0 :=
lt.by_cases
(assume Ha : 0 < a,
lt.by_cases
(assume Hb : 0 < b,
begin
have H1 : 0 < a * b, from mul_pos Ha Hb,
rewrite H at H1,
apply absurd_a_lt_a H1
end)
(assume Hb : 0 = b, or.inr (Hb⁻¹))
(assume Hb : 0 > b,
begin
have H1 : 0 > a * b, from mul_neg_of_pos_of_neg Ha Hb,
rewrite H at H1,
apply absurd_a_lt_a H1
end))
(assume Ha : 0 = a, or.inl (Ha⁻¹))
(assume Ha : 0 > a,
lt.by_cases
(assume Hb : 0 < b,
begin
have H1 : 0 > a * b, from mul_neg_of_neg_of_pos Ha Hb,
rewrite H at H1,
apply absurd_a_lt_a H1
end)
(assume Hb : 0 = b, or.inr (Hb⁻¹))
(assume Hb : 0 > b,
begin
have H1 : 0 < a * b, from mul_pos_of_neg_of_neg Ha Hb,
rewrite H at H1,
apply absurd_a_lt_a H1
end))
-- Linearity implies no zero divisors. Doesn't need commutativity.
definition linear_ordered_comm_ring.to_integral_domain [trans_instance] [reducible]
[s: linear_ordered_comm_ring A] : integral_domain A :=
⦃ integral_domain, s,
eq_zero_or_eq_zero_of_mul_eq_zero :=
@linear_ordered_comm_ring.eq_zero_or_eq_zero_of_mul_eq_zero A s ⦄
section
variable [s : linear_ordered_ring A]
variables (a b c : A)
include s
theorem mul_self_nonneg : a * a ≥ 0 :=
or.elim (le.total 0 a)
(assume H : a ≥ 0, mul_nonneg H H)
(assume H : a ≤ 0, mul_nonneg_of_nonpos_of_nonpos H H)
theorem zero_le_one : 0 ≤ (1:A) := one_mul 1 ▸ mul_self_nonneg 1
theorem pos_and_pos_or_neg_and_neg_of_mul_pos {a b : A} (Hab : a * b > 0) :
(a > 0 ∧ b > 0) ∨ (a < 0 ∧ b < 0) :=
lt.by_cases
(assume Ha : 0 < a,
lt.by_cases
(assume Hb : 0 < b, or.inl (and.intro Ha Hb))
(assume Hb : 0 = b,
begin
rewrite [-Hb at Hab, mul_zero at Hab],
apply absurd_a_lt_a Hab
end)
(assume Hb : b < 0,
absurd Hab (lt.asymm (mul_neg_of_pos_of_neg Ha Hb))))
(assume Ha : 0 = a,
begin
rewrite [-Ha at Hab, zero_mul at Hab],
apply absurd_a_lt_a Hab
end)
(assume Ha : a < 0,
lt.by_cases
(assume Hb : 0 < b,
absurd Hab (lt.asymm (mul_neg_of_neg_of_pos Ha Hb)))
(assume Hb : 0 = b,
begin
rewrite [-Hb at Hab, mul_zero at Hab],
apply absurd_a_lt_a Hab
end)
(assume Hb : b < 0, or.inr (and.intro Ha Hb)))
theorem gt_of_mul_lt_mul_neg_left {a b c : A} (H : c * a < c * b) (Hc : c ≤ 0) : a > b :=
have nhc : -c ≥ 0, from neg_nonneg_of_nonpos Hc,
have H2 : -(c * b) < -(c * a), from iff.mpr (neg_lt_neg_iff_lt _ _) H,
have H3 : (-c) * b < (-c) * a, from calc
(-c) * b = - (c * b) : neg_mul_eq_neg_mul
... < -(c * a) : H2
... = (-c) * a : neg_mul_eq_neg_mul,
lt_of_mul_lt_mul_left H3 nhc
theorem zero_gt_neg_one : -1 < (0:A) :=
neg_zero ▸ (neg_lt_neg zero_lt_one)
theorem le_of_mul_le_of_ge_one {a b c : A} (H : a * c ≤ b) (Hb : b ≥ 0) (Hc : c ≥ 1) : a ≤ b :=
have H' : a * c ≤ b * c, from calc
a * c ≤ b : H
... = b * 1 : mul_one
... ≤ b * c : mul_le_mul_of_nonneg_left Hc Hb,
le_of_mul_le_mul_right H' (lt_of_lt_of_le zero_lt_one Hc)
theorem nonneg_le_nonneg_of_squares_le {a b : A} (Ha : a ≥ 0) (Hb : b ≥ 0) (H : a * a ≤ b * b) :
a ≤ b :=
begin
apply le_of_not_gt,
intro Hab,
note Hposa := lt_of_le_of_lt Hb Hab,
note H' := calc
b * b ≤ a * b : mul_le_mul_of_nonneg_right (le_of_lt Hab) Hb
... < a * a : mul_lt_mul_of_pos_left Hab Hposa,
apply (not_le_of_gt H') H
end
end
/- TODO: Isabelle's library has all kinds of cancelation rules for the simplifier.
Search on mult_le_cancel_right1 in Rings.thy. -/
structure decidable_linear_ordered_comm_ring [class] (A : Type) extends linear_ordered_comm_ring A,
decidable_linear_ordered_comm_group A
definition decidable_linear_ordered_comm_ring.to_decidable_linear_ordered_semiring
[trans_instance] [reducible] [s : decidable_linear_ordered_comm_ring A] :
decidable_linear_ordered_semiring A :=
⦃decidable_linear_ordered_semiring, s, @linear_ordered_ring.to_linear_ordered_semiring A _⦄
section
variable [s : decidable_linear_ordered_comm_ring A]
variables {a b c : A}
include s
definition sign (a : A) : A := lt.cases a 0 (-1) 0 1
theorem sign_of_neg (H : a < 0) : sign a = -1 := lt.cases_of_lt H
theorem sign_zero : sign 0 = (0:A) := lt.cases_of_eq rfl
theorem sign_of_pos (H : a > 0) : sign a = 1 := lt.cases_of_gt H
theorem sign_one : sign 1 = (1:A) := sign_of_pos zero_lt_one
theorem sign_neg_one : sign (-1) = -(1:A) := sign_of_neg (neg_neg_of_pos zero_lt_one)
theorem sign_sign (a : A) : sign (sign a) = sign a :=
lt.by_cases
(assume H : a > 0,
calc
sign (sign a) = sign 1 : by rewrite (sign_of_pos H)
... = 1 : by rewrite sign_one
... = sign a : by rewrite (sign_of_pos H))
(assume H : 0 = a,
calc
sign (sign a) = sign (sign 0) : by rewrite H
... = sign 0 : by rewrite sign_zero at {1}
... = sign a : by rewrite -H)
(assume H : a < 0,
calc
sign (sign a) = sign (-1) : by rewrite (sign_of_neg H)
... = -1 : by rewrite sign_neg_one
... = sign a : by rewrite (sign_of_neg H))
theorem pos_of_sign_eq_one (H : sign a = 1) : a > 0 :=
lt.by_cases
(assume H1 : 0 < a, H1)
(assume H1 : 0 = a,
begin
rewrite [-H1 at H, sign_zero at H],
apply absurd H zero_ne_one
end)
(assume H1 : 0 > a,
have H2 : -1 = 1, from (sign_of_neg H1)⁻¹ ⬝ H,
absurd ((eq_zero_of_neg_eq H2)⁻¹) zero_ne_one)
theorem eq_zero_of_sign_eq_zero (H : sign a = 0) : a = 0 :=
lt.by_cases
(assume H1 : 0 < a,
absurd (H⁻¹ ⬝ sign_of_pos H1) zero_ne_one)
(assume H1 : 0 = a, H1⁻¹)
(assume H1 : 0 > a,
have H2 : 0 = -1, from H⁻¹ ⬝ sign_of_neg H1,
have H3 : 1 = 0, from eq_neg_of_eq_neg H2 ⬝ neg_zero,
absurd (H3⁻¹) zero_ne_one)
theorem neg_of_sign_eq_neg_one (H : sign a = -1) : a < 0 :=
lt.by_cases
(assume H1 : 0 < a,
have H2 : -1 = 1, from H⁻¹ ⬝ (sign_of_pos H1),
absurd ((eq_zero_of_neg_eq H2)⁻¹) zero_ne_one)
(assume H1 : 0 = a,
have H2 : (0:A) = -1,
begin
rewrite [-H1 at H, sign_zero at H],
exact H
end,
have H3 : 1 = 0, from eq_neg_of_eq_neg H2 ⬝ neg_zero,
absurd (H3⁻¹) zero_ne_one)
(assume H1 : 0 > a, H1)
theorem sign_neg (a : A) : sign (-a) = -(sign a) :=
lt.by_cases
(assume H1 : 0 < a,
calc
sign (-a) = -1 : sign_of_neg (neg_neg_of_pos H1)
... = -(sign a) : by rewrite (sign_of_pos H1))
(assume H1 : 0 = a,
calc
sign (-a) = sign (-0) : by rewrite H1
... = sign 0 : by rewrite neg_zero
... = 0 : by rewrite sign_zero
... = -0 : by rewrite neg_zero
... = -(sign 0) : by rewrite sign_zero
... = -(sign a) : by rewrite -H1)
(assume H1 : 0 > a,
calc
sign (-a) = 1 : sign_of_pos (neg_pos_of_neg H1)
... = -(-1) : by rewrite neg_neg
... = -(sign a) : sign_of_neg H1)
theorem sign_mul (a b : A) : sign (a * b) = sign a * sign b :=
lt.by_cases
(assume z_lt_a : 0 < a,
lt.by_cases
(assume z_lt_b : 0 < b,
by rewrite [sign_of_pos z_lt_a, sign_of_pos z_lt_b,
sign_of_pos (mul_pos z_lt_a z_lt_b), one_mul])
(assume z_eq_b : 0 = b, by rewrite [-z_eq_b, mul_zero, *sign_zero, mul_zero])
(assume z_gt_b : 0 > b,
by rewrite [sign_of_pos z_lt_a, sign_of_neg z_gt_b,
sign_of_neg (mul_neg_of_pos_of_neg z_lt_a z_gt_b), one_mul]))
(assume z_eq_a : 0 = a, by rewrite [-z_eq_a, zero_mul, *sign_zero, zero_mul])
(assume z_gt_a : 0 > a,
lt.by_cases
(assume z_lt_b : 0 < b,
by rewrite [sign_of_neg z_gt_a, sign_of_pos z_lt_b,
sign_of_neg (mul_neg_of_neg_of_pos z_gt_a z_lt_b), mul_one])
(assume z_eq_b : 0 = b, by rewrite [-z_eq_b, mul_zero, *sign_zero, mul_zero])
(assume z_gt_b : 0 > b,
by rewrite [sign_of_neg z_gt_a, sign_of_neg z_gt_b,
sign_of_pos (mul_pos_of_neg_of_neg z_gt_a z_gt_b),
neg_mul_neg, one_mul]))
theorem abs_eq_sign_mul (a : A) : abs a = sign a * a :=
lt.by_cases
(assume H1 : 0 < a,
calc
abs a = a : abs_of_pos H1
... = 1 * a : by rewrite one_mul
... = sign a * a : by rewrite (sign_of_pos H1))
(assume H1 : 0 = a,
calc
abs a = abs 0 : by rewrite H1
... = 0 : by rewrite abs_zero
... = 0 * a : by rewrite zero_mul
... = sign 0 * a : by rewrite sign_zero
... = sign a * a : by rewrite H1)
(assume H1 : a < 0,
calc
abs a = -a : abs_of_neg H1
... = -1 * a : by rewrite neg_eq_neg_one_mul
... = sign a * a : by rewrite (sign_of_neg H1))
theorem eq_sign_mul_abs (a : A) : a = sign a * abs a :=
lt.by_cases
(assume H1 : 0 < a,
calc
a = abs a : abs_of_pos H1
... = 1 * abs a : by rewrite one_mul
... = sign a * abs a : by rewrite (sign_of_pos H1))
(assume H1 : 0 = a,
calc
a = 0 : H1⁻¹
... = 0 * abs a : by rewrite zero_mul
... = sign 0 * abs a : by rewrite sign_zero
... = sign a * abs a : by rewrite H1)
(assume H1 : a < 0,
calc
a = -(-a) : by rewrite neg_neg
... = -abs a : by rewrite (abs_of_neg H1)
... = -1 * abs a : by rewrite neg_eq_neg_one_mul
... = sign a * abs a : by rewrite (sign_of_neg H1))
theorem abs_dvd_iff (a b : A) : abs a ∣ b ↔ a ∣ b :=
abs.by_cases !iff.refl !neg_dvd_iff_dvd
theorem abs_dvd_of_dvd {a b : A} : a ∣ b → abs a ∣ b :=
iff.mpr !abs_dvd_iff
theorem dvd_abs_iff (a b : A) : a ∣ abs b ↔ a ∣ b :=
abs.by_cases !iff.refl !dvd_neg_iff_dvd
theorem dvd_abs_of_dvd {a b : A} : a ∣ b → a ∣ abs b :=
iff.mpr !dvd_abs_iff
theorem abs_mul (a b : A) : abs (a * b) = abs a * abs b :=
or.elim (le.total 0 a)
(assume H1 : 0 ≤ a,
or.elim (le.total 0 b)
(assume H2 : 0 ≤ b,
calc
abs (a * b) = a * b : abs_of_nonneg (mul_nonneg H1 H2)
... = abs a * b : by rewrite (abs_of_nonneg H1)
... = abs a * abs b : by rewrite (abs_of_nonneg H2))
(assume H2 : b ≤ 0,
calc
abs (a * b) = -(a * b) : abs_of_nonpos (mul_nonpos_of_nonneg_of_nonpos H1 H2)
... = a * -b : by rewrite neg_mul_eq_mul_neg
... = abs a * -b : by rewrite (abs_of_nonneg H1)
... = abs a * abs b : by rewrite (abs_of_nonpos H2)))
(assume H1 : a ≤ 0,
or.elim (le.total 0 b)
(assume H2 : 0 ≤ b,
calc
abs (a * b) = -(a * b) : abs_of_nonpos (mul_nonpos_of_nonpos_of_nonneg H1 H2)
... = -a * b : by rewrite neg_mul_eq_neg_mul
... = abs a * b : by rewrite (abs_of_nonpos H1)
... = abs a * abs b : by rewrite (abs_of_nonneg H2))
(assume H2 : b ≤ 0,
calc
abs (a * b) = a * b : abs_of_nonneg (mul_nonneg_of_nonpos_of_nonpos H1 H2)
... = -a * -b : by rewrite neg_mul_neg
... = abs a * -b : by rewrite (abs_of_nonpos H1)
... = abs a * abs b : by rewrite (abs_of_nonpos H2)))
theorem abs_mul_abs_self (a : A) : abs a * abs a = a * a :=
abs.by_cases rfl !neg_mul_neg
theorem abs_mul_self (a : A) : abs (a * a) = a * a :=
by rewrite [abs_mul, abs_mul_abs_self]
theorem sub_le_of_abs_sub_le_left (H : abs (a - b) ≤ c) : b - c ≤ a :=
if Hz : 0 ≤ a - b then
(calc
a ≥ b : (iff.mp !sub_nonneg_iff_le) Hz
... ≥ b - c : sub_le_of_nonneg _ (le.trans !abs_nonneg H))
else
(have Habs : b - a ≤ c, by rewrite [abs_of_neg (lt_of_not_ge Hz) at H, neg_sub at H]; apply H,
have Habs' : b ≤ c + a, from (iff.mpr !le_add_iff_sub_right_le) Habs,
(iff.mp !le_add_iff_sub_left_le) Habs')
theorem sub_le_of_abs_sub_le_right (H : abs (a - b) ≤ c) : a - c ≤ b :=
sub_le_of_abs_sub_le_left (!abs_sub ▸ H)
theorem sub_lt_of_abs_sub_lt_left (H : abs (a - b) < c) : b - c < a :=
if Hz : 0 ≤ a - b then
(calc
a ≥ b : (iff.mp !sub_nonneg_iff_le) Hz
... > b - c : sub_lt_of_pos _ (lt_of_le_of_lt !abs_nonneg H))
else
(have Habs : b - a < c, by rewrite [abs_of_neg (lt_of_not_ge Hz) at H, neg_sub at H]; apply H,
have Habs' : b < c + a, from lt_add_of_sub_lt_right Habs,
sub_lt_left_of_lt_add Habs')
theorem sub_lt_of_abs_sub_lt_right (H : abs (a - b) < c) : a - c < b :=
sub_lt_of_abs_sub_lt_left (!abs_sub ▸ H)
theorem abs_sub_square (a b : A) : abs (a - b) * abs (a - b) = a * a + b * b - (1 + 1) * a * b :=
begin
rewrite [abs_mul_abs_self, *mul_sub_left_distrib, *mul_sub_right_distrib,
sub_eq_add_neg (a*b), sub_add_eq_sub_sub, sub_neg_eq_add, *right_distrib, sub_add_eq_sub_sub, *one_mul,
*add.assoc, {_ + b * b}add.comm, *sub_eq_add_neg],
rewrite [{a*a + b*b}add.comm],
rewrite [mul.comm b a, *add.assoc]
end
theorem abs_abs_sub_abs_le_abs_sub (a b : A) : abs (abs a - abs b) ≤ abs (a - b) :=
begin
apply nonneg_le_nonneg_of_squares_le,
repeat apply abs_nonneg,
rewrite [*abs_sub_square, *abs_abs, *abs_mul_abs_self],
apply sub_le_sub_left,
rewrite *mul.assoc,
apply mul_le_mul_of_nonneg_left,
rewrite -abs_mul,
apply le_abs_self,
apply le_of_lt,
apply add_pos,
apply zero_lt_one,
apply zero_lt_one
end
lemma eq_zero_of_mul_self_add_mul_self_eq_zero {x y : A} (H : x * x + y * y = 0) : x = 0 :=
have x * x ≤ (0 : A), from calc
x * x ≤ x * x + y * y : le_add_of_nonneg_right (mul_self_nonneg y)
... = 0 : H,
eq_zero_of_mul_self_eq_zero (le.antisymm this (mul_self_nonneg x))
end
/- TODO: Multiplication and one, starting with mult_right_le_one_le. -/
namespace norm_num
theorem pos_bit0_helper [s : linear_ordered_semiring A] (a : A) (H : a > 0) : bit0 a > 0 :=
by rewrite ↑bit0; apply add_pos H H
theorem nonneg_bit0_helper [s : linear_ordered_semiring A] (a : A) (H : a ≥ 0) : bit0 a ≥ 0 :=
by rewrite ↑bit0; apply add_nonneg H H
theorem pos_bit1_helper [s : linear_ordered_semiring A] (a : A) (H : a ≥ 0) : bit1 a > 0 :=
begin
rewrite ↑bit1,
apply add_pos_of_nonneg_of_pos,
apply nonneg_bit0_helper _ H,
apply zero_lt_one
end
theorem nonneg_bit1_helper [s : linear_ordered_semiring A] (a : A) (H : a ≥ 0) : bit1 a ≥ 0 :=
by apply le_of_lt; apply pos_bit1_helper _ H
theorem nonzero_of_pos_helper [s : linear_ordered_semiring A] (a : A) (H : a > 0) : a ≠ 0 :=
ne_of_gt H
theorem nonzero_of_neg_helper [s : linear_ordered_ring A] (a : A) (H : a ≠ 0) : -a ≠ 0 :=
begin intro Ha, apply H, apply eq_of_neg_eq_neg, rewrite neg_zero, exact Ha end
end norm_num
|
63c8812d956ea665295930448bdaf43b4b3578cd | 05f637fa14ac28031cb1ea92086a0f4eb23ff2b1 | /tests/lean/univ.lean | 9906ce40f111478d6150fd42e48ac8f03ca95c55 | [
"Apache-2.0"
] | permissive | codyroux/lean0.1 | 1ce92751d664aacff0529e139083304a7bbc8a71 | 0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef | refs/heads/master | 1,610,830,535,062 | 1,402,150,480,000 | 1,402,150,480,000 | 19,588,851 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 777 | lean | universe M1 >= 1
universe U >= M1 + 1
definition TypeM1 := (Type M1)
universe Z ≥ M1+3
(*
local env = get_environment()
assert(env:get_universe_distance("Z", "M1") == 3)
assert(not env:get_universe_distance("Z", "U"))
*)
(*
local env = get_environment()
assert(env:get_universe_distance("Z", "M1") == 3)
assert(not env:get_universe_distance("Z", "U"))
*)
universe Z1 ≥ M1 + 1073741824.
universe Z2 ≥ Z1 + 1073741824.
universe U1
universe U2 ≥ U1 + 1
universe U3 ≥ U1 + 1
universe U4 ≥ U2 + 1
universe U4 ≥ U3 + 3
(*
local env = get_environment()
assert(env:get_universe_distance("U4", "U1") == 4)
assert(env:get_universe_distance("U4", "U3") == 3)
assert(env:get_universe_distance("U4", "U2") == 1)
*)
universe U1 ≥ U4.
universe Z >= U.
universe Z >= U + 1. |
770db4153aa3ef1f737235c1a0eea8681ae4fd8f | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/category_theory/limits/small_complete.lean | da8df06c0cc86af8398eeb5519887db31c5d9fdd | [
"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 | 2,044 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import category_theory.limits.shapes.products
import set_theory.cardinal
/-!
# Any small complete category is a preorder
We show that any small category which has all (small) limits is a preorder: In particular, we show
that if a small category `C` in universe `u` has products of size `u`, then for any `X Y : C`
there is at most one morphism `X ⟶ Y`.
Note that in Lean, a preorder category is strictly one where the morphisms are in `Prop`, so
we instead show that the homsets are subsingleton.
## References
* https://ncatlab.org/nlab/show/complete+small+category#in_classical_logic
## Tags
small complete, preorder, Freyd
-/
namespace category_theory
open category limits
universe u
variables {C : Type u} [small_category C] [has_products C]
/--
A small category with products is a thin category.
in Lean, a preorder category is one where the morphisms are in Prop, which is weaker than the usual
notion of a preorder/thin category which says that each homset is subsingleton; we show the latter
rather than providing a `preorder C` instance.
-/
instance {X Y : C} : subsingleton (X ⟶ Y) :=
⟨λ r s,
begin
classical,
by_contra r_ne_s,
have z : (2 : cardinal) ≤ cardinal.mk (X ⟶ Y),
{ rw cardinal.two_le_iff,
exact ⟨_, _, r_ne_s⟩ },
let md := Σ (Z W : C), Z ⟶ W,
let α := cardinal.mk md,
apply not_le_of_lt (cardinal.cantor α),
let yp : C := ∏ (λ (f : md), Y),
transitivity (cardinal.mk (X ⟶ yp)),
{ apply le_trans (cardinal.power_le_power_right z),
rw cardinal.power_def,
apply le_of_eq,
rw cardinal.eq,
refine ⟨⟨pi.lift, λ f k, f ≫ pi.π _ k, _, _⟩⟩,
{ intros f,
ext k,
simp },
{ intros f,
ext,
simp } },
{ apply cardinal.mk_le_of_injective _,
{ intro f,
exact ⟨_, _, f⟩ },
{ rintro f g k,
cases k,
refl } },
end⟩
end category_theory
|
077508e2a407b0586af118db9af0504d112f3129 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/krivine.lean | 4ff5785dd0243ed7140d13c6c05ed94604cbfdcf | [
"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,640 | lean | inductive KrivineInstruction
| Access (n: Nat)
| Grab (next: KrivineInstruction)
| Push (next: KrivineInstruction) (continuation: KrivineInstruction)
inductive KrivineClosure
| pair (i: KrivineInstruction) (e: List KrivineClosure)
namespace Ex1
def KrivineEnv := List KrivineClosure
-- We need to define a `SizeOf` instance for `KrivineEnv`. Otherwise, we cannot use the auto-generated well-founded relation in
-- recursive definitions.
noncomputable instance : SizeOf KrivineEnv := inferInstanceAs (SizeOf (List KrivineClosure))
-- We also need a `simp` theorem
@[simp] theorem KrivineEnv.sizeOf_spec (env : KrivineEnv) : sizeOf env = sizeOf (α := List KrivineClosure) env := rfl
-- It would be great to have a `deriving SizeOf` for definitions such as `KrivineEnv` above.
def KrivineEnv.depth (env : KrivineEnv) : Nat :=
match env with
| [] => 0
| KrivineClosure.pair u e :: closures => Nat.max (1 + depth e) (depth closures)
end Ex1
namespace Ex2
-- Same example, but we use `abbrev` to define `KrivineEnv`. Thus, we inherit the `SizeOf` instance for `List`s.
abbrev KrivineEnv := List KrivineClosure
def KrivineEnv.depth (env : KrivineEnv) : Nat :=
match env with
| [] => 0
| KrivineClosure.pair u e :: closures => Nat.max (1 + depth e) (depth closures)
end Ex2
namespace Ex3
-- Same example, but using `structure` instead of `def`
structure KrivineEnv where
env : List KrivineClosure
def KrivineEnv.depth (env : KrivineEnv) : Nat :=
match env with
| { env := [] } => 0
| { env := KrivineClosure.pair u e :: closures } => Nat.max (1 + depth { env := e }) (depth { env := closures })
end Ex3
|
cf3b923ba8b59850a041812dc47fd1b3aff45364 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /tests/lean/funInfoBug.lean | a3efb6ba0959f995b6af89337883fcf4c08cb02b | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 349 | lean | import Std.Data.AssocList
def l : List (Prod Nat Nat) := [(1, 1), (2, 2)]
#eval l -- works
def Std.AssocList.ToList : AssocList α β → List (α × β)
| AssocList.nil => []
| AssocList.cons k v t => (k, v) :: ToList t
instance [Repr α] [Repr β] : Repr (Std.AssocList α β) where
reprPrec f _ := repr f.ToList
#reduce (l.toAssocList)
|
24a96f03929b585889c5325a3559944e2ae8c58a | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/data/nat/count.lean | 86e5626ad2acc77898fcd590cecd592a1e85ea13 | [
"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 | 4,730 | lean | /-
Copyright (c) 2021 Vladimir Goryachev. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies, Vladimir Goryachev, Kyle Miller, Scott Morrison, Eric Rodriguez
-/
import data.list.basic
import data.nat.prime
import set_theory.fincard
/-!
# Counting on ℕ
This file defines the `count` function, which gives, for any predicate on the natural numbers,
"how many numbers under `k` satisfy this predicate?".
We then prove several expected lemmas about `count`, relating it to the cardinality of other
objects, and helping to evaluate it for specific `k`.
-/
open finset
namespace nat
variable (p : ℕ → Prop)
section count
variable [decidable_pred p]
/-- Count the number of naturals `k < n` satisfying `p k`. -/
def count (n : ℕ) : ℕ := (list.range n).countp p
@[simp] lemma count_zero : count p 0 = 0 :=
by rw [count, list.range_zero, list.countp]
/-- A fintype instance for the set relevant to `nat.count`. Locally an instance in locale `count` -/
def count_set.fintype (n : ℕ) : fintype {i // i < n ∧ p i} :=
begin
apply fintype.of_finset ((finset.range n).filter p),
intro x,
rw [mem_filter, mem_range],
refl,
end
localized "attribute [instance] nat.count_set.fintype" in count
lemma count_eq_card_filter_range (n : ℕ) : count p n = ((range n).filter p).card :=
by { rw [count, list.countp_eq_length_filter], refl, }
/-- `count p n` can be expressed as the cardinality of `{k // k < n ∧ p k}`. -/
lemma count_eq_card_fintype (n : ℕ) : count p n = fintype.card {k : ℕ // k < n ∧ p k} :=
by { rw [count_eq_card_filter_range, ←fintype.card_of_finset, ←count_set.fintype], refl, }
lemma count_succ (n : ℕ) : count p (n + 1) = count p n + (if p n then 1 else 0) :=
by split_ifs; simp [count, list.range_succ, h]
@[mono] lemma count_monotone : monotone (count p) :=
monotone_nat_of_le_succ $ λ n, by by_cases h : p n; simp [count_succ, h]
lemma count_add (a b : ℕ) : count p (a + b) = count p a + count (λ k, p (a + k)) b :=
begin
have : disjoint ((range a).filter p) (((range b).map $ add_left_embedding a).filter p),
{ intros x hx,
simp_rw [inf_eq_inter, mem_inter, mem_filter, mem_map, mem_range] at hx,
obtain ⟨⟨hx, _⟩, ⟨c, _, rfl⟩, _⟩ := hx,
exact (self_le_add_right _ _).not_lt hx },
simp_rw [count_eq_card_filter_range, range_add, filter_union, card_disjoint_union this,
map_filter, add_left_embedding, card_map], refl,
end
lemma count_add' (a b : ℕ) : count p (a + b) = count (λ k, p (k + b)) a + count p b :=
by { rw [add_comm, count_add, add_comm], simp_rw [add_comm b] }
lemma count_one : count p 1 = if p 0 then 1 else 0 := by simp [count_succ]
lemma count_succ' (n : ℕ) : count p (n + 1) = count (λ k, p (k + 1)) n + if p 0 then 1 else 0 :=
by rw [count_add', count_one]
variables {p}
@[simp] lemma count_lt_count_succ_iff {n : ℕ} : count p n < count p (n + 1) ↔ p n :=
by by_cases h : p n; simp [count_succ, h]
lemma count_succ_eq_succ_count_iff {n : ℕ} : count p (n + 1) = count p n + 1 ↔ p n :=
by by_cases h : p n; simp [h, count_succ]
lemma count_succ_eq_count_iff {n : ℕ} : count p (n + 1) = count p n ↔ ¬p n :=
by by_cases h : p n; simp [h, count_succ]
alias count_succ_eq_succ_count_iff ↔ _ count_succ_eq_succ_count
alias count_succ_eq_count_iff ↔ _ count_succ_eq_count
lemma count_le_cardinal (n : ℕ) : (count p n : cardinal) ≤ cardinal.mk {k | p k} :=
begin
rw [count_eq_card_fintype, ← cardinal.mk_fintype],
exact cardinal.mk_subtype_mono (λ x hx, hx.2),
end
lemma lt_of_count_lt_count {a b : ℕ} (h : count p a < count p b) : a < b :=
(count_monotone p).reflect_lt h
lemma count_strict_mono {m n : ℕ} (hm : p m) (hmn : m < n) : count p m < count p n :=
(count_lt_count_succ_iff.2 hm).trans_le $ count_monotone _ (nat.succ_le_iff.2 hmn)
lemma count_injective {m n : ℕ} (hm : p m) (hn : p n) (heq : count p m = count p n) : m = n :=
begin
by_contra,
wlog hmn : m < n,
{ exact ne.lt_or_lt h },
{ simpa [heq] using count_strict_mono hm hmn }
end
lemma count_le_card (hp : (set_of p).finite) (n : ℕ) : count p n ≤ hp.to_finset.card :=
begin
rw count_eq_card_filter_range,
exact finset.card_mono (λ x hx, hp.mem_to_finset.2 (mem_filter.1 hx).2)
end
lemma count_lt_card {n : ℕ} (hp : (set_of p).finite) (hpn : p n) :
count p n < hp.to_finset.card :=
(count_lt_count_succ_iff.2 hpn).trans_le (count_le_card hp _)
variable {q : ℕ → Prop}
variable [decidable_pred q]
lemma count_mono_left {n : ℕ} (hpq : ∀ k, p k → q k) : count p n ≤ count q n :=
begin
simp only [count_eq_card_filter_range],
exact card_le_of_subset ((range n).monotone_filter_right hpq),
end
end count
end nat
|
ab193d9ed80845c775fba1f36b62a789ac018284 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/floatOptParam.lean | de508a773a780705e7ccadf2e93eaafc90582c06 | [
"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 | 63 | lean | def foo (a : Float := 0.0) (b : Float := 1.0) : Float := a - b
|
206a762be2e50d41d85648ee9d56c1ea54ac3313 | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/algebra/ordered_ring.lean | 33349a2d6c7deeff5b42afa0d7983dcca80fa95a | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 69,409 | 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, Mario Carneiro
-/
import algebra.invertible
import algebra.ordered_group
import data.set.intervals.basic
/-!
# Ordered rings and semirings
This file develops the basics of ordered (semi)rings.
Each typeclass here comprises
* an algebraic class (`semiring`, `comm_semiring`, `ring`, `comm_ring`)
* an order class (`partial_order`, `linear_order`)
* assumptions on how both interact ((strict) monotonicity, canonicity)
For short,
* "`+` respects `≤`" means "monotonicity of addition"
* "`*` respects `<`" means "strict monotonicity of multiplication by a positive number".
## Typeclasses
* `ordered_semiring`: Semiring with a partial order such that `+` respects `≤` and `*` respects `<`.
* `ordered_comm_semiring`: Commutative semiring with a partial order such that `+` respects `≤` and
`*` respects `<`.
* `ordered_ring`: Ring with a partial order such that `+` respects `≤` and `*` respects `<`.
* `ordered_comm_ring`: Commutative ring with a partial order such that `+` respects `≤` and
`*` respects `<`.
* `linear_ordered_semiring`: Semiring with a linear order such that `+` respects `≤` and
`*` respects `<`.
* `linear_ordered_ring`: Ring with a linear order such that `+` respects `≤` and `*` respects `<`.
* `linear_ordered_comm_ring`: Commutative ring with a linear order such that `+` respects `≤` and
`*` respects `<`.
* `canonically_ordered_comm_semiring`: Commutative semiring with a partial order such that `+`
respects `≤`, `*` respects `<`, and `a ≤ b ↔ ∃ c, b = a + c`.
and some typeclasses to define ordered rings by specifying their nonegative elements:
* `nonneg_ring`: To define `ordered_ring`s.
* `linear_nonneg_ring`: To define `linear_ordered_ring`s.
## Hierarchy
The hardest part of proving order lemmas might be to figure out the correct generality and its
corresponding typeclass. Here's an attempt at demystifying it. For each typeclass, we list its
immediate predecessors and what conditions are added to each of them.
* `ordered_semiring`
- `ordered_cancel_add_comm_monoid` & multiplication & `*` respects `<`
- `semiring` & partial order structure & `+` respects `≤` & `*` respects `<`
* `ordered_comm_semiring`
- `ordered_semiring` & commutativity of multiplication
- `comm_semiring` & partial order structure & `+` respects `≤` & `*` respects `<`
* `ordered_ring`
- `ordered_semiring` & additive inverses
- `ordered_add_comm_group` & multiplication & `*` respects `<`
- `ring` & partial order structure & `+` respects `≤` & `*` respects `<`
* `ordered_comm_ring`
- `ordered_ring` & commutativity of multiplication
- `ordered_comm_semiring` & additive inverses
- `comm_ring` & partial order structure & `+` respects `≤` & `*` respects `<`
* `linear_ordered_semiring`
- `ordered_semiring` & totality of the order & nontriviality
- `linear_ordered_add_comm_monoid` & multiplication & nontriviality & `*` respects `<`
* `linear_ordered_ring`
- `ordered_ring` & totality of the order & nontriviality
- `linear_ordered_semiring` & additive inverses
- `linear_ordered_add_comm_group` & multiplication & `*` respects `<`
- `domain` & linear order structure
* `linear_ordered_comm_ring`
- `ordered_comm_ring` & totality of the order & nontriviality
- `linear_ordered_ring` & commutativity of multiplication
- `integral_domain` & linear order structure
* `canonically_ordered_comm_semiring`
- `canonically_ordered_add_monoid` & multiplication & `*` respects `<` & no zero divisors
- `comm_semiring` & `a ≤ b ↔ ∃ c, b = a + c` & no zero divisors
## TODO
We're still missing some typeclasses, like
* `linear_ordered_comm_semiring`
* `canonically_ordered_semiring`
They have yet to come up in practice.
-/
set_option old_structure_cmd true
universe u
variable {α : Type u}
lemma add_one_le_two_mul [preorder α] [semiring α] [covariant_class α α (+) (≤)]
{a : α} (a1 : 1 ≤ a) :
a + 1 ≤ 2 * a :=
calc a + 1 ≤ a + a : add_le_add_left a1 a
... = 2 * a : (two_mul _).symm
/-- An `ordered_semiring α` is a semiring `α` with a partial order such that
addition is monotone and multiplication by a positive number is strictly monotone. -/
@[protect_proj]
class ordered_semiring (α : Type u) extends semiring α, ordered_cancel_add_comm_monoid α :=
(zero_le_one : 0 ≤ (1 : α))
(mul_lt_mul_of_pos_left : ∀ a b c : α, a < b → 0 < c → c * a < c * b)
(mul_lt_mul_of_pos_right : ∀ a b c : α, a < b → 0 < c → a * c < b * c)
section ordered_semiring
variables [ordered_semiring α] {a b c d : α}
@[simp] lemma zero_le_one : 0 ≤ (1:α) :=
ordered_semiring.zero_le_one
lemma zero_le_two : 0 ≤ (2:α) :=
add_nonneg zero_le_one zero_le_one
lemma one_le_two : 1 ≤ (2:α) :=
calc (1:α) = 0 + 1 : (zero_add _).symm
... ≤ 1 + 1 : add_le_add_right zero_le_one _
section nontrivial
variables [nontrivial α]
@[simp] lemma zero_lt_one : 0 < (1 : α) :=
lt_of_le_of_ne zero_le_one zero_ne_one
lemma zero_lt_two : 0 < (2:α) := add_pos zero_lt_one zero_lt_one
@[field_simps] lemma two_ne_zero : (2:α) ≠ 0 :=
ne.symm (ne_of_lt zero_lt_two)
lemma one_lt_two : 1 < (2:α) :=
calc (2:α) = 1+1 : one_add_one_eq_two
... > 1+0 : add_lt_add_left zero_lt_one _
... = 1 : add_zero 1
lemma zero_lt_three : 0 < (3:α) := add_pos zero_lt_two zero_lt_one
lemma zero_lt_four : 0 < (4:α) := add_pos zero_lt_two zero_lt_two
end nontrivial
lemma mul_lt_mul_of_pos_left (h₁ : a < b) (h₂ : 0 < c) : c * a < c * b :=
ordered_semiring.mul_lt_mul_of_pos_left a b c h₁ h₂
lemma mul_lt_mul_of_pos_right (h₁ : a < b) (h₂ : 0 < c) : a * c < b * c :=
ordered_semiring.mul_lt_mul_of_pos_right a b c h₁ h₂
-- See Note [decidable namespace]
protected lemma decidable.mul_le_mul_of_nonneg_left [@decidable_rel α (≤)]
(h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b :=
begin
by_cases ba : b ≤ a, { simp [ba.antisymm h₁] },
by_cases c0 : c ≤ 0, { simp [c0.antisymm h₂] },
exact (mul_lt_mul_of_pos_left (h₁.lt_of_not_le ba) (h₂.lt_of_not_le c0)).le,
end
lemma mul_le_mul_of_nonneg_left : a ≤ b → 0 ≤ c → c * a ≤ c * b :=
by classical; exact decidable.mul_le_mul_of_nonneg_left
-- See Note [decidable namespace]
protected lemma decidable.mul_le_mul_of_nonneg_right [@decidable_rel α (≤)]
(h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c :=
begin
by_cases ba : b ≤ a, { simp [ba.antisymm h₁] },
by_cases c0 : c ≤ 0, { simp [c0.antisymm h₂] },
exact (mul_lt_mul_of_pos_right (h₁.lt_of_not_le ba) (h₂.lt_of_not_le c0)).le,
end
lemma mul_le_mul_of_nonneg_right : a ≤ b → 0 ≤ c → a * c ≤ b * c :=
by classical; exact decidable.mul_le_mul_of_nonneg_right
-- TODO: there are four variations, depending on which variables we assume to be nonneg
-- See Note [decidable namespace]
protected lemma decidable.mul_le_mul [@decidable_rel α (≤)]
(hac : a ≤ c) (hbd : b ≤ d) (nn_b : 0 ≤ b) (nn_c : 0 ≤ c) : a * b ≤ c * d :=
calc
a * b ≤ c * b : decidable.mul_le_mul_of_nonneg_right hac nn_b
... ≤ c * d : decidable.mul_le_mul_of_nonneg_left hbd nn_c
lemma mul_le_mul : a ≤ c → b ≤ d → 0 ≤ b → 0 ≤ c → a * b ≤ c * d :=
by classical; exact decidable.mul_le_mul
-- See Note [decidable namespace]
protected lemma decidable.mul_nonneg_le_one_le {α : Type*} [ordered_semiring α]
[@decidable_rel α (≤)] {a b c : α}
(h₁ : 0 ≤ c) (h₂ : a ≤ c) (h₃ : 0 ≤ b) (h₄ : b ≤ 1) : a * b ≤ c :=
by simpa only [mul_one] using decidable.mul_le_mul h₂ h₄ h₃ h₁
lemma mul_nonneg_le_one_le {α : Type*} [ordered_semiring α] {a b c : α} :
0 ≤ c → a ≤ c → 0 ≤ b → b ≤ 1 → a * b ≤ c :=
by classical; exact decidable.mul_nonneg_le_one_le
-- See Note [decidable namespace]
protected lemma decidable.mul_nonneg [@decidable_rel α (≤)]
(ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b :=
have h : 0 * b ≤ a * b, from decidable.mul_le_mul_of_nonneg_right ha hb,
by rwa [zero_mul] at h
lemma mul_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a * b := by classical; exact decidable.mul_nonneg
-- See Note [decidable namespace]
protected lemma decidable.mul_nonpos_of_nonneg_of_nonpos [@decidable_rel α (≤)]
(ha : 0 ≤ a) (hb : b ≤ 0) : a * b ≤ 0 :=
have h : a * b ≤ a * 0, from decidable.mul_le_mul_of_nonneg_left hb ha,
by rwa mul_zero at h
lemma mul_nonpos_of_nonneg_of_nonpos : 0 ≤ a → b ≤ 0 → a * b ≤ 0 :=
by classical; exact decidable.mul_nonpos_of_nonneg_of_nonpos
-- See Note [decidable namespace]
protected lemma decidable.mul_nonpos_of_nonpos_of_nonneg [@decidable_rel α (≤)]
(ha : a ≤ 0) (hb : 0 ≤ b) : a * b ≤ 0 :=
have h : a * b ≤ 0 * b, from decidable.mul_le_mul_of_nonneg_right ha hb,
by rwa zero_mul at h
lemma mul_nonpos_of_nonpos_of_nonneg : a ≤ 0 → 0 ≤ b → a * b ≤ 0 :=
by classical; exact decidable.mul_nonpos_of_nonpos_of_nonneg
-- See Note [decidable namespace]
protected lemma decidable.mul_lt_mul [@decidable_rel α (≤)]
(hac : a < c) (hbd : b ≤ d) (pos_b : 0 < b) (nn_c : 0 ≤ c) : a * b < c * d :=
calc
a * b < c * b : mul_lt_mul_of_pos_right hac pos_b
... ≤ c * d : decidable.mul_le_mul_of_nonneg_left hbd nn_c
lemma mul_lt_mul : a < c → b ≤ d → 0 < b → 0 ≤ c → a * b < c * d :=
by classical; exact decidable.mul_lt_mul
-- See Note [decidable namespace]
protected lemma decidable.mul_lt_mul' [@decidable_rel α (≤)]
(h1 : a ≤ c) (h2 : b < d) (h3 : 0 ≤ b) (h4 : 0 < c) : a * b < c * d :=
calc
a * b ≤ c * b : decidable.mul_le_mul_of_nonneg_right h1 h3
... < c * d : mul_lt_mul_of_pos_left h2 h4
lemma mul_lt_mul' : a ≤ c → b < d → 0 ≤ b → 0 < c → a * b < c * d :=
by classical; exact decidable.mul_lt_mul'
lemma mul_pos (ha : 0 < a) (hb : 0 < b) : 0 < a * b :=
have h : 0 * b < a * b, from mul_lt_mul_of_pos_right ha hb,
by rwa zero_mul at h
lemma mul_neg_of_pos_of_neg (ha : 0 < a) (hb : b < 0) : a * b < 0 :=
have h : a * b < a * 0, from mul_lt_mul_of_pos_left hb ha,
by rwa mul_zero at h
lemma mul_neg_of_neg_of_pos (ha : a < 0) (hb : 0 < b) : a * b < 0 :=
have h : a * b < 0 * b, from mul_lt_mul_of_pos_right ha hb,
by rwa zero_mul at h
-- See Note [decidable namespace]
protected lemma decidable.mul_self_lt_mul_self [@decidable_rel α (≤)]
(h1 : 0 ≤ a) (h2 : a < b) : a * a < b * b :=
decidable.mul_lt_mul' h2.le h2 h1 $ h1.trans_lt h2
lemma mul_self_lt_mul_self (h1 : 0 ≤ a) (h2 : a < b) : a * a < b * b :=
mul_lt_mul' h2.le h2 h1 $ h1.trans_lt h2
-- See Note [decidable namespace]
protected lemma decidable.strict_mono_incr_on_mul_self [@decidable_rel α (≤)] :
strict_mono_incr_on (λ x : α, x * x) (set.Ici 0) :=
λ x hx y hy hxy, decidable.mul_self_lt_mul_self hx hxy
lemma strict_mono_incr_on_mul_self : strict_mono_incr_on (λ x : α, x * x) (set.Ici 0) :=
λ x hx y hy hxy, mul_self_lt_mul_self hx hxy
-- See Note [decidable namespace]
protected lemma decidable.mul_self_le_mul_self [@decidable_rel α (≤)]
(h1 : 0 ≤ a) (h2 : a ≤ b) : a * a ≤ b * b :=
decidable.mul_le_mul h2 h2 h1 $ h1.trans h2
lemma mul_self_le_mul_self (h1 : 0 ≤ a) (h2 : a ≤ b) : a * a ≤ b * b :=
mul_le_mul h2 h2 h1 $ h1.trans h2
-- See Note [decidable namespace]
protected lemma decidable.mul_lt_mul'' [@decidable_rel α (≤)]
(h1 : a < c) (h2 : b < d) (h3 : 0 ≤ a) (h4 : 0 ≤ b) : a * b < c * d :=
h4.lt_or_eq_dec.elim
(λ b0, decidable.mul_lt_mul h1 h2.le b0 $ h3.trans h1.le)
(λ b0, by rw [← b0, mul_zero]; exact
mul_pos (h3.trans_lt h1) (h4.trans_lt h2))
lemma mul_lt_mul'' : a < c → b < d → 0 ≤ a → 0 ≤ b → a * b < c * d :=
by classical; exact decidable.mul_lt_mul''
-- See Note [decidable namespace]
protected lemma decidable.le_mul_of_one_le_right [@decidable_rel α (≤)]
(hb : 0 ≤ b) (h : 1 ≤ a) : b ≤ b * a :=
suffices b * 1 ≤ b * a, by rwa mul_one at this,
decidable.mul_le_mul_of_nonneg_left h hb
lemma le_mul_of_one_le_right : 0 ≤ b → 1 ≤ a → b ≤ b * a :=
by classical; exact decidable.le_mul_of_one_le_right
-- See Note [decidable namespace]
protected lemma decidable.le_mul_of_one_le_left [@decidable_rel α (≤)]
(hb : 0 ≤ b) (h : 1 ≤ a) : b ≤ a * b :=
suffices 1 * b ≤ a * b, by rwa one_mul at this,
decidable.mul_le_mul_of_nonneg_right h hb
lemma le_mul_of_one_le_left : 0 ≤ b → 1 ≤ a → b ≤ a * b :=
by classical; exact decidable.le_mul_of_one_le_left
-- See Note [decidable namespace]
protected lemma decidable.lt_mul_of_one_lt_right [@decidable_rel α (≤)]
(hb : 0 < b) (h : 1 < a) : b < b * a :=
suffices b * 1 < b * a, by rwa mul_one at this,
decidable.mul_lt_mul' (le_refl _) h zero_le_one hb
lemma lt_mul_of_one_lt_right : 0 < b → 1 < a → b < b * a :=
by classical; exact decidable.lt_mul_of_one_lt_right
-- See Note [decidable namespace]
protected lemma decidable.lt_mul_of_one_lt_left [@decidable_rel α (≤)]
(hb : 0 < b) (h : 1 < a) : b < a * b :=
suffices 1 * b < a * b, by rwa one_mul at this,
decidable.mul_lt_mul h (le_refl _) hb (zero_le_one.trans h.le)
lemma lt_mul_of_one_lt_left : 0 < b → 1 < a → b < a * b :=
by classical; exact decidable.lt_mul_of_one_lt_left
-- See Note [decidable namespace]
protected lemma decidable.add_le_mul_two_add [@decidable_rel α (≤)] {a b : α}
(a2 : 2 ≤ a) (b0 : 0 ≤ b) : a + (2 + b) ≤ a * (2 + b) :=
calc a + (2 + b) ≤ a + (a + a * b) :
add_le_add_left (add_le_add a2 (decidable.le_mul_of_one_le_left b0 (one_le_two.trans a2))) a
... ≤ a * (2 + b) : by rw [mul_add, mul_two, add_assoc]
lemma add_le_mul_two_add {a b : α} : 2 ≤ a → 0 ≤ b → a + (2 + b) ≤ a * (2 + b) :=
by classical; exact decidable.add_le_mul_two_add
-- See Note [decidable namespace]
protected lemma decidable.one_le_mul_of_one_le_of_one_le [@decidable_rel α (≤)]
{a b : α} (a1 : 1 ≤ a) (b1 : 1 ≤ b) : (1 : α) ≤ a * b :=
(mul_one (1 : α)).symm.le.trans (decidable.mul_le_mul a1 b1 zero_le_one (zero_le_one.trans a1))
lemma one_le_mul_of_one_le_of_one_le {a b : α} : 1 ≤ a → 1 ≤ b → (1 : α) ≤ a * b :=
by classical; exact decidable.one_le_mul_of_one_le_of_one_le
/-- Pullback an `ordered_semiring` under an injective map.
See note [reducible non-instances]. -/
@[reducible]
def function.injective.ordered_semiring {β : Type*}
[has_zero β] [has_one β] [has_add β] [has_mul β]
(f : β → α) (hf : function.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) :
ordered_semiring β :=
{ zero_le_one := show f 0 ≤ f 1, by simp only [zero, one, zero_le_one],
mul_lt_mul_of_pos_left := λ a b c ab c0, show f (c * a) < f (c * b),
begin
rw [mul, mul],
refine mul_lt_mul_of_pos_left ab _,
rwa ← zero,
end,
mul_lt_mul_of_pos_right := λ a b c ab c0, show f (a * c) < f (b * c),
begin
rw [mul, mul],
refine mul_lt_mul_of_pos_right ab _,
rwa ← zero,
end,
..hf.ordered_cancel_add_comm_monoid f zero add,
..hf.semiring f zero one add mul }
section
variable [nontrivial α]
lemma bit1_pos (h : 0 ≤ a) : 0 < bit1 a :=
lt_add_of_le_of_pos (add_nonneg h h) zero_lt_one
lemma lt_add_one (a : α) : a < a + 1 :=
lt_add_of_le_of_pos le_rfl zero_lt_one
lemma lt_one_add (a : α) : a < 1 + a :=
by { rw [add_comm], apply lt_add_one }
end
lemma bit1_pos' (h : 0 < a) : 0 < bit1 a :=
begin
nontriviality,
exact bit1_pos h.le,
end
-- See Note [decidable namespace]
protected lemma decidable.one_lt_mul [@decidable_rel α (≤)]
(ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b :=
begin
nontriviality,
exact (one_mul (1 : α)) ▸ decidable.mul_lt_mul' ha hb zero_le_one (zero_lt_one.trans_le ha)
end
lemma one_lt_mul : 1 ≤ a → 1 < b → 1 < a * b :=
by classical; exact decidable.one_lt_mul
-- See Note [decidable namespace]
protected lemma decidable.mul_le_one [@decidable_rel α (≤)]
(ha : a ≤ 1) (hb' : 0 ≤ b) (hb : b ≤ 1) : a * b ≤ 1 :=
begin rw ← one_mul (1 : α), apply decidable.mul_le_mul; {assumption <|> apply zero_le_one} end
lemma mul_le_one : a ≤ 1 → 0 ≤ b → b ≤ 1 → a * b ≤ 1 :=
by classical; exact decidable.mul_le_one
-- See Note [decidable namespace]
protected lemma decidable.one_lt_mul_of_le_of_lt [@decidable_rel α (≤)]
(ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b :=
begin
nontriviality,
calc 1 = 1 * 1 : by rw one_mul
... < a * b : decidable.mul_lt_mul' ha hb zero_le_one (zero_lt_one.trans_le ha)
end
lemma one_lt_mul_of_le_of_lt : 1 ≤ a → 1 < b → 1 < a * b :=
by classical; exact decidable.one_lt_mul_of_le_of_lt
-- See Note [decidable namespace]
protected lemma decidable.one_lt_mul_of_lt_of_le [@decidable_rel α (≤)]
(ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b :=
begin
nontriviality,
calc 1 = 1 * 1 : by rw one_mul
... < a * b : decidable.mul_lt_mul ha hb zero_lt_one $ zero_le_one.trans ha.le
end
lemma one_lt_mul_of_lt_of_le : 1 < a → 1 ≤ b → 1 < a * b :=
by classical; exact decidable.one_lt_mul_of_lt_of_le
-- See Note [decidable namespace]
protected lemma decidable.mul_le_of_le_one_right [@decidable_rel α (≤)]
(ha : 0 ≤ a) (hb1 : b ≤ 1) : a * b ≤ a :=
calc a * b ≤ a * 1 : decidable.mul_le_mul_of_nonneg_left hb1 ha
... = a : mul_one a
lemma mul_le_of_le_one_right : 0 ≤ a → b ≤ 1 → a * b ≤ a :=
by classical; exact decidable.mul_le_of_le_one_right
-- See Note [decidable namespace]
protected lemma decidable.mul_le_of_le_one_left [@decidable_rel α (≤)]
(hb : 0 ≤ b) (ha1 : a ≤ 1) : a * b ≤ b :=
calc a * b ≤ 1 * b : decidable.mul_le_mul ha1 le_rfl hb zero_le_one
... = b : one_mul b
lemma mul_le_of_le_one_left : 0 ≤ b → a ≤ 1 → a * b ≤ b :=
by classical; exact decidable.mul_le_of_le_one_left
-- See Note [decidable namespace]
protected lemma decidable.mul_lt_one_of_nonneg_of_lt_one_left [@decidable_rel α (≤)]
(ha0 : 0 ≤ a) (ha : a < 1) (hb : b ≤ 1) : a * b < 1 :=
calc a * b ≤ a : decidable.mul_le_of_le_one_right ha0 hb
... < 1 : ha
lemma mul_lt_one_of_nonneg_of_lt_one_left : 0 ≤ a → a < 1 → b ≤ 1 → a * b < 1 :=
by classical; exact decidable.mul_lt_one_of_nonneg_of_lt_one_left
-- See Note [decidable namespace]
protected lemma decidable.mul_lt_one_of_nonneg_of_lt_one_right [@decidable_rel α (≤)]
(ha : a ≤ 1) (hb0 : 0 ≤ b) (hb : b < 1) : a * b < 1 :=
calc a * b ≤ b : decidable.mul_le_of_le_one_left hb0 ha
... < 1 : hb
lemma mul_lt_one_of_nonneg_of_lt_one_right : a ≤ 1 → 0 ≤ b → b < 1 → a * b < 1 :=
by classical; exact decidable.mul_lt_one_of_nonneg_of_lt_one_right
end ordered_semiring
section ordered_comm_semiring
/-- An `ordered_comm_semiring α` is a commutative semiring `α` with a partial order such that
addition is monotone and multiplication by a positive number is strictly monotone. -/
@[protect_proj]
class ordered_comm_semiring (α : Type u) extends ordered_semiring α, comm_semiring α
/-- Pullback an `ordered_comm_semiring` under an injective map.
See note [reducible non-instances]. -/
@[reducible]
def function.injective.ordered_comm_semiring [ordered_comm_semiring α] {β : Type*}
[has_zero β] [has_one β] [has_add β] [has_mul β]
(f : β → α) (hf : function.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) :
ordered_comm_semiring β :=
{ ..hf.comm_semiring f zero one add mul,
..hf.ordered_semiring f zero one add mul }
end ordered_comm_semiring
/--
A `linear_ordered_semiring α` is a nontrivial semiring `α` with a linear order
such that addition is monotone and multiplication by a positive number is strictly monotone.
-/
-- It's not entirely clear we should assume `nontrivial` at this point;
-- it would be reasonable to explore changing this,
-- but be warned that the instances involving `domain` may cause
-- typeclass search loops.
@[protect_proj]
class linear_ordered_semiring (α : Type u)
extends ordered_semiring α, linear_ordered_add_comm_monoid α, nontrivial α
section linear_ordered_semiring
variables [linear_ordered_semiring α] {a b c d : α}
-- `norm_num` expects the lemma stating `0 < 1` to have a single typeclass argument
-- (see `norm_num.prove_pos_nat`).
-- Rather than working out how to relax that assumption,
-- we provide a synonym for `zero_lt_one` (which needs both `ordered_semiring α` and `nontrivial α`)
-- with only a `linear_ordered_semiring` typeclass argument.
lemma zero_lt_one' : 0 < (1 : α) := zero_lt_one
lemma lt_of_mul_lt_mul_left (h : c * a < c * b) (hc : 0 ≤ c) : a < b :=
by haveI := @linear_order.decidable_le α _; exact lt_of_not_ge
(assume h1 : b ≤ a,
have h2 : c * b ≤ c * a, from decidable.mul_le_mul_of_nonneg_left h1 hc,
h2.not_lt h)
lemma lt_of_mul_lt_mul_right (h : a * c < b * c) (hc : 0 ≤ c) : a < b :=
by haveI := @linear_order.decidable_le α _; exact lt_of_not_ge
(assume h1 : b ≤ a,
have h2 : b * c ≤ a * c, from decidable.mul_le_mul_of_nonneg_right h1 hc,
h2.not_lt h)
lemma le_of_mul_le_mul_left (h : c * a ≤ c * b) (hc : 0 < c) : a ≤ b :=
le_of_not_gt
(assume h1 : b < a,
have h2 : c * b < c * a, from mul_lt_mul_of_pos_left h1 hc,
h2.not_le h)
lemma le_of_mul_le_mul_right (h : a * c ≤ b * c) (hc : 0 < c) : a ≤ b :=
le_of_not_gt
(assume h1 : b < a,
have h2 : b * c < a * c, from mul_lt_mul_of_pos_right h1 hc,
h2.not_le h)
lemma pos_and_pos_or_neg_and_neg_of_mul_pos (hab : 0 < a * b) :
(0 < a ∧ 0 < b) ∨ (a < 0 ∧ b < 0) :=
begin
haveI := @linear_order.decidable_le α _,
rcases lt_trichotomy 0 a with (ha|rfl|ha),
{ refine or.inl ⟨ha, lt_imp_lt_of_le_imp_le (λ hb, _) hab⟩,
exact decidable.mul_nonpos_of_nonneg_of_nonpos ha.le hb },
{ rw [zero_mul] at hab, exact hab.false.elim },
{ refine or.inr ⟨ha, lt_imp_lt_of_le_imp_le (λ hb, _) hab⟩,
exact decidable.mul_nonpos_of_nonpos_of_nonneg ha.le hb }
end
lemma nonneg_and_nonneg_or_nonpos_and_nonpos_of_mul_nnonneg (hab : 0 ≤ a * b) :
(0 ≤ a ∧ 0 ≤ b) ∨ (a ≤ 0 ∧ b ≤ 0) :=
begin
haveI := @linear_order.decidable_le α _,
refine decidable.or_iff_not_and_not.2 _,
simp only [not_and, not_le], intros ab nab, apply not_lt_of_le hab _,
rcases lt_trichotomy 0 a with (ha|rfl|ha),
exacts [mul_neg_of_pos_of_neg ha (ab ha.le), ((ab le_rfl).asymm (nab le_rfl)).elim,
mul_neg_of_neg_of_pos ha (nab ha.le)]
end
lemma pos_of_mul_pos_left (h : 0 < a * b) (ha : 0 ≤ a) : 0 < b :=
((pos_and_pos_or_neg_and_neg_of_mul_pos h).resolve_right $ λ h, h.1.not_le ha).2
lemma pos_of_mul_pos_right (h : 0 < a * b) (hb : 0 ≤ b) : 0 < a :=
((pos_and_pos_or_neg_and_neg_of_mul_pos h).resolve_right $ λ h, h.2.not_le hb).1
@[simp] lemma inv_of_pos [invertible a] : 0 < ⅟a ↔ 0 < a :=
begin
have : 0 < a * ⅟a, by simp only [mul_inv_of_self, zero_lt_one],
exact ⟨λ h, pos_of_mul_pos_right this h.le, λ h, pos_of_mul_pos_left this h.le⟩
end
@[simp] lemma inv_of_nonpos [invertible a] : ⅟a ≤ 0 ↔ a ≤ 0 :=
by simp only [← not_lt, inv_of_pos]
lemma nonneg_of_mul_nonneg_left (h : 0 ≤ a * b) (h1 : 0 < a) : 0 ≤ b :=
le_of_not_gt (assume h2 : b < 0, (mul_neg_of_pos_of_neg h1 h2).not_le h)
lemma nonneg_of_mul_nonneg_right (h : 0 ≤ a * b) (h1 : 0 < b) : 0 ≤ a :=
le_of_not_gt (assume h2 : a < 0, (mul_neg_of_neg_of_pos h2 h1).not_le h)
@[simp] lemma inv_of_nonneg [invertible a] : 0 ≤ ⅟a ↔ 0 ≤ a :=
begin
have : 0 < a * ⅟a, by simp only [mul_inv_of_self, zero_lt_one],
exact ⟨λ h, (pos_of_mul_pos_right this h).le, λ h, (pos_of_mul_pos_left this h).le⟩
end
@[simp] lemma inv_of_lt_zero [invertible a] : ⅟a < 0 ↔ a < 0 :=
by simp only [← not_le, inv_of_nonneg]
@[simp] lemma inv_of_le_one [invertible a] (h : 1 ≤ a) : ⅟a ≤ 1 :=
by haveI := @linear_order.decidable_le α _; exact
mul_inv_of_self a ▸ decidable.le_mul_of_one_le_left (inv_of_nonneg.2 $ zero_le_one.trans h) h
lemma neg_of_mul_neg_left (h : a * b < 0) (h1 : 0 ≤ a) : b < 0 :=
by haveI := @linear_order.decidable_le α _; exact
lt_of_not_ge (assume h2 : b ≥ 0, (decidable.mul_nonneg h1 h2).not_lt h)
lemma neg_of_mul_neg_right (h : a * b < 0) (h1 : 0 ≤ b) : a < 0 :=
by haveI := @linear_order.decidable_le α _; exact
lt_of_not_ge (assume h2 : a ≥ 0, (decidable.mul_nonneg h2 h1).not_lt h)
lemma nonpos_of_mul_nonpos_left (h : a * b ≤ 0) (h1 : 0 < a) : b ≤ 0 :=
le_of_not_gt (assume h2 : b > 0, (mul_pos h1 h2).not_le h)
lemma nonpos_of_mul_nonpos_right (h : a * b ≤ 0) (h1 : 0 < b) : a ≤ 0 :=
le_of_not_gt (assume h2 : a > 0, (mul_pos h2 h1).not_le h)
@[simp] lemma mul_le_mul_left (h : 0 < c) : c * a ≤ c * b ↔ a ≤ b :=
by haveI := @linear_order.decidable_le α _; exact
⟨λ h', le_of_mul_le_mul_left h' h, λ h', decidable.mul_le_mul_of_nonneg_left h' h.le⟩
@[simp] lemma mul_le_mul_right (h : 0 < c) : a * c ≤ b * c ↔ a ≤ b :=
by haveI := @linear_order.decidable_le α _; exact
⟨λ h', le_of_mul_le_mul_right h' h, λ h', decidable.mul_le_mul_of_nonneg_right h' h.le⟩
@[simp] lemma mul_lt_mul_left (h : 0 < c) : c * a < c * b ↔ a < b :=
by haveI := @linear_order.decidable_le α _; exact
⟨lt_imp_lt_of_le_imp_le $ λ h', decidable.mul_le_mul_of_nonneg_left h' h.le,
λ h', mul_lt_mul_of_pos_left h' h⟩
@[simp] lemma mul_lt_mul_right (h : 0 < c) : a * c < b * c ↔ a < b :=
by haveI := @linear_order.decidable_le α _; exact
⟨lt_imp_lt_of_le_imp_le $ λ h', decidable.mul_le_mul_of_nonneg_right h' h.le,
λ h', mul_lt_mul_of_pos_right h' h⟩
@[simp] lemma zero_le_mul_left (h : 0 < c) : 0 ≤ c * b ↔ 0 ≤ b :=
by { convert mul_le_mul_left h, simp }
@[simp] lemma zero_le_mul_right (h : 0 < c) : 0 ≤ b * c ↔ 0 ≤ b :=
by { convert mul_le_mul_right h, simp }
@[simp] lemma zero_lt_mul_left (h : 0 < c) : 0 < c * b ↔ 0 < b :=
by { convert mul_lt_mul_left h, simp }
@[simp] lemma zero_lt_mul_right (h : 0 < c) : 0 < b * c ↔ 0 < b :=
by { convert mul_lt_mul_right h, simp }
lemma add_le_mul_of_left_le_right (a2 : 2 ≤ a) (ab : a ≤ b) : a + b ≤ a * b :=
have 0 < b, from
calc 0 < 2 : zero_lt_two
... ≤ a : a2
... ≤ b : ab,
calc a + b ≤ b + b : add_le_add_right ab b
... = 2 * b : (two_mul b).symm
... ≤ a * b : (mul_le_mul_right this).mpr a2
lemma add_le_mul_of_right_le_left (b2 : 2 ≤ b) (ba : b ≤ a) : a + b ≤ a * b :=
have 0 < a, from
calc 0 < 2 : zero_lt_two
... ≤ b : b2
... ≤ a : ba,
calc a + b ≤ a + a : add_le_add_left ba a
... = a * 2 : (mul_two a).symm
... ≤ a * b : (mul_le_mul_left this).mpr b2
lemma add_le_mul (a2 : 2 ≤ a) (b2 : 2 ≤ b) : a + b ≤ a * b :=
if hab : a ≤ b then add_le_mul_of_left_le_right a2 hab
else add_le_mul_of_right_le_left b2 (le_of_not_le hab)
lemma add_le_mul' (a2 : 2 ≤ a) (b2 : 2 ≤ b) : a + b ≤ b * a :=
(le_of_eq (add_comm _ _)).trans (add_le_mul b2 a2)
section
variables [nontrivial α]
@[simp] lemma bit0_le_bit0 : bit0 a ≤ bit0 b ↔ a ≤ b :=
by rw [bit0, bit0, ← two_mul, ← two_mul, mul_le_mul_left (zero_lt_two : 0 < (2:α))]
@[simp] lemma bit0_lt_bit0 : bit0 a < bit0 b ↔ a < b :=
by rw [bit0, bit0, ← two_mul, ← two_mul, mul_lt_mul_left (zero_lt_two : 0 < (2:α))]
@[simp] lemma bit1_le_bit1 : bit1 a ≤ bit1 b ↔ a ≤ b :=
(add_le_add_iff_right 1).trans bit0_le_bit0
@[simp] lemma bit1_lt_bit1 : bit1 a < bit1 b ↔ a < b :=
(add_lt_add_iff_right 1).trans bit0_lt_bit0
@[simp] lemma one_le_bit1 : (1 : α) ≤ bit1 a ↔ 0 ≤ a :=
by rw [bit1, le_add_iff_nonneg_left, bit0, ← two_mul, zero_le_mul_left (zero_lt_two : 0 < (2:α))]
@[simp] lemma one_lt_bit1 : (1 : α) < bit1 a ↔ 0 < a :=
by rw [bit1, lt_add_iff_pos_left, bit0, ← two_mul, zero_lt_mul_left (zero_lt_two : 0 < (2:α))]
@[simp] lemma zero_le_bit0 : (0 : α) ≤ bit0 a ↔ 0 ≤ a :=
by rw [bit0, ← two_mul, zero_le_mul_left (zero_lt_two : 0 < (2:α))]
@[simp] lemma zero_lt_bit0 : (0 : α) < bit0 a ↔ 0 < a :=
by rw [bit0, ← two_mul, zero_lt_mul_left (zero_lt_two : 0 < (2:α))]
end
lemma le_mul_iff_one_le_left (hb : 0 < b) : b ≤ a * b ↔ 1 ≤ a :=
suffices 1 * b ≤ a * b ↔ 1 ≤ a, by rwa one_mul at this,
mul_le_mul_right hb
lemma lt_mul_iff_one_lt_left (hb : 0 < b) : b < a * b ↔ 1 < a :=
suffices 1 * b < a * b ↔ 1 < a, by rwa one_mul at this,
mul_lt_mul_right hb
lemma le_mul_iff_one_le_right (hb : 0 < b) : b ≤ b * a ↔ 1 ≤ a :=
suffices b * 1 ≤ b * a ↔ 1 ≤ a, by rwa mul_one at this,
mul_le_mul_left hb
lemma lt_mul_iff_one_lt_right (hb : 0 < b) : b < b * a ↔ 1 < a :=
suffices b * 1 < b * a ↔ 1 < a, by rwa mul_one at this,
mul_lt_mul_left hb
theorem mul_nonneg_iff_right_nonneg_of_pos (ha : 0 < a) : 0 ≤ b * a ↔ 0 ≤ b :=
by haveI := @linear_order.decidable_le α _; exact
⟨λ h, nonneg_of_mul_nonneg_right h ha, λ h, decidable.mul_nonneg h ha.le⟩
lemma mul_le_iff_le_one_left (hb : 0 < b) : a * b ≤ b ↔ a ≤ 1 :=
⟨ λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_left hb).2 h.not_lt),
λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_left hb).1 h.not_lt) ⟩
lemma mul_lt_iff_lt_one_left (hb : 0 < b) : a * b < b ↔ a < 1 :=
⟨ λ h, lt_of_not_ge (mt (le_mul_iff_one_le_left hb).2 h.not_le),
λ h, lt_of_not_ge (mt (le_mul_iff_one_le_left hb).1 h.not_le) ⟩
lemma mul_le_iff_le_one_right (hb : 0 < b) : b * a ≤ b ↔ a ≤ 1 :=
⟨ λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_right hb).2 h.not_lt),
λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_right hb).1 h.not_lt) ⟩
lemma mul_lt_iff_lt_one_right (hb : 0 < b) : b * a < b ↔ a < 1 :=
⟨ λ h, lt_of_not_ge (mt (le_mul_iff_one_le_right hb).2 h.not_le),
λ h, lt_of_not_ge (mt (le_mul_iff_one_le_right hb).1 h.not_le) ⟩
lemma nonpos_of_mul_nonneg_left (h : 0 ≤ a * b) (hb : b < 0) : a ≤ 0 :=
le_of_not_gt (λ ha, absurd h (mul_neg_of_pos_of_neg ha hb).not_le)
lemma nonpos_of_mul_nonneg_right (h : 0 ≤ a * b) (ha : a < 0) : b ≤ 0 :=
le_of_not_gt (λ hb, absurd h (mul_neg_of_neg_of_pos ha hb).not_le)
lemma neg_of_mul_pos_left (h : 0 < a * b) (hb : b ≤ 0) : a < 0 :=
by haveI := @linear_order.decidable_le α _; exact
lt_of_not_ge (λ ha, absurd h (decidable.mul_nonpos_of_nonneg_of_nonpos ha hb).not_lt)
lemma neg_of_mul_pos_right (h : 0 < a * b) (ha : a ≤ 0) : b < 0 :=
by haveI := @linear_order.decidable_le α _; exact
lt_of_not_ge (λ hb, absurd h (decidable.mul_nonpos_of_nonpos_of_nonneg ha hb).not_lt)
@[priority 100] -- see Note [lower instance priority]
instance linear_ordered_semiring.to_no_top_order {α : Type*} [linear_ordered_semiring α] :
no_top_order α :=
⟨assume a, ⟨a + 1, lt_add_of_pos_right _ zero_lt_one⟩⟩
/-- Pullback a `linear_ordered_semiring` under an injective map.
See note [reducible non-instances]. -/
@[reducible]
def function.injective.linear_ordered_semiring {β : Type*}
[has_zero β] [has_one β] [has_add β] [has_mul β] [nontrivial β]
(f : β → α) (hf : function.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) :
linear_ordered_semiring β :=
{ ..linear_order.lift f hf,
..‹nontrivial β›,
..hf.ordered_semiring f zero one add mul }
end linear_ordered_semiring
section mono
variables {β : Type*} [linear_ordered_semiring α] [preorder β] {f g : β → α} {a : α}
lemma monotone_mul_left_of_nonneg (ha : 0 ≤ a) : monotone (λ x, a*x) :=
by haveI := @linear_order.decidable_le α _; exact
assume b c b_le_c, decidable.mul_le_mul_of_nonneg_left b_le_c ha
lemma monotone_mul_right_of_nonneg (ha : 0 ≤ a) : monotone (λ x, x*a) :=
by haveI := @linear_order.decidable_le α _; exact
assume b c b_le_c, decidable.mul_le_mul_of_nonneg_right b_le_c ha
lemma monotone.mul_const (hf : monotone f) (ha : 0 ≤ a) :
monotone (λ x, (f x) * a) :=
(monotone_mul_right_of_nonneg ha).comp hf
lemma monotone.const_mul (hf : monotone f) (ha : 0 ≤ a) :
monotone (λ x, a * (f x)) :=
(monotone_mul_left_of_nonneg ha).comp hf
lemma monotone.mul (hf : monotone f) (hg : monotone g) (hf0 : ∀ x, 0 ≤ f x) (hg0 : ∀ x, 0 ≤ g x) :
monotone (λ x, f x * g x) :=
by haveI := @linear_order.decidable_le α _; exact
λ x y h, decidable.mul_le_mul (hf h) (hg h) (hg0 x) (hf0 y)
lemma strict_mono_mul_left_of_pos (ha : 0 < a) : strict_mono (λ x, a * x) :=
assume b c b_lt_c, (mul_lt_mul_left ha).2 b_lt_c
lemma strict_mono_mul_right_of_pos (ha : 0 < a) : strict_mono (λ x, x * a) :=
assume b c b_lt_c, (mul_lt_mul_right ha).2 b_lt_c
lemma strict_mono.mul_const (hf : strict_mono f) (ha : 0 < a) :
strict_mono (λ x, (f x) * a) :=
(strict_mono_mul_right_of_pos ha).comp hf
lemma strict_mono.const_mul (hf : strict_mono f) (ha : 0 < a) :
strict_mono (λ x, a * (f x)) :=
(strict_mono_mul_left_of_pos ha).comp hf
lemma strict_mono.mul_monotone (hf : strict_mono f) (hg : monotone g) (hf0 : ∀ x, 0 ≤ f x)
(hg0 : ∀ x, 0 < g x) :
strict_mono (λ x, f x * g x) :=
by haveI := @linear_order.decidable_le α _; exact
λ x y h, decidable.mul_lt_mul (hf h) (hg h.le) (hg0 x) (hf0 y)
lemma monotone.mul_strict_mono (hf : monotone f) (hg : strict_mono g) (hf0 : ∀ x, 0 < f x)
(hg0 : ∀ x, 0 ≤ g x) :
strict_mono (λ x, f x * g x) :=
by haveI := @linear_order.decidable_le α _; exact
λ x y h, decidable.mul_lt_mul' (hf h.le) (hg h) (hg0 x) (hf0 y)
lemma strict_mono.mul (hf : strict_mono f) (hg : strict_mono g) (hf0 : ∀ x, 0 ≤ f x)
(hg0 : ∀ x, 0 ≤ g x) :
strict_mono (λ x, f x * g x) :=
by haveI := @linear_order.decidable_le α _; exact
λ x y h, decidable.mul_lt_mul'' (hf h) (hg h) (hf0 x) (hg0 x)
end mono
section linear_ordered_semiring
variables [linear_ordered_semiring α] {a b c : α}
lemma mul_max_of_nonneg (b c : α) (ha : 0 ≤ a) : a * max b c = max (a * b) (a * c) :=
(monotone_mul_left_of_nonneg ha).map_max
lemma mul_min_of_nonneg (b c : α) (ha : 0 ≤ a) : a * min b c = min (a * b) (a * c) :=
(monotone_mul_left_of_nonneg ha).map_min
lemma max_mul_of_nonneg (a b : α) (hc : 0 ≤ c) : max a b * c = max (a * c) (b * c) :=
(monotone_mul_right_of_nonneg hc).map_max
lemma min_mul_of_nonneg (a b : α) (hc : 0 ≤ c) : min a b * c = min (a * c) (b * c) :=
(monotone_mul_right_of_nonneg hc).map_min
end linear_ordered_semiring
/-- An `ordered_ring α` is a ring `α` with a partial order such that
addition is monotone and multiplication by a positive number is strictly monotone. -/
@[protect_proj]
class ordered_ring (α : Type u) extends ring α, ordered_add_comm_group α :=
(zero_le_one : 0 ≤ (1 : α))
(mul_pos : ∀ a b : α, 0 < a → 0 < b → 0 < a * b)
section ordered_ring
variables [ordered_ring α] {a b c : α}
-- See Note [decidable namespace]
protected lemma decidable.ordered_ring.mul_nonneg [@decidable_rel α (≤)]
{a b : α} (h₁ : 0 ≤ a) (h₂ : 0 ≤ b) : 0 ≤ a * b :=
begin
by_cases ha : a ≤ 0, { simp [le_antisymm ha h₁] },
by_cases hb : b ≤ 0, { simp [le_antisymm hb h₂] },
exact (le_not_le_of_lt (ordered_ring.mul_pos a b (h₁.lt_of_not_le ha) (h₂.lt_of_not_le hb))).1,
end
lemma ordered_ring.mul_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a * b :=
by classical; exact decidable.ordered_ring.mul_nonneg
-- See Note [decidable namespace]
protected lemma decidable.ordered_ring.mul_le_mul_of_nonneg_left
[@decidable_rel α (≤)] (h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b :=
begin
rw [← sub_nonneg, ← mul_sub],
exact decidable.ordered_ring.mul_nonneg h₂ (sub_nonneg.2 h₁),
end
lemma ordered_ring.mul_le_mul_of_nonneg_left : a ≤ b → 0 ≤ c → c * a ≤ c * b :=
by classical; exact decidable.ordered_ring.mul_le_mul_of_nonneg_left
-- See Note [decidable namespace]
protected lemma decidable.ordered_ring.mul_le_mul_of_nonneg_right
[@decidable_rel α (≤)] (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c :=
begin
rw [← sub_nonneg, ← sub_mul],
exact decidable.ordered_ring.mul_nonneg (sub_nonneg.2 h₁) h₂,
end
lemma ordered_ring.mul_le_mul_of_nonneg_right : a ≤ b → 0 ≤ c → a * c ≤ b * c :=
by classical; exact decidable.ordered_ring.mul_le_mul_of_nonneg_right
lemma ordered_ring.mul_lt_mul_of_pos_left (h₁ : a < b) (h₂ : 0 < c) : c * a < c * b :=
begin
rw [← sub_pos, ← mul_sub],
exact ordered_ring.mul_pos _ _ h₂ (sub_pos.2 h₁),
end
lemma ordered_ring.mul_lt_mul_of_pos_right (h₁ : a < b) (h₂ : 0 < c) : a * c < b * c :=
begin
rw [← sub_pos, ← sub_mul],
exact ordered_ring.mul_pos _ _ (sub_pos.2 h₁) h₂,
end
@[priority 100] -- see Note [lower instance priority]
instance ordered_ring.to_ordered_semiring : ordered_semiring α :=
{ mul_zero := mul_zero,
zero_mul := zero_mul,
add_left_cancel := @add_left_cancel α _,
le_of_add_le_add_left := @le_of_add_le_add_left α _ _ _,
mul_lt_mul_of_pos_left := @ordered_ring.mul_lt_mul_of_pos_left α _,
mul_lt_mul_of_pos_right := @ordered_ring.mul_lt_mul_of_pos_right α _,
..‹ordered_ring α› }
-- See Note [decidable namespace]
protected lemma decidable.mul_le_mul_of_nonpos_left [@decidable_rel α (≤)]
{a b c : α} (h : b ≤ a) (hc : c ≤ 0) : c * a ≤ c * b :=
have -c ≥ 0, from neg_nonneg_of_nonpos hc,
have -c * b ≤ -c * a, from decidable.mul_le_mul_of_nonneg_left h this,
have -(c * b) ≤ -(c * a), by rwa [← neg_mul_eq_neg_mul, ← neg_mul_eq_neg_mul] at this,
le_of_neg_le_neg this
lemma mul_le_mul_of_nonpos_left {a b c : α} : b ≤ a → c ≤ 0 → c * a ≤ c * b :=
by classical; exact decidable.mul_le_mul_of_nonpos_left
-- See Note [decidable namespace]
protected lemma decidable.mul_le_mul_of_nonpos_right [@decidable_rel α (≤)]
{a b c : α} (h : b ≤ a) (hc : c ≤ 0) : a * c ≤ b * c :=
have -c ≥ 0, from neg_nonneg_of_nonpos hc,
have b * -c ≤ a * -c, from decidable.mul_le_mul_of_nonneg_right h this,
have -(b * c) ≤ -(a * c), by rwa [← neg_mul_eq_mul_neg, ← neg_mul_eq_mul_neg] at this,
le_of_neg_le_neg this
lemma mul_le_mul_of_nonpos_right {a b c : α} : b ≤ a → c ≤ 0 → a * c ≤ b * c :=
by classical; exact decidable.mul_le_mul_of_nonpos_right
-- See Note [decidable namespace]
protected lemma decidable.mul_nonneg_of_nonpos_of_nonpos [@decidable_rel α (≤)]
{a b : α} (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a * b :=
have 0 * b ≤ a * b, from decidable.mul_le_mul_of_nonpos_right ha hb,
by rwa zero_mul at this
lemma mul_nonneg_of_nonpos_of_nonpos {a b : α} : a ≤ 0 → b ≤ 0 → 0 ≤ a * b :=
by classical; exact decidable.mul_nonneg_of_nonpos_of_nonpos
lemma mul_lt_mul_of_neg_left {a b c : α} (h : b < a) (hc : c < 0) : c * a < c * b :=
have -c > 0, from neg_pos_of_neg hc,
have -c * b < -c * a, from mul_lt_mul_of_pos_left h this,
have -(c * b) < -(c * a), by rwa [← neg_mul_eq_neg_mul, ← neg_mul_eq_neg_mul] at this,
lt_of_neg_lt_neg this
lemma mul_lt_mul_of_neg_right {a b c : α} (h : b < a) (hc : c < 0) : a * c < b * c :=
have -c > 0, from neg_pos_of_neg hc,
have b * -c < a * -c, from mul_lt_mul_of_pos_right h this,
have -(b * c) < -(a * c), by rwa [← neg_mul_eq_mul_neg, ← neg_mul_eq_mul_neg] at this,
lt_of_neg_lt_neg this
lemma mul_pos_of_neg_of_neg {a b : α} (ha : a < 0) (hb : b < 0) : 0 < a * b :=
have 0 * b < a * b, from mul_lt_mul_of_neg_right ha hb,
by rwa zero_mul at this
/-- Pullback an `ordered_ring` under an injective map.
See note [reducible non-instances]. -/
@[reducible]
def function.injective.ordered_ring {β : Type*}
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
(f : β → α) (hf : function.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) :
ordered_ring β :=
{ mul_pos := λ a b a0 b0, show f 0 < f (a * b), by { rw [zero, mul], apply mul_pos; rwa ← zero },
..hf.ordered_semiring f zero one add mul,
..hf.ring f zero one add mul neg sub }
end ordered_ring
section ordered_comm_ring
/-- An `ordered_comm_ring α` is a commutative ring `α` with a partial order such that
addition is monotone and multiplication by a positive number is strictly monotone. -/
@[protect_proj]
class ordered_comm_ring (α : Type u) extends ordered_ring α, ordered_comm_semiring α, comm_ring α
/-- Pullback an `ordered_comm_ring` under an injective map.
See note [reducible non-instances]. -/
@[reducible]
def function.injective.ordered_comm_ring [ordered_comm_ring α] {β : Type*}
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
(f : β → α) (hf : function.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) :
ordered_comm_ring β :=
{ ..hf.ordered_comm_semiring f zero one add mul,
..hf.ordered_ring f zero one add mul neg sub,
..hf.comm_ring f zero one add mul neg sub }
end ordered_comm_ring
/-- A `linear_ordered_ring α` is a ring `α` with a linear order such that
addition is monotone and multiplication by a positive number is strictly monotone. -/
@[protect_proj] class linear_ordered_ring (α : Type u)
extends ordered_ring α, linear_order α, nontrivial α
@[priority 100] -- see Note [lower instance priority]
instance linear_ordered_ring.to_linear_ordered_add_comm_group [s : linear_ordered_ring α] :
linear_ordered_add_comm_group α :=
{ .. s }
section linear_ordered_ring
variables [linear_ordered_ring α] {a b c : α}
@[priority 100] -- see Note [lower instance priority]
instance linear_ordered_ring.to_linear_ordered_semiring : linear_ordered_semiring α :=
{ mul_zero := mul_zero,
zero_mul := zero_mul,
add_left_cancel := @add_left_cancel α _,
le_of_add_le_add_left := @le_of_add_le_add_left α _ _ _,
mul_lt_mul_of_pos_left := @mul_lt_mul_of_pos_left α _,
mul_lt_mul_of_pos_right := @mul_lt_mul_of_pos_right α _,
le_total := linear_ordered_ring.le_total,
..‹linear_ordered_ring α› }
@[priority 100] -- see Note [lower instance priority]
instance linear_ordered_ring.to_domain : domain α :=
{ eq_zero_or_eq_zero_of_mul_eq_zero :=
begin
intros a b hab,
refine decidable.or_iff_not_and_not.2 (λ h, _), revert hab,
cases lt_or_gt_of_ne h.1 with ha ha; cases lt_or_gt_of_ne h.2 with hb hb,
exacts [(mul_pos_of_neg_of_neg ha hb).ne.symm, (mul_neg_of_neg_of_pos ha hb).ne,
(mul_neg_of_pos_of_neg ha hb).ne, (mul_pos ha hb).ne.symm]
end,
.. ‹linear_ordered_ring α› }
@[simp] lemma abs_one : abs (1 : α) = 1 := abs_of_pos zero_lt_one
@[simp] lemma abs_two : abs (2 : α) = 2 := abs_of_pos zero_lt_two
lemma abs_mul (a b : α) : abs (a * b) = abs a * abs b :=
begin
haveI := @linear_order.decidable_le α _,
rw [abs_eq (decidable.mul_nonneg (abs_nonneg a) (abs_nonneg b))],
cases le_total a 0 with ha ha; cases le_total b 0 with hb hb;
simp only [abs_of_nonpos, abs_of_nonneg, true_or, or_true, eq_self_iff_true,
neg_mul_eq_neg_mul_symm, mul_neg_eq_neg_mul_symm, neg_neg, *]
end
/-- `abs` as a `monoid_with_zero_hom`. -/
def abs_hom : monoid_with_zero_hom α α := ⟨abs, abs_zero, abs_one, abs_mul⟩
@[simp] lemma abs_mul_abs_self (a : α) : abs a * abs a = a * a :=
abs_by_cases (λ x, x * x = a * a) rfl (neg_mul_neg a a)
@[simp] lemma abs_mul_self (a : α) : abs (a * a) = a * a :=
by rw [abs_mul, abs_mul_abs_self]
lemma mul_pos_iff : 0 < a * b ↔ 0 < a ∧ 0 < b ∨ a < 0 ∧ b < 0 :=
⟨pos_and_pos_or_neg_and_neg_of_mul_pos,
λ h, h.elim (and_imp.2 mul_pos) (and_imp.2 mul_pos_of_neg_of_neg)⟩
lemma mul_neg_iff : a * b < 0 ↔ 0 < a ∧ b < 0 ∨ a < 0 ∧ 0 < b :=
by rw [← neg_pos, neg_mul_eq_mul_neg, mul_pos_iff, neg_pos, neg_lt_zero]
lemma mul_nonneg_iff : 0 ≤ a * b ↔ 0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0 :=
by haveI := @linear_order.decidable_le α _; exact
⟨nonneg_and_nonneg_or_nonpos_and_nonpos_of_mul_nnonneg,
λ h, h.elim (and_imp.2 decidable.mul_nonneg) (and_imp.2 decidable.mul_nonneg_of_nonpos_of_nonpos)⟩
/-- Out of three elements of a `linear_ordered_ring`, two must have the same sign. -/
lemma mul_nonneg_of_three (a b c : α) :
0 ≤ a * b ∨ 0 ≤ b * c ∨ 0 ≤ c * a :=
by iterate 3 { rw mul_nonneg_iff };
have := le_total 0 a; have := le_total 0 b; have := le_total 0 c; itauto
lemma mul_nonpos_iff : a * b ≤ 0 ↔ 0 ≤ a ∧ b ≤ 0 ∨ a ≤ 0 ∧ 0 ≤ b :=
by rw [← neg_nonneg, neg_mul_eq_mul_neg, mul_nonneg_iff, neg_nonneg, neg_nonpos]
lemma mul_self_nonneg (a : α) : 0 ≤ a * a :=
abs_mul_self a ▸ abs_nonneg _
@[simp] lemma neg_le_self_iff : -a ≤ a ↔ 0 ≤ a :=
by simp [neg_le_iff_add_nonneg, ← two_mul, mul_nonneg_iff, zero_le_one, (@zero_lt_two α _ _).not_le]
@[simp] lemma neg_lt_self_iff : -a < a ↔ 0 < a :=
by simp [neg_lt_iff_pos_add, ← two_mul, mul_pos_iff, zero_lt_one, (@zero_lt_two α _ _).not_lt]
@[simp] lemma le_neg_self_iff : a ≤ -a ↔ a ≤ 0 :=
calc a ≤ -a ↔ -(-a) ≤ -a : by rw neg_neg
... ↔ 0 ≤ -a : neg_le_self_iff
... ↔ a ≤ 0 : neg_nonneg
@[simp] lemma lt_neg_self_iff : a < -a ↔ a < 0 :=
calc a < -a ↔ -(-a) < -a : by rw neg_neg
... ↔ 0 < -a : neg_lt_self_iff
... ↔ a < 0 : neg_pos
@[simp] lemma abs_eq_self : abs a = a ↔ 0 ≤ a := by simp [abs_eq_max_neg]
@[simp] lemma abs_eq_neg_self : abs a = -a ↔ a ≤ 0 := by simp [abs_eq_max_neg]
/-- For an element `a` of a linear ordered ring, either `abs a = a` and `0 ≤ a`,
or `abs a = -a` and `a < 0`.
Use cases on this lemma to automate linarith in inequalities -/
lemma abs_cases (a : α) : (abs a = a ∧ 0 ≤ a) ∨ (abs a = -a ∧ a < 0) :=
begin
by_cases 0 ≤ a,
{ left,
exact ⟨abs_eq_self.mpr h, h⟩ },
{ right,
push_neg at h,
exact ⟨abs_eq_neg_self.mpr (le_of_lt h), h⟩ }
end
lemma gt_of_mul_lt_mul_neg_left (h : c * a < c * b) (hc : c ≤ 0) : b < a :=
have nhc : 0 ≤ -c, from neg_nonneg_of_nonpos hc,
have h2 : -(c * b) < -(c * a), from neg_lt_neg h,
have h3 : (-c) * b < (-c) * a, from calc
(-c) * b = - (c * b) : by rewrite neg_mul_eq_neg_mul
... < -(c * a) : h2
... = (-c) * a : by rewrite neg_mul_eq_neg_mul,
lt_of_mul_lt_mul_left h3 nhc
lemma neg_one_lt_zero : -1 < (0:α) := neg_lt_zero.2 zero_lt_one
lemma le_of_mul_le_of_one_le {a b c : α} (h : a * c ≤ b) (hb : 0 ≤ b) (hc : 1 ≤ c) : a ≤ b :=
by haveI := @linear_order.decidable_le α _; exact
have h' : a * c ≤ b * c, from calc
a * c ≤ b : h
... = b * 1 : by rewrite mul_one
... ≤ b * c : decidable.mul_le_mul_of_nonneg_left hc hb,
le_of_mul_le_mul_right h' (zero_lt_one.trans_le hc)
lemma nonneg_le_nonneg_of_sq_le_sq {a b : α} (hb : 0 ≤ b) (h : a * a ≤ b * b) : a ≤ b :=
by haveI := @linear_order.decidable_le α _; exact
le_of_not_gt (λhab, (decidable.mul_self_lt_mul_self hb hab).not_le h)
lemma mul_self_le_mul_self_iff {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a ≤ b ↔ a * a ≤ b * b :=
by haveI := @linear_order.decidable_le α _; exact
⟨decidable.mul_self_le_mul_self h1, nonneg_le_nonneg_of_sq_le_sq h2⟩
lemma mul_self_lt_mul_self_iff {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a < b ↔ a * a < b * b :=
by haveI := @linear_order.decidable_le α _; exact
((@decidable.strict_mono_incr_on_mul_self α _ _).lt_iff_lt h1 h2).symm
lemma mul_self_inj {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a * a = b * b ↔ a = b :=
by haveI := @linear_order.decidable_le α _; exact
(@decidable.strict_mono_incr_on_mul_self α _ _).inj_on.eq_iff h1 h2
@[simp] lemma mul_le_mul_left_of_neg {a b c : α} (h : c < 0) : c * a ≤ c * b ↔ b ≤ a :=
by haveI := @linear_order.decidable_le α _; exact
⟨le_imp_le_of_lt_imp_lt $ λ h', mul_lt_mul_of_neg_left h' h,
λ h', decidable.mul_le_mul_of_nonpos_left h' h.le⟩
@[simp] lemma mul_le_mul_right_of_neg {a b c : α} (h : c < 0) : a * c ≤ b * c ↔ b ≤ a :=
by haveI := @linear_order.decidable_le α _; exact
⟨le_imp_le_of_lt_imp_lt $ λ h', mul_lt_mul_of_neg_right h' h,
λ h', decidable.mul_le_mul_of_nonpos_right h' h.le⟩
@[simp] lemma mul_lt_mul_left_of_neg {a b c : α} (h : c < 0) : c * a < c * b ↔ b < a :=
lt_iff_lt_of_le_iff_le (mul_le_mul_left_of_neg h)
@[simp] lemma mul_lt_mul_right_of_neg {a b c : α} (h : c < 0) : a * c < b * c ↔ b < a :=
lt_iff_lt_of_le_iff_le (mul_le_mul_right_of_neg h)
lemma sub_one_lt (a : α) : a - 1 < a :=
sub_lt_iff_lt_add.2 (lt_add_one a)
lemma mul_self_pos {a : α} (ha : a ≠ 0) : 0 < a * a :=
by rcases lt_trichotomy a 0 with h|h|h;
[exact mul_pos_of_neg_of_neg h h, exact (ha h).elim, exact mul_pos h h]
lemma mul_self_le_mul_self_of_le_of_neg_le {x y : α} (h₁ : x ≤ y) (h₂ : -x ≤ y) : x * x ≤ y * y :=
begin
haveI := @linear_order.decidable_le α _,
rw [← abs_mul_abs_self x],
exact decidable.mul_self_le_mul_self (abs_nonneg x) (abs_le.2 ⟨neg_le.2 h₂, h₁⟩)
end
lemma nonneg_of_mul_nonpos_left {a b : α} (h : a * b ≤ 0) (hb : b < 0) : 0 ≤ a :=
le_of_not_gt (λ ha, absurd h (mul_pos_of_neg_of_neg ha hb).not_le)
lemma nonneg_of_mul_nonpos_right {a b : α} (h : a * b ≤ 0) (ha : a < 0) : 0 ≤ b :=
le_of_not_gt (λ hb, absurd h (mul_pos_of_neg_of_neg ha hb).not_le)
lemma pos_of_mul_neg_left {a b : α} (h : a * b < 0) (hb : b ≤ 0) : 0 < a :=
by haveI := @linear_order.decidable_le α _; exact
lt_of_not_ge (λ ha, absurd h (decidable.mul_nonneg_of_nonpos_of_nonpos ha hb).not_lt)
lemma pos_of_mul_neg_right {a b : α} (h : a * b < 0) (ha : a ≤ 0) : 0 < b :=
by haveI := @linear_order.decidable_le α _; exact
lt_of_not_ge (λ hb, absurd h (decidable.mul_nonneg_of_nonpos_of_nonpos ha hb).not_lt)
/-- The sum of two squares is zero iff both elements are zero. -/
lemma mul_self_add_mul_self_eq_zero {x y : α} : x * x + y * y = 0 ↔ x = 0 ∧ y = 0 :=
by rw [add_eq_zero_iff', mul_self_eq_zero, mul_self_eq_zero]; apply mul_self_nonneg
lemma eq_zero_of_mul_self_add_mul_self_eq_zero (h : a * a + b * b = 0) : a = 0 :=
(mul_self_add_mul_self_eq_zero.mp h).left
lemma abs_eq_iff_mul_self_eq : abs a = abs b ↔ a * a = b * b :=
begin
rw [← abs_mul_abs_self, ← abs_mul_abs_self b],
exact (mul_self_inj (abs_nonneg a) (abs_nonneg b)).symm,
end
lemma abs_lt_iff_mul_self_lt : abs a < abs b ↔ a * a < b * b :=
begin
rw [← abs_mul_abs_self, ← abs_mul_abs_self b],
exact mul_self_lt_mul_self_iff (abs_nonneg a) (abs_nonneg b)
end
lemma abs_le_iff_mul_self_le : abs a ≤ abs b ↔ a * a ≤ b * b :=
begin
rw [← abs_mul_abs_self, ← abs_mul_abs_self b],
exact mul_self_le_mul_self_iff (abs_nonneg a) (abs_nonneg b)
end
lemma abs_le_one_iff_mul_self_le_one : abs a ≤ 1 ↔ a * a ≤ 1 :=
by simpa only [abs_one, one_mul] using @abs_le_iff_mul_self_le α _ a 1
/-- Pullback a `linear_ordered_ring` under an injective map.
See note [reducible non-instances]. -/
@[reducible]
def function.injective.linear_ordered_ring {β : Type*}
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] [nontrivial β]
(f : β → α) (hf : function.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) :
linear_ordered_ring β :=
{ ..linear_order.lift f hf,
..‹nontrivial β›,
..hf.ordered_ring f zero one add mul neg sub }
end linear_ordered_ring
/-- A `linear_ordered_comm_ring α` is a commutative ring `α` with a linear order
such that addition is monotone and multiplication by a positive number is strictly monotone. -/
@[protect_proj]
class linear_ordered_comm_ring (α : Type u) extends linear_ordered_ring α, comm_monoid α
@[priority 100] -- see Note [lower instance priority]
instance linear_ordered_comm_ring.to_ordered_comm_ring [d : linear_ordered_comm_ring α] :
ordered_comm_ring α :=
-- One might hope that `{ ..linear_ordered_ring.to_linear_ordered_semiring, ..d }`
-- achieved the same result here.
-- Unfortunately with that definition we see mismatched instances in `algebra.star.chsh`.
let s : linear_ordered_semiring α := @linear_ordered_ring.to_linear_ordered_semiring α _ in
{ zero_mul := @linear_ordered_semiring.zero_mul α s,
mul_zero := @linear_ordered_semiring.mul_zero α s,
add_left_cancel := @linear_ordered_semiring.add_left_cancel α s,
le_of_add_le_add_left := @linear_ordered_semiring.le_of_add_le_add_left α s,
mul_lt_mul_of_pos_left := @linear_ordered_semiring.mul_lt_mul_of_pos_left α s,
mul_lt_mul_of_pos_right := @linear_ordered_semiring.mul_lt_mul_of_pos_right α s,
..d }
@[priority 100] -- see Note [lower instance priority]
instance linear_ordered_comm_ring.to_integral_domain [s : linear_ordered_comm_ring α] :
integral_domain α :=
{ ..linear_ordered_ring.to_domain, ..s }
@[priority 100] -- see Note [lower instance priority]
instance linear_ordered_comm_ring.to_linear_ordered_semiring [d : linear_ordered_comm_ring α] :
linear_ordered_semiring α :=
-- One might hope that `{ ..linear_ordered_ring.to_linear_ordered_semiring, ..d }`
-- achieved the same result here.
-- Unfortunately with that definition we see mismatched `preorder ℝ` instances in
-- `topology.metric_space.basic`.
let s : linear_ordered_semiring α := @linear_ordered_ring.to_linear_ordered_semiring α _ in
{ zero_mul := @linear_ordered_semiring.zero_mul α s,
mul_zero := @linear_ordered_semiring.mul_zero α s,
add_left_cancel := @linear_ordered_semiring.add_left_cancel α s,
le_of_add_le_add_left := @linear_ordered_semiring.le_of_add_le_add_left α s,
mul_lt_mul_of_pos_left := @linear_ordered_semiring.mul_lt_mul_of_pos_left α s,
mul_lt_mul_of_pos_right := @linear_ordered_semiring.mul_lt_mul_of_pos_right α s,
..d }
section linear_ordered_comm_ring
variables [linear_ordered_comm_ring α] {a b c d : α}
lemma max_mul_mul_le_max_mul_max (b c : α) (ha : 0 ≤ a) (hd: 0 ≤ d) :
max (a * b) (d * c) ≤ max a c * max d b :=
by haveI := @linear_order.decidable_le α _; exact
have ba : b * a ≤ max d b * max c a, from
decidable.mul_le_mul (le_max_right d b) (le_max_right c a) ha (le_trans hd (le_max_left d b)),
have cd : c * d ≤ max a c * max b d, from
decidable.mul_le_mul (le_max_right a c) (le_max_right b d) hd (le_trans ha (le_max_left a c)),
max_le
(by simpa [mul_comm, max_comm] using ba)
(by simpa [mul_comm, max_comm] using cd)
lemma abs_sub_sq (a b : α) : abs (a - b) * abs (a - b) = a * a + b * b - (1 + 1) * a * b :=
begin
rw abs_mul_abs_self,
simp only [mul_add, add_comm, add_left_comm, mul_comm, sub_eq_add_neg,
mul_one, mul_neg_eq_neg_mul_symm, neg_add_rev, neg_neg],
end
@[simp] lemma abs_dvd (a b : α) : abs a ∣ b ↔ a ∣ b :=
by { cases abs_choice a with h h; simp only [h, neg_dvd] }
lemma abs_dvd_self (a : α) : abs a ∣ a :=
(abs_dvd a a).mpr (dvd_refl a)
@[simp] lemma dvd_abs (a b : α) : a ∣ abs b ↔ a ∣ b :=
by { cases abs_choice b with h h; simp only [h, dvd_neg] }
lemma self_dvd_abs (a : α) : a ∣ abs a :=
(dvd_abs a a).mpr (dvd_refl a)
lemma abs_dvd_abs (a b : α) : abs a ∣ abs b ↔ a ∣ b :=
(abs_dvd _ _).trans (dvd_abs _ _)
lemma even_abs {a : α} : even (abs a) ↔ even a :=
dvd_abs _ _
/-- Pullback a `linear_ordered_comm_ring` under an injective map.
See note [reducible non-instances]. -/
@[reducible]
def function.injective.linear_ordered_comm_ring {β : Type*}
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] [nontrivial β]
(f : β → α) (hf : function.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) :
linear_ordered_comm_ring β :=
{ ..linear_order.lift f hf,
..‹nontrivial β›,
..hf.ordered_comm_ring f zero one add mul neg sub }
end linear_ordered_comm_ring
/-- Extend `nonneg_add_comm_group` to support ordered rings
specified by their nonnegative elements -/
class nonneg_ring (α : Type*) extends ring α, nonneg_add_comm_group α :=
(one_nonneg : nonneg 1)
(mul_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a * b))
(mul_pos : ∀ {a b}, pos a → pos b → pos (a * b))
/-- Extend `nonneg_add_comm_group` to support linearly ordered rings
specified by their nonnegative elements -/
class linear_nonneg_ring (α : Type*) extends domain α, nonneg_add_comm_group α :=
(one_pos : pos 1)
(mul_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a * b))
(nonneg_total : ∀ a, nonneg a ∨ nonneg (-a))
[dec_nonneg : decidable_pred nonneg]
namespace nonneg_ring
open nonneg_add_comm_group
variable [nonneg_ring α]
/-- `to_linear_nonneg_ring` shows that a `nonneg_ring` with a linear order is a `domain`,
hence a `linear_nonneg_ring`. -/
def to_linear_nonneg_ring [nontrivial α] [decidable_pred (@nonneg α _)]
(nonneg_total : ∀ a : α, nonneg a ∨ nonneg (-a))
: linear_nonneg_ring α :=
{ one_pos := (pos_iff 1).mpr ⟨one_nonneg, λ h, zero_ne_one (nonneg_antisymm one_nonneg h).symm⟩,
nonneg_total := nonneg_total,
eq_zero_or_eq_zero_of_mul_eq_zero :=
suffices ∀ {a} b : α, nonneg a → a * b = 0 → a = 0 ∨ b = 0,
from λ a b, (nonneg_total a).elim (this b)
(λ na, by simpa using this b na),
suffices ∀ {a b : α}, nonneg a → nonneg b → a * b = 0 → a = 0 ∨ b = 0,
from λ a b na, (nonneg_total b).elim (this na)
(λ nb, by simpa using this na nb),
λ a b na nb z, decidable.by_cases
(λ nna : nonneg (-a), or.inl (nonneg_antisymm na nna))
(λ pa, decidable.by_cases
(λ nnb : nonneg (-b), or.inr (nonneg_antisymm nb nnb))
(λ pb, absurd z $ ne_of_gt $ pos_def.1 $ mul_pos
((pos_iff _).2 ⟨na, pa⟩)
((pos_iff _).2 ⟨nb, pb⟩))),
..‹nontrivial α›,
..‹nonneg_ring α› }
end nonneg_ring
namespace linear_nonneg_ring
open nonneg_add_comm_group
variable [linear_nonneg_ring α]
@[priority 100] -- see Note [lower instance priority]
instance to_nonneg_ring : nonneg_ring α :=
{ one_nonneg := ((pos_iff _).mp one_pos).1,
mul_pos := λ a b pa pb,
let ⟨a1, a2⟩ := (pos_iff a).1 pa,
⟨b1, b2⟩ := (pos_iff b).1 pb in
have ab : nonneg (a * b), from mul_nonneg a1 b1,
(pos_iff _).2 ⟨ab, λ hn,
have a * b = 0, from nonneg_antisymm ab hn,
(eq_zero_or_eq_zero_of_mul_eq_zero _ _ this).elim
(ne_of_gt (pos_def.1 pa))
(ne_of_gt (pos_def.1 pb))⟩,
..‹linear_nonneg_ring α› }
/-- Construct `linear_order` from `linear_nonneg_ring`. This is not an instance
because we don't use it in `mathlib`. -/
local attribute [instance]
def to_linear_order [decidable_pred (nonneg : α → Prop)] : linear_order α :=
{ le_total := nonneg_total_iff.1 nonneg_total,
decidable_le := by apply_instance,
decidable_lt := by apply_instance,
..‹linear_nonneg_ring α›, ..(infer_instance : ordered_add_comm_group α) }
/-- Construct `linear_ordered_ring` from `linear_nonneg_ring`.
This is not an instance because we don't use it in `mathlib`. -/
local attribute [instance]
def to_linear_ordered_ring [decidable_pred (nonneg : α → Prop)] : linear_ordered_ring α :=
{ mul_pos := by simp [pos_def.symm]; exact @nonneg_ring.mul_pos _ _,
zero_le_one := le_of_lt $ lt_of_not_ge $ λ (h : nonneg (0 - 1)), begin
rw [zero_sub] at h,
have := mul_nonneg h h, simp at this,
exact zero_ne_one (nonneg_antisymm this h).symm
end,
..‹linear_nonneg_ring α›, ..(infer_instance : ordered_add_comm_group α),
..(infer_instance : linear_order α) }
/-- Convert a `linear_nonneg_ring` with a commutative multiplication and
decidable non-negativity into a `linear_ordered_comm_ring` -/
def to_linear_ordered_comm_ring
[decidable_pred (@nonneg α _)]
[comm : @is_commutative α (*)]
: linear_ordered_comm_ring α :=
{ mul_comm := is_commutative.comm,
..@linear_nonneg_ring.to_linear_ordered_ring _ _ _ }
end linear_nonneg_ring
/-- A canonically ordered commutative semiring is an ordered, commutative semiring
in which `a ≤ b` iff there exists `c` with `b = a + c`. This is satisfied by the
natural numbers, for example, but not the integers or other ordered groups. -/
@[protect_proj]
class canonically_ordered_comm_semiring (α : Type*) extends
canonically_ordered_add_monoid α, comm_semiring α :=
(eq_zero_or_eq_zero_of_mul_eq_zero : ∀ a b : α, a * b = 0 → a = 0 ∨ b = 0)
namespace canonically_ordered_comm_semiring
variables [canonically_ordered_comm_semiring α] {a b : α}
@[priority 100] -- see Note [lower instance priority]
instance to_no_zero_divisors : no_zero_divisors α :=
⟨canonically_ordered_comm_semiring.eq_zero_or_eq_zero_of_mul_eq_zero⟩
@[priority 100] -- see Note [lower instance priority]
instance to_covariant_mul_le : covariant_class α α (*) (≤) :=
begin
refine ⟨λ a b c h, _⟩,
rcases le_iff_exists_add.1 h with ⟨c, rfl⟩,
rw mul_add,
apply self_le_add_right
end
/-- A version of `zero_lt_one : 0 < 1` for a `canonically_ordered_comm_semiring`. -/
lemma zero_lt_one [nontrivial α] : (0:α) < 1 := (zero_le 1).lt_of_ne zero_ne_one
lemma mul_pos : 0 < a * b ↔ (0 < a) ∧ (0 < b) :=
by simp only [pos_iff_ne_zero, ne.def, mul_eq_zero, not_or_distrib]
end canonically_ordered_comm_semiring
/-! ### Structures involving `*` and `0` on `with_top` and `with_bot`
The main results of this section are `with_top.canonically_ordered_comm_semiring` and
`with_bot.comm_monoid_with_zero`.
-/
namespace with_top
instance [nonempty α] : nontrivial (with_top α) :=
option.nontrivial
variable [decidable_eq α]
section has_mul
variables [has_zero α] [has_mul α]
instance : mul_zero_class (with_top α) :=
{ zero := 0,
mul := λm n, if m = 0 ∨ n = 0 then 0 else m.bind (λa, n.bind $ λb, ↑(a * b)),
zero_mul := assume a, if_pos $ or.inl rfl,
mul_zero := assume a, if_pos $ or.inr rfl }
lemma mul_def {a b : with_top α} :
a * b = if a = 0 ∨ b = 0 then 0 else a.bind (λa, b.bind $ λb, ↑(a * b)) := rfl
@[simp] lemma mul_top {a : with_top α} (h : a ≠ 0) : a * ⊤ = ⊤ :=
by cases a; simp [mul_def, h]; refl
@[simp] lemma top_mul {a : with_top α} (h : a ≠ 0) : ⊤ * a = ⊤ :=
by cases a; simp [mul_def, h]; refl
@[simp] lemma top_mul_top : (⊤ * ⊤ : with_top α) = ⊤ :=
top_mul top_ne_zero
end has_mul
section mul_zero_class
variables [mul_zero_class α]
@[norm_cast] lemma coe_mul {a b : α} : (↑(a * b) : with_top α) = a * b :=
decidable.by_cases (assume : a = 0, by simp [this]) $ assume ha,
decidable.by_cases (assume : b = 0, by simp [this]) $ assume hb,
by { simp [*, mul_def], refl }
lemma mul_coe {b : α} (hb : b ≠ 0) : ∀{a : with_top α}, a * b = a.bind (λa:α, ↑(a * b))
| none := show (if (⊤:with_top α) = 0 ∨ (b:with_top α) = 0 then 0 else ⊤ : with_top α) = ⊤,
by simp [hb]
| (some a) := show ↑a * ↑b = ↑(a * b), from coe_mul.symm
@[simp] lemma mul_eq_top_iff {a b : with_top α} : a * b = ⊤ ↔ (a ≠ 0 ∧ b = ⊤) ∨ (a = ⊤ ∧ b ≠ 0) :=
begin
cases a; cases b; simp only [none_eq_top, some_eq_coe],
{ simp [← coe_mul] },
{ suffices : ⊤ * (b : with_top α) = ⊤ ↔ b ≠ 0, by simpa,
by_cases hb : b = 0; simp [hb] },
{ suffices : (a : with_top α) * ⊤ = ⊤ ↔ a ≠ 0, by simpa,
by_cases ha : a = 0; simp [ha] },
{ simp [← coe_mul] }
end
lemma mul_lt_top [partial_order α] {a b : with_top α} (ha : a < ⊤) (hb : b < ⊤) : a * b < ⊤ :=
begin
lift a to α using ne_top_of_lt ha,
lift b to α using ne_top_of_lt hb,
simp only [← coe_mul, coe_lt_top]
end
end mul_zero_class
/-- `nontrivial α` is needed here as otherwise we have `1 * ⊤ = ⊤` but also `= 0 * ⊤ = 0`. -/
instance [mul_zero_one_class α] [nontrivial α] : mul_zero_one_class (with_top α) :=
{ mul := (*),
one := 1,
zero := 0,
one_mul := λ a, match a with
| none := show ((1:α) : with_top α) * ⊤ = ⊤, by simp [-with_top.coe_one]
| (some a) := show ((1:α) : with_top α) * a = a, by simp [coe_mul.symm, -with_top.coe_one]
end,
mul_one := λ a, match a with
| none := show ⊤ * ((1:α) : with_top α) = ⊤, by simp [-with_top.coe_one]
| (some a) := show ↑a * ((1:α) : with_top α) = a, by simp [coe_mul.symm, -with_top.coe_one]
end,
.. with_top.mul_zero_class }
instance [mul_zero_class α] [no_zero_divisors α] : no_zero_divisors (with_top α) :=
⟨λ a b, by cases a; cases b; dsimp [mul_def]; split_ifs;
simp [*, none_eq_top, some_eq_coe, mul_eq_zero] at *⟩
instance [semigroup_with_zero α] [no_zero_divisors α] : semigroup_with_zero (with_top α) :=
{ mul := (*),
zero := 0,
mul_assoc := λ a b c, begin
cases a,
{ by_cases hb : b = 0; by_cases hc : c = 0;
simp [*, none_eq_top] },
cases b,
{ by_cases ha : a = 0; by_cases hc : c = 0;
simp [*, none_eq_top, some_eq_coe] },
cases c,
{ by_cases ha : a = 0; by_cases hb : b = 0;
simp [*, none_eq_top, some_eq_coe] },
simp [some_eq_coe, coe_mul.symm, mul_assoc]
end,
.. with_top.mul_zero_class }
instance [monoid_with_zero α] [no_zero_divisors α] [nontrivial α] : monoid_with_zero (with_top α) :=
{ .. with_top.mul_zero_one_class, .. with_top.semigroup_with_zero }
instance [comm_monoid_with_zero α] [no_zero_divisors α] [nontrivial α] :
comm_monoid_with_zero (with_top α) :=
{ mul := (*),
zero := 0,
mul_comm := λ a b, begin
by_cases ha : a = 0, { simp [ha] },
by_cases hb : b = 0, { simp [hb] },
simp [ha, hb, mul_def, option.bind_comm a b, mul_comm]
end,
.. with_top.monoid_with_zero }
variables [canonically_ordered_comm_semiring α]
private lemma distrib' (a b c : with_top α) : (a + b) * c = a * c + b * c :=
begin
cases c,
{ show (a + b) * ⊤ = a * ⊤ + b * ⊤,
by_cases ha : a = 0; simp [ha] },
{ show (a + b) * c = a * c + b * c,
by_cases hc : c = 0, { simp [hc] },
simp [mul_coe hc], cases a; cases b,
repeat { refl <|> exact congr_arg some (add_mul _ _ _) } }
end
/-- This instance requires `canonically_ordered_comm_semiring` as it is the smallest class
that derives from both `non_assoc_non_unital_semiring` and `canonically_ordered_add_monoid`, both
of which are required for distributivity. -/
instance [nontrivial α] : comm_semiring (with_top α) :=
{ right_distrib := distrib',
left_distrib := assume a b c, by rw [mul_comm, distrib', mul_comm b, mul_comm c]; refl,
.. with_top.add_comm_monoid, .. with_top.comm_monoid_with_zero,}
instance [nontrivial α] : canonically_ordered_comm_semiring (with_top α) :=
{ .. with_top.comm_semiring,
.. with_top.canonically_ordered_add_monoid,
.. with_top.no_zero_divisors, }
end with_top
namespace with_bot
instance [nonempty α] : nontrivial (with_bot α) :=
option.nontrivial
variable [decidable_eq α]
section has_mul
variables [has_zero α] [has_mul α]
instance : mul_zero_class (with_bot α) :=
with_top.mul_zero_class
lemma mul_def {a b : with_bot α} :
a * b = if a = 0 ∨ b = 0 then 0 else a.bind (λa, b.bind $ λb, ↑(a * b)) := rfl
@[simp] lemma mul_bot {a : with_bot α} (h : a ≠ 0) : a * ⊥ = ⊥ :=
with_top.mul_top h
@[simp] lemma bot_mul {a : with_bot α} (h : a ≠ 0) : ⊥ * a = ⊥ :=
with_top.top_mul h
@[simp] lemma bot_mul_bot : (⊥ * ⊥ : with_bot α) = ⊥ :=
with_top.top_mul_top
end has_mul
section mul_zero_class
variables [mul_zero_class α]
@[norm_cast] lemma coe_mul {a b : α} : (↑(a * b) : with_bot α) = a * b :=
decidable.by_cases (assume : a = 0, by simp [this]) $ assume ha,
decidable.by_cases (assume : b = 0, by simp [this]) $ assume hb,
by { simp [*, mul_def], refl }
lemma mul_coe {b : α} (hb : b ≠ 0) {a : with_bot α} : a * b = a.bind (λa:α, ↑(a * b)) :=
with_top.mul_coe hb
@[simp] lemma mul_eq_bot_iff {a b : with_bot α} : a * b = ⊥ ↔ (a ≠ 0 ∧ b = ⊥) ∨ (a = ⊥ ∧ b ≠ 0) :=
with_top.mul_eq_top_iff
lemma bot_lt_mul [partial_order α] {a b : with_bot α} (ha : ⊥ < a) (hb : ⊥ < b) : ⊥ < a * b :=
begin
lift a to α using ne_bot_of_gt ha,
lift b to α using ne_bot_of_gt hb,
simp only [← coe_mul, bot_lt_coe],
end
end mul_zero_class
/-- `nontrivial α` is needed here as otherwise we have `1 * ⊥ = ⊥` but also `= 0 * ⊥ = 0`. -/
instance [mul_zero_one_class α] [nontrivial α] : mul_zero_one_class (with_bot α) :=
with_top.mul_zero_one_class
instance [mul_zero_class α] [no_zero_divisors α] : no_zero_divisors (with_bot α) :=
with_top.no_zero_divisors
instance [semigroup_with_zero α] [no_zero_divisors α] : semigroup_with_zero (with_bot α) :=
with_top.semigroup_with_zero
instance [monoid_with_zero α] [no_zero_divisors α] [nontrivial α] : monoid_with_zero (with_bot α) :=
with_top.monoid_with_zero
instance [comm_monoid_with_zero α] [no_zero_divisors α] [nontrivial α] :
comm_monoid_with_zero (with_bot α) :=
with_top.comm_monoid_with_zero
instance [canonically_ordered_comm_semiring α] [nontrivial α] : comm_semiring (with_bot α) :=
with_top.comm_semiring
end with_bot
|
3b2f97c4e34125ae2ac412d7aa2d60919c86ca1c | ce6917c5bacabee346655160b74a307b4a5ab620 | /src/ch5/ex0702.lean | 74800fb21cc525b5677ad0cd6fa54c856d0b6eb3 | [] | 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 | 390 | lean | import data.list.basic
universe u
variable {α : Type}
open list
example (xs : list ℕ) :
reverse (xs ++ [1, 2, 3]) = [3, 2, 1] ++ reverse xs :=
by simp
example (xs ys : list α) :
length (reverse (xs ++ ys)) = length xs + length ys :=
by simp [add_comm]
-- lean 3.6 removes simp attributes from add_comm
-- i.e., now it is required to provide that lemma to solve this example.
|
fce0edfb467b8a38d69e6e6f29280d17cfd5932c | 12dabd587ce2621d9a4eff9f16e354d02e206c8e | /world07/level02.lean | f6e7bcdad4fab30aafaba07c67fffff6f9d07c60 | [] | no_license | abdelq/natural-number-game | a1b5b8f1d52625a7addcefc97c966d3f06a48263 | bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2 | refs/heads/master | 1,668,606,478,691 | 1,594,175,058,000 | 1,594,175,058,000 | 278,673,209 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 115 | lean | lemma and_symm (P Q : Prop) : P ∧ Q → Q ∧ P :=
begin
intro h,
cases h with p q,
split,
exact q,
exact p,
end
|
cb6aebffae4ad71a09857a66662ce3afd0a74338 | 57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d | /tests/lean/run/allGoals.lean | 886fadf7f9b883e16bea1ce56af5489c7cea0e40 | [
"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 | 1,675 | lean | inductive Weekday where
| sunday : Weekday
| monday : Weekday
| tuesday : Weekday
| wednesday : Weekday
| thursday : Weekday
| friday : Weekday
| saturday : Weekday
def Weekday.next : Weekday -> Weekday :=
fun d => match d with
| sunday => monday
| monday => tuesday
| tuesday => wednesday
| wednesday => thursday
| thursday => friday
| friday => saturday
| saturday => sunday
def Weekday.previous : Weekday -> Weekday
| sunday => saturday
| monday => sunday
| tuesday => monday
| wednesday => tuesday
| thursday => wednesday
| friday => thursday
| saturday => friday
theorem Weekday.nextOfPrevious (d : Weekday) : next (previous d) = d := by
cases d
allGoals rfl
theorem Weekday.nextOfPrevious' (d : Weekday) : previous (next d) = d ∧ next (previous d) = d := by
apply And.intro
cases d <;> rfl
cases d <;> rfl
theorem Weekday.nextOfPrevious'' (d : Weekday) : previous (next d) = d ∧ next (previous d) = d := by
apply And.intro <;> cases d <;> rfl
open Lean.Parser.Tactic in
macro "rwd " x:term : tactic => `(rw $x:term; done)
theorem ex (a b c : α) (h₁ : a = b) (h₂ : a = c) : b = a ∧ c = a := by
apply And.intro <;> first rwd h₁ | rwd h₂
theorem idEq (a : α) : id a = a :=
rfl
theorem Weekday.test (d : Weekday) : next (previous d) = id d := by
cases d
traceState
allGoals rw idEq
traceState
allGoals rfl
theorem Weekday.test2 (d : Weekday) : next (previous d) = id d := by
cases d <;> rw idEq
traceState
allGoals rfl
def bug {a b c : Nat} (h₁ : a = b) (h₂ : b = c) : a = c := by
apply Eq.trans <;> assumption
|
bdd81f4a18bbf24d682b7695eae842090096ac1e | c3f2fcd060adfa2ca29f924839d2d925e8f2c685 | /library/algebra/function.lean | f76509f201f2c68626268a5c910f0d3a7752c671 | [
"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 | 1,438 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Module: algebra.function
Author: Leonardo de Moura
General operations on functions.
-/
namespace function
variables {A : Type} {B : Type} {C : Type} {D : Type} {E : Type}
definition compose [reducible] (f : B → C) (g : A → B) : A → C :=
λx, f (g x)
definition id [reducible] (a : A) : A :=
a
definition on_fun [reducible] (f : B → B → C) (g : A → B) : A → A → C :=
λx y, f (g x) (g y)
definition combine [reducible] (f : A → B → C) (op : C → D → E) (g : A → B → D) : A → B → E :=
λx y, op (f x y) (g x y)
definition const [reducible] (B : Type) (a : A) : B → A :=
λx, a
definition dcompose [reducible] {B : A → Type} {C : Π {x : A}, B x → Type}
(f : Π {x : A} (y : B x), C y) (g : Πx, B x) : Πx, C (g x) :=
λx, f (g x)
definition flip [reducible] {C : A → B → Type} (f : Πx y, C x y) : Πy x, C x y :=
λy x, f x y
definition app [reducible] {B : A → Type} (f : Πx, B x) (x : A) : B x :=
f x
precedence `∘'`:60
precedence `on`:1
precedence `$`:1
infixr ∘ := compose
infixr ∘' := dcompose
infixl on := on_fun
infixr $ := app
notation f `-[` op `]-` g := combine f op g
end function
-- copy reducible annotations to top-level
export [reduce-hints] function
|
dcc3b92c76f0190c50350b10bd22a542a96d148b | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/beginend.lean | 01f1d15d50533454c4a69e9c0ddb6ac2ea033e22 | [
"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 | 157 | lean | import logic.eq
open tactic
theorem foo (A : Type) (a b c : A) (Hab : a = b) (Hbc : b = c) : a = c :=
begin
apply eq.trans,
apply Hab,
apply Hbc,
end
|
c14bb2d8316f25c94a945f4ff7017a0757196647 | 3618c6e11aa822fd542440674dfb9a7b9921dba0 | /src/P.lean | 13e4e77f001fb86711d7659d94f97a4ab13e3adb | [] | no_license | ChrisHughes24/single_relation | 99ceedcc02d236ce46d6c65d72caa669857533c5 | 057e157a59de6d0e43b50fcb537d66792ec20450 | refs/heads/master | 1,683,652,062,698 | 1,683,360,089,000 | 1,683,360,089,000 | 279,346,432 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,408 | lean | import group_theory.semidirect_product
import coprod.free_group
/-!
# The P functor
Given a group `G` an element `p` of the group `P G` can be used as a certificate
of congruences modulo `r : G` in `G`. There are two surjective maps `P G →* G`,
`lhs r` and `right`. `lhs r p` and `right p` will always be congruent modulo
`r` in `G`, and so `p` is a certificate of this congruence. This is used is the
`group_thingy` tactic.
-/
variables {G : Type} [group G] [decidable_eq G]
variables {H : Type} [group H] [decidable_eq H]
open free_group semidirect_product function
/-- `mul_free` is the extension of the left regular action of `G` to an
action of `G` on `free_group G` -/
def mul_free : G →* mul_aut (free_group G) :=
{ to_fun := λ g, free_group.equiv (equiv.mul_left g),
map_mul' := λ _ _, mul_equiv.to_monoid_hom_injective $ free_group.hom_ext $
by simp [mul_aut.mul_def, equiv.perm.mul_def, mul_assoc],
map_one' := mul_equiv.to_monoid_hom_injective $ free_group.hom_ext $
by simp [mul_aut.mul_def, equiv.perm.mul_def, mul_assoc] }
@[simp] lemma mul_free_of' (g : G) (h : G) (n : C∞) :
mul_free g (of' h n) = of' (g * h) n :=
by simp [mul_free]
@[simp] lemma mul_free_of (g : G) (h : G) : mul_free g (of h) = of (g * h) :=
by simp [mul_free, of_eq_of']
@[simp] lemma mul_free_inv_of (g : G) (h : G) : (mul_free g)⁻¹ (of h) = of (g⁻¹ * h) :=
by rw [← monoid_hom.map_inv, mul_free_of]
variable (G)
/-- The group `P G` is used as a certificate of congruences of elements of `G`
modulo a relation `r : G`. -/
@[reducible] def P : Type* := free_group G ⋊[mul_free] G
instance [decidable_eq G] : decidable_eq (P G) :=
semidirect_product.decidable_eq _ _ _
variable {G}
namespace P
variables {r : G}
instance : group (P G) := semidirect_product.group
/-- `p` is a certificate of the congruence between `lhs r p` and `right p` modulo `r` -/
def lhs (r : G) : P G →* G :=
semidirect_product.lift (free_group.lift (λ g, mul_aut.conj g r))
(monoid_hom.id _)
(λ g, free_group.hom_ext
(by simp [of'_eq_of_pow, gpowers_hom, mul_assoc]))
@[simp] lemma lhs_comp_inr : (lhs r).comp inr = monoid_hom.id _ :=
by simp [lhs]
@[simp] lemma lhs_inr (g : G) : lhs r (inr g) = g := by simp [lhs]
@[simp] lemma lhs_comp_inl : (lhs r).comp inl = free_group.lift (λ g, mul_aut.conj g r) :=
by simp [lhs]
lemma lhs_inl (w : free_group G) :
lhs r (inl w) = free_group.lift (λ g, mul_aut.conj g r) w :=
by simp [lhs]
/-- -/
def trans (p q : P G) : P G :=
⟨p.1 * q.1, q.2⟩
lemma trans_eq (p q : P G) : trans p q = p * inr p.right⁻¹ * q :=
semidirect_product.ext _ _ (by simp [trans, right_hom]) (by simp [trans, right_hom])
@[simp] lemma trans_right (p q : P G) : (p.trans q).right = q.right :=
by simp [right_hom, trans]
@[simp] lemma lhs_trans (p q : P G) : lhs r (p.trans q) = lhs r p * p.right⁻¹ * lhs r q :=
by simp [lhs, right_hom, trans_eq]
section map
/-- This is mainly used with `f` being `remove_subscript` which is not injective,
nor is it injective on the words on which it is used. -/
def map (f : G →* H) (hf : injective f) : P G →* P H :=
semidirect_product.map (free_group.embedding ⟨f, hf⟩) f
(λ g, free_group.hom_ext (by simp))
@[simp] lemma map_id : map (monoid_hom.id G) injective_id = monoid_hom.id (P G) :=
semidirect_product.hom_ext (free_group.hom_ext (by simp [map]))
(monoid_hom.ext $ by simp [map])
/-- `change_r w` take a certificate of a congruence modulo `w * r * w⁻¹`
and returns a certificate of a congruence modulo `r` -/
def change_r (w : G) : P G →* P G :=
semidirect_product.map (free_group.equiv (equiv.mul_right w)).to_monoid_hom
(monoid_hom.id _)
(λ g, free_group.hom_ext $ by simp [mul_assoc])
@[simp] lemma lhs_comp_change_r (r w : G) :
(lhs r).comp (change_r w) = lhs (w * r * w⁻¹) :=
semidirect_product.hom_ext (free_group.hom_ext
(by simp [change_r, mul_assoc, lhs_inl]))
(by simp [monoid_hom.comp_assoc, change_r])
@[simp] lemma right_hom_comp_change_r (w : G) (x : P G) :
right_hom.comp (change_r w) = right_hom := rfl
end map
end P
/-- `solver r T` is defined to be the type `free_group ι → option (P (free_group ι))`.
By convention, a `solver r T` will solve the word problem with respect to `r` and `T` -/
@[reducible] def solver {ι : Type} [decidable_eq ι] (r : free_group ι) (T : set ι) : Type :=
free_group ι → option (P (free_group ι))
|
8bff6902091f8398713edc341235bdc60135ce91 | ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5 | /stage0/src/Init/Notation.lean | 504464cf4f70135f87d125c4bf3ba58e099b1c0a | [
"Apache-2.0"
] | permissive | dupuisf/lean4 | d082d13b01243e1de29ae680eefb476961221eef | 6a39c65bd28eb0e28c3870188f348c8914502718 | refs/heads/master | 1,676,948,755,391 | 1,610,665,114,000 | 1,610,665,114,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,420 | 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
Notation for operators defined at Prelude.lean
-/
prelude
import Init.Prelude
-- DSL for specifying parser precedences and priorities
namespace Lean.Parser.Syntax
syntax:65 (name := addPrec) prec " + " prec:66 : prec
syntax:65 (name := subPrec) prec " - " prec:66 : prec
syntax:65 (name := addPrio) prio " + " prio:66 : prio
syntax:65 (name := subPrio) prio " - " prio:66 : prio
end Lean.Parser.Syntax
macro "max" : prec => `(1024) -- maximum precedence used in term parsers
macro "lead" : prec => `(1023)
macro "(" p:prec ")" : prec => p
macro "min" : prec => `(10) -- minimum precedence used in term parsers
macro "min1" : prec => `(11) -- `(min+1) we can only `min+1` after `Meta.lean`
/-
`max:prec` as a term. It is equivalent to `evalPrec! max` for `evalPrec!` defined at `Meta.lean`.
We use `maxPrec!` to workaround bootstrapping issues. -/
macro "maxPrec!" : term => `(1024)
macro "default" : prio => `(1000)
macro "low" : prio => `(100)
macro "high" : prio => `(10000)
macro "(" p:prio ")" : prio => p
-- Basic notation for defining parsers
syntax stx "+" : stx
syntax stx "*" : stx
syntax stx "?" : stx
syntax:2 stx " <|> " stx:1 : stx
macro_rules
| `(stx| $p +) => `(stx| many1($p))
| `(stx| $p *) => `(stx| many($p))
| `(stx| $p ?) => `(stx| optional($p))
| `(stx| $p₁ <|> $p₂) => `(stx| orelse($p₁, $p₂))
/- Comma-separated sequence. -/
macro:max x:stx ",*" : stx => `(stx| sepBy($x, ",", ", "))
macro:max x:stx ",+" : stx => `(stx| sepBy1($x, ",", ", "))
/- Comma-separated sequence with optional trailing comma. -/
macro:max x:stx ",*,?" : stx => `(stx| sepBy($x, ",", ", ", allowTrailingSep))
macro:max x:stx ",+,?" : stx => `(stx| sepBy1($x, ",", ", ", allowTrailingSep))
macro "!" x:stx : stx => `(stx| notFollowedBy($x))
syntax (name := rawNatLit) "natLit! " num : term
infixr:90 " ∘ " => Function.comp
infixr:35 " × " => Prod
infixl:65 " + " => HAdd.hAdd
infixl:65 " - " => HSub.hSub
infixl:70 " * " => HMul.hMul
infixl:70 " / " => HDiv.hDiv
infixl:70 " % " => HMod.hMod
infixr:80 " ^ " => HPow.hPow
prefix:100 "-" => Neg.neg
-- declare ASCII alternatives first so that the latter Unicode unexpander wins
infix:50 " <= " => HasLessEq.LessEq
infix:50 " ≤ " => HasLessEq.LessEq
infix:50 " < " => HasLess.Less
infix:50 " >= " => GreaterEq
infix:50 " ≥ " => GreaterEq
infix:50 " > " => Greater
infix:50 " = " => Eq
infix:50 " == " => BEq.beq
infix:50 " ~= " => HEq
infix:50 " ≅ " => HEq
/-
Remark: the infix commands above ensure a delaborator is generated for each relations.
We redefine the macros below to be able to use the auxiliary `binrel!` elaboration helper for binary relations.
It has better support for applying coercions. For example, suppose we have `binrel! Eq n i` where `n : Nat` and
`i : Int`. The default elaborator fails because we don't have a coercion from `Int` to `Nat`, but
`binrel!` succeeds because it also tries a coercion from `Nat` to `Int` even when the nat occurs before the int. -/
macro_rules | `($x <= $y) => `(binrel! HasLessEq.LessEq $x $y)
macro_rules | `($x ≤ $y) => `(binrel! HasLessEq.LessEq $x $y)
macro_rules | `($x < $y) => `(binrel! HasLess.Less $x $y)
macro_rules | `($x > $y) => `(binrel! Greater $x $y)
macro_rules | `($x >= $y) => `(binrel! GreaterEq $x $y)
macro_rules | `($x ≥ $y) => `(binrel! GreaterEq $x $y)
macro_rules | `($x = $y) => `(binrel! Eq $x $y)
macro_rules | `($x == $y) => `(binrel! BEq.beq $x $y)
infixr:35 " /\\ " => And
infixr:35 " ∧ " => And
infixr:30 " \\/ " => Or
infixr:30 " ∨ " => Or
notation:max "¬" p:40 => Not p
infixl:35 " && " => and
infixl:30 " || " => or
notation:max "!" b:40 => not b
infixl:65 " ++ " => HAppend.hAppend
infixr:67 " :: " => List.cons
infixr:20 " <|> " => HOrElse.hOrElse
infixr:60 " >> " => HAndThen.hAndThen
infixl:55 " >>= " => Bind.bind
infixl:60 " <*> " => Seq.seq
infixl:60 " <* " => SeqLeft.seqLeft
infixr:60 " *> " => SeqRight.seqRight
infixr:100 " <$> " => Functor.map
syntax (name := termDepIfThenElse) ppGroup(ppDedent("if " ident " : " term " then" ppSpace term ppDedent(ppSpace "else") ppSpace term)) : term
macro_rules
| `(if $h:ident : $c then $t:term else $e:term) => `(dite $c (fun $h:ident => $t) (fun $h:ident => $e))
syntax (name := termIfThenElse) ppGroup(ppDedent("if " term " then" ppSpace term ppDedent(ppSpace "else") ppSpace term)) : term
macro_rules
| `(if $c then $t:term else $e:term) => `(ite $c $t $e)
macro "if " "let " pat:term " := " d:term " then " t:term " else " e:term : term =>
`(match $d:term with | $pat:term => $t | _ => $e)
syntax:min term "<|" term:min : term
macro_rules
| `($f $args* <| $a) => let args := args.push a; `($f $args*)
| `($f <| $a) => `($f $a)
syntax:min term "|>" term:min1 : term
macro_rules
| `($a |> $f $args*) => let args := args.push a; `($f $args*)
| `($a |> $f) => `($f $a)
-- Haskell-like pipe <|
-- Note that we have a whitespace after `$` to avoid an ambiguity with the antiquotations.
syntax:min term atomic("$" ws) term:min : term
macro_rules
| `($f $args* $ $a) => let args := args.push a; `($f $args*)
| `($f $ $a) => `($f $a)
syntax "{ " ident (" : " term)? " // " term " }" : term
macro_rules
| `({ $x : $type // $p }) => `(Subtype (fun ($x:ident : $type) => $p))
| `({ $x // $p }) => `(Subtype (fun ($x:ident : _) => $p))
/-
`withoutExpected! t` instructs Lean to elaborate `t` without an expected type.
Recall that terms such as `match ... with ...` and `⟨...⟩` will postpone elaboration until
expected type is known. So, `withoutExpected!` is not effective in this case. -/
macro "withoutExpectedType! " x:term : term => `(let aux := $x; aux)
syntax "[" term,* "]" : term
syntax "%[" term,* "|" term "]" : term -- auxiliary notation for creating big list literals
namespace Lean
macro_rules
| `([ $elems,* ]) => do
let rec expandListLit (i : Nat) (skip : Bool) (result : Syntax) : MacroM Syntax := do
match i, skip with
| 0, _ => pure result
| i+1, true => expandListLit i false result
| i+1, false => expandListLit i true (← `(List.cons $(elems.elemsAndSeps[i]) $result))
if elems.elemsAndSeps.size < 64 then
expandListLit elems.elemsAndSeps.size false (← `(List.nil))
else
`(%[ $elems,* | List.nil ])
namespace Parser.Tactic
syntax (name := intro) "intro " notFollowedBy("|") (colGt term:max)* : tactic
syntax (name := intros) "intros " (colGt (ident <|> "_"))* : tactic
syntax (name := revert) "revert " (colGt ident)+ : tactic
syntax (name := clear) "clear " (colGt ident)+ : tactic
syntax (name := subst) "subst " (colGt ident)+ : tactic
syntax (name := assumption) "assumption" : tactic
syntax (name := apply) "apply " term : tactic
syntax (name := exact) "exact " term : tactic
syntax (name := refine) "refine " term : tactic
syntax (name := refine!) "refine! " term : tactic
syntax (name := case) "case " ident " => " tacticSeq : tactic
syntax (name := allGoals) "allGoals " tacticSeq : tactic
syntax (name := focus) "focus " tacticSeq : tactic
syntax (name := skip) "skip" : tactic
syntax (name := done) "done" : tactic
syntax (name := traceState) "traceState" : tactic
syntax (name := failIfSuccess) "failIfSuccess " tacticSeq : tactic
syntax (name := generalize) "generalize " atomic(ident " : ")? term:51 " = " ident : tactic
syntax (name := paren) "(" tacticSeq ")" : tactic
syntax (name := withReducible) "withReducible " tacticSeq : tactic
syntax (name := withReducibleAndInstances) "withReducibleAndInstances " tacticSeq : tactic
syntax (name := first) "first " "|"? sepBy1(tacticSeq, "|") : tactic
macro "try " t:tacticSeq : tactic => `(first $t | skip)
macro:1 x:tactic " <;> " y:tactic:0 : tactic => `(tactic| focus ($x:tactic; allGoals $y:tactic))
macro "rfl" : tactic => `(exact rfl)
macro "decide!" : tactic => `(exact decide!)
macro "admit" : tactic => `(exact sorry)
syntax locationWildcard := "*"
syntax locationTarget := "⊢" <|> "|-"
syntax locationHyp := (colGt ident)+
syntax location := withPosition("at " locationWildcard <|> locationTarget <|> locationHyp)
syntax (name := change) "change " term (location)? : tactic
syntax (name := changeWith) "change " term " with " term (location)? : tactic
syntax rwRule := ("←" <|> "<-")? term
syntax rwRuleSeq := "[" rwRule,+,? "]"
syntax (name := rewrite) "rewrite " rwRule (location)? : tactic
syntax (name := rewriteSeq) (priority := high) "rewrite " rwRuleSeq (location)? : tactic
syntax (name := erewrite) "erewrite " rwRule (location)? : tactic
syntax (name := erewriteSeq) (priority := high) "erewrite " rwRuleSeq (location)? : tactic
syntax (name := rw) "rw " rwRule (location)? : tactic
syntax (name := rwSeq) (priority := high) "rw " rwRuleSeq (location)? : tactic
syntax (name := erw) "erw " rwRule (location)? : tactic
syntax (name := erwSeq) (priority := high) "erw " rwRuleSeq (location)? : tactic
private def withCheapRefl (tac : Syntax) : MacroM Syntax :=
`(tactic| $tac; try (withReducible rfl))
@[macro rw]
def expandRw : Macro :=
fun stx => withCheapRefl (stx.setKind `Lean.Parser.Tactic.rewrite |>.setArg 0 (mkAtomFrom stx "rewrite"))
@[macro rwSeq]
def expandRwSeq : Macro :=
fun stx => withCheapRefl (stx.setKind `Lean.Parser.Tactic.rewriteSeq |>.setArg 0 (mkAtomFrom stx "rewrite"))
@[macro erw]
def expandERw : Macro :=
fun stx => withCheapRefl (stx.setKind `Lean.Parser.Tactic.erewrite |>.setArg 0 (mkAtomFrom stx "erewrite"))
@[macro erwSeq]
def expandERwSeq : Macro :=
fun stx => withCheapRefl (stx.setKind `Lean.Parser.Tactic.erewriteSeq |>.setArg 0 (mkAtomFrom stx "erewrite"))
syntax (name := injection) "injection " term (" with " (colGt (ident <|> "_"))+)? : tactic
syntax simpPre := "↓"
syntax simpPost := "↑"
syntax simpLemma := (simpPre <|> simpPost)? term
syntax (name := simp) "simp " ("[" simpLemma,* "]")? (colGt term)? (location)? : tactic
syntax (name := «have») "have " haveDecl : tactic
syntax (name := «suffices») "suffices " sufficesDecl : tactic
syntax (name := «show») "show " term : tactic
syntax (name := «let») "let " letDecl : tactic
syntax (name := «let!») "let! " letDecl : tactic
syntax (name := letrec) withPosition(atomic(group("let " &"rec ")) letRecDecls) : tactic
syntax inductionAlt := "| " (ident <|> "_") (ident <|> "_")* " => " (hole <|> syntheticHole <|> tacticSeq)
syntax inductionAlts := "with " withPosition( (colGe inductionAlt)+)
syntax (name := induction) "induction " term,+ (" using " ident)? ("generalizing " ident+)? (inductionAlts)? : tactic
syntax casesTarget := atomic(ident " : ")? term
syntax (name := cases) "cases " casesTarget,+ (" using " ident)? (inductionAlts)? : tactic
syntax (name := existsIntro) "exists " term : tactic
/- We use a priority > default, to avoid ambiguity with the builtin `have` notation -/
macro (priority := high) "have" x:ident " := " p:term : tactic => `(have $x:ident : _ := $p)
syntax "repeat " tacticSeq : tactic
macro_rules
| `(tactic| repeat $seq) => `(tactic| first ($seq); repeat $seq | skip)
end Tactic
namespace Attr
-- simp attribute syntax
syntax (name := simp) "simp" (Tactic.simpPre <|> Tactic.simpPost)? (prio)? : attr
end Attr
end Parser
end Lean
|
6847db02d89c0c67b6f7239551dab0d3c274c2b0 | 206422fb9edabf63def0ed2aa3f489150fb09ccb | /src/algebra/module/pi.lean | 6694b062b88a28405fc3e05ac4fbdde08cd27601 | [
"Apache-2.0"
] | permissive | hamdysalah1/mathlib | b915f86b2503feeae268de369f1b16932321f097 | 95454452f6b3569bf967d35aab8d852b1ddf8017 | refs/heads/master | 1,677,154,116,545 | 1,611,797,994,000 | 1,611,797,994,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,788 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Patrick Massot
-/
import algebra.module.basic
import algebra.ring.pi
/-!
# Pi instances for module and multiplicative actions
This file defines instances for module, mul_action and related structures on Pi Types
-/
namespace pi
universes u v w
variable {I : Type u} -- The indexing type
variable {f : I → Type v} -- The family of types already equipped with instances
variables (x y : Π i, f i) (i : I)
instance has_scalar {α : Type*} [Π i, has_scalar α $ f i] :
has_scalar α (Π i : I, f i) :=
⟨λ s x, λ i, s • (x i)⟩
@[simp] lemma smul_apply {α : Type*} [Π i, has_scalar α $ f i] (s : α) : (s • x) i = s • x i := rfl
instance has_scalar' {g : I → Type*} [Π i, has_scalar (f i) (g i)] :
has_scalar (Π i, f i) (Π i : I, g i) :=
⟨λ s x, λ i, (s i) • (x i)⟩
@[simp]
lemma smul_apply' {g : I → Type*} [∀ i, has_scalar (f i) (g i)] (s : Π i, f i) (x : Π i, g i) :
(s • x) i = s i • x i :=
rfl
instance is_scalar_tower {α β : Type*}
[has_scalar α β] [Π i, has_scalar β $ f i] [Π i, has_scalar α $ f i]
[Π i, is_scalar_tower α β (f i)] : is_scalar_tower α β (Π i : I, f i) :=
⟨λ x y z, funext $ λ i, smul_assoc x y (z i)⟩
instance is_scalar_tower' {g : I → Type*} {α : Type*}
[Π i, has_scalar α $ f i] [Π i, has_scalar (f i) (g i)] [Π i, has_scalar α $ g i]
[Π i, is_scalar_tower α (f i) (g i)] : is_scalar_tower α (Π i : I, f i) (Π i : I, g i) :=
⟨λ x y z, funext $ λ i, smul_assoc x (y i) (z i)⟩
instance is_scalar_tower'' {g : I → Type*} {h : I → Type*}
[Π i, has_scalar (f i) (g i)] [Π i, has_scalar (g i) (h i)] [Π i, has_scalar (f i) (h i)]
[Π i, is_scalar_tower (f i) (g i) (h i)] : is_scalar_tower (Π i, f i) (Π i, g i) (Π i, h i) :=
⟨λ x y z, funext $ λ i, smul_assoc (x i) (y i) (z i)⟩
instance mul_action (α) {m : monoid α} [Π i, mul_action α $ f i] :
@mul_action α (Π i : I, f i) m :=
{ smul := (•),
mul_smul := λ r s f, funext $ λ i, mul_smul _ _ _,
one_smul := λ f, funext $ λ i, one_smul α _ }
instance mul_action' {g : I → Type*} {m : Π i, monoid (f i)} [Π i, mul_action (f i) (g i)] :
@mul_action (Π i, f i) (Π i : I, g i) (@pi.monoid I f m) :=
{ smul := (•),
mul_smul := λ r s f, funext $ λ i, mul_smul _ _ _,
one_smul := λ f, funext $ λ i, one_smul _ _ }
instance distrib_mul_action (α) {m : monoid α} {n : ∀ i, add_monoid $ f i}
[∀ i, distrib_mul_action α $ f i] :
@distrib_mul_action α (Π i : I, f i) m (@pi.add_monoid I f n) :=
{ smul_zero := λ c, funext $ λ i, smul_zero _,
smul_add := λ c f g, funext $ λ i, smul_add _ _ _,
..pi.mul_action _ }
instance distrib_mul_action' {g : I → Type*} {m : Π i, monoid (f i)} {n : Π i, add_monoid $ g i}
[Π i, distrib_mul_action (f i) (g i)] :
@distrib_mul_action (Π i, f i) (Π i : I, g i) (@pi.monoid I f m) (@pi.add_monoid I g n) :=
{ smul_add := by { intros, ext x, apply smul_add },
smul_zero := by { intros, ext x, apply smul_zero } }
variables (I f)
instance semimodule (α) {r : semiring α} {m : ∀ i, add_comm_monoid $ f i}
[∀ i, semimodule α $ f i] :
@semimodule α (Π i : I, f i) r (@pi.add_comm_monoid I f m) :=
{ add_smul := λ c f g, funext $ λ i, add_smul _ _ _,
zero_smul := λ f, funext $ λ i, zero_smul α _,
..pi.distrib_mul_action _ }
variables {I f}
instance semimodule' {g : I → Type*} {r : Π i, semiring (f i)} {m : Π i, add_comm_monoid (g i)}
[Π i, semimodule (f i) (g i)] :
semimodule (Π i, f i) (Π i, g i) :=
{ add_smul := by { intros, ext1, apply add_smul },
zero_smul := by { intros, ext1, apply zero_smul } }
end pi
|
2a0c6bfb3c835c00b6b8ed075b0fba8f0e0d2656 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/autoLift.lean | 4d8f90fec2c0ba556d581bba8ee20d398820f6bf | [
"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 | 299 | lean | def f : IO Nat := do
IO.println "foo"
return 0
abbrev M := StateRefT Nat IO
def g (a : Nat) : M Unit :=
pure ()
#check id (α := M Unit) do let a ← f; g a
set_option autoLift false
#check_failure id (α := M Unit) do let a ← f; g a
#check id (α := M Unit) do let a ← liftM f; g a
|
82ed12f959633f0a695f8ce0262799b62a2bafbc | 6e9cd8d58e550c481a3b45806bd34a3514c6b3e0 | /src/topology/instances/ennreal.lean | a67277248f6f9459edfdbb5b8ec925b56dda86df | [
"Apache-2.0"
] | permissive | sflicht/mathlib | 220fd16e463928110e7b0a50bbed7b731979407f | 1b2048d7195314a7e34e06770948ee00f0ac3545 | refs/heads/master | 1,665,934,056,043 | 1,591,373,803,000 | 1,591,373,803,000 | 269,815,267 | 0 | 0 | Apache-2.0 | 1,591,402,068,000 | 1,591,402,067,000 | null | UTF-8 | Lean | false | false | 38,655 | 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
-/
import topology.instances.nnreal
/-!
# Extended non-negative reals
-/
noncomputable theory
open classical set filter metric
open_locale classical
open_locale topological_space
variables {α : Type*} {β : Type*} {γ : Type*}
open_locale ennreal big_operators
namespace ennreal
variables {a b c d : ennreal} {r p q : nnreal}
variables {x y z : ennreal} {ε ε₁ ε₂ : ennreal} {s : set ennreal}
section topological_space
open topological_space
/-- Topology on `ennreal`.
Note: this is different from the `emetric_space` topology. The `emetric_space` topology has
`is_open {⊤}`, while this topology doesn't have singleton elements. -/
instance : topological_space ennreal := preorder.topology ennreal
instance : order_topology ennreal := ⟨rfl⟩
instance : t2_space ennreal := by apply_instance -- short-circuit type class inference
instance : second_countable_topology ennreal :=
⟨⟨⋃q ≥ (0:ℚ), {{a : ennreal | a < nnreal.of_real q}, {a : ennreal | ↑(nnreal.of_real q) < a}},
(countable_encodable _).bUnion $ assume a ha, (countable_singleton _).insert _,
le_antisymm
(le_generate_from $ by simp [or_imp_distrib, is_open_lt', is_open_gt'] {contextual := tt})
(le_generate_from $ λ s h, begin
rcases h with ⟨a, hs | hs⟩;
[ rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ a < nnreal.of_real q}, {b | ↑(nnreal.of_real q) < b},
from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn a b, and_assoc]),
rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ ↑(nnreal.of_real q) < a}, {b | b < ↑(nnreal.of_real q)},
from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn b a, and_comm, and_assoc])];
{ apply is_open_Union, intro q,
apply is_open_Union, intro hq,
exact generate_open.basic _ (mem_bUnion hq.1 $ by simp) }
end)⟩⟩
lemma embedding_coe : embedding (coe : nnreal → ennreal) :=
⟨⟨begin
refine le_antisymm _ _,
{ rw [@order_topology.topology_eq_generate_intervals ennreal _,
← coinduced_le_iff_le_induced],
refine le_generate_from (assume s ha, _),
rcases ha with ⟨a, rfl | rfl⟩,
show is_open {b : nnreal | a < ↑b},
{ cases a; simp [none_eq_top, some_eq_coe, is_open_lt'] },
show is_open {b : nnreal | ↑b < a},
{ cases a; simp [none_eq_top, some_eq_coe, is_open_gt', is_open_const] } },
{ rw [@order_topology.topology_eq_generate_intervals nnreal _],
refine le_generate_from (assume s ha, _),
rcases ha with ⟨a, rfl | rfl⟩,
exact ⟨Ioi a, is_open_Ioi, by simp [Ioi]⟩,
exact ⟨Iio a, is_open_Iio, by simp [Iio]⟩ }
end⟩,
assume a b, coe_eq_coe.1⟩
lemma is_open_ne_top : is_open {a : ennreal | a ≠ ⊤} := is_open_ne
lemma is_open_Ico_zero : is_open (Ico 0 b) := by { rw ennreal.Ico_eq_Iio, exact is_open_Iio}
lemma coe_range_mem_nhds : range (coe : nnreal → ennreal) ∈ 𝓝 (r : ennreal) :=
have {a : ennreal | a ≠ ⊤} = range (coe : nnreal → ennreal),
from set.ext $ assume a, by cases a; simp [none_eq_top, some_eq_coe],
this ▸ mem_nhds_sets is_open_ne_top coe_ne_top
@[norm_cast] lemma tendsto_coe {f : filter α} {m : α → nnreal} {a : nnreal} :
tendsto (λa, (m a : ennreal)) f (𝓝 ↑a) ↔ tendsto m f (𝓝 a) :=
embedding_coe.tendsto_nhds_iff.symm
lemma continuous_coe {α} [topological_space α] {f : α → nnreal} :
continuous (λa, (f a : ennreal)) ↔ continuous f :=
embedding_coe.continuous_iff.symm
lemma nhds_coe {r : nnreal} : 𝓝 (r : ennreal) = (𝓝 r).map coe :=
by rw [embedding_coe.induced, map_nhds_induced_eq coe_range_mem_nhds]
lemma nhds_coe_coe {r p : nnreal} : 𝓝 ((r : ennreal), (p : ennreal)) =
(𝓝 (r, p)).map (λp:nnreal×nnreal, (p.1, p.2)) :=
begin
rw [(embedding_coe.prod_mk embedding_coe).map_nhds_eq],
rw [← prod_range_range_eq],
exact prod_mem_nhds_sets coe_range_mem_nhds coe_range_mem_nhds
end
lemma continuous_of_real : continuous ennreal.of_real :=
(continuous_coe.2 continuous_id).comp nnreal.continuous_of_real
lemma tendsto_of_real {f : filter α} {m : α → ℝ} {a : ℝ} (h : tendsto m f (𝓝 a)) :
tendsto (λa, ennreal.of_real (m a)) f (𝓝 (ennreal.of_real a)) :=
tendsto.comp (continuous.tendsto continuous_of_real _) h
lemma tendsto_to_nnreal {a : ennreal} : a ≠ ⊤ →
tendsto (ennreal.to_nnreal) (𝓝 a) (𝓝 a.to_nnreal) :=
begin
cases a; simp [some_eq_coe, none_eq_top, nhds_coe, tendsto_map'_iff, (∘)],
exact tendsto_id
end
lemma continuous_on_to_nnreal : continuous_on ennreal.to_nnreal {a | a ≠ ∞} :=
continuous_on_iff_continuous_restrict.2 $ continuous_iff_continuous_at.2 $ λ x,
(tendsto_to_nnreal x.2).comp continuous_at_subtype_val
lemma tendsto_to_real {a : ennreal} : a ≠ ⊤ → tendsto (ennreal.to_real) (𝓝 a) (𝓝 a.to_real) :=
λ ha, tendsto.comp ((@nnreal.tendsto_coe _ (𝓝 a.to_nnreal) id (a.to_nnreal)).2 tendsto_id)
(tendsto_to_nnreal ha)
lemma tendsto_nhds_top {m : α → ennreal} {f : filter α}
(h : ∀ n : ℕ, ∀ᶠ a in f, ↑n < m a) : tendsto m f (𝓝 ⊤) :=
tendsto_nhds_generate_from $ assume s hs,
match s, hs with
| _, ⟨none, or.inl rfl⟩, hr := (lt_irrefl ⊤ hr).elim
| _, ⟨some r, or.inl rfl⟩, hr :=
let ⟨n, hrn⟩ := exists_nat_gt r in
mem_sets_of_superset (h n) $ assume a hnma, show ↑r < m a, from
lt_trans (show (r : ennreal) < n, from (coe_nat n) ▸ coe_lt_coe.2 hrn) hnma
| _, ⟨a, or.inr rfl⟩, hr := (not_top_lt $ show ⊤ < a, from hr).elim
end
lemma tendsto_nat_nhds_top : tendsto (λ n : ℕ, ↑n) at_top (𝓝 ∞) :=
tendsto_nhds_top $ λ n, mem_at_top_sets.2
⟨n+1, λ m hm, ennreal.coe_nat_lt_coe_nat.2 $ nat.lt_of_succ_le hm⟩
lemma nhds_top : 𝓝 ∞ = ⨅a ≠ ∞, principal (Ioi a) :=
nhds_top_order.trans $ by simp [lt_top_iff_ne_top, Ioi]
lemma nhds_zero : 𝓝 (0 : ennreal) = ⨅a ≠ 0, principal (Iio a) :=
nhds_bot_order.trans $ by simp [bot_lt_iff_ne_bot, Iio]
/-- The set of finite `ennreal` numbers is homeomorphic to `nnreal`. -/
def ne_top_homeomorph_nnreal : {a | a ≠ ∞} ≃ₜ nnreal :=
{ 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,
continuous_to_fun := continuous_on_iff_continuous_restrict.1 continuous_on_to_nnreal,
continuous_inv_fun := continuous_subtype_mk _ (continuous_coe.2 continuous_id) }
/-- The set of finite `ennreal` numbers is homeomorphic to `nnreal`. -/
def lt_top_homeomorph_nnreal : {a | a < ∞} ≃ₜ nnreal :=
by refine (homeomorph.set_congr $ set.ext $ λ x, _).trans ne_top_homeomorph_nnreal;
simp only [mem_set_of_eq, lt_top_iff_ne_top]
-- using Icc because
-- • don't have 'Ioo (x - ε) (x + ε) ∈ 𝓝 x' unless x > 0
-- • (x - y ≤ ε ↔ x ≤ ε + y) is true, while (x - y < ε ↔ x < ε + y) is not
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma Icc_mem_nhds : x ≠ ⊤ → ε > 0 → Icc (x - ε) (x + ε) ∈ 𝓝 x :=
begin
assume xt ε0, rw mem_nhds_sets_iff,
by_cases x0 : x = 0,
{ use Iio (x + ε),
have : Iio (x + ε) ⊆ Icc (x - ε) (x + ε), assume a, rw x0, simpa using le_of_lt,
use this, exact ⟨is_open_Iio, mem_Iio_self_add xt ε0⟩ },
{ use Ioo (x - ε) (x + ε), use Ioo_subset_Icc_self,
exact ⟨is_open_Ioo, mem_Ioo_self_sub_add xt x0 ε0 ε0 ⟩ }
end
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma nhds_of_ne_top : x ≠ ⊤ → 𝓝 x = ⨅ε > 0, principal (Icc (x - ε) (x + ε)) :=
begin
assume xt, refine le_antisymm _ _,
-- first direction
simp only [le_infi_iff, le_principal_iff], assume ε ε0, exact Icc_mem_nhds xt ε0,
-- second direction
rw nhds_generate_from, refine le_infi (assume s, le_infi $ assume hs, _),
simp only [mem_set_of_eq] at hs, rcases hs with ⟨xs, ⟨a, ha⟩⟩,
cases ha,
{ rw ha at *,
rcases dense xs with ⟨b, ⟨ab, bx⟩⟩,
have xb_pos : x - b > 0 := zero_lt_sub_iff_lt.2 bx,
have xxb : x - (x - b) = b := sub_sub_cancel (by rwa lt_top_iff_ne_top) (le_of_lt bx),
refine infi_le_of_le (x - b) (infi_le_of_le xb_pos _),
simp only [mem_principal_sets, le_principal_iff],
assume y, rintros ⟨h₁, h₂⟩, rw xxb at h₁, calc a < b : ab ... ≤ y : h₁ },
{ rw ha at *,
rcases dense xs with ⟨b, ⟨xb, ba⟩⟩,
have bx_pos : b - x > 0 := zero_lt_sub_iff_lt.2 xb,
have xbx : x + (b - x) = b := add_sub_cancel_of_le (le_of_lt xb),
refine infi_le_of_le (b - x) (infi_le_of_le bx_pos _),
simp only [mem_principal_sets, le_principal_iff],
assume y, rintros ⟨h₁, h₂⟩, rw xbx at h₂, calc y ≤ b : h₂ ... < a : ba },
end
/-- Characterization of neighborhoods for `ennreal` numbers. See also `tendsto_order`
for a version with strict inequalities. -/
@[nolint ge_or_gt] -- see Note [nolint_ge]
protected theorem tendsto_nhds {f : filter α} {u : α → ennreal} {a : ennreal} (ha : a ≠ ⊤) :
tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, (u x) ∈ Icc (a - ε) (a + ε) :=
by simp only [nhds_of_ne_top ha, tendsto_infi, tendsto_principal, mem_Icc]
protected lemma tendsto_at_top [nonempty β] [semilattice_sup β] {f : β → ennreal} {a : ennreal}
(ha : a ≠ ⊤) : tendsto f at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n≥N, (f n) ∈ Icc (a - ε) (a + ε) :=
by simp only [ennreal.tendsto_nhds ha, mem_at_top_sets, mem_set_of_eq, filter.eventually]
lemma tendsto_coe_nnreal_nhds_top {α} {l : filter α} {f : α → nnreal} (h : tendsto f l at_top) :
tendsto (λa, (f a : ennreal)) l (𝓝 ∞) :=
tendsto_nhds_top $ assume n,
have ∀ᶠ a in l, ↑(n+1) ≤ f a := h $ mem_at_top _,
mem_sets_of_superset this $ assume a (ha : ↑(n+1) ≤ f a),
begin
rw [← coe_nat],
dsimp,
exact coe_lt_coe.2 (lt_of_lt_of_le (nat.cast_lt.2 (nat.lt_succ_self _)) ha)
end
instance : topological_add_monoid ennreal :=
⟨ continuous_iff_continuous_at.2 $
have hl : ∀a:ennreal, tendsto (λ (p : ennreal × ennreal), p.fst + p.snd) (𝓝 (⊤, a)) (𝓝 ⊤), from
assume a, tendsto_nhds_top $ assume n,
have set.prod {a | ↑n < a } univ ∈ 𝓝 ((⊤:ennreal), a), from
prod_mem_nhds_sets (lt_mem_nhds $ coe_nat n ▸ coe_lt_top) univ_mem_sets,
show {a : ennreal × ennreal | ↑n < a.fst + a.snd} ∈ 𝓝 (⊤, a),
begin filter_upwards [this] assume ⟨a₁, a₂⟩ ⟨h₁, h₂⟩, lt_of_lt_of_le h₁ (le_add_right $ le_refl _) end,
begin
rintro ⟨a₁, a₂⟩,
cases a₁, { simp [continuous_at, none_eq_top, hl a₂], },
cases a₂, { simp [continuous_at, none_eq_top, some_eq_coe, nhds_swap (a₁ : ennreal) ⊤,
tendsto_map'_iff, (∘)], convert hl a₁, simp [add_comm] },
simp [continuous_at, some_eq_coe, nhds_coe_coe, tendsto_map'_iff, (∘)],
simp only [coe_add.symm, tendsto_coe, tendsto_add]
end ⟩
protected lemma tendsto_mul (ha : a ≠ 0 ∨ b ≠ ⊤) (hb : b ≠ 0 ∨ a ≠ ⊤) :
tendsto (λp:ennreal×ennreal, p.1 * p.2) (𝓝 (a, b)) (𝓝 (a * b)) :=
have ht : ∀b:ennreal, b ≠ 0 → tendsto (λp:ennreal×ennreal, p.1 * p.2) (𝓝 ((⊤:ennreal), b)) (𝓝 ⊤),
begin
refine assume b hb, tendsto_nhds_top $ assume n, _,
rcases dense (zero_lt_iff_ne_zero.2 hb) with ⟨ε', hε', hεb'⟩,
rcases ennreal.lt_iff_exists_coe.1 hεb' with ⟨ε, rfl, h⟩,
rcases exists_nat_gt (↑n / ε) with ⟨m, hm⟩,
have hε : ε > 0, from coe_lt_coe.1 hε',
refine mem_sets_of_superset (prod_mem_nhds_sets (lt_mem_nhds $ @coe_lt_top m) (lt_mem_nhds $ h)) _,
rintros ⟨a₁, a₂⟩ ⟨h₁, h₂⟩,
dsimp at h₁ h₂ ⊢,
calc (n:ennreal) = ↑(((n:nnreal) / ε) * ε) :
begin
simp [nnreal.div_def],
rw [mul_assoc, ← coe_mul, nnreal.inv_mul_cancel, coe_one, ← coe_nat, mul_one],
exact zero_lt_iff_ne_zero.1 hε
end
... < (↑m * ε : nnreal) : coe_lt_coe.2 $ mul_lt_mul hm (le_refl _) hε (nat.cast_nonneg _)
... ≤ a₁ * a₂ : by rw [coe_mul]; exact canonically_ordered_semiring.mul_le_mul
(le_of_lt h₁)
(le_of_lt h₂)
end,
begin
cases a, {simp [none_eq_top] at hb, simp [none_eq_top, ht b hb, top_mul, hb] },
cases b, {
simp [none_eq_top] at ha,
simp [*, nhds_swap (a : ennreal) ⊤, none_eq_top, some_eq_coe, top_mul, tendsto_map'_iff, (∘), mul_comm] },
simp [some_eq_coe, nhds_coe_coe, tendsto_map'_iff, (∘)],
simp only [coe_mul.symm, tendsto_coe, tendsto_mul]
end
protected lemma tendsto.mul {f : filter α} {ma : α → ennreal} {mb : α → ennreal} {a b : ennreal}
(hma : tendsto ma f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ ⊤) (hmb : tendsto mb f (𝓝 b)) (hb : b ≠ 0 ∨ a ≠ ⊤) :
tendsto (λa, ma a * mb a) f (𝓝 (a * b)) :=
show tendsto ((λp:ennreal×ennreal, p.1 * p.2) ∘ (λa, (ma a, mb a))) f (𝓝 (a * b)), from
tendsto.comp (ennreal.tendsto_mul ha hb) (hma.prod_mk_nhds hmb)
protected lemma tendsto.const_mul {f : filter α} {m : α → ennreal} {a b : ennreal}
(hm : tendsto m f (𝓝 b)) (hb : b ≠ 0 ∨ a ≠ ⊤) : tendsto (λb, a * m b) f (𝓝 (a * b)) :=
by_cases
(assume : a = 0, by simp [this, tendsto_const_nhds])
(assume ha : a ≠ 0, ennreal.tendsto.mul tendsto_const_nhds (or.inl ha) hm hb)
protected lemma tendsto.mul_const {f : filter α} {m : α → ennreal} {a b : ennreal}
(hm : tendsto m f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ ⊤) : tendsto (λx, m x * b) f (𝓝 (a * b)) :=
by simpa only [mul_comm] using ennreal.tendsto.const_mul hm ha
protected lemma continuous_const_mul {a : ennreal} (ha : a < ⊤) : continuous ((*) a) :=
continuous_iff_continuous_at.2 $ λ x, tendsto.const_mul tendsto_id $ or.inr $ ne_of_lt ha
protected lemma continuous_mul_const {a : ennreal} (ha : a < ⊤) : continuous (λ x, x * a) :=
by simpa only [mul_comm] using ennreal.continuous_const_mul ha
protected lemma continuous_inv : continuous (has_inv.inv : ennreal → ennreal) :=
continuous_iff_continuous_at.2 $ λ a, tendsto_order.2
⟨begin
assume b hb,
simp only [@ennreal.lt_inv_iff_lt_inv b],
exact gt_mem_nhds (ennreal.lt_inv_iff_lt_inv.1 hb),
end,
begin
assume b hb,
simp only [gt_iff_lt, @ennreal.inv_lt_iff_inv_lt _ b],
exact lt_mem_nhds (ennreal.inv_lt_iff_inv_lt.1 hb)
end⟩
@[simp] protected lemma tendsto_inv_iff {f : filter α} {m : α → ennreal} {a : ennreal} :
tendsto (λ x, (m x)⁻¹) f (𝓝 a⁻¹) ↔ tendsto m f (𝓝 a) :=
⟨λ h, by simpa only [function.comp, ennreal.inv_inv]
using (ennreal.continuous_inv.tendsto a⁻¹).comp h,
(ennreal.continuous_inv.tendsto a).comp⟩
protected lemma tendsto.div {f : filter α} {ma : α → ennreal} {mb : α → ennreal} {a b : ennreal}
(hma : tendsto ma f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ 0) (hmb : tendsto mb f (𝓝 b)) (hb : b ≠ ⊤ ∨ a ≠ ⊤) :
tendsto (λa, ma a / mb a) f (𝓝 (a / b)) :=
by { apply tendsto.mul hma _ (ennreal.tendsto_inv_iff.2 hmb) _; simp [ha, hb] }
protected lemma tendsto.const_div {f : filter α} {m : α → ennreal} {a b : ennreal}
(hm : tendsto m f (𝓝 b)) (hb : b ≠ ⊤ ∨ a ≠ ⊤) : tendsto (λb, a / m b) f (𝓝 (a / b)) :=
by { apply tendsto.const_mul (ennreal.tendsto_inv_iff.2 hm), simp [hb] }
protected lemma tendsto.div_const {f : filter α} {m : α → ennreal} {a b : ennreal}
(hm : tendsto m f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ 0) : tendsto (λx, m x / b) f (𝓝 (a / b)) :=
by { apply tendsto.mul_const hm, simp [ha] }
protected lemma tendsto_inv_nat_nhds_zero : tendsto (λ n : ℕ, (n : ennreal)⁻¹) at_top (𝓝 0) :=
ennreal.inv_top ▸ ennreal.tendsto_inv_iff.2 tendsto_nat_nhds_top
lemma Sup_add {s : set ennreal} (hs : s.nonempty) : Sup s + a = ⨆b∈s, b + a :=
have Sup ((λb, b + a) '' s) = Sup s + a,
from is_lub.Sup_eq (is_lub_of_is_lub_of_tendsto
(assume x _ y _ h, add_le_add' h (le_refl _))
(is_lub_Sup s)
hs
(tendsto.add (tendsto_id' inf_le_left) tendsto_const_nhds)),
by simp [Sup_image, -add_comm] at this; exact this.symm
lemma supr_add {ι : Sort*} {s : ι → ennreal} [h : nonempty ι] : supr s + a = ⨆b, s b + a :=
let ⟨x⟩ := h in
calc supr s + a = Sup (range s) + a : by simp [Sup_range]
... = (⨆b∈range s, b + a) : Sup_add ⟨s x, x, rfl⟩
... = _ : supr_range
lemma add_supr {ι : Sort*} {s : ι → ennreal} [h : nonempty ι] : a + supr s = ⨆b, a + s b :=
by rw [add_comm, supr_add]; simp [add_comm]
lemma supr_add_supr {ι : Sort*} {f g : ι → ennreal} (h : ∀i j, ∃k, f i + g j ≤ f k + g k) :
supr f + supr g = (⨆ a, f a + g a) :=
begin
by_cases hι : nonempty ι,
{ letI := hι,
refine le_antisymm _ (supr_le $ λ a, add_le_add' (le_supr _ _) (le_supr _ _)),
simpa [add_supr, supr_add] using
λ i j:ι, show f i + g j ≤ ⨆ a, f a + g a, from
let ⟨k, hk⟩ := h i j in le_supr_of_le k hk },
{ have : ∀f:ι → ennreal, (⨆i, f i) = 0 := assume f, bot_unique (supr_le $ assume i, (hι ⟨i⟩).elim),
rw [this, this, this, zero_add] }
end
lemma supr_add_supr_of_monotone {ι : Sort*} [semilattice_sup ι]
{f g : ι → ennreal} (hf : monotone f) (hg : monotone g) :
supr f + supr g = (⨆ a, f a + g a) :=
supr_add_supr $ assume i j, ⟨i ⊔ j, add_le_add' (hf $ le_sup_left) (hg $ le_sup_right)⟩
lemma finset_sum_supr_nat {α} {ι} [semilattice_sup ι] {s : finset α} {f : α → ι → ennreal}
(hf : ∀a, monotone (f a)) :
s.sum (λa, supr (f a)) = (⨆ n, s.sum (λa, f a n)) :=
begin
refine finset.induction_on s _ _,
{ simp,
exact (bot_unique $ supr_le $ assume i, le_refl ⊥).symm },
{ assume a s has ih,
simp only [finset.sum_insert has],
rw [ih, supr_add_supr_of_monotone (hf a)],
assume i j h,
exact (finset.sum_le_sum $ assume a ha, hf a h) }
end
section priority
-- for some reason the next proof fails without changing the priority of this instance
local attribute [instance, priority 1000] classical.prop_decidable
lemma mul_Sup {s : set ennreal} {a : ennreal} : a * Sup s = ⨆i∈s, a * i :=
begin
by_cases hs : ∀x∈s, x = (0:ennreal),
{ have h₁ : Sup s = 0 := (bot_unique $ Sup_le $ assume a ha, (hs a ha).symm ▸ le_refl 0),
have h₂ : (⨆i ∈ s, a * i) = 0 :=
(bot_unique $ supr_le $ assume a, supr_le $ assume ha, by simp [hs a ha]),
rw [h₁, h₂, mul_zero] },
{ simp only [not_forall] at hs,
rcases hs with ⟨x, hx, hx0⟩,
have s₁ : Sup s ≠ 0 :=
zero_lt_iff_ne_zero.1 (lt_of_lt_of_le (zero_lt_iff_ne_zero.2 hx0) (le_Sup hx)),
have : Sup ((λb, a * b) '' s) = a * Sup s :=
is_lub.Sup_eq (is_lub_of_is_lub_of_tendsto
(assume x _ y _ h, canonically_ordered_semiring.mul_le_mul (le_refl _) h)
(is_lub_Sup _)
⟨x, hx⟩
(ennreal.tendsto.const_mul (tendsto_id' inf_le_left) (or.inl s₁))),
rw [this.symm, Sup_image] }
end
end priority
lemma mul_supr {ι : Sort*} {f : ι → ennreal} {a : ennreal} : a * supr f = ⨆i, a * f i :=
by rw [← Sup_range, mul_Sup, supr_range]
lemma supr_mul {ι : Sort*} {f : ι → ennreal} {a : ennreal} : supr f * a = ⨆i, f i * a :=
by rw [mul_comm, mul_supr]; congr; funext; rw [mul_comm]
protected lemma tendsto_coe_sub : ∀{b:ennreal}, tendsto (λb:ennreal, ↑r - b) (𝓝 b) (𝓝 (↑r - b)) :=
begin
refine (forall_ennreal.2 $ and.intro (assume a, _) _),
{ simp [@nhds_coe a, tendsto_map'_iff, (∘), tendsto_coe, coe_sub.symm],
exact nnreal.tendsto.sub tendsto_const_nhds tendsto_id },
simp,
exact (tendsto.congr' (mem_sets_of_superset (lt_mem_nhds $ @coe_lt_top r) $
by simp [le_of_lt] {contextual := tt})) tendsto_const_nhds
end
lemma sub_supr {ι : Sort*} [hι : nonempty ι] {b : ι → ennreal} (hr : a < ⊤) :
a - (⨆i, b i) = (⨅i, a - b i) :=
let ⟨i⟩ := hι in
let ⟨r, eq, _⟩ := lt_iff_exists_coe.mp hr in
have Inf ((λb, ↑r - b) '' range b) = ↑r - (⨆i, b i),
from is_glb.Inf_eq $ is_glb_of_is_lub_of_tendsto
(assume x _ y _, sub_le_sub (le_refl _))
is_lub_supr
⟨_, i, rfl⟩
(tendsto.comp ennreal.tendsto_coe_sub (tendsto_id' inf_le_left)),
by rw [eq, ←this]; simp [Inf_image, infi_range, -mem_range]; exact le_refl _
end topological_space
section tsum
variables {f g : α → ennreal}
@[norm_cast] protected lemma has_sum_coe {f : α → nnreal} {r : nnreal} :
has_sum (λa, (f a : ennreal)) ↑r ↔ has_sum f r :=
have (λs:finset α, s.sum (coe ∘ f)) = (coe : nnreal → ennreal) ∘ (λs:finset α, s.sum f),
from funext $ assume s, ennreal.coe_finset_sum.symm,
by unfold has_sum; rw [this, tendsto_coe]
protected lemma tsum_coe_eq {f : α → nnreal} (h : has_sum f r) : (∑'a, (f a : ennreal)) = r :=
tsum_eq_has_sum $ ennreal.has_sum_coe.2 $ h
protected lemma coe_tsum {f : α → nnreal} : summable f → ↑(tsum f) = (∑'a, (f a : ennreal))
| ⟨r, hr⟩ := by rw [tsum_eq_has_sum hr, ennreal.tsum_coe_eq hr]
protected lemma has_sum : has_sum f (⨆s:finset α, s.sum f) :=
tendsto_order.2
⟨assume a' ha',
let ⟨s, hs⟩ := lt_supr_iff.mp ha' in
mem_at_top_sets.mpr ⟨s, assume t ht, lt_of_lt_of_le hs $ finset.sum_le_sum_of_subset ht⟩,
assume a' ha',
univ_mem_sets' $ assume s,
have s.sum f ≤ ⨆(s : finset α), s.sum f,
from le_supr (λ(s : finset α), s.sum f) s,
lt_of_le_of_lt this ha'⟩
@[simp] protected lemma summable : summable f := ⟨_, ennreal.has_sum⟩
lemma tsum_coe_ne_top_iff_summable {f : β → nnreal} :
(∑' b, (f b:ennreal)) ≠ ∞ ↔ summable f :=
begin
refine ⟨λ h, _, λ h, ennreal.coe_tsum h ▸ ennreal.coe_ne_top⟩,
lift (∑' b, (f b:ennreal)) to nnreal using h with a ha,
refine ⟨a, ennreal.has_sum_coe.1 _⟩,
rw ha,
exact ennreal.summable.has_sum
end
protected lemma tsum_eq_supr_sum : (∑'a, f a) = (⨆s:finset α, s.sum f) :=
tsum_eq_has_sum ennreal.has_sum
protected lemma tsum_eq_supr_sum' {ι : Type*} (s : ι → finset α) (hs : ∀ t, ∃ i, t ⊆ s i) :
(∑' a, f a) = ⨆ i, (s i).sum f :=
begin
rw [ennreal.tsum_eq_supr_sum],
symmetry,
change (⨆i:ι, (λ t : finset α, t.sum f) (s i)) = ⨆s:finset α, s.sum f,
exact (finset.sum_mono_set f).supr_comp_eq hs
end
protected lemma tsum_sigma {β : α → Type*} (f : Πa, β a → ennreal) :
(∑'p:Σa, β a, f p.1 p.2) = (∑'a b, f a b) :=
tsum_sigma (assume b, ennreal.summable) ennreal.summable
protected lemma tsum_sigma' {β : α → Type*} (f : (Σ a, β a) → ennreal) :
(∑'p:(Σa, β a), f p) = (∑'a b, f ⟨a, b⟩) :=
tsum_sigma (assume b, ennreal.summable) ennreal.summable
protected lemma tsum_prod {f : α → β → ennreal} : (∑'p:α×β, f p.1 p.2) = (∑'a, ∑'b, f a b) :=
let j : α × β → (Σa:α, β) := λp, sigma.mk p.1 p.2 in
let i : (Σa:α, β) → α × β := λp, (p.1, p.2) in
let f' : (Σa:α, β) → ennreal := λp, f p.1 p.2 in
calc (∑'p:α×β, f' (j p)) = (∑'p:Σa:α, β, f p.1 p.2) :
tsum_eq_tsum_of_iso j i (assume ⟨a, b⟩, rfl) (assume ⟨a, b⟩, rfl)
... = (∑'a, ∑'b, f a b) : ennreal.tsum_sigma f
protected lemma tsum_comm {f : α → β → ennreal} : (∑'a, ∑'b, f a b) = (∑'b, ∑'a, f a b) :=
let f' : α×β → ennreal := λp, f p.1 p.2 in
calc (∑'a, ∑'b, f a b) = (∑'p:α×β, f' p) : ennreal.tsum_prod.symm
... = (∑'p:β×α, f' (prod.swap p)) :
(tsum_eq_tsum_of_iso prod.swap (@prod.swap α β) (assume ⟨a, b⟩, rfl) (assume ⟨a, b⟩, rfl)).symm
... = (∑'b, ∑'a, f' (prod.swap (b, a))) : @ennreal.tsum_prod β α (λb a, f' (prod.swap (b, a)))
protected lemma tsum_add : (∑'a, f a + g a) = (∑'a, f a) + (∑'a, g a) :=
tsum_add ennreal.summable ennreal.summable
protected lemma tsum_le_tsum (h : ∀a, f a ≤ g a) : (∑'a, f a) ≤ (∑'a, g a) :=
tsum_le_tsum h ennreal.summable ennreal.summable
protected lemma tsum_eq_supr_nat {f : ℕ → ennreal} :
(∑'i:ℕ, f i) = (⨆i:ℕ, (finset.range i).sum f) :=
ennreal.tsum_eq_supr_sum' _ finset.exists_nat_subset_range
protected lemma le_tsum (a : α) : f a ≤ (∑'a, f a) :=
calc f a = ({a} : finset α).sum f : by simp
... ≤ (⨆s:finset α, s.sum f) : le_supr (λs:finset α, s.sum f) _
... = (∑'a, f a) : by rw [ennreal.tsum_eq_supr_sum]
protected lemma tsum_eq_top_of_eq_top : (∃ a, f a = ∞) → (∑' a, f a) = ∞
| ⟨a, ha⟩ := top_unique $ ha ▸ ennreal.le_tsum a
protected lemma ne_top_of_tsum_ne_top (h : (∑' a, f a) ≠ ∞) (a : α) : f a ≠ ∞ :=
λ ha, h $ ennreal.tsum_eq_top_of_eq_top ⟨a, ha⟩
protected lemma tsum_mul_left : (∑'i, a * f i) = a * (∑'i, f i) :=
if h : ∀i, f i = 0 then by simp [h] else
let ⟨i, (hi : f i ≠ 0)⟩ := classical.not_forall.mp h in
have sum_ne_0 : (∑'i, f i) ≠ 0, from ne_of_gt $
calc 0 < f i : lt_of_le_of_ne (zero_le _) hi.symm
... ≤ (∑'i, f i) : ennreal.le_tsum _,
have tendsto (λs:finset α, s.sum ((*) a ∘ f)) at_top (𝓝 (a * (∑'i, f i))),
by rw [← show (*) a ∘ (λs:finset α, s.sum f) = λs, s.sum ((*) a ∘ f),
from funext $ λ s, finset.mul_sum];
exact ennreal.tendsto.const_mul ennreal.summable.has_sum (or.inl sum_ne_0),
tsum_eq_has_sum this
protected lemma tsum_mul_right : (∑'i, f i * a) = (∑'i, f i) * a :=
by simp [mul_comm, ennreal.tsum_mul_left]
@[simp] lemma tsum_supr_eq {α : Type*} (a : α) {f : α → ennreal} :
(∑'b:α, ⨆ (h : a = b), f b) = f a :=
le_antisymm
(by rw [ennreal.tsum_eq_supr_sum]; exact supr_le (assume s,
calc (∑ b in s, ⨆ (h : a = b), f b) ≤ ∑ b in {a}, ⨆ (h : a = b), f b :
finset.sum_le_sum_of_ne_zero $ assume b _ hb,
suffices a = b, by simpa using this.symm,
classical.by_contradiction $ assume h,
by simpa [h] using hb
... = f a : by simp))
(calc f a ≤ (⨆ (h : a = a), f a) : le_supr (λh:a=a, f a) rfl
... ≤ (∑'b:α, ⨆ (h : a = b), f b) : ennreal.le_tsum _)
lemma has_sum_iff_tendsto_nat {f : ℕ → ennreal} (r : ennreal) :
has_sum f r ↔ tendsto (λn:ℕ, (finset.range n).sum f) at_top (𝓝 r) :=
begin
refine ⟨has_sum.tendsto_sum_nat, assume h, _⟩,
rw [← supr_eq_of_tendsto _ h, ← ennreal.tsum_eq_supr_nat],
{ exact ennreal.summable.has_sum },
{ exact assume s t hst, finset.sum_le_sum_of_subset (finset.range_subset.2 hst) }
end
end tsum
end ennreal
namespace nnreal
lemma exists_le_has_sum_of_le {f g : β → nnreal} {r : nnreal}
(hgf : ∀b, g b ≤ f b) (hfr : has_sum f r) : ∃p≤r, has_sum g p :=
have (∑'b, (g b : ennreal)) ≤ r,
begin
refine has_sum_le (assume b, _) ennreal.summable.has_sum (ennreal.has_sum_coe.2 hfr),
exact ennreal.coe_le_coe.2 (hgf _)
end,
let ⟨p, eq, hpr⟩ := ennreal.le_coe_iff.1 this in
⟨p, hpr, ennreal.has_sum_coe.1 $ eq ▸ ennreal.summable.has_sum⟩
lemma summable_of_le {f g : β → nnreal} (hgf : ∀b, g b ≤ f b) : summable f → summable g
| ⟨r, hfr⟩ := let ⟨p, _, hp⟩ := exists_le_has_sum_of_le hgf hfr in hp.summable
lemma has_sum_iff_tendsto_nat {f : ℕ → nnreal} (r : nnreal) :
has_sum f r ↔ tendsto (λn:ℕ, (finset.range n).sum f) at_top (𝓝 r) :=
begin
rw [← ennreal.has_sum_coe, ennreal.has_sum_iff_tendsto_nat],
simp only [ennreal.coe_finset_sum.symm],
exact ennreal.tendsto_coe
end
lemma tsum_comp_le_tsum_of_inj {β : Type*} {f : α → nnreal} (hf : summable f)
{i : β → α} (hi : function.injective i) : tsum (f ∘ i) ≤ tsum f :=
tsum_le_tsum_of_inj i hi (λ c hc, zero_le _) (λ b, le_refl _) (summable_comp_injective hf hi) hf
end nnreal
lemma tsum_comp_le_tsum_of_inj {β : Type*} {f : α → ℝ} (hf : summable f) (hn : ∀ a, 0 ≤ f a)
{i : β → α} (hi : function.injective i) : tsum (f ∘ i) ≤ tsum f :=
begin
let g : α → nnreal := λ a, ⟨f a, hn a⟩,
have hg : summable g, by rwa ← nnreal.summable_coe,
convert nnreal.coe_le_coe.2 (nnreal.tsum_comp_le_tsum_of_inj hg hi);
{ rw nnreal.coe_tsum, congr }
end
lemma summable_of_nonneg_of_le {f g : β → ℝ}
(hg : ∀b, 0 ≤ g b) (hgf : ∀b, g b ≤ f b) (hf : summable f) : summable g :=
let f' (b : β) : nnreal := ⟨f b, le_trans (hg b) (hgf b)⟩ in
let g' (b : β) : nnreal := ⟨g b, hg b⟩ in
have summable f', from nnreal.summable_coe.1 hf,
have summable g', from
nnreal.summable_of_le (assume b, (@nnreal.coe_le_coe (g' b) (f' b)).2 $ hgf b) this,
show summable (λb, g' b : β → ℝ), from nnreal.summable_coe.2 this
lemma has_sum_iff_tendsto_nat_of_nonneg {f : ℕ → ℝ} (hf : ∀i, 0 ≤ f i) (r : ℝ) :
has_sum f r ↔ tendsto (λn:ℕ, (finset.range n).sum f) at_top (𝓝 r) :=
⟨has_sum.tendsto_sum_nat,
assume hfr,
have 0 ≤ r := ge_of_tendsto at_top_ne_bot hfr $ univ_mem_sets' $ assume i,
show 0 ≤ (finset.range i).sum f, from finset.sum_nonneg $ assume i _, hf i,
let f' (n : ℕ) : nnreal := ⟨f n, hf n⟩, r' : nnreal := ⟨r, this⟩ in
have f_eq : f = (λi:ℕ, (f' i : ℝ)) := rfl,
have r_eq : r = r' := rfl,
begin
rw [f_eq, r_eq, nnreal.has_sum_coe, nnreal.has_sum_iff_tendsto_nat, ← nnreal.tendsto_coe],
simp only [nnreal.coe_sum],
exact hfr
end⟩
lemma infi_real_pos_eq_infi_nnreal_pos {α : Type*} [complete_lattice α] {f : ℝ → α} :
(⨅(n:ℝ) (h : n > 0), f n) = (⨅(n:nnreal) (h : n > 0), f n) :=
le_antisymm
(le_infi $ assume n, le_infi $ assume hn, infi_le_of_le n $ infi_le _ (nnreal.coe_pos.2 hn))
(le_infi $ assume r, le_infi $ assume hr, infi_le_of_le ⟨r, le_of_lt hr⟩ $ infi_le _ hr)
section
variables [emetric_space β]
open ennreal filter emetric
/-- In an emetric ball, the distance between points is everywhere finite -/
lemma edist_ne_top_of_mem_ball {a : β} {r : ennreal} (x y : ball a r) : edist x.1 y.1 ≠ ⊤ :=
lt_top_iff_ne_top.1 $
calc edist x y ≤ edist a x + edist a y : edist_triangle_left x.1 y.1 a
... < r + r : by rw [edist_comm a x, edist_comm a y]; exact add_lt_add x.2 y.2
... ≤ ⊤ : le_top
/-- Each ball in an extended metric space gives us a metric space, as the edist
is everywhere finite. -/
def metric_space_emetric_ball (a : β) (r : ennreal) : metric_space (ball a r) :=
emetric_space.to_metric_space edist_ne_top_of_mem_ball
local attribute [instance] metric_space_emetric_ball
lemma nhds_eq_nhds_emetric_ball (a x : β) (r : ennreal) (h : x ∈ ball a r) :
𝓝 x = map (coe : ball a r → β) (𝓝 ⟨x, h⟩) :=
(map_nhds_subtype_val_eq _ $ mem_nhds_sets emetric.is_open_ball h).symm
end
section
variable [emetric_space α]
open emetric
lemma tendsto_iff_edist_tendsto_0 {l : filter β} {f : β → α} {y : α} :
tendsto f l (𝓝 y) ↔ tendsto (λ x, edist (f x) y) l (𝓝 0) :=
by simp only [emetric.nhds_basis_eball.tendsto_right_iff, emetric.mem_ball,
@tendsto_order ennreal β _ _, forall_prop_of_false ennreal.not_lt_zero, forall_const, true_and]
/-- Yet another metric characterization of Cauchy sequences on integers. This one is often the
most efficient. -/
lemma emetric.cauchy_seq_iff_le_tendsto_0 [nonempty β] [semilattice_sup β] {s : β → α} :
cauchy_seq s ↔ (∃ (b: β → ennreal), (∀ n m N : β, N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N)
∧ (tendsto b at_top (𝓝 0))) :=
⟨begin
assume hs,
rw emetric.cauchy_seq_iff at hs,
/- `s` is Cauchy sequence. The sequence `b` will be constructed by taking
the supremum of the distances between `s n` and `s m` for `n m ≥ N`-/
let b := λN, Sup ((λ(p : β × β), edist (s p.1) (s p.2))''{p | p.1 ≥ N ∧ p.2 ≥ N}),
--Prove that it bounds the distances of points in the Cauchy sequence
have C : ∀ n m N, N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N,
{ refine λm n N hm hn, le_Sup _,
use (prod.mk m n),
simp only [and_true, eq_self_iff_true, set.mem_set_of_eq],
exact ⟨hm, hn⟩ },
--Prove that it tends to `0`, by using the Cauchy property of `s`
have D : tendsto b at_top (𝓝 0),
{ refine tendsto_order.2 ⟨λa ha, absurd ha (ennreal.not_lt_zero), λε εpos, _⟩,
rcases dense εpos with ⟨δ, δpos, δlt⟩,
rcases hs δ δpos with ⟨N, hN⟩,
refine filter.mem_at_top_sets.2 ⟨N, λn hn, _⟩,
have : b n ≤ δ := Sup_le begin
simp only [and_imp, set.mem_image, set.mem_set_of_eq, exists_imp_distrib, prod.exists],
intros d p q hp hq hd,
rw ← hd,
exact le_of_lt (hN p q (le_trans hn hp) (le_trans hn hq))
end,
simpa using lt_of_le_of_lt this δlt },
-- Conclude
exact ⟨b, ⟨C, D⟩⟩
end,
begin
rintros ⟨b, ⟨b_bound, b_lim⟩⟩,
/-b : ℕ → ℝ, b_bound : ∀ (n m N : ℕ), N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N,
b_lim : tendsto b at_top (𝓝 0)-/
refine emetric.cauchy_seq_iff.2 (λε εpos, _),
have : ∀ᶠ n in at_top, b n < ε := (tendsto_order.1 b_lim ).2 _ εpos,
rcases filter.mem_at_top_sets.1 this with ⟨N, hN⟩,
exact ⟨N, λm n hm hn, calc
edist (s m) (s n) ≤ b N : b_bound m n N hm hn
... < ε : (hN _ (le_refl N)) ⟩
end⟩
lemma continuous_of_le_add_edist {f : α → ennreal} (C : ennreal)
(hC : C ≠ ⊤) (h : ∀x y, f x ≤ f y + C * edist x y) : continuous f :=
begin
refine continuous_iff_continuous_at.2 (λx, tendsto_order.2 ⟨_, _⟩),
show ∀e, e < f x → ∀ᶠ y in 𝓝 x, e < f y,
{ assume e he,
let ε := min (f x - e) 1,
have : ε < ⊤ := lt_of_le_of_lt (min_le_right _ _) (by simp [lt_top_iff_ne_top]),
have : 0 < ε := by simp [ε, hC, he, ennreal.zero_lt_one],
have : 0 < C⁻¹ * (ε/2) := bot_lt_iff_ne_bot.2 (by simp [hC, (ne_of_lt this).symm, ennreal.mul_eq_zero]),
have I : C * (C⁻¹ * (ε/2)) < ε,
{ by_cases C_zero : C = 0,
{ simp [C_zero, ‹0 < ε›] },
{ calc C * (C⁻¹ * (ε/2)) = (C * C⁻¹) * (ε/2) : by simp [mul_assoc]
... = ε/2 : by simp [ennreal.mul_inv_cancel C_zero hC]
... < ε : ennreal.half_lt_self (bot_lt_iff_ne_bot.1 ‹0 < ε›) (lt_top_iff_ne_top.1 ‹ε < ⊤›) }},
have : ball x (C⁻¹ * (ε/2)) ⊆ {y : α | e < f y},
{ rintros y hy,
by_cases htop : f y = ⊤,
{ simp [htop, lt_top_iff_ne_top, ne_top_of_lt he] },
{ simp at hy,
have : e + ε < f y + ε := calc
e + ε ≤ e + (f x - e) : add_le_add_left' (min_le_left _ _)
... = f x : by simp [le_of_lt he]
... ≤ f y + C * edist x y : h x y
... = f y + C * edist y x : by simp [edist_comm]
... ≤ f y + C * (C⁻¹ * (ε/2)) :
add_le_add_left' $ canonically_ordered_semiring.mul_le_mul (le_refl _) (le_of_lt hy)
... < f y + ε : (ennreal.add_lt_add_iff_left (lt_top_iff_ne_top.2 htop)).2 I,
show e < f y, from
(ennreal.add_lt_add_iff_right ‹ε < ⊤›).1 this }},
apply filter.mem_sets_of_superset (ball_mem_nhds _ (‹0 < C⁻¹ * (ε/2)›)) this },
show ∀e, f x < e → ∀ᶠ y in 𝓝 x, f y < e,
{ assume e he,
let ε := min (e - f x) 1,
have : ε < ⊤ := lt_of_le_of_lt (min_le_right _ _) (by simp [lt_top_iff_ne_top]),
have : 0 < ε := by simp [ε, he, ennreal.zero_lt_one],
have : 0 < C⁻¹ * (ε/2) := bot_lt_iff_ne_bot.2 (by simp [hC, (ne_of_lt this).symm, ennreal.mul_eq_zero]),
have I : C * (C⁻¹ * (ε/2)) < ε,
{ by_cases C_zero : C = 0,
simp [C_zero, ‹0 < ε›],
calc C * (C⁻¹ * (ε/2)) = (C * C⁻¹) * (ε/2) : by simp [mul_assoc]
... = ε/2 : by simp [ennreal.mul_inv_cancel C_zero hC]
... < ε : ennreal.half_lt_self (bot_lt_iff_ne_bot.1 ‹0 < ε›) (lt_top_iff_ne_top.1 ‹ε < ⊤›) },
have : ball x (C⁻¹ * (ε/2)) ⊆ {y : α | f y < e},
{ rintros y hy,
have htop : f x ≠ ⊤ := ne_top_of_lt he,
show f y < e, from calc
f y ≤ f x + C * edist y x : h y x
... ≤ f x + C * (C⁻¹ * (ε/2)) :
add_le_add_left' $ canonically_ordered_semiring.mul_le_mul (le_refl _) (le_of_lt hy)
... < f x + ε : (ennreal.add_lt_add_iff_left (lt_top_iff_ne_top.2 htop)).2 I
... ≤ f x + (e - f x) : add_le_add_left' (min_le_left _ _)
... = e : by simp [le_of_lt he] },
apply filter.mem_sets_of_superset (ball_mem_nhds _ (‹0 < C⁻¹ * (ε/2)›)) this },
end
theorem continuous_edist : continuous (λp:α×α, edist p.1 p.2) :=
begin
apply continuous_of_le_add_edist 2 (by simp),
rintros ⟨x, y⟩ ⟨x', y'⟩,
calc edist x y ≤ edist x x' + edist x' y' + edist y' y : edist_triangle4 _ _ _ _
... = edist x' y' + (edist x x' + edist y y') : by simp [edist_comm]; cc
... ≤ edist x' y' + (edist (x, y) (x', y') + edist (x, y) (x', y')) :
add_le_add_left' (add_le_add' (by simp [edist, le_refl]) (by simp [edist, le_refl]))
... = edist x' y' + 2 * edist (x, y) (x', y') : by rw [← mul_two, mul_comm]
end
theorem continuous.edist [topological_space β] {f g : β → α}
(hf : continuous f) (hg : continuous g) : continuous (λb, edist (f b) (g b)) :=
continuous_edist.comp (hf.prod_mk hg)
theorem filter.tendsto.edist {f g : β → α} {x : filter β} {a b : α}
(hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) :
tendsto (λx, edist (f x) (g x)) x (𝓝 (edist a b)) :=
(continuous_edist.tendsto (a, b)).comp (hf.prod_mk_nhds hg)
lemma cauchy_seq_of_edist_le_of_tsum_ne_top {f : ℕ → α} (d : ℕ → ennreal)
(hf : ∀ n, edist (f n) (f n.succ) ≤ d n) (hd : tsum d ≠ ∞) :
cauchy_seq f :=
begin
lift d to (ℕ → nnreal) using (λ i, ennreal.ne_top_of_tsum_ne_top hd i),
rw ennreal.tsum_coe_ne_top_iff_summable at hd,
exact cauchy_seq_of_edist_le_of_summable d hf hd
end
/-- If `edist (f n) (f (n+1))` is bounded above by a function `d : ℕ → ennreal`,
then the distance from `f n` to the limit is bounded by `∑'_{k=n}^∞ d k`. -/
lemma edist_le_tsum_of_edist_le_of_tendsto {f : ℕ → α} (d : ℕ → ennreal)
(hf : ∀ n, edist (f n) (f n.succ) ≤ d n)
{a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) :
edist (f n) a ≤ ∑' m, d (n + m) :=
begin
refine le_of_tendsto at_top_ne_bot (tendsto_const_nhds.edist ha)
(mem_at_top_sets.2 ⟨n, λ m hnm, _⟩),
refine le_trans (edist_le_Ico_sum_of_edist_le hnm (λ k _ _, hf k)) _,
rw [finset.sum_Ico_eq_sum_range],
exact sum_le_tsum _ (λ _ _, zero_le _) ennreal.summable
end
/-- If `edist (f n) (f (n+1))` is bounded above by a function `d : ℕ → ennreal`,
then the distance from `f 0` to the limit is bounded by `∑'_{k=0}^∞ d k`. -/
lemma edist_le_tsum_of_edist_le_of_tendsto₀ {f : ℕ → α} (d : ℕ → ennreal)
(hf : ∀ n, edist (f n) (f n.succ) ≤ d n)
{a : α} (ha : tendsto f at_top (𝓝 a)) :
edist (f 0) a ≤ ∑' m, d m :=
by simpa using edist_le_tsum_of_edist_le_of_tendsto d hf ha 0
end --section
|
2c887a9e92bd66fda0f2e9c878196e4ff052b108 | 36938939954e91f23dec66a02728db08a7acfcf9 | /lean/src/prover_interface.lean | 6b9ae633e42c1407326f6aee8c48ae9062b5739e | [
"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 | 618 | lean | universes u v
--open smt2
-- A word 64
def word64 := { x : ℕ // x < 2^64 }
@[reducible]
def llvm_instruction_index := ℕ
@[reducible]
def mc_instruction_addr := word64
-- The main interface to the verification condition generator.
class is_vcg (m : Type u → Type v) :=
(start_llvm_instruction : llvm_instruction_index → m punit)
(start_mc_instruction : mc_instruction_addr → m punit)
--(check_sat_assuming : string → list (term bool) → m punit)
(fail : string → m punit)
-- Use cases:
-- 1. Generate SMTLIB files for each proof.
-- 2. Call SMT solver
-- 3. Write assertion about what we proved.
|
3c4d14ce5f942e173a79126efdd5d207e17aa0dd | 08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4 | /src/Lean/Meta/ExprDefEq.lean | 06429ebf4843420dcbc613bfc4ec988b34d632d0 | [
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"Apache-2.0",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | gebner/lean4 | d51c4922640a52a6f7426536ea669ef18a1d9af5 | 8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f | refs/heads/master | 1,685,732,780,391 | 1,672,962,627,000 | 1,673,459,398,000 | 373,307,283 | 0 | 0 | Apache-2.0 | 1,691,316,730,000 | 1,622,669,271,000 | Lean | UTF-8 | Lean | false | false | 80,543 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Offset
import Lean.Meta.UnificationHint
import Lean.Util.OccursCheck
namespace Lean.Meta
/--
Return true if `b` is of the form `mk a.1 ... a.n`, and `a` is not a constructor application.
If `a` and `b` are constructor applications, the method returns `false` to force `isDefEq` to use `isDefEqArgs`.
For example, suppose we are trying to solve the constraint
```
Fin.mk ?n ?h =?= Fin.mk n h
```
If this method is applied, the constraints are reduced to
```
n =?= (Fin.mk ?n ?h).1
h =?= (Fin.mk ?n ?h).2
```
The first constraint produces the assignment `?n := n`. Then, the second constraint is solved using proof irrelevance without
assigning `?h`.
TODO: investigate better solutions for the proof irrelevance issue. The problem above can happen is other scenarios.
That is, proof irrelevance may prevent us from performing desired mvar assignments.
-/
private def isDefEqEtaStruct (a b : Expr) : MetaM Bool := do
matchConstCtor b.getAppFn (fun _ => return false) fun ctorVal us => do
if (← useEtaStruct ctorVal.induct) then
matchConstCtor a.getAppFn (fun _ => go ctorVal us) fun _ _ => return false
else
return false
where
go ctorVal us := do
if ctorVal.numParams + ctorVal.numFields != b.getAppNumArgs then
trace[Meta.isDefEq.eta.struct] "failed, insufficient number of arguments at{indentExpr b}"
return false
else
if !isStructureLike (← getEnv) ctorVal.induct then
trace[Meta.isDefEq.eta.struct] "failed, type is not a structure{indentExpr b}"
return false
else if (← isDefEq (← inferType a) (← inferType b)) then
checkpointDefEq do
let args := b.getAppArgs
let params := args[:ctorVal.numParams].toArray
for i in [ctorVal.numParams : args.size] do
let j := i - ctorVal.numParams
let proj ← mkProjFn ctorVal us params j a
trace[Meta.isDefEq.eta.struct] "{a} =?= {b} @ [{j}], {proj} =?= {args[i]!}"
unless (← isDefEq proj args[i]!) do
trace[Meta.isDefEq.eta.struct] "failed, unexpect arg #{i}, projection{indentExpr proj}\nis not defeq to{indentExpr args[i]!}"
return false
return true
else
return false
/--
Try to solve `a := (fun x => t) =?= b` by eta-expanding `b`,
resulting in `t =?= b x` (with a fresh free variable `x`).
Remark: eta-reduction is not a good alternative even in a system without universe cumulativity like Lean.
Example:
```
(fun x : A => f ?m) =?= f
```
The left-hand side of the constraint above it not eta-reduced because `?m` is a metavariable.
Note: we do not backtrack after applying η-expansion anymore.
There is no case where `(fun x => t) =?= b` unifies, but `t =?= b x` does not.
Backtracking after η-expansion results in lots of duplicate δ-reductions,
because we can δ-reduce `a` before and after the η-expansion.
The fresh free variable `x` also busts the cache.
See https://github.com/leanprover/lean4/pull/2002 -/
private def isDefEqEta (a b : Expr) : MetaM LBool := do
if a.isLambda && !b.isLambda then
let bType ← inferType b
let bType ← whnfD bType
match bType with
| Expr.forallE n d _ c =>
let b' := mkLambda n c d (mkApp b (mkBVar 0))
toLBoolM <| checkpointDefEq <| Meta.isExprDefEqAux a b'
| _ => return .undef
else
return .undef
/-- Support for `Lean.reduceBool` and `Lean.reduceNat` -/
def isDefEqNative (s t : Expr) : MetaM LBool := do
let isDefEq (s t) : MetaM LBool := toLBoolM <| Meta.isExprDefEqAux s t
let s? ← reduceNative? s
let t? ← reduceNative? t
match s?, t? with
| some s, some t => isDefEq s t
| some s, none => isDefEq s t
| none, some t => isDefEq s t
| none, none => pure LBool.undef
/-- Support for reducing Nat basic operations. -/
def isDefEqNat (s t : Expr) : MetaM LBool := do
let isDefEq (s t) : MetaM LBool := toLBoolM <| Meta.isExprDefEqAux s t
if s.hasFVar || s.hasMVar || t.hasFVar || t.hasMVar then
pure LBool.undef
else
let s? ← reduceNat? s
let t? ← reduceNat? t
match s?, t? with
| some s, some t => isDefEq s t
| some s, none => isDefEq s t
| none, some t => isDefEq s t
| none, none => pure LBool.undef
/-- Support for constraints of the form `("..." =?= String.mk cs)` -/
def isDefEqStringLit (s t : Expr) : MetaM LBool := do
let isDefEq (s t) : MetaM LBool := toLBoolM <| Meta.isExprDefEqAux s t
if s.isStringLit && t.isAppOf ``String.mk then
isDefEq s.toCtorIfLit t
else if s.isAppOf `String.mk && t.isStringLit then
isDefEq s t.toCtorIfLit
else
pure LBool.undef
/--
Return `true` if `e` is of the form `fun (x_1 ... x_n) => ?m x_1 ... x_n)`, and `?m` is unassigned.
Remark: `n` may be 0. -/
def isEtaUnassignedMVar (e : Expr) : MetaM Bool := do
match e.etaExpanded? with
| some (Expr.mvar mvarId) =>
if (← mvarId.isReadOnlyOrSyntheticOpaque) then
pure false
else if (← mvarId.isAssigned) then
pure false
else
pure true
| _ => pure false
private def trySynthPending (e : Expr) : MetaM Bool := do
let mvarId? ← getStuckMVar? e
match mvarId? with
| some mvarId => Meta.synthPending mvarId
| none => pure false
/--
Result type for `isDefEqArgsFirstPass`.
-/
inductive DefEqArgsFirstPassResult where
/--
Failed to establish that explicit arguments are def-eq.
Remark: higher output parameters, and parameters that depend on them
are postponed.
-/
| failed
/--
Succeeded. The array `postponedImplicit` contains the position
of the implicit arguments for which def-eq has been postponed.
`postponedHO` contains the higher order output parameters, and parameters
that depend on them. They should be processed after the implict ones.
`postponedHO` is used to handle applications involving functions that
contain higher order output parameters. Example:
```lean
getElem :
{cont : Type u_1} → {idx : Type u_2} → {elem : Type u_3} →
{dom : cont → idx → Prop} → [self : GetElem cont idx elem dom] →
(xs : cont) → (i : idx) → (h : dom xs i) → elem
```
The argumengs `dom` and `h` must be processed after all implicit arguments
otherwise higher-order unification problems are generated. See issue #1299,
when trying to solve
```
getElem ?a ?i ?h =?= getElem a i (Fin.val_lt_of_le i ...)
```
we have to solve the constraint
```
?dom a i.val =?= LT.lt i.val (Array.size a)
```
by solving after the instance has been synthesized, we reduce this constraint to
a simple check.
-/
| ok (postponedImplicit : Array Nat) (postponedHO : Array Nat)
/--
First pass for `isDefEqArgs`. We unify explicit arguments, *and* easy cases
Here, we say a case is easy if it is of the form
?m =?= t
or
t =?= ?m
where `?m` is unassigned.
These easy cases are not just an optimization. When
`?m` is a function, by assigning it to t, we make sure
a unification constraint (in the explicit part)
```
?m t =?= f s
```
is not higher-order.
We also handle the eta-expanded cases:
```
fun x₁ ... xₙ => ?m x₁ ... xₙ =?= t
t =?= fun x₁ ... xₙ => ?m x₁ ... xₙ
```
This is important because type inference often produces
eta-expanded terms, and without this extra case, we could
introduce counter intuitive behavior.
Pre: `paramInfo.size <= args₁.size = args₂.size`
See `DefEqArgsFirstPassResult` for additional information.
-/
private def isDefEqArgsFirstPass
(paramInfo : Array ParamInfo) (args₁ args₂ : Array Expr) : MetaM DefEqArgsFirstPassResult := do
let mut postponedImplicit := #[]
let mut postponedHO := #[]
for i in [:paramInfo.size] do
let info := paramInfo[i]!
let a₁ := args₁[i]!
let a₂ := args₂[i]!
if info.dependsOnHigherOrderOutParam || info.higherOrderOutParam then
trace[Meta.isDefEq] "found messy {a₁} =?= {a₂}"
postponedHO := postponedHO.push i
else if info.isExplicit then
unless (← Meta.isExprDefEqAux a₁ a₂) do
return .failed
else if (← isEtaUnassignedMVar a₁ <||> isEtaUnassignedMVar a₂) then
unless (← Meta.isExprDefEqAux a₁ a₂) do
return .failed
else
postponedImplicit := postponedImplicit.push i
return .ok postponedImplicit postponedHO
private partial def isDefEqArgs (f : Expr) (args₁ args₂ : Array Expr) : MetaM Bool := do
unless args₁.size == args₂.size do return false
let finfo ← getFunInfoNArgs f args₁.size
let .ok postponedImplicit postponedHO ← isDefEqArgsFirstPass finfo.paramInfo args₁ args₂ | pure false
-- finfo.paramInfo.size may be smaller than args₁.size
for i in [finfo.paramInfo.size:args₁.size] do
unless (← Meta.isExprDefEqAux args₁[i]! args₂[i]!) do
return false
for i in postponedImplicit do
/- Second pass: unify implicit arguments.
In the second pass, we make sure we are unfolding at
least non reducible definitions (default setting). -/
let a₁ := args₁[i]!
let a₂ := args₂[i]!
let info := finfo.paramInfo[i]!
if info.isInstImplicit then
discard <| trySynthPending a₁
discard <| trySynthPending a₂
unless (← withAtLeastTransparency TransparencyMode.default <| Meta.isExprDefEqAux a₁ a₂) do
return false
for i in postponedHO do
let a₁ := args₁[i]!
let a₂ := args₂[i]!
let info := finfo.paramInfo[i]!
if info.isInstImplicit then
unless (← withAtLeastTransparency TransparencyMode.default <| Meta.isExprDefEqAux a₁ a₂) do
return false
else
unless (← Meta.isExprDefEqAux a₁ a₂) do
return false
return true
/--
Check whether the types of the free variables at `fvars` are
definitionally equal to the types at `ds₂`.
Pre: `fvars.size == ds₂.size`
This method also updates the set of local instances, and invokes
the continuation `k` with the updated set.
We can't use `withNewLocalInstances` because the `isDeq fvarType d₂`
may use local instances. -/
@[specialize] partial def isDefEqBindingDomain (fvars : Array Expr) (ds₂ : Array Expr) (k : MetaM Bool) : MetaM Bool :=
let rec loop (i : Nat) := do
if h : i < fvars.size then do
let fvar := fvars.get ⟨i, h⟩
let fvarDecl ← getFVarLocalDecl fvar
let fvarType := fvarDecl.type
let d₂ := ds₂[i]!
if (← Meta.isExprDefEqAux fvarType d₂) then
match (← isClass? fvarType) with
| some className => withNewLocalInstance className fvar <| loop (i+1)
| none => loop (i+1)
else
pure false
else
k
loop 0
/-- Auxiliary function for `isDefEqBinding` for handling binders `forall/fun`.
It accumulates the new free variables in `fvars`, and declare them at `lctx`.
We use the domain types of `e₁` to create the new free variables.
We store the domain types of `e₂` at `ds₂`. -/
private partial def isDefEqBindingAux (lctx : LocalContext) (fvars : Array Expr) (e₁ e₂ : Expr) (ds₂ : Array Expr) : MetaM Bool :=
let process (n : Name) (d₁ d₂ b₁ b₂ : Expr) : MetaM Bool := do
let d₁ := d₁.instantiateRev fvars
let d₂ := d₂.instantiateRev fvars
let fvarId ← mkFreshFVarId
let lctx := lctx.mkLocalDecl fvarId n d₁
let fvars := fvars.push (mkFVar fvarId)
isDefEqBindingAux lctx fvars b₁ b₂ (ds₂.push d₂)
match e₁, e₂ with
| Expr.forallE n d₁ b₁ _, Expr.forallE _ d₂ b₂ _ => process n d₁ d₂ b₁ b₂
| Expr.lam n d₁ b₁ _, Expr.lam _ d₂ b₂ _ => process n d₁ d₂ b₁ b₂
| _, _ =>
withReader (fun ctx => { ctx with lctx := lctx }) do
isDefEqBindingDomain fvars ds₂ do
Meta.isExprDefEqAux (e₁.instantiateRev fvars) (e₂.instantiateRev fvars)
@[inline] private def isDefEqBinding (a b : Expr) : MetaM Bool := do
let lctx ← getLCtx
isDefEqBindingAux lctx #[] a b #[]
private def checkTypesAndAssign (mvar : Expr) (v : Expr) : MetaM Bool :=
withTraceNodeBefore `Meta.isDefEq.assign.checkTypes (return m!"({mvar} : {← inferType mvar}) := ({v} : {← inferType v})") do
if !mvar.isMVar then
trace[Meta.isDefEq.assign.checkTypes] "metavariable expected"
return false
else
-- must check whether types are definitionally equal or not, before assigning and returning true
let mvarType ← inferType mvar
let vType ← inferType v
if (← withTransparency TransparencyMode.default <| Meta.isExprDefEqAux mvarType vType) then
mvar.mvarId!.assign v
pure true
else
pure false
/--
Auxiliary method for solving constraints of the form `?m xs := v`.
It creates a lambda using `mkLambdaFVars ys v`, where `ys` is a superset of `xs`.
`ys` is often equal to `xs`. It is a bigger when there are let-declaration dependencies in `xs`.
For example, suppose we have `xs` of the form `#[a, c]` where
```
a : Nat
b : Nat := f a
c : b = a
```
In this scenario, the type of `?m` is `(x1 : Nat) -> (x2 : f x1 = x1) -> C[x1, x2]`,
and type of `v` is `C[a, c]`. Note that, `?m a c` is type correct since `f a = a` is definitionally equal
to the type of `c : b = a`, and the type of `?m a c` is equal to the type of `v`.
Note that `fun xs => v` is the term `fun (x1 : Nat) (x2 : b = x1) => v` which has type
`(x1 : Nat) -> (x2 : b = x1) -> C[x1, x2]` which is not definitionally equal to the type of `?m`,
and may not even be type correct.
The issue here is that we are not capturing the `let`-declarations.
This method collects let-declarations `y` occurring between `xs[0]` and `xs.back` s.t.
some `x` in `xs` depends on `y`.
`ys` is the `xs` with these extra let-declarations included.
In the example above, `ys` is `#[a, b, c]`, and `mkLambdaFVars ys v` produces
`fun a => let b := f a; fun (c : b = a) => v` which has a type definitionally equal to the type of `?m`.
Recall that the method `checkAssignment` ensures `v` does not contain offending `let`-declarations.
This method assumes that for any `xs[i]` and `xs[j]` where `i < j`, we have that `index of xs[i]` < `index of xs[j]`.
where the index is the position in the local context.
-/
private partial def mkLambdaFVarsWithLetDeps (xs : Array Expr) (v : Expr) : MetaM (Option Expr) := do
if not (← hasLetDeclsInBetween) then
mkLambdaFVars xs v
else
let ys ← addLetDeps
mkLambdaFVars ys v
where
/-- Return true if there are let-declarions between `xs[0]` and `xs[xs.size-1]`.
We use it a quick-check to avoid the more expensive collection procedure. -/
hasLetDeclsInBetween : MetaM Bool := do
let check (lctx : LocalContext) : Bool := Id.run do
let start := lctx.getFVar! xs[0]! |>.index
let stop := lctx.getFVar! xs.back |>.index
for i in [start+1:stop] do
match lctx.getAt? i with
| some localDecl =>
if localDecl.isLet then
return true
| _ => pure ()
return false
if xs.size <= 1 then
return false
else
return check (← getLCtx)
/-- Traverse `e` and stores in the state `NameHashSet` any let-declaration with index greater than `(← read)`.
The context `Nat` is the position of `xs[0]` in the local context. -/
collectLetDeclsFrom (e : Expr) : ReaderT Nat (StateRefT FVarIdHashSet MetaM) Unit := do
let rec visit (e : Expr) : MonadCacheT Expr Unit (ReaderT Nat (StateRefT FVarIdHashSet MetaM)) Unit :=
checkCache e fun _ => do
match e with
| Expr.forallE _ d b _ => visit d; visit b
| Expr.lam _ d b _ => visit d; visit b
| Expr.letE _ t v b _ => visit t; visit v; visit b
| Expr.app f a => visit f; visit a
| Expr.mdata _ b => visit b
| Expr.proj _ _ b => visit b
| Expr.fvar fvarId =>
let localDecl ← fvarId.getDecl
if localDecl.isLet && localDecl.index > (← read) then
modify fun s => s.insert localDecl.fvarId
| _ => pure ()
visit (← instantiateMVars e) |>.run
/--
Auxiliary definition for traversing all declarations between `xs[0]` ... `xs.back` backwards.
The `Nat` argument is the current position in the local context being visited, and it is less than
or equal to the position of `xs.back` in the local context.
The `Nat` context `(← read)` is the position of `xs[0]` in the local context.
-/
collectLetDepsAux : Nat → ReaderT Nat (StateRefT FVarIdHashSet MetaM) Unit
| 0 => return ()
| i+1 => do
if i+1 == (← read) then
return ()
else
match (← getLCtx).getAt? (i+1) with
| none => collectLetDepsAux i
| some localDecl =>
if (← get).contains localDecl.fvarId then
collectLetDeclsFrom localDecl.type
match localDecl.value? with
| some val => collectLetDeclsFrom val
| _ => pure ()
collectLetDepsAux i
/-- Computes the set `ys`. It is a set of `FVarId`s, -/
collectLetDeps : MetaM FVarIdHashSet := do
let lctx ← getLCtx
let start := lctx.getFVar! xs[0]! |>.index
let stop := lctx.getFVar! xs.back |>.index
let s := xs.foldl (init := {}) fun s x => s.insert x.fvarId!
let (_, s) ← collectLetDepsAux stop |>.run start |>.run s
return s
/-- Computes the array `ys` containing let-decls between `xs[0]` and `xs.back` that
some `x` in `xs` depends on. -/
addLetDeps : MetaM (Array Expr) := do
let lctx ← getLCtx
let s ← collectLetDeps
/- Convert `s` into the array `ys` -/
let start := lctx.getFVar! xs[0]! |>.index
let stop := lctx.getFVar! xs.back |>.index
let mut ys := #[]
for i in [start:stop+1] do
match lctx.getAt? i with
| none => pure ()
| some localDecl =>
if s.contains localDecl.fvarId then
ys := ys.push localDecl.toExpr
return ys
/-!
Each metavariable is declared in a particular local context.
We use the notation `C |- ?m : t` to denote a metavariable `?m` that
was declared at the local context `C` with type `t` (see `MetavarDecl`).
We also use `?m@C` as a shorthand for `C |- ?m : t` where `t` is the type of `?m`.
The following method process the unification constraint
?m@C a₁ ... aₙ =?= t
We say the unification constraint is a pattern IFF
1) `a₁ ... aₙ` are pairwise distinct free variables that are *not* let-variables.
2) `a₁ ... aₙ` are not in `C`
3) `t` only contains free variables in `C` and/or `{a₁, ..., aₙ}`
4) For every metavariable `?m'@C'` occurring in `t`, `C'` is a subprefix of `C`
5) `?m` does not occur in `t`
Claim: we don't have to check free variable declarations. That is,
if `t` contains a reference to `x : A := v`, we don't need to check `v`.
Reason: The reference to `x` is a free variable, and it must be in `C` (by 1 and 3).
If `x` is in `C`, then any metavariable occurring in `v` must have been defined in a strict subprefix of `C`.
So, condition 4 and 5 are satisfied.
If the conditions above have been satisfied, then the
solution for the unification constrain is
?m := fun a₁ ... aₙ => t
Now, we consider some workarounds/approximations.
A1) Suppose `t` contains a reference to `x : A := v` and `x` is not in `C` (failed condition 3)
(precise) solution: unfold `x` in `t`.
A2) Suppose some `aᵢ` is in `C` (failed condition 2)
(approximated) solution (when `config.quasiPatternApprox` is set to true) :
ignore condition and also use
?m := fun a₁ ... aₙ => t
Here is an example where this approximation fails:
Given `C` containing `a : nat`, consider the following two constraints
?m@C a =?= a
?m@C b =?= a
If we use the approximation in the first constraint, we get
?m := fun x => x
when we apply this solution to the second one we get a failure.
IMPORTANT: When applying this approximation we need to make sure the
abstracted term `fun a₁ ... aₙ => t` is type correct. The check
can only be skipped in the pattern case described above. Consider
the following example. Given the local context
(α : Type) (a : α)
we try to solve
?m α =?= @id α a
If we use the approximation above we obtain:
?m := (fun α' => @id α' a)
which is a type incorrect term. `a` has type `α` but it is expected to have
type `α'`.
The problem occurs because the right hand side contains a free variable
`a` that depends on the free variable `α` being abstracted. Note that
this dependency cannot occur in patterns.
We can address this by type checking
the term after abstraction. This is not a significant performance
bottleneck because this case doesn't happen very often in practice
(262 times when compiling stdlib on Jan 2018). The second example
is trickier, but it also occurs less frequently (8 times when compiling
stdlib on Jan 2018, and all occurrences were at Init/Control when
we define monads and auxiliary combinators for them).
We considered three options for the addressing the issue on the second example:
A3) `a₁ ... aₙ` are not pairwise distinct (failed condition 1).
In Lean3, we would try to approximate this case using an approach similar to A2.
However, this approximation complicates the code, and is never used in the
Lean3 stdlib and mathlib.
A4) `t` contains a metavariable `?m'@C'` where `C'` is not a subprefix of `C`.
If `?m'` is assigned, we substitute.
If not, we create an auxiliary metavariable with a smaller scope.
Actually, we let `elimMVarDeps` at `MetavarContext.lean` to perform this step.
A5) If some `aᵢ` is not a free variable,
then we use first-order unification (if `config.foApprox` is set to true)
?m a_1 ... a_i a_{i+1} ... a_{i+k} =?= f b_1 ... b_k
reduces to
?M a_1 ... a_i =?= f
a_{i+1} =?= b_1
...
a_{i+k} =?= b_k
A6) If (m =?= v) is of the form
?m a_1 ... a_n =?= ?m b_1 ... b_k
then we use first-order unification (if `config.foApprox` is set to true)
A7) When `foApprox`, we may use another approximation (`constApprox`) for solving constraints of the form
```
?m s₁ ... sₙ =?= t
```
where `s₁ ... sₙ` are arbitrary terms. We solve them by assigning the constant function to `?m`.
```
?m := fun _ ... _ => t
```
In general, this approximation may produce bad solutions, and may prevent coercions from being tried.
For example, consider the term `pure (x > 0)` with inferred type `?m Prop` and expected type `IO Bool`.
In this situation, the
elaborator generates the unification constraint
```
?m Prop =?= IO Bool
```
It is not a higher-order pattern, nor first-order approximation is applicable. However, constant approximation
produces the bogus solution `?m := fun _ => IO Bool`, and prevents the system from using the coercion from
the decidable proposition `x > 0` to `Bool`.
On the other hand, the constant approximation is desirable for elaborating the term
```
let f (x : _) := pure "hello"; f ()
```
with expected type `IO String`.
In this example, the following unification contraint is generated.
```
?m () String =?= IO String
```
It is not a higher-order pattern, first-order approximation reduces it to
```
?m () =?= IO
```
which fails to be solved. However, constant approximation solves it by assigning
```
?m := fun _ => IO
```
Note that `f`s type is `(x : ?α) -> ?m x String`. The metavariable `?m` may depend on `x`.
If `constApprox` is set to true, we use constant approximation. Otherwise, we use a heuristic to decide
whether we should apply it or not. The heuristic is based on observing where the constraints above come from.
In the first example, the constraint `?m Prop =?= IO Bool` come from polymorphic method where `?m` is expected to
be a **function** of type `Type -> Type`. In the second example, the first argument of `?m` is used to model
a **potential** dependency on `x`. By using constant approximation here, we are just saying the type of `f`
does **not** depend on `x`. We claim this is a reasonable approximation in practice. Moreover, it is expected
by any functional programmer used to non-dependently type languages (e.g., Haskell).
We distinguish the two cases above by using the field `numScopeArgs` at `MetavarDecl`. This fiels tracks
how many metavariable arguments are representing dependencies.
-/
def mkAuxMVar (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (numScopeArgs : Nat := 0) : MetaM Expr := do
mkFreshExprMVarAt lctx localInsts type MetavarKind.natural Name.anonymous numScopeArgs
namespace CheckAssignment
builtin_initialize checkAssignmentExceptionId : InternalExceptionId ← registerInternalExceptionId `checkAssignment
builtin_initialize outOfScopeExceptionId : InternalExceptionId ← registerInternalExceptionId `outOfScope
structure State where
cache : ExprStructMap Expr := {}
structure Context where
mvarId : MVarId
mvarDecl : MetavarDecl
fvars : Array Expr
hasCtxLocals : Bool
rhs : Expr
abbrev CheckAssignmentM := ReaderT Context $ StateRefT State MetaM
def throwCheckAssignmentFailure : CheckAssignmentM α :=
throw <| Exception.internal checkAssignmentExceptionId
def throwOutOfScopeFVar : CheckAssignmentM α :=
throw <| Exception.internal outOfScopeExceptionId
private def findCached? (e : Expr) : CheckAssignmentM (Option Expr) := do
return (← get).cache.find? e
private def cache (e r : Expr) : CheckAssignmentM Unit := do
modify fun s => { s with cache := s.cache.insert e r }
instance : MonadCache Expr Expr CheckAssignmentM where
findCached? := findCached?
cache := cache
private def addAssignmentInfo (msg : MessageData) : CheckAssignmentM MessageData := do
let ctx ← read
return m!"{msg} @ {mkMVar ctx.mvarId} {ctx.fvars} := {ctx.rhs}"
@[inline] def run (x : CheckAssignmentM Expr) (mvarId : MVarId) (fvars : Array Expr) (hasCtxLocals : Bool) (v : Expr) : MetaM (Option Expr) := do
let mvarDecl ← mvarId.getDecl
let ctx := { mvarId := mvarId, mvarDecl := mvarDecl, fvars := fvars, hasCtxLocals := hasCtxLocals, rhs := v : Context }
let x : CheckAssignmentM (Option Expr) :=
catchInternalIds [outOfScopeExceptionId, checkAssignmentExceptionId]
(do let e ← x; return some e)
(fun _ => pure none)
x.run ctx |>.run' {}
mutual
partial def checkFVar (fvar : Expr) : CheckAssignmentM Expr := do
let ctxMeta ← readThe Meta.Context
let ctx ← read
if ctx.mvarDecl.lctx.containsFVar fvar then
pure fvar
else
let lctx := ctxMeta.lctx
match lctx.findFVar? fvar with
| some (.ldecl (value := v) ..) => check v
| _ =>
if ctx.fvars.contains fvar then pure fvar
else
traceM `Meta.isDefEq.assign.outOfScopeFVar do addAssignmentInfo fvar
throwOutOfScopeFVar
partial def checkMVar (mvar : Expr) : CheckAssignmentM Expr := do
let mvarId := mvar.mvarId!
let ctx ← read
if mvarId == ctx.mvarId then
traceM `Meta.isDefEq.assign.occursCheck <| addAssignmentInfo "occurs check failed"
throwCheckAssignmentFailure
else match (← getExprMVarAssignment? mvarId) with
| some v => check v
| none =>
match (← mvarId.findDecl?) with
| none => throwUnknownMVar mvarId
| some mvarDecl =>
if ctx.hasCtxLocals then
throwCheckAssignmentFailure -- It is not a pattern, then we fail and fall back to FO unification
else if mvarDecl.lctx.isSubPrefixOf ctx.mvarDecl.lctx ctx.fvars then
/- The local context of `mvar` - free variables being abstracted is a subprefix of the metavariable being assigned.
We "substract" variables being abstracted because we use `elimMVarDeps` -/
pure mvar
else if mvarDecl.depth != (← getMCtx).depth || mvarDecl.kind.isSyntheticOpaque then
traceM `Meta.isDefEq.assign.readOnlyMVarWithBiggerLCtx <| addAssignmentInfo (mkMVar mvarId)
throwCheckAssignmentFailure
else
let ctxMeta ← readThe Meta.Context
if ctxMeta.config.ctxApprox && ctx.mvarDecl.lctx.isSubPrefixOf mvarDecl.lctx then
/- Create an auxiliary metavariable with a smaller context and "checked" type.
Note that `mvarType` may be different from `mvarDecl.type`. Example: `mvarType` contains
a metavariable that we also need to reduce the context.
We remove from `ctx.mvarDecl.lctx` any variable that is not in `mvarDecl.lctx`
or in `ctx.fvars`. We don't need to remove the ones in `ctx.fvars` because
`elimMVarDeps` will take care of them.
First, we collect `toErase` the variables that need to be erased.
Notat that if a variable is `ctx.fvars`, but it depends on variable at `toErase`,
we must also erase it.
-/
let toErase ← mvarDecl.lctx.foldlM (init := #[]) fun toErase localDecl => do
if ctx.mvarDecl.lctx.contains localDecl.fvarId then
return toErase
else if ctx.fvars.any fun fvar => fvar.fvarId! == localDecl.fvarId then
if (← findLocalDeclDependsOn localDecl fun fvarId => toErase.contains fvarId) then
-- localDecl depends on a variable that will be erased. So, we must add it to `toErase` too
return toErase.push localDecl.fvarId
else
return toErase
else
return toErase.push localDecl.fvarId
let lctx := toErase.foldl (init := mvarDecl.lctx) fun lctx toEraseFVar =>
lctx.erase toEraseFVar
/- Compute new set of local instances. -/
let localInsts := mvarDecl.localInstances.filter fun localInst => !toErase.contains localInst.fvar.fvarId!
let mvarType ← check mvarDecl.type
let newMVar ← mkAuxMVar lctx localInsts mvarType mvarDecl.numScopeArgs
mvarId.assign newMVar
pure newMVar
else
traceM `Meta.isDefEq.assign.readOnlyMVarWithBiggerLCtx <| addAssignmentInfo (mkMVar mvarId)
throwCheckAssignmentFailure
/--
Auxiliary function used to "fix" subterms of the form `?m x_1 ... x_n` where `x_i`s are free variables,
and one of them is out-of-scope.
See `Expr.app` case at `check`.
If `ctxApprox` is true, then we solve this case by creating a fresh metavariable ?n with the correct scope,
an assigning `?m := fun _ ... _ => ?n` -/
partial def assignToConstFun (mvar : Expr) (numArgs : Nat) (newMVar : Expr) : MetaM Bool := do
let mvarType ← inferType mvar
forallBoundedTelescope mvarType numArgs fun xs _ => do
if xs.size != numArgs then pure false
else
let some v ← mkLambdaFVarsWithLetDeps xs newMVar | return false
match (← checkAssignmentAux mvar.mvarId! #[] false v) with
| some v => checkTypesAndAssign mvar v
| none => return false
-- See checkAssignment
partial def checkAssignmentAux (mvarId : MVarId) (fvars : Array Expr) (hasCtxLocals : Bool) (v : Expr) : MetaM (Option Expr) := do
run (check v) mvarId fvars hasCtxLocals v
partial def checkApp (e : Expr) : CheckAssignmentM Expr :=
e.withApp fun f args => do
let ctxMeta ← readThe Meta.Context
if f.isMVar && ctxMeta.config.ctxApprox && args.all Expr.isFVar then
let f ← check f
catchInternalId outOfScopeExceptionId
(do
let args ← args.mapM check
return mkAppN f args)
(fun ex => do
if !f.isMVar then
throw ex
else if (← f.mvarId!.isDelayedAssigned) then
throw ex
else
let eType ← inferType e
let mvarType ← check eType
/- Create an auxiliary metavariable with a smaller context and "checked" type, assign `?f := fun _ => ?newMVar`
Note that `mvarType` may be different from `eType`. -/
let ctx ← read
let newMVar ← mkAuxMVar ctx.mvarDecl.lctx ctx.mvarDecl.localInstances mvarType
if (← assignToConstFun f args.size newMVar) then
pure newMVar
else
throw ex)
else
let f ← check f
let args ← args.mapM check
return mkAppN f args
partial def check (e : Expr) : CheckAssignmentM Expr := do
if !e.hasExprMVar && !e.hasFVar then
return e
else checkCache e fun _ =>
match e with
| Expr.mdata _ b => return e.updateMData! (← check b)
| Expr.proj _ _ s => return e.updateProj! (← check s)
| Expr.lam _ d b _ => return e.updateLambdaE! (← check d) (← check b)
| Expr.forallE _ d b _ => return e.updateForallE! (← check d) (← check b)
| Expr.letE _ t v b _ => return e.updateLet! (← check t) (← check v) (← check b)
| Expr.bvar .. => return e
| Expr.sort .. => return e
| Expr.const .. => return e
| Expr.lit .. => return e
| Expr.fvar .. => checkFVar e
| Expr.mvar .. => checkMVar e
| Expr.app .. =>
try
checkApp e
catch ex => match ex with
| .internal id =>
/-
If `ex` is an `CheckAssignmentM` internal exception and `e` is a beta-redex, we reduce `e` and try again.
This is useful for assignments such as `?m := (fun _ => A) a` where `a` is free variable that is not in
the scope of `?m`.
Note that, we do not try expensive reductions (e.g., `delta`). Thus, the following assignment
```lean
?m := Function.const 0 a
```
still fails because we do reduce the rhs to `0`. We assume this is not an issue in practice.
-/
if (id == outOfScopeExceptionId || id == checkAssignmentExceptionId) && e.isHeadBetaTarget then
checkApp e.headBeta
else
throw ex
| _ => throw ex
-- TODO: investigate whether the following feature is too expensive or not
/-
catchInternalIds [checkAssignmentExceptionId, outOfScopeExceptionId]
(checkApp e)
fun ex => do
let e' ← whnfR e
if e != e' then
check e'
else
throw ex
-/
end
end CheckAssignment
namespace CheckAssignmentQuick
partial def check
(hasCtxLocals : Bool)
(mctx : MetavarContext) (lctx : LocalContext) (mvarDecl : MetavarDecl) (mvarId : MVarId) (fvars : Array Expr) (e : Expr) : Bool :=
let rec visit (e : Expr) : Bool :=
if !e.hasExprMVar && !e.hasFVar then
true
else match e with
| Expr.mdata _ b => visit b
| Expr.proj _ _ s => visit s
| Expr.app f a => visit f && visit a
| Expr.lam _ d b _ => visit d && visit b
| Expr.forallE _ d b _ => visit d && visit b
| Expr.letE _ t v b _ => visit t && visit v && visit b
| Expr.bvar .. => true
| Expr.sort .. => true
| Expr.const .. => true
| Expr.lit .. => true
| Expr.fvar fvarId .. =>
if mvarDecl.lctx.contains fvarId then true
else match lctx.find? fvarId with
| some (LocalDecl.ldecl ..) => false -- need expensive CheckAssignment.check
| _ =>
if fvars.any fun x => x.fvarId! == fvarId then true
else false -- We could throw an exception here, but we would have to use ExceptM. So, we let CheckAssignment.check do it
| Expr.mvar mvarId' =>
match mctx.getExprAssignmentCore? mvarId' with
| some _ => false -- use CheckAssignment.check to instantiate
| none =>
if mvarId' == mvarId then false -- occurs check failed, use CheckAssignment.check to throw exception
else match mctx.findDecl? mvarId' with
| none => false
| some mvarDecl' =>
if hasCtxLocals then false -- use CheckAssignment.check
else if mvarDecl'.lctx.isSubPrefixOf mvarDecl.lctx fvars then true
else false -- use CheckAssignment.check
visit e
end CheckAssignmentQuick
/--
Auxiliary function for handling constraints of the form `?m a₁ ... aₙ =?= v`.
It will check whether we can perform the assignment
```
?m := fun fvars => v
```
The result is `none` if the assignment can't be performed.
The result is `some newV` where `newV` is a possibly updated `v`. This method may need
to unfold let-declarations. -/
def checkAssignment (mvarId : MVarId) (fvars : Array Expr) (v : Expr) : MetaM (Option Expr) := do
/- Check whether `mvarId` occurs in the type of `fvars` or not. If it does, return `none`
to prevent us from creating the cyclic assignment `?m := fun fvars => v` -/
for fvar in fvars do
unless (← occursCheck mvarId (← inferType fvar)) do
return none
if !v.hasExprMVar && !v.hasFVar then
pure (some v)
else
let mvarDecl ← mvarId.getDecl
let hasCtxLocals := fvars.any fun fvar => mvarDecl.lctx.containsFVar fvar
let ctx ← read
let mctx ← getMCtx
if CheckAssignmentQuick.check hasCtxLocals mctx ctx.lctx mvarDecl mvarId fvars v then
pure (some v)
else
let v ← instantiateMVars v
CheckAssignment.checkAssignmentAux mvarId fvars hasCtxLocals v
private def processAssignmentFOApproxAux (mvar : Expr) (args : Array Expr) (v : Expr) : MetaM Bool :=
match v with
| .mdata _ e => processAssignmentFOApproxAux mvar args e
| Expr.app f a =>
if args.isEmpty then
pure false
else
Meta.isExprDefEqAux args.back a <&&> Meta.isExprDefEqAux (mkAppRange mvar 0 (args.size - 1) args) f
| _ => pure false
/--
Auxiliary method for applying first-order unification. It is an approximation.
Remark: this method is trying to solve the unification constraint:
?m a₁ ... aₙ =?= v
It is uses processAssignmentFOApproxAux, if it fails, it tries to unfold `v`.
We have added support for unfolding here because we want to be able to solve unification problems such as
?m Unit =?= ITactic
where `ITactic` is defined as
def ITactic := Tactic Unit
-/
private partial def processAssignmentFOApprox (mvar : Expr) (args : Array Expr) (v : Expr) : MetaM Bool :=
let rec loop (v : Expr) := do
let cfg ← getConfig
if !cfg.foApprox then
pure false
else
trace[Meta.isDefEq.foApprox] "{mvar} {args} := {v}"
let v := v.headBeta
if (← checkpointDefEq <| processAssignmentFOApproxAux mvar args v) then
pure true
else
match (← unfoldDefinition? v) with
| none => pure false
| some v => loop v
loop v
private partial def simpAssignmentArgAux (e : Expr) : MetaM Expr := do
match e with
| .mdata _ e => simpAssignmentArgAux e
| .fvar fvarId =>
let some value ← fvarId.getValue? | return e
simpAssignmentArgAux value
| _ => return e
/-- Auxiliary procedure for processing `?m a₁ ... aₙ =?= v`.
We apply it to each `aᵢ`. It instantiates assigned metavariables if `aᵢ` is of the form `f[?n] b₁ ... bₘ`,
and then removes metadata, and zeta-expand let-decls. -/
private def simpAssignmentArg (arg : Expr) : MetaM Expr := do
let arg ← if arg.getAppFn.hasExprMVar then instantiateMVars arg else pure arg
simpAssignmentArgAux arg
/-- Assign `mvar := fun a_1 ... a_{numArgs} => v`.
We use it at `processConstApprox` and `isDefEqMVarSelf` -/
private def assignConst (mvar : Expr) (numArgs : Nat) (v : Expr) : MetaM Bool := do
let mvarDecl ← mvar.mvarId!.getDecl
forallBoundedTelescope mvarDecl.type numArgs fun xs _ => do
if xs.size != numArgs then
pure false
else
let some v ← mkLambdaFVarsWithLetDeps xs v | pure false
match (← checkAssignment mvar.mvarId! #[] v) with
| none => pure false
| some v =>
trace[Meta.isDefEq.constApprox] "{mvar} := {v}"
checkTypesAndAssign mvar v
/--
Auxiliary procedure for solving `?m args =?= v` when `args[:patternVarPrefix]` contains
only pairwise distinct free variables.
Let `args[:patternVarPrefix] = #[a₁, ..., aₙ]`, and `args[patternVarPrefix:] = #[b₁, ..., bᵢ]`,
this procedure first reduces the constraint to
```
?m a₁ ... aₙ =?= fun x₁ ... xᵢ => v
```
where the left-hand-side is a constant function.
Then, it tries to find the longest prefix `#[a₁, ..., aⱼ]` of `#[a₁, ..., aₙ]` such that the following assignment is valid.
```
?m := fun y₁ ... yⱼ => (fun y_{j+1} ... yₙ x₁ ... xᵢ => v)[a₁/y₁, .., aⱼ/yⱼ]
```
That is, after the longest prefix is found, we solve the contraint as the lhs was a pattern. See the definition of "pattern" above.
-/
private partial def processConstApprox (mvar : Expr) (args : Array Expr) (patternVarPrefix : Nat) (v : Expr) : MetaM Bool := do
trace[Meta.isDefEq.constApprox] "{mvar} {args} := {v}"
let rec defaultCase : MetaM Bool := assignConst mvar args.size v
let cfg ← getConfig
let mvarId := mvar.mvarId!
let mvarDecl ← mvarId.getDecl
let numArgs := args.size
if mvarDecl.numScopeArgs != numArgs && !cfg.constApprox then
return false
else if patternVarPrefix == 0 then
defaultCase
else
let argsPrefix : Array Expr := args[:patternVarPrefix]
let type ← instantiateForall mvarDecl.type argsPrefix
let suffixSize := numArgs - argsPrefix.size
forallBoundedTelescope type suffixSize fun xs _ => do
if xs.size != suffixSize then
defaultCase
else
let some v ← mkLambdaFVarsWithLetDeps xs v | defaultCase
let rec go (argsPrefix : Array Expr) (v : Expr) : MetaM Bool := do
trace[Meta.isDefEq] "processConstApprox.go {mvar} {argsPrefix} := {v}"
let rec cont : MetaM Bool := do
if argsPrefix.isEmpty then
defaultCase
else
let some v ← mkLambdaFVarsWithLetDeps #[argsPrefix.back] v | defaultCase
go argsPrefix.pop v
match (← checkAssignment mvarId argsPrefix v) with
| none => cont
| some vNew =>
let some vNew ← mkLambdaFVarsWithLetDeps argsPrefix vNew | cont
if argsPrefix.any (fun arg => mvarDecl.lctx.containsFVar arg) then
/- We need to type check `vNew` because abstraction using `mkLambdaFVars` may have produced
a type incorrect term. See discussion at A2 -/
(isTypeCorrect vNew <&&> checkTypesAndAssign mvar vNew) <||> cont
else
checkTypesAndAssign mvar vNew <||> cont
go argsPrefix v
/-- Tries to solve `?m a₁ ... aₙ =?= v` by assigning `?m`.
It assumes `?m` is unassigned. -/
private partial def processAssignment (mvarApp : Expr) (v : Expr) : MetaM Bool :=
withTraceNodeBefore `Meta.isDefEq.assign (return m!"{mvarApp} := {v}") do
let mvar := mvarApp.getAppFn
let mvarDecl ← mvar.mvarId!.getDecl
let rec process (i : Nat) (args : Array Expr) (v : Expr) := do
let cfg ← getConfig
let useFOApprox (args : Array Expr) : MetaM Bool :=
processAssignmentFOApprox mvar args v <||> processConstApprox mvar args i v
if h : i < args.size then
let arg := args.get ⟨i, h⟩
let arg ← simpAssignmentArg arg
let args := args.set ⟨i, h⟩ arg
match arg with
| Expr.fvar fvarId =>
if args[0:i].any fun prevArg => prevArg == arg then
useFOApprox args
else if mvarDecl.lctx.contains fvarId && !cfg.quasiPatternApprox then
useFOApprox args
else
process (i+1) args v
| _ =>
useFOApprox args
else
let v ← instantiateMVars v -- enforce A4
if v.getAppFn == mvar then
-- using A6
useFOApprox args
else
let mvarId := mvar.mvarId!
match (← checkAssignment mvarId args v) with
| none => useFOApprox args
| some v => do
trace[Meta.isDefEq.assign.beforeMkLambda] "{mvar} {args} := {v}"
let some v ← mkLambdaFVarsWithLetDeps args v | return false
if args.any (fun arg => mvarDecl.lctx.containsFVar arg) then
/- We need to type check `v` because abstraction using `mkLambdaFVars` may have produced
a type incorrect term. See discussion at A2 -/
if (← isTypeCorrect v) then
checkTypesAndAssign mvar v
else
trace[Meta.isDefEq.assign.typeError] "{mvar} := {v}"
useFOApprox args
else
checkTypesAndAssign mvar v
process 0 mvarApp.getAppArgs v
/--
Similar to processAssignment, but if it fails, compute v's whnf and try again.
This helps to solve constraints such as `?m =?= { α := ?m, ... }.α`
Note this is not perfect solution since we still fail occurs check for constraints such as
```lean
?m =?= List { α := ?m, β := Nat }.β
```
-/
private def processAssignment' (mvarApp : Expr) (v : Expr) : MetaM Bool := do
if (← processAssignment mvarApp v) then
return true
else
let vNew ← whnf v
if vNew != v then
if mvarApp == vNew then
return true
else
processAssignment mvarApp vNew
else
return false
private def isDeltaCandidate? (t : Expr) : MetaM (Option ConstantInfo) := do
match t.getAppFn with
| Expr.const c _ =>
match (← getConst? c) with
| r@(some info) => if info.hasValue then return r else return none
| _ => return none
| _ => pure none
/-- Auxiliary method for isDefEqDelta -/
private def isListLevelDefEq (us vs : List Level) : MetaM LBool :=
toLBoolM <| isListLevelDefEqAux us vs
/-- Auxiliary method for isDefEqDelta -/
private def isDefEqLeft (fn : Name) (t s : Expr) : MetaM LBool := do
trace[Meta.isDefEq.delta.unfoldLeft] fn
toLBoolM <| Meta.isExprDefEqAux t s
/-- Auxiliary method for isDefEqDelta -/
private def isDefEqRight (fn : Name) (t s : Expr) : MetaM LBool := do
trace[Meta.isDefEq.delta.unfoldRight] fn
toLBoolM <| Meta.isExprDefEqAux t s
/-- Auxiliary method for isDefEqDelta -/
private def isDefEqLeftRight (fn : Name) (t s : Expr) : MetaM LBool := do
trace[Meta.isDefEq.delta.unfoldLeftRight] fn
toLBoolM <| Meta.isExprDefEqAux t s
/-- Try to solve `f a₁ ... aₙ =?= f b₁ ... bₙ` by solving `a₁ =?= b₁, ..., aₙ =?= bₙ`.
Auxiliary method for isDefEqDelta -/
private def tryHeuristic (t s : Expr) : MetaM Bool := do
let mut t := t
let mut s := s
let tFn := t.getAppFn
let sFn := s.getAppFn
let info ← getConstInfo tFn.constName!
/- We only use the heuristic when `f` is a regular definition or an auxiliary `match` application.
That is, it is not marked an abbreviation (e.g., a user-facing projection) or as opaque (e.g., proof).
We check whether terms contain metavariables to make sure we can solve constraints such
as `S.proj ?x =?= S.proj t` without performing delta-reduction.
That is, we are assuming the heuristic implemented by this method is seldom effective
when `t` and `s` do not have metavariables, are not structurally equal, and `f` is an abbreviation.
On the other hand, by unfolding `f`, we often produce smaller terms.
Recall that auxiliary `match` definitions are marked as abbreviations, but we must use the heuristic on
them since they will not be unfolded when smartUnfolding is turned on. The abbreviation annotation in this
case is used to help the kernel type checker. -/
unless info.hints.isRegular || isMatcherCore (← getEnv) tFn.constName! do
unless t.hasExprMVar || s.hasExprMVar do
return false
withTraceNodeBefore `Meta.isDefEq.delta (return m!"{t} =?= {s}") do
/-
We process arguments before universe levels to reduce a source of brittleness in the TC procedure.
In the TC procedure, we can solve problems containing metavariables.
If the TC procedure tries to assign one of these metavariables, it interrupts the search
using a "stuck" exception. The elaborator catches it, and "interprets" it as "we should try again later".
Now suppose we have a TC problem, and there are two "local" candidate instances we can try: "bad" and "good".
The "bad" candidate is stuck because of a universe metavariable in the TC problem.
If we try "bad" first, the TC procedure is interrupted. Moreover, if we have ignored the exception,
"bad" would fail anyway trying to assign two different free variables `α =?= β`.
Example: `Preorder.{?u} α =?= Preorder.{?v} β`, where `?u` and `?v` are universe metavariables that were
not created by the TC procedure.
The key issue here is that we have an `isDefEq t s` invocation that is interrupted by the "stuck" exception,
but it would have failed anyway if we had continued processing it.
By solving the arguments first, we make the example above fail without throwing the "stuck" exception.
TODO: instead of throwing an exception as soon as we get stuck, we should just set a flag.
Then the entry-point for `isDefEq` checks the flag before returning `true`.
-/
checkpointDefEq do
isDefEqArgs tFn t.getAppArgs s.getAppArgs <&&>
isListLevelDefEqAux tFn.constLevels! sFn.constLevels!
/-- Auxiliary method for isDefEqDelta -/
private abbrev unfold (e : Expr) (failK : MetaM α) (successK : Expr → MetaM α) : MetaM α := do
match (← unfoldDefinition? e) with
| some e => successK e
| none => failK
/-- Auxiliary method for isDefEqDelta -/
private def unfoldBothDefEq (fn : Name) (t s : Expr) : MetaM LBool := do
match t, s with
| Expr.const _ ls₁, Expr.const _ ls₂ => isListLevelDefEq ls₁ ls₂
| Expr.app _ _, Expr.app _ _ =>
if (← tryHeuristic t s) then
pure LBool.true
else
unfold t
(unfold s (pure LBool.undef) (fun s => isDefEqRight fn t s))
(fun t => unfold s (isDefEqLeft fn t s) (fun s => isDefEqLeftRight fn t s))
| _, _ => pure LBool.false
private def sameHeadSymbol (t s : Expr) : Bool :=
match t.getAppFn, s.getAppFn with
| Expr.const c₁ _, Expr.const c₂ _ => c₁ == c₂
| _, _ => false
/--
- If headSymbol (unfold t) == headSymbol s, then unfold t
- If headSymbol (unfold s) == headSymbol t, then unfold s
- Otherwise unfold t and s if possible.
Auxiliary method for isDefEqDelta -/
private def unfoldComparingHeadsDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool :=
unfold t
(unfold s
(pure LBool.undef) -- `t` and `s` failed to be unfolded
(fun s => isDefEqRight sInfo.name t s))
(fun tNew =>
if sameHeadSymbol tNew s then
isDefEqLeft tInfo.name tNew s
else
unfold s
(isDefEqLeft tInfo.name tNew s)
(fun sNew =>
if sameHeadSymbol t sNew then
isDefEqRight sInfo.name t sNew
else
isDefEqLeftRight tInfo.name tNew sNew))
/-- If `t` and `s` do not contain metavariables, then use
kernel definitional equality heuristics.
Otherwise, use `unfoldComparingHeadsDefEq`.
Auxiliary method for isDefEqDelta -/
private def unfoldDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool :=
if !t.hasExprMVar && !s.hasExprMVar then
/- If `t` and `s` do not contain metavariables,
we simulate strategy used in the kernel. -/
if tInfo.hints.lt sInfo.hints then
unfold t (unfoldComparingHeadsDefEq tInfo sInfo t s) fun t => isDefEqLeft tInfo.name t s
else if sInfo.hints.lt tInfo.hints then
unfold s (unfoldComparingHeadsDefEq tInfo sInfo t s) fun s => isDefEqRight sInfo.name t s
else
unfoldComparingHeadsDefEq tInfo sInfo t s
else
unfoldComparingHeadsDefEq tInfo sInfo t s
/--
When `TransparencyMode` is set to `default` or `all`.
If `t` is reducible and `s` is not ==> `isDefEqLeft (unfold t) s`
If `s` is reducible and `t` is not ==> `isDefEqRight t (unfold s)`
Otherwise, use `unfoldDefEq`
Auxiliary method for isDefEqDelta -/
private def unfoldReducibeDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool := do
if (← shouldReduceReducibleOnly) then
unfoldDefEq tInfo sInfo t s
else
let tReducible ← isReducible tInfo.name
let sReducible ← isReducible sInfo.name
if tReducible && !sReducible then
unfold t (unfoldDefEq tInfo sInfo t s) fun t => isDefEqLeft tInfo.name t s
else if !tReducible && sReducible then
unfold s (unfoldDefEq tInfo sInfo t s) fun s => isDefEqRight sInfo.name t s
else
unfoldDefEq tInfo sInfo t s
/--
This is an auxiliary method for isDefEqDelta.
If `t` is a (non-class) projection function application and `s` is not ==> `isDefEqRight t (unfold s)`
If `s` is a (non-class) projection function application and `t` is not ==> `isDefEqRight (unfold t) s`
Otherwise, use `unfoldReducibeDefEq`
One motivation for the heuristic above is unification problems such as
```
id (?m.1) =?= (a, b).1
```
We want to reduce the lhs instead of the rhs, and eventually assign `?m := (a, b)`.
Another motivation for the heuristic above is unification problems such as
```
List.length (a :: as) =?= HAdd.hAdd (List.length as) 1
```
However, for class projections, we also unpack them and check whether the result function is the one
on the other side. This is relevant for unification problems such as
```
Foo.pow x 256 =?= Pow.pow x 256
```
where the the `Pow` instance is wrapping `Foo.pow`
See issue #1419 for the complete example.
-/
private partial def unfoldNonProjFnDefEq (tInfo sInfo : ConstantInfo) (t s : Expr) : MetaM LBool := do
let tProjInfo? ← getProjectionFnInfo? tInfo.name
let sProjInfo? ← getProjectionFnInfo? sInfo.name
if let some tNew ← packedInstanceOf? tProjInfo? t sInfo.name then
isDefEqLeft tInfo.name tNew s
else if let some sNew ← packedInstanceOf? sProjInfo? s tInfo.name then
isDefEqRight sInfo.name t sNew
else match tProjInfo?, sProjInfo? with
| some _, none => unfold s (unfoldDefEq tInfo sInfo t s) fun s => isDefEqRight sInfo.name t s
| none, some _ => unfold t (unfoldDefEq tInfo sInfo t s) fun t => isDefEqLeft tInfo.name t s
| _, _ => unfoldReducibeDefEq tInfo sInfo t s
where
packedInstanceOf? (projInfo? : Option ProjectionFunctionInfo) (e : Expr) (declName : Name) : MetaM (Option Expr) := do
let some { fromClass := true, .. } := projInfo? | return none -- It is not a class projection
let some e ← unfoldDefinition? e | return none
let e ← whnfCore e
if e.isAppOf declName then return some e
let .const name _ := e.getAppFn | return none
-- Keep going if new `e` is also a class projection
packedInstanceOf? (← getProjectionFnInfo? name) e declName
/--
isDefEq by lazy delta reduction.
This method implements many different heuristics:
1- If only `t` can be unfolded => then unfold `t` and continue
2- If only `s` can be unfolded => then unfold `s` and continue
3- If `t` and `s` can be unfolded and they have the same head symbol, then
a) First try to solve unification by unifying arguments.
b) If it fails, unfold both and continue.
Implemented by `unfoldBothDefEq`
4- If `t` is a projection function application and `s` is not => then unfold `s` and continue.
5- If `s` is a projection function application and `t` is not => then unfold `t` and continue.
Remark: 4&5 are implemented by `unfoldNonProjFnDefEq`
6- If `t` is reducible and `s` is not => then unfold `t` and continue.
7- If `s` is reducible and `t` is not => then unfold `s` and continue
Remark: 6&7 are implemented by `unfoldReducibeDefEq`
8- If `t` and `s` do not contain metavariables, then use heuristic used in the Kernel.
Implemented by `unfoldDefEq`
9- If `headSymbol (unfold t) == headSymbol s`, then unfold t and continue.
10- If `headSymbol (unfold s) == headSymbol t`, then unfold s
11- Otherwise, unfold `t` and `s` and continue.
Remark: 9&10&11 are implemented by `unfoldComparingHeadsDefEq` -/
private def isDefEqDelta (t s : Expr) : MetaM LBool := do
let tInfo? ← isDeltaCandidate? t
let sInfo? ← isDeltaCandidate? s
match tInfo?, sInfo? with
| none, none => pure LBool.undef
| some tInfo, none => unfold t (pure LBool.undef) fun t => isDefEqLeft tInfo.name t s
| none, some sInfo => unfold s (pure LBool.undef) fun s => isDefEqRight sInfo.name t s
| some tInfo, some sInfo =>
if tInfo.name == sInfo.name then
unfoldBothDefEq tInfo.name t s
else
unfoldNonProjFnDefEq tInfo sInfo t s
private def isAssigned : Expr → MetaM Bool
| Expr.mvar mvarId => mvarId.isAssigned
| _ => pure false
private def expandDelayedAssigned? (t : Expr) : MetaM (Option Expr) := do
let tFn := t.getAppFn
if !tFn.isMVar then return none
let some { fvars, mvarIdPending } ← getDelayedMVarAssignment? tFn.mvarId! | return none
let tNew ← instantiateMVars t
if tNew != t then return some tNew
/-
If `assignSyntheticOpaque` is true, we must follow the delayed assignment.
Recall a delayed assignment `mvarId [xs] := mvarIdPending` is morally an assingment
`mvarId := fun xs => mvarIdPending` where `xs` are free variables in the scope of `mvarIdPending`,
but not in the scope of `mvarId`. We can only perform the abstraction when `mvarIdPending` has been fully synthesized.
That is, `instantiateMVars (mkMVar mvarIdPending)` does not contain any expression metavariables.
Here we just consume `fvar.size` arguments. That is, if `t` is of the form `mvarId as bs` where `as.size == fvars.size`,
we return `mvarIdPending bs`.
TODO: improve this transformation. Here is a possible improvement.
Assume `t` is of the form `?m as` where `as` represent the arguments, and we are trying to solve
`?m as =?= s[as]` where `s[as]` represents a term containing occurrences of `as`.
We could try to compute the solution as usual `?m := fun ys => s[as/ys]`
We also have the delayed assignment `?m [xs] := ?n`, where `xs` are variables in the scope of `?n`,
and this delayed assignment is morally `?m := fun xs => ?n`.
Thus, we can reduce `?m as =?= s[as]` to `?n =?= s[as/xs]`, and solve it using `?n`'s local context.
This is more precise than simplying droping the arguments `as`.
-/
unless (← getConfig).assignSyntheticOpaque do return none
let tArgs := t.getAppArgs
if tArgs.size < fvars.size then return none
return some (mkAppRange (mkMVar mvarIdPending) fvars.size tArgs.size tArgs)
private def isAssignable : Expr → MetaM Bool
| Expr.mvar mvarId => do let b ← mvarId.isReadOnlyOrSyntheticOpaque; pure (!b)
| _ => pure false
private def etaEq (t s : Expr) : Bool :=
match t.etaExpanded? with
| some t => t == s
| none => false
/--
Helper method for implementing `isDefEqProofIrrel`. Execute `k` with a transparency setting
that is at least as strong as `.default`. This is important for modules that use the `.reducible`
setting (e.g., `simp`, `rw`, etc). We added this feature to address issue #1302.
```lean
@[simp] theorem get_cons_zero {as : List α} : (a :: as).get ⟨0, Nat.zero_lt_succ _⟩ = a := rfl
example (a b c : α) : [a, b, c].get ⟨0, by simp⟩ = a := by simp
```
In the example above `simp` fails to use `get_cons_zero` because it fails to establish that
the proof objects are definitionally equal using proof irrelevance. In this example,
the propositions are
```lean
0 < Nat.succ (List.length [b, c]) =?= 0 < Nat.succ (Nat.succ (Nat.succ 0))
```
So, unless we can unfold `List.length`, it fails.
Remark: if this becomes a performance bottleneck, we should add a flag to control when it is used.
Then, we can enable the flag only when applying `simp` and `rw` theorems.
-/
private def withProofIrrelTransparency (k : MetaM α) : MetaM α :=
withAtLeastTransparency .default k
private def isDefEqProofIrrel (t s : Expr) : MetaM LBool := do
if (← getConfig).proofIrrelevance then
match (← isProofQuick t) with
| LBool.false =>
pure LBool.undef
| LBool.true =>
let tType ← inferType t
let sType ← inferType s
toLBoolM <| withProofIrrelTransparency <| Meta.isExprDefEqAux tType sType
| LBool.undef =>
let tType ← inferType t
if (← isProp tType) then
let sType ← inferType s
toLBoolM <| withProofIrrelTransparency <| Meta.isExprDefEqAux tType sType
else
pure LBool.undef
else
pure LBool.undef
/-- Try to solve constraint of the form `?m args₁ =?= ?m args₂`.
- First try to unify `args₁` and `args₂`, and return true if successful
- Otherwise, try to assign `?m` to a constant function of the form `fun x_1 ... x_n => ?n`
where `?n` is a fresh metavariable. See `assignConst`. -/
private def isDefEqMVarSelf (mvar : Expr) (args₁ args₂ : Array Expr) : MetaM Bool := do
if args₁.size != args₂.size then
pure false
else if (← isDefEqArgs mvar args₁ args₂) then
pure true
else if !(← isAssignable mvar) then
pure false
else
let cfg ← getConfig
let mvarId := mvar.mvarId!
let mvarDecl ← mvarId.getDecl
if mvarDecl.numScopeArgs == args₁.size || cfg.constApprox then
let type ← inferType (mkAppN mvar args₁)
let auxMVar ← mkAuxMVar mvarDecl.lctx mvarDecl.localInstances type
assignConst mvar args₁.size auxMVar
else
pure false
/-- Remove unnecessary let-decls -/
private def consumeLet : Expr → Expr
| e@(Expr.letE _ _ _ b _) => if b.hasLooseBVars then e else consumeLet b
| e => e
mutual
private partial def isDefEqQuick (t s : Expr) : MetaM LBool :=
let t := consumeLet t
let s := consumeLet s
match t, s with
| .lit l₁, .lit l₂ => return (l₁ == l₂).toLBool
| .sort u, .sort v => toLBoolM <| isLevelDefEqAux u v
| .lam .., .lam .. => if t == s then pure LBool.true else toLBoolM <| isDefEqBinding t s
| .forallE .., .forallE .. => if t == s then pure LBool.true else toLBoolM <| isDefEqBinding t s
-- | Expr.mdata _ t _, s => isDefEqQuick t s
-- | t, Expr.mdata _ s _ => isDefEqQuick t s
| .fvar fvarId₁, .fvar fvarId₂ => do
if (← fvarId₁.isLetVar <||> fvarId₂.isLetVar) then
return LBool.undef
else if fvarId₁ == fvarId₂ then
return LBool.true
else
isDefEqProofIrrel t s
| t, s =>
isDefEqQuickOther t s
private partial def isDefEqQuickOther (t s : Expr) : MetaM LBool := do
/-
We used to eagerly consume all metadata (see commented lines at `isDefEqQuick`),
but it was unnecessarily removing helpful annotations
for the pretty-printer. For example, consider the following example.
```
constant p : Nat → Prop
constant q : Nat → Prop
theorem p_of_q : q x → p x := sorry
theorem pletfun : p (let_fun x := 0; x + 1) := by
-- ⊢ p (let_fun x := 0; x + 1)
apply p_of_q -- If we eagerly consume all metadata, the let_fun annotation is lost during `isDefEq`
-- ⊢ q ((fun x => x + 1) 0)
sorry
```
However, pattern annotations (`inaccessible?` and `patternWithRef?`) must be consumed.
The frontend relies on the fact that is must not be propagated by `isDefEq`.
Thus, we consume it here. This is a bit hackish since it is very adhoc.
We might other annotations in the future that we should not preserve.
Perhaps, we should mark the annotation we do want to preserve ones
(e.g., hints for the pretty printer), and consume all other
-/
if let some t := patternAnnotation? t then
isDefEqQuick t s
else if let some s := patternAnnotation? s then
isDefEqQuick t s
else if t == s then
return LBool.true
else if etaEq t s || etaEq s t then
return LBool.true -- t =?= (fun xs => t xs)
else
let tFn := t.getAppFn
let sFn := s.getAppFn
if !tFn.isMVar && !sFn.isMVar then
return LBool.undef
else if (← isAssigned tFn) then
let t ← instantiateMVars t
isDefEqQuick t s
else if (← isAssigned sFn) then
let s ← instantiateMVars s
isDefEqQuick t s
else if let some t ← expandDelayedAssigned? t then
isDefEqQuick t s
else if let some s ← expandDelayedAssigned? s then
isDefEqQuick t s
/- Remark: we do not eagerly synthesize synthetic metavariables when the constraint is not stuck.
Reason: we may fail to solve a constraint of the form `?x =?= A` when the synthesized instance
is not definitionally equal to `A`. We left the code here as a remainder of this issue. -/
-- else if (← isSynthetic tFn <&&> trySynthPending tFn) then
-- let t ← instantiateMVars t
-- isDefEqQuick t s
-- else if (← isSynthetic sFn <&&> trySynthPending sFn) then
-- let s ← instantiateMVars s
-- isDefEqQuick t s
else if tFn.isMVar && sFn.isMVar && tFn == sFn then
Bool.toLBool <$> isDefEqMVarSelf tFn t.getAppArgs s.getAppArgs
else
let tAssign? ← isAssignable tFn
let sAssign? ← isAssignable sFn
let assignableMsg (b : Bool) := if b then "[assignable]" else "[nonassignable]"
trace[Meta.isDefEq] "{t} {assignableMsg tAssign?} =?= {s} {assignableMsg sAssign?}"
if tAssign? && !sAssign? then
toLBoolM <| processAssignment' t s
else if !tAssign? && sAssign? then
toLBoolM <| processAssignment' s t
else if !tAssign? && !sAssign? then
/- Trying to unify `?m ... =?= ?n ...` where both `?m` and `?n` cannot be assigned.
This can happen when both of them are `syntheticOpaque` (e.g., metavars associated with tactics), or a metavariables
from previous levels.
If their types are propositions and are defeq, we can solve the constraint by proof irrelevance.
This test is important for fixing a performance problem exposed by test `isDefEqPerfIssue.lean`.
Without the proof irrelevance check, this example timeouts. Recall that:
1- The elaborator has a pending list of things to do: Tactics, TC, etc.
2- The elaborator only tries tactics after it tried to solve pending TC problems, delayed elaboratio, etc.
The motivation: avoid unassigned metavariables in goals.
3- Each pending tactic goal is represented as a metavariable. It is marked as `synthethicOpaque` to make it clear
that it should not be assigned by unification.
4- When we abstract a term containing metavariables, we often create new metavariables.
Example: when abstracting `x` at `f ?m`, we obtain `fun x => f (?m' x)`. If `x` is in the scope of `?m`.
If `?m` is `syntheticOpaque`, so is `?m'`, and we also have the delayed assignment `?m' x := ?m`
5- When checking a metavariable assignment, `?m := v` we check whether the type of `?m` is defeq to type of `v`
with default reducibility setting.
Now consider the following fragment
```
let a' := g 100 a ⟨i, h⟩ ⟨i - Nat.zero.succ, by exact Nat.lt_of_le_of_lt (Nat.pred_le i) h⟩
have : a'.size - i >= 0 := sorry
f (i+1) a'
```
The elaborator tries to synthesize the instance `OfNat Nat 1` before we generate the tactic proof for `by exact ...` (remark 2).
The solution `instOfNatNat 1` is synthesized. Let `m? a i h a' this` be the "hole" associated with the pending instance.
Then, `isDefEq` tries to assign `m? a i h a' this := instOfNatNat 1` which is reduced to
`m? := mkLambdaFVars #[a, i, h, a', this] (instOfNatNat 1)`. Note that, this is an abstraction step (remark 4), and the type
contains the `syntheticOpaque` metavariable for the pending tactic proof (remark 3). Thus, a new `syntheticOpaque`
opaque is created (remark 4). Then, `isDefEq` must check whether the type of `?m` is defeq to
`mkLambdaFVars #[a, i, h, a', this] (instOfNatNat 1)` (remark 5). The two types are almost identical, but they
contain different `syntheticOpaque` in the subterm corresponding to the `by exact ...` tactic proof. Without the following
proof irrelevance test, the check will fail, and `isDefEq` timeouts unfolding `g` and its dependencies.
Note that this test does not prevent a similar performance problem in a usecase where the tactic is used to synthesize a
term that is not a proof. TODO: add better support for checking the delayed assignments. This is not high priority because
tactics are usually only used for synthesizing proofs.
-/
match (← isDefEqProofIrrel t s) with
| LBool.true => return LBool.true
| LBool.false => return LBool.false
| _ =>
if tFn.isMVar || sFn.isMVar then
let ctx ← read
if ctx.config.isDefEqStuckEx then do
trace[Meta.isDefEq.stuck] "{t} =?= {s}"
Meta.throwIsDefEqStuck
else
return LBool.false
else
return LBool.undef
else
isDefEqQuickMVarMVar t s
/-- Both `t` and `s` are terms of the form `?m ...` -/
private partial def isDefEqQuickMVarMVar (t s : Expr) : MetaM LBool := do
if s.isMVar && !t.isMVar then
/- Solve `?m t =?= ?n` by trying first `?n := ?m t`.
Reason: this assignment is precise. -/
if (← checkpointDefEq (processAssignment s t)) then
return LBool.true
else
toLBoolM <| processAssignment t s
else
if (← checkpointDefEq (processAssignment t s)) then
return LBool.true
else
toLBoolM <| processAssignment s t
end
@[inline] def whenUndefDo (x : MetaM LBool) (k : MetaM Bool) : MetaM Bool := do
let status ← x
match status with
| LBool.true => pure true
| LBool.false => pure false
| LBool.undef => k
@[specialize] private def unstuckMVar (e : Expr) (successK : Expr → MetaM Bool) (failK : MetaM Bool): MetaM Bool := do
match (← getStuckMVar? e) with
| some mvarId =>
trace[Meta.isDefEq.stuckMVar] "found stuck MVar {mkMVar mvarId} : {← inferType (mkMVar mvarId)}"
if (← Meta.synthPending mvarId) then
let e ← instantiateMVars e
successK e
else
failK
| none => failK
private def isDefEqOnFailure (t s : Expr) : MetaM Bool := do
withTraceNodeBefore `Meta.isDefEq.onFailure (return m!"{t} =?= {s}") do
unstuckMVar t (fun t => Meta.isExprDefEqAux t s) <|
unstuckMVar s (fun s => Meta.isExprDefEqAux t s) <|
tryUnificationHints t s <||> tryUnificationHints s t
private def isDefEqProj : Expr → Expr → MetaM Bool
| Expr.proj _ i t, Expr.proj _ j s => pure (i == j) <&&> Meta.isExprDefEqAux t s
| Expr.proj structName 0 s, v => isDefEqSingleton structName s v
| v, Expr.proj structName 0 s => isDefEqSingleton structName s v
| _, _ => pure false
where
/-- If `structName` is a structure with a single field and `(?m ...).1 =?= v`, then solve contraint as `?m ... =?= ⟨v⟩` -/
isDefEqSingleton (structName : Name) (s : Expr) (v : Expr) : MetaM Bool := do
if isClass (← getEnv) structName then
/-
We disable this feature is `structName` is a class. See issue #2011.
The example at issue #2011, the following weird
instance was being generated for `Zero (f x)`
```
(@Zero.mk (f x✝) ((@instZero I (fun i => f i) fun i => inst✝¹ i).1 x✝)
```
where `inst✝¹` is the local instance `[∀ i, Zero (f i)]`
Note that this instance is definitinally equal to the expected nicer
instance `inst✝¹ x✝`.
However, the nasty instance trigger nasty unification higher order
constraints later.
We say this behavior is defensible because it is more reliable to use TC resolution to
assign `?m`.
-/
return false
let ctorVal := getStructureCtor (← getEnv) structName
if ctorVal.numFields != 1 then
return false -- It is not a structure with a single field.
let sType ← whnf (← inferType s)
let sTypeFn := sType.getAppFn
if !sTypeFn.isConstOf structName then
return false
let s ← whnf s
let sFn := s.getAppFn
if !sFn.isMVar then
return false
if (← isAssignable sFn) then
let ctorApp := mkApp (mkAppN (mkConst ctorVal.name sTypeFn.constLevels!) sType.getAppArgs) v
processAssignment' s ctorApp
else
return false
/--
Given applications `t` and `s` that are in WHNF (modulo the current transparency setting),
check whether they are definitionally equal or not.
-/
private def isDefEqApp (t s : Expr) : MetaM Bool := do
let tFn := t.getAppFn
let sFn := s.getAppFn
if tFn.isConst && sFn.isConst && tFn.constName! == sFn.constName! then
/- See comment at `tryHeuristic` explaining why we processe arguments before universe levels. -/
if (← checkpointDefEq (isDefEqArgs tFn t.getAppArgs s.getAppArgs <&&> isListLevelDefEqAux tFn.constLevels! sFn.constLevels!)) then
return true
else
isDefEqOnFailure t s
else if (← checkpointDefEq (Meta.isExprDefEqAux tFn s.getAppFn <&&> isDefEqArgs tFn t.getAppArgs s.getAppArgs)) then
return true
else
isDefEqOnFailure t s
/-- Return `true` if the types of the given expressions is an inductive datatype with an inductive datatype with a single constructor with no fields. -/
private def isDefEqUnitLike (t : Expr) (s : Expr) : MetaM Bool := do
let tType ← whnf (← inferType t)
matchConstStruct tType.getAppFn (fun _ => return false) fun _ _ ctorVal => do
if ctorVal.numFields != 0 then
return false
else if (← useEtaStruct ctorVal.induct) then
Meta.isExprDefEqAux tType (← inferType s)
else
return false
/--
The `whnf` procedure has support for unfolding class projections when the
transparency mode is set to `.instances`. This method ensures the behavior
of `whnf` and `isDefEq` is consistent in this transparency mode.
-/
private def isDefEqProjInst (t : Expr) (s : Expr) : MetaM LBool := do
if (← getTransparency) != .instances then return .undef
let t? ← unfoldProjInstWhenIntances? t
let s? ← unfoldProjInstWhenIntances? s
if t?.isSome || s?.isSome then
toLBoolM <| Meta.isExprDefEqAux (t?.getD t) (s?.getD s)
else
return .undef
private def isExprDefEqExpensive (t : Expr) (s : Expr) : MetaM Bool := do
whenUndefDo (isDefEqEta t s) do
whenUndefDo (isDefEqEta s t) do
-- TODO: investigate whether this is the place for putting this check
if (← (isDefEqEtaStruct t s <||> isDefEqEtaStruct s t)) then return true
if (← isDefEqProj t s) then return true
let t' ← whnfCore t
let s' ← whnfCore s
if t != t' || s != s' then
Meta.isExprDefEqAux t' s'
else
whenUndefDo (isDefEqNative t s) do
whenUndefDo (isDefEqNat t s) do
whenUndefDo (isDefEqOffset t s) do
whenUndefDo (isDefEqDelta t s) do
if t.isConst && s.isConst then
if t.constName! == s.constName! then isListLevelDefEqAux t.constLevels! s.constLevels! else return false
else if (← pure t.isApp <&&> pure s.isApp <&&> isDefEqApp t s) then
return true
else
whenUndefDo (isDefEqProjInst t s) do
whenUndefDo (isDefEqStringLit t s) do
if (← isDefEqUnitLike t s) then return true else
isDefEqOnFailure t s
private def mkCacheKey (t : Expr) (s : Expr) : Expr × Expr :=
if Expr.quickLt t s then (t, s) else (s, t)
private def getCachedResult (key : Expr × Expr) : MetaM LBool := do
match (← get).cache.defEq.find? key with
| some val => return val.toLBool
| none => return .undef
private def cacheResult (key : Expr × Expr) (result : Bool) : MetaM Unit := do
/-
We must ensure that all assigned metavariables in the key are replaced by their current assingments.
Otherwise, the key is invalid after the assignment is "backtracked".
See issue #1870 for an example.
-/
let key := (← instantiateMVars key.1, ← instantiateMVars key.2)
modifyDefEqCache fun c => c.insert key result
@[export lean_is_expr_def_eq]
partial def isExprDefEqAuxImpl (t : Expr) (s : Expr) : MetaM Bool := withIncRecDepth do
withTraceNodeBefore `Meta.isDefEq (return m!"{t} =?= {s}") do
checkMaxHeartbeats "isDefEq"
whenUndefDo (isDefEqQuick t s) do
whenUndefDo (isDefEqProofIrrel t s) do
-- We perform `whnfCore` again with `deltaAtProj := true` at `isExprDefEqExpensive` after `isDefEqProj`
let t' ← whnfCore t (deltaAtProj := false)
let s' ← whnfCore s (deltaAtProj := false)
if t != t' || s != s' then
isExprDefEqAuxImpl t' s'
else
/-
TODO: check whether the following `instantiateMVar`s are expensive or not in practice.
Lean 3 does not use them, and may miss caching opportunities since it is not safe to cache when `t` and `s` may contain mvars.
The unit test `tryHeuristicPerfIssue2.lean` cannot be solved without these two `instantiateMVar`s.
If it becomes a problem, we may use store a flag in the context indicating whether we have already used `instantiateMVar` in
outer invocations or not. It is not perfect (we may assign mvars in nested calls), but it should work well enough in practice,
and prevent repeated traversals in nested calls.
-/
let t ← instantiateMVars t
let s ← instantiateMVars s
let numPostponed ← getNumPostponed
let k := mkCacheKey t s
match (← getCachedResult k) with
| .true =>
trace[Meta.isDefEq.cache] "cache hit 'true' for {t} =?= {s}"
return true
| .false =>
trace[Meta.isDefEq.cache] "cache hit 'false' for {t} =?= {s}"
return false
| .undef =>
let result ← isExprDefEqExpensive t s
if numPostponed == (← getNumPostponed) then
trace[Meta.isDefEq.cache] "cache {result} for {t} =?= {s}"
cacheResult k result
return result
builtin_initialize
registerTraceClass `Meta.isDefEq
registerTraceClass `Meta.isDefEq.stuck
registerTraceClass `Meta.isDefEq.stuck.mvar (inherited := true)
registerTraceClass `Meta.isDefEq.cache
registerTraceClass `Meta.isDefEq.foApprox (inherited := true)
registerTraceClass `Meta.isDefEq.onFailure (inherited := true)
registerTraceClass `Meta.isDefEq.constApprox (inherited := true)
registerTraceClass `Meta.isDefEq.delta
registerTraceClass `Meta.isDefEq.delta.unfoldLeft (inherited := true)
registerTraceClass `Meta.isDefEq.delta.unfoldRight (inherited := true)
registerTraceClass `Meta.isDefEq.delta.unfoldLeftRight (inherited := true)
registerTraceClass `Meta.isDefEq.assign
registerTraceClass `Meta.isDefEq.assign.checkTypes (inherited := true)
registerTraceClass `Meta.isDefEq.assign.outOfScopeFVar (inherited := true)
registerTraceClass `Meta.isDefEq.assign.beforeMkLambda (inherited := true)
registerTraceClass `Meta.isDefEq.assign.typeError (inherited := true)
registerTraceClass `Meta.isDefEq.assign.occursCheck (inherited := true)
registerTraceClass `Meta.isDefEq.assign.readOnlyMVarWithBiggerLCtx (inherited := true)
registerTraceClass `Meta.isDefEq.eta.struct
end Lean.Meta
|
4236483258719f1f039d2526118e8d7f804afa66 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/limits/shapes/zero_morphisms.lean | a4c8d04ca24b4f79bdd11f7dc3a0283fd0b0e83d | [
"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 | 20,086 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import data.pi.algebra
import category_theory.limits.shapes.products
import category_theory.limits.shapes.images
import category_theory.isomorphism_classes
import category_theory.limits.shapes.zero_objects
/-!
# Zero morphisms and zero objects
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
A category "has zero morphisms" if there is a designated "zero morphism" in each morphism space,
and compositions of zero morphisms with anything give the zero morphism. (Notice this is extra
structure, not merely a property.)
A category "has a zero object" if it has an object which is both initial and terminal. Having a
zero object provides zero morphisms, as the unique morphisms factoring through the zero object.
## References
* https://en.wikipedia.org/wiki/Zero_morphism
* [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2]
-/
noncomputable theory
universes v u
universes v' u'
open category_theory
open category_theory.category
open_locale classical
namespace category_theory.limits
variables (C : Type u) [category.{v} C]
variables (D : Type u') [category.{v'} D]
/-- A category "has zero morphisms" if there is a designated "zero morphism" in each morphism space,
and compositions of zero morphisms with anything give the zero morphism. -/
class has_zero_morphisms :=
[has_zero : Π X Y : C, has_zero (X ⟶ Y)]
(comp_zero' : ∀ {X Y : C} (f : X ⟶ Y) (Z : C), f ≫ (0 : Y ⟶ Z) = (0 : X ⟶ Z) . obviously)
(zero_comp' : ∀ (X : C) {Y Z : C} (f : Y ⟶ Z), (0 : X ⟶ Y) ≫ f = (0 : X ⟶ Z) . obviously)
attribute [instance] has_zero_morphisms.has_zero
restate_axiom has_zero_morphisms.comp_zero'
restate_axiom has_zero_morphisms.zero_comp'
variables {C}
@[simp] lemma comp_zero [has_zero_morphisms C] {X Y : C} {f : X ⟶ Y} {Z : C} :
f ≫ (0 : Y ⟶ Z) = (0 : X ⟶ Z) := has_zero_morphisms.comp_zero f Z
@[simp] lemma zero_comp [has_zero_morphisms C] {X : C} {Y Z : C} {f : Y ⟶ Z} :
(0 : X ⟶ Y) ≫ f = (0 : X ⟶ Z) := has_zero_morphisms.zero_comp X f
instance has_zero_morphisms_pempty : has_zero_morphisms (discrete pempty) :=
{ has_zero := by tidy }
instance has_zero_morphisms_punit : has_zero_morphisms (discrete punit) :=
{ has_zero := by tidy }
namespace has_zero_morphisms
variables {C}
/-- This lemma will be immediately superseded by `ext`, below. -/
private lemma ext_aux (I J : has_zero_morphisms C)
(w : ∀ X Y : C, (@has_zero_morphisms.has_zero _ _ I X Y).zero =
(@has_zero_morphisms.has_zero _ _ J X Y).zero) : I = J :=
begin
casesI I, casesI J,
congr,
{ ext X Y,
exact w X Y },
{ apply proof_irrel_heq, },
{ apply proof_irrel_heq, }
end
/--
If you're tempted to use this lemma "in the wild", you should probably
carefully consider whether you've made a mistake in allowing two
instances of `has_zero_morphisms` to exist at all.
See, particularly, the note on `zero_morphisms_of_zero_object` below.
-/
lemma ext (I J : has_zero_morphisms C) : I = J :=
begin
apply ext_aux,
intros X Y,
rw ←@has_zero_morphisms.comp_zero _ _ I X X (@has_zero_morphisms.has_zero _ _ J X X).zero,
rw @has_zero_morphisms.zero_comp _ _ J,
end
instance : subsingleton (has_zero_morphisms C) :=
⟨ext⟩
end has_zero_morphisms
open opposite has_zero_morphisms
instance has_zero_morphisms_opposite [has_zero_morphisms C] :
has_zero_morphisms Cᵒᵖ :=
{ has_zero := λ X Y, ⟨(0 : unop Y ⟶ unop X).op⟩,
comp_zero' := λ X Y f Z, congr_arg quiver.hom.op (has_zero_morphisms.zero_comp (unop Z) f.unop),
zero_comp' := λ X Y Z f, congr_arg quiver.hom.op (has_zero_morphisms.comp_zero f.unop (unop X)), }
section
variables {C} [has_zero_morphisms C]
lemma zero_of_comp_mono {X Y Z : C} {f : X ⟶ Y} (g : Y ⟶ Z) [mono g] (h : f ≫ g = 0) : f = 0 :=
by { rw [←zero_comp, cancel_mono] at h, exact h }
lemma zero_of_epi_comp {X Y Z : C} (f : X ⟶ Y) {g : Y ⟶ Z} [epi f] (h : f ≫ g = 0) : g = 0 :=
by { rw [←comp_zero, cancel_epi] at h, exact h }
lemma eq_zero_of_image_eq_zero {X Y : C} {f : X ⟶ Y} [has_image f] (w : image.ι f = 0) : f = 0 :=
by rw [←image.fac f, w, has_zero_morphisms.comp_zero]
lemma nonzero_image_of_nonzero {X Y : C} {f : X ⟶ Y} [has_image f] (w : f ≠ 0) : image.ι f ≠ 0 :=
λ h, w (eq_zero_of_image_eq_zero h)
end
section
variables [has_zero_morphisms D]
instance : has_zero_morphisms (C ⥤ D) :=
{ has_zero := λ F G, ⟨{ app := λ X, 0, }⟩ }
@[simp] lemma zero_app (F G : C ⥤ D) (j : C) : (0 : F ⟶ G).app j = 0 := rfl
end
namespace is_zero
variables [has_zero_morphisms C]
lemma eq_zero_of_src {X Y : C} (o : is_zero X) (f : X ⟶ Y) : f = 0 :=
o.eq_of_src _ _
lemma eq_zero_of_tgt {X Y : C} (o : is_zero Y) (f : X ⟶ Y) : f = 0 :=
o.eq_of_tgt _ _
lemma iff_id_eq_zero (X : C) : is_zero X ↔ (𝟙 X = 0) :=
⟨λ h, h.eq_of_src _ _,
λ h, ⟨
λ Y, ⟨⟨⟨0⟩, λ f, by { rw [←id_comp f, ←id_comp default, h, zero_comp, zero_comp], }⟩⟩,
λ Y, ⟨⟨⟨0⟩, λ f, by { rw [←comp_id f, ←comp_id default, h, comp_zero, comp_zero], }⟩⟩⟩⟩
lemma of_mono_zero (X Y : C) [mono (0 : X ⟶ Y)] : is_zero X :=
(iff_id_eq_zero X).mpr ((cancel_mono (0 : X ⟶ Y)).1 (by simp))
lemma of_epi_zero (X Y : C) [epi (0 : X ⟶ Y)] : is_zero Y :=
(iff_id_eq_zero Y).mpr ((cancel_epi (0 : X ⟶ Y)).1 (by simp))
lemma of_mono_eq_zero {X Y : C} (f : X ⟶ Y) [mono f] (h : f = 0) : is_zero X :=
by { unfreezingI { subst h, }, apply of_mono_zero X Y, }
lemma of_epi_eq_zero {X Y : C} (f : X ⟶ Y) [epi f] (h : f = 0) : is_zero Y :=
by { unfreezingI { subst h, }, apply of_epi_zero X Y, }
lemma iff_is_split_mono_eq_zero {X Y : C} (f : X ⟶ Y) [is_split_mono f] : is_zero X ↔ f = 0 :=
begin
rw iff_id_eq_zero,
split,
{ intro h, rw [←category.id_comp f, h, zero_comp], },
{ intro h, rw [←is_split_mono.id f], simp [h], },
end
lemma iff_is_split_epi_eq_zero {X Y : C} (f : X ⟶ Y) [is_split_epi f] : is_zero Y ↔ f = 0 :=
begin
rw iff_id_eq_zero,
split,
{ intro h, rw [←category.comp_id f, h, comp_zero], },
{ intro h, rw [←is_split_epi.id f], simp [h], },
end
lemma of_mono {X Y : C} (f : X ⟶ Y) [mono f] (i : is_zero Y) : is_zero X :=
begin
unfreezingI { have hf := i.eq_zero_of_tgt f, subst hf, },
exact is_zero.of_mono_zero X Y,
end
lemma of_epi {X Y : C} (f : X ⟶ Y) [epi f] (i : is_zero X) : is_zero Y :=
begin
unfreezingI { have hf := i.eq_zero_of_src f, subst hf, },
exact is_zero.of_epi_zero X Y,
end
end is_zero
/-- A category with a zero object has zero morphisms.
It is rarely a good idea to use this. Many categories that have a zero object have zero
morphisms for some other reason, for example from additivity. Library code that uses
`zero_morphisms_of_zero_object` will then be incompatible with these categories because
the `has_zero_morphisms` instances will not be definitionally equal. For this reason library
code should generally ask for an instance of `has_zero_morphisms` separately, even if it already
asks for an instance of `has_zero_objects`. -/
def is_zero.has_zero_morphisms {O : C} (hO : is_zero O) : has_zero_morphisms C :=
{ has_zero := λ X Y,
{ zero := hO.from X ≫ hO.to Y },
zero_comp' := λ X Y Z f, by { rw category.assoc, congr, apply hO.eq_of_src, },
comp_zero' := λ X Y Z f, by { rw ←category.assoc, congr, apply hO.eq_of_tgt, }}
namespace has_zero_object
variables [has_zero_object C]
open_locale zero_object
/-- A category with a zero object has zero morphisms.
It is rarely a good idea to use this. Many categories that have a zero object have zero
morphisms for some other reason, for example from additivity. Library code that uses
`zero_morphisms_of_zero_object` will then be incompatible with these categories because
the `has_zero_morphisms` instances will not be definitionally equal. For this reason library
code should generally ask for an instance of `has_zero_morphisms` separately, even if it already
asks for an instance of `has_zero_objects`. -/
def zero_morphisms_of_zero_object : has_zero_morphisms C :=
{ has_zero := λ X Y,
{ zero := (default : X ⟶ 0) ≫ default },
zero_comp' := λ X Y Z f, by { dunfold has_zero.zero, rw category.assoc, congr, },
comp_zero' := λ X Y Z f, by { dunfold has_zero.zero, rw ←category.assoc, congr, }}
section has_zero_morphisms
variables [has_zero_morphisms C]
@[simp] lemma zero_iso_is_initial_hom {X : C} (t : is_initial X) :
(zero_iso_is_initial t).hom = 0 :=
by ext
@[simp] lemma zero_iso_is_initial_inv {X : C} (t : is_initial X) :
(zero_iso_is_initial t).inv = 0 :=
by ext
@[simp] lemma zero_iso_is_terminal_hom {X : C} (t : is_terminal X) :
(zero_iso_is_terminal t).hom = 0 :=
by ext
@[simp] lemma zero_iso_is_terminal_inv {X : C} (t : is_terminal X) :
(zero_iso_is_terminal t).inv = 0 :=
by ext
@[simp] lemma zero_iso_initial_hom [has_initial C] : zero_iso_initial.hom = (0 : 0 ⟶ ⊥_ C) :=
by ext
@[simp] lemma zero_iso_initial_inv [has_initial C] : zero_iso_initial.inv = (0 : ⊥_ C ⟶ 0) :=
by ext
@[simp] lemma zero_iso_terminal_hom [has_terminal C] : zero_iso_terminal.hom = (0 : 0 ⟶ ⊤_ C) :=
by ext
@[simp] lemma zero_iso_terminal_inv [has_terminal C] : zero_iso_terminal.inv = (0 : ⊤_ C ⟶ 0) :=
by ext
end has_zero_morphisms
open_locale zero_object
instance {B : Type*} [category B] : has_zero_object (B ⥤ C) :=
(((category_theory.functor.const B).obj (0 : C)).is_zero $ λ X, is_zero_zero _).has_zero_object
end has_zero_object
open_locale zero_object
variables {D}
@[simp] lemma is_zero.map [has_zero_object D] [has_zero_morphisms D] {F : C ⥤ D} (hF : is_zero F)
{X Y : C} (f : X ⟶ Y) : F.map f = 0 :=
(hF.obj _).eq_of_src _ _
@[simp] lemma _root_.category_theory.functor.zero_obj [has_zero_object D]
(X : C) : is_zero ((0 : C ⥤ D).obj X) :=
(is_zero_zero _).obj _
@[simp] lemma _root_.category_theory.zero_map [has_zero_object D] [has_zero_morphisms D]
{X Y : C} (f : X ⟶ Y) : (0 : C ⥤ D).map f = 0 :=
(is_zero_zero _).map _
section
variables [has_zero_object C] [has_zero_morphisms C]
open_locale zero_object
@[simp]
lemma id_zero : 𝟙 (0 : C) = (0 : 0 ⟶ 0) :=
by ext
/-- An arrow ending in the zero object is zero -/
-- This can't be a `simp` lemma because the left hand side would be a metavariable.
lemma zero_of_to_zero {X : C} (f : X ⟶ 0) : f = 0 :=
by ext
lemma zero_of_target_iso_zero {X Y : C} (f : X ⟶ Y) (i : Y ≅ 0) : f = 0 :=
begin
have h : f = f ≫ i.hom ≫ 𝟙 0 ≫ i.inv := by simp only [iso.hom_inv_id, id_comp, comp_id],
simpa using h,
end
/-- An arrow starting at the zero object is zero -/
lemma zero_of_from_zero {X : C} (f : 0 ⟶ X) : f = 0 :=
by ext
lemma zero_of_source_iso_zero {X Y : C} (f : X ⟶ Y) (i : X ≅ 0) : f = 0 :=
begin
have h : f = i.hom ≫ 𝟙 0 ≫ i.inv ≫ f := by simp only [iso.hom_inv_id_assoc, id_comp, comp_id],
simpa using h,
end
lemma zero_of_source_iso_zero' {X Y : C} (f : X ⟶ Y) (i : is_isomorphic X 0) : f = 0 :=
zero_of_source_iso_zero f (nonempty.some i)
lemma zero_of_target_iso_zero' {X Y : C} (f : X ⟶ Y) (i : is_isomorphic Y 0) : f = 0 :=
zero_of_target_iso_zero f (nonempty.some i)
lemma mono_of_source_iso_zero {X Y : C} (f : X ⟶ Y) (i : X ≅ 0) : mono f :=
⟨λ Z g h w, by rw [zero_of_target_iso_zero g i, zero_of_target_iso_zero h i]⟩
lemma epi_of_target_iso_zero {X Y : C} (f : X ⟶ Y) (i : Y ≅ 0) : epi f :=
⟨λ Z g h w, by rw [zero_of_source_iso_zero g i, zero_of_source_iso_zero h i]⟩
/--
An object `X` has `𝟙 X = 0` if and only if it is isomorphic to the zero object.
Because `X ≅ 0` contains data (even if a subsingleton), we express this `↔` as an `≃`.
-/
def id_zero_equiv_iso_zero (X : C) : (𝟙 X = 0) ≃ (X ≅ 0) :=
{ to_fun := λ h, { hom := 0, inv := 0, },
inv_fun := λ i, zero_of_target_iso_zero (𝟙 X) i,
left_inv := by tidy,
right_inv := by tidy, }
@[simp]
lemma id_zero_equiv_iso_zero_apply_hom (X : C) (h : 𝟙 X = 0) :
((id_zero_equiv_iso_zero X) h).hom = 0 := rfl
@[simp]
lemma id_zero_equiv_iso_zero_apply_inv (X : C) (h : 𝟙 X = 0) :
((id_zero_equiv_iso_zero X) h).inv = 0 := rfl
/-- If `0 : X ⟶ Y` is an monomorphism, then `X ≅ 0`. -/
@[simps]
def iso_zero_of_mono_zero {X Y : C} (h : mono (0 : X ⟶ Y)) : X ≅ 0 :=
{ hom := 0,
inv := 0,
hom_inv_id' := (cancel_mono (0 : X ⟶ Y)).mp (by simp) }
/-- If `0 : X ⟶ Y` is an epimorphism, then `Y ≅ 0`. -/
@[simps]
def iso_zero_of_epi_zero {X Y : C} (h : epi (0 : X ⟶ Y)) : Y ≅ 0 :=
{ hom := 0,
inv := 0,
hom_inv_id' := (cancel_epi (0 : X ⟶ Y)).mp (by simp) }
/-- If a monomorphism out of `X` is zero, then `X ≅ 0`. -/
def iso_zero_of_mono_eq_zero {X Y : C} {f : X ⟶ Y} [mono f] (h : f = 0) : X ≅ 0 :=
by { unfreezingI { subst h, }, apply iso_zero_of_mono_zero ‹_›, }
/-- If an epimorphism in to `Y` is zero, then `Y ≅ 0`. -/
def iso_zero_of_epi_eq_zero {X Y : C} {f : X ⟶ Y} [epi f] (h : f = 0) : Y ≅ 0 :=
by { unfreezingI { subst h, }, apply iso_zero_of_epi_zero ‹_›, }
/-- If an object `X` is isomorphic to 0, there's no need to use choice to construct
an explicit isomorphism: the zero morphism suffices. -/
def iso_of_is_isomorphic_zero {X : C} (P : is_isomorphic X 0) : X ≅ 0 :=
{ hom := 0,
inv := 0,
hom_inv_id' :=
begin
casesI P,
rw ←P.hom_inv_id,
rw ←category.id_comp P.inv,
simp,
end,
inv_hom_id' := by simp, }
end
section is_iso
variables [has_zero_morphisms C]
/--
A zero morphism `0 : X ⟶ Y` is an isomorphism if and only if
the identities on both `X` and `Y` are zero.
-/
@[simps]
def is_iso_zero_equiv (X Y : C) : is_iso (0 : X ⟶ Y) ≃ (𝟙 X = 0 ∧ 𝟙 Y = 0) :=
{ to_fun := by { introsI i, rw ←is_iso.hom_inv_id (0 : X ⟶ Y),
rw ←is_iso.inv_hom_id (0 : X ⟶ Y), simp },
inv_fun := λ h, ⟨⟨(0 : Y ⟶ X), by tidy⟩⟩,
left_inv := by tidy,
right_inv := by tidy, }
/--
A zero morphism `0 : X ⟶ X` is an isomorphism if and only if
the identity on `X` is zero.
-/
def is_iso_zero_self_equiv (X : C) : is_iso (0 : X ⟶ X) ≃ (𝟙 X = 0) :=
by simpa using is_iso_zero_equiv X X
variables [has_zero_object C]
open_locale zero_object
/--
A zero morphism `0 : X ⟶ Y` is an isomorphism if and only if
`X` and `Y` are isomorphic to the zero object.
-/
def is_iso_zero_equiv_iso_zero (X Y : C) : is_iso (0 : X ⟶ Y) ≃ (X ≅ 0) × (Y ≅ 0) :=
begin
-- This is lame, because `prod` can't cope with `Prop`, so we can't use `equiv.prod_congr`.
refine (is_iso_zero_equiv X Y).trans _,
symmetry,
fsplit,
{ rintros ⟨eX, eY⟩, fsplit,
exact (id_zero_equiv_iso_zero X).symm eX,
exact (id_zero_equiv_iso_zero Y).symm eY, },
{ rintros ⟨hX, hY⟩, fsplit,
exact (id_zero_equiv_iso_zero X) hX,
exact (id_zero_equiv_iso_zero Y) hY, },
{ tidy, },
{ tidy, },
end
lemma is_iso_of_source_target_iso_zero {X Y : C} (f : X ⟶ Y) (i : X ≅ 0) (j : Y ≅ 0) : is_iso f :=
begin
rw zero_of_source_iso_zero f i,
exact (is_iso_zero_equiv_iso_zero _ _).inv_fun ⟨i, j⟩,
end
/--
A zero morphism `0 : X ⟶ X` is an isomorphism if and only if
`X` is isomorphic to the zero object.
-/
def is_iso_zero_self_equiv_iso_zero (X : C) : is_iso (0 : X ⟶ X) ≃ (X ≅ 0) :=
(is_iso_zero_equiv_iso_zero X X).trans subsingleton_prod_self_equiv
end is_iso
/-- If there are zero morphisms, any initial object is a zero object. -/
lemma has_zero_object_of_has_initial_object
[has_zero_morphisms C] [has_initial C] : has_zero_object C :=
begin
refine ⟨⟨⊥_ C, λ X, ⟨⟨⟨0⟩, by tidy⟩⟩, λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩⟩⟩,
calc
f = f ≫ 𝟙 _ : (category.comp_id _).symm
... = f ≫ 0 : by congr
... = 0 : has_zero_morphisms.comp_zero _ _
end
/-- If there are zero morphisms, any terminal object is a zero object. -/
lemma has_zero_object_of_has_terminal_object
[has_zero_morphisms C] [has_terminal C] : has_zero_object C :=
begin
refine ⟨⟨⊤_ C, λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨0⟩, by tidy⟩⟩⟩⟩,
calc
f = 𝟙 _ ≫ f : (category.id_comp _).symm
... = 0 ≫ f : by congr
... = 0 : zero_comp
end
section image
variable [has_zero_morphisms C]
lemma image_ι_comp_eq_zero {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} [has_image f]
[epi (factor_thru_image f)] (h : f ≫ g = 0) : image.ι f ≫ g = 0 :=
zero_of_epi_comp (factor_thru_image f) $ by simp [h]
lemma comp_factor_thru_image_eq_zero {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} [has_image g]
(h : f ≫ g = 0) : f ≫ factor_thru_image g = 0 :=
zero_of_comp_mono (image.ι g) $ by simp [h]
variables [has_zero_object C]
open_locale zero_object
/--
The zero morphism has a `mono_factorisation` through the zero object.
-/
@[simps]
def mono_factorisation_zero (X Y : C) : mono_factorisation (0 : X ⟶ Y) :=
{ I := 0, m := 0, e := 0, }
/--
The factorisation through the zero object is an image factorisation.
-/
def image_factorisation_zero (X Y : C) : image_factorisation (0 : X ⟶ Y) :=
{ F := mono_factorisation_zero X Y,
is_image := { lift := λ F', 0 } }
instance has_image_zero {X Y : C} : has_image (0 : X ⟶ Y) :=
has_image.mk $ image_factorisation_zero _ _
/-- The image of a zero morphism is the zero object. -/
def image_zero {X Y : C} : image (0 : X ⟶ Y) ≅ 0 :=
is_image.iso_ext (image.is_image (0 : X ⟶ Y)) (image_factorisation_zero X Y).is_image
/-- The image of a morphism which is equal to zero is the zero object. -/
def image_zero' {X Y : C} {f : X ⟶ Y} (h : f = 0) [has_image f] : image f ≅ 0 :=
image.eq_to_iso h ≪≫ image_zero
@[simp]
lemma image.ι_zero {X Y : C} [has_image (0 : X ⟶ Y)] : image.ι (0 : X ⟶ Y) = 0 :=
begin
rw ←image.lift_fac (mono_factorisation_zero X Y),
simp,
end
/--
If we know `f = 0`,
it requires a little work to conclude `image.ι f = 0`,
because `f = g` only implies `image f ≅ image g`.
-/
@[simp]
lemma image.ι_zero' [has_equalizers C] {X Y : C} {f : X ⟶ Y} (h : f = 0) [has_image f] :
image.ι f = 0 :=
by { rw image.eq_fac h, simp }
end image
/-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/
instance is_split_mono_sigma_ι {β : Type u'} [has_zero_morphisms C] (f : β → C)
[has_colimit (discrete.functor f)] (b : β) : is_split_mono (sigma.ι f b) := is_split_mono.mk'
{ retraction := sigma.desc $ pi.single b (𝟙 _) }
/-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/
instance is_split_epi_pi_π {β : Type u'} [has_zero_morphisms C] (f : β → C)
[has_limit (discrete.functor f)] (b : β) : is_split_epi (pi.π f b) := is_split_epi.mk'
{ section_ := pi.lift $ pi.single b (𝟙 _) }
/-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/
instance is_split_mono_coprod_inl [has_zero_morphisms C] {X Y : C} [has_colimit (pair X Y)] :
is_split_mono (coprod.inl : X ⟶ X ⨿ Y) := is_split_mono.mk'
{ retraction := coprod.desc (𝟙 X) 0, }
/-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/
instance is_split_mono_coprod_inr [has_zero_morphisms C] {X Y : C} [has_colimit (pair X Y)] :
is_split_mono (coprod.inr : Y ⟶ X ⨿ Y) := is_split_mono.mk'
{ retraction := coprod.desc 0 (𝟙 Y), }
/-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/
instance is_split_epi_prod_fst [has_zero_morphisms C] {X Y : C} [has_limit (pair X Y)] :
is_split_epi (prod.fst : X ⨯ Y ⟶ X) := is_split_epi.mk'
{ section_ := prod.lift (𝟙 X) 0, }
/-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/
instance is_split_epi_prod_snd [has_zero_morphisms C] {X Y : C} [has_limit (pair X Y)] :
is_split_epi (prod.snd : X ⨯ Y ⟶ Y) := is_split_epi.mk'
{ section_ := prod.lift 0 (𝟙 Y), }
end category_theory.limits
|
2f414596c78411591516b21883de8f43909a9eb8 | 2bafba05c98c1107866b39609d15e849a4ca2bb8 | /src/week_1/Part_C_functions.lean | 4979ef6968efc8e7a34bba81a383ff273e9b9285 | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/formalising-mathematics | b54c83c94b5c315024ff09997fcd6b303892a749 | 7cf1d51c27e2038d2804561d63c74711924044a1 | refs/heads/master | 1,651,267,046,302 | 1,638,888,459,000 | 1,638,888,459,000 | 331,592,375 | 284 | 24 | Apache-2.0 | 1,669,593,705,000 | 1,611,224,849,000 | Lean | UTF-8 | Lean | false | false | 3,138 | lean | import tactic
-- injective and surjective functions are already in Lean.
-- They are called `function.injective` and `function.surjective`.
-- It gets a bit boring typing `function.` a lot so we start
-- by opening the `function` namespace
open function
-- We now move into the `xena` namespace
namespace xena
-- let X, Y, Z be "types", i.e. sets, and let `f : X → Y` and `g : Y → Z`
-- be functions
variables {X Y Z : Type} {f : X → Y} {g : Y → Z}
-- let a,b,x be elements of X, let y be an element of Y and let z be an
-- element of Z
variables (a b x : X) (y : Y) (z : Z)
/-!
# Injective functions
-/
-- let's start by checking the definition of injective is
-- what we think it is.
lemma injective_def : injective f ↔ ∀ a b : X, f a = f b → a = b :=
begin
-- true by definition
refl
end
-- You can now `rw injective_def` to change `injective f` into its definition.
-- The identity function id : X → X is defined by id(x) = x. Let's check this
lemma id_def : id x = x :=
begin
-- true by definition
refl
end
-- you can now `rw id_def` to change `id x` into `x`
/-- The identity function is injective -/
lemma injective_id : injective (id : X → X) :=
begin
sorry,
end
-- function composition g ∘ f is satisfies (g ∘ f) (x) = g(f(x)). This
-- is true by definition. Let's check this
lemma comp_def : (g ∘ f) x = g (f x) :=
begin
-- true by definition
refl
end
/-- Composite of two injective functions is injective -/
lemma injective_comp (hf : injective f) (hg : injective g) : injective (g ∘ f) :=
begin
-- you could start with `rw injective_def at *` if you like.
-- In some sense it doesn't do anything, but it might make you happier.
sorry,
end
/-!
### Surjective functions
-/
-- Let's start by checking the definition of surjectivity is what we think it is
lemma surjective_def : surjective f ↔ ∀ y : Y, ∃ x : X, f x = y :=
begin
-- true by definition
refl
end
/-- The identity function is surjective -/
lemma surjective_id : surjective (id : X → X) :=
begin
-- you can start with `rw surjective_def` if you like.
sorry,
end
-- If you started with `rw surjective_def` -- try deleting it.
-- Probably your proof still works! This is because
-- `surjective_def` is true *by definition*. The proof is `refl`.
-- For this next one, the `have` tactic is helpful.
/-- Composite of two surjective functions is surjective -/
lemma surjective_comp (hf : surjective f) (hg : surjective g) : surjective (g ∘ f) :=
begin
sorry,
end
/-!
### Bijective functions
In Lean a function is defined to be bijective if it is injective and surjective.
Let's check this.
-/
lemma bijective_def : bijective f ↔ injective f ∧ surjective f :=
begin
-- true by definition
refl
end
-- You can now use the lemmas you've proved already to make these
-- proofs very short.
/-- The identity function is bijective. -/
lemma bijective_id : bijective (id : X → X) :=
begin
sorry,
end
/-- A composite of bijective functions is bijective. -/
lemma bijective_comp (hf : bijective f) (hg : bijective g) : bijective (g ∘ f) :=
begin
sorry,
end
end xena
|
8f68fd636a46a4324e3938d156de43b93aceccec | 1446f520c1db37e157b631385707cc28a17a595e | /tests/bench/binarytrees.lean | 10349c67edb9b3d019b1ff223e755831c75c5586 | [
"Apache-2.0"
] | permissive | bdbabiak/lean4 | cab06b8a2606d99a168dd279efdd404edb4e825a | 3f4d0d78b2ce3ef541cb643bbe21496bd6b057ac | refs/heads/master | 1,615,045,275,530 | 1,583,793,696,000 | 1,583,793,696,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,695 | lean | inductive Tree
| Nil
| Node (l r : Tree) : Tree
open Tree
instance : Inhabited Tree := ⟨Nil⟩
-- This Function has an extra argument to suppress the
-- common sub-expression elimination optimization
partial def make' : UInt32 -> UInt32 -> Tree
| n, d =>
if d = 0 then Node Nil Nil
else Node (make' n (d - 1)) (make' (n + 1) (d - 1))
-- build a tree
def make (d : UInt32) := make' d d
def check : Tree → UInt32
| Nil => 0
| Node l r => 1 + check l + check r
def minN := 4
def out (s) (n : Nat) (t : UInt32) : IO Unit :=
IO.println (s ++ " of depth " ++ toString n ++ "\t check: " ++ toString t)
-- allocate and check lots of trees
partial def sumT : UInt32 -> UInt32 -> UInt32 -> UInt32
| d, i, t =>
if i = 0 then t
else
let a := check (make d);
sumT d (i-1) (t + a)
-- generate many trees
partial def depth : Nat -> Nat -> List (Nat × Nat × Task UInt32)
| d, m => if d ≤ m then
let n := 2 ^ (m - d + minN);
(n, d, Task.mk (fun _ => sumT (UInt32.ofNat d) (UInt32.ofNat n) 0)) :: depth (d+2) m
else []
def main : List String → IO UInt32
| [s] => do
let n := s.toNat;
let maxN := Nat.max (minN + 2) n;
let stretchN := maxN + 1;
-- stretch memory tree
let c := check (make $ UInt32.ofNat stretchN);
out "stretch tree" stretchN c;
-- allocate a long lived tree
let long := make $ UInt32.ofNat maxN;
-- allocate, walk, and deallocate many bottom-up binary trees
let vs := (depth minN maxN); -- `using` (parList $ evalTuple3 r0 r0 rseq)
vs.mapM (fun ⟨m,d,i⟩ => out (toString m ++ "\t trees") d i.get);
-- confirm the the long-lived binary tree still exists
out "long lived tree" maxN (check long);
pure 0
| _ => pure 1
|
e6691ac3662e23437b131201a1dcfa85b630d11d | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/data/equiv/transfer_instance.lean | 920d81800b280c10ae62a7eaf377eae206abe3b0 | [
"Apache-2.0"
] | permissive | waynemunro/mathlib | e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552 | 065a70810b5480d584033f7bbf8e0409480c2118 | refs/heads/master | 1,693,417,182,397 | 1,634,644,781,000 | 1,634,644,781,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,341 | 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
-/
import data.equiv.basic
import algebra.field
import algebra.module
import algebra.algebra.basic
import algebra.group.type_tags
import ring_theory.ideal.local_ring
/-!
# Transfer algebraic structures across `equiv`s
In this file we prove theorems of the following form: if `β` has a
group structure and `α ≃ β` then `α` has a group structure, and
similarly for monoids, semigroups, rings, integral domains, fields and
so on.
Note that most of these constructions can also be obtained using the `transport` tactic.
## Tags
equiv, group, ring, field, module, algebra
-/
universes u v
variables {α : Type u} {β : Type v}
namespace equiv
section instances
variables (e : α ≃ β)
/-- Transfer `has_one` across an `equiv` -/
@[to_additive "Transfer `has_zero` across an `equiv`"]
protected def has_one [has_one β] : has_one α := ⟨e.symm 1⟩
@[to_additive]
lemma one_def [has_one β] : @has_one.one _ (equiv.has_one e) = e.symm 1 := rfl
/-- Transfer `has_mul` across an `equiv` -/
@[to_additive "Transfer `has_add` across an `equiv`"]
protected def has_mul [has_mul β] : has_mul α := ⟨λ x y, e.symm (e x * e y)⟩
@[to_additive]
lemma mul_def [has_mul β] (x y : α) :
@has_mul.mul _ (equiv.has_mul e) x y = e.symm (e x * e y) := rfl
/-- Transfer `has_div` across an `equiv` -/
@[to_additive "Transfer `has_sub` across an `equiv`"]
protected def has_div [has_div β] : has_div α := ⟨λ x y, e.symm (e x / e y)⟩
@[to_additive]
lemma div_def [has_div β] (x y : α) :
@has_div.div _ (equiv.has_div e) x y = e.symm (e x / e y) := rfl
/-- Transfer `has_inv` across an `equiv` -/
@[to_additive "Transfer `has_neg` across an `equiv`"]
protected def has_inv [has_inv β] : has_inv α := ⟨λ x, e.symm (e x)⁻¹⟩
@[to_additive]
lemma inv_def [has_inv β] (x : α) : @has_inv.inv _ (equiv.has_inv e) x = e.symm (e x)⁻¹ := rfl
/-- Transfer `has_scalar` across an `equiv` -/
protected def has_scalar {R : Type*} [has_scalar R β] : has_scalar R α :=
⟨λ r x, e.symm (r • (e x))⟩
lemma smul_def {R : Type*} [has_scalar R β] (r : R) (x : α) :
@has_scalar.smul _ _ (equiv.has_scalar e) r x = e.symm (r • (e x)) := rfl
/--
An equivalence `e : α ≃ β` gives a multiplicative equivalence `α ≃* β`
where the multiplicative structure on `α` is
the one obtained by transporting a multiplicative structure on `β` back along `e`.
-/
@[to_additive
"An equivalence `e : α ≃ β` gives a additive equivalence `α ≃+ β`
where the additive structure on `α` is
the one obtained by transporting an additive structure on `β` back along `e`."]
def mul_equiv (e : α ≃ β) [has_mul β] :
by { letI := equiv.has_mul e, exact α ≃* β } :=
begin
introsI,
exact
{ map_mul' := λ x y, by { apply e.symm.injective, simp, refl, },
..e }
end
@[simp, to_additive] lemma mul_equiv_apply (e : α ≃ β) [has_mul β] (a : α) :
(mul_equiv e) a = e a := rfl
@[to_additive] lemma mul_equiv_symm_apply (e : α ≃ β) [has_mul β] (b : β) :
by { letI := equiv.has_mul e, exact (mul_equiv e).symm b = e.symm b } :=
begin
intros, refl,
end
/--
An equivalence `e : α ≃ β` gives a ring equivalence `α ≃+* β`
where the ring structure on `α` is
the one obtained by transporting a ring structure on `β` back along `e`.
-/
def ring_equiv (e : α ≃ β) [has_add β] [has_mul β] :
by { letI := equiv.has_add e, letI := equiv.has_mul e, exact α ≃+* β } :=
begin
introsI,
exact
{ map_add' := λ x y, by { apply e.symm.injective, simp, refl, },
map_mul' := λ x y, by { apply e.symm.injective, simp, refl, },
..e }
end
@[simp] lemma ring_equiv_apply (e : α ≃ β) [has_add β] [has_mul β] (a : α) :
(ring_equiv e) a = e a := rfl
lemma ring_equiv_symm_apply (e : α ≃ β) [has_add β] [has_mul β] (b : β) :
by { letI := equiv.has_add e, letI := equiv.has_mul e, exact (ring_equiv e).symm b = e.symm b } :=
begin
intros, refl,
end
/-- Transfer `semigroup` across an `equiv` -/
@[to_additive "Transfer `add_semigroup` across an `equiv`"]
protected def semigroup [semigroup β] : semigroup α :=
let mul := e.has_mul in
by resetI; apply e.injective.semigroup _; intros; exact e.apply_symm_apply _
/-- Transfer `semigroup_with_zero` across an `equiv` -/
protected def semigroup_with_zero [semigroup_with_zero β] : semigroup_with_zero α :=
let mul := e.has_mul, zero := e.has_zero in
by resetI; apply e.injective.semigroup_with_zero _; intros; exact e.apply_symm_apply _
/-- Transfer `comm_semigroup` across an `equiv` -/
@[to_additive "Transfer `add_comm_semigroup` across an `equiv`"]
protected def comm_semigroup [comm_semigroup β] : comm_semigroup α :=
let mul := e.has_mul in
by resetI; apply e.injective.comm_semigroup _; intros; exact e.apply_symm_apply _
/-- Transfer `mul_zero_class` across an `equiv` -/
protected def mul_zero_class [mul_zero_class β] : mul_zero_class α :=
let zero := e.has_zero, mul := e.has_mul in
by resetI; apply e.injective.mul_zero_class _; intros; exact e.apply_symm_apply _
/-- Transfer `mul_one_class` across an `equiv` -/
@[to_additive "Transfer `add_zero_class` across an `equiv`"]
protected def mul_one_class [mul_one_class β] : mul_one_class α :=
let one := e.has_one, mul := e.has_mul in
by resetI; apply e.injective.mul_one_class _; intros; exact e.apply_symm_apply _
/-- Transfer `mul_zero_one_class` across an `equiv` -/
protected def mul_zero_one_class [mul_zero_one_class β] : mul_zero_one_class α :=
let zero := e.has_zero, one := e.has_one,mul := e.has_mul in
by resetI; apply e.injective.mul_zero_one_class _; intros; exact e.apply_symm_apply _
/-- Transfer `monoid` across an `equiv` -/
@[to_additive "Transfer `add_monoid` across an `equiv`"]
protected def monoid [monoid β] : monoid α :=
let one := e.has_one, mul := e.has_mul in
by resetI; apply e.injective.monoid _; intros; exact e.apply_symm_apply _
/-- Transfer `comm_monoid` across an `equiv` -/
@[to_additive "Transfer `add_comm_monoid` across an `equiv`"]
protected def comm_monoid [comm_monoid β] : comm_monoid α :=
let one := e.has_one, mul := e.has_mul in
by resetI; apply e.injective.comm_monoid _; intros; exact e.apply_symm_apply _
/-- Transfer `group` across an `equiv` -/
@[to_additive "Transfer `add_group` across an `equiv`"]
protected def group [group β] : group α :=
let one := e.has_one, mul := e.has_mul, inv := e.has_inv, div := e.has_div in
by resetI; apply e.injective.group _; intros; exact e.apply_symm_apply _
/-- Transfer `comm_group` across an `equiv` -/
@[to_additive "Transfer `add_comm_group` across an `equiv`"]
protected def comm_group [comm_group β] : comm_group α :=
let one := e.has_one, mul := e.has_mul, inv := e.has_inv, div := e.has_div in
by resetI; apply e.injective.comm_group _; intros; exact e.apply_symm_apply _
/-- Transfer `non_unital_non_assoc_semiring` across an `equiv` -/
protected def non_unital_non_assoc_semiring [non_unital_non_assoc_semiring β] :
non_unital_non_assoc_semiring α :=
let zero := e.has_zero, add := e.has_add, mul := e.has_mul in
by resetI; apply e.injective.non_unital_non_assoc_semiring _; intros; exact e.apply_symm_apply _
/-- Transfer `non_unital_semiring` across an `equiv` -/
protected def non_unital_semiring [non_unital_semiring β] : non_unital_semiring α :=
let zero := e.has_zero, add := e.has_add, mul := e.has_mul in
by resetI; apply e.injective.non_unital_semiring _; intros; exact e.apply_symm_apply _
/-- Transfer `non_assoc_semiring` across an `equiv` -/
protected def non_assoc_semiring [non_assoc_semiring β] : non_assoc_semiring α :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul in
by resetI; apply e.injective.non_assoc_semiring _; intros; exact e.apply_symm_apply _
/-- Transfer `semiring` across an `equiv` -/
protected def semiring [semiring β] : semiring α :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul in
by resetI; apply e.injective.semiring _; intros; exact e.apply_symm_apply _
/-- Transfer `comm_semiring` across an `equiv` -/
protected def comm_semiring [comm_semiring β] : comm_semiring α :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul in
by resetI; apply e.injective.comm_semiring _; intros; exact e.apply_symm_apply _
/-- Transfer `ring` across an `equiv` -/
protected def ring [ring β] : ring α :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul, neg := e.has_neg,
sub := e.has_sub in
by resetI; apply e.injective.ring _; intros; exact e.apply_symm_apply _
/-- Transfer `comm_ring` across an `equiv` -/
protected def comm_ring [comm_ring β] : comm_ring α :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul, neg := e.has_neg,
sub := e.has_sub in
by resetI; apply e.injective.comm_ring _; intros; exact e.apply_symm_apply _
/-- Transfer `nonzero` across an `equiv` -/
protected theorem nontrivial [nontrivial β] : nontrivial α :=
e.surjective.nontrivial
/-- Transfer `domain` across an `equiv` -/
protected theorem domain [ring α] [ring β] [domain β] (e : α ≃+* β) : domain α :=
function.injective.domain e.to_ring_hom e.injective
/-- Transfer `integral_domain` across an `equiv` -/
protected theorem integral_domain [comm_ring α] [comm_ring β] [integral_domain β] (e : α ≃+* β) :
integral_domain α :=
function.injective.integral_domain e.to_ring_hom e.injective
/-- Transfer `division_ring` across an `equiv` -/
protected def division_ring [division_ring β] : division_ring α :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul, neg := e.has_neg,
sub := e.has_sub, inv := e.has_inv, div := e.has_div in
by resetI; apply e.injective.division_ring _; intros; exact e.apply_symm_apply _
/-- Transfer `field` across an `equiv` -/
protected def field [field β] : field α :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul, neg := e.has_neg,
sub := e.has_sub, inv := e.has_inv, div := e.has_div in
by resetI; apply e.injective.field _; intros; exact e.apply_symm_apply _
section R
variables (R : Type*)
include R
section
variables [monoid R]
/-- Transfer `mul_action` across an `equiv` -/
protected def mul_action (e : α ≃ β) [mul_action R β] : mul_action R α :=
{ one_smul := by simp [smul_def],
mul_smul := by simp [smul_def, mul_smul],
..equiv.has_scalar e }
/-- Transfer `distrib_mul_action` across an `equiv` -/
protected def distrib_mul_action (e : α ≃ β) [add_comm_monoid β] :
begin
letI := equiv.add_comm_monoid e,
exact Π [distrib_mul_action R β], distrib_mul_action R α
end :=
begin
intros,
letI := equiv.add_comm_monoid e,
exact (
{ smul_zero := by simp [zero_def, smul_def],
smul_add := by simp [add_def, smul_def, smul_add],
..equiv.mul_action R e } : distrib_mul_action R α)
end
end
section
variables [semiring R]
/-- Transfer `module` across an `equiv` -/
protected def module (e : α ≃ β) [add_comm_monoid β] :
begin
letI := equiv.add_comm_monoid e,
exact Π [module R β], module R α
end :=
begin
introsI,
exact (
{ zero_smul := by simp [zero_def, smul_def],
add_smul := by simp [add_def, smul_def, add_smul],
..equiv.distrib_mul_action R e } : module R α)
end
/--
An equivalence `e : α ≃ β` gives a linear equivalence `α ≃ₗ[R] β`
where the `R`-module structure on `α` is
the one obtained by transporting an `R`-module structure on `β` back along `e`.
-/
def linear_equiv (e : α ≃ β) [add_comm_monoid β] [module R β] :
begin
letI := equiv.add_comm_monoid e,
letI := equiv.module R e,
exact α ≃ₗ[R] β
end :=
begin
introsI,
exact
{ map_smul' := λ r x, by { apply e.symm.injective, simp, refl, },
..equiv.add_equiv e }
end
end
section
variables [comm_semiring R]
/-- Transfer `algebra` across an `equiv` -/
protected def algebra (e : α ≃ β) [semiring β] :
begin
letI := equiv.semiring e,
exact Π [algebra R β], algebra R α
end :=
begin
introsI,
fapply ring_hom.to_algebra',
{ exact ((ring_equiv e).symm : β →+* α).comp (algebra_map R β), },
{ intros r x,
simp only [function.comp_app, ring_hom.coe_comp],
have p := ring_equiv_symm_apply e,
dsimp at p,
erw p, clear p,
apply (ring_equiv e).injective,
simp only [(ring_equiv e).map_mul],
simp [algebra.commutes], }
end
/--
An equivalence `e : α ≃ β` gives an algebra equivalence `α ≃ₐ[R] β`
where the `R`-algebra structure on `α` is
the one obtained by transporting an `R`-algebra structure on `β` back along `e`.
-/
def alg_equiv (e : α ≃ β) [semiring β] [algebra R β] :
begin
letI := equiv.semiring e,
letI := equiv.algebra R e,
exact α ≃ₐ[R] β
end :=
begin
introsI,
exact
{ commutes' := λ r, by { apply e.symm.injective, simp, refl, },
..equiv.ring_equiv e }
end
end
end R
end instances
end equiv
namespace ring_equiv
protected lemma local_ring {A B : Type*} [comm_ring A] [local_ring A] [comm_ring B] (e : A ≃+* B) :
local_ring B :=
begin
haveI := e.symm.to_equiv.nontrivial,
refine @local_of_surjective A B _ _ _ _ e e.to_equiv.surjective,
end
end ring_equiv
|
094112438cd7a94a70b9b1027eeb48a87df5b11d | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /src/Lean/Compiler/LCNF/Main.lean | c72feda8b9ecdd96bac1fd2276ff69d3cb77b023 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | tobiasgrosser/lean4 | ce0fd9cca0feba1100656679bf41f0bffdbabb71 | ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f | refs/heads/master | 1,673,103,412,948 | 1,664,930,501,000 | 1,664,930,501,000 | 186,870,185 | 0 | 0 | Apache-2.0 | 1,665,129,237,000 | 1,557,939,901,000 | Lean | UTF-8 | Lean | false | false | 4,018 | lean | /-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Compiler.Options
import Lean.Compiler.LCNF.PassManager
import Lean.Compiler.LCNF.Passes
import Lean.Compiler.LCNF.PrettyPrinter
import Lean.Compiler.LCNF.ToDecl
import Lean.Compiler.LCNF.Check
import Lean.Compiler.LCNF.PullLetDecls
import Lean.Compiler.LCNF.PhaseExt
import Lean.Compiler.LCNF.CSE
namespace Lean.Compiler.LCNF
/--
We do not generate code for `declName` if
- Its type is a proposition.
- Its type is a type former.
- It is tagged as `[macroInline]`.
- It is a type class instance.
Remark: we still generate code for declarations tagged as `[inline]`
and `[specialize]` since they can be partially applied.
-/
def shouldGenerateCode (declName : Name) : CoreM Bool := do
if (← isCompIrrelevant |>.run') then return false
let some info ← getDeclInfo? declName | return false
unless info.hasValue do return false
let env ← getEnv
if hasMacroInlineAttribute env declName then return false
if (← Meta.isMatcher declName) then return false
if isCasesOnRecursor env declName then return false
-- TODO: check if type class instance
return true
where
isCompIrrelevant : MetaM Bool := do
let info ← getConstInfo declName
Meta.isProp info.type <||> Meta.isTypeFormerType info.type
/--
A checkpoint in code generation to print all declarations in between
compiler passes in order to ease debugging.
The trace can be viewed with `set_option trace.Compiler.step true`.
-/
def checkpoint (stepName : Name) (decls : Array Decl) : CompilerM Unit := do
for decl in decls do
trace[Compiler.stat] "{decl.name} : {decl.size}"
withOptions (fun opts => opts.setBool `pp.motives.pi false) do
let clsName := `Compiler ++ stepName
if (← Lean.isTracingEnabledFor clsName) then
Lean.addTrace clsName m!"size: {decl.size}\n{← ppDecl' decl}"
if compiler.check.get (← getOptions) then
decl.check
if compiler.check.get (← getOptions) then
checkDeadLocalDecls decls
namespace PassManager
def run (declNames : Array Name) : CompilerM (Array Decl) := withAtLeastMaxRecDepth 8192 do
/-
Note: we need to increase the recursion depth because we currently do to save phase1
declarations in .olean files. Then, we have to recursively compile all dependencies,
and it often creates a very deep recursion.
Moreover, some declarations get very big during simplification.
-/
let declNames ← declNames.filterM (shouldGenerateCode ·)
if declNames.isEmpty then return #[]
let mut decls ← declNames.mapM toDecl
decls := markRecDecls decls
let manager ← getPassManager
for pass in manager.passes do
trace[Compiler] s!"Running pass: {pass.name}"
decls ← withPhase pass.phase <| pass.run decls
withPhase pass.phase <| checkpoint pass.name decls
if (← Lean.isTracingEnabledFor `Compiler.result) then
for decl in decls do
-- We display the declaration saved in the environment because the names have been normalized
let some decl' ← getDeclAt? decl.name .base | unreachable!
Lean.addTrace `Compiler.result m!"size: {decl.size}\n{← ppDecl' decl'}"
return decls
end PassManager
def compile (declNames : Array Name) : CoreM (Array Decl) :=
CompilerM.run <| PassManager.run declNames
def showDecl (phase : Phase) (declName : Name) : CoreM Format := do
let some decl ← getDeclAt? declName phase | return "<not-available>"
ppDecl' decl
@[export lean_lcnf_compile_decls]
def main (declNames : List Name) : CoreM Unit := do
profileitM Exception "compilation new" (← getOptions) do
CompilerM.run <| discard <| PassManager.run declNames.toArray
builtin_initialize
registerTraceClass `Compiler.init (inherited := true)
registerTraceClass `Compiler.test (inherited := true)
registerTraceClass `Compiler.result (inherited := true)
registerTraceClass `Compiler.jp
end Lean.Compiler.LCNF
|
72286437bb178effcb53ad380d37a2fc5b93fb8d | b2e508d02500f1512e1618150413e6be69d9db10 | /src/algebra/pi_instances.lean | eb917e9b5726c2dfc2b8a7fdeff2fba409f49f26 | [
"Apache-2.0"
] | permissive | callum-sutton/mathlib | c3788f90216e9cd43eeffcb9f8c9f959b3b01771 | afd623825a3ac6bfbcc675a9b023edad3f069e89 | refs/heads/master | 1,591,371,888,053 | 1,560,990,690,000 | 1,560,990,690,000 | 192,476,045 | 0 | 0 | Apache-2.0 | 1,568,941,843,000 | 1,560,837,965,000 | Lean | UTF-8 | Lean | false | false | 18,411 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Patrick Massot
Pi instances for algebraic structures.
-/
import order.basic
import algebra.module algebra.group
import data.finset
import ring_theory.subring
import tactic.pi_instances
namespace pi
universes u v w
variable {I : Type u} -- The indexing type
variable {f : I → Type v} -- The family of types already equiped with instances
variables (x y : Π i, f i) (i : I)
instance has_zero [∀ i, has_zero $ f i] : has_zero (Π i : I, f i) := ⟨λ i, 0⟩
@[simp] lemma zero_apply [∀ i, has_zero $ f i] : (0 : Π i, f i) i = 0 := rfl
instance has_one [∀ i, has_one $ f i] : has_one (Π i : I, f i) := ⟨λ i, 1⟩
@[simp] lemma one_apply [∀ i, has_one $ f i] : (1 : Π i, f i) i = 1 := rfl
attribute [to_additive pi.has_zero] pi.has_one
attribute [to_additive pi.zero_apply] pi.one_apply
instance has_add [∀ i, has_add $ f i] : has_add (Π i : I, f i) := ⟨λ x y, λ i, x i + y i⟩
@[simp] lemma add_apply [∀ i, has_add $ f i] : (x + y) i = x i + y i := rfl
instance has_mul [∀ i, has_mul $ f i] : has_mul (Π i : I, f i) := ⟨λ x y, λ i, x i * y i⟩
@[simp] lemma mul_apply [∀ i, has_mul $ f i] : (x * y) i = x i * y i := rfl
attribute [to_additive pi.has_add] pi.has_mul
attribute [to_additive pi.add_apply] pi.mul_apply
instance has_inv [∀ i, has_inv $ f i] : has_inv (Π i : I, f i) := ⟨λ x, λ i, (x i)⁻¹⟩
@[simp] lemma inv_apply [∀ i, has_inv $ f i] : x⁻¹ i = (x i)⁻¹ := rfl
instance has_neg [∀ i, has_neg $ f i] : has_neg (Π i : I, f i) := ⟨λ x, λ i, -(x i)⟩
@[simp] lemma neg_apply [∀ i, has_neg $ f i] : (-x) i = -x i := rfl
attribute [to_additive pi.has_neg] pi.has_inv
attribute [to_additive pi.neg_apply] pi.inv_apply
instance has_scalar {α : Type*} [∀ i, has_scalar α $ f i] : has_scalar α (Π i : I, f i) := ⟨λ s x, λ i, s • (x i)⟩
@[simp] lemma smul_apply {α : Type*} [∀ i, has_scalar α $ f i] (s : α) : (s • x) i = s • x i := rfl
instance semigroup [∀ i, semigroup $ f i] : semigroup (Π i : I, f i) := by pi_instance
instance comm_semigroup [∀ i, comm_semigroup $ f i] : comm_semigroup (Π i : I, f i) := by pi_instance
instance monoid [∀ i, monoid $ f i] : monoid (Π i : I, f i) := by pi_instance
instance comm_monoid [∀ i, comm_monoid $ f i] : comm_monoid (Π i : I, f i) := by pi_instance
instance group [∀ i, group $ f i] : group (Π i : I, f i) := by pi_instance
instance comm_group [∀ i, comm_group $ f i] : comm_group (Π i : I, f i) := by pi_instance
instance add_semigroup [∀ i, add_semigroup $ f i] : add_semigroup (Π i : I, f i) := by pi_instance
instance add_comm_semigroup [∀ i, add_comm_semigroup $ f i] : add_comm_semigroup (Π i : I, f i) := by pi_instance
instance add_monoid [∀ i, add_monoid $ f i] : add_monoid (Π i : I, f i) := by pi_instance
instance add_comm_monoid [∀ i, add_comm_monoid $ f i] : add_comm_monoid (Π i : I, f i) := by pi_instance
instance add_group [∀ i, add_group $ f i] : add_group (Π i : I, f i) := by pi_instance
instance add_comm_group [∀ i, add_comm_group $ f i] : add_comm_group (Π i : I, f i) := by pi_instance
instance ring [∀ i, ring $ f i] : ring (Π i : I, f i) := by pi_instance
instance comm_ring [∀ i, comm_ring $ f i] : comm_ring (Π i : I, f i) := by pi_instance
instance mul_action (α) {m : monoid α} [∀ i, mul_action α $ f i] : mul_action α (Π i : I, f i) :=
{ smul := λ c f i, c • f i,
mul_smul := λ r s f, funext $ λ i, mul_smul _ _ _,
one_smul := λ f, funext $ λ i, one_smul α _ }
instance distrib_mul_action (α) {m : monoid α} [∀ i, add_monoid $ f i] [∀ i, distrib_mul_action α $ f i] : distrib_mul_action α (Π i : I, f i) :=
{ smul_zero := λ c, funext $ λ i, smul_zero _,
smul_add := λ c f g, funext $ λ i, smul_add _ _ _,
..pi.mul_action _ }
variables (I f)
instance semimodule (α) {r : semiring α} [∀ i, add_comm_monoid $ f i] [∀ i, semimodule α $ f i] : semimodule α (Π i : I, f i) :=
{ add_smul := λ c f g, funext $ λ i, add_smul _ _ _,
zero_smul := λ f, funext $ λ i, zero_smul α _,
..pi.distrib_mul_action _ }
variables {I f}
instance module (α) {r : ring α} [∀ i, add_comm_group $ f i] [∀ i, module α $ f i] : module α (Π i : I, f i) := {..pi.semimodule I f α}
instance vector_space (α) {r : discrete_field α} [∀ i, add_comm_group $ f i] [∀ i, vector_space α $ f i] : vector_space α (Π i : I, f i) := {..pi.module α}
instance left_cancel_semigroup [∀ i, left_cancel_semigroup $ f i] : left_cancel_semigroup (Π i : I, f i) :=
by pi_instance
instance add_left_cancel_semigroup [∀ i, add_left_cancel_semigroup $ f i] : add_left_cancel_semigroup (Π i : I, f i) :=
by pi_instance
instance right_cancel_semigroup [∀ i, right_cancel_semigroup $ f i] : right_cancel_semigroup (Π i : I, f i) :=
by pi_instance
instance add_right_cancel_semigroup [∀ i, add_right_cancel_semigroup $ f i] : add_right_cancel_semigroup (Π i : I, f i) :=
by pi_instance
instance ordered_cancel_comm_monoid [∀ i, ordered_cancel_comm_monoid $ f i] : ordered_cancel_comm_monoid (Π i : I, f i) :=
by pi_instance
attribute [to_additive pi.add_semigroup] pi.semigroup
attribute [to_additive pi.add_comm_semigroup] pi.comm_semigroup
attribute [to_additive pi.add_monoid] pi.monoid
attribute [to_additive pi.add_comm_monoid] pi.comm_monoid
attribute [to_additive pi.add_group] pi.group
attribute [to_additive pi.add_comm_group] pi.comm_group
attribute [to_additive pi.add_left_cancel_semigroup] pi.left_cancel_semigroup
attribute [to_additive pi.add_right_cancel_semigroup] pi.right_cancel_semigroup
@[to_additive pi.list_sum_apply]
lemma list_prod_apply {α : Type*} {β : α → Type*} [∀a, monoid (β a)] (a : α) :
∀ (l : list (Πa, β a)), l.prod a = (l.map (λf:Πa, β a, f a)).prod
| [] := rfl
| (f :: l) := by simp [mul_apply f l.prod a, list_prod_apply l]
@[to_additive pi.multiset_sum_apply]
lemma multiset_prod_apply {α : Type*} {β : α → Type*} [∀a, comm_monoid (β a)] (a : α)
(s : multiset (Πa, β a)) : s.prod a = (s.map (λf:Πa, β a, f a)).prod :=
quotient.induction_on s $ assume l, begin simp [list_prod_apply a l] end
@[to_additive pi.finset_sum_apply]
lemma finset_prod_apply {α : Type*} {β : α → Type*} {γ} [∀a, comm_monoid (β a)] (a : α)
(s : finset γ) (g : γ → Πa, β a) : s.prod g a = s.prod (λc, g c a) :=
show (s.val.map g).prod a = (s.val.map (λc, g c a)).prod,
by rw [multiset_prod_apply, multiset.map_map]
instance is_ring_hom_pi
{α : Type u} {β : α → Type v} [R : Π a : α, ring (β a)]
{γ : Type w} [ring γ]
(f : Π a : α, γ → β a) [Rh : Π a : α, is_ring_hom (f a)] :
is_ring_hom (λ x b, f b x) :=
begin
split,
-- It's a pity that these can't be done using `simp` lemmas.
{ ext, rw [is_ring_hom.map_one (f x)], refl, },
{ intros x y, ext1 z, rw [is_ring_hom.map_mul (f z)], refl, },
{ intros x y, ext1 z, rw [is_ring_hom.map_add (f z)], refl, }
end
end pi
namespace prod
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {p q : α × β}
instance [has_add α] [has_add β] : has_add (α × β) :=
⟨λp q, (p.1 + q.1, p.2 + q.2)⟩
@[to_additive prod.has_add]
instance [has_mul α] [has_mul β] : has_mul (α × β) :=
⟨λp q, (p.1 * q.1, p.2 * q.2)⟩
@[simp, to_additive prod.fst_add]
lemma fst_mul [has_mul α] [has_mul β] : (p * q).1 = p.1 * q.1 := rfl
@[simp, to_additive prod.snd_add]
lemma snd_mul [has_mul α] [has_mul β] : (p * q).2 = p.2 * q.2 := rfl
@[simp, to_additive prod.mk_add_mk]
lemma mk_mul_mk [has_mul α] [has_mul β] (a₁ a₂ : α) (b₁ b₂ : β) :
(a₁, b₁) * (a₂, b₂) = (a₁ * a₂, b₁ * b₂) := rfl
instance [has_zero α] [has_zero β] : has_zero (α × β) := ⟨(0, 0)⟩
@[to_additive prod.has_zero]
instance [has_one α] [has_one β] : has_one (α × β) := ⟨(1, 1)⟩
@[simp, to_additive prod.fst_zero]
lemma fst_one [has_one α] [has_one β] : (1 : α × β).1 = 1 := rfl
@[simp, to_additive prod.snd_zero]
lemma snd_one [has_one α] [has_one β] : (1 : α × β).2 = 1 := rfl
@[to_additive prod.zero_eq_mk]
lemma one_eq_mk [has_one α] [has_one β] : (1 : α × β) = (1, 1) := rfl
instance [has_neg α] [has_neg β] : has_neg (α × β) := ⟨λp, (- p.1, - p.2)⟩
@[to_additive prod.has_neg]
instance [has_inv α] [has_inv β] : has_inv (α × β) := ⟨λp, (p.1⁻¹, p.2⁻¹)⟩
@[simp, to_additive prod.fst_neg]
lemma fst_inv [has_inv α] [has_inv β] : (p⁻¹).1 = (p.1)⁻¹ := rfl
@[simp, to_additive prod.snd_neg]
lemma snd_inv [has_inv α] [has_inv β] : (p⁻¹).2 = (p.2)⁻¹ := rfl
@[to_additive prod.neg_mk]
lemma inv_mk [has_inv α] [has_inv β] (a : α) (b : β) : (a, b)⁻¹ = (a⁻¹, b⁻¹) := rfl
instance [add_semigroup α] [add_semigroup β] : add_semigroup (α × β) :=
{ add_assoc := assume a b c, mk.inj_iff.mpr ⟨add_assoc _ _ _, add_assoc _ _ _⟩,
.. prod.has_add }
@[to_additive prod.add_semigroup]
instance [semigroup α] [semigroup β] : semigroup (α × β) :=
{ mul_assoc := assume a b c, mk.inj_iff.mpr ⟨mul_assoc _ _ _, mul_assoc _ _ _⟩,
.. prod.has_mul }
instance [add_monoid α] [add_monoid β] : add_monoid (α × β) :=
{ zero_add := assume a, prod.rec_on a $ λa b, mk.inj_iff.mpr ⟨zero_add _, zero_add _⟩,
add_zero := assume a, prod.rec_on a $ λa b, mk.inj_iff.mpr ⟨add_zero _, add_zero _⟩,
.. prod.add_semigroup, .. prod.has_zero }
@[to_additive prod.add_monoid]
instance [monoid α] [monoid β] : monoid (α × β) :=
{ one_mul := assume a, prod.rec_on a $ λa b, mk.inj_iff.mpr ⟨one_mul _, one_mul _⟩,
mul_one := assume a, prod.rec_on a $ λa b, mk.inj_iff.mpr ⟨mul_one _, mul_one _⟩,
.. prod.semigroup, .. prod.has_one }
instance [add_group α] [add_group β] : add_group (α × β) :=
{ add_left_neg := assume a, mk.inj_iff.mpr ⟨add_left_neg _, add_left_neg _⟩,
.. prod.add_monoid, .. prod.has_neg }
@[to_additive prod.add_group]
instance [group α] [group β] : group (α × β) :=
{ mul_left_inv := assume a, mk.inj_iff.mpr ⟨mul_left_inv _, mul_left_inv _⟩,
.. prod.monoid, .. prod.has_inv }
instance [add_comm_semigroup α] [add_comm_semigroup β] : add_comm_semigroup (α × β) :=
{ add_comm := assume a b, mk.inj_iff.mpr ⟨add_comm _ _, add_comm _ _⟩,
.. prod.add_semigroup }
@[to_additive prod.add_comm_semigroup]
instance [comm_semigroup α] [comm_semigroup β] : comm_semigroup (α × β) :=
{ mul_comm := assume a b, mk.inj_iff.mpr ⟨mul_comm _ _, mul_comm _ _⟩,
.. prod.semigroup }
instance [add_comm_monoid α] [add_comm_monoid β] : add_comm_monoid (α × β) :=
{ .. prod.add_comm_semigroup, .. prod.add_monoid }
@[to_additive prod.add_comm_monoid]
instance [comm_monoid α] [comm_monoid β] : comm_monoid (α × β) :=
{ .. prod.comm_semigroup, .. prod.monoid }
instance [add_comm_group α] [add_comm_group β] : add_comm_group (α × β) :=
{ .. prod.add_comm_semigroup, .. prod.add_group }
@[to_additive prod.add_comm_group]
instance [comm_group α] [comm_group β] : comm_group (α × β) :=
{ .. prod.comm_semigroup, .. prod.group }
@[to_additive fst.is_add_monoid_hom]
lemma fst.is_monoid_hom [monoid α] [monoid β] : is_monoid_hom (prod.fst : α × β → α) :=
{ map_mul := λ _ _, rfl, map_one := rfl }
@[to_additive snd.is_add_monoid_hom]
lemma snd.is_monoid_hom [monoid α] [monoid β] : is_monoid_hom (prod.snd : α × β → β) :=
{ map_mul := λ _ _, rfl, map_one := rfl }
@[to_additive fst.is_add_group_hom]
lemma fst.is_group_hom [group α] [group β] : is_group_hom (prod.fst : α × β → α) :=
{ map_mul := λ _ _, rfl }
@[to_additive snd.is_add_group_hom]
lemma snd.is_group_hom [group α] [group β] : is_group_hom (prod.snd : α × β → β) :=
{ map_mul := λ _ _, rfl }
attribute [instance] fst.is_monoid_hom fst.is_add_monoid_hom snd.is_monoid_hom snd.is_add_monoid_hom
fst.is_group_hom fst.is_add_group_hom snd.is_group_hom snd.is_add_group_hom
@[to_additive prod.fst_sum]
lemma fst_prod [comm_monoid α] [comm_monoid β] {t : finset γ} {f : γ → α × β} :
(t.prod f).1 = t.prod (λc, (f c).1) :=
(finset.prod_hom prod.fst).symm
@[to_additive prod.snd_sum]
lemma snd_prod [comm_monoid α] [comm_monoid β] {t : finset γ} {f : γ → α × β} :
(t.prod f).2 = t.prod (λc, (f c).2) :=
(finset.prod_hom prod.snd).symm
instance [semiring α] [semiring β] : semiring (α × β) :=
{ zero_mul := λ a, mk.inj_iff.mpr ⟨zero_mul _, zero_mul _⟩,
mul_zero := λ a, mk.inj_iff.mpr ⟨mul_zero _, mul_zero _⟩,
left_distrib := λ a b c, mk.inj_iff.mpr ⟨left_distrib _ _ _, left_distrib _ _ _⟩,
right_distrib := λ a b c, mk.inj_iff.mpr ⟨right_distrib _ _ _, right_distrib _ _ _⟩,
..prod.add_comm_monoid, ..prod.monoid }
instance [ring α] [ring β] : ring (α × β) :=
{ ..prod.add_comm_group, ..prod.semiring }
instance [comm_ring α] [comm_ring β] : comm_ring (α × β) :=
{ ..prod.ring, ..prod.comm_monoid }
instance [nonzero_comm_ring α] [comm_ring β] : nonzero_comm_ring (α × β) :=
{ zero_ne_one := mt (congr_arg prod.fst) zero_ne_one,
..prod.comm_ring }
instance fst.is_semiring_hom [semiring α] [semiring β] : is_semiring_hom (prod.fst : α × β → α) :=
by refine_struct {..}; simp
instance snd.is_semiring_hom [semiring α] [semiring β] : is_semiring_hom (prod.snd : α × β → β) :=
by refine_struct {..}; simp
instance fst.is_ring_hom [ring α] [ring β] : is_ring_hom (prod.fst : α × β → α) :=
by refine_struct {..}; simp
instance snd.is_ring_hom [ring α] [ring β] : is_ring_hom (prod.snd : α × β → β) :=
by refine_struct {..}; simp
/-- Left injection function for the inner product
From a vector space (and also group and module) perspective the product is the same as the sum of
two vector spaces. `inl` and `inr` provide the corresponding injection functions.
-/
def inl [has_zero β] (a : α) : α × β := (a, 0)
/-- Right injection function for the inner product -/
def inr [has_zero α] (b : β) : α × β := (0, b)
lemma injective_inl [has_zero β] : function.injective (inl : α → α × β) :=
assume x y h, (prod.mk.inj_iff.mp h).1
lemma injective_inr [has_zero α] : function.injective (inr : β → α × β) :=
assume x y h, (prod.mk.inj_iff.mp h).2
@[simp] lemma inl_eq_inl [has_zero β] {a₁ a₂ : α} : (inl a₁ : α × β) = inl a₂ ↔ a₁ = a₂ :=
iff.intro (assume h, injective_inl h) (assume h, h ▸ rfl)
@[simp] lemma inr_eq_inr [has_zero α] {b₁ b₂ : β} : (inr b₁ : α × β) = inr b₂ ↔ b₁ = b₂ :=
iff.intro (assume h, injective_inr h) (assume h, h ▸ rfl)
@[simp] lemma inl_eq_inr [has_zero α] [has_zero β] {a : α} {b : β} :
inl a = inr b ↔ a = 0 ∧ b = 0 :=
by constructor; simp [inl, inr] {contextual := tt}
@[simp] lemma inr_eq_inl [has_zero α] [has_zero β] {a : α} {b : β} :
inr b = inl a ↔ a = 0 ∧ b = 0 :=
by constructor; simp [inl, inr] {contextual := tt}
@[simp] lemma fst_inl [has_zero β] (a : α) : (inl a : α × β).1 = a := rfl
@[simp] lemma snd_inl [has_zero β] (a : α) : (inl a : α × β).2 = 0 := rfl
@[simp] lemma fst_inr [has_zero α] (b : β) : (inr b : α × β).1 = 0 := rfl
@[simp] lemma snd_inr [has_zero α] (b : β) : (inr b : α × β).2 = b := rfl
instance [has_scalar α β] [has_scalar α γ] : has_scalar α (β × γ) := ⟨λa p, (a • p.1, a • p.2)⟩
@[simp] theorem smul_fst [has_scalar α β] [has_scalar α γ]
(a : α) (x : β × γ) : (a • x).1 = a • x.1 := rfl
@[simp] theorem smul_snd [has_scalar α β] [has_scalar α γ]
(a : α) (x : β × γ) : (a • x).2 = a • x.2 := rfl
@[simp] theorem smul_mk [has_scalar α β] [has_scalar α γ]
(a : α) (b : β) (c : γ) : a • (b, c) = (a • b, a • c) := rfl
instance {r : semiring α} [add_comm_monoid β] [add_comm_monoid γ]
[semimodule α β] [semimodule α γ] : semimodule α (β × γ) :=
{ smul_add := assume a p₁ p₂, mk.inj_iff.mpr ⟨smul_add _ _ _, smul_add _ _ _⟩,
add_smul := assume a p₁ p₂, mk.inj_iff.mpr ⟨add_smul _ _ _, add_smul _ _ _⟩,
mul_smul := assume a₁ a₂ p, mk.inj_iff.mpr ⟨mul_smul _ _ _, mul_smul _ _ _⟩,
one_smul := assume ⟨b, c⟩, mk.inj_iff.mpr ⟨one_smul _ _, one_smul _ _⟩,
zero_smul := assume ⟨b, c⟩, mk.inj_iff.mpr ⟨zero_smul _ _, zero_smul _ _⟩,
smul_zero := assume a, mk.inj_iff.mpr ⟨smul_zero _, smul_zero _⟩,
.. prod.has_scalar }
instance {r : ring α} [add_comm_group β] [add_comm_group γ]
[module α β] [module α γ] : module α (β × γ) := {}
instance {r : discrete_field α} [add_comm_group β] [add_comm_group γ]
[vector_space α β] [vector_space α γ] : vector_space α (β × γ) := {}
section substructures
variables (s : set α) (t : set β)
@[to_additive prod.is_add_submonoid]
instance [monoid α] [monoid β] [is_submonoid s] [is_submonoid t] :
is_submonoid (s.prod t) :=
{ one_mem := by rw set.mem_prod; split; apply is_submonoid.one_mem,
mul_mem := by intros; rw set.mem_prod at *; split; apply is_submonoid.mul_mem; tauto }
@[to_additive prod.is_add_subgroup]
instance is_subgroup.prod [group α] [group β] [is_subgroup s] [is_subgroup t] :
is_subgroup (s.prod t) :=
{ inv_mem := by intros; rw set.mem_prod at *; split; apply is_subgroup.inv_mem; tauto,
.. prod.is_submonoid s t }
instance is_subring.prod [ring α] [ring β] [is_subring s] [is_subring t] :
is_subring (s.prod t) :=
{ .. prod.is_submonoid s t, .. prod.is_add_subgroup s t }
end substructures
end prod
namespace finset
@[to_additive finset.prod_mk_sum]
lemma prod_mk_prod {α β γ : Type*} [comm_monoid α] [comm_monoid β] (s : finset γ)
(f : γ → α) (g : γ → β) : (s.prod f, s.prod g) = s.prod (λ x, (f x, g x)) :=
by haveI := classical.dec_eq γ; exact
finset.induction_on s rfl (by simp [prod.ext_iff] {contextual := tt})
end finset
|
748b532e12e72b74af006d79c835eb670e1bc438 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/Lean3Lib/data/rbtree/find.lean | 6cbc1eaf443e588d2cd518d110f967093f12e37e | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,444 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.Lean3Lib.data.rbtree.basic
universes u
namespace Mathlib
namespace rbnode
theorem find.induction {α : Type u} {p : rbnode α → Prop} (lt : α → α → Prop) [DecidableRel lt] (t : rbnode α) (x : α) (h₁ : p leaf) (h₂ : ∀ (l : rbnode α) (y : α) (r : rbnode α), cmp_using lt x y = ordering.lt → p l → p (red_node l y r)) (h₃ : ∀ (l : rbnode α) (y : α) (r : rbnode α), cmp_using lt x y = ordering.eq → p (red_node l y r)) (h₄ : ∀ (l : rbnode α) (y : α) (r : rbnode α), cmp_using lt x y = ordering.gt → p r → p (red_node l y r)) (h₅ : ∀ (l : rbnode α) (y : α) (r : rbnode α), cmp_using lt x y = ordering.lt → p l → p (black_node l y r)) (h₆ : ∀ (l : rbnode α) (y : α) (r : rbnode α), cmp_using lt x y = ordering.eq → p (black_node l y r)) (h₇ : ∀ (l : rbnode α) (y : α) (r : rbnode α), cmp_using lt x y = ordering.gt → p r → p (black_node l y r)) : p t := sorry
theorem find_correct {α : Type u} {t : rbnode α} {lt : α → α → Prop} {x : α} [DecidableRel lt] [is_strict_weak_order α lt] {lo : Option α} {hi : Option α} (hs : is_searchable lt t lo hi) : mem lt x t ↔ ∃ (y : α), find lt t x = some y ∧ strict_weak_order.equiv x y := sorry
theorem mem_of_mem_exact {α : Type u} {lt : α → α → Prop} [is_irrefl α lt] {x : α} {t : rbnode α} : mem_exact x t → mem lt x t := sorry
theorem find_correct_exact {α : Type u} {t : rbnode α} {lt : α → α → Prop} {x : α} [DecidableRel lt] [is_strict_weak_order α lt] {lo : Option α} {hi : Option α} (hs : is_searchable lt t lo hi) : mem_exact x t ↔ find lt t x = some x := sorry
theorem eqv_of_find_some {α : Type u} {t : rbnode α} {lt : α → α → Prop} {x : α} {y : α} [DecidableRel lt] [is_strict_weak_order α lt] {lo : Option α} {hi : Option α} (hs : is_searchable lt t lo hi) (he : find lt t x = some y) : strict_weak_order.equiv x y := sorry
theorem find_eq_find_of_eqv {α : Type u} {lt : α → α → Prop} {a : α} {b : α} [DecidableRel lt] [is_strict_weak_order α lt] {t : rbnode α} {lo : Option α} {hi : Option α} (hs : is_searchable lt t lo hi) (heqv : strict_weak_order.equiv a b) : find lt t a = find lt t b := sorry
|
1e868e72218c23e52a149cb00e47f9a79098545c | 3aad12fe82645d2d3173fbedc2e5c2ba945a4d75 | /src/order/nursery.lean | 5a9ffa16c1ad957f68697f9050b1e69c2ae803f2 | [] | 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 | 277 | lean |
import order.bounded_lattice
theorem abs_le_abs {α : Type*} [decidable_linear_ordered_comm_group α] {a b : α}
(h₀ : a ≤ b) (h₁ : -a ≤ b) :
abs a ≤ abs b :=
calc abs a
≤ b : by { apply abs_le_of_le_of_neg_le; assumption }
... ≤ abs b : le_abs_self _
|
7d9ec549e3210a4ac975ce4d3d3201b8e557d2f3 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/measure_theory/measure/regular.lean | cd7069955c47a7d93020f11e7a2956c217e66a97 | [
"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 | 33,792 | lean | /-
Copyright (c) 2021 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris Van Doorn, Yury Kudryashov
-/
import measure_theory.constructions.borel_space
/-!
# Regular measures
A measure is `outer_regular` if the measure of any measurable set `A` is the infimum of `μ U` over
all open sets `U` containing `A`.
A measure is `regular` if it satisfies the following properties:
* it is finite on compact sets;
* it is outer regular;
* it is inner regular for open sets with respect to compacts sets: the measure of any open set `U`
is the supremum of `μ K` over all compact sets `K` contained in `U`.
A measure is `weakly_regular` if it satisfies the following properties:
* it is outer regular;
* it is inner regular for open sets with respect to closed sets: the measure of any open set `U`
is the supremum of `μ F` over all closed sets `F` contained in `U`.
In a Hausdorff topological space, regularity implies weak regularity. These three conditions are
registered as typeclasses for a measure `μ`, and this implication is recorded as an instance.
In order to avoid code duplication, we also define a measure `μ` to be `inner_regular` for sets
satisfying a predicate `q` with respect to sets satisfying a predicate `p` if for any set
`U ∈ {U | q U}` and a number `r < μ U` there exists `F ⊆ U` such that `p F` and `r < μ F`.
We prove that inner regularity for open sets with respect to compact sets or closed sets implies
inner regularity for all measurable sets of finite measure (with respect to
compact sets or closed sets respectively), and register some corollaries for (weakly) regular
measures.
Note that a similar statement for measurable sets of infinite mass can fail. For a counterexample,
consider the group `ℝ × ℝ` where the first factor has the discrete topology and the second one the
usual topology. It is a locally compact Hausdorff topological group, with Haar measure equal to
Lebesgue measure on each vertical fiber. The set `ℝ × {0}` has infinite measure (by outer
regularity), but any compact set it contains has zero measure (as it is finite).
Several authors require as a definition of regularity that all measurable sets are inner regular.
We have opted for the slightly weaker definition above as it holds for all Haar measures, it is
enough for essentially all applications, and it is equivalent to the other definition when the
measure is finite.
The interest of the notion of weak regularity is that it is enough for many applications, and it
is automatically satisfied by any finite measure on a metric space.
## Main definitions
* `measure_theory.measure.outer_regular μ`: a typeclass registering that a measure `μ` on a
topological space is outer regular.
* `measure_theory.measure.regular μ`: a typeclass registering that a measure `μ` on a topological
space is regular.
* `measure_theory.measure.weakly_regular μ`: a typeclass registering that a measure `μ` on a
topological space is weakly regular.
* `measure_theory.measure.inner_regular μ p q`: a non-typeclass predicate saying that a measure `μ`
is inner regular for sets satisfying `q` with respect to sets satisfying `p`.
## Main results
### Outer regular measures
* `set.measure_eq_infi_is_open` asserts that, when `μ` is outer regular, the measure of a
set is the infimum of the measure of open sets containing it.
* `set.exists_is_open_lt_of_lt'` asserts that, when `μ` is outer regular, for every set `s`
and `r > μ s` there exists an open superset `U ⊇ s` of measure less than `r`.
* push forward of an outer regular measure is outer regular, and scalar multiplication of a regular
measure by a finite number is outer regular.
* `measure_theory.measure.outer_regular.of_sigma_compact_space_of_is_locally_finite_measure`:
a locally finite measure on a `σ`-compact metric (or even pseudo emetric) space is outer regular.
### Weakly regular measures
* `is_open.measure_eq_supr_is_closed` asserts that the measure of an open set is the supremum of
the measure of closed sets it contains.
* `is_open.exists_lt_is_closed`: for an open set `U` and `r < μ U`, there exists a closed `F ⊆ U`
of measure greater than `r`;
* `measurable_set.measure_eq_supr_is_closed_of_ne_top` asserts that the measure of a measurable set
of finite measure is the supremum of the measure of closed sets it contains.
* `measurable_set.exists_lt_is_closed_of_ne_top` and `measurable_set.exists_is_closed_lt_add`:
a measurable set of finite measure can be approximated by a closed subset (stated as
`r < μ F` and `μ s < μ F + ε`, respectively).
* `measure_theory.measure.weakly_regular.of_pseudo_emetric_space_of_is_finite_measure` is an
instance registering that a finite measure on a metric space is weakly regular (in fact, a pseudo
emetric space is enough);
* `measure_theory.measure.weakly_regular.of_pseudo_emetric_sigma_compact_space_of_locally_finite`
is an instance registering that a locally finite measure on a `σ`-compact metric space (or even
a pseudo emetric space) is weakly regular.
### Regular measures
* `is_open.measure_eq_supr_is_compact` asserts that the measure of an open set is the supremum of
the measure of compact sets it contains.
* `is_open.exists_lt_is_compact`: for an open set `U` and `r < μ U`, there exists a compact `K ⊆ U`
of measure greater than `r`;
* `measurable_set.measure_eq_supr_is_compact_of_ne_top` asserts that the measure of a measurable set
of finite measure is the supremum of the measure of compact sets it contains.
* `measurable_set.exists_lt_is_compact_of_ne_top` and `measurable_set.exists_is_compact_lt_add`:
a measurable set of finite measure can be approximated by a compact subset (stated as
`r < μ K` and `μ s < μ K + ε`, respectively).
* `measure_theory.measure.regular.of_sigma_compact_space_of_is_locally_finite_measure` is an
instance registering that a locally finite measure on a `σ`-compact metric space is regular (in
fact, an emetric space is enough).
## Implementation notes
The main nontrivial statement is `measure_theory.measure.inner_regular.weakly_regular_of_finite`,
expressing that in a finite measure space, if every open set can be approximated from inside by
closed sets, then the measure is in fact weakly regular. To prove that we show that any measurable
set can be approximated from inside by closed sets and from outside by open sets. This statement is
proved by measurable induction, starting from open sets and checking that it is stable by taking
complements (this is the point of this condition, being symmetrical between inside and outside) and
countable disjoint unions.
Once this statement is proved, one deduces results for `σ`-finite measures from this statement, by
restricting them to finite measure sets (and proving that this restriction is weakly regular, using
again the same statement).
## References
[Halmos, Measure Theory, §52][halmos1950measure]. Note that Halmos uses an unusual definition of
Borel sets (for him, they are elements of the `σ`-algebra generated by compact sets!), so his
proofs or statements do not apply directly.
[Billingsley, Convergence of Probability Measures][billingsley1999]
-/
open set filter
open_locale ennreal topological_space nnreal big_operators
namespace measure_theory
namespace measure
/-- We say that a measure `μ` is *inner regular* with respect to predicates `p q : set α → Prop`,
if for every `U` such that `q U` and `r < μ U`, there exists a subset `K ⊆ U` satisfying `p K`
of measure greater than `r`.
This definition is used to prove some facts about regular and weakly regular measures without
repeating the proofs. -/
def inner_regular {α} {m : measurable_space α} (μ : measure α) (p q : set α → Prop) :=
∀ ⦃U⦄, q U → ∀ r < μ U, ∃ K ⊆ U, p K ∧ r < μ K
namespace inner_regular
variables {α : Type*} {m : measurable_space α} {μ : measure α} {p q : set α → Prop}
{U : set α} {ε : ℝ≥0∞}
lemma measure_eq_supr (H : inner_regular μ p q) (hU : q U) : μ U = ⨆ (K ⊆ U) (hK : p K), μ K :=
begin
refine le_antisymm (le_of_forall_lt $ λ r hr, _) (supr₂_le $ λ K hK, supr_le $ λ _, μ.mono hK),
simpa only [lt_supr_iff, exists_prop] using H hU r hr
end
lemma exists_subset_lt_add (H : inner_regular μ p q) (h0 : p ∅) (hU : q U) (hμU : μ U ≠ ∞)
(hε : ε ≠ 0) :
∃ K ⊆ U, p K ∧ μ U < μ K + ε :=
begin
cases eq_or_ne (μ U) 0 with h₀ h₀,
{ refine ⟨∅, empty_subset _, h0, _⟩,
rwa [measure_empty, h₀, zero_add, pos_iff_ne_zero] },
{ rcases H hU _ (ennreal.sub_lt_self hμU h₀ hε) with ⟨K, hKU, hKc, hrK⟩,
exact ⟨K, hKU, hKc, ennreal.lt_add_of_sub_lt_right (or.inl hμU) hrK⟩ }
end
lemma map {α β} [measurable_space α] [measurable_space β] {μ : measure α} {pa qa : set α → Prop}
(H : inner_regular μ pa qa) (f : α ≃ β) (hf : ae_measurable f μ)
{pb qb : set β → Prop} (hAB : ∀ U, qb U → qa (f ⁻¹' U)) (hAB' : ∀ K, pa K → pb (f '' K))
(hB₁ : ∀ K, pb K → measurable_set K) (hB₂ : ∀ U, qb U → measurable_set U) :
inner_regular (map f μ) pb qb :=
begin
intros U hU r hr,
rw [map_apply_of_ae_measurable hf (hB₂ _ hU)] at hr,
rcases H (hAB U hU) r hr with ⟨K, hKU, hKc, hK⟩,
refine ⟨f '' K, image_subset_iff.2 hKU, hAB' _ hKc, _⟩,
rwa [map_apply_of_ae_measurable hf (hB₁ _ $ hAB' _ hKc), f.preimage_image]
end
lemma smul (H : inner_regular μ p q) (c : ℝ≥0∞) : inner_regular (c • μ) p q :=
begin
intros U hU r hr,
rw [smul_apply, H.measure_eq_supr hU, smul_eq_mul] at hr,
simpa only [ennreal.mul_supr, lt_supr_iff, exists_prop] using hr
end
lemma trans {q' : set α → Prop} (H : inner_regular μ p q) (H' : inner_regular μ q q') :
inner_regular μ p q' :=
begin
intros U hU r hr,
rcases H' hU r hr with ⟨F, hFU, hqF, hF⟩, rcases H hqF _ hF with ⟨K, hKF, hpK, hrK⟩,
exact ⟨K, hKF.trans hFU, hpK, hrK⟩
end
end inner_regular
variables {α β : Type*} [measurable_space α] [topological_space α] {μ : measure α}
/-- A measure `μ` is outer regular if `μ(A) = inf {μ(U) | A ⊆ U open}` for a measurable set `A`.
This definition implies the same equality for any (not necessarily measurable) set, see
`set.measure_eq_infi_is_open`. -/
@[protect_proj] class outer_regular (μ : measure α) : Prop :=
(outer_regular : ∀ ⦃A : set α⦄, measurable_set A → ∀ r > μ A, ∃ U ⊇ A, is_open U ∧ μ U < r)
/-- A measure `μ` is regular if
- it is finite on all compact sets;
- it is outer regular: `μ(A) = inf {μ(U) | A ⊆ U open}` for `A` measurable;
- it is inner regular for open sets, using compact sets:
`μ(U) = sup {μ(K) | K ⊆ U compact}` for `U` open. -/
@[protect_proj] class regular (μ : measure α)
extends is_finite_measure_on_compacts μ, outer_regular μ : Prop :=
(inner_regular : inner_regular μ is_compact is_open)
/-- A measure `μ` is weakly regular if
- it is outer regular: `μ(A) = inf {μ(U) | A ⊆ U open}` for `A` measurable;
- it is inner regular for open sets, using closed sets:
`μ(U) = sup {μ(F) | F ⊆ U compact}` for `U` open. -/
@[protect_proj] class weakly_regular (μ : measure α) extends outer_regular μ : Prop :=
(inner_regular : inner_regular μ is_closed is_open)
/-- A regular measure is weakly regular. -/
@[priority 100] -- see Note [lower instance priority]
instance regular.weakly_regular [t2_space α] [regular μ] : weakly_regular μ :=
{ inner_regular := λ U hU r hr, let ⟨K, hKU, hcK, hK⟩ := regular.inner_regular hU r hr
in ⟨K, hKU, hcK.is_closed, hK⟩ }
namespace outer_regular
instance zero : outer_regular (0 : measure α) :=
⟨λ A hA r hr, ⟨univ, subset_univ A, is_open_univ, hr⟩⟩
/-- Given `r` larger than the measure of a set `A`, there exists an open superset of `A` with
measure less than `r`. -/
lemma _root_.set.exists_is_open_lt_of_lt [outer_regular μ] (A : set α) (r : ℝ≥0∞) (hr : μ A < r) :
∃ U ⊇ A, is_open U ∧ μ U < r :=
begin
rcases outer_regular.outer_regular (measurable_set_to_measurable μ A) r
(by rwa measure_to_measurable) with ⟨U, hAU, hUo, hU⟩,
exact ⟨U, (subset_to_measurable _ _).trans hAU, hUo, hU⟩
end
/-- For an outer regular measure, the measure of a set is the infimum of the measures of open sets
containing it. -/
lemma _root_.set.measure_eq_infi_is_open (A : set α) (μ : measure α) [outer_regular μ] :
μ A = (⨅ (U : set α) (h : A ⊆ U) (h2 : is_open U), μ U) :=
begin
refine le_antisymm (le_infi₂ $ λ s hs, le_infi $ λ h2s, μ.mono hs) _,
refine le_of_forall_lt' (λ r hr, _),
simpa only [infi_lt_iff, exists_prop] using A.exists_is_open_lt_of_lt r hr
end
lemma _root_.set.exists_is_open_lt_add [outer_regular μ] (A : set α) (hA : μ A ≠ ∞)
{ε : ℝ≥0∞} (hε : ε ≠ 0) :
∃ U ⊇ A, is_open U ∧ μ U < μ A + ε :=
A.exists_is_open_lt_of_lt _ (ennreal.lt_add_right hA hε)
lemma _root_.set.exists_is_open_le_add (A : set α) (μ : measure α) [outer_regular μ]
{ε : ℝ≥0∞} (hε : ε ≠ 0) :
∃ U ⊇ A, is_open U ∧ μ U ≤ μ A + ε :=
begin
rcases le_or_lt ∞ (μ A) with H|H,
{ exact ⟨univ, subset_univ _, is_open_univ,
by simp only [top_le_iff.mp H, ennreal.top_add, le_top]⟩ },
{ rcases A.exists_is_open_lt_add H.ne hε with ⟨U, AU, U_open, hU⟩,
exact ⟨U, AU, U_open, hU.le⟩ }
end
lemma _root_.measurable_set.exists_is_open_diff_lt [outer_regular μ] {A : set α}
(hA : measurable_set A) (hA' : μ A ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) :
∃ U ⊇ A, is_open U ∧ μ U < ∞ ∧ μ (U \ A) < ε :=
begin
rcases A.exists_is_open_lt_add hA' hε with ⟨U, hAU, hUo, hU⟩,
use [U, hAU, hUo, hU.trans_le le_top],
exact measure_diff_lt_of_lt_add hA hAU hA' hU,
end
protected lemma map [opens_measurable_space α] [measurable_space β] [topological_space β]
[borel_space β] (f : α ≃ₜ β) (μ : measure α) [outer_regular μ] :
(measure.map f μ).outer_regular :=
begin
refine ⟨λ A hA r hr, _⟩,
rw [map_apply f.measurable hA, ← f.image_symm] at hr,
rcases set.exists_is_open_lt_of_lt _ r hr with ⟨U, hAU, hUo, hU⟩,
have : is_open (f.symm ⁻¹' U), from hUo.preimage f.symm.continuous,
refine ⟨f.symm ⁻¹' U, image_subset_iff.1 hAU, this, _⟩,
rwa [map_apply f.measurable this.measurable_set, f.preimage_symm, f.preimage_image],
end
protected lemma smul (μ : measure α) [outer_regular μ] {x : ℝ≥0∞} (hx : x ≠ ∞) :
(x • μ).outer_regular :=
begin
rcases eq_or_ne x 0 with rfl|h0,
{ rw zero_smul, exact outer_regular.zero },
{ refine ⟨λ A hA r hr, _⟩,
rw [smul_apply, A.measure_eq_infi_is_open, smul_eq_mul] at hr,
simpa only [ennreal.mul_infi_of_ne h0 hx, gt_iff_lt, infi_lt_iff, exists_prop] using hr }
end
end outer_regular
/-- If a measure `μ` admits finite spanning open sets such that the restriction of `μ` to each set
is outer regular, then the original measure is outer regular as well. -/
protected lemma finite_spanning_sets_in.outer_regular [opens_measurable_space α] {μ : measure α}
(s : μ.finite_spanning_sets_in {U | is_open U ∧ outer_regular (μ.restrict U)}) :
outer_regular μ :=
begin
refine ⟨λ A hA r hr, _⟩,
have hm : ∀ n, measurable_set (s.set n), from λ n, (s.set_mem n).1.measurable_set,
haveI : ∀ n, outer_regular (μ.restrict (s.set n)) := λ n, (s.set_mem n).2,
-- Note that `A = ⋃ n, A ∩ disjointed s n`. We replace `A` with this sequence.
obtain ⟨A, hAm, hAs, hAd, rfl⟩ : ∃ A' : ℕ → set α, (∀ n, measurable_set (A' n)) ∧
(∀ n, A' n ⊆ s.set n) ∧ pairwise (disjoint on A') ∧ A = ⋃ n, A' n,
{ refine ⟨λ n, A ∩ disjointed s.set n, λ n, hA.inter (measurable_set.disjointed hm _),
λ n, (inter_subset_right _ _).trans (disjointed_subset _ _),
(disjoint_disjointed s.set).mono (λ k l hkl, hkl.mono inf_le_right inf_le_right), _⟩,
rw [← inter_Union, Union_disjointed, s.spanning, inter_univ] },
rcases ennreal.exists_pos_sum_of_encodable' (tsub_pos_iff_lt.2 hr).ne' ℕ with ⟨δ, δ0, hδε⟩,
rw [lt_tsub_iff_right, add_comm] at hδε,
have : ∀ n, ∃ U ⊇ A n, is_open U ∧ μ U < μ (A n) + δ n,
{ intro n,
have H₁ : ∀ t, μ.restrict (s.set n) t = μ (t ∩ s.set n), from λ t, restrict_apply' (hm n),
have Ht : μ.restrict (s.set n) (A n) ≠ ⊤,
{ rw H₁, exact ((measure_mono $ inter_subset_right _ _).trans_lt (s.finite n)).ne },
rcases (A n).exists_is_open_lt_add Ht (δ0 n).ne' with ⟨U, hAU, hUo, hU⟩,
rw [H₁, H₁, inter_eq_self_of_subset_left (hAs _)] at hU,
exact ⟨U ∩ s.set n, subset_inter hAU (hAs _), hUo.inter (s.set_mem n).1, hU⟩ },
choose U hAU hUo hU,
refine ⟨⋃ n, U n, Union_mono hAU, is_open_Union hUo, _⟩,
calc μ (⋃ n, U n) ≤ ∑' n, μ (U n) : measure_Union_le _
... ≤ ∑' n, (μ (A n) + δ n) : ennreal.tsum_le_tsum (λ n, (hU n).le)
... = ∑' n, μ (A n) + ∑' n, δ n : ennreal.tsum_add
... = μ (⋃ n, A n) + ∑' n, δ n : congr_arg2 (+) (measure_Union hAd hAm).symm rfl
... < r : hδε
end
namespace inner_regular
variables {p q : set α → Prop} {U s : set α} {ε r : ℝ≥0∞}
/-- If a measure is inner regular (using closed or compact sets), then every measurable set of
finite measure can by approximated by a (closed or compact) subset. -/
lemma measurable_set_of_open [outer_regular μ]
(H : inner_regular μ p is_open) (h0 : p ∅) (hd : ∀ ⦃s U⦄, p s → is_open U → p (s \ U)) :
inner_regular μ p (λ s, measurable_set s ∧ μ s ≠ ∞) :=
begin
rintros s ⟨hs, hμs⟩ r hr,
obtain ⟨ε, hε, hεs, rfl⟩ : ∃ ε ≠ 0, ε + ε ≤ μ s ∧ r = μ s - (ε + ε),
{ use (μ s - r) / 2, simp [*, hr.le, ennreal.add_halves, ennreal.sub_sub_cancel, le_add_right] },
rcases hs.exists_is_open_diff_lt hμs hε with ⟨U, hsU, hUo, hUt, hμU⟩,
rcases (U \ s).exists_is_open_lt_of_lt _ hμU with ⟨U', hsU', hU'o, hμU'⟩,
replace hsU' := diff_subset_comm.1 hsU',
rcases H.exists_subset_lt_add h0 hUo hUt.ne hε with ⟨K, hKU, hKc, hKr⟩,
refine ⟨K \ U', λ x hx, hsU' ⟨hKU hx.1, hx.2⟩, hd hKc hU'o, ennreal.sub_lt_of_lt_add hεs _⟩,
calc μ s ≤ μ U : μ.mono hsU
... < μ K + ε : hKr
... ≤ μ (K \ U') + μ U' + ε :
add_le_add_right (tsub_le_iff_right.1 le_measure_diff) _
... ≤ μ (K \ U') + ε + ε : by { mono*, exacts [hμU'.le, le_rfl] }
... = μ (K \ U') + (ε + ε) : add_assoc _ _ _
end
open finset
/-- In a finite measure space, assume that any open set can be approximated from inside by closed
sets. Then the measure is weakly regular. -/
lemma weakly_regular_of_finite [borel_space α] (μ : measure α) [is_finite_measure μ]
(H : inner_regular μ is_closed is_open) : weakly_regular μ :=
begin
have hfin : ∀ {s}, μ s ≠ ⊤ := measure_ne_top μ,
suffices : ∀ s, measurable_set s → ∀ ε ≠ 0,
∃ (F ⊆ s) (U ⊇ s), is_closed F ∧ is_open U ∧ μ s ≤ μ F + ε ∧ μ U ≤ μ s + ε,
{ refine { outer_regular := λ s hs r hr, _, inner_regular := H },
rcases exists_between hr with ⟨r', hsr', hr'r⟩,
rcases this s hs _ (tsub_pos_iff_lt.2 hsr').ne' with ⟨-, -, U, hsU, -, hUo, -, H⟩,
refine ⟨U, hsU, hUo, _⟩,
rw [add_tsub_cancel_of_le hsr'.le] at H, exact H.trans_lt hr'r },
refine measurable_set.induction_on_open _ _ _,
/- The proof is by measurable induction: we should check that the property is true for the empty
set, for open sets, and is stable by taking the complement and by taking countable disjoint
unions. The point of the property we are proving is that it is stable by taking complements
(exchanging the roles of closed and open sets and thanks to the finiteness of the measure). -/
-- check for open set
{ intros U hU ε hε,
rcases H.exists_subset_lt_add is_closed_empty hU hfin hε with ⟨F, hsF, hFc, hF⟩,
exact ⟨F, hsF, U, subset.rfl, hFc, hU, hF.le, le_self_add⟩ },
-- check for complements
{ rintros s hs H ε hε,
rcases H ε hε with ⟨F, hFs, U, hsU, hFc, hUo, hF, hU⟩,
refine ⟨Uᶜ, compl_subset_compl.2 hsU, Fᶜ, compl_subset_compl.2 hFs,
hUo.is_closed_compl, hFc.is_open_compl, _⟩,
simp only [measure_compl_le_add_iff, *, hUo.measurable_set, hFc.measurable_set, true_and] },
-- check for disjoint unions
{ intros s hsd hsm H ε ε0, have ε0' : ε / 2 ≠ 0, from (ennreal.half_pos ε0).ne',
rcases ennreal.exists_pos_sum_of_encodable' ε0' ℕ with ⟨δ, δ0, hδε⟩,
choose F hFs U hsU hFc hUo hF hU using λ n, H n (δ n) (δ0 n).ne',
-- the approximating closed set is constructed by considering finitely many sets `s i`, which
-- cover all the measure up to `ε/2`, approximating each of these by a closed set `F i`, and
-- taking the union of these (finitely many) `F i`.
have : tendsto (λ t, ∑ k in t, μ (s k) + ε / 2) at_top (𝓝 $ μ (⋃ n, s n) + ε / 2),
{ rw measure_Union hsd hsm, exact tendsto.add ennreal.summable.has_sum tendsto_const_nhds },
rcases (this.eventually $ lt_mem_nhds $ ennreal.lt_add_right hfin ε0').exists with ⟨t, ht⟩,
-- the approximating open set is constructed by taking for each `s n` an approximating open set
-- `U n` with measure at most `μ (s n) + δ n` for a summable `δ`, and taking the union of these.
refine ⟨⋃ k ∈ t, F k, Union_mono $ λ k, Union_subset $ λ _, hFs _,
⋃ n, U n, Union_mono hsU, is_closed_bUnion t.finite_to_set $ λ k _, hFc k,
is_open_Union hUo, ht.le.trans _, _⟩,
{ calc ∑ k in t, μ (s k) + ε / 2 ≤ ∑ k in t, μ (F k) + ∑ k in t, δ k + ε / 2 :
by { rw ← sum_add_distrib, exact add_le_add_right (sum_le_sum $ λ k hk, hF k) _ }
... ≤ ∑ k in t, μ (F k) + ε / 2 + ε / 2 :
add_le_add_right (add_le_add_left ((ennreal.sum_le_tsum _).trans hδε.le) _) _
... = μ (⋃ k ∈ t, F k) + ε : _,
rw [measure_bUnion_finset, add_assoc, ennreal.add_halves],
exacts [λ k _ n _ hkn, (hsd k n hkn).mono (hFs k) (hFs n), λ k hk, (hFc k).measurable_set] },
{ calc μ (⋃ n, U n) ≤ ∑' n, μ (U n) : measure_Union_le _
... ≤ ∑' n, (μ (s n) + δ n) : ennreal.tsum_le_tsum hU
... = μ (⋃ n, s n) + ∑' n, δ n : by rw [measure_Union hsd hsm, ennreal.tsum_add]
... ≤ μ (⋃ n, s n) + ε : add_le_add_left (hδε.le.trans ennreal.half_le_self) _ } }
end
/-- In a metric space (or even a pseudo emetric space), an open set can be approximated from inside
by closed sets. -/
lemma of_pseudo_emetric_space {X : Type*} [pseudo_emetric_space X]
[measurable_space X] (μ : measure X) :
inner_regular μ is_closed is_open :=
begin
intros U hU r hr,
rcases hU.exists_Union_is_closed with ⟨F, F_closed, -, rfl, F_mono⟩,
rw measure_Union_eq_supr F_mono.directed_le at hr,
rcases lt_supr_iff.1 hr with ⟨n, hn⟩,
exact ⟨F n, subset_Union _ _, F_closed n, hn⟩
end
/-- In a `σ`-compact space, any closed set can be approximated by a compact subset. -/
lemma is_compact_is_closed {X : Type*} [topological_space X]
[sigma_compact_space X] [measurable_space X] (μ : measure X) :
inner_regular μ is_compact is_closed :=
begin
intros F hF r hr,
set B : ℕ → set X := compact_covering X,
have hBc : ∀ n, is_compact (F ∩ B n), from λ n, (is_compact_compact_covering X n).inter_left hF,
have hBU : (⋃ n, F ∩ B n) = F, by rw [← inter_Union, Union_compact_covering, set.inter_univ],
have : μ F = ⨆ n, μ (F ∩ B n),
{ rw [← measure_Union_eq_supr, hBU],
exact monotone.directed_le
(λ m n h, inter_subset_inter_right _ (compact_covering_subset _ h)) },
rw this at hr, rcases lt_supr_iff.1 hr with ⟨n, hn⟩,
exact ⟨_, inter_subset_left _ _, hBc n, hn⟩
end
end inner_regular
namespace regular
instance zero : regular (0 : measure α) :=
⟨λ U hU r hr, ⟨∅, empty_subset _, is_compact_empty, hr⟩⟩
/-- If `μ` is a regular measure, then any open set can be approximated by a compact subset. -/
lemma _root_.is_open.exists_lt_is_compact [regular μ] ⦃U : set α⦄ (hU : is_open U)
{r : ℝ≥0∞} (hr : r < μ U) :
∃ K ⊆ U, is_compact K ∧ r < μ K :=
regular.inner_regular hU r hr
/-- The measure of an open set is the supremum of the measures of compact sets it contains. -/
lemma _root_.is_open.measure_eq_supr_is_compact ⦃U : set α⦄ (hU : is_open U)
(μ : measure α) [regular μ] :
μ U = (⨆ (K : set α) (h : K ⊆ U) (h2 : is_compact K), μ K) :=
regular.inner_regular.measure_eq_supr hU
lemma exists_compact_not_null [regular μ] : (∃ K, is_compact K ∧ μ K ≠ 0) ↔ μ ≠ 0 :=
by simp_rw [ne.def, ← measure_univ_eq_zero, is_open_univ.measure_eq_supr_is_compact,
ennreal.supr_eq_zero, not_forall, exists_prop, subset_univ, true_and]
/-- If `μ` is a regular measure, then any measurable set of finite measure can be approximated by a
compact subset. See also `measurable_set.exists_is_compact_lt_add` and
`measurable_set.exists_lt_is_compact_of_ne_top`. -/
lemma inner_regular_measurable [regular μ] :
inner_regular μ is_compact (λ s, measurable_set s ∧ μ s ≠ ∞) :=
regular.inner_regular.measurable_set_of_open is_compact_empty (λ _ _, is_compact.diff)
/-- If `μ` is a regular measure, then any measurable set of finite measure can be approximated by a
compact subset. See also `measurable_set.exists_lt_is_compact_of_ne_top`. -/
lemma _root_.measurable_set.exists_is_compact_lt_add
[regular μ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : μ A ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) :
∃ K ⊆ A, is_compact K ∧ μ A < μ K + ε :=
regular.inner_regular_measurable.exists_subset_lt_add is_compact_empty ⟨hA, h'A⟩ h'A hε
/-- If `μ` is a regular measure, then any measurable set of finite measure can be approximated by a
compact subset. See also `measurable_set.exists_is_compact_lt_add` and
`measurable_set.exists_lt_is_compact_of_ne_top`. -/
lemma _root_.measurable_set.exists_is_compact_diff_lt [opens_measurable_space α] [t2_space α]
[regular μ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : μ A ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) :
∃ K ⊆ A, is_compact K ∧ μ (A \ K) < ε :=
begin
rcases hA.exists_is_compact_lt_add h'A hε with ⟨K, hKA, hKc, hK⟩,
exact ⟨K, hKA, hKc, measure_diff_lt_of_lt_add hKc.measurable_set hKA
(ne_top_of_le_ne_top h'A $ measure_mono hKA) hK⟩
end
/-- If `μ` is a regular measure, then any measurable set of finite measure can be approximated by a
compact subset. See also `measurable_set.exists_is_compact_lt_add`. -/
lemma _root_.measurable_set.exists_lt_is_compact_of_ne_top [regular μ] ⦃A : set α⦄
(hA : measurable_set A) (h'A : μ A ≠ ∞) {r : ℝ≥0∞} (hr : r < μ A) :
∃ K ⊆ A, is_compact K ∧ r < μ K :=
regular.inner_regular_measurable ⟨hA, h'A⟩ _ hr
/-- Given a regular measure, any measurable set of finite mass can be approximated from
inside by compact sets. -/
lemma _root_.measurable_set.measure_eq_supr_is_compact_of_ne_top [regular μ]
⦃A : set α⦄ (hA : measurable_set A) (h'A : μ A ≠ ∞) :
μ A = (⨆ (K ⊆ A) (h : is_compact K), μ K) :=
regular.inner_regular_measurable.measure_eq_supr ⟨hA, h'A⟩
protected lemma map [opens_measurable_space α] [measurable_space β] [topological_space β]
[t2_space β] [borel_space β] [regular μ] (f : α ≃ₜ β) :
(measure.map f μ).regular :=
begin
haveI := outer_regular.map f μ,
haveI := is_finite_measure_on_compacts.map μ f,
exact ⟨regular.inner_regular.map f.to_equiv f.measurable.ae_measurable
(λ U hU, hU.preimage f.continuous) (λ K hK, hK.image f.continuous)
(λ K hK, hK.measurable_set) (λ U hU, hU.measurable_set)⟩
end
protected lemma smul [regular μ] {x : ℝ≥0∞} (hx : x ≠ ∞) :
(x • μ).regular :=
begin
haveI := outer_regular.smul μ hx,
haveI := is_finite_measure_on_compacts.smul μ hx,
exact ⟨regular.inner_regular.smul x⟩
end
/-- A regular measure in a σ-compact space is σ-finite. -/
@[priority 100] -- see Note [lower instance priority]
instance sigma_finite [sigma_compact_space α] [regular μ] : sigma_finite μ :=
⟨⟨{ set := compact_covering α,
set_mem := λ n, trivial,
finite := λ n, (is_compact_compact_covering α n).measure_lt_top,
spanning := Union_compact_covering α }⟩⟩
end regular
namespace weakly_regular
/-- If `μ` is a weakly regular measure, then any open set can be approximated by a closed subset. -/
lemma _root_.is_open.exists_lt_is_closed [weakly_regular μ] ⦃U : set α⦄ (hU : is_open U)
{r : ℝ≥0∞} (hr : r < μ U) :
∃ F ⊆ U, is_closed F ∧ r < μ F :=
weakly_regular.inner_regular hU r hr
/-- If `μ` is a weakly regular measure, then any open set can be approximated by a closed subset. -/
lemma _root_.is_open.measure_eq_supr_is_closed ⦃U : set α⦄ (hU : is_open U)
(μ : measure α) [weakly_regular μ] :
μ U = (⨆ (F ⊆ U) (h : is_closed F), μ F) :=
weakly_regular.inner_regular.measure_eq_supr hU
lemma inner_regular_measurable [weakly_regular μ] :
inner_regular μ is_closed (λ s, measurable_set s ∧ μ s ≠ ∞) :=
weakly_regular.inner_regular.measurable_set_of_open is_closed_empty
(λ _ _ h₁ h₂, h₁.inter h₂.is_closed_compl)
/-- If `s` is a measurable set, a weakly regular measure `μ` is finite on `s`, and `ε` is a positive
number, then there exist a closed set `K ⊆ s` such that `μ s < μ K + ε`. -/
lemma _root_.measurable_set.exists_is_closed_lt_add [weakly_regular μ] {s : set α}
(hs : measurable_set s) (hμs : μ s ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) :
∃ K ⊆ s, is_closed K ∧ μ s < μ K + ε :=
inner_regular_measurable.exists_subset_lt_add is_closed_empty ⟨hs, hμs⟩ hμs hε
lemma _root_.measurable_set.exists_is_closed_diff_lt [opens_measurable_space α]
[weakly_regular μ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : μ A ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) :
∃ F ⊆ A, is_closed F ∧ μ (A \ F) < ε :=
begin
rcases hA.exists_is_closed_lt_add h'A hε with ⟨F, hFA, hFc, hF⟩,
exact ⟨F, hFA, hFc, measure_diff_lt_of_lt_add hFc.measurable_set hFA
(ne_top_of_le_ne_top h'A $ measure_mono hFA) hF⟩
end
/-- Given a weakly regular measure, any measurable set of finite mass can be approximated from
inside by closed sets. -/
lemma _root_.measurable_set.exists_lt_is_closed_of_ne_top [weakly_regular μ]
⦃A : set α⦄ (hA : measurable_set A) (h'A : μ A ≠ ∞) {r : ℝ≥0∞} (hr : r < μ A) :
∃ K ⊆ A, is_closed K ∧ r < μ K :=
inner_regular_measurable ⟨hA, h'A⟩ _ hr
/-- Given a weakly regular measure, any measurable set of finite mass can be approximated from
inside by closed sets. -/
lemma _root_.measurable_set.measure_eq_supr_is_closed_of_ne_top [weakly_regular μ] ⦃A : set α⦄
(hA : measurable_set A) (h'A : μ A ≠ ∞) :
μ A = (⨆ (K ⊆ A) (h : is_closed K), μ K) :=
inner_regular_measurable.measure_eq_supr ⟨hA, h'A⟩
/-- The restriction of a weakly regular measure to a measurable set of finite measure is
weakly regular. -/
lemma restrict_of_measurable_set [borel_space α] [weakly_regular μ] (A : set α)
(hA : measurable_set A) (h'A : μ A ≠ ∞) : weakly_regular (μ.restrict A) :=
begin
haveI : fact (μ A < ∞) := ⟨h'A.lt_top⟩,
refine inner_regular.weakly_regular_of_finite _ (λ V V_open, _),
simp only [restrict_apply' hA], intros r hr,
have : μ (V ∩ A) ≠ ∞, from ne_top_of_le_ne_top h'A (measure_mono $ inter_subset_right _ _),
rcases (V_open.measurable_set.inter hA).exists_lt_is_closed_of_ne_top this hr
with ⟨F, hFVA, hFc, hF⟩,
refine ⟨F, hFVA.trans (inter_subset_left _ _), hFc, _⟩,
rwa inter_eq_self_of_subset_left (hFVA.trans $ inter_subset_right _ _)
end
/-- Any finite measure on a metric space (or even a pseudo emetric space) is weakly regular. -/
@[priority 100] -- see Note [lower instance priority]
instance of_pseudo_emetric_space_of_is_finite_measure {X : Type*} [pseudo_emetric_space X]
[measurable_space X] [borel_space X] (μ : measure X) [is_finite_measure μ] :
weakly_regular μ :=
(inner_regular.of_pseudo_emetric_space μ).weakly_regular_of_finite μ
/-- Any locally finite measure on a `σ`-compact metric space (or even a pseudo emetric space) is
weakly regular. -/
@[priority 100] -- see Note [lower instance priority]
instance of_pseudo_emetric_sigma_compact_space_of_locally_finite {X : Type*}
[pseudo_emetric_space X] [sigma_compact_space X] [measurable_space X] [borel_space X]
(μ : measure X) [is_locally_finite_measure μ] :
weakly_regular μ :=
begin
haveI : outer_regular μ,
{ refine (μ.finite_spanning_sets_in_open.mono' $ λ U hU, _).outer_regular,
haveI : fact (μ U < ∞), from ⟨hU.2⟩,
exact ⟨hU.1, infer_instance⟩ },
exact ⟨inner_regular.of_pseudo_emetric_space μ⟩
end
end weakly_regular
/-- Any locally finite measure on a `σ`-compact (e)metric space is regular. -/
@[priority 100] -- see Note [lower instance priority]
instance regular.of_sigma_compact_space_of_is_locally_finite_measure {X : Type*}
[emetric_space X] [sigma_compact_space X] [measurable_space X] [borel_space X] (μ : measure X)
[is_locally_finite_measure μ] : regular μ :=
{ lt_top_of_is_compact := λ K hK, hK.measure_lt_top,
inner_regular := (inner_regular.is_compact_is_closed μ).trans
(inner_regular.of_pseudo_emetric_space μ) }
end measure
end measure_theory
|
79f97590a042cf0f7715812dcd76a47b430636a6 | d1a52c3f208fa42c41df8278c3d280f075eb020c | /stage0/src/Lean/Compiler/ExportAttr.lean | 59d7cec27594ce92064fdf92238738638995f638 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | cipher1024/lean4 | 6e1f98bb58e7a92b28f5364eb38a14c8d0aae393 | 69114d3b50806264ef35b57394391c3e738a9822 | refs/heads/master | 1,642,227,983,603 | 1,642,011,696,000 | 1,642,011,696,000 | 228,607,691 | 0 | 0 | Apache-2.0 | 1,576,584,269,000 | 1,576,584,268,000 | null | UTF-8 | Lean | false | false | 1,358 | 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.Attributes
namespace Lean
private def isValidCppId (id : String) : Bool :=
let first := id.get 0;
first.isAlpha && (id.toSubstring.drop 1).all (fun c => c.isAlpha || c.isDigit || c == '_')
private def isValidCppName : Name → Bool
| Name.str Name.anonymous s _ => isValidCppId s
| Name.str p s _ => isValidCppId s && isValidCppName p
| _ => false
builtin_initialize exportAttr : ParametricAttribute Name ←
registerParametricAttribute {
name := `export,
descr := "name to be used by code generators",
getParam := fun _ stx => do
let exportName ← Attribute.Builtin.getId stx
unless isValidCppName exportName do
throwError "invalid 'export' function name, is not a valid C++ identifier"
return exportName
}
@[export lean_get_export_name_for]
def getExportNameFor (env : Environment) (n : Name) : Option Name :=
exportAttr.getParam env n
def isExport (env : Environment) (n : Name) : Bool :=
-- The main function morally is an exported function as well. In particular,
-- it should not participate in borrow inference.
(getExportNameFor env n).isSome || n == `main
end Lean
|
3fc8bf49f133b80eb78260a64e372b12a78f89b5 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/data/polynomial/field_division.lean | c88dde66b739d7ce85bb45631959826f381c427a | [
"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 | 21,214 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import algebra.gcd_monoid.basic
import data.polynomial.derivative
import data.polynomial.ring_division
import data.set.pairwise
import ring_theory.coprime.lemmas
import ring_theory.euclidean_domain
/-!
# Theory of univariate polynomials
This file starts looking like the ring theory of $ R[X] $
-/
noncomputable theory
open_locale classical big_operators
namespace polynomial
universes u v w y z
variables {R : Type u} {S : Type v} {k : Type y} {A : Type z} {a b : R} {n : ℕ}
section is_domain
variables [comm_ring R] [is_domain R] [normalization_monoid R]
instance : normalization_monoid (polynomial R) :=
{ norm_unit := λ p, ⟨C ↑(norm_unit (p.leading_coeff)), C ↑(norm_unit (p.leading_coeff))⁻¹,
by rw [← ring_hom.map_mul, units.mul_inv, C_1], by rw [← ring_hom.map_mul, units.inv_mul, C_1]⟩,
norm_unit_zero := units.ext (by simp),
norm_unit_mul := λ p q hp0 hq0, units.ext (begin
dsimp,
rw [ne.def, ← leading_coeff_eq_zero] at *,
rw [leading_coeff_mul, norm_unit_mul hp0 hq0, units.coe_mul, C_mul],
end),
norm_unit_coe_units := λ u,
units.ext begin
rw [← mul_one u⁻¹, units.coe_mul, units.eq_inv_mul_iff_mul_eq],
dsimp,
rcases polynomial.is_unit_iff.1 ⟨u, rfl⟩ with ⟨_, ⟨w, rfl⟩, h2⟩,
rw [← h2, leading_coeff_C, norm_unit_coe_units, ← C_mul, units.mul_inv, C_1],
end }
@[simp]
lemma coe_norm_unit {p : polynomial R} :
(norm_unit p : polynomial R) = C ↑(norm_unit p.leading_coeff) :=
by simp [norm_unit]
lemma leading_coeff_normalize (p : polynomial R) :
leading_coeff (normalize p) = normalize (leading_coeff p) := by simp
lemma monic.normalize_eq_self {p : polynomial R} (hp : p.monic) :
normalize p = p :=
by simp only [polynomial.coe_norm_unit, normalize_apply, hp.leading_coeff, norm_unit_one,
units.coe_one, polynomial.C.map_one, mul_one]
end is_domain
section field
variables [field R] {p q : polynomial R}
lemma is_unit_iff_degree_eq_zero : is_unit p ↔ degree p = 0 :=
⟨degree_eq_zero_of_is_unit,
λ h, have degree p ≤ 0, by simp [*, le_refl],
have hc : coeff p 0 ≠ 0, from λ hc,
by rw [eq_C_of_degree_le_zero this, hc] at h;
simpa using h,
is_unit_iff_dvd_one.2 ⟨C (coeff p 0)⁻¹, begin
conv in p { rw eq_C_of_degree_le_zero this },
rw [← C_mul, _root_.mul_inv_cancel hc, C_1]
end⟩⟩
lemma degree_pos_of_ne_zero_of_nonunit (hp0 : p ≠ 0) (hp : ¬is_unit p) :
0 < degree p :=
lt_of_not_ge (λ h, begin
rw [eq_C_of_degree_le_zero h] at hp0 hp,
exact hp (is_unit.map (C.to_monoid_hom : R →* _)
(is_unit.mk0 (coeff p 0) (mt C_inj.2 (by simpa using hp0)))),
end)
lemma monic_mul_leading_coeff_inv (h : p ≠ 0) :
monic (p * C (leading_coeff p)⁻¹) :=
by rw [monic, leading_coeff_mul, leading_coeff_C,
mul_inv_cancel (show leading_coeff p ≠ 0, from mt leading_coeff_eq_zero.1 h)]
lemma degree_mul_leading_coeff_inv (p : polynomial R) (h : q ≠ 0) :
degree (p * C (leading_coeff q)⁻¹) = degree p :=
have h₁ : (leading_coeff q)⁻¹ ≠ 0 :=
inv_ne_zero (mt leading_coeff_eq_zero.1 h),
by rw [degree_mul, degree_C h₁, add_zero]
theorem irreducible_of_monic {p : polynomial R} (hp1 : p.monic) (hp2 : p ≠ 1) :
irreducible p ↔ (∀ f g : polynomial R, f.monic → g.monic → f * g = p → f = 1 ∨ g = 1) :=
⟨λ hp3 f g hf hg hfg, or.cases_on (hp3.is_unit_or_is_unit hfg.symm)
(assume huf : is_unit f, or.inl $ eq_one_of_is_unit_of_monic hf huf)
(assume hug : is_unit g, or.inr $ eq_one_of_is_unit_of_monic hg hug),
λ hp3, ⟨mt (eq_one_of_is_unit_of_monic hp1) hp2, λ f g hp,
have hf : f ≠ 0, from λ hf, by { rw [hp, hf, zero_mul] at hp1, exact not_monic_zero hp1 },
have hg : g ≠ 0, from λ hg, by { rw [hp, hg, mul_zero] at hp1, exact not_monic_zero hp1 },
or.imp (λ hf, is_unit_of_mul_eq_one _ _ hf) (λ hg, is_unit_of_mul_eq_one _ _ hg) $
hp3 (f * C f.leading_coeff⁻¹) (g * C g.leading_coeff⁻¹)
(monic_mul_leading_coeff_inv hf) (monic_mul_leading_coeff_inv hg) $
by rw [mul_assoc, mul_left_comm _ g, ← mul_assoc, ← C_mul, ← mul_inv₀, ← leading_coeff_mul,
← hp, monic.def.1 hp1, inv_one, C_1, mul_one]⟩⟩
/-- Division of polynomials. See polynomial.div_by_monic for more details.-/
def div (p q : polynomial R) :=
C (leading_coeff q)⁻¹ * (p /ₘ (q * C (leading_coeff q)⁻¹))
/-- Remainder of polynomial division, see the lemma `quotient_mul_add_remainder_eq_aux`.
See polynomial.mod_by_monic for more details. -/
def mod (p q : polynomial R) :=
p %ₘ (q * C (leading_coeff q)⁻¹)
private lemma quotient_mul_add_remainder_eq_aux (p q : polynomial R) :
q * div p q + mod p q = p :=
if h : q = 0 then by simp only [h, zero_mul, mod, mod_by_monic_zero, zero_add]
else begin
conv {to_rhs, rw ← mod_by_monic_add_div p (monic_mul_leading_coeff_inv h)},
rw [div, mod, add_comm, mul_assoc]
end
private lemma remainder_lt_aux (p : polynomial R) (hq : q ≠ 0) :
degree (mod p q) < degree q :=
by rw ← degree_mul_leading_coeff_inv q hq; exact
degree_mod_by_monic_lt p (monic_mul_leading_coeff_inv hq)
instance : has_div (polynomial R) := ⟨div⟩
instance : has_mod (polynomial R) := ⟨mod⟩
lemma div_def : p / q = C (leading_coeff q)⁻¹ * (p /ₘ (q * C (leading_coeff q)⁻¹)) := rfl
lemma mod_def : p % q = p %ₘ (q * C (leading_coeff q)⁻¹) := rfl
lemma mod_by_monic_eq_mod (p : polynomial R) (hq : monic q) : p %ₘ q = p % q :=
show p %ₘ q = p %ₘ (q * C (leading_coeff q)⁻¹), by simp only [monic.def.1 hq, inv_one, mul_one, C_1]
lemma div_by_monic_eq_div (p : polynomial R) (hq : monic q) : p /ₘ q = p / q :=
show p /ₘ q = C (leading_coeff q)⁻¹ * (p /ₘ (q * C (leading_coeff q)⁻¹)),
by simp only [monic.def.1 hq, inv_one, C_1, one_mul, mul_one]
lemma mod_X_sub_C_eq_C_eval (p : polynomial R) (a : R) : p % (X - C a) = C (p.eval a) :=
mod_by_monic_eq_mod p (monic_X_sub_C a) ▸ mod_by_monic_X_sub_C_eq_C_eval _ _
lemma mul_div_eq_iff_is_root : (X - C a) * (p / (X - C a)) = p ↔ is_root p a :=
div_by_monic_eq_div p (monic_X_sub_C a) ▸ mul_div_by_monic_eq_iff_is_root
instance : euclidean_domain (polynomial R) :=
{ quotient := (/),
quotient_zero := by simp [div_def],
remainder := (%),
r := _,
r_well_founded := degree_lt_wf,
quotient_mul_add_remainder_eq := quotient_mul_add_remainder_eq_aux,
remainder_lt := λ p q hq, remainder_lt_aux _ hq,
mul_left_not_lt := λ p q hq, not_lt_of_ge (degree_le_mul_left _ hq),
.. polynomial.comm_ring,
.. polynomial.nontrivial }
lemma mod_eq_self_iff (hq0 : q ≠ 0) : p % q = p ↔ degree p < degree q :=
⟨λ h, h ▸ euclidean_domain.mod_lt _ hq0,
λ h, have ¬degree (q * C (leading_coeff q)⁻¹) ≤ degree p :=
not_le_of_gt $ by rwa degree_mul_leading_coeff_inv q hq0,
begin
rw [mod_def, mod_by_monic, dif_pos (monic_mul_leading_coeff_inv hq0)],
unfold div_mod_by_monic_aux,
simp only [this, false_and, if_false]
end⟩
lemma div_eq_zero_iff (hq0 : q ≠ 0) : p / q = 0 ↔ degree p < degree q :=
⟨λ h, by have := euclidean_domain.div_add_mod p q;
rwa [h, mul_zero, zero_add, mod_eq_self_iff hq0] at this,
λ h, have hlt : degree p < degree (q * C (leading_coeff q)⁻¹),
by rwa degree_mul_leading_coeff_inv q hq0,
have hm : monic (q * C (leading_coeff q)⁻¹) := monic_mul_leading_coeff_inv hq0,
by rw [div_def, (div_by_monic_eq_zero_iff hm).2 hlt, mul_zero]⟩
lemma degree_add_div (hq0 : q ≠ 0) (hpq : degree q ≤ degree p) :
degree q + degree (p / q) = degree p :=
have degree (p % q) < degree (q * (p / q)) :=
calc degree (p % q) < degree q : euclidean_domain.mod_lt _ hq0
... ≤ _ : degree_le_mul_left _ (mt (div_eq_zero_iff hq0).1 (not_lt_of_ge hpq)),
by conv_rhs { rw [← euclidean_domain.div_add_mod p q,
degree_add_eq_left_of_degree_lt this, degree_mul] }
lemma degree_div_le (p q : polynomial R) : degree (p / q) ≤ degree p :=
if hq : q = 0 then by simp [hq]
else by rw [div_def, mul_comm, degree_mul_leading_coeff_inv _ hq];
exact degree_div_by_monic_le _ _
lemma degree_div_lt (hp : p ≠ 0) (hq : 0 < degree q) : degree (p / q) < degree p :=
have hq0 : q ≠ 0, from λ hq0, by simpa [hq0] using hq,
by rw [div_def, mul_comm, degree_mul_leading_coeff_inv _ hq0];
exact degree_div_by_monic_lt _ (monic_mul_leading_coeff_inv hq0) hp
(by rw degree_mul_leading_coeff_inv _ hq0; exact hq)
@[simp] lemma degree_map [field k] (p : polynomial R) (f : R →+* k) :
degree (p.map f) = degree p :=
p.degree_map_eq_of_injective f.injective
@[simp] lemma nat_degree_map [field k] (f : R →+* k) :
nat_degree (p.map f) = nat_degree p :=
nat_degree_eq_of_degree_eq (degree_map _ f)
@[simp] lemma leading_coeff_map [field k] (f : R →+* k) :
leading_coeff (p.map f) = f (leading_coeff p) :=
by simp only [← coeff_nat_degree, coeff_map f, nat_degree_map]
theorem monic_map_iff [field k] {f : R →+* k} {p : polynomial R} :
(p.map f).monic ↔ p.monic :=
by rw [monic, leading_coeff_map, ← f.map_one, function.injective.eq_iff f.injective, monic]
theorem is_unit_map [field k] (f : R →+* k) :
is_unit (p.map f) ↔ is_unit p :=
by simp_rw [is_unit_iff_degree_eq_zero, degree_map]
lemma map_div [field k] (f : R →+* k) :
(p / q).map f = p.map f / q.map f :=
if hq0 : q = 0 then by simp [hq0]
else
by rw [div_def, div_def, map_mul, map_div_by_monic f (monic_mul_leading_coeff_inv hq0)];
simp [f.map_inv, coeff_map f]
lemma map_mod [field k] (f : R →+* k) :
(p % q).map f = p.map f % q.map f :=
if hq0 : q = 0 then by simp [hq0]
else by rw [mod_def, mod_def, leading_coeff_map f, ← f.map_inv, ← map_C f,
← map_mul f, map_mod_by_monic f (monic_mul_leading_coeff_inv hq0)]
section
open euclidean_domain
theorem gcd_map [field k] (f : R →+* k) :
gcd (p.map f) (q.map f) = (gcd p q).map f :=
gcd.induction p q (λ x, by simp_rw [map_zero, euclidean_domain.gcd_zero_left]) $ λ x y hx ih,
by rw [gcd_val, ← map_mod, ih, ← gcd_val]
end
lemma eval₂_gcd_eq_zero [comm_semiring k] {ϕ : R →+* k} {f g : polynomial R} {α : k}
(hf : f.eval₂ ϕ α = 0) (hg : g.eval₂ ϕ α = 0) : (euclidean_domain.gcd f g).eval₂ ϕ α = 0 :=
by rw [euclidean_domain.gcd_eq_gcd_ab f g, polynomial.eval₂_add, polynomial.eval₂_mul,
polynomial.eval₂_mul, hf, hg, zero_mul, zero_mul, zero_add]
lemma eval_gcd_eq_zero {f g : polynomial R} {α : R} (hf : f.eval α = 0) (hg : g.eval α = 0) :
(euclidean_domain.gcd f g).eval α = 0 := eval₂_gcd_eq_zero hf hg
lemma root_left_of_root_gcd [comm_semiring k] {ϕ : R →+* k} {f g : polynomial R} {α : k}
(hα : (euclidean_domain.gcd f g).eval₂ ϕ α = 0) : f.eval₂ ϕ α = 0 :=
by { cases euclidean_domain.gcd_dvd_left f g with p hp,
rw [hp, polynomial.eval₂_mul, hα, zero_mul] }
lemma root_right_of_root_gcd [comm_semiring k] {ϕ : R →+* k} {f g : polynomial R} {α : k}
(hα : (euclidean_domain.gcd f g).eval₂ ϕ α = 0) : g.eval₂ ϕ α = 0 :=
by { cases euclidean_domain.gcd_dvd_right f g with p hp,
rw [hp, polynomial.eval₂_mul, hα, zero_mul] }
lemma root_gcd_iff_root_left_right [comm_semiring k] {ϕ : R →+* k} {f g : polynomial R} {α : k} :
(euclidean_domain.gcd f g).eval₂ ϕ α = 0 ↔ (f.eval₂ ϕ α = 0) ∧ (g.eval₂ ϕ α = 0) :=
⟨λ h, ⟨root_left_of_root_gcd h, root_right_of_root_gcd h⟩, λ h, eval₂_gcd_eq_zero h.1 h.2⟩
lemma is_root_gcd_iff_is_root_left_right {f g : polynomial R} {α : R} :
(euclidean_domain.gcd f g).is_root α ↔ f.is_root α ∧ g.is_root α :=
root_gcd_iff_root_left_right
theorem is_coprime_map [field k] (f : R →+* k) :
is_coprime (p.map f) (q.map f) ↔ is_coprime p q :=
by rw [← gcd_is_unit_iff, ← gcd_is_unit_iff, gcd_map, is_unit_map]
@[simp] lemma map_eq_zero [semiring S] [nontrivial S] (f : R →+* S) :
p.map f = 0 ↔ p = 0 :=
by simp only [polynomial.ext_iff, f.map_eq_zero, coeff_map, coeff_zero]
lemma map_ne_zero [semiring S] [nontrivial S] {f : R →+* S} (hp : p ≠ 0) : p.map f ≠ 0 :=
mt (map_eq_zero f).1 hp
lemma mem_roots_map [field k] {f : R →+* k} {x : k} (hp : p ≠ 0) :
x ∈ (p.map f).roots ↔ p.eval₂ f x = 0 :=
begin
rw mem_roots (show p.map f ≠ 0, by exact map_ne_zero hp),
dsimp only [is_root],
rw polynomial.eval_map,
end
lemma mem_root_set [field k] [algebra R k] {x : k} (hp : p ≠ 0) :
x ∈ p.root_set k ↔ aeval x p = 0 :=
iff.trans multiset.mem_to_finset (mem_roots_map hp)
lemma root_set_C_mul_X_pow {R S : Type*} [field R] [field S] [algebra R S]
{n : ℕ} (hn : n ≠ 0) {a : R} (ha : a ≠ 0) : (C a * X ^ n).root_set S = {0} :=
begin
ext x,
rw [set.mem_singleton_iff, mem_root_set, aeval_mul, aeval_C, aeval_X_pow, mul_eq_zero],
{ simp_rw [ring_hom.map_eq_zero, pow_eq_zero_iff (nat.pos_of_ne_zero hn), or_iff_right_iff_imp],
exact λ ha', (ha ha').elim },
{ exact mul_ne_zero (mt C_eq_zero.mp ha) (pow_ne_zero n X_ne_zero) },
end
lemma root_set_monomial {R S : Type*} [field R] [field S] [algebra R S]
{n : ℕ} (hn : n ≠ 0) {a : R} (ha : a ≠ 0) : (monomial n a).root_set S = {0} :=
by rw [←C_mul_X_pow_eq_monomial, root_set_C_mul_X_pow hn ha]
lemma root_set_X_pow {R S : Type*} [field R] [field S] [algebra R S]
{n : ℕ} (hn : n ≠ 0) : (X ^ n : polynomial R).root_set S = {0} :=
by { rw [←one_mul (X ^ n : polynomial R), ←C_1, root_set_C_mul_X_pow hn], exact one_ne_zero }
lemma exists_root_of_degree_eq_one (h : degree p = 1) : ∃ x, is_root p x :=
⟨-(p.coeff 0 / p.coeff 1),
have p.coeff 1 ≠ 0,
by rw ← nat_degree_eq_of_degree_eq_some h;
exact mt leading_coeff_eq_zero.1 (λ h0, by simpa [h0] using h),
by conv in p { rw [eq_X_add_C_of_degree_le_one (show degree p ≤ 1, by rw h; exact le_refl _)] };
simp [is_root, mul_div_cancel' _ this]⟩
lemma coeff_inv_units (u : units (polynomial R)) (n : ℕ) :
((↑u : polynomial R).coeff n)⁻¹ = ((↑u⁻¹ : polynomial R).coeff n) :=
begin
rw [eq_C_of_degree_eq_zero (degree_coe_units u), eq_C_of_degree_eq_zero (degree_coe_units u⁻¹),
coeff_C, coeff_C, inv_eq_one_div],
split_ifs,
{ rw [div_eq_iff_mul_eq (coeff_coe_units_zero_ne_zero u), coeff_zero_eq_eval_zero,
coeff_zero_eq_eval_zero, ← eval_mul, ← units.coe_mul, inv_mul_self];
simp },
{ simp }
end
lemma monic_normalize (hp0 : p ≠ 0) : monic (normalize p) :=
begin
rw [ne.def, ← leading_coeff_eq_zero, ← ne.def, ← is_unit_iff_ne_zero] at hp0,
rw [monic, leading_coeff_normalize, normalize_eq_one],
apply hp0,
end
lemma leading_coeff_div (hpq : q.degree ≤ p.degree) :
(p / q).leading_coeff = p.leading_coeff / q.leading_coeff :=
begin
by_cases hq : q = 0, { simp [hq] },
rw [div_def, leading_coeff_mul, leading_coeff_C,
leading_coeff_div_by_monic_of_monic (monic_mul_leading_coeff_inv hq) _,
mul_comm, div_eq_mul_inv],
rwa [degree_mul_leading_coeff_inv q hq]
end
lemma div_C_mul : p / (C a * q) = C a⁻¹ * (p / q) :=
begin
by_cases ha : a = 0,
{ simp [ha] },
simp only [div_def, leading_coeff_mul, mul_inv₀,
leading_coeff_C, C.map_mul, mul_assoc],
congr' 3,
rw [mul_left_comm q, ← mul_assoc, ← C.map_mul, mul_inv_cancel ha,
C.map_one, one_mul]
end
lemma C_mul_dvd (ha : a ≠ 0) : C a * p ∣ q ↔ p ∣ q :=
⟨λ h, dvd_trans (dvd_mul_left _ _) h, λ ⟨r, hr⟩, ⟨C a⁻¹ * r,
by rw [mul_assoc, mul_left_comm p, ← mul_assoc, ← C.map_mul, _root_.mul_inv_cancel ha,
C.map_one, one_mul, hr]⟩⟩
lemma dvd_C_mul (ha : a ≠ 0) : p ∣ polynomial.C a * q ↔ p ∣ q :=
⟨λ ⟨r, hr⟩, ⟨C a⁻¹ * r,
by rw [mul_left_comm p, ← hr, ← mul_assoc, ← C.map_mul, _root_.inv_mul_cancel ha,
C.map_one, one_mul]⟩,
λ h, dvd_trans h (dvd_mul_left _ _)⟩
lemma coe_norm_unit_of_ne_zero (hp : p ≠ 0) : (norm_unit p : polynomial R) = C p.leading_coeff⁻¹ :=
have p.leading_coeff ≠ 0 := mt leading_coeff_eq_zero.mp hp,
by simp [comm_group_with_zero.coe_norm_unit _ this]
theorem map_dvd_map' [field k] (f : R →+* k) {x y : polynomial R} : x.map f ∣ y.map f ↔ x ∣ y :=
if H : x = 0 then by rw [H, map_zero, zero_dvd_iff, zero_dvd_iff, map_eq_zero]
else by rw [← normalize_dvd_iff, ← @normalize_dvd_iff (polynomial R),
normalize_apply, normalize_apply,
coe_norm_unit_of_ne_zero H, coe_norm_unit_of_ne_zero (mt (map_eq_zero f).1 H),
leading_coeff_map, ← f.map_inv, ← map_C, ← map_mul,
map_dvd_map _ f.injective (monic_mul_leading_coeff_inv H)]
lemma degree_normalize : degree (normalize p) = degree p := by simp
lemma prime_of_degree_eq_one (hp1 : degree p = 1) : prime p :=
have prime (normalize p),
from monic.prime_of_degree_eq_one (hp1 ▸ degree_normalize)
(monic_normalize (λ hp0, absurd hp1 (hp0.symm ▸ by simp; exact dec_trivial))),
(normalize_associated _).prime this
lemma irreducible_of_degree_eq_one (hp1 : degree p = 1) : irreducible p :=
(prime_of_degree_eq_one hp1).irreducible
theorem not_irreducible_C (x : R) : ¬irreducible (C x) :=
if H : x = 0 then by { rw [H, C_0], exact not_irreducible_zero }
else λ hx, irreducible.not_unit hx $ is_unit_C.2 $ is_unit_iff_ne_zero.2 H
theorem degree_pos_of_irreducible (hp : irreducible p) : 0 < p.degree :=
lt_of_not_ge $ λ hp0, have _ := eq_C_of_degree_le_zero hp0,
not_irreducible_C (p.coeff 0) $ this ▸ hp
theorem pairwise_coprime_X_sub {α : Type u} [field α] {I : Type v}
{s : I → α} (H : function.injective s) :
pairwise (is_coprime on (λ i : I, polynomial.X - polynomial.C (s i))) :=
λ i j hij, have h : s j - s i ≠ 0, from sub_ne_zero_of_ne $ function.injective.ne H hij.symm,
⟨polynomial.C (s j - s i)⁻¹, -polynomial.C (s j - s i)⁻¹,
by rw [neg_mul_eq_neg_mul_symm, ← sub_eq_add_neg, ← mul_sub, sub_sub_sub_cancel_left,
← polynomial.C_sub, ← polynomial.C_mul, inv_mul_cancel h, polynomial.C_1]⟩
/-- If `f` is a polynomial over a field, and `a : K` satisfies `f' a ≠ 0`,
then `f / (X - a)` is coprime with `X - a`.
Note that we do not assume `f a = 0`, because `f / (X - a) = (f - f a) / (X - a)`. -/
lemma is_coprime_of_is_root_of_eval_derivative_ne_zero {K : Type*} [field K]
(f : polynomial K) (a : K) (hf' : f.derivative.eval a ≠ 0) :
is_coprime (X - C a : polynomial K) (f /ₘ (X - C a)) :=
begin
refine or.resolve_left (dvd_or_coprime (X - C a) (f /ₘ (X - C a))
(irreducible_of_degree_eq_one (polynomial.degree_X_sub_C a))) _,
contrapose! hf' with h,
have key : (X - C a) * (f /ₘ (X - C a)) = f - (f %ₘ (X - C a)),
{ rw [eq_sub_iff_add_eq, ← eq_sub_iff_add_eq', mod_by_monic_eq_sub_mul_div],
exact monic_X_sub_C a },
replace key := congr_arg derivative key,
simp only [derivative_X, derivative_mul, one_mul, sub_zero, derivative_sub,
mod_by_monic_X_sub_C_eq_C_eval, derivative_C] at key,
have : (X - C a) ∣ derivative f := key ▸ (dvd_add h (dvd_mul_right _ _)),
rw [← dvd_iff_mod_by_monic_eq_zero (monic_X_sub_C _), mod_by_monic_X_sub_C_eq_C_eval] at this,
rw [← C_inj, this, C_0],
end
lemma prod_multiset_root_eq_finset_root {p : polynomial R} (hzero : p ≠ 0) :
(multiset.map (λ (a : R), X - C a) p.roots).prod =
∏ a in (multiset.to_finset p.roots), (λ (a : R), (X - C a) ^ (root_multiplicity a p)) a :=
by simp only [count_roots hzero, finset.prod_multiset_map_count]
/-- The product `∏ (X - a)` for `a` inside the multiset `p.roots` divides `p`. -/
lemma prod_multiset_X_sub_C_dvd (p : polynomial R) :
(multiset.map (λ (a : R), X - C a) p.roots).prod ∣ p :=
begin
by_cases hp0 : p = 0,
{ simp only [hp0, roots_zero, is_unit_one, multiset.prod_zero, multiset.map_zero, is_unit.dvd] },
rw prod_multiset_root_eq_finset_root hp0,
have hcoprime : pairwise (is_coprime on λ (a : R), polynomial.X - C (id a)) :=
pairwise_coprime_X_sub function.injective_id,
have H : pairwise (is_coprime on λ (a : R), (polynomial.X - C (id a)) ^ (root_multiplicity a p)),
{ intros a b hdiff, exact (hcoprime a b hdiff).pow },
apply finset.prod_dvd_of_coprime (H.set_pairwise (↑(multiset.to_finset p.roots) : set R)),
intros a h,
rw multiset.mem_to_finset at h,
exact pow_root_multiplicity_dvd p a
end
lemma roots_C_mul (p : polynomial R) {a : R} (hzero : a ≠ 0) : (C a * p).roots = p.roots :=
begin
by_cases hpzero : p = 0,
{ simp only [hpzero, mul_zero] },
rw multiset.ext,
intro b,
have prodzero : C a * p ≠ 0,
{ simp only [hpzero, or_false, ne.def, mul_eq_zero, C_eq_zero, hzero, not_false_iff] },
rw [count_roots hpzero, count_roots prodzero, root_multiplicity_mul prodzero],
have mulzero : root_multiplicity b (C a) = 0,
{ simp only [hzero, root_multiplicity_eq_zero, eval_C, is_root.def, not_false_iff] },
simp only [mulzero, zero_add]
end
lemma roots_normalize : (normalize p).roots = p.roots :=
begin
by_cases hzero : p = 0,
{ rw [hzero, normalize_zero], },
{ have hcoeff : p.leading_coeff ≠ 0,
{ intro h, exact hzero (leading_coeff_eq_zero.1 h) },
rw [normalize_apply, mul_comm, coe_norm_unit_of_ne_zero hzero,
roots_C_mul _ (inv_ne_zero hcoeff)], },
end
end field
end polynomial
|
83d71e7659ee636fc34ec060a13b4555a30d236e | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/eq9.lean | 6443bacfc0aa31c5062bee45e89dddaf8d3f99bd | [
"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 | 330 | lean | open nat
theorem lt_trans : ∀ {a b c : nat}, a < b → b < c → a < c
| lt_trans h (lt.base _) := lt.step h
| lt_trans h₁ (lt.step h₂) := lt.step (lt_trans h₁ h₂)
theorem lt_succ : ∀ {a b : nat}, a < b → succ a < succ b
| lt_succ (lt.base a) := lt.base (succ a)
| lt_succ (lt.step h) := lt.step (lt_succ h)
|
e01a8b2850e2d6be5263f8318ce73a53be2b3b6e | b2fe74b11b57d362c13326bc5651244f111fa6f4 | /src/data/nat/cast.lean | bf34f2fbba03434d77552cbe5e9d76dccfe06126 | [
"Apache-2.0"
] | permissive | midfield/mathlib | c4db5fa898b5ac8f2f80ae0d00c95eb6f745f4c7 | 775edc615ecec631d65b6180dbcc7bc26c3abc26 | refs/heads/master | 1,675,330,551,921 | 1,608,304,514,000 | 1,608,304,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,801 | lean | /-
Copyright (c) 2014 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
Natural homomorphism from the natural numbers into a monoid with one.
-/
import algebra.ordered_field
import data.nat.basic
namespace nat
variables {α : Type*}
section
variables [has_zero α] [has_one α] [has_add α]
/-- Canonical homomorphism from `ℕ` to a type `α` with `0`, `1` and `+`. -/
protected def cast : ℕ → α
| 0 := 0
| (n+1) := cast n + 1
/--
Coercions such as `nat.cast_coe` that go from a concrete structure such as
`ℕ` to an arbitrary ring `α` should be set up as follows:
```lean
@[priority 900] instance : has_coe_t ℕ α := ⟨...⟩
```
It needs to be `has_coe_t` instead of `has_coe` because otherwise type-class
inference would loop when constructing the transitive coercion `ℕ → ℕ → ℕ → ...`.
The reduced priority is necessary so that it doesn't conflict with instances
such as `has_coe_t α (option α)`.
For this to work, we reduce the priority of the `coe_base` and `coe_trans`
instances because we want the instances for `has_coe_t` to be tried in the
following order:
1. `has_coe_t` instances declared in mathlib (such as `has_coe_t α (with_top α)`, etc.)
2. `coe_base`, which contains instances such as `has_coe (fin n) n`
3. `nat.cast_coe : has_coe_t ℕ α` etc.
4. `coe_trans`
If `coe_trans` is tried first, then `nat.cast_coe` doesn't get a chance to apply.
-/
library_note "coercion into rings"
attribute [instance, priority 950] coe_base
attribute [instance, priority 500] coe_trans
-- see note [coercion into rings]
@[priority 900] instance cast_coe : has_coe_t ℕ α := ⟨nat.cast⟩
@[simp, norm_cast] theorem cast_zero : ((0 : ℕ) : α) = 0 := rfl
theorem cast_add_one (n : ℕ) : ((n + 1 : ℕ) : α) = n + 1 := rfl
@[simp, norm_cast, priority 500]
theorem cast_succ (n : ℕ) : ((succ n : ℕ) : α) = n + 1 := rfl
@[simp, norm_cast] theorem cast_ite (P : Prop) [decidable P] (m n : ℕ) :
(((ite P m n) : ℕ) : α) = ite P (m : α) (n : α) :=
by { split_ifs; refl, }
end
@[simp, norm_cast] theorem cast_one [add_monoid α] [has_one α] : ((1 : ℕ) : α) = 1 := zero_add _
@[simp, norm_cast] theorem cast_add [add_monoid α] [has_one α] (m) : ∀ n, ((m + n : ℕ) : α) = m + n
| 0 := (add_zero _).symm
| (n+1) := show ((m + n : ℕ) : α) + 1 = m + (n + 1), by rw [cast_add n, add_assoc]
/-- `coe : ℕ → α` as an `add_monoid_hom`. -/
def cast_add_monoid_hom (α : Type*) [add_monoid α] [has_one α] : ℕ →+ α :=
{ to_fun := coe,
map_add' := cast_add,
map_zero' := cast_zero }
@[simp] lemma coe_cast_add_monoid_hom [add_monoid α] [has_one α] :
(cast_add_monoid_hom α : ℕ → α) = coe := rfl
@[simp, norm_cast] theorem cast_bit0 [add_monoid α] [has_one α] (n : ℕ) :
((bit0 n : ℕ) : α) = bit0 n := cast_add _ _
@[simp, norm_cast] theorem cast_bit1 [add_monoid α] [has_one α] (n : ℕ) :
((bit1 n : ℕ) : α) = bit1 n :=
by rw [bit1, cast_add_one, cast_bit0]; refl
lemma cast_two {α : Type*} [add_monoid α] [has_one α] : ((2 : ℕ) : α) = 2 := by simp
@[simp, norm_cast] theorem cast_pred [add_group α] [has_one α] :
∀ {n}, 0 < n → ((n - 1 : ℕ) : α) = n - 1
| (n+1) h := (add_sub_cancel (n:α) 1).symm
@[simp, norm_cast] theorem cast_sub [add_group α] [has_one α] {m n} (h : m ≤ n) :
((n - m : ℕ) : α) = n - m :=
eq_sub_of_add_eq $ by rw [← cast_add, nat.sub_add_cancel h]
@[simp, norm_cast] theorem cast_mul [semiring α] (m) : ∀ n, ((m * n : ℕ) : α) = m * n
| 0 := (mul_zero _).symm
| (n+1) := (cast_add _ _).trans $
show ((m * n : ℕ) : α) + m = m * (n + 1), by rw [cast_mul n, left_distrib, mul_one]
@[simp] theorem cast_dvd {α : Type*} [field α] {m n : ℕ} (n_dvd : n ∣ m) (n_nonzero : (n:α) ≠ 0) : ((m / n : ℕ) : α) = m / n :=
begin
rcases n_dvd with ⟨k, rfl⟩,
have : n ≠ 0, {rintro rfl, simpa using n_nonzero},
rw nat.mul_div_cancel_left _ (nat.pos_iff_ne_zero.2 this),
rw [nat.cast_mul, mul_div_cancel_left _ n_nonzero],
end
/-- `coe : ℕ → α` as a `ring_hom` -/
def cast_ring_hom (α : Type*) [semiring α] : ℕ →+* α :=
{ to_fun := coe,
map_one' := cast_one,
map_mul' := cast_mul,
.. cast_add_monoid_hom α }
@[simp] lemma coe_cast_ring_hom [semiring α] : (cast_ring_hom α : ℕ → α) = coe := rfl
lemma cast_commute [semiring α] (n : ℕ) (x : α) : commute ↑n x :=
nat.rec_on n (commute.zero_left x) $ λ n ihn, ihn.add_left $ commute.one_left x
lemma commute_cast [semiring α] (x : α) (n : ℕ) : commute x n :=
(n.cast_commute x).symm
section
variables [ordered_semiring α]
@[simp] theorem cast_nonneg : ∀ n : ℕ, 0 ≤ (n : α)
| 0 := le_refl _
| (n+1) := add_nonneg (cast_nonneg n) zero_le_one
theorem mono_cast : monotone (coe : ℕ → α) :=
λ m n h, let ⟨k, hk⟩ := le_iff_exists_add.1 h in by simp [hk]
variable [nontrivial α]
theorem strict_mono_cast : strict_mono (coe : ℕ → α) :=
λ m n h, nat.le_induction (lt_add_of_pos_right _ zero_lt_one)
(λ n _ h, lt_add_of_lt_of_pos h zero_lt_one) _ h
@[simp, norm_cast] theorem cast_le {m n : ℕ} :
(m : α) ≤ n ↔ m ≤ n :=
strict_mono_cast.le_iff_le
@[simp, norm_cast] theorem cast_lt {m n : ℕ} : (m : α) < n ↔ m < n :=
strict_mono_cast.lt_iff_lt
@[simp] theorem cast_pos {n : ℕ} : (0 : α) < n ↔ 0 < n :=
by rw [← cast_zero, cast_lt]
lemma cast_add_one_pos (n : ℕ) : 0 < (n : α) + 1 :=
add_pos_of_nonneg_of_pos n.cast_nonneg zero_lt_one
@[simp, norm_cast] theorem one_lt_cast {n : ℕ} : 1 < (n : α) ↔ 1 < n :=
by rw [← cast_one, cast_lt]
@[simp, norm_cast] theorem one_le_cast {n : ℕ} : 1 ≤ (n : α) ↔ 1 ≤ n :=
by rw [← cast_one, cast_le]
@[simp, norm_cast] theorem cast_lt_one {n : ℕ} : (n : α) < 1 ↔ n = 0 :=
by rw [← cast_one, cast_lt, lt_succ_iff, le_zero_iff]
@[simp, norm_cast] theorem cast_le_one {n : ℕ} : (n : α) ≤ 1 ↔ n ≤ 1 :=
by rw [← cast_one, cast_le]
end
@[simp, norm_cast] theorem cast_min [linear_ordered_semiring α] {a b : ℕ} :
(↑(min a b) : α) = min a b :=
by by_cases a ≤ b; simp [h, min]
@[simp, norm_cast] theorem cast_max [linear_ordered_semiring α] {a b : ℕ} :
(↑(max a b) : α) = max a b :=
by by_cases a ≤ b; simp [h, max]
@[simp, norm_cast] theorem abs_cast [linear_ordered_ring α] (a : ℕ) :
abs (a : α) = a :=
abs_of_nonneg (cast_nonneg a)
lemma coe_nat_dvd [comm_semiring α] {m n : ℕ} (h : m ∣ n) :
(m : α) ∣ (n : α) :=
ring_hom.map_dvd (nat.cast_ring_hom α) h
alias coe_nat_dvd ← has_dvd.dvd.nat_cast
section linear_ordered_field
variables [linear_ordered_field α]
lemma inv_pos_of_nat {n : ℕ} : 0 < ((n : α) + 1)⁻¹ :=
inv_pos.2 $ add_pos_of_nonneg_of_pos n.cast_nonneg zero_lt_one
lemma one_div_pos_of_nat {n : ℕ} : 0 < 1 / ((n : α) + 1) :=
by { rw one_div, exact inv_pos_of_nat }
lemma one_div_le_one_div {n m : ℕ} (h : n ≤ m) : 1 / ((m : α) + 1) ≤ 1 / ((n : α) + 1) :=
by { refine one_div_le_one_div_of_le _ _, exact nat.cast_add_one_pos _, simpa }
lemma one_div_lt_one_div {n m : ℕ} (h : n < m) : 1 / ((m : α) + 1) < 1 / ((n : α) + 1) :=
by { refine one_div_lt_one_div_of_lt _ _, exact nat.cast_add_one_pos _, simpa }
end linear_ordered_field
end nat
namespace add_monoid_hom
variables {A B : Type*} [add_monoid A]
@[ext] lemma ext_nat {f g : ℕ →+ A} (h : f 1 = g 1) : f = g :=
ext $ λ n, nat.rec_on n (f.map_zero.trans g.map_zero.symm) $ λ n ihn,
by simp only [nat.succ_eq_add_one, *, map_add]
variables [has_one A] [add_monoid B] [has_one B]
lemma eq_nat_cast (f : ℕ →+ A) (h1 : f 1 = 1) :
∀ n : ℕ, f n = n :=
congr_fun $ show f = nat.cast_add_monoid_hom A, from ext_nat (h1.trans nat.cast_one.symm)
lemma map_nat_cast (f : A →+ B) (h1 : f 1 = 1) (n : ℕ) : f n = n :=
(f.comp (nat.cast_add_monoid_hom A)).eq_nat_cast (by simp [h1]) _
end add_monoid_hom
namespace ring_hom
variables {R : Type*} {S : Type*} [semiring R] [semiring S]
@[simp] lemma eq_nat_cast (f : ℕ →+* R) (n : ℕ) : f n = n :=
f.to_add_monoid_hom.eq_nat_cast f.map_one n
@[simp] lemma map_nat_cast (f : R →+* S) (n : ℕ) :
f n = n :=
(f.comp (nat.cast_ring_hom R)).eq_nat_cast n
lemma ext_nat (f g : ℕ →+* R) : f = g :=
coe_add_monoid_hom_injective $ add_monoid_hom.ext_nat $ f.map_one.trans g.map_one.symm
end ring_hom
@[simp, norm_cast] theorem nat.cast_id (n : ℕ) : ↑n = n :=
((ring_hom.id ℕ).eq_nat_cast n).symm
@[simp] theorem nat.cast_with_bot : ∀ (n : ℕ),
@coe ℕ (with_bot ℕ) (@coe_to_lift _ _ nat.cast_coe) n = n
| 0 := rfl
| (n+1) := by rw [with_bot.coe_add, nat.cast_add, nat.cast_with_bot n]; refl
instance nat.subsingleton_ring_hom {R : Type*} [semiring R] : subsingleton (ℕ →+* R) :=
⟨ring_hom.ext_nat⟩
namespace with_top
variables {α : Type*}
variables [has_zero α] [has_one α] [has_add α]
@[simp, norm_cast] lemma coe_nat : ∀(n : nat), ((n : α) : with_top α) = n
| 0 := rfl
| (n+1) := by { push_cast, rw [coe_nat n] }
@[simp] lemma nat_ne_top (n : nat) : (n : with_top α) ≠ ⊤ :=
by { rw [←coe_nat n], apply coe_ne_top }
@[simp] lemma top_ne_nat (n : nat) : (⊤ : with_top α) ≠ n :=
by { rw [←coe_nat n], apply top_ne_coe }
lemma add_one_le_of_lt {i n : with_top ℕ} (h : i < n) : i + 1 ≤ n :=
begin
cases n, { exact le_top },
cases i, { exact (not_le_of_lt h le_top).elim },
exact with_top.coe_le_coe.2 (with_top.coe_lt_coe.1 h)
end
@[elab_as_eliminator]
lemma nat_induction {P : with_top ℕ → Prop} (a : with_top ℕ)
(h0 : P 0) (hsuc : ∀n:ℕ, P n → P n.succ) (htop : (∀n : ℕ, P n) → P ⊤) : P a :=
begin
have A : ∀n:ℕ, P n := λ n, nat.rec_on n h0 hsuc,
cases a,
{ exact htop A },
{ exact A a }
end
end with_top
|
658eba364275f9843a5ea1a77c988d3049b1b624 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/adjunction/mates.lean | 640132902d5b0c2bd03610f147c66ed1483b9bb9 | [] | 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 | 11,844 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Bhavik Mehta
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.adjunction.basic
import Mathlib.category_theory.conj
import Mathlib.category_theory.yoneda
import Mathlib.PostPort
universes u₁ u₂ u₃ u₄ v₁ v₂ v₃ v₄
namespace Mathlib
/-!
# Mate of natural transformations
This file establishes the bijection between the 2-cells
L₁ R₁
C --→ D C ←-- D
G ↓ ↗ ↓ H G ↓ ↘ ↓ H
E --→ F E ←-- F
L₂ R₂
where `L₁ ⊣ R₁` and `L₂ ⊣ R₂`, and shows that in the special case where `G,H` are identity then the
bijection preserves and reflects isomorphisms (i.e. we have bijections `(L₂ ⟶ L₁) ≃ (R₁ ⟶ R₂)`, and
if either side is an iso then the other side is as well).
On its own, this bijection is not particularly useful but it includes a number of interesting cases
as specializations.
For instance, this generalises the fact that adjunctions are unique (since if `L₁ ≅ L₂` then we
deduce `R₁ ≅ R₂`).
Another example arises from considering the square representing that a functor `H` preserves
products, in particular the morphism `HA ⨯ H- ⟶ H(A ⨯ -)`. Then provided `(A ⨯ -)` and `HA ⨯ -` have
left adjoints (for instance if the relevant categories are cartesian closed), the transferred
natural transformation is the exponential comparison morphism: `H(A ^ -) ⟶ HA ^ H-`.
Furthermore if `H` has a left adjoint `L`, this morphism is an isomorphism iff its mate
`L(HA ⨯ -) ⟶ A ⨯ L-` is an isomorphism, see
https://ncatlab.org/nlab/show/Frobenius+reciprocity#InCategoryTheory.
This also relates to Grothendieck's yoga of six operations, though this is not spelled out in
mathlib: https://ncatlab.org/nlab/show/six+operations.
-/
namespace category_theory
/--
Suppose we have a square of functors (where the top and bottom are adjunctions `L₁ ⊣ R₁` and
`L₂ ⊣ R₂` respectively).
C ↔ D
G ↓ ↓ H
E ↔ F
Then we have a bijection between natural transformations `G ⋙ L₂ ⟶ L₁ ⋙ H` and
`R₁ ⋙ G ⟶ H ⋙ R₂`.
This can be seen as a bijection of the 2-cells:
L₁ R₁
C --→ D C ←-- D
G ↓ ↗ ↓ H G ↓ ↘ ↓ H
E --→ F E ←-- F
L₂ R₂
Note that if one of the transformations is an iso, it does not imply the other is an iso.
-/
def transfer_nat_trans {C : Type u₁} {D : Type u₂} [category C] [category D] {E : Type u₃} {F : Type u₄} [category E] [category F] {G : C ⥤ E} {H : D ⥤ F} {L₁ : C ⥤ D} {R₁ : D ⥤ C} {L₂ : E ⥤ F} {R₂ : F ⥤ E} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) : (G ⋙ L₂ ⟶ L₁ ⋙ H) ≃ (R₁ ⋙ G ⟶ H ⋙ R₂) :=
equiv.mk
(fun (h : G ⋙ L₂ ⟶ L₁ ⋙ H) =>
nat_trans.mk
fun (X : D) =>
nat_trans.app (adjunction.unit adj₂) (functor.obj G (functor.obj R₁ X)) ≫
functor.map R₂
(nat_trans.app h (functor.obj R₁ X) ≫ functor.map H (nat_trans.app (adjunction.counit adj₁) X)))
(fun (h : R₁ ⋙ G ⟶ H ⋙ R₂) =>
nat_trans.mk
fun (X : C) =>
functor.map L₂ (functor.map G (nat_trans.app (adjunction.unit adj₁) X) ≫ nat_trans.app h (functor.obj L₁ X)) ≫
nat_trans.app (adjunction.counit adj₂) (functor.obj H (functor.obj L₁ X)))
sorry sorry
theorem transfer_nat_trans_counit {C : Type u₁} {D : Type u₂} [category C] [category D] {E : Type u₃} {F : Type u₄} [category E] [category F] {G : C ⥤ E} {H : D ⥤ F} {L₁ : C ⥤ D} {R₁ : D ⥤ C} {L₂ : E ⥤ F} {R₂ : F ⥤ E} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (f : G ⋙ L₂ ⟶ L₁ ⋙ H) (Y : D) : functor.map L₂ (nat_trans.app (coe_fn (transfer_nat_trans adj₁ adj₂) f) Y) ≫
nat_trans.app (adjunction.counit adj₂) (functor.obj H Y) =
nat_trans.app f (functor.obj R₁ Y) ≫ functor.map H (nat_trans.app (adjunction.counit adj₁) Y) := sorry
theorem unit_transfer_nat_trans {C : Type u₁} {D : Type u₂} [category C] [category D] {E : Type u₃} {F : Type u₄} [category E] [category F] {G : C ⥤ E} {H : D ⥤ F} {L₁ : C ⥤ D} {R₁ : D ⥤ C} {L₂ : E ⥤ F} {R₂ : F ⥤ E} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (f : G ⋙ L₂ ⟶ L₁ ⋙ H) (X : C) : functor.map G (nat_trans.app (adjunction.unit adj₁) X) ≫
nat_trans.app (coe_fn (transfer_nat_trans adj₁ adj₂) f) (functor.obj L₁ X) =
nat_trans.app (adjunction.unit adj₂) (functor.obj G (functor.obj 𝟭 X)) ≫
functor.map R₂ (nat_trans.app f (functor.obj 𝟭 X)) := sorry
/--
Given two adjunctions `L₁ ⊣ R₁` and `L₂ ⊣ R₂` both between categories `C`, `D`, there is a
bijection between natural transformations `L₂ ⟶ L₁` and natural transformations `R₁ ⟶ R₂`.
This is defined as a special case of `transfer_nat_trans`, where the two "vertical" functors are
identity.
TODO: Generalise to when the two vertical functors are equivalences rather than being exactly `𝟭`.
Furthermore, this bijection preserves (and reflects) isomorphisms, i.e. a transformation is an iso
iff its image under the bijection is an iso, see eg `category_theory.transfer_nat_trans_self_iso`.
This is in contrast to the general case `transfer_nat_trans` which does not in general have this
property.
-/
def transfer_nat_trans_self {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {L₂ : C ⥤ D} {R₁ : D ⥤ C} {R₂ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) : (L₂ ⟶ L₁) ≃ (R₁ ⟶ R₂) :=
equiv.trans
(equiv.trans (equiv.symm (iso.hom_congr (functor.left_unitor L₂) (functor.right_unitor L₁)))
(transfer_nat_trans adj₁ adj₂))
(iso.hom_congr (functor.right_unitor R₁) (functor.left_unitor R₂))
theorem transfer_nat_trans_self_counit {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {L₂ : C ⥤ D} {R₁ : D ⥤ C} {R₂ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (f : L₂ ⟶ L₁) (X : D) : functor.map L₂ (nat_trans.app (coe_fn (transfer_nat_trans_self adj₁ adj₂) f) X) ≫
nat_trans.app (adjunction.counit adj₂) X =
nat_trans.app f (functor.obj R₁ X) ≫ nat_trans.app (adjunction.counit adj₁) X := sorry
theorem unit_transfer_nat_trans_self {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {L₂ : C ⥤ D} {R₁ : D ⥤ C} {R₂ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (f : L₂ ⟶ L₁) (X : C) : nat_trans.app (adjunction.unit adj₁) X ≫
nat_trans.app (coe_fn (transfer_nat_trans_self adj₁ adj₂) f) (functor.obj L₁ X) =
nat_trans.app (adjunction.unit adj₂) X ≫ functor.map R₂ (nat_trans.app f X) := sorry
@[simp] theorem transfer_nat_trans_self_id {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {R₁ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) : coe_fn (transfer_nat_trans_self adj₁ adj₁) 𝟙 = 𝟙 := sorry
@[simp] theorem transfer_nat_trans_self_symm_id {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {R₁ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) : coe_fn (equiv.symm (transfer_nat_trans_self adj₁ adj₁)) 𝟙 = 𝟙 := sorry
theorem transfer_nat_trans_self_comp {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {L₂ : C ⥤ D} {L₃ : C ⥤ D} {R₁ : D ⥤ C} {R₂ : D ⥤ C} {R₃ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (adj₃ : L₃ ⊣ R₃) (f : L₂ ⟶ L₁) (g : L₃ ⟶ L₂) : coe_fn (transfer_nat_trans_self adj₁ adj₂) f ≫ coe_fn (transfer_nat_trans_self adj₂ adj₃) g =
coe_fn (transfer_nat_trans_self adj₁ adj₃) (g ≫ f) := sorry
theorem transfer_nat_trans_self_symm_comp {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {L₂ : C ⥤ D} {L₃ : C ⥤ D} {R₁ : D ⥤ C} {R₂ : D ⥤ C} {R₃ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (adj₃ : L₃ ⊣ R₃) (f : R₂ ⟶ R₁) (g : R₃ ⟶ R₂) : coe_fn (equiv.symm (transfer_nat_trans_self adj₂ adj₁)) f ≫ coe_fn (equiv.symm (transfer_nat_trans_self adj₃ adj₂)) g =
coe_fn (equiv.symm (transfer_nat_trans_self adj₃ adj₁)) (g ≫ f) := sorry
theorem transfer_nat_trans_self_comm {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {L₂ : C ⥤ D} {R₁ : D ⥤ C} {R₂ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) {f : L₂ ⟶ L₁} {g : L₁ ⟶ L₂} (gf : g ≫ f = 𝟙) : coe_fn (transfer_nat_trans_self adj₁ adj₂) f ≫ coe_fn (transfer_nat_trans_self adj₂ adj₁) g = 𝟙 := sorry
theorem transfer_nat_trans_self_symm_comm {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {L₂ : C ⥤ D} {R₁ : D ⥤ C} {R₂ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) {f : R₁ ⟶ R₂} {g : R₂ ⟶ R₁} (gf : g ≫ f = 𝟙) : coe_fn (equiv.symm (transfer_nat_trans_self adj₁ adj₂)) f ≫ coe_fn (equiv.symm (transfer_nat_trans_self adj₂ adj₁)) g =
𝟙 := sorry
/--
If `f` is an isomorphism, then the transferred natural transformation is an isomorphism.
The converse is given in `transfer_nat_trans_self_of_iso`.
-/
protected instance transfer_nat_trans_self_iso {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {L₂ : C ⥤ D} {R₁ : D ⥤ C} {R₂ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (f : L₂ ⟶ L₁) [is_iso f] : is_iso (coe_fn (transfer_nat_trans_self adj₁ adj₂) f) :=
is_iso.mk (coe_fn (transfer_nat_trans_self adj₂ adj₁) (inv f))
/--
If `f` is an isomorphism, then the un-transferred natural transformation is an isomorphism.
The converse is given in `transfer_nat_trans_self_symm_of_iso`.
-/
protected instance transfer_nat_trans_self_symm_iso {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {L₂ : C ⥤ D} {R₁ : D ⥤ C} {R₂ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (f : R₁ ⟶ R₂) [is_iso f] : is_iso (coe_fn (equiv.symm (transfer_nat_trans_self adj₁ adj₂)) f) :=
is_iso.mk (coe_fn (equiv.symm (transfer_nat_trans_self adj₂ adj₁)) (inv f))
/--
If `f` is a natural transformation whose transferred natural transformation is an isomorphism,
then `f` is an isomorphism.
The converse is given in `transfer_nat_trans_self_iso`.
-/
def transfer_nat_trans_self_of_iso {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {L₂ : C ⥤ D} {R₁ : D ⥤ C} {R₂ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (f : L₂ ⟶ L₁) [is_iso (coe_fn (transfer_nat_trans_self adj₁ adj₂) f)] : is_iso f :=
eq.mpr sorry
(eq.mp sorry
(category_theory.transfer_nat_trans_self_symm_iso adj₁ adj₂ (coe_fn (transfer_nat_trans_self adj₁ adj₂) f)))
/--
If `f` is a natural transformation whose un-transferred natural transformation is an isomorphism,
then `f` is an isomorphism.
The converse is given in `transfer_nat_trans_self_symm_iso`.
-/
def transfer_nat_trans_self_symm_of_iso {C : Type u₁} {D : Type u₂} [category C] [category D] {L₁ : C ⥤ D} {L₂ : C ⥤ D} {R₁ : D ⥤ C} {R₂ : D ⥤ C} (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (f : R₁ ⟶ R₂) [is_iso (coe_fn (equiv.symm (transfer_nat_trans_self adj₁ adj₂)) f)] : is_iso f :=
eq.mpr sorry
(eq.mp sorry
(category_theory.transfer_nat_trans_self_iso adj₁ adj₂ (coe_fn (equiv.symm (transfer_nat_trans_self adj₁ adj₂)) f)))
|
9536592ae7ba3cb678831c595cdc9f35c2f103e7 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/infix_paren_improved.lean | 9ec6945901b329eb204f0fcb3e411d665f5c55a6 | [
"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 | 79 | lean | constant f : Π {α : Type}, α → α → α
infix `**`:60 := f
#check (**)
|
78d0bb0fafdba4119118a567dd5117fb5b2baeba | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebraic_topology/dold_kan/degeneracies.lean | ab0584b0614944e4f4658446526bdd8fd2697bdd | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 6,216 | lean | /-
Copyright (c) 2022 Joël Riou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joël Riou
-/
import algebraic_topology.dold_kan.decomposition
import tactic.fin_cases
/-!
# Behaviour of P_infty with respect to degeneracies
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
For any `X : simplicial_object C` where `C` is an abelian category,
the projector `P_infty : K[X] ⟶ K[X]` is supposed to be the projection
on the normalized subcomplex, parallel to the degenerate subcomplex, i.e.
the subcomplex generated by the images of all `X.σ i`.
In this file, we obtain `degeneracy_comp_P_infty` which states that
if `X : simplicial_object C` with `C` a preadditive category,
`θ : [n] ⟶ Δ'` is a non injective map in `simplex_category`, then
`X.map θ.op ≫ P_infty.f n = 0`. It follows from the more precise
statement vanishing statement `σ_comp_P_eq_zero` for the `P q`.
(See `equivalence.lean` for the general strategy of proof of the Dold-Kan equivalence.)
-/
open category_theory category_theory.category category_theory.limits
category_theory.preadditive opposite
open_locale simplicial dold_kan
namespace algebraic_topology
namespace dold_kan
variables {C : Type*} [category C] [preadditive C]
lemma higher_faces_vanish.comp_σ {Y : C} {X : simplicial_object C} {n b q : ℕ} {φ : Y ⟶ X _[n+1]}
(v : higher_faces_vanish q φ) (hnbq : n + 1 = b + q) :
higher_faces_vanish q (φ ≫ X.σ ⟨b,
by simpa only [hnbq, nat.lt_succ_iff, le_add_iff_nonneg_right] using zero_le q⟩) :=
λ j hj, begin
rw [assoc, simplicial_object.δ_comp_σ_of_gt', fin.pred_succ,
v.comp_δ_eq_zero_assoc _ _ hj, zero_comp],
{ intro hj',
simpa only [hj', hnbq, fin.coe_zero, zero_add, add_comm b, add_assoc, false_and,
add_le_iff_nonpos_right, le_zero_iff, add_eq_zero_iff, nat.one_ne_zero] using hj, },
{ simp only [fin.lt_iff_coe_lt_coe, nat.lt_iff_add_one_le,
fin.succ_mk, fin.coe_mk, fin.coe_succ, add_le_add_iff_right],
linarith, },
end
lemma σ_comp_P_eq_zero (X : simplicial_object C)
{n q : ℕ} (i : fin (n + 1)) (hi : n + 1 ≤ i + q) : (X.σ i) ≫ (P q).f (n + 1) = 0 :=
begin
induction q with q hq generalizing i hi,
{ exfalso,
have h := fin.is_lt i,
linarith, },
{ by_cases n+1 ≤ (i : ℕ) + q,
{ unfold P,
simp only [homological_complex.comp_f, ← assoc],
rw [hq i h, zero_comp], },
{ have hi' : n = (i : ℕ) + q,
{ cases le_iff_exists_add.mp hi with j hj,
rw [← nat.lt_succ_iff, nat.succ_eq_add_one, add_assoc, hj, not_lt,
add_le_iff_nonpos_right, nonpos_iff_eq_zero] at h,
rw [← add_left_inj 1, add_assoc, hj, self_eq_add_right, h], },
cases n,
{ fin_cases i,
rw [show q = 0, by linarith],
unfold P,
simp only [id_comp, homological_complex.add_f_apply, comp_add, homological_complex.id_f,
Hσ, homotopy.null_homotopic_map'_f (c_mk 2 1 rfl) (c_mk 1 0 rfl),
alternating_face_map_complex.obj_d_eq],
erw [hσ'_eq' (zero_add 0).symm, hσ'_eq' (add_zero 1).symm, comp_id,
fin.sum_univ_two, fin.sum_univ_succ, fin.sum_univ_two],
simp only [pow_zero, pow_one, pow_two, fin.coe_zero, fin.coe_one, fin.coe_two,
one_zsmul, neg_zsmul, fin.mk_zero, fin.mk_one, fin.coe_succ, pow_add, one_mul,
neg_mul, neg_neg, fin.succ_zero_eq_one, fin.succ_one_eq_two, comp_neg, neg_comp,
add_comp, comp_add],
erw [simplicial_object.δ_comp_σ_self, simplicial_object.δ_comp_σ_self_assoc,
simplicial_object.δ_comp_σ_succ, comp_id, simplicial_object.δ_comp_σ_of_le X
(show (0 : fin(2)) ≤ fin.cast_succ 0, by rw fin.cast_succ_zero),
simplicial_object.δ_comp_σ_self_assoc, simplicial_object.δ_comp_σ_succ_assoc],
abel, },
{ rw [← id_comp (X.σ i), ← (P_add_Q_f q n.succ : _ = 𝟙 (X.obj _)), add_comp, add_comp],
have v : higher_faces_vanish q ((P q).f n.succ ≫ X.σ i) :=
(higher_faces_vanish.of_P q n).comp_σ hi',
unfold P,
erw [← assoc, v.comp_P_eq_self, homological_complex.add_f_apply,
preadditive.comp_add, comp_id, v.comp_Hσ_eq hi', assoc,
simplicial_object.δ_comp_σ_succ'_assoc, fin.eta,
decomposition_Q n q, sum_comp, sum_comp, finset.sum_eq_zero, add_zero,
add_neg_eq_zero], swap,
{ ext, simp only [fin.coe_mk, fin.coe_succ], },
{ intros j hj,
simp only [true_and, finset.mem_univ, finset.mem_filter] at hj,
simp only [nat.succ_eq_add_one] at hi',
obtain ⟨k, hk⟩ := nat.le.dest (nat.lt_succ_iff.mp (fin.is_lt j)),
rw add_comm at hk,
have hi'' : i = fin.cast_succ ⟨i, by linarith⟩ :=
by { ext, simp only [fin.cast_succ_mk, fin.eta], },
have eq := hq j.rev.succ begin
simp only [← hk, fin.rev_eq j hk.symm, nat.succ_eq_add_one, fin.succ_mk, fin.coe_mk],
linarith,
end,
rw [homological_complex.comp_f, assoc, assoc, assoc, hi'',
simplicial_object.σ_comp_σ_assoc, reassoc_of eq, zero_comp, comp_zero,
comp_zero, comp_zero],
simp only [fin.rev_eq j hk.symm, fin.le_iff_coe_le_coe, fin.coe_mk],
linarith, }, }, }, }
end
@[simp, reassoc]
lemma σ_comp_P_infty (X : simplicial_object C) {n : ℕ} (i : fin (n+1)) :
(X.σ i) ≫ P_infty.f (n+1) = 0 :=
begin
rw [P_infty_f, σ_comp_P_eq_zero X i],
simp only [le_add_iff_nonneg_left, zero_le],
end
@[reassoc]
lemma degeneracy_comp_P_infty (X : simplicial_object C)
(n : ℕ) {Δ' : simplex_category} (θ : [n] ⟶ Δ') (hθ : ¬mono θ) :
X.map θ.op ≫ P_infty.f n = 0 :=
begin
rw simplex_category.mono_iff_injective at hθ,
cases n,
{ exfalso,
apply hθ,
intros x y h,
fin_cases x,
fin_cases y, },
{ obtain ⟨i, α, h⟩ := simplex_category.eq_σ_comp_of_not_injective θ hθ,
rw [h, op_comp, X.map_comp, assoc, (show X.map (simplex_category.σ i).op = X.σ i, by refl),
σ_comp_P_infty, comp_zero], },
end
end dold_kan
end algebraic_topology
|
4835e2c93d0cf9e37f70bf3b57717645308a5c69 | c45b34bfd44d8607a2e8762c926e3cfaa7436201 | /uexp/src/uexp/rules/transitiveInferenceConstantEquiPredicate.lean | ca7504dd921c802766e6c329a6b39e3f79d5bc63 | [
"BSD-2-Clause"
] | permissive | Shamrock-Frost/Cosette | b477c442c07e45082348a145f19ebb35a7f29392 | 24cbc4adebf627f13f5eac878f04ffa20d1209af | refs/heads/master | 1,619,721,304,969 | 1,526,082,841,000 | 1,526,082,841,000 | 121,695,605 | 1 | 0 | null | 1,518,737,210,000 | 1,518,737,210,000 | null | UTF-8 | Lean | false | false | 1,884 | lean | import ..sql
import ..tactics
import ..u_semiring
import ..extra_constants
import ..ucongr
import ..TDP
set_option profiler true
open Expr
open Proj
open Pred
open SQL
open tree
notation `int` := datatypes.int
variable integer_1: const datatypes.int
theorem rule:
forall ( Γ scm_t scm_account scm_bonus scm_dept scm_emp: Schema) (rel_t: relation scm_t) (rel_account: relation scm_account) (rel_bonus: relation scm_bonus) (rel_dept: relation scm_dept) (rel_emp: relation scm_emp) (t_k0 : Column int scm_t) (t_c1 : Column int scm_t) (t_f1_a0 : Column int scm_t) (t_f2_a0 : Column int scm_t) (t_f0_c0 : Column int scm_t) (t_f1_c0 : Column int scm_t) (t_f0_c1 : Column int scm_t) (t_f1_c2 : Column int scm_t) (t_f2_c3 : Column int scm_t) (account_acctno : Column int scm_account) (account_type : Column int scm_account) (account_balance : Column int scm_account) (bonus_ename : Column int scm_bonus) (bonus_job : Column int scm_bonus) (bonus_sal : Column int scm_bonus) (bonus_comm : Column int scm_bonus) (dept_deptno : Column int scm_dept) (dept_name : Column int scm_dept) (emp_empno : Column int scm_emp) (emp_ename : Column int scm_emp) (emp_job : Column int scm_emp) (emp_mgr : Column int scm_emp) (emp_hiredate : Column int scm_emp) (emp_comm : Column int scm_emp) (emp_sal : Column int scm_emp) (emp_deptno : Column int scm_emp) (emp_slacker : Column int scm_emp),
denoteSQL ((SELECT1 (e2p (constantExpr integer_1)) FROM1 (product (table rel_emp) (table rel_emp)) WHERE (equal (uvariable (right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅emp_deptno)))) :SQL Γ _)
=
denoteSQL ((SELECT1 (e2p (constantExpr integer_1)) FROM1 (product (table rel_emp) (table rel_emp)) WHERE (equal (uvariable (right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅emp_deptno)))) :SQL Γ _) :=
begin
intros,
unfold_all_denotations,
funext,
try {simp},
try {TDP' ucongr},
end |
5b2592d2c5b923c8a583ef0713a16f14bfb9d887 | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/order/complete_lattice.lean | 21b492c94817a51faf76d5c3abd4b6a55ea786c1 | [
"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 | 37,305 | 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 order.bounds
/-!
# Theory of complete lattices
## Main definitions
* `Sup` and `Inf` are the supremum and the infimum of a set;
* `supr (f : ι → α)` and `infi (f : ι → α)` are indexed supremum and infimum of a function,
defined as `Sup` and `Inf` of the range of this function;
* `class complete_lattice`: a bounded lattice such that `Sup s` is always the least upper boundary
of `s` and `Inf s` is always the greatest lower boundary of `s`;
* `class complete_linear_order`: a linear ordered complete lattice.
## Naming conventions
We use `Sup`/`Inf`/`supr`/`infi` for the corresponding functions in the statement. Sometimes we
also use `bsupr`/`binfi` for "bounded` supremum or infimum, i.e. one of `⨆ i ∈ s, f i`,
`⨆ i (hi : p i), f i`, or more generally `⨆ i (hi : p i), f i hi`.
## Notation
* `⨆ i, f i` : `supr f`, the supremum of the range of `f`;
* `⨅ i, f i` : `infi f`, the infimum of the range of `f`.
-/
set_option old_structure_cmd true
open set
variables {α β β₂ : Type*} {ι ι₂ : Sort*}
/-- class for the `Sup` operator -/
class has_Sup (α : Type*) := (Sup : set α → α)
/-- class for the `Inf` operator -/
class has_Inf (α : Type*) := (Inf : set α → α)
export has_Sup (Sup) has_Inf (Inf)
/-- Supremum of a set -/
add_decl_doc has_Sup.Sup
/-- Infimum of a set -/
add_decl_doc has_Inf.Inf
/-- Indexed supremum -/
def supr [has_Sup α] {ι} (s : ι → α) : α := Sup (range s)
/-- Indexed infimum -/
def infi [has_Inf α] {ι} (s : ι → α) : α := Inf (range s)
@[priority 50] instance has_Inf_to_nonempty (α) [has_Inf α] : nonempty α := ⟨Inf ∅⟩
@[priority 50] instance has_Sup_to_nonempty (α) [has_Sup α] : nonempty α := ⟨Sup ∅⟩
notation `⨆` binders `, ` r:(scoped f, supr f) := r
notation `⨅` binders `, ` r:(scoped f, infi f) := r
instance (α) [has_Inf α] : has_Sup (order_dual α) := ⟨(Inf : set α → α)⟩
instance (α) [has_Sup α] : has_Inf (order_dual α) := ⟨(Sup : set α → α)⟩
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A complete lattice is a bounded lattice which
has suprema and infima for every subset. -/
class complete_lattice (α : Type*) extends bounded_lattice α, has_Sup α, has_Inf α :=
(le_Sup : ∀s, ∀a∈s, a ≤ Sup s)
(Sup_le : ∀s a, (∀b∈s, b ≤ a) → Sup s ≤ a)
(Inf_le : ∀s, ∀a∈s, Inf s ≤ a)
(le_Inf : ∀s a, (∀b∈s, a ≤ b) → a ≤ Inf s)
/-- Create a `complete_lattice` from a `partial_order` and `Inf` function
that returns the greatest lower bound of a set. Usually this constructor provides
poor definitional equalities, so it should be used with
`.. complete_lattice_of_Inf α _`. -/
def complete_lattice_of_Inf (α : Type*) [H1 : partial_order α]
[H2 : has_Inf α] (is_glb_Inf : ∀ s : set α, is_glb s (Inf s)) :
complete_lattice α :=
{ bot := Inf univ,
bot_le := λ x, (is_glb_Inf univ).1 trivial,
top := Inf ∅,
le_top := λ a, (is_glb_Inf ∅).2 $ by simp,
sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x},
inf := λ a b, Inf {a, b},
le_inf := λ a b c hab hac, by { apply (is_glb_Inf _).2, simp [*] },
inf_le_right := λ a b, (is_glb_Inf _).1 $ mem_insert_of_mem _ $ mem_singleton _,
inf_le_left := λ a b, (is_glb_Inf _).1 $ mem_insert _ _,
sup_le := λ a b c hac hbc, (is_glb_Inf _).1 $ by simp [*],
le_sup_left := λ a b, (is_glb_Inf _).2 $ λ x, and.left,
le_sup_right := λ a b, (is_glb_Inf _).2 $ λ x, and.right,
le_Inf := λ s a ha, (is_glb_Inf s).2 ha,
Inf_le := λ s a ha, (is_glb_Inf s).1 ha,
Sup := λ s, Inf (upper_bounds s),
le_Sup := λ s a ha, (is_glb_Inf (upper_bounds s)).2 $ λ b hb, hb ha,
Sup_le := λ s a ha, (is_glb_Inf (upper_bounds s)).1 ha,
.. H1, .. H2 }
/-- Create a `complete_lattice` from a `partial_order` and `Sup` function
that returns the least upper bound of a set. Usually this constructor provides
poor definitional equalities, so it should be used with
`.. complete_lattice_of_Sup α _`. -/
def complete_lattice_of_Sup (α : Type*) [H1 : partial_order α]
[H2 : has_Sup α] (is_lub_Sup : ∀ s : set α, is_lub s (Sup s)) :
complete_lattice α :=
{ top := Sup univ,
le_top := λ x, (is_lub_Sup univ).1 trivial,
bot := Sup ∅,
bot_le := λ x, (is_lub_Sup ∅).2 $ by simp,
sup := λ a b, Sup {a, b},
sup_le := λ a b c hac hbc, (is_lub_Sup _).2 (by simp [*]),
le_sup_left := λ a b, (is_lub_Sup _).1 $ mem_insert _ _,
le_sup_right := λ a b, (is_lub_Sup _).1 $ mem_insert_of_mem _ $ mem_singleton _,
inf := λ a b, Sup {x | x ≤ a ∧ x ≤ b},
le_inf := λ a b c hab hac, (is_lub_Sup _).1 $ by simp [*],
inf_le_left := λ a b, (is_lub_Sup _).2 (λ x, and.left),
inf_le_right := λ a b, (is_lub_Sup _).2 (λ x, and.right),
Inf := λ s, Sup (lower_bounds s),
Sup_le := λ s a ha, (is_lub_Sup s).2 ha,
le_Sup := λ s a ha, (is_lub_Sup s).1 ha,
Inf_le := λ s a ha, (is_lub_Sup (lower_bounds s)).2 (λ b hb, hb ha),
le_Inf := λ s a ha, (is_lub_Sup (lower_bounds s)).1 ha,
.. H1, .. H2 }
/-- A complete linear order is a linear order whose lattice structure is complete. -/
class complete_linear_order (α : Type*) extends complete_lattice α, decidable_linear_order α
end prio
namespace order_dual
variable (α)
instance [complete_lattice α] : complete_lattice (order_dual α) :=
{ le_Sup := @complete_lattice.Inf_le α _,
Sup_le := @complete_lattice.le_Inf α _,
Inf_le := @complete_lattice.le_Sup α _,
le_Inf := @complete_lattice.Sup_le α _,
.. order_dual.bounded_lattice α, ..order_dual.has_Sup α, ..order_dual.has_Inf α }
instance [complete_linear_order α] : complete_linear_order (order_dual α) :=
{ .. order_dual.complete_lattice α, .. order_dual.decidable_linear_order α }
end order_dual
section
variables [complete_lattice α] {s t : set α} {a b : α}
@[ematch] theorem le_Sup : a ∈ s → a ≤ Sup s := complete_lattice.le_Sup s a
theorem Sup_le : (∀b∈s, b ≤ a) → Sup s ≤ a := complete_lattice.Sup_le s a
@[ematch] theorem Inf_le : a ∈ s → Inf s ≤ a := complete_lattice.Inf_le s a
theorem le_Inf : (∀b∈s, a ≤ b) → a ≤ Inf s := complete_lattice.le_Inf s a
lemma is_lub_Sup (s : set α) : is_lub s (Sup s) := ⟨assume x, le_Sup, assume x, Sup_le⟩
lemma is_lub.Sup_eq (h : is_lub s a) : Sup s = a := (is_lub_Sup s).unique h
lemma is_glb_Inf (s : set α) : is_glb s (Inf s) := ⟨assume a, Inf_le, assume a, le_Inf⟩
lemma is_glb.Inf_eq (h : is_glb s a) : Inf s = a := (is_glb_Inf s).unique h
theorem le_Sup_of_le (hb : b ∈ s) (h : a ≤ b) : a ≤ Sup s :=
le_trans h (le_Sup hb)
theorem Inf_le_of_le (hb : b ∈ s) (h : b ≤ a) : Inf s ≤ a :=
le_trans (Inf_le hb) h
theorem Sup_le_Sup (h : s ⊆ t) : Sup s ≤ Sup t :=
(is_lub_Sup s).mono (is_lub_Sup t) h
theorem Inf_le_Inf (h : s ⊆ t) : Inf t ≤ Inf s :=
(is_glb_Inf s).mono (is_glb_Inf t) h
@[simp] theorem Sup_le_iff : Sup s ≤ a ↔ (∀b ∈ s, b ≤ a) :=
is_lub_le_iff (is_lub_Sup s)
@[simp] theorem le_Inf_iff : a ≤ Inf s ↔ (∀b ∈ s, a ≤ b) :=
le_is_glb_iff (is_glb_Inf s)
theorem Inf_le_Sup (hs : s.nonempty) : Inf s ≤ Sup s :=
is_glb_le_is_lub (is_glb_Inf s) (is_lub_Sup s) hs
-- TODO: it is weird that we have to add union_def
theorem Sup_union {s t : set α} : Sup (s ∪ t) = Sup s ⊔ Sup t :=
((is_lub_Sup s).union (is_lub_Sup t)).Sup_eq
theorem Sup_inter_le {s t : set α} : Sup (s ∩ t) ≤ Sup s ⊓ Sup t :=
by finish
/-
Sup_le (assume a ⟨a_s, a_t⟩, le_inf (le_Sup a_s) (le_Sup a_t))
-/
theorem Inf_union {s t : set α} : Inf (s ∪ t) = Inf s ⊓ Inf t :=
((is_glb_Inf s).union (is_glb_Inf t)).Inf_eq
theorem le_Inf_inter {s t : set α} : Inf s ⊔ Inf t ≤ Inf (s ∩ t) :=
@Sup_inter_le (order_dual α) _ _ _
@[simp] theorem Sup_empty : Sup ∅ = (⊥ : α) :=
is_lub_empty.Sup_eq
@[simp] theorem Inf_empty : Inf ∅ = (⊤ : α) :=
(@is_glb_empty α _).Inf_eq
@[simp] theorem Sup_univ : Sup univ = (⊤ : α) :=
(@is_lub_univ α _).Sup_eq
@[simp] theorem Inf_univ : Inf univ = (⊥ : α) :=
is_glb_univ.Inf_eq
-- TODO(Jeremy): get this automatically
@[simp] theorem Sup_insert {a : α} {s : set α} : Sup (insert a s) = a ⊔ Sup s :=
((is_lub_Sup s).insert a).Sup_eq
@[simp] theorem Inf_insert {a : α} {s : set α} : Inf (insert a s) = a ⊓ Inf s :=
((is_glb_Inf s).insert a).Inf_eq
theorem Sup_le_Sup_of_subset_instert_bot (h : s ⊆ insert ⊥ t) : Sup s ≤ Sup t :=
le_trans (Sup_le_Sup h) (le_of_eq (trans Sup_insert bot_sup_eq))
theorem Inf_le_Inf_of_subset_insert_top (h : s ⊆ insert ⊤ t) : Inf t ≤ Inf s :=
le_trans (le_of_eq (trans top_inf_eq.symm Inf_insert.symm)) (Inf_le_Inf h)
-- We will generalize this to conditionally complete lattices in `cSup_singleton`.
theorem Sup_singleton {a : α} : Sup {a} = a :=
is_lub_singleton.Sup_eq
-- We will generalize this to conditionally complete lattices in `cInf_singleton`.
theorem Inf_singleton {a : α} : Inf {a} = a :=
is_glb_singleton.Inf_eq
theorem Sup_pair {a b : α} : Sup {a, b} = a ⊔ b :=
(@is_lub_pair α _ a b).Sup_eq
theorem Inf_pair {a b : α} : Inf {a, b} = a ⊓ b :=
(@is_glb_pair α _ a b).Inf_eq
@[simp] theorem Inf_eq_top : Inf s = ⊤ ↔ (∀a∈s, a = ⊤) :=
iff.intro
(assume h a ha, top_unique $ h ▸ Inf_le ha)
(assume h, top_unique $ le_Inf $ assume a ha, top_le_iff.2 $ h a ha)
@[simp] theorem Sup_eq_bot : Sup s = ⊥ ↔ (∀a∈s, a = ⊥) :=
@Inf_eq_top (order_dual α) _ _
end
section complete_linear_order
variables [complete_linear_order α] {s t : set α} {a b : α}
lemma Inf_lt_iff : Inf s < b ↔ (∃a∈s, a < b) :=
is_glb_lt_iff (is_glb_Inf s)
lemma lt_Sup_iff : b < Sup s ↔ (∃a∈s, b < a) :=
lt_is_lub_iff (is_lub_Sup s)
lemma Sup_eq_top : Sup s = ⊤ ↔ (∀b<⊤, ∃a∈s, b < a) :=
iff.intro
(assume (h : Sup s = ⊤) b hb, by rwa [←h, lt_Sup_iff] at hb)
(assume h, top_unique $ le_of_not_gt $ assume h',
let ⟨a, ha, h⟩ := h _ h' in
lt_irrefl a $ lt_of_le_of_lt (le_Sup ha) h)
lemma Inf_eq_bot : Inf s = ⊥ ↔ (∀b>⊥, ∃a∈s, a < b) :=
@Sup_eq_top (order_dual α) _ _
lemma lt_supr_iff {f : ι → α} : a < supr f ↔ (∃i, a < f i) :=
lt_Sup_iff.trans exists_range_iff
lemma infi_lt_iff {f : ι → α} : infi f < a ↔ (∃i, f i < a) :=
Inf_lt_iff.trans exists_range_iff
end complete_linear_order
/-
### supr & infi
-/
section
variables [complete_lattice α] {s t : ι → α} {a b : α}
-- TODO: this declaration gives error when starting smt state
--@[ematch]
theorem le_supr (s : ι → α) (i : ι) : s i ≤ supr s :=
le_Sup ⟨i, rfl⟩
@[ematch] theorem le_supr' (s : ι → α) (i : ι) : (: s i ≤ supr s :) :=
le_Sup ⟨i, rfl⟩
/- TODO: this version would be more powerful, but, alas, the pattern matcher
doesn't accept it.
@[ematch] theorem le_supr' (s : ι → α) (i : ι) : (: s i :) ≤ (: supr s :) :=
le_Sup ⟨i, rfl⟩
-/
lemma is_lub_supr : is_lub (range s) (⨆j, s j) := is_lub_Sup _
lemma is_lub.supr_eq (h : is_lub (range s) a) : (⨆j, s j) = a := h.Sup_eq
lemma is_glb_infi : is_glb (range s) (⨅j, s j) := is_glb_Inf _
lemma is_glb.infi_eq (h : is_glb (range s) a) : (⨅j, s j) = a := h.Inf_eq
theorem le_supr_of_le (i : ι) (h : a ≤ s i) : a ≤ supr s :=
le_trans h (le_supr _ i)
theorem le_bsupr {p : ι → Prop} {f : Π i (h : p i), α} (i : ι) (hi : p i) :
f i hi ≤ ⨆ i hi, f i hi :=
le_supr_of_le i $ le_supr (f i) hi
theorem supr_le (h : ∀i, s i ≤ a) : supr s ≤ a :=
Sup_le $ assume b ⟨i, eq⟩, eq ▸ h i
theorem bsupr_le {p : ι → Prop} {f : Π i (h : p i), α} (h : ∀ i hi, f i hi ≤ a) :
(⨆ i (hi : p i), f i hi) ≤ a :=
supr_le $ λ i, supr_le $ h i
theorem bsupr_le_supr (p : ι → Prop) (f : ι → α) : (⨆ i (H : p i), f i) ≤ ⨆ i, f i :=
bsupr_le (λ i hi, le_supr f i)
theorem supr_le_supr (h : ∀i, s i ≤ t i) : supr s ≤ supr t :=
supr_le $ assume i, le_supr_of_le i (h i)
theorem supr_le_supr2 {t : ι₂ → α} (h : ∀i, ∃j, s i ≤ t j) : supr s ≤ supr t :=
supr_le $ assume j, exists.elim (h j) le_supr_of_le
theorem bsupr_le_bsupr {p : ι → Prop} {f g : Π i (hi : p i), α} (h : ∀ i hi, f i hi ≤ g i hi) :
(⨆ i hi, f i hi) ≤ ⨆ i hi, g i hi :=
bsupr_le $ λ i hi, le_trans (h i hi) (le_bsupr i hi)
theorem supr_le_supr_const (h : ι → ι₂) : (⨆ i:ι, a) ≤ (⨆ j:ι₂, a) :=
supr_le $ le_supr _ ∘ h
@[simp] theorem supr_le_iff : supr s ≤ a ↔ (∀i, s i ≤ a) :=
(is_lub_le_iff is_lub_supr).trans forall_range_iff
theorem Sup_eq_supr {s : set α} : Sup s = (⨆a ∈ s, a) :=
le_antisymm
(Sup_le $ assume b h, le_supr_of_le b $ le_supr _ h)
(supr_le $ assume b, supr_le $ assume h, le_Sup h)
lemma le_supr_iff : (a ≤ supr s) ↔ (∀ b, (∀ i, s i ≤ b) → a ≤ b) :=
⟨λ h b hb, le_trans h (supr_le hb), λ h, h _ $ λ i, le_supr s i⟩
lemma monotone.le_map_supr [complete_lattice β] {f : α → β} (hf : monotone f) :
(⨆ i, f (s i)) ≤ f (supr s) :=
supr_le $ λ i, hf $ le_supr _ _
lemma monotone.le_map_supr2 [complete_lattice β] {f : α → β} (hf : monotone f)
{ι' : ι → Sort*} (s : Π i, ι' i → α) :
(⨆ i (h : ι' i), f (s i h)) ≤ f (⨆ i (h : ι' i), s i h) :=
calc (⨆ i h, f (s i h)) ≤ (⨆ i, f (⨆ h, s i h)) :
supr_le_supr $ λ i, hf.le_map_supr
... ≤ f (⨆ i (h : ι' i), s i h) : hf.le_map_supr
lemma monotone.le_map_Sup [complete_lattice β] {s : set α} {f : α → β} (hf : monotone f) :
(⨆a∈s, f a) ≤ f (Sup s) :=
by rw [Sup_eq_supr]; exact hf.le_map_supr2 _
lemma supr_comp_le {ι' : Sort*} (f : ι' → α) (g : ι → ι') :
(⨆ x, f (g x)) ≤ ⨆ y, f y :=
supr_le_supr2 $ λ x, ⟨_, le_refl _⟩
lemma monotone.supr_comp_eq [preorder β] {f : β → α} (hf : monotone f)
{s : ι → β} (hs : ∀ x, ∃ i, x ≤ s i) :
(⨆ x, f (s x)) = ⨆ y, f y :=
le_antisymm (supr_comp_le _ _) (supr_le_supr2 $ λ x, (hs x).imp $ λ i hi, hf hi)
lemma function.surjective.supr_comp {α : Type*} [has_Sup α] {f : ι → ι₂}
(hf : function.surjective f) (g : ι₂ → α) :
(⨆ x, g (f x)) = ⨆ y, g y :=
by simp only [supr, hf.range_comp]
lemma supr_congr {α : Type*} [has_Sup α] {f : ι → α} {g : ι₂ → α} (h : ι → ι₂)
(h1 : function.surjective h) (h2 : ∀ x, g (h x) = f x) : (⨆ x, f x) = ⨆ y, g y :=
by { convert h1.supr_comp g, exact (funext h2).symm }
-- TODO: finish doesn't do well here.
@[congr] theorem supr_congr_Prop {α : Type*} [has_Sup α] {p q : Prop} {f₁ : p → α} {f₂ : q → α}
(pq : p ↔ q) (f : ∀x, f₁ (pq.mpr x) = f₂ x) : supr f₁ = supr f₂ :=
begin
have : f₁ ∘ pq.mpr = f₂ := funext f,
rw [← this],
refine (function.surjective.supr_comp (λ h, ⟨pq.1 h, _⟩) f₁).symm,
refl
end
theorem infi_le (s : ι → α) (i : ι) : infi s ≤ s i :=
Inf_le ⟨i, rfl⟩
@[ematch] theorem infi_le' (s : ι → α) (i : ι) : (: infi s ≤ s i :) :=
Inf_le ⟨i, rfl⟩
theorem infi_le_of_le (i : ι) (h : s i ≤ a) : infi s ≤ a :=
le_trans (infi_le _ i) h
theorem binfi_le {p : ι → Prop} {f : Π i (hi : p i), α} (i : ι) (hi : p i) :
(⨅ i hi, f i hi) ≤ f i hi :=
infi_le_of_le i $ infi_le (f i) hi
theorem le_infi (h : ∀i, a ≤ s i) : a ≤ infi s :=
le_Inf $ assume b ⟨i, eq⟩, eq ▸ h i
theorem le_binfi {p : ι → Prop} {f : Π i (h : p i), α} (h : ∀ i hi, a ≤ f i hi) :
a ≤ ⨅ i hi, f i hi :=
le_infi $ λ i, le_infi $ h i
theorem infi_le_binfi (p : ι → Prop) (f : ι → α) : (⨅ i, f i) ≤ ⨅ i (H : p i), f i :=
le_binfi (λ i hi, infi_le f i)
theorem infi_le_infi (h : ∀i, s i ≤ t i) : infi s ≤ infi t :=
le_infi $ assume i, infi_le_of_le i (h i)
theorem infi_le_infi2 {t : ι₂ → α} (h : ∀j, ∃i, s i ≤ t j) : infi s ≤ infi t :=
le_infi $ assume j, exists.elim (h j) infi_le_of_le
theorem binfi_le_binfi {p : ι → Prop} {f g : Π i (h : p i), α} (h : ∀ i hi, f i hi ≤ g i hi) :
(⨅ i hi, f i hi) ≤ ⨅ i hi, g i hi :=
le_binfi $ λ i hi, le_trans (binfi_le i hi) (h i hi)
theorem infi_le_infi_const (h : ι₂ → ι) : (⨅ i:ι, a) ≤ (⨅ j:ι₂, a) :=
le_infi $ infi_le _ ∘ h
@[simp] theorem le_infi_iff : a ≤ infi s ↔ (∀i, a ≤ s i) :=
⟨assume : a ≤ infi s, assume i, le_trans this (infi_le _ _), le_infi⟩
theorem Inf_eq_infi {s : set α} : Inf s = (⨅a ∈ s, a) :=
@Sup_eq_supr (order_dual α) _ _
lemma monotone.map_infi_le [complete_lattice β] {f : α → β} (hf : monotone f) :
f (infi s) ≤ (⨅ i, f (s i)) :=
le_infi $ λ i, hf $ infi_le _ _
lemma monotone.map_infi2_le [complete_lattice β] {f : α → β} (hf : monotone f)
{ι' : ι → Sort*} (s : Π i, ι' i → α) :
f (⨅ i (h : ι' i), s i h) ≤ (⨅ i (h : ι' i), f (s i h)) :=
@monotone.le_map_supr2 (order_dual α) (order_dual β) _ _ _ f hf.order_dual _ _
lemma monotone.map_Inf_le [complete_lattice β] {s : set α} {f : α → β} (hf : monotone f) :
f (Inf s) ≤ ⨅ a∈s, f a :=
by rw [Inf_eq_infi]; exact hf.map_infi2_le _
lemma le_infi_comp {ι' : Sort*} (f : ι' → α) (g : ι → ι') :
(⨅ y, f y) ≤ ⨅ x, f (g x) :=
infi_le_infi2 $ λ x, ⟨_, le_refl _⟩
lemma monotone.infi_comp_eq [preorder β] {f : β → α} (hf : monotone f)
{s : ι → β} (hs : ∀ x, ∃ i, s i ≤ x) :
(⨅ x, f (s x)) = ⨅ y, f y :=
le_antisymm (infi_le_infi2 $ λ x, (hs x).imp $ λ i hi, hf hi) (le_infi_comp _ _)
lemma function.surjective.infi_comp {α : Type*} [has_Inf α] {f : ι → ι₂}
(hf : function.surjective f) (g : ι₂ → α) :
(⨅ x, g (f x)) = ⨅ y, g y :=
@function.surjective.supr_comp _ _ (order_dual α) _ f hf g
lemma infi_congr {α : Type*} [has_Inf α] {f : ι → α} {g : ι₂ → α} (h : ι → ι₂)
(h1 : function.surjective h) (h2 : ∀ x, g (h x) = f x) : (⨅ x, f x) = ⨅ y, g y :=
@supr_congr _ _ (order_dual α) _ _ _ h h1 h2
@[congr] theorem infi_congr_Prop {α : Type*} [has_Inf α] {p q : Prop} {f₁ : p → α} {f₂ : q → α}
(pq : p ↔ q) (f : ∀x, f₁ (pq.mpr x) = f₂ x) : infi f₁ = infi f₂ :=
@supr_congr_Prop (order_dual α) _ p q f₁ f₂ pq f
-- We will generalize this to conditionally complete lattices in `cinfi_const`.
theorem infi_const [nonempty ι] {a : α} : (⨅ b:ι, a) = a :=
by rw [infi, range_const, Inf_singleton]
-- We will generalize this to conditionally complete lattices in `csupr_const`.
theorem supr_const [nonempty ι] {a : α} : (⨆ b:ι, a) = a :=
@infi_const (order_dual α) _ _ _ _
@[simp] lemma infi_top : (⨅i:ι, ⊤ : α) = ⊤ :=
top_unique $ le_infi $ assume i, le_refl _
@[simp] lemma supr_bot : (⨆i:ι, ⊥ : α) = ⊥ :=
@infi_top (order_dual α) _ _
@[simp] lemma infi_eq_top : infi s = ⊤ ↔ (∀i, s i = ⊤) :=
Inf_eq_top.trans forall_range_iff
@[simp] lemma supr_eq_bot : supr s = ⊥ ↔ (∀i, s i = ⊥) :=
Sup_eq_bot.trans forall_range_iff
@[simp] lemma infi_pos {p : Prop} {f : p → α} (hp : p) : (⨅ h : p, f h) = f hp :=
le_antisymm (infi_le _ _) (le_infi $ assume h, le_refl _)
@[simp] lemma infi_neg {p : Prop} {f : p → α} (hp : ¬ p) : (⨅ h : p, f h) = ⊤ :=
le_antisymm le_top $ le_infi $ assume h, (hp h).elim
@[simp] lemma supr_pos {p : Prop} {f : p → α} (hp : p) : (⨆ h : p, f h) = f hp :=
le_antisymm (supr_le $ assume h, le_refl _) (le_supr _ _)
@[simp] lemma supr_neg {p : Prop} {f : p → α} (hp : ¬ p) : (⨆ h : p, f h) = ⊥ :=
le_antisymm (supr_le $ assume h, (hp h).elim) bot_le
lemma supr_eq_dif {p : Prop} [decidable p] (a : p → α) :
(⨆h:p, a h) = (if h : p then a h else ⊥) :=
by by_cases p; simp [h]
lemma supr_eq_if {p : Prop} [decidable p] (a : α) :
(⨆h:p, a) = (if p then a else ⊥) :=
supr_eq_dif (λ _, a)
lemma infi_eq_dif {p : Prop} [decidable p] (a : p → α) :
(⨅h:p, a h) = (if h : p then a h else ⊤) :=
@supr_eq_dif (order_dual α) _ _ _ _
lemma infi_eq_if {p : Prop} [decidable p] (a : α) :
(⨅h:p, a) = (if p then a else ⊤) :=
infi_eq_dif (λ _, a)
-- TODO: should this be @[simp]?
theorem infi_comm {f : ι → ι₂ → α} : (⨅i, ⨅j, f i j) = (⨅j, ⨅i, f i j) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume j, infi_le_of_le j $ infi_le _ i)
(le_infi $ assume j, le_infi $ assume i, infi_le_of_le i $ infi_le _ j)
/- TODO: this is strange. In the proof below, we get exactly the desired
among the equalities, but close does not get it.
begin
apply @le_antisymm,
simp, intros,
begin [smt]
ematch, ematch, ematch, trace_state, have := le_refl (f i_1 i),
trace_state, close
end
end
-/
-- TODO: should this be @[simp]?
theorem supr_comm {f : ι → ι₂ → α} : (⨆i, ⨆j, f i j) = (⨆j, ⨆i, f i j) :=
@infi_comm (order_dual α) _ _ _ _
@[simp] theorem infi_infi_eq_left {b : β} {f : Πx:β, x = b → α} :
(⨅x, ⨅h:x = b, f x h) = f b rfl :=
le_antisymm
(infi_le_of_le b $ infi_le _ rfl)
(le_infi $ assume b', le_infi $ assume eq, match b', eq with ._, rfl := le_refl _ end)
@[simp] theorem infi_infi_eq_right {b : β} {f : Πx:β, b = x → α} :
(⨅x, ⨅h:b = x, f x h) = f b rfl :=
le_antisymm
(infi_le_of_le b $ infi_le _ rfl)
(le_infi $ assume b', le_infi $ assume eq, match b', eq with ._, rfl := le_refl _ end)
@[simp] theorem supr_supr_eq_left {b : β} {f : Πx:β, x = b → α} :
(⨆x, ⨆h : x = b, f x h) = f b rfl :=
@infi_infi_eq_left (order_dual α) _ _ _ _
@[simp] theorem supr_supr_eq_right {b : β} {f : Πx:β, b = x → α} :
(⨆x, ⨆h : b = x, f x h) = f b rfl :=
@infi_infi_eq_right (order_dual α) _ _ _ _
attribute [ematch] le_refl
theorem infi_subtype {p : ι → Prop} {f : subtype p → α} : (⨅ x, f x) = (⨅ i (h:p i), f ⟨i, h⟩) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume : p i, infi_le _ _)
(le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _)
lemma infi_subtype' {p : ι → Prop} {f : ∀ i, p i → α} :
(⨅ i (h : p i), f i h) = (⨅ x : subtype p, f x x.property) :=
(@infi_subtype _ _ _ p (λ x, f x.val x.property)).symm
lemma infi_subtype'' {ι} (s : set ι) (f : ι → α) :
(⨅ i : s, f i) = ⨅ (t : ι) (H : t ∈ s), f t :=
infi_subtype
theorem infi_inf_eq {f g : ι → α} : (⨅ x, f x ⊓ g x) = (⨅ x, f x) ⊓ (⨅ x, g x) :=
le_antisymm
(le_inf
(le_infi $ assume i, infi_le_of_le i inf_le_left)
(le_infi $ assume i, infi_le_of_le i inf_le_right))
(le_infi $ assume i, le_inf
(inf_le_left_of_le $ infi_le _ _)
(inf_le_right_of_le $ infi_le _ _))
/- TODO: here is another example where more flexible pattern matching
might help.
begin
apply @le_antisymm,
safe, pose h := f a ⊓ g a, begin [smt] ematch, ematch end
end
-/
lemma infi_inf [h : nonempty ι] {f : ι → α} {a : α} : (⨅x, f x) ⊓ a = (⨅ x, f x ⊓ a) :=
by rw [infi_inf_eq, infi_const]
lemma inf_infi [nonempty ι] {f : ι → α} {a : α} : a ⊓ (⨅x, f x) = (⨅ x, a ⊓ f x) :=
by rw [inf_comm, infi_inf]; simp [inf_comm]
lemma binfi_inf {p : ι → Prop} {f : Π i (hi : p i), α} {a : α} (h : ∃ i, p i) :
(⨅i (h : p i), f i h) ⊓ a = (⨅ i (h : p i), f i h ⊓ a) :=
by haveI : nonempty {i // p i} := (let ⟨i, hi⟩ := h in ⟨⟨i, hi⟩⟩);
rw [infi_subtype', infi_subtype', infi_inf]
theorem supr_sup_eq {f g : β → α} : (⨆ x, f x ⊔ g x) = (⨆ x, f x) ⊔ (⨆ x, g x) :=
@infi_inf_eq (order_dual α) β _ _ _
/- supr and infi under Prop -/
@[simp] theorem infi_false {s : false → α} : infi s = ⊤ :=
le_antisymm le_top (le_infi $ assume i, false.elim i)
@[simp] theorem supr_false {s : false → α} : supr s = ⊥ :=
le_antisymm (supr_le $ assume i, false.elim i) bot_le
@[simp] theorem infi_true {s : true → α} : infi s = s trivial :=
le_antisymm (infi_le _ _) (le_infi $ assume ⟨⟩, le_refl _)
@[simp] theorem supr_true {s : true → α} : supr s = s trivial :=
le_antisymm (supr_le $ assume ⟨⟩, le_refl _) (le_supr _ _)
@[simp] theorem infi_exists {p : ι → Prop} {f : Exists p → α} :
(⨅ x, f x) = (⨅ i, ⨅ h:p i, f ⟨i, h⟩) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume : p i, infi_le _ _)
(le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _)
@[simp] theorem supr_exists {p : ι → Prop} {f : Exists p → α} :
(⨆ x, f x) = (⨆ i, ⨆ h:p i, f ⟨i, h⟩) :=
@infi_exists (order_dual α) _ _ _ _
theorem infi_and {p q : Prop} {s : p ∧ q → α} : infi s = (⨅ h₁ h₂, s ⟨h₁, h₂⟩) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume j, infi_le _ _)
(le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _)
/-- The symmetric case of `infi_and`, useful for rewriting into a infimum over a conjunction -/
lemma infi_and' {p q : Prop} {s : p → q → α} :
(⨅ (h₁ : p) (h₂ : q), s h₁ h₂) = ⨅ (h : p ∧ q), s h.1 h.2 :=
by { symmetry, exact infi_and }
theorem supr_and {p q : Prop} {s : p ∧ q → α} : supr s = (⨆ h₁ h₂, s ⟨h₁, h₂⟩) :=
@infi_and (order_dual α) _ _ _ _
/-- The symmetric case of `supr_and`, useful for rewriting into a supremum over a conjunction -/
lemma supr_and' {p q : Prop} {s : p → q → α} :
(⨆ (h₁ : p) (h₂ : q), s h₁ h₂) = ⨆ (h : p ∧ q), s h.1 h.2 :=
by { symmetry, exact supr_and }
theorem infi_or {p q : Prop} {s : p ∨ q → α} :
infi s = (⨅ h : p, s (or.inl h)) ⊓ (⨅ h : q, s (or.inr h)) :=
le_antisymm
(le_inf
(infi_le_infi2 $ assume j, ⟨_, le_refl _⟩)
(infi_le_infi2 $ assume j, ⟨_, le_refl _⟩))
(le_infi $ assume i, match i with
| or.inl i := inf_le_left_of_le $ infi_le _ _
| or.inr j := inf_le_right_of_le $ infi_le _ _
end)
theorem supr_or {p q : Prop} {s : p ∨ q → α} :
(⨆ x, s x) = (⨆ i, s (or.inl i)) ⊔ (⨆ j, s (or.inr j)) :=
@infi_or (order_dual α) _ _ _ _
lemma Sup_range {α : Type*} [has_Sup α] {f : ι → α} : Sup (range f) = supr f := rfl
lemma Inf_range {α : Type*} [has_Inf α] {f : ι → α} : Inf (range f) = infi f := rfl
lemma supr_range {g : β → α} {f : ι → β} : (⨆b∈range f, g b) = (⨆i, g (f i)) :=
le_antisymm
(supr_le $ assume b, supr_le $ assume ⟨i, (h : f i = b)⟩, h ▸ le_supr _ i)
(supr_le $ assume i, le_supr_of_le (f i) $ le_supr (λp, g (f i)) (mem_range_self _))
lemma infi_range {g : β → α} {f : ι → β} : (⨅b∈range f, g b) = (⨅i, g (f i)) :=
@supr_range (order_dual α) _ _ _ _ _
theorem Inf_image {s : set β} {f : β → α} : Inf (f '' s) = (⨅ a ∈ s, f a) :=
by rw [← infi_subtype'', infi, range_comp, subtype.range_coe]
theorem Sup_image {s : set β} {f : β → α} : Sup (f '' s) = (⨆ a ∈ s, f a) :=
@Inf_image (order_dual α) _ _ _ _
/-
### supr and infi under set constructions
-/
theorem infi_emptyset {f : β → α} : (⨅ x ∈ (∅ : set β), f x) = ⊤ :=
by simp
theorem supr_emptyset {f : β → α} : (⨆ x ∈ (∅ : set β), f x) = ⊥ :=
by simp
theorem infi_univ {f : β → α} : (⨅ x ∈ (univ : set β), f x) = (⨅ x, f x) :=
by simp
theorem supr_univ {f : β → α} : (⨆ x ∈ (univ : set β), f x) = (⨆ x, f x) :=
by simp
theorem infi_union {f : β → α} {s t : set β} : (⨅ x ∈ s ∪ t, f x) = (⨅x∈s, f x) ⊓ (⨅x∈t, f x) :=
by simp only [← infi_inf_eq, infi_or]
lemma infi_split (f : β → α) (p : β → Prop) :
(⨅ i, f i) = (⨅ i (h : p i), f i) ⊓ (⨅ i (h : ¬ p i), f i) :=
by simpa [classical.em] using @infi_union _ _ _ f {i | p i} {i | ¬ p i}
lemma infi_split_single (f : β → α) (i₀ : β) :
(⨅ i, f i) = f i₀ ⊓ (⨅ i (h : i ≠ i₀), f i) :=
by convert infi_split _ _; simp
theorem infi_le_infi_of_subset {f : β → α} {s t : set β} (h : s ⊆ t) :
(⨅ x ∈ t, f x) ≤ (⨅ x ∈ s, f x) :=
by rw [(union_eq_self_of_subset_left h).symm, infi_union]; exact inf_le_left
theorem supr_union {f : β → α} {s t : set β} : (⨆ x ∈ s ∪ t, f x) = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) :=
@infi_union (order_dual α) _ _ _ _ _
lemma supr_split (f : β → α) (p : β → Prop) :
(⨆ i, f i) = (⨆ i (h : p i), f i) ⊔ (⨆ i (h : ¬ p i), f i) :=
@infi_split (order_dual α) _ _ _ _
lemma supr_split_single (f : β → α) (i₀ : β) :
(⨆ i, f i) = f i₀ ⊔ (⨆ i (h : i ≠ i₀), f i) :=
@infi_split_single (order_dual α) _ _ _ _
theorem supr_le_supr_of_subset {f : β → α} {s t : set β} (h : s ⊆ t) :
(⨆ x ∈ s, f x) ≤ (⨆ x ∈ t, f x) :=
@infi_le_infi_of_subset (order_dual α) _ _ _ _ _ h
theorem infi_insert {f : β → α} {s : set β} {b : β} :
(⨅ x ∈ insert b s, f x) = f b ⊓ (⨅x∈s, f x) :=
eq.trans infi_union $ congr_arg (λx:α, x ⊓ (⨅x∈s, f x)) infi_infi_eq_left
theorem supr_insert {f : β → α} {s : set β} {b : β} :
(⨆ x ∈ insert b s, f x) = f b ⊔ (⨆x∈s, f x) :=
eq.trans supr_union $ congr_arg (λx:α, x ⊔ (⨆x∈s, f x)) supr_supr_eq_left
theorem infi_singleton {f : β → α} {b : β} : (⨅ x ∈ (singleton b : set β), f x) = f b :=
by simp
theorem infi_pair {f : β → α} {a b : β} : (⨅ x ∈ ({a, b} : set β), f x) = f a ⊓ f b :=
by rw [infi_insert, infi_singleton]
theorem supr_singleton {f : β → α} {b : β} : (⨆ x ∈ (singleton b : set β), f x) = f b :=
@infi_singleton (order_dual α) _ _ _ _
theorem supr_pair {f : β → α} {a b : β} : (⨆ x ∈ ({a, b} : set β), f x) = f a ⊔ f b :=
by rw [supr_insert, supr_singleton]
lemma infi_image {γ} {f : β → γ} {g : γ → α} {t : set β} :
(⨅ c ∈ f '' t, g c) = (⨅ b ∈ t, g (f b)) :=
by rw [← Inf_image, ← Inf_image, ← image_comp]
lemma supr_image {γ} {f : β → γ} {g : γ → α} {t : set β} :
(⨆ c ∈ f '' t, g c) = (⨆ b ∈ t, g (f b)) :=
@infi_image (order_dual α) _ _ _ _ _ _
/-!
### `supr` and `infi` under `Type`
-/
theorem infi_of_empty' (h : ι → false) {s : ι → α} : infi s = ⊤ :=
top_unique (le_infi $ assume i, (h i).elim)
theorem supr_of_empty' (h : ι → false) {s : ι → α} : supr s = ⊥ :=
bot_unique (supr_le $ assume i, (h i).elim)
theorem infi_of_empty (h : ¬nonempty ι) {s : ι → α} : infi s = ⊤ :=
infi_of_empty' (λ i, h ⟨i⟩)
theorem supr_of_empty (h : ¬nonempty ι) {s : ι → α} : supr s = ⊥ :=
supr_of_empty' (λ i, h ⟨i⟩)
@[simp] theorem infi_empty {s : empty → α} : infi s = ⊤ :=
infi_of_empty nonempty_empty
@[simp] theorem supr_empty {s : empty → α} : supr s = ⊥ :=
supr_of_empty nonempty_empty
@[simp] theorem infi_unit {f : unit → α} : (⨅ x, f x) = f () :=
le_antisymm (infi_le _ _) (le_infi $ assume ⟨⟩, le_refl _)
@[simp] theorem supr_unit {f : unit → α} : (⨆ x, f x) = f () :=
le_antisymm (supr_le $ assume ⟨⟩, le_refl _) (le_supr _ _)
lemma supr_bool_eq {f : bool → α} : (⨆b:bool, f b) = f tt ⊔ f ff :=
le_antisymm
(supr_le $ assume b, match b with tt := le_sup_left | ff := le_sup_right end)
(sup_le (le_supr _ _) (le_supr _ _))
lemma infi_bool_eq {f : bool → α} : (⨅b:bool, f b) = f tt ⊓ f ff :=
@supr_bool_eq (order_dual α) _ _
lemma is_glb_binfi {s : set β} {f : β → α} : is_glb (f '' s) (⨅ x ∈ s, f x) :=
by simpa only [range_comp, subtype.range_coe, infi_subtype'] using @is_glb_infi α s _ (f ∘ coe)
theorem supr_subtype {p : ι → Prop} {f : subtype p → α} : (⨆ x, f x) = (⨆ i (h:p i), f ⟨i, h⟩) :=
@infi_subtype (order_dual α) _ _ _ _
lemma supr_subtype' {p : ι → Prop} {f : ∀ i, p i → α} :
(⨆ i (h : p i), f i h) = (⨆ x : subtype p, f x x.property) :=
(@supr_subtype _ _ _ p (λ x, f x.val x.property)).symm
lemma Sup_eq_supr' {s : set α} : Sup s = ⨆ x : s, (x : α) :=
by rw [Sup_eq_supr, supr_subtype']; refl
lemma is_lub_bsupr {s : set β} {f : β → α} : is_lub (f '' s) (⨆ x ∈ s, f x) :=
by simpa only [range_comp, subtype.range_coe, supr_subtype'] using @is_lub_supr α s _ (f ∘ coe)
theorem infi_sigma {p : β → Type*} {f : sigma p → α} : (⨅ x, f x) = (⨅ i (h:p i), f ⟨i, h⟩) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume : p i, infi_le _ _)
(le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _)
theorem supr_sigma {p : β → Type*} {f : sigma p → α} : (⨆ x, f x) = (⨆ i (h:p i), f ⟨i, h⟩) :=
@infi_sigma (order_dual α) _ _ _ _
theorem infi_prod {γ : Type*} {f : β × γ → α} : (⨅ x, f x) = (⨅ i j, f (i, j)) :=
le_antisymm
(le_infi $ assume i, le_infi $ assume j, infi_le _ _)
(le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _)
theorem supr_prod {γ : Type*} {f : β × γ → α} : (⨆ x, f x) = (⨆ i j, f (i, j)) :=
@infi_prod (order_dual α) _ _ _ _
theorem infi_sum {γ : Type*} {f : β ⊕ γ → α} :
(⨅ x, f x) = (⨅ i, f (sum.inl i)) ⊓ (⨅ j, f (sum.inr j)) :=
le_antisymm
(le_inf
(infi_le_infi2 $ assume i, ⟨_, le_refl _⟩)
(infi_le_infi2 $ assume j, ⟨_, le_refl _⟩))
(le_infi $ assume s, match s with
| sum.inl i := inf_le_left_of_le $ infi_le _ _
| sum.inr j := inf_le_right_of_le $ infi_le _ _
end)
theorem supr_sum {γ : Type*} {f : β ⊕ γ → α} :
(⨆ x, f x) = (⨆ i, f (sum.inl i)) ⊔ (⨆ j, f (sum.inr j)) :=
@infi_sum (order_dual α) _ _ _ _
end
section complete_linear_order
variables [complete_linear_order α]
lemma supr_eq_top (f : ι → α) : supr f = ⊤ ↔ (∀b<⊤, ∃i, b < f i) :=
by simp only [← Sup_range, Sup_eq_top, set.exists_range_iff]
lemma infi_eq_bot (f : ι → α) : infi f = ⊥ ↔ (∀b>⊥, ∃i, f i < b) :=
by simp only [← Inf_range, Inf_eq_bot, set.exists_range_iff]
end complete_linear_order
/-!
### Instances
-/
instance complete_lattice_Prop : complete_lattice Prop :=
{ Sup := λs, ∃a∈s, a,
le_Sup := assume s a h p, ⟨a, h, p⟩,
Sup_le := assume s a h ⟨b, h', p⟩, h b h' p,
Inf := λs, ∀a:Prop, a∈s → a,
Inf_le := assume s a h p, p a h,
le_Inf := assume s a h p b hb, h b hb p,
.. bounded_distrib_lattice_Prop }
lemma Inf_Prop_eq {s : set Prop} : Inf s = (∀p ∈ s, p) := rfl
lemma Sup_Prop_eq {s : set Prop} : Sup s = (∃p ∈ s, p) := rfl
lemma infi_Prop_eq {ι : Sort*} {p : ι → Prop} : (⨅i, p i) = (∀i, p i) :=
le_antisymm (assume h i, h _ ⟨i, rfl⟩ ) (assume h p ⟨i, eq⟩, eq ▸ h i)
lemma supr_Prop_eq {ι : Sort*} {p : ι → Prop} : (⨆i, p i) = (∃i, p i) :=
le_antisymm (λ ⟨q, ⟨i, (eq : p i = q)⟩, hq⟩, ⟨i, eq.symm ▸ hq⟩) (λ ⟨i, hi⟩, ⟨p i, ⟨i, rfl⟩, hi⟩)
instance pi.has_Sup {α : Type*} {β : α → Type*} [Π i, has_Sup (β i)] : has_Sup (Π i, β i) :=
⟨λ s i, ⨆ f : s, (f : Π i, β i) i⟩
instance pi.has_Inf {α : Type*} {β : α → Type*} [Π i, has_Inf (β i)] : has_Inf (Π i, β i) :=
⟨λ s i, ⨅ f : s, (f : Π i, β i) i⟩
instance pi.complete_lattice {α : Type*} {β : α → Type*} [∀ i, complete_lattice (β i)] :
complete_lattice (Π i, β i) :=
{ Sup := Sup,
Inf := Inf,
le_Sup := λ s f hf i, le_supr (λ f : s, (f : Π i, β i) i) ⟨f, hf⟩,
Inf_le := λ s f hf i, infi_le (λ f : s, (f : Π i, β i) i) ⟨f, hf⟩,
Sup_le := λ s f hf i, supr_le $ λ g, hf g g.2 i,
le_Inf := λ s f hf i, le_infi $ λ g, hf g g.2 i,
.. pi.bounded_lattice }
lemma Inf_apply {α : Type*} {β : α → Type*} [Π i, has_Inf (β i)]
{s : set (Πa, β a)} {a : α} :
(Inf s) a = (⨅ f : s, (f : Πa, β a) a) :=
rfl
lemma infi_apply {α : Type*} {β : α → Type*} {ι : Sort*} [Π i, has_Inf (β i)]
{f : ι → Πa, β a} {a : α} :
(⨅i, f i) a = (⨅i, f i a) :=
by rw [infi, Inf_apply, infi, infi, ← image_eq_range (λ f : Π i, β i, f a) (range f), ← range_comp]
lemma Sup_apply {α : Type*} {β : α → Type*} [Π i, has_Sup (β i)] {s : set (Πa, β a)} {a : α} :
(Sup s) a = (⨆f:s, (f : Πa, β a) a) :=
rfl
lemma supr_apply {α : Type*} {β : α → Type*} {ι : Sort*} [Π i, has_Sup (β i)] {f : ι → Πa, β a}
{a : α} :
(⨆i, f i) a = (⨆i, f i a) :=
@infi_apply α (λ i, order_dual (β i)) _ _ f a
section complete_lattice
variables [preorder α] [complete_lattice β]
theorem monotone_Sup_of_monotone {s : set (α → β)} (m_s : ∀f∈s, monotone f) : monotone (Sup s) :=
assume x y h, supr_le $ λ f, le_supr_of_le f $ m_s f f.2 h
theorem monotone_Inf_of_monotone {s : set (α → β)} (m_s : ∀f∈s, monotone f) : monotone (Inf s) :=
assume x y h, le_infi $ λ f, infi_le_of_le f $ m_s f f.2 h
end complete_lattice
namespace prod
variables (α β)
instance [has_Inf α] [has_Inf β] : has_Inf (α × β) :=
⟨λs, (Inf (prod.fst '' s), Inf (prod.snd '' s))⟩
instance [has_Sup α] [has_Sup β] : has_Sup (α × β) :=
⟨λs, (Sup (prod.fst '' s), Sup (prod.snd '' s))⟩
instance [complete_lattice α] [complete_lattice β] : complete_lattice (α × β) :=
{ le_Sup := assume s p hab, ⟨le_Sup $ mem_image_of_mem _ hab, le_Sup $ mem_image_of_mem _ hab⟩,
Sup_le := assume s p h,
⟨ Sup_le $ ball_image_of_ball $ assume p hp, (h p hp).1,
Sup_le $ ball_image_of_ball $ assume p hp, (h p hp).2⟩,
Inf_le := assume s p hab, ⟨Inf_le $ mem_image_of_mem _ hab, Inf_le $ mem_image_of_mem _ hab⟩,
le_Inf := assume s p h,
⟨ le_Inf $ ball_image_of_ball $ assume p hp, (h p hp).1,
le_Inf $ ball_image_of_ball $ assume p hp, (h p hp).2⟩,
.. prod.bounded_lattice α β,
.. prod.has_Sup α β,
.. prod.has_Inf α β }
end prod
|
0e80877a60fa1d5c91e59d4b0dfdc7bdb3deed8a | bb31430994044506fa42fd667e2d556327e18dfe | /src/topology/algebra/order/extend_from.lean | 922ff11885cc53b5329826737821988279744727 | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 3,137 | 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, Yury Kudryashov
-/
import topology.order.basic
import topology.extend_from
/-!
# Lemmas about `extend_from` in an order topology.
-/
open filter set topological_space
open_locale topological_space classical
universes u v
variables {α : Type u} {β : Type v}
lemma continuous_on_Icc_extend_from_Ioo [topological_space α] [linear_order α] [densely_ordered α]
[order_topology α] [topological_space β] [regular_space β] {f : α → β} {a b : α}
{la lb : β} (hab : a ≠ b) (hf : continuous_on f (Ioo a b))
(ha : tendsto f (𝓝[>] a) (𝓝 la)) (hb : tendsto f (𝓝[<] b) (𝓝 lb)) :
continuous_on (extend_from (Ioo a b) f) (Icc a b) :=
begin
apply continuous_on_extend_from,
{ rw closure_Ioo hab },
{ intros x x_in,
rcases eq_endpoints_or_mem_Ioo_of_mem_Icc x_in with rfl | rfl | h,
{ exact ⟨la, ha.mono_left $ nhds_within_mono _ Ioo_subset_Ioi_self⟩ },
{ exact ⟨lb, hb.mono_left $ nhds_within_mono _ Ioo_subset_Iio_self⟩ },
{ use [f x, hf x h] } }
end
lemma eq_lim_at_left_extend_from_Ioo [topological_space α] [linear_order α] [densely_ordered α]
[order_topology α] [topological_space β] [t2_space β] {f : α → β} {a b : α}
{la : β} (hab : a < b) (ha : tendsto f (𝓝[>] a) (𝓝 la)) :
extend_from (Ioo a b) f a = la :=
begin
apply extend_from_eq,
{ rw closure_Ioo hab.ne,
simp only [le_of_lt hab, left_mem_Icc, right_mem_Icc] },
{ simpa [hab] }
end
lemma eq_lim_at_right_extend_from_Ioo [topological_space α] [linear_order α] [densely_ordered α]
[order_topology α] [topological_space β] [t2_space β] {f : α → β} {a b : α}
{lb : β} (hab : a < b) (hb : tendsto f (𝓝[<] b) (𝓝 lb)) :
extend_from (Ioo a b) f b = lb :=
begin
apply extend_from_eq,
{ rw closure_Ioo hab.ne,
simp only [le_of_lt hab, left_mem_Icc, right_mem_Icc] },
{ simpa [hab] }
end
lemma continuous_on_Ico_extend_from_Ioo [topological_space α]
[linear_order α] [densely_ordered α] [order_topology α] [topological_space β]
[regular_space β] {f : α → β} {a b : α} {la : β} (hab : a < b) (hf : continuous_on f (Ioo a b))
(ha : tendsto f (𝓝[>] a) (𝓝 la)) :
continuous_on (extend_from (Ioo a b) f) (Ico a b) :=
begin
apply continuous_on_extend_from,
{ rw [closure_Ioo hab.ne], exact Ico_subset_Icc_self, },
{ intros x x_in,
rcases eq_left_or_mem_Ioo_of_mem_Ico x_in with rfl | h,
{ use la,
simpa [hab] },
{ use [f x, hf x h] } }
end
lemma continuous_on_Ioc_extend_from_Ioo [topological_space α]
[linear_order α] [densely_ordered α] [order_topology α] [topological_space β]
[regular_space β] {f : α → β} {a b : α} {lb : β} (hab : a < b) (hf : continuous_on f (Ioo a b))
(hb : tendsto f (𝓝[<] b) (𝓝 lb)) :
continuous_on (extend_from (Ioo a b) f) (Ioc a b) :=
begin
have := @continuous_on_Ico_extend_from_Ioo αᵒᵈ _ _ _ _ _ _ _ f _ _ _ hab,
erw [dual_Ico, dual_Ioi, dual_Ioo] at this,
exact this hf hb
end
|
bc6e239fa28d0125c6483580912880709faad4ec | 618003631150032a5676f229d13a079ac875ff77 | /src/data/fintype/card.lean | fcbae2f7a25b5ffafabbaab589a26583a593fdbd | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 13,452 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
-/
import data.fintype.basic
import data.nat.choose
import tactic.ring
/-!
Results about "big operations" over a `fintype`, and consequent
results about cardinalities of certain types.
## Implementation note
This content had previously been in `data.fintype`, but was moved here to avoid
requiring `algebra.big_operators` (and hence many other imports) as a
dependency of `fintype`.
-/
universes u v
variables {α : Type*} {β : Type*} {γ : Type*}
open_locale big_operators
namespace fintype
lemma card_eq_sum_ones {α} [fintype α] : fintype.card α = ∑ a : α, 1 :=
finset.card_eq_sum_ones _
section
open finset
variables {ι : Type*} [fintype ι] [decidable_eq ι]
@[to_additive]
lemma prod_extend_by_one [comm_monoid α] (s : finset ι) (f : ι → α) :
∏ i, (if i ∈ s then f i else 1) = ∏ i in s, f i :=
by rw [← prod_filter, filter_mem_eq_inter, univ_inter]
end
section
variables {M : Type*} [fintype α] [comm_monoid M]
@[to_additive]
lemma prod_eq_one (f : α → M) (h : ∀ a, f a = 1) :
(∏ a, f a) = 1 :=
finset.prod_eq_one $ λ a ha, h a
@[to_additive]
lemma prod_congr (f g : α → M) (h : ∀ a, f a = g a) :
(∏ a, f a) = ∏ a, g a :=
finset.prod_congr rfl $ λ a ha, h a
@[to_additive]
lemma prod_unique [unique β] (f : β → M) :
(∏ x, f x) = f (default β) :=
by simp only [finset.prod_singleton, univ_unique]
end
end fintype
open finset
theorem fin.prod_univ_succ [comm_monoid β] {n:ℕ} (f : fin n.succ → β) :
univ.prod f = f 0 * univ.prod (λ i:fin n, f i.succ) :=
begin
rw [fin.univ_succ, prod_insert, prod_image],
{ intros x _ y _ hxy, exact fin.succ.inj hxy },
{ simpa using fin.succ_ne_zero }
end
@[simp, to_additive] theorem fin.prod_univ_zero [comm_monoid β] (f : fin 0 → β) : univ.prod f = 1 := rfl
theorem fin.sum_univ_succ [add_comm_monoid β] {n:ℕ} (f : fin n.succ → β) :
univ.sum f = f 0 + univ.sum (λ i:fin n, f i.succ) :=
by apply @fin.prod_univ_succ (multiplicative β)
attribute [to_additive] fin.prod_univ_succ
theorem fin.prod_univ_cast_succ [comm_monoid β] {n:ℕ} (f : fin n.succ → β) :
univ.prod f = univ.prod (λ i:fin n, f i.cast_succ) * f (fin.last n) :=
begin
rw [fin.univ_cast_succ, prod_insert, prod_image, mul_comm],
{ intros x _ y _ hxy, exact fin.cast_succ_inj.mp hxy },
{ simpa using fin.cast_succ_ne_last }
end
theorem fin.sum_univ_cast_succ [add_comm_monoid β] {n:ℕ} (f : fin n.succ → β) :
univ.sum f = univ.sum (λ i:fin n, f i.cast_succ) + f (fin.last n) :=
by apply @fin.prod_univ_cast_succ (multiplicative β)
attribute [to_additive] fin.prod_univ_cast_succ
@[simp] theorem fintype.card_sigma {α : Type*} (β : α → Type*)
[fintype α] [∀ a, fintype (β a)] :
fintype.card (sigma β) = univ.sum (λ a, fintype.card (β a)) :=
card_sigma _ _
-- FIXME ouch, this should be in the main file.
@[simp] theorem fintype.card_sum (α β : Type*) [fintype α] [fintype β] :
fintype.card (α ⊕ β) = fintype.card α + fintype.card β :=
by simp [sum.fintype, fintype.of_equiv_card]
@[simp] lemma fintype.card_pi_finset [decidable_eq α] [fintype α]
{δ : α → Type*} (t : Π a, finset (δ a)) :
(fintype.pi_finset t).card = finset.univ.prod (λ a, card (t a)) :=
by simp [fintype.pi_finset, card_map]
@[simp] lemma fintype.card_pi {β : α → Type*} [fintype α] [decidable_eq α]
[f : Π a, fintype (β a)] : fintype.card (Π a, β a) = univ.prod (λ a, fintype.card (β a)) :=
fintype.card_pi_finset _
-- FIXME ouch, this should be in the main file.
@[simp] lemma fintype.card_fun [fintype α] [decidable_eq α] [fintype β] :
fintype.card (α → β) = fintype.card β ^ fintype.card α :=
by rw [fintype.card_pi, finset.prod_const, nat.pow_eq_pow]; refl
@[simp] lemma card_vector [fintype α] (n : ℕ) :
fintype.card (vector α n) = fintype.card α ^ n :=
by rw fintype.of_equiv_card; simp
@[simp, to_additive]
lemma finset.prod_attach_univ [fintype α] [comm_monoid β] (f : {a : α // a ∈ @univ α _} → β) :
univ.attach.prod (λ x, f x) = univ.prod (λ x, f ⟨x, (mem_univ _)⟩) :=
prod_bij (λ x _, x.1) (λ _ _, mem_univ _) (λ _ _ , by simp) (by simp) (λ b _, ⟨⟨b, mem_univ _⟩, by simp⟩)
@[to_additive]
lemma finset.range_prod_eq_univ_prod [comm_monoid β] (n : ℕ) (f : ℕ → β) :
(range n).prod f = univ.prod (λ (k : fin n), f k) :=
begin
symmetry,
refine prod_bij (λ k hk, k) _ _ _ _,
{ rintro ⟨k, hk⟩ _, simp * },
{ rintro ⟨k, hk⟩ _, simp * },
{ intros, rwa fin.eq_iff_veq },
{ intros k hk, rw mem_range at hk,
exact ⟨⟨k, hk⟩, mem_univ _, rfl⟩ }
end
/-- Taking a product over `univ.pi t` is the same as taking the product over `fintype.pi_finset t`.
`univ.pi t` and `fintype.pi_finset t` are essentially the same `finset`, but differ
in the type of their element, `univ.pi t` is a `finset (Π a ∈ univ, t a)` and
`fintype.pi_finset t` is a `finset (Π a, t a)`. -/
@[to_additive "Taking a sum over `univ.pi t` is the same as taking the sum over
`fintype.pi_finset t`. `univ.pi t` and `fintype.pi_finset t` are essentially the same `finset`,
but differ in the type of their element, `univ.pi t` is a `finset (Π a ∈ univ, t a)` and
`fintype.pi_finset t` is a `finset (Π a, t a)`."]
lemma finset.prod_univ_pi [decidable_eq α] [fintype α] [comm_monoid β]
{δ : α → Type*} {t : Π (a : α), finset (δ a)}
(f : (Π (a : α), a ∈ (univ : finset α) → δ a) → β) :
(univ.pi t).prod f = (fintype.pi_finset t).prod (λ x, f (λ a _, x a)) :=
prod_bij (λ x _ a, x a (mem_univ _))
(by simp)
(by simp)
(by simp [function.funext_iff] {contextual := tt})
(λ x hx, ⟨λ a _, x a, by simp * at *⟩)
/-- The product over `univ` of a sum can be written as a sum over the product of sets,
`fintype.pi_finset`. `finset.prod_sum` is an alternative statement when the product is not
over `univ` -/
lemma finset.prod_univ_sum [decidable_eq α] [fintype α] [comm_semiring β] {δ : α → Type u_1}
[Π (a : α), decidable_eq (δ a)] {t : Π (a : α), finset (δ a)}
{f : Π (a : α), δ a → β} :
univ.prod (λ a, (t a).sum (λ b, f a b)) =
(fintype.pi_finset t).sum (λ p, univ.prod (λ x, f x (p x))) :=
by simp only [finset.prod_attach_univ, prod_sum, finset.sum_univ_pi]
/-- Summing `a^s.card * b^(n-s.card)` over all finite subsets `s` of a fintype of cardinality `n`
gives `(a + b)^n`. The "good" proof involves expanding along all coordinates using the fact that
`x^n` is multilinear, but multilinear maps are only available now over rings, so we give instead
a proof reducing to the usual binomial theorem to have a result over semirings. -/
lemma fintype.sum_pow_mul_eq_add_pow
(α : Type*) [fintype α] {R : Type*} [comm_semiring R] (a b : R) :
finset.univ.sum (λ (s : finset α), a ^ s.card * b ^ (fintype.card α - s.card)) =
(a + b) ^ (fintype.card α) :=
finset.sum_pow_mul_eq_add_pow _ _ _
lemma fin.sum_pow_mul_eq_add_pow {n : ℕ} {R : Type*} [comm_semiring R] (a b : R) :
finset.univ.sum (λ (s : finset (fin n)), a ^ s.card * b ^ (n - s.card)) =
(a + b) ^ n :=
by simpa using fintype.sum_pow_mul_eq_add_pow (fin n) a b
/-- It is equivalent to sum a function over `fin n` or `finset.range n`. -/
@[to_additive]
lemma fin.prod_univ_eq_prod_range [comm_monoid α] (f : ℕ → α) (n : ℕ) :
finset.univ.prod (λ (i : fin n), f i.val) = (finset.range n).prod f :=
begin
apply finset.prod_bij (λ (a : fin n) ha, a.val),
{ assume a ha, simp [a.2] },
{ assume a ha, refl },
{ assume a b ha hb H, exact (fin.ext_iff _ _).2 H },
{ assume b hb, exact ⟨⟨b, list.mem_range.mp hb⟩, finset.mem_univ _, rfl⟩, }
end
@[to_additive]
lemma finset.prod_equiv [fintype α] [fintype β] [comm_monoid γ] (e : α ≃ β) (f : β → γ) :
finset.univ.prod (f ∘ e) = finset.univ.prod f :=
begin
apply prod_bij (λ i hi, e i) (λ i hi, mem_univ _) _ (λ a b _ _ h, e.injective h),
{ assume b hb,
rcases e.surjective b with ⟨a, ha⟩,
exact ⟨a, mem_univ _, ha.symm⟩, },
{ simp }
end
@[to_additive]
lemma finset.prod_subtype {M : Type*} [comm_monoid M]
{p : α → Prop} {F : fintype (subtype p)} {s : finset α} (h : ∀ x, x ∈ s ↔ p x) (f : α → M) :
∏ a in s, f a = ∏ a : subtype p, f a :=
have (∈ s) = p, from set.ext h,
begin
rw ← prod_attach,
resetI,
subst p,
congr,
simp [finset.ext]
end
@[to_additive] lemma finset.prod_fiberwise [fintype β] [decidable_eq β] [comm_monoid γ]
(s : finset α) (f : α → β) (g : α → γ) :
∏ b : β, ∏ a in s.filter (λ a, f a = b), g a = ∏ a in s, g a :=
begin
classical,
have key : ∏ (b : β), ∏ a in s.filter (λ a, f a = b), g a =
∏ (a : α) in univ.bind (λ (b : β), s.filter (λ a, f a = b)), g a :=
(@prod_bind _ _ β g _ _ finset.univ (λ b : β, s.filter (λ a, f a = b)) _).symm,
{ simp only [key, filter_congr_decidable],
apply finset.prod_congr,
{ ext, simp only [mem_bind, mem_filter, mem_univ, exists_prop_of_true, exists_eq_right'] },
{ intros, refl } },
{ intros x hx y hy H z hz, apply H,
simp only [mem_filter, inf_eq_inter, mem_inter] at hz,
rw [← hz.1.2, ← hz.2.2] }
end
@[to_additive]
lemma fintype.prod_fiberwise [fintype α] [fintype β] [decidable_eq β] [comm_monoid γ]
(f : α → β) (g : α → γ) :
(∏ b : β, ∏ a : {a // f a = b}, g (a : α)) = ∏ a, g a :=
begin
rw [← finset.prod_equiv (equiv.sigma_preimage_equiv f) _, ← univ_sigma_univ, prod_sigma],
refl
end
section
open finset
variables {α₁ : Type*} {α₂ : Type*} {M : Type*} [fintype α₁] [fintype α₂] [comm_monoid M]
@[to_additive]
lemma fintype.prod_sum_type (f : α₁ ⊕ α₂ → M) :
(∏ x, f x) = (∏ a₁, f (sum.inl a₁)) * (∏ a₂, f (sum.inr a₂)) :=
begin
classical,
let s : finset (α₁ ⊕ α₂) := univ.image sum.inr,
rw [← prod_sdiff (subset_univ s),
← @prod_image (α₁ ⊕ α₂) _ _ _ _ _ _ sum.inl,
← @prod_image (α₁ ⊕ α₂) _ _ _ _ _ _ sum.inr],
{ congr, rw finset.ext, rintro (a|a);
{ simp only [mem_image, exists_eq, mem_sdiff, mem_univ, exists_false,
exists_prop_of_true, not_false_iff, and_self, not_true, and_false], } },
all_goals { intros, solve_by_elim [sum.inl.inj, sum.inr.inj], }
end
end
namespace list
lemma prod_take_of_fn [comm_monoid α] {n : ℕ} (f : fin n → α) (i : ℕ) :
((of_fn f).take i).prod = (finset.univ.filter (λ (j : fin n), j.val < i)).prod f :=
begin
have A : ∀ (j : fin n), ¬ (j.val < 0) := λ j, not_lt_bot,
induction i with i IH, { simp [A] },
by_cases h : i < n,
{ have : i < length (of_fn f), by rwa [length_of_fn f],
rw prod_take_succ _ _ this,
have A : ((finset.univ : finset (fin n)).filter (λ j, j.val < i + 1))
= ((finset.univ : finset (fin n)).filter (λ j, j.val < i)) ∪ {(⟨i, h⟩ : fin n)},
by { ext j, simp [nat.lt_succ_iff_lt_or_eq, fin.ext_iff, - add_comm] },
have B : _root_.disjoint (finset.filter (λ (j : fin n), j.val < i) finset.univ)
(singleton (⟨i, h⟩ : fin n)), by simp,
rw [A, finset.prod_union B, IH],
simp },
{ have A : (of_fn f).take i = (of_fn f).take i.succ,
{ rw ← length_of_fn f at h,
have : length (of_fn f) ≤ i := not_lt.mp h,
rw [take_all_of_le this, take_all_of_le (le_trans this (nat.le_succ _))] },
have B : ∀ (j : fin n), (j.val < i.succ) = (j.val < i),
{ assume j,
have : j.val < i := lt_of_lt_of_le j.2 (not_lt.mp h),
simp [this, lt_trans this (nat.lt_succ_self _)] },
simp [← A, B, IH] }
end
-- `to_additive` does not work on `prod_take_of_fn` because of `0 : ℕ` in the proof. Copy-paste the
-- proof instead...
lemma sum_take_of_fn [add_comm_monoid α] {n : ℕ} (f : fin n → α) (i : ℕ) :
((of_fn f).take i).sum = (finset.univ.filter (λ (j : fin n), j.val < i)).sum f :=
begin
have A : ∀ (j : fin n), ¬ (j.val < 0) := λ j, not_lt_bot,
induction i with i IH, { simp [A] },
by_cases h : i < n,
{ have : i < length (of_fn f), by rwa [length_of_fn f],
rw sum_take_succ _ _ this,
have A : ((finset.univ : finset (fin n)).filter (λ j, j.val < i + 1))
= ((finset.univ : finset (fin n)).filter (λ j, j.val < i)) ∪ singleton (⟨i, h⟩ : fin n),
by { ext j, simp [nat.lt_succ_iff_lt_or_eq, fin.ext_iff, - add_comm] },
have B : _root_.disjoint (finset.filter (λ (j : fin n), j.val < i) finset.univ)
(singleton (⟨i, h⟩ : fin n)), by simp,
rw [A, finset.sum_union B, IH],
simp },
{ have A : (of_fn f).take i = (of_fn f).take i.succ,
{ rw ← length_of_fn f at h,
have : length (of_fn f) ≤ i := not_lt.mp h,
rw [take_all_of_le this, take_all_of_le (le_trans this (nat.le_succ _))] },
have B : ∀ (j : fin n), (j.val < i.succ) = (j.val < i),
{ assume j,
have : j.val < i := lt_of_lt_of_le j.2 (not_lt.mp h),
simp [this, lt_trans this (nat.lt_succ_self _)] },
simp [← A, B, IH] }
end
attribute [to_additive] prod_take_of_fn
@[to_additive]
lemma prod_of_fn [comm_monoid α] {n : ℕ} {f : fin n → α} :
(of_fn f).prod = finset.univ.prod f :=
begin
convert prod_take_of_fn f n,
{ rw [take_all_of_le (le_of_eq (length_of_fn f))] },
{ have : ∀ (j : fin n), j.val < n := λ j, j.2,
simp [this] }
end
end list
|
69d20458dd901e290454da8aa4ea41503b7d7dc0 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/ring_theory/kaehler.lean | 2ec88c0e1d9fa329cad25de7d772fce8db0239d6 | [
"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 | 28,172 | lean | /-
Copyright © 2020 Nicolò Cavalleri. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nicolò Cavalleri, Andrew Yang
-/
import ring_theory.derivation.to_square_zero
import ring_theory.ideal.cotangent
import ring_theory.is_tensor_product
/-!
# The module of kaehler differentials
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
## Main results
- `kaehler_differential`: The module of kaehler differentials. For an `R`-algebra `S`, we provide
the notation `Ω[S⁄R]` for `kaehler_differential R S`.
Note that the slash is `\textfractionsolidus`.
- `kaehler_differential.D`: The derivation into the module of kaehler differentials.
- `kaehler_differential.span_range_derivation`: The image of `D` spans `Ω[S⁄R]` as an `S`-module.
- `kaehler_differential.linear_map_equiv_derivation`:
The isomorphism `Hom_R(Ω[S⁄R], M) ≃ₗ[S] Der_R(S, M)`.
- `kaehler_differential.quot_ker_total_equiv`: An alternative description of `Ω[S⁄R]` as `S` copies
of `S` with kernel (`kaehler_differential.ker_total`) generated by the relations:
1. `dx + dy = d(x + y)`
2. `x dy + y dx = d(x * y)`
3. `dr = 0` for `r ∈ R`
- `kaehler_differential.map`: Given a map between the arrows `R → A` and `S → B`, we have an
`A`-linear map `Ω[A⁄R] → Ω[B⁄S]`.
## Future project
- Define a `is_kaehler_differential` predicate.
-/
section kaehler_differential
open_locale tensor_product
open algebra
variables (R S : Type*) [comm_ring R] [comm_ring S] [algebra R S]
/-- The kernel of the multiplication map `S ⊗[R] S →ₐ[R] S`. -/
abbreviation kaehler_differential.ideal : ideal (S ⊗[R] S) :=
ring_hom.ker (tensor_product.lmul' R : S ⊗[R] S →ₐ[R] S)
variable {S}
lemma kaehler_differential.one_smul_sub_smul_one_mem_ideal (a : S) :
(1 : S) ⊗ₜ[R] a - a ⊗ₜ[R] (1 : S) ∈ kaehler_differential.ideal R S :=
by simp [ring_hom.mem_ker]
variables {R}
variables {M : Type*} [add_comm_group M] [module R M] [module S M] [is_scalar_tower R S M]
/-- For a `R`-derivation `S → M`, this is the map `S ⊗[R] S →ₗ[S] M` sending `s ⊗ₜ t ↦ s • D t`. -/
def derivation.tensor_product_to (D : derivation R S M) : S ⊗[R] S →ₗ[S] M :=
tensor_product.algebra_tensor_module.lift ((linear_map.lsmul S (S →ₗ[R] M)).flip D.to_linear_map)
lemma derivation.tensor_product_to_tmul (D : derivation R S M) (s t : S) :
D.tensor_product_to (s ⊗ₜ t) = s • D t :=
rfl
lemma derivation.tensor_product_to_mul (D : derivation R S M) (x y : S ⊗[R] S) :
D.tensor_product_to (x * y) = tensor_product.lmul' R x • D.tensor_product_to y +
tensor_product.lmul' R y • D.tensor_product_to x :=
begin
apply tensor_product.induction_on x,
{ rw [zero_mul, map_zero, map_zero, zero_smul, smul_zero, add_zero] },
swap, { rintros, simp only [add_mul, map_add, add_smul, *, smul_add], rw add_add_add_comm },
intros x₁ x₂,
apply tensor_product.induction_on y,
{ rw [mul_zero, map_zero, map_zero, zero_smul, smul_zero, add_zero] },
swap, { rintros, simp only [mul_add, map_add, add_smul, *, smul_add], rw add_add_add_comm },
intros x y,
simp only [tensor_product.tmul_mul_tmul, derivation.tensor_product_to,
tensor_product.algebra_tensor_module.lift_apply, tensor_product.lift.tmul',
tensor_product.lmul'_apply_tmul],
dsimp,
rw D.leibniz,
simp only [smul_smul, smul_add, mul_comm (x * y) x₁, mul_right_comm x₁ x₂, ← mul_assoc],
end
variables (R S)
/-- The kernel of `S ⊗[R] S →ₐ[R] S` is generated by `1 ⊗ s - s ⊗ 1` as a `S`-module. -/
lemma kaehler_differential.submodule_span_range_eq_ideal :
submodule.span S (set.range $ λ s : S, (1 : S) ⊗ₜ[R] s - s ⊗ₜ[R] (1 : S)) =
(kaehler_differential.ideal R S).restrict_scalars S :=
begin
apply le_antisymm,
{ rw submodule.span_le,
rintros _ ⟨s, rfl⟩,
exact kaehler_differential.one_smul_sub_smul_one_mem_ideal _ _ },
{ rintros x (hx : _ = _),
have : x - (tensor_product.lmul' R x) ⊗ₜ[R] (1 : S) = x,
{ rw [hx, tensor_product.zero_tmul, sub_zero] },
rw ← this,
clear this hx,
apply tensor_product.induction_on x; clear x,
{ rw [map_zero, tensor_product.zero_tmul, sub_zero], exact zero_mem _ },
{ intros x y,
convert_to x • (1 ⊗ₜ y - y ⊗ₜ 1) ∈ _,
{ rw [tensor_product.lmul'_apply_tmul, smul_sub, tensor_product.smul_tmul',
tensor_product.smul_tmul', smul_eq_mul, smul_eq_mul, mul_one] },
{ refine submodule.smul_mem _ x _,
apply submodule.subset_span,
exact set.mem_range_self y } },
{ intros x y hx hy,
rw [map_add, tensor_product.add_tmul, ← sub_add_sub_comm],
exact add_mem hx hy } }
end
lemma kaehler_differential.span_range_eq_ideal :
ideal.span (set.range $ λ s : S, (1 : S) ⊗ₜ[R] s - s ⊗ₜ[R] (1 : S)) =
kaehler_differential.ideal R S :=
begin
apply le_antisymm,
{ rw ideal.span_le,
rintros _ ⟨s, rfl⟩,
exact kaehler_differential.one_smul_sub_smul_one_mem_ideal _ _ },
{ change (kaehler_differential.ideal R S).restrict_scalars S ≤ (ideal.span _).restrict_scalars S,
rw [← kaehler_differential.submodule_span_range_eq_ideal, ideal.span],
conv_rhs { rw ← submodule.span_span_of_tower S },
exact submodule.subset_span }
end
/--
The module of Kähler differentials (Kahler differentials, Kaehler differentials).
This is implemented as `I / I ^ 2` with `I` the kernel of the multiplication map `S ⊗[R] S →ₐ[R] S`.
To view elements as a linear combination of the form `s • D s'`, use
`kaehler_differential.tensor_product_to_surjective` and `derivation.tensor_product_to_tmul`.
We also provide the notation `Ω[S⁄R]` for `kaehler_differential R S`.
Note that the slash is `\textfractionsolidus`.
-/
@[derive [add_comm_group, module (S ⊗[R] S)]]
def kaehler_differential : Type* := (kaehler_differential.ideal R S).cotangent
notation `Ω[`:100 S `⁄`:0 R `]`:0 := kaehler_differential R S
instance : nonempty Ω[S⁄R] := ⟨0⟩
instance kaehler_differential.module' {R' : Type*} [comm_ring R'] [algebra R' S]
[smul_comm_class R R' S] :
module R' Ω[S⁄R] :=
submodule.quotient.module' _
instance : is_scalar_tower S (S ⊗[R] S) Ω[S⁄R] :=
ideal.cotangent.is_scalar_tower _
instance kaehler_differential.is_scalar_tower_of_tower {R₁ R₂ : Type*} [comm_ring R₁] [comm_ring R₂]
[algebra R₁ S] [algebra R₂ S] [has_smul R₁ R₂]
[smul_comm_class R R₁ S] [smul_comm_class R R₂ S] [is_scalar_tower R₁ R₂ S] :
is_scalar_tower R₁ R₂ Ω[S⁄R] :=
submodule.quotient.is_scalar_tower _ _
instance kaehler_differential.is_scalar_tower' :
is_scalar_tower R (S ⊗[R] S) Ω[S⁄R] :=
submodule.quotient.is_scalar_tower _ _
/-- The quotient map `I → Ω[S⁄R]` with `I` being the kernel of `S ⊗[R] S → S`. -/
def kaehler_differential.from_ideal : kaehler_differential.ideal R S →ₗ[S ⊗[R] S] Ω[S⁄R] :=
(kaehler_differential.ideal R S).to_cotangent
/-- (Implementation) The underlying linear map of the derivation into `Ω[S⁄R]`. -/
def kaehler_differential.D_linear_map : S →ₗ[R] Ω[S⁄R] :=
((kaehler_differential.from_ideal R S).restrict_scalars R).comp
((tensor_product.include_right.to_linear_map - tensor_product.include_left.to_linear_map :
S →ₗ[R] S ⊗[R] S).cod_restrict ((kaehler_differential.ideal R S).restrict_scalars R)
(kaehler_differential.one_smul_sub_smul_one_mem_ideal R) : _ →ₗ[R] _)
lemma kaehler_differential.D_linear_map_apply (s : S) :
kaehler_differential.D_linear_map R S s = (kaehler_differential.ideal R S).to_cotangent
⟨1 ⊗ₜ s - s ⊗ₜ 1, kaehler_differential.one_smul_sub_smul_one_mem_ideal R s⟩ :=
rfl
/-- The universal derivation into `Ω[S⁄R]`. -/
def kaehler_differential.D : derivation R S Ω[S⁄R] :=
{ map_one_eq_zero' := begin
dsimp only [kaehler_differential.D_linear_map_apply],
rw [ideal.to_cotangent_eq_zero, subtype.coe_mk, sub_self],
exact zero_mem _
end,
leibniz' := λ a b, begin
dsimp only [kaehler_differential.D_linear_map_apply],
rw [← linear_map.map_smul_of_tower ((kaehler_differential.ideal R S).to_cotangent) a,
← linear_map.map_smul_of_tower ((kaehler_differential.ideal R S).to_cotangent) b, ← map_add,
ideal.to_cotangent_eq, pow_two],
convert submodule.mul_mem_mul (kaehler_differential.one_smul_sub_smul_one_mem_ideal R a : _)
(kaehler_differential.one_smul_sub_smul_one_mem_ideal R b : _) using 1,
simp only [add_subgroup_class.coe_sub, submodule.coe_add, submodule.coe_mk,
tensor_product.tmul_mul_tmul, mul_sub, sub_mul, mul_comm b,
submodule.coe_smul_of_tower, smul_sub, tensor_product.smul_tmul', smul_eq_mul, mul_one],
ring_nf,
end,
to_linear_map := kaehler_differential.D_linear_map R S }
lemma kaehler_differential.D_apply (s : S) :
kaehler_differential.D R S s = (kaehler_differential.ideal R S).to_cotangent
⟨1 ⊗ₜ s - s ⊗ₜ 1, kaehler_differential.one_smul_sub_smul_one_mem_ideal R s⟩ :=
rfl
lemma kaehler_differential.span_range_derivation :
submodule.span S (set.range $ kaehler_differential.D R S) = ⊤ :=
begin
rw _root_.eq_top_iff,
rintros x -,
obtain ⟨⟨x, hx⟩, rfl⟩ := ideal.to_cotangent_surjective _ x,
have : x ∈ (kaehler_differential.ideal R S).restrict_scalars S := hx,
rw ← kaehler_differential.submodule_span_range_eq_ideal at this,
suffices : ∃ hx, (kaehler_differential.ideal R S).to_cotangent ⟨x, hx⟩ ∈
submodule.span S (set.range $ kaehler_differential.D R S),
{ exact this.some_spec },
apply submodule.span_induction this,
{ rintros _ ⟨x, rfl⟩,
refine ⟨kaehler_differential.one_smul_sub_smul_one_mem_ideal R x, _⟩,
apply submodule.subset_span,
exact ⟨x, kaehler_differential.D_linear_map_apply R S x⟩ },
{ exact ⟨zero_mem _, submodule.zero_mem _⟩ },
{ rintros x y ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩, exact ⟨add_mem hx₁ hy₁, submodule.add_mem _ hx₂ hy₂⟩ },
{ rintros r x ⟨hx₁, hx₂⟩, exact ⟨((kaehler_differential.ideal R S).restrict_scalars S).smul_mem
r hx₁, submodule.smul_mem _ r hx₂⟩ }
end
variables {R S}
/-- The linear map from `Ω[S⁄R]`, associated with a derivation. -/
def derivation.lift_kaehler_differential (D : derivation R S M) : Ω[S⁄R] →ₗ[S] M :=
begin
refine ((kaehler_differential.ideal R S • ⊤ :
submodule (S ⊗[R] S) (kaehler_differential.ideal R S)).restrict_scalars S).liftq _ _,
{ exact D.tensor_product_to.comp ((kaehler_differential.ideal R S).subtype.restrict_scalars S) },
{ intros x hx,
change _ = _,
apply submodule.smul_induction_on hx; clear hx x,
{ rintros x (hx : _ = _) ⟨y, hy : _ = _⟩ -,
dsimp,
rw [derivation.tensor_product_to_mul, hx, hy, zero_smul, zero_smul, zero_add] },
{ intros x y ex ey, rw [map_add, ex, ey, zero_add] } }
end
lemma derivation.lift_kaehler_differential_apply (D : derivation R S M) (x) :
D.lift_kaehler_differential
((kaehler_differential.ideal R S).to_cotangent x) = D.tensor_product_to x :=
rfl
lemma derivation.lift_kaehler_differential_comp (D : derivation R S M) :
D.lift_kaehler_differential.comp_der (kaehler_differential.D R S) = D :=
begin
ext a,
dsimp [kaehler_differential.D_apply],
refine (D.lift_kaehler_differential_apply _).trans _,
rw [subtype.coe_mk, map_sub, derivation.tensor_product_to_tmul,
derivation.tensor_product_to_tmul, one_smul, D.map_one_eq_zero, smul_zero, sub_zero],
end
@[simp] lemma derivation.lift_kaehler_differential_comp_D (D' : derivation R S M) (x : S) :
D'.lift_kaehler_differential (kaehler_differential.D R S x) = D' x :=
derivation.congr_fun D'.lift_kaehler_differential_comp x
@[ext]
lemma derivation.lift_kaehler_differential_unique
(f f' : Ω[S⁄R] →ₗ[S] M)
(hf : f.comp_der (kaehler_differential.D R S) =
f'.comp_der (kaehler_differential.D R S)) :
f = f' :=
begin
apply linear_map.ext,
intro x,
have : x ∈ submodule.span S (set.range $ kaehler_differential.D R S),
{ rw kaehler_differential.span_range_derivation, trivial },
apply submodule.span_induction this,
{ rintros _ ⟨x, rfl⟩, exact congr_arg (λ D : derivation R S M, D x) hf },
{ rw [map_zero, map_zero] },
{ intros x y hx hy, rw [map_add, map_add, hx, hy] },
{ intros a x e, rw [map_smul, map_smul, e] }
end
variables (R S)
lemma derivation.lift_kaehler_differential_D :
(kaehler_differential.D R S).lift_kaehler_differential = linear_map.id :=
derivation.lift_kaehler_differential_unique _ _
(kaehler_differential.D R S).lift_kaehler_differential_comp
variables {R S}
lemma kaehler_differential.D_tensor_product_to (x : kaehler_differential.ideal R S) :
(kaehler_differential.D R S).tensor_product_to x =
(kaehler_differential.ideal R S).to_cotangent x :=
begin
rw [← derivation.lift_kaehler_differential_apply, derivation.lift_kaehler_differential_D],
refl,
end
variables (R S)
lemma kaehler_differential.tensor_product_to_surjective :
function.surjective (kaehler_differential.D R S).tensor_product_to :=
begin
intro x, obtain ⟨x, rfl⟩ := (kaehler_differential.ideal R S).to_cotangent_surjective x,
exact ⟨x, kaehler_differential.D_tensor_product_to x⟩
end
/-- The `S`-linear maps from `Ω[S⁄R]` to `M` are (`S`-linearly) equivalent to `R`-derivations
from `S` to `M`. -/
def kaehler_differential.linear_map_equiv_derivation : (Ω[S⁄R] →ₗ[S] M) ≃ₗ[S] derivation R S M :=
{ inv_fun := derivation.lift_kaehler_differential,
left_inv := λ f, derivation.lift_kaehler_differential_unique _ _
(derivation.lift_kaehler_differential_comp _),
right_inv := derivation.lift_kaehler_differential_comp,
..(derivation.llcomp.flip $ kaehler_differential.D R S) }
/-- The quotient ring of `S ⊗ S ⧸ J ^ 2` by `Ω[S⁄R]` is isomorphic to `S`. -/
def kaehler_differential.quotient_cotangent_ideal_ring_equiv :
(S ⊗ S ⧸ kaehler_differential.ideal R S ^ 2) ⧸
(kaehler_differential.ideal R S).cotangent_ideal ≃+* S :=
begin
have : function.right_inverse tensor_product.include_left
(↑(tensor_product.lmul' R : S ⊗[R] S →ₐ[R] S) : S ⊗[R] S →+* S),
{ intro x, rw [alg_hom.coe_to_ring_hom, ← alg_hom.comp_apply,
tensor_product.lmul'_comp_include_left], refl },
refine (ideal.quot_cotangent _).trans _,
refine (ideal.quot_equiv_of_eq _).trans (ring_hom.quotient_ker_equiv_of_right_inverse this),
ext, refl,
end
/-- The quotient ring of `S ⊗ S ⧸ J ^ 2` by `Ω[S⁄R]` is isomorphic to `S` as an `S`-algebra. -/
def kaehler_differential.quotient_cotangent_ideal :
((S ⊗ S ⧸ kaehler_differential.ideal R S ^ 2) ⧸
(kaehler_differential.ideal R S).cotangent_ideal) ≃ₐ[S] S :=
{ commutes' := (kaehler_differential.quotient_cotangent_ideal_ring_equiv R S).apply_symm_apply,
..kaehler_differential.quotient_cotangent_ideal_ring_equiv R S }
lemma kaehler_differential.End_equiv_aux (f : S →ₐ[R] S ⊗ S ⧸ kaehler_differential.ideal R S ^ 2) :
(ideal.quotient.mkₐ R (kaehler_differential.ideal R S).cotangent_ideal).comp f =
is_scalar_tower.to_alg_hom R S _ ↔
(tensor_product.lmul' R : S ⊗[R] S →ₐ[R] S).ker_square_lift.comp f = alg_hom.id R S :=
begin
rw [alg_hom.ext_iff, alg_hom.ext_iff],
apply forall_congr,
intro x,
have e₁ : (tensor_product.lmul' R : S ⊗[R] S →ₐ[R] S).ker_square_lift (f x) =
kaehler_differential.quotient_cotangent_ideal_ring_equiv R S
(ideal.quotient.mk (kaehler_differential.ideal R S).cotangent_ideal $ f x),
{ generalize : f x = y, obtain ⟨y, rfl⟩ := ideal.quotient.mk_surjective y, refl },
have e₂ : x = kaehler_differential.quotient_cotangent_ideal_ring_equiv
R S (is_scalar_tower.to_alg_hom R S _ x),
{ exact (mul_one x).symm },
split,
{ intro e,
exact (e₁.trans (@ring_equiv.congr_arg _ _ _ _ _ _
(kaehler_differential.quotient_cotangent_ideal_ring_equiv R S) _ _ e)).trans e₂.symm },
{ intro e, apply (kaehler_differential.quotient_cotangent_ideal_ring_equiv R S).injective,
exact e₁.symm.trans (e.trans e₂) }
end
/-- Derivations into `Ω[S⁄R]` is equivalent to derivations
into `(kaehler_differential.ideal R S).cotangent_ideal` -/
-- This has type
-- `derivation R S Ω[S⁄R] ≃ₗ[R] derivation R S (kaehler_differential.ideal R S).cotangent_ideal`
-- But lean times-out if this is given explicitly.
noncomputable
def kaehler_differential.End_equiv_derivation' :
derivation R S Ω[S⁄R] ≃ₗ[R] derivation R S _ :=
linear_equiv.comp_der ((kaehler_differential.ideal R S).cotangent_equiv_ideal.restrict_scalars S)
/-- (Implementation) An `equiv` version of `kaehler_differential.End_equiv_aux`.
Used in `kaehler_differential.End_equiv`. -/
def kaehler_differential.End_equiv_aux_equiv :
{f // (ideal.quotient.mkₐ R (kaehler_differential.ideal R S).cotangent_ideal).comp f =
is_scalar_tower.to_alg_hom R S _ } ≃
{ f // (tensor_product.lmul' R : S ⊗[R] S →ₐ[R] S).ker_square_lift.comp f = alg_hom.id R S } :=
(equiv.refl _).subtype_equiv (kaehler_differential.End_equiv_aux R S)
/--
The endomorphisms of `Ω[S⁄R]` corresponds to sections of the surjection `S ⊗[R] S ⧸ J ^ 2 →ₐ[R] S`,
with `J` being the kernel of the multiplication map `S ⊗[R] S →ₐ[R] S`.
-/
noncomputable
def kaehler_differential.End_equiv :
module.End S Ω[S⁄R] ≃
{ f // (tensor_product.lmul' R : S ⊗[R] S →ₐ[R] S).ker_square_lift.comp f = alg_hom.id R S } :=
(kaehler_differential.linear_map_equiv_derivation R S).to_equiv.trans $
(kaehler_differential.End_equiv_derivation' R S).to_equiv.trans $
(derivation_to_square_zero_equiv_lift
(kaehler_differential.ideal R S).cotangent_ideal
(kaehler_differential.ideal R S).cotangent_ideal_square).trans $
kaehler_differential.End_equiv_aux_equiv R S
section presentation
open kaehler_differential (D)
open finsupp (single)
/-- The `S`-submodule of `S →₀ S` (the direct sum of copies of `S` indexed by `S`) generated by
the relations:
1. `dx + dy = d(x + y)`
2. `x dy + y dx = d(x * y)`
3. `dr = 0` for `r ∈ R`
where `db` is the unit in the copy of `S` with index `b`.
This is the kernel of the surjection `finsupp.total S Ω[S⁄R] S (kaehler_differential.D R S)`.
See `kaehler_differential.ker_total_eq` and `kaehler_differential.total_surjective`.
-/
noncomputable
def kaehler_differential.ker_total : submodule S (S →₀ S) :=
submodule.span S
((set.range (λ (x : S × S), single x.1 1 + single x.2 1 - single (x.1 + x.2) 1)) ∪
(set.range (λ (x : S × S), single x.2 x.1 + single x.1 x.2 - single (x.1 * x.2) 1)) ∪
(set.range (λ x : R, single (algebra_map R S x) 1)))
local notation x `𝖣` y := (kaehler_differential.ker_total R S).mkq (single y x)
lemma kaehler_differential.ker_total_mkq_single_add (x y z) :
(z 𝖣 (x + y)) = (z 𝖣 x) + (z 𝖣 y) :=
begin
rw [← map_add, eq_comm, ← sub_eq_zero, ← map_sub, submodule.mkq_apply,
submodule.quotient.mk_eq_zero],
simp_rw [← finsupp.smul_single_one _ z, ← smul_add, ← smul_sub],
exact submodule.smul_mem _ _ (submodule.subset_span (or.inl $ or.inl $ ⟨⟨_, _⟩, rfl⟩)),
end
lemma kaehler_differential.ker_total_mkq_single_mul (x y z) :
(z 𝖣 (x * y)) = ((z * x) 𝖣 y) + ((z * y) 𝖣 x) :=
begin
rw [← map_add, eq_comm, ← sub_eq_zero, ← map_sub, submodule.mkq_apply,
submodule.quotient.mk_eq_zero],
simp_rw [← finsupp.smul_single_one _ z, ← @smul_eq_mul _ _ z,
← finsupp.smul_single, ← smul_add, ← smul_sub],
exact submodule.smul_mem _ _ (submodule.subset_span (or.inl $ or.inr $ ⟨⟨_, _⟩, rfl⟩)),
end
lemma kaehler_differential.ker_total_mkq_single_algebra_map (x y) :
(y 𝖣 (algebra_map R S x)) = 0 :=
begin
rw [submodule.mkq_apply, submodule.quotient.mk_eq_zero, ← finsupp.smul_single_one _ y],
exact submodule.smul_mem _ _ (submodule.subset_span (or.inr $ ⟨_, rfl⟩)),
end
lemma kaehler_differential.ker_total_mkq_single_algebra_map_one (x) :
(x 𝖣 1) = 0 :=
begin
rw [← (algebra_map R S).map_one, kaehler_differential.ker_total_mkq_single_algebra_map],
end
lemma kaehler_differential.ker_total_mkq_single_smul (r : R) (x y) :
(y 𝖣 (r • x)) = r • (y 𝖣 x) :=
begin
rw [algebra.smul_def, kaehler_differential.ker_total_mkq_single_mul,
kaehler_differential.ker_total_mkq_single_algebra_map, add_zero,
← linear_map.map_smul_of_tower, finsupp.smul_single, mul_comm, algebra.smul_def],
end
/-- The (universal) derivation into `(S →₀ S) ⧸ kaehler_differential.ker_total R S`. -/
noncomputable
def kaehler_differential.derivation_quot_ker_total :
derivation R S ((S →₀ S) ⧸ kaehler_differential.ker_total R S) :=
{ to_fun := λ x, 1 𝖣 x,
map_add' := λ x y, kaehler_differential.ker_total_mkq_single_add _ _ _ _ _,
map_smul' := λ r s, kaehler_differential.ker_total_mkq_single_smul _ _ _ _ _,
map_one_eq_zero' := kaehler_differential.ker_total_mkq_single_algebra_map_one _ _ _,
leibniz' := λ a b, (kaehler_differential.ker_total_mkq_single_mul _ _ _ _ _).trans
(by { simp_rw [← finsupp.smul_single_one _ (1 * _ : S)], dsimp, simp }) }
lemma kaehler_differential.derivation_quot_ker_total_apply (x) :
kaehler_differential.derivation_quot_ker_total R S x = (1 𝖣 x) := rfl
lemma kaehler_differential.derivation_quot_ker_total_lift_comp_total :
(kaehler_differential.derivation_quot_ker_total R S).lift_kaehler_differential.comp
(finsupp.total S Ω[S⁄R] S (kaehler_differential.D R S)) = submodule.mkq _ :=
begin
apply finsupp.lhom_ext,
intros a b,
conv_rhs { rw [← finsupp.smul_single_one a b, linear_map.map_smul] },
simp [kaehler_differential.derivation_quot_ker_total_apply],
end
lemma kaehler_differential.ker_total_eq :
(finsupp.total S Ω[S⁄R] S (kaehler_differential.D R S)).ker =
kaehler_differential.ker_total R S :=
begin
apply le_antisymm,
{ conv_rhs { rw ← (kaehler_differential.ker_total R S).ker_mkq },
rw ← kaehler_differential.derivation_quot_ker_total_lift_comp_total,
exact linear_map.ker_le_ker_comp _ _ },
{ rw [kaehler_differential.ker_total, submodule.span_le],
rintros _ ((⟨⟨x, y⟩, rfl⟩|⟨⟨x, y⟩, rfl⟩)|⟨x, rfl⟩); dsimp; simp [linear_map.mem_ker] },
end
lemma kaehler_differential.total_surjective :
function.surjective (finsupp.total S Ω[S⁄R] S (kaehler_differential.D R S)) :=
begin
rw [← linear_map.range_eq_top, finsupp.range_total, kaehler_differential.span_range_derivation],
end
/-- `Ω[S⁄R]` is isomorphic to `S` copies of `S` with kernel `kaehler_differential.ker_total`. -/
@[simps] noncomputable
def kaehler_differential.quot_ker_total_equiv :
((S →₀ S) ⧸ kaehler_differential.ker_total R S) ≃ₗ[S] Ω[S⁄R] :=
{ inv_fun := (kaehler_differential.derivation_quot_ker_total R S).lift_kaehler_differential,
left_inv := begin
intro x,
obtain ⟨x, rfl⟩ := submodule.mkq_surjective _ x,
exact linear_map.congr_fun
(kaehler_differential.derivation_quot_ker_total_lift_comp_total R S : _) x,
end,
right_inv := begin
intro x,
obtain ⟨x, rfl⟩ := kaehler_differential.total_surjective R S x,
erw linear_map.congr_fun
(kaehler_differential.derivation_quot_ker_total_lift_comp_total R S : _) x,
refl
end,
..(kaehler_differential.ker_total R S).liftq
(finsupp.total S Ω[S⁄R] S (kaehler_differential.D R S))
(kaehler_differential.ker_total_eq R S).ge }
lemma kaehler_differential.quot_ker_total_equiv_symm_comp_D :
(kaehler_differential.quot_ker_total_equiv R S).symm.to_linear_map.comp_der
(kaehler_differential.D R S) = kaehler_differential.derivation_quot_ker_total R S :=
by convert
(kaehler_differential.derivation_quot_ker_total R S).lift_kaehler_differential_comp using 0
variables (A B : Type*) [comm_ring A] [comm_ring B] [algebra R A] [algebra S B] [algebra R B]
variables [algebra A B] [is_scalar_tower R S B] [is_scalar_tower R A B]
-- The map `(A →₀ A) →ₗ[A] (B →₀ B)`
local notation `finsupp_map` := ((finsupp.map_range.linear_map (algebra.linear_map A B))
.comp (finsupp.lmap_domain A A (algebra_map A B)))
lemma kaehler_differential.ker_total_map (h : function.surjective (algebra_map A B)) :
(kaehler_differential.ker_total R A).map finsupp_map ⊔
submodule.span A (set.range (λ x : S, single (algebra_map S B x) (1 : B))) =
(kaehler_differential.ker_total S B).restrict_scalars _ :=
begin
rw [kaehler_differential.ker_total, submodule.map_span, kaehler_differential.ker_total,
submodule.restrict_scalars_span _ _ h],
simp_rw [set.image_union, submodule.span_union, ← set.image_univ, set.image_image,
set.image_univ, map_sub, map_add],
simp only [linear_map.comp_apply, finsupp.map_range.linear_map_apply, finsupp.map_range_single,
finsupp.lmap_domain_apply, finsupp.map_domain_single, alg_hom.to_linear_map_apply,
algebra.linear_map_apply, ← is_scalar_tower.algebra_map_apply, map_one, map_add, map_mul],
simp_rw [sup_assoc, ← (h.prod_map h).range_comp],
congr' 3,
rw [sup_eq_right],
apply submodule.span_mono,
simp_rw is_scalar_tower.algebra_map_apply R S B,
exact set.range_comp_subset_range (algebra_map R S) (λ x, single (algebra_map S B x) (1 : B))
end
end presentation
section exact_sequence
/- We have the commutative diagram
A --→ B
↑ ↑
| |
R --→ S -/
variables (A B : Type*) [comm_ring A] [comm_ring B] [algebra R A] [algebra R B]
variables [algebra A B] [algebra S B] [is_scalar_tower R A B] [is_scalar_tower R S B]
variables {R B}
/-- For a tower `R → A → B` and an `R`-derivation `B → M`, we may compose with `A → B` to obtain an
`R`-derivation `A → M`. -/
def derivation.comp_algebra_map [module A M] [module B M] [is_scalar_tower A B M]
(d : derivation R B M) : derivation R A M :=
{ map_one_eq_zero' := by simp,
leibniz' := λ a b, by simp,
to_linear_map := d.to_linear_map.comp (is_scalar_tower.to_alg_hom R A B).to_linear_map }
variables (R B) [smul_comm_class S A B]
/-- The map `Ω[A⁄R] →ₗ[A] Ω[B⁄R]` given a square
A --→ B
↑ ↑
| |
R --→ S -/
def kaehler_differential.map : Ω[A⁄R] →ₗ[A] Ω[B⁄S] :=
derivation.lift_kaehler_differential
(((kaehler_differential.D S B).restrict_scalars R).comp_algebra_map A)
lemma kaehler_differential.map_comp_der :
(kaehler_differential.map R S A B).comp_der (kaehler_differential.D R A) =
(((kaehler_differential.D S B).restrict_scalars R).comp_algebra_map A) :=
derivation.lift_kaehler_differential_comp _
lemma kaehler_differential.map_D (x : A) :
kaehler_differential.map R S A B (kaehler_differential.D R A x) =
kaehler_differential.D S B (algebra_map A B x) :=
derivation.congr_fun (kaehler_differential.map_comp_der R S A B) x
open is_scalar_tower (to_alg_hom)
lemma kaehler_differential.map_surjective_of_surjective
(h : function.surjective (algebra_map A B)) :
function.surjective (kaehler_differential.map R S A B) :=
begin
rw [← linear_map.range_eq_top, _root_.eq_top_iff, ← @submodule.restrict_scalars_top B A,
← kaehler_differential.span_range_derivation, submodule.restrict_scalars_span _ _ h,
submodule.span_le],
rintros _ ⟨x, rfl⟩,
obtain ⟨y, rfl⟩ := h x,
rw ← kaehler_differential.map_D R S A B,
exact ⟨_, rfl⟩,
end
/-- The lift of the map `Ω[A⁄R] →ₗ[A] Ω[B⁄R]` to the base change along `A → B`.
This is the first map in the exact sequence `B ⊗[A] Ω[A⁄R] → Ω[B⁄R] → Ω[B⁄A] → 0`. -/
noncomputable
def kaehler_differential.map_base_change : B ⊗[A] Ω[A⁄R] →ₗ[B] Ω[B⁄R] :=
(tensor_product.is_base_change A Ω[A⁄R] B).lift (kaehler_differential.map R R A B)
@[simp]
lemma kaehler_differential.map_base_change_tmul (x : B) (y : Ω[A⁄R]) :
kaehler_differential.map_base_change R A B (x ⊗ₜ y) =
x • kaehler_differential.map R R A B y :=
begin
conv_lhs { rw [← mul_one x, ← smul_eq_mul, ← tensor_product.smul_tmul', linear_map.map_smul] },
congr' 1,
exact is_base_change.lift_eq _ _ _
end
end exact_sequence
end kaehler_differential
|
33063a0547a5f1cd9a846df13b20ce545a1b1ef9 | e42ba0bcfa11e5be053e2ce1ce1f49f027680525 | /src/separation/tactic.lean | 440f4872ca69a10a1f703b8ad656e996b1279942 | [] | no_license | unitb/separation-logic | 8e17c5b466f86caa143265f4c04c78eec9d3c3b8 | bdde6fc8f16fd43932aea9827d6c63cadd91c2e8 | refs/heads/master | 1,540,936,751,653 | 1,535,050,181,000 | 1,535,056,425,000 | 109,461,809 | 6 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 14,124 | lean |
import data.dlist
import separation.specification
import tactic.monotonicity
import util.meta.tactic
universes u v
namespace tactic.interactive
open applicative
open lean.parser
open interactive
open interactive.types
open tactic functor list nat separation
local postfix `?`:9001 := optional
local postfix *:9001 := many
@[user_attribute]
meta def ptr_abstraction : user_attribute :=
{ name := `ptr_abstraction
, descr := "Abstraction predicate for pointer structures" }
meta def mk_sep_assert : list expr → expr
| [] := `(emp)
| (e :: []) := e
| (e :: es) := `(%%e :*: %%(mk_sep_assert es))
meta inductive assert_with_vars
| s_and : assert_with_vars → assert_with_vars → assert_with_vars
| s_exists : expr → assert_with_vars → assert_with_vars
| leaf : expr → assert_with_vars
| prop : expr → assert_with_vars
open predicate
meta def parse_assert_with_vars : expr → tactic assert_with_vars
| `(s_and %%e₀ %%e₁) := assert_with_vars.s_and <$> parse_assert_with_vars e₀
<*> parse_assert_with_vars e₁
| `(p_exists %%e) :=
do `(λ _ : %%t, %%e') ← pure e,
assert_with_vars.s_exists t <$> parse_assert_with_vars e'
| `([| %%e |]) :=
return $ assert_with_vars.prop e
| e := return $ assert_with_vars.leaf e
meta def assert_with_vars.to_expr : assert_with_vars → pexpr
| (assert_with_vars.leaf e) := to_pexpr e
| (assert_with_vars.prop e) := ``([| %%e |])
| (assert_with_vars.s_and e₀ e₁) := ``(%%(e₀.to_expr) :*: %%(e₁.to_expr))
| (assert_with_vars.s_exists t e) := ``(p_exists %%(expr.lam `x binder_info.default (to_pexpr t) e.to_expr))
meta def hexists_to_meta_var (rules : list simp_arg_type)
: list expr → assert_with_vars → tactic (assert_with_vars × expr)
| vs (assert_with_vars.s_and e₀ e₁) :=
do (x₀,p₀) ← hexists_to_meta_var vs e₀,
(x₁,p₁) ← hexists_to_meta_var vs e₁,
let x := assert_with_vars.s_and x₀ x₁,
prod.mk x <$> to_expr ``(s_and_s_imp_s_and %%p₀ %%p₁)
| vs (assert_with_vars.s_exists t e) :=
do v ← mk_meta_var t,
(x,p) ← hexists_to_meta_var (v :: vs) e,
let x := assert_with_vars.s_exists t x,
q ← expr.lam `x binder_info.default t <$> to_expr e.to_expr,
prod.mk x <$> to_expr ``(@s_exists_intro _ _ %%q %%v %%p)
| vs (assert_with_vars.prop e) :=
do let x := assert_with_vars.leaf `(emp),
p ← mk_meta_var e,
prod.mk x <$> to_expr ``(s_imp_of_eq (eq.symm (embed_eq_emp %%p)))
| vs (assert_with_vars.leaf e) :=
do let e' := e.instantiate_vars vs.reverse,
(r,u) ← mk_simp_set tt [] rules,
(e'',p) ← conv.convert (conv.interactive.simp tt rules []
{ fail_if_unchanged := ff }) e',
ast ← parse_assert_with_vars e'' ,
(assert_with_vars.leaf _) ← pure ast | (do
(x,p') ← hexists_to_meta_var [] ast,
prod.mk x <$> to_expr ``(s_imp_trans _ %%p' (s_imp_of_eq (eq.symm %%p)))),
prod.mk ast <$> to_expr ``(s_imp_of_eq (eq.symm %%p))
meta def parse_sep_assert' : expr → tactic (dlist expr)
| `(%%e₀ :*: %%e₁) := (++) <$> parse_sep_assert' e₀ <*> parse_sep_assert' e₁
| e := return $ dlist.singleton e
meta def parse_sep_assert : expr → tactic (list expr) :=
map dlist.to_list ∘ parse_sep_assert'
meta def match_sep' (unif : bool)
: list expr → list expr → tactic (list expr × list expr × list expr)
| es (x :: xs) := do
es' ← delete_expr { unify := unif } x es,
match es' with
| (some es') := do
(c,l,r) ← match_sep' es' xs, return (x::c,l,r)
| none := do
(c,l,r) ← match_sep' es xs, return (c,l,x::r)
end
| es [] := do
return ([],es,[])
/--
`(common,left,right) ← match_sep unif l r` finds the commonalities
between `l` and `r` and returns the differences -/
meta def match_sep (unif : bool) (l : list expr) (r : list expr)
: tactic (list expr × list expr × list expr) :=
do (s',l',r') ← match_sep' unif l r,
s' ← mmap instantiate_mvars s',
l' ← mmap instantiate_mvars l',
r' ← mmap instantiate_mvars r',
return (s',l',r')
def expr_pat (t₀ t₁ : Type) : ℕ → Type
| 0 := t₁
| (succ n) := t₀ → expr_pat n
def tuple : ℕ → Type → Type
| 0 _ := unit
| 1 t := t
| (succ n) t := t × tuple n t
meta def match_expr : ∀ (n : ℕ) (p : expr_pat expr pexpr n) (e : expr), tactic (tuple n expr)
| 0 p e := to_expr p >>= unify e
| 1 p e := do v ← mk_mvar, match_expr 0 (p v) e, instantiate_mvars v
| (succ (succ n)) p e := do
v ← mk_mvar,
r ← match_expr (succ n) (p v) e,
e ← instantiate_mvars v,
return (e,r)
meta def reshuffle (e₀ e₁ : expr) : tactic unit := do
t ← target,
(t₀,t₁) ← match_eq t,
h₀ ← to_expr ``(%%t₀ = %%e₀) >>= assert `h₀,
solve1 ac_refl,
h₁ ← to_expr ``(%%t₁ = %%e₁) >>= assert `h₁,
solve1 admit,
`[rw h₀],
`[rw h₁],
tactic.clear h₀, tactic.clear h₁
meta def find_match (pat : expr) : expr → list expr → tactic expr
| e rest := do
(unify e pat >> return (mk_sep_assert rest))
<|>
(do hprop ← to_expr ``(hprop),
lv ← mk_meta_var hprop,
rv ← mk_meta_var hprop,
to_expr ``(%%lv :*: %%rv) >>= unify e,
-- this unification could be generalized to:
-- (le,re) ← match_pat (λ p₀ p₁, ``(%%p₀ :*: %%p₁))
le ← instantiate_mvars lv,
re ← instantiate_mvars rv,
(find_match le (re :: rest) <|> find_match re (le :: rest)))
<|>
do p ← pp pat,
e ← pp e,
fail $ to_fmt "no match found for `" ++ p ++ to_fmt "` in: \n`" ++ e ++ to_fmt "`"
meta def sep_goal : tactic (expr × expr × expr) := do
g ← target,
t ← to_expr ``(hprop),
e₀ ← mk_meta_var t,
e₂ ← mk_meta_var t,
e₃ ← mk_meta_var t,
pat ← to_expr ``(%%e₀ = %%e₂ :*: %%e₃) >>= unify g,
prod.mk <$> instantiate_mvars e₀
<*> (prod.mk <$> instantiate_mvars e₂
<*> instantiate_mvars e₃)
/-- Apply on a goal of the form
``x = y :*: m?``
with m? a meta variable. The goal is to decompose `x` into a conjunction
made of an occurrence of `y` (anywhere).
-/
meta def match_assert : tactic unit := do
(hp,pat,var) ← sep_goal,
e ← find_match pat hp [],
unify e var,
tactic.target >>= instantiate_mvars >>= tactic.change,
try `[simp] >> ac_refl
/-- apply on a goal of the form `sat p spec` -/
meta def extract_context_aux (h : name) (subst_flag : bool) : tactic unit :=
do `[apply precondition _],
swap,
`[symmetry],
solve1 (do
-- cxt ← mk_meta_var `(Prop),
(hp,pat,var) ← sep_goal,
to_expr ``( [| _ |] ) >>= unify pat,
e ← find_match pat hp [],
unify e var,
tactic.target >>= instantiate_mvars >>= tactic.change,
try `[simp],
try ac_refl),
`[apply context_left],
x ← tactic.intro h,
when subst_flag $ try (tactic.subst x),
return ()
meta def subst_flag := (tk "with" *> tk "subst" *> pure tt) <|> return ff
meta def extract_context
: parse ident* → ∀ (x : parse subst_flag), tactic unit
| [] x := return ()
| (h :: hs) x := extract_context_aux h x >> extract_context hs x
meta def match_sep_imp : expr → tactic (expr × expr)
| `(%%e₀ =*> %%e₁) := return (e₀, e₁)
| _ := fail "expression is not an sep implication"
meta def intro_unit (v : parse ident_?) : tactic unit :=
do e ← match v with
| none := intro1
| (some v) := tactic.intro v
end,
t ← infer_type e,
if t = `(unit)
then () <$ tactic.cases e
else return ()
meta def simp_ptr_abstr : tactic unit :=
do abs ← attribute.get_instances `ptr_abstraction,
abs' ← mmap (map simp_arg_type.expr ∘ resolve_name) abs,
try (dsimp tt abs' [] (loc.ns [none])),
try `[simp [s_and_s_exists_distr,s_exists_s_and_distr]
{ fail_if_unchanged := ff }]
meta def ac_match' : tactic unit :=
do abs ← attribute.get_instances `ptr_abstraction
>>= mmap (map simp_arg_type.expr ∘ resolve_name),
try (simp none tt abs [] (loc.ns [none])),
try `[simp [s_and_s_exists_distr,s_exists_s_and_distr]
{ fail_if_unchanged := ff }],
repeat `[apply s_exists_elim, intro_unit],
repeat `[apply s_exists_intro],
try (unfold_projs (loc.ns [])),
done <|> focus1 (do
repeat `[rw [embed_eq_emp],
simp { fail_if_unchanged := ff } ],
all_goals (try assumption)),
done <|> (do
solve1 (do
try `[apply s_imp_of_eq],
t ← target,
(e₀,e₁) ← match_eq t,
e₀ ← parse_sep_assert e₀,
e₁ ← parse_sep_assert e₁,
(c,l,r) ← match_sep ff e₀ e₁,
(c',l',r') ← match_sep tt l r,
(c'',l',r') ← match_sep tt l' r',
let ll := l'.length,
let rl := r'.length,
let l'' := l' ++ list.repeat `(emp) (rl - ll),
h ← assert `h `(%%(mk_sep_assert l'') = (%%(mk_sep_assert r') : hprop)),
solve1 tactic.reflexivity,
target >>= instantiate_mvars >>= tactic.change,
try `[simp { fail_if_unchanged := ff }],
try `[rw s_and_comm, try { refl }],
try ac_refl))
meta def ac_match : tactic unit := do
ac_match'
example (e₁ e₂ e₃ : hprop)
: e₁ :*: e₂ :*: e₃ = e₂ :*: e₃ :*: e₁ :=
begin
ac_mono1,
exact @rfl _ emp
end
def replicate {m : Type u → Type v} [monad m] {α} : ℕ → m α → m (list α)
| 0 _ := return []
| (succ n) m := lift₂ cons m (replicate n m)
private meta def get_pi_expl_arity_aux (t : expr) : expr → expr → tactic expr
| e r := do
(unify t e >> instantiate_mvars r)
<|>
match e with
| (expr.pi n bi d b) :=
do m ← mk_fresh_name,
-- let l := expr.local_const m n bi d,
l ← mk_meta_var d,
new_b ← instantiate_mvars (expr.instantiate_var b l),
if binder_info.default = bi
then get_pi_expl_arity_aux new_b (r l)
else get_pi_expl_arity_aux new_b r
| e := instantiate_mvars r
end
/-- Compute the arity of the given (Pi-)type -/
meta def get_pi_expl_arity (target e : expr) : tactic expr := do
t ← infer_type e,
get_pi_expl_arity_aux target t e
meta def s_exists1 (v : parse ident) : tactic unit := do
`[ simp [s_exists_s_and_distr,s_and_s_exists_distr] { fail_if_unchanged := ff }
, apply s_exists_intro_pre],
intro v, return ()
meta def s_exists (vs : parse ident*) : tactic unit :=
mmap' s_exists1 vs
meta def s_intros : parse ident* → parse subst_flag → tactic unit
| [] _ := return ()
| (x :: xs) sbst := do
v ← tactic.try_core (s_exists1 x),
match v with
| (some _) := s_intros xs sbst
| none := extract_context (x :: xs) sbst
end
meta def decide : tactic unit := do
solve1 $ do
`[apply of_as_true],
triv
meta def bind_step (spec_thm : parse texpr? ) (ids : parse with_ident_list) : tactic unit :=
do g ← target,
(hd,tl,spec) ← (match_expr 3 (λ e₀ e₁ s, ``(sat (%%e₀ >>= %%e₁) %%s)) g
: tactic (expr × expr × expr)),
let (cmd,args) := hd.get_app_fn_args,
let s : option _ := spec_thm,
e ← (resolve_name (cmd.const_name <.> "spec") >>= to_expr) <| (to_expr <$> s),
r ← to_expr ``(sat _ _),
e' ← get_pi_expl_arity r e,
`[apply (bind_framing_left _ %%e')],
solve1 (try `[simp [s_and_assoc]] >> try ac_match'),
all_goals (try `[apply of_as_true, apply trivial]),
(v,ids) ← return $ match ids with
| [] := (none,[])
| (id :: ids) := (some id, ids)
end,
intro_unit v, `[simp],
s_intros ids tt
open option
meta def simp_h_entails : tactic unit :=
do `(%%p =*> %%q) ← target,
r ← parse_assert_with_vars q,
abs ← attribute.get_instances `ptr_abstraction,
abs' ← mmap (map (simp_arg_type.expr ∘ to_pexpr) ∘ resolve_name) abs,
(_,p) ← hexists_to_meta_var abs' [] r,
h ← mk_meta_var `(hprop),
g ← mk_mvar,
p' ← to_expr ``(s_imp_trans %%h %%g %%p),
t ← target,
infer_type p' >>= unify t,
gs ← get_goals,
set_goals [g],
(applyc `separation.s_imp_of_eq >> ac_match'),
done,
set_goals gs,
() <$ tactic.apply p' { new_goals := new_goals.non_dep_only }
meta def last_step' (s : parse texpr?) : tactic unit :=
solve1 $
do `(sat %%hd %%spec) ← target,
let (cmd,args) := hd.get_app_fn_args,
e ← (resolve_name (cmd.const_name <.> "spec") >>= to_expr) <| (to_expr <$> s),
r ← to_expr ``(sat _ _),
e' ← get_pi_expl_arity r e,
p₀ ← mk_mvar,
p₁ ← mk_mvar,
r ← to_expr ``(framing_spec' _ %%e' %%p₀ %%p₁),
t ← target,
infer_type r >>= unify t,
gs ← get_goals,
set_goals [p₀],
simp_h_entails,
all_goals (try $ `[apply of_as_true, apply trivial] <|> solve_by_elim),
done,
set_goals [p₁],
intro1,
simp_h_entails,
all_goals (try $ `[apply of_as_true, apply trivial] <|> solve_by_elim),
done,
set_goals gs,
tactic.apply r,
return ()
meta def last_step (spec : parse texpr?) : tactic unit :=
do last_step' spec,
solve1 (do
try `[simp [s_and_assoc]],
ac_match') <|> fail "first solve1",
solve1 (do
intro_unit `_,
ac_match',
all_goals (try assumption)) <|> fail "second solve1",
all_goals (try $ `[apply of_as_true, apply trivial] <|> solve_by_elim)
-- meta def himp_zoom : tactic unit :=
-- do `(%%lhs =*> %%rhs) ← target | failed,
-- ls ← parse_sep_assert lhs,
-- rs ← parse_sep_assert rhs,
-- (s,l,r) ← match_sep ff ls rs,
-- let s' := mk_sep_assert s,
-- let lhs' := mk_sep_assert (l ++ s),
-- let rhs' := mk_sep_assert (r ++ s),
-- h ← to_expr ``(%%(mk_sep_assert l) =*> %%(mk_sep_assert r))
-- >>= assert `h,
-- tactic.swap,
-- prf ← to_expr ``(s_and_s_imp_s_and_right %%(mk_sep_assert s) %%h),
-- note `h none prf,
-- return ()
example (e₁ e₂ e₃ e₄ e₅ e₆ : hprop)
(h : e₃ :*: e₁ :*: e₅ :*: e₄ =*> e₆)
: e₃ :*: e₁ :*: e₅ :*: e₂ :*: e₄ =*> e₂ :*: e₆ :=
begin
ac_mono1,
solve_by_elim
end
example (e₁ e₂ e₃ e₄ e₅ e₆ : hprop)
(h : e₁ :*: e₅ :*: e₄ =*> emp)
: e₃ :*: e₁ :*: e₅ :*: e₂ :*: e₄ =*> e₂ :*: e₃ :=
begin
ac_mono1,
solve_by_elim
end
end tactic.interactive
|
90381d2b5421278cf11ee1fc2d3ee74ab90ddd7e | c777c32c8e484e195053731103c5e52af26a25d1 | /src/analysis/p_series.lean | 3418373f0dab9f2693d99f1f060e6de313095e0b | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 13,508 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import analysis.special_functions.pow
/-!
# Convergence of `p`-series
In this file we prove that the series `∑' k in ℕ, 1 / k ^ p` converges if and only if `p > 1`.
The proof is based on the
[Cauchy condensation test](https://en.wikipedia.org/wiki/Cauchy_condensation_test): `∑ k, f k`
converges if and only if so does `∑ k, 2 ^ k f (2 ^ k)`. We prove this test in
`nnreal.summable_condensed_iff` and `summable_condensed_iff_of_nonneg`, then use it to prove
`summable_one_div_rpow`. After this transformation, a `p`-series turns into a geometric series.
## TODO
It should be easy to generalize arguments to Schlömilch's generalization of the Cauchy condensation
test once we need it.
## Tags
p-series, Cauchy condensation test
-/
open filter
open_locale big_operators ennreal nnreal topology
/-!
### Cauchy condensation test
In this section we prove the Cauchy condensation test: for `f : ℕ → ℝ≥0` or `f : ℕ → ℝ`,
`∑ k, f k` converges if and only if so does `∑ k, 2 ^ k f (2 ^ k)`. Instead of giving a monolithic
proof, we split it into a series of lemmas with explicit estimates of partial sums of each series in
terms of the partial sums of the other series.
-/
namespace finset
variables {M : Type*} [ordered_add_comm_monoid M] {f : ℕ → M}
lemma le_sum_condensed' (hf : ∀ ⦃m n⦄, 0 < m → m ≤ n → f n ≤ f m) (n : ℕ) :
(∑ k in Ico 1 (2 ^ n), f k) ≤ ∑ k in range n, (2 ^ k) • f (2 ^ k) :=
begin
induction n with n ihn, { simp },
suffices : (∑ k in Ico (2 ^ n) (2 ^ (n + 1)), f k) ≤ (2 ^ n) • f (2 ^ n),
{ rw [sum_range_succ, ← sum_Ico_consecutive],
exact add_le_add ihn this,
exacts [n.one_le_two_pow, nat.pow_le_pow_of_le_right zero_lt_two n.le_succ] },
have : ∀ k ∈ Ico (2 ^ n) (2 ^ (n + 1)), f k ≤ f (2 ^ n) :=
λ k hk, hf (pow_pos zero_lt_two _) (mem_Ico.mp hk).1,
convert sum_le_sum this,
simp [pow_succ, two_mul]
end
lemma le_sum_condensed (hf : ∀ ⦃m n⦄, 0 < m → m ≤ n → f n ≤ f m) (n : ℕ) :
(∑ k in range (2 ^ n), f k) ≤ f 0 + ∑ k in range n, (2 ^ k) • f (2 ^ k) :=
begin
convert add_le_add_left (le_sum_condensed' hf n) (f 0),
rw [← sum_range_add_sum_Ico _ n.one_le_two_pow, sum_range_succ, sum_range_zero, zero_add]
end
lemma sum_condensed_le' (hf : ∀ ⦃m n⦄, 1 < m → m ≤ n → f n ≤ f m) (n : ℕ) :
(∑ k in range n, (2 ^ k) • f (2 ^ (k + 1))) ≤ ∑ k in Ico 2 (2 ^ n + 1), f k :=
begin
induction n with n ihn, { simp },
suffices : (2 ^ n) • f (2 ^ (n + 1)) ≤ ∑ k in Ico (2 ^ n + 1) (2 ^ (n + 1) + 1), f k,
{ rw [sum_range_succ, ← sum_Ico_consecutive],
exact add_le_add ihn this,
exacts [add_le_add_right n.one_le_two_pow _,
add_le_add_right (nat.pow_le_pow_of_le_right zero_lt_two n.le_succ) _] },
have : ∀ k ∈ Ico (2 ^ n + 1) (2 ^ (n + 1) + 1), f (2 ^ (n + 1)) ≤ f k :=
λ k hk, hf (n.one_le_two_pow.trans_lt $ (nat.lt_succ_of_le le_rfl).trans_le (mem_Ico.mp hk).1)
(nat.le_of_lt_succ $ (mem_Ico.mp hk).2),
convert sum_le_sum this,
simp [pow_succ, two_mul]
end
lemma sum_condensed_le (hf : ∀ ⦃m n⦄, 1 < m → m ≤ n → f n ≤ f m) (n : ℕ) :
(∑ k in range (n + 1), (2 ^ k) • f (2 ^ k)) ≤ f 1 + 2 • ∑ k in Ico 2 (2 ^ n + 1), f k :=
begin
convert add_le_add_left (nsmul_le_nsmul_of_le_right (sum_condensed_le' hf n) 2) (f 1),
simp [sum_range_succ', add_comm, pow_succ, mul_nsmul, sum_nsmul]
end
end finset
namespace ennreal
variable {f : ℕ → ℝ≥0∞}
lemma le_tsum_condensed (hf : ∀ ⦃m n⦄, 0 < m → m ≤ n → f n ≤ f m) :
∑' k, f k ≤ f 0 + ∑' k : ℕ, (2 ^ k) * f (2 ^ k) :=
begin
rw [ennreal.tsum_eq_supr_nat' (nat.tendsto_pow_at_top_at_top_of_one_lt _root_.one_lt_two)],
refine supr_le (λ n, (finset.le_sum_condensed hf n).trans (add_le_add_left _ _)),
simp only [nsmul_eq_mul, nat.cast_pow, nat.cast_two],
apply ennreal.sum_le_tsum
end
lemma tsum_condensed_le (hf : ∀ ⦃m n⦄, 1 < m → m ≤ n → f n ≤ f m) :
∑' k : ℕ, (2 ^ k * f (2 ^ k)) ≤ f 1 + 2 * ∑' k, f k :=
begin
rw [ennreal.tsum_eq_supr_nat' (tendsto_at_top_mono nat.le_succ tendsto_id), two_mul, ← two_nsmul],
refine supr_le (λ n, le_trans _ (add_le_add_left (nsmul_le_nsmul_of_le_right
(ennreal.sum_le_tsum $ finset.Ico 2 (2^n + 1)) _) _)),
simpa using finset.sum_condensed_le hf n
end
end ennreal
namespace nnreal
/-- Cauchy condensation test for a series of `nnreal` version. -/
lemma summable_condensed_iff {f : ℕ → ℝ≥0} (hf : ∀ ⦃m n⦄, 0 < m → m ≤ n → f n ≤ f m) :
summable (λ k : ℕ, (2 ^ k) * f (2 ^ k)) ↔ summable f :=
begin
simp only [← ennreal.tsum_coe_ne_top_iff_summable, ne.def, not_iff_not, ennreal.coe_mul,
ennreal.coe_pow, ennreal.coe_two],
split; intro h,
{ replace hf : ∀ m n, 1 < m → m ≤ n → (f n : ℝ≥0∞) ≤ f m :=
λ m n hm hmn, ennreal.coe_le_coe.2 (hf (zero_lt_one.trans hm) hmn),
simpa [h, ennreal.add_eq_top, ennreal.mul_eq_top] using ennreal.tsum_condensed_le hf },
{ replace hf : ∀ m n, 0 < m → m ≤ n → (f n : ℝ≥0∞) ≤ f m :=
λ m n hm hmn, ennreal.coe_le_coe.2 (hf hm hmn),
simpa [h, ennreal.add_eq_top] using (ennreal.le_tsum_condensed hf) }
end
end nnreal
/-- Cauchy condensation test for series of nonnegative real numbers. -/
lemma summable_condensed_iff_of_nonneg {f : ℕ → ℝ} (h_nonneg : ∀ n, 0 ≤ f n)
(h_mono : ∀ ⦃m n⦄, 0 < m → m ≤ n → f n ≤ f m) :
summable (λ k : ℕ, (2 ^ k) * f (2 ^ k)) ↔ summable f :=
begin
lift f to ℕ → ℝ≥0 using h_nonneg,
simp only [nnreal.coe_le_coe] at *,
exact_mod_cast nnreal.summable_condensed_iff h_mono
end
open real
/-!
### Convergence of the `p`-series
In this section we prove that for a real number `p`, the series `∑' n : ℕ, 1 / (n ^ p)` converges if
and only if `1 < p`. There are many different proofs of this fact. The proof in this file uses the
Cauchy condensation test we formalized above. This test implies that `∑ n, 1 / (n ^ p)` converges if
and only if `∑ n, 2 ^ n / ((2 ^ n) ^ p)` converges, and the latter series is a geometric series with
common ratio `2 ^ {1 - p}`. -/
/-- Test for convergence of the `p`-series: the real-valued series `∑' n : ℕ, (n ^ p)⁻¹` converges
if and only if `1 < p`. -/
@[simp] lemma real.summable_nat_rpow_inv {p : ℝ} : summable (λ n, (n ^ p)⁻¹ : ℕ → ℝ) ↔ 1 < p :=
begin
cases le_or_lt 0 p with hp hp,
/- Cauchy condensation test applies only to antitone sequences, so we consider the
cases `0 ≤ p` and `p < 0` separately. -/
{ rw ← summable_condensed_iff_of_nonneg,
{ simp_rw [nat.cast_pow, nat.cast_two, ← rpow_nat_cast, ← rpow_mul zero_lt_two.le, mul_comm _ p,
rpow_mul zero_lt_two.le, rpow_nat_cast, ← inv_pow, ← mul_pow,
summable_geometric_iff_norm_lt_1],
nth_rewrite 0 [← rpow_one 2],
rw [← division_def, ← rpow_sub zero_lt_two, norm_eq_abs,
abs_of_pos (rpow_pos_of_pos zero_lt_two _), rpow_lt_one_iff zero_lt_two.le],
norm_num },
{ intro n,
exact inv_nonneg.2 (rpow_nonneg_of_nonneg n.cast_nonneg _) },
{ intros m n hm hmn,
exact inv_le_inv_of_le (rpow_pos_of_pos (nat.cast_pos.2 hm) _)
(rpow_le_rpow m.cast_nonneg (nat.cast_le.2 hmn) hp) } },
/- If `p < 0`, then `1 / n ^ p` tends to infinity, thus the series diverges. -/
{ suffices : ¬summable (λ n, (n ^ p)⁻¹ : ℕ → ℝ),
{ have : ¬(1 < p) := λ hp₁, hp.not_le (zero_le_one.trans hp₁.le),
simpa [this, -one_div] },
{ intro h,
obtain ⟨k : ℕ, hk₁ : ((k ^ p)⁻¹ : ℝ) < 1, hk₀ : k ≠ 0⟩ :=
((h.tendsto_cofinite_zero.eventually (gt_mem_nhds zero_lt_one)).and
(eventually_cofinite_ne 0)).exists,
apply hk₀,
rw [← pos_iff_ne_zero, ← @nat.cast_pos ℝ] at hk₀,
simpa [inv_lt_one_iff_of_pos (rpow_pos_of_pos hk₀ _), one_lt_rpow_iff_of_pos hk₀, hp,
hp.not_lt, hk₀] using hk₁ } }
end
@[simp] lemma real.summable_nat_rpow {p : ℝ} : summable (λ n, n ^ p : ℕ → ℝ) ↔ p < -1 :=
by { rcases neg_surjective p with ⟨p, rfl⟩, simp [rpow_neg] }
/-- Test for convergence of the `p`-series: the real-valued series `∑' n : ℕ, 1 / n ^ p` converges
if and only if `1 < p`. -/
lemma real.summable_one_div_nat_rpow {p : ℝ} : summable (λ n, 1 / n ^ p : ℕ → ℝ) ↔ 1 < p :=
by simp
/-- Test for convergence of the `p`-series: the real-valued series `∑' n : ℕ, (n ^ p)⁻¹` converges
if and only if `1 < p`. -/
@[simp] lemma real.summable_nat_pow_inv {p : ℕ} : summable (λ n, (n ^ p)⁻¹ : ℕ → ℝ) ↔ 1 < p :=
by simp only [← rpow_nat_cast, real.summable_nat_rpow_inv, nat.one_lt_cast]
/-- Test for convergence of the `p`-series: the real-valued series `∑' n : ℕ, 1 / n ^ p` converges
if and only if `1 < p`. -/
lemma real.summable_one_div_nat_pow {p : ℕ} : summable (λ n, 1 / n ^ p : ℕ → ℝ) ↔ 1 < p :=
by simp
/-- Summability of the `p`-series over `ℤ`. -/
lemma real.summable_one_div_int_pow {p : ℕ} : summable (λ n:ℤ, 1 / (n : ℝ) ^ p) ↔ 1 < p :=
begin
refine ⟨λ h, real.summable_one_div_nat_pow.mp (h.comp_injective nat.cast_injective),
λ h, summable_int_of_summable_nat (real.summable_one_div_nat_pow.mpr h)
(((real.summable_one_div_nat_pow.mpr h).mul_left $ 1 / (-1) ^ p).congr $ λ n, _)⟩,
conv_rhs { rw [int.cast_neg, neg_eq_neg_one_mul, mul_pow, ←div_div] },
conv_lhs { rw [mul_div, mul_one], },
refl,
end
lemma real.summable_abs_int_rpow {b : ℝ} (hb : 1 < b) : summable (λ n : ℤ, |(n : ℝ)| ^ (-b)) :=
begin
refine summable_int_of_summable_nat (_ : summable (λ n : ℕ, |(n : ℝ)| ^ _))
(_ : summable (λ n : ℕ, |((-n : ℤ) : ℝ)| ^ _)),
work_on_goal 2 { simp_rw [int.cast_neg, int.cast_coe_nat, abs_neg] },
all_goals { simp_rw (λ n : ℕ, abs_of_nonneg (n.cast_nonneg : 0 ≤ (n : ℝ))),
rwa [real.summable_nat_rpow, neg_lt_neg_iff] },
end
/-- Harmonic series is not unconditionally summable. -/
lemma real.not_summable_nat_cast_inv : ¬summable (λ n, n⁻¹ : ℕ → ℝ) :=
have ¬summable (λ n, (n^1)⁻¹ : ℕ → ℝ), from mt real.summable_nat_pow_inv.1 (lt_irrefl 1),
by simpa
/-- Harmonic series is not unconditionally summable. -/
lemma real.not_summable_one_div_nat_cast : ¬summable (λ n, 1 / n : ℕ → ℝ) :=
by simpa only [inv_eq_one_div] using real.not_summable_nat_cast_inv
/-- **Divergence of the Harmonic Series** -/
lemma real.tendsto_sum_range_one_div_nat_succ_at_top :
tendsto (λ n, ∑ i in finset.range n, (1 / (i + 1) : ℝ)) at_top at_top :=
begin
rw ← not_summable_iff_tendsto_nat_at_top_of_nonneg,
{ exact_mod_cast mt (summable_nat_add_iff 1).1 real.not_summable_one_div_nat_cast },
{ exact λ i, div_nonneg zero_le_one i.cast_add_one_pos.le }
end
@[simp] lemma nnreal.summable_rpow_inv {p : ℝ} : summable (λ n, (n ^ p)⁻¹ : ℕ → ℝ≥0) ↔ 1 < p :=
by simp [← nnreal.summable_coe]
@[simp] lemma nnreal.summable_rpow {p : ℝ} : summable (λ n, n ^ p : ℕ → ℝ≥0) ↔ p < -1 :=
by simp [← nnreal.summable_coe]
lemma nnreal.summable_one_div_rpow {p : ℝ} : summable (λ n, 1 / n ^ p : ℕ → ℝ≥0) ↔ 1 < p :=
by simp
section
open finset
variables {α : Type*} [linear_ordered_field α]
lemma sum_Ioc_inv_sq_le_sub {k n : ℕ} (hk : k ≠ 0) (h : k ≤ n) :
∑ i in Ioc k n, ((i ^ 2) ⁻¹ : α) ≤ k ⁻¹ - n ⁻¹ :=
begin
refine nat.le_induction _ _ n h,
{ simp only [Ioc_self, sum_empty, sub_self] },
assume n hn IH,
rw [sum_Ioc_succ_top hn],
apply (add_le_add IH le_rfl).trans,
simp only [sub_eq_add_neg, add_assoc, nat.cast_add, nat.cast_one, le_add_neg_iff_add_le,
add_le_iff_nonpos_right, neg_add_le_iff_le_add, add_zero],
have A : 0 < (n : α), by simpa using hk.bot_lt.trans_le hn,
have B : 0 < (n : α) + 1, by linarith,
field_simp [B.ne'],
rw [div_le_div_iff _ A, ← sub_nonneg],
{ ring_nf, exact B.le },
{ nlinarith },
end
lemma sum_Ioo_inv_sq_le (k n : ℕ) :
∑ i in Ioo k n, ((i ^ 2) ⁻¹ : α) ≤ 2 / (k + 1) :=
calc
∑ i in Ioo k n, ((i ^ 2) ⁻¹ : α) ≤ ∑ i in Ioc k (max (k+1) n), (i ^ 2) ⁻¹ :
begin
apply sum_le_sum_of_subset_of_nonneg,
{ assume x hx,
simp only [mem_Ioo] at hx,
simp only [hx, hx.2.le, mem_Ioc, le_max_iff, or_true, and_self] },
{ assume i hi hident,
exact inv_nonneg.2 (sq_nonneg _), }
end
... ≤ ((k + 1) ^ 2) ⁻¹ + ∑ i in Ioc k.succ (max (k + 1) n), (i ^ 2) ⁻¹ :
begin
rw [← nat.Icc_succ_left, ← nat.Ico_succ_right, sum_eq_sum_Ico_succ_bot],
swap, { exact nat.succ_lt_succ ((nat.lt_succ_self k).trans_le (le_max_left _ _)) },
rw [nat.Ico_succ_right, nat.Icc_succ_left, nat.cast_succ],
end
... ≤ ((k + 1) ^ 2) ⁻¹ + (k + 1) ⁻¹ :
begin
refine add_le_add le_rfl ((sum_Ioc_inv_sq_le_sub _ (le_max_left _ _)).trans _),
{ simp only [ne.def, nat.succ_ne_zero, not_false_iff] },
{ simp only [nat.cast_succ, one_div, sub_le_self_iff, inv_nonneg, nat.cast_nonneg] }
end
... ≤ 1 / (k + 1) + 1 / (k + 1) :
begin
have A : (1 : α) ≤ k + 1, by simp only [le_add_iff_nonneg_left, nat.cast_nonneg],
simp_rw ← one_div,
apply add_le_add_right,
refine div_le_div zero_le_one le_rfl (zero_lt_one.trans_le A) _,
simpa using pow_le_pow A one_le_two,
end
... = 2 / (k + 1) : by ring
end
|
91e4f6c21f4493f060d539136107dd1e113c63db | 97f752b44fd85ec3f635078a2dd125ddae7a82b6 | /hott/algebra/category/functor/yoneda.hlean | dda9e0a569fc3679c1d21e4a243081f15974a789 | [
"Apache-2.0"
] | permissive | tectronics/lean | ab977ba6be0fcd46047ddbb3c8e16e7c26710701 | f38af35e0616f89c6e9d7e3eb1d48e47ee666efe | refs/heads/master | 1,532,358,526,384 | 1,456,276,623,000 | 1,456,276,623,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,935 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
Yoneda embedding and Yoneda lemma
-/
import .examples .attributes
open category eq functor prod.ops is_trunc iso is_equiv equiv category.set nat_trans lift
namespace yoneda
/-
These attributes make sure that the fields of the category "set" reduce to the right things
However, we don't want to have them globally, because that will unfold the composition g ∘ f
in a Category to category.category.comp g f
-/
local attribute category.to_precategory [constructor]
-- should this be defined as "yoneda_embedding Cᵒᵖ"?
definition contravariant_yoneda_embedding [constructor] [reducible]
(C : Precategory) : Cᵒᵖ ⇒ cset ^c C :=
functor_curry !hom_functor
/-
we use (change_fun) to make sure that (to_fun_ob (yoneda_embedding C) c) will reduce to
(hom_functor_left c) instead of (functor_curry_rev_ob (hom_functor C) c)
-/
definition yoneda_embedding [constructor] (C : Precategory) : C ⇒ cset ^c Cᵒᵖ :=
--(functor_curry_rev !hom_functor)
change_fun
(functor_curry_rev !hom_functor)
hom_functor_left
nat_trans_hom_functor_left
idp
idpo
notation `ɏ` := yoneda_embedding _
definition yoneda_lemma_hom [constructor] {C : Precategory} (c : C) (F : Cᵒᵖ ⇒ cset)
(x : trunctype.carrier (F c)) : ɏ c ⟹ F :=
begin
fapply nat_trans.mk,
{ intro c', esimp [yoneda_embedding], intro f, exact F f x},
{ intro c' c'' f, esimp [yoneda_embedding], apply eq_of_homotopy, intro f',
refine _ ⬝ ap (λy, to_fun_hom F y x) !(@id_left _ C)⁻¹,
exact ap10 !(@respect_comp Cᵒᵖ cset)⁻¹ x}
end
definition yoneda_lemma_equiv [constructor] {C : Precategory} (c : C)
(F : Cᵒᵖ ⇒ cset) : hom (ɏ c) F ≃ lift (trunctype.carrier (to_fun_ob F c)) :=
begin
fapply equiv.MK,
{ intro η, exact up (η c id)},
{ intro x, induction x with x, exact yoneda_lemma_hom c F x},
{ exact abstract begin intro x, induction x with x, esimp, apply ap up,
exact ap10 !respect_id x end end},
{ exact abstract begin intro η, esimp, apply nat_trans_eq,
intro c', esimp, apply eq_of_homotopy,
intro f,
transitivity (F f ∘ η c) id, reflexivity,
rewrite naturality, esimp [yoneda_embedding], rewrite [id_left], apply ap _ !id_left end end},
end
definition yoneda_lemma {C : Precategory} (c : C) (F : Cᵒᵖ ⇒ cset) :
homset (ɏ c) F ≅ functor_lift (F c) :=
begin
apply iso_of_equiv, esimp, apply yoneda_lemma_equiv,
end
theorem yoneda_lemma_natural_ob {C : Precategory} (F : Cᵒᵖ ⇒ cset) {c c' : C} (f : c' ⟶ c)
(η : ɏ c ⟹ F) :
to_fun_hom (functor_lift ∘f F) f (to_hom (yoneda_lemma c F) η) =
to_hom (yoneda_lemma c' F) (η ∘n to_fun_hom ɏ f) :=
begin
esimp [yoneda_lemma,yoneda_embedding], apply ap up,
transitivity (F f ∘ η c) id, reflexivity,
rewrite naturality,
esimp [yoneda_embedding],
apply ap (η c'),
esimp [yoneda_embedding, Opposite],
rewrite [+id_left,+id_right],
end
-- TODO: Investigate what is the bottleneck to type check the next theorem
-- attribute yoneda_lemma functor_lift Precategory_Set precategory_Set homset
-- yoneda_embedding nat_trans.compose functor_nat_trans_compose [reducible]
-- attribute tlift functor.compose [reducible]
theorem yoneda_lemma_natural_functor.{u v} {C : Precategory.{u v}} (c : C) (F F' : Cᵒᵖ ⇒ cset)
(θ : F ⟹ F') (η : to_fun_ob ɏ c ⟹ F) :
(functor_lift.{v u} ∘fn θ) c (to_hom (yoneda_lemma c F) η) =
proof to_hom (yoneda_lemma c F') (θ ∘n η) qed :=
by reflexivity
-- theorem xx.{u v} {C : Precategory.{u v}} (c : C) (F F' : Cᵒᵖ ⇒ set)
-- (θ : F ⟹ F') (η : to_fun_ob ɏ c ⟹ F) :
-- proof _ qed =
-- to_hom (yoneda_lemma c F') (θ ∘n η) :=
-- by reflexivity
-- theorem yy.{u v} {C : Precategory.{u v}} (c : C) (F F' : Cᵒᵖ ⇒ set)
-- (θ : F ⟹ F') (η : to_fun_ob ɏ c ⟹ F) :
-- (functor_lift.{v u} ∘fn θ) c (to_hom (yoneda_lemma c F) η) =
-- proof _ qed :=
-- by reflexivity
definition fully_faithful_yoneda_embedding [instance] (C : Precategory) :
fully_faithful (ɏ : C ⇒ cset ^c Cᵒᵖ) :=
begin
intro c c',
fapply is_equiv_of_equiv_of_homotopy,
{ symmetry, transitivity _, apply @equiv_of_iso (homset _ _),
rexact yoneda_lemma c (ɏ c'), esimp [yoneda_embedding], exact !equiv_lift⁻¹ᵉ},
{ intro f, apply nat_trans_eq, intro c, apply eq_of_homotopy, intro f',
esimp [equiv.symm,equiv.trans],
esimp [yoneda_lemma,yoneda_embedding,Opposite],
rewrite [id_left,id_right]}
end
definition is_embedding_yoneda_embedding (C : Category) :
is_embedding (ɏ : C → Cᵒᵖ ⇒ cset) :=
begin
intro c c', fapply is_equiv_of_equiv_of_homotopy,
{ exact !eq_equiv_iso ⬝e !iso_equiv_F_iso_F ⬝e !eq_equiv_iso⁻¹ᵉ},
{ intro p, induction p, esimp [equiv.trans, equiv.symm, to_fun_iso], -- to_fun_iso not unfolded
esimp [to_fun_iso],
rewrite -eq_of_iso_refl,
apply ap eq_of_iso, apply iso_eq, esimp,
apply nat_trans_eq, intro c',
apply eq_of_homotopy, intro f,
rewrite [▸*, category.category.id_left], apply id_right}
end
definition is_representable {C : Precategory} (F : Cᵒᵖ ⇒ cset) := Σ(c : C), ɏ c ≅ F
section
set_option apply.class_instance false
definition is_prop_representable {C : Category} (F : Cᵒᵖ ⇒ cset)
: is_prop (is_representable F) :=
begin
fapply is_trunc_equiv_closed,
{ exact proof fiber.sigma_char ɏ F qed ⬝e sigma.sigma_equiv_sigma_right (λc, !eq_equiv_iso)},
{ apply function.is_prop_fiber_of_is_embedding, apply is_embedding_yoneda_embedding}
end
end
end yoneda
|
b085cf9db8a2aaef9239159c783bfbaec12ce76f | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/group/opposite.lean | ee00fdab71b3d408a1bf55ca50993df8c7ec5119 | [
"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 | 21,234 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import algebra.group.inj_surj
import algebra.group.commute
import algebra.hom.equiv.basic
import algebra.opposites
import data.int.cast.defs
/-!
# Group structures on the multiplicative and additive opposites
-/
universes u v
variables (α : Type u)
namespace mul_opposite
/-!
### Additive structures on `αᵐᵒᵖ`
-/
instance [add_semigroup α] : add_semigroup (αᵐᵒᵖ) :=
unop_injective.add_semigroup _ (λ x y, rfl)
instance [add_left_cancel_semigroup α] : add_left_cancel_semigroup αᵐᵒᵖ :=
unop_injective.add_left_cancel_semigroup _ (λ x y, rfl)
instance [add_right_cancel_semigroup α] : add_right_cancel_semigroup αᵐᵒᵖ :=
unop_injective.add_right_cancel_semigroup _ (λ x y, rfl)
instance [add_comm_semigroup α] : add_comm_semigroup αᵐᵒᵖ :=
unop_injective.add_comm_semigroup _ (λ x y, rfl)
instance [add_zero_class α] : add_zero_class αᵐᵒᵖ :=
unop_injective.add_zero_class _ rfl (λ x y, rfl)
instance [add_monoid α] : add_monoid αᵐᵒᵖ :=
unop_injective.add_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl)
instance [add_monoid_with_one α] : add_monoid_with_one αᵐᵒᵖ :=
{ nat_cast := λ n, op n,
nat_cast_zero := show op ((0 : ℕ) : α) = 0, by simp,
nat_cast_succ := show ∀ n, op ((n + 1 : ℕ) : α) = op (n : ℕ) + 1, by simp,
.. mul_opposite.add_monoid α, .. mul_opposite.has_one α }
instance [add_comm_monoid α] : add_comm_monoid αᵐᵒᵖ :=
unop_injective.add_comm_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl)
instance [sub_neg_monoid α] : sub_neg_monoid αᵐᵒᵖ :=
unop_injective.sub_neg_monoid _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
instance [add_group α] : add_group αᵐᵒᵖ :=
unop_injective.add_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
instance [add_group_with_one α] : add_group_with_one αᵐᵒᵖ :=
{ int_cast := λ n, op n,
int_cast_of_nat := λ n, show op ((n : ℤ) : α) = op n, by rw int.cast_coe_nat,
int_cast_neg_succ_of_nat := λ n, show op _ = op (- unop (op ((n + 1 : ℕ) : α))),
by erw [unop_op, int.cast_neg_succ_of_nat]; refl,
.. mul_opposite.add_monoid_with_one α, .. mul_opposite.add_group α }
instance [add_comm_group α] : add_comm_group αᵐᵒᵖ :=
unop_injective.add_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
/-!
### Multiplicative structures on `αᵐᵒᵖ`
We also generate additive structures on `αᵃᵒᵖ` using `to_additive`
-/
@[to_additive] instance [semigroup α] : semigroup αᵐᵒᵖ :=
{ mul_assoc := λ x y z, unop_injective $ eq.symm $ mul_assoc (unop z) (unop y) (unop x),
.. mul_opposite.has_mul α }
@[to_additive] instance [right_cancel_semigroup α] : left_cancel_semigroup αᵐᵒᵖ :=
{ mul_left_cancel := λ x y z H, unop_injective $ mul_right_cancel $ op_injective H,
.. mul_opposite.semigroup α }
@[to_additive] instance [left_cancel_semigroup α] : right_cancel_semigroup αᵐᵒᵖ :=
{ mul_right_cancel := λ x y z H, unop_injective $ mul_left_cancel $ op_injective H,
.. mul_opposite.semigroup α }
@[to_additive] instance [comm_semigroup α] : comm_semigroup αᵐᵒᵖ :=
{ mul_comm := λ x y, unop_injective $ mul_comm (unop y) (unop x),
.. mul_opposite.semigroup α }
@[to_additive] instance [mul_one_class α] : mul_one_class αᵐᵒᵖ :=
{ one_mul := λ x, unop_injective $ mul_one $ unop x,
mul_one := λ x, unop_injective $ one_mul $ unop x,
.. mul_opposite.has_mul α, .. mul_opposite.has_one α }
@[to_additive] instance [monoid α] : monoid αᵐᵒᵖ :=
{ npow := λ n x, op $ x.unop ^ n,
npow_zero' := λ x, unop_injective $ monoid.npow_zero' x.unop,
npow_succ' := λ n x, unop_injective $ pow_succ' x.unop n,
.. mul_opposite.semigroup α, .. mul_opposite.mul_one_class α }
@[to_additive] instance [right_cancel_monoid α] : left_cancel_monoid αᵐᵒᵖ :=
{ .. mul_opposite.left_cancel_semigroup α, .. mul_opposite.monoid α }
@[to_additive] instance [left_cancel_monoid α] : right_cancel_monoid αᵐᵒᵖ :=
{ .. mul_opposite.right_cancel_semigroup α, .. mul_opposite.monoid α }
@[to_additive] instance [cancel_monoid α] : cancel_monoid αᵐᵒᵖ :=
{ .. mul_opposite.right_cancel_monoid α, .. mul_opposite.left_cancel_monoid α }
@[to_additive] instance [comm_monoid α] : comm_monoid αᵐᵒᵖ :=
{ .. mul_opposite.monoid α, .. mul_opposite.comm_semigroup α }
@[to_additive] instance [cancel_comm_monoid α] : cancel_comm_monoid αᵐᵒᵖ :=
{ .. mul_opposite.cancel_monoid α, .. mul_opposite.comm_monoid α }
@[to_additive add_opposite.sub_neg_monoid] instance [div_inv_monoid α] : div_inv_monoid αᵐᵒᵖ :=
{ zpow := λ n x, op $ x.unop ^ n,
zpow_zero' := λ x, unop_injective $ div_inv_monoid.zpow_zero' x.unop,
zpow_succ' := λ n x, unop_injective $
by rw [unop_op, zpow_of_nat, zpow_of_nat, pow_succ', unop_mul, unop_op],
zpow_neg' := λ z x, unop_injective $ div_inv_monoid.zpow_neg' z x.unop,
.. mul_opposite.monoid α, .. mul_opposite.has_inv α }
@[to_additive add_opposite.subtraction_monoid] instance [division_monoid α] :
division_monoid αᵐᵒᵖ :=
{ mul_inv_rev := λ a b, unop_injective $ mul_inv_rev _ _,
inv_eq_of_mul := λ a b h, unop_injective $ inv_eq_of_mul_eq_one_left $ congr_arg unop h,
.. mul_opposite.div_inv_monoid α, .. mul_opposite.has_involutive_inv α }
@[to_additive add_opposite.subtraction_comm_monoid] instance [division_comm_monoid α] :
division_comm_monoid αᵐᵒᵖ :=
{ ..mul_opposite.division_monoid α, ..mul_opposite.comm_semigroup α }
@[to_additive] instance [group α] : group αᵐᵒᵖ :=
{ mul_left_inv := λ x, unop_injective $ mul_inv_self $ unop x,
.. mul_opposite.div_inv_monoid α, }
@[to_additive] instance [comm_group α] : comm_group αᵐᵒᵖ :=
{ .. mul_opposite.group α, .. mul_opposite.comm_monoid α }
variable {α}
@[simp, to_additive] lemma unop_div [div_inv_monoid α] (x y : αᵐᵒᵖ) :
unop (x / y) = (unop y)⁻¹ * unop x :=
rfl
@[simp, to_additive] lemma op_div [div_inv_monoid α] (x y : α) :
op (x / y) = (op y)⁻¹ * op x :=
by simp [div_eq_mul_inv]
@[simp, to_additive] lemma semiconj_by_op [has_mul α] {a x y : α} :
semiconj_by (op a) (op y) (op x) ↔ semiconj_by a x y :=
by simp only [semiconj_by, ← op_mul, op_inj, eq_comm]
@[simp, to_additive] lemma semiconj_by_unop [has_mul α] {a x y : αᵐᵒᵖ} :
semiconj_by (unop a) (unop y) (unop x) ↔ semiconj_by a x y :=
by conv_rhs { rw [← op_unop a, ← op_unop x, ← op_unop y, semiconj_by_op] }
@[to_additive] lemma _root_.semiconj_by.op [has_mul α] {a x y : α} (h : semiconj_by a x y) :
semiconj_by (op a) (op y) (op x) :=
semiconj_by_op.2 h
@[to_additive] lemma _root_.semiconj_by.unop [has_mul α] {a x y : αᵐᵒᵖ} (h : semiconj_by a x y) :
semiconj_by (unop a) (unop y) (unop x) :=
semiconj_by_unop.2 h
@[to_additive] lemma _root_.commute.op [has_mul α] {x y : α} (h : commute x y) :
commute (op x) (op y) := h.op
@[to_additive] lemma commute.unop [has_mul α] {x y : αᵐᵒᵖ} (h : commute x y) :
commute (unop x) (unop y) := h.unop
@[simp, to_additive] lemma commute_op [has_mul α] {x y : α} :
commute (op x) (op y) ↔ commute x y :=
semiconj_by_op
@[simp, to_additive] lemma commute_unop [has_mul α] {x y : αᵐᵒᵖ} :
commute (unop x) (unop y) ↔ commute x y :=
semiconj_by_unop
/-- The function `mul_opposite.op` is an additive equivalence. -/
@[simps { fully_applied := ff, simp_rhs := tt }]
def op_add_equiv [has_add α] : α ≃+ αᵐᵒᵖ :=
{ map_add' := λ a b, rfl, .. op_equiv }
@[simp] lemma op_add_equiv_to_equiv [has_add α] :
(op_add_equiv : α ≃+ αᵐᵒᵖ).to_equiv = op_equiv :=
rfl
end mul_opposite
/-!
### Multiplicative structures on `αᵃᵒᵖ`
-/
namespace add_opposite
instance [semigroup α] : semigroup (αᵃᵒᵖ) :=
unop_injective.semigroup _ (λ x y, rfl)
instance [left_cancel_semigroup α] : left_cancel_semigroup αᵃᵒᵖ :=
unop_injective.left_cancel_semigroup _ (λ x y, rfl)
instance [right_cancel_semigroup α] : right_cancel_semigroup αᵃᵒᵖ :=
unop_injective.right_cancel_semigroup _ (λ x y, rfl)
instance [comm_semigroup α] : comm_semigroup αᵃᵒᵖ :=
unop_injective.comm_semigroup _ (λ x y, rfl)
instance [mul_one_class α] : mul_one_class αᵃᵒᵖ :=
unop_injective.mul_one_class _ rfl (λ x y, rfl)
instance {β} [has_pow α β] : has_pow αᵃᵒᵖ β := { pow := λ a b, op (unop a ^ b) }
@[simp] lemma op_pow {β} [has_pow α β] (a : α) (b : β) : op (a ^ b) = op a ^ b := rfl
@[simp] lemma unop_pow {β} [has_pow α β] (a : αᵃᵒᵖ) (b : β) : unop (a ^ b) = unop a ^ b := rfl
instance [monoid α] : monoid αᵃᵒᵖ :=
unop_injective.monoid _ rfl (λ _ _, rfl) (λ _ _, rfl)
instance [comm_monoid α] : comm_monoid αᵃᵒᵖ :=
unop_injective.comm_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl)
instance [div_inv_monoid α] : div_inv_monoid αᵃᵒᵖ :=
unop_injective.div_inv_monoid _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
instance [group α] : group αᵃᵒᵖ :=
unop_injective.group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
instance [comm_group α] : comm_group αᵃᵒᵖ :=
unop_injective.comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl)
variable {α}
/-- The function `add_opposite.op` is a multiplicative equivalence. -/
@[simps { fully_applied := ff, simp_rhs := tt }]
def op_mul_equiv [has_mul α] : α ≃* αᵃᵒᵖ :=
{ map_mul' := λ a b, rfl, .. op_equiv }
@[simp] lemma op_mul_equiv_to_equiv [has_mul α] :
(op_mul_equiv : α ≃* αᵃᵒᵖ).to_equiv = op_equiv :=
rfl
end add_opposite
open mul_opposite
/-- Inversion on a group is a `mul_equiv` to the opposite group. When `G` is commutative, there is
`mul_equiv.inv`. -/
@[to_additive "Negation on an additive group is an `add_equiv` to the opposite group. When `G`
is commutative, there is `add_equiv.inv`.", simps { fully_applied := ff, simp_rhs := tt }]
def mul_equiv.inv' (G : Type*) [division_monoid G] : G ≃* Gᵐᵒᵖ :=
{ map_mul' := λ x y, unop_injective $ mul_inv_rev x y,
.. (equiv.inv G).trans op_equiv }
/-- A semigroup homomorphism `f : M →ₙ* N` such that `f x` commutes with `f y` for all `x, y`
defines a semigroup homomorphism to `Nᵐᵒᵖ`. -/
@[to_additive "An additive semigroup homomorphism `f : add_hom M N` such that `f x` additively
commutes with `f y` for all `x, y` defines an additive semigroup homomorphism to `Sᵃᵒᵖ`.",
simps {fully_applied := ff}]
def mul_hom.to_opposite {M N : Type*} [has_mul M] [has_mul N] (f : M →ₙ* N)
(hf : ∀ x y, commute (f x) (f y)) : M →ₙ* Nᵐᵒᵖ :=
{ to_fun := mul_opposite.op ∘ f,
map_mul' := λ x y, by simp [(hf x y).eq] }
/-- A semigroup homomorphism `f : M →ₙ* N` such that `f x` commutes with `f y` for all `x, y`
defines a semigroup homomorphism from `Mᵐᵒᵖ`. -/
@[to_additive "An additive semigroup homomorphism `f : add_hom M N` such that `f x` additively
commutes with `f y` for all `x`, `y` defines an additive semigroup homomorphism from `Mᵃᵒᵖ`.",
simps {fully_applied := ff}]
def mul_hom.from_opposite {M N : Type*} [has_mul M] [has_mul N] (f : M →ₙ* N)
(hf : ∀ x y, commute (f x) (f y)) : Mᵐᵒᵖ →ₙ* N :=
{ to_fun := f ∘ mul_opposite.unop,
map_mul' := λ x y, (f.map_mul _ _).trans (hf _ _).eq }
/-- A monoid homomorphism `f : M →* N` such that `f x` commutes with `f y` for all `x, y` defines
a monoid homomorphism to `Nᵐᵒᵖ`. -/
@[to_additive "An additive monoid homomorphism `f : M →+ N` such that `f x` additively commutes
with `f y` for all `x, y` defines an additive monoid homomorphism to `Sᵃᵒᵖ`.",
simps {fully_applied := ff}]
def monoid_hom.to_opposite {M N : Type*} [mul_one_class M] [mul_one_class N] (f : M →* N)
(hf : ∀ x y, commute (f x) (f y)) : M →* Nᵐᵒᵖ :=
{ to_fun := mul_opposite.op ∘ f,
map_one' := congr_arg op f.map_one,
map_mul' := λ x y, by simp [(hf x y).eq] }
/-- A monoid homomorphism `f : M →* N` such that `f x` commutes with `f y` for all `x, y` defines
a monoid homomorphism from `Mᵐᵒᵖ`. -/
@[to_additive "An additive monoid homomorphism `f : M →+ N` such that `f x` additively commutes
with `f y` for all `x`, `y` defines an additive monoid homomorphism from `Mᵃᵒᵖ`.",
simps {fully_applied := ff}]
def monoid_hom.from_opposite {M N : Type*} [mul_one_class M] [mul_one_class N] (f : M →* N)
(hf : ∀ x y, commute (f x) (f y)) : Mᵐᵒᵖ →* N :=
{ to_fun := f ∘ mul_opposite.unop,
map_one' := f.map_one,
map_mul' := λ x y, (f.map_mul _ _).trans (hf _ _).eq }
/-- The units of the opposites are equivalent to the opposites of the units. -/
@[to_additive "The additive units of the additive opposites are equivalent to the additive opposites
of the additive units."]
def units.op_equiv {M} [monoid M] : (Mᵐᵒᵖ)ˣ ≃* (Mˣ)ᵐᵒᵖ :=
{ to_fun := λ u, op ⟨unop u, unop ↑(u⁻¹), op_injective u.4, op_injective u.3⟩,
inv_fun := mul_opposite.rec $ λ u, ⟨op ↑(u), op ↑(u⁻¹), unop_injective $ u.4, unop_injective u.3⟩,
map_mul' := λ x y, unop_injective $ units.ext $ rfl,
left_inv := λ x, units.ext $ by simp,
right_inv := λ x, unop_injective $ units.ext $ rfl }
@[simp, to_additive]
lemma units.coe_unop_op_equiv {M} [monoid M] (u : (Mᵐᵒᵖ)ˣ) :
((units.op_equiv u).unop : M) = unop (u : Mᵐᵒᵖ) :=
rfl
@[simp, to_additive]
lemma units.coe_op_equiv_symm {M} [monoid M] (u : (Mˣ)ᵐᵒᵖ) :
(units.op_equiv.symm u : Mᵐᵒᵖ) = op (u.unop : M) :=
rfl
/-- A semigroup homomorphism `M →ₙ* N` can equivalently be viewed as a semigroup homomorphism
`Mᵐᵒᵖ →ₙ* Nᵐᵒᵖ`. This is the action of the (fully faithful) `ᵐᵒᵖ`-functor on morphisms. -/
@[to_additive "An additive semigroup homomorphism `add_hom M N` can equivalently be viewed as an
additive semigroup homomorphism `add_hom Mᵃᵒᵖ Nᵃᵒᵖ`. This is the action of the (fully faithful)
`ᵃᵒᵖ`-functor on morphisms.", simps]
def mul_hom.op {M N} [has_mul M] [has_mul N] :
(M →ₙ* N) ≃ (Mᵐᵒᵖ →ₙ* Nᵐᵒᵖ) :=
{ to_fun := λ f, { to_fun := op ∘ f ∘ unop,
map_mul' := λ x y, unop_injective (f.map_mul y.unop x.unop) },
inv_fun := λ f, { to_fun := unop ∘ f ∘ op,
map_mul' := λ x y, congr_arg unop (f.map_mul (op y) (op x)) },
left_inv := λ f, by { ext, refl },
right_inv := λ f, by { ext x, simp } }
/-- The 'unopposite' of a semigroup homomorphism `Mᵐᵒᵖ →ₙ* Nᵐᵒᵖ`. Inverse to `mul_hom.op`. -/
@[simp, to_additive "The 'unopposite' of an additive semigroup homomorphism `Mᵃᵒᵖ →ₙ+ Nᵃᵒᵖ`. Inverse
to `add_hom.op`."]
def mul_hom.unop {M N} [has_mul M] [has_mul N] :
(Mᵐᵒᵖ →ₙ* Nᵐᵒᵖ) ≃ (M →ₙ* N) := mul_hom.op.symm
/-- An additive semigroup homomorphism `add_hom M N` can equivalently be viewed as an additive
homomorphism `add_hom Mᵐᵒᵖ Nᵐᵒᵖ`. This is the action of the (fully faithful) `ᵐᵒᵖ`-functor on
morphisms. -/
@[simps]
def add_hom.mul_op {M N} [has_add M] [has_add N] :
(add_hom M N) ≃ (add_hom Mᵐᵒᵖ Nᵐᵒᵖ) :=
{ to_fun := λ f, { to_fun := op ∘ f ∘ unop,
map_add' := λ x y, unop_injective (f.map_add x.unop y.unop) },
inv_fun := λ f, { to_fun := unop ∘ f ∘ op,
map_add' := λ x y, congr_arg unop (f.map_add (op x) (op y)) },
left_inv := λ f, by { ext, refl },
right_inv := λ f, by { ext, simp } }
/-- The 'unopposite' of an additive semigroup hom `αᵐᵒᵖ →+ βᵐᵒᵖ`. Inverse to
`add_hom.mul_op`. -/
@[simp] def add_hom.mul_unop {α β} [has_add α] [has_add β] :
(add_hom αᵐᵒᵖ βᵐᵒᵖ) ≃ (add_hom α β) := add_hom.mul_op.symm
/-- A monoid homomorphism `M →* N` can equivalently be viewed as a monoid homomorphism
`Mᵐᵒᵖ →* Nᵐᵒᵖ`. This is the action of the (fully faithful) `ᵐᵒᵖ`-functor on morphisms. -/
@[to_additive "An additive monoid homomorphism `M →+ N` can equivalently be viewed as an
additive monoid homomorphism `Mᵃᵒᵖ →+ Nᵃᵒᵖ`. This is the action of the (fully faithful)
`ᵃᵒᵖ`-functor on morphisms.", simps]
def monoid_hom.op {M N} [mul_one_class M] [mul_one_class N] :
(M →* N) ≃ (Mᵐᵒᵖ →* Nᵐᵒᵖ) :=
{ to_fun := λ f, { to_fun := op ∘ f ∘ unop,
map_one' := congr_arg op f.map_one,
map_mul' := λ x y, unop_injective (f.map_mul y.unop x.unop) },
inv_fun := λ f, { to_fun := unop ∘ f ∘ op,
map_one' := congr_arg unop f.map_one,
map_mul' := λ x y, congr_arg unop (f.map_mul (op y) (op x)) },
left_inv := λ f, by { ext, refl },
right_inv := λ f, by { ext x, simp } }
/-- The 'unopposite' of a monoid homomorphism `Mᵐᵒᵖ →* Nᵐᵒᵖ`. Inverse to `monoid_hom.op`. -/
@[simp, to_additive "The 'unopposite' of an additive monoid homomorphism `Mᵃᵒᵖ →+ Nᵃᵒᵖ`. Inverse to
`add_monoid_hom.op`."]
def monoid_hom.unop {M N} [mul_one_class M] [mul_one_class N] :
(Mᵐᵒᵖ →* Nᵐᵒᵖ) ≃ (M →* N) := monoid_hom.op.symm
/-- An additive homomorphism `M →+ N` can equivalently be viewed as an additive homomorphism
`Mᵐᵒᵖ →+ Nᵐᵒᵖ`. This is the action of the (fully faithful) `ᵐᵒᵖ`-functor on morphisms. -/
@[simps]
def add_monoid_hom.mul_op {M N} [add_zero_class M] [add_zero_class N] :
(M →+ N) ≃ (Mᵐᵒᵖ →+ Nᵐᵒᵖ) :=
{ to_fun := λ f, { to_fun := op ∘ f ∘ unop,
map_zero' := unop_injective f.map_zero,
map_add' := λ x y, unop_injective (f.map_add x.unop y.unop) },
inv_fun := λ f, { to_fun := unop ∘ f ∘ op,
map_zero' := congr_arg unop f.map_zero,
map_add' := λ x y, congr_arg unop (f.map_add (op x) (op y)) },
left_inv := λ f, by { ext, refl },
right_inv := λ f, by { ext, simp } }
/-- The 'unopposite' of an additive monoid hom `αᵐᵒᵖ →+ βᵐᵒᵖ`. Inverse to
`add_monoid_hom.mul_op`. -/
@[simp] def add_monoid_hom.mul_unop {α β} [add_zero_class α] [add_zero_class β] :
(αᵐᵒᵖ →+ βᵐᵒᵖ) ≃ (α →+ β) := add_monoid_hom.mul_op.symm
/-- A iso `α ≃+ β` can equivalently be viewed as an iso `αᵐᵒᵖ ≃+ βᵐᵒᵖ`. -/
@[simps]
def add_equiv.mul_op {α β} [has_add α] [has_add β] :
(α ≃+ β) ≃ (αᵐᵒᵖ ≃+ βᵐᵒᵖ) :=
{ to_fun := λ f, op_add_equiv.symm.trans (f.trans op_add_equiv),
inv_fun := λ f, op_add_equiv.trans (f.trans op_add_equiv.symm),
left_inv := λ f, by { ext, refl },
right_inv := λ f, by { ext, simp } }
/-- The 'unopposite' of an iso `αᵐᵒᵖ ≃+ βᵐᵒᵖ`. Inverse to `add_equiv.mul_op`. -/
@[simp] def add_equiv.mul_unop {α β} [has_add α] [has_add β] :
(αᵐᵒᵖ ≃+ βᵐᵒᵖ) ≃ (α ≃+ β) := add_equiv.mul_op.symm
/-- A iso `α ≃* β` can equivalently be viewed as an iso `αᵐᵒᵖ ≃* βᵐᵒᵖ`. -/
@[to_additive "A iso `α ≃+ β` can equivalently be viewed as an iso `αᵃᵒᵖ ≃+ βᵃᵒᵖ`.", simps]
def mul_equiv.op {α β} [has_mul α] [has_mul β] :
(α ≃* β) ≃ (αᵐᵒᵖ ≃* βᵐᵒᵖ) :=
{ to_fun := λ f, { to_fun := op ∘ f ∘ unop,
inv_fun := op ∘ f.symm ∘ unop,
left_inv := λ x, unop_injective (f.symm_apply_apply x.unop),
right_inv := λ x, unop_injective (f.apply_symm_apply x.unop),
map_mul' := λ x y, unop_injective (f.map_mul y.unop x.unop) },
inv_fun := λ f, { to_fun := unop ∘ f ∘ op,
inv_fun := unop ∘ f.symm ∘ op,
left_inv := λ x, by simp,
right_inv := λ x, by simp,
map_mul' := λ x y, congr_arg unop (f.map_mul (op y) (op x)) },
left_inv := λ f, by { ext, refl },
right_inv := λ f, by { ext, simp } }
/-- The 'unopposite' of an iso `αᵐᵒᵖ ≃* βᵐᵒᵖ`. Inverse to `mul_equiv.op`. -/
@[simp, to_additive "The 'unopposite' of an iso `αᵃᵒᵖ ≃+ βᵃᵒᵖ`. Inverse to `add_equiv.op`."]
def mul_equiv.unop {α β} [has_mul α] [has_mul β] :
(αᵐᵒᵖ ≃* βᵐᵒᵖ) ≃ (α ≃* β) := mul_equiv.op.symm
section ext
/-- This ext lemma change equalities on `αᵐᵒᵖ →+ β` to equalities on `α →+ β`.
This is useful because there are often ext lemmas for specific `α`s that will apply
to an equality of `α →+ β` such as `finsupp.add_hom_ext'`. -/
@[ext]
lemma add_monoid_hom.mul_op_ext {α β} [add_zero_class α] [add_zero_class β]
(f g : αᵐᵒᵖ →+ β)
(h : f.comp (op_add_equiv : α ≃+ αᵐᵒᵖ).to_add_monoid_hom =
g.comp (op_add_equiv : α ≃+ αᵐᵒᵖ).to_add_monoid_hom) : f = g :=
add_monoid_hom.ext $ mul_opposite.rec $ λ x, (add_monoid_hom.congr_fun h : _) x
end ext
|
e661c1c1d79644759cf6e2597e8df4f93838fde3 | 02005f45e00c7ecf2c8ca5db60251bd1e9c860b5 | /src/data/complex/basic.lean | 2a8fdc8bc400537b92cc242d3927f2682e01e515 | [
"Apache-2.0"
] | permissive | anthony2698/mathlib | 03cd69fe5c280b0916f6df2d07c614c8e1efe890 | 407615e05814e98b24b2ff322b14e8e3eb5e5d67 | refs/heads/master | 1,678,792,774,873 | 1,614,371,563,000 | 1,614,371,563,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 26,670 | lean | /-
Copyright (c) 2017 Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Mario Carneiro
-/
import data.real.sqrt
/-!
# The complex numbers
The complex numbers are modelled as ℝ^2 in the obvious way and it is shown that they form a field
of characteristic zero. The result that the complex numbers are algebraically closed, see
`field_theory.algebraic_closure`.
-/
open_locale big_operators
/-! ### Definition and basic arithmmetic -/
/-- Complex numbers consist of two `real`s: a real part `re` and an imaginary part `im`. -/
structure complex : Type :=
(re : ℝ) (im : ℝ)
notation `ℂ` := complex
namespace complex
noncomputable instance : decidable_eq ℂ := classical.dec_eq _
/-- The equivalence between the complex numbers and `ℝ × ℝ`. -/
def equiv_real_prod : ℂ ≃ (ℝ × ℝ) :=
{ to_fun := λ z, ⟨z.re, z.im⟩,
inv_fun := λ p, ⟨p.1, p.2⟩,
left_inv := λ ⟨x, y⟩, rfl,
right_inv := λ ⟨x, y⟩, rfl }
@[simp] theorem equiv_real_prod_apply (z : ℂ) : equiv_real_prod z = (z.re, z.im) := rfl
theorem equiv_real_prod_symm_re (x y : ℝ) : (equiv_real_prod.symm (x, y)).re = x := rfl
theorem equiv_real_prod_symm_im (x y : ℝ) : (equiv_real_prod.symm (x, y)).im = y := rfl
@[simp] theorem eta : ∀ z : ℂ, complex.mk z.re z.im = z
| ⟨a, b⟩ := rfl
@[ext]
theorem ext : ∀ {z w : ℂ}, z.re = w.re → z.im = w.im → z = w
| ⟨zr, zi⟩ ⟨_, _⟩ rfl rfl := rfl
theorem ext_iff {z w : ℂ} : z = w ↔ z.re = w.re ∧ z.im = w.im :=
⟨λ H, by simp [H], and.rec ext⟩
instance : has_coe ℝ ℂ := ⟨λ r, ⟨r, 0⟩⟩
@[simp, norm_cast] lemma of_real_re (r : ℝ) : (r : ℂ).re = r := rfl
@[simp, norm_cast] lemma of_real_im (r : ℝ) : (r : ℂ).im = 0 := rfl
lemma of_real_def (r : ℝ) : (r : ℂ) = ⟨r, 0⟩ := rfl
@[simp, norm_cast] theorem of_real_inj {z w : ℝ} : (z : ℂ) = w ↔ z = w :=
⟨congr_arg re, congr_arg _⟩
instance : has_zero ℂ := ⟨(0 : ℝ)⟩
instance : inhabited ℂ := ⟨0⟩
@[simp] lemma zero_re : (0 : ℂ).re = 0 := rfl
@[simp] lemma zero_im : (0 : ℂ).im = 0 := rfl
@[simp, norm_cast] lemma of_real_zero : ((0 : ℝ) : ℂ) = 0 := rfl
@[simp] theorem of_real_eq_zero {z : ℝ} : (z : ℂ) = 0 ↔ z = 0 := of_real_inj
theorem of_real_ne_zero {z : ℝ} : (z : ℂ) ≠ 0 ↔ z ≠ 0 := not_congr of_real_eq_zero
instance : has_one ℂ := ⟨(1 : ℝ)⟩
@[simp] lemma one_re : (1 : ℂ).re = 1 := rfl
@[simp] lemma one_im : (1 : ℂ).im = 0 := rfl
@[simp, norm_cast] lemma of_real_one : ((1 : ℝ) : ℂ) = 1 := rfl
instance : has_add ℂ := ⟨λ z w, ⟨z.re + w.re, z.im + w.im⟩⟩
@[simp] lemma add_re (z w : ℂ) : (z + w).re = z.re + w.re := rfl
@[simp] lemma add_im (z w : ℂ) : (z + w).im = z.im + w.im := rfl
@[simp] lemma bit0_re (z : ℂ) : (bit0 z).re = bit0 z.re := rfl
@[simp] lemma bit1_re (z : ℂ) : (bit1 z).re = bit1 z.re := rfl
@[simp] lemma bit0_im (z : ℂ) : (bit0 z).im = bit0 z.im := eq.refl _
@[simp] lemma bit1_im (z : ℂ) : (bit1 z).im = bit0 z.im := add_zero _
@[simp, norm_cast] lemma of_real_add (r s : ℝ) : ((r + s : ℝ) : ℂ) = r + s :=
ext_iff.2 $ by simp
@[simp, norm_cast] lemma of_real_bit0 (r : ℝ) : ((bit0 r : ℝ) : ℂ) = bit0 r :=
ext_iff.2 $ by simp [bit0]
@[simp, norm_cast] lemma of_real_bit1 (r : ℝ) : ((bit1 r : ℝ) : ℂ) = bit1 r :=
ext_iff.2 $ by simp [bit1]
instance : has_neg ℂ := ⟨λ z, ⟨-z.re, -z.im⟩⟩
@[simp] lemma neg_re (z : ℂ) : (-z).re = -z.re := rfl
@[simp] lemma neg_im (z : ℂ) : (-z).im = -z.im := rfl
@[simp, norm_cast] lemma of_real_neg (r : ℝ) : ((-r : ℝ) : ℂ) = -r := ext_iff.2 $ by simp
instance : has_sub ℂ := ⟨λ z w, ⟨z.re - w.re, z.im - w.im⟩⟩
instance : has_mul ℂ := ⟨λ z w, ⟨z.re * w.re - z.im * w.im, z.re * w.im + z.im * w.re⟩⟩
@[simp] lemma mul_re (z w : ℂ) : (z * w).re = z.re * w.re - z.im * w.im := rfl
@[simp] lemma mul_im (z w : ℂ) : (z * w).im = z.re * w.im + z.im * w.re := rfl
@[simp, norm_cast] lemma of_real_mul (r s : ℝ) : ((r * s : ℝ) : ℂ) = r * s := ext_iff.2 $ by simp
lemma smul_re (r : ℝ) (z : ℂ) : (↑r * z).re = r * z.re := by simp
lemma smul_im (r : ℝ) (z : ℂ) : (↑r * z).im = r * z.im := by simp
lemma of_real_smul (r : ℝ) (z : ℂ) : (↑r * z) = ⟨r * z.re, r * z.im⟩ := ext (smul_re _ _) (smul_im _ _)
/-! ### The imaginary unit, `I` -/
/-- The imaginary unit. -/
def I : ℂ := ⟨0, 1⟩
@[simp] lemma I_re : I.re = 0 := rfl
@[simp] lemma I_im : I.im = 1 := rfl
@[simp] lemma I_mul_I : I * I = -1 := ext_iff.2 $ by simp
lemma I_mul (z : ℂ) : I * z = ⟨-z.im, z.re⟩ :=
ext_iff.2 $ by simp
lemma I_ne_zero : (I : ℂ) ≠ 0 := mt (congr_arg im) zero_ne_one.symm
lemma mk_eq_add_mul_I (a b : ℝ) : complex.mk a b = a + b * I :=
ext_iff.2 $ by simp
@[simp] lemma re_add_im (z : ℂ) : (z.re : ℂ) + z.im * I = z :=
ext_iff.2 $ by simp
/-! ### Commutative ring instance and lemmas -/
instance : comm_ring ℂ :=
by refine { zero := 0, add := (+), neg := has_neg.neg, sub := has_sub.sub, one := 1, mul := (*),
sub_eq_add_neg := _, ..};
{ intros, apply ext_iff.2; split; simp; ring }
instance re.is_add_group_hom : is_add_group_hom complex.re :=
{ map_add := complex.add_re }
instance im.is_add_group_hom : is_add_group_hom complex.im :=
{ map_add := complex.add_im }
@[simp] lemma I_pow_bit0 (n : ℕ) : I ^ (bit0 n) = (-1) ^ n :=
by rw [pow_bit0', I_mul_I]
@[simp] lemma I_pow_bit1 (n : ℕ) : I ^ (bit1 n) = (-1) ^ n * I :=
by rw [pow_bit1', I_mul_I]
/-! ### Complex conjugation -/
/-- The complex conjugate. -/
def conj : ℂ →+* ℂ :=
begin
refine_struct { to_fun := λ z : ℂ, (⟨z.re, -z.im⟩ : ℂ), .. };
{ intros, ext; simp [add_comm], },
end
@[simp] lemma conj_re (z : ℂ) : (conj z).re = z.re := rfl
@[simp] lemma conj_im (z : ℂ) : (conj z).im = -z.im := rfl
@[simp] lemma conj_of_real (r : ℝ) : conj r = r := ext_iff.2 $ by simp [conj]
@[simp] lemma conj_I : conj I = -I := ext_iff.2 $ by simp
@[simp] lemma conj_bit0 (z : ℂ) : conj (bit0 z) = bit0 (conj z) := ext_iff.2 $ by simp [bit0]
@[simp] lemma conj_bit1 (z : ℂ) : conj (bit1 z) = bit1 (conj z) := ext_iff.2 $ by simp [bit0]
@[simp] lemma conj_neg_I : conj (-I) = I := ext_iff.2 $ by simp
@[simp] lemma conj_conj (z : ℂ) : conj (conj z) = z :=
ext_iff.2 $ by simp
lemma conj_involutive : function.involutive conj := conj_conj
lemma conj_bijective : function.bijective conj := conj_involutive.bijective
lemma conj_inj {z w : ℂ} : conj z = conj w ↔ z = w :=
conj_bijective.1.eq_iff
@[simp] lemma conj_eq_zero {z : ℂ} : conj z = 0 ↔ z = 0 :=
by simpa using @conj_inj z 0
lemma eq_conj_iff_real {z : ℂ} : conj z = z ↔ ∃ r : ℝ, z = r :=
⟨λ h, ⟨z.re, ext rfl $ eq_zero_of_neg_eq (congr_arg im h)⟩,
λ ⟨h, e⟩, by rw [e, conj_of_real]⟩
lemma eq_conj_iff_re {z : ℂ} : conj z = z ↔ (z.re : ℂ) = z :=
eq_conj_iff_real.trans ⟨by rintro ⟨r, rfl⟩; simp, λ h, ⟨_, h.symm⟩⟩
instance : star_ring ℂ :=
{ star := λ z, conj z,
star_involutive := λ z, by simp,
star_mul := λ r s, by { ext; simp [mul_comm], },
star_add := by simp, }
/-! ### Norm squared -/
/-- The norm squared function. -/
@[pp_nodot] def norm_sq : monoid_with_zero_hom ℂ ℝ :=
{ to_fun := λ z, z.re * z.re + z.im * z.im,
map_zero' := by simp,
map_one' := by simp,
map_mul' := λ z w, by { dsimp, ring } }
lemma norm_sq_apply (z : ℂ) : norm_sq z = z.re * z.re + z.im * z.im := rfl
@[simp] lemma norm_sq_of_real (r : ℝ) : norm_sq r = r * r :=
by simp [norm_sq]
lemma norm_sq_eq_conj_mul_self {z : ℂ} : (norm_sq z : ℂ) = conj z * z :=
by { ext; simp [norm_sq, mul_comm], }
@[simp] lemma norm_sq_zero : norm_sq 0 = 0 := norm_sq.map_zero
@[simp] lemma norm_sq_one : norm_sq 1 = 1 := norm_sq.map_one
@[simp] lemma norm_sq_I : norm_sq I = 1 := by simp [norm_sq]
lemma norm_sq_nonneg (z : ℂ) : 0 ≤ norm_sq z :=
add_nonneg (mul_self_nonneg _) (mul_self_nonneg _)
lemma norm_sq_eq_zero {z : ℂ} : norm_sq z = 0 ↔ z = 0 :=
⟨λ h, ext
(eq_zero_of_mul_self_add_mul_self_eq_zero h)
(eq_zero_of_mul_self_add_mul_self_eq_zero $ (add_comm _ _).trans h),
λ h, h.symm ▸ norm_sq_zero⟩
@[simp] lemma norm_sq_pos {z : ℂ} : 0 < norm_sq z ↔ z ≠ 0 :=
(norm_sq_nonneg z).lt_iff_ne.trans $ not_congr (eq_comm.trans norm_sq_eq_zero)
@[simp] lemma norm_sq_neg (z : ℂ) : norm_sq (-z) = norm_sq z :=
by simp [norm_sq]
@[simp] lemma norm_sq_conj (z : ℂ) : norm_sq (conj z) = norm_sq z :=
by simp [norm_sq]
lemma norm_sq_mul (z w : ℂ) : norm_sq (z * w) = norm_sq z * norm_sq w :=
norm_sq.map_mul z w
lemma norm_sq_add (z w : ℂ) : norm_sq (z + w) =
norm_sq z + norm_sq w + 2 * (z * conj w).re :=
by dsimp [norm_sq]; ring
lemma re_sq_le_norm_sq (z : ℂ) : z.re * z.re ≤ norm_sq z :=
le_add_of_nonneg_right (mul_self_nonneg _)
lemma im_sq_le_norm_sq (z : ℂ) : z.im * z.im ≤ norm_sq z :=
le_add_of_nonneg_left (mul_self_nonneg _)
theorem mul_conj (z : ℂ) : z * conj z = norm_sq z :=
ext_iff.2 $ by simp [norm_sq, mul_comm, sub_eq_neg_add, add_comm]
theorem add_conj (z : ℂ) : z + conj z = (2 * z.re : ℝ) :=
ext_iff.2 $ by simp [two_mul]
/-- The coercion `ℝ → ℂ` as a `ring_hom`. -/
def of_real : ℝ →+* ℂ := ⟨coe, of_real_one, of_real_mul, of_real_zero, of_real_add⟩
@[simp] lemma of_real_eq_coe (r : ℝ) : of_real r = r := rfl
@[simp] lemma I_sq : I ^ 2 = -1 := by rw [pow_two, I_mul_I]
@[simp] lemma sub_re (z w : ℂ) : (z - w).re = z.re - w.re := rfl
@[simp] lemma sub_im (z w : ℂ) : (z - w).im = z.im - w.im := rfl
@[simp, norm_cast] lemma of_real_sub (r s : ℝ) : ((r - s : ℝ) : ℂ) = r - s := ext_iff.2 $ by simp
@[simp, norm_cast] lemma of_real_pow (r : ℝ) (n : ℕ) : ((r ^ n : ℝ) : ℂ) = r ^ n :=
by induction n; simp [*, of_real_mul, pow_succ]
theorem sub_conj (z : ℂ) : z - conj z = (2 * z.im : ℝ) * I :=
ext_iff.2 $ by simp [two_mul, sub_eq_add_neg]
lemma norm_sq_sub (z w : ℂ) : norm_sq (z - w) =
norm_sq z + norm_sq w - 2 * (z * conj w).re :=
by rw [sub_eq_add_neg, norm_sq_add]; simp [-mul_re, add_comm, add_left_comm, sub_eq_add_neg]
/-! ### Inversion -/
noncomputable instance : has_inv ℂ := ⟨λ z, conj z * ((norm_sq z)⁻¹:ℝ)⟩
theorem inv_def (z : ℂ) : z⁻¹ = conj z * ((norm_sq z)⁻¹:ℝ) := rfl
@[simp] lemma inv_re (z : ℂ) : (z⁻¹).re = z.re / norm_sq z := by simp [inv_def, division_def]
@[simp] lemma inv_im (z : ℂ) : (z⁻¹).im = -z.im / norm_sq z := by simp [inv_def, division_def]
@[simp, norm_cast] lemma of_real_inv (r : ℝ) : ((r⁻¹ : ℝ) : ℂ) = r⁻¹ :=
ext_iff.2 $ by simp
protected lemma inv_zero : (0⁻¹ : ℂ) = 0 :=
by rw [← of_real_zero, ← of_real_inv, inv_zero]
protected theorem mul_inv_cancel {z : ℂ} (h : z ≠ 0) : z * z⁻¹ = 1 :=
by rw [inv_def, ← mul_assoc, mul_conj, ← of_real_mul,
mul_inv_cancel (mt norm_sq_eq_zero.1 h), of_real_one]
/-! ### Field instance and lemmas -/
noncomputable instance : field ℂ :=
{ inv := has_inv.inv,
exists_pair_ne := ⟨0, 1, mt (congr_arg re) zero_ne_one⟩,
mul_inv_cancel := @complex.mul_inv_cancel,
inv_zero := complex.inv_zero,
..complex.comm_ring }
@[simp] lemma I_fpow_bit0 (n : ℤ) : I ^ (bit0 n) = (-1) ^ n :=
by rw [fpow_bit0', I_mul_I]
@[simp] lemma I_fpow_bit1 (n : ℤ) : I ^ (bit1 n) = (-1) ^ n * I :=
by rw [fpow_bit1', I_mul_I]
lemma div_re (z w : ℂ) : (z / w).re = z.re * w.re / norm_sq w + z.im * w.im / norm_sq w :=
by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg]
lemma div_im (z w : ℂ) : (z / w).im = z.im * w.re / norm_sq w - z.re * w.im / norm_sq w :=
by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg, add_comm]
@[simp, norm_cast] lemma of_real_div (r s : ℝ) : ((r / s : ℝ) : ℂ) = r / s :=
of_real.map_div r s
@[simp, norm_cast] lemma of_real_fpow (r : ℝ) (n : ℤ) : ((r ^ n : ℝ) : ℂ) = (r : ℂ) ^ n :=
of_real.map_fpow r n
@[simp] lemma div_I (z : ℂ) : z / I = -(z * I) :=
(div_eq_iff_mul_eq I_ne_zero).2 $ by simp [mul_assoc]
@[simp] lemma inv_I : I⁻¹ = -I :=
by simp [inv_eq_one_div]
@[simp] lemma norm_sq_inv (z : ℂ) : norm_sq z⁻¹ = (norm_sq z)⁻¹ :=
norm_sq.map_inv' z
@[simp] lemma norm_sq_div (z w : ℂ) : norm_sq (z / w) = norm_sq z / norm_sq w :=
norm_sq.map_div z w
/-! ### Cast lemmas -/
@[simp, norm_cast] theorem of_real_nat_cast (n : ℕ) : ((n : ℝ) : ℂ) = n :=
of_real.map_nat_cast n
@[simp, norm_cast] lemma nat_cast_re (n : ℕ) : (n : ℂ).re = n :=
by rw [← of_real_nat_cast, of_real_re]
@[simp, norm_cast] lemma nat_cast_im (n : ℕ) : (n : ℂ).im = 0 :=
by rw [← of_real_nat_cast, of_real_im]
@[simp, norm_cast] theorem of_real_int_cast (n : ℤ) : ((n : ℝ) : ℂ) = n :=
of_real.map_int_cast n
@[simp, norm_cast] lemma int_cast_re (n : ℤ) : (n : ℂ).re = n :=
by rw [← of_real_int_cast, of_real_re]
@[simp, norm_cast] lemma int_cast_im (n : ℤ) : (n : ℂ).im = 0 :=
by rw [← of_real_int_cast, of_real_im]
@[simp, norm_cast] theorem of_real_rat_cast (n : ℚ) : ((n : ℝ) : ℂ) = n :=
of_real.map_rat_cast n
@[simp, norm_cast] lemma rat_cast_re (q : ℚ) : (q : ℂ).re = q :=
by rw [← of_real_rat_cast, of_real_re]
@[simp, norm_cast] lemma rat_cast_im (q : ℚ) : (q : ℂ).im = 0 :=
by rw [← of_real_rat_cast, of_real_im]
/-! ### Characteristic zero -/
instance char_zero_complex : char_zero ℂ :=
char_zero_of_inj_zero $ λ n h,
by rwa [← of_real_nat_cast, of_real_eq_zero, nat.cast_eq_zero] at h
/-- A complex number `z` plus its conjugate `conj z` is `2` times its real part. -/
theorem re_eq_add_conj (z : ℂ) : (z.re : ℂ) = (z + conj z) / 2 :=
by simp only [add_conj, of_real_mul, of_real_one, of_real_bit0,
mul_div_cancel_left (z.re:ℂ) two_ne_zero']
/-- A complex number `z` minus its conjugate `conj z` is `2i` times its imaginary part. -/
theorem im_eq_sub_conj (z : ℂ) : (z.im : ℂ) = (z - conj(z))/(2 * I) :=
by simp only [sub_conj, of_real_mul, of_real_one, of_real_bit0, mul_right_comm,
mul_div_cancel_left _ (mul_ne_zero two_ne_zero' I_ne_zero : 2 * I ≠ 0)]
/-! ### Absolute value -/
/-- The complex absolute value function, defined as the square root of the norm squared. -/
@[pp_nodot] noncomputable def abs (z : ℂ) : ℝ := (norm_sq z).sqrt
local notation `abs'` := _root_.abs
@[simp, norm_cast] lemma abs_of_real (r : ℝ) : abs r = abs' r :=
by simp [abs, norm_sq_of_real, real.sqrt_mul_self_eq_abs]
lemma abs_of_nonneg {r : ℝ} (h : 0 ≤ r) : abs r = r :=
(abs_of_real _).trans (abs_of_nonneg h)
lemma abs_of_nat (n : ℕ) : complex.abs n = n :=
calc complex.abs n = complex.abs (n:ℝ) : by rw [of_real_nat_cast]
... = _ : abs_of_nonneg (nat.cast_nonneg n)
lemma mul_self_abs (z : ℂ) : abs z * abs z = norm_sq z :=
real.mul_self_sqrt (norm_sq_nonneg _)
@[simp] lemma abs_zero : abs 0 = 0 := by simp [abs]
@[simp] lemma abs_one : abs 1 = 1 := by simp [abs]
@[simp] lemma abs_I : abs I = 1 := by simp [abs]
@[simp] lemma abs_two : abs 2 = 2 :=
calc abs 2 = abs (2 : ℝ) : by rw [of_real_bit0, of_real_one]
... = (2 : ℝ) : abs_of_nonneg (by norm_num)
lemma abs_nonneg (z : ℂ) : 0 ≤ abs z :=
real.sqrt_nonneg _
@[simp] lemma abs_eq_zero {z : ℂ} : abs z = 0 ↔ z = 0 :=
(real.sqrt_eq_zero $ norm_sq_nonneg _).trans norm_sq_eq_zero
lemma abs_ne_zero {z : ℂ} : abs z ≠ 0 ↔ z ≠ 0 :=
not_congr abs_eq_zero
@[simp] lemma abs_conj (z : ℂ) : abs (conj z) = abs z :=
by simp [abs]
@[simp] lemma abs_mul (z w : ℂ) : abs (z * w) = abs z * abs w :=
by rw [abs, norm_sq_mul, real.sqrt_mul (norm_sq_nonneg _)]; refl
lemma abs_re_le_abs (z : ℂ) : abs' z.re ≤ abs z :=
by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg z.re) (abs_nonneg _),
abs_mul_abs_self, mul_self_abs];
apply re_sq_le_norm_sq
lemma abs_im_le_abs (z : ℂ) : abs' z.im ≤ abs z :=
by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg z.im) (abs_nonneg _),
abs_mul_abs_self, mul_self_abs];
apply im_sq_le_norm_sq
lemma re_le_abs (z : ℂ) : z.re ≤ abs z :=
(abs_le.1 (abs_re_le_abs _)).2
lemma im_le_abs (z : ℂ) : z.im ≤ abs z :=
(abs_le.1 (abs_im_le_abs _)).2
lemma abs_add (z w : ℂ) : abs (z + w) ≤ abs z + abs w :=
(mul_self_le_mul_self_iff (abs_nonneg _)
(add_nonneg (abs_nonneg _) (abs_nonneg _))).2 $
begin
rw [mul_self_abs, add_mul_self_eq, mul_self_abs, mul_self_abs,
add_right_comm, norm_sq_add, add_le_add_iff_left,
mul_assoc, mul_le_mul_left (@zero_lt_two ℝ _ _)],
simpa [-mul_re] using re_le_abs (z * conj w)
end
instance : is_absolute_value abs :=
{ abv_nonneg := abs_nonneg,
abv_eq_zero := λ _, abs_eq_zero,
abv_add := abs_add,
abv_mul := abs_mul }
open is_absolute_value
@[simp] lemma abs_abs (z : ℂ) : abs' (abs z) = abs z :=
_root_.abs_of_nonneg (abs_nonneg _)
@[simp] lemma abs_pos {z : ℂ} : 0 < abs z ↔ z ≠ 0 := abv_pos abs
@[simp] lemma abs_neg : ∀ z, abs (-z) = abs z := abv_neg abs
lemma abs_sub : ∀ z w, abs (z - w) = abs (w - z) := abv_sub abs
lemma abs_sub_le : ∀ a b c, abs (a - c) ≤ abs (a - b) + abs (b - c) := abv_sub_le abs
@[simp] theorem abs_inv : ∀ z, abs z⁻¹ = (abs z)⁻¹ := abv_inv abs
@[simp] theorem abs_div : ∀ z w, abs (z / w) = abs z / abs w := abv_div abs
lemma abs_abs_sub_le_abs_sub : ∀ z w, abs' (abs z - abs w) ≤ abs (z - w) :=
abs_abv_sub_le_abv_sub abs
lemma abs_le_abs_re_add_abs_im (z : ℂ) : abs z ≤ abs' z.re + abs' z.im :=
by simpa [re_add_im] using abs_add z.re (z.im * I)
lemma abs_re_div_abs_le_one (z : ℂ) : abs' (z.re / z.abs) ≤ 1 :=
if hz : z = 0 then by simp [hz, zero_le_one]
else by { simp_rw [_root_.abs_div, abs_abs, div_le_iff (abs_pos.2 hz), one_mul, abs_re_le_abs] }
lemma abs_im_div_abs_le_one (z : ℂ) : abs' (z.im / z.abs) ≤ 1 :=
if hz : z = 0 then by simp [hz, zero_le_one]
else by { simp_rw [_root_.abs_div, abs_abs, div_le_iff (abs_pos.2 hz), one_mul, abs_im_le_abs] }
@[simp, norm_cast] lemma abs_cast_nat (n : ℕ) : abs (n : ℂ) = n :=
by rw [← of_real_nat_cast, abs_of_nonneg (nat.cast_nonneg n)]
@[simp, norm_cast] lemma int_cast_abs (n : ℤ) : ↑(abs' n) = abs n :=
by rw [← of_real_int_cast, abs_of_real, int.cast_abs]
lemma norm_sq_eq_abs (x : ℂ) : norm_sq x = abs x ^ 2 :=
by rw [abs, pow_two, real.mul_self_sqrt (norm_sq_nonneg _)]
/--
We put a partial order on ℂ so that `z ≤ w` exactly if `w - z` is real and nonnegative.
Complex numbers with different imaginary parts are incomparable.
-/
def complex_order : partial_order ℂ :=
{ le := λ z w, ∃ x : ℝ, 0 ≤ x ∧ w = z + x,
le_refl := λ x, ⟨0, by simp⟩,
le_trans := λ x y z h₁ h₂,
begin
obtain ⟨w₁, l₁, rfl⟩ := h₁,
obtain ⟨w₂, l₂, rfl⟩ := h₂,
refine ⟨w₁ + w₂, _, _⟩,
{ linarith, },
{ simp [add_assoc], },
end,
le_antisymm := λ z w h₁ h₂,
begin
obtain ⟨w₁, l₁, rfl⟩ := h₁,
obtain ⟨w₂, l₂, e⟩ := h₂,
have h₃ : w₁ + w₂ = 0,
{ symmetry,
rw add_assoc at e,
apply of_real_inj.mp,
apply add_left_cancel,
convert e; simp, },
have h₄ : w₁ = 0, linarith,
simp [h₄],
end, }
localized "attribute [instance] complex_order" in complex_order
section complex_order
open_locale complex_order
lemma le_def {z w : ℂ} : z ≤ w ↔ ∃ x : ℝ, 0 ≤ x ∧ w = z + x := iff.refl _
lemma lt_def {z w : ℂ} : z < w ↔ ∃ x : ℝ, 0 < x ∧ w = z + x :=
begin
rw [lt_iff_le_not_le],
fsplit,
{ rintro ⟨⟨x, l, rfl⟩, h⟩,
by_cases hx : x = 0,
{ simp [hx] at h, exfalso, exact h (le_refl _), },
{ replace l : 0 < x := l.lt_of_ne (ne.symm hx),
exact ⟨x, l, rfl⟩, } },
{ rintro ⟨x, l, rfl⟩,
fsplit,
{ exact ⟨x, l.le, rfl⟩, },
{ rintro ⟨x', l', e⟩,
rw [add_assoc] at e,
replace e := add_left_cancel (by { convert e, simp }),
norm_cast at e,
linarith, } }
end
@[simp, norm_cast] lemma real_le_real {x y : ℝ} : (x : ℂ) ≤ (y : ℂ) ↔ x ≤ y :=
begin
rw [le_def],
fsplit,
{ rintro ⟨r, l, e⟩,
norm_cast at e,
subst e,
exact le_add_of_nonneg_right l, },
{ intro h,
exact ⟨y - x, sub_nonneg.mpr h, (by simp)⟩, },
end
@[simp, norm_cast] lemma real_lt_real {x y : ℝ} : (x : ℂ) < (y : ℂ) ↔ x < y :=
begin
rw [lt_def],
fsplit,
{ rintro ⟨r, l, e⟩,
norm_cast at e,
subst e,
exact lt_add_of_pos_right x l, },
{ intro h,
exact ⟨y - x, sub_pos.mpr h, (by simp)⟩, },
end
@[simp, norm_cast] lemma zero_le_real {x : ℝ} : (0 : ℂ) ≤ (x : ℂ) ↔ 0 ≤ x := real_le_real
@[simp, norm_cast] lemma zero_lt_real {x : ℝ} : (0 : ℂ) < (x : ℂ) ↔ 0 < x := real_lt_real
/--
With `z ≤ w` iff `w - z` is real and nonnegative, `ℂ` is an ordered ring.
-/
def complex_ordered_comm_ring : ordered_comm_ring ℂ :=
{ zero_le_one := ⟨1, zero_le_one, by simp⟩,
add_le_add_left := λ w z h y,
begin
obtain ⟨x, l, rfl⟩ := h,
exact ⟨x, l, by simp [add_assoc]⟩,
end,
mul_pos := λ z w hz hw,
begin
obtain ⟨zx, lz, rfl⟩ := lt_def.mp hz,
obtain ⟨wx, lw, rfl⟩ := lt_def.mp hw,
norm_cast,
simp only [mul_pos, lz, lw, zero_add],
end,
le_of_add_le_add_left := λ u v z h,
begin
obtain ⟨x, l, e⟩ := h,
rw add_assoc at e,
exact ⟨x, l, add_left_cancel e⟩,
end,
mul_lt_mul_of_pos_left := λ u v z h₁ h₂,
begin
obtain ⟨x₁, l₁, rfl⟩ := lt_def.mp h₁,
obtain ⟨x₂, l₂, rfl⟩ := lt_def.mp h₂,
simp only [mul_add, zero_add],
exact lt_def.mpr ⟨x₂ * x₁, mul_pos l₂ l₁, (by norm_cast)⟩,
end,
mul_lt_mul_of_pos_right := λ u v z h₁ h₂,
begin
obtain ⟨x₁, l₁, rfl⟩ := lt_def.mp h₁,
obtain ⟨x₂, l₂, rfl⟩ := lt_def.mp h₂,
simp only [add_mul, zero_add],
exact lt_def.mpr ⟨x₁ * x₂, mul_pos l₁ l₂, (by norm_cast)⟩,
end,
-- we need more instances here because comm_ring doesn't have zero_add et al as fields,
-- they are derived as lemmas
..(by apply_instance : partial_order ℂ),
..(by apply_instance : comm_ring ℂ),
..(by apply_instance : comm_semiring ℂ),
..(by apply_instance : add_cancel_monoid ℂ) }
localized "attribute [instance] complex_ordered_comm_ring" in complex_order
/--
With `z ≤ w` iff `w - z` is real and nonnegative, `ℂ` is a star ordered ring.
(That is, an ordered ring in which every element of the form `star z * z` is nonnegative.)
In fact, the nonnegative elements are precisely those of this form.
This hold in any C*-algebra, e.g. `ℂ`,
but we don't yet have C*-algebras in mathlib.
-/
def complex_star_ordered_ring : star_ordered_ring ℂ :=
{ star_mul_self_nonneg := λ z,
begin
refine ⟨z.abs^2, pow_nonneg (abs_nonneg z) 2, _⟩,
simp only [has_star.star, of_real_pow, zero_add],
norm_cast,
rw [←norm_sq_eq_abs, norm_sq_eq_conj_mul_self],
end, }
localized "attribute [instance] complex_star_ordered_ring" in complex_order
end complex_order
/-! ### Cauchy sequences -/
theorem is_cau_seq_re (f : cau_seq ℂ abs) : is_cau_seq abs' (λ n, (f n).re) :=
λ ε ε0, (f.cauchy ε0).imp $ λ i H j ij,
lt_of_le_of_lt (by simpa using abs_re_le_abs (f j - f i)) (H _ ij)
theorem is_cau_seq_im (f : cau_seq ℂ abs) : is_cau_seq abs' (λ n, (f n).im) :=
λ ε ε0, (f.cauchy ε0).imp $ λ i H j ij,
lt_of_le_of_lt (by simpa using abs_im_le_abs (f j - f i)) (H _ ij)
/-- The real part of a complex Cauchy sequence, as a real Cauchy sequence. -/
noncomputable def cau_seq_re (f : cau_seq ℂ abs) : cau_seq ℝ abs' :=
⟨_, is_cau_seq_re f⟩
/-- The imaginary part of a complex Cauchy sequence, as a real Cauchy sequence. -/
noncomputable def cau_seq_im (f : cau_seq ℂ abs) : cau_seq ℝ abs' :=
⟨_, is_cau_seq_im f⟩
lemma is_cau_seq_abs {f : ℕ → ℂ} (hf : is_cau_seq abs f) :
is_cau_seq abs' (abs ∘ f) :=
λ ε ε0, let ⟨i, hi⟩ := hf ε ε0 in
⟨i, λ j hj, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _) (hi j hj)⟩
/-- The limit of a Cauchy sequence of complex numbers. -/
noncomputable def lim_aux (f : cau_seq ℂ abs) : ℂ :=
⟨cau_seq.lim (cau_seq_re f), cau_seq.lim (cau_seq_im f)⟩
theorem equiv_lim_aux (f : cau_seq ℂ abs) : f ≈ cau_seq.const abs (lim_aux f) :=
λ ε ε0, (exists_forall_ge_and
(cau_seq.equiv_lim ⟨_, is_cau_seq_re f⟩ _ (half_pos ε0))
(cau_seq.equiv_lim ⟨_, is_cau_seq_im f⟩ _ (half_pos ε0))).imp $
λ i H j ij, begin
cases H _ ij with H₁ H₂,
apply lt_of_le_of_lt (abs_le_abs_re_add_abs_im _),
dsimp [lim_aux] at *,
have := add_lt_add H₁ H₂,
rwa add_halves at this,
end
noncomputable instance : cau_seq.is_complete ℂ abs :=
⟨λ f, ⟨lim_aux f, equiv_lim_aux f⟩⟩
open cau_seq
lemma lim_eq_lim_im_add_lim_re (f : cau_seq ℂ abs) : lim f =
↑(lim (cau_seq_re f)) + ↑(lim (cau_seq_im f)) * I :=
lim_eq_of_equiv_const $
calc f ≈ _ : equiv_lim_aux f
... = cau_seq.const abs (↑(lim (cau_seq_re f)) + ↑(lim (cau_seq_im f)) * I) :
cau_seq.ext (λ _, complex.ext (by simp [lim_aux, cau_seq_re]) (by simp [lim_aux, cau_seq_im]))
lemma lim_re (f : cau_seq ℂ abs) : lim (cau_seq_re f) = (lim f).re :=
by rw [lim_eq_lim_im_add_lim_re]; simp
lemma lim_im (f : cau_seq ℂ abs) : lim (cau_seq_im f) = (lim f).im :=
by rw [lim_eq_lim_im_add_lim_re]; simp
lemma is_cau_seq_conj (f : cau_seq ℂ abs) : is_cau_seq abs (λ n, conj (f n)) :=
λ ε ε0, let ⟨i, hi⟩ := f.2 ε ε0 in
⟨i, λ j hj, by rw [← conj.map_sub, abs_conj]; exact hi j hj⟩
/-- The complex conjugate of a complex Cauchy sequence, as a complex Cauchy sequence. -/
noncomputable def cau_seq_conj (f : cau_seq ℂ abs) : cau_seq ℂ abs :=
⟨_, is_cau_seq_conj f⟩
lemma lim_conj (f : cau_seq ℂ abs) : lim (cau_seq_conj f) = conj (lim f) :=
complex.ext (by simp [cau_seq_conj, (lim_re _).symm, cau_seq_re])
(by simp [cau_seq_conj, (lim_im _).symm, cau_seq_im, (lim_neg _).symm]; refl)
/-- The absolute value of a complex Cauchy sequence, as a real Cauchy sequence. -/
noncomputable def cau_seq_abs (f : cau_seq ℂ abs) : cau_seq ℝ abs' :=
⟨_, is_cau_seq_abs f.2⟩
lemma lim_abs (f : cau_seq ℂ abs) : lim (cau_seq_abs f) = abs (lim f) :=
lim_eq_of_equiv_const (λ ε ε0,
let ⟨i, hi⟩ := equiv_lim f ε ε0 in
⟨i, λ j hj, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _) (hi j hj)⟩)
@[simp, norm_cast] lemma of_real_prod {α : Type*} (s : finset α) (f : α → ℝ) :
((∏ i in s, f i : ℝ) : ℂ) = ∏ i in s, (f i : ℂ) :=
ring_hom.map_prod of_real _ _
@[simp, norm_cast] lemma of_real_sum {α : Type*} (s : finset α) (f : α → ℝ) :
((∑ i in s, f i : ℝ) : ℂ) = ∑ i in s, (f i : ℂ) :=
ring_hom.map_sum of_real _ _
end complex
|
95bad17f82185c2c2619233b2c133c2cfaee2678 | 83c8119e3298c0bfc53fc195c41a6afb63d01513 | /library/init/category/lift.lean | 68edc49b993220030c083a478ae91b5b8be61298 | [
"Apache-2.0"
] | permissive | anfelor/lean | 584b91c4e87a6d95f7630c2a93fb082a87319ed0 | 31cfc2b6bf7d674f3d0f73848b842c9c9869c9f1 | refs/heads/master | 1,610,067,141,310 | 1,585,992,232,000 | 1,585,992,232,000 | 251,683,543 | 0 | 0 | Apache-2.0 | 1,585,676,570,000 | 1,585,676,569,000 | null | UTF-8 | Lean | false | false | 4,497 | lean | /-
Copyright (c) 2016 Gabriel Ebner. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gabriel Ebner, Sebastian Ullrich
Classy functions for lifting monadic actions of different shapes.
This theory is roughly modeled after the Haskell 'layers' package https://hackage.haskell.org/package/layers-0.1.
Please see https://hackage.haskell.org/package/layers-0.1/docs/Documentation-Layers-Overview.html for an exhaustive discussion of the different approaches to lift functions.
-/
prelude
import init.function init.coe
import init.category.monad
universes u v w
/-- A function for lifting a computation from an inner monad to an outer monad.
Like [MonadTrans](https://hackage.haskell.org/package/transformers-0.5.5.0/docs/Control-Monad-Trans-Class.html),
but `n` does not have to be a monad transformer.
Alternatively, an implementation of [MonadLayer](https://hackage.haskell.org/package/layers-0.1/docs/Control-Monad-Layer.html#t:MonadLayer) without `layerInvmap` (so far). -/
class has_monad_lift (m : Type u → Type v) (n : Type u → Type w) :=
(monad_lift {} : ∀ {α}, m α → n α)
/-- The reflexive-transitive closure of `has_monad_lift`.
`monad_lift` is used to transitively lift monadic computations such as `state_t.get` or `state_t.put s`.
Corresponds to [MonadLift](https://hackage.haskell.org/package/layers-0.1/docs/Control-Monad-Layer.html#t:MonadLift). -/
class has_monad_lift_t (m : Type u → Type v) (n : Type u → Type w) :=
(monad_lift {} : ∀ {α}, m α → n α)
export has_monad_lift_t (monad_lift)
/-- A coercion that may reduce the need for explicit lifting.
Because of [limitations of the current coercion resolution](https://github.com/leanprover/lean/issues/1402), this definition is not marked as a global instance and should be marked locally instead. -/
@[reducible] def has_monad_lift_to_has_coe {m n} [has_monad_lift_t m n] {α} : has_coe (m α) (n α) :=
⟨monad_lift⟩
instance has_monad_lift_t_trans (m n o) [has_monad_lift_t m n] [has_monad_lift n o] : has_monad_lift_t m o :=
⟨λ α ma, has_monad_lift.monad_lift (monad_lift ma : n α)⟩
instance has_monad_lift_t_refl (m) : has_monad_lift_t m m :=
⟨λ α, id⟩
@[simp] lemma monad_lift_refl {m : Type u → Type v} {α} : (monad_lift : m α → m α) = id := rfl
/-- A functor in the category of monads. Can be used to lift monad-transforming functions.
Based on pipes' [MFunctor](https://hackage.haskell.org/package/pipes-2.4.0/docs/Control-MFunctor.html),
but not restricted to monad transformers.
Alternatively, an implementation of [MonadTransFunctor](http://duairc.netsoc.ie/layers-docs/Control-Monad-Layer.html#t:MonadTransFunctor). -/
class monad_functor (m m' : Type u → Type v) (n n' : Type u → Type w) :=
(monad_map {} {α : Type u} : (∀ {α}, m α → m' α) → n α → n' α)
/-- The reflexive-transitive closure of `monad_functor`.
`monad_map` is used to transitively lift monad morphisms such as `state_t.zoom`.
A generalization of [MonadLiftFunctor](http://duairc.netsoc.ie/layers-docs/Control-Monad-Layer.html#t:MonadLiftFunctor), which can only lift endomorphisms (i.e. m = m', n = n'). -/
class monad_functor_t (m m' : Type u → Type v) (n n' : Type u → Type w) :=
(monad_map {} {α : Type u} : (∀ {α}, m α → m' α) → n α → n' α)
export monad_functor_t (monad_map)
instance monad_functor_t_trans (m m' n n' o o') [monad_functor_t m m' n n'] [monad_functor n n' o o'] :
monad_functor_t m m' o o' :=
⟨λ α f, monad_functor.monad_map (λ α, (monad_map @f : n α → n' α))⟩
instance monad_functor_t_refl (m m') : monad_functor_t m m' m m' :=
⟨λ α f, f⟩
@[simp] lemma monad_map_refl {m m' : Type u → Type v} (f : ∀ {α}, m α → m' α) {α} : (monad_map @f : m α → m' α) = f := rfl
/-- Run a monad stack to completion.
`run` should be the composition of the transformers' individual `run` functions.
This class mostly saves some typing when using highly nested monad stacks:
```
@[reducible] def my_monad := reader_t my_cfg $ state_t my_state $ except_t my_err id
-- def my_monad.run {α : Type} (x : my_monad α) (cfg : my_cfg) (st : my_state) := ((x.run cfg).run st).run
def my_monad.run {α : Type} (x : my_monad α) := monad_run.run x
```
-/
class monad_run (out : out_param $ Type u → Type v) (m : Type u → Type v) :=
(run {} {α : Type u} : m α → out α)
export monad_run (run)
|
527ce4ab6439d9e5217ca90e876c62cc412f07c5 | 0851884047bb567d19e188e8f1ad959c5ae9c5ce | /src/Topology/Material/homeomorphism_extension_of_equiv.lean | 6d994a830771b93a0fc14c2be748005c416759b5 | [
"Apache-2.0"
] | permissive | yz5216/xena-UROP-2018 | 00d3f71a831324966d40d302544ed2cbbec3fd0f | 5e9da9dc64dc51897677f8b73ab9f94061a8d977 | refs/heads/master | 1,584,922,436,989 | 1,531,487,250,000 | 1,531,487,250,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,266 | lean | import analysis.topology.continuity
import analysis.topology.topological_space
import analysis.topology.infinite_sum
import analysis.topology.topological_structures
import analysis.topology.uniform_space
import data.equiv
-- import data.equiv.basic
local attribute [instance] classical.prop_decidable
universes u v w
open set filter lattice classical
definition is_open_sets {α : Type u} (is_open : set α → Prop) :=
is_open univ ∧ (∀s t, is_open s → is_open t → is_open (s ∩ t)) ∧ (∀s, (∀t∈s, is_open t) → is_open (⋃₀ s))
definition is_to_top {α : Type u} (is_open : set α → Prop) (H : is_open_sets (is_open)) : topological_space α :=
{ is_open := is_open,
is_open_univ := H.left,
is_open_inter := H.right.left,
is_open_sUnion := H.right.right
}
definition top_to_is {α : Type u} (T : topological_space α) : is_open_sets (T.is_open) :=
⟨T.is_open_univ,T.is_open_inter,T.is_open_sUnion⟩
structure homeomorphism {α : Type*} {β : Type*} (X : topological_space α) (Y : topological_space β) extends equiv α β :=
(to_is_continuous : continuous to_fun)
(from_is_continuous : continuous inv_fun)
definition is_homeomorphic_to {α : Type u} {β : Type v} (X :topological_space α) (Y : topological_space β) : Prop := nonempty (homeomorphism X Y)
lemma id_map_continuous {α : Type u} {X : topological_space α} : continuous (@id α) :=
begin
intros s H1,
exact H1,
end
lemma constant_map_is_continuous {α : Type u} {β : Type v} {X : topological_space α} {Y : topological_space β} {f : α → β} (H : (∃ (b : β), f = function.const α b)) : continuous f :=
begin
unfold continuous,
cases H with b Hb,
rw Hb,
intros s Hs,
by_cases (b ∈ s),
unfold set.preimage,
unfold function.const,
have H : {x : α | b ∈ s} = univ,
apply set.ext,
simp,
intro,
assumption,
rw H,
exact X.is_open_univ,
unfold set.preimage,
unfold function.const,
have H : {x : α | b ∈ s} = ∅,
apply set.ext,
simp,
intro,
assumption,
rw H,
simp,
end
def indiscrete_topology (α : Type u) : topological_space α := {
is_open := λ y, y = ∅ ∨ y = univ,
is_open_univ := or.inr rfl,
is_open_inter := begin
intros s t Hs Ht,
cases Hs with Hsempty Hsuniv;
cases Ht with Htempty Htuniv,
rw Hsempty,
simp,
rw Hsempty,
simp,
rw Htempty,
simp,
rw [Hsuniv, Htuniv],
simp,
end,
is_open_sUnion := begin
intros I HI,
by_cases ∃ t ∈ I, t = univ,
cases h with U HU,
cases HU with U_in_I U_is_univ,
apply or.inr,
rw U_is_univ at U_in_I,
exact set.eq_of_subset_of_subset (set.subset_univ ⋃₀ I) (set.subset_sUnion_of_mem U_in_I),
simp at h,
apply or.inl,
have HI2 : ∀ (t : set α), t ∈ I → t = ∅,
intros t Ht,
cases (HI t Ht),
assumption,
rw h_1 at Ht,
cc,
rw set.sUnion_eq_Union,
apply set.ext,
intro x,
simp,
intros t Ht,
rw (HI2 t Ht),
simp,
end
}
def discrete_topology (α : Type u) : topological_space α := {
is_open := λ y, true,
is_open_univ := trivial,
is_open_inter := λ _ _ _ _, trivial,
is_open_sUnion := λ _ _, trivial,
}
lemma map_from_discrete_is_continuous {α : Type u} {β : Type v} [topological_space α] [Y : topological_space β]
(H : X = indiscrete_topology α) (f : α → β) : continuous f :=
begin
admit,
end
theorem smap_from_discrete_is_continuous {α : Type u} {β : Type v} {X : topological_space α} {Y : topological_space β}
{H : X = indiscrete_topology α} (f : α → β) : continuous f := map_from_discrete_is_continuous H f
#print continuous
definition id_is_homeomorphism {α : Type u} {X : topological_space α} : homeomorphism X X := {
to_fun := id,
inv_fun := id,
left_inv :=
begin
unfold function.left_inverse,
intro x,
simp,
end,
right_inv :=
begin
unfold function.right_inverse,
intro x,
simp,
end,
to_is_continuous :=
begin
unfold continuous,
unfold set.preimage,
intros s H1,
exact H1,
end,
from_is_continuous :=
begin
unfold continuous,
unfold set.preimage,
intros s H1,
exact H1,
end,
}
theorem homeomorphism_is_reflexive : reflexive (λ X Y : Σ α, topological_space α, is_homeomorphic_to X.2 Y.2) :=
begin
unfold reflexive,
intro x,
unfold is_homeomorphic_to,
have hom : homeomorphism (x.snd) (x.snd),
exact id_is_homeomorphism,
exact ⟨hom⟩,
end
theorem homeomorphism_is_symmetric : symmetric (λ X Y : Σ α, topological_space α, is_homeomorphic_to X.2 Y.2) :=
begin
unfold symmetric,
intros x y Hxy,
unfold is_homeomorphic_to, unfold is_homeomorphic_to at Hxy,
have homto : homeomorphism x.snd y.snd,
exact classical.choice Hxy,
have homfrom : homeomorphism y.snd x.snd,
exact {to_fun := homto.inv_fun, inv_fun := homto.to_fun, left_inv := homto.right_inv, right_inv := homto.left_inv, to_is_continuous := homto.from_is_continuous, from_is_continuous := homto.to_is_continuous},
exact nonempty.intro homfrom,
end
theorem homeomorphism_is_transitive : transitive (λ X Y : Σ α, topological_space α, is_homeomorphic_to X.2 Y.2) :=
begin
unfold transitive,
intros x y z Hxy Hyz,
unfold is_homeomorphic_to at *,
have homxy : homeomorphism x.snd y.snd, by exact classical.choice Hxy,
have homyz : homeomorphism y.snd z.snd, by exact classical.choice Hyz,
have homxz : homeomorphism x.snd z.snd,
exact {to_fun := homyz.to_fun ∘ homxy.to_fun,
inv_fun := homxy.inv_fun ∘ homyz.inv_fun,
left_inv := begin
unfold function.left_inverse,
simp,
intro a,
have H1 : (homyz.to_equiv).inv_fun ∘ (homyz.to_equiv).to_fun = id,
exact function.id_of_left_inverse homyz.left_inv,
rw function.funext_iff at H1,
simp at H1,
rw H1,
have H2 : (homxy.to_equiv).inv_fun ∘ (homxy.to_equiv).to_fun = id,
exact function.id_of_left_inverse homxy.left_inv,
rw function.funext_iff at H2,
simp at H2,
rw H2,
end,
right_inv := begin
unfold function.right_inverse,
unfold function.left_inverse,
simp,
intro a,
have H1: (homxy.to_equiv).to_fun ∘ (homxy.to_equiv).inv_fun = id,
exact function.id_of_right_inverse homxy.right_inv,
rw function.funext_iff at H1,
simp at H1,
rw H1,
have H2 : (homyz.to_equiv).to_fun ∘ (homyz.to_equiv).inv_fun = id,
exact function.id_of_right_inverse homyz.right_inv,
rw function.funext_iff at H2,
simp at H2,
rw H2,
end,
to_is_continuous := begin
exact @continuous.comp _ _ _ x.2 y.2 z.2 _ _ homxy.to_is_continuous homyz.to_is_continuous,
end,
from_is_continuous := begin
exact @continuous.comp _ _ _ z.2 y.2 x.2 _ _ homyz.from_is_continuous homxy.from_is_continuous,
end},
exact nonempty.intro homxz,
end
theorem homeomorphism_is_equivalence : equivalence (λ X Y : Σ α, topological_space α, is_homeomorphic_to X.2 Y.2) := ⟨homeomorphism_is_reflexive, homeomorphism_is_symmetric, homeomorphism_is_transitive⟩
theorem continuous_basis_to_continuous {α : Type*} {β : Type*} [X : topological_space α] [Y : topological_space β] : ∀ f : α → β, (∀ B : set (set β), topological_space.is_topological_basis B → (∀ b : B, is_open (f ⁻¹' b)) → continuous f) :=
begin
intros f Basis HBasis HBasisInverses U HU,
have HU_union_basis : ∃ S ⊆ Basis, U = ⋃₀ S, by exact topological_space.sUnion_basis_of_is_open HBasis HU,
cases HU_union_basis with S HS,
cases HS with HS1 HS2,
rw HS2,
rw set.preimage_sUnion,
have f_inv_t_open : ∀ t : set β, t ∈ S → topological_space.is_open X (f ⁻¹' t),
intros t HtS,
have t_is_open : topological_space.is_open Y t,
exact topological_space.is_open_of_is_topological_basis HBasis (set.mem_of_subset_of_mem HS1 HtS),
simp at HBasisInverses,
exact HBasisInverses t (set.mem_of_subset_of_mem HS1 HtS),
let set_of_preimages : set (set α) := {x | ∃ t ∈ S, x = f ⁻¹' t},
have preimages_are_open : topological_space.is_open X (⋃₀ set_of_preimages),
have H3 : ∀ (t : set α), t ∈ set_of_preimages → topological_space.is_open X t,
intros tinv Htinv,
simp at Htinv,
cases Htinv with t Ht,
rw Ht.2,
exact f_inv_t_open t Ht.1,
exact X.is_open_sUnion set_of_preimages H3,
unfold is_open,
have equal : (⋃₀ set_of_preimages) = (⋃ (t : set β) (H : t ∈ S), f ⁻¹' t),
rw set.sUnion_eq_Union,
apply set.ext,
intro x,
split,
intro Hx,
simp,
simp at Hx,
cases Hx with f_inv_t Hf_inv_t,
cases Hf_inv_t.1 with t Ht,
existsi t,
split,
exact Ht.1,
rw ← set.mem_preimage_eq,
rw ← Ht.2,
exact Hf_inv_t.2,
intro Hx,
simp,
rw set.mem_Union_eq at Hx,
cases Hx with i Hi,
simp at Hi,
existsi (f ⁻¹' i),
split,
existsi i,
exact ⟨Hi.1, eq.refl (f ⁻¹' i)⟩,
simp,
exact Hi.2,
rw ← equal,
assumption,
end
|
8ec682cc4d1acaa0f73cd0ec901e9cacd0b3eb77 | 367134ba5a65885e863bdc4507601606690974c1 | /src/analysis/normed_space/multilinear.lean | aca97c5736893caeb14eb77465b0e3e806c8f4a5 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 70,621 | 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.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
open finset metric
local attribute [instance, priority 1001]
add_comm_group.to_add_comm_monoid normed_group.to_add_comm_group normed_space.to_semimodule
-- 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
by_cases hι : nonempty ι, swap,
{ refine ⟨∥f 0∥ + 1, add_pos_of_nonneg_of_pos (norm_nonneg _) zero_lt_one, λ m, _⟩,
obtain rfl : m = 0, from funext (λ i, (hι ⟨i⟩).elim),
simp [univ_eq_empty.2 hι, zero_le_one] },
resetI,
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] } }
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⟩
lemma norm_def : ∥f∥ = Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} := rfl
-- So that invocations of `real.Inf_le` 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∥ :=
lb_le_Inf _ 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_Inf _ bounds_nonempty bounds_bdd_below).2,
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 _ _) 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 :=
Inf_le _ bounds_bdd_below ⟨hMp, hM⟩
/-- The operator norm satisfies the triangle inequality. -/
theorem op_norm_add_le : ∥f + g∥ ≤ ∥f∥ + ∥g∥ :=
Inf_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
variables {𝕜' : Type*} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜' 𝕜]
[normed_space 𝕜' G] [is_scalar_tower 𝕜' 𝕜 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 to_normed_group : normed_group (continuous_multilinear_map 𝕜 E G) :=
normed_group.of_core _ ⟨op_norm_zero_iff, op_norm_add_le, op_norm_neg⟩
instance to_normed_space : normed_space 𝕜' (continuous_multilinear_map 𝕜 E G) :=
⟨λ c f, f.op_norm_smul_le c⟩
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 _))
/-- `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 }
section restrict_scalars
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, by simp [le_trans zero_le_one],
have A : ∥q∥ ≤ ∥p∥ + 1 := norm_le_of_mem_closed_ball (le_of_lt hq),
have : (max ∥q.2∥ ∥p.2∥) ≤ ∥p∥ + 1 :=
le_trans (max_le_max (norm_snd_le q) (norm_snd_le p)) (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
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_refl _) 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]
/-- The continuous multilinear map on `A^ι`, where `A` is a normed commutative algebra
over `𝕜`, associating to `m` the product of all the `m i`.
See also `continuous_multilinear_map.mk_pi_algebra_fin`. -/
protected def mk_pi_algebra : continuous_multilinear_map 𝕜 (λ i : ι, A) A :=
@multilinear_map.mk_continuous 𝕜 ι (λ i : ι, A) A _ _ _ _ _ _ _
(multilinear_map.mk_pi_algebra 𝕜 ι A) (if nonempty ι then 1 else ∥(1 : A)∥) $
begin
intro m,
by_cases hι : nonempty ι,
{ resetI, simp [hι, norm_prod_le' univ univ_nonempty] },
{ simp [eq_empty_of_not_nonempty hι univ, hι] }
end
variables {A 𝕜 ι}
@[simp] lemma mk_pi_algebra_apply (m : ι → A) :
continuous_multilinear_map.mk_pi_algebra 𝕜 ι A m = ∏ i, m i :=
rfl
lemma norm_mk_pi_algebra_le [nonempty ι] :
∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ 1 :=
calc ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ if nonempty ι then 1 else ∥(1 : A)∥ :
multilinear_map.mk_continuous_norm_le _ (by split_ifs; simp [zero_le_one]) _
... = _ : if_pos ‹_›
lemma norm_mk_pi_algebra_of_empty (h : ¬nonempty ι) :
∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ = ∥(1 : A)∥ :=
begin
apply le_antisymm,
calc ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ if nonempty ι then 1 else ∥(1 : A)∥ :
multilinear_map.mk_continuous_norm_le _ (by split_ifs; simp [zero_le_one]) _
... = ∥(1 : A)∥ : if_neg ‹_›,
convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance],
simp [eq_empty_of_not_nonempty h univ]
end
@[simp] lemma norm_mk_pi_algebra [norm_one_class A] :
∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ = 1 :=
begin
by_cases hι : nonempty ι,
{ resetI,
refine le_antisymm norm_mk_pi_algebra_le _,
convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance],
simp },
{ simp [norm_mk_pi_algebra_of_empty hι] }
end
end
section
variables (𝕜 n) (A : Type*) [normed_ring A] [normed_algebra 𝕜 A]
/-- The continuous multilinear map on `A^n`, where `A` is a normed algebra over `𝕜`, associating to
`m` the product of all the `m i`.
See also: `multilinear_map.mk_pi_algebra`. -/
protected def mk_pi_algebra_fin : continuous_multilinear_map 𝕜 (λ i : fin n, A) A :=
@multilinear_map.mk_continuous 𝕜 (fin n) (λ i : fin n, A) A _ _ _ _ _ _ _
(multilinear_map.mk_pi_algebra_fin 𝕜 n A) (nat.cases_on n ∥(1 : A)∥ (λ _, 1)) $
begin
intro m,
cases n,
{ simp },
{ have : @list.of_fn A n.succ m ≠ [] := by simp,
simpa [← fin.prod_of_fn] using list.norm_prod_le' this }
end
variables {A 𝕜 n}
@[simp] lemma mk_pi_algebra_fin_apply (m : fin n → A) :
continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A m = (list.of_fn m).prod :=
rfl
lemma norm_mk_pi_algebra_fin_succ_le :
∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n.succ A∥ ≤ 1 :=
multilinear_map.mk_continuous_norm_le _ zero_le_one _
lemma norm_mk_pi_algebra_fin_le_of_pos (hn : 0 < n) :
∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A∥ ≤ 1 :=
by cases n; [exact hn.false.elim, exact norm_mk_pi_algebra_fin_succ_le]
lemma norm_mk_pi_algebra_fin_zero :
∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 0 A∥ = ∥(1 : A)∥ :=
begin
refine le_antisymm (multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _) _,
convert ratio_le_op_norm _ (λ _, 1); [simp, apply_instance]
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 𝕜 ι (λ(i : ι), 𝕜) G _ _ _ _ _ _ _
(multilinear_map.mk_pi_ring 𝕜 ι z) (∥z∥)
(λ m, by simp only [multilinear_map.mk_pi_ring_apply, norm_smul, normed_field.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
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 equivalence in
`continuous_multilinear_map.pi_field_equiv_aux`. The continuous linear equivalence is
`continuous_multilinear_map.pi_field_equiv`. -/
protected def pi_field_equiv_aux : 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 }
/-- 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 continuous linear equivalence in
`continuous_multilinear_map.pi_field_equiv`. -/
protected def pi_field_equiv : G ≃L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G) :=
{ continuous_to_fun := begin
refine (continuous_multilinear_map.pi_field_equiv_aux 𝕜 ι G).to_linear_map.continuous_of_bound
(1 : ℝ) (λz, _),
rw one_mul,
change ∥continuous_multilinear_map.mk_pi_field 𝕜 ι z∥ ≤ ∥z∥,
exact multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _
end,
continuous_inv_fun := begin
refine
(continuous_multilinear_map.pi_field_equiv_aux 𝕜 ι G).symm.to_linear_map.continuous_of_bound
(1 : ℝ) (λf, _),
rw one_mul,
change ∥f (λi, 1)∥ ≤ ∥f∥,
apply @continuous_multilinear_map.unit_le_op_norm 𝕜 ι (λ (i : ι), 𝕜) G _ _ _ _ _ _ _ f,
simp [pi_norm_le_iff zero_le_one, le_refl]
end,
.. continuous_multilinear_map.pi_field_equiv_aux 𝕜 ι G }
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
/-- `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 }
/-- 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]}
(∥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, refl },
map_smul' := λ m i c x, by { simp, refl } } 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 : Π (i : fin n.succ), 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 : Π (i : 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
have : x = 0 := subsingleton.elim _ _, subst this,
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)} [decidable_pred (s : set (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)} [decidable_pred (s : set (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
|
efe9eb602daec93b8e1702b948feeee7f0812564 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/category_theory/limits/presheaf.lean | 53e4ac4544e22bfb5f18f3fe9b9157123e48b22f | [
"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 | 12,599 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import category_theory.adjunction
import category_theory.elements
import category_theory.limits.functor_category
import category_theory.limits.preserves.limits
import category_theory.limits.shapes.terminal
import category_theory.limits.types
/-!
# Colimit of representables
This file constructs an adjunction `yoneda_adjunction` between `(Cᵒᵖ ⥤ Type u)` and `ℰ` given a
functor `A : C ⥤ ℰ`, where the right adjoint sends `(E : ℰ)` to `c ↦ (A.obj c ⟶ E)` (provided `ℰ`
has colimits).
This adjunction is used to show that every presheaf is a colimit of representables.
Further, the left adjoint `colimit_adj.extend_along_yoneda : (Cᵒᵖ ⥤ Type u) ⥤ ℰ` satisfies
`yoneda ⋙ L ≅ A`, that is, an extension of `A : C ⥤ ℰ` to `(Cᵒᵖ ⥤ Type u) ⥤ ℰ` through
`yoneda : C ⥤ Cᵒᵖ ⥤ Type u`. It is the left Kan extension of `A` along the yoneda embedding,
sometimes known as the Yoneda extension.
`unique_extension_along_yoneda` shows `extend_along_yoneda` is unique amongst cocontinuous functors
with this property, establishing the presheaf category as the free cocompletion of a small category.
## Tags
colimit, representable, presheaf, free cocompletion
## References
* [S. MacLane, I. Moerdijk, *Sheaves in Geometry and Logic*][MM92]
* https://ncatlab.org/nlab/show/Yoneda+extension
-/
namespace category_theory
noncomputable theory
open category limits
universes u₁ u₂
variables {C : Type u₁} [small_category C]
variables {ℰ : Type u₂} [category.{u₁} ℰ]
variable (A : C ⥤ ℰ)
namespace colimit_adj
/--
The functor taking `(E : ℰ) (c : Cᵒᵖ)` to the homset `(A.obj C ⟶ E)`. It is shown in `L_adjunction`
that this functor has a left adjoint (provided `E` has colimits) given by taking colimits over
categories of elements.
In the case where `ℰ = Cᵒᵖ ⥤ Type u` and `A = yoneda`, this functor is isomorphic to the identity.
Defined as in [MM92], Chapter I, Section 5, Theorem 2.
-/
@[simps]
def restricted_yoneda : ℰ ⥤ (Cᵒᵖ ⥤ Type u₁) :=
yoneda ⋙ (whiskering_left _ _ (Type u₁)).obj (functor.op A)
/--
The functor `restricted_yoneda` is isomorphic to the identity functor when evaluated at the yoneda
embedding.
-/
def restricted_yoneda_yoneda : restricted_yoneda (yoneda : C ⥤ Cᵒᵖ ⥤ Type u₁) ≅ 𝟭 _ :=
nat_iso.of_components
(λ P, nat_iso.of_components (λ X, yoneda_sections_small X.unop _)
(λ X Y f, funext $ λ x,
begin
dsimp,
rw ← functor_to_types.naturality _ _ x f (𝟙 _),
dsimp,
simp,
end))
(λ _ _ _, rfl)
/--
(Implementation). The equivalence of homsets which helps construct the left adjoint to
`colimit_adj.restricted_yoneda`.
It is shown in `restrict_yoneda_hom_equiv_natural` that this is a natural bijection.
-/
def restrict_yoneda_hom_equiv (P : Cᵒᵖ ⥤ Type u₁) (E : ℰ)
{c : cocone ((category_of_elements.π P).left_op ⋙ A)} (t : is_colimit c) :
(c.X ⟶ E) ≃ (P ⟶ (restricted_yoneda A).obj E) :=
(t.hom_iso' E).to_equiv.trans
{ to_fun := λ k,
{ app := λ c p, k.1 (opposite.op ⟨_, p⟩),
naturality' := λ c c' f, funext $ λ p,
(k.2 (quiver.hom.op ⟨f, rfl⟩ :
(opposite.op ⟨c', P.map f p⟩ : P.elementsᵒᵖ) ⟶ opposite.op ⟨c, p⟩)).symm },
inv_fun := λ τ,
{ val := λ p, τ.app p.unop.1 p.unop.2,
property := λ p p' f,
begin
simp_rw [← f.unop.2],
apply (congr_fun (τ.naturality f.unop.1) p'.unop.2).symm,
end },
left_inv :=
begin
rintro ⟨k₁, k₂⟩,
ext,
dsimp,
congr' 1,
simp,
end,
right_inv :=
begin
rintro ⟨_, _⟩,
refl,
end }
/--
(Implementation). Show that the bijection in `restrict_yoneda_hom_equiv` is natural (on the right).
-/
lemma restrict_yoneda_hom_equiv_natural (P : Cᵒᵖ ⥤ Type u₁) (E₁ E₂ : ℰ) (g : E₁ ⟶ E₂)
{c : cocone _} (t : is_colimit c) (k : c.X ⟶ E₁) :
restrict_yoneda_hom_equiv A P E₂ t (k ≫ g) =
restrict_yoneda_hom_equiv A P E₁ t k ≫ (restricted_yoneda A).map g :=
begin
ext _ X p,
apply (assoc _ _ _).symm,
end
variables [has_colimits ℰ]
/--
The left adjoint to the functor `restricted_yoneda` (shown in `yoneda_adjunction`). It is also an
extension of `A` along the yoneda embedding (shown in `is_extension_along_yoneda`), in particular
it is the left Kan extension of `A` through the yoneda embedding.
-/
def extend_along_yoneda : (Cᵒᵖ ⥤ Type u₁) ⥤ ℰ :=
adjunction.left_adjoint_of_equiv
(λ P E, restrict_yoneda_hom_equiv A P E (colimit.is_colimit _))
(λ P E E' g, restrict_yoneda_hom_equiv_natural A P E E' g _)
@[simp]
lemma extend_along_yoneda_obj (P : Cᵒᵖ ⥤ Type u₁) : (extend_along_yoneda A).obj P =
colimit ((category_of_elements.π P).left_op ⋙ A) := rfl
/--
Show `extend_along_yoneda` is left adjoint to `restricted_yoneda`.
The construction of [MM92], Chapter I, Section 5, Theorem 2.
-/
def yoneda_adjunction : extend_along_yoneda A ⊣ restricted_yoneda A :=
adjunction.adjunction_of_equiv_left _ _
/--
The initial object in the category of elements for a representable functor. In `is_initial` it is
shown that this is initial.
-/
def elements.initial (A : C) : (yoneda.obj A).elements :=
⟨opposite.op A, 𝟙 _⟩
/--
Show that `elements.initial A` is initial in the category of elements for the `yoneda` functor.
-/
def is_initial (A : C) : is_initial (elements.initial A) :=
{ desc := λ s, ⟨s.X.2.op, comp_id _⟩,
uniq' := λ s m w,
begin
simp_rw ← m.2,
dsimp [elements.initial],
simp,
end }
/--
`extend_along_yoneda A` is an extension of `A` to the presheaf category along the yoneda embedding.
`unique_extension_along_yoneda` shows it is unique among functors preserving colimits with this
property (up to isomorphism).
The first part of [MM92], Chapter I, Section 5, Corollary 4.
See Property 1 of https://ncatlab.org/nlab/show/Yoneda+extension#properties.
-/
def is_extension_along_yoneda : (yoneda : C ⥤ Cᵒᵖ ⥤ Type u₁) ⋙ extend_along_yoneda A ≅ A :=
nat_iso.of_components
(λ X, (colimit.is_colimit _).cocone_point_unique_up_to_iso
(colimit_of_diagram_terminal (terminal_op_of_initial (is_initial _)) _))
begin
intros X Y f,
change (colimit.desc _ ⟨_, _⟩ ≫ colimit.desc _ _) = colimit.desc _ _ ≫ _,
apply colimit.hom_ext,
intro j,
rw [colimit.ι_desc_assoc, colimit.ι_desc_assoc],
change (colimit.ι _ _ ≫ 𝟙 _) ≫ colimit.desc _ _ = _,
rw [comp_id, colimit.ι_desc],
dsimp,
rw ← A.map_comp,
congr' 1,
end
/-- See Property 2 of https://ncatlab.org/nlab/show/Yoneda+extension#properties. -/
instance : preserves_colimits (extend_along_yoneda A) :=
(yoneda_adjunction A).left_adjoint_preserves_colimits
end colimit_adj
open colimit_adj
/--
Since `extend_along_yoneda A` is adjoint to `restricted_yoneda A`, if we use `A = yoneda`
then `restricted_yoneda A` is isomorphic to the identity, and so `extend_along_yoneda A` is as well.
-/
def extend_along_yoneda_yoneda : extend_along_yoneda (yoneda : C ⥤ _) ≅ 𝟭 _ :=
adjunction.nat_iso_of_right_adjoint_nat_iso
(yoneda_adjunction _)
adjunction.id
restricted_yoneda_yoneda
/--
A functor to the presheaf category in which everything in the image is representable (witnessed
by the fact that it factors through the yoneda embedding).
`cocone_of_representable` gives a cocone for this functor which is a colimit and has point `P`.
-/
-- Maybe this should be reducible or an abbreviation?
def functor_to_representables (P : Cᵒᵖ ⥤ Type u₁) :
(P.elements)ᵒᵖ ⥤ Cᵒᵖ ⥤ Type u₁ :=
(category_of_elements.π P).left_op ⋙ yoneda
/--
This is a cocone with point `P` for the functor `functor_to_representables P`. It is shown in
`colimit_of_representable P` that this cocone is a colimit: that is, we have exhibited an arbitrary
presheaf `P` as a colimit of representables.
The construction of [MM92], Chapter I, Section 5, Corollary 3.
-/
def cocone_of_representable (P : Cᵒᵖ ⥤ Type u₁) :
cocone (functor_to_representables P) :=
cocone.extend (colimit.cocone _) (extend_along_yoneda_yoneda.hom.app P)
@[simp] lemma cocone_of_representable_X (P : Cᵒᵖ ⥤ Type u₁) :
(cocone_of_representable P).X = P :=
rfl
/-- An explicit formula for the legs of the cocone `cocone_of_representable`. -/
-- Marking this as a simp lemma seems to make things more awkward.
lemma cocone_of_representable_ι_app (P : Cᵒᵖ ⥤ Type u₁) (j : (P.elements)ᵒᵖ):
(cocone_of_representable P).ι.app j = (yoneda_sections_small _ _).inv j.unop.2 :=
colimit.ι_desc _ _
/-- The legs of the cocone `cocone_of_representable` are natural in the choice of presheaf. -/
lemma cocone_of_representable_naturality {P₁ P₂ : Cᵒᵖ ⥤ Type u₁} (α : P₁ ⟶ P₂)
(j : (P₁.elements)ᵒᵖ) :
(cocone_of_representable P₁).ι.app j ≫ α =
(cocone_of_representable P₂).ι.app ((category_of_elements.map α).op.obj j) :=
begin
ext T f,
simpa [cocone_of_representable_ι_app] using functor_to_types.naturality _ _ α f.op _,
end
/--
The cocone with point `P` given by `the_cocone` is a colimit: that is, we have exhibited an
arbitrary presheaf `P` as a colimit of representables.
The result of [MM92], Chapter I, Section 5, Corollary 3.
-/
def colimit_of_representable (P : Cᵒᵖ ⥤ Type u₁) : is_colimit (cocone_of_representable P) :=
begin
apply is_colimit.of_point_iso (colimit.is_colimit (functor_to_representables P)),
change is_iso (colimit.desc _ (cocone.extend _ _)),
rw [colimit.desc_extend, colimit.desc_cocone],
apply_instance,
end
/--
Given two functors L₁ and L₂ which preserve colimits, if they agree when restricted to the
representable presheaves then they agree everywhere.
-/
def nat_iso_of_nat_iso_on_representables (L₁ L₂ : (Cᵒᵖ ⥤ Type u₁) ⥤ ℰ)
[preserves_colimits L₁] [preserves_colimits L₂]
(h : yoneda ⋙ L₁ ≅ yoneda ⋙ L₂) : L₁ ≅ L₂ :=
begin
apply nat_iso.of_components _ _,
{ intro P,
refine (is_colimit_of_preserves L₁ (colimit_of_representable P)).cocone_points_iso_of_nat_iso
(is_colimit_of_preserves L₂ (colimit_of_representable P)) _,
apply functor.associator _ _ _ ≪≫ _,
exact iso_whisker_left (category_of_elements.π P).left_op h },
{ intros P₁ P₂ f,
apply (is_colimit_of_preserves L₁ (colimit_of_representable P₁)).hom_ext,
intro j,
dsimp only [id.def, is_colimit.cocone_points_iso_of_nat_iso_hom, iso_whisker_left_hom],
have :
(L₁.map_cocone (cocone_of_representable P₁)).ι.app j ≫ L₁.map f =
(L₁.map_cocone (cocone_of_representable P₂)).ι.app ((category_of_elements.map f).op.obj j),
{ dsimp,
rw [← L₁.map_comp, cocone_of_representable_naturality],
refl },
rw [reassoc_of this, is_colimit.ι_map_assoc, is_colimit.ι_map],
dsimp,
rw [← L₂.map_comp, cocone_of_representable_naturality],
refl }
end
variable [has_colimits ℰ]
/--
Show that `extend_along_yoneda` is the unique colimit-preserving functor which extends `A` to
the presheaf category.
The second part of [MM92], Chapter I, Section 5, Corollary 4.
See Property 3 of https://ncatlab.org/nlab/show/Yoneda+extension#properties.
-/
def unique_extension_along_yoneda (L : (Cᵒᵖ ⥤ Type u₁) ⥤ ℰ) (hL : yoneda ⋙ L ≅ A)
[preserves_colimits L] :
L ≅ extend_along_yoneda A :=
nat_iso_of_nat_iso_on_representables _ _ (hL ≪≫ (is_extension_along_yoneda _).symm)
/--
If `L` preserves colimits and `ℰ` has them, then it is a left adjoint. This is a special case of
`is_left_adjoint_of_preserves_colimits` used to prove that.
-/
def is_left_adjoint_of_preserves_colimits_aux (L : (Cᵒᵖ ⥤ Type u₁) ⥤ ℰ) [preserves_colimits L] :
is_left_adjoint L :=
{ right := restricted_yoneda (yoneda ⋙ L),
adj := (yoneda_adjunction _).of_nat_iso_left
((unique_extension_along_yoneda _ L (iso.refl _)).symm) }
/--
If `L` preserves colimits and `ℰ` has them, then it is a left adjoint. Note this is a (partial)
converse to `left_adjoint_preserves_colimits`.
-/
def is_left_adjoint_of_preserves_colimits (L : (C ⥤ Type u₁) ⥤ ℰ) [preserves_colimits L] :
is_left_adjoint L :=
let e : (_ ⥤ Type u₁) ≌ (_ ⥤ Type u₁) := (op_op_equivalence C).congr_left,
t := is_left_adjoint_of_preserves_colimits_aux (e.functor ⋙ L : _)
in by exactI adjunction.left_adjoint_of_nat_iso (e.inv_fun_id_assoc _)
end category_theory
|
0b3f8f1eb36cf059b1348ad1d69012aa08f4bb6d | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/number_theory/fermat_psp.lean | cde8495f8b725da55aff29522aaf952f266a5f40 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 19,752 | lean | /-
Copyright (c) 2022 Niels Voss. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Niels Voss
-/
import data.nat.prime
import field_theory.finite.basic
import order.filter.cofinite
/-!
# Fermat Pseudoprimes
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we define Fermat pseudoprimes: composite numbers that pass the Fermat primality test.
A natural number `n` passes the Fermat primality test to base `b` (and is therefore deemed a
"probable prime") if `n` divides `b ^ (n - 1) - 1`. `n` is a Fermat pseudoprime to base `b` if `n`
is a composite number that passes the Fermat primality test to base `b` and is coprime with `b`.
Fermat pseudoprimes can also be seen as composite numbers for which Fermat's little theorem holds
true.
Numbers which are Fermat pseudoprimes to all bases are known as Carmichael numbers (not yet defined
in this file).
## Main Results
The main definitions for this file are
- `fermat_psp.probable_prime`: A number `n` is a probable prime to base `b` if it passes the Fermat
primality test; that is, if `n` divides `b ^ (n - 1) - 1`
- `fermat_psp`: A number `n` is a pseudoprime to base `b` if it is a probable prime to base `b`, is
composite, and is coprime with `b` (this last condition is automatically true if `n` divides
`b ^ (n - 1) - 1`, but some sources include it in the definition).
Note that all composite numbers are pseudoprimes to base 0 and 1, and that the definiton of
`probable_prime` in this file implies that all numbers are probable primes to bases 0 and 1, and
that 0 and 1 are probable primes to any base.
The main theorems are
- `fermat_psp.exists_infinite_pseudoprimes`: there are infinite pseudoprimes to any base `b ≥ 1`
-/
/--
`n` is a probable prime to base `b` if `n` passes the Fermat primality test; that is, `n` divides
`b ^ (n - 1) - 1`.
This definition implies that all numbers are probable primes to base 0 or 1, and that 0 and 1 are
probable primes to any base.
-/
def fermat_psp.probable_prime (n b : ℕ) : Prop := n ∣ b ^ (n - 1) - 1
/--
`n` is a Fermat pseudoprime to base `b` if `n` is a probable prime to base `b` and is composite. By
this definition, all composite natural numbers are pseudoprimes to base 0 and 1. This definition
also permits `n` to be less than `b`, so that 4 is a pseudoprime to base 5, for example.
-/
def fermat_psp (n b : ℕ) : Prop := fermat_psp.probable_prime n b ∧ ¬n.prime ∧ 1 < n
namespace fermat_psp
instance decidable_probable_prime (n b : ℕ) : decidable (probable_prime n b) :=
nat.decidable_dvd _ _
instance decidable_psp (n b : ℕ) : decidable (fermat_psp n b) := and.decidable
/--
If `n` passes the Fermat primality test to base `b`, then `n` is coprime with `b`, assuming that
`n` and `b` are both positive.
-/
lemma coprime_of_probable_prime {n b : ℕ} (h : probable_prime n b) (h₁ : 1 ≤ n) (h₂ : 1 ≤ b) :
nat.coprime n b :=
begin
by_cases h₃ : 2 ≤ n,
{ -- To prove that `n` is coprime with `b`, we we need to show that for all prime factors of `n`,
-- we can derive a contradiction if `n` divides `b`.
apply nat.coprime_of_dvd,
-- If `k` is a prime number that divides both `n` and `b`, then we know that `n = m * k` and
-- `b = j * k` for some natural numbers `m` and `j`. We substitute these into the hypothesis.
rintros k hk ⟨m, rfl⟩ ⟨j, rfl⟩,
-- Because prime numbers do not divide 1, it suffices to show that `k ∣ 1` to prove a
-- contradiction
apply nat.prime.not_dvd_one hk,
-- Since `n` divides `b ^ (n - 1) - 1`, `k` also divides `b ^ (n - 1) - 1`
replace h := dvd_of_mul_right_dvd h,
-- Because `k` divides `b ^ (n - 1) - 1`, if we can show that `k` also divides `b ^ (n - 1)`,
-- then we know `k` divides 1.
rw [nat.dvd_add_iff_right h, nat.sub_add_cancel (nat.one_le_pow _ _ h₂)],
-- Since `k` divides `b`, `k` also divides any power of `b` except `b ^ 0`. Therefore, it
-- suffices to show that `n - 1` isn't zero. However, we know that `n - 1` isn't zero because we
-- assumed `2 ≤ n` when doing `by_cases`.
refine dvd_of_mul_right_dvd (dvd_pow_self (k * j) _),
linarith },
-- If `n = 1`, then it follows trivially that `n` is coprime with `b`.
{ rw (show n = 1, by linarith),
norm_num }
end
lemma probable_prime_iff_modeq (n : ℕ) {b : ℕ} (h : 1 ≤ b) :
probable_prime n b ↔ b ^ (n - 1) ≡ 1 [MOD n] :=
begin
have : 1 ≤ b ^ (n - 1) := one_le_pow_of_one_le h (n - 1), -- For exact_mod_cast
rw nat.modeq.comm,
split,
{ intro h₁,
apply nat.modeq_of_dvd,
exact_mod_cast h₁, },
{ intro h₁,
exact_mod_cast nat.modeq.dvd h₁, },
end
/--
If `n` is a Fermat pseudoprime to base `b`, then `n` is coprime with `b`, assuming that `b` is
positive.
This lemma is a small wrapper based on `coprime_of_probable_prime`
-/
lemma coprime_of_fermat_psp {n b : ℕ} (h : fermat_psp n b) (h₁ : 1 ≤ b) : nat.coprime n b :=
begin
rcases h with ⟨hp, hn₁, hn₂⟩,
exact coprime_of_probable_prime hp (by linarith) h₁,
end
/--
All composite numbers are Fermat pseudoprimes to base 1.
-/
lemma base_one {n : ℕ} (h₁ : 1 < n) (h₂ : ¬n.prime) : fermat_psp n 1 :=
begin
refine ⟨show n ∣ 1 ^ (n - 1) - 1, from _, h₂, h₁⟩,
exact (show 0 = 1 ^ (n - 1) - 1, by norm_num) ▸ dvd_zero n,
end
-- Lemmas that are needed to prove statements in this file, but aren't directly related to Fermat
-- pseudoprimes
section helper_lemmas
private lemma pow_gt_exponent {a : ℕ} (b : ℕ) (h : 2 ≤ a) : b < a ^ b :=
lt_of_lt_of_le (nat.lt_two_pow b) $ nat.pow_le_pow_of_le_left h _
private lemma a_id_helper {a b : ℕ} (ha : 2 ≤ a) (hb : 2 ≤ b) : 2 ≤ (a ^ b - 1) / (a - 1) :=
begin
change 1 < _,
have h₁ : a - 1 ∣ a ^ b - 1 := by simpa only [one_pow] using nat_sub_dvd_pow_sub_pow a 1 b,
rw [nat.lt_div_iff_mul_lt h₁, mul_one, tsub_lt_tsub_iff_right (nat.le_of_succ_le ha)],
convert pow_lt_pow (nat.lt_of_succ_le ha) hb,
rw pow_one
end
private lemma b_id_helper {a b : ℕ} (ha : 2 ≤ a) (hb : 2 < b) : 2 ≤ (a ^ b + 1) / (a + 1) :=
begin
rw nat.le_div_iff_mul_le (nat.zero_lt_succ _),
apply nat.succ_le_succ,
calc 2 * a + 1 ≤ a ^ 2 * a : by nlinarith
... = a ^ 3 : by rw pow_succ' a 2
... ≤ a ^ b : pow_le_pow (nat.le_of_succ_le ha) hb
end
private lemma AB_id_helper (b p : ℕ) (hb : 2 ≤ b) (hp : odd p)
: (b ^ p - 1) / (b - 1) * ((b ^ p + 1) / (b + 1)) = (b ^ (2 * p) - 1) / (b ^ 2 - 1) :=
begin
have q₁ : b - 1 ∣ b ^ p - 1 := by simpa only [one_pow] using nat_sub_dvd_pow_sub_pow b 1 p,
have q₂ : b + 1 ∣ b ^ p + 1 := by simpa only [one_pow] using hp.nat_add_dvd_pow_add_pow b 1,
convert nat.div_mul_div_comm q₁ q₂; rw [mul_comm (_ - 1), ←nat.sq_sub_sq],
{ ring_exp },
{ simp }
end
/--
Used in the proof of `psp_from_prime_psp`
-/
private lemma bp_helper {b p : ℕ} (hb : 0 < b) (hp : 1 ≤ p) :
b ^ (2 * p) - 1 - (b ^ 2 - 1) = b * (b ^ (p - 1) - 1) * (b ^ p + b) :=
have hi_bsquared : 1 ≤ b ^ 2 := nat.one_le_pow _ _ hb,
calc b ^ (2 * p) - 1 - (b ^ 2 - 1) = b ^ (2 * p) - (1 + (b ^ 2 - 1)) : by rw nat.sub_sub
... = b ^ (2 * p) - (1 + b ^ 2 - 1) : by rw nat.add_sub_assoc hi_bsquared
... = b ^ (2 * p) - (b ^ 2) : by rw nat.add_sub_cancel_left
... = b ^ (p * 2) - (b ^ 2) : by rw mul_comm
... = (b ^ p) ^ 2 - (b ^ 2) : by rw pow_mul
... = (b ^ p + b) * (b ^ p - b) : by rw nat.sq_sub_sq
... = (b ^ p - b) * (b ^ p + b) : by rw mul_comm
... = (b ^ (p - 1 + 1) - b) * (b ^ p + b) : by rw nat.sub_add_cancel hp
... = (b * b ^ (p - 1) - b) * (b ^ p + b) : by rw pow_succ
... = (b * b ^ (p - 1) - b * 1) * (b ^ p + b) : by rw mul_one
... = b * (b ^ (p - 1) - 1) * (b ^ p + b) : by rw nat.mul_sub_left_distrib
end helper_lemmas
/--
Given a prime `p` which does not divide `b * (b ^ 2 - 1)`, we can produce a number `n` which is
larger than `p` and pseudoprime to base `b`. We do this by defining
`n = ((b ^ p - 1) / (b - 1)) * ((b ^ p + 1) / (b + 1))`
The primary purpose of this definition is to help prove `exists_infinite_pseudoprimes`. For a proof
that `n` is actually pseudoprime to base `b`, see `psp_from_prime_psp`, and for a proof that `n` is
greater than `p`, see `psp_from_prime_gt_p`.
This lemma is intended to be used when `2 ≤ b`, `2 < p`, `p` is prime, and `¬p ∣ b * (b ^ 2 - 1)`,
because those are the hypotheses for `psp_from_prime_psp`.
-/
private def psp_from_prime (b : ℕ) (p : ℕ) : ℕ := (b ^ p - 1) / (b - 1) * ((b ^ p + 1) / (b + 1))
/--
This is a proof that the number produced using `psp_from_prime` is actually pseudoprime to base `b`.
The primary purpose of this lemma is to help prove `exists_infinite_pseudoprimes`.
We use <https://primes.utm.edu/notes/proofs/a_pseudoprimes.html> as a rough outline of the proof.
-/
private lemma psp_from_prime_psp {b : ℕ} (b_ge_two : 2 ≤ b) {p : ℕ} (p_prime : p.prime)
(p_gt_two : 2 < p) (not_dvd : ¬p ∣ b * (b ^ 2 - 1)) :
fermat_psp (psp_from_prime b p) b :=
begin
unfold psp_from_prime,
set A := (b ^ p - 1) / (b - 1),
set B := (b ^ p + 1) / (b + 1),
-- Inequalities
have hi_A : 1 < A := a_id_helper (nat.succ_le_iff.mp b_ge_two) (nat.prime.one_lt p_prime),
have hi_B : 1 < B := b_id_helper (nat.succ_le_iff.mp b_ge_two) p_gt_two,
have hi_AB : 1 < A * B := one_lt_mul'' hi_A hi_B,
have hi_b : 0 < b := by linarith,
have hi_p : 1 ≤ p := nat.one_le_of_lt p_gt_two,
have hi_bsquared : 0 < b ^ 2 - 1 := by nlinarith [nat.one_le_pow 2 b hi_b],
have hi_bpowtwop : 1 ≤ b ^ (2 * p) := nat.one_le_pow (2 * p) b hi_b,
have hi_bpowpsubone : 1 ≤ b ^ (p - 1) := nat.one_le_pow (p - 1) b hi_b,
-- Other useful facts
have p_odd : odd p := p_prime.odd_of_ne_two p_gt_two.ne.symm,
have AB_not_prime : ¬nat.prime (A * B) := nat.not_prime_mul hi_A hi_B,
have AB_id : A * B = (b ^ (2 * p) - 1) / (b ^ 2 - 1) := AB_id_helper _ _ b_ge_two p_odd,
have hd : b ^ 2 - 1 ∣ b ^ (2 * p) - 1,
{ simpa only [one_pow, pow_mul] using nat_sub_dvd_pow_sub_pow _ 1 p },
-- We know that `A * B` is not prime, and that `1 < A * B`. Since two conditions of being
-- pseudoprime are satisfied, we only need to show that `A * B` is probable prime to base `b`
refine ⟨_, AB_not_prime, hi_AB⟩,
-- Used to prove that `2 * p * (b ^ 2 - 1) ∣ (b ^ 2 - 1) * (A * B - 1)`.
have ha₁ : (b ^ 2 - 1) * (A * B - 1) = b * (b ^ (p - 1) - 1) * (b ^ p + b),
{ apply_fun (λ x, x * (b ^ 2 - 1)) at AB_id,
rw nat.div_mul_cancel hd at AB_id,
apply_fun (λ x, x - (b ^ 2 - 1)) at AB_id,
nth_rewrite 1 ←one_mul (b ^ 2 - 1) at AB_id,
rw [←nat.mul_sub_right_distrib, mul_comm] at AB_id,
rw AB_id,
exact bp_helper hi_b hi_p },
-- If `b` is even, then `b^p` is also even, so `2 ∣ b^p + b`
-- If `b` is odd, then `b^p` is also odd, so `2 ∣ b^p + b`
have ha₂ : 2 ∣ b ^ p + b,
{ by_cases h : even b,
{ replace h : 2 ∣ b := even_iff_two_dvd.mp h,
have : p ≠ 0 := by linarith,
have : 2 ∣ b^p := dvd_pow h this,
exact dvd_add this h },
{ have h : odd b := nat.odd_iff_not_even.mpr h,
have : odd (b ^ p) := odd.pow h,
have : even (b ^ p + b) := odd.add_odd this h,
exact even_iff_two_dvd.mp this } },
-- Since `b` isn't divisible by `p`, `b` is coprime with `p`. we can use Fermat's Little Theorem
-- to prove this.
have ha₃ : p ∣ b ^ (p - 1) - 1,
{ have : ¬p ∣ b := mt (assume h : p ∣ b, dvd_mul_of_dvd_left h _) not_dvd,
have : p.coprime b := or.resolve_right (nat.coprime_or_dvd_of_prime p_prime b) this,
have : is_coprime (b : ℤ) ↑p := this.symm.is_coprime,
have : ↑b ^ (p - 1) ≡ 1 [ZMOD ↑p] := int.modeq.pow_card_sub_one_eq_one p_prime this,
have : ↑p ∣ ↑b ^ (p - 1) - ↑1 := int.modeq.dvd (int.modeq.symm this),
exact_mod_cast this },
-- Because `p - 1` is even, there is a `c` such that `2 * c = p - 1`. `nat_sub_dvd_pow_sub_pow`
-- implies that `b ^ c - 1 ∣ (b ^ c) ^ 2 - 1`, and `(b ^ c) ^ 2 = b ^ (p - 1)`.
have ha₄ : b ^ 2 - 1 ∣ b ^ (p - 1) - 1,
{ cases p_odd with k hk,
have : 2 ∣ p - 1 := ⟨k, by simp [hk]⟩,
cases this with c hc,
have : b ^ 2 - 1 ∣ (b ^ 2) ^ c - 1 :=
by simpa only [one_pow] using nat_sub_dvd_pow_sub_pow _ 1 c,
have : b ^ 2 - 1 ∣ b ^ (2 * c) - 1 := by rwa ←pow_mul at this,
rwa ←hc at this },
-- Used to prove that `2 * p` divides `A * B - 1`
have ha₅ : 2 * p * (b ^ 2 - 1) ∣ (b ^ 2 - 1) * (A * B - 1),
{ suffices q : 2 * p * (b ^ 2 - 1) ∣ b * (b ^ (p - 1) - 1) * (b ^ p + b),
{ rwa ha₁ },
-- We already proved that `b ^ 2 - 1 ∣ b ^ (p - 1) - 1`.
-- Since `2 ∣ b ^ p + b` and `p ∣ b ^ p + b`, if we show that 2 and p are coprime, then we
-- know that `2 * p ∣ b ^ p + b`
have q₁ : nat.coprime p (b ^ 2 - 1),
{ have q₂ : ¬p ∣ b ^ 2 - 1,
{ rw mul_comm at not_dvd,
exact mt (assume h : p ∣ b ^ 2 - 1, dvd_mul_of_dvd_left h _) not_dvd },
exact (nat.prime.coprime_iff_not_dvd p_prime).mpr q₂ },
have q₂ : p * (b ^ 2 - 1) ∣ b ^ (p - 1) - 1 := nat.coprime.mul_dvd_of_dvd_of_dvd q₁ ha₃ ha₄,
have q₃ : p * (b ^ 2 - 1) * 2 ∣ (b ^ (p - 1) - 1) * (b ^ p + b) := mul_dvd_mul q₂ ha₂,
have q₄ : p * (b ^ 2 - 1) * 2 ∣ b * ((b ^ (p - 1) - 1) * (b ^ p + b)),
from dvd_mul_of_dvd_right q₃ _,
rwa [mul_assoc, mul_comm, mul_assoc b] },
have ha₆ : 2 * p ∣ A * B - 1,
{ rw mul_comm at ha₅,
exact nat.dvd_of_mul_dvd_mul_left hi_bsquared ha₅ },
-- `A * B` divides `b ^ (2 * p) - 1` because `A * B * (b ^ 2 - 1) = b ^ (2 * p) - 1`.
-- This can be proven by multiplying both sides of `AB_id` by `b ^ 2 - 1`.
have ha₇ : A * B ∣ b ^ (2 * p) - 1,
{ use b ^ 2 - 1,
have : A * B * (b ^ 2 - 1) = (b ^ (2 * p) - 1) / (b ^ 2 - 1) * (b ^ 2 - 1),
from congr_arg (λ x : ℕ, x * (b ^ 2 - 1)) AB_id,
simpa only [add_comm, nat.div_mul_cancel hd, nat.sub_add_cancel hi_bpowtwop] using this.symm },
-- Since `2 * p ∣ A * B - 1`, there is a number `q` such that `2 * p * q = A * B - 1`.
-- By `nat_sub_dvd_pow_sub_pow`, we know that `b ^ (2 * p) - 1 ∣ b ^ (2 * p * q) - 1`.
-- This means that `b ^ (2 * p) - 1 ∣ b ^ (A * B - 1) - 1`.
cases ha₆ with q hq,
have ha₈ : b ^ (2 * p) - 1 ∣ b ^ (A * B - 1) - 1 :=
by simpa only [one_pow, pow_mul, hq] using nat_sub_dvd_pow_sub_pow _ 1 q,
-- We have proved that `A * B ∣ b ^ (2 * p) - 1` and `b ^ (2 * p) - 1 ∣ b ^ (A * B - 1) - 1`.
-- Therefore, `A * B ∣ b ^ (A * B - 1) - 1`.
exact dvd_trans ha₇ ha₈
end
/--
This is a proof that the number produced using `psp_from_prime` is greater than the prime `p` used
to create it. The primary purpose of this lemma is to help prove `exists_infinite_pseudoprimes`.
-/
private lemma psp_from_prime_gt_p {b : ℕ} (b_ge_two : 2 ≤ b) {p : ℕ} (p_prime : p.prime)
(p_gt_two : 2 < p) :
p < psp_from_prime b p :=
begin
unfold psp_from_prime,
set A := (b ^ p - 1) / (b - 1),
set B := (b ^ p + 1) / (b + 1),
rw show A * B = (b ^ (2 * p) - 1) / (b ^ 2 - 1),
from AB_id_helper _ _ b_ge_two (p_prime.odd_of_ne_two p_gt_two.ne.symm),
have AB_dvd : b ^ 2 - 1 ∣ b ^ (2 * p) - 1,
by simpa only [one_pow, pow_mul] using nat_sub_dvd_pow_sub_pow _ 1 p,
suffices h : p * (b ^ 2 - 1) < b ^ (2 * p) - 1,
{ have h₁ : (p * (b ^ 2 - 1)) / (b ^ 2 - 1) < (b ^ (2 * p) - 1) / (b ^ 2 - 1),
from nat.div_lt_div_of_lt_of_dvd AB_dvd h,
have h₂ : 0 < b ^ 2 - 1,
by linarith [show 3 ≤ b ^ 2 - 1, from le_tsub_of_add_le_left (show 4 ≤ b ^ 2, by nlinarith)],
rwa nat.mul_div_cancel _ h₂ at h₁ },
rw [nat.mul_sub_left_distrib, mul_one, pow_mul],
nth_rewrite_rhs 0 ←nat.sub_add_cancel (show 1 ≤ p, by linarith),
rw pow_succ (b ^ 2),
suffices h : p * b ^ 2 < b ^ 2 * (b ^ 2) ^ (p - 1),
{ apply gt_of_ge_of_gt,
{ exact tsub_le_tsub_left (show 1 ≤ p, by linarith) (b ^ 2 * (b ^ 2) ^ (p - 1)) },
{ have : p ≤ p * b ^ 2 := nat.le_mul_of_pos_right (show 0 < b ^ 2, by nlinarith),
exact tsub_lt_tsub_right_of_le this h } },
suffices h : p < (b ^ 2) ^ (p - 1),
{ rw mul_comm (b ^ 2),
have : 4 ≤ b ^ 2 := by nlinarith,
have : 0 < b ^ 2 := by linarith,
exact mul_lt_mul_of_pos_right h this },
rw [←pow_mul, nat.mul_sub_left_distrib, mul_one],
have : 2 ≤ 2 * p - 2 := le_tsub_of_add_le_left (show 4 ≤ 2 * p, by linarith),
have : 2 + p ≤ 2 * p := by linarith,
have : p ≤ 2 * p - 2 := le_tsub_of_add_le_left this,
exact nat.lt_of_le_of_lt this (pow_gt_exponent _ b_ge_two)
end
/--
For all positive bases, there exist Fermat infinite pseudoprimes to that base.
Given in this form: for all numbers `b ≥ 1` and `m`, there exists a pseudoprime `n` to base `b` such
that `m ≤ n`. This form is similar to `nat.exists_infinite_primes`.
-/
theorem exists_infinite_pseudoprimes {b : ℕ} (h : 1 ≤ b) (m : ℕ) : ∃ n : ℕ, fermat_psp n b ∧ m ≤ n
:=
begin
by_cases b_ge_two : 2 ≤ b,
-- If `2 ≤ b`, then because there exist infinite prime numbers, there is a prime number p such
-- `m ≤ p` and `¬p ∣ b*(b^2 - 1)`. We pick a prime number `b*(b^2 - 1) + 1 + m ≤ p` because we
-- automatically know that `p` is greater than m and that it does not divide `b*(b^2 - 1)`
-- (because `p` can't divide a number less than `p`).
-- From `p`, we can use the lemmas we proved earlier to show that
-- `((b^p - 1)/(b - 1)) * ((b^p + 1)/(b + 1))` is a pseudoprime to base `b`.
{ have h := nat.exists_infinite_primes (b * (b ^ 2 - 1) + 1 + m),
cases h with p hp,
cases hp with hp₁ hp₂,
have h₁ : 0 < b := pos_of_gt (nat.succ_le_iff.mp b_ge_two),
have h₂ : 4 ≤ b ^ 2 := pow_le_pow_of_le_left' b_ge_two 2,
have h₃ : 0 < b ^ 2 - 1 := tsub_pos_of_lt (gt_of_ge_of_gt h₂ (by norm_num)),
have h₄ : 0 < b * (b ^ 2 - 1) := mul_pos h₁ h₃,
have h₅ : b * (b ^ 2 - 1) < p := by linarith,
have h₆ : ¬p ∣ b * (b ^ 2 - 1) := nat.not_dvd_of_pos_of_lt h₄ h₅,
have h₇ : b ≤ b * (b ^ 2 - 1) := nat.le_mul_of_pos_right h₃,
have h₈ : 2 ≤ b * (b ^ 2 - 1) := le_trans b_ge_two h₇,
have h₉ : 2 < p := gt_of_gt_of_ge h₅ h₈,
have h₁₀ := psp_from_prime_gt_p b_ge_two hp₂ h₉,
use psp_from_prime b p,
split,
{ exact psp_from_prime_psp b_ge_two hp₂ h₉ h₆ },
{ exact le_trans (show m ≤ p, by linarith) (le_of_lt h₁₀) } },
-- If `¬2 ≤ b`, then `b = 1`. Since all composite numbers are pseudoprimes to base 1, we can pick
-- any composite number greater than m. We choose `2 * (m + 2)` because it is greater than `m` and
-- is composite for all natural numbers `m`.
{ have h₁ : b = 1 := by linarith,
rw h₁,
use 2 * (m + 2),
have : ¬nat.prime (2 * (m + 2)) := nat.not_prime_mul (by norm_num) (by norm_num),
exact ⟨base_one (by linarith) this, by linarith⟩ }
end
theorem frequently_at_top_fermat_psp {b : ℕ} (h : 1 ≤ b) : ∃ᶠ n in filter.at_top, fermat_psp n b :=
begin
-- Based on the proof of `nat.frequently_at_top_modeq_one`
refine filter.frequently_at_top.2 (λ n, _),
obtain ⟨p, hp⟩ := exists_infinite_pseudoprimes h n,
exact ⟨p, hp.2, hp.1⟩
end
/--
Infinite set variant of `exists_infinite_pseudoprimes`
-/
theorem infinite_set_of_prime_modeq_one {b : ℕ} (h : 1 ≤ b) :
set.infinite {n : ℕ | fermat_psp n b} :=
nat.frequently_at_top_iff_infinite.mp (frequently_at_top_fermat_psp h)
end fermat_psp
|
983bc1e66816b756cfdd77b50c11c3031efd3b33 | 4fa161becb8ce7378a709f5992a594764699e268 | /src/data/mv_polynomial.lean | 622d2dc0fe93ad6262ec28ed147feba90abbb626 | [
"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 | 59,771 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro, Shing Tak Lam
-/
import data.polynomial
import data.equiv.ring
import data.equiv.fin
import tactic.omega
/-!
# Multivariate polynomials
This file defines polynomial rings over a base ring (or even semiring),
with variables from a general type `σ` (which could be infinite).
## Important definitions
Let `R` be a commutative ring (or a semiring) and let `σ` be an arbitrary
type. This file creates the type `mv_polynomial σ R`, which mathematicians
might denote `R[X_i : i ∈ σ]`. It is the type of multivariate
(a.k.a. multivariable) polynomials, with variables
corresponding to the terms in `σ`, and coefficients in `R`.
### Notation
In the definitions below, we use the following notation:
+ `σ : Type*` (indexing the variables)
+ `R : Type*` `[comm_semiring R]` (the coefficients)
+ `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set.
This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s`
+ `a : R`
+ `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `p : mv_polynomial σ R`
### Definitions
* `mv_polynomial σ R` : the type of polynomials with variables of type `σ` and coefficients
in the commutative semiring `R`
* `monomial s a` : the monomial which mathematically would be denoted `a * X^s`
* `C a` : the constant polynomial with value `a`
* `X i` : the degree one monomial corresponding to i; mathematically this might be denoted `Xᵢ`.
* `coeff s p` : the coefficient of `s` in `p`.
* `eval₂ (f : R → S) (g : σ → S) p` : given a semiring homomorphism from `R` to another
semiring `S`, and a map `σ → S`, evaluates `p` at this valuation, returning a term of type `S`.
Note that `eval₂` can be made using `eval` and `map` (see below), and it has been suggested
that sticking to `eval` and `map` might make the code less brittle.
* `eval (g : σ → R) p` : given a map `σ → R`, evaluates `p` at this valuation,
returning a term of type `R`
* `map (f : R → S) p` : returns the multivariate polynomial obtained from `p` by the change of
coefficient semiring corresponding to `f`
* `degrees p` : the multiset of variables representing the union of the multisets corresponding
to each non-zero monomial in `p`. For example if `7 ≠ 0` in `R` and `p = x²y+7y³` then
`degrees p = {x, x, y, y, y}`
* `vars p` : the finset of variables occurring in `p`. For example if `p = x⁴y+yz` then
`vars p = {x, y, z}`
* `degree_of n p : ℕ` -- the total degree of `p` with respect to the variable `n`. For example
if `p = x⁴y+yz` then `degree_of y p = 1`.
* `total_degree p : ℕ` -- the max of the sizes of the multisets `s` whose monomials `X^s` occur
in `p`. For example if `p = x⁴y+yz` then `total_degree p = 5`.
* `pderivative i p` : the partial derivative of `p` with respect to `i`.
## Implementation notes
Recall that if `Y` has a zero, then `X →₀ Y` is the type of functions from `X` to `Y` with finite
support, i.e. such that only finitely many elements of `X` get sent to non-zero terms in `Y`.
The definition of `mv_polynomial σ α` is `(σ →₀ ℕ) →₀ α` ; here `σ →₀ ℕ` denotes the space of all
monomials in the variables, and the function to `α` sends a monomial to its coefficient in
the polynomial being represented.
## Tags
polynomial, multivariate polynomial, multivariable polynomial
-/
noncomputable theory
open_locale classical big_operators
open set function finsupp add_monoid_algebra
open_locale big_operators
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x}
/-- Multivariate polynomial, where `σ` is the index set of the variables and
`α` is the coefficient ring -/
def mv_polynomial (σ : Type*) (α : Type*) [comm_semiring α] := add_monoid_algebra α (σ →₀ ℕ)
namespace mv_polynomial
variables {σ : Type*} {a a' a₁ a₂ : α} {e : ℕ} {n m : σ} {s : σ →₀ ℕ}
section comm_semiring
variables [comm_semiring α] {p q : mv_polynomial σ α}
instance decidable_eq_mv_polynomial [decidable_eq σ] [decidable_eq α] :
decidable_eq (mv_polynomial σ α) := finsupp.decidable_eq
instance : comm_semiring (mv_polynomial σ α) := add_monoid_algebra.comm_semiring
instance : inhabited (mv_polynomial σ α) := ⟨0⟩
instance : has_scalar α (mv_polynomial σ α) := add_monoid_algebra.has_scalar
instance : semimodule α (mv_polynomial σ α) := add_monoid_algebra.semimodule
instance : algebra α (mv_polynomial σ α) := add_monoid_algebra.algebra
/-- the coercion turning an `mv_polynomial` into the function which reports the coefficient of a given monomial -/
def coeff_coe_to_fun : has_coe_to_fun (mv_polynomial σ α) :=
finsupp.has_coe_to_fun
local attribute [instance] coeff_coe_to_fun
/-- `monomial s a` is the monomial `a * X^s` -/
def monomial (s : σ →₀ ℕ) (a : α) : mv_polynomial σ α := single s a
/-- `C a` is the constant polynomial with value `a` -/
def C (a : α) : mv_polynomial σ α := monomial 0 a
/-- `X n` is the degree `1` monomial `1*n` -/
def X (n : σ) : mv_polynomial σ α := monomial (single n 1) 1
@[simp] lemma C_0 : C 0 = (0 : mv_polynomial σ α) := by simp [C, monomial]; refl
@[simp] lemma C_1 : C 1 = (1 : mv_polynomial σ α) := rfl
lemma C_mul_monomial : C a * monomial s a' = monomial s (a * a') :=
by simp [C, monomial, single_mul_single]
@[simp] lemma C_add : (C (a + a') : mv_polynomial σ α) = C a + C a' := single_add
@[simp] lemma C_mul : (C (a * a') : mv_polynomial σ α) = C a * C a' := C_mul_monomial.symm
@[simp] lemma C_pow (a : α) (n : ℕ) : (C (a^n) : mv_polynomial σ α) = (C a)^n :=
by induction n; simp [pow_succ, *]
instance : is_semiring_hom (C : α → mv_polynomial σ α) :=
{ map_zero := C_0,
map_one := C_1,
map_add := λ a a', C_add,
map_mul := λ a a', C_mul }
lemma C_injective (σ : Type*) (R : Type*) [comm_ring R] :
function.injective (C : R → mv_polynomial σ R) :=
finsupp.single_injective _
@[simp] lemma C_inj {σ : Type*} (R : Type*) [comm_ring R] (r s : R) :
(C r : mv_polynomial σ R) = C s ↔ r = s :=
(C_injective σ R).eq_iff
lemma C_eq_coe_nat (n : ℕ) : (C ↑n : mv_polynomial σ α) = n :=
by induction n; simp [nat.succ_eq_add_one, *]
lemma X_pow_eq_single : X n ^ e = monomial (single n e) (1 : α) :=
begin
induction e,
{ simp [X], refl },
{ simp [pow_succ, e_ih],
simp [X, monomial, single_mul_single, nat.succ_eq_add_one, add_comm] }
end
lemma monomial_add_single : monomial (s + single n e) a = (monomial s a * X n ^ e) :=
by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp
lemma monomial_single_add : monomial (single n e + s) a = (X n ^ e * monomial s a) :=
by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp
lemma single_eq_C_mul_X {s : σ} {a : α} {n : ℕ} :
monomial (single s n) a = C a * (X s)^n :=
by rw [← zero_add (single s n), monomial_add_single, C]
@[simp] lemma monomial_add {s : σ →₀ ℕ} {a b : α} :
monomial s a + monomial s b = monomial s (a + b) :=
by simp [monomial]
@[simp] lemma monomial_mul {s s' : σ →₀ ℕ} {a b : α} :
monomial s a * monomial s' b = monomial (s + s') (a * b) :=
by rw [monomial, monomial, monomial, add_monoid_algebra.single_mul_single]
@[simp] lemma monomial_zero {s : σ →₀ ℕ}: monomial s (0 : α) = 0 :=
by rw [monomial, single_zero]; refl
@[simp] lemma sum_monomial {A : Type*} [add_comm_monoid A]
{u : σ →₀ ℕ} {r : α} {b : (σ →₀ ℕ) → α → A} (w : b u 0 = 0) :
sum (monomial u r) b = b u r :=
sum_single_index w
lemma monomial_eq : monomial s a = C a * (s.prod $ λn e, X n ^ e : mv_polynomial σ α) :=
begin
apply @finsupp.induction σ ℕ _ _ s,
{ simp only [C, prod_zero_index]; exact (mul_one _).symm },
{ assume n e s hns he ih,
rw [monomial_single_add, ih, prod_add_index, prod_single_index, mul_left_comm],
{ simp only [pow_zero], },
{ intro a, simp only [pow_zero], },
{ intros, rw pow_add, }, }
end
@[recursor 5]
lemma induction_on {M : mv_polynomial σ α → Prop} (p : mv_polynomial σ α)
(h_C : ∀a, M (C a)) (h_add : ∀p q, M p → M q → M (p + q)) (h_X : ∀p n, M p → M (p * X n)) :
M p :=
have ∀s a, M (monomial s a),
begin
assume s a,
apply @finsupp.induction σ ℕ _ _ s,
{ show M (monomial 0 a), from h_C a, },
{ assume n e p hpn he ih,
have : ∀e:ℕ, M (monomial p a * X n ^ e),
{ intro e,
induction e,
{ simp [ih] },
{ simp [ih, pow_succ', (mul_assoc _ _ _).symm, h_X, e_ih] } },
simp [add_comm, monomial_add_single, this] }
end,
finsupp.induction p
(by have : M (C 0) := h_C 0; rwa [C_0] at this)
(assume s a p hsp ha hp, h_add _ _ (this s a) hp)
theorem induction_on' {P : mv_polynomial σ α → Prop} (p : mv_polynomial σ α)
(h1 : ∀ (u : σ →₀ ℕ) (a : α), P (monomial u a))
(h2 : ∀ (p q : mv_polynomial σ α), P p → P q → P (p + q)) : P p :=
finsupp.induction p (suffices P (monomial 0 0), by rwa monomial_zero at this,
show P (monomial 0 0), from h1 0 0)
(λ a b f ha hb hPf, h2 _ _ (h1 _ _) hPf)
lemma hom_eq_hom [semiring γ]
(f g : mv_polynomial σ α → γ) (hf : is_semiring_hom f) (hg : is_semiring_hom g)
(hC : ∀a:α, f (C a) = g (C a)) (hX : ∀n:σ, f (X n) = g (X n)) (p : mv_polynomial σ α) :
f p = g p :=
mv_polynomial.induction_on p hC
begin assume p q hp hq, rw [is_semiring_hom.map_add f, is_semiring_hom.map_add g, hp, hq] end
begin assume p n hp, rw [is_semiring_hom.map_mul f, is_semiring_hom.map_mul g, hp, hX] end
lemma is_id (f : mv_polynomial σ α → mv_polynomial σ α) (hf : is_semiring_hom f)
(hC : ∀a:α, f (C a) = (C a)) (hX : ∀n:σ, f (X n) = (X n)) (p : mv_polynomial σ α) :
f p = p :=
hom_eq_hom f id hf is_semiring_hom.id hC hX p
section coeff
section
-- While setting up `coeff`, we make `mv_polynomial` reducible so we can treat it as a function.
local attribute [reducible] mv_polynomial
/-- The coefficient of the monomial `m` in the multi-variable polynomial `p`. -/
def coeff (m : σ →₀ ℕ) (p : mv_polynomial σ α) : α := p m
end
lemma ext (p q : mv_polynomial σ α) :
(∀ m, coeff m p = coeff m q) → p = q := ext
lemma ext_iff (p q : mv_polynomial σ α) :
p = q ↔ (∀ m, coeff m p = coeff m q) :=
⟨ λ h m, by rw h, ext p q⟩
@[simp] lemma coeff_add (m : σ →₀ ℕ) (p q : mv_polynomial σ α) :
coeff m (p + q) = coeff m p + coeff m q := add_apply
@[simp] lemma coeff_zero (m : σ →₀ ℕ) :
coeff m (0 : mv_polynomial σ α) = 0 := rfl
@[simp] lemma coeff_zero_X (i : σ) : coeff 0 (X i : mv_polynomial σ α) = 0 :=
single_eq_of_ne (λ h, by cases single_eq_zero.1 h)
instance coeff.is_add_monoid_hom (m : σ →₀ ℕ) :
is_add_monoid_hom (coeff m : mv_polynomial σ α → α) :=
{ map_add := coeff_add m,
map_zero := coeff_zero m }
lemma coeff_sum {X : Type*} (s : finset X) (f : X → mv_polynomial σ α) (m : σ →₀ ℕ) :
coeff m (∑ x in s, f x) = ∑ x in s, coeff m (f x) :=
(s.sum_hom _).symm
lemma monic_monomial_eq (m) : monomial m (1:α) = (m.prod $ λn e, X n ^ e : mv_polynomial σ α) :=
by simp [monomial_eq]
@[simp] lemma coeff_monomial (m n) (a) :
coeff m (monomial n a : mv_polynomial σ α) = if n = m then a else 0 :=
by convert single_apply
@[simp] lemma coeff_C (m) (a) :
coeff m (C a : mv_polynomial σ α) = if 0 = m then a else 0 :=
by convert single_apply
lemma coeff_X_pow (i : σ) (m) (k : ℕ) :
coeff m (X i ^ k : mv_polynomial σ α) = if single i k = m then 1 else 0 :=
begin
have := coeff_monomial m (finsupp.single i k) (1:α),
rwa [@monomial_eq _ _ (1:α) (finsupp.single i k) _,
C_1, one_mul, finsupp.prod_single_index] at this,
exact pow_zero _
end
lemma coeff_X' (i : σ) (m) :
coeff m (X i : mv_polynomial σ α) = if single i 1 = m then 1 else 0 :=
by rw [← coeff_X_pow, pow_one]
@[simp] lemma coeff_X (i : σ) :
coeff (single i 1) (X i : mv_polynomial σ α) = 1 :=
by rw [coeff_X', if_pos rfl]
@[simp] lemma coeff_C_mul (m) (a : α) (p : mv_polynomial σ α) : coeff m (C a * p) = a * coeff m p :=
begin
rw [mul_def, C, monomial],
rw sum_single_index,
{ simp only [zero_add],
convert sum_apply,
simp only [single_apply, finsupp.sum],
rw finset.sum_eq_single m,
{ rw if_pos rfl, refl },
{ intros m' hm' H, apply if_neg, exact H },
{ intros hm, rw if_pos rfl, rw not_mem_support_iff at hm, simp [hm] } },
simp only [zero_mul, single_zero, zero_add],
exact sum_zero, -- TODO doesn't work if we put this inside the simp
end
lemma coeff_mul (p q : mv_polynomial σ α) (n : σ →₀ ℕ) :
coeff n (p * q) = ∑ x in (antidiagonal n).support, coeff x.1 p * coeff x.2 q :=
begin
rw mul_def,
have := @finset.sum_sigma (σ →₀ ℕ) α _ _ p.support (λ _, q.support)
(λ x, if (x.1 + x.2 = n) then coeff x.1 p * coeff x.2 q else 0),
convert this.symm using 1; clear this,
{ rw [coeff],
repeat {rw sum_apply, apply finset.sum_congr rfl, intros, dsimp only},
convert single_apply },
{ have : (antidiagonal n).support.filter (λ x, x.1 ∈ p.support ∧ x.2 ∈ q.support) ⊆
(antidiagonal n).support := finset.filter_subset _,
rw [← finset.sum_sdiff this, finset.sum_eq_zero, zero_add], swap,
{ intros x hx,
rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter),
not_and, not_and, not_mem_support_iff] at hx,
by_cases H : x.1 ∈ p.support,
{ rw [coeff, coeff, hx.2 hx.1 H, mul_zero] },
{ rw not_mem_support_iff at H, rw [coeff, H, zero_mul] } },
symmetry,
rw [← finset.sum_sdiff (finset.filter_subset _), finset.sum_eq_zero, zero_add], swap,
{ intros x hx,
rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter), not_and] at hx,
simp only [if_neg (hx.2 hx.1)] },
{ apply finset.sum_bij, swap 5,
{ intros x hx, exact (x.1, x.2) },
{ intros x hx, rw [finset.mem_filter, finset.mem_sigma] at hx,
simpa [finset.mem_filter, mem_antidiagonal_support] using hx.symm },
{ intros x hx, rw finset.mem_filter at hx, simp only [if_pos hx.2], },
{ rintros ⟨i,j⟩ ⟨k,l⟩ hij hkl, simpa using and.intro },
{ rintros ⟨i,j⟩ hij, refine ⟨⟨i,j⟩, _, _⟩, { apply_instance },
{ rw [finset.mem_filter, mem_antidiagonal_support] at hij,
simpa [finset.mem_filter, finset.mem_sigma] using hij.symm },
{ refl } } },
all_goals { apply_instance } }
end
@[simp] lemma coeff_mul_X (m) (s : σ) (p : mv_polynomial σ α) :
coeff (m + single s 1) (p * X s) = coeff m p :=
begin
have : (m, single s 1) ∈ (m + single s 1).antidiagonal.support := mem_antidiagonal_support.2 rfl,
rw [coeff_mul, ← finset.insert_erase this, finset.sum_insert (finset.not_mem_erase _ _),
finset.sum_eq_zero, add_zero, coeff_X, mul_one],
rintros ⟨i,j⟩ hij,
rw [finset.mem_erase, mem_antidiagonal_support] at hij,
by_cases H : single s 1 = j,
{ subst j, simpa using hij },
{ rw [coeff_X', if_neg H, mul_zero] },
end
lemma coeff_mul_X' (m) (s : σ) (p : mv_polynomial σ α) :
coeff m (p * X s) = if s ∈ m.support then coeff (m - single s 1) p else 0 :=
begin
split_ifs with h h,
{ conv_rhs {rw ← coeff_mul_X _ s},
congr' 1, ext t,
by_cases hj : s = t,
{ subst t, simp only [nat_sub_apply, add_apply, single_eq_same],
refine (nat.sub_add_cancel $ nat.pos_of_ne_zero _).symm, rwa mem_support_iff at h },
{ simp [single_eq_of_ne hj] } },
{ delta coeff, rw ← not_mem_support_iff, intro hm, apply h,
have H := support_mul _ _ hm, simp only [finset.mem_bind] at H,
rcases H with ⟨j, hj, i', hi', H⟩,
delta X monomial at hi', rw mem_support_single at hi', cases hi', subst i',
erw finset.mem_singleton at H, subst m,
rw [mem_support_iff, add_apply, single_apply, if_pos rfl],
intro H, rw [_root_.add_eq_zero_iff] at H, exact one_ne_zero H.2 }
end
end coeff
section as_sum
@[simp]
lemma support_sum_monomial_coeff (p : mv_polynomial σ α) : ∑ v in p.support, monomial v (coeff v p) = p :=
finsupp.sum_single p
lemma as_sum (p : mv_polynomial σ α) : p = ∑ v in p.support, monomial v (coeff v p) :=
(support_sum_monomial_coeff p).symm
end as_sum
section eval₂
variables [comm_semiring β]
variables (f : α → β) (g : σ → β)
/-- Evaluate a polynomial `p` given a valuation `g` of all the variables
and a ring hom `f` from the scalar ring to the target -/
def eval₂ (p : mv_polynomial σ α) : β :=
p.sum (λs a, f a * s.prod (λn e, g n ^ e))
lemma eval₂_eq (g : α →+* β) (x : σ → β) (f : mv_polynomial σ α) :
f.eval₂ g x = ∑ d in f.support, g (f.coeff d) * ∏ i in d.support, x i ^ d i :=
rfl
lemma eval₂_eq' [fintype σ] (g : α →+* β) (x : σ → β) (f : mv_polynomial σ α) :
f.eval₂ g x = ∑ d in f.support, g (f.coeff d) * ∏ i, x i ^ d i :=
by { simp only [eval₂_eq, ← finsupp.prod_pow], refl }
@[simp] lemma eval₂_zero : (0 : mv_polynomial σ α).eval₂ f g = 0 :=
finsupp.sum_zero_index
section
variables [is_semiring_hom f]
@[simp] lemma eval₂_add : (p + q).eval₂ f g = p.eval₂ f g + q.eval₂ f g :=
finsupp.sum_add_index
(by simp [is_semiring_hom.map_zero f])
(by simp [add_mul, is_semiring_hom.map_add f])
@[simp] lemma eval₂_monomial : (monomial s a).eval₂ f g = f a * s.prod (λn e, g n ^ e) :=
finsupp.sum_single_index (by simp [is_semiring_hom.map_zero f])
@[simp] lemma eval₂_C (a) : (C a).eval₂ f g = f a :=
by simp [eval₂_monomial, C, prod_zero_index]
@[simp] lemma eval₂_one : (1 : mv_polynomial σ α).eval₂ f g = 1 :=
(eval₂_C _ _ _).trans (is_semiring_hom.map_one f)
@[simp] lemma eval₂_X (n) : (X n).eval₂ f g = g n :=
by simp [eval₂_monomial,
is_semiring_hom.map_one f, X, prod_single_index, pow_one]
lemma eval₂_mul_monomial :
∀{s a}, (p * monomial s a).eval₂ f g = p.eval₂ f g * f a * s.prod (λn e, g n ^ e) :=
begin
apply mv_polynomial.induction_on p,
{ assume a' s a,
simp [C_mul_monomial, eval₂_monomial, is_semiring_hom.map_mul f] },
{ assume p q ih_p ih_q, simp [add_mul, eval₂_add, ih_p, ih_q] },
{ assume p n ih s a,
from calc (p * X n * monomial s a).eval₂ f g = (p * monomial (single n 1 + s) a).eval₂ f g :
by simp [monomial_single_add, -add_comm, pow_one, mul_assoc]
... = (p * monomial (single n 1) 1).eval₂ f g * f a * s.prod (λn e, g n ^ e) :
by simp [ih, prod_single_index, prod_add_index, pow_one, pow_add, mul_assoc, mul_left_comm,
is_semiring_hom.map_one f, -add_comm] }
end
@[simp] lemma eval₂_mul : ∀{p}, (p * q).eval₂ f g = p.eval₂ f g * q.eval₂ f g :=
begin
apply mv_polynomial.induction_on q,
{ simp [C, eval₂_monomial, eval₂_mul_monomial, prod_zero_index] },
{ simp [mul_add, eval₂_add] {contextual := tt} },
{ simp [X, eval₂_monomial, eval₂_mul_monomial, (mul_assoc _ _ _).symm] { contextual := tt} }
end
@[simp] lemma eval₂_pow {p:mv_polynomial σ α} : ∀{n:ℕ}, (p ^ n).eval₂ f g = (p.eval₂ f g)^n
| 0 := eval₂_one _ _
| (n + 1) := by rw [pow_add, pow_one, pow_add, pow_one, eval₂_mul, eval₂_pow]
instance eval₂.is_semiring_hom : is_semiring_hom (eval₂ f g) :=
{ map_zero := eval₂_zero _ _,
map_one := eval₂_one _ _,
map_add := λ p q, eval₂_add _ _,
map_mul := λ p q, eval₂_mul _ _ }
/-- `mv_polynomial.eval₂` as a `ring_hom`. -/
def eval₂_hom (f : α →+* β) (g : σ → β) : mv_polynomial σ α →+* β := ring_hom.of (eval₂ f g)
@[simp] lemma coe_eval₂_hom (f : α →+* β) (g : σ → β) : ⇑(eval₂_hom f g) = eval₂ f g := rfl
end
section
local attribute [instance, priority 10] is_semiring_hom.comp
lemma eval₂_comp_left {γ} [comm_semiring γ]
(k : β → γ) [is_semiring_hom k]
(f : α → β) [is_semiring_hom f] (g : σ → β)
(p) : k (eval₂ f g p) = eval₂ (k ∘ f) (k ∘ g) p :=
by apply mv_polynomial.induction_on p; simp [
eval₂_add, is_semiring_hom.map_add k,
eval₂_mul, is_semiring_hom.map_mul k] {contextual := tt}
end
@[simp] lemma eval₂_eta (p : mv_polynomial σ α) : eval₂ C X p = p :=
by apply mv_polynomial.induction_on p;
simp [eval₂_add, eval₂_mul] {contextual := tt}
lemma eval₂_congr (g₁ g₂ : σ → β)
(h : ∀ {i : σ} {c : σ →₀ ℕ}, i ∈ c.support → coeff c p ≠ 0 → g₁ i = g₂ i) :
p.eval₂ f g₁ = p.eval₂ f g₂ :=
begin
apply finset.sum_congr rfl,
intros c hc, dsimp, congr' 1,
apply finset.prod_congr rfl,
intros i hi, dsimp, congr' 1,
apply h hi,
rwa finsupp.mem_support_iff at hc
end
variables [is_semiring_hom f]
@[simp] lemma eval₂_prod (s : finset γ) (p : γ → mv_polynomial σ α) :
eval₂ f g (∏ x in s, p x) = ∏ x in s, eval₂ f g (p x) :=
(s.prod_hom _).symm
@[simp] lemma eval₂_sum (s : finset γ) (p : γ → mv_polynomial σ α) :
eval₂ f g (∑ x in s, p x) = ∑ x in s, eval₂ f g (p x) :=
(s.sum_hom _).symm
attribute [to_additive] eval₂_prod
lemma eval₂_assoc (q : γ → mv_polynomial σ α) (p : mv_polynomial γ α) :
eval₂ f (λ t, eval₂ f g (q t)) p = eval₂ f g (eval₂ C q p) :=
by { rw eval₂_comp_left (eval₂ f g), congr, funext, simp }
end eval₂
section eval
variables {f : σ → α}
/-- Evaluate a polynomial `p` given a valuation `f` of all the variables -/
def eval (f : σ → α) : mv_polynomial σ α → α := eval₂ id f
lemma eval_eq (x : σ → α) (f : mv_polynomial σ α) :
f.eval x = ∑ d in f.support, f.coeff d * ∏ i in d.support, x i ^ d i :=
rfl
lemma eval_eq' [fintype σ] (x : σ → α) (f : mv_polynomial σ α) :
f.eval x = ∑ d in f.support, f.coeff d * ∏ i, x i ^ d i :=
eval₂_eq' (ring_hom.id α) x f
@[simp] lemma eval_zero : (0 : mv_polynomial σ α).eval f = 0 := eval₂_zero _ _
@[simp] lemma eval_one : (1 : mv_polynomial σ α).eval f = 1 := eval₂_one _ _
@[simp] lemma eval_add : (p + q).eval f = p.eval f + q.eval f := eval₂_add _ _
lemma eval_monomial : (monomial s a).eval f = a * s.prod (λn e, f n ^ e) :=
eval₂_monomial _ _
@[simp] lemma eval_C : ∀ a, (C a).eval f = a := eval₂_C _ _
@[simp] lemma eval_X : ∀ n, (X n).eval f = f n := eval₂_X _ _
@[simp] lemma eval_mul : (p * q).eval f = p.eval f * q.eval f := eval₂_mul _ _
@[simp] lemma eval_pow (n : ℕ) : (p ^ n).eval f = (p.eval f) ^ n := eval₂_pow _ _
instance eval.is_semiring_hom : is_semiring_hom (eval f) :=
eval₂.is_semiring_hom _ _
lemma eval_sum {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) (g : σ → α) :
eval g (∑ i in s, f i) = ∑ i in s, eval g (f i) :=
eval₂_sum (ring_hom.id α) g s f
@[to_additive]
lemma eval_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) (g : σ → α) :
eval g (∏ i in s, f i) = ∏ i in s, eval g (f i) :=
eval₂_prod (ring_hom.id α) g s f
theorem eval_assoc {τ}
(f : σ → mv_polynomial τ α) (g : τ → α)
(p : mv_polynomial σ α) :
p.eval (eval g ∘ f) = (eval₂ C f p).eval g :=
begin
rw eval₂_comp_left (eval g),
unfold eval, congr; funext a; simp
end
end eval
section map
variables [comm_semiring β]
variables (f : α → β)
/-- `map f p` maps a polynomial `p` across a ring hom `f` -/
def map : mv_polynomial σ α → mv_polynomial σ β := eval₂ (C ∘ f) X
variables [is_semiring_hom f]
instance is_semiring_hom_C_f :
is_semiring_hom ((C : β → mv_polynomial σ β) ∘ f) :=
is_semiring_hom.comp _ _
@[simp] theorem map_monomial (s : σ →₀ ℕ) (a : α) : map f (monomial s a) = monomial s (f a) :=
(eval₂_monomial _ _).trans monomial_eq.symm
@[simp] theorem map_C : ∀ (a : α), map f (C a : mv_polynomial σ α) = C (f a) := map_monomial _ _
@[simp] theorem map_X : ∀ (n : σ), map f (X n : mv_polynomial σ α) = X n := eval₂_X _ _
@[simp] theorem map_one : map f (1 : mv_polynomial σ α) = 1 := eval₂_one _ _
@[simp] theorem map_add (p q : mv_polynomial σ α) :
map f (p + q) = map f p + map f q := eval₂_add _ _
@[simp] theorem map_mul (p q : mv_polynomial σ α) :
map f (p * q) = map f p * map f q := eval₂_mul _ _
@[simp] lemma map_pow (p : mv_polynomial σ α) (n : ℕ) :
map f (p^n) = (map f p)^n := eval₂_pow _ _
instance map.is_semiring_hom :
is_semiring_hom (map f : mv_polynomial σ α → mv_polynomial σ β) :=
eval₂.is_semiring_hom _ _
theorem map_id : ∀ (p : mv_polynomial σ α), map id p = p := eval₂_eta
theorem map_map [comm_semiring γ]
(g : β → γ) [is_semiring_hom g]
(p : mv_polynomial σ α) :
map g (map f p) = map (g ∘ f) p :=
(eval₂_comp_left (map g) (C ∘ f) X p).trans $
by congr; funext a; simp
theorem eval₂_eq_eval_map (g : σ → β) (p : mv_polynomial σ α) :
p.eval₂ f g = (map f p).eval g :=
begin
unfold map eval,
rw eval₂_comp_left (eval₂ id g),
congr; funext a; simp
end
lemma eval₂_comp_right {γ} [comm_semiring γ]
(k : β → γ) [is_semiring_hom k]
(f : α → β) [is_semiring_hom f] (g : σ → β)
(p) : k (eval₂ f g p) = eval₂ k (k ∘ g) (map f p) :=
begin
apply mv_polynomial.induction_on p,
{ intro r, rw [eval₂_C, map_C, eval₂_C] },
{ intros p q hp hq, rw [eval₂_add, is_semiring_hom.map_add k, map_add, eval₂_add, hp, hq] },
{ intros p s hp,
rw [eval₂_mul, is_semiring_hom.map_mul k, map_mul, eval₂_mul, map_X, hp, eval₂_X, eval₂_X] }
end
lemma map_eval₂ (f : α → β) [is_semiring_hom f] (g : γ → mv_polynomial δ α) (p : mv_polynomial γ α) :
map f (eval₂ C g p) = eval₂ C (map f ∘ g) (map f p) :=
begin
apply mv_polynomial.induction_on p,
{ intro r, rw [eval₂_C, map_C, map_C, eval₂_C] },
{ intros p q hp hq, rw [eval₂_add, map_add, hp, hq, map_add, eval₂_add] },
{ intros p s hp,
rw [eval₂_mul, map_mul, hp, map_mul, map_X, eval₂_mul, eval₂_X, eval₂_X] }
end
lemma coeff_map (p : mv_polynomial σ α) : ∀ (m : σ →₀ ℕ), coeff m (p.map f) = f (coeff m p) :=
begin
apply mv_polynomial.induction_on p; clear p,
{ intros r m, rw [map_C], simp only [coeff_C], split_ifs, {refl}, rw is_semiring_hom.map_zero f },
{ intros p q hp hq m, simp only [hp, hq, map_add, coeff_add], rw is_semiring_hom.map_add f },
{ intros p i hp m, simp only [hp, map_mul, map_X],
simp only [hp, mem_support_iff, coeff_mul_X'],
split_ifs, {refl},
rw is_semiring_hom.map_zero f }
end
lemma map_injective (hf : function.injective f) :
function.injective (map f : mv_polynomial σ α → mv_polynomial σ β) :=
begin
intros p q h,
simp only [ext_iff, coeff_map] at h ⊢,
intro m,
exact hf (h m),
end
end map
section degrees
section comm_semiring
/--
The maximal degrees of each variable in a multi-variable polynomial, expressed as a multiset.
(For example, `degrees (x^2 * y + y^3)` would be `{x, x, y, y, y}`.)
-/
def degrees (p : mv_polynomial σ α) : multiset σ :=
p.support.sup (λs:σ →₀ ℕ, s.to_multiset)
lemma degrees_monomial (s : σ →₀ ℕ) (a : α) : degrees (monomial s a) ≤ s.to_multiset :=
finset.sup_le $ assume t h,
begin
have := finsupp.support_single_subset h,
rw [finset.mem_singleton] at this,
rw this
end
lemma degrees_monomial_eq (s : σ →₀ ℕ) (a : α) (ha : a ≠ 0) :
degrees (monomial s a) = s.to_multiset :=
le_antisymm (degrees_monomial s a) $ finset.le_sup $
by rw [monomial, finsupp.support_single_ne_zero ha, finset.mem_singleton]
lemma degrees_C (a : α) : degrees (C a : mv_polynomial σ α) = 0 :=
multiset.le_zero.1 $ degrees_monomial _ _
lemma degrees_X (n : σ) : degrees (X n : mv_polynomial σ α) ≤ {n} :=
le_trans (degrees_monomial _ _) $ le_of_eq $ to_multiset_single _ _
lemma degrees_zero : degrees (0 : mv_polynomial σ α) = 0 :=
by { rw ← C_0, exact degrees_C 0 }
lemma degrees_one : degrees (1 : mv_polynomial σ α) = 0 := degrees_C 1
lemma degrees_add (p q : mv_polynomial σ α) : (p + q).degrees ≤ p.degrees ⊔ q.degrees :=
begin
refine finset.sup_le (assume b hb, _),
have := finsupp.support_add hb, rw finset.mem_union at this,
cases this,
{ exact le_sup_left_of_le (finset.le_sup this) },
{ exact le_sup_right_of_le (finset.le_sup this) },
end
lemma degrees_sum {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) :
(∑ i in s, f i).degrees ≤ s.sup (λi, (f i).degrees) :=
begin
refine s.induction _ _,
{ simp only [finset.sum_empty, finset.sup_empty, degrees_zero], exact le_refl _ },
{ assume i s his ih,
rw [finset.sup_insert, finset.sum_insert his],
exact le_trans (degrees_add _ _) (sup_le_sup_left ih _) }
end
lemma degrees_mul (p q : mv_polynomial σ α) : (p * q).degrees ≤ p.degrees + q.degrees :=
begin
refine finset.sup_le (assume b hb, _),
have := support_mul p q hb,
simp only [finset.mem_bind, finset.mem_singleton] at this,
rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩,
rw [finsupp.to_multiset_add],
exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂)
end
lemma degrees_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) :
(∏ i in s, f i).degrees ≤ ∑ i in s, (f i).degrees :=
begin
refine s.induction _ _,
{ simp only [finset.prod_empty, finset.sum_empty, degrees_one] },
{ assume i s his ih,
rw [finset.prod_insert his, finset.sum_insert his],
exact le_trans (degrees_mul _ _) (add_le_add_left ih _) }
end
lemma degrees_pow (p : mv_polynomial σ α) :
∀(n : ℕ), (p^n).degrees ≤ n •ℕ p.degrees
| 0 := begin rw [pow_zero, degrees_one], exact multiset.zero_le _ end
| (n + 1) := le_trans (degrees_mul _ _) (add_le_add_left (degrees_pow n) _)
end comm_semiring
end degrees
section vars
/-- `vars p` is the set of variables appearing in the polynomial `p` -/
def vars (p : mv_polynomial σ α) : finset σ := p.degrees.to_finset
@[simp] lemma vars_0 : (0 : mv_polynomial σ α).vars = ∅ :=
by rw [vars, degrees_zero, multiset.to_finset_zero]
@[simp] lemma vars_monomial (h : a ≠ 0) : (monomial s a).vars = s.support :=
by rw [vars, degrees_monomial_eq _ _ h, finsupp.to_finset_to_multiset]
@[simp] lemma vars_C : (C a : mv_polynomial σ α).vars = ∅ :=
by rw [vars, degrees_C, multiset.to_finset_zero]
@[simp] lemma vars_X (h : 0 ≠ (1 : α)) : (X n : mv_polynomial σ α).vars = {n} :=
by rw [X, vars_monomial h.symm, finsupp.support_single_ne_zero (one_ne_zero : 1 ≠ 0)]
lemma mem_support_not_mem_vars_zero {f : mv_polynomial σ α} {x : σ →₀ ℕ} (H : x ∈ f.support) {v : σ} (h : v ∉ vars f) :
x v = 0 :=
begin
rw [vars, multiset.mem_to_finset] at h,
rw ←not_mem_support_iff,
contrapose! h,
unfold degrees,
rw (show f.support = insert x f.support, from eq.symm $ finset.insert_eq_of_mem H),
rw finset.sup_insert,
simp only [multiset.mem_union, multiset.sup_eq_union],
left,
rwa [←to_finset_to_multiset, multiset.mem_to_finset] at h,
end
end vars
section degree_of
/-- `degree_of n p` gives the highest power of X_n that appears in `p` -/
def degree_of (n : σ) (p : mv_polynomial σ α) : ℕ := p.degrees.count n
end degree_of
section total_degree
/-- `total_degree p` gives the maximum |s| over the monomials X^s in `p` -/
def total_degree (p : mv_polynomial σ α) : ℕ := p.support.sup (λs, s.sum $ λn e, e)
lemma total_degree_eq (p : mv_polynomial σ α) :
p.total_degree = p.support.sup (λm, m.to_multiset.card) :=
begin
rw [total_degree],
congr, funext m,
exact (finsupp.card_to_multiset _).symm
end
lemma total_degree_le_degrees_card (p : mv_polynomial σ α) :
p.total_degree ≤ p.degrees.card :=
begin
rw [total_degree_eq],
exact finset.sup_le (assume s hs, multiset.card_le_of_le $ finset.le_sup hs)
end
@[simp] lemma total_degree_C (a : α) : (C a : mv_polynomial σ α).total_degree = 0 :=
nat.eq_zero_of_le_zero $ finset.sup_le $ assume n hn,
have _ := finsupp.support_single_subset hn,
begin
rw [finset.mem_singleton] at this,
subst this,
exact le_refl _
end
@[simp] lemma total_degree_zero : (0 : mv_polynomial σ α).total_degree = 0 :=
by rw [← C_0]; exact total_degree_C (0 : α)
@[simp] lemma total_degree_one : (1 : mv_polynomial σ α).total_degree = 0 :=
total_degree_C (1 : α)
@[simp] lemma total_degree_X {α} [comm_semiring α] [nonzero α] (s : σ) :
(X s : mv_polynomial σ α).total_degree = 1 :=
begin
rw [total_degree, X, monomial, finsupp.support_single_ne_zero (one_ne_zero : (1 : α) ≠ 0)],
simp only [finset.sup, sum_single_index, finset.fold_singleton, sup_bot_eq],
end
lemma total_degree_add (a b : mv_polynomial σ α) :
(a + b).total_degree ≤ max a.total_degree b.total_degree :=
finset.sup_le $ assume n hn,
have _ := finsupp.support_add hn,
begin
rw finset.mem_union at this,
cases this,
{ exact le_max_left_of_le (finset.le_sup this) },
{ exact le_max_right_of_le (finset.le_sup this) }
end
lemma total_degree_mul (a b : mv_polynomial σ α) :
(a * b).total_degree ≤ a.total_degree + b.total_degree :=
finset.sup_le $ assume n hn,
have _ := add_monoid_algebra.support_mul a b hn,
begin
simp only [finset.mem_bind, finset.mem_singleton] at this,
rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩,
rw [finsupp.sum_add_index],
{ exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) },
{ assume a, refl },
{ assume a b₁ b₂, refl }
end
lemma total_degree_pow (a : mv_polynomial σ α) (n : ℕ) :
(a ^ n).total_degree ≤ n * a.total_degree :=
begin
induction n with n ih,
{ simp only [nat.nat_zero_eq_zero, zero_mul, pow_zero, total_degree_one] },
rw pow_succ,
calc total_degree (a * a ^ n) ≤ a.total_degree + (a^n).total_degree : total_degree_mul _ _
... ≤ a.total_degree + n * a.total_degree : add_le_add_left ih _
... = (n+1) * a.total_degree : by rw [add_mul, one_mul, add_comm]
end
lemma total_degree_list_prod :
∀(s : list (mv_polynomial σ α)), s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum
| [] := by rw [@list.prod_nil (mv_polynomial σ α) _, total_degree_one]; refl
| (p :: ps) :=
begin
rw [@list.prod_cons (mv_polynomial σ α) _, list.map, list.sum_cons],
exact le_trans (total_degree_mul _ _) (add_le_add_left (total_degree_list_prod ps) _)
end
lemma total_degree_multiset_prod (s : multiset (mv_polynomial σ α)) :
s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum :=
begin
refine quotient.induction_on s (assume l, _),
rw [multiset.quot_mk_to_coe, multiset.coe_prod, multiset.coe_map, multiset.coe_sum],
exact total_degree_list_prod l
end
lemma total_degree_finset_prod {ι : Type*}
(s : finset ι) (f : ι → mv_polynomial σ α) :
(s.prod f).total_degree ≤ ∑ i in s, (f i).total_degree :=
begin
refine le_trans (total_degree_multiset_prod _) _,
rw [multiset.map_map],
refl
end
lemma exists_degree_lt [fintype σ] (f : mv_polynomial σ α) (n : ℕ)
(h : f.total_degree < n * fintype.card σ) {d : σ →₀ ℕ} (hd : d ∈ f.support) :
∃ i, d i < n :=
begin
contrapose! h,
calc n * fintype.card σ
= ∑ s:σ, n : by rw [finset.sum_const, nat.nsmul_eq_mul, mul_comm, finset.card_univ]
... ≤ ∑ s, d s : finset.sum_le_sum (λ s _, h s)
... ≤ d.sum (λ i e, e) : by { rw [finsupp.sum_fintype], intros, refl }
... ≤ f.total_degree : finset.le_sup hd,
end
end total_degree
end comm_semiring
section comm_ring
variable [comm_ring α]
variables {p q : mv_polynomial σ α}
instance : comm_ring (mv_polynomial σ α) := add_monoid_algebra.comm_ring
instance C.is_ring_hom : is_ring_hom (C : α → mv_polynomial σ α) :=
by apply is_ring_hom.of_semiring
variables (σ a a')
@[simp] lemma C_sub : (C (a - a') : mv_polynomial σ α) = C a - C a' := is_ring_hom.map_sub _
@[simp] lemma C_neg : (C (-a) : mv_polynomial σ α) = -C a := is_ring_hom.map_neg _
@[simp] lemma coeff_neg (m : σ →₀ ℕ) (p : mv_polynomial σ α) :
coeff m (-p) = -coeff m p := finsupp.neg_apply
@[simp] lemma coeff_sub (m : σ →₀ ℕ) (p q : mv_polynomial σ α) :
coeff m (p - q) = coeff m p - coeff m q := finsupp.sub_apply
instance coeff.is_add_group_hom (m : σ →₀ ℕ) :
is_add_group_hom (coeff m : mv_polynomial σ α → α) :=
{ map_add := coeff_add m }
variables {σ} (p)
theorem C_mul' : mv_polynomial.C a * p = a • p :=
begin
apply finsupp.induction p,
{ exact (mul_zero $ mv_polynomial.C a).trans (@smul_zero α (mv_polynomial σ α) _ _ _ a).symm },
intros p b f haf hb0 ih,
rw [mul_add, ih, @smul_add α (mv_polynomial σ α) _ _ _ a], congr' 1,
rw [add_monoid_algebra.mul_def, finsupp.smul_single, mv_polynomial.C, mv_polynomial.monomial],
rw [finsupp.sum_single_index, finsupp.sum_single_index, zero_add, smul_eq_mul],
{ rw [mul_zero, finsupp.single_zero] },
{ rw finsupp.sum_single_index,
all_goals { rw [zero_mul, finsupp.single_zero] }, }
end
lemma smul_eq_C_mul (p : mv_polynomial σ α) (a : α) : a • p = C a * p :=
begin
rw [← finsupp.sum_single p, @finsupp.smul_sum (σ →₀ ℕ) α α, finsupp.mul_sum],
refine finset.sum_congr rfl (assume n _, _),
simp only [finsupp.smul_single],
exact C_mul_monomial.symm
end
@[simp] lemma smul_eval (x) (p : mv_polynomial σ α) (s) : (s • p).eval x = s * p.eval x :=
by rw [smul_eq_C_mul, eval_mul, eval_C]
section degrees
lemma degrees_neg (p : mv_polynomial σ α) : (- p).degrees = p.degrees :=
by rw [degrees, finsupp.support_neg]; refl
lemma degrees_sub (p q : mv_polynomial σ α) :
(p - q).degrees ≤ p.degrees ⊔ q.degrees :=
le_trans (degrees_add p (-q)) $ by rw [degrees_neg]
end degrees
section eval₂
variables [comm_ring β]
variables (f : α → β) [is_ring_hom f] (g : σ → β)
instance eval₂.is_ring_hom : is_ring_hom (eval₂ f g) :=
by apply is_ring_hom.of_semiring
@[simp] lemma eval₂_sub : (p - q).eval₂ f g = p.eval₂ f g - q.eval₂ f g := is_ring_hom.map_sub _
@[simp] lemma eval₂_neg : (-p).eval₂ f g = -(p.eval₂ f g) := is_ring_hom.map_neg _
lemma hom_C (f : mv_polynomial σ ℤ → β) [is_ring_hom f] (n : ℤ) : f (C n) = (n : β) :=
((ring_hom.of f).comp (ring_hom.of C)).eq_int_cast n
/-- A ring homomorphism f : Z[X_1, X_2, ...] → R
is determined by the evaluations f(X_1), f(X_2), ... -/
@[simp] lemma eval₂_hom_X {α : Type u} (c : ℤ → β) [is_ring_hom c]
(f : mv_polynomial α ℤ → β) [is_ring_hom f] (x : mv_polynomial α ℤ) :
eval₂ c (f ∘ X) x = f x :=
mv_polynomial.induction_on x
(λ n, by { rw [hom_C f, eval₂_C], exact (ring_hom.of c).eq_int_cast n })
(λ p q hp hq, by { rw [eval₂_add, hp, hq], exact (is_ring_hom.map_add f).symm })
(λ p n hp, by { rw [eval₂_mul, eval₂_X, hp], exact (is_ring_hom.map_mul f).symm })
/-- Ring homomorphisms out of integer polynomials on a type `σ` are the same as
functions out of the type `σ`, -/
def hom_equiv : (mv_polynomial σ ℤ →+* β) ≃ (σ → β) :=
{ to_fun := λ f, ⇑f ∘ X,
inv_fun := λ f, eval₂_hom (int.cast_ring_hom β) f,
left_inv := λ f, ring_hom.ext $ eval₂_hom_X _ _,
right_inv := λ f, funext $ λ x, by simp only [coe_eval₂_hom, function.comp_app, eval₂_X] }
end eval₂
section eval
variables (f : σ → α)
instance eval.is_ring_hom : is_ring_hom (eval f) := eval₂.is_ring_hom _ _
@[simp] lemma eval_sub : (p - q).eval f = p.eval f - q.eval f := is_ring_hom.map_sub _
@[simp] lemma eval_neg : (-p).eval f = -(p.eval f) := is_ring_hom.map_neg _
end eval
section map
variables [comm_ring β]
variables (f : α → β) [is_ring_hom f]
instance is_ring_hom_C_f : is_ring_hom ((C : β → mv_polynomial σ β) ∘ f) :=
is_ring_hom.comp _ _
instance map.is_ring_hom : is_ring_hom (map f : mv_polynomial σ α → mv_polynomial σ β) :=
eval₂.is_ring_hom _ _
@[simp] lemma map_sub : (p - q).map f = p.map f - q.map f := is_ring_hom.map_sub _
@[simp] lemma map_neg : (-p).map f = -(p.map f) := is_ring_hom.map_neg _
end map
section total_degree
@[simp] lemma total_degree_neg (a : mv_polynomial σ α) :
(-a).total_degree = a.total_degree :=
by simp only [total_degree, finsupp.support_neg]
lemma total_degree_sub (a b : mv_polynomial σ α) :
(a - b).total_degree ≤ max a.total_degree b.total_degree :=
calc (a - b).total_degree = (a + -b).total_degree : by rw sub_eq_add_neg
... ≤ max a.total_degree (-b).total_degree : total_degree_add a (-b)
... = max a.total_degree b.total_degree : by rw total_degree_neg
end total_degree
section aeval
/-- The algebra of multivariate polynomials. -/
variables (R : Type u) (A : Type v) (f : σ → A)
variables [comm_ring R] [comm_ring A] [algebra R A]
/-- A map `σ → A` where `A` is an algebra over `R` generates an `R`-algebra homomorphism
from multivariate polynomials over `σ` to `A`. -/
def aeval : mv_polynomial σ R →ₐ[R] A :=
{ commutes' := λ r, eval₂_C _ _ _
.. eval₂_hom (algebra_map R A) f }
theorem aeval_def (p : mv_polynomial σ R) : aeval R A f p = eval₂ (algebra_map R A) f p := rfl
@[simp] lemma aeval_X (s : σ) : aeval R A f (X s) = f s := eval₂_X _ _ _
@[simp] lemma aeval_C (r : R) : aeval R A f (C r) = algebra_map R A r := eval₂_C _ _ _
theorem eval_unique (φ : mv_polynomial σ R →ₐ[R] A) :
φ = aeval R A (φ ∘ X) :=
begin
ext p,
apply mv_polynomial.induction_on p,
{ intro r, rw aeval_C, exact φ.commutes r },
{ intros f g ih1 ih2,
rw [φ.map_add, ih1, ih2, alg_hom.map_add] },
{ intros p j ih,
rw [φ.map_mul, alg_hom.map_mul, aeval_X, ih] }
end
end aeval
end comm_ring
section rename
variables {α} [comm_semiring α]
/-- Rename all the variables in a multivariable polynomial. -/
def rename (f : β → γ) : mv_polynomial β α → mv_polynomial γ α :=
eval₂ C (X ∘ f)
instance rename.is_semiring_hom (f : β → γ) :
is_semiring_hom (rename f : mv_polynomial β α → mv_polynomial γ α) :=
by unfold rename; apply_instance
@[simp] lemma rename_C (f : β → γ) (a : α) : rename f (C a) = C a :=
eval₂_C _ _ _
@[simp] lemma rename_X (f : β → γ) (b : β) : rename f (X b : mv_polynomial β α) = X (f b) :=
eval₂_X _ _ _
@[simp] lemma rename_zero (f : β → γ) :
rename f (0 : mv_polynomial β α) = 0 :=
eval₂_zero _ _
@[simp] lemma rename_one (f : β → γ) :
rename f (1 : mv_polynomial β α) = 1 :=
eval₂_one _ _
@[simp] lemma rename_add (f : β → γ) (p q : mv_polynomial β α) :
rename f (p + q) = rename f p + rename f q :=
eval₂_add _ _
@[simp] lemma rename_neg {α} [comm_ring α]
(f : β → γ) (p : mv_polynomial β α) :
rename f (-p) = -rename f p :=
eval₂_neg _ _ _
@[simp] lemma rename_sub {α} [comm_ring α]
(f : β → γ) (p q : mv_polynomial β α) :
rename f (p - q) = rename f p - rename f q :=
eval₂_sub _ _ _
@[simp] lemma rename_mul (f : β → γ) (p q : mv_polynomial β α) :
rename f (p * q) = rename f p * rename f q :=
eval₂_mul _ _
@[simp] lemma rename_pow (f : β → γ) (p : mv_polynomial β α) (n : ℕ) :
rename f (p^n) = (rename f p)^n :=
eval₂_pow _ _
lemma map_rename [comm_semiring β] (f : α → β) [is_semiring_hom f]
(g : γ → δ) (p : mv_polynomial γ α) :
map f (rename g p) = rename g (map f p) :=
mv_polynomial.induction_on p
(λ a, by simp)
(λ p q hp hq, by simp [hp, hq])
(λ p n hp, by simp [hp])
@[simp] lemma rename_rename (f : β → γ) (g : γ → δ) (p : mv_polynomial β α) :
rename g (rename f p) = rename (g ∘ f) p :=
show rename g (eval₂ C (X ∘ f) p) = _,
by simp only [eval₂_comp_left (rename g) C (X ∘ f) p, (∘), rename_C, rename_X]; refl
@[simp] lemma rename_id (p : mv_polynomial β α) : rename id p = p :=
eval₂_eta p
lemma rename_monomial (f : β → γ) (p : β →₀ ℕ) (a : α) :
rename f (monomial p a) = monomial (p.map_domain f) a :=
begin
rw [rename, eval₂_monomial, monomial_eq, finsupp.prod_map_domain_index],
{ exact assume n, pow_zero _ },
{ exact assume n i₁ i₂, pow_add _ _ _ }
end
lemma rename_eq (f : β → γ) (p : mv_polynomial β α) :
rename f p = finsupp.map_domain (finsupp.map_domain f) p :=
begin
simp only [rename, eval₂, finsupp.map_domain],
congr, ext s a : 2,
rw [← monomial, monomial_eq, finsupp.prod_sum_index],
congr, ext n i : 2,
rw [finsupp.prod_single_index],
exact pow_zero _,
exact assume a, pow_zero _,
exact assume a b c, pow_add _ _ _
end
lemma rename_injective (f : β → γ) (hf : function.injective f) :
function.injective (rename f : mv_polynomial β α → mv_polynomial γ α) :=
have (rename f : mv_polynomial β α → mv_polynomial γ α) =
finsupp.map_domain (finsupp.map_domain f) := funext (rename_eq f),
begin
rw this,
exact finsupp.map_domain_injective (finsupp.map_domain_injective hf)
end
lemma total_degree_rename_le (f : β → γ) (p : mv_polynomial β α) :
(p.rename f).total_degree ≤ p.total_degree :=
finset.sup_le $ assume b,
begin
assume h,
rw rename_eq at h,
have h' := finsupp.map_domain_support h,
rw finset.mem_image at h',
rcases h' with ⟨s, hs, rfl⟩,
rw finsupp.sum_map_domain_index,
exact le_trans (le_refl _) (finset.le_sup hs),
exact assume _, rfl,
exact assume _ _ _, rfl
end
section
variables [comm_semiring β] (f : α → β) [is_semiring_hom f]
variables (k : γ → δ) (g : δ → β) (p : mv_polynomial γ α)
lemma eval₂_rename : (p.rename k).eval₂ f g = p.eval₂ f (g ∘ k) :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma rename_eval₂ (g : δ → mv_polynomial γ α) :
(p.eval₂ C (g ∘ k)).rename k = (p.rename k).eval₂ C (rename k ∘ g) :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma rename_prodmk_eval₂ (d : δ) (g : γ → mv_polynomial γ α) :
(p.eval₂ C g).rename (prod.mk d) = p.eval₂ C (λ x, (g x).rename (prod.mk d)) :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma eval₂_rename_prodmk (g : δ × γ → β) (d : δ) :
(rename (prod.mk d) p).eval₂ f g = eval₂ f (λ i, g (d, i)) p :=
by apply mv_polynomial.induction_on p; { intros, simp [*] }
lemma eval_rename_prodmk (g : δ × γ → α) (d : δ) :
(rename (prod.mk d) p).eval g = eval (λ i, g (d, i)) p :=
eval₂_rename_prodmk id _ _ _
end
/-- Every polynomial is a polynomial in finitely many variables. -/
theorem exists_finset_rename (p : mv_polynomial γ α) :
∃ (s : finset γ) (q : mv_polynomial {x // x ∈ s} α), p = q.rename coe :=
begin
apply induction_on p,
{ intro r, exact ⟨∅, C r, by rw rename_C⟩ },
{ rintro p q ⟨s, p, rfl⟩ ⟨t, q, rfl⟩,
refine ⟨s ∪ t, ⟨_, _⟩⟩,
{ exact rename (subtype.map id (finset.subset_union_left s t)) p +
rename (subtype.map id (finset.subset_union_right s t)) q },
{ rw [rename_add, rename_rename, rename_rename], refl } },
{ rintro p n ⟨s, p, rfl⟩,
refine ⟨insert n s, ⟨_, _⟩⟩,
{ exact rename (subtype.map id (finset.subset_insert n s)) p * X ⟨n, s.mem_insert_self n⟩ },
{ rw [rename_mul, rename_X, rename_rename], refl } }
end
/-- Every polynomial is a polynomial in finitely many variables. -/
theorem exists_fin_rename (p : mv_polynomial γ α) :
∃ (n : ℕ) (f : fin n → γ) (hf : injective f) (q : mv_polynomial (fin n) α), p = q.rename f :=
begin
obtain ⟨s, q, rfl⟩ := exists_finset_rename p,
obtain ⟨n, ⟨e⟩⟩ := fintype.exists_equiv_fin {x // x ∈ s},
refine ⟨n, coe ∘ e.symm, subtype.val_injective.comp e.symm.injective, q.rename e, _⟩,
rw [← rename_rename, rename_rename e],
simp only [function.comp, equiv.symm_apply_apply, rename_rename]
end
end rename
lemma eval₂_cast_comp {β : Type u} {γ : Type v} (f : γ → β)
{α : Type w} [comm_ring α] (c : ℤ → α) [is_ring_hom c] (g : β → α) (x : mv_polynomial γ ℤ) :
eval₂ c (g ∘ f) x = eval₂ c g (rename f x) :=
mv_polynomial.induction_on x
(λ n, by simp only [eval₂_C, rename_C])
(λ p q hp hq, by simp only [hp, hq, rename, eval₂_add])
(λ p n hp, by simp only [hp, rename, eval₂_X, eval₂_mul])
instance rename.is_ring_hom
{α} [comm_ring α] (f : β → γ) :
is_ring_hom (rename f : mv_polynomial β α → mv_polynomial γ α) :=
@is_ring_hom.of_semiring (mv_polynomial β α) (mv_polynomial γ α) _ _ (rename f)
(rename.is_semiring_hom f)
section equiv
variables (α) [comm_semiring α]
/-- The ring isomorphism between multivariable polynomials in no variables and the ground ring. -/
def pempty_ring_equiv : mv_polynomial pempty α ≃+* α :=
{ to_fun := mv_polynomial.eval₂ id $ pempty.elim,
inv_fun := C,
left_inv := is_id _ (by apply_instance) (assume a, by rw [eval₂_C]; refl) (assume a, a.elim),
right_inv := λ r, eval₂_C _ _ _,
map_mul' := λ _ _, eval₂_mul _ _,
map_add' := λ _ _, eval₂_add _ _ }
/--
The ring isomorphism between multivariable polynomials in a single variable and
polynomials over the ground ring.
-/
def punit_ring_equiv : mv_polynomial punit α ≃+* polynomial α :=
{ to_fun := eval₂ polynomial.C (λu:punit, polynomial.X),
inv_fun := polynomial.eval₂ mv_polynomial.C (X punit.star),
left_inv :=
begin
refine is_id _ _ _ _,
apply is_semiring_hom.comp (eval₂ polynomial.C (λu:punit, polynomial.X)) _; apply_instance,
{ assume a, rw [eval₂_C, polynomial.eval₂_C] },
{ rintros ⟨⟩, rw [eval₂_X, polynomial.eval₂_X] }
end,
right_inv := assume p, polynomial.induction_on p
(assume a, by rw [polynomial.eval₂_C, mv_polynomial.eval₂_C])
(assume p q hp hq, by rw [polynomial.eval₂_add, mv_polynomial.eval₂_add, hp, hq])
(assume p n hp,
by rw [polynomial.eval₂_mul, polynomial.eval₂_pow, polynomial.eval₂_X, polynomial.eval₂_C,
eval₂_mul, eval₂_C, eval₂_pow, eval₂_X]),
map_mul' := λ _ _, eval₂_mul _ _,
map_add' := λ _ _, eval₂_add _ _ }
/-- The ring isomorphism between multivariable polynomials induced by an equivalence of the variables. -/
def ring_equiv_of_equiv (e : β ≃ γ) : mv_polynomial β α ≃+* mv_polynomial γ α :=
{ to_fun := rename e,
inv_fun := rename e.symm,
left_inv := λ p, by simp only [rename_rename, (∘), e.symm_apply_apply]; exact rename_id p,
right_inv := λ p, by simp only [rename_rename, (∘), e.apply_symm_apply]; exact rename_id p,
map_mul' := rename_mul e,
map_add' := rename_add e }
/-- The ring isomorphism between multivariable polynomials induced by a ring isomorphism of the ground ring. -/
def ring_equiv_congr [comm_semiring γ] (e : α ≃+* γ) : mv_polynomial β α ≃+* mv_polynomial β γ :=
{ to_fun := map e,
inv_fun := map e.symm,
left_inv := assume p,
have (e.symm ∘ e) = id,
{ ext a, exact e.symm_apply_apply a },
by simp only [map_map, this, map_id],
right_inv := assume p,
have (e ∘ e.symm) = id,
{ ext a, exact e.apply_symm_apply a },
by simp only [map_map, this, map_id],
map_mul' := map_mul _,
map_add' := map_add _ }
section
variables (β γ δ)
/--
The function from multivariable polynomials in a sum of two types,
to multivariable polynomials in one of the types,
with coefficents in multivariable polynomials in the other type.
See `sum_ring_equiv` for the ring isomorphism.
-/
def sum_to_iter : mv_polynomial (β ⊕ γ) α → mv_polynomial β (mv_polynomial γ α) :=
eval₂ (C ∘ C) (λbc, sum.rec_on bc X (C ∘ X))
instance is_semiring_hom_C_C :
is_semiring_hom (C ∘ C : α → mv_polynomial β (mv_polynomial γ α)) :=
@is_semiring_hom.comp _ _ _ _ C mv_polynomial.is_semiring_hom _ _ C mv_polynomial.is_semiring_hom
instance is_semiring_hom_sum_to_iter : is_semiring_hom (sum_to_iter α β γ) :=
eval₂.is_semiring_hom _ _
lemma sum_to_iter_C (a : α) : sum_to_iter α β γ (C a) = C (C a) :=
eval₂_C _ _ a
lemma sum_to_iter_Xl (b : β) : sum_to_iter α β γ (X (sum.inl b)) = X b :=
eval₂_X _ _ (sum.inl b)
lemma sum_to_iter_Xr (c : γ) : sum_to_iter α β γ (X (sum.inr c)) = C (X c) :=
eval₂_X _ _ (sum.inr c)
/--
The function from multivariable polynomials in one type,
with coefficents in multivariable polynomials in another type,
to multivariable polynomials in the sum of the two types.
See `sum_ring_equiv` for the ring isomorphism.
-/
def iter_to_sum : mv_polynomial β (mv_polynomial γ α) → mv_polynomial (β ⊕ γ) α :=
eval₂ (eval₂ C (X ∘ sum.inr)) (X ∘ sum.inl)
instance is_semiring_hom_iter_to_sum : is_semiring_hom (iter_to_sum α β γ) :=
eval₂.is_semiring_hom _ _
lemma iter_to_sum_C_C (a : α) : iter_to_sum α β γ (C (C a)) = C a :=
eq.trans (eval₂_C _ _ (C a)) (eval₂_C _ _ _)
lemma iter_to_sum_X (b : β) : iter_to_sum α β γ (X b) = X (sum.inl b) :=
eval₂_X _ _ _
lemma iter_to_sum_C_X (c : γ) : iter_to_sum α β γ (C (X c)) = X (sum.inr c) :=
eq.trans (eval₂_C _ _ (X c)) (eval₂_X _ _ _)
/-- A helper function for `sum_ring_equiv`. -/
def mv_polynomial_equiv_mv_polynomial [comm_semiring δ]
(f : mv_polynomial β α → mv_polynomial γ δ) (hf : is_semiring_hom f)
(g : mv_polynomial γ δ → mv_polynomial β α) (hg : is_semiring_hom g)
(hfgC : ∀a, f (g (C a)) = C a)
(hfgX : ∀n, f (g (X n)) = X n)
(hgfC : ∀a, g (f (C a)) = C a)
(hgfX : ∀n, g (f (X n)) = X n) :
mv_polynomial β α ≃+* mv_polynomial γ δ :=
{ to_fun := f, inv_fun := g,
left_inv := is_id _ (is_semiring_hom.comp _ _) hgfC hgfX,
right_inv := is_id _ (is_semiring_hom.comp _ _) hfgC hfgX,
map_mul' := hf.map_mul,
map_add' := hf.map_add }
/--
The ring isomorphism between multivariable polynomials in a sum of two types,
and multivariable polynomials in one of the types,
with coefficents in multivariable polynomials in the other type.
-/
def sum_ring_equiv : mv_polynomial (β ⊕ γ) α ≃+* mv_polynomial β (mv_polynomial γ α) :=
begin
apply @mv_polynomial_equiv_mv_polynomial α (β ⊕ γ) _ _ _ _
(sum_to_iter α β γ) _ (iter_to_sum α β γ) _,
{ assume p,
apply hom_eq_hom _ _ _ _ _ _ p,
apply_instance,
{ apply @is_semiring_hom.comp _ _ _ _ _ _ _ _ _ _,
apply_instance,
apply @is_semiring_hom.comp _ _ _ _ _ _ _ _ _ _,
apply_instance,
{ apply @mv_polynomial.is_semiring_hom },
{ apply mv_polynomial.is_semiring_hom_iter_to_sum α β γ },
{ apply mv_polynomial.is_semiring_hom_sum_to_iter α β γ } },
{ apply mv_polynomial.is_semiring_hom },
{ assume a, rw [iter_to_sum_C_C α β γ, sum_to_iter_C α β γ] },
{ assume c, rw [iter_to_sum_C_X α β γ, sum_to_iter_Xr α β γ] } },
{ assume b, rw [iter_to_sum_X α β γ, sum_to_iter_Xl α β γ] },
{ assume a, rw [sum_to_iter_C α β γ, iter_to_sum_C_C α β γ] },
{ assume n, cases n with b c,
{ rw [sum_to_iter_Xl, iter_to_sum_X] },
{ rw [sum_to_iter_Xr, iter_to_sum_C_X] } },
{ apply mv_polynomial.is_semiring_hom_sum_to_iter α β γ },
{ apply mv_polynomial.is_semiring_hom_iter_to_sum α β γ }
end
/--
The ring isomorphism between multivariable polynomials in `option β` and
polynomials with coefficients in `mv_polynomial β α`.
-/
def option_equiv_left : mv_polynomial (option β) α ≃+* polynomial (mv_polynomial β α) :=
(ring_equiv_of_equiv α $ (equiv.option_equiv_sum_punit β).trans (equiv.sum_comm _ _)).trans $
(sum_ring_equiv α _ _).trans $
punit_ring_equiv _
/--
The ring isomorphism between multivariable polynomials in `option β` and
multivariable polynomials with coefficients in polynomials.
-/
def option_equiv_right : mv_polynomial (option β) α ≃+* mv_polynomial β (polynomial α) :=
(ring_equiv_of_equiv α $ equiv.option_equiv_sum_punit.{0} β).trans $
(sum_ring_equiv α β unit).trans $
ring_equiv_congr (mv_polynomial unit α) (punit_ring_equiv α)
/--
The ring isomorphism between multivariable polynomials in `fin (n + 1)` and
polynomials over multivariable polynomials in `fin n`.
-/
def fin_succ_equiv (n : ℕ) :
mv_polynomial (fin (n + 1)) α ≃+* polynomial (mv_polynomial (fin n) α) :=
(ring_equiv_of_equiv α (fin_succ_equiv n)).trans
(option_equiv_left α (fin n))
end
end equiv
/-!
## Partial derivatives
-/
section pderivative
variables {R : Type} [comm_ring R]
variable {S : Type}
/-- `pderivative v p` is the partial derivative of `p` with respect to `v` -/
def pderivative (v : S) (p : mv_polynomial S R) : mv_polynomial S R :=
p.sum (λ A B, monomial (A - single v 1) (B * (A v)))
@[simp]
lemma pderivative_add {v : S} {f g : mv_polynomial S R} :
pderivative v (f + g) = pderivative v f + pderivative v g :=
begin
refine sum_add_index _ _,
{ simp },
simp [add_mul],
end
@[simp]
lemma pderivative_monomial {v : S} {u : S →₀ ℕ} {a : R} :
pderivative v (monomial u a) = monomial (u - single v 1) (a * (u v)) :=
by simp [pderivative]
@[simp]
lemma pderivative_C {v : S} {a : R} : pderivative v (C a) = 0 :=
suffices pderivative v (monomial 0 a) = 0, by simpa,
by simp
@[simp]
lemma pderivative_zero {v : S} : pderivative v (0 : mv_polynomial S R) = 0 :=
suffices pderivative v (C 0 : mv_polynomial S R) = 0, by simpa,
show pderivative v (C 0 : mv_polynomial S R) = 0, from pderivative_C
section
variables (R)
/-- `pderivative : S → mv_polynomial S R → mv_polynomial S R` as an `add_monoid_hom` -/
def pderivative.add_monoid_hom (v : S) : mv_polynomial S R →+ mv_polynomial S R :=
{ to_fun := pderivative v,
map_zero' := pderivative_zero,
map_add' := λ x y, pderivative_add, }
@[simp]
lemma pderivative.add_monoid_hom_apply (v : S) (p : mv_polynomial S R) :
(pderivative.add_monoid_hom R v) p = pderivative v p :=
rfl
end
lemma pderivative_eq_zero_of_not_mem_vars {v : S} {f : mv_polynomial S R} (h : v ∉ f.vars) :
pderivative v f = 0 :=
begin
change (pderivative.add_monoid_hom R v) f = 0,
rw [f.as_sum, add_monoid_hom.map_sum],
apply finset.sum_eq_zero,
intros,
simp [mem_support_not_mem_vars_zero H h],
end
lemma pderivative_monomial_single {a : R} {v : S} {n : ℕ} :
pderivative v (monomial (single v n) a) = monomial (single v (n-1)) (a * n) :=
by simp
private lemma monomial_sub_single_one_add {v : S} {u u' : S →₀ ℕ} {r r' : R} :
monomial (u - single v 1 + u') (r * (u v) * r') =
monomial (u + u' - single v 1) (r * (u v) * r') :=
by by_cases h : u v = 0; simp [h, sub_single_one_add]
private lemma monomial_add_sub_single_one {v : S} {u u' : S →₀ ℕ} {r r' : R} :
monomial (u + (u' - single v 1)) (r * (r' * (u' v))) =
monomial (u + u' - single v 1) (r * (r' * (u' v))) :=
by by_cases h : u' v = 0; simp [h, add_sub_single_one]
lemma pderivative_monomial_mul {v : S} {u u' : S →₀ ℕ} {r r' : R} :
pderivative v (monomial u r * monomial u' r') =
pderivative v (monomial u r) * monomial u' r' + monomial u r * pderivative v (monomial u' r') :=
begin
simp [monomial_sub_single_one_add, monomial_add_sub_single_one],
congr,
ring,
end
@[simp]
lemma pderivative_mul {v : S} {f g : mv_polynomial S R} :
pderivative v (f * g) = pderivative v f * g + f * pderivative v g :=
begin
apply induction_on' f,
{ apply induction_on' g,
{ intros u r u' r', exact pderivative_monomial_mul },
{ intros p q hp hq u r,
rw [mul_add, pderivative_add, hp, hq, mul_add, pderivative_add],
ring } },
{ intros p q hp hq,
simp [add_mul, hp, hq],
ring, }
end
@[simp]
lemma pderivative_C_mul {a : R} {f : mv_polynomial S R} {v : S} :
pderivative v (C a * f) = C a * pderivative v f :=
by rw [pderivative_mul, pderivative_C, zero_mul, zero_add]
end pderivative
end mv_polynomial
|
e09c64a130ea78a23886d13761fd66d224dd8559 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/group_theory/submonoid/membership.lean | b90924f5ee5b864e41ad46548f94701f944c5fe1 | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 22,100 | lean | /-
Copyright (c) 2018 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard,
Amelia Livingston, Yury Kudryashov
-/
import group_theory.submonoid.operations
import algebra.big_operators.basic
import algebra.free_monoid
import data.finset.noncomm_prod
/-!
# Submonoids: membership criteria
In this file we prove various facts about membership in a submonoid:
* `list_prod_mem`, `multiset_prod_mem`, `prod_mem`: if each element of a collection belongs
to a multiplicative submonoid, then so does their product;
* `list_sum_mem`, `multiset_sum_mem`, `sum_mem`: if each element of a collection belongs
to an additive submonoid, then so does their sum;
* `pow_mem`, `nsmul_mem`: if `x ∈ S` where `S` is a multiplicative (resp., additive) submonoid and
`n` is a natural number, then `x^n` (resp., `n • x`) belongs to `S`;
* `mem_supr_of_directed`, `coe_supr_of_directed`, `mem_Sup_of_directed_on`,
`coe_Sup_of_directed_on`: the supremum of a directed collection of submonoid is their union.
* `sup_eq_range`, `mem_sup`: supremum of two submonoids `S`, `T` of a commutative monoid is the set
of products;
* `closure_singleton_eq`, `mem_closure_singleton`, `mem_closure_pair`: the multiplicative (resp.,
additive) closure of `{x}` consists of powers (resp., natural multiples) of `x`, and a similar
result holds for the closure of `{x, y}`.
## Tags
submonoid, submonoids
-/
open_locale big_operators
variables {M A B : Type*}
section assoc
variables [monoid M] [set_like B M] [submonoid_class B M] {S : B}
namespace submonoid_class
@[simp, norm_cast, to_additive] theorem coe_list_prod (l : list S) :
(l.prod : M) = (l.map coe).prod :=
(submonoid_class.subtype S : _ →* M).map_list_prod l
@[simp, norm_cast, to_additive] theorem coe_multiset_prod {M} [comm_monoid M] [set_like B M]
[submonoid_class B M] (m : multiset S) : (m.prod : M) = (m.map coe).prod :=
(submonoid_class.subtype S : _ →* M).map_multiset_prod m
@[simp, norm_cast, to_additive] theorem coe_finset_prod {ι M} [comm_monoid M] [set_like B M]
[submonoid_class B M] (f : ι → S) (s : finset ι) :
↑(∏ i in s, f i) = (∏ i in s, f i : M) :=
(submonoid_class.subtype S : _ →* M).map_prod f s
end submonoid_class
open submonoid_class
/-- Product of a list of elements in a submonoid is in the submonoid. -/
@[to_additive "Sum of a list of elements in an `add_submonoid` is in the `add_submonoid`."]
lemma list_prod_mem {l : list M} (hl : ∀ x ∈ l, x ∈ S) : l.prod ∈ S :=
by { lift l to list S using hl, rw ← coe_list_prod, exact l.prod.coe_prop }
/-- Product of a multiset of elements in a submonoid of a `comm_monoid` is in the submonoid. -/
@[to_additive "Sum of a multiset of elements in an `add_submonoid` of an `add_comm_monoid` is
in the `add_submonoid`."]
lemma multiset_prod_mem {M} [comm_monoid M] [set_like B M] [submonoid_class B M] (m : multiset M)
(hm : ∀ a ∈ m, a ∈ S) : m.prod ∈ S :=
by { lift m to multiset S using hm, rw ← coe_multiset_prod, exact m.prod.coe_prop }
/-- Product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is in the
submonoid. -/
@[to_additive "Sum of elements in an `add_submonoid` of an `add_comm_monoid` indexed by a `finset`
is in the `add_submonoid`."]
lemma prod_mem {M : Type*} [comm_monoid M] [set_like B M] [submonoid_class B M]
{ι : Type*} {t : finset ι} {f : ι → M} (h : ∀c ∈ t, f c ∈ S) :
∏ c in t, f c ∈ S :=
multiset_prod_mem (t.1.map f) $ λ x hx, let ⟨i, hi, hix⟩ := multiset.mem_map.1 hx in hix ▸ h i hi
namespace submonoid
variables (s : submonoid M)
@[simp, norm_cast, to_additive] theorem coe_list_prod (l : list s) :
(l.prod : M) = (l.map coe).prod :=
s.subtype.map_list_prod l
@[simp, norm_cast, to_additive] theorem coe_multiset_prod {M} [comm_monoid M] (S : submonoid M)
(m : multiset S) : (m.prod : M) = (m.map coe).prod :=
S.subtype.map_multiset_prod m
@[simp, norm_cast, to_additive] theorem coe_finset_prod {ι M} [comm_monoid M] (S : submonoid M)
(f : ι → S) (s : finset ι) :
↑(∏ i in s, f i) = (∏ i in s, f i : M) :=
S.subtype.map_prod f s
/-- Product of a list of elements in a submonoid is in the submonoid. -/
@[to_additive "Sum of a list of elements in an `add_submonoid` is in the `add_submonoid`."]
lemma list_prod_mem {l : list M} (hl : ∀ x ∈ l, x ∈ s) : l.prod ∈ s :=
by { lift l to list s using hl, rw ← coe_list_prod, exact l.prod.coe_prop }
/-- Product of a multiset of elements in a submonoid of a `comm_monoid` is in the submonoid. -/
@[to_additive "Sum of a multiset of elements in an `add_submonoid` of an `add_comm_monoid` is
in the `add_submonoid`."]
lemma multiset_prod_mem {M} [comm_monoid M] (S : submonoid M) (m : multiset M)
(hm : ∀ a ∈ m, a ∈ S) : m.prod ∈ S :=
by { lift m to multiset S using hm, rw ← coe_multiset_prod, exact m.prod.coe_prop }
@[to_additive]
lemma multiset_noncomm_prod_mem (S : submonoid M) (m : multiset M)
(comm : ∀ (x ∈ m) (y ∈ m), commute x y) (h : ∀ (x ∈ m), x ∈ S) :
m.noncomm_prod comm ∈ S :=
begin
induction m using quotient.induction_on with l,
simp only [multiset.quot_mk_to_coe, multiset.noncomm_prod_coe],
exact submonoid.list_prod_mem _ h,
end
/-- Product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is in the
submonoid. -/
@[to_additive "Sum of elements in an `add_submonoid` of an `add_comm_monoid` indexed by a `finset`
is in the `add_submonoid`."]
lemma prod_mem {M : Type*} [comm_monoid M] (S : submonoid M)
{ι : Type*} {t : finset ι} {f : ι → M} (h : ∀c ∈ t, f c ∈ S) :
∏ c in t, f c ∈ S :=
S.multiset_prod_mem (t.1.map f) $ λ x hx, let ⟨i, hi, hix⟩ := multiset.mem_map.1 hx in hix ▸ h i hi
@[to_additive]
lemma noncomm_prod_mem (S : submonoid M) {ι : Type*} (t : finset ι) (f : ι → M)
(comm : ∀ (x ∈ t) (y ∈ t), commute (f x) (f y)) (h : ∀ c ∈ t, f c ∈ S) :
t.noncomm_prod f comm ∈ S :=
begin
apply multiset_noncomm_prod_mem,
intro y,
rw multiset.mem_map,
rintros ⟨x, ⟨hx, rfl⟩⟩,
exact h x hx,
end
end submonoid
end assoc
section non_assoc
variables [mul_one_class M]
open set
namespace submonoid
-- TODO: this section can be generalized to `[submonoid_class B M] [complete_lattice B]`
-- such that `complete_lattice.le` coincides with `set_like.le`
@[to_additive]
lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → submonoid M} (hS : directed (≤) S)
{x : M} :
x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i :=
begin
refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr S i) hi⟩,
suffices : x ∈ closure (⋃ i, (S i : set M)) → ∃ i, x ∈ S i,
by simpa only [closure_Union, closure_eq (S _)] using this,
refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _),
{ exact hι.elim (λ i, ⟨i, (S i).one_mem⟩) },
{ rintros x y ⟨i, hi⟩ ⟨j, hj⟩,
rcases hS i j with ⟨k, hki, hkj⟩,
exact ⟨k, (S k).mul_mem (hki hi) (hkj hj)⟩ }
end
@[to_additive]
lemma coe_supr_of_directed {ι} [nonempty ι] {S : ι → submonoid M} (hS : directed (≤) S) :
((⨆ i, S i : submonoid M) : set M) = ⋃ i, ↑(S i) :=
set.ext $ λ x, by simp [mem_supr_of_directed hS]
@[to_additive]
lemma mem_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty)
(hS : directed_on (≤) S) {x : M} :
x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s :=
begin
haveI : nonempty S := Sne.to_subtype,
simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk]
end
@[to_additive]
lemma coe_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≤) S) :
(↑(Sup S) : set M) = ⋃ s ∈ S, ↑s :=
set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS]
@[to_additive]
lemma mem_sup_left {S T : submonoid M} : ∀ {x : M}, x ∈ S → x ∈ S ⊔ T :=
show S ≤ S ⊔ T, from le_sup_left
@[to_additive]
lemma mem_sup_right {S T : submonoid M} : ∀ {x : M}, x ∈ T → x ∈ S ⊔ T :=
show T ≤ S ⊔ T, from le_sup_right
@[to_additive]
lemma mul_mem_sup {S T : submonoid M} {x y : M} (hx : x ∈ S) (hy : y ∈ T) : x * y ∈ S ⊔ T :=
(S ⊔ T).mul_mem (mem_sup_left hx) (mem_sup_right hy)
@[to_additive]
lemma mem_supr_of_mem {ι : Sort*} {S : ι → submonoid M} (i : ι) :
∀ {x : M}, x ∈ S i → x ∈ supr S :=
show S i ≤ supr S, from le_supr _ _
@[to_additive]
lemma mem_Sup_of_mem {S : set (submonoid M)} {s : submonoid M}
(hs : s ∈ S) : ∀ {x : M}, x ∈ s → x ∈ Sup S :=
show s ≤ Sup S, from le_Sup hs
/-- An induction principle for elements of `⨆ i, S i`.
If `C` holds for `1` and all elements of `S i` for all `i`, and is preserved under multiplication,
then it holds for all elements of the supremum of `S`. -/
@[elab_as_eliminator, to_additive /-" An induction principle for elements of `⨆ i, S i`.
If `C` holds for `0` and all elements of `S i` for all `i`, and is preserved under addition,
then it holds for all elements of the supremum of `S`. "-/]
lemma supr_induction {ι : Sort*} (S : ι → submonoid M) {C : M → Prop} {x : M} (hx : x ∈ ⨆ i, S i)
(hp : ∀ i (x ∈ S i), C x)
(h1 : C 1)
(hmul : ∀ x y, C x → C y → C (x * y)) : C x :=
begin
rw supr_eq_closure at hx,
refine closure_induction hx (λ x hx, _) h1 hmul,
obtain ⟨i, hi⟩ := set.mem_Union.mp hx,
exact hp _ _ hi,
end
/-- A dependent version of `submonoid.supr_induction`. -/
@[elab_as_eliminator, to_additive /-"A dependent version of `add_submonoid.supr_induction`. "-/]
lemma supr_induction' {ι : Sort*} (S : ι → submonoid M) {C : Π x, (x ∈ ⨆ i, S i) → Prop}
(hp : ∀ i (x ∈ S i), C x (mem_supr_of_mem i ‹_›))
(h1 : C 1 (one_mem _))
(hmul : ∀ x y hx hy, C x hx → C y hy → C (x * y) (mul_mem ‹_› ‹_›))
{x : M} (hx : x ∈ ⨆ i, S i) : C x hx :=
begin
refine exists.elim _ (λ (hx : x ∈ ⨆ i, S i) (hc : C x hx), hc),
refine supr_induction S hx (λ i x hx, _) _ (λ x y, _),
{ exact ⟨_, hp _ _ hx⟩ },
{ exact ⟨_, h1⟩ },
{ rintro ⟨_, Cx⟩ ⟨_, Cy⟩,
refine ⟨_, hmul _ _ _ _ Cx Cy⟩ },
end
end submonoid
end non_assoc
namespace free_monoid
variables {α : Type*}
open submonoid
@[to_additive]
theorem closure_range_of : closure (set.range $ @of α) = ⊤ :=
eq_top_iff.2 $ λ x hx, free_monoid.rec_on x (one_mem _) $ λ x xs hxs,
mul_mem (subset_closure $ set.mem_range_self _) hxs
end free_monoid
namespace submonoid
variables [monoid M]
open monoid_hom
lemma closure_singleton_eq (x : M) : closure ({x} : set M) = (powers_hom M x).mrange :=
closure_eq_of_le (set.singleton_subset_iff.2 ⟨multiplicative.of_add 1, pow_one x⟩) $
λ x ⟨n, hn⟩, hn ▸ pow_mem (subset_closure $ set.mem_singleton _) _
/-- The submonoid generated by an element of a monoid equals the set of natural number powers of
the element. -/
lemma mem_closure_singleton {x y : M} : y ∈ closure ({x} : set M) ↔ ∃ n:ℕ, x^n=y :=
by rw [closure_singleton_eq, mem_mrange]; refl
lemma mem_closure_singleton_self {y : M} : y ∈ closure ({y} : set M) :=
mem_closure_singleton.2 ⟨1, pow_one y⟩
lemma closure_singleton_one : closure ({1} : set M) = ⊥ :=
by simp [eq_bot_iff_forall, mem_closure_singleton]
@[to_additive]
lemma closure_eq_mrange (s : set M) : closure s = (free_monoid.lift (coe : s → M)).mrange :=
by rw [mrange_eq_map, ← free_monoid.closure_range_of, map_mclosure, ← set.range_comp,
free_monoid.lift_comp_of, subtype.range_coe]
@[to_additive] lemma closure_eq_image_prod (s : set M) :
(closure s : set M) = list.prod '' {l : list M | ∀ x ∈ l, x ∈ s} :=
begin
rw [closure_eq_mrange, coe_mrange, ← list.range_map_coe, ← set.range_comp],
refl
end
@[to_additive]
lemma exists_list_of_mem_closure {s : set M} {x : M} (hx : x ∈ closure s) :
∃ (l : list M) (hl : ∀ y ∈ l, y ∈ s), l.prod = x :=
by rwa [← set_like.mem_coe, closure_eq_image_prod, set.mem_image_iff_bex] at hx
@[to_additive]
lemma exists_multiset_of_mem_closure {M : Type*} [comm_monoid M] {s : set M}
{x : M} (hx : x ∈ closure s) : ∃ (l : multiset M) (hl : ∀ y ∈ l, y ∈ s), l.prod = x :=
begin
obtain ⟨l, h1, h2⟩ := exists_list_of_mem_closure hx,
exact ⟨l, h1, (multiset.coe_prod l).trans h2⟩,
end
@[to_additive]
lemma closure_induction_left {s : set M} {p : M → Prop} {x : M} (h : x ∈ closure s) (H1 : p 1)
(Hmul : ∀ (x ∈ s) y, p y → p (x * y)) : p x :=
begin
rw closure_eq_mrange at h,
obtain ⟨l, rfl⟩ := h,
induction l using free_monoid.rec_on with x y ih,
{ exact H1 },
{ simpa only [map_mul, free_monoid.lift_eval_of] using Hmul _ x.prop _ ih }
end
@[to_additive]
lemma closure_induction_right {s : set M} {p : M → Prop} {x : M} (h : x ∈ closure s) (H1 : p 1)
(Hmul : ∀ x (y ∈ s), p x → p (x * y)) : p x :=
@closure_induction_left _ _ (mul_opposite.unop ⁻¹' s) (p ∘ mul_opposite.unop) (mul_opposite.op x)
(closure_induction h (λ x hx, subset_closure hx) (one_mem _) (λ x y hx hy, mul_mem hy hx))
H1 (λ x hx y, Hmul _ _ hx)
/-- The submonoid generated by an element. -/
def powers (n : M) : submonoid M :=
submonoid.copy (powers_hom M n).mrange (set.range ((^) n : ℕ → M)) $
set.ext (λ n, exists_congr $ λ i, by simp; refl)
@[simp] lemma mem_powers (n : M) : n ∈ powers n := ⟨1, pow_one _⟩
lemma mem_powers_iff (x z : M) : x ∈ powers z ↔ ∃ n : ℕ, z ^ n = x := iff.rfl
lemma powers_eq_closure (n : M) : powers n = closure {n} :=
by { ext, exact mem_closure_singleton.symm }
lemma powers_subset {n : M} {P : submonoid M} (h : n ∈ P) : powers n ≤ P :=
λ x hx, match x, hx with _, ⟨i, rfl⟩ := pow_mem h i end
/-- Exponentiation map from natural numbers to powers. -/
@[simps] def pow (n : M) (m : ℕ) : powers n :=
(powers_hom M n).mrange_restrict (multiplicative.of_add m)
lemma pow_apply (n : M) (m : ℕ) : submonoid.pow n m = ⟨n ^ m, m, rfl⟩ := rfl
/-- Logarithms from powers to natural numbers. -/
def log [decidable_eq M] {n : M} (p : powers n) : ℕ :=
nat.find $ (mem_powers_iff p.val n).mp p.prop
@[simp] theorem pow_log_eq_self [decidable_eq M] {n : M} (p : powers n) : pow n (log p) = p :=
subtype.ext $ nat.find_spec p.prop
lemma pow_right_injective_iff_pow_injective {n : M} :
function.injective (λ m : ℕ, n ^ m) ↔ function.injective (pow n) :=
subtype.coe_injective.of_comp_iff (pow n)
@[simp] theorem log_pow_eq_self [decidable_eq M] {n : M} (h : function.injective (λ m : ℕ, n ^ m))
(m : ℕ) : log (pow n m) = m :=
pow_right_injective_iff_pow_injective.mp h $ pow_log_eq_self _
/-- The exponentiation map is an isomorphism from the additive monoid on natural numbers to powers
when it is injective. The inverse is given by the logarithms. -/
@[simps]
def pow_log_equiv [decidable_eq M] {n : M} (h : function.injective (λ m : ℕ, n ^ m)) :
multiplicative ℕ ≃* powers n :=
{ to_fun := λ m, pow n m.to_add,
inv_fun := λ m, multiplicative.of_add (log m),
left_inv := log_pow_eq_self h,
right_inv := pow_log_eq_self,
map_mul' := λ _ _, by { simp only [pow, map_mul, of_add_add, to_add_mul] } }
lemma log_mul [decidable_eq M] {n : M} (h : function.injective (λ m : ℕ, n ^ m))
(x y : powers (n : M)) : log (x * y) = log x + log y := (pow_log_equiv h).symm.map_mul x y
theorem log_pow_int_eq_self {x : ℤ} (h : 1 < x.nat_abs) (m : ℕ) : log (pow x m) = m :=
(pow_log_equiv (int.pow_right_injective h)).symm_apply_apply _
@[simp] lemma map_powers {N : Type*} {F : Type*} [monoid N] [monoid_hom_class F M N]
(f : F) (m : M) : (powers m).map f = powers (f m) :=
by simp only [powers_eq_closure, map_mclosure f, set.image_singleton]
/-- If all the elements of a set `s` commute, then `closure s` is a commutative monoid. -/
@[to_additive "If all the elements of a set `s` commute, then `closure s` forms an additive
commutative monoid."]
def closure_comm_monoid_of_comm {s : set M} (hcomm : ∀ (a ∈ s) (b ∈ s), a * b = b * a) :
comm_monoid (closure s) :=
{ mul_comm := λ x y,
begin
ext,
simp only [submonoid.coe_mul],
exact closure_induction₂ x.prop y.prop hcomm
(λ x, by simp only [mul_one, one_mul])
(λ x, by simp only [mul_one, one_mul])
(λ x y z h₁ h₂, by rw [mul_assoc, h₂, ←mul_assoc, h₁, mul_assoc])
(λ x y z h₁ h₂, by rw [←mul_assoc, h₁, mul_assoc, h₂, ←mul_assoc]),
end,
..(closure s).to_monoid }
end submonoid
namespace submonoid
variables {N : Type*} [comm_monoid N]
open monoid_hom
@[to_additive]
lemma sup_eq_range (s t : submonoid N) : s ⊔ t = (s.subtype.coprod t.subtype).mrange :=
by rw [mrange_eq_map, ← mrange_inl_sup_mrange_inr, map_sup, map_mrange, coprod_comp_inl,
map_mrange, coprod_comp_inr, range_subtype, range_subtype]
@[to_additive]
lemma mem_sup {s t : submonoid N} {x : N} :
x ∈ s ⊔ t ↔ ∃ (y ∈ s) (z ∈ t), y * z = x :=
by simp only [sup_eq_range, mem_mrange, coprod_apply, prod.exists, set_like.exists,
coe_subtype, subtype.coe_mk]
end submonoid
namespace add_submonoid
variables [add_monoid A]
open set
lemma closure_singleton_eq (x : A) : closure ({x} : set A) = (multiples_hom A x).mrange :=
closure_eq_of_le (set.singleton_subset_iff.2 ⟨1, one_nsmul x⟩) $
λ x ⟨n, hn⟩, hn ▸ nsmul_mem (subset_closure $ set.mem_singleton _) _
/-- The `add_submonoid` generated by an element of an `add_monoid` equals the set of
natural number multiples of the element. -/
lemma mem_closure_singleton {x y : A} :
y ∈ closure ({x} : set A) ↔ ∃ n:ℕ, n • x = y :=
by rw [closure_singleton_eq, add_monoid_hom.mem_mrange]; refl
lemma closure_singleton_zero : closure ({0} : set A) = ⊥ :=
by simp [eq_bot_iff_forall, mem_closure_singleton, nsmul_zero]
/-- The additive submonoid generated by an element. -/
def multiples (x : A) : add_submonoid A :=
add_submonoid.copy (multiples_hom A x).mrange (set.range (λ i, i • x : ℕ → A)) $
set.ext (λ n, exists_congr $ λ i, by simp; refl)
@[simp] lemma mem_multiples (x : A) : x ∈ multiples x := ⟨1, one_nsmul _⟩
lemma mem_multiples_iff (x z : A) : x ∈ multiples z ↔ ∃ n : ℕ, n • z = x := iff.rfl
lemma multiples_eq_closure (x : A) : multiples x = closure {x} :=
by { ext, exact mem_closure_singleton.symm }
lemma multiples_subset {x : A} {P : add_submonoid A} (h : x ∈ P) : multiples x ≤ P :=
λ x hx, match x, hx with _, ⟨i, rfl⟩ := nsmul_mem h i end
attribute [to_additive add_submonoid.multiples] submonoid.powers
attribute [to_additive add_submonoid.mem_multiples] submonoid.mem_powers
attribute [to_additive add_submonoid.mem_multiples_iff] submonoid.mem_powers_iff
attribute [to_additive add_submonoid.multiples_eq_closure] submonoid.powers_eq_closure
attribute [to_additive add_submonoid.multiples_subset] submonoid.powers_subset
end add_submonoid
/-! Lemmas about additive closures of `subsemigroup`. -/
namespace mul_mem_class
variables {R : Type*} [non_unital_non_assoc_semiring R] [set_like M R] [mul_mem_class M R]
{S : M} {a b : R}
/-- The product of an element of the additive closure of a multiplicative subsemigroup `M`
and an element of `M` is contained in the additive closure of `M`. -/
lemma mul_right_mem_add_closure
(ha : a ∈ add_submonoid.closure (S : set R)) (hb : b ∈ S) :
a * b ∈ add_submonoid.closure (S : set R) :=
begin
revert b,
refine add_submonoid.closure_induction ha _ _ _; clear ha a,
{ exact λ r hr b hb, add_submonoid.mem_closure.mpr (λ y hy, hy (mul_mem hr hb)) },
{ exact λ b hb, by simp only [zero_mul, (add_submonoid.closure (S : set R)).zero_mem] },
{ simp_rw add_mul,
exact λ r s hr hs b hb, (add_submonoid.closure (S : set R)).add_mem (hr hb) (hs hb) }
end
/-- The product of two elements of the additive closure of a submonoid `M` is an element of the
additive closure of `M`. -/
lemma mul_mem_add_closure
(ha : a ∈ add_submonoid.closure (S : set R)) (hb : b ∈ add_submonoid.closure (S : set R)) :
a * b ∈ add_submonoid.closure (S : set R) :=
begin
revert a,
refine add_submonoid.closure_induction hb _ _ _; clear hb b,
{ exact λ r hr b hb, mul_mem_class.mul_right_mem_add_closure hb hr },
{ exact λ b hb, by simp only [mul_zero, (add_submonoid.closure (S : set R)).zero_mem] },
{ simp_rw mul_add,
exact λ r s hr hs b hb, (add_submonoid.closure (S : set R)).add_mem (hr hb) (hs hb) }
end
/-- The product of an element of `S` and an element of the additive closure of a multiplicative
submonoid `S` is contained in the additive closure of `S`. -/
lemma mul_left_mem_add_closure (ha : a ∈ S) (hb : b ∈ add_submonoid.closure (S : set R)) :
a * b ∈ add_submonoid.closure (S : set R) :=
mul_mem_add_closure (add_submonoid.mem_closure.mpr (λ sT hT, hT ha)) hb
end mul_mem_class
namespace submonoid
/-- An element is in the closure of a two-element set if it is a linear combination of those two
elements. -/
@[to_additive "An element is in the closure of a two-element set if it is a linear combination of
those two elements."]
lemma mem_closure_pair {A : Type*} [comm_monoid A] (a b c : A) :
c ∈ submonoid.closure ({a, b} : set A) ↔ ∃ m n : ℕ, a ^ m * b ^ n = c :=
begin
rw [←set.singleton_union, submonoid.closure_union, mem_sup],
simp_rw [exists_prop, mem_closure_singleton, exists_exists_eq_and],
end
end submonoid
section mul_add
lemma of_mul_image_powers_eq_multiples_of_mul [monoid M] {x : M} :
additive.of_mul '' ((submonoid.powers x) : set M) = add_submonoid.multiples (additive.of_mul x) :=
begin
ext,
split,
{ rintros ⟨y, ⟨n, hy1⟩, hy2⟩,
use n,
simpa [← of_mul_pow, hy1] },
{ rintros ⟨n, hn⟩,
refine ⟨x ^ n, ⟨n, rfl⟩, _⟩,
rwa of_mul_pow }
end
lemma of_add_image_multiples_eq_powers_of_add [add_monoid A] {x : A} :
multiplicative.of_add '' ((add_submonoid.multiples x) : set A) =
submonoid.powers (multiplicative.of_add x) :=
begin
symmetry,
rw equiv.eq_image_iff_symm_image_eq,
exact of_mul_image_powers_eq_multiples_of_mul,
end
end mul_add
|
a001195ee5aa9d68fd3cfe9505828a27d6e215a8 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/data/finsupp/pointwise.lean | 2133040e0b18482c9a5914c06415803d6f07f7d7 | [
"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,967 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import data.finsupp.basic
/-!
# The pointwise product on `finsupp`.
TODO per issue #1864:
We intend to remove the convolution product on finsupp, and define
it only on a type synonym `add_monoid_algebra`. After we've done this,
it would be good to make this the default product on `finsupp`.
-/
noncomputable theory
open_locale classical
open finset
universes u₁ u₂ u₃ u₄ u₅
variables {α : Type u₁} {β : Type u₂} {γ : Type u₃} {δ : Type u₄} {ι : Type u₅}
namespace finsupp
/-! ### Declarations about the pointwise product on `finsupp`s -/
section
variables [mul_zero_class β]
/-- The product of `f g : α →₀ β` is the finitely supported function
whose value at `a` is `f a * g a`. -/
instance : has_mul (α →₀ β) := ⟨zip_with (*) (mul_zero 0)⟩
@[simp] lemma mul_apply {g₁ g₂ : α →₀ β} {a : α} : (g₁ * g₂) a = g₁ a * g₂ a :=
rfl
lemma support_mul {g₁ g₂ : α →₀ β} : (g₁ * g₂).support ⊆ g₁.support ∩ g₂.support :=
begin
intros a h,
simp only [mul_apply, mem_support_iff] at h,
simp only [mem_support_iff, mem_inter, ne.def],
rw ←not_or_distrib,
intro w,
apply h,
cases w; { rw w, simp },
end
end
instance [semiring β] : semigroup (α →₀ β) :=
{ mul := (*),
mul_assoc := λ f g h, by { ext, simp only [mul_apply, mul_assoc], }, }
instance [ring β] : distrib (α →₀ β) :=
{ left_distrib := λ f g h, by { ext, simp only [mul_apply, add_apply, left_distrib] {proj := ff} },
right_distrib := λ f g h, by { ext, simp only [mul_apply, add_apply, right_distrib] {proj := ff} },
..(infer_instance : semigroup (α →₀ β)),
..(infer_instance : add_comm_group (α →₀ β)) }
-- If `non_unital_semiring` existed in the algebraic hierarchy, we could produce one here.
end finsupp
|
be6c5a714606fe592939684ee6b1d0151fd7a08d | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/order/with_zero.lean | 8d5df167e102d6a25d626699547aa2e13a28676b | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 10,201 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Johan Commelin, Patrick Massot
-/
import algebra.hom.equiv.units.group_with_zero
import algebra.group_with_zero.inj_surj
import algebra.order.group.units
import algebra.order.monoid.basic
import algebra.order.monoid.with_zero.defs
import algebra.order.group.instances
import algebra.order.monoid.type_tags
/-!
# Linearly ordered commutative groups and monoids with a zero element adjoined
This file sets up a special class of linearly ordered commutative monoids
that show up as the target of so-called “valuations” in algebraic number theory.
Usually, in the informal literature, these objects are constructed
by taking a linearly ordered commutative group Γ and formally adjoining a zero element: Γ ∪ {0}.
The disadvantage is that a type such as `nnreal` is not of that form,
whereas it is a very common target for valuations.
The solutions is to use a typeclass, and that is exactly what we do in this file.
Note that to avoid issues with import cycles, `linear_ordered_comm_monoid_with_zero` is defined
in another file. However, the lemmas about it are stated here.
-/
set_option old_structure_cmd true
/-- A linearly ordered commutative group with a zero element. -/
@[protect_proj, ancestor linear_ordered_comm_monoid_with_zero comm_group_with_zero]
class linear_ordered_comm_group_with_zero (α : Type*)
extends linear_ordered_comm_monoid_with_zero α, comm_group_with_zero α
variables {α : Type*}
variables {a b c d x y z : α}
instance [linear_ordered_add_comm_monoid_with_top α] :
linear_ordered_comm_monoid_with_zero (multiplicative αᵒᵈ) :=
{ zero := multiplicative.of_add (⊤ : α),
zero_mul := top_add,
mul_zero := add_top,
zero_le_one := (le_top : (0 : α) ≤ ⊤),
..multiplicative.ordered_comm_monoid,
..multiplicative.linear_order }
instance [linear_ordered_add_comm_group_with_top α] :
linear_ordered_comm_group_with_zero (multiplicative αᵒᵈ) :=
{ inv_zero := linear_ordered_add_comm_group_with_top.neg_top,
mul_inv_cancel := linear_ordered_add_comm_group_with_top.add_neg_cancel,
..multiplicative.div_inv_monoid,
..multiplicative.linear_ordered_comm_monoid_with_zero,
..multiplicative.nontrivial }
instance [linear_ordered_comm_monoid α] :
linear_ordered_comm_monoid_with_zero (with_zero α) :=
{ mul_le_mul_left := λ x y, mul_le_mul_left',
zero_le_one := with_zero.zero_le _,
..with_zero.linear_order,
..with_zero.comm_monoid_with_zero }
instance [linear_ordered_comm_group α] :
linear_ordered_comm_group_with_zero (with_zero α) :=
{ ..with_zero.linear_ordered_comm_monoid_with_zero,
..with_zero.comm_group_with_zero }
section linear_ordered_comm_monoid
variables [linear_ordered_comm_monoid_with_zero α]
/-
The following facts are true more generally in a (linearly) ordered commutative monoid.
-/
/-- Pullback a `linear_ordered_comm_monoid_with_zero` under an injective map.
See note [reducible non-instances]. -/
@[reducible]
def function.injective.linear_ordered_comm_monoid_with_zero {β : Type*}
[has_zero β] [has_one β] [has_mul β] [has_pow β ℕ] [has_sup β] [has_inf β]
(f : β → α) (hf : function.injective f) (zero : f 0 = 0) (one : f 1 = 1)
(mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n)
(hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) :
linear_ordered_comm_monoid_with_zero β :=
{ zero_le_one := show f 0 ≤ f 1, by simp only [zero, one,
linear_ordered_comm_monoid_with_zero.zero_le_one],
..linear_order.lift f hf hsup hinf,
..hf.ordered_comm_monoid f one mul npow,
..hf.comm_monoid_with_zero f zero one mul npow }
@[simp] lemma zero_le' : 0 ≤ a :=
by simpa only [mul_zero, mul_one] using mul_le_mul_left' zero_le_one a
@[simp] lemma not_lt_zero' : ¬a < 0 :=
not_lt_of_le zero_le'
@[simp] lemma le_zero_iff : a ≤ 0 ↔ a = 0 :=
⟨λ h, le_antisymm h zero_le', λ h, h ▸ le_rfl⟩
lemma zero_lt_iff : 0 < a ↔ a ≠ 0 :=
⟨ne_of_gt, λ h, lt_of_le_of_ne zero_le' h.symm⟩
lemma ne_zero_of_lt (h : b < a) : a ≠ 0 :=
λ h1, not_lt_zero' $ show b < 0, from h1 ▸ h
instance : linear_ordered_add_comm_monoid_with_top (additive αᵒᵈ) :=
{ top := (0 : α),
top_add' := λ a, (zero_mul a : (0 : α) * a = 0),
le_top := λ _, zero_le',
..additive.ordered_add_comm_monoid,
..additive.linear_order }
end linear_ordered_comm_monoid
variables [linear_ordered_comm_group_with_zero α]
-- TODO: Do we really need the following two?
/-- Alias of `mul_le_one'` for unification. -/
lemma mul_le_one₀ (ha : a ≤ 1) (hb : b ≤ 1) : a * b ≤ 1 := mul_le_one' ha hb
/-- Alias of `one_le_mul'` for unification. -/
lemma one_le_mul₀ (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := one_le_mul ha hb
lemma le_of_le_mul_right (h : c ≠ 0) (hab : a * c ≤ b * c) : a ≤ b :=
by simpa only [mul_inv_cancel_right₀ h] using (mul_le_mul_right' hab c⁻¹)
lemma le_mul_inv_of_mul_le (h : c ≠ 0) (hab : a * c ≤ b) : a ≤ b * c⁻¹ :=
le_of_le_mul_right h (by simpa [h] using hab)
lemma mul_inv_le_of_le_mul (hab : a ≤ b * c) : a * c⁻¹ ≤ b :=
begin
by_cases h : c = 0,
{ simp [h], },
{ exact le_of_le_mul_right h (by simpa [h] using hab), },
end
lemma inv_le_one₀ (ha : a ≠ 0) : a⁻¹ ≤ 1 ↔ 1 ≤ a := @inv_le_one' _ _ _ _ $ units.mk0 a ha
lemma one_le_inv₀ (ha : a ≠ 0) : 1 ≤ a⁻¹ ↔ a ≤ 1 := @one_le_inv' _ _ _ _ $ units.mk0 a ha
lemma le_mul_inv_iff₀ (hc : c ≠ 0) : a ≤ b * c⁻¹ ↔ a * c ≤ b :=
⟨λ h, inv_inv c ▸ mul_inv_le_of_le_mul h, le_mul_inv_of_mul_le hc⟩
lemma mul_inv_le_iff₀ (hc : c ≠ 0) : a * c⁻¹ ≤ b ↔ a ≤ b * c :=
⟨λ h, inv_inv c ▸ le_mul_inv_of_mul_le (inv_ne_zero hc) h, mul_inv_le_of_le_mul⟩
lemma div_le_div₀ (a b c d : α) (hb : b ≠ 0) (hd : d ≠ 0) :
a * b⁻¹ ≤ c * d⁻¹ ↔ a * d ≤ c * b :=
if ha : a = 0 then by simp [ha] else
if hc : c = 0 then by simp [inv_ne_zero hb, hc, hd] else
show (units.mk0 a ha) * (units.mk0 b hb)⁻¹ ≤ (units.mk0 c hc) * (units.mk0 d hd)⁻¹ ↔
(units.mk0 a ha) * (units.mk0 d hd) ≤ (units.mk0 c hc) * (units.mk0 b hb),
from mul_inv_le_mul_inv_iff'
@[simp] lemma units.zero_lt (u : αˣ) : (0 : α) < u :=
zero_lt_iff.2 $ u.ne_zero
lemma mul_lt_mul_of_lt_of_le₀ (hab : a ≤ b) (hb : b ≠ 0) (hcd : c < d) : a * c < b * d :=
have hd : d ≠ 0 := ne_zero_of_lt hcd,
if ha : a = 0 then by { rw [ha, zero_mul, zero_lt_iff], exact mul_ne_zero hb hd } else
if hc : c = 0 then by { rw [hc, mul_zero, zero_lt_iff], exact mul_ne_zero hb hd } else
show (units.mk0 a ha) * (units.mk0 c hc) < (units.mk0 b hb) * (units.mk0 d hd),
from mul_lt_mul_of_le_of_lt hab hcd
lemma mul_lt_mul₀ (hab : a < b) (hcd : c < d) : a * c < b * d :=
mul_lt_mul_of_lt_of_le₀ hab.le (ne_zero_of_lt hab) hcd
lemma mul_inv_lt_of_lt_mul₀ (h : x < y * z) : x * z⁻¹ < y :=
by { contrapose! h, simpa only [inv_inv] using mul_inv_le_of_le_mul h }
lemma inv_mul_lt_of_lt_mul₀ (h : x < y * z) : y⁻¹ * x < z :=
by { rw mul_comm at *, exact mul_inv_lt_of_lt_mul₀ h }
lemma mul_lt_right₀ (c : α) (h : a < b) (hc : c ≠ 0) : a * c < b * c :=
by { contrapose! h, exact le_of_le_mul_right hc h }
lemma inv_lt_inv₀ (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ < b⁻¹ ↔ b < a :=
show (units.mk0 a ha)⁻¹ < (units.mk0 b hb)⁻¹ ↔ (units.mk0 b hb) < (units.mk0 a ha),
from inv_lt_inv_iff
lemma inv_le_inv₀ (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ ≤ b⁻¹ ↔ b ≤ a :=
show (units.mk0 a ha)⁻¹ ≤ (units.mk0 b hb)⁻¹ ↔ (units.mk0 b hb) ≤ (units.mk0 a ha),
from inv_le_inv_iff
lemma lt_of_mul_lt_mul_of_le₀ (h : a * b < c * d) (hc : 0 < c) (hh : c ≤ a) : b < d :=
begin
have ha : a ≠ 0 := ne_of_gt (lt_of_lt_of_le hc hh),
simp_rw ← inv_le_inv₀ ha (ne_of_gt hc) at hh,
have := mul_lt_mul_of_lt_of_le₀ hh (inv_ne_zero (ne_of_gt hc)) h,
simpa [inv_mul_cancel_left₀ ha, inv_mul_cancel_left₀ (ne_of_gt hc)] using this,
end
lemma mul_le_mul_right₀ (hc : c ≠ 0) : a * c ≤ b * c ↔ a ≤ b :=
⟨le_of_le_mul_right hc, λ hab, mul_le_mul_right' hab _⟩
lemma mul_le_mul_left₀ (ha : a ≠ 0) : a * b ≤ a * c ↔ b ≤ c :=
by {simp only [mul_comm a], exact mul_le_mul_right₀ ha }
lemma div_le_div_right₀ (hc : c ≠ 0) : a / c ≤ b / c ↔ a ≤ b :=
by rw [div_eq_mul_inv, div_eq_mul_inv, mul_le_mul_right₀ (inv_ne_zero hc)]
lemma div_le_div_left₀ (ha : a ≠ 0) (hb : b ≠ 0) (hc : c ≠ 0) : a / b ≤ a / c ↔ c ≤ b :=
by simp only [div_eq_mul_inv, mul_le_mul_left₀ ha, inv_le_inv₀ hb hc]
lemma le_div_iff₀ (hc : c ≠ 0) : a ≤ b / c ↔ a*c ≤ b :=
by rw [div_eq_mul_inv, le_mul_inv_iff₀ hc]
lemma div_le_iff₀ (hc : c ≠ 0) : a / c ≤ b ↔ a ≤ b*c :=
by rw [div_eq_mul_inv, mul_inv_le_iff₀ hc]
/-- `equiv.mul_left₀` as an order_iso on a `linear_ordered_comm_group_with_zero.`.
Note that `order_iso.mul_left₀` refers to the `linear_ordered_field` version. -/
@[simps apply to_equiv {simp_rhs := tt}]
def order_iso.mul_left₀' {a : α} (ha : a ≠ 0) : α ≃o α :=
{ map_rel_iff' := λ x y, mul_le_mul_left₀ ha, ..equiv.mul_left₀ a ha }
lemma order_iso.mul_left₀'_symm {a : α} (ha : a ≠ 0) :
(order_iso.mul_left₀' ha).symm = order_iso.mul_left₀' (inv_ne_zero ha) :=
by { ext, refl }
/-- `equiv.mul_right₀` as an order_iso on a `linear_ordered_comm_group_with_zero.`.
Note that `order_iso.mul_right₀` refers to the `linear_ordered_field` version. -/
@[simps apply to_equiv {simp_rhs := tt}]
def order_iso.mul_right₀' {a : α} (ha : a ≠ 0) : α ≃o α :=
{ map_rel_iff' := λ _ _, mul_le_mul_right₀ ha, ..equiv.mul_right₀ a ha }
lemma order_iso.mul_right₀'_symm {a : α} (ha : a ≠ 0) :
(order_iso.mul_right₀' ha).symm = order_iso.mul_right₀' (inv_ne_zero ha) :=
by { ext, refl }
instance : linear_ordered_add_comm_group_with_top (additive αᵒᵈ) :=
{ neg_top := inv_zero,
add_neg_cancel := λ a ha, mul_inv_cancel ha,
..additive.sub_neg_monoid,
..additive.linear_ordered_add_comm_monoid_with_top,
..additive.nontrivial }
|
a8c5dcc12a715e4c60f15aec09e9badb1379a5e2 | 217bb195841a8be2d1b4edd2084d6b69ccd62f50 | /library/init/data/default.lean | 066cf1e31e473cada39bb75a6471f57cbfa46113 | [
"Apache-2.0"
] | permissive | frank-lesser/lean4 | 717f56c9bacd5bf3a67542d2f5cea721d4743a30 | 79e2abe33f73162f773ea731265e456dbfe822f9 | refs/heads/master | 1,589,741,267,933 | 1,556,424,200,000 | 1,556,424,281,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 463 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.data.basic init.data.nat init.data.char init.data.string
import init.data.list init.data.int init.data.array
import init.data.fin init.data.uint init.data.ordering
import init.data.rbtree init.data.rbmap init.data.option.basic init.data.option.instances
import init.data.hashmap
|
3551ad1c1b3cdea953431874e5166c6e3ee6ccc9 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/topology/continuous_function/algebra.lean | f60f007b216292b6ce1608e9952e16fa7816fa19 | [
"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 | 38,311 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Nicolò Cavalleri
-/
import algebra.algebra.pi
import algebra.periodic
import algebra.algebra.subalgebra.basic
import algebra.star.star_alg_hom
import tactic.field_simp
import topology.algebra.module.basic
import topology.algebra.infinite_sum.basic
import topology.algebra.star
import topology.algebra.uniform_group
import topology.continuous_function.ordered
import topology.uniform_space.compact_convergence
/-!
# Algebraic structures over continuous functions
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we define instances of algebraic structures over the type `continuous_map α β`
(denoted `C(α, β)`) of **bundled** continuous maps from `α` to `β`. For example, `C(α, β)`
is a group when `β` is a group, a ring when `β` is a ring, etc.
For each type of algebraic structure, we also define an appropriate subobject of `α → β`
with carrier `{ f : α → β | continuous f }`. For example, when `β` is a group, a subgroup
`continuous_subgroup α β` of `α → β` is constructed with carrier `{ f : α → β | continuous f }`.
Note that, rather than using the derived algebraic structures on these subobjects
(for example, when `β` is a group, the derived group structure on `continuous_subgroup α β`),
one should use `C(α, β)` with the appropriate instance of the structure.
-/
local attribute [elab_simple] continuous.comp
namespace continuous_functions
variables {α : Type*} {β : Type*} [topological_space α] [topological_space β]
variables {f g : {f : α → β | continuous f }}
instance : has_coe_to_fun {f : α → β | continuous f} (λ _, α → β) := ⟨subtype.val⟩
end continuous_functions
namespace continuous_map
variables {α : Type*} {β : Type*} {γ : Type*}
variables [topological_space α] [topological_space β] [topological_space γ]
/- ### "mul" and "add" -/
@[to_additive]
instance has_mul [has_mul β] [has_continuous_mul β] : has_mul C(α, β) :=
⟨λ f g, ⟨f * g, continuous_mul.comp (f.continuous.prod_mk g.continuous : _)⟩⟩
@[simp, norm_cast, to_additive]
lemma coe_mul [has_mul β] [has_continuous_mul β] (f g : C(α, β)) : ⇑(f * g) = f * g := rfl
@[simp, to_additive]
lemma mul_apply [has_mul β] [has_continuous_mul β] (f g : C(α, β)) (x : α) :
(f * g) x = f x * g x := rfl
@[simp, to_additive] lemma mul_comp [has_mul γ] [has_continuous_mul γ]
(f₁ f₂ : C(β, γ)) (g : C(α, β)) :
(f₁ * f₂).comp g = f₁.comp g * f₂.comp g :=
rfl
/- ### "one" -/
@[to_additive] instance [has_one β] : has_one C(α, β) := ⟨const α 1⟩
@[simp, norm_cast, to_additive] lemma coe_one [has_one β] : ⇑(1 : C(α, β)) = 1 := rfl
@[simp, to_additive] lemma one_apply [has_one β] (x : α) : (1 : C(α, β)) x = 1 := rfl
@[simp, to_additive] lemma one_comp [has_one γ] (g : C(α, β)) : (1 : C(β, γ)).comp g = 1 := rfl
/- ### "nat_cast" -/
instance [has_nat_cast β] : has_nat_cast C(α, β) := ⟨λ n, continuous_map.const _ n⟩
@[simp, norm_cast] lemma coe_nat_cast [has_nat_cast β] (n : ℕ) : ((n : C(α, β)) : α → β) = n := rfl
@[simp] lemma nat_cast_apply [has_nat_cast β] (n : ℕ) (x : α) : (n : C(α, β)) x = n := rfl
/- ### "int_cast" -/
instance [has_int_cast β] : has_int_cast C(α, β) :=
⟨λ n, continuous_map.const _ n⟩
@[simp, norm_cast]
lemma coe_int_cast [has_int_cast β] (n : ℤ) : ((n : C(α, β)) : α → β) = n := rfl
@[simp] lemma int_cast_apply [has_int_cast β] (n : ℤ) (x : α) : (n : C(α, β)) x = n := rfl
/- ### "nsmul" and "pow" -/
instance has_nsmul [add_monoid β] [has_continuous_add β] : has_smul ℕ C(α, β) :=
⟨λ n f, ⟨n • f, f.continuous.nsmul n⟩⟩
@[to_additive]
instance has_pow [monoid β] [has_continuous_mul β] : has_pow C(α, β) ℕ :=
⟨λ f n, ⟨f ^ n, f.continuous.pow n⟩⟩
@[norm_cast, to_additive]
lemma coe_pow [monoid β] [has_continuous_mul β] (f : C(α, β)) (n : ℕ) :
⇑(f ^ n) = f ^ n := rfl
@[to_additive] lemma pow_apply [monoid β] [has_continuous_mul β]
(f : C(α, β)) (n : ℕ) (x : α) :
(f ^ n) x = f x ^ n :=
rfl
-- don't make auto-generated `coe_nsmul` and `nsmul_apply` simp, as the linter complains they're
-- redundant WRT `coe_smul`
attribute [simp] coe_pow pow_apply
@[to_additive] lemma pow_comp [monoid γ] [has_continuous_mul γ]
(f : C(β, γ)) (n : ℕ) (g : C(α, β)) :
(f^n).comp g = (f.comp g)^n :=
rfl
-- don't make `nsmul_comp` simp as the linter complains it's redundant WRT `smul_comp`
attribute [simp] pow_comp
/- ### "inv" and "neg" -/
@[to_additive]
instance [group β] [topological_group β] : has_inv C(α, β) :=
{ inv := λ f, ⟨f⁻¹, f.continuous.inv⟩ }
@[simp, norm_cast, to_additive]
lemma coe_inv [group β] [topological_group β] (f : C(α, β)) :
⇑(f⁻¹) = f⁻¹ :=
rfl
@[simp, to_additive] lemma inv_apply [group β] [topological_group β] (f : C(α, β)) (x : α) :
f⁻¹ x = (f x)⁻¹ :=
rfl
@[simp, to_additive] lemma inv_comp [group γ] [topological_group γ] (f : C(β, γ)) (g : C(α, β)) :
(f⁻¹).comp g = (f.comp g)⁻¹ :=
rfl
/- ### "div" and "sub" -/
@[to_additive]
instance [has_div β] [has_continuous_div β] : has_div C(α, β) :=
{ div := λ f g, ⟨f / g, f.continuous.div' g.continuous⟩ }
@[simp, norm_cast, to_additive]
lemma coe_div [has_div β] [has_continuous_div β] (f g : C(α, β)) : ⇑(f / g) = f / g :=
rfl
@[simp, to_additive] lemma div_apply [has_div β] [has_continuous_div β] (f g : C(α, β)) (x : α) :
(f / g) x = f x / g x :=
rfl
@[simp, to_additive] lemma div_comp [has_div γ] [has_continuous_div γ]
(f g : C(β, γ)) (h : C(α, β)) :
(f / g).comp h = (f.comp h) / (g.comp h) :=
rfl
/- ### "zpow" and "zsmul" -/
instance has_zsmul [add_group β] [topological_add_group β] : has_smul ℤ C(α, β) :=
{ smul := λ z f, ⟨z • f, f.continuous.zsmul z⟩ }
@[to_additive]
instance has_zpow [group β] [topological_group β] :
has_pow C(α, β) ℤ :=
{ pow := λ f z, ⟨f ^ z, f.continuous.zpow z⟩ }
@[norm_cast, to_additive]
lemma coe_zpow [group β] [topological_group β] (f : C(α, β)) (z : ℤ) :
⇑(f ^ z) = f ^ z :=
rfl
@[to_additive] lemma zpow_apply [group β] [topological_group β]
(f : C(α, β)) (z : ℤ) (x : α) :
(f ^ z) x = f x ^ z :=
rfl
-- don't make auto-generated `coe_zsmul` and `zsmul_apply` simp as the linter complains they're
-- redundant WRT `coe_smul`
attribute [simp] coe_zpow zpow_apply
@[to_additive]
lemma zpow_comp [group γ] [topological_group γ] (f : C(β, γ)) (z : ℤ) (g : C(α, β)) :
(f^z).comp g = (f.comp g)^z :=
rfl
-- don't make `zsmul_comp` simp as the linter complains it's redundant WRT `smul_comp`
attribute [simp] zpow_comp
end continuous_map
section group_structure
/-!
### Group stucture
In this section we show that continuous functions valued in a topological group inherit
the structure of a group.
-/
section subtype
/-- The `submonoid` of continuous maps `α → β`. -/
@[to_additive "The `add_submonoid` of continuous maps `α → β`. "]
def continuous_submonoid (α : Type*) (β : Type*) [topological_space α] [topological_space β]
[mul_one_class β] [has_continuous_mul β] : submonoid (α → β) :=
{ carrier := { f : α → β | continuous f },
one_mem' := @continuous_const _ _ _ _ 1,
mul_mem' := λ f g fc gc, fc.mul gc }
/-- The subgroup of continuous maps `α → β`. -/
@[to_additive "The `add_subgroup` of continuous maps `α → β`. "]
def continuous_subgroup (α : Type*) (β : Type*) [topological_space α] [topological_space β]
[group β] [topological_group β] : subgroup (α → β) :=
{ inv_mem' := λ f fc, continuous.inv fc,
..continuous_submonoid α β, }.
end subtype
namespace continuous_map
variables {α β : Type*} [topological_space α] [topological_space β]
@[to_additive]
instance [semigroup β] [has_continuous_mul β] : semigroup C(α, β) :=
coe_injective.semigroup _ coe_mul
@[to_additive]
instance [comm_semigroup β] [has_continuous_mul β] : comm_semigroup C(α, β) :=
coe_injective.comm_semigroup _ coe_mul
@[to_additive]
instance [mul_one_class β] [has_continuous_mul β] : mul_one_class C(α, β) :=
coe_injective.mul_one_class _ coe_one coe_mul
instance [mul_zero_class β] [has_continuous_mul β] : mul_zero_class C(α, β) :=
coe_injective.mul_zero_class _ coe_zero coe_mul
instance [semigroup_with_zero β] [has_continuous_mul β] : semigroup_with_zero C(α, β) :=
coe_injective.semigroup_with_zero _ coe_zero coe_mul
@[to_additive]
instance [monoid β] [has_continuous_mul β] : monoid C(α, β) :=
coe_injective.monoid _ coe_one coe_mul coe_pow
instance [monoid_with_zero β] [has_continuous_mul β] : monoid_with_zero C(α, β) :=
coe_injective.monoid_with_zero _ coe_zero coe_one coe_mul coe_pow
@[to_additive]
instance [comm_monoid β] [has_continuous_mul β] : comm_monoid C(α, β) :=
coe_injective.comm_monoid _ coe_one coe_mul coe_pow
instance [comm_monoid_with_zero β] [has_continuous_mul β] : comm_monoid_with_zero C(α, β) :=
coe_injective.comm_monoid_with_zero _ coe_zero coe_one coe_mul coe_pow
@[to_additive]
instance [locally_compact_space α] [has_mul β] [has_continuous_mul β] :
has_continuous_mul C(α, β) :=
⟨begin
refine continuous_of_continuous_uncurry _ _,
have h1 : continuous (λ x : (C(α, β) × C(α, β)) × α, x.fst.fst x.snd) :=
continuous_eval'.comp (continuous_fst.prod_map continuous_id),
have h2 : continuous (λ x : (C(α, β) × C(α, β)) × α, x.fst.snd x.snd) :=
continuous_eval'.comp (continuous_snd.prod_map continuous_id),
exact h1.mul h2,
end⟩
/-- Coercion to a function as an `monoid_hom`. Similar to `monoid_hom.coe_fn`. -/
@[to_additive "Coercion to a function as an `add_monoid_hom`. Similar to `add_monoid_hom.coe_fn`.",
simps]
def coe_fn_monoid_hom [monoid β] [has_continuous_mul β] : C(α, β) →* (α → β) :=
{ to_fun := coe_fn, map_one' := coe_one, map_mul' := coe_mul }
variables (α)
/-- Composition on the left by a (continuous) homomorphism of topological monoids, as a
`monoid_hom`. Similar to `monoid_hom.comp_left`. -/
@[to_additive "Composition on the left by a (continuous) homomorphism of topological `add_monoid`s,
as an `add_monoid_hom`. Similar to `add_monoid_hom.comp_left`.", simps]
protected def _root_.monoid_hom.comp_left_continuous
{γ : Type*} [monoid β] [has_continuous_mul β]
[topological_space γ] [monoid γ] [has_continuous_mul γ] (g : β →* γ) (hg : continuous g) :
C(α, β) →* C(α, γ) :=
{ to_fun := λ f, (⟨g, hg⟩ : C(β, γ)).comp f,
map_one' := ext $ λ x, g.map_one,
map_mul' := λ f₁ f₂, ext $ λ x, g.map_mul _ _ }
variables {α}
/-- Composition on the right as a `monoid_hom`. Similar to `monoid_hom.comp_hom'`. -/
@[to_additive "Composition on the right as an `add_monoid_hom`. Similar to
`add_monoid_hom.comp_hom'`.", simps]
def comp_monoid_hom' {γ : Type*} [topological_space γ]
[mul_one_class γ] [has_continuous_mul γ] (g : C(α, β)) : C(β, γ) →* C(α, γ) :=
{ to_fun := λ f, f.comp g, map_one' := one_comp g, map_mul' := λ f₁ f₂, mul_comp f₁ f₂ g }
open_locale big_operators
@[simp, to_additive] lemma coe_prod [comm_monoid β] [has_continuous_mul β]
{ι : Type*} (s : finset ι) (f : ι → C(α, β)) :
⇑(∏ i in s, f i) = (∏ i in s, (f i : α → β)) :=
(coe_fn_monoid_hom : C(α, β) →* _).map_prod f s
@[to_additive]
lemma prod_apply [comm_monoid β] [has_continuous_mul β]
{ι : Type*} (s : finset ι) (f : ι → C(α, β)) (a : α) :
(∏ i in s, f i) a = (∏ i in s, f i a) :=
by simp
@[to_additive]
instance [group β] [topological_group β] : group C(α, β) :=
coe_injective.group _ coe_one coe_mul coe_inv coe_div coe_pow coe_zpow
@[to_additive]
instance [comm_group β] [topological_group β] : comm_group C(α, β) :=
coe_injective.comm_group _ coe_one coe_mul coe_inv coe_div coe_pow coe_zpow
@[to_additive] instance [comm_group β] [topological_group β] : topological_group C(α, β) :=
{ continuous_mul := by
{ letI : uniform_space β := topological_group.to_uniform_space β,
have : uniform_group β := topological_comm_group_is_uniform,
rw continuous_iff_continuous_at,
rintros ⟨f, g⟩,
rw [continuous_at, tendsto_iff_forall_compact_tendsto_uniformly_on, nhds_prod_eq],
exactI λ K hK, uniform_continuous_mul.comp_tendsto_uniformly_on
((tendsto_iff_forall_compact_tendsto_uniformly_on.mp filter.tendsto_id K hK).prod
(tendsto_iff_forall_compact_tendsto_uniformly_on.mp filter.tendsto_id K hK)), },
continuous_inv := by
{ letI : uniform_space β := topological_group.to_uniform_space β,
have : uniform_group β := topological_comm_group_is_uniform,
rw continuous_iff_continuous_at,
intro f,
rw [continuous_at, tendsto_iff_forall_compact_tendsto_uniformly_on],
exactI λ K hK, uniform_continuous_inv.comp_tendsto_uniformly_on
(tendsto_iff_forall_compact_tendsto_uniformly_on.mp filter.tendsto_id K hK), } }
-- TODO: rewrite the next three lemmas for products and deduce sum case via `to_additive`, once
-- definition of `tprod` is in place
/-- If `α` is locally compact, and an infinite sum of functions in `C(α, β)`
converges to `g` (for the compact-open topology), then the pointwise sum converges to `g x` for
all `x ∈ α`. -/
lemma has_sum_apply {γ : Type*} [locally_compact_space α] [add_comm_monoid β] [has_continuous_add β]
{f : γ → C(α, β)} {g : C(α, β)} (hf : has_sum f g) (x : α) :
has_sum (λ i : γ, f i x) (g x) :=
begin
let evₓ : add_monoid_hom C(α, β) β := (pi.eval_add_monoid_hom _ x).comp coe_fn_add_monoid_hom,
exact hf.map evₓ (continuous_map.continuous_eval_const' x),
end
lemma summable_apply [locally_compact_space α] [add_comm_monoid β] [has_continuous_add β]
{γ : Type*} {f : γ → C(α, β)} (hf : summable f) (x : α) :
summable (λ i : γ, f i x) :=
(has_sum_apply hf.has_sum x).summable
lemma tsum_apply [locally_compact_space α] [t2_space β] [add_comm_monoid β] [has_continuous_add β]
{γ : Type*} {f : γ → C(α, β)} (hf : summable f) (x : α) :
(∑' (i:γ), f i x) = (∑' (i:γ), f i) x :=
(has_sum_apply hf.has_sum x).tsum_eq
end continuous_map
end group_structure
section ring_structure
/-!
### Ring stucture
In this section we show that continuous functions valued in a topological semiring `R` inherit
the structure of a ring.
-/
section subtype
/-- The subsemiring of continuous maps `α → β`. -/
def continuous_subsemiring (α : Type*) (R : Type*) [topological_space α] [topological_space R]
[non_assoc_semiring R] [topological_semiring R] : subsemiring (α → R) :=
{ ..continuous_add_submonoid α R,
..continuous_submonoid α R }
/-- The subring of continuous maps `α → β`. -/
def continuous_subring (α : Type*) (R : Type*) [topological_space α] [topological_space R]
[ring R] [topological_ring R] : subring (α → R) :=
{ ..continuous_subsemiring α R,
..continuous_add_subgroup α R }
end subtype
namespace continuous_map
instance {α : Type*} {β : Type*} [topological_space α] [topological_space β]
[non_unital_non_assoc_semiring β] [topological_semiring β] :
non_unital_non_assoc_semiring C(α, β) :=
coe_injective.non_unital_non_assoc_semiring _ coe_zero coe_add coe_mul coe_nsmul
instance {α : Type*} {β : Type*} [topological_space α] [topological_space β]
[non_unital_semiring β] [topological_semiring β] :
non_unital_semiring C(α, β) :=
coe_injective.non_unital_semiring _ coe_zero coe_add coe_mul coe_nsmul
instance {α : Type*} {β : Type*} [topological_space α] [topological_space β]
[add_monoid_with_one β] [has_continuous_add β] :
add_monoid_with_one C(α, β) :=
coe_injective.add_monoid_with_one _ coe_zero coe_one coe_add coe_nsmul coe_nat_cast
instance {α : Type*} {β : Type*} [topological_space α] [topological_space β]
[non_assoc_semiring β] [topological_semiring β] :
non_assoc_semiring C(α, β) :=
coe_injective.non_assoc_semiring _ coe_zero coe_one coe_add coe_mul coe_nsmul coe_nat_cast
instance {α : Type*} {β : Type*} [topological_space α] [topological_space β]
[semiring β] [topological_semiring β] : semiring C(α, β) :=
coe_injective.semiring _ coe_zero coe_one coe_add coe_mul coe_nsmul coe_pow coe_nat_cast
instance {α : Type*} {β : Type*} [topological_space α] [topological_space β]
[non_unital_non_assoc_ring β] [topological_ring β] : non_unital_non_assoc_ring C(α, β) :=
coe_injective.non_unital_non_assoc_ring _ coe_zero coe_add coe_mul coe_neg coe_sub
coe_nsmul coe_zsmul
instance {α : Type*} {β : Type*} [topological_space α] [topological_space β]
[non_unital_ring β] [topological_ring β] : non_unital_ring C(α, β) :=
coe_injective.non_unital_ring _ coe_zero coe_add coe_mul coe_neg coe_sub coe_nsmul coe_zsmul
instance {α : Type*} {β : Type*} [topological_space α] [topological_space β]
[non_assoc_ring β] [topological_ring β] : non_assoc_ring C(α, β) :=
coe_injective.non_assoc_ring _ coe_zero coe_one coe_add coe_mul coe_neg coe_sub coe_nsmul coe_zsmul
coe_nat_cast coe_int_cast
instance {α : Type*} {β : Type*} [topological_space α] [topological_space β]
[ring β] [topological_ring β] : ring C(α, β) :=
coe_injective.ring _ coe_zero coe_one coe_add coe_mul coe_neg coe_sub coe_nsmul coe_zsmul coe_pow
coe_nat_cast coe_int_cast
instance {α : Type*} {β : Type*} [topological_space α] [topological_space β]
[non_unital_comm_semiring β] [topological_semiring β] : non_unital_comm_semiring C(α, β) :=
coe_injective.non_unital_comm_semiring _ coe_zero coe_add coe_mul coe_nsmul
instance {α : Type*} {β : Type*} [topological_space α]
[topological_space β] [comm_semiring β] [topological_semiring β] : comm_semiring C(α, β) :=
coe_injective.comm_semiring _ coe_zero coe_one coe_add coe_mul coe_nsmul coe_pow coe_nat_cast
instance {α : Type*} {β : Type*} [topological_space α] [topological_space β]
[non_unital_comm_ring β] [topological_ring β] : non_unital_comm_ring C(α, β) :=
coe_injective.non_unital_comm_ring _ coe_zero coe_add coe_mul coe_neg coe_sub coe_nsmul coe_zsmul
instance {α : Type*} {β : Type*} [topological_space α]
[topological_space β] [comm_ring β] [topological_ring β] : comm_ring C(α, β) :=
coe_injective.comm_ring _ coe_zero coe_one coe_add coe_mul coe_neg coe_sub coe_nsmul coe_zsmul
coe_pow coe_nat_cast coe_int_cast
/-- Composition on the left by a (continuous) homomorphism of topological semirings, as a
`ring_hom`. Similar to `ring_hom.comp_left`. -/
@[simps] protected def _root_.ring_hom.comp_left_continuous (α : Type*) {β : Type*} {γ : Type*}
[topological_space α] [topological_space β] [semiring β] [topological_semiring β]
[topological_space γ] [semiring γ] [topological_semiring γ] (g : β →+* γ) (hg : continuous g) :
C(α, β) →+* C(α, γ) :=
{ .. g.to_monoid_hom.comp_left_continuous α hg,
.. g.to_add_monoid_hom.comp_left_continuous α hg }
/-- Coercion to a function as a `ring_hom`. -/
@[simps]
def coe_fn_ring_hom {α : Type*} {β : Type*} [topological_space α] [topological_space β]
[semiring β] [topological_semiring β] : C(α, β) →+* (α → β) :=
{ to_fun := coe_fn,
..(coe_fn_monoid_hom : C(α, β) →* _),
..(coe_fn_add_monoid_hom : C(α, β) →+ _) }
end continuous_map
end ring_structure
local attribute [ext] subtype.eq
section module_structure
/-!
### Semiodule stucture
In this section we show that continuous functions valued in a topological module `M` over a
topological semiring `R` inherit the structure of a module.
-/
section subtype
variables (α : Type*) [topological_space α]
variables (R : Type*) [semiring R]
variables (M : Type*) [topological_space M] [add_comm_group M]
variables [module R M] [has_continuous_const_smul R M] [topological_add_group M]
/-- The `R`-submodule of continuous maps `α → M`. -/
def continuous_submodule : submodule R (α → M) :=
{ carrier := { f : α → M | continuous f },
smul_mem' := λ c f hf, hf.const_smul c,
..continuous_add_subgroup α M }
end subtype
namespace continuous_map
variables {α β : Type*} [topological_space α] [topological_space β]
{R R₁ : Type*}
{M : Type*} [topological_space M]
{M₂ : Type*} [topological_space M₂]
@[to_additive continuous_map.has_vadd]
instance [has_smul R M] [has_continuous_const_smul R M] : has_smul R C(α, M) :=
⟨λ r f, ⟨r • f, f.continuous.const_smul r⟩⟩
@[to_additive]
instance [locally_compact_space α] [has_smul R M] [has_continuous_const_smul R M] :
has_continuous_const_smul R C(α, M) :=
⟨λ γ, continuous_of_continuous_uncurry _ (continuous_eval'.const_smul γ)⟩
@[to_additive]
instance [locally_compact_space α] [topological_space R] [has_smul R M]
[has_continuous_smul R M] : has_continuous_smul R C(α, M) :=
⟨begin
refine continuous_of_continuous_uncurry _ _,
have h : continuous (λ x : (R × C(α, M)) × α, x.fst.snd x.snd) :=
continuous_eval'.comp (continuous_snd.prod_map continuous_id),
exact (continuous_fst.comp continuous_fst).smul h,
end⟩
@[simp, norm_cast, to_additive]
lemma coe_smul [has_smul R M] [has_continuous_const_smul R M]
(c : R) (f : C(α, M)) : ⇑(c • f) = c • f := rfl
@[to_additive]
lemma smul_apply [has_smul R M] [has_continuous_const_smul R M]
(c : R) (f : C(α, M)) (a : α) : (c • f) a = c • (f a) :=
rfl
@[simp, to_additive] lemma smul_comp [has_smul R M] [has_continuous_const_smul R M]
(r : R) (f : C(β, M)) (g : C(α, β)) :
(r • f).comp g = r • (f.comp g) :=
rfl
@[to_additive]
instance [has_smul R M] [has_continuous_const_smul R M]
[has_smul R₁ M] [has_continuous_const_smul R₁ M]
[smul_comm_class R R₁ M] : smul_comm_class R R₁ C(α, M) :=
{ smul_comm := λ _ _ _, ext $ λ _, smul_comm _ _ _ }
instance [has_smul R M] [has_continuous_const_smul R M]
[has_smul R₁ M] [has_continuous_const_smul R₁ M]
[has_smul R R₁] [is_scalar_tower R R₁ M] : is_scalar_tower R R₁ C(α, M) :=
{ smul_assoc := λ _ _ _, ext $ λ _, smul_assoc _ _ _ }
instance [has_smul R M] [has_smul Rᵐᵒᵖ M] [has_continuous_const_smul R M]
[is_central_scalar R M] : is_central_scalar R C(α, M) :=
{ op_smul_eq_smul := λ _ _, ext $ λ _, op_smul_eq_smul _ _ }
instance [monoid R] [mul_action R M] [has_continuous_const_smul R M] : mul_action R C(α, M) :=
function.injective.mul_action _ coe_injective coe_smul
instance [monoid R] [add_monoid M] [distrib_mul_action R M]
[has_continuous_add M] [has_continuous_const_smul R M] :
distrib_mul_action R C(α, M) :=
function.injective.distrib_mul_action coe_fn_add_monoid_hom coe_injective coe_smul
variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂]
variables [has_continuous_add M] [module R M] [has_continuous_const_smul R M]
variables [has_continuous_add M₂] [module R M₂] [has_continuous_const_smul R M₂]
instance module : module R C(α, M) :=
function.injective.module R coe_fn_add_monoid_hom coe_injective coe_smul
variables (R)
/-- Composition on the left by a continuous linear map, as a `linear_map`.
Similar to `linear_map.comp_left`. -/
@[simps] protected def _root_.continuous_linear_map.comp_left_continuous (α : Type*)
[topological_space α] (g : M →L[R] M₂) :
C(α, M) →ₗ[R] C(α, M₂) :=
{ map_smul' := λ c f, ext $ λ x, g.map_smul' c _,
.. g.to_linear_map.to_add_monoid_hom.comp_left_continuous α g.continuous }
/-- Coercion to a function as a `linear_map`. -/
@[simps]
def coe_fn_linear_map : C(α, M) →ₗ[R] (α → M) :=
{ to_fun := coe_fn,
map_smul' := coe_smul,
..(coe_fn_add_monoid_hom : C(α, M) →+ _) }
end continuous_map
end module_structure
section algebra_structure
/-!
### Algebra structure
In this section we show that continuous functions valued in a topological algebra `A` over a ring
`R` inherit the structure of an algebra. Note that the hypothesis that `A` is a topological algebra
is obtained by requiring that `A` be both a `has_continuous_smul` and a `topological_semiring`.-/
section subtype
variables {α : Type*} [topological_space α]
{R : Type*} [comm_semiring R]
{A : Type*} [topological_space A] [semiring A]
[algebra R A] [topological_semiring A]
/-- The `R`-subalgebra of continuous maps `α → A`. -/
def continuous_subalgebra : subalgebra R (α → A) :=
{ carrier := { f : α → A | continuous f },
algebra_map_mem' := λ r, (continuous_const : continuous $ λ (x : α), algebra_map R A r),
..continuous_subsemiring α A }
end subtype
section continuous_map
variables {α : Type*} [topological_space α]
{R : Type*} [comm_semiring R]
{A : Type*} [topological_space A] [semiring A]
[algebra R A] [topological_semiring A]
{A₂ : Type*} [topological_space A₂] [semiring A₂]
[algebra R A₂] [topological_semiring A₂]
/-- Continuous constant functions as a `ring_hom`. -/
def continuous_map.C : R →+* C(α, A) :=
{ to_fun := λ c : R, ⟨λ x: α, ((algebra_map R A) c), continuous_const⟩,
map_one' := by ext x; exact (algebra_map R A).map_one,
map_mul' := λ c₁ c₂, by ext x; exact (algebra_map R A).map_mul _ _,
map_zero' := by ext x; exact (algebra_map R A).map_zero,
map_add' := λ c₁ c₂, by ext x; exact (algebra_map R A).map_add _ _ }
@[simp] lemma continuous_map.C_apply (r : R) (a : α) : continuous_map.C r a = algebra_map R A r :=
rfl
instance continuous_map.algebra : algebra R C(α, A) :=
{ to_ring_hom := continuous_map.C,
commutes' := λ c f, by ext x; exact algebra.commutes' _ _,
smul_def' := λ c f, by ext x; exact algebra.smul_def' _ _, }
variables (R)
/-- Composition on the left by a (continuous) homomorphism of topological `R`-algebras, as an
`alg_hom`. Similar to `alg_hom.comp_left`. -/
@[simps] protected def alg_hom.comp_left_continuous {α : Type*} [topological_space α]
(g : A →ₐ[R] A₂) (hg : continuous g) :
C(α, A) →ₐ[R] C(α, A₂) :=
{ commutes' := λ c, continuous_map.ext $ λ _, g.commutes' _,
.. g.to_ring_hom.comp_left_continuous α hg }
variables (A)
/--
Precomposition of functions into a normed ring by a continuous map is an algebra homomorphism.
-/
@[simps] def continuous_map.comp_right_alg_hom {α β : Type*} [topological_space α]
[topological_space β] (f : C(α, β)) : C(β, A) →ₐ[R] C(α, A) :=
{ to_fun := λ g, g.comp f,
map_zero' := by { ext, refl, },
map_add' := λ g₁ g₂, by { ext, refl, },
map_one' := by { ext, refl, },
map_mul' := λ g₁ g₂, by { ext, refl, },
commutes' := λ r, by { ext, refl, }, }
variables {A}
/-- Coercion to a function as an `alg_hom`. -/
@[simps]
def continuous_map.coe_fn_alg_hom : C(α, A) →ₐ[R] (α → A) :=
{ to_fun := coe_fn,
commutes' := λ r, rfl,
..(continuous_map.coe_fn_ring_hom : C(α, A) →+* _) }
variables {R}
/--
A version of `separates_points` for subalgebras of the continuous functions,
used for stating the Stone-Weierstrass theorem.
-/
abbreviation subalgebra.separates_points (s : subalgebra R C(α, A)) : Prop :=
set.separates_points ((λ f : C(α, A), (f : α → A)) '' (s : set C(α, A)))
lemma subalgebra.separates_points_monotone :
monotone (λ s : subalgebra R C(α, A), s.separates_points) :=
λ s s' r h x y n,
begin
obtain ⟨f, m, w⟩ := h n,
rcases m with ⟨f, ⟨m, rfl⟩⟩,
exact ⟨_, ⟨f, ⟨r m, rfl⟩⟩, w⟩,
end
@[simp] lemma algebra_map_apply (k : R) (a : α) :
algebra_map R C(α, A) k a = k • 1 :=
by { rw algebra.algebra_map_eq_smul_one, refl, }
variables {𝕜 : Type*} [topological_space 𝕜]
/--
A set of continuous maps "separates points strongly"
if for each pair of distinct points there is a function with specified values on them.
We give a slightly unusual formulation, where the specified values are given by some
function `v`, and we ask `f x = v x ∧ f y = v y`. This avoids needing a hypothesis `x ≠ y`.
In fact, this definition would work perfectly well for a set of non-continuous functions,
but as the only current use case is in the Stone-Weierstrass theorem,
writing it this way avoids having to deal with casts inside the set.
(This may need to change if we do Stone-Weierstrass on non-compact spaces,
where the functions would be continuous functions vanishing at infinity.)
-/
def set.separates_points_strongly (s : set C(α, 𝕜)) : Prop :=
∀ (v : α → 𝕜) (x y : α), ∃ f ∈ s, (f x : 𝕜) = v x ∧ f y = v y
variables [field 𝕜] [topological_ring 𝕜]
/--
Working in continuous functions into a topological field,
a subalgebra of functions that separates points also separates points strongly.
By the hypothesis, we can find a function `f` so `f x ≠ f y`.
By an affine transformation in the field we can arrange so that `f x = a` and `f x = b`.
-/
lemma subalgebra.separates_points.strongly {s : subalgebra 𝕜 C(α, 𝕜)} (h : s.separates_points) :
(s : set C(α, 𝕜)).separates_points_strongly :=
λ v x y,
begin
by_cases n : x = y,
{ subst n,
refine ⟨_, ((v x) • 1 : s).prop, mul_one _, mul_one _⟩ },
obtain ⟨_, ⟨f, hf, rfl⟩, hxy⟩ := h n,
replace hxy : f x - f y ≠ 0 := sub_ne_zero_of_ne hxy,
let a := v x,
let b := v y,
let f' : s := ((b - a) * (f x - f y)⁻¹) • (algebra_map _ _ (f x) - ⟨f, hf⟩) + algebra_map _ _ a,
refine ⟨f', f'.prop, _, _⟩,
{ simp [f'], },
{ simp [f', inv_mul_cancel_right₀ hxy], },
end
end continuous_map
instance continuous_map.subsingleton_subalgebra (α : Type*) [topological_space α]
(R : Type*) [comm_semiring R] [topological_space R] [topological_semiring R]
[subsingleton α] : subsingleton (subalgebra R C(α, R)) :=
⟨λ s₁ s₂, begin
casesI is_empty_or_nonempty α,
{ haveI : subsingleton C(α, R) := fun_like.coe_injective.subsingleton,
exact subsingleton.elim _ _ },
{ inhabit α,
ext f,
have h : f = algebra_map R C(α, R) (f default),
{ ext x', simp only [mul_one, algebra.id.smul_eq_mul, algebra_map_apply], congr, },
rw h,
simp only [subalgebra.algebra_map_mem], },
end⟩
end algebra_structure
section module_over_continuous_functions
/-!
### Structure as module over scalar functions
If `M` is a module over `R`, then we show that the space of continuous functions from `α` to `M`
is naturally a module over the ring of continuous functions from `α` to `R`. -/
namespace continuous_map
instance has_smul' {α : Type*} [topological_space α]
{R : Type*} [semiring R] [topological_space R]
{M : Type*} [topological_space M] [add_comm_monoid M]
[module R M] [has_continuous_smul R M] :
has_smul C(α, R) C(α, M) :=
⟨λ f g, ⟨λ x, (f x) • (g x), (continuous.smul f.2 g.2)⟩⟩
instance module' {α : Type*} [topological_space α]
(R : Type*) [semiring R] [topological_space R] [topological_semiring R]
(M : Type*) [topological_space M] [add_comm_monoid M] [has_continuous_add M]
[module R M] [has_continuous_smul R M] :
module C(α, R) C(α, M) :=
{ smul := (•),
smul_add := λ c f g, by ext x; exact smul_add (c x) (f x) (g x),
add_smul := λ c₁ c₂ f, by ext x; exact add_smul (c₁ x) (c₂ x) (f x),
mul_smul := λ c₁ c₂ f, by ext x; exact mul_smul (c₁ x) (c₂ x) (f x),
one_smul := λ f, by ext x; exact one_smul R (f x),
zero_smul := λ f, by ext x; exact zero_smul _ _,
smul_zero := λ r, by ext x; exact smul_zero _, }
end continuous_map
end module_over_continuous_functions
/-!
We now provide formulas for `f ⊓ g` and `f ⊔ g`, where `f g : C(α, β)`,
in terms of `continuous_map.abs`.
-/
section
variables {R : Type*} [linear_ordered_field R]
-- TODO:
-- This lemma (and the next) could go all the way back in `algebra.order.field`,
-- except that it is tedious to prove without tactics.
-- Rather than stranding it at some intermediate location,
-- it's here, immediately prior to the point of use.
lemma min_eq_half_add_sub_abs_sub {x y : R} : min x y = 2⁻¹ * (x + y - |x - y|) :=
by cases le_total x y with h h; field_simp [h, abs_of_nonneg, abs_of_nonpos, mul_two]; abel
lemma max_eq_half_add_add_abs_sub {x y : R} : max x y = 2⁻¹ * (x + y + |x - y|) :=
by cases le_total x y with h h; field_simp [h, abs_of_nonneg, abs_of_nonpos, mul_two]; abel
end
namespace continuous_map
section lattice
variables {α : Type*} [topological_space α]
variables {β : Type*} [linear_ordered_field β] [topological_space β]
[order_topology β] [topological_ring β]
lemma inf_eq (f g : C(α, β)) : f ⊓ g = (2⁻¹ : β) • (f + g - |f - g|) :=
ext (λ x, by simpa using min_eq_half_add_sub_abs_sub)
-- Not sure why this is grosser than `inf_eq`:
lemma sup_eq (f g : C(α, β)) : f ⊔ g = (2⁻¹ : β) • (f + g + |f - g|) :=
ext (λ x, by simpa [mul_add] using @max_eq_half_add_add_abs_sub _ _ (f x) (g x))
end lattice
/-!
### Star structure
If `β` has a continuous star operation, we put a star structure on `C(α, β)` by using the
star operation pointwise.
If `β` is a ⋆-ring, then `C(α, β)` inherits a ⋆-ring structure.
If `β` is a ⋆-ring and a ⋆-module over `R`, then the space of continuous functions from `α` to `β`
is a ⋆-module over `R`.
-/
section star_structure
variables {R α β : Type*}
variables [topological_space α] [topological_space β]
section has_star
variables [has_star β] [has_continuous_star β]
instance : has_star C(α, β) :=
{ star := λ f, star_continuous_map.comp f }
@[simp] lemma coe_star (f : C(α, β)) : ⇑(star f) = star f := rfl
@[simp] lemma star_apply (f : C(α, β)) (x : α) : star f x = star (f x) := rfl
end has_star
instance [has_involutive_star β] [has_continuous_star β] : has_involutive_star C(α, β) :=
{ star_involutive := λ f, ext $ λ x, star_star _ }
instance [add_monoid β] [has_continuous_add β] [star_add_monoid β] [has_continuous_star β] :
star_add_monoid C(α, β) :=
{ star_add := λ f g, ext $ λ x, star_add _ _ }
instance [semigroup β] [has_continuous_mul β] [star_semigroup β] [has_continuous_star β] :
star_semigroup C(α, β) :=
{ star_mul := λ f g, ext $ λ x, star_mul _ _ }
instance [non_unital_semiring β] [topological_semiring β] [star_ring β] [has_continuous_star β] :
star_ring C(α, β) :=
{ ..continuous_map.star_add_monoid }
instance [has_star R] [has_star β] [has_smul R β] [star_module R β]
[has_continuous_star β] [has_continuous_const_smul R β] :
star_module R C(α, β) :=
{ star_smul := λ k f, ext $ λ x, star_smul _ _ }
end star_structure
variables {X Y Z : Type*} [topological_space X] [topological_space Y] [topological_space Z]
variables (𝕜 : Type*) [comm_semiring 𝕜]
variables (A : Type*) [topological_space A] [semiring A] [topological_semiring A] [star_ring A]
variables [has_continuous_star A] [algebra 𝕜 A]
/-- The functorial map taking `f : C(X, Y)` to `C(Y, A) →⋆ₐ[𝕜] C(X, A)` given by pre-composition
with the continuous function `f`. See `continuous_map.comp_monoid_hom'` and
`continuous_map.comp_add_monoid_hom'`, `continuous_map.comp_right_alg_hom` for bundlings of
pre-composition into a `monoid_hom`, an `add_monoid_hom` and an `alg_hom`, respectively, under
suitable assumptions on `A`. -/
@[simps] def comp_star_alg_hom' (f : C(X, Y)) : C(Y, A) →⋆ₐ[𝕜] C(X, A) :=
{ to_fun := λ g, g.comp f,
map_one' := one_comp _,
map_mul' := λ _ _, rfl,
map_zero' := zero_comp _,
map_add' := λ _ _, rfl,
commutes' := λ _, rfl,
map_star' := λ _, rfl }
/-- `continuous_map.comp_star_alg_hom'` sends the identity continuous map to the identity
`star_alg_hom` -/
lemma comp_star_alg_hom'_id :
comp_star_alg_hom' 𝕜 A (continuous_map.id X) = star_alg_hom.id 𝕜 C(X, A) :=
star_alg_hom.ext $ λ _, continuous_map.ext $ λ _, rfl
/-- `continuous_map.comp_star_alg_hom` is functorial. -/
lemma comp_star_alg_hom'_comp (g : C(Y, Z)) (f : C(X, Y)) :
comp_star_alg_hom' 𝕜 A (g.comp f) = (comp_star_alg_hom' 𝕜 A f).comp (comp_star_alg_hom' 𝕜 A g) :=
star_alg_hom.ext $ λ _, continuous_map.ext $ λ _, rfl
section periodicity
/-! ### Summing translates of a function -/
/-- Summing the translates of `f` by `ℤ • p` gives a map which is periodic with period `p`.
(This is true without any convergence conditions, since if the sum doesn't converge it is taken to
be the zero map, which is periodic.) -/
lemma periodic_tsum_comp_add_zsmul [locally_compact_space X] [add_comm_group X]
[topological_add_group X] [add_comm_monoid Y] [has_continuous_add Y] [t2_space Y]
(f : C(X, Y)) (p : X) :
function.periodic ⇑(∑' (n : ℤ), f.comp (continuous_map.add_right (n • p))) p :=
begin
intro x,
by_cases h : summable (λ n : ℤ, f.comp (continuous_map.add_right (n • p))),
{ convert congr_arg (λ f : C(X, Y), f x) ((equiv.add_right (1 : ℤ)).tsum_eq _) using 1,
simp_rw [←tsum_apply h, ←tsum_apply ((equiv.add_right (1 : ℤ)).summable_iff.mpr h),
equiv.coe_add_right, comp_apply, coe_add_right, add_one_zsmul, add_comm (_ • p) p,
←add_assoc] },
{ rw tsum_eq_zero_of_not_summable h,
simp only [coe_zero, pi.zero_apply] }
end
end periodicity
end continuous_map
namespace homeomorph
variables {X Y : Type*} [topological_space X] [topological_space Y]
variables (𝕜 : Type*) [comm_semiring 𝕜]
variables (A : Type*) [topological_space A] [semiring A] [topological_semiring A] [star_ring A]
variables [has_continuous_star A] [algebra 𝕜 A]
/-- `continuous_map.comp_star_alg_hom'` as a `star_alg_equiv` when the continuous map `f` is
actually a homeomorphism. -/
@[simps] def comp_star_alg_equiv' (f : X ≃ₜ Y) : C(Y, A) ≃⋆ₐ[𝕜] C(X, A) :=
{ to_fun := (f : C(X, Y)).comp_star_alg_hom' 𝕜 A,
inv_fun := (f.symm : C(Y, X)).comp_star_alg_hom' 𝕜 A,
left_inv := λ g, by simp only [continuous_map.comp_star_alg_hom'_apply, continuous_map.comp_assoc,
to_continuous_map_comp_symm, continuous_map.comp_id],
right_inv := λ g, by simp only [continuous_map.comp_star_alg_hom'_apply,
continuous_map.comp_assoc, symm_comp_to_continuous_map, continuous_map.comp_id],
map_smul' := λ k a, map_smul (f.to_continuous_map.comp_star_alg_hom' 𝕜 A) k a,
.. (f.to_continuous_map.comp_star_alg_hom' 𝕜 A) }
end homeomorph
|
ff24c5fe3c859724bf38aedc19f1b5eeee3ee722 | 4950bf76e5ae40ba9f8491647d0b6f228ddce173 | /src/linear_algebra/alternating.lean | ad7e4d0d482f3b9ea2c8cdbb23cea24f479ac8b0 | [
"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 | 11,179 | lean | /-
Copyright (c) 2020 Zhangir Azerbayev. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Eric Wieser, Zhangir Azerbayev
-/
import linear_algebra.multilinear
import group_theory.perm.sign
/-!
# Alternating Maps
We construct the bundled function `alternating_map`, which extends `multilinear_map` with all the
arguments of the same type.
## Main definitions
* `alternating_map R M N ι` is the space of `R`-linear alternating maps from `ι → M` to `N`.
* `f.map_eq_zero_of_eq` expresses that `f` is zero when two inputs are equal.
* `f.map_swap` expresses that `f` is negated when two inputs are swapped.
* `f.map_perm` expresses how `f` varies by a sign change under a permutation of its inputs.
* An `add_comm_monoid`, `add_comm_group`, and `semimodule` structure over `alternating_map`s that
matches the definitions over `multilinear_map`s.
## Implementation notes
`alternating_map` is defined in terms of `map_eq_zero_of_eq`, as this is easier to work with than
using `map_swap` as a definition, and does not require `has_neg N`.
-/
-- semiring / add_comm_monoid
variables {R : Type*} [semiring R]
variables {M : Type*} [add_comm_monoid M] [semimodule R M]
variables {N : Type*} [add_comm_monoid N] [semimodule R N]
-- semiring / add_comm_group
variables {L : Type*} [add_comm_group L] [semimodule R L]
-- ring / add_comm_group
variables {R' : Type*} [ring R']
variables {M' : Type*} [add_comm_group M'] [semimodule R' M']
variables {N' : Type*} [add_comm_group N'] [semimodule R' N']
variables {ι : Type*} [decidable_eq ι]
set_option old_structure_cmd true
section
variables (R M N ι)
/--
An alternating map is a multilinear map that vanishes when two of its arguments are equal.
-/
structure alternating_map extends multilinear_map R (λ i : ι, M) N :=
(map_eq_zero_of_eq' : ∀ (v : ι → M) (i j : ι) (h : v i = v j) (hij : i ≠ j), to_fun v = 0)
end
/-- The multilinear map associated to an alternating map -/
add_decl_doc alternating_map.to_multilinear_map
namespace alternating_map
variables (f f' : alternating_map R M N ι)
variables (g' : alternating_map R' M' N' ι)
variables (v : ι → M) (v' : ι → M')
open function
/-! Basic coercion simp lemmas, largely copied from `ring_hom` and `multilinear_map` -/
section coercions
instance : has_coe_to_fun (alternating_map R M N ι) := ⟨_, λ x, x.to_fun⟩
initialize_simps_projections alternating_map (to_fun → apply)
@[simp] lemma to_fun_eq_coe : f.to_fun = f := rfl
@[simp] lemma coe_mk (f : (ι → M) → N) (h₁ h₂ h₃) : ⇑(⟨f, h₁, h₂, h₃⟩ :
alternating_map R M N ι) = f := rfl
theorem congr_fun {f g : alternating_map R M N ι} (h : f = g) (x : ι → M) : f x = g x :=
congr_arg (λ h : alternating_map R M N ι, h x) h
theorem congr_arg (f : alternating_map R M N ι) {x y : ι → M} (h : x = y) : f x = f y :=
congr_arg (λ x : ι → M, f x) h
theorem coe_inj ⦃f g : alternating_map R M N ι⦄ (h : ⇑f = g) : f = g :=
by { cases f, cases g, cases h, refl }
@[ext] theorem ext {f f' : alternating_map R M N ι} (H : ∀ x, f x = f' x) : f = f' :=
coe_inj (funext H)
theorem ext_iff {f g : alternating_map R M N ι} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, ext h⟩
instance : has_coe (alternating_map R M N ι) (multilinear_map R (λ i : ι, M) N) :=
⟨λ x, x.to_multilinear_map⟩
@[simp, norm_cast] lemma coe_multilinear_map : ⇑(f : multilinear_map R (λ i : ι, M) N) = f := rfl
@[simp] lemma to_multilinear_map_eq_coe : f.to_multilinear_map = f := rfl
@[simp] lemma coe_multilinear_map_mk (f : (ι → M) → N) (h₁ h₂ h₃) :
((⟨f, h₁, h₂, h₃⟩ : alternating_map R M N ι) : multilinear_map R (λ i : ι, M) N) = ⟨f, h₁, h₂⟩ :=
rfl
end coercions
/-!
### Simp-normal forms of the structure fields
These are expressed in terms of `⇑f` instead of `f.to_fun`.
-/
@[simp] lemma map_add (i : ι) (x y : M) :
f (update v i (x + y)) = f (update v i x) + f (update v i y) :=
f.to_multilinear_map.map_add' v i x y
@[simp] lemma map_sub (i : ι) (x y : M') :
g' (update v' i (x - y)) = g' (update v' i x) - g' (update v' i y) :=
g'.to_multilinear_map.map_sub v' i x y
@[simp] lemma map_smul (i : ι) (r : R) (x : M) :
f (update v i (r • x)) = r • f (update v i x) :=
f.to_multilinear_map.map_smul' v i r x
@[simp] lemma map_eq_zero_of_eq (v : ι → M) {i j : ι} (h : v i = v j) (hij : i ≠ j) :
f v = 0 :=
f.map_eq_zero_of_eq' v i j h hij
/-!
### Algebraic structure inherited from `multilinear_map`
`alternating_map` carries the same `add_comm_monoid`, `add_comm_group`, and `semimodule` structure
as `multilinear_map`
-/
instance : has_add (alternating_map R M N ι) :=
⟨λ a b,
{ map_eq_zero_of_eq' :=
λ v i j h hij, by simp [a.map_eq_zero_of_eq v h hij, b.map_eq_zero_of_eq v h hij],
..(a + b : multilinear_map R (λ i : ι, M) N)}⟩
@[simp] lemma add_apply : (f + f') v = f v + f' v := rfl
instance : has_zero (alternating_map R M N ι) :=
⟨{map_eq_zero_of_eq' := λ v i j h hij, by simp,
..(0 : multilinear_map R (λ i : ι, M) N)}⟩
@[simp] lemma zero_apply : (0 : alternating_map R M N ι) v = 0 := rfl
instance : inhabited (alternating_map R M N ι) := ⟨0⟩
instance : add_comm_monoid (alternating_map R M N ι) :=
by refine {zero := 0, add := (+), ..};
intros; ext; simp [add_comm, add_left_comm]
instance : has_neg (alternating_map R' M' N' ι) :=
⟨λ f,
{ map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij],
..(-(f : multilinear_map R' (λ i : ι, M') N')) }⟩
@[simp] lemma neg_apply (m : ι → M') : (-g') m = - (g' m) := rfl
instance : add_comm_group (alternating_map R' M' N' ι) :=
by refine {zero := 0, add := (+), neg := has_neg.neg, ..alternating_map.add_comm_monoid, ..};
intros; ext; simp [add_comm, add_left_comm]
section semimodule
variables {S : Type*} [comm_semiring S] [algebra S R] [semimodule S N]
[is_scalar_tower S R N]
instance : has_scalar S (alternating_map R M N ι) :=
⟨λ c f,
{ map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij],
..((c • f : multilinear_map R (λ i : ι, M) N)) }⟩
@[simp] lemma smul_apply (f : alternating_map R M N ι) (c : S) (m : ι → M) :
(c • f) m = c • f m := rfl
/-- The space of multilinear maps over an algebra over `S` is a module over `S`, for the pointwise
addition and scalar multiplication. -/
instance : semimodule S (alternating_map R M N ι) :=
{ one_smul := λ f, ext $ λ x, one_smul _ _,
mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul _ _ _,
smul_zero := λ r, ext $ λ x, smul_zero _,
smul_add := λ r f₁ f₂, ext $ λ x, smul_add _ _ _,
add_smul := λ r₁ r₂ f, ext $ λ x, add_smul _ _ _,
zero_smul := λ f, ext $ λ x, zero_smul _ _ }
end semimodule
/-!
### Theorems specific to alternating maps
Various properties of reordered and repeated inputs which follow from
`alternating_map.map_eq_zero_of_eq`.
-/
lemma map_update_self {i j : ι} (hij : i ≠ j) :
f (function.update v i (v j)) = 0 :=
f.map_eq_zero_of_eq _ (by rw [function.update_same, function.update_noteq hij.symm]) hij
lemma map_update_update {i j : ι} (hij : i ≠ j) (m : M) :
f (function.update (function.update v i m) j m) = 0 :=
f.map_eq_zero_of_eq _
(by rw [function.update_same, function.update_noteq hij, function.update_same]) hij
lemma map_swap_add {i j : ι} (hij : i ≠ j) :
f (v ∘ equiv.swap i j) + f v = 0 :=
begin
rw equiv.comp_swap_eq_update,
convert f.map_update_update v hij (v i + v j),
simp [f.map_update_self _ hij,
f.map_update_self _ hij.symm,
function.update_comm hij (v i + v j) (v _) v,
function.update_comm hij.symm (v i) (v i) v],
end
lemma map_add_swap {i j : ι} (hij : i ≠ j) :
f v + f (v ∘ equiv.swap i j) = 0 :=
by { rw add_comm, exact f.map_swap_add v hij }
variable (g : alternating_map R M L ι)
lemma map_swap {i j : ι} (hij : i ≠ j) :
g (v ∘ equiv.swap i j) = - g v :=
eq_neg_of_add_eq_zero (g.map_swap_add v hij)
lemma map_perm [fintype ι] (v : ι → M) (σ : equiv.perm ι) :
g (v ∘ σ) = (equiv.perm.sign σ : ℤ) • g v :=
begin
apply equiv.perm.swap_induction_on' σ,
{ simp },
{ intros s x y hxy hI,
simpa [g.map_swap (v ∘ s) hxy, equiv.perm.sign_swap hxy] using hI, }
end
lemma map_congr_perm [fintype ι] (σ : equiv.perm ι) :
g v = (equiv.perm.sign σ : ℤ) • g (v ∘ σ) :=
by { rw [g.map_perm, smul_smul], simp }
end alternating_map
open_locale big_operators
namespace multilinear_map
open equiv
variables [fintype ι]
private lemma alternization_map_eq_zero_of_eq_aux
(m : multilinear_map R (λ i : ι, M) L)
(v : ι → M) (i j : ι) (i_ne_j : i ≠ j) (hv : v i = v j) :
∑ (σ : perm ι), (σ.sign : ℤ) • m.dom_dom_congr σ v = 0 :=
finset.sum_involution
(λ σ _, swap i j * σ)
(λ σ _, begin
convert add_right_neg (↑σ.sign • m.dom_dom_congr σ v),
rw [perm.sign_mul, perm.sign_swap i_ne_j, ←neg_smul,
multilinear_map.dom_dom_congr_apply, multilinear_map.dom_dom_congr_apply],
congr' 2,
{ simp },
{ ext, simp [apply_swap_eq_self hv] },
end)
(λ σ _ _, (not_congr swap_mul_eq_iff).mpr i_ne_j)
(λ σ _, finset.mem_univ _)
(λ σ _, swap_mul_involutive i j σ)
/-- Produce an `alternating_map` out of a `multilinear_map`, by summing over all argument
permutations. -/
def alternatization : multilinear_map R (λ i : ι, M) L →+ alternating_map R M L ι :=
{ to_fun := λ m,
{ to_fun := λ v, ∑ (σ : perm ι), (σ.sign : ℤ) • m.dom_dom_congr σ v,
map_add' := λ v i a b, by simp_rw [←finset.sum_add_distrib, multilinear_map.map_add, smul_add],
map_smul' := λ v i c a, by simp_rw [finset.smul_sum, multilinear_map.map_smul, smul_comm],
map_eq_zero_of_eq' := λ v i j hvij hij, alternization_map_eq_zero_of_eq_aux m v i j hij hvij },
map_add' := λ a b, begin
ext,
simp only [
finset.sum_add_distrib, smul_add, add_apply, dom_dom_congr_apply, alternating_map.add_apply,
alternating_map.coe_mk],
end,
map_zero' := begin
ext,
simp only [
dom_dom_congr_apply, alternating_map.zero_apply, finset.sum_const_zero, smul_zero,
alternating_map.coe_mk, zero_apply]
end }
lemma alternatization_apply (m : multilinear_map R (λ i : ι, M) L) (v : ι → M) :
alternatization m v = ∑ (σ : perm ι), (σ.sign : ℤ) • m.dom_dom_congr σ v := rfl
end multilinear_map
namespace alternating_map
/-- Alternatizing a multilinear map that is already alternating results in a scale factor of `n!`,
where `n` is the number of inputs. -/
lemma to_multilinear_map_alternization [fintype ι] (a : alternating_map R M L ι) :
a.to_multilinear_map.alternatization = nat.factorial (fintype.card ι) • a :=
begin
ext,
simp only [multilinear_map.alternatization_apply, map_perm, smul_smul, ←nat.smul_def, coe_mk,
smul_apply, add_monoid_hom.coe_mk, finset.sum_const, coe_multilinear_map, one_smul,
multilinear_map.dom_dom_congr_apply, int.units_coe_mul_self, to_multilinear_map_eq_coe,
finset.card_univ, fintype.card_perm],
end
end alternating_map
|
636a3892c4d6ae5a0db4d1482b976eb1d47ad630 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/abst.lean | 2ff0776ed98667620fe5c54d1c8fa68fac4d5aec | [
"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 | 534 | lean | import Lean.Expr
open Lean
def tst : IO Unit :=
do
let f := mkConst `f;
let x := mkFVar { name := `x };
let y := mkFVar { name := `y };
let t := mkApp (mkApp (mkApp f x) y) (mkApp f x);
IO.println t;
let p := t.abstract [x, y].toArray;
IO.println p;
IO.println $ p.instantiateRev #[x, y];
let a := mkConst `a;
let b := mkApp f (mkConst `b);
IO.println $ p.instantiateRev #[a, b];
IO.println $ p.instantiate #[a];
let p := t.abstractRange 1 #[x, y];
IO.println p;
let p := t.abstractRange 3 #[x, y];
IO.println p;
pure ()
#eval tst
|
678cd69d52cfcb7b7d76e4d0510237339e54b5b2 | ff5230333a701471f46c57e8c115a073ebaaa448 | /tests/lean/task.lean | 8f1b91f3d86fa7f2ec944aa6c28e5d325c0fa110 | [
"Apache-2.0"
] | permissive | stanford-cs242/lean | f81721d2b5d00bc175f2e58c57b710d465e6c858 | 7bd861261f4a37326dcf8d7a17f1f1f330e4548c | refs/heads/master | 1,600,957,431,849 | 1,576,465,093,000 | 1,576,465,093,000 | 225,779,423 | 0 | 3 | Apache-2.0 | 1,575,433,936,000 | 1,575,433,935,000 | null | UTF-8 | Lean | false | false | 253 | lean | run_cmd tactic.run_async (tactic.trace
"trace message from a different task")
def {u} foo {α : Type u} {n : ℕ} : array (0+n) α → array n α :=
if n = 0 then
λ v, cast (by async { simp }) v
else
λ v, cast (by async { simp }) v
#print foo
|
14af865cc43d96c778ed799cb4f3a7c67b837d35 | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/analysis/special_functions/exp_log.lean | dbf9777133820453e6dd48d9a78ee23cdc659d42 | [
"Apache-2.0"
] | permissive | dexmagic/mathlib | ff48eefc56e2412429b31d4fddd41a976eb287ce | 7a5d15a955a92a90e1d398b2281916b9c41270b2 | refs/heads/master | 1,693,481,322,046 | 1,633,360,193,000 | 1,633,360,193,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 38,019 | 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 analysis.calculus.inverse
import analysis.complex.real_deriv
import data.complex.exponential
/-!
# Complex and real exponential, real logarithm
## Main statements
This file establishes the basic analytical properties of the complex and real exponential functions
(continuity, differentiability, computation of the derivative).
It also contains the definition of the real logarithm function (as the inverse of the
exponential on `(0, +∞)`, extended to `ℝ` by setting `log (-x) = log x`) and its basic
properties (continuity, differentiability, formula for the derivative).
The complex logarithm is *not* defined in this file as it relies on trigonometric functions. See
instead `trigonometric.lean`.
## Tags
exp, log
-/
noncomputable theory
open finset filter metric asymptotics set function
open_locale classical topological_space
section continuity
namespace complex
variables {z y x : ℝ}
lemma exp_bound_sq (x z : ℂ) (hz : ∥z∥ ≤ 1) :
∥exp (x + z) - exp x - z • exp x∥ ≤ ∥exp x∥ * ∥z∥ ^ 2 :=
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 hz) (norm_nonneg _)
lemma locally_lipschitz_exp {r : ℝ} (hr_nonneg : 0 ≤ r) (hr_le : r ≤ 1) (x y : ℂ)
(hyx : ∥y - x∥ < r) :
∥exp y - exp x∥ ≤ (1 + r) * ∥exp x∥ * ∥y - x∥ :=
begin
have hy_eq : y = x + (y - x), by abel,
have hyx_sq_le : ∥y - x∥ ^ 2 ≤ r * ∥y - x∥,
{ rw pow_two,
exact mul_le_mul hyx.le le_rfl (norm_nonneg _) hr_nonneg, },
have h_sq : ∀ z, ∥z∥ ≤ 1 → ∥exp (x + z) - exp x∥ ≤ ∥z∥ * ∥exp x∥ + ∥exp x∥ * ∥z∥ ^ 2,
{ intros z hz,
have : ∥exp (x + z) - exp x - z • exp x∥ ≤ ∥exp x∥ * ∥z∥ ^ 2, from exp_bound_sq x z hz,
rw [← sub_le_iff_le_add', ← norm_smul z],
exact (norm_sub_norm_le _ _).trans this, },
calc ∥exp y - exp x∥ = ∥exp (x + (y - x)) - exp x∥ : by nth_rewrite 0 hy_eq
... ≤ ∥y - x∥ * ∥exp x∥ + ∥exp x∥ * ∥y - x∥ ^ 2 : h_sq (y - x) (hyx.le.trans hr_le)
... ≤ ∥y - x∥ * ∥exp x∥ + ∥exp x∥ * (r * ∥y - x∥) :
add_le_add_left (mul_le_mul le_rfl hyx_sq_le (sq_nonneg _) (norm_nonneg _)) _
... = (1 + r) * ∥exp x∥ * ∥y - x∥ : by ring,
end
@[continuity] lemma continuous_exp : continuous exp :=
continuous_iff_continuous_at.mpr $
λ x, continuous_at_of_locally_lipschitz zero_lt_one (2 * ∥exp x∥)
(locally_lipschitz_exp zero_le_one le_rfl x)
lemma continuous_on_exp {s : set ℂ} : continuous_on exp s :=
continuous_exp.continuous_on
end complex
section complex_continuous_exp_comp
variable {α : Type*}
open complex
lemma filter.tendsto.cexp {l : filter α} {f : α → ℂ} {z : ℂ} (hf : tendsto f l (𝓝 z)) :
tendsto (λ x, exp (f x)) l (𝓝 (exp z)) :=
(continuous_exp.tendsto _).comp hf
variables [topological_space α] {f : α → ℂ} {s : set α} {x : α}
lemma continuous_within_at.cexp (h : continuous_within_at f s x) :
continuous_within_at (λ y, exp (f y)) s x :=
h.cexp
lemma continuous_at.cexp (h : continuous_at f x) : continuous_at (λ y, exp (f y)) x :=
h.cexp
lemma continuous_on.cexp (h : continuous_on f s) : continuous_on (λ y, exp (f y)) s :=
λ x hx, (h x hx).cexp
lemma continuous.cexp (h : continuous f) : continuous (λ y, exp (f y)) :=
continuous_iff_continuous_at.2 $ λ x, h.continuous_at.cexp
end complex_continuous_exp_comp
namespace real
@[continuity] lemma continuous_exp : continuous exp :=
complex.continuous_re.comp (complex.continuous_exp.comp complex.continuous_of_real)
lemma continuous_on_exp {s : set ℝ} : continuous_on exp s :=
continuous_exp.continuous_on
end real
end continuity
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),
filter_upwards [metric.ball_mem_nhds (0 : ℂ) zero_lt_one],
simp only [metric.mem_ball, dist_zero_right, normed_field.norm_pow],
exact λ z hz, exp_bound_sq x z hz.le,
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 [iterate_succ_apply, deriv_exp, iter_deriv_exp n]
lemma times_cont_diff_exp : ∀ {n}, times_cont_diff ℂ n exp :=
begin
refine times_cont_diff_all_iff_nat.2 (λ n, _),
induction n with n ihn,
{ exact times_cont_diff_zero.2 continuous_exp },
{ rw times_cont_diff_succ_iff_deriv,
use differentiable_exp,
rwa deriv_exp }
end
lemma has_strict_deriv_at_exp (x : ℂ) : has_strict_deriv_at exp (exp x) x :=
times_cont_diff_exp.times_cont_diff_at.has_strict_deriv_at' (has_deriv_at_exp x) le_rfl
lemma has_strict_fderiv_at_exp_real (x : ℂ) :
has_strict_fderiv_at exp (exp x • (1 : ℂ →L[ℝ] ℂ)) x :=
(has_strict_deriv_at_exp x).complex_to_real_fderiv
lemma is_open_map_exp : is_open_map exp :=
open_map_of_strict_deriv has_strict_deriv_at_exp exp_ne_zero
end complex
section
variables {f : ℂ → ℂ} {f' x : ℂ} {s : set ℂ}
lemma has_strict_deriv_at.cexp (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') x :=
(complex.has_strict_deriv_at_exp (f x)).comp x hf
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 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
section
variables {f : ℝ → ℂ} {f' : ℂ} {x : ℝ} {s : set ℝ}
open complex
lemma has_strict_deriv_at.cexp_real (h : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, exp (f x)) (exp (f x) * f') x :=
(has_strict_fderiv_at_exp_real (f x)).comp_has_strict_deriv_at x h
lemma has_deriv_at.cexp_real (h : has_deriv_at f f' x) :
has_deriv_at (λ x, exp (f x)) (exp (f x) * f') x :=
(has_strict_fderiv_at_exp_real (f x)).has_fderiv_at.comp_has_deriv_at x h
lemma has_deriv_within_at.cexp_real (h : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, exp (f x)) (exp (f x) * f') s x :=
(has_strict_fderiv_at_exp_real (f x)).has_fderiv_at.comp_has_deriv_within_at x h
end
section
variables {E : Type*} [normed_group E] [normed_space ℂ E] {f : E → ℂ} {f' : E →L[ℂ] ℂ}
{x : E} {s : set E}
lemma has_strict_fderiv_at.cexp (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, complex.exp (f x)) (complex.exp (f x) • f') x :=
(complex.has_strict_deriv_at_exp (f x)).comp_has_strict_fderiv_at x hf
lemma has_fderiv_within_at.cexp (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, complex.exp (f x)) (complex.exp (f x) • f') s x :=
(complex.has_deriv_at_exp (f x)).comp_has_fderiv_within_at x hf
lemma has_fderiv_at.cexp (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, complex.exp (f x)) (complex.exp (f x) • f') x :=
has_fderiv_within_at_univ.1 $ hf.has_fderiv_within_at.cexp
lemma differentiable_within_at.cexp (hf : differentiable_within_at ℂ f s x) :
differentiable_within_at ℂ (λ x, complex.exp (f x)) s x :=
hf.has_fderiv_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_fderiv_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 times_cont_diff.cexp {n} (h : times_cont_diff ℂ n f) :
times_cont_diff ℂ n (λ x, complex.exp (f x)) :=
complex.times_cont_diff_exp.comp h
lemma times_cont_diff_at.cexp {n} (hf : times_cont_diff_at ℂ n f x) :
times_cont_diff_at ℂ n (λ x, complex.exp (f x)) x :=
complex.times_cont_diff_exp.times_cont_diff_at.comp x hf
lemma times_cont_diff_on.cexp {n} (hf : times_cont_diff_on ℂ n f s) :
times_cont_diff_on ℂ n (λ x, complex.exp (f x)) s :=
complex.times_cont_diff_exp.comp_times_cont_diff_on hf
lemma times_cont_diff_within_at.cexp {n} (hf : times_cont_diff_within_at ℂ n f s x) :
times_cont_diff_within_at ℂ n (λ x, complex.exp (f x)) s x :=
complex.times_cont_diff_exp.times_cont_diff_at.comp_times_cont_diff_within_at x hf
end
namespace real
variables {x y z : ℝ}
lemma has_strict_deriv_at_exp (x : ℝ) : has_strict_deriv_at exp (exp x) x :=
(complex.has_strict_deriv_at_exp x).real_of_complex
lemma has_deriv_at_exp (x : ℝ) : has_deriv_at exp (exp x) x :=
(complex.has_deriv_at_exp x).real_of_complex
lemma times_cont_diff_exp {n} : times_cont_diff ℝ n exp :=
complex.times_cont_diff_exp.real_of_complex
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 [iterate_succ_apply, deriv_exp, iter_deriv_exp n]
end real
section
/-! Register lemmas for the derivatives of the composition of `real.exp` with a differentiable
function, for standalone use and use with `simp`. -/
variables {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ}
lemma has_strict_deriv_at.exp (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, real.exp (f x)) (real.exp (f x) * f') x :=
(real.has_strict_deriv_at_exp (f x)).comp x hf
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 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
end
section
/-! Register lemmas for the derivatives of the composition of `real.exp` with a differentiable
function, for standalone use and use with `simp`. -/
variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {f' : E →L[ℝ] ℝ}
{x : E} {s : set E}
lemma times_cont_diff.exp {n} (hf : times_cont_diff ℝ n f) :
times_cont_diff ℝ n (λ x, real.exp (f x)) :=
real.times_cont_diff_exp.comp hf
lemma times_cont_diff_at.exp {n} (hf : times_cont_diff_at ℝ n f x) :
times_cont_diff_at ℝ n (λ x, real.exp (f x)) x :=
real.times_cont_diff_exp.times_cont_diff_at.comp x hf
lemma times_cont_diff_on.exp {n} (hf : times_cont_diff_on ℝ n f s) :
times_cont_diff_on ℝ n (λ x, real.exp (f x)) s :=
real.times_cont_diff_exp.comp_times_cont_diff_on hf
lemma times_cont_diff_within_at.exp {n} (hf : times_cont_diff_within_at ℝ n f s x) :
times_cont_diff_within_at ℝ n (λ x, real.exp (f x)) s x :=
real.times_cont_diff_exp.times_cont_diff_at.comp_times_cont_diff_within_at x hf
lemma has_fderiv_within_at.exp (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, real.exp (f x)) (real.exp (f x) • f') s x :=
(real.has_deriv_at_exp (f x)).comp_has_fderiv_within_at x hf
lemma has_fderiv_at.exp (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ x, real.exp (f x)) (real.exp (f x) • f') x :=
(real.has_deriv_at_exp (f x)).comp_has_fderiv_at x hf
lemma has_strict_fderiv_at.exp (hf : has_strict_fderiv_at f f' x) :
has_strict_fderiv_at (λ x, real.exp (f x)) (real.exp (f x) • f') x :=
(real.has_strict_deriv_at_exp (f x)).comp_has_strict_fderiv_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_fderiv_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_fderiv_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 fderiv_within_exp (hf : differentiable_within_at ℝ f s x)
(hxs : unique_diff_within_at ℝ s x) :
fderiv_within ℝ (λx, real.exp (f x)) s x = real.exp (f x) • (fderiv_within ℝ f s x) :=
hf.has_fderiv_within_at.exp.fderiv_within hxs
@[simp] lemma fderiv_exp (hc : differentiable_at ℝ f x) :
fderiv ℝ (λx, real.exp (f x)) x = real.exp (f x) • (fderiv ℝ f x) :=
hc.has_fderiv_at.exp.fderiv
end
namespace real
variables {x y z : ℝ}
/-- The real exponential function tends to `+∞` at `+∞`. -/
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 :=
eventually_at_top.2 ⟨0, λ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 `-∞` or, equivalently, `exp(-x)` tends to `0`
at `+∞` -/
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 real exponential function tends to `1` at `0`. -/
lemma tendsto_exp_nhds_0_nhds_1 : tendsto exp (𝓝 0) (𝓝 1) :=
by { convert continuous_exp.tendsto 0, simp }
lemma tendsto_exp_at_bot : tendsto exp at_bot (𝓝 0) :=
(tendsto_exp_neg_at_top_nhds_0.comp tendsto_neg_at_bot_at_top).congr $
λ x, congr_arg exp $ neg_neg x
lemma tendsto_exp_at_bot_nhds_within : tendsto exp at_bot (𝓝[Ioi 0] 0) :=
tendsto_inf.2 ⟨tendsto_exp_at_bot, tendsto_principal.2 $ eventually_of_forall exp_pos⟩
/-- `real.exp` as an order isomorphism between `ℝ` and `(0, +∞)`. -/
def exp_order_iso : ℝ ≃o Ioi (0 : ℝ) :=
strict_mono.order_iso_of_surjective _ (exp_strict_mono.cod_restrict exp_pos) $
(continuous_subtype_mk _ continuous_exp).surjective
(by simp only [tendsto_Ioi_at_top, subtype.coe_mk, tendsto_exp_at_top])
(by simp [tendsto_exp_at_bot_nhds_within])
@[simp] lemma coe_exp_order_iso_apply (x : ℝ) : (exp_order_iso x : ℝ) = exp x := rfl
@[simp] lemma coe_comp_exp_order_iso : coe ∘ exp_order_iso = exp := rfl
@[simp] lemma range_exp : range exp = Ioi 0 :=
by rw [← coe_comp_exp_order_iso, range_comp, exp_order_iso.range_eq, image_univ, subtype.range_coe]
@[simp] lemma map_exp_at_top : map exp at_top = at_top :=
by rw [← coe_comp_exp_order_iso, ← filter.map_map, order_iso.map_at_top, map_coe_Ioi_at_top]
@[simp] lemma comap_exp_at_top : comap exp at_top = at_top :=
by rw [← map_exp_at_top, comap_map exp_injective, map_exp_at_top]
@[simp] lemma tendsto_exp_comp_at_top {α : Type*} {l : filter α} {f : α → ℝ} :
tendsto (λ x, exp (f x)) l at_top ↔ tendsto f l at_top :=
by rw [← tendsto_comap_iff, comap_exp_at_top]
lemma tendsto_comp_exp_at_top {α : Type*} {l : filter α} {f : ℝ → α} :
tendsto (λ x, f (exp x)) at_top l ↔ tendsto f at_top l :=
by rw [← tendsto_map'_iff, map_exp_at_top]
@[simp] lemma map_exp_at_bot : map exp at_bot = 𝓝[Ioi 0] 0 :=
by rw [← coe_comp_exp_order_iso, ← filter.map_map, exp_order_iso.map_at_bot, ← map_coe_Ioi_at_bot]
lemma comap_exp_nhds_within_Ioi_zero : comap exp (𝓝[Ioi 0] 0) = at_bot :=
by rw [← map_exp_at_bot, comap_map exp_injective]
lemma tendsto_comp_exp_at_bot {α : Type*} {l : filter α} {f : ℝ → α} :
tendsto (λ x, f (exp x)) at_bot l ↔ tendsto f (𝓝[Ioi 0] 0) l :=
by rw [← map_exp_at_bot, tendsto_map'_iff]
/-- The real logarithm function, equal to the inverse of the exponential for `x > 0`,
to `log |x|` for `x < 0`, and to `0` for `0`. We use this unconventional extension to
`(-∞, 0]` as it gives the formula `log (x * y) = log x + log y` for all nonzero `x` and `y`, and
the derivative of `log` is `1/x` away from `0`. -/
@[pp_nodot] noncomputable def log (x : ℝ) : ℝ :=
if hx : x = 0 then 0 else exp_order_iso.symm ⟨|x|, abs_pos.2 hx⟩
lemma log_of_ne_zero (hx : x ≠ 0) : log x = exp_order_iso.symm ⟨|x|, abs_pos.2 hx⟩ := dif_neg hx
lemma log_of_pos (hx : 0 < x) : log x = exp_order_iso.symm ⟨x, hx⟩ :=
by { rw [log_of_ne_zero hx.ne'], congr, exact abs_of_pos hx }
lemma exp_log_eq_abs (hx : x ≠ 0) : exp (log x) = |x| :=
by rw [log_of_ne_zero hx, ← coe_exp_order_iso_apply, order_iso.apply_symm_apply, subtype.coe_mk]
lemma exp_log (hx : 0 < x) : exp (log x) = x :=
by { rw exp_log_eq_abs hx.ne', exact abs_of_pos hx }
lemma exp_log_of_neg (hx : x < 0) : exp (log x) = -x :=
by { rw exp_log_eq_abs (ne_of_lt hx), exact abs_of_neg hx }
@[simp] lemma log_exp (x : ℝ) : log (exp x) = x :=
exp_injective $ exp_log (exp_pos x)
lemma surj_on_log : surj_on log (Ioi 0) univ :=
λ x _, ⟨exp x, exp_pos x, log_exp x⟩
lemma log_surjective : surjective log :=
λ x, ⟨exp x, log_exp x⟩
@[simp] lemma range_log : range log = univ :=
log_surjective.range_eq
@[simp] lemma log_zero : log 0 = 0 := dif_pos rfl
@[simp] lemma log_one : log 1 = 0 :=
exp_injective $ by rw [exp_log zero_lt_one, exp_zero]
@[simp] lemma log_abs (x : ℝ) : log (|x|) = log x :=
begin
by_cases h : x = 0,
{ simp [h] },
{ rw [← exp_eq_exp, exp_log_eq_abs h, exp_log_eq_abs (abs_pos.2 h).ne', abs_abs] }
end
@[simp] lemma log_neg_eq_log (x : ℝ) : log (-x) = log x :=
by rw [← log_abs x, ← log_abs (-x), abs_neg]
lemma surj_on_log' : surj_on log (Iio 0) univ :=
λ x _, ⟨-exp x, neg_lt_zero.2 $ exp_pos x, by rw [log_neg_eq_log, log_exp]⟩
lemma log_mul (hx : x ≠ 0) (hy : y ≠ 0) : log (x * y) = log x + log y :=
exp_injective $
by rw [exp_log_eq_abs (mul_ne_zero hx hy), exp_add, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_mul]
lemma log_div (hx : x ≠ 0) (hy : y ≠ 0) : log (x / y) = log x - log y :=
exp_injective $
by rw [exp_log_eq_abs (div_ne_zero hx hy), exp_sub, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_div]
@[simp] lemma log_inv (x : ℝ) : log (x⁻¹) = -log x :=
begin
by_cases hx : x = 0, { simp [hx] },
rw [← exp_eq_exp, exp_log_eq_abs (inv_ne_zero hx), exp_neg, exp_log_eq_abs hx, abs_inv]
end
lemma log_le_log (h : 0 < x) (h₁ : 0 < y) : real.log x ≤ real.log y ↔ x ≤ y :=
by rw [← exp_le_exp, exp_log h, 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 (hx : 0 < x) : 0 < log x ↔ 1 < x :=
by { rw ← log_one, exact log_lt_log_iff zero_lt_one hx }
lemma log_pos (hx : 1 < x) : 0 < log x :=
(log_pos_iff (lt_trans zero_lt_one hx)).2 hx
lemma log_neg_iff (h : 0 < x) : log x < 0 ↔ x < 1 :=
by { rw ← log_one, exact log_lt_log_iff h zero_lt_one }
lemma log_neg (h0 : 0 < x) (h1 : x < 1) : log x < 0 := (log_neg_iff h0).2 h1
lemma log_nonneg_iff (hx : 0 < x) : 0 ≤ log x ↔ 1 ≤ x :=
by rw [← not_lt, log_neg_iff hx, not_lt]
lemma log_nonneg (hx : 1 ≤ x) : 0 ≤ log x :=
(log_nonneg_iff (zero_lt_one.trans_le hx)).2 hx
lemma log_nonpos_iff (hx : 0 < x) : log x ≤ 0 ↔ x ≤ 1 :=
by rw [← not_lt, log_pos_iff hx, not_lt]
lemma log_nonpos_iff' (hx : 0 ≤ x) : log x ≤ 0 ↔ x ≤ 1 :=
begin
rcases hx.eq_or_lt with (rfl|hx),
{ simp [le_refl, zero_le_one] },
exact log_nonpos_iff hx
end
lemma log_nonpos (hx : 0 ≤ x) (h'x : x ≤ 1) : log x ≤ 0 :=
(log_nonpos_iff' hx).2 h'x
lemma strict_mono_on_log : strict_mono_on log (set.Ioi 0) :=
λ x hx y hy hxy, log_lt_log hx hxy
lemma strict_anti_on_log : strict_anti_on log (set.Iio 0) :=
begin
rintros x (hx : x < 0) y (hy : y < 0) hxy,
rw [← log_abs y, ← log_abs x],
refine log_lt_log (abs_pos.2 hy.ne) _,
rwa [abs_of_neg hy, abs_of_neg hx, neg_lt_neg_iff]
end
lemma log_inj_on_pos : set.inj_on log (set.Ioi 0) :=
strict_mono_on_log.inj_on
lemma eq_one_of_pos_of_log_eq_zero {x : ℝ} (h₁ : 0 < x) (h₂ : log x = 0) : x = 1 :=
log_inj_on_pos (set.mem_Ioi.2 h₁) (set.mem_Ioi.2 zero_lt_one) (h₂.trans real.log_one.symm)
lemma log_ne_zero_of_pos_of_ne_one {x : ℝ} (hx_pos : 0 < x) (hx : x ≠ 1) : log x ≠ 0 :=
mt (eq_one_of_pos_of_log_eq_zero hx_pos) hx
/-- The real logarithm function tends to `+∞` at `+∞`. -/
lemma tendsto_log_at_top : tendsto log at_top at_top :=
tendsto_comp_exp_at_top.1 $ by simpa only [log_exp] using tendsto_id
lemma tendsto_log_nhds_within_zero : tendsto log (𝓝[{0}ᶜ] 0) at_bot :=
begin
rw [← (show _ = log, from funext log_abs)],
refine tendsto.comp _ tendsto_abs_nhds_within_zero,
simpa [← tendsto_comp_exp_at_bot] using tendsto_id
end
lemma continuous_on_log : continuous_on log {0}ᶜ :=
begin
rw [continuous_on_iff_continuous_restrict, restrict],
conv in (log _) { rw [log_of_ne_zero (show (x : ℝ) ≠ 0, from x.2)] },
exact exp_order_iso.symm.continuous.comp (continuous_subtype_mk _ continuous_subtype_coe.norm)
end
@[continuity] lemma continuous_log : continuous (λ x : {x : ℝ // x ≠ 0}, log x) :=
continuous_on_iff_continuous_restrict.1 $ continuous_on_log.mono $ λ x hx, hx
@[continuity] lemma continuous_log' : continuous (λ x : {x : ℝ // 0 < x}, log x) :=
continuous_on_iff_continuous_restrict.1 $ continuous_on_log.mono $ λ x hx, ne_of_gt hx
lemma continuous_at_log (hx : x ≠ 0) : continuous_at log x :=
(continuous_on_log x hx).continuous_at $ is_open.mem_nhds is_open_compl_singleton hx
@[simp] lemma continuous_at_log_iff : continuous_at log x ↔ x ≠ 0 :=
begin
refine ⟨_, continuous_at_log⟩,
rintros h rfl,
exact not_tendsto_nhds_of_tendsto_at_bot tendsto_log_nhds_within_zero _
(h.tendsto.mono_left inf_le_left)
end
lemma has_strict_deriv_at_log_of_pos (hx : 0 < x) : has_strict_deriv_at log x⁻¹ x :=
have has_strict_deriv_at log (exp $ log x)⁻¹ x,
from (has_strict_deriv_at_exp $ log x).of_local_left_inverse (continuous_at_log hx.ne')
(ne_of_gt $ exp_pos _) $ eventually.mono (lt_mem_nhds hx) @exp_log,
by rwa [exp_log hx] at this
lemma has_strict_deriv_at_log (hx : x ≠ 0) : has_strict_deriv_at log x⁻¹ x :=
begin
cases hx.lt_or_lt with hx hx,
{ convert (has_strict_deriv_at_log_of_pos (neg_pos.mpr hx)).comp x (has_strict_deriv_at_neg x),
{ ext y, exact (log_neg_eq_log y).symm },
{ field_simp [hx.ne] } },
{ exact has_strict_deriv_at_log_of_pos hx }
end
lemma has_deriv_at_log (hx : x ≠ 0) : has_deriv_at log x⁻¹ x :=
(has_strict_deriv_at_log hx).has_deriv_at
lemma differentiable_at_log (hx : x ≠ 0) : differentiable_at ℝ log x :=
(has_deriv_at_log hx).differentiable_at
lemma differentiable_on_log : differentiable_on ℝ log {0}ᶜ :=
λ x hx, (differentiable_at_log hx).differentiable_within_at
@[simp] lemma differentiable_at_log_iff : differentiable_at ℝ log x ↔ x ≠ 0 :=
⟨λ h, continuous_at_log_iff.1 h.continuous_at, differentiable_at_log⟩
lemma deriv_log (x : ℝ) : deriv log x = x⁻¹ :=
if hx : x = 0 then
by rw [deriv_zero_of_not_differentiable_at (mt differentiable_at_log_iff.1 (not_not.2 hx)), hx,
inv_zero]
else (has_deriv_at_log hx).deriv
@[simp] lemma deriv_log' : deriv log = has_inv.inv := funext deriv_log
lemma times_cont_diff_on_log {n : with_top ℕ} : times_cont_diff_on ℝ n log {0}ᶜ :=
begin
suffices : times_cont_diff_on ℝ ⊤ log {0}ᶜ, from this.of_le le_top,
refine (times_cont_diff_on_top_iff_deriv_of_open is_open_compl_singleton).2 _,
simp [differentiable_on_log, times_cont_diff_on_inv]
end
lemma times_cont_diff_at_log {n : with_top ℕ} : times_cont_diff_at ℝ n log x ↔ x ≠ 0 :=
⟨λ h, continuous_at_log_iff.1 h.continuous_at,
λ hx, (times_cont_diff_on_log x hx).times_cont_diff_at $
is_open.mem_nhds is_open_compl_singleton hx⟩
end real
section log_differentiable
open real
section continuity
variables {α : Type*}
lemma filter.tendsto.log {f : α → ℝ} {l : filter α} {x : ℝ} (h : tendsto f l (𝓝 x)) (hx : x ≠ 0) :
tendsto (λ x, log (f x)) l (𝓝 (log x)) :=
(continuous_at_log hx).tendsto.comp h
variables [topological_space α] {f : α → ℝ} {s : set α} {a : α}
lemma continuous.log (hf : continuous f) (h₀ : ∀ x, f x ≠ 0) : continuous (λ x, log (f x)) :=
continuous_on_log.comp_continuous hf h₀
lemma continuous_at.log (hf : continuous_at f a) (h₀ : f a ≠ 0) :
continuous_at (λ x, log (f x)) a :=
hf.log h₀
lemma continuous_within_at.log (hf : continuous_within_at f s a) (h₀ : f a ≠ 0) :
continuous_within_at (λ x, log (f x)) s a :=
hf.log h₀
lemma continuous_on.log (hf : continuous_on f s) (h₀ : ∀ x ∈ s, f x ≠ 0) :
continuous_on (λ x, log (f x)) s :=
λ x hx, (hf x hx).log (h₀ x hx)
end continuity
section deriv
variables {f : ℝ → ℝ} {x f' : ℝ} {s : set ℝ}
lemma has_deriv_within_at.log (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) :
has_deriv_within_at (λ y, log (f y)) (f' / (f x)) s x :=
begin
rw div_eq_inv_mul,
exact (has_deriv_at_log hx).comp_has_deriv_within_at x hf
end
lemma has_deriv_at.log (hf : has_deriv_at f f' x) (hx : f x ≠ 0) :
has_deriv_at (λ y, log (f y)) (f' / f x) x :=
begin
rw ← has_deriv_within_at_univ at *,
exact hf.log hx
end
lemma has_strict_deriv_at.log (hf : has_strict_deriv_at f f' x) (hx : f x ≠ 0) :
has_strict_deriv_at (λ y, log (f y)) (f' / f x) x :=
begin
rw div_eq_inv_mul,
exact (has_strict_deriv_at_log hx).comp x hf
end
lemma deriv_within.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0)
(hxs : unique_diff_within_at ℝ s x) :
deriv_within (λx, log (f x)) s x = (deriv_within f s x) / (f x) :=
(hf.has_deriv_within_at.log hx).deriv_within hxs
@[simp] lemma deriv.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) :
deriv (λx, log (f x)) x = (deriv f x) / (f x) :=
(hf.has_deriv_at.log hx).deriv
end deriv
section fderiv
variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {x : E} {f' : E →L[ℝ] ℝ}
{s : set E}
lemma has_fderiv_within_at.log (hf : has_fderiv_within_at f f' s x) (hx : f x ≠ 0) :
has_fderiv_within_at (λ x, log (f x)) ((f x)⁻¹ • f') s x :=
(has_deriv_at_log hx).comp_has_fderiv_within_at x hf
lemma has_fderiv_at.log (hf : has_fderiv_at f f' x) (hx : f x ≠ 0) :
has_fderiv_at (λ x, log (f x)) ((f x)⁻¹ • f') x :=
(has_deriv_at_log hx).comp_has_fderiv_at x hf
lemma has_strict_fderiv_at.log (hf : has_strict_fderiv_at f f' x) (hx : f x ≠ 0) :
has_strict_fderiv_at (λ x, log (f x)) ((f x)⁻¹ • f') x :=
(has_strict_deriv_at_log hx).comp_has_strict_fderiv_at x hf
lemma differentiable_within_at.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) :
differentiable_within_at ℝ (λx, log (f x)) s x :=
(hf.has_fderiv_within_at.log hx).differentiable_within_at
@[simp] lemma differentiable_at.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) :
differentiable_at ℝ (λx, log (f x)) x :=
(hf.has_fderiv_at.log hx).differentiable_at
lemma times_cont_diff_at.log {n} (hf : times_cont_diff_at ℝ n f x) (hx : f x ≠ 0) :
times_cont_diff_at ℝ n (λ x, log (f x)) x :=
(times_cont_diff_at_log.2 hx).comp x hf
lemma times_cont_diff_within_at.log {n} (hf : times_cont_diff_within_at ℝ n f s x) (hx : f x ≠ 0) :
times_cont_diff_within_at ℝ n (λ x, log (f x)) s x :=
(times_cont_diff_at_log.2 hx).comp_times_cont_diff_within_at x hf
lemma times_cont_diff_on.log {n} (hf : times_cont_diff_on ℝ n f s) (hs : ∀ x ∈ s, f x ≠ 0) :
times_cont_diff_on ℝ n (λ x, log (f x)) s :=
λ x hx, (hf x hx).log (hs x hx)
lemma times_cont_diff.log {n} (hf : times_cont_diff ℝ n f) (h : ∀ x, f x ≠ 0) :
times_cont_diff ℝ n (λ x, log (f x)) :=
times_cont_diff_iff_times_cont_diff_at.2 $ λ x, hf.times_cont_diff_at.log (h x)
lemma differentiable_on.log (hf : differentiable_on ℝ f s) (hx : ∀ x ∈ s, f x ≠ 0) :
differentiable_on ℝ (λx, log (f x)) s :=
λx h, (hf x h).log (hx x h)
@[simp] lemma differentiable.log (hf : differentiable ℝ f) (hx : ∀ x, f x ≠ 0) :
differentiable ℝ (λx, log (f x)) :=
λx, (hf x).log (hx x)
lemma fderiv_within.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0)
(hxs : unique_diff_within_at ℝ s x) :
fderiv_within ℝ (λx, log (f x)) s x = (f x)⁻¹ • fderiv_within ℝ f s x :=
(hf.has_fderiv_within_at.log hx).fderiv_within hxs
@[simp] lemma fderiv.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) :
fderiv ℝ (λx, log (f x)) x = (f x)⁻¹ • fderiv ℝ f x :=
(hf.has_fderiv_at.log hx).fderiv
end fderiv
end log_differentiable
namespace real
/-- The function `exp(x)/x^n` tends to `+∞` at `+∞`, for any natural number `n` -/
lemma tendsto_exp_div_pow_at_top (n : ℕ) : tendsto (λx, exp x / x^n) at_top at_top :=
begin
refine (at_top_basis_Ioi.tendsto_iff (at_top_basis' 1)).2 (λ C hC₁, _),
have hC₀ : 0 < C, from zero_lt_one.trans_le hC₁,
have : 0 < (exp 1 * C)⁻¹ := inv_pos.2 (mul_pos (exp_pos _) hC₀),
obtain ⟨N, hN⟩ : ∃ N, ∀ k ≥ N, (↑k ^ n : ℝ) / exp 1 ^ k < (exp 1 * C)⁻¹ :=
eventually_at_top.1 ((tendsto_pow_const_div_const_pow_of_one_lt n
(one_lt_exp_iff.2 zero_lt_one)).eventually (gt_mem_nhds this)),
simp only [← exp_nat_mul, mul_one, div_lt_iff, exp_pos, ← div_eq_inv_mul] at hN,
refine ⟨N, trivial, λ x hx, _⟩, rw mem_Ioi at hx,
have hx₀ : 0 < x, from N.cast_nonneg.trans_lt hx,
rw [mem_Ici, le_div_iff (pow_pos hx₀ _), ← le_div_iff' hC₀],
calc x ^ n ≤ ⌈x⌉₊ ^ n : pow_le_pow_of_le_left hx₀.le (le_nat_ceil _) _
... ≤ exp ⌈x⌉₊ / (exp 1 * C) : (hN _ (lt_nat_ceil.2 hx).le).le
... ≤ exp (x + 1) / (exp 1 * C) : div_le_div_of_le (mul_pos (exp_pos _) hC₀).le
(exp_le_exp.2 $ (nat_ceil_lt_add_one hx₀.le).le)
... = exp x / C : by rw [add_comm, exp_add, mul_div_mul_left _ _ (exp_pos _).ne']
end
/-- The function `x^n * exp(-x)` tends to `0` at `+∞`, 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 [comp_app, inv_eq_one_div, div_div_eq_mul_div, one_mul, div_eq_mul_inv, exp_neg]
/-- The function `(b * exp x + c) / (x ^ n)` tends to `+∞` at `+∞`, for any positive natural number
`n` and any real numbers `b` and `c` such that `b` is positive. -/
lemma tendsto_mul_exp_add_div_pow_at_top (b c : ℝ) (n : ℕ) (hb : 0 < b) (hn : 1 ≤ n) :
tendsto (λ x, (b * (exp x) + c) / (x^n)) at_top at_top :=
begin
refine tendsto.congr' (eventually_eq_of_mem (Ioi_mem_at_top 0) _)
(((tendsto_exp_div_pow_at_top n).const_mul_at_top hb).at_top_add
((tendsto_pow_neg_at_top hn).mul (@tendsto_const_nhds _ _ _ c _))),
intros x hx,
simp only [fpow_neg x n],
ring,
end
/-- The function `(x ^ n) / (b * exp x + c)` tends to `0` at `+∞`, for any positive natural number
`n` and any real numbers `b` and `c` such that `b` is nonzero. -/
lemma tendsto_div_pow_mul_exp_add_at_top (b c : ℝ) (n : ℕ) (hb : 0 ≠ b) (hn : 1 ≤ n) :
tendsto (λ x, x^n / (b * (exp x) + c)) at_top (𝓝 0) :=
begin
have H : ∀ d e, 0 < d → tendsto (λ (x:ℝ), x^n / (d * (exp x) + e)) at_top (𝓝 0),
{ intros b' c' h,
convert (tendsto_mul_exp_add_div_pow_at_top b' c' n h hn).inv_tendsto_at_top ,
ext x,
simpa only [pi.inv_apply] using inv_div.symm },
cases lt_or_gt_of_ne hb,
{ exact H b c h },
{ convert (H (-b) (-c) (neg_pos.mpr h)).neg,
{ ext x,
field_simp,
rw [← neg_add (b * exp x) c, neg_div_neg_eq] },
{ exact neg_zero.symm } },
end
/-- The function `x * log (1 + t / x)` tends to `t` at `+∞`. -/
lemma tendsto_mul_log_one_plus_div_at_top (t : ℝ) :
tendsto (λ x, x * log (1 + t / x)) at_top (𝓝 t) :=
begin
have h₁ : tendsto (λ h, h⁻¹ * log (1 + t * h)) (𝓝[{0}ᶜ] 0) (𝓝 t),
{ simpa [has_deriv_at_iff_tendsto_slope] using
((has_deriv_at_const _ 1).add ((has_deriv_at_id (0 : ℝ)).const_mul t)).log (by simp) },
have h₂ : tendsto (λ x : ℝ, x⁻¹) at_top (𝓝[{0}ᶜ] 0) :=
tendsto_inv_at_top_zero'.mono_right (nhds_within_mono _ (λ x hx, (set.mem_Ioi.mp hx).ne')),
convert h₁.comp h₂,
ext,
field_simp [mul_comm],
end
open_locale big_operators
/-- A crude lemma estimating the difference between `log (1-x)` and its Taylor series at `0`,
where the main point of the bound is that it tends to `0`. The goal is to deduce the series
expansion of the logarithm, in `has_sum_pow_div_log_of_abs_lt_1`.
-/
lemma abs_log_sub_add_sum_range_le {x : ℝ} (h : |x| < 1) (n : ℕ) :
|((∑ i in range n, x^(i+1)/(i+1)) + log (1-x))| ≤ (|x|)^(n+1) / (1 - |x|) :=
begin
/- For the proof, we show that the derivative of the function to be estimated is small,
and then apply the mean value inequality. -/
let F : ℝ → ℝ := λ x, ∑ i in range n, x^(i+1)/(i+1) + log (1-x),
-- First step: compute the derivative of `F`
have A : ∀ y ∈ Ioo (-1 : ℝ) 1, deriv F y = - (y^n) / (1 - y),
{ assume y hy,
have : (∑ i in range n, (↑i + 1) * y ^ i / (↑i + 1)) = (∑ i in range n, y ^ i),
{ congr' with i,
have : (i : ℝ) + 1 ≠ 0 := ne_of_gt (nat.cast_add_one_pos i),
field_simp [this, mul_comm] },
field_simp [F, this, ← geom_sum_def, geom_sum_eq (ne_of_lt hy.2),
sub_ne_zero_of_ne (ne_of_gt hy.2), sub_ne_zero_of_ne (ne_of_lt hy.2)],
ring },
-- second step: show that the derivative of `F` is small
have B : ∀ y ∈ Icc (-|x|) (|x|), |deriv F y| ≤ |x|^n / (1 - |x|),
{ assume y hy,
have : y ∈ Ioo (-(1 : ℝ)) 1 := ⟨lt_of_lt_of_le (neg_lt_neg h) hy.1, lt_of_le_of_lt hy.2 h⟩,
calc |deriv F y| = | -(y^n) / (1 - y)| : by rw [A y this]
... ≤ |x|^n / (1 - |x|) :
begin
have : |y| ≤ |x| := abs_le.2 hy,
have : 0 < 1 - |x|, by linarith,
have : 1 - |x| ≤ |1 - y| := le_trans (by linarith [hy.2]) (le_abs_self _),
simp only [← pow_abs, abs_div, abs_neg],
apply_rules [div_le_div, pow_nonneg, abs_nonneg, pow_le_pow_of_le_left]
end },
-- third step: apply the mean value inequality
have C : ∥F x - F 0∥ ≤ (|x|^n / (1 - |x|)) * ∥x - 0∥,
{ have : ∀ y ∈ Icc (- |x|) (|x|), differentiable_at ℝ F y,
{ assume y hy,
have : 1 - y ≠ 0 := sub_ne_zero_of_ne (ne_of_gt (lt_of_le_of_lt hy.2 h)),
simp [F, this] },
apply convex.norm_image_sub_le_of_norm_deriv_le this B (convex_Icc _ _) _ _,
{ simpa using abs_nonneg x },
{ simp [le_abs_self x, neg_le.mp (neg_le_abs_self x)] } },
-- fourth step: conclude by massaging the inequality of the third step
simpa [F, norm_eq_abs, div_mul_eq_mul_div, pow_succ'] using C
end
/-- Power series expansion of the logarithm around `1`. -/
theorem has_sum_pow_div_log_of_abs_lt_1 {x : ℝ} (h : |x| < 1) :
has_sum (λ (n : ℕ), x ^ (n + 1) / (n + 1)) (-log (1 - x)) :=
begin
rw summable.has_sum_iff_tendsto_nat,
show tendsto (λ (n : ℕ), ∑ (i : ℕ) in range n, x ^ (i + 1) / (i + 1)) at_top (𝓝 (-log (1 - x))),
{ rw [tendsto_iff_norm_tendsto_zero],
simp only [norm_eq_abs, sub_neg_eq_add],
refine squeeze_zero (λ n, abs_nonneg _) (abs_log_sub_add_sum_range_le h) _,
suffices : tendsto (λ (t : ℕ), |x| ^ (t + 1) / (1 - |x|)) at_top
(𝓝 (|x| * 0 / (1 - |x|))), by simpa,
simp only [pow_succ],
refine (tendsto_const_nhds.mul _).div_const,
exact tendsto_pow_at_top_nhds_0_of_lt_1 (abs_nonneg _) h },
show summable (λ (n : ℕ), x ^ (n + 1) / (n + 1)),
{ refine summable_of_norm_bounded _ (summable_geometric_of_lt_1 (abs_nonneg _) h) (λ i, _),
calc ∥x ^ (i + 1) / (i + 1)∥
= |x| ^ (i+1) / (i+1) :
begin
have : (0 : ℝ) ≤ i + 1 := le_of_lt (nat.cast_add_one_pos i),
rw [norm_eq_abs, abs_div, ← pow_abs, abs_of_nonneg this],
end
... ≤ |x| ^ (i+1) / (0 + 1) :
begin
apply_rules [div_le_div_of_le_left, pow_nonneg, abs_nonneg, add_le_add_right,
i.cast_nonneg],
norm_num,
end
... ≤ |x| ^ i :
by simpa [pow_succ'] using mul_le_of_le_one_right (pow_nonneg (abs_nonneg x) i) (le_of_lt h) }
end
end real
|
88d073fdbd2e47a450649768b6ea04f1c5d3ca72 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/group_theory/complement.lean | 4864f3a939d2fd40db709c226958d0a436741bda | [
"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 | 20,170 | lean | /-
Copyright (c) 2021 Thomas Browning. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Thomas Browning
-/
import group_theory.order_of_element
/-!
# Complements
In this file we define the complement of a subgroup.
## Main definitions
- `is_complement S T` where `S` and `T` are subsets of `G` states that every `g : G` can be
written uniquely as a product `s * t` for `s ∈ S`, `t ∈ T`.
- `left_transversals T` where `T` is a subset of `G` is the set of all left-complements of `T`,
i.e. the set of all `S : set G` that contain exactly one element of each left coset of `T`.
- `right_transversals S` where `S` is a subset of `G` is the set of all right-complements of `S`,
i.e. the set of all `T : set G` that contain exactly one element of each right coset of `S`.
## Main results
- `is_complement_of_coprime` : Subgroups of coprime order are complements.
-/
open_locale big_operators
namespace subgroup
variables {G : Type*} [group G] (H K : subgroup G) (S T : set G)
/-- `S` and `T` are complements if `(*) : S × T → G` is a bijection.
This notion generalizes left transversals, right transversals, and complementary subgroups. -/
@[to_additive "`S` and `T` are complements if `(*) : S × T → G` is a bijection"]
def is_complement : Prop := function.bijective (λ x : S × T, x.1.1 * x.2.1)
/-- `H` and `K` are complements if `(*) : H × K → G` is a bijection -/
@[to_additive "`H` and `K` are complements if `(*) : H × K → G` is a bijection"]
abbreviation is_complement' := is_complement (H : set G) (K : set G)
/-- The set of left-complements of `T : set G` -/
@[to_additive "The set of left-complements of `T : set G`"]
def left_transversals : set (set G) := {S : set G | is_complement S T}
/-- The set of right-complements of `S : set G` -/
@[to_additive "The set of right-complements of `S : set G`"]
def right_transversals : set (set G) := {T : set G | is_complement S T}
variables {H K S T}
@[to_additive] lemma is_complement'_def :
is_complement' H K ↔ is_complement (H : set G) (K : set G) := iff.rfl
@[to_additive] lemma is_complement_iff_exists_unique :
is_complement S T ↔ ∀ g : G, ∃! x : S × T, x.1.1 * x.2.1 = g :=
function.bijective_iff_exists_unique _
@[to_additive] lemma is_complement.exists_unique (h : is_complement S T) (g : G) :
∃! x : S × T, x.1.1 * x.2.1 = g :=
is_complement_iff_exists_unique.mp h g
@[to_additive] lemma is_complement'.symm (h : is_complement' H K) : is_complement' K H :=
begin
let ϕ : H × K ≃ K × H := equiv.mk (λ x, ⟨x.2⁻¹, x.1⁻¹⟩) (λ x, ⟨x.2⁻¹, x.1⁻¹⟩)
(λ x, prod.ext (inv_inv _) (inv_inv _)) (λ x, prod.ext (inv_inv _) (inv_inv _)),
let ψ : G ≃ G := equiv.mk (λ g : G, g⁻¹) (λ g : G, g⁻¹) inv_inv inv_inv,
suffices : ψ ∘ (λ x : H × K, x.1.1 * x.2.1) = (λ x : K × H, x.1.1 * x.2.1) ∘ ϕ,
{ rwa [is_complement'_def, is_complement, ←equiv.bijective_comp, ←this, equiv.comp_bijective] },
exact funext (λ x, mul_inv_rev _ _),
end
@[to_additive] lemma is_complement'_comm : is_complement' H K ↔ is_complement' K H :=
⟨is_complement'.symm, is_complement'.symm⟩
@[to_additive] lemma is_complement_top_singleton {g : G} : is_complement (⊤ : set G) {g} :=
⟨λ ⟨x, _, rfl⟩ ⟨y, _, rfl⟩ h, prod.ext (subtype.ext (mul_right_cancel h)) rfl,
λ x, ⟨⟨⟨x * g⁻¹, ⟨⟩⟩, g, rfl⟩, inv_mul_cancel_right x g⟩⟩
@[to_additive] lemma is_complement_singleton_top {g : G} : is_complement ({g} : set G) ⊤ :=
⟨λ ⟨⟨_, rfl⟩, x⟩ ⟨⟨_, rfl⟩, y⟩ h, prod.ext rfl (subtype.ext (mul_left_cancel h)),
λ x, ⟨⟨⟨g, rfl⟩, g⁻¹ * x, ⟨⟩⟩, mul_inv_cancel_left g x⟩⟩
@[to_additive] lemma is_complement_singleton_left {g : G} : is_complement {g} S ↔ S = ⊤ :=
begin
refine ⟨λ h, top_le_iff.mp (λ x hx, _), λ h, (congr_arg _ h).mpr is_complement_singleton_top⟩,
obtain ⟨⟨⟨z, rfl : z = g⟩, y, _⟩, hy⟩ := h.2 (g * x),
rwa ← mul_left_cancel hy,
end
@[to_additive] lemma is_complement_singleton_right {g : G} : is_complement S {g} ↔ S = ⊤ :=
begin
refine ⟨λ h, top_le_iff.mp (λ x hx, _), λ h, (congr_arg _ h).mpr is_complement_top_singleton⟩,
obtain ⟨y, hy⟩ := h.2 (x * g),
conv_rhs at hy { rw ← (show y.2.1 = g, from y.2.2) },
rw ← mul_right_cancel hy,
exact y.1.2,
end
@[to_additive] lemma is_complement_top_left : is_complement ⊤ S ↔ ∃ g : G, S = {g} :=
begin
refine ⟨λ h, set.exists_eq_singleton_iff_nonempty_subsingleton.mpr ⟨_, λ a ha b hb, _⟩, _⟩,
{ obtain ⟨a, ha⟩ := h.2 1,
exact ⟨a.2.1, a.2.2⟩ },
{ have : (⟨⟨_, mem_top a⁻¹⟩, ⟨a, ha⟩⟩ : (⊤ : set G) × S) = ⟨⟨_, mem_top b⁻¹⟩, ⟨b, hb⟩⟩ :=
h.1 ((inv_mul_self a).trans (inv_mul_self b).symm),
exact subtype.ext_iff.mp ((prod.ext_iff.mp this).2) },
{ rintro ⟨g, rfl⟩,
exact is_complement_top_singleton },
end
@[to_additive] lemma is_complement_top_right : is_complement S ⊤ ↔ ∃ g : G, S = {g} :=
begin
refine ⟨λ h, set.exists_eq_singleton_iff_nonempty_subsingleton.mpr ⟨_, λ a ha b hb, _⟩, _⟩,
{ obtain ⟨a, ha⟩ := h.2 1,
exact ⟨a.1.1, a.1.2⟩ },
{ have : (⟨⟨a, ha⟩, ⟨_, mem_top a⁻¹⟩⟩ : S × (⊤ : set G)) = ⟨⟨b, hb⟩, ⟨_, mem_top b⁻¹⟩⟩ :=
h.1 ((mul_inv_self a).trans (mul_inv_self b).symm),
exact subtype.ext_iff.mp ((prod.ext_iff.mp this).1) },
{ rintro ⟨g, rfl⟩,
exact is_complement_singleton_top },
end
@[to_additive] lemma is_complement'_top_bot : is_complement' (⊤ : subgroup G) ⊥ :=
is_complement_top_singleton
@[to_additive] lemma is_complement'_bot_top : is_complement' (⊥ : subgroup G) ⊤ :=
is_complement_singleton_top
@[simp, to_additive] lemma is_complement'_bot_left : is_complement' ⊥ H ↔ H = ⊤ :=
is_complement_singleton_left.trans coe_eq_univ
@[simp, to_additive] lemma is_complement'_bot_right : is_complement' H ⊥ ↔ H = ⊤ :=
is_complement_singleton_right.trans coe_eq_univ
@[simp, to_additive] lemma is_complement'_top_left : is_complement' ⊤ H ↔ H = ⊥ :=
is_complement_top_left.trans coe_eq_singleton
@[simp, to_additive] lemma is_complement'_top_right : is_complement' H ⊤ ↔ H = ⊥ :=
is_complement_top_right.trans coe_eq_singleton
@[to_additive] lemma mem_left_transversals_iff_exists_unique_inv_mul_mem :
S ∈ left_transversals T ↔ ∀ g : G, ∃! s : S, (s : G)⁻¹ * g ∈ T :=
begin
rw [left_transversals, set.mem_set_of_eq, is_complement_iff_exists_unique],
refine ⟨λ h g, _, λ h g, _⟩,
{ obtain ⟨x, h1, h2⟩ := h g,
exact ⟨x.1, (congr_arg (∈ T) (eq_inv_mul_of_mul_eq h1)).mp x.2.2, λ y hy,
(prod.ext_iff.mp (h2 ⟨y, y⁻¹ * g, hy⟩ (mul_inv_cancel_left y g))).1⟩ },
{ obtain ⟨x, h1, h2⟩ := h g,
refine ⟨⟨x, x⁻¹ * g, h1⟩, mul_inv_cancel_left x g, λ y hy, _⟩,
have := h2 y.1 ((congr_arg (∈ T) (eq_inv_mul_of_mul_eq hy)).mp y.2.2),
exact prod.ext this (subtype.ext (eq_inv_mul_of_mul_eq ((congr_arg _ this).mp hy))) },
end
@[to_additive] lemma mem_right_transversals_iff_exists_unique_mul_inv_mem :
S ∈ right_transversals T ↔ ∀ g : G, ∃! s : S, g * (s : G)⁻¹ ∈ T :=
begin
rw [right_transversals, set.mem_set_of_eq, is_complement_iff_exists_unique],
refine ⟨λ h g, _, λ h g, _⟩,
{ obtain ⟨x, h1, h2⟩ := h g,
exact ⟨x.2, (congr_arg (∈ T) (eq_mul_inv_of_mul_eq h1)).mp x.1.2, λ y hy,
(prod.ext_iff.mp (h2 ⟨⟨g * y⁻¹, hy⟩, y⟩ (inv_mul_cancel_right g y))).2⟩ },
{ obtain ⟨x, h1, h2⟩ := h g,
refine ⟨⟨⟨g * x⁻¹, h1⟩, x⟩, inv_mul_cancel_right g x, λ y hy, _⟩,
have := h2 y.2 ((congr_arg (∈ T) (eq_mul_inv_of_mul_eq hy)).mp y.1.2),
exact prod.ext (subtype.ext (eq_mul_inv_of_mul_eq ((congr_arg _ this).mp hy))) this },
end
@[to_additive] lemma mem_left_transversals_iff_exists_unique_quotient_mk'_eq :
S ∈ left_transversals (H : set G) ↔
∀ q : quotient (quotient_group.left_rel H), ∃! s : S, quotient.mk' s.1 = q :=
begin
have key : ∀ g h, quotient.mk' g = quotient.mk' h ↔ g⁻¹ * h ∈ H :=
@quotient.eq' G (quotient_group.left_rel H),
simp_rw [mem_left_transversals_iff_exists_unique_inv_mul_mem, set_like.mem_coe, ←key],
exact ⟨λ h q, quotient.induction_on' q h, λ h g, h (quotient.mk' g)⟩,
end
@[to_additive] lemma mem_right_transversals_iff_exists_unique_quotient_mk'_eq :
S ∈ right_transversals (H : set G) ↔
∀ q : quotient (quotient_group.right_rel H), ∃! s : S, quotient.mk' s.1 = q :=
begin
have key : ∀ g h, quotient.mk' g = quotient.mk' h ↔ h * g⁻¹ ∈ H :=
@quotient.eq' G (quotient_group.right_rel H),
simp_rw [mem_right_transversals_iff_exists_unique_mul_inv_mem, set_like.mem_coe, ←key],
exact ⟨λ h q, quotient.induction_on' q h, λ h g, h (quotient.mk' g)⟩,
end
@[to_additive] lemma mem_left_transversals_iff_bijective : S ∈ left_transversals (H : set G) ↔
function.bijective (S.restrict (quotient.mk' : G → quotient (quotient_group.left_rel H))) :=
mem_left_transversals_iff_exists_unique_quotient_mk'_eq.trans
(function.bijective_iff_exists_unique (S.restrict quotient.mk')).symm
@[to_additive] lemma mem_right_transversals_iff_bijective : S ∈ right_transversals (H : set G) ↔
function.bijective (S.restrict (quotient.mk' : G → quotient (quotient_group.right_rel H))) :=
mem_right_transversals_iff_exists_unique_quotient_mk'_eq.trans
(function.bijective_iff_exists_unique (S.restrict quotient.mk')).symm
@[to_additive] lemma range_mem_left_transversals {f : G ⧸ H → G} (hf : ∀ q, ↑(f q) = q) :
set.range f ∈ left_transversals (H : set G) :=
mem_left_transversals_iff_bijective.mpr ⟨by rintros ⟨-, q₁, rfl⟩ ⟨-, q₂, rfl⟩ h;
exact congr_arg _ (((hf q₁).symm.trans h).trans (hf q₂)), λ q, ⟨⟨f q, q, rfl⟩, hf q⟩⟩
@[to_additive] lemma range_mem_right_transversals {f : quotient (quotient_group.right_rel H) → G}
(hf : ∀ q, quotient.mk' (f q) = q) : set.range f ∈ right_transversals (H : set G) :=
mem_right_transversals_iff_bijective.mpr ⟨by rintros ⟨-, q₁, rfl⟩ ⟨-, q₂, rfl⟩ h;
exact congr_arg _ (((hf q₁).symm.trans h).trans (hf q₂)), λ q, ⟨⟨f q, q, rfl⟩, hf q⟩⟩
@[to_additive] lemma exists_left_transversal (g : G) :
∃ S ∈ left_transversals (H : set G), g ∈ S :=
begin
classical,
refine ⟨set.range (function.update quotient.out' ↑g g), range_mem_left_transversals (λ q, _),
g, function.update_same g g quotient.out'⟩,
by_cases hq : q = g,
{ exact hq.symm ▸ congr_arg _ (function.update_same g g quotient.out') },
{ exact eq.trans (congr_arg _ (function.update_noteq hq g quotient.out')) q.out_eq' },
end
@[to_additive] lemma exists_right_transversal (g : G) :
∃ S ∈ right_transversals (H : set G), g ∈ S :=
begin
classical,
refine ⟨set.range (function.update quotient.out' _ g), range_mem_right_transversals (λ q, _),
quotient.mk' g, function.update_same (quotient.mk' g) g quotient.out'⟩,
by_cases hq : q = quotient.mk' g,
{ exact hq.symm ▸ congr_arg _ (function.update_same (quotient.mk' g) g quotient.out') },
{ exact eq.trans (congr_arg _ (function.update_noteq hq g quotient.out')) q.out_eq' },
end
namespace mem_left_transversals
/-- A left transversal is in bijection with left cosets. -/
@[to_additive "A left transversal is in bijection with left cosets."]
noncomputable def to_equiv (hS : S ∈ subgroup.left_transversals (H : set G)) : G ⧸ H ≃ S :=
(equiv.of_bijective _ (subgroup.mem_left_transversals_iff_bijective.mp hS)).symm
@[to_additive] lemma mk'_to_equiv (hS : S ∈ subgroup.left_transversals (H : set G)) (q : G ⧸ H) :
quotient.mk' (to_equiv hS q : G) = q :=
(to_equiv hS).symm_apply_apply q
@[to_additive] lemma to_equiv_apply {f : G ⧸ H → G} (hf : ∀ q, (f q : G ⧸ H) = q) (q : G ⧸ H) :
(to_equiv (range_mem_left_transversals hf) q : G) = f q :=
begin
refine (subtype.ext_iff.mp _).trans (subtype.coe_mk (f q) ⟨q, rfl⟩),
exact (to_equiv (range_mem_left_transversals hf)).apply_eq_iff_eq_symm_apply.mpr (hf q).symm,
end
/-- A left transversal can be viewed as a function mapping each element of the group
to the chosen representative from that left coset. -/
@[to_additive "A left transversal can be viewed as a function mapping each element of the group
to the chosen representative from that left coset."]
noncomputable def to_fun (hS : S ∈ subgroup.left_transversals (H : set G)) : G → S :=
to_equiv hS ∘ quotient.mk'
@[to_additive] lemma inv_to_fun_mul_mem (hS : S ∈ subgroup.left_transversals (H : set G))
(g : G) : (to_fun hS g : G)⁻¹ * g ∈ H :=
quotient.exact' (mk'_to_equiv hS g)
@[to_additive] lemma inv_mul_to_fun_mem (hS : S ∈ subgroup.left_transversals (H : set G))
(g : G) : g⁻¹ * to_fun hS g ∈ H :=
(congr_arg (∈ H) (by rw [mul_inv_rev, inv_inv])).mp (H.inv_mem (inv_to_fun_mul_mem hS g))
end mem_left_transversals
namespace mem_right_transversals
/-- A right transversal is in bijection with right cosets. -/
@[to_additive "A right transversal is in bijection with right cosets."]
noncomputable def to_equiv (hS : S ∈ subgroup.right_transversals (H : set G)) :
quotient (quotient_group.right_rel H) ≃ S :=
(equiv.of_bijective _ (subgroup.mem_right_transversals_iff_bijective.mp hS)).symm
@[to_additive] lemma mk'_to_equiv (hS : S ∈ subgroup.right_transversals (H : set G))
(q : quotient (quotient_group.right_rel H)) : quotient.mk' (to_equiv hS q : G) = q :=
(to_equiv hS).symm_apply_apply q
@[to_additive] lemma to_equiv_apply {f : quotient (quotient_group.right_rel H) → G}
(hf : ∀ q, quotient.mk' (f q) = q) (q : quotient (quotient_group.right_rel H)) :
(to_equiv (range_mem_right_transversals hf) q : G) = f q :=
begin
refine (subtype.ext_iff.mp _).trans (subtype.coe_mk (f q) ⟨q, rfl⟩),
exact (to_equiv (range_mem_right_transversals hf)).apply_eq_iff_eq_symm_apply.mpr (hf q).symm,
end
/-- A right transversal can be viewed as a function mapping each element of the group
to the chosen representative from that right coset. -/
@[to_additive "A right transversal can be viewed as a function mapping each element of the group
to the chosen representative from that right coset."]
noncomputable def to_fun (hS : S ∈ subgroup.right_transversals (H : set G)) : G → S :=
to_equiv hS ∘ quotient.mk'
@[to_additive] lemma mul_inv_to_fun_mem (hS : S ∈ subgroup.right_transversals (H : set G))
(g : G) : g * (to_fun hS g : G)⁻¹ ∈ H :=
quotient.exact' (mk'_to_equiv hS _)
@[to_additive] lemma to_fun_mul_inv_mem (hS : S ∈ subgroup.right_transversals (H : set G))
(g : G) : (to_fun hS g : G) * g⁻¹ ∈ H :=
(congr_arg (∈ H) (by rw [mul_inv_rev, inv_inv])).mp (H.inv_mem (mul_inv_to_fun_mem hS g))
end mem_right_transversals
section action
open_locale pointwise
open mul_action mem_left_transversals
variables {F : Type*} [group F] [mul_action F G] [quotient_action F H]
@[to_additive] instance : mul_action F (left_transversals (H : set G)) :=
{ smul := λ f T, ⟨f • T, by
{ refine mem_left_transversals_iff_exists_unique_inv_mul_mem.mpr (λ g, _),
obtain ⟨t, ht1, ht2⟩ := mem_left_transversals_iff_exists_unique_inv_mul_mem.mp T.2 (f⁻¹ • g),
refine ⟨⟨f • t, set.smul_mem_smul_set t.2⟩, _, _⟩,
{ exact (congr_arg _ (smul_inv_smul f g)).mp (quotient_action.inv_mul_mem f ht1) },
{ rintros ⟨-, t', ht', rfl⟩ h,
replace h := quotient_action.inv_mul_mem f⁻¹ h,
simp only [subtype.ext_iff, subtype.coe_mk, smul_left_cancel_iff, inv_smul_smul] at h ⊢,
exact subtype.ext_iff.mp (ht2 ⟨t', ht'⟩ h) } }⟩,
one_smul := λ T, subtype.ext (one_smul F T),
mul_smul := λ f₁ f₂ T, subtype.ext (mul_smul f₁ f₂ T) }
@[to_additive] lemma smul_to_fun (f : F) (T : left_transversals (H : set G)) (g : G) :
(f • to_fun T.2 g : G) = to_fun (f • T).2 (f • g) :=
subtype.ext_iff.mp $ @unique_of_exists_unique ↥(f • T) (λ s, (↑s)⁻¹ * f • g ∈ H)
(mem_left_transversals_iff_exists_unique_inv_mul_mem.mp (f • T).2 (f • g))
⟨f • to_fun T.2 g, set.smul_mem_smul_set (subtype.coe_prop _)⟩ (to_fun (f • T).2 (f • g))
(quotient_action.inv_mul_mem f (inv_to_fun_mul_mem T.2 g)) (inv_to_fun_mul_mem (f • T).2 (f • g))
@[to_additive] lemma smul_to_equiv (f : F) (T : left_transversals (H : set G)) (q : G ⧸ H) :
f • (to_equiv T.2 q : G) = to_equiv (f • T).2 (f • q) :=
quotient.induction_on' q (λ g, smul_to_fun f T g)
@[to_additive] lemma smul_apply_eq_smul_apply_inv_smul (f : F) (T : left_transversals (H : set G))
(q : G ⧸ H) : (to_equiv (f • T).2 q : G) = f • (to_equiv T.2 (f⁻¹ • q) : G) :=
by rw [smul_to_equiv, smul_inv_smul]
end action
@[to_additive] instance : inhabited (left_transversals (H : set G)) :=
⟨⟨set.range quotient.out', range_mem_left_transversals quotient.out_eq'⟩⟩
@[to_additive] instance : inhabited (right_transversals (H : set G)) :=
⟨⟨set.range quotient.out', range_mem_right_transversals quotient.out_eq'⟩⟩
lemma is_complement'.is_compl (h : is_complement' H K) : is_compl H K :=
begin
refine ⟨λ g ⟨p, q⟩, let x : H × K := ⟨⟨g, p⟩, 1⟩, y : H × K := ⟨1, g, q⟩ in subtype.ext_iff.mp
(prod.ext_iff.mp (show x = y, from h.1 ((mul_one g).trans (one_mul g).symm))).1, λ g _, _⟩,
obtain ⟨⟨h, k⟩, rfl⟩ := h.2 g,
exact subgroup.mul_mem_sup h.2 k.2,
end
lemma is_complement'.sup_eq_top (h : subgroup.is_complement' H K) : H ⊔ K = ⊤ :=
h.is_compl.sup_eq_top
lemma is_complement'.disjoint (h : is_complement' H K) : disjoint H K :=
h.is_compl.disjoint
lemma is_complement.card_mul [fintype G] [fintype S] [fintype T] (h : is_complement S T) :
fintype.card S * fintype.card T = fintype.card G :=
(fintype.card_prod _ _).symm.trans (fintype.card_of_bijective h)
lemma is_complement'.card_mul [fintype G] [fintype H] [fintype K] (h : is_complement' H K) :
fintype.card H * fintype.card K = fintype.card G :=
h.card_mul
lemma is_complement'_of_card_mul_and_disjoint [fintype G] [fintype H] [fintype K]
(h1 : fintype.card H * fintype.card K = fintype.card G) (h2 : disjoint H K) :
is_complement' H K :=
begin
refine (fintype.bijective_iff_injective_and_card _).mpr
⟨λ x y h, _, (fintype.card_prod H K).trans h1⟩,
rw [←eq_inv_mul_iff_mul_eq, ←mul_assoc, ←mul_inv_eq_iff_eq_mul] at h,
change ↑(x.2 * y.2⁻¹) = ↑(x.1⁻¹ * y.1) at h,
rw [prod.ext_iff, ←@inv_mul_eq_one H _ x.1 y.1, ←@mul_inv_eq_one K _ x.2 y.2, subtype.ext_iff,
subtype.ext_iff, coe_one, coe_one, h, and_self, ←mem_bot, ←h2.eq_bot, mem_inf],
exact ⟨subtype.mem ((x.1)⁻¹ * (y.1)), (congr_arg (∈ K) h).mp (subtype.mem (x.2 * (y.2)⁻¹))⟩,
end
lemma is_complement'_iff_card_mul_and_disjoint [fintype G] [fintype H] [fintype K] :
is_complement' H K ↔
fintype.card H * fintype.card K = fintype.card G ∧ disjoint H K :=
⟨λ h, ⟨h.card_mul, h.disjoint⟩, λ h, is_complement'_of_card_mul_and_disjoint h.1 h.2⟩
lemma is_complement'_of_coprime [fintype G] [fintype H] [fintype K]
(h1 : fintype.card H * fintype.card K = fintype.card G)
(h2 : nat.coprime (fintype.card H) (fintype.card K)) :
is_complement' H K :=
is_complement'_of_card_mul_and_disjoint h1 (disjoint_iff.mpr (inf_eq_bot_of_coprime h2))
lemma is_complement'_stabilizer {α : Type*} [mul_action G α] (a : α)
(h1 : ∀ (h : H), h • a = a → h = 1) (h2 : ∀ g : G, ∃ h : H, h • (g • a) = a) :
is_complement' H (mul_action.stabilizer G a) :=
begin
refine is_complement_iff_exists_unique.mpr (λ g, _),
obtain ⟨h, hh⟩ := h2 g,
have hh' : (↑h * g) • a = a := by rwa [mul_smul],
refine ⟨⟨h⁻¹, h * g, hh'⟩, inv_mul_cancel_left h g, _⟩,
rintros ⟨h', g, hg : g • a = a⟩ rfl,
specialize h1 (h * h') (by rwa [mul_smul, smul_def h', ←hg, ←mul_smul, hg]),
refine prod.ext (eq_inv_of_mul_eq_one_right h1) (subtype.ext _),
rwa [subtype.ext_iff, coe_one, coe_mul, ←self_eq_mul_left, mul_assoc ↑h ↑h' g] at h1,
end
end subgroup
|
4d93472ef5bb1e1e6e0ae91e1116558db31b3621 | 969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb | /src/category_theory/monoidal/category.lean | 2c6b1989c8850955bf594db0e2e57225c9749b53 | [
"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 | 21,013 | lean | /-
Copyright (c) 2018 Michael Jendrusch. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Michael Jendrusch, Scott Morrison
-/
import category_theory.products.basic
/-!
# Monoidal categories
A monoidal category is a category equipped with a tensor product, unitors, and an associator.
In the definition, we provide the tensor product as a pair of functions
* `tensor_obj : C → C → C`
* `tensor_hom : (X₁ ⟶ Y₁) → (X₂ ⟶ Y₂) → ((X₁ ⊗ X₂) ⟶ (Y₁ ⊗ Y₂))`
and allow use of the overloaded notation `⊗` for both.
The unitors and associator are provided componentwise.
The tensor product can be expressed as a functor via `tensor : C × C ⥤ C`.
The unitors and associator are gathered together as natural
isomorphisms in `left_unitor_nat_iso`, `right_unitor_nat_iso` and `associator_nat_iso`.
Some consequences of the definition are proved in other files,
e.g. `(λ_ (𝟙_ C)).hom = (ρ_ (𝟙_ C)).hom` in `category_theory.monoidal.unitors_equal`.
## Implementation
Dealing with unitors and associators is painful, and at this stage we do not have a useful
implementation of coherence for monoidal categories.
In an effort to lessen the pain, we put some effort into choosing the right `simp` lemmas.
Generally, the rule is that the component index of a natural transformation "weighs more"
in considering the complexity of an expression than does a structural isomorphism (associator, etc).
As an example when we prove Proposition 2.2.4 of
<http://www-math.mit.edu/~etingof/egnobookfinal.pdf>
we state it as a `@[simp]` lemma as
```
(λ_ (X ⊗ Y)).hom = (α_ (𝟙_ C) X Y).inv ≫ (λ_ X).hom ⊗ (𝟙 Y)
```
This is far from completely effective, but seems to prove a useful principle.
## References
* Tensor categories, Etingof, Gelaki, Nikshych, Ostrik,
http://www-math.mit.edu/~etingof/egnobookfinal.pdf
* https://stacks.math.columbia.edu/tag/0FFK.
-/
open category_theory
universes v u
open category_theory
open category_theory.category
open category_theory.iso
namespace category_theory
/--
In a monoidal category, we can take the tensor product of objects, `X ⊗ Y` and of morphisms `f ⊗ g`.
Tensor product does not need to be strictly associative on objects, but there is a
specified associator, `α_ X Y Z : (X ⊗ Y) ⊗ Z ≅ X ⊗ (Y ⊗ Z)`. There is a tensor unit `𝟙_ C`,
with specified left and right unitor isomorphisms `λ_ X : 𝟙_ C ⊗ X ≅ X` and `ρ_ X : X ⊗ 𝟙_ C ≅ X`.
These associators and unitors satisfy the pentagon and triangle equations.
See https://stacks.math.columbia.edu/tag/0FFK.
-/
class monoidal_category (C : Type u) [𝒞 : category.{v} C] :=
-- curried tensor product of objects:
(tensor_obj : C → C → C)
(infixr ` ⊗ `:70 := tensor_obj) -- This notation is only temporary
-- curried tensor product of morphisms:
(tensor_hom :
Π {X₁ Y₁ X₂ Y₂ : C}, (X₁ ⟶ Y₁) → (X₂ ⟶ Y₂) → ((X₁ ⊗ X₂) ⟶ (Y₁ ⊗ Y₂)))
(infixr ` ⊗' `:69 := tensor_hom) -- This notation is only temporary
-- tensor product laws:
(tensor_id' :
∀ (X₁ X₂ : C), (𝟙 X₁) ⊗' (𝟙 X₂) = 𝟙 (X₁ ⊗ X₂) . obviously)
(tensor_comp' :
∀ {X₁ Y₁ Z₁ X₂ Y₂ Z₂ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (g₁ : Y₁ ⟶ Z₁) (g₂ : Y₂ ⟶ Z₂),
(f₁ ≫ g₁) ⊗' (f₂ ≫ g₂) = (f₁ ⊗' f₂) ≫ (g₁ ⊗' g₂) . obviously)
-- tensor unit:
(tensor_unit [] : C)
(notation `𝟙_` := tensor_unit)
-- associator:
(associator :
Π X Y Z : C, (X ⊗ Y) ⊗ Z ≅ X ⊗ (Y ⊗ Z))
(notation `α_` := associator)
(associator_naturality' :
∀ {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃),
((f₁ ⊗' f₂) ⊗' f₃) ≫ (α_ Y₁ Y₂ Y₃).hom = (α_ X₁ X₂ X₃).hom ≫ (f₁ ⊗' (f₂ ⊗' f₃)) . obviously)
-- left unitor:
(left_unitor : Π X : C, 𝟙_ ⊗ X ≅ X)
(notation `λ_` := left_unitor)
(left_unitor_naturality' :
∀ {X Y : C} (f : X ⟶ Y), ((𝟙 𝟙_) ⊗' f) ≫ (λ_ Y).hom = (λ_ X).hom ≫ f . obviously)
-- right unitor:
(right_unitor : Π X : C, X ⊗ 𝟙_ ≅ X)
(notation `ρ_` := right_unitor)
(right_unitor_naturality' :
∀ {X Y : C} (f : X ⟶ Y), (f ⊗' (𝟙 𝟙_)) ≫ (ρ_ Y).hom = (ρ_ X).hom ≫ f . obviously)
-- pentagon identity:
(pentagon' : ∀ W X Y Z : C,
((α_ W X Y).hom ⊗' (𝟙 Z)) ≫ (α_ W (X ⊗ Y) Z).hom ≫ ((𝟙 W) ⊗' (α_ X Y Z).hom)
= (α_ (W ⊗ X) Y Z).hom ≫ (α_ W X (Y ⊗ Z)).hom . obviously)
-- triangle identity:
(triangle' :
∀ X Y : C, (α_ X 𝟙_ Y).hom ≫ ((𝟙 X) ⊗' (λ_ Y).hom) = (ρ_ X).hom ⊗' (𝟙 Y) . obviously)
restate_axiom monoidal_category.tensor_id'
attribute [simp] monoidal_category.tensor_id
restate_axiom monoidal_category.tensor_comp'
attribute [reassoc] monoidal_category.tensor_comp -- This would be redundant in the simp set.
attribute [simp] monoidal_category.tensor_comp
restate_axiom monoidal_category.associator_naturality'
attribute [reassoc] monoidal_category.associator_naturality
restate_axiom monoidal_category.left_unitor_naturality'
attribute [reassoc] monoidal_category.left_unitor_naturality
restate_axiom monoidal_category.right_unitor_naturality'
attribute [reassoc] monoidal_category.right_unitor_naturality
restate_axiom monoidal_category.pentagon'
restate_axiom monoidal_category.triangle'
attribute [simp, reassoc] monoidal_category.triangle
open monoidal_category
infixr ` ⊗ `:70 := tensor_obj
infixr ` ⊗ `:70 := tensor_hom
notation `𝟙_` := tensor_unit
notation `α_` := associator
notation `λ_` := left_unitor
notation `ρ_` := right_unitor
/-- The tensor product of two isomorphisms is an isomorphism. -/
@[simps]
def tensor_iso {C : Type u} {X Y X' Y' : C} [category.{v} C] [monoidal_category.{v} C]
(f : X ≅ Y) (g : X' ≅ Y') :
X ⊗ X' ≅ Y ⊗ Y' :=
{ hom := f.hom ⊗ g.hom,
inv := f.inv ⊗ g.inv,
hom_inv_id' := by rw [←tensor_comp, iso.hom_inv_id, iso.hom_inv_id, ←tensor_id],
inv_hom_id' := by rw [←tensor_comp, iso.inv_hom_id, iso.inv_hom_id, ←tensor_id] }
infixr ` ⊗ `:70 := tensor_iso
namespace monoidal_category
section
variables {C : Type u} [category.{v} C] [monoidal_category.{v} C]
instance tensor_is_iso {W X Y Z : C} (f : W ⟶ X) [is_iso f] (g : Y ⟶ Z) [is_iso g] :
is_iso (f ⊗ g) :=
is_iso.of_iso (as_iso f ⊗ as_iso g)
@[simp] lemma inv_tensor {W X Y Z : C} (f : W ⟶ X) [is_iso f] (g : Y ⟶ Z) [is_iso g] :
inv (f ⊗ g) = inv f ⊗ inv g :=
by { ext, simp [←tensor_comp], }
variables {U V W X Y Z : C}
-- When `rewrite_search` lands, add @[search] attributes to
-- monoidal_category.tensor_id monoidal_category.tensor_comp monoidal_category.associator_naturality
-- monoidal_category.left_unitor_naturality monoidal_category.right_unitor_naturality
-- monoidal_category.pentagon monoidal_category.triangle
-- tensor_comp_id tensor_id_comp comp_id_tensor_tensor_id
-- triangle_assoc_comp_left triangle_assoc_comp_right
-- triangle_assoc_comp_left_inv triangle_assoc_comp_right_inv
-- left_unitor_tensor left_unitor_tensor_inv
-- right_unitor_tensor right_unitor_tensor_inv
-- pentagon_inv
-- associator_inv_naturality
-- left_unitor_inv_naturality
-- right_unitor_inv_naturality
@[simp] lemma comp_tensor_id (f : W ⟶ X) (g : X ⟶ Y) :
(f ≫ g) ⊗ (𝟙 Z) = (f ⊗ (𝟙 Z)) ≫ (g ⊗ (𝟙 Z)) :=
by { rw ←tensor_comp, simp }
@[simp] lemma id_tensor_comp (f : W ⟶ X) (g : X ⟶ Y) :
(𝟙 Z) ⊗ (f ≫ g) = (𝟙 Z ⊗ f) ≫ (𝟙 Z ⊗ g) :=
by { rw ←tensor_comp, simp }
@[simp, reassoc] lemma id_tensor_comp_tensor_id (f : W ⟶ X) (g : Y ⟶ Z) :
((𝟙 Y) ⊗ f) ≫ (g ⊗ (𝟙 X)) = g ⊗ f :=
by { rw [←tensor_comp], simp }
@[simp, reassoc] lemma tensor_id_comp_id_tensor (f : W ⟶ X) (g : Y ⟶ Z) :
(g ⊗ (𝟙 W)) ≫ ((𝟙 Z) ⊗ f) = g ⊗ f :=
by { rw [←tensor_comp], simp }
lemma left_unitor_inv_naturality {X X' : C} (f : X ⟶ X') :
f ≫ (λ_ X').inv = (λ_ X).inv ≫ (𝟙 _ ⊗ f) :=
begin
apply (cancel_mono (λ_ X').hom).1,
simp only [assoc, comp_id, iso.inv_hom_id],
rw [left_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp]
end
lemma right_unitor_inv_naturality {X X' : C} (f : X ⟶ X') :
f ≫ (ρ_ X').inv = (ρ_ X).inv ≫ (f ⊗ 𝟙 _) :=
begin
apply (cancel_mono (ρ_ X').hom).1,
simp only [assoc, comp_id, iso.inv_hom_id],
rw [right_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp]
end
@[simp]
lemma right_unitor_conjugation {X Y : C} (f : X ⟶ Y) :
(ρ_ X).inv ≫ (f ⊗ (𝟙 (𝟙_ C))) ≫ (ρ_ Y).hom = f :=
by rw [right_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp]
@[simp]
lemma left_unitor_conjugation {X Y : C} (f : X ⟶ Y) :
(λ_ X).inv ≫ ((𝟙 (𝟙_ C)) ⊗ f) ≫ (λ_ Y).hom = f :=
by rw [left_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp]
@[simp] lemma tensor_left_iff
{X Y : C} (f g : X ⟶ Y) :
((𝟙 (𝟙_ C)) ⊗ f = (𝟙 (𝟙_ C)) ⊗ g) ↔ (f = g) :=
begin
split,
{ intro h,
have h' := congr_arg (λ k, (λ_ _).inv ≫ k) h,
dsimp at h',
rw [←left_unitor_inv_naturality, ←left_unitor_inv_naturality] at h',
exact (cancel_mono _).1 h', },
{ intro h, subst h, }
end
@[simp] lemma tensor_right_iff
{X Y : C} (f g : X ⟶ Y) :
(f ⊗ (𝟙 (𝟙_ C)) = g ⊗ (𝟙 (𝟙_ C))) ↔ (f = g) :=
begin
split,
{ intro h,
have h' := congr_arg (λ k, (ρ_ _).inv ≫ k) h,
dsimp at h',
rw [←right_unitor_inv_naturality, ←right_unitor_inv_naturality] at h',
exact (cancel_mono _).1 h' },
{ intro h, subst h, }
end
-- We now prove:
-- ((α_ (𝟙_ C) X Y).hom) ≫
-- ((λ_ (X ⊗ Y)).hom)
-- = ((λ_ X).hom ⊗ (𝟙 Y))
-- (and the corresponding fact for right unitors)
-- following the proof on nLab:
-- Lemma 2.2 at <https://ncatlab.org/nlab/revision/monoidal+category/115>
lemma left_unitor_product_aux_perimeter (X Y : C) :
((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y)) ≫
(α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom ≫
((𝟙 (𝟙_ C)) ⊗ (α_ (𝟙_ C) X Y).hom) ≫
((𝟙 (𝟙_ C)) ⊗ (λ_ (X ⊗ Y)).hom)
= (((ρ_ (𝟙_ C)).hom ⊗ (𝟙 X)) ⊗ (𝟙 Y)) ≫
(α_ (𝟙_ C) X Y).hom :=
begin
conv_lhs { congr, skip, rw [←category.assoc] },
rw [←category.assoc, monoidal_category.pentagon, associator_naturality, tensor_id,
←monoidal_category.triangle, ←category.assoc]
end
lemma left_unitor_product_aux_triangle (X Y : C) :
((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y)) ≫
(((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom) ⊗ (𝟙 Y))
= ((ρ_ (𝟙_ C)).hom ⊗ (𝟙 X)) ⊗ (𝟙 Y) :=
by rw [←comp_tensor_id, ←monoidal_category.triangle]
lemma left_unitor_product_aux_square (X Y : C) :
(α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom ≫
((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom ⊗ (𝟙 Y))
= (((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom) ⊗ (𝟙 Y)) ≫
(α_ (𝟙_ C) X Y).hom :=
by rw associator_naturality
lemma left_unitor_product_aux (X Y : C) :
((𝟙 (𝟙_ C)) ⊗ (α_ (𝟙_ C) X Y).hom) ≫
((𝟙 (𝟙_ C)) ⊗ (λ_ (X ⊗ Y)).hom)
= (𝟙 (𝟙_ C)) ⊗ ((λ_ X).hom ⊗ (𝟙 Y)) :=
begin
rw ←(cancel_epi (α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom),
rw left_unitor_product_aux_square,
rw ←(cancel_epi ((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y))),
slice_rhs 1 2 { rw left_unitor_product_aux_triangle },
conv_lhs { rw [left_unitor_product_aux_perimeter] }
end
lemma right_unitor_product_aux_perimeter (X Y : C) :
((α_ X Y (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫
(α_ X (Y ⊗ (𝟙_ C)) (𝟙_ C)).hom ≫
((𝟙 X) ⊗ (α_ Y (𝟙_ C) (𝟙_ C)).hom) ≫
((𝟙 X) ⊗ (𝟙 Y) ⊗ (λ_ (𝟙_ C)).hom)
= ((ρ_ (X ⊗ Y)).hom ⊗ (𝟙 (𝟙_ C))) ≫
(α_ X Y (𝟙_ C)).hom :=
begin
transitivity (((α_ X Y _).hom ⊗ 𝟙 _) ≫ (α_ X _ _).hom ≫
(𝟙 X ⊗ (α_ Y _ _).hom)) ≫
(𝟙 X ⊗ 𝟙 Y ⊗ (λ_ _).hom),
{ conv_lhs { congr, skip, rw [←category.assoc] },
conv_rhs { rw [category.assoc] } },
{ conv_lhs { congr, rw [monoidal_category.pentagon] },
conv_rhs { congr, rw [←monoidal_category.triangle] },
conv_rhs { rw [category.assoc] },
conv_rhs { congr, skip, congr, congr, rw [←tensor_id] },
conv_rhs { congr, skip, rw [associator_naturality] },
conv_rhs { rw [←category.assoc] } }
end
lemma right_unitor_product_aux_triangle (X Y : C) :
((𝟙 X) ⊗ (α_ Y (𝟙_ C) (𝟙_ C)).hom) ≫
((𝟙 X) ⊗ (𝟙 Y) ⊗ (λ_ (𝟙_ C)).hom)
= (𝟙 X) ⊗ (ρ_ Y).hom ⊗ (𝟙 (𝟙_ C)) :=
by rw [←id_tensor_comp, ←monoidal_category.triangle]
lemma right_unitor_product_aux_square (X Y : C) :
(α_ X (Y ⊗ (𝟙_ C)) (𝟙_ C)).hom ≫
((𝟙 X) ⊗ (ρ_ Y).hom ⊗ (𝟙 (𝟙_ C)))
= (((𝟙 X) ⊗ (ρ_ Y).hom) ⊗ (𝟙 (𝟙_ C))) ≫
(α_ X Y (𝟙_ C)).hom :=
by rw [associator_naturality]
lemma right_unitor_product_aux (X Y : C) :
((α_ X Y (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫
(((𝟙 X) ⊗ (ρ_ Y).hom) ⊗ (𝟙 (𝟙_ C)))
= ((ρ_ (X ⊗ Y)).hom ⊗ (𝟙 (𝟙_ C))) :=
begin
rw ←(cancel_mono (α_ X Y (𝟙_ C)).hom),
slice_lhs 2 3 { rw ←right_unitor_product_aux_square },
rw [←right_unitor_product_aux_triangle, ←right_unitor_product_aux_perimeter],
end
-- See Proposition 2.2.4 of <http://www-math.mit.edu/~etingof/egnobookfinal.pdf>
lemma left_unitor_tensor' (X Y : C) :
((α_ (𝟙_ C) X Y).hom) ≫ ((λ_ (X ⊗ Y)).hom) = ((λ_ X).hom ⊗ (𝟙 Y)) :=
by rw [←tensor_left_iff, id_tensor_comp, left_unitor_product_aux]
@[simp]
lemma left_unitor_tensor (X Y : C) :
((λ_ (X ⊗ Y)).hom) = ((α_ (𝟙_ C) X Y).inv) ≫ ((λ_ X).hom ⊗ (𝟙 Y)) :=
by { rw [←left_unitor_tensor'], simp }
lemma left_unitor_tensor_inv' (X Y : C) :
((λ_ (X ⊗ Y)).inv) ≫ ((α_ (𝟙_ C) X Y).inv) = ((λ_ X).inv ⊗ (𝟙 Y)) :=
eq_of_inv_eq_inv (by simp)
@[simp]
lemma left_unitor_tensor_inv (X Y : C) :
((λ_ (X ⊗ Y)).inv) = ((λ_ X).inv ⊗ (𝟙 Y)) ≫ ((α_ (𝟙_ C) X Y).hom) :=
by { rw [←left_unitor_tensor_inv'], simp }
@[simp]
lemma right_unitor_tensor (X Y : C) :
((ρ_ (X ⊗ Y)).hom) = ((α_ X Y (𝟙_ C)).hom) ≫ ((𝟙 X) ⊗ (ρ_ Y).hom) :=
by rw [←tensor_right_iff, comp_tensor_id, right_unitor_product_aux]
@[simp]
lemma right_unitor_tensor_inv (X Y : C) :
((ρ_ (X ⊗ Y)).inv) = ((𝟙 X) ⊗ (ρ_ Y).inv) ≫ ((α_ X Y (𝟙_ C)).inv) :=
eq_of_inv_eq_inv (by simp)
lemma associator_inv_naturality {X Y Z X' Y' Z' : C} (f : X ⟶ X') (g : Y ⟶ Y') (h : Z ⟶ Z') :
(f ⊗ (g ⊗ h)) ≫ (α_ X' Y' Z').inv = (α_ X Y Z).inv ≫ ((f ⊗ g) ⊗ h) :=
begin
apply (cancel_mono (α_ X' Y' Z').hom).1,
simp only [assoc, comp_id, iso.inv_hom_id],
rw [associator_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp]
end
lemma pentagon_inv (W X Y Z : C) :
((𝟙 W) ⊗ (α_ X Y Z).inv) ≫ (α_ W (X ⊗ Y) Z).inv ≫ ((α_ W X Y).inv ⊗ (𝟙 Z))
= (α_ W X (Y ⊗ Z)).inv ≫ (α_ (W ⊗ X) Y Z).inv :=
begin
apply category_theory.eq_of_inv_eq_inv,
simp [monoidal_category.pentagon]
end
lemma triangle_assoc_comp_left (X Y : C) :
(α_ X (𝟙_ C) Y).hom ≫ ((𝟙 X) ⊗ (λ_ Y).hom) = (ρ_ X).hom ⊗ 𝟙 Y :=
monoidal_category.triangle X Y
@[simp] lemma triangle_assoc_comp_right (X Y : C) :
(α_ X (𝟙_ C) Y).inv ≫ ((ρ_ X).hom ⊗ 𝟙 Y) = ((𝟙 X) ⊗ (λ_ Y).hom) :=
by rw [←triangle_assoc_comp_left, ←category.assoc, iso.inv_hom_id, category.id_comp]
@[simp] lemma triangle_assoc_comp_right_inv (X Y : C) :
((ρ_ X).inv ⊗ 𝟙 Y) ≫ (α_ X (𝟙_ C) Y).hom = ((𝟙 X) ⊗ (λ_ Y).inv) :=
begin
apply (cancel_mono (𝟙 X ⊗ (λ_ Y).hom)).1,
simp only [assoc, triangle_assoc_comp_left],
rw [←comp_tensor_id, iso.inv_hom_id, ←id_tensor_comp, iso.inv_hom_id]
end
@[simp] lemma triangle_assoc_comp_left_inv (X Y : C) :
((𝟙 X) ⊗ (λ_ Y).inv) ≫ (α_ X (𝟙_ C) Y).inv = ((ρ_ X).inv ⊗ 𝟙 Y) :=
begin
apply (cancel_mono ((ρ_ X).hom ⊗ 𝟙 Y)).1,
simp only [triangle_assoc_comp_right, assoc],
rw [←id_tensor_comp, iso.inv_hom_id, ←comp_tensor_id, iso.inv_hom_id]
end
end
section
variables (C : Type u) [category.{v} C] [monoidal_category.{v} C]
/-- The tensor product expressed as a functor. -/
def tensor : (C × C) ⥤ C :=
{ obj := λ X, X.1 ⊗ X.2,
map := λ {X Y : C × C} (f : X ⟶ Y), f.1 ⊗ f.2 }
/-- The left-associated triple tensor product as a functor. -/
def left_assoc_tensor : (C × C × C) ⥤ C :=
{ obj := λ X, (X.1 ⊗ X.2.1) ⊗ X.2.2,
map := λ {X Y : C × C × C} (f : X ⟶ Y), (f.1 ⊗ f.2.1) ⊗ f.2.2 }
@[simp] lemma left_assoc_tensor_obj (X) :
(left_assoc_tensor C).obj X = (X.1 ⊗ X.2.1) ⊗ X.2.2 := rfl
@[simp] lemma left_assoc_tensor_map {X Y} (f : X ⟶ Y) :
(left_assoc_tensor C).map f = (f.1 ⊗ f.2.1) ⊗ f.2.2 := rfl
/-- The right-associated triple tensor product as a functor. -/
def right_assoc_tensor : (C × C × C) ⥤ C :=
{ obj := λ X, X.1 ⊗ (X.2.1 ⊗ X.2.2),
map := λ {X Y : C × C × C} (f : X ⟶ Y), f.1 ⊗ (f.2.1 ⊗ f.2.2) }
@[simp] lemma right_assoc_tensor_obj (X) :
(right_assoc_tensor C).obj X = X.1 ⊗ (X.2.1 ⊗ X.2.2) := rfl
@[simp] lemma right_assoc_tensor_map {X Y} (f : X ⟶ Y) :
(right_assoc_tensor C).map f = f.1 ⊗ (f.2.1 ⊗ f.2.2) := rfl
/-- The functor `λ X, 𝟙_ C ⊗ X`. -/
def tensor_unit_left : C ⥤ C :=
{ obj := λ X, 𝟙_ C ⊗ X,
map := λ {X Y : C} (f : X ⟶ Y), (𝟙 (𝟙_ C)) ⊗ f }
/-- The functor `λ X, X ⊗ 𝟙_ C`. -/
def tensor_unit_right : C ⥤ C :=
{ obj := λ X, X ⊗ 𝟙_ C,
map := λ {X Y : C} (f : X ⟶ Y), f ⊗ (𝟙 (𝟙_ C)) }
-- We can express the associator and the unitors, given componentwise above,
-- as natural isomorphisms.
/-- The associator as a natural isomorphism. -/
@[simps]
def associator_nat_iso :
left_assoc_tensor C ≅ right_assoc_tensor C :=
nat_iso.of_components
(by { intros, apply monoidal_category.associator })
(by { intros, apply monoidal_category.associator_naturality })
/-- The left unitor as a natural isomorphism. -/
@[simps]
def left_unitor_nat_iso :
tensor_unit_left C ≅ 𝟭 C :=
nat_iso.of_components
(by { intros, apply monoidal_category.left_unitor })
(by { intros, apply monoidal_category.left_unitor_naturality })
/-- The right unitor as a natural isomorphism. -/
@[simps]
def right_unitor_nat_iso :
tensor_unit_right C ≅ 𝟭 C :=
nat_iso.of_components
(by { intros, apply monoidal_category.right_unitor })
(by { intros, apply monoidal_category.right_unitor_naturality })
section
variables {C}
/-- Tensoring on the left with a fixed object, as a functor. -/
@[simps]
def tensor_left (X : C) : C ⥤ C :=
{ obj := λ Y, X ⊗ Y,
map := λ Y Y' f, (𝟙 X) ⊗ f, }
/--
Tensoring on the left with `X ⊗ Y` is naturally isomorphic to
tensoring on the left with `Y`, and then again with `X`.
-/
def tensor_left_tensor (X Y : C) : tensor_left (X ⊗ Y) ≅ tensor_left Y ⋙ tensor_left X :=
nat_iso.of_components
(associator _ _)
(λ Z Z' f, by { dsimp, rw[←tensor_id], apply associator_naturality })
@[simp] lemma tensor_left_tensor_hom_app (X Y Z : C) :
(tensor_left_tensor X Y).hom.app Z = (associator X Y Z).hom :=
rfl
@[simp] lemma tensor_left_tensor_inv_app (X Y Z : C) :
(tensor_left_tensor X Y).inv.app Z = (associator X Y Z).inv :=
by { simp [tensor_left_tensor], }
/-- Tensoring on the right with a fixed object, as a functor. -/
@[simps]
def tensor_right (X : C) : C ⥤ C :=
{ obj := λ Y, Y ⊗ X,
map := λ Y Y' f, f ⊗ (𝟙 X), }
variables (C)
/--
Tensoring on the right, as a functor from `C` into endofunctors of `C`.
We later show this is a monoidal functor.
-/
@[simps]
def tensoring_right : C ⥤ (C ⥤ C) :=
{ obj := tensor_right,
map := λ X Y f,
{ app := λ Z, (𝟙 Z) ⊗ f } }
instance : faithful (tensoring_right C) :=
{ map_injective' := λ X Y f g h,
begin
injections with h,
replace h := congr_fun h (𝟙_ C),
simpa using h,
end }
variables {C}
/--
Tensoring on the right with `X ⊗ Y` is naturally isomorphic to
tensoring on the right with `X`, and then again with `Y`.
-/
def tensor_right_tensor (X Y : C) : tensor_right (X ⊗ Y) ≅ tensor_right X ⋙ tensor_right Y :=
nat_iso.of_components
(λ Z, (associator Z X Y).symm)
(λ Z Z' f, by { dsimp, rw[←tensor_id], apply associator_inv_naturality })
@[simp] lemma tensor_right_tensor_hom_app (X Y Z : C) :
(tensor_right_tensor X Y).hom.app Z = (associator Z X Y).inv :=
rfl
@[simp] lemma tensor_right_tensor_inv_app (X Y Z : C) :
(tensor_right_tensor X Y).inv.app Z = (associator Z X Y).hom :=
by simp [tensor_right_tensor]
end
end
end monoidal_category
end category_theory
|
1a106dc4ee9ddb56ea0499847b4a0576ae017e29 | 99b36386640f67a61e85dddccbc0f82edec9bf9c | /src/tactic_example.lean | 303958bf484db9496c23b2a143b2ced4a64cb5cf | [] | no_license | aymonwuolanne/lean-theorem-proving | 1ba46fb89f446b78b7f8f8cb084aad68fadce9a3 | 701a7873f9f66f2c24962b02450ec90b20935702 | refs/heads/master | 1,586,068,982,414 | 1,575,091,205,000 | 1,575,091,205,000 | 156,647,753 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 874 | lean | open tactic
-- meta def extract_equals (e : expr) : option (expr × expr) :=
-- match e with
-- | expr.app f x := sorry
-- end
meta def extract_equals : expr → tactic (option (expr × expr))
| (expr.app (expr.app f x) y) :=
do e ← mk_const `eq,
trace e,
trace f,
if f = e then
return (some (x, y))
else
return none
| _ := none
meta def extract_equals' : expr → option (expr × expr)
| `(%%a = %%b) := some (a, b)
| _ := none
meta def swp : tactic unit :=
do t ← target,
r ← extract_equals t,
trace r,
trace "hello world",
trace t
meta def swp' : tactic unit :=
do t ← target,
(a, b) ← extract_equals' t,
ng ← to_expr ``(%%b = %%a),
trace (a, b),
eqs ← to_expr ``(eq.symm),
apply eqs,
skip
#check expr
-- set_option pp.all true
lemma foo : 1 + 1 = 3 :=
begin
symmetry,
swp',
end |
339d8c04b832d70371f28b554c3c7f95341963c8 | 5e3548e65f2c037cb94cd5524c90c623fbd6d46a | /src_icannos_totilas/aops/2003-USAMO-Problem_5.lean | f00edbe63263fb55a4f5c41cf12edd6b6f955832 | [] | 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 | 197 | lean | import data.real.basic
theorem exo:
(forall (a b c: real), ((2*a + b + c)^2)/(2*a^2 + (b + c)^2) + ((2*b + c + a)^2)/(2*b^2 + (c + a)^2) + ((2*c + a + b)^2)/(2*c^2 + (a + b)^2) <= 8)
:=
sorry
|
2c1eafc9f50336d279909af8ac382f33a2d0e911 | 367134ba5a65885e863bdc4507601606690974c1 | /src/meta/expr_lens.lean | 068e1e29cfe333a944f99cf66b19aceda48aee14 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 5,726 | lean | /-
Copyright (c) 2020 Keeley Hoek. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Keeley Hoek, Scott Morrison
-/
import meta.expr
/-!
# A lens for zooming into nested `expr` applications
A "lens" for looking into the subterms of an expression, tracking where we've been, so that
when we "zoom out" after making a change we know exactly which order of `congr_fun`s and
`congr_arg`s we need to make things work.
This file defines the `expr_lens` inductive type, defines basic operations this type, and defines a
useful map-like function `expr.app_map` on `expr`s which maps over applications.
This file is for non-tactics.
## Tags
expr, expr_lens, congr, environment, meta, metaprogramming, tactic
-/
/-! ### Declarations about `expr_lens` -/
/-- You're supposed to think of an `expr_lens` as a big set of nested applications with a single
hole which needs to be filled, either in a function spot or argument spot. `expr_lens.fill` can
fill this hole and turn your lens back into a real `expr`. -/
meta inductive expr_lens
| app_fun : expr_lens → expr → expr_lens
| app_arg : expr_lens → expr → expr_lens
| entire : expr_lens
namespace expr_lens
/-- Inductive type with two constructors `F` and `A`,
that represent the function-part `f` and arg-part `a` of an application `f a`. They specify the
directions in which an `expr_lens` should zoom into an `expr`.
This type is used in the development of rewriting tactics such as `nth_rewrite` and
`rewrite_search`. -/
@[derive [decidable_eq, inhabited]]
inductive dir
| F
| A
/-- String representation of `dir`. -/
def dir.to_string : dir → string
| dir.F := "F"
| dir.A := "A"
instance : has_to_string dir := ⟨dir.to_string⟩
open tactic
/-- Fill the function or argument hole in this lens with the given `expr`. -/
meta def fill : expr_lens → expr → expr
| entire e := e
| (app_fun l f) x := l.fill (expr.app f x)
| (app_arg l x) f := l.fill (expr.app f x)
/-- Zoom into `e : expr` given the context of an `expr_lens`, popping out an `expr` and a new
zoomed `expr_lens`, if this is possible (`e` has to be an application). -/
meta def zoom : expr_lens → list dir → expr → option (expr_lens × expr)
| l [] e := (l, e)
| l (dir.F :: rest) (expr.app f x) := (expr_lens.app_arg l x).zoom rest f
| l (dir.A :: rest) (expr.app f x) := (expr_lens.app_fun l f).zoom rest x
| _ _ _ := none
/-- Convert an `expr_lens` into a list of instructions needed to build it; repeatedly inspecting a
function or its argument a finite number of times. -/
meta def to_dirs : expr_lens → list dir
| expr_lens.entire := []
| (expr_lens.app_fun l _) := l.to_dirs.concat dir.A
| (expr_lens.app_arg l _) := l.to_dirs.concat dir.F
/-- Sometimes `mk_congr_arg` fails, when the function is 'superficially dependent'.
Try to `dsimp` the function first before building the `congr_arg` expression. -/
meta def mk_congr_arg_using_dsimp (G W : expr) (u : list name) : tactic expr :=
do s ← simp_lemmas.mk_default,
t ← infer_type G,
t' ← s.dsimplify u t { fail_if_unchanged := ff },
to_expr ```(congr_arg (show %%t', from %%G) %%W)
private meta def trace_congr_error (f : expr) (x_eq : expr) : tactic unit :=
do pp_f ← pp f,
pp_f_t ← (infer_type f >>= λ t, pp t),
pp_x_eq ← pp x_eq,
pp_x_eq_t ← (infer_type x_eq >>= λ t, pp t),
trace format!"expr_lens.congr failed on \n{pp_f} : {pp_f_t}\n{pp_x_eq} : {pp_x_eq_t}"
/-- Turn an `e : expr_lens` and a proof that `a = b` into a series of `congr_arg` or `congr_fun`
applications showing that the expressions obtained from `e.fill a` and `e.fill b` are equal. -/
meta def congr : expr_lens → expr → tactic expr
| entire e_eq := pure e_eq
| (app_fun l f) x_eq := do fx_eq ← try_core $ do {
mk_congr_arg f x_eq
<|> mk_congr_arg_using_dsimp f x_eq [`has_coe_to_fun.F]
},
match fx_eq with
| (some fx_eq) := l.congr fx_eq
| none := trace_congr_error f x_eq >> failed
end
| (app_arg l x) f_eq := mk_congr_fun f_eq x >>= l.congr
/-- Pretty print a lens. -/
meta def to_tactic_string : expr_lens → tactic string
| entire := return "(entire)"
| (app_fun l f) := do pp ← pp f,
rest ← l.to_tactic_string,
return sformat!"(fun \"{pp}\" {rest})"
| (app_arg l x) := do pp ← pp x,
rest ← l.to_tactic_string,
return sformat!"(arg \"{pp}\" {rest})"
end expr_lens
namespace expr
/-- The private internal function used by `app_map`, which "does the work". -/
private meta def app_map_aux {α} (F : expr_lens → expr → tactic (list α)) :
option (expr_lens × expr) → tactic (list α)
| (some (l, e)) := list.join <$> monad.sequence [
F l e,
app_map_aux $ l.zoom [expr_lens.dir.F] e,
app_map_aux $ l.zoom [expr_lens.dir.A] e
] <|> pure []
| none := pure []
/-- `app_map F e` maps a function `F` which understands `expr_lens`es over the given `e : expr` in the natural way;
that is, make holes in `e` everywhere where that is possible (generating `expr_lens`es in the
process), and at each stage call the function `F` passing both the `expr_lens` generated and the
`expr` which was removed to make the hole.
At each stage `F` returns a list of some type, and `app_map` collects these lists together and
returns a concatenation of them all. -/
meta def app_map {α} (F : expr_lens → expr → tactic (list α)) (e : expr) : tactic (list α) :=
app_map_aux F (expr_lens.entire, e)
end expr
|
864ba69e3cd2b7cda00e859f1118582896412603 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/analysis/analytic/basic.lean | 25183cfcc83e4236ea2c18f98305a42d6164b1eb | [
"Apache-2.0"
] | permissive | gebner/mathlib | eab0150cc4f79ec45d2016a8c21750244a2e7ff0 | cc6a6edc397c55118df62831e23bfbd6e6c6b4ab | refs/heads/master | 1,625,574,853,976 | 1,586,712,827,000 | 1,586,712,827,000 | 99,101,412 | 1 | 0 | Apache-2.0 | 1,586,716,389,000 | 1,501,667,958,000 | Lean | UTF-8 | Lean | false | false | 37,621 | 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.calculus.times_cont_diff tactic.omega analysis.complex.exponential
analysis.specific_limits
/-!
# Analytic functions
A function is analytic in one dimension around `0` if it can be written as a converging power series
`Σ pₙ zⁿ`. This definition can be extended to any dimension (even in infinite dimension) by
requiring that `pₙ` is a continuous `n`-multilinear map. In general, `pₙ` is not unique (in two
dimensions, taking `p₂ (x, y) (x', y') = x y'` or `y x'` gives the same map when applied to a
vector `(x, y) (x, y)`). A way to guarantee uniqueness is to take a symmetric `pₙ`, but this is not
always possible in nonzero characteristic (in characteristic 2, the previous example has no
symmetric representative). Therefore, we do not insist on symmetry or uniqueness in the definition,
and we only require the existence of a converging series.
The general framework is important to say that the exponential map on bounded operators on a Banach
space is analytic, as well as the inverse on invertible operators.
## Main definitions
Let `p` be a formal multilinear series from `E` to `F`, i.e., `p n` is a multilinear map on `E^n`
for `n : ℕ`.
* `p.radius`: the largest `r : ennreal` such that `∥p n∥ * r^n` grows subexponentially, defined as
a liminf.
* `p.le_radius_of_bound`, `p.bound_of_lt_radius`, `p.geometric_bound_of_lt_radius`: relating the
value of the radius with the growth of `∥p n∥ * r^n`.
* `p.partial_sum n x`: the sum `∑_{i = 0}^{n-1} pᵢ xⁱ`.
* `p.sum x`: the sum `∑_{i = 0}^{∞} pᵢ xⁱ`.
Additionally, let `f` be a function from `E` to `F`.
* `has_fpower_series_on_ball f p x r`: on the ball of center `x` with radius `r`,
`f (x + y) = ∑_n pₙ yⁿ`.
* `has_fpower_series_at f p x`: on some ball of center `x` with positive radius, holds
`has_fpower_series_on_ball f p x r`.
* `analytic_at 𝕜 f x`: there exists a power series `p` such that holds
`has_fpower_series_at f p x`.
We develop the basic properties of these notions, notably:
* If a function admits a power series, it is continuous (see
`has_fpower_series_on_ball.continuous_on` and `has_fpower_series_at.continuous_at` and
`analytic_at.continuous_at`).
* In a complete space, the sum of a formal power series with positive radius is well defined on the
disk of convergence, see `formal_multilinear_series.has_fpower_series_on_ball`.
* If a function admits a power series in a ball, then it is analytic at any point `y` of this ball,
and the power series there can be expressed in terms of the initial power series `p` as
`p.change_origin y`. See `has_fpower_series_on_ball.change_origin`. It follows in particular that
the set of points at which a given function is analytic is open, see `is_open_analytic_at`.
## Implementation details
We only introduce the radius of convergence of a power series, as `p.radius`.
For a power series in finitely many dimensions, there is a finer (directional, coordinate-dependent)
notion, describing the polydisk of convergence. This notion is more specific, and not necessary to
build the general theory. We do not define it here.
-/
noncomputable theory
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{F : Type*} [normed_group F] [normed_space 𝕜 F]
{G : Type*} [normed_group G] [normed_space 𝕜 G]
open_locale topological_space classical
open filter
/-! ### The radius of a formal multilinear series -/
namespace formal_multilinear_series
/-- The radius of a formal multilinear series is the largest `r` such that the sum `Σ pₙ yⁿ`
converges for all `∥y∥ < r`. -/
def radius (p : formal_multilinear_series 𝕜 E F) : ennreal :=
liminf at_top (λ n, 1/((nnnorm (p n)) ^ (1 / (n : ℝ)) : nnreal))
/--If `∥pₙ∥ rⁿ` is bounded in `n`, then the radius of `p` is at least `r`. -/
lemma le_radius_of_bound (p : formal_multilinear_series 𝕜 E F) (C : nnreal) {r : nnreal}
(h : ∀ (n : ℕ), nnnorm (p n) * r^n ≤ C) : (r : ennreal) ≤ p.radius :=
begin
have L : tendsto (λ n : ℕ, (r : ennreal) / ((C + 1)^(1/(n : ℝ)) : nnreal))
at_top (𝓝 ((r : ennreal) / ((C + 1)^(0 : ℝ) : nnreal))),
{ apply ennreal.tendsto.div tendsto_const_nhds,
{ simp },
{ rw ennreal.tendsto_coe,
apply tendsto_const_nhds.nnrpow (tendsto_const_div_at_top_nhds_0_nat 1),
simp },
{ simp } },
have A : ∀ n : ℕ , 0 < n →
(r : ennreal) ≤ ((C + 1)^(1/(n : ℝ)) : nnreal) * (1 / (nnnorm (p n) ^ (1/(n:ℝ)) : nnreal)),
{ assume n npos,
simp only [one_div_eq_inv, mul_assoc, mul_one, eq.symm ennreal.mul_div_assoc],
rw [ennreal.le_div_iff_mul_le _ _, ← nnreal.pow_nat_rpow_nat_inv r npos, ← ennreal.coe_mul,
ennreal.coe_le_coe, ← nnreal.mul_rpow, mul_comm],
{ exact nnreal.rpow_le_rpow (le_trans (h n) (le_add_right (le_refl _))) (by simp) },
{ simp },
{ simp } },
have B : ∀ᶠ (n : ℕ) in at_top,
(r : ennreal) / ((C + 1)^(1/(n : ℝ)) : nnreal) ≤ 1 / (nnnorm (p n) ^ (1/(n:ℝ)) : nnreal),
{ apply eventually_at_top.2 ⟨1, λ n hn, _⟩,
rw [ennreal.div_le_iff_le_mul, mul_comm],
{ apply A n hn },
{ simp },
{ simp } },
have D : liminf at_top (λ n : ℕ, (r : ennreal) / ((C + 1)^(1/(n : ℝ)) : nnreal)) ≤ p.radius :=
liminf_le_liminf B,
rw liminf_eq_of_tendsto filter.at_top_ne_bot L at D,
simpa using D
end
/-- For `r` strictly smaller than the radius of `p`, then `∥pₙ∥ rⁿ` is bounded. -/
lemma bound_of_lt_radius (p : formal_multilinear_series 𝕜 E F) {r : nnreal}
(h : (r : ennreal) < p.radius) : ∃ (C : nnreal), ∀ n, nnnorm (p n) * r^n ≤ C :=
begin
obtain ⟨N, hN⟩ : ∃ (N : ℕ), ∀ n, n ≥ N → (r : ennreal) < 1 / ↑(nnnorm (p n) ^ (1 / (n : ℝ))) :=
eventually.exists_forall_of_at_top (eventually_lt_of_lt_liminf h),
obtain ⟨D, hD⟩ : ∃D, ∀ x ∈ (↑((finset.range N.succ).image (λ i, nnnorm (p i) * r^i))), x ≤ D :=
finset.bdd_above _,
refine ⟨max D 1, λ n, _⟩,
cases le_or_lt n N with hn hn,
{ refine le_trans _ (le_max_left D 1),
apply hD,
have : n ∈ finset.range N.succ := list.mem_range.mpr (nat.lt_succ_iff.mpr hn),
exact finset.mem_image_of_mem _ this },
{ by_cases hpn : nnnorm (p n) = 0, { simp [hpn] },
have A : nnnorm (p n) ^ (1 / (n : ℝ)) ≠ 0, by simp [nnreal.rpow_eq_zero_iff, hpn],
have B : r < (nnnorm (p n) ^ (1 / (n : ℝ)))⁻¹,
{ have := hN n (le_of_lt hn),
rwa [ennreal.div_def, ← ennreal.coe_inv A, one_mul, ennreal.coe_lt_coe] at this },
rw [nnreal.lt_inv_iff_mul_lt A, mul_comm] at B,
have : (nnnorm (p n) ^ (1 / (n : ℝ)) * r) ^ n ≤ 1 :=
pow_le_one n (zero_le (nnnorm (p n) ^ (1 / ↑n) * r)) (le_of_lt B),
rw [mul_pow, one_div_eq_inv, nnreal.rpow_nat_inv_pow_nat _ (lt_of_le_of_lt (zero_le _) hn)]
at this,
exact le_trans this (le_max_right _ _) },
end
/-- For `r` strictly smaller than the radius of `p`, then `∥pₙ∥ rⁿ` tends to zero exponentially. -/
lemma geometric_bound_of_lt_radius (p : formal_multilinear_series 𝕜 E F) {r : nnreal}
(h : (r : ennreal) < p.radius) : ∃ a C, a < 1 ∧ ∀ n, nnnorm (p n) * r^n ≤ C * a^n :=
begin
obtain ⟨t, rt, tp⟩ : ∃ (t : nnreal), (r : ennreal) < t ∧ (t : ennreal) < p.radius :=
ennreal.lt_iff_exists_nnreal_btwn.1 h,
rw ennreal.coe_lt_coe at rt,
have tpos : t ≠ 0 := ne_of_gt (lt_of_le_of_lt (zero_le _) rt),
obtain ⟨C, hC⟩ : ∃ (C : nnreal), ∀ n, nnnorm (p n) * t^n ≤ C := p.bound_of_lt_radius tp,
refine ⟨r / t, C, nnreal.div_lt_one_of_lt rt, λ n, _⟩,
calc nnnorm (p n) * r ^ n
= (nnnorm (p n) * t ^ n) * (r / t) ^ n : by { field_simp [tpos], ac_refl }
... ≤ C * (r / t) ^ n : mul_le_mul_of_nonneg_right (hC n) (zero_le _)
end
/-- The radius of the sum of two formal series is at least the minimum of their two radii. -/
lemma min_radius_le_radius_add (p q : formal_multilinear_series 𝕜 E F) :
min p.radius q.radius ≤ (p + q).radius :=
begin
refine le_of_forall_ge_of_dense (λ r hr, _),
cases r, { simpa using hr },
obtain ⟨Cp, hCp⟩ : ∃ (C : nnreal), ∀ n, nnnorm (p n) * r^n ≤ C :=
p.bound_of_lt_radius (lt_of_lt_of_le hr (min_le_left _ _)),
obtain ⟨Cq, hCq⟩ : ∃ (C : nnreal), ∀ n, nnnorm (q n) * r^n ≤ C :=
q.bound_of_lt_radius (lt_of_lt_of_le hr (min_le_right _ _)),
have : ∀ n, nnnorm ((p + q) n) * r^n ≤ Cp + Cq,
{ assume n,
calc nnnorm (p n + q n) * r ^ n
≤ (nnnorm (p n) + nnnorm (q n)) * r ^ n :
mul_le_mul_of_nonneg_right (norm_add_le (p n) (q n)) (zero_le (r ^ n))
... ≤ Cp + Cq : by { rw add_mul, exact add_le_add (hCp n) (hCq n) } },
exact (p + q).le_radius_of_bound _ this
end
lemma radius_neg (p : formal_multilinear_series 𝕜 E F) : (-p).radius = p.radius :=
by simp [formal_multilinear_series.radius, nnnorm_neg]
/-- Given a formal multilinear series `p` and a vector `x`, then `p.sum x` is the sum `Σ pₙ xⁿ`. A
priori, it only behaves well when `∥x∥ < p.radius`. -/
protected def sum (p : formal_multilinear_series 𝕜 E F) (x : E) : F :=
tsum (λn:ℕ, p n (λ(i : fin n), x))
/-- Given a formal multilinear series `p` and a vector `x`, then `p.partial_sum n x` is the sum
`Σ pₖ xᵏ` for `k ∈ {0,..., n-1}`. -/
def partial_sum (p : formal_multilinear_series 𝕜 E F) (n : ℕ) (x : E) : F :=
(finset.range n).sum (λ k, p k (λ(i : fin k), x))
/-- The partial sums of a formal multilinear series are continuous. -/
lemma partial_sum_continuous (p : formal_multilinear_series 𝕜 E F) (n : ℕ) :
continuous (p.partial_sum n) :=
continuous_finset_sum (finset.range n) $ λ k hk, (p k).cont.comp (continuous_pi (λ i, continuous_id))
end formal_multilinear_series
/-! ### Expanding a function as a power series -/
section
variables {f g : E → F} {p pf pg : formal_multilinear_series 𝕜 E F} {x : E} {r r' : ennreal}
/-- Given a function `f : E → F` and a formal multilinear series `p`, we say that `f` has `p` as
a power series on the ball of radius `r > 0` around `x` if `f (x + y) = ∑ pₙ yⁿ` for all `∥y∥ < r`. -/
structure has_fpower_series_on_ball
(f : E → F) (p : formal_multilinear_series 𝕜 E F) (x : E) (r : ennreal) : Prop :=
(r_le : r ≤ p.radius)
(r_pos : 0 < r)
(has_sum : ∀ {y}, y ∈ emetric.ball (0 : E) r → has_sum (λn:ℕ, p n (λ(i : fin n), y)) (f (x + y)))
/-- Given a function `f : E → F` and a formal multilinear series `p`, we say that `f` has `p` as
a power series around `x` if `f (x + y) = ∑ pₙ yⁿ` for all `y` in a neighborhood of `0`. -/
def has_fpower_series_at (f : E → F) (p : formal_multilinear_series 𝕜 E F) (x : E) :=
∃ r, has_fpower_series_on_ball f p x r
variable (𝕜)
/-- Given a function `f : E → F`, we say that `f` is analytic at `x` if it admits a convergent power
series expansion around `x`. -/
def analytic_at (f : E → F) (x : E) :=
∃ (p : formal_multilinear_series 𝕜 E F), has_fpower_series_at f p x
variable {𝕜}
lemma has_fpower_series_on_ball.has_fpower_series_at (hf : has_fpower_series_on_ball f p x r) :
has_fpower_series_at f p x := ⟨r, hf⟩
lemma has_fpower_series_at.analytic_at (hf : has_fpower_series_at f p x) : analytic_at 𝕜 f x :=
⟨p, hf⟩
lemma has_fpower_series_on_ball.analytic_at (hf : has_fpower_series_on_ball f p x r) :
analytic_at 𝕜 f x :=
hf.has_fpower_series_at.analytic_at
lemma has_fpower_series_on_ball.radius_pos (hf : has_fpower_series_on_ball f p x r) :
0 < p.radius :=
lt_of_lt_of_le hf.r_pos hf.r_le
lemma has_fpower_series_at.radius_pos (hf : has_fpower_series_at f p x) :
0 < p.radius :=
let ⟨r, hr⟩ := hf in hr.radius_pos
lemma has_fpower_series_on_ball.mono
(hf : has_fpower_series_on_ball f p x r) (r'_pos : 0 < r') (hr : r' ≤ r) :
has_fpower_series_on_ball f p x r' :=
⟨le_trans hr hf.1, r'_pos, λ y hy, hf.has_sum (emetric.ball_subset_ball hr hy)⟩
lemma has_fpower_series_on_ball.add
(hf : has_fpower_series_on_ball f pf x r) (hg : has_fpower_series_on_ball g pg x r) :
has_fpower_series_on_ball (f + g) (pf + pg) x r :=
{ r_le := le_trans (le_min_iff.2 ⟨hf.r_le, hg.r_le⟩) (pf.min_radius_le_radius_add pg),
r_pos := hf.r_pos,
has_sum := λ y hy, (hf.has_sum hy).add (hg.has_sum hy) }
lemma has_fpower_series_at.add
(hf : has_fpower_series_at f pf x) (hg : has_fpower_series_at g pg x) :
has_fpower_series_at (f + g) (pf + pg) x :=
begin
rcases hf with ⟨rf, hrf⟩,
rcases hg with ⟨rg, hrg⟩,
have P : 0 < min rf rg, by simp [hrf.r_pos, hrg.r_pos],
exact ⟨min rf rg, (hrf.mono P (min_le_left _ _)).add (hrg.mono P (min_le_right _ _))⟩
end
lemma analytic_at.add (hf : analytic_at 𝕜 f x) (hg : analytic_at 𝕜 g x) :
analytic_at 𝕜 (f + g) x :=
let ⟨pf, hpf⟩ := hf, ⟨qf, hqf⟩ := hg in (hpf.add hqf).analytic_at
lemma has_fpower_series_on_ball.neg (hf : has_fpower_series_on_ball f pf x r) :
has_fpower_series_on_ball (-f) (-pf) x r :=
{ r_le := by { rw pf.radius_neg, exact hf.r_le },
r_pos := hf.r_pos,
has_sum := λ y hy, (hf.has_sum hy).neg }
lemma has_fpower_series_at.neg
(hf : has_fpower_series_at f pf x) : has_fpower_series_at (-f) (-pf) x :=
let ⟨rf, hrf⟩ := hf in hrf.neg.has_fpower_series_at
lemma analytic_at.neg (hf : analytic_at 𝕜 f x) : analytic_at 𝕜 (-f) x :=
let ⟨pf, hpf⟩ := hf in hpf.neg.analytic_at
lemma has_fpower_series_on_ball.sub
(hf : has_fpower_series_on_ball f pf x r) (hg : has_fpower_series_on_ball g pg x r) :
has_fpower_series_on_ball (f - g) (pf - pg) x r :=
hf.add hg.neg
lemma has_fpower_series_at.sub
(hf : has_fpower_series_at f pf x) (hg : has_fpower_series_at g pg x) :
has_fpower_series_at (f - g) (pf - pg) x :=
hf.add hg.neg
lemma analytic_at.sub (hf : analytic_at 𝕜 f x) (hg : analytic_at 𝕜 g x) :
analytic_at 𝕜 (f - g) x :=
hf.add hg.neg
lemma has_fpower_series_on_ball.coeff_zero (hf : has_fpower_series_on_ball f pf x r)
(v : fin 0 → E) : pf 0 v = f x :=
begin
have v_eq : v = (λ i, 0), by { ext i, apply fin_zero_elim i },
have zero_mem : (0 : E) ∈ emetric.ball (0 : E) r, by simp [hf.r_pos],
have : ∀ i ≠ 0, pf i (λ j, 0) = 0,
{ assume i hi,
have : 0 < i := bot_lt_iff_ne_bot.mpr hi,
apply continuous_multilinear_map.map_coord_zero _ (⟨0, this⟩ : fin i),
refl },
have A := has_sum_unique (hf.has_sum zero_mem) (has_sum_single _ this),
simpa [v_eq] using A.symm,
end
lemma has_fpower_series_at.coeff_zero (hf : has_fpower_series_at f pf x) (v : fin 0 → E) :
pf 0 v = f x :=
let ⟨rf, hrf⟩ := hf in hrf.coeff_zero v
/-- If a function admits a power series expansion, then it is exponentially close to the partial
sums of this power series on strict subdisks of the disk of convergence. -/
lemma has_fpower_series_on_ball.uniform_geometric_approx {r' : nnreal}
(hf : has_fpower_series_on_ball f p x r) (h : (r' : ennreal) < r) :
∃ (a C : nnreal), a < 1 ∧ (∀ y ∈ metric.ball (0 : E) r', ∀ n,
∥f (x + y) - p.partial_sum n y∥ ≤ C * a ^ n) :=
begin
obtain ⟨a, C, ha, hC⟩ : ∃ a C, a < 1 ∧ ∀ n, nnnorm (p n) * r' ^n ≤ C * a^n :=
p.geometric_bound_of_lt_radius (lt_of_lt_of_le h hf.r_le),
refine ⟨a, C / (1 - a), ha, λ y hy n, _⟩,
have yr' : ∥y∥ < r', by { rw ball_0_eq at hy, exact hy },
have : y ∈ emetric.ball (0 : E) r,
{ rw [emetric.mem_ball, edist_eq_coe_nnnorm],
apply lt_trans _ h,
rw [ennreal.coe_lt_coe, ← nnreal.coe_lt_coe],
exact yr' },
simp only [nnreal.coe_sub (le_of_lt ha), nnreal.coe_sub, nnreal.coe_div, nnreal.coe_one],
rw [← dist_eq_norm, dist_comm, dist_eq_norm, ← mul_div_right_comm],
apply norm_sub_le_of_geometric_bound_of_has_sum ha _ (hf.has_sum this),
assume n,
calc ∥(p n) (λ (i : fin n), y)∥
≤ ∥p n∥ * (finset.univ.prod (λ i : fin n, ∥y∥)) : continuous_multilinear_map.le_op_norm _ _
... = nnnorm (p n) * (nnnorm y)^n : by simp
... ≤ nnnorm (p n) * r' ^ n :
mul_le_mul_of_nonneg_left (pow_le_pow_of_le_left (nnreal.coe_nonneg _) (le_of_lt yr') _)
(nnreal.coe_nonneg _)
... ≤ C * a ^ n : by exact_mod_cast hC n,
end
/-- If a function admits a power series expansion at `x`, then it is the uniform limit of the
partial sums of this power series on strict subdisks of the disk of convergence, i.e., `f (x + y)`
is the uniform limit of `p.partial_sum n y` there. -/
lemma has_fpower_series_on_ball.tendsto_uniformly_on {r' : nnreal}
(hf : has_fpower_series_on_ball f p x r) (h : (r' : ennreal) < r) :
tendsto_uniformly_on (λ n y, p.partial_sum n y) (λ y, f (x + y)) at_top (metric.ball (0 : E) r') :=
begin
rcases hf.uniform_geometric_approx h with ⟨a, C, ha, hC⟩,
refine metric.tendsto_uniformly_on_iff.2 (λ ε εpos, _),
have L : tendsto (λ n, (C : ℝ) * a^n) at_top (𝓝 ((C : ℝ) * 0)) :=
tendsto_const_nhds.mul (tendsto_pow_at_top_nhds_0_of_lt_1 (a.2) ha),
rw mul_zero at L,
apply ((tendsto_order.1 L).2 ε εpos).mono (λ n hn, _),
assume y hy,
rw dist_eq_norm,
exact lt_of_le_of_lt (hC y hy n) hn
end
/-- If a function admits a power series expansion at `x`, then it is the locally uniform limit of
the partial sums of this power series on the disk of convergence, i.e., `f (x + y)`
is the locally uniform limit of `p.partial_sum n y` there. -/
lemma has_fpower_series_on_ball.tendsto_locally_uniformly_on
(hf : has_fpower_series_on_ball f p x r) :
tendsto_locally_uniformly_on (λ n y, p.partial_sum n y) (λ y, f (x + y))
at_top (emetric.ball (0 : E) r) :=
begin
assume u hu x hx,
rcases ennreal.lt_iff_exists_nnreal_btwn.1 hx with ⟨r', xr', hr'⟩,
have : emetric.ball (0 : E) r' ∈ 𝓝 x :=
mem_nhds_sets emetric.is_open_ball xr',
refine ⟨emetric.ball (0 : E) r', mem_nhds_within_of_mem_nhds this, _⟩,
simpa [metric.emetric_ball_nnreal] using hf.tendsto_uniformly_on hr' u hu
end
/-- If a function admits a power series expansion at `x`, then it is the uniform limit of the
partial sums of this power series on strict subdisks of the disk of convergence, i.e., `f y`
is the uniform limit of `p.partial_sum n (y - x)` there. -/
lemma has_fpower_series_on_ball.tendsto_uniformly_on' {r' : nnreal}
(hf : has_fpower_series_on_ball f p x r) (h : (r' : ennreal) < r) :
tendsto_uniformly_on (λ n y, p.partial_sum n (y - x)) f at_top (metric.ball (x : E) r') :=
begin
convert (hf.tendsto_uniformly_on h).comp (λ y, y - x),
{ ext z, simp },
{ ext z, simp [dist_eq_norm] }
end
/-- If a function admits a power series expansion at `x`, then it is the locally uniform limit of
the partial sums of this power series on the disk of convergence, i.e., `f y`
is the locally uniform limit of `p.partial_sum n (y - x)` there. -/
lemma has_fpower_series_on_ball.tendsto_locally_uniformly_on'
(hf : has_fpower_series_on_ball f p x r) :
tendsto_locally_uniformly_on (λ n y, p.partial_sum n (y - x)) f at_top (emetric.ball (x : E) r) :=
begin
have A : continuous_on (λ (y : E), y - x) (emetric.ball (x : E) r) :=
(continuous_id.sub continuous_const).continuous_on,
convert (hf.tendsto_locally_uniformly_on).comp (λ (y : E), y - x) _ A,
{ ext z, simp },
{ assume z, simp [edist_eq_coe_nnnorm, edist_eq_coe_nnnorm_sub] }
end
/-- If a function admits a power series expansion on a disk, then it is continuous there. -/
lemma has_fpower_series_on_ball.continuous_on
(hf : has_fpower_series_on_ball f p x r) : continuous_on f (emetric.ball x r) :=
begin
apply hf.tendsto_locally_uniformly_on'.continuous_on _ at_top_ne_bot,
exact λ n, ((p.partial_sum_continuous n).comp (continuous_id.sub continuous_const)).continuous_on
end
lemma has_fpower_series_at.continuous_at (hf : has_fpower_series_at f p x) : continuous_at f x :=
let ⟨r, hr⟩ := hf in hr.continuous_on.continuous_at (emetric.ball_mem_nhds x (hr.r_pos))
lemma analytic_at.continuous_at (hf : analytic_at 𝕜 f x) : continuous_at f x :=
let ⟨p, hp⟩ := hf in hp.continuous_at
/-- In a complete space, the sum of a converging power series `p` admits `p` as a power series.
This is not totally obvious as we need to check the convergence of the series. -/
lemma formal_multilinear_series.has_fpower_series_on_ball [complete_space F]
(p : formal_multilinear_series 𝕜 E F) (h : 0 < p.radius) :
has_fpower_series_on_ball p.sum p 0 p.radius :=
{ r_le := le_refl _,
r_pos := h,
has_sum := λ y hy, begin
rw zero_add,
replace hy : (nnnorm y : ennreal) < p.radius,
by { convert hy, exact (edist_eq_coe_nnnorm _).symm },
obtain ⟨a, C, ha, hC⟩ : ∃ a C, a < 1 ∧ ∀ n, nnnorm (p n) * (nnnorm y)^n ≤ C * a^n :=
p.geometric_bound_of_lt_radius hy,
refine (summable_of_norm_bounded (λ n, (C : ℝ) * a ^ n)
((summable_geometric a.2 ha).mul_left _) (λ n, _)).has_sum,
calc ∥(p n) (λ (i : fin n), y)∥
≤ ∥p n∥ * (finset.univ.prod (λ i : fin n, ∥y∥)) : continuous_multilinear_map.le_op_norm _ _
... = nnnorm (p n) * (nnnorm y)^n : by simp
... ≤ C * a ^ n : by exact_mod_cast hC n
end }
lemma has_fpower_series_on_ball.sum [complete_space F] (h : has_fpower_series_on_ball f p x r)
{y : E} (hy : y ∈ emetric.ball (0 : E) r) : f (x + y) = p.sum y :=
begin
have A := h.has_sum hy,
have B := (p.has_fpower_series_on_ball h.radius_pos).has_sum (lt_of_lt_of_le hy h.r_le),
simpa using has_sum_unique A B
end
/-- The sum of a converging power series is continuous in its disk of convergence. -/
lemma formal_multilinear_series.continuous_on [complete_space F] :
continuous_on p.sum (emetric.ball 0 p.radius) :=
begin
by_cases h : 0 < p.radius,
{ exact (p.has_fpower_series_on_ball h).continuous_on },
{ simp at h,
simp [h, continuous_on_empty] }
end
end
/-!
### Changing origin in a power series
If a function is analytic in a disk `D(x, R)`, then it is analytic in any disk contained in that
one. Indeed, one can write
$$
f (x + y + z) = \sum_{n} p_n (y + z)^n = \sum_{n, k} \choose n k p_n y^{n-k} z^k
= \sum_{k} (\sum_{n} \choose n k p_n y^{n-k}) z^k.
$$
The corresponding power series has thus a `k`-th coefficient equal to
`\sum_{n} \choose n k p_n y^{n-k}`. In the general case where `pₙ` is a multilinear map, this has
to be interpreted suitably: instead of having a binomial coefficient, one should sum over all
possible subsets `s` of `fin n` of cardinal `k`, and attribute `z` to the indices in `s` and
`y` to the indices outside of `s`.
In this paragraph, we implement this. The new power series is called `p.change_origin y`. Then, we
check its convergence and the fact that its sum coincides with the original sum. The outcome of this
discussion is that the set of points where a function is analytic is open.
-/
namespace formal_multilinear_series
variables (p : formal_multilinear_series 𝕜 E F) {x y : E} {r : nnreal}
/--
Changing the origin of a formal multilinear series `p`, so that
`p.sum (x+y) = (p.change_origin x).sum y` when this makes sense.
Here, we don't use the bracket notation `⟨n, s, hs⟩` in place of the argument `i` in the lambda,
as this leads to a bad definition with auxiliary `_match` statements,
but we will try to use pattern matching in lambdas as much as possible in the proofs below
to increase readability.
-/
def change_origin (x : E) :
formal_multilinear_series 𝕜 E F :=
λ k, tsum (λi, (p i.1).restr i.2.1 i.2.2 x :
(Σ (n : ℕ), {s : finset (fin n) // finset.card s = k}) → (E [×k]→L[𝕜] F))
/-- Auxiliary lemma controlling the summability of the sequence appearing in the definition of
`p.change_origin`, first version. -/
-- Note here and below it is necessary to use `@` and provide implicit arguments using `_`,
-- so that it is possible to use pattern matching in the lambda.
-- Overall this seems a good trade-off in readability.
lemma change_origin_summable_aux1 (h : (nnnorm x + r : ennreal) < p.radius) :
@summable ℝ _ _ _ ((λ ⟨n, s⟩, ∥p n∥ * ∥x∥ ^ (n - s.card) * r ^ s.card) :
(Σ (n : ℕ), finset (fin n)) → ℝ) :=
begin
obtain ⟨a, C, ha, hC⟩ :
∃ a C, a < 1 ∧ ∀ n, nnnorm (p n) * (nnnorm x + r) ^ n ≤ C * a^n :=
p.geometric_bound_of_lt_radius h,
let Bnnnorm : (Σ (n : ℕ), finset (fin n)) → nnreal :=
λ ⟨n, s⟩, nnnorm (p n) * (nnnorm x) ^ (n - s.card) * r ^ s.card,
have : ((λ ⟨n, s⟩, ∥p n∥ * ∥x∥ ^ (n - s.card) * r ^ s.card) :
(Σ (n : ℕ), finset (fin n)) → ℝ) = (λ b, (Bnnnorm b : ℝ)),
by { ext b, rcases b with ⟨n, s⟩, simp [Bnnnorm, nnreal.coe_pow, coe_nnnorm] },
rw [this, nnreal.summable_coe, ← ennreal.tsum_coe_ne_top_iff_summable],
apply ne_of_lt,
calc (∑ b, ↑(Bnnnorm b))
= (∑ n, (∑ s, ↑(Bnnnorm ⟨n, s⟩))) : by exact ennreal.tsum_sigma' _
... ≤ (∑ n, (((nnnorm (p n) * (nnnorm x + r)^n) : nnreal) : ennreal)) :
begin
refine ennreal.tsum_le_tsum (λ n, _),
rw [tsum_fintype, ← ennreal.coe_finset_sum, ennreal.coe_le_coe],
apply le_of_eq,
calc finset.univ.sum (λ (s : finset (fin n)), Bnnnorm ⟨n, s⟩)
= finset.univ.sum (λ (s : finset (fin n)),
nnnorm (p n) * ((nnnorm x) ^ (n - s.card) * r ^ s.card)) :
by simp [← mul_assoc]
... = nnnorm (p n) * (nnnorm x + r) ^ n :
by { rw [add_comm, ← finset.mul_sum, ← fin.sum_pow_mul_eq_add_pow], congr, ext s, ring }
end
... ≤ (∑ (n : ℕ), (C * a ^ n : ennreal)) :
tsum_le_tsum (λ n, by exact_mod_cast hC n) ennreal.summable ennreal.summable
... < ⊤ :
by simp [ennreal.mul_eq_top, ha, ennreal.tsum_mul_left, ennreal.tsum_geometric,
ennreal.lt_top_iff_ne_top]
end
/-- Auxiliary lemma controlling the summability of the sequence appearing in the definition of
`p.change_origin`, second version. -/
lemma change_origin_summable_aux2 (h : (nnnorm x + r : ennreal) < p.radius) :
@summable ℝ _ _ _ ((λ ⟨k, n, s, hs⟩, ∥(p n).restr s hs x∥ * ↑r ^ k) :
(Σ (k : ℕ) (n : ℕ), {s : finset (fin n) // finset.card s = k}) → ℝ) :=
begin
let γ : ℕ → Type* := λ k, (Σ (n : ℕ), {s : finset (fin n) // s.card = k}),
let Bnorm : (Σ (n : ℕ), finset (fin n)) → ℝ := λ ⟨n, s⟩, ∥p n∥ * ∥x∥ ^ (n - s.card) * r ^ s.card,
have SBnorm : summable Bnorm := p.change_origin_summable_aux1 h,
let Anorm : (Σ (n : ℕ), finset (fin n)) → ℝ := λ ⟨n, s⟩, ∥(p n).restr s rfl x∥ * r ^ s.card,
have SAnorm : summable Anorm,
{ refine summable_of_norm_bounded _ SBnorm (λ i, _),
rcases i with ⟨n, s⟩,
suffices H : ∥(p n).restr s rfl x∥ * (r : ℝ) ^ s.card ≤
(∥p n∥ * ∥x∥ ^ (n - finset.card s) * r ^ s.card),
{ have : ∥(r: ℝ)∥ = r, by rw [real.norm_eq_abs, abs_of_nonneg (nnreal.coe_nonneg _)],
simpa [Anorm, Bnorm, this] using H },
exact mul_le_mul_of_nonneg_right ((p n).norm_restr s rfl x)
(pow_nonneg (nnreal.coe_nonneg _) _) },
let e : (Σ (n : ℕ), finset (fin n)) ≃
(Σ (k : ℕ) (n : ℕ), {s : finset (fin n) // finset.card s = k}) :=
{ to_fun := λ ⟨n, s⟩, ⟨s.card, n, s, rfl⟩,
inv_fun := λ ⟨k, n, s, hs⟩, ⟨n, s⟩,
left_inv := λ ⟨n, s⟩, rfl,
right_inv := λ ⟨k, n, s, hs⟩, by { induction hs, refl } },
rw ← e.summable_iff,
convert SAnorm,
ext i,
rcases i with ⟨n, s⟩,
refl
end
/-- Auxiliary lemma controlling the summability of the sequence appearing in the definition of
`p.change_origin`, third version. -/
lemma change_origin_summable_aux3 (k : ℕ) (h : (nnnorm x : ennreal) < p.radius) :
@summable ℝ _ _ _ (λ ⟨n, s, hs⟩, ∥(p n).restr s hs x∥ :
(Σ (n : ℕ), {s : finset (fin n) // finset.card s = k}) → ℝ) :=
begin
obtain ⟨r, rpos, hr⟩ : ∃ (r : nnreal), 0 < r ∧ ((nnnorm x + r) : ennreal) < p.radius :=
ennreal.lt_iff_exists_add_pos_lt.mp h,
have S : @summable ℝ _ _ _ ((λ ⟨n, s, hs⟩, ∥(p n).restr s hs x∥ * (r : ℝ) ^ k) :
(Σ (n : ℕ), {s : finset (fin n) // finset.card s = k}) → ℝ),
{ let j : (Σ (n : ℕ), {s : finset (fin n) // finset.card s = k})
→ (Σ (k : ℕ) (n : ℕ), {s : finset (fin n) // finset.card s = k}) :=
λ ⟨n, s, hs⟩, ⟨k, n, s, hs⟩,
have j_inj : function.injective j, by tidy,
convert summable.summable_comp_of_injective (p.change_origin_summable_aux2 hr) j_inj,
tidy },
have : (r : ℝ)^k ≠ 0, by simp [pow_ne_zero, nnreal.coe_eq_zero, ne_of_gt rpos],
apply (summable_mul_right_iff this).2,
convert S,
tidy
end
/-- The radius of convergence of `p.change_origin x` is at least `p.radius - ∥x∥`. In other words,
`p.change_origin x` is well defined on the largest ball contained in the original ball of
convergence.-/
lemma change_origin_radius : p.radius - nnnorm x ≤ (p.change_origin x).radius :=
begin
by_cases h : p.radius ≤ nnnorm x,
{ have : radius p - ↑(nnnorm x) = 0 := ennreal.sub_eq_zero_of_le h,
rw this,
exact zero_le _ },
replace h : (nnnorm x : ennreal) < p.radius, by simpa using h,
refine le_of_forall_ge_of_dense (λ r hr, _),
cases r, { simpa using hr },
rw [ennreal.lt_sub_iff_add_lt, add_comm] at hr,
let A : (Σ (k : ℕ) (n : ℕ), {s : finset (fin n) // finset.card s = k}) → ℝ :=
λ ⟨k, n, s, hs⟩, ∥(p n).restr s hs x∥ * (r : ℝ) ^ k,
have SA : summable A := p.change_origin_summable_aux2 hr,
have A_nonneg : ∀ i, 0 ≤ A i,
{ rintros ⟨k, n, s, hs⟩,
change 0 ≤ ∥(p n).restr s hs x∥ * (r : ℝ) ^ k,
refine mul_nonneg' (norm_nonneg _) (pow_nonneg (nnreal.coe_nonneg _) _) },
have tsum_nonneg : 0 ≤ tsum A := tsum_nonneg A_nonneg,
apply le_radius_of_bound _ (nnreal.of_real (tsum A)) (λ k, _),
rw [← nnreal.coe_le_coe, nnreal.coe_mul, nnreal.coe_pow, coe_nnnorm,
nnreal.coe_of_real _ tsum_nonneg],
let j : (Σ (n : ℕ), {s : finset (fin n) // finset.card s = k})
→ (Σ (k : ℕ) (n : ℕ), {s : finset (fin n) // finset.card s = k}) :=
λ ⟨n, s, hs⟩, ⟨k, n, s, hs⟩,
have j_inj : function.injective j, by tidy,
calc ∥change_origin p x k∥ * ↑r ^ k
= ∥@tsum (E [×k]→L[𝕜] F) _ _ _ (λ i, (p i.1).restr i.2.1 i.2.2 x :
(Σ (n : ℕ), {s : finset (fin n) // finset.card s = k}) → (E [×k]→L[𝕜] F))∥ * ↑r ^ k : rfl
... ≤ tsum (λ i, ∥(p i.1).restr i.2.1 i.2.2 x∥ :
(Σ (n : ℕ), {s : finset (fin n) // finset.card s = k}) → ℝ) * ↑r ^ k :
begin
apply mul_le_mul_of_nonneg_right _ (pow_nonneg (nnreal.coe_nonneg _) _),
apply norm_tsum_le_tsum_norm,
convert p.change_origin_summable_aux3 k h,
ext a,
tidy
end
... = tsum (λ i, ∥(p i.1).restr i.2.1 i.2.2 x∥ * ↑r ^ k :
(Σ (n : ℕ), {s : finset (fin n) // finset.card s = k}) → ℝ) :
by { rw tsum_mul_right, convert p.change_origin_summable_aux3 k h, tidy }
... = tsum (A ∘ j) : by { congr, tidy }
... ≤ tsum A : tsum_comp_le_tsum_of_inj SA A_nonneg j_inj
end
-- From this point on, assume that the space is complete, to make sure that series that converge
-- in norm also converge in `F`.
variable [complete_space F]
/-- The `k`-th coefficient of `p.change_origin` is the sum of a summable series. -/
lemma change_origin_has_sum (k : ℕ) (h : (nnnorm x : ennreal) < p.radius) :
@has_sum (E [×k]→L[𝕜] F) _ _ _ ((λ i, (p i.1).restr i.2.1 i.2.2 x) :
(Σ (n : ℕ), {s : finset (fin n) // finset.card s = k}) → (E [×k]→L[𝕜] F))
(p.change_origin x k) :=
begin
apply summable.has_sum,
apply summable_of_summable_norm,
convert p.change_origin_summable_aux3 k h,
tidy
end
/-- Summing the series `p.change_origin x` at a point `y` gives back `p (x + y)`-/
theorem change_origin_eval (h : (nnnorm x + nnnorm y : ennreal) < p.radius) :
has_sum ((λk:ℕ, p.change_origin x k (λ (i : fin k), y))) (p.sum (x + y)) :=
begin
/- The series on the left is a series of series. If we order the terms differently, we get back
to `p.sum (x + y)`, in which the `n`-th term is expanded by multilinearity. In the proof below,
the term on the left is the sum of a series of terms `A`, the sum on the right is the sum of a
series of terms `B`, and we show that they correspond to each other by reordering to conclude the
proof. -/
have radius_pos : 0 < p.radius := lt_of_le_of_lt (zero_le _) h,
-- `A` is the terms of the series whose sum gives the series for `p.change_origin`
let A : (Σ (k : ℕ) (n : ℕ), {s : finset (fin n) // s.card = k}) → F :=
λ ⟨k, n, s, hs⟩, (p n).restr s hs x (λ(i : fin k), y),
-- `B` is the terms of the series whose sum gives `p (x + y)`, after expansion by multilinearity.
let B : (Σ (n : ℕ), finset (fin n)) → F := λ ⟨n, s⟩, (p n).restr s rfl x (λ (i : fin s.card), y),
let Bnorm : (Σ (n : ℕ), finset (fin n)) → ℝ := λ ⟨n, s⟩, ∥p n∥ * ∥x∥ ^ (n - s.card) * ∥y∥ ^ s.card,
have SBnorm : summable Bnorm, by convert p.change_origin_summable_aux1 h,
have SB : summable B,
{ refine summable_of_norm_bounded _ SBnorm _,
rintros ⟨n, s⟩,
calc ∥(p n).restr s rfl x (λ (i : fin s.card), y)∥
≤ ∥(p n).restr s rfl x∥ * ∥y∥ ^ s.card :
begin
convert ((p n).restr s rfl x).le_op_norm (λ (i : fin s.card), y),
simp [(finset.prod_const (∥y∥))],
end
... ≤ (∥p n∥ * ∥x∥ ^ (n - s.card)) * ∥y∥ ^ s.card :
mul_le_mul_of_nonneg_right ((p n).norm_restr _ _ _) (pow_nonneg (norm_nonneg _) _) },
-- Check that indeed the sum of `B` is `p (x + y)`.
have has_sum_B : has_sum B (p.sum (x + y)),
{ have K1 : ∀ n, has_sum (λ (s : finset (fin n)), B ⟨n, s⟩) (p n (λ (i : fin n), x + y)),
{ assume n,
have : (p n) (λ (i : fin n), y + x) = finset.univ.sum
(λ (s : finset (fin n)), p n (finset.piecewise s (λ (i : fin n), y) (λ (i : fin n), x))) :=
(p n).map_add_univ (λ i, y) (λ i, x),
simp [add_comm y x] at this,
rw this,
exact has_sum_fintype _ },
have K2 : has_sum (λ (n : ℕ), (p n) (λ (i : fin n), x + y)) (p.sum (x + y)),
{ have : x + y ∈ emetric.ball (0 : E) p.radius,
{ apply lt_of_le_of_lt _ h,
rw [edist_eq_coe_nnnorm, ← ennreal.coe_add, ennreal.coe_le_coe],
exact norm_add_le x y },
simpa using (p.has_fpower_series_on_ball radius_pos).has_sum this },
exact has_sum.sigma_of_has_sum K2 K1 SB },
-- Deduce that the sum of `A` is also `p (x + y)`, as the terms `A` and `B` are the same up to
-- reordering
have has_sum_A : has_sum A (p.sum (x + y)),
{ let e : (Σ (n : ℕ), finset (fin n)) ≃
(Σ (k : ℕ) (n : ℕ), {s : finset (fin n) // finset.card s = k}) :=
{ to_fun := λ ⟨n, s⟩, ⟨s.card, n, s, rfl⟩,
inv_fun := λ ⟨k, n, s, hs⟩, ⟨n, s⟩,
left_inv := λ ⟨n, s⟩, rfl,
right_inv := λ ⟨k, n, s, hs⟩, by { induction hs, refl } },
have : A ∘ e = B, by { ext x, cases x, refl },
rw ← e.has_sum_iff,
convert has_sum_B },
-- Summing `A ⟨k, c⟩` with fixed `k` and varying `c` is exactly the `k`-th term in the series
-- defining `p.change_origin`, by definition
have J : ∀k, has_sum (λ c, A ⟨k, c⟩) (p.change_origin x k (λ(i : fin k), y)),
{ assume k,
have : (nnnorm x : ennreal) < radius p := lt_of_le_of_lt (le_add_right (le_refl _)) h,
convert continuous_multilinear_map.has_sum_eval (p.change_origin_has_sum k this)
(λ(i : fin k), y),
ext i,
tidy },
exact has_sum_A.sigma J
end
end formal_multilinear_series
section
variables [complete_space F] {f : E → F} {p : formal_multilinear_series 𝕜 E F} {x y : E}
{r : ennreal}
/-- If a function admits a power series expansion `p` on a ball `B (x, r)`, then it also admits a
power series on any subball of this ball (even with a different center), given by `p.change_origin`.
-/
theorem has_fpower_series_on_ball.change_origin
(hf : has_fpower_series_on_ball f p x r) (h : (nnnorm y : ennreal) < r) :
has_fpower_series_on_ball f (p.change_origin y) (x + y) (r - nnnorm y) :=
{ r_le := begin
apply le_trans _ p.change_origin_radius,
exact ennreal.sub_le_sub hf.r_le (le_refl _)
end,
r_pos := by simp [h],
has_sum := begin
assume z hz,
have A : (nnnorm y : ennreal) + nnnorm z < r,
{ have : edist z 0 < r - ↑(nnnorm y) := hz,
rwa [edist_eq_coe_nnnorm, ennreal.lt_sub_iff_add_lt, add_comm] at this },
convert p.change_origin_eval (lt_of_lt_of_le A hf.r_le),
have : y + z ∈ emetric.ball (0 : E) r := calc
edist (y + z) 0 ≤ ↑(nnnorm y) + ↑(nnnorm z) :
by { rw [edist_eq_coe_nnnorm, ← ennreal.coe_add, ennreal.coe_le_coe], exact norm_add_le y z }
... < r : A,
simpa using hf.sum this
end }
lemma has_fpower_series_on_ball.analytic_at_of_mem
(hf : has_fpower_series_on_ball f p x r) (h : y ∈ emetric.ball x r) :
analytic_at 𝕜 f y :=
begin
have : (nnnorm (y - x) : ennreal) < r, by simpa [edist_eq_coe_nnnorm_sub] using h,
have := hf.change_origin this,
rw [add_sub_cancel'_right] at this,
exact this.analytic_at
end
variables (𝕜 f)
lemma is_open_analytic_at : is_open {x | analytic_at 𝕜 f x} :=
begin
rw is_open_iff_forall_mem_open,
assume x hx,
rcases hx with ⟨p, r, hr⟩,
refine ⟨emetric.ball x r, λ y hy, hr.analytic_at_of_mem hy, emetric.is_open_ball, _⟩,
simp only [edist_self, emetric.mem_ball, hr.r_pos]
end
variables {𝕜 f}
end
|
ede2af7c8113e134193010c93c2e2fc66561be38 | 5d166a16ae129621cb54ca9dde86c275d7d2b483 | /tests/lean/notation2.lean | 231fffce159029f8834c57ca764457afa554ddd8 | [
"Apache-2.0"
] | permissive | jcarlson23/lean | b00098763291397e0ac76b37a2dd96bc013bd247 | 8de88701247f54d325edd46c0eed57aeacb64baf | refs/heads/master | 1,611,571,813,719 | 1,497,020,963,000 | 1,497,021,515,000 | 93,882,536 | 1 | 0 | null | 1,497,029,896,000 | 1,497,029,896,000 | null | UTF-8 | Lean | false | false | 267 | lean | --
inductive List (T : Type) : Type | nil {} : List | cons : T → List → List open List notation h :: t := cons h t notation `[` l:(foldr `,` (h t, cons h t) nil) `]` := l
infixr `::` := cons
#check (1:nat) :: 2 :: nil
#check (1:nat) :: 2 :: 3 :: 4 :: 5 :: nil
|
c38cbef790a7dc701f945f0d88048ad8fea245de | 958488bc7f3c2044206e0358e56d7690b6ae696c | /lean/while/Stmt.lean | 927bc67fa66aaa68769d1e4b3af2aaac5422fc7f | [] | no_license | possientis/Prog | a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4 | d4b3debc37610a88e0dac3ac5914903604fd1d1f | refs/heads/master | 1,692,263,717,723 | 1,691,757,179,000 | 1,691,757,179,000 | 40,361,602 | 3 | 0 | null | 1,679,896,438,000 | 1,438,953,859,000 | Coq | UTF-8 | Lean | false | false | 990 | lean | import Env
import AExp
import BExp
-- The WHILE language: deep embedding for actual language: full syntax and semantics specified
inductive Stmt : Type
| skip : Stmt
| assign : string → AExp → Stmt
| seq : Stmt → Stmt → Stmt
| ite : BExp → Stmt → Stmt → Stmt
| while : BExp → Stmt → Stmt
open Stmt
infixr ` ;; ` : 70 := seq
infix ` :== ` : 80 := assign
infixl ` :+: ` : 90 := aPlus
-- set of variables written by a thread
def W : Stmt → set string
| skip := ∅
| (x :== _) := {x}
| (e₁ ;; e₂) := W e₁ ∪ W e₂
| (ite _ e₁ e₂) := W e₁ ∪ W e₂
| (while _ e₁) := W e₁
-- set of variables read by a thread
def R : Stmt → set string
| skip := ∅
| (_ :== a) := ARead a
| (e₁ ;; e₂) := R e₁ ∪ R e₂
| (ite b e₁ e₂) := BRead b ∪ R e₁ ∪ R e₂
| (while b e₁) := BRead b ∪ R e₁
-- set of variables written or read by a thread
def V (e:Stmt) : set string := W e ∪ R e
|
e2f540ca034eb3cb937be554738a90d2d526017c | d642a6b1261b2cbe691e53561ac777b924751b63 | /src/group_theory/submonoid.lean | 84bc2e08b8413bef47a753a59299c1ba27c234c9 | [
"Apache-2.0"
] | permissive | cipher1024/mathlib | fee56b9954e969721715e45fea8bcb95f9dc03fe | d077887141000fefa5a264e30fa57520e9f03522 | refs/heads/master | 1,651,806,490,504 | 1,573,508,694,000 | 1,573,508,694,000 | 107,216,176 | 0 | 0 | Apache-2.0 | 1,647,363,136,000 | 1,508,213,014,000 | Lean | UTF-8 | Lean | false | false | 11,999 | lean | /-
Copyright (c) 2018 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro
-/
import algebra.big_operators
import data.finset
import tactic.subtype_instance
variables {α : Type*} [monoid α] {s : set α}
variables {β : Type*} [add_monoid β] {t : set β}
/-- `s` is an additive submonoid: a set containing 0 and closed under addition. -/
class is_add_submonoid (s : set β) : Prop :=
(zero_mem : (0:β) ∈ s)
(add_mem {a b} : a ∈ s → b ∈ s → a + b ∈ s)
/-- `s` is a submonoid: a set containing 1 and closed under multiplication. -/
@[to_additive is_add_submonoid]
class is_submonoid (s : set α) : Prop :=
(one_mem : (1:α) ∈ s)
(mul_mem {a b} : a ∈ s → b ∈ s → a * b ∈ s)
instance additive.is_add_submonoid
(s : set α) : ∀ [is_submonoid s], @is_add_submonoid (additive α) _ s
| ⟨h₁, h₂⟩ := ⟨h₁, @h₂⟩
theorem additive.is_add_submonoid_iff
{s : set α} : @is_add_submonoid (additive α) _ s ↔ is_submonoid s :=
⟨λ ⟨h₁, h₂⟩, ⟨h₁, @h₂⟩, λ h, by resetI; apply_instance⟩
instance multiplicative.is_submonoid
(s : set β) : ∀ [is_add_submonoid s], @is_submonoid (multiplicative β) _ s
| ⟨h₁, h₂⟩ := ⟨h₁, @h₂⟩
theorem multiplicative.is_submonoid_iff
{s : set β} : @is_submonoid (multiplicative β) _ s ↔ is_add_submonoid s :=
⟨λ ⟨h₁, h₂⟩, ⟨h₁, @h₂⟩, λ h, by resetI; apply_instance⟩
@[to_additive]
instance is_submonoid.inter (s₁ s₂ : set α) [is_submonoid s₁] [is_submonoid s₂] :
is_submonoid (s₁ ∩ s₂) :=
{ one_mem := ⟨is_submonoid.one_mem _, is_submonoid.one_mem _⟩,
mul_mem := λ x y hx hy,
⟨is_submonoid.mul_mem hx.1 hy.1, is_submonoid.mul_mem hx.2 hy.2⟩ }
@[to_additive]
instance is_submonoid.Inter {ι : Sort*} (s : ι → set α) [h : ∀ y : ι, is_submonoid (s y)] :
is_submonoid (set.Inter s) :=
{ one_mem := set.mem_Inter.2 $ λ y, is_submonoid.one_mem (s y),
mul_mem := λ x₁ x₂ h₁ h₂, set.mem_Inter.2 $
λ y, is_submonoid.mul_mem (set.mem_Inter.1 h₁ y) (set.mem_Inter.1 h₂ y) }
@[to_additive is_add_submonoid_Union_of_directed]
lemma is_submonoid_Union_of_directed {ι : Type*} [hι : nonempty ι]
(s : ι → set α) [∀ i, is_submonoid (s i)]
(directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) :
is_submonoid (⋃i, s i) :=
{ one_mem := let ⟨i⟩ := hι in set.mem_Union.2 ⟨i, is_submonoid.one_mem _⟩,
mul_mem := λ a b ha hb,
let ⟨i, hi⟩ := set.mem_Union.1 ha in
let ⟨j, hj⟩ := set.mem_Union.1 hb in
let ⟨k, hk⟩ := directed i j in
set.mem_Union.2 ⟨k, is_submonoid.mul_mem (hk.1 hi) (hk.2 hj)⟩ }
section powers
def powers (x : α) : set α := {y | ∃ n:ℕ, x^n = y}
def multiples (x : β) : set β := {y | ∃ n:ℕ, add_monoid.smul n x = y}
attribute [to_additive multiples] powers
lemma powers.one_mem {x : α} : (1 : α) ∈ powers x := ⟨0, pow_zero _⟩
lemma multiples.zero_mem {x : β} : (0 : β) ∈ multiples x := ⟨0, add_monoid.zero_smul _⟩
attribute [to_additive] powers.one_mem
lemma powers.self_mem {x : α} : x ∈ powers x := ⟨1, pow_one _⟩
lemma multiples.self_mem {x : β} : x ∈ multiples x := ⟨1, add_monoid.one_smul _⟩
attribute [to_additive] powers.self_mem
lemma powers.mul_mem {x y z: α} : (y ∈ powers x) → (z ∈ powers x) → (y * z ∈ powers x) :=
λ ⟨n₁, h₁⟩ ⟨n₂, h₂⟩, ⟨n₁ + n₂, by simp only [pow_add, *]⟩
lemma multiples.add_mem {x y z: β} : (y ∈ multiples x) → (z ∈ multiples x) → (y + z ∈ multiples x) :=
@powers.mul_mem (multiplicative β) _ _ _ _
attribute [to_additive] powers.mul_mem
@[to_additive is_add_submonoid]
instance powers.is_submonoid (x : α) : is_submonoid (powers x) :=
{ one_mem := powers.one_mem,
mul_mem := λ y z, powers.mul_mem }
@[to_additive is_add_submonoid]
instance univ.is_submonoid : is_submonoid (@set.univ α) := by split; simp
@[to_additive is_add_submonoid]
instance preimage.is_submonoid {γ : Type*} [monoid γ] (f : α → γ) [is_monoid_hom f]
(s : set γ) [is_submonoid s] : is_submonoid (f ⁻¹' s) :=
{ one_mem := show f 1 ∈ s, by rw is_monoid_hom.map_one f; exact is_submonoid.one_mem s,
mul_mem := λ a b (ha : f a ∈ s) (hb : f b ∈ s),
show f (a * b) ∈ s, by rw is_monoid_hom.map_mul f; exact is_submonoid.mul_mem ha hb }
@[instance, to_additive is_add_submonoid]
lemma image.is_submonoid {γ : Type*} [monoid γ] (f : α → γ) [is_monoid_hom f]
(s : set α) [is_submonoid s] : is_submonoid (f '' s) :=
{ one_mem := ⟨1, is_submonoid.one_mem s, is_monoid_hom.map_one f⟩,
mul_mem := λ a b ⟨x, hx⟩ ⟨y, hy⟩, ⟨x * y, is_submonoid.mul_mem hx.1 hy.1,
by rw [is_monoid_hom.map_mul f, hx.2, hy.2]⟩ }
@[to_additive is_add_submonoid]
instance range.is_submonoid {γ : Type*} [monoid γ] (f : α → γ) [is_monoid_hom f] :
is_submonoid (set.range f) :=
by rw ← set.image_univ; apply_instance
lemma is_submonoid.pow_mem {a : α} [is_submonoid s] (h : a ∈ s) : ∀ {n : ℕ}, a ^ n ∈ s
| 0 := is_submonoid.one_mem s
| (n + 1) := is_submonoid.mul_mem h is_submonoid.pow_mem
lemma is_add_submonoid.smul_mem {a : β} [is_add_submonoid t] :
∀ (h : a ∈ t) {n : ℕ}, add_monoid.smul n a ∈ t :=
@is_submonoid.pow_mem (multiplicative β) _ _ _ _
attribute [to_additive smul_mem] is_submonoid.pow_mem
lemma is_submonoid.power_subset {a : α} [is_submonoid s] (h : a ∈ s) : powers a ⊆ s :=
assume x ⟨n, hx⟩, hx ▸ is_submonoid.pow_mem h
lemma is_add_submonoid.multiple_subset {a : β} [is_add_submonoid t] :
a ∈ t → multiples a ⊆ t :=
@is_submonoid.power_subset (multiplicative β) _ _ _ _
attribute [to_additive multiple_subset] is_submonoid.power_subset
end powers
namespace is_submonoid
@[to_additive]
lemma list_prod_mem [is_submonoid s] : ∀{l : list α}, (∀x∈l, x ∈ s) → l.prod ∈ s
| [] h := one_mem s
| (a::l) h :=
suffices a * l.prod ∈ s, by simpa,
have a ∈ s ∧ (∀x∈l, x ∈ s), by simpa using h,
is_submonoid.mul_mem this.1 (list_prod_mem this.2)
@[to_additive]
lemma multiset_prod_mem {α} [comm_monoid α] (s : set α) [is_submonoid s] (m : multiset α) :
(∀a∈m, a ∈ s) → m.prod ∈ s :=
begin
refine quotient.induction_on m (assume l hl, _),
rw [multiset.quot_mk_to_coe, multiset.coe_prod],
exact list_prod_mem hl
end
@[to_additive]
lemma finset_prod_mem {α β} [comm_monoid α] (s : set α) [is_submonoid s] (f : β → α) :
∀(t : finset β), (∀b∈t, f b ∈ s) → t.prod f ∈ s
| ⟨m, hm⟩ hs :=
begin
refine multiset_prod_mem s _ _,
simp,
rintros a b hb rfl,
exact hs _ hb
end
end is_submonoid
@[to_additive add_monoid]
instance subtype.monoid {s : set α} [is_submonoid s] : monoid s :=
by subtype_instance
@[to_additive add_comm_monoid]
instance subtype.comm_monoid {α} [comm_monoid α] {s : set α} [is_submonoid s] : comm_monoid s :=
by subtype_instance
@[simp, to_additive]
lemma is_submonoid.coe_one [is_submonoid s] : ((1 : s) : α) = 1 := rfl
@[simp, to_additive]
lemma is_submonoid.coe_mul [is_submonoid s] (a b : s) : ((a * b : s) : α) = a * b := rfl
@[simp] lemma is_submonoid.coe_pow [is_submonoid s] (a : s) (n : ℕ) : ((a ^ n : s) : α) = a ^ n :=
by induction n; simp [*, pow_succ]
@[simp] lemma is_add_submonoid.smul_coe {β : Type*} [add_monoid β] {s : set β}
[is_add_submonoid s] (a : s) (n : ℕ) : ((add_monoid.smul n a : s) : β) = add_monoid.smul n a :=
by induction n; [refl, simp [*, succ_smul]]
attribute [to_additive smul_coe] is_submonoid.coe_pow
@[to_additive is_add_monoid_hom]
instance subtype_val.is_monoid_hom [is_submonoid s] : is_monoid_hom (subtype.val : s → α) :=
{ map_one := rfl, map_mul := λ _ _, rfl }
@[to_additive is_add_monoid_hom]
instance coe.is_monoid_hom [is_submonoid s] : is_monoid_hom (coe : s → α) :=
subtype_val.is_monoid_hom
@[to_additive is_add_monoid_hom]
instance subtype_mk.is_monoid_hom {γ : Type*} [monoid γ] [is_submonoid s] (f : γ → α)
[is_monoid_hom f] (h : ∀ x, f x ∈ s) : is_monoid_hom (λ x, (⟨f x, h x⟩ : s)) :=
{ map_one := subtype.eq (is_monoid_hom.map_one f),
map_mul := λ x y, subtype.eq (is_monoid_hom.map_mul f x y) }
@[to_additive is_add_monoid_hom]
instance set_inclusion.is_monoid_hom (t : set α) [is_submonoid s] [is_submonoid t] (h : s ⊆ t) :
is_monoid_hom (set.inclusion h) :=
subtype_mk.is_monoid_hom _ _
namespace add_monoid
inductive in_closure (s : set β) : β → Prop
| basic {a : β} : a ∈ s → in_closure a
| zero : in_closure 0
| add {a b : β} : in_closure a → in_closure b → in_closure (a + b)
end add_monoid
namespace monoid
inductive in_closure (s : set α) : α → Prop
| basic {a : α} : a ∈ s → in_closure a
| one : in_closure 1
| mul {a b : α} : in_closure a → in_closure b → in_closure (a * b)
attribute [to_additive] monoid.in_closure
attribute [to_additive] monoid.in_closure.one
attribute [to_additive] monoid.in_closure.mul
@[to_additive]
def closure (s : set α) : set α := {a | in_closure s a }
@[to_additive is_add_submonoid]
instance closure.is_submonoid (s : set α) : is_submonoid (closure s) :=
{ one_mem := in_closure.one s, mul_mem := assume a b, in_closure.mul }
@[to_additive]
theorem subset_closure {s : set α} : s ⊆ closure s :=
assume a, in_closure.basic
@[to_additive]
theorem closure_subset {s t : set α} [is_submonoid t] (h : s ⊆ t) : closure s ⊆ t :=
assume a ha, by induction ha; simp [h _, *, is_submonoid.one_mem, is_submonoid.mul_mem]
@[to_additive]
theorem closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t :=
closure_subset $ set.subset.trans h subset_closure
@[to_additive]
theorem closure_singleton {x : α} : closure ({x} : set α) = powers x :=
set.eq_of_subset_of_subset (closure_subset $ set.singleton_subset_iff.2 $ powers.self_mem) $
is_submonoid.power_subset $ set.singleton_subset_iff.1 $ subset_closure
@[to_additive]
lemma image_closure {β : Type*} [monoid β] (f : α → β) [is_monoid_hom f] (s : set α) :
f '' closure s = closure (f '' s) :=
le_antisymm
begin
rintros _ ⟨x, hx, rfl⟩,
apply in_closure.rec_on hx; intros,
{ solve_by_elim [subset_closure, set.mem_image_of_mem] },
{ rw [is_monoid_hom.map_one f], apply is_submonoid.one_mem },
{ rw [is_monoid_hom.map_mul f], solve_by_elim [is_submonoid.mul_mem] }
end
(closure_subset $ set.image_subset _ subset_closure)
@[to_additive]
theorem exists_list_of_mem_closure {s : set α} {a : α} (h : a ∈ closure s) :
(∃l:list α, (∀x∈l, x ∈ s) ∧ l.prod = a) :=
begin
induction h,
case in_closure.basic : a ha { existsi ([a]), simp [ha] },
case in_closure.one { existsi ([]), simp },
case in_closure.mul : a b _ _ ha hb {
rcases ha with ⟨la, ha, eqa⟩,
rcases hb with ⟨lb, hb, eqb⟩,
existsi (la ++ lb),
simp [eqa.symm, eqb.symm, or_imp_distrib],
exact assume a, ⟨ha a, hb a⟩
}
end
@[to_additive]
theorem mem_closure_union_iff {α : Type*} [comm_monoid α] {s t : set α} {x : α} :
x ∈ closure (s ∪ t) ↔ ∃ y ∈ closure s, ∃ z ∈ closure t, y * z = x :=
⟨λ hx, let ⟨L, HL1, HL2⟩ := exists_list_of_mem_closure hx in HL2 ▸
list.rec_on L (λ _, ⟨1, is_submonoid.one_mem _, 1, is_submonoid.one_mem _, mul_one _⟩)
(λ hd tl ih HL1, let ⟨y, hy, z, hz, hyzx⟩ := ih (list.forall_mem_of_forall_mem_cons HL1) in
or.cases_on (HL1 hd $ list.mem_cons_self _ _)
(λ hs, ⟨hd * y, is_submonoid.mul_mem (subset_closure hs) hy, z, hz, by rw [mul_assoc, list.prod_cons, ← hyzx]; refl⟩)
(λ ht, ⟨y, hy, z * hd, is_submonoid.mul_mem hz (subset_closure ht), by rw [← mul_assoc, list.prod_cons, ← hyzx, mul_comm hd]; refl⟩)) HL1,
λ ⟨y, hy, z, hz, hyzx⟩, hyzx ▸ is_submonoid.mul_mem (closure_mono (set.subset_union_left _ _) hy)
(closure_mono (set.subset_union_right _ _) hz)⟩
end monoid
|
f1a93a9694fea61da801d5cef09501c67e221119 | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/topology/instances/real.lean | 08e93c9936f88306204932dfa236038a62019aee | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,962 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import topology.metric_space.basic
import topology.algebra.uniform_group
import topology.algebra.ring
import ring_theory.subring
import group_theory.archimedean
import algebra.periodic
/-!
# Topological properties of ℝ
-/
noncomputable theory
open classical set filter topological_space metric
open_locale classical topological_space filter uniformity interval
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
instance : metric_space ℚ :=
metric_space.induced coe rat.cast_injective real.metric_space
theorem rat.dist_eq (x y : ℚ) : dist x y = abs (x - y) := rfl
@[norm_cast, simp] lemma rat.dist_cast (x y : ℚ) : dist (x : ℝ) y = dist x y := rfl
namespace int
lemma uniform_embedding_coe_real : uniform_embedding (coe : ℤ → ℝ) :=
{ comap_uniformity :=
begin
refine le_antisymm (le_principal_iff.2 _) (@refl_le_uniformity ℤ $
uniform_space.comap coe (infer_instance : uniform_space ℝ)),
refine (uniformity_basis_dist.comap _).mem_iff.2 ⟨1, zero_lt_one, _⟩,
rintro ⟨a, b⟩ (h : abs (a - b : ℝ) < 1),
norm_cast at h,
erw [@int.lt_add_one_iff _ 0, abs_nonpos_iff, sub_eq_zero] at h, assumption
end,
inj := int.cast_injective }
instance : metric_space ℤ := int.uniform_embedding_coe_real.comap_metric_space _
theorem dist_eq (x y : ℤ) : dist x y = abs (x - y) := rfl
@[norm_cast, simp] theorem dist_cast_real (x y : ℤ) : dist (x : ℝ) y = dist x y := rfl
@[norm_cast, simp] theorem dist_cast_rat (x y : ℤ) : dist (x : ℚ) y = dist x y :=
by rw [← int.dist_cast_real, ← rat.dist_cast]; congr' 1; norm_cast
theorem preimage_ball (x : ℤ) (r : ℝ) : coe ⁻¹' (ball (x : ℝ) r) = ball x r := rfl
theorem preimage_closed_ball (x : ℤ) (r : ℝ) :
coe ⁻¹' (closed_ball (x : ℝ) r) = closed_ball x r := rfl
theorem ball_eq (x : ℤ) (r : ℝ) : ball x r = Ioo ⌊↑x - r⌋ ⌈↑x + r⌉ :=
by rw [← preimage_ball, real.ball_eq, preimage_Ioo]
theorem closed_ball_eq (x : ℤ) (r : ℝ) : closed_ball x r = Icc ⌈↑x - r⌉ ⌊↑x + r⌋ :=
by rw [← preimage_closed_ball, real.closed_ball_eq, preimage_Icc]
instance : proper_space ℤ :=
⟨ begin
intros x r,
rw closed_ball_eq,
exact (set.Icc_ℤ_finite _ _).is_compact,
end ⟩
end int
theorem uniform_continuous_of_rat : uniform_continuous (coe : ℚ → ℝ) :=
uniform_continuous_comap
theorem uniform_embedding_of_rat : uniform_embedding (coe : ℚ → ℝ) :=
uniform_embedding_comap rat.cast_injective
theorem dense_embedding_of_rat : dense_embedding (coe : ℚ → ℝ) :=
uniform_embedding_of_rat.dense_embedding $
λ x, mem_closure_iff_nhds.2 $ λ t ht,
let ⟨ε,ε0, hε⟩ := metric.mem_nhds_iff.1 ht in
let ⟨q, h⟩ := exists_rat_near x ε0 in
⟨_, hε (mem_ball'.2 h), q, rfl⟩
theorem embedding_of_rat : embedding (coe : ℚ → ℝ) := dense_embedding_of_rat.to_embedding
theorem continuous_of_rat : continuous (coe : ℚ → ℝ) := uniform_continuous_of_rat.continuous
theorem real.uniform_continuous_add : uniform_continuous (λp : ℝ × ℝ, p.1 + p.2) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abs ε0 in
⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ h₁ h₂⟩
-- TODO(Mario): Find a way to use rat_add_continuous_lemma
theorem rat.uniform_continuous_add : uniform_continuous (λp : ℚ × ℚ, p.1 + p.2) :=
uniform_embedding_of_rat.to_uniform_inducing.uniform_continuous_iff.2 $ by simp [(∘)]; exact
real.uniform_continuous_add.comp ((uniform_continuous_of_rat.comp uniform_continuous_fst).prod_mk
(uniform_continuous_of_rat.comp uniform_continuous_snd))
theorem real.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℝ _) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h,
by rw dist_comm at h; simpa [real.dist_eq] using h⟩
theorem rat.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℚ _) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h,
by rw dist_comm at h; simpa [rat.dist_eq] using h⟩
instance : uniform_add_group ℝ :=
uniform_add_group.mk' real.uniform_continuous_add real.uniform_continuous_neg
instance : uniform_add_group ℚ :=
uniform_add_group.mk' rat.uniform_continuous_add rat.uniform_continuous_neg
-- short-circuit type class inference
instance : topological_add_group ℝ := by apply_instance
instance : topological_add_group ℚ := by apply_instance
instance : order_topology ℚ :=
induced_order_topology _ (λ x y, rat.cast_lt) (@exists_rat_btwn _ _ _)
instance : proper_space ℝ :=
{ compact_ball := λx r, by { rw real.closed_ball_eq, apply is_compact_Icc } }
instance : second_countable_topology ℝ := second_countable_of_proper
lemma real.is_topological_basis_Ioo_rat :
@is_topological_basis ℝ _ (⋃(a b : ℚ) (h : a < b), {Ioo a b}) :=
is_topological_basis_of_open_of_nhds
(by simp [is_open_Ioo] {contextual:=tt})
(assume a v hav hv,
let ⟨l, u, ⟨hl, hu⟩, h⟩ := mem_nhds_iff_exists_Ioo_subset.mp (is_open.mem_nhds hv hav),
⟨q, hlq, hqa⟩ := exists_rat_btwn hl,
⟨p, hap, hpu⟩ := exists_rat_btwn hu in
⟨Ioo q p,
by { simp only [mem_Union], exact ⟨q, p, rat.cast_lt.1 $ hqa.trans hap, rfl⟩ },
⟨hqa, hap⟩, assume a' ⟨hqa', ha'p⟩, h ⟨hlq.trans hqa', ha'p.trans hpu⟩⟩)
/- TODO(Mario): Prove that these are uniform isomorphisms instead of uniform embeddings
lemma uniform_embedding_add_rat {r : ℚ} : uniform_embedding (λp:ℚ, p + r) :=
_
lemma uniform_embedding_mul_rat {q : ℚ} (hq : q ≠ 0) : uniform_embedding ((*) q) :=
_ -/
lemma real.mem_closure_iff {s : set ℝ} {x : ℝ} :
x ∈ closure s ↔ ∀ ε > 0, ∃ y ∈ s, abs (y - x) < ε :=
by simp [mem_closure_iff_nhds_basis nhds_basis_ball, real.dist_eq]
lemma real.uniform_continuous_inv (s : set ℝ) {r : ℝ} (r0 : 0 < r) (H : ∀ x ∈ s, r ≤ abs x) :
uniform_continuous (λp:s, p.1⁻¹) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abs ε0 r0 in
⟨δ, δ0, λ a b h, Hδ (H _ a.2) (H _ b.2) h⟩
lemma real.uniform_continuous_abs : uniform_continuous (abs : ℝ → ℝ) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
⟨ε, ε0, λ a b, lt_of_le_of_lt (abs_abs_sub_abs_le_abs_sub _ _)⟩
lemma rat.uniform_continuous_abs : uniform_continuous (abs : ℚ → ℚ) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
⟨ε, ε0, λ a b h, lt_of_le_of_lt
(by simpa [rat.dist_eq] using abs_abs_sub_abs_le_abs_sub _ _) h⟩
lemma real.tendsto_inv {r : ℝ} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (𝓝 r) (𝓝 r⁻¹) :=
by rw ← abs_pos at r0; exact
tendsto_of_uniform_continuous_subtype
(real.uniform_continuous_inv {x | abs r / 2 < abs x} (half_pos r0) (λ x h, le_of_lt h))
(is_open.mem_nhds ((is_open_lt' (abs r / 2)).preimage continuous_abs) (half_lt_self r0))
lemma real.continuous_inv : continuous (λa:{r:ℝ // r ≠ 0}, a.val⁻¹) :=
continuous_iff_continuous_at.mpr $ assume ⟨r, hr⟩,
tendsto.comp (real.tendsto_inv hr) (continuous_iff_continuous_at.mp continuous_subtype_val _)
lemma real.continuous.inv [topological_space α] {f : α → ℝ} (h : ∀a, f a ≠ 0) (hf : continuous f) :
continuous (λa, (f a)⁻¹) :=
show continuous ((has_inv.inv ∘ @subtype.val ℝ (λr, r ≠ 0)) ∘ λa, ⟨f a, h a⟩),
from real.continuous_inv.comp (continuous_subtype_mk _ hf)
lemma real.uniform_continuous_mul_const {x : ℝ} : uniform_continuous ((*) x) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, begin
cases no_top (abs x) with y xy,
have y0 := lt_of_le_of_lt (abs_nonneg _) xy,
refine ⟨_, div_pos ε0 y0, λ a b h, _⟩,
rw [real.dist_eq, ← mul_sub, abs_mul, ← mul_div_cancel' ε (ne_of_gt y0)],
exact mul_lt_mul' (le_of_lt xy) h (abs_nonneg _) y0
end
lemma real.uniform_continuous_mul (s : set (ℝ × ℝ))
{r₁ r₂ : ℝ} (H : ∀ x ∈ s, abs (x : ℝ × ℝ).1 < r₁ ∧ abs x.2 < r₂) :
uniform_continuous (λp:s, p.1.1 * p.1.2) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abs ε0 in
⟨δ, δ0, λ a b h,
let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ (H _ a.2).1 (H _ b.2).2 h₁ h₂⟩
protected lemma real.continuous_mul : continuous (λp : ℝ × ℝ, p.1 * p.2) :=
continuous_iff_continuous_at.2 $ λ ⟨a₁, a₂⟩,
tendsto_of_uniform_continuous_subtype
(real.uniform_continuous_mul
({x | abs x < abs a₁ + 1}.prod {x | abs x < abs a₂ + 1})
(λ x, id))
(is_open.mem_nhds
(((is_open_gt' (abs a₁ + 1)).preimage continuous_abs).prod
((is_open_gt' (abs a₂ + 1)).preimage continuous_abs ))
⟨lt_add_one (abs a₁), lt_add_one (abs a₂)⟩)
instance : topological_ring ℝ :=
{ continuous_mul := real.continuous_mul, ..real.topological_add_group }
lemma rat.continuous_mul : continuous (λp : ℚ × ℚ, p.1 * p.2) :=
embedding_of_rat.continuous_iff.2 $ by simp [(∘)]; exact
real.continuous_mul.comp ((continuous_of_rat.comp continuous_fst).prod_mk
(continuous_of_rat.comp continuous_snd))
instance : topological_ring ℚ :=
{ continuous_mul := rat.continuous_mul, ..rat.topological_add_group }
theorem real.ball_eq_Ioo (x ε : ℝ) : ball x ε = Ioo (x - ε) (x + ε) :=
set.ext $ λ y, by rw [mem_ball, real.dist_eq,
abs_sub_lt_iff, sub_lt_iff_lt_add', and_comm, sub_lt]; refl
theorem real.Ioo_eq_ball (x y : ℝ) : Ioo x y = ball ((x + y) / 2) ((y - x) / 2) :=
by rw [real.ball_eq_Ioo, ← sub_div, add_comm, ← sub_add,
add_sub_cancel', add_self_div_two, ← add_div,
add_assoc, add_sub_cancel'_right, add_self_div_two]
instance : complete_space ℝ :=
begin
apply complete_of_cauchy_seq_tendsto,
intros u hu,
let c : cau_seq ℝ abs := ⟨u, metric.cauchy_seq_iff'.1 hu⟩,
refine ⟨c.lim, λ s h, _⟩,
rcases metric.mem_nhds_iff.1 h with ⟨ε, ε0, hε⟩,
have := c.equiv_lim ε ε0,
simp only [mem_map, mem_at_top_sets, mem_set_of_eq],
refine this.imp (λ N hN n hn, hε (hN n hn))
end
lemma real.totally_bounded_ball (x ε : ℝ) : totally_bounded (ball x ε) :=
by rw real.ball_eq_Ioo; apply totally_bounded_Ioo
lemma rat.totally_bounded_Icc (a b : ℚ) : totally_bounded (Icc a b) :=
begin
have := totally_bounded_preimage uniform_embedding_of_rat (totally_bounded_Icc a b),
rwa (set.ext (λ q, _) : Icc _ _ = _), simp
end
section
lemma closure_of_rat_image_lt {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q < x}) = {r | ↑q ≤ r} :=
subset.antisymm
((is_closed_ge' _).closure_subset_iff.2
(image_subset_iff.2 $ λ p h, le_of_lt $ (@rat.cast_lt ℝ _ _ _).2 h)) $
λ x hx, mem_closure_iff_nhds.2 $ λ t ht,
let ⟨ε, ε0, hε⟩ := metric.mem_nhds_iff.1 ht in
let ⟨p, h₁, h₂⟩ := exists_rat_btwn ((lt_add_iff_pos_right x).2 ε0) in
⟨_, hε (show abs _ < _,
by rwa [abs_of_nonneg (le_of_lt $ sub_pos.2 h₁), sub_lt_iff_lt_add']),
p, rat.cast_lt.1 (@lt_of_le_of_lt ℝ _ _ _ _ hx h₁), rfl⟩
/- TODO(Mario): Put these back only if needed later
lemma closure_of_rat_image_le_eq {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q ≤ x}) = {r | ↑q ≤ r} :=
_
lemma closure_of_rat_image_le_le_eq {a b : ℚ} (hab : a ≤ b) :
closure (of_rat '' {q:ℚ | a ≤ q ∧ q ≤ b}) = {r:ℝ | of_rat a ≤ r ∧ r ≤ of_rat b} :=
_-/
lemma real.bounded_iff_bdd_below_bdd_above {s : set ℝ} : bounded s ↔ bdd_below s ∧ bdd_above s :=
⟨begin
assume bdd,
rcases (bounded_iff_subset_ball 0).1 bdd with ⟨r, hr⟩, -- hr : s ⊆ closed_ball 0 r
rw real.closed_ball_eq at hr, -- hr : s ⊆ Icc (0 - r) (0 + r)
exact ⟨bdd_below_Icc.mono hr, bdd_above_Icc.mono hr⟩
end,
begin
intro h,
rcases bdd_below_bdd_above_iff_subset_Icc.1 h with ⟨m, M, I : s ⊆ Icc m M⟩,
exact (bounded_Icc m M).subset I
end⟩
lemma real.subset_Icc_Inf_Sup_of_bounded {s : set ℝ} (h : bounded s) :
s ⊆ Icc (Inf s) (Sup s) :=
subset_Icc_cInf_cSup (real.bounded_iff_bdd_below_bdd_above.1 h).1
(real.bounded_iff_bdd_below_bdd_above.1 h).2
lemma real.image_Icc {f : ℝ → ℝ} {a b : ℝ} (hab : a ≤ b) (h : continuous_on f $ Icc a b) :
f '' Icc a b = Icc (Inf $ f '' Icc a b) (Sup $ f '' Icc a b) :=
eq_Icc_of_connected_compact ⟨(nonempty_Icc.2 hab).image f, is_preconnected_Icc.image f h⟩
(is_compact_Icc.image_of_continuous_on h)
lemma real.image_interval_eq_Icc {f : ℝ → ℝ} {a b : ℝ} (h : continuous_on f $ [a, b]) :
f '' [a, b] = Icc (Inf (f '' [a, b])) (Sup (f '' [a, b])) :=
begin
cases le_total a b with h2 h2,
{ simp_rw [interval_of_le h2] at h ⊢, exact real.image_Icc h2 h },
{ simp_rw [interval_of_ge h2] at h ⊢, exact real.image_Icc h2 h },
end
lemma real.image_interval {f : ℝ → ℝ} {a b : ℝ} (h : continuous_on f $ [a, b]) :
f '' [a, b] = [Inf (f '' [a, b]), Sup (f '' [a, b])] :=
begin
refine (real.image_interval_eq_Icc h).trans (interval_of_le _).symm,
rw [real.image_interval_eq_Icc h],
exact real.Inf_le_Sup _ bdd_below_Icc bdd_above_Icc
end
lemma real.interval_subset_image_interval {f : ℝ → ℝ} {a b x y : ℝ}
(h : continuous_on f [a, b]) (hx : x ∈ [a, b]) (hy : y ∈ [a, b]) :
[f x, f y] ⊆ f '' [a, b] :=
begin
rw [real.image_interval h, interval_subset_interval_iff_mem, ← real.image_interval h],
exact ⟨mem_image_of_mem f hx, mem_image_of_mem f hy⟩
end
end
section periodic
namespace function
lemma periodic.compact_of_continuous' [topological_space α] {f : ℝ → α} {c : ℝ}
(hp : periodic f c) (hc : 0 < c) (hf : continuous f) :
is_compact (range f) :=
begin
convert is_compact_Icc.image hf,
ext x,
refine ⟨_, mem_range_of_mem_image f (Icc 0 c)⟩,
rintros ⟨y, h1⟩,
obtain ⟨z, hz, h2⟩ := hp.exists_mem_Ico hc y,
exact ⟨z, mem_Icc_of_Ico hz, h2.symm.trans h1⟩,
end
/-- A continuous, periodic function has compact range. -/
lemma periodic.compact_of_continuous [topological_space α] {f : ℝ → α} {c : ℝ}
(hp : periodic f c) (hc : c ≠ 0) (hf : continuous f) :
is_compact (range f) :=
begin
cases lt_or_gt_of_ne hc with hneg hpos,
exacts [hp.neg.compact_of_continuous' (neg_pos.mpr hneg) hf, hp.compact_of_continuous' hpos hf],
end
/-- A continuous, periodic function is bounded. -/
lemma periodic.bounded_of_continuous [pseudo_metric_space α] {f : ℝ → α} {c : ℝ}
(hp : periodic f c) (hc : c ≠ 0) (hf : continuous f) :
bounded (range f) :=
(hp.compact_of_continuous hc hf).bounded
end function
end periodic
section subgroups
/-- Given a nontrivial subgroup `G ⊆ ℝ`, if `G ∩ ℝ_{>0}` has no minimum then `G` is dense. -/
lemma real.subgroup_dense_of_no_min {G : add_subgroup ℝ} {g₀ : ℝ} (g₀_in : g₀ ∈ G) (g₀_ne : g₀ ≠ 0)
(H' : ¬ ∃ a : ℝ, is_least {g : ℝ | g ∈ G ∧ 0 < g} a) :
dense (G : set ℝ) :=
begin
let G_pos := {g : ℝ | g ∈ G ∧ 0 < g},
push_neg at H',
intros x,
suffices : ∀ ε > (0 : ℝ), ∃ g ∈ G, abs (x - g) < ε,
by simpa only [real.mem_closure_iff, abs_sub_comm],
intros ε ε_pos,
obtain ⟨g₁, g₁_in, g₁_pos⟩ : ∃ g₁ : ℝ, g₁ ∈ G ∧ 0 < g₁,
{ cases lt_or_gt_of_ne g₀_ne with Hg₀ Hg₀,
{ exact ⟨-g₀, G.neg_mem g₀_in, neg_pos.mpr Hg₀⟩ },
{ exact ⟨g₀, g₀_in, Hg₀⟩ } },
obtain ⟨a, ha⟩ : ∃ a, is_glb G_pos a :=
⟨Inf G_pos, is_glb_cInf ⟨g₁, g₁_in, g₁_pos⟩ ⟨0, λ _ hx, le_of_lt hx.2⟩⟩,
have a_notin : a ∉ G_pos,
{ intros H,
exact H' a ⟨H, ha.1⟩ },
obtain ⟨g₂, g₂_in, g₂_pos, g₂_lt⟩ : ∃ g₂ : ℝ, g₂ ∈ G ∧ 0 < g₂ ∧ g₂ < ε,
{ obtain ⟨b, hb, hb', hb''⟩ := ha.exists_between_self_add' a_notin ε_pos,
obtain ⟨c, hc, hc', hc''⟩ := ha.exists_between_self_add' a_notin (sub_pos.2 hb'),
refine ⟨b - c, G.sub_mem hb.1 hc.1, _, _⟩ ;
linarith },
refine ⟨floor (x/g₂) * g₂, _, _⟩,
{ exact add_subgroup.int_mul_mem _ g₂_in },
{ rw abs_of_nonneg (sub_floor_div_mul_nonneg x g₂_pos),
linarith [sub_floor_div_mul_lt x g₂_pos] }
end
/-- Subgroups of `ℝ` are either dense or cyclic. See `real.subgroup_dense_of_no_min` and
`subgroup_cyclic_of_min` for more precise statements. -/
lemma real.subgroup_dense_or_cyclic (G : add_subgroup ℝ) :
dense (G : set ℝ) ∨ ∃ a : ℝ, G = add_subgroup.closure {a} :=
begin
cases add_subgroup.bot_or_exists_ne_zero G with H H,
{ right,
use 0,
rw [H, add_subgroup.closure_singleton_zero] },
{ let G_pos := {g : ℝ | g ∈ G ∧ 0 < g},
by_cases H' : ∃ a, is_least G_pos a,
{ right,
rcases H' with ⟨a, ha⟩,
exact ⟨a, add_subgroup.cyclic_of_min ha⟩ },
{ left,
rcases H with ⟨g₀, g₀_in, g₀_ne⟩,
exact real.subgroup_dense_of_no_min g₀_in g₀_ne H' } }
end
end subgroups
|
e6aaf58c7e68868b91b1dd9f229fe63f1ffb12f3 | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/data/polynomial/denoms_clearable.lean | f3398f6d7cbe059efcd419ccd63a78795412c5d9 | [
"Apache-2.0"
] | permissive | waynemunro/mathlib | e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552 | 065a70810b5480d584033f7bbf8e0409480c2118 | refs/heads/master | 1,693,417,182,397 | 1,634,644,781,000 | 1,634,644,781,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,685 | lean | /-
Copyright (c) 2020 Damiano Testa. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Damiano Testa
-/
import data.polynomial.erase_lead
import data.polynomial.eval
/-!
# Denominators of evaluation of polynomials at ratios
Let `i : R → K` be a homomorphism of semirings. Assume that `K` is commutative. If `a` and
`b` are elements of `R` such that `i b ∈ K` is invertible, then for any polynomial
`f ∈ polynomial R` the "mathematical" expression `b ^ f.nat_degree * f (a / b) ∈ K` is in
the image of the homomorphism `i`.
-/
open polynomial finset
section denoms_clearable
variables {R K : Type*} [semiring R] [comm_semiring K] {i : R →+* K}
variables {a b : R} {bi : K}
-- TODO: use hypothesis (ub : is_unit (i b)) to work with localizations.
/-- `denoms_clearable` formalizes the property that `b ^ N * f (a / b)`
does not have denominators, if the inequality `f.nat_degree ≤ N` holds.
The definition asserts the existence of an element `D` of `R` and an
element `bi = 1 / i b` of `K` such that clearing the denominators of
the fraction equals `i D`.
-/
def denoms_clearable (a b : R) (N : ℕ) (f : polynomial R) (i : R →+* K) : Prop :=
∃ (D : R) (bi : K), bi * i b = 1 ∧ i D = i b ^ N * eval (i a * bi) (f.map i)
lemma denoms_clearable_zero (N : ℕ) (a : R) (bu : bi * i b = 1) :
denoms_clearable a b N 0 i :=
⟨0, bi, bu, by simp only [eval_zero, ring_hom.map_zero, mul_zero, map_zero]⟩
lemma denoms_clearable_C_mul_X_pow {N : ℕ} (a : R) (bu : bi * i b = 1) {n : ℕ} (r : R)
(nN : n ≤ N) : denoms_clearable a b N (C r * X ^ n) i :=
begin
refine ⟨r * a ^ n * b ^ (N - n), bi, bu, _⟩,
rw [C_mul_X_pow_eq_monomial, map_monomial, ← C_mul_X_pow_eq_monomial, eval_mul, eval_pow, eval_C],
rw [ring_hom.map_mul, ring_hom.map_mul, ring_hom.map_pow, ring_hom.map_pow, eval_X, mul_comm],
rw [← nat.sub_add_cancel nN] {occs := occurrences.pos [2]},
rw [pow_add, mul_assoc, mul_comm (i b ^ n), mul_pow, mul_assoc, mul_assoc (i a ^ n), ← mul_pow],
rw [bu, one_pow, mul_one],
end
lemma denoms_clearable.add {N : ℕ} {f g : polynomial R} :
denoms_clearable a b N f i → denoms_clearable a b N g i → denoms_clearable a b N (f + g) i :=
λ ⟨Df, bf, bfu, Hf⟩ ⟨Dg, bg, bgu, Hg⟩, ⟨Df + Dg, bf, bfu,
begin
rw [ring_hom.map_add, polynomial.map_add, eval_add, mul_add, Hf, Hg],
congr,
refine @inv_unique K _ (i b) bg bf _ _;
rwa mul_comm,
end ⟩
lemma denoms_clearable_of_nat_degree_le (N : ℕ) (a : R) (bu : bi * i b = 1) :
∀ (f : polynomial R), f.nat_degree ≤ N → denoms_clearable a b N f i :=
induction_with_nat_degree_le N
(denoms_clearable_zero N a bu)
(λ N_1 r r0, denoms_clearable_C_mul_X_pow a bu r)
(λ f g fN gN df dg, df.add dg)
/-- If `i : R → K` is a ring homomorphism, `f` is a polynomial with coefficients in `R`,
`a, b` are elements of `R`, with `i b` invertible, then there is a `D ∈ R` such that
`b ^ f.nat_degree * f (a / b)` equals `i D`. -/
theorem denoms_clearable_nat_degree
(i : R →+* K) (f : polynomial R) (a : R) (bu : bi * i b = 1) :
denoms_clearable a b f.nat_degree f i :=
denoms_clearable_of_nat_degree_le f.nat_degree a bu f le_rfl
end denoms_clearable
open ring_hom
/-- Evaluating a polynomial with integer coefficients at a rational number and clearing
denominators, yields a number greater than or equal to one. The target can be any
`linear_ordered_field K`.
The assumption on `K` could be weakened to `linear_ordered_comm_ring` assuming that the
image of the denominator is invertible in `K`. -/
lemma one_le_pow_mul_abs_eval_div {K : Type*} [linear_ordered_field K] {f : polynomial ℤ}
{a b : ℤ} (b0 : 0 < b) (fab : eval ((a : K) / b) (f.map (algebra_map ℤ K)) ≠ 0) :
(1 : K) ≤ b ^ f.nat_degree * |eval ((a : K) / b) (f.map (algebra_map ℤ K))| :=
begin
obtain ⟨ev, bi, bu, hF⟩ := @denoms_clearable_nat_degree _ _ _ _ b _ (algebra_map ℤ K)
f a (by { rw [eq_int_cast, one_div_mul_cancel], rw [int.cast_ne_zero], exact (b0.ne.symm) }),
obtain Fa := congr_arg abs hF,
rw [eq_one_div_of_mul_eq_one_left bu, eq_int_cast, eq_int_cast, abs_mul] at Fa,
rw [abs_of_pos (pow_pos (int.cast_pos.mpr b0) _ : 0 < (b : K) ^ _), one_div, eq_int_cast] at Fa,
rw [div_eq_mul_inv, ← Fa, ← int.cast_abs, ← int.cast_one, int.cast_le],
refine int.le_of_lt_add_one ((lt_add_iff_pos_left 1).mpr (abs_pos.mpr (λ F0, fab _))),
rw [eq_one_div_of_mul_eq_one_left bu, F0, one_div, eq_int_cast, int.cast_zero, zero_eq_mul] at hF,
cases hF with hF hF,
{ exact (not_le.mpr b0 (le_of_eq (int.cast_eq_zero.mp (pow_eq_zero hF)))).elim },
{ rwa div_eq_mul_inv }
end
|
0266857e917da252fddf594edf5817771b7bdb58 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/327.lean | c3cb16d1366b3aaa0b638a1a22ee026d9b2bd2ef | [
"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 | 403 | lean | open Lean
def foo : Name := `foo
def isFoo : Name → Bool
| ``foo => true
| _ => false
theorem ex1 : isFoo `foo = true := rfl
theorem ex2 : isFoo `bla = false := rfl
def Bla.boo : Name := `boo
open Bla
def isBoo : Name → Bool
| ``boo => true
| _ => false
theorem ex3 : isBoo `Bla.boo = true := rfl
theorem ex4 : isBoo ``boo = true := rfl
theorem ex5 : ``boo = `Bla.boo := rfl
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.